From 6465fd362bd58773ce7483c355938db940226982 Mon Sep 17 00:00:00 2001 From: James O'Leary Date: Sun, 22 Feb 2026 08:24:20 -0800 Subject: [PATCH 01/69] fix: use globalThis instead of window for web worker compatibility window is undefined in web worker contexts (DedicatedWorkerGlobalScope, SharedWorkerGlobalScope, ServiceWorkerGlobalScope). globalThis resolves to the correct global object in all contexts: window on main thread, self in workers, global in Node.js. This fixes TypeError when using webcrypto APIs inside Squadron/isolate web workers. --- lib/src/crypto_subtle.dart | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/lib/src/crypto_subtle.dart b/lib/src/crypto_subtle.dart index 55631943b..c4484cbf5 100644 --- a/lib/src/crypto_subtle.dart +++ b/lib/src/crypto_subtle.dart @@ -44,16 +44,23 @@ Uint8List bigIntToUint8ListBigInteger(BigInt integer) { throw UnimplementedError('Only supports 65537 and 3 for now'); } -/// The `window` object. -@JS() -external JSWindow get window; - -/// https://developer.mozilla.org/en-US/docs/Web/API/Window -extension type JSWindow(JSObject _) implements JSObject { +/// The global scope object. +/// +/// Uses `globalThis` instead of `window` so that this works in both the main +/// thread (`window`) and web workers (`self`/`DedicatedWorkerGlobalScope`), +/// where `window` is undefined. +@JS('globalThis') +external JSGlobalScope get _globalScope; + +/// https://developer.mozilla.org/en-US/docs/Web/API/globalThis +extension type JSGlobalScope(JSObject _) implements JSObject { /// https://developer.mozilla.org/en-US/docs/Web/API/crypto_property external JSCrypto get crypto; } +/// Backward-compatible accessor — all call sites use `window.crypto`. +JSGlobalScope get window => _globalScope; + /// The `window.crypto` object. /// /// https://www.w3.org/TR/WebCryptoAPI/#crypto-interface From b8ba8e9c66ce8adaf756226d718f64da5163002c Mon Sep 17 00:00:00 2001 From: James O'Leary Date: Sun, 22 Feb 2026 08:24:20 -0800 Subject: [PATCH 02/69] fix: use globalThis instead of window for web worker compatibility window is undefined in web worker contexts (DedicatedWorkerGlobalScope, SharedWorkerGlobalScope, ServiceWorkerGlobalScope). globalThis resolves to the correct global object in all contexts: window on main thread, self in workers, global in Node.js. This fixes TypeError when using webcrypto APIs inside Squadron/isolate web workers. --- lib/src/crypto_subtle.dart | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/lib/src/crypto_subtle.dart b/lib/src/crypto_subtle.dart index 8e51bb29d..0616c0fa0 100644 --- a/lib/src/crypto_subtle.dart +++ b/lib/src/crypto_subtle.dart @@ -44,12 +44,16 @@ Uint8List bigIntToUint8ListBigInteger(BigInt integer) { throw UnimplementedError('Only supports 65537 and 3 for now'); } -/// The `window` object. -@JS() -external JSWindow get window; - -/// https://developer.mozilla.org/en-US/docs/Web/API/Window -extension type JSWindow(JSObject _) implements JSObject { +/// The global scope object. +/// +/// Uses `globalThis` instead of `window` so that this works in both the main +/// thread (`window`) and web workers (`self`/`DedicatedWorkerGlobalScope`), +/// where `window` is undefined. +@JS('globalThis') +external JSGlobalScope get _globalScope; + +/// https://developer.mozilla.org/en-US/docs/Web/API/globalThis +extension type JSGlobalScope(JSObject _) implements JSObject { /// https://developer.mozilla.org/en-US/docs/Web/API/Window/isSecureContext external bool get isSecureContext; @@ -57,6 +61,9 @@ extension type JSWindow(JSObject _) implements JSObject { external JSCrypto get crypto; } +/// Backward-compatible accessor — all call sites use `window.crypto`. +JSGlobalScope get window => _globalScope; + /// The `window.crypto` object. /// /// https://www.w3.org/TR/WebCryptoAPI/#crypto-interface From a6242c6164ad1b92759e0396620ed183edf73f42 Mon Sep 17 00:00:00 2001 From: James O'Leary Date: Tue, 1 Sep 2026 14:47:24 -0700 Subject: [PATCH 03/69] perf: cache native webcrypto builds --- CHANGELOG.md | 4 + hook/build.dart | 463 +++++++++++++++++++++++++++++++++++++++++------- pubspec.yaml | 7 +- 3 files changed, 408 insertions(+), 66 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 660b3c05f..822cbf49b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,8 @@ # 0.6.2-wip +* Synced with the latest upstream `google/webcrypto.dart` changes. +* Added a content-addressed native build cache under `~/.cache/webcrypto.dart` + to avoid recompiling BoringSSL when the native inputs are unchanged. +* Preserved the `globalThis` web-worker compatibility fix. * Fixed JS interop to enable WebAssembly. # 0.6.1 diff --git a/hook/build.dart b/hook/build.dart index fcbf862bb..ab610cfbe 100644 --- a/hook/build.dart +++ b/hook/build.dart @@ -1,103 +1,438 @@ -// Copyright 2025 Google LLC +// Copyright 2026 The webcrypto.dart authors. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +// https://www.apache.org/licenses/LICENSE-2.0 + +/// Builds package:webcrypto's vendored BoringSSL wrapper as a Dart Native +/// Asset on every supported native target. +/// +/// Web builds do not request code assets, so this hook intentionally does +/// nothing for them. Native builds use a content-addressed, process-locked +/// cache because compiling BoringSSL for every hook invocation is expensive. +library; +import 'dart:convert'; import 'dart:io'; +import 'dart:typed_data'; import 'package:code_assets/code_assets.dart'; +import 'package:crypto/crypto.dart'; import 'package:hooks/hooks.dart'; +import 'package:logging/logging.dart'; import 'package:native_toolchain_cmake/native_toolchain_cmake.dart'; +import 'package:path/path.dart' as p; const _assetName = 'webcrypto.dart'; +const _cacheSchema = 'webcrypto-native-assets-v3'; -Future main(List args) async { +void main(List args) async { await build(args, (input, output) async { - // Skip build for non-code targets (e.g. web builds via flutter drive). - if (!input.config.buildCodeAssets) { - stdout.writeln( - 'webcrypto: skipping native asset build (code assets not requested).', - ); - return; + if (!input.config.buildCodeAssets) return; + + final code = input.config.code; + _validateTarget(code.targetOS, code.targetArchitecture); + + final logger = Logger.root + ..level = Level.ALL + ..onRecord.listen((record) => stderr.writeln(record.message)); + + final nativeInputs = await _collectNativeInputs(input.packageRoot); + output.dependencies.addAll(nativeInputs.map((input) => input.uri)); + + final defines = { + 'CMAKE_BUILD_TYPE': 'Release', + 'CMAKE_POSITION_INDEPENDENT_CODE': 'ON', + }; + final buildKey = await _computeBuildKey( + code: code, + defines: defines, + inputs: nativeInputs, + ); + final libraryFileName = _libraryFileName(code.targetOS); + final cacheDir = _cacheDirectory(buildKey); + final cachedLibrary = File(p.join(cacheDir.path, libraryFileName)); + final cachedDigest = File('${cachedLibrary.path}.sha256'); + final lockFile = File('${cacheDir.path}.lock'); + + if (!await _validLibrary(cachedLibrary, cachedDigest)) { + await _withExclusiveLock(lockFile, () async { + if (await _validLibrary(cachedLibrary, cachedDigest)) return; + await _buildIntoCache( + input: input, + output: output, + defines: defines, + cacheDir: cacheDir, + cachedLibrary: cachedLibrary, + cachedDigest: cachedDigest, + libraryFileName: libraryFileName, + logger: logger, + ); + }); } - final packageRoot = input.packageRoot; - final installDir = input.outputDirectory.resolve('install/'); - final sourceDir = packageRoot.resolve('src/'); + if (!await _validLibrary(cachedLibrary, cachedDigest)) { + throw StateError( + 'webcrypto cache publication failed for ${code.targetOS.name}/' + '${code.targetArchitecture.name}: ${cachedLibrary.path}', + ); + } - stdout.writeln( - 'webcrypto: building native asset for ' - '${input.config.code.targetOS}-${input.config.code.targetArchitecture}.', + final publishedLibrary = await _publish( + cachedLibrary, + input.outputDirectory, + libraryFileName, + ); + output.assets.code.add( + CodeAsset( + package: input.packageName, + name: _assetName, + linkMode: DynamicLoadingBundled(), + file: publishedLibrary.uri, + ), ); + }); +} + +Future _buildIntoCache({ + required BuildInput input, + required BuildOutputBuilder output, + required Map defines, + required Directory cacheDir, + required File cachedLibrary, + required File cachedDigest, + required String libraryFileName, + required Logger logger, +}) async { + await cacheDir.create(recursive: true); + final buildDir = Directory(p.join(cacheDir.path, 'build')); + if (await buildDir.exists()) await buildDir.delete(recursive: true); + await buildDir.create(recursive: true); + final code = input.config.code; + try { final builder = CMakeBuilder.create( name: 'webcrypto', - sourceDir: sourceDir, - defines: { - 'CMAKE_BUILD_TYPE': 'Release', - 'CMAKE_INSTALL_PREFIX': installDir.toFilePath(), - }, - targets: ['install'], + sourceDir: input.packageRoot.resolve('src/'), + outDir: buildDir.uri, + defines: defines, + targets: const ['webcrypto'], + buildLocal: false, + parallelUseAllProcessors: true, + logger: logger, ); + await builder.run(input: input, output: output, logger: logger); - await builder.run(input: input, output: output); - - final assets = await output.findAndAddCodeAssets( - input, - outDir: installDir, - names: {r'(lib)?webcrypto\.(dll|dylib|so)': _assetName}, - regExp: true, - ); - if (assets.isEmpty) { - throw BuildError( - message: - 'Failed to locate built webcrypto dynamic library in ' - '${installDir.toFilePath()}', + final builtLibrary = await _findLibrary(buildDir, libraryFileName); + if (builtLibrary == null || !await _validNonEmptyFile(builtLibrary)) { + throw StateError( + 'CMake reported success but did not produce a non-empty ' + '$libraryFileName. Build output:\n${await _listBuildOutput(buildDir)}', ); } - output.dependencies.addAll(_buildDependencies(packageRoot)); - }); + // Publish library and digest under the exclusive build-key lock. The + // digest lets later invocations reject truncated/corrupt cache entries. + final temporaryLibrary = File( + p.join(cacheDir.path, '.$libraryFileName.$pid.tmp'), + ); + final temporaryDigest = File('${temporaryLibrary.path}.sha256'); + if (await temporaryLibrary.exists()) await temporaryLibrary.delete(); + if (await temporaryDigest.exists()) await temporaryDigest.delete(); + await builtLibrary.copy(temporaryLibrary.path); + final digest = await _sha256File(temporaryLibrary); + await temporaryDigest.writeAsString('$digest\n', flush: true); + if (await cachedLibrary.exists()) await cachedLibrary.delete(); + await temporaryLibrary.rename(cachedLibrary.path); + if (await cachedDigest.exists()) await cachedDigest.delete(); + await temporaryDigest.rename(cachedDigest.path); + } catch (error, stackTrace) { + throw StateError( + 'Failed to build package:webcrypto for ${code.targetOS.name}/' + '${code.targetArchitecture.name} using the Native Assets target ' + 'toolchain. Build directory: ${buildDir.path}. Error: $error\n' + '$stackTrace', + ); + } } -final _buildDependencyExtensions = { - '.S', - '.asm', - '.c', - '.cc', - '.cmake', - '.cpp', - '.h', +void _validateTarget(OS os, Architecture architecture) { + final supported = switch (os) { + OS.android => const { + Architecture.arm, + Architecture.arm64, + Architecture.ia32, + Architecture.x64, + }, + OS.iOS => const {Architecture.arm64, Architecture.x64}, + OS.linux => const {Architecture.arm64, Architecture.x64}, + OS.macOS => const {Architecture.arm64, Architecture.x64}, + OS.windows => const { + Architecture.arm64, + Architecture.ia32, + Architecture.x64, + }, + _ => const {}, + }; + if (!supported.contains(architecture)) { + throw UnsupportedError( + 'package:webcrypto does not support Native Assets target ' + '${os.name}/${architecture.name}. Supported architectures for ' + '${os.name}: ${supported.map((value) => value.name).join(', ')}.', + ); + } +} + +String _libraryFileName(OS os) => switch (os) { + OS.windows => 'webcrypto.dll', + OS.iOS || OS.macOS => 'libwebcrypto.dylib', + OS.android || OS.linux => 'libwebcrypto.so', + _ => throw UnsupportedError('package:webcrypto does not support ${os.name}.'), }; -Iterable _buildDependencies(Uri packageRoot) sync* { - yield* _filesForBuild(Directory.fromUri(packageRoot.resolve('src/'))); - yield* _filesForBuild( - Directory.fromUri(packageRoot.resolve('third_party/boringssl/')), - ); +final class _NativeInput { + const _NativeInput(this.relativePath, this.uri); + + final String relativePath; + final Uri uri; +} + +Future> _collectNativeInputs(Uri packageRoot) async { + final packagePath = Directory.fromUri(packageRoot).path; + final roots = [ + packageRoot.resolve('src/'), + packageRoot.resolve('third_party/boringssl/'), + packageRoot.resolve('hook/'), + ]; + final inputs = <_NativeInput>[ + _NativeInput('pubspec.yaml', packageRoot.resolve('pubspec.yaml')), + ]; + + for (final root in roots) { + final directory = Directory.fromUri(root); + if (!await directory.exists()) { + throw StateError('Required webcrypto native input is missing: $root'); + } + await for (final entity in directory.list( + recursive: true, + followLinks: false, + )) { + if (entity is! File) continue; + final relative = p.relative(entity.path, from: packagePath); + if (p + .split(relative) + .any((part) => part == 'build' || part == '.dart_tool')) { + continue; + } + if (_isNativeInput(entity.path)) { + inputs.add(_NativeInput(relative, entity.uri)); + } + } + } + + inputs.sort((a, b) => a.relativePath.compareTo(b.relativePath)); + return inputs; } -Iterable _filesForBuild(Directory root) sync* { - if (!root.existsSync()) { - return; +bool _isNativeInput(String path) { + final extension = p.extension(path).toLowerCase(); + return const { + '.c', + '.cc', + '.cpp', + '.h', + '.inc', + '.s', + '.asm', + '.cmake', + '.dart', + '.yaml', + }.contains(extension) || + p.basename(path) == 'CMakeLists.txt'; +} + +Future _computeBuildKey({ + required CodeConfig code, + required Map defines, + required List<_NativeInput> inputs, +}) async { + final bytes = BytesBuilder(copy: false); + + void addText(String value) { + bytes.add(utf8.encode(value)); + bytes.addByte(0); } - for (final entity in root.listSync(recursive: true, followLinks: false)) { - if (entity is! File) { - continue; + addText(_cacheSchema); + addText('os=${code.targetOS.name}'); + addText('arch=${code.targetArchitecture.name}'); + addText('link=${code.linkModePreference}'); + final compiler = code.cCompiler; + if (compiler != null) { + for (final tool in { + 'cc': compiler.compiler, + 'ld': compiler.linker, + 'ar': compiler.archiver, + }.entries) { + addText('${tool.key}=${tool.value}'); + if (tool.value.scheme == 'file') { + final file = File.fromUri(tool.value); + if (await file.exists()) { + final stat = await file.stat(); + addText( + '${tool.key}Stat=${stat.size}:' + '${stat.modified.microsecondsSinceEpoch}', + ); + } + } } - if (!_buildDependencyExtensions.any(entity.uri.path.endsWith)) { - continue; + } + if (code.targetOS == OS.android) { + addText('androidApi=${code.android.targetNdkApi}'); + } else if (code.targetOS == OS.iOS) { + addText('iosSdk=${code.iOS.targetSdk}'); + addText('iosVersion=${code.iOS.targetVersion}'); + } else if (code.targetOS == OS.macOS) { + addText('macosVersion=${code.macOS.targetVersion}'); + } + for (final define + in defines.entries.toList()..sort((a, b) => a.key.compareTo(b.key))) { + addText('define:${define.key}=${define.value}'); + } + for (final input in inputs) { + addText('file:${input.relativePath}'); + final file = File.fromUri(input.uri); + if (!await file.exists()) { + throw StateError('Native build input disappeared: ${input.uri}'); + } + await for (final chunk in file.openRead()) { + bytes.add(chunk); + } + bytes.addByte(0); + } + + return sha256.convert(bytes.takeBytes()).toString(); +} + +Directory _cacheDirectory(String buildKey) { + final xdg = Platform.environment['XDG_CACHE_HOME']; + if (xdg != null && xdg.isNotEmpty) { + return Directory(p.join(xdg, 'webcrypto.dart', buildKey)); + } + if (Platform.isWindows) { + final localAppData = Platform.environment['LOCALAPPDATA']; + if (localAppData != null && localAppData.isNotEmpty) { + return Directory( + p.join(localAppData, 'webcrypto.dart', 'Cache', buildKey), + ); + } + } + final home = + Platform.environment['HOME'] ?? + Platform.environment['USERPROFILE'] ?? + (throw StateError( + 'Cannot locate the webcrypto build cache: HOME, USERPROFILE, and ' + 'XDG_CACHE_HOME are all unset.', + )); + return Directory(p.join(home, '.cache', 'webcrypto.dart', buildKey)); +} + +Future _withExclusiveLock( + File lockFile, + Future Function() body, +) async { + await lockFile.parent.create(recursive: true); + final randomAccessFile = await lockFile.open(mode: FileMode.append); + try { + await randomAccessFile.lock(FileLock.blockingExclusive); + try { + await body(); + } finally { + await randomAccessFile.unlock(); + } + } finally { + await randomAccessFile.close(); + } +} + +Future _validLibrary(File library, File digestFile) async { + if (!await _validNonEmptyFile(library) || !await digestFile.exists()) { + return false; + } + final expected = (await digestFile.readAsString()).trim(); + if (!RegExp(r'^[0-9a-f]{64}$').hasMatch(expected)) return false; + return await _sha256File(library) == expected; +} + +Future _validNonEmptyFile(File file) async => + await file.exists() && await file.length() > 0; + +Future _sha256File(File file) async { + final digestSink = _DigestSink(); + final input = sha256.startChunkedConversion(digestSink); + await for (final chunk in file.openRead()) { + input.add(chunk); + } + input.close(); + return digestSink.value.toString(); +} + +final class _DigestSink implements Sink { + late Digest value; + + @override + void add(Digest data) => value = data; + + @override + void close() {} +} + +Future _findLibrary(Directory root, String fileName) async { + final candidates = []; + await for (final entity in root.list(recursive: true, followLinks: false)) { + if (entity is File && p.basename(entity.path) == fileName) { + candidates.add(entity); + } + } + if (candidates.isEmpty) return null; + candidates.sort((a, b) { + final releaseA = p.split(a.path).contains('Release') ? 0 : 1; + final releaseB = p.split(b.path).contains('Release') ? 0 : 1; + final byConfiguration = releaseA.compareTo(releaseB); + return byConfiguration != 0 ? byConfiguration : a.path.compareTo(b.path); + }); + return candidates.first; +} + +Future _publish( + File cachedLibrary, + Uri outputDirectory, + String libraryFileName, +) async { + final output = Directory.fromUri(outputDirectory); + await output.create(recursive: true); + final destination = File(p.join(output.path, libraryFileName)); + final temporary = File('${destination.path}.$pid.tmp'); + if (await temporary.exists()) await temporary.delete(); + await cachedLibrary.copy(temporary.path); + if (await destination.exists()) await destination.delete(); + return temporary.rename(destination.path); +} + +Future _listBuildOutput(Directory directory) async { + final lines = []; + if (!await directory.exists()) return ' (output directory does not exist)'; + await for (final entity in directory.list( + recursive: true, + followLinks: false, + )) { + lines.add(' ${entity.path}'); + if (lines.length == 100) { + lines.add(' (truncated)'); + break; } - yield entity.uri; } + return lines.join('\n'); } diff --git a/pubspec.yaml b/pubspec.yaml index 3baa6d58c..83fe114e6 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -15,16 +15,19 @@ name: webcrypto version: 0.6.2-wip description: Cross-platform implementation of Web Cryptography APIs for Dart. -repository: https://github.com/google/webcrypto.dart +repository: https://github.com/Telosnex/webcrypto.dart environment: sdk: ^3.10.0 dependencies: + crypto: ^3.0.7 ffi: ^2.0.0 hooks: ^1.0.0 + logging: ^1.3.0 + native_toolchain_cmake: ^0.2.7 + path: ^1.9.0 code_assets: ^1.0.0 - native_toolchain_cmake: ^0.2.4 meta: ^1.3.0 dev_dependencies: From 50d464c0ec41de35e0d3afb5b59e66c16431916f Mon Sep 17 00:00:00 2001 From: James O'Leary Date: Tue, 1 Sep 2026 14:54:23 -0700 Subject: [PATCH 04/69] feat: log native build hook timings --- hook/build.dart | 111 ++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 98 insertions(+), 13 deletions(-) diff --git a/hook/build.dart b/hook/build.dart index ab610cfbe..b49d8ebdc 100644 --- a/hook/build.dart +++ b/hook/build.dart @@ -29,37 +29,59 @@ const _assetName = 'webcrypto.dart'; const _cacheSchema = 'webcrypto-native-assets-v3'; void main(List args) async { + final hookLog = _HookLogBuffer('webcrypto'); await build(args, (input, output) async { if (!input.config.buildCodeAssets) return; + final hookStopwatch = Stopwatch()..start(); final code = input.config.code; _validateTarget(code.targetOS, code.targetArchitecture); - final logger = Logger.root + final logger = Logger('') ..level = Level.ALL - ..onRecord.listen((record) => stderr.writeln(record.message)); + ..onRecord.listen((record) => hookLog.add(record.message)); + final collectStopwatch = Stopwatch()..start(); final nativeInputs = await _collectNativeInputs(input.packageRoot); output.dependencies.addAll(nativeInputs.map((input) => input.uri)); + logger.info( + 'Enumerated ${nativeInputs.length} native inputs in ' + '${_formatDuration(collectStopwatch.elapsed)}', + ); final defines = { 'CMAKE_BUILD_TYPE': 'Release', 'CMAKE_POSITION_INDEPENDENT_CODE': 'ON', }; + final keyStopwatch = Stopwatch()..start(); final buildKey = await _computeBuildKey( code: code, defines: defines, inputs: nativeInputs, ); + logger.info( + 'Computed build key in ${_formatDuration(keyStopwatch.elapsed)}', + ); + final libraryFileName = _libraryFileName(code.targetOS); final cacheDir = _cacheDirectory(buildKey); final cachedLibrary = File(p.join(cacheDir.path, libraryFileName)); final cachedDigest = File('${cachedLibrary.path}.sha256'); final lockFile = File('${cacheDir.path}.lock'); - if (!await _validLibrary(cachedLibrary, cachedDigest)) { + logger.info('Build key: $buildKey'); + logger.info('Cache: ${cacheDir.path}'); + + final cacheHit = await _validLibrary(cachedLibrary, cachedDigest); + if (!cacheHit) { + final lockAndBuildStopwatch = Stopwatch()..start(); await _withExclusiveLock(lockFile, () async { - if (await _validLibrary(cachedLibrary, cachedDigest)) return; + if (await _validLibrary(cachedLibrary, cachedDigest)) { + logger.info( + 'Build completed by another process while waiting for the lock', + ); + return; + } await _buildIntoCache( input: input, output: output, @@ -70,16 +92,21 @@ void main(List args) async { libraryFileName: libraryFileName, logger: logger, ); - }); - } + }, logger: logger); - if (!await _validLibrary(cachedLibrary, cachedDigest)) { - throw StateError( - 'webcrypto cache publication failed for ${code.targetOS.name}/' - '${code.targetArchitecture.name}: ${cachedLibrary.path}', + if (!await _validLibrary(cachedLibrary, cachedDigest)) { + throw StateError( + 'webcrypto cache publication failed for ${code.targetOS.name}/' + '${code.targetArchitecture.name}: ${cachedLibrary.path}', + ); + } + logger.info( + 'Cache miss resolved in ' + '${_formatDuration(lockAndBuildStopwatch.elapsed)}', ); } + final publishStopwatch = Stopwatch()..start(); final publishedLibrary = await _publish( cachedLibrary, input.outputDirectory, @@ -93,7 +120,54 @@ void main(List args) async { file: publishedLibrary.uri, ), ); - }); + logger.info( + cacheHit + ? 'Hook completed from cache in ' + '${_formatDuration(hookStopwatch.elapsed)} ' + '(publish/register ' + '${_formatDuration(publishStopwatch.elapsed)})' + : 'Hook completed after build in ' + '${_formatDuration(hookStopwatch.elapsed)} ' + '(publish/register ' + '${_formatDuration(publishStopwatch.elapsed)})', + ); + }).whenComplete(hookLog.flush); +} + +/// Collects logger records so hooks_runner receives one newline-normalized +/// stderr message instead of adding a blank line after every streamed chunk. +final class _HookLogBuffer { + _HookLogBuffer(this.tag); + + final String tag; + final List _lines = []; + + void add(String message) { + final normalized = message.replaceAll('\r\n', '\n').replaceAll('\r', '\n'); + for (final line in normalized.split('\n')) { + if (line.trim().isEmpty) continue; + _lines.add('[$tag] $line'); + } + } + + void flush() { + if (_lines.isEmpty) return; + // hooks_runner adds the terminating newline while capturing this chunk. + stderr.write(_lines.join('\n')); + } +} + +String _formatDuration(Duration duration) { + final millis = duration.inMilliseconds; + if (millis < 1000) return '${millis}ms'; + final seconds = duration.inSeconds; + final remainderMillis = millis - seconds * 1000; + if (seconds < 60) { + return '$seconds.${(remainderMillis ~/ 100).toString()}s'; + } + final minutes = seconds ~/ 60; + final remainderSeconds = seconds % 60; + return '${minutes}m ${remainderSeconds}s'; } Future _buildIntoCache({ @@ -123,7 +197,11 @@ Future _buildIntoCache({ parallelUseAllProcessors: true, logger: logger, ); + final cmakeStopwatch = Stopwatch()..start(); await builder.run(input: input, output: output, logger: logger); + logger.info( + 'CMake build finished in ${_formatDuration(cmakeStopwatch.elapsed)}', + ); final builtLibrary = await _findLibrary(buildDir, libraryFileName); if (builtLibrary == null || !await _validNonEmptyFile(builtLibrary)) { @@ -341,12 +419,19 @@ Directory _cacheDirectory(String buildKey) { Future _withExclusiveLock( File lockFile, - Future Function() body, -) async { + Future Function() body, { + required Logger logger, +}) async { await lockFile.parent.create(recursive: true); final randomAccessFile = await lockFile.open(mode: FileMode.append); try { + final lockStopwatch = Stopwatch()..start(); await randomAccessFile.lock(FileLock.blockingExclusive); + if (lockStopwatch.elapsedMilliseconds > 100) { + logger.info( + 'Waited ${_formatDuration(lockStopwatch.elapsed)} for build lock', + ); + } try { await body(); } finally { From d46233b0af1235b2abc46c0ecce8d19aa3dc06cf Mon Sep 17 00:00:00 2001 From: James O'Leary Date: Wed, 2 Sep 2026 12:32:20 -0700 Subject: [PATCH 05/69] chore(deps): widen hooks constraint to allow hooks ^2 --- pubspec.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pubspec.yaml b/pubspec.yaml index 83fe114e6..372a34d53 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -23,7 +23,7 @@ environment: dependencies: crypto: ^3.0.7 ffi: ^2.0.0 - hooks: ^1.0.0 + hooks: ">=1.0.0 <3.0.0" logging: ^1.3.0 native_toolchain_cmake: ^0.2.7 path: ^1.9.0 From f05dfb6197eb7d56e8062cd2e92c6904d9e10422 Mon Sep 17 00:00:00 2001 From: James O'Leary Date: Sun, 4 Oct 2026 11:14:46 -0700 Subject: [PATCH 06/69] Fix Windows arm64 build: disable MSVC C4702 BoringSSL's AES code has paths that are statically unreachable on arm64, and the build treats warnings as errors. Upstream BoringSSL disables C4702 for MSVC for this reason (https://crbug.com/385161043). Verified on Windows 11 arm64: the package test suite passes. --- CHANGELOG.md | 2 ++ src/CMakeLists.txt | 2 ++ 2 files changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 822cbf49b..569a65edc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ to avoid recompiling BoringSSL when the native inputs are unchanged. * Preserved the `globalThis` web-worker compatibility fix. * Fixed JS interop to enable WebAssembly. +* Fixed Windows arm64 builds: disable MSVC warning C4702 (unreachable code), + matching upstream BoringSSL. # 0.6.1 * Added Dart native build hooks and native asset lookup for the bundled diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 0d739186d..49b744002 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -108,6 +108,8 @@ if(MSVC) "C4244" # 'function' : conversion from 'int' to 'uint8_t', # possible loss of data "C4267" # conversion from 'size_t' to 'int', possible loss of data + "C4702" # unreachable code; MSVC's warning is too aggressive. Matches + # upstream BoringSSL; see https://crbug.com/385161043 "C4706" # assignment within conditional expression "C4141" "C4201" # nonstandard extension used: nameless struct/union From b75ec449461b725a207288b1e93eb0d64e04a0ec Mon Sep 17 00:00:00 2001 From: James O'Leary Date: Sun, 4 Oct 2026 23:06:17 -0700 Subject: [PATCH 07/69] Fix macOS x86_64 link: add fiat ADX assembly to apple_x86_64 sources BoringSSL guards third_party/fiat/asm/*.S with __APPLE__ || __ELF__, and p256_64.h and curve25519 call them on any non-Windows x86_64 with GCC or Clang. The source generator assigned them only to linux_x86_64, so a macOS x64 build failed with undefined _webcrypto_fiat_p256_adx_mul and _webcrypto_fiat_p256_adx_sqr. --- third_party/boringssl/sources.cmake | 4 ++++ tool/bump-boringssl-revision.sh | 9 ++++++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/third_party/boringssl/sources.cmake b/third_party/boringssl/sources.cmake index 90437ae92..751cfa3da 100644 --- a/third_party/boringssl/sources.cmake +++ b/third_party/boringssl/sources.cmake @@ -321,6 +321,10 @@ set(crypto_sources_apple_x86_64 ${BORINGSSL_ROOT}gen/crypto/md5-586-apple.S ${BORINGSSL_ROOT}gen/crypto/md5-x86_64-apple.S ${BORINGSSL_ROOT}gen/test_support/trampoline-x86_64-apple.S + ${BORINGSSL_ROOT}third_party/fiat/asm/fiat_curve25519_adx_mul.S + ${BORINGSSL_ROOT}third_party/fiat/asm/fiat_curve25519_adx_square.S + ${BORINGSSL_ROOT}third_party/fiat/asm/fiat_p256_adx_mul.S + ${BORINGSSL_ROOT}third_party/fiat/asm/fiat_p256_adx_sqr.S ) set(crypto_sources_linux_aarch64 diff --git a/tool/bump-boringssl-revision.sh b/tool/bump-boringssl-revision.sh index 32de27be2..947401d96 100755 --- a/tool/bump-boringssl-revision.sh +++ b/tool/bump-boringssl-revision.sh @@ -208,7 +208,8 @@ def classify_asm(path): if normalized == "crypto/hrss/asm/poly_rq_mul.S": return "linux_x86_64" if normalized.startswith("third_party/fiat/asm/"): - return "linux_x86_64" + # Guarded by __APPLE__ || __ELF__, so both platforms need them. + return ("apple_x86_64", "linux_x86_64") return None @@ -237,8 +238,10 @@ for file in ( + test_support.get("nasm", []) ): key = classify_asm(file) - if key is not None: - asm_outputs[key].append(file) + if key is None: + continue + for k in key if isinstance(key, tuple) else (key,): + asm_outputs[k].append(file) payload = { "crypto_sources": sorted(bcm.get("srcs", []) + crypto.get("srcs", [])), From 24b7ed2a200f3f0e5d07244ae072e8292db5c6b8 Mon Sep 17 00:00:00 2001 From: James O'Leary Date: Mon, 5 Oct 2026 00:10:34 -0700 Subject: [PATCH 08/69] Prebuilt native libraries with native_prebuilt (fllama ADR 005 step 2) The hook downloads the library from a GitHub release when native_artifacts/prebuilt.json matches the sources. User define native_build: auto (default) | download | source. Otherwise it builds from source as before. The local cache key is now the native_prebuilt source key plus the toolchain (D4). .github/workflows/native_release.yml builds all 12 targets on fixed runners, releases them, and pushes native-manifest/. .gitattributes keeps LF line endings so the source key is equal on Windows. --- .gitattributes | 1 + .github/workflows/native_release.yml | 188 +++++++++++++++++++ hook/build.dart | 258 ++++++++++----------------- native_artifacts/source_excludes.txt | 4 + pubspec.yaml | 4 + 5 files changed, 295 insertions(+), 160 deletions(-) create mode 100644 .gitattributes create mode 100644 .github/workflows/native_release.yml create mode 100644 native_artifacts/source_excludes.txt diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 000000000..6313b56c5 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +* text=auto eol=lf diff --git a/.github/workflows/native_release.yml b/.github/workflows/native_release.yml new file mode 100644 index 000000000..ce34b8405 --- /dev/null +++ b/.github/workflows/native_release.yml @@ -0,0 +1,188 @@ +# Builds the native library for every target and publishes the files as a +# GitHub release (fllama ADR 005 D5, D6, D8). Start it with +# git push -f origin HEAD:native-release (release) +# git push -f origin HEAD:native-release-dry (build only, no upload) +# or with workflow_dispatch. On success it pushes the branch +# native-manifest/ with native_artifacts/prebuilt.json. Merge that +# branch to use the release. +name: native release + +on: + workflow_dispatch: + inputs: + dry_run: + description: Build only, upload nothing. + type: boolean + default: false + push: + branches: [native-release, native-release-dry] + +concurrency: + group: native-release + cancel-in-progress: false + +permissions: + contents: read + +env: + DART_VERSION: 3.13.1 + NDK_VERSION: 28.2.13676358 + NASM_VERSION: 2.16.03 + NASM_SHA256: 3ee4782247bcb874378d02f7eab4e294a84d3d15f3f6ee2de2f47a46aa7226e6 + +jobs: + build: + name: ${{ matrix.target }} + runs-on: ${{ matrix.runner }} + timeout-minutes: 45 + strategy: + fail-fast: true + matrix: + include: + - { target: windows-x64, runner: windows-2022 } + - { target: windows-arm64, runner: windows-11-arm } + - { target: linux-x64, runner: ubuntu-22.04 } + - { target: linux-arm64, runner: ubuntu-22.04-arm } + - { target: macos-arm64, runner: macos-15 } + - { target: macos-x64, runner: macos-15 } + - { target: ios-arm64-iphoneos, runner: macos-15 } + - { target: ios-arm64-iphonesimulator, runner: macos-15 } + - { target: ios-x64-iphonesimulator, runner: macos-15 } + - { target: android-arm, runner: ubuntu-22.04 } + - { target: android-arm64, runner: ubuntu-22.04 } + - { target: android-x64, runner: ubuntu-22.04 } + defaults: + run: + shell: bash + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + - uses: dart-lang/setup-dart@6afc89df92d6eb3834022f73cd65adc8cdfcb92d # v1.8.1 + with: + sdk: ${{ env.DART_VERSION }} + + - name: Install NASM + if: runner.os == 'Windows' + run: | + curl -sSfLo nasm.zip "https://www.nasm.us/pub/nasm/releasebuilds/$NASM_VERSION/win64/nasm-$NASM_VERSION-win64.zip" + echo "$NASM_SHA256 nasm.zip" | sha256sum -c - + unzip -q nasm.zip -d "$RUNNER_TEMP" + rm nasm.zip + echo "$RUNNER_TEMP/nasm-$NASM_VERSION" >> "$GITHUB_PATH" + + - name: Install the NDK + if: startsWith(matrix.target, 'android-') + run: | + yes | "$ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager" "ndk;$NDK_VERSION" > /dev/null + test -d "$ANDROID_HOME/ndk/$NDK_VERSION" + + - name: Describe the toolchain + id: toolchain + run: | + case "${{ matrix.target }}" in + android-*) t="Android NDK $NDK_VERSION" ;; + windows-*) + vs="$("/c/Program Files (x86)/Microsoft Visual Studio/Installer/vswhere.exe" -latest -products '*' -property catalog_productDisplayVersion || true)" + t="MSVC, Visual Studio ${vs:-unknown}, NASM $NASM_VERSION" ;; + macos-*|ios-*) t="$(xcodebuild -version | head -1)" ;; + linux-*) t="$(gcc --version | head -1)" ;; + esac + echo "Toolchain: $t" + echo "value=$t" >> "$GITHUB_OUTPUT" + + - run: dart pub get + + - name: Source key + run: | + mkdir -p key + dart run native_prebuilt:key --list > "key/${{ matrix.target }}.txt" + tail -1 "key/${{ matrix.target }}.txt" + + - name: Build + env: + TOOLCHAIN: ${{ steps.toolchain.outputs.value }} + run: | + defines=() + if [[ "${{ matrix.target }}" == android-* ]]; then + defines+=(--define "android={\"android_home\":\"$ANDROID_HOME\",\"ndk_version\":\"$NDK_VERSION\"}") + fi + dart run native_prebuilt:build \ + --target "${{ matrix.target }}" \ + --out "out/${{ matrix.target }}" \ + --runner "${{ matrix.runner }}" \ + --toolchain "$TOOLCHAIN" \ + "${defines[@]}" + cat "out/${{ matrix.target }}/target.json" + + - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 + with: + name: native-${{ matrix.target }} + path: | + out/ + key/ + if-no-files-found: error + retention-days: 90 + + release: + needs: build + runs-on: ubuntu-22.04 + timeout-minutes: 20 + permissions: + contents: write + env: + DRY_RUN: ${{ github.ref_name == 'native-release-dry' || inputs.dry_run == true }} + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - uses: dart-lang/setup-dart@6afc89df92d6eb3834022f73cd65adc8cdfcb92d # v1.8.1 + with: + sdk: ${{ env.DART_VERSION }} + - run: dart pub get + - uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1 + with: + pattern: native-* + path: artifacts + merge-multiple: true + + - name: Compare the source keys of all runners (I2) + run: | + dart run native_prebuilt:key --list > local-key.txt + status=0 + for f in artifacts/key/*.txt; do + if ! diff -u local-key.txt "$f"; then + echo "::error::The source key on $(basename "$f" .txt) differs." + status=1 + fi + done + exit $status + + - name: Release + env: + GH_TOKEN: ${{ github.token }} + run: | + args=(--staging staging) + if [[ "$DRY_RUN" == true ]]; then args+=(--dry-run); fi + dart run native_prebuilt:release "${args[@]}" artifacts/out + cat native_artifacts/prebuilt.json + + - name: Push the manifest branch + if: env.DRY_RUN != 'true' + run: | + tag="native-$(dart run native_prebuilt:key | tail -1 | cut -c1-16)" + branch="native-manifest/$tag" + git config user.name "github-actions[bot]" + git config user.email "41898282+github-actions[bot]@users.noreply.github.com" + git switch -c "$branch" + git add native_artifacts/prebuilt.json + git commit -m "native: prebuilt files of release $tag" + git push origin "$branch" + { + echo "Release: https://github.com/$GITHUB_REPOSITORY/releases/tag/$tag" + echo + echo "Merge branch \`$branch\` to use it." + } >> "$GITHUB_STEP_SUMMARY" + + - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 + with: + name: prebuilt-manifest + path: native_artifacts/prebuilt.json diff --git a/hook/build.dart b/hook/build.dart index b49d8ebdc..007c5f24e 100644 --- a/hook/build.dart +++ b/hook/build.dart @@ -10,7 +10,10 @@ /// Asset on every supported native target. /// /// Web builds do not request code assets, so this hook intentionally does -/// nothing for them. Native builds use a content-addressed, process-locked +/// nothing for them. With package:native_prebuilt, a native build downloads +/// the library from a release when native_artifacts/prebuilt.json matches +/// the sources (user define `native_build`: auto, download or source). +/// Otherwise it builds from source into a content-addressed, process-locked /// cache because compiling BoringSSL for every hook invocation is expensive. library; @@ -22,11 +25,12 @@ import 'package:code_assets/code_assets.dart'; import 'package:crypto/crypto.dart'; import 'package:hooks/hooks.dart'; import 'package:logging/logging.dart'; +import 'package:native_prebuilt/native_prebuilt.dart'; import 'package:native_toolchain_cmake/native_toolchain_cmake.dart'; import 'package:path/path.dart' as p; const _assetName = 'webcrypto.dart'; -const _cacheSchema = 'webcrypto-native-assets-v3'; +const _cacheSchema = 'webcrypto-native-assets-v4'; void main(List args) async { final hookLog = _HookLogBuffer('webcrypto'); @@ -41,97 +45,101 @@ void main(List args) async { ..level = Level.ALL ..onRecord.listen((record) => hookLog.add(record.message)); - final collectStopwatch = Stopwatch()..start(); - final nativeInputs = await _collectNativeInputs(input.packageRoot); - output.dependencies.addAll(nativeInputs.map((input) => input.uri)); - logger.info( - 'Enumerated ${nativeInputs.length} native inputs in ' - '${_formatDuration(collectStopwatch.elapsed)}', - ); - - final defines = { - 'CMAKE_BUILD_TYPE': 'Release', - 'CMAKE_POSITION_INDEPENDENT_CODE': 'ON', - }; - final keyStopwatch = Stopwatch()..start(); - final buildKey = await _computeBuildKey( - code: code, - defines: defines, - inputs: nativeInputs, - ); - logger.info( - 'Computed build key in ${_formatDuration(keyStopwatch.elapsed)}', - ); - - final libraryFileName = _libraryFileName(code.targetOS); - final cacheDir = _cacheDirectory(buildKey); - final cachedLibrary = File(p.join(cacheDir.path, libraryFileName)); - final cachedDigest = File('${cachedLibrary.path}.sha256'); - final lockFile = File('${cacheDir.path}.lock'); - - logger.info('Build key: $buildKey'); - logger.info('Cache: ${cacheDir.path}'); - - final cacheHit = await _validLibrary(cachedLibrary, cachedDigest); - if (!cacheHit) { - final lockAndBuildStopwatch = Stopwatch()..start(); - await _withExclusiveLock(lockFile, () async { - if (await _validLibrary(cachedLibrary, cachedDigest)) { - logger.info( - 'Build completed by another process while waiting for the lock', - ); - return; - } - await _buildIntoCache( - input: input, - output: output, - defines: defines, - cacheDir: cacheDir, - cachedLibrary: cachedLibrary, - cachedDigest: cachedDigest, - libraryFileName: libraryFileName, - logger: logger, - ); - }, logger: logger); + await NativePrebuilt(input: input, output: output, log: logger.info).run(( + source, + ) async { + await _buildFromSource( + input: input, + output: output, + sourceKey: source.sourceKey.key, + logger: logger, + ); + }); + logger.info('Hook completed in ${_formatDuration(hookStopwatch.elapsed)}'); + }).whenComplete(hookLog.flush); +} - if (!await _validLibrary(cachedLibrary, cachedDigest)) { - throw StateError( - 'webcrypto cache publication failed for ${code.targetOS.name}/' - '${code.targetArchitecture.name}: ${cachedLibrary.path}', +Future _buildFromSource({ + required BuildInput input, + required BuildOutputBuilder output, + required String sourceKey, + required Logger logger, +}) async { + final code = input.config.code; + final defines = { + 'CMAKE_BUILD_TYPE': 'Release', + 'CMAKE_POSITION_INDEPENDENT_CODE': 'ON', + }; + final buildKey = await _computeBuildKey( + code: code, + defines: defines, + sourceKey: sourceKey, + ); + + final libraryFileName = _libraryFileName(code.targetOS); + final cacheDir = _cacheDirectory(buildKey); + final cachedLibrary = File(p.join(cacheDir.path, libraryFileName)); + final cachedDigest = File('${cachedLibrary.path}.sha256'); + final lockFile = File('${cacheDir.path}.lock'); + + logger.info('Build key: $buildKey'); + logger.info('Cache: ${cacheDir.path}'); + + final cacheHit = await _validLibrary(cachedLibrary, cachedDigest); + if (!cacheHit) { + final lockAndBuildStopwatch = Stopwatch()..start(); + await _withExclusiveLock(lockFile, () async { + if (await _validLibrary(cachedLibrary, cachedDigest)) { + logger.info( + 'Build completed by another process while waiting for the lock', ); + return; } - logger.info( - 'Cache miss resolved in ' - '${_formatDuration(lockAndBuildStopwatch.elapsed)}', + await _buildIntoCache( + input: input, + output: output, + defines: defines, + cacheDir: cacheDir, + cachedLibrary: cachedLibrary, + cachedDigest: cachedDigest, + libraryFileName: libraryFileName, + logger: logger, ); - } + }, logger: logger); - final publishStopwatch = Stopwatch()..start(); - final publishedLibrary = await _publish( - cachedLibrary, - input.outputDirectory, - libraryFileName, - ); - output.assets.code.add( - CodeAsset( - package: input.packageName, - name: _assetName, - linkMode: DynamicLoadingBundled(), - file: publishedLibrary.uri, - ), - ); + if (!await _validLibrary(cachedLibrary, cachedDigest)) { + throw StateError( + 'webcrypto cache publication failed for ${code.targetOS.name}/' + '${code.targetArchitecture.name}: ${cachedLibrary.path}', + ); + } logger.info( - cacheHit - ? 'Hook completed from cache in ' - '${_formatDuration(hookStopwatch.elapsed)} ' - '(publish/register ' - '${_formatDuration(publishStopwatch.elapsed)})' - : 'Hook completed after build in ' - '${_formatDuration(hookStopwatch.elapsed)} ' - '(publish/register ' - '${_formatDuration(publishStopwatch.elapsed)})', + 'Cache miss resolved in ' + '${_formatDuration(lockAndBuildStopwatch.elapsed)}', ); - }).whenComplete(hookLog.flush); + } + + final publishStopwatch = Stopwatch()..start(); + final publishedLibrary = await _publish( + cachedLibrary, + input.outputDirectory, + libraryFileName, + ); + output.assets.code.add( + CodeAsset( + package: input.packageName, + name: _assetName, + linkMode: DynamicLoadingBundled(), + file: publishedLibrary.uri, + ), + ); + logger.info( + cacheHit + ? 'Published the cached build ' + '(${_formatDuration(publishStopwatch.elapsed)})' + : 'Published the new build ' + '(${_formatDuration(publishStopwatch.elapsed)})', + ); } /// Collects logger records so hooks_runner receives one newline-normalized @@ -270,71 +278,12 @@ String _libraryFileName(OS os) => switch (os) { _ => throw UnsupportedError('package:webcrypto does not support ${os.name}.'), }; -final class _NativeInput { - const _NativeInput(this.relativePath, this.uri); - - final String relativePath; - final Uri uri; -} - -Future> _collectNativeInputs(Uri packageRoot) async { - final packagePath = Directory.fromUri(packageRoot).path; - final roots = [ - packageRoot.resolve('src/'), - packageRoot.resolve('third_party/boringssl/'), - packageRoot.resolve('hook/'), - ]; - final inputs = <_NativeInput>[ - _NativeInput('pubspec.yaml', packageRoot.resolve('pubspec.yaml')), - ]; - - for (final root in roots) { - final directory = Directory.fromUri(root); - if (!await directory.exists()) { - throw StateError('Required webcrypto native input is missing: $root'); - } - await for (final entity in directory.list( - recursive: true, - followLinks: false, - )) { - if (entity is! File) continue; - final relative = p.relative(entity.path, from: packagePath); - if (p - .split(relative) - .any((part) => part == 'build' || part == '.dart_tool')) { - continue; - } - if (_isNativeInput(entity.path)) { - inputs.add(_NativeInput(relative, entity.uri)); - } - } - } - - inputs.sort((a, b) => a.relativePath.compareTo(b.relativePath)); - return inputs; -} - -bool _isNativeInput(String path) { - final extension = p.extension(path).toLowerCase(); - return const { - '.c', - '.cc', - '.cpp', - '.h', - '.inc', - '.s', - '.asm', - '.cmake', - '.dart', - '.yaml', - }.contains(extension) || - p.basename(path) == 'CMakeLists.txt'; -} - +/// The key of the local build cache (ADR 005 D4): the source key of the +/// package and the host toolchain. Future _computeBuildKey({ required CodeConfig code, required Map defines, - required List<_NativeInput> inputs, + required String sourceKey, }) async { final bytes = BytesBuilder(copy: false); @@ -344,6 +293,7 @@ Future _computeBuildKey({ } addText(_cacheSchema); + addText('source=$sourceKey'); addText('os=${code.targetOS.name}'); addText('arch=${code.targetArchitecture.name}'); addText('link=${code.linkModePreference}'); @@ -379,18 +329,6 @@ Future _computeBuildKey({ in defines.entries.toList()..sort((a, b) => a.key.compareTo(b.key))) { addText('define:${define.key}=${define.value}'); } - for (final input in inputs) { - addText('file:${input.relativePath}'); - final file = File.fromUri(input.uri); - if (!await file.exists()) { - throw StateError('Native build input disappeared: ${input.uri}'); - } - await for (final chunk in file.openRead()) { - bytes.add(chunk); - } - bytes.addByte(0); - } - return sha256.convert(bytes.takeBytes()).toString(); } diff --git a/native_artifacts/source_excludes.txt b/native_artifacts/source_excludes.txt new file mode 100644 index 000000000..7c87bd0a1 --- /dev/null +++ b/native_artifacts/source_excludes.txt @@ -0,0 +1,4 @@ +# Files that the native build does not read (ADR 005 D3). +doc/ +tool/ +analysis_options.yaml diff --git a/pubspec.yaml b/pubspec.yaml index 372a34d53..ee7e91501 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -25,6 +25,10 @@ dependencies: ffi: ^2.0.0 hooks: ">=1.0.0 <3.0.0" logging: ^1.3.0 + native_prebuilt: + git: + url: https://github.com/Telosnex/native_prebuilt.git + ref: c2d414c6a3dd34ad57be0952fe6eb291ccf945f7 native_toolchain_cmake: ^0.2.7 path: ^1.9.0 code_assets: ^1.0.0 From a739ab6c291e22a23c99ef733e506a4d6fc74aef Mon Sep 17 00:00:00 2001 From: James O'Leary Date: Mon, 5 Oct 2026 00:15:37 -0700 Subject: [PATCH 09/69] native release workflow: fix the NDK install under pipefail; no fail-fast --- .github/workflows/native_release.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/native_release.yml b/.github/workflows/native_release.yml index ce34b8405..d638cc964 100644 --- a/.github/workflows/native_release.yml +++ b/.github/workflows/native_release.yml @@ -36,7 +36,7 @@ jobs: runs-on: ${{ matrix.runner }} timeout-minutes: 45 strategy: - fail-fast: true + fail-fast: false matrix: include: - { target: windows-x64, runner: windows-2022 } @@ -74,7 +74,7 @@ jobs: - name: Install the NDK if: startsWith(matrix.target, 'android-') run: | - yes | "$ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager" "ndk;$NDK_VERSION" > /dev/null + { yes || true; } | "$ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager" "ndk;$NDK_VERSION" > /dev/null test -d "$ANDROID_HOME/ndk/$NDK_VERSION" - name: Describe the toolchain From 1e586ca859a8e54f7f16d0f5b4dc8228f677d73d Mon Sep 17 00:00:00 2001 From: James O'Leary Date: Mon, 5 Oct 2026 00:23:20 -0700 Subject: [PATCH 10/69] native release workflow: no head in a pipe under pipefail --- .github/workflows/native_release.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/native_release.yml b/.github/workflows/native_release.yml index d638cc964..8c8c7afc4 100644 --- a/.github/workflows/native_release.yml +++ b/.github/workflows/native_release.yml @@ -85,8 +85,8 @@ jobs: windows-*) vs="$("/c/Program Files (x86)/Microsoft Visual Studio/Installer/vswhere.exe" -latest -products '*' -property catalog_productDisplayVersion || true)" t="MSVC, Visual Studio ${vs:-unknown}, NASM $NASM_VERSION" ;; - macos-*|ios-*) t="$(xcodebuild -version | head -1)" ;; - linux-*) t="$(gcc --version | head -1)" ;; + macos-*|ios-*) t="$(xcodebuild -version | sed -n 1p)" ;; + linux-*) t="$(gcc --version | sed -n 1p)" ;; esac echo "Toolchain: $t" echo "value=$t" >> "$GITHUB_OUTPUT" From 773ed9ad1ee03971fcdf896244b368c1d47b5833 Mon Sep 17 00:00:00 2001 From: James O'Leary Date: Mon, 5 Oct 2026 00:30:41 -0700 Subject: [PATCH 11/69] native release workflow: keep output out of the checkout; owner-only publish The source key counts untracked files, so build output and key listings now go to RUNNER_TEMP, and each build checks the work tree is clean. Follows the Telosnex workflow conventions: run-name, default bash shell, exact-SHA shallow checkout, owner gate on the publishing job. --- .github/workflows/native_release.yml | 68 +++++++++++++++++++--------- 1 file changed, 47 insertions(+), 21 deletions(-) diff --git a/.github/workflows/native_release.yml b/.github/workflows/native_release.yml index 8c8c7afc4..3a0e44642 100644 --- a/.github/workflows/native_release.yml +++ b/.github/workflows/native_release.yml @@ -6,6 +6,7 @@ # native-manifest/ with native_artifacts/prebuilt.json. Merge that # branch to use the release. name: native release +run-name: Native release / ${{ (github.ref_name == 'native-release-dry' || inputs.dry_run) && 'build only' || 'publish' }} on: workflow_dispatch: @@ -24,6 +25,10 @@ concurrency: permissions: contents: read +defaults: + run: + shell: bash + env: DART_VERSION: 3.13.1 NDK_VERSION: 28.2.13676358 @@ -51,13 +56,12 @@ jobs: - { target: android-arm, runner: ubuntu-22.04 } - { target: android-arm64, runner: ubuntu-22.04 } - { target: android-x64, runner: ubuntu-22.04 } - defaults: - run: - shell: bash steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: + ref: ${{ github.sha }} persist-credentials: false + fetch-depth: 1 - uses: dart-lang/setup-dart@6afc89df92d6eb3834022f73cd65adc8cdfcb92d # v1.8.1 with: sdk: ${{ env.DART_VERSION }} @@ -65,10 +69,10 @@ jobs: - name: Install NASM if: runner.os == 'Windows' run: | - curl -sSfLo nasm.zip "https://www.nasm.us/pub/nasm/releasebuilds/$NASM_VERSION/win64/nasm-$NASM_VERSION-win64.zip" - echo "$NASM_SHA256 nasm.zip" | sha256sum -c - - unzip -q nasm.zip -d "$RUNNER_TEMP" - rm nasm.zip + zip="$RUNNER_TEMP/nasm.zip" + curl -sSfLo "$zip" "https://www.nasm.us/pub/nasm/releasebuilds/$NASM_VERSION/win64/nasm-$NASM_VERSION-win64.zip" + echo "$NASM_SHA256 $zip" | sha256sum -c - + unzip -q "$zip" -d "$RUNNER_TEMP" echo "$RUNNER_TEMP/nasm-$NASM_VERSION" >> "$GITHUB_PATH" - name: Install the NDK @@ -93,11 +97,25 @@ jobs: - run: dart pub get + # Files go to $RUNNER_TEMP: untracked files in the checkout count in + # the source key. + - name: Set paths + run: echo "OUT=$RUNNER_TEMP/native" >> "$GITHUB_ENV" + + - &clean + name: Check that the work tree is clean + run: | + if [[ -n "$(git status --porcelain)" ]]; then + git status --porcelain + echo "::error::Untracked or changed files would change the source key." + exit 1 + fi + - name: Source key run: | - mkdir -p key - dart run native_prebuilt:key --list > "key/${{ matrix.target }}.txt" - tail -1 "key/${{ matrix.target }}.txt" + mkdir -p "$OUT/key" + dart run native_prebuilt:key --list > "$OUT/key/${{ matrix.target }}.txt" + tail -1 "$OUT/key/${{ matrix.target }}.txt" - name: Build env: @@ -109,23 +127,27 @@ jobs: fi dart run native_prebuilt:build \ --target "${{ matrix.target }}" \ - --out "out/${{ matrix.target }}" \ + --out "$OUT/out/${{ matrix.target }}" \ --runner "${{ matrix.runner }}" \ --toolchain "$TOOLCHAIN" \ "${defines[@]}" - cat "out/${{ matrix.target }}/target.json" + cat "$OUT/out/${{ matrix.target }}/target.json" + + - *clean - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 with: name: native-${{ matrix.target }} - path: | - out/ - key/ + path: ${{ runner.temp }}/native/ if-no-files-found: error retention-days: 90 release: needs: build + # Only the owner publishes, from this repository. + if: >- + github.repository == 'Telosnex/webcrypto.dart' && + github.actor == 'jpohhhh' && github.triggering_actor == 'jpohhhh' runs-on: ubuntu-22.04 timeout-minutes: 20 permissions: @@ -134,22 +156,26 @@ jobs: DRY_RUN: ${{ github.ref_name == 'native-release-dry' || inputs.dry_run == true }} steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + ref: ${{ github.sha }} + fetch-depth: 1 - uses: dart-lang/setup-dart@6afc89df92d6eb3834022f73cd65adc8cdfcb92d # v1.8.1 with: sdk: ${{ env.DART_VERSION }} - run: dart pub get + - run: echo "ARTIFACTS=$RUNNER_TEMP/artifacts" >> "$GITHUB_ENV" - uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1 with: pattern: native-* - path: artifacts + path: ${{ runner.temp }}/artifacts merge-multiple: true - name: Compare the source keys of all runners (I2) run: | - dart run native_prebuilt:key --list > local-key.txt + dart run native_prebuilt:key --list > "$RUNNER_TEMP/local-key.txt" status=0 - for f in artifacts/key/*.txt; do - if ! diff -u local-key.txt "$f"; then + for f in "$ARTIFACTS"/key/*.txt; do + if ! diff -u "$RUNNER_TEMP/local-key.txt" "$f"; then echo "::error::The source key on $(basename "$f" .txt) differs." status=1 fi @@ -160,9 +186,9 @@ jobs: env: GH_TOKEN: ${{ github.token }} run: | - args=(--staging staging) + args=(--staging "$RUNNER_TEMP/staging") if [[ "$DRY_RUN" == true ]]; then args+=(--dry-run); fi - dart run native_prebuilt:release "${args[@]}" artifacts/out + dart run native_prebuilt:release "${args[@]}" "$ARTIFACTS/out" cat native_artifacts/prebuilt.json - name: Push the manifest branch From 84e2f49e618d4cc1bab61d76e52f471d716614f4 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Mon, 5 Oct 2026 07:40:47 +0000 Subject: [PATCH 12/69] native: prebuilt files of release native-1b3dd752e9a6ede1 --- native_artifacts/prebuilt.json | 183 +++++++++++++++++++++++++++++++++ 1 file changed, 183 insertions(+) create mode 100644 native_artifacts/prebuilt.json diff --git a/native_artifacts/prebuilt.json b/native_artifacts/prebuilt.json new file mode 100644 index 000000000..3e8d8126d --- /dev/null +++ b/native_artifacts/prebuilt.json @@ -0,0 +1,183 @@ +{ + "schema": 1, + "sourceKey": "1b3dd752e9a6ede1b05af61b6a093f34b79d24662eee2d7537412d181c35e78f", + "release": "https://github.com/Telosnex/webcrypto.dart/releases/tag/native-1b3dd752e9a6ede1", + "targets": { + "android-arm": { + "runner": "ubuntu-22.04", + "toolchain": "Android NDK 28.2.13676358", + "minOSVersion": 24, + "files": [ + { + "name": "libwebcrypto.so", + "sha256": "38b99212b651a1bf67239ed4504c4b40f004effdfd23e02a79994c120a49fb22", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-1b3dd752e9a6ede1/android-arm-libwebcrypto.so.gz", + "downloadSha256": "87d5576f8e329e9d66fef63736ac13744da3feb6a7605af5496793b967d1c61a", + "delivery": "bundle", + "asset": "webcrypto.dart" + } + ] + }, + "android-arm64": { + "runner": "ubuntu-22.04", + "toolchain": "Android NDK 28.2.13676358", + "minOSVersion": 24, + "files": [ + { + "name": "libwebcrypto.so", + "sha256": "d635557be84eaf5ec4663aee3c95ccbb2a442ee025fb13ea5b538adf5a9e6b46", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-1b3dd752e9a6ede1/android-arm64-libwebcrypto.so.gz", + "downloadSha256": "1dd59fb190aacd22e0d9b96965ddbaea07bb9e9d66da4cb44914a9ff105a00b7", + "delivery": "bundle", + "asset": "webcrypto.dart" + } + ] + }, + "android-x64": { + "runner": "ubuntu-22.04", + "toolchain": "Android NDK 28.2.13676358", + "minOSVersion": 24, + "files": [ + { + "name": "libwebcrypto.so", + "sha256": "ca2fe2be01d6842ca85106bb5a85a4588ebe53ac40a6e78fcd461c227dde3915", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-1b3dd752e9a6ede1/android-x64-libwebcrypto.so.gz", + "downloadSha256": "902d8be062b9e8761780fca15be3bb7365d4d976c556224c583c2f290528c541", + "delivery": "bundle", + "asset": "webcrypto.dart" + } + ] + }, + "ios-arm64-iphoneos": { + "runner": "macos-15", + "toolchain": "Xcode 16.4", + "minOSVersion": 15, + "files": [ + { + "name": "libwebcrypto.dylib", + "sha256": "abd79243a7e637511a64ad4b61a30feaf2ba310cdc6c268cceede1f378e17d5d", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-1b3dd752e9a6ede1/ios-arm64-iphoneos-libwebcrypto.dylib.gz", + "downloadSha256": "8682ad90ed8bdb834db05a72655c9db9d5c2efc82f3125a916cbb55a3a9e82ce", + "delivery": "bundle", + "asset": "webcrypto.dart" + } + ] + }, + "ios-arm64-iphonesimulator": { + "runner": "macos-15", + "toolchain": "Xcode 16.4", + "minOSVersion": 15, + "files": [ + { + "name": "libwebcrypto.dylib", + "sha256": "c3a1e824a10e981069a42013ba5b7f3a1d21ee41da0aa717a57ec7458a49faff", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-1b3dd752e9a6ede1/ios-arm64-iphonesimulator-libwebcrypto.dylib.gz", + "downloadSha256": "7c483af8f4d38574ffd2044fa3f42b4a57243639280f15456a172e1d80dd5170", + "delivery": "bundle", + "asset": "webcrypto.dart" + } + ] + }, + "ios-x64-iphonesimulator": { + "runner": "macos-15", + "toolchain": "Xcode 16.4", + "minOSVersion": 15, + "files": [ + { + "name": "libwebcrypto.dylib", + "sha256": "8681dc3ba130103f972fa3cfe3867686a72c4a21721cdc8ab763e4c3f6105133", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-1b3dd752e9a6ede1/ios-x64-iphonesimulator-libwebcrypto.dylib.gz", + "downloadSha256": "5dd501b658c6ea6170a4a6c8766036dde16effdec1bca431fd7eb1ebe9d6d4f0", + "delivery": "bundle", + "asset": "webcrypto.dart" + } + ] + }, + "linux-arm64": { + "runner": "ubuntu-22.04-arm", + "toolchain": "gcc (Ubuntu 11.4.0-1ubuntu1~22.04.3) 11.4.0", + "files": [ + { + "name": "libwebcrypto.so", + "sha256": "b314cac1cd104679023513dd43d6e3a2a7c654ee1af84006b9ab4df81324ab9d", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-1b3dd752e9a6ede1/linux-arm64-libwebcrypto.so.gz", + "downloadSha256": "acba41b1f7b4042d993e65030d82eff83679e67dcfb7c36a202c341cf45f1625", + "delivery": "bundle", + "asset": "webcrypto.dart" + } + ] + }, + "linux-x64": { + "runner": "ubuntu-22.04", + "toolchain": "gcc (Ubuntu 11.4.0-1ubuntu1~22.04.3) 11.4.0", + "files": [ + { + "name": "libwebcrypto.so", + "sha256": "b1b2eb86748511627ba6452f3ed8b70e3738cbe057bd49716aac79817532d10a", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-1b3dd752e9a6ede1/linux-x64-libwebcrypto.so.gz", + "downloadSha256": "0c37b4b10b86b2eed622ffc5069f037e3f59b8ec8c8fe71371001e77a71e3c57", + "delivery": "bundle", + "asset": "webcrypto.dart" + } + ] + }, + "macos-arm64": { + "runner": "macos-15", + "toolchain": "Xcode 16.4", + "minOSVersion": 12, + "files": [ + { + "name": "libwebcrypto.dylib", + "sha256": "66a5a2366db930b2865132477b7de1b284926b644fff55ccfe648480c75d3f94", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-1b3dd752e9a6ede1/macos-arm64-libwebcrypto.dylib.gz", + "downloadSha256": "ae0b8038ada762c26a0f95b62e5e6fdc8a87ab3f5adba0aa2ad9b2db276dc787", + "delivery": "bundle", + "asset": "webcrypto.dart" + } + ] + }, + "macos-x64": { + "runner": "macos-15", + "toolchain": "Xcode 16.4", + "minOSVersion": 12, + "files": [ + { + "name": "libwebcrypto.dylib", + "sha256": "8e12dd585a5ea205f387399e04a0207fbe47a1b5fc7dab7b774de76c7e4dd0c6", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-1b3dd752e9a6ede1/macos-x64-libwebcrypto.dylib.gz", + "downloadSha256": "5844d45cc9342043ee90bedd226380e8da524102be12ea2702b57cf16a79dccf", + "delivery": "bundle", + "asset": "webcrypto.dart" + } + ] + }, + "windows-arm64": { + "runner": "windows-11-arm", + "toolchain": "MSVC, Visual Studio 18.10.2, NASM 2.16.03", + "files": [ + { + "name": "webcrypto.dll", + "sha256": "606670950a942502e4b4c3fc6687da40fe689289846c35fe3b1f78b95b5cbd9d", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-1b3dd752e9a6ede1/windows-arm64-webcrypto.dll.gz", + "downloadSha256": "d122d4255a2be7c6b9438bc3668378d4a9c647586e641cd2a3aca8aa6681633f", + "delivery": "bundle", + "asset": "webcrypto.dart" + } + ] + }, + "windows-x64": { + "runner": "windows-2022", + "toolchain": "MSVC, Visual Studio 17.14.41 (September 2026), NASM 2.16.03", + "files": [ + { + "name": "webcrypto.dll", + "sha256": "d0f0994c9b9c10df7e0afd4d6a2113980b963f86fea8c958fa05e4682b296224", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-1b3dd752e9a6ede1/windows-x64-webcrypto.dll.gz", + "downloadSha256": "1911020c9c87c98b5673a428a19ee8ed09d5c62177cd87b0d6c00a261011855c", + "delivery": "bundle", + "asset": "webcrypto.dart" + } + ] + } + } +} From 4f2a5c80276c211d50c3d4004cef9c437dd741e3 Mon Sep 17 00:00:00 2001 From: James O'Leary Date: Mon, 5 Oct 2026 08:25:16 -0700 Subject: [PATCH 13/69] Update native_prebuilt: release iOS libraries for Flutter's iOS 13 hooks --- pubspec.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pubspec.yaml b/pubspec.yaml index ee7e91501..ee799cb1c 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -28,7 +28,7 @@ dependencies: native_prebuilt: git: url: https://github.com/Telosnex/native_prebuilt.git - ref: c2d414c6a3dd34ad57be0952fe6eb291ccf945f7 + ref: e995f4073e335ef57051ac930d05b6c5926fc44a native_toolchain_cmake: ^0.2.7 path: ^1.9.0 code_assets: ^1.0.0 From c78b101a28777a7f24ceb68dc5a2d3e67d7dc851 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Mon, 5 Oct 2026 15:32:51 +0000 Subject: [PATCH 14/69] native: prebuilt files of release native-9366b7f0cd50b313 --- native_artifacts/prebuilt.json | 66 +++++++++++++++++----------------- 1 file changed, 33 insertions(+), 33 deletions(-) diff --git a/native_artifacts/prebuilt.json b/native_artifacts/prebuilt.json index 3e8d8126d..d09306a33 100644 --- a/native_artifacts/prebuilt.json +++ b/native_artifacts/prebuilt.json @@ -1,7 +1,7 @@ { "schema": 1, - "sourceKey": "1b3dd752e9a6ede1b05af61b6a093f34b79d24662eee2d7537412d181c35e78f", - "release": "https://github.com/Telosnex/webcrypto.dart/releases/tag/native-1b3dd752e9a6ede1", + "sourceKey": "9366b7f0cd50b3138ab2492a46dcd12b768ea7499a517df8e7c290f22cdeb01c", + "release": "https://github.com/Telosnex/webcrypto.dart/releases/tag/native-9366b7f0cd50b313", "targets": { "android-arm": { "runner": "ubuntu-22.04", @@ -10,9 +10,9 @@ "files": [ { "name": "libwebcrypto.so", - "sha256": "38b99212b651a1bf67239ed4504c4b40f004effdfd23e02a79994c120a49fb22", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-1b3dd752e9a6ede1/android-arm-libwebcrypto.so.gz", - "downloadSha256": "87d5576f8e329e9d66fef63736ac13744da3feb6a7605af5496793b967d1c61a", + "sha256": "4b67fb3d2cf20316038cf65391e0caa8641e6ab6db653a79db731307e2503752", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-9366b7f0cd50b313/android-arm-libwebcrypto.so.gz", + "downloadSha256": "70f0f6a8dde3d4ad8ca4acaf26eea0a877b4ded58949586f7204d6a92535f70b", "delivery": "bundle", "asset": "webcrypto.dart" } @@ -25,9 +25,9 @@ "files": [ { "name": "libwebcrypto.so", - "sha256": "d635557be84eaf5ec4663aee3c95ccbb2a442ee025fb13ea5b538adf5a9e6b46", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-1b3dd752e9a6ede1/android-arm64-libwebcrypto.so.gz", - "downloadSha256": "1dd59fb190aacd22e0d9b96965ddbaea07bb9e9d66da4cb44914a9ff105a00b7", + "sha256": "572487bfa08aa1b765f0c383355217f6cda0d74508ee3d484995adf44563f50b", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-9366b7f0cd50b313/android-arm64-libwebcrypto.so.gz", + "downloadSha256": "220c25332924ab44247d8ca5af563c88fd31dd6027ef949ee5d6c79218a3490e", "delivery": "bundle", "asset": "webcrypto.dart" } @@ -40,9 +40,9 @@ "files": [ { "name": "libwebcrypto.so", - "sha256": "ca2fe2be01d6842ca85106bb5a85a4588ebe53ac40a6e78fcd461c227dde3915", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-1b3dd752e9a6ede1/android-x64-libwebcrypto.so.gz", - "downloadSha256": "902d8be062b9e8761780fca15be3bb7365d4d976c556224c583c2f290528c541", + "sha256": "46666111e897ec1d3ae5429e170c013928153fdb1cb1c8ed5c17427d1e487f76", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-9366b7f0cd50b313/android-x64-libwebcrypto.so.gz", + "downloadSha256": "44b86def7c9bba961589f7f21aa359d39655cb65624507f1506fc8b11741ff4b", "delivery": "bundle", "asset": "webcrypto.dart" } @@ -51,13 +51,13 @@ "ios-arm64-iphoneos": { "runner": "macos-15", "toolchain": "Xcode 16.4", - "minOSVersion": 15, + "minOSVersion": 13, "files": [ { "name": "libwebcrypto.dylib", - "sha256": "abd79243a7e637511a64ad4b61a30feaf2ba310cdc6c268cceede1f378e17d5d", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-1b3dd752e9a6ede1/ios-arm64-iphoneos-libwebcrypto.dylib.gz", - "downloadSha256": "8682ad90ed8bdb834db05a72655c9db9d5c2efc82f3125a916cbb55a3a9e82ce", + "sha256": "0a7466fc185fcf577ed8c0e6da70f9ad33f596d0b28a487459e87db76a4e49d6", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-9366b7f0cd50b313/ios-arm64-iphoneos-libwebcrypto.dylib.gz", + "downloadSha256": "9ea467950695bd526292a2b815236f3fd14cb8b0e7c00bdde0dbcb04e2770690", "delivery": "bundle", "asset": "webcrypto.dart" } @@ -66,13 +66,13 @@ "ios-arm64-iphonesimulator": { "runner": "macos-15", "toolchain": "Xcode 16.4", - "minOSVersion": 15, + "minOSVersion": 13, "files": [ { "name": "libwebcrypto.dylib", - "sha256": "c3a1e824a10e981069a42013ba5b7f3a1d21ee41da0aa717a57ec7458a49faff", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-1b3dd752e9a6ede1/ios-arm64-iphonesimulator-libwebcrypto.dylib.gz", - "downloadSha256": "7c483af8f4d38574ffd2044fa3f42b4a57243639280f15456a172e1d80dd5170", + "sha256": "3393b602567febb40fdc5b9b54943b8325aee02656d6fcb93bfce80fd3395861", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-9366b7f0cd50b313/ios-arm64-iphonesimulator-libwebcrypto.dylib.gz", + "downloadSha256": "8cc335c7277fc3f58a203ac899b5111c99d79ea49ba89f8715eca8ba7f00186f", "delivery": "bundle", "asset": "webcrypto.dart" } @@ -81,13 +81,13 @@ "ios-x64-iphonesimulator": { "runner": "macos-15", "toolchain": "Xcode 16.4", - "minOSVersion": 15, + "minOSVersion": 13, "files": [ { "name": "libwebcrypto.dylib", - "sha256": "8681dc3ba130103f972fa3cfe3867686a72c4a21721cdc8ab763e4c3f6105133", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-1b3dd752e9a6ede1/ios-x64-iphonesimulator-libwebcrypto.dylib.gz", - "downloadSha256": "5dd501b658c6ea6170a4a6c8766036dde16effdec1bca431fd7eb1ebe9d6d4f0", + "sha256": "084ca81cb6e9d67a2680db33209f4e7d0267917e1ea4e3851405b4099d094845", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-9366b7f0cd50b313/ios-x64-iphonesimulator-libwebcrypto.dylib.gz", + "downloadSha256": "654b113782a02e7991c2113bb8fb5d1fc69926558e1f73ad9edcd7f236419e54", "delivery": "bundle", "asset": "webcrypto.dart" } @@ -100,7 +100,7 @@ { "name": "libwebcrypto.so", "sha256": "b314cac1cd104679023513dd43d6e3a2a7c654ee1af84006b9ab4df81324ab9d", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-1b3dd752e9a6ede1/linux-arm64-libwebcrypto.so.gz", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-9366b7f0cd50b313/linux-arm64-libwebcrypto.so.gz", "downloadSha256": "acba41b1f7b4042d993e65030d82eff83679e67dcfb7c36a202c341cf45f1625", "delivery": "bundle", "asset": "webcrypto.dart" @@ -114,7 +114,7 @@ { "name": "libwebcrypto.so", "sha256": "b1b2eb86748511627ba6452f3ed8b70e3738cbe057bd49716aac79817532d10a", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-1b3dd752e9a6ede1/linux-x64-libwebcrypto.so.gz", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-9366b7f0cd50b313/linux-x64-libwebcrypto.so.gz", "downloadSha256": "0c37b4b10b86b2eed622ffc5069f037e3f59b8ec8c8fe71371001e77a71e3c57", "delivery": "bundle", "asset": "webcrypto.dart" @@ -129,7 +129,7 @@ { "name": "libwebcrypto.dylib", "sha256": "66a5a2366db930b2865132477b7de1b284926b644fff55ccfe648480c75d3f94", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-1b3dd752e9a6ede1/macos-arm64-libwebcrypto.dylib.gz", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-9366b7f0cd50b313/macos-arm64-libwebcrypto.dylib.gz", "downloadSha256": "ae0b8038ada762c26a0f95b62e5e6fdc8a87ab3f5adba0aa2ad9b2db276dc787", "delivery": "bundle", "asset": "webcrypto.dart" @@ -144,7 +144,7 @@ { "name": "libwebcrypto.dylib", "sha256": "8e12dd585a5ea205f387399e04a0207fbe47a1b5fc7dab7b774de76c7e4dd0c6", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-1b3dd752e9a6ede1/macos-x64-libwebcrypto.dylib.gz", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-9366b7f0cd50b313/macos-x64-libwebcrypto.dylib.gz", "downloadSha256": "5844d45cc9342043ee90bedd226380e8da524102be12ea2702b57cf16a79dccf", "delivery": "bundle", "asset": "webcrypto.dart" @@ -157,9 +157,9 @@ "files": [ { "name": "webcrypto.dll", - "sha256": "606670950a942502e4b4c3fc6687da40fe689289846c35fe3b1f78b95b5cbd9d", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-1b3dd752e9a6ede1/windows-arm64-webcrypto.dll.gz", - "downloadSha256": "d122d4255a2be7c6b9438bc3668378d4a9c647586e641cd2a3aca8aa6681633f", + "sha256": "1e21d2d3ed461ebe5194d41734553c1a67fbc9f22bff797b08ed9554925cd815", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-9366b7f0cd50b313/windows-arm64-webcrypto.dll.gz", + "downloadSha256": "9694bb6fcc746e0b417ae0e27b69b3ef1f479bb05a53a4c9409bc51181eead98", "delivery": "bundle", "asset": "webcrypto.dart" } @@ -171,9 +171,9 @@ "files": [ { "name": "webcrypto.dll", - "sha256": "d0f0994c9b9c10df7e0afd4d6a2113980b963f86fea8c958fa05e4682b296224", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-1b3dd752e9a6ede1/windows-x64-webcrypto.dll.gz", - "downloadSha256": "1911020c9c87c98b5673a428a19ee8ed09d5c62177cd87b0d6c00a261011855c", + "sha256": "770de53416644f3bd02e9aa5937d909aa8b534d1a592d0410afad0898701914f", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-9366b7f0cd50b313/windows-x64-webcrypto.dll.gz", + "downloadSha256": "3120f7177f8080aceffe92bcb8db4fd1755e300dc0bcc195406b7f363847b0ce", "delivery": "bundle", "asset": "webcrypto.dart" } From 38ecb850013be182270585f46ad27de434b3bcf1 Mon Sep 17 00:00:00 2001 From: James O'Leary Date: Tue, 6 Oct 2026 11:39:59 -0700 Subject: [PATCH 15/69] native_prebuilt ced023d: package releases take the GitHub latest label --- pubspec.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pubspec.yaml b/pubspec.yaml index ee799cb1c..746342b6c 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -28,7 +28,7 @@ dependencies: native_prebuilt: git: url: https://github.com/Telosnex/native_prebuilt.git - ref: e995f4073e335ef57051ac930d05b6c5926fc44a + ref: ced023d6b94f2a886a765ef164fe552333ab4252 native_toolchain_cmake: ^0.2.7 path: ^1.9.0 code_assets: ^1.0.0 From abadb7f061551323683c184ac1966401c855b4ae Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Tue, 6 Oct 2026 18:44:45 +0000 Subject: [PATCH 16/69] native: prebuilt files of release native-b31ff1121ed67f1c --- native_artifacts/prebuilt.json | 48 +++++++++++++++++----------------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/native_artifacts/prebuilt.json b/native_artifacts/prebuilt.json index d09306a33..f92355355 100644 --- a/native_artifacts/prebuilt.json +++ b/native_artifacts/prebuilt.json @@ -1,7 +1,7 @@ { "schema": 1, - "sourceKey": "9366b7f0cd50b3138ab2492a46dcd12b768ea7499a517df8e7c290f22cdeb01c", - "release": "https://github.com/Telosnex/webcrypto.dart/releases/tag/native-9366b7f0cd50b313", + "sourceKey": "b31ff1121ed67f1cac88d07abb79f6dfd218efc8304486c5421d012b647ccd84", + "release": "https://github.com/Telosnex/webcrypto.dart/releases/tag/native-b31ff1121ed67f1c", "targets": { "android-arm": { "runner": "ubuntu-22.04", @@ -10,9 +10,9 @@ "files": [ { "name": "libwebcrypto.so", - "sha256": "4b67fb3d2cf20316038cf65391e0caa8641e6ab6db653a79db731307e2503752", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-9366b7f0cd50b313/android-arm-libwebcrypto.so.gz", - "downloadSha256": "70f0f6a8dde3d4ad8ca4acaf26eea0a877b4ded58949586f7204d6a92535f70b", + "sha256": "eb635ce54a9ec437bb7f29ddbcc4e76645de156a435638f846699d0b8d952f5b", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-b31ff1121ed67f1c/android-arm-libwebcrypto.so.gz", + "downloadSha256": "5949eb0ac475ab3a087ce6d5489538693686a86614f8d06a0352c1e6637f9a4b", "delivery": "bundle", "asset": "webcrypto.dart" } @@ -25,9 +25,9 @@ "files": [ { "name": "libwebcrypto.so", - "sha256": "572487bfa08aa1b765f0c383355217f6cda0d74508ee3d484995adf44563f50b", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-9366b7f0cd50b313/android-arm64-libwebcrypto.so.gz", - "downloadSha256": "220c25332924ab44247d8ca5af563c88fd31dd6027ef949ee5d6c79218a3490e", + "sha256": "1ed1d19b9f9cac6b088447ff76ea273e9055110471c2d0292b2526fe22ba308f", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-b31ff1121ed67f1c/android-arm64-libwebcrypto.so.gz", + "downloadSha256": "fa0ccb91f51409658c98570a9cd518d36fe798d583e795b4cda7de82c6b1c6f2", "delivery": "bundle", "asset": "webcrypto.dart" } @@ -40,9 +40,9 @@ "files": [ { "name": "libwebcrypto.so", - "sha256": "46666111e897ec1d3ae5429e170c013928153fdb1cb1c8ed5c17427d1e487f76", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-9366b7f0cd50b313/android-x64-libwebcrypto.so.gz", - "downloadSha256": "44b86def7c9bba961589f7f21aa359d39655cb65624507f1506fc8b11741ff4b", + "sha256": "91384fefa010427b3732bb0826dcb91b1ffaf77dbd9d6ddc5a7281ecf1d355eb", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-b31ff1121ed67f1c/android-x64-libwebcrypto.so.gz", + "downloadSha256": "a1190a094299aa0fe5e18fcbdb900b613326aba48e858bd3b159a5776c31cd7e", "delivery": "bundle", "asset": "webcrypto.dart" } @@ -56,7 +56,7 @@ { "name": "libwebcrypto.dylib", "sha256": "0a7466fc185fcf577ed8c0e6da70f9ad33f596d0b28a487459e87db76a4e49d6", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-9366b7f0cd50b313/ios-arm64-iphoneos-libwebcrypto.dylib.gz", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-b31ff1121ed67f1c/ios-arm64-iphoneos-libwebcrypto.dylib.gz", "downloadSha256": "9ea467950695bd526292a2b815236f3fd14cb8b0e7c00bdde0dbcb04e2770690", "delivery": "bundle", "asset": "webcrypto.dart" @@ -71,7 +71,7 @@ { "name": "libwebcrypto.dylib", "sha256": "3393b602567febb40fdc5b9b54943b8325aee02656d6fcb93bfce80fd3395861", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-9366b7f0cd50b313/ios-arm64-iphonesimulator-libwebcrypto.dylib.gz", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-b31ff1121ed67f1c/ios-arm64-iphonesimulator-libwebcrypto.dylib.gz", "downloadSha256": "8cc335c7277fc3f58a203ac899b5111c99d79ea49ba89f8715eca8ba7f00186f", "delivery": "bundle", "asset": "webcrypto.dart" @@ -86,7 +86,7 @@ { "name": "libwebcrypto.dylib", "sha256": "084ca81cb6e9d67a2680db33209f4e7d0267917e1ea4e3851405b4099d094845", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-9366b7f0cd50b313/ios-x64-iphonesimulator-libwebcrypto.dylib.gz", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-b31ff1121ed67f1c/ios-x64-iphonesimulator-libwebcrypto.dylib.gz", "downloadSha256": "654b113782a02e7991c2113bb8fb5d1fc69926558e1f73ad9edcd7f236419e54", "delivery": "bundle", "asset": "webcrypto.dart" @@ -100,7 +100,7 @@ { "name": "libwebcrypto.so", "sha256": "b314cac1cd104679023513dd43d6e3a2a7c654ee1af84006b9ab4df81324ab9d", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-9366b7f0cd50b313/linux-arm64-libwebcrypto.so.gz", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-b31ff1121ed67f1c/linux-arm64-libwebcrypto.so.gz", "downloadSha256": "acba41b1f7b4042d993e65030d82eff83679e67dcfb7c36a202c341cf45f1625", "delivery": "bundle", "asset": "webcrypto.dart" @@ -114,7 +114,7 @@ { "name": "libwebcrypto.so", "sha256": "b1b2eb86748511627ba6452f3ed8b70e3738cbe057bd49716aac79817532d10a", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-9366b7f0cd50b313/linux-x64-libwebcrypto.so.gz", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-b31ff1121ed67f1c/linux-x64-libwebcrypto.so.gz", "downloadSha256": "0c37b4b10b86b2eed622ffc5069f037e3f59b8ec8c8fe71371001e77a71e3c57", "delivery": "bundle", "asset": "webcrypto.dart" @@ -129,7 +129,7 @@ { "name": "libwebcrypto.dylib", "sha256": "66a5a2366db930b2865132477b7de1b284926b644fff55ccfe648480c75d3f94", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-9366b7f0cd50b313/macos-arm64-libwebcrypto.dylib.gz", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-b31ff1121ed67f1c/macos-arm64-libwebcrypto.dylib.gz", "downloadSha256": "ae0b8038ada762c26a0f95b62e5e6fdc8a87ab3f5adba0aa2ad9b2db276dc787", "delivery": "bundle", "asset": "webcrypto.dart" @@ -144,7 +144,7 @@ { "name": "libwebcrypto.dylib", "sha256": "8e12dd585a5ea205f387399e04a0207fbe47a1b5fc7dab7b774de76c7e4dd0c6", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-9366b7f0cd50b313/macos-x64-libwebcrypto.dylib.gz", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-b31ff1121ed67f1c/macos-x64-libwebcrypto.dylib.gz", "downloadSha256": "5844d45cc9342043ee90bedd226380e8da524102be12ea2702b57cf16a79dccf", "delivery": "bundle", "asset": "webcrypto.dart" @@ -157,9 +157,9 @@ "files": [ { "name": "webcrypto.dll", - "sha256": "1e21d2d3ed461ebe5194d41734553c1a67fbc9f22bff797b08ed9554925cd815", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-9366b7f0cd50b313/windows-arm64-webcrypto.dll.gz", - "downloadSha256": "9694bb6fcc746e0b417ae0e27b69b3ef1f479bb05a53a4c9409bc51181eead98", + "sha256": "f3c4dea462be47c729121a1dc5b1ae9c15aedf4f078fef9a9f0624518cd91f79", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-b31ff1121ed67f1c/windows-arm64-webcrypto.dll.gz", + "downloadSha256": "9089c7640931cf73c4c049d68998cebc0249a4fe498b7e7e77eef89ba5088a9b", "delivery": "bundle", "asset": "webcrypto.dart" } @@ -171,9 +171,9 @@ "files": [ { "name": "webcrypto.dll", - "sha256": "770de53416644f3bd02e9aa5937d909aa8b534d1a592d0410afad0898701914f", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-9366b7f0cd50b313/windows-x64-webcrypto.dll.gz", - "downloadSha256": "3120f7177f8080aceffe92bcb8db4fd1755e300dc0bcc195406b7f363847b0ce", + "sha256": "e3761e7ef61c9188d9477dd672c463d0d676d9cfc14d463c67d8afd296a6da1f", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-b31ff1121ed67f1c/windows-x64-webcrypto.dll.gz", + "downloadSha256": "bdbbcaa82ed91c36985a31f21aa868d8470af3564ee30c10d8d5b6fbc9f52211", "delivery": "bundle", "asset": "webcrypto.dart" } From 5ad2c573fe6d8b1d6ffc9072ad57e4a078696ebf Mon Sep 17 00:00:00 2001 From: James O'Leary Date: Tue, 6 Oct 2026 12:14:31 -0700 Subject: [PATCH 17/69] LICENSE lists the native code's licenses in the Flutter format Each bundled native library's notice is a separate license in LICENSE, separated by 80 '-' as Flutter's license collector expects, so apps show them. native_prebuilt 59c965b excludes LICENSE from the source key. --- LICENSE | 259 +++++++++++++++++++++++++++++++++++++++++++++++++++ pubspec.yaml | 2 +- 2 files changed, 260 insertions(+), 1 deletion(-) diff --git a/LICENSE b/LICENSE index d64569567..7ea910598 100644 --- a/LICENSE +++ b/LICENSE @@ -1,3 +1,4 @@ +webcrypto Apache License Version 2.0, January 2004 @@ -200,3 +201,261 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. +-------------------------------------------------------------------------------- +BoringSSL + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + + +Licenses for support code +------------------------- + +Parts of the TLS test suite are under the Go license. This code is not included +in BoringSSL (i.e. libcrypto and libssl) when compiled, however, so +distributing code linked against BoringSSL does not trigger this license: + +Copyright (c) 2009 The Go Authors. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + * Redistributions of source code must retain the above copyright +notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above +copyright notice, this list of conditions and the following disclaimer +in the documentation and/or other materials provided with the +distribution. + * Neither the name of Google Inc. nor the names of its +contributors may be used to endorse or promote products derived from +this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +-------------------------------------------------------------------------------- +fiat-crypto + +The Apache License, Version 2.0 (Apache-2.0) + +Copyright 2015-2020 the fiat-crypto authors (see the AUTHORS file) + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. diff --git a/pubspec.yaml b/pubspec.yaml index 746342b6c..6d71eb8df 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -28,7 +28,7 @@ dependencies: native_prebuilt: git: url: https://github.com/Telosnex/native_prebuilt.git - ref: ced023d6b94f2a886a765ef164fe552333ab4252 + ref: 59c965b2b4e3fee14e3efa316ed7f7dea4cc49dd native_toolchain_cmake: ^0.2.7 path: ^1.9.0 code_assets: ^1.0.0 From 9d34c8250ed23aca2e77737d57f5aa0f2f244c54 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Tue, 6 Oct 2026 19:20:08 +0000 Subject: [PATCH 18/69] native: prebuilt files of release native-0770b1742e68c43c --- native_artifacts/prebuilt.json | 48 +++++++++++++++++----------------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/native_artifacts/prebuilt.json b/native_artifacts/prebuilt.json index f92355355..c0764da4b 100644 --- a/native_artifacts/prebuilt.json +++ b/native_artifacts/prebuilt.json @@ -1,7 +1,7 @@ { "schema": 1, - "sourceKey": "b31ff1121ed67f1cac88d07abb79f6dfd218efc8304486c5421d012b647ccd84", - "release": "https://github.com/Telosnex/webcrypto.dart/releases/tag/native-b31ff1121ed67f1c", + "sourceKey": "0770b1742e68c43ca0f8a29597f701f94b956b000caceac642968a00c910dd1e", + "release": "https://github.com/Telosnex/webcrypto.dart/releases/tag/native-0770b1742e68c43c", "targets": { "android-arm": { "runner": "ubuntu-22.04", @@ -10,9 +10,9 @@ "files": [ { "name": "libwebcrypto.so", - "sha256": "eb635ce54a9ec437bb7f29ddbcc4e76645de156a435638f846699d0b8d952f5b", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-b31ff1121ed67f1c/android-arm-libwebcrypto.so.gz", - "downloadSha256": "5949eb0ac475ab3a087ce6d5489538693686a86614f8d06a0352c1e6637f9a4b", + "sha256": "c8461740f2f8ae46f3afcca91834c097e9a7e5f59a0b89f3feec6e3f47c8eb18", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-0770b1742e68c43c/android-arm-libwebcrypto.so.gz", + "downloadSha256": "6241bde450867eac95ffb4f1a38ca928fdf7085e4494ea043ca3f090c9773afe", "delivery": "bundle", "asset": "webcrypto.dart" } @@ -25,9 +25,9 @@ "files": [ { "name": "libwebcrypto.so", - "sha256": "1ed1d19b9f9cac6b088447ff76ea273e9055110471c2d0292b2526fe22ba308f", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-b31ff1121ed67f1c/android-arm64-libwebcrypto.so.gz", - "downloadSha256": "fa0ccb91f51409658c98570a9cd518d36fe798d583e795b4cda7de82c6b1c6f2", + "sha256": "d811c6db934f3bd5fcc4f0fd6b6e3a18db87bb4b702ffc4d626f4bf0414fbcb3", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-0770b1742e68c43c/android-arm64-libwebcrypto.so.gz", + "downloadSha256": "56c59a5c58838d3eed0597b8ceede0c10040293cea47786158f42a8abb40c0a7", "delivery": "bundle", "asset": "webcrypto.dart" } @@ -40,9 +40,9 @@ "files": [ { "name": "libwebcrypto.so", - "sha256": "91384fefa010427b3732bb0826dcb91b1ffaf77dbd9d6ddc5a7281ecf1d355eb", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-b31ff1121ed67f1c/android-x64-libwebcrypto.so.gz", - "downloadSha256": "a1190a094299aa0fe5e18fcbdb900b613326aba48e858bd3b159a5776c31cd7e", + "sha256": "7589d185665a3120efbd4b5d6f8597e66a5f34f64facb64e53634b40ac423fb3", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-0770b1742e68c43c/android-x64-libwebcrypto.so.gz", + "downloadSha256": "865872959412df917edd110c0839a036e057574a8fcd2c41c2ac987ffffffbb5", "delivery": "bundle", "asset": "webcrypto.dart" } @@ -56,7 +56,7 @@ { "name": "libwebcrypto.dylib", "sha256": "0a7466fc185fcf577ed8c0e6da70f9ad33f596d0b28a487459e87db76a4e49d6", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-b31ff1121ed67f1c/ios-arm64-iphoneos-libwebcrypto.dylib.gz", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-0770b1742e68c43c/ios-arm64-iphoneos-libwebcrypto.dylib.gz", "downloadSha256": "9ea467950695bd526292a2b815236f3fd14cb8b0e7c00bdde0dbcb04e2770690", "delivery": "bundle", "asset": "webcrypto.dart" @@ -71,7 +71,7 @@ { "name": "libwebcrypto.dylib", "sha256": "3393b602567febb40fdc5b9b54943b8325aee02656d6fcb93bfce80fd3395861", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-b31ff1121ed67f1c/ios-arm64-iphonesimulator-libwebcrypto.dylib.gz", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-0770b1742e68c43c/ios-arm64-iphonesimulator-libwebcrypto.dylib.gz", "downloadSha256": "8cc335c7277fc3f58a203ac899b5111c99d79ea49ba89f8715eca8ba7f00186f", "delivery": "bundle", "asset": "webcrypto.dart" @@ -86,7 +86,7 @@ { "name": "libwebcrypto.dylib", "sha256": "084ca81cb6e9d67a2680db33209f4e7d0267917e1ea4e3851405b4099d094845", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-b31ff1121ed67f1c/ios-x64-iphonesimulator-libwebcrypto.dylib.gz", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-0770b1742e68c43c/ios-x64-iphonesimulator-libwebcrypto.dylib.gz", "downloadSha256": "654b113782a02e7991c2113bb8fb5d1fc69926558e1f73ad9edcd7f236419e54", "delivery": "bundle", "asset": "webcrypto.dart" @@ -100,7 +100,7 @@ { "name": "libwebcrypto.so", "sha256": "b314cac1cd104679023513dd43d6e3a2a7c654ee1af84006b9ab4df81324ab9d", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-b31ff1121ed67f1c/linux-arm64-libwebcrypto.so.gz", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-0770b1742e68c43c/linux-arm64-libwebcrypto.so.gz", "downloadSha256": "acba41b1f7b4042d993e65030d82eff83679e67dcfb7c36a202c341cf45f1625", "delivery": "bundle", "asset": "webcrypto.dart" @@ -114,7 +114,7 @@ { "name": "libwebcrypto.so", "sha256": "b1b2eb86748511627ba6452f3ed8b70e3738cbe057bd49716aac79817532d10a", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-b31ff1121ed67f1c/linux-x64-libwebcrypto.so.gz", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-0770b1742e68c43c/linux-x64-libwebcrypto.so.gz", "downloadSha256": "0c37b4b10b86b2eed622ffc5069f037e3f59b8ec8c8fe71371001e77a71e3c57", "delivery": "bundle", "asset": "webcrypto.dart" @@ -129,7 +129,7 @@ { "name": "libwebcrypto.dylib", "sha256": "66a5a2366db930b2865132477b7de1b284926b644fff55ccfe648480c75d3f94", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-b31ff1121ed67f1c/macos-arm64-libwebcrypto.dylib.gz", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-0770b1742e68c43c/macos-arm64-libwebcrypto.dylib.gz", "downloadSha256": "ae0b8038ada762c26a0f95b62e5e6fdc8a87ab3f5adba0aa2ad9b2db276dc787", "delivery": "bundle", "asset": "webcrypto.dart" @@ -144,7 +144,7 @@ { "name": "libwebcrypto.dylib", "sha256": "8e12dd585a5ea205f387399e04a0207fbe47a1b5fc7dab7b774de76c7e4dd0c6", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-b31ff1121ed67f1c/macos-x64-libwebcrypto.dylib.gz", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-0770b1742e68c43c/macos-x64-libwebcrypto.dylib.gz", "downloadSha256": "5844d45cc9342043ee90bedd226380e8da524102be12ea2702b57cf16a79dccf", "delivery": "bundle", "asset": "webcrypto.dart" @@ -157,9 +157,9 @@ "files": [ { "name": "webcrypto.dll", - "sha256": "f3c4dea462be47c729121a1dc5b1ae9c15aedf4f078fef9a9f0624518cd91f79", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-b31ff1121ed67f1c/windows-arm64-webcrypto.dll.gz", - "downloadSha256": "9089c7640931cf73c4c049d68998cebc0249a4fe498b7e7e77eef89ba5088a9b", + "sha256": "7549298fe7f1bea53cd08d7420b501f299aee0bf5332d8d140f46658861c6a95", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-0770b1742e68c43c/windows-arm64-webcrypto.dll.gz", + "downloadSha256": "932ff467c7fa55fbd8020181268ff4a52f27468608a7aea4bbf088be29bda518", "delivery": "bundle", "asset": "webcrypto.dart" } @@ -171,9 +171,9 @@ "files": [ { "name": "webcrypto.dll", - "sha256": "e3761e7ef61c9188d9477dd672c463d0d676d9cfc14d463c67d8afd296a6da1f", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-b31ff1121ed67f1c/windows-x64-webcrypto.dll.gz", - "downloadSha256": "bdbbcaa82ed91c36985a31f21aa868d8470af3564ee30c10d8d5b6fbc9f52211", + "sha256": "c5b9957db6ab0d509c664ccd0852a690f7d853d44de8ddfa706c3c264384c733", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-0770b1742e68c43c/windows-x64-webcrypto.dll.gz", + "downloadSha256": "ff61d3cca486a9f1dbbe158843d363fc15c5261dff3a0b8062bc809a3bfe1e57", "delivery": "bundle", "asset": "webcrypto.dart" } From 5bee8393872780b961548a951f04ccbde89ed5ea Mon Sep 17 00:00:00 2001 From: James O'Leary Date: Mon, 12 Oct 2026 14:28:41 -0700 Subject: [PATCH 19/69] Point ADR 005 references to native_prebuilt ADR 001 --- .github/workflows/native_release.yml | 2 +- hook/build.dart | 4 ++-- native_artifacts/source_excludes.txt | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/native_release.yml b/.github/workflows/native_release.yml index 3a0e44642..06da71c7b 100644 --- a/.github/workflows/native_release.yml +++ b/.github/workflows/native_release.yml @@ -1,5 +1,5 @@ # Builds the native library for every target and publishes the files as a -# GitHub release (fllama ADR 005 D5, D6, D8). Start it with +# GitHub release (native_prebuilt ADR 001 D5, D6, D8). Start it with # git push -f origin HEAD:native-release (release) # git push -f origin HEAD:native-release-dry (build only, no upload) # or with workflow_dispatch. On success it pushes the branch diff --git a/hook/build.dart b/hook/build.dart index 007c5f24e..eee7d2fe2 100644 --- a/hook/build.dart +++ b/hook/build.dart @@ -278,8 +278,8 @@ String _libraryFileName(OS os) => switch (os) { _ => throw UnsupportedError('package:webcrypto does not support ${os.name}.'), }; -/// The key of the local build cache (ADR 005 D4): the source key of the -/// package and the host toolchain. +/// The key of the local build cache (native_prebuilt ADR 001 D4): the source +/// key of the package and the host toolchain. Future _computeBuildKey({ required CodeConfig code, required Map defines, diff --git a/native_artifacts/source_excludes.txt b/native_artifacts/source_excludes.txt index 7c87bd0a1..fe8a5ff33 100644 --- a/native_artifacts/source_excludes.txt +++ b/native_artifacts/source_excludes.txt @@ -1,4 +1,4 @@ -# Files that the native build does not read (ADR 005 D3). +# Files that the native build does not read (native_prebuilt ADR 001 D3). doc/ tool/ analysis_options.yaml From fd1c38d9e5b00d956410af2042cfacfba9445a5d Mon Sep 17 00:00:00 2001 From: James O'Leary Date: Tue, 13 Oct 2026 14:16:20 -0700 Subject: [PATCH 20/69] Update native_prebuilt to bf48992 (ADR 002 web runtime files) The native files do not change. Telosnex can then drop its native_prebuilt dependency override. --- pubspec.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pubspec.yaml b/pubspec.yaml index 6d71eb8df..2db969dd7 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -28,7 +28,7 @@ dependencies: native_prebuilt: git: url: https://github.com/Telosnex/native_prebuilt.git - ref: 59c965b2b4e3fee14e3efa316ed7f7dea4cc49dd + ref: bf489921cbd79f96a41d3ac08d88df6cdab97366 native_toolchain_cmake: ^0.2.7 path: ^1.9.0 code_assets: ^1.0.0 From 7a0fe777c01dbfdafe97b7b5a81644aec0222d27 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Tue, 13 Oct 2026 21:20:47 +0000 Subject: [PATCH 21/69] native: prebuilt files of release native-fe3ce5c624211592 --- native_artifacts/prebuilt.json | 50 +++++++++++++++++----------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/native_artifacts/prebuilt.json b/native_artifacts/prebuilt.json index c0764da4b..037243fca 100644 --- a/native_artifacts/prebuilt.json +++ b/native_artifacts/prebuilt.json @@ -1,7 +1,7 @@ { "schema": 1, - "sourceKey": "0770b1742e68c43ca0f8a29597f701f94b956b000caceac642968a00c910dd1e", - "release": "https://github.com/Telosnex/webcrypto.dart/releases/tag/native-0770b1742e68c43c", + "sourceKey": "fe3ce5c624211592aa1467b56944068f2fb25ba595e66a9d89b99d6e175b02e7", + "release": "https://github.com/Telosnex/webcrypto.dart/releases/tag/native-fe3ce5c624211592", "targets": { "android-arm": { "runner": "ubuntu-22.04", @@ -10,9 +10,9 @@ "files": [ { "name": "libwebcrypto.so", - "sha256": "c8461740f2f8ae46f3afcca91834c097e9a7e5f59a0b89f3feec6e3f47c8eb18", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-0770b1742e68c43c/android-arm-libwebcrypto.so.gz", - "downloadSha256": "6241bde450867eac95ffb4f1a38ca928fdf7085e4494ea043ca3f090c9773afe", + "sha256": "1438754e06332d363f1e0873dd1847d6c0917e59038ccaa16c92051fa392696a", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-fe3ce5c624211592/android-arm-libwebcrypto.so.gz", + "downloadSha256": "7d065eda343389fdcd0cb6dc39a36d642de1f08a368c244c9e0df108608c8f57", "delivery": "bundle", "asset": "webcrypto.dart" } @@ -25,9 +25,9 @@ "files": [ { "name": "libwebcrypto.so", - "sha256": "d811c6db934f3bd5fcc4f0fd6b6e3a18db87bb4b702ffc4d626f4bf0414fbcb3", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-0770b1742e68c43c/android-arm64-libwebcrypto.so.gz", - "downloadSha256": "56c59a5c58838d3eed0597b8ceede0c10040293cea47786158f42a8abb40c0a7", + "sha256": "ba0ee1965febd77a1abcc01a13d69f8b7de5c9909a3a4ec1cb1f66732f3c9f65", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-fe3ce5c624211592/android-arm64-libwebcrypto.so.gz", + "downloadSha256": "455ce669b4ecb59676af7789e3cf51fee7d6a3c03e6c1e6fd811253cbb87fa82", "delivery": "bundle", "asset": "webcrypto.dart" } @@ -40,9 +40,9 @@ "files": [ { "name": "libwebcrypto.so", - "sha256": "7589d185665a3120efbd4b5d6f8597e66a5f34f64facb64e53634b40ac423fb3", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-0770b1742e68c43c/android-x64-libwebcrypto.so.gz", - "downloadSha256": "865872959412df917edd110c0839a036e057574a8fcd2c41c2ac987ffffffbb5", + "sha256": "ee7d5fa412333ea44baf47577758b862157ccdf2da90b9ad1d97bf7186c209ba", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-fe3ce5c624211592/android-x64-libwebcrypto.so.gz", + "downloadSha256": "1efb755d83d8cd080cd61fa320b58a256d5f9412fca0287df1a0cb3cec516697", "delivery": "bundle", "asset": "webcrypto.dart" } @@ -56,7 +56,7 @@ { "name": "libwebcrypto.dylib", "sha256": "0a7466fc185fcf577ed8c0e6da70f9ad33f596d0b28a487459e87db76a4e49d6", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-0770b1742e68c43c/ios-arm64-iphoneos-libwebcrypto.dylib.gz", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-fe3ce5c624211592/ios-arm64-iphoneos-libwebcrypto.dylib.gz", "downloadSha256": "9ea467950695bd526292a2b815236f3fd14cb8b0e7c00bdde0dbcb04e2770690", "delivery": "bundle", "asset": "webcrypto.dart" @@ -71,7 +71,7 @@ { "name": "libwebcrypto.dylib", "sha256": "3393b602567febb40fdc5b9b54943b8325aee02656d6fcb93bfce80fd3395861", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-0770b1742e68c43c/ios-arm64-iphonesimulator-libwebcrypto.dylib.gz", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-fe3ce5c624211592/ios-arm64-iphonesimulator-libwebcrypto.dylib.gz", "downloadSha256": "8cc335c7277fc3f58a203ac899b5111c99d79ea49ba89f8715eca8ba7f00186f", "delivery": "bundle", "asset": "webcrypto.dart" @@ -86,7 +86,7 @@ { "name": "libwebcrypto.dylib", "sha256": "084ca81cb6e9d67a2680db33209f4e7d0267917e1ea4e3851405b4099d094845", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-0770b1742e68c43c/ios-x64-iphonesimulator-libwebcrypto.dylib.gz", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-fe3ce5c624211592/ios-x64-iphonesimulator-libwebcrypto.dylib.gz", "downloadSha256": "654b113782a02e7991c2113bb8fb5d1fc69926558e1f73ad9edcd7f236419e54", "delivery": "bundle", "asset": "webcrypto.dart" @@ -100,7 +100,7 @@ { "name": "libwebcrypto.so", "sha256": "b314cac1cd104679023513dd43d6e3a2a7c654ee1af84006b9ab4df81324ab9d", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-0770b1742e68c43c/linux-arm64-libwebcrypto.so.gz", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-fe3ce5c624211592/linux-arm64-libwebcrypto.so.gz", "downloadSha256": "acba41b1f7b4042d993e65030d82eff83679e67dcfb7c36a202c341cf45f1625", "delivery": "bundle", "asset": "webcrypto.dart" @@ -114,7 +114,7 @@ { "name": "libwebcrypto.so", "sha256": "b1b2eb86748511627ba6452f3ed8b70e3738cbe057bd49716aac79817532d10a", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-0770b1742e68c43c/linux-x64-libwebcrypto.so.gz", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-fe3ce5c624211592/linux-x64-libwebcrypto.so.gz", "downloadSha256": "0c37b4b10b86b2eed622ffc5069f037e3f59b8ec8c8fe71371001e77a71e3c57", "delivery": "bundle", "asset": "webcrypto.dart" @@ -129,7 +129,7 @@ { "name": "libwebcrypto.dylib", "sha256": "66a5a2366db930b2865132477b7de1b284926b644fff55ccfe648480c75d3f94", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-0770b1742e68c43c/macos-arm64-libwebcrypto.dylib.gz", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-fe3ce5c624211592/macos-arm64-libwebcrypto.dylib.gz", "downloadSha256": "ae0b8038ada762c26a0f95b62e5e6fdc8a87ab3f5adba0aa2ad9b2db276dc787", "delivery": "bundle", "asset": "webcrypto.dart" @@ -144,7 +144,7 @@ { "name": "libwebcrypto.dylib", "sha256": "8e12dd585a5ea205f387399e04a0207fbe47a1b5fc7dab7b774de76c7e4dd0c6", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-0770b1742e68c43c/macos-x64-libwebcrypto.dylib.gz", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-fe3ce5c624211592/macos-x64-libwebcrypto.dylib.gz", "downloadSha256": "5844d45cc9342043ee90bedd226380e8da524102be12ea2702b57cf16a79dccf", "delivery": "bundle", "asset": "webcrypto.dart" @@ -153,13 +153,13 @@ }, "windows-arm64": { "runner": "windows-11-arm", - "toolchain": "MSVC, Visual Studio 18.10.2, NASM 2.16.03", + "toolchain": "MSVC, Visual Studio 18.10.3, NASM 2.16.03", "files": [ { "name": "webcrypto.dll", - "sha256": "7549298fe7f1bea53cd08d7420b501f299aee0bf5332d8d140f46658861c6a95", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-0770b1742e68c43c/windows-arm64-webcrypto.dll.gz", - "downloadSha256": "932ff467c7fa55fbd8020181268ff4a52f27468608a7aea4bbf088be29bda518", + "sha256": "994878356cce9921180939fa810476f13f5ff1ac6cd4144ee8cc291edaa4630b", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-fe3ce5c624211592/windows-arm64-webcrypto.dll.gz", + "downloadSha256": "a7ab7e1e2e63e6c31fa3e092c1c1dc107b437b8bd222b7be2f3d1d39e4dfd4fa", "delivery": "bundle", "asset": "webcrypto.dart" } @@ -171,9 +171,9 @@ "files": [ { "name": "webcrypto.dll", - "sha256": "c5b9957db6ab0d509c664ccd0852a690f7d853d44de8ddfa706c3c264384c733", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-0770b1742e68c43c/windows-x64-webcrypto.dll.gz", - "downloadSha256": "ff61d3cca486a9f1dbbe158843d363fc15c5261dff3a0b8062bc809a3bfe1e57", + "sha256": "0208a0998ca51b6476ae3645e66306e96770db1ad87cf83bcf1d85caed05f44b", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-fe3ce5c624211592/windows-x64-webcrypto.dll.gz", + "downloadSha256": "0a2a13a4f4b156a23c2ec9b203a2bc6d3496b28df64ffa22236b1f118c5f0f25", "delivery": "bundle", "asset": "webcrypto.dart" } From 9acd1156b6b4504e1bae161f6d669a0948558395 Mon Sep 17 00:00:00 2001 From: James O'Leary Date: Thu, 15 Oct 2026 09:37:04 -0700 Subject: [PATCH 22/69] Update native_prebuilt to dfe2a5b (Flatpak support) Pinned sources, defaults.json and host_toolchain. The app overrides native_prebuilt to this commit; with every package on it, the override goes. The source key changes, so this needs a new release. --- pubspec.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pubspec.yaml b/pubspec.yaml index 2db969dd7..d61c7d353 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -28,7 +28,7 @@ dependencies: native_prebuilt: git: url: https://github.com/Telosnex/native_prebuilt.git - ref: bf489921cbd79f96a41d3ac08d88df6cdab97366 + ref: dfe2a5bfeb95426e87f6243cab14f4affd879b38 native_toolchain_cmake: ^0.2.7 path: ^1.9.0 code_assets: ^1.0.0 From 2c20ea3d11cfe31b89dbfeb03089fe49627d3650 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Thu, 15 Oct 2026 16:50:40 +0000 Subject: [PATCH 23/69] native: prebuilt files of release native-44c6a51d2f6a1907 --- native_artifacts/prebuilt.json | 48 +++++++++++++++++----------------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/native_artifacts/prebuilt.json b/native_artifacts/prebuilt.json index 037243fca..1c238ce10 100644 --- a/native_artifacts/prebuilt.json +++ b/native_artifacts/prebuilt.json @@ -1,7 +1,7 @@ { "schema": 1, - "sourceKey": "fe3ce5c624211592aa1467b56944068f2fb25ba595e66a9d89b99d6e175b02e7", - "release": "https://github.com/Telosnex/webcrypto.dart/releases/tag/native-fe3ce5c624211592", + "sourceKey": "44c6a51d2f6a1907b35dc92b88b780cb14eaf3ab9a30b261e0e5d8815056adc1", + "release": "https://github.com/Telosnex/webcrypto.dart/releases/tag/native-44c6a51d2f6a1907", "targets": { "android-arm": { "runner": "ubuntu-22.04", @@ -10,9 +10,9 @@ "files": [ { "name": "libwebcrypto.so", - "sha256": "1438754e06332d363f1e0873dd1847d6c0917e59038ccaa16c92051fa392696a", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-fe3ce5c624211592/android-arm-libwebcrypto.so.gz", - "downloadSha256": "7d065eda343389fdcd0cb6dc39a36d642de1f08a368c244c9e0df108608c8f57", + "sha256": "bed1b3b78342772a7b5922e149ff9397f1c6f9da9e56f0374d92868e94752fa8", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-44c6a51d2f6a1907/android-arm-libwebcrypto.so.gz", + "downloadSha256": "6aaf7e5db566a46712f36fcdd8dc4b2e6cbba55fa3339dea42df2ed4ebe5b768", "delivery": "bundle", "asset": "webcrypto.dart" } @@ -25,9 +25,9 @@ "files": [ { "name": "libwebcrypto.so", - "sha256": "ba0ee1965febd77a1abcc01a13d69f8b7de5c9909a3a4ec1cb1f66732f3c9f65", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-fe3ce5c624211592/android-arm64-libwebcrypto.so.gz", - "downloadSha256": "455ce669b4ecb59676af7789e3cf51fee7d6a3c03e6c1e6fd811253cbb87fa82", + "sha256": "6fac3d7eeb8feadc7d48b4fae54271359f71ee0b80f931f1cb1fd31c5d622e69", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-44c6a51d2f6a1907/android-arm64-libwebcrypto.so.gz", + "downloadSha256": "65b7b72c0daa5718e100080331a541808c98e35541c17395c00d75289b9f4e51", "delivery": "bundle", "asset": "webcrypto.dart" } @@ -40,9 +40,9 @@ "files": [ { "name": "libwebcrypto.so", - "sha256": "ee7d5fa412333ea44baf47577758b862157ccdf2da90b9ad1d97bf7186c209ba", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-fe3ce5c624211592/android-x64-libwebcrypto.so.gz", - "downloadSha256": "1efb755d83d8cd080cd61fa320b58a256d5f9412fca0287df1a0cb3cec516697", + "sha256": "d974fa634290b7638e739c4ae21e75df989327748f3d4576ceb920d4fd8abd54", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-44c6a51d2f6a1907/android-x64-libwebcrypto.so.gz", + "downloadSha256": "c041ec0311bdd7f4f89d0ad87edc17d1195ea8ad4520ca90691e8b308eed76f5", "delivery": "bundle", "asset": "webcrypto.dart" } @@ -56,7 +56,7 @@ { "name": "libwebcrypto.dylib", "sha256": "0a7466fc185fcf577ed8c0e6da70f9ad33f596d0b28a487459e87db76a4e49d6", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-fe3ce5c624211592/ios-arm64-iphoneos-libwebcrypto.dylib.gz", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-44c6a51d2f6a1907/ios-arm64-iphoneos-libwebcrypto.dylib.gz", "downloadSha256": "9ea467950695bd526292a2b815236f3fd14cb8b0e7c00bdde0dbcb04e2770690", "delivery": "bundle", "asset": "webcrypto.dart" @@ -71,7 +71,7 @@ { "name": "libwebcrypto.dylib", "sha256": "3393b602567febb40fdc5b9b54943b8325aee02656d6fcb93bfce80fd3395861", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-fe3ce5c624211592/ios-arm64-iphonesimulator-libwebcrypto.dylib.gz", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-44c6a51d2f6a1907/ios-arm64-iphonesimulator-libwebcrypto.dylib.gz", "downloadSha256": "8cc335c7277fc3f58a203ac899b5111c99d79ea49ba89f8715eca8ba7f00186f", "delivery": "bundle", "asset": "webcrypto.dart" @@ -86,7 +86,7 @@ { "name": "libwebcrypto.dylib", "sha256": "084ca81cb6e9d67a2680db33209f4e7d0267917e1ea4e3851405b4099d094845", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-fe3ce5c624211592/ios-x64-iphonesimulator-libwebcrypto.dylib.gz", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-44c6a51d2f6a1907/ios-x64-iphonesimulator-libwebcrypto.dylib.gz", "downloadSha256": "654b113782a02e7991c2113bb8fb5d1fc69926558e1f73ad9edcd7f236419e54", "delivery": "bundle", "asset": "webcrypto.dart" @@ -100,7 +100,7 @@ { "name": "libwebcrypto.so", "sha256": "b314cac1cd104679023513dd43d6e3a2a7c654ee1af84006b9ab4df81324ab9d", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-fe3ce5c624211592/linux-arm64-libwebcrypto.so.gz", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-44c6a51d2f6a1907/linux-arm64-libwebcrypto.so.gz", "downloadSha256": "acba41b1f7b4042d993e65030d82eff83679e67dcfb7c36a202c341cf45f1625", "delivery": "bundle", "asset": "webcrypto.dart" @@ -114,7 +114,7 @@ { "name": "libwebcrypto.so", "sha256": "b1b2eb86748511627ba6452f3ed8b70e3738cbe057bd49716aac79817532d10a", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-fe3ce5c624211592/linux-x64-libwebcrypto.so.gz", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-44c6a51d2f6a1907/linux-x64-libwebcrypto.so.gz", "downloadSha256": "0c37b4b10b86b2eed622ffc5069f037e3f59b8ec8c8fe71371001e77a71e3c57", "delivery": "bundle", "asset": "webcrypto.dart" @@ -129,7 +129,7 @@ { "name": "libwebcrypto.dylib", "sha256": "66a5a2366db930b2865132477b7de1b284926b644fff55ccfe648480c75d3f94", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-fe3ce5c624211592/macos-arm64-libwebcrypto.dylib.gz", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-44c6a51d2f6a1907/macos-arm64-libwebcrypto.dylib.gz", "downloadSha256": "ae0b8038ada762c26a0f95b62e5e6fdc8a87ab3f5adba0aa2ad9b2db276dc787", "delivery": "bundle", "asset": "webcrypto.dart" @@ -144,7 +144,7 @@ { "name": "libwebcrypto.dylib", "sha256": "8e12dd585a5ea205f387399e04a0207fbe47a1b5fc7dab7b774de76c7e4dd0c6", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-fe3ce5c624211592/macos-x64-libwebcrypto.dylib.gz", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-44c6a51d2f6a1907/macos-x64-libwebcrypto.dylib.gz", "downloadSha256": "5844d45cc9342043ee90bedd226380e8da524102be12ea2702b57cf16a79dccf", "delivery": "bundle", "asset": "webcrypto.dart" @@ -157,9 +157,9 @@ "files": [ { "name": "webcrypto.dll", - "sha256": "994878356cce9921180939fa810476f13f5ff1ac6cd4144ee8cc291edaa4630b", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-fe3ce5c624211592/windows-arm64-webcrypto.dll.gz", - "downloadSha256": "a7ab7e1e2e63e6c31fa3e092c1c1dc107b437b8bd222b7be2f3d1d39e4dfd4fa", + "sha256": "b739a4579aaf915902ca2ba632d980fbfa804bbb910358d5b13dfbb2814ffcb4", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-44c6a51d2f6a1907/windows-arm64-webcrypto.dll.gz", + "downloadSha256": "0ed3b3b2eb51f30326c4818f130e1d9abb6d79d0ca16f24724878d3f63a2f620", "delivery": "bundle", "asset": "webcrypto.dart" } @@ -171,9 +171,9 @@ "files": [ { "name": "webcrypto.dll", - "sha256": "0208a0998ca51b6476ae3645e66306e96770db1ad87cf83bcf1d85caed05f44b", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-fe3ce5c624211592/windows-x64-webcrypto.dll.gz", - "downloadSha256": "0a2a13a4f4b156a23c2ec9b203a2bc6d3496b28df64ffa22236b1f118c5f0f25", + "sha256": "fc8f569f3889faf9c0259a8395cf0edbfe9585b14223608afd48f2352a02fbcb", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-44c6a51d2f6a1907/windows-x64-webcrypto.dll.gz", + "downloadSha256": "7aa309b9f510f7ed84ee3ffec70082e217989a4802b674b42a8c6e10fbb2d4b6", "delivery": "bundle", "asset": "webcrypto.dart" } From 84c11c8798cbcb377ad473d5720b94ee88458a04 Mon Sep 17 00:00:00 2001 From: James O'Leary Date: Sat, 24 Oct 2026 12:06:47 -0700 Subject: [PATCH 24/69] Move to kiln and the shared kiln release workflow - kiln ^0.1.1 from pub.dev instead of git native_prebuilt. - .github/workflows/kiln_release.yml and kiln_check.yml call Telosnex/telosnex_ci_tools v0.1.1. tool/kiln/prepare.dart installs NASM for Windows. - doc/ leaves source_excludes.txt: kiln excludes it by default. --- .github/workflows/kiln_check.yml | 19 +++ .github/workflows/kiln_release.yml | 38 +++++ .github/workflows/native_release.yml | 214 --------------------------- hook/build.dart | 8 +- native_artifacts/source_excludes.txt | 3 +- pubspec.yaml | 5 +- tool/kiln/prepare.dart | 46 ++++++ 7 files changed, 109 insertions(+), 224 deletions(-) create mode 100644 .github/workflows/kiln_check.yml create mode 100644 .github/workflows/kiln_release.yml delete mode 100644 .github/workflows/native_release.yml create mode 100644 tool/kiln/prepare.dart diff --git a/.github/workflows/kiln_check.yml b/.github/workflows/kiln_check.yml new file mode 100644 index 000000000..d854593b1 --- /dev/null +++ b/.github/workflows/kiln_check.yml @@ -0,0 +1,19 @@ +name: Kiln check + +on: + pull_request: + branches: [telosnex_main] + paths: + - native_artifacts/prebuilt.json + - native_artifacts/source_excludes.txt + - .github/workflows/kiln_check.yml + schedule: + - cron: '37 6 * * *' + workflow_dispatch: + +permissions: + contents: read + +jobs: + kiln-check: + uses: Telosnex/telosnex_ci_tools/.github/workflows/kiln_check.yml@71385c9e6410a0b9fdfd2d2a17f0fcc03fe592ee # v0.1.1 diff --git a/.github/workflows/kiln_release.yml b/.github/workflows/kiln_release.yml new file mode 100644 index 000000000..94f1cc32e --- /dev/null +++ b/.github/workflows/kiln_release.yml @@ -0,0 +1,38 @@ +# Builds the native library for every target and publishes the files as a +# GitHub release. Telosnex/telosnex_ci_tools has the jobs; +# tool/kiln/prepare.dart installs NASM for the Windows builds. +# +# Start it with +# git push -f origin HEAD:kiln-release (release) +# git push -f origin HEAD:kiln-release-dry (build only, no upload) +# or with workflow_dispatch. On success it pushes the branch +# kiln-manifest/. Merge that branch to use the release. +name: Kiln release +run-name: Kiln release / ${{ (github.ref_name == 'kiln-release-dry' || inputs.dry_run) && 'build only' || 'publish' }} + +on: + workflow_dispatch: + inputs: + dry_run: + description: Build only, upload nothing. + type: boolean + default: false + push: + branches: [kiln-release, kiln-release-dry] + +permissions: + contents: read + +jobs: + kiln-release: + uses: Telosnex/telosnex_ci_tools/.github/workflows/kiln_release.yml@71385c9e6410a0b9fdfd2d2a17f0fcc03fe592ee # v0.1.1 + permissions: + contents: write + with: + dry-run: ${{ inputs.dry_run == true }} + # The 10 default targets, and the Intel macOS and iOS simulator. + targets: >- + ["windows-x64", "windows-arm64", "linux-x64", "linux-arm64", + "macos-arm64", "macos-x64", "ios-arm64-iphoneos", + "ios-arm64-iphonesimulator", "ios-x64-iphonesimulator", + "android-arm", "android-arm64", "android-x64"] diff --git a/.github/workflows/native_release.yml b/.github/workflows/native_release.yml deleted file mode 100644 index 06da71c7b..000000000 --- a/.github/workflows/native_release.yml +++ /dev/null @@ -1,214 +0,0 @@ -# Builds the native library for every target and publishes the files as a -# GitHub release (native_prebuilt ADR 001 D5, D6, D8). Start it with -# git push -f origin HEAD:native-release (release) -# git push -f origin HEAD:native-release-dry (build only, no upload) -# or with workflow_dispatch. On success it pushes the branch -# native-manifest/ with native_artifacts/prebuilt.json. Merge that -# branch to use the release. -name: native release -run-name: Native release / ${{ (github.ref_name == 'native-release-dry' || inputs.dry_run) && 'build only' || 'publish' }} - -on: - workflow_dispatch: - inputs: - dry_run: - description: Build only, upload nothing. - type: boolean - default: false - push: - branches: [native-release, native-release-dry] - -concurrency: - group: native-release - cancel-in-progress: false - -permissions: - contents: read - -defaults: - run: - shell: bash - -env: - DART_VERSION: 3.13.1 - NDK_VERSION: 28.2.13676358 - NASM_VERSION: 2.16.03 - NASM_SHA256: 3ee4782247bcb874378d02f7eab4e294a84d3d15f3f6ee2de2f47a46aa7226e6 - -jobs: - build: - name: ${{ matrix.target }} - runs-on: ${{ matrix.runner }} - timeout-minutes: 45 - strategy: - fail-fast: false - matrix: - include: - - { target: windows-x64, runner: windows-2022 } - - { target: windows-arm64, runner: windows-11-arm } - - { target: linux-x64, runner: ubuntu-22.04 } - - { target: linux-arm64, runner: ubuntu-22.04-arm } - - { target: macos-arm64, runner: macos-15 } - - { target: macos-x64, runner: macos-15 } - - { target: ios-arm64-iphoneos, runner: macos-15 } - - { target: ios-arm64-iphonesimulator, runner: macos-15 } - - { target: ios-x64-iphonesimulator, runner: macos-15 } - - { target: android-arm, runner: ubuntu-22.04 } - - { target: android-arm64, runner: ubuntu-22.04 } - - { target: android-x64, runner: ubuntu-22.04 } - steps: - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - with: - ref: ${{ github.sha }} - persist-credentials: false - fetch-depth: 1 - - uses: dart-lang/setup-dart@6afc89df92d6eb3834022f73cd65adc8cdfcb92d # v1.8.1 - with: - sdk: ${{ env.DART_VERSION }} - - - name: Install NASM - if: runner.os == 'Windows' - run: | - zip="$RUNNER_TEMP/nasm.zip" - curl -sSfLo "$zip" "https://www.nasm.us/pub/nasm/releasebuilds/$NASM_VERSION/win64/nasm-$NASM_VERSION-win64.zip" - echo "$NASM_SHA256 $zip" | sha256sum -c - - unzip -q "$zip" -d "$RUNNER_TEMP" - echo "$RUNNER_TEMP/nasm-$NASM_VERSION" >> "$GITHUB_PATH" - - - name: Install the NDK - if: startsWith(matrix.target, 'android-') - run: | - { yes || true; } | "$ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager" "ndk;$NDK_VERSION" > /dev/null - test -d "$ANDROID_HOME/ndk/$NDK_VERSION" - - - name: Describe the toolchain - id: toolchain - run: | - case "${{ matrix.target }}" in - android-*) t="Android NDK $NDK_VERSION" ;; - windows-*) - vs="$("/c/Program Files (x86)/Microsoft Visual Studio/Installer/vswhere.exe" -latest -products '*' -property catalog_productDisplayVersion || true)" - t="MSVC, Visual Studio ${vs:-unknown}, NASM $NASM_VERSION" ;; - macos-*|ios-*) t="$(xcodebuild -version | sed -n 1p)" ;; - linux-*) t="$(gcc --version | sed -n 1p)" ;; - esac - echo "Toolchain: $t" - echo "value=$t" >> "$GITHUB_OUTPUT" - - - run: dart pub get - - # Files go to $RUNNER_TEMP: untracked files in the checkout count in - # the source key. - - name: Set paths - run: echo "OUT=$RUNNER_TEMP/native" >> "$GITHUB_ENV" - - - &clean - name: Check that the work tree is clean - run: | - if [[ -n "$(git status --porcelain)" ]]; then - git status --porcelain - echo "::error::Untracked or changed files would change the source key." - exit 1 - fi - - - name: Source key - run: | - mkdir -p "$OUT/key" - dart run native_prebuilt:key --list > "$OUT/key/${{ matrix.target }}.txt" - tail -1 "$OUT/key/${{ matrix.target }}.txt" - - - name: Build - env: - TOOLCHAIN: ${{ steps.toolchain.outputs.value }} - run: | - defines=() - if [[ "${{ matrix.target }}" == android-* ]]; then - defines+=(--define "android={\"android_home\":\"$ANDROID_HOME\",\"ndk_version\":\"$NDK_VERSION\"}") - fi - dart run native_prebuilt:build \ - --target "${{ matrix.target }}" \ - --out "$OUT/out/${{ matrix.target }}" \ - --runner "${{ matrix.runner }}" \ - --toolchain "$TOOLCHAIN" \ - "${defines[@]}" - cat "$OUT/out/${{ matrix.target }}/target.json" - - - *clean - - - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 - with: - name: native-${{ matrix.target }} - path: ${{ runner.temp }}/native/ - if-no-files-found: error - retention-days: 90 - - release: - needs: build - # Only the owner publishes, from this repository. - if: >- - github.repository == 'Telosnex/webcrypto.dart' && - github.actor == 'jpohhhh' && github.triggering_actor == 'jpohhhh' - runs-on: ubuntu-22.04 - timeout-minutes: 20 - permissions: - contents: write - env: - DRY_RUN: ${{ github.ref_name == 'native-release-dry' || inputs.dry_run == true }} - steps: - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - with: - ref: ${{ github.sha }} - fetch-depth: 1 - - uses: dart-lang/setup-dart@6afc89df92d6eb3834022f73cd65adc8cdfcb92d # v1.8.1 - with: - sdk: ${{ env.DART_VERSION }} - - run: dart pub get - - run: echo "ARTIFACTS=$RUNNER_TEMP/artifacts" >> "$GITHUB_ENV" - - uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1 - with: - pattern: native-* - path: ${{ runner.temp }}/artifacts - merge-multiple: true - - - name: Compare the source keys of all runners (I2) - run: | - dart run native_prebuilt:key --list > "$RUNNER_TEMP/local-key.txt" - status=0 - for f in "$ARTIFACTS"/key/*.txt; do - if ! diff -u "$RUNNER_TEMP/local-key.txt" "$f"; then - echo "::error::The source key on $(basename "$f" .txt) differs." - status=1 - fi - done - exit $status - - - name: Release - env: - GH_TOKEN: ${{ github.token }} - run: | - args=(--staging "$RUNNER_TEMP/staging") - if [[ "$DRY_RUN" == true ]]; then args+=(--dry-run); fi - dart run native_prebuilt:release "${args[@]}" "$ARTIFACTS/out" - cat native_artifacts/prebuilt.json - - - name: Push the manifest branch - if: env.DRY_RUN != 'true' - run: | - tag="native-$(dart run native_prebuilt:key | tail -1 | cut -c1-16)" - branch="native-manifest/$tag" - git config user.name "github-actions[bot]" - git config user.email "41898282+github-actions[bot]@users.noreply.github.com" - git switch -c "$branch" - git add native_artifacts/prebuilt.json - git commit -m "native: prebuilt files of release $tag" - git push origin "$branch" - { - echo "Release: https://github.com/$GITHUB_REPOSITORY/releases/tag/$tag" - echo - echo "Merge branch \`$branch\` to use it." - } >> "$GITHUB_STEP_SUMMARY" - - - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 - with: - name: prebuilt-manifest - path: native_artifacts/prebuilt.json diff --git a/hook/build.dart b/hook/build.dart index eee7d2fe2..a5bb87dad 100644 --- a/hook/build.dart +++ b/hook/build.dart @@ -10,7 +10,7 @@ /// Asset on every supported native target. /// /// Web builds do not request code assets, so this hook intentionally does -/// nothing for them. With package:native_prebuilt, a native build downloads +/// nothing for them. With package:kiln, a native build downloads /// the library from a release when native_artifacts/prebuilt.json matches /// the sources (user define `native_build`: auto, download or source). /// Otherwise it builds from source into a content-addressed, process-locked @@ -25,7 +25,7 @@ import 'package:code_assets/code_assets.dart'; import 'package:crypto/crypto.dart'; import 'package:hooks/hooks.dart'; import 'package:logging/logging.dart'; -import 'package:native_prebuilt/native_prebuilt.dart'; +import 'package:kiln/kiln.dart'; import 'package:native_toolchain_cmake/native_toolchain_cmake.dart'; import 'package:path/path.dart' as p; @@ -45,7 +45,7 @@ void main(List args) async { ..level = Level.ALL ..onRecord.listen((record) => hookLog.add(record.message)); - await NativePrebuilt(input: input, output: output, log: logger.info).run(( + await Kiln(input: input, output: output, log: logger.info).run(( source, ) async { await _buildFromSource( @@ -278,7 +278,7 @@ String _libraryFileName(OS os) => switch (os) { _ => throw UnsupportedError('package:webcrypto does not support ${os.name}.'), }; -/// The key of the local build cache (native_prebuilt ADR 001 D4): the source +/// The key of the local build cache (kiln ADR 001 D4): the source /// key of the package and the host toolchain. Future _computeBuildKey({ required CodeConfig code, diff --git a/native_artifacts/source_excludes.txt b/native_artifacts/source_excludes.txt index fe8a5ff33..dd4dd3549 100644 --- a/native_artifacts/source_excludes.txt +++ b/native_artifacts/source_excludes.txt @@ -1,4 +1,3 @@ -# Files that the native build does not read (native_prebuilt ADR 001 D3). -doc/ +# Files that the native build does not read (kiln ADR 001 D3). tool/ analysis_options.yaml diff --git a/pubspec.yaml b/pubspec.yaml index d61c7d353..1fd5c80d1 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -25,10 +25,7 @@ dependencies: ffi: ^2.0.0 hooks: ">=1.0.0 <3.0.0" logging: ^1.3.0 - native_prebuilt: - git: - url: https://github.com/Telosnex/native_prebuilt.git - ref: dfe2a5bfeb95426e87f6243cab14f4affd879b38 + kiln: ^0.1.1 native_toolchain_cmake: ^0.2.7 path: ^1.9.0 code_assets: ^1.0.0 diff --git a/tool/kiln/prepare.dart b/tool/kiln/prepare.dart new file mode 100644 index 000000000..2632f7032 --- /dev/null +++ b/tool/kiln/prepare.dart @@ -0,0 +1,46 @@ +/// Runs before each native build of the kiln release workflow. BoringSSL +/// needs NASM for its x86 assembly on Windows; the runner images have none. +/// This installs the pinned NASM and adds it to the toolchain description. +library; + +import 'dart:io'; + +const _version = '2.16.03'; +const _sha256 = + '3ee4782247bcb874378d02f7eab4e294a84d3d15f3f6ee2de2f47a46aa7226e6'; + +Future main() async { + final target = Platform.environment['TARGET'] ?? ''; + if (!target.startsWith('windows-')) return; + + final temp = Platform.environment['RUNNER_TEMP']!; + final zip = '$temp/nasm.zip'; + await _run('curl', [ + '-sSfLo', + zip, + 'https://www.nasm.us/pub/nasm/releasebuilds/$_version/win64/' + 'nasm-$_version-win64.zip', + ]); + final sum = await Process.run('sha256sum', [zip]); + if ('${sum.stdout}'.split(' ').first != _sha256) { + stderr.writeln('NASM: SHA-256 mismatch: ${sum.stdout}'); + exit(1); + } + await _run('unzip', ['-q', zip, '-d', temp]); + + File( + Platform.environment['GITHUB_PATH']!, + ).writeAsStringSync('$temp/nasm-$_version\n', mode: FileMode.append); + File(Platform.environment['GITHUB_ENV']!).writeAsStringSync( + 'KILN_TOOLCHAIN_EXTRA=NASM $_version\n', + mode: FileMode.append, + ); +} + +Future _run(String executable, List args) async { + final result = await Process.run(executable, args); + if (result.exitCode != 0) { + stderr.writeln('$executable failed:\n${result.stderr}'); + exit(result.exitCode); + } +} From 86a5fb3d92f2841ee8fad9a9668803c81a5316e6 Mon Sep 17 00:00:00 2001 From: James O'Leary Date: Sat, 24 Oct 2026 12:22:15 -0700 Subject: [PATCH 25/69] telosnex_ci_tools v0.1.2: prepare.dart runs outside the package --- .github/workflows/kiln_check.yml | 2 +- .github/workflows/kiln_release.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/kiln_check.yml b/.github/workflows/kiln_check.yml index d854593b1..0f8b3b0ef 100644 --- a/.github/workflows/kiln_check.yml +++ b/.github/workflows/kiln_check.yml @@ -16,4 +16,4 @@ permissions: jobs: kiln-check: - uses: Telosnex/telosnex_ci_tools/.github/workflows/kiln_check.yml@71385c9e6410a0b9fdfd2d2a17f0fcc03fe592ee # v0.1.1 + uses: Telosnex/telosnex_ci_tools/.github/workflows/kiln_check.yml@0e3d782cce29ab6f9fdb2fa6d796bcb365882407 # v0.1.2 diff --git a/.github/workflows/kiln_release.yml b/.github/workflows/kiln_release.yml index 94f1cc32e..f6450ddc6 100644 --- a/.github/workflows/kiln_release.yml +++ b/.github/workflows/kiln_release.yml @@ -25,7 +25,7 @@ permissions: jobs: kiln-release: - uses: Telosnex/telosnex_ci_tools/.github/workflows/kiln_release.yml@71385c9e6410a0b9fdfd2d2a17f0fcc03fe592ee # v0.1.1 + uses: Telosnex/telosnex_ci_tools/.github/workflows/kiln_release.yml@0e3d782cce29ab6f9fdb2fa6d796bcb365882407 # v0.1.2 permissions: contents: write with: From 4108237f116d3bb98fe3c4a716a43ac248b58ea0 Mon Sep 17 00:00:00 2001 From: James O'Leary Date: Sat, 24 Oct 2026 12:25:13 -0700 Subject: [PATCH 26/69] prepare.dart: read the hash from the Windows sha256sum output --- tool/kiln/prepare.dart | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tool/kiln/prepare.dart b/tool/kiln/prepare.dart index 2632f7032..24008b6e6 100644 --- a/tool/kiln/prepare.dart +++ b/tool/kiln/prepare.dart @@ -21,8 +21,10 @@ Future main() async { 'https://www.nasm.us/pub/nasm/releasebuilds/$_version/win64/' 'nasm-$_version-win64.zip', ]); + // On Windows, sha256sum puts a backslash before the hash of a path with + // backslashes. final sum = await Process.run('sha256sum', [zip]); - if ('${sum.stdout}'.split(' ').first != _sha256) { + if (RegExp('[0-9a-f]{64}').firstMatch('${sum.stdout}')?[0] != _sha256) { stderr.writeln('NASM: SHA-256 mismatch: ${sum.stdout}'); exit(1); } From e8c0ed3dd40b3925528a02090850d5bf91c3370d Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Sat, 24 Oct 2026 19:38:29 +0000 Subject: [PATCH 27/69] kiln: manifest of release native-f946ab06ef46444f --- native_artifacts/prebuilt.json | 96 +++++++++++++++++----------------- 1 file changed, 48 insertions(+), 48 deletions(-) diff --git a/native_artifacts/prebuilt.json b/native_artifacts/prebuilt.json index 1c238ce10..838d32889 100644 --- a/native_artifacts/prebuilt.json +++ b/native_artifacts/prebuilt.json @@ -1,62 +1,62 @@ { "schema": 1, - "sourceKey": "44c6a51d2f6a1907b35dc92b88b780cb14eaf3ab9a30b261e0e5d8815056adc1", - "release": "https://github.com/Telosnex/webcrypto.dart/releases/tag/native-44c6a51d2f6a1907", + "sourceKey": "f946ab06ef46444f2b2e106273335c4caa32e3c5ef300a3943b25e55e8962536", + "release": "https://github.com/Telosnex/webcrypto.dart/releases/tag/native-f946ab06ef46444f", "targets": { "android-arm": { - "runner": "ubuntu-22.04", - "toolchain": "Android NDK 28.2.13676358", + "runner": "ubuntu22 20261012.322.1", + "toolchain": "Android NDK 28.2.13676358, cmake version 3.31.6", "minOSVersion": 24, "files": [ { "name": "libwebcrypto.so", - "sha256": "bed1b3b78342772a7b5922e149ff9397f1c6f9da9e56f0374d92868e94752fa8", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-44c6a51d2f6a1907/android-arm-libwebcrypto.so.gz", - "downloadSha256": "6aaf7e5db566a46712f36fcdd8dc4b2e6cbba55fa3339dea42df2ed4ebe5b768", + "sha256": "ca311dda5aaac1e313861d37cfeba6437deebf0796530e3379480996587d79e5", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-f946ab06ef46444f/android-arm-libwebcrypto.so.gz", + "downloadSha256": "b97a7edbe31893adef3a0d721cd36baa3c258ea270dd594a7f786faa762c404c", "delivery": "bundle", "asset": "webcrypto.dart" } ] }, "android-arm64": { - "runner": "ubuntu-22.04", - "toolchain": "Android NDK 28.2.13676358", + "runner": "ubuntu22 20261012.322.1", + "toolchain": "Android NDK 28.2.13676358, cmake version 3.31.6", "minOSVersion": 24, "files": [ { "name": "libwebcrypto.so", - "sha256": "6fac3d7eeb8feadc7d48b4fae54271359f71ee0b80f931f1cb1fd31c5d622e69", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-44c6a51d2f6a1907/android-arm64-libwebcrypto.so.gz", - "downloadSha256": "65b7b72c0daa5718e100080331a541808c98e35541c17395c00d75289b9f4e51", + "sha256": "68dbb5a39f8abb6612276489030883193a7f5f988de9d4556c93458fdc69f78f", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-f946ab06ef46444f/android-arm64-libwebcrypto.so.gz", + "downloadSha256": "2f72d84faf5b23af07bc795f6fa41f3e215d6fd2fe5dea61c574b48a1b1507fe", "delivery": "bundle", "asset": "webcrypto.dart" } ] }, "android-x64": { - "runner": "ubuntu-22.04", - "toolchain": "Android NDK 28.2.13676358", + "runner": "ubuntu22 20261012.322.1", + "toolchain": "Android NDK 28.2.13676358, cmake version 3.31.6", "minOSVersion": 24, "files": [ { "name": "libwebcrypto.so", - "sha256": "d974fa634290b7638e739c4ae21e75df989327748f3d4576ceb920d4fd8abd54", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-44c6a51d2f6a1907/android-x64-libwebcrypto.so.gz", - "downloadSha256": "c041ec0311bdd7f4f89d0ad87edc17d1195ea8ad4520ca90691e8b308eed76f5", + "sha256": "a90b4d6593f2cc7b2b84a9955f10c0dec90a9d787c0032b4ffd5bbbb2ff09f60", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-f946ab06ef46444f/android-x64-libwebcrypto.so.gz", + "downloadSha256": "35b3f4d30ce02d107d31b067dc156d04b11a23e83c9355a41b623edfe8677c1c", "delivery": "bundle", "asset": "webcrypto.dart" } ] }, "ios-arm64-iphoneos": { - "runner": "macos-15", - "toolchain": "Xcode 16.4", + "runner": "macos15 20260907.0337.1", + "toolchain": "Xcode 16.4, cmake version 4.4.3", "minOSVersion": 13, "files": [ { "name": "libwebcrypto.dylib", "sha256": "0a7466fc185fcf577ed8c0e6da70f9ad33f596d0b28a487459e87db76a4e49d6", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-44c6a51d2f6a1907/ios-arm64-iphoneos-libwebcrypto.dylib.gz", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-f946ab06ef46444f/ios-arm64-iphoneos-libwebcrypto.dylib.gz", "downloadSha256": "9ea467950695bd526292a2b815236f3fd14cb8b0e7c00bdde0dbcb04e2770690", "delivery": "bundle", "asset": "webcrypto.dart" @@ -64,14 +64,14 @@ ] }, "ios-arm64-iphonesimulator": { - "runner": "macos-15", - "toolchain": "Xcode 16.4", + "runner": "macos15 20260907.0337.1", + "toolchain": "Xcode 16.4, cmake version 4.4.3", "minOSVersion": 13, "files": [ { "name": "libwebcrypto.dylib", "sha256": "3393b602567febb40fdc5b9b54943b8325aee02656d6fcb93bfce80fd3395861", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-44c6a51d2f6a1907/ios-arm64-iphonesimulator-libwebcrypto.dylib.gz", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-f946ab06ef46444f/ios-arm64-iphonesimulator-libwebcrypto.dylib.gz", "downloadSha256": "8cc335c7277fc3f58a203ac899b5111c99d79ea49ba89f8715eca8ba7f00186f", "delivery": "bundle", "asset": "webcrypto.dart" @@ -79,14 +79,14 @@ ] }, "ios-x64-iphonesimulator": { - "runner": "macos-15", - "toolchain": "Xcode 16.4", + "runner": "macos15 20260907.0337.1", + "toolchain": "Xcode 16.4, cmake version 4.4.3", "minOSVersion": 13, "files": [ { "name": "libwebcrypto.dylib", "sha256": "084ca81cb6e9d67a2680db33209f4e7d0267917e1ea4e3851405b4099d094845", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-44c6a51d2f6a1907/ios-x64-iphonesimulator-libwebcrypto.dylib.gz", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-f946ab06ef46444f/ios-x64-iphonesimulator-libwebcrypto.dylib.gz", "downloadSha256": "654b113782a02e7991c2113bb8fb5d1fc69926558e1f73ad9edcd7f236419e54", "delivery": "bundle", "asset": "webcrypto.dart" @@ -94,13 +94,13 @@ ] }, "linux-arm64": { - "runner": "ubuntu-22.04-arm", - "toolchain": "gcc (Ubuntu 11.4.0-1ubuntu1~22.04.3) 11.4.0", + "runner": "ubuntu22-arm64 20261023.156.3", + "toolchain": "gcc (Ubuntu 11.4.0-1ubuntu1~22.04.3) 11.4.0, cmake version 3.31.6", "files": [ { "name": "libwebcrypto.so", "sha256": "b314cac1cd104679023513dd43d6e3a2a7c654ee1af84006b9ab4df81324ab9d", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-44c6a51d2f6a1907/linux-arm64-libwebcrypto.so.gz", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-f946ab06ef46444f/linux-arm64-libwebcrypto.so.gz", "downloadSha256": "acba41b1f7b4042d993e65030d82eff83679e67dcfb7c36a202c341cf45f1625", "delivery": "bundle", "asset": "webcrypto.dart" @@ -108,13 +108,13 @@ ] }, "linux-x64": { - "runner": "ubuntu-22.04", - "toolchain": "gcc (Ubuntu 11.4.0-1ubuntu1~22.04.3) 11.4.0", + "runner": "ubuntu22 20261023.322.3", + "toolchain": "gcc (Ubuntu 11.4.0-1ubuntu1~22.04.3) 11.4.0, cmake version 3.31.6", "files": [ { "name": "libwebcrypto.so", "sha256": "b1b2eb86748511627ba6452f3ed8b70e3738cbe057bd49716aac79817532d10a", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-44c6a51d2f6a1907/linux-x64-libwebcrypto.so.gz", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-f946ab06ef46444f/linux-x64-libwebcrypto.so.gz", "downloadSha256": "0c37b4b10b86b2eed622ffc5069f037e3f59b8ec8c8fe71371001e77a71e3c57", "delivery": "bundle", "asset": "webcrypto.dart" @@ -122,14 +122,14 @@ ] }, "macos-arm64": { - "runner": "macos-15", - "toolchain": "Xcode 16.4", + "runner": "macos15 20260907.0337.1", + "toolchain": "Xcode 16.4, cmake version 4.4.3", "minOSVersion": 12, "files": [ { "name": "libwebcrypto.dylib", "sha256": "66a5a2366db930b2865132477b7de1b284926b644fff55ccfe648480c75d3f94", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-44c6a51d2f6a1907/macos-arm64-libwebcrypto.dylib.gz", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-f946ab06ef46444f/macos-arm64-libwebcrypto.dylib.gz", "downloadSha256": "ae0b8038ada762c26a0f95b62e5e6fdc8a87ab3f5adba0aa2ad9b2db276dc787", "delivery": "bundle", "asset": "webcrypto.dart" @@ -137,14 +137,14 @@ ] }, "macos-x64": { - "runner": "macos-15", - "toolchain": "Xcode 16.4", + "runner": "macos15 20260907.0337.1", + "toolchain": "Xcode 16.4, cmake version 4.4.3", "minOSVersion": 12, "files": [ { "name": "libwebcrypto.dylib", "sha256": "8e12dd585a5ea205f387399e04a0207fbe47a1b5fc7dab7b774de76c7e4dd0c6", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-44c6a51d2f6a1907/macos-x64-libwebcrypto.dylib.gz", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-f946ab06ef46444f/macos-x64-libwebcrypto.dylib.gz", "downloadSha256": "5844d45cc9342043ee90bedd226380e8da524102be12ea2702b57cf16a79dccf", "delivery": "bundle", "asset": "webcrypto.dart" @@ -152,28 +152,28 @@ ] }, "windows-arm64": { - "runner": "windows-11-arm", - "toolchain": "MSVC, Visual Studio 18.10.3, NASM 2.16.03", + "runner": "win11-vs2026-arm64 20261022.194.1", + "toolchain": "Visual Studio 18.11.1, cmake version 4.4.4, NASM 2.16.03", "files": [ { "name": "webcrypto.dll", - "sha256": "b739a4579aaf915902ca2ba632d980fbfa804bbb910358d5b13dfbb2814ffcb4", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-44c6a51d2f6a1907/windows-arm64-webcrypto.dll.gz", - "downloadSha256": "0ed3b3b2eb51f30326c4818f130e1d9abb6d79d0ca16f24724878d3f63a2f620", + "sha256": "93cf9310215254f8d64a414190f4008922ceb9875568b549155f7f5ce49772e3", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-f946ab06ef46444f/windows-arm64-webcrypto.dll.gz", + "downloadSha256": "2c3a51dc16804297196ce461d41e729901075283239ac34be5ca4be699ac0112", "delivery": "bundle", "asset": "webcrypto.dart" } ] }, "windows-x64": { - "runner": "windows-2022", - "toolchain": "MSVC, Visual Studio 17.14.41 (September 2026), NASM 2.16.03", + "runner": "win22 20261022.344.1", + "toolchain": "Visual Studio 17.14.42, cmake version 3.31.6, NASM 2.16.03", "files": [ { "name": "webcrypto.dll", - "sha256": "fc8f569f3889faf9c0259a8395cf0edbfe9585b14223608afd48f2352a02fbcb", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-44c6a51d2f6a1907/windows-x64-webcrypto.dll.gz", - "downloadSha256": "7aa309b9f510f7ed84ee3ffec70082e217989a4802b674b42a8c6e10fbb2d4b6", + "sha256": "26a1728105e7a44236da83f57eb6186767e4152716a3f7398db9417cfdc4ae16", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-f946ab06ef46444f/windows-x64-webcrypto.dll.gz", + "downloadSha256": "e268fcce5b0f6ad0e96cca0fb18cd919a52cee27c6d11031ecb156fd5ec28f17", "delivery": "bundle", "asset": "webcrypto.dart" } From f87921d5ce12c383072fcf20a4d661b5ce9cd55b Mon Sep 17 00:00:00 2001 From: James O'Leary Date: Sat, 24 Oct 2026 12:45:42 -0700 Subject: [PATCH 28/69] Renovate: extend the Telosnex preset --- renovate.json | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 renovate.json diff --git a/renovate.json b/renovate.json new file mode 100644 index 000000000..91c765d82 --- /dev/null +++ b/renovate.json @@ -0,0 +1,4 @@ +{ + "$schema": "https://docs.renovatebot.com/renovate-schema.json", + "extends": ["github>Telosnex/telosnex_ci_tools"] +} From f3335ecf4be385c333fbc129f861061bf3a73235 Mon Sep 17 00:00:00 2001 From: James O'Leary Date: Sat, 24 Oct 2026 12:49:32 -0700 Subject: [PATCH 29/69] kiln_check: the main branch is main --- .github/workflows/kiln_check.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/kiln_check.yml b/.github/workflows/kiln_check.yml index 0f8b3b0ef..b45a06ff7 100644 --- a/.github/workflows/kiln_check.yml +++ b/.github/workflows/kiln_check.yml @@ -2,7 +2,7 @@ name: Kiln check on: pull_request: - branches: [telosnex_main] + branches: [main] paths: - native_artifacts/prebuilt.json - native_artifacts/source_excludes.txt From a9a7d81946fcc4d3ecd90bd680083ecbbbb29360 Mon Sep 17 00:00:00 2001 From: James O'Leary Date: Sat, 24 Oct 2026 12:51:50 -0700 Subject: [PATCH 30/69] Renovate: config in .github/, outside the kiln source key --- renovate.json => .github/renovate.json | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename renovate.json => .github/renovate.json (100%) diff --git a/renovate.json b/.github/renovate.json similarity index 100% rename from renovate.json rename to .github/renovate.json From c7beaa6f0b622b07048cd3713e6a0a8ec8e1a6da Mon Sep 17 00:00:00 2001 From: James O'Leary Date: Mon, 26 Oct 2026 08:32:15 -0700 Subject: [PATCH 31/69] CI: test.yml (upstream's, unpinned actions) replaced by unit.yml (Unit: format, analyze, kiln check, VM tests with Valgrind) and integration.yml (Integration: VM tests and the demo app on Linux/macOS/Windows x64 and ARM64, Chrome/Firefox dart2js/dart2wasm, Safari, iOS simulator, Android emulator; telosnex_ci_tools actions) --- .github/workflows/integration.yml | 122 ++++++++++++++++++++ .github/workflows/test.yml | 177 ------------------------------ .github/workflows/unit.yml | 37 +++++++ 3 files changed, 159 insertions(+), 177 deletions(-) create mode 100644 .github/workflows/integration.yml delete mode 100644 .github/workflows/test.yml create mode 100644 .github/workflows/unit.yml diff --git a/.github/workflows/integration.yml b/.github/workflows/integration.yml new file mode 100644 index 000000000..e5333735c --- /dev/null +++ b/.github/workflows/integration.yml @@ -0,0 +1,122 @@ +name: Integration + +on: + push: + branches: [main, 'ci/**'] + schedule: + - cron: '29 7 * * *' + workflow_dispatch: + +permissions: + contents: read + +concurrency: + group: integration-${{ github.ref }} + cancel-in-progress: true + +defaults: + run: + shell: bash + +jobs: + # The VM tests against the released BoringSSL library of each platform, + # then the demo app's integration test (Flutter desktop). + native: + name: ${{ matrix.name }} + runs-on: ${{ matrix.runner }} + timeout-minutes: 40 + strategy: + fail-fast: false + matrix: + include: + - { name: Linux x64, runner: ubuntu-24.04, device: linux } + - { name: Linux ARM64, runner: ubuntu-24.04-arm, device: linux } + - { name: macOS ARM64, runner: macos-26, device: macos } + - { name: macOS x64, runner: macos-15-intel, device: macos } + - { name: Windows x64, runner: windows-2025, device: windows } + - { name: Windows ARM64, runner: windows-11-arm, device: windows } + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + - if: runner.os == 'Linux' + run: | + sudo apt-get update -qq + sudo apt-get install -y -qq clang cmake ninja-build pkg-config libgtk-3-dev xvfb > /dev/null + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@a9a1bb441373e9ff3801fe91acf7efc80954cbe6 # main + - run: flutter pub get + - run: dart test --exclude-tags browser-interop -p vm -r expanded + - name: Demo app integration test + working-directory: example/webcrypto_demo_flutter_app + env: + DEVICE: ${{ matrix.device }} + run: | + flutter pub get + run=(flutter test integration_test/webcrypto_test.dart -d "$DEVICE" --timeout 5m -r expanded) + if [[ "$RUNNER_OS" == Linux ]]; then run=(xvfb-run -a "${run[@]}"); fi + "${run[@]}" + + web: + name: ${{ matrix.name }} + runs-on: ${{ matrix.runner }} + timeout-minutes: 30 + strategy: + fail-fast: false + matrix: + include: + - { name: Web Linux x64 Chrome dart2js, runner: ubuntu-24.04, platform: chrome, compiler: dart2js } + - { name: Web Linux x64 Chrome dart2wasm, runner: ubuntu-24.04, platform: chrome, compiler: dart2wasm } + - { name: Web Linux x64 Firefox dart2js, runner: ubuntu-24.04, platform: firefox, compiler: dart2js } + - { name: Web Linux x64 Firefox dart2wasm, runner: ubuntu-24.04, platform: firefox, compiler: dart2wasm } + - { name: Web macOS ARM64 Safari dart2js, runner: macos-26, platform: safari, compiler: dart2js } + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@a9a1bb441373e9ff3801fe91acf7efc80954cbe6 # main + - run: flutter pub get + - if: matrix.platform == 'safari' + uses: Telosnex/telosnex_ci_tools/actions/dart-test-safari@a9a1bb441373e9ff3801fe91acf7efc80954cbe6 # main + - name: dart test -p ${{ matrix.platform }} -c ${{ matrix.compiler }} + env: + PLATFORM: ${{ matrix.platform }} + COMPILER: ${{ matrix.compiler }} + run: dart test --exclude-tags browser-interop -p "$PLATFORM" -c "$COMPILER" -r expanded + + ios: + name: iOS ARM64 simulator + runs-on: macos-26 + timeout-minutes: 45 + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@a9a1bb441373e9ff3801fe91acf7efc80954cbe6 # main + - run: flutter pub get + - uses: Telosnex/telosnex_ci_tools/actions/ios-flutter-test@a9a1bb441373e9ff3801fe91acf7efc80954cbe6 # main + with: + working-directory: example/webcrypto_demo_flutter_app + target: integration_test/webcrypto_test.dart + args: --timeout 5m + + android: + name: Android x64 emulator + runs-on: ubuntu-24.04 + timeout-minutes: 45 + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@a9a1bb441373e9ff3801fe91acf7efc80954cbe6 # main + - run: flutter pub get + - uses: Telosnex/telosnex_ci_tools/actions/android-emulator@a9a1bb441373e9ff3801fe91acf7efc80954cbe6 # main + id: emulator + with: + api-level: '36' + - name: Demo app integration test + working-directory: example/webcrypto_demo_flutter_app + env: + SERIAL: ${{ steps.emulator.outputs.serial }} + run: | + flutter pub get + flutter test integration_test/webcrypto_test.dart -d "$SERIAL" --timeout 5m -r expanded diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml deleted file mode 100644 index 4b7fa2cec..000000000 --- a/.github/workflows/test.yml +++ /dev/null @@ -1,177 +0,0 @@ -name: test -on: - push: - branches: [master] - pull_request: - branches: [master] - -concurrency: - group: ${{ github.workflow }}-${{ github.ref }} - cancel-in-progress: true - -env: - PUB_ENVIRONMENT: bot.github - -jobs: - lints: - name: Format + Lints - runs-on: ubuntu-latest - timeout-minutes: 15 - steps: - - uses: actions/checkout@v4 - - uses: subosito/flutter-action@v2 - with: - channel: 'stable' - cache: true - - run: flutter pub get - - run: dart format --output none --set-exit-if-changed . - - run: dart analyze --fatal-warnings . - linux: - name: Linux desktop / Chrome / Firefox - runs-on: ubuntu-latest - timeout-minutes: 15 - steps: - - uses: actions/checkout@v4 - - uses: subosito/flutter-action@v2 - with: - channel: 'stable' - cache: true - - name: Configure Flutter - run: | - sudo apt-get update -y - sudo apt-get install -y ninja-build libgtk-3-dev - flutter config --no-analytics - - run: flutter pub get - - run: xvfb-run dart test --exclude-tags browser-interop -p vm,chrome,firefox -c dart2js,dart2wasm --coverage ./coverage - - run: xvfb-run flutter test integration_test/webcrypto_test.dart -d linux - working-directory: ./example/webcrypto_demo_flutter_app - # Report collected coverage - - name: Convert coverage to lcov - run: dart run coverage:format_coverage -i ./coverage -o ./coverage/lcov.info --lcov --report-on lib/ - - uses: coverallsapp/github-action@v2 - linux-dart: - name: Linux Dart / Chrome / Firefox - runs-on: ubuntu-latest - timeout-minutes: 15 - steps: - - uses: actions/checkout@v4 - - uses: dart-lang/setup-dart@v1 - - name: Install Valgrind - run: | - sudo apt-get update -y - sudo apt-get install -y valgrind - - run: dart pub get --no-example - - run: xvfb-run dart test --exclude-tags browser-interop -p vm,chrome,firefox -c dart2js,dart2wasm - macos: - name: MacOS desktop / Chrome - runs-on: macos-15 # Test with xcode 16 - timeout-minutes: 15 - steps: - - uses: actions/checkout@v4 - - uses: subosito/flutter-action@v2 - with: - channel: 'stable' - cache: true - - name: Configure Flutter - run: | - flutter config --no-analytics - - run: flutter pub get - - run: dart test --exclude-tags browser-interop -p vm,chrome -c dart2js,dart2wasm - - run: flutter test integration_test/webcrypto_test.dart -d macos --timeout 5m - working-directory: ./example/webcrypto_demo_flutter_app - windows: - name: Windows desktop / Chrome / Firefox - runs-on: windows-latest - timeout-minutes: 15 - steps: - - uses: ilammy/setup-nasm@v1 - - uses: actions/checkout@v4 - - uses: subosito/flutter-action@v2 - with: - channel: 'stable' - cache: true - - name: Configure Flutter - run: | - flutter config --no-analytics - - run: flutter pub get - - run: dart test --exclude-tags browser-interop -p vm,chrome,firefox - - run: flutter test integration_test/webcrypto_test.dart -d windows - working-directory: ./example/webcrypto_demo_flutter_app - ios: - name: iOS emulator (iPhone) - runs-on: macos-14 - timeout-minutes: 45 - steps: - - uses: actions/checkout@v4 - - uses: subosito/flutter-action@v2 - with: - channel: 'stable' - cache: true - - name: Configure Flutter - run: | - flutter config --no-analytics - - uses: futureware-tech/simulator-action@v3 - with: - model: 'iPhone 15' - - run: flutter pub get - - name: Run integration tests - uses: nick-fields/retry@v3 - with: - timeout_minutes: 20 - max_attempts: 2 - command: cd example/webcrypto_demo_flutter_app && flutter test integration_test/webcrypto_test.dart -d iphone --timeout 60s - android: - name: Android emulator - runs-on: ubuntu-latest - timeout-minutes: 15 - steps: - - uses: actions/checkout@v4 - - uses: subosito/flutter-action@v2 - with: - channel: 'stable' - cache: true - - uses: actions/setup-java@v4 - with: - distribution: 'zulu' - java-version: 21 - - name: Configure Flutter - run: | - flutter config --no-analytics - - run: flutter pub get - - name: Enable KVM - run: | - echo 'KERNEL=="kvm", GROUP="kvm", MODE="0666", OPTIONS+="static_node=kvm"' | sudo tee /etc/udev/rules.d/99-kvm4all.rules - sudo udevadm control --reload-rules - sudo udevadm trigger --name-match=kvm - - name: AVD cache - uses: actions/cache@v4 - id: avd-cache - with: - path: | - ~/.android/avd/* - ~/.android/adb* - key: avd-ubuntu-api36-google_apis-x86_64 - - name: Create AVD snapshot for caching - if: steps.avd-cache.outputs.cache-hit != 'true' - uses: reactivecircus/android-emulator-runner@v2 - with: - api-level: 36 - target: google_apis - arch: x86_64 - force-avd-creation: false - emulator-options: -no-window -gpu swiftshader_indirect -noaudio -no-boot-anim - working-directory: ./example/webcrypto_demo_flutter_app - script: echo "Generated AVD snapshot for caching." - - name: Run flutter test integration_test/webcrypto_test.dart -d emulator - uses: reactivecircus/android-emulator-runner@v2 - with: - api-level: 36 - target: google_apis - arch: x86_64 - force-avd-creation: false - emulator-options: -no-snapshot-save -no-window -gpu swiftshader_indirect -noaudio -no-boot-anim - working-directory: ./example/webcrypto_demo_flutter_app - script: >- - flutter test integration_test/webcrypto_test.dart -d emulator --timeout 5m || - flutter test integration_test/webcrypto_test.dart -d emulator --timeout 5m || - flutter test integration_test/webcrypto_test.dart -d emulator --timeout 5m diff --git a/.github/workflows/unit.yml b/.github/workflows/unit.yml new file mode 100644 index 000000000..ae44e46d2 --- /dev/null +++ b/.github/workflows/unit.yml @@ -0,0 +1,37 @@ +name: Unit + +on: + pull_request: + branches: [main] + push: + # ci/** branches run it too: a new workflow cannot be started by hand + # before it is on main. + branches: [main, 'ci/**'] + workflow_dispatch: + +permissions: + contents: read + +concurrency: + group: unit-${{ github.ref }} + cancel-in-progress: true + +jobs: + unit: + name: Unit + runs-on: ubuntu-24.04 + timeout-minutes: 20 + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@a9a1bb441373e9ff3801fe91acf7efc80954cbe6 # main + - run: flutter pub get + - run: dart format --output=none --set-exit-if-changed . + - run: dart analyze --fatal-warnings . + - run: dart run kiln:check + # The VM tests, with Valgrind (test/ffi/valgrind_test.dart). + - run: | + sudo apt-get update -qq + sudo apt-get install -y -qq valgrind > /dev/null + - run: dart test --exclude-tags browser-interop -p vm -r expanded From 6dd6e530c38dca346a5e60189618747aec089cf3 Mon Sep 17 00:00:00 2001 From: James O'Leary Date: Mon, 26 Oct 2026 08:33:36 -0700 Subject: [PATCH 32/69] CI: publish.yml and roll_boringssl.yml (was update-boringssl.yml): pinned actions, setup-flutter from telosnex_ci_tools, permissions, timeouts, names --- .github/workflows/publish.yml | 17 +++++++++++------ ...{update-boringssl.yml => roll_boringssl.yml} | 13 +++++-------- 2 files changed, 16 insertions(+), 14 deletions(-) rename .github/workflows/{update-boringssl.yml => roll_boringssl.yml} (89%) diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 9bd6af7ba..220874716 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -1,23 +1,28 @@ -name: Publish to pub.dev +name: Publish on: push: tags: - '[0-9]+.[0-9]+.[0-9]+*' +permissions: + contents: read + jobs: publish: name: Publish to pub.dev - runs-on: ubuntu-latest + runs-on: ubuntu-24.04 + timeout-minutes: 15 environment: pub.dev permissions: id-token: write steps: - - uses: actions/checkout@v4 - - uses: dart-lang/setup-dart@v1 - - uses: subosito/flutter-action@v2 + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: - channel: 'stable' + persist-credentials: false + # Exchanges the OIDC token for a pub.dev token. + - uses: dart-lang/setup-dart@6afc89df92d6eb3834022f73cd65adc8cdfcb92d # v1.8.1 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@a9a1bb441373e9ff3801fe91acf7efc80954cbe6 # main - run: flutter pub get - name: Publish run: flutter pub publish --force diff --git a/.github/workflows/update-boringssl.yml b/.github/workflows/roll_boringssl.yml similarity index 89% rename from .github/workflows/update-boringssl.yml rename to .github/workflows/roll_boringssl.yml index 7c00ff056..df89c86af 100644 --- a/.github/workflows/update-boringssl.yml +++ b/.github/workflows/roll_boringssl.yml @@ -1,4 +1,4 @@ -name: update-boringssl +name: Roll BoringSSL on: workflow_dispatch: @@ -21,17 +21,14 @@ concurrency: jobs: roll: name: Roll BoringSSL - runs-on: ubuntu-latest + runs-on: ubuntu-24.04 timeout-minutes: 60 steps: - - uses: actions/checkout@v4 + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: fetch-depth: 0 - - uses: subosito/flutter-action@v2 - with: - channel: stable - cache: true + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@a9a1bb441373e9ff3801fe91acf7efc80954cbe6 # main - name: Install system dependencies run: | @@ -72,7 +69,7 @@ jobs: - name: Create pull request if: steps.changes.outputs.changed == 'true' - uses: peter-evans/create-pull-request@v7 + uses: peter-evans/create-pull-request@5f6978faf089d4d20b00c7766989d076bb2fc7f1 # v8.1.1 with: branch: "update-boringssl-${{ steps.roll.outputs.short_revision }}" delete-branch: true From f733d7f647fb3b69e05278febaa6044f5ebafe41 Mon Sep 17 00:00:00 2001 From: James O'Leary Date: Mon, 26 Oct 2026 08:48:35 -0700 Subject: [PATCH 33/69] CI: dart-test-browser (Firefox under xvfb, Safari without the file prompt); package:test < 1.33 (1.33 drives Safari through safaridriver and hangs); fixes from the first run --- .github/workflows/integration.yml | 24 +++++++++++------------- .github/workflows/unit.yml | 2 +- pubspec.yaml | 2 +- 3 files changed, 13 insertions(+), 15 deletions(-) diff --git a/.github/workflows/integration.yml b/.github/workflows/integration.yml index e5333735c..b77ac6a3a 100644 --- a/.github/workflows/integration.yml +++ b/.github/workflows/integration.yml @@ -43,7 +43,7 @@ jobs: run: | sudo apt-get update -qq sudo apt-get install -y -qq clang cmake ninja-build pkg-config libgtk-3-dev xvfb > /dev/null - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@a9a1bb441373e9ff3801fe91acf7efc80954cbe6 # main + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@81f9d8a288f7172d3767a2dfa1b6f57fa8a2689b # main - run: flutter pub get - run: dart test --exclude-tags browser-interop -p vm -r expanded - name: Demo app integration test @@ -73,15 +73,13 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@a9a1bb441373e9ff3801fe91acf7efc80954cbe6 # main + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@81f9d8a288f7172d3767a2dfa1b6f57fa8a2689b # main - run: flutter pub get - - if: matrix.platform == 'safari' - uses: Telosnex/telosnex_ci_tools/actions/dart-test-safari@a9a1bb441373e9ff3801fe91acf7efc80954cbe6 # main - - name: dart test -p ${{ matrix.platform }} -c ${{ matrix.compiler }} - env: - PLATFORM: ${{ matrix.platform }} - COMPILER: ${{ matrix.compiler }} - run: dart test --exclude-tags browser-interop -p "$PLATFORM" -c "$COMPILER" -r expanded + - uses: Telosnex/telosnex_ci_tools/actions/dart-test-browser@81f9d8a288f7172d3767a2dfa1b6f57fa8a2689b # main + with: + platform: ${{ matrix.platform }} + compiler: ${{ matrix.compiler }} + args: --exclude-tags browser-interop ios: name: iOS ARM64 simulator @@ -91,9 +89,9 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@a9a1bb441373e9ff3801fe91acf7efc80954cbe6 # main + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@81f9d8a288f7172d3767a2dfa1b6f57fa8a2689b # main - run: flutter pub get - - uses: Telosnex/telosnex_ci_tools/actions/ios-flutter-test@a9a1bb441373e9ff3801fe91acf7efc80954cbe6 # main + - uses: Telosnex/telosnex_ci_tools/actions/ios-flutter-test@81f9d8a288f7172d3767a2dfa1b6f57fa8a2689b # main with: working-directory: example/webcrypto_demo_flutter_app target: integration_test/webcrypto_test.dart @@ -107,9 +105,9 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@a9a1bb441373e9ff3801fe91acf7efc80954cbe6 # main + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@81f9d8a288f7172d3767a2dfa1b6f57fa8a2689b # main - run: flutter pub get - - uses: Telosnex/telosnex_ci_tools/actions/android-emulator@a9a1bb441373e9ff3801fe91acf7efc80954cbe6 # main + - uses: Telosnex/telosnex_ci_tools/actions/android-emulator@81f9d8a288f7172d3767a2dfa1b6f57fa8a2689b # main id: emulator with: api-level: '36' diff --git a/.github/workflows/unit.yml b/.github/workflows/unit.yml index ae44e46d2..8cfbda900 100644 --- a/.github/workflows/unit.yml +++ b/.github/workflows/unit.yml @@ -25,7 +25,7 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@a9a1bb441373e9ff3801fe91acf7efc80954cbe6 # main + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@81f9d8a288f7172d3767a2dfa1b6f57fa8a2689b # main - run: flutter pub get - run: dart format --output=none --set-exit-if-changed . - run: dart analyze --fatal-warnings . diff --git a/pubspec.yaml b/pubspec.yaml index 1fd5c80d1..14a8944fe 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -33,7 +33,7 @@ dependencies: dev_dependencies: ffigen: ^11.0.0 - test: ^1.30.0 + test: '>=1.28.0 <1.33.0' yaml: ^3.0.0 flutter_lints: ^6.0.0 coverage: any # we only need format_coverage, don't care what version From 1a377f1a3a333260da3f7c9cd0142b2d6aa593b8 Mon Sep 17 00:00:00 2001 From: James O'Leary Date: Mon, 26 Oct 2026 09:12:25 -0700 Subject: [PATCH 34/69] CI: no package:test pin (it changed the source key); Safari through safaridriver (package:test 1.33), old launcher removed; ci_tools ac661f9 --- .github/workflows/integration.yml | 14 +++++++------- .github/workflows/publish.yml | 2 +- .github/workflows/roll_boringssl.yml | 2 +- .github/workflows/unit.yml | 2 +- pubspec.yaml | 2 +- 5 files changed, 11 insertions(+), 11 deletions(-) diff --git a/.github/workflows/integration.yml b/.github/workflows/integration.yml index b77ac6a3a..e433cfc8b 100644 --- a/.github/workflows/integration.yml +++ b/.github/workflows/integration.yml @@ -43,7 +43,7 @@ jobs: run: | sudo apt-get update -qq sudo apt-get install -y -qq clang cmake ninja-build pkg-config libgtk-3-dev xvfb > /dev/null - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@81f9d8a288f7172d3767a2dfa1b6f57fa8a2689b # main + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@ac661f93e2b65b203d12956ecc215da3b8ddf871 # main - run: flutter pub get - run: dart test --exclude-tags browser-interop -p vm -r expanded - name: Demo app integration test @@ -73,9 +73,9 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@81f9d8a288f7172d3767a2dfa1b6f57fa8a2689b # main + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@ac661f93e2b65b203d12956ecc215da3b8ddf871 # main - run: flutter pub get - - uses: Telosnex/telosnex_ci_tools/actions/dart-test-browser@81f9d8a288f7172d3767a2dfa1b6f57fa8a2689b # main + - uses: Telosnex/telosnex_ci_tools/actions/dart-test-browser@ac661f93e2b65b203d12956ecc215da3b8ddf871 # main with: platform: ${{ matrix.platform }} compiler: ${{ matrix.compiler }} @@ -89,9 +89,9 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@81f9d8a288f7172d3767a2dfa1b6f57fa8a2689b # main + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@ac661f93e2b65b203d12956ecc215da3b8ddf871 # main - run: flutter pub get - - uses: Telosnex/telosnex_ci_tools/actions/ios-flutter-test@81f9d8a288f7172d3767a2dfa1b6f57fa8a2689b # main + - uses: Telosnex/telosnex_ci_tools/actions/ios-flutter-test@ac661f93e2b65b203d12956ecc215da3b8ddf871 # main with: working-directory: example/webcrypto_demo_flutter_app target: integration_test/webcrypto_test.dart @@ -105,9 +105,9 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@81f9d8a288f7172d3767a2dfa1b6f57fa8a2689b # main + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@ac661f93e2b65b203d12956ecc215da3b8ddf871 # main - run: flutter pub get - - uses: Telosnex/telosnex_ci_tools/actions/android-emulator@81f9d8a288f7172d3767a2dfa1b6f57fa8a2689b # main + - uses: Telosnex/telosnex_ci_tools/actions/android-emulator@ac661f93e2b65b203d12956ecc215da3b8ddf871 # main id: emulator with: api-level: '36' diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 220874716..e9702f188 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -22,7 +22,7 @@ jobs: persist-credentials: false # Exchanges the OIDC token for a pub.dev token. - uses: dart-lang/setup-dart@6afc89df92d6eb3834022f73cd65adc8cdfcb92d # v1.8.1 - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@a9a1bb441373e9ff3801fe91acf7efc80954cbe6 # main + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@ac661f93e2b65b203d12956ecc215da3b8ddf871 # main - run: flutter pub get - name: Publish run: flutter pub publish --force diff --git a/.github/workflows/roll_boringssl.yml b/.github/workflows/roll_boringssl.yml index df89c86af..daebda613 100644 --- a/.github/workflows/roll_boringssl.yml +++ b/.github/workflows/roll_boringssl.yml @@ -28,7 +28,7 @@ jobs: with: fetch-depth: 0 - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@a9a1bb441373e9ff3801fe91acf7efc80954cbe6 # main + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@ac661f93e2b65b203d12956ecc215da3b8ddf871 # main - name: Install system dependencies run: | diff --git a/.github/workflows/unit.yml b/.github/workflows/unit.yml index 8cfbda900..af33c970f 100644 --- a/.github/workflows/unit.yml +++ b/.github/workflows/unit.yml @@ -25,7 +25,7 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@81f9d8a288f7172d3767a2dfa1b6f57fa8a2689b # main + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@ac661f93e2b65b203d12956ecc215da3b8ddf871 # main - run: flutter pub get - run: dart format --output=none --set-exit-if-changed . - run: dart analyze --fatal-warnings . diff --git a/pubspec.yaml b/pubspec.yaml index 14a8944fe..1fd5c80d1 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -33,7 +33,7 @@ dependencies: dev_dependencies: ffigen: ^11.0.0 - test: '>=1.28.0 <1.33.0' + test: ^1.30.0 yaml: ^3.0.0 flutter_lints: ^6.0.0 coverage: any # we only need format_coverage, don't care what version From a59d583e830bc89688a9f6ab0e3c1c3ea7a7d3de Mon Sep 17 00:00:00 2001 From: James O'Leary Date: Mon, 26 Oct 2026 10:01:14 -0700 Subject: [PATCH 35/69] Test: skip RSA e=3 key generation cases on Safari (CommonCrypto rejects e=3, likely FIPS 186-4 e > 2^16; imported e=3 keys still tested) --- lib/src/testing/utils/detected_runtime.dart | 26 +++++++++++++++++++ lib/src/testing/webcrypto/rsaoaep.dart | 3 ++- lib/src/testing/webcrypto/rsapss.dart | 2 +- lib/src/testing/webcrypto/rsassapkcs1v15.dart | 3 ++- 4 files changed, 31 insertions(+), 3 deletions(-) diff --git a/lib/src/testing/utils/detected_runtime.dart b/lib/src/testing/utils/detected_runtime.dart index b70358985..f814c960e 100644 --- a/lib/src/testing/utils/detected_runtime.dart +++ b/lib/src/testing/utils/detected_runtime.dart @@ -33,3 +33,29 @@ T? nullOnFirefox(T value) => detectedRuntime == 'firefox' ? null : value; /// This utility helps filter away test cases and features known to not work on /// Safari and which has been documented in the API documentation. T? nullOnSafari(T value) => detectedRuntime == 'safari' ? null : value; + +/// Removes, on Safari, the test cases that generate an RSA key with public +/// exponent 3; other browsers and the VM keep them. +/// +/// Safari cannot generate such a key: `generateKey` throws `OperationError`. +/// WebKit's `CryptoKeyRSA::generatePair` (Source/WebCore/crypto/cocoa/ +/// CryptoKeyRSACocoa.cpp) passes the exponent to CommonCrypto's +/// `CCRSACryptorGeneratePair` and turns any failure into `OperationError`. +/// CommonCrypto rejects e = 3, most likely because it follows FIPS 186-4 +/// (B.3.1: e > 2^16), which rules out small exponents after the e = 3 attacks +/// on careless RSA use (Hastad's broadcast attack, cube roots of unpadded +/// messages, Bleichenbacher's 2006 signature forgery). CommonCrypto returns +/// only a status code, so the FIPS reason is inferred, not confirmed. OAEP and +/// PSS padding prevent those attacks, and Chrome and Firefox generate e = 3. +/// +/// Safari does import and use e = 3 keys: the cases with keys generated on +/// Linux or Chrome pass, so only generation is skipped. +/// `test/crypto_subtle_test.dart` ("generateKeyPair: e3") checks that Safari +/// still rejects generation. +List withoutRsaE3GenerationOnSafari(List cases) { + if (detectedRuntime != 'safari') return cases; + return cases.where((c) { + final params = c['generateKeyParams']; + return !(params is Map && '${params['publicExponent']}' == '3'); + }).toList(); +} diff --git a/lib/src/testing/webcrypto/rsaoaep.dart b/lib/src/testing/webcrypto/rsaoaep.dart index 22e03f13e..2b2c63d25 100644 --- a/lib/src/testing/webcrypto/rsaoaep.dart +++ b/lib/src/testing/webcrypto/rsaoaep.dart @@ -12,6 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +import '../utils/detected_runtime.dart'; import 'package:webcrypto/webcrypto.dart'; import '../utils/utils.dart'; import '../utils/testrunner.dart'; @@ -49,7 +50,7 @@ final runner = TestRunner.asymmetric( key.encryptBytes(data, label: bytesFromJson(encryptParams, 'label')), decryptBytes: (key, data, decryptParams) => key.decryptBytes(data, label: bytesFromJson(decryptParams, 'label')), - testData: _testData, + testData: withoutRsaE3GenerationOnSafari(_testData), ); void main() => runner.tests().runTests(); diff --git a/lib/src/testing/webcrypto/rsapss.dart b/lib/src/testing/webcrypto/rsapss.dart index b54d6b2d4..a0667120d 100644 --- a/lib/src/testing/webcrypto/rsapss.dart +++ b/lib/src/testing/webcrypto/rsapss.dart @@ -54,7 +54,7 @@ final runner = TestRunner.asymmetric( key.verifyBytes(signature, data, verifyParams['saltLength']), verifyStream: (key, signature, data, verifyParams) => key.verifyStream(signature, data, verifyParams['saltLength']), - testData: _testData, + testData: withoutRsaE3GenerationOnSafari(_testData), ); void main() => runner.tests().runTests(); diff --git a/lib/src/testing/webcrypto/rsassapkcs1v15.dart b/lib/src/testing/webcrypto/rsassapkcs1v15.dart index 82c824567..58a5f4096 100644 --- a/lib/src/testing/webcrypto/rsassapkcs1v15.dart +++ b/lib/src/testing/webcrypto/rsassapkcs1v15.dart @@ -12,6 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +import '../utils/detected_runtime.dart'; import 'package:webcrypto/webcrypto.dart'; import '../utils/utils.dart'; import '../utils/testrunner.dart'; @@ -59,7 +60,7 @@ final runner = key.verifyBytes(signature, data), verifyStream: (key, signature, data, verifyParams) => key.verifyStream(signature, data), - testData: _testData, + testData: withoutRsaE3GenerationOnSafari(_testData), ); void main() => runner.tests().runTests(); From a850409c5ed9fd73aac5e77addc89fd0af0c0885 Mon Sep 17 00:00:00 2001 From: James O'Leary Date: Fri, 30 Oct 2026 15:24:09 -0700 Subject: [PATCH 36/69] ci: Telosnex/telosnex_ci_tools v0.5.6 --- .github/workflows/integration.yml | 14 +++++++------- .github/workflows/kiln_check.yml | 2 +- .github/workflows/kiln_release.yml | 2 +- .github/workflows/publish.yml | 2 +- .github/workflows/roll_boringssl.yml | 2 +- .github/workflows/unit.yml | 2 +- 6 files changed, 12 insertions(+), 12 deletions(-) diff --git a/.github/workflows/integration.yml b/.github/workflows/integration.yml index e433cfc8b..f7b3930da 100644 --- a/.github/workflows/integration.yml +++ b/.github/workflows/integration.yml @@ -43,7 +43,7 @@ jobs: run: | sudo apt-get update -qq sudo apt-get install -y -qq clang cmake ninja-build pkg-config libgtk-3-dev xvfb > /dev/null - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@ac661f93e2b65b203d12956ecc215da3b8ddf871 # main + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@0554f3662445d678e2effbd838d15968d673f7af # v0.5.6 # main - run: flutter pub get - run: dart test --exclude-tags browser-interop -p vm -r expanded - name: Demo app integration test @@ -73,9 +73,9 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@ac661f93e2b65b203d12956ecc215da3b8ddf871 # main + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@0554f3662445d678e2effbd838d15968d673f7af # v0.5.6 # main - run: flutter pub get - - uses: Telosnex/telosnex_ci_tools/actions/dart-test-browser@ac661f93e2b65b203d12956ecc215da3b8ddf871 # main + - uses: Telosnex/telosnex_ci_tools/actions/dart-test-browser@0554f3662445d678e2effbd838d15968d673f7af # v0.5.6 # main with: platform: ${{ matrix.platform }} compiler: ${{ matrix.compiler }} @@ -89,9 +89,9 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@ac661f93e2b65b203d12956ecc215da3b8ddf871 # main + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@0554f3662445d678e2effbd838d15968d673f7af # v0.5.6 # main - run: flutter pub get - - uses: Telosnex/telosnex_ci_tools/actions/ios-flutter-test@ac661f93e2b65b203d12956ecc215da3b8ddf871 # main + - uses: Telosnex/telosnex_ci_tools/actions/ios-flutter-test@0554f3662445d678e2effbd838d15968d673f7af # v0.5.6 # main with: working-directory: example/webcrypto_demo_flutter_app target: integration_test/webcrypto_test.dart @@ -105,9 +105,9 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@ac661f93e2b65b203d12956ecc215da3b8ddf871 # main + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@0554f3662445d678e2effbd838d15968d673f7af # v0.5.6 # main - run: flutter pub get - - uses: Telosnex/telosnex_ci_tools/actions/android-emulator@ac661f93e2b65b203d12956ecc215da3b8ddf871 # main + - uses: Telosnex/telosnex_ci_tools/actions/android-emulator@0554f3662445d678e2effbd838d15968d673f7af # v0.5.6 # main id: emulator with: api-level: '36' diff --git a/.github/workflows/kiln_check.yml b/.github/workflows/kiln_check.yml index b45a06ff7..af67d8909 100644 --- a/.github/workflows/kiln_check.yml +++ b/.github/workflows/kiln_check.yml @@ -16,4 +16,4 @@ permissions: jobs: kiln-check: - uses: Telosnex/telosnex_ci_tools/.github/workflows/kiln_check.yml@0e3d782cce29ab6f9fdb2fa6d796bcb365882407 # v0.1.2 + uses: Telosnex/telosnex_ci_tools/.github/workflows/kiln_check.yml@0554f3662445d678e2effbd838d15968d673f7af # v0.5.6 diff --git a/.github/workflows/kiln_release.yml b/.github/workflows/kiln_release.yml index f6450ddc6..cbc4f0a3c 100644 --- a/.github/workflows/kiln_release.yml +++ b/.github/workflows/kiln_release.yml @@ -25,7 +25,7 @@ permissions: jobs: kiln-release: - uses: Telosnex/telosnex_ci_tools/.github/workflows/kiln_release.yml@0e3d782cce29ab6f9fdb2fa6d796bcb365882407 # v0.1.2 + uses: Telosnex/telosnex_ci_tools/.github/workflows/kiln_release.yml@0554f3662445d678e2effbd838d15968d673f7af # v0.5.6 permissions: contents: write with: diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index e9702f188..4b1dfd37e 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -22,7 +22,7 @@ jobs: persist-credentials: false # Exchanges the OIDC token for a pub.dev token. - uses: dart-lang/setup-dart@6afc89df92d6eb3834022f73cd65adc8cdfcb92d # v1.8.1 - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@ac661f93e2b65b203d12956ecc215da3b8ddf871 # main + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@0554f3662445d678e2effbd838d15968d673f7af # v0.5.6 # main - run: flutter pub get - name: Publish run: flutter pub publish --force diff --git a/.github/workflows/roll_boringssl.yml b/.github/workflows/roll_boringssl.yml index daebda613..6f7a5d81a 100644 --- a/.github/workflows/roll_boringssl.yml +++ b/.github/workflows/roll_boringssl.yml @@ -28,7 +28,7 @@ jobs: with: fetch-depth: 0 - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@ac661f93e2b65b203d12956ecc215da3b8ddf871 # main + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@0554f3662445d678e2effbd838d15968d673f7af # v0.5.6 # main - name: Install system dependencies run: | diff --git a/.github/workflows/unit.yml b/.github/workflows/unit.yml index af33c970f..c3281b0d8 100644 --- a/.github/workflows/unit.yml +++ b/.github/workflows/unit.yml @@ -25,7 +25,7 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@ac661f93e2b65b203d12956ecc215da3b8ddf871 # main + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@0554f3662445d678e2effbd838d15968d673f7af # v0.5.6 # main - run: flutter pub get - run: dart format --output=none --set-exit-if-changed . - run: dart analyze --fatal-warnings . From a54c2ffd2e720bfbe3bc4f9108410f737e526626 Mon Sep 17 00:00:00 2001 From: James O'Leary <65884233+jpohhhh@users.noreply.github.com> Date: Fri, 30 Oct 2026 17:09:36 -0700 Subject: [PATCH 37/69] ci: lint in Unit, no matrix; drop publish.yml (#4) telosnex_ci_tools pins: v0.5.6, already on main. --- .github/workflows/integration.yml | 140 +++++++++++++++++++-------- .github/workflows/publish.yml | 28 ------ .github/workflows/roll_boringssl.yml | 2 +- .github/workflows/unit.yml | 3 +- 4 files changed, 104 insertions(+), 69 deletions(-) delete mode 100644 .github/workflows/publish.yml diff --git a/.github/workflows/integration.yml b/.github/workflows/integration.yml index f7b3930da..8aa8990e6 100644 --- a/.github/workflows/integration.yml +++ b/.github/workflows/integration.yml @@ -20,22 +20,15 @@ defaults: jobs: # The VM tests against the released BoringSSL library of each platform, - # then the demo app's integration test (Flutter desktop). - native: - name: ${{ matrix.name }} - runs-on: ${{ matrix.runner }} + # then the demo app's integration test (Flutter desktop). The jobs share + # the steps of the first (YAML anchor). + desktop-linux-x64: + name: Linux x64 + runs-on: ubuntu-24.04 timeout-minutes: 40 - strategy: - fail-fast: false - matrix: - include: - - { name: Linux x64, runner: ubuntu-24.04, device: linux } - - { name: Linux ARM64, runner: ubuntu-24.04-arm, device: linux } - - { name: macOS ARM64, runner: macos-26, device: macos } - - { name: macOS x64, runner: macos-15-intel, device: macos } - - { name: Windows x64, runner: windows-2025, device: windows } - - { name: Windows ARM64, runner: windows-11-arm, device: windows } - steps: + env: + DEVICE: linux + steps: &native_steps - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false @@ -43,44 +36,113 @@ jobs: run: | sudo apt-get update -qq sudo apt-get install -y -qq clang cmake ninja-build pkg-config libgtk-3-dev xvfb > /dev/null - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@0554f3662445d678e2effbd838d15968d673f7af # v0.5.6 # main + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@0554f3662445d678e2effbd838d15968d673f7af # v0.5.6 - run: flutter pub get - run: dart test --exclude-tags browser-interop -p vm -r expanded - name: Demo app integration test working-directory: example/webcrypto_demo_flutter_app - env: - DEVICE: ${{ matrix.device }} run: | flutter pub get run=(flutter test integration_test/webcrypto_test.dart -d "$DEVICE" --timeout 5m -r expanded) if [[ "$RUNNER_OS" == Linux ]]; then run=(xvfb-run -a "${run[@]}"); fi "${run[@]}" - web: - name: ${{ matrix.name }} - runs-on: ${{ matrix.runner }} + desktop-linux-arm64: + name: Linux ARM64 + runs-on: ubuntu-24.04-arm + timeout-minutes: 40 + env: + DEVICE: linux + steps: *native_steps + + desktop-macos-arm64: + name: macOS ARM64 + runs-on: macos-26 + timeout-minutes: 40 + env: + DEVICE: macos + steps: *native_steps + + desktop-macos-x64: + name: macOS x64 + runs-on: macos-15-intel + timeout-minutes: 40 + env: + DEVICE: macos + steps: *native_steps + + desktop-windows-x64: + name: Windows x64 + runs-on: windows-2025 + timeout-minutes: 40 + env: + DEVICE: windows + steps: *native_steps + + desktop-windows-arm64: + name: Windows ARM64 + runs-on: windows-11-arm + timeout-minutes: 40 + env: + DEVICE: windows + steps: *native_steps + + # The jobs share the steps of the first (YAML anchor). + web-linux-chrome-dart2js: + name: Web Linux x64 Chrome dart2js + runs-on: ubuntu-24.04 timeout-minutes: 30 - strategy: - fail-fast: false - matrix: - include: - - { name: Web Linux x64 Chrome dart2js, runner: ubuntu-24.04, platform: chrome, compiler: dart2js } - - { name: Web Linux x64 Chrome dart2wasm, runner: ubuntu-24.04, platform: chrome, compiler: dart2wasm } - - { name: Web Linux x64 Firefox dart2js, runner: ubuntu-24.04, platform: firefox, compiler: dart2js } - - { name: Web Linux x64 Firefox dart2wasm, runner: ubuntu-24.04, platform: firefox, compiler: dart2wasm } - - { name: Web macOS ARM64 Safari dart2js, runner: macos-26, platform: safari, compiler: dart2js } - steps: + env: + BROWSER: chrome + COMPILER: dart2js + steps: &web_steps - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@0554f3662445d678e2effbd838d15968d673f7af # v0.5.6 # main + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@0554f3662445d678e2effbd838d15968d673f7af # v0.5.6 - run: flutter pub get - - uses: Telosnex/telosnex_ci_tools/actions/dart-test-browser@0554f3662445d678e2effbd838d15968d673f7af # v0.5.6 # main + - uses: Telosnex/telosnex_ci_tools/actions/dart-test-browser@0554f3662445d678e2effbd838d15968d673f7af # v0.5.6 with: - platform: ${{ matrix.platform }} - compiler: ${{ matrix.compiler }} + platform: ${{ env.BROWSER }} + compiler: ${{ env.COMPILER }} args: --exclude-tags browser-interop + web-linux-chrome-dart2wasm: + name: Web Linux x64 Chrome dart2wasm + runs-on: ubuntu-24.04 + timeout-minutes: 30 + env: + BROWSER: chrome + COMPILER: dart2wasm + steps: *web_steps + + web-linux-firefox-dart2js: + name: Web Linux x64 Firefox dart2js + runs-on: ubuntu-24.04 + timeout-minutes: 30 + env: + BROWSER: firefox + COMPILER: dart2js + steps: *web_steps + + web-linux-firefox-dart2wasm: + name: Web Linux x64 Firefox dart2wasm + runs-on: ubuntu-24.04 + timeout-minutes: 30 + env: + BROWSER: firefox + COMPILER: dart2wasm + steps: *web_steps + + web-macos-safari-dart2js: + name: Web macOS ARM64 Safari dart2js + runs-on: macos-26 + timeout-minutes: 30 + env: + BROWSER: safari + COMPILER: dart2js + steps: *web_steps + ios: name: iOS ARM64 simulator runs-on: macos-26 @@ -89,9 +151,9 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@0554f3662445d678e2effbd838d15968d673f7af # v0.5.6 # main + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@0554f3662445d678e2effbd838d15968d673f7af # v0.5.6 - run: flutter pub get - - uses: Telosnex/telosnex_ci_tools/actions/ios-flutter-test@0554f3662445d678e2effbd838d15968d673f7af # v0.5.6 # main + - uses: Telosnex/telosnex_ci_tools/actions/ios-flutter-test@0554f3662445d678e2effbd838d15968d673f7af # v0.5.6 with: working-directory: example/webcrypto_demo_flutter_app target: integration_test/webcrypto_test.dart @@ -105,9 +167,9 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@0554f3662445d678e2effbd838d15968d673f7af # v0.5.6 # main + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@0554f3662445d678e2effbd838d15968d673f7af # v0.5.6 - run: flutter pub get - - uses: Telosnex/telosnex_ci_tools/actions/android-emulator@0554f3662445d678e2effbd838d15968d673f7af # v0.5.6 # main + - uses: Telosnex/telosnex_ci_tools/actions/android-emulator@0554f3662445d678e2effbd838d15968d673f7af # v0.5.6 id: emulator with: api-level: '36' diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml deleted file mode 100644 index 4b1dfd37e..000000000 --- a/.github/workflows/publish.yml +++ /dev/null @@ -1,28 +0,0 @@ -name: Publish - -on: - push: - tags: - - '[0-9]+.[0-9]+.[0-9]+*' - -permissions: - contents: read - -jobs: - publish: - name: Publish to pub.dev - runs-on: ubuntu-24.04 - timeout-minutes: 15 - environment: pub.dev - permissions: - id-token: write - steps: - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - with: - persist-credentials: false - # Exchanges the OIDC token for a pub.dev token. - - uses: dart-lang/setup-dart@6afc89df92d6eb3834022f73cd65adc8cdfcb92d # v1.8.1 - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@0554f3662445d678e2effbd838d15968d673f7af # v0.5.6 # main - - run: flutter pub get - - name: Publish - run: flutter pub publish --force diff --git a/.github/workflows/roll_boringssl.yml b/.github/workflows/roll_boringssl.yml index 6f7a5d81a..41fde7cb5 100644 --- a/.github/workflows/roll_boringssl.yml +++ b/.github/workflows/roll_boringssl.yml @@ -28,7 +28,7 @@ jobs: with: fetch-depth: 0 - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@0554f3662445d678e2effbd838d15968d673f7af # v0.5.6 # main + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@0554f3662445d678e2effbd838d15968d673f7af # v0.5.6 - name: Install system dependencies run: | diff --git a/.github/workflows/unit.yml b/.github/workflows/unit.yml index c3281b0d8..6a996375b 100644 --- a/.github/workflows/unit.yml +++ b/.github/workflows/unit.yml @@ -25,7 +25,8 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@0554f3662445d678e2effbd838d15968d673f7af # v0.5.6 # main + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@0554f3662445d678e2effbd838d15968d673f7af # v0.5.6 + - uses: Telosnex/telosnex_ci_tools/actions/lint@0554f3662445d678e2effbd838d15968d673f7af # v0.5.6 - run: flutter pub get - run: dart format --output=none --set-exit-if-changed . - run: dart analyze --fatal-warnings . From 4e97c87480903d72e2cd11b23ae5a5797a10f437 Mon Sep 17 00:00:00 2001 From: James O'Leary Date: Wed, 4 Nov 2026 08:34:18 -0800 Subject: [PATCH 38/69] ci: Telosnex/telosnex_ci_tools v0.5.7 --- .github/workflows/integration.yml | 14 +++++++------- .github/workflows/kiln_check.yml | 2 +- .github/workflows/kiln_release.yml | 2 +- .github/workflows/roll_boringssl.yml | 2 +- .github/workflows/unit.yml | 4 ++-- 5 files changed, 12 insertions(+), 12 deletions(-) diff --git a/.github/workflows/integration.yml b/.github/workflows/integration.yml index 8aa8990e6..545e5617f 100644 --- a/.github/workflows/integration.yml +++ b/.github/workflows/integration.yml @@ -36,7 +36,7 @@ jobs: run: | sudo apt-get update -qq sudo apt-get install -y -qq clang cmake ninja-build pkg-config libgtk-3-dev xvfb > /dev/null - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@0554f3662445d678e2effbd838d15968d673f7af # v0.5.6 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@c03733211dca28d8590002199d7a87af85edde51 # v0.5.7 - run: flutter pub get - run: dart test --exclude-tags browser-interop -p vm -r expanded - name: Demo app integration test @@ -99,9 +99,9 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@0554f3662445d678e2effbd838d15968d673f7af # v0.5.6 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@c03733211dca28d8590002199d7a87af85edde51 # v0.5.7 - run: flutter pub get - - uses: Telosnex/telosnex_ci_tools/actions/dart-test-browser@0554f3662445d678e2effbd838d15968d673f7af # v0.5.6 + - uses: Telosnex/telosnex_ci_tools/actions/dart-test-browser@c03733211dca28d8590002199d7a87af85edde51 # v0.5.7 with: platform: ${{ env.BROWSER }} compiler: ${{ env.COMPILER }} @@ -151,9 +151,9 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@0554f3662445d678e2effbd838d15968d673f7af # v0.5.6 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@c03733211dca28d8590002199d7a87af85edde51 # v0.5.7 - run: flutter pub get - - uses: Telosnex/telosnex_ci_tools/actions/ios-flutter-test@0554f3662445d678e2effbd838d15968d673f7af # v0.5.6 + - uses: Telosnex/telosnex_ci_tools/actions/ios-flutter-test@c03733211dca28d8590002199d7a87af85edde51 # v0.5.7 with: working-directory: example/webcrypto_demo_flutter_app target: integration_test/webcrypto_test.dart @@ -167,9 +167,9 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@0554f3662445d678e2effbd838d15968d673f7af # v0.5.6 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@c03733211dca28d8590002199d7a87af85edde51 # v0.5.7 - run: flutter pub get - - uses: Telosnex/telosnex_ci_tools/actions/android-emulator@0554f3662445d678e2effbd838d15968d673f7af # v0.5.6 + - uses: Telosnex/telosnex_ci_tools/actions/android-emulator@c03733211dca28d8590002199d7a87af85edde51 # v0.5.7 id: emulator with: api-level: '36' diff --git a/.github/workflows/kiln_check.yml b/.github/workflows/kiln_check.yml index af67d8909..e3cf2a110 100644 --- a/.github/workflows/kiln_check.yml +++ b/.github/workflows/kiln_check.yml @@ -16,4 +16,4 @@ permissions: jobs: kiln-check: - uses: Telosnex/telosnex_ci_tools/.github/workflows/kiln_check.yml@0554f3662445d678e2effbd838d15968d673f7af # v0.5.6 + uses: Telosnex/telosnex_ci_tools/.github/workflows/kiln_check.yml@c03733211dca28d8590002199d7a87af85edde51 # v0.5.7 diff --git a/.github/workflows/kiln_release.yml b/.github/workflows/kiln_release.yml index cbc4f0a3c..fb1a42ac6 100644 --- a/.github/workflows/kiln_release.yml +++ b/.github/workflows/kiln_release.yml @@ -25,7 +25,7 @@ permissions: jobs: kiln-release: - uses: Telosnex/telosnex_ci_tools/.github/workflows/kiln_release.yml@0554f3662445d678e2effbd838d15968d673f7af # v0.5.6 + uses: Telosnex/telosnex_ci_tools/.github/workflows/kiln_release.yml@c03733211dca28d8590002199d7a87af85edde51 # v0.5.7 permissions: contents: write with: diff --git a/.github/workflows/roll_boringssl.yml b/.github/workflows/roll_boringssl.yml index 41fde7cb5..343b57d85 100644 --- a/.github/workflows/roll_boringssl.yml +++ b/.github/workflows/roll_boringssl.yml @@ -28,7 +28,7 @@ jobs: with: fetch-depth: 0 - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@0554f3662445d678e2effbd838d15968d673f7af # v0.5.6 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@c03733211dca28d8590002199d7a87af85edde51 # v0.5.7 - name: Install system dependencies run: | diff --git a/.github/workflows/unit.yml b/.github/workflows/unit.yml index 6a996375b..95a3f5284 100644 --- a/.github/workflows/unit.yml +++ b/.github/workflows/unit.yml @@ -25,8 +25,8 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@0554f3662445d678e2effbd838d15968d673f7af # v0.5.6 - - uses: Telosnex/telosnex_ci_tools/actions/lint@0554f3662445d678e2effbd838d15968d673f7af # v0.5.6 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@c03733211dca28d8590002199d7a87af85edde51 # v0.5.7 + - uses: Telosnex/telosnex_ci_tools/actions/lint@c03733211dca28d8590002199d7a87af85edde51 # v0.5.7 - run: flutter pub get - run: dart format --output=none --set-exit-if-changed . - run: dart analyze --fatal-warnings . From d7fdce8f8532acb08823f006682cad983857e2f9 Mon Sep 17 00:00:00 2001 From: James O'Leary Date: Wed, 4 Nov 2026 08:36:14 -0800 Subject: [PATCH 39/69] ci: upstream watch (telosnex_ci_tools v0.5.7) .github/upstream.yaml lists the code this package pins from other projects; the daily Upstream watch workflow fails when it has an update. --- .github/upstream.yaml | 9 +++++++++ .github/workflows/upstream_watch.yml | 19 +++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 .github/upstream.yaml create mode 100644 .github/workflows/upstream_watch.yml diff --git a/.github/upstream.yaml b/.github/upstream.yaml new file mode 100644 index 000000000..1cd8f776a --- /dev/null +++ b/.github/upstream.yaml @@ -0,0 +1,9 @@ +# Upstream sources, checked daily by the Upstream watch workflow +# (telosnex_ci_tools, lib/src/upstream.dart). Set `seen` to skip an update. +# BoringSSL has no releases and changes daily: alert when the pin is a month +# behind. Update with the Roll BoringSSL workflow. +sources: + - name: BoringSSL + branch: https://github.com/google/boringssl + pin: {file: tool/REVISION, pattern: '([0-9a-f]{40})'} + lag_days: 30 diff --git a/.github/workflows/upstream_watch.yml b/.github/workflows/upstream_watch.yml new file mode 100644 index 000000000..128c655b7 --- /dev/null +++ b/.github/workflows/upstream_watch.yml @@ -0,0 +1,19 @@ +# Daily: do the sources in .github/upstream.yaml have an update? The run +# fails when one does, and GitHub emails whoever last changed this file. +# See Upstream watch in the telosnex_ci_tools README. +name: Upstream watch + +on: + schedule: + - cron: '23 7 * * *' + push: + branches: [main] + paths: [.github/upstream.yaml] + workflow_dispatch: + +permissions: + contents: read + +jobs: + watch: + uses: Telosnex/telosnex_ci_tools/.github/workflows/upstream_watch.yml@c03733211dca28d8590002199d7a87af85edde51 # v0.5.7 From e823b9b4e66e3cf447b954ff910bac837f508a01 Mon Sep 17 00:00:00 2001 From: James O'Leary Date: Wed, 4 Nov 2026 14:28:41 -0800 Subject: [PATCH 40/69] chore: update BoringSSL to fd0ed4d0 5ee9407b..fd0ed4d0a1273570612a08a31bace08e4dd73c81, 575 commits, with tool/bump-boringssl-revision.sh (bindings regenerated on Linux: ffigen stops on macOS SDK nullability warnings, with the old headers too). The bindings change only in two private BoringSSL structs that webcrypto never allocates (it uses the _new functions): EVP_CIPHER_CTX loses `poisoned` and a union grows from 208 to 240 bytes. Tested on macOS arm64, built from source: dart test on the VM (1444 pass, 1 skipped), Chrome (1462) and Firefox (1436), the same as before. --- .../boringssl/generated_bindings.dart | 784 ++-- .../gen/bcm/aes-gcm-avx2-x86_64-apple.S | 448 +-- .../gen/bcm/aes-gcm-avx2-x86_64-linux.S | 444 +-- .../gen/bcm/aes-gcm-avx2-x86_64-win.asm | 462 +-- .../gen/bcm/aes-gcm-avx512-x86_64-apple.S | 826 ++-- .../gen/bcm/aes-gcm-avx512-x86_64-linux.S | 822 ++-- .../gen/bcm/aes-gcm-avx512-x86_64-win.asm | 838 ++-- .../gen/bcm/aesni-gcm-x86_64-apple.S | 313 +- .../gen/bcm/aesni-gcm-x86_64-linux.S | 310 +- .../gen/bcm/aesni-gcm-x86_64-win.asm | 336 +- .../boringssl/gen/bcm/aesni-x86_64-apple.S | 571 +-- .../boringssl/gen/bcm/aesni-x86_64-linux.S | 554 +-- .../boringssl/gen/bcm/aesni-x86_64-win.asm | 708 ++-- .../boringssl/gen/bcm/aesv8-armv7-linux.S | 2 + .../boringssl/gen/bcm/aesv8-armv8-apple.S | 7 + .../boringssl/gen/bcm/aesv8-armv8-linux.S | 2 + .../boringssl/gen/bcm/aesv8-armv8-win.S | 2 + .../boringssl/gen/bcm/aesv8-gcm-armv8-apple.S | 3 + .../boringssl/gen/bcm/armv8-mont-apple.S | 18 +- .../boringssl/gen/bcm/armv8-mont-linux.S | 24 +- .../boringssl/gen/bcm/armv8-mont-win.S | 22 +- .../boringssl/gen/bcm/bn-armv8-apple.S | 1 + .../gen/bcm/ghash-neon-armv8-apple.S | 4 + .../gen/bcm/ghash-neon-armv8-linux.S | 2 + .../boringssl/gen/bcm/ghash-neon-armv8-win.S | 2 + .../gen/bcm/ghash-ssse3-x86_64-apple.S | 239 +- .../gen/bcm/ghash-ssse3-x86_64-linux.S | 238 +- .../gen/bcm/ghash-ssse3-x86_64-win.asm | 244 +- .../boringssl/gen/bcm/ghash-x86_64-apple.S | 699 ++-- .../boringssl/gen/bcm/ghash-x86_64-linux.S | 694 ++-- .../boringssl/gen/bcm/ghash-x86_64-win.asm | 704 ++-- .../boringssl/gen/bcm/ghashv8-armv8-apple.S | 3 + .../boringssl/gen/bcm/p256-armv8-asm-apple.S | 20 + .../boringssl/gen/bcm/p256-armv8-asm-linux.S | 1 + .../boringssl/gen/bcm/p256-armv8-asm-win.S | 1 + .../boringssl/gen/bcm/p256-x86_64-asm-apple.S | 1235 +++--- .../boringssl/gen/bcm/p256-x86_64-asm-linux.S | 1205 +++--- .../boringssl/gen/bcm/p256-x86_64-asm-win.asm | 1471 +++---- .../gen/bcm/p256_beeu-x86_64-asm-apple.S | 90 +- .../gen/bcm/p256_beeu-x86_64-asm-linux.S | 90 +- .../gen/bcm/p256_beeu-x86_64-asm-win.asm | 90 +- .../boringssl/gen/bcm/rdrand-x86_64-apple.S | 17 +- .../boringssl/gen/bcm/rdrand-x86_64-linux.S | 16 +- .../boringssl/gen/bcm/rdrand-x86_64-win.asm | 16 +- .../boringssl/gen/bcm/rsaz-avx2-apple.S | 215 +- .../boringssl/gen/bcm/rsaz-avx2-linux.S | 210 +- .../boringssl/gen/bcm/rsaz-avx2-win.asm | 334 +- .../boringssl/gen/bcm/sha1-armv8-apple.S | 3 + .../boringssl/gen/bcm/sha1-armv8-linux.S | 2 + .../boringssl/gen/bcm/sha1-armv8-win.S | 2 + .../boringssl/gen/bcm/sha1-x86_64-apple.S | 206 +- .../boringssl/gen/bcm/sha1-x86_64-linux.S | 202 +- .../boringssl/gen/bcm/sha1-x86_64-win.asm | 318 +- .../boringssl/gen/bcm/sha256-armv8-apple.S | 3 + .../boringssl/gen/bcm/sha256-armv8-linux.S | 2 + .../boringssl/gen/bcm/sha256-armv8-win.S | 2 + .../boringssl/gen/bcm/sha256-x86_64-apple.S | 1155 +++--- .../boringssl/gen/bcm/sha256-x86_64-linux.S | 1152 +++--- .../boringssl/gen/bcm/sha256-x86_64-win.asm | 1256 +++--- .../boringssl/gen/bcm/sha512-armv8-apple.S | 3 + .../boringssl/gen/bcm/sha512-armv8-linux.S | 2 + .../boringssl/gen/bcm/sha512-armv8-win.S | 2 + .../boringssl/gen/bcm/sha512-x86_64-apple.S | 1063 ++--- .../boringssl/gen/bcm/sha512-x86_64-linux.S | 1062 ++--- .../boringssl/gen/bcm/sha512-x86_64-win.asm | 1152 +++--- .../boringssl/gen/bcm/vpaes-armv8-apple.S | 26 +- .../boringssl/gen/bcm/vpaes-armv8-linux.S | 10 +- .../boringssl/gen/bcm/vpaes-armv8-win.S | 10 +- .../boringssl/gen/bcm/vpaes-x86_64-apple.S | 1005 ++--- .../boringssl/gen/bcm/vpaes-x86_64-linux.S | 990 ++--- .../boringssl/gen/bcm/vpaes-x86_64-win.asm | 1074 ++--- .../boringssl/gen/bcm/x86_64-mont-apple.S | 593 +-- .../boringssl/gen/bcm/x86_64-mont-linux.S | 590 +-- .../boringssl/gen/bcm/x86_64-mont-win.asm | 704 ++-- .../boringssl/gen/bcm/x86_64-mont5-apple.S | 1948 ++++----- .../boringssl/gen/bcm/x86_64-mont5-linux.S | 1932 ++++----- .../boringssl/gen/bcm/x86_64-mont5-win.asm | 2032 +++++----- .../gen/crypto/aes128gcmsiv-x86_64-apple.S | 370 +- .../gen/crypto/aes128gcmsiv-x86_64-linux.S | 338 +- .../gen/crypto/aes128gcmsiv-x86_64-win.asm | 338 +- .../boringssl/gen/crypto/chacha-armv4-linux.S | 1 + .../boringssl/gen/crypto/chacha-armv8-apple.S | 3 + .../boringssl/gen/crypto/chacha-armv8-linux.S | 1 + .../boringssl/gen/crypto/chacha-armv8-win.S | 1 + .../gen/crypto/chacha-x86_64-apple.S | 409 +- .../gen/crypto/chacha-x86_64-linux.S | 406 +- .../gen/crypto/chacha-x86_64-win.asm | 528 +-- .../crypto/chacha20_poly1305_armv8-apple.S | 4 + .../crypto/chacha20_poly1305_armv8-linux.S | 2 + .../gen/crypto/chacha20_poly1305_armv8-win.S | 2 + .../crypto/chacha20_poly1305_x86_64-apple.S | 567 +-- .../crypto/chacha20_poly1305_x86_64-linux.S | 562 +-- .../crypto/chacha20_poly1305_x86_64-win.asm | 562 +-- .../boringssl/gen/crypto/md5-x86_64-apple.S | 1234 +++--- .../boringssl/gen/crypto/md5-x86_64-linux.S | 1230 +++--- .../boringssl/gen/crypto/md5-x86_64-win.asm | 1292 +++--- .../gen/test_support/trampoline-armv8-apple.S | 69 + .../test_support/trampoline-x86_64-apple.S | 144 +- .../test_support/trampoline-x86_64-linux.S | 106 +- .../test_support/trampoline-x86_64-win.asm | 124 +- third_party/boringssl/sources.cmake | 6 +- .../boringssl/src/crypto/asn1/a_bitstr.cc | 2 +- .../boringssl/src/crypto/asn1/a_gentm.cc | 2 +- .../boringssl/src/crypto/asn1/a_int.cc | 12 +- .../boringssl/src/crypto/asn1/a_mbstr.cc | 4 +- .../boringssl/src/crypto/asn1/a_object.cc | 8 +- .../boringssl/src/crypto/asn1/a_strex.cc | 52 +- .../boringssl/src/crypto/asn1/a_strnid.cc | 82 +- .../boringssl/src/crypto/asn1/a_time.cc | 2 +- .../boringssl/src/crypto/asn1/a_type.cc | 152 +- .../boringssl/src/crypto/asn1/a_utctm.cc | 2 +- .../boringssl/src/crypto/asn1/asn1_lib.cc | 36 +- .../boringssl/src/crypto/asn1/asn1_par.cc | 2 +- .../boringssl/src/crypto/asn1/internal.h | 349 +- .../boringssl/src/crypto/asn1/tasn_dec.cc | 871 ++-- .../boringssl/src/crypto/asn1/tasn_enc.cc | 746 +--- .../boringssl/src/crypto/asn1/tasn_fre.cc | 36 +- .../boringssl/src/crypto/asn1/tasn_new.cc | 74 +- .../boringssl/src/crypto/asn1/tasn_typ.cc | 4 +- .../boringssl/src/crypto/asn1/tasn_utl.cc | 64 +- .../boringssl/src/crypto/base64/base64.cc | 12 +- .../boringssl/src/crypto/bcm_support.h | 8 +- third_party/boringssl/src/crypto/bio/bio.cc | 139 +- .../boringssl/src/crypto/bio/bio_mem.cc | 41 +- .../boringssl/src/crypto/bio/connect.cc | 46 +- third_party/boringssl/src/crypto/bio/fd.cc | 36 +- third_party/boringssl/src/crypto/bio/file.cc | 45 +- .../boringssl/src/crypto/bio/hexdump.cc | 8 +- .../boringssl/src/crypto/bio/internal.h | 39 +- third_party/boringssl/src/crypto/bio/pair.cc | 47 +- .../boringssl/src/crypto/bio/socket.cc | 58 +- .../boringssl/src/crypto/bio/socket_helper.cc | 72 +- .../boringssl/src/crypto/blake2/blake2.cc | 17 +- .../boringssl/src/crypto/bn/bn_asn1.cc | 2 +- .../boringssl/src/crypto/bn/convert.cc | 12 +- third_party/boringssl/src/crypto/bn/div.cc | 24 +- .../boringssl/src/crypto/bn/exponentiation.cc | 2 +- third_party/boringssl/src/crypto/bn/sqrt.cc | 12 +- .../src/crypto/bytestring/asn1_compat.cc | 1 - .../boringssl/src/crypto/bytestring/ber.cc | 24 +- .../boringssl/src/crypto/bytestring/cbb.cc | 156 +- .../boringssl/src/crypto/bytestring/cbs.cc | 107 +- .../src/crypto/bytestring/internal.h | 61 +- .../src/crypto/bytestring/unicode.cc | 4 +- .../boringssl/src/crypto/chacha/chacha.cc | 2 +- .../boringssl/src/crypto/chacha/internal.h | 6 +- .../src/crypto/cipher/e_aesctrhmac.cc | 7 + .../boringssl/src/crypto/cipher/e_aeseax.cc | 6 +- .../src/crypto/cipher/e_aesgcmsiv.cc | 225 +- .../src/crypto/cipher/e_chacha20poly1305.cc | 12 +- .../boringssl/src/crypto/cipher/e_rc2.cc | 49 +- .../boringssl/src/crypto/cipher/e_tls.cc | 31 +- .../boringssl/src/crypto/cipher/get_cipher.cc | 2 +- .../boringssl/src/crypto/cipher/internal.h | 207 +- .../boringssl/src/crypto/cipher/tls_cbc.cc | 74 +- third_party/boringssl/src/crypto/cms/cms.cc | 15 +- third_party/boringssl/src/crypto/conf/conf.cc | 4 +- .../boringssl/src/crypto/conf/internal.h | 8 +- .../boringssl/src/crypto/cpu_arm_freebsd.cc | 2 +- .../boringssl/src/crypto/cpu_arm_linux.cc | 4 +- .../boringssl/src/crypto/cpu_arm_linux.h | 28 +- third_party/boringssl/src/crypto/cpu_intel.cc | 14 +- .../boringssl/src/crypto/curve25519/cpace.cc | 227 ++ .../src/crypto/curve25519/curve25519.cc | 495 ++- .../src/crypto/curve25519/curve25519_tables.h | 498 +-- .../src/crypto/curve25519/internal.h | 71 +- .../src/crypto/curve25519/spake25519.cc | 48 +- .../boringssl/src/crypto/des/internal.h | 4 +- third_party/boringssl/src/crypto/dh/params.cc | 4 +- .../src/crypto/digest/digest_extra.cc | 186 +- third_party/boringssl/src/crypto/dsa/dsa.cc | 45 +- .../boringssl/src/crypto/dsa/dsa_asn1.cc | 16 +- .../boringssl/src/crypto/dsa/internal.h | 2 +- .../boringssl/src/crypto/ec/ec_asn1.cc | 24 +- .../boringssl/src/crypto/ec/ec_derive.cc | 10 +- .../boringssl/src/crypto/ec/hash_to_curve.cc | 282 +- .../boringssl/src/crypto/ec/internal.h | 48 +- .../boringssl/src/crypto/ecdsa/ecdsa_asn1.cc | 4 +- .../boringssl/src/crypto/engine/engine.cc | 2 +- third_party/boringssl/src/crypto/err/err.cc | 48 +- .../boringssl/src/crypto/err/internal.h | 12 +- third_party/boringssl/src/crypto/evp/evp.cc | 30 +- .../boringssl/src/crypto/evp/evp_asn1.cc | 17 +- .../boringssl/src/crypto/evp/evp_ctx.cc | 31 +- .../boringssl/src/crypto/evp/evp_kem.cc | 34 +- .../boringssl/src/crypto/evp/internal.h | 153 +- third_party/boringssl/src/crypto/evp/p_dh.cc | 46 +- third_party/boringssl/src/crypto/evp/p_dsa.cc | 54 +- third_party/boringssl/src/crypto/evp/p_ec.cc | 201 +- .../boringssl/src/crypto/evp/p_ed25519.cc | 62 +- .../boringssl/src/crypto/evp/p_hkdf.cc | 14 +- .../boringssl/src/crypto/evp/p_keccak.cc | 118 + .../boringssl/src/crypto/evp/p_mldsa.cc | 23 +- .../boringssl/src/crypto/evp/p_mlkem.cc | 49 +- third_party/boringssl/src/crypto/evp/p_rsa.cc | 210 +- .../boringssl/src/crypto/evp/p_slhdsa.cc | 537 +++ .../boringssl/src/crypto/evp/p_x25519.cc | 56 +- .../boringssl/src/crypto/evp/p_xwing.cc | 102 +- third_party/boringssl/src/crypto/evp/pbkdf.cc | 7 +- third_party/boringssl/src/crypto/evp/print.cc | 2 +- .../boringssl/src/crypto/evp/scrypt.cc | 40 +- third_party/boringssl/src/crypto/evp/sign.cc | 12 +- third_party/boringssl/src/crypto/ex_data.cc | 18 +- .../src/crypto/fipsmodule/aes/aes.cc.inc | 18 +- .../src/crypto/fipsmodule/aes/aes_nohw.cc.inc | 138 +- .../src/crypto/fipsmodule/aes/cbc.cc.inc | 8 +- .../src/crypto/fipsmodule/aes/gcm.cc.inc | 14 +- .../src/crypto/fipsmodule/aes/gcm_nohw.cc.inc | 18 +- .../src/crypto/fipsmodule/aes/internal.h | 112 +- .../fipsmodule/aes/mode_wrappers.cc.inc | 4 +- .../boringssl/src/crypto/fipsmodule/bcm.cc | 7 +- .../src/crypto/fipsmodule/bcm_interface.h | 224 +- .../src/crypto/fipsmodule/bn/add.cc.inc | 19 +- .../fipsmodule/bn/asm/x86_64-gcc.cc.inc | 8 +- .../src/crypto/fipsmodule/bn/bn.cc.inc | 20 +- .../src/crypto/fipsmodule/bn/bytes.cc.inc | 14 +- .../src/crypto/fipsmodule/bn/cmp.cc.inc | 6 +- .../src/crypto/fipsmodule/bn/ctx.cc.inc | 30 +- .../src/crypto/fipsmodule/bn/div.cc.inc | 82 +- .../src/crypto/fipsmodule/bn/div_extra.cc.inc | 20 +- .../fipsmodule/bn/exponentiation.cc.inc | 188 +- .../src/crypto/fipsmodule/bn/gcd.cc.inc | 14 +- .../src/crypto/fipsmodule/bn/gcd_extra.cc.inc | 44 +- .../src/crypto/fipsmodule/bn/internal.h | 404 +- .../src/crypto/fipsmodule/bn/jacobi.cc.inc | 2 +- .../crypto/fipsmodule/bn/montgomery.cc.inc | 36 +- .../fipsmodule/bn/montgomery_inv.cc.inc | 34 +- .../src/crypto/fipsmodule/bn/mul.cc.inc | 30 +- .../src/crypto/fipsmodule/bn/prime.cc.inc | 66 +- .../src/crypto/fipsmodule/bn/random.cc.inc | 20 +- .../src/crypto/fipsmodule/bn/rsaz_exp.cc.inc | 20 +- .../src/crypto/fipsmodule/bn/rsaz_exp.h | 44 +- .../src/crypto/fipsmodule/bn/shift.cc.inc | 4 +- .../src/crypto/fipsmodule/bn/sqrt.cc.inc | 42 +- .../src/crypto/fipsmodule/cipher/aead.cc.inc | 20 +- .../crypto/fipsmodule/cipher/cipher.cc.inc | 242 +- .../src/crypto/fipsmodule/cipher/e_aes.cc.inc | 17 +- .../crypto/fipsmodule/cipher/e_aesccm.cc.inc | 6 +- .../src/crypto/fipsmodule/cipher/internal.h | 107 +- .../src/crypto/fipsmodule/cmac/cmac.cc.inc | 26 +- .../src/crypto/fipsmodule/delocate.h | 8 +- .../src/crypto/fipsmodule/dh/check.cc.inc | 8 +- .../src/crypto/fipsmodule/dh/dh.cc.inc | 25 +- .../src/crypto/fipsmodule/dh/internal.h | 10 +- .../crypto/fipsmodule/digest/digest.cc.inc | 36 +- .../crypto/fipsmodule/digest/digests.cc.inc | 6 +- .../src/crypto/fipsmodule/digest/internal.h | 20 +- .../crypto/fipsmodule/digest/md32_common.h | 30 +- .../fipsmodule/digestsign/digestsign.cc.inc | 60 +- .../src/crypto/fipsmodule/ec/ec.cc.inc | 126 +- .../src/crypto/fipsmodule/ec/ec_key.cc.inc | 14 +- .../crypto/fipsmodule/ec/ec_montgomery.cc.inc | 36 +- .../src/crypto/fipsmodule/ec/felem.cc.inc | 151 +- .../src/crypto/fipsmodule/ec/internal.h | 361 +- .../src/crypto/fipsmodule/ec/oct.cc.inc | 182 +- .../crypto/fipsmodule/ec/p256-nistz.cc.inc | 58 +- .../src/crypto/fipsmodule/ec/p256-nistz.h | 34 +- .../src/crypto/fipsmodule/ec/p256.cc.inc | 48 +- .../src/crypto/fipsmodule/ec/scalar.cc.inc | 4 +- .../src/crypto/fipsmodule/ec/simple.cc.inc | 63 +- .../crypto/fipsmodule/ec/simple_mul.cc.inc | 18 +- .../src/crypto/fipsmodule/ec/util.cc.inc | 2 +- .../src/crypto/fipsmodule/ec/wnaf.cc.inc | 10 +- .../src/crypto/fipsmodule/ecdsa/ecdsa.cc.inc | 22 +- .../src/crypto/fipsmodule/ecdsa/internal.h | 6 +- .../crypto/fipsmodule/entropy/jitter.cc.inc | 44 +- .../crypto/fipsmodule/entropy/sha512.cc.inc | 4 +- .../crypto/fipsmodule/fips_shared_support.cc | 8 +- .../src/crypto/fipsmodule/hmac/hmac.cc.inc | 6 +- .../src/crypto/fipsmodule/keccak/internal.h | 106 +- .../crypto/fipsmodule/keccak/keccak.cc.inc | 362 +- .../src/crypto/fipsmodule/mldsa/mldsa.cc.inc | 1016 +++-- .../src/crypto/fipsmodule/mlkem/mlkem.cc.inc | 504 ++- .../rand/android_entropy_client.cc.inc | 82 + .../src/crypto/fipsmodule/rand/ctrdrbg.cc.inc | 2 +- .../src/crypto/fipsmodule/rand/internal.h | 28 +- .../src/crypto/fipsmodule/rand/rand.cc.inc | 368 +- .../src/crypto/fipsmodule/rsa/internal.h | 24 +- .../src/crypto/fipsmodule/rsa/padding.cc.inc | 4 +- .../src/crypto/fipsmodule/rsa/rsa.cc.inc | 34 +- .../src/crypto/fipsmodule/rsa/rsa_impl.cc.inc | 132 +- .../crypto/fipsmodule/self_check/fips.cc.inc | 2 +- .../fipsmodule/self_check/self_check.cc.inc | 4 +- .../fipsmodule/service_indicator/internal.h | 20 +- .../service_indicator.cc.inc | 18 +- .../src/crypto/fipsmodule/sha/sha1.cc.inc | 4 +- .../src/crypto/fipsmodule/sha/sha256.cc.inc | 10 +- .../src/crypto/fipsmodule/sha/sha512.cc.inc | 14 +- .../src/crypto/fipsmodule/slhdsa/fors.cc.inc | 37 +- .../src/crypto/fipsmodule/slhdsa/fors.h | 9 +- .../crypto/fipsmodule/slhdsa/merkle.cc.inc | 43 +- .../src/crypto/fipsmodule/slhdsa/merkle.h | 17 +- .../crypto/fipsmodule/slhdsa/slhdsa.cc.inc | 49 +- .../src/crypto/fipsmodule/slhdsa/thash.cc.inc | 114 +- .../src/crypto/fipsmodule/slhdsa/thash.h | 30 +- .../src/crypto/fipsmodule/slhdsa/wots.cc.inc | 48 +- .../src/crypto/fipsmodule/slhdsa/wots.h | 7 +- .../src/crypto/fipsmodule/tls/kdf.cc.inc | 12 +- third_party/boringssl/src/crypto/hpke/hpke.cc | 1565 ++++---- third_party/boringssl/src/crypto/hrss/hrss.cc | 244 +- .../boringssl/src/crypto/hrss/internal.h | 2 +- third_party/boringssl/src/crypto/internal.h | 287 +- .../boringssl/src/crypto/kyber/internal.h | 165 - .../boringssl/src/crypto/kyber/kyber.cc | 870 ---- .../boringssl/src/crypto/lhash/internal.h | 50 +- .../boringssl/src/crypto/lhash/lhash.cc | 42 +- third_party/boringssl/src/crypto/md5/md5.cc | 42 + third_party/boringssl/src/crypto/mem.cc | 16 +- .../boringssl/src/crypto/mem_internal.h | 136 +- third_party/boringssl/src/crypto/obj/obj.cc | 63 +- .../boringssl/src/crypto/obj/obj_dat.h | 90 +- .../boringssl/src/crypto/pem/internal.h | 104 +- .../boringssl/src/crypto/pem/pem_all.cc | 35 +- .../boringssl/src/crypto/pem/pem_info.cc | 34 +- .../boringssl/src/crypto/pem/pem_lib.cc | 207 +- .../boringssl/src/crypto/pem/pem_pk8.cc | 100 +- .../boringssl/src/crypto/pem/pem_pkey.cc | 14 +- .../boringssl/src/crypto/pem/pem_x509.cc | 3 + .../boringssl/src/crypto/pem/pem_xaux.cc | 3 + .../boringssl/src/crypto/pkcs7/internal.h | 25 +- .../boringssl/src/crypto/pkcs7/pkcs7.cc | 20 +- .../boringssl/src/crypto/pkcs7/pkcs7_x509.cc | 48 +- .../boringssl/src/crypto/pkcs8/internal.h | 30 +- .../boringssl/src/crypto/pkcs8/p5_pbev2.cc | 41 +- .../boringssl/src/crypto/pkcs8/pkcs8.cc | 41 +- .../boringssl/src/crypto/pkcs8/pkcs8_x509.cc | 74 +- .../boringssl/src/crypto/poly1305/internal.h | 17 + .../boringssl/src/crypto/poly1305/poly1305.cc | 20 +- .../src/crypto/poly1305/poly1305_arm.cc | 28 +- .../src/crypto/poly1305/poly1305_vec.cc | 14 +- .../boringssl/src/crypto/pool/internal.h | 6 +- third_party/boringssl/src/crypto/pool/pool.cc | 32 +- .../src/crypto/rand/deterministic.cc | 12 +- .../boringssl/src/crypto/rand/fork_detect.cc | 50 +- .../boringssl/src/crypto/rand/forkunsafe.cc | 6 +- .../boringssl/src/crypto/rand/getentropy.cc | 4 +- .../boringssl/src/crypto/rand/passive.cc | 180 - third_party/boringssl/src/crypto/rand/rand.cc | 6 +- .../boringssl/src/crypto/rand/urandom.cc | 26 +- .../boringssl/src/crypto/rand/windows.cc | 10 +- .../boringssl/src/crypto/rsa/internal.h | 12 +- .../boringssl/src/crypto/rsa/rsa_asn1.cc | 78 +- .../boringssl/src/crypto/rsa/rsa_crypt.cc | 39 +- .../boringssl/src/crypto/rsa/rsa_extra.cc | 4 +- .../boringssl/src/crypto/rsa/rsa_print.cc | 3 +- third_party/boringssl/src/crypto/sha/sha1.cc | 9 - .../boringssl/src/crypto/sha/sha256.cc | 20 - .../boringssl/src/crypto/sha/sha512.cc | 30 - .../boringssl/src/crypto/slhdsa/slhdsa.cc | 29 +- .../src/crypto/spake2plus/internal.h | 60 +- .../boringssl/src/crypto/stack/stack.cc | 35 +- .../boringssl/src/crypto/thread_win.cc | 14 +- .../src/crypto/trust_token/internal.h | 76 +- .../src/crypto/trust_token/pmbtoken.cc | 16 +- .../src/crypto/trust_token/trust_token.cc | 12 +- .../boringssl/src/crypto/trust_token/voprf.cc | 6 +- .../boringssl/src/crypto/x509/a_sign.cc | 8 +- .../boringssl/src/crypto/x509/a_verify.cc | 26 +- .../boringssl/src/crypto/x509/algorithm.cc | 12 +- .../boringssl/src/crypto/x509/asn1_gen.cc | 127 +- .../boringssl/src/crypto/x509/by_dir.cc | 412 +- .../boringssl/src/crypto/x509/by_file.cc | 26 +- .../boringssl/src/crypto/x509/internal.h | 422 +- .../boringssl/src/crypto/x509/name_print.cc | 19 +- .../boringssl/src/crypto/x509/policy.cc | 110 +- .../boringssl/src/crypto/x509/rsa_pss.cc | 39 +- .../boringssl/src/crypto/x509/t_crl.cc | 4 +- .../boringssl/src/crypto/x509/t_req.cc | 6 +- .../boringssl/src/crypto/x509/t_x509.cc | 13 +- .../boringssl/src/crypto/x509/v3_akey.cc | 157 +- .../boringssl/src/crypto/x509/v3_alt.cc | 754 ++-- .../boringssl/src/crypto/x509/v3_bcons.cc | 55 +- .../boringssl/src/crypto/x509/v3_bitst.cc | 40 +- .../boringssl/src/crypto/x509/v3_conf.cc | 198 +- .../boringssl/src/crypto/x509/v3_cpols.cc | 479 ++- .../boringssl/src/crypto/x509/v3_crld.cc | 433 +- .../boringssl/src/crypto/x509/v3_enum.cc | 9 +- .../boringssl/src/crypto/x509/v3_extku.cc | 55 +- .../boringssl/src/crypto/x509/v3_genn.cc | 47 +- .../boringssl/src/crypto/x509/v3_ia5.cc | 10 +- .../boringssl/src/crypto/x509/v3_info.cc | 89 +- .../boringssl/src/crypto/x509/v3_int.cc | 10 +- .../boringssl/src/crypto/x509/v3_lib.cc | 8 +- .../boringssl/src/crypto/x509/v3_ncons.cc | 14 +- .../boringssl/src/crypto/x509/v3_ocsp.cc | 56 +- .../boringssl/src/crypto/x509/v3_pcons.cc | 54 +- .../boringssl/src/crypto/x509/v3_pmaps.cc | 55 +- .../boringssl/src/crypto/x509/v3_prn.cc | 61 +- .../boringssl/src/crypto/x509/v3_purp.cc | 366 +- .../boringssl/src/crypto/x509/v3_skey.cc | 73 +- .../boringssl/src/crypto/x509/v3_utl.cc | 1758 ++++----- third_party/boringssl/src/crypto/x509/x509.cc | 2 +- .../boringssl/src/crypto/x509/x509_att.cc | 14 +- .../boringssl/src/crypto/x509/x509_cmp.cc | 28 +- .../boringssl/src/crypto/x509/x509_ext.cc | 24 +- .../boringssl/src/crypto/x509/x509_lu.cc | 401 +- .../boringssl/src/crypto/x509/x509_mtc.cc | 632 +++ .../boringssl/src/crypto/x509/x509_obj.cc | 17 +- .../boringssl/src/crypto/x509/x509_req.cc | 30 +- .../boringssl/src/crypto/x509/x509_set.cc | 54 +- .../boringssl/src/crypto/x509/x509_trs.cc | 99 +- .../boringssl/src/crypto/x509/x509_vfy.cc | 1483 +++---- .../boringssl/src/crypto/x509/x509_vpm.cc | 231 +- .../boringssl/src/crypto/x509/x509cset.cc | 9 + .../boringssl/src/crypto/x509/x509name.cc | 62 +- .../boringssl/src/crypto/x509/x509rset.cc | 4 +- .../boringssl/src/crypto/x509/x509spki.cc | 4 +- .../boringssl/src/crypto/x509/x_algor.cc | 17 +- .../boringssl/src/crypto/x509/x_all.cc | 36 +- .../boringssl/src/crypto/x509/x_crl.cc | 205 +- .../boringssl/src/crypto/x509/x_name.cc | 318 +- .../boringssl/src/crypto/x509/x_pubkey.cc | 115 +- .../boringssl/src/crypto/x509/x_req.cc | 12 +- .../boringssl/src/crypto/x509/x_x509.cc | 278 +- .../boringssl/src/crypto/x509/x_x509a.cc | 10 +- .../boringssl/src/gen/crypto/err_data.cc | 1008 ++--- .../boringssl/src/include/openssl/aead.h | 371 +- .../boringssl/src/include/openssl/aes.h | 86 +- .../boringssl/src/include/openssl/asm_base.h | 34 +- .../boringssl/src/include/openssl/asn1.h | 1244 +++--- .../boringssl/src/include/openssl/asn1t.h | 4 +- .../boringssl/src/include/openssl/base.h | 95 +- .../boringssl/src/include/openssl/base64.h | 60 +- .../boringssl/src/include/openssl/bio.h | 634 +-- .../boringssl/src/include/openssl/blake2.h | 16 +- .../boringssl/src/include/openssl/bn.h | 598 +-- .../boringssl/src/include/openssl/buf.h | 28 +- .../src/include/openssl/bytestring.h | 502 +-- .../boringssl/src/include/openssl/cast.h | 32 +- .../boringssl/src/include/openssl/chacha.h | 6 +- .../boringssl/src/include/openssl/cipher.h | 406 +- .../boringssl/src/include/openssl/cmac.h | 26 +- .../boringssl/src/include/openssl/cms.h | 78 +- .../boringssl/src/include/openssl/conf.h | 24 +- .../src/include/openssl/configuration.h | 18 + .../boringssl/src/include/openssl/crypto.h | 18 +- .../boringssl/src/include/openssl/ctrdrbg.h | 24 +- .../src/include/openssl/curve25519.h | 166 +- .../boringssl/src/include/openssl/des.h | 44 +- .../boringssl/src/include/openssl/dh.h | 167 +- .../boringssl/src/include/openssl/digest.h | 238 +- .../boringssl/src/include/openssl/dsa.h | 209 +- .../boringssl/src/include/openssl/ec.h | 276 +- .../boringssl/src/include/openssl/ec_key.h | 205 +- .../boringssl/src/include/openssl/ecdh.h | 18 +- .../boringssl/src/include/openssl/ecdsa.h | 100 +- .../boringssl/src/include/openssl/engine.h | 10 +- .../boringssl/src/include/openssl/err.h | 104 +- .../boringssl/src/include/openssl/evp.h | 1026 ++--- .../src/include/openssl/evp_errors.h | 1 + .../boringssl/src/include/openssl/ex_data.h | 34 +- .../boringssl/src/include/openssl/hkdf.h | 12 +- .../boringssl/src/include/openssl/hmac.h | 64 +- .../boringssl/src/include/openssl/hpke.h | 273 +- .../boringssl/src/include/openssl/hrss.h | 16 +- .../boringssl/src/include/openssl/kdf.h | 36 +- .../boringssl/src/include/openssl/md4.h | 14 +- .../boringssl/src/include/openssl/md5.h | 34 +- .../boringssl/src/include/openssl/mem.h | 84 +- .../boringssl/src/include/openssl/mldsa.h | 222 +- .../boringssl/src/include/openssl/mlkem.h | 84 +- .../boringssl/src/include/openssl/nid.h | 41 + .../boringssl/src/include/openssl/obj.h | 98 +- .../src/include/openssl/opensslconf.h | 4 +- .../boringssl/src/include/openssl/params.h | 2 +- .../boringssl/src/include/openssl/pem.h | 1020 +++-- .../boringssl/src/include/openssl/pkcs7.h | 90 +- .../boringssl/src/include/openssl/pkcs8.h | 124 +- .../boringssl/src/include/openssl/poly1305.h | 8 +- .../boringssl/src/include/openssl/pool.h | 82 +- .../src/include/openssl/posix_time.h | 16 +- .../src/include/openssl/prefix_symbols.h | 220 +- .../boringssl/src/include/openssl/rand.h | 47 +- .../boringssl/src/include/openssl/rc4.h | 8 +- .../boringssl/src/include/openssl/ripemd.h | 18 +- .../boringssl/src/include/openssl/rsa.h | 495 +-- .../boringssl/src/include/openssl/sha.h | 68 +- .../boringssl/src/include/openssl/sha2.h | 252 +- .../boringssl/src/include/openssl/slhdsa.h | 136 +- .../boringssl/src/include/openssl/span.h | 12 +- .../boringssl/src/include/openssl/stack.h | 171 +- .../boringssl/src/include/openssl/target.h | 10 + .../boringssl/src/include/openssl/tls_prf.h | 8 +- .../src/include/openssl/trust_token.h | 182 +- .../src/include/openssl/type_check.h | 2 +- .../boringssl/src/include/openssl/types.h | 18 + .../boringssl/src/include/openssl/x509.h | 3512 +++++++++-------- .../boringssl/src/include/openssl/x509v3.h | 2 +- .../boringssl/src/include/openssl/xwing.h | 28 +- .../fiat/bedrock_unverified_platform.c.inc | 10 + .../boringssl/src/third_party/fiat/p256_64.h | 16 +- tool/REVISION | 2 +- 492 files changed, 45671 insertions(+), 41831 deletions(-) create mode 100644 third_party/boringssl/src/crypto/curve25519/cpace.cc create mode 100644 third_party/boringssl/src/crypto/evp/p_keccak.cc create mode 100644 third_party/boringssl/src/crypto/evp/p_slhdsa.cc create mode 100644 third_party/boringssl/src/crypto/fipsmodule/rand/android_entropy_client.cc.inc delete mode 100644 third_party/boringssl/src/crypto/kyber/internal.h delete mode 100644 third_party/boringssl/src/crypto/kyber/kyber.cc delete mode 100644 third_party/boringssl/src/crypto/rand/passive.cc create mode 100644 third_party/boringssl/src/crypto/x509/x509_mtc.cc create mode 100644 third_party/boringssl/src/include/openssl/configuration.h create mode 100644 third_party/boringssl/src/include/openssl/types.h diff --git a/lib/src/third_party/boringssl/generated_bindings.dart b/lib/src/third_party/boringssl/generated_bindings.dart index cc237037b..553b68a4d 100644 --- a/lib/src/third_party/boringssl/generated_bindings.dart +++ b/lib/src/third_party/boringssl/generated_bindings.dart @@ -61,8 +61,8 @@ // ignore_for_file: type=lint import 'dart:ffi' as ffi; -/// BN_add sets |r| = |a| + |b|, where |r| may be the same pointer as either |a| -/// or |b|. It returns one on success and zero on allocation failure. +/// BN_add sets `r` = `a` + `b`, where `r` may be the same pointer as either `a` +/// or `b`. It returns one on success and zero on allocation failure. @ffi.Native< ffi.Int Function( ffi.Pointer, @@ -76,9 +76,9 @@ external int BN_add( ffi.Pointer b, ); -/// BN_bin2bn sets |*ret| to the value of |len| bytes from |in|, interpreted as -/// a big-endian number, and returns |ret|. If |ret| is NULL then a fresh -/// |BIGNUM| is allocated and returned. It returns NULL on allocation +/// BN_bin2bn sets `*ret` to the value of `len` bytes from `in`, interpreted as +/// a big-endian number, and returns `ret`. If `ret` is NULL then a fresh +/// `BIGNUM` is allocated and returned. It returns NULL on allocation /// failure. @ffi.Native< ffi.Pointer Function( @@ -93,9 +93,9 @@ external ffi.Pointer BN_bin2bn( ffi.Pointer ret, ); -/// BN_bn2bin_padded serialises the absolute value of |in| to |out| as a +/// BN_bn2bin_padded serialises the absolute value of `in` to `out` as a /// big-endian integer. The integer is padded with leading zeros up to size -/// |len|. If |len| is smaller than |BN_num_bytes|, the function fails and +/// `len`. If `len` is smaller than `BN_num_bytes`, the function fails and /// returns 0. Otherwise, it returns 1. @ffi.Native< ffi.Int Function(ffi.Pointer, ffi.Size, ffi.Pointer) @@ -109,24 +109,24 @@ external int BN_bn2bin_padded( ffi.Pointer in1, ); -/// BN_cmp returns a value less than, equal to or greater than zero if |a| is -/// less than, equal to or greater than |b|, respectively. +/// BN_cmp returns a value less than, equal to or greater than zero if `a` is +/// less than, equal to or greater than `b`, respectively. @ffi.Native, ffi.Pointer)>( symbol: 'webcrypto_BN_cmp', assetId: 'package:webcrypto/webcrypto.dart', ) external int BN_cmp(ffi.Pointer a, ffi.Pointer b); -/// BN_free frees the data referenced by |bn| and, if |bn| was originally -/// allocated on the heap, frees |bn| also. +/// BN_free frees the data referenced by `bn` and, if `bn` was originally +/// allocated on the heap, frees `bn` also. @ffi.Native)>( symbol: 'webcrypto_BN_free', assetId: 'package:webcrypto/webcrypto.dart', ) external void BN_free(ffi.Pointer bn); -/// BN_lshift sets |r| equal to |a| << n. The |a| and |r| arguments may be the -/// same |BIGNUM|. It returns one on success and zero on allocation failure. +/// BN_lshift sets `r` equal to `a` << n. The `a` and `r` arguments may be the +/// same `BIGNUM`. It returns one on success and zero on allocation failure. @ffi.Native< ffi.Int Function(ffi.Pointer, ffi.Pointer, ffi.Int) >(symbol: 'webcrypto_BN_lshift', assetId: 'package:webcrypto/webcrypto.dart') @@ -140,18 +140,18 @@ external int BN_lshift(ffi.Pointer r, ffi.Pointer a, int n); external ffi.Pointer BN_new(); /// BN_num_bytes returns the minimum number of bytes needed to represent the -/// absolute value of |bn|. +/// absolute value of `bn`. /// -/// While |size_t| is the preferred type for byte counts, callers can assume that -/// |BIGNUM|s are bounded such that this value, and its corresponding bit count, -/// will always fit in |int|. +/// While `size_t` is the preferred type for byte counts, callers can assume that +/// `BIGNUM`s are bounded such that this value, and its corresponding bit count, +/// will always fit in `int`. @ffi.Native)>( symbol: 'webcrypto_BN_num_bytes', assetId: 'package:webcrypto/webcrypto.dart', ) external int BN_num_bytes(ffi.Pointer bn); -/// BN_set_word sets |bn| to |value|. It returns one on success or zero on +/// BN_set_word sets `bn` to `value`. It returns one on success or zero on /// allocation failure. @ffi.Native, BN_ULONG)>( symbol: 'webcrypto_BN_set_word', @@ -159,8 +159,8 @@ external int BN_num_bytes(ffi.Pointer bn); ) external int BN_set_word(ffi.Pointer bn, int value); -/// BN_sub sets |r| = |a| - |b|, where |r| may be the same pointer as either |a| -/// or |b|. It returns one on success and zero on allocation failure. +/// BN_sub sets `r` = `a` - `b`, where `r` may be the same pointer as either `a` +/// or `b`. It returns one on success and zero on allocation failure. @ffi.Native< ffi.Int Function( ffi.Pointer, @@ -190,21 +190,21 @@ external ffi.Pointer BN_value_one(); ) external int BORINGSSL_self_test(); -/// CBB_cleanup frees all resources owned by |cbb| and other |CBB| objects +/// CBB_cleanup frees all resources owned by `cbb` and other `CBB` objects /// writing to the same buffer. This should be used in an error case where a /// serialisation is abandoned. /// -/// This function can only be called on a "top level" |CBB|, i.e. one initialised -/// with |CBB_init| or |CBB_init_fixed|, or a |CBB| set to the zero state with -/// |CBB_zero|. +/// This function can only be called on a "top level" `CBB`, i.e. one initialised +/// with `CBB_init` or `CBB_init_fixed`, or a `CBB` set to the zero state with +/// `CBB_zero`. @ffi.Native)>( symbol: 'webcrypto_CBB_cleanup', assetId: 'package:webcrypto/webcrypto.dart', ) external void CBB_cleanup(ffi.Pointer cbb); -/// CBB_data returns a pointer to the bytes written to |cbb|. It does not flush -/// |cbb|. The pointer is valid until the next operation to |cbb|. +/// CBB_data returns a pointer to the bytes written to `cbb`. It does not flush +/// `cbb`. The pointer is valid until the next operation to `cbb`. /// /// To avoid unfinalized length prefixes, it is a fatal error to call this on a /// CBB with any active children. @@ -215,9 +215,9 @@ external void CBB_cleanup(ffi.Pointer cbb); external ffi.Pointer CBB_data(ffi.Pointer cbb); /// CBB_flush causes any pending length prefixes to be written out and any child -/// |CBB| objects of |cbb| to be invalidated. This allows |cbb| to continue to be -/// used after the children go out of scope, e.g. when local |CBB| objects are -/// added as children to a |CBB| that persists after a function returns. This +/// `CBB` objects of `cbb` to be invalidated. This allows `cbb` to continue to be +/// used after the children go out of scope, e.g. when local `CBB` objects are +/// added as children to a `CBB` that persists after a function returns. This /// function returns one on success or zero on error. @ffi.Native)>( symbol: 'webcrypto_CBB_flush', @@ -225,8 +225,8 @@ external ffi.Pointer CBB_data(ffi.Pointer cbb); ) external int CBB_flush(ffi.Pointer cbb); -/// CBB_init initialises |cbb| with |initial_capacity|. Since a |CBB| grows as -/// needed, the |initial_capacity| is just a hint. It returns one on success or +/// CBB_init initialises `cbb` with `initial_capacity`. Since a `CBB` grows as +/// needed, the `initial_capacity` is just a hint. It returns one on success or /// zero on allocation failure. @ffi.Native, ffi.Size)>( symbol: 'webcrypto_CBB_init', @@ -234,8 +234,8 @@ external int CBB_flush(ffi.Pointer cbb); ) external int CBB_init(ffi.Pointer cbb, int initial_capacity); -/// CBB_len returns the number of bytes written to |cbb|. It does not flush -/// |cbb|. +/// CBB_len returns the number of bytes written to `cbb`. It does not flush +/// `cbb`. /// /// To avoid unfinalized length prefixes, it is a fatal error to call this on a /// CBB with any active children. @@ -245,19 +245,19 @@ external int CBB_init(ffi.Pointer cbb, int initial_capacity); ) external int CBB_len(ffi.Pointer cbb); -/// CBB_zero sets an uninitialised |cbb| to the zero state. It must be -/// initialised with |CBB_init| or |CBB_init_fixed| before use, but it is safe to -/// call |CBB_cleanup| without a successful |CBB_init|. This may be used for more -/// uniform cleanup of a |CBB|. +/// CBB_zero sets an uninitialised `cbb` to the zero state. It must be +/// initialised with `CBB_init` or `CBB_init_fixed` before use, but it is safe to +/// call `CBB_cleanup` without a successful `CBB_init`. This may be used for more +/// uniform cleanup of a `CBB`. @ffi.Native)>( symbol: 'webcrypto_CBB_zero', assetId: 'package:webcrypto/webcrypto.dart', ) external void CBB_zero(ffi.Pointer cbb); -/// CRYPTO_memcmp returns zero iff the |len| bytes at |a| and |b| are equal. It -/// takes an amount of time dependent on |len|, but independent of the contents -/// of |a| and |b|. Unlike memcmp, it cannot be used to put elements into a +/// CRYPTO_memcmp returns zero iff the `len` bytes at `a` and `b` are equal. It +/// takes an amount of time dependent on `len`, but independent of the contents +/// of `a` and `b`. Unlike memcmp, it cannot be used to put elements into a /// defined order as the return value when a != b is undefined, other than to be /// non-zero. @ffi.Native< @@ -272,12 +272,12 @@ external int CRYPTO_memcmp( int len, ); -/// ECDH_compute_key calculates the shared key between |pub_key| and |priv_key|. -/// If |kdf| is not NULL, then it is called with the bytes of the shared key and -/// the parameter |out|. When |kdf| returns, the value of |*outlen| becomes the +/// ECDH_compute_key calculates the shared key between `pub_key` and `priv_key`. +/// If `kdf` is not NULL, then it is called with the bytes of the shared key and +/// the parameter `out`. When `kdf` returns, the value of `*outlen` becomes the /// return value. Otherwise, as many bytes of the shared key as will fit are -/// copied directly to, at most, |outlen| bytes at |out|. It returns the number -/// of bytes written to |out|, or -1 on error. +/// copied directly to, at most, `outlen` bytes at `out`. It returns the number +/// of bytes written to `out`, or -1 on error. @ffi.Native< ffi.Int Function( ffi.Pointer, @@ -317,15 +317,15 @@ external int ECDH_compute_key( kdf, ); -/// ECDSA_SIG_free frees |sig| its member |BIGNUM|s. +/// ECDSA_SIG_free frees `sig` its member `BIGNUM`s. @ffi.Native)>( symbol: 'webcrypto_ECDSA_SIG_free', assetId: 'package:webcrypto/webcrypto.dart', ) external void ECDSA_SIG_free(ffi.Pointer sig); -/// ECDSA_SIG_get0 sets |*out_r| and |*out_s|, if non-NULL, to the two -/// components of |sig|. +/// ECDSA_SIG_get0 sets `*out_r` and `*out_s`, if non-NULL, to the two +/// components of `sig`. @ffi.Native< ffi.Void Function( ffi.Pointer, @@ -342,8 +342,8 @@ external void ECDSA_SIG_get0( ffi.Pointer> out_s, ); -/// ECDSA_SIG_marshal marshals |sig| as a DER-encoded ECDSA-Sig-Value and appends -/// the result to |cbb|. It returns one on success and zero on error. +/// ECDSA_SIG_marshal marshals `sig` as a DER-encoded ECDSA-Sig-Value and appends +/// the result to `cbb`. It returns one on success and zero on error. @ffi.Native, ffi.Pointer)>( symbol: 'webcrypto_ECDSA_SIG_marshal', assetId: 'package:webcrypto/webcrypto.dart', @@ -353,26 +353,26 @@ external int ECDSA_SIG_marshal( ffi.Pointer sig, ); -/// ECDSA_SIG_new returns a fresh |ECDSA_SIG| structure or NULL on error. +/// ECDSA_SIG_new returns a fresh `ECDSA_SIG` structure or NULL on error. @ffi.Native Function()>( symbol: 'webcrypto_ECDSA_SIG_new', assetId: 'package:webcrypto/webcrypto.dart', ) external ffi.Pointer ECDSA_SIG_new(); -/// ECDSA_SIG_parse parses a DER-encoded ECDSA-Sig-Value structure from |cbs| and -/// advances |cbs|. It returns a newly-allocated |ECDSA_SIG| or NULL on error. +/// ECDSA_SIG_parse parses a DER-encoded ECDSA-Sig-Value structure from `cbs` and +/// advances `cbs`. It returns a newly-allocated `ECDSA_SIG` or NULL on error. @ffi.Native Function(ffi.Pointer)>( symbol: 'webcrypto_ECDSA_SIG_parse', assetId: 'package:webcrypto/webcrypto.dart', ) external ffi.Pointer ECDSA_SIG_parse(ffi.Pointer cbs); -/// EC_GROUP_free releases a reference to |group|, if |group| was created by -/// |EC_GROUP_new_curve_GFp|. If |group| is static, it does nothing. +/// EC_GROUP_free releases a reference to `group`, if `group` was created by +/// `EC_GROUP_new_curve_GFp`. If `group` is static, it does nothing. /// /// This function exists for OpenSSL compatibility, and to manage dynamic -/// |EC_GROUP|s constructed by |EC_GROUP_new_curve_GFp|. Callers that do not need +/// `EC_GROUP`s constructed by `EC_GROUP_new_curve_GFp`. Callers that do not need /// either may ignore this function. @ffi.Native)>( symbol: 'webcrypto_EC_GROUP_free', @@ -380,15 +380,15 @@ external ffi.Pointer ECDSA_SIG_parse(ffi.Pointer cbs); ) external void EC_GROUP_free(ffi.Pointer group); -/// EC_GROUP_get0_order returns a pointer to the internal |BIGNUM| object in -/// |group| that specifies the order of the group. +/// EC_GROUP_get0_order returns a pointer to the internal `BIGNUM` object in +/// `group` that specifies the order of the group. @ffi.Native Function(ffi.Pointer)>( symbol: 'webcrypto_EC_GROUP_get0_order', assetId: 'package:webcrypto/webcrypto.dart', ) external ffi.Pointer EC_GROUP_get0_order(ffi.Pointer group); -/// EC_GROUP_get_curve_name returns a NID that identifies |group|. +/// EC_GROUP_get_curve_name returns a NID that identifies `group`. @ffi.Native)>( symbol: 'webcrypto_EC_GROUP_get_curve_name', assetId: 'package:webcrypto/webcrypto.dart', @@ -396,29 +396,29 @@ external ffi.Pointer EC_GROUP_get0_order(ffi.Pointer group); external int EC_GROUP_get_curve_name(ffi.Pointer group); /// EC_GROUP_get_degree returns the number of bits needed to represent an -/// element of the field underlying |group|. +/// element of the field underlying `group`. @ffi.Native)>( symbol: 'webcrypto_EC_GROUP_get_degree', assetId: 'package:webcrypto/webcrypto.dart', ) external int EC_GROUP_get_degree(ffi.Pointer group); -/// EC_GROUP_new_by_curve_name returns the |EC_GROUP| object for the elliptic -/// curve specified by |nid|, or NULL on unsupported NID. For OpenSSL +/// EC_GROUP_new_by_curve_name returns the `EC_GROUP` object for the elliptic +/// curve specified by `nid`, or NULL on unsupported NID. For OpenSSL /// compatibility, this function returns a non-const pointer which may be passed -/// to |EC_GROUP_free|. However, the resulting object is actually static and -/// calling |EC_GROUP_free| is optional. +/// to `EC_GROUP_free`. However, the resulting object is actually static and +/// calling `EC_GROUP_free` is optional. /// /// The supported NIDs are: -/// - |NID_secp224r1| (P-224) -/// - |NID_X9_62_prime256v1| (P-256) -/// - |NID_secp384r1| (P-384) -/// - |NID_secp521r1| (P-521) +/// - `NID_secp224r1` (P-224) +/// - `NID_X9_62_prime256v1` (P-256) +/// - `NID_secp384r1` (P-384) +/// - `NID_secp521r1` (P-521) /// /// Calling this function causes all four curves to be linked into the binary. -/// Prefer calling |EC_group_*| to allow the static linker to drop unused curves. +/// Prefer calling `EC_group_*` to allow the static linker to drop unused curves. /// -/// If in doubt, use |NID_X9_62_prime256v1|, or see the curve25519.h header for +/// If in doubt, use `NID_X9_62_prime256v1`, or see the curve25519.h header for /// more modern primitives. @ffi.Native Function(ffi.Int)>( symbol: 'webcrypto_EC_GROUP_new_by_curve_name', @@ -426,7 +426,7 @@ external int EC_GROUP_get_degree(ffi.Pointer group); ) external ffi.Pointer EC_GROUP_new_by_curve_name(int nid); -/// EC_KEY_check_key performs several checks on |key| (possibly including an +/// EC_KEY_check_key performs several checks on `key` (possibly including an /// expensive check that the public key is in the primary subgroup). It returns /// one if all checks pass and zero otherwise. If it returns zero then detail /// about the problem can be found on the error stack. @@ -436,7 +436,7 @@ external ffi.Pointer EC_GROUP_new_by_curve_name(int nid); ) external int EC_KEY_check_key(ffi.Pointer key); -/// EC_KEY_free frees all the data owned by |key| and |key| itself. +/// EC_KEY_free frees all the data owned by `key` and `key` itself. @ffi.Native)>( symbol: 'webcrypto_EC_KEY_free', assetId: 'package:webcrypto/webcrypto.dart', @@ -444,7 +444,7 @@ external int EC_KEY_check_key(ffi.Pointer key); external void EC_KEY_free(ffi.Pointer key); /// EC_KEY_generate_key generates a random, private key, calculates the -/// corresponding public key and stores both in |key|. It returns one on success +/// corresponding public key and stores both in `key`. It returns one on success /// or zero otherwise. @ffi.Native)>( symbol: 'webcrypto_EC_KEY_generate_key', @@ -452,14 +452,14 @@ external void EC_KEY_free(ffi.Pointer key); ) external int EC_KEY_generate_key(ffi.Pointer key); -/// EC_KEY_get0_group returns a pointer to the |EC_GROUP| object inside |key|. +/// EC_KEY_get0_group returns a pointer to the `EC_GROUP` object inside `key`. @ffi.Native Function(ffi.Pointer)>( symbol: 'webcrypto_EC_KEY_get0_group', assetId: 'package:webcrypto/webcrypto.dart', ) external ffi.Pointer EC_KEY_get0_group(ffi.Pointer key); -/// EC_KEY_get0_private_key returns a pointer to the private key inside |key|. +/// EC_KEY_get0_private_key returns a pointer to the private key inside `key`. @ffi.Native Function(ffi.Pointer)>( symbol: 'webcrypto_EC_KEY_get0_private_key', assetId: 'package:webcrypto/webcrypto.dart', @@ -467,22 +467,22 @@ external ffi.Pointer EC_KEY_get0_group(ffi.Pointer key); external ffi.Pointer EC_KEY_get0_private_key(ffi.Pointer key); /// EC_KEY_get0_public_key returns a pointer to the public key point inside -/// |key|. +/// `key`. @ffi.Native Function(ffi.Pointer)>( symbol: 'webcrypto_EC_KEY_get0_public_key', assetId: 'package:webcrypto/webcrypto.dart', ) external ffi.Pointer EC_KEY_get0_public_key(ffi.Pointer key); -/// EC_KEY_get_enc_flags returns the encoding flags for |key|, which is a -/// bitwise-OR of |EC_PKEY_*| values. +/// EC_KEY_get_enc_flags returns the encoding flags for `key`, which is a +/// bitwise-OR of `EC_PKEY_*` values. @ffi.Native)>( symbol: 'webcrypto_EC_KEY_get_enc_flags', assetId: 'package:webcrypto/webcrypto.dart', ) external int EC_KEY_get_enc_flags(ffi.Pointer key); -/// EC_KEY_new_by_curve_name returns a fresh EC_KEY for group specified by |nid| +/// EC_KEY_new_by_curve_name returns a fresh EC_KEY for group specified by `nid` /// or NULL on error. @ffi.Native Function(ffi.Int)>( symbol: 'webcrypto_EC_KEY_new_by_curve_name', @@ -490,17 +490,17 @@ external int EC_KEY_get_enc_flags(ffi.Pointer key); ) external ffi.Pointer EC_KEY_new_by_curve_name(int nid); -/// EC_KEY_set_enc_flags sets the encoding flags for |key|, which is a -/// bitwise-OR of |EC_PKEY_*| values. +/// EC_KEY_set_enc_flags sets the encoding flags for `key`, which is a +/// bitwise-OR of `EC_PKEY_*` values. @ffi.Native, ffi.UnsignedInt)>( symbol: 'webcrypto_EC_KEY_set_enc_flags', assetId: 'package:webcrypto/webcrypto.dart', ) external void EC_KEY_set_enc_flags(ffi.Pointer key, int flags); -/// EC_KEY_set_private_key sets the private key of |key| to |priv|. It returns -/// one on success and zero otherwise. |key| must already have had a group -/// configured (see |EC_KEY_set_group| and |EC_KEY_new_by_curve_name|). +/// EC_KEY_set_private_key sets the private key of `key` to `priv`. It returns +/// one on success and zero otherwise. `key` must already have had a group +/// configured (see `EC_KEY_set_group` and `EC_KEY_new_by_curve_name`). @ffi.Native, ffi.Pointer)>( symbol: 'webcrypto_EC_KEY_set_private_key', assetId: 'package:webcrypto/webcrypto.dart', @@ -510,10 +510,10 @@ external int EC_KEY_set_private_key( ffi.Pointer priv, ); -/// EC_KEY_set_public_key sets the public key of |key| to |pub|, by copying it. -/// It returns one on success and zero otherwise. |key| must already have had a -/// group configured (see |EC_KEY_set_group| and |EC_KEY_new_by_curve_name|), and -/// |pub| must also belong to that group, and must not be the point at infinity. +/// EC_KEY_set_public_key sets the public key of `key` to `pub`, by copying it. +/// It returns one on success and zero otherwise. `key` must already have had a +/// group configured (see `EC_KEY_set_group` and `EC_KEY_new_by_curve_name`), and +/// `pub` must also belong to that group, and must not be the point at infinity. @ffi.Native, ffi.Pointer)>( symbol: 'webcrypto_EC_KEY_set_public_key', assetId: 'package:webcrypto/webcrypto.dart', @@ -523,9 +523,9 @@ external int EC_KEY_set_public_key( ffi.Pointer pub, ); -/// EC_KEY_set_public_key_affine_coordinates sets the public key in |key| to -/// (|x|, |y|). It returns one on success and zero on error. It's considered an -/// error if |x| and |y| do not represent a point on |key|'s curve. +/// EC_KEY_set_public_key_affine_coordinates sets the public key in `key` to +/// (`x`, `y`). It returns one on success and zero on error. It's considered an +/// error if `x` and `y` do not represent a point on `key`'s curve. @ffi.Native< ffi.Int Function( ffi.Pointer, @@ -542,18 +542,18 @@ external int EC_KEY_set_public_key_affine_coordinates( ffi.Pointer y, ); -/// EC_POINT_free frees |point| and the data that it points to. +/// EC_POINT_free frees `point` and the data that it points to. @ffi.Native)>( symbol: 'webcrypto_EC_POINT_free', assetId: 'package:webcrypto/webcrypto.dart', ) external void EC_POINT_free(ffi.Pointer point); -/// EC_POINT_get_affine_coordinates_GFp sets |x| and |y| to the affine value of -/// |point|. It returns one on success and zero otherwise. |ctx| is ignored and +/// EC_POINT_get_affine_coordinates_GFp sets `x` and `y` to the affine value of +/// `point`. It returns one on success and zero otherwise. `ctx` is ignored and /// may be NULL. /// -/// Either |x| or |y| may be NULL to skip computing that coordinate. This is +/// Either `x` or `y` may be NULL to skip computing that coordinate. This is /// slightly faster in the common case where only the x-coordinate is needed. @ffi.Native< ffi.Int Function( @@ -575,7 +575,7 @@ external int EC_POINT_get_affine_coordinates_GFp( ffi.Pointer ctx, ); -/// EC_POINT_new returns a fresh |EC_POINT| object in the given group, or NULL +/// EC_POINT_new returns a fresh `EC_POINT` object in the given group, or NULL /// on error. @ffi.Native Function(ffi.Pointer)>( symbol: 'webcrypto_EC_POINT_new', @@ -583,9 +583,9 @@ external int EC_POINT_get_affine_coordinates_GFp( ) external ffi.Pointer EC_POINT_new(ffi.Pointer group); -/// EC_POINT_oct2point sets |point| from |len| bytes of X9.62 format -/// serialisation in |buf|. It returns one on success and zero on error. |ctx| -/// may be NULL. It's considered an error if |buf| does not represent a point on +/// EC_POINT_oct2point sets `point` from `len` bytes of X9.62 format +/// serialisation in `buf`. It returns one on success and zero on error. `ctx` +/// may be NULL. It's considered an error if `buf` does not represent a point on /// the curve. @ffi.Native< ffi.Int Function( @@ -607,8 +607,8 @@ external int EC_POINT_oct2point( ffi.Pointer ctx, ); -/// EC_POINT_point2cbb behaves like |EC_POINT_point2oct| but appends the -/// serialised point to |cbb|. It returns one on success and zero on error. |ctx| +/// EC_POINT_point2cbb behaves like `EC_POINT_point2oct` but appends the +/// serialised point to `cbb`. It returns one on success and zero on error. `ctx` /// is ignored and may be NULL. @ffi.Native< ffi.Int Function( @@ -638,9 +638,9 @@ external int EC_POINT_point2cbb( external void ERR_clear_error(); /// ERR_error_string_n generates a human-readable string representing -/// |packed_error|, places it at |buf|, and returns |buf|. It writes at most -/// |len| bytes (including the terminating NUL) and truncates the string if -/// necessary. If |len| is greater than zero then |buf| is always NUL terminated. +/// `packed_error`, places it at `buf`, and returns `buf`. It writes at most +/// `len` bytes (including the terminating NUL) and truncates the string if +/// necessary. If `len` is greater than zero then `buf` is always NUL terminated. /// /// The string will have the following format: /// @@ -669,7 +669,7 @@ external ffi.Pointer ERR_error_string_n( ) external int ERR_get_error(); -/// The "peek" functions act like the |ERR_get_error| functions, above, but they +/// The "peek" functions act like the `ERR_get_error` functions, above, but they /// do not remove the error from the queue. @ffi.Native( symbol: 'webcrypto_ERR_peek_error', @@ -677,16 +677,16 @@ external int ERR_get_error(); ) external int ERR_peek_error(); -/// EVP_AEAD_CTX_free calls |EVP_AEAD_CTX_cleanup| and |OPENSSL_free| on -/// |ctx|. +/// EVP_AEAD_CTX_free calls `EVP_AEAD_CTX_cleanup` and `OPENSSL_free` on +/// `ctx`. @ffi.Native)>( symbol: 'webcrypto_EVP_AEAD_CTX_free', assetId: 'package:webcrypto/webcrypto.dart', ) external void EVP_AEAD_CTX_free(ffi.Pointer ctx); -/// EVP_AEAD_CTX_new allocates an |EVP_AEAD_CTX|, calls |EVP_AEAD_CTX_init| and -/// returns the |EVP_AEAD_CTX|, or NULL on error. +/// EVP_AEAD_CTX_new allocates an `EVP_AEAD_CTX`, calls `EVP_AEAD_CTX_init` and +/// returns the `EVP_AEAD_CTX`, or NULL on error. @ffi.Native< ffi.Pointer Function( ffi.Pointer, @@ -705,25 +705,25 @@ external ffi.Pointer EVP_AEAD_CTX_new( int tag_len, ); -/// EVP_AEAD_CTX_open authenticates |in_len| bytes from |in| and |ad_len| bytes -/// from |ad| and decrypts at most |in_len| bytes into |out|. It returns one on +/// EVP_AEAD_CTX_open authenticates `in_len` bytes from `in` and `ad_len` bytes +/// from `ad` and decrypts at most `in_len` bytes into `out`. It returns one on /// success and zero otherwise. /// /// This function may be called concurrently with itself or any other seal/open -/// function on the same |EVP_AEAD_CTX|. +/// function on the same `EVP_AEAD_CTX`. /// -/// At most |in_len| bytes are written to |out|. In order to ensure success, -/// |max_out_len| should be at least |in_len|. On successful return, |*out_len| -/// is set to the the actual number of bytes written. +/// At most `in_len` bytes are written to `out`. In order to ensure success, +/// `max_out_len` should be at least `in_len`. On successful return, `*out_len` +/// is set to the actual number of bytes written. /// -/// The length of |nonce|, |nonce_len|, must be equal to the result of -/// |EVP_AEAD_nonce_length| for this AEAD. +/// The length of `nonce`, `nonce_len`, must be equal to the result of +/// `EVP_AEAD_nonce_length` for this AEAD. /// -/// |EVP_AEAD_CTX_open| never results in a partial output. If |max_out_len| is -/// insufficient, zero will be returned. If any error occurs, |out| will be -/// filled with zero bytes and |*out_len| set to zero. +/// `EVP_AEAD_CTX_open` never results in a partial output. If `max_out_len` is +/// insufficient, zero will be returned. If any error occurs, `out` will be +/// filled with zero bytes and `*out_len` set to zero. /// -/// If |in| and |out| alias then |out| must be == |in|. +/// If `in` and `out` alias then `out` must be == `in`. @ffi.Native< ffi.Int Function( ffi.Pointer, @@ -754,26 +754,26 @@ external int EVP_AEAD_CTX_open( int ad_len, ); -/// EVP_AEAD_CTX_seal encrypts and authenticates |in_len| bytes from |in| and -/// authenticates |ad_len| bytes from |ad| and writes the result to |out|. It +/// EVP_AEAD_CTX_seal encrypts and authenticates `in_len` bytes from `in` and +/// authenticates `ad_len` bytes from `ad` and writes the result to `out`. It /// returns one on success and zero otherwise. /// /// This function may be called concurrently with itself or any other seal/open -/// function on the same |EVP_AEAD_CTX|. +/// function on the same `EVP_AEAD_CTX`. /// -/// At most |max_out_len| bytes are written to |out| and, in order to ensure -/// success, |max_out_len| should be |in_len| plus the result of -/// |EVP_AEAD_max_overhead|. On successful return, |*out_len| is set to the +/// At most `max_out_len` bytes are written to `out` and, in order to ensure +/// success, `max_out_len` should be `in_len` plus the result of +/// `EVP_AEAD_max_overhead`. On successful return, `*out_len` is set to the /// actual number of bytes written. /// -/// The length of |nonce|, |nonce_len|, must be equal to the result of -/// |EVP_AEAD_nonce_length| for this AEAD. +/// The length of `nonce`, `nonce_len`, must be equal to the result of +/// `EVP_AEAD_nonce_length` for this AEAD. /// -/// |EVP_AEAD_CTX_seal| never results in a partial output. If |max_out_len| is -/// insufficient, zero will be returned. If any error occurs, |out| will be -/// filled with zero bytes and |*out_len| set to zero. +/// `EVP_AEAD_CTX_seal` never results in a partial output. If `max_out_len` is +/// insufficient, zero will be returned. If any error occurs, `out` will be +/// filled with zero bytes and `*out_len` set to zero. /// -/// If |in| and |out| alias then |out| must be == |in|. +/// If `in` and `out` alias then `out` must be == `in`. @ffi.Native< ffi.Int Function( ffi.Pointer, @@ -805,7 +805,7 @@ external int EVP_AEAD_CTX_seal( ); /// EVP_AEAD_key_length returns the length, in bytes, of the keys used by -/// |aead|. +/// `aead`. @ffi.Native)>( symbol: 'webcrypto_EVP_AEAD_key_length', assetId: 'package:webcrypto/webcrypto.dart', @@ -813,16 +813,16 @@ external int EVP_AEAD_CTX_seal( external int EVP_AEAD_key_length(ffi.Pointer aead); /// EVP_AEAD_max_overhead returns the maximum number of additional bytes added -/// by the act of sealing data with |aead|. +/// by the act of sealing data with `aead`. @ffi.Native)>( symbol: 'webcrypto_EVP_AEAD_max_overhead', assetId: 'package:webcrypto/webcrypto.dart', ) external int EVP_AEAD_max_overhead(ffi.Pointer aead); -/// EVP_AEAD_max_tag_len returns the maximum tag length when using |aead|. This -/// is the largest value that can be passed as |tag_len| to -/// |EVP_AEAD_CTX_init|. +/// EVP_AEAD_max_tag_len returns the maximum tag length when using `aead`. This +/// is the largest value that can be passed as `tag_len` to +/// `EVP_AEAD_CTX_init`. @ffi.Native)>( symbol: 'webcrypto_EVP_AEAD_max_tag_len', assetId: 'package:webcrypto/webcrypto.dart', @@ -830,50 +830,51 @@ external int EVP_AEAD_max_overhead(ffi.Pointer aead); external int EVP_AEAD_max_tag_len(ffi.Pointer aead); /// EVP_AEAD_nonce_length returns the length, in bytes, of the per-message nonce -/// for |aead|. +/// for `aead`. @ffi.Native)>( symbol: 'webcrypto_EVP_AEAD_nonce_length', assetId: 'package:webcrypto/webcrypto.dart', ) external int EVP_AEAD_nonce_length(ffi.Pointer aead); -/// EVP_CIPHER_CTX_free calls |EVP_CIPHER_CTX_cleanup| on |ctx| and then frees -/// |ctx| itself. +/// EVP_CIPHER_CTX_free calls `EVP_CIPHER_CTX_cleanup` on `ctx` and then frees +/// `ctx` itself. @ffi.Native)>( symbol: 'webcrypto_EVP_CIPHER_CTX_free', assetId: 'package:webcrypto/webcrypto.dart', ) external void EVP_CIPHER_CTX_free(ffi.Pointer ctx); -/// EVP_CIPHER_CTX_new allocates a fresh |EVP_CIPHER_CTX|, calls -/// |EVP_CIPHER_CTX_init| and returns it, or NULL on allocation failure. +/// EVP_CIPHER_CTX_new returns a newly-allocated `EVP_CIPHER_CTX` in the zero +/// state, or NULL on allocation failure. The caller must use +/// `EVP_CIPHER_CTX_free` to release the resulting object. @ffi.Native Function()>( symbol: 'webcrypto_EVP_CIPHER_CTX_new', assetId: 'package:webcrypto/webcrypto.dart', ) external ffi.Pointer EVP_CIPHER_CTX_new(); -/// EVP_CIPHER_block_size returns the block size, in bytes, for |cipher|, or one -/// if |cipher| is a stream cipher. +/// EVP_CIPHER_block_size returns the block size, in bytes, for `cipher`, or one +/// if `cipher` is a stream cipher. @ffi.Native)>( symbol: 'webcrypto_EVP_CIPHER_block_size', assetId: 'package:webcrypto/webcrypto.dart', ) external int EVP_CIPHER_block_size(ffi.Pointer cipher); -/// EVP_CIPHER_iv_length returns the IV size, in bytes, of |cipher|, or zero if -/// |cipher| doesn't take an IV. +/// EVP_CIPHER_iv_length returns the IV size, in bytes, of `cipher`, or zero if +/// `cipher` doesn't take an IV. @ffi.Native)>( symbol: 'webcrypto_EVP_CIPHER_iv_length', assetId: 'package:webcrypto/webcrypto.dart', ) external int EVP_CIPHER_iv_length(ffi.Pointer cipher); -/// EVP_CipherFinal_ex does the same as |EVP_CipherFinal_ex2|, except that no +/// EVP_CipherFinal_ex does the same as `EVP_CipherFinal_ex2`, except that no /// output size is given and thus no bounds checking is performed. /// /// WARNING: This function does not check bounds on out, and correctly sizing -/// the output buffer is difficult. Use |EVP_CipherFinal_ex2| instead. +/// the output buffer is difficult. Use `EVP_CipherFinal_ex2` instead. @ffi.Native< ffi.Int Function( ffi.Pointer, @@ -890,13 +891,46 @@ external int EVP_CipherFinal_ex( ffi.Pointer out_len, ); -/// EVP_CipherInit_ex configures |ctx| for a fresh encryption (or decryption, if -/// |enc| is zero) operation using |cipher|. If |ctx| has been previously -/// configured with a cipher then |cipher|, |key| and |iv| may be |NULL| and -/// |enc| may be -1 to reuse the previous values. The operation will use |key| -/// as the key and |iv| as the IV (if any). These should have the correct -/// lengths given by |EVP_CIPHER_key_length| and |EVP_CIPHER_iv_length|. It -/// returns one on success and zero on error. +/// EVP_CipherInit_ex configures `ctx` for a fresh encryption (or decryption, if +/// `enc` is zero) operation using `cipher`. The memory in `ctx` must already +/// have been initialized, e.g. with `EVP_CIPHER_CTX_init` or +/// `EVP_CIPHER_CTX_new`. If non-NULL, `key` and `iv` will be used as the key and +/// IV, respectively. These must point to `EVP_CIPHER_key_length` and +/// `EVP_CIPHER_iv_length` bytes, respectively. It returns one on success and +/// zero on error. +/// +/// This function may be called multiple times on a single `EVP_CIPHER_CTX`, +/// either to reset an existing object, or to configure a single operation in +/// multiple steps. In such cases: +/// +/// - `cipher` may be NULL to reuse the previous cipher state. A non-NULL +/// `cipher` will reset all cipher state, including the key and IV, even if +/// configuring the same `EVP_CIPHER` as before. +/// +/// - `key` and `iv` may be NULL to either reuse the previous value, or leave +/// them unconfigured. +/// +/// - `enc` may be -1 to reuse the previous value. In ciphers that use a +/// different key schedule between encryption and decryption (e.g. CBC and ECB +/// modes), callers must configure `key` and `enc` together. +/// +/// Some example multi-step configuration patterns: +/// +/// - If setting a variable-length key with `EVP_CIPHER_CTX_set_key_length`, +/// first configure only `cipher`, then use `EVP_CIPHER_CTX_set_key_length`, +/// and finally configure `key`, leaving `cipher` NULL. +/// +/// - If setting a variable-length IV with `EVP_CTRL_AEAD_SET_IVLEN`, first +/// configure `cipher` and `key`, then use `EVP_CTRL_AEAD_SET_IVLEN`, and +/// finally configure `iv`, leaving other parameters NULL. +/// +/// - If retaining a long-lived context to reuse the key schedule across +/// operations, first configure only `cipher` and `key` on the long-lived +/// context, then copy the key schedule to a per-operation context with +/// `EVP_CIPHER_CTX_copy`, and finally configure only `iv` on the per-operation +/// context. +/// +/// WARNING: This API is difficult to use correctly. Use `EVP_AEAD` for AEADs. @ffi.Native< ffi.Int Function( ffi.Pointer, @@ -919,15 +953,15 @@ external int EVP_CipherInit_ex( int enc, ); -/// EVP_CipherUpdate does the same as |EVP_CipherUpdate_ex|, except that no +/// EVP_CipherUpdate does the same as `EVP_CipherUpdate_ex`, except that no /// output size is given and thus no bounds checking is performed. /// -/// Additionally, if |ctx| is an AEAD cipher, e.g. |EVP_aes_128_gcm|, and |out| -/// is NULL, this function instead behaves like |EVP_CipherUpdateAAD|. +/// Additionally, if `ctx` is an AEAD cipher, e.g. `EVP_aes_128_gcm`, and `out` +/// is NULL, this function instead behaves like `EVP_CipherUpdateAAD`. /// -/// WARNING: This function does not check bounds on |out|, and correctly sizing -/// the output buffer is difficult. Use |EVP_CipherUpdate_ex| or -/// |EVP_CipherUpdateAAD| instead. +/// WARNING: This function does not check bounds on `out`, and correctly sizing +/// the output buffer is difficult. Use `EVP_CipherUpdate_ex` or +/// `EVP_CipherUpdateAAD` instead. @ffi.Native< ffi.Int Function( ffi.Pointer, @@ -948,8 +982,8 @@ external int EVP_CipherUpdate( int in_len, ); -/// EVP_DigestFinal acts like |EVP_DigestFinal_ex| except that -/// |EVP_MD_CTX_cleanup| is called on |ctx| before returning. +/// EVP_DigestFinal acts like `EVP_DigestFinal_ex` except that +/// `EVP_MD_CTX_cleanup` is called on `ctx` before returning. @ffi.Native< ffi.Int Function( ffi.Pointer, @@ -966,8 +1000,18 @@ external int EVP_DigestFinal( ffi.Pointer out_size, ); -/// EVP_DigestInit acts like |EVP_DigestInit_ex| except that |ctx| is -/// initialised before use. +/// EVP_DigestInit calls `EVP_MD_CTX_init`, followed by `EVP_DigestInit_ex`. +/// +/// `EVP_MD_CTX_init` is used for initializing uninitialized memory in an +/// `EVP_MD_CTX`, e.g. if it is declared as a local variable on the stack. Thus +/// this function should not be used on objects that have already been +/// initialized. +/// +/// WARNING: This differs from OpenSSL 1.1.x, where `EVP_DigestInit` and +/// `EVP_DigestInit_ex` are largely equivalent. This difference is because +/// BoringSSL, like OpenSSL 1.0.x, still supports stack-allocating `EVP_MD_CTX`. +/// Implementing the OpenSSL 1.1.x semantics would introduce uninitialized reads +/// in those callers. @ffi.Native, ffi.Pointer)>( symbol: 'webcrypto_EVP_DigestInit', assetId: 'package:webcrypto/webcrypto.dart', @@ -978,14 +1022,14 @@ external int EVP_DigestInit( ); /// EVP_DigestSignFinal signs the data that has been included by one or more -/// calls to |EVP_DigestSignUpdate|. If |out_sig| is NULL then |*out_sig_len| is +/// calls to `EVP_DigestSignUpdate`. If `out_sig` is NULL then `*out_sig_len` is /// set to the maximum number of output bytes. Otherwise, on entry, -/// |*out_sig_len| must contain the length of the |out_sig| buffer. If the call -/// is successful, the signature is written to |out_sig| and |*out_sig_len| is +/// `*out_sig_len` must contain the length of the `out_sig` buffer. If the call +/// is successful, the signature is written to `out_sig` and `*out_sig_len` is /// set to its length. /// /// This function performs a streaming signing operation and will fail for -/// signature algorithms which do not support this. Use |EVP_DigestSign| for a +/// signature algorithms which do not support this. Use `EVP_DigestSign` for a /// single-shot operation. /// /// It returns one on success, or zero on error. @@ -1005,18 +1049,15 @@ external int EVP_DigestSignFinal( ffi.Pointer out_sig_len, ); -/// EVP_DigestSignInit sets up |ctx| for a signing operation with |type| and -/// |pkey|. The |ctx| argument must have been initialised with -/// |EVP_MD_CTX_init|. If |pctx| is not NULL, the |EVP_PKEY_CTX| of the signing -/// operation will be written to |*pctx|; this can be used to set alternative +/// EVP_DigestSignInit sets up `ctx` for a signing operation with `type` and +/// `pkey`. The `ctx` argument must have been initialised with +/// `EVP_MD_CTX_init`. If `pctx` is not NULL, the `EVP_PKEY_CTX` of the signing +/// operation will be written to `*pctx`; this can be used to set alternative /// signing options. /// /// For single-shot signing algorithms which do not use a pre-hash, such as -/// Ed25519, |type| should be NULL. The |EVP_MD_CTX| itself is unused but is -/// present so the API is uniform. See |EVP_DigestSign|. -/// -/// This function does not mutate |pkey| for thread-safety purposes and may be -/// used concurrently with other non-mutating functions on |pkey|. +/// Ed25519, `type` should be NULL. The `EVP_MD_CTX` itself is unused but is +/// present so the API is uniform. See `EVP_DigestSign`. /// /// It returns one on success, or zero on error. @ffi.Native< @@ -1039,11 +1080,11 @@ external int EVP_DigestSignInit( ffi.Pointer pkey, ); -/// EVP_DigestSignUpdate appends |len| bytes from |data| to the data which will -/// be signed in |EVP_DigestSignFinal|. It returns one. +/// EVP_DigestSignUpdate appends `len` bytes from `data` to the data which will +/// be signed in `EVP_DigestSignFinal`. It returns one. /// /// This function performs a streaming signing operation and will fail for -/// signature algorithms which do not support this. Use |EVP_DigestSign| for a +/// signature algorithms which do not support this. Use `EVP_DigestSign` for a /// single-shot operation. @ffi.Native< ffi.Int Function(ffi.Pointer, ffi.Pointer, ffi.Size) @@ -1057,8 +1098,8 @@ external int EVP_DigestSignUpdate( int len, ); -/// EVP_DigestUpdate hashes |len| bytes from |data| into the hashing operation -/// in |ctx|. It returns one. +/// EVP_DigestUpdate hashes `len` bytes from `data` into the hashing operation +/// in `ctx`. It returns one. @ffi.Native< ffi.Int Function(ffi.Pointer, ffi.Pointer, ffi.Size) >( @@ -1071,12 +1112,12 @@ external int EVP_DigestUpdate( int len, ); -/// EVP_DigestVerifyFinal verifies that |sig_len| bytes of |sig| are a valid +/// EVP_DigestVerifyFinal verifies that `sig_len` bytes of `sig` are a valid /// signature for the data that has been included by one or more calls to -/// |EVP_DigestVerifyUpdate|. It returns one on success and zero otherwise. +/// `EVP_DigestVerifyUpdate`. It returns one on success and zero otherwise. /// /// This function performs streaming signature verification and will fail for -/// signature algorithms which do not support this. Use |EVP_DigestVerify| for a +/// signature algorithms which do not support this. Use `EVP_DigestVerify` for a /// single-shot verification. @ffi.Native< ffi.Int Function(ffi.Pointer, ffi.Pointer, ffi.Size) @@ -1090,18 +1131,15 @@ external int EVP_DigestVerifyFinal( int sig_len, ); -/// EVP_DigestVerifyInit sets up |ctx| for a signature verification operation -/// with |type| and |pkey|. The |ctx| argument must have been initialised with -/// |EVP_MD_CTX_init|. If |pctx| is not NULL, the |EVP_PKEY_CTX| of the signing -/// operation will be written to |*pctx|; this can be used to set alternative +/// EVP_DigestVerifyInit sets up `ctx` for a signature verification operation +/// with `type` and `pkey`. The `ctx` argument must have been initialised with +/// `EVP_MD_CTX_init`. If `pctx` is not NULL, the `EVP_PKEY_CTX` of the signing +/// operation will be written to `*pctx`; this can be used to set alternative /// signing options. /// /// For single-shot signing algorithms which do not use a pre-hash, such as -/// Ed25519, |type| should be NULL. The |EVP_MD_CTX| itself is unused but is -/// present so the API is uniform. See |EVP_DigestVerify|. -/// -/// This function does not mutate |pkey| for thread-safety purposes and may be -/// used concurrently with other non-mutating functions on |pkey|. +/// Ed25519, `type` should be NULL. The `EVP_MD_CTX` itself is unused but is +/// present so the API is uniform. See `EVP_DigestVerify`. /// /// It returns one on success, or zero on error. @ffi.Native< @@ -1124,11 +1162,11 @@ external int EVP_DigestVerifyInit( ffi.Pointer pkey, ); -/// EVP_DigestVerifyUpdate appends |len| bytes from |data| to the data which -/// will be verified by |EVP_DigestVerifyFinal|. It returns one. +/// EVP_DigestVerifyUpdate appends `len` bytes from `data` to the data which +/// will be verified by `EVP_DigestVerifyFinal`. It returns one. /// /// This function performs streaming signature verification and will fail for -/// signature algorithms which do not support this. Use |EVP_DigestVerify| for a +/// signature algorithms which do not support this. Use `EVP_DigestVerify` for a /// single-shot verification. @ffi.Native< ffi.Int Function(ffi.Pointer, ffi.Pointer, ffi.Size) @@ -1142,45 +1180,45 @@ external int EVP_DigestVerifyUpdate( int len, ); -/// EVP_MD_CTX_free calls |EVP_MD_CTX_cleanup| and then frees |ctx| itself. +/// EVP_MD_CTX_free calls `EVP_MD_CTX_cleanup` and then frees `ctx` itself. @ffi.Native)>( symbol: 'webcrypto_EVP_MD_CTX_free', assetId: 'package:webcrypto/webcrypto.dart', ) external void EVP_MD_CTX_free(ffi.Pointer ctx); -/// EVP_MD_CTX_new allocates and initialises a fresh |EVP_MD_CTX| and returns -/// it, or NULL on allocation failure. The caller must use |EVP_MD_CTX_free| to -/// release the resulting object. +/// EVP_MD_CTX_new returns a newly-allocated `EVP_MD_CTX` in the zero state, or +/// NULL on allocation failure. The caller must use `EVP_MD_CTX_free` to release +/// the resulting object. @ffi.Native Function()>( symbol: 'webcrypto_EVP_MD_CTX_new', assetId: 'package:webcrypto/webcrypto.dart', ) external ffi.Pointer EVP_MD_CTX_new(); -/// EVP_MD_CTX_size returns the digest size of |ctx|, in bytes. It -/// will crash if a digest hasn't been set on |ctx|. +/// EVP_MD_CTX_size returns the digest size of `ctx`, in bytes. It +/// will crash if a digest hasn't been set on `ctx`. @ffi.Native)>( symbol: 'webcrypto_EVP_MD_CTX_size', assetId: 'package:webcrypto/webcrypto.dart', ) external int EVP_MD_CTX_size(ffi.Pointer ctx); -/// EVP_MD_size returns the digest size of |md|, in bytes. +/// EVP_MD_size returns the digest size of `md`, in bytes. @ffi.Native)>( symbol: 'webcrypto_EVP_MD_size', assetId: 'package:webcrypto/webcrypto.dart', ) external int EVP_MD_size(ffi.Pointer md); -/// EVP_PKEY_CTX_free frees |ctx| and the data it owns. +/// EVP_PKEY_CTX_free frees `ctx` and the data it owns. @ffi.Native)>( symbol: 'webcrypto_EVP_PKEY_CTX_free', assetId: 'package:webcrypto/webcrypto.dart', ) external void EVP_PKEY_CTX_free(ffi.Pointer ctx); -/// EVP_PKEY_CTX_new allocates a fresh |EVP_PKEY_CTX| for use with |pkey|. It +/// EVP_PKEY_CTX_new allocates a fresh `EVP_PKEY_CTX` for use with `pkey`. It /// returns the context or NULL on error. @ffi.Native< ffi.Pointer Function(ffi.Pointer, ffi.Pointer) @@ -1193,9 +1231,9 @@ external ffi.Pointer EVP_PKEY_CTX_new( ffi.Pointer e, ); -/// EVP_PKEY_CTX_set0_rsa_oaep_label sets |label_len| bytes from |label| as the -/// label used in OAEP. DANGER: On success, this call takes ownership of |label| -/// and will call |OPENSSL_free| on it when |ctx| is destroyed. +/// EVP_PKEY_CTX_set0_rsa_oaep_label sets `label_len` bytes from `label` as the +/// label used in OAEP. DANGER: On success, this call takes ownership of `label` +/// and will call `OPENSSL_free` on it when `ctx` is destroyed. /// /// Returns one on success or zero on error. @ffi.Native< @@ -1210,11 +1248,11 @@ external int EVP_PKEY_CTX_set0_rsa_oaep_label( int label_len, ); -/// EVP_PKEY_CTX_set_rsa_mgf1_md sets |md| as the digest used in MGF1. Returns +/// EVP_PKEY_CTX_set_rsa_mgf1_md sets `md` as the digest used in MGF1. Returns /// one on success or zero on error. /// -/// If unset, the default is the signing hash for |RSA_PKCS1_PSS_PADDING| and the -/// OAEP hash for |RSA_PKCS1_OAEP_PADDING|. Callers are recommended to use this +/// If unset, the default is the signing hash for `RSA_PKCS1_PSS_PADDING` and the +/// OAEP hash for `RSA_PKCS1_OAEP_PADDING`. Callers are recommended to use this /// default and not call this function. @ffi.Native, ffi.Pointer)>( symbol: 'webcrypto_EVP_PKEY_CTX_set_rsa_mgf1_md', @@ -1225,7 +1263,7 @@ external int EVP_PKEY_CTX_set_rsa_mgf1_md( ffi.Pointer md, ); -/// EVP_PKEY_CTX_set_rsa_oaep_md sets |md| as the digest used in OAEP padding. +/// EVP_PKEY_CTX_set_rsa_oaep_md sets `md` as the digest used in OAEP padding. /// Returns one on success or zero on error. If unset, the default is SHA-1. /// Callers are recommended to overwrite this default. /// @@ -1240,8 +1278,8 @@ external int EVP_PKEY_CTX_set_rsa_oaep_md( ); /// EVP_PKEY_CTX_set_rsa_padding sets the padding type to use. It should be one -/// of the |RSA_*_PADDING| values. Returns one on success or zero on error. By -/// default, the padding is |RSA_PKCS1_PADDING|. +/// of the `RSA_*_PADDING` values. Returns one on success or zero on error. By +/// default, the padding is `RSA_PKCS1_PADDING`. @ffi.Native, ffi.Int)>( symbol: 'webcrypto_EVP_PKEY_CTX_set_rsa_padding', assetId: 'package:webcrypto/webcrypto.dart', @@ -1252,14 +1290,14 @@ external int EVP_PKEY_CTX_set_rsa_padding( ); /// EVP_PKEY_CTX_set_rsa_pss_saltlen sets the length of the salt in a PSS-padded -/// signature. A value of |RSA_PSS_SALTLEN_DIGEST| causes the salt to be the same -/// length as the digest in the signature. A value of |RSA_PSS_SALTLEN_AUTO| +/// signature. A value of `RSA_PSS_SALTLEN_DIGEST` causes the salt to be the same +/// length as the digest in the signature. A value of `RSA_PSS_SALTLEN_AUTO` /// causes the salt to be the maximum length that will fit when signing and /// recovered from the signature when verifying. Otherwise the value gives the /// size of the salt in bytes. /// -/// If unsure, use |RSA_PSS_SALTLEN_DIGEST|, which is the default. Note this -/// differs from OpenSSL, which defaults to |RSA_PSS_SALTLEN_AUTO|. +/// If unsure, use `RSA_PSS_SALTLEN_DIGEST`, which is the default. Note this +/// differs from OpenSSL, which defaults to `RSA_PSS_SALTLEN_AUTO`. /// /// Returns one on success or zero on error. @ffi.Native, ffi.Int)>( @@ -1271,13 +1309,13 @@ external int EVP_PKEY_CTX_set_rsa_pss_saltlen( int salt_len, ); -/// EVP_PKEY_decrypt decrypts |in_len| bytes from |in|. If |out| is NULL, the -/// maximum size of the plaintext is written to |out_len|. Otherwise, |*out_len| -/// must contain the number of bytes of space available at |out|. If sufficient, -/// the ciphertext will be written to |out| and |*out_len| updated with the true +/// EVP_PKEY_decrypt decrypts `in_len` bytes from `in`. If `out` is NULL, the +/// maximum size of the plaintext is written to `out_len`. Otherwise, `*out_len` +/// must contain the number of bytes of space available at `out`. If sufficient, +/// the ciphertext will be written to `out` and `*out_len` updated with the true /// length. /// -/// WARNING: Setting |out| to NULL only gives the maximum size of the +/// WARNING: Setting `out` to NULL only gives the maximum size of the /// plaintext. The actual plaintext may be smaller. /// /// It returns one on success or zero on error. @@ -1301,8 +1339,8 @@ external int EVP_PKEY_decrypt( int in_len, ); -/// EVP_PKEY_decrypt_init initialises an |EVP_PKEY_CTX| for a decryption -/// operation. It should be called before |EVP_PKEY_decrypt|. +/// EVP_PKEY_decrypt_init initialises an `EVP_PKEY_CTX` for a decryption +/// operation. It should be called before `EVP_PKEY_decrypt`. /// /// It returns one on success or zero on error. @ffi.Native)>( @@ -1311,13 +1349,13 @@ external int EVP_PKEY_decrypt( ) external int EVP_PKEY_decrypt_init(ffi.Pointer ctx); -/// EVP_PKEY_encrypt encrypts |in_len| bytes from |in|. If |out| is NULL, the -/// maximum size of the ciphertext is written to |out_len|. Otherwise, |*out_len| -/// must contain the number of bytes of space available at |out|. If sufficient, -/// the ciphertext will be written to |out| and |*out_len| updated with the true +/// EVP_PKEY_encrypt encrypts `in_len` bytes from `in`. If `out` is NULL, the +/// maximum size of the ciphertext is written to `out_len`. Otherwise, `*out_len` +/// must contain the number of bytes of space available at `out`. If sufficient, +/// the ciphertext will be written to `out` and `*out_len` updated with the true /// length. /// -/// WARNING: Setting |out| to NULL only gives the maximum size of the +/// WARNING: Setting `out` to NULL only gives the maximum size of the /// ciphertext. The actual ciphertext may be smaller. /// /// It returns one on success or zero on error. @@ -1341,8 +1379,8 @@ external int EVP_PKEY_encrypt( int in_len, ); -/// EVP_PKEY_encrypt_init initialises an |EVP_PKEY_CTX| for an encryption -/// operation. It should be called before |EVP_PKEY_encrypt|. +/// EVP_PKEY_encrypt_init initialises an `EVP_PKEY_CTX` for an encryption +/// operation. It should be called before `EVP_PKEY_encrypt`. /// /// It returns one on success or zero on error. @ffi.Native)>( @@ -1351,7 +1389,7 @@ external int EVP_PKEY_encrypt( ) external int EVP_PKEY_encrypt_init(ffi.Pointer ctx); -/// EVP_PKEY_free decrements the reference count of |pkey| and frees it if the +/// EVP_PKEY_free decrements the reference count of `pkey` and frees it if the /// reference count drops to zero. @ffi.Native)>( symbol: 'webcrypto_EVP_PKEY_free', @@ -1371,12 +1409,12 @@ external ffi.Pointer EVP_PKEY_get1_EC_KEY(ffi.Pointer pkey); ) external ffi.Pointer EVP_PKEY_get1_RSA(ffi.Pointer pkey); -/// EVP_PKEY_id returns the type of |pkey|, which is one of the |EVP_PKEY_*| +/// EVP_PKEY_id returns the type of `pkey`, which is one of the `EVP_PKEY_*` /// values above. These type values generally correspond to the algorithm OID, /// but not the parameters, of a SubjectPublicKeyInfo (RFC 5280) or /// PrivateKeyInfo (RFC 5208) AlgorithmIdentifier. Algorithm parameters can be /// inspected with algorithm-specific accessors, e.g. -/// |EVP_PKEY_get_ec_curve_nid|. +/// `EVP_PKEY_get_ec_curve_nid`. @ffi.Native)>( symbol: 'webcrypto_EVP_PKEY_id', assetId: 'package:webcrypto/webcrypto.dart', @@ -1403,29 +1441,29 @@ external int EVP_PKEY_set1_EC_KEY( /// Getting and setting concrete key types. /// /// The following functions get and set the underlying key representation in an -/// |EVP_PKEY| object. The |set1| functions take an additional reference to the -/// underlying key and return one on success or zero if |key| is NULL. The -/// |assign| functions adopt the caller's reference and return one on success or -/// zero if |key| is NULL. The |get1| functions return a fresh reference to the -/// underlying object or NULL if |pkey| is not of the correct type. The |get0| +/// `EVP_PKEY` object. The `set1` functions take an additional reference to the +/// underlying key and return one on success or zero if `key` is NULL. The +/// `assign` functions adopt the caller's reference and return one on success or +/// zero if `key` is NULL. The `get1` functions return a fresh reference to the +/// underlying object or NULL if `pkey` is not of the correct type. The `get0` /// functions behave the same but return a non-owning pointer. /// -/// The |get0| and |get1| functions take |const| pointers and are thus +/// The `get0` and `get1` functions take `const` pointers and are thus /// non-mutating for thread-safety purposes, but mutating functions on the -/// returned lower-level objects are considered to also mutate the |EVP_PKEY| and -/// may not be called concurrently with other operations on the |EVP_PKEY|. +/// returned lower-level objects are considered to also mutate the `EVP_PKEY` and +/// may not be called concurrently with other operations on the `EVP_PKEY`. /// /// WARNING: Matching OpenSSL, the RSA functions behave non-uniformly. -/// |EVP_PKEY_set1_RSA| and |EVP_PKEY_assign_RSA| construct an |EVP_PKEY_RSA| -/// key, while the |EVP_PKEY_get0_RSA| and |EVP_PKEY_get1_RSA| will return -/// non-NULL for both |EVP_PKEY_RSA| and |EVP_PKEY_RSA_PSS|. +/// `EVP_PKEY_set1_RSA` and `EVP_PKEY_assign_RSA` construct an `EVP_PKEY_RSA` +/// key, while the `EVP_PKEY_get0_RSA` and `EVP_PKEY_get1_RSA` will return +/// non-NULL for both `EVP_PKEY_RSA` and `EVP_PKEY_RSA_PSS`. /// /// This means callers risk misusing a key if they assume a non-NULL return from -/// |EVP_PKEY_get0_RSA| or |EVP_PKEY_get1_RSA| implies |EVP_PKEY_RSA|. Prefer -/// |EVP_PKEY_id| to check the type of a key. To reduce this risk, BoringSSL does -/// not make |EVP_PKEY_RSA_PSS| available by default, only when callers opt in -/// via |EVP_pkey_rsa_pss_sha256|. This differs from upstream OpenSSL, where -/// callers are exposed to |EVP_PKEY_RSA_PSS| by default. +/// `EVP_PKEY_get0_RSA` or `EVP_PKEY_get1_RSA` implies `EVP_PKEY_RSA`. Prefer +/// `EVP_PKEY_id` to check the type of a key. To reduce this risk, BoringSSL does +/// not make `EVP_PKEY_RSA_PSS` available by default, only when callers opt in +/// via `EVP_pkey_rsa_pss_sha256`. This differs from upstream OpenSSL, where +/// callers are exposed to `EVP_PKEY_RSA_PSS` by default. @ffi.Native, ffi.Pointer)>( symbol: 'webcrypto_EVP_PKEY_set1_RSA', assetId: 'package:webcrypto/webcrypto.dart', @@ -1435,23 +1473,23 @@ external int EVP_PKEY_set1_RSA( ffi.Pointer key, ); -/// EVP_PKEY_set_type sets the type of |pkey| to |type|. It returns one if -/// successful or zero if the |type| argument is not one of the |EVP_PKEY_*| -/// values supported for use with this function. If |pkey| is NULL, it simply +/// EVP_PKEY_set_type sets the type of `pkey` to `type`. It returns one if +/// successful or zero if the `type` argument is not one of the `EVP_PKEY_*` +/// values supported for use with this function. If `pkey` is NULL, it simply /// reports whether the type is known. /// -/// There are very few cases where this function is useful. Changing |pkey|'s +/// There are very few cases where this function is useful. Changing `pkey`'s /// type clears any previously stored keys, so there is no benefit to loading a -/// key and then changing its type. Although |pkey| is left with a type +/// key and then changing its type. Although `pkey` is left with a type /// configured, it has no key, and functions which set a key, such as -/// |EVP_PKEY_set1_RSA|, will configure a type anyway. If writing unit tests that +/// `EVP_PKEY_set1_RSA`, will configure a type anyway. If writing unit tests that /// are only sensitive to the type of a key, it is preferable to construct a real /// key, so that tests are more representative of production code. /// /// The only API pattern which requires this function is -/// |EVP_PKEY_set1_tls_encodedpoint| with X25519, which requires a half-empty -/// |EVP_PKEY| that was first configured with |EVP_PKEY_X25519|. Currently, all -/// other values of |type| will result in an error. +/// `EVP_PKEY_set1_tls_encodedpoint` with X25519, which requires a half-empty +/// `EVP_PKEY` that was first configured with `EVP_PKEY_X25519`. Currently, all +/// other values of `type` will result in an error. @ffi.Native, ffi.Int)>( symbol: 'webcrypto_EVP_PKEY_set_type', assetId: 'package:webcrypto/webcrypto.dart', @@ -1508,8 +1546,8 @@ external ffi.Pointer EVP_aes_256_cbc(); ) external ffi.Pointer EVP_aes_256_ctr(); -/// EVP_marshal_private_key marshals |key| as a DER-encoded PrivateKeyInfo -/// structure (RFC 5208) and appends the result to |cbb|. It returns one on +/// EVP_marshal_private_key marshals `key` as a DER-encoded PrivateKeyInfo +/// structure (RFC 5208) and appends the result to `cbb`. It returns one on /// success and zero on error. @ffi.Native, ffi.Pointer)>( symbol: 'webcrypto_EVP_marshal_private_key', @@ -1520,8 +1558,8 @@ external int EVP_marshal_private_key( ffi.Pointer key, ); -/// EVP_marshal_public_key marshals |key| as a DER-encoded SubjectPublicKeyInfo -/// structure (RFC 5280) and appends the result to |cbb|. It returns one on +/// EVP_marshal_public_key marshals `key` as a DER-encoded SubjectPublicKeyInfo +/// structure (RFC 5280) and appends the result to `cbb`. It returns one on /// success and zero on error. @ffi.Native, ffi.Pointer)>( symbol: 'webcrypto_EVP_marshal_public_key', @@ -1533,13 +1571,13 @@ external int EVP_marshal_public_key( ); /// EVP_parse_private_key decodes a DER-encoded PrivateKeyInfo structure (RFC -/// 5208) from |cbs| and advances |cbs|. It returns a newly-allocated |EVP_PKEY| +/// 5208) from `cbs` and advances `cbs`. It returns a newly-allocated `EVP_PKEY` /// or NULL on error. /// -/// Prefer |EVP_PKEY_from_private_key_info| instead. This function has +/// Prefer `EVP_PKEY_from_private_key_info` instead. This function has /// several pitfalls: /// -/// Callers are expected to handle trailing data returned from |cbs|, making more +/// Callers are expected to handle trailing data returned from `cbs`, making more /// common cases error-prone. /// /// There is also no way to pass in supported algorithms. This function instead @@ -1552,7 +1590,7 @@ external int EVP_marshal_public_key( /// it is suitable and validate other desired key properties such as RSA modulus /// size or EC curve. In particular, RSA private key operations scale cubicly, so /// applications accepting RSA private keys from external sources may need to -/// bound key sizes (use |EVP_PKEY_bits| or |RSA_bits|) to avoid a DoS vector. +/// bound key sizes (use `EVP_PKEY_bits` or `RSA_bits`) to avoid a DoS vector. /// /// A PrivateKeyInfo ends with an optional set of attributes. These are silently /// ignored. @@ -1563,13 +1601,13 @@ external int EVP_marshal_public_key( external ffi.Pointer EVP_parse_private_key(ffi.Pointer cbs); /// EVP_parse_public_key decodes a DER-encoded SubjectPublicKeyInfo structure -/// (RFC 5280) from |cbs| and advances |cbs|. It returns a newly-allocated -/// |EVP_PKEY| or NULL on error. +/// (RFC 5280) from `cbs` and advances `cbs`. It returns a newly-allocated +/// `EVP_PKEY` or NULL on error. /// -/// Prefer |EVP_PKEY_from_subject_public_key_info| instead. This function has +/// Prefer `EVP_PKEY_from_subject_public_key_info` instead. This function has /// several pitfalls: /// -/// Callers are expected to handle trailing data returned from |cbs|, making more +/// Callers are expected to handle trailing data returned from `cbs`, making more /// common cases error-prone. /// /// There is also no way to pass in supported algorithms. This function instead @@ -1612,8 +1650,8 @@ external ffi.Pointer EVP_sha384(); external ffi.Pointer EVP_sha512(); /// HKDF computes HKDF (as specified by RFC 5869) of initial keying material -/// |secret| with |salt| and |info| using |digest|, and outputs |out_len| bytes -/// to |out_key|. It returns one on success and zero on error. +/// `secret` with `salt` and `info` using `digest`, and outputs `out_len` bytes +/// to `out_key`. It returns one on success and zero on error. /// /// HKDF is an Extract-and-Expand algorithm. It does not do any key stretching, /// and as such, is not suited to be used alone to generate a key from a @@ -1643,15 +1681,15 @@ external int HKDF( int info_len, ); -/// HMAC_CTX_free calls |HMAC_CTX_cleanup| and then frees |ctx| itself. +/// HMAC_CTX_free calls `HMAC_CTX_cleanup` and then frees `ctx` itself. @ffi.Native)>( symbol: 'webcrypto_HMAC_CTX_free', assetId: 'package:webcrypto/webcrypto.dart', ) external void HMAC_CTX_free(ffi.Pointer ctx); -/// HMAC_CTX_new allocates and initialises a new |HMAC_CTX| and returns it, or -/// NULL on allocation failure. The caller must use |HMAC_CTX_free| to release +/// HMAC_CTX_new allocates and initialises a new `HMAC_CTX` and returns it, or +/// NULL on allocation failure. The caller must use `HMAC_CTX_free` to release /// the resulting object. @ffi.Native Function()>( symbol: 'webcrypto_HMAC_CTX_new', @@ -1659,10 +1697,10 @@ external void HMAC_CTX_free(ffi.Pointer ctx); ) external ffi.Pointer HMAC_CTX_new(); -/// HMAC_Final completes the HMAC operation in |ctx| and writes the result to -/// |out|. If |out_len| is not |NULL| then it writes the length of the result to -/// |*out_len|. On entry, |out| must contain at least |HMAC_size| bytes of -/// space. An output size of |EVP_MAX_MD_SIZE| will always be large enough. It +/// HMAC_Final completes the HMAC operation in `ctx` and writes the result to +/// `out`. If `out_len` is not `NULL` then it writes the length of the result to +/// `*out_len`. On entry, `out` must contain at least `HMAC_size` bytes of +/// space. An output size of `EVP_MAX_MD_SIZE` will always be large enough. It /// returns one on success or zero on allocation failure. @ffi.Native< ffi.Int Function( @@ -1677,14 +1715,14 @@ external int HMAC_Final( ffi.Pointer out_len, ); -/// HMAC_Init_ex sets up an initialised |HMAC_CTX| to use |md| as the hash -/// function and |key| as the key. For a non-initial call, |md| may be NULL, in +/// HMAC_Init_ex sets up an initialised `HMAC_CTX` to use `md` as the hash +/// function and `key` as the key. For a non-initial call, `md` may be NULL, in /// which case the previous hash function will be used. If the hash function has -/// not changed and |key| is NULL, |ctx| reuses the previous key. It returns one +/// not changed and `key` is NULL, `ctx` reuses the previous key. It returns one /// on success or zero on allocation failure. /// /// WARNING: NULL and empty keys are ambiguous on non-initial calls. Passing NULL -/// |key| but repeating the previous |md| reuses the previous key rather than the +/// `key` but repeating the previous `md` reuses the previous key rather than the /// empty key. @ffi.Native< ffi.Int Function( @@ -1703,8 +1741,8 @@ external int HMAC_Init_ex( ffi.Pointer impl, ); -/// HMAC_Update hashes |data_len| bytes from |data| into the current HMAC -/// operation in |ctx|. It returns one. +/// HMAC_Update hashes `data_len` bytes from `data` into the current HMAC +/// operation in `ctx`. It returns one. @ffi.Native< ffi.Int Function(ffi.Pointer, ffi.Pointer, ffi.Size) >(symbol: 'webcrypto_HMAC_Update', assetId: 'package:webcrypto/webcrypto.dart') @@ -1715,35 +1753,35 @@ external int HMAC_Update( ); /// HMAC_size returns the size, in bytes, of the HMAC that will be produced by -/// |ctx|. On entry, |ctx| must have been setup with |HMAC_Init_ex|. +/// `ctx`. On entry, `ctx` must have been setup with `HMAC_Init_ex`. @ffi.Native)>( symbol: 'webcrypto_HMAC_size', assetId: 'package:webcrypto/webcrypto.dart', ) external int HMAC_size(ffi.Pointer ctx); -/// OPENSSL_free does nothing if |ptr| is NULL. Otherwise it zeros out the -/// memory allocated at |ptr| and frees it along with the private data. -/// It must only be used on on |ptr| values obtained from |OPENSSL_malloc| +/// OPENSSL_free does nothing if `ptr` is NULL. Otherwise it zeros out the +/// memory allocated at `ptr` and frees it along with the private data. +/// It must only be used on `ptr` values obtained from `OPENSSL_malloc` @ffi.Native)>( symbol: 'webcrypto_OPENSSL_free', assetId: 'package:webcrypto/webcrypto.dart', ) external void OPENSSL_free(ffi.Pointer ptr); -/// OPENSSL_malloc is similar to a regular |malloc|, but allocates additional -/// private data. The resulting pointer must be freed with |OPENSSL_free|. In -/// the case of a malloc failure, prior to returning NULL |OPENSSL_malloc| will -/// push |ERR_R_MALLOC_FAILURE| onto the openssl error stack. +/// OPENSSL_malloc is similar to a regular `malloc`, but allocates additional +/// private data. The resulting pointer must be freed with `OPENSSL_free`. In +/// the case of a malloc failure, prior to returning NULL `OPENSSL_malloc` will +/// push `ERR_R_MALLOC_FAILURE` onto the openssl error stack. @ffi.Native Function(ffi.Size)>( symbol: 'webcrypto_OPENSSL_malloc', assetId: 'package:webcrypto/webcrypto.dart', ) external ffi.Pointer OPENSSL_malloc(int size); -/// OPENSSL_memdup returns an allocated, duplicate of |size| bytes from |data| or +/// OPENSSL_memdup returns an allocated, duplicate of `size` bytes from `data` or /// NULL on allocation failure. The memory allocated must be freed with -/// |OPENSSL_free|. +/// `OPENSSL_free`. @ffi.Native Function(ffi.Pointer, ffi.Size)>( symbol: 'webcrypto_OPENSSL_memdup', assetId: 'package:webcrypto/webcrypto.dart', @@ -1753,8 +1791,8 @@ external ffi.Pointer OPENSSL_memdup( int size, ); -/// PKCS5_PBKDF2_HMAC computes |iterations| iterations of PBKDF2 of |password| -/// and |salt|, using |digest|, and outputs |key_len| bytes to |out_key|. It +/// PKCS5_PBKDF2_HMAC computes `iterations` iterations of PBKDF2 of `password` +/// and `salt`, using `digest`, and outputs `key_len` bytes to `out_key`. It /// returns one on success and zero on allocation failure or if iterations is 0. @ffi.Native< ffi.Int Function( @@ -1782,23 +1820,23 @@ external int PKCS5_PBKDF2_HMAC( ffi.Pointer out_key, ); -/// RAND_bytes writes |len| bytes of random data to |buf| and returns one. In the -/// event that sufficient random data can not be obtained, |abort| is called. +/// RAND_bytes writes `len` bytes of random data to `buf` and returns one. In the +/// event that sufficient random data can not be obtained, `abort` is called. @ffi.Native, ffi.Size)>( symbol: 'webcrypto_RAND_bytes', assetId: 'package:webcrypto/webcrypto.dart', ) external int RAND_bytes(ffi.Pointer buf, int len); -/// RSAPublicKey_dup allocates a fresh |RSA| and copies the public key from -/// |rsa| into it. It returns the fresh |RSA| object, or NULL on error. +/// RSAPublicKey_dup allocates a fresh `RSA` and copies the public key from +/// `rsa` into it. It returns the fresh `RSA` object, or NULL on error. @ffi.Native Function(ffi.Pointer)>( symbol: 'webcrypto_RSAPublicKey_dup', assetId: 'package:webcrypto/webcrypto.dart', ) external ffi.Pointer RSAPublicKey_dup(ffi.Pointer rsa); -/// RSA_check_key performs basic validity tests on |rsa|. It returns one if +/// RSA_check_key performs basic validity tests on `rsa`. It returns one if /// they pass and zero otherwise. Opaque keys and public keys always pass. If it /// returns zero then a more detailed error is available on the error queue. @ffi.Native)>( @@ -1807,7 +1845,7 @@ external ffi.Pointer RSAPublicKey_dup(ffi.Pointer rsa); ) external int RSA_check_key(ffi.Pointer rsa); -/// RSA_free decrements the reference count of |rsa| and frees it if the +/// RSA_free decrements the reference count of `rsa` and frees it if the /// reference count drops to zero. @ffi.Native)>( symbol: 'webcrypto_RSA_free', @@ -1816,11 +1854,11 @@ external int RSA_check_key(ffi.Pointer rsa); external void RSA_free(ffi.Pointer rsa); /// RSA_generate_key_ex generates a new RSA key where the modulus has size -/// |bits| and the public exponent is |e|. If unsure, |RSA_F4| is a good value -/// for |e|. If |cb| is not NULL then it is called during the key generation -/// process. In addition to the calls documented for |BN_generate_prime_ex|, it +/// `bits` and the public exponent is `e`. If unsure, `RSA_F4` is a good value +/// for `e`. If `cb` is not NULL then it is called during the key generation +/// process. In addition to the calls documented for `BN_generate_prime_ex`, it /// is called with event=2 when the n'th prime is rejected as unsuitable and -/// with event=3 when a suitable value for |p| is found. +/// with event=3 when a suitable value for `p` is found. /// /// It returns one on success or zero on error. @ffi.Native< @@ -1841,9 +1879,9 @@ external int RSA_generate_key_ex( ffi.Pointer cb, ); -/// RSA_get0_crt_params sets |*out_dmp1|, |*out_dmq1|, and |*out_iqmp|, if -/// non-NULL, to |rsa|'s CRT parameters. These are d (mod p-1), d (mod q-1) and -/// q^-1 (mod p), respectively. If |rsa| is a public key, each parameter will be +/// RSA_get0_crt_params sets `*out_dmp1`, `*out_dmq1`, and `*out_iqmp`, if +/// non-NULL, to `rsa`'s CRT parameters. These are d (mod p-1), d (mod q-1) and +/// q^-1 (mod p), respectively. If `rsa` is a public key, each parameter will be /// set to NULL. @ffi.Native< ffi.Void Function( @@ -1863,8 +1901,8 @@ external void RSA_get0_crt_params( ffi.Pointer> out_iqmp, ); -/// RSA_get0_factors sets |*out_p| and |*out_q|, if non-NULL, to |rsa|'s prime -/// factors. If |rsa| is a public key, they will be set to NULL. +/// RSA_get0_factors sets `*out_p` and `*out_q`, if non-NULL, to `rsa`'s prime +/// factors. If `rsa` is a public key, they will be set to NULL. @ffi.Native< ffi.Void Function( ffi.Pointer, @@ -1881,8 +1919,8 @@ external void RSA_get0_factors( ffi.Pointer> out_q, ); -/// RSA_get0_key sets |*out_n|, |*out_e|, and |*out_d|, if non-NULL, to |rsa|'s -/// modulus, public exponent, and private exponent, respectively. If |rsa| is a +/// RSA_get0_key sets `*out_n`, `*out_e`, and `*out_d`, if non-NULL, to `rsa`'s +/// modulus, public exponent, and private exponent, respectively. If `rsa` is a /// public key, the private exponent will be set to NULL. @ffi.Native< ffi.Void Function( @@ -1899,22 +1937,22 @@ external void RSA_get0_key( ffi.Pointer> out_d, ); -/// RSA_new returns a new, empty |RSA| object or NULL on error. Prefer using -/// |RSA_new_public_key| or |RSA_new_private_key| to import an RSA key. +/// RSA_new returns a new, empty `RSA` object or NULL on error. Prefer using +/// `RSA_new_public_key` or `RSA_new_private_key` to import an RSA key. @ffi.Native Function()>( symbol: 'webcrypto_RSA_new', assetId: 'package:webcrypto/webcrypto.dart', ) external ffi.Pointer RSA_new(); -/// RSA_set0_crt_params sets |rsa|'s CRT parameters to |dmp1|, |dmq1|, and -/// |iqmp|, if non-NULL, and takes ownership of them. On success, it takes +/// RSA_set0_crt_params sets `rsa`'s CRT parameters to `dmp1`, `dmq1`, and +/// `iqmp`, if non-NULL, and takes ownership of them. On success, it takes /// ownership of its parameters and returns one. Otherwise, it returns zero. /// -/// Each argument must either be non-NULL or already configured on |rsa|. +/// Each argument must either be non-NULL or already configured on `rsa`. /// -/// It is an error to call this function after |rsa| has been used for a -/// cryptographic operation. Construct a new |RSA| object instead. +/// It is an error to call this function after `rsa` has been used for a +/// cryptographic operation. Construct a new `RSA` object instead. @ffi.Native< ffi.Int Function( ffi.Pointer, @@ -1933,14 +1971,14 @@ external int RSA_set0_crt_params( ffi.Pointer iqmp, ); -/// RSA_set0_factors sets |rsa|'s prime factors to |p| and |q|, if non-NULL, and +/// RSA_set0_factors sets `rsa`'s prime factors to `p` and `q`, if non-NULL, and /// takes ownership of them. On success, it takes ownership of each argument and /// returns one. Otherwise, it returns zero. /// -/// Each argument must either be non-NULL or already configured on |rsa|. +/// Each argument must either be non-NULL or already configured on `rsa`. /// -/// It is an error to call this function after |rsa| has been used for a -/// cryptographic operation. Construct a new |RSA| object instead. +/// It is an error to call this function after `rsa` has been used for a +/// cryptographic operation. Construct a new `RSA` object instead. @ffi.Native< ffi.Int Function(ffi.Pointer, ffi.Pointer, ffi.Pointer) >( @@ -1953,15 +1991,15 @@ external int RSA_set0_factors( ffi.Pointer q, ); -/// RSA_set0_key sets |rsa|'s modulus, public exponent, and private exponent to -/// |n|, |e|, and |d| respectively, if non-NULL. On success, it takes ownership +/// RSA_set0_key sets `rsa`'s modulus, public exponent, and private exponent to +/// `n`, `e`, and `d` respectively, if non-NULL. On success, it takes ownership /// of each argument and returns one. Otherwise, it returns zero. /// -/// |d| may be NULL, but |n| and |e| must either be non-NULL or already -/// configured on |rsa|. +/// `d` may be NULL, but `n` and `e` must either be non-NULL or already +/// configured on `rsa`. /// -/// It is an error to call this function after |rsa| has been used for a -/// cryptographic operation. Construct a new |RSA| object instead. +/// It is an error to call this function after `rsa` has been used for a +/// cryptographic operation. Construct a new `RSA` object instead. @ffi.Native< ffi.Int Function( ffi.Pointer, @@ -2030,7 +2068,7 @@ const int RSA_PKCS1_PSS_PADDING = 6; /// md_data contains the hash-specific context. final class UnnamedUnion2 extends ffi.Union { - @ffi.Array.multi([208]) + @ffi.Array.multi([240]) external ffi.Array md_data; @ffi.Uint64() @@ -2041,28 +2079,28 @@ final class bignum_ctx extends ffi.Opaque {} /// Private functions final class bignum_st extends ffi.Struct { - /// d is a pointer to an array of |width| |BN_BITS2|-bit chunks in + /// d is a pointer to an array of `width` `BN_BITS2`-bit chunks in /// little-endian order. This stores the absolute value of the number. external ffi.Pointer d; - /// width is the number of elements of |d| which are valid. This value is not - /// necessarily minimal; the most-significant words of |d| may be zero. - /// |width| determines a potentially loose upper-bound on the absolute value - /// of the |BIGNUM|. + /// width is the number of elements of `d` which are valid. This value is not + /// necessarily minimal; the most-significant words of `d` may be zero. + /// `width` determines a potentially loose upper-bound on the absolute value + /// of the `BIGNUM`. /// - /// Functions taking |BIGNUM| inputs must compute the same answer for all - /// possible widths. |bn_minimal_width|, |bn_set_minimal_width|, and other + /// Functions taking `BIGNUM` inputs must compute the same answer for all + /// possible widths. `bn_minimal_width`, `bn_set_minimal_width`, and other /// helpers may be used to recover the minimal width, provided it is not /// secret. If it is secret, use a different algorithm. Functions may output - /// minimal or non-minimal |BIGNUM|s depending on secrecy requirements, but + /// minimal or non-minimal `BIGNUM`s depending on secrecy requirements, but /// those which cause widths to unboundedly grow beyond the minimal value /// should be documented such. /// - /// Note this is different from historical |BIGNUM| semantics. + /// Note this is different from historical `BIGNUM` semantics. @ffi.Int() external int width; - /// dmax is number of elements of |d| which are allocated. + /// dmax is number of elements of `d` which are allocated. @ffi.Int() external int dmax; @@ -2070,17 +2108,17 @@ final class bignum_st extends ffi.Struct { @ffi.Int() external int neg; - /// flags is a bitmask of |BN_FLG_*| values + /// flags is a bitmask of `BN_FLG_*` values @ffi.Int() external int flags; } -/// bn_gencb_st, or |BN_GENCB|, holds a callback function that is used by +/// bn_gencb_st, or `BN_GENCB`, holds a callback function that is used by /// generation functions that can take a very long time to complete. Use -/// |BN_GENCB_set| to initialise a |BN_GENCB| structure. +/// `BN_GENCB_set` to initialise a `BN_GENCB` structure. /// -/// The callback receives the address of that |BN_GENCB| structure as its last -/// argument and the user is free to put an arbitrary pointer in |arg|. The other +/// The callback receives the address of that `BN_GENCB` structure as its last +/// argument and the user is free to put an arbitrary pointer in `arg`. The other /// arguments are set as follows: /// - event=BN_GENCB_GENERATED, n=i: after generating the i'th possible prime /// number. @@ -2097,18 +2135,18 @@ final class bn_gencb_st extends ffi.Opaque {} /// CRYPTO ByteBuilder. /// -/// |CBB| objects allow one to build length-prefixed serialisations. A |CBB| +/// `CBB` objects allow one to build length-prefixed serialisations. A `CBB` /// object is associated with a buffer and new buffers are created with -/// |CBB_init|. Several |CBB| objects can point at the same buffer when a -/// length-prefix is pending, however only a single |CBB| can be 'current' at -/// any one time. For example, if one calls |CBB_add_u8_length_prefixed| then -/// the new |CBB| points at the same buffer as the original. But if the original -/// |CBB| is used then the length prefix is written out and the new |CBB| must +/// `CBB_init`. Several `CBB` objects can point at the same buffer when a +/// length-prefix is pending, however only a single `CBB` can be 'current' at +/// any one time. For example, if one calls `CBB_add_u8_length_prefixed` then +/// the new `CBB` points at the same buffer as the original. But if the original +/// `CBB` is used then the length prefix is written out and the new `CBB` must /// not be used again. /// -/// If one needs to force a length prefix to be written out because a |CBB| is -/// going out of scope, use |CBB_flush|. If an operation on a |CBB| fails, it is -/// in an undefined state and must not be used except to call |CBB_cleanup|. +/// If one needs to force a length prefix to be written out because a `CBB` is +/// going out of scope, use `CBB_flush`. If an operation on a `CBB` fails, it is +/// in an undefined state and must not be used except to call `CBB_cleanup`. final class cbb_buffer_st extends ffi.Opaque {} final class cbb_child_st extends ffi.Opaque {} @@ -2131,7 +2169,7 @@ final class ec_point_st extends ffi.Opaque {} /// Low-level signing and verification. /// -/// Low-level functions handle signatures as |ECDSA_SIG| structures which allow +/// Low-level functions handle signatures as `ECDSA_SIG` structures which allow /// the two values in an ECDSA signature to be handled separately. final class ecdsa_sig_st extends ffi.Struct { external ffi.Pointer r; @@ -2156,13 +2194,13 @@ final class env_md_ctx_st extends ffi.Struct { external ffi.Pointer pctx; /// pctx_ops, if not NULL, points to a vtable that contains functions to - /// manipulate |pctx|. + /// manipulate `pctx`. external ffi.Pointer pctx_ops; } final class env_md_st extends ffi.Opaque {} -/// An evp_aead_ctx_st (typedefed as |EVP_AEAD_CTX| in base.h) represents an AEAD +/// An evp_aead_ctx_st (typedefed as `EVP_AEAD_CTX` in base.h) represents an AEAD /// algorithm configured with a specific key and message-independent IV. final class evp_aead_ctx_st extends ffi.Struct { external ffi.Pointer aead; @@ -2193,11 +2231,11 @@ final class evp_cipher_ctx_st extends ffi.Struct { /// application stuff external ffi.Pointer app_data; - /// cipher_data points to the |cipher| specific state. + /// cipher_data points to the `cipher` specific state. external ffi.Pointer cipher_data; /// key_len contains the length of the key, which may differ from - /// |cipher->key_len| if the cipher can take a variable key length. + /// `cipher->key_len` if the cipher can take a variable key length. @ffi.UnsignedInt() external int key_len; @@ -2205,7 +2243,7 @@ final class evp_cipher_ctx_st extends ffi.Struct { @ffi.Int() external int encrypt; - /// flags contains the OR of zero or more |EVP_CIPH_*| flags, above. + /// flags contains the OR of zero or more `EVP_CIPH_*` flags, above. @ffi.Uint32() external int flags; @@ -2223,26 +2261,22 @@ final class evp_cipher_ctx_st extends ffi.Struct { external ffi.Array buf; /// buf_len contains the number of bytes of a partial block contained in - /// |buf|. + /// `buf`. @ffi.Int() external int buf_len; - /// num contains the number of bytes of |iv| which are valid for modes that + /// num contains the number of bytes of `iv` which are valid for modes that /// manage partial blocks themselves. @ffi.UnsignedInt() external int num; - /// final_used is non-zero if the |final| buffer contains plaintext. + /// final_used is non-zero if the `final` buffer contains plaintext. @ffi.Int() external int final_used; /// possible final block @ffi.Array.multi([32]) external ffi.Array final1; - - /// Has this structure been rendered unusable by a failure. - @ffi.Int() - external int poisoned; } final class evp_cipher_st extends ffi.Opaque {} @@ -2266,7 +2300,7 @@ final class hmac_ctx_st extends ffi.Struct { } /// point_conversion_form_t enumerates forms, as defined in X9.62 (ECDSA), for -/// the encoding of a elliptic curve point (x,y) +/// the encoding of an elliptic curve point (x,y) abstract class point_conversion_form_t { /// POINT_CONVERSION_COMPRESSED indicates that the point is encoded as z||x, /// where the octet z specifies which solution of the quadratic equation y diff --git a/third_party/boringssl/gen/bcm/aes-gcm-avx2-x86_64-apple.S b/third_party/boringssl/gen/bcm/aes-gcm-avx2-x86_64-apple.S index eb2ac3bdb..e50dca2b7 100644 --- a/third_party/boringssl/gen/bcm/aes-gcm-avx2-x86_64-apple.S +++ b/third_party/boringssl/gen/bcm/aes-gcm-avx2-x86_64-apple.S @@ -5,28 +5,30 @@ #if !defined(OPENSSL_NO_ASM) && defined(OPENSSL_X86_64) && defined(__APPLE__) .section __DATA,__const -.p2align 4 +.p2align 4 +aes_gcm_avx2_constants: +// A shuffle mask that reflects the bytes of 16-byte blocks L$bswap_mask: .quad 0x08090a0b0c0d0e0f, 0x0001020304050607 - - - - - - - +// This is the GHASH reducing polynomial without its constant term, i.e. +// x^128 + x^7 + x^2 + x, represented using the backwards mapping +// between bits and polynomial coefficients. +// +// Alternatively, it can be interpreted as the naturally-ordered +// representation of the polynomial x^127 + x^126 + x^121 + 1, i.e. the +// "reversed" GHASH reducing polynomial without its x^128 term. L$gfpoly: .quad 1, 0xc200000000000000 - +// Same as above, but with the (1 << 64) bit set. L$gfpoly_and_internal_carrybit: .quad 1, 0xc200000000000001 .p2align 5 - +// The below constants are used for incrementing the counter blocks. L$ctr_pattern: .quad 0, 0 .quad 1, 0 @@ -46,14 +48,14 @@ _CET_ENDBR - - +// Load the byte-reflected hash subkey. BoringSSL provides it in +// byte-reflected form except the two halves are in the wrong order. vpshufd $0x4e,(%rsi),%xmm3 - - - - +// Finish preprocessing the byte-reflected hash subkey by multiplying it by +// x^-1 ("standard" interpretation of polynomial coefficients) or +// equivalently x^1 (natural interpretation). This gets the key into a +// format that avoids having to bit-reflect the data blocks later. vpshufd $0xd3,%xmm3,%xmm0 vpsrad $31,%xmm0,%xmm0 vpaddq %xmm3,%xmm3,%xmm3 @@ -62,83 +64,83 @@ _CET_ENDBR vbroadcasti128 L$gfpoly(%rip),%ymm6 - - vpclmulqdq $0x00,%xmm3,%xmm3,%xmm0 - vpclmulqdq $0x11,%xmm3,%xmm3,%xmm5 - vpclmulqdq $0x01,%xmm0,%xmm6,%xmm1 - vpshufd $0x4e,%xmm0,%xmm0 - vpxor %xmm0,%xmm1,%xmm1 - vpclmulqdq $0x01,%xmm1,%xmm6,%xmm0 - vpshufd $0x4e,%xmm1,%xmm1 - vpxor %xmm1,%xmm5,%xmm5 - vpxor %xmm0,%xmm5,%xmm5 - +// Square H^1 to get H^2. + vpclmulqdq $0x00,%xmm3,%xmm3,%xmm0 // LO = a_L * a_L + vpclmulqdq $0x11,%xmm3,%xmm3,%xmm5 // HI = a_H * a_H + vpclmulqdq $0x01,%xmm0,%xmm6,%xmm1 // LO_L*(x^63 + x^62 + x^57) + vpshufd $0x4e,%xmm0,%xmm0 // Swap halves of LO + vpxor %xmm0,%xmm1,%xmm1 // Fold LO into MI + vpclmulqdq $0x01,%xmm1,%xmm6,%xmm0 // MI_L*(x^63 + x^62 + x^57) + vpshufd $0x4e,%xmm1,%xmm1 // Swap halves of MI + vpxor %xmm1,%xmm5,%xmm5 // Fold MI into HI (part 1) + vpxor %xmm0,%xmm5,%xmm5 // Fold MI into HI (part 2) +// Create H_CUR = [H^2, H^1] and H_INC = [H^2, H^2]. vinserti128 $1,%xmm3,%ymm5,%ymm3 vinserti128 $1,%xmm5,%ymm5,%ymm5 - - vpclmulqdq $0x00,%ymm5,%ymm3,%ymm0 - vpclmulqdq $0x01,%ymm5,%ymm3,%ymm1 - vpclmulqdq $0x10,%ymm5,%ymm3,%ymm2 - vpxor %ymm2,%ymm1,%ymm1 - vpclmulqdq $0x01,%ymm0,%ymm6,%ymm2 - vpshufd $0x4e,%ymm0,%ymm0 - vpxor %ymm0,%ymm1,%ymm1 - vpxor %ymm2,%ymm1,%ymm1 - vpclmulqdq $0x11,%ymm5,%ymm3,%ymm4 - vpclmulqdq $0x01,%ymm1,%ymm6,%ymm0 - vpshufd $0x4e,%ymm1,%ymm1 - vpxor %ymm1,%ymm4,%ymm4 - vpxor %ymm0,%ymm4,%ymm4 - - - +// Compute H_CUR2 = [H^4, H^3]. + vpclmulqdq $0x00,%ymm5,%ymm3,%ymm0 // LO = a_L * b_L + vpclmulqdq $0x01,%ymm5,%ymm3,%ymm1 // MI_0 = a_L * b_H + vpclmulqdq $0x10,%ymm5,%ymm3,%ymm2 // MI_1 = a_H * b_L + vpxor %ymm2,%ymm1,%ymm1 // MI = MI_0 + MI_1 + vpclmulqdq $0x01,%ymm0,%ymm6,%ymm2 // LO_L*(x^63 + x^62 + x^57) + vpshufd $0x4e,%ymm0,%ymm0 // Swap halves of LO + vpxor %ymm0,%ymm1,%ymm1 // Fold LO into MI (part 1) + vpxor %ymm2,%ymm1,%ymm1 // Fold LO into MI (part 2) + vpclmulqdq $0x11,%ymm5,%ymm3,%ymm4 // HI = a_H * b_H + vpclmulqdq $0x01,%ymm1,%ymm6,%ymm0 // MI_L*(x^63 + x^62 + x^57) + vpshufd $0x4e,%ymm1,%ymm1 // Swap halves of MI + vpxor %ymm1,%ymm4,%ymm4 // Fold MI into HI (part 1) + vpxor %ymm0,%ymm4,%ymm4 // Fold MI into HI (part 2) + + +// Store [H^2, H^1] and [H^4, H^3]. vmovdqu %ymm3,96(%rdi) vmovdqu %ymm4,64(%rdi) - - +// For Karatsuba multiplication: compute and store the two 64-bit halves of +// each key power XOR'd together. Order is 4,2,3,1. vpunpcklqdq %ymm3,%ymm4,%ymm0 vpunpckhqdq %ymm3,%ymm4,%ymm1 vpxor %ymm1,%ymm0,%ymm0 vmovdqu %ymm0,128+32(%rdi) - - vpclmulqdq $0x00,%ymm5,%ymm4,%ymm0 - vpclmulqdq $0x01,%ymm5,%ymm4,%ymm1 - vpclmulqdq $0x10,%ymm5,%ymm4,%ymm2 - vpxor %ymm2,%ymm1,%ymm1 - vpclmulqdq $0x01,%ymm0,%ymm6,%ymm2 - vpshufd $0x4e,%ymm0,%ymm0 - vpxor %ymm0,%ymm1,%ymm1 - vpxor %ymm2,%ymm1,%ymm1 - vpclmulqdq $0x11,%ymm5,%ymm4,%ymm3 - vpclmulqdq $0x01,%ymm1,%ymm6,%ymm0 - vpshufd $0x4e,%ymm1,%ymm1 - vpxor %ymm1,%ymm3,%ymm3 - vpxor %ymm0,%ymm3,%ymm3 - - vpclmulqdq $0x00,%ymm5,%ymm3,%ymm0 - vpclmulqdq $0x01,%ymm5,%ymm3,%ymm1 - vpclmulqdq $0x10,%ymm5,%ymm3,%ymm2 - vpxor %ymm2,%ymm1,%ymm1 - vpclmulqdq $0x01,%ymm0,%ymm6,%ymm2 - vpshufd $0x4e,%ymm0,%ymm0 - vpxor %ymm0,%ymm1,%ymm1 - vpxor %ymm2,%ymm1,%ymm1 - vpclmulqdq $0x11,%ymm5,%ymm3,%ymm4 - vpclmulqdq $0x01,%ymm1,%ymm6,%ymm0 - vpshufd $0x4e,%ymm1,%ymm1 - vpxor %ymm1,%ymm4,%ymm4 - vpxor %ymm0,%ymm4,%ymm4 +// Compute and store H_CUR = [H^6, H^5] and H_CUR2 = [H^8, H^7]. + vpclmulqdq $0x00,%ymm5,%ymm4,%ymm0 // LO = a_L * b_L + vpclmulqdq $0x01,%ymm5,%ymm4,%ymm1 // MI_0 = a_L * b_H + vpclmulqdq $0x10,%ymm5,%ymm4,%ymm2 // MI_1 = a_H * b_L + vpxor %ymm2,%ymm1,%ymm1 // MI = MI_0 + MI_1 + vpclmulqdq $0x01,%ymm0,%ymm6,%ymm2 // LO_L*(x^63 + x^62 + x^57) + vpshufd $0x4e,%ymm0,%ymm0 // Swap halves of LO + vpxor %ymm0,%ymm1,%ymm1 // Fold LO into MI (part 1) + vpxor %ymm2,%ymm1,%ymm1 // Fold LO into MI (part 2) + vpclmulqdq $0x11,%ymm5,%ymm4,%ymm3 // HI = a_H * b_H + vpclmulqdq $0x01,%ymm1,%ymm6,%ymm0 // MI_L*(x^63 + x^62 + x^57) + vpshufd $0x4e,%ymm1,%ymm1 // Swap halves of MI + vpxor %ymm1,%ymm3,%ymm3 // Fold MI into HI (part 1) + vpxor %ymm0,%ymm3,%ymm3 // Fold MI into HI (part 2) + + vpclmulqdq $0x00,%ymm5,%ymm3,%ymm0 // LO = a_L * b_L + vpclmulqdq $0x01,%ymm5,%ymm3,%ymm1 // MI_0 = a_L * b_H + vpclmulqdq $0x10,%ymm5,%ymm3,%ymm2 // MI_1 = a_H * b_L + vpxor %ymm2,%ymm1,%ymm1 // MI = MI_0 + MI_1 + vpclmulqdq $0x01,%ymm0,%ymm6,%ymm2 // LO_L*(x^63 + x^62 + x^57) + vpshufd $0x4e,%ymm0,%ymm0 // Swap halves of LO + vpxor %ymm0,%ymm1,%ymm1 // Fold LO into MI (part 1) + vpxor %ymm2,%ymm1,%ymm1 // Fold LO into MI (part 2) + vpclmulqdq $0x11,%ymm5,%ymm3,%ymm4 // HI = a_H * b_H + vpclmulqdq $0x01,%ymm1,%ymm6,%ymm0 // MI_L*(x^63 + x^62 + x^57) + vpshufd $0x4e,%ymm1,%ymm1 // Swap halves of MI + vpxor %ymm1,%ymm4,%ymm4 // Fold MI into HI (part 1) + vpxor %ymm0,%ymm4,%ymm4 // Fold MI into HI (part 2) vmovdqu %ymm3,32(%rdi) vmovdqu %ymm4,0(%rdi) - - +// Again, compute and store the two 64-bit halves of each key power XOR'd +// together. Order is 8,6,7,5. vpunpcklqdq %ymm3,%ymm4,%ymm0 vpunpckhqdq %ymm3,%ymm4,%ymm1 vpxor %ymm1,%ymm0,%ymm0 @@ -153,6 +155,7 @@ _CET_ENDBR .private_extern _gcm_gmult_vpclmulqdq_avx2 .p2align 5 +.alt_entry _gcm_gmult_vpclmulqdq_avx2 _gcm_gmult_vpclmulqdq_avx2: @@ -166,25 +169,25 @@ _CET_ENDBR vmovdqu L$gfpoly(%rip),%xmm3 vpshufb %xmm1,%xmm0,%xmm0 - vpclmulqdq $0x00,%xmm2,%xmm0,%xmm4 - vpclmulqdq $0x01,%xmm2,%xmm0,%xmm5 - vpclmulqdq $0x10,%xmm2,%xmm0,%xmm6 - vpxor %xmm6,%xmm5,%xmm5 - vpclmulqdq $0x01,%xmm4,%xmm3,%xmm6 - vpshufd $0x4e,%xmm4,%xmm4 - vpxor %xmm4,%xmm5,%xmm5 - vpxor %xmm6,%xmm5,%xmm5 - vpclmulqdq $0x11,%xmm2,%xmm0,%xmm0 - vpclmulqdq $0x01,%xmm5,%xmm3,%xmm4 - vpshufd $0x4e,%xmm5,%xmm5 - vpxor %xmm5,%xmm0,%xmm0 - vpxor %xmm4,%xmm0,%xmm0 + vpclmulqdq $0x00,%xmm2,%xmm0,%xmm4 // LO = a_L * b_L + vpclmulqdq $0x01,%xmm2,%xmm0,%xmm5 // MI_0 = a_L * b_H + vpclmulqdq $0x10,%xmm2,%xmm0,%xmm6 // MI_1 = a_H * b_L + vpxor %xmm6,%xmm5,%xmm5 // MI = MI_0 + MI_1 + vpclmulqdq $0x01,%xmm4,%xmm3,%xmm6 // LO_L*(x^63 + x^62 + x^57) + vpshufd $0x4e,%xmm4,%xmm4 // Swap halves of LO + vpxor %xmm4,%xmm5,%xmm5 // Fold LO into MI (part 1) + vpxor %xmm6,%xmm5,%xmm5 // Fold LO into MI (part 2) + vpclmulqdq $0x11,%xmm2,%xmm0,%xmm0 // HI = a_H * b_H + vpclmulqdq $0x01,%xmm5,%xmm3,%xmm4 // MI_L*(x^63 + x^62 + x^57) + vpshufd $0x4e,%xmm5,%xmm5 // Swap halves of MI + vpxor %xmm5,%xmm0,%xmm0 // Fold MI into HI (part 1) + vpxor %xmm4,%xmm0,%xmm0 // Fold MI into HI (part 2) vpshufb %xmm1,%xmm0,%xmm0 vmovdqu %xmm0,(%rdi) - +// No need for vzeroupper, since only xmm registers were used. ret @@ -193,6 +196,7 @@ _CET_ENDBR .private_extern _gcm_ghash_vpclmulqdq_avx2 .p2align 5 +.alt_entry _gcm_ghash_vpclmulqdq_avx2 _gcm_ghash_vpclmulqdq_avx2: @@ -200,33 +204,33 @@ _CET_ENDBR - - - +// Load the bswap_mask and gfpoly constants. Since AADLEN is usually small, +// usually only 128-bit vectors will be used. So as an optimization, don't +// broadcast these constants to both 128-bit lanes quite yet. vmovdqu L$bswap_mask(%rip),%xmm6 vmovdqu L$gfpoly(%rip),%xmm7 - +// Load the GHASH accumulator. vmovdqu (%rdi),%xmm5 vpshufb %xmm6,%xmm5,%xmm5 - +// Optimize for AADLEN < 32 by checking for AADLEN < 32 before AADLEN < 128. cmpq $32,%rcx jb L$ghash_lastblock - - +// AADLEN >= 32, so we'll operate on full vectors. Broadcast bswap_mask and +// gfpoly to both 128-bit lanes. vinserti128 $1,%xmm6,%ymm6,%ymm6 vinserti128 $1,%xmm7,%ymm7,%ymm7 cmpq $127,%rcx jbe L$ghash_loop_1x - +// Update GHASH with 128 bytes of AAD at a time. vmovdqu 128(%rsi),%ymm8 vmovdqu 128+32(%rsi),%ymm9 L$ghash_loop_4x: - +// First vector vmovdqu 0(%rdx),%ymm1 vpshufb %ymm6,%ymm1,%ymm1 vmovdqu 0(%rsi),%ymm2 @@ -236,7 +240,7 @@ L$ghash_loop_4x: vpunpckhqdq %ymm1,%ymm1,%ymm0 vpxor %ymm1,%ymm0,%ymm0 vpclmulqdq $0x00,%ymm8,%ymm0,%ymm4 - +// Second vector vmovdqu 32(%rdx),%ymm1 vpshufb %ymm6,%ymm1,%ymm1 vmovdqu 32(%rsi),%ymm2 @@ -248,7 +252,7 @@ L$ghash_loop_4x: vpxor %ymm1,%ymm0,%ymm0 vpclmulqdq $0x10,%ymm8,%ymm0,%ymm0 vpxor %ymm0,%ymm4,%ymm4 - +// Third vector vmovdqu 64(%rdx),%ymm1 vpshufb %ymm6,%ymm1,%ymm1 vmovdqu 64(%rsi),%ymm2 @@ -261,7 +265,7 @@ L$ghash_loop_4x: vpclmulqdq $0x00,%ymm9,%ymm0,%ymm0 vpxor %ymm0,%ymm4,%ymm4 - +// Fourth vector vmovdqu 96(%rdx),%ymm1 vpshufb %ymm6,%ymm1,%ymm1 vmovdqu 96(%rsi),%ymm2 @@ -273,17 +277,17 @@ L$ghash_loop_4x: vpxor %ymm1,%ymm0,%ymm0 vpclmulqdq $0x10,%ymm9,%ymm0,%ymm0 vpxor %ymm0,%ymm4,%ymm4 - +// Finalize 'mi' following Karatsuba multiplication. vpxor %ymm3,%ymm4,%ymm4 vpxor %ymm5,%ymm4,%ymm4 - +// Fold lo into mi. vbroadcasti128 L$gfpoly(%rip),%ymm2 vpclmulqdq $0x01,%ymm3,%ymm2,%ymm0 vpshufd $0x4e,%ymm3,%ymm3 vpxor %ymm3,%ymm4,%ymm4 vpxor %ymm0,%ymm4,%ymm4 - +// Fold mi into hi. vpclmulqdq $0x01,%ymm4,%ymm2,%ymm0 vpshufd $0x4e,%ymm4,%ymm4 vpxor %ymm4,%ymm5,%ymm5 @@ -291,12 +295,12 @@ L$ghash_loop_4x: vextracti128 $1,%ymm5,%xmm0 vpxor %xmm0,%xmm5,%xmm5 - subq $-128,%rdx + subq $-128,%rdx // 128 is 4 bytes, -128 is 1 byte addq $-128,%rcx cmpq $127,%rcx ja L$ghash_loop_4x - +// Update GHASH with 32 bytes of AAD at a time. cmpq $32,%rcx jb L$ghash_loop_1x_done L$ghash_loop_1x: @@ -304,19 +308,19 @@ L$ghash_loop_1x: vpshufb %ymm6,%ymm0,%ymm0 vpxor %ymm0,%ymm5,%ymm5 vmovdqu 128-32(%rsi),%ymm0 - vpclmulqdq $0x00,%ymm0,%ymm5,%ymm1 - vpclmulqdq $0x01,%ymm0,%ymm5,%ymm2 - vpclmulqdq $0x10,%ymm0,%ymm5,%ymm3 - vpxor %ymm3,%ymm2,%ymm2 - vpclmulqdq $0x01,%ymm1,%ymm7,%ymm3 - vpshufd $0x4e,%ymm1,%ymm1 - vpxor %ymm1,%ymm2,%ymm2 - vpxor %ymm3,%ymm2,%ymm2 - vpclmulqdq $0x11,%ymm0,%ymm5,%ymm5 - vpclmulqdq $0x01,%ymm2,%ymm7,%ymm1 - vpshufd $0x4e,%ymm2,%ymm2 - vpxor %ymm2,%ymm5,%ymm5 - vpxor %ymm1,%ymm5,%ymm5 + vpclmulqdq $0x00,%ymm0,%ymm5,%ymm1 // LO = a_L * b_L + vpclmulqdq $0x01,%ymm0,%ymm5,%ymm2 // MI_0 = a_L * b_H + vpclmulqdq $0x10,%ymm0,%ymm5,%ymm3 // MI_1 = a_H * b_L + vpxor %ymm3,%ymm2,%ymm2 // MI = MI_0 + MI_1 + vpclmulqdq $0x01,%ymm1,%ymm7,%ymm3 // LO_L*(x^63 + x^62 + x^57) + vpshufd $0x4e,%ymm1,%ymm1 // Swap halves of LO + vpxor %ymm1,%ymm2,%ymm2 // Fold LO into MI (part 1) + vpxor %ymm3,%ymm2,%ymm2 // Fold LO into MI (part 2) + vpclmulqdq $0x11,%ymm0,%ymm5,%ymm5 // HI = a_H * b_H + vpclmulqdq $0x01,%ymm2,%ymm7,%ymm1 // MI_L*(x^63 + x^62 + x^57) + vpshufd $0x4e,%ymm2,%ymm2 // Swap halves of MI + vpxor %ymm2,%ymm5,%ymm5 // Fold MI into HI (part 1) + vpxor %ymm1,%ymm5,%ymm5 // Fold MI into HI (part 2) vextracti128 $1,%ymm5,%xmm0 vpxor %xmm0,%xmm5,%xmm5 @@ -326,7 +330,7 @@ L$ghash_loop_1x: jae L$ghash_loop_1x L$ghash_loop_1x_done: - +// Update GHASH with the remaining 16-byte block if any. L$ghash_lastblock: testq %rcx,%rcx jz L$ghash_done @@ -334,23 +338,23 @@ L$ghash_lastblock: vpshufb %xmm6,%xmm0,%xmm0 vpxor %xmm0,%xmm5,%xmm5 vmovdqu 128-16(%rsi),%xmm0 - vpclmulqdq $0x00,%xmm0,%xmm5,%xmm1 - vpclmulqdq $0x01,%xmm0,%xmm5,%xmm2 - vpclmulqdq $0x10,%xmm0,%xmm5,%xmm3 - vpxor %xmm3,%xmm2,%xmm2 - vpclmulqdq $0x01,%xmm1,%xmm7,%xmm3 - vpshufd $0x4e,%xmm1,%xmm1 - vpxor %xmm1,%xmm2,%xmm2 - vpxor %xmm3,%xmm2,%xmm2 - vpclmulqdq $0x11,%xmm0,%xmm5,%xmm5 - vpclmulqdq $0x01,%xmm2,%xmm7,%xmm1 - vpshufd $0x4e,%xmm2,%xmm2 - vpxor %xmm2,%xmm5,%xmm5 - vpxor %xmm1,%xmm5,%xmm5 + vpclmulqdq $0x00,%xmm0,%xmm5,%xmm1 // LO = a_L * b_L + vpclmulqdq $0x01,%xmm0,%xmm5,%xmm2 // MI_0 = a_L * b_H + vpclmulqdq $0x10,%xmm0,%xmm5,%xmm3 // MI_1 = a_H * b_L + vpxor %xmm3,%xmm2,%xmm2 // MI = MI_0 + MI_1 + vpclmulqdq $0x01,%xmm1,%xmm7,%xmm3 // LO_L*(x^63 + x^62 + x^57) + vpshufd $0x4e,%xmm1,%xmm1 // Swap halves of LO + vpxor %xmm1,%xmm2,%xmm2 // Fold LO into MI (part 1) + vpxor %xmm3,%xmm2,%xmm2 // Fold LO into MI (part 2) + vpclmulqdq $0x11,%xmm0,%xmm5,%xmm5 // HI = a_H * b_H + vpclmulqdq $0x01,%xmm2,%xmm7,%xmm1 // MI_L*(x^63 + x^62 + x^57) + vpshufd $0x4e,%xmm2,%xmm2 // Swap halves of MI + vpxor %xmm2,%xmm5,%xmm5 // Fold MI into HI (part 1) + vpxor %xmm1,%xmm5,%xmm5 // Fold MI into HI (part 2) L$ghash_done: - +// Store the updated GHASH accumulator back to memory. vpshufb %xmm6,%xmm5,%xmm5 vmovdqu %xmm5,(%rdi) @@ -363,6 +367,7 @@ L$ghash_done: .private_extern _aes_gcm_enc_update_vaes_avx2 .p2align 5 +.alt_entry _aes_gcm_enc_update_vaes_avx2 _aes_gcm_enc_update_vaes_avx2: @@ -370,45 +375,45 @@ _CET_ENDBR pushq %r12 - movq 16(%rsp),%r12 + movq 16(%rsp),%r12 // arg7 #ifdef BORINGSSL_DISPATCH_TEST movb $1,_BORINGSSL_function_hit+6(%rip) #endif vbroadcasti128 L$bswap_mask(%rip),%ymm0 - - +// Load the GHASH accumulator and the starting counter. +// BoringSSL passes these values in big endian format. vmovdqu (%r12),%xmm1 vpshufb %xmm0,%xmm1,%xmm1 vbroadcasti128 (%r8),%ymm11 vpshufb %ymm0,%ymm11,%ymm11 - - +// Load the AES key length in bytes. BoringSSL stores number of rounds +// minus 1, so convert using: AESKEYLEN = 4 * aeskey->rounds - 20. movl 240(%rcx),%r10d leal -20(,%r10,4),%r10d - - - +// Make RNDKEYLAST_PTR point to the last AES round key. This is the +// round key with index 10, 12, or 14 for AES-128, AES-192, or AES-256 +// respectively. Then load the zero-th and last round keys. leaq 96(%rcx,%r10,4),%r11 vbroadcasti128 (%rcx),%ymm9 vbroadcasti128 (%r11),%ymm10 - +// Finish initializing LE_CTR by adding 1 to the second block. vpaddd L$ctr_pattern(%rip),%ymm11,%ymm11 - - +// If there are at least 128 bytes of data, then continue into the loop that +// processes 128 bytes of data at a time. Otherwise skip it. cmpq $127,%rdx jbe L$crypt_loop_4x_done__func1 vmovdqu 128(%r9),%ymm7 vmovdqu 128+32(%r9),%ymm8 - - - +// Encrypt the first 4 vectors of plaintext blocks. +// Increment le_ctr four times to generate four vectors of little-endian +// counter blocks, swap each to big-endian, and store them in aesdata[0-3]. vmovdqu L$inc_2blocks(%rip),%ymm2 vpshufb %ymm0,%ymm11,%ymm12 vpaddd %ymm2,%ymm11,%ymm11 @@ -419,7 +424,7 @@ _CET_ENDBR vpshufb %ymm0,%ymm11,%ymm15 vpaddd %ymm2,%ymm11,%ymm11 - +// AES "round zero": XOR in the zero-th round key. vpxor %ymm9,%ymm12,%ymm12 vpxor %ymm9,%ymm13,%ymm13 vpxor %ymm9,%ymm14,%ymm14 @@ -449,16 +454,16 @@ L$vaesenc_loop_first_4_vecs__func1: vmovdqu %ymm14,64(%rsi) vmovdqu %ymm15,96(%rsi) - subq $-128,%rdi + subq $-128,%rdi // 128 is 4 bytes, -128 is 1 byte addq $-128,%rdx cmpq $127,%rdx jbe L$ghash_last_ciphertext_4x__func1 .p2align 4 L$crypt_loop_4x__func1: - - - +// Start the AES encryption of the counter blocks. +// Increment le_ctr four times to generate four vectors of little-endian +// counter blocks, swap each to big-endian, and store them in aesdata[0-3]. vmovdqu L$inc_2blocks(%rip),%ymm2 vpshufb %ymm0,%ymm11,%ymm12 vpaddd %ymm2,%ymm11,%ymm11 @@ -469,7 +474,7 @@ L$crypt_loop_4x__func1: vpshufb %ymm0,%ymm11,%ymm15 vpaddd %ymm2,%ymm11,%ymm11 - +// AES "round zero": XOR in the zero-th round key. vpxor %ymm9,%ymm12,%ymm12 vpxor %ymm9,%ymm13,%ymm13 vpxor %ymm9,%ymm14,%ymm14 @@ -478,7 +483,7 @@ L$crypt_loop_4x__func1: cmpl $24,%r10d jl L$aes128__func1 je L$aes192__func1 - +// AES-256 vbroadcasti128 -208(%r11),%ymm2 vaesenc %ymm2,%ymm12,%ymm12 vaesenc %ymm2,%ymm13,%ymm13 @@ -507,7 +512,7 @@ L$aes192__func1: L$aes128__func1: prefetcht0 512(%rdi) prefetcht0 512+64(%rdi) - +// First vector vmovdqu 0(%rsi),%ymm3 vpshufb %ymm0,%ymm3,%ymm3 vmovdqu 0(%r9),%ymm4 @@ -531,7 +536,7 @@ L$aes128__func1: vaesenc %ymm2,%ymm14,%ymm14 vaesenc %ymm2,%ymm15,%ymm15 - +// Second vector vmovdqu 32(%rsi),%ymm3 vpshufb %ymm0,%ymm3,%ymm3 vmovdqu 32(%r9),%ymm4 @@ -550,7 +555,7 @@ L$aes128__func1: vaesenc %ymm2,%ymm14,%ymm14 vaesenc %ymm2,%ymm15,%ymm15 - +// Third vector vmovdqu 64(%rsi),%ymm3 vpshufb %ymm0,%ymm3,%ymm3 vmovdqu 64(%r9),%ymm4 @@ -577,7 +582,7 @@ L$aes128__func1: vpclmulqdq $0x00,%ymm8,%ymm2,%ymm2 vpxor %ymm2,%ymm6,%ymm6 - +// Fourth vector vmovdqu 96(%rsi),%ymm3 vpshufb %ymm0,%ymm3,%ymm3 @@ -603,11 +608,11 @@ L$aes128__func1: vaesenc %ymm2,%ymm14,%ymm14 vaesenc %ymm2,%ymm15,%ymm15 - +// Finalize 'mi' following Karatsuba multiplication. vpxor %ymm5,%ymm6,%ymm6 vpxor %ymm1,%ymm6,%ymm6 - +// Fold lo into mi. vbroadcasti128 L$gfpoly(%rip),%ymm4 vpclmulqdq $0x01,%ymm5,%ymm4,%ymm2 vpshufd $0x4e,%ymm5,%ymm5 @@ -620,7 +625,7 @@ L$aes128__func1: vaesenc %ymm2,%ymm14,%ymm14 vaesenc %ymm2,%ymm15,%ymm15 - +// Fold mi into hi. vpclmulqdq $0x01,%ymm6,%ymm4,%ymm2 vpshufd $0x4e,%ymm6,%ymm6 vpxor %ymm6,%ymm1,%ymm1 @@ -636,7 +641,7 @@ L$aes128__func1: vpxor %xmm2,%xmm1,%xmm1 - subq $-128,%rsi + subq $-128,%rsi // 128 is 4 bytes, -128 is 1 byte vpxor 0(%rdi),%ymm10,%ymm2 vpxor 32(%rdi),%ymm10,%ymm3 vpxor 64(%rdi),%ymm10,%ymm5 @@ -656,7 +661,7 @@ L$aes128__func1: cmpq $127,%rdx ja L$crypt_loop_4x__func1 L$ghash_last_ciphertext_4x__func1: - +// First vector vmovdqu 0(%rsi),%ymm3 vpshufb %ymm0,%ymm3,%ymm3 vmovdqu 0(%r9),%ymm4 @@ -666,7 +671,7 @@ L$ghash_last_ciphertext_4x__func1: vpunpckhqdq %ymm3,%ymm3,%ymm2 vpxor %ymm3,%ymm2,%ymm2 vpclmulqdq $0x00,%ymm7,%ymm2,%ymm6 - +// Second vector vmovdqu 32(%rsi),%ymm3 vpshufb %ymm0,%ymm3,%ymm3 vmovdqu 32(%r9),%ymm4 @@ -678,7 +683,7 @@ L$ghash_last_ciphertext_4x__func1: vpxor %ymm3,%ymm2,%ymm2 vpclmulqdq $0x10,%ymm7,%ymm2,%ymm2 vpxor %ymm2,%ymm6,%ymm6 - +// Third vector vmovdqu 64(%rsi),%ymm3 vpshufb %ymm0,%ymm3,%ymm3 vmovdqu 64(%r9),%ymm4 @@ -691,7 +696,7 @@ L$ghash_last_ciphertext_4x__func1: vpclmulqdq $0x00,%ymm8,%ymm2,%ymm2 vpxor %ymm2,%ymm6,%ymm6 - +// Fourth vector vmovdqu 96(%rsi),%ymm3 vpshufb %ymm0,%ymm3,%ymm3 vmovdqu 96(%r9),%ymm4 @@ -703,17 +708,17 @@ L$ghash_last_ciphertext_4x__func1: vpxor %ymm3,%ymm2,%ymm2 vpclmulqdq $0x10,%ymm8,%ymm2,%ymm2 vpxor %ymm2,%ymm6,%ymm6 - +// Finalize 'mi' following Karatsuba multiplication. vpxor %ymm5,%ymm6,%ymm6 vpxor %ymm1,%ymm6,%ymm6 - +// Fold lo into mi. vbroadcasti128 L$gfpoly(%rip),%ymm4 vpclmulqdq $0x01,%ymm5,%ymm4,%ymm2 vpshufd $0x4e,%ymm5,%ymm5 vpxor %ymm5,%ymm6,%ymm6 vpxor %ymm2,%ymm6,%ymm6 - +// Fold mi into hi. vpclmulqdq $0x01,%ymm6,%ymm4,%ymm2 vpshufd $0x4e,%ymm6,%ymm6 vpxor %ymm6,%ymm1,%ymm1 @@ -723,18 +728,18 @@ L$ghash_last_ciphertext_4x__func1: subq $-128,%rsi L$crypt_loop_4x_done__func1: - +// Check whether any data remains. testq %rdx,%rdx jz L$done__func1 +// DATALEN is in [16, 32, 48, 64, 80, 96, 112]. - - - +// Make POWERS_PTR point to the key powers [H^N, H^(N-1), ...] where N +// is the number of blocks that remain. leaq 128(%r9),%r8 subq %rdx,%r8 - +// Start collecting the unreduced GHASH intermediate value LO, MI, HI. vpxor %xmm5,%xmm5,%xmm5 vpxor %xmm6,%xmm6,%xmm6 vpxor %xmm7,%xmm7,%xmm7 @@ -742,7 +747,7 @@ L$crypt_loop_4x_done__func1: cmpq $64,%rdx jb L$lessthan64bytes__func1 - +// DATALEN is in [64, 80, 96, 112]. Encrypt two vectors of counter blocks. vpshufb %ymm0,%ymm11,%ymm12 vpaddd L$inc_2blocks(%rip),%ymm11,%ymm11 vpshufb %ymm0,%ymm11,%ymm13 @@ -760,7 +765,7 @@ L$vaesenc_loop_tail_1__func1: vaesenclast %ymm10,%ymm12,%ymm12 vaesenclast %ymm10,%ymm13,%ymm13 - +// XOR the data with the two vectors of keystream blocks. vmovdqu 0(%rdi),%ymm2 vmovdqu 32(%rdi),%ymm3 vpxor %ymm2,%ymm12,%ymm12 @@ -768,7 +773,7 @@ L$vaesenc_loop_tail_1__func1: vmovdqu %ymm12,0(%rsi) vmovdqu %ymm13,32(%rsi) - +// Update GHASH with two vectors of ciphertext blocks, without reducing. vpshufb %ymm0,%ymm12,%ymm12 vpshufb %ymm0,%ymm13,%ymm13 vpxor %ymm1,%ymm12,%ymm12 @@ -796,7 +801,7 @@ L$vaesenc_loop_tail_1__func1: vpxor %xmm1,%xmm1,%xmm1 - +// DATALEN is in [16, 32, 48]. Encrypt two last vectors of counter blocks. L$lessthan64bytes__func1: vpshufb %ymm0,%ymm11,%ymm12 vpaddd L$inc_2blocks(%rip),%ymm11,%ymm11 @@ -814,8 +819,8 @@ L$vaesenc_loop_tail_2__func1: vaesenclast %ymm10,%ymm12,%ymm12 vaesenclast %ymm10,%ymm13,%ymm13 - - +// XOR the remaining data with the keystream blocks, and update GHASH with +// the remaining ciphertext blocks without reducing. cmpq $32,%rdx jb L$xor_one_block__func1 @@ -872,7 +877,7 @@ L$ghash_mul_one_vec_unreduced__func1: vpxor %ymm4,%ymm7,%ymm7 L$reduce__func1: - +// Finally, do the GHASH reduction. vbroadcasti128 L$gfpoly(%rip),%ymm2 vpclmulqdq $0x01,%ymm5,%ymm2,%ymm3 vpshufd $0x4e,%ymm5,%ymm5 @@ -886,7 +891,7 @@ L$reduce__func1: vpxor %xmm7,%xmm1,%xmm1 L$done__func1: - +// Store the updated GHASH accumulator back to memory. vpshufb %xmm0,%xmm1,%xmm1 vmovdqu %xmm1,(%r12) @@ -901,6 +906,7 @@ L$done__func1: .private_extern _aes_gcm_dec_update_vaes_avx2 .p2align 5 +.alt_entry _aes_gcm_dec_update_vaes_avx2 _aes_gcm_dec_update_vaes_avx2: @@ -908,33 +914,33 @@ _CET_ENDBR pushq %r12 - movq 16(%rsp),%r12 + movq 16(%rsp),%r12 // arg7 vbroadcasti128 L$bswap_mask(%rip),%ymm0 - - +// Load the GHASH accumulator and the starting counter. +// BoringSSL passes these values in big endian format. vmovdqu (%r12),%xmm1 vpshufb %xmm0,%xmm1,%xmm1 vbroadcasti128 (%r8),%ymm11 vpshufb %ymm0,%ymm11,%ymm11 - - +// Load the AES key length in bytes. BoringSSL stores number of rounds +// minus 1, so convert using: AESKEYLEN = 4 * aeskey->rounds - 20. movl 240(%rcx),%r10d leal -20(,%r10,4),%r10d - - - +// Make RNDKEYLAST_PTR point to the last AES round key. This is the +// round key with index 10, 12, or 14 for AES-128, AES-192, or AES-256 +// respectively. Then load the zero-th and last round keys. leaq 96(%rcx,%r10,4),%r11 vbroadcasti128 (%rcx),%ymm9 vbroadcasti128 (%r11),%ymm10 - +// Finish initializing LE_CTR by adding 1 to the second block. vpaddd L$ctr_pattern(%rip),%ymm11,%ymm11 - - +// If there are at least 128 bytes of data, then continue into the loop that +// processes 128 bytes of data at a time. Otherwise skip it. cmpq $127,%rdx jbe L$crypt_loop_4x_done__func2 @@ -943,9 +949,9 @@ _CET_ENDBR .p2align 4 L$crypt_loop_4x__func2: - - - +// Start the AES encryption of the counter blocks. +// Increment le_ctr four times to generate four vectors of little-endian +// counter blocks, swap each to big-endian, and store them in aesdata[0-3]. vmovdqu L$inc_2blocks(%rip),%ymm2 vpshufb %ymm0,%ymm11,%ymm12 vpaddd %ymm2,%ymm11,%ymm11 @@ -956,7 +962,7 @@ L$crypt_loop_4x__func2: vpshufb %ymm0,%ymm11,%ymm15 vpaddd %ymm2,%ymm11,%ymm11 - +// AES "round zero": XOR in the zero-th round key. vpxor %ymm9,%ymm12,%ymm12 vpxor %ymm9,%ymm13,%ymm13 vpxor %ymm9,%ymm14,%ymm14 @@ -965,7 +971,7 @@ L$crypt_loop_4x__func2: cmpl $24,%r10d jl L$aes128__func2 je L$aes192__func2 - +// AES-256 vbroadcasti128 -208(%r11),%ymm2 vaesenc %ymm2,%ymm12,%ymm12 vaesenc %ymm2,%ymm13,%ymm13 @@ -994,7 +1000,7 @@ L$aes192__func2: L$aes128__func2: prefetcht0 512(%rdi) prefetcht0 512+64(%rdi) - +// First vector vmovdqu 0(%rdi),%ymm3 vpshufb %ymm0,%ymm3,%ymm3 vmovdqu 0(%r9),%ymm4 @@ -1018,7 +1024,7 @@ L$aes128__func2: vaesenc %ymm2,%ymm14,%ymm14 vaesenc %ymm2,%ymm15,%ymm15 - +// Second vector vmovdqu 32(%rdi),%ymm3 vpshufb %ymm0,%ymm3,%ymm3 vmovdqu 32(%r9),%ymm4 @@ -1037,7 +1043,7 @@ L$aes128__func2: vaesenc %ymm2,%ymm14,%ymm14 vaesenc %ymm2,%ymm15,%ymm15 - +// Third vector vmovdqu 64(%rdi),%ymm3 vpshufb %ymm0,%ymm3,%ymm3 vmovdqu 64(%r9),%ymm4 @@ -1064,7 +1070,7 @@ L$aes128__func2: vpclmulqdq $0x00,%ymm8,%ymm2,%ymm2 vpxor %ymm2,%ymm6,%ymm6 - +// Fourth vector vmovdqu 96(%rdi),%ymm3 vpshufb %ymm0,%ymm3,%ymm3 @@ -1090,11 +1096,11 @@ L$aes128__func2: vaesenc %ymm2,%ymm14,%ymm14 vaesenc %ymm2,%ymm15,%ymm15 - +// Finalize 'mi' following Karatsuba multiplication. vpxor %ymm5,%ymm6,%ymm6 vpxor %ymm1,%ymm6,%ymm6 - +// Fold lo into mi. vbroadcasti128 L$gfpoly(%rip),%ymm4 vpclmulqdq $0x01,%ymm5,%ymm4,%ymm2 vpshufd $0x4e,%ymm5,%ymm5 @@ -1107,7 +1113,7 @@ L$aes128__func2: vaesenc %ymm2,%ymm14,%ymm14 vaesenc %ymm2,%ymm15,%ymm15 - +// Fold mi into hi. vpclmulqdq $0x01,%ymm6,%ymm4,%ymm2 vpshufd $0x4e,%ymm6,%ymm6 vpxor %ymm6,%ymm1,%ymm1 @@ -1123,7 +1129,7 @@ L$aes128__func2: vpxor %xmm2,%xmm1,%xmm1 - +// 128 is 4 bytes, -128 is 1 byte vpxor 0(%rdi),%ymm10,%ymm2 vpxor 32(%rdi),%ymm10,%ymm3 vpxor 64(%rdi),%ymm10,%ymm5 @@ -1143,18 +1149,18 @@ L$aes128__func2: cmpq $127,%rdx ja L$crypt_loop_4x__func2 L$crypt_loop_4x_done__func2: - +// Check whether any data remains. testq %rdx,%rdx jz L$done__func2 +// DATALEN is in [16, 32, 48, 64, 80, 96, 112]. - - - +// Make POWERS_PTR point to the key powers [H^N, H^(N-1), ...] where N +// is the number of blocks that remain. leaq 128(%r9),%r8 subq %rdx,%r8 - +// Start collecting the unreduced GHASH intermediate value LO, MI, HI. vpxor %xmm5,%xmm5,%xmm5 vpxor %xmm6,%xmm6,%xmm6 vpxor %xmm7,%xmm7,%xmm7 @@ -1162,7 +1168,7 @@ L$crypt_loop_4x_done__func2: cmpq $64,%rdx jb L$lessthan64bytes__func2 - +// DATALEN is in [64, 80, 96, 112]. Encrypt two vectors of counter blocks. vpshufb %ymm0,%ymm11,%ymm12 vpaddd L$inc_2blocks(%rip),%ymm11,%ymm11 vpshufb %ymm0,%ymm11,%ymm13 @@ -1180,7 +1186,7 @@ L$vaesenc_loop_tail_1__func2: vaesenclast %ymm10,%ymm12,%ymm12 vaesenclast %ymm10,%ymm13,%ymm13 - +// XOR the data with the two vectors of keystream blocks. vmovdqu 0(%rdi),%ymm2 vmovdqu 32(%rdi),%ymm3 vpxor %ymm2,%ymm12,%ymm12 @@ -1188,7 +1194,7 @@ L$vaesenc_loop_tail_1__func2: vmovdqu %ymm12,0(%rsi) vmovdqu %ymm13,32(%rsi) - +// Update GHASH with two vectors of ciphertext blocks, without reducing. vpshufb %ymm0,%ymm2,%ymm12 vpshufb %ymm0,%ymm3,%ymm13 vpxor %ymm1,%ymm12,%ymm12 @@ -1216,7 +1222,7 @@ L$vaesenc_loop_tail_1__func2: vpxor %xmm1,%xmm1,%xmm1 - +// DATALEN is in [16, 32, 48]. Encrypt two last vectors of counter blocks. L$lessthan64bytes__func2: vpshufb %ymm0,%ymm11,%ymm12 vpaddd L$inc_2blocks(%rip),%ymm11,%ymm11 @@ -1234,8 +1240,8 @@ L$vaesenc_loop_tail_2__func2: vaesenclast %ymm10,%ymm12,%ymm12 vaesenclast %ymm10,%ymm13,%ymm13 - - +// XOR the remaining data with the keystream blocks, and update GHASH with +// the remaining ciphertext blocks without reducing. cmpq $32,%rdx jb L$xor_one_block__func2 @@ -1292,7 +1298,7 @@ L$ghash_mul_one_vec_unreduced__func2: vpxor %ymm4,%ymm7,%ymm7 L$reduce__func2: - +// Finally, do the GHASH reduction. vbroadcasti128 L$gfpoly(%rip),%ymm2 vpclmulqdq $0x01,%ymm5,%ymm2,%ymm3 vpshufd $0x4e,%ymm5,%ymm5 @@ -1306,7 +1312,7 @@ L$reduce__func2: vpxor %xmm7,%xmm1,%xmm1 L$done__func2: - +// Store the updated GHASH accumulator back to memory. vpshufb %xmm0,%xmm1,%xmm1 vmovdqu %xmm1,(%r12) diff --git a/third_party/boringssl/gen/bcm/aes-gcm-avx2-x86_64-linux.S b/third_party/boringssl/gen/bcm/aes-gcm-avx2-x86_64-linux.S index 018397a11..e21f71206 100644 --- a/third_party/boringssl/gen/bcm/aes-gcm-avx2-x86_64-linux.S +++ b/third_party/boringssl/gen/bcm/aes-gcm-avx2-x86_64-linux.S @@ -5,28 +5,30 @@ #if !defined(OPENSSL_NO_ASM) && defined(OPENSSL_X86_64) && defined(__ELF__) .section .rodata -.align 16 +.align 16 +aes_gcm_avx2_constants: +// A shuffle mask that reflects the bytes of 16-byte blocks .Lbswap_mask: .quad 0x08090a0b0c0d0e0f, 0x0001020304050607 - - - - - - - +// This is the GHASH reducing polynomial without its constant term, i.e. +// x^128 + x^7 + x^2 + x, represented using the backwards mapping +// between bits and polynomial coefficients. +// +// Alternatively, it can be interpreted as the naturally-ordered +// representation of the polynomial x^127 + x^126 + x^121 + 1, i.e. the +// "reversed" GHASH reducing polynomial without its x^128 term. .Lgfpoly: .quad 1, 0xc200000000000000 - +// Same as above, but with the (1 << 64) bit set. .Lgfpoly_and_internal_carrybit: .quad 1, 0xc200000000000001 .align 32 - +// The below constants are used for incrementing the counter blocks. .Lctr_pattern: .quad 0, 0 .quad 1, 0 @@ -46,14 +48,14 @@ _CET_ENDBR - - +// Load the byte-reflected hash subkey. BoringSSL provides it in +// byte-reflected form except the two halves are in the wrong order. vpshufd $0x4e,(%rsi),%xmm3 - - - - +// Finish preprocessing the byte-reflected hash subkey by multiplying it by +// x^-1 ("standard" interpretation of polynomial coefficients) or +// equivalently x^1 (natural interpretation). This gets the key into a +// format that avoids having to bit-reflect the data blocks later. vpshufd $0xd3,%xmm3,%xmm0 vpsrad $31,%xmm0,%xmm0 vpaddq %xmm3,%xmm3,%xmm3 @@ -62,83 +64,83 @@ _CET_ENDBR vbroadcasti128 .Lgfpoly(%rip),%ymm6 - - vpclmulqdq $0x00,%xmm3,%xmm3,%xmm0 - vpclmulqdq $0x11,%xmm3,%xmm3,%xmm5 - vpclmulqdq $0x01,%xmm0,%xmm6,%xmm1 - vpshufd $0x4e,%xmm0,%xmm0 - vpxor %xmm0,%xmm1,%xmm1 - vpclmulqdq $0x01,%xmm1,%xmm6,%xmm0 - vpshufd $0x4e,%xmm1,%xmm1 - vpxor %xmm1,%xmm5,%xmm5 - vpxor %xmm0,%xmm5,%xmm5 - +// Square H^1 to get H^2. + vpclmulqdq $0x00,%xmm3,%xmm3,%xmm0 // LO = a_L * a_L + vpclmulqdq $0x11,%xmm3,%xmm3,%xmm5 // HI = a_H * a_H + vpclmulqdq $0x01,%xmm0,%xmm6,%xmm1 // LO_L*(x^63 + x^62 + x^57) + vpshufd $0x4e,%xmm0,%xmm0 // Swap halves of LO + vpxor %xmm0,%xmm1,%xmm1 // Fold LO into MI + vpclmulqdq $0x01,%xmm1,%xmm6,%xmm0 // MI_L*(x^63 + x^62 + x^57) + vpshufd $0x4e,%xmm1,%xmm1 // Swap halves of MI + vpxor %xmm1,%xmm5,%xmm5 // Fold MI into HI (part 1) + vpxor %xmm0,%xmm5,%xmm5 // Fold MI into HI (part 2) +// Create H_CUR = [H^2, H^1] and H_INC = [H^2, H^2]. vinserti128 $1,%xmm3,%ymm5,%ymm3 vinserti128 $1,%xmm5,%ymm5,%ymm5 - - vpclmulqdq $0x00,%ymm5,%ymm3,%ymm0 - vpclmulqdq $0x01,%ymm5,%ymm3,%ymm1 - vpclmulqdq $0x10,%ymm5,%ymm3,%ymm2 - vpxor %ymm2,%ymm1,%ymm1 - vpclmulqdq $0x01,%ymm0,%ymm6,%ymm2 - vpshufd $0x4e,%ymm0,%ymm0 - vpxor %ymm0,%ymm1,%ymm1 - vpxor %ymm2,%ymm1,%ymm1 - vpclmulqdq $0x11,%ymm5,%ymm3,%ymm4 - vpclmulqdq $0x01,%ymm1,%ymm6,%ymm0 - vpshufd $0x4e,%ymm1,%ymm1 - vpxor %ymm1,%ymm4,%ymm4 - vpxor %ymm0,%ymm4,%ymm4 - - - +// Compute H_CUR2 = [H^4, H^3]. + vpclmulqdq $0x00,%ymm5,%ymm3,%ymm0 // LO = a_L * b_L + vpclmulqdq $0x01,%ymm5,%ymm3,%ymm1 // MI_0 = a_L * b_H + vpclmulqdq $0x10,%ymm5,%ymm3,%ymm2 // MI_1 = a_H * b_L + vpxor %ymm2,%ymm1,%ymm1 // MI = MI_0 + MI_1 + vpclmulqdq $0x01,%ymm0,%ymm6,%ymm2 // LO_L*(x^63 + x^62 + x^57) + vpshufd $0x4e,%ymm0,%ymm0 // Swap halves of LO + vpxor %ymm0,%ymm1,%ymm1 // Fold LO into MI (part 1) + vpxor %ymm2,%ymm1,%ymm1 // Fold LO into MI (part 2) + vpclmulqdq $0x11,%ymm5,%ymm3,%ymm4 // HI = a_H * b_H + vpclmulqdq $0x01,%ymm1,%ymm6,%ymm0 // MI_L*(x^63 + x^62 + x^57) + vpshufd $0x4e,%ymm1,%ymm1 // Swap halves of MI + vpxor %ymm1,%ymm4,%ymm4 // Fold MI into HI (part 1) + vpxor %ymm0,%ymm4,%ymm4 // Fold MI into HI (part 2) + + +// Store [H^2, H^1] and [H^4, H^3]. vmovdqu %ymm3,96(%rdi) vmovdqu %ymm4,64(%rdi) - - +// For Karatsuba multiplication: compute and store the two 64-bit halves of +// each key power XOR'd together. Order is 4,2,3,1. vpunpcklqdq %ymm3,%ymm4,%ymm0 vpunpckhqdq %ymm3,%ymm4,%ymm1 vpxor %ymm1,%ymm0,%ymm0 vmovdqu %ymm0,128+32(%rdi) - - vpclmulqdq $0x00,%ymm5,%ymm4,%ymm0 - vpclmulqdq $0x01,%ymm5,%ymm4,%ymm1 - vpclmulqdq $0x10,%ymm5,%ymm4,%ymm2 - vpxor %ymm2,%ymm1,%ymm1 - vpclmulqdq $0x01,%ymm0,%ymm6,%ymm2 - vpshufd $0x4e,%ymm0,%ymm0 - vpxor %ymm0,%ymm1,%ymm1 - vpxor %ymm2,%ymm1,%ymm1 - vpclmulqdq $0x11,%ymm5,%ymm4,%ymm3 - vpclmulqdq $0x01,%ymm1,%ymm6,%ymm0 - vpshufd $0x4e,%ymm1,%ymm1 - vpxor %ymm1,%ymm3,%ymm3 - vpxor %ymm0,%ymm3,%ymm3 - - vpclmulqdq $0x00,%ymm5,%ymm3,%ymm0 - vpclmulqdq $0x01,%ymm5,%ymm3,%ymm1 - vpclmulqdq $0x10,%ymm5,%ymm3,%ymm2 - vpxor %ymm2,%ymm1,%ymm1 - vpclmulqdq $0x01,%ymm0,%ymm6,%ymm2 - vpshufd $0x4e,%ymm0,%ymm0 - vpxor %ymm0,%ymm1,%ymm1 - vpxor %ymm2,%ymm1,%ymm1 - vpclmulqdq $0x11,%ymm5,%ymm3,%ymm4 - vpclmulqdq $0x01,%ymm1,%ymm6,%ymm0 - vpshufd $0x4e,%ymm1,%ymm1 - vpxor %ymm1,%ymm4,%ymm4 - vpxor %ymm0,%ymm4,%ymm4 +// Compute and store H_CUR = [H^6, H^5] and H_CUR2 = [H^8, H^7]. + vpclmulqdq $0x00,%ymm5,%ymm4,%ymm0 // LO = a_L * b_L + vpclmulqdq $0x01,%ymm5,%ymm4,%ymm1 // MI_0 = a_L * b_H + vpclmulqdq $0x10,%ymm5,%ymm4,%ymm2 // MI_1 = a_H * b_L + vpxor %ymm2,%ymm1,%ymm1 // MI = MI_0 + MI_1 + vpclmulqdq $0x01,%ymm0,%ymm6,%ymm2 // LO_L*(x^63 + x^62 + x^57) + vpshufd $0x4e,%ymm0,%ymm0 // Swap halves of LO + vpxor %ymm0,%ymm1,%ymm1 // Fold LO into MI (part 1) + vpxor %ymm2,%ymm1,%ymm1 // Fold LO into MI (part 2) + vpclmulqdq $0x11,%ymm5,%ymm4,%ymm3 // HI = a_H * b_H + vpclmulqdq $0x01,%ymm1,%ymm6,%ymm0 // MI_L*(x^63 + x^62 + x^57) + vpshufd $0x4e,%ymm1,%ymm1 // Swap halves of MI + vpxor %ymm1,%ymm3,%ymm3 // Fold MI into HI (part 1) + vpxor %ymm0,%ymm3,%ymm3 // Fold MI into HI (part 2) + + vpclmulqdq $0x00,%ymm5,%ymm3,%ymm0 // LO = a_L * b_L + vpclmulqdq $0x01,%ymm5,%ymm3,%ymm1 // MI_0 = a_L * b_H + vpclmulqdq $0x10,%ymm5,%ymm3,%ymm2 // MI_1 = a_H * b_L + vpxor %ymm2,%ymm1,%ymm1 // MI = MI_0 + MI_1 + vpclmulqdq $0x01,%ymm0,%ymm6,%ymm2 // LO_L*(x^63 + x^62 + x^57) + vpshufd $0x4e,%ymm0,%ymm0 // Swap halves of LO + vpxor %ymm0,%ymm1,%ymm1 // Fold LO into MI (part 1) + vpxor %ymm2,%ymm1,%ymm1 // Fold LO into MI (part 2) + vpclmulqdq $0x11,%ymm5,%ymm3,%ymm4 // HI = a_H * b_H + vpclmulqdq $0x01,%ymm1,%ymm6,%ymm0 // MI_L*(x^63 + x^62 + x^57) + vpshufd $0x4e,%ymm1,%ymm1 // Swap halves of MI + vpxor %ymm1,%ymm4,%ymm4 // Fold MI into HI (part 1) + vpxor %ymm0,%ymm4,%ymm4 // Fold MI into HI (part 2) vmovdqu %ymm3,32(%rdi) vmovdqu %ymm4,0(%rdi) - - +// Again, compute and store the two 64-bit halves of each key power XOR'd +// together. Order is 8,6,7,5. vpunpcklqdq %ymm3,%ymm4,%ymm0 vpunpckhqdq %ymm3,%ymm4,%ymm1 vpxor %ymm1,%ymm0,%ymm0 @@ -166,25 +168,25 @@ _CET_ENDBR vmovdqu .Lgfpoly(%rip),%xmm3 vpshufb %xmm1,%xmm0,%xmm0 - vpclmulqdq $0x00,%xmm2,%xmm0,%xmm4 - vpclmulqdq $0x01,%xmm2,%xmm0,%xmm5 - vpclmulqdq $0x10,%xmm2,%xmm0,%xmm6 - vpxor %xmm6,%xmm5,%xmm5 - vpclmulqdq $0x01,%xmm4,%xmm3,%xmm6 - vpshufd $0x4e,%xmm4,%xmm4 - vpxor %xmm4,%xmm5,%xmm5 - vpxor %xmm6,%xmm5,%xmm5 - vpclmulqdq $0x11,%xmm2,%xmm0,%xmm0 - vpclmulqdq $0x01,%xmm5,%xmm3,%xmm4 - vpshufd $0x4e,%xmm5,%xmm5 - vpxor %xmm5,%xmm0,%xmm0 - vpxor %xmm4,%xmm0,%xmm0 + vpclmulqdq $0x00,%xmm2,%xmm0,%xmm4 // LO = a_L * b_L + vpclmulqdq $0x01,%xmm2,%xmm0,%xmm5 // MI_0 = a_L * b_H + vpclmulqdq $0x10,%xmm2,%xmm0,%xmm6 // MI_1 = a_H * b_L + vpxor %xmm6,%xmm5,%xmm5 // MI = MI_0 + MI_1 + vpclmulqdq $0x01,%xmm4,%xmm3,%xmm6 // LO_L*(x^63 + x^62 + x^57) + vpshufd $0x4e,%xmm4,%xmm4 // Swap halves of LO + vpxor %xmm4,%xmm5,%xmm5 // Fold LO into MI (part 1) + vpxor %xmm6,%xmm5,%xmm5 // Fold LO into MI (part 2) + vpclmulqdq $0x11,%xmm2,%xmm0,%xmm0 // HI = a_H * b_H + vpclmulqdq $0x01,%xmm5,%xmm3,%xmm4 // MI_L*(x^63 + x^62 + x^57) + vpshufd $0x4e,%xmm5,%xmm5 // Swap halves of MI + vpxor %xmm5,%xmm0,%xmm0 // Fold MI into HI (part 1) + vpxor %xmm4,%xmm0,%xmm0 // Fold MI into HI (part 2) vpshufb %xmm1,%xmm0,%xmm0 vmovdqu %xmm0,(%rdi) - +// No need for vzeroupper, since only xmm registers were used. ret .cfi_endproc @@ -200,33 +202,33 @@ _CET_ENDBR - - - +// Load the bswap_mask and gfpoly constants. Since AADLEN is usually small, +// usually only 128-bit vectors will be used. So as an optimization, don't +// broadcast these constants to both 128-bit lanes quite yet. vmovdqu .Lbswap_mask(%rip),%xmm6 vmovdqu .Lgfpoly(%rip),%xmm7 - +// Load the GHASH accumulator. vmovdqu (%rdi),%xmm5 vpshufb %xmm6,%xmm5,%xmm5 - +// Optimize for AADLEN < 32 by checking for AADLEN < 32 before AADLEN < 128. cmpq $32,%rcx jb .Lghash_lastblock - - +// AADLEN >= 32, so we'll operate on full vectors. Broadcast bswap_mask and +// gfpoly to both 128-bit lanes. vinserti128 $1,%xmm6,%ymm6,%ymm6 vinserti128 $1,%xmm7,%ymm7,%ymm7 cmpq $127,%rcx jbe .Lghash_loop_1x - +// Update GHASH with 128 bytes of AAD at a time. vmovdqu 128(%rsi),%ymm8 vmovdqu 128+32(%rsi),%ymm9 .Lghash_loop_4x: - +// First vector vmovdqu 0(%rdx),%ymm1 vpshufb %ymm6,%ymm1,%ymm1 vmovdqu 0(%rsi),%ymm2 @@ -236,7 +238,7 @@ _CET_ENDBR vpunpckhqdq %ymm1,%ymm1,%ymm0 vpxor %ymm1,%ymm0,%ymm0 vpclmulqdq $0x00,%ymm8,%ymm0,%ymm4 - +// Second vector vmovdqu 32(%rdx),%ymm1 vpshufb %ymm6,%ymm1,%ymm1 vmovdqu 32(%rsi),%ymm2 @@ -248,7 +250,7 @@ _CET_ENDBR vpxor %ymm1,%ymm0,%ymm0 vpclmulqdq $0x10,%ymm8,%ymm0,%ymm0 vpxor %ymm0,%ymm4,%ymm4 - +// Third vector vmovdqu 64(%rdx),%ymm1 vpshufb %ymm6,%ymm1,%ymm1 vmovdqu 64(%rsi),%ymm2 @@ -261,7 +263,7 @@ _CET_ENDBR vpclmulqdq $0x00,%ymm9,%ymm0,%ymm0 vpxor %ymm0,%ymm4,%ymm4 - +// Fourth vector vmovdqu 96(%rdx),%ymm1 vpshufb %ymm6,%ymm1,%ymm1 vmovdqu 96(%rsi),%ymm2 @@ -273,17 +275,17 @@ _CET_ENDBR vpxor %ymm1,%ymm0,%ymm0 vpclmulqdq $0x10,%ymm9,%ymm0,%ymm0 vpxor %ymm0,%ymm4,%ymm4 - +// Finalize 'mi' following Karatsuba multiplication. vpxor %ymm3,%ymm4,%ymm4 vpxor %ymm5,%ymm4,%ymm4 - +// Fold lo into mi. vbroadcasti128 .Lgfpoly(%rip),%ymm2 vpclmulqdq $0x01,%ymm3,%ymm2,%ymm0 vpshufd $0x4e,%ymm3,%ymm3 vpxor %ymm3,%ymm4,%ymm4 vpxor %ymm0,%ymm4,%ymm4 - +// Fold mi into hi. vpclmulqdq $0x01,%ymm4,%ymm2,%ymm0 vpshufd $0x4e,%ymm4,%ymm4 vpxor %ymm4,%ymm5,%ymm5 @@ -291,12 +293,12 @@ _CET_ENDBR vextracti128 $1,%ymm5,%xmm0 vpxor %xmm0,%xmm5,%xmm5 - subq $-128,%rdx + subq $-128,%rdx // 128 is 4 bytes, -128 is 1 byte addq $-128,%rcx cmpq $127,%rcx ja .Lghash_loop_4x - +// Update GHASH with 32 bytes of AAD at a time. cmpq $32,%rcx jb .Lghash_loop_1x_done .Lghash_loop_1x: @@ -304,19 +306,19 @@ _CET_ENDBR vpshufb %ymm6,%ymm0,%ymm0 vpxor %ymm0,%ymm5,%ymm5 vmovdqu 128-32(%rsi),%ymm0 - vpclmulqdq $0x00,%ymm0,%ymm5,%ymm1 - vpclmulqdq $0x01,%ymm0,%ymm5,%ymm2 - vpclmulqdq $0x10,%ymm0,%ymm5,%ymm3 - vpxor %ymm3,%ymm2,%ymm2 - vpclmulqdq $0x01,%ymm1,%ymm7,%ymm3 - vpshufd $0x4e,%ymm1,%ymm1 - vpxor %ymm1,%ymm2,%ymm2 - vpxor %ymm3,%ymm2,%ymm2 - vpclmulqdq $0x11,%ymm0,%ymm5,%ymm5 - vpclmulqdq $0x01,%ymm2,%ymm7,%ymm1 - vpshufd $0x4e,%ymm2,%ymm2 - vpxor %ymm2,%ymm5,%ymm5 - vpxor %ymm1,%ymm5,%ymm5 + vpclmulqdq $0x00,%ymm0,%ymm5,%ymm1 // LO = a_L * b_L + vpclmulqdq $0x01,%ymm0,%ymm5,%ymm2 // MI_0 = a_L * b_H + vpclmulqdq $0x10,%ymm0,%ymm5,%ymm3 // MI_1 = a_H * b_L + vpxor %ymm3,%ymm2,%ymm2 // MI = MI_0 + MI_1 + vpclmulqdq $0x01,%ymm1,%ymm7,%ymm3 // LO_L*(x^63 + x^62 + x^57) + vpshufd $0x4e,%ymm1,%ymm1 // Swap halves of LO + vpxor %ymm1,%ymm2,%ymm2 // Fold LO into MI (part 1) + vpxor %ymm3,%ymm2,%ymm2 // Fold LO into MI (part 2) + vpclmulqdq $0x11,%ymm0,%ymm5,%ymm5 // HI = a_H * b_H + vpclmulqdq $0x01,%ymm2,%ymm7,%ymm1 // MI_L*(x^63 + x^62 + x^57) + vpshufd $0x4e,%ymm2,%ymm2 // Swap halves of MI + vpxor %ymm2,%ymm5,%ymm5 // Fold MI into HI (part 1) + vpxor %ymm1,%ymm5,%ymm5 // Fold MI into HI (part 2) vextracti128 $1,%ymm5,%xmm0 vpxor %xmm0,%xmm5,%xmm5 @@ -326,7 +328,7 @@ _CET_ENDBR jae .Lghash_loop_1x .Lghash_loop_1x_done: - +// Update GHASH with the remaining 16-byte block if any. .Lghash_lastblock: testq %rcx,%rcx jz .Lghash_done @@ -334,23 +336,23 @@ _CET_ENDBR vpshufb %xmm6,%xmm0,%xmm0 vpxor %xmm0,%xmm5,%xmm5 vmovdqu 128-16(%rsi),%xmm0 - vpclmulqdq $0x00,%xmm0,%xmm5,%xmm1 - vpclmulqdq $0x01,%xmm0,%xmm5,%xmm2 - vpclmulqdq $0x10,%xmm0,%xmm5,%xmm3 - vpxor %xmm3,%xmm2,%xmm2 - vpclmulqdq $0x01,%xmm1,%xmm7,%xmm3 - vpshufd $0x4e,%xmm1,%xmm1 - vpxor %xmm1,%xmm2,%xmm2 - vpxor %xmm3,%xmm2,%xmm2 - vpclmulqdq $0x11,%xmm0,%xmm5,%xmm5 - vpclmulqdq $0x01,%xmm2,%xmm7,%xmm1 - vpshufd $0x4e,%xmm2,%xmm2 - vpxor %xmm2,%xmm5,%xmm5 - vpxor %xmm1,%xmm5,%xmm5 + vpclmulqdq $0x00,%xmm0,%xmm5,%xmm1 // LO = a_L * b_L + vpclmulqdq $0x01,%xmm0,%xmm5,%xmm2 // MI_0 = a_L * b_H + vpclmulqdq $0x10,%xmm0,%xmm5,%xmm3 // MI_1 = a_H * b_L + vpxor %xmm3,%xmm2,%xmm2 // MI = MI_0 + MI_1 + vpclmulqdq $0x01,%xmm1,%xmm7,%xmm3 // LO_L*(x^63 + x^62 + x^57) + vpshufd $0x4e,%xmm1,%xmm1 // Swap halves of LO + vpxor %xmm1,%xmm2,%xmm2 // Fold LO into MI (part 1) + vpxor %xmm3,%xmm2,%xmm2 // Fold LO into MI (part 2) + vpclmulqdq $0x11,%xmm0,%xmm5,%xmm5 // HI = a_H * b_H + vpclmulqdq $0x01,%xmm2,%xmm7,%xmm1 // MI_L*(x^63 + x^62 + x^57) + vpshufd $0x4e,%xmm2,%xmm2 // Swap halves of MI + vpxor %xmm2,%xmm5,%xmm5 // Fold MI into HI (part 1) + vpxor %xmm1,%xmm5,%xmm5 // Fold MI into HI (part 2) .Lghash_done: - +// Store the updated GHASH accumulator back to memory. vpshufb %xmm6,%xmm5,%xmm5 vmovdqu %xmm5,(%rdi) @@ -371,7 +373,7 @@ _CET_ENDBR .cfi_adjust_cfa_offset 8 .cfi_offset %r12,-16 - movq 16(%rsp),%r12 + movq 16(%rsp),%r12 // arg7 #ifdef BORINGSSL_DISPATCH_TEST .extern BORINGSSL_function_hit .hidden BORINGSSL_function_hit @@ -379,38 +381,38 @@ _CET_ENDBR #endif vbroadcasti128 .Lbswap_mask(%rip),%ymm0 - - +// Load the GHASH accumulator and the starting counter. +// BoringSSL passes these values in big endian format. vmovdqu (%r12),%xmm1 vpshufb %xmm0,%xmm1,%xmm1 vbroadcasti128 (%r8),%ymm11 vpshufb %ymm0,%ymm11,%ymm11 - - +// Load the AES key length in bytes. BoringSSL stores number of rounds +// minus 1, so convert using: AESKEYLEN = 4 * aeskey->rounds - 20. movl 240(%rcx),%r10d leal -20(,%r10,4),%r10d - - - +// Make RNDKEYLAST_PTR point to the last AES round key. This is the +// round key with index 10, 12, or 14 for AES-128, AES-192, or AES-256 +// respectively. Then load the zero-th and last round keys. leaq 96(%rcx,%r10,4),%r11 vbroadcasti128 (%rcx),%ymm9 vbroadcasti128 (%r11),%ymm10 - +// Finish initializing LE_CTR by adding 1 to the second block. vpaddd .Lctr_pattern(%rip),%ymm11,%ymm11 - - +// If there are at least 128 bytes of data, then continue into the loop that +// processes 128 bytes of data at a time. Otherwise skip it. cmpq $127,%rdx jbe .Lcrypt_loop_4x_done__func1 vmovdqu 128(%r9),%ymm7 vmovdqu 128+32(%r9),%ymm8 - - - +// Encrypt the first 4 vectors of plaintext blocks. +// Increment le_ctr four times to generate four vectors of little-endian +// counter blocks, swap each to big-endian, and store them in aesdata[0-3]. vmovdqu .Linc_2blocks(%rip),%ymm2 vpshufb %ymm0,%ymm11,%ymm12 vpaddd %ymm2,%ymm11,%ymm11 @@ -421,7 +423,7 @@ _CET_ENDBR vpshufb %ymm0,%ymm11,%ymm15 vpaddd %ymm2,%ymm11,%ymm11 - +// AES "round zero": XOR in the zero-th round key. vpxor %ymm9,%ymm12,%ymm12 vpxor %ymm9,%ymm13,%ymm13 vpxor %ymm9,%ymm14,%ymm14 @@ -451,16 +453,16 @@ _CET_ENDBR vmovdqu %ymm14,64(%rsi) vmovdqu %ymm15,96(%rsi) - subq $-128,%rdi + subq $-128,%rdi // 128 is 4 bytes, -128 is 1 byte addq $-128,%rdx cmpq $127,%rdx jbe .Lghash_last_ciphertext_4x__func1 .align 16 .Lcrypt_loop_4x__func1: - - - +// Start the AES encryption of the counter blocks. +// Increment le_ctr four times to generate four vectors of little-endian +// counter blocks, swap each to big-endian, and store them in aesdata[0-3]. vmovdqu .Linc_2blocks(%rip),%ymm2 vpshufb %ymm0,%ymm11,%ymm12 vpaddd %ymm2,%ymm11,%ymm11 @@ -471,7 +473,7 @@ _CET_ENDBR vpshufb %ymm0,%ymm11,%ymm15 vpaddd %ymm2,%ymm11,%ymm11 - +// AES "round zero": XOR in the zero-th round key. vpxor %ymm9,%ymm12,%ymm12 vpxor %ymm9,%ymm13,%ymm13 vpxor %ymm9,%ymm14,%ymm14 @@ -480,7 +482,7 @@ _CET_ENDBR cmpl $24,%r10d jl .Laes128__func1 je .Laes192__func1 - +// AES-256 vbroadcasti128 -208(%r11),%ymm2 vaesenc %ymm2,%ymm12,%ymm12 vaesenc %ymm2,%ymm13,%ymm13 @@ -509,7 +511,7 @@ _CET_ENDBR .Laes128__func1: prefetcht0 512(%rdi) prefetcht0 512+64(%rdi) - +// First vector vmovdqu 0(%rsi),%ymm3 vpshufb %ymm0,%ymm3,%ymm3 vmovdqu 0(%r9),%ymm4 @@ -533,7 +535,7 @@ _CET_ENDBR vaesenc %ymm2,%ymm14,%ymm14 vaesenc %ymm2,%ymm15,%ymm15 - +// Second vector vmovdqu 32(%rsi),%ymm3 vpshufb %ymm0,%ymm3,%ymm3 vmovdqu 32(%r9),%ymm4 @@ -552,7 +554,7 @@ _CET_ENDBR vaesenc %ymm2,%ymm14,%ymm14 vaesenc %ymm2,%ymm15,%ymm15 - +// Third vector vmovdqu 64(%rsi),%ymm3 vpshufb %ymm0,%ymm3,%ymm3 vmovdqu 64(%r9),%ymm4 @@ -579,7 +581,7 @@ _CET_ENDBR vpclmulqdq $0x00,%ymm8,%ymm2,%ymm2 vpxor %ymm2,%ymm6,%ymm6 - +// Fourth vector vmovdqu 96(%rsi),%ymm3 vpshufb %ymm0,%ymm3,%ymm3 @@ -605,11 +607,11 @@ _CET_ENDBR vaesenc %ymm2,%ymm14,%ymm14 vaesenc %ymm2,%ymm15,%ymm15 - +// Finalize 'mi' following Karatsuba multiplication. vpxor %ymm5,%ymm6,%ymm6 vpxor %ymm1,%ymm6,%ymm6 - +// Fold lo into mi. vbroadcasti128 .Lgfpoly(%rip),%ymm4 vpclmulqdq $0x01,%ymm5,%ymm4,%ymm2 vpshufd $0x4e,%ymm5,%ymm5 @@ -622,7 +624,7 @@ _CET_ENDBR vaesenc %ymm2,%ymm14,%ymm14 vaesenc %ymm2,%ymm15,%ymm15 - +// Fold mi into hi. vpclmulqdq $0x01,%ymm6,%ymm4,%ymm2 vpshufd $0x4e,%ymm6,%ymm6 vpxor %ymm6,%ymm1,%ymm1 @@ -638,7 +640,7 @@ _CET_ENDBR vpxor %xmm2,%xmm1,%xmm1 - subq $-128,%rsi + subq $-128,%rsi // 128 is 4 bytes, -128 is 1 byte vpxor 0(%rdi),%ymm10,%ymm2 vpxor 32(%rdi),%ymm10,%ymm3 vpxor 64(%rdi),%ymm10,%ymm5 @@ -658,7 +660,7 @@ _CET_ENDBR cmpq $127,%rdx ja .Lcrypt_loop_4x__func1 .Lghash_last_ciphertext_4x__func1: - +// First vector vmovdqu 0(%rsi),%ymm3 vpshufb %ymm0,%ymm3,%ymm3 vmovdqu 0(%r9),%ymm4 @@ -668,7 +670,7 @@ _CET_ENDBR vpunpckhqdq %ymm3,%ymm3,%ymm2 vpxor %ymm3,%ymm2,%ymm2 vpclmulqdq $0x00,%ymm7,%ymm2,%ymm6 - +// Second vector vmovdqu 32(%rsi),%ymm3 vpshufb %ymm0,%ymm3,%ymm3 vmovdqu 32(%r9),%ymm4 @@ -680,7 +682,7 @@ _CET_ENDBR vpxor %ymm3,%ymm2,%ymm2 vpclmulqdq $0x10,%ymm7,%ymm2,%ymm2 vpxor %ymm2,%ymm6,%ymm6 - +// Third vector vmovdqu 64(%rsi),%ymm3 vpshufb %ymm0,%ymm3,%ymm3 vmovdqu 64(%r9),%ymm4 @@ -693,7 +695,7 @@ _CET_ENDBR vpclmulqdq $0x00,%ymm8,%ymm2,%ymm2 vpxor %ymm2,%ymm6,%ymm6 - +// Fourth vector vmovdqu 96(%rsi),%ymm3 vpshufb %ymm0,%ymm3,%ymm3 vmovdqu 96(%r9),%ymm4 @@ -705,17 +707,17 @@ _CET_ENDBR vpxor %ymm3,%ymm2,%ymm2 vpclmulqdq $0x10,%ymm8,%ymm2,%ymm2 vpxor %ymm2,%ymm6,%ymm6 - +// Finalize 'mi' following Karatsuba multiplication. vpxor %ymm5,%ymm6,%ymm6 vpxor %ymm1,%ymm6,%ymm6 - +// Fold lo into mi. vbroadcasti128 .Lgfpoly(%rip),%ymm4 vpclmulqdq $0x01,%ymm5,%ymm4,%ymm2 vpshufd $0x4e,%ymm5,%ymm5 vpxor %ymm5,%ymm6,%ymm6 vpxor %ymm2,%ymm6,%ymm6 - +// Fold mi into hi. vpclmulqdq $0x01,%ymm6,%ymm4,%ymm2 vpshufd $0x4e,%ymm6,%ymm6 vpxor %ymm6,%ymm1,%ymm1 @@ -725,18 +727,18 @@ _CET_ENDBR subq $-128,%rsi .Lcrypt_loop_4x_done__func1: - +// Check whether any data remains. testq %rdx,%rdx jz .Ldone__func1 +// DATALEN is in [16, 32, 48, 64, 80, 96, 112]. - - - +// Make POWERS_PTR point to the key powers [H^N, H^(N-1), ...] where N +// is the number of blocks that remain. leaq 128(%r9),%r8 subq %rdx,%r8 - +// Start collecting the unreduced GHASH intermediate value LO, MI, HI. vpxor %xmm5,%xmm5,%xmm5 vpxor %xmm6,%xmm6,%xmm6 vpxor %xmm7,%xmm7,%xmm7 @@ -744,7 +746,7 @@ _CET_ENDBR cmpq $64,%rdx jb .Llessthan64bytes__func1 - +// DATALEN is in [64, 80, 96, 112]. Encrypt two vectors of counter blocks. vpshufb %ymm0,%ymm11,%ymm12 vpaddd .Linc_2blocks(%rip),%ymm11,%ymm11 vpshufb %ymm0,%ymm11,%ymm13 @@ -762,7 +764,7 @@ _CET_ENDBR vaesenclast %ymm10,%ymm12,%ymm12 vaesenclast %ymm10,%ymm13,%ymm13 - +// XOR the data with the two vectors of keystream blocks. vmovdqu 0(%rdi),%ymm2 vmovdqu 32(%rdi),%ymm3 vpxor %ymm2,%ymm12,%ymm12 @@ -770,7 +772,7 @@ _CET_ENDBR vmovdqu %ymm12,0(%rsi) vmovdqu %ymm13,32(%rsi) - +// Update GHASH with two vectors of ciphertext blocks, without reducing. vpshufb %ymm0,%ymm12,%ymm12 vpshufb %ymm0,%ymm13,%ymm13 vpxor %ymm1,%ymm12,%ymm12 @@ -798,7 +800,7 @@ _CET_ENDBR vpxor %xmm1,%xmm1,%xmm1 - +// DATALEN is in [16, 32, 48]. Encrypt two last vectors of counter blocks. .Llessthan64bytes__func1: vpshufb %ymm0,%ymm11,%ymm12 vpaddd .Linc_2blocks(%rip),%ymm11,%ymm11 @@ -816,8 +818,8 @@ _CET_ENDBR vaesenclast %ymm10,%ymm12,%ymm12 vaesenclast %ymm10,%ymm13,%ymm13 - - +// XOR the remaining data with the keystream blocks, and update GHASH with +// the remaining ciphertext blocks without reducing. cmpq $32,%rdx jb .Lxor_one_block__func1 @@ -874,7 +876,7 @@ _CET_ENDBR vpxor %ymm4,%ymm7,%ymm7 .Lreduce__func1: - +// Finally, do the GHASH reduction. vbroadcasti128 .Lgfpoly(%rip),%ymm2 vpclmulqdq $0x01,%ymm5,%ymm2,%ymm3 vpshufd $0x4e,%ymm5,%ymm5 @@ -888,7 +890,7 @@ _CET_ENDBR vpxor %xmm7,%xmm1,%xmm1 .Ldone__func1: - +// Store the updated GHASH accumulator back to memory. vpshufb %xmm0,%xmm1,%xmm1 vmovdqu %xmm1,(%r12) @@ -912,33 +914,33 @@ _CET_ENDBR .cfi_adjust_cfa_offset 8 .cfi_offset %r12,-16 - movq 16(%rsp),%r12 + movq 16(%rsp),%r12 // arg7 vbroadcasti128 .Lbswap_mask(%rip),%ymm0 - - +// Load the GHASH accumulator and the starting counter. +// BoringSSL passes these values in big endian format. vmovdqu (%r12),%xmm1 vpshufb %xmm0,%xmm1,%xmm1 vbroadcasti128 (%r8),%ymm11 vpshufb %ymm0,%ymm11,%ymm11 - - +// Load the AES key length in bytes. BoringSSL stores number of rounds +// minus 1, so convert using: AESKEYLEN = 4 * aeskey->rounds - 20. movl 240(%rcx),%r10d leal -20(,%r10,4),%r10d - - - +// Make RNDKEYLAST_PTR point to the last AES round key. This is the +// round key with index 10, 12, or 14 for AES-128, AES-192, or AES-256 +// respectively. Then load the zero-th and last round keys. leaq 96(%rcx,%r10,4),%r11 vbroadcasti128 (%rcx),%ymm9 vbroadcasti128 (%r11),%ymm10 - +// Finish initializing LE_CTR by adding 1 to the second block. vpaddd .Lctr_pattern(%rip),%ymm11,%ymm11 - - +// If there are at least 128 bytes of data, then continue into the loop that +// processes 128 bytes of data at a time. Otherwise skip it. cmpq $127,%rdx jbe .Lcrypt_loop_4x_done__func2 @@ -947,9 +949,9 @@ _CET_ENDBR .align 16 .Lcrypt_loop_4x__func2: - - - +// Start the AES encryption of the counter blocks. +// Increment le_ctr four times to generate four vectors of little-endian +// counter blocks, swap each to big-endian, and store them in aesdata[0-3]. vmovdqu .Linc_2blocks(%rip),%ymm2 vpshufb %ymm0,%ymm11,%ymm12 vpaddd %ymm2,%ymm11,%ymm11 @@ -960,7 +962,7 @@ _CET_ENDBR vpshufb %ymm0,%ymm11,%ymm15 vpaddd %ymm2,%ymm11,%ymm11 - +// AES "round zero": XOR in the zero-th round key. vpxor %ymm9,%ymm12,%ymm12 vpxor %ymm9,%ymm13,%ymm13 vpxor %ymm9,%ymm14,%ymm14 @@ -969,7 +971,7 @@ _CET_ENDBR cmpl $24,%r10d jl .Laes128__func2 je .Laes192__func2 - +// AES-256 vbroadcasti128 -208(%r11),%ymm2 vaesenc %ymm2,%ymm12,%ymm12 vaesenc %ymm2,%ymm13,%ymm13 @@ -998,7 +1000,7 @@ _CET_ENDBR .Laes128__func2: prefetcht0 512(%rdi) prefetcht0 512+64(%rdi) - +// First vector vmovdqu 0(%rdi),%ymm3 vpshufb %ymm0,%ymm3,%ymm3 vmovdqu 0(%r9),%ymm4 @@ -1022,7 +1024,7 @@ _CET_ENDBR vaesenc %ymm2,%ymm14,%ymm14 vaesenc %ymm2,%ymm15,%ymm15 - +// Second vector vmovdqu 32(%rdi),%ymm3 vpshufb %ymm0,%ymm3,%ymm3 vmovdqu 32(%r9),%ymm4 @@ -1041,7 +1043,7 @@ _CET_ENDBR vaesenc %ymm2,%ymm14,%ymm14 vaesenc %ymm2,%ymm15,%ymm15 - +// Third vector vmovdqu 64(%rdi),%ymm3 vpshufb %ymm0,%ymm3,%ymm3 vmovdqu 64(%r9),%ymm4 @@ -1068,7 +1070,7 @@ _CET_ENDBR vpclmulqdq $0x00,%ymm8,%ymm2,%ymm2 vpxor %ymm2,%ymm6,%ymm6 - +// Fourth vector vmovdqu 96(%rdi),%ymm3 vpshufb %ymm0,%ymm3,%ymm3 @@ -1094,11 +1096,11 @@ _CET_ENDBR vaesenc %ymm2,%ymm14,%ymm14 vaesenc %ymm2,%ymm15,%ymm15 - +// Finalize 'mi' following Karatsuba multiplication. vpxor %ymm5,%ymm6,%ymm6 vpxor %ymm1,%ymm6,%ymm6 - +// Fold lo into mi. vbroadcasti128 .Lgfpoly(%rip),%ymm4 vpclmulqdq $0x01,%ymm5,%ymm4,%ymm2 vpshufd $0x4e,%ymm5,%ymm5 @@ -1111,7 +1113,7 @@ _CET_ENDBR vaesenc %ymm2,%ymm14,%ymm14 vaesenc %ymm2,%ymm15,%ymm15 - +// Fold mi into hi. vpclmulqdq $0x01,%ymm6,%ymm4,%ymm2 vpshufd $0x4e,%ymm6,%ymm6 vpxor %ymm6,%ymm1,%ymm1 @@ -1127,7 +1129,7 @@ _CET_ENDBR vpxor %xmm2,%xmm1,%xmm1 - +// 128 is 4 bytes, -128 is 1 byte vpxor 0(%rdi),%ymm10,%ymm2 vpxor 32(%rdi),%ymm10,%ymm3 vpxor 64(%rdi),%ymm10,%ymm5 @@ -1147,18 +1149,18 @@ _CET_ENDBR cmpq $127,%rdx ja .Lcrypt_loop_4x__func2 .Lcrypt_loop_4x_done__func2: - +// Check whether any data remains. testq %rdx,%rdx jz .Ldone__func2 +// DATALEN is in [16, 32, 48, 64, 80, 96, 112]. - - - +// Make POWERS_PTR point to the key powers [H^N, H^(N-1), ...] where N +// is the number of blocks that remain. leaq 128(%r9),%r8 subq %rdx,%r8 - +// Start collecting the unreduced GHASH intermediate value LO, MI, HI. vpxor %xmm5,%xmm5,%xmm5 vpxor %xmm6,%xmm6,%xmm6 vpxor %xmm7,%xmm7,%xmm7 @@ -1166,7 +1168,7 @@ _CET_ENDBR cmpq $64,%rdx jb .Llessthan64bytes__func2 - +// DATALEN is in [64, 80, 96, 112]. Encrypt two vectors of counter blocks. vpshufb %ymm0,%ymm11,%ymm12 vpaddd .Linc_2blocks(%rip),%ymm11,%ymm11 vpshufb %ymm0,%ymm11,%ymm13 @@ -1184,7 +1186,7 @@ _CET_ENDBR vaesenclast %ymm10,%ymm12,%ymm12 vaesenclast %ymm10,%ymm13,%ymm13 - +// XOR the data with the two vectors of keystream blocks. vmovdqu 0(%rdi),%ymm2 vmovdqu 32(%rdi),%ymm3 vpxor %ymm2,%ymm12,%ymm12 @@ -1192,7 +1194,7 @@ _CET_ENDBR vmovdqu %ymm12,0(%rsi) vmovdqu %ymm13,32(%rsi) - +// Update GHASH with two vectors of ciphertext blocks, without reducing. vpshufb %ymm0,%ymm2,%ymm12 vpshufb %ymm0,%ymm3,%ymm13 vpxor %ymm1,%ymm12,%ymm12 @@ -1220,7 +1222,7 @@ _CET_ENDBR vpxor %xmm1,%xmm1,%xmm1 - +// DATALEN is in [16, 32, 48]. Encrypt two last vectors of counter blocks. .Llessthan64bytes__func2: vpshufb %ymm0,%ymm11,%ymm12 vpaddd .Linc_2blocks(%rip),%ymm11,%ymm11 @@ -1238,8 +1240,8 @@ _CET_ENDBR vaesenclast %ymm10,%ymm12,%ymm12 vaesenclast %ymm10,%ymm13,%ymm13 - - +// XOR the remaining data with the keystream blocks, and update GHASH with +// the remaining ciphertext blocks without reducing. cmpq $32,%rdx jb .Lxor_one_block__func2 @@ -1296,7 +1298,7 @@ _CET_ENDBR vpxor %ymm4,%ymm7,%ymm7 .Lreduce__func2: - +// Finally, do the GHASH reduction. vbroadcasti128 .Lgfpoly(%rip),%ymm2 vpclmulqdq $0x01,%ymm5,%ymm2,%ymm3 vpshufd $0x4e,%ymm5,%ymm5 @@ -1310,7 +1312,7 @@ _CET_ENDBR vpxor %xmm7,%xmm1,%xmm1 .Ldone__func2: - +// Store the updated GHASH accumulator back to memory. vpshufb %xmm0,%xmm1,%xmm1 vmovdqu %xmm1,(%r12) diff --git a/third_party/boringssl/gen/bcm/aes-gcm-avx2-x86_64-win.asm b/third_party/boringssl/gen/bcm/aes-gcm-avx2-x86_64-win.asm index 41104322f..1bd096ee8 100644 --- a/third_party/boringssl/gen/bcm/aes-gcm-avx2-x86_64-win.asm +++ b/third_party/boringssl/gen/bcm/aes-gcm-avx2-x86_64-win.asm @@ -12,28 +12,30 @@ default rel %include "boringssl_prefix_symbols_internal_x86_64_win_asm.inc" %endif section .rdata rdata align=8 -ALIGN 16 +ALIGN 16 +aes_gcm_avx2_constants: +; A shuffle mask that reflects the bytes of 16-byte blocks $L$bswap_mask: DQ 0x08090a0b0c0d0e0f,0x0001020304050607 - - - - - - - +; This is the GHASH reducing polynomial without its constant term, i.e. +; x^128 + x^7 + x^2 + x, represented using the backwards mapping +; between bits and polynomial coefficients. +; +; Alternatively, it can be interpreted as the naturally-ordered +; representation of the polynomial x^127 + x^126 + x^121 + 1, i.e. the +; "reversed" GHASH reducing polynomial without its x^128 term. $L$gfpoly: DQ 1,0xc200000000000000 - +; Same as above, but with the (1 << 64) bit set. $L$gfpoly_and_internal_carrybit: DQ 1,0xc200000000000001 ALIGN 32 - +; The below constants are used for incrementing the counter blocks. $L$ctr_pattern: DQ 0,0 DQ 1,0 @@ -57,14 +59,14 @@ $L$SEH_prologue_gcm_init_vpclmulqdq_avx2_3: $L$SEH_endprologue_gcm_init_vpclmulqdq_avx2_4: - - +; Load the byte-reflected hash subkey. BoringSSL provides it in +; byte-reflected form except the two halves are in the wrong order. vpshufd xmm3,XMMWORD[rdx],0x4e - - - - +; Finish preprocessing the byte-reflected hash subkey by multiplying it by +; x^-1 ("standard" interpretation of polynomial coefficients) or +; equivalently x^1 (natural interpretation). This gets the key into a +; format that avoids having to bit-reflect the data blocks later. vpshufd xmm0,xmm3,0xd3 vpsrad xmm0,xmm0,31 vpaddq xmm3,xmm3,xmm3 @@ -73,83 +75,83 @@ $L$SEH_endprologue_gcm_init_vpclmulqdq_avx2_4: vbroadcasti128 ymm6,XMMWORD[$L$gfpoly] - - vpclmulqdq xmm0,xmm3,xmm3,0x00 - vpclmulqdq xmm5,xmm3,xmm3,0x11 - vpclmulqdq xmm1,xmm6,xmm0,0x01 - vpshufd xmm0,xmm0,0x4e - vpxor xmm1,xmm1,xmm0 - vpclmulqdq xmm0,xmm6,xmm1,0x01 - vpshufd xmm1,xmm1,0x4e - vpxor xmm5,xmm5,xmm1 - vpxor xmm5,xmm5,xmm0 - +; Square H^1 to get H^2. + vpclmulqdq xmm0,xmm3,xmm3,0x00 ; LO = a_L * a_L + vpclmulqdq xmm5,xmm3,xmm3,0x11 ; HI = a_H * a_H + vpclmulqdq xmm1,xmm6,xmm0,0x01 ; LO_L*(x^63 + x^62 + x^57) + vpshufd xmm0,xmm0,0x4e ; Swap halves of LO + vpxor xmm1,xmm1,xmm0 ; Fold LO into MI + vpclmulqdq xmm0,xmm6,xmm1,0x01 ; MI_L*(x^63 + x^62 + x^57) + vpshufd xmm1,xmm1,0x4e ; Swap halves of MI + vpxor xmm5,xmm5,xmm1 ; Fold MI into HI (part 1) + vpxor xmm5,xmm5,xmm0 ; Fold MI into HI (part 2) +; Create H_CUR = [H^2, H^1] and H_INC = [H^2, H^2]. vinserti128 ymm3,ymm5,xmm3,1 vinserti128 ymm5,ymm5,xmm5,1 - - vpclmulqdq ymm0,ymm3,ymm5,0x00 - vpclmulqdq ymm1,ymm3,ymm5,0x01 - vpclmulqdq ymm2,ymm3,ymm5,0x10 - vpxor ymm1,ymm1,ymm2 - vpclmulqdq ymm2,ymm6,ymm0,0x01 - vpshufd ymm0,ymm0,0x4e - vpxor ymm1,ymm1,ymm0 - vpxor ymm1,ymm1,ymm2 - vpclmulqdq ymm4,ymm3,ymm5,0x11 - vpclmulqdq ymm0,ymm6,ymm1,0x01 - vpshufd ymm1,ymm1,0x4e - vpxor ymm4,ymm4,ymm1 - vpxor ymm4,ymm4,ymm0 - - - +; Compute H_CUR2 = [H^4, H^3]. + vpclmulqdq ymm0,ymm3,ymm5,0x00 ; LO = a_L * b_L + vpclmulqdq ymm1,ymm3,ymm5,0x01 ; MI_0 = a_L * b_H + vpclmulqdq ymm2,ymm3,ymm5,0x10 ; MI_1 = a_H * b_L + vpxor ymm1,ymm1,ymm2 ; MI = MI_0 + MI_1 + vpclmulqdq ymm2,ymm6,ymm0,0x01 ; LO_L*(x^63 + x^62 + x^57) + vpshufd ymm0,ymm0,0x4e ; Swap halves of LO + vpxor ymm1,ymm1,ymm0 ; Fold LO into MI (part 1) + vpxor ymm1,ymm1,ymm2 ; Fold LO into MI (part 2) + vpclmulqdq ymm4,ymm3,ymm5,0x11 ; HI = a_H * b_H + vpclmulqdq ymm0,ymm6,ymm1,0x01 ; MI_L*(x^63 + x^62 + x^57) + vpshufd ymm1,ymm1,0x4e ; Swap halves of MI + vpxor ymm4,ymm4,ymm1 ; Fold MI into HI (part 1) + vpxor ymm4,ymm4,ymm0 ; Fold MI into HI (part 2) + + +; Store [H^2, H^1] and [H^4, H^3]. vmovdqu YMMWORD[96+rcx],ymm3 vmovdqu YMMWORD[64+rcx],ymm4 - - +; For Karatsuba multiplication: compute and store the two 64-bit halves of +; each key power XOR'd together. Order is 4,2,3,1. vpunpcklqdq ymm0,ymm4,ymm3 vpunpckhqdq ymm1,ymm4,ymm3 vpxor ymm0,ymm0,ymm1 vmovdqu YMMWORD[(128+32)+rcx],ymm0 - - vpclmulqdq ymm0,ymm4,ymm5,0x00 - vpclmulqdq ymm1,ymm4,ymm5,0x01 - vpclmulqdq ymm2,ymm4,ymm5,0x10 - vpxor ymm1,ymm1,ymm2 - vpclmulqdq ymm2,ymm6,ymm0,0x01 - vpshufd ymm0,ymm0,0x4e - vpxor ymm1,ymm1,ymm0 - vpxor ymm1,ymm1,ymm2 - vpclmulqdq ymm3,ymm4,ymm5,0x11 - vpclmulqdq ymm0,ymm6,ymm1,0x01 - vpshufd ymm1,ymm1,0x4e - vpxor ymm3,ymm3,ymm1 - vpxor ymm3,ymm3,ymm0 - - vpclmulqdq ymm0,ymm3,ymm5,0x00 - vpclmulqdq ymm1,ymm3,ymm5,0x01 - vpclmulqdq ymm2,ymm3,ymm5,0x10 - vpxor ymm1,ymm1,ymm2 - vpclmulqdq ymm2,ymm6,ymm0,0x01 - vpshufd ymm0,ymm0,0x4e - vpxor ymm1,ymm1,ymm0 - vpxor ymm1,ymm1,ymm2 - vpclmulqdq ymm4,ymm3,ymm5,0x11 - vpclmulqdq ymm0,ymm6,ymm1,0x01 - vpshufd ymm1,ymm1,0x4e - vpxor ymm4,ymm4,ymm1 - vpxor ymm4,ymm4,ymm0 +; Compute and store H_CUR = [H^6, H^5] and H_CUR2 = [H^8, H^7]. + vpclmulqdq ymm0,ymm4,ymm5,0x00 ; LO = a_L * b_L + vpclmulqdq ymm1,ymm4,ymm5,0x01 ; MI_0 = a_L * b_H + vpclmulqdq ymm2,ymm4,ymm5,0x10 ; MI_1 = a_H * b_L + vpxor ymm1,ymm1,ymm2 ; MI = MI_0 + MI_1 + vpclmulqdq ymm2,ymm6,ymm0,0x01 ; LO_L*(x^63 + x^62 + x^57) + vpshufd ymm0,ymm0,0x4e ; Swap halves of LO + vpxor ymm1,ymm1,ymm0 ; Fold LO into MI (part 1) + vpxor ymm1,ymm1,ymm2 ; Fold LO into MI (part 2) + vpclmulqdq ymm3,ymm4,ymm5,0x11 ; HI = a_H * b_H + vpclmulqdq ymm0,ymm6,ymm1,0x01 ; MI_L*(x^63 + x^62 + x^57) + vpshufd ymm1,ymm1,0x4e ; Swap halves of MI + vpxor ymm3,ymm3,ymm1 ; Fold MI into HI (part 1) + vpxor ymm3,ymm3,ymm0 ; Fold MI into HI (part 2) + + vpclmulqdq ymm0,ymm3,ymm5,0x00 ; LO = a_L * b_L + vpclmulqdq ymm1,ymm3,ymm5,0x01 ; MI_0 = a_L * b_H + vpclmulqdq ymm2,ymm3,ymm5,0x10 ; MI_1 = a_H * b_L + vpxor ymm1,ymm1,ymm2 ; MI = MI_0 + MI_1 + vpclmulqdq ymm2,ymm6,ymm0,0x01 ; LO_L*(x^63 + x^62 + x^57) + vpshufd ymm0,ymm0,0x4e ; Swap halves of LO + vpxor ymm1,ymm1,ymm0 ; Fold LO into MI (part 1) + vpxor ymm1,ymm1,ymm2 ; Fold LO into MI (part 2) + vpclmulqdq ymm4,ymm3,ymm5,0x11 ; HI = a_H * b_H + vpclmulqdq ymm0,ymm6,ymm1,0x01 ; MI_L*(x^63 + x^62 + x^57) + vpshufd ymm1,ymm1,0x4e ; Swap halves of MI + vpxor ymm4,ymm4,ymm1 ; Fold MI into HI (part 1) + vpxor ymm4,ymm4,ymm0 ; Fold MI into HI (part 2) vmovdqu YMMWORD[32+rcx],ymm3 vmovdqu YMMWORD[rcx],ymm4 - - +; Again, compute and store the two 64-bit halves of each key power XOR'd +; together. Order is 8,6,7,5. vpunpcklqdq ymm0,ymm4,ymm3 vpunpckhqdq ymm1,ymm4,ymm3 vpxor ymm0,ymm0,ymm1 @@ -182,25 +184,25 @@ $L$SEH_endprologue_gcm_gmult_vpclmulqdq_avx2_4: vmovdqu xmm3,XMMWORD[$L$gfpoly] vpshufb xmm0,xmm0,xmm1 - vpclmulqdq xmm4,xmm0,xmm2,0x00 - vpclmulqdq xmm5,xmm0,xmm2,0x01 - vpclmulqdq xmm6,xmm0,xmm2,0x10 - vpxor xmm5,xmm5,xmm6 - vpclmulqdq xmm6,xmm3,xmm4,0x01 - vpshufd xmm4,xmm4,0x4e - vpxor xmm5,xmm5,xmm4 - vpxor xmm5,xmm5,xmm6 - vpclmulqdq xmm0,xmm0,xmm2,0x11 - vpclmulqdq xmm4,xmm3,xmm5,0x01 - vpshufd xmm5,xmm5,0x4e - vpxor xmm0,xmm0,xmm5 - vpxor xmm0,xmm0,xmm4 + vpclmulqdq xmm4,xmm0,xmm2,0x00 ; LO = a_L * b_L + vpclmulqdq xmm5,xmm0,xmm2,0x01 ; MI_0 = a_L * b_H + vpclmulqdq xmm6,xmm0,xmm2,0x10 ; MI_1 = a_H * b_L + vpxor xmm5,xmm5,xmm6 ; MI = MI_0 + MI_1 + vpclmulqdq xmm6,xmm3,xmm4,0x01 ; LO_L*(x^63 + x^62 + x^57) + vpshufd xmm4,xmm4,0x4e ; Swap halves of LO + vpxor xmm5,xmm5,xmm4 ; Fold LO into MI (part 1) + vpxor xmm5,xmm5,xmm6 ; Fold LO into MI (part 2) + vpclmulqdq xmm0,xmm0,xmm2,0x11 ; HI = a_H * b_H + vpclmulqdq xmm4,xmm3,xmm5,0x01 ; MI_L*(x^63 + x^62 + x^57) + vpshufd xmm5,xmm5,0x4e ; Swap halves of MI + vpxor xmm0,xmm0,xmm5 ; Fold MI into HI (part 1) + vpxor xmm0,xmm0,xmm4 ; Fold MI into HI (part 2) vpshufb xmm0,xmm0,xmm1 vmovdqu XMMWORD[rcx],xmm0 - +; No need for vzeroupper, since only xmm registers were used. vmovdqa xmm6,XMMWORD[rsp] add rsp,24 ret @@ -227,33 +229,33 @@ $L$SEH_prologue_gcm_ghash_vpclmulqdq_avx2_6: $L$SEH_endprologue_gcm_ghash_vpclmulqdq_avx2_7: - - - +; Load the bswap_mask and gfpoly constants. Since AADLEN is usually small, +; usually only 128-bit vectors will be used. So as an optimization, don't +; broadcast these constants to both 128-bit lanes quite yet. vmovdqu xmm6,XMMWORD[$L$bswap_mask] vmovdqu xmm7,XMMWORD[$L$gfpoly] - +; Load the GHASH accumulator. vmovdqu xmm5,XMMWORD[rcx] vpshufb xmm5,xmm5,xmm6 - +; Optimize for AADLEN < 32 by checking for AADLEN < 32 before AADLEN < 128. cmp r9,32 jb NEAR $L$ghash_lastblock - - +; AADLEN >= 32, so we'll operate on full vectors. Broadcast bswap_mask and +; gfpoly to both 128-bit lanes. vinserti128 ymm6,ymm6,xmm6,1 vinserti128 ymm7,ymm7,xmm7,1 cmp r9,127 jbe NEAR $L$ghash_loop_1x - +; Update GHASH with 128 bytes of AAD at a time. vmovdqu ymm8,YMMWORD[128+rdx] vmovdqu ymm9,YMMWORD[((128+32))+rdx] $L$ghash_loop_4x: - +; First vector vmovdqu ymm1,YMMWORD[r8] vpshufb ymm1,ymm1,ymm6 vmovdqu ymm2,YMMWORD[rdx] @@ -263,7 +265,7 @@ $L$ghash_loop_4x: vpunpckhqdq ymm0,ymm1,ymm1 vpxor ymm0,ymm0,ymm1 vpclmulqdq ymm4,ymm0,ymm8,0x00 - +; Second vector vmovdqu ymm1,YMMWORD[32+r8] vpshufb ymm1,ymm1,ymm6 vmovdqu ymm2,YMMWORD[32+rdx] @@ -275,7 +277,7 @@ $L$ghash_loop_4x: vpxor ymm0,ymm0,ymm1 vpclmulqdq ymm0,ymm0,ymm8,0x10 vpxor ymm4,ymm4,ymm0 - +; Third vector vmovdqu ymm1,YMMWORD[64+r8] vpshufb ymm1,ymm1,ymm6 vmovdqu ymm2,YMMWORD[64+rdx] @@ -288,7 +290,7 @@ $L$ghash_loop_4x: vpclmulqdq ymm0,ymm0,ymm9,0x00 vpxor ymm4,ymm4,ymm0 - +; Fourth vector vmovdqu ymm1,YMMWORD[96+r8] vpshufb ymm1,ymm1,ymm6 vmovdqu ymm2,YMMWORD[96+rdx] @@ -300,17 +302,17 @@ $L$ghash_loop_4x: vpxor ymm0,ymm0,ymm1 vpclmulqdq ymm0,ymm0,ymm9,0x10 vpxor ymm4,ymm4,ymm0 - +; Finalize 'mi' following Karatsuba multiplication. vpxor ymm4,ymm4,ymm3 vpxor ymm4,ymm4,ymm5 - +; Fold lo into mi. vbroadcasti128 ymm2,XMMWORD[$L$gfpoly] vpclmulqdq ymm0,ymm2,ymm3,0x01 vpshufd ymm3,ymm3,0x4e vpxor ymm4,ymm4,ymm3 vpxor ymm4,ymm4,ymm0 - +; Fold mi into hi. vpclmulqdq ymm0,ymm2,ymm4,0x01 vpshufd ymm4,ymm4,0x4e vpxor ymm5,ymm5,ymm4 @@ -318,12 +320,12 @@ $L$ghash_loop_4x: vextracti128 xmm0,ymm5,1 vpxor xmm5,xmm5,xmm0 - sub r8,-128 + sub r8,-128 ; 128 is 4 bytes, -128 is 1 byte add r9,-128 cmp r9,127 ja NEAR $L$ghash_loop_4x - +; Update GHASH with 32 bytes of AAD at a time. cmp r9,32 jb NEAR $L$ghash_loop_1x_done $L$ghash_loop_1x: @@ -331,19 +333,19 @@ $L$ghash_loop_1x: vpshufb ymm0,ymm0,ymm6 vpxor ymm5,ymm5,ymm0 vmovdqu ymm0,YMMWORD[((128-32))+rdx] - vpclmulqdq ymm1,ymm5,ymm0,0x00 - vpclmulqdq ymm2,ymm5,ymm0,0x01 - vpclmulqdq ymm3,ymm5,ymm0,0x10 - vpxor ymm2,ymm2,ymm3 - vpclmulqdq ymm3,ymm7,ymm1,0x01 - vpshufd ymm1,ymm1,0x4e - vpxor ymm2,ymm2,ymm1 - vpxor ymm2,ymm2,ymm3 - vpclmulqdq ymm5,ymm5,ymm0,0x11 - vpclmulqdq ymm1,ymm7,ymm2,0x01 - vpshufd ymm2,ymm2,0x4e - vpxor ymm5,ymm5,ymm2 - vpxor ymm5,ymm5,ymm1 + vpclmulqdq ymm1,ymm5,ymm0,0x00 ; LO = a_L * b_L + vpclmulqdq ymm2,ymm5,ymm0,0x01 ; MI_0 = a_L * b_H + vpclmulqdq ymm3,ymm5,ymm0,0x10 ; MI_1 = a_H * b_L + vpxor ymm2,ymm2,ymm3 ; MI = MI_0 + MI_1 + vpclmulqdq ymm3,ymm7,ymm1,0x01 ; LO_L*(x^63 + x^62 + x^57) + vpshufd ymm1,ymm1,0x4e ; Swap halves of LO + vpxor ymm2,ymm2,ymm1 ; Fold LO into MI (part 1) + vpxor ymm2,ymm2,ymm3 ; Fold LO into MI (part 2) + vpclmulqdq ymm5,ymm5,ymm0,0x11 ; HI = a_H * b_H + vpclmulqdq ymm1,ymm7,ymm2,0x01 ; MI_L*(x^63 + x^62 + x^57) + vpshufd ymm2,ymm2,0x4e ; Swap halves of MI + vpxor ymm5,ymm5,ymm2 ; Fold MI into HI (part 1) + vpxor ymm5,ymm5,ymm1 ; Fold MI into HI (part 2) vextracti128 xmm0,ymm5,1 vpxor xmm5,xmm5,xmm0 @@ -353,7 +355,7 @@ $L$ghash_loop_1x: jae NEAR $L$ghash_loop_1x $L$ghash_loop_1x_done: - +; Update GHASH with the remaining 16-byte block if any. $L$ghash_lastblock: test r9,r9 jz NEAR $L$ghash_done @@ -361,23 +363,23 @@ $L$ghash_lastblock: vpshufb xmm0,xmm0,xmm6 vpxor xmm5,xmm5,xmm0 vmovdqu xmm0,XMMWORD[((128-16))+rdx] - vpclmulqdq xmm1,xmm5,xmm0,0x00 - vpclmulqdq xmm2,xmm5,xmm0,0x01 - vpclmulqdq xmm3,xmm5,xmm0,0x10 - vpxor xmm2,xmm2,xmm3 - vpclmulqdq xmm3,xmm7,xmm1,0x01 - vpshufd xmm1,xmm1,0x4e - vpxor xmm2,xmm2,xmm1 - vpxor xmm2,xmm2,xmm3 - vpclmulqdq xmm5,xmm5,xmm0,0x11 - vpclmulqdq xmm1,xmm7,xmm2,0x01 - vpshufd xmm2,xmm2,0x4e - vpxor xmm5,xmm5,xmm2 - vpxor xmm5,xmm5,xmm1 + vpclmulqdq xmm1,xmm5,xmm0,0x00 ; LO = a_L * b_L + vpclmulqdq xmm2,xmm5,xmm0,0x01 ; MI_0 = a_L * b_H + vpclmulqdq xmm3,xmm5,xmm0,0x10 ; MI_1 = a_H * b_L + vpxor xmm2,xmm2,xmm3 ; MI = MI_0 + MI_1 + vpclmulqdq xmm3,xmm7,xmm1,0x01 ; LO_L*(x^63 + x^62 + x^57) + vpshufd xmm1,xmm1,0x4e ; Swap halves of LO + vpxor xmm2,xmm2,xmm1 ; Fold LO into MI (part 1) + vpxor xmm2,xmm2,xmm3 ; Fold LO into MI (part 2) + vpclmulqdq xmm5,xmm5,xmm0,0x11 ; HI = a_H * b_H + vpclmulqdq xmm1,xmm7,xmm2,0x01 ; MI_L*(x^63 + x^62 + x^57) + vpshufd xmm2,xmm2,0x4e ; Swap halves of MI + vpxor xmm5,xmm5,xmm2 ; Fold MI into HI (part 1) + vpxor xmm5,xmm5,xmm1 ; Fold MI into HI (part 2) $L$ghash_done: - +; Store the updated GHASH accumulator back to memory. vpshufb xmm5,xmm5,xmm6 vmovdqu XMMWORD[rcx],xmm5 @@ -405,9 +407,9 @@ $L$SEH_prologue_aes_gcm_enc_update_vaes_avx2_3: push r12 $L$SEH_prologue_aes_gcm_enc_update_vaes_avx2_4: - mov rsi,QWORD[64+rsp] - mov rdi,QWORD[72+rsp] - mov r12,QWORD[80+rsp] + mov rsi,QWORD[64+rsp] ; arg5 + mov rdi,QWORD[72+rsp] ; arg6 + mov r12,QWORD[80+rsp] ; arg7 sub rsp,160 $L$SEH_prologue_aes_gcm_enc_update_vaes_avx2_5: vmovdqa XMMWORD[rsp],xmm6 @@ -438,38 +440,38 @@ EXTERN BORINGSSL_function_hit %endif vbroadcasti128 ymm0,XMMWORD[$L$bswap_mask] - - +; Load the GHASH accumulator and the starting counter. +; BoringSSL passes these values in big endian format. vmovdqu xmm1,XMMWORD[r12] vpshufb xmm1,xmm1,xmm0 vbroadcasti128 ymm11,XMMWORD[rsi] vpshufb ymm11,ymm11,ymm0 - - +; Load the AES key length in bytes. BoringSSL stores number of rounds +; minus 1, so convert using: AESKEYLEN = 4 * aeskey->rounds - 20. mov r10d,DWORD[240+r9] lea r10d,[((-20))+r10*4] - - - +; Make RNDKEYLAST_PTR point to the last AES round key. This is the +; round key with index 10, 12, or 14 for AES-128, AES-192, or AES-256 +; respectively. Then load the zero-th and last round keys. lea r11,[96+r10*4+r9] vbroadcasti128 ymm9,XMMWORD[r9] vbroadcasti128 ymm10,XMMWORD[r11] - +; Finish initializing LE_CTR by adding 1 to the second block. vpaddd ymm11,ymm11,YMMWORD[$L$ctr_pattern] - - +; If there are at least 128 bytes of data, then continue into the loop that +; processes 128 bytes of data at a time. Otherwise skip it. cmp r8,127 jbe NEAR $L$crypt_loop_4x_done__func1 vmovdqu ymm7,YMMWORD[128+rdi] vmovdqu ymm8,YMMWORD[((128+32))+rdi] - - - +; Encrypt the first 4 vectors of plaintext blocks. +; Increment le_ctr four times to generate four vectors of little-endian +; counter blocks, swap each to big-endian, and store them in aesdata[0-3]. vmovdqu ymm2,YMMWORD[$L$inc_2blocks] vpshufb ymm12,ymm11,ymm0 vpaddd ymm11,ymm11,ymm2 @@ -480,7 +482,7 @@ EXTERN BORINGSSL_function_hit vpshufb ymm15,ymm11,ymm0 vpaddd ymm11,ymm11,ymm2 - +; AES "round zero": XOR in the zero-th round key. vpxor ymm12,ymm12,ymm9 vpxor ymm13,ymm13,ymm9 vpxor ymm14,ymm14,ymm9 @@ -510,16 +512,16 @@ $L$vaesenc_loop_first_4_vecs__func1: vmovdqu YMMWORD[64+rdx],ymm14 vmovdqu YMMWORD[96+rdx],ymm15 - sub rcx,-128 + sub rcx,-128 ; 128 is 4 bytes, -128 is 1 byte add r8,-128 cmp r8,127 jbe NEAR $L$ghash_last_ciphertext_4x__func1 ALIGN 16 $L$crypt_loop_4x__func1: - - - +; Start the AES encryption of the counter blocks. +; Increment le_ctr four times to generate four vectors of little-endian +; counter blocks, swap each to big-endian, and store them in aesdata[0-3]. vmovdqu ymm2,YMMWORD[$L$inc_2blocks] vpshufb ymm12,ymm11,ymm0 vpaddd ymm11,ymm11,ymm2 @@ -530,7 +532,7 @@ $L$crypt_loop_4x__func1: vpshufb ymm15,ymm11,ymm0 vpaddd ymm11,ymm11,ymm2 - +; AES "round zero": XOR in the zero-th round key. vpxor ymm12,ymm12,ymm9 vpxor ymm13,ymm13,ymm9 vpxor ymm14,ymm14,ymm9 @@ -539,7 +541,7 @@ $L$crypt_loop_4x__func1: cmp r10d,24 jl NEAR $L$aes128__func1 je NEAR $L$aes192__func1 - +; AES-256 vbroadcasti128 ymm2,XMMWORD[((-208))+r11] vaesenc ymm12,ymm12,ymm2 vaesenc ymm13,ymm13,ymm2 @@ -568,7 +570,7 @@ $L$aes192__func1: $L$aes128__func1: prefetcht0 [512+rcx] prefetcht0 [((512+64))+rcx] - +; First vector vmovdqu ymm3,YMMWORD[rdx] vpshufb ymm3,ymm3,ymm0 vmovdqu ymm4,YMMWORD[rdi] @@ -592,7 +594,7 @@ $L$aes128__func1: vaesenc ymm14,ymm14,ymm2 vaesenc ymm15,ymm15,ymm2 - +; Second vector vmovdqu ymm3,YMMWORD[32+rdx] vpshufb ymm3,ymm3,ymm0 vmovdqu ymm4,YMMWORD[32+rdi] @@ -611,7 +613,7 @@ $L$aes128__func1: vaesenc ymm14,ymm14,ymm2 vaesenc ymm15,ymm15,ymm2 - +; Third vector vmovdqu ymm3,YMMWORD[64+rdx] vpshufb ymm3,ymm3,ymm0 vmovdqu ymm4,YMMWORD[64+rdi] @@ -638,7 +640,7 @@ $L$aes128__func1: vpclmulqdq ymm2,ymm2,ymm8,0x00 vpxor ymm6,ymm6,ymm2 - +; Fourth vector vmovdqu ymm3,YMMWORD[96+rdx] vpshufb ymm3,ymm3,ymm0 @@ -664,11 +666,11 @@ $L$aes128__func1: vaesenc ymm14,ymm14,ymm2 vaesenc ymm15,ymm15,ymm2 - +; Finalize 'mi' following Karatsuba multiplication. vpxor ymm6,ymm6,ymm5 vpxor ymm6,ymm6,ymm1 - +; Fold lo into mi. vbroadcasti128 ymm4,XMMWORD[$L$gfpoly] vpclmulqdq ymm2,ymm4,ymm5,0x01 vpshufd ymm5,ymm5,0x4e @@ -681,7 +683,7 @@ $L$aes128__func1: vaesenc ymm14,ymm14,ymm2 vaesenc ymm15,ymm15,ymm2 - +; Fold mi into hi. vpclmulqdq ymm2,ymm4,ymm6,0x01 vpshufd ymm6,ymm6,0x4e vpxor ymm1,ymm1,ymm6 @@ -697,7 +699,7 @@ $L$aes128__func1: vpxor xmm1,xmm1,xmm2 - sub rdx,-128 + sub rdx,-128 ; 128 is 4 bytes, -128 is 1 byte vpxor ymm2,ymm10,YMMWORD[rcx] vpxor ymm3,ymm10,YMMWORD[32+rcx] vpxor ymm5,ymm10,YMMWORD[64+rcx] @@ -717,7 +719,7 @@ $L$aes128__func1: cmp r8,127 ja NEAR $L$crypt_loop_4x__func1 $L$ghash_last_ciphertext_4x__func1: - +; First vector vmovdqu ymm3,YMMWORD[rdx] vpshufb ymm3,ymm3,ymm0 vmovdqu ymm4,YMMWORD[rdi] @@ -727,7 +729,7 @@ $L$ghash_last_ciphertext_4x__func1: vpunpckhqdq ymm2,ymm3,ymm3 vpxor ymm2,ymm2,ymm3 vpclmulqdq ymm6,ymm2,ymm7,0x00 - +; Second vector vmovdqu ymm3,YMMWORD[32+rdx] vpshufb ymm3,ymm3,ymm0 vmovdqu ymm4,YMMWORD[32+rdi] @@ -739,7 +741,7 @@ $L$ghash_last_ciphertext_4x__func1: vpxor ymm2,ymm2,ymm3 vpclmulqdq ymm2,ymm2,ymm7,0x10 vpxor ymm6,ymm6,ymm2 - +; Third vector vmovdqu ymm3,YMMWORD[64+rdx] vpshufb ymm3,ymm3,ymm0 vmovdqu ymm4,YMMWORD[64+rdi] @@ -752,7 +754,7 @@ $L$ghash_last_ciphertext_4x__func1: vpclmulqdq ymm2,ymm2,ymm8,0x00 vpxor ymm6,ymm6,ymm2 - +; Fourth vector vmovdqu ymm3,YMMWORD[96+rdx] vpshufb ymm3,ymm3,ymm0 vmovdqu ymm4,YMMWORD[96+rdi] @@ -764,17 +766,17 @@ $L$ghash_last_ciphertext_4x__func1: vpxor ymm2,ymm2,ymm3 vpclmulqdq ymm2,ymm2,ymm8,0x10 vpxor ymm6,ymm6,ymm2 - +; Finalize 'mi' following Karatsuba multiplication. vpxor ymm6,ymm6,ymm5 vpxor ymm6,ymm6,ymm1 - +; Fold lo into mi. vbroadcasti128 ymm4,XMMWORD[$L$gfpoly] vpclmulqdq ymm2,ymm4,ymm5,0x01 vpshufd ymm5,ymm5,0x4e vpxor ymm6,ymm6,ymm5 vpxor ymm6,ymm6,ymm2 - +; Fold mi into hi. vpclmulqdq ymm2,ymm4,ymm6,0x01 vpshufd ymm6,ymm6,0x4e vpxor ymm1,ymm1,ymm6 @@ -784,18 +786,18 @@ $L$ghash_last_ciphertext_4x__func1: sub rdx,-128 $L$crypt_loop_4x_done__func1: - +; Check whether any data remains. test r8,r8 jz NEAR $L$done__func1 +; DATALEN is in [16, 32, 48, 64, 80, 96, 112]. - - - +; Make POWERS_PTR point to the key powers [H^N, H^(N-1), ...] where N +; is the number of blocks that remain. lea rsi,[128+rdi] sub rsi,r8 - +; Start collecting the unreduced GHASH intermediate value LO, MI, HI. vpxor xmm5,xmm5,xmm5 vpxor xmm6,xmm6,xmm6 vpxor xmm7,xmm7,xmm7 @@ -803,7 +805,7 @@ $L$crypt_loop_4x_done__func1: cmp r8,64 jb NEAR $L$lessthan64bytes__func1 - +; DATALEN is in [64, 80, 96, 112]. Encrypt two vectors of counter blocks. vpshufb ymm12,ymm11,ymm0 vpaddd ymm11,ymm11,YMMWORD[$L$inc_2blocks] vpshufb ymm13,ymm11,ymm0 @@ -821,7 +823,7 @@ $L$vaesenc_loop_tail_1__func1: vaesenclast ymm12,ymm12,ymm10 vaesenclast ymm13,ymm13,ymm10 - +; XOR the data with the two vectors of keystream blocks. vmovdqu ymm2,YMMWORD[rcx] vmovdqu ymm3,YMMWORD[32+rcx] vpxor ymm12,ymm12,ymm2 @@ -829,7 +831,7 @@ $L$vaesenc_loop_tail_1__func1: vmovdqu YMMWORD[rdx],ymm12 vmovdqu YMMWORD[32+rdx],ymm13 - +; Update GHASH with two vectors of ciphertext blocks, without reducing. vpshufb ymm12,ymm12,ymm0 vpshufb ymm13,ymm13,ymm0 vpxor ymm12,ymm12,ymm1 @@ -857,7 +859,7 @@ $L$vaesenc_loop_tail_1__func1: vpxor xmm1,xmm1,xmm1 - +; DATALEN is in [16, 32, 48]. Encrypt two last vectors of counter blocks. $L$lessthan64bytes__func1: vpshufb ymm12,ymm11,ymm0 vpaddd ymm11,ymm11,YMMWORD[$L$inc_2blocks] @@ -875,8 +877,8 @@ $L$vaesenc_loop_tail_2__func1: vaesenclast ymm12,ymm12,ymm10 vaesenclast ymm13,ymm13,ymm10 - - +; XOR the remaining data with the keystream blocks, and update GHASH with +; the remaining ciphertext blocks without reducing. cmp r8,32 jb NEAR $L$xor_one_block__func1 @@ -933,7 +935,7 @@ $L$ghash_mul_one_vec_unreduced__func1: vpxor ymm7,ymm7,ymm4 $L$reduce__func1: - +; Finally, do the GHASH reduction. vbroadcasti128 ymm2,XMMWORD[$L$gfpoly] vpclmulqdq ymm3,ymm2,ymm5,0x01 vpshufd ymm5,ymm5,0x4e @@ -947,7 +949,7 @@ $L$reduce__func1: vpxor xmm1,xmm1,xmm7 $L$done__func1: - +; Store the updated GHASH accumulator back to memory. vpshufb xmm1,xmm1,xmm0 vmovdqu XMMWORD[r12],xmm1 @@ -984,9 +986,9 @@ $L$SEH_prologue_aes_gcm_dec_update_vaes_avx2_3: push r12 $L$SEH_prologue_aes_gcm_dec_update_vaes_avx2_4: - mov rsi,QWORD[64+rsp] - mov rdi,QWORD[72+rsp] - mov r12,QWORD[80+rsp] + mov rsi,QWORD[64+rsp] ; arg5 + mov rdi,QWORD[72+rsp] ; arg6 + mov r12,QWORD[80+rsp] ; arg7 sub rsp,160 $L$SEH_prologue_aes_gcm_dec_update_vaes_avx2_5: vmovdqa XMMWORD[rsp],xmm6 @@ -1013,30 +1015,30 @@ $L$SEH_prologue_aes_gcm_dec_update_vaes_avx2_15: $L$SEH_endprologue_aes_gcm_dec_update_vaes_avx2_16: vbroadcasti128 ymm0,XMMWORD[$L$bswap_mask] - - +; Load the GHASH accumulator and the starting counter. +; BoringSSL passes these values in big endian format. vmovdqu xmm1,XMMWORD[r12] vpshufb xmm1,xmm1,xmm0 vbroadcasti128 ymm11,XMMWORD[rsi] vpshufb ymm11,ymm11,ymm0 - - +; Load the AES key length in bytes. BoringSSL stores number of rounds +; minus 1, so convert using: AESKEYLEN = 4 * aeskey->rounds - 20. mov r10d,DWORD[240+r9] lea r10d,[((-20))+r10*4] - - - +; Make RNDKEYLAST_PTR point to the last AES round key. This is the +; round key with index 10, 12, or 14 for AES-128, AES-192, or AES-256 +; respectively. Then load the zero-th and last round keys. lea r11,[96+r10*4+r9] vbroadcasti128 ymm9,XMMWORD[r9] vbroadcasti128 ymm10,XMMWORD[r11] - +; Finish initializing LE_CTR by adding 1 to the second block. vpaddd ymm11,ymm11,YMMWORD[$L$ctr_pattern] - - +; If there are at least 128 bytes of data, then continue into the loop that +; processes 128 bytes of data at a time. Otherwise skip it. cmp r8,127 jbe NEAR $L$crypt_loop_4x_done__func2 @@ -1045,9 +1047,9 @@ $L$SEH_endprologue_aes_gcm_dec_update_vaes_avx2_16: ALIGN 16 $L$crypt_loop_4x__func2: - - - +; Start the AES encryption of the counter blocks. +; Increment le_ctr four times to generate four vectors of little-endian +; counter blocks, swap each to big-endian, and store them in aesdata[0-3]. vmovdqu ymm2,YMMWORD[$L$inc_2blocks] vpshufb ymm12,ymm11,ymm0 vpaddd ymm11,ymm11,ymm2 @@ -1058,7 +1060,7 @@ $L$crypt_loop_4x__func2: vpshufb ymm15,ymm11,ymm0 vpaddd ymm11,ymm11,ymm2 - +; AES "round zero": XOR in the zero-th round key. vpxor ymm12,ymm12,ymm9 vpxor ymm13,ymm13,ymm9 vpxor ymm14,ymm14,ymm9 @@ -1067,7 +1069,7 @@ $L$crypt_loop_4x__func2: cmp r10d,24 jl NEAR $L$aes128__func2 je NEAR $L$aes192__func2 - +; AES-256 vbroadcasti128 ymm2,XMMWORD[((-208))+r11] vaesenc ymm12,ymm12,ymm2 vaesenc ymm13,ymm13,ymm2 @@ -1096,7 +1098,7 @@ $L$aes192__func2: $L$aes128__func2: prefetcht0 [512+rcx] prefetcht0 [((512+64))+rcx] - +; First vector vmovdqu ymm3,YMMWORD[rcx] vpshufb ymm3,ymm3,ymm0 vmovdqu ymm4,YMMWORD[rdi] @@ -1120,7 +1122,7 @@ $L$aes128__func2: vaesenc ymm14,ymm14,ymm2 vaesenc ymm15,ymm15,ymm2 - +; Second vector vmovdqu ymm3,YMMWORD[32+rcx] vpshufb ymm3,ymm3,ymm0 vmovdqu ymm4,YMMWORD[32+rdi] @@ -1139,7 +1141,7 @@ $L$aes128__func2: vaesenc ymm14,ymm14,ymm2 vaesenc ymm15,ymm15,ymm2 - +; Third vector vmovdqu ymm3,YMMWORD[64+rcx] vpshufb ymm3,ymm3,ymm0 vmovdqu ymm4,YMMWORD[64+rdi] @@ -1166,7 +1168,7 @@ $L$aes128__func2: vpclmulqdq ymm2,ymm2,ymm8,0x00 vpxor ymm6,ymm6,ymm2 - +; Fourth vector vmovdqu ymm3,YMMWORD[96+rcx] vpshufb ymm3,ymm3,ymm0 @@ -1192,11 +1194,11 @@ $L$aes128__func2: vaesenc ymm14,ymm14,ymm2 vaesenc ymm15,ymm15,ymm2 - +; Finalize 'mi' following Karatsuba multiplication. vpxor ymm6,ymm6,ymm5 vpxor ymm6,ymm6,ymm1 - +; Fold lo into mi. vbroadcasti128 ymm4,XMMWORD[$L$gfpoly] vpclmulqdq ymm2,ymm4,ymm5,0x01 vpshufd ymm5,ymm5,0x4e @@ -1209,7 +1211,7 @@ $L$aes128__func2: vaesenc ymm14,ymm14,ymm2 vaesenc ymm15,ymm15,ymm2 - +; Fold mi into hi. vpclmulqdq ymm2,ymm4,ymm6,0x01 vpshufd ymm6,ymm6,0x4e vpxor ymm1,ymm1,ymm6 @@ -1225,7 +1227,7 @@ $L$aes128__func2: vpxor xmm1,xmm1,xmm2 - +; 128 is 4 bytes, -128 is 1 byte vpxor ymm2,ymm10,YMMWORD[rcx] vpxor ymm3,ymm10,YMMWORD[32+rcx] vpxor ymm5,ymm10,YMMWORD[64+rcx] @@ -1245,18 +1247,18 @@ $L$aes128__func2: cmp r8,127 ja NEAR $L$crypt_loop_4x__func2 $L$crypt_loop_4x_done__func2: - +; Check whether any data remains. test r8,r8 jz NEAR $L$done__func2 +; DATALEN is in [16, 32, 48, 64, 80, 96, 112]. - - - +; Make POWERS_PTR point to the key powers [H^N, H^(N-1), ...] where N +; is the number of blocks that remain. lea rsi,[128+rdi] sub rsi,r8 - +; Start collecting the unreduced GHASH intermediate value LO, MI, HI. vpxor xmm5,xmm5,xmm5 vpxor xmm6,xmm6,xmm6 vpxor xmm7,xmm7,xmm7 @@ -1264,7 +1266,7 @@ $L$crypt_loop_4x_done__func2: cmp r8,64 jb NEAR $L$lessthan64bytes__func2 - +; DATALEN is in [64, 80, 96, 112]. Encrypt two vectors of counter blocks. vpshufb ymm12,ymm11,ymm0 vpaddd ymm11,ymm11,YMMWORD[$L$inc_2blocks] vpshufb ymm13,ymm11,ymm0 @@ -1282,7 +1284,7 @@ $L$vaesenc_loop_tail_1__func2: vaesenclast ymm12,ymm12,ymm10 vaesenclast ymm13,ymm13,ymm10 - +; XOR the data with the two vectors of keystream blocks. vmovdqu ymm2,YMMWORD[rcx] vmovdqu ymm3,YMMWORD[32+rcx] vpxor ymm12,ymm12,ymm2 @@ -1290,7 +1292,7 @@ $L$vaesenc_loop_tail_1__func2: vmovdqu YMMWORD[rdx],ymm12 vmovdqu YMMWORD[32+rdx],ymm13 - +; Update GHASH with two vectors of ciphertext blocks, without reducing. vpshufb ymm12,ymm2,ymm0 vpshufb ymm13,ymm3,ymm0 vpxor ymm12,ymm12,ymm1 @@ -1318,7 +1320,7 @@ $L$vaesenc_loop_tail_1__func2: vpxor xmm1,xmm1,xmm1 - +; DATALEN is in [16, 32, 48]. Encrypt two last vectors of counter blocks. $L$lessthan64bytes__func2: vpshufb ymm12,ymm11,ymm0 vpaddd ymm11,ymm11,YMMWORD[$L$inc_2blocks] @@ -1336,8 +1338,8 @@ $L$vaesenc_loop_tail_2__func2: vaesenclast ymm12,ymm12,ymm10 vaesenclast ymm13,ymm13,ymm10 - - +; XOR the remaining data with the keystream blocks, and update GHASH with +; the remaining ciphertext blocks without reducing. cmp r8,32 jb NEAR $L$xor_one_block__func2 @@ -1394,7 +1396,7 @@ $L$ghash_mul_one_vec_unreduced__func2: vpxor ymm7,ymm7,ymm4 $L$reduce__func2: - +; Finally, do the GHASH reduction. vbroadcasti128 ymm2,XMMWORD[$L$gfpoly] vpclmulqdq ymm3,ymm2,ymm5,0x01 vpshufd ymm5,ymm5,0x4e @@ -1408,7 +1410,7 @@ $L$reduce__func2: vpxor xmm1,xmm1,xmm7 $L$done__func2: - +; Store the updated GHASH accumulator back to memory. vpshufb xmm1,xmm1,xmm0 vmovdqu XMMWORD[r12],xmm1 @@ -1457,7 +1459,7 @@ ALIGN 4 section .xdata rdata align=8 ALIGN 4 $L$SEH_info_gcm_init_vpclmulqdq_avx2_0: - DB 1 + DB 1 ; version 1, no flags DB $L$SEH_endprologue_gcm_init_vpclmulqdq_avx2_4-$L$SEH_begin_gcm_init_vpclmulqdq_avx2_1 DB 3 DB 0 @@ -1469,7 +1471,7 @@ $L$SEH_info_gcm_init_vpclmulqdq_avx2_0: DW 0 $L$SEH_info_gcm_gmult_vpclmulqdq_avx2_0: - DB 1 + DB 1 ; version 1, no flags DB $L$SEH_endprologue_gcm_gmult_vpclmulqdq_avx2_4-$L$SEH_begin_gcm_gmult_vpclmulqdq_avx2_1 DB 3 DB 0 @@ -1481,7 +1483,7 @@ $L$SEH_info_gcm_gmult_vpclmulqdq_avx2_0: DW 0 $L$SEH_info_gcm_ghash_vpclmulqdq_avx2_0: - DB 1 + DB 1 ; version 1, no flags DB $L$SEH_endprologue_gcm_ghash_vpclmulqdq_avx2_7-$L$SEH_begin_gcm_ghash_vpclmulqdq_avx2_1 DB 9 DB 0 @@ -1502,7 +1504,7 @@ $L$SEH_info_gcm_ghash_vpclmulqdq_avx2_0: DW 0 $L$SEH_info_aes_gcm_enc_update_vaes_avx2_0: - DB 1 + DB 1 ; version 1, no flags DB $L$SEH_endprologue_aes_gcm_enc_update_vaes_avx2_16-$L$SEH_begin_aes_gcm_enc_update_vaes_avx2_1 DB 25 DB 0 @@ -1548,7 +1550,7 @@ $L$SEH_info_aes_gcm_enc_update_vaes_avx2_0: DW 0 $L$SEH_info_aes_gcm_dec_update_vaes_avx2_0: - DB 1 + DB 1 ; version 1, no flags DB $L$SEH_endprologue_aes_gcm_dec_update_vaes_avx2_16-$L$SEH_begin_aes_gcm_dec_update_vaes_avx2_1 DB 25 DB 0 diff --git a/third_party/boringssl/gen/bcm/aes-gcm-avx512-x86_64-apple.S b/third_party/boringssl/gen/bcm/aes-gcm-avx512-x86_64-apple.S index 874ec0a47..3e66ec678 100644 --- a/third_party/boringssl/gen/bcm/aes-gcm-avx512-x86_64-apple.S +++ b/third_party/boringssl/gen/bcm/aes-gcm-avx512-x86_64-apple.S @@ -5,34 +5,36 @@ #if !defined(OPENSSL_NO_ASM) && defined(OPENSSL_X86_64) && defined(__APPLE__) .section __DATA,__const -.p2align 6 +.p2align 6 +aes_gcm_avx512_constants: +// A shuffle mask that reflects the bytes of 16-byte blocks L$bswap_mask: .quad 0x08090a0b0c0d0e0f, 0x0001020304050607 - - - - - - - +// This is the GHASH reducing polynomial without its constant term, i.e. +// x^128 + x^7 + x^2 + x, represented using the backwards mapping +// between bits and polynomial coefficients. +// +// Alternatively, it can be interpreted as the naturally-ordered +// representation of the polynomial x^127 + x^126 + x^121 + 1, i.e. the +// "reversed" GHASH reducing polynomial without its x^128 term. L$gfpoly: .quad 1, 0xc200000000000000 - +// Same as above, but with the (1 << 64) bit set. L$gfpoly_and_internal_carrybit: .quad 1, 0xc200000000000001 - +// Values needed to prepare the initial vector of counter blocks. L$ctr_pattern: .quad 0, 0 .quad 1, 0 .quad 2, 0 .quad 3, 0 - +// The number of AES blocks per vector, as a 128-bit value. L$inc_4blocks: .quad 4, 0 @@ -45,100 +47,100 @@ _gcm_init_vpclmulqdq_avx512: _CET_ENDBR - +// Get pointer to lowest set of key powers (located at end of array). leaq 256-64(%rdi),%r8 - - +// Load the byte-reflected hash subkey. BoringSSL provides it in +// byte-reflected form except the two halves are in the wrong order. vpshufd $0x4e,(%rsi),%xmm3 - - - - - - - - - - - - - - - +// Finish preprocessing the first key power, H^1. Since this GHASH +// implementation operates directly on values with the backwards bit +// order specified by the GCM standard, it's necessary to preprocess the +// raw key as follows. First, reflect its bytes. Second, multiply it +// by x^-1 mod x^128 + x^7 + x^2 + x + 1 (if using the backwards +// interpretation of polynomial coefficients), which can also be +// interpreted as multiplication by x mod x^128 + x^127 + x^126 + x^121 +// + 1 using the alternative, natural interpretation of polynomial +// coefficients. For details, see the comment above _ghash_mul. +// +// Either way, for the multiplication the concrete operation performed +// is a left shift of the 128-bit value by 1 bit, then an XOR with (0xc2 +// << 120) | 1 if a 1 bit was carried out. However, there's no 128-bit +// wide shift instruction, so instead double each of the two 64-bit +// halves and incorporate the internal carry bit into the value XOR'd. vpshufd $0xd3,%xmm3,%xmm0 vpsrad $31,%xmm0,%xmm0 vpaddq %xmm3,%xmm3,%xmm3 - +// H_CUR_XMM ^= TMP0_XMM & gfpoly_and_internal_carrybit vpternlogd $0x78,L$gfpoly_and_internal_carrybit(%rip),%xmm0,%xmm3 - +// Load the gfpoly constant. vbroadcasti32x4 L$gfpoly(%rip),%zmm5 - - - - - - - - vpclmulqdq $0x00,%xmm3,%xmm3,%xmm0 - vpclmulqdq $0x11,%xmm3,%xmm3,%xmm4 - vpclmulqdq $0x01,%xmm0,%xmm5,%xmm1 - vpshufd $0x4e,%xmm0,%xmm0 - vpxor %xmm0,%xmm1,%xmm1 - vpclmulqdq $0x01,%xmm1,%xmm5,%xmm0 - vpshufd $0x4e,%xmm1,%xmm1 - vpternlogd $0x96,%xmm0,%xmm1,%xmm4 - - - +// Square H^1 to get H^2. +// +// Note that as with H^1, all higher key powers also need an extra +// factor of x^-1 (or x using the natural interpretation). Nothing +// special needs to be done to make this happen, though: H^1 * H^1 would +// end up with two factors of x^-1, but the multiplication consumes one. +// So the product H^2 ends up with the desired one factor of x^-1. + vpclmulqdq $0x00,%xmm3,%xmm3,%xmm0 // LO = a_L * a_L + vpclmulqdq $0x11,%xmm3,%xmm3,%xmm4 // HI = a_H * a_H + vpclmulqdq $0x01,%xmm0,%xmm5,%xmm1 // LO_L*(x^63 + x^62 + x^57) + vpshufd $0x4e,%xmm0,%xmm0 // Swap halves of LO + vpxor %xmm0,%xmm1,%xmm1 // Fold LO into MI + vpclmulqdq $0x01,%xmm1,%xmm5,%xmm0 // MI_L*(x^63 + x^62 + x^57) + vpshufd $0x4e,%xmm1,%xmm1 // Swap halves of MI + vpternlogd $0x96,%xmm0,%xmm1,%xmm4 // Fold MI into HI + + +// Create H_CUR_YMM = [H^2, H^1] and H_INC_YMM = [H^2, H^2]. vinserti128 $1,%xmm3,%ymm4,%ymm3 vinserti128 $1,%xmm4,%ymm4,%ymm4 - - vpclmulqdq $0x00,%ymm4,%ymm3,%ymm0 - vpclmulqdq $0x01,%ymm4,%ymm3,%ymm1 - vpclmulqdq $0x10,%ymm4,%ymm3,%ymm2 - vpxord %ymm2,%ymm1,%ymm1 - vpclmulqdq $0x01,%ymm0,%ymm5,%ymm2 - vpshufd $0x4e,%ymm0,%ymm0 - vpternlogd $0x96,%ymm2,%ymm0,%ymm1 - vpclmulqdq $0x11,%ymm4,%ymm3,%ymm4 - vpclmulqdq $0x01,%ymm1,%ymm5,%ymm0 - vpshufd $0x4e,%ymm1,%ymm1 - vpternlogd $0x96,%ymm0,%ymm1,%ymm4 +// Create H_CUR = [H^4, H^3, H^2, H^1] and H_INC = [H^4, H^4, H^4, H^4]. + vpclmulqdq $0x00,%ymm4,%ymm3,%ymm0 // LO = a_L * b_L + vpclmulqdq $0x01,%ymm4,%ymm3,%ymm1 // MI_0 = a_L * b_H + vpclmulqdq $0x10,%ymm4,%ymm3,%ymm2 // MI_1 = a_H * b_L + vpxord %ymm2,%ymm1,%ymm1 // MI = MI_0 + MI_1 + vpclmulqdq $0x01,%ymm0,%ymm5,%ymm2 // LO_L*(x^63 + x^62 + x^57) + vpshufd $0x4e,%ymm0,%ymm0 // Swap halves of LO + vpternlogd $0x96,%ymm2,%ymm0,%ymm1 // Fold LO into MI + vpclmulqdq $0x11,%ymm4,%ymm3,%ymm4 // HI = a_H * b_H + vpclmulqdq $0x01,%ymm1,%ymm5,%ymm0 // MI_L*(x^63 + x^62 + x^57) + vpshufd $0x4e,%ymm1,%ymm1 // Swap halves of MI + vpternlogd $0x96,%ymm0,%ymm1,%ymm4 // Fold MI into HI vinserti64x4 $1,%ymm3,%zmm4,%zmm3 vshufi64x2 $0,%zmm4,%zmm4,%zmm4 - +// Store the lowest set of key powers. vmovdqu8 %zmm3,(%r8) - - - +// Compute and store the remaining key powers. +// Repeatedly multiply [H^(i+3), H^(i+2), H^(i+1), H^i] by +// [H^4, H^4, H^4, H^4] to get [H^(i+7), H^(i+6), H^(i+5), H^(i+4)]. movl $3,%eax L$precompute_next: subq $64,%r8 - vpclmulqdq $0x00,%zmm4,%zmm3,%zmm0 - vpclmulqdq $0x01,%zmm4,%zmm3,%zmm1 - vpclmulqdq $0x10,%zmm4,%zmm3,%zmm2 - vpxord %zmm2,%zmm1,%zmm1 - vpclmulqdq $0x01,%zmm0,%zmm5,%zmm2 - vpshufd $0x4e,%zmm0,%zmm0 - vpternlogd $0x96,%zmm2,%zmm0,%zmm1 - vpclmulqdq $0x11,%zmm4,%zmm3,%zmm3 - vpclmulqdq $0x01,%zmm1,%zmm5,%zmm0 - vpshufd $0x4e,%zmm1,%zmm1 - vpternlogd $0x96,%zmm0,%zmm1,%zmm3 + vpclmulqdq $0x00,%zmm4,%zmm3,%zmm0 // LO = a_L * b_L + vpclmulqdq $0x01,%zmm4,%zmm3,%zmm1 // MI_0 = a_L * b_H + vpclmulqdq $0x10,%zmm4,%zmm3,%zmm2 // MI_1 = a_H * b_L + vpxord %zmm2,%zmm1,%zmm1 // MI = MI_0 + MI_1 + vpclmulqdq $0x01,%zmm0,%zmm5,%zmm2 // LO_L*(x^63 + x^62 + x^57) + vpshufd $0x4e,%zmm0,%zmm0 // Swap halves of LO + vpternlogd $0x96,%zmm2,%zmm0,%zmm1 // Fold LO into MI + vpclmulqdq $0x11,%zmm4,%zmm3,%zmm3 // HI = a_H * b_H + vpclmulqdq $0x01,%zmm1,%zmm5,%zmm0 // MI_L*(x^63 + x^62 + x^57) + vpshufd $0x4e,%zmm1,%zmm1 // Swap halves of MI + vpternlogd $0x96,%zmm0,%zmm1,%zmm3 // Fold MI into HI vmovdqu8 %zmm3,(%r8) decl %eax jnz L$precompute_next - vzeroupper + vzeroupper // This is needed after using ymm or zmm registers. ret @@ -147,6 +149,7 @@ L$precompute_next: .private_extern _gcm_gmult_vpclmulqdq_avx512 .p2align 5 +.alt_entry _gcm_gmult_vpclmulqdq_avx512 _gcm_gmult_vpclmulqdq_avx512: @@ -160,23 +163,23 @@ _CET_ENDBR vmovdqu L$gfpoly(%rip),%xmm3 vpshufb %xmm1,%xmm0,%xmm0 - vpclmulqdq $0x00,%xmm2,%xmm0,%xmm4 - vpclmulqdq $0x01,%xmm2,%xmm0,%xmm5 - vpclmulqdq $0x10,%xmm2,%xmm0,%xmm6 - vpxord %xmm6,%xmm5,%xmm5 - vpclmulqdq $0x01,%xmm4,%xmm3,%xmm6 - vpshufd $0x4e,%xmm4,%xmm4 - vpternlogd $0x96,%xmm6,%xmm4,%xmm5 - vpclmulqdq $0x11,%xmm2,%xmm0,%xmm0 - vpclmulqdq $0x01,%xmm5,%xmm3,%xmm4 - vpshufd $0x4e,%xmm5,%xmm5 - vpternlogd $0x96,%xmm4,%xmm5,%xmm0 + vpclmulqdq $0x00,%xmm2,%xmm0,%xmm4 // LO = a_L * b_L + vpclmulqdq $0x01,%xmm2,%xmm0,%xmm5 // MI_0 = a_L * b_H + vpclmulqdq $0x10,%xmm2,%xmm0,%xmm6 // MI_1 = a_H * b_L + vpxord %xmm6,%xmm5,%xmm5 // MI = MI_0 + MI_1 + vpclmulqdq $0x01,%xmm4,%xmm3,%xmm6 // LO_L*(x^63 + x^62 + x^57) + vpshufd $0x4e,%xmm4,%xmm4 // Swap halves of LO + vpternlogd $0x96,%xmm6,%xmm4,%xmm5 // Fold LO into MI + vpclmulqdq $0x11,%xmm2,%xmm0,%xmm0 // HI = a_H * b_H + vpclmulqdq $0x01,%xmm5,%xmm3,%xmm4 // MI_L*(x^63 + x^62 + x^57) + vpshufd $0x4e,%xmm5,%xmm5 // Swap halves of MI + vpternlogd $0x96,%xmm4,%xmm5,%xmm0 // Fold MI into HI vpshufb %xmm1,%xmm0,%xmm0 vmovdqu %xmm0,(%rdi) - +// No need for vzeroupper, since only xmm registers were used. ret @@ -185,6 +188,7 @@ _CET_ENDBR .private_extern _gcm_ghash_vpclmulqdq_avx512 .p2align 5 +.alt_entry _gcm_ghash_vpclmulqdq_avx512 _gcm_ghash_vpclmulqdq_avx512: @@ -192,37 +196,37 @@ _CET_ENDBR - - - +// Load the bswap_mask and gfpoly constants. Since AADLEN is usually small, +// usually only 128-bit vectors will be used. So as an optimization, don't +// broadcast these constants to all 128-bit lanes quite yet. vmovdqu L$bswap_mask(%rip),%xmm4 vmovdqu L$gfpoly(%rip),%xmm10 - +// Load the GHASH accumulator. vmovdqu (%rdi),%xmm5 vpshufb %xmm4,%xmm5,%xmm5 - +// Optimize for AADLEN < 64 by checking for AADLEN < 64 before AADLEN < 256. cmpq $64,%rcx jb L$aad_blockbyblock - - +// AADLEN >= 64, so we'll operate on full vectors. Broadcast bswap_mask and +// gfpoly to all 128-bit lanes. vshufi64x2 $0,%zmm4,%zmm4,%zmm4 vshufi64x2 $0,%zmm10,%zmm10,%zmm10 - +// Load the lowest set of key powers. vmovdqu8 256-64(%rsi),%zmm9 cmpq $256,%rcx jb L$aad_loop_1x - +// AADLEN >= 256. Load the higher key powers. vmovdqu8 256-256(%rsi),%zmm6 vmovdqu8 256-192(%rsi),%zmm7 vmovdqu8 256-128(%rsi),%zmm8 - +// Update GHASH with 256 bytes of AAD at a time. L$aad_loop_4x: vmovdqu8 0(%rdx),%zmm0 vmovdqu8 64(%rdx),%zmm1 @@ -233,36 +237,36 @@ L$aad_loop_4x: vpshufb %zmm4,%zmm1,%zmm1 vpshufb %zmm4,%zmm2,%zmm2 vpshufb %zmm4,%zmm3,%zmm3 - vpclmulqdq $0x00,%zmm6,%zmm0,%zmm5 - vpclmulqdq $0x00,%zmm7,%zmm1,%zmm11 - vpclmulqdq $0x00,%zmm8,%zmm2,%zmm12 - vpxord %zmm11,%zmm5,%zmm5 - vpclmulqdq $0x00,%zmm9,%zmm3,%zmm13 - vpternlogd $0x96,%zmm13,%zmm12,%zmm5 - vpclmulqdq $0x01,%zmm6,%zmm0,%zmm11 - vpclmulqdq $0x01,%zmm7,%zmm1,%zmm12 - vpclmulqdq $0x01,%zmm8,%zmm2,%zmm13 - vpternlogd $0x96,%zmm13,%zmm12,%zmm11 - vpclmulqdq $0x01,%zmm9,%zmm3,%zmm12 - vpclmulqdq $0x10,%zmm6,%zmm0,%zmm13 - vpternlogd $0x96,%zmm13,%zmm12,%zmm11 - vpclmulqdq $0x10,%zmm7,%zmm1,%zmm12 - vpclmulqdq $0x10,%zmm8,%zmm2,%zmm13 - vpternlogd $0x96,%zmm13,%zmm12,%zmm11 - vpclmulqdq $0x01,%zmm5,%zmm10,%zmm13 - vpclmulqdq $0x10,%zmm9,%zmm3,%zmm12 - vpxord %zmm12,%zmm11,%zmm11 - vpshufd $0x4e,%zmm5,%zmm5 - vpclmulqdq $0x11,%zmm6,%zmm0,%zmm0 - vpclmulqdq $0x11,%zmm7,%zmm1,%zmm1 - vpclmulqdq $0x11,%zmm8,%zmm2,%zmm2 - vpternlogd $0x96,%zmm13,%zmm5,%zmm11 - vpclmulqdq $0x11,%zmm9,%zmm3,%zmm3 - vpternlogd $0x96,%zmm2,%zmm1,%zmm0 - vpclmulqdq $0x01,%zmm11,%zmm10,%zmm12 - vpxord %zmm3,%zmm0,%zmm5 - vpshufd $0x4e,%zmm11,%zmm11 - vpternlogd $0x96,%zmm12,%zmm11,%zmm5 + vpclmulqdq $0x00,%zmm6,%zmm0,%zmm5 // LO_0 + vpclmulqdq $0x00,%zmm7,%zmm1,%zmm11 // LO_1 + vpclmulqdq $0x00,%zmm8,%zmm2,%zmm12 // LO_2 + vpxord %zmm11,%zmm5,%zmm5 // sum(LO_{1,0}) + vpclmulqdq $0x00,%zmm9,%zmm3,%zmm13 // LO_3 + vpternlogd $0x96,%zmm13,%zmm12,%zmm5 // LO = sum(LO_{3,2,1,0}) + vpclmulqdq $0x01,%zmm6,%zmm0,%zmm11 // MI_0 + vpclmulqdq $0x01,%zmm7,%zmm1,%zmm12 // MI_1 + vpclmulqdq $0x01,%zmm8,%zmm2,%zmm13 // MI_2 + vpternlogd $0x96,%zmm13,%zmm12,%zmm11 // sum(MI_{2,1,0}) + vpclmulqdq $0x01,%zmm9,%zmm3,%zmm12 // MI_3 + vpclmulqdq $0x10,%zmm6,%zmm0,%zmm13 // MI_4 + vpternlogd $0x96,%zmm13,%zmm12,%zmm11 // sum(MI_{4,3,2,1,0}) + vpclmulqdq $0x10,%zmm7,%zmm1,%zmm12 // MI_5 + vpclmulqdq $0x10,%zmm8,%zmm2,%zmm13 // MI_6 + vpternlogd $0x96,%zmm13,%zmm12,%zmm11 // sum(MI_{6,5,4,3,2,1,0}) + vpclmulqdq $0x01,%zmm5,%zmm10,%zmm13 // LO_L*(x^63 + x^62 + x^57) + vpclmulqdq $0x10,%zmm9,%zmm3,%zmm12 // MI_7 + vpxord %zmm12,%zmm11,%zmm11 // MI = sum(MI_{7,6,5,4,3,2,1,0}) + vpshufd $0x4e,%zmm5,%zmm5 // Swap halves of LO + vpclmulqdq $0x11,%zmm6,%zmm0,%zmm0 // HI_0 + vpclmulqdq $0x11,%zmm7,%zmm1,%zmm1 // HI_1 + vpclmulqdq $0x11,%zmm8,%zmm2,%zmm2 // HI_2 + vpternlogd $0x96,%zmm13,%zmm5,%zmm11 // Fold LO into MI + vpclmulqdq $0x11,%zmm9,%zmm3,%zmm3 // HI_3 + vpternlogd $0x96,%zmm2,%zmm1,%zmm0 // sum(HI_{2,1,0}) + vpclmulqdq $0x01,%zmm11,%zmm10,%zmm12 // MI_L*(x^63 + x^62 + x^57) + vpxord %zmm3,%zmm0,%zmm5 // HI = sum(HI_{3,2,1,0}) + vpshufd $0x4e,%zmm11,%zmm11 // Swap halves of MI + vpternlogd $0x96,%zmm12,%zmm11,%zmm5 // Fold MI into HI vextracti32x4 $1,%zmm5,%xmm0 vextracti32x4 $2,%zmm5,%xmm1 vextracti32x4 $3,%zmm5,%xmm2 @@ -274,24 +278,24 @@ L$aad_loop_4x: cmpq $256,%rcx jae L$aad_loop_4x - +// Update GHASH with 64 bytes of AAD at a time. cmpq $64,%rcx jb L$aad_large_done L$aad_loop_1x: vmovdqu8 (%rdx),%zmm0 vpshufb %zmm4,%zmm0,%zmm0 vpxord %zmm0,%zmm5,%zmm5 - vpclmulqdq $0x00,%zmm9,%zmm5,%zmm0 - vpclmulqdq $0x01,%zmm9,%zmm5,%zmm1 - vpclmulqdq $0x10,%zmm9,%zmm5,%zmm2 - vpxord %zmm2,%zmm1,%zmm1 - vpclmulqdq $0x01,%zmm0,%zmm10,%zmm2 - vpshufd $0x4e,%zmm0,%zmm0 - vpternlogd $0x96,%zmm2,%zmm0,%zmm1 - vpclmulqdq $0x11,%zmm9,%zmm5,%zmm5 - vpclmulqdq $0x01,%zmm1,%zmm10,%zmm0 - vpshufd $0x4e,%zmm1,%zmm1 - vpternlogd $0x96,%zmm0,%zmm1,%zmm5 + vpclmulqdq $0x00,%zmm9,%zmm5,%zmm0 // LO = a_L * b_L + vpclmulqdq $0x01,%zmm9,%zmm5,%zmm1 // MI_0 = a_L * b_H + vpclmulqdq $0x10,%zmm9,%zmm5,%zmm2 // MI_1 = a_H * b_L + vpxord %zmm2,%zmm1,%zmm1 // MI = MI_0 + MI_1 + vpclmulqdq $0x01,%zmm0,%zmm10,%zmm2 // LO_L*(x^63 + x^62 + x^57) + vpshufd $0x4e,%zmm0,%zmm0 // Swap halves of LO + vpternlogd $0x96,%zmm2,%zmm0,%zmm1 // Fold LO into MI + vpclmulqdq $0x11,%zmm9,%zmm5,%zmm5 // HI = a_H * b_H + vpclmulqdq $0x01,%zmm1,%zmm10,%zmm0 // MI_L*(x^63 + x^62 + x^57) + vpshufd $0x4e,%zmm1,%zmm1 // Swap halves of MI + vpternlogd $0x96,%zmm0,%zmm1,%zmm5 // Fold MI into HI vextracti32x4 $1,%zmm5,%xmm0 vextracti32x4 $2,%zmm5,%xmm1 @@ -306,7 +310,7 @@ L$aad_loop_1x: L$aad_large_done: - +// GHASH the remaining data 16 bytes at a time, using xmm registers only. L$aad_blockbyblock: testq %rcx,%rcx jz L$aad_done @@ -315,28 +319,28 @@ L$aad_loop_blockbyblock: vmovdqu (%rdx),%xmm0 vpshufb %xmm4,%xmm0,%xmm0 vpxor %xmm0,%xmm5,%xmm5 - vpclmulqdq $0x00,%xmm9,%xmm5,%xmm0 - vpclmulqdq $0x01,%xmm9,%xmm5,%xmm1 - vpclmulqdq $0x10,%xmm9,%xmm5,%xmm2 - vpxord %xmm2,%xmm1,%xmm1 - vpclmulqdq $0x01,%xmm0,%xmm10,%xmm2 - vpshufd $0x4e,%xmm0,%xmm0 - vpternlogd $0x96,%xmm2,%xmm0,%xmm1 - vpclmulqdq $0x11,%xmm9,%xmm5,%xmm5 - vpclmulqdq $0x01,%xmm1,%xmm10,%xmm0 - vpshufd $0x4e,%xmm1,%xmm1 - vpternlogd $0x96,%xmm0,%xmm1,%xmm5 + vpclmulqdq $0x00,%xmm9,%xmm5,%xmm0 // LO = a_L * b_L + vpclmulqdq $0x01,%xmm9,%xmm5,%xmm1 // MI_0 = a_L * b_H + vpclmulqdq $0x10,%xmm9,%xmm5,%xmm2 // MI_1 = a_H * b_L + vpxord %xmm2,%xmm1,%xmm1 // MI = MI_0 + MI_1 + vpclmulqdq $0x01,%xmm0,%xmm10,%xmm2 // LO_L*(x^63 + x^62 + x^57) + vpshufd $0x4e,%xmm0,%xmm0 // Swap halves of LO + vpternlogd $0x96,%xmm2,%xmm0,%xmm1 // Fold LO into MI + vpclmulqdq $0x11,%xmm9,%xmm5,%xmm5 // HI = a_H * b_H + vpclmulqdq $0x01,%xmm1,%xmm10,%xmm0 // MI_L*(x^63 + x^62 + x^57) + vpshufd $0x4e,%xmm1,%xmm1 // Swap halves of MI + vpternlogd $0x96,%xmm0,%xmm1,%xmm5 // Fold MI into HI addq $16,%rdx subq $16,%rcx jnz L$aad_loop_blockbyblock L$aad_done: - +// Store the updated GHASH accumulator back to memory. vpshufb %xmm4,%xmm5,%xmm5 vmovdqu %xmm5,(%rdi) - vzeroupper + vzeroupper // This is needed after using ymm or zmm registers. ret @@ -345,6 +349,7 @@ L$aad_done: .private_extern _aes_gcm_enc_update_vaes_avx512 .p2align 5 +.alt_entry _aes_gcm_enc_update_vaes_avx512 _aes_gcm_enc_update_vaes_avx512: @@ -352,54 +357,54 @@ _CET_ENDBR pushq %r12 - movq 16(%rsp),%r12 + movq 16(%rsp),%r12 // arg7 #ifdef BORINGSSL_DISPATCH_TEST movb $1,_BORINGSSL_function_hit+7(%rip) #endif - +// Load some constants. vbroadcasti32x4 L$bswap_mask(%rip),%zmm8 vbroadcasti32x4 L$gfpoly(%rip),%zmm31 - - +// Load the GHASH accumulator and the starting counter. +// BoringSSL passes these values in big endian format. vmovdqu (%r12),%xmm10 vpshufb %xmm8,%xmm10,%xmm10 vbroadcasti32x4 (%r8),%zmm12 vpshufb %zmm8,%zmm12,%zmm12 - - +// Load the AES key length in bytes. BoringSSL stores number of rounds +// minus 1, so convert using: AESKEYLEN = 4 * aeskey->rounds - 20. movl 240(%rcx),%r10d leal -20(,%r10,4),%r10d - - - +// Make RNDKEYLAST_PTR point to the last AES round key. This is the +// round key with index 10, 12, or 14 for AES-128, AES-192, or AES-256 +// respectively. Then load the zero-th and last round keys. leaq 96(%rcx,%r10,4),%r11 vbroadcasti32x4 (%rcx),%zmm13 vbroadcasti32x4 (%r11),%zmm14 - +// Finish initializing LE_CTR by adding [0, 1, 2, 3] to its low words. vpaddd L$ctr_pattern(%rip),%zmm12,%zmm12 - +// Load 4 into all 128-bit lanes of LE_CTR_INC. vbroadcasti32x4 L$inc_4blocks(%rip),%zmm11 - - +// If there are at least 256 bytes of data, then continue into the loop +// that processes 256 bytes of data at a time. Otherwise skip it. cmpq $256,%rdx jb L$crypt_loop_4x_done__func1 - +// Load powers of the hash key. vmovdqu8 256-256(%r9),%zmm27 vmovdqu8 256-192(%r9),%zmm28 vmovdqu8 256-128(%r9),%zmm29 vmovdqu8 256-64(%r9),%zmm30 - - - - +// Encrypt the first 4 vectors of plaintext blocks. Leave the resulting +// ciphertext in GHASHDATA[0-3] for GHASH. +// Increment le_ctr four times to generate four vectors of little-endian +// counter blocks, swap each to big-endian, and store them in aesdata[0-3]. vpshufb %zmm8,%zmm12,%zmm0 vpaddd %zmm11,%zmm12,%zmm12 vpshufb %zmm8,%zmm12,%zmm1 @@ -409,7 +414,7 @@ _CET_ENDBR vpshufb %zmm8,%zmm12,%zmm3 vpaddd %zmm11,%zmm12,%zmm12 - +// AES "round zero": XOR in the zero-th round key. vpxord %zmm13,%zmm0,%zmm0 vpxord %zmm13,%zmm1,%zmm1 vpxord %zmm13,%zmm2,%zmm2 @@ -444,7 +449,7 @@ L$vaesenc_loop_first_4_vecs__func1: subq $256,%rdx cmpq $256,%rdx jb L$ghash_last_ciphertext_4x__func1 - +// Cache as many additional AES round keys as possible. vbroadcasti32x4 -144(%r11),%zmm15 vbroadcasti32x4 -128(%r11),%zmm16 vbroadcasti32x4 -112(%r11),%zmm17 @@ -456,9 +461,9 @@ L$vaesenc_loop_first_4_vecs__func1: vbroadcasti32x4 -16(%r11),%zmm23 L$crypt_loop_4x__func1: - - - +// Start the AES encryption of the counter blocks. +// Increment le_ctr four times to generate four vectors of little-endian +// counter blocks, swap each to big-endian, and store them in aesdata[0-3]. vpshufb %zmm8,%zmm12,%zmm0 vpaddd %zmm11,%zmm12,%zmm12 vpshufb %zmm8,%zmm12,%zmm1 @@ -468,7 +473,7 @@ L$crypt_loop_4x__func1: vpshufb %zmm8,%zmm12,%zmm3 vpaddd %zmm11,%zmm12,%zmm12 - +// AES "round zero": XOR in the zero-th round key. vpxord %zmm13,%zmm0,%zmm0 vpxord %zmm13,%zmm1,%zmm1 vpxord %zmm13,%zmm2,%zmm2 @@ -477,7 +482,7 @@ L$crypt_loop_4x__func1: cmpl $24,%r10d jl L$aes128__func1 je L$aes192__func1 - +// AES-256 vbroadcasti32x4 -208(%r11),%zmm9 vaesenc %zmm9,%zmm0,%zmm0 vaesenc %zmm9,%zmm1,%zmm1 @@ -505,17 +510,17 @@ L$aes192__func1: L$aes128__func1: - - - +// Prefetch the source data 512 bytes ahead into the L1 data cache, to +// improve performance when the hardware prefetcher is disabled. Assumes the +// L1 data cache line size is 64 bytes (de facto standard on x86_64). prefetcht0 512+0(%rdi) prefetcht0 512+64(%rdi) prefetcht0 512+128(%rdi) prefetcht0 512+192(%rdi) - - - +// Finish the AES encryption of the counter blocks in AESDATA[0-3], +// interleaved with the GHASH update of the ciphertext blocks in +// GHASHDATA[0-3]. vpshufb %zmm8,%zmm4,%zmm4 vpxord %zmm10,%zmm4,%zmm4 vpshufb %zmm8,%zmm5,%zmm5 @@ -527,78 +532,78 @@ L$aes128__func1: vaesenc %zmm15,%zmm3,%zmm3 vpshufb %zmm8,%zmm7,%zmm7 - vpclmulqdq $0x00,%zmm27,%zmm4,%zmm10 - vpclmulqdq $0x00,%zmm28,%zmm5,%zmm24 - vpclmulqdq $0x00,%zmm29,%zmm6,%zmm25 + vpclmulqdq $0x00,%zmm27,%zmm4,%zmm10 // LO_0 + vpclmulqdq $0x00,%zmm28,%zmm5,%zmm24 // LO_1 + vpclmulqdq $0x00,%zmm29,%zmm6,%zmm25 // LO_2 vaesenc %zmm16,%zmm0,%zmm0 vaesenc %zmm16,%zmm1,%zmm1 vaesenc %zmm16,%zmm2,%zmm2 vaesenc %zmm16,%zmm3,%zmm3 - vpxord %zmm24,%zmm10,%zmm10 - vpclmulqdq $0x00,%zmm30,%zmm7,%zmm26 - vpternlogd $0x96,%zmm26,%zmm25,%zmm10 - vpclmulqdq $0x01,%zmm27,%zmm4,%zmm24 + vpxord %zmm24,%zmm10,%zmm10 // sum(LO_{1,0}) + vpclmulqdq $0x00,%zmm30,%zmm7,%zmm26 // LO_3 + vpternlogd $0x96,%zmm26,%zmm25,%zmm10 // LO = sum(LO_{3,2,1,0}) + vpclmulqdq $0x01,%zmm27,%zmm4,%zmm24 // MI_0 vaesenc %zmm17,%zmm0,%zmm0 vaesenc %zmm17,%zmm1,%zmm1 vaesenc %zmm17,%zmm2,%zmm2 vaesenc %zmm17,%zmm3,%zmm3 - vpclmulqdq $0x01,%zmm28,%zmm5,%zmm25 - vpclmulqdq $0x01,%zmm29,%zmm6,%zmm26 - vpternlogd $0x96,%zmm26,%zmm25,%zmm24 - vpclmulqdq $0x01,%zmm30,%zmm7,%zmm25 + vpclmulqdq $0x01,%zmm28,%zmm5,%zmm25 // MI_1 + vpclmulqdq $0x01,%zmm29,%zmm6,%zmm26 // MI_2 + vpternlogd $0x96,%zmm26,%zmm25,%zmm24 // sum(MI_{2,1,0}) + vpclmulqdq $0x01,%zmm30,%zmm7,%zmm25 // MI_3 vaesenc %zmm18,%zmm0,%zmm0 vaesenc %zmm18,%zmm1,%zmm1 vaesenc %zmm18,%zmm2,%zmm2 vaesenc %zmm18,%zmm3,%zmm3 - vpclmulqdq $0x10,%zmm27,%zmm4,%zmm26 - vpternlogd $0x96,%zmm26,%zmm25,%zmm24 - vpclmulqdq $0x10,%zmm28,%zmm5,%zmm25 - vpclmulqdq $0x10,%zmm29,%zmm6,%zmm26 + vpclmulqdq $0x10,%zmm27,%zmm4,%zmm26 // MI_4 + vpternlogd $0x96,%zmm26,%zmm25,%zmm24 // sum(MI_{4,3,2,1,0}) + vpclmulqdq $0x10,%zmm28,%zmm5,%zmm25 // MI_5 + vpclmulqdq $0x10,%zmm29,%zmm6,%zmm26 // MI_6 vaesenc %zmm19,%zmm0,%zmm0 vaesenc %zmm19,%zmm1,%zmm1 vaesenc %zmm19,%zmm2,%zmm2 vaesenc %zmm19,%zmm3,%zmm3 - vpternlogd $0x96,%zmm26,%zmm25,%zmm24 - vpclmulqdq $0x01,%zmm10,%zmm31,%zmm26 - vpclmulqdq $0x10,%zmm30,%zmm7,%zmm25 - vpxord %zmm25,%zmm24,%zmm24 + vpternlogd $0x96,%zmm26,%zmm25,%zmm24 // sum(MI_{6,5,4,3,2,1,0}) + vpclmulqdq $0x01,%zmm10,%zmm31,%zmm26 // LO_L*(x^63 + x^62 + x^57) + vpclmulqdq $0x10,%zmm30,%zmm7,%zmm25 // MI_7 + vpxord %zmm25,%zmm24,%zmm24 // MI = sum(MI_{7,6,5,4,3,2,1,0}) vaesenc %zmm20,%zmm0,%zmm0 vaesenc %zmm20,%zmm1,%zmm1 vaesenc %zmm20,%zmm2,%zmm2 vaesenc %zmm20,%zmm3,%zmm3 - vpshufd $0x4e,%zmm10,%zmm10 - vpclmulqdq $0x11,%zmm27,%zmm4,%zmm4 - vpclmulqdq $0x11,%zmm28,%zmm5,%zmm5 - vpclmulqdq $0x11,%zmm29,%zmm6,%zmm6 + vpshufd $0x4e,%zmm10,%zmm10 // Swap halves of LO + vpclmulqdq $0x11,%zmm27,%zmm4,%zmm4 // HI_0 + vpclmulqdq $0x11,%zmm28,%zmm5,%zmm5 // HI_1 + vpclmulqdq $0x11,%zmm29,%zmm6,%zmm6 // HI_2 vaesenc %zmm21,%zmm0,%zmm0 vaesenc %zmm21,%zmm1,%zmm1 vaesenc %zmm21,%zmm2,%zmm2 vaesenc %zmm21,%zmm3,%zmm3 - vpternlogd $0x96,%zmm26,%zmm10,%zmm24 - vpclmulqdq $0x11,%zmm30,%zmm7,%zmm7 - vpternlogd $0x96,%zmm6,%zmm5,%zmm4 - vpclmulqdq $0x01,%zmm24,%zmm31,%zmm25 + vpternlogd $0x96,%zmm26,%zmm10,%zmm24 // Fold LO into MI + vpclmulqdq $0x11,%zmm30,%zmm7,%zmm7 // HI_3 + vpternlogd $0x96,%zmm6,%zmm5,%zmm4 // sum(HI_{2,1,0}) + vpclmulqdq $0x01,%zmm24,%zmm31,%zmm25 // MI_L*(x^63 + x^62 + x^57) vaesenc %zmm22,%zmm0,%zmm0 vaesenc %zmm22,%zmm1,%zmm1 vaesenc %zmm22,%zmm2,%zmm2 vaesenc %zmm22,%zmm3,%zmm3 - vpxord %zmm7,%zmm4,%zmm10 - vpshufd $0x4e,%zmm24,%zmm24 - vpternlogd $0x96,%zmm25,%zmm24,%zmm10 + vpxord %zmm7,%zmm4,%zmm10 // HI = sum(HI_{3,2,1,0}) + vpshufd $0x4e,%zmm24,%zmm24 // Swap halves of MI + vpternlogd $0x96,%zmm25,%zmm24,%zmm10 // Fold MI into HI vaesenc %zmm23,%zmm0,%zmm0 vaesenc %zmm23,%zmm1,%zmm1 @@ -636,36 +641,36 @@ L$ghash_last_ciphertext_4x__func1: vpshufb %zmm8,%zmm5,%zmm5 vpshufb %zmm8,%zmm6,%zmm6 vpshufb %zmm8,%zmm7,%zmm7 - vpclmulqdq $0x00,%zmm27,%zmm4,%zmm10 - vpclmulqdq $0x00,%zmm28,%zmm5,%zmm24 - vpclmulqdq $0x00,%zmm29,%zmm6,%zmm25 - vpxord %zmm24,%zmm10,%zmm10 - vpclmulqdq $0x00,%zmm30,%zmm7,%zmm26 - vpternlogd $0x96,%zmm26,%zmm25,%zmm10 - vpclmulqdq $0x01,%zmm27,%zmm4,%zmm24 - vpclmulqdq $0x01,%zmm28,%zmm5,%zmm25 - vpclmulqdq $0x01,%zmm29,%zmm6,%zmm26 - vpternlogd $0x96,%zmm26,%zmm25,%zmm24 - vpclmulqdq $0x01,%zmm30,%zmm7,%zmm25 - vpclmulqdq $0x10,%zmm27,%zmm4,%zmm26 - vpternlogd $0x96,%zmm26,%zmm25,%zmm24 - vpclmulqdq $0x10,%zmm28,%zmm5,%zmm25 - vpclmulqdq $0x10,%zmm29,%zmm6,%zmm26 - vpternlogd $0x96,%zmm26,%zmm25,%zmm24 - vpclmulqdq $0x01,%zmm10,%zmm31,%zmm26 - vpclmulqdq $0x10,%zmm30,%zmm7,%zmm25 - vpxord %zmm25,%zmm24,%zmm24 - vpshufd $0x4e,%zmm10,%zmm10 - vpclmulqdq $0x11,%zmm27,%zmm4,%zmm4 - vpclmulqdq $0x11,%zmm28,%zmm5,%zmm5 - vpclmulqdq $0x11,%zmm29,%zmm6,%zmm6 - vpternlogd $0x96,%zmm26,%zmm10,%zmm24 - vpclmulqdq $0x11,%zmm30,%zmm7,%zmm7 - vpternlogd $0x96,%zmm6,%zmm5,%zmm4 - vpclmulqdq $0x01,%zmm24,%zmm31,%zmm25 - vpxord %zmm7,%zmm4,%zmm10 - vpshufd $0x4e,%zmm24,%zmm24 - vpternlogd $0x96,%zmm25,%zmm24,%zmm10 + vpclmulqdq $0x00,%zmm27,%zmm4,%zmm10 // LO_0 + vpclmulqdq $0x00,%zmm28,%zmm5,%zmm24 // LO_1 + vpclmulqdq $0x00,%zmm29,%zmm6,%zmm25 // LO_2 + vpxord %zmm24,%zmm10,%zmm10 // sum(LO_{1,0}) + vpclmulqdq $0x00,%zmm30,%zmm7,%zmm26 // LO_3 + vpternlogd $0x96,%zmm26,%zmm25,%zmm10 // LO = sum(LO_{3,2,1,0}) + vpclmulqdq $0x01,%zmm27,%zmm4,%zmm24 // MI_0 + vpclmulqdq $0x01,%zmm28,%zmm5,%zmm25 // MI_1 + vpclmulqdq $0x01,%zmm29,%zmm6,%zmm26 // MI_2 + vpternlogd $0x96,%zmm26,%zmm25,%zmm24 // sum(MI_{2,1,0}) + vpclmulqdq $0x01,%zmm30,%zmm7,%zmm25 // MI_3 + vpclmulqdq $0x10,%zmm27,%zmm4,%zmm26 // MI_4 + vpternlogd $0x96,%zmm26,%zmm25,%zmm24 // sum(MI_{4,3,2,1,0}) + vpclmulqdq $0x10,%zmm28,%zmm5,%zmm25 // MI_5 + vpclmulqdq $0x10,%zmm29,%zmm6,%zmm26 // MI_6 + vpternlogd $0x96,%zmm26,%zmm25,%zmm24 // sum(MI_{6,5,4,3,2,1,0}) + vpclmulqdq $0x01,%zmm10,%zmm31,%zmm26 // LO_L*(x^63 + x^62 + x^57) + vpclmulqdq $0x10,%zmm30,%zmm7,%zmm25 // MI_7 + vpxord %zmm25,%zmm24,%zmm24 // MI = sum(MI_{7,6,5,4,3,2,1,0}) + vpshufd $0x4e,%zmm10,%zmm10 // Swap halves of LO + vpclmulqdq $0x11,%zmm27,%zmm4,%zmm4 // HI_0 + vpclmulqdq $0x11,%zmm28,%zmm5,%zmm5 // HI_1 + vpclmulqdq $0x11,%zmm29,%zmm6,%zmm6 // HI_2 + vpternlogd $0x96,%zmm26,%zmm10,%zmm24 // Fold LO into MI + vpclmulqdq $0x11,%zmm30,%zmm7,%zmm7 // HI_3 + vpternlogd $0x96,%zmm6,%zmm5,%zmm4 // sum(HI_{2,1,0}) + vpclmulqdq $0x01,%zmm24,%zmm31,%zmm25 // MI_L*(x^63 + x^62 + x^57) + vpxord %zmm7,%zmm4,%zmm10 // HI = sum(HI_{3,2,1,0}) + vpshufd $0x4e,%zmm24,%zmm24 // Swap halves of MI + vpternlogd $0x96,%zmm25,%zmm24,%zmm10 // Fold MI into HI vextracti32x4 $1,%zmm10,%xmm4 vextracti32x4 $2,%zmm10,%xmm5 vextracti32x4 $3,%zmm10,%xmm6 @@ -673,32 +678,32 @@ L$ghash_last_ciphertext_4x__func1: vpternlogd $0x96,%xmm5,%xmm6,%xmm10 L$crypt_loop_4x_done__func1: - +// Check whether any data remains. testq %rdx,%rdx jz L$done__func1 - - - - - - - - - - - - - - - - - - - +// The data length isn't a multiple of 256 bytes. Process the remaining +// data of length 1 <= DATALEN < 256, up to one 64-byte vector at a time. +// Going one vector at a time may seem inefficient compared to having +// separate code paths for each possible number of vectors remaining. +// However, using a loop keeps the code size down, and it performs +// surprising well; modern CPUs will start executing the next iteration +// before the previous one finishes and also predict the number of loop +// iterations. For a similar reason, we roll up the AES rounds. +// +// On the last iteration, the remaining length may be less than 64 bytes. +// Handle this using masking. +// +// Since there are enough key powers available for all remaining data, +// there is no need to do a GHASH reduction after each iteration. +// Instead, multiply each remaining block by its own key power, and only +// do a GHASH reduction at the very end. + +// Make POWERS_PTR point to the key powers [H^N, H^(N-1), ...] where N +// is the number of blocks that remain. movq %rdx,%rax negq %rax - andq $-16,%rax + andq $-16,%rax // -round_up(DATALEN, 16) leaq 256(%r9,%rax,1),%r8 vpxor %xmm4,%xmm4,%xmm4 vpxor %xmm5,%xmm5,%xmm5 @@ -708,9 +713,9 @@ L$crypt_loop_4x_done__func1: jb L$partial_vec__func1 L$crypt_loop_1x__func1: +// Process a full 64-byte vector. - - +// Encrypt a vector of counter blocks. vpshufb %zmm8,%zmm12,%zmm0 vpaddd %zmm11,%zmm12,%zmm12 vpxord %zmm13,%zmm0,%zmm0 @@ -723,19 +728,19 @@ L$vaesenc_loop_tail_full_vec__func1: jne L$vaesenc_loop_tail_full_vec__func1 vaesenclast %zmm14,%zmm0,%zmm0 - +// XOR the data with the vector of keystream blocks. vmovdqu8 (%rdi),%zmm1 vpxord %zmm1,%zmm0,%zmm0 vmovdqu8 %zmm0,(%rsi) - +// Update GHASH with the ciphertext blocks, without reducing. vmovdqu8 (%r8),%zmm30 vpshufb %zmm8,%zmm0,%zmm0 vpxord %zmm10,%zmm0,%zmm0 - vpclmulqdq $0x00,%zmm30,%zmm0,%zmm7 - vpclmulqdq $0x01,%zmm30,%zmm0,%zmm1 - vpclmulqdq $0x10,%zmm30,%zmm0,%zmm2 - vpclmulqdq $0x11,%zmm30,%zmm0,%zmm3 + vpclmulqdq $0x00,%zmm30,%zmm0,%zmm7 // a_L * b_L + vpclmulqdq $0x01,%zmm30,%zmm0,%zmm1 // a_L * b_H + vpclmulqdq $0x10,%zmm30,%zmm0,%zmm2 // a_H * b_L + vpclmulqdq $0x11,%zmm30,%zmm0,%zmm3 // a_H * b_H vpxord %zmm7,%zmm4,%zmm4 vpternlogd $0x96,%zmm2,%zmm1,%zmm5 vpxord %zmm3,%zmm6,%zmm6 @@ -753,10 +758,10 @@ L$vaesenc_loop_tail_full_vec__func1: jz L$reduce__func1 L$partial_vec__func1: +// Process a partial vector of length 1 <= DATALEN < 64. - - - +// Set the data mask %k1 to DATALEN 1's. +// Set the key powers mask %k2 to round_up(DATALEN, 16) 1's. movq $-1,%rax bzhiq %rdx,%rax,%rax kmovq %rax,%k1 @@ -766,8 +771,8 @@ L$partial_vec__func1: bzhiq %rdx,%rax,%rax kmovq %rax,%k2 - - +// Encrypt one last vector of counter blocks. This does not need to be +// masked. The counter does not need to be incremented here. vpshufb %zmm8,%zmm12,%zmm0 vpxord %zmm13,%zmm0,%zmm0 leaq 16(%rcx),%rax @@ -779,38 +784,38 @@ L$vaesenc_loop_tail_partialvec__func1: jne L$vaesenc_loop_tail_partialvec__func1 vaesenclast %zmm14,%zmm0,%zmm0 - +// XOR the data with the appropriate number of keystream bytes. vmovdqu8 (%rdi),%zmm1{%k1}{z} vpxord %zmm1,%zmm0,%zmm0 vmovdqu8 %zmm0,(%rsi){%k1} - - - - - - - - - - - - +// Update GHASH with the ciphertext block(s), without reducing. +// +// In the case of DATALEN < 64, the ciphertext is zero-padded to 64 +// bytes. (If decrypting, it's done by the above masked load. If +// encrypting, it's done by the below masked register-to-register move.) +// Note that if DATALEN <= 48, there will be additional padding beyond +// the padding of the last block specified by GHASH itself; i.e., there +// may be whole block(s) that get processed by the GHASH multiplication +// and reduction instructions but should not actually be included in the +// GHASH. However, any such blocks are all-zeroes, and the values that +// they're multiplied with are also all-zeroes. Therefore they just add +// 0 * 0 = 0 to the final GHASH result, which makes no difference. vmovdqu8 (%r8),%zmm30{%k2}{z} vmovdqu8 %zmm0,%zmm1{%k1}{z} vpshufb %zmm8,%zmm1,%zmm0 vpxord %zmm10,%zmm0,%zmm0 - vpclmulqdq $0x00,%zmm30,%zmm0,%zmm7 - vpclmulqdq $0x01,%zmm30,%zmm0,%zmm1 - vpclmulqdq $0x10,%zmm30,%zmm0,%zmm2 - vpclmulqdq $0x11,%zmm30,%zmm0,%zmm3 + vpclmulqdq $0x00,%zmm30,%zmm0,%zmm7 // a_L * b_L + vpclmulqdq $0x01,%zmm30,%zmm0,%zmm1 // a_L * b_H + vpclmulqdq $0x10,%zmm30,%zmm0,%zmm2 // a_H * b_L + vpclmulqdq $0x11,%zmm30,%zmm0,%zmm3 // a_H * b_H vpxord %zmm7,%zmm4,%zmm4 vpternlogd $0x96,%zmm2,%zmm1,%zmm5 vpxord %zmm3,%zmm6,%zmm6 L$reduce__func1: - +// Finally, do the GHASH reduction. vpclmulqdq $0x01,%zmm4,%zmm31,%zmm0 vpshufd $0x4e,%zmm4,%zmm4 vpternlogd $0x96,%zmm0,%zmm4,%zmm5 @@ -826,11 +831,11 @@ L$reduce__func1: L$done__func1: - +// Store the updated GHASH accumulator back to memory. vpshufb %xmm8,%xmm10,%xmm10 vmovdqu %xmm10,(%r12) - vzeroupper + vzeroupper // This is needed after using ymm or zmm registers. popq %r12 ret @@ -841,6 +846,7 @@ L$done__func1: .private_extern _aes_gcm_dec_update_vaes_avx512 .p2align 5 +.alt_entry _aes_gcm_dec_update_vaes_avx512 _aes_gcm_dec_update_vaes_avx512: @@ -848,47 +854,47 @@ _CET_ENDBR pushq %r12 - movq 16(%rsp),%r12 - + movq 16(%rsp),%r12 // arg7 +// Load some constants. vbroadcasti32x4 L$bswap_mask(%rip),%zmm8 vbroadcasti32x4 L$gfpoly(%rip),%zmm31 - - +// Load the GHASH accumulator and the starting counter. +// BoringSSL passes these values in big endian format. vmovdqu (%r12),%xmm10 vpshufb %xmm8,%xmm10,%xmm10 vbroadcasti32x4 (%r8),%zmm12 vpshufb %zmm8,%zmm12,%zmm12 - - +// Load the AES key length in bytes. BoringSSL stores number of rounds +// minus 1, so convert using: AESKEYLEN = 4 * aeskey->rounds - 20. movl 240(%rcx),%r10d leal -20(,%r10,4),%r10d - - - +// Make RNDKEYLAST_PTR point to the last AES round key. This is the +// round key with index 10, 12, or 14 for AES-128, AES-192, or AES-256 +// respectively. Then load the zero-th and last round keys. leaq 96(%rcx,%r10,4),%r11 vbroadcasti32x4 (%rcx),%zmm13 vbroadcasti32x4 (%r11),%zmm14 - +// Finish initializing LE_CTR by adding [0, 1, 2, 3] to its low words. vpaddd L$ctr_pattern(%rip),%zmm12,%zmm12 - +// Load 4 into all 128-bit lanes of LE_CTR_INC. vbroadcasti32x4 L$inc_4blocks(%rip),%zmm11 - - +// If there are at least 256 bytes of data, then continue into the loop +// that processes 256 bytes of data at a time. Otherwise skip it. cmpq $256,%rdx jb L$crypt_loop_4x_done__func2 - +// Load powers of the hash key. vmovdqu8 256-256(%r9),%zmm27 vmovdqu8 256-192(%r9),%zmm28 vmovdqu8 256-128(%r9),%zmm29 vmovdqu8 256-64(%r9),%zmm30 - +// Cache as many additional AES round keys as possible. vbroadcasti32x4 -144(%r11),%zmm15 vbroadcasti32x4 -128(%r11),%zmm16 vbroadcasti32x4 -112(%r11),%zmm17 @@ -904,9 +910,9 @@ L$crypt_loop_4x__func2: vmovdqu8 64(%rdi),%zmm5 vmovdqu8 128(%rdi),%zmm6 vmovdqu8 192(%rdi),%zmm7 - - - +// Start the AES encryption of the counter blocks. +// Increment le_ctr four times to generate four vectors of little-endian +// counter blocks, swap each to big-endian, and store them in aesdata[0-3]. vpshufb %zmm8,%zmm12,%zmm0 vpaddd %zmm11,%zmm12,%zmm12 vpshufb %zmm8,%zmm12,%zmm1 @@ -916,7 +922,7 @@ L$crypt_loop_4x__func2: vpshufb %zmm8,%zmm12,%zmm3 vpaddd %zmm11,%zmm12,%zmm12 - +// AES "round zero": XOR in the zero-th round key. vpxord %zmm13,%zmm0,%zmm0 vpxord %zmm13,%zmm1,%zmm1 vpxord %zmm13,%zmm2,%zmm2 @@ -925,7 +931,7 @@ L$crypt_loop_4x__func2: cmpl $24,%r10d jl L$aes128__func2 je L$aes192__func2 - +// AES-256 vbroadcasti32x4 -208(%r11),%zmm9 vaesenc %zmm9,%zmm0,%zmm0 vaesenc %zmm9,%zmm1,%zmm1 @@ -953,17 +959,17 @@ L$aes192__func2: L$aes128__func2: - - - +// Prefetch the source data 512 bytes ahead into the L1 data cache, to +// improve performance when the hardware prefetcher is disabled. Assumes the +// L1 data cache line size is 64 bytes (de facto standard on x86_64). prefetcht0 512+0(%rdi) prefetcht0 512+64(%rdi) prefetcht0 512+128(%rdi) prefetcht0 512+192(%rdi) - - - +// Finish the AES encryption of the counter blocks in AESDATA[0-3], +// interleaved with the GHASH update of the ciphertext blocks in +// GHASHDATA[0-3]. vpshufb %zmm8,%zmm4,%zmm4 vpxord %zmm10,%zmm4,%zmm4 vpshufb %zmm8,%zmm5,%zmm5 @@ -975,78 +981,78 @@ L$aes128__func2: vaesenc %zmm15,%zmm3,%zmm3 vpshufb %zmm8,%zmm7,%zmm7 - vpclmulqdq $0x00,%zmm27,%zmm4,%zmm10 - vpclmulqdq $0x00,%zmm28,%zmm5,%zmm24 - vpclmulqdq $0x00,%zmm29,%zmm6,%zmm25 + vpclmulqdq $0x00,%zmm27,%zmm4,%zmm10 // LO_0 + vpclmulqdq $0x00,%zmm28,%zmm5,%zmm24 // LO_1 + vpclmulqdq $0x00,%zmm29,%zmm6,%zmm25 // LO_2 vaesenc %zmm16,%zmm0,%zmm0 vaesenc %zmm16,%zmm1,%zmm1 vaesenc %zmm16,%zmm2,%zmm2 vaesenc %zmm16,%zmm3,%zmm3 - vpxord %zmm24,%zmm10,%zmm10 - vpclmulqdq $0x00,%zmm30,%zmm7,%zmm26 - vpternlogd $0x96,%zmm26,%zmm25,%zmm10 - vpclmulqdq $0x01,%zmm27,%zmm4,%zmm24 + vpxord %zmm24,%zmm10,%zmm10 // sum(LO_{1,0}) + vpclmulqdq $0x00,%zmm30,%zmm7,%zmm26 // LO_3 + vpternlogd $0x96,%zmm26,%zmm25,%zmm10 // LO = sum(LO_{3,2,1,0}) + vpclmulqdq $0x01,%zmm27,%zmm4,%zmm24 // MI_0 vaesenc %zmm17,%zmm0,%zmm0 vaesenc %zmm17,%zmm1,%zmm1 vaesenc %zmm17,%zmm2,%zmm2 vaesenc %zmm17,%zmm3,%zmm3 - vpclmulqdq $0x01,%zmm28,%zmm5,%zmm25 - vpclmulqdq $0x01,%zmm29,%zmm6,%zmm26 - vpternlogd $0x96,%zmm26,%zmm25,%zmm24 - vpclmulqdq $0x01,%zmm30,%zmm7,%zmm25 + vpclmulqdq $0x01,%zmm28,%zmm5,%zmm25 // MI_1 + vpclmulqdq $0x01,%zmm29,%zmm6,%zmm26 // MI_2 + vpternlogd $0x96,%zmm26,%zmm25,%zmm24 // sum(MI_{2,1,0}) + vpclmulqdq $0x01,%zmm30,%zmm7,%zmm25 // MI_3 vaesenc %zmm18,%zmm0,%zmm0 vaesenc %zmm18,%zmm1,%zmm1 vaesenc %zmm18,%zmm2,%zmm2 vaesenc %zmm18,%zmm3,%zmm3 - vpclmulqdq $0x10,%zmm27,%zmm4,%zmm26 - vpternlogd $0x96,%zmm26,%zmm25,%zmm24 - vpclmulqdq $0x10,%zmm28,%zmm5,%zmm25 - vpclmulqdq $0x10,%zmm29,%zmm6,%zmm26 + vpclmulqdq $0x10,%zmm27,%zmm4,%zmm26 // MI_4 + vpternlogd $0x96,%zmm26,%zmm25,%zmm24 // sum(MI_{4,3,2,1,0}) + vpclmulqdq $0x10,%zmm28,%zmm5,%zmm25 // MI_5 + vpclmulqdq $0x10,%zmm29,%zmm6,%zmm26 // MI_6 vaesenc %zmm19,%zmm0,%zmm0 vaesenc %zmm19,%zmm1,%zmm1 vaesenc %zmm19,%zmm2,%zmm2 vaesenc %zmm19,%zmm3,%zmm3 - vpternlogd $0x96,%zmm26,%zmm25,%zmm24 - vpclmulqdq $0x01,%zmm10,%zmm31,%zmm26 - vpclmulqdq $0x10,%zmm30,%zmm7,%zmm25 - vpxord %zmm25,%zmm24,%zmm24 + vpternlogd $0x96,%zmm26,%zmm25,%zmm24 // sum(MI_{6,5,4,3,2,1,0}) + vpclmulqdq $0x01,%zmm10,%zmm31,%zmm26 // LO_L*(x^63 + x^62 + x^57) + vpclmulqdq $0x10,%zmm30,%zmm7,%zmm25 // MI_7 + vpxord %zmm25,%zmm24,%zmm24 // MI = sum(MI_{7,6,5,4,3,2,1,0}) vaesenc %zmm20,%zmm0,%zmm0 vaesenc %zmm20,%zmm1,%zmm1 vaesenc %zmm20,%zmm2,%zmm2 vaesenc %zmm20,%zmm3,%zmm3 - vpshufd $0x4e,%zmm10,%zmm10 - vpclmulqdq $0x11,%zmm27,%zmm4,%zmm4 - vpclmulqdq $0x11,%zmm28,%zmm5,%zmm5 - vpclmulqdq $0x11,%zmm29,%zmm6,%zmm6 + vpshufd $0x4e,%zmm10,%zmm10 // Swap halves of LO + vpclmulqdq $0x11,%zmm27,%zmm4,%zmm4 // HI_0 + vpclmulqdq $0x11,%zmm28,%zmm5,%zmm5 // HI_1 + vpclmulqdq $0x11,%zmm29,%zmm6,%zmm6 // HI_2 vaesenc %zmm21,%zmm0,%zmm0 vaesenc %zmm21,%zmm1,%zmm1 vaesenc %zmm21,%zmm2,%zmm2 vaesenc %zmm21,%zmm3,%zmm3 - vpternlogd $0x96,%zmm26,%zmm10,%zmm24 - vpclmulqdq $0x11,%zmm30,%zmm7,%zmm7 - vpternlogd $0x96,%zmm6,%zmm5,%zmm4 - vpclmulqdq $0x01,%zmm24,%zmm31,%zmm25 + vpternlogd $0x96,%zmm26,%zmm10,%zmm24 // Fold LO into MI + vpclmulqdq $0x11,%zmm30,%zmm7,%zmm7 // HI_3 + vpternlogd $0x96,%zmm6,%zmm5,%zmm4 // sum(HI_{2,1,0}) + vpclmulqdq $0x01,%zmm24,%zmm31,%zmm25 // MI_L*(x^63 + x^62 + x^57) vaesenc %zmm22,%zmm0,%zmm0 vaesenc %zmm22,%zmm1,%zmm1 vaesenc %zmm22,%zmm2,%zmm2 vaesenc %zmm22,%zmm3,%zmm3 - vpxord %zmm7,%zmm4,%zmm10 - vpshufd $0x4e,%zmm24,%zmm24 - vpternlogd $0x96,%zmm25,%zmm24,%zmm10 + vpxord %zmm7,%zmm4,%zmm10 // HI = sum(HI_{3,2,1,0}) + vpshufd $0x4e,%zmm24,%zmm24 // Swap halves of MI + vpternlogd $0x96,%zmm25,%zmm24,%zmm10 // Fold MI into HI vaesenc %zmm23,%zmm0,%zmm0 vaesenc %zmm23,%zmm1,%zmm1 @@ -1079,32 +1085,32 @@ L$aes128__func2: cmpq $256,%rdx jae L$crypt_loop_4x__func2 L$crypt_loop_4x_done__func2: - +// Check whether any data remains. testq %rdx,%rdx jz L$done__func2 - - - - - - - - - - - - - - - - - - - +// The data length isn't a multiple of 256 bytes. Process the remaining +// data of length 1 <= DATALEN < 256, up to one 64-byte vector at a time. +// Going one vector at a time may seem inefficient compared to having +// separate code paths for each possible number of vectors remaining. +// However, using a loop keeps the code size down, and it performs +// surprising well; modern CPUs will start executing the next iteration +// before the previous one finishes and also predict the number of loop +// iterations. For a similar reason, we roll up the AES rounds. +// +// On the last iteration, the remaining length may be less than 64 bytes. +// Handle this using masking. +// +// Since there are enough key powers available for all remaining data, +// there is no need to do a GHASH reduction after each iteration. +// Instead, multiply each remaining block by its own key power, and only +// do a GHASH reduction at the very end. + +// Make POWERS_PTR point to the key powers [H^N, H^(N-1), ...] where N +// is the number of blocks that remain. movq %rdx,%rax negq %rax - andq $-16,%rax + andq $-16,%rax // -round_up(DATALEN, 16) leaq 256(%r9,%rax,1),%r8 vpxor %xmm4,%xmm4,%xmm4 vpxor %xmm5,%xmm5,%xmm5 @@ -1114,9 +1120,9 @@ L$crypt_loop_4x_done__func2: jb L$partial_vec__func2 L$crypt_loop_1x__func2: +// Process a full 64-byte vector. - - +// Encrypt a vector of counter blocks. vpshufb %zmm8,%zmm12,%zmm0 vpaddd %zmm11,%zmm12,%zmm12 vpxord %zmm13,%zmm0,%zmm0 @@ -1129,19 +1135,19 @@ L$vaesenc_loop_tail_full_vec__func2: jne L$vaesenc_loop_tail_full_vec__func2 vaesenclast %zmm14,%zmm0,%zmm0 - +// XOR the data with the vector of keystream blocks. vmovdqu8 (%rdi),%zmm1 vpxord %zmm1,%zmm0,%zmm0 vmovdqu8 %zmm0,(%rsi) - +// Update GHASH with the ciphertext blocks, without reducing. vmovdqu8 (%r8),%zmm30 vpshufb %zmm8,%zmm1,%zmm0 vpxord %zmm10,%zmm0,%zmm0 - vpclmulqdq $0x00,%zmm30,%zmm0,%zmm7 - vpclmulqdq $0x01,%zmm30,%zmm0,%zmm1 - vpclmulqdq $0x10,%zmm30,%zmm0,%zmm2 - vpclmulqdq $0x11,%zmm30,%zmm0,%zmm3 + vpclmulqdq $0x00,%zmm30,%zmm0,%zmm7 // a_L * b_L + vpclmulqdq $0x01,%zmm30,%zmm0,%zmm1 // a_L * b_H + vpclmulqdq $0x10,%zmm30,%zmm0,%zmm2 // a_H * b_L + vpclmulqdq $0x11,%zmm30,%zmm0,%zmm3 // a_H * b_H vpxord %zmm7,%zmm4,%zmm4 vpternlogd $0x96,%zmm2,%zmm1,%zmm5 vpxord %zmm3,%zmm6,%zmm6 @@ -1159,10 +1165,10 @@ L$vaesenc_loop_tail_full_vec__func2: jz L$reduce__func2 L$partial_vec__func2: +// Process a partial vector of length 1 <= DATALEN < 64. - - - +// Set the data mask %k1 to DATALEN 1's. +// Set the key powers mask %k2 to round_up(DATALEN, 16) 1's. movq $-1,%rax bzhiq %rdx,%rax,%rax kmovq %rax,%k1 @@ -1172,8 +1178,8 @@ L$partial_vec__func2: bzhiq %rdx,%rax,%rax kmovq %rax,%k2 - - +// Encrypt one last vector of counter blocks. This does not need to be +// masked. The counter does not need to be incremented here. vpshufb %zmm8,%zmm12,%zmm0 vpxord %zmm13,%zmm0,%zmm0 leaq 16(%rcx),%rax @@ -1185,38 +1191,38 @@ L$vaesenc_loop_tail_partialvec__func2: jne L$vaesenc_loop_tail_partialvec__func2 vaesenclast %zmm14,%zmm0,%zmm0 - +// XOR the data with the appropriate number of keystream bytes. vmovdqu8 (%rdi),%zmm1{%k1}{z} vpxord %zmm1,%zmm0,%zmm0 vmovdqu8 %zmm0,(%rsi){%k1} - - - - - - - - - - - - +// Update GHASH with the ciphertext block(s), without reducing. +// +// In the case of DATALEN < 64, the ciphertext is zero-padded to 64 +// bytes. (If decrypting, it's done by the above masked load. If +// encrypting, it's done by the below masked register-to-register move.) +// Note that if DATALEN <= 48, there will be additional padding beyond +// the padding of the last block specified by GHASH itself; i.e., there +// may be whole block(s) that get processed by the GHASH multiplication +// and reduction instructions but should not actually be included in the +// GHASH. However, any such blocks are all-zeroes, and the values that +// they're multiplied with are also all-zeroes. Therefore they just add +// 0 * 0 = 0 to the final GHASH result, which makes no difference. vmovdqu8 (%r8),%zmm30{%k2}{z} vpshufb %zmm8,%zmm1,%zmm0 vpxord %zmm10,%zmm0,%zmm0 - vpclmulqdq $0x00,%zmm30,%zmm0,%zmm7 - vpclmulqdq $0x01,%zmm30,%zmm0,%zmm1 - vpclmulqdq $0x10,%zmm30,%zmm0,%zmm2 - vpclmulqdq $0x11,%zmm30,%zmm0,%zmm3 + vpclmulqdq $0x00,%zmm30,%zmm0,%zmm7 // a_L * b_L + vpclmulqdq $0x01,%zmm30,%zmm0,%zmm1 // a_L * b_H + vpclmulqdq $0x10,%zmm30,%zmm0,%zmm2 // a_H * b_L + vpclmulqdq $0x11,%zmm30,%zmm0,%zmm3 // a_H * b_H vpxord %zmm7,%zmm4,%zmm4 vpternlogd $0x96,%zmm2,%zmm1,%zmm5 vpxord %zmm3,%zmm6,%zmm6 L$reduce__func2: - +// Finally, do the GHASH reduction. vpclmulqdq $0x01,%zmm4,%zmm31,%zmm0 vpshufd $0x4e,%zmm4,%zmm4 vpternlogd $0x96,%zmm0,%zmm4,%zmm5 @@ -1232,11 +1238,11 @@ L$reduce__func2: L$done__func2: - +// Store the updated GHASH accumulator back to memory. vpshufb %xmm8,%xmm10,%xmm10 vmovdqu %xmm10,(%r12) - vzeroupper + vzeroupper // This is needed after using ymm or zmm registers. popq %r12 ret diff --git a/third_party/boringssl/gen/bcm/aes-gcm-avx512-x86_64-linux.S b/third_party/boringssl/gen/bcm/aes-gcm-avx512-x86_64-linux.S index bec5e883e..5bfc2bcff 100644 --- a/third_party/boringssl/gen/bcm/aes-gcm-avx512-x86_64-linux.S +++ b/third_party/boringssl/gen/bcm/aes-gcm-avx512-x86_64-linux.S @@ -5,34 +5,36 @@ #if !defined(OPENSSL_NO_ASM) && defined(OPENSSL_X86_64) && defined(__ELF__) .section .rodata -.align 64 +.align 64 +aes_gcm_avx512_constants: +// A shuffle mask that reflects the bytes of 16-byte blocks .Lbswap_mask: .quad 0x08090a0b0c0d0e0f, 0x0001020304050607 - - - - - - - +// This is the GHASH reducing polynomial without its constant term, i.e. +// x^128 + x^7 + x^2 + x, represented using the backwards mapping +// between bits and polynomial coefficients. +// +// Alternatively, it can be interpreted as the naturally-ordered +// representation of the polynomial x^127 + x^126 + x^121 + 1, i.e. the +// "reversed" GHASH reducing polynomial without its x^128 term. .Lgfpoly: .quad 1, 0xc200000000000000 - +// Same as above, but with the (1 << 64) bit set. .Lgfpoly_and_internal_carrybit: .quad 1, 0xc200000000000001 - +// Values needed to prepare the initial vector of counter blocks. .Lctr_pattern: .quad 0, 0 .quad 1, 0 .quad 2, 0 .quad 3, 0 - +// The number of AES blocks per vector, as a 128-bit value. .Linc_4blocks: .quad 4, 0 @@ -45,100 +47,100 @@ gcm_init_vpclmulqdq_avx512: .cfi_startproc _CET_ENDBR - +// Get pointer to lowest set of key powers (located at end of array). leaq 256-64(%rdi),%r8 - - +// Load the byte-reflected hash subkey. BoringSSL provides it in +// byte-reflected form except the two halves are in the wrong order. vpshufd $0x4e,(%rsi),%xmm3 - - - - - - - - - - - - - - - +// Finish preprocessing the first key power, H^1. Since this GHASH +// implementation operates directly on values with the backwards bit +// order specified by the GCM standard, it's necessary to preprocess the +// raw key as follows. First, reflect its bytes. Second, multiply it +// by x^-1 mod x^128 + x^7 + x^2 + x + 1 (if using the backwards +// interpretation of polynomial coefficients), which can also be +// interpreted as multiplication by x mod x^128 + x^127 + x^126 + x^121 +// + 1 using the alternative, natural interpretation of polynomial +// coefficients. For details, see the comment above _ghash_mul. +// +// Either way, for the multiplication the concrete operation performed +// is a left shift of the 128-bit value by 1 bit, then an XOR with (0xc2 +// << 120) | 1 if a 1 bit was carried out. However, there's no 128-bit +// wide shift instruction, so instead double each of the two 64-bit +// halves and incorporate the internal carry bit into the value XOR'd. vpshufd $0xd3,%xmm3,%xmm0 vpsrad $31,%xmm0,%xmm0 vpaddq %xmm3,%xmm3,%xmm3 - +// H_CUR_XMM ^= TMP0_XMM & gfpoly_and_internal_carrybit vpternlogd $0x78,.Lgfpoly_and_internal_carrybit(%rip),%xmm0,%xmm3 - +// Load the gfpoly constant. vbroadcasti32x4 .Lgfpoly(%rip),%zmm5 - - - - - - - - vpclmulqdq $0x00,%xmm3,%xmm3,%xmm0 - vpclmulqdq $0x11,%xmm3,%xmm3,%xmm4 - vpclmulqdq $0x01,%xmm0,%xmm5,%xmm1 - vpshufd $0x4e,%xmm0,%xmm0 - vpxor %xmm0,%xmm1,%xmm1 - vpclmulqdq $0x01,%xmm1,%xmm5,%xmm0 - vpshufd $0x4e,%xmm1,%xmm1 - vpternlogd $0x96,%xmm0,%xmm1,%xmm4 - - - +// Square H^1 to get H^2. +// +// Note that as with H^1, all higher key powers also need an extra +// factor of x^-1 (or x using the natural interpretation). Nothing +// special needs to be done to make this happen, though: H^1 * H^1 would +// end up with two factors of x^-1, but the multiplication consumes one. +// So the product H^2 ends up with the desired one factor of x^-1. + vpclmulqdq $0x00,%xmm3,%xmm3,%xmm0 // LO = a_L * a_L + vpclmulqdq $0x11,%xmm3,%xmm3,%xmm4 // HI = a_H * a_H + vpclmulqdq $0x01,%xmm0,%xmm5,%xmm1 // LO_L*(x^63 + x^62 + x^57) + vpshufd $0x4e,%xmm0,%xmm0 // Swap halves of LO + vpxor %xmm0,%xmm1,%xmm1 // Fold LO into MI + vpclmulqdq $0x01,%xmm1,%xmm5,%xmm0 // MI_L*(x^63 + x^62 + x^57) + vpshufd $0x4e,%xmm1,%xmm1 // Swap halves of MI + vpternlogd $0x96,%xmm0,%xmm1,%xmm4 // Fold MI into HI + + +// Create H_CUR_YMM = [H^2, H^1] and H_INC_YMM = [H^2, H^2]. vinserti128 $1,%xmm3,%ymm4,%ymm3 vinserti128 $1,%xmm4,%ymm4,%ymm4 - - vpclmulqdq $0x00,%ymm4,%ymm3,%ymm0 - vpclmulqdq $0x01,%ymm4,%ymm3,%ymm1 - vpclmulqdq $0x10,%ymm4,%ymm3,%ymm2 - vpxord %ymm2,%ymm1,%ymm1 - vpclmulqdq $0x01,%ymm0,%ymm5,%ymm2 - vpshufd $0x4e,%ymm0,%ymm0 - vpternlogd $0x96,%ymm2,%ymm0,%ymm1 - vpclmulqdq $0x11,%ymm4,%ymm3,%ymm4 - vpclmulqdq $0x01,%ymm1,%ymm5,%ymm0 - vpshufd $0x4e,%ymm1,%ymm1 - vpternlogd $0x96,%ymm0,%ymm1,%ymm4 +// Create H_CUR = [H^4, H^3, H^2, H^1] and H_INC = [H^4, H^4, H^4, H^4]. + vpclmulqdq $0x00,%ymm4,%ymm3,%ymm0 // LO = a_L * b_L + vpclmulqdq $0x01,%ymm4,%ymm3,%ymm1 // MI_0 = a_L * b_H + vpclmulqdq $0x10,%ymm4,%ymm3,%ymm2 // MI_1 = a_H * b_L + vpxord %ymm2,%ymm1,%ymm1 // MI = MI_0 + MI_1 + vpclmulqdq $0x01,%ymm0,%ymm5,%ymm2 // LO_L*(x^63 + x^62 + x^57) + vpshufd $0x4e,%ymm0,%ymm0 // Swap halves of LO + vpternlogd $0x96,%ymm2,%ymm0,%ymm1 // Fold LO into MI + vpclmulqdq $0x11,%ymm4,%ymm3,%ymm4 // HI = a_H * b_H + vpclmulqdq $0x01,%ymm1,%ymm5,%ymm0 // MI_L*(x^63 + x^62 + x^57) + vpshufd $0x4e,%ymm1,%ymm1 // Swap halves of MI + vpternlogd $0x96,%ymm0,%ymm1,%ymm4 // Fold MI into HI vinserti64x4 $1,%ymm3,%zmm4,%zmm3 vshufi64x2 $0,%zmm4,%zmm4,%zmm4 - +// Store the lowest set of key powers. vmovdqu8 %zmm3,(%r8) - - - +// Compute and store the remaining key powers. +// Repeatedly multiply [H^(i+3), H^(i+2), H^(i+1), H^i] by +// [H^4, H^4, H^4, H^4] to get [H^(i+7), H^(i+6), H^(i+5), H^(i+4)]. movl $3,%eax .Lprecompute_next: subq $64,%r8 - vpclmulqdq $0x00,%zmm4,%zmm3,%zmm0 - vpclmulqdq $0x01,%zmm4,%zmm3,%zmm1 - vpclmulqdq $0x10,%zmm4,%zmm3,%zmm2 - vpxord %zmm2,%zmm1,%zmm1 - vpclmulqdq $0x01,%zmm0,%zmm5,%zmm2 - vpshufd $0x4e,%zmm0,%zmm0 - vpternlogd $0x96,%zmm2,%zmm0,%zmm1 - vpclmulqdq $0x11,%zmm4,%zmm3,%zmm3 - vpclmulqdq $0x01,%zmm1,%zmm5,%zmm0 - vpshufd $0x4e,%zmm1,%zmm1 - vpternlogd $0x96,%zmm0,%zmm1,%zmm3 + vpclmulqdq $0x00,%zmm4,%zmm3,%zmm0 // LO = a_L * b_L + vpclmulqdq $0x01,%zmm4,%zmm3,%zmm1 // MI_0 = a_L * b_H + vpclmulqdq $0x10,%zmm4,%zmm3,%zmm2 // MI_1 = a_H * b_L + vpxord %zmm2,%zmm1,%zmm1 // MI = MI_0 + MI_1 + vpclmulqdq $0x01,%zmm0,%zmm5,%zmm2 // LO_L*(x^63 + x^62 + x^57) + vpshufd $0x4e,%zmm0,%zmm0 // Swap halves of LO + vpternlogd $0x96,%zmm2,%zmm0,%zmm1 // Fold LO into MI + vpclmulqdq $0x11,%zmm4,%zmm3,%zmm3 // HI = a_H * b_H + vpclmulqdq $0x01,%zmm1,%zmm5,%zmm0 // MI_L*(x^63 + x^62 + x^57) + vpshufd $0x4e,%zmm1,%zmm1 // Swap halves of MI + vpternlogd $0x96,%zmm0,%zmm1,%zmm3 // Fold MI into HI vmovdqu8 %zmm3,(%r8) decl %eax jnz .Lprecompute_next - vzeroupper + vzeroupper // This is needed after using ymm or zmm registers. ret .cfi_endproc @@ -160,23 +162,23 @@ _CET_ENDBR vmovdqu .Lgfpoly(%rip),%xmm3 vpshufb %xmm1,%xmm0,%xmm0 - vpclmulqdq $0x00,%xmm2,%xmm0,%xmm4 - vpclmulqdq $0x01,%xmm2,%xmm0,%xmm5 - vpclmulqdq $0x10,%xmm2,%xmm0,%xmm6 - vpxord %xmm6,%xmm5,%xmm5 - vpclmulqdq $0x01,%xmm4,%xmm3,%xmm6 - vpshufd $0x4e,%xmm4,%xmm4 - vpternlogd $0x96,%xmm6,%xmm4,%xmm5 - vpclmulqdq $0x11,%xmm2,%xmm0,%xmm0 - vpclmulqdq $0x01,%xmm5,%xmm3,%xmm4 - vpshufd $0x4e,%xmm5,%xmm5 - vpternlogd $0x96,%xmm4,%xmm5,%xmm0 + vpclmulqdq $0x00,%xmm2,%xmm0,%xmm4 // LO = a_L * b_L + vpclmulqdq $0x01,%xmm2,%xmm0,%xmm5 // MI_0 = a_L * b_H + vpclmulqdq $0x10,%xmm2,%xmm0,%xmm6 // MI_1 = a_H * b_L + vpxord %xmm6,%xmm5,%xmm5 // MI = MI_0 + MI_1 + vpclmulqdq $0x01,%xmm4,%xmm3,%xmm6 // LO_L*(x^63 + x^62 + x^57) + vpshufd $0x4e,%xmm4,%xmm4 // Swap halves of LO + vpternlogd $0x96,%xmm6,%xmm4,%xmm5 // Fold LO into MI + vpclmulqdq $0x11,%xmm2,%xmm0,%xmm0 // HI = a_H * b_H + vpclmulqdq $0x01,%xmm5,%xmm3,%xmm4 // MI_L*(x^63 + x^62 + x^57) + vpshufd $0x4e,%xmm5,%xmm5 // Swap halves of MI + vpternlogd $0x96,%xmm4,%xmm5,%xmm0 // Fold MI into HI vpshufb %xmm1,%xmm0,%xmm0 vmovdqu %xmm0,(%rdi) - +// No need for vzeroupper, since only xmm registers were used. ret .cfi_endproc @@ -192,37 +194,37 @@ _CET_ENDBR - - - +// Load the bswap_mask and gfpoly constants. Since AADLEN is usually small, +// usually only 128-bit vectors will be used. So as an optimization, don't +// broadcast these constants to all 128-bit lanes quite yet. vmovdqu .Lbswap_mask(%rip),%xmm4 vmovdqu .Lgfpoly(%rip),%xmm10 - +// Load the GHASH accumulator. vmovdqu (%rdi),%xmm5 vpshufb %xmm4,%xmm5,%xmm5 - +// Optimize for AADLEN < 64 by checking for AADLEN < 64 before AADLEN < 256. cmpq $64,%rcx jb .Laad_blockbyblock - - +// AADLEN >= 64, so we'll operate on full vectors. Broadcast bswap_mask and +// gfpoly to all 128-bit lanes. vshufi64x2 $0,%zmm4,%zmm4,%zmm4 vshufi64x2 $0,%zmm10,%zmm10,%zmm10 - +// Load the lowest set of key powers. vmovdqu8 256-64(%rsi),%zmm9 cmpq $256,%rcx jb .Laad_loop_1x - +// AADLEN >= 256. Load the higher key powers. vmovdqu8 256-256(%rsi),%zmm6 vmovdqu8 256-192(%rsi),%zmm7 vmovdqu8 256-128(%rsi),%zmm8 - +// Update GHASH with 256 bytes of AAD at a time. .Laad_loop_4x: vmovdqu8 0(%rdx),%zmm0 vmovdqu8 64(%rdx),%zmm1 @@ -233,36 +235,36 @@ _CET_ENDBR vpshufb %zmm4,%zmm1,%zmm1 vpshufb %zmm4,%zmm2,%zmm2 vpshufb %zmm4,%zmm3,%zmm3 - vpclmulqdq $0x00,%zmm6,%zmm0,%zmm5 - vpclmulqdq $0x00,%zmm7,%zmm1,%zmm11 - vpclmulqdq $0x00,%zmm8,%zmm2,%zmm12 - vpxord %zmm11,%zmm5,%zmm5 - vpclmulqdq $0x00,%zmm9,%zmm3,%zmm13 - vpternlogd $0x96,%zmm13,%zmm12,%zmm5 - vpclmulqdq $0x01,%zmm6,%zmm0,%zmm11 - vpclmulqdq $0x01,%zmm7,%zmm1,%zmm12 - vpclmulqdq $0x01,%zmm8,%zmm2,%zmm13 - vpternlogd $0x96,%zmm13,%zmm12,%zmm11 - vpclmulqdq $0x01,%zmm9,%zmm3,%zmm12 - vpclmulqdq $0x10,%zmm6,%zmm0,%zmm13 - vpternlogd $0x96,%zmm13,%zmm12,%zmm11 - vpclmulqdq $0x10,%zmm7,%zmm1,%zmm12 - vpclmulqdq $0x10,%zmm8,%zmm2,%zmm13 - vpternlogd $0x96,%zmm13,%zmm12,%zmm11 - vpclmulqdq $0x01,%zmm5,%zmm10,%zmm13 - vpclmulqdq $0x10,%zmm9,%zmm3,%zmm12 - vpxord %zmm12,%zmm11,%zmm11 - vpshufd $0x4e,%zmm5,%zmm5 - vpclmulqdq $0x11,%zmm6,%zmm0,%zmm0 - vpclmulqdq $0x11,%zmm7,%zmm1,%zmm1 - vpclmulqdq $0x11,%zmm8,%zmm2,%zmm2 - vpternlogd $0x96,%zmm13,%zmm5,%zmm11 - vpclmulqdq $0x11,%zmm9,%zmm3,%zmm3 - vpternlogd $0x96,%zmm2,%zmm1,%zmm0 - vpclmulqdq $0x01,%zmm11,%zmm10,%zmm12 - vpxord %zmm3,%zmm0,%zmm5 - vpshufd $0x4e,%zmm11,%zmm11 - vpternlogd $0x96,%zmm12,%zmm11,%zmm5 + vpclmulqdq $0x00,%zmm6,%zmm0,%zmm5 // LO_0 + vpclmulqdq $0x00,%zmm7,%zmm1,%zmm11 // LO_1 + vpclmulqdq $0x00,%zmm8,%zmm2,%zmm12 // LO_2 + vpxord %zmm11,%zmm5,%zmm5 // sum(LO_{1,0}) + vpclmulqdq $0x00,%zmm9,%zmm3,%zmm13 // LO_3 + vpternlogd $0x96,%zmm13,%zmm12,%zmm5 // LO = sum(LO_{3,2,1,0}) + vpclmulqdq $0x01,%zmm6,%zmm0,%zmm11 // MI_0 + vpclmulqdq $0x01,%zmm7,%zmm1,%zmm12 // MI_1 + vpclmulqdq $0x01,%zmm8,%zmm2,%zmm13 // MI_2 + vpternlogd $0x96,%zmm13,%zmm12,%zmm11 // sum(MI_{2,1,0}) + vpclmulqdq $0x01,%zmm9,%zmm3,%zmm12 // MI_3 + vpclmulqdq $0x10,%zmm6,%zmm0,%zmm13 // MI_4 + vpternlogd $0x96,%zmm13,%zmm12,%zmm11 // sum(MI_{4,3,2,1,0}) + vpclmulqdq $0x10,%zmm7,%zmm1,%zmm12 // MI_5 + vpclmulqdq $0x10,%zmm8,%zmm2,%zmm13 // MI_6 + vpternlogd $0x96,%zmm13,%zmm12,%zmm11 // sum(MI_{6,5,4,3,2,1,0}) + vpclmulqdq $0x01,%zmm5,%zmm10,%zmm13 // LO_L*(x^63 + x^62 + x^57) + vpclmulqdq $0x10,%zmm9,%zmm3,%zmm12 // MI_7 + vpxord %zmm12,%zmm11,%zmm11 // MI = sum(MI_{7,6,5,4,3,2,1,0}) + vpshufd $0x4e,%zmm5,%zmm5 // Swap halves of LO + vpclmulqdq $0x11,%zmm6,%zmm0,%zmm0 // HI_0 + vpclmulqdq $0x11,%zmm7,%zmm1,%zmm1 // HI_1 + vpclmulqdq $0x11,%zmm8,%zmm2,%zmm2 // HI_2 + vpternlogd $0x96,%zmm13,%zmm5,%zmm11 // Fold LO into MI + vpclmulqdq $0x11,%zmm9,%zmm3,%zmm3 // HI_3 + vpternlogd $0x96,%zmm2,%zmm1,%zmm0 // sum(HI_{2,1,0}) + vpclmulqdq $0x01,%zmm11,%zmm10,%zmm12 // MI_L*(x^63 + x^62 + x^57) + vpxord %zmm3,%zmm0,%zmm5 // HI = sum(HI_{3,2,1,0}) + vpshufd $0x4e,%zmm11,%zmm11 // Swap halves of MI + vpternlogd $0x96,%zmm12,%zmm11,%zmm5 // Fold MI into HI vextracti32x4 $1,%zmm5,%xmm0 vextracti32x4 $2,%zmm5,%xmm1 vextracti32x4 $3,%zmm5,%xmm2 @@ -274,24 +276,24 @@ _CET_ENDBR cmpq $256,%rcx jae .Laad_loop_4x - +// Update GHASH with 64 bytes of AAD at a time. cmpq $64,%rcx jb .Laad_large_done .Laad_loop_1x: vmovdqu8 (%rdx),%zmm0 vpshufb %zmm4,%zmm0,%zmm0 vpxord %zmm0,%zmm5,%zmm5 - vpclmulqdq $0x00,%zmm9,%zmm5,%zmm0 - vpclmulqdq $0x01,%zmm9,%zmm5,%zmm1 - vpclmulqdq $0x10,%zmm9,%zmm5,%zmm2 - vpxord %zmm2,%zmm1,%zmm1 - vpclmulqdq $0x01,%zmm0,%zmm10,%zmm2 - vpshufd $0x4e,%zmm0,%zmm0 - vpternlogd $0x96,%zmm2,%zmm0,%zmm1 - vpclmulqdq $0x11,%zmm9,%zmm5,%zmm5 - vpclmulqdq $0x01,%zmm1,%zmm10,%zmm0 - vpshufd $0x4e,%zmm1,%zmm1 - vpternlogd $0x96,%zmm0,%zmm1,%zmm5 + vpclmulqdq $0x00,%zmm9,%zmm5,%zmm0 // LO = a_L * b_L + vpclmulqdq $0x01,%zmm9,%zmm5,%zmm1 // MI_0 = a_L * b_H + vpclmulqdq $0x10,%zmm9,%zmm5,%zmm2 // MI_1 = a_H * b_L + vpxord %zmm2,%zmm1,%zmm1 // MI = MI_0 + MI_1 + vpclmulqdq $0x01,%zmm0,%zmm10,%zmm2 // LO_L*(x^63 + x^62 + x^57) + vpshufd $0x4e,%zmm0,%zmm0 // Swap halves of LO + vpternlogd $0x96,%zmm2,%zmm0,%zmm1 // Fold LO into MI + vpclmulqdq $0x11,%zmm9,%zmm5,%zmm5 // HI = a_H * b_H + vpclmulqdq $0x01,%zmm1,%zmm10,%zmm0 // MI_L*(x^63 + x^62 + x^57) + vpshufd $0x4e,%zmm1,%zmm1 // Swap halves of MI + vpternlogd $0x96,%zmm0,%zmm1,%zmm5 // Fold MI into HI vextracti32x4 $1,%zmm5,%xmm0 vextracti32x4 $2,%zmm5,%xmm1 @@ -306,7 +308,7 @@ _CET_ENDBR .Laad_large_done: - +// GHASH the remaining data 16 bytes at a time, using xmm registers only. .Laad_blockbyblock: testq %rcx,%rcx jz .Laad_done @@ -315,28 +317,28 @@ _CET_ENDBR vmovdqu (%rdx),%xmm0 vpshufb %xmm4,%xmm0,%xmm0 vpxor %xmm0,%xmm5,%xmm5 - vpclmulqdq $0x00,%xmm9,%xmm5,%xmm0 - vpclmulqdq $0x01,%xmm9,%xmm5,%xmm1 - vpclmulqdq $0x10,%xmm9,%xmm5,%xmm2 - vpxord %xmm2,%xmm1,%xmm1 - vpclmulqdq $0x01,%xmm0,%xmm10,%xmm2 - vpshufd $0x4e,%xmm0,%xmm0 - vpternlogd $0x96,%xmm2,%xmm0,%xmm1 - vpclmulqdq $0x11,%xmm9,%xmm5,%xmm5 - vpclmulqdq $0x01,%xmm1,%xmm10,%xmm0 - vpshufd $0x4e,%xmm1,%xmm1 - vpternlogd $0x96,%xmm0,%xmm1,%xmm5 + vpclmulqdq $0x00,%xmm9,%xmm5,%xmm0 // LO = a_L * b_L + vpclmulqdq $0x01,%xmm9,%xmm5,%xmm1 // MI_0 = a_L * b_H + vpclmulqdq $0x10,%xmm9,%xmm5,%xmm2 // MI_1 = a_H * b_L + vpxord %xmm2,%xmm1,%xmm1 // MI = MI_0 + MI_1 + vpclmulqdq $0x01,%xmm0,%xmm10,%xmm2 // LO_L*(x^63 + x^62 + x^57) + vpshufd $0x4e,%xmm0,%xmm0 // Swap halves of LO + vpternlogd $0x96,%xmm2,%xmm0,%xmm1 // Fold LO into MI + vpclmulqdq $0x11,%xmm9,%xmm5,%xmm5 // HI = a_H * b_H + vpclmulqdq $0x01,%xmm1,%xmm10,%xmm0 // MI_L*(x^63 + x^62 + x^57) + vpshufd $0x4e,%xmm1,%xmm1 // Swap halves of MI + vpternlogd $0x96,%xmm0,%xmm1,%xmm5 // Fold MI into HI addq $16,%rdx subq $16,%rcx jnz .Laad_loop_blockbyblock .Laad_done: - +// Store the updated GHASH accumulator back to memory. vpshufb %xmm4,%xmm5,%xmm5 vmovdqu %xmm5,(%rdi) - vzeroupper + vzeroupper // This is needed after using ymm or zmm registers. ret .cfi_endproc @@ -353,55 +355,55 @@ _CET_ENDBR .cfi_adjust_cfa_offset 8 .cfi_offset %r12,-16 - movq 16(%rsp),%r12 + movq 16(%rsp),%r12 // arg7 #ifdef BORINGSSL_DISPATCH_TEST .extern BORINGSSL_function_hit .hidden BORINGSSL_function_hit movb $1,BORINGSSL_function_hit+7(%rip) #endif - +// Load some constants. vbroadcasti32x4 .Lbswap_mask(%rip),%zmm8 vbroadcasti32x4 .Lgfpoly(%rip),%zmm31 - - +// Load the GHASH accumulator and the starting counter. +// BoringSSL passes these values in big endian format. vmovdqu (%r12),%xmm10 vpshufb %xmm8,%xmm10,%xmm10 vbroadcasti32x4 (%r8),%zmm12 vpshufb %zmm8,%zmm12,%zmm12 - - +// Load the AES key length in bytes. BoringSSL stores number of rounds +// minus 1, so convert using: AESKEYLEN = 4 * aeskey->rounds - 20. movl 240(%rcx),%r10d leal -20(,%r10,4),%r10d - - - +// Make RNDKEYLAST_PTR point to the last AES round key. This is the +// round key with index 10, 12, or 14 for AES-128, AES-192, or AES-256 +// respectively. Then load the zero-th and last round keys. leaq 96(%rcx,%r10,4),%r11 vbroadcasti32x4 (%rcx),%zmm13 vbroadcasti32x4 (%r11),%zmm14 - +// Finish initializing LE_CTR by adding [0, 1, 2, 3] to its low words. vpaddd .Lctr_pattern(%rip),%zmm12,%zmm12 - +// Load 4 into all 128-bit lanes of LE_CTR_INC. vbroadcasti32x4 .Linc_4blocks(%rip),%zmm11 - - +// If there are at least 256 bytes of data, then continue into the loop +// that processes 256 bytes of data at a time. Otherwise skip it. cmpq $256,%rdx jb .Lcrypt_loop_4x_done__func1 - +// Load powers of the hash key. vmovdqu8 256-256(%r9),%zmm27 vmovdqu8 256-192(%r9),%zmm28 vmovdqu8 256-128(%r9),%zmm29 vmovdqu8 256-64(%r9),%zmm30 - - - - +// Encrypt the first 4 vectors of plaintext blocks. Leave the resulting +// ciphertext in GHASHDATA[0-3] for GHASH. +// Increment le_ctr four times to generate four vectors of little-endian +// counter blocks, swap each to big-endian, and store them in aesdata[0-3]. vpshufb %zmm8,%zmm12,%zmm0 vpaddd %zmm11,%zmm12,%zmm12 vpshufb %zmm8,%zmm12,%zmm1 @@ -411,7 +413,7 @@ _CET_ENDBR vpshufb %zmm8,%zmm12,%zmm3 vpaddd %zmm11,%zmm12,%zmm12 - +// AES "round zero": XOR in the zero-th round key. vpxord %zmm13,%zmm0,%zmm0 vpxord %zmm13,%zmm1,%zmm1 vpxord %zmm13,%zmm2,%zmm2 @@ -446,7 +448,7 @@ _CET_ENDBR subq $256,%rdx cmpq $256,%rdx jb .Lghash_last_ciphertext_4x__func1 - +// Cache as many additional AES round keys as possible. vbroadcasti32x4 -144(%r11),%zmm15 vbroadcasti32x4 -128(%r11),%zmm16 vbroadcasti32x4 -112(%r11),%zmm17 @@ -458,9 +460,9 @@ _CET_ENDBR vbroadcasti32x4 -16(%r11),%zmm23 .Lcrypt_loop_4x__func1: - - - +// Start the AES encryption of the counter blocks. +// Increment le_ctr four times to generate four vectors of little-endian +// counter blocks, swap each to big-endian, and store them in aesdata[0-3]. vpshufb %zmm8,%zmm12,%zmm0 vpaddd %zmm11,%zmm12,%zmm12 vpshufb %zmm8,%zmm12,%zmm1 @@ -470,7 +472,7 @@ _CET_ENDBR vpshufb %zmm8,%zmm12,%zmm3 vpaddd %zmm11,%zmm12,%zmm12 - +// AES "round zero": XOR in the zero-th round key. vpxord %zmm13,%zmm0,%zmm0 vpxord %zmm13,%zmm1,%zmm1 vpxord %zmm13,%zmm2,%zmm2 @@ -479,7 +481,7 @@ _CET_ENDBR cmpl $24,%r10d jl .Laes128__func1 je .Laes192__func1 - +// AES-256 vbroadcasti32x4 -208(%r11),%zmm9 vaesenc %zmm9,%zmm0,%zmm0 vaesenc %zmm9,%zmm1,%zmm1 @@ -507,17 +509,17 @@ _CET_ENDBR .Laes128__func1: - - - +// Prefetch the source data 512 bytes ahead into the L1 data cache, to +// improve performance when the hardware prefetcher is disabled. Assumes the +// L1 data cache line size is 64 bytes (de facto standard on x86_64). prefetcht0 512+0(%rdi) prefetcht0 512+64(%rdi) prefetcht0 512+128(%rdi) prefetcht0 512+192(%rdi) - - - +// Finish the AES encryption of the counter blocks in AESDATA[0-3], +// interleaved with the GHASH update of the ciphertext blocks in +// GHASHDATA[0-3]. vpshufb %zmm8,%zmm4,%zmm4 vpxord %zmm10,%zmm4,%zmm4 vpshufb %zmm8,%zmm5,%zmm5 @@ -529,78 +531,78 @@ _CET_ENDBR vaesenc %zmm15,%zmm3,%zmm3 vpshufb %zmm8,%zmm7,%zmm7 - vpclmulqdq $0x00,%zmm27,%zmm4,%zmm10 - vpclmulqdq $0x00,%zmm28,%zmm5,%zmm24 - vpclmulqdq $0x00,%zmm29,%zmm6,%zmm25 + vpclmulqdq $0x00,%zmm27,%zmm4,%zmm10 // LO_0 + vpclmulqdq $0x00,%zmm28,%zmm5,%zmm24 // LO_1 + vpclmulqdq $0x00,%zmm29,%zmm6,%zmm25 // LO_2 vaesenc %zmm16,%zmm0,%zmm0 vaesenc %zmm16,%zmm1,%zmm1 vaesenc %zmm16,%zmm2,%zmm2 vaesenc %zmm16,%zmm3,%zmm3 - vpxord %zmm24,%zmm10,%zmm10 - vpclmulqdq $0x00,%zmm30,%zmm7,%zmm26 - vpternlogd $0x96,%zmm26,%zmm25,%zmm10 - vpclmulqdq $0x01,%zmm27,%zmm4,%zmm24 + vpxord %zmm24,%zmm10,%zmm10 // sum(LO_{1,0}) + vpclmulqdq $0x00,%zmm30,%zmm7,%zmm26 // LO_3 + vpternlogd $0x96,%zmm26,%zmm25,%zmm10 // LO = sum(LO_{3,2,1,0}) + vpclmulqdq $0x01,%zmm27,%zmm4,%zmm24 // MI_0 vaesenc %zmm17,%zmm0,%zmm0 vaesenc %zmm17,%zmm1,%zmm1 vaesenc %zmm17,%zmm2,%zmm2 vaesenc %zmm17,%zmm3,%zmm3 - vpclmulqdq $0x01,%zmm28,%zmm5,%zmm25 - vpclmulqdq $0x01,%zmm29,%zmm6,%zmm26 - vpternlogd $0x96,%zmm26,%zmm25,%zmm24 - vpclmulqdq $0x01,%zmm30,%zmm7,%zmm25 + vpclmulqdq $0x01,%zmm28,%zmm5,%zmm25 // MI_1 + vpclmulqdq $0x01,%zmm29,%zmm6,%zmm26 // MI_2 + vpternlogd $0x96,%zmm26,%zmm25,%zmm24 // sum(MI_{2,1,0}) + vpclmulqdq $0x01,%zmm30,%zmm7,%zmm25 // MI_3 vaesenc %zmm18,%zmm0,%zmm0 vaesenc %zmm18,%zmm1,%zmm1 vaesenc %zmm18,%zmm2,%zmm2 vaesenc %zmm18,%zmm3,%zmm3 - vpclmulqdq $0x10,%zmm27,%zmm4,%zmm26 - vpternlogd $0x96,%zmm26,%zmm25,%zmm24 - vpclmulqdq $0x10,%zmm28,%zmm5,%zmm25 - vpclmulqdq $0x10,%zmm29,%zmm6,%zmm26 + vpclmulqdq $0x10,%zmm27,%zmm4,%zmm26 // MI_4 + vpternlogd $0x96,%zmm26,%zmm25,%zmm24 // sum(MI_{4,3,2,1,0}) + vpclmulqdq $0x10,%zmm28,%zmm5,%zmm25 // MI_5 + vpclmulqdq $0x10,%zmm29,%zmm6,%zmm26 // MI_6 vaesenc %zmm19,%zmm0,%zmm0 vaesenc %zmm19,%zmm1,%zmm1 vaesenc %zmm19,%zmm2,%zmm2 vaesenc %zmm19,%zmm3,%zmm3 - vpternlogd $0x96,%zmm26,%zmm25,%zmm24 - vpclmulqdq $0x01,%zmm10,%zmm31,%zmm26 - vpclmulqdq $0x10,%zmm30,%zmm7,%zmm25 - vpxord %zmm25,%zmm24,%zmm24 + vpternlogd $0x96,%zmm26,%zmm25,%zmm24 // sum(MI_{6,5,4,3,2,1,0}) + vpclmulqdq $0x01,%zmm10,%zmm31,%zmm26 // LO_L*(x^63 + x^62 + x^57) + vpclmulqdq $0x10,%zmm30,%zmm7,%zmm25 // MI_7 + vpxord %zmm25,%zmm24,%zmm24 // MI = sum(MI_{7,6,5,4,3,2,1,0}) vaesenc %zmm20,%zmm0,%zmm0 vaesenc %zmm20,%zmm1,%zmm1 vaesenc %zmm20,%zmm2,%zmm2 vaesenc %zmm20,%zmm3,%zmm3 - vpshufd $0x4e,%zmm10,%zmm10 - vpclmulqdq $0x11,%zmm27,%zmm4,%zmm4 - vpclmulqdq $0x11,%zmm28,%zmm5,%zmm5 - vpclmulqdq $0x11,%zmm29,%zmm6,%zmm6 + vpshufd $0x4e,%zmm10,%zmm10 // Swap halves of LO + vpclmulqdq $0x11,%zmm27,%zmm4,%zmm4 // HI_0 + vpclmulqdq $0x11,%zmm28,%zmm5,%zmm5 // HI_1 + vpclmulqdq $0x11,%zmm29,%zmm6,%zmm6 // HI_2 vaesenc %zmm21,%zmm0,%zmm0 vaesenc %zmm21,%zmm1,%zmm1 vaesenc %zmm21,%zmm2,%zmm2 vaesenc %zmm21,%zmm3,%zmm3 - vpternlogd $0x96,%zmm26,%zmm10,%zmm24 - vpclmulqdq $0x11,%zmm30,%zmm7,%zmm7 - vpternlogd $0x96,%zmm6,%zmm5,%zmm4 - vpclmulqdq $0x01,%zmm24,%zmm31,%zmm25 + vpternlogd $0x96,%zmm26,%zmm10,%zmm24 // Fold LO into MI + vpclmulqdq $0x11,%zmm30,%zmm7,%zmm7 // HI_3 + vpternlogd $0x96,%zmm6,%zmm5,%zmm4 // sum(HI_{2,1,0}) + vpclmulqdq $0x01,%zmm24,%zmm31,%zmm25 // MI_L*(x^63 + x^62 + x^57) vaesenc %zmm22,%zmm0,%zmm0 vaesenc %zmm22,%zmm1,%zmm1 vaesenc %zmm22,%zmm2,%zmm2 vaesenc %zmm22,%zmm3,%zmm3 - vpxord %zmm7,%zmm4,%zmm10 - vpshufd $0x4e,%zmm24,%zmm24 - vpternlogd $0x96,%zmm25,%zmm24,%zmm10 + vpxord %zmm7,%zmm4,%zmm10 // HI = sum(HI_{3,2,1,0}) + vpshufd $0x4e,%zmm24,%zmm24 // Swap halves of MI + vpternlogd $0x96,%zmm25,%zmm24,%zmm10 // Fold MI into HI vaesenc %zmm23,%zmm0,%zmm0 vaesenc %zmm23,%zmm1,%zmm1 @@ -638,36 +640,36 @@ _CET_ENDBR vpshufb %zmm8,%zmm5,%zmm5 vpshufb %zmm8,%zmm6,%zmm6 vpshufb %zmm8,%zmm7,%zmm7 - vpclmulqdq $0x00,%zmm27,%zmm4,%zmm10 - vpclmulqdq $0x00,%zmm28,%zmm5,%zmm24 - vpclmulqdq $0x00,%zmm29,%zmm6,%zmm25 - vpxord %zmm24,%zmm10,%zmm10 - vpclmulqdq $0x00,%zmm30,%zmm7,%zmm26 - vpternlogd $0x96,%zmm26,%zmm25,%zmm10 - vpclmulqdq $0x01,%zmm27,%zmm4,%zmm24 - vpclmulqdq $0x01,%zmm28,%zmm5,%zmm25 - vpclmulqdq $0x01,%zmm29,%zmm6,%zmm26 - vpternlogd $0x96,%zmm26,%zmm25,%zmm24 - vpclmulqdq $0x01,%zmm30,%zmm7,%zmm25 - vpclmulqdq $0x10,%zmm27,%zmm4,%zmm26 - vpternlogd $0x96,%zmm26,%zmm25,%zmm24 - vpclmulqdq $0x10,%zmm28,%zmm5,%zmm25 - vpclmulqdq $0x10,%zmm29,%zmm6,%zmm26 - vpternlogd $0x96,%zmm26,%zmm25,%zmm24 - vpclmulqdq $0x01,%zmm10,%zmm31,%zmm26 - vpclmulqdq $0x10,%zmm30,%zmm7,%zmm25 - vpxord %zmm25,%zmm24,%zmm24 - vpshufd $0x4e,%zmm10,%zmm10 - vpclmulqdq $0x11,%zmm27,%zmm4,%zmm4 - vpclmulqdq $0x11,%zmm28,%zmm5,%zmm5 - vpclmulqdq $0x11,%zmm29,%zmm6,%zmm6 - vpternlogd $0x96,%zmm26,%zmm10,%zmm24 - vpclmulqdq $0x11,%zmm30,%zmm7,%zmm7 - vpternlogd $0x96,%zmm6,%zmm5,%zmm4 - vpclmulqdq $0x01,%zmm24,%zmm31,%zmm25 - vpxord %zmm7,%zmm4,%zmm10 - vpshufd $0x4e,%zmm24,%zmm24 - vpternlogd $0x96,%zmm25,%zmm24,%zmm10 + vpclmulqdq $0x00,%zmm27,%zmm4,%zmm10 // LO_0 + vpclmulqdq $0x00,%zmm28,%zmm5,%zmm24 // LO_1 + vpclmulqdq $0x00,%zmm29,%zmm6,%zmm25 // LO_2 + vpxord %zmm24,%zmm10,%zmm10 // sum(LO_{1,0}) + vpclmulqdq $0x00,%zmm30,%zmm7,%zmm26 // LO_3 + vpternlogd $0x96,%zmm26,%zmm25,%zmm10 // LO = sum(LO_{3,2,1,0}) + vpclmulqdq $0x01,%zmm27,%zmm4,%zmm24 // MI_0 + vpclmulqdq $0x01,%zmm28,%zmm5,%zmm25 // MI_1 + vpclmulqdq $0x01,%zmm29,%zmm6,%zmm26 // MI_2 + vpternlogd $0x96,%zmm26,%zmm25,%zmm24 // sum(MI_{2,1,0}) + vpclmulqdq $0x01,%zmm30,%zmm7,%zmm25 // MI_3 + vpclmulqdq $0x10,%zmm27,%zmm4,%zmm26 // MI_4 + vpternlogd $0x96,%zmm26,%zmm25,%zmm24 // sum(MI_{4,3,2,1,0}) + vpclmulqdq $0x10,%zmm28,%zmm5,%zmm25 // MI_5 + vpclmulqdq $0x10,%zmm29,%zmm6,%zmm26 // MI_6 + vpternlogd $0x96,%zmm26,%zmm25,%zmm24 // sum(MI_{6,5,4,3,2,1,0}) + vpclmulqdq $0x01,%zmm10,%zmm31,%zmm26 // LO_L*(x^63 + x^62 + x^57) + vpclmulqdq $0x10,%zmm30,%zmm7,%zmm25 // MI_7 + vpxord %zmm25,%zmm24,%zmm24 // MI = sum(MI_{7,6,5,4,3,2,1,0}) + vpshufd $0x4e,%zmm10,%zmm10 // Swap halves of LO + vpclmulqdq $0x11,%zmm27,%zmm4,%zmm4 // HI_0 + vpclmulqdq $0x11,%zmm28,%zmm5,%zmm5 // HI_1 + vpclmulqdq $0x11,%zmm29,%zmm6,%zmm6 // HI_2 + vpternlogd $0x96,%zmm26,%zmm10,%zmm24 // Fold LO into MI + vpclmulqdq $0x11,%zmm30,%zmm7,%zmm7 // HI_3 + vpternlogd $0x96,%zmm6,%zmm5,%zmm4 // sum(HI_{2,1,0}) + vpclmulqdq $0x01,%zmm24,%zmm31,%zmm25 // MI_L*(x^63 + x^62 + x^57) + vpxord %zmm7,%zmm4,%zmm10 // HI = sum(HI_{3,2,1,0}) + vpshufd $0x4e,%zmm24,%zmm24 // Swap halves of MI + vpternlogd $0x96,%zmm25,%zmm24,%zmm10 // Fold MI into HI vextracti32x4 $1,%zmm10,%xmm4 vextracti32x4 $2,%zmm10,%xmm5 vextracti32x4 $3,%zmm10,%xmm6 @@ -675,32 +677,32 @@ _CET_ENDBR vpternlogd $0x96,%xmm5,%xmm6,%xmm10 .Lcrypt_loop_4x_done__func1: - +// Check whether any data remains. testq %rdx,%rdx jz .Ldone__func1 - - - - - - - - - - - - - - - - - - - +// The data length isn't a multiple of 256 bytes. Process the remaining +// data of length 1 <= DATALEN < 256, up to one 64-byte vector at a time. +// Going one vector at a time may seem inefficient compared to having +// separate code paths for each possible number of vectors remaining. +// However, using a loop keeps the code size down, and it performs +// surprising well; modern CPUs will start executing the next iteration +// before the previous one finishes and also predict the number of loop +// iterations. For a similar reason, we roll up the AES rounds. +// +// On the last iteration, the remaining length may be less than 64 bytes. +// Handle this using masking. +// +// Since there are enough key powers available for all remaining data, +// there is no need to do a GHASH reduction after each iteration. +// Instead, multiply each remaining block by its own key power, and only +// do a GHASH reduction at the very end. + +// Make POWERS_PTR point to the key powers [H^N, H^(N-1), ...] where N +// is the number of blocks that remain. movq %rdx,%rax negq %rax - andq $-16,%rax + andq $-16,%rax // -round_up(DATALEN, 16) leaq 256(%r9,%rax,1),%r8 vpxor %xmm4,%xmm4,%xmm4 vpxor %xmm5,%xmm5,%xmm5 @@ -710,9 +712,9 @@ _CET_ENDBR jb .Lpartial_vec__func1 .Lcrypt_loop_1x__func1: +// Process a full 64-byte vector. - - +// Encrypt a vector of counter blocks. vpshufb %zmm8,%zmm12,%zmm0 vpaddd %zmm11,%zmm12,%zmm12 vpxord %zmm13,%zmm0,%zmm0 @@ -725,19 +727,19 @@ _CET_ENDBR jne .Lvaesenc_loop_tail_full_vec__func1 vaesenclast %zmm14,%zmm0,%zmm0 - +// XOR the data with the vector of keystream blocks. vmovdqu8 (%rdi),%zmm1 vpxord %zmm1,%zmm0,%zmm0 vmovdqu8 %zmm0,(%rsi) - +// Update GHASH with the ciphertext blocks, without reducing. vmovdqu8 (%r8),%zmm30 vpshufb %zmm8,%zmm0,%zmm0 vpxord %zmm10,%zmm0,%zmm0 - vpclmulqdq $0x00,%zmm30,%zmm0,%zmm7 - vpclmulqdq $0x01,%zmm30,%zmm0,%zmm1 - vpclmulqdq $0x10,%zmm30,%zmm0,%zmm2 - vpclmulqdq $0x11,%zmm30,%zmm0,%zmm3 + vpclmulqdq $0x00,%zmm30,%zmm0,%zmm7 // a_L * b_L + vpclmulqdq $0x01,%zmm30,%zmm0,%zmm1 // a_L * b_H + vpclmulqdq $0x10,%zmm30,%zmm0,%zmm2 // a_H * b_L + vpclmulqdq $0x11,%zmm30,%zmm0,%zmm3 // a_H * b_H vpxord %zmm7,%zmm4,%zmm4 vpternlogd $0x96,%zmm2,%zmm1,%zmm5 vpxord %zmm3,%zmm6,%zmm6 @@ -755,10 +757,10 @@ _CET_ENDBR jz .Lreduce__func1 .Lpartial_vec__func1: +// Process a partial vector of length 1 <= DATALEN < 64. - - - +// Set the data mask %k1 to DATALEN 1's. +// Set the key powers mask %k2 to round_up(DATALEN, 16) 1's. movq $-1,%rax bzhiq %rdx,%rax,%rax kmovq %rax,%k1 @@ -768,8 +770,8 @@ _CET_ENDBR bzhiq %rdx,%rax,%rax kmovq %rax,%k2 - - +// Encrypt one last vector of counter blocks. This does not need to be +// masked. The counter does not need to be incremented here. vpshufb %zmm8,%zmm12,%zmm0 vpxord %zmm13,%zmm0,%zmm0 leaq 16(%rcx),%rax @@ -781,38 +783,38 @@ _CET_ENDBR jne .Lvaesenc_loop_tail_partialvec__func1 vaesenclast %zmm14,%zmm0,%zmm0 - +// XOR the data with the appropriate number of keystream bytes. vmovdqu8 (%rdi),%zmm1{%k1}{z} vpxord %zmm1,%zmm0,%zmm0 vmovdqu8 %zmm0,(%rsi){%k1} - - - - - - - - - - - - +// Update GHASH with the ciphertext block(s), without reducing. +// +// In the case of DATALEN < 64, the ciphertext is zero-padded to 64 +// bytes. (If decrypting, it's done by the above masked load. If +// encrypting, it's done by the below masked register-to-register move.) +// Note that if DATALEN <= 48, there will be additional padding beyond +// the padding of the last block specified by GHASH itself; i.e., there +// may be whole block(s) that get processed by the GHASH multiplication +// and reduction instructions but should not actually be included in the +// GHASH. However, any such blocks are all-zeroes, and the values that +// they're multiplied with are also all-zeroes. Therefore they just add +// 0 * 0 = 0 to the final GHASH result, which makes no difference. vmovdqu8 (%r8),%zmm30{%k2}{z} vmovdqu8 %zmm0,%zmm1{%k1}{z} vpshufb %zmm8,%zmm1,%zmm0 vpxord %zmm10,%zmm0,%zmm0 - vpclmulqdq $0x00,%zmm30,%zmm0,%zmm7 - vpclmulqdq $0x01,%zmm30,%zmm0,%zmm1 - vpclmulqdq $0x10,%zmm30,%zmm0,%zmm2 - vpclmulqdq $0x11,%zmm30,%zmm0,%zmm3 + vpclmulqdq $0x00,%zmm30,%zmm0,%zmm7 // a_L * b_L + vpclmulqdq $0x01,%zmm30,%zmm0,%zmm1 // a_L * b_H + vpclmulqdq $0x10,%zmm30,%zmm0,%zmm2 // a_H * b_L + vpclmulqdq $0x11,%zmm30,%zmm0,%zmm3 // a_H * b_H vpxord %zmm7,%zmm4,%zmm4 vpternlogd $0x96,%zmm2,%zmm1,%zmm5 vpxord %zmm3,%zmm6,%zmm6 .Lreduce__func1: - +// Finally, do the GHASH reduction. vpclmulqdq $0x01,%zmm4,%zmm31,%zmm0 vpshufd $0x4e,%zmm4,%zmm4 vpternlogd $0x96,%zmm0,%zmm4,%zmm5 @@ -828,11 +830,11 @@ _CET_ENDBR .Ldone__func1: - +// Store the updated GHASH accumulator back to memory. vpshufb %xmm8,%xmm10,%xmm10 vmovdqu %xmm10,(%r12) - vzeroupper + vzeroupper // This is needed after using ymm or zmm registers. popq %r12 .cfi_adjust_cfa_offset -8 .cfi_restore %r12 @@ -852,47 +854,47 @@ _CET_ENDBR .cfi_adjust_cfa_offset 8 .cfi_offset %r12,-16 - movq 16(%rsp),%r12 - + movq 16(%rsp),%r12 // arg7 +// Load some constants. vbroadcasti32x4 .Lbswap_mask(%rip),%zmm8 vbroadcasti32x4 .Lgfpoly(%rip),%zmm31 - - +// Load the GHASH accumulator and the starting counter. +// BoringSSL passes these values in big endian format. vmovdqu (%r12),%xmm10 vpshufb %xmm8,%xmm10,%xmm10 vbroadcasti32x4 (%r8),%zmm12 vpshufb %zmm8,%zmm12,%zmm12 - - +// Load the AES key length in bytes. BoringSSL stores number of rounds +// minus 1, so convert using: AESKEYLEN = 4 * aeskey->rounds - 20. movl 240(%rcx),%r10d leal -20(,%r10,4),%r10d - - - +// Make RNDKEYLAST_PTR point to the last AES round key. This is the +// round key with index 10, 12, or 14 for AES-128, AES-192, or AES-256 +// respectively. Then load the zero-th and last round keys. leaq 96(%rcx,%r10,4),%r11 vbroadcasti32x4 (%rcx),%zmm13 vbroadcasti32x4 (%r11),%zmm14 - +// Finish initializing LE_CTR by adding [0, 1, 2, 3] to its low words. vpaddd .Lctr_pattern(%rip),%zmm12,%zmm12 - +// Load 4 into all 128-bit lanes of LE_CTR_INC. vbroadcasti32x4 .Linc_4blocks(%rip),%zmm11 - - +// If there are at least 256 bytes of data, then continue into the loop +// that processes 256 bytes of data at a time. Otherwise skip it. cmpq $256,%rdx jb .Lcrypt_loop_4x_done__func2 - +// Load powers of the hash key. vmovdqu8 256-256(%r9),%zmm27 vmovdqu8 256-192(%r9),%zmm28 vmovdqu8 256-128(%r9),%zmm29 vmovdqu8 256-64(%r9),%zmm30 - +// Cache as many additional AES round keys as possible. vbroadcasti32x4 -144(%r11),%zmm15 vbroadcasti32x4 -128(%r11),%zmm16 vbroadcasti32x4 -112(%r11),%zmm17 @@ -908,9 +910,9 @@ _CET_ENDBR vmovdqu8 64(%rdi),%zmm5 vmovdqu8 128(%rdi),%zmm6 vmovdqu8 192(%rdi),%zmm7 - - - +// Start the AES encryption of the counter blocks. +// Increment le_ctr four times to generate four vectors of little-endian +// counter blocks, swap each to big-endian, and store them in aesdata[0-3]. vpshufb %zmm8,%zmm12,%zmm0 vpaddd %zmm11,%zmm12,%zmm12 vpshufb %zmm8,%zmm12,%zmm1 @@ -920,7 +922,7 @@ _CET_ENDBR vpshufb %zmm8,%zmm12,%zmm3 vpaddd %zmm11,%zmm12,%zmm12 - +// AES "round zero": XOR in the zero-th round key. vpxord %zmm13,%zmm0,%zmm0 vpxord %zmm13,%zmm1,%zmm1 vpxord %zmm13,%zmm2,%zmm2 @@ -929,7 +931,7 @@ _CET_ENDBR cmpl $24,%r10d jl .Laes128__func2 je .Laes192__func2 - +// AES-256 vbroadcasti32x4 -208(%r11),%zmm9 vaesenc %zmm9,%zmm0,%zmm0 vaesenc %zmm9,%zmm1,%zmm1 @@ -957,17 +959,17 @@ _CET_ENDBR .Laes128__func2: - - - +// Prefetch the source data 512 bytes ahead into the L1 data cache, to +// improve performance when the hardware prefetcher is disabled. Assumes the +// L1 data cache line size is 64 bytes (de facto standard on x86_64). prefetcht0 512+0(%rdi) prefetcht0 512+64(%rdi) prefetcht0 512+128(%rdi) prefetcht0 512+192(%rdi) - - - +// Finish the AES encryption of the counter blocks in AESDATA[0-3], +// interleaved with the GHASH update of the ciphertext blocks in +// GHASHDATA[0-3]. vpshufb %zmm8,%zmm4,%zmm4 vpxord %zmm10,%zmm4,%zmm4 vpshufb %zmm8,%zmm5,%zmm5 @@ -979,78 +981,78 @@ _CET_ENDBR vaesenc %zmm15,%zmm3,%zmm3 vpshufb %zmm8,%zmm7,%zmm7 - vpclmulqdq $0x00,%zmm27,%zmm4,%zmm10 - vpclmulqdq $0x00,%zmm28,%zmm5,%zmm24 - vpclmulqdq $0x00,%zmm29,%zmm6,%zmm25 + vpclmulqdq $0x00,%zmm27,%zmm4,%zmm10 // LO_0 + vpclmulqdq $0x00,%zmm28,%zmm5,%zmm24 // LO_1 + vpclmulqdq $0x00,%zmm29,%zmm6,%zmm25 // LO_2 vaesenc %zmm16,%zmm0,%zmm0 vaesenc %zmm16,%zmm1,%zmm1 vaesenc %zmm16,%zmm2,%zmm2 vaesenc %zmm16,%zmm3,%zmm3 - vpxord %zmm24,%zmm10,%zmm10 - vpclmulqdq $0x00,%zmm30,%zmm7,%zmm26 - vpternlogd $0x96,%zmm26,%zmm25,%zmm10 - vpclmulqdq $0x01,%zmm27,%zmm4,%zmm24 + vpxord %zmm24,%zmm10,%zmm10 // sum(LO_{1,0}) + vpclmulqdq $0x00,%zmm30,%zmm7,%zmm26 // LO_3 + vpternlogd $0x96,%zmm26,%zmm25,%zmm10 // LO = sum(LO_{3,2,1,0}) + vpclmulqdq $0x01,%zmm27,%zmm4,%zmm24 // MI_0 vaesenc %zmm17,%zmm0,%zmm0 vaesenc %zmm17,%zmm1,%zmm1 vaesenc %zmm17,%zmm2,%zmm2 vaesenc %zmm17,%zmm3,%zmm3 - vpclmulqdq $0x01,%zmm28,%zmm5,%zmm25 - vpclmulqdq $0x01,%zmm29,%zmm6,%zmm26 - vpternlogd $0x96,%zmm26,%zmm25,%zmm24 - vpclmulqdq $0x01,%zmm30,%zmm7,%zmm25 + vpclmulqdq $0x01,%zmm28,%zmm5,%zmm25 // MI_1 + vpclmulqdq $0x01,%zmm29,%zmm6,%zmm26 // MI_2 + vpternlogd $0x96,%zmm26,%zmm25,%zmm24 // sum(MI_{2,1,0}) + vpclmulqdq $0x01,%zmm30,%zmm7,%zmm25 // MI_3 vaesenc %zmm18,%zmm0,%zmm0 vaesenc %zmm18,%zmm1,%zmm1 vaesenc %zmm18,%zmm2,%zmm2 vaesenc %zmm18,%zmm3,%zmm3 - vpclmulqdq $0x10,%zmm27,%zmm4,%zmm26 - vpternlogd $0x96,%zmm26,%zmm25,%zmm24 - vpclmulqdq $0x10,%zmm28,%zmm5,%zmm25 - vpclmulqdq $0x10,%zmm29,%zmm6,%zmm26 + vpclmulqdq $0x10,%zmm27,%zmm4,%zmm26 // MI_4 + vpternlogd $0x96,%zmm26,%zmm25,%zmm24 // sum(MI_{4,3,2,1,0}) + vpclmulqdq $0x10,%zmm28,%zmm5,%zmm25 // MI_5 + vpclmulqdq $0x10,%zmm29,%zmm6,%zmm26 // MI_6 vaesenc %zmm19,%zmm0,%zmm0 vaesenc %zmm19,%zmm1,%zmm1 vaesenc %zmm19,%zmm2,%zmm2 vaesenc %zmm19,%zmm3,%zmm3 - vpternlogd $0x96,%zmm26,%zmm25,%zmm24 - vpclmulqdq $0x01,%zmm10,%zmm31,%zmm26 - vpclmulqdq $0x10,%zmm30,%zmm7,%zmm25 - vpxord %zmm25,%zmm24,%zmm24 + vpternlogd $0x96,%zmm26,%zmm25,%zmm24 // sum(MI_{6,5,4,3,2,1,0}) + vpclmulqdq $0x01,%zmm10,%zmm31,%zmm26 // LO_L*(x^63 + x^62 + x^57) + vpclmulqdq $0x10,%zmm30,%zmm7,%zmm25 // MI_7 + vpxord %zmm25,%zmm24,%zmm24 // MI = sum(MI_{7,6,5,4,3,2,1,0}) vaesenc %zmm20,%zmm0,%zmm0 vaesenc %zmm20,%zmm1,%zmm1 vaesenc %zmm20,%zmm2,%zmm2 vaesenc %zmm20,%zmm3,%zmm3 - vpshufd $0x4e,%zmm10,%zmm10 - vpclmulqdq $0x11,%zmm27,%zmm4,%zmm4 - vpclmulqdq $0x11,%zmm28,%zmm5,%zmm5 - vpclmulqdq $0x11,%zmm29,%zmm6,%zmm6 + vpshufd $0x4e,%zmm10,%zmm10 // Swap halves of LO + vpclmulqdq $0x11,%zmm27,%zmm4,%zmm4 // HI_0 + vpclmulqdq $0x11,%zmm28,%zmm5,%zmm5 // HI_1 + vpclmulqdq $0x11,%zmm29,%zmm6,%zmm6 // HI_2 vaesenc %zmm21,%zmm0,%zmm0 vaesenc %zmm21,%zmm1,%zmm1 vaesenc %zmm21,%zmm2,%zmm2 vaesenc %zmm21,%zmm3,%zmm3 - vpternlogd $0x96,%zmm26,%zmm10,%zmm24 - vpclmulqdq $0x11,%zmm30,%zmm7,%zmm7 - vpternlogd $0x96,%zmm6,%zmm5,%zmm4 - vpclmulqdq $0x01,%zmm24,%zmm31,%zmm25 + vpternlogd $0x96,%zmm26,%zmm10,%zmm24 // Fold LO into MI + vpclmulqdq $0x11,%zmm30,%zmm7,%zmm7 // HI_3 + vpternlogd $0x96,%zmm6,%zmm5,%zmm4 // sum(HI_{2,1,0}) + vpclmulqdq $0x01,%zmm24,%zmm31,%zmm25 // MI_L*(x^63 + x^62 + x^57) vaesenc %zmm22,%zmm0,%zmm0 vaesenc %zmm22,%zmm1,%zmm1 vaesenc %zmm22,%zmm2,%zmm2 vaesenc %zmm22,%zmm3,%zmm3 - vpxord %zmm7,%zmm4,%zmm10 - vpshufd $0x4e,%zmm24,%zmm24 - vpternlogd $0x96,%zmm25,%zmm24,%zmm10 + vpxord %zmm7,%zmm4,%zmm10 // HI = sum(HI_{3,2,1,0}) + vpshufd $0x4e,%zmm24,%zmm24 // Swap halves of MI + vpternlogd $0x96,%zmm25,%zmm24,%zmm10 // Fold MI into HI vaesenc %zmm23,%zmm0,%zmm0 vaesenc %zmm23,%zmm1,%zmm1 @@ -1083,32 +1085,32 @@ _CET_ENDBR cmpq $256,%rdx jae .Lcrypt_loop_4x__func2 .Lcrypt_loop_4x_done__func2: - +// Check whether any data remains. testq %rdx,%rdx jz .Ldone__func2 - - - - - - - - - - - - - - - - - - - +// The data length isn't a multiple of 256 bytes. Process the remaining +// data of length 1 <= DATALEN < 256, up to one 64-byte vector at a time. +// Going one vector at a time may seem inefficient compared to having +// separate code paths for each possible number of vectors remaining. +// However, using a loop keeps the code size down, and it performs +// surprising well; modern CPUs will start executing the next iteration +// before the previous one finishes and also predict the number of loop +// iterations. For a similar reason, we roll up the AES rounds. +// +// On the last iteration, the remaining length may be less than 64 bytes. +// Handle this using masking. +// +// Since there are enough key powers available for all remaining data, +// there is no need to do a GHASH reduction after each iteration. +// Instead, multiply each remaining block by its own key power, and only +// do a GHASH reduction at the very end. + +// Make POWERS_PTR point to the key powers [H^N, H^(N-1), ...] where N +// is the number of blocks that remain. movq %rdx,%rax negq %rax - andq $-16,%rax + andq $-16,%rax // -round_up(DATALEN, 16) leaq 256(%r9,%rax,1),%r8 vpxor %xmm4,%xmm4,%xmm4 vpxor %xmm5,%xmm5,%xmm5 @@ -1118,9 +1120,9 @@ _CET_ENDBR jb .Lpartial_vec__func2 .Lcrypt_loop_1x__func2: +// Process a full 64-byte vector. - - +// Encrypt a vector of counter blocks. vpshufb %zmm8,%zmm12,%zmm0 vpaddd %zmm11,%zmm12,%zmm12 vpxord %zmm13,%zmm0,%zmm0 @@ -1133,19 +1135,19 @@ _CET_ENDBR jne .Lvaesenc_loop_tail_full_vec__func2 vaesenclast %zmm14,%zmm0,%zmm0 - +// XOR the data with the vector of keystream blocks. vmovdqu8 (%rdi),%zmm1 vpxord %zmm1,%zmm0,%zmm0 vmovdqu8 %zmm0,(%rsi) - +// Update GHASH with the ciphertext blocks, without reducing. vmovdqu8 (%r8),%zmm30 vpshufb %zmm8,%zmm1,%zmm0 vpxord %zmm10,%zmm0,%zmm0 - vpclmulqdq $0x00,%zmm30,%zmm0,%zmm7 - vpclmulqdq $0x01,%zmm30,%zmm0,%zmm1 - vpclmulqdq $0x10,%zmm30,%zmm0,%zmm2 - vpclmulqdq $0x11,%zmm30,%zmm0,%zmm3 + vpclmulqdq $0x00,%zmm30,%zmm0,%zmm7 // a_L * b_L + vpclmulqdq $0x01,%zmm30,%zmm0,%zmm1 // a_L * b_H + vpclmulqdq $0x10,%zmm30,%zmm0,%zmm2 // a_H * b_L + vpclmulqdq $0x11,%zmm30,%zmm0,%zmm3 // a_H * b_H vpxord %zmm7,%zmm4,%zmm4 vpternlogd $0x96,%zmm2,%zmm1,%zmm5 vpxord %zmm3,%zmm6,%zmm6 @@ -1163,10 +1165,10 @@ _CET_ENDBR jz .Lreduce__func2 .Lpartial_vec__func2: +// Process a partial vector of length 1 <= DATALEN < 64. - - - +// Set the data mask %k1 to DATALEN 1's. +// Set the key powers mask %k2 to round_up(DATALEN, 16) 1's. movq $-1,%rax bzhiq %rdx,%rax,%rax kmovq %rax,%k1 @@ -1176,8 +1178,8 @@ _CET_ENDBR bzhiq %rdx,%rax,%rax kmovq %rax,%k2 - - +// Encrypt one last vector of counter blocks. This does not need to be +// masked. The counter does not need to be incremented here. vpshufb %zmm8,%zmm12,%zmm0 vpxord %zmm13,%zmm0,%zmm0 leaq 16(%rcx),%rax @@ -1189,38 +1191,38 @@ _CET_ENDBR jne .Lvaesenc_loop_tail_partialvec__func2 vaesenclast %zmm14,%zmm0,%zmm0 - +// XOR the data with the appropriate number of keystream bytes. vmovdqu8 (%rdi),%zmm1{%k1}{z} vpxord %zmm1,%zmm0,%zmm0 vmovdqu8 %zmm0,(%rsi){%k1} - - - - - - - - - - - - +// Update GHASH with the ciphertext block(s), without reducing. +// +// In the case of DATALEN < 64, the ciphertext is zero-padded to 64 +// bytes. (If decrypting, it's done by the above masked load. If +// encrypting, it's done by the below masked register-to-register move.) +// Note that if DATALEN <= 48, there will be additional padding beyond +// the padding of the last block specified by GHASH itself; i.e., there +// may be whole block(s) that get processed by the GHASH multiplication +// and reduction instructions but should not actually be included in the +// GHASH. However, any such blocks are all-zeroes, and the values that +// they're multiplied with are also all-zeroes. Therefore they just add +// 0 * 0 = 0 to the final GHASH result, which makes no difference. vmovdqu8 (%r8),%zmm30{%k2}{z} vpshufb %zmm8,%zmm1,%zmm0 vpxord %zmm10,%zmm0,%zmm0 - vpclmulqdq $0x00,%zmm30,%zmm0,%zmm7 - vpclmulqdq $0x01,%zmm30,%zmm0,%zmm1 - vpclmulqdq $0x10,%zmm30,%zmm0,%zmm2 - vpclmulqdq $0x11,%zmm30,%zmm0,%zmm3 + vpclmulqdq $0x00,%zmm30,%zmm0,%zmm7 // a_L * b_L + vpclmulqdq $0x01,%zmm30,%zmm0,%zmm1 // a_L * b_H + vpclmulqdq $0x10,%zmm30,%zmm0,%zmm2 // a_H * b_L + vpclmulqdq $0x11,%zmm30,%zmm0,%zmm3 // a_H * b_H vpxord %zmm7,%zmm4,%zmm4 vpternlogd $0x96,%zmm2,%zmm1,%zmm5 vpxord %zmm3,%zmm6,%zmm6 .Lreduce__func2: - +// Finally, do the GHASH reduction. vpclmulqdq $0x01,%zmm4,%zmm31,%zmm0 vpshufd $0x4e,%zmm4,%zmm4 vpternlogd $0x96,%zmm0,%zmm4,%zmm5 @@ -1236,11 +1238,11 @@ _CET_ENDBR .Ldone__func2: - +// Store the updated GHASH accumulator back to memory. vpshufb %xmm8,%xmm10,%xmm10 vmovdqu %xmm10,(%r12) - vzeroupper + vzeroupper // This is needed after using ymm or zmm registers. popq %r12 .cfi_adjust_cfa_offset -8 .cfi_restore %r12 diff --git a/third_party/boringssl/gen/bcm/aes-gcm-avx512-x86_64-win.asm b/third_party/boringssl/gen/bcm/aes-gcm-avx512-x86_64-win.asm index 34f4a4f6b..17e44e618 100644 --- a/third_party/boringssl/gen/bcm/aes-gcm-avx512-x86_64-win.asm +++ b/third_party/boringssl/gen/bcm/aes-gcm-avx512-x86_64-win.asm @@ -12,34 +12,36 @@ default rel %include "boringssl_prefix_symbols_internal_x86_64_win_asm.inc" %endif section .rdata rdata align=8 -ALIGN 64 +ALIGN 64 +aes_gcm_avx512_constants: +; A shuffle mask that reflects the bytes of 16-byte blocks $L$bswap_mask: DQ 0x08090a0b0c0d0e0f,0x0001020304050607 - - - - - - - +; This is the GHASH reducing polynomial without its constant term, i.e. +; x^128 + x^7 + x^2 + x, represented using the backwards mapping +; between bits and polynomial coefficients. +; +; Alternatively, it can be interpreted as the naturally-ordered +; representation of the polynomial x^127 + x^126 + x^121 + 1, i.e. the +; "reversed" GHASH reducing polynomial without its x^128 term. $L$gfpoly: DQ 1,0xc200000000000000 - +; Same as above, but with the (1 << 64) bit set. $L$gfpoly_and_internal_carrybit: DQ 1,0xc200000000000001 - +; Values needed to prepare the initial vector of counter blocks. $L$ctr_pattern: DQ 0,0 DQ 1,0 DQ 2,0 DQ 3,0 - +; The number of AES blocks per vector, as a 128-bit value. $L$inc_4blocks: DQ 4,0 @@ -52,100 +54,100 @@ gcm_init_vpclmulqdq_avx512: _CET_ENDBR - +; Get pointer to lowest set of key powers (located at end of array). lea r8,[((256-64))+rcx] - - +; Load the byte-reflected hash subkey. BoringSSL provides it in +; byte-reflected form except the two halves are in the wrong order. vpshufd xmm3,XMMWORD[rdx],0x4e - - - - - - - - - - - - - - - +; Finish preprocessing the first key power, H^1. Since this GHASH +; implementation operates directly on values with the backwards bit +; order specified by the GCM standard, it's necessary to preprocess the +; raw key as follows. First, reflect its bytes. Second, multiply it +; by x^-1 mod x^128 + x^7 + x^2 + x + 1 (if using the backwards +; interpretation of polynomial coefficients), which can also be +; interpreted as multiplication by x mod x^128 + x^127 + x^126 + x^121 +; + 1 using the alternative, natural interpretation of polynomial +; coefficients. For details, see the comment above _ghash_mul. +; +; Either way, for the multiplication the concrete operation performed +; is a left shift of the 128-bit value by 1 bit, then an XOR with (0xc2 +; << 120) | 1 if a 1 bit was carried out. However, there's no 128-bit +; wide shift instruction, so instead double each of the two 64-bit +; halves and incorporate the internal carry bit into the value XOR'd. vpshufd xmm0,xmm3,0xd3 vpsrad xmm0,xmm0,31 vpaddq xmm3,xmm3,xmm3 - +; H_CUR_XMM ^= TMP0_XMM & gfpoly_and_internal_carrybit vpternlogd xmm3,xmm0,XMMWORD[$L$gfpoly_and_internal_carrybit],0x78 - +; Load the gfpoly constant. vbroadcasti32x4 zmm5,ZMMWORD[$L$gfpoly] - - - - - - - - vpclmulqdq xmm0,xmm3,xmm3,0x00 - vpclmulqdq xmm4,xmm3,xmm3,0x11 - vpclmulqdq xmm1,xmm5,xmm0,0x01 - vpshufd xmm0,xmm0,0x4e - vpxor xmm1,xmm1,xmm0 - vpclmulqdq xmm0,xmm5,xmm1,0x01 - vpshufd xmm1,xmm1,0x4e - vpternlogd xmm4,xmm1,xmm0,0x96 - - - +; Square H^1 to get H^2. +; +; Note that as with H^1, all higher key powers also need an extra +; factor of x^-1 (or x using the natural interpretation). Nothing +; special needs to be done to make this happen, though: H^1 * H^1 would +; end up with two factors of x^-1, but the multiplication consumes one. +; So the product H^2 ends up with the desired one factor of x^-1. + vpclmulqdq xmm0,xmm3,xmm3,0x00 ; LO = a_L * a_L + vpclmulqdq xmm4,xmm3,xmm3,0x11 ; HI = a_H * a_H + vpclmulqdq xmm1,xmm5,xmm0,0x01 ; LO_L*(x^63 + x^62 + x^57) + vpshufd xmm0,xmm0,0x4e ; Swap halves of LO + vpxor xmm1,xmm1,xmm0 ; Fold LO into MI + vpclmulqdq xmm0,xmm5,xmm1,0x01 ; MI_L*(x^63 + x^62 + x^57) + vpshufd xmm1,xmm1,0x4e ; Swap halves of MI + vpternlogd xmm4,xmm1,xmm0,0x96 ; Fold MI into HI + + +; Create H_CUR_YMM = [H^2, H^1] and H_INC_YMM = [H^2, H^2]. vinserti128 ymm3,ymm4,xmm3,1 vinserti128 ymm4,ymm4,xmm4,1 - - vpclmulqdq ymm0,ymm3,ymm4,0x00 - vpclmulqdq ymm1,ymm3,ymm4,0x01 - vpclmulqdq ymm2,ymm3,ymm4,0x10 - vpxord ymm1,ymm1,ymm2 - vpclmulqdq ymm2,ymm5,ymm0,0x01 - vpshufd ymm0,ymm0,0x4e - vpternlogd ymm1,ymm0,ymm2,0x96 - vpclmulqdq ymm4,ymm3,ymm4,0x11 - vpclmulqdq ymm0,ymm5,ymm1,0x01 - vpshufd ymm1,ymm1,0x4e - vpternlogd ymm4,ymm1,ymm0,0x96 +; Create H_CUR = [H^4, H^3, H^2, H^1] and H_INC = [H^4, H^4, H^4, H^4]. + vpclmulqdq ymm0,ymm3,ymm4,0x00 ; LO = a_L * b_L + vpclmulqdq ymm1,ymm3,ymm4,0x01 ; MI_0 = a_L * b_H + vpclmulqdq ymm2,ymm3,ymm4,0x10 ; MI_1 = a_H * b_L + vpxord ymm1,ymm1,ymm2 ; MI = MI_0 + MI_1 + vpclmulqdq ymm2,ymm5,ymm0,0x01 ; LO_L*(x^63 + x^62 + x^57) + vpshufd ymm0,ymm0,0x4e ; Swap halves of LO + vpternlogd ymm1,ymm0,ymm2,0x96 ; Fold LO into MI + vpclmulqdq ymm4,ymm3,ymm4,0x11 ; HI = a_H * b_H + vpclmulqdq ymm0,ymm5,ymm1,0x01 ; MI_L*(x^63 + x^62 + x^57) + vpshufd ymm1,ymm1,0x4e ; Swap halves of MI + vpternlogd ymm4,ymm1,ymm0,0x96 ; Fold MI into HI vinserti64x4 zmm3,zmm4,ymm3,1 vshufi64x2 zmm4,zmm4,zmm4,0 - +; Store the lowest set of key powers. vmovdqu8 ZMMWORD[r8],zmm3 - - - +; Compute and store the remaining key powers. +; Repeatedly multiply [H^(i+3), H^(i+2), H^(i+1), H^i] by +; [H^4, H^4, H^4, H^4] to get [H^(i+7), H^(i+6), H^(i+5), H^(i+4)]. mov eax,3 $L$precompute_next: sub r8,64 - vpclmulqdq zmm0,zmm3,zmm4,0x00 - vpclmulqdq zmm1,zmm3,zmm4,0x01 - vpclmulqdq zmm2,zmm3,zmm4,0x10 - vpxord zmm1,zmm1,zmm2 - vpclmulqdq zmm2,zmm5,zmm0,0x01 - vpshufd zmm0,zmm0,0x4e - vpternlogd zmm1,zmm0,zmm2,0x96 - vpclmulqdq zmm3,zmm3,zmm4,0x11 - vpclmulqdq zmm0,zmm5,zmm1,0x01 - vpshufd zmm1,zmm1,0x4e - vpternlogd zmm3,zmm1,zmm0,0x96 + vpclmulqdq zmm0,zmm3,zmm4,0x00 ; LO = a_L * b_L + vpclmulqdq zmm1,zmm3,zmm4,0x01 ; MI_0 = a_L * b_H + vpclmulqdq zmm2,zmm3,zmm4,0x10 ; MI_1 = a_H * b_L + vpxord zmm1,zmm1,zmm2 ; MI = MI_0 + MI_1 + vpclmulqdq zmm2,zmm5,zmm0,0x01 ; LO_L*(x^63 + x^62 + x^57) + vpshufd zmm0,zmm0,0x4e ; Swap halves of LO + vpternlogd zmm1,zmm0,zmm2,0x96 ; Fold LO into MI + vpclmulqdq zmm3,zmm3,zmm4,0x11 ; HI = a_H * b_H + vpclmulqdq zmm0,zmm5,zmm1,0x01 ; MI_L*(x^63 + x^62 + x^57) + vpshufd zmm1,zmm1,0x4e ; Swap halves of MI + vpternlogd zmm3,zmm1,zmm0,0x96 ; Fold MI into HI vmovdqu8 ZMMWORD[r8],zmm3 dec eax jnz NEAR $L$precompute_next - vzeroupper + vzeroupper ; This is needed after using ymm or zmm registers. ret @@ -170,23 +172,23 @@ $L$SEH_endprologue_gcm_gmult_vpclmulqdq_avx512_4: vmovdqu xmm3,XMMWORD[$L$gfpoly] vpshufb xmm0,xmm0,xmm1 - vpclmulqdq xmm4,xmm0,xmm2,0x00 - vpclmulqdq xmm5,xmm0,xmm2,0x01 - vpclmulqdq xmm6,xmm0,xmm2,0x10 - vpxord xmm5,xmm5,xmm6 - vpclmulqdq xmm6,xmm3,xmm4,0x01 - vpshufd xmm4,xmm4,0x4e - vpternlogd xmm5,xmm4,xmm6,0x96 - vpclmulqdq xmm0,xmm0,xmm2,0x11 - vpclmulqdq xmm4,xmm3,xmm5,0x01 - vpshufd xmm5,xmm5,0x4e - vpternlogd xmm0,xmm5,xmm4,0x96 + vpclmulqdq xmm4,xmm0,xmm2,0x00 ; LO = a_L * b_L + vpclmulqdq xmm5,xmm0,xmm2,0x01 ; MI_0 = a_L * b_H + vpclmulqdq xmm6,xmm0,xmm2,0x10 ; MI_1 = a_H * b_L + vpxord xmm5,xmm5,xmm6 ; MI = MI_0 + MI_1 + vpclmulqdq xmm6,xmm3,xmm4,0x01 ; LO_L*(x^63 + x^62 + x^57) + vpshufd xmm4,xmm4,0x4e ; Swap halves of LO + vpternlogd xmm5,xmm4,xmm6,0x96 ; Fold LO into MI + vpclmulqdq xmm0,xmm0,xmm2,0x11 ; HI = a_H * b_H + vpclmulqdq xmm4,xmm3,xmm5,0x01 ; MI_L*(x^63 + x^62 + x^57) + vpshufd xmm5,xmm5,0x4e ; Swap halves of MI + vpternlogd xmm0,xmm5,xmm4,0x96 ; Fold MI into HI vpshufb xmm0,xmm0,xmm1 vmovdqu XMMWORD[rcx],xmm0 - +; No need for vzeroupper, since only xmm registers were used. vmovdqa xmm6,XMMWORD[rsp] add rsp,24 ret @@ -221,37 +223,37 @@ $L$SEH_prologue_gcm_ghash_vpclmulqdq_avx512_10: $L$SEH_endprologue_gcm_ghash_vpclmulqdq_avx512_11: - - - +; Load the bswap_mask and gfpoly constants. Since AADLEN is usually small, +; usually only 128-bit vectors will be used. So as an optimization, don't +; broadcast these constants to all 128-bit lanes quite yet. vmovdqu xmm4,XMMWORD[$L$bswap_mask] vmovdqu xmm10,XMMWORD[$L$gfpoly] - +; Load the GHASH accumulator. vmovdqu xmm5,XMMWORD[rcx] vpshufb xmm5,xmm5,xmm4 - +; Optimize for AADLEN < 64 by checking for AADLEN < 64 before AADLEN < 256. cmp r9,64 jb NEAR $L$aad_blockbyblock - - +; AADLEN >= 64, so we'll operate on full vectors. Broadcast bswap_mask and +; gfpoly to all 128-bit lanes. vshufi64x2 zmm4,zmm4,zmm4,0 vshufi64x2 zmm10,zmm10,zmm10,0 - +; Load the lowest set of key powers. vmovdqu8 zmm9,ZMMWORD[((256-64))+rdx] cmp r9,256 jb NEAR $L$aad_loop_1x - +; AADLEN >= 256. Load the higher key powers. vmovdqu8 zmm6,ZMMWORD[((256-256))+rdx] vmovdqu8 zmm7,ZMMWORD[((256-192))+rdx] vmovdqu8 zmm8,ZMMWORD[((256-128))+rdx] - +; Update GHASH with 256 bytes of AAD at a time. $L$aad_loop_4x: vmovdqu8 zmm0,ZMMWORD[r8] vmovdqu8 zmm1,ZMMWORD[64+r8] @@ -262,36 +264,36 @@ $L$aad_loop_4x: vpshufb zmm1,zmm1,zmm4 vpshufb zmm2,zmm2,zmm4 vpshufb zmm3,zmm3,zmm4 - vpclmulqdq zmm5,zmm0,zmm6,0x00 - vpclmulqdq zmm11,zmm1,zmm7,0x00 - vpclmulqdq zmm12,zmm2,zmm8,0x00 - vpxord zmm5,zmm5,zmm11 - vpclmulqdq zmm13,zmm3,zmm9,0x00 - vpternlogd zmm5,zmm12,zmm13,0x96 - vpclmulqdq zmm11,zmm0,zmm6,0x01 - vpclmulqdq zmm12,zmm1,zmm7,0x01 - vpclmulqdq zmm13,zmm2,zmm8,0x01 - vpternlogd zmm11,zmm12,zmm13,0x96 - vpclmulqdq zmm12,zmm3,zmm9,0x01 - vpclmulqdq zmm13,zmm0,zmm6,0x10 - vpternlogd zmm11,zmm12,zmm13,0x96 - vpclmulqdq zmm12,zmm1,zmm7,0x10 - vpclmulqdq zmm13,zmm2,zmm8,0x10 - vpternlogd zmm11,zmm12,zmm13,0x96 - vpclmulqdq zmm13,zmm10,zmm5,0x01 - vpclmulqdq zmm12,zmm3,zmm9,0x10 - vpxord zmm11,zmm11,zmm12 - vpshufd zmm5,zmm5,0x4e - vpclmulqdq zmm0,zmm0,zmm6,0x11 - vpclmulqdq zmm1,zmm1,zmm7,0x11 - vpclmulqdq zmm2,zmm2,zmm8,0x11 - vpternlogd zmm11,zmm5,zmm13,0x96 - vpclmulqdq zmm3,zmm3,zmm9,0x11 - vpternlogd zmm0,zmm1,zmm2,0x96 - vpclmulqdq zmm12,zmm10,zmm11,0x01 - vpxord zmm5,zmm0,zmm3 - vpshufd zmm11,zmm11,0x4e - vpternlogd zmm5,zmm11,zmm12,0x96 + vpclmulqdq zmm5,zmm0,zmm6,0x00 ; LO_0 + vpclmulqdq zmm11,zmm1,zmm7,0x00 ; LO_1 + vpclmulqdq zmm12,zmm2,zmm8,0x00 ; LO_2 + vpxord zmm5,zmm5,zmm11 ; sum(LO_{1,0}) + vpclmulqdq zmm13,zmm3,zmm9,0x00 ; LO_3 + vpternlogd zmm5,zmm12,zmm13,0x96 ; LO = sum(LO_{3,2,1,0}) + vpclmulqdq zmm11,zmm0,zmm6,0x01 ; MI_0 + vpclmulqdq zmm12,zmm1,zmm7,0x01 ; MI_1 + vpclmulqdq zmm13,zmm2,zmm8,0x01 ; MI_2 + vpternlogd zmm11,zmm12,zmm13,0x96 ; sum(MI_{2,1,0}) + vpclmulqdq zmm12,zmm3,zmm9,0x01 ; MI_3 + vpclmulqdq zmm13,zmm0,zmm6,0x10 ; MI_4 + vpternlogd zmm11,zmm12,zmm13,0x96 ; sum(MI_{4,3,2,1,0}) + vpclmulqdq zmm12,zmm1,zmm7,0x10 ; MI_5 + vpclmulqdq zmm13,zmm2,zmm8,0x10 ; MI_6 + vpternlogd zmm11,zmm12,zmm13,0x96 ; sum(MI_{6,5,4,3,2,1,0}) + vpclmulqdq zmm13,zmm10,zmm5,0x01 ; LO_L*(x^63 + x^62 + x^57) + vpclmulqdq zmm12,zmm3,zmm9,0x10 ; MI_7 + vpxord zmm11,zmm11,zmm12 ; MI = sum(MI_{7,6,5,4,3,2,1,0}) + vpshufd zmm5,zmm5,0x4e ; Swap halves of LO + vpclmulqdq zmm0,zmm0,zmm6,0x11 ; HI_0 + vpclmulqdq zmm1,zmm1,zmm7,0x11 ; HI_1 + vpclmulqdq zmm2,zmm2,zmm8,0x11 ; HI_2 + vpternlogd zmm11,zmm5,zmm13,0x96 ; Fold LO into MI + vpclmulqdq zmm3,zmm3,zmm9,0x11 ; HI_3 + vpternlogd zmm0,zmm1,zmm2,0x96 ; sum(HI_{2,1,0}) + vpclmulqdq zmm12,zmm10,zmm11,0x01 ; MI_L*(x^63 + x^62 + x^57) + vpxord zmm5,zmm0,zmm3 ; HI = sum(HI_{3,2,1,0}) + vpshufd zmm11,zmm11,0x4e ; Swap halves of MI + vpternlogd zmm5,zmm11,zmm12,0x96 ; Fold MI into HI vextracti32x4 xmm0,zmm5,1 vextracti32x4 xmm1,zmm5,2 vextracti32x4 xmm2,zmm5,3 @@ -303,24 +305,24 @@ $L$aad_loop_4x: cmp r9,256 jae NEAR $L$aad_loop_4x - +; Update GHASH with 64 bytes of AAD at a time. cmp r9,64 jb NEAR $L$aad_large_done $L$aad_loop_1x: vmovdqu8 zmm0,ZMMWORD[r8] vpshufb zmm0,zmm0,zmm4 vpxord zmm5,zmm5,zmm0 - vpclmulqdq zmm0,zmm5,zmm9,0x00 - vpclmulqdq zmm1,zmm5,zmm9,0x01 - vpclmulqdq zmm2,zmm5,zmm9,0x10 - vpxord zmm1,zmm1,zmm2 - vpclmulqdq zmm2,zmm10,zmm0,0x01 - vpshufd zmm0,zmm0,0x4e - vpternlogd zmm1,zmm0,zmm2,0x96 - vpclmulqdq zmm5,zmm5,zmm9,0x11 - vpclmulqdq zmm0,zmm10,zmm1,0x01 - vpshufd zmm1,zmm1,0x4e - vpternlogd zmm5,zmm1,zmm0,0x96 + vpclmulqdq zmm0,zmm5,zmm9,0x00 ; LO = a_L * b_L + vpclmulqdq zmm1,zmm5,zmm9,0x01 ; MI_0 = a_L * b_H + vpclmulqdq zmm2,zmm5,zmm9,0x10 ; MI_1 = a_H * b_L + vpxord zmm1,zmm1,zmm2 ; MI = MI_0 + MI_1 + vpclmulqdq zmm2,zmm10,zmm0,0x01 ; LO_L*(x^63 + x^62 + x^57) + vpshufd zmm0,zmm0,0x4e ; Swap halves of LO + vpternlogd zmm1,zmm0,zmm2,0x96 ; Fold LO into MI + vpclmulqdq zmm5,zmm5,zmm9,0x11 ; HI = a_H * b_H + vpclmulqdq zmm0,zmm10,zmm1,0x01 ; MI_L*(x^63 + x^62 + x^57) + vpshufd zmm1,zmm1,0x4e ; Swap halves of MI + vpternlogd zmm5,zmm1,zmm0,0x96 ; Fold MI into HI vextracti32x4 xmm0,zmm5,1 vextracti32x4 xmm1,zmm5,2 @@ -335,7 +337,7 @@ $L$aad_loop_1x: $L$aad_large_done: - +; GHASH the remaining data 16 bytes at a time, using xmm registers only. $L$aad_blockbyblock: test r9,r9 jz NEAR $L$aad_done @@ -344,28 +346,28 @@ $L$aad_loop_blockbyblock: vmovdqu xmm0,XMMWORD[r8] vpshufb xmm0,xmm0,xmm4 vpxor xmm5,xmm5,xmm0 - vpclmulqdq xmm0,xmm5,xmm9,0x00 - vpclmulqdq xmm1,xmm5,xmm9,0x01 - vpclmulqdq xmm2,xmm5,xmm9,0x10 - vpxord xmm1,xmm1,xmm2 - vpclmulqdq xmm2,xmm10,xmm0,0x01 - vpshufd xmm0,xmm0,0x4e - vpternlogd xmm1,xmm0,xmm2,0x96 - vpclmulqdq xmm5,xmm5,xmm9,0x11 - vpclmulqdq xmm0,xmm10,xmm1,0x01 - vpshufd xmm1,xmm1,0x4e - vpternlogd xmm5,xmm1,xmm0,0x96 + vpclmulqdq xmm0,xmm5,xmm9,0x00 ; LO = a_L * b_L + vpclmulqdq xmm1,xmm5,xmm9,0x01 ; MI_0 = a_L * b_H + vpclmulqdq xmm2,xmm5,xmm9,0x10 ; MI_1 = a_H * b_L + vpxord xmm1,xmm1,xmm2 ; MI = MI_0 + MI_1 + vpclmulqdq xmm2,xmm10,xmm0,0x01 ; LO_L*(x^63 + x^62 + x^57) + vpshufd xmm0,xmm0,0x4e ; Swap halves of LO + vpternlogd xmm1,xmm0,xmm2,0x96 ; Fold LO into MI + vpclmulqdq xmm5,xmm5,xmm9,0x11 ; HI = a_H * b_H + vpclmulqdq xmm0,xmm10,xmm1,0x01 ; MI_L*(x^63 + x^62 + x^57) + vpshufd xmm1,xmm1,0x4e ; Swap halves of MI + vpternlogd xmm5,xmm1,xmm0,0x96 ; Fold MI into HI add r8,16 sub r9,16 jnz NEAR $L$aad_loop_blockbyblock $L$aad_done: - +; Store the updated GHASH accumulator back to memory. vpshufb xmm5,xmm5,xmm4 vmovdqu XMMWORD[rcx],xmm5 - vzeroupper + vzeroupper ; This is needed after using ymm or zmm registers. vmovdqa xmm6,XMMWORD[rsp] vmovdqa xmm7,XMMWORD[16+rsp] vmovdqa xmm8,XMMWORD[32+rsp] @@ -393,9 +395,9 @@ $L$SEH_prologue_aes_gcm_enc_update_vaes_avx512_3: push r12 $L$SEH_prologue_aes_gcm_enc_update_vaes_avx512_4: - mov rsi,QWORD[64+rsp] - mov rdi,QWORD[72+rsp] - mov r12,QWORD[80+rsp] + mov rsi,QWORD[64+rsp] ; arg5 + mov rdi,QWORD[72+rsp] ; arg6 + mov r12,QWORD[80+rsp] ; arg7 sub rsp,160 $L$SEH_prologue_aes_gcm_enc_update_vaes_avx512_5: vmovdqa XMMWORD[rsp],xmm6 @@ -424,49 +426,49 @@ $L$SEH_endprologue_aes_gcm_enc_update_vaes_avx512_16: EXTERN BORINGSSL_function_hit mov BYTE[((BORINGSSL_function_hit+7))],1 %endif - +; Load some constants. vbroadcasti32x4 zmm8,ZMMWORD[$L$bswap_mask] vbroadcasti32x4 zmm31,ZMMWORD[$L$gfpoly] - - +; Load the GHASH accumulator and the starting counter. +; BoringSSL passes these values in big endian format. vmovdqu xmm10,XMMWORD[r12] vpshufb xmm10,xmm10,xmm8 vbroadcasti32x4 zmm12,ZMMWORD[rsi] vpshufb zmm12,zmm12,zmm8 - - +; Load the AES key length in bytes. BoringSSL stores number of rounds +; minus 1, so convert using: AESKEYLEN = 4 * aeskey->rounds - 20. mov r10d,DWORD[240+r9] lea r10d,[((-20))+r10*4] - - - +; Make RNDKEYLAST_PTR point to the last AES round key. This is the +; round key with index 10, 12, or 14 for AES-128, AES-192, or AES-256 +; respectively. Then load the zero-th and last round keys. lea r11,[96+r10*4+r9] vbroadcasti32x4 zmm13,ZMMWORD[r9] vbroadcasti32x4 zmm14,ZMMWORD[r11] - +; Finish initializing LE_CTR by adding [0, 1, 2, 3] to its low words. vpaddd zmm12,zmm12,ZMMWORD[$L$ctr_pattern] - +; Load 4 into all 128-bit lanes of LE_CTR_INC. vbroadcasti32x4 zmm11,ZMMWORD[$L$inc_4blocks] - - +; If there are at least 256 bytes of data, then continue into the loop +; that processes 256 bytes of data at a time. Otherwise skip it. cmp r8,256 jb NEAR $L$crypt_loop_4x_done__func1 - +; Load powers of the hash key. vmovdqu8 zmm27,ZMMWORD[((256-256))+rdi] vmovdqu8 zmm28,ZMMWORD[((256-192))+rdi] vmovdqu8 zmm29,ZMMWORD[((256-128))+rdi] vmovdqu8 zmm30,ZMMWORD[((256-64))+rdi] - - - - +; Encrypt the first 4 vectors of plaintext blocks. Leave the resulting +; ciphertext in GHASHDATA[0-3] for GHASH. +; Increment le_ctr four times to generate four vectors of little-endian +; counter blocks, swap each to big-endian, and store them in aesdata[0-3]. vpshufb zmm0,zmm12,zmm8 vpaddd zmm12,zmm12,zmm11 vpshufb zmm1,zmm12,zmm8 @@ -476,7 +478,7 @@ EXTERN BORINGSSL_function_hit vpshufb zmm3,zmm12,zmm8 vpaddd zmm12,zmm12,zmm11 - +; AES "round zero": XOR in the zero-th round key. vpxord zmm0,zmm0,zmm13 vpxord zmm1,zmm1,zmm13 vpxord zmm2,zmm2,zmm13 @@ -511,7 +513,7 @@ $L$vaesenc_loop_first_4_vecs__func1: sub r8,256 cmp r8,256 jb NEAR $L$ghash_last_ciphertext_4x__func1 - +; Cache as many additional AES round keys as possible. vbroadcasti32x4 zmm15,ZMMWORD[((-144))+r11] vbroadcasti32x4 zmm16,ZMMWORD[((-128))+r11] vbroadcasti32x4 zmm17,ZMMWORD[((-112))+r11] @@ -523,9 +525,9 @@ $L$vaesenc_loop_first_4_vecs__func1: vbroadcasti32x4 zmm23,ZMMWORD[((-16))+r11] $L$crypt_loop_4x__func1: - - - +; Start the AES encryption of the counter blocks. +; Increment le_ctr four times to generate four vectors of little-endian +; counter blocks, swap each to big-endian, and store them in aesdata[0-3]. vpshufb zmm0,zmm12,zmm8 vpaddd zmm12,zmm12,zmm11 vpshufb zmm1,zmm12,zmm8 @@ -535,7 +537,7 @@ $L$crypt_loop_4x__func1: vpshufb zmm3,zmm12,zmm8 vpaddd zmm12,zmm12,zmm11 - +; AES "round zero": XOR in the zero-th round key. vpxord zmm0,zmm0,zmm13 vpxord zmm1,zmm1,zmm13 vpxord zmm2,zmm2,zmm13 @@ -544,7 +546,7 @@ $L$crypt_loop_4x__func1: cmp r10d,24 jl NEAR $L$aes128__func1 je NEAR $L$aes192__func1 - +; AES-256 vbroadcasti32x4 zmm9,ZMMWORD[((-208))+r11] vaesenc zmm0,zmm0,zmm9 vaesenc zmm1,zmm1,zmm9 @@ -572,17 +574,17 @@ $L$aes192__func1: $L$aes128__func1: - - - +; Prefetch the source data 512 bytes ahead into the L1 data cache, to +; improve performance when the hardware prefetcher is disabled. Assumes the +; L1 data cache line size is 64 bytes (de facto standard on x86_64). prefetcht0 [((512+0))+rcx] prefetcht0 [((512+64))+rcx] prefetcht0 [((512+128))+rcx] prefetcht0 [((512+192))+rcx] - - - +; Finish the AES encryption of the counter blocks in AESDATA[0-3], +; interleaved with the GHASH update of the ciphertext blocks in +; GHASHDATA[0-3]. vpshufb zmm4,zmm4,zmm8 vpxord zmm4,zmm4,zmm10 vpshufb zmm5,zmm5,zmm8 @@ -594,78 +596,78 @@ $L$aes128__func1: vaesenc zmm3,zmm3,zmm15 vpshufb zmm7,zmm7,zmm8 - vpclmulqdq zmm10,zmm4,zmm27,0x00 - vpclmulqdq zmm24,zmm5,zmm28,0x00 - vpclmulqdq zmm25,zmm6,zmm29,0x00 + vpclmulqdq zmm10,zmm4,zmm27,0x00 ; LO_0 + vpclmulqdq zmm24,zmm5,zmm28,0x00 ; LO_1 + vpclmulqdq zmm25,zmm6,zmm29,0x00 ; LO_2 vaesenc zmm0,zmm0,zmm16 vaesenc zmm1,zmm1,zmm16 vaesenc zmm2,zmm2,zmm16 vaesenc zmm3,zmm3,zmm16 - vpxord zmm10,zmm10,zmm24 - vpclmulqdq zmm26,zmm7,zmm30,0x00 - vpternlogd zmm10,zmm25,zmm26,0x96 - vpclmulqdq zmm24,zmm4,zmm27,0x01 + vpxord zmm10,zmm10,zmm24 ; sum(LO_{1,0}) + vpclmulqdq zmm26,zmm7,zmm30,0x00 ; LO_3 + vpternlogd zmm10,zmm25,zmm26,0x96 ; LO = sum(LO_{3,2,1,0}) + vpclmulqdq zmm24,zmm4,zmm27,0x01 ; MI_0 vaesenc zmm0,zmm0,zmm17 vaesenc zmm1,zmm1,zmm17 vaesenc zmm2,zmm2,zmm17 vaesenc zmm3,zmm3,zmm17 - vpclmulqdq zmm25,zmm5,zmm28,0x01 - vpclmulqdq zmm26,zmm6,zmm29,0x01 - vpternlogd zmm24,zmm25,zmm26,0x96 - vpclmulqdq zmm25,zmm7,zmm30,0x01 + vpclmulqdq zmm25,zmm5,zmm28,0x01 ; MI_1 + vpclmulqdq zmm26,zmm6,zmm29,0x01 ; MI_2 + vpternlogd zmm24,zmm25,zmm26,0x96 ; sum(MI_{2,1,0}) + vpclmulqdq zmm25,zmm7,zmm30,0x01 ; MI_3 vaesenc zmm0,zmm0,zmm18 vaesenc zmm1,zmm1,zmm18 vaesenc zmm2,zmm2,zmm18 vaesenc zmm3,zmm3,zmm18 - vpclmulqdq zmm26,zmm4,zmm27,0x10 - vpternlogd zmm24,zmm25,zmm26,0x96 - vpclmulqdq zmm25,zmm5,zmm28,0x10 - vpclmulqdq zmm26,zmm6,zmm29,0x10 + vpclmulqdq zmm26,zmm4,zmm27,0x10 ; MI_4 + vpternlogd zmm24,zmm25,zmm26,0x96 ; sum(MI_{4,3,2,1,0}) + vpclmulqdq zmm25,zmm5,zmm28,0x10 ; MI_5 + vpclmulqdq zmm26,zmm6,zmm29,0x10 ; MI_6 vaesenc zmm0,zmm0,zmm19 vaesenc zmm1,zmm1,zmm19 vaesenc zmm2,zmm2,zmm19 vaesenc zmm3,zmm3,zmm19 - vpternlogd zmm24,zmm25,zmm26,0x96 - vpclmulqdq zmm26,zmm31,zmm10,0x01 - vpclmulqdq zmm25,zmm7,zmm30,0x10 - vpxord zmm24,zmm24,zmm25 + vpternlogd zmm24,zmm25,zmm26,0x96 ; sum(MI_{6,5,4,3,2,1,0}) + vpclmulqdq zmm26,zmm31,zmm10,0x01 ; LO_L*(x^63 + x^62 + x^57) + vpclmulqdq zmm25,zmm7,zmm30,0x10 ; MI_7 + vpxord zmm24,zmm24,zmm25 ; MI = sum(MI_{7,6,5,4,3,2,1,0}) vaesenc zmm0,zmm0,zmm20 vaesenc zmm1,zmm1,zmm20 vaesenc zmm2,zmm2,zmm20 vaesenc zmm3,zmm3,zmm20 - vpshufd zmm10,zmm10,0x4e - vpclmulqdq zmm4,zmm4,zmm27,0x11 - vpclmulqdq zmm5,zmm5,zmm28,0x11 - vpclmulqdq zmm6,zmm6,zmm29,0x11 + vpshufd zmm10,zmm10,0x4e ; Swap halves of LO + vpclmulqdq zmm4,zmm4,zmm27,0x11 ; HI_0 + vpclmulqdq zmm5,zmm5,zmm28,0x11 ; HI_1 + vpclmulqdq zmm6,zmm6,zmm29,0x11 ; HI_2 vaesenc zmm0,zmm0,zmm21 vaesenc zmm1,zmm1,zmm21 vaesenc zmm2,zmm2,zmm21 vaesenc zmm3,zmm3,zmm21 - vpternlogd zmm24,zmm10,zmm26,0x96 - vpclmulqdq zmm7,zmm7,zmm30,0x11 - vpternlogd zmm4,zmm5,zmm6,0x96 - vpclmulqdq zmm25,zmm31,zmm24,0x01 + vpternlogd zmm24,zmm10,zmm26,0x96 ; Fold LO into MI + vpclmulqdq zmm7,zmm7,zmm30,0x11 ; HI_3 + vpternlogd zmm4,zmm5,zmm6,0x96 ; sum(HI_{2,1,0}) + vpclmulqdq zmm25,zmm31,zmm24,0x01 ; MI_L*(x^63 + x^62 + x^57) vaesenc zmm0,zmm0,zmm22 vaesenc zmm1,zmm1,zmm22 vaesenc zmm2,zmm2,zmm22 vaesenc zmm3,zmm3,zmm22 - vpxord zmm10,zmm4,zmm7 - vpshufd zmm24,zmm24,0x4e - vpternlogd zmm10,zmm24,zmm25,0x96 + vpxord zmm10,zmm4,zmm7 ; HI = sum(HI_{3,2,1,0}) + vpshufd zmm24,zmm24,0x4e ; Swap halves of MI + vpternlogd zmm10,zmm24,zmm25,0x96 ; Fold MI into HI vaesenc zmm0,zmm0,zmm23 vaesenc zmm1,zmm1,zmm23 @@ -703,36 +705,36 @@ $L$ghash_last_ciphertext_4x__func1: vpshufb zmm5,zmm5,zmm8 vpshufb zmm6,zmm6,zmm8 vpshufb zmm7,zmm7,zmm8 - vpclmulqdq zmm10,zmm4,zmm27,0x00 - vpclmulqdq zmm24,zmm5,zmm28,0x00 - vpclmulqdq zmm25,zmm6,zmm29,0x00 - vpxord zmm10,zmm10,zmm24 - vpclmulqdq zmm26,zmm7,zmm30,0x00 - vpternlogd zmm10,zmm25,zmm26,0x96 - vpclmulqdq zmm24,zmm4,zmm27,0x01 - vpclmulqdq zmm25,zmm5,zmm28,0x01 - vpclmulqdq zmm26,zmm6,zmm29,0x01 - vpternlogd zmm24,zmm25,zmm26,0x96 - vpclmulqdq zmm25,zmm7,zmm30,0x01 - vpclmulqdq zmm26,zmm4,zmm27,0x10 - vpternlogd zmm24,zmm25,zmm26,0x96 - vpclmulqdq zmm25,zmm5,zmm28,0x10 - vpclmulqdq zmm26,zmm6,zmm29,0x10 - vpternlogd zmm24,zmm25,zmm26,0x96 - vpclmulqdq zmm26,zmm31,zmm10,0x01 - vpclmulqdq zmm25,zmm7,zmm30,0x10 - vpxord zmm24,zmm24,zmm25 - vpshufd zmm10,zmm10,0x4e - vpclmulqdq zmm4,zmm4,zmm27,0x11 - vpclmulqdq zmm5,zmm5,zmm28,0x11 - vpclmulqdq zmm6,zmm6,zmm29,0x11 - vpternlogd zmm24,zmm10,zmm26,0x96 - vpclmulqdq zmm7,zmm7,zmm30,0x11 - vpternlogd zmm4,zmm5,zmm6,0x96 - vpclmulqdq zmm25,zmm31,zmm24,0x01 - vpxord zmm10,zmm4,zmm7 - vpshufd zmm24,zmm24,0x4e - vpternlogd zmm10,zmm24,zmm25,0x96 + vpclmulqdq zmm10,zmm4,zmm27,0x00 ; LO_0 + vpclmulqdq zmm24,zmm5,zmm28,0x00 ; LO_1 + vpclmulqdq zmm25,zmm6,zmm29,0x00 ; LO_2 + vpxord zmm10,zmm10,zmm24 ; sum(LO_{1,0}) + vpclmulqdq zmm26,zmm7,zmm30,0x00 ; LO_3 + vpternlogd zmm10,zmm25,zmm26,0x96 ; LO = sum(LO_{3,2,1,0}) + vpclmulqdq zmm24,zmm4,zmm27,0x01 ; MI_0 + vpclmulqdq zmm25,zmm5,zmm28,0x01 ; MI_1 + vpclmulqdq zmm26,zmm6,zmm29,0x01 ; MI_2 + vpternlogd zmm24,zmm25,zmm26,0x96 ; sum(MI_{2,1,0}) + vpclmulqdq zmm25,zmm7,zmm30,0x01 ; MI_3 + vpclmulqdq zmm26,zmm4,zmm27,0x10 ; MI_4 + vpternlogd zmm24,zmm25,zmm26,0x96 ; sum(MI_{4,3,2,1,0}) + vpclmulqdq zmm25,zmm5,zmm28,0x10 ; MI_5 + vpclmulqdq zmm26,zmm6,zmm29,0x10 ; MI_6 + vpternlogd zmm24,zmm25,zmm26,0x96 ; sum(MI_{6,5,4,3,2,1,0}) + vpclmulqdq zmm26,zmm31,zmm10,0x01 ; LO_L*(x^63 + x^62 + x^57) + vpclmulqdq zmm25,zmm7,zmm30,0x10 ; MI_7 + vpxord zmm24,zmm24,zmm25 ; MI = sum(MI_{7,6,5,4,3,2,1,0}) + vpshufd zmm10,zmm10,0x4e ; Swap halves of LO + vpclmulqdq zmm4,zmm4,zmm27,0x11 ; HI_0 + vpclmulqdq zmm5,zmm5,zmm28,0x11 ; HI_1 + vpclmulqdq zmm6,zmm6,zmm29,0x11 ; HI_2 + vpternlogd zmm24,zmm10,zmm26,0x96 ; Fold LO into MI + vpclmulqdq zmm7,zmm7,zmm30,0x11 ; HI_3 + vpternlogd zmm4,zmm5,zmm6,0x96 ; sum(HI_{2,1,0}) + vpclmulqdq zmm25,zmm31,zmm24,0x01 ; MI_L*(x^63 + x^62 + x^57) + vpxord zmm10,zmm4,zmm7 ; HI = sum(HI_{3,2,1,0}) + vpshufd zmm24,zmm24,0x4e ; Swap halves of MI + vpternlogd zmm10,zmm24,zmm25,0x96 ; Fold MI into HI vextracti32x4 xmm4,zmm10,1 vextracti32x4 xmm5,zmm10,2 vextracti32x4 xmm6,zmm10,3 @@ -740,32 +742,32 @@ $L$ghash_last_ciphertext_4x__func1: vpternlogd xmm10,xmm6,xmm5,0x96 $L$crypt_loop_4x_done__func1: - +; Check whether any data remains. test r8,r8 jz NEAR $L$done__func1 - - - - - - - - - - - - - - - - - - - +; The data length isn't a multiple of 256 bytes. Process the remaining +; data of length 1 <= DATALEN < 256, up to one 64-byte vector at a time. +; Going one vector at a time may seem inefficient compared to having +; separate code paths for each possible number of vectors remaining. +; However, using a loop keeps the code size down, and it performs +; surprising well; modern CPUs will start executing the next iteration +; before the previous one finishes and also predict the number of loop +; iterations. For a similar reason, we roll up the AES rounds. +; +; On the last iteration, the remaining length may be less than 64 bytes. +; Handle this using masking. +; +; Since there are enough key powers available for all remaining data, +; there is no need to do a GHASH reduction after each iteration. +; Instead, multiply each remaining block by its own key power, and only +; do a GHASH reduction at the very end. + +; Make POWERS_PTR point to the key powers [H^N, H^(N-1), ...] where N +; is the number of blocks that remain. mov rax,r8 neg rax - and rax,-16 + and rax,-16 ; -round_up(DATALEN, 16) lea rsi,[256+rax*1+rdi] vpxor xmm4,xmm4,xmm4 vpxor xmm5,xmm5,xmm5 @@ -775,9 +777,9 @@ $L$crypt_loop_4x_done__func1: jb NEAR $L$partial_vec__func1 $L$crypt_loop_1x__func1: +; Process a full 64-byte vector. - - +; Encrypt a vector of counter blocks. vpshufb zmm0,zmm12,zmm8 vpaddd zmm12,zmm12,zmm11 vpxord zmm0,zmm0,zmm13 @@ -790,19 +792,19 @@ $L$vaesenc_loop_tail_full_vec__func1: jne NEAR $L$vaesenc_loop_tail_full_vec__func1 vaesenclast zmm0,zmm0,zmm14 - +; XOR the data with the vector of keystream blocks. vmovdqu8 zmm1,ZMMWORD[rcx] vpxord zmm0,zmm0,zmm1 vmovdqu8 ZMMWORD[rdx],zmm0 - +; Update GHASH with the ciphertext blocks, without reducing. vmovdqu8 zmm30,ZMMWORD[rsi] vpshufb zmm0,zmm0,zmm8 vpxord zmm0,zmm0,zmm10 - vpclmulqdq zmm7,zmm0,zmm30,0x00 - vpclmulqdq zmm1,zmm0,zmm30,0x01 - vpclmulqdq zmm2,zmm0,zmm30,0x10 - vpclmulqdq zmm3,zmm0,zmm30,0x11 + vpclmulqdq zmm7,zmm0,zmm30,0x00 ; a_L * b_L + vpclmulqdq zmm1,zmm0,zmm30,0x01 ; a_L * b_H + vpclmulqdq zmm2,zmm0,zmm30,0x10 ; a_H * b_L + vpclmulqdq zmm3,zmm0,zmm30,0x11 ; a_H * b_H vpxord zmm4,zmm4,zmm7 vpternlogd zmm5,zmm1,zmm2,0x96 vpxord zmm6,zmm6,zmm3 @@ -820,10 +822,10 @@ $L$vaesenc_loop_tail_full_vec__func1: jz NEAR $L$reduce__func1 $L$partial_vec__func1: +; Process a partial vector of length 1 <= DATALEN < 64. - - - +; Set the data mask %k1 to DATALEN 1's. +; Set the key powers mask %k2 to round_up(DATALEN, 16) 1's. mov rax,-1 bzhi rax,rax,r8 kmovq k1,rax @@ -833,8 +835,8 @@ $L$partial_vec__func1: bzhi rax,rax,r8 kmovq k2,rax - - +; Encrypt one last vector of counter blocks. This does not need to be +; masked. The counter does not need to be incremented here. vpshufb zmm0,zmm12,zmm8 vpxord zmm0,zmm0,zmm13 lea rax,[16+r9] @@ -846,38 +848,38 @@ $L$vaesenc_loop_tail_partialvec__func1: jne NEAR $L$vaesenc_loop_tail_partialvec__func1 vaesenclast zmm0,zmm0,zmm14 - +; XOR the data with the appropriate number of keystream bytes. vmovdqu8 zmm1{k1}{z},[rcx] vpxord zmm0,zmm0,zmm1 vmovdqu8 ZMMWORD[rdx]{k1},zmm0 - - - - - - - - - - - - +; Update GHASH with the ciphertext block(s), without reducing. +; +; In the case of DATALEN < 64, the ciphertext is zero-padded to 64 +; bytes. (If decrypting, it's done by the above masked load. If +; encrypting, it's done by the below masked register-to-register move.) +; Note that if DATALEN <= 48, there will be additional padding beyond +; the padding of the last block specified by GHASH itself; i.e., there +; may be whole block(s) that get processed by the GHASH multiplication +; and reduction instructions but should not actually be included in the +; GHASH. However, any such blocks are all-zeroes, and the values that +; they're multiplied with are also all-zeroes. Therefore they just add +; 0 * 0 = 0 to the final GHASH result, which makes no difference. vmovdqu8 zmm30{k2}{z},[rsi] vmovdqu8 zmm1{k1}{z},zmm0 vpshufb zmm0,zmm1,zmm8 vpxord zmm0,zmm0,zmm10 - vpclmulqdq zmm7,zmm0,zmm30,0x00 - vpclmulqdq zmm1,zmm0,zmm30,0x01 - vpclmulqdq zmm2,zmm0,zmm30,0x10 - vpclmulqdq zmm3,zmm0,zmm30,0x11 + vpclmulqdq zmm7,zmm0,zmm30,0x00 ; a_L * b_L + vpclmulqdq zmm1,zmm0,zmm30,0x01 ; a_L * b_H + vpclmulqdq zmm2,zmm0,zmm30,0x10 ; a_H * b_L + vpclmulqdq zmm3,zmm0,zmm30,0x11 ; a_H * b_H vpxord zmm4,zmm4,zmm7 vpternlogd zmm5,zmm1,zmm2,0x96 vpxord zmm6,zmm6,zmm3 $L$reduce__func1: - +; Finally, do the GHASH reduction. vpclmulqdq zmm0,zmm31,zmm4,0x01 vpshufd zmm4,zmm4,0x4e vpternlogd zmm5,zmm4,zmm0,0x96 @@ -893,11 +895,11 @@ $L$reduce__func1: $L$done__func1: - +; Store the updated GHASH accumulator back to memory. vpshufb xmm10,xmm10,xmm8 vmovdqu XMMWORD[r12],xmm10 - vzeroupper + vzeroupper ; This is needed after using ymm or zmm registers. vmovdqa xmm6,XMMWORD[rsp] vmovdqa xmm7,XMMWORD[16+rsp] vmovdqa xmm8,XMMWORD[32+rsp] @@ -930,9 +932,9 @@ $L$SEH_prologue_aes_gcm_dec_update_vaes_avx512_3: push r12 $L$SEH_prologue_aes_gcm_dec_update_vaes_avx512_4: - mov rsi,QWORD[64+rsp] - mov rdi,QWORD[72+rsp] - mov r12,QWORD[80+rsp] + mov rsi,QWORD[64+rsp] ; arg5 + mov rdi,QWORD[72+rsp] ; arg6 + mov r12,QWORD[80+rsp] ; arg7 sub rsp,160 $L$SEH_prologue_aes_gcm_dec_update_vaes_avx512_5: vmovdqa XMMWORD[rsp],xmm6 @@ -957,46 +959,46 @@ $L$SEH_prologue_aes_gcm_dec_update_vaes_avx512_14: $L$SEH_prologue_aes_gcm_dec_update_vaes_avx512_15: $L$SEH_endprologue_aes_gcm_dec_update_vaes_avx512_16: - +; Load some constants. vbroadcasti32x4 zmm8,ZMMWORD[$L$bswap_mask] vbroadcasti32x4 zmm31,ZMMWORD[$L$gfpoly] - - +; Load the GHASH accumulator and the starting counter. +; BoringSSL passes these values in big endian format. vmovdqu xmm10,XMMWORD[r12] vpshufb xmm10,xmm10,xmm8 vbroadcasti32x4 zmm12,ZMMWORD[rsi] vpshufb zmm12,zmm12,zmm8 - - +; Load the AES key length in bytes. BoringSSL stores number of rounds +; minus 1, so convert using: AESKEYLEN = 4 * aeskey->rounds - 20. mov r10d,DWORD[240+r9] lea r10d,[((-20))+r10*4] - - - +; Make RNDKEYLAST_PTR point to the last AES round key. This is the +; round key with index 10, 12, or 14 for AES-128, AES-192, or AES-256 +; respectively. Then load the zero-th and last round keys. lea r11,[96+r10*4+r9] vbroadcasti32x4 zmm13,ZMMWORD[r9] vbroadcasti32x4 zmm14,ZMMWORD[r11] - +; Finish initializing LE_CTR by adding [0, 1, 2, 3] to its low words. vpaddd zmm12,zmm12,ZMMWORD[$L$ctr_pattern] - +; Load 4 into all 128-bit lanes of LE_CTR_INC. vbroadcasti32x4 zmm11,ZMMWORD[$L$inc_4blocks] - - +; If there are at least 256 bytes of data, then continue into the loop +; that processes 256 bytes of data at a time. Otherwise skip it. cmp r8,256 jb NEAR $L$crypt_loop_4x_done__func2 - +; Load powers of the hash key. vmovdqu8 zmm27,ZMMWORD[((256-256))+rdi] vmovdqu8 zmm28,ZMMWORD[((256-192))+rdi] vmovdqu8 zmm29,ZMMWORD[((256-128))+rdi] vmovdqu8 zmm30,ZMMWORD[((256-64))+rdi] - +; Cache as many additional AES round keys as possible. vbroadcasti32x4 zmm15,ZMMWORD[((-144))+r11] vbroadcasti32x4 zmm16,ZMMWORD[((-128))+r11] vbroadcasti32x4 zmm17,ZMMWORD[((-112))+r11] @@ -1012,9 +1014,9 @@ $L$crypt_loop_4x__func2: vmovdqu8 zmm5,ZMMWORD[64+rcx] vmovdqu8 zmm6,ZMMWORD[128+rcx] vmovdqu8 zmm7,ZMMWORD[192+rcx] - - - +; Start the AES encryption of the counter blocks. +; Increment le_ctr four times to generate four vectors of little-endian +; counter blocks, swap each to big-endian, and store them in aesdata[0-3]. vpshufb zmm0,zmm12,zmm8 vpaddd zmm12,zmm12,zmm11 vpshufb zmm1,zmm12,zmm8 @@ -1024,7 +1026,7 @@ $L$crypt_loop_4x__func2: vpshufb zmm3,zmm12,zmm8 vpaddd zmm12,zmm12,zmm11 - +; AES "round zero": XOR in the zero-th round key. vpxord zmm0,zmm0,zmm13 vpxord zmm1,zmm1,zmm13 vpxord zmm2,zmm2,zmm13 @@ -1033,7 +1035,7 @@ $L$crypt_loop_4x__func2: cmp r10d,24 jl NEAR $L$aes128__func2 je NEAR $L$aes192__func2 - +; AES-256 vbroadcasti32x4 zmm9,ZMMWORD[((-208))+r11] vaesenc zmm0,zmm0,zmm9 vaesenc zmm1,zmm1,zmm9 @@ -1061,17 +1063,17 @@ $L$aes192__func2: $L$aes128__func2: - - - +; Prefetch the source data 512 bytes ahead into the L1 data cache, to +; improve performance when the hardware prefetcher is disabled. Assumes the +; L1 data cache line size is 64 bytes (de facto standard on x86_64). prefetcht0 [((512+0))+rcx] prefetcht0 [((512+64))+rcx] prefetcht0 [((512+128))+rcx] prefetcht0 [((512+192))+rcx] - - - +; Finish the AES encryption of the counter blocks in AESDATA[0-3], +; interleaved with the GHASH update of the ciphertext blocks in +; GHASHDATA[0-3]. vpshufb zmm4,zmm4,zmm8 vpxord zmm4,zmm4,zmm10 vpshufb zmm5,zmm5,zmm8 @@ -1083,78 +1085,78 @@ $L$aes128__func2: vaesenc zmm3,zmm3,zmm15 vpshufb zmm7,zmm7,zmm8 - vpclmulqdq zmm10,zmm4,zmm27,0x00 - vpclmulqdq zmm24,zmm5,zmm28,0x00 - vpclmulqdq zmm25,zmm6,zmm29,0x00 + vpclmulqdq zmm10,zmm4,zmm27,0x00 ; LO_0 + vpclmulqdq zmm24,zmm5,zmm28,0x00 ; LO_1 + vpclmulqdq zmm25,zmm6,zmm29,0x00 ; LO_2 vaesenc zmm0,zmm0,zmm16 vaesenc zmm1,zmm1,zmm16 vaesenc zmm2,zmm2,zmm16 vaesenc zmm3,zmm3,zmm16 - vpxord zmm10,zmm10,zmm24 - vpclmulqdq zmm26,zmm7,zmm30,0x00 - vpternlogd zmm10,zmm25,zmm26,0x96 - vpclmulqdq zmm24,zmm4,zmm27,0x01 + vpxord zmm10,zmm10,zmm24 ; sum(LO_{1,0}) + vpclmulqdq zmm26,zmm7,zmm30,0x00 ; LO_3 + vpternlogd zmm10,zmm25,zmm26,0x96 ; LO = sum(LO_{3,2,1,0}) + vpclmulqdq zmm24,zmm4,zmm27,0x01 ; MI_0 vaesenc zmm0,zmm0,zmm17 vaesenc zmm1,zmm1,zmm17 vaesenc zmm2,zmm2,zmm17 vaesenc zmm3,zmm3,zmm17 - vpclmulqdq zmm25,zmm5,zmm28,0x01 - vpclmulqdq zmm26,zmm6,zmm29,0x01 - vpternlogd zmm24,zmm25,zmm26,0x96 - vpclmulqdq zmm25,zmm7,zmm30,0x01 + vpclmulqdq zmm25,zmm5,zmm28,0x01 ; MI_1 + vpclmulqdq zmm26,zmm6,zmm29,0x01 ; MI_2 + vpternlogd zmm24,zmm25,zmm26,0x96 ; sum(MI_{2,1,0}) + vpclmulqdq zmm25,zmm7,zmm30,0x01 ; MI_3 vaesenc zmm0,zmm0,zmm18 vaesenc zmm1,zmm1,zmm18 vaesenc zmm2,zmm2,zmm18 vaesenc zmm3,zmm3,zmm18 - vpclmulqdq zmm26,zmm4,zmm27,0x10 - vpternlogd zmm24,zmm25,zmm26,0x96 - vpclmulqdq zmm25,zmm5,zmm28,0x10 - vpclmulqdq zmm26,zmm6,zmm29,0x10 + vpclmulqdq zmm26,zmm4,zmm27,0x10 ; MI_4 + vpternlogd zmm24,zmm25,zmm26,0x96 ; sum(MI_{4,3,2,1,0}) + vpclmulqdq zmm25,zmm5,zmm28,0x10 ; MI_5 + vpclmulqdq zmm26,zmm6,zmm29,0x10 ; MI_6 vaesenc zmm0,zmm0,zmm19 vaesenc zmm1,zmm1,zmm19 vaesenc zmm2,zmm2,zmm19 vaesenc zmm3,zmm3,zmm19 - vpternlogd zmm24,zmm25,zmm26,0x96 - vpclmulqdq zmm26,zmm31,zmm10,0x01 - vpclmulqdq zmm25,zmm7,zmm30,0x10 - vpxord zmm24,zmm24,zmm25 + vpternlogd zmm24,zmm25,zmm26,0x96 ; sum(MI_{6,5,4,3,2,1,0}) + vpclmulqdq zmm26,zmm31,zmm10,0x01 ; LO_L*(x^63 + x^62 + x^57) + vpclmulqdq zmm25,zmm7,zmm30,0x10 ; MI_7 + vpxord zmm24,zmm24,zmm25 ; MI = sum(MI_{7,6,5,4,3,2,1,0}) vaesenc zmm0,zmm0,zmm20 vaesenc zmm1,zmm1,zmm20 vaesenc zmm2,zmm2,zmm20 vaesenc zmm3,zmm3,zmm20 - vpshufd zmm10,zmm10,0x4e - vpclmulqdq zmm4,zmm4,zmm27,0x11 - vpclmulqdq zmm5,zmm5,zmm28,0x11 - vpclmulqdq zmm6,zmm6,zmm29,0x11 + vpshufd zmm10,zmm10,0x4e ; Swap halves of LO + vpclmulqdq zmm4,zmm4,zmm27,0x11 ; HI_0 + vpclmulqdq zmm5,zmm5,zmm28,0x11 ; HI_1 + vpclmulqdq zmm6,zmm6,zmm29,0x11 ; HI_2 vaesenc zmm0,zmm0,zmm21 vaesenc zmm1,zmm1,zmm21 vaesenc zmm2,zmm2,zmm21 vaesenc zmm3,zmm3,zmm21 - vpternlogd zmm24,zmm10,zmm26,0x96 - vpclmulqdq zmm7,zmm7,zmm30,0x11 - vpternlogd zmm4,zmm5,zmm6,0x96 - vpclmulqdq zmm25,zmm31,zmm24,0x01 + vpternlogd zmm24,zmm10,zmm26,0x96 ; Fold LO into MI + vpclmulqdq zmm7,zmm7,zmm30,0x11 ; HI_3 + vpternlogd zmm4,zmm5,zmm6,0x96 ; sum(HI_{2,1,0}) + vpclmulqdq zmm25,zmm31,zmm24,0x01 ; MI_L*(x^63 + x^62 + x^57) vaesenc zmm0,zmm0,zmm22 vaesenc zmm1,zmm1,zmm22 vaesenc zmm2,zmm2,zmm22 vaesenc zmm3,zmm3,zmm22 - vpxord zmm10,zmm4,zmm7 - vpshufd zmm24,zmm24,0x4e - vpternlogd zmm10,zmm24,zmm25,0x96 + vpxord zmm10,zmm4,zmm7 ; HI = sum(HI_{3,2,1,0}) + vpshufd zmm24,zmm24,0x4e ; Swap halves of MI + vpternlogd zmm10,zmm24,zmm25,0x96 ; Fold MI into HI vaesenc zmm0,zmm0,zmm23 vaesenc zmm1,zmm1,zmm23 @@ -1187,32 +1189,32 @@ $L$aes128__func2: cmp r8,256 jae NEAR $L$crypt_loop_4x__func2 $L$crypt_loop_4x_done__func2: - +; Check whether any data remains. test r8,r8 jz NEAR $L$done__func2 - - - - - - - - - - - - - - - - - - - +; The data length isn't a multiple of 256 bytes. Process the remaining +; data of length 1 <= DATALEN < 256, up to one 64-byte vector at a time. +; Going one vector at a time may seem inefficient compared to having +; separate code paths for each possible number of vectors remaining. +; However, using a loop keeps the code size down, and it performs +; surprising well; modern CPUs will start executing the next iteration +; before the previous one finishes and also predict the number of loop +; iterations. For a similar reason, we roll up the AES rounds. +; +; On the last iteration, the remaining length may be less than 64 bytes. +; Handle this using masking. +; +; Since there are enough key powers available for all remaining data, +; there is no need to do a GHASH reduction after each iteration. +; Instead, multiply each remaining block by its own key power, and only +; do a GHASH reduction at the very end. + +; Make POWERS_PTR point to the key powers [H^N, H^(N-1), ...] where N +; is the number of blocks that remain. mov rax,r8 neg rax - and rax,-16 + and rax,-16 ; -round_up(DATALEN, 16) lea rsi,[256+rax*1+rdi] vpxor xmm4,xmm4,xmm4 vpxor xmm5,xmm5,xmm5 @@ -1222,9 +1224,9 @@ $L$crypt_loop_4x_done__func2: jb NEAR $L$partial_vec__func2 $L$crypt_loop_1x__func2: +; Process a full 64-byte vector. - - +; Encrypt a vector of counter blocks. vpshufb zmm0,zmm12,zmm8 vpaddd zmm12,zmm12,zmm11 vpxord zmm0,zmm0,zmm13 @@ -1237,19 +1239,19 @@ $L$vaesenc_loop_tail_full_vec__func2: jne NEAR $L$vaesenc_loop_tail_full_vec__func2 vaesenclast zmm0,zmm0,zmm14 - +; XOR the data with the vector of keystream blocks. vmovdqu8 zmm1,ZMMWORD[rcx] vpxord zmm0,zmm0,zmm1 vmovdqu8 ZMMWORD[rdx],zmm0 - +; Update GHASH with the ciphertext blocks, without reducing. vmovdqu8 zmm30,ZMMWORD[rsi] vpshufb zmm0,zmm1,zmm8 vpxord zmm0,zmm0,zmm10 - vpclmulqdq zmm7,zmm0,zmm30,0x00 - vpclmulqdq zmm1,zmm0,zmm30,0x01 - vpclmulqdq zmm2,zmm0,zmm30,0x10 - vpclmulqdq zmm3,zmm0,zmm30,0x11 + vpclmulqdq zmm7,zmm0,zmm30,0x00 ; a_L * b_L + vpclmulqdq zmm1,zmm0,zmm30,0x01 ; a_L * b_H + vpclmulqdq zmm2,zmm0,zmm30,0x10 ; a_H * b_L + vpclmulqdq zmm3,zmm0,zmm30,0x11 ; a_H * b_H vpxord zmm4,zmm4,zmm7 vpternlogd zmm5,zmm1,zmm2,0x96 vpxord zmm6,zmm6,zmm3 @@ -1267,10 +1269,10 @@ $L$vaesenc_loop_tail_full_vec__func2: jz NEAR $L$reduce__func2 $L$partial_vec__func2: +; Process a partial vector of length 1 <= DATALEN < 64. - - - +; Set the data mask %k1 to DATALEN 1's. +; Set the key powers mask %k2 to round_up(DATALEN, 16) 1's. mov rax,-1 bzhi rax,rax,r8 kmovq k1,rax @@ -1280,8 +1282,8 @@ $L$partial_vec__func2: bzhi rax,rax,r8 kmovq k2,rax - - +; Encrypt one last vector of counter blocks. This does not need to be +; masked. The counter does not need to be incremented here. vpshufb zmm0,zmm12,zmm8 vpxord zmm0,zmm0,zmm13 lea rax,[16+r9] @@ -1293,38 +1295,38 @@ $L$vaesenc_loop_tail_partialvec__func2: jne NEAR $L$vaesenc_loop_tail_partialvec__func2 vaesenclast zmm0,zmm0,zmm14 - +; XOR the data with the appropriate number of keystream bytes. vmovdqu8 zmm1{k1}{z},[rcx] vpxord zmm0,zmm0,zmm1 vmovdqu8 ZMMWORD[rdx]{k1},zmm0 - - - - - - - - - - - - +; Update GHASH with the ciphertext block(s), without reducing. +; +; In the case of DATALEN < 64, the ciphertext is zero-padded to 64 +; bytes. (If decrypting, it's done by the above masked load. If +; encrypting, it's done by the below masked register-to-register move.) +; Note that if DATALEN <= 48, there will be additional padding beyond +; the padding of the last block specified by GHASH itself; i.e., there +; may be whole block(s) that get processed by the GHASH multiplication +; and reduction instructions but should not actually be included in the +; GHASH. However, any such blocks are all-zeroes, and the values that +; they're multiplied with are also all-zeroes. Therefore they just add +; 0 * 0 = 0 to the final GHASH result, which makes no difference. vmovdqu8 zmm30{k2}{z},[rsi] vpshufb zmm0,zmm1,zmm8 vpxord zmm0,zmm0,zmm10 - vpclmulqdq zmm7,zmm0,zmm30,0x00 - vpclmulqdq zmm1,zmm0,zmm30,0x01 - vpclmulqdq zmm2,zmm0,zmm30,0x10 - vpclmulqdq zmm3,zmm0,zmm30,0x11 + vpclmulqdq zmm7,zmm0,zmm30,0x00 ; a_L * b_L + vpclmulqdq zmm1,zmm0,zmm30,0x01 ; a_L * b_H + vpclmulqdq zmm2,zmm0,zmm30,0x10 ; a_H * b_L + vpclmulqdq zmm3,zmm0,zmm30,0x11 ; a_H * b_H vpxord zmm4,zmm4,zmm7 vpternlogd zmm5,zmm1,zmm2,0x96 vpxord zmm6,zmm6,zmm3 $L$reduce__func2: - +; Finally, do the GHASH reduction. vpclmulqdq zmm0,zmm31,zmm4,0x01 vpshufd zmm4,zmm4,0x4e vpternlogd zmm5,zmm4,zmm0,0x96 @@ -1340,11 +1342,11 @@ $L$reduce__func2: $L$done__func2: - +; Store the updated GHASH accumulator back to memory. vpshufb xmm10,xmm10,xmm8 vmovdqu XMMWORD[r12],xmm10 - vzeroupper + vzeroupper ; This is needed after using ymm or zmm registers. vmovdqa xmm6,XMMWORD[rsp] vmovdqa xmm7,XMMWORD[16+rsp] vmovdqa xmm8,XMMWORD[32+rsp] @@ -1385,7 +1387,7 @@ ALIGN 4 section .xdata rdata align=8 ALIGN 4 $L$SEH_info_gcm_gmult_vpclmulqdq_avx512_0: - DB 1 + DB 1 ; version 1, no flags DB $L$SEH_endprologue_gcm_gmult_vpclmulqdq_avx512_4-$L$SEH_begin_gcm_gmult_vpclmulqdq_avx512_1 DB 3 DB 0 @@ -1397,7 +1399,7 @@ $L$SEH_info_gcm_gmult_vpclmulqdq_avx512_0: DW 0 $L$SEH_info_gcm_ghash_vpclmulqdq_avx512_0: - DB 1 + DB 1 ; version 1, no flags DB $L$SEH_endprologue_gcm_ghash_vpclmulqdq_avx512_11-$L$SEH_begin_gcm_ghash_vpclmulqdq_avx512_1 DB 18 DB 0 @@ -1430,7 +1432,7 @@ $L$SEH_info_gcm_ghash_vpclmulqdq_avx512_0: DW 17 $L$SEH_info_aes_gcm_enc_update_vaes_avx512_0: - DB 1 + DB 1 ; version 1, no flags DB $L$SEH_endprologue_aes_gcm_enc_update_vaes_avx512_16-$L$SEH_begin_aes_gcm_enc_update_vaes_avx512_1 DB 25 DB 0 @@ -1476,7 +1478,7 @@ $L$SEH_info_aes_gcm_enc_update_vaes_avx512_0: DW 0 $L$SEH_info_aes_gcm_dec_update_vaes_avx512_0: - DB 1 + DB 1 ; version 1, no flags DB $L$SEH_endprologue_aes_gcm_dec_update_vaes_avx512_16-$L$SEH_begin_aes_gcm_dec_update_vaes_avx512_1 DB 25 DB 0 diff --git a/third_party/boringssl/gen/bcm/aesni-gcm-x86_64-apple.S b/third_party/boringssl/gen/bcm/aesni-gcm-x86_64-apple.S index e1247bc89..aa92f4f70 100644 --- a/third_party/boringssl/gen/bcm/aesni-gcm-x86_64-apple.S +++ b/third_party/boringssl/gen/bcm/aesni-gcm-x86_64-apple.S @@ -10,9 +10,9 @@ .p2align 5 _aesni_ctr32_ghash_6x: - vmovdqu 32(%r11),%xmm2 + vmovdqu 32(%r11),%xmm2 // borrow %xmm2, .Lone_msb subq $6,%rdx - vpxor %xmm4,%xmm4,%xmm4 + vpxor %xmm4,%xmm4,%xmm4 // %xmm4 = 0 vmovdqu 0-128(%rcx),%xmm15 vpaddb %xmm2,%xmm1,%xmm10 vpaddb %xmm2,%xmm10,%xmm11 @@ -20,46 +20,46 @@ _aesni_ctr32_ghash_6x: vpaddb %xmm2,%xmm12,%xmm13 vpaddb %xmm2,%xmm13,%xmm14 vpxor %xmm15,%xmm1,%xmm9 - vmovdqu %xmm4,16+8(%rsp) + vmovdqu %xmm4,16+8(%rsp) // "%xmm7" = 0 jmp L$oop6x .p2align 5 L$oop6x: addl $100663296,%ebx - jc L$handle_ctr32 - vmovdqu 0-32(%r9),%xmm3 - vpaddb %xmm2,%xmm14,%xmm1 + jc L$handle_ctr32 // discard ? + vmovdqu 0-32(%r9),%xmm3 // %xmm3^1 + vpaddb %xmm2,%xmm14,%xmm1 // next counter value vpxor %xmm15,%xmm10,%xmm10 vpxor %xmm15,%xmm11,%xmm11 L$resume_ctr32: - vmovdqu %xmm1,(%r8) + vmovdqu %xmm1,(%r8) // save next counter value vpclmulqdq $0x10,%xmm3,%xmm7,%xmm5 vpxor %xmm15,%xmm12,%xmm12 - vmovups 16-128(%rcx),%xmm2 + vmovups 16-128(%rcx),%xmm2 // borrow %xmm2 for %xmm15 vpclmulqdq $0x01,%xmm3,%xmm7,%xmm6 - - - - - - - - - - - - - - - - +// At this point, the current block of 96 (0x60) bytes has already been +// loaded into registers. Concurrently with processing it, we want to +// load the next 96 bytes of input for the next round. Obviously, we can +// only do this if there are at least 96 more bytes of input beyond the +// input we're currently processing, or else we'd read past the end of +// the input buffer. Here, we set |%r12| to 96 if there are at least 96 +// bytes of input beyond the 96 bytes we're already processing, and we +// set |%r12| to 0 otherwise. In the case where we set |%r12| to 96, +// we'll read in the next block so that it is in registers for the next +// loop iteration. In the case where we set |%r12| to 0, we'll re-read +// the current block and then ignore what we re-read. +// +// At this point, |%r14| points to the current (already read into +// registers) block, and |%r15| points to 2*96 bytes before the end of +// the input. Thus, |%r14| > |%r15| means that we do not have the next +// 96-byte block to read in, and |%r14| <= |%r15| means we do. xorq %r12,%r12 cmpq %r14,%r15 vaesenc %xmm2,%xmm9,%xmm9 - vmovdqu 48+8(%rsp),%xmm0 + vmovdqu 48+8(%rsp),%xmm0 // I[4] vpxor %xmm15,%xmm13,%xmm13 vpclmulqdq $0x00,%xmm3,%xmm7,%xmm1 vaesenc %xmm2,%xmm10,%xmm10 @@ -67,12 +67,12 @@ L$resume_ctr32: setnc %r12b vpclmulqdq $0x11,%xmm3,%xmm7,%xmm7 vaesenc %xmm2,%xmm11,%xmm11 - vmovdqu 16-32(%r9),%xmm3 + vmovdqu 16-32(%r9),%xmm3 // %xmm3^2 negq %r12 vaesenc %xmm2,%xmm12,%xmm12 vpxor %xmm5,%xmm6,%xmm6 vpclmulqdq $0x00,%xmm3,%xmm0,%xmm5 - vpxor %xmm4,%xmm8,%xmm8 + vpxor %xmm4,%xmm8,%xmm8 // modulo-scheduled vaesenc %xmm2,%xmm13,%xmm13 vpxor %xmm5,%xmm1,%xmm4 andq $0x60,%r12 @@ -83,9 +83,9 @@ L$resume_ctr32: vpclmulqdq $0x01,%xmm3,%xmm0,%xmm2 leaq (%r14,%r12,1),%r14 vaesenc %xmm15,%xmm9,%xmm9 - vpxor 16+8(%rsp),%xmm8,%xmm8 + vpxor 16+8(%rsp),%xmm8,%xmm8 // modulo-scheduled [vpxor %xmm7,%xmm8,%xmm8] vpclmulqdq $0x11,%xmm3,%xmm0,%xmm3 - vmovdqu 64+8(%rsp),%xmm0 + vmovdqu 64+8(%rsp),%xmm0 // I[3] vaesenc %xmm15,%xmm10,%xmm10 movbeq 88(%r14),%r13 vaesenc %xmm15,%xmm11,%xmm11 @@ -94,7 +94,7 @@ L$resume_ctr32: movq %r13,32+8(%rsp) vaesenc %xmm15,%xmm13,%xmm13 movq %r12,40+8(%rsp) - vmovdqu 48-32(%r9),%xmm5 + vmovdqu 48-32(%r9),%xmm5 // borrow %xmm5 for %xmm3^3 vaesenc %xmm15,%xmm14,%xmm14 vmovups 48-128(%rcx),%xmm15 @@ -108,11 +108,11 @@ L$resume_ctr32: vpclmulqdq $0x01,%xmm5,%xmm0,%xmm3 vaesenc %xmm15,%xmm11,%xmm11 vpclmulqdq $0x11,%xmm5,%xmm0,%xmm5 - vmovdqu 80+8(%rsp),%xmm0 + vmovdqu 80+8(%rsp),%xmm0 // I[2] vaesenc %xmm15,%xmm12,%xmm12 vaesenc %xmm15,%xmm13,%xmm13 vpxor %xmm1,%xmm4,%xmm4 - vmovdqu 64-32(%r9),%xmm1 + vmovdqu 64-32(%r9),%xmm1 // borrow %xmm1 for %xmm3^4 vaesenc %xmm15,%xmm14,%xmm14 vmovups 64-128(%rcx),%xmm15 @@ -128,13 +128,13 @@ L$resume_ctr32: vaesenc %xmm15,%xmm11,%xmm11 movbeq 64(%r14),%r12 vpclmulqdq $0x11,%xmm1,%xmm0,%xmm1 - vmovdqu 96+8(%rsp),%xmm0 + vmovdqu 96+8(%rsp),%xmm0 // I[1] vaesenc %xmm15,%xmm12,%xmm12 movq %r13,48+8(%rsp) vaesenc %xmm15,%xmm13,%xmm13 movq %r12,56+8(%rsp) vpxor %xmm2,%xmm4,%xmm4 - vmovdqu 96-32(%r9),%xmm2 + vmovdqu 96-32(%r9),%xmm2 // borrow %xmm2 for %xmm3^5 vaesenc %xmm15,%xmm14,%xmm14 vmovups 80-128(%rcx),%xmm15 @@ -147,7 +147,7 @@ L$resume_ctr32: movbeq 56(%r14),%r13 vpxor %xmm1,%xmm7,%xmm7 vpclmulqdq $0x01,%xmm2,%xmm0,%xmm1 - vpxor 112+8(%rsp),%xmm8,%xmm8 + vpxor 112+8(%rsp),%xmm8,%xmm8 // accumulate I[0] vaesenc %xmm15,%xmm11,%xmm11 movbeq 48(%r14),%r12 vpclmulqdq $0x11,%xmm2,%xmm0,%xmm2 @@ -156,7 +156,7 @@ L$resume_ctr32: vaesenc %xmm15,%xmm13,%xmm13 movq %r12,72+8(%rsp) vpxor %xmm3,%xmm4,%xmm4 - vmovdqu 112-32(%r9),%xmm3 + vmovdqu 112-32(%r9),%xmm3 // %xmm3^6 vaesenc %xmm15,%xmm14,%xmm14 vmovups 96-128(%rcx),%xmm15 @@ -183,7 +183,7 @@ L$resume_ctr32: vmovups 112-128(%rcx),%xmm15 vpslldq $8,%xmm6,%xmm5 vpxor %xmm2,%xmm4,%xmm4 - vmovdqu 16(%r11),%xmm3 + vmovdqu 16(%r11),%xmm3 // .Lpoly vaesenc %xmm15,%xmm9,%xmm9 vpxor %xmm8,%xmm7,%xmm7 @@ -192,13 +192,13 @@ L$resume_ctr32: movbeq 24(%r14),%r13 vaesenc %xmm15,%xmm11,%xmm11 movbeq 16(%r14),%r12 - vpalignr $8,%xmm4,%xmm4,%xmm0 + vpalignr $8,%xmm4,%xmm4,%xmm0 // 1st phase vpclmulqdq $0x10,%xmm3,%xmm4,%xmm4 movq %r13,96+8(%rsp) vaesenc %xmm15,%xmm12,%xmm12 movq %r12,104+8(%rsp) vaesenc %xmm15,%xmm13,%xmm13 - vmovups 128-128(%rcx),%xmm1 + vmovups 128-128(%rcx),%xmm1 // borrow %xmm1 for %xmm15 vaesenc %xmm15,%xmm14,%xmm14 vaesenc %xmm1,%xmm9,%xmm9 @@ -215,7 +215,7 @@ L$resume_ctr32: vaesenc %xmm1,%xmm14,%xmm14 vmovups 160-128(%rcx),%xmm1 cmpl $11,%r10d - jb L$enc_tail + jb L$enc_tail // 128-bit key vaesenc %xmm15,%xmm9,%xmm9 vaesenc %xmm15,%xmm10,%xmm10 @@ -232,7 +232,7 @@ L$resume_ctr32: vmovups 176-128(%rcx),%xmm15 vaesenc %xmm1,%xmm14,%xmm14 vmovups 192-128(%rcx),%xmm1 - je L$enc_tail + je L$enc_tail // 192-bit key vaesenc %xmm15,%xmm9,%xmm9 vaesenc %xmm15,%xmm10,%xmm10 @@ -249,16 +249,16 @@ L$resume_ctr32: vmovups 208-128(%rcx),%xmm15 vaesenc %xmm1,%xmm14,%xmm14 vmovups 224-128(%rcx),%xmm1 - jmp L$enc_tail + jmp L$enc_tail // 256-bit key .p2align 5 L$handle_ctr32: - vmovdqu (%r11),%xmm0 - vpshufb %xmm0,%xmm1,%xmm6 - vmovdqu 48(%r11),%xmm5 - vpaddd 64(%r11),%xmm6,%xmm10 + vmovdqu (%r11),%xmm0 // borrow %xmm0 for .Lbswap_mask + vpshufb %xmm0,%xmm1,%xmm6 // byte-swap counter + vmovdqu 48(%r11),%xmm5 // borrow %xmm5, .Ltwo_lsb + vpaddd 64(%r11),%xmm6,%xmm10 // .Lone_lsb vpaddd %xmm5,%xmm6,%xmm11 - vmovdqu 0-32(%r9),%xmm3 + vmovdqu 0-32(%r9),%xmm3 // %xmm3^1 vpaddd %xmm5,%xmm10,%xmm12 vpshufb %xmm0,%xmm10,%xmm10 vpaddd %xmm5,%xmm11,%xmm13 @@ -267,17 +267,17 @@ L$handle_ctr32: vpaddd %xmm5,%xmm12,%xmm14 vpshufb %xmm0,%xmm12,%xmm12 vpxor %xmm15,%xmm11,%xmm11 - vpaddd %xmm5,%xmm13,%xmm1 + vpaddd %xmm5,%xmm13,%xmm1 // byte-swapped next counter value vpshufb %xmm0,%xmm13,%xmm13 vpshufb %xmm0,%xmm14,%xmm14 - vpshufb %xmm0,%xmm1,%xmm1 + vpshufb %xmm0,%xmm1,%xmm1 // next counter value jmp L$resume_ctr32 .p2align 5 L$enc_tail: vaesenc %xmm15,%xmm9,%xmm9 - vmovdqu %xmm7,16+8(%rsp) - vpalignr $8,%xmm4,%xmm4,%xmm8 + vmovdqu %xmm7,16+8(%rsp) // postpone vpxor %xmm7,%xmm8,%xmm8 + vpalignr $8,%xmm4,%xmm4,%xmm8 // 2nd phase vaesenc %xmm15,%xmm10,%xmm10 vpclmulqdq $0x10,%xmm3,%xmm4,%xmm4 vpxor 0(%rdi),%xmm1,%xmm2 @@ -290,16 +290,16 @@ L$enc_tail: vaesenc %xmm15,%xmm14,%xmm14 vpxor 64(%rdi),%xmm1,%xmm7 vpxor 80(%rdi),%xmm1,%xmm3 - vmovdqu (%r8),%xmm1 + vmovdqu (%r8),%xmm1 // load next counter value vaesenclast %xmm2,%xmm9,%xmm9 - vmovdqu 32(%r11),%xmm2 + vmovdqu 32(%r11),%xmm2 // borrow %xmm2, .Lone_msb vaesenclast %xmm0,%xmm10,%xmm10 vpaddb %xmm2,%xmm1,%xmm0 movq %r13,112+8(%rsp) leaq 96(%rdi),%rdi - - prefetcht0 512(%rdi) +// These two prefetches were added in BoringSSL. See change that added them. + prefetcht0 512(%rdi) // We use 96-byte block so prefetch 2 lines (128 bytes) prefetcht0 576(%rdi) vaesenclast %xmm5,%xmm11,%xmm11 vpaddb %xmm2,%xmm0,%xmm5 @@ -317,24 +317,24 @@ L$enc_tail: subq $0x6,%rdx jc L$6x_done - vmovups %xmm9,-96(%rsi) + vmovups %xmm9,-96(%rsi) // save output vpxor %xmm15,%xmm1,%xmm9 vmovups %xmm10,-80(%rsi) - vmovdqa %xmm0,%xmm10 + vmovdqa %xmm0,%xmm10 // 0 latency vmovups %xmm11,-64(%rsi) - vmovdqa %xmm5,%xmm11 + vmovdqa %xmm5,%xmm11 // 0 latency vmovups %xmm12,-48(%rsi) - vmovdqa %xmm6,%xmm12 + vmovdqa %xmm6,%xmm12 // 0 latency vmovups %xmm13,-32(%rsi) - vmovdqa %xmm7,%xmm13 + vmovdqa %xmm7,%xmm13 // 0 latency vmovups %xmm14,-16(%rsi) - vmovdqa %xmm3,%xmm14 - vmovdqu 32+8(%rsp),%xmm7 + vmovdqa %xmm3,%xmm14 // 0 latency + vmovdqu 32+8(%rsp),%xmm7 // I[5] jmp L$oop6x L$6x_done: - vpxor 16+8(%rsp),%xmm8,%xmm8 - vpxor %xmm4,%xmm8,%xmm8 + vpxor 16+8(%rsp),%xmm8,%xmm8 // modulo-scheduled + vpxor %xmm4,%xmm8,%xmm8 // modulo-scheduled ret @@ -343,21 +343,22 @@ L$6x_done: .private_extern _aesni_gcm_decrypt .p2align 5 +.alt_entry _aesni_gcm_decrypt _aesni_gcm_decrypt: _CET_ENDBR xorq %rax,%rax - - - cmpq $0x60,%rdx +// We call |_aesni_ctr32_ghash_6x|, which requires at least 96 (0x60) +// bytes of input. + cmpq $0x60,%rdx // minimal accepted length jb L$gcm_dec_abort pushq %rbp - movq %rsp,%rbp + movq %rsp,%rbp // save stack pointer pushq %rbx @@ -377,17 +378,17 @@ _CET_ENDBR vzeroupper movq 16(%rbp),%r12 - vmovdqu (%r8),%xmm1 + vmovdqu (%r8),%xmm1 // input counter value addq $-128,%rsp movl 12(%r8),%ebx leaq L$bswap_mask(%rip),%r11 - leaq -128(%rcx),%r14 - movq $0xf80,%r15 - vmovdqu (%r12),%xmm8 - andq $-128,%rsp - vmovdqu (%r11),%xmm0 - leaq 128(%rcx),%rcx - leaq 32(%r9),%r9 + leaq -128(%rcx),%r14 // borrow %r14 + movq $0xf80,%r15 // borrow %r15 + vmovdqu (%r12),%xmm8 // load Xi + andq $-128,%rsp // ensure stack alignment + vmovdqu (%r11),%xmm0 // borrow %xmm0 for .Lbswap_mask + leaq 128(%rcx),%rcx // size optimization + leaq 32(%r9),%r9 // size optimization movl 240-128(%rcx),%r10d vpshufb %xmm0,%xmm8,%xmm8 @@ -397,26 +398,26 @@ _CET_ENDBR jc L$dec_no_key_aliasing cmpq $768,%r15 jnc L$dec_no_key_aliasing - subq %r15,%rsp + subq %r15,%rsp // avoid aliasing with key L$dec_no_key_aliasing: - vmovdqu 80(%rdi),%xmm7 + vmovdqu 80(%rdi),%xmm7 // I[5] movq %rdi,%r14 vmovdqu 64(%rdi),%xmm4 - - - - - - +// |_aesni_ctr32_ghash_6x| requires |%r15| to point to 2*96 (0xc0) +// bytes before the end of the input. Note, in particular, that this is +// correct even if |%rdx| is not an even multiple of 96 or 16. XXX: This +// seems to require that |%rdi| + |%rdx| >= 2*96 (0xc0); i.e. |%rdi| must +// not be near the very beginning of the address space when |%rdx| < 2*96 +// (0xc0). leaq -192(%rdi,%rdx,1),%r15 vmovdqu 48(%rdi),%xmm5 shrq $4,%rdx xorq %rax,%rax vmovdqu 32(%rdi),%xmm6 - vpshufb %xmm0,%xmm7,%xmm7 + vpshufb %xmm0,%xmm7,%xmm7 // passed to _aesni_ctr32_ghash_6x vmovdqu 16(%rdi),%xmm2 vpshufb %xmm0,%xmm4,%xmm4 vmovdqu (%rdi),%xmm3 @@ -433,18 +434,18 @@ L$dec_no_key_aliasing: call _aesni_ctr32_ghash_6x movq 16(%rbp),%r12 - vmovups %xmm9,-96(%rsi) + vmovups %xmm9,-96(%rsi) // save output vmovups %xmm10,-80(%rsi) vmovups %xmm11,-64(%rsi) vmovups %xmm12,-48(%rsi) vmovups %xmm13,-32(%rsi) vmovups %xmm14,-16(%rsi) - vpshufb (%r11),%xmm8,%xmm8 - vmovdqu %xmm8,(%r12) + vpshufb (%r11),%xmm8,%xmm8 // .Lbswap_mask + vmovdqu %xmm8,(%r12) // output Xi vzeroupper - leaq -40(%rbp),%rsp + leaq -40(%rbp),%rsp // restore %rsp to fixed allocation popq %r15 @@ -465,10 +466,11 @@ L$gcm_dec_abort: .p2align 5 +.alt_entry _aesni_ctr32_6x _aesni_ctr32_6x: - vmovdqu 0-128(%rcx),%xmm4 - vmovdqu 32(%r11),%xmm2 + vmovdqu 0-128(%rcx),%xmm4 // borrow %xmm4 for %xmm15 + vmovdqu 32(%r11),%xmm2 // borrow %xmm2, .Lone_msb leaq -1(%r10),%r13 vmovups 16-128(%rcx),%xmm15 leaq 32-128(%rcx),%r12 @@ -501,7 +503,7 @@ L$oop_ctr32: decl %r13d jnz L$oop_ctr32 - vmovdqu (%r12),%xmm3 + vmovdqu (%r12),%xmm3 // last round key vaesenc %xmm15,%xmm9,%xmm9 vpxor 0(%rdi),%xmm3,%xmm4 vaesenc %xmm15,%xmm10,%xmm10 @@ -533,9 +535,9 @@ L$oop_ctr32: ret .p2align 5 L$handle_ctr32_2: - vpshufb %xmm0,%xmm1,%xmm6 - vmovdqu 48(%r11),%xmm5 - vpaddd 64(%r11),%xmm6,%xmm10 + vpshufb %xmm0,%xmm1,%xmm6 // byte-swap counter + vmovdqu 48(%r11),%xmm5 // borrow %xmm5, .Ltwo_lsb + vpaddd 64(%r11),%xmm6,%xmm10 // .Lone_lsb vpaddd %xmm5,%xmm6,%xmm11 vpaddd %xmm5,%xmm10,%xmm12 vpshufb %xmm0,%xmm10,%xmm10 @@ -545,12 +547,12 @@ L$handle_ctr32_2: vpaddd %xmm5,%xmm12,%xmm14 vpshufb %xmm0,%xmm12,%xmm12 vpxor %xmm4,%xmm11,%xmm11 - vpaddd %xmm5,%xmm13,%xmm1 + vpaddd %xmm5,%xmm13,%xmm1 // byte-swapped next counter value vpshufb %xmm0,%xmm13,%xmm13 vpxor %xmm4,%xmm12,%xmm12 vpshufb %xmm0,%xmm14,%xmm14 vpxor %xmm4,%xmm13,%xmm13 - vpshufb %xmm0,%xmm1,%xmm1 + vpshufb %xmm0,%xmm1,%xmm1 // next counter value vpxor %xmm4,%xmm14,%xmm14 jmp L$oop_ctr32 @@ -560,6 +562,7 @@ L$handle_ctr32_2: .private_extern _aesni_gcm_encrypt .p2align 5 +.alt_entry _aesni_gcm_encrypt _aesni_gcm_encrypt: @@ -570,16 +573,16 @@ _CET_ENDBR #endif xorq %rax,%rax - - - - cmpq $288,%rdx +// We call |_aesni_ctr32_6x| twice, each call consuming 96 bytes of +// input. Then we call |_aesni_ctr32_ghash_6x|, which requires at +// least 96 more bytes of input. + cmpq $288,%rdx // minimal accepted length jb L$gcm_enc_abort pushq %rbp - movq %rsp,%rbp + movq %rsp,%rbp // save stack pointer pushq %rbx @@ -598,15 +601,15 @@ _CET_ENDBR vzeroupper - vmovdqu (%r8),%xmm1 + vmovdqu (%r8),%xmm1 // input counter value addq $-128,%rsp movl 12(%r8),%ebx leaq L$bswap_mask(%rip),%r11 - leaq -128(%rcx),%r14 - movq $0xf80,%r15 - leaq 128(%rcx),%rcx - vmovdqu (%r11),%xmm0 - andq $-128,%rsp + leaq -128(%rcx),%r14 // borrow %r14 + movq $0xf80,%r15 // borrow %r15 + leaq 128(%rcx),%rcx // size optimization + vmovdqu (%r11),%xmm0 // borrow %xmm0 for .Lbswap_mask + andq $-128,%rsp // ensure stack alignment movl 240-128(%rcx),%r10d andq %r15,%r14 @@ -615,24 +618,24 @@ _CET_ENDBR jc L$enc_no_key_aliasing cmpq $768,%r15 jnc L$enc_no_key_aliasing - subq %r15,%rsp + subq %r15,%rsp // avoid aliasing with key L$enc_no_key_aliasing: movq %rsi,%r14 - - - - - - - +// |_aesni_ctr32_ghash_6x| requires |%r15| to point to 2*96 (0xc0) +// bytes before the end of the input. Note, in particular, that this is +// correct even if |%rdx| is not an even multiple of 96 or 16. Unlike in +// the decryption case, there's no caveat that |%rsi| must not be near +// the very beginning of the address space, because we know that +// |%rdx| >= 3*96 from the check above, and so we know +// |%rsi| + |%rdx| >= 2*96 (0xc0). leaq -192(%rsi,%rdx,1),%r15 shrq $4,%rdx call _aesni_ctr32_6x - vpshufb %xmm0,%xmm9,%xmm8 + vpshufb %xmm0,%xmm9,%xmm8 // save bswapped output on stack vpshufb %xmm0,%xmm10,%xmm2 vmovdqu %xmm8,112(%rsp) vpshufb %xmm0,%xmm11,%xmm4 @@ -641,26 +644,26 @@ L$enc_no_key_aliasing: vmovdqu %xmm4,80(%rsp) vpshufb %xmm0,%xmm13,%xmm6 vmovdqu %xmm5,64(%rsp) - vpshufb %xmm0,%xmm14,%xmm7 + vpshufb %xmm0,%xmm14,%xmm7 // passed to _aesni_ctr32_ghash_6x vmovdqu %xmm6,48(%rsp) call _aesni_ctr32_6x movq 16(%rbp),%r12 - leaq 32(%r9),%r9 - vmovdqu (%r12),%xmm8 + leaq 32(%r9),%r9 // size optimization + vmovdqu (%r12),%xmm8 // load Xi subq $12,%rdx movq $192,%rax vpshufb %xmm0,%xmm8,%xmm8 call _aesni_ctr32_ghash_6x - vmovdqu 32(%rsp),%xmm7 - vmovdqu (%r11),%xmm0 - vmovdqu 0-32(%r9),%xmm3 + vmovdqu 32(%rsp),%xmm7 // I[5] + vmovdqu (%r11),%xmm0 // borrow %xmm0 for .Lbswap_mask + vmovdqu 0-32(%r9),%xmm3 // %xmm3^1 vpunpckhqdq %xmm7,%xmm7,%xmm1 - vmovdqu 32-32(%r9),%xmm15 - vmovups %xmm9,-96(%rsi) - vpshufb %xmm0,%xmm9,%xmm9 + vmovdqu 32-32(%r9),%xmm15 // borrow %xmm15 for + vmovups %xmm9,-96(%rsi) // save output + vpshufb %xmm0,%xmm9,%xmm9 // but keep bswapped copy vpxor %xmm7,%xmm1,%xmm1 vmovups %xmm10,-80(%rsi) vpshufb %xmm0,%xmm10,%xmm10 @@ -672,18 +675,18 @@ L$enc_no_key_aliasing: vpshufb %xmm0,%xmm13,%xmm13 vmovups %xmm14,-16(%rsi) vpshufb %xmm0,%xmm14,%xmm14 - vmovdqu %xmm9,16(%rsp) - vmovdqu 48(%rsp),%xmm6 - vmovdqu 16-32(%r9),%xmm0 + vmovdqu %xmm9,16(%rsp) // free %xmm9 + vmovdqu 48(%rsp),%xmm6 // I[4] + vmovdqu 16-32(%r9),%xmm0 // borrow %xmm0 for %xmm3^2 vpunpckhqdq %xmm6,%xmm6,%xmm2 vpclmulqdq $0x00,%xmm3,%xmm7,%xmm5 vpxor %xmm6,%xmm2,%xmm2 vpclmulqdq $0x11,%xmm3,%xmm7,%xmm7 vpclmulqdq $0x00,%xmm15,%xmm1,%xmm1 - vmovdqu 64(%rsp),%xmm9 + vmovdqu 64(%rsp),%xmm9 // I[3] vpclmulqdq $0x00,%xmm0,%xmm6,%xmm4 - vmovdqu 48-32(%r9),%xmm3 + vmovdqu 48-32(%r9),%xmm3 // %xmm3^3 vpxor %xmm5,%xmm4,%xmm4 vpunpckhqdq %xmm9,%xmm9,%xmm5 vpclmulqdq $0x11,%xmm0,%xmm6,%xmm6 @@ -693,9 +696,9 @@ L$enc_no_key_aliasing: vmovdqu 80-32(%r9),%xmm15 vpxor %xmm1,%xmm2,%xmm2 - vmovdqu 80(%rsp),%xmm1 + vmovdqu 80(%rsp),%xmm1 // I[2] vpclmulqdq $0x00,%xmm3,%xmm9,%xmm7 - vmovdqu 64-32(%r9),%xmm0 + vmovdqu 64-32(%r9),%xmm0 // borrow %xmm0 for %xmm3^4 vpxor %xmm4,%xmm7,%xmm7 vpunpckhqdq %xmm1,%xmm1,%xmm4 vpclmulqdq $0x11,%xmm3,%xmm9,%xmm9 @@ -704,9 +707,9 @@ L$enc_no_key_aliasing: vpclmulqdq $0x00,%xmm15,%xmm5,%xmm5 vpxor %xmm2,%xmm5,%xmm5 - vmovdqu 96(%rsp),%xmm2 + vmovdqu 96(%rsp),%xmm2 // I[1] vpclmulqdq $0x00,%xmm0,%xmm1,%xmm6 - vmovdqu 96-32(%r9),%xmm3 + vmovdqu 96-32(%r9),%xmm3 // %xmm3^5 vpxor %xmm7,%xmm6,%xmm6 vpunpckhqdq %xmm2,%xmm2,%xmm7 vpclmulqdq $0x11,%xmm0,%xmm1,%xmm1 @@ -716,9 +719,9 @@ L$enc_no_key_aliasing: vmovdqu 128-32(%r9),%xmm15 vpxor %xmm5,%xmm4,%xmm4 - vpxor 112(%rsp),%xmm8,%xmm8 + vpxor 112(%rsp),%xmm8,%xmm8 // accumulate I[0] vpclmulqdq $0x00,%xmm3,%xmm2,%xmm5 - vmovdqu 112-32(%r9),%xmm0 + vmovdqu 112-32(%r9),%xmm0 // borrow %xmm0 for %xmm3^6 vpunpckhqdq %xmm8,%xmm8,%xmm9 vpxor %xmm6,%xmm5,%xmm5 vpclmulqdq $0x11,%xmm3,%xmm2,%xmm2 @@ -728,7 +731,7 @@ L$enc_no_key_aliasing: vpxor %xmm4,%xmm7,%xmm4 vpclmulqdq $0x00,%xmm0,%xmm8,%xmm6 - vmovdqu 0-32(%r9),%xmm3 + vmovdqu 0-32(%r9),%xmm3 // %xmm3^1 vpunpckhqdq %xmm14,%xmm14,%xmm1 vpclmulqdq $0x11,%xmm0,%xmm8,%xmm8 vpxor %xmm14,%xmm1,%xmm1 @@ -738,8 +741,8 @@ L$enc_no_key_aliasing: vpxor %xmm2,%xmm8,%xmm7 vpxor %xmm4,%xmm9,%xmm6 - vmovdqu 16-32(%r9),%xmm0 - vpxor %xmm5,%xmm7,%xmm9 + vmovdqu 16-32(%r9),%xmm0 // borrow %xmm0 for %xmm3^2 + vpxor %xmm5,%xmm7,%xmm9 // aggregated Karatsuba post-processing vpclmulqdq $0x00,%xmm3,%xmm14,%xmm4 vpxor %xmm9,%xmm6,%xmm6 vpunpckhqdq %xmm13,%xmm13,%xmm2 @@ -752,25 +755,25 @@ L$enc_no_key_aliasing: vpxor %xmm6,%xmm7,%xmm7 vpclmulqdq $0x00,%xmm0,%xmm13,%xmm5 - vmovdqu 48-32(%r9),%xmm3 + vmovdqu 48-32(%r9),%xmm3 // %xmm3^3 vpxor %xmm4,%xmm5,%xmm5 vpunpckhqdq %xmm12,%xmm12,%xmm9 vpclmulqdq $0x11,%xmm0,%xmm13,%xmm13 vpxor %xmm12,%xmm9,%xmm9 vpxor %xmm14,%xmm13,%xmm13 - vpalignr $8,%xmm8,%xmm8,%xmm14 + vpalignr $8,%xmm8,%xmm8,%xmm14 // 1st phase vpclmulqdq $0x10,%xmm15,%xmm2,%xmm2 vmovdqu 80-32(%r9),%xmm15 vpxor %xmm1,%xmm2,%xmm2 vpclmulqdq $0x00,%xmm3,%xmm12,%xmm4 - vmovdqu 64-32(%r9),%xmm0 + vmovdqu 64-32(%r9),%xmm0 // borrow %xmm0 for %xmm3^4 vpxor %xmm5,%xmm4,%xmm4 vpunpckhqdq %xmm11,%xmm11,%xmm1 vpclmulqdq $0x11,%xmm3,%xmm12,%xmm12 vpxor %xmm11,%xmm1,%xmm1 vpxor %xmm13,%xmm12,%xmm12 - vxorps 16(%rsp),%xmm7,%xmm7 + vxorps 16(%rsp),%xmm7,%xmm7 // accumulate %xmm9 vpclmulqdq $0x00,%xmm15,%xmm9,%xmm9 vpxor %xmm2,%xmm9,%xmm9 @@ -778,12 +781,12 @@ L$enc_no_key_aliasing: vxorps %xmm14,%xmm8,%xmm8 vpclmulqdq $0x00,%xmm0,%xmm11,%xmm5 - vmovdqu 96-32(%r9),%xmm3 + vmovdqu 96-32(%r9),%xmm3 // %xmm3^5 vpxor %xmm4,%xmm5,%xmm5 vpunpckhqdq %xmm10,%xmm10,%xmm2 vpclmulqdq $0x11,%xmm0,%xmm11,%xmm11 vpxor %xmm10,%xmm2,%xmm2 - vpalignr $8,%xmm8,%xmm8,%xmm14 + vpalignr $8,%xmm8,%xmm8,%xmm14 // 2nd phase vpxor %xmm12,%xmm11,%xmm11 vpclmulqdq $0x10,%xmm15,%xmm1,%xmm1 vmovdqu 128-32(%r9),%xmm15 @@ -794,7 +797,7 @@ L$enc_no_key_aliasing: vxorps %xmm14,%xmm8,%xmm8 vpclmulqdq $0x00,%xmm3,%xmm10,%xmm4 - vmovdqu 112-32(%r9),%xmm0 + vmovdqu 112-32(%r9),%xmm0 // borrow %xmm0 for %xmm3^6 vpxor %xmm5,%xmm4,%xmm4 vpunpckhqdq %xmm8,%xmm8,%xmm9 vpclmulqdq $0x11,%xmm3,%xmm10,%xmm10 @@ -810,28 +813,28 @@ L$enc_no_key_aliasing: vpxor %xmm10,%xmm7,%xmm7 vpxor %xmm2,%xmm6,%xmm6 - vpxor %xmm5,%xmm7,%xmm4 + vpxor %xmm5,%xmm7,%xmm4 // aggregated Karatsuba post-processing vpxor %xmm4,%xmm6,%xmm6 vpslldq $8,%xmm6,%xmm1 - vmovdqu 16(%r11),%xmm3 + vmovdqu 16(%r11),%xmm3 // .Lpoly vpsrldq $8,%xmm6,%xmm6 vpxor %xmm1,%xmm5,%xmm8 vpxor %xmm6,%xmm7,%xmm7 - vpalignr $8,%xmm8,%xmm8,%xmm2 + vpalignr $8,%xmm8,%xmm8,%xmm2 // 1st phase vpclmulqdq $0x10,%xmm3,%xmm8,%xmm8 vpxor %xmm2,%xmm8,%xmm8 - vpalignr $8,%xmm8,%xmm8,%xmm2 + vpalignr $8,%xmm8,%xmm8,%xmm2 // 2nd phase vpclmulqdq $0x10,%xmm3,%xmm8,%xmm8 vpxor %xmm7,%xmm2,%xmm2 vpxor %xmm2,%xmm8,%xmm8 movq 16(%rbp),%r12 - vpshufb (%r11),%xmm8,%xmm8 - vmovdqu %xmm8,(%r12) + vpshufb (%r11),%xmm8,%xmm8 // .Lbswap_mask + vmovdqu %xmm8,(%r12) // output Xi vzeroupper - leaq -40(%rbp),%rsp + leaq -40(%rbp),%rsp // restore %rsp to fixed allocation popq %r15 @@ -851,7 +854,9 @@ L$gcm_enc_abort: .section __DATA,__const + .p2align 6 +aesni_gcm_constants: L$bswap_mask: .byte 15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0 L$poly: diff --git a/third_party/boringssl/gen/bcm/aesni-gcm-x86_64-linux.S b/third_party/boringssl/gen/bcm/aesni-gcm-x86_64-linux.S index 774a8d12d..723bb2e9f 100644 --- a/third_party/boringssl/gen/bcm/aesni-gcm-x86_64-linux.S +++ b/third_party/boringssl/gen/bcm/aesni-gcm-x86_64-linux.S @@ -10,9 +10,9 @@ .align 32 _aesni_ctr32_ghash_6x: .cfi_startproc - vmovdqu 32(%r11),%xmm2 + vmovdqu 32(%r11),%xmm2 // borrow %xmm2, .Lone_msb subq $6,%rdx - vpxor %xmm4,%xmm4,%xmm4 + vpxor %xmm4,%xmm4,%xmm4 // %xmm4 = 0 vmovdqu 0-128(%rcx),%xmm15 vpaddb %xmm2,%xmm1,%xmm10 vpaddb %xmm2,%xmm10,%xmm11 @@ -20,46 +20,46 @@ _aesni_ctr32_ghash_6x: vpaddb %xmm2,%xmm12,%xmm13 vpaddb %xmm2,%xmm13,%xmm14 vpxor %xmm15,%xmm1,%xmm9 - vmovdqu %xmm4,16+8(%rsp) + vmovdqu %xmm4,16+8(%rsp) // "%xmm7" = 0 jmp .Loop6x .align 32 .Loop6x: addl $100663296,%ebx - jc .Lhandle_ctr32 - vmovdqu 0-32(%r9),%xmm3 - vpaddb %xmm2,%xmm14,%xmm1 + jc .Lhandle_ctr32 // discard ? + vmovdqu 0-32(%r9),%xmm3 // %xmm3^1 + vpaddb %xmm2,%xmm14,%xmm1 // next counter value vpxor %xmm15,%xmm10,%xmm10 vpxor %xmm15,%xmm11,%xmm11 .Lresume_ctr32: - vmovdqu %xmm1,(%r8) + vmovdqu %xmm1,(%r8) // save next counter value vpclmulqdq $0x10,%xmm3,%xmm7,%xmm5 vpxor %xmm15,%xmm12,%xmm12 - vmovups 16-128(%rcx),%xmm2 + vmovups 16-128(%rcx),%xmm2 // borrow %xmm2 for %xmm15 vpclmulqdq $0x01,%xmm3,%xmm7,%xmm6 - - - - - - - - - - - - - - - - +// At this point, the current block of 96 (0x60) bytes has already been +// loaded into registers. Concurrently with processing it, we want to +// load the next 96 bytes of input for the next round. Obviously, we can +// only do this if there are at least 96 more bytes of input beyond the +// input we're currently processing, or else we'd read past the end of +// the input buffer. Here, we set |%r12| to 96 if there are at least 96 +// bytes of input beyond the 96 bytes we're already processing, and we +// set |%r12| to 0 otherwise. In the case where we set |%r12| to 96, +// we'll read in the next block so that it is in registers for the next +// loop iteration. In the case where we set |%r12| to 0, we'll re-read +// the current block and then ignore what we re-read. +// +// At this point, |%r14| points to the current (already read into +// registers) block, and |%r15| points to 2*96 bytes before the end of +// the input. Thus, |%r14| > |%r15| means that we do not have the next +// 96-byte block to read in, and |%r14| <= |%r15| means we do. xorq %r12,%r12 cmpq %r14,%r15 vaesenc %xmm2,%xmm9,%xmm9 - vmovdqu 48+8(%rsp),%xmm0 + vmovdqu 48+8(%rsp),%xmm0 // I[4] vpxor %xmm15,%xmm13,%xmm13 vpclmulqdq $0x00,%xmm3,%xmm7,%xmm1 vaesenc %xmm2,%xmm10,%xmm10 @@ -67,12 +67,12 @@ _aesni_ctr32_ghash_6x: setnc %r12b vpclmulqdq $0x11,%xmm3,%xmm7,%xmm7 vaesenc %xmm2,%xmm11,%xmm11 - vmovdqu 16-32(%r9),%xmm3 + vmovdqu 16-32(%r9),%xmm3 // %xmm3^2 negq %r12 vaesenc %xmm2,%xmm12,%xmm12 vpxor %xmm5,%xmm6,%xmm6 vpclmulqdq $0x00,%xmm3,%xmm0,%xmm5 - vpxor %xmm4,%xmm8,%xmm8 + vpxor %xmm4,%xmm8,%xmm8 // modulo-scheduled vaesenc %xmm2,%xmm13,%xmm13 vpxor %xmm5,%xmm1,%xmm4 andq $0x60,%r12 @@ -83,9 +83,9 @@ _aesni_ctr32_ghash_6x: vpclmulqdq $0x01,%xmm3,%xmm0,%xmm2 leaq (%r14,%r12,1),%r14 vaesenc %xmm15,%xmm9,%xmm9 - vpxor 16+8(%rsp),%xmm8,%xmm8 + vpxor 16+8(%rsp),%xmm8,%xmm8 // modulo-scheduled [vpxor %xmm7,%xmm8,%xmm8] vpclmulqdq $0x11,%xmm3,%xmm0,%xmm3 - vmovdqu 64+8(%rsp),%xmm0 + vmovdqu 64+8(%rsp),%xmm0 // I[3] vaesenc %xmm15,%xmm10,%xmm10 movbeq 88(%r14),%r13 vaesenc %xmm15,%xmm11,%xmm11 @@ -94,7 +94,7 @@ _aesni_ctr32_ghash_6x: movq %r13,32+8(%rsp) vaesenc %xmm15,%xmm13,%xmm13 movq %r12,40+8(%rsp) - vmovdqu 48-32(%r9),%xmm5 + vmovdqu 48-32(%r9),%xmm5 // borrow %xmm5 for %xmm3^3 vaesenc %xmm15,%xmm14,%xmm14 vmovups 48-128(%rcx),%xmm15 @@ -108,11 +108,11 @@ _aesni_ctr32_ghash_6x: vpclmulqdq $0x01,%xmm5,%xmm0,%xmm3 vaesenc %xmm15,%xmm11,%xmm11 vpclmulqdq $0x11,%xmm5,%xmm0,%xmm5 - vmovdqu 80+8(%rsp),%xmm0 + vmovdqu 80+8(%rsp),%xmm0 // I[2] vaesenc %xmm15,%xmm12,%xmm12 vaesenc %xmm15,%xmm13,%xmm13 vpxor %xmm1,%xmm4,%xmm4 - vmovdqu 64-32(%r9),%xmm1 + vmovdqu 64-32(%r9),%xmm1 // borrow %xmm1 for %xmm3^4 vaesenc %xmm15,%xmm14,%xmm14 vmovups 64-128(%rcx),%xmm15 @@ -128,13 +128,13 @@ _aesni_ctr32_ghash_6x: vaesenc %xmm15,%xmm11,%xmm11 movbeq 64(%r14),%r12 vpclmulqdq $0x11,%xmm1,%xmm0,%xmm1 - vmovdqu 96+8(%rsp),%xmm0 + vmovdqu 96+8(%rsp),%xmm0 // I[1] vaesenc %xmm15,%xmm12,%xmm12 movq %r13,48+8(%rsp) vaesenc %xmm15,%xmm13,%xmm13 movq %r12,56+8(%rsp) vpxor %xmm2,%xmm4,%xmm4 - vmovdqu 96-32(%r9),%xmm2 + vmovdqu 96-32(%r9),%xmm2 // borrow %xmm2 for %xmm3^5 vaesenc %xmm15,%xmm14,%xmm14 vmovups 80-128(%rcx),%xmm15 @@ -147,7 +147,7 @@ _aesni_ctr32_ghash_6x: movbeq 56(%r14),%r13 vpxor %xmm1,%xmm7,%xmm7 vpclmulqdq $0x01,%xmm2,%xmm0,%xmm1 - vpxor 112+8(%rsp),%xmm8,%xmm8 + vpxor 112+8(%rsp),%xmm8,%xmm8 // accumulate I[0] vaesenc %xmm15,%xmm11,%xmm11 movbeq 48(%r14),%r12 vpclmulqdq $0x11,%xmm2,%xmm0,%xmm2 @@ -156,7 +156,7 @@ _aesni_ctr32_ghash_6x: vaesenc %xmm15,%xmm13,%xmm13 movq %r12,72+8(%rsp) vpxor %xmm3,%xmm4,%xmm4 - vmovdqu 112-32(%r9),%xmm3 + vmovdqu 112-32(%r9),%xmm3 // %xmm3^6 vaesenc %xmm15,%xmm14,%xmm14 vmovups 96-128(%rcx),%xmm15 @@ -183,7 +183,7 @@ _aesni_ctr32_ghash_6x: vmovups 112-128(%rcx),%xmm15 vpslldq $8,%xmm6,%xmm5 vpxor %xmm2,%xmm4,%xmm4 - vmovdqu 16(%r11),%xmm3 + vmovdqu 16(%r11),%xmm3 // .Lpoly vaesenc %xmm15,%xmm9,%xmm9 vpxor %xmm8,%xmm7,%xmm7 @@ -192,13 +192,13 @@ _aesni_ctr32_ghash_6x: movbeq 24(%r14),%r13 vaesenc %xmm15,%xmm11,%xmm11 movbeq 16(%r14),%r12 - vpalignr $8,%xmm4,%xmm4,%xmm0 + vpalignr $8,%xmm4,%xmm4,%xmm0 // 1st phase vpclmulqdq $0x10,%xmm3,%xmm4,%xmm4 movq %r13,96+8(%rsp) vaesenc %xmm15,%xmm12,%xmm12 movq %r12,104+8(%rsp) vaesenc %xmm15,%xmm13,%xmm13 - vmovups 128-128(%rcx),%xmm1 + vmovups 128-128(%rcx),%xmm1 // borrow %xmm1 for %xmm15 vaesenc %xmm15,%xmm14,%xmm14 vaesenc %xmm1,%xmm9,%xmm9 @@ -215,7 +215,7 @@ _aesni_ctr32_ghash_6x: vaesenc %xmm1,%xmm14,%xmm14 vmovups 160-128(%rcx),%xmm1 cmpl $11,%r10d - jb .Lenc_tail + jb .Lenc_tail // 128-bit key vaesenc %xmm15,%xmm9,%xmm9 vaesenc %xmm15,%xmm10,%xmm10 @@ -232,7 +232,7 @@ _aesni_ctr32_ghash_6x: vmovups 176-128(%rcx),%xmm15 vaesenc %xmm1,%xmm14,%xmm14 vmovups 192-128(%rcx),%xmm1 - je .Lenc_tail + je .Lenc_tail // 192-bit key vaesenc %xmm15,%xmm9,%xmm9 vaesenc %xmm15,%xmm10,%xmm10 @@ -249,16 +249,16 @@ _aesni_ctr32_ghash_6x: vmovups 208-128(%rcx),%xmm15 vaesenc %xmm1,%xmm14,%xmm14 vmovups 224-128(%rcx),%xmm1 - jmp .Lenc_tail + jmp .Lenc_tail // 256-bit key .align 32 .Lhandle_ctr32: - vmovdqu (%r11),%xmm0 - vpshufb %xmm0,%xmm1,%xmm6 - vmovdqu 48(%r11),%xmm5 - vpaddd 64(%r11),%xmm6,%xmm10 + vmovdqu (%r11),%xmm0 // borrow %xmm0 for .Lbswap_mask + vpshufb %xmm0,%xmm1,%xmm6 // byte-swap counter + vmovdqu 48(%r11),%xmm5 // borrow %xmm5, .Ltwo_lsb + vpaddd 64(%r11),%xmm6,%xmm10 // .Lone_lsb vpaddd %xmm5,%xmm6,%xmm11 - vmovdqu 0-32(%r9),%xmm3 + vmovdqu 0-32(%r9),%xmm3 // %xmm3^1 vpaddd %xmm5,%xmm10,%xmm12 vpshufb %xmm0,%xmm10,%xmm10 vpaddd %xmm5,%xmm11,%xmm13 @@ -267,17 +267,17 @@ _aesni_ctr32_ghash_6x: vpaddd %xmm5,%xmm12,%xmm14 vpshufb %xmm0,%xmm12,%xmm12 vpxor %xmm15,%xmm11,%xmm11 - vpaddd %xmm5,%xmm13,%xmm1 + vpaddd %xmm5,%xmm13,%xmm1 // byte-swapped next counter value vpshufb %xmm0,%xmm13,%xmm13 vpshufb %xmm0,%xmm14,%xmm14 - vpshufb %xmm0,%xmm1,%xmm1 + vpshufb %xmm0,%xmm1,%xmm1 // next counter value jmp .Lresume_ctr32 .align 32 .Lenc_tail: vaesenc %xmm15,%xmm9,%xmm9 - vmovdqu %xmm7,16+8(%rsp) - vpalignr $8,%xmm4,%xmm4,%xmm8 + vmovdqu %xmm7,16+8(%rsp) // postpone vpxor %xmm7,%xmm8,%xmm8 + vpalignr $8,%xmm4,%xmm4,%xmm8 // 2nd phase vaesenc %xmm15,%xmm10,%xmm10 vpclmulqdq $0x10,%xmm3,%xmm4,%xmm4 vpxor 0(%rdi),%xmm1,%xmm2 @@ -290,16 +290,16 @@ _aesni_ctr32_ghash_6x: vaesenc %xmm15,%xmm14,%xmm14 vpxor 64(%rdi),%xmm1,%xmm7 vpxor 80(%rdi),%xmm1,%xmm3 - vmovdqu (%r8),%xmm1 + vmovdqu (%r8),%xmm1 // load next counter value vaesenclast %xmm2,%xmm9,%xmm9 - vmovdqu 32(%r11),%xmm2 + vmovdqu 32(%r11),%xmm2 // borrow %xmm2, .Lone_msb vaesenclast %xmm0,%xmm10,%xmm10 vpaddb %xmm2,%xmm1,%xmm0 movq %r13,112+8(%rsp) leaq 96(%rdi),%rdi - - prefetcht0 512(%rdi) +// These two prefetches were added in BoringSSL. See change that added them. + prefetcht0 512(%rdi) // We use 96-byte block so prefetch 2 lines (128 bytes) prefetcht0 576(%rdi) vaesenclast %xmm5,%xmm11,%xmm11 vpaddb %xmm2,%xmm0,%xmm5 @@ -317,24 +317,24 @@ _aesni_ctr32_ghash_6x: subq $0x6,%rdx jc .L6x_done - vmovups %xmm9,-96(%rsi) + vmovups %xmm9,-96(%rsi) // save output vpxor %xmm15,%xmm1,%xmm9 vmovups %xmm10,-80(%rsi) - vmovdqa %xmm0,%xmm10 + vmovdqa %xmm0,%xmm10 // 0 latency vmovups %xmm11,-64(%rsi) - vmovdqa %xmm5,%xmm11 + vmovdqa %xmm5,%xmm11 // 0 latency vmovups %xmm12,-48(%rsi) - vmovdqa %xmm6,%xmm12 + vmovdqa %xmm6,%xmm12 // 0 latency vmovups %xmm13,-32(%rsi) - vmovdqa %xmm7,%xmm13 + vmovdqa %xmm7,%xmm13 // 0 latency vmovups %xmm14,-16(%rsi) - vmovdqa %xmm3,%xmm14 - vmovdqu 32+8(%rsp),%xmm7 + vmovdqa %xmm3,%xmm14 // 0 latency + vmovdqu 32+8(%rsp),%xmm7 // I[5] jmp .Loop6x .L6x_done: - vpxor 16+8(%rsp),%xmm8,%xmm8 - vpxor %xmm4,%xmm8,%xmm8 + vpxor 16+8(%rsp),%xmm8,%xmm8 // modulo-scheduled + vpxor %xmm4,%xmm8,%xmm8 // modulo-scheduled ret .cfi_endproc @@ -349,16 +349,16 @@ aesni_gcm_decrypt: _CET_ENDBR xorq %rax,%rax - - - cmpq $0x60,%rdx +// We call |_aesni_ctr32_ghash_6x|, which requires at least 96 (0x60) +// bytes of input. + cmpq $0x60,%rdx // minimal accepted length jb .Lgcm_dec_abort pushq %rbp .cfi_adjust_cfa_offset 8 .cfi_offset %rbp,-16 - movq %rsp,%rbp + movq %rsp,%rbp // save stack pointer .cfi_def_cfa_register %rbp pushq %rbx .cfi_offset %rbx,-24 @@ -378,17 +378,17 @@ _CET_ENDBR vzeroupper movq 16(%rbp),%r12 - vmovdqu (%r8),%xmm1 + vmovdqu (%r8),%xmm1 // input counter value addq $-128,%rsp movl 12(%r8),%ebx leaq .Lbswap_mask(%rip),%r11 - leaq -128(%rcx),%r14 - movq $0xf80,%r15 - vmovdqu (%r12),%xmm8 - andq $-128,%rsp - vmovdqu (%r11),%xmm0 - leaq 128(%rcx),%rcx - leaq 32(%r9),%r9 + leaq -128(%rcx),%r14 // borrow %r14 + movq $0xf80,%r15 // borrow %r15 + vmovdqu (%r12),%xmm8 // load Xi + andq $-128,%rsp // ensure stack alignment + vmovdqu (%r11),%xmm0 // borrow %xmm0 for .Lbswap_mask + leaq 128(%rcx),%rcx // size optimization + leaq 32(%r9),%r9 // size optimization movl 240-128(%rcx),%r10d vpshufb %xmm0,%xmm8,%xmm8 @@ -398,26 +398,26 @@ _CET_ENDBR jc .Ldec_no_key_aliasing cmpq $768,%r15 jnc .Ldec_no_key_aliasing - subq %r15,%rsp + subq %r15,%rsp // avoid aliasing with key .Ldec_no_key_aliasing: - vmovdqu 80(%rdi),%xmm7 + vmovdqu 80(%rdi),%xmm7 // I[5] movq %rdi,%r14 vmovdqu 64(%rdi),%xmm4 - - - - - - +// |_aesni_ctr32_ghash_6x| requires |%r15| to point to 2*96 (0xc0) +// bytes before the end of the input. Note, in particular, that this is +// correct even if |%rdx| is not an even multiple of 96 or 16. XXX: This +// seems to require that |%rdi| + |%rdx| >= 2*96 (0xc0); i.e. |%rdi| must +// not be near the very beginning of the address space when |%rdx| < 2*96 +// (0xc0). leaq -192(%rdi,%rdx,1),%r15 vmovdqu 48(%rdi),%xmm5 shrq $4,%rdx xorq %rax,%rax vmovdqu 32(%rdi),%xmm6 - vpshufb %xmm0,%xmm7,%xmm7 + vpshufb %xmm0,%xmm7,%xmm7 // passed to _aesni_ctr32_ghash_6x vmovdqu 16(%rdi),%xmm2 vpshufb %xmm0,%xmm4,%xmm4 vmovdqu (%rdi),%xmm3 @@ -434,18 +434,18 @@ _CET_ENDBR call _aesni_ctr32_ghash_6x movq 16(%rbp),%r12 - vmovups %xmm9,-96(%rsi) + vmovups %xmm9,-96(%rsi) // save output vmovups %xmm10,-80(%rsi) vmovups %xmm11,-64(%rsi) vmovups %xmm12,-48(%rsi) vmovups %xmm13,-32(%rsi) vmovups %xmm14,-16(%rsi) - vpshufb (%r11),%xmm8,%xmm8 - vmovdqu %xmm8,(%r12) + vpshufb (%r11),%xmm8,%xmm8 // .Lbswap_mask + vmovdqu %xmm8,(%r12) // output Xi vzeroupper - leaq -40(%rbp),%rsp + leaq -40(%rbp),%rsp // restore %rsp to fixed allocation .cfi_def_cfa %rsp, 0x38 popq %r15 .cfi_adjust_cfa_offset -8 @@ -474,8 +474,8 @@ _CET_ENDBR .align 32 _aesni_ctr32_6x: .cfi_startproc - vmovdqu 0-128(%rcx),%xmm4 - vmovdqu 32(%r11),%xmm2 + vmovdqu 0-128(%rcx),%xmm4 // borrow %xmm4 for %xmm15 + vmovdqu 32(%r11),%xmm2 // borrow %xmm2, .Lone_msb leaq -1(%r10),%r13 vmovups 16-128(%rcx),%xmm15 leaq 32-128(%rcx),%r12 @@ -508,7 +508,7 @@ _aesni_ctr32_6x: decl %r13d jnz .Loop_ctr32 - vmovdqu (%r12),%xmm3 + vmovdqu (%r12),%xmm3 // last round key vaesenc %xmm15,%xmm9,%xmm9 vpxor 0(%rdi),%xmm3,%xmm4 vaesenc %xmm15,%xmm10,%xmm10 @@ -540,9 +540,9 @@ _aesni_ctr32_6x: ret .align 32 .Lhandle_ctr32_2: - vpshufb %xmm0,%xmm1,%xmm6 - vmovdqu 48(%r11),%xmm5 - vpaddd 64(%r11),%xmm6,%xmm10 + vpshufb %xmm0,%xmm1,%xmm6 // byte-swap counter + vmovdqu 48(%r11),%xmm5 // borrow %xmm5, .Ltwo_lsb + vpaddd 64(%r11),%xmm6,%xmm10 // .Lone_lsb vpaddd %xmm5,%xmm6,%xmm11 vpaddd %xmm5,%xmm10,%xmm12 vpshufb %xmm0,%xmm10,%xmm10 @@ -552,12 +552,12 @@ _aesni_ctr32_6x: vpaddd %xmm5,%xmm12,%xmm14 vpshufb %xmm0,%xmm12,%xmm12 vpxor %xmm4,%xmm11,%xmm11 - vpaddd %xmm5,%xmm13,%xmm1 + vpaddd %xmm5,%xmm13,%xmm1 // byte-swapped next counter value vpshufb %xmm0,%xmm13,%xmm13 vpxor %xmm4,%xmm12,%xmm12 vpshufb %xmm0,%xmm14,%xmm14 vpxor %xmm4,%xmm13,%xmm13 - vpshufb %xmm0,%xmm1,%xmm1 + vpshufb %xmm0,%xmm1,%xmm1 // next counter value vpxor %xmm4,%xmm14,%xmm14 jmp .Loop_ctr32 .cfi_endproc @@ -578,17 +578,17 @@ _CET_ENDBR #endif xorq %rax,%rax - - - - cmpq $288,%rdx +// We call |_aesni_ctr32_6x| twice, each call consuming 96 bytes of +// input. Then we call |_aesni_ctr32_ghash_6x|, which requires at +// least 96 more bytes of input. + cmpq $288,%rdx // minimal accepted length jb .Lgcm_enc_abort pushq %rbp .cfi_adjust_cfa_offset 8 .cfi_offset %rbp,-16 - movq %rsp,%rbp + movq %rsp,%rbp // save stack pointer .cfi_def_cfa_register %rbp pushq %rbx .cfi_offset %rbx,-24 @@ -607,15 +607,15 @@ _CET_ENDBR vzeroupper - vmovdqu (%r8),%xmm1 + vmovdqu (%r8),%xmm1 // input counter value addq $-128,%rsp movl 12(%r8),%ebx leaq .Lbswap_mask(%rip),%r11 - leaq -128(%rcx),%r14 - movq $0xf80,%r15 - leaq 128(%rcx),%rcx - vmovdqu (%r11),%xmm0 - andq $-128,%rsp + leaq -128(%rcx),%r14 // borrow %r14 + movq $0xf80,%r15 // borrow %r15 + leaq 128(%rcx),%rcx // size optimization + vmovdqu (%r11),%xmm0 // borrow %xmm0 for .Lbswap_mask + andq $-128,%rsp // ensure stack alignment movl 240-128(%rcx),%r10d andq %r15,%r14 @@ -624,24 +624,24 @@ _CET_ENDBR jc .Lenc_no_key_aliasing cmpq $768,%r15 jnc .Lenc_no_key_aliasing - subq %r15,%rsp + subq %r15,%rsp // avoid aliasing with key .Lenc_no_key_aliasing: movq %rsi,%r14 - - - - - - - +// |_aesni_ctr32_ghash_6x| requires |%r15| to point to 2*96 (0xc0) +// bytes before the end of the input. Note, in particular, that this is +// correct even if |%rdx| is not an even multiple of 96 or 16. Unlike in +// the decryption case, there's no caveat that |%rsi| must not be near +// the very beginning of the address space, because we know that +// |%rdx| >= 3*96 from the check above, and so we know +// |%rsi| + |%rdx| >= 2*96 (0xc0). leaq -192(%rsi,%rdx,1),%r15 shrq $4,%rdx call _aesni_ctr32_6x - vpshufb %xmm0,%xmm9,%xmm8 + vpshufb %xmm0,%xmm9,%xmm8 // save bswapped output on stack vpshufb %xmm0,%xmm10,%xmm2 vmovdqu %xmm8,112(%rsp) vpshufb %xmm0,%xmm11,%xmm4 @@ -650,26 +650,26 @@ _CET_ENDBR vmovdqu %xmm4,80(%rsp) vpshufb %xmm0,%xmm13,%xmm6 vmovdqu %xmm5,64(%rsp) - vpshufb %xmm0,%xmm14,%xmm7 + vpshufb %xmm0,%xmm14,%xmm7 // passed to _aesni_ctr32_ghash_6x vmovdqu %xmm6,48(%rsp) call _aesni_ctr32_6x movq 16(%rbp),%r12 - leaq 32(%r9),%r9 - vmovdqu (%r12),%xmm8 + leaq 32(%r9),%r9 // size optimization + vmovdqu (%r12),%xmm8 // load Xi subq $12,%rdx movq $192,%rax vpshufb %xmm0,%xmm8,%xmm8 call _aesni_ctr32_ghash_6x - vmovdqu 32(%rsp),%xmm7 - vmovdqu (%r11),%xmm0 - vmovdqu 0-32(%r9),%xmm3 + vmovdqu 32(%rsp),%xmm7 // I[5] + vmovdqu (%r11),%xmm0 // borrow %xmm0 for .Lbswap_mask + vmovdqu 0-32(%r9),%xmm3 // %xmm3^1 vpunpckhqdq %xmm7,%xmm7,%xmm1 - vmovdqu 32-32(%r9),%xmm15 - vmovups %xmm9,-96(%rsi) - vpshufb %xmm0,%xmm9,%xmm9 + vmovdqu 32-32(%r9),%xmm15 // borrow %xmm15 for + vmovups %xmm9,-96(%rsi) // save output + vpshufb %xmm0,%xmm9,%xmm9 // but keep bswapped copy vpxor %xmm7,%xmm1,%xmm1 vmovups %xmm10,-80(%rsi) vpshufb %xmm0,%xmm10,%xmm10 @@ -681,18 +681,18 @@ _CET_ENDBR vpshufb %xmm0,%xmm13,%xmm13 vmovups %xmm14,-16(%rsi) vpshufb %xmm0,%xmm14,%xmm14 - vmovdqu %xmm9,16(%rsp) - vmovdqu 48(%rsp),%xmm6 - vmovdqu 16-32(%r9),%xmm0 + vmovdqu %xmm9,16(%rsp) // free %xmm9 + vmovdqu 48(%rsp),%xmm6 // I[4] + vmovdqu 16-32(%r9),%xmm0 // borrow %xmm0 for %xmm3^2 vpunpckhqdq %xmm6,%xmm6,%xmm2 vpclmulqdq $0x00,%xmm3,%xmm7,%xmm5 vpxor %xmm6,%xmm2,%xmm2 vpclmulqdq $0x11,%xmm3,%xmm7,%xmm7 vpclmulqdq $0x00,%xmm15,%xmm1,%xmm1 - vmovdqu 64(%rsp),%xmm9 + vmovdqu 64(%rsp),%xmm9 // I[3] vpclmulqdq $0x00,%xmm0,%xmm6,%xmm4 - vmovdqu 48-32(%r9),%xmm3 + vmovdqu 48-32(%r9),%xmm3 // %xmm3^3 vpxor %xmm5,%xmm4,%xmm4 vpunpckhqdq %xmm9,%xmm9,%xmm5 vpclmulqdq $0x11,%xmm0,%xmm6,%xmm6 @@ -702,9 +702,9 @@ _CET_ENDBR vmovdqu 80-32(%r9),%xmm15 vpxor %xmm1,%xmm2,%xmm2 - vmovdqu 80(%rsp),%xmm1 + vmovdqu 80(%rsp),%xmm1 // I[2] vpclmulqdq $0x00,%xmm3,%xmm9,%xmm7 - vmovdqu 64-32(%r9),%xmm0 + vmovdqu 64-32(%r9),%xmm0 // borrow %xmm0 for %xmm3^4 vpxor %xmm4,%xmm7,%xmm7 vpunpckhqdq %xmm1,%xmm1,%xmm4 vpclmulqdq $0x11,%xmm3,%xmm9,%xmm9 @@ -713,9 +713,9 @@ _CET_ENDBR vpclmulqdq $0x00,%xmm15,%xmm5,%xmm5 vpxor %xmm2,%xmm5,%xmm5 - vmovdqu 96(%rsp),%xmm2 + vmovdqu 96(%rsp),%xmm2 // I[1] vpclmulqdq $0x00,%xmm0,%xmm1,%xmm6 - vmovdqu 96-32(%r9),%xmm3 + vmovdqu 96-32(%r9),%xmm3 // %xmm3^5 vpxor %xmm7,%xmm6,%xmm6 vpunpckhqdq %xmm2,%xmm2,%xmm7 vpclmulqdq $0x11,%xmm0,%xmm1,%xmm1 @@ -725,9 +725,9 @@ _CET_ENDBR vmovdqu 128-32(%r9),%xmm15 vpxor %xmm5,%xmm4,%xmm4 - vpxor 112(%rsp),%xmm8,%xmm8 + vpxor 112(%rsp),%xmm8,%xmm8 // accumulate I[0] vpclmulqdq $0x00,%xmm3,%xmm2,%xmm5 - vmovdqu 112-32(%r9),%xmm0 + vmovdqu 112-32(%r9),%xmm0 // borrow %xmm0 for %xmm3^6 vpunpckhqdq %xmm8,%xmm8,%xmm9 vpxor %xmm6,%xmm5,%xmm5 vpclmulqdq $0x11,%xmm3,%xmm2,%xmm2 @@ -737,7 +737,7 @@ _CET_ENDBR vpxor %xmm4,%xmm7,%xmm4 vpclmulqdq $0x00,%xmm0,%xmm8,%xmm6 - vmovdqu 0-32(%r9),%xmm3 + vmovdqu 0-32(%r9),%xmm3 // %xmm3^1 vpunpckhqdq %xmm14,%xmm14,%xmm1 vpclmulqdq $0x11,%xmm0,%xmm8,%xmm8 vpxor %xmm14,%xmm1,%xmm1 @@ -747,8 +747,8 @@ _CET_ENDBR vpxor %xmm2,%xmm8,%xmm7 vpxor %xmm4,%xmm9,%xmm6 - vmovdqu 16-32(%r9),%xmm0 - vpxor %xmm5,%xmm7,%xmm9 + vmovdqu 16-32(%r9),%xmm0 // borrow %xmm0 for %xmm3^2 + vpxor %xmm5,%xmm7,%xmm9 // aggregated Karatsuba post-processing vpclmulqdq $0x00,%xmm3,%xmm14,%xmm4 vpxor %xmm9,%xmm6,%xmm6 vpunpckhqdq %xmm13,%xmm13,%xmm2 @@ -761,25 +761,25 @@ _CET_ENDBR vpxor %xmm6,%xmm7,%xmm7 vpclmulqdq $0x00,%xmm0,%xmm13,%xmm5 - vmovdqu 48-32(%r9),%xmm3 + vmovdqu 48-32(%r9),%xmm3 // %xmm3^3 vpxor %xmm4,%xmm5,%xmm5 vpunpckhqdq %xmm12,%xmm12,%xmm9 vpclmulqdq $0x11,%xmm0,%xmm13,%xmm13 vpxor %xmm12,%xmm9,%xmm9 vpxor %xmm14,%xmm13,%xmm13 - vpalignr $8,%xmm8,%xmm8,%xmm14 + vpalignr $8,%xmm8,%xmm8,%xmm14 // 1st phase vpclmulqdq $0x10,%xmm15,%xmm2,%xmm2 vmovdqu 80-32(%r9),%xmm15 vpxor %xmm1,%xmm2,%xmm2 vpclmulqdq $0x00,%xmm3,%xmm12,%xmm4 - vmovdqu 64-32(%r9),%xmm0 + vmovdqu 64-32(%r9),%xmm0 // borrow %xmm0 for %xmm3^4 vpxor %xmm5,%xmm4,%xmm4 vpunpckhqdq %xmm11,%xmm11,%xmm1 vpclmulqdq $0x11,%xmm3,%xmm12,%xmm12 vpxor %xmm11,%xmm1,%xmm1 vpxor %xmm13,%xmm12,%xmm12 - vxorps 16(%rsp),%xmm7,%xmm7 + vxorps 16(%rsp),%xmm7,%xmm7 // accumulate %xmm9 vpclmulqdq $0x00,%xmm15,%xmm9,%xmm9 vpxor %xmm2,%xmm9,%xmm9 @@ -787,12 +787,12 @@ _CET_ENDBR vxorps %xmm14,%xmm8,%xmm8 vpclmulqdq $0x00,%xmm0,%xmm11,%xmm5 - vmovdqu 96-32(%r9),%xmm3 + vmovdqu 96-32(%r9),%xmm3 // %xmm3^5 vpxor %xmm4,%xmm5,%xmm5 vpunpckhqdq %xmm10,%xmm10,%xmm2 vpclmulqdq $0x11,%xmm0,%xmm11,%xmm11 vpxor %xmm10,%xmm2,%xmm2 - vpalignr $8,%xmm8,%xmm8,%xmm14 + vpalignr $8,%xmm8,%xmm8,%xmm14 // 2nd phase vpxor %xmm12,%xmm11,%xmm11 vpclmulqdq $0x10,%xmm15,%xmm1,%xmm1 vmovdqu 128-32(%r9),%xmm15 @@ -803,7 +803,7 @@ _CET_ENDBR vxorps %xmm14,%xmm8,%xmm8 vpclmulqdq $0x00,%xmm3,%xmm10,%xmm4 - vmovdqu 112-32(%r9),%xmm0 + vmovdqu 112-32(%r9),%xmm0 // borrow %xmm0 for %xmm3^6 vpxor %xmm5,%xmm4,%xmm4 vpunpckhqdq %xmm8,%xmm8,%xmm9 vpclmulqdq $0x11,%xmm3,%xmm10,%xmm10 @@ -819,28 +819,28 @@ _CET_ENDBR vpxor %xmm10,%xmm7,%xmm7 vpxor %xmm2,%xmm6,%xmm6 - vpxor %xmm5,%xmm7,%xmm4 + vpxor %xmm5,%xmm7,%xmm4 // aggregated Karatsuba post-processing vpxor %xmm4,%xmm6,%xmm6 vpslldq $8,%xmm6,%xmm1 - vmovdqu 16(%r11),%xmm3 + vmovdqu 16(%r11),%xmm3 // .Lpoly vpsrldq $8,%xmm6,%xmm6 vpxor %xmm1,%xmm5,%xmm8 vpxor %xmm6,%xmm7,%xmm7 - vpalignr $8,%xmm8,%xmm8,%xmm2 + vpalignr $8,%xmm8,%xmm8,%xmm2 // 1st phase vpclmulqdq $0x10,%xmm3,%xmm8,%xmm8 vpxor %xmm2,%xmm8,%xmm8 - vpalignr $8,%xmm8,%xmm8,%xmm2 + vpalignr $8,%xmm8,%xmm8,%xmm2 // 2nd phase vpclmulqdq $0x10,%xmm3,%xmm8,%xmm8 vpxor %xmm7,%xmm2,%xmm2 vpxor %xmm2,%xmm8,%xmm8 movq 16(%rbp),%r12 - vpshufb (%r11),%xmm8,%xmm8 - vmovdqu %xmm8,(%r12) + vpshufb (%r11),%xmm8,%xmm8 // .Lbswap_mask + vmovdqu %xmm8,(%r12) // output Xi vzeroupper - leaq -40(%rbp),%rsp + leaq -40(%rbp),%rsp // restore %rsp to fixed allocation .cfi_def_cfa %rsp, 0x38 popq %r15 .cfi_adjust_cfa_offset -8 @@ -866,7 +866,9 @@ _CET_ENDBR .cfi_endproc .size aesni_gcm_encrypt,.-aesni_gcm_encrypt .section .rodata + .align 64 +aesni_gcm_constants: .Lbswap_mask: .byte 15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0 .Lpoly: diff --git a/third_party/boringssl/gen/bcm/aesni-gcm-x86_64-win.asm b/third_party/boringssl/gen/bcm/aesni-gcm-x86_64-win.asm index 3f7241ad9..258862e70 100644 --- a/third_party/boringssl/gen/bcm/aesni-gcm-x86_64-win.asm +++ b/third_party/boringssl/gen/bcm/aesni-gcm-x86_64-win.asm @@ -18,9 +18,9 @@ section .text code align=64 ALIGN 32 _aesni_ctr32_ghash_6x: - vmovdqu xmm2,XMMWORD[32+r11] + vmovdqu xmm2,XMMWORD[32+r11] ; borrow %xmm2, .Lone_msb sub r8,6 - vpxor xmm4,xmm4,xmm4 + vpxor xmm4,xmm4,xmm4 ; %xmm4 = 0 vmovdqu xmm15,XMMWORD[((0-128))+r9] vpaddb xmm10,xmm1,xmm2 vpaddb xmm11,xmm10,xmm2 @@ -28,46 +28,46 @@ _aesni_ctr32_ghash_6x: vpaddb xmm13,xmm12,xmm2 vpaddb xmm14,xmm13,xmm2 vpxor xmm9,xmm1,xmm15 - vmovdqu XMMWORD[(16+8)+rsp],xmm4 + vmovdqu XMMWORD[(16+8)+rsp],xmm4 ; "%xmm7" = 0 jmp NEAR $L$oop6x ALIGN 32 $L$oop6x: add ebx,100663296 - jc NEAR $L$handle_ctr32 - vmovdqu xmm3,XMMWORD[((0-32))+rsi] - vpaddb xmm1,xmm14,xmm2 + jc NEAR $L$handle_ctr32 ; discard ? + vmovdqu xmm3,XMMWORD[((0-32))+rsi] ; %xmm3^1 + vpaddb xmm1,xmm14,xmm2 ; next counter value vpxor xmm10,xmm10,xmm15 vpxor xmm11,xmm11,xmm15 $L$resume_ctr32: - vmovdqu XMMWORD[rdi],xmm1 + vmovdqu XMMWORD[rdi],xmm1 ; save next counter value vpclmulqdq xmm5,xmm7,xmm3,0x10 vpxor xmm12,xmm12,xmm15 - vmovups xmm2,XMMWORD[((16-128))+r9] + vmovups xmm2,XMMWORD[((16-128))+r9] ; borrow %xmm2 for %xmm15 vpclmulqdq xmm6,xmm7,xmm3,0x01 - - - - - - - - - - - - - - - - +; At this point, the current block of 96 (0x60) bytes has already been +; loaded into registers. Concurrently with processing it, we want to +; load the next 96 bytes of input for the next round. Obviously, we can +; only do this if there are at least 96 more bytes of input beyond the +; input we're currently processing, or else we'd read past the end of +; the input buffer. Here, we set |%r12| to 96 if there are at least 96 +; bytes of input beyond the 96 bytes we're already processing, and we +; set |%r12| to 0 otherwise. In the case where we set |%r12| to 96, +; we'll read in the next block so that it is in registers for the next +; loop iteration. In the case where we set |%r12| to 0, we'll re-read +; the current block and then ignore what we re-read. +; +; At this point, |%r14| points to the current (already read into +; registers) block, and |%r15| points to 2*96 bytes before the end of +; the input. Thus, |%r14| > |%r15| means that we do not have the next +; 96-byte block to read in, and |%r14| <= |%r15| means we do. xor r12,r12 cmp r15,r14 vaesenc xmm9,xmm9,xmm2 - vmovdqu xmm0,XMMWORD[((48+8))+rsp] + vmovdqu xmm0,XMMWORD[((48+8))+rsp] ; I[4] vpxor xmm13,xmm13,xmm15 vpclmulqdq xmm1,xmm7,xmm3,0x00 vaesenc xmm10,xmm10,xmm2 @@ -75,12 +75,12 @@ $L$resume_ctr32: setnc r12b vpclmulqdq xmm7,xmm7,xmm3,0x11 vaesenc xmm11,xmm11,xmm2 - vmovdqu xmm3,XMMWORD[((16-32))+rsi] + vmovdqu xmm3,XMMWORD[((16-32))+rsi] ; %xmm3^2 neg r12 vaesenc xmm12,xmm12,xmm2 vpxor xmm6,xmm6,xmm5 vpclmulqdq xmm5,xmm0,xmm3,0x00 - vpxor xmm8,xmm8,xmm4 + vpxor xmm8,xmm8,xmm4 ; modulo-scheduled vaesenc xmm13,xmm13,xmm2 vpxor xmm4,xmm1,xmm5 and r12,0x60 @@ -91,9 +91,9 @@ $L$resume_ctr32: vpclmulqdq xmm2,xmm0,xmm3,0x01 lea r14,[r12*1+r14] vaesenc xmm9,xmm9,xmm15 - vpxor xmm8,xmm8,XMMWORD[((16+8))+rsp] + vpxor xmm8,xmm8,XMMWORD[((16+8))+rsp] ; modulo-scheduled [vpxor %xmm7,%xmm8,%xmm8] vpclmulqdq xmm3,xmm0,xmm3,0x11 - vmovdqu xmm0,XMMWORD[((64+8))+rsp] + vmovdqu xmm0,XMMWORD[((64+8))+rsp] ; I[3] vaesenc xmm10,xmm10,xmm15 movbe r13,QWORD[88+r14] vaesenc xmm11,xmm11,xmm15 @@ -102,7 +102,7 @@ $L$resume_ctr32: mov QWORD[((32+8))+rsp],r13 vaesenc xmm13,xmm13,xmm15 mov QWORD[((40+8))+rsp],r12 - vmovdqu xmm5,XMMWORD[((48-32))+rsi] + vmovdqu xmm5,XMMWORD[((48-32))+rsi] ; borrow %xmm5 for %xmm3^3 vaesenc xmm14,xmm14,xmm15 vmovups xmm15,XMMWORD[((48-128))+r9] @@ -116,11 +116,11 @@ $L$resume_ctr32: vpclmulqdq xmm3,xmm0,xmm5,0x01 vaesenc xmm11,xmm11,xmm15 vpclmulqdq xmm5,xmm0,xmm5,0x11 - vmovdqu xmm0,XMMWORD[((80+8))+rsp] + vmovdqu xmm0,XMMWORD[((80+8))+rsp] ; I[2] vaesenc xmm12,xmm12,xmm15 vaesenc xmm13,xmm13,xmm15 vpxor xmm4,xmm4,xmm1 - vmovdqu xmm1,XMMWORD[((64-32))+rsi] + vmovdqu xmm1,XMMWORD[((64-32))+rsi] ; borrow %xmm1 for %xmm3^4 vaesenc xmm14,xmm14,xmm15 vmovups xmm15,XMMWORD[((64-128))+r9] @@ -136,13 +136,13 @@ $L$resume_ctr32: vaesenc xmm11,xmm11,xmm15 movbe r12,QWORD[64+r14] vpclmulqdq xmm1,xmm0,xmm1,0x11 - vmovdqu xmm0,XMMWORD[((96+8))+rsp] + vmovdqu xmm0,XMMWORD[((96+8))+rsp] ; I[1] vaesenc xmm12,xmm12,xmm15 mov QWORD[((48+8))+rsp],r13 vaesenc xmm13,xmm13,xmm15 mov QWORD[((56+8))+rsp],r12 vpxor xmm4,xmm4,xmm2 - vmovdqu xmm2,XMMWORD[((96-32))+rsi] + vmovdqu xmm2,XMMWORD[((96-32))+rsi] ; borrow %xmm2 for %xmm3^5 vaesenc xmm14,xmm14,xmm15 vmovups xmm15,XMMWORD[((80-128))+r9] @@ -155,7 +155,7 @@ $L$resume_ctr32: movbe r13,QWORD[56+r14] vpxor xmm7,xmm7,xmm1 vpclmulqdq xmm1,xmm0,xmm2,0x01 - vpxor xmm8,xmm8,XMMWORD[((112+8))+rsp] + vpxor xmm8,xmm8,XMMWORD[((112+8))+rsp] ; accumulate I[0] vaesenc xmm11,xmm11,xmm15 movbe r12,QWORD[48+r14] vpclmulqdq xmm2,xmm0,xmm2,0x11 @@ -164,7 +164,7 @@ $L$resume_ctr32: vaesenc xmm13,xmm13,xmm15 mov QWORD[((72+8))+rsp],r12 vpxor xmm4,xmm4,xmm3 - vmovdqu xmm3,XMMWORD[((112-32))+rsi] + vmovdqu xmm3,XMMWORD[((112-32))+rsi] ; %xmm3^6 vaesenc xmm14,xmm14,xmm15 vmovups xmm15,XMMWORD[((96-128))+r9] @@ -191,7 +191,7 @@ $L$resume_ctr32: vmovups xmm15,XMMWORD[((112-128))+r9] vpslldq xmm5,xmm6,8 vpxor xmm4,xmm4,xmm2 - vmovdqu xmm3,XMMWORD[16+r11] + vmovdqu xmm3,XMMWORD[16+r11] ; .Lpoly vaesenc xmm9,xmm9,xmm15 vpxor xmm7,xmm7,xmm8 @@ -200,13 +200,13 @@ $L$resume_ctr32: movbe r13,QWORD[24+r14] vaesenc xmm11,xmm11,xmm15 movbe r12,QWORD[16+r14] - vpalignr xmm0,xmm4,xmm4,8 + vpalignr xmm0,xmm4,xmm4,8 ; 1st phase vpclmulqdq xmm4,xmm4,xmm3,0x10 mov QWORD[((96+8))+rsp],r13 vaesenc xmm12,xmm12,xmm15 mov QWORD[((104+8))+rsp],r12 vaesenc xmm13,xmm13,xmm15 - vmovups xmm1,XMMWORD[((128-128))+r9] + vmovups xmm1,XMMWORD[((128-128))+r9] ; borrow %xmm1 for %xmm15 vaesenc xmm14,xmm14,xmm15 vaesenc xmm9,xmm9,xmm1 @@ -223,7 +223,7 @@ $L$resume_ctr32: vaesenc xmm14,xmm14,xmm1 vmovups xmm1,XMMWORD[((160-128))+r9] cmp r10d,11 - jb NEAR $L$enc_tail + jb NEAR $L$enc_tail ; 128-bit key vaesenc xmm9,xmm9,xmm15 vaesenc xmm10,xmm10,xmm15 @@ -240,7 +240,7 @@ $L$resume_ctr32: vmovups xmm15,XMMWORD[((176-128))+r9] vaesenc xmm14,xmm14,xmm1 vmovups xmm1,XMMWORD[((192-128))+r9] - je NEAR $L$enc_tail + je NEAR $L$enc_tail ; 192-bit key vaesenc xmm9,xmm9,xmm15 vaesenc xmm10,xmm10,xmm15 @@ -257,16 +257,16 @@ $L$resume_ctr32: vmovups xmm15,XMMWORD[((208-128))+r9] vaesenc xmm14,xmm14,xmm1 vmovups xmm1,XMMWORD[((224-128))+r9] - jmp NEAR $L$enc_tail + jmp NEAR $L$enc_tail ; 256-bit key ALIGN 32 $L$handle_ctr32: - vmovdqu xmm0,XMMWORD[r11] - vpshufb xmm6,xmm1,xmm0 - vmovdqu xmm5,XMMWORD[48+r11] - vpaddd xmm10,xmm6,XMMWORD[64+r11] + vmovdqu xmm0,XMMWORD[r11] ; borrow %xmm0 for .Lbswap_mask + vpshufb xmm6,xmm1,xmm0 ; byte-swap counter + vmovdqu xmm5,XMMWORD[48+r11] ; borrow %xmm5, .Ltwo_lsb + vpaddd xmm10,xmm6,XMMWORD[64+r11] ; .Lone_lsb vpaddd xmm11,xmm6,xmm5 - vmovdqu xmm3,XMMWORD[((0-32))+rsi] + vmovdqu xmm3,XMMWORD[((0-32))+rsi] ; %xmm3^1 vpaddd xmm12,xmm10,xmm5 vpshufb xmm10,xmm10,xmm0 vpaddd xmm13,xmm11,xmm5 @@ -275,17 +275,17 @@ $L$handle_ctr32: vpaddd xmm14,xmm12,xmm5 vpshufb xmm12,xmm12,xmm0 vpxor xmm11,xmm11,xmm15 - vpaddd xmm1,xmm13,xmm5 + vpaddd xmm1,xmm13,xmm5 ; byte-swapped next counter value vpshufb xmm13,xmm13,xmm0 vpshufb xmm14,xmm14,xmm0 - vpshufb xmm1,xmm1,xmm0 + vpshufb xmm1,xmm1,xmm0 ; next counter value jmp NEAR $L$resume_ctr32 ALIGN 32 $L$enc_tail: vaesenc xmm9,xmm9,xmm15 - vmovdqu XMMWORD[(16+8)+rsp],xmm7 - vpalignr xmm8,xmm4,xmm4,8 + vmovdqu XMMWORD[(16+8)+rsp],xmm7 ; postpone vpxor %xmm7,%xmm8,%xmm8 + vpalignr xmm8,xmm4,xmm4,8 ; 2nd phase vaesenc xmm10,xmm10,xmm15 vpclmulqdq xmm4,xmm4,xmm3,0x10 vpxor xmm2,xmm1,XMMWORD[rcx] @@ -298,16 +298,16 @@ $L$enc_tail: vaesenc xmm14,xmm14,xmm15 vpxor xmm7,xmm1,XMMWORD[64+rcx] vpxor xmm3,xmm1,XMMWORD[80+rcx] - vmovdqu xmm1,XMMWORD[rdi] + vmovdqu xmm1,XMMWORD[rdi] ; load next counter value vaesenclast xmm9,xmm9,xmm2 - vmovdqu xmm2,XMMWORD[32+r11] + vmovdqu xmm2,XMMWORD[32+r11] ; borrow %xmm2, .Lone_msb vaesenclast xmm10,xmm10,xmm0 vpaddb xmm0,xmm1,xmm2 mov QWORD[((112+8))+rsp],r13 lea rcx,[96+rcx] - - prefetcht0 [512+rcx] +; These two prefetches were added in BoringSSL. See change that added them. + prefetcht0 [512+rcx] ; We use 96-byte block so prefetch 2 lines (128 bytes) prefetcht0 [576+rcx] vaesenclast xmm11,xmm11,xmm5 vpaddb xmm5,xmm0,xmm2 @@ -325,24 +325,24 @@ $L$enc_tail: sub r8,0x6 jc NEAR $L$6x_done - vmovups XMMWORD[(-96)+rdx],xmm9 + vmovups XMMWORD[(-96)+rdx],xmm9 ; save output vpxor xmm9,xmm1,xmm15 vmovups XMMWORD[(-80)+rdx],xmm10 - vmovdqa xmm10,xmm0 + vmovdqa xmm10,xmm0 ; 0 latency vmovups XMMWORD[(-64)+rdx],xmm11 - vmovdqa xmm11,xmm5 + vmovdqa xmm11,xmm5 ; 0 latency vmovups XMMWORD[(-48)+rdx],xmm12 - vmovdqa xmm12,xmm6 + vmovdqa xmm12,xmm6 ; 0 latency vmovups XMMWORD[(-32)+rdx],xmm13 - vmovdqa xmm13,xmm7 + vmovdqa xmm13,xmm7 ; 0 latency vmovups XMMWORD[(-16)+rdx],xmm14 - vmovdqa xmm14,xmm3 - vmovdqu xmm7,XMMWORD[((32+8))+rsp] + vmovdqa xmm14,xmm3 ; 0 latency + vmovdqu xmm7,XMMWORD[((32+8))+rsp] ; I[5] jmp NEAR $L$oop6x $L$6x_done: - vpxor xmm8,xmm8,XMMWORD[((16+8))+rsp] - vpxor xmm8,xmm8,xmm4 + vpxor xmm8,xmm8,XMMWORD[((16+8))+rsp] ; modulo-scheduled + vpxor xmm8,xmm8,xmm4 ; modulo-scheduled ret @@ -356,15 +356,15 @@ $L$SEH_begin_aesni_gcm_decrypt_1: _CET_ENDBR xor rax,rax - - - cmp r8,0x60 +; We call |_aesni_ctr32_ghash_6x|, which requires at least 96 (0x60) +; bytes of input. + cmp r8,0x60 ; minimal accepted length jb NEAR $L$gcm_dec_abort push rbp $L$SEH_prologue_aesni_gcm_decrypt_2: - mov rbp,rsp + mov rbp,rsp ; save stack pointer push rbx @@ -381,19 +381,19 @@ $L$SEH_prologue_aesni_gcm_decrypt_6: push r15 $L$SEH_prologue_aesni_gcm_decrypt_7: - lea rsp,[((-168))+rsp] + lea rsp,[((-168))+rsp] ; 8 extra bytes to align the stack $L$SEH_prologue_aesni_gcm_decrypt_8: $L$SEH_prologue_aesni_gcm_decrypt_9: - - - +; Load the last two parameters. These go into %rdi and %rsi, which are +; non-volatile on Windows, so stash them in the parameter stack area +; first. mov QWORD[16+rbp],rdi $L$SEH_prologue_aesni_gcm_decrypt_10: mov QWORD[24+rbp],rsi $L$SEH_prologue_aesni_gcm_decrypt_11: mov rdi,QWORD[48+rbp] mov rsi,QWORD[56+rbp] - +; Save non-volatile XMM registers. movaps XMMWORD[(-208)+rbp],xmm6 $L$SEH_prologue_aesni_gcm_decrypt_12: movaps XMMWORD[(-192)+rbp],xmm7 @@ -418,17 +418,17 @@ $L$SEH_endprologue_aesni_gcm_decrypt_22: vzeroupper mov r12,QWORD[64+rbp] - vmovdqu xmm1,XMMWORD[rdi] + vmovdqu xmm1,XMMWORD[rdi] ; input counter value add rsp,-128 mov ebx,DWORD[12+rdi] lea r11,[$L$bswap_mask] - lea r14,[((-128))+r9] - mov r15,0xf80 - vmovdqu xmm8,XMMWORD[r12] - and rsp,-128 - vmovdqu xmm0,XMMWORD[r11] - lea r9,[128+r9] - lea rsi,[32+rsi] + lea r14,[((-128))+r9] ; borrow %r14 + mov r15,0xf80 ; borrow %r15 + vmovdqu xmm8,XMMWORD[r12] ; load Xi + and rsp,-128 ; ensure stack alignment + vmovdqu xmm0,XMMWORD[r11] ; borrow %xmm0 for .Lbswap_mask + lea r9,[128+r9] ; size optimization + lea rsi,[32+rsi] ; size optimization mov r10d,DWORD[((240-128))+r9] vpshufb xmm8,xmm8,xmm0 @@ -438,26 +438,26 @@ $L$SEH_endprologue_aesni_gcm_decrypt_22: jc NEAR $L$dec_no_key_aliasing cmp r15,768 jnc NEAR $L$dec_no_key_aliasing - sub rsp,r15 + sub rsp,r15 ; avoid aliasing with key $L$dec_no_key_aliasing: - vmovdqu xmm7,XMMWORD[80+rcx] + vmovdqu xmm7,XMMWORD[80+rcx] ; I[5] mov r14,rcx vmovdqu xmm4,XMMWORD[64+rcx] - - - - - - +; |_aesni_ctr32_ghash_6x| requires |%r15| to point to 2*96 (0xc0) +; bytes before the end of the input. Note, in particular, that this is +; correct even if |%r8| is not an even multiple of 96 or 16. XXX: This +; seems to require that |%rcx| + |%r8| >= 2*96 (0xc0); i.e. |%rcx| must +; not be near the very beginning of the address space when |%r8| < 2*96 +; (0xc0). lea r15,[((-192))+r8*1+rcx] vmovdqu xmm5,XMMWORD[48+rcx] shr r8,4 xor rax,rax vmovdqu xmm6,XMMWORD[32+rcx] - vpshufb xmm7,xmm7,xmm0 + vpshufb xmm7,xmm7,xmm0 ; passed to _aesni_ctr32_ghash_6x vmovdqu xmm2,XMMWORD[16+rcx] vpshufb xmm4,xmm4,xmm0 vmovdqu xmm3,XMMWORD[rcx] @@ -474,15 +474,15 @@ $L$dec_no_key_aliasing: call _aesni_ctr32_ghash_6x mov r12,QWORD[64+rbp] - vmovups XMMWORD[(-96)+rdx],xmm9 + vmovups XMMWORD[(-96)+rdx],xmm9 ; save output vmovups XMMWORD[(-80)+rdx],xmm10 vmovups XMMWORD[(-64)+rdx],xmm11 vmovups XMMWORD[(-48)+rdx],xmm12 vmovups XMMWORD[(-32)+rdx],xmm13 vmovups XMMWORD[(-16)+rdx],xmm14 - vpshufb xmm8,xmm8,XMMWORD[r11] - vmovdqu XMMWORD[r12],xmm8 + vpshufb xmm8,xmm8,XMMWORD[r11] ; .Lbswap_mask + vmovdqu XMMWORD[r12],xmm8 ; output Xi vzeroupper movaps xmm6,XMMWORD[((-208))+rbp] @@ -497,7 +497,7 @@ $L$dec_no_key_aliasing: movaps xmm15,XMMWORD[((-64))+rbp] mov rdi,QWORD[16+rbp] mov rsi,QWORD[24+rbp] - lea rsp,[((-40))+rbp] + lea rsp,[((-40))+rbp] ; restore %rsp to fixed allocation pop r15 @@ -520,8 +520,8 @@ $L$SEH_end_aesni_gcm_decrypt_23: ALIGN 32 _aesni_ctr32_6x: - vmovdqu xmm4,XMMWORD[((0-128))+r9] - vmovdqu xmm2,XMMWORD[32+r11] + vmovdqu xmm4,XMMWORD[((0-128))+r9] ; borrow %xmm4 for %xmm15 + vmovdqu xmm2,XMMWORD[32+r11] ; borrow %xmm2, .Lone_msb lea r13,[((-1))+r10] vmovups xmm15,XMMWORD[((16-128))+r9] lea r12,[((32-128))+r9] @@ -554,7 +554,7 @@ $L$oop_ctr32: dec r13d jnz NEAR $L$oop_ctr32 - vmovdqu xmm3,XMMWORD[r12] + vmovdqu xmm3,XMMWORD[r12] ; last round key vaesenc xmm9,xmm9,xmm15 vpxor xmm4,xmm3,XMMWORD[rcx] vaesenc xmm10,xmm10,xmm15 @@ -586,9 +586,9 @@ $L$oop_ctr32: ret ALIGN 32 $L$handle_ctr32_2: - vpshufb xmm6,xmm1,xmm0 - vmovdqu xmm5,XMMWORD[48+r11] - vpaddd xmm10,xmm6,XMMWORD[64+r11] + vpshufb xmm6,xmm1,xmm0 ; byte-swap counter + vmovdqu xmm5,XMMWORD[48+r11] ; borrow %xmm5, .Ltwo_lsb + vpaddd xmm10,xmm6,XMMWORD[64+r11] ; .Lone_lsb vpaddd xmm11,xmm6,xmm5 vpaddd xmm12,xmm10,xmm5 vpshufb xmm10,xmm10,xmm0 @@ -598,12 +598,12 @@ $L$handle_ctr32_2: vpaddd xmm14,xmm12,xmm5 vpshufb xmm12,xmm12,xmm0 vpxor xmm11,xmm11,xmm4 - vpaddd xmm1,xmm13,xmm5 + vpaddd xmm1,xmm13,xmm5 ; byte-swapped next counter value vpshufb xmm13,xmm13,xmm0 vpxor xmm12,xmm12,xmm4 vpshufb xmm14,xmm14,xmm0 vpxor xmm13,xmm13,xmm4 - vpshufb xmm1,xmm1,xmm0 + vpshufb xmm1,xmm1,xmm0 ; next counter value vpxor xmm14,xmm14,xmm4 jmp NEAR $L$oop_ctr32 @@ -622,16 +622,16 @@ EXTERN BORINGSSL_function_hit %endif xor rax,rax - - - - cmp r8,0x60*3 +; We call |_aesni_ctr32_6x| twice, each call consuming 96 bytes of +; input. Then we call |_aesni_ctr32_ghash_6x|, which requires at +; least 96 more bytes of input. + cmp r8,0x60*3 ; minimal accepted length jb NEAR $L$gcm_enc_abort push rbp $L$SEH_prologue_aesni_gcm_encrypt_2: - mov rbp,rsp + mov rbp,rsp ; save stack pointer push rbx @@ -648,19 +648,19 @@ $L$SEH_prologue_aesni_gcm_encrypt_6: push r15 $L$SEH_prologue_aesni_gcm_encrypt_7: - lea rsp,[((-168))+rsp] + lea rsp,[((-168))+rsp] ; 8 extra bytes to align the stack $L$SEH_prologue_aesni_gcm_encrypt_8: $L$SEH_prologue_aesni_gcm_encrypt_9: - - - +; Load the last two parameters. These go into %rdi and %rsi, which are +; non-volatile on Windows, so stash them in the parameter stack area +; first. mov QWORD[16+rbp],rdi $L$SEH_prologue_aesni_gcm_encrypt_10: mov QWORD[24+rbp],rsi $L$SEH_prologue_aesni_gcm_encrypt_11: mov rdi,QWORD[48+rbp] mov rsi,QWORD[56+rbp] - +; Save non-volatile XMM registers. movaps XMMWORD[(-208)+rbp],xmm6 $L$SEH_prologue_aesni_gcm_encrypt_12: movaps XMMWORD[(-192)+rbp],xmm7 @@ -684,15 +684,15 @@ $L$SEH_prologue_aesni_gcm_encrypt_21: $L$SEH_endprologue_aesni_gcm_encrypt_22: vzeroupper - vmovdqu xmm1,XMMWORD[rdi] + vmovdqu xmm1,XMMWORD[rdi] ; input counter value add rsp,-128 mov ebx,DWORD[12+rdi] lea r11,[$L$bswap_mask] - lea r14,[((-128))+r9] - mov r15,0xf80 - lea r9,[128+r9] - vmovdqu xmm0,XMMWORD[r11] - and rsp,-128 + lea r14,[((-128))+r9] ; borrow %r14 + mov r15,0xf80 ; borrow %r15 + lea r9,[128+r9] ; size optimization + vmovdqu xmm0,XMMWORD[r11] ; borrow %xmm0 for .Lbswap_mask + and rsp,-128 ; ensure stack alignment mov r10d,DWORD[((240-128))+r9] and r14,r15 @@ -701,24 +701,24 @@ $L$SEH_endprologue_aesni_gcm_encrypt_22: jc NEAR $L$enc_no_key_aliasing cmp r15,768 jnc NEAR $L$enc_no_key_aliasing - sub rsp,r15 + sub rsp,r15 ; avoid aliasing with key $L$enc_no_key_aliasing: mov r14,rdx - - - - - - - +; |_aesni_ctr32_ghash_6x| requires |%r15| to point to 2*96 (0xc0) +; bytes before the end of the input. Note, in particular, that this is +; correct even if |%r8| is not an even multiple of 96 or 16. Unlike in +; the decryption case, there's no caveat that |%rdx| must not be near +; the very beginning of the address space, because we know that +; |%r8| >= 3*96 from the check above, and so we know +; |%rdx| + |%r8| >= 2*96 (0xc0). lea r15,[((-192))+r8*1+rdx] shr r8,4 call _aesni_ctr32_6x - vpshufb xmm8,xmm9,xmm0 + vpshufb xmm8,xmm9,xmm0 ; save bswapped output on stack vpshufb xmm2,xmm10,xmm0 vmovdqu XMMWORD[112+rsp],xmm8 vpshufb xmm4,xmm11,xmm0 @@ -727,26 +727,26 @@ $L$enc_no_key_aliasing: vmovdqu XMMWORD[80+rsp],xmm4 vpshufb xmm6,xmm13,xmm0 vmovdqu XMMWORD[64+rsp],xmm5 - vpshufb xmm7,xmm14,xmm0 + vpshufb xmm7,xmm14,xmm0 ; passed to _aesni_ctr32_ghash_6x vmovdqu XMMWORD[48+rsp],xmm6 call _aesni_ctr32_6x mov r12,QWORD[64+rbp] - lea rsi,[32+rsi] - vmovdqu xmm8,XMMWORD[r12] + lea rsi,[32+rsi] ; size optimization + vmovdqu xmm8,XMMWORD[r12] ; load Xi sub r8,12 mov rax,0x60*2 vpshufb xmm8,xmm8,xmm0 call _aesni_ctr32_ghash_6x - vmovdqu xmm7,XMMWORD[32+rsp] - vmovdqu xmm0,XMMWORD[r11] - vmovdqu xmm3,XMMWORD[((0-32))+rsi] + vmovdqu xmm7,XMMWORD[32+rsp] ; I[5] + vmovdqu xmm0,XMMWORD[r11] ; borrow %xmm0 for .Lbswap_mask + vmovdqu xmm3,XMMWORD[((0-32))+rsi] ; %xmm3^1 vpunpckhqdq xmm1,xmm7,xmm7 - vmovdqu xmm15,XMMWORD[((32-32))+rsi] - vmovups XMMWORD[(-96)+rdx],xmm9 - vpshufb xmm9,xmm9,xmm0 + vmovdqu xmm15,XMMWORD[((32-32))+rsi] ; borrow %xmm15 for + vmovups XMMWORD[(-96)+rdx],xmm9 ; save output + vpshufb xmm9,xmm9,xmm0 ; but keep bswapped copy vpxor xmm1,xmm1,xmm7 vmovups XMMWORD[(-80)+rdx],xmm10 vpshufb xmm10,xmm10,xmm0 @@ -758,18 +758,18 @@ $L$enc_no_key_aliasing: vpshufb xmm13,xmm13,xmm0 vmovups XMMWORD[(-16)+rdx],xmm14 vpshufb xmm14,xmm14,xmm0 - vmovdqu XMMWORD[16+rsp],xmm9 - vmovdqu xmm6,XMMWORD[48+rsp] - vmovdqu xmm0,XMMWORD[((16-32))+rsi] + vmovdqu XMMWORD[16+rsp],xmm9 ; free %xmm9 + vmovdqu xmm6,XMMWORD[48+rsp] ; I[4] + vmovdqu xmm0,XMMWORD[((16-32))+rsi] ; borrow %xmm0 for %xmm3^2 vpunpckhqdq xmm2,xmm6,xmm6 vpclmulqdq xmm5,xmm7,xmm3,0x00 vpxor xmm2,xmm2,xmm6 vpclmulqdq xmm7,xmm7,xmm3,0x11 vpclmulqdq xmm1,xmm1,xmm15,0x00 - vmovdqu xmm9,XMMWORD[64+rsp] + vmovdqu xmm9,XMMWORD[64+rsp] ; I[3] vpclmulqdq xmm4,xmm6,xmm0,0x00 - vmovdqu xmm3,XMMWORD[((48-32))+rsi] + vmovdqu xmm3,XMMWORD[((48-32))+rsi] ; %xmm3^3 vpxor xmm4,xmm4,xmm5 vpunpckhqdq xmm5,xmm9,xmm9 vpclmulqdq xmm6,xmm6,xmm0,0x11 @@ -779,9 +779,9 @@ $L$enc_no_key_aliasing: vmovdqu xmm15,XMMWORD[((80-32))+rsi] vpxor xmm2,xmm2,xmm1 - vmovdqu xmm1,XMMWORD[80+rsp] + vmovdqu xmm1,XMMWORD[80+rsp] ; I[2] vpclmulqdq xmm7,xmm9,xmm3,0x00 - vmovdqu xmm0,XMMWORD[((64-32))+rsi] + vmovdqu xmm0,XMMWORD[((64-32))+rsi] ; borrow %xmm0 for %xmm3^4 vpxor xmm7,xmm7,xmm4 vpunpckhqdq xmm4,xmm1,xmm1 vpclmulqdq xmm9,xmm9,xmm3,0x11 @@ -790,9 +790,9 @@ $L$enc_no_key_aliasing: vpclmulqdq xmm5,xmm5,xmm15,0x00 vpxor xmm5,xmm5,xmm2 - vmovdqu xmm2,XMMWORD[96+rsp] + vmovdqu xmm2,XMMWORD[96+rsp] ; I[1] vpclmulqdq xmm6,xmm1,xmm0,0x00 - vmovdqu xmm3,XMMWORD[((96-32))+rsi] + vmovdqu xmm3,XMMWORD[((96-32))+rsi] ; %xmm3^5 vpxor xmm6,xmm6,xmm7 vpunpckhqdq xmm7,xmm2,xmm2 vpclmulqdq xmm1,xmm1,xmm0,0x11 @@ -802,9 +802,9 @@ $L$enc_no_key_aliasing: vmovdqu xmm15,XMMWORD[((128-32))+rsi] vpxor xmm4,xmm4,xmm5 - vpxor xmm8,xmm8,XMMWORD[112+rsp] + vpxor xmm8,xmm8,XMMWORD[112+rsp] ; accumulate I[0] vpclmulqdq xmm5,xmm2,xmm3,0x00 - vmovdqu xmm0,XMMWORD[((112-32))+rsi] + vmovdqu xmm0,XMMWORD[((112-32))+rsi] ; borrow %xmm0 for %xmm3^6 vpunpckhqdq xmm9,xmm8,xmm8 vpxor xmm5,xmm5,xmm6 vpclmulqdq xmm2,xmm2,xmm3,0x11 @@ -814,7 +814,7 @@ $L$enc_no_key_aliasing: vpxor xmm4,xmm7,xmm4 vpclmulqdq xmm6,xmm8,xmm0,0x00 - vmovdqu xmm3,XMMWORD[((0-32))+rsi] + vmovdqu xmm3,XMMWORD[((0-32))+rsi] ; %xmm3^1 vpunpckhqdq xmm1,xmm14,xmm14 vpclmulqdq xmm8,xmm8,xmm0,0x11 vpxor xmm1,xmm1,xmm14 @@ -824,8 +824,8 @@ $L$enc_no_key_aliasing: vpxor xmm7,xmm8,xmm2 vpxor xmm6,xmm9,xmm4 - vmovdqu xmm0,XMMWORD[((16-32))+rsi] - vpxor xmm9,xmm7,xmm5 + vmovdqu xmm0,XMMWORD[((16-32))+rsi] ; borrow %xmm0 for %xmm3^2 + vpxor xmm9,xmm7,xmm5 ; aggregated Karatsuba post-processing vpclmulqdq xmm4,xmm14,xmm3,0x00 vpxor xmm6,xmm6,xmm9 vpunpckhqdq xmm2,xmm13,xmm13 @@ -838,25 +838,25 @@ $L$enc_no_key_aliasing: vpxor xmm7,xmm7,xmm6 vpclmulqdq xmm5,xmm13,xmm0,0x00 - vmovdqu xmm3,XMMWORD[((48-32))+rsi] + vmovdqu xmm3,XMMWORD[((48-32))+rsi] ; %xmm3^3 vpxor xmm5,xmm5,xmm4 vpunpckhqdq xmm9,xmm12,xmm12 vpclmulqdq xmm13,xmm13,xmm0,0x11 vpxor xmm9,xmm9,xmm12 vpxor xmm13,xmm13,xmm14 - vpalignr xmm14,xmm8,xmm8,8 + vpalignr xmm14,xmm8,xmm8,8 ; 1st phase vpclmulqdq xmm2,xmm2,xmm15,0x10 vmovdqu xmm15,XMMWORD[((80-32))+rsi] vpxor xmm2,xmm2,xmm1 vpclmulqdq xmm4,xmm12,xmm3,0x00 - vmovdqu xmm0,XMMWORD[((64-32))+rsi] + vmovdqu xmm0,XMMWORD[((64-32))+rsi] ; borrow %xmm0 for %xmm3^4 vpxor xmm4,xmm4,xmm5 vpunpckhqdq xmm1,xmm11,xmm11 vpclmulqdq xmm12,xmm12,xmm3,0x11 vpxor xmm1,xmm1,xmm11 vpxor xmm12,xmm12,xmm13 - vxorps xmm7,xmm7,XMMWORD[16+rsp] + vxorps xmm7,xmm7,XMMWORD[16+rsp] ; accumulate %xmm9 vpclmulqdq xmm9,xmm9,xmm15,0x00 vpxor xmm9,xmm9,xmm2 @@ -864,12 +864,12 @@ $L$enc_no_key_aliasing: vxorps xmm8,xmm8,xmm14 vpclmulqdq xmm5,xmm11,xmm0,0x00 - vmovdqu xmm3,XMMWORD[((96-32))+rsi] + vmovdqu xmm3,XMMWORD[((96-32))+rsi] ; %xmm3^5 vpxor xmm5,xmm5,xmm4 vpunpckhqdq xmm2,xmm10,xmm10 vpclmulqdq xmm11,xmm11,xmm0,0x11 vpxor xmm2,xmm2,xmm10 - vpalignr xmm14,xmm8,xmm8,8 + vpalignr xmm14,xmm8,xmm8,8 ; 2nd phase vpxor xmm11,xmm11,xmm12 vpclmulqdq xmm1,xmm1,xmm15,0x10 vmovdqu xmm15,XMMWORD[((128-32))+rsi] @@ -880,7 +880,7 @@ $L$enc_no_key_aliasing: vxorps xmm8,xmm8,xmm14 vpclmulqdq xmm4,xmm10,xmm3,0x00 - vmovdqu xmm0,XMMWORD[((112-32))+rsi] + vmovdqu xmm0,XMMWORD[((112-32))+rsi] ; borrow %xmm0 for %xmm3^6 vpxor xmm4,xmm4,xmm5 vpunpckhqdq xmm9,xmm8,xmm8 vpclmulqdq xmm10,xmm10,xmm3,0x11 @@ -896,25 +896,25 @@ $L$enc_no_key_aliasing: vpxor xmm7,xmm7,xmm10 vpxor xmm6,xmm6,xmm2 - vpxor xmm4,xmm7,xmm5 + vpxor xmm4,xmm7,xmm5 ; aggregated Karatsuba post-processing vpxor xmm6,xmm6,xmm4 vpslldq xmm1,xmm6,8 - vmovdqu xmm3,XMMWORD[16+r11] + vmovdqu xmm3,XMMWORD[16+r11] ; .Lpoly vpsrldq xmm6,xmm6,8 vpxor xmm8,xmm5,xmm1 vpxor xmm7,xmm7,xmm6 - vpalignr xmm2,xmm8,xmm8,8 + vpalignr xmm2,xmm8,xmm8,8 ; 1st phase vpclmulqdq xmm8,xmm8,xmm3,0x10 vpxor xmm8,xmm8,xmm2 - vpalignr xmm2,xmm8,xmm8,8 + vpalignr xmm2,xmm8,xmm8,8 ; 2nd phase vpclmulqdq xmm8,xmm8,xmm3,0x10 vpxor xmm2,xmm2,xmm7 vpxor xmm8,xmm8,xmm2 mov r12,QWORD[64+rbp] - vpshufb xmm8,xmm8,XMMWORD[r11] - vmovdqu XMMWORD[r12],xmm8 + vpshufb xmm8,xmm8,XMMWORD[r11] ; .Lbswap_mask + vmovdqu XMMWORD[r12],xmm8 ; output Xi vzeroupper movaps xmm6,XMMWORD[((-208))+rbp] @@ -929,7 +929,7 @@ $L$enc_no_key_aliasing: movaps xmm15,XMMWORD[((-64))+rbp] mov rdi,QWORD[16+rbp] mov rsi,QWORD[24+rbp] - lea rsp,[((-40))+rbp] + lea rsp,[((-40))+rbp] ; restore %rsp to fixed allocation pop r15 @@ -949,7 +949,9 @@ $L$SEH_end_aesni_gcm_encrypt_23: section .rdata rdata align=8 + ALIGN 64 +aesni_gcm_constants: $L$bswap_mask: DB 15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0 $L$poly: @@ -965,7 +967,7 @@ $L$one_lsb: DB 89,80,84,79,71,65,77,83,32,98,121,32,60,97,112,112 DB 114,111,64,111,112,101,110,115,115,108,46,111,114,103,62,0 ALIGN 64 -section .text +section .text code align=64 section .pdata rdata align=4 ALIGN 4 @@ -981,7 +983,7 @@ ALIGN 4 section .xdata rdata align=8 ALIGN 4 $L$SEH_info_aesni_gcm_decrypt_0: - DB 1 + DB 1 ; version 1, no flags DB $L$SEH_endprologue_aesni_gcm_decrypt_22-$L$SEH_begin_aesni_gcm_decrypt_1 DB 33 DB 213 @@ -1041,7 +1043,7 @@ $L$SEH_info_aesni_gcm_decrypt_0: DW 0 $L$SEH_info_aesni_gcm_encrypt_0: - DB 1 + DB 1 ; version 1, no flags DB $L$SEH_endprologue_aesni_gcm_encrypt_22-$L$SEH_begin_aesni_gcm_encrypt_1 DB 33 DB 213 diff --git a/third_party/boringssl/gen/bcm/aesni-x86_64-apple.S b/third_party/boringssl/gen/bcm/aesni-x86_64-apple.S index 958cc5a2f..5ae78fb21 100644 --- a/third_party/boringssl/gen/bcm/aesni-x86_64-apple.S +++ b/third_party/boringssl/gen/bcm/aesni-x86_64-apple.S @@ -16,8 +16,8 @@ _CET_ENDBR movb $1,_BORINGSSL_function_hit+1(%rip) #endif - movups (%rdi),%xmm2 - movl 240(%rdx),%eax + movups (%rdi),%xmm2 // load input + movl 240(%rdx),%eax // key->rounds movups (%rdx),%xmm0 movups 16(%rdx),%xmm1 leaq 32(%rdx),%rdx @@ -27,11 +27,11 @@ L$oop_enc1_1: decl %eax movups (%rdx),%xmm1 leaq 16(%rdx),%rdx - jnz L$oop_enc1_1 + jnz L$oop_enc1_1 // loop body is 16 bytes aesenclast %xmm1,%xmm2 - pxor %xmm0,%xmm0 + pxor %xmm0,%xmm0 // clear register bank pxor %xmm1,%xmm1 - movups %xmm2,(%rsi) + movups %xmm2,(%rsi) // output pxor %xmm2,%xmm2 ret @@ -41,11 +41,12 @@ L$oop_enc1_1: .private_extern _aes_hw_decrypt .p2align 4 +.alt_entry _aes_hw_decrypt _aes_hw_decrypt: _CET_ENDBR - movups (%rdi),%xmm2 - movl 240(%rdx),%eax + movups (%rdi),%xmm2 // load input + movl 240(%rdx),%eax // key->rounds movups (%rdx),%xmm0 movups 16(%rdx),%xmm1 leaq 32(%rdx),%rdx @@ -55,17 +56,18 @@ L$oop_dec1_2: decl %eax movups (%rdx),%xmm1 leaq 16(%rdx),%rdx - jnz L$oop_dec1_2 + jnz L$oop_dec1_2 // loop body is 16 bytes aesdeclast %xmm1,%xmm2 - pxor %xmm0,%xmm0 + pxor %xmm0,%xmm0 // clear register bank pxor %xmm1,%xmm1 - movups %xmm2,(%rsi) + movups %xmm2,(%rsi) // output pxor %xmm2,%xmm2 ret .p2align 4 +.alt_entry _aesni_encrypt2 _aesni_encrypt2: movups (%rcx),%xmm0 @@ -75,7 +77,7 @@ _aesni_encrypt2: xorps %xmm0,%xmm3 movups 32(%rcx),%xmm0 leaq 32(%rcx,%rax,1),%rcx - negq %rax + negq %rax // %eax addq $16,%rax L$enc_loop2: @@ -97,6 +99,7 @@ L$enc_loop2: .p2align 4 +.alt_entry _aesni_decrypt2 _aesni_decrypt2: movups (%rcx),%xmm0 @@ -106,7 +109,7 @@ _aesni_decrypt2: xorps %xmm0,%xmm3 movups 32(%rcx),%xmm0 leaq 32(%rcx,%rax,1),%rcx - negq %rax + negq %rax // %eax addq $16,%rax L$dec_loop2: @@ -128,6 +131,7 @@ L$dec_loop2: .p2align 4 +.alt_entry _aesni_encrypt3 _aesni_encrypt3: movups (%rcx),%xmm0 @@ -138,7 +142,7 @@ _aesni_encrypt3: xorps %xmm0,%xmm4 movups 32(%rcx),%xmm0 leaq 32(%rcx,%rax,1),%rcx - negq %rax + negq %rax // %eax addq $16,%rax L$enc_loop3: @@ -164,6 +168,7 @@ L$enc_loop3: .p2align 4 +.alt_entry _aesni_decrypt3 _aesni_decrypt3: movups (%rcx),%xmm0 @@ -174,7 +179,7 @@ _aesni_decrypt3: xorps %xmm0,%xmm4 movups 32(%rcx),%xmm0 leaq 32(%rcx,%rax,1),%rcx - negq %rax + negq %rax // %eax addq $16,%rax L$dec_loop3: @@ -200,6 +205,7 @@ L$dec_loop3: .p2align 4 +.alt_entry _aesni_encrypt4 _aesni_encrypt4: movups (%rcx),%xmm0 @@ -211,7 +217,7 @@ _aesni_encrypt4: xorps %xmm0,%xmm5 movups 32(%rcx),%xmm0 leaq 32(%rcx,%rax,1),%rcx - negq %rax + negq %rax // %eax .byte 0x0f,0x1f,0x00 addq $16,%rax @@ -242,6 +248,7 @@ L$enc_loop4: .p2align 4 +.alt_entry _aesni_decrypt4 _aesni_decrypt4: movups (%rcx),%xmm0 @@ -253,7 +260,7 @@ _aesni_decrypt4: xorps %xmm0,%xmm5 movups 32(%rcx),%xmm0 leaq 32(%rcx,%rax,1),%rcx - negq %rax + negq %rax // %eax .byte 0x0f,0x1f,0x00 addq $16,%rax @@ -284,6 +291,7 @@ L$dec_loop4: .p2align 4 +.alt_entry _aesni_encrypt6 _aesni_encrypt6: movups (%rcx),%xmm0 @@ -294,7 +302,7 @@ _aesni_encrypt6: pxor %xmm0,%xmm4 aesenc %xmm1,%xmm2 leaq 32(%rcx,%rax,1),%rcx - negq %rax + negq %rax // %eax aesenc %xmm1,%xmm3 pxor %xmm0,%xmm5 pxor %xmm0,%xmm6 @@ -340,6 +348,7 @@ L$enc_loop6_enter: .p2align 4 +.alt_entry _aesni_decrypt6 _aesni_decrypt6: movups (%rcx),%xmm0 @@ -350,7 +359,7 @@ _aesni_decrypt6: pxor %xmm0,%xmm4 aesdec %xmm1,%xmm2 leaq 32(%rcx,%rax,1),%rcx - negq %rax + negq %rax // %eax aesdec %xmm1,%xmm3 pxor %xmm0,%xmm5 pxor %xmm0,%xmm6 @@ -396,6 +405,7 @@ L$dec_loop6_enter: .p2align 4 +.alt_entry _aesni_encrypt8 _aesni_encrypt8: movups (%rcx),%xmm0 @@ -407,7 +417,7 @@ _aesni_encrypt8: pxor %xmm0,%xmm5 pxor %xmm0,%xmm6 leaq 32(%rcx,%rax,1),%rcx - negq %rax + negq %rax // %eax aesenc %xmm1,%xmm2 pxor %xmm0,%xmm7 pxor %xmm0,%xmm8 @@ -462,6 +472,7 @@ L$enc_loop8_enter: .p2align 4 +.alt_entry _aesni_decrypt8 _aesni_decrypt8: movups (%rcx),%xmm0 @@ -473,7 +484,7 @@ _aesni_decrypt8: pxor %xmm0,%xmm5 pxor %xmm0,%xmm6 leaq 32(%rcx,%rax,1),%rcx - negq %rax + negq %rax // %eax aesdec %xmm1,%xmm2 pxor %xmm0,%xmm7 pxor %xmm0,%xmm8 @@ -530,23 +541,24 @@ L$dec_loop8_enter: .private_extern _aes_hw_ecb_encrypt .p2align 4 +.alt_entry _aes_hw_ecb_encrypt _aes_hw_ecb_encrypt: _CET_ENDBR - andq $-16,%rdx - jz L$ecb_ret + andq $-16,%rdx // if (%rdx<16) + jz L$ecb_ret // return - movl 240(%rcx),%eax + movl 240(%rcx),%eax // key->rounds movups (%rcx),%xmm0 - movq %rcx,%r11 - movl %eax,%r10d - testl %r8d,%r8d + movq %rcx,%r11 // backup %rcx + movl %eax,%r10d // backup %eax + testl %r8d,%r8d // 5th argument jz L$ecb_decrypt +// --------------------------- ECB ENCRYPT ------------------------------ + cmpq $0x80,%rdx // if (%rdx<8*16) + jb L$ecb_enc_tail // short input - cmpq $0x80,%rdx - jb L$ecb_enc_tail - - movdqu (%rdi),%xmm2 + movdqu (%rdi),%xmm2 // load 8 input blocks movdqu 16(%rdi),%xmm3 movdqu 32(%rdi),%xmm4 movdqu 48(%rdi),%xmm5 @@ -554,15 +566,15 @@ _CET_ENDBR movdqu 80(%rdi),%xmm7 movdqu 96(%rdi),%xmm8 movdqu 112(%rdi),%xmm9 - leaq 128(%rdi),%rdi - subq $0x80,%rdx + leaq 128(%rdi),%rdi // %rdi+=8*16 + subq $0x80,%rdx // %rdx-=8*16 (can be zero) jmp L$ecb_enc_loop8_enter .p2align 4 L$ecb_enc_loop8: - movups %xmm2,(%rsi) - movq %r11,%rcx - movdqu (%rdi),%xmm2 - movl %r10d,%eax + movups %xmm2,(%rsi) // store 8 output blocks + movq %r11,%rcx // restore %rcx + movdqu (%rdi),%xmm2 // load 8 input blocks + movl %r10d,%eax // restore %eax movups %xmm3,16(%rsi) movdqu 16(%rdi),%xmm3 movups %xmm4,32(%rsi) @@ -576,31 +588,31 @@ L$ecb_enc_loop8: movups %xmm8,96(%rsi) movdqu 96(%rdi),%xmm8 movups %xmm9,112(%rsi) - leaq 128(%rsi),%rsi + leaq 128(%rsi),%rsi // %rsi+=8*16 movdqu 112(%rdi),%xmm9 - leaq 128(%rdi),%rdi + leaq 128(%rdi),%rdi // %rdi+=8*16 L$ecb_enc_loop8_enter: call _aesni_encrypt8 subq $0x80,%rdx - jnc L$ecb_enc_loop8 + jnc L$ecb_enc_loop8 // loop if %rdx-=8*16 didn't borrow - movups %xmm2,(%rsi) - movq %r11,%rcx + movups %xmm2,(%rsi) // store 8 output blocks + movq %r11,%rcx // restore %rcx movups %xmm3,16(%rsi) - movl %r10d,%eax + movl %r10d,%eax // restore %eax movups %xmm4,32(%rsi) movups %xmm5,48(%rsi) movups %xmm6,64(%rsi) movups %xmm7,80(%rsi) movups %xmm8,96(%rsi) movups %xmm9,112(%rsi) - leaq 128(%rsi),%rsi - addq $0x80,%rdx - jz L$ecb_ret + leaq 128(%rsi),%rsi // %rsi+=8*16 + addq $0x80,%rdx // restore real remaining %rdx + jz L$ecb_ret // done if (%rdx==0) -L$ecb_enc_tail: +L$ecb_enc_tail: // %rdx is less than 8*16 movups (%rdi),%xmm2 cmpq $0x20,%rdx jb L$ecb_enc_one @@ -619,7 +631,7 @@ L$ecb_enc_tail: movdqu 96(%rdi),%xmm8 xorps %xmm9,%xmm9 call _aesni_encrypt8 - movups %xmm2,(%rsi) + movups %xmm2,(%rsi) // store 7 output blocks movups %xmm3,16(%rsi) movups %xmm4,32(%rsi) movups %xmm5,48(%rsi) @@ -638,27 +650,27 @@ L$oop_enc1_3: decl %eax movups (%rcx),%xmm1 leaq 16(%rcx),%rcx - jnz L$oop_enc1_3 + jnz L$oop_enc1_3 // loop body is 16 bytes aesenclast %xmm1,%xmm2 - movups %xmm2,(%rsi) + movups %xmm2,(%rsi) // store one output block jmp L$ecb_ret .p2align 4 L$ecb_enc_two: call _aesni_encrypt2 - movups %xmm2,(%rsi) + movups %xmm2,(%rsi) // store 2 output blocks movups %xmm3,16(%rsi) jmp L$ecb_ret .p2align 4 L$ecb_enc_three: call _aesni_encrypt3 - movups %xmm2,(%rsi) + movups %xmm2,(%rsi) // store 3 output blocks movups %xmm3,16(%rsi) movups %xmm4,32(%rsi) jmp L$ecb_ret .p2align 4 L$ecb_enc_four: call _aesni_encrypt4 - movups %xmm2,(%rsi) + movups %xmm2,(%rsi) // store 4 output blocks movups %xmm3,16(%rsi) movups %xmm4,32(%rsi) movups %xmm5,48(%rsi) @@ -667,7 +679,7 @@ L$ecb_enc_four: L$ecb_enc_five: xorps %xmm7,%xmm7 call _aesni_encrypt6 - movups %xmm2,(%rsi) + movups %xmm2,(%rsi) // store 5 output blocks movups %xmm3,16(%rsi) movups %xmm4,32(%rsi) movups %xmm5,48(%rsi) @@ -676,20 +688,20 @@ L$ecb_enc_five: .p2align 4 L$ecb_enc_six: call _aesni_encrypt6 - movups %xmm2,(%rsi) + movups %xmm2,(%rsi) // store 6 output blocks movups %xmm3,16(%rsi) movups %xmm4,32(%rsi) movups %xmm5,48(%rsi) movups %xmm6,64(%rsi) movups %xmm7,80(%rsi) jmp L$ecb_ret - +// --------------------------- ECB DECRYPT ------------------------------# .p2align 4 L$ecb_decrypt: - cmpq $0x80,%rdx - jb L$ecb_dec_tail + cmpq $0x80,%rdx // if (%rdx<8*16) + jb L$ecb_dec_tail // short input - movdqu (%rdi),%xmm2 + movdqu (%rdi),%xmm2 // load 8 input blocks movdqu 16(%rdi),%xmm3 movdqu 32(%rdi),%xmm4 movdqu 48(%rdi),%xmm5 @@ -697,15 +709,15 @@ L$ecb_decrypt: movdqu 80(%rdi),%xmm7 movdqu 96(%rdi),%xmm8 movdqu 112(%rdi),%xmm9 - leaq 128(%rdi),%rdi - subq $0x80,%rdx + leaq 128(%rdi),%rdi // %rdi+=8*16 + subq $0x80,%rdx // %rdx-=8*16 (can be zero) jmp L$ecb_dec_loop8_enter .p2align 4 L$ecb_dec_loop8: - movups %xmm2,(%rsi) - movq %r11,%rcx - movdqu (%rdi),%xmm2 - movl %r10d,%eax + movups %xmm2,(%rsi) // store 8 output blocks + movq %r11,%rcx // restore %rcx + movdqu (%rdi),%xmm2 // load 8 input blocks + movl %r10d,%eax // restore %eax movups %xmm3,16(%rsi) movdqu 16(%rdi),%xmm3 movups %xmm4,32(%rsi) @@ -719,23 +731,23 @@ L$ecb_dec_loop8: movups %xmm8,96(%rsi) movdqu 96(%rdi),%xmm8 movups %xmm9,112(%rsi) - leaq 128(%rsi),%rsi + leaq 128(%rsi),%rsi // %rsi+=8*16 movdqu 112(%rdi),%xmm9 - leaq 128(%rdi),%rdi + leaq 128(%rdi),%rdi // %rdi+=8*16 L$ecb_dec_loop8_enter: call _aesni_decrypt8 movups (%r11),%xmm0 subq $0x80,%rdx - jnc L$ecb_dec_loop8 + jnc L$ecb_dec_loop8 // loop if %rdx-=8*16 didn't borrow - movups %xmm2,(%rsi) - pxor %xmm2,%xmm2 - movq %r11,%rcx + movups %xmm2,(%rsi) // store 8 output blocks + pxor %xmm2,%xmm2 // clear register bank + movq %r11,%rcx // restore %rcx movups %xmm3,16(%rsi) pxor %xmm3,%xmm3 - movl %r10d,%eax + movl %r10d,%eax // restore %eax movups %xmm4,32(%rsi) pxor %xmm4,%xmm4 movups %xmm5,48(%rsi) @@ -748,9 +760,9 @@ L$ecb_dec_loop8_enter: pxor %xmm8,%xmm8 movups %xmm9,112(%rsi) pxor %xmm9,%xmm9 - leaq 128(%rsi),%rsi - addq $0x80,%rdx - jz L$ecb_ret + leaq 128(%rsi),%rsi // %rsi+=8*16 + addq $0x80,%rdx // restore real remaining %rdx + jz L$ecb_ret // done if (%rdx==0) L$ecb_dec_tail: movups (%rdi),%xmm2 @@ -772,8 +784,8 @@ L$ecb_dec_tail: movups (%rcx),%xmm0 xorps %xmm9,%xmm9 call _aesni_decrypt8 - movups %xmm2,(%rsi) - pxor %xmm2,%xmm2 + movups %xmm2,(%rsi) // store 7 output blocks + pxor %xmm2,%xmm2 // clear register bank movups %xmm3,16(%rsi) pxor %xmm3,%xmm3 movups %xmm4,32(%rsi) @@ -799,24 +811,24 @@ L$oop_dec1_4: decl %eax movups (%rcx),%xmm1 leaq 16(%rcx),%rcx - jnz L$oop_dec1_4 + jnz L$oop_dec1_4 // loop body is 16 bytes aesdeclast %xmm1,%xmm2 - movups %xmm2,(%rsi) - pxor %xmm2,%xmm2 + movups %xmm2,(%rsi) // store one output block + pxor %xmm2,%xmm2 // clear register bank jmp L$ecb_ret .p2align 4 L$ecb_dec_two: call _aesni_decrypt2 - movups %xmm2,(%rsi) - pxor %xmm2,%xmm2 + movups %xmm2,(%rsi) // store 2 output blocks + pxor %xmm2,%xmm2 // clear register bank movups %xmm3,16(%rsi) pxor %xmm3,%xmm3 jmp L$ecb_ret .p2align 4 L$ecb_dec_three: call _aesni_decrypt3 - movups %xmm2,(%rsi) - pxor %xmm2,%xmm2 + movups %xmm2,(%rsi) // store 3 output blocks + pxor %xmm2,%xmm2 // clear register bank movups %xmm3,16(%rsi) pxor %xmm3,%xmm3 movups %xmm4,32(%rsi) @@ -825,8 +837,8 @@ L$ecb_dec_three: .p2align 4 L$ecb_dec_four: call _aesni_decrypt4 - movups %xmm2,(%rsi) - pxor %xmm2,%xmm2 + movups %xmm2,(%rsi) // store 4 output blocks + pxor %xmm2,%xmm2 // clear register bank movups %xmm3,16(%rsi) pxor %xmm3,%xmm3 movups %xmm4,32(%rsi) @@ -838,8 +850,8 @@ L$ecb_dec_four: L$ecb_dec_five: xorps %xmm7,%xmm7 call _aesni_decrypt6 - movups %xmm2,(%rsi) - pxor %xmm2,%xmm2 + movups %xmm2,(%rsi) // store 5 output blocks + pxor %xmm2,%xmm2 // clear register bank movups %xmm3,16(%rsi) pxor %xmm3,%xmm3 movups %xmm4,32(%rsi) @@ -853,8 +865,8 @@ L$ecb_dec_five: .p2align 4 L$ecb_dec_six: call _aesni_decrypt6 - movups %xmm2,(%rsi) - pxor %xmm2,%xmm2 + movups %xmm2,(%rsi) // store 6 output blocks + pxor %xmm2,%xmm2 // clear register bank movups %xmm3,16(%rsi) pxor %xmm3,%xmm3 movups %xmm4,32(%rsi) @@ -867,7 +879,7 @@ L$ecb_dec_six: pxor %xmm7,%xmm7 L$ecb_ret: - xorps %xmm0,%xmm0 + xorps %xmm0,%xmm0 // %xmm0 pxor %xmm1,%xmm1 ret @@ -876,6 +888,7 @@ L$ecb_ret: .private_extern _aes_hw_ctr32_encrypt_blocks .p2align 4 +.alt_entry _aes_hw_ctr32_encrypt_blocks _aes_hw_ctr32_encrypt_blocks: _CET_ENDBR @@ -885,11 +898,11 @@ _CET_ENDBR cmpq $1,%rdx jne L$ctr32_bulk - - +// handle single block without allocating stack frame, +// useful when handling edges movups (%r8),%xmm2 movups (%rdi),%xmm3 - movl 240(%rcx),%edx + movl 240(%rcx),%edx // key->rounds movups (%rcx),%xmm0 movups 16(%rcx),%xmm1 leaq 32(%rcx),%rcx @@ -899,9 +912,9 @@ L$oop_enc1_5: decl %edx movups (%rcx),%xmm1 leaq 16(%rcx),%rcx - jnz L$oop_enc1_5 + jnz L$oop_enc1_5 // loop body is 16 bytes aesenclast %xmm1,%xmm2 - pxor %xmm0,%xmm0 + pxor %xmm0,%xmm0 // clear register bank pxor %xmm1,%xmm1 xorps %xmm3,%xmm2 pxor %xmm3,%xmm3 @@ -911,22 +924,22 @@ L$oop_enc1_5: .p2align 4 L$ctr32_bulk: - leaq (%rsp),%r11 + leaq (%rsp),%r11 // use %r11 as frame pointer pushq %rbp subq $128,%rsp - andq $-16,%rsp - - + andq $-16,%rsp // Linux kernel stack can be incorrectly seeded +// 8 16-byte words on top of stack are counter values +// xor-ed with zero-round key movdqu (%r8),%xmm2 movdqu (%rcx),%xmm0 - movl 12(%r8),%r8d + movl 12(%r8),%r8d // counter LSB pxor %xmm0,%xmm2 - movl 12(%rcx),%ebp - movdqa %xmm2,0(%rsp) + movl 12(%rcx),%ebp // 0-round key LSB + movdqa %xmm2,0(%rsp) // populate counter block bswapl %r8d movdqa %xmm2,%xmm3 movdqa %xmm2,%xmm4 @@ -934,7 +947,7 @@ L$ctr32_bulk: movdqa %xmm2,64(%rsp) movdqa %xmm2,80(%rsp) movdqa %xmm2,96(%rsp) - movq %rdx,%r10 + movq %rdx,%r10 // about to borrow %rdx movdqa %xmm2,112(%rsp) leaq 1(%r8),%rax @@ -948,7 +961,7 @@ L$ctr32_bulk: movdqa %xmm3,16(%rsp) pinsrd $3,%edx,%xmm4 bswapl %eax - movq %r10,%rdx + movq %r10,%rdx // restore %rdx leaq 4(%r8),%r10 movdqa %xmm4,32(%rsp) xorl %ebp,%eax @@ -960,7 +973,7 @@ L$ctr32_bulk: movl %r10d,64+12(%rsp) bswapl %r9d leaq 6(%r8),%r10 - movl 240(%rcx),%eax + movl 240(%rcx),%eax // key->rounds xorl %ebp,%r9d bswapl %r10d movl %r9d,80+12(%rsp) @@ -976,16 +989,16 @@ L$ctr32_bulk: movdqa 64(%rsp),%xmm6 movdqa 80(%rsp),%xmm7 - cmpq $8,%rdx - jb L$ctr32_tail + cmpq $8,%rdx // %rdx is in blocks + jb L$ctr32_tail // short input if (%rdx<8) - leaq 128(%rcx),%rcx - subq $8,%rdx + leaq 128(%rcx),%rcx // size optimization + subq $8,%rdx // %rdx is biased by -8 jmp L$ctr32_loop8 .p2align 5 L$ctr32_loop8: - addl $8,%r8d + addl $8,%r8d // next counter value movdqa 96(%rsp),%xmm8 aesenc %xmm1,%xmm2 movl %r8d,%r9d @@ -997,7 +1010,7 @@ L$ctr32_loop8: xorl %ebp,%r9d nop aesenc %xmm1,%xmm5 - movl %r9d,0+12(%rsp) + movl %r9d,0+12(%rsp) // store next counter value leaq 1(%r8),%r9 aesenc %xmm1,%xmm6 aesenc %xmm1,%xmm7 @@ -1093,7 +1106,7 @@ L$ctr32_loop8: aesenc %xmm0,%xmm3 aesenc %xmm0,%xmm4 xorl %ebp,%r9d - movdqu 0(%rdi),%xmm10 + movdqu 0(%rdi),%xmm10 // start loading input aesenc %xmm0,%xmm5 movl %r9d,112+12(%rsp) cmpl $11,%eax @@ -1150,7 +1163,7 @@ L$ctr32_loop8: .p2align 4 L$ctr32_enc_done: movdqu 16(%rdi),%xmm11 - pxor %xmm0,%xmm10 + pxor %xmm0,%xmm10 // input^=round[last] movdqu 32(%rdi),%xmm12 pxor %xmm0,%xmm11 movdqu 48(%rdi),%xmm13 @@ -1159,8 +1172,8 @@ L$ctr32_enc_done: pxor %xmm0,%xmm13 movdqu 80(%rdi),%xmm15 pxor %xmm0,%xmm14 - prefetcht0 448(%rdi) - prefetcht0 512(%rdi) + prefetcht0 448(%rdi) // We process 128 bytes (8*16), so to prefetch 1 iteration + prefetcht0 512(%rdi) // We need to prefetch 2 64 byte lines pxor %xmm0,%xmm15 aesenc %xmm1,%xmm2 aesenc %xmm1,%xmm3 @@ -1170,15 +1183,15 @@ L$ctr32_enc_done: aesenc %xmm1,%xmm7 aesenc %xmm1,%xmm8 aesenc %xmm1,%xmm9 - movdqu 96(%rdi),%xmm1 - leaq 128(%rdi),%rdi + movdqu 96(%rdi),%xmm1 // borrow %xmm1 for inp[6] + leaq 128(%rdi),%rdi // %rdi+=8*16 - aesenclast %xmm10,%xmm2 - pxor %xmm0,%xmm1 + aesenclast %xmm10,%xmm2 // is inp[N]^round[last] + pxor %xmm0,%xmm1 // borrowed movdqu 112-128(%rdi),%xmm10 aesenclast %xmm11,%xmm3 pxor %xmm0,%xmm10 - movdqa 0(%rsp),%xmm11 + movdqa 0(%rsp),%xmm11 // load next counter block aesenclast %xmm12,%xmm4 aesenclast %xmm13,%xmm5 movdqa 16(%rsp),%xmm12 @@ -1189,10 +1202,10 @@ L$ctr32_enc_done: movdqa 64(%rsp),%xmm15 aesenclast %xmm1,%xmm8 movdqa 80(%rsp),%xmm0 - movups 16-128(%rcx),%xmm1 + movups 16-128(%rcx),%xmm1 // real 1st-round key aesenclast %xmm10,%xmm9 - movups %xmm2,(%rsi) + movups %xmm2,(%rsi) // store 8 output blocks movdqa %xmm11,%xmm2 movups %xmm3,16(%rsi) movdqa %xmm12,%xmm3 @@ -1206,24 +1219,24 @@ L$ctr32_enc_done: movdqa %xmm0,%xmm7 movups %xmm8,96(%rsi) movups %xmm9,112(%rsi) - leaq 128(%rsi),%rsi + leaq 128(%rsi),%rsi // %rsi+=8*16 subq $8,%rdx - jnc L$ctr32_loop8 + jnc L$ctr32_loop8 // loop if %rdx-=8 didn't borrow - addq $8,%rdx - jz L$ctr32_done + addq $8,%rdx // restore real remaining %rdx + jz L$ctr32_done // done if (%rdx==0) leaq -128(%rcx),%rcx L$ctr32_tail: - - +// note that at this point %xmm2..5 are populated with +// counter values xor-ed with 0-round key leaq 16(%rcx),%rcx cmpq $4,%rdx jb L$ctr32_loop3 je L$ctr32_loop4 - +// if (%rdx>4) compute 7 E(counter) shll $4,%eax movdqa 96(%rsp),%xmm8 pxor %xmm9,%xmm9 @@ -1231,14 +1244,14 @@ L$ctr32_tail: movups 16(%rcx),%xmm0 aesenc %xmm1,%xmm2 aesenc %xmm1,%xmm3 - leaq 32-16(%rcx,%rax,1),%rcx + leaq 32-16(%rcx,%rax,1),%rcx // prepare for .Lenc_loop8_enter negq %rax aesenc %xmm1,%xmm4 - addq $16,%rax + addq $16,%rax // prepare for .Lenc_loop8_enter movups (%rdi),%xmm10 aesenc %xmm1,%xmm5 aesenc %xmm1,%xmm6 - movups 16(%rdi),%xmm11 + movups 16(%rdi),%xmm11 // pre-load input movups 32(%rdi),%xmm12 aesenc %xmm1,%xmm7 aesenc %xmm1,%xmm8 @@ -1249,7 +1262,7 @@ L$ctr32_tail: pxor %xmm10,%xmm2 movdqu 64(%rdi),%xmm10 pxor %xmm11,%xmm3 - movdqu %xmm2,(%rsi) + movdqu %xmm2,(%rsi) // store output pxor %xmm12,%xmm4 movdqu %xmm3,16(%rsi) pxor %xmm13,%xmm5 @@ -1258,17 +1271,17 @@ L$ctr32_tail: movdqu %xmm5,48(%rsi) movdqu %xmm6,64(%rsi) cmpq $6,%rdx - jb L$ctr32_done + jb L$ctr32_done // %rdx was 5, stop store movups 80(%rdi),%xmm11 xorps %xmm11,%xmm7 movups %xmm7,80(%rsi) - je L$ctr32_done + je L$ctr32_done // %rdx was 6, stop store movups 96(%rdi),%xmm12 xorps %xmm12,%xmm8 movups %xmm8,96(%rsi) - jmp L$ctr32_done + jmp L$ctr32_done // %rdx was 7, stop store .p2align 5 L$ctr32_loop4: @@ -1282,7 +1295,7 @@ L$ctr32_loop4: jnz L$ctr32_loop4 aesenclast %xmm1,%xmm2 aesenclast %xmm1,%xmm3 - movups (%rdi),%xmm10 + movups (%rdi),%xmm10 // load input movups 16(%rdi),%xmm11 aesenclast %xmm1,%xmm4 aesenclast %xmm1,%xmm5 @@ -1290,14 +1303,14 @@ L$ctr32_loop4: movups 48(%rdi),%xmm13 xorps %xmm10,%xmm2 - movups %xmm2,(%rsi) + movups %xmm2,(%rsi) // store output xorps %xmm11,%xmm3 movups %xmm3,16(%rsi) pxor %xmm12,%xmm4 movdqu %xmm4,32(%rsi) pxor %xmm13,%xmm5 movdqu %xmm5,48(%rsi) - jmp L$ctr32_done + jmp L$ctr32_done // %rdx was 4, stop store .p2align 5 L$ctr32_loop3: @@ -1312,23 +1325,23 @@ L$ctr32_loop3: aesenclast %xmm1,%xmm3 aesenclast %xmm1,%xmm4 - movups (%rdi),%xmm10 + movups (%rdi),%xmm10 // load input xorps %xmm10,%xmm2 - movups %xmm2,(%rsi) + movups %xmm2,(%rsi) // store output cmpq $2,%rdx - jb L$ctr32_done + jb L$ctr32_done // %rdx was 1, stop store movups 16(%rdi),%xmm11 xorps %xmm11,%xmm3 movups %xmm3,16(%rsi) - je L$ctr32_done + je L$ctr32_done // %rdx was 2, stop store movups 32(%rdi),%xmm12 xorps %xmm12,%xmm4 - movups %xmm4,32(%rsi) + movups %xmm4,32(%rsi) // %rdx was 3, stop store L$ctr32_done: - xorps %xmm0,%xmm0 + xorps %xmm0,%xmm0 // clear register bank xorl %ebp,%ebp pxor %xmm1,%xmm1 pxor %xmm2,%xmm2 @@ -1337,7 +1350,7 @@ L$ctr32_done: pxor %xmm5,%xmm5 pxor %xmm6,%xmm6 pxor %xmm7,%xmm7 - movaps %xmm0,0(%rsp) + movaps %xmm0,0(%rsp) // clear stack pxor %xmm8,%xmm8 movaps %xmm0,16(%rsp) pxor %xmm9,%xmm9 @@ -1365,18 +1378,19 @@ L$ctr32_epilogue: .private_extern _aes_hw_cbc_encrypt .p2align 4 +.alt_entry _aes_hw_cbc_encrypt _aes_hw_cbc_encrypt: _CET_ENDBR - testq %rdx,%rdx + testq %rdx,%rdx // check length jz L$cbc_ret - movl 240(%rcx),%r10d - movq %rcx,%r11 - testl %r9d,%r9d + movl 240(%rcx),%r10d // key->rounds + movq %rcx,%r11 // backup %rcx + testl %r9d,%r9d // 6th argument jz L$cbc_decrypt - - movups (%r8),%xmm2 +// --------------------------- CBC ENCRYPT ------------------------------ + movups (%r8),%xmm2 // load iv as initial state movl %r10d,%eax cmpq $16,%rdx jb L$cbc_enc_tail @@ -1384,9 +1398,9 @@ _CET_ENDBR jmp L$cbc_enc_loop .p2align 4 L$cbc_enc_loop: - movups (%rdi),%xmm3 + movups (%rdi),%xmm3 // load input leaq 16(%rdi),%rdi - +// xorps %xmm3,%xmm2 movups (%rcx),%xmm0 movups 16(%rcx),%xmm1 xorps %xmm0,%xmm3 @@ -1397,17 +1411,17 @@ L$oop_enc1_6: decl %eax movups (%rcx),%xmm1 leaq 16(%rcx),%rcx - jnz L$oop_enc1_6 + jnz L$oop_enc1_6 // loop body is 16 bytes aesenclast %xmm1,%xmm2 - movl %r10d,%eax - movq %r11,%rcx - movups %xmm2,0(%rsi) + movl %r10d,%eax // restore %eax + movq %r11,%rcx // restore %rcx + movups %xmm2,0(%rsi) // store output leaq 16(%rsi),%rsi subq $16,%rdx jnc L$cbc_enc_loop addq $16,%rdx jnz L$cbc_enc_tail - pxor %xmm0,%xmm0 + pxor %xmm0,%xmm0 // clear register bank pxor %xmm1,%xmm1 movups %xmm2,(%r8) pxor %xmm2,%xmm2 @@ -1415,30 +1429,30 @@ L$oop_enc1_6: jmp L$cbc_ret L$cbc_enc_tail: - movq %rdx,%rcx - xchgq %rdi,%rsi -.long 0x9066A4F3 - movl $16,%ecx + movq %rdx,%rcx // zaps %rcx + xchgq %rdi,%rsi // %rdi is %rsi and %rsi is %rdi now +.long 0x9066A4F3 // rep movsb + movl $16,%ecx // zero tail subq %rdx,%rcx xorl %eax,%eax -.long 0x9066AAF3 - leaq -16(%rdi),%rdi - movl %r10d,%eax - movq %rdi,%rsi - movq %r11,%rcx - xorq %rdx,%rdx - jmp L$cbc_enc_loop - +.long 0x9066AAF3 // rep stosb + leaq -16(%rdi),%rdi // rewind %rsi by 1 block + movl %r10d,%eax // restore %eax + movq %rdi,%rsi // %rdi and %rsi are the same + movq %r11,%rcx // restore %rcx + xorq %rdx,%rdx // len=16 + jmp L$cbc_enc_loop // one more spin +// --------------------------- CBC DECRYPT ------------------------------# .p2align 4 L$cbc_decrypt: cmpq $16,%rdx jne L$cbc_decrypt_bulk - - - movdqu (%rdi),%xmm2 - movdqu (%r8),%xmm3 - movdqa %xmm2,%xmm4 +// handle single block without allocating stack frame, +// useful in ciphertext stealing mode + movdqu (%rdi),%xmm2 // load input + movdqu (%r8),%xmm3 // load iv + movdqa %xmm2,%xmm4 // future iv movups (%rcx),%xmm0 movups 16(%rcx),%xmm1 leaq 32(%rcx),%rcx @@ -1448,32 +1462,32 @@ L$oop_dec1_7: decl %r10d movups (%rcx),%xmm1 leaq 16(%rcx),%rcx - jnz L$oop_dec1_7 + jnz L$oop_dec1_7 // loop body is 16 bytes aesdeclast %xmm1,%xmm2 - pxor %xmm0,%xmm0 + pxor %xmm0,%xmm0 // clear register bank pxor %xmm1,%xmm1 - movdqu %xmm4,(%r8) - xorps %xmm3,%xmm2 + movdqu %xmm4,(%r8) // store iv + xorps %xmm3,%xmm2 // ^=iv pxor %xmm3,%xmm3 - movups %xmm2,(%rsi) + movups %xmm2,(%rsi) // store output pxor %xmm2,%xmm2 jmp L$cbc_ret .p2align 4 L$cbc_decrypt_bulk: - leaq (%rsp),%r11 + leaq (%rsp),%r11 // frame pointer pushq %rbp subq $16,%rsp - andq $-16,%rsp - movq %rcx,%rbp + andq $-16,%rsp // Linux kernel stack can be incorrectly seeded + movq %rcx,%rbp // [re-]backup %rcx [after reassignment] movups (%r8),%xmm10 movl %r10d,%eax cmpq $0x50,%rdx jbe L$cbc_dec_tail movups (%rcx),%xmm0 - movdqu 0(%rdi),%xmm2 + movdqu 0(%rdi),%xmm2 // load input movdqu 16(%rdi),%xmm3 movdqa %xmm2,%xmm11 movdqu 32(%rdi),%xmm4 @@ -1487,8 +1501,8 @@ L$cbc_decrypt_bulk: cmpq $0x70,%rdx jbe L$cbc_dec_six_or_seven - subq $0x70,%rdx - leaq 112(%rcx),%rcx + subq $0x70,%rdx // %rdx is biased by -7*16 + leaq 112(%rcx),%rcx // size optimization jmp L$cbc_dec_loop8_enter .p2align 4 L$cbc_dec_loop8: @@ -1502,7 +1516,7 @@ L$cbc_dec_loop8_enter: movups 16-112(%rcx),%xmm1 pxor %xmm0,%xmm4 movq $-1,%rbp - cmpq $0x70,%rdx + cmpq $0x70,%rdx // is there at least 0x60 bytes ahead? pxor %xmm0,%xmm5 pxor %xmm0,%xmm6 pxor %xmm0,%xmm7 @@ -1651,11 +1665,11 @@ L$cbc_dec_done: movdqu 80(%rdi),%xmm1 aesdeclast %xmm10,%xmm2 - movdqu 96(%rdi),%xmm10 + movdqu 96(%rdi),%xmm10 // borrow %xmm10 pxor %xmm0,%xmm1 aesdeclast %xmm11,%xmm3 pxor %xmm0,%xmm10 - movdqu 112(%rdi),%xmm0 + movdqu 112(%rdi),%xmm0 // next IV aesdeclast %xmm12,%xmm4 leaq 128(%rdi),%rdi movdqu 0(%rbp),%xmm11 @@ -1668,11 +1682,11 @@ L$cbc_dec_done: movdqu 48(%rbp),%xmm14 movdqu 64(%rbp),%xmm15 aesdeclast %xmm10,%xmm9 - movdqa %xmm0,%xmm10 + movdqa %xmm0,%xmm10 // return %xmm10 movdqu 80(%rbp),%xmm1 movups -112(%rcx),%xmm0 - movups %xmm2,(%rsi) + movups %xmm2,(%rsi) // store output movdqa %xmm11,%xmm2 movups %xmm3,16(%rsi) movdqa %xmm12,%xmm3 @@ -1706,13 +1720,13 @@ L$cbc_dec_six_or_seven: movaps %xmm7,%xmm8 call _aesni_decrypt6 - pxor %xmm10,%xmm2 + pxor %xmm10,%xmm2 // ^= IV movaps %xmm8,%xmm10 pxor %xmm11,%xmm3 movdqu %xmm2,(%rsi) pxor %xmm12,%xmm4 movdqu %xmm3,16(%rsi) - pxor %xmm3,%xmm3 + pxor %xmm3,%xmm3 // clear register bank pxor %xmm13,%xmm5 movdqu %xmm4,32(%rsi) pxor %xmm4,%xmm4 @@ -1733,13 +1747,13 @@ L$cbc_dec_seven: xorps %xmm9,%xmm9 call _aesni_decrypt8 movups 80(%rdi),%xmm9 - pxor %xmm10,%xmm2 + pxor %xmm10,%xmm2 // ^= IV movups 96(%rdi),%xmm10 pxor %xmm11,%xmm3 movdqu %xmm2,(%rsi) pxor %xmm12,%xmm4 movdqu %xmm3,16(%rsi) - pxor %xmm3,%xmm3 + pxor %xmm3,%xmm3 // clear register bank pxor %xmm13,%xmm5 movdqu %xmm4,32(%rsi) pxor %xmm4,%xmm4 @@ -1761,24 +1775,24 @@ L$cbc_dec_seven: L$cbc_dec_tail: movups (%rdi),%xmm2 subq $0x10,%rdx - jbe L$cbc_dec_one + jbe L$cbc_dec_one // %rdx is 1*16 or less movups 16(%rdi),%xmm3 movaps %xmm2,%xmm11 subq $0x10,%rdx - jbe L$cbc_dec_two + jbe L$cbc_dec_two // %rdx is 2*16 or less movups 32(%rdi),%xmm4 movaps %xmm3,%xmm12 subq $0x10,%rdx - jbe L$cbc_dec_three + jbe L$cbc_dec_three // %rdx is 3*16 or less movups 48(%rdi),%xmm5 movaps %xmm4,%xmm13 subq $0x10,%rdx - jbe L$cbc_dec_four + jbe L$cbc_dec_four // %rdx is 4*16 or less - movups 64(%rdi),%xmm6 + movups 64(%rdi),%xmm6 // %rdx is 5*16 or less movaps %xmm5,%xmm14 movaps %xmm6,%xmm15 xorps %xmm7,%xmm7 @@ -1789,7 +1803,7 @@ L$cbc_dec_tail: movdqu %xmm2,(%rsi) pxor %xmm12,%xmm4 movdqu %xmm3,16(%rsi) - pxor %xmm3,%xmm3 + pxor %xmm3,%xmm3 // clear register bank pxor %xmm13,%xmm5 movdqu %xmm4,32(%rsi) pxor %xmm4,%xmm4 @@ -1815,7 +1829,7 @@ L$oop_dec1_8: decl %eax movups (%rcx),%xmm1 leaq 16(%rcx),%rcx - jnz L$oop_dec1_8 + jnz L$oop_dec1_8 // loop body is 16 bytes aesdeclast %xmm1,%xmm2 xorps %xmm10,%xmm2 movaps %xmm11,%xmm10 @@ -1829,7 +1843,7 @@ L$cbc_dec_two: pxor %xmm11,%xmm3 movdqu %xmm2,(%rsi) movdqa %xmm3,%xmm2 - pxor %xmm3,%xmm3 + pxor %xmm3,%xmm3 // clear register bank leaq 16(%rsi),%rsi jmp L$cbc_dec_tail_collected .p2align 4 @@ -1842,7 +1856,7 @@ L$cbc_dec_three: movdqu %xmm2,(%rsi) pxor %xmm12,%xmm4 movdqu %xmm3,16(%rsi) - pxor %xmm3,%xmm3 + pxor %xmm3,%xmm3 // clear register bank movdqa %xmm4,%xmm2 pxor %xmm4,%xmm4 leaq 32(%rsi),%rsi @@ -1857,7 +1871,7 @@ L$cbc_dec_four: movdqu %xmm2,(%rsi) pxor %xmm12,%xmm4 movdqu %xmm3,16(%rsi) - pxor %xmm3,%xmm3 + pxor %xmm3,%xmm3 // clear register bank pxor %xmm13,%xmm5 movdqu %xmm4,32(%rsi) pxor %xmm4,%xmm4 @@ -1868,10 +1882,10 @@ L$cbc_dec_four: .p2align 4 L$cbc_dec_clear_tail_collected: - pxor %xmm3,%xmm3 + pxor %xmm3,%xmm3 // clear register bank pxor %xmm4,%xmm4 pxor %xmm5,%xmm5 - pxor %xmm6,%xmm6 + pxor %xmm6,%xmm6 // %xmm6..9 pxor %xmm7,%xmm7 pxor %xmm8,%xmm8 pxor %xmm9,%xmm9 @@ -1890,11 +1904,11 @@ L$cbc_dec_tail_partial: movq %rsi,%rdi subq %rdx,%rcx leaq (%rsp),%rsi -.long 0x9066A4F3 +.long 0x9066A4F3 // rep movsb movdqa %xmm2,(%rsp) L$cbc_dec_ret: - xorps %xmm0,%xmm0 + xorps %xmm0,%xmm0 // %xmm0 pxor %xmm1,%xmm1 movq -8(%r11),%rbp @@ -1908,6 +1922,7 @@ L$cbc_ret: .private_extern _aes_hw_encrypt_key_to_decrypt_key .p2align 4 +.alt_entry _aes_hw_encrypt_key_to_decrypt_key _aes_hw_encrypt_key_to_decrypt_key: _CET_ENDBR @@ -1915,9 +1930,9 @@ _CET_ENDBR movl 240(%rdi),%esi shll $4,%esi - leaq 16(%rdi,%rsi,1),%rdx + leaq 16(%rdi,%rsi,1),%rdx // points at the end of key schedule - movups (%rdi),%xmm0 + movups (%rdi),%xmm0 // just swap movups (%rdx),%xmm1 movups %xmm0,(%rdx) movups %xmm1,(%rdi) @@ -1925,7 +1940,7 @@ _CET_ENDBR leaq -16(%rdx),%rdx L$dec_key_inverse: - movups (%rdi),%xmm0 + movups (%rdi),%xmm0 // swap and inverse movups (%rdx),%xmm1 aesimc %xmm0,%xmm0 aesimc %xmm1,%xmm1 @@ -1936,7 +1951,7 @@ L$dec_key_inverse: cmpq %rdi,%rdx ja L$dec_key_inverse - movups (%rdi),%xmm0 + movups (%rdi),%xmm0 // inverse middle aesimc %xmm0,%xmm0 pxor %xmm1,%xmm1 movups %xmm0,(%rdx) @@ -1948,6 +1963,7 @@ L$dec_key_inverse: .private_extern _aes_hw_set_encrypt_key_base .p2align 4 +.alt_entry _aes_hw_set_encrypt_key_base _aes_hw_set_encrypt_key_base: @@ -1959,9 +1975,9 @@ _CET_ENDBR - movups (%rdi),%xmm0 - xorps %xmm4,%xmm4 - leaq 16(%rdx),%rax + movups (%rdi),%xmm0 // pull first 128 bits of *userKey + xorps %xmm4,%xmm4 // low dword of xmm4 is assumed 0 + leaq 16(%rdx),%rax // %rax is used as modifiable copy of %rdx cmpl $256,%esi je L$14rounds cmpl $192,%esi @@ -1970,97 +1986,97 @@ _CET_ENDBR jne L$bad_keybits L$10rounds: - movl $9,%esi + movl $9,%esi // 10 rounds for 128-bit key - movups %xmm0,(%rdx) - aeskeygenassist $0x1,%xmm0,%xmm1 + movups %xmm0,(%rdx) // round 0 + aeskeygenassist $0x1,%xmm0,%xmm1 // round 1 call L$key_expansion_128_cold - aeskeygenassist $0x2,%xmm0,%xmm1 + aeskeygenassist $0x2,%xmm0,%xmm1 // round 2 call L$key_expansion_128 - aeskeygenassist $0x4,%xmm0,%xmm1 + aeskeygenassist $0x4,%xmm0,%xmm1 // round 3 call L$key_expansion_128 - aeskeygenassist $0x8,%xmm0,%xmm1 + aeskeygenassist $0x8,%xmm0,%xmm1 // round 4 call L$key_expansion_128 - aeskeygenassist $0x10,%xmm0,%xmm1 + aeskeygenassist $0x10,%xmm0,%xmm1 // round 5 call L$key_expansion_128 - aeskeygenassist $0x20,%xmm0,%xmm1 + aeskeygenassist $0x20,%xmm0,%xmm1 // round 6 call L$key_expansion_128 - aeskeygenassist $0x40,%xmm0,%xmm1 + aeskeygenassist $0x40,%xmm0,%xmm1 // round 7 call L$key_expansion_128 - aeskeygenassist $0x80,%xmm0,%xmm1 + aeskeygenassist $0x80,%xmm0,%xmm1 // round 8 call L$key_expansion_128 - aeskeygenassist $0x1b,%xmm0,%xmm1 + aeskeygenassist $0x1b,%xmm0,%xmm1 // round 9 call L$key_expansion_128 - aeskeygenassist $0x36,%xmm0,%xmm1 + aeskeygenassist $0x36,%xmm0,%xmm1 // round 10 call L$key_expansion_128 movups %xmm0,(%rax) - movl %esi,80(%rax) + movl %esi,80(%rax) // 240(%rdx) xorl %eax,%eax jmp L$enc_key_ret .p2align 4 L$12rounds: - movq 16(%rdi),%xmm2 - movl $11,%esi + movq 16(%rdi),%xmm2 // remaining 1/3 of *userKey + movl $11,%esi // 12 rounds for 192 - movups %xmm0,(%rdx) - aeskeygenassist $0x1,%xmm2,%xmm1 + movups %xmm0,(%rdx) // round 0 + aeskeygenassist $0x1,%xmm2,%xmm1 // round 1,2 call L$key_expansion_192a_cold - aeskeygenassist $0x2,%xmm2,%xmm1 + aeskeygenassist $0x2,%xmm2,%xmm1 // round 2,3 call L$key_expansion_192b - aeskeygenassist $0x4,%xmm2,%xmm1 + aeskeygenassist $0x4,%xmm2,%xmm1 // round 4,5 call L$key_expansion_192a - aeskeygenassist $0x8,%xmm2,%xmm1 + aeskeygenassist $0x8,%xmm2,%xmm1 // round 5,6 call L$key_expansion_192b - aeskeygenassist $0x10,%xmm2,%xmm1 + aeskeygenassist $0x10,%xmm2,%xmm1 // round 7,8 call L$key_expansion_192a - aeskeygenassist $0x20,%xmm2,%xmm1 + aeskeygenassist $0x20,%xmm2,%xmm1 // round 8,9 call L$key_expansion_192b - aeskeygenassist $0x40,%xmm2,%xmm1 + aeskeygenassist $0x40,%xmm2,%xmm1 // round 10,11 call L$key_expansion_192a - aeskeygenassist $0x80,%xmm2,%xmm1 + aeskeygenassist $0x80,%xmm2,%xmm1 // round 11,12 call L$key_expansion_192b movups %xmm0,(%rax) - movl %esi,48(%rax) + movl %esi,48(%rax) // 240(%rdx) xorq %rax,%rax jmp L$enc_key_ret .p2align 4 L$14rounds: - movups 16(%rdi),%xmm2 - movl $13,%esi + movups 16(%rdi),%xmm2 // remaining half of *userKey + movl $13,%esi // 14 rounds for 256 leaq 16(%rax),%rax - movups %xmm0,(%rdx) - movups %xmm2,16(%rdx) - aeskeygenassist $0x1,%xmm2,%xmm1 + movups %xmm0,(%rdx) // round 0 + movups %xmm2,16(%rdx) // round 1 + aeskeygenassist $0x1,%xmm2,%xmm1 // round 2 call L$key_expansion_256a_cold - aeskeygenassist $0x1,%xmm0,%xmm1 + aeskeygenassist $0x1,%xmm0,%xmm1 // round 3 call L$key_expansion_256b - aeskeygenassist $0x2,%xmm2,%xmm1 + aeskeygenassist $0x2,%xmm2,%xmm1 // round 4 call L$key_expansion_256a - aeskeygenassist $0x2,%xmm0,%xmm1 + aeskeygenassist $0x2,%xmm0,%xmm1 // round 5 call L$key_expansion_256b - aeskeygenassist $0x4,%xmm2,%xmm1 + aeskeygenassist $0x4,%xmm2,%xmm1 // round 6 call L$key_expansion_256a - aeskeygenassist $0x4,%xmm0,%xmm1 + aeskeygenassist $0x4,%xmm0,%xmm1 // round 7 call L$key_expansion_256b - aeskeygenassist $0x8,%xmm2,%xmm1 + aeskeygenassist $0x8,%xmm2,%xmm1 // round 8 call L$key_expansion_256a - aeskeygenassist $0x8,%xmm0,%xmm1 + aeskeygenassist $0x8,%xmm0,%xmm1 // round 9 call L$key_expansion_256b - aeskeygenassist $0x10,%xmm2,%xmm1 + aeskeygenassist $0x10,%xmm2,%xmm1 // round 10 call L$key_expansion_256a - aeskeygenassist $0x10,%xmm0,%xmm1 + aeskeygenassist $0x10,%xmm0,%xmm1 // round 11 call L$key_expansion_256b - aeskeygenassist $0x20,%xmm2,%xmm1 + aeskeygenassist $0x20,%xmm2,%xmm1 // round 12 call L$key_expansion_256a - aeskeygenassist $0x20,%xmm0,%xmm1 + aeskeygenassist $0x20,%xmm0,%xmm1 // round 13 call L$key_expansion_256b - aeskeygenassist $0x40,%xmm2,%xmm1 + aeskeygenassist $0x40,%xmm2,%xmm1 // round 14 call L$key_expansion_256a movups %xmm0,(%rax) - movl %esi,16(%rax) + movl %esi,16(%rax) // 240(%rdx) xorq %rax,%rax jmp L$enc_key_ret @@ -2080,6 +2096,7 @@ L$enc_key_ret: + .p2align 4 L$key_expansion_128: @@ -2090,7 +2107,7 @@ L$key_expansion_128_cold: xorps %xmm4,%xmm0 shufps $140,%xmm0,%xmm4 xorps %xmm4,%xmm0 - shufps $255,%xmm1,%xmm1 + shufps $255,%xmm1,%xmm1 // critical path xorps %xmm1,%xmm0 ret @@ -2109,7 +2126,7 @@ L$key_expansion_192b_warm: shufps $140,%xmm0,%xmm4 pslldq $4,%xmm3 xorps %xmm4,%xmm0 - pshufd $85,%xmm1,%xmm1 + pshufd $85,%xmm1,%xmm1 // critical path pxor %xmm3,%xmm2 pxor %xmm1,%xmm0 pshufd $255,%xmm0,%xmm3 @@ -2139,7 +2156,7 @@ L$key_expansion_256a_cold: xorps %xmm4,%xmm0 shufps $140,%xmm0,%xmm4 xorps %xmm4,%xmm0 - shufps $255,%xmm1,%xmm1 + shufps $255,%xmm1,%xmm1 // critical path xorps %xmm1,%xmm0 ret @@ -2154,16 +2171,16 @@ L$key_expansion_256b: xorps %xmm4,%xmm2 shufps $140,%xmm2,%xmm4 xorps %xmm4,%xmm2 - shufps $170,%xmm1,%xmm1 + shufps $170,%xmm1,%xmm1 // critical path xorps %xmm1,%xmm2 ret - .globl _aes_hw_set_encrypt_key_alt .private_extern _aes_hw_set_encrypt_key_alt .p2align 4 +.alt_entry _aes_hw_set_encrypt_key_alt _aes_hw_set_encrypt_key_alt: @@ -2175,9 +2192,9 @@ _CET_ENDBR - movups (%rdi),%xmm0 - xorps %xmm4,%xmm4 - leaq 16(%rdx),%rax + movups (%rdi),%xmm0 // pull first 128 bits of *userKey + xorps %xmm4,%xmm4 // low dword of xmm4 is assumed 0 + leaq 16(%rdx),%rax // %rax is used as modifiable copy of %rdx cmpl $256,%esi je L$14rounds_alt cmpl $192,%esi @@ -2185,7 +2202,7 @@ _CET_ENDBR cmpl $128,%esi jne L$bad_keybits_alt - movl $9,%esi + movl $9,%esi // 10 rounds for 128-bit key movdqa L$key_rotate(%rip),%xmm5 movl $8,%r10d movdqa L$key_rcon1(%rip),%xmm4 @@ -2247,14 +2264,14 @@ L$oop_key128: pxor %xmm2,%xmm0 movdqu %xmm0,16(%rax) - movl %esi,96(%rax) + movl %esi,96(%rax) // 240(%rdx) xorl %eax,%eax jmp L$enc_key_ret_alt .p2align 4 L$12rounds_alt: - movq 16(%rdi),%xmm2 - movl $11,%esi + movq 16(%rdi),%xmm2 // remaining 1/3 of *userKey + movl $11,%esi // 12 rounds for 192 movdqa L$key_rotate192(%rip),%xmm5 movdqa L$key_rcon1(%rip),%xmm4 movl $8,%r10d @@ -2290,14 +2307,14 @@ L$oop_key192: decl %r10d jnz L$oop_key192 - movl %esi,32(%rax) + movl %esi,32(%rax) // 240(%rdx) xorl %eax,%eax jmp L$enc_key_ret_alt .p2align 4 L$14rounds_alt: - movups 16(%rdi),%xmm2 - movl $13,%esi + movups 16(%rdi),%xmm2 // remaining half of *userKey + movl $13,%esi // 14 rounds for 256 leaq 16(%rax),%rax movdqa L$key_rotate(%rip),%xmm5 movdqa L$key_rcon1(%rip),%xmm4 @@ -2347,7 +2364,7 @@ L$oop_key256: jmp L$oop_key256 L$done_key256: - movl %esi,16(%rax) + movl %esi,16(%rax) // 240(%rdx) xorl %eax,%eax jmp L$enc_key_ret_alt @@ -2368,7 +2385,9 @@ L$enc_key_ret_alt: .section __DATA,__const + .p2align 6 +aesni_constants: L$bswap_mask: .byte 15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0 L$increment32: diff --git a/third_party/boringssl/gen/bcm/aesni-x86_64-linux.S b/third_party/boringssl/gen/bcm/aesni-x86_64-linux.S index 4bce582ca..19996a7f1 100644 --- a/third_party/boringssl/gen/bcm/aesni-x86_64-linux.S +++ b/third_party/boringssl/gen/bcm/aesni-x86_64-linux.S @@ -17,8 +17,8 @@ _CET_ENDBR .hidden BORINGSSL_function_hit movb $1,BORINGSSL_function_hit+1(%rip) #endif - movups (%rdi),%xmm2 - movl 240(%rdx),%eax + movups (%rdi),%xmm2 // load input + movl 240(%rdx),%eax // key->rounds movups (%rdx),%xmm0 movups 16(%rdx),%xmm1 leaq 32(%rdx),%rdx @@ -28,11 +28,11 @@ _CET_ENDBR decl %eax movups (%rdx),%xmm1 leaq 16(%rdx),%rdx - jnz .Loop_enc1_1 + jnz .Loop_enc1_1 // loop body is 16 bytes aesenclast %xmm1,%xmm2 - pxor %xmm0,%xmm0 + pxor %xmm0,%xmm0 // clear register bank pxor %xmm1,%xmm1 - movups %xmm2,(%rsi) + movups %xmm2,(%rsi) // output pxor %xmm2,%xmm2 ret .cfi_endproc @@ -45,8 +45,8 @@ _CET_ENDBR aes_hw_decrypt: .cfi_startproc _CET_ENDBR - movups (%rdi),%xmm2 - movl 240(%rdx),%eax + movups (%rdi),%xmm2 // load input + movl 240(%rdx),%eax // key->rounds movups (%rdx),%xmm0 movups 16(%rdx),%xmm1 leaq 32(%rdx),%rdx @@ -56,11 +56,11 @@ _CET_ENDBR decl %eax movups (%rdx),%xmm1 leaq 16(%rdx),%rdx - jnz .Loop_dec1_2 + jnz .Loop_dec1_2 // loop body is 16 bytes aesdeclast %xmm1,%xmm2 - pxor %xmm0,%xmm0 + pxor %xmm0,%xmm0 // clear register bank pxor %xmm1,%xmm1 - movups %xmm2,(%rsi) + movups %xmm2,(%rsi) // output pxor %xmm2,%xmm2 ret .cfi_endproc @@ -76,7 +76,7 @@ _aesni_encrypt2: xorps %xmm0,%xmm3 movups 32(%rcx),%xmm0 leaq 32(%rcx,%rax,1),%rcx - negq %rax + negq %rax // %eax addq $16,%rax .Lenc_loop2: @@ -107,7 +107,7 @@ _aesni_decrypt2: xorps %xmm0,%xmm3 movups 32(%rcx),%xmm0 leaq 32(%rcx,%rax,1),%rcx - negq %rax + negq %rax // %eax addq $16,%rax .Ldec_loop2: @@ -139,7 +139,7 @@ _aesni_encrypt3: xorps %xmm0,%xmm4 movups 32(%rcx),%xmm0 leaq 32(%rcx,%rax,1),%rcx - negq %rax + negq %rax // %eax addq $16,%rax .Lenc_loop3: @@ -175,7 +175,7 @@ _aesni_decrypt3: xorps %xmm0,%xmm4 movups 32(%rcx),%xmm0 leaq 32(%rcx,%rax,1),%rcx - negq %rax + negq %rax // %eax addq $16,%rax .Ldec_loop3: @@ -212,7 +212,7 @@ _aesni_encrypt4: xorps %xmm0,%xmm5 movups 32(%rcx),%xmm0 leaq 32(%rcx,%rax,1),%rcx - negq %rax + negq %rax // %eax .byte 0x0f,0x1f,0x00 addq $16,%rax @@ -254,7 +254,7 @@ _aesni_decrypt4: xorps %xmm0,%xmm5 movups 32(%rcx),%xmm0 leaq 32(%rcx,%rax,1),%rcx - negq %rax + negq %rax // %eax .byte 0x0f,0x1f,0x00 addq $16,%rax @@ -295,7 +295,7 @@ _aesni_encrypt6: pxor %xmm0,%xmm4 aesenc %xmm1,%xmm2 leaq 32(%rcx,%rax,1),%rcx - negq %rax + negq %rax // %eax aesenc %xmm1,%xmm3 pxor %xmm0,%xmm5 pxor %xmm0,%xmm6 @@ -351,7 +351,7 @@ _aesni_decrypt6: pxor %xmm0,%xmm4 aesdec %xmm1,%xmm2 leaq 32(%rcx,%rax,1),%rcx - negq %rax + negq %rax // %eax aesdec %xmm1,%xmm3 pxor %xmm0,%xmm5 pxor %xmm0,%xmm6 @@ -408,7 +408,7 @@ _aesni_encrypt8: pxor %xmm0,%xmm5 pxor %xmm0,%xmm6 leaq 32(%rcx,%rax,1),%rcx - negq %rax + negq %rax // %eax aesenc %xmm1,%xmm2 pxor %xmm0,%xmm7 pxor %xmm0,%xmm8 @@ -474,7 +474,7 @@ _aesni_decrypt8: pxor %xmm0,%xmm5 pxor %xmm0,%xmm6 leaq 32(%rcx,%rax,1),%rcx - negq %rax + negq %rax // %eax aesdec %xmm1,%xmm2 pxor %xmm0,%xmm7 pxor %xmm0,%xmm8 @@ -534,20 +534,20 @@ _aesni_decrypt8: aes_hw_ecb_encrypt: .cfi_startproc _CET_ENDBR - andq $-16,%rdx - jz .Lecb_ret + andq $-16,%rdx // if (%rdx<16) + jz .Lecb_ret // return - movl 240(%rcx),%eax + movl 240(%rcx),%eax // key->rounds movups (%rcx),%xmm0 - movq %rcx,%r11 - movl %eax,%r10d - testl %r8d,%r8d + movq %rcx,%r11 // backup %rcx + movl %eax,%r10d // backup %eax + testl %r8d,%r8d // 5th argument jz .Lecb_decrypt +// --------------------------- ECB ENCRYPT ------------------------------ + cmpq $0x80,%rdx // if (%rdx<8*16) + jb .Lecb_enc_tail // short input - cmpq $0x80,%rdx - jb .Lecb_enc_tail - - movdqu (%rdi),%xmm2 + movdqu (%rdi),%xmm2 // load 8 input blocks movdqu 16(%rdi),%xmm3 movdqu 32(%rdi),%xmm4 movdqu 48(%rdi),%xmm5 @@ -555,15 +555,15 @@ _CET_ENDBR movdqu 80(%rdi),%xmm7 movdqu 96(%rdi),%xmm8 movdqu 112(%rdi),%xmm9 - leaq 128(%rdi),%rdi - subq $0x80,%rdx + leaq 128(%rdi),%rdi // %rdi+=8*16 + subq $0x80,%rdx // %rdx-=8*16 (can be zero) jmp .Lecb_enc_loop8_enter .align 16 .Lecb_enc_loop8: - movups %xmm2,(%rsi) - movq %r11,%rcx - movdqu (%rdi),%xmm2 - movl %r10d,%eax + movups %xmm2,(%rsi) // store 8 output blocks + movq %r11,%rcx // restore %rcx + movdqu (%rdi),%xmm2 // load 8 input blocks + movl %r10d,%eax // restore %eax movups %xmm3,16(%rsi) movdqu 16(%rdi),%xmm3 movups %xmm4,32(%rsi) @@ -577,31 +577,31 @@ _CET_ENDBR movups %xmm8,96(%rsi) movdqu 96(%rdi),%xmm8 movups %xmm9,112(%rsi) - leaq 128(%rsi),%rsi + leaq 128(%rsi),%rsi // %rsi+=8*16 movdqu 112(%rdi),%xmm9 - leaq 128(%rdi),%rdi + leaq 128(%rdi),%rdi // %rdi+=8*16 .Lecb_enc_loop8_enter: call _aesni_encrypt8 subq $0x80,%rdx - jnc .Lecb_enc_loop8 + jnc .Lecb_enc_loop8 // loop if %rdx-=8*16 didn't borrow - movups %xmm2,(%rsi) - movq %r11,%rcx + movups %xmm2,(%rsi) // store 8 output blocks + movq %r11,%rcx // restore %rcx movups %xmm3,16(%rsi) - movl %r10d,%eax + movl %r10d,%eax // restore %eax movups %xmm4,32(%rsi) movups %xmm5,48(%rsi) movups %xmm6,64(%rsi) movups %xmm7,80(%rsi) movups %xmm8,96(%rsi) movups %xmm9,112(%rsi) - leaq 128(%rsi),%rsi - addq $0x80,%rdx - jz .Lecb_ret + leaq 128(%rsi),%rsi // %rsi+=8*16 + addq $0x80,%rdx // restore real remaining %rdx + jz .Lecb_ret // done if (%rdx==0) -.Lecb_enc_tail: +.Lecb_enc_tail: // %rdx is less than 8*16 movups (%rdi),%xmm2 cmpq $0x20,%rdx jb .Lecb_enc_one @@ -620,7 +620,7 @@ _CET_ENDBR movdqu 96(%rdi),%xmm8 xorps %xmm9,%xmm9 call _aesni_encrypt8 - movups %xmm2,(%rsi) + movups %xmm2,(%rsi) // store 7 output blocks movups %xmm3,16(%rsi) movups %xmm4,32(%rsi) movups %xmm5,48(%rsi) @@ -639,27 +639,27 @@ _CET_ENDBR decl %eax movups (%rcx),%xmm1 leaq 16(%rcx),%rcx - jnz .Loop_enc1_3 + jnz .Loop_enc1_3 // loop body is 16 bytes aesenclast %xmm1,%xmm2 - movups %xmm2,(%rsi) + movups %xmm2,(%rsi) // store one output block jmp .Lecb_ret .align 16 .Lecb_enc_two: call _aesni_encrypt2 - movups %xmm2,(%rsi) + movups %xmm2,(%rsi) // store 2 output blocks movups %xmm3,16(%rsi) jmp .Lecb_ret .align 16 .Lecb_enc_three: call _aesni_encrypt3 - movups %xmm2,(%rsi) + movups %xmm2,(%rsi) // store 3 output blocks movups %xmm3,16(%rsi) movups %xmm4,32(%rsi) jmp .Lecb_ret .align 16 .Lecb_enc_four: call _aesni_encrypt4 - movups %xmm2,(%rsi) + movups %xmm2,(%rsi) // store 4 output blocks movups %xmm3,16(%rsi) movups %xmm4,32(%rsi) movups %xmm5,48(%rsi) @@ -668,7 +668,7 @@ _CET_ENDBR .Lecb_enc_five: xorps %xmm7,%xmm7 call _aesni_encrypt6 - movups %xmm2,(%rsi) + movups %xmm2,(%rsi) // store 5 output blocks movups %xmm3,16(%rsi) movups %xmm4,32(%rsi) movups %xmm5,48(%rsi) @@ -677,20 +677,20 @@ _CET_ENDBR .align 16 .Lecb_enc_six: call _aesni_encrypt6 - movups %xmm2,(%rsi) + movups %xmm2,(%rsi) // store 6 output blocks movups %xmm3,16(%rsi) movups %xmm4,32(%rsi) movups %xmm5,48(%rsi) movups %xmm6,64(%rsi) movups %xmm7,80(%rsi) jmp .Lecb_ret - +// --------------------------- ECB DECRYPT ------------------------------# .align 16 .Lecb_decrypt: - cmpq $0x80,%rdx - jb .Lecb_dec_tail + cmpq $0x80,%rdx // if (%rdx<8*16) + jb .Lecb_dec_tail // short input - movdqu (%rdi),%xmm2 + movdqu (%rdi),%xmm2 // load 8 input blocks movdqu 16(%rdi),%xmm3 movdqu 32(%rdi),%xmm4 movdqu 48(%rdi),%xmm5 @@ -698,15 +698,15 @@ _CET_ENDBR movdqu 80(%rdi),%xmm7 movdqu 96(%rdi),%xmm8 movdqu 112(%rdi),%xmm9 - leaq 128(%rdi),%rdi - subq $0x80,%rdx + leaq 128(%rdi),%rdi // %rdi+=8*16 + subq $0x80,%rdx // %rdx-=8*16 (can be zero) jmp .Lecb_dec_loop8_enter .align 16 .Lecb_dec_loop8: - movups %xmm2,(%rsi) - movq %r11,%rcx - movdqu (%rdi),%xmm2 - movl %r10d,%eax + movups %xmm2,(%rsi) // store 8 output blocks + movq %r11,%rcx // restore %rcx + movdqu (%rdi),%xmm2 // load 8 input blocks + movl %r10d,%eax // restore %eax movups %xmm3,16(%rsi) movdqu 16(%rdi),%xmm3 movups %xmm4,32(%rsi) @@ -720,23 +720,23 @@ _CET_ENDBR movups %xmm8,96(%rsi) movdqu 96(%rdi),%xmm8 movups %xmm9,112(%rsi) - leaq 128(%rsi),%rsi + leaq 128(%rsi),%rsi // %rsi+=8*16 movdqu 112(%rdi),%xmm9 - leaq 128(%rdi),%rdi + leaq 128(%rdi),%rdi // %rdi+=8*16 .Lecb_dec_loop8_enter: call _aesni_decrypt8 movups (%r11),%xmm0 subq $0x80,%rdx - jnc .Lecb_dec_loop8 + jnc .Lecb_dec_loop8 // loop if %rdx-=8*16 didn't borrow - movups %xmm2,(%rsi) - pxor %xmm2,%xmm2 - movq %r11,%rcx + movups %xmm2,(%rsi) // store 8 output blocks + pxor %xmm2,%xmm2 // clear register bank + movq %r11,%rcx // restore %rcx movups %xmm3,16(%rsi) pxor %xmm3,%xmm3 - movl %r10d,%eax + movl %r10d,%eax // restore %eax movups %xmm4,32(%rsi) pxor %xmm4,%xmm4 movups %xmm5,48(%rsi) @@ -749,9 +749,9 @@ _CET_ENDBR pxor %xmm8,%xmm8 movups %xmm9,112(%rsi) pxor %xmm9,%xmm9 - leaq 128(%rsi),%rsi - addq $0x80,%rdx - jz .Lecb_ret + leaq 128(%rsi),%rsi // %rsi+=8*16 + addq $0x80,%rdx // restore real remaining %rdx + jz .Lecb_ret // done if (%rdx==0) .Lecb_dec_tail: movups (%rdi),%xmm2 @@ -773,8 +773,8 @@ _CET_ENDBR movups (%rcx),%xmm0 xorps %xmm9,%xmm9 call _aesni_decrypt8 - movups %xmm2,(%rsi) - pxor %xmm2,%xmm2 + movups %xmm2,(%rsi) // store 7 output blocks + pxor %xmm2,%xmm2 // clear register bank movups %xmm3,16(%rsi) pxor %xmm3,%xmm3 movups %xmm4,32(%rsi) @@ -800,24 +800,24 @@ _CET_ENDBR decl %eax movups (%rcx),%xmm1 leaq 16(%rcx),%rcx - jnz .Loop_dec1_4 + jnz .Loop_dec1_4 // loop body is 16 bytes aesdeclast %xmm1,%xmm2 - movups %xmm2,(%rsi) - pxor %xmm2,%xmm2 + movups %xmm2,(%rsi) // store one output block + pxor %xmm2,%xmm2 // clear register bank jmp .Lecb_ret .align 16 .Lecb_dec_two: call _aesni_decrypt2 - movups %xmm2,(%rsi) - pxor %xmm2,%xmm2 + movups %xmm2,(%rsi) // store 2 output blocks + pxor %xmm2,%xmm2 // clear register bank movups %xmm3,16(%rsi) pxor %xmm3,%xmm3 jmp .Lecb_ret .align 16 .Lecb_dec_three: call _aesni_decrypt3 - movups %xmm2,(%rsi) - pxor %xmm2,%xmm2 + movups %xmm2,(%rsi) // store 3 output blocks + pxor %xmm2,%xmm2 // clear register bank movups %xmm3,16(%rsi) pxor %xmm3,%xmm3 movups %xmm4,32(%rsi) @@ -826,8 +826,8 @@ _CET_ENDBR .align 16 .Lecb_dec_four: call _aesni_decrypt4 - movups %xmm2,(%rsi) - pxor %xmm2,%xmm2 + movups %xmm2,(%rsi) // store 4 output blocks + pxor %xmm2,%xmm2 // clear register bank movups %xmm3,16(%rsi) pxor %xmm3,%xmm3 movups %xmm4,32(%rsi) @@ -839,8 +839,8 @@ _CET_ENDBR .Lecb_dec_five: xorps %xmm7,%xmm7 call _aesni_decrypt6 - movups %xmm2,(%rsi) - pxor %xmm2,%xmm2 + movups %xmm2,(%rsi) // store 5 output blocks + pxor %xmm2,%xmm2 // clear register bank movups %xmm3,16(%rsi) pxor %xmm3,%xmm3 movups %xmm4,32(%rsi) @@ -854,8 +854,8 @@ _CET_ENDBR .align 16 .Lecb_dec_six: call _aesni_decrypt6 - movups %xmm2,(%rsi) - pxor %xmm2,%xmm2 + movups %xmm2,(%rsi) // store 6 output blocks + pxor %xmm2,%xmm2 // clear register bank movups %xmm3,16(%rsi) pxor %xmm3,%xmm3 movups %xmm4,32(%rsi) @@ -868,7 +868,7 @@ _CET_ENDBR pxor %xmm7,%xmm7 .Lecb_ret: - xorps %xmm0,%xmm0 + xorps %xmm0,%xmm0 // %xmm0 pxor %xmm1,%xmm1 ret .cfi_endproc @@ -886,11 +886,11 @@ _CET_ENDBR cmpq $1,%rdx jne .Lctr32_bulk - - +// handle single block without allocating stack frame, +// useful when handling edges movups (%r8),%xmm2 movups (%rdi),%xmm3 - movl 240(%rcx),%edx + movl 240(%rcx),%edx // key->rounds movups (%rcx),%xmm0 movups 16(%rcx),%xmm1 leaq 32(%rcx),%rcx @@ -900,9 +900,9 @@ _CET_ENDBR decl %edx movups (%rcx),%xmm1 leaq 16(%rcx),%rcx - jnz .Loop_enc1_5 + jnz .Loop_enc1_5 // loop body is 16 bytes aesenclast %xmm1,%xmm2 - pxor %xmm0,%xmm0 + pxor %xmm0,%xmm0 // clear register bank pxor %xmm1,%xmm1 xorps %xmm3,%xmm2 pxor %xmm3,%xmm3 @@ -912,22 +912,22 @@ _CET_ENDBR .align 16 .Lctr32_bulk: - leaq (%rsp),%r11 + leaq (%rsp),%r11 // use %r11 as frame pointer .cfi_def_cfa_register %r11 pushq %rbp .cfi_offset %rbp,-16 subq $128,%rsp - andq $-16,%rsp - - + andq $-16,%rsp // Linux kernel stack can be incorrectly seeded +// 8 16-byte words on top of stack are counter values +// xor-ed with zero-round key movdqu (%r8),%xmm2 movdqu (%rcx),%xmm0 - movl 12(%r8),%r8d + movl 12(%r8),%r8d // counter LSB pxor %xmm0,%xmm2 - movl 12(%rcx),%ebp - movdqa %xmm2,0(%rsp) + movl 12(%rcx),%ebp // 0-round key LSB + movdqa %xmm2,0(%rsp) // populate counter block bswapl %r8d movdqa %xmm2,%xmm3 movdqa %xmm2,%xmm4 @@ -935,7 +935,7 @@ _CET_ENDBR movdqa %xmm2,64(%rsp) movdqa %xmm2,80(%rsp) movdqa %xmm2,96(%rsp) - movq %rdx,%r10 + movq %rdx,%r10 // about to borrow %rdx movdqa %xmm2,112(%rsp) leaq 1(%r8),%rax @@ -949,7 +949,7 @@ _CET_ENDBR movdqa %xmm3,16(%rsp) pinsrd $3,%edx,%xmm4 bswapl %eax - movq %r10,%rdx + movq %r10,%rdx // restore %rdx leaq 4(%r8),%r10 movdqa %xmm4,32(%rsp) xorl %ebp,%eax @@ -961,7 +961,7 @@ _CET_ENDBR movl %r10d,64+12(%rsp) bswapl %r9d leaq 6(%r8),%r10 - movl 240(%rcx),%eax + movl 240(%rcx),%eax // key->rounds xorl %ebp,%r9d bswapl %r10d movl %r9d,80+12(%rsp) @@ -977,16 +977,16 @@ _CET_ENDBR movdqa 64(%rsp),%xmm6 movdqa 80(%rsp),%xmm7 - cmpq $8,%rdx - jb .Lctr32_tail + cmpq $8,%rdx // %rdx is in blocks + jb .Lctr32_tail // short input if (%rdx<8) - leaq 128(%rcx),%rcx - subq $8,%rdx + leaq 128(%rcx),%rcx // size optimization + subq $8,%rdx // %rdx is biased by -8 jmp .Lctr32_loop8 .align 32 .Lctr32_loop8: - addl $8,%r8d + addl $8,%r8d // next counter value movdqa 96(%rsp),%xmm8 aesenc %xmm1,%xmm2 movl %r8d,%r9d @@ -998,7 +998,7 @@ _CET_ENDBR xorl %ebp,%r9d nop aesenc %xmm1,%xmm5 - movl %r9d,0+12(%rsp) + movl %r9d,0+12(%rsp) // store next counter value leaq 1(%r8),%r9 aesenc %xmm1,%xmm6 aesenc %xmm1,%xmm7 @@ -1094,7 +1094,7 @@ _CET_ENDBR aesenc %xmm0,%xmm3 aesenc %xmm0,%xmm4 xorl %ebp,%r9d - movdqu 0(%rdi),%xmm10 + movdqu 0(%rdi),%xmm10 // start loading input aesenc %xmm0,%xmm5 movl %r9d,112+12(%rsp) cmpl $11,%eax @@ -1151,7 +1151,7 @@ _CET_ENDBR .align 16 .Lctr32_enc_done: movdqu 16(%rdi),%xmm11 - pxor %xmm0,%xmm10 + pxor %xmm0,%xmm10 // input^=round[last] movdqu 32(%rdi),%xmm12 pxor %xmm0,%xmm11 movdqu 48(%rdi),%xmm13 @@ -1160,8 +1160,8 @@ _CET_ENDBR pxor %xmm0,%xmm13 movdqu 80(%rdi),%xmm15 pxor %xmm0,%xmm14 - prefetcht0 448(%rdi) - prefetcht0 512(%rdi) + prefetcht0 448(%rdi) // We process 128 bytes (8*16), so to prefetch 1 iteration + prefetcht0 512(%rdi) // We need to prefetch 2 64 byte lines pxor %xmm0,%xmm15 aesenc %xmm1,%xmm2 aesenc %xmm1,%xmm3 @@ -1171,15 +1171,15 @@ _CET_ENDBR aesenc %xmm1,%xmm7 aesenc %xmm1,%xmm8 aesenc %xmm1,%xmm9 - movdqu 96(%rdi),%xmm1 - leaq 128(%rdi),%rdi + movdqu 96(%rdi),%xmm1 // borrow %xmm1 for inp[6] + leaq 128(%rdi),%rdi // %rdi+=8*16 - aesenclast %xmm10,%xmm2 - pxor %xmm0,%xmm1 + aesenclast %xmm10,%xmm2 // is inp[N]^round[last] + pxor %xmm0,%xmm1 // borrowed movdqu 112-128(%rdi),%xmm10 aesenclast %xmm11,%xmm3 pxor %xmm0,%xmm10 - movdqa 0(%rsp),%xmm11 + movdqa 0(%rsp),%xmm11 // load next counter block aesenclast %xmm12,%xmm4 aesenclast %xmm13,%xmm5 movdqa 16(%rsp),%xmm12 @@ -1190,10 +1190,10 @@ _CET_ENDBR movdqa 64(%rsp),%xmm15 aesenclast %xmm1,%xmm8 movdqa 80(%rsp),%xmm0 - movups 16-128(%rcx),%xmm1 + movups 16-128(%rcx),%xmm1 // real 1st-round key aesenclast %xmm10,%xmm9 - movups %xmm2,(%rsi) + movups %xmm2,(%rsi) // store 8 output blocks movdqa %xmm11,%xmm2 movups %xmm3,16(%rsi) movdqa %xmm12,%xmm3 @@ -1207,24 +1207,24 @@ _CET_ENDBR movdqa %xmm0,%xmm7 movups %xmm8,96(%rsi) movups %xmm9,112(%rsi) - leaq 128(%rsi),%rsi + leaq 128(%rsi),%rsi // %rsi+=8*16 subq $8,%rdx - jnc .Lctr32_loop8 + jnc .Lctr32_loop8 // loop if %rdx-=8 didn't borrow - addq $8,%rdx - jz .Lctr32_done + addq $8,%rdx // restore real remaining %rdx + jz .Lctr32_done // done if (%rdx==0) leaq -128(%rcx),%rcx .Lctr32_tail: - - +// note that at this point %xmm2..5 are populated with +// counter values xor-ed with 0-round key leaq 16(%rcx),%rcx cmpq $4,%rdx jb .Lctr32_loop3 je .Lctr32_loop4 - +// if (%rdx>4) compute 7 E(counter) shll $4,%eax movdqa 96(%rsp),%xmm8 pxor %xmm9,%xmm9 @@ -1232,14 +1232,14 @@ _CET_ENDBR movups 16(%rcx),%xmm0 aesenc %xmm1,%xmm2 aesenc %xmm1,%xmm3 - leaq 32-16(%rcx,%rax,1),%rcx + leaq 32-16(%rcx,%rax,1),%rcx // prepare for .Lenc_loop8_enter negq %rax aesenc %xmm1,%xmm4 - addq $16,%rax + addq $16,%rax // prepare for .Lenc_loop8_enter movups (%rdi),%xmm10 aesenc %xmm1,%xmm5 aesenc %xmm1,%xmm6 - movups 16(%rdi),%xmm11 + movups 16(%rdi),%xmm11 // pre-load input movups 32(%rdi),%xmm12 aesenc %xmm1,%xmm7 aesenc %xmm1,%xmm8 @@ -1250,7 +1250,7 @@ _CET_ENDBR pxor %xmm10,%xmm2 movdqu 64(%rdi),%xmm10 pxor %xmm11,%xmm3 - movdqu %xmm2,(%rsi) + movdqu %xmm2,(%rsi) // store output pxor %xmm12,%xmm4 movdqu %xmm3,16(%rsi) pxor %xmm13,%xmm5 @@ -1259,17 +1259,17 @@ _CET_ENDBR movdqu %xmm5,48(%rsi) movdqu %xmm6,64(%rsi) cmpq $6,%rdx - jb .Lctr32_done + jb .Lctr32_done // %rdx was 5, stop store movups 80(%rdi),%xmm11 xorps %xmm11,%xmm7 movups %xmm7,80(%rsi) - je .Lctr32_done + je .Lctr32_done // %rdx was 6, stop store movups 96(%rdi),%xmm12 xorps %xmm12,%xmm8 movups %xmm8,96(%rsi) - jmp .Lctr32_done + jmp .Lctr32_done // %rdx was 7, stop store .align 32 .Lctr32_loop4: @@ -1283,7 +1283,7 @@ _CET_ENDBR jnz .Lctr32_loop4 aesenclast %xmm1,%xmm2 aesenclast %xmm1,%xmm3 - movups (%rdi),%xmm10 + movups (%rdi),%xmm10 // load input movups 16(%rdi),%xmm11 aesenclast %xmm1,%xmm4 aesenclast %xmm1,%xmm5 @@ -1291,14 +1291,14 @@ _CET_ENDBR movups 48(%rdi),%xmm13 xorps %xmm10,%xmm2 - movups %xmm2,(%rsi) + movups %xmm2,(%rsi) // store output xorps %xmm11,%xmm3 movups %xmm3,16(%rsi) pxor %xmm12,%xmm4 movdqu %xmm4,32(%rsi) pxor %xmm13,%xmm5 movdqu %xmm5,48(%rsi) - jmp .Lctr32_done + jmp .Lctr32_done // %rdx was 4, stop store .align 32 .Lctr32_loop3: @@ -1313,23 +1313,23 @@ _CET_ENDBR aesenclast %xmm1,%xmm3 aesenclast %xmm1,%xmm4 - movups (%rdi),%xmm10 + movups (%rdi),%xmm10 // load input xorps %xmm10,%xmm2 - movups %xmm2,(%rsi) + movups %xmm2,(%rsi) // store output cmpq $2,%rdx - jb .Lctr32_done + jb .Lctr32_done // %rdx was 1, stop store movups 16(%rdi),%xmm11 xorps %xmm11,%xmm3 movups %xmm3,16(%rsi) - je .Lctr32_done + je .Lctr32_done // %rdx was 2, stop store movups 32(%rdi),%xmm12 xorps %xmm12,%xmm4 - movups %xmm4,32(%rsi) + movups %xmm4,32(%rsi) // %rdx was 3, stop store .Lctr32_done: - xorps %xmm0,%xmm0 + xorps %xmm0,%xmm0 // clear register bank xorl %ebp,%ebp pxor %xmm1,%xmm1 pxor %xmm2,%xmm2 @@ -1338,7 +1338,7 @@ _CET_ENDBR pxor %xmm5,%xmm5 pxor %xmm6,%xmm6 pxor %xmm7,%xmm7 - movaps %xmm0,0(%rsp) + movaps %xmm0,0(%rsp) // clear stack pxor %xmm8,%xmm8 movaps %xmm0,16(%rsp) pxor %xmm9,%xmm9 @@ -1369,15 +1369,15 @@ _CET_ENDBR aes_hw_cbc_encrypt: .cfi_startproc _CET_ENDBR - testq %rdx,%rdx + testq %rdx,%rdx // check length jz .Lcbc_ret - movl 240(%rcx),%r10d - movq %rcx,%r11 - testl %r9d,%r9d + movl 240(%rcx),%r10d // key->rounds + movq %rcx,%r11 // backup %rcx + testl %r9d,%r9d // 6th argument jz .Lcbc_decrypt - - movups (%r8),%xmm2 +// --------------------------- CBC ENCRYPT ------------------------------ + movups (%r8),%xmm2 // load iv as initial state movl %r10d,%eax cmpq $16,%rdx jb .Lcbc_enc_tail @@ -1385,9 +1385,9 @@ _CET_ENDBR jmp .Lcbc_enc_loop .align 16 .Lcbc_enc_loop: - movups (%rdi),%xmm3 + movups (%rdi),%xmm3 // load input leaq 16(%rdi),%rdi - +// xorps %xmm3,%xmm2 movups (%rcx),%xmm0 movups 16(%rcx),%xmm1 xorps %xmm0,%xmm3 @@ -1398,17 +1398,17 @@ _CET_ENDBR decl %eax movups (%rcx),%xmm1 leaq 16(%rcx),%rcx - jnz .Loop_enc1_6 + jnz .Loop_enc1_6 // loop body is 16 bytes aesenclast %xmm1,%xmm2 - movl %r10d,%eax - movq %r11,%rcx - movups %xmm2,0(%rsi) + movl %r10d,%eax // restore %eax + movq %r11,%rcx // restore %rcx + movups %xmm2,0(%rsi) // store output leaq 16(%rsi),%rsi subq $16,%rdx jnc .Lcbc_enc_loop addq $16,%rdx jnz .Lcbc_enc_tail - pxor %xmm0,%xmm0 + pxor %xmm0,%xmm0 // clear register bank pxor %xmm1,%xmm1 movups %xmm2,(%r8) pxor %xmm2,%xmm2 @@ -1416,30 +1416,30 @@ _CET_ENDBR jmp .Lcbc_ret .Lcbc_enc_tail: - movq %rdx,%rcx - xchgq %rdi,%rsi -.long 0x9066A4F3 - movl $16,%ecx + movq %rdx,%rcx // zaps %rcx + xchgq %rdi,%rsi // %rdi is %rsi and %rsi is %rdi now +.long 0x9066A4F3 // rep movsb + movl $16,%ecx // zero tail subq %rdx,%rcx xorl %eax,%eax -.long 0x9066AAF3 - leaq -16(%rdi),%rdi - movl %r10d,%eax - movq %rdi,%rsi - movq %r11,%rcx - xorq %rdx,%rdx - jmp .Lcbc_enc_loop - +.long 0x9066AAF3 // rep stosb + leaq -16(%rdi),%rdi // rewind %rsi by 1 block + movl %r10d,%eax // restore %eax + movq %rdi,%rsi // %rdi and %rsi are the same + movq %r11,%rcx // restore %rcx + xorq %rdx,%rdx // len=16 + jmp .Lcbc_enc_loop // one more spin +// --------------------------- CBC DECRYPT ------------------------------# .align 16 .Lcbc_decrypt: cmpq $16,%rdx jne .Lcbc_decrypt_bulk - - - movdqu (%rdi),%xmm2 - movdqu (%r8),%xmm3 - movdqa %xmm2,%xmm4 +// handle single block without allocating stack frame, +// useful in ciphertext stealing mode + movdqu (%rdi),%xmm2 // load input + movdqu (%r8),%xmm3 // load iv + movdqa %xmm2,%xmm4 // future iv movups (%rcx),%xmm0 movups 16(%rcx),%xmm1 leaq 32(%rcx),%rcx @@ -1449,32 +1449,32 @@ _CET_ENDBR decl %r10d movups (%rcx),%xmm1 leaq 16(%rcx),%rcx - jnz .Loop_dec1_7 + jnz .Loop_dec1_7 // loop body is 16 bytes aesdeclast %xmm1,%xmm2 - pxor %xmm0,%xmm0 + pxor %xmm0,%xmm0 // clear register bank pxor %xmm1,%xmm1 - movdqu %xmm4,(%r8) - xorps %xmm3,%xmm2 + movdqu %xmm4,(%r8) // store iv + xorps %xmm3,%xmm2 // ^=iv pxor %xmm3,%xmm3 - movups %xmm2,(%rsi) + movups %xmm2,(%rsi) // store output pxor %xmm2,%xmm2 jmp .Lcbc_ret .align 16 .Lcbc_decrypt_bulk: - leaq (%rsp),%r11 + leaq (%rsp),%r11 // frame pointer .cfi_def_cfa_register %r11 pushq %rbp .cfi_offset %rbp,-16 subq $16,%rsp - andq $-16,%rsp - movq %rcx,%rbp + andq $-16,%rsp // Linux kernel stack can be incorrectly seeded + movq %rcx,%rbp // [re-]backup %rcx [after reassignment] movups (%r8),%xmm10 movl %r10d,%eax cmpq $0x50,%rdx jbe .Lcbc_dec_tail movups (%rcx),%xmm0 - movdqu 0(%rdi),%xmm2 + movdqu 0(%rdi),%xmm2 // load input movdqu 16(%rdi),%xmm3 movdqa %xmm2,%xmm11 movdqu 32(%rdi),%xmm4 @@ -1488,8 +1488,8 @@ _CET_ENDBR cmpq $0x70,%rdx jbe .Lcbc_dec_six_or_seven - subq $0x70,%rdx - leaq 112(%rcx),%rcx + subq $0x70,%rdx // %rdx is biased by -7*16 + leaq 112(%rcx),%rcx // size optimization jmp .Lcbc_dec_loop8_enter .align 16 .Lcbc_dec_loop8: @@ -1503,7 +1503,7 @@ _CET_ENDBR movups 16-112(%rcx),%xmm1 pxor %xmm0,%xmm4 movq $-1,%rbp - cmpq $0x70,%rdx + cmpq $0x70,%rdx // is there at least 0x60 bytes ahead? pxor %xmm0,%xmm5 pxor %xmm0,%xmm6 pxor %xmm0,%xmm7 @@ -1652,11 +1652,11 @@ _CET_ENDBR movdqu 80(%rdi),%xmm1 aesdeclast %xmm10,%xmm2 - movdqu 96(%rdi),%xmm10 + movdqu 96(%rdi),%xmm10 // borrow %xmm10 pxor %xmm0,%xmm1 aesdeclast %xmm11,%xmm3 pxor %xmm0,%xmm10 - movdqu 112(%rdi),%xmm0 + movdqu 112(%rdi),%xmm0 // next IV aesdeclast %xmm12,%xmm4 leaq 128(%rdi),%rdi movdqu 0(%rbp),%xmm11 @@ -1669,11 +1669,11 @@ _CET_ENDBR movdqu 48(%rbp),%xmm14 movdqu 64(%rbp),%xmm15 aesdeclast %xmm10,%xmm9 - movdqa %xmm0,%xmm10 + movdqa %xmm0,%xmm10 // return %xmm10 movdqu 80(%rbp),%xmm1 movups -112(%rcx),%xmm0 - movups %xmm2,(%rsi) + movups %xmm2,(%rsi) // store output movdqa %xmm11,%xmm2 movups %xmm3,16(%rsi) movdqa %xmm12,%xmm3 @@ -1707,13 +1707,13 @@ _CET_ENDBR movaps %xmm7,%xmm8 call _aesni_decrypt6 - pxor %xmm10,%xmm2 + pxor %xmm10,%xmm2 // ^= IV movaps %xmm8,%xmm10 pxor %xmm11,%xmm3 movdqu %xmm2,(%rsi) pxor %xmm12,%xmm4 movdqu %xmm3,16(%rsi) - pxor %xmm3,%xmm3 + pxor %xmm3,%xmm3 // clear register bank pxor %xmm13,%xmm5 movdqu %xmm4,32(%rsi) pxor %xmm4,%xmm4 @@ -1734,13 +1734,13 @@ _CET_ENDBR xorps %xmm9,%xmm9 call _aesni_decrypt8 movups 80(%rdi),%xmm9 - pxor %xmm10,%xmm2 + pxor %xmm10,%xmm2 // ^= IV movups 96(%rdi),%xmm10 pxor %xmm11,%xmm3 movdqu %xmm2,(%rsi) pxor %xmm12,%xmm4 movdqu %xmm3,16(%rsi) - pxor %xmm3,%xmm3 + pxor %xmm3,%xmm3 // clear register bank pxor %xmm13,%xmm5 movdqu %xmm4,32(%rsi) pxor %xmm4,%xmm4 @@ -1762,24 +1762,24 @@ _CET_ENDBR .Lcbc_dec_tail: movups (%rdi),%xmm2 subq $0x10,%rdx - jbe .Lcbc_dec_one + jbe .Lcbc_dec_one // %rdx is 1*16 or less movups 16(%rdi),%xmm3 movaps %xmm2,%xmm11 subq $0x10,%rdx - jbe .Lcbc_dec_two + jbe .Lcbc_dec_two // %rdx is 2*16 or less movups 32(%rdi),%xmm4 movaps %xmm3,%xmm12 subq $0x10,%rdx - jbe .Lcbc_dec_three + jbe .Lcbc_dec_three // %rdx is 3*16 or less movups 48(%rdi),%xmm5 movaps %xmm4,%xmm13 subq $0x10,%rdx - jbe .Lcbc_dec_four + jbe .Lcbc_dec_four // %rdx is 4*16 or less - movups 64(%rdi),%xmm6 + movups 64(%rdi),%xmm6 // %rdx is 5*16 or less movaps %xmm5,%xmm14 movaps %xmm6,%xmm15 xorps %xmm7,%xmm7 @@ -1790,7 +1790,7 @@ _CET_ENDBR movdqu %xmm2,(%rsi) pxor %xmm12,%xmm4 movdqu %xmm3,16(%rsi) - pxor %xmm3,%xmm3 + pxor %xmm3,%xmm3 // clear register bank pxor %xmm13,%xmm5 movdqu %xmm4,32(%rsi) pxor %xmm4,%xmm4 @@ -1816,7 +1816,7 @@ _CET_ENDBR decl %eax movups (%rcx),%xmm1 leaq 16(%rcx),%rcx - jnz .Loop_dec1_8 + jnz .Loop_dec1_8 // loop body is 16 bytes aesdeclast %xmm1,%xmm2 xorps %xmm10,%xmm2 movaps %xmm11,%xmm10 @@ -1830,7 +1830,7 @@ _CET_ENDBR pxor %xmm11,%xmm3 movdqu %xmm2,(%rsi) movdqa %xmm3,%xmm2 - pxor %xmm3,%xmm3 + pxor %xmm3,%xmm3 // clear register bank leaq 16(%rsi),%rsi jmp .Lcbc_dec_tail_collected .align 16 @@ -1843,7 +1843,7 @@ _CET_ENDBR movdqu %xmm2,(%rsi) pxor %xmm12,%xmm4 movdqu %xmm3,16(%rsi) - pxor %xmm3,%xmm3 + pxor %xmm3,%xmm3 // clear register bank movdqa %xmm4,%xmm2 pxor %xmm4,%xmm4 leaq 32(%rsi),%rsi @@ -1858,7 +1858,7 @@ _CET_ENDBR movdqu %xmm2,(%rsi) pxor %xmm12,%xmm4 movdqu %xmm3,16(%rsi) - pxor %xmm3,%xmm3 + pxor %xmm3,%xmm3 // clear register bank pxor %xmm13,%xmm5 movdqu %xmm4,32(%rsi) pxor %xmm4,%xmm4 @@ -1869,10 +1869,10 @@ _CET_ENDBR .align 16 .Lcbc_dec_clear_tail_collected: - pxor %xmm3,%xmm3 + pxor %xmm3,%xmm3 // clear register bank pxor %xmm4,%xmm4 pxor %xmm5,%xmm5 - pxor %xmm6,%xmm6 + pxor %xmm6,%xmm6 // %xmm6..9 pxor %xmm7,%xmm7 pxor %xmm8,%xmm8 pxor %xmm9,%xmm9 @@ -1891,11 +1891,11 @@ _CET_ENDBR movq %rsi,%rdi subq %rdx,%rcx leaq (%rsp),%rsi -.long 0x9066A4F3 +.long 0x9066A4F3 // rep movsb movdqa %xmm2,(%rsp) .Lcbc_dec_ret: - xorps %xmm0,%xmm0 + xorps %xmm0,%xmm0 // %xmm0 pxor %xmm1,%xmm1 movq -8(%r11),%rbp .cfi_restore %rbp @@ -1916,9 +1916,9 @@ _CET_ENDBR movl 240(%rdi),%esi shll $4,%esi - leaq 16(%rdi,%rsi,1),%rdx + leaq 16(%rdi,%rsi,1),%rdx // points at the end of key schedule - movups (%rdi),%xmm0 + movups (%rdi),%xmm0 // just swap movups (%rdx),%xmm1 movups %xmm0,(%rdx) movups %xmm1,(%rdi) @@ -1926,7 +1926,7 @@ _CET_ENDBR leaq -16(%rdx),%rdx .Ldec_key_inverse: - movups (%rdi),%xmm0 + movups (%rdi),%xmm0 // swap and inverse movups (%rdx),%xmm1 aesimc %xmm0,%xmm0 aesimc %xmm1,%xmm1 @@ -1937,7 +1937,7 @@ _CET_ENDBR cmpq %rdi,%rdx ja .Ldec_key_inverse - movups (%rdi),%xmm0 + movups (%rdi),%xmm0 // inverse middle aesimc %xmm0,%xmm0 pxor %xmm1,%xmm1 movups %xmm0,(%rdx) @@ -1960,9 +1960,9 @@ _CET_ENDBR .cfi_adjust_cfa_offset 8 - movups (%rdi),%xmm0 - xorps %xmm4,%xmm4 - leaq 16(%rdx),%rax + movups (%rdi),%xmm0 // pull first 128 bits of *userKey + xorps %xmm4,%xmm4 // low dword of xmm4 is assumed 0 + leaq 16(%rdx),%rax // %rax is used as modifiable copy of %rdx cmpl $256,%esi je .L14rounds cmpl $192,%esi @@ -1971,97 +1971,97 @@ _CET_ENDBR jne .Lbad_keybits .L10rounds: - movl $9,%esi + movl $9,%esi // 10 rounds for 128-bit key - movups %xmm0,(%rdx) - aeskeygenassist $0x1,%xmm0,%xmm1 + movups %xmm0,(%rdx) // round 0 + aeskeygenassist $0x1,%xmm0,%xmm1 // round 1 call .Lkey_expansion_128_cold - aeskeygenassist $0x2,%xmm0,%xmm1 + aeskeygenassist $0x2,%xmm0,%xmm1 // round 2 call .Lkey_expansion_128 - aeskeygenassist $0x4,%xmm0,%xmm1 + aeskeygenassist $0x4,%xmm0,%xmm1 // round 3 call .Lkey_expansion_128 - aeskeygenassist $0x8,%xmm0,%xmm1 + aeskeygenassist $0x8,%xmm0,%xmm1 // round 4 call .Lkey_expansion_128 - aeskeygenassist $0x10,%xmm0,%xmm1 + aeskeygenassist $0x10,%xmm0,%xmm1 // round 5 call .Lkey_expansion_128 - aeskeygenassist $0x20,%xmm0,%xmm1 + aeskeygenassist $0x20,%xmm0,%xmm1 // round 6 call .Lkey_expansion_128 - aeskeygenassist $0x40,%xmm0,%xmm1 + aeskeygenassist $0x40,%xmm0,%xmm1 // round 7 call .Lkey_expansion_128 - aeskeygenassist $0x80,%xmm0,%xmm1 + aeskeygenassist $0x80,%xmm0,%xmm1 // round 8 call .Lkey_expansion_128 - aeskeygenassist $0x1b,%xmm0,%xmm1 + aeskeygenassist $0x1b,%xmm0,%xmm1 // round 9 call .Lkey_expansion_128 - aeskeygenassist $0x36,%xmm0,%xmm1 + aeskeygenassist $0x36,%xmm0,%xmm1 // round 10 call .Lkey_expansion_128 movups %xmm0,(%rax) - movl %esi,80(%rax) + movl %esi,80(%rax) // 240(%rdx) xorl %eax,%eax jmp .Lenc_key_ret .align 16 .L12rounds: - movq 16(%rdi),%xmm2 - movl $11,%esi + movq 16(%rdi),%xmm2 // remaining 1/3 of *userKey + movl $11,%esi // 12 rounds for 192 - movups %xmm0,(%rdx) - aeskeygenassist $0x1,%xmm2,%xmm1 + movups %xmm0,(%rdx) // round 0 + aeskeygenassist $0x1,%xmm2,%xmm1 // round 1,2 call .Lkey_expansion_192a_cold - aeskeygenassist $0x2,%xmm2,%xmm1 + aeskeygenassist $0x2,%xmm2,%xmm1 // round 2,3 call .Lkey_expansion_192b - aeskeygenassist $0x4,%xmm2,%xmm1 + aeskeygenassist $0x4,%xmm2,%xmm1 // round 4,5 call .Lkey_expansion_192a - aeskeygenassist $0x8,%xmm2,%xmm1 + aeskeygenassist $0x8,%xmm2,%xmm1 // round 5,6 call .Lkey_expansion_192b - aeskeygenassist $0x10,%xmm2,%xmm1 + aeskeygenassist $0x10,%xmm2,%xmm1 // round 7,8 call .Lkey_expansion_192a - aeskeygenassist $0x20,%xmm2,%xmm1 + aeskeygenassist $0x20,%xmm2,%xmm1 // round 8,9 call .Lkey_expansion_192b - aeskeygenassist $0x40,%xmm2,%xmm1 + aeskeygenassist $0x40,%xmm2,%xmm1 // round 10,11 call .Lkey_expansion_192a - aeskeygenassist $0x80,%xmm2,%xmm1 + aeskeygenassist $0x80,%xmm2,%xmm1 // round 11,12 call .Lkey_expansion_192b movups %xmm0,(%rax) - movl %esi,48(%rax) + movl %esi,48(%rax) // 240(%rdx) xorq %rax,%rax jmp .Lenc_key_ret .align 16 .L14rounds: - movups 16(%rdi),%xmm2 - movl $13,%esi + movups 16(%rdi),%xmm2 // remaining half of *userKey + movl $13,%esi // 14 rounds for 256 leaq 16(%rax),%rax - movups %xmm0,(%rdx) - movups %xmm2,16(%rdx) - aeskeygenassist $0x1,%xmm2,%xmm1 + movups %xmm0,(%rdx) // round 0 + movups %xmm2,16(%rdx) // round 1 + aeskeygenassist $0x1,%xmm2,%xmm1 // round 2 call .Lkey_expansion_256a_cold - aeskeygenassist $0x1,%xmm0,%xmm1 + aeskeygenassist $0x1,%xmm0,%xmm1 // round 3 call .Lkey_expansion_256b - aeskeygenassist $0x2,%xmm2,%xmm1 + aeskeygenassist $0x2,%xmm2,%xmm1 // round 4 call .Lkey_expansion_256a - aeskeygenassist $0x2,%xmm0,%xmm1 + aeskeygenassist $0x2,%xmm0,%xmm1 // round 5 call .Lkey_expansion_256b - aeskeygenassist $0x4,%xmm2,%xmm1 + aeskeygenassist $0x4,%xmm2,%xmm1 // round 6 call .Lkey_expansion_256a - aeskeygenassist $0x4,%xmm0,%xmm1 + aeskeygenassist $0x4,%xmm0,%xmm1 // round 7 call .Lkey_expansion_256b - aeskeygenassist $0x8,%xmm2,%xmm1 + aeskeygenassist $0x8,%xmm2,%xmm1 // round 8 call .Lkey_expansion_256a - aeskeygenassist $0x8,%xmm0,%xmm1 + aeskeygenassist $0x8,%xmm0,%xmm1 // round 9 call .Lkey_expansion_256b - aeskeygenassist $0x10,%xmm2,%xmm1 + aeskeygenassist $0x10,%xmm2,%xmm1 // round 10 call .Lkey_expansion_256a - aeskeygenassist $0x10,%xmm0,%xmm1 + aeskeygenassist $0x10,%xmm0,%xmm1 // round 11 call .Lkey_expansion_256b - aeskeygenassist $0x20,%xmm2,%xmm1 + aeskeygenassist $0x20,%xmm2,%xmm1 // round 12 call .Lkey_expansion_256a - aeskeygenassist $0x20,%xmm0,%xmm1 + aeskeygenassist $0x20,%xmm0,%xmm1 // round 13 call .Lkey_expansion_256b - aeskeygenassist $0x40,%xmm2,%xmm1 + aeskeygenassist $0x40,%xmm2,%xmm1 // round 14 call .Lkey_expansion_256a movups %xmm0,(%rax) - movl %esi,16(%rax) + movl %esi,16(%rax) // 240(%rdx) xorq %rax,%rax jmp .Lenc_key_ret @@ -2080,6 +2080,7 @@ _CET_ENDBR ret .cfi_endproc +.size aes_hw_set_encrypt_key_base,.-aes_hw_set_encrypt_key_base .align 16 .Lkey_expansion_128: @@ -2091,7 +2092,7 @@ _CET_ENDBR xorps %xmm4,%xmm0 shufps $140,%xmm0,%xmm4 xorps %xmm4,%xmm0 - shufps $255,%xmm1,%xmm1 + shufps $255,%xmm1,%xmm1 // critical path xorps %xmm1,%xmm0 ret .cfi_endproc @@ -2110,7 +2111,7 @@ _CET_ENDBR shufps $140,%xmm0,%xmm4 pslldq $4,%xmm3 xorps %xmm4,%xmm0 - pshufd $85,%xmm1,%xmm1 + pshufd $85,%xmm1,%xmm1 // critical path pxor %xmm3,%xmm2 pxor %xmm1,%xmm0 pshufd $255,%xmm0,%xmm3 @@ -2140,7 +2141,7 @@ _CET_ENDBR xorps %xmm4,%xmm0 shufps $140,%xmm0,%xmm4 xorps %xmm4,%xmm0 - shufps $255,%xmm1,%xmm1 + shufps $255,%xmm1,%xmm1 // critical path xorps %xmm1,%xmm0 ret .cfi_endproc @@ -2155,11 +2156,10 @@ _CET_ENDBR xorps %xmm4,%xmm2 shufps $140,%xmm2,%xmm4 xorps %xmm4,%xmm2 - shufps $170,%xmm1,%xmm1 + shufps $170,%xmm1,%xmm1 // critical path xorps %xmm1,%xmm2 ret .cfi_endproc -.size aes_hw_set_encrypt_key_base,.-aes_hw_set_encrypt_key_base .globl aes_hw_set_encrypt_key_alt .hidden aes_hw_set_encrypt_key_alt @@ -2176,9 +2176,9 @@ _CET_ENDBR .cfi_adjust_cfa_offset 8 - movups (%rdi),%xmm0 - xorps %xmm4,%xmm4 - leaq 16(%rdx),%rax + movups (%rdi),%xmm0 // pull first 128 bits of *userKey + xorps %xmm4,%xmm4 // low dword of xmm4 is assumed 0 + leaq 16(%rdx),%rax // %rax is used as modifiable copy of %rdx cmpl $256,%esi je .L14rounds_alt cmpl $192,%esi @@ -2186,7 +2186,7 @@ _CET_ENDBR cmpl $128,%esi jne .Lbad_keybits_alt - movl $9,%esi + movl $9,%esi // 10 rounds for 128-bit key movdqa .Lkey_rotate(%rip),%xmm5 movl $8,%r10d movdqa .Lkey_rcon1(%rip),%xmm4 @@ -2248,14 +2248,14 @@ _CET_ENDBR pxor %xmm2,%xmm0 movdqu %xmm0,16(%rax) - movl %esi,96(%rax) + movl %esi,96(%rax) // 240(%rdx) xorl %eax,%eax jmp .Lenc_key_ret_alt .align 16 .L12rounds_alt: - movq 16(%rdi),%xmm2 - movl $11,%esi + movq 16(%rdi),%xmm2 // remaining 1/3 of *userKey + movl $11,%esi // 12 rounds for 192 movdqa .Lkey_rotate192(%rip),%xmm5 movdqa .Lkey_rcon1(%rip),%xmm4 movl $8,%r10d @@ -2291,14 +2291,14 @@ _CET_ENDBR decl %r10d jnz .Loop_key192 - movl %esi,32(%rax) + movl %esi,32(%rax) // 240(%rdx) xorl %eax,%eax jmp .Lenc_key_ret_alt .align 16 .L14rounds_alt: - movups 16(%rdi),%xmm2 - movl $13,%esi + movups 16(%rdi),%xmm2 // remaining half of *userKey + movl $13,%esi // 14 rounds for 256 leaq 16(%rax),%rax movdqa .Lkey_rotate(%rip),%xmm5 movdqa .Lkey_rcon1(%rip),%xmm4 @@ -2348,7 +2348,7 @@ _CET_ENDBR jmp .Loop_key256 .Ldone_key256: - movl %esi,16(%rax) + movl %esi,16(%rax) // 240(%rdx) xorl %eax,%eax jmp .Lenc_key_ret_alt @@ -2369,7 +2369,9 @@ _CET_ENDBR .size aes_hw_set_encrypt_key_alt,.-aes_hw_set_encrypt_key_alt .section .rodata + .align 64 +aesni_constants: .Lbswap_mask: .byte 15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0 .Lincrement32: diff --git a/third_party/boringssl/gen/bcm/aesni-x86_64-win.asm b/third_party/boringssl/gen/bcm/aesni-x86_64-win.asm index e7a8613ab..9a79ff051 100644 --- a/third_party/boringssl/gen/bcm/aesni-x86_64-win.asm +++ b/third_party/boringssl/gen/bcm/aesni-x86_64-win.asm @@ -23,8 +23,8 @@ _CET_ENDBR EXTERN BORINGSSL_function_hit mov BYTE[((BORINGSSL_function_hit+1))],1 %endif - movups xmm2,XMMWORD[rcx] - mov eax,DWORD[240+r8] + movups xmm2,XMMWORD[rcx] ; load input + mov eax,DWORD[240+r8] ; key->rounds movups xmm0,XMMWORD[r8] movups xmm1,XMMWORD[16+r8] lea r8,[32+r8] @@ -34,11 +34,11 @@ $L$oop_enc1_1: dec eax movups xmm1,XMMWORD[r8] lea r8,[16+r8] - jnz NEAR $L$oop_enc1_1 + jnz NEAR $L$oop_enc1_1 ; loop body is 16 bytes aesenclast xmm2,xmm1 - pxor xmm0,xmm0 + pxor xmm0,xmm0 ; clear register bank pxor xmm1,xmm1 - movups XMMWORD[rdx],xmm2 + movups XMMWORD[rdx],xmm2 ; output pxor xmm2,xmm2 ret @@ -50,8 +50,8 @@ ALIGN 16 aes_hw_decrypt: _CET_ENDBR - movups xmm2,XMMWORD[rcx] - mov eax,DWORD[240+r8] + movups xmm2,XMMWORD[rcx] ; load input + mov eax,DWORD[240+r8] ; key->rounds movups xmm0,XMMWORD[r8] movups xmm1,XMMWORD[16+r8] lea r8,[32+r8] @@ -61,11 +61,11 @@ $L$oop_dec1_2: dec eax movups xmm1,XMMWORD[r8] lea r8,[16+r8] - jnz NEAR $L$oop_dec1_2 + jnz NEAR $L$oop_dec1_2 ; loop body is 16 bytes aesdeclast xmm2,xmm1 - pxor xmm0,xmm0 + pxor xmm0,xmm0 ; clear register bank pxor xmm1,xmm1 - movups XMMWORD[rdx],xmm2 + movups XMMWORD[rdx],xmm2 ; output pxor xmm2,xmm2 ret @@ -81,7 +81,7 @@ _aesni_encrypt2: xorps xmm3,xmm0 movups xmm0,XMMWORD[32+rcx] lea rcx,[32+rax*1+rcx] - neg rax + neg rax ; %eax add rax,16 $L$enc_loop2: @@ -112,7 +112,7 @@ _aesni_decrypt2: xorps xmm3,xmm0 movups xmm0,XMMWORD[32+rcx] lea rcx,[32+rax*1+rcx] - neg rax + neg rax ; %eax add rax,16 $L$dec_loop2: @@ -144,7 +144,7 @@ _aesni_encrypt3: xorps xmm4,xmm0 movups xmm0,XMMWORD[32+rcx] lea rcx,[32+rax*1+rcx] - neg rax + neg rax ; %eax add rax,16 $L$enc_loop3: @@ -180,7 +180,7 @@ _aesni_decrypt3: xorps xmm4,xmm0 movups xmm0,XMMWORD[32+rcx] lea rcx,[32+rax*1+rcx] - neg rax + neg rax ; %eax add rax,16 $L$dec_loop3: @@ -217,7 +217,7 @@ _aesni_encrypt4: xorps xmm5,xmm0 movups xmm0,XMMWORD[32+rcx] lea rcx,[32+rax*1+rcx] - neg rax + neg rax ; %eax DB 0x0f,0x1f,0x00 add rax,16 @@ -259,7 +259,7 @@ _aesni_decrypt4: xorps xmm5,xmm0 movups xmm0,XMMWORD[32+rcx] lea rcx,[32+rax*1+rcx] - neg rax + neg rax ; %eax DB 0x0f,0x1f,0x00 add rax,16 @@ -300,7 +300,7 @@ _aesni_encrypt6: pxor xmm4,xmm0 aesenc xmm2,xmm1 lea rcx,[32+rax*1+rcx] - neg rax + neg rax ; %eax aesenc xmm3,xmm1 pxor xmm5,xmm0 pxor xmm6,xmm0 @@ -356,7 +356,7 @@ _aesni_decrypt6: pxor xmm4,xmm0 aesdec xmm2,xmm1 lea rcx,[32+rax*1+rcx] - neg rax + neg rax ; %eax aesdec xmm3,xmm1 pxor xmm5,xmm0 pxor xmm6,xmm0 @@ -413,7 +413,7 @@ _aesni_encrypt8: pxor xmm5,xmm0 pxor xmm6,xmm0 lea rcx,[32+rax*1+rcx] - neg rax + neg rax ; %eax aesenc xmm2,xmm1 pxor xmm7,xmm0 pxor xmm8,xmm0 @@ -479,7 +479,7 @@ _aesni_decrypt8: pxor xmm5,xmm0 pxor xmm6,xmm0 lea rcx,[32+rax*1+rcx] - neg rax + neg rax ; %eax aesdec xmm2,xmm1 pxor xmm7,xmm0 pxor xmm8,xmm0 @@ -550,25 +550,25 @@ $L$SEH_begin_aes_hw_ecb_encrypt: _CET_ENDBR lea rsp,[((-88))+rsp] - movaps XMMWORD[rsp],xmm6 + movaps XMMWORD[rsp],xmm6 ; offload %xmm6..7 movaps XMMWORD[16+rsp],xmm7 movaps XMMWORD[32+rsp],xmm8 movaps XMMWORD[48+rsp],xmm9 $L$ecb_enc_body: - and rdx,-16 - jz NEAR $L$ecb_ret + and rdx,-16 ; if (%rdx<16) + jz NEAR $L$ecb_ret ; return - mov eax,DWORD[240+rcx] + mov eax,DWORD[240+rcx] ; key->rounds movups xmm0,XMMWORD[rcx] - mov r11,rcx - mov r10d,eax - test r8d,r8d + mov r11,rcx ; backup %rcx + mov r10d,eax ; backup %eax + test r8d,r8d ; 5th argument jz NEAR $L$ecb_decrypt +; --------------------------- ECB ENCRYPT ------------------------------# + cmp rdx,0x80 ; if (%rdx<8*16) + jb NEAR $L$ecb_enc_tail ; short input - cmp rdx,0x80 - jb NEAR $L$ecb_enc_tail - - movdqu xmm2,XMMWORD[rdi] + movdqu xmm2,XMMWORD[rdi] ; load 8 input blocks movdqu xmm3,XMMWORD[16+rdi] movdqu xmm4,XMMWORD[32+rdi] movdqu xmm5,XMMWORD[48+rdi] @@ -576,15 +576,15 @@ $L$ecb_enc_body: movdqu xmm7,XMMWORD[80+rdi] movdqu xmm8,XMMWORD[96+rdi] movdqu xmm9,XMMWORD[112+rdi] - lea rdi,[128+rdi] - sub rdx,0x80 + lea rdi,[128+rdi] ; %rdi+=8*16 + sub rdx,0x80 ; %rdx-=8*16 (can be zero) jmp NEAR $L$ecb_enc_loop8_enter ALIGN 16 $L$ecb_enc_loop8: - movups XMMWORD[rsi],xmm2 - mov rcx,r11 - movdqu xmm2,XMMWORD[rdi] - mov eax,r10d + movups XMMWORD[rsi],xmm2 ; store 8 output blocks + mov rcx,r11 ; restore %rcx + movdqu xmm2,XMMWORD[rdi] ; load 8 input blocks + mov eax,r10d ; restore %eax movups XMMWORD[16+rsi],xmm3 movdqu xmm3,XMMWORD[16+rdi] movups XMMWORD[32+rsi],xmm4 @@ -598,31 +598,31 @@ $L$ecb_enc_loop8: movups XMMWORD[96+rsi],xmm8 movdqu xmm8,XMMWORD[96+rdi] movups XMMWORD[112+rsi],xmm9 - lea rsi,[128+rsi] + lea rsi,[128+rsi] ; %rsi+=8*16 movdqu xmm9,XMMWORD[112+rdi] - lea rdi,[128+rdi] + lea rdi,[128+rdi] ; %rdi+=8*16 $L$ecb_enc_loop8_enter: call _aesni_encrypt8 sub rdx,0x80 - jnc NEAR $L$ecb_enc_loop8 + jnc NEAR $L$ecb_enc_loop8 ; loop if %rdx-=8*16 didn't borrow - movups XMMWORD[rsi],xmm2 - mov rcx,r11 + movups XMMWORD[rsi],xmm2 ; store 8 output blocks + mov rcx,r11 ; restore %rcx movups XMMWORD[16+rsi],xmm3 - mov eax,r10d + mov eax,r10d ; restore %eax movups XMMWORD[32+rsi],xmm4 movups XMMWORD[48+rsi],xmm5 movups XMMWORD[64+rsi],xmm6 movups XMMWORD[80+rsi],xmm7 movups XMMWORD[96+rsi],xmm8 movups XMMWORD[112+rsi],xmm9 - lea rsi,[128+rsi] - add rdx,0x80 - jz NEAR $L$ecb_ret + lea rsi,[128+rsi] ; %rsi+=8*16 + add rdx,0x80 ; restore real remaining %rdx + jz NEAR $L$ecb_ret ; done if (%rdx==0) -$L$ecb_enc_tail: +$L$ecb_enc_tail: ; %rdx is less than 8*16 movups xmm2,XMMWORD[rdi] cmp rdx,0x20 jb NEAR $L$ecb_enc_one @@ -641,7 +641,7 @@ $L$ecb_enc_tail: movdqu xmm8,XMMWORD[96+rdi] xorps xmm9,xmm9 call _aesni_encrypt8 - movups XMMWORD[rsi],xmm2 + movups XMMWORD[rsi],xmm2 ; store 7 output blocks movups XMMWORD[16+rsi],xmm3 movups XMMWORD[32+rsi],xmm4 movups XMMWORD[48+rsi],xmm5 @@ -660,27 +660,27 @@ $L$oop_enc1_3: dec eax movups xmm1,XMMWORD[rcx] lea rcx,[16+rcx] - jnz NEAR $L$oop_enc1_3 + jnz NEAR $L$oop_enc1_3 ; loop body is 16 bytes aesenclast xmm2,xmm1 - movups XMMWORD[rsi],xmm2 + movups XMMWORD[rsi],xmm2 ; store one output block jmp NEAR $L$ecb_ret ALIGN 16 $L$ecb_enc_two: call _aesni_encrypt2 - movups XMMWORD[rsi],xmm2 + movups XMMWORD[rsi],xmm2 ; store 2 output blocks movups XMMWORD[16+rsi],xmm3 jmp NEAR $L$ecb_ret ALIGN 16 $L$ecb_enc_three: call _aesni_encrypt3 - movups XMMWORD[rsi],xmm2 + movups XMMWORD[rsi],xmm2 ; store 3 output blocks movups XMMWORD[16+rsi],xmm3 movups XMMWORD[32+rsi],xmm4 jmp NEAR $L$ecb_ret ALIGN 16 $L$ecb_enc_four: call _aesni_encrypt4 - movups XMMWORD[rsi],xmm2 + movups XMMWORD[rsi],xmm2 ; store 4 output blocks movups XMMWORD[16+rsi],xmm3 movups XMMWORD[32+rsi],xmm4 movups XMMWORD[48+rsi],xmm5 @@ -689,7 +689,7 @@ ALIGN 16 $L$ecb_enc_five: xorps xmm7,xmm7 call _aesni_encrypt6 - movups XMMWORD[rsi],xmm2 + movups XMMWORD[rsi],xmm2 ; store 5 output blocks movups XMMWORD[16+rsi],xmm3 movups XMMWORD[32+rsi],xmm4 movups XMMWORD[48+rsi],xmm5 @@ -698,20 +698,20 @@ $L$ecb_enc_five: ALIGN 16 $L$ecb_enc_six: call _aesni_encrypt6 - movups XMMWORD[rsi],xmm2 + movups XMMWORD[rsi],xmm2 ; store 6 output blocks movups XMMWORD[16+rsi],xmm3 movups XMMWORD[32+rsi],xmm4 movups XMMWORD[48+rsi],xmm5 movups XMMWORD[64+rsi],xmm6 movups XMMWORD[80+rsi],xmm7 jmp NEAR $L$ecb_ret - +; --------------------------- ECB DECRYPT ------------------------------# ALIGN 16 $L$ecb_decrypt: - cmp rdx,0x80 - jb NEAR $L$ecb_dec_tail + cmp rdx,0x80 ; if (%rdx<8*16) + jb NEAR $L$ecb_dec_tail ; short input - movdqu xmm2,XMMWORD[rdi] + movdqu xmm2,XMMWORD[rdi] ; load 8 input blocks movdqu xmm3,XMMWORD[16+rdi] movdqu xmm4,XMMWORD[32+rdi] movdqu xmm5,XMMWORD[48+rdi] @@ -719,15 +719,15 @@ $L$ecb_decrypt: movdqu xmm7,XMMWORD[80+rdi] movdqu xmm8,XMMWORD[96+rdi] movdqu xmm9,XMMWORD[112+rdi] - lea rdi,[128+rdi] - sub rdx,0x80 + lea rdi,[128+rdi] ; %rdi+=8*16 + sub rdx,0x80 ; %rdx-=8*16 (can be zero) jmp NEAR $L$ecb_dec_loop8_enter ALIGN 16 $L$ecb_dec_loop8: - movups XMMWORD[rsi],xmm2 - mov rcx,r11 - movdqu xmm2,XMMWORD[rdi] - mov eax,r10d + movups XMMWORD[rsi],xmm2 ; store 8 output blocks + mov rcx,r11 ; restore %rcx + movdqu xmm2,XMMWORD[rdi] ; load 8 input blocks + mov eax,r10d ; restore %eax movups XMMWORD[16+rsi],xmm3 movdqu xmm3,XMMWORD[16+rdi] movups XMMWORD[32+rsi],xmm4 @@ -741,23 +741,23 @@ $L$ecb_dec_loop8: movups XMMWORD[96+rsi],xmm8 movdqu xmm8,XMMWORD[96+rdi] movups XMMWORD[112+rsi],xmm9 - lea rsi,[128+rsi] + lea rsi,[128+rsi] ; %rsi+=8*16 movdqu xmm9,XMMWORD[112+rdi] - lea rdi,[128+rdi] + lea rdi,[128+rdi] ; %rdi+=8*16 $L$ecb_dec_loop8_enter: call _aesni_decrypt8 movups xmm0,XMMWORD[r11] sub rdx,0x80 - jnc NEAR $L$ecb_dec_loop8 + jnc NEAR $L$ecb_dec_loop8 ; loop if %rdx-=8*16 didn't borrow - movups XMMWORD[rsi],xmm2 - pxor xmm2,xmm2 - mov rcx,r11 + movups XMMWORD[rsi],xmm2 ; store 8 output blocks + pxor xmm2,xmm2 ; clear register bank + mov rcx,r11 ; restore %rcx movups XMMWORD[16+rsi],xmm3 pxor xmm3,xmm3 - mov eax,r10d + mov eax,r10d ; restore %eax movups XMMWORD[32+rsi],xmm4 pxor xmm4,xmm4 movups XMMWORD[48+rsi],xmm5 @@ -770,9 +770,9 @@ $L$ecb_dec_loop8_enter: pxor xmm8,xmm8 movups XMMWORD[112+rsi],xmm9 pxor xmm9,xmm9 - lea rsi,[128+rsi] - add rdx,0x80 - jz NEAR $L$ecb_ret + lea rsi,[128+rsi] ; %rsi+=8*16 + add rdx,0x80 ; restore real remaining %rdx + jz NEAR $L$ecb_ret ; done if (%rdx==0) $L$ecb_dec_tail: movups xmm2,XMMWORD[rdi] @@ -794,8 +794,8 @@ $L$ecb_dec_tail: movups xmm0,XMMWORD[rcx] xorps xmm9,xmm9 call _aesni_decrypt8 - movups XMMWORD[rsi],xmm2 - pxor xmm2,xmm2 + movups XMMWORD[rsi],xmm2 ; store 7 output blocks + pxor xmm2,xmm2 ; clear register bank movups XMMWORD[16+rsi],xmm3 pxor xmm3,xmm3 movups XMMWORD[32+rsi],xmm4 @@ -821,24 +821,24 @@ $L$oop_dec1_4: dec eax movups xmm1,XMMWORD[rcx] lea rcx,[16+rcx] - jnz NEAR $L$oop_dec1_4 + jnz NEAR $L$oop_dec1_4 ; loop body is 16 bytes aesdeclast xmm2,xmm1 - movups XMMWORD[rsi],xmm2 - pxor xmm2,xmm2 + movups XMMWORD[rsi],xmm2 ; store one output block + pxor xmm2,xmm2 ; clear register bank jmp NEAR $L$ecb_ret ALIGN 16 $L$ecb_dec_two: call _aesni_decrypt2 - movups XMMWORD[rsi],xmm2 - pxor xmm2,xmm2 + movups XMMWORD[rsi],xmm2 ; store 2 output blocks + pxor xmm2,xmm2 ; clear register bank movups XMMWORD[16+rsi],xmm3 pxor xmm3,xmm3 jmp NEAR $L$ecb_ret ALIGN 16 $L$ecb_dec_three: call _aesni_decrypt3 - movups XMMWORD[rsi],xmm2 - pxor xmm2,xmm2 + movups XMMWORD[rsi],xmm2 ; store 3 output blocks + pxor xmm2,xmm2 ; clear register bank movups XMMWORD[16+rsi],xmm3 pxor xmm3,xmm3 movups XMMWORD[32+rsi],xmm4 @@ -847,8 +847,8 @@ $L$ecb_dec_three: ALIGN 16 $L$ecb_dec_four: call _aesni_decrypt4 - movups XMMWORD[rsi],xmm2 - pxor xmm2,xmm2 + movups XMMWORD[rsi],xmm2 ; store 4 output blocks + pxor xmm2,xmm2 ; clear register bank movups XMMWORD[16+rsi],xmm3 pxor xmm3,xmm3 movups XMMWORD[32+rsi],xmm4 @@ -860,8 +860,8 @@ ALIGN 16 $L$ecb_dec_five: xorps xmm7,xmm7 call _aesni_decrypt6 - movups XMMWORD[rsi],xmm2 - pxor xmm2,xmm2 + movups XMMWORD[rsi],xmm2 ; store 5 output blocks + pxor xmm2,xmm2 ; clear register bank movups XMMWORD[16+rsi],xmm3 pxor xmm3,xmm3 movups XMMWORD[32+rsi],xmm4 @@ -875,8 +875,8 @@ $L$ecb_dec_five: ALIGN 16 $L$ecb_dec_six: call _aesni_decrypt6 - movups XMMWORD[rsi],xmm2 - pxor xmm2,xmm2 + movups XMMWORD[rsi],xmm2 ; store 6 output blocks + pxor xmm2,xmm2 ; clear register bank movups XMMWORD[16+rsi],xmm3 pxor xmm3,xmm3 movups XMMWORD[32+rsi],xmm4 @@ -889,10 +889,10 @@ $L$ecb_dec_six: pxor xmm7,xmm7 $L$ecb_ret: - xorps xmm0,xmm0 + xorps xmm0,xmm0 ; %xmm0 pxor xmm1,xmm1 movaps xmm6,XMMWORD[rsp] - movaps XMMWORD[rsp],xmm0 + movaps XMMWORD[rsp],xmm0 ; clear stack movaps xmm7,XMMWORD[16+rsp] movaps XMMWORD[16+rsp],xmm0 movaps xmm8,XMMWORD[32+rsp] @@ -929,11 +929,11 @@ _CET_ENDBR cmp rdx,1 jne NEAR $L$ctr32_bulk - - +; handle single block without allocating stack frame, +; useful when handling edges movups xmm2,XMMWORD[r8] movups xmm3,XMMWORD[rdi] - mov edx,DWORD[240+rcx] + mov edx,DWORD[240+rcx] ; key->rounds movups xmm0,XMMWORD[rcx] movups xmm1,XMMWORD[16+rcx] lea rcx,[32+rcx] @@ -943,9 +943,9 @@ $L$oop_enc1_5: dec edx movups xmm1,XMMWORD[rcx] lea rcx,[16+rcx] - jnz NEAR $L$oop_enc1_5 + jnz NEAR $L$oop_enc1_5 ; loop body is 16 bytes aesenclast xmm2,xmm1 - pxor xmm0,xmm0 + pxor xmm0,xmm0 ; clear register bank pxor xmm1,xmm1 xorps xmm2,xmm3 pxor xmm3,xmm3 @@ -955,13 +955,13 @@ $L$oop_enc1_5: ALIGN 16 $L$ctr32_bulk: - lea r11,[rsp] + lea r11,[rsp] ; use %r11 as frame pointer push rbp sub rsp,288 - and rsp,-16 - movaps XMMWORD[(-168)+r11],xmm6 + and rsp,-16 ; Linux kernel stack can be incorrectly seeded + movaps XMMWORD[(-168)+r11],xmm6 ; offload everything movaps XMMWORD[(-152)+r11],xmm7 movaps XMMWORD[(-136)+r11],xmm8 movaps XMMWORD[(-120)+r11],xmm9 @@ -973,15 +973,15 @@ $L$ctr32_bulk: movaps XMMWORD[(-24)+r11],xmm15 $L$ctr32_body: - - +; 8 16-byte words on top of stack are counter values +; xor-ed with zero-round key movdqu xmm2,XMMWORD[r8] movdqu xmm0,XMMWORD[rcx] - mov r8d,DWORD[12+r8] + mov r8d,DWORD[12+r8] ; counter LSB pxor xmm2,xmm0 - mov ebp,DWORD[12+rcx] - movdqa XMMWORD[rsp],xmm2 + mov ebp,DWORD[12+rcx] ; 0-round key LSB + movdqa XMMWORD[rsp],xmm2 ; populate counter block bswap r8d movdqa xmm3,xmm2 movdqa xmm4,xmm2 @@ -989,7 +989,7 @@ $L$ctr32_body: movdqa XMMWORD[64+rsp],xmm2 movdqa XMMWORD[80+rsp],xmm2 movdqa XMMWORD[96+rsp],xmm2 - mov r10,rdx + mov r10,rdx ; about to borrow %rdx movdqa XMMWORD[112+rsp],xmm2 lea rax,[1+r8] @@ -1003,7 +1003,7 @@ $L$ctr32_body: movdqa XMMWORD[16+rsp],xmm3 pinsrd xmm4,edx,3 bswap eax - mov rdx,r10 + mov rdx,r10 ; restore %rdx lea r10,[4+r8] movdqa XMMWORD[32+rsp],xmm4 xor eax,ebp @@ -1015,7 +1015,7 @@ $L$ctr32_body: mov DWORD[((64+12))+rsp],r10d bswap r9d lea r10,[6+r8] - mov eax,DWORD[240+rcx] + mov eax,DWORD[240+rcx] ; key->rounds xor r9d,ebp bswap r10d mov DWORD[((80+12))+rsp],r9d @@ -1031,16 +1031,16 @@ $L$ctr32_body: movdqa xmm6,XMMWORD[64+rsp] movdqa xmm7,XMMWORD[80+rsp] - cmp rdx,8 - jb NEAR $L$ctr32_tail + cmp rdx,8 ; %rdx is in blocks + jb NEAR $L$ctr32_tail ; short input if (%rdx<8) - lea rcx,[128+rcx] - sub rdx,8 + lea rcx,[128+rcx] ; size optimization + sub rdx,8 ; %rdx is biased by -8 jmp NEAR $L$ctr32_loop8 ALIGN 32 $L$ctr32_loop8: - add r8d,8 + add r8d,8 ; next counter value movdqa xmm8,XMMWORD[96+rsp] aesenc xmm2,xmm1 mov r9d,r8d @@ -1052,7 +1052,7 @@ $L$ctr32_loop8: xor r9d,ebp nop aesenc xmm5,xmm1 - mov DWORD[((0+12))+rsp],r9d + mov DWORD[((0+12))+rsp],r9d ; store next counter value lea r9,[1+r8] aesenc xmm6,xmm1 aesenc xmm7,xmm1 @@ -1148,7 +1148,7 @@ $L$ctr32_loop8: aesenc xmm3,xmm0 aesenc xmm4,xmm0 xor r9d,ebp - movdqu xmm10,XMMWORD[rdi] + movdqu xmm10,XMMWORD[rdi] ; start loading input aesenc xmm5,xmm0 mov DWORD[((112+12))+rsp],r9d cmp eax,11 @@ -1205,7 +1205,7 @@ $L$ctr32_loop8: ALIGN 16 $L$ctr32_enc_done: movdqu xmm11,XMMWORD[16+rdi] - pxor xmm10,xmm0 + pxor xmm10,xmm0 ; input^=round[last] movdqu xmm12,XMMWORD[32+rdi] pxor xmm11,xmm0 movdqu xmm13,XMMWORD[48+rdi] @@ -1214,8 +1214,8 @@ $L$ctr32_enc_done: pxor xmm13,xmm0 movdqu xmm15,XMMWORD[80+rdi] pxor xmm14,xmm0 - prefetcht0 [448+rdi] - prefetcht0 [512+rdi] + prefetcht0 [448+rdi] ; We process 128 bytes (8*16), so to prefetch 1 iteration + prefetcht0 [512+rdi] ; We need to prefetch 2 64 byte lines pxor xmm15,xmm0 aesenc xmm2,xmm1 aesenc xmm3,xmm1 @@ -1225,15 +1225,15 @@ $L$ctr32_enc_done: aesenc xmm7,xmm1 aesenc xmm8,xmm1 aesenc xmm9,xmm1 - movdqu xmm1,XMMWORD[96+rdi] - lea rdi,[128+rdi] + movdqu xmm1,XMMWORD[96+rdi] ; borrow %xmm1 for inp[6] + lea rdi,[128+rdi] ; %rdi+=8*16 - aesenclast xmm2,xmm10 - pxor xmm1,xmm0 + aesenclast xmm2,xmm10 ; is inp[N]^round[last] + pxor xmm1,xmm0 ; borrowed movdqu xmm10,XMMWORD[((112-128))+rdi] aesenclast xmm3,xmm11 pxor xmm10,xmm0 - movdqa xmm11,XMMWORD[rsp] + movdqa xmm11,XMMWORD[rsp] ; load next counter block aesenclast xmm4,xmm12 aesenclast xmm5,xmm13 movdqa xmm12,XMMWORD[16+rsp] @@ -1244,10 +1244,10 @@ $L$ctr32_enc_done: movdqa xmm15,XMMWORD[64+rsp] aesenclast xmm8,xmm1 movdqa xmm0,XMMWORD[80+rsp] - movups xmm1,XMMWORD[((16-128))+rcx] + movups xmm1,XMMWORD[((16-128))+rcx] ; real 1st-round key aesenclast xmm9,xmm10 - movups XMMWORD[rsi],xmm2 + movups XMMWORD[rsi],xmm2 ; store 8 output blocks movdqa xmm2,xmm11 movups XMMWORD[16+rsi],xmm3 movdqa xmm3,xmm12 @@ -1261,24 +1261,24 @@ $L$ctr32_enc_done: movdqa xmm7,xmm0 movups XMMWORD[96+rsi],xmm8 movups XMMWORD[112+rsi],xmm9 - lea rsi,[128+rsi] + lea rsi,[128+rsi] ; %rsi+=8*16 sub rdx,8 - jnc NEAR $L$ctr32_loop8 + jnc NEAR $L$ctr32_loop8 ; loop if %rdx-=8 didn't borrow - add rdx,8 - jz NEAR $L$ctr32_done + add rdx,8 ; restore real remaining %rdx + jz NEAR $L$ctr32_done ; done if (%rdx==0) lea rcx,[((-128))+rcx] $L$ctr32_tail: - - +; note that at this point %xmm2..5 are populated with +; counter values xor-ed with 0-round key lea rcx,[16+rcx] cmp rdx,4 jb NEAR $L$ctr32_loop3 je NEAR $L$ctr32_loop4 - +; if (%rdx>4) compute 7 E(counter) shl eax,4 movdqa xmm8,XMMWORD[96+rsp] pxor xmm9,xmm9 @@ -1286,14 +1286,14 @@ $L$ctr32_tail: movups xmm0,XMMWORD[16+rcx] aesenc xmm2,xmm1 aesenc xmm3,xmm1 - lea rcx,[((32-16))+rax*1+rcx] + lea rcx,[((32-16))+rax*1+rcx] ; prepare for .Lenc_loop8_enter neg rax aesenc xmm4,xmm1 - add rax,16 + add rax,16 ; prepare for .Lenc_loop8_enter movups xmm10,XMMWORD[rdi] aesenc xmm5,xmm1 aesenc xmm6,xmm1 - movups xmm11,XMMWORD[16+rdi] + movups xmm11,XMMWORD[16+rdi] ; pre-load input movups xmm12,XMMWORD[32+rdi] aesenc xmm7,xmm1 aesenc xmm8,xmm1 @@ -1304,7 +1304,7 @@ $L$ctr32_tail: pxor xmm2,xmm10 movdqu xmm10,XMMWORD[64+rdi] pxor xmm3,xmm11 - movdqu XMMWORD[rsi],xmm2 + movdqu XMMWORD[rsi],xmm2 ; store output pxor xmm4,xmm12 movdqu XMMWORD[16+rsi],xmm3 pxor xmm5,xmm13 @@ -1313,17 +1313,17 @@ $L$ctr32_tail: movdqu XMMWORD[48+rsi],xmm5 movdqu XMMWORD[64+rsi],xmm6 cmp rdx,6 - jb NEAR $L$ctr32_done + jb NEAR $L$ctr32_done ; %rdx was 5, stop store movups xmm11,XMMWORD[80+rdi] xorps xmm7,xmm11 movups XMMWORD[80+rsi],xmm7 - je NEAR $L$ctr32_done + je NEAR $L$ctr32_done ; %rdx was 6, stop store movups xmm12,XMMWORD[96+rdi] xorps xmm8,xmm12 movups XMMWORD[96+rsi],xmm8 - jmp NEAR $L$ctr32_done + jmp NEAR $L$ctr32_done ; %rdx was 7, stop store ALIGN 32 $L$ctr32_loop4: @@ -1337,7 +1337,7 @@ $L$ctr32_loop4: jnz NEAR $L$ctr32_loop4 aesenclast xmm2,xmm1 aesenclast xmm3,xmm1 - movups xmm10,XMMWORD[rdi] + movups xmm10,XMMWORD[rdi] ; load input movups xmm11,XMMWORD[16+rdi] aesenclast xmm4,xmm1 aesenclast xmm5,xmm1 @@ -1345,14 +1345,14 @@ $L$ctr32_loop4: movups xmm13,XMMWORD[48+rdi] xorps xmm2,xmm10 - movups XMMWORD[rsi],xmm2 + movups XMMWORD[rsi],xmm2 ; store output xorps xmm3,xmm11 movups XMMWORD[16+rsi],xmm3 pxor xmm4,xmm12 movdqu XMMWORD[32+rsi],xmm4 pxor xmm5,xmm13 movdqu XMMWORD[48+rsi],xmm5 - jmp NEAR $L$ctr32_done + jmp NEAR $L$ctr32_done ; %rdx was 4, stop store ALIGN 32 $L$ctr32_loop3: @@ -1367,23 +1367,23 @@ $L$ctr32_loop3: aesenclast xmm3,xmm1 aesenclast xmm4,xmm1 - movups xmm10,XMMWORD[rdi] + movups xmm10,XMMWORD[rdi] ; load input xorps xmm2,xmm10 - movups XMMWORD[rsi],xmm2 + movups XMMWORD[rsi],xmm2 ; store output cmp rdx,2 - jb NEAR $L$ctr32_done + jb NEAR $L$ctr32_done ; %rdx was 1, stop store movups xmm11,XMMWORD[16+rdi] xorps xmm3,xmm11 movups XMMWORD[16+rsi],xmm3 - je NEAR $L$ctr32_done + je NEAR $L$ctr32_done ; %rdx was 2, stop store movups xmm12,XMMWORD[32+rdi] xorps xmm4,xmm12 - movups XMMWORD[32+rsi],xmm4 + movups XMMWORD[32+rsi],xmm4 ; %rdx was 3, stop store $L$ctr32_done: - xorps xmm0,xmm0 + xorps xmm0,xmm0 ; clear register bank xor ebp,ebp pxor xmm1,xmm1 pxor xmm2,xmm2 @@ -1391,7 +1391,7 @@ $L$ctr32_done: pxor xmm4,xmm4 pxor xmm5,xmm5 movaps xmm6,XMMWORD[((-168))+r11] - movaps XMMWORD[(-168)+r11],xmm0 + movaps XMMWORD[(-168)+r11],xmm0 ; clear stack movaps xmm7,XMMWORD[((-152))+r11] movaps XMMWORD[(-152)+r11],xmm0 movaps xmm8,XMMWORD[((-136))+r11] @@ -1446,15 +1446,15 @@ $L$SEH_begin_aes_hw_cbc_encrypt: _CET_ENDBR - test rdx,rdx + test rdx,rdx ; check length jz NEAR $L$cbc_ret - mov r10d,DWORD[240+rcx] - mov r11,rcx - test r9d,r9d + mov r10d,DWORD[240+rcx] ; key->rounds + mov r11,rcx ; backup %rcx + test r9d,r9d ; 6th argument jz NEAR $L$cbc_decrypt - - movups xmm2,XMMWORD[r8] +; --------------------------- CBC ENCRYPT ------------------------------# + movups xmm2,XMMWORD[r8] ; load iv as initial state mov eax,r10d cmp rdx,16 jb NEAR $L$cbc_enc_tail @@ -1462,9 +1462,9 @@ _CET_ENDBR jmp NEAR $L$cbc_enc_loop ALIGN 16 $L$cbc_enc_loop: - movups xmm3,XMMWORD[rdi] + movups xmm3,XMMWORD[rdi] ; load input lea rdi,[16+rdi] - +; xorps %xmm3,%xmm2 movups xmm0,XMMWORD[rcx] movups xmm1,XMMWORD[16+rcx] xorps xmm3,xmm0 @@ -1475,17 +1475,17 @@ $L$oop_enc1_6: dec eax movups xmm1,XMMWORD[rcx] lea rcx,[16+rcx] - jnz NEAR $L$oop_enc1_6 + jnz NEAR $L$oop_enc1_6 ; loop body is 16 bytes aesenclast xmm2,xmm1 - mov eax,r10d - mov rcx,r11 - movups XMMWORD[rsi],xmm2 + mov eax,r10d ; restore %eax + mov rcx,r11 ; restore %rcx + movups XMMWORD[rsi],xmm2 ; store output lea rsi,[16+rsi] sub rdx,16 jnc NEAR $L$cbc_enc_loop add rdx,16 jnz NEAR $L$cbc_enc_tail - pxor xmm0,xmm0 + pxor xmm0,xmm0 ; clear register bank pxor xmm1,xmm1 movups XMMWORD[r8],xmm2 pxor xmm2,xmm2 @@ -1493,30 +1493,30 @@ $L$oop_enc1_6: jmp NEAR $L$cbc_ret $L$cbc_enc_tail: - mov rcx,rdx - xchg rsi,rdi - DD 0x9066A4F3 - mov ecx,16 + mov rcx,rdx ; zaps %rcx + xchg rsi,rdi ; %rdi is %rsi and %rsi is %rdi now + DD 0x9066A4F3 ; rep movsb + mov ecx,16 ; zero tail sub rcx,rdx xor eax,eax - DD 0x9066AAF3 - lea rdi,[((-16))+rdi] - mov eax,r10d - mov rsi,rdi - mov rcx,r11 - xor rdx,rdx - jmp NEAR $L$cbc_enc_loop - + DD 0x9066AAF3 ; rep stosb + lea rdi,[((-16))+rdi] ; rewind %rsi by 1 block + mov eax,r10d ; restore %eax + mov rsi,rdi ; %rdi and %rsi are the same + mov rcx,r11 ; restore %rcx + xor rdx,rdx ; len=16 + jmp NEAR $L$cbc_enc_loop ; one more spin +; --------------------------- CBC DECRYPT ------------------------------# ALIGN 16 $L$cbc_decrypt: cmp rdx,16 jne NEAR $L$cbc_decrypt_bulk - - - movdqu xmm2,XMMWORD[rdi] - movdqu xmm3,XMMWORD[r8] - movdqa xmm4,xmm2 +; handle single block without allocating stack frame, +; useful in ciphertext stealing mode + movdqu xmm2,XMMWORD[rdi] ; load input + movdqu xmm3,XMMWORD[r8] ; load iv + movdqa xmm4,xmm2 ; future iv movups xmm0,XMMWORD[rcx] movups xmm1,XMMWORD[16+rcx] lea rcx,[32+rcx] @@ -1526,24 +1526,24 @@ $L$oop_dec1_7: dec r10d movups xmm1,XMMWORD[rcx] lea rcx,[16+rcx] - jnz NEAR $L$oop_dec1_7 + jnz NEAR $L$oop_dec1_7 ; loop body is 16 bytes aesdeclast xmm2,xmm1 - pxor xmm0,xmm0 + pxor xmm0,xmm0 ; clear register bank pxor xmm1,xmm1 - movdqu XMMWORD[r8],xmm4 - xorps xmm2,xmm3 + movdqu XMMWORD[r8],xmm4 ; store iv + xorps xmm2,xmm3 ; ^=iv pxor xmm3,xmm3 - movups XMMWORD[rsi],xmm2 + movups XMMWORD[rsi],xmm2 ; store output pxor xmm2,xmm2 jmp NEAR $L$cbc_ret ALIGN 16 $L$cbc_decrypt_bulk: - lea r11,[rsp] + lea r11,[rsp] ; frame pointer push rbp sub rsp,176 - and rsp,-16 + and rsp,-16 ; Linux kernel stack can be incorrectly seeded movaps XMMWORD[16+rsp],xmm6 movaps XMMWORD[32+rsp],xmm7 movaps XMMWORD[48+rsp],xmm8 @@ -1555,14 +1555,14 @@ $L$cbc_decrypt_bulk: movaps XMMWORD[144+rsp],xmm14 movaps XMMWORD[160+rsp],xmm15 $L$cbc_decrypt_body: - mov rbp,rcx + mov rbp,rcx ; [re-]backup %rcx [after reassignment] movups xmm10,XMMWORD[r8] mov eax,r10d cmp rdx,0x50 jbe NEAR $L$cbc_dec_tail movups xmm0,XMMWORD[rcx] - movdqu xmm2,XMMWORD[rdi] + movdqu xmm2,XMMWORD[rdi] ; load input movdqu xmm3,XMMWORD[16+rdi] movdqa xmm11,xmm2 movdqu xmm4,XMMWORD[32+rdi] @@ -1576,8 +1576,8 @@ $L$cbc_decrypt_body: cmp rdx,0x70 jbe NEAR $L$cbc_dec_six_or_seven - sub rdx,0x70 - lea rcx,[112+rcx] + sub rdx,0x70 ; %rdx is biased by -7*16 + lea rcx,[112+rcx] ; size optimization jmp NEAR $L$cbc_dec_loop8_enter ALIGN 16 $L$cbc_dec_loop8: @@ -1591,7 +1591,7 @@ $L$cbc_dec_loop8_enter: movups xmm1,XMMWORD[((16-112))+rcx] pxor xmm4,xmm0 mov rbp,-1 - cmp rdx,0x70 + cmp rdx,0x70 ; is there at least 0x60 bytes ahead? pxor xmm5,xmm0 pxor xmm6,xmm0 pxor xmm7,xmm0 @@ -1740,11 +1740,11 @@ $L$cbc_dec_done: movdqu xmm1,XMMWORD[80+rdi] aesdeclast xmm2,xmm10 - movdqu xmm10,XMMWORD[96+rdi] + movdqu xmm10,XMMWORD[96+rdi] ; borrow %xmm10 pxor xmm1,xmm0 aesdeclast xmm3,xmm11 pxor xmm10,xmm0 - movdqu xmm0,XMMWORD[112+rdi] + movdqu xmm0,XMMWORD[112+rdi] ; next IV aesdeclast xmm4,xmm12 lea rdi,[128+rdi] movdqu xmm11,XMMWORD[rbp] @@ -1757,11 +1757,11 @@ $L$cbc_dec_done: movdqu xmm14,XMMWORD[48+rbp] movdqu xmm15,XMMWORD[64+rbp] aesdeclast xmm9,xmm10 - movdqa xmm10,xmm0 + movdqa xmm10,xmm0 ; return %xmm10 movdqu xmm1,XMMWORD[80+rbp] movups xmm0,XMMWORD[((-112))+rcx] - movups XMMWORD[rsi],xmm2 + movups XMMWORD[rsi],xmm2 ; store output movdqa xmm2,xmm11 movups XMMWORD[16+rsi],xmm3 movdqa xmm3,xmm12 @@ -1795,13 +1795,13 @@ $L$cbc_dec_six_or_seven: movaps xmm8,xmm7 call _aesni_decrypt6 - pxor xmm2,xmm10 + pxor xmm2,xmm10 ; ^= IV movaps xmm10,xmm8 pxor xmm3,xmm11 movdqu XMMWORD[rsi],xmm2 pxor xmm4,xmm12 movdqu XMMWORD[16+rsi],xmm3 - pxor xmm3,xmm3 + pxor xmm3,xmm3 ; clear register bank pxor xmm5,xmm13 movdqu XMMWORD[32+rsi],xmm4 pxor xmm4,xmm4 @@ -1822,13 +1822,13 @@ $L$cbc_dec_seven: xorps xmm9,xmm9 call _aesni_decrypt8 movups xmm9,XMMWORD[80+rdi] - pxor xmm2,xmm10 + pxor xmm2,xmm10 ; ^= IV movups xmm10,XMMWORD[96+rdi] pxor xmm3,xmm11 movdqu XMMWORD[rsi],xmm2 pxor xmm4,xmm12 movdqu XMMWORD[16+rsi],xmm3 - pxor xmm3,xmm3 + pxor xmm3,xmm3 ; clear register bank pxor xmm5,xmm13 movdqu XMMWORD[32+rsi],xmm4 pxor xmm4,xmm4 @@ -1850,24 +1850,24 @@ $L$cbc_dec_seven: $L$cbc_dec_tail: movups xmm2,XMMWORD[rdi] sub rdx,0x10 - jbe NEAR $L$cbc_dec_one + jbe NEAR $L$cbc_dec_one ; %rdx is 1*16 or less movups xmm3,XMMWORD[16+rdi] movaps xmm11,xmm2 sub rdx,0x10 - jbe NEAR $L$cbc_dec_two + jbe NEAR $L$cbc_dec_two ; %rdx is 2*16 or less movups xmm4,XMMWORD[32+rdi] movaps xmm12,xmm3 sub rdx,0x10 - jbe NEAR $L$cbc_dec_three + jbe NEAR $L$cbc_dec_three ; %rdx is 3*16 or less movups xmm5,XMMWORD[48+rdi] movaps xmm13,xmm4 sub rdx,0x10 - jbe NEAR $L$cbc_dec_four + jbe NEAR $L$cbc_dec_four ; %rdx is 4*16 or less - movups xmm6,XMMWORD[64+rdi] + movups xmm6,XMMWORD[64+rdi] ; %rdx is 5*16 or less movaps xmm14,xmm5 movaps xmm15,xmm6 xorps xmm7,xmm7 @@ -1878,7 +1878,7 @@ $L$cbc_dec_tail: movdqu XMMWORD[rsi],xmm2 pxor xmm4,xmm12 movdqu XMMWORD[16+rsi],xmm3 - pxor xmm3,xmm3 + pxor xmm3,xmm3 ; clear register bank pxor xmm5,xmm13 movdqu XMMWORD[32+rsi],xmm4 pxor xmm4,xmm4 @@ -1904,7 +1904,7 @@ $L$oop_dec1_8: dec eax movups xmm1,XMMWORD[rcx] lea rcx,[16+rcx] - jnz NEAR $L$oop_dec1_8 + jnz NEAR $L$oop_dec1_8 ; loop body is 16 bytes aesdeclast xmm2,xmm1 xorps xmm2,xmm10 movaps xmm10,xmm11 @@ -1918,7 +1918,7 @@ $L$cbc_dec_two: pxor xmm3,xmm11 movdqu XMMWORD[rsi],xmm2 movdqa xmm2,xmm3 - pxor xmm3,xmm3 + pxor xmm3,xmm3 ; clear register bank lea rsi,[16+rsi] jmp NEAR $L$cbc_dec_tail_collected ALIGN 16 @@ -1931,7 +1931,7 @@ $L$cbc_dec_three: movdqu XMMWORD[rsi],xmm2 pxor xmm4,xmm12 movdqu XMMWORD[16+rsi],xmm3 - pxor xmm3,xmm3 + pxor xmm3,xmm3 ; clear register bank movdqa xmm2,xmm4 pxor xmm4,xmm4 lea rsi,[32+rsi] @@ -1946,7 +1946,7 @@ $L$cbc_dec_four: movdqu XMMWORD[rsi],xmm2 pxor xmm4,xmm12 movdqu XMMWORD[16+rsi],xmm3 - pxor xmm3,xmm3 + pxor xmm3,xmm3 ; clear register bank pxor xmm5,xmm13 movdqu XMMWORD[32+rsi],xmm4 pxor xmm4,xmm4 @@ -1957,7 +1957,7 @@ $L$cbc_dec_four: ALIGN 16 $L$cbc_dec_clear_tail_collected: - pxor xmm3,xmm3 + pxor xmm3,xmm3 ; clear register bank pxor xmm4,xmm4 pxor xmm5,xmm5 $L$cbc_dec_tail_collected: @@ -1975,14 +1975,14 @@ $L$cbc_dec_tail_partial: mov rdi,rsi sub rcx,rdx lea rsi,[rsp] - DD 0x9066A4F3 + DD 0x9066A4F3 ; rep movsb movdqa XMMWORD[rsp],xmm2 $L$cbc_dec_ret: - xorps xmm0,xmm0 + xorps xmm0,xmm0 ; %xmm0 pxor xmm1,xmm1 movaps xmm6,XMMWORD[16+rsp] - movaps XMMWORD[16+rsp],xmm0 + movaps XMMWORD[16+rsp],xmm0 ; clear stack movaps xmm7,XMMWORD[32+rsp] movaps XMMWORD[32+rsp],xmm0 movaps xmm8,XMMWORD[48+rsp] @@ -2021,9 +2021,9 @@ _CET_ENDBR mov edx,DWORD[240+rcx] shl edx,4 - lea r8,[16+rdx*1+rcx] + lea r8,[16+rdx*1+rcx] ; points at the end of key schedule - movups xmm0,XMMWORD[rcx] + movups xmm0,XMMWORD[rcx] ; just swap movups xmm1,XMMWORD[r8] movups XMMWORD[r8],xmm0 movups XMMWORD[rcx],xmm1 @@ -2031,7 +2031,7 @@ _CET_ENDBR lea r8,[((-16))+r8] $L$dec_key_inverse: - movups xmm0,XMMWORD[rcx] + movups xmm0,XMMWORD[rcx] ; swap and inverse movups xmm1,XMMWORD[r8] aesimc xmm0,xmm0 aesimc xmm1,xmm1 @@ -2042,7 +2042,7 @@ $L$dec_key_inverse: cmp r8,rcx ja NEAR $L$dec_key_inverse - movups xmm0,XMMWORD[rcx] + movups xmm0,XMMWORD[rcx] ; inverse middle aesimc xmm0,xmm0 pxor xmm1,xmm1 movups XMMWORD[r8],xmm0 @@ -2064,9 +2064,9 @@ _CET_ENDBR $L$SEH_prologue_aes_hw_set_encrypt_key_base_2: $L$SEH_endprologue_aes_hw_set_encrypt_key_base_3: - movups xmm0,XMMWORD[rcx] - xorps xmm4,xmm4 - lea rax,[16+r8] + movups xmm0,XMMWORD[rcx] ; pull first 128 bits of *userKey + xorps xmm4,xmm4 ; low dword of xmm4 is assumed 0 + lea rax,[16+r8] ; %rax is used as modifiable copy of %r8 cmp edx,256 je NEAR $L$14rounds cmp edx,192 @@ -2075,97 +2075,97 @@ $L$SEH_endprologue_aes_hw_set_encrypt_key_base_3: jne NEAR $L$bad_keybits $L$10rounds: - mov edx,9 + mov edx,9 ; 10 rounds for 128-bit key - movups XMMWORD[r8],xmm0 - aeskeygenassist xmm1,xmm0,0x1 + movups XMMWORD[r8],xmm0 ; round 0 + aeskeygenassist xmm1,xmm0,0x1 ; round 1 call $L$key_expansion_128_cold - aeskeygenassist xmm1,xmm0,0x2 + aeskeygenassist xmm1,xmm0,0x2 ; round 2 call $L$key_expansion_128 - aeskeygenassist xmm1,xmm0,0x4 + aeskeygenassist xmm1,xmm0,0x4 ; round 3 call $L$key_expansion_128 - aeskeygenassist xmm1,xmm0,0x8 + aeskeygenassist xmm1,xmm0,0x8 ; round 4 call $L$key_expansion_128 - aeskeygenassist xmm1,xmm0,0x10 + aeskeygenassist xmm1,xmm0,0x10 ; round 5 call $L$key_expansion_128 - aeskeygenassist xmm1,xmm0,0x20 + aeskeygenassist xmm1,xmm0,0x20 ; round 6 call $L$key_expansion_128 - aeskeygenassist xmm1,xmm0,0x40 + aeskeygenassist xmm1,xmm0,0x40 ; round 7 call $L$key_expansion_128 - aeskeygenassist xmm1,xmm0,0x80 + aeskeygenassist xmm1,xmm0,0x80 ; round 8 call $L$key_expansion_128 - aeskeygenassist xmm1,xmm0,0x1b + aeskeygenassist xmm1,xmm0,0x1b ; round 9 call $L$key_expansion_128 - aeskeygenassist xmm1,xmm0,0x36 + aeskeygenassist xmm1,xmm0,0x36 ; round 10 call $L$key_expansion_128 movups XMMWORD[rax],xmm0 - mov DWORD[80+rax],edx + mov DWORD[80+rax],edx ; 240(%rdx) xor eax,eax jmp NEAR $L$enc_key_ret ALIGN 16 $L$12rounds: - movq xmm2,QWORD[16+rcx] - mov edx,11 + movq xmm2,QWORD[16+rcx] ; remaining 1/3 of *userKey + mov edx,11 ; 12 rounds for 192 - movups XMMWORD[r8],xmm0 - aeskeygenassist xmm1,xmm2,0x1 + movups XMMWORD[r8],xmm0 ; round 0 + aeskeygenassist xmm1,xmm2,0x1 ; round 1,2 call $L$key_expansion_192a_cold - aeskeygenassist xmm1,xmm2,0x2 + aeskeygenassist xmm1,xmm2,0x2 ; round 2,3 call $L$key_expansion_192b - aeskeygenassist xmm1,xmm2,0x4 + aeskeygenassist xmm1,xmm2,0x4 ; round 4,5 call $L$key_expansion_192a - aeskeygenassist xmm1,xmm2,0x8 + aeskeygenassist xmm1,xmm2,0x8 ; round 5,6 call $L$key_expansion_192b - aeskeygenassist xmm1,xmm2,0x10 + aeskeygenassist xmm1,xmm2,0x10 ; round 7,8 call $L$key_expansion_192a - aeskeygenassist xmm1,xmm2,0x20 + aeskeygenassist xmm1,xmm2,0x20 ; round 8,9 call $L$key_expansion_192b - aeskeygenassist xmm1,xmm2,0x40 + aeskeygenassist xmm1,xmm2,0x40 ; round 10,11 call $L$key_expansion_192a - aeskeygenassist xmm1,xmm2,0x80 + aeskeygenassist xmm1,xmm2,0x80 ; round 11,12 call $L$key_expansion_192b movups XMMWORD[rax],xmm0 - mov DWORD[48+rax],edx + mov DWORD[48+rax],edx ; 240(%rdx) xor rax,rax jmp NEAR $L$enc_key_ret ALIGN 16 $L$14rounds: - movups xmm2,XMMWORD[16+rcx] - mov edx,13 + movups xmm2,XMMWORD[16+rcx] ; remaining half of *userKey + mov edx,13 ; 14 rounds for 256 lea rax,[16+rax] - movups XMMWORD[r8],xmm0 - movups XMMWORD[16+r8],xmm2 - aeskeygenassist xmm1,xmm2,0x1 + movups XMMWORD[r8],xmm0 ; round 0 + movups XMMWORD[16+r8],xmm2 ; round 1 + aeskeygenassist xmm1,xmm2,0x1 ; round 2 call $L$key_expansion_256a_cold - aeskeygenassist xmm1,xmm0,0x1 + aeskeygenassist xmm1,xmm0,0x1 ; round 3 call $L$key_expansion_256b - aeskeygenassist xmm1,xmm2,0x2 + aeskeygenassist xmm1,xmm2,0x2 ; round 4 call $L$key_expansion_256a - aeskeygenassist xmm1,xmm0,0x2 + aeskeygenassist xmm1,xmm0,0x2 ; round 5 call $L$key_expansion_256b - aeskeygenassist xmm1,xmm2,0x4 + aeskeygenassist xmm1,xmm2,0x4 ; round 6 call $L$key_expansion_256a - aeskeygenassist xmm1,xmm0,0x4 + aeskeygenassist xmm1,xmm0,0x4 ; round 7 call $L$key_expansion_256b - aeskeygenassist xmm1,xmm2,0x8 + aeskeygenassist xmm1,xmm2,0x8 ; round 8 call $L$key_expansion_256a - aeskeygenassist xmm1,xmm0,0x8 + aeskeygenassist xmm1,xmm0,0x8 ; round 9 call $L$key_expansion_256b - aeskeygenassist xmm1,xmm2,0x10 + aeskeygenassist xmm1,xmm2,0x10 ; round 10 call $L$key_expansion_256a - aeskeygenassist xmm1,xmm0,0x10 + aeskeygenassist xmm1,xmm0,0x10 ; round 11 call $L$key_expansion_256b - aeskeygenassist xmm1,xmm2,0x20 + aeskeygenassist xmm1,xmm2,0x20 ; round 12 call $L$key_expansion_256a - aeskeygenassist xmm1,xmm0,0x20 + aeskeygenassist xmm1,xmm0,0x20 ; round 13 call $L$key_expansion_256b - aeskeygenassist xmm1,xmm2,0x40 + aeskeygenassist xmm1,xmm2,0x40 ; round 14 call $L$key_expansion_256a movups XMMWORD[rax],xmm0 - mov DWORD[16+rax],edx + mov DWORD[16+rax],edx ; 240(%rdx) xor rax,rax jmp NEAR $L$enc_key_ret @@ -2185,6 +2185,7 @@ $L$enc_key_ret: $L$SEH_end_aes_hw_set_encrypt_key_base_4: + ALIGN 16 $L$key_expansion_128: @@ -2195,7 +2196,7 @@ $L$key_expansion_128_cold: xorps xmm0,xmm4 shufps xmm4,xmm0,140 xorps xmm0,xmm4 - shufps xmm1,xmm1,255 + shufps xmm1,xmm1,255 ; critical path xorps xmm0,xmm1 ret @@ -2214,7 +2215,7 @@ $L$key_expansion_192b_warm: shufps xmm4,xmm0,140 pslldq xmm3,4 xorps xmm0,xmm4 - pshufd xmm1,xmm1,85 + pshufd xmm1,xmm1,85 ; critical path pxor xmm2,xmm3 pxor xmm0,xmm1 pshufd xmm3,xmm0,255 @@ -2244,7 +2245,7 @@ $L$key_expansion_256a_cold: xorps xmm0,xmm4 shufps xmm4,xmm0,140 xorps xmm0,xmm4 - shufps xmm1,xmm1,255 + shufps xmm1,xmm1,255 ; critical path xorps xmm0,xmm1 ret @@ -2259,12 +2260,11 @@ $L$key_expansion_256b: xorps xmm2,xmm4 shufps xmm4,xmm2,140 xorps xmm2,xmm4 - shufps xmm1,xmm1,170 + shufps xmm1,xmm1,170 ; critical path xorps xmm2,xmm1 ret - global aes_hw_set_encrypt_key_alt ALIGN 16 @@ -2279,9 +2279,9 @@ _CET_ENDBR $L$SEH_prologue_aes_hw_set_encrypt_key_alt_2: $L$SEH_endprologue_aes_hw_set_encrypt_key_alt_3: - movups xmm0,XMMWORD[rcx] - xorps xmm4,xmm4 - lea rax,[16+r8] + movups xmm0,XMMWORD[rcx] ; pull first 128 bits of *userKey + xorps xmm4,xmm4 ; low dword of xmm4 is assumed 0 + lea rax,[16+r8] ; %rax is used as modifiable copy of %r8 cmp edx,256 je NEAR $L$14rounds_alt cmp edx,192 @@ -2289,7 +2289,7 @@ $L$SEH_endprologue_aes_hw_set_encrypt_key_alt_3: cmp edx,128 jne NEAR $L$bad_keybits_alt - mov edx,9 + mov edx,9 ; 10 rounds for 128-bit key movdqa xmm5,XMMWORD[$L$key_rotate] mov r10d,8 movdqa xmm4,XMMWORD[$L$key_rcon1] @@ -2351,14 +2351,14 @@ $L$oop_key128: pxor xmm0,xmm2 movdqu XMMWORD[16+rax],xmm0 - mov DWORD[96+rax],edx + mov DWORD[96+rax],edx ; 240(%r8) xor eax,eax jmp NEAR $L$enc_key_ret_alt ALIGN 16 $L$12rounds_alt: - movq xmm2,QWORD[16+rcx] - mov edx,11 + movq xmm2,QWORD[16+rcx] ; remaining 1/3 of *userKey + mov edx,11 ; 12 rounds for 192 movdqa xmm5,XMMWORD[$L$key_rotate192] movdqa xmm4,XMMWORD[$L$key_rcon1] mov r10d,8 @@ -2394,14 +2394,14 @@ $L$oop_key192: dec r10d jnz NEAR $L$oop_key192 - mov DWORD[32+rax],edx + mov DWORD[32+rax],edx ; 240(%r8) xor eax,eax jmp NEAR $L$enc_key_ret_alt ALIGN 16 $L$14rounds_alt: - movups xmm2,XMMWORD[16+rcx] - mov edx,13 + movups xmm2,XMMWORD[16+rcx] ; remaining half of *userKey + mov edx,13 ; 14 rounds for 256 lea rax,[16+rax] movdqa xmm5,XMMWORD[$L$key_rotate] movdqa xmm4,XMMWORD[$L$key_rcon1] @@ -2451,7 +2451,7 @@ $L$oop_key256: jmp NEAR $L$oop_key256 $L$done_key256: - mov DWORD[16+rax],edx + mov DWORD[16+rax],edx ; 240(%r8) xor eax,eax jmp NEAR $L$enc_key_ret_alt @@ -2472,7 +2472,9 @@ $L$enc_key_ret_alt: $L$SEH_end_aes_hw_set_encrypt_key_alt_4: section .rdata rdata align=8 + ALIGN 64 +aesni_constants: $L$bswap_mask: DB 15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0 $L$increment32: @@ -2497,7 +2499,7 @@ $L$key_rcon1b: DB 32,98,121,32,60,97,112,112,114,111,64,111,112,101,110,115 DB 115,108,46,111,114,103,62,0 ALIGN 64 -section .text +section .text code align=64 EXTERN __imp_RtlVirtualUnwind @@ -2514,29 +2516,29 @@ ecb_ccm64_se_handler: pushfq sub rsp,64 - mov rax,QWORD[120+r8] - mov rbx,QWORD[248+r8] + mov rax,QWORD[120+r8] ; pull context->Rax + mov rbx,QWORD[248+r8] ; pull context->Rip - mov rsi,QWORD[8+r9] - mov r11,QWORD[56+r9] + mov rsi,QWORD[8+r9] ; disp->ImageBase + mov r11,QWORD[56+r9] ; disp->HandlerData - mov r10d,DWORD[r11] - lea r10,[r10*1+rsi] - cmp rbx,r10 + mov r10d,DWORD[r11] ; HandlerData[0] + lea r10,[r10*1+rsi] ; prologue label + cmp rbx,r10 ; context->RipRsp - mov r10d,DWORD[4+r11] - lea r10,[r10*1+rsi] - cmp rbx,r10 + mov r10d,DWORD[4+r11] ; HandlerData[1] + lea r10,[r10*1+rsi] ; epilogue label + cmp rbx,r10 ; context->Rip>=epilogue label jae NEAR $L$common_seh_tail - lea rsi,[rax] - lea rdi,[512+r8] - mov ecx,8 - DD 0xa548f3fc - lea rax,[88+rax] + lea rsi,[rax] ; %xmm save area + lea rdi,[512+r8] ; &context.Xmm6 + mov ecx,8 ; 4*sizeof(%xmm0)/sizeof(%rax) + DD 0xa548f3fc ; cld; rep movsq + lea rax,[88+rax] ; adjust stack pointer jmp NEAR $L$common_seh_tail @@ -2555,33 +2557,33 @@ ctr_xts_se_handler: pushfq sub rsp,64 - mov rax,QWORD[120+r8] - mov rbx,QWORD[248+r8] + mov rax,QWORD[120+r8] ; pull context->Rax + mov rbx,QWORD[248+r8] ; pull context->Rip - mov rsi,QWORD[8+r9] - mov r11,QWORD[56+r9] + mov rsi,QWORD[8+r9] ; disp->ImageBase + mov r11,QWORD[56+r9] ; disp->HandlerData - mov r10d,DWORD[r11] - lea r10,[r10*1+rsi] - cmp rbx,r10 + mov r10d,DWORD[r11] ; HandlerData[0] + lea r10,[r10*1+rsi] ; prologue label + cmp rbx,r10 ; context->RipRsp - mov r10d,DWORD[4+r11] - lea r10,[r10*1+rsi] - cmp rbx,r10 + mov r10d,DWORD[4+r11] ; HandlerData[1] + lea r10,[r10*1+rsi] ; epilogue label + cmp rbx,r10 ; context->Rip>=epilogue label jae NEAR $L$common_seh_tail - mov rax,QWORD[208+r8] + mov rax,QWORD[208+r8] ; pull context->R11 - lea rsi,[((-168))+rax] - lea rdi,[512+r8] - mov ecx,20 - DD 0xa548f3fc + lea rsi,[((-168))+rax] ; %xmm save area + lea rdi,[512+r8] ; & context.Xmm6 + mov ecx,20 ; 10*sizeof(%xmm0)/sizeof(%rax) + DD 0xa548f3fc ; cld; rep movsq - mov rbp,QWORD[((-8))+rax] - mov QWORD[160+r8],rbp + mov rbp,QWORD[((-8))+rax] ; restore saved %rbp + mov QWORD[160+r8],rbp ; restore context->Rbp jmp NEAR $L$common_seh_tail @@ -2599,62 +2601,62 @@ cbc_se_handler: pushfq sub rsp,64 - mov rax,QWORD[152+r8] - mov rbx,QWORD[248+r8] + mov rax,QWORD[152+r8] ; pull context->Rsp + mov rbx,QWORD[248+r8] ; pull context->Rip lea r10,[$L$cbc_decrypt_bulk] - cmp rbx,r10 + cmp rbx,r10 ; context->Rip<"prologue" label jb NEAR $L$common_seh_tail - mov rax,QWORD[120+r8] + mov rax,QWORD[120+r8] ; pull context->Rax lea r10,[$L$cbc_decrypt_body] - cmp rbx,r10 + cmp rbx,r10 ; context->RipRsp lea r10,[$L$cbc_ret] - cmp rbx,r10 + cmp rbx,r10 ; context->Rip>="epilogue" label jae NEAR $L$common_seh_tail - lea rsi,[16+rax] - lea rdi,[512+r8] - mov ecx,20 - DD 0xa548f3fc + lea rsi,[16+rax] ; %xmm save area + lea rdi,[512+r8] ; &context.Xmm6 + mov ecx,20 ; 10*sizeof(%xmm0)/sizeof(%rax) + DD 0xa548f3fc ; cld; rep movsq - mov rax,QWORD[208+r8] + mov rax,QWORD[208+r8] ; pull context->R11 - mov rbp,QWORD[((-8))+rax] - mov QWORD[160+r8],rbp + mov rbp,QWORD[((-8))+rax] ; restore saved %rbp + mov QWORD[160+r8],rbp ; restore context->Rbp $L$common_seh_tail: mov rdi,QWORD[8+rax] mov rsi,QWORD[16+rax] - mov QWORD[152+r8],rax - mov QWORD[168+r8],rsi - mov QWORD[176+r8],rdi + mov QWORD[152+r8],rax ; restore context->Rsp + mov QWORD[168+r8],rsi ; restore context->Rsi + mov QWORD[176+r8],rdi ; restore context->Rdi - mov rdi,QWORD[40+r9] - mov rsi,r8 - mov ecx,154 - DD 0xa548f3fc + mov rdi,QWORD[40+r9] ; disp->ContextRecord + mov rsi,r8 ; context + mov ecx,154 ; sizeof(CONTEXT) + DD 0xa548f3fc ; cld; rep movsq mov rsi,r9 - xor rcx,rcx - mov rdx,QWORD[8+rsi] - mov r8,QWORD[rsi] - mov r9,QWORD[16+rsi] - mov r10,QWORD[40+rsi] - lea r11,[56+rsi] - lea r12,[24+rsi] - mov QWORD[32+rsp],r10 - mov QWORD[40+rsp],r11 - mov QWORD[48+rsp],r12 - mov QWORD[56+rsp],rcx + xor rcx,rcx ; arg1, UNW_FLAG_NHANDLER + mov rdx,QWORD[8+rsi] ; arg2, disp->ImageBase + mov r8,QWORD[rsi] ; arg3, disp->ControlPc + mov r9,QWORD[16+rsi] ; arg4, disp->FunctionEntry + mov r10,QWORD[40+rsi] ; disp->ContextRecord + lea r11,[56+rsi] ; &disp->HandlerData + lea r12,[24+rsi] ; &disp->EstablisherFrame + mov QWORD[32+rsp],r10 ; arg5 + mov QWORD[40+rsp],r11 ; arg6 + mov QWORD[48+rsp],r12 ; arg7 + mov QWORD[56+rsp],rcx ; arg8, (NULL) call QWORD[__imp_RtlVirtualUnwind] - mov eax,1 + mov eax,1 ; ExceptionContinueSearch add rsp,64 popfq pop r15 @@ -2685,15 +2687,15 @@ ALIGN 8 $L$SEH_info_ecb: DB 9,0,0,0 DD ecb_ccm64_se_handler wrt ..imagebase - DD $L$ecb_enc_body wrt ..imagebase,$L$ecb_enc_ret wrt ..imagebase + DD $L$ecb_enc_body wrt ..imagebase,$L$ecb_enc_ret wrt ..imagebase ; HandlerData[] $L$SEH_info_ctr32: DB 9,0,0,0 DD ctr_xts_se_handler wrt ..imagebase - DD $L$ctr32_body wrt ..imagebase,$L$ctr32_epilogue wrt ..imagebase + DD $L$ctr32_body wrt ..imagebase,$L$ctr32_epilogue wrt ..imagebase ; HandlerData[] $L$SEH_info_cbc: DB 9,0,0,0 DD cbc_se_handler wrt ..imagebase -section .pdata +section .pdata rdata align=4 ALIGN 4 DD $L$SEH_begin_aes_hw_set_encrypt_key_base_1 wrt ..imagebase DD $L$SEH_end_aes_hw_set_encrypt_key_base_4 wrt ..imagebase @@ -2704,10 +2706,10 @@ ALIGN 4 DD $L$SEH_info_aes_hw_set_encrypt_key_alt_0 wrt ..imagebase -section .xdata +section .xdata rdata align=8 ALIGN 4 $L$SEH_info_aes_hw_set_encrypt_key_base_0: - DB 1 + DB 1 ; version 1, no flags DB $L$SEH_endprologue_aes_hw_set_encrypt_key_base_3-$L$SEH_begin_aes_hw_set_encrypt_key_base_1 DB 1 DB 0 @@ -2716,7 +2718,7 @@ $L$SEH_info_aes_hw_set_encrypt_key_base_0: DW 0 $L$SEH_info_aes_hw_set_encrypt_key_alt_0: - DB 1 + DB 1 ; version 1, no flags DB $L$SEH_endprologue_aes_hw_set_encrypt_key_alt_3-$L$SEH_begin_aes_hw_set_encrypt_key_alt_1 DB 1 DB 0 diff --git a/third_party/boringssl/gen/bcm/aesv8-armv7-linux.S b/third_party/boringssl/gen/bcm/aesv8-armv7-linux.S index 2b3929a17..da6549f71 100644 --- a/third_party/boringssl/gen/bcm/aesv8-armv7-linux.S +++ b/third_party/boringssl/gen/bcm/aesv8-armv7-linux.S @@ -10,7 +10,9 @@ .fpu neon .code 32 #undef __thumb2__ + .align 5 +aes_hw_constants: .Lrcon: .long 0x01,0x01,0x01,0x01 .long 0x0c0f0e0d,0x0c0f0e0d,0x0c0f0e0d,0x0c0f0e0d @ rotate-n-splat diff --git a/third_party/boringssl/gen/bcm/aesv8-armv8-apple.S b/third_party/boringssl/gen/bcm/aesv8-armv8-apple.S index e34778be4..89acb2388 100644 --- a/third_party/boringssl/gen/bcm/aesv8-armv8-apple.S +++ b/third_party/boringssl/gen/bcm/aesv8-armv8-apple.S @@ -9,7 +9,9 @@ .arch_extension crypto .section __TEXT,__const + .align 5 +aes_hw_constants: Lrcon: .long 0x01,0x01,0x01,0x01 .long 0x0c0f0e0d,0x0c0f0e0d,0x0c0f0e0d,0x0c0f0e0d // rotate-n-splat @@ -187,6 +189,7 @@ Lenc_key_abort: .private_extern _aes_hw_set_decrypt_key .align 5 +.alt_entry _aes_hw_set_decrypt_key _aes_hw_set_decrypt_key: AARCH64_SIGN_LINK_REGISTER stp x29,x30,[sp,#-16]! @@ -229,6 +232,7 @@ Ldec_key_abort: .private_extern _aes_hw_encrypt .align 5 +.alt_entry _aes_hw_encrypt _aes_hw_encrypt: AARCH64_VALID_CALL_TARGET ldr w3,[x2,#240] @@ -260,6 +264,7 @@ Loop_enc: .private_extern _aes_hw_decrypt .align 5 +.alt_entry _aes_hw_decrypt _aes_hw_decrypt: AARCH64_VALID_CALL_TARGET ldr w3,[x2,#240] @@ -291,6 +296,7 @@ Loop_dec: .private_extern _aes_hw_cbc_encrypt .align 5 +.alt_entry _aes_hw_cbc_encrypt _aes_hw_cbc_encrypt: // Armv8.3-A PAuth: even though x30 is pushed to stack it is not popped later. AARCH64_VALID_CALL_TARGET @@ -584,6 +590,7 @@ Lcbc_abort: .private_extern _aes_hw_ctr32_encrypt_blocks .align 5 +.alt_entry _aes_hw_ctr32_encrypt_blocks _aes_hw_ctr32_encrypt_blocks: // Armv8.3-A PAuth: even though x30 is pushed to stack it is not popped later. AARCH64_VALID_CALL_TARGET diff --git a/third_party/boringssl/gen/bcm/aesv8-armv8-linux.S b/third_party/boringssl/gen/bcm/aesv8-armv8-linux.S index cbf5c0a65..7d9b33f32 100644 --- a/third_party/boringssl/gen/bcm/aesv8-armv8-linux.S +++ b/third_party/boringssl/gen/bcm/aesv8-armv8-linux.S @@ -8,7 +8,9 @@ .text .arch armv8-a+crypto .section .rodata + .align 5 +aes_hw_constants: .Lrcon: .long 0x01,0x01,0x01,0x01 .long 0x0c0f0e0d,0x0c0f0e0d,0x0c0f0e0d,0x0c0f0e0d // rotate-n-splat diff --git a/third_party/boringssl/gen/bcm/aesv8-armv8-win.S b/third_party/boringssl/gen/bcm/aesv8-armv8-win.S index 54bd1c519..5af113d5a 100644 --- a/third_party/boringssl/gen/bcm/aesv8-armv8-win.S +++ b/third_party/boringssl/gen/bcm/aesv8-armv8-win.S @@ -8,7 +8,9 @@ .text .arch armv8-a+crypto .section .rodata + .align 5 +aes_hw_constants: Lrcon: .long 0x01,0x01,0x01,0x01 .long 0x0c0f0e0d,0x0c0f0e0d,0x0c0f0e0d,0x0c0f0e0d // rotate-n-splat diff --git a/third_party/boringssl/gen/bcm/aesv8-gcm-armv8-apple.S b/third_party/boringssl/gen/bcm/aesv8-gcm-armv8-apple.S index 6a76daab3..e28ae4cb8 100644 --- a/third_party/boringssl/gen/bcm/aesv8-gcm-armv8-apple.S +++ b/third_party/boringssl/gen/bcm/aesv8-gcm-armv8-apple.S @@ -747,6 +747,7 @@ Lenc_blocks_less_than_1: // Last partial block handling .private_extern _aes_gcm_dec_kernel .align 4 +.alt_entry _aes_gcm_dec_kernel _aes_gcm_dec_kernel: AARCH64_SIGN_LINK_REGISTER stp x29, x30, [sp, #-224]! @@ -1479,6 +1480,7 @@ Ldec_blocks_less_than_1: // blocks left <= 1 .private_extern _aes_gcm_enc_kernel_eor3 .align 4 +.alt_entry _aes_gcm_enc_kernel_eor3 _aes_gcm_enc_kernel_eor3: AARCH64_SIGN_LINK_REGISTER stp x29, x30, [sp, #-224]! @@ -2195,6 +2197,7 @@ Lenc_blocks_less_than_1_eor3: // Last partial block handling .private_extern _aes_gcm_dec_kernel_eor3 .align 4 +.alt_entry _aes_gcm_dec_kernel_eor3 _aes_gcm_dec_kernel_eor3: AARCH64_SIGN_LINK_REGISTER stp x29, x30, [sp, #-224]! diff --git a/third_party/boringssl/gen/bcm/armv8-mont-apple.S b/third_party/boringssl/gen/bcm/armv8-mont-apple.S index 5e9a8e912..2a156d56a 100644 --- a/third_party/boringssl/gen/bcm/armv8-mont-apple.S +++ b/third_party/boringssl/gen/bcm/armv8-mont-apple.S @@ -13,9 +13,9 @@ _bn_mul_mont_words: AARCH64_SIGN_LINK_REGISTER tst x5,#7 - b.eq __bn_sqr8x_mont + b.eq Lsqr8x_mont tst x5,#3 - b.eq __bn_mul4x_mont + b.eq Lmul4x_mont Lmul_mont: stp x29,x30,[sp,#-64]! add x29,sp,#0 @@ -215,14 +215,13 @@ Lcond_copy: AARCH64_VALIDATE_LINK_REGISTER ret - .align 5 -__bn_sqr8x_mont: - // Not adding AARCH64_SIGN_LINK_REGISTER here because __bn_sqr8x_mont is jumped to +Lsqr8x_mont: + // Not adding AARCH64_SIGN_LINK_REGISTER here because .Lsqr8x_mont is jumped to // only from bn_mul_mont_words which has already signed the return address. cmp x1,x2 - b.ne __bn_mul4x_mont -Lsqr8x_mont: + b.ne Lmul4x_mont + stp x29,x30,[sp,#-128]! add x29,sp,#0 stp x19,x20,[sp,#16] @@ -990,10 +989,9 @@ Lsqr8x_done: AARCH64_VALIDATE_LINK_REGISTER ret - .align 5 -__bn_mul4x_mont: - // Not adding AARCH64_SIGN_LINK_REGISTER here because __bn_mul4x_mont is jumped to +Lmul4x_mont: + // Not adding AARCH64_SIGN_LINK_REGISTER here because .Lmul4x_mont is jumped to // only from bn_mul_mont_words or __bn_mul8x_mont which have already signed the // return address. stp x29,x30,[sp,#-128]! diff --git a/third_party/boringssl/gen/bcm/armv8-mont-linux.S b/third_party/boringssl/gen/bcm/armv8-mont-linux.S index de02424e9..0ddc42aae 100644 --- a/third_party/boringssl/gen/bcm/armv8-mont-linux.S +++ b/third_party/boringssl/gen/bcm/armv8-mont-linux.S @@ -13,9 +13,9 @@ bn_mul_mont_words: AARCH64_SIGN_LINK_REGISTER tst x5,#7 - b.eq __bn_sqr8x_mont + b.eq .Lsqr8x_mont tst x5,#3 - b.eq __bn_mul4x_mont + b.eq .Lmul4x_mont .Lmul_mont: stp x29,x30,[sp,#-64]! add x29,sp,#0 @@ -214,15 +214,14 @@ bn_mul_mont_words: ldr x29,[sp],#64 AARCH64_VALIDATE_LINK_REGISTER ret -.size bn_mul_mont_words,.-bn_mul_mont_words -.type __bn_sqr8x_mont,%function +.type .Lsqr8x_mont,%function .align 5 -__bn_sqr8x_mont: - // Not adding AARCH64_SIGN_LINK_REGISTER here because __bn_sqr8x_mont is jumped to +.Lsqr8x_mont: + // Not adding AARCH64_SIGN_LINK_REGISTER here because .Lsqr8x_mont is jumped to // only from bn_mul_mont_words which has already signed the return address. cmp x1,x2 - b.ne __bn_mul4x_mont -.Lsqr8x_mont: + b.ne .Lmul4x_mont + stp x29,x30,[sp,#-128]! add x29,sp,#0 stp x19,x20,[sp,#16] @@ -989,11 +988,10 @@ __bn_sqr8x_mont: // x30 is popped earlier AARCH64_VALIDATE_LINK_REGISTER ret -.size __bn_sqr8x_mont,.-__bn_sqr8x_mont -.type __bn_mul4x_mont,%function +.type .Lmul4x_mont,%function .align 5 -__bn_mul4x_mont: - // Not adding AARCH64_SIGN_LINK_REGISTER here because __bn_mul4x_mont is jumped to +.Lmul4x_mont: + // Not adding AARCH64_SIGN_LINK_REGISTER here because .Lmul4x_mont is jumped to // only from bn_mul_mont_words or __bn_mul8x_mont which have already signed the // return address. stp x29,x30,[sp,#-128]! @@ -1435,7 +1433,7 @@ __bn_mul4x_mont: // x30 is popped earlier AARCH64_VALIDATE_LINK_REGISTER ret -.size __bn_mul4x_mont,.-__bn_mul4x_mont +.size bn_mul_mont_words,.-bn_mul_mont_words .byte 77,111,110,116,103,111,109,101,114,121,32,77,117,108,116,105,112,108,105,99,97,116,105,111,110,32,102,111,114,32,65,82,77,118,56,44,32,67,82,89,80,84,79,71,65,77,83,32,98,121,32,60,97,112,112,114,111,64,111,112,101,110,115,115,108,46,111,114,103,62,0 .align 2 .align 4 diff --git a/third_party/boringssl/gen/bcm/armv8-mont-win.S b/third_party/boringssl/gen/bcm/armv8-mont-win.S index adfdba441..798ab5c37 100644 --- a/third_party/boringssl/gen/bcm/armv8-mont-win.S +++ b/third_party/boringssl/gen/bcm/armv8-mont-win.S @@ -15,9 +15,9 @@ bn_mul_mont_words: AARCH64_SIGN_LINK_REGISTER tst x5,#7 - b.eq __bn_sqr8x_mont + b.eq Lsqr8x_mont tst x5,#3 - b.eq __bn_mul4x_mont + b.eq Lmul4x_mont Lmul_mont: stp x29,x30,[sp,#-64]! add x29,sp,#0 @@ -216,17 +216,16 @@ Lcond_copy: ldr x29,[sp],#64 AARCH64_VALIDATE_LINK_REGISTER ret - -.def __bn_sqr8x_mont +.def Lsqr8x_mont .type 32 .endef .align 5 -__bn_sqr8x_mont: - // Not adding AARCH64_SIGN_LINK_REGISTER here because __bn_sqr8x_mont is jumped to +Lsqr8x_mont: + // Not adding AARCH64_SIGN_LINK_REGISTER here because .Lsqr8x_mont is jumped to // only from bn_mul_mont_words which has already signed the return address. cmp x1,x2 - b.ne __bn_mul4x_mont -Lsqr8x_mont: + b.ne Lmul4x_mont + stp x29,x30,[sp,#-128]! add x29,sp,#0 stp x19,x20,[sp,#16] @@ -993,13 +992,12 @@ Lsqr8x_done: // x30 is popped earlier AARCH64_VALIDATE_LINK_REGISTER ret - -.def __bn_mul4x_mont +.def Lmul4x_mont .type 32 .endef .align 5 -__bn_mul4x_mont: - // Not adding AARCH64_SIGN_LINK_REGISTER here because __bn_mul4x_mont is jumped to +Lmul4x_mont: + // Not adding AARCH64_SIGN_LINK_REGISTER here because .Lmul4x_mont is jumped to // only from bn_mul_mont_words or __bn_mul8x_mont which have already signed the // return address. stp x29,x30,[sp,#-128]! diff --git a/third_party/boringssl/gen/bcm/bn-armv8-apple.S b/third_party/boringssl/gen/bcm/bn-armv8-apple.S index d37c48e82..7e02b1a55 100644 --- a/third_party/boringssl/gen/bcm/bn-armv8-apple.S +++ b/third_party/boringssl/gen/bcm/bn-armv8-apple.S @@ -51,6 +51,7 @@ Ladd_exit: .globl _bn_sub_words .private_extern _bn_sub_words .align 4 +.alt_entry _bn_sub_words _bn_sub_words: AARCH64_VALID_CALL_TARGET # Set the carry flag. Arm's borrow bit is flipped from the carry flag, diff --git a/third_party/boringssl/gen/bcm/ghash-neon-armv8-apple.S b/third_party/boringssl/gen/bcm/ghash-neon-armv8-apple.S index 15b822c07..61ff561bd 100644 --- a/third_party/boringssl/gen/bcm/ghash-neon-armv8-apple.S +++ b/third_party/boringssl/gen/bcm/ghash-neon-armv8-apple.S @@ -36,6 +36,7 @@ _gcm_init_neon: .private_extern _gcm_gmult_neon .align 4 +.alt_entry _gcm_gmult_neon _gcm_gmult_neon: AARCH64_VALID_CALL_TARGET ld1 {v3.16b}, [x0] // load Xi @@ -56,6 +57,7 @@ _gcm_gmult_neon: .private_extern _gcm_ghash_neon .align 4 +.alt_entry _gcm_ghash_neon _gcm_ghash_neon: AARCH64_VALID_CALL_TARGET ld1 {v0.16b}, [x0] // load Xi @@ -321,7 +323,9 @@ Lgmult_neon: .section __TEXT,__const + .align 4 +ghash_neon_masks: Lmasks: .quad 0x0000ffffffffffff // k48 .quad 0x00000000ffffffff // k32 diff --git a/third_party/boringssl/gen/bcm/ghash-neon-armv8-linux.S b/third_party/boringssl/gen/bcm/ghash-neon-armv8-linux.S index ee57d5123..65a984120 100644 --- a/third_party/boringssl/gen/bcm/ghash-neon-armv8-linux.S +++ b/third_party/boringssl/gen/bcm/ghash-neon-armv8-linux.S @@ -321,7 +321,9 @@ gcm_ghash_neon: .size gcm_ghash_neon,.-gcm_ghash_neon .section .rodata + .align 4 +ghash_neon_masks: .Lmasks: .quad 0x0000ffffffffffff // k48 .quad 0x00000000ffffffff // k32 diff --git a/third_party/boringssl/gen/bcm/ghash-neon-armv8-win.S b/third_party/boringssl/gen/bcm/ghash-neon-armv8-win.S index 91814da04..937e58684 100644 --- a/third_party/boringssl/gen/bcm/ghash-neon-armv8-win.S +++ b/third_party/boringssl/gen/bcm/ghash-neon-armv8-win.S @@ -327,7 +327,9 @@ Lgmult_neon: .section .rodata + .align 4 +ghash_neon_masks: Lmasks: .quad 0x0000ffffffffffff // k48 .quad 0x00000000ffffffff // k32 diff --git a/third_party/boringssl/gen/bcm/ghash-ssse3-x86_64-apple.S b/third_party/boringssl/gen/bcm/ghash-ssse3-x86_64-apple.S index 53af23f83..558fb6eb7 100644 --- a/third_party/boringssl/gen/bcm/ghash-ssse3-x86_64-apple.S +++ b/third_party/boringssl/gen/bcm/ghash-ssse3-x86_64-apple.S @@ -6,10 +6,10 @@ #if !defined(OPENSSL_NO_ASM) && defined(OPENSSL_X86_64) && defined(__APPLE__) .text - - - - +// gcm_gmult_ssse3 multiplies |Xi| by |Htable| and writes the result to |Xi|. +// |Xi| is represented in GHASH's serialized byte representation. |Htable| is +// formatted as described above. +// void gcm_gmult_ssse3(uint64_t Xi[2], const u128 Htable[16]); .globl _gcm_gmult_ssse3 .private_extern _gcm_gmult_ssse3 @@ -22,18 +22,18 @@ _CET_ENDBR movdqa L$reverse_bytes(%rip),%xmm10 movdqa L$low4_mask(%rip),%xmm2 - +// Reverse input bytes to deserialize. pshufb %xmm10,%xmm0 - +// Split each byte into low (%xmm0) and high (%xmm1) halves. movdqa %xmm2,%xmm1 pandn %xmm0,%xmm1 psrld $4,%xmm1 pand %xmm2,%xmm0 - - - +// Maintain the result in %xmm2 (the value) and %xmm3 (carry bits). Note +// that, due to bit reversal, %xmm3 contains bits that fall off when +// right-shifting, not left-shifting. pxor %xmm2,%xmm2 pxor %xmm3,%xmm3 movq $5,%rax @@ -41,31 +41,31 @@ L$oop_row_1: movdqu (%rsi),%xmm4 leaq 16(%rsi),%rsi - +// Right-shift %xmm2 and %xmm3 by 8 bytes. movdqa %xmm2,%xmm6 palignr $1,%xmm3,%xmm6 movdqa %xmm6,%xmm3 psrldq $1,%xmm2 - - - +// Load the next table row and index the low and high bits of the input. +// Note the low (respectively, high) half corresponds to more +// (respectively, less) significant coefficients. movdqa %xmm4,%xmm5 pshufb %xmm0,%xmm4 pshufb %xmm1,%xmm5 - +// Add the high half (%xmm5) without shifting. pxor %xmm5,%xmm2 - - +// Add the low half (%xmm4). This must be right-shifted by 4 bits. First, +// add into the carry register (%xmm3). movdqa %xmm4,%xmm5 psllq $60,%xmm5 movdqa %xmm5,%xmm6 pslldq $8,%xmm6 pxor %xmm6,%xmm3 - +// Next, add into %xmm2. psrldq $8,%xmm5 pxor %xmm5,%xmm2 psrlq $4,%xmm4 @@ -74,46 +74,46 @@ L$oop_row_1: subq $1,%rax jnz L$oop_row_1 - - - pxor %xmm3,%xmm2 +// Reduce the carry register. The reduction polynomial is 1 + x + x^2 + +// x^7, so we shift and XOR four times. + pxor %xmm3,%xmm2 // x^0 = 0 psrlq $1,%xmm3 - pxor %xmm3,%xmm2 + pxor %xmm3,%xmm2 // x^1 = x psrlq $1,%xmm3 - pxor %xmm3,%xmm2 + pxor %xmm3,%xmm2 // x^(1+1) = x^2 psrlq $5,%xmm3 - pxor %xmm3,%xmm2 + pxor %xmm3,%xmm2 // x^(1+1+5) = x^7 pxor %xmm3,%xmm3 movq $5,%rax L$oop_row_2: movdqu (%rsi),%xmm4 leaq 16(%rsi),%rsi - +// Right-shift %xmm2 and %xmm3 by 8 bytes. movdqa %xmm2,%xmm6 palignr $1,%xmm3,%xmm6 movdqa %xmm6,%xmm3 psrldq $1,%xmm2 - - - +// Load the next table row and index the low and high bits of the input. +// Note the low (respectively, high) half corresponds to more +// (respectively, less) significant coefficients. movdqa %xmm4,%xmm5 pshufb %xmm0,%xmm4 pshufb %xmm1,%xmm5 - +// Add the high half (%xmm5) without shifting. pxor %xmm5,%xmm2 - - +// Add the low half (%xmm4). This must be right-shifted by 4 bits. First, +// add into the carry register (%xmm3). movdqa %xmm4,%xmm5 psllq $60,%xmm5 movdqa %xmm5,%xmm6 pslldq $8,%xmm6 pxor %xmm6,%xmm3 - +// Next, add into %xmm2. psrldq $8,%xmm5 pxor %xmm5,%xmm2 psrlq $4,%xmm4 @@ -122,46 +122,46 @@ L$oop_row_2: subq $1,%rax jnz L$oop_row_2 - - - pxor %xmm3,%xmm2 +// Reduce the carry register. The reduction polynomial is 1 + x + x^2 + +// x^7, so we shift and XOR four times. + pxor %xmm3,%xmm2 // x^0 = 0 psrlq $1,%xmm3 - pxor %xmm3,%xmm2 + pxor %xmm3,%xmm2 // x^1 = x psrlq $1,%xmm3 - pxor %xmm3,%xmm2 + pxor %xmm3,%xmm2 // x^(1+1) = x^2 psrlq $5,%xmm3 - pxor %xmm3,%xmm2 + pxor %xmm3,%xmm2 // x^(1+1+5) = x^7 pxor %xmm3,%xmm3 movq $6,%rax L$oop_row_3: movdqu (%rsi),%xmm4 leaq 16(%rsi),%rsi - +// Right-shift %xmm2 and %xmm3 by 8 bytes. movdqa %xmm2,%xmm6 palignr $1,%xmm3,%xmm6 movdqa %xmm6,%xmm3 psrldq $1,%xmm2 - - - +// Load the next table row and index the low and high bits of the input. +// Note the low (respectively, high) half corresponds to more +// (respectively, less) significant coefficients. movdqa %xmm4,%xmm5 pshufb %xmm0,%xmm4 pshufb %xmm1,%xmm5 - +// Add the high half (%xmm5) without shifting. pxor %xmm5,%xmm2 - - +// Add the low half (%xmm4). This must be right-shifted by 4 bits. First, +// add into the carry register (%xmm3). movdqa %xmm4,%xmm5 psllq $60,%xmm5 movdqa %xmm5,%xmm6 pslldq $8,%xmm6 pxor %xmm6,%xmm3 - +// Next, add into %xmm2. psrldq $8,%xmm5 pxor %xmm5,%xmm2 psrlq $4,%xmm4 @@ -170,21 +170,21 @@ L$oop_row_3: subq $1,%rax jnz L$oop_row_3 - - - pxor %xmm3,%xmm2 +// Reduce the carry register. The reduction polynomial is 1 + x + x^2 + +// x^7, so we shift and XOR four times. + pxor %xmm3,%xmm2 // x^0 = 0 psrlq $1,%xmm3 - pxor %xmm3,%xmm2 + pxor %xmm3,%xmm2 // x^1 = x psrlq $1,%xmm3 - pxor %xmm3,%xmm2 + pxor %xmm3,%xmm2 // x^(1+1) = x^2 psrlq $5,%xmm3 - pxor %xmm3,%xmm2 + pxor %xmm3,%xmm2 // x^(1+1+5) = x^7 pxor %xmm3,%xmm3 - +// Store the result. Reverse bytes to serialize. pshufb %xmm10,%xmm2 movdqu %xmm2,(%rdi) - +// Zero any registers which contain secrets. pxor %xmm0,%xmm0 pxor %xmm1,%xmm1 pxor %xmm2,%xmm2 @@ -196,15 +196,16 @@ L$oop_row_3: - - - - - +// gcm_ghash_ssse3 incorporates |len| bytes from |in| to |Xi|, using |Htable| as +// the key. It writes the result back to |Xi|. |Xi| is represented in GHASH's +// serialized byte representation. |Htable| is formatted as described above. +// void gcm_ghash_ssse3(uint64_t Xi[2], const u128 Htable[16], const uint8_t *in, +// size_t len); .globl _gcm_ghash_ssse3 .private_extern _gcm_ghash_ssse3 .p2align 4 +.alt_entry _gcm_ghash_ssse3 _gcm_ghash_ssse3: @@ -213,62 +214,62 @@ _CET_ENDBR movdqa L$reverse_bytes(%rip),%xmm10 movdqa L$low4_mask(%rip),%xmm11 - +// This function only processes whole blocks. andq $-16,%rcx - - +// Reverse input bytes to deserialize. We maintain the running +// total in %xmm0. pshufb %xmm10,%xmm0 - +// Iterate over each block. On entry to each iteration, %xmm3 is zero. pxor %xmm3,%xmm3 L$oop_ghash: - +// Incorporate the next block of input. movdqu (%rdx),%xmm1 - pshufb %xmm10,%xmm1 + pshufb %xmm10,%xmm1 // Reverse bytes. pxor %xmm1,%xmm0 - +// Split each byte into low (%xmm0) and high (%xmm1) halves. movdqa %xmm11,%xmm1 pandn %xmm0,%xmm1 psrld $4,%xmm1 pand %xmm11,%xmm0 - - - +// Maintain the result in %xmm2 (the value) and %xmm3 (carry bits). Note +// that, due to bit reversal, %xmm3 contains bits that fall off when +// right-shifting, not left-shifting. pxor %xmm2,%xmm2 - +// %xmm3 is already zero at this point. movq $5,%rax L$oop_row_4: movdqu (%rsi),%xmm4 leaq 16(%rsi),%rsi - +// Right-shift %xmm2 and %xmm3 by 8 bytes. movdqa %xmm2,%xmm6 palignr $1,%xmm3,%xmm6 movdqa %xmm6,%xmm3 psrldq $1,%xmm2 - - - +// Load the next table row and index the low and high bits of the input. +// Note the low (respectively, high) half corresponds to more +// (respectively, less) significant coefficients. movdqa %xmm4,%xmm5 pshufb %xmm0,%xmm4 pshufb %xmm1,%xmm5 - +// Add the high half (%xmm5) without shifting. pxor %xmm5,%xmm2 - - +// Add the low half (%xmm4). This must be right-shifted by 4 bits. First, +// add into the carry register (%xmm3). movdqa %xmm4,%xmm5 psllq $60,%xmm5 movdqa %xmm5,%xmm6 pslldq $8,%xmm6 pxor %xmm6,%xmm3 - +// Next, add into %xmm2. psrldq $8,%xmm5 pxor %xmm5,%xmm2 psrlq $4,%xmm4 @@ -277,46 +278,46 @@ L$oop_row_4: subq $1,%rax jnz L$oop_row_4 - - - pxor %xmm3,%xmm2 +// Reduce the carry register. The reduction polynomial is 1 + x + x^2 + +// x^7, so we shift and XOR four times. + pxor %xmm3,%xmm2 // x^0 = 0 psrlq $1,%xmm3 - pxor %xmm3,%xmm2 + pxor %xmm3,%xmm2 // x^1 = x psrlq $1,%xmm3 - pxor %xmm3,%xmm2 + pxor %xmm3,%xmm2 // x^(1+1) = x^2 psrlq $5,%xmm3 - pxor %xmm3,%xmm2 + pxor %xmm3,%xmm2 // x^(1+1+5) = x^7 pxor %xmm3,%xmm3 movq $5,%rax L$oop_row_5: movdqu (%rsi),%xmm4 leaq 16(%rsi),%rsi - +// Right-shift %xmm2 and %xmm3 by 8 bytes. movdqa %xmm2,%xmm6 palignr $1,%xmm3,%xmm6 movdqa %xmm6,%xmm3 psrldq $1,%xmm2 - - - +// Load the next table row and index the low and high bits of the input. +// Note the low (respectively, high) half corresponds to more +// (respectively, less) significant coefficients. movdqa %xmm4,%xmm5 pshufb %xmm0,%xmm4 pshufb %xmm1,%xmm5 - +// Add the high half (%xmm5) without shifting. pxor %xmm5,%xmm2 - - +// Add the low half (%xmm4). This must be right-shifted by 4 bits. First, +// add into the carry register (%xmm3). movdqa %xmm4,%xmm5 psllq $60,%xmm5 movdqa %xmm5,%xmm6 pslldq $8,%xmm6 pxor %xmm6,%xmm3 - +// Next, add into %xmm2. psrldq $8,%xmm5 pxor %xmm5,%xmm2 psrlq $4,%xmm4 @@ -325,46 +326,46 @@ L$oop_row_5: subq $1,%rax jnz L$oop_row_5 - - - pxor %xmm3,%xmm2 +// Reduce the carry register. The reduction polynomial is 1 + x + x^2 + +// x^7, so we shift and XOR four times. + pxor %xmm3,%xmm2 // x^0 = 0 psrlq $1,%xmm3 - pxor %xmm3,%xmm2 + pxor %xmm3,%xmm2 // x^1 = x psrlq $1,%xmm3 - pxor %xmm3,%xmm2 + pxor %xmm3,%xmm2 // x^(1+1) = x^2 psrlq $5,%xmm3 - pxor %xmm3,%xmm2 + pxor %xmm3,%xmm2 // x^(1+1+5) = x^7 pxor %xmm3,%xmm3 movq $6,%rax L$oop_row_6: movdqu (%rsi),%xmm4 leaq 16(%rsi),%rsi - +// Right-shift %xmm2 and %xmm3 by 8 bytes. movdqa %xmm2,%xmm6 palignr $1,%xmm3,%xmm6 movdqa %xmm6,%xmm3 psrldq $1,%xmm2 - - - +// Load the next table row and index the low and high bits of the input. +// Note the low (respectively, high) half corresponds to more +// (respectively, less) significant coefficients. movdqa %xmm4,%xmm5 pshufb %xmm0,%xmm4 pshufb %xmm1,%xmm5 - +// Add the high half (%xmm5) without shifting. pxor %xmm5,%xmm2 - - +// Add the low half (%xmm4). This must be right-shifted by 4 bits. First, +// add into the carry register (%xmm3). movdqa %xmm4,%xmm5 psllq $60,%xmm5 movdqa %xmm5,%xmm6 pslldq $8,%xmm6 pxor %xmm6,%xmm3 - +// Next, add into %xmm2. psrldq $8,%xmm5 pxor %xmm5,%xmm2 psrlq $4,%xmm4 @@ -373,31 +374,31 @@ L$oop_row_6: subq $1,%rax jnz L$oop_row_6 - - - pxor %xmm3,%xmm2 +// Reduce the carry register. The reduction polynomial is 1 + x + x^2 + +// x^7, so we shift and XOR four times. + pxor %xmm3,%xmm2 // x^0 = 0 psrlq $1,%xmm3 - pxor %xmm3,%xmm2 + pxor %xmm3,%xmm2 // x^1 = x psrlq $1,%xmm3 - pxor %xmm3,%xmm2 + pxor %xmm3,%xmm2 // x^(1+1) = x^2 psrlq $5,%xmm3 - pxor %xmm3,%xmm2 + pxor %xmm3,%xmm2 // x^(1+1+5) = x^7 pxor %xmm3,%xmm3 movdqa %xmm2,%xmm0 - +// Rewind %rsi for the next iteration. leaq -256(%rsi),%rsi - +// Advance input and continue. leaq 16(%rdx),%rdx subq $16,%rcx jnz L$oop_ghash - +// Reverse bytes and store the result. pshufb %xmm10,%xmm0 movdqu %xmm0,(%rdi) - +// Zero any registers which contain secrets. pxor %xmm0,%xmm0 pxor %xmm1,%xmm1 pxor %xmm2,%xmm2 @@ -411,12 +412,14 @@ L$oop_row_6: .section __DATA,__const -.p2align 4 - +.p2align 4 +ghash_ssse3_constants: +// .Lreverse_bytes is a permutation which, if applied with pshufb, reverses the +// bytes in an XMM register. L$reverse_bytes: .byte 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 - +// .Llow4_mask is an XMM mask which selects the low four bits of each byte. L$low4_mask: .quad 0x0f0f0f0f0f0f0f0f, 0x0f0f0f0f0f0f0f0f .text diff --git a/third_party/boringssl/gen/bcm/ghash-ssse3-x86_64-linux.S b/third_party/boringssl/gen/bcm/ghash-ssse3-x86_64-linux.S index edce38d02..52bae0d85 100644 --- a/third_party/boringssl/gen/bcm/ghash-ssse3-x86_64-linux.S +++ b/third_party/boringssl/gen/bcm/ghash-ssse3-x86_64-linux.S @@ -6,10 +6,10 @@ #if !defined(OPENSSL_NO_ASM) && defined(OPENSSL_X86_64) && defined(__ELF__) .text - - - - +// gcm_gmult_ssse3 multiplies |Xi| by |Htable| and writes the result to |Xi|. +// |Xi| is represented in GHASH's serialized byte representation. |Htable| is +// formatted as described above. +// void gcm_gmult_ssse3(uint64_t Xi[2], const u128 Htable[16]); .type gcm_gmult_ssse3, @function .globl gcm_gmult_ssse3 .hidden gcm_gmult_ssse3 @@ -22,18 +22,18 @@ _CET_ENDBR movdqa .Lreverse_bytes(%rip),%xmm10 movdqa .Llow4_mask(%rip),%xmm2 - +// Reverse input bytes to deserialize. pshufb %xmm10,%xmm0 - +// Split each byte into low (%xmm0) and high (%xmm1) halves. movdqa %xmm2,%xmm1 pandn %xmm0,%xmm1 psrld $4,%xmm1 pand %xmm2,%xmm0 - - - +// Maintain the result in %xmm2 (the value) and %xmm3 (carry bits). Note +// that, due to bit reversal, %xmm3 contains bits that fall off when +// right-shifting, not left-shifting. pxor %xmm2,%xmm2 pxor %xmm3,%xmm3 movq $5,%rax @@ -41,31 +41,31 @@ _CET_ENDBR movdqu (%rsi),%xmm4 leaq 16(%rsi),%rsi - +// Right-shift %xmm2 and %xmm3 by 8 bytes. movdqa %xmm2,%xmm6 palignr $1,%xmm3,%xmm6 movdqa %xmm6,%xmm3 psrldq $1,%xmm2 - - - +// Load the next table row and index the low and high bits of the input. +// Note the low (respectively, high) half corresponds to more +// (respectively, less) significant coefficients. movdqa %xmm4,%xmm5 pshufb %xmm0,%xmm4 pshufb %xmm1,%xmm5 - +// Add the high half (%xmm5) without shifting. pxor %xmm5,%xmm2 - - +// Add the low half (%xmm4). This must be right-shifted by 4 bits. First, +// add into the carry register (%xmm3). movdqa %xmm4,%xmm5 psllq $60,%xmm5 movdqa %xmm5,%xmm6 pslldq $8,%xmm6 pxor %xmm6,%xmm3 - +// Next, add into %xmm2. psrldq $8,%xmm5 pxor %xmm5,%xmm2 psrlq $4,%xmm4 @@ -74,46 +74,46 @@ _CET_ENDBR subq $1,%rax jnz .Loop_row_1 - - - pxor %xmm3,%xmm2 +// Reduce the carry register. The reduction polynomial is 1 + x + x^2 + +// x^7, so we shift and XOR four times. + pxor %xmm3,%xmm2 // x^0 = 0 psrlq $1,%xmm3 - pxor %xmm3,%xmm2 + pxor %xmm3,%xmm2 // x^1 = x psrlq $1,%xmm3 - pxor %xmm3,%xmm2 + pxor %xmm3,%xmm2 // x^(1+1) = x^2 psrlq $5,%xmm3 - pxor %xmm3,%xmm2 + pxor %xmm3,%xmm2 // x^(1+1+5) = x^7 pxor %xmm3,%xmm3 movq $5,%rax .Loop_row_2: movdqu (%rsi),%xmm4 leaq 16(%rsi),%rsi - +// Right-shift %xmm2 and %xmm3 by 8 bytes. movdqa %xmm2,%xmm6 palignr $1,%xmm3,%xmm6 movdqa %xmm6,%xmm3 psrldq $1,%xmm2 - - - +// Load the next table row and index the low and high bits of the input. +// Note the low (respectively, high) half corresponds to more +// (respectively, less) significant coefficients. movdqa %xmm4,%xmm5 pshufb %xmm0,%xmm4 pshufb %xmm1,%xmm5 - +// Add the high half (%xmm5) without shifting. pxor %xmm5,%xmm2 - - +// Add the low half (%xmm4). This must be right-shifted by 4 bits. First, +// add into the carry register (%xmm3). movdqa %xmm4,%xmm5 psllq $60,%xmm5 movdqa %xmm5,%xmm6 pslldq $8,%xmm6 pxor %xmm6,%xmm3 - +// Next, add into %xmm2. psrldq $8,%xmm5 pxor %xmm5,%xmm2 psrlq $4,%xmm4 @@ -122,46 +122,46 @@ _CET_ENDBR subq $1,%rax jnz .Loop_row_2 - - - pxor %xmm3,%xmm2 +// Reduce the carry register. The reduction polynomial is 1 + x + x^2 + +// x^7, so we shift and XOR four times. + pxor %xmm3,%xmm2 // x^0 = 0 psrlq $1,%xmm3 - pxor %xmm3,%xmm2 + pxor %xmm3,%xmm2 // x^1 = x psrlq $1,%xmm3 - pxor %xmm3,%xmm2 + pxor %xmm3,%xmm2 // x^(1+1) = x^2 psrlq $5,%xmm3 - pxor %xmm3,%xmm2 + pxor %xmm3,%xmm2 // x^(1+1+5) = x^7 pxor %xmm3,%xmm3 movq $6,%rax .Loop_row_3: movdqu (%rsi),%xmm4 leaq 16(%rsi),%rsi - +// Right-shift %xmm2 and %xmm3 by 8 bytes. movdqa %xmm2,%xmm6 palignr $1,%xmm3,%xmm6 movdqa %xmm6,%xmm3 psrldq $1,%xmm2 - - - +// Load the next table row and index the low and high bits of the input. +// Note the low (respectively, high) half corresponds to more +// (respectively, less) significant coefficients. movdqa %xmm4,%xmm5 pshufb %xmm0,%xmm4 pshufb %xmm1,%xmm5 - +// Add the high half (%xmm5) without shifting. pxor %xmm5,%xmm2 - - +// Add the low half (%xmm4). This must be right-shifted by 4 bits. First, +// add into the carry register (%xmm3). movdqa %xmm4,%xmm5 psllq $60,%xmm5 movdqa %xmm5,%xmm6 pslldq $8,%xmm6 pxor %xmm6,%xmm3 - +// Next, add into %xmm2. psrldq $8,%xmm5 pxor %xmm5,%xmm2 psrlq $4,%xmm4 @@ -170,21 +170,21 @@ _CET_ENDBR subq $1,%rax jnz .Loop_row_3 - - - pxor %xmm3,%xmm2 +// Reduce the carry register. The reduction polynomial is 1 + x + x^2 + +// x^7, so we shift and XOR four times. + pxor %xmm3,%xmm2 // x^0 = 0 psrlq $1,%xmm3 - pxor %xmm3,%xmm2 + pxor %xmm3,%xmm2 // x^1 = x psrlq $1,%xmm3 - pxor %xmm3,%xmm2 + pxor %xmm3,%xmm2 // x^(1+1) = x^2 psrlq $5,%xmm3 - pxor %xmm3,%xmm2 + pxor %xmm3,%xmm2 // x^(1+1+5) = x^7 pxor %xmm3,%xmm3 - +// Store the result. Reverse bytes to serialize. pshufb %xmm10,%xmm2 movdqu %xmm2,(%rdi) - +// Zero any registers which contain secrets. pxor %xmm0,%xmm0 pxor %xmm1,%xmm1 pxor %xmm2,%xmm2 @@ -196,11 +196,11 @@ _CET_ENDBR .cfi_endproc .size gcm_gmult_ssse3,.-gcm_gmult_ssse3 - - - - - +// gcm_ghash_ssse3 incorporates |len| bytes from |in| to |Xi|, using |Htable| as +// the key. It writes the result back to |Xi|. |Xi| is represented in GHASH's +// serialized byte representation. |Htable| is formatted as described above. +// void gcm_ghash_ssse3(uint64_t Xi[2], const u128 Htable[16], const uint8_t *in, +// size_t len); .type gcm_ghash_ssse3, @function .globl gcm_ghash_ssse3 .hidden gcm_ghash_ssse3 @@ -213,62 +213,62 @@ _CET_ENDBR movdqa .Lreverse_bytes(%rip),%xmm10 movdqa .Llow4_mask(%rip),%xmm11 - +// This function only processes whole blocks. andq $-16,%rcx - - +// Reverse input bytes to deserialize. We maintain the running +// total in %xmm0. pshufb %xmm10,%xmm0 - +// Iterate over each block. On entry to each iteration, %xmm3 is zero. pxor %xmm3,%xmm3 .Loop_ghash: - +// Incorporate the next block of input. movdqu (%rdx),%xmm1 - pshufb %xmm10,%xmm1 + pshufb %xmm10,%xmm1 // Reverse bytes. pxor %xmm1,%xmm0 - +// Split each byte into low (%xmm0) and high (%xmm1) halves. movdqa %xmm11,%xmm1 pandn %xmm0,%xmm1 psrld $4,%xmm1 pand %xmm11,%xmm0 - - - +// Maintain the result in %xmm2 (the value) and %xmm3 (carry bits). Note +// that, due to bit reversal, %xmm3 contains bits that fall off when +// right-shifting, not left-shifting. pxor %xmm2,%xmm2 - +// %xmm3 is already zero at this point. movq $5,%rax .Loop_row_4: movdqu (%rsi),%xmm4 leaq 16(%rsi),%rsi - +// Right-shift %xmm2 and %xmm3 by 8 bytes. movdqa %xmm2,%xmm6 palignr $1,%xmm3,%xmm6 movdqa %xmm6,%xmm3 psrldq $1,%xmm2 - - - +// Load the next table row and index the low and high bits of the input. +// Note the low (respectively, high) half corresponds to more +// (respectively, less) significant coefficients. movdqa %xmm4,%xmm5 pshufb %xmm0,%xmm4 pshufb %xmm1,%xmm5 - +// Add the high half (%xmm5) without shifting. pxor %xmm5,%xmm2 - - +// Add the low half (%xmm4). This must be right-shifted by 4 bits. First, +// add into the carry register (%xmm3). movdqa %xmm4,%xmm5 psllq $60,%xmm5 movdqa %xmm5,%xmm6 pslldq $8,%xmm6 pxor %xmm6,%xmm3 - +// Next, add into %xmm2. psrldq $8,%xmm5 pxor %xmm5,%xmm2 psrlq $4,%xmm4 @@ -277,46 +277,46 @@ _CET_ENDBR subq $1,%rax jnz .Loop_row_4 - - - pxor %xmm3,%xmm2 +// Reduce the carry register. The reduction polynomial is 1 + x + x^2 + +// x^7, so we shift and XOR four times. + pxor %xmm3,%xmm2 // x^0 = 0 psrlq $1,%xmm3 - pxor %xmm3,%xmm2 + pxor %xmm3,%xmm2 // x^1 = x psrlq $1,%xmm3 - pxor %xmm3,%xmm2 + pxor %xmm3,%xmm2 // x^(1+1) = x^2 psrlq $5,%xmm3 - pxor %xmm3,%xmm2 + pxor %xmm3,%xmm2 // x^(1+1+5) = x^7 pxor %xmm3,%xmm3 movq $5,%rax .Loop_row_5: movdqu (%rsi),%xmm4 leaq 16(%rsi),%rsi - +// Right-shift %xmm2 and %xmm3 by 8 bytes. movdqa %xmm2,%xmm6 palignr $1,%xmm3,%xmm6 movdqa %xmm6,%xmm3 psrldq $1,%xmm2 - - - +// Load the next table row and index the low and high bits of the input. +// Note the low (respectively, high) half corresponds to more +// (respectively, less) significant coefficients. movdqa %xmm4,%xmm5 pshufb %xmm0,%xmm4 pshufb %xmm1,%xmm5 - +// Add the high half (%xmm5) without shifting. pxor %xmm5,%xmm2 - - +// Add the low half (%xmm4). This must be right-shifted by 4 bits. First, +// add into the carry register (%xmm3). movdqa %xmm4,%xmm5 psllq $60,%xmm5 movdqa %xmm5,%xmm6 pslldq $8,%xmm6 pxor %xmm6,%xmm3 - +// Next, add into %xmm2. psrldq $8,%xmm5 pxor %xmm5,%xmm2 psrlq $4,%xmm4 @@ -325,46 +325,46 @@ _CET_ENDBR subq $1,%rax jnz .Loop_row_5 - - - pxor %xmm3,%xmm2 +// Reduce the carry register. The reduction polynomial is 1 + x + x^2 + +// x^7, so we shift and XOR four times. + pxor %xmm3,%xmm2 // x^0 = 0 psrlq $1,%xmm3 - pxor %xmm3,%xmm2 + pxor %xmm3,%xmm2 // x^1 = x psrlq $1,%xmm3 - pxor %xmm3,%xmm2 + pxor %xmm3,%xmm2 // x^(1+1) = x^2 psrlq $5,%xmm3 - pxor %xmm3,%xmm2 + pxor %xmm3,%xmm2 // x^(1+1+5) = x^7 pxor %xmm3,%xmm3 movq $6,%rax .Loop_row_6: movdqu (%rsi),%xmm4 leaq 16(%rsi),%rsi - +// Right-shift %xmm2 and %xmm3 by 8 bytes. movdqa %xmm2,%xmm6 palignr $1,%xmm3,%xmm6 movdqa %xmm6,%xmm3 psrldq $1,%xmm2 - - - +// Load the next table row and index the low and high bits of the input. +// Note the low (respectively, high) half corresponds to more +// (respectively, less) significant coefficients. movdqa %xmm4,%xmm5 pshufb %xmm0,%xmm4 pshufb %xmm1,%xmm5 - +// Add the high half (%xmm5) without shifting. pxor %xmm5,%xmm2 - - +// Add the low half (%xmm4). This must be right-shifted by 4 bits. First, +// add into the carry register (%xmm3). movdqa %xmm4,%xmm5 psllq $60,%xmm5 movdqa %xmm5,%xmm6 pslldq $8,%xmm6 pxor %xmm6,%xmm3 - +// Next, add into %xmm2. psrldq $8,%xmm5 pxor %xmm5,%xmm2 psrlq $4,%xmm4 @@ -373,31 +373,31 @@ _CET_ENDBR subq $1,%rax jnz .Loop_row_6 - - - pxor %xmm3,%xmm2 +// Reduce the carry register. The reduction polynomial is 1 + x + x^2 + +// x^7, so we shift and XOR four times. + pxor %xmm3,%xmm2 // x^0 = 0 psrlq $1,%xmm3 - pxor %xmm3,%xmm2 + pxor %xmm3,%xmm2 // x^1 = x psrlq $1,%xmm3 - pxor %xmm3,%xmm2 + pxor %xmm3,%xmm2 // x^(1+1) = x^2 psrlq $5,%xmm3 - pxor %xmm3,%xmm2 + pxor %xmm3,%xmm2 // x^(1+1+5) = x^7 pxor %xmm3,%xmm3 movdqa %xmm2,%xmm0 - +// Rewind %rsi for the next iteration. leaq -256(%rsi),%rsi - +// Advance input and continue. leaq 16(%rdx),%rdx subq $16,%rcx jnz .Loop_ghash - +// Reverse bytes and store the result. pshufb %xmm10,%xmm0 movdqu %xmm0,(%rdi) - +// Zero any registers which contain secrets. pxor %xmm0,%xmm0 pxor %xmm1,%xmm1 pxor %xmm2,%xmm2 @@ -411,12 +411,14 @@ _CET_ENDBR .size gcm_ghash_ssse3,.-gcm_ghash_ssse3 .section .rodata -.align 16 - +.align 16 +ghash_ssse3_constants: +// .Lreverse_bytes is a permutation which, if applied with pshufb, reverses the +// bytes in an XMM register. .Lreverse_bytes: .byte 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 - +// .Llow4_mask is an XMM mask which selects the low four bits of each byte. .Llow4_mask: .quad 0x0f0f0f0f0f0f0f0f, 0x0f0f0f0f0f0f0f0f .text diff --git a/third_party/boringssl/gen/bcm/ghash-ssse3-x86_64-win.asm b/third_party/boringssl/gen/bcm/ghash-ssse3-x86_64-win.asm index 5f78da861..64faab646 100644 --- a/third_party/boringssl/gen/bcm/ghash-ssse3-x86_64-win.asm +++ b/third_party/boringssl/gen/bcm/ghash-ssse3-x86_64-win.asm @@ -14,10 +14,10 @@ default rel section .text code align=64 - - - - +; gcm_gmult_ssse3 multiplies |Xi| by |Htable| and writes the result to |Xi|. +; |Xi| is represented in GHASH's serialized byte representation. |Htable| is +; formatted as described above. +; void gcm_gmult_ssse3(uint64_t Xi[2], const u128 Htable[16]); global gcm_gmult_ssse3 ALIGN 16 @@ -36,18 +36,18 @@ $L$SEH_endprologue_gcm_gmult_ssse3_5: movdqa xmm10,XMMWORD[$L$reverse_bytes] movdqa xmm2,XMMWORD[$L$low4_mask] - +; Reverse input bytes to deserialize. pshufb xmm0,xmm10 - +; Split each byte into low (%xmm0) and high (%xmm1) halves. movdqa xmm1,xmm2 pandn xmm1,xmm0 psrld xmm1,4 pand xmm0,xmm2 - - - +; Maintain the result in %xmm2 (the value) and %xmm3 (carry bits). Note +; that, due to bit reversal, %xmm3 contains bits that fall off when +; right-shifting, not left-shifting. pxor xmm2,xmm2 pxor xmm3,xmm3 mov rax,5 @@ -55,31 +55,31 @@ $L$oop_row_1: movdqu xmm4,XMMWORD[rdx] lea rdx,[16+rdx] - +; Right-shift %xmm2 and %xmm3 by 8 bytes. movdqa xmm6,xmm2 palignr xmm6,xmm3,1 movdqa xmm3,xmm6 psrldq xmm2,1 - - - +; Load the next table row and index the low and high bits of the input. +; Note the low (respectively, high) half corresponds to more +; (respectively, less) significant coefficients. movdqa xmm5,xmm4 pshufb xmm4,xmm0 pshufb xmm5,xmm1 - +; Add the high half (%xmm5) without shifting. pxor xmm2,xmm5 - - +; Add the low half (%xmm4). This must be right-shifted by 4 bits. First, +; add into the carry register (%xmm3). movdqa xmm5,xmm4 psllq xmm5,60 movdqa xmm6,xmm5 pslldq xmm6,8 pxor xmm3,xmm6 - +; Next, add into %xmm2. psrldq xmm5,8 pxor xmm2,xmm5 psrlq xmm4,4 @@ -88,46 +88,46 @@ $L$oop_row_1: sub rax,1 jnz NEAR $L$oop_row_1 - - - pxor xmm2,xmm3 +; Reduce the carry register. The reduction polynomial is 1 + x + x^2 + +; x^7, so we shift and XOR four times. + pxor xmm2,xmm3 ; x^0 = 0 psrlq xmm3,1 - pxor xmm2,xmm3 + pxor xmm2,xmm3 ; x^1 = x psrlq xmm3,1 - pxor xmm2,xmm3 + pxor xmm2,xmm3 ; x^(1+1) = x^2 psrlq xmm3,5 - pxor xmm2,xmm3 + pxor xmm2,xmm3 ; x^(1+1+5) = x^7 pxor xmm3,xmm3 mov rax,5 $L$oop_row_2: movdqu xmm4,XMMWORD[rdx] lea rdx,[16+rdx] - +; Right-shift %xmm2 and %xmm3 by 8 bytes. movdqa xmm6,xmm2 palignr xmm6,xmm3,1 movdqa xmm3,xmm6 psrldq xmm2,1 - - - +; Load the next table row and index the low and high bits of the input. +; Note the low (respectively, high) half corresponds to more +; (respectively, less) significant coefficients. movdqa xmm5,xmm4 pshufb xmm4,xmm0 pshufb xmm5,xmm1 - +; Add the high half (%xmm5) without shifting. pxor xmm2,xmm5 - - +; Add the low half (%xmm4). This must be right-shifted by 4 bits. First, +; add into the carry register (%xmm3). movdqa xmm5,xmm4 psllq xmm5,60 movdqa xmm6,xmm5 pslldq xmm6,8 pxor xmm3,xmm6 - +; Next, add into %xmm2. psrldq xmm5,8 pxor xmm2,xmm5 psrlq xmm4,4 @@ -136,46 +136,46 @@ $L$oop_row_2: sub rax,1 jnz NEAR $L$oop_row_2 - - - pxor xmm2,xmm3 +; Reduce the carry register. The reduction polynomial is 1 + x + x^2 + +; x^7, so we shift and XOR four times. + pxor xmm2,xmm3 ; x^0 = 0 psrlq xmm3,1 - pxor xmm2,xmm3 + pxor xmm2,xmm3 ; x^1 = x psrlq xmm3,1 - pxor xmm2,xmm3 + pxor xmm2,xmm3 ; x^(1+1) = x^2 psrlq xmm3,5 - pxor xmm2,xmm3 + pxor xmm2,xmm3 ; x^(1+1+5) = x^7 pxor xmm3,xmm3 mov rax,6 $L$oop_row_3: movdqu xmm4,XMMWORD[rdx] lea rdx,[16+rdx] - +; Right-shift %xmm2 and %xmm3 by 8 bytes. movdqa xmm6,xmm2 palignr xmm6,xmm3,1 movdqa xmm3,xmm6 psrldq xmm2,1 - - - +; Load the next table row and index the low and high bits of the input. +; Note the low (respectively, high) half corresponds to more +; (respectively, less) significant coefficients. movdqa xmm5,xmm4 pshufb xmm4,xmm0 pshufb xmm5,xmm1 - +; Add the high half (%xmm5) without shifting. pxor xmm2,xmm5 - - +; Add the low half (%xmm4). This must be right-shifted by 4 bits. First, +; add into the carry register (%xmm3). movdqa xmm5,xmm4 psllq xmm5,60 movdqa xmm6,xmm5 pslldq xmm6,8 pxor xmm3,xmm6 - +; Next, add into %xmm2. psrldq xmm5,8 pxor xmm2,xmm5 psrlq xmm4,4 @@ -184,21 +184,21 @@ $L$oop_row_3: sub rax,1 jnz NEAR $L$oop_row_3 - - - pxor xmm2,xmm3 +; Reduce the carry register. The reduction polynomial is 1 + x + x^2 + +; x^7, so we shift and XOR four times. + pxor xmm2,xmm3 ; x^0 = 0 psrlq xmm3,1 - pxor xmm2,xmm3 + pxor xmm2,xmm3 ; x^1 = x psrlq xmm3,1 - pxor xmm2,xmm3 + pxor xmm2,xmm3 ; x^(1+1) = x^2 psrlq xmm3,5 - pxor xmm2,xmm3 + pxor xmm2,xmm3 ; x^(1+1+5) = x^7 pxor xmm3,xmm3 - +; Store the result. Reverse bytes to serialize. pshufb xmm2,xmm10 movdqu XMMWORD[rcx],xmm2 - +; Zero any registers which contain secrets. pxor xmm0,xmm0 pxor xmm1,xmm1 pxor xmm2,xmm2 @@ -213,11 +213,11 @@ $L$oop_row_3: $L$SEH_end_gcm_gmult_ssse3_6: - - - - - +; gcm_ghash_ssse3 incorporates |len| bytes from |in| to |Xi|, using |Htable| as +; the key. It writes the result back to |Xi|. |Xi| is represented in GHASH's +; serialized byte representation. |Htable| is formatted as described above. +; void gcm_ghash_ssse3(uint64_t Xi[2], const u128 Htable[16], const uint8_t *in, +; size_t len); global gcm_ghash_ssse3 ALIGN 16 @@ -238,62 +238,62 @@ $L$SEH_endprologue_gcm_ghash_ssse3_6: movdqa xmm10,XMMWORD[$L$reverse_bytes] movdqa xmm11,XMMWORD[$L$low4_mask] - +; This function only processes whole blocks. and r9,-16 - - +; Reverse input bytes to deserialize. We maintain the running +; total in %xmm0. pshufb xmm0,xmm10 - +; Iterate over each block. On entry to each iteration, %xmm3 is zero. pxor xmm3,xmm3 $L$oop_ghash: - +; Incorporate the next block of input. movdqu xmm1,XMMWORD[r8] - pshufb xmm1,xmm10 + pshufb xmm1,xmm10 ; Reverse bytes. pxor xmm0,xmm1 - +; Split each byte into low (%xmm0) and high (%xmm1) halves. movdqa xmm1,xmm11 pandn xmm1,xmm0 psrld xmm1,4 pand xmm0,xmm11 - - - +; Maintain the result in %xmm2 (the value) and %xmm3 (carry bits). Note +; that, due to bit reversal, %xmm3 contains bits that fall off when +; right-shifting, not left-shifting. pxor xmm2,xmm2 - +; %xmm3 is already zero at this point. mov rax,5 $L$oop_row_4: movdqu xmm4,XMMWORD[rdx] lea rdx,[16+rdx] - +; Right-shift %xmm2 and %xmm3 by 8 bytes. movdqa xmm6,xmm2 palignr xmm6,xmm3,1 movdqa xmm3,xmm6 psrldq xmm2,1 - - - +; Load the next table row and index the low and high bits of the input. +; Note the low (respectively, high) half corresponds to more +; (respectively, less) significant coefficients. movdqa xmm5,xmm4 pshufb xmm4,xmm0 pshufb xmm5,xmm1 - +; Add the high half (%xmm5) without shifting. pxor xmm2,xmm5 - - +; Add the low half (%xmm4). This must be right-shifted by 4 bits. First, +; add into the carry register (%xmm3). movdqa xmm5,xmm4 psllq xmm5,60 movdqa xmm6,xmm5 pslldq xmm6,8 pxor xmm3,xmm6 - +; Next, add into %xmm2. psrldq xmm5,8 pxor xmm2,xmm5 psrlq xmm4,4 @@ -302,46 +302,46 @@ $L$oop_row_4: sub rax,1 jnz NEAR $L$oop_row_4 - - - pxor xmm2,xmm3 +; Reduce the carry register. The reduction polynomial is 1 + x + x^2 + +; x^7, so we shift and XOR four times. + pxor xmm2,xmm3 ; x^0 = 0 psrlq xmm3,1 - pxor xmm2,xmm3 + pxor xmm2,xmm3 ; x^1 = x psrlq xmm3,1 - pxor xmm2,xmm3 + pxor xmm2,xmm3 ; x^(1+1) = x^2 psrlq xmm3,5 - pxor xmm2,xmm3 + pxor xmm2,xmm3 ; x^(1+1+5) = x^7 pxor xmm3,xmm3 mov rax,5 $L$oop_row_5: movdqu xmm4,XMMWORD[rdx] lea rdx,[16+rdx] - +; Right-shift %xmm2 and %xmm3 by 8 bytes. movdqa xmm6,xmm2 palignr xmm6,xmm3,1 movdqa xmm3,xmm6 psrldq xmm2,1 - - - +; Load the next table row and index the low and high bits of the input. +; Note the low (respectively, high) half corresponds to more +; (respectively, less) significant coefficients. movdqa xmm5,xmm4 pshufb xmm4,xmm0 pshufb xmm5,xmm1 - +; Add the high half (%xmm5) without shifting. pxor xmm2,xmm5 - - +; Add the low half (%xmm4). This must be right-shifted by 4 bits. First, +; add into the carry register (%xmm3). movdqa xmm5,xmm4 psllq xmm5,60 movdqa xmm6,xmm5 pslldq xmm6,8 pxor xmm3,xmm6 - +; Next, add into %xmm2. psrldq xmm5,8 pxor xmm2,xmm5 psrlq xmm4,4 @@ -350,46 +350,46 @@ $L$oop_row_5: sub rax,1 jnz NEAR $L$oop_row_5 - - - pxor xmm2,xmm3 +; Reduce the carry register. The reduction polynomial is 1 + x + x^2 + +; x^7, so we shift and XOR four times. + pxor xmm2,xmm3 ; x^0 = 0 psrlq xmm3,1 - pxor xmm2,xmm3 + pxor xmm2,xmm3 ; x^1 = x psrlq xmm3,1 - pxor xmm2,xmm3 + pxor xmm2,xmm3 ; x^(1+1) = x^2 psrlq xmm3,5 - pxor xmm2,xmm3 + pxor xmm2,xmm3 ; x^(1+1+5) = x^7 pxor xmm3,xmm3 mov rax,6 $L$oop_row_6: movdqu xmm4,XMMWORD[rdx] lea rdx,[16+rdx] - +; Right-shift %xmm2 and %xmm3 by 8 bytes. movdqa xmm6,xmm2 palignr xmm6,xmm3,1 movdqa xmm3,xmm6 psrldq xmm2,1 - - - +; Load the next table row and index the low and high bits of the input. +; Note the low (respectively, high) half corresponds to more +; (respectively, less) significant coefficients. movdqa xmm5,xmm4 pshufb xmm4,xmm0 pshufb xmm5,xmm1 - +; Add the high half (%xmm5) without shifting. pxor xmm2,xmm5 - - +; Add the low half (%xmm4). This must be right-shifted by 4 bits. First, +; add into the carry register (%xmm3). movdqa xmm5,xmm4 psllq xmm5,60 movdqa xmm6,xmm5 pslldq xmm6,8 pxor xmm3,xmm6 - +; Next, add into %xmm2. psrldq xmm5,8 pxor xmm2,xmm5 psrlq xmm4,4 @@ -398,31 +398,31 @@ $L$oop_row_6: sub rax,1 jnz NEAR $L$oop_row_6 - - - pxor xmm2,xmm3 +; Reduce the carry register. The reduction polynomial is 1 + x + x^2 + +; x^7, so we shift and XOR four times. + pxor xmm2,xmm3 ; x^0 = 0 psrlq xmm3,1 - pxor xmm2,xmm3 + pxor xmm2,xmm3 ; x^1 = x psrlq xmm3,1 - pxor xmm2,xmm3 + pxor xmm2,xmm3 ; x^(1+1) = x^2 psrlq xmm3,5 - pxor xmm2,xmm3 + pxor xmm2,xmm3 ; x^(1+1+5) = x^7 pxor xmm3,xmm3 movdqa xmm0,xmm2 - +; Rewind %rdx for the next iteration. lea rdx,[((-256))+rdx] - +; Advance input and continue. lea r8,[16+r8] sub r9,16 jnz NEAR $L$oop_ghash - +; Reverse bytes and store the result. pshufb xmm0,xmm10 movdqu XMMWORD[rcx],xmm0 - +; Zero any registers which contain secrets. pxor xmm0,xmm0 pxor xmm1,xmm1 pxor xmm2,xmm2 @@ -440,15 +440,17 @@ $L$SEH_end_gcm_ghash_ssse3_7: section .rdata rdata align=8 -ALIGN 16 - +ALIGN 16 +ghash_ssse3_constants: +; .Lreverse_bytes is a permutation which, if applied with pshufb, reverses the +; bytes in an XMM register. $L$reverse_bytes: DB 15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0 - +; .Llow4_mask is an XMM mask which selects the low four bits of each byte. $L$low4_mask: DQ 0x0f0f0f0f0f0f0f0f,0x0f0f0f0f0f0f0f0f -section .text +section .text code align=64 section .pdata rdata align=4 ALIGN 4 @@ -464,7 +466,7 @@ ALIGN 4 section .xdata rdata align=8 ALIGN 4 $L$SEH_info_gcm_gmult_ssse3_0: - DB 1 + DB 1 ; version 1, no flags DB $L$SEH_endprologue_gcm_gmult_ssse3_5-$L$SEH_begin_gcm_gmult_ssse3_1 DB 5 DB 0 @@ -479,7 +481,7 @@ $L$SEH_info_gcm_gmult_ssse3_0: DW 0 $L$SEH_info_gcm_ghash_ssse3_0: - DB 1 + DB 1 ; version 1, no flags DB $L$SEH_endprologue_gcm_ghash_ssse3_6-$L$SEH_begin_gcm_ghash_ssse3_1 DB 7 DB 0 diff --git a/third_party/boringssl/gen/bcm/ghash-x86_64-apple.S b/third_party/boringssl/gen/bcm/ghash-x86_64-apple.S index 0cf60d1bc..0cfc20d82 100644 --- a/third_party/boringssl/gen/bcm/ghash-x86_64-apple.S +++ b/third_party/boringssl/gen/bcm/ghash-x86_64-apple.S @@ -15,154 +15,154 @@ _gcm_init_clmul: _CET_ENDBR L$_init_clmul: movdqu (%rsi),%xmm2 - pshufd $78,%xmm2,%xmm2 + pshufd $78,%xmm2,%xmm2 // dword swap - - pshufd $255,%xmm2,%xmm4 +// <<1 twist + pshufd $255,%xmm2,%xmm4 // broadcast uppermost dword movdqa %xmm2,%xmm3 psllq $1,%xmm2 - pxor %xmm5,%xmm5 + pxor %xmm5,%xmm5 // psrlq $63,%xmm3 - pcmpgtd %xmm4,%xmm5 + pcmpgtd %xmm4,%xmm5 // broadcast carry bit pslldq $8,%xmm3 - por %xmm3,%xmm2 - + por %xmm3,%xmm2 // H<<=1 +// magic reduction pand L$0x1c2_polynomial(%rip),%xmm5 - pxor %xmm5,%xmm2 - + pxor %xmm5,%xmm2 // if(carry) H^=0x1c2_polynomial +// calculate H^2 pshufd $78,%xmm2,%xmm6 movdqa %xmm2,%xmm0 pxor %xmm2,%xmm6 - movdqa %xmm0,%xmm1 + movdqa %xmm0,%xmm1 // pshufd $78,%xmm0,%xmm3 - pxor %xmm0,%xmm3 - pclmulqdq $0x00,%xmm2,%xmm0 - pclmulqdq $0x11,%xmm2,%xmm1 - pclmulqdq $0x00,%xmm6,%xmm3 - pxor %xmm0,%xmm3 - pxor %xmm1,%xmm3 - - movdqa %xmm3,%xmm4 + pxor %xmm0,%xmm3 // + pclmulqdq $0x00,%xmm2,%xmm0 // ###### + pclmulqdq $0x11,%xmm2,%xmm1 // ###### + pclmulqdq $0x00,%xmm6,%xmm3 // ###### + pxor %xmm0,%xmm3 // + pxor %xmm1,%xmm3 // + + movdqa %xmm3,%xmm4 // psrldq $8,%xmm3 - pslldq $8,%xmm4 + pslldq $8,%xmm4 // pxor %xmm3,%xmm1 - pxor %xmm4,%xmm0 - - movdqa %xmm0,%xmm4 + pxor %xmm4,%xmm0 // +// 1st phase + movdqa %xmm0,%xmm4 // movdqa %xmm0,%xmm3 psllq $5,%xmm0 - pxor %xmm0,%xmm3 + pxor %xmm0,%xmm3 // psllq $1,%xmm0 - pxor %xmm3,%xmm0 - psllq $57,%xmm0 - movdqa %xmm0,%xmm3 + pxor %xmm3,%xmm0 // + psllq $57,%xmm0 // + movdqa %xmm0,%xmm3 // pslldq $8,%xmm0 - psrldq $8,%xmm3 + psrldq $8,%xmm3 // pxor %xmm4,%xmm0 - pxor %xmm3,%xmm1 - + pxor %xmm3,%xmm1 // +// 2nd phase movdqa %xmm0,%xmm4 psrlq $1,%xmm0 - pxor %xmm4,%xmm1 + pxor %xmm4,%xmm1 // pxor %xmm0,%xmm4 psrlq $5,%xmm0 - pxor %xmm4,%xmm0 - psrlq $1,%xmm0 - pxor %xmm1,%xmm0 + pxor %xmm4,%xmm0 // + psrlq $1,%xmm0 // + pxor %xmm1,%xmm0 // pshufd $78,%xmm2,%xmm3 pshufd $78,%xmm0,%xmm4 - pxor %xmm2,%xmm3 - movdqu %xmm2,0(%rdi) - pxor %xmm0,%xmm4 - movdqu %xmm0,16(%rdi) - palignr $8,%xmm3,%xmm4 - movdqu %xmm4,32(%rdi) - movdqa %xmm0,%xmm1 + pxor %xmm2,%xmm3 // Karatsuba pre-processing + movdqu %xmm2,0(%rdi) // save H + pxor %xmm0,%xmm4 // Karatsuba pre-processing + movdqu %xmm0,16(%rdi) // save H^2 + palignr $8,%xmm3,%xmm4 // low part is H.lo^H.hi... + movdqu %xmm4,32(%rdi) // save Karatsuba "salt" + movdqa %xmm0,%xmm1 // pshufd $78,%xmm0,%xmm3 - pxor %xmm0,%xmm3 - pclmulqdq $0x00,%xmm2,%xmm0 - pclmulqdq $0x11,%xmm2,%xmm1 - pclmulqdq $0x00,%xmm6,%xmm3 - pxor %xmm0,%xmm3 - pxor %xmm1,%xmm3 - - movdqa %xmm3,%xmm4 + pxor %xmm0,%xmm3 // + pclmulqdq $0x00,%xmm2,%xmm0 // ###### + pclmulqdq $0x11,%xmm2,%xmm1 // ###### + pclmulqdq $0x00,%xmm6,%xmm3 // ###### + pxor %xmm0,%xmm3 // + pxor %xmm1,%xmm3 // + + movdqa %xmm3,%xmm4 // psrldq $8,%xmm3 - pslldq $8,%xmm4 + pslldq $8,%xmm4 // pxor %xmm3,%xmm1 - pxor %xmm4,%xmm0 - - movdqa %xmm0,%xmm4 + pxor %xmm4,%xmm0 // +// 1st phase + movdqa %xmm0,%xmm4 // movdqa %xmm0,%xmm3 psllq $5,%xmm0 - pxor %xmm0,%xmm3 + pxor %xmm0,%xmm3 // psllq $1,%xmm0 - pxor %xmm3,%xmm0 - psllq $57,%xmm0 - movdqa %xmm0,%xmm3 + pxor %xmm3,%xmm0 // + psllq $57,%xmm0 // + movdqa %xmm0,%xmm3 // pslldq $8,%xmm0 - psrldq $8,%xmm3 + psrldq $8,%xmm3 // pxor %xmm4,%xmm0 - pxor %xmm3,%xmm1 - + pxor %xmm3,%xmm1 // +// 2nd phase movdqa %xmm0,%xmm4 psrlq $1,%xmm0 - pxor %xmm4,%xmm1 + pxor %xmm4,%xmm1 // pxor %xmm0,%xmm4 psrlq $5,%xmm0 - pxor %xmm4,%xmm0 - psrlq $1,%xmm0 - pxor %xmm1,%xmm0 + pxor %xmm4,%xmm0 // + psrlq $1,%xmm0 // + pxor %xmm1,%xmm0 // movdqa %xmm0,%xmm5 - movdqa %xmm0,%xmm1 + movdqa %xmm0,%xmm1 // pshufd $78,%xmm0,%xmm3 - pxor %xmm0,%xmm3 - pclmulqdq $0x00,%xmm2,%xmm0 - pclmulqdq $0x11,%xmm2,%xmm1 - pclmulqdq $0x00,%xmm6,%xmm3 - pxor %xmm0,%xmm3 - pxor %xmm1,%xmm3 - - movdqa %xmm3,%xmm4 + pxor %xmm0,%xmm3 // + pclmulqdq $0x00,%xmm2,%xmm0 // ###### + pclmulqdq $0x11,%xmm2,%xmm1 // ###### + pclmulqdq $0x00,%xmm6,%xmm3 // ###### + pxor %xmm0,%xmm3 // + pxor %xmm1,%xmm3 // + + movdqa %xmm3,%xmm4 // psrldq $8,%xmm3 - pslldq $8,%xmm4 + pslldq $8,%xmm4 // pxor %xmm3,%xmm1 - pxor %xmm4,%xmm0 - - movdqa %xmm0,%xmm4 + pxor %xmm4,%xmm0 // +// 1st phase + movdqa %xmm0,%xmm4 // movdqa %xmm0,%xmm3 psllq $5,%xmm0 - pxor %xmm0,%xmm3 + pxor %xmm0,%xmm3 // psllq $1,%xmm0 - pxor %xmm3,%xmm0 - psllq $57,%xmm0 - movdqa %xmm0,%xmm3 + pxor %xmm3,%xmm0 // + psllq $57,%xmm0 // + movdqa %xmm0,%xmm3 // pslldq $8,%xmm0 - psrldq $8,%xmm3 + psrldq $8,%xmm3 // pxor %xmm4,%xmm0 - pxor %xmm3,%xmm1 - + pxor %xmm3,%xmm1 // +// 2nd phase movdqa %xmm0,%xmm4 psrlq $1,%xmm0 - pxor %xmm4,%xmm1 + pxor %xmm4,%xmm1 // pxor %xmm0,%xmm4 psrlq $5,%xmm0 - pxor %xmm4,%xmm0 - psrlq $1,%xmm0 - pxor %xmm1,%xmm0 + pxor %xmm4,%xmm0 // + psrlq $1,%xmm0 // + pxor %xmm1,%xmm0 // pshufd $78,%xmm5,%xmm3 pshufd $78,%xmm0,%xmm4 - pxor %xmm5,%xmm3 - movdqu %xmm5,48(%rdi) - pxor %xmm0,%xmm4 - movdqu %xmm0,64(%rdi) - palignr $8,%xmm3,%xmm4 - movdqu %xmm4,80(%rdi) + pxor %xmm5,%xmm3 // Karatsuba pre-processing + movdqu %xmm5,48(%rdi) // save H^3 + pxor %xmm0,%xmm4 // Karatsuba pre-processing + movdqu %xmm0,64(%rdi) // save H^4 + palignr $8,%xmm3,%xmm4 // low part is H^3.lo^H^3.hi... + movdqu %xmm4,80(%rdi) // save Karatsuba "salt" ret @@ -171,6 +171,7 @@ L$_init_clmul: .private_extern _gcm_gmult_clmul .p2align 4 +.alt_entry _gcm_gmult_clmul _gcm_gmult_clmul: _CET_ENDBR @@ -180,43 +181,43 @@ L$_gmult_clmul: movdqu (%rsi),%xmm2 movdqu 32(%rsi),%xmm4 pshufb %xmm5,%xmm0 - movdqa %xmm0,%xmm1 + movdqa %xmm0,%xmm1 // pshufd $78,%xmm0,%xmm3 - pxor %xmm0,%xmm3 - pclmulqdq $0x00,%xmm2,%xmm0 - pclmulqdq $0x11,%xmm2,%xmm1 - pclmulqdq $0x00,%xmm4,%xmm3 - pxor %xmm0,%xmm3 - pxor %xmm1,%xmm3 - - movdqa %xmm3,%xmm4 + pxor %xmm0,%xmm3 // + pclmulqdq $0x00,%xmm2,%xmm0 // ###### + pclmulqdq $0x11,%xmm2,%xmm1 // ###### + pclmulqdq $0x00,%xmm4,%xmm3 // ###### + pxor %xmm0,%xmm3 // + pxor %xmm1,%xmm3 // + + movdqa %xmm3,%xmm4 // psrldq $8,%xmm3 - pslldq $8,%xmm4 + pslldq $8,%xmm4 // pxor %xmm3,%xmm1 - pxor %xmm4,%xmm0 - - movdqa %xmm0,%xmm4 + pxor %xmm4,%xmm0 // +// 1st phase + movdqa %xmm0,%xmm4 // movdqa %xmm0,%xmm3 psllq $5,%xmm0 - pxor %xmm0,%xmm3 + pxor %xmm0,%xmm3 // psllq $1,%xmm0 - pxor %xmm3,%xmm0 - psllq $57,%xmm0 - movdqa %xmm0,%xmm3 + pxor %xmm3,%xmm0 // + psllq $57,%xmm0 // + movdqa %xmm0,%xmm3 // pslldq $8,%xmm0 - psrldq $8,%xmm3 + psrldq $8,%xmm3 // pxor %xmm4,%xmm0 - pxor %xmm3,%xmm1 - + pxor %xmm3,%xmm1 // +// 2nd phase movdqa %xmm0,%xmm4 psrlq $1,%xmm0 - pxor %xmm4,%xmm1 + pxor %xmm4,%xmm1 // pxor %xmm0,%xmm4 psrlq $5,%xmm0 - pxor %xmm4,%xmm0 - psrlq $1,%xmm0 - pxor %xmm1,%xmm0 + pxor %xmm4,%xmm0 // + psrlq $1,%xmm0 // + pxor %xmm1,%xmm0 // pshufb %xmm5,%xmm0 movdqu %xmm0,(%rdi) ret @@ -226,6 +227,7 @@ L$_gmult_clmul: .private_extern _gcm_ghash_clmul .p2align 5 +.alt_entry _gcm_ghash_clmul _gcm_ghash_clmul: @@ -246,13 +248,13 @@ L$_ghash_clmul: jb L$skip4x subq $0x30,%rcx - movq $0xA040608020C0E000,%rax + movq $0xA040608020C0E000,%rax // ((7..0)·0xE0)&0xff movdqu 48(%rsi),%xmm14 movdqu 64(%rsi),%xmm15 - - - +// ###### +// Xi+4 =[(H*Ii+3) + (H^2*Ii+2) + (H^3*Ii+1) + H^4*(Ii+Xi)] mod P +// movdqu 48(%rdx),%xmm3 movdqu 32(%rdx),%xmm11 pshufb %xmm10,%xmm3 @@ -317,33 +319,33 @@ L$mod4_loop: pclmulqdq $0x00,%xmm2,%xmm11 pshufd $78,%xmm3,%xmm4 - pxor %xmm0,%xmm8 + pxor %xmm0,%xmm8 // aggregated Karatsuba post-processing movdqa %xmm3,%xmm5 - pxor %xmm1,%xmm8 + pxor %xmm1,%xmm8 // pxor %xmm3,%xmm4 - movdqa %xmm8,%xmm9 + movdqa %xmm8,%xmm9 // pclmulqdq $0x11,%xmm2,%xmm13 pslldq $8,%xmm8 - psrldq $8,%xmm9 + psrldq $8,%xmm9 // pxor %xmm8,%xmm0 movdqa L$7_mask(%rip),%xmm8 - pxor %xmm9,%xmm1 + pxor %xmm9,%xmm1 // movq %rax,%xmm9 - pand %xmm0,%xmm8 - pshufb %xmm8,%xmm9 - pxor %xmm0,%xmm9 + pand %xmm0,%xmm8 // 1st phase + pshufb %xmm8,%xmm9 // + pxor %xmm0,%xmm9 // pclmulqdq $0x00,%xmm7,%xmm12 - psllq $57,%xmm9 - movdqa %xmm9,%xmm8 + psllq $57,%xmm9 // + movdqa %xmm9,%xmm8 // pslldq $8,%xmm9 pclmulqdq $0x00,%xmm6,%xmm3 - psrldq $8,%xmm8 + psrldq $8,%xmm8 // pxor %xmm9,%xmm0 - pxor %xmm8,%xmm1 + pxor %xmm8,%xmm1 // movdqu 0(%rdx),%xmm8 - movdqa %xmm0,%xmm9 + movdqa %xmm0,%xmm9 // 2nd phase psrlq $1,%xmm0 pclmulqdq $0x11,%xmm6,%xmm5 xorps %xmm11,%xmm3 @@ -353,19 +355,19 @@ L$mod4_loop: xorps %xmm13,%xmm5 movups 80(%rsi),%xmm7 pshufb %xmm10,%xmm8 - pxor %xmm9,%xmm1 + pxor %xmm9,%xmm1 // pxor %xmm0,%xmm9 psrlq $5,%xmm0 movdqa %xmm11,%xmm13 pxor %xmm12,%xmm4 pshufd $78,%xmm11,%xmm12 - pxor %xmm9,%xmm0 + pxor %xmm9,%xmm0 // pxor %xmm8,%xmm1 pxor %xmm11,%xmm12 pclmulqdq $0x00,%xmm14,%xmm11 - psrlq $1,%xmm0 - pxor %xmm1,%xmm0 + psrlq $1,%xmm0 // + pxor %xmm1,%xmm0 // movdqa %xmm0,%xmm1 pclmulqdq $0x11,%xmm14,%xmm13 xorps %xmm11,%xmm3 @@ -386,56 +388,56 @@ L$tail4x: xorps %xmm12,%xmm4 xorps %xmm3,%xmm0 xorps %xmm5,%xmm1 - pxor %xmm0,%xmm1 + pxor %xmm0,%xmm1 // aggregated Karatsuba post-processing pxor %xmm4,%xmm8 - pxor %xmm1,%xmm8 + pxor %xmm1,%xmm8 // pxor %xmm0,%xmm1 - movdqa %xmm8,%xmm9 + movdqa %xmm8,%xmm9 // psrldq $8,%xmm8 - pslldq $8,%xmm9 + pslldq $8,%xmm9 // pxor %xmm8,%xmm1 - pxor %xmm9,%xmm0 - - movdqa %xmm0,%xmm4 + pxor %xmm9,%xmm0 // +// 1st phase + movdqa %xmm0,%xmm4 // movdqa %xmm0,%xmm3 psllq $5,%xmm0 - pxor %xmm0,%xmm3 + pxor %xmm0,%xmm3 // psllq $1,%xmm0 - pxor %xmm3,%xmm0 - psllq $57,%xmm0 - movdqa %xmm0,%xmm3 + pxor %xmm3,%xmm0 // + psllq $57,%xmm0 // + movdqa %xmm0,%xmm3 // pslldq $8,%xmm0 - psrldq $8,%xmm3 + psrldq $8,%xmm3 // pxor %xmm4,%xmm0 - pxor %xmm3,%xmm1 - + pxor %xmm3,%xmm1 // +// 2nd phase movdqa %xmm0,%xmm4 psrlq $1,%xmm0 - pxor %xmm4,%xmm1 + pxor %xmm4,%xmm1 // pxor %xmm0,%xmm4 psrlq $5,%xmm0 - pxor %xmm4,%xmm0 - psrlq $1,%xmm0 - pxor %xmm1,%xmm0 + pxor %xmm4,%xmm0 // + psrlq $1,%xmm0 // + pxor %xmm1,%xmm0 // addq $0x40,%rcx jz L$done movdqu 32(%rsi),%xmm7 subq $0x10,%rcx jz L$odd_tail L$skip4x: - - - - - - movdqu (%rdx),%xmm8 - movdqu 16(%rdx),%xmm3 +// ###### +// Xi+2 =[H*(Ii+1 + Xi+1)] mod P = +// [(H*Ii+1) + (H*Xi+1)] mod P = +// [(H*Ii+1) + H^2*(Ii+Xi)] mod P +// + movdqu (%rdx),%xmm8 // Ii + movdqu 16(%rdx),%xmm3 // Ii+1 pshufb %xmm10,%xmm8 pshufb %xmm10,%xmm3 - pxor %xmm8,%xmm0 + pxor %xmm8,%xmm0 // Ii+Xi movdqa %xmm3,%xmm5 pshufd $78,%xmm3,%xmm4 @@ -444,7 +446,7 @@ L$skip4x: pclmulqdq $0x11,%xmm2,%xmm5 pclmulqdq $0x00,%xmm7,%xmm4 - leaq 32(%rdx),%rdx + leaq 32(%rdx),%rdx // i+=2 nop subq $0x20,%rcx jbe L$even_tail @@ -455,59 +457,59 @@ L$skip4x: L$mod_loop: movdqa %xmm0,%xmm1 movdqa %xmm4,%xmm8 - pshufd $78,%xmm0,%xmm4 - pxor %xmm0,%xmm4 + pshufd $78,%xmm0,%xmm4 // + pxor %xmm0,%xmm4 // pclmulqdq $0x00,%xmm6,%xmm0 pclmulqdq $0x11,%xmm6,%xmm1 pclmulqdq $0x10,%xmm7,%xmm4 - pxor %xmm3,%xmm0 + pxor %xmm3,%xmm0 // (H*Ii+1) + H^2*(Ii+Xi) pxor %xmm5,%xmm1 - movdqu (%rdx),%xmm9 - pxor %xmm0,%xmm8 + movdqu (%rdx),%xmm9 // Ii + pxor %xmm0,%xmm8 // aggregated Karatsuba post-processing pshufb %xmm10,%xmm9 - movdqu 16(%rdx),%xmm3 + movdqu 16(%rdx),%xmm3 // Ii+1 pxor %xmm1,%xmm8 - pxor %xmm9,%xmm1 + pxor %xmm9,%xmm1 // "Ii+Xi", consume early pxor %xmm8,%xmm4 pshufb %xmm10,%xmm3 - movdqa %xmm4,%xmm8 + movdqa %xmm4,%xmm8 // psrldq $8,%xmm8 - pslldq $8,%xmm4 + pslldq $8,%xmm4 // pxor %xmm8,%xmm1 - pxor %xmm4,%xmm0 + pxor %xmm4,%xmm0 // - movdqa %xmm3,%xmm5 + movdqa %xmm3,%xmm5 // - movdqa %xmm0,%xmm9 + movdqa %xmm0,%xmm9 // 1st phase movdqa %xmm0,%xmm8 psllq $5,%xmm0 - pxor %xmm0,%xmm8 - pclmulqdq $0x00,%xmm2,%xmm3 + pxor %xmm0,%xmm8 // + pclmulqdq $0x00,%xmm2,%xmm3 // ###### psllq $1,%xmm0 - pxor %xmm8,%xmm0 - psllq $57,%xmm0 - movdqa %xmm0,%xmm8 + pxor %xmm8,%xmm0 // + psllq $57,%xmm0 // + movdqa %xmm0,%xmm8 // pslldq $8,%xmm0 - psrldq $8,%xmm8 + psrldq $8,%xmm8 // pxor %xmm9,%xmm0 pshufd $78,%xmm5,%xmm4 - pxor %xmm8,%xmm1 - pxor %xmm5,%xmm4 + pxor %xmm8,%xmm1 // + pxor %xmm5,%xmm4 // - movdqa %xmm0,%xmm9 + movdqa %xmm0,%xmm9 // 2nd phase psrlq $1,%xmm0 - pclmulqdq $0x11,%xmm2,%xmm5 - pxor %xmm9,%xmm1 + pclmulqdq $0x11,%xmm2,%xmm5 // ###### + pxor %xmm9,%xmm1 // pxor %xmm0,%xmm9 psrlq $5,%xmm0 - pxor %xmm9,%xmm0 + pxor %xmm9,%xmm0 // leaq 32(%rdx),%rdx - psrlq $1,%xmm0 - pclmulqdq $0x00,%xmm7,%xmm4 - pxor %xmm1,%xmm0 + psrlq $1,%xmm0 // + pclmulqdq $0x00,%xmm7,%xmm4 // ###### + pxor %xmm1,%xmm0 // subq $0x20,%rcx ja L$mod_loop @@ -515,90 +517,90 @@ L$mod_loop: L$even_tail: movdqa %xmm0,%xmm1 movdqa %xmm4,%xmm8 - pshufd $78,%xmm0,%xmm4 - pxor %xmm0,%xmm4 + pshufd $78,%xmm0,%xmm4 // + pxor %xmm0,%xmm4 // pclmulqdq $0x00,%xmm6,%xmm0 pclmulqdq $0x11,%xmm6,%xmm1 pclmulqdq $0x10,%xmm7,%xmm4 - pxor %xmm3,%xmm0 + pxor %xmm3,%xmm0 // (H*Ii+1) + H^2*(Ii+Xi) pxor %xmm5,%xmm1 pxor %xmm0,%xmm8 pxor %xmm1,%xmm8 pxor %xmm8,%xmm4 - movdqa %xmm4,%xmm8 + movdqa %xmm4,%xmm8 // psrldq $8,%xmm8 - pslldq $8,%xmm4 + pslldq $8,%xmm4 // pxor %xmm8,%xmm1 - pxor %xmm4,%xmm0 - - movdqa %xmm0,%xmm4 + pxor %xmm4,%xmm0 // +// 1st phase + movdqa %xmm0,%xmm4 // movdqa %xmm0,%xmm3 psllq $5,%xmm0 - pxor %xmm0,%xmm3 + pxor %xmm0,%xmm3 // psllq $1,%xmm0 - pxor %xmm3,%xmm0 - psllq $57,%xmm0 - movdqa %xmm0,%xmm3 + pxor %xmm3,%xmm0 // + psllq $57,%xmm0 // + movdqa %xmm0,%xmm3 // pslldq $8,%xmm0 - psrldq $8,%xmm3 + psrldq $8,%xmm3 // pxor %xmm4,%xmm0 - pxor %xmm3,%xmm1 - + pxor %xmm3,%xmm1 // +// 2nd phase movdqa %xmm0,%xmm4 psrlq $1,%xmm0 - pxor %xmm4,%xmm1 + pxor %xmm4,%xmm1 // pxor %xmm0,%xmm4 psrlq $5,%xmm0 - pxor %xmm4,%xmm0 - psrlq $1,%xmm0 - pxor %xmm1,%xmm0 + pxor %xmm4,%xmm0 // + psrlq $1,%xmm0 // + pxor %xmm1,%xmm0 // testq %rcx,%rcx jnz L$done L$odd_tail: - movdqu (%rdx),%xmm8 + movdqu (%rdx),%xmm8 // Ii pshufb %xmm10,%xmm8 - pxor %xmm8,%xmm0 - movdqa %xmm0,%xmm1 + pxor %xmm8,%xmm0 // Ii+Xi + movdqa %xmm0,%xmm1 // pshufd $78,%xmm0,%xmm3 - pxor %xmm0,%xmm3 - pclmulqdq $0x00,%xmm2,%xmm0 - pclmulqdq $0x11,%xmm2,%xmm1 - pclmulqdq $0x00,%xmm7,%xmm3 - pxor %xmm0,%xmm3 - pxor %xmm1,%xmm3 - - movdqa %xmm3,%xmm4 + pxor %xmm0,%xmm3 // + pclmulqdq $0x00,%xmm2,%xmm0 // ###### + pclmulqdq $0x11,%xmm2,%xmm1 // ###### + pclmulqdq $0x00,%xmm7,%xmm3 // ###### + pxor %xmm0,%xmm3 // + pxor %xmm1,%xmm3 // + + movdqa %xmm3,%xmm4 // psrldq $8,%xmm3 - pslldq $8,%xmm4 + pslldq $8,%xmm4 // pxor %xmm3,%xmm1 - pxor %xmm4,%xmm0 - - movdqa %xmm0,%xmm4 + pxor %xmm4,%xmm0 // +// 1st phase + movdqa %xmm0,%xmm4 // movdqa %xmm0,%xmm3 psllq $5,%xmm0 - pxor %xmm0,%xmm3 + pxor %xmm0,%xmm3 // psllq $1,%xmm0 - pxor %xmm3,%xmm0 - psllq $57,%xmm0 - movdqa %xmm0,%xmm3 + pxor %xmm3,%xmm0 // + psllq $57,%xmm0 // + movdqa %xmm0,%xmm3 // pslldq $8,%xmm0 - psrldq $8,%xmm3 + psrldq $8,%xmm3 // pxor %xmm4,%xmm0 - pxor %xmm3,%xmm1 - + pxor %xmm3,%xmm1 // +// 2nd phase movdqa %xmm0,%xmm4 psrlq $1,%xmm0 - pxor %xmm4,%xmm1 + pxor %xmm4,%xmm1 // pxor %xmm0,%xmm4 psrlq $5,%xmm0 - pxor %xmm4,%xmm0 - psrlq $1,%xmm0 - pxor %xmm1,%xmm0 + pxor %xmm4,%xmm0 // + psrlq $1,%xmm0 // + pxor %xmm1,%xmm0 // L$done: pshufb %xmm10,%xmm0 movdqu %xmm0,(%rdi) @@ -610,6 +612,7 @@ L$done: .private_extern _gcm_init_avx .p2align 5 +.alt_entry _gcm_init_avx _gcm_init_avx: @@ -617,101 +620,101 @@ _CET_ENDBR vzeroupper vmovdqu (%rsi),%xmm2 - vpshufd $78,%xmm2,%xmm2 - + vpshufd $78,%xmm2,%xmm2 // dword swap - vpshufd $255,%xmm2,%xmm4 +// <<1 twist + vpshufd $255,%xmm2,%xmm4 // broadcast uppermost dword vpsrlq $63,%xmm2,%xmm3 vpsllq $1,%xmm2,%xmm2 - vpxor %xmm5,%xmm5,%xmm5 - vpcmpgtd %xmm4,%xmm5,%xmm5 + vpxor %xmm5,%xmm5,%xmm5 // + vpcmpgtd %xmm4,%xmm5,%xmm5 // broadcast carry bit vpslldq $8,%xmm3,%xmm3 - vpor %xmm3,%xmm2,%xmm2 - + vpor %xmm3,%xmm2,%xmm2 // H<<=1 +// magic reduction vpand L$0x1c2_polynomial(%rip),%xmm5,%xmm5 - vpxor %xmm5,%xmm2,%xmm2 + vpxor %xmm5,%xmm2,%xmm2 // if(carry) H^=0x1c2_polynomial vpunpckhqdq %xmm2,%xmm2,%xmm6 vmovdqa %xmm2,%xmm0 vpxor %xmm2,%xmm6,%xmm6 - movq $4,%r10 + movq $4,%r10 // up to H^8 jmp L$init_start_avx .p2align 5 L$init_loop_avx: - vpalignr $8,%xmm3,%xmm4,%xmm5 - vmovdqu %xmm5,-16(%rdi) + vpalignr $8,%xmm3,%xmm4,%xmm5 // low part is H.lo^H.hi... + vmovdqu %xmm5,-16(%rdi) // save Karatsuba "salt" vpunpckhqdq %xmm0,%xmm0,%xmm3 - vpxor %xmm0,%xmm3,%xmm3 - vpclmulqdq $0x11,%xmm2,%xmm0,%xmm1 - vpclmulqdq $0x00,%xmm2,%xmm0,%xmm0 - vpclmulqdq $0x00,%xmm6,%xmm3,%xmm3 - vpxor %xmm0,%xmm1,%xmm4 - vpxor %xmm4,%xmm3,%xmm3 - - vpslldq $8,%xmm3,%xmm4 + vpxor %xmm0,%xmm3,%xmm3 // + vpclmulqdq $0x11,%xmm2,%xmm0,%xmm1 // ###### + vpclmulqdq $0x00,%xmm2,%xmm0,%xmm0 // ###### + vpclmulqdq $0x00,%xmm6,%xmm3,%xmm3 // ###### + vpxor %xmm0,%xmm1,%xmm4 // + vpxor %xmm4,%xmm3,%xmm3 // + + vpslldq $8,%xmm3,%xmm4 // vpsrldq $8,%xmm3,%xmm3 - vpxor %xmm4,%xmm0,%xmm0 + vpxor %xmm4,%xmm0,%xmm0 // vpxor %xmm3,%xmm1,%xmm1 - vpsllq $57,%xmm0,%xmm3 + vpsllq $57,%xmm0,%xmm3 // 1st phase vpsllq $62,%xmm0,%xmm4 - vpxor %xmm3,%xmm4,%xmm4 + vpxor %xmm3,%xmm4,%xmm4 // vpsllq $63,%xmm0,%xmm3 - vpxor %xmm3,%xmm4,%xmm4 - vpslldq $8,%xmm4,%xmm3 + vpxor %xmm3,%xmm4,%xmm4 // + vpslldq $8,%xmm4,%xmm3 // vpsrldq $8,%xmm4,%xmm4 - vpxor %xmm3,%xmm0,%xmm0 + vpxor %xmm3,%xmm0,%xmm0 // vpxor %xmm4,%xmm1,%xmm1 - vpsrlq $1,%xmm0,%xmm4 + vpsrlq $1,%xmm0,%xmm4 // 2nd phase vpxor %xmm0,%xmm1,%xmm1 - vpxor %xmm4,%xmm0,%xmm0 + vpxor %xmm4,%xmm0,%xmm0 // vpsrlq $5,%xmm4,%xmm4 - vpxor %xmm4,%xmm0,%xmm0 - vpsrlq $1,%xmm0,%xmm0 - vpxor %xmm1,%xmm0,%xmm0 + vpxor %xmm4,%xmm0,%xmm0 // + vpsrlq $1,%xmm0,%xmm0 // + vpxor %xmm1,%xmm0,%xmm0 // L$init_start_avx: vmovdqa %xmm0,%xmm5 vpunpckhqdq %xmm0,%xmm0,%xmm3 - vpxor %xmm0,%xmm3,%xmm3 - vpclmulqdq $0x11,%xmm2,%xmm0,%xmm1 - vpclmulqdq $0x00,%xmm2,%xmm0,%xmm0 - vpclmulqdq $0x00,%xmm6,%xmm3,%xmm3 - vpxor %xmm0,%xmm1,%xmm4 - vpxor %xmm4,%xmm3,%xmm3 - - vpslldq $8,%xmm3,%xmm4 + vpxor %xmm0,%xmm3,%xmm3 // + vpclmulqdq $0x11,%xmm2,%xmm0,%xmm1 // ###### + vpclmulqdq $0x00,%xmm2,%xmm0,%xmm0 // ###### + vpclmulqdq $0x00,%xmm6,%xmm3,%xmm3 // ###### + vpxor %xmm0,%xmm1,%xmm4 // + vpxor %xmm4,%xmm3,%xmm3 // + + vpslldq $8,%xmm3,%xmm4 // vpsrldq $8,%xmm3,%xmm3 - vpxor %xmm4,%xmm0,%xmm0 + vpxor %xmm4,%xmm0,%xmm0 // vpxor %xmm3,%xmm1,%xmm1 - vpsllq $57,%xmm0,%xmm3 + vpsllq $57,%xmm0,%xmm3 // 1st phase vpsllq $62,%xmm0,%xmm4 - vpxor %xmm3,%xmm4,%xmm4 + vpxor %xmm3,%xmm4,%xmm4 // vpsllq $63,%xmm0,%xmm3 - vpxor %xmm3,%xmm4,%xmm4 - vpslldq $8,%xmm4,%xmm3 + vpxor %xmm3,%xmm4,%xmm4 // + vpslldq $8,%xmm4,%xmm3 // vpsrldq $8,%xmm4,%xmm4 - vpxor %xmm3,%xmm0,%xmm0 + vpxor %xmm3,%xmm0,%xmm0 // vpxor %xmm4,%xmm1,%xmm1 - vpsrlq $1,%xmm0,%xmm4 + vpsrlq $1,%xmm0,%xmm4 // 2nd phase vpxor %xmm0,%xmm1,%xmm1 - vpxor %xmm4,%xmm0,%xmm0 + vpxor %xmm4,%xmm0,%xmm0 // vpsrlq $5,%xmm4,%xmm4 - vpxor %xmm4,%xmm0,%xmm0 - vpsrlq $1,%xmm0,%xmm0 - vpxor %xmm1,%xmm0,%xmm0 + vpxor %xmm4,%xmm0,%xmm0 // + vpsrlq $1,%xmm0,%xmm0 // + vpxor %xmm1,%xmm0,%xmm0 // vpshufd $78,%xmm5,%xmm3 vpshufd $78,%xmm0,%xmm4 - vpxor %xmm5,%xmm3,%xmm3 - vmovdqu %xmm5,0(%rdi) - vpxor %xmm0,%xmm4,%xmm4 - vmovdqu %xmm0,16(%rdi) + vpxor %xmm5,%xmm3,%xmm3 // Karatsuba pre-processing + vmovdqu %xmm5,0(%rdi) // save H^1,3,5,7 + vpxor %xmm0,%xmm4,%xmm4 // Karatsuba pre-processing + vmovdqu %xmm0,16(%rdi) // save H^2,4,6,8 leaq 48(%rdi),%rdi subq $1,%r10 jnz L$init_loop_avx - vpalignr $8,%xmm4,%xmm3,%xmm5 + vpalignr $8,%xmm4,%xmm3,%xmm5 // last "salt" is flipped vmovdqu %xmm5,-16(%rdi) vzeroupper @@ -723,6 +726,7 @@ L$init_start_avx: .private_extern _gcm_gmult_avx .p2align 5 +.alt_entry _gcm_gmult_avx _gcm_gmult_avx: _CET_ENDBR @@ -733,35 +737,36 @@ _CET_ENDBR .private_extern _gcm_ghash_avx .p2align 5 +.alt_entry _gcm_ghash_avx _gcm_ghash_avx: _CET_ENDBR vzeroupper - vmovdqu (%rdi),%xmm10 + vmovdqu (%rdi),%xmm10 // load %xmm10 leaq L$0x1c2_polynomial(%rip),%r10 - leaq 64(%rsi),%rsi + leaq 64(%rsi),%rsi // size optimization vmovdqu L$bswap_mask(%rip),%xmm13 vpshufb %xmm13,%xmm10,%xmm10 cmpq $0x80,%rcx jb L$short_avx subq $0x80,%rcx - vmovdqu 112(%rdx),%xmm14 - vmovdqu 0-64(%rsi),%xmm6 + vmovdqu 112(%rdx),%xmm14 // I[7] + vmovdqu 0-64(%rsi),%xmm6 // %xmm6^1 vpshufb %xmm13,%xmm14,%xmm14 vmovdqu 32-64(%rsi),%xmm7 vpunpckhqdq %xmm14,%xmm14,%xmm9 - vmovdqu 96(%rdx),%xmm15 + vmovdqu 96(%rdx),%xmm15 // I[6] vpclmulqdq $0x00,%xmm6,%xmm14,%xmm0 vpxor %xmm14,%xmm9,%xmm9 vpshufb %xmm13,%xmm15,%xmm15 vpclmulqdq $0x11,%xmm6,%xmm14,%xmm1 - vmovdqu 16-64(%rsi),%xmm6 + vmovdqu 16-64(%rsi),%xmm6 // %xmm6^2 vpunpckhqdq %xmm15,%xmm15,%xmm8 - vmovdqu 80(%rdx),%xmm14 + vmovdqu 80(%rdx),%xmm14 // I[5] vpclmulqdq $0x00,%xmm7,%xmm9,%xmm2 vpxor %xmm15,%xmm8,%xmm8 @@ -769,9 +774,9 @@ _CET_ENDBR vpclmulqdq $0x00,%xmm6,%xmm15,%xmm3 vpunpckhqdq %xmm14,%xmm14,%xmm9 vpclmulqdq $0x11,%xmm6,%xmm15,%xmm4 - vmovdqu 48-64(%rsi),%xmm6 + vmovdqu 48-64(%rsi),%xmm6 // %xmm6^3 vpxor %xmm14,%xmm9,%xmm9 - vmovdqu 64(%rdx),%xmm15 + vmovdqu 64(%rdx),%xmm15 // I[4] vpclmulqdq $0x10,%xmm7,%xmm8,%xmm5 vmovdqu 80-64(%rsi),%xmm7 @@ -781,56 +786,56 @@ _CET_ENDBR vpxor %xmm1,%xmm4,%xmm4 vpunpckhqdq %xmm15,%xmm15,%xmm8 vpclmulqdq $0x11,%xmm6,%xmm14,%xmm1 - vmovdqu 64-64(%rsi),%xmm6 + vmovdqu 64-64(%rsi),%xmm6 // %xmm6^4 vpxor %xmm2,%xmm5,%xmm5 vpclmulqdq $0x00,%xmm7,%xmm9,%xmm2 vpxor %xmm15,%xmm8,%xmm8 - vmovdqu 48(%rdx),%xmm14 + vmovdqu 48(%rdx),%xmm14 // I[3] vpxor %xmm3,%xmm0,%xmm0 vpclmulqdq $0x00,%xmm6,%xmm15,%xmm3 vpxor %xmm4,%xmm1,%xmm1 vpshufb %xmm13,%xmm14,%xmm14 vpclmulqdq $0x11,%xmm6,%xmm15,%xmm4 - vmovdqu 96-64(%rsi),%xmm6 + vmovdqu 96-64(%rsi),%xmm6 // %xmm6^5 vpxor %xmm5,%xmm2,%xmm2 vpunpckhqdq %xmm14,%xmm14,%xmm9 vpclmulqdq $0x10,%xmm7,%xmm8,%xmm5 vmovdqu 128-64(%rsi),%xmm7 vpxor %xmm14,%xmm9,%xmm9 - vmovdqu 32(%rdx),%xmm15 + vmovdqu 32(%rdx),%xmm15 // I[2] vpxor %xmm0,%xmm3,%xmm3 vpclmulqdq $0x00,%xmm6,%xmm14,%xmm0 vpxor %xmm1,%xmm4,%xmm4 vpshufb %xmm13,%xmm15,%xmm15 vpclmulqdq $0x11,%xmm6,%xmm14,%xmm1 - vmovdqu 112-64(%rsi),%xmm6 + vmovdqu 112-64(%rsi),%xmm6 // %xmm6^6 vpxor %xmm2,%xmm5,%xmm5 vpunpckhqdq %xmm15,%xmm15,%xmm8 vpclmulqdq $0x00,%xmm7,%xmm9,%xmm2 vpxor %xmm15,%xmm8,%xmm8 - vmovdqu 16(%rdx),%xmm14 + vmovdqu 16(%rdx),%xmm14 // I[1] vpxor %xmm3,%xmm0,%xmm0 vpclmulqdq $0x00,%xmm6,%xmm15,%xmm3 vpxor %xmm4,%xmm1,%xmm1 vpshufb %xmm13,%xmm14,%xmm14 vpclmulqdq $0x11,%xmm6,%xmm15,%xmm4 - vmovdqu 144-64(%rsi),%xmm6 + vmovdqu 144-64(%rsi),%xmm6 // %xmm6^7 vpxor %xmm5,%xmm2,%xmm2 vpunpckhqdq %xmm14,%xmm14,%xmm9 vpclmulqdq $0x10,%xmm7,%xmm8,%xmm5 vmovdqu 176-64(%rsi),%xmm7 vpxor %xmm14,%xmm9,%xmm9 - vmovdqu (%rdx),%xmm15 + vmovdqu (%rdx),%xmm15 // I[0] vpxor %xmm0,%xmm3,%xmm3 vpclmulqdq $0x00,%xmm6,%xmm14,%xmm0 vpxor %xmm1,%xmm4,%xmm4 vpshufb %xmm13,%xmm15,%xmm15 vpclmulqdq $0x11,%xmm6,%xmm14,%xmm1 - vmovdqu 160-64(%rsi),%xmm6 + vmovdqu 160-64(%rsi),%xmm6 // %xmm6^8 vpxor %xmm2,%xmm5,%xmm5 vpclmulqdq $0x10,%xmm7,%xmm9,%xmm2 @@ -838,41 +843,41 @@ _CET_ENDBR cmpq $0x80,%rcx jb L$tail_avx - vpxor %xmm10,%xmm15,%xmm15 + vpxor %xmm10,%xmm15,%xmm15 // accumulate %xmm10 subq $0x80,%rcx jmp L$oop8x_avx .p2align 5 L$oop8x_avx: vpunpckhqdq %xmm15,%xmm15,%xmm8 - vmovdqu 112(%rdx),%xmm14 + vmovdqu 112(%rdx),%xmm14 // I[7] vpxor %xmm0,%xmm3,%xmm3 vpxor %xmm15,%xmm8,%xmm8 vpclmulqdq $0x00,%xmm6,%xmm15,%xmm10 vpshufb %xmm13,%xmm14,%xmm14 vpxor %xmm1,%xmm4,%xmm4 vpclmulqdq $0x11,%xmm6,%xmm15,%xmm11 - vmovdqu 0-64(%rsi),%xmm6 + vmovdqu 0-64(%rsi),%xmm6 // %xmm6^1 vpunpckhqdq %xmm14,%xmm14,%xmm9 vpxor %xmm2,%xmm5,%xmm5 vpclmulqdq $0x00,%xmm7,%xmm8,%xmm12 vmovdqu 32-64(%rsi),%xmm7 vpxor %xmm14,%xmm9,%xmm9 - vmovdqu 96(%rdx),%xmm15 + vmovdqu 96(%rdx),%xmm15 // I[6] vpclmulqdq $0x00,%xmm6,%xmm14,%xmm0 - vpxor %xmm3,%xmm10,%xmm10 + vpxor %xmm3,%xmm10,%xmm10 // collect result vpshufb %xmm13,%xmm15,%xmm15 vpclmulqdq $0x11,%xmm6,%xmm14,%xmm1 vxorps %xmm4,%xmm11,%xmm11 - vmovdqu 16-64(%rsi),%xmm6 + vmovdqu 16-64(%rsi),%xmm6 // %xmm6^2 vpunpckhqdq %xmm15,%xmm15,%xmm8 vpclmulqdq $0x00,%xmm7,%xmm9,%xmm2 vpxor %xmm5,%xmm12,%xmm12 vxorps %xmm15,%xmm8,%xmm8 - vmovdqu 80(%rdx),%xmm14 - vpxor %xmm10,%xmm12,%xmm12 + vmovdqu 80(%rdx),%xmm14 // I[5] + vpxor %xmm10,%xmm12,%xmm12 // aggregated Karatsuba post-processing vpclmulqdq $0x00,%xmm6,%xmm15,%xmm3 vpxor %xmm11,%xmm12,%xmm12 vpslldq $8,%xmm12,%xmm9 @@ -880,7 +885,7 @@ L$oop8x_avx: vpclmulqdq $0x11,%xmm6,%xmm15,%xmm4 vpsrldq $8,%xmm12,%xmm12 vpxor %xmm9,%xmm10,%xmm10 - vmovdqu 48-64(%rsi),%xmm6 + vmovdqu 48-64(%rsi),%xmm6 // %xmm6^3 vpshufb %xmm13,%xmm14,%xmm14 vxorps %xmm12,%xmm11,%xmm11 vpxor %xmm1,%xmm4,%xmm4 @@ -890,26 +895,26 @@ L$oop8x_avx: vpxor %xmm14,%xmm9,%xmm9 vpxor %xmm2,%xmm5,%xmm5 - vmovdqu 64(%rdx),%xmm15 - vpalignr $8,%xmm10,%xmm10,%xmm12 + vmovdqu 64(%rdx),%xmm15 // I[4] + vpalignr $8,%xmm10,%xmm10,%xmm12 // 1st phase vpclmulqdq $0x00,%xmm6,%xmm14,%xmm0 vpshufb %xmm13,%xmm15,%xmm15 vpxor %xmm3,%xmm0,%xmm0 vpclmulqdq $0x11,%xmm6,%xmm14,%xmm1 - vmovdqu 64-64(%rsi),%xmm6 + vmovdqu 64-64(%rsi),%xmm6 // %xmm6^4 vpunpckhqdq %xmm15,%xmm15,%xmm8 vpxor %xmm4,%xmm1,%xmm1 vpclmulqdq $0x00,%xmm7,%xmm9,%xmm2 vxorps %xmm15,%xmm8,%xmm8 vpxor %xmm5,%xmm2,%xmm2 - vmovdqu 48(%rdx),%xmm14 + vmovdqu 48(%rdx),%xmm14 // I[3] vpclmulqdq $0x10,(%r10),%xmm10,%xmm10 vpclmulqdq $0x00,%xmm6,%xmm15,%xmm3 vpshufb %xmm13,%xmm14,%xmm14 vpxor %xmm0,%xmm3,%xmm3 vpclmulqdq $0x11,%xmm6,%xmm15,%xmm4 - vmovdqu 96-64(%rsi),%xmm6 + vmovdqu 96-64(%rsi),%xmm6 // %xmm6^5 vpunpckhqdq %xmm14,%xmm14,%xmm9 vpxor %xmm1,%xmm4,%xmm4 vpclmulqdq $0x10,%xmm7,%xmm8,%xmm5 @@ -917,12 +922,12 @@ L$oop8x_avx: vpxor %xmm14,%xmm9,%xmm9 vpxor %xmm2,%xmm5,%xmm5 - vmovdqu 32(%rdx),%xmm15 + vmovdqu 32(%rdx),%xmm15 // I[2] vpclmulqdq $0x00,%xmm6,%xmm14,%xmm0 vpshufb %xmm13,%xmm15,%xmm15 vpxor %xmm3,%xmm0,%xmm0 vpclmulqdq $0x11,%xmm6,%xmm14,%xmm1 - vmovdqu 112-64(%rsi),%xmm6 + vmovdqu 112-64(%rsi),%xmm6 // %xmm6^6 vpunpckhqdq %xmm15,%xmm15,%xmm8 vpxor %xmm4,%xmm1,%xmm1 vpclmulqdq $0x00,%xmm7,%xmm9,%xmm2 @@ -930,13 +935,13 @@ L$oop8x_avx: vpxor %xmm5,%xmm2,%xmm2 vxorps %xmm12,%xmm10,%xmm10 - vmovdqu 16(%rdx),%xmm14 - vpalignr $8,%xmm10,%xmm10,%xmm12 + vmovdqu 16(%rdx),%xmm14 // I[1] + vpalignr $8,%xmm10,%xmm10,%xmm12 // 2nd phase vpclmulqdq $0x00,%xmm6,%xmm15,%xmm3 vpshufb %xmm13,%xmm14,%xmm14 vpxor %xmm0,%xmm3,%xmm3 vpclmulqdq $0x11,%xmm6,%xmm15,%xmm4 - vmovdqu 144-64(%rsi),%xmm6 + vmovdqu 144-64(%rsi),%xmm6 // %xmm6^7 vpclmulqdq $0x10,(%r10),%xmm10,%xmm10 vxorps %xmm11,%xmm12,%xmm12 vpunpckhqdq %xmm14,%xmm14,%xmm9 @@ -946,14 +951,14 @@ L$oop8x_avx: vpxor %xmm14,%xmm9,%xmm9 vpxor %xmm2,%xmm5,%xmm5 - vmovdqu (%rdx),%xmm15 + vmovdqu (%rdx),%xmm15 // I[0] vpclmulqdq $0x00,%xmm6,%xmm14,%xmm0 vpshufb %xmm13,%xmm15,%xmm15 vpclmulqdq $0x11,%xmm6,%xmm14,%xmm1 - vmovdqu 160-64(%rsi),%xmm6 + vmovdqu 160-64(%rsi),%xmm6 // %xmm6^8 vpxor %xmm12,%xmm15,%xmm15 vpclmulqdq $0x10,%xmm7,%xmm9,%xmm2 - vpxor %xmm10,%xmm15,%xmm15 + vpxor %xmm10,%xmm15,%xmm15 // accumulate %xmm10 leaq 128(%rdx),%rdx subq $0x80,%rcx @@ -964,15 +969,15 @@ L$oop8x_avx: .p2align 5 L$short_avx: - vmovdqu -16(%rdx,%rcx,1),%xmm14 + vmovdqu -16(%rdx,%rcx,1),%xmm14 // very last word leaq (%rdx,%rcx,1),%rdx - vmovdqu 0-64(%rsi),%xmm6 + vmovdqu 0-64(%rsi),%xmm6 // %xmm6^1 vmovdqu 32-64(%rsi),%xmm7 vpshufb %xmm13,%xmm14,%xmm15 - vmovdqa %xmm0,%xmm3 - vmovdqa %xmm1,%xmm4 - vmovdqa %xmm2,%xmm5 + vmovdqa %xmm0,%xmm3 // subtle way to zero %xmm3, + vmovdqa %xmm1,%xmm4 // %xmm4 and + vmovdqa %xmm2,%xmm5 // %xmm5 subq $0x10,%rcx jz L$tail_avx @@ -983,7 +988,7 @@ L$short_avx: vmovdqu -32(%rdx),%xmm14 vpxor %xmm1,%xmm4,%xmm4 vpclmulqdq $0x11,%xmm6,%xmm15,%xmm1 - vmovdqu 16-64(%rsi),%xmm6 + vmovdqu 16-64(%rsi),%xmm6 // %xmm6^2 vpshufb %xmm13,%xmm14,%xmm15 vpxor %xmm2,%xmm5,%xmm5 vpclmulqdq $0x00,%xmm7,%xmm8,%xmm2 @@ -998,7 +1003,7 @@ L$short_avx: vmovdqu -48(%rdx),%xmm14 vpxor %xmm1,%xmm4,%xmm4 vpclmulqdq $0x11,%xmm6,%xmm15,%xmm1 - vmovdqu 48-64(%rsi),%xmm6 + vmovdqu 48-64(%rsi),%xmm6 // %xmm6^3 vpshufb %xmm13,%xmm14,%xmm15 vpxor %xmm2,%xmm5,%xmm5 vpclmulqdq $0x00,%xmm7,%xmm8,%xmm2 @@ -1013,7 +1018,7 @@ L$short_avx: vmovdqu -64(%rdx),%xmm14 vpxor %xmm1,%xmm4,%xmm4 vpclmulqdq $0x11,%xmm6,%xmm15,%xmm1 - vmovdqu 64-64(%rsi),%xmm6 + vmovdqu 64-64(%rsi),%xmm6 // %xmm6^4 vpshufb %xmm13,%xmm14,%xmm15 vpxor %xmm2,%xmm5,%xmm5 vpclmulqdq $0x00,%xmm7,%xmm8,%xmm2 @@ -1028,7 +1033,7 @@ L$short_avx: vmovdqu -80(%rdx),%xmm14 vpxor %xmm1,%xmm4,%xmm4 vpclmulqdq $0x11,%xmm6,%xmm15,%xmm1 - vmovdqu 96-64(%rsi),%xmm6 + vmovdqu 96-64(%rsi),%xmm6 // %xmm6^5 vpshufb %xmm13,%xmm14,%xmm15 vpxor %xmm2,%xmm5,%xmm5 vpclmulqdq $0x00,%xmm7,%xmm8,%xmm2 @@ -1043,7 +1048,7 @@ L$short_avx: vmovdqu -96(%rdx),%xmm14 vpxor %xmm1,%xmm4,%xmm4 vpclmulqdq $0x11,%xmm6,%xmm15,%xmm1 - vmovdqu 112-64(%rsi),%xmm6 + vmovdqu 112-64(%rsi),%xmm6 // %xmm6^6 vpshufb %xmm13,%xmm14,%xmm15 vpxor %xmm2,%xmm5,%xmm5 vpclmulqdq $0x00,%xmm7,%xmm8,%xmm2 @@ -1058,7 +1063,7 @@ L$short_avx: vmovdqu -112(%rdx),%xmm14 vpxor %xmm1,%xmm4,%xmm4 vpclmulqdq $0x11,%xmm6,%xmm15,%xmm1 - vmovdqu 144-64(%rsi),%xmm6 + vmovdqu 144-64(%rsi),%xmm6 // %xmm6^7 vpshufb %xmm13,%xmm14,%xmm15 vpxor %xmm2,%xmm5,%xmm5 vpclmulqdq $0x00,%xmm7,%xmm8,%xmm2 @@ -1068,7 +1073,7 @@ L$short_avx: .p2align 5 L$tail_avx: - vpxor %xmm10,%xmm15,%xmm15 + vpxor %xmm10,%xmm15,%xmm15 // accumulate %xmm10 L$tail_no_xor_avx: vpunpckhqdq %xmm15,%xmm15,%xmm8 vpxor %xmm0,%xmm3,%xmm3 @@ -1085,18 +1090,18 @@ L$tail_no_xor_avx: vpxor %xmm1,%xmm4,%xmm11 vpxor %xmm2,%xmm5,%xmm5 - vpxor %xmm10,%xmm5,%xmm5 + vpxor %xmm10,%xmm5,%xmm5 // aggregated Karatsuba post-processing vpxor %xmm11,%xmm5,%xmm5 vpslldq $8,%xmm5,%xmm9 vpsrldq $8,%xmm5,%xmm5 vpxor %xmm9,%xmm10,%xmm10 vpxor %xmm5,%xmm11,%xmm11 - vpclmulqdq $0x10,%xmm12,%xmm10,%xmm9 + vpclmulqdq $0x10,%xmm12,%xmm10,%xmm9 // 1st phase vpalignr $8,%xmm10,%xmm10,%xmm10 vpxor %xmm9,%xmm10,%xmm10 - vpclmulqdq $0x10,%xmm12,%xmm10,%xmm9 + vpclmulqdq $0x10,%xmm12,%xmm10,%xmm9 // 2nd phase vpalignr $8,%xmm10,%xmm10,%xmm10 vpxor %xmm11,%xmm10,%xmm10 vpxor %xmm9,%xmm10,%xmm10 @@ -1112,7 +1117,9 @@ L$tail_no_xor_avx: .section __DATA,__const + .p2align 6 +ghash_constants: L$bswap_mask: .byte 15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0 L$0x1c2_polynomial: diff --git a/third_party/boringssl/gen/bcm/ghash-x86_64-linux.S b/third_party/boringssl/gen/bcm/ghash-x86_64-linux.S index f1ffcb824..6cfec7fb6 100644 --- a/third_party/boringssl/gen/bcm/ghash-x86_64-linux.S +++ b/third_party/boringssl/gen/bcm/ghash-x86_64-linux.S @@ -15,154 +15,154 @@ gcm_init_clmul: _CET_ENDBR .L_init_clmul: movdqu (%rsi),%xmm2 - pshufd $78,%xmm2,%xmm2 + pshufd $78,%xmm2,%xmm2 // dword swap - - pshufd $255,%xmm2,%xmm4 +// <<1 twist + pshufd $255,%xmm2,%xmm4 // broadcast uppermost dword movdqa %xmm2,%xmm3 psllq $1,%xmm2 - pxor %xmm5,%xmm5 + pxor %xmm5,%xmm5 // psrlq $63,%xmm3 - pcmpgtd %xmm4,%xmm5 + pcmpgtd %xmm4,%xmm5 // broadcast carry bit pslldq $8,%xmm3 - por %xmm3,%xmm2 - + por %xmm3,%xmm2 // H<<=1 +// magic reduction pand .L0x1c2_polynomial(%rip),%xmm5 - pxor %xmm5,%xmm2 - + pxor %xmm5,%xmm2 // if(carry) H^=0x1c2_polynomial +// calculate H^2 pshufd $78,%xmm2,%xmm6 movdqa %xmm2,%xmm0 pxor %xmm2,%xmm6 - movdqa %xmm0,%xmm1 + movdqa %xmm0,%xmm1 // pshufd $78,%xmm0,%xmm3 - pxor %xmm0,%xmm3 - pclmulqdq $0x00,%xmm2,%xmm0 - pclmulqdq $0x11,%xmm2,%xmm1 - pclmulqdq $0x00,%xmm6,%xmm3 - pxor %xmm0,%xmm3 - pxor %xmm1,%xmm3 - - movdqa %xmm3,%xmm4 + pxor %xmm0,%xmm3 // + pclmulqdq $0x00,%xmm2,%xmm0 // ###### + pclmulqdq $0x11,%xmm2,%xmm1 // ###### + pclmulqdq $0x00,%xmm6,%xmm3 // ###### + pxor %xmm0,%xmm3 // + pxor %xmm1,%xmm3 // + + movdqa %xmm3,%xmm4 // psrldq $8,%xmm3 - pslldq $8,%xmm4 + pslldq $8,%xmm4 // pxor %xmm3,%xmm1 - pxor %xmm4,%xmm0 - - movdqa %xmm0,%xmm4 + pxor %xmm4,%xmm0 // +// 1st phase + movdqa %xmm0,%xmm4 // movdqa %xmm0,%xmm3 psllq $5,%xmm0 - pxor %xmm0,%xmm3 + pxor %xmm0,%xmm3 // psllq $1,%xmm0 - pxor %xmm3,%xmm0 - psllq $57,%xmm0 - movdqa %xmm0,%xmm3 + pxor %xmm3,%xmm0 // + psllq $57,%xmm0 // + movdqa %xmm0,%xmm3 // pslldq $8,%xmm0 - psrldq $8,%xmm3 + psrldq $8,%xmm3 // pxor %xmm4,%xmm0 - pxor %xmm3,%xmm1 - + pxor %xmm3,%xmm1 // +// 2nd phase movdqa %xmm0,%xmm4 psrlq $1,%xmm0 - pxor %xmm4,%xmm1 + pxor %xmm4,%xmm1 // pxor %xmm0,%xmm4 psrlq $5,%xmm0 - pxor %xmm4,%xmm0 - psrlq $1,%xmm0 - pxor %xmm1,%xmm0 + pxor %xmm4,%xmm0 // + psrlq $1,%xmm0 // + pxor %xmm1,%xmm0 // pshufd $78,%xmm2,%xmm3 pshufd $78,%xmm0,%xmm4 - pxor %xmm2,%xmm3 - movdqu %xmm2,0(%rdi) - pxor %xmm0,%xmm4 - movdqu %xmm0,16(%rdi) - palignr $8,%xmm3,%xmm4 - movdqu %xmm4,32(%rdi) - movdqa %xmm0,%xmm1 + pxor %xmm2,%xmm3 // Karatsuba pre-processing + movdqu %xmm2,0(%rdi) // save H + pxor %xmm0,%xmm4 // Karatsuba pre-processing + movdqu %xmm0,16(%rdi) // save H^2 + palignr $8,%xmm3,%xmm4 // low part is H.lo^H.hi... + movdqu %xmm4,32(%rdi) // save Karatsuba "salt" + movdqa %xmm0,%xmm1 // pshufd $78,%xmm0,%xmm3 - pxor %xmm0,%xmm3 - pclmulqdq $0x00,%xmm2,%xmm0 - pclmulqdq $0x11,%xmm2,%xmm1 - pclmulqdq $0x00,%xmm6,%xmm3 - pxor %xmm0,%xmm3 - pxor %xmm1,%xmm3 - - movdqa %xmm3,%xmm4 + pxor %xmm0,%xmm3 // + pclmulqdq $0x00,%xmm2,%xmm0 // ###### + pclmulqdq $0x11,%xmm2,%xmm1 // ###### + pclmulqdq $0x00,%xmm6,%xmm3 // ###### + pxor %xmm0,%xmm3 // + pxor %xmm1,%xmm3 // + + movdqa %xmm3,%xmm4 // psrldq $8,%xmm3 - pslldq $8,%xmm4 + pslldq $8,%xmm4 // pxor %xmm3,%xmm1 - pxor %xmm4,%xmm0 - - movdqa %xmm0,%xmm4 + pxor %xmm4,%xmm0 // +// 1st phase + movdqa %xmm0,%xmm4 // movdqa %xmm0,%xmm3 psllq $5,%xmm0 - pxor %xmm0,%xmm3 + pxor %xmm0,%xmm3 // psllq $1,%xmm0 - pxor %xmm3,%xmm0 - psllq $57,%xmm0 - movdqa %xmm0,%xmm3 + pxor %xmm3,%xmm0 // + psllq $57,%xmm0 // + movdqa %xmm0,%xmm3 // pslldq $8,%xmm0 - psrldq $8,%xmm3 + psrldq $8,%xmm3 // pxor %xmm4,%xmm0 - pxor %xmm3,%xmm1 - + pxor %xmm3,%xmm1 // +// 2nd phase movdqa %xmm0,%xmm4 psrlq $1,%xmm0 - pxor %xmm4,%xmm1 + pxor %xmm4,%xmm1 // pxor %xmm0,%xmm4 psrlq $5,%xmm0 - pxor %xmm4,%xmm0 - psrlq $1,%xmm0 - pxor %xmm1,%xmm0 + pxor %xmm4,%xmm0 // + psrlq $1,%xmm0 // + pxor %xmm1,%xmm0 // movdqa %xmm0,%xmm5 - movdqa %xmm0,%xmm1 + movdqa %xmm0,%xmm1 // pshufd $78,%xmm0,%xmm3 - pxor %xmm0,%xmm3 - pclmulqdq $0x00,%xmm2,%xmm0 - pclmulqdq $0x11,%xmm2,%xmm1 - pclmulqdq $0x00,%xmm6,%xmm3 - pxor %xmm0,%xmm3 - pxor %xmm1,%xmm3 - - movdqa %xmm3,%xmm4 + pxor %xmm0,%xmm3 // + pclmulqdq $0x00,%xmm2,%xmm0 // ###### + pclmulqdq $0x11,%xmm2,%xmm1 // ###### + pclmulqdq $0x00,%xmm6,%xmm3 // ###### + pxor %xmm0,%xmm3 // + pxor %xmm1,%xmm3 // + + movdqa %xmm3,%xmm4 // psrldq $8,%xmm3 - pslldq $8,%xmm4 + pslldq $8,%xmm4 // pxor %xmm3,%xmm1 - pxor %xmm4,%xmm0 - - movdqa %xmm0,%xmm4 + pxor %xmm4,%xmm0 // +// 1st phase + movdqa %xmm0,%xmm4 // movdqa %xmm0,%xmm3 psllq $5,%xmm0 - pxor %xmm0,%xmm3 + pxor %xmm0,%xmm3 // psllq $1,%xmm0 - pxor %xmm3,%xmm0 - psllq $57,%xmm0 - movdqa %xmm0,%xmm3 + pxor %xmm3,%xmm0 // + psllq $57,%xmm0 // + movdqa %xmm0,%xmm3 // pslldq $8,%xmm0 - psrldq $8,%xmm3 + psrldq $8,%xmm3 // pxor %xmm4,%xmm0 - pxor %xmm3,%xmm1 - + pxor %xmm3,%xmm1 // +// 2nd phase movdqa %xmm0,%xmm4 psrlq $1,%xmm0 - pxor %xmm4,%xmm1 + pxor %xmm4,%xmm1 // pxor %xmm0,%xmm4 psrlq $5,%xmm0 - pxor %xmm4,%xmm0 - psrlq $1,%xmm0 - pxor %xmm1,%xmm0 + pxor %xmm4,%xmm0 // + psrlq $1,%xmm0 // + pxor %xmm1,%xmm0 // pshufd $78,%xmm5,%xmm3 pshufd $78,%xmm0,%xmm4 - pxor %xmm5,%xmm3 - movdqu %xmm5,48(%rdi) - pxor %xmm0,%xmm4 - movdqu %xmm0,64(%rdi) - palignr $8,%xmm3,%xmm4 - movdqu %xmm4,80(%rdi) + pxor %xmm5,%xmm3 // Karatsuba pre-processing + movdqu %xmm5,48(%rdi) // save H^3 + pxor %xmm0,%xmm4 // Karatsuba pre-processing + movdqu %xmm0,64(%rdi) // save H^4 + palignr $8,%xmm3,%xmm4 // low part is H^3.lo^H^3.hi... + movdqu %xmm4,80(%rdi) // save Karatsuba "salt" ret .cfi_endproc @@ -180,43 +180,43 @@ _CET_ENDBR movdqu (%rsi),%xmm2 movdqu 32(%rsi),%xmm4 pshufb %xmm5,%xmm0 - movdqa %xmm0,%xmm1 + movdqa %xmm0,%xmm1 // pshufd $78,%xmm0,%xmm3 - pxor %xmm0,%xmm3 - pclmulqdq $0x00,%xmm2,%xmm0 - pclmulqdq $0x11,%xmm2,%xmm1 - pclmulqdq $0x00,%xmm4,%xmm3 - pxor %xmm0,%xmm3 - pxor %xmm1,%xmm3 - - movdqa %xmm3,%xmm4 + pxor %xmm0,%xmm3 // + pclmulqdq $0x00,%xmm2,%xmm0 // ###### + pclmulqdq $0x11,%xmm2,%xmm1 // ###### + pclmulqdq $0x00,%xmm4,%xmm3 // ###### + pxor %xmm0,%xmm3 // + pxor %xmm1,%xmm3 // + + movdqa %xmm3,%xmm4 // psrldq $8,%xmm3 - pslldq $8,%xmm4 + pslldq $8,%xmm4 // pxor %xmm3,%xmm1 - pxor %xmm4,%xmm0 - - movdqa %xmm0,%xmm4 + pxor %xmm4,%xmm0 // +// 1st phase + movdqa %xmm0,%xmm4 // movdqa %xmm0,%xmm3 psllq $5,%xmm0 - pxor %xmm0,%xmm3 + pxor %xmm0,%xmm3 // psllq $1,%xmm0 - pxor %xmm3,%xmm0 - psllq $57,%xmm0 - movdqa %xmm0,%xmm3 + pxor %xmm3,%xmm0 // + psllq $57,%xmm0 // + movdqa %xmm0,%xmm3 // pslldq $8,%xmm0 - psrldq $8,%xmm3 + psrldq $8,%xmm3 // pxor %xmm4,%xmm0 - pxor %xmm3,%xmm1 - + pxor %xmm3,%xmm1 // +// 2nd phase movdqa %xmm0,%xmm4 psrlq $1,%xmm0 - pxor %xmm4,%xmm1 + pxor %xmm4,%xmm1 // pxor %xmm0,%xmm4 psrlq $5,%xmm0 - pxor %xmm4,%xmm0 - psrlq $1,%xmm0 - pxor %xmm1,%xmm0 + pxor %xmm4,%xmm0 // + psrlq $1,%xmm0 // + pxor %xmm1,%xmm0 // pshufb %xmm5,%xmm0 movdqu %xmm0,(%rdi) ret @@ -246,13 +246,13 @@ _CET_ENDBR jb .Lskip4x subq $0x30,%rcx - movq $0xA040608020C0E000,%rax + movq $0xA040608020C0E000,%rax // ((7..0)·0xE0)&0xff movdqu 48(%rsi),%xmm14 movdqu 64(%rsi),%xmm15 - - - +// ###### +// Xi+4 =[(H*Ii+3) + (H^2*Ii+2) + (H^3*Ii+1) + H^4*(Ii+Xi)] mod P +// movdqu 48(%rdx),%xmm3 movdqu 32(%rdx),%xmm11 pshufb %xmm10,%xmm3 @@ -317,33 +317,33 @@ _CET_ENDBR pclmulqdq $0x00,%xmm2,%xmm11 pshufd $78,%xmm3,%xmm4 - pxor %xmm0,%xmm8 + pxor %xmm0,%xmm8 // aggregated Karatsuba post-processing movdqa %xmm3,%xmm5 - pxor %xmm1,%xmm8 + pxor %xmm1,%xmm8 // pxor %xmm3,%xmm4 - movdqa %xmm8,%xmm9 + movdqa %xmm8,%xmm9 // pclmulqdq $0x11,%xmm2,%xmm13 pslldq $8,%xmm8 - psrldq $8,%xmm9 + psrldq $8,%xmm9 // pxor %xmm8,%xmm0 movdqa .L7_mask(%rip),%xmm8 - pxor %xmm9,%xmm1 + pxor %xmm9,%xmm1 // movq %rax,%xmm9 - pand %xmm0,%xmm8 - pshufb %xmm8,%xmm9 - pxor %xmm0,%xmm9 + pand %xmm0,%xmm8 // 1st phase + pshufb %xmm8,%xmm9 // + pxor %xmm0,%xmm9 // pclmulqdq $0x00,%xmm7,%xmm12 - psllq $57,%xmm9 - movdqa %xmm9,%xmm8 + psllq $57,%xmm9 // + movdqa %xmm9,%xmm8 // pslldq $8,%xmm9 pclmulqdq $0x00,%xmm6,%xmm3 - psrldq $8,%xmm8 + psrldq $8,%xmm8 // pxor %xmm9,%xmm0 - pxor %xmm8,%xmm1 + pxor %xmm8,%xmm1 // movdqu 0(%rdx),%xmm8 - movdqa %xmm0,%xmm9 + movdqa %xmm0,%xmm9 // 2nd phase psrlq $1,%xmm0 pclmulqdq $0x11,%xmm6,%xmm5 xorps %xmm11,%xmm3 @@ -353,19 +353,19 @@ _CET_ENDBR xorps %xmm13,%xmm5 movups 80(%rsi),%xmm7 pshufb %xmm10,%xmm8 - pxor %xmm9,%xmm1 + pxor %xmm9,%xmm1 // pxor %xmm0,%xmm9 psrlq $5,%xmm0 movdqa %xmm11,%xmm13 pxor %xmm12,%xmm4 pshufd $78,%xmm11,%xmm12 - pxor %xmm9,%xmm0 + pxor %xmm9,%xmm0 // pxor %xmm8,%xmm1 pxor %xmm11,%xmm12 pclmulqdq $0x00,%xmm14,%xmm11 - psrlq $1,%xmm0 - pxor %xmm1,%xmm0 + psrlq $1,%xmm0 // + pxor %xmm1,%xmm0 // movdqa %xmm0,%xmm1 pclmulqdq $0x11,%xmm14,%xmm13 xorps %xmm11,%xmm3 @@ -386,56 +386,56 @@ _CET_ENDBR xorps %xmm12,%xmm4 xorps %xmm3,%xmm0 xorps %xmm5,%xmm1 - pxor %xmm0,%xmm1 + pxor %xmm0,%xmm1 // aggregated Karatsuba post-processing pxor %xmm4,%xmm8 - pxor %xmm1,%xmm8 + pxor %xmm1,%xmm8 // pxor %xmm0,%xmm1 - movdqa %xmm8,%xmm9 + movdqa %xmm8,%xmm9 // psrldq $8,%xmm8 - pslldq $8,%xmm9 + pslldq $8,%xmm9 // pxor %xmm8,%xmm1 - pxor %xmm9,%xmm0 - - movdqa %xmm0,%xmm4 + pxor %xmm9,%xmm0 // +// 1st phase + movdqa %xmm0,%xmm4 // movdqa %xmm0,%xmm3 psllq $5,%xmm0 - pxor %xmm0,%xmm3 + pxor %xmm0,%xmm3 // psllq $1,%xmm0 - pxor %xmm3,%xmm0 - psllq $57,%xmm0 - movdqa %xmm0,%xmm3 + pxor %xmm3,%xmm0 // + psllq $57,%xmm0 // + movdqa %xmm0,%xmm3 // pslldq $8,%xmm0 - psrldq $8,%xmm3 + psrldq $8,%xmm3 // pxor %xmm4,%xmm0 - pxor %xmm3,%xmm1 - + pxor %xmm3,%xmm1 // +// 2nd phase movdqa %xmm0,%xmm4 psrlq $1,%xmm0 - pxor %xmm4,%xmm1 + pxor %xmm4,%xmm1 // pxor %xmm0,%xmm4 psrlq $5,%xmm0 - pxor %xmm4,%xmm0 - psrlq $1,%xmm0 - pxor %xmm1,%xmm0 + pxor %xmm4,%xmm0 // + psrlq $1,%xmm0 // + pxor %xmm1,%xmm0 // addq $0x40,%rcx jz .Ldone movdqu 32(%rsi),%xmm7 subq $0x10,%rcx jz .Lodd_tail .Lskip4x: - - - - - - movdqu (%rdx),%xmm8 - movdqu 16(%rdx),%xmm3 +// ###### +// Xi+2 =[H*(Ii+1 + Xi+1)] mod P = +// [(H*Ii+1) + (H*Xi+1)] mod P = +// [(H*Ii+1) + H^2*(Ii+Xi)] mod P +// + movdqu (%rdx),%xmm8 // Ii + movdqu 16(%rdx),%xmm3 // Ii+1 pshufb %xmm10,%xmm8 pshufb %xmm10,%xmm3 - pxor %xmm8,%xmm0 + pxor %xmm8,%xmm0 // Ii+Xi movdqa %xmm3,%xmm5 pshufd $78,%xmm3,%xmm4 @@ -444,7 +444,7 @@ _CET_ENDBR pclmulqdq $0x11,%xmm2,%xmm5 pclmulqdq $0x00,%xmm7,%xmm4 - leaq 32(%rdx),%rdx + leaq 32(%rdx),%rdx // i+=2 nop subq $0x20,%rcx jbe .Leven_tail @@ -455,59 +455,59 @@ _CET_ENDBR .Lmod_loop: movdqa %xmm0,%xmm1 movdqa %xmm4,%xmm8 - pshufd $78,%xmm0,%xmm4 - pxor %xmm0,%xmm4 + pshufd $78,%xmm0,%xmm4 // + pxor %xmm0,%xmm4 // pclmulqdq $0x00,%xmm6,%xmm0 pclmulqdq $0x11,%xmm6,%xmm1 pclmulqdq $0x10,%xmm7,%xmm4 - pxor %xmm3,%xmm0 + pxor %xmm3,%xmm0 // (H*Ii+1) + H^2*(Ii+Xi) pxor %xmm5,%xmm1 - movdqu (%rdx),%xmm9 - pxor %xmm0,%xmm8 + movdqu (%rdx),%xmm9 // Ii + pxor %xmm0,%xmm8 // aggregated Karatsuba post-processing pshufb %xmm10,%xmm9 - movdqu 16(%rdx),%xmm3 + movdqu 16(%rdx),%xmm3 // Ii+1 pxor %xmm1,%xmm8 - pxor %xmm9,%xmm1 + pxor %xmm9,%xmm1 // "Ii+Xi", consume early pxor %xmm8,%xmm4 pshufb %xmm10,%xmm3 - movdqa %xmm4,%xmm8 + movdqa %xmm4,%xmm8 // psrldq $8,%xmm8 - pslldq $8,%xmm4 + pslldq $8,%xmm4 // pxor %xmm8,%xmm1 - pxor %xmm4,%xmm0 + pxor %xmm4,%xmm0 // - movdqa %xmm3,%xmm5 + movdqa %xmm3,%xmm5 // - movdqa %xmm0,%xmm9 + movdqa %xmm0,%xmm9 // 1st phase movdqa %xmm0,%xmm8 psllq $5,%xmm0 - pxor %xmm0,%xmm8 - pclmulqdq $0x00,%xmm2,%xmm3 + pxor %xmm0,%xmm8 // + pclmulqdq $0x00,%xmm2,%xmm3 // ###### psllq $1,%xmm0 - pxor %xmm8,%xmm0 - psllq $57,%xmm0 - movdqa %xmm0,%xmm8 + pxor %xmm8,%xmm0 // + psllq $57,%xmm0 // + movdqa %xmm0,%xmm8 // pslldq $8,%xmm0 - psrldq $8,%xmm8 + psrldq $8,%xmm8 // pxor %xmm9,%xmm0 pshufd $78,%xmm5,%xmm4 - pxor %xmm8,%xmm1 - pxor %xmm5,%xmm4 + pxor %xmm8,%xmm1 // + pxor %xmm5,%xmm4 // - movdqa %xmm0,%xmm9 + movdqa %xmm0,%xmm9 // 2nd phase psrlq $1,%xmm0 - pclmulqdq $0x11,%xmm2,%xmm5 - pxor %xmm9,%xmm1 + pclmulqdq $0x11,%xmm2,%xmm5 // ###### + pxor %xmm9,%xmm1 // pxor %xmm0,%xmm9 psrlq $5,%xmm0 - pxor %xmm9,%xmm0 + pxor %xmm9,%xmm0 // leaq 32(%rdx),%rdx - psrlq $1,%xmm0 - pclmulqdq $0x00,%xmm7,%xmm4 - pxor %xmm1,%xmm0 + psrlq $1,%xmm0 // + pclmulqdq $0x00,%xmm7,%xmm4 // ###### + pxor %xmm1,%xmm0 // subq $0x20,%rcx ja .Lmod_loop @@ -515,90 +515,90 @@ _CET_ENDBR .Leven_tail: movdqa %xmm0,%xmm1 movdqa %xmm4,%xmm8 - pshufd $78,%xmm0,%xmm4 - pxor %xmm0,%xmm4 + pshufd $78,%xmm0,%xmm4 // + pxor %xmm0,%xmm4 // pclmulqdq $0x00,%xmm6,%xmm0 pclmulqdq $0x11,%xmm6,%xmm1 pclmulqdq $0x10,%xmm7,%xmm4 - pxor %xmm3,%xmm0 + pxor %xmm3,%xmm0 // (H*Ii+1) + H^2*(Ii+Xi) pxor %xmm5,%xmm1 pxor %xmm0,%xmm8 pxor %xmm1,%xmm8 pxor %xmm8,%xmm4 - movdqa %xmm4,%xmm8 + movdqa %xmm4,%xmm8 // psrldq $8,%xmm8 - pslldq $8,%xmm4 + pslldq $8,%xmm4 // pxor %xmm8,%xmm1 - pxor %xmm4,%xmm0 - - movdqa %xmm0,%xmm4 + pxor %xmm4,%xmm0 // +// 1st phase + movdqa %xmm0,%xmm4 // movdqa %xmm0,%xmm3 psllq $5,%xmm0 - pxor %xmm0,%xmm3 + pxor %xmm0,%xmm3 // psllq $1,%xmm0 - pxor %xmm3,%xmm0 - psllq $57,%xmm0 - movdqa %xmm0,%xmm3 + pxor %xmm3,%xmm0 // + psllq $57,%xmm0 // + movdqa %xmm0,%xmm3 // pslldq $8,%xmm0 - psrldq $8,%xmm3 + psrldq $8,%xmm3 // pxor %xmm4,%xmm0 - pxor %xmm3,%xmm1 - + pxor %xmm3,%xmm1 // +// 2nd phase movdqa %xmm0,%xmm4 psrlq $1,%xmm0 - pxor %xmm4,%xmm1 + pxor %xmm4,%xmm1 // pxor %xmm0,%xmm4 psrlq $5,%xmm0 - pxor %xmm4,%xmm0 - psrlq $1,%xmm0 - pxor %xmm1,%xmm0 + pxor %xmm4,%xmm0 // + psrlq $1,%xmm0 // + pxor %xmm1,%xmm0 // testq %rcx,%rcx jnz .Ldone .Lodd_tail: - movdqu (%rdx),%xmm8 + movdqu (%rdx),%xmm8 // Ii pshufb %xmm10,%xmm8 - pxor %xmm8,%xmm0 - movdqa %xmm0,%xmm1 + pxor %xmm8,%xmm0 // Ii+Xi + movdqa %xmm0,%xmm1 // pshufd $78,%xmm0,%xmm3 - pxor %xmm0,%xmm3 - pclmulqdq $0x00,%xmm2,%xmm0 - pclmulqdq $0x11,%xmm2,%xmm1 - pclmulqdq $0x00,%xmm7,%xmm3 - pxor %xmm0,%xmm3 - pxor %xmm1,%xmm3 - - movdqa %xmm3,%xmm4 + pxor %xmm0,%xmm3 // + pclmulqdq $0x00,%xmm2,%xmm0 // ###### + pclmulqdq $0x11,%xmm2,%xmm1 // ###### + pclmulqdq $0x00,%xmm7,%xmm3 // ###### + pxor %xmm0,%xmm3 // + pxor %xmm1,%xmm3 // + + movdqa %xmm3,%xmm4 // psrldq $8,%xmm3 - pslldq $8,%xmm4 + pslldq $8,%xmm4 // pxor %xmm3,%xmm1 - pxor %xmm4,%xmm0 - - movdqa %xmm0,%xmm4 + pxor %xmm4,%xmm0 // +// 1st phase + movdqa %xmm0,%xmm4 // movdqa %xmm0,%xmm3 psllq $5,%xmm0 - pxor %xmm0,%xmm3 + pxor %xmm0,%xmm3 // psllq $1,%xmm0 - pxor %xmm3,%xmm0 - psllq $57,%xmm0 - movdqa %xmm0,%xmm3 + pxor %xmm3,%xmm0 // + psllq $57,%xmm0 // + movdqa %xmm0,%xmm3 // pslldq $8,%xmm0 - psrldq $8,%xmm3 + psrldq $8,%xmm3 // pxor %xmm4,%xmm0 - pxor %xmm3,%xmm1 - + pxor %xmm3,%xmm1 // +// 2nd phase movdqa %xmm0,%xmm4 psrlq $1,%xmm0 - pxor %xmm4,%xmm1 + pxor %xmm4,%xmm1 // pxor %xmm0,%xmm4 psrlq $5,%xmm0 - pxor %xmm4,%xmm0 - psrlq $1,%xmm0 - pxor %xmm1,%xmm0 + pxor %xmm4,%xmm0 // + psrlq $1,%xmm0 // + pxor %xmm1,%xmm0 // .Ldone: pshufb %xmm10,%xmm0 movdqu %xmm0,(%rdi) @@ -617,101 +617,101 @@ _CET_ENDBR vzeroupper vmovdqu (%rsi),%xmm2 - vpshufd $78,%xmm2,%xmm2 - + vpshufd $78,%xmm2,%xmm2 // dword swap - vpshufd $255,%xmm2,%xmm4 +// <<1 twist + vpshufd $255,%xmm2,%xmm4 // broadcast uppermost dword vpsrlq $63,%xmm2,%xmm3 vpsllq $1,%xmm2,%xmm2 - vpxor %xmm5,%xmm5,%xmm5 - vpcmpgtd %xmm4,%xmm5,%xmm5 + vpxor %xmm5,%xmm5,%xmm5 // + vpcmpgtd %xmm4,%xmm5,%xmm5 // broadcast carry bit vpslldq $8,%xmm3,%xmm3 - vpor %xmm3,%xmm2,%xmm2 - + vpor %xmm3,%xmm2,%xmm2 // H<<=1 +// magic reduction vpand .L0x1c2_polynomial(%rip),%xmm5,%xmm5 - vpxor %xmm5,%xmm2,%xmm2 + vpxor %xmm5,%xmm2,%xmm2 // if(carry) H^=0x1c2_polynomial vpunpckhqdq %xmm2,%xmm2,%xmm6 vmovdqa %xmm2,%xmm0 vpxor %xmm2,%xmm6,%xmm6 - movq $4,%r10 + movq $4,%r10 // up to H^8 jmp .Linit_start_avx .align 32 .Linit_loop_avx: - vpalignr $8,%xmm3,%xmm4,%xmm5 - vmovdqu %xmm5,-16(%rdi) + vpalignr $8,%xmm3,%xmm4,%xmm5 // low part is H.lo^H.hi... + vmovdqu %xmm5,-16(%rdi) // save Karatsuba "salt" vpunpckhqdq %xmm0,%xmm0,%xmm3 - vpxor %xmm0,%xmm3,%xmm3 - vpclmulqdq $0x11,%xmm2,%xmm0,%xmm1 - vpclmulqdq $0x00,%xmm2,%xmm0,%xmm0 - vpclmulqdq $0x00,%xmm6,%xmm3,%xmm3 - vpxor %xmm0,%xmm1,%xmm4 - vpxor %xmm4,%xmm3,%xmm3 - - vpslldq $8,%xmm3,%xmm4 + vpxor %xmm0,%xmm3,%xmm3 // + vpclmulqdq $0x11,%xmm2,%xmm0,%xmm1 // ###### + vpclmulqdq $0x00,%xmm2,%xmm0,%xmm0 // ###### + vpclmulqdq $0x00,%xmm6,%xmm3,%xmm3 // ###### + vpxor %xmm0,%xmm1,%xmm4 // + vpxor %xmm4,%xmm3,%xmm3 // + + vpslldq $8,%xmm3,%xmm4 // vpsrldq $8,%xmm3,%xmm3 - vpxor %xmm4,%xmm0,%xmm0 + vpxor %xmm4,%xmm0,%xmm0 // vpxor %xmm3,%xmm1,%xmm1 - vpsllq $57,%xmm0,%xmm3 + vpsllq $57,%xmm0,%xmm3 // 1st phase vpsllq $62,%xmm0,%xmm4 - vpxor %xmm3,%xmm4,%xmm4 + vpxor %xmm3,%xmm4,%xmm4 // vpsllq $63,%xmm0,%xmm3 - vpxor %xmm3,%xmm4,%xmm4 - vpslldq $8,%xmm4,%xmm3 + vpxor %xmm3,%xmm4,%xmm4 // + vpslldq $8,%xmm4,%xmm3 // vpsrldq $8,%xmm4,%xmm4 - vpxor %xmm3,%xmm0,%xmm0 + vpxor %xmm3,%xmm0,%xmm0 // vpxor %xmm4,%xmm1,%xmm1 - vpsrlq $1,%xmm0,%xmm4 + vpsrlq $1,%xmm0,%xmm4 // 2nd phase vpxor %xmm0,%xmm1,%xmm1 - vpxor %xmm4,%xmm0,%xmm0 + vpxor %xmm4,%xmm0,%xmm0 // vpsrlq $5,%xmm4,%xmm4 - vpxor %xmm4,%xmm0,%xmm0 - vpsrlq $1,%xmm0,%xmm0 - vpxor %xmm1,%xmm0,%xmm0 + vpxor %xmm4,%xmm0,%xmm0 // + vpsrlq $1,%xmm0,%xmm0 // + vpxor %xmm1,%xmm0,%xmm0 // .Linit_start_avx: vmovdqa %xmm0,%xmm5 vpunpckhqdq %xmm0,%xmm0,%xmm3 - vpxor %xmm0,%xmm3,%xmm3 - vpclmulqdq $0x11,%xmm2,%xmm0,%xmm1 - vpclmulqdq $0x00,%xmm2,%xmm0,%xmm0 - vpclmulqdq $0x00,%xmm6,%xmm3,%xmm3 - vpxor %xmm0,%xmm1,%xmm4 - vpxor %xmm4,%xmm3,%xmm3 - - vpslldq $8,%xmm3,%xmm4 + vpxor %xmm0,%xmm3,%xmm3 // + vpclmulqdq $0x11,%xmm2,%xmm0,%xmm1 // ###### + vpclmulqdq $0x00,%xmm2,%xmm0,%xmm0 // ###### + vpclmulqdq $0x00,%xmm6,%xmm3,%xmm3 // ###### + vpxor %xmm0,%xmm1,%xmm4 // + vpxor %xmm4,%xmm3,%xmm3 // + + vpslldq $8,%xmm3,%xmm4 // vpsrldq $8,%xmm3,%xmm3 - vpxor %xmm4,%xmm0,%xmm0 + vpxor %xmm4,%xmm0,%xmm0 // vpxor %xmm3,%xmm1,%xmm1 - vpsllq $57,%xmm0,%xmm3 + vpsllq $57,%xmm0,%xmm3 // 1st phase vpsllq $62,%xmm0,%xmm4 - vpxor %xmm3,%xmm4,%xmm4 + vpxor %xmm3,%xmm4,%xmm4 // vpsllq $63,%xmm0,%xmm3 - vpxor %xmm3,%xmm4,%xmm4 - vpslldq $8,%xmm4,%xmm3 + vpxor %xmm3,%xmm4,%xmm4 // + vpslldq $8,%xmm4,%xmm3 // vpsrldq $8,%xmm4,%xmm4 - vpxor %xmm3,%xmm0,%xmm0 + vpxor %xmm3,%xmm0,%xmm0 // vpxor %xmm4,%xmm1,%xmm1 - vpsrlq $1,%xmm0,%xmm4 + vpsrlq $1,%xmm0,%xmm4 // 2nd phase vpxor %xmm0,%xmm1,%xmm1 - vpxor %xmm4,%xmm0,%xmm0 + vpxor %xmm4,%xmm0,%xmm0 // vpsrlq $5,%xmm4,%xmm4 - vpxor %xmm4,%xmm0,%xmm0 - vpsrlq $1,%xmm0,%xmm0 - vpxor %xmm1,%xmm0,%xmm0 + vpxor %xmm4,%xmm0,%xmm0 // + vpsrlq $1,%xmm0,%xmm0 // + vpxor %xmm1,%xmm0,%xmm0 // vpshufd $78,%xmm5,%xmm3 vpshufd $78,%xmm0,%xmm4 - vpxor %xmm5,%xmm3,%xmm3 - vmovdqu %xmm5,0(%rdi) - vpxor %xmm0,%xmm4,%xmm4 - vmovdqu %xmm0,16(%rdi) + vpxor %xmm5,%xmm3,%xmm3 // Karatsuba pre-processing + vmovdqu %xmm5,0(%rdi) // save H^1,3,5,7 + vpxor %xmm0,%xmm4,%xmm4 // Karatsuba pre-processing + vmovdqu %xmm0,16(%rdi) // save H^2,4,6,8 leaq 48(%rdi),%rdi subq $1,%r10 jnz .Linit_loop_avx - vpalignr $8,%xmm4,%xmm3,%xmm5 + vpalignr $8,%xmm4,%xmm3,%xmm5 // last "salt" is flipped vmovdqu %xmm5,-16(%rdi) vzeroupper @@ -739,29 +739,29 @@ gcm_ghash_avx: _CET_ENDBR vzeroupper - vmovdqu (%rdi),%xmm10 + vmovdqu (%rdi),%xmm10 // load %xmm10 leaq .L0x1c2_polynomial(%rip),%r10 - leaq 64(%rsi),%rsi + leaq 64(%rsi),%rsi // size optimization vmovdqu .Lbswap_mask(%rip),%xmm13 vpshufb %xmm13,%xmm10,%xmm10 cmpq $0x80,%rcx jb .Lshort_avx subq $0x80,%rcx - vmovdqu 112(%rdx),%xmm14 - vmovdqu 0-64(%rsi),%xmm6 + vmovdqu 112(%rdx),%xmm14 // I[7] + vmovdqu 0-64(%rsi),%xmm6 // %xmm6^1 vpshufb %xmm13,%xmm14,%xmm14 vmovdqu 32-64(%rsi),%xmm7 vpunpckhqdq %xmm14,%xmm14,%xmm9 - vmovdqu 96(%rdx),%xmm15 + vmovdqu 96(%rdx),%xmm15 // I[6] vpclmulqdq $0x00,%xmm6,%xmm14,%xmm0 vpxor %xmm14,%xmm9,%xmm9 vpshufb %xmm13,%xmm15,%xmm15 vpclmulqdq $0x11,%xmm6,%xmm14,%xmm1 - vmovdqu 16-64(%rsi),%xmm6 + vmovdqu 16-64(%rsi),%xmm6 // %xmm6^2 vpunpckhqdq %xmm15,%xmm15,%xmm8 - vmovdqu 80(%rdx),%xmm14 + vmovdqu 80(%rdx),%xmm14 // I[5] vpclmulqdq $0x00,%xmm7,%xmm9,%xmm2 vpxor %xmm15,%xmm8,%xmm8 @@ -769,9 +769,9 @@ _CET_ENDBR vpclmulqdq $0x00,%xmm6,%xmm15,%xmm3 vpunpckhqdq %xmm14,%xmm14,%xmm9 vpclmulqdq $0x11,%xmm6,%xmm15,%xmm4 - vmovdqu 48-64(%rsi),%xmm6 + vmovdqu 48-64(%rsi),%xmm6 // %xmm6^3 vpxor %xmm14,%xmm9,%xmm9 - vmovdqu 64(%rdx),%xmm15 + vmovdqu 64(%rdx),%xmm15 // I[4] vpclmulqdq $0x10,%xmm7,%xmm8,%xmm5 vmovdqu 80-64(%rsi),%xmm7 @@ -781,56 +781,56 @@ _CET_ENDBR vpxor %xmm1,%xmm4,%xmm4 vpunpckhqdq %xmm15,%xmm15,%xmm8 vpclmulqdq $0x11,%xmm6,%xmm14,%xmm1 - vmovdqu 64-64(%rsi),%xmm6 + vmovdqu 64-64(%rsi),%xmm6 // %xmm6^4 vpxor %xmm2,%xmm5,%xmm5 vpclmulqdq $0x00,%xmm7,%xmm9,%xmm2 vpxor %xmm15,%xmm8,%xmm8 - vmovdqu 48(%rdx),%xmm14 + vmovdqu 48(%rdx),%xmm14 // I[3] vpxor %xmm3,%xmm0,%xmm0 vpclmulqdq $0x00,%xmm6,%xmm15,%xmm3 vpxor %xmm4,%xmm1,%xmm1 vpshufb %xmm13,%xmm14,%xmm14 vpclmulqdq $0x11,%xmm6,%xmm15,%xmm4 - vmovdqu 96-64(%rsi),%xmm6 + vmovdqu 96-64(%rsi),%xmm6 // %xmm6^5 vpxor %xmm5,%xmm2,%xmm2 vpunpckhqdq %xmm14,%xmm14,%xmm9 vpclmulqdq $0x10,%xmm7,%xmm8,%xmm5 vmovdqu 128-64(%rsi),%xmm7 vpxor %xmm14,%xmm9,%xmm9 - vmovdqu 32(%rdx),%xmm15 + vmovdqu 32(%rdx),%xmm15 // I[2] vpxor %xmm0,%xmm3,%xmm3 vpclmulqdq $0x00,%xmm6,%xmm14,%xmm0 vpxor %xmm1,%xmm4,%xmm4 vpshufb %xmm13,%xmm15,%xmm15 vpclmulqdq $0x11,%xmm6,%xmm14,%xmm1 - vmovdqu 112-64(%rsi),%xmm6 + vmovdqu 112-64(%rsi),%xmm6 // %xmm6^6 vpxor %xmm2,%xmm5,%xmm5 vpunpckhqdq %xmm15,%xmm15,%xmm8 vpclmulqdq $0x00,%xmm7,%xmm9,%xmm2 vpxor %xmm15,%xmm8,%xmm8 - vmovdqu 16(%rdx),%xmm14 + vmovdqu 16(%rdx),%xmm14 // I[1] vpxor %xmm3,%xmm0,%xmm0 vpclmulqdq $0x00,%xmm6,%xmm15,%xmm3 vpxor %xmm4,%xmm1,%xmm1 vpshufb %xmm13,%xmm14,%xmm14 vpclmulqdq $0x11,%xmm6,%xmm15,%xmm4 - vmovdqu 144-64(%rsi),%xmm6 + vmovdqu 144-64(%rsi),%xmm6 // %xmm6^7 vpxor %xmm5,%xmm2,%xmm2 vpunpckhqdq %xmm14,%xmm14,%xmm9 vpclmulqdq $0x10,%xmm7,%xmm8,%xmm5 vmovdqu 176-64(%rsi),%xmm7 vpxor %xmm14,%xmm9,%xmm9 - vmovdqu (%rdx),%xmm15 + vmovdqu (%rdx),%xmm15 // I[0] vpxor %xmm0,%xmm3,%xmm3 vpclmulqdq $0x00,%xmm6,%xmm14,%xmm0 vpxor %xmm1,%xmm4,%xmm4 vpshufb %xmm13,%xmm15,%xmm15 vpclmulqdq $0x11,%xmm6,%xmm14,%xmm1 - vmovdqu 160-64(%rsi),%xmm6 + vmovdqu 160-64(%rsi),%xmm6 // %xmm6^8 vpxor %xmm2,%xmm5,%xmm5 vpclmulqdq $0x10,%xmm7,%xmm9,%xmm2 @@ -838,41 +838,41 @@ _CET_ENDBR cmpq $0x80,%rcx jb .Ltail_avx - vpxor %xmm10,%xmm15,%xmm15 + vpxor %xmm10,%xmm15,%xmm15 // accumulate %xmm10 subq $0x80,%rcx jmp .Loop8x_avx .align 32 .Loop8x_avx: vpunpckhqdq %xmm15,%xmm15,%xmm8 - vmovdqu 112(%rdx),%xmm14 + vmovdqu 112(%rdx),%xmm14 // I[7] vpxor %xmm0,%xmm3,%xmm3 vpxor %xmm15,%xmm8,%xmm8 vpclmulqdq $0x00,%xmm6,%xmm15,%xmm10 vpshufb %xmm13,%xmm14,%xmm14 vpxor %xmm1,%xmm4,%xmm4 vpclmulqdq $0x11,%xmm6,%xmm15,%xmm11 - vmovdqu 0-64(%rsi),%xmm6 + vmovdqu 0-64(%rsi),%xmm6 // %xmm6^1 vpunpckhqdq %xmm14,%xmm14,%xmm9 vpxor %xmm2,%xmm5,%xmm5 vpclmulqdq $0x00,%xmm7,%xmm8,%xmm12 vmovdqu 32-64(%rsi),%xmm7 vpxor %xmm14,%xmm9,%xmm9 - vmovdqu 96(%rdx),%xmm15 + vmovdqu 96(%rdx),%xmm15 // I[6] vpclmulqdq $0x00,%xmm6,%xmm14,%xmm0 - vpxor %xmm3,%xmm10,%xmm10 + vpxor %xmm3,%xmm10,%xmm10 // collect result vpshufb %xmm13,%xmm15,%xmm15 vpclmulqdq $0x11,%xmm6,%xmm14,%xmm1 vxorps %xmm4,%xmm11,%xmm11 - vmovdqu 16-64(%rsi),%xmm6 + vmovdqu 16-64(%rsi),%xmm6 // %xmm6^2 vpunpckhqdq %xmm15,%xmm15,%xmm8 vpclmulqdq $0x00,%xmm7,%xmm9,%xmm2 vpxor %xmm5,%xmm12,%xmm12 vxorps %xmm15,%xmm8,%xmm8 - vmovdqu 80(%rdx),%xmm14 - vpxor %xmm10,%xmm12,%xmm12 + vmovdqu 80(%rdx),%xmm14 // I[5] + vpxor %xmm10,%xmm12,%xmm12 // aggregated Karatsuba post-processing vpclmulqdq $0x00,%xmm6,%xmm15,%xmm3 vpxor %xmm11,%xmm12,%xmm12 vpslldq $8,%xmm12,%xmm9 @@ -880,7 +880,7 @@ _CET_ENDBR vpclmulqdq $0x11,%xmm6,%xmm15,%xmm4 vpsrldq $8,%xmm12,%xmm12 vpxor %xmm9,%xmm10,%xmm10 - vmovdqu 48-64(%rsi),%xmm6 + vmovdqu 48-64(%rsi),%xmm6 // %xmm6^3 vpshufb %xmm13,%xmm14,%xmm14 vxorps %xmm12,%xmm11,%xmm11 vpxor %xmm1,%xmm4,%xmm4 @@ -890,26 +890,26 @@ _CET_ENDBR vpxor %xmm14,%xmm9,%xmm9 vpxor %xmm2,%xmm5,%xmm5 - vmovdqu 64(%rdx),%xmm15 - vpalignr $8,%xmm10,%xmm10,%xmm12 + vmovdqu 64(%rdx),%xmm15 // I[4] + vpalignr $8,%xmm10,%xmm10,%xmm12 // 1st phase vpclmulqdq $0x00,%xmm6,%xmm14,%xmm0 vpshufb %xmm13,%xmm15,%xmm15 vpxor %xmm3,%xmm0,%xmm0 vpclmulqdq $0x11,%xmm6,%xmm14,%xmm1 - vmovdqu 64-64(%rsi),%xmm6 + vmovdqu 64-64(%rsi),%xmm6 // %xmm6^4 vpunpckhqdq %xmm15,%xmm15,%xmm8 vpxor %xmm4,%xmm1,%xmm1 vpclmulqdq $0x00,%xmm7,%xmm9,%xmm2 vxorps %xmm15,%xmm8,%xmm8 vpxor %xmm5,%xmm2,%xmm2 - vmovdqu 48(%rdx),%xmm14 + vmovdqu 48(%rdx),%xmm14 // I[3] vpclmulqdq $0x10,(%r10),%xmm10,%xmm10 vpclmulqdq $0x00,%xmm6,%xmm15,%xmm3 vpshufb %xmm13,%xmm14,%xmm14 vpxor %xmm0,%xmm3,%xmm3 vpclmulqdq $0x11,%xmm6,%xmm15,%xmm4 - vmovdqu 96-64(%rsi),%xmm6 + vmovdqu 96-64(%rsi),%xmm6 // %xmm6^5 vpunpckhqdq %xmm14,%xmm14,%xmm9 vpxor %xmm1,%xmm4,%xmm4 vpclmulqdq $0x10,%xmm7,%xmm8,%xmm5 @@ -917,12 +917,12 @@ _CET_ENDBR vpxor %xmm14,%xmm9,%xmm9 vpxor %xmm2,%xmm5,%xmm5 - vmovdqu 32(%rdx),%xmm15 + vmovdqu 32(%rdx),%xmm15 // I[2] vpclmulqdq $0x00,%xmm6,%xmm14,%xmm0 vpshufb %xmm13,%xmm15,%xmm15 vpxor %xmm3,%xmm0,%xmm0 vpclmulqdq $0x11,%xmm6,%xmm14,%xmm1 - vmovdqu 112-64(%rsi),%xmm6 + vmovdqu 112-64(%rsi),%xmm6 // %xmm6^6 vpunpckhqdq %xmm15,%xmm15,%xmm8 vpxor %xmm4,%xmm1,%xmm1 vpclmulqdq $0x00,%xmm7,%xmm9,%xmm2 @@ -930,13 +930,13 @@ _CET_ENDBR vpxor %xmm5,%xmm2,%xmm2 vxorps %xmm12,%xmm10,%xmm10 - vmovdqu 16(%rdx),%xmm14 - vpalignr $8,%xmm10,%xmm10,%xmm12 + vmovdqu 16(%rdx),%xmm14 // I[1] + vpalignr $8,%xmm10,%xmm10,%xmm12 // 2nd phase vpclmulqdq $0x00,%xmm6,%xmm15,%xmm3 vpshufb %xmm13,%xmm14,%xmm14 vpxor %xmm0,%xmm3,%xmm3 vpclmulqdq $0x11,%xmm6,%xmm15,%xmm4 - vmovdqu 144-64(%rsi),%xmm6 + vmovdqu 144-64(%rsi),%xmm6 // %xmm6^7 vpclmulqdq $0x10,(%r10),%xmm10,%xmm10 vxorps %xmm11,%xmm12,%xmm12 vpunpckhqdq %xmm14,%xmm14,%xmm9 @@ -946,14 +946,14 @@ _CET_ENDBR vpxor %xmm14,%xmm9,%xmm9 vpxor %xmm2,%xmm5,%xmm5 - vmovdqu (%rdx),%xmm15 + vmovdqu (%rdx),%xmm15 // I[0] vpclmulqdq $0x00,%xmm6,%xmm14,%xmm0 vpshufb %xmm13,%xmm15,%xmm15 vpclmulqdq $0x11,%xmm6,%xmm14,%xmm1 - vmovdqu 160-64(%rsi),%xmm6 + vmovdqu 160-64(%rsi),%xmm6 // %xmm6^8 vpxor %xmm12,%xmm15,%xmm15 vpclmulqdq $0x10,%xmm7,%xmm9,%xmm2 - vpxor %xmm10,%xmm15,%xmm15 + vpxor %xmm10,%xmm15,%xmm15 // accumulate %xmm10 leaq 128(%rdx),%rdx subq $0x80,%rcx @@ -964,15 +964,15 @@ _CET_ENDBR .align 32 .Lshort_avx: - vmovdqu -16(%rdx,%rcx,1),%xmm14 + vmovdqu -16(%rdx,%rcx,1),%xmm14 // very last word leaq (%rdx,%rcx,1),%rdx - vmovdqu 0-64(%rsi),%xmm6 + vmovdqu 0-64(%rsi),%xmm6 // %xmm6^1 vmovdqu 32-64(%rsi),%xmm7 vpshufb %xmm13,%xmm14,%xmm15 - vmovdqa %xmm0,%xmm3 - vmovdqa %xmm1,%xmm4 - vmovdqa %xmm2,%xmm5 + vmovdqa %xmm0,%xmm3 // subtle way to zero %xmm3, + vmovdqa %xmm1,%xmm4 // %xmm4 and + vmovdqa %xmm2,%xmm5 // %xmm5 subq $0x10,%rcx jz .Ltail_avx @@ -983,7 +983,7 @@ _CET_ENDBR vmovdqu -32(%rdx),%xmm14 vpxor %xmm1,%xmm4,%xmm4 vpclmulqdq $0x11,%xmm6,%xmm15,%xmm1 - vmovdqu 16-64(%rsi),%xmm6 + vmovdqu 16-64(%rsi),%xmm6 // %xmm6^2 vpshufb %xmm13,%xmm14,%xmm15 vpxor %xmm2,%xmm5,%xmm5 vpclmulqdq $0x00,%xmm7,%xmm8,%xmm2 @@ -998,7 +998,7 @@ _CET_ENDBR vmovdqu -48(%rdx),%xmm14 vpxor %xmm1,%xmm4,%xmm4 vpclmulqdq $0x11,%xmm6,%xmm15,%xmm1 - vmovdqu 48-64(%rsi),%xmm6 + vmovdqu 48-64(%rsi),%xmm6 // %xmm6^3 vpshufb %xmm13,%xmm14,%xmm15 vpxor %xmm2,%xmm5,%xmm5 vpclmulqdq $0x00,%xmm7,%xmm8,%xmm2 @@ -1013,7 +1013,7 @@ _CET_ENDBR vmovdqu -64(%rdx),%xmm14 vpxor %xmm1,%xmm4,%xmm4 vpclmulqdq $0x11,%xmm6,%xmm15,%xmm1 - vmovdqu 64-64(%rsi),%xmm6 + vmovdqu 64-64(%rsi),%xmm6 // %xmm6^4 vpshufb %xmm13,%xmm14,%xmm15 vpxor %xmm2,%xmm5,%xmm5 vpclmulqdq $0x00,%xmm7,%xmm8,%xmm2 @@ -1028,7 +1028,7 @@ _CET_ENDBR vmovdqu -80(%rdx),%xmm14 vpxor %xmm1,%xmm4,%xmm4 vpclmulqdq $0x11,%xmm6,%xmm15,%xmm1 - vmovdqu 96-64(%rsi),%xmm6 + vmovdqu 96-64(%rsi),%xmm6 // %xmm6^5 vpshufb %xmm13,%xmm14,%xmm15 vpxor %xmm2,%xmm5,%xmm5 vpclmulqdq $0x00,%xmm7,%xmm8,%xmm2 @@ -1043,7 +1043,7 @@ _CET_ENDBR vmovdqu -96(%rdx),%xmm14 vpxor %xmm1,%xmm4,%xmm4 vpclmulqdq $0x11,%xmm6,%xmm15,%xmm1 - vmovdqu 112-64(%rsi),%xmm6 + vmovdqu 112-64(%rsi),%xmm6 // %xmm6^6 vpshufb %xmm13,%xmm14,%xmm15 vpxor %xmm2,%xmm5,%xmm5 vpclmulqdq $0x00,%xmm7,%xmm8,%xmm2 @@ -1058,7 +1058,7 @@ _CET_ENDBR vmovdqu -112(%rdx),%xmm14 vpxor %xmm1,%xmm4,%xmm4 vpclmulqdq $0x11,%xmm6,%xmm15,%xmm1 - vmovdqu 144-64(%rsi),%xmm6 + vmovdqu 144-64(%rsi),%xmm6 // %xmm6^7 vpshufb %xmm13,%xmm14,%xmm15 vpxor %xmm2,%xmm5,%xmm5 vpclmulqdq $0x00,%xmm7,%xmm8,%xmm2 @@ -1068,7 +1068,7 @@ _CET_ENDBR .align 32 .Ltail_avx: - vpxor %xmm10,%xmm15,%xmm15 + vpxor %xmm10,%xmm15,%xmm15 // accumulate %xmm10 .Ltail_no_xor_avx: vpunpckhqdq %xmm15,%xmm15,%xmm8 vpxor %xmm0,%xmm3,%xmm3 @@ -1085,18 +1085,18 @@ _CET_ENDBR vpxor %xmm1,%xmm4,%xmm11 vpxor %xmm2,%xmm5,%xmm5 - vpxor %xmm10,%xmm5,%xmm5 + vpxor %xmm10,%xmm5,%xmm5 // aggregated Karatsuba post-processing vpxor %xmm11,%xmm5,%xmm5 vpslldq $8,%xmm5,%xmm9 vpsrldq $8,%xmm5,%xmm5 vpxor %xmm9,%xmm10,%xmm10 vpxor %xmm5,%xmm11,%xmm11 - vpclmulqdq $0x10,%xmm12,%xmm10,%xmm9 + vpclmulqdq $0x10,%xmm12,%xmm10,%xmm9 // 1st phase vpalignr $8,%xmm10,%xmm10,%xmm10 vpxor %xmm9,%xmm10,%xmm10 - vpclmulqdq $0x10,%xmm12,%xmm10,%xmm9 + vpclmulqdq $0x10,%xmm12,%xmm10,%xmm9 // 2nd phase vpalignr $8,%xmm10,%xmm10,%xmm10 vpxor %xmm11,%xmm10,%xmm10 vpxor %xmm9,%xmm10,%xmm10 @@ -1112,7 +1112,9 @@ _CET_ENDBR .size gcm_ghash_avx,.-gcm_ghash_avx .section .rodata + .align 64 +ghash_constants: .Lbswap_mask: .byte 15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0 .L0x1c2_polynomial: diff --git a/third_party/boringssl/gen/bcm/ghash-x86_64-win.asm b/third_party/boringssl/gen/bcm/ghash-x86_64-win.asm index dd732483c..abdbb7056 100644 --- a/third_party/boringssl/gen/bcm/ghash-x86_64-win.asm +++ b/third_party/boringssl/gen/bcm/ghash-x86_64-win.asm @@ -27,154 +27,154 @@ $L$SEH_prologue_gcm_init_clmul_2: $L$SEH_prologue_gcm_init_clmul_3: $L$SEH_endprologue_gcm_init_clmul_4: movdqu xmm2,XMMWORD[rdx] - pshufd xmm2,xmm2,78 + pshufd xmm2,xmm2,78 ; dword swap - - pshufd xmm4,xmm2,255 +; <<1 twist + pshufd xmm4,xmm2,255 ; broadcast uppermost dword movdqa xmm3,xmm2 psllq xmm2,1 - pxor xmm5,xmm5 + pxor xmm5,xmm5 ; psrlq xmm3,63 - pcmpgtd xmm5,xmm4 + pcmpgtd xmm5,xmm4 ; broadcast carry bit pslldq xmm3,8 - por xmm2,xmm3 - + por xmm2,xmm3 ; H<<=1 +; magic reduction pand xmm5,XMMWORD[$L$0x1c2_polynomial] - pxor xmm2,xmm5 - + pxor xmm2,xmm5 ; if(carry) H^=0x1c2_polynomial +; calculate H^2 pshufd xmm6,xmm2,78 movdqa xmm0,xmm2 pxor xmm6,xmm2 - movdqa xmm1,xmm0 + movdqa xmm1,xmm0 ; pshufd xmm3,xmm0,78 - pxor xmm3,xmm0 - pclmulqdq xmm0,xmm2,0x00 - pclmulqdq xmm1,xmm2,0x11 - pclmulqdq xmm3,xmm6,0x00 - pxor xmm3,xmm0 - pxor xmm3,xmm1 - - movdqa xmm4,xmm3 + pxor xmm3,xmm0 ; + pclmulqdq xmm0,xmm2,0x00 ; ###### + pclmulqdq xmm1,xmm2,0x11 ; ###### + pclmulqdq xmm3,xmm6,0x00 ; ###### + pxor xmm3,xmm0 ; + pxor xmm3,xmm1 ; + + movdqa xmm4,xmm3 ; psrldq xmm3,8 - pslldq xmm4,8 + pslldq xmm4,8 ; pxor xmm1,xmm3 - pxor xmm0,xmm4 - - movdqa xmm4,xmm0 + pxor xmm0,xmm4 ; +; 1st phase + movdqa xmm4,xmm0 ; movdqa xmm3,xmm0 psllq xmm0,5 - pxor xmm3,xmm0 + pxor xmm3,xmm0 ; psllq xmm0,1 - pxor xmm0,xmm3 - psllq xmm0,57 - movdqa xmm3,xmm0 + pxor xmm0,xmm3 ; + psllq xmm0,57 ; + movdqa xmm3,xmm0 ; pslldq xmm0,8 - psrldq xmm3,8 + psrldq xmm3,8 ; pxor xmm0,xmm4 - pxor xmm1,xmm3 - + pxor xmm1,xmm3 ; +; 2nd phase movdqa xmm4,xmm0 psrlq xmm0,1 - pxor xmm1,xmm4 + pxor xmm1,xmm4 ; pxor xmm4,xmm0 psrlq xmm0,5 - pxor xmm0,xmm4 - psrlq xmm0,1 - pxor xmm0,xmm1 + pxor xmm0,xmm4 ; + psrlq xmm0,1 ; + pxor xmm0,xmm1 ; pshufd xmm3,xmm2,78 pshufd xmm4,xmm0,78 - pxor xmm3,xmm2 - movdqu XMMWORD[rcx],xmm2 - pxor xmm4,xmm0 - movdqu XMMWORD[16+rcx],xmm0 - palignr xmm4,xmm3,8 - movdqu XMMWORD[32+rcx],xmm4 - movdqa xmm1,xmm0 + pxor xmm3,xmm2 ; Karatsuba pre-processing + movdqu XMMWORD[rcx],xmm2 ; save H + pxor xmm4,xmm0 ; Karatsuba pre-processing + movdqu XMMWORD[16+rcx],xmm0 ; save H^2 + palignr xmm4,xmm3,8 ; low part is H.lo^H.hi... + movdqu XMMWORD[32+rcx],xmm4 ; save Karatsuba "salt" + movdqa xmm1,xmm0 ; pshufd xmm3,xmm0,78 - pxor xmm3,xmm0 - pclmulqdq xmm0,xmm2,0x00 - pclmulqdq xmm1,xmm2,0x11 - pclmulqdq xmm3,xmm6,0x00 - pxor xmm3,xmm0 - pxor xmm3,xmm1 - - movdqa xmm4,xmm3 + pxor xmm3,xmm0 ; + pclmulqdq xmm0,xmm2,0x00 ; ###### + pclmulqdq xmm1,xmm2,0x11 ; ###### + pclmulqdq xmm3,xmm6,0x00 ; ###### + pxor xmm3,xmm0 ; + pxor xmm3,xmm1 ; + + movdqa xmm4,xmm3 ; psrldq xmm3,8 - pslldq xmm4,8 + pslldq xmm4,8 ; pxor xmm1,xmm3 - pxor xmm0,xmm4 - - movdqa xmm4,xmm0 + pxor xmm0,xmm4 ; +; 1st phase + movdqa xmm4,xmm0 ; movdqa xmm3,xmm0 psllq xmm0,5 - pxor xmm3,xmm0 + pxor xmm3,xmm0 ; psllq xmm0,1 - pxor xmm0,xmm3 - psllq xmm0,57 - movdqa xmm3,xmm0 + pxor xmm0,xmm3 ; + psllq xmm0,57 ; + movdqa xmm3,xmm0 ; pslldq xmm0,8 - psrldq xmm3,8 + psrldq xmm3,8 ; pxor xmm0,xmm4 - pxor xmm1,xmm3 - + pxor xmm1,xmm3 ; +; 2nd phase movdqa xmm4,xmm0 psrlq xmm0,1 - pxor xmm1,xmm4 + pxor xmm1,xmm4 ; pxor xmm4,xmm0 psrlq xmm0,5 - pxor xmm0,xmm4 - psrlq xmm0,1 - pxor xmm0,xmm1 + pxor xmm0,xmm4 ; + psrlq xmm0,1 ; + pxor xmm0,xmm1 ; movdqa xmm5,xmm0 - movdqa xmm1,xmm0 + movdqa xmm1,xmm0 ; pshufd xmm3,xmm0,78 - pxor xmm3,xmm0 - pclmulqdq xmm0,xmm2,0x00 - pclmulqdq xmm1,xmm2,0x11 - pclmulqdq xmm3,xmm6,0x00 - pxor xmm3,xmm0 - pxor xmm3,xmm1 - - movdqa xmm4,xmm3 + pxor xmm3,xmm0 ; + pclmulqdq xmm0,xmm2,0x00 ; ###### + pclmulqdq xmm1,xmm2,0x11 ; ###### + pclmulqdq xmm3,xmm6,0x00 ; ###### + pxor xmm3,xmm0 ; + pxor xmm3,xmm1 ; + + movdqa xmm4,xmm3 ; psrldq xmm3,8 - pslldq xmm4,8 + pslldq xmm4,8 ; pxor xmm1,xmm3 - pxor xmm0,xmm4 - - movdqa xmm4,xmm0 + pxor xmm0,xmm4 ; +; 1st phase + movdqa xmm4,xmm0 ; movdqa xmm3,xmm0 psllq xmm0,5 - pxor xmm3,xmm0 + pxor xmm3,xmm0 ; psllq xmm0,1 - pxor xmm0,xmm3 - psllq xmm0,57 - movdqa xmm3,xmm0 + pxor xmm0,xmm3 ; + psllq xmm0,57 ; + movdqa xmm3,xmm0 ; pslldq xmm0,8 - psrldq xmm3,8 + psrldq xmm3,8 ; pxor xmm0,xmm4 - pxor xmm1,xmm3 - + pxor xmm1,xmm3 ; +; 2nd phase movdqa xmm4,xmm0 psrlq xmm0,1 - pxor xmm1,xmm4 + pxor xmm1,xmm4 ; pxor xmm4,xmm0 psrlq xmm0,5 - pxor xmm0,xmm4 - psrlq xmm0,1 - pxor xmm0,xmm1 + pxor xmm0,xmm4 ; + psrlq xmm0,1 ; + pxor xmm0,xmm1 ; pshufd xmm3,xmm5,78 pshufd xmm4,xmm0,78 - pxor xmm3,xmm5 - movdqu XMMWORD[48+rcx],xmm5 - pxor xmm4,xmm0 - movdqu XMMWORD[64+rcx],xmm0 - palignr xmm4,xmm3,8 - movdqu XMMWORD[80+rcx],xmm4 + pxor xmm3,xmm5 ; Karatsuba pre-processing + movdqu XMMWORD[48+rcx],xmm5 ; save H^3 + pxor xmm4,xmm0 ; Karatsuba pre-processing + movdqu XMMWORD[64+rcx],xmm0 ; save H^4 + palignr xmm4,xmm3,8 ; low part is H^3.lo^H^3.hi... + movdqu XMMWORD[80+rcx],xmm4 ; save Karatsuba "salt" movaps xmm6,XMMWORD[rsp] lea rsp,[24+rsp] ret @@ -193,43 +193,43 @@ $L$_gmult_clmul: movdqu xmm2,XMMWORD[rdx] movdqu xmm4,XMMWORD[32+rdx] pshufb xmm0,xmm5 - movdqa xmm1,xmm0 + movdqa xmm1,xmm0 ; pshufd xmm3,xmm0,78 - pxor xmm3,xmm0 - pclmulqdq xmm0,xmm2,0x00 - pclmulqdq xmm1,xmm2,0x11 - pclmulqdq xmm3,xmm4,0x00 - pxor xmm3,xmm0 - pxor xmm3,xmm1 - - movdqa xmm4,xmm3 + pxor xmm3,xmm0 ; + pclmulqdq xmm0,xmm2,0x00 ; ###### + pclmulqdq xmm1,xmm2,0x11 ; ###### + pclmulqdq xmm3,xmm4,0x00 ; ###### + pxor xmm3,xmm0 ; + pxor xmm3,xmm1 ; + + movdqa xmm4,xmm3 ; psrldq xmm3,8 - pslldq xmm4,8 + pslldq xmm4,8 ; pxor xmm1,xmm3 - pxor xmm0,xmm4 - - movdqa xmm4,xmm0 + pxor xmm0,xmm4 ; +; 1st phase + movdqa xmm4,xmm0 ; movdqa xmm3,xmm0 psllq xmm0,5 - pxor xmm3,xmm0 + pxor xmm3,xmm0 ; psllq xmm0,1 - pxor xmm0,xmm3 - psllq xmm0,57 - movdqa xmm3,xmm0 + pxor xmm0,xmm3 ; + psllq xmm0,57 ; + movdqa xmm3,xmm0 ; pslldq xmm0,8 - psrldq xmm3,8 + psrldq xmm3,8 ; pxor xmm0,xmm4 - pxor xmm1,xmm3 - + pxor xmm1,xmm3 ; +; 2nd phase movdqa xmm4,xmm0 psrlq xmm0,1 - pxor xmm1,xmm4 + pxor xmm1,xmm4 ; pxor xmm4,xmm0 psrlq xmm0,5 - pxor xmm0,xmm4 - psrlq xmm0,1 - pxor xmm0,xmm1 + pxor xmm0,xmm4 ; + psrlq xmm0,1 ; + pxor xmm0,xmm1 ; pshufb xmm0,xmm5 movdqu XMMWORD[rcx],xmm0 ret @@ -282,13 +282,13 @@ $L$SEH_endprologue_gcm_ghash_clmul_13: jb NEAR $L$skip4x sub r9,0x30 - mov rax,0xA040608020C0E000 + mov rax,0xA040608020C0E000 ; ((7..0)·0xE0)&0xff movdqu xmm14,XMMWORD[48+rdx] movdqu xmm15,XMMWORD[64+rdx] - - - +; ###### +; Xi+4 =[(H*Ii+3) + (H^2*Ii+2) + (H^3*Ii+1) + H^4*(Ii+Xi)] mod P +; movdqu xmm3,XMMWORD[48+r8] movdqu xmm11,XMMWORD[32+r8] pshufb xmm3,xmm10 @@ -353,33 +353,33 @@ $L$mod4_loop: pclmulqdq xmm11,xmm2,0x00 pshufd xmm4,xmm3,78 - pxor xmm8,xmm0 + pxor xmm8,xmm0 ; aggregated Karatsuba post-processing movdqa xmm5,xmm3 - pxor xmm8,xmm1 + pxor xmm8,xmm1 ; pxor xmm4,xmm3 - movdqa xmm9,xmm8 + movdqa xmm9,xmm8 ; pclmulqdq xmm13,xmm2,0x11 pslldq xmm8,8 - psrldq xmm9,8 + psrldq xmm9,8 ; pxor xmm0,xmm8 movdqa xmm8,XMMWORD[$L$7_mask] - pxor xmm1,xmm9 + pxor xmm1,xmm9 ; movq xmm9,rax - pand xmm8,xmm0 - pshufb xmm9,xmm8 - pxor xmm9,xmm0 + pand xmm8,xmm0 ; 1st phase + pshufb xmm9,xmm8 ; + pxor xmm9,xmm0 ; pclmulqdq xmm12,xmm7,0x00 - psllq xmm9,57 - movdqa xmm8,xmm9 + psllq xmm9,57 ; + movdqa xmm8,xmm9 ; pslldq xmm9,8 pclmulqdq xmm3,xmm6,0x00 - psrldq xmm8,8 + psrldq xmm8,8 ; pxor xmm0,xmm9 - pxor xmm1,xmm8 + pxor xmm1,xmm8 ; movdqu xmm8,XMMWORD[r8] - movdqa xmm9,xmm0 + movdqa xmm9,xmm0 ; 2nd phase psrlq xmm0,1 pclmulqdq xmm5,xmm6,0x11 xorps xmm3,xmm11 @@ -389,19 +389,19 @@ $L$mod4_loop: xorps xmm5,xmm13 movups xmm7,XMMWORD[80+rdx] pshufb xmm8,xmm10 - pxor xmm1,xmm9 + pxor xmm1,xmm9 ; pxor xmm9,xmm0 psrlq xmm0,5 movdqa xmm13,xmm11 pxor xmm4,xmm12 pshufd xmm12,xmm11,78 - pxor xmm0,xmm9 + pxor xmm0,xmm9 ; pxor xmm1,xmm8 pxor xmm12,xmm11 pclmulqdq xmm11,xmm14,0x00 - psrlq xmm0,1 - pxor xmm0,xmm1 + psrlq xmm0,1 ; + pxor xmm0,xmm1 ; movdqa xmm1,xmm0 pclmulqdq xmm13,xmm14,0x11 xorps xmm3,xmm11 @@ -422,56 +422,56 @@ $L$tail4x: xorps xmm4,xmm12 xorps xmm0,xmm3 xorps xmm1,xmm5 - pxor xmm1,xmm0 + pxor xmm1,xmm0 ; aggregated Karatsuba post-processing pxor xmm8,xmm4 - pxor xmm8,xmm1 + pxor xmm8,xmm1 ; pxor xmm1,xmm0 - movdqa xmm9,xmm8 + movdqa xmm9,xmm8 ; psrldq xmm8,8 - pslldq xmm9,8 + pslldq xmm9,8 ; pxor xmm1,xmm8 - pxor xmm0,xmm9 - - movdqa xmm4,xmm0 + pxor xmm0,xmm9 ; +; 1st phase + movdqa xmm4,xmm0 ; movdqa xmm3,xmm0 psllq xmm0,5 - pxor xmm3,xmm0 + pxor xmm3,xmm0 ; psllq xmm0,1 - pxor xmm0,xmm3 - psllq xmm0,57 - movdqa xmm3,xmm0 + pxor xmm0,xmm3 ; + psllq xmm0,57 ; + movdqa xmm3,xmm0 ; pslldq xmm0,8 - psrldq xmm3,8 + psrldq xmm3,8 ; pxor xmm0,xmm4 - pxor xmm1,xmm3 - + pxor xmm1,xmm3 ; +; 2nd phase movdqa xmm4,xmm0 psrlq xmm0,1 - pxor xmm1,xmm4 + pxor xmm1,xmm4 ; pxor xmm4,xmm0 psrlq xmm0,5 - pxor xmm0,xmm4 - psrlq xmm0,1 - pxor xmm0,xmm1 + pxor xmm0,xmm4 ; + psrlq xmm0,1 ; + pxor xmm0,xmm1 ; add r9,0x40 jz NEAR $L$done movdqu xmm7,XMMWORD[32+rdx] sub r9,0x10 jz NEAR $L$odd_tail $L$skip4x: - - - - - - movdqu xmm8,XMMWORD[r8] - movdqu xmm3,XMMWORD[16+r8] +; ###### +; Xi+2 =[H*(Ii+1 + Xi+1)] mod P = +; [(H*Ii+1) + (H*Xi+1)] mod P = +; [(H*Ii+1) + H^2*(Ii+Xi)] mod P +; + movdqu xmm8,XMMWORD[r8] ; Ii + movdqu xmm3,XMMWORD[16+r8] ; Ii+1 pshufb xmm8,xmm10 pshufb xmm3,xmm10 - pxor xmm0,xmm8 + pxor xmm0,xmm8 ; Ii+Xi movdqa xmm5,xmm3 pshufd xmm4,xmm3,78 @@ -480,7 +480,7 @@ $L$skip4x: pclmulqdq xmm5,xmm2,0x11 pclmulqdq xmm4,xmm7,0x00 - lea r8,[32+r8] + lea r8,[32+r8] ; i+=2 nop sub r9,0x20 jbe NEAR $L$even_tail @@ -491,59 +491,59 @@ ALIGN 32 $L$mod_loop: movdqa xmm1,xmm0 movdqa xmm8,xmm4 - pshufd xmm4,xmm0,78 - pxor xmm4,xmm0 + pshufd xmm4,xmm0,78 ; + pxor xmm4,xmm0 ; pclmulqdq xmm0,xmm6,0x00 pclmulqdq xmm1,xmm6,0x11 pclmulqdq xmm4,xmm7,0x10 - pxor xmm0,xmm3 + pxor xmm0,xmm3 ; (H*Ii+1) + H^2*(Ii+Xi) pxor xmm1,xmm5 - movdqu xmm9,XMMWORD[r8] - pxor xmm8,xmm0 + movdqu xmm9,XMMWORD[r8] ; Ii + pxor xmm8,xmm0 ; aggregated Karatsuba post-processing pshufb xmm9,xmm10 - movdqu xmm3,XMMWORD[16+r8] + movdqu xmm3,XMMWORD[16+r8] ; Ii+1 pxor xmm8,xmm1 - pxor xmm1,xmm9 + pxor xmm1,xmm9 ; "Ii+Xi", consume early pxor xmm4,xmm8 pshufb xmm3,xmm10 - movdqa xmm8,xmm4 + movdqa xmm8,xmm4 ; psrldq xmm8,8 - pslldq xmm4,8 + pslldq xmm4,8 ; pxor xmm1,xmm8 - pxor xmm0,xmm4 + pxor xmm0,xmm4 ; - movdqa xmm5,xmm3 + movdqa xmm5,xmm3 ; - movdqa xmm9,xmm0 + movdqa xmm9,xmm0 ; 1st phase movdqa xmm8,xmm0 psllq xmm0,5 - pxor xmm8,xmm0 - pclmulqdq xmm3,xmm2,0x00 + pxor xmm8,xmm0 ; + pclmulqdq xmm3,xmm2,0x00 ; ###### psllq xmm0,1 - pxor xmm0,xmm8 - psllq xmm0,57 - movdqa xmm8,xmm0 + pxor xmm0,xmm8 ; + psllq xmm0,57 ; + movdqa xmm8,xmm0 ; pslldq xmm0,8 - psrldq xmm8,8 + psrldq xmm8,8 ; pxor xmm0,xmm9 pshufd xmm4,xmm5,78 - pxor xmm1,xmm8 - pxor xmm4,xmm5 + pxor xmm1,xmm8 ; + pxor xmm4,xmm5 ; - movdqa xmm9,xmm0 + movdqa xmm9,xmm0 ; 2nd phase psrlq xmm0,1 - pclmulqdq xmm5,xmm2,0x11 - pxor xmm1,xmm9 + pclmulqdq xmm5,xmm2,0x11 ; ###### + pxor xmm1,xmm9 ; pxor xmm9,xmm0 psrlq xmm0,5 - pxor xmm0,xmm9 + pxor xmm0,xmm9 ; lea r8,[32+r8] - psrlq xmm0,1 - pclmulqdq xmm4,xmm7,0x00 - pxor xmm0,xmm1 + psrlq xmm0,1 ; + pclmulqdq xmm4,xmm7,0x00 ; ###### + pxor xmm0,xmm1 ; sub r9,0x20 ja NEAR $L$mod_loop @@ -551,90 +551,90 @@ $L$mod_loop: $L$even_tail: movdqa xmm1,xmm0 movdqa xmm8,xmm4 - pshufd xmm4,xmm0,78 - pxor xmm4,xmm0 + pshufd xmm4,xmm0,78 ; + pxor xmm4,xmm0 ; pclmulqdq xmm0,xmm6,0x00 pclmulqdq xmm1,xmm6,0x11 pclmulqdq xmm4,xmm7,0x10 - pxor xmm0,xmm3 + pxor xmm0,xmm3 ; (H*Ii+1) + H^2*(Ii+Xi) pxor xmm1,xmm5 pxor xmm8,xmm0 pxor xmm8,xmm1 pxor xmm4,xmm8 - movdqa xmm8,xmm4 + movdqa xmm8,xmm4 ; psrldq xmm8,8 - pslldq xmm4,8 + pslldq xmm4,8 ; pxor xmm1,xmm8 - pxor xmm0,xmm4 - - movdqa xmm4,xmm0 + pxor xmm0,xmm4 ; +; 1st phase + movdqa xmm4,xmm0 ; movdqa xmm3,xmm0 psllq xmm0,5 - pxor xmm3,xmm0 + pxor xmm3,xmm0 ; psllq xmm0,1 - pxor xmm0,xmm3 - psllq xmm0,57 - movdqa xmm3,xmm0 + pxor xmm0,xmm3 ; + psllq xmm0,57 ; + movdqa xmm3,xmm0 ; pslldq xmm0,8 - psrldq xmm3,8 + psrldq xmm3,8 ; pxor xmm0,xmm4 - pxor xmm1,xmm3 - + pxor xmm1,xmm3 ; +; 2nd phase movdqa xmm4,xmm0 psrlq xmm0,1 - pxor xmm1,xmm4 + pxor xmm1,xmm4 ; pxor xmm4,xmm0 psrlq xmm0,5 - pxor xmm0,xmm4 - psrlq xmm0,1 - pxor xmm0,xmm1 + pxor xmm0,xmm4 ; + psrlq xmm0,1 ; + pxor xmm0,xmm1 ; test r9,r9 jnz NEAR $L$done $L$odd_tail: - movdqu xmm8,XMMWORD[r8] + movdqu xmm8,XMMWORD[r8] ; Ii pshufb xmm8,xmm10 - pxor xmm0,xmm8 - movdqa xmm1,xmm0 + pxor xmm0,xmm8 ; Ii+Xi + movdqa xmm1,xmm0 ; pshufd xmm3,xmm0,78 - pxor xmm3,xmm0 - pclmulqdq xmm0,xmm2,0x00 - pclmulqdq xmm1,xmm2,0x11 - pclmulqdq xmm3,xmm7,0x00 - pxor xmm3,xmm0 - pxor xmm3,xmm1 - - movdqa xmm4,xmm3 + pxor xmm3,xmm0 ; + pclmulqdq xmm0,xmm2,0x00 ; ###### + pclmulqdq xmm1,xmm2,0x11 ; ###### + pclmulqdq xmm3,xmm7,0x00 ; ###### + pxor xmm3,xmm0 ; + pxor xmm3,xmm1 ; + + movdqa xmm4,xmm3 ; psrldq xmm3,8 - pslldq xmm4,8 + pslldq xmm4,8 ; pxor xmm1,xmm3 - pxor xmm0,xmm4 - - movdqa xmm4,xmm0 + pxor xmm0,xmm4 ; +; 1st phase + movdqa xmm4,xmm0 ; movdqa xmm3,xmm0 psllq xmm0,5 - pxor xmm3,xmm0 + pxor xmm3,xmm0 ; psllq xmm0,1 - pxor xmm0,xmm3 - psllq xmm0,57 - movdqa xmm3,xmm0 + pxor xmm0,xmm3 ; + psllq xmm0,57 ; + movdqa xmm3,xmm0 ; pslldq xmm0,8 - psrldq xmm3,8 + psrldq xmm3,8 ; pxor xmm0,xmm4 - pxor xmm1,xmm3 - + pxor xmm1,xmm3 ; +; 2nd phase movdqa xmm4,xmm0 psrlq xmm0,1 - pxor xmm1,xmm4 + pxor xmm1,xmm4 ; pxor xmm4,xmm0 psrlq xmm0,5 - pxor xmm0,xmm4 - psrlq xmm0,1 - pxor xmm0,xmm1 + pxor xmm0,xmm4 ; + psrlq xmm0,1 ; + pxor xmm0,xmm1 ; $L$done: pshufb xmm0,xmm10 movdqu XMMWORD[rcx],xmm0 @@ -668,101 +668,101 @@ $L$SEH_endprologue_gcm_init_avx_4: vzeroupper vmovdqu xmm2,XMMWORD[rdx] - vpshufd xmm2,xmm2,78 - + vpshufd xmm2,xmm2,78 ; dword swap - vpshufd xmm4,xmm2,255 +; <<1 twist + vpshufd xmm4,xmm2,255 ; broadcast uppermost dword vpsrlq xmm3,xmm2,63 vpsllq xmm2,xmm2,1 - vpxor xmm5,xmm5,xmm5 - vpcmpgtd xmm5,xmm5,xmm4 + vpxor xmm5,xmm5,xmm5 ; + vpcmpgtd xmm5,xmm5,xmm4 ; broadcast carry bit vpslldq xmm3,xmm3,8 - vpor xmm2,xmm2,xmm3 - + vpor xmm2,xmm2,xmm3 ; H<<=1 +; magic reduction vpand xmm5,xmm5,XMMWORD[$L$0x1c2_polynomial] - vpxor xmm2,xmm2,xmm5 + vpxor xmm2,xmm2,xmm5 ; if(carry) H^=0x1c2_polynomial vpunpckhqdq xmm6,xmm2,xmm2 vmovdqa xmm0,xmm2 vpxor xmm6,xmm6,xmm2 - mov r10,4 + mov r10,4 ; up to H^8 jmp NEAR $L$init_start_avx ALIGN 32 $L$init_loop_avx: - vpalignr xmm5,xmm4,xmm3,8 - vmovdqu XMMWORD[(-16)+rcx],xmm5 + vpalignr xmm5,xmm4,xmm3,8 ; low part is H.lo^H.hi... + vmovdqu XMMWORD[(-16)+rcx],xmm5 ; save Karatsuba "salt" vpunpckhqdq xmm3,xmm0,xmm0 - vpxor xmm3,xmm3,xmm0 - vpclmulqdq xmm1,xmm0,xmm2,0x11 - vpclmulqdq xmm0,xmm0,xmm2,0x00 - vpclmulqdq xmm3,xmm3,xmm6,0x00 - vpxor xmm4,xmm1,xmm0 - vpxor xmm3,xmm3,xmm4 - - vpslldq xmm4,xmm3,8 + vpxor xmm3,xmm3,xmm0 ; + vpclmulqdq xmm1,xmm0,xmm2,0x11 ; ###### + vpclmulqdq xmm0,xmm0,xmm2,0x00 ; ###### + vpclmulqdq xmm3,xmm3,xmm6,0x00 ; ###### + vpxor xmm4,xmm1,xmm0 ; + vpxor xmm3,xmm3,xmm4 ; + + vpslldq xmm4,xmm3,8 ; vpsrldq xmm3,xmm3,8 - vpxor xmm0,xmm0,xmm4 + vpxor xmm0,xmm0,xmm4 ; vpxor xmm1,xmm1,xmm3 - vpsllq xmm3,xmm0,57 + vpsllq xmm3,xmm0,57 ; 1st phase vpsllq xmm4,xmm0,62 - vpxor xmm4,xmm4,xmm3 + vpxor xmm4,xmm4,xmm3 ; vpsllq xmm3,xmm0,63 - vpxor xmm4,xmm4,xmm3 - vpslldq xmm3,xmm4,8 + vpxor xmm4,xmm4,xmm3 ; + vpslldq xmm3,xmm4,8 ; vpsrldq xmm4,xmm4,8 - vpxor xmm0,xmm0,xmm3 + vpxor xmm0,xmm0,xmm3 ; vpxor xmm1,xmm1,xmm4 - vpsrlq xmm4,xmm0,1 + vpsrlq xmm4,xmm0,1 ; 2nd phase vpxor xmm1,xmm1,xmm0 - vpxor xmm0,xmm0,xmm4 + vpxor xmm0,xmm0,xmm4 ; vpsrlq xmm4,xmm4,5 - vpxor xmm0,xmm0,xmm4 - vpsrlq xmm0,xmm0,1 - vpxor xmm0,xmm0,xmm1 + vpxor xmm0,xmm0,xmm4 ; + vpsrlq xmm0,xmm0,1 ; + vpxor xmm0,xmm0,xmm1 ; $L$init_start_avx: vmovdqa xmm5,xmm0 vpunpckhqdq xmm3,xmm0,xmm0 - vpxor xmm3,xmm3,xmm0 - vpclmulqdq xmm1,xmm0,xmm2,0x11 - vpclmulqdq xmm0,xmm0,xmm2,0x00 - vpclmulqdq xmm3,xmm3,xmm6,0x00 - vpxor xmm4,xmm1,xmm0 - vpxor xmm3,xmm3,xmm4 - - vpslldq xmm4,xmm3,8 + vpxor xmm3,xmm3,xmm0 ; + vpclmulqdq xmm1,xmm0,xmm2,0x11 ; ###### + vpclmulqdq xmm0,xmm0,xmm2,0x00 ; ###### + vpclmulqdq xmm3,xmm3,xmm6,0x00 ; ###### + vpxor xmm4,xmm1,xmm0 ; + vpxor xmm3,xmm3,xmm4 ; + + vpslldq xmm4,xmm3,8 ; vpsrldq xmm3,xmm3,8 - vpxor xmm0,xmm0,xmm4 + vpxor xmm0,xmm0,xmm4 ; vpxor xmm1,xmm1,xmm3 - vpsllq xmm3,xmm0,57 + vpsllq xmm3,xmm0,57 ; 1st phase vpsllq xmm4,xmm0,62 - vpxor xmm4,xmm4,xmm3 + vpxor xmm4,xmm4,xmm3 ; vpsllq xmm3,xmm0,63 - vpxor xmm4,xmm4,xmm3 - vpslldq xmm3,xmm4,8 + vpxor xmm4,xmm4,xmm3 ; + vpslldq xmm3,xmm4,8 ; vpsrldq xmm4,xmm4,8 - vpxor xmm0,xmm0,xmm3 + vpxor xmm0,xmm0,xmm3 ; vpxor xmm1,xmm1,xmm4 - vpsrlq xmm4,xmm0,1 + vpsrlq xmm4,xmm0,1 ; 2nd phase vpxor xmm1,xmm1,xmm0 - vpxor xmm0,xmm0,xmm4 + vpxor xmm0,xmm0,xmm4 ; vpsrlq xmm4,xmm4,5 - vpxor xmm0,xmm0,xmm4 - vpsrlq xmm0,xmm0,1 - vpxor xmm0,xmm0,xmm1 + vpxor xmm0,xmm0,xmm4 ; + vpsrlq xmm0,xmm0,1 ; + vpxor xmm0,xmm0,xmm1 ; vpshufd xmm3,xmm5,78 vpshufd xmm4,xmm0,78 - vpxor xmm3,xmm3,xmm5 - vmovdqu XMMWORD[rcx],xmm5 - vpxor xmm4,xmm4,xmm0 - vmovdqu XMMWORD[16+rcx],xmm0 + vpxor xmm3,xmm3,xmm5 ; Karatsuba pre-processing + vmovdqu XMMWORD[rcx],xmm5 ; save H^1,3,5,7 + vpxor xmm4,xmm4,xmm0 ; Karatsuba pre-processing + vmovdqu XMMWORD[16+rcx],xmm0 ; save H^2,4,6,8 lea rcx,[48+rcx] sub r10,1 jnz NEAR $L$init_loop_avx - vpalignr xmm5,xmm3,xmm4,8 + vpalignr xmm5,xmm3,xmm4,8 ; last "salt" is flipped vmovdqu XMMWORD[(-16)+rcx],xmm5 vzeroupper @@ -814,29 +814,29 @@ $L$SEH_prologue_gcm_ghash_avx_12: $L$SEH_endprologue_gcm_ghash_avx_13: vzeroupper - vmovdqu xmm10,XMMWORD[rcx] + vmovdqu xmm10,XMMWORD[rcx] ; load %xmm10 lea r10,[$L$0x1c2_polynomial] - lea rdx,[64+rdx] + lea rdx,[64+rdx] ; size optimization vmovdqu xmm13,XMMWORD[$L$bswap_mask] vpshufb xmm10,xmm10,xmm13 cmp r9,0x80 jb NEAR $L$short_avx sub r9,0x80 - vmovdqu xmm14,XMMWORD[112+r8] - vmovdqu xmm6,XMMWORD[((0-64))+rdx] + vmovdqu xmm14,XMMWORD[112+r8] ; I[7] + vmovdqu xmm6,XMMWORD[((0-64))+rdx] ; %xmm6^1 vpshufb xmm14,xmm14,xmm13 vmovdqu xmm7,XMMWORD[((32-64))+rdx] vpunpckhqdq xmm9,xmm14,xmm14 - vmovdqu xmm15,XMMWORD[96+r8] + vmovdqu xmm15,XMMWORD[96+r8] ; I[6] vpclmulqdq xmm0,xmm14,xmm6,0x00 vpxor xmm9,xmm9,xmm14 vpshufb xmm15,xmm15,xmm13 vpclmulqdq xmm1,xmm14,xmm6,0x11 - vmovdqu xmm6,XMMWORD[((16-64))+rdx] + vmovdqu xmm6,XMMWORD[((16-64))+rdx] ; %xmm6^2 vpunpckhqdq xmm8,xmm15,xmm15 - vmovdqu xmm14,XMMWORD[80+r8] + vmovdqu xmm14,XMMWORD[80+r8] ; I[5] vpclmulqdq xmm2,xmm9,xmm7,0x00 vpxor xmm8,xmm8,xmm15 @@ -844,9 +844,9 @@ $L$SEH_endprologue_gcm_ghash_avx_13: vpclmulqdq xmm3,xmm15,xmm6,0x00 vpunpckhqdq xmm9,xmm14,xmm14 vpclmulqdq xmm4,xmm15,xmm6,0x11 - vmovdqu xmm6,XMMWORD[((48-64))+rdx] + vmovdqu xmm6,XMMWORD[((48-64))+rdx] ; %xmm6^3 vpxor xmm9,xmm9,xmm14 - vmovdqu xmm15,XMMWORD[64+r8] + vmovdqu xmm15,XMMWORD[64+r8] ; I[4] vpclmulqdq xmm5,xmm8,xmm7,0x10 vmovdqu xmm7,XMMWORD[((80-64))+rdx] @@ -856,56 +856,56 @@ $L$SEH_endprologue_gcm_ghash_avx_13: vpxor xmm4,xmm4,xmm1 vpunpckhqdq xmm8,xmm15,xmm15 vpclmulqdq xmm1,xmm14,xmm6,0x11 - vmovdqu xmm6,XMMWORD[((64-64))+rdx] + vmovdqu xmm6,XMMWORD[((64-64))+rdx] ; %xmm6^4 vpxor xmm5,xmm5,xmm2 vpclmulqdq xmm2,xmm9,xmm7,0x00 vpxor xmm8,xmm8,xmm15 - vmovdqu xmm14,XMMWORD[48+r8] + vmovdqu xmm14,XMMWORD[48+r8] ; I[3] vpxor xmm0,xmm0,xmm3 vpclmulqdq xmm3,xmm15,xmm6,0x00 vpxor xmm1,xmm1,xmm4 vpshufb xmm14,xmm14,xmm13 vpclmulqdq xmm4,xmm15,xmm6,0x11 - vmovdqu xmm6,XMMWORD[((96-64))+rdx] + vmovdqu xmm6,XMMWORD[((96-64))+rdx] ; %xmm6^5 vpxor xmm2,xmm2,xmm5 vpunpckhqdq xmm9,xmm14,xmm14 vpclmulqdq xmm5,xmm8,xmm7,0x10 vmovdqu xmm7,XMMWORD[((128-64))+rdx] vpxor xmm9,xmm9,xmm14 - vmovdqu xmm15,XMMWORD[32+r8] + vmovdqu xmm15,XMMWORD[32+r8] ; I[2] vpxor xmm3,xmm3,xmm0 vpclmulqdq xmm0,xmm14,xmm6,0x00 vpxor xmm4,xmm4,xmm1 vpshufb xmm15,xmm15,xmm13 vpclmulqdq xmm1,xmm14,xmm6,0x11 - vmovdqu xmm6,XMMWORD[((112-64))+rdx] + vmovdqu xmm6,XMMWORD[((112-64))+rdx] ; %xmm6^6 vpxor xmm5,xmm5,xmm2 vpunpckhqdq xmm8,xmm15,xmm15 vpclmulqdq xmm2,xmm9,xmm7,0x00 vpxor xmm8,xmm8,xmm15 - vmovdqu xmm14,XMMWORD[16+r8] + vmovdqu xmm14,XMMWORD[16+r8] ; I[1] vpxor xmm0,xmm0,xmm3 vpclmulqdq xmm3,xmm15,xmm6,0x00 vpxor xmm1,xmm1,xmm4 vpshufb xmm14,xmm14,xmm13 vpclmulqdq xmm4,xmm15,xmm6,0x11 - vmovdqu xmm6,XMMWORD[((144-64))+rdx] + vmovdqu xmm6,XMMWORD[((144-64))+rdx] ; %xmm6^7 vpxor xmm2,xmm2,xmm5 vpunpckhqdq xmm9,xmm14,xmm14 vpclmulqdq xmm5,xmm8,xmm7,0x10 vmovdqu xmm7,XMMWORD[((176-64))+rdx] vpxor xmm9,xmm9,xmm14 - vmovdqu xmm15,XMMWORD[r8] + vmovdqu xmm15,XMMWORD[r8] ; I[0] vpxor xmm3,xmm3,xmm0 vpclmulqdq xmm0,xmm14,xmm6,0x00 vpxor xmm4,xmm4,xmm1 vpshufb xmm15,xmm15,xmm13 vpclmulqdq xmm1,xmm14,xmm6,0x11 - vmovdqu xmm6,XMMWORD[((160-64))+rdx] + vmovdqu xmm6,XMMWORD[((160-64))+rdx] ; %xmm6^8 vpxor xmm5,xmm5,xmm2 vpclmulqdq xmm2,xmm9,xmm7,0x10 @@ -913,41 +913,41 @@ $L$SEH_endprologue_gcm_ghash_avx_13: cmp r9,0x80 jb NEAR $L$tail_avx - vpxor xmm15,xmm15,xmm10 + vpxor xmm15,xmm15,xmm10 ; accumulate %xmm10 sub r9,0x80 jmp NEAR $L$oop8x_avx ALIGN 32 $L$oop8x_avx: vpunpckhqdq xmm8,xmm15,xmm15 - vmovdqu xmm14,XMMWORD[112+r8] + vmovdqu xmm14,XMMWORD[112+r8] ; I[7] vpxor xmm3,xmm3,xmm0 vpxor xmm8,xmm8,xmm15 vpclmulqdq xmm10,xmm15,xmm6,0x00 vpshufb xmm14,xmm14,xmm13 vpxor xmm4,xmm4,xmm1 vpclmulqdq xmm11,xmm15,xmm6,0x11 - vmovdqu xmm6,XMMWORD[((0-64))+rdx] + vmovdqu xmm6,XMMWORD[((0-64))+rdx] ; %xmm6^1 vpunpckhqdq xmm9,xmm14,xmm14 vpxor xmm5,xmm5,xmm2 vpclmulqdq xmm12,xmm8,xmm7,0x00 vmovdqu xmm7,XMMWORD[((32-64))+rdx] vpxor xmm9,xmm9,xmm14 - vmovdqu xmm15,XMMWORD[96+r8] + vmovdqu xmm15,XMMWORD[96+r8] ; I[6] vpclmulqdq xmm0,xmm14,xmm6,0x00 - vpxor xmm10,xmm10,xmm3 + vpxor xmm10,xmm10,xmm3 ; collect result vpshufb xmm15,xmm15,xmm13 vpclmulqdq xmm1,xmm14,xmm6,0x11 vxorps xmm11,xmm11,xmm4 - vmovdqu xmm6,XMMWORD[((16-64))+rdx] + vmovdqu xmm6,XMMWORD[((16-64))+rdx] ; %xmm6^2 vpunpckhqdq xmm8,xmm15,xmm15 vpclmulqdq xmm2,xmm9,xmm7,0x00 vpxor xmm12,xmm12,xmm5 vxorps xmm8,xmm8,xmm15 - vmovdqu xmm14,XMMWORD[80+r8] - vpxor xmm12,xmm12,xmm10 + vmovdqu xmm14,XMMWORD[80+r8] ; I[5] + vpxor xmm12,xmm12,xmm10 ; aggregated Karatsuba post-processing vpclmulqdq xmm3,xmm15,xmm6,0x00 vpxor xmm12,xmm12,xmm11 vpslldq xmm9,xmm12,8 @@ -955,7 +955,7 @@ $L$oop8x_avx: vpclmulqdq xmm4,xmm15,xmm6,0x11 vpsrldq xmm12,xmm12,8 vpxor xmm10,xmm10,xmm9 - vmovdqu xmm6,XMMWORD[((48-64))+rdx] + vmovdqu xmm6,XMMWORD[((48-64))+rdx] ; %xmm6^3 vpshufb xmm14,xmm14,xmm13 vxorps xmm11,xmm11,xmm12 vpxor xmm4,xmm4,xmm1 @@ -965,26 +965,26 @@ $L$oop8x_avx: vpxor xmm9,xmm9,xmm14 vpxor xmm5,xmm5,xmm2 - vmovdqu xmm15,XMMWORD[64+r8] - vpalignr xmm12,xmm10,xmm10,8 + vmovdqu xmm15,XMMWORD[64+r8] ; I[4] + vpalignr xmm12,xmm10,xmm10,8 ; 1st phase vpclmulqdq xmm0,xmm14,xmm6,0x00 vpshufb xmm15,xmm15,xmm13 vpxor xmm0,xmm0,xmm3 vpclmulqdq xmm1,xmm14,xmm6,0x11 - vmovdqu xmm6,XMMWORD[((64-64))+rdx] + vmovdqu xmm6,XMMWORD[((64-64))+rdx] ; %xmm6^4 vpunpckhqdq xmm8,xmm15,xmm15 vpxor xmm1,xmm1,xmm4 vpclmulqdq xmm2,xmm9,xmm7,0x00 vxorps xmm8,xmm8,xmm15 vpxor xmm2,xmm2,xmm5 - vmovdqu xmm14,XMMWORD[48+r8] + vmovdqu xmm14,XMMWORD[48+r8] ; I[3] vpclmulqdq xmm10,xmm10,XMMWORD[r10],0x10 vpclmulqdq xmm3,xmm15,xmm6,0x00 vpshufb xmm14,xmm14,xmm13 vpxor xmm3,xmm3,xmm0 vpclmulqdq xmm4,xmm15,xmm6,0x11 - vmovdqu xmm6,XMMWORD[((96-64))+rdx] + vmovdqu xmm6,XMMWORD[((96-64))+rdx] ; %xmm6^5 vpunpckhqdq xmm9,xmm14,xmm14 vpxor xmm4,xmm4,xmm1 vpclmulqdq xmm5,xmm8,xmm7,0x10 @@ -992,12 +992,12 @@ $L$oop8x_avx: vpxor xmm9,xmm9,xmm14 vpxor xmm5,xmm5,xmm2 - vmovdqu xmm15,XMMWORD[32+r8] + vmovdqu xmm15,XMMWORD[32+r8] ; I[2] vpclmulqdq xmm0,xmm14,xmm6,0x00 vpshufb xmm15,xmm15,xmm13 vpxor xmm0,xmm0,xmm3 vpclmulqdq xmm1,xmm14,xmm6,0x11 - vmovdqu xmm6,XMMWORD[((112-64))+rdx] + vmovdqu xmm6,XMMWORD[((112-64))+rdx] ; %xmm6^6 vpunpckhqdq xmm8,xmm15,xmm15 vpxor xmm1,xmm1,xmm4 vpclmulqdq xmm2,xmm9,xmm7,0x00 @@ -1005,13 +1005,13 @@ $L$oop8x_avx: vpxor xmm2,xmm2,xmm5 vxorps xmm10,xmm10,xmm12 - vmovdqu xmm14,XMMWORD[16+r8] - vpalignr xmm12,xmm10,xmm10,8 + vmovdqu xmm14,XMMWORD[16+r8] ; I[1] + vpalignr xmm12,xmm10,xmm10,8 ; 2nd phase vpclmulqdq xmm3,xmm15,xmm6,0x00 vpshufb xmm14,xmm14,xmm13 vpxor xmm3,xmm3,xmm0 vpclmulqdq xmm4,xmm15,xmm6,0x11 - vmovdqu xmm6,XMMWORD[((144-64))+rdx] + vmovdqu xmm6,XMMWORD[((144-64))+rdx] ; %xmm6^7 vpclmulqdq xmm10,xmm10,XMMWORD[r10],0x10 vxorps xmm12,xmm12,xmm11 vpunpckhqdq xmm9,xmm14,xmm14 @@ -1021,14 +1021,14 @@ $L$oop8x_avx: vpxor xmm9,xmm9,xmm14 vpxor xmm5,xmm5,xmm2 - vmovdqu xmm15,XMMWORD[r8] + vmovdqu xmm15,XMMWORD[r8] ; I[0] vpclmulqdq xmm0,xmm14,xmm6,0x00 vpshufb xmm15,xmm15,xmm13 vpclmulqdq xmm1,xmm14,xmm6,0x11 - vmovdqu xmm6,XMMWORD[((160-64))+rdx] + vmovdqu xmm6,XMMWORD[((160-64))+rdx] ; %xmm6^8 vpxor xmm15,xmm15,xmm12 vpclmulqdq xmm2,xmm9,xmm7,0x10 - vpxor xmm15,xmm15,xmm10 + vpxor xmm15,xmm15,xmm10 ; accumulate %xmm10 lea r8,[128+r8] sub r9,0x80 @@ -1039,15 +1039,15 @@ $L$oop8x_avx: ALIGN 32 $L$short_avx: - vmovdqu xmm14,XMMWORD[((-16))+r9*1+r8] + vmovdqu xmm14,XMMWORD[((-16))+r9*1+r8] ; very last word lea r8,[r9*1+r8] - vmovdqu xmm6,XMMWORD[((0-64))+rdx] + vmovdqu xmm6,XMMWORD[((0-64))+rdx] ; %xmm6^1 vmovdqu xmm7,XMMWORD[((32-64))+rdx] vpshufb xmm15,xmm14,xmm13 - vmovdqa xmm3,xmm0 - vmovdqa xmm4,xmm1 - vmovdqa xmm5,xmm2 + vmovdqa xmm3,xmm0 ; subtle way to zero %xmm3, + vmovdqa xmm4,xmm1 ; %xmm4 and + vmovdqa xmm5,xmm2 ; %xmm5 sub r9,0x10 jz NEAR $L$tail_avx @@ -1058,7 +1058,7 @@ $L$short_avx: vmovdqu xmm14,XMMWORD[((-32))+r8] vpxor xmm4,xmm4,xmm1 vpclmulqdq xmm1,xmm15,xmm6,0x11 - vmovdqu xmm6,XMMWORD[((16-64))+rdx] + vmovdqu xmm6,XMMWORD[((16-64))+rdx] ; %xmm6^2 vpshufb xmm15,xmm14,xmm13 vpxor xmm5,xmm5,xmm2 vpclmulqdq xmm2,xmm8,xmm7,0x00 @@ -1073,7 +1073,7 @@ $L$short_avx: vmovdqu xmm14,XMMWORD[((-48))+r8] vpxor xmm4,xmm4,xmm1 vpclmulqdq xmm1,xmm15,xmm6,0x11 - vmovdqu xmm6,XMMWORD[((48-64))+rdx] + vmovdqu xmm6,XMMWORD[((48-64))+rdx] ; %xmm6^3 vpshufb xmm15,xmm14,xmm13 vpxor xmm5,xmm5,xmm2 vpclmulqdq xmm2,xmm8,xmm7,0x00 @@ -1088,7 +1088,7 @@ $L$short_avx: vmovdqu xmm14,XMMWORD[((-64))+r8] vpxor xmm4,xmm4,xmm1 vpclmulqdq xmm1,xmm15,xmm6,0x11 - vmovdqu xmm6,XMMWORD[((64-64))+rdx] + vmovdqu xmm6,XMMWORD[((64-64))+rdx] ; %xmm6^4 vpshufb xmm15,xmm14,xmm13 vpxor xmm5,xmm5,xmm2 vpclmulqdq xmm2,xmm8,xmm7,0x00 @@ -1103,7 +1103,7 @@ $L$short_avx: vmovdqu xmm14,XMMWORD[((-80))+r8] vpxor xmm4,xmm4,xmm1 vpclmulqdq xmm1,xmm15,xmm6,0x11 - vmovdqu xmm6,XMMWORD[((96-64))+rdx] + vmovdqu xmm6,XMMWORD[((96-64))+rdx] ; %xmm6^5 vpshufb xmm15,xmm14,xmm13 vpxor xmm5,xmm5,xmm2 vpclmulqdq xmm2,xmm8,xmm7,0x00 @@ -1118,7 +1118,7 @@ $L$short_avx: vmovdqu xmm14,XMMWORD[((-96))+r8] vpxor xmm4,xmm4,xmm1 vpclmulqdq xmm1,xmm15,xmm6,0x11 - vmovdqu xmm6,XMMWORD[((112-64))+rdx] + vmovdqu xmm6,XMMWORD[((112-64))+rdx] ; %xmm6^6 vpshufb xmm15,xmm14,xmm13 vpxor xmm5,xmm5,xmm2 vpclmulqdq xmm2,xmm8,xmm7,0x00 @@ -1133,7 +1133,7 @@ $L$short_avx: vmovdqu xmm14,XMMWORD[((-112))+r8] vpxor xmm4,xmm4,xmm1 vpclmulqdq xmm1,xmm15,xmm6,0x11 - vmovdqu xmm6,XMMWORD[((144-64))+rdx] + vmovdqu xmm6,XMMWORD[((144-64))+rdx] ; %xmm6^7 vpshufb xmm15,xmm14,xmm13 vpxor xmm5,xmm5,xmm2 vpclmulqdq xmm2,xmm8,xmm7,0x00 @@ -1143,7 +1143,7 @@ $L$short_avx: ALIGN 32 $L$tail_avx: - vpxor xmm15,xmm15,xmm10 + vpxor xmm15,xmm15,xmm10 ; accumulate %xmm10 $L$tail_no_xor_avx: vpunpckhqdq xmm8,xmm15,xmm15 vpxor xmm3,xmm3,xmm0 @@ -1160,18 +1160,18 @@ $L$tail_no_xor_avx: vpxor xmm11,xmm4,xmm1 vpxor xmm5,xmm5,xmm2 - vpxor xmm5,xmm5,xmm10 + vpxor xmm5,xmm5,xmm10 ; aggregated Karatsuba post-processing vpxor xmm5,xmm5,xmm11 vpslldq xmm9,xmm5,8 vpsrldq xmm5,xmm5,8 vpxor xmm10,xmm10,xmm9 vpxor xmm11,xmm11,xmm5 - vpclmulqdq xmm9,xmm10,xmm12,0x10 + vpclmulqdq xmm9,xmm10,xmm12,0x10 ; 1st phase vpalignr xmm10,xmm10,xmm10,8 vpxor xmm10,xmm10,xmm9 - vpclmulqdq xmm9,xmm10,xmm12,0x10 + vpclmulqdq xmm9,xmm10,xmm12,0x10 ; 2nd phase vpalignr xmm10,xmm10,xmm10,8 vpxor xmm10,xmm10,xmm11 vpxor xmm10,xmm10,xmm9 @@ -1198,7 +1198,9 @@ $L$tail_no_xor_avx: $L$SEH_end_gcm_ghash_avx_14: section .rdata rdata align=8 + ALIGN 64 +ghash_constants: $L$bswap_mask: DB 15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0 $L$0x1c2_polynomial: @@ -1212,7 +1214,7 @@ ALIGN 64 DB 60,97,112,112,114,111,64,111,112,101,110,115,115,108,46,111 DB 114,103,62,0 ALIGN 64 -section .text +section .text code align=64 section .pdata rdata align=4 ALIGN 4 @@ -1236,7 +1238,7 @@ ALIGN 4 section .xdata rdata align=8 ALIGN 4 $L$SEH_info_gcm_init_clmul_0: - DB 1 + DB 1 ; version 1, no flags DB $L$SEH_endprologue_gcm_init_clmul_4-$L$SEH_begin_gcm_init_clmul_1 DB 3 DB 0 @@ -1248,7 +1250,7 @@ $L$SEH_info_gcm_init_clmul_0: DW 0 $L$SEH_info_gcm_ghash_clmul_0: - DB 1 + DB 1 ; version 1, no flags DB $L$SEH_endprologue_gcm_ghash_clmul_13-$L$SEH_begin_gcm_ghash_clmul_1 DB 22 DB 0 @@ -1287,7 +1289,7 @@ $L$SEH_info_gcm_ghash_clmul_0: DW 21 $L$SEH_info_gcm_init_avx_0: - DB 1 + DB 1 ; version 1, no flags DB $L$SEH_endprologue_gcm_init_avx_4-$L$SEH_begin_gcm_init_avx_1 DB 3 DB 0 @@ -1299,7 +1301,7 @@ $L$SEH_info_gcm_init_avx_0: DW 0 $L$SEH_info_gcm_ghash_avx_0: - DB 1 + DB 1 ; version 1, no flags DB $L$SEH_endprologue_gcm_ghash_avx_13-$L$SEH_begin_gcm_ghash_avx_1 DB 22 DB 0 diff --git a/third_party/boringssl/gen/bcm/ghashv8-armv8-apple.S b/third_party/boringssl/gen/bcm/ghashv8-armv8-apple.S index ee458be49..115b94f7f 100644 --- a/third_party/boringssl/gen/bcm/ghashv8-armv8-apple.S +++ b/third_party/boringssl/gen/bcm/ghashv8-armv8-apple.S @@ -104,6 +104,7 @@ _gcm_init_v8: .private_extern _gcm_gmult_v8 .align 4 +.alt_entry _gcm_gmult_v8 _gcm_gmult_v8: AARCH64_VALID_CALL_TARGET ld1 {v17.2d},[x0] //load Xi @@ -147,6 +148,7 @@ _gcm_gmult_v8: .private_extern _gcm_ghash_v8 .align 4 +.alt_entry _gcm_ghash_v8 _gcm_ghash_v8: AARCH64_VALID_CALL_TARGET cmp x3,#64 @@ -279,6 +281,7 @@ Ldone_v8: .align 4 +.alt_entry gcm_ghash_v8_4x gcm_ghash_v8_4x: Lgcm_ghash_v8_4x: ld1 {v0.2d},[x0] //load [rotated] Xi diff --git a/third_party/boringssl/gen/bcm/p256-armv8-asm-apple.S b/third_party/boringssl/gen/bcm/p256-armv8-asm-apple.S index 283240fb1..5c206c47b 100644 --- a/third_party/boringssl/gen/bcm/p256-armv8-asm-apple.S +++ b/third_party/boringssl/gen/bcm/p256-armv8-asm-apple.S @@ -6,6 +6,7 @@ #if !defined(OPENSSL_NO_ASM) && defined(OPENSSL_AARCH64) && defined(__APPLE__) .section __TEXT,__const .align 5 +p256_constants: Lpoly: .quad 0xffffffffffffffff,0x00000000ffffffff,0x0000000000000000,0xffffffff00000001 LRR: // 2^512 mod P precomputed for NIST P256 polynomial @@ -55,6 +56,7 @@ _ecp_nistz256_mul_mont: .private_extern _ecp_nistz256_sqr_mont .align 4 +.alt_entry _ecp_nistz256_sqr_mont _ecp_nistz256_sqr_mont: AARCH64_SIGN_LINK_REGISTER stp x29,x30,[sp,#-32]! @@ -81,6 +83,7 @@ _ecp_nistz256_sqr_mont: .private_extern _ecp_nistz256_div_by_2 .align 4 +.alt_entry _ecp_nistz256_div_by_2 _ecp_nistz256_div_by_2: AARCH64_SIGN_LINK_REGISTER stp x29,x30,[sp,#-16]! @@ -105,6 +108,7 @@ _ecp_nistz256_div_by_2: .private_extern _ecp_nistz256_mul_by_2 .align 4 +.alt_entry _ecp_nistz256_mul_by_2 _ecp_nistz256_mul_by_2: AARCH64_SIGN_LINK_REGISTER stp x29,x30,[sp,#-16]! @@ -133,6 +137,7 @@ _ecp_nistz256_mul_by_2: .private_extern _ecp_nistz256_mul_by_3 .align 4 +.alt_entry _ecp_nistz256_mul_by_3 _ecp_nistz256_mul_by_3: AARCH64_SIGN_LINK_REGISTER stp x29,x30,[sp,#-16]! @@ -173,6 +178,7 @@ _ecp_nistz256_mul_by_3: .private_extern _ecp_nistz256_sub .align 4 +.alt_entry _ecp_nistz256_sub _ecp_nistz256_sub: AARCH64_SIGN_LINK_REGISTER stp x29,x30,[sp,#-16]! @@ -197,6 +203,7 @@ _ecp_nistz256_sub: .private_extern _ecp_nistz256_neg .align 4 +.alt_entry _ecp_nistz256_neg _ecp_nistz256_neg: AARCH64_SIGN_LINK_REGISTER stp x29,x30,[sp,#-16]! @@ -223,6 +230,7 @@ _ecp_nistz256_neg: // to x4-x7 and b[0] - to x3 .align 4 +.alt_entry __ecp_nistz256_mul_mont __ecp_nistz256_mul_mont: mul x14,x4,x3 // a[0]*b[0] umulh x8,x4,x3 @@ -359,6 +367,7 @@ __ecp_nistz256_mul_mont: // to x4-x7 .align 4 +.alt_entry __ecp_nistz256_sqr_mont __ecp_nistz256_sqr_mont: // | | | | | |a1*a0| | // | | | | |a2*a0| | | @@ -483,6 +492,7 @@ __ecp_nistz256_sqr_mont: // contexts, e.g. in multiplication by 2 and 3... .align 4 +.alt_entry __ecp_nistz256_add_to __ecp_nistz256_add_to: adds x14,x14,x8 // ret = a+b adcs x15,x15,x9 @@ -508,6 +518,7 @@ __ecp_nistz256_add_to: .align 4 +.alt_entry __ecp_nistz256_sub_from __ecp_nistz256_sub_from: ldp x8,x9,[x2] ldp x10,x11,[x2,#16] @@ -535,6 +546,7 @@ __ecp_nistz256_sub_from: .align 4 +.alt_entry __ecp_nistz256_sub_morf __ecp_nistz256_sub_morf: ldp x8,x9,[x2] ldp x10,x11,[x2,#16] @@ -562,6 +574,7 @@ __ecp_nistz256_sub_morf: .align 4 +.alt_entry __ecp_nistz256_div_by_2 __ecp_nistz256_div_by_2: subs x8,x14,#1 // adds x8,x4,#-1 // tmp = a+modulus adcs x9,x15,x12 @@ -593,6 +606,7 @@ __ecp_nistz256_div_by_2: .private_extern _ecp_nistz256_point_double .align 5 +.alt_entry _ecp_nistz256_point_double _ecp_nistz256_point_double: AARCH64_SIGN_LINK_REGISTER stp x29,x30,[sp,#-96]! @@ -738,6 +752,7 @@ Ldouble_shortcut: .private_extern _ecp_nistz256_point_add .align 5 +.alt_entry _ecp_nistz256_point_add _ecp_nistz256_point_add: AARCH64_SIGN_LINK_REGISTER stp x29,x30,[sp,#-96]! @@ -995,6 +1010,7 @@ Ladd_done: .private_extern _ecp_nistz256_point_add_affine .align 5 +.alt_entry _ecp_nistz256_point_add_affine _ecp_nistz256_point_add_affine: AARCH64_SIGN_LINK_REGISTER stp x29,x30,[sp,#-80]! @@ -1208,6 +1224,7 @@ _ecp_nistz256_point_add_affine: .private_extern _ecp_nistz256_ord_mul_mont .align 4 +.alt_entry _ecp_nistz256_ord_mul_mont _ecp_nistz256_ord_mul_mont: AARCH64_VALID_CALL_TARGET // Armv8.3-A PAuth: even though x30 is pushed to stack it is not popped later. @@ -1420,6 +1437,7 @@ _ecp_nistz256_ord_mul_mont: .private_extern _ecp_nistz256_ord_sqr_mont .align 4 +.alt_entry _ecp_nistz256_ord_sqr_mont _ecp_nistz256_ord_sqr_mont: AARCH64_VALID_CALL_TARGET // Armv8.3-A PAuth: even though x30 is pushed to stack it is not popped later. @@ -1612,6 +1630,7 @@ Loop_ord_sqr: .private_extern _ecp_nistz256_select_w5 .align 4 +.alt_entry _ecp_nistz256_select_w5 _ecp_nistz256_select_w5: AARCH64_VALID_CALL_TARGET @@ -1676,6 +1695,7 @@ Lselect_w5_loop: .private_extern _ecp_nistz256_select_w7 .align 4 +.alt_entry _ecp_nistz256_select_w7 _ecp_nistz256_select_w7: AARCH64_VALID_CALL_TARGET diff --git a/third_party/boringssl/gen/bcm/p256-armv8-asm-linux.S b/third_party/boringssl/gen/bcm/p256-armv8-asm-linux.S index 1d63f0242..56d5f004a 100644 --- a/third_party/boringssl/gen/bcm/p256-armv8-asm-linux.S +++ b/third_party/boringssl/gen/bcm/p256-armv8-asm-linux.S @@ -6,6 +6,7 @@ #if !defined(OPENSSL_NO_ASM) && defined(OPENSSL_AARCH64) && defined(__ELF__) .section .rodata .align 5 +p256_constants: .Lpoly: .quad 0xffffffffffffffff,0x00000000ffffffff,0x0000000000000000,0xffffffff00000001 .LRR: // 2^512 mod P precomputed for NIST P256 polynomial diff --git a/third_party/boringssl/gen/bcm/p256-armv8-asm-win.S b/third_party/boringssl/gen/bcm/p256-armv8-asm-win.S index f15b8ab01..fd80d7742 100644 --- a/third_party/boringssl/gen/bcm/p256-armv8-asm-win.S +++ b/third_party/boringssl/gen/bcm/p256-armv8-asm-win.S @@ -6,6 +6,7 @@ #if !defined(OPENSSL_NO_ASM) && defined(OPENSSL_AARCH64) && defined(_WIN32) .section .rodata .align 5 +p256_constants: Lpoly: .quad 0xffffffffffffffff,0x00000000ffffffff,0x0000000000000000,0xffffffff00000001 LRR: // 2^512 mod P precomputed for NIST P256 polynomial diff --git a/third_party/boringssl/gen/bcm/p256-x86_64-asm-apple.S b/third_party/boringssl/gen/bcm/p256-x86_64-asm-apple.S index 80ffa014d..eb8000d48 100644 --- a/third_party/boringssl/gen/bcm/p256-x86_64-asm-apple.S +++ b/third_party/boringssl/gen/bcm/p256-x86_64-asm-apple.S @@ -6,9 +6,10 @@ #if !defined(OPENSSL_NO_ASM) && defined(OPENSSL_X86_64) && defined(__APPLE__) .text - +// The polynomial .section __DATA,__const .p2align 6 +p256_constants: L$poly: .quad 0xffffffffffffffff, 0x00000000ffffffff, 0x0000000000000000, 0xffffffff00000001 @@ -21,15 +22,15 @@ L$Three: L$ONE_mont: .quad 0x0000000000000001, 0xffffffff00000000, 0xffffffffffffffff, 0x00000000fffffffe - +// Constants for computations modulo ord(p256) L$ord: .quad 0xf3b9cac2fc632551, 0xbce6faada7179e84, 0xffffffffffffffff, 0xffffffff00000000 L$ordK: .quad 0xccd1c8aaee00bc4f .text - - +// ############################################################################## +// void ecp_nistz256_neg(uint64_t res[4], uint64_t a[4]); .globl _ecp_nistz256_neg .private_extern _ecp_nistz256_neg @@ -85,16 +86,17 @@ L$neg_epilogue: ret - - - - - +// ############################################################################## +// void ecp_nistz256_ord_mul_mont( +// uint64_t res[4], +// uint64_t a[4], +// uint64_t b[4]); .globl _ecp_nistz256_ord_mul_mont_nohw .private_extern _ecp_nistz256_ord_mul_mont_nohw .p2align 5 +.alt_entry _ecp_nistz256_ord_mul_mont_nohw _ecp_nistz256_ord_mul_mont_nohw: _CET_ENDBR @@ -117,7 +119,7 @@ L$ord_mul_body: leaq L$ord(%rip),%r14 movq L$ordK(%rip),%r15 - +// ################################ * b[0] movq %rax,%rcx mulq 0(%rsi) movq %rax,%r8 @@ -145,16 +147,16 @@ L$ord_mul_body: adcq $0,%rdx movq %rdx,%r12 - +// ################################ First reduction step mulq 0(%r14) movq %r8,%rbp - addq %rax,%r13 + addq %rax,%r13 // guaranteed to be zero movq %r8,%rax adcq $0,%rdx movq %rdx,%rcx subq %r8,%r10 - sbbq $0,%r8 + sbbq $0,%r8 // can't borrow mulq 8(%r14) addq %rcx,%r9 @@ -163,19 +165,19 @@ L$ord_mul_body: movq %rbp,%rax adcq %rdx,%r10 movq %rbp,%rdx - adcq $0,%r8 + adcq $0,%r8 // can't overflow shlq $32,%rax shrq $32,%rdx subq %rax,%r11 movq 8(%rbx),%rax - sbbq %rdx,%rbp + sbbq %rdx,%rbp // can't borrow addq %r8,%r11 adcq %rbp,%r12 adcq $0,%r13 - +// ################################ * b[1] movq %rax,%rcx mulq 0(%rsi) addq %rax,%r9 @@ -211,15 +213,15 @@ L$ord_mul_body: adcq %rdx,%r13 adcq $0,%r8 - +// ################################ Second reduction step mulq 0(%r14) movq %r9,%rbp - addq %rax,%rcx + addq %rax,%rcx // guaranteed to be zero movq %r9,%rax adcq %rdx,%rcx subq %r9,%r11 - sbbq $0,%r9 + sbbq $0,%r9 // can't borrow mulq 8(%r14) addq %rcx,%r10 @@ -228,19 +230,19 @@ L$ord_mul_body: movq %rbp,%rax adcq %rdx,%r11 movq %rbp,%rdx - adcq $0,%r9 + adcq $0,%r9 // can't overflow shlq $32,%rax shrq $32,%rdx subq %rax,%r12 movq 16(%rbx),%rax - sbbq %rdx,%rbp + sbbq %rdx,%rbp // can't borrow addq %r9,%r12 adcq %rbp,%r13 adcq $0,%r8 - +// ################################# * b[2] movq %rax,%rcx mulq 0(%rsi) addq %rax,%r10 @@ -276,15 +278,15 @@ L$ord_mul_body: adcq %rdx,%r8 adcq $0,%r9 - +// ################################ Third reduction step mulq 0(%r14) movq %r10,%rbp - addq %rax,%rcx + addq %rax,%rcx // guaranteed to be zero movq %r10,%rax adcq %rdx,%rcx subq %r10,%r12 - sbbq $0,%r10 + sbbq $0,%r10 // can't borrow mulq 8(%r14) addq %rcx,%r11 @@ -293,19 +295,19 @@ L$ord_mul_body: movq %rbp,%rax adcq %rdx,%r12 movq %rbp,%rdx - adcq $0,%r10 + adcq $0,%r10 // can't overflow shlq $32,%rax shrq $32,%rdx subq %rax,%r13 movq 24(%rbx),%rax - sbbq %rdx,%rbp + sbbq %rdx,%rbp // can't borrow addq %r10,%r13 adcq %rbp,%r8 adcq $0,%r9 - +// ################################ * b[3] movq %rax,%rcx mulq 0(%rsi) addq %rax,%r11 @@ -341,15 +343,15 @@ L$ord_mul_body: adcq %rdx,%r9 adcq $0,%r10 - +// ################################ Last reduction step mulq 0(%r14) movq %r11,%rbp - addq %rax,%rcx + addq %rax,%rcx // guaranteed to be zero movq %r11,%rax adcq %rdx,%rcx subq %r11,%r13 - sbbq $0,%r11 + sbbq $0,%r11 // can't borrow mulq 8(%r14) addq %rcx,%r12 @@ -358,18 +360,18 @@ L$ord_mul_body: movq %rbp,%rax adcq %rdx,%r13 movq %rbp,%rdx - adcq $0,%r11 + adcq $0,%r11 // can't overflow shlq $32,%rax shrq $32,%rdx subq %rax,%r8 - sbbq %rdx,%rbp + sbbq %rdx,%rbp // can't borrow addq %r11,%r8 adcq %rbp,%r9 adcq $0,%r10 - +// ################################ Subtract ord movq %r12,%rsi subq 0(%r14),%r12 movq %r13,%r11 @@ -409,16 +411,17 @@ L$ord_mul_epilogue: - - - - - +// ############################################################################## +// void ecp_nistz256_ord_sqr_mont( +// uint64_t res[4], +// uint64_t a[4], +// uint64_t rep); .globl _ecp_nistz256_ord_sqr_mont_nohw .private_extern _ecp_nistz256_ord_sqr_mont_nohw .p2align 5 +.alt_entry _ecp_nistz256_ord_sqr_mont_nohw _ecp_nistz256_ord_sqr_mont_nohw: _CET_ENDBR @@ -440,56 +443,56 @@ L$ord_sqr_body: movq 8(%rsi),%rax movq 16(%rsi),%r14 movq 24(%rsi),%r15 - leaq L$ord(%rip),%rsi + leaq L$ord(%rip),%rsi // pointer to modulus movq %rdx,%rbx jmp L$oop_ord_sqr .p2align 5 L$oop_ord_sqr: - - movq %rax,%rbp - mulq %r8 +// ################################ a[1:] * a[0] + movq %rax,%rbp // put aside a[1] + mulq %r8 // a[1] * a[0] movq %rax,%r9 - movq %rbp,%xmm1 + movq %rbp,%xmm1 // offload a[1] movq %r14,%rax movq %rdx,%r10 - mulq %r8 + mulq %r8 // a[2] * a[0] addq %rax,%r10 movq %r15,%rax - movq %r14,%xmm2 + movq %r14,%xmm2 // offload a[2] adcq $0,%rdx movq %rdx,%r11 - mulq %r8 + mulq %r8 // a[3] * a[0] addq %rax,%r11 movq %r15,%rax - movq %r15,%xmm3 + movq %r15,%xmm3 // offload a[3] adcq $0,%rdx movq %rdx,%r12 - - mulq %r14 +// ################################ a[3] * a[2] + mulq %r14 // a[3] * a[2] movq %rax,%r13 movq %r14,%rax movq %rdx,%r14 - - mulq %rbp +// ################################ a[2:] * a[1] + mulq %rbp // a[2] * a[1] addq %rax,%r11 movq %r15,%rax adcq $0,%rdx movq %rdx,%r15 - mulq %rbp + mulq %rbp // a[3] * a[1] addq %rax,%r12 adcq $0,%rdx addq %r15,%r12 adcq %rdx,%r13 - adcq $0,%r14 - + adcq $0,%r14 // can't overflow +// ################################ *2 xorq %r15,%r15 movq %r8,%rax addq %r9,%r9 @@ -500,20 +503,20 @@ L$oop_ord_sqr: adcq %r14,%r14 adcq $0,%r15 - - mulq %rax +// ################################ Missing products + mulq %rax // a[0] * a[0] movq %rax,%r8 movq %xmm1,%rax movq %rdx,%rbp - mulq %rax + mulq %rax // a[1] * a[1] addq %rbp,%r9 adcq %rax,%r10 movq %xmm2,%rax adcq $0,%rdx movq %rdx,%rbp - mulq %rax + mulq %rax // a[2] * a[2] addq %rbp,%r11 adcq %rax,%r12 movq %xmm3,%rax @@ -521,23 +524,23 @@ L$oop_ord_sqr: movq %rdx,%rbp movq %r8,%rcx - imulq 32(%rsi),%r8 + imulq 32(%rsi),%r8 // *= .LordK - mulq %rax + mulq %rax // a[3] * a[3] addq %rbp,%r13 adcq %rax,%r14 - movq 0(%rsi),%rax - adcq %rdx,%r15 - + movq 0(%rsi),%rax // modulus[0] + adcq %rdx,%r15 // can't overflow +// ################################ First reduction step mulq %r8 movq %r8,%rbp - addq %rax,%rcx - movq 8(%rsi),%rax + addq %rax,%rcx // guaranteed to be zero + movq 8(%rsi),%rax // modulus[1] adcq %rdx,%rcx subq %r8,%r10 - sbbq $0,%rbp + sbbq $0,%rbp // can't borrow mulq %r8 addq %rcx,%r9 @@ -546,29 +549,29 @@ L$oop_ord_sqr: movq %r8,%rax adcq %rdx,%r10 movq %r8,%rdx - adcq $0,%rbp + adcq $0,%rbp // can't overflow movq %r9,%rcx - imulq 32(%rsi),%r9 + imulq 32(%rsi),%r9 // *= .LordK shlq $32,%rax shrq $32,%rdx subq %rax,%r11 movq 0(%rsi),%rax - sbbq %rdx,%r8 + sbbq %rdx,%r8 // can't borrow addq %rbp,%r11 - adcq $0,%r8 - + adcq $0,%r8 // can't overflow +// ################################ Second reduction step mulq %r9 movq %r9,%rbp - addq %rax,%rcx + addq %rax,%rcx // guaranteed to be zero movq 8(%rsi),%rax adcq %rdx,%rcx subq %r9,%r11 - sbbq $0,%rbp + sbbq $0,%rbp // can't borrow mulq %r9 addq %rcx,%r10 @@ -577,29 +580,29 @@ L$oop_ord_sqr: movq %r9,%rax adcq %rdx,%r11 movq %r9,%rdx - adcq $0,%rbp + adcq $0,%rbp // can't overflow movq %r10,%rcx - imulq 32(%rsi),%r10 + imulq 32(%rsi),%r10 // *= .LordK shlq $32,%rax shrq $32,%rdx subq %rax,%r8 movq 0(%rsi),%rax - sbbq %rdx,%r9 + sbbq %rdx,%r9 // can't borrow addq %rbp,%r8 - adcq $0,%r9 - + adcq $0,%r9 // can't overflow +// ################################ Third reduction step mulq %r10 movq %r10,%rbp - addq %rax,%rcx + addq %rax,%rcx // guaranteed to be zero movq 8(%rsi),%rax adcq %rdx,%rcx subq %r10,%r8 - sbbq $0,%rbp + sbbq $0,%rbp // can't borrow mulq %r10 addq %rcx,%r11 @@ -608,29 +611,29 @@ L$oop_ord_sqr: movq %r10,%rax adcq %rdx,%r8 movq %r10,%rdx - adcq $0,%rbp + adcq $0,%rbp // can't overflow movq %r11,%rcx - imulq 32(%rsi),%r11 + imulq 32(%rsi),%r11 // *= .LordK shlq $32,%rax shrq $32,%rdx subq %rax,%r9 movq 0(%rsi),%rax - sbbq %rdx,%r10 + sbbq %rdx,%r10 // can't borrow addq %rbp,%r9 - adcq $0,%r10 - + adcq $0,%r10 // can't overflow +// ################################ Last reduction step mulq %r11 movq %r11,%rbp - addq %rax,%rcx + addq %rax,%rcx // guaranteed to be zero movq 8(%rsi),%rax adcq %rdx,%rcx subq %r11,%r9 - sbbq $0,%rbp + sbbq $0,%rbp // can't borrow mulq %r11 addq %rcx,%r8 @@ -639,17 +642,17 @@ L$oop_ord_sqr: movq %r11,%rax adcq %rdx,%r9 movq %r11,%rdx - adcq $0,%rbp + adcq $0,%rbp // can't overflow shlq $32,%rax shrq $32,%rdx subq %rax,%r10 - sbbq %rdx,%r11 + sbbq %rdx,%r11 // can't borrow addq %rbp,%r10 - adcq $0,%r11 - + adcq $0,%r11 // can't overflow +// ################################ Add bits [511:256] of the sqr result xorq %rdx,%rdx addq %r12,%r8 adcq %r13,%r9 @@ -659,7 +662,7 @@ L$oop_ord_sqr: movq %r9,%rax adcq $0,%rdx - +// ################################ Compare to modulus subq 0(%rsi),%r8 movq %r10,%r14 sbbq 8(%rsi),%r9 @@ -702,11 +705,12 @@ L$ord_sqr_epilogue: ret - +// ############################################################################## .globl _ecp_nistz256_ord_mul_mont_adx .private_extern _ecp_nistz256_ord_mul_mont_adx .p2align 5 +.alt_entry _ecp_nistz256_ord_mul_mont_adx _ecp_nistz256_ord_mul_mont_adx: L$ecp_nistz256_ord_mul_mont_adx: @@ -731,11 +735,11 @@ L$ord_mulx_body: movq 8(%rsi),%r10 movq 16(%rsi),%r11 movq 24(%rsi),%r12 - leaq -128(%rsi),%rsi + leaq -128(%rsi),%rsi // control u-op density leaq L$ord-128(%rip),%r14 movq L$ordK(%rip),%r15 - +// ################################ Multiply by b[0] mulxq %r9,%r8,%r9 mulxq %r10,%rcx,%r10 mulxq %r11,%rbp,%r11 @@ -747,10 +751,10 @@ L$ord_mulx_body: adcq %rcx,%r11 adcq $0,%r12 - - xorq %r13,%r13 +// ################################ reduction + xorq %r13,%r13 // %r13=0, cf=0, of=0 mulxq 0+128(%r14),%rcx,%rbp - adcxq %rcx,%r8 + adcxq %rcx,%r8 // guaranteed to be zero adoxq %rbp,%r9 mulxq 8+128(%r14),%rcx,%rbp @@ -767,9 +771,9 @@ L$ord_mulx_body: adoxq %rbp,%r12 adcxq %r8,%r12 adoxq %r8,%r13 - adcq $0,%r13 - + adcq $0,%r13 // cf=0, of=0 +// ################################ Multiply by b[1] mulxq 0+128(%rsi),%rcx,%rbp adcxq %rcx,%r9 adoxq %rbp,%r10 @@ -790,11 +794,11 @@ L$ord_mulx_body: adcxq %r8,%r13 adoxq %r8,%r8 - adcq $0,%r8 - + adcq $0,%r8 // cf=0, of=0 +// ################################ reduction mulxq 0+128(%r14),%rcx,%rbp - adcxq %rcx,%r9 + adcxq %rcx,%r9 // guaranteed to be zero adoxq %rbp,%r10 mulxq 8+128(%r14),%rcx,%rbp @@ -811,9 +815,9 @@ L$ord_mulx_body: adoxq %rbp,%r13 adcxq %r9,%r13 adoxq %r9,%r8 - adcq $0,%r8 - + adcq $0,%r8 // cf=0, of=0 +// ################################ Multiply by b[2] mulxq 0+128(%rsi),%rcx,%rbp adcxq %rcx,%r10 adoxq %rbp,%r11 @@ -834,11 +838,11 @@ L$ord_mulx_body: adcxq %r9,%r8 adoxq %r9,%r9 - adcq $0,%r9 - + adcq $0,%r9 // cf=0, of=0 +// ################################ reduction mulxq 0+128(%r14),%rcx,%rbp - adcxq %rcx,%r10 + adcxq %rcx,%r10 // guaranteed to be zero adoxq %rbp,%r11 mulxq 8+128(%r14),%rcx,%rbp @@ -855,9 +859,9 @@ L$ord_mulx_body: adoxq %rbp,%r8 adcxq %r10,%r8 adoxq %r10,%r9 - adcq $0,%r9 - + adcq $0,%r9 // cf=0, of=0 +// ################################ Multiply by b[3] mulxq 0+128(%rsi),%rcx,%rbp adcxq %rcx,%r11 adoxq %rbp,%r12 @@ -878,11 +882,11 @@ L$ord_mulx_body: adcxq %r10,%r9 adoxq %r10,%r10 - adcq $0,%r10 - + adcq $0,%r10 // cf=0, of=0 +// ################################ reduction mulxq 0+128(%r14),%rcx,%rbp - adcxq %rcx,%r11 + adcxq %rcx,%r11 // guaranteed to be zero adoxq %rbp,%r12 mulxq 8+128(%r14),%rcx,%rbp @@ -903,8 +907,8 @@ L$ord_mulx_body: adoxq %r11,%r10 adcq $0,%r10 - - +// ################################ +// Branch-less conditional subtraction of P movq %r8,%rcx subq 0(%r14),%r12 sbbq 8(%r14),%r13 @@ -946,6 +950,7 @@ L$ord_mulx_epilogue: .private_extern _ecp_nistz256_ord_sqr_mont_adx .p2align 5 +.alt_entry _ecp_nistz256_ord_sqr_mont_adx _ecp_nistz256_ord_sqr_mont_adx: _CET_ENDBR @@ -974,38 +979,38 @@ L$ord_sqrx_body: .p2align 5 L$oop_ord_sqrx: - mulxq %r14,%r9,%r10 - mulxq %r15,%rcx,%r11 - movq %rdx,%rax - movq %r14,%xmm1 - mulxq %r8,%rbp,%r12 + mulxq %r14,%r9,%r10 // a[0]*a[1] + mulxq %r15,%rcx,%r11 // a[0]*a[2] + movq %rdx,%rax // offload a[0] + movq %r14,%xmm1 // offload a[1] + mulxq %r8,%rbp,%r12 // a[0]*a[3] movq %r14,%rdx addq %rcx,%r10 - movq %r15,%xmm2 + movq %r15,%xmm2 // offload a[2] adcq %rbp,%r11 adcq $0,%r12 - xorq %r13,%r13 - - mulxq %r15,%rcx,%rbp + xorq %r13,%r13 // %r13=0,cf=0,of=0 +// ################################ + mulxq %r15,%rcx,%rbp // a[1]*a[2] adcxq %rcx,%r11 adoxq %rbp,%r12 - mulxq %r8,%rcx,%rbp + mulxq %r8,%rcx,%rbp // a[1]*a[3] movq %r15,%rdx adcxq %rcx,%r12 adoxq %rbp,%r13 adcq $0,%r13 - - mulxq %r8,%rcx,%r14 +// ################################ + mulxq %r8,%rcx,%r14 // a[2]*a[3] movq %rax,%rdx - movq %r8,%xmm3 - xorq %r15,%r15 - adcxq %r9,%r9 + movq %r8,%xmm3 // offload a[3] + xorq %r15,%r15 // %r15=0,cf=0,of=0 + adcxq %r9,%r9 // acc1:6<<1 adoxq %rcx,%r13 adcxq %r10,%r10 - adoxq %r15,%r14 - + adoxq %r15,%r14 // of=0 +// ################################ a[i]*a[i] mulxq %rdx,%r8,%rbp movq %xmm1,%rdx adcxq %r11,%r11 @@ -1027,13 +1032,13 @@ L$oop_ord_sqrx: adoxq %rcx,%r14 adoxq %rax,%r15 - +// ################################ reduction movq %r8,%rdx mulxq 32(%rsi),%rdx,%rcx - xorq %rax,%rax + xorq %rax,%rax // cf=0, of=0 mulxq 0(%rsi),%rcx,%rbp - adcxq %rcx,%r8 + adcxq %rcx,%r8 // guaranteed to be zero adoxq %rbp,%r9 mulxq 8(%rsi),%rcx,%rbp adcxq %rcx,%r9 @@ -1043,15 +1048,15 @@ L$oop_ord_sqrx: adoxq %rbp,%r11 mulxq 24(%rsi),%rcx,%rbp adcxq %rcx,%r11 - adoxq %rbp,%r8 - adcxq %rax,%r8 - + adoxq %rbp,%r8 // of=0 + adcxq %rax,%r8 // cf=0 +// ################################ movq %r9,%rdx mulxq 32(%rsi),%rdx,%rcx mulxq 0(%rsi),%rcx,%rbp - adoxq %rcx,%r9 + adoxq %rcx,%r9 // guaranteed to be zero adcxq %rbp,%r10 mulxq 8(%rsi),%rcx,%rbp adoxq %rcx,%r10 @@ -1061,15 +1066,15 @@ L$oop_ord_sqrx: adcxq %rbp,%r8 mulxq 24(%rsi),%rcx,%rbp adoxq %rcx,%r8 - adcxq %rbp,%r9 - adoxq %rax,%r9 - + adcxq %rbp,%r9 // cf=0 + adoxq %rax,%r9 // of=0 +// ################################ movq %r10,%rdx mulxq 32(%rsi),%rdx,%rcx mulxq 0(%rsi),%rcx,%rbp - adcxq %rcx,%r10 + adcxq %rcx,%r10 // guaranteed to be zero adoxq %rbp,%r11 mulxq 8(%rsi),%rcx,%rbp adcxq %rcx,%r11 @@ -1079,15 +1084,15 @@ L$oop_ord_sqrx: adoxq %rbp,%r9 mulxq 24(%rsi),%rcx,%rbp adcxq %rcx,%r9 - adoxq %rbp,%r10 - adcxq %rax,%r10 - + adoxq %rbp,%r10 // of=0 + adcxq %rax,%r10 // cf=0 +// ################################ movq %r11,%rdx mulxq 32(%rsi),%rdx,%rcx mulxq 0(%rsi),%rcx,%rbp - adoxq %rcx,%r11 + adoxq %rcx,%r11 // guaranteed to be zero adcxq %rbp,%r8 mulxq 8(%rsi),%rcx,%rbp adoxq %rcx,%r8 @@ -1100,8 +1105,8 @@ L$oop_ord_sqrx: adcxq %rbp,%r11 adoxq %rax,%r11 - - addq %r8,%r12 +// ################################ accumulate upper half + addq %r8,%r12 // add %r12, %r8 adcq %r13,%r9 movq %r12,%rdx adcq %r14,%r10 @@ -1109,7 +1114,7 @@ L$oop_ord_sqrx: movq %r9,%r14 adcq $0,%rax - +// ################################ compare to modulus subq 0(%rsi),%r12 movq %r10,%r15 sbbq 8(%rsi),%r9 @@ -1152,16 +1157,17 @@ L$ord_sqrx_epilogue: ret - - - - - +// ############################################################################## +// void ecp_nistz256_mul_mont( +// uint64_t res[4], +// uint64_t a[4], +// uint64_t b[4]); .globl _ecp_nistz256_mul_mont_nohw .private_extern _ecp_nistz256_mul_mont_nohw .p2align 5 +.alt_entry _ecp_nistz256_mul_mont_nohw _ecp_nistz256_mul_mont_nohw: _CET_ENDBR @@ -1208,10 +1214,11 @@ L$mul_epilogue: .p2align 5 +.alt_entry __ecp_nistz256_mul_montq __ecp_nistz256_mul_montq: - - +// ####################################################################### +// Multiply a by b[0] movq %rax,%rbp mulq %r9 movq L$poly+8(%rip),%r14 @@ -1239,20 +1246,20 @@ __ecp_nistz256_mul_montq: xorq %r13,%r13 movq %rdx,%r12 - - - - - - - - +// ####################################################################### +// First reduction step +// Basically now we want to multiply acc[0] by p256, +// and add the result to the acc. +// Due to the special form of p256 we do some optimizations +// +// acc[0] x p256[0..1] = acc[0] x 2^96 - acc[0] +// then we add acc[0] and get acc[0] x 2^96 movq %r8,%rbp shlq $32,%r8 mulq %r15 shrq $32,%rbp - addq %r8,%r9 + addq %r8,%r9 // +=acc[0]<<96 adcq %rbp,%r10 adcq %rax,%r11 movq 8(%rbx),%rax @@ -1260,8 +1267,8 @@ __ecp_nistz256_mul_montq: adcq $0,%r13 xorq %r8,%r8 - - +// ####################################################################### +// Multiply by b[1] movq %rax,%rbp mulq 0(%rsi) addq %rax,%r9 @@ -1293,8 +1300,8 @@ __ecp_nistz256_mul_montq: adcq %rdx,%r13 adcq $0,%r8 - - +// ####################################################################### +// Second reduction step movq %r9,%rbp shlq $32,%r9 mulq %r15 @@ -1307,8 +1314,8 @@ __ecp_nistz256_mul_montq: adcq $0,%r8 xorq %r9,%r9 - - +// ####################################################################### +// Multiply by b[2] movq %rax,%rbp mulq 0(%rsi) addq %rax,%r10 @@ -1340,8 +1347,8 @@ __ecp_nistz256_mul_montq: adcq %rdx,%r8 adcq $0,%r9 - - +// ####################################################################### +// Third reduction step movq %r10,%rbp shlq $32,%r10 mulq %r15 @@ -1354,8 +1361,8 @@ __ecp_nistz256_mul_montq: adcq $0,%r9 xorq %r10,%r10 - - +// ####################################################################### +// Multiply by b[3] movq %rax,%rbp mulq 0(%rsi) addq %rax,%r11 @@ -1387,8 +1394,8 @@ __ecp_nistz256_mul_montq: adcq %rdx,%r9 adcq $0,%r10 - - +// ####################################################################### +// Final reduction step movq %r11,%rbp shlq $32,%r11 mulq %r15 @@ -1401,14 +1408,14 @@ __ecp_nistz256_mul_montq: movq %r13,%rbp adcq $0,%r10 - - - subq $-1,%r12 +// ####################################################################### +// Branch-less conditional subtraction of P + subq $-1,%r12 // .Lpoly[0] movq %r8,%rbx - sbbq %r14,%r13 - sbbq $0,%r8 + sbbq %r14,%r13 // .Lpoly[1] + sbbq $0,%r8 // .Lpoly[2] movq %r9,%rdx - sbbq %r15,%r9 + sbbq %r15,%r9 // .Lpoly[3] sbbq $0,%r10 cmovcq %rcx,%r12 @@ -1424,17 +1431,18 @@ __ecp_nistz256_mul_montq: +// ############################################################################## +// void ecp_nistz256_sqr_mont( +// uint64_t res[4], +// uint64_t a[4]); - - - - - - +// we optimize the square according to S.Gueron and V.Krasnov, +// "Speeding up Big-Number Squaring" .globl _ecp_nistz256_sqr_mont_nohw .private_extern _ecp_nistz256_sqr_mont_nohw .p2align 5 +.alt_entry _ecp_nistz256_sqr_mont_nohw _ecp_nistz256_sqr_mont_nohw: _CET_ENDBR @@ -1479,34 +1487,35 @@ L$sqr_epilogue: .p2align 5 +.alt_entry __ecp_nistz256_sqr_montq __ecp_nistz256_sqr_montq: movq %rax,%r13 - mulq %r14 + mulq %r14 // a[1]*a[0] movq %rax,%r9 movq %r15,%rax movq %rdx,%r10 - mulq %r13 + mulq %r13 // a[0]*a[2] addq %rax,%r10 movq %r8,%rax adcq $0,%rdx movq %rdx,%r11 - mulq %r13 + mulq %r13 // a[0]*a[3] addq %rax,%r11 movq %r15,%rax adcq $0,%rdx movq %rdx,%r12 - - mulq %r14 +// ################################ + mulq %r14 // a[1]*a[2] addq %rax,%r11 movq %r8,%rax adcq $0,%rdx movq %rdx,%rbp - mulq %r14 + mulq %r14 // a[1]*a[3] addq %rax,%r12 movq %r8,%rax adcq $0,%rdx @@ -1514,15 +1523,15 @@ __ecp_nistz256_sqr_montq: movq %rdx,%r13 adcq $0,%r13 - - mulq %r15 +// ################################ + mulq %r15 // a[2]*a[3] xorq %r15,%r15 addq %rax,%r13 movq 0(%rsi),%rax movq %rdx,%r14 adcq $0,%r14 - addq %r9,%r9 + addq %r9,%r9 // acc1:6<<1 adcq %r10,%r10 adcq %r11,%r11 adcq %r12,%r12 @@ -1558,21 +1567,21 @@ __ecp_nistz256_sqr_montq: movq L$poly+8(%rip),%rsi movq L$poly+24(%rip),%rbp - - - +// ######################################### +// Now the reduction +// First iteration movq %r8,%rcx shlq $32,%r8 mulq %rbp shrq $32,%rcx - addq %r8,%r9 + addq %r8,%r9 // +=acc[0]<<96 adcq %rcx,%r10 adcq %rax,%r11 movq %r9,%rax adcq $0,%rdx - - +// ######################################### +// Second iteration movq %r9,%rcx shlq $32,%r9 movq %rdx,%r8 @@ -1584,8 +1593,8 @@ __ecp_nistz256_sqr_montq: movq %r10,%rax adcq $0,%rdx - - +// ######################################### +// Third iteration movq %r10,%rcx shlq $32,%r10 movq %rdx,%r9 @@ -1597,8 +1606,8 @@ __ecp_nistz256_sqr_montq: movq %r11,%rax adcq $0,%rdx - - +// ########################################## +// Last iteration movq %r11,%rcx shlq $32,%r11 movq %rdx,%r10 @@ -1610,8 +1619,8 @@ __ecp_nistz256_sqr_montq: adcq $0,%rdx xorq %r11,%r11 - - +// ########################################### +// Add the rest of the acc addq %r8,%r12 adcq %r9,%r13 movq %r12,%r8 @@ -1620,12 +1629,12 @@ __ecp_nistz256_sqr_montq: movq %r13,%r9 adcq $0,%r11 - subq $-1,%r12 + subq $-1,%r12 // .Lpoly[0] movq %r14,%r10 - sbbq %rsi,%r13 - sbbq $0,%r14 + sbbq %rsi,%r13 // .Lpoly[1] + sbbq $0,%r14 // .Lpoly[2] movq %r15,%rcx - sbbq %rbp,%r15 + sbbq %rbp,%r15 // .Lpoly[3] sbbq $0,%r11 cmovcq %r8,%r12 @@ -1644,6 +1653,7 @@ __ecp_nistz256_sqr_montq: .private_extern _ecp_nistz256_mul_mont_adx .p2align 5 +.alt_entry _ecp_nistz256_mul_mont_adx _ecp_nistz256_mul_mont_adx: _CET_ENDBR @@ -1666,7 +1676,7 @@ L$mulx_body: movq 8(%rsi),%r10 movq 16(%rsi),%r11 movq 24(%rsi),%r12 - leaq -128(%rsi),%rsi + leaq -128(%rsi),%rsi // control u-op density call __ecp_nistz256_mul_montx @@ -1691,14 +1701,15 @@ L$mulx_epilogue: .p2align 5 +.alt_entry __ecp_nistz256_mul_montx __ecp_nistz256_mul_montx: - - +// ####################################################################### +// Multiply by b[0] mulxq %r9,%r8,%r9 mulxq %r10,%rcx,%r10 movq $32,%r14 - xorq %r13,%r13 + xorq %r13,%r13 // cf=0 mulxq %r11,%rbp,%r11 movq L$poly+24(%rip),%r15 adcq %rcx,%r9 @@ -1710,8 +1721,8 @@ __ecp_nistz256_mul_montx: shrxq %r14,%r8,%rcx adcq $0,%r12 - - +// ####################################################################### +// First reduction step addq %rbp,%r9 adcq %rcx,%r10 @@ -1720,10 +1731,10 @@ __ecp_nistz256_mul_montx: adcq %rcx,%r11 adcq %rbp,%r12 adcq $0,%r13 - xorq %r8,%r8 - - + xorq %r8,%r8 // %r8=0,cf=0,of=0 +// ####################################################################### +// Multiply by b[1] mulxq 0+128(%rsi),%rcx,%rbp adcxq %rcx,%r9 adoxq %rbp,%r10 @@ -1747,8 +1758,8 @@ __ecp_nistz256_mul_montx: adoxq %r8,%r8 adcq $0,%r8 - - +// ####################################################################### +// Second reduction step addq %rcx,%r10 adcq %rbp,%r11 @@ -1757,10 +1768,10 @@ __ecp_nistz256_mul_montx: adcq %rcx,%r12 adcq %rbp,%r13 adcq $0,%r8 - xorq %r9,%r9 - - + xorq %r9,%r9 // %r9=0,cf=0,of=0 +// ####################################################################### +// Multiply by b[2] mulxq 0+128(%rsi),%rcx,%rbp adcxq %rcx,%r10 adoxq %rbp,%r11 @@ -1784,8 +1795,8 @@ __ecp_nistz256_mul_montx: adoxq %r9,%r9 adcq $0,%r9 - - +// ####################################################################### +// Third reduction step addq %rcx,%r11 adcq %rbp,%r12 @@ -1794,10 +1805,10 @@ __ecp_nistz256_mul_montx: adcq %rcx,%r13 adcq %rbp,%r8 adcq $0,%r9 - xorq %r10,%r10 - - + xorq %r10,%r10 // %r10=0,cf=0,of=0 +// ####################################################################### +// Multiply by b[3] mulxq 0+128(%rsi),%rcx,%rbp adcxq %rcx,%r11 adoxq %rbp,%r12 @@ -1821,8 +1832,8 @@ __ecp_nistz256_mul_montx: adoxq %r10,%r10 adcq $0,%r10 - - +// ####################################################################### +// Fourth reduction step addq %rcx,%r12 adcq %rbp,%r13 @@ -1834,15 +1845,15 @@ __ecp_nistz256_mul_montx: adcq %rbp,%r9 adcq $0,%r10 - - +// ####################################################################### +// Branch-less conditional subtraction of P xorl %eax,%eax movq %r8,%rcx - sbbq $-1,%r12 - sbbq %r14,%r13 - sbbq $0,%r8 + sbbq $-1,%r12 // .Lpoly[0] + sbbq %r14,%r13 // .Lpoly[1] + sbbq $0,%r8 // .Lpoly[2] movq %r9,%rbp - sbbq %r15,%r9 + sbbq %r15,%r9 // .Lpoly[3] sbbq $0,%r10 cmovcq %rbx,%r12 @@ -1862,6 +1873,7 @@ __ecp_nistz256_mul_montx: .private_extern _ecp_nistz256_sqr_mont_adx .p2align 5 +.alt_entry _ecp_nistz256_sqr_mont_adx _ecp_nistz256_sqr_mont_adx: _CET_ENDBR @@ -1882,7 +1894,7 @@ L$sqrx_body: movq 8(%rsi),%r14 movq 16(%rsi),%r15 movq 24(%rsi),%r8 - leaq -128(%rsi),%rsi + leaq -128(%rsi),%rsi // control u-op density call __ecp_nistz256_sqr_montx @@ -1907,37 +1919,38 @@ L$sqrx_epilogue: .p2align 5 +.alt_entry __ecp_nistz256_sqr_montx __ecp_nistz256_sqr_montx: - mulxq %r14,%r9,%r10 - mulxq %r15,%rcx,%r11 + mulxq %r14,%r9,%r10 // a[0]*a[1] + mulxq %r15,%rcx,%r11 // a[0]*a[2] xorl %eax,%eax adcq %rcx,%r10 - mulxq %r8,%rbp,%r12 + mulxq %r8,%rbp,%r12 // a[0]*a[3] movq %r14,%rdx adcq %rbp,%r11 adcq $0,%r12 - xorq %r13,%r13 + xorq %r13,%r13 // %r13=0,cf=0,of=0 - - mulxq %r15,%rcx,%rbp +// ################################ + mulxq %r15,%rcx,%rbp // a[1]*a[2] adcxq %rcx,%r11 adoxq %rbp,%r12 - mulxq %r8,%rcx,%rbp + mulxq %r8,%rcx,%rbp // a[1]*a[3] movq %r15,%rdx adcxq %rcx,%r12 adoxq %rbp,%r13 adcq $0,%r13 - - mulxq %r8,%rcx,%r14 +// ################################ + mulxq %r8,%rcx,%r14 // a[2]*a[3] movq 0+128(%rsi),%rdx - xorq %r15,%r15 - adcxq %r9,%r9 + xorq %r15,%r15 // %r15=0,cf=0,of=0 + adcxq %r9,%r9 // acc1:6<<1 adoxq %rcx,%r13 adcxq %r10,%r10 - adoxq %r15,%r14 + adoxq %r15,%r14 // of=0 mulxq %rdx,%r8,%rbp movq 8+128(%rsi),%rdx @@ -1966,7 +1979,7 @@ __ecp_nistz256_sqr_montx: shrxq %rsi,%r8,%rax movq %rdx,%rbp - +// reduction step 1 addq %rcx,%r9 adcq %rax,%r10 @@ -1976,7 +1989,7 @@ __ecp_nistz256_sqr_montx: adcq $0,%r8 shrxq %rsi,%r9,%rax - +// reduction step 2 addq %rcx,%r10 adcq %rax,%r11 @@ -1986,7 +1999,7 @@ __ecp_nistz256_sqr_montx: adcq $0,%r9 shrxq %rsi,%r10,%rax - +// reduction step 3 addq %rcx,%r11 adcq %rax,%r8 @@ -1996,7 +2009,7 @@ __ecp_nistz256_sqr_montx: adcq $0,%r10 shrxq %rsi,%r11,%rax - +// reduction step 4 addq %rcx,%r8 adcq %rax,%r9 @@ -2005,7 +2018,7 @@ __ecp_nistz256_sqr_montx: adcq $0,%r11 xorq %rdx,%rdx - addq %r8,%r12 + addq %r8,%r12 // accumulate upper half movq L$poly+8(%rip),%rsi adcq %r9,%r13 movq %r12,%r8 @@ -2014,12 +2027,12 @@ __ecp_nistz256_sqr_montx: movq %r13,%r9 adcq $0,%rdx - subq $-1,%r12 + subq $-1,%r12 // .Lpoly[0] movq %r14,%r10 - sbbq %rsi,%r13 - sbbq $0,%r14 + sbbq %rsi,%r13 // .Lpoly[1] + sbbq $0,%r14 // .Lpoly[2] movq %r15,%r11 - sbbq %rbp,%r15 + sbbq %rbp,%r15 // .Lpoly[3] sbbq $0,%rdx cmovcq %r8,%r12 @@ -2034,12 +2047,13 @@ __ecp_nistz256_sqr_montx: ret - - +// ############################################################################## +// void ecp_nistz256_select_w5_nohw(uint64_t *val, uint64_t *in_t, int index); .globl _ecp_nistz256_select_w5_nohw .private_extern _ecp_nistz256_select_w5_nohw .p2align 5 +.alt_entry _ecp_nistz256_select_w5_nohw _ecp_nistz256_select_w5_nohw: _CET_ENDBR @@ -2098,12 +2112,13 @@ L$select_loop_sse_w5: L$SEH_end_ecp_nistz256_select_w5_nohw: - - +// ############################################################################## +// void ecp_nistz256_select_w7_nohw(uint64_t *val, uint64_t *in_t, int index); .globl _ecp_nistz256_select_w7_nohw .private_extern _ecp_nistz256_select_w7_nohw .p2align 5 +.alt_entry _ecp_nistz256_select_w7_nohw _ecp_nistz256_select_w7_nohw: _CET_ENDBR @@ -2150,12 +2165,13 @@ L$select_loop_sse_w7: L$SEH_end_ecp_nistz256_select_w7_nohw: - - +// ############################################################################## +// void ecp_nistz256_select_w5_avx2(uint64_t *val, uint64_t *in_t, int index); .globl _ecp_nistz256_select_w5_avx2 .private_extern _ecp_nistz256_select_w5_avx2 .p2align 5 +.alt_entry _ecp_nistz256_select_w5_avx2 _ecp_nistz256_select_w5_avx2: _CET_ENDBR @@ -2216,12 +2232,13 @@ L$select_loop_avx2_w5: L$SEH_end_ecp_nistz256_select_w5_avx2: - - +// ############################################################################## +// void ecp_nistz256_select_w7_avx2(uint64_t *val, uint64_t *in_t, int index); .globl _ecp_nistz256_select_w7_avx2 .private_extern _ecp_nistz256_select_w7_avx2 .p2align 5 +.alt_entry _ecp_nistz256_select_w7_avx2 _ecp_nistz256_select_w7_avx2: _CET_ENDBR @@ -2237,7 +2254,7 @@ _CET_ENDBR vmovd %edx,%xmm1 vpermd %ymm1,%ymm2,%ymm1 - +// Skip index = 0, because it is implicitly the point at infinity movq $21,%rax L$select_loop_avx2_w7: @@ -2298,6 +2315,7 @@ L$SEH_end_ecp_nistz256_select_w7_avx2: .p2align 5 +.alt_entry __ecp_nistz256_add_toq __ecp_nistz256_add_toq: xorq %r11,%r11 @@ -2332,6 +2350,7 @@ __ecp_nistz256_add_toq: .p2align 5 +.alt_entry __ecp_nistz256_sub_fromq __ecp_nistz256_sub_fromq: subq 0(%rbx),%r12 @@ -2365,6 +2384,7 @@ __ecp_nistz256_sub_fromq: .p2align 5 +.alt_entry __ecp_nistz256_subq __ecp_nistz256_subq: subq %r12,%rax @@ -2394,10 +2414,11 @@ __ecp_nistz256_subq: .p2align 5 +.alt_entry __ecp_nistz256_mul_by_2q __ecp_nistz256_mul_by_2q: xorq %r11,%r11 - addq %r12,%r12 + addq %r12,%r12 // a0:a3+a0:a3 adcq %r13,%r13 movq %r12,%rax adcq %r8,%r8 @@ -2429,6 +2450,7 @@ __ecp_nistz256_mul_by_2q: .private_extern _ecp_nistz256_point_double_nohw .p2align 5 +.alt_entry _ecp_nistz256_point_double_nohw _ecp_nistz256_point_double_nohw: _CET_ENDBR @@ -2449,10 +2471,10 @@ _CET_ENDBR L$point_doubleq_body: L$point_double_shortcutq: - movdqu 0(%rsi),%xmm0 - movq %rsi,%rbx + movdqu 0(%rsi),%xmm0 // copy *(P256_POINT *)%rsi.x + movq %rsi,%rbx // backup copy movdqu 16(%rsi),%xmm1 - movq 32+0(%rsi),%r12 + movq 32+0(%rsi),%r12 // load in_y in "5-4-0-1" order movq 32+8(%rsi),%r13 movq 32+16(%rsi),%r8 movq 32+24(%rsi),%r9 @@ -2467,7 +2489,7 @@ L$point_double_shortcutq: movq %r11,%xmm2 leaq 0(%rsp),%rdi - call __ecp_nistz256_mul_by_2q + call __ecp_nistz256_mul_by_2q // p256_mul_by_2(S, in_y); movq 64+0(%rsi),%rax movq 64+8(%rsi),%r14 @@ -2475,7 +2497,7 @@ L$point_double_shortcutq: movq 64+24(%rsi),%r8 leaq 64-0(%rsi),%rsi leaq 64(%rsp),%rdi - call __ecp_nistz256_sqr_montq + call __ecp_nistz256_sqr_montq // p256_sqr_mont(Zsqr, in_z); movq 0+0(%rsp),%rax movq 8+0(%rsp),%r14 @@ -2483,9 +2505,9 @@ L$point_double_shortcutq: movq 16+0(%rsp),%r15 movq 24+0(%rsp),%r8 leaq 0(%rsp),%rdi - call __ecp_nistz256_sqr_montq + call __ecp_nistz256_sqr_montq // p256_sqr_mont(S, S); - movq 32(%rbx),%rax + movq 32(%rbx),%rax // %rbx is still valid movq 64+0(%rbx),%r9 movq 64+8(%rbx),%r10 movq 64+16(%rbx),%r11 @@ -2493,24 +2515,24 @@ L$point_double_shortcutq: leaq 64-0(%rbx),%rsi leaq 32(%rbx),%rbx movq %xmm2,%rdi - call __ecp_nistz256_mul_montq - call __ecp_nistz256_mul_by_2q + call __ecp_nistz256_mul_montq // p256_mul_mont(res_z, in_z, in_y); + call __ecp_nistz256_mul_by_2q // p256_mul_by_2(res_z, res_z); - movq 96+0(%rsp),%r12 + movq 96+0(%rsp),%r12 // "5-4-0-1" order movq 96+8(%rsp),%r13 leaq 64(%rsp),%rbx movq 96+16(%rsp),%r8 movq 96+24(%rsp),%r9 leaq 32(%rsp),%rdi - call __ecp_nistz256_add_toq + call __ecp_nistz256_add_toq // p256_add(M, in_x, Zsqr); - movq 96+0(%rsp),%r12 + movq 96+0(%rsp),%r12 // "5-4-0-1" order movq 96+8(%rsp),%r13 leaq 64(%rsp),%rbx movq 96+16(%rsp),%r8 movq 96+24(%rsp),%r9 leaq 64(%rsp),%rdi - call __ecp_nistz256_sub_fromq + call __ecp_nistz256_sub_fromq // p256_sub(Zsqr, in_x, Zsqr); movq 0+0(%rsp),%rax movq 8+0(%rsp),%r14 @@ -2518,7 +2540,7 @@ L$point_double_shortcutq: movq 16+0(%rsp),%r15 movq 24+0(%rsp),%r8 movq %xmm1,%rdi - call __ecp_nistz256_sqr_montq + call __ecp_nistz256_sqr_montq // p256_sqr_mont(res_y, S); xorq %r9,%r9 movq %r12,%rax addq $-1,%r12 @@ -2529,7 +2551,7 @@ L$point_double_shortcutq: movq %r15,%r8 adcq %rbp,%r15 adcq $0,%r9 - xorq %rsi,%rsi + xorq %rsi,%rsi // borrow %rsi testq $1,%rax cmovzq %rax,%r12 @@ -2538,7 +2560,7 @@ L$point_double_shortcutq: cmovzq %r8,%r15 cmovzq %rsi,%r9 - movq %r13,%rax + movq %r13,%rax // a0:a3>>1 shrq $1,%r12 shlq $63,%rax movq %r14,%r10 @@ -2565,14 +2587,14 @@ L$point_double_shortcutq: movq 16+32(%rsp),%r11 movq 24+32(%rsp),%r12 leaq 32(%rsp),%rdi - call __ecp_nistz256_mul_montq + call __ecp_nistz256_mul_montq // p256_mul_mont(M, M, Zsqr); leaq 128(%rsp),%rdi call __ecp_nistz256_mul_by_2q leaq 32(%rsp),%rbx leaq 32(%rsp),%rdi - call __ecp_nistz256_add_toq + call __ecp_nistz256_add_toq // p256_mul_by_3(M, M); movq 96(%rsp),%rax leaq 96(%rsp),%rbx @@ -2582,10 +2604,10 @@ L$point_double_shortcutq: movq 16+0(%rsp),%r11 movq 24+0(%rsp),%r12 leaq 0(%rsp),%rdi - call __ecp_nistz256_mul_montq + call __ecp_nistz256_mul_montq // p256_mul_mont(S, S, in_x); leaq 128(%rsp),%rdi - call __ecp_nistz256_mul_by_2q + call __ecp_nistz256_mul_by_2q // p256_mul_by_2(tmp0, S); movq 0+32(%rsp),%rax movq 8+32(%rsp),%r14 @@ -2593,27 +2615,27 @@ L$point_double_shortcutq: movq 16+32(%rsp),%r15 movq 24+32(%rsp),%r8 movq %xmm0,%rdi - call __ecp_nistz256_sqr_montq + call __ecp_nistz256_sqr_montq // p256_sqr_mont(res_x, M); leaq 128(%rsp),%rbx - movq %r14,%r8 + movq %r14,%r8 // harmonize sqr output and sub input movq %r15,%r9 movq %rsi,%r14 movq %rbp,%r15 - call __ecp_nistz256_sub_fromq + call __ecp_nistz256_sub_fromq // p256_sub(res_x, res_x, tmp0); movq 0+0(%rsp),%rax movq 0+8(%rsp),%rbp movq 0+16(%rsp),%rcx - movq 0+24(%rsp),%r10 + movq 0+24(%rsp),%r10 // "4-5-0-1" order leaq 0(%rsp),%rdi - call __ecp_nistz256_subq + call __ecp_nistz256_subq // p256_sub(S, S, res_x); movq 32(%rsp),%rax leaq 32(%rsp),%rbx - movq %r12,%r14 + movq %r12,%r14 // harmonize sub output and mul input xorl %ecx,%ecx - movq %r12,0+0(%rsp) + movq %r12,0+0(%rsp) // have to save:-( movq %r13,%r10 movq %r13,0+8(%rsp) cmovzq %r8,%r11 @@ -2623,11 +2645,11 @@ L$point_double_shortcutq: movq %r9,0+24(%rsp) movq %r14,%r9 leaq 0(%rsp),%rdi - call __ecp_nistz256_mul_montq + call __ecp_nistz256_mul_montq // p256_mul_mont(S, S, M); movq %xmm1,%rbx movq %xmm1,%rdi - call __ecp_nistz256_sub_fromq + call __ecp_nistz256_sub_fromq // p256_sub(res_y, S, res_y); leaq 160+56(%rsp),%rsi @@ -2653,6 +2675,7 @@ L$point_doubleq_epilogue: .private_extern _ecp_nistz256_point_add_nohw .p2align 5 +.alt_entry _ecp_nistz256_point_add_nohw _ecp_nistz256_point_add_nohw: _CET_ENDBR @@ -2672,14 +2695,14 @@ _CET_ENDBR L$point_addq_body: - movdqu 0(%rsi),%xmm0 + movdqu 0(%rsi),%xmm0 // copy *(P256_POINT *)%rsi movdqu 16(%rsi),%xmm1 movdqu 32(%rsi),%xmm2 movdqu 48(%rsi),%xmm3 movdqu 64(%rsi),%xmm4 movdqu 80(%rsi),%xmm5 - movq %rsi,%rbx - movq %rdx,%rsi + movq %rsi,%rbx // reassign + movq %rdx,%rsi // reassign movdqa %xmm0,384(%rsp) movdqa %xmm1,384+16(%rsp) movdqa %xmm2,416(%rsp) @@ -2688,54 +2711,54 @@ L$point_addq_body: movdqa %xmm5,448+16(%rsp) por %xmm4,%xmm5 - movdqu 0(%rsi),%xmm0 + movdqu 0(%rsi),%xmm0 // copy *(P256_POINT *)%rbx pshufd $0xb1,%xmm5,%xmm3 movdqu 16(%rsi),%xmm1 movdqu 32(%rsi),%xmm2 por %xmm3,%xmm5 movdqu 48(%rsi),%xmm3 - movq 64+0(%rsi),%rax + movq 64+0(%rsi),%rax // load original in2_z movq 64+8(%rsi),%r14 movq 64+16(%rsi),%r15 movq 64+24(%rsi),%r8 movdqa %xmm0,480(%rsp) pshufd $0x1e,%xmm5,%xmm4 movdqa %xmm1,480+16(%rsp) - movdqu 64(%rsi),%xmm0 + movdqu 64(%rsi),%xmm0 // in2_z again movdqu 80(%rsi),%xmm1 movdqa %xmm2,512(%rsp) movdqa %xmm3,512+16(%rsp) por %xmm4,%xmm5 pxor %xmm4,%xmm4 por %xmm0,%xmm1 - movq %rdi,%xmm0 + movq %rdi,%xmm0 // save %rdi - leaq 64-0(%rsi),%rsi - movq %rax,544+0(%rsp) + leaq 64-0(%rsi),%rsi // %rsi is still valid + movq %rax,544+0(%rsp) // make in2_z copy movq %r14,544+8(%rsp) movq %r15,544+16(%rsp) movq %r8,544+24(%rsp) - leaq 96(%rsp),%rdi - call __ecp_nistz256_sqr_montq + leaq 96(%rsp),%rdi // Z2^2 + call __ecp_nistz256_sqr_montq // p256_sqr_mont(Z2sqr, in2_z); pcmpeqd %xmm4,%xmm5 pshufd $0xb1,%xmm1,%xmm4 por %xmm1,%xmm4 - pshufd $0,%xmm5,%xmm5 + pshufd $0,%xmm5,%xmm5 // in1infty pshufd $0x1e,%xmm4,%xmm3 por %xmm3,%xmm4 pxor %xmm3,%xmm3 pcmpeqd %xmm3,%xmm4 - pshufd $0,%xmm4,%xmm4 - movq 64+0(%rbx),%rax + pshufd $0,%xmm4,%xmm4 // in2infty + movq 64+0(%rbx),%rax // load original in1_z movq 64+8(%rbx),%r14 movq 64+16(%rbx),%r15 movq 64+24(%rbx),%r8 movq %rbx,%xmm1 leaq 64-0(%rbx),%rsi - leaq 32(%rsp),%rdi - call __ecp_nistz256_sqr_montq + leaq 32(%rsp),%rdi // Z1^2 + call __ecp_nistz256_sqr_montq // p256_sqr_mont(Z1sqr, in1_z); movq 544(%rsp),%rax leaq 544(%rsp),%rbx @@ -2744,8 +2767,8 @@ L$point_addq_body: leaq 0+96(%rsp),%rsi movq 16+96(%rsp),%r11 movq 24+96(%rsp),%r12 - leaq 224(%rsp),%rdi - call __ecp_nistz256_mul_montq + leaq 224(%rsp),%rdi // S1 = Z2^3 + call __ecp_nistz256_mul_montq // p256_mul_mont(S1, Z2sqr, in2_z); movq 448(%rsp),%rax leaq 448(%rsp),%rbx @@ -2754,8 +2777,8 @@ L$point_addq_body: leaq 0+32(%rsp),%rsi movq 16+32(%rsp),%r11 movq 24+32(%rsp),%r12 - leaq 256(%rsp),%rdi - call __ecp_nistz256_mul_montq + leaq 256(%rsp),%rdi // S2 = Z1^3 + call __ecp_nistz256_mul_montq // p256_mul_mont(S2, Z1sqr, in1_z); movq 416(%rsp),%rax leaq 416(%rsp),%rbx @@ -2764,8 +2787,8 @@ L$point_addq_body: leaq 0+224(%rsp),%rsi movq 16+224(%rsp),%r11 movq 24+224(%rsp),%r12 - leaq 224(%rsp),%rdi - call __ecp_nistz256_mul_montq + leaq 224(%rsp),%rdi // S1 = Y1*Z2^3 + call __ecp_nistz256_mul_montq // p256_mul_mont(S1, S1, in1_y); movq 512(%rsp),%rax leaq 512(%rsp),%rbx @@ -2774,18 +2797,18 @@ L$point_addq_body: leaq 0+256(%rsp),%rsi movq 16+256(%rsp),%r11 movq 24+256(%rsp),%r12 - leaq 256(%rsp),%rdi - call __ecp_nistz256_mul_montq + leaq 256(%rsp),%rdi // S2 = Y2*Z1^3 + call __ecp_nistz256_mul_montq // p256_mul_mont(S2, S2, in2_y); leaq 224(%rsp),%rbx - leaq 64(%rsp),%rdi - call __ecp_nistz256_sub_fromq + leaq 64(%rsp),%rdi // R = S2 - S1 + call __ecp_nistz256_sub_fromq // p256_sub(R, S2, S1); - orq %r13,%r12 + orq %r13,%r12 // see if result is zero movdqa %xmm4,%xmm2 orq %r8,%r12 orq %r9,%r12 - por %xmm5,%xmm2 + por %xmm5,%xmm2 // in1infty || in2infty movq %r12,%xmm3 movq 384(%rsp),%rax @@ -2795,8 +2818,8 @@ L$point_addq_body: leaq 0+96(%rsp),%rsi movq 16+96(%rsp),%r11 movq 24+96(%rsp),%r12 - leaq 160(%rsp),%rdi - call __ecp_nistz256_mul_montq + leaq 160(%rsp),%rdi // U1 = X1*Z2^2 + call __ecp_nistz256_mul_montq // p256_mul_mont(U1, in1_x, Z2sqr); movq 480(%rsp),%rax leaq 480(%rsp),%rbx @@ -2805,34 +2828,34 @@ L$point_addq_body: leaq 0+32(%rsp),%rsi movq 16+32(%rsp),%r11 movq 24+32(%rsp),%r12 - leaq 192(%rsp),%rdi - call __ecp_nistz256_mul_montq + leaq 192(%rsp),%rdi // U2 = X2*Z1^2 + call __ecp_nistz256_mul_montq // p256_mul_mont(U2, in2_x, Z1sqr); leaq 160(%rsp),%rbx - leaq 0(%rsp),%rdi - call __ecp_nistz256_sub_fromq + leaq 0(%rsp),%rdi // H = U2 - U1 + call __ecp_nistz256_sub_fromq // p256_sub(H, U2, U1); - orq %r13,%r12 + orq %r13,%r12 // see if result is zero orq %r8,%r12 - orq %r9,%r12 + orq %r9,%r12 // movq %xmm2,%r8 movq %xmm3,%r9 orq %r8,%r12 -.byte 0x3e - jnz L$add_proceedq - - +.byte 0x3e // predict taken + jnz L$add_proceedq // +// We now know A = B or A = -B and neither is infinity. Compare the +// y-coordinates via S1 and S2. testq %r9,%r9 - jz L$add_doubleq - - - - - - - movq %xmm0,%rdi + jz L$add_doubleq // is_equal(S1, S2) + +// A = -B, so the result is infinity. +// +// TODO(davidben): Does .Ladd_proceed handle this case? It seems to, in +// which case we should eliminate this special-case and simplify the +// timing analysis. + movq %xmm0,%rdi // restore %rdi pxor %xmm0,%xmm0 movdqu %xmm0,0(%rdi) movdqu %xmm0,16(%rdi) @@ -2844,9 +2867,9 @@ L$point_addq_body: .p2align 5 L$add_doubleq: - movq %xmm1,%rsi - movq %xmm0,%rdi - addq $416,%rsp + movq %xmm1,%rsi // restore %rsi + movq %xmm0,%rdi // restore %rdi + addq $416,%rsp // difference in frame sizes jmp L$point_double_shortcutq @@ -2858,8 +2881,8 @@ L$add_proceedq: leaq 0+64(%rsp),%rsi movq 16+64(%rsp),%r15 movq 24+64(%rsp),%r8 - leaq 96(%rsp),%rdi - call __ecp_nistz256_sqr_montq + leaq 96(%rsp),%rdi // R^2 + call __ecp_nistz256_sqr_montq // p256_sqr_mont(Rsqr, R); movq 448(%rsp),%rax leaq 448(%rsp),%rbx @@ -2868,16 +2891,16 @@ L$add_proceedq: leaq 0+0(%rsp),%rsi movq 16+0(%rsp),%r11 movq 24+0(%rsp),%r12 - leaq 352(%rsp),%rdi - call __ecp_nistz256_mul_montq + leaq 352(%rsp),%rdi // Z3 = H*Z1*Z2 + call __ecp_nistz256_mul_montq // p256_mul_mont(res_z, H, in1_z); movq 0+0(%rsp),%rax movq 8+0(%rsp),%r14 leaq 0+0(%rsp),%rsi movq 16+0(%rsp),%r15 movq 24+0(%rsp),%r8 - leaq 32(%rsp),%rdi - call __ecp_nistz256_sqr_montq + leaq 32(%rsp),%rdi // H^2 + call __ecp_nistz256_sqr_montq // p256_sqr_mont(Hsqr, H); movq 544(%rsp),%rax leaq 544(%rsp),%rbx @@ -2886,8 +2909,8 @@ L$add_proceedq: leaq 0+352(%rsp),%rsi movq 16+352(%rsp),%r11 movq 24+352(%rsp),%r12 - leaq 352(%rsp),%rdi - call __ecp_nistz256_mul_montq + leaq 352(%rsp),%rdi // Z3 = H*Z1*Z2 + call __ecp_nistz256_mul_montq // p256_mul_mont(res_z, res_z, in2_z); movq 0(%rsp),%rax leaq 0(%rsp),%rbx @@ -2896,8 +2919,8 @@ L$add_proceedq: leaq 0+32(%rsp),%rsi movq 16+32(%rsp),%r11 movq 24+32(%rsp),%r12 - leaq 128(%rsp),%rdi - call __ecp_nistz256_mul_montq + leaq 128(%rsp),%rdi // H^3 + call __ecp_nistz256_mul_montq // p256_mul_mont(Hcub, Hsqr, H); movq 160(%rsp),%rax leaq 160(%rsp),%rbx @@ -2906,14 +2929,14 @@ L$add_proceedq: leaq 0+32(%rsp),%rsi movq 16+32(%rsp),%r11 movq 24+32(%rsp),%r12 - leaq 192(%rsp),%rdi - call __ecp_nistz256_mul_montq - - - + leaq 192(%rsp),%rdi // U1*H^2 + call __ecp_nistz256_mul_montq // p256_mul_mont(U2, U1, Hsqr); +// lea 192(%rsp), %rsi +// lea 32(%rsp), %rdi # 2*U1*H^2 +// call __ecp_nistz256_mul_by_2 # ecp_nistz256_mul_by_2(Hsqr, U2); xorq %r11,%r11 - addq %r12,%r12 + addq %r12,%r12 // a0:a3+a0:a3 leaq 96(%rsp),%rsi adcq %r13,%r13 movq %r12,%rax @@ -2939,11 +2962,11 @@ L$add_proceedq: cmovcq %r10,%r9 movq 24(%rsi),%r10 - call __ecp_nistz256_subq + call __ecp_nistz256_subq // p256_sub(res_x, Rsqr, Hsqr); leaq 128(%rsp),%rbx leaq 288(%rsp),%rdi - call __ecp_nistz256_sub_fromq + call __ecp_nistz256_sub_fromq // p256_sub(res_x, res_x, Hcub); movq 192+0(%rsp),%rax movq 192+8(%rsp),%rbp @@ -2951,10 +2974,10 @@ L$add_proceedq: movq 192+24(%rsp),%r10 leaq 320(%rsp),%rdi - call __ecp_nistz256_subq + call __ecp_nistz256_subq // p256_sub(res_y, U2, res_x); - movq %r12,0(%rdi) - movq %r13,8(%rdi) + movq %r12,0(%rdi) // save the result, as + movq %r13,8(%rdi) // __ecp_nistz256_sub doesn't movq %r8,16(%rdi) movq %r9,24(%rdi) movq 128(%rsp),%rax @@ -2965,7 +2988,7 @@ L$add_proceedq: movq 16+224(%rsp),%r11 movq 24+224(%rsp),%r12 leaq 256(%rsp),%rdi - call __ecp_nistz256_mul_montq + call __ecp_nistz256_mul_montq // p256_mul_mont(S2, S1, Hcub); movq 320(%rsp),%rax leaq 320(%rsp),%rbx @@ -2975,15 +2998,15 @@ L$add_proceedq: movq 16+64(%rsp),%r11 movq 24+64(%rsp),%r12 leaq 320(%rsp),%rdi - call __ecp_nistz256_mul_montq + call __ecp_nistz256_mul_montq // p256_mul_mont(res_y, R, res_y); leaq 256(%rsp),%rbx leaq 320(%rsp),%rdi - call __ecp_nistz256_sub_fromq + call __ecp_nistz256_sub_fromq // p256_sub(res_y, res_y, S2); - movq %xmm0,%rdi + movq %xmm0,%rdi // restore %rdi - movdqa %xmm5,%xmm0 + movdqa %xmm5,%xmm0 // copy_conditional(res_z, in2_z, in1infty); movdqa %xmm5,%xmm1 pandn 352(%rsp),%xmm0 movdqa %xmm5,%xmm2 @@ -2994,7 +3017,7 @@ L$add_proceedq: por %xmm0,%xmm2 por %xmm1,%xmm3 - movdqa %xmm4,%xmm0 + movdqa %xmm4,%xmm0 // copy_conditional(res_z, in1_z, in2infty); movdqa %xmm4,%xmm1 pandn %xmm2,%xmm0 movdqa %xmm4,%xmm2 @@ -3007,7 +3030,7 @@ L$add_proceedq: movdqu %xmm2,64(%rdi) movdqu %xmm3,80(%rdi) - movdqa %xmm5,%xmm0 + movdqa %xmm5,%xmm0 // copy_conditional(res_x, in2_x, in1infty); movdqa %xmm5,%xmm1 pandn 288(%rsp),%xmm0 movdqa %xmm5,%xmm2 @@ -3018,7 +3041,7 @@ L$add_proceedq: por %xmm0,%xmm2 por %xmm1,%xmm3 - movdqa %xmm4,%xmm0 + movdqa %xmm4,%xmm0 // copy_conditional(res_x, in1_x, in2infty); movdqa %xmm4,%xmm1 pandn %xmm2,%xmm0 movdqa %xmm4,%xmm2 @@ -3031,7 +3054,7 @@ L$add_proceedq: movdqu %xmm2,0(%rdi) movdqu %xmm3,16(%rdi) - movdqa %xmm5,%xmm0 + movdqa %xmm5,%xmm0 // copy_conditional(res_y, in2_y, in1infty); movdqa %xmm5,%xmm1 pandn 320(%rsp),%xmm0 movdqa %xmm5,%xmm2 @@ -3042,7 +3065,7 @@ L$add_proceedq: por %xmm0,%xmm2 por %xmm1,%xmm3 - movdqa %xmm4,%xmm0 + movdqa %xmm4,%xmm0 // copy_conditional(res_y, in1_y, in2infty); movdqa %xmm4,%xmm1 pandn %xmm2,%xmm0 movdqa %xmm4,%xmm2 @@ -3080,6 +3103,7 @@ L$point_addq_epilogue: .private_extern _ecp_nistz256_point_add_affine_nohw .p2align 5 +.alt_entry _ecp_nistz256_point_add_affine_nohw _ecp_nistz256_point_add_affine_nohw: _CET_ENDBR @@ -3099,14 +3123,14 @@ _CET_ENDBR L$add_affineq_body: - movdqu 0(%rsi),%xmm0 - movq %rdx,%rbx + movdqu 0(%rsi),%xmm0 // copy *(P256_POINT *)%rsi + movq %rdx,%rbx // reassign movdqu 16(%rsi),%xmm1 movdqu 32(%rsi),%xmm2 movdqu 48(%rsi),%xmm3 movdqu 64(%rsi),%xmm4 movdqu 80(%rsi),%xmm5 - movq 64+0(%rsi),%rax + movq 64+0(%rsi),%rax // load original in1_z movq 64+8(%rsi),%r14 movq 64+16(%rsi),%r15 movq 64+24(%rsi),%r8 @@ -3118,7 +3142,7 @@ L$add_affineq_body: movdqa %xmm5,384+16(%rsp) por %xmm4,%xmm5 - movdqu 0(%rbx),%xmm0 + movdqu 0(%rbx),%xmm0 // copy *(P256_POINT_AFFINE *)%rbx pshufd $0xb1,%xmm5,%xmm3 movdqu 16(%rbx),%xmm1 movdqu 32(%rbx),%xmm2 @@ -3128,7 +3152,7 @@ L$add_affineq_body: pshufd $0x1e,%xmm5,%xmm4 movdqa %xmm1,416+16(%rsp) por %xmm0,%xmm1 - movq %rdi,%xmm0 + movq %rdi,%xmm0 // save %rdi movdqa %xmm2,448(%rsp) movdqa %xmm3,448+16(%rsp) por %xmm2,%xmm3 @@ -3136,33 +3160,33 @@ L$add_affineq_body: pxor %xmm4,%xmm4 por %xmm1,%xmm3 - leaq 64-0(%rsi),%rsi - leaq 32(%rsp),%rdi - call __ecp_nistz256_sqr_montq + leaq 64-0(%rsi),%rsi // %rsi is still valid + leaq 32(%rsp),%rdi // Z1^2 + call __ecp_nistz256_sqr_montq // p256_sqr_mont(Z1sqr, in1_z); pcmpeqd %xmm4,%xmm5 pshufd $0xb1,%xmm3,%xmm4 - movq 0(%rbx),%rax - - movq %r12,%r9 + movq 0(%rbx),%rax // %rbx is still valid +// lea 0x00(%rbx), %rbx + movq %r12,%r9 // harmonize sqr output and mul input por %xmm3,%xmm4 - pshufd $0,%xmm5,%xmm5 + pshufd $0,%xmm5,%xmm5 // in1infty pshufd $0x1e,%xmm4,%xmm3 movq %r13,%r10 por %xmm3,%xmm4 pxor %xmm3,%xmm3 movq %r14,%r11 pcmpeqd %xmm3,%xmm4 - pshufd $0,%xmm4,%xmm4 + pshufd $0,%xmm4,%xmm4 // in2infty leaq 32-0(%rsp),%rsi movq %r15,%r12 - leaq 0(%rsp),%rdi - call __ecp_nistz256_mul_montq + leaq 0(%rsp),%rdi // U2 = X2*Z1^2 + call __ecp_nistz256_mul_montq // p256_mul_mont(U2, Z1sqr, in2_x); leaq 320(%rsp),%rbx - leaq 64(%rsp),%rdi - call __ecp_nistz256_sub_fromq + leaq 64(%rsp),%rdi // H = U2 - U1 + call __ecp_nistz256_sub_fromq // p256_sub(H, U2, in1_x); movq 384(%rsp),%rax leaq 384(%rsp),%rbx @@ -3171,8 +3195,8 @@ L$add_affineq_body: leaq 0+32(%rsp),%rsi movq 16+32(%rsp),%r11 movq 24+32(%rsp),%r12 - leaq 32(%rsp),%rdi - call __ecp_nistz256_mul_montq + leaq 32(%rsp),%rdi // S2 = Z1^3 + call __ecp_nistz256_mul_montq // p256_mul_mont(S2, Z1sqr, in1_z); movq 384(%rsp),%rax leaq 384(%rsp),%rbx @@ -3181,8 +3205,8 @@ L$add_affineq_body: leaq 0+64(%rsp),%rsi movq 16+64(%rsp),%r11 movq 24+64(%rsp),%r12 - leaq 288(%rsp),%rdi - call __ecp_nistz256_mul_montq + leaq 288(%rsp),%rdi // Z3 = H*Z1*Z2 + call __ecp_nistz256_mul_montq // p256_mul_mont(res_z, H, in1_z); movq 448(%rsp),%rax leaq 448(%rsp),%rbx @@ -3191,28 +3215,28 @@ L$add_affineq_body: leaq 0+32(%rsp),%rsi movq 16+32(%rsp),%r11 movq 24+32(%rsp),%r12 - leaq 32(%rsp),%rdi - call __ecp_nistz256_mul_montq + leaq 32(%rsp),%rdi // S2 = Y2*Z1^3 + call __ecp_nistz256_mul_montq // p256_mul_mont(S2, S2, in2_y); leaq 352(%rsp),%rbx - leaq 96(%rsp),%rdi - call __ecp_nistz256_sub_fromq + leaq 96(%rsp),%rdi // R = S2 - S1 + call __ecp_nistz256_sub_fromq // p256_sub(R, S2, in1_y); movq 0+64(%rsp),%rax movq 8+64(%rsp),%r14 leaq 0+64(%rsp),%rsi movq 16+64(%rsp),%r15 movq 24+64(%rsp),%r8 - leaq 128(%rsp),%rdi - call __ecp_nistz256_sqr_montq + leaq 128(%rsp),%rdi // H^2 + call __ecp_nistz256_sqr_montq // p256_sqr_mont(Hsqr, H); movq 0+96(%rsp),%rax movq 8+96(%rsp),%r14 leaq 0+96(%rsp),%rsi movq 16+96(%rsp),%r15 movq 24+96(%rsp),%r8 - leaq 192(%rsp),%rdi - call __ecp_nistz256_sqr_montq + leaq 192(%rsp),%rdi // R^2 + call __ecp_nistz256_sqr_montq // p256_sqr_mont(Rsqr, R); movq 128(%rsp),%rax leaq 128(%rsp),%rbx @@ -3221,8 +3245,8 @@ L$add_affineq_body: leaq 0+64(%rsp),%rsi movq 16+64(%rsp),%r11 movq 24+64(%rsp),%r12 - leaq 160(%rsp),%rdi - call __ecp_nistz256_mul_montq + leaq 160(%rsp),%rdi // H^3 + call __ecp_nistz256_mul_montq // p256_mul_mont(Hcub, Hsqr, H); movq 320(%rsp),%rax leaq 320(%rsp),%rbx @@ -3231,14 +3255,14 @@ L$add_affineq_body: leaq 0+128(%rsp),%rsi movq 16+128(%rsp),%r11 movq 24+128(%rsp),%r12 - leaq 0(%rsp),%rdi - call __ecp_nistz256_mul_montq - - - + leaq 0(%rsp),%rdi // U1*H^2 + call __ecp_nistz256_mul_montq // p256_mul_mont(U2, in1_x, Hsqr); +// lea 0(%rsp), %rsi +// lea 128(%rsp), %rdi # 2*U1*H^2 +// call __ecp_nistz256_mul_by_2 # ecp_nistz256_mul_by_2(Hsqr, U2); xorq %r11,%r11 - addq %r12,%r12 + addq %r12,%r12 // a0:a3+a0:a3 leaq 192(%rsp),%rsi adcq %r13,%r13 movq %r12,%rax @@ -3264,11 +3288,11 @@ L$add_affineq_body: cmovcq %r10,%r9 movq 24(%rsi),%r10 - call __ecp_nistz256_subq + call __ecp_nistz256_subq // p256_sub(res_x, Rsqr, Hsqr); leaq 160(%rsp),%rbx leaq 224(%rsp),%rdi - call __ecp_nistz256_sub_fromq + call __ecp_nistz256_sub_fromq // p256_sub(res_x, res_x, Hcub); movq 0+0(%rsp),%rax movq 0+8(%rsp),%rbp @@ -3276,10 +3300,10 @@ L$add_affineq_body: movq 0+24(%rsp),%r10 leaq 64(%rsp),%rdi - call __ecp_nistz256_subq + call __ecp_nistz256_subq // p256_sub(H, U2, res_x); - movq %r12,0(%rdi) - movq %r13,8(%rdi) + movq %r12,0(%rdi) // save the result, as + movq %r13,8(%rdi) // __ecp_nistz256_sub doesn't movq %r8,16(%rdi) movq %r9,24(%rdi) movq 352(%rsp),%rax @@ -3290,7 +3314,7 @@ L$add_affineq_body: movq 16+160(%rsp),%r11 movq 24+160(%rsp),%r12 leaq 32(%rsp),%rdi - call __ecp_nistz256_mul_montq + call __ecp_nistz256_mul_montq // p256_mul_mont(S2, Hcub, in1_y); movq 96(%rsp),%rax leaq 96(%rsp),%rbx @@ -3300,15 +3324,15 @@ L$add_affineq_body: movq 16+64(%rsp),%r11 movq 24+64(%rsp),%r12 leaq 64(%rsp),%rdi - call __ecp_nistz256_mul_montq + call __ecp_nistz256_mul_montq // p256_mul_mont(H, H, R); leaq 32(%rsp),%rbx leaq 256(%rsp),%rdi - call __ecp_nistz256_sub_fromq + call __ecp_nistz256_sub_fromq // p256_sub(res_y, H, S2); - movq %xmm0,%rdi + movq %xmm0,%rdi // restore %rdi - movdqa %xmm5,%xmm0 + movdqa %xmm5,%xmm0 // copy_conditional(res_z, ONE, in1infty); movdqa %xmm5,%xmm1 pandn 288(%rsp),%xmm0 movdqa %xmm5,%xmm2 @@ -3319,7 +3343,7 @@ L$add_affineq_body: por %xmm0,%xmm2 por %xmm1,%xmm3 - movdqa %xmm4,%xmm0 + movdqa %xmm4,%xmm0 // copy_conditional(res_z, in1_z, in2infty); movdqa %xmm4,%xmm1 pandn %xmm2,%xmm0 movdqa %xmm4,%xmm2 @@ -3332,7 +3356,7 @@ L$add_affineq_body: movdqu %xmm2,64(%rdi) movdqu %xmm3,80(%rdi) - movdqa %xmm5,%xmm0 + movdqa %xmm5,%xmm0 // copy_conditional(res_x, in2_x, in1infty); movdqa %xmm5,%xmm1 pandn 224(%rsp),%xmm0 movdqa %xmm5,%xmm2 @@ -3343,7 +3367,7 @@ L$add_affineq_body: por %xmm0,%xmm2 por %xmm1,%xmm3 - movdqa %xmm4,%xmm0 + movdqa %xmm4,%xmm0 // copy_conditional(res_x, in1_x, in2infty); movdqa %xmm4,%xmm1 pandn %xmm2,%xmm0 movdqa %xmm4,%xmm2 @@ -3356,7 +3380,7 @@ L$add_affineq_body: movdqu %xmm2,0(%rdi) movdqu %xmm3,16(%rdi) - movdqa %xmm5,%xmm0 + movdqa %xmm5,%xmm0 // copy_conditional(res_y, in2_y, in1infty); movdqa %xmm5,%xmm1 pandn 256(%rsp),%xmm0 movdqa %xmm5,%xmm2 @@ -3367,7 +3391,7 @@ L$add_affineq_body: por %xmm0,%xmm2 por %xmm1,%xmm3 - movdqa %xmm4,%xmm0 + movdqa %xmm4,%xmm0 // copy_conditional(res_y, in1_y, in2infty); movdqa %xmm4,%xmm1 pandn %xmm2,%xmm0 movdqa %xmm4,%xmm2 @@ -3402,6 +3426,7 @@ L$add_affineq_epilogue: .p2align 5 +.alt_entry __ecp_nistz256_add_tox __ecp_nistz256_add_tox: xorq %r11,%r11 @@ -3437,6 +3462,7 @@ __ecp_nistz256_add_tox: .p2align 5 +.alt_entry __ecp_nistz256_sub_fromx __ecp_nistz256_sub_fromx: xorq %r11,%r11 @@ -3472,6 +3498,7 @@ __ecp_nistz256_sub_fromx: .p2align 5 +.alt_entry __ecp_nistz256_subx __ecp_nistz256_subx: xorq %r11,%r11 @@ -3503,10 +3530,11 @@ __ecp_nistz256_subx: .p2align 5 +.alt_entry __ecp_nistz256_mul_by_2x __ecp_nistz256_mul_by_2x: xorq %r11,%r11 - adcq %r12,%r12 + adcq %r12,%r12 // a0:a3+a0:a3 adcq %r13,%r13 movq %r12,%rax adcq %r8,%r8 @@ -3539,6 +3567,7 @@ __ecp_nistz256_mul_by_2x: .private_extern _ecp_nistz256_point_double_adx .p2align 5 +.alt_entry _ecp_nistz256_point_double_adx _ecp_nistz256_point_double_adx: _CET_ENDBR @@ -3559,10 +3588,10 @@ _CET_ENDBR L$point_doublex_body: L$point_double_shortcutx: - movdqu 0(%rsi),%xmm0 - movq %rsi,%rbx + movdqu 0(%rsi),%xmm0 // copy *(P256_POINT *)%rsi.x + movq %rsi,%rbx // backup copy movdqu 16(%rsi),%xmm1 - movq 32+0(%rsi),%r12 + movq 32+0(%rsi),%r12 // load in_y in "5-4-0-1" order movq 32+8(%rsi),%r13 movq 32+16(%rsi),%r8 movq 32+24(%rsi),%r9 @@ -3577,7 +3606,7 @@ L$point_double_shortcutx: movq %r11,%xmm2 leaq 0(%rsp),%rdi - call __ecp_nistz256_mul_by_2x + call __ecp_nistz256_mul_by_2x // p256_mul_by_2(S, in_y); movq 64+0(%rsi),%rdx movq 64+8(%rsi),%r14 @@ -3585,7 +3614,7 @@ L$point_double_shortcutx: movq 64+24(%rsi),%r8 leaq 64-128(%rsi),%rsi leaq 64(%rsp),%rdi - call __ecp_nistz256_sqr_montx + call __ecp_nistz256_sqr_montx // p256_sqr_mont(Zsqr, in_z); movq 0+0(%rsp),%rdx movq 8+0(%rsp),%r14 @@ -3593,9 +3622,9 @@ L$point_double_shortcutx: movq 16+0(%rsp),%r15 movq 24+0(%rsp),%r8 leaq 0(%rsp),%rdi - call __ecp_nistz256_sqr_montx + call __ecp_nistz256_sqr_montx // p256_sqr_mont(S, S); - movq 32(%rbx),%rdx + movq 32(%rbx),%rdx // %rbx is still valid movq 64+0(%rbx),%r9 movq 64+8(%rbx),%r10 movq 64+16(%rbx),%r11 @@ -3603,24 +3632,24 @@ L$point_double_shortcutx: leaq 64-128(%rbx),%rsi leaq 32(%rbx),%rbx movq %xmm2,%rdi - call __ecp_nistz256_mul_montx - call __ecp_nistz256_mul_by_2x + call __ecp_nistz256_mul_montx // p256_mul_mont(res_z, in_z, in_y); + call __ecp_nistz256_mul_by_2x // p256_mul_by_2(res_z, res_z); - movq 96+0(%rsp),%r12 + movq 96+0(%rsp),%r12 // "5-4-0-1" order movq 96+8(%rsp),%r13 leaq 64(%rsp),%rbx movq 96+16(%rsp),%r8 movq 96+24(%rsp),%r9 leaq 32(%rsp),%rdi - call __ecp_nistz256_add_tox + call __ecp_nistz256_add_tox // p256_add(M, in_x, Zsqr); - movq 96+0(%rsp),%r12 + movq 96+0(%rsp),%r12 // "5-4-0-1" order movq 96+8(%rsp),%r13 leaq 64(%rsp),%rbx movq 96+16(%rsp),%r8 movq 96+24(%rsp),%r9 leaq 64(%rsp),%rdi - call __ecp_nistz256_sub_fromx + call __ecp_nistz256_sub_fromx // p256_sub(Zsqr, in_x, Zsqr); movq 0+0(%rsp),%rdx movq 8+0(%rsp),%r14 @@ -3628,7 +3657,7 @@ L$point_double_shortcutx: movq 16+0(%rsp),%r15 movq 24+0(%rsp),%r8 movq %xmm1,%rdi - call __ecp_nistz256_sqr_montx + call __ecp_nistz256_sqr_montx // p256_sqr_mont(res_y, S); xorq %r9,%r9 movq %r12,%rax addq $-1,%r12 @@ -3639,7 +3668,7 @@ L$point_double_shortcutx: movq %r15,%r8 adcq %rbp,%r15 adcq $0,%r9 - xorq %rsi,%rsi + xorq %rsi,%rsi // borrow %rsi testq $1,%rax cmovzq %rax,%r12 @@ -3648,7 +3677,7 @@ L$point_double_shortcutx: cmovzq %r8,%r15 cmovzq %rsi,%r9 - movq %r13,%rax + movq %r13,%rax // a0:a3>>1 shrq $1,%r12 shlq $63,%rax movq %r14,%r10 @@ -3675,14 +3704,14 @@ L$point_double_shortcutx: movq 16+32(%rsp),%r11 movq 24+32(%rsp),%r12 leaq 32(%rsp),%rdi - call __ecp_nistz256_mul_montx + call __ecp_nistz256_mul_montx // p256_mul_mont(M, M, Zsqr); leaq 128(%rsp),%rdi call __ecp_nistz256_mul_by_2x leaq 32(%rsp),%rbx leaq 32(%rsp),%rdi - call __ecp_nistz256_add_tox + call __ecp_nistz256_add_tox // p256_mul_by_3(M, M); movq 96(%rsp),%rdx leaq 96(%rsp),%rbx @@ -3692,10 +3721,10 @@ L$point_double_shortcutx: movq 16+0(%rsp),%r11 movq 24+0(%rsp),%r12 leaq 0(%rsp),%rdi - call __ecp_nistz256_mul_montx + call __ecp_nistz256_mul_montx // p256_mul_mont(S, S, in_x); leaq 128(%rsp),%rdi - call __ecp_nistz256_mul_by_2x + call __ecp_nistz256_mul_by_2x // p256_mul_by_2(tmp0, S); movq 0+32(%rsp),%rdx movq 8+32(%rsp),%r14 @@ -3703,27 +3732,27 @@ L$point_double_shortcutx: movq 16+32(%rsp),%r15 movq 24+32(%rsp),%r8 movq %xmm0,%rdi - call __ecp_nistz256_sqr_montx + call __ecp_nistz256_sqr_montx // p256_sqr_mont(res_x, M); leaq 128(%rsp),%rbx - movq %r14,%r8 + movq %r14,%r8 // harmonize sqr output and sub input movq %r15,%r9 movq %rsi,%r14 movq %rbp,%r15 - call __ecp_nistz256_sub_fromx + call __ecp_nistz256_sub_fromx // p256_sub(res_x, res_x, tmp0); movq 0+0(%rsp),%rax movq 0+8(%rsp),%rbp movq 0+16(%rsp),%rcx - movq 0+24(%rsp),%r10 + movq 0+24(%rsp),%r10 // "4-5-0-1" order leaq 0(%rsp),%rdi - call __ecp_nistz256_subx + call __ecp_nistz256_subx // p256_sub(S, S, res_x); movq 32(%rsp),%rdx leaq 32(%rsp),%rbx - movq %r12,%r14 + movq %r12,%r14 // harmonize sub output and mul input xorl %ecx,%ecx - movq %r12,0+0(%rsp) + movq %r12,0+0(%rsp) // have to save:-( movq %r13,%r10 movq %r13,0+8(%rsp) cmovzq %r8,%r11 @@ -3733,11 +3762,11 @@ L$point_double_shortcutx: movq %r9,0+24(%rsp) movq %r14,%r9 leaq 0(%rsp),%rdi - call __ecp_nistz256_mul_montx + call __ecp_nistz256_mul_montx // p256_mul_mont(S, S, M); movq %xmm1,%rbx movq %xmm1,%rdi - call __ecp_nistz256_sub_fromx + call __ecp_nistz256_sub_fromx // p256_sub(res_y, S, res_y); leaq 160+56(%rsp),%rsi @@ -3763,6 +3792,7 @@ L$point_doublex_epilogue: .private_extern _ecp_nistz256_point_add_adx .p2align 5 +.alt_entry _ecp_nistz256_point_add_adx _ecp_nistz256_point_add_adx: _CET_ENDBR @@ -3782,14 +3812,14 @@ _CET_ENDBR L$point_addx_body: - movdqu 0(%rsi),%xmm0 + movdqu 0(%rsi),%xmm0 // copy *(P256_POINT *)%rsi movdqu 16(%rsi),%xmm1 movdqu 32(%rsi),%xmm2 movdqu 48(%rsi),%xmm3 movdqu 64(%rsi),%xmm4 movdqu 80(%rsi),%xmm5 - movq %rsi,%rbx - movq %rdx,%rsi + movq %rsi,%rbx // reassign + movq %rdx,%rsi // reassign movdqa %xmm0,384(%rsp) movdqa %xmm1,384+16(%rsp) movdqa %xmm2,416(%rsp) @@ -3798,54 +3828,54 @@ L$point_addx_body: movdqa %xmm5,448+16(%rsp) por %xmm4,%xmm5 - movdqu 0(%rsi),%xmm0 + movdqu 0(%rsi),%xmm0 // copy *(P256_POINT *)%rbx pshufd $0xb1,%xmm5,%xmm3 movdqu 16(%rsi),%xmm1 movdqu 32(%rsi),%xmm2 por %xmm3,%xmm5 movdqu 48(%rsi),%xmm3 - movq 64+0(%rsi),%rdx + movq 64+0(%rsi),%rdx // load original in2_z movq 64+8(%rsi),%r14 movq 64+16(%rsi),%r15 movq 64+24(%rsi),%r8 movdqa %xmm0,480(%rsp) pshufd $0x1e,%xmm5,%xmm4 movdqa %xmm1,480+16(%rsp) - movdqu 64(%rsi),%xmm0 + movdqu 64(%rsi),%xmm0 // in2_z again movdqu 80(%rsi),%xmm1 movdqa %xmm2,512(%rsp) movdqa %xmm3,512+16(%rsp) por %xmm4,%xmm5 pxor %xmm4,%xmm4 por %xmm0,%xmm1 - movq %rdi,%xmm0 + movq %rdi,%xmm0 // save %rdi - leaq 64-128(%rsi),%rsi - movq %rdx,544+0(%rsp) + leaq 64-128(%rsi),%rsi // %rsi is still valid + movq %rdx,544+0(%rsp) // make in2_z copy movq %r14,544+8(%rsp) movq %r15,544+16(%rsp) movq %r8,544+24(%rsp) - leaq 96(%rsp),%rdi - call __ecp_nistz256_sqr_montx + leaq 96(%rsp),%rdi // Z2^2 + call __ecp_nistz256_sqr_montx // p256_sqr_mont(Z2sqr, in2_z); pcmpeqd %xmm4,%xmm5 pshufd $0xb1,%xmm1,%xmm4 por %xmm1,%xmm4 - pshufd $0,%xmm5,%xmm5 + pshufd $0,%xmm5,%xmm5 // in1infty pshufd $0x1e,%xmm4,%xmm3 por %xmm3,%xmm4 pxor %xmm3,%xmm3 pcmpeqd %xmm3,%xmm4 - pshufd $0,%xmm4,%xmm4 - movq 64+0(%rbx),%rdx + pshufd $0,%xmm4,%xmm4 // in2infty + movq 64+0(%rbx),%rdx // load original in1_z movq 64+8(%rbx),%r14 movq 64+16(%rbx),%r15 movq 64+24(%rbx),%r8 movq %rbx,%xmm1 leaq 64-128(%rbx),%rsi - leaq 32(%rsp),%rdi - call __ecp_nistz256_sqr_montx + leaq 32(%rsp),%rdi // Z1^2 + call __ecp_nistz256_sqr_montx // p256_sqr_mont(Z1sqr, in1_z); movq 544(%rsp),%rdx leaq 544(%rsp),%rbx @@ -3854,8 +3884,8 @@ L$point_addx_body: leaq -128+96(%rsp),%rsi movq 16+96(%rsp),%r11 movq 24+96(%rsp),%r12 - leaq 224(%rsp),%rdi - call __ecp_nistz256_mul_montx + leaq 224(%rsp),%rdi // S1 = Z2^3 + call __ecp_nistz256_mul_montx // p256_mul_mont(S1, Z2sqr, in2_z); movq 448(%rsp),%rdx leaq 448(%rsp),%rbx @@ -3864,8 +3894,8 @@ L$point_addx_body: leaq -128+32(%rsp),%rsi movq 16+32(%rsp),%r11 movq 24+32(%rsp),%r12 - leaq 256(%rsp),%rdi - call __ecp_nistz256_mul_montx + leaq 256(%rsp),%rdi // S2 = Z1^3 + call __ecp_nistz256_mul_montx // p256_mul_mont(S2, Z1sqr, in1_z); movq 416(%rsp),%rdx leaq 416(%rsp),%rbx @@ -3874,8 +3904,8 @@ L$point_addx_body: leaq -128+224(%rsp),%rsi movq 16+224(%rsp),%r11 movq 24+224(%rsp),%r12 - leaq 224(%rsp),%rdi - call __ecp_nistz256_mul_montx + leaq 224(%rsp),%rdi // S1 = Y1*Z2^3 + call __ecp_nistz256_mul_montx // p256_mul_mont(S1, S1, in1_y); movq 512(%rsp),%rdx leaq 512(%rsp),%rbx @@ -3884,18 +3914,18 @@ L$point_addx_body: leaq -128+256(%rsp),%rsi movq 16+256(%rsp),%r11 movq 24+256(%rsp),%r12 - leaq 256(%rsp),%rdi - call __ecp_nistz256_mul_montx + leaq 256(%rsp),%rdi // S2 = Y2*Z1^3 + call __ecp_nistz256_mul_montx // p256_mul_mont(S2, S2, in2_y); leaq 224(%rsp),%rbx - leaq 64(%rsp),%rdi - call __ecp_nistz256_sub_fromx + leaq 64(%rsp),%rdi // R = S2 - S1 + call __ecp_nistz256_sub_fromx // p256_sub(R, S2, S1); - orq %r13,%r12 + orq %r13,%r12 // see if result is zero movdqa %xmm4,%xmm2 orq %r8,%r12 orq %r9,%r12 - por %xmm5,%xmm2 + por %xmm5,%xmm2 // in1infty || in2infty movq %r12,%xmm3 movq 384(%rsp),%rdx @@ -3905,8 +3935,8 @@ L$point_addx_body: leaq -128+96(%rsp),%rsi movq 16+96(%rsp),%r11 movq 24+96(%rsp),%r12 - leaq 160(%rsp),%rdi - call __ecp_nistz256_mul_montx + leaq 160(%rsp),%rdi // U1 = X1*Z2^2 + call __ecp_nistz256_mul_montx // p256_mul_mont(U1, in1_x, Z2sqr); movq 480(%rsp),%rdx leaq 480(%rsp),%rbx @@ -3915,34 +3945,34 @@ L$point_addx_body: leaq -128+32(%rsp),%rsi movq 16+32(%rsp),%r11 movq 24+32(%rsp),%r12 - leaq 192(%rsp),%rdi - call __ecp_nistz256_mul_montx + leaq 192(%rsp),%rdi // U2 = X2*Z1^2 + call __ecp_nistz256_mul_montx // p256_mul_mont(U2, in2_x, Z1sqr); leaq 160(%rsp),%rbx - leaq 0(%rsp),%rdi - call __ecp_nistz256_sub_fromx + leaq 0(%rsp),%rdi // H = U2 - U1 + call __ecp_nistz256_sub_fromx // p256_sub(H, U2, U1); - orq %r13,%r12 + orq %r13,%r12 // see if result is zero orq %r8,%r12 - orq %r9,%r12 + orq %r9,%r12 // movq %xmm2,%r8 movq %xmm3,%r9 orq %r8,%r12 -.byte 0x3e - jnz L$add_proceedx - - +.byte 0x3e // predict taken + jnz L$add_proceedx // +// We now know A = B or A = -B and neither is infinity. Compare the +// y-coordinates via S1 and S2. testq %r9,%r9 - jz L$add_doublex - - - - - - - movq %xmm0,%rdi + jz L$add_doublex // is_equal(S1, S2) + +// A = -B, so the result is infinity. +// +// TODO(davidben): Does .Ladd_proceed handle this case? It seems to, in +// which case we should eliminate this special-case and simplify the +// timing analysis. + movq %xmm0,%rdi // restore %rdi pxor %xmm0,%xmm0 movdqu %xmm0,0(%rdi) movdqu %xmm0,16(%rdi) @@ -3954,9 +3984,9 @@ L$point_addx_body: .p2align 5 L$add_doublex: - movq %xmm1,%rsi - movq %xmm0,%rdi - addq $416,%rsp + movq %xmm1,%rsi // restore %rsi + movq %xmm0,%rdi // restore %rdi + addq $416,%rsp // difference in frame sizes jmp L$point_double_shortcutx @@ -3968,8 +3998,8 @@ L$add_proceedx: leaq -128+64(%rsp),%rsi movq 16+64(%rsp),%r15 movq 24+64(%rsp),%r8 - leaq 96(%rsp),%rdi - call __ecp_nistz256_sqr_montx + leaq 96(%rsp),%rdi // R^2 + call __ecp_nistz256_sqr_montx // p256_sqr_mont(Rsqr, R); movq 448(%rsp),%rdx leaq 448(%rsp),%rbx @@ -3978,16 +4008,16 @@ L$add_proceedx: leaq -128+0(%rsp),%rsi movq 16+0(%rsp),%r11 movq 24+0(%rsp),%r12 - leaq 352(%rsp),%rdi - call __ecp_nistz256_mul_montx + leaq 352(%rsp),%rdi // Z3 = H*Z1*Z2 + call __ecp_nistz256_mul_montx // p256_mul_mont(res_z, H, in1_z); movq 0+0(%rsp),%rdx movq 8+0(%rsp),%r14 leaq -128+0(%rsp),%rsi movq 16+0(%rsp),%r15 movq 24+0(%rsp),%r8 - leaq 32(%rsp),%rdi - call __ecp_nistz256_sqr_montx + leaq 32(%rsp),%rdi // H^2 + call __ecp_nistz256_sqr_montx // p256_sqr_mont(Hsqr, H); movq 544(%rsp),%rdx leaq 544(%rsp),%rbx @@ -3996,8 +4026,8 @@ L$add_proceedx: leaq -128+352(%rsp),%rsi movq 16+352(%rsp),%r11 movq 24+352(%rsp),%r12 - leaq 352(%rsp),%rdi - call __ecp_nistz256_mul_montx + leaq 352(%rsp),%rdi // Z3 = H*Z1*Z2 + call __ecp_nistz256_mul_montx // p256_mul_mont(res_z, res_z, in2_z); movq 0(%rsp),%rdx leaq 0(%rsp),%rbx @@ -4006,8 +4036,8 @@ L$add_proceedx: leaq -128+32(%rsp),%rsi movq 16+32(%rsp),%r11 movq 24+32(%rsp),%r12 - leaq 128(%rsp),%rdi - call __ecp_nistz256_mul_montx + leaq 128(%rsp),%rdi // H^3 + call __ecp_nistz256_mul_montx // p256_mul_mont(Hcub, Hsqr, H); movq 160(%rsp),%rdx leaq 160(%rsp),%rbx @@ -4016,14 +4046,14 @@ L$add_proceedx: leaq -128+32(%rsp),%rsi movq 16+32(%rsp),%r11 movq 24+32(%rsp),%r12 - leaq 192(%rsp),%rdi - call __ecp_nistz256_mul_montx - - - + leaq 192(%rsp),%rdi // U1*H^2 + call __ecp_nistz256_mul_montx // p256_mul_mont(U2, U1, Hsqr); +// lea 192(%rsp), %rsi +// lea 32(%rsp), %rdi # 2*U1*H^2 +// call __ecp_nistz256_mul_by_2 # ecp_nistz256_mul_by_2(Hsqr, U2); xorq %r11,%r11 - addq %r12,%r12 + addq %r12,%r12 // a0:a3+a0:a3 leaq 96(%rsp),%rsi adcq %r13,%r13 movq %r12,%rax @@ -4049,11 +4079,11 @@ L$add_proceedx: cmovcq %r10,%r9 movq 24(%rsi),%r10 - call __ecp_nistz256_subx + call __ecp_nistz256_subx // p256_sub(res_x, Rsqr, Hsqr); leaq 128(%rsp),%rbx leaq 288(%rsp),%rdi - call __ecp_nistz256_sub_fromx + call __ecp_nistz256_sub_fromx // p256_sub(res_x, res_x, Hcub); movq 192+0(%rsp),%rax movq 192+8(%rsp),%rbp @@ -4061,10 +4091,10 @@ L$add_proceedx: movq 192+24(%rsp),%r10 leaq 320(%rsp),%rdi - call __ecp_nistz256_subx + call __ecp_nistz256_subx // p256_sub(res_y, U2, res_x); - movq %r12,0(%rdi) - movq %r13,8(%rdi) + movq %r12,0(%rdi) // save the result, as + movq %r13,8(%rdi) // __ecp_nistz256_sub doesn't movq %r8,16(%rdi) movq %r9,24(%rdi) movq 128(%rsp),%rdx @@ -4075,7 +4105,7 @@ L$add_proceedx: movq 16+224(%rsp),%r11 movq 24+224(%rsp),%r12 leaq 256(%rsp),%rdi - call __ecp_nistz256_mul_montx + call __ecp_nistz256_mul_montx // p256_mul_mont(S2, S1, Hcub); movq 320(%rsp),%rdx leaq 320(%rsp),%rbx @@ -4085,15 +4115,15 @@ L$add_proceedx: movq 16+64(%rsp),%r11 movq 24+64(%rsp),%r12 leaq 320(%rsp),%rdi - call __ecp_nistz256_mul_montx + call __ecp_nistz256_mul_montx // p256_mul_mont(res_y, R, res_y); leaq 256(%rsp),%rbx leaq 320(%rsp),%rdi - call __ecp_nistz256_sub_fromx + call __ecp_nistz256_sub_fromx // p256_sub(res_y, res_y, S2); - movq %xmm0,%rdi + movq %xmm0,%rdi // restore %rdi - movdqa %xmm5,%xmm0 + movdqa %xmm5,%xmm0 // copy_conditional(res_z, in2_z, in1infty); movdqa %xmm5,%xmm1 pandn 352(%rsp),%xmm0 movdqa %xmm5,%xmm2 @@ -4104,7 +4134,7 @@ L$add_proceedx: por %xmm0,%xmm2 por %xmm1,%xmm3 - movdqa %xmm4,%xmm0 + movdqa %xmm4,%xmm0 // copy_conditional(res_z, in1_z, in2infty); movdqa %xmm4,%xmm1 pandn %xmm2,%xmm0 movdqa %xmm4,%xmm2 @@ -4117,7 +4147,7 @@ L$add_proceedx: movdqu %xmm2,64(%rdi) movdqu %xmm3,80(%rdi) - movdqa %xmm5,%xmm0 + movdqa %xmm5,%xmm0 // copy_conditional(res_x, in2_x, in1infty); movdqa %xmm5,%xmm1 pandn 288(%rsp),%xmm0 movdqa %xmm5,%xmm2 @@ -4128,7 +4158,7 @@ L$add_proceedx: por %xmm0,%xmm2 por %xmm1,%xmm3 - movdqa %xmm4,%xmm0 + movdqa %xmm4,%xmm0 // copy_conditional(res_x, in1_x, in2infty); movdqa %xmm4,%xmm1 pandn %xmm2,%xmm0 movdqa %xmm4,%xmm2 @@ -4141,7 +4171,7 @@ L$add_proceedx: movdqu %xmm2,0(%rdi) movdqu %xmm3,16(%rdi) - movdqa %xmm5,%xmm0 + movdqa %xmm5,%xmm0 // copy_conditional(res_y, in2_y, in1infty); movdqa %xmm5,%xmm1 pandn 320(%rsp),%xmm0 movdqa %xmm5,%xmm2 @@ -4152,7 +4182,7 @@ L$add_proceedx: por %xmm0,%xmm2 por %xmm1,%xmm3 - movdqa %xmm4,%xmm0 + movdqa %xmm4,%xmm0 // copy_conditional(res_y, in1_y, in2infty); movdqa %xmm4,%xmm1 pandn %xmm2,%xmm0 movdqa %xmm4,%xmm2 @@ -4190,6 +4220,7 @@ L$point_addx_epilogue: .private_extern _ecp_nistz256_point_add_affine_adx .p2align 5 +.alt_entry _ecp_nistz256_point_add_affine_adx _ecp_nistz256_point_add_affine_adx: _CET_ENDBR @@ -4209,14 +4240,14 @@ _CET_ENDBR L$add_affinex_body: - movdqu 0(%rsi),%xmm0 - movq %rdx,%rbx + movdqu 0(%rsi),%xmm0 // copy *(P256_POINT *)%rsi + movq %rdx,%rbx // reassign movdqu 16(%rsi),%xmm1 movdqu 32(%rsi),%xmm2 movdqu 48(%rsi),%xmm3 movdqu 64(%rsi),%xmm4 movdqu 80(%rsi),%xmm5 - movq 64+0(%rsi),%rdx + movq 64+0(%rsi),%rdx // load original in1_z movq 64+8(%rsi),%r14 movq 64+16(%rsi),%r15 movq 64+24(%rsi),%r8 @@ -4228,7 +4259,7 @@ L$add_affinex_body: movdqa %xmm5,384+16(%rsp) por %xmm4,%xmm5 - movdqu 0(%rbx),%xmm0 + movdqu 0(%rbx),%xmm0 // copy *(P256_POINT_AFFINE *)%rbx pshufd $0xb1,%xmm5,%xmm3 movdqu 16(%rbx),%xmm1 movdqu 32(%rbx),%xmm2 @@ -4238,7 +4269,7 @@ L$add_affinex_body: pshufd $0x1e,%xmm5,%xmm4 movdqa %xmm1,416+16(%rsp) por %xmm0,%xmm1 - movq %rdi,%xmm0 + movq %rdi,%xmm0 // save %rdi movdqa %xmm2,448(%rsp) movdqa %xmm3,448+16(%rsp) por %xmm2,%xmm3 @@ -4246,33 +4277,33 @@ L$add_affinex_body: pxor %xmm4,%xmm4 por %xmm1,%xmm3 - leaq 64-128(%rsi),%rsi - leaq 32(%rsp),%rdi - call __ecp_nistz256_sqr_montx + leaq 64-128(%rsi),%rsi // %rsi is still valid + leaq 32(%rsp),%rdi // Z1^2 + call __ecp_nistz256_sqr_montx // p256_sqr_mont(Z1sqr, in1_z); pcmpeqd %xmm4,%xmm5 pshufd $0xb1,%xmm3,%xmm4 - movq 0(%rbx),%rdx - - movq %r12,%r9 + movq 0(%rbx),%rdx // %rbx is still valid +// lea 0x00(%rbx), %rbx + movq %r12,%r9 // harmonize sqr output and mul input por %xmm3,%xmm4 - pshufd $0,%xmm5,%xmm5 + pshufd $0,%xmm5,%xmm5 // in1infty pshufd $0x1e,%xmm4,%xmm3 movq %r13,%r10 por %xmm3,%xmm4 pxor %xmm3,%xmm3 movq %r14,%r11 pcmpeqd %xmm3,%xmm4 - pshufd $0,%xmm4,%xmm4 + pshufd $0,%xmm4,%xmm4 // in2infty leaq 32-128(%rsp),%rsi movq %r15,%r12 - leaq 0(%rsp),%rdi - call __ecp_nistz256_mul_montx + leaq 0(%rsp),%rdi // U2 = X2*Z1^2 + call __ecp_nistz256_mul_montx // p256_mul_mont(U2, Z1sqr, in2_x); leaq 320(%rsp),%rbx - leaq 64(%rsp),%rdi - call __ecp_nistz256_sub_fromx + leaq 64(%rsp),%rdi // H = U2 - U1 + call __ecp_nistz256_sub_fromx // p256_sub(H, U2, in1_x); movq 384(%rsp),%rdx leaq 384(%rsp),%rbx @@ -4281,8 +4312,8 @@ L$add_affinex_body: leaq -128+32(%rsp),%rsi movq 16+32(%rsp),%r11 movq 24+32(%rsp),%r12 - leaq 32(%rsp),%rdi - call __ecp_nistz256_mul_montx + leaq 32(%rsp),%rdi // S2 = Z1^3 + call __ecp_nistz256_mul_montx // p256_mul_mont(S2, Z1sqr, in1_z); movq 384(%rsp),%rdx leaq 384(%rsp),%rbx @@ -4291,8 +4322,8 @@ L$add_affinex_body: leaq -128+64(%rsp),%rsi movq 16+64(%rsp),%r11 movq 24+64(%rsp),%r12 - leaq 288(%rsp),%rdi - call __ecp_nistz256_mul_montx + leaq 288(%rsp),%rdi // Z3 = H*Z1*Z2 + call __ecp_nistz256_mul_montx // p256_mul_mont(res_z, H, in1_z); movq 448(%rsp),%rdx leaq 448(%rsp),%rbx @@ -4301,28 +4332,28 @@ L$add_affinex_body: leaq -128+32(%rsp),%rsi movq 16+32(%rsp),%r11 movq 24+32(%rsp),%r12 - leaq 32(%rsp),%rdi - call __ecp_nistz256_mul_montx + leaq 32(%rsp),%rdi // S2 = Y2*Z1^3 + call __ecp_nistz256_mul_montx // p256_mul_mont(S2, S2, in2_y); leaq 352(%rsp),%rbx - leaq 96(%rsp),%rdi - call __ecp_nistz256_sub_fromx + leaq 96(%rsp),%rdi // R = S2 - S1 + call __ecp_nistz256_sub_fromx // p256_sub(R, S2, in1_y); movq 0+64(%rsp),%rdx movq 8+64(%rsp),%r14 leaq -128+64(%rsp),%rsi movq 16+64(%rsp),%r15 movq 24+64(%rsp),%r8 - leaq 128(%rsp),%rdi - call __ecp_nistz256_sqr_montx + leaq 128(%rsp),%rdi // H^2 + call __ecp_nistz256_sqr_montx // p256_sqr_mont(Hsqr, H); movq 0+96(%rsp),%rdx movq 8+96(%rsp),%r14 leaq -128+96(%rsp),%rsi movq 16+96(%rsp),%r15 movq 24+96(%rsp),%r8 - leaq 192(%rsp),%rdi - call __ecp_nistz256_sqr_montx + leaq 192(%rsp),%rdi // R^2 + call __ecp_nistz256_sqr_montx // p256_sqr_mont(Rsqr, R); movq 128(%rsp),%rdx leaq 128(%rsp),%rbx @@ -4331,8 +4362,8 @@ L$add_affinex_body: leaq -128+64(%rsp),%rsi movq 16+64(%rsp),%r11 movq 24+64(%rsp),%r12 - leaq 160(%rsp),%rdi - call __ecp_nistz256_mul_montx + leaq 160(%rsp),%rdi // H^3 + call __ecp_nistz256_mul_montx // p256_mul_mont(Hcub, Hsqr, H); movq 320(%rsp),%rdx leaq 320(%rsp),%rbx @@ -4341,14 +4372,14 @@ L$add_affinex_body: leaq -128+128(%rsp),%rsi movq 16+128(%rsp),%r11 movq 24+128(%rsp),%r12 - leaq 0(%rsp),%rdi - call __ecp_nistz256_mul_montx - - - + leaq 0(%rsp),%rdi // U1*H^2 + call __ecp_nistz256_mul_montx // p256_mul_mont(U2, in1_x, Hsqr); +// lea 0(%rsp), %rsi +// lea 128(%rsp), %rdi # 2*U1*H^2 +// call __ecp_nistz256_mul_by_2 # ecp_nistz256_mul_by_2(Hsqr, U2); xorq %r11,%r11 - addq %r12,%r12 + addq %r12,%r12 // a0:a3+a0:a3 leaq 192(%rsp),%rsi adcq %r13,%r13 movq %r12,%rax @@ -4374,11 +4405,11 @@ L$add_affinex_body: cmovcq %r10,%r9 movq 24(%rsi),%r10 - call __ecp_nistz256_subx + call __ecp_nistz256_subx // p256_sub(res_x, Rsqr, Hsqr); leaq 160(%rsp),%rbx leaq 224(%rsp),%rdi - call __ecp_nistz256_sub_fromx + call __ecp_nistz256_sub_fromx // p256_sub(res_x, res_x, Hcub); movq 0+0(%rsp),%rax movq 0+8(%rsp),%rbp @@ -4386,10 +4417,10 @@ L$add_affinex_body: movq 0+24(%rsp),%r10 leaq 64(%rsp),%rdi - call __ecp_nistz256_subx + call __ecp_nistz256_subx // p256_sub(H, U2, res_x); - movq %r12,0(%rdi) - movq %r13,8(%rdi) + movq %r12,0(%rdi) // save the result, as + movq %r13,8(%rdi) // __ecp_nistz256_sub doesn't movq %r8,16(%rdi) movq %r9,24(%rdi) movq 352(%rsp),%rdx @@ -4400,7 +4431,7 @@ L$add_affinex_body: movq 16+160(%rsp),%r11 movq 24+160(%rsp),%r12 leaq 32(%rsp),%rdi - call __ecp_nistz256_mul_montx + call __ecp_nistz256_mul_montx // p256_mul_mont(S2, Hcub, in1_y); movq 96(%rsp),%rdx leaq 96(%rsp),%rbx @@ -4410,15 +4441,15 @@ L$add_affinex_body: movq 16+64(%rsp),%r11 movq 24+64(%rsp),%r12 leaq 64(%rsp),%rdi - call __ecp_nistz256_mul_montx + call __ecp_nistz256_mul_montx // p256_mul_mont(H, H, R); leaq 32(%rsp),%rbx leaq 256(%rsp),%rdi - call __ecp_nistz256_sub_fromx + call __ecp_nistz256_sub_fromx // p256_sub(res_y, H, S2); - movq %xmm0,%rdi + movq %xmm0,%rdi // restore %rdi - movdqa %xmm5,%xmm0 + movdqa %xmm5,%xmm0 // copy_conditional(res_z, ONE, in1infty); movdqa %xmm5,%xmm1 pandn 288(%rsp),%xmm0 movdqa %xmm5,%xmm2 @@ -4429,7 +4460,7 @@ L$add_affinex_body: por %xmm0,%xmm2 por %xmm1,%xmm3 - movdqa %xmm4,%xmm0 + movdqa %xmm4,%xmm0 // copy_conditional(res_z, in1_z, in2infty); movdqa %xmm4,%xmm1 pandn %xmm2,%xmm0 movdqa %xmm4,%xmm2 @@ -4442,7 +4473,7 @@ L$add_affinex_body: movdqu %xmm2,64(%rdi) movdqu %xmm3,80(%rdi) - movdqa %xmm5,%xmm0 + movdqa %xmm5,%xmm0 // copy_conditional(res_x, in2_x, in1infty); movdqa %xmm5,%xmm1 pandn 224(%rsp),%xmm0 movdqa %xmm5,%xmm2 @@ -4453,7 +4484,7 @@ L$add_affinex_body: por %xmm0,%xmm2 por %xmm1,%xmm3 - movdqa %xmm4,%xmm0 + movdqa %xmm4,%xmm0 // copy_conditional(res_x, in1_x, in2infty); movdqa %xmm4,%xmm1 pandn %xmm2,%xmm0 movdqa %xmm4,%xmm2 @@ -4466,7 +4497,7 @@ L$add_affinex_body: movdqu %xmm2,0(%rdi) movdqu %xmm3,16(%rdi) - movdqa %xmm5,%xmm0 + movdqa %xmm5,%xmm0 // copy_conditional(res_y, in2_y, in1infty); movdqa %xmm5,%xmm1 pandn 256(%rsp),%xmm0 movdqa %xmm5,%xmm2 @@ -4477,7 +4508,7 @@ L$add_affinex_body: por %xmm0,%xmm2 por %xmm1,%xmm3 - movdqa %xmm4,%xmm0 + movdqa %xmm4,%xmm0 // copy_conditional(res_y, in1_y, in2infty); movdqa %xmm4,%xmm1 pandn %xmm2,%xmm0 movdqa %xmm4,%xmm2 diff --git a/third_party/boringssl/gen/bcm/p256-x86_64-asm-linux.S b/third_party/boringssl/gen/bcm/p256-x86_64-asm-linux.S index c54d57724..c155c963d 100644 --- a/third_party/boringssl/gen/bcm/p256-x86_64-asm-linux.S +++ b/third_party/boringssl/gen/bcm/p256-x86_64-asm-linux.S @@ -6,9 +6,10 @@ #if !defined(OPENSSL_NO_ASM) && defined(OPENSSL_X86_64) && defined(__ELF__) .text - +// The polynomial .section .rodata .align 64 +p256_constants: .Lpoly: .quad 0xffffffffffffffff, 0x00000000ffffffff, 0x0000000000000000, 0xffffffff00000001 @@ -21,15 +22,15 @@ .LONE_mont: .quad 0x0000000000000001, 0xffffffff00000000, 0xffffffffffffffff, 0x00000000fffffffe - +// Constants for computations modulo ord(p256) .Lord: .quad 0xf3b9cac2fc632551, 0xbce6faada7179e84, 0xffffffffffffffff, 0xffffffff00000000 .LordK: .quad 0xccd1c8aaee00bc4f .text - - +// ############################################################################## +// void ecp_nistz256_neg(uint64_t res[4], uint64_t a[4]); .globl ecp_nistz256_neg .hidden ecp_nistz256_neg .type ecp_nistz256_neg,@function @@ -87,11 +88,11 @@ _CET_ENDBR ret .cfi_endproc .size ecp_nistz256_neg,.-ecp_nistz256_neg - - - - - +// ############################################################################## +// void ecp_nistz256_ord_mul_mont( +// uint64_t res[4], +// uint64_t a[4], +// uint64_t b[4]); .globl ecp_nistz256_ord_mul_mont_nohw .hidden ecp_nistz256_ord_mul_mont_nohw @@ -125,7 +126,7 @@ _CET_ENDBR leaq .Lord(%rip),%r14 movq .LordK(%rip),%r15 - +// ################################ * b[0] movq %rax,%rcx mulq 0(%rsi) movq %rax,%r8 @@ -153,16 +154,16 @@ _CET_ENDBR adcq $0,%rdx movq %rdx,%r12 - +// ################################ First reduction step mulq 0(%r14) movq %r8,%rbp - addq %rax,%r13 + addq %rax,%r13 // guaranteed to be zero movq %r8,%rax adcq $0,%rdx movq %rdx,%rcx subq %r8,%r10 - sbbq $0,%r8 + sbbq $0,%r8 // can't borrow mulq 8(%r14) addq %rcx,%r9 @@ -171,19 +172,19 @@ _CET_ENDBR movq %rbp,%rax adcq %rdx,%r10 movq %rbp,%rdx - adcq $0,%r8 + adcq $0,%r8 // can't overflow shlq $32,%rax shrq $32,%rdx subq %rax,%r11 movq 8(%rbx),%rax - sbbq %rdx,%rbp + sbbq %rdx,%rbp // can't borrow addq %r8,%r11 adcq %rbp,%r12 adcq $0,%r13 - +// ################################ * b[1] movq %rax,%rcx mulq 0(%rsi) addq %rax,%r9 @@ -219,15 +220,15 @@ _CET_ENDBR adcq %rdx,%r13 adcq $0,%r8 - +// ################################ Second reduction step mulq 0(%r14) movq %r9,%rbp - addq %rax,%rcx + addq %rax,%rcx // guaranteed to be zero movq %r9,%rax adcq %rdx,%rcx subq %r9,%r11 - sbbq $0,%r9 + sbbq $0,%r9 // can't borrow mulq 8(%r14) addq %rcx,%r10 @@ -236,19 +237,19 @@ _CET_ENDBR movq %rbp,%rax adcq %rdx,%r11 movq %rbp,%rdx - adcq $0,%r9 + adcq $0,%r9 // can't overflow shlq $32,%rax shrq $32,%rdx subq %rax,%r12 movq 16(%rbx),%rax - sbbq %rdx,%rbp + sbbq %rdx,%rbp // can't borrow addq %r9,%r12 adcq %rbp,%r13 adcq $0,%r8 - +// ################################# * b[2] movq %rax,%rcx mulq 0(%rsi) addq %rax,%r10 @@ -284,15 +285,15 @@ _CET_ENDBR adcq %rdx,%r8 adcq $0,%r9 - +// ################################ Third reduction step mulq 0(%r14) movq %r10,%rbp - addq %rax,%rcx + addq %rax,%rcx // guaranteed to be zero movq %r10,%rax adcq %rdx,%rcx subq %r10,%r12 - sbbq $0,%r10 + sbbq $0,%r10 // can't borrow mulq 8(%r14) addq %rcx,%r11 @@ -301,19 +302,19 @@ _CET_ENDBR movq %rbp,%rax adcq %rdx,%r12 movq %rbp,%rdx - adcq $0,%r10 + adcq $0,%r10 // can't overflow shlq $32,%rax shrq $32,%rdx subq %rax,%r13 movq 24(%rbx),%rax - sbbq %rdx,%rbp + sbbq %rdx,%rbp // can't borrow addq %r10,%r13 adcq %rbp,%r8 adcq $0,%r9 - +// ################################ * b[3] movq %rax,%rcx mulq 0(%rsi) addq %rax,%r11 @@ -349,15 +350,15 @@ _CET_ENDBR adcq %rdx,%r9 adcq $0,%r10 - +// ################################ Last reduction step mulq 0(%r14) movq %r11,%rbp - addq %rax,%rcx + addq %rax,%rcx // guaranteed to be zero movq %r11,%rax adcq %rdx,%rcx subq %r11,%r13 - sbbq $0,%r11 + sbbq $0,%r11 // can't borrow mulq 8(%r14) addq %rcx,%r12 @@ -366,18 +367,18 @@ _CET_ENDBR movq %rbp,%rax adcq %rdx,%r13 movq %rbp,%rdx - adcq $0,%r11 + adcq $0,%r11 // can't overflow shlq $32,%rax shrq $32,%rdx subq %rax,%r8 - sbbq %rdx,%rbp + sbbq %rdx,%rbp // can't borrow addq %r11,%r8 adcq %rbp,%r9 adcq $0,%r10 - +// ################################ Subtract ord movq %r12,%rsi subq 0(%r14),%r12 movq %r13,%r11 @@ -417,11 +418,11 @@ _CET_ENDBR .cfi_endproc .size ecp_nistz256_ord_mul_mont_nohw,.-ecp_nistz256_ord_mul_mont_nohw - - - - - +// ############################################################################## +// void ecp_nistz256_ord_sqr_mont( +// uint64_t res[4], +// uint64_t a[4], +// uint64_t rep); .globl ecp_nistz256_ord_sqr_mont_nohw .hidden ecp_nistz256_ord_sqr_mont_nohw @@ -454,56 +455,56 @@ _CET_ENDBR movq 8(%rsi),%rax movq 16(%rsi),%r14 movq 24(%rsi),%r15 - leaq .Lord(%rip),%rsi + leaq .Lord(%rip),%rsi // pointer to modulus movq %rdx,%rbx jmp .Loop_ord_sqr .align 32 .Loop_ord_sqr: - - movq %rax,%rbp - mulq %r8 +// ################################ a[1:] * a[0] + movq %rax,%rbp // put aside a[1] + mulq %r8 // a[1] * a[0] movq %rax,%r9 - movq %rbp,%xmm1 + movq %rbp,%xmm1 // offload a[1] movq %r14,%rax movq %rdx,%r10 - mulq %r8 + mulq %r8 // a[2] * a[0] addq %rax,%r10 movq %r15,%rax - movq %r14,%xmm2 + movq %r14,%xmm2 // offload a[2] adcq $0,%rdx movq %rdx,%r11 - mulq %r8 + mulq %r8 // a[3] * a[0] addq %rax,%r11 movq %r15,%rax - movq %r15,%xmm3 + movq %r15,%xmm3 // offload a[3] adcq $0,%rdx movq %rdx,%r12 - - mulq %r14 +// ################################ a[3] * a[2] + mulq %r14 // a[3] * a[2] movq %rax,%r13 movq %r14,%rax movq %rdx,%r14 - - mulq %rbp +// ################################ a[2:] * a[1] + mulq %rbp // a[2] * a[1] addq %rax,%r11 movq %r15,%rax adcq $0,%rdx movq %rdx,%r15 - mulq %rbp + mulq %rbp // a[3] * a[1] addq %rax,%r12 adcq $0,%rdx addq %r15,%r12 adcq %rdx,%r13 - adcq $0,%r14 - + adcq $0,%r14 // can't overflow +// ################################ *2 xorq %r15,%r15 movq %r8,%rax addq %r9,%r9 @@ -514,20 +515,20 @@ _CET_ENDBR adcq %r14,%r14 adcq $0,%r15 - - mulq %rax +// ################################ Missing products + mulq %rax // a[0] * a[0] movq %rax,%r8 movq %xmm1,%rax movq %rdx,%rbp - mulq %rax + mulq %rax // a[1] * a[1] addq %rbp,%r9 adcq %rax,%r10 movq %xmm2,%rax adcq $0,%rdx movq %rdx,%rbp - mulq %rax + mulq %rax // a[2] * a[2] addq %rbp,%r11 adcq %rax,%r12 movq %xmm3,%rax @@ -535,23 +536,23 @@ _CET_ENDBR movq %rdx,%rbp movq %r8,%rcx - imulq 32(%rsi),%r8 + imulq 32(%rsi),%r8 // *= .LordK - mulq %rax + mulq %rax // a[3] * a[3] addq %rbp,%r13 adcq %rax,%r14 - movq 0(%rsi),%rax - adcq %rdx,%r15 - + movq 0(%rsi),%rax // modulus[0] + adcq %rdx,%r15 // can't overflow +// ################################ First reduction step mulq %r8 movq %r8,%rbp - addq %rax,%rcx - movq 8(%rsi),%rax + addq %rax,%rcx // guaranteed to be zero + movq 8(%rsi),%rax // modulus[1] adcq %rdx,%rcx subq %r8,%r10 - sbbq $0,%rbp + sbbq $0,%rbp // can't borrow mulq %r8 addq %rcx,%r9 @@ -560,29 +561,29 @@ _CET_ENDBR movq %r8,%rax adcq %rdx,%r10 movq %r8,%rdx - adcq $0,%rbp + adcq $0,%rbp // can't overflow movq %r9,%rcx - imulq 32(%rsi),%r9 + imulq 32(%rsi),%r9 // *= .LordK shlq $32,%rax shrq $32,%rdx subq %rax,%r11 movq 0(%rsi),%rax - sbbq %rdx,%r8 + sbbq %rdx,%r8 // can't borrow addq %rbp,%r11 - adcq $0,%r8 - + adcq $0,%r8 // can't overflow +// ################################ Second reduction step mulq %r9 movq %r9,%rbp - addq %rax,%rcx + addq %rax,%rcx // guaranteed to be zero movq 8(%rsi),%rax adcq %rdx,%rcx subq %r9,%r11 - sbbq $0,%rbp + sbbq $0,%rbp // can't borrow mulq %r9 addq %rcx,%r10 @@ -591,29 +592,29 @@ _CET_ENDBR movq %r9,%rax adcq %rdx,%r11 movq %r9,%rdx - adcq $0,%rbp + adcq $0,%rbp // can't overflow movq %r10,%rcx - imulq 32(%rsi),%r10 + imulq 32(%rsi),%r10 // *= .LordK shlq $32,%rax shrq $32,%rdx subq %rax,%r8 movq 0(%rsi),%rax - sbbq %rdx,%r9 + sbbq %rdx,%r9 // can't borrow addq %rbp,%r8 - adcq $0,%r9 - + adcq $0,%r9 // can't overflow +// ################################ Third reduction step mulq %r10 movq %r10,%rbp - addq %rax,%rcx + addq %rax,%rcx // guaranteed to be zero movq 8(%rsi),%rax adcq %rdx,%rcx subq %r10,%r8 - sbbq $0,%rbp + sbbq $0,%rbp // can't borrow mulq %r10 addq %rcx,%r11 @@ -622,29 +623,29 @@ _CET_ENDBR movq %r10,%rax adcq %rdx,%r8 movq %r10,%rdx - adcq $0,%rbp + adcq $0,%rbp // can't overflow movq %r11,%rcx - imulq 32(%rsi),%r11 + imulq 32(%rsi),%r11 // *= .LordK shlq $32,%rax shrq $32,%rdx subq %rax,%r9 movq 0(%rsi),%rax - sbbq %rdx,%r10 + sbbq %rdx,%r10 // can't borrow addq %rbp,%r9 - adcq $0,%r10 - + adcq $0,%r10 // can't overflow +// ################################ Last reduction step mulq %r11 movq %r11,%rbp - addq %rax,%rcx + addq %rax,%rcx // guaranteed to be zero movq 8(%rsi),%rax adcq %rdx,%rcx subq %r11,%r9 - sbbq $0,%rbp + sbbq $0,%rbp // can't borrow mulq %r11 addq %rcx,%r8 @@ -653,17 +654,17 @@ _CET_ENDBR movq %r11,%rax adcq %rdx,%r9 movq %r11,%rdx - adcq $0,%rbp + adcq $0,%rbp // can't overflow shlq $32,%rax shrq $32,%rdx subq %rax,%r10 - sbbq %rdx,%r11 + sbbq %rdx,%r11 // can't borrow addq %rbp,%r10 - adcq $0,%r11 - + adcq $0,%r11 // can't overflow +// ################################ Add bits [511:256] of the sqr result xorq %rdx,%rdx addq %r12,%r8 adcq %r13,%r9 @@ -673,7 +674,7 @@ _CET_ENDBR movq %r9,%rax adcq $0,%rdx - +// ################################ Compare to modulus subq 0(%rsi),%r8 movq %r10,%r14 sbbq 8(%rsi),%r9 @@ -716,7 +717,7 @@ _CET_ENDBR ret .cfi_endproc .size ecp_nistz256_ord_sqr_mont_nohw,.-ecp_nistz256_ord_sqr_mont_nohw - +// ############################################################################## .globl ecp_nistz256_ord_mul_mont_adx .hidden ecp_nistz256_ord_mul_mont_adx .type ecp_nistz256_ord_mul_mont_adx,@function @@ -751,11 +752,11 @@ _CET_ENDBR movq 8(%rsi),%r10 movq 16(%rsi),%r11 movq 24(%rsi),%r12 - leaq -128(%rsi),%rsi + leaq -128(%rsi),%rsi // control u-op density leaq .Lord-128(%rip),%r14 movq .LordK(%rip),%r15 - +// ################################ Multiply by b[0] mulxq %r9,%r8,%r9 mulxq %r10,%rcx,%r10 mulxq %r11,%rbp,%r11 @@ -767,10 +768,10 @@ _CET_ENDBR adcq %rcx,%r11 adcq $0,%r12 - - xorq %r13,%r13 +// ################################ reduction + xorq %r13,%r13 // %r13=0, cf=0, of=0 mulxq 0+128(%r14),%rcx,%rbp - adcxq %rcx,%r8 + adcxq %rcx,%r8 // guaranteed to be zero adoxq %rbp,%r9 mulxq 8+128(%r14),%rcx,%rbp @@ -787,9 +788,9 @@ _CET_ENDBR adoxq %rbp,%r12 adcxq %r8,%r12 adoxq %r8,%r13 - adcq $0,%r13 - + adcq $0,%r13 // cf=0, of=0 +// ################################ Multiply by b[1] mulxq 0+128(%rsi),%rcx,%rbp adcxq %rcx,%r9 adoxq %rbp,%r10 @@ -810,11 +811,11 @@ _CET_ENDBR adcxq %r8,%r13 adoxq %r8,%r8 - adcq $0,%r8 - + adcq $0,%r8 // cf=0, of=0 +// ################################ reduction mulxq 0+128(%r14),%rcx,%rbp - adcxq %rcx,%r9 + adcxq %rcx,%r9 // guaranteed to be zero adoxq %rbp,%r10 mulxq 8+128(%r14),%rcx,%rbp @@ -831,9 +832,9 @@ _CET_ENDBR adoxq %rbp,%r13 adcxq %r9,%r13 adoxq %r9,%r8 - adcq $0,%r8 - + adcq $0,%r8 // cf=0, of=0 +// ################################ Multiply by b[2] mulxq 0+128(%rsi),%rcx,%rbp adcxq %rcx,%r10 adoxq %rbp,%r11 @@ -854,11 +855,11 @@ _CET_ENDBR adcxq %r9,%r8 adoxq %r9,%r9 - adcq $0,%r9 - + adcq $0,%r9 // cf=0, of=0 +// ################################ reduction mulxq 0+128(%r14),%rcx,%rbp - adcxq %rcx,%r10 + adcxq %rcx,%r10 // guaranteed to be zero adoxq %rbp,%r11 mulxq 8+128(%r14),%rcx,%rbp @@ -875,9 +876,9 @@ _CET_ENDBR adoxq %rbp,%r8 adcxq %r10,%r8 adoxq %r10,%r9 - adcq $0,%r9 - + adcq $0,%r9 // cf=0, of=0 +// ################################ Multiply by b[3] mulxq 0+128(%rsi),%rcx,%rbp adcxq %rcx,%r11 adoxq %rbp,%r12 @@ -898,11 +899,11 @@ _CET_ENDBR adcxq %r10,%r9 adoxq %r10,%r10 - adcq $0,%r10 - + adcq $0,%r10 // cf=0, of=0 +// ################################ reduction mulxq 0+128(%r14),%rcx,%rbp - adcxq %rcx,%r11 + adcxq %rcx,%r11 // guaranteed to be zero adoxq %rbp,%r12 mulxq 8+128(%r14),%rcx,%rbp @@ -923,8 +924,8 @@ _CET_ENDBR adoxq %r11,%r10 adcq $0,%r10 - - +// ################################ +// Branch-less conditional subtraction of P movq %r8,%rcx subq 0(%r14),%r12 sbbq 8(%r14),%r13 @@ -1000,38 +1001,38 @@ _CET_ENDBR .align 32 .Loop_ord_sqrx: - mulxq %r14,%r9,%r10 - mulxq %r15,%rcx,%r11 - movq %rdx,%rax - movq %r14,%xmm1 - mulxq %r8,%rbp,%r12 + mulxq %r14,%r9,%r10 // a[0]*a[1] + mulxq %r15,%rcx,%r11 // a[0]*a[2] + movq %rdx,%rax // offload a[0] + movq %r14,%xmm1 // offload a[1] + mulxq %r8,%rbp,%r12 // a[0]*a[3] movq %r14,%rdx addq %rcx,%r10 - movq %r15,%xmm2 + movq %r15,%xmm2 // offload a[2] adcq %rbp,%r11 adcq $0,%r12 - xorq %r13,%r13 - - mulxq %r15,%rcx,%rbp + xorq %r13,%r13 // %r13=0,cf=0,of=0 +// ################################ + mulxq %r15,%rcx,%rbp // a[1]*a[2] adcxq %rcx,%r11 adoxq %rbp,%r12 - mulxq %r8,%rcx,%rbp + mulxq %r8,%rcx,%rbp // a[1]*a[3] movq %r15,%rdx adcxq %rcx,%r12 adoxq %rbp,%r13 adcq $0,%r13 - - mulxq %r8,%rcx,%r14 +// ################################ + mulxq %r8,%rcx,%r14 // a[2]*a[3] movq %rax,%rdx - movq %r8,%xmm3 - xorq %r15,%r15 - adcxq %r9,%r9 + movq %r8,%xmm3 // offload a[3] + xorq %r15,%r15 // %r15=0,cf=0,of=0 + adcxq %r9,%r9 // acc1:6<<1 adoxq %rcx,%r13 adcxq %r10,%r10 - adoxq %r15,%r14 - + adoxq %r15,%r14 // of=0 +// ################################ a[i]*a[i] mulxq %rdx,%r8,%rbp movq %xmm1,%rdx adcxq %r11,%r11 @@ -1053,13 +1054,13 @@ _CET_ENDBR adoxq %rcx,%r14 adoxq %rax,%r15 - +// ################################ reduction movq %r8,%rdx mulxq 32(%rsi),%rdx,%rcx - xorq %rax,%rax + xorq %rax,%rax // cf=0, of=0 mulxq 0(%rsi),%rcx,%rbp - adcxq %rcx,%r8 + adcxq %rcx,%r8 // guaranteed to be zero adoxq %rbp,%r9 mulxq 8(%rsi),%rcx,%rbp adcxq %rcx,%r9 @@ -1069,15 +1070,15 @@ _CET_ENDBR adoxq %rbp,%r11 mulxq 24(%rsi),%rcx,%rbp adcxq %rcx,%r11 - adoxq %rbp,%r8 - adcxq %rax,%r8 - + adoxq %rbp,%r8 // of=0 + adcxq %rax,%r8 // cf=0 +// ################################ movq %r9,%rdx mulxq 32(%rsi),%rdx,%rcx mulxq 0(%rsi),%rcx,%rbp - adoxq %rcx,%r9 + adoxq %rcx,%r9 // guaranteed to be zero adcxq %rbp,%r10 mulxq 8(%rsi),%rcx,%rbp adoxq %rcx,%r10 @@ -1087,15 +1088,15 @@ _CET_ENDBR adcxq %rbp,%r8 mulxq 24(%rsi),%rcx,%rbp adoxq %rcx,%r8 - adcxq %rbp,%r9 - adoxq %rax,%r9 - + adcxq %rbp,%r9 // cf=0 + adoxq %rax,%r9 // of=0 +// ################################ movq %r10,%rdx mulxq 32(%rsi),%rdx,%rcx mulxq 0(%rsi),%rcx,%rbp - adcxq %rcx,%r10 + adcxq %rcx,%r10 // guaranteed to be zero adoxq %rbp,%r11 mulxq 8(%rsi),%rcx,%rbp adcxq %rcx,%r11 @@ -1105,15 +1106,15 @@ _CET_ENDBR adoxq %rbp,%r9 mulxq 24(%rsi),%rcx,%rbp adcxq %rcx,%r9 - adoxq %rbp,%r10 - adcxq %rax,%r10 - + adoxq %rbp,%r10 // of=0 + adcxq %rax,%r10 // cf=0 +// ################################ movq %r11,%rdx mulxq 32(%rsi),%rdx,%rcx mulxq 0(%rsi),%rcx,%rbp - adoxq %rcx,%r11 + adoxq %rcx,%r11 // guaranteed to be zero adcxq %rbp,%r8 mulxq 8(%rsi),%rcx,%rbp adoxq %rcx,%r8 @@ -1126,8 +1127,8 @@ _CET_ENDBR adcxq %rbp,%r11 adoxq %rax,%r11 - - addq %r8,%r12 +// ################################ accumulate upper half + addq %r8,%r12 // add %r12, %r8 adcq %r13,%r9 movq %r12,%rdx adcq %r14,%r10 @@ -1135,7 +1136,7 @@ _CET_ENDBR movq %r9,%r14 adcq $0,%rax - +// ################################ compare to modulus subq 0(%rsi),%r12 movq %r10,%r15 sbbq 8(%rsi),%r9 @@ -1178,11 +1179,11 @@ _CET_ENDBR ret .cfi_endproc .size ecp_nistz256_ord_sqr_mont_adx,.-ecp_nistz256_ord_sqr_mont_adx - - - - - +// ############################################################################## +// void ecp_nistz256_mul_mont( +// uint64_t res[4], +// uint64_t a[4], +// uint64_t b[4]); .globl ecp_nistz256_mul_mont_nohw .hidden ecp_nistz256_mul_mont_nohw @@ -1242,8 +1243,8 @@ _CET_ENDBR .align 32 __ecp_nistz256_mul_montq: .cfi_startproc - - +// ####################################################################### +// Multiply a by b[0] movq %rax,%rbp mulq %r9 movq .Lpoly+8(%rip),%r14 @@ -1271,20 +1272,20 @@ __ecp_nistz256_mul_montq: xorq %r13,%r13 movq %rdx,%r12 - - - - - - - - +// ####################################################################### +// First reduction step +// Basically now we want to multiply acc[0] by p256, +// and add the result to the acc. +// Due to the special form of p256 we do some optimizations +// +// acc[0] x p256[0..1] = acc[0] x 2^96 - acc[0] +// then we add acc[0] and get acc[0] x 2^96 movq %r8,%rbp shlq $32,%r8 mulq %r15 shrq $32,%rbp - addq %r8,%r9 + addq %r8,%r9 // +=acc[0]<<96 adcq %rbp,%r10 adcq %rax,%r11 movq 8(%rbx),%rax @@ -1292,8 +1293,8 @@ __ecp_nistz256_mul_montq: adcq $0,%r13 xorq %r8,%r8 - - +// ####################################################################### +// Multiply by b[1] movq %rax,%rbp mulq 0(%rsi) addq %rax,%r9 @@ -1325,8 +1326,8 @@ __ecp_nistz256_mul_montq: adcq %rdx,%r13 adcq $0,%r8 - - +// ####################################################################### +// Second reduction step movq %r9,%rbp shlq $32,%r9 mulq %r15 @@ -1339,8 +1340,8 @@ __ecp_nistz256_mul_montq: adcq $0,%r8 xorq %r9,%r9 - - +// ####################################################################### +// Multiply by b[2] movq %rax,%rbp mulq 0(%rsi) addq %rax,%r10 @@ -1372,8 +1373,8 @@ __ecp_nistz256_mul_montq: adcq %rdx,%r8 adcq $0,%r9 - - +// ####################################################################### +// Third reduction step movq %r10,%rbp shlq $32,%r10 mulq %r15 @@ -1386,8 +1387,8 @@ __ecp_nistz256_mul_montq: adcq $0,%r9 xorq %r10,%r10 - - +// ####################################################################### +// Multiply by b[3] movq %rax,%rbp mulq 0(%rsi) addq %rax,%r11 @@ -1419,8 +1420,8 @@ __ecp_nistz256_mul_montq: adcq %rdx,%r9 adcq $0,%r10 - - +// ####################################################################### +// Final reduction step movq %r11,%rbp shlq $32,%r11 mulq %r15 @@ -1433,14 +1434,14 @@ __ecp_nistz256_mul_montq: movq %r13,%rbp adcq $0,%r10 - - - subq $-1,%r12 +// ####################################################################### +// Branch-less conditional subtraction of P + subq $-1,%r12 // .Lpoly[0] movq %r8,%rbx - sbbq %r14,%r13 - sbbq $0,%r8 + sbbq %r14,%r13 // .Lpoly[1] + sbbq $0,%r8 // .Lpoly[2] movq %r9,%rdx - sbbq %r15,%r9 + sbbq %r15,%r9 // .Lpoly[3] sbbq $0,%r10 cmovcq %rcx,%r12 @@ -1456,13 +1457,13 @@ __ecp_nistz256_mul_montq: .cfi_endproc .size __ecp_nistz256_mul_montq,.-__ecp_nistz256_mul_montq +// ############################################################################## +// void ecp_nistz256_sqr_mont( +// uint64_t res[4], +// uint64_t a[4]); - - - - - - +// we optimize the square according to S.Gueron and V.Krasnov, +// "Speeding up Big-Number Squaring" .globl ecp_nistz256_sqr_mont_nohw .hidden ecp_nistz256_sqr_mont_nohw .type ecp_nistz256_sqr_mont_nohw,@function @@ -1520,31 +1521,31 @@ _CET_ENDBR __ecp_nistz256_sqr_montq: .cfi_startproc movq %rax,%r13 - mulq %r14 + mulq %r14 // a[1]*a[0] movq %rax,%r9 movq %r15,%rax movq %rdx,%r10 - mulq %r13 + mulq %r13 // a[0]*a[2] addq %rax,%r10 movq %r8,%rax adcq $0,%rdx movq %rdx,%r11 - mulq %r13 + mulq %r13 // a[0]*a[3] addq %rax,%r11 movq %r15,%rax adcq $0,%rdx movq %rdx,%r12 - - mulq %r14 +// ################################ + mulq %r14 // a[1]*a[2] addq %rax,%r11 movq %r8,%rax adcq $0,%rdx movq %rdx,%rbp - mulq %r14 + mulq %r14 // a[1]*a[3] addq %rax,%r12 movq %r8,%rax adcq $0,%rdx @@ -1552,15 +1553,15 @@ __ecp_nistz256_sqr_montq: movq %rdx,%r13 adcq $0,%r13 - - mulq %r15 +// ################################ + mulq %r15 // a[2]*a[3] xorq %r15,%r15 addq %rax,%r13 movq 0(%rsi),%rax movq %rdx,%r14 adcq $0,%r14 - addq %r9,%r9 + addq %r9,%r9 // acc1:6<<1 adcq %r10,%r10 adcq %r11,%r11 adcq %r12,%r12 @@ -1596,21 +1597,21 @@ __ecp_nistz256_sqr_montq: movq .Lpoly+8(%rip),%rsi movq .Lpoly+24(%rip),%rbp - - - +// ######################################### +// Now the reduction +// First iteration movq %r8,%rcx shlq $32,%r8 mulq %rbp shrq $32,%rcx - addq %r8,%r9 + addq %r8,%r9 // +=acc[0]<<96 adcq %rcx,%r10 adcq %rax,%r11 movq %r9,%rax adcq $0,%rdx - - +// ######################################### +// Second iteration movq %r9,%rcx shlq $32,%r9 movq %rdx,%r8 @@ -1622,8 +1623,8 @@ __ecp_nistz256_sqr_montq: movq %r10,%rax adcq $0,%rdx - - +// ######################################### +// Third iteration movq %r10,%rcx shlq $32,%r10 movq %rdx,%r9 @@ -1635,8 +1636,8 @@ __ecp_nistz256_sqr_montq: movq %r11,%rax adcq $0,%rdx - - +// ########################################## +// Last iteration movq %r11,%rcx shlq $32,%r11 movq %rdx,%r10 @@ -1648,8 +1649,8 @@ __ecp_nistz256_sqr_montq: adcq $0,%rdx xorq %r11,%r11 - - +// ########################################### +// Add the rest of the acc addq %r8,%r12 adcq %r9,%r13 movq %r12,%r8 @@ -1658,12 +1659,12 @@ __ecp_nistz256_sqr_montq: movq %r13,%r9 adcq $0,%r11 - subq $-1,%r12 + subq $-1,%r12 // .Lpoly[0] movq %r14,%r10 - sbbq %rsi,%r13 - sbbq $0,%r14 + sbbq %rsi,%r13 // .Lpoly[1] + sbbq $0,%r14 // .Lpoly[2] movq %r15,%rcx - sbbq %rbp,%r15 + sbbq %rbp,%r15 // .Lpoly[3] sbbq $0,%r11 cmovcq %r8,%r12 @@ -1710,7 +1711,7 @@ _CET_ENDBR movq 8(%rsi),%r10 movq 16(%rsi),%r11 movq 24(%rsi),%r12 - leaq -128(%rsi),%rsi + leaq -128(%rsi),%rsi // control u-op density call __ecp_nistz256_mul_montx @@ -1737,12 +1738,12 @@ _CET_ENDBR .align 32 __ecp_nistz256_mul_montx: .cfi_startproc - - +// ####################################################################### +// Multiply by b[0] mulxq %r9,%r8,%r9 mulxq %r10,%rcx,%r10 movq $32,%r14 - xorq %r13,%r13 + xorq %r13,%r13 // cf=0 mulxq %r11,%rbp,%r11 movq .Lpoly+24(%rip),%r15 adcq %rcx,%r9 @@ -1754,8 +1755,8 @@ __ecp_nistz256_mul_montx: shrxq %r14,%r8,%rcx adcq $0,%r12 - - +// ####################################################################### +// First reduction step addq %rbp,%r9 adcq %rcx,%r10 @@ -1764,10 +1765,10 @@ __ecp_nistz256_mul_montx: adcq %rcx,%r11 adcq %rbp,%r12 adcq $0,%r13 - xorq %r8,%r8 - - + xorq %r8,%r8 // %r8=0,cf=0,of=0 +// ####################################################################### +// Multiply by b[1] mulxq 0+128(%rsi),%rcx,%rbp adcxq %rcx,%r9 adoxq %rbp,%r10 @@ -1791,8 +1792,8 @@ __ecp_nistz256_mul_montx: adoxq %r8,%r8 adcq $0,%r8 - - +// ####################################################################### +// Second reduction step addq %rcx,%r10 adcq %rbp,%r11 @@ -1801,10 +1802,10 @@ __ecp_nistz256_mul_montx: adcq %rcx,%r12 adcq %rbp,%r13 adcq $0,%r8 - xorq %r9,%r9 - - + xorq %r9,%r9 // %r9=0,cf=0,of=0 +// ####################################################################### +// Multiply by b[2] mulxq 0+128(%rsi),%rcx,%rbp adcxq %rcx,%r10 adoxq %rbp,%r11 @@ -1828,8 +1829,8 @@ __ecp_nistz256_mul_montx: adoxq %r9,%r9 adcq $0,%r9 - - +// ####################################################################### +// Third reduction step addq %rcx,%r11 adcq %rbp,%r12 @@ -1838,10 +1839,10 @@ __ecp_nistz256_mul_montx: adcq %rcx,%r13 adcq %rbp,%r8 adcq $0,%r9 - xorq %r10,%r10 - - + xorq %r10,%r10 // %r10=0,cf=0,of=0 +// ####################################################################### +// Multiply by b[3] mulxq 0+128(%rsi),%rcx,%rbp adcxq %rcx,%r11 adoxq %rbp,%r12 @@ -1865,8 +1866,8 @@ __ecp_nistz256_mul_montx: adoxq %r10,%r10 adcq $0,%r10 - - +// ####################################################################### +// Fourth reduction step addq %rcx,%r12 adcq %rbp,%r13 @@ -1878,15 +1879,15 @@ __ecp_nistz256_mul_montx: adcq %rbp,%r9 adcq $0,%r10 - - +// ####################################################################### +// Branch-less conditional subtraction of P xorl %eax,%eax movq %r8,%rcx - sbbq $-1,%r12 - sbbq %r14,%r13 - sbbq $0,%r8 + sbbq $-1,%r12 // .Lpoly[0] + sbbq %r14,%r13 // .Lpoly[1] + sbbq $0,%r8 // .Lpoly[2] movq %r9,%rbp - sbbq %r15,%r9 + sbbq %r15,%r9 // .Lpoly[3] sbbq $0,%r10 cmovcq %rbx,%r12 @@ -1932,7 +1933,7 @@ _CET_ENDBR movq 8(%rsi),%r14 movq 16(%rsi),%r15 movq 24(%rsi),%r8 - leaq -128(%rsi),%rsi + leaq -128(%rsi),%rsi // control u-op density call __ecp_nistz256_sqr_montx @@ -1959,35 +1960,35 @@ _CET_ENDBR .align 32 __ecp_nistz256_sqr_montx: .cfi_startproc - mulxq %r14,%r9,%r10 - mulxq %r15,%rcx,%r11 + mulxq %r14,%r9,%r10 // a[0]*a[1] + mulxq %r15,%rcx,%r11 // a[0]*a[2] xorl %eax,%eax adcq %rcx,%r10 - mulxq %r8,%rbp,%r12 + mulxq %r8,%rbp,%r12 // a[0]*a[3] movq %r14,%rdx adcq %rbp,%r11 adcq $0,%r12 - xorq %r13,%r13 + xorq %r13,%r13 // %r13=0,cf=0,of=0 - - mulxq %r15,%rcx,%rbp +// ################################ + mulxq %r15,%rcx,%rbp // a[1]*a[2] adcxq %rcx,%r11 adoxq %rbp,%r12 - mulxq %r8,%rcx,%rbp + mulxq %r8,%rcx,%rbp // a[1]*a[3] movq %r15,%rdx adcxq %rcx,%r12 adoxq %rbp,%r13 adcq $0,%r13 - - mulxq %r8,%rcx,%r14 +// ################################ + mulxq %r8,%rcx,%r14 // a[2]*a[3] movq 0+128(%rsi),%rdx - xorq %r15,%r15 - adcxq %r9,%r9 + xorq %r15,%r15 // %r15=0,cf=0,of=0 + adcxq %r9,%r9 // acc1:6<<1 adoxq %rcx,%r13 adcxq %r10,%r10 - adoxq %r15,%r14 + adoxq %r15,%r14 // of=0 mulxq %rdx,%r8,%rbp movq 8+128(%rsi),%rdx @@ -2016,7 +2017,7 @@ __ecp_nistz256_sqr_montx: shrxq %rsi,%r8,%rax movq %rdx,%rbp - +// reduction step 1 addq %rcx,%r9 adcq %rax,%r10 @@ -2026,7 +2027,7 @@ __ecp_nistz256_sqr_montx: adcq $0,%r8 shrxq %rsi,%r9,%rax - +// reduction step 2 addq %rcx,%r10 adcq %rax,%r11 @@ -2036,7 +2037,7 @@ __ecp_nistz256_sqr_montx: adcq $0,%r9 shrxq %rsi,%r10,%rax - +// reduction step 3 addq %rcx,%r11 adcq %rax,%r8 @@ -2046,7 +2047,7 @@ __ecp_nistz256_sqr_montx: adcq $0,%r10 shrxq %rsi,%r11,%rax - +// reduction step 4 addq %rcx,%r8 adcq %rax,%r9 @@ -2055,7 +2056,7 @@ __ecp_nistz256_sqr_montx: adcq $0,%r11 xorq %rdx,%rdx - addq %r8,%r12 + addq %r8,%r12 // accumulate upper half movq .Lpoly+8(%rip),%rsi adcq %r9,%r13 movq %r12,%r8 @@ -2064,12 +2065,12 @@ __ecp_nistz256_sqr_montx: movq %r13,%r9 adcq $0,%rdx - subq $-1,%r12 + subq $-1,%r12 // .Lpoly[0] movq %r14,%r10 - sbbq %rsi,%r13 - sbbq $0,%r14 + sbbq %rsi,%r13 // .Lpoly[1] + sbbq $0,%r14 // .Lpoly[2] movq %r15,%r11 - sbbq %rbp,%r15 + sbbq %rbp,%r15 // .Lpoly[3] sbbq $0,%rdx cmovcq %r8,%r12 @@ -2084,8 +2085,8 @@ __ecp_nistz256_sqr_montx: ret .cfi_endproc .size __ecp_nistz256_sqr_montx,.-__ecp_nistz256_sqr_montx - - +// ############################################################################## +// void ecp_nistz256_select_w5_nohw(uint64_t *val, uint64_t *in_t, int index); .globl ecp_nistz256_select_w5_nohw .hidden ecp_nistz256_select_w5_nohw .type ecp_nistz256_select_w5_nohw,@function @@ -2148,8 +2149,8 @@ _CET_ENDBR .LSEH_end_ecp_nistz256_select_w5_nohw: .size ecp_nistz256_select_w5_nohw,.-ecp_nistz256_select_w5_nohw - - +// ############################################################################## +// void ecp_nistz256_select_w7_nohw(uint64_t *val, uint64_t *in_t, int index); .globl ecp_nistz256_select_w7_nohw .hidden ecp_nistz256_select_w7_nohw .type ecp_nistz256_select_w7_nohw,@function @@ -2200,8 +2201,8 @@ _CET_ENDBR .cfi_endproc .LSEH_end_ecp_nistz256_select_w7_nohw: .size ecp_nistz256_select_w7_nohw,.-ecp_nistz256_select_w7_nohw - - +// ############################################################################## +// void ecp_nistz256_select_w5_avx2(uint64_t *val, uint64_t *in_t, int index); .globl ecp_nistz256_select_w5_avx2 .hidden ecp_nistz256_select_w5_avx2 .type ecp_nistz256_select_w5_avx2,@function @@ -2266,8 +2267,8 @@ _CET_ENDBR .LSEH_end_ecp_nistz256_select_w5_avx2: .size ecp_nistz256_select_w5_avx2,.-ecp_nistz256_select_w5_avx2 - - +// ############################################################################## +// void ecp_nistz256_select_w7_avx2(uint64_t *val, uint64_t *in_t, int index); .globl ecp_nistz256_select_w7_avx2 .hidden ecp_nistz256_select_w7_avx2 .type ecp_nistz256_select_w7_avx2,@function @@ -2287,7 +2288,7 @@ _CET_ENDBR vmovd %edx,%xmm1 vpermd %ymm1,%ymm2,%ymm1 - +// Skip index = 0, because it is implicitly the point at infinity movq $21,%rax .Lselect_loop_avx2_w7: @@ -2447,7 +2448,7 @@ __ecp_nistz256_subq: __ecp_nistz256_mul_by_2q: .cfi_startproc xorq %r11,%r11 - addq %r12,%r12 + addq %r12,%r12 // a0:a3+a0:a3 adcq %r13,%r13 movq %r12,%rax adcq %r8,%r8 @@ -2505,10 +2506,10 @@ _CET_ENDBR .Lpoint_doubleq_body: .Lpoint_double_shortcutq: - movdqu 0(%rsi),%xmm0 - movq %rsi,%rbx + movdqu 0(%rsi),%xmm0 // copy *(P256_POINT *)%rsi.x + movq %rsi,%rbx // backup copy movdqu 16(%rsi),%xmm1 - movq 32+0(%rsi),%r12 + movq 32+0(%rsi),%r12 // load in_y in "5-4-0-1" order movq 32+8(%rsi),%r13 movq 32+16(%rsi),%r8 movq 32+24(%rsi),%r9 @@ -2523,7 +2524,7 @@ _CET_ENDBR movq %r11,%xmm2 leaq 0(%rsp),%rdi - call __ecp_nistz256_mul_by_2q + call __ecp_nistz256_mul_by_2q // p256_mul_by_2(S, in_y); movq 64+0(%rsi),%rax movq 64+8(%rsi),%r14 @@ -2531,7 +2532,7 @@ _CET_ENDBR movq 64+24(%rsi),%r8 leaq 64-0(%rsi),%rsi leaq 64(%rsp),%rdi - call __ecp_nistz256_sqr_montq + call __ecp_nistz256_sqr_montq // p256_sqr_mont(Zsqr, in_z); movq 0+0(%rsp),%rax movq 8+0(%rsp),%r14 @@ -2539,9 +2540,9 @@ _CET_ENDBR movq 16+0(%rsp),%r15 movq 24+0(%rsp),%r8 leaq 0(%rsp),%rdi - call __ecp_nistz256_sqr_montq + call __ecp_nistz256_sqr_montq // p256_sqr_mont(S, S); - movq 32(%rbx),%rax + movq 32(%rbx),%rax // %rbx is still valid movq 64+0(%rbx),%r9 movq 64+8(%rbx),%r10 movq 64+16(%rbx),%r11 @@ -2549,24 +2550,24 @@ _CET_ENDBR leaq 64-0(%rbx),%rsi leaq 32(%rbx),%rbx movq %xmm2,%rdi - call __ecp_nistz256_mul_montq - call __ecp_nistz256_mul_by_2q + call __ecp_nistz256_mul_montq // p256_mul_mont(res_z, in_z, in_y); + call __ecp_nistz256_mul_by_2q // p256_mul_by_2(res_z, res_z); - movq 96+0(%rsp),%r12 + movq 96+0(%rsp),%r12 // "5-4-0-1" order movq 96+8(%rsp),%r13 leaq 64(%rsp),%rbx movq 96+16(%rsp),%r8 movq 96+24(%rsp),%r9 leaq 32(%rsp),%rdi - call __ecp_nistz256_add_toq + call __ecp_nistz256_add_toq // p256_add(M, in_x, Zsqr); - movq 96+0(%rsp),%r12 + movq 96+0(%rsp),%r12 // "5-4-0-1" order movq 96+8(%rsp),%r13 leaq 64(%rsp),%rbx movq 96+16(%rsp),%r8 movq 96+24(%rsp),%r9 leaq 64(%rsp),%rdi - call __ecp_nistz256_sub_fromq + call __ecp_nistz256_sub_fromq // p256_sub(Zsqr, in_x, Zsqr); movq 0+0(%rsp),%rax movq 8+0(%rsp),%r14 @@ -2574,7 +2575,7 @@ _CET_ENDBR movq 16+0(%rsp),%r15 movq 24+0(%rsp),%r8 movq %xmm1,%rdi - call __ecp_nistz256_sqr_montq + call __ecp_nistz256_sqr_montq // p256_sqr_mont(res_y, S); xorq %r9,%r9 movq %r12,%rax addq $-1,%r12 @@ -2585,7 +2586,7 @@ _CET_ENDBR movq %r15,%r8 adcq %rbp,%r15 adcq $0,%r9 - xorq %rsi,%rsi + xorq %rsi,%rsi // borrow %rsi testq $1,%rax cmovzq %rax,%r12 @@ -2594,7 +2595,7 @@ _CET_ENDBR cmovzq %r8,%r15 cmovzq %rsi,%r9 - movq %r13,%rax + movq %r13,%rax // a0:a3>>1 shrq $1,%r12 shlq $63,%rax movq %r14,%r10 @@ -2621,14 +2622,14 @@ _CET_ENDBR movq 16+32(%rsp),%r11 movq 24+32(%rsp),%r12 leaq 32(%rsp),%rdi - call __ecp_nistz256_mul_montq + call __ecp_nistz256_mul_montq // p256_mul_mont(M, M, Zsqr); leaq 128(%rsp),%rdi call __ecp_nistz256_mul_by_2q leaq 32(%rsp),%rbx leaq 32(%rsp),%rdi - call __ecp_nistz256_add_toq + call __ecp_nistz256_add_toq // p256_mul_by_3(M, M); movq 96(%rsp),%rax leaq 96(%rsp),%rbx @@ -2638,10 +2639,10 @@ _CET_ENDBR movq 16+0(%rsp),%r11 movq 24+0(%rsp),%r12 leaq 0(%rsp),%rdi - call __ecp_nistz256_mul_montq + call __ecp_nistz256_mul_montq // p256_mul_mont(S, S, in_x); leaq 128(%rsp),%rdi - call __ecp_nistz256_mul_by_2q + call __ecp_nistz256_mul_by_2q // p256_mul_by_2(tmp0, S); movq 0+32(%rsp),%rax movq 8+32(%rsp),%r14 @@ -2649,27 +2650,27 @@ _CET_ENDBR movq 16+32(%rsp),%r15 movq 24+32(%rsp),%r8 movq %xmm0,%rdi - call __ecp_nistz256_sqr_montq + call __ecp_nistz256_sqr_montq // p256_sqr_mont(res_x, M); leaq 128(%rsp),%rbx - movq %r14,%r8 + movq %r14,%r8 // harmonize sqr output and sub input movq %r15,%r9 movq %rsi,%r14 movq %rbp,%r15 - call __ecp_nistz256_sub_fromq + call __ecp_nistz256_sub_fromq // p256_sub(res_x, res_x, tmp0); movq 0+0(%rsp),%rax movq 0+8(%rsp),%rbp movq 0+16(%rsp),%rcx - movq 0+24(%rsp),%r10 + movq 0+24(%rsp),%r10 // "4-5-0-1" order leaq 0(%rsp),%rdi - call __ecp_nistz256_subq + call __ecp_nistz256_subq // p256_sub(S, S, res_x); movq 32(%rsp),%rax leaq 32(%rsp),%rbx - movq %r12,%r14 + movq %r12,%r14 // harmonize sub output and mul input xorl %ecx,%ecx - movq %r12,0+0(%rsp) + movq %r12,0+0(%rsp) // have to save:-( movq %r13,%r10 movq %r13,0+8(%rsp) cmovzq %r8,%r11 @@ -2679,11 +2680,11 @@ _CET_ENDBR movq %r9,0+24(%rsp) movq %r14,%r9 leaq 0(%rsp),%rdi - call __ecp_nistz256_mul_montq + call __ecp_nistz256_mul_montq // p256_mul_mont(S, S, M); movq %xmm1,%rbx movq %xmm1,%rdi - call __ecp_nistz256_sub_fromq + call __ecp_nistz256_sub_fromq // p256_sub(res_y, S, res_y); leaq 160+56(%rsp),%rsi .cfi_def_cfa %rsi,8 @@ -2734,14 +2735,14 @@ _CET_ENDBR .cfi_adjust_cfa_offset 32*18+8 .Lpoint_addq_body: - movdqu 0(%rsi),%xmm0 + movdqu 0(%rsi),%xmm0 // copy *(P256_POINT *)%rsi movdqu 16(%rsi),%xmm1 movdqu 32(%rsi),%xmm2 movdqu 48(%rsi),%xmm3 movdqu 64(%rsi),%xmm4 movdqu 80(%rsi),%xmm5 - movq %rsi,%rbx - movq %rdx,%rsi + movq %rsi,%rbx // reassign + movq %rdx,%rsi // reassign movdqa %xmm0,384(%rsp) movdqa %xmm1,384+16(%rsp) movdqa %xmm2,416(%rsp) @@ -2750,54 +2751,54 @@ _CET_ENDBR movdqa %xmm5,448+16(%rsp) por %xmm4,%xmm5 - movdqu 0(%rsi),%xmm0 + movdqu 0(%rsi),%xmm0 // copy *(P256_POINT *)%rbx pshufd $0xb1,%xmm5,%xmm3 movdqu 16(%rsi),%xmm1 movdqu 32(%rsi),%xmm2 por %xmm3,%xmm5 movdqu 48(%rsi),%xmm3 - movq 64+0(%rsi),%rax + movq 64+0(%rsi),%rax // load original in2_z movq 64+8(%rsi),%r14 movq 64+16(%rsi),%r15 movq 64+24(%rsi),%r8 movdqa %xmm0,480(%rsp) pshufd $0x1e,%xmm5,%xmm4 movdqa %xmm1,480+16(%rsp) - movdqu 64(%rsi),%xmm0 + movdqu 64(%rsi),%xmm0 // in2_z again movdqu 80(%rsi),%xmm1 movdqa %xmm2,512(%rsp) movdqa %xmm3,512+16(%rsp) por %xmm4,%xmm5 pxor %xmm4,%xmm4 por %xmm0,%xmm1 - movq %rdi,%xmm0 + movq %rdi,%xmm0 // save %rdi - leaq 64-0(%rsi),%rsi - movq %rax,544+0(%rsp) + leaq 64-0(%rsi),%rsi // %rsi is still valid + movq %rax,544+0(%rsp) // make in2_z copy movq %r14,544+8(%rsp) movq %r15,544+16(%rsp) movq %r8,544+24(%rsp) - leaq 96(%rsp),%rdi - call __ecp_nistz256_sqr_montq + leaq 96(%rsp),%rdi // Z2^2 + call __ecp_nistz256_sqr_montq // p256_sqr_mont(Z2sqr, in2_z); pcmpeqd %xmm4,%xmm5 pshufd $0xb1,%xmm1,%xmm4 por %xmm1,%xmm4 - pshufd $0,%xmm5,%xmm5 + pshufd $0,%xmm5,%xmm5 // in1infty pshufd $0x1e,%xmm4,%xmm3 por %xmm3,%xmm4 pxor %xmm3,%xmm3 pcmpeqd %xmm3,%xmm4 - pshufd $0,%xmm4,%xmm4 - movq 64+0(%rbx),%rax + pshufd $0,%xmm4,%xmm4 // in2infty + movq 64+0(%rbx),%rax // load original in1_z movq 64+8(%rbx),%r14 movq 64+16(%rbx),%r15 movq 64+24(%rbx),%r8 movq %rbx,%xmm1 leaq 64-0(%rbx),%rsi - leaq 32(%rsp),%rdi - call __ecp_nistz256_sqr_montq + leaq 32(%rsp),%rdi // Z1^2 + call __ecp_nistz256_sqr_montq // p256_sqr_mont(Z1sqr, in1_z); movq 544(%rsp),%rax leaq 544(%rsp),%rbx @@ -2806,8 +2807,8 @@ _CET_ENDBR leaq 0+96(%rsp),%rsi movq 16+96(%rsp),%r11 movq 24+96(%rsp),%r12 - leaq 224(%rsp),%rdi - call __ecp_nistz256_mul_montq + leaq 224(%rsp),%rdi // S1 = Z2^3 + call __ecp_nistz256_mul_montq // p256_mul_mont(S1, Z2sqr, in2_z); movq 448(%rsp),%rax leaq 448(%rsp),%rbx @@ -2816,8 +2817,8 @@ _CET_ENDBR leaq 0+32(%rsp),%rsi movq 16+32(%rsp),%r11 movq 24+32(%rsp),%r12 - leaq 256(%rsp),%rdi - call __ecp_nistz256_mul_montq + leaq 256(%rsp),%rdi // S2 = Z1^3 + call __ecp_nistz256_mul_montq // p256_mul_mont(S2, Z1sqr, in1_z); movq 416(%rsp),%rax leaq 416(%rsp),%rbx @@ -2826,8 +2827,8 @@ _CET_ENDBR leaq 0+224(%rsp),%rsi movq 16+224(%rsp),%r11 movq 24+224(%rsp),%r12 - leaq 224(%rsp),%rdi - call __ecp_nistz256_mul_montq + leaq 224(%rsp),%rdi // S1 = Y1*Z2^3 + call __ecp_nistz256_mul_montq // p256_mul_mont(S1, S1, in1_y); movq 512(%rsp),%rax leaq 512(%rsp),%rbx @@ -2836,18 +2837,18 @@ _CET_ENDBR leaq 0+256(%rsp),%rsi movq 16+256(%rsp),%r11 movq 24+256(%rsp),%r12 - leaq 256(%rsp),%rdi - call __ecp_nistz256_mul_montq + leaq 256(%rsp),%rdi // S2 = Y2*Z1^3 + call __ecp_nistz256_mul_montq // p256_mul_mont(S2, S2, in2_y); leaq 224(%rsp),%rbx - leaq 64(%rsp),%rdi - call __ecp_nistz256_sub_fromq + leaq 64(%rsp),%rdi // R = S2 - S1 + call __ecp_nistz256_sub_fromq // p256_sub(R, S2, S1); - orq %r13,%r12 + orq %r13,%r12 // see if result is zero movdqa %xmm4,%xmm2 orq %r8,%r12 orq %r9,%r12 - por %xmm5,%xmm2 + por %xmm5,%xmm2 // in1infty || in2infty movq %r12,%xmm3 movq 384(%rsp),%rax @@ -2857,8 +2858,8 @@ _CET_ENDBR leaq 0+96(%rsp),%rsi movq 16+96(%rsp),%r11 movq 24+96(%rsp),%r12 - leaq 160(%rsp),%rdi - call __ecp_nistz256_mul_montq + leaq 160(%rsp),%rdi // U1 = X1*Z2^2 + call __ecp_nistz256_mul_montq // p256_mul_mont(U1, in1_x, Z2sqr); movq 480(%rsp),%rax leaq 480(%rsp),%rbx @@ -2867,34 +2868,34 @@ _CET_ENDBR leaq 0+32(%rsp),%rsi movq 16+32(%rsp),%r11 movq 24+32(%rsp),%r12 - leaq 192(%rsp),%rdi - call __ecp_nistz256_mul_montq + leaq 192(%rsp),%rdi // U2 = X2*Z1^2 + call __ecp_nistz256_mul_montq // p256_mul_mont(U2, in2_x, Z1sqr); leaq 160(%rsp),%rbx - leaq 0(%rsp),%rdi - call __ecp_nistz256_sub_fromq + leaq 0(%rsp),%rdi // H = U2 - U1 + call __ecp_nistz256_sub_fromq // p256_sub(H, U2, U1); - orq %r13,%r12 + orq %r13,%r12 // see if result is zero orq %r8,%r12 - orq %r9,%r12 + orq %r9,%r12 // movq %xmm2,%r8 movq %xmm3,%r9 orq %r8,%r12 -.byte 0x3e - jnz .Ladd_proceedq - - +.byte 0x3e // predict taken + jnz .Ladd_proceedq // +// We now know A = B or A = -B and neither is infinity. Compare the +// y-coordinates via S1 and S2. testq %r9,%r9 - jz .Ladd_doubleq - - - - - - - movq %xmm0,%rdi + jz .Ladd_doubleq // is_equal(S1, S2) + +// A = -B, so the result is infinity. +// +// TODO(davidben): Does .Ladd_proceed handle this case? It seems to, in +// which case we should eliminate this special-case and simplify the +// timing analysis. + movq %xmm0,%rdi // restore %rdi pxor %xmm0,%xmm0 movdqu %xmm0,0(%rdi) movdqu %xmm0,16(%rdi) @@ -2906,9 +2907,9 @@ _CET_ENDBR .align 32 .Ladd_doubleq: - movq %xmm1,%rsi - movq %xmm0,%rdi - addq $416,%rsp + movq %xmm1,%rsi // restore %rsi + movq %xmm0,%rdi // restore %rdi + addq $416,%rsp // difference in frame sizes .cfi_adjust_cfa_offset -416 jmp .Lpoint_double_shortcutq .cfi_adjust_cfa_offset 416 @@ -2920,8 +2921,8 @@ _CET_ENDBR leaq 0+64(%rsp),%rsi movq 16+64(%rsp),%r15 movq 24+64(%rsp),%r8 - leaq 96(%rsp),%rdi - call __ecp_nistz256_sqr_montq + leaq 96(%rsp),%rdi // R^2 + call __ecp_nistz256_sqr_montq // p256_sqr_mont(Rsqr, R); movq 448(%rsp),%rax leaq 448(%rsp),%rbx @@ -2930,16 +2931,16 @@ _CET_ENDBR leaq 0+0(%rsp),%rsi movq 16+0(%rsp),%r11 movq 24+0(%rsp),%r12 - leaq 352(%rsp),%rdi - call __ecp_nistz256_mul_montq + leaq 352(%rsp),%rdi // Z3 = H*Z1*Z2 + call __ecp_nistz256_mul_montq // p256_mul_mont(res_z, H, in1_z); movq 0+0(%rsp),%rax movq 8+0(%rsp),%r14 leaq 0+0(%rsp),%rsi movq 16+0(%rsp),%r15 movq 24+0(%rsp),%r8 - leaq 32(%rsp),%rdi - call __ecp_nistz256_sqr_montq + leaq 32(%rsp),%rdi // H^2 + call __ecp_nistz256_sqr_montq // p256_sqr_mont(Hsqr, H); movq 544(%rsp),%rax leaq 544(%rsp),%rbx @@ -2948,8 +2949,8 @@ _CET_ENDBR leaq 0+352(%rsp),%rsi movq 16+352(%rsp),%r11 movq 24+352(%rsp),%r12 - leaq 352(%rsp),%rdi - call __ecp_nistz256_mul_montq + leaq 352(%rsp),%rdi // Z3 = H*Z1*Z2 + call __ecp_nistz256_mul_montq // p256_mul_mont(res_z, res_z, in2_z); movq 0(%rsp),%rax leaq 0(%rsp),%rbx @@ -2958,8 +2959,8 @@ _CET_ENDBR leaq 0+32(%rsp),%rsi movq 16+32(%rsp),%r11 movq 24+32(%rsp),%r12 - leaq 128(%rsp),%rdi - call __ecp_nistz256_mul_montq + leaq 128(%rsp),%rdi // H^3 + call __ecp_nistz256_mul_montq // p256_mul_mont(Hcub, Hsqr, H); movq 160(%rsp),%rax leaq 160(%rsp),%rbx @@ -2968,14 +2969,14 @@ _CET_ENDBR leaq 0+32(%rsp),%rsi movq 16+32(%rsp),%r11 movq 24+32(%rsp),%r12 - leaq 192(%rsp),%rdi - call __ecp_nistz256_mul_montq - - - + leaq 192(%rsp),%rdi // U1*H^2 + call __ecp_nistz256_mul_montq // p256_mul_mont(U2, U1, Hsqr); +// lea 192(%rsp), %rsi +// lea 32(%rsp), %rdi # 2*U1*H^2 +// call __ecp_nistz256_mul_by_2 # ecp_nistz256_mul_by_2(Hsqr, U2); xorq %r11,%r11 - addq %r12,%r12 + addq %r12,%r12 // a0:a3+a0:a3 leaq 96(%rsp),%rsi adcq %r13,%r13 movq %r12,%rax @@ -3001,11 +3002,11 @@ _CET_ENDBR cmovcq %r10,%r9 movq 24(%rsi),%r10 - call __ecp_nistz256_subq + call __ecp_nistz256_subq // p256_sub(res_x, Rsqr, Hsqr); leaq 128(%rsp),%rbx leaq 288(%rsp),%rdi - call __ecp_nistz256_sub_fromq + call __ecp_nistz256_sub_fromq // p256_sub(res_x, res_x, Hcub); movq 192+0(%rsp),%rax movq 192+8(%rsp),%rbp @@ -3013,10 +3014,10 @@ _CET_ENDBR movq 192+24(%rsp),%r10 leaq 320(%rsp),%rdi - call __ecp_nistz256_subq + call __ecp_nistz256_subq // p256_sub(res_y, U2, res_x); - movq %r12,0(%rdi) - movq %r13,8(%rdi) + movq %r12,0(%rdi) // save the result, as + movq %r13,8(%rdi) // __ecp_nistz256_sub doesn't movq %r8,16(%rdi) movq %r9,24(%rdi) movq 128(%rsp),%rax @@ -3027,7 +3028,7 @@ _CET_ENDBR movq 16+224(%rsp),%r11 movq 24+224(%rsp),%r12 leaq 256(%rsp),%rdi - call __ecp_nistz256_mul_montq + call __ecp_nistz256_mul_montq // p256_mul_mont(S2, S1, Hcub); movq 320(%rsp),%rax leaq 320(%rsp),%rbx @@ -3037,15 +3038,15 @@ _CET_ENDBR movq 16+64(%rsp),%r11 movq 24+64(%rsp),%r12 leaq 320(%rsp),%rdi - call __ecp_nistz256_mul_montq + call __ecp_nistz256_mul_montq // p256_mul_mont(res_y, R, res_y); leaq 256(%rsp),%rbx leaq 320(%rsp),%rdi - call __ecp_nistz256_sub_fromq + call __ecp_nistz256_sub_fromq // p256_sub(res_y, res_y, S2); - movq %xmm0,%rdi + movq %xmm0,%rdi // restore %rdi - movdqa %xmm5,%xmm0 + movdqa %xmm5,%xmm0 // copy_conditional(res_z, in2_z, in1infty); movdqa %xmm5,%xmm1 pandn 352(%rsp),%xmm0 movdqa %xmm5,%xmm2 @@ -3056,7 +3057,7 @@ _CET_ENDBR por %xmm0,%xmm2 por %xmm1,%xmm3 - movdqa %xmm4,%xmm0 + movdqa %xmm4,%xmm0 // copy_conditional(res_z, in1_z, in2infty); movdqa %xmm4,%xmm1 pandn %xmm2,%xmm0 movdqa %xmm4,%xmm2 @@ -3069,7 +3070,7 @@ _CET_ENDBR movdqu %xmm2,64(%rdi) movdqu %xmm3,80(%rdi) - movdqa %xmm5,%xmm0 + movdqa %xmm5,%xmm0 // copy_conditional(res_x, in2_x, in1infty); movdqa %xmm5,%xmm1 pandn 288(%rsp),%xmm0 movdqa %xmm5,%xmm2 @@ -3080,7 +3081,7 @@ _CET_ENDBR por %xmm0,%xmm2 por %xmm1,%xmm3 - movdqa %xmm4,%xmm0 + movdqa %xmm4,%xmm0 // copy_conditional(res_x, in1_x, in2infty); movdqa %xmm4,%xmm1 pandn %xmm2,%xmm0 movdqa %xmm4,%xmm2 @@ -3093,7 +3094,7 @@ _CET_ENDBR movdqu %xmm2,0(%rdi) movdqu %xmm3,16(%rdi) - movdqa %xmm5,%xmm0 + movdqa %xmm5,%xmm0 // copy_conditional(res_y, in2_y, in1infty); movdqa %xmm5,%xmm1 pandn 320(%rsp),%xmm0 movdqa %xmm5,%xmm2 @@ -3104,7 +3105,7 @@ _CET_ENDBR por %xmm0,%xmm2 por %xmm1,%xmm3 - movdqa %xmm4,%xmm0 + movdqa %xmm4,%xmm0 // copy_conditional(res_y, in1_y, in2infty); movdqa %xmm4,%xmm1 pandn %xmm2,%xmm0 movdqa %xmm4,%xmm2 @@ -3167,14 +3168,14 @@ _CET_ENDBR .cfi_adjust_cfa_offset 32*15+8 .Ladd_affineq_body: - movdqu 0(%rsi),%xmm0 - movq %rdx,%rbx + movdqu 0(%rsi),%xmm0 // copy *(P256_POINT *)%rsi + movq %rdx,%rbx // reassign movdqu 16(%rsi),%xmm1 movdqu 32(%rsi),%xmm2 movdqu 48(%rsi),%xmm3 movdqu 64(%rsi),%xmm4 movdqu 80(%rsi),%xmm5 - movq 64+0(%rsi),%rax + movq 64+0(%rsi),%rax // load original in1_z movq 64+8(%rsi),%r14 movq 64+16(%rsi),%r15 movq 64+24(%rsi),%r8 @@ -3186,7 +3187,7 @@ _CET_ENDBR movdqa %xmm5,384+16(%rsp) por %xmm4,%xmm5 - movdqu 0(%rbx),%xmm0 + movdqu 0(%rbx),%xmm0 // copy *(P256_POINT_AFFINE *)%rbx pshufd $0xb1,%xmm5,%xmm3 movdqu 16(%rbx),%xmm1 movdqu 32(%rbx),%xmm2 @@ -3196,7 +3197,7 @@ _CET_ENDBR pshufd $0x1e,%xmm5,%xmm4 movdqa %xmm1,416+16(%rsp) por %xmm0,%xmm1 - movq %rdi,%xmm0 + movq %rdi,%xmm0 // save %rdi movdqa %xmm2,448(%rsp) movdqa %xmm3,448+16(%rsp) por %xmm2,%xmm3 @@ -3204,33 +3205,33 @@ _CET_ENDBR pxor %xmm4,%xmm4 por %xmm1,%xmm3 - leaq 64-0(%rsi),%rsi - leaq 32(%rsp),%rdi - call __ecp_nistz256_sqr_montq + leaq 64-0(%rsi),%rsi // %rsi is still valid + leaq 32(%rsp),%rdi // Z1^2 + call __ecp_nistz256_sqr_montq // p256_sqr_mont(Z1sqr, in1_z); pcmpeqd %xmm4,%xmm5 pshufd $0xb1,%xmm3,%xmm4 - movq 0(%rbx),%rax - - movq %r12,%r9 + movq 0(%rbx),%rax // %rbx is still valid +// lea 0x00(%rbx), %rbx + movq %r12,%r9 // harmonize sqr output and mul input por %xmm3,%xmm4 - pshufd $0,%xmm5,%xmm5 + pshufd $0,%xmm5,%xmm5 // in1infty pshufd $0x1e,%xmm4,%xmm3 movq %r13,%r10 por %xmm3,%xmm4 pxor %xmm3,%xmm3 movq %r14,%r11 pcmpeqd %xmm3,%xmm4 - pshufd $0,%xmm4,%xmm4 + pshufd $0,%xmm4,%xmm4 // in2infty leaq 32-0(%rsp),%rsi movq %r15,%r12 - leaq 0(%rsp),%rdi - call __ecp_nistz256_mul_montq + leaq 0(%rsp),%rdi // U2 = X2*Z1^2 + call __ecp_nistz256_mul_montq // p256_mul_mont(U2, Z1sqr, in2_x); leaq 320(%rsp),%rbx - leaq 64(%rsp),%rdi - call __ecp_nistz256_sub_fromq + leaq 64(%rsp),%rdi // H = U2 - U1 + call __ecp_nistz256_sub_fromq // p256_sub(H, U2, in1_x); movq 384(%rsp),%rax leaq 384(%rsp),%rbx @@ -3239,8 +3240,8 @@ _CET_ENDBR leaq 0+32(%rsp),%rsi movq 16+32(%rsp),%r11 movq 24+32(%rsp),%r12 - leaq 32(%rsp),%rdi - call __ecp_nistz256_mul_montq + leaq 32(%rsp),%rdi // S2 = Z1^3 + call __ecp_nistz256_mul_montq // p256_mul_mont(S2, Z1sqr, in1_z); movq 384(%rsp),%rax leaq 384(%rsp),%rbx @@ -3249,8 +3250,8 @@ _CET_ENDBR leaq 0+64(%rsp),%rsi movq 16+64(%rsp),%r11 movq 24+64(%rsp),%r12 - leaq 288(%rsp),%rdi - call __ecp_nistz256_mul_montq + leaq 288(%rsp),%rdi // Z3 = H*Z1*Z2 + call __ecp_nistz256_mul_montq // p256_mul_mont(res_z, H, in1_z); movq 448(%rsp),%rax leaq 448(%rsp),%rbx @@ -3259,28 +3260,28 @@ _CET_ENDBR leaq 0+32(%rsp),%rsi movq 16+32(%rsp),%r11 movq 24+32(%rsp),%r12 - leaq 32(%rsp),%rdi - call __ecp_nistz256_mul_montq + leaq 32(%rsp),%rdi // S2 = Y2*Z1^3 + call __ecp_nistz256_mul_montq // p256_mul_mont(S2, S2, in2_y); leaq 352(%rsp),%rbx - leaq 96(%rsp),%rdi - call __ecp_nistz256_sub_fromq + leaq 96(%rsp),%rdi // R = S2 - S1 + call __ecp_nistz256_sub_fromq // p256_sub(R, S2, in1_y); movq 0+64(%rsp),%rax movq 8+64(%rsp),%r14 leaq 0+64(%rsp),%rsi movq 16+64(%rsp),%r15 movq 24+64(%rsp),%r8 - leaq 128(%rsp),%rdi - call __ecp_nistz256_sqr_montq + leaq 128(%rsp),%rdi // H^2 + call __ecp_nistz256_sqr_montq // p256_sqr_mont(Hsqr, H); movq 0+96(%rsp),%rax movq 8+96(%rsp),%r14 leaq 0+96(%rsp),%rsi movq 16+96(%rsp),%r15 movq 24+96(%rsp),%r8 - leaq 192(%rsp),%rdi - call __ecp_nistz256_sqr_montq + leaq 192(%rsp),%rdi // R^2 + call __ecp_nistz256_sqr_montq // p256_sqr_mont(Rsqr, R); movq 128(%rsp),%rax leaq 128(%rsp),%rbx @@ -3289,8 +3290,8 @@ _CET_ENDBR leaq 0+64(%rsp),%rsi movq 16+64(%rsp),%r11 movq 24+64(%rsp),%r12 - leaq 160(%rsp),%rdi - call __ecp_nistz256_mul_montq + leaq 160(%rsp),%rdi // H^3 + call __ecp_nistz256_mul_montq // p256_mul_mont(Hcub, Hsqr, H); movq 320(%rsp),%rax leaq 320(%rsp),%rbx @@ -3299,14 +3300,14 @@ _CET_ENDBR leaq 0+128(%rsp),%rsi movq 16+128(%rsp),%r11 movq 24+128(%rsp),%r12 - leaq 0(%rsp),%rdi - call __ecp_nistz256_mul_montq - - - + leaq 0(%rsp),%rdi // U1*H^2 + call __ecp_nistz256_mul_montq // p256_mul_mont(U2, in1_x, Hsqr); +// lea 0(%rsp), %rsi +// lea 128(%rsp), %rdi # 2*U1*H^2 +// call __ecp_nistz256_mul_by_2 # ecp_nistz256_mul_by_2(Hsqr, U2); xorq %r11,%r11 - addq %r12,%r12 + addq %r12,%r12 // a0:a3+a0:a3 leaq 192(%rsp),%rsi adcq %r13,%r13 movq %r12,%rax @@ -3332,11 +3333,11 @@ _CET_ENDBR cmovcq %r10,%r9 movq 24(%rsi),%r10 - call __ecp_nistz256_subq + call __ecp_nistz256_subq // p256_sub(res_x, Rsqr, Hsqr); leaq 160(%rsp),%rbx leaq 224(%rsp),%rdi - call __ecp_nistz256_sub_fromq + call __ecp_nistz256_sub_fromq // p256_sub(res_x, res_x, Hcub); movq 0+0(%rsp),%rax movq 0+8(%rsp),%rbp @@ -3344,10 +3345,10 @@ _CET_ENDBR movq 0+24(%rsp),%r10 leaq 64(%rsp),%rdi - call __ecp_nistz256_subq + call __ecp_nistz256_subq // p256_sub(H, U2, res_x); - movq %r12,0(%rdi) - movq %r13,8(%rdi) + movq %r12,0(%rdi) // save the result, as + movq %r13,8(%rdi) // __ecp_nistz256_sub doesn't movq %r8,16(%rdi) movq %r9,24(%rdi) movq 352(%rsp),%rax @@ -3358,7 +3359,7 @@ _CET_ENDBR movq 16+160(%rsp),%r11 movq 24+160(%rsp),%r12 leaq 32(%rsp),%rdi - call __ecp_nistz256_mul_montq + call __ecp_nistz256_mul_montq // p256_mul_mont(S2, Hcub, in1_y); movq 96(%rsp),%rax leaq 96(%rsp),%rbx @@ -3368,15 +3369,15 @@ _CET_ENDBR movq 16+64(%rsp),%r11 movq 24+64(%rsp),%r12 leaq 64(%rsp),%rdi - call __ecp_nistz256_mul_montq + call __ecp_nistz256_mul_montq // p256_mul_mont(H, H, R); leaq 32(%rsp),%rbx leaq 256(%rsp),%rdi - call __ecp_nistz256_sub_fromq + call __ecp_nistz256_sub_fromq // p256_sub(res_y, H, S2); - movq %xmm0,%rdi + movq %xmm0,%rdi // restore %rdi - movdqa %xmm5,%xmm0 + movdqa %xmm5,%xmm0 // copy_conditional(res_z, ONE, in1infty); movdqa %xmm5,%xmm1 pandn 288(%rsp),%xmm0 movdqa %xmm5,%xmm2 @@ -3387,7 +3388,7 @@ _CET_ENDBR por %xmm0,%xmm2 por %xmm1,%xmm3 - movdqa %xmm4,%xmm0 + movdqa %xmm4,%xmm0 // copy_conditional(res_z, in1_z, in2infty); movdqa %xmm4,%xmm1 pandn %xmm2,%xmm0 movdqa %xmm4,%xmm2 @@ -3400,7 +3401,7 @@ _CET_ENDBR movdqu %xmm2,64(%rdi) movdqu %xmm3,80(%rdi) - movdqa %xmm5,%xmm0 + movdqa %xmm5,%xmm0 // copy_conditional(res_x, in2_x, in1infty); movdqa %xmm5,%xmm1 pandn 224(%rsp),%xmm0 movdqa %xmm5,%xmm2 @@ -3411,7 +3412,7 @@ _CET_ENDBR por %xmm0,%xmm2 por %xmm1,%xmm3 - movdqa %xmm4,%xmm0 + movdqa %xmm4,%xmm0 // copy_conditional(res_x, in1_x, in2infty); movdqa %xmm4,%xmm1 pandn %xmm2,%xmm0 movdqa %xmm4,%xmm2 @@ -3424,7 +3425,7 @@ _CET_ENDBR movdqu %xmm2,0(%rdi) movdqu %xmm3,16(%rdi) - movdqa %xmm5,%xmm0 + movdqa %xmm5,%xmm0 // copy_conditional(res_y, in2_y, in1infty); movdqa %xmm5,%xmm1 pandn 256(%rsp),%xmm0 movdqa %xmm5,%xmm2 @@ -3435,7 +3436,7 @@ _CET_ENDBR por %xmm0,%xmm2 por %xmm1,%xmm3 - movdqa %xmm4,%xmm0 + movdqa %xmm4,%xmm0 // copy_conditional(res_y, in1_y, in2infty); movdqa %xmm4,%xmm1 pandn %xmm2,%xmm0 movdqa %xmm4,%xmm2 @@ -3574,7 +3575,7 @@ __ecp_nistz256_subx: __ecp_nistz256_mul_by_2x: .cfi_startproc xorq %r11,%r11 - adcq %r12,%r12 + adcq %r12,%r12 // a0:a3+a0:a3 adcq %r13,%r13 movq %r12,%rax adcq %r8,%r8 @@ -3633,10 +3634,10 @@ _CET_ENDBR .Lpoint_doublex_body: .Lpoint_double_shortcutx: - movdqu 0(%rsi),%xmm0 - movq %rsi,%rbx + movdqu 0(%rsi),%xmm0 // copy *(P256_POINT *)%rsi.x + movq %rsi,%rbx // backup copy movdqu 16(%rsi),%xmm1 - movq 32+0(%rsi),%r12 + movq 32+0(%rsi),%r12 // load in_y in "5-4-0-1" order movq 32+8(%rsi),%r13 movq 32+16(%rsi),%r8 movq 32+24(%rsi),%r9 @@ -3651,7 +3652,7 @@ _CET_ENDBR movq %r11,%xmm2 leaq 0(%rsp),%rdi - call __ecp_nistz256_mul_by_2x + call __ecp_nistz256_mul_by_2x // p256_mul_by_2(S, in_y); movq 64+0(%rsi),%rdx movq 64+8(%rsi),%r14 @@ -3659,7 +3660,7 @@ _CET_ENDBR movq 64+24(%rsi),%r8 leaq 64-128(%rsi),%rsi leaq 64(%rsp),%rdi - call __ecp_nistz256_sqr_montx + call __ecp_nistz256_sqr_montx // p256_sqr_mont(Zsqr, in_z); movq 0+0(%rsp),%rdx movq 8+0(%rsp),%r14 @@ -3667,9 +3668,9 @@ _CET_ENDBR movq 16+0(%rsp),%r15 movq 24+0(%rsp),%r8 leaq 0(%rsp),%rdi - call __ecp_nistz256_sqr_montx + call __ecp_nistz256_sqr_montx // p256_sqr_mont(S, S); - movq 32(%rbx),%rdx + movq 32(%rbx),%rdx // %rbx is still valid movq 64+0(%rbx),%r9 movq 64+8(%rbx),%r10 movq 64+16(%rbx),%r11 @@ -3677,24 +3678,24 @@ _CET_ENDBR leaq 64-128(%rbx),%rsi leaq 32(%rbx),%rbx movq %xmm2,%rdi - call __ecp_nistz256_mul_montx - call __ecp_nistz256_mul_by_2x + call __ecp_nistz256_mul_montx // p256_mul_mont(res_z, in_z, in_y); + call __ecp_nistz256_mul_by_2x // p256_mul_by_2(res_z, res_z); - movq 96+0(%rsp),%r12 + movq 96+0(%rsp),%r12 // "5-4-0-1" order movq 96+8(%rsp),%r13 leaq 64(%rsp),%rbx movq 96+16(%rsp),%r8 movq 96+24(%rsp),%r9 leaq 32(%rsp),%rdi - call __ecp_nistz256_add_tox + call __ecp_nistz256_add_tox // p256_add(M, in_x, Zsqr); - movq 96+0(%rsp),%r12 + movq 96+0(%rsp),%r12 // "5-4-0-1" order movq 96+8(%rsp),%r13 leaq 64(%rsp),%rbx movq 96+16(%rsp),%r8 movq 96+24(%rsp),%r9 leaq 64(%rsp),%rdi - call __ecp_nistz256_sub_fromx + call __ecp_nistz256_sub_fromx // p256_sub(Zsqr, in_x, Zsqr); movq 0+0(%rsp),%rdx movq 8+0(%rsp),%r14 @@ -3702,7 +3703,7 @@ _CET_ENDBR movq 16+0(%rsp),%r15 movq 24+0(%rsp),%r8 movq %xmm1,%rdi - call __ecp_nistz256_sqr_montx + call __ecp_nistz256_sqr_montx // p256_sqr_mont(res_y, S); xorq %r9,%r9 movq %r12,%rax addq $-1,%r12 @@ -3713,7 +3714,7 @@ _CET_ENDBR movq %r15,%r8 adcq %rbp,%r15 adcq $0,%r9 - xorq %rsi,%rsi + xorq %rsi,%rsi // borrow %rsi testq $1,%rax cmovzq %rax,%r12 @@ -3722,7 +3723,7 @@ _CET_ENDBR cmovzq %r8,%r15 cmovzq %rsi,%r9 - movq %r13,%rax + movq %r13,%rax // a0:a3>>1 shrq $1,%r12 shlq $63,%rax movq %r14,%r10 @@ -3749,14 +3750,14 @@ _CET_ENDBR movq 16+32(%rsp),%r11 movq 24+32(%rsp),%r12 leaq 32(%rsp),%rdi - call __ecp_nistz256_mul_montx + call __ecp_nistz256_mul_montx // p256_mul_mont(M, M, Zsqr); leaq 128(%rsp),%rdi call __ecp_nistz256_mul_by_2x leaq 32(%rsp),%rbx leaq 32(%rsp),%rdi - call __ecp_nistz256_add_tox + call __ecp_nistz256_add_tox // p256_mul_by_3(M, M); movq 96(%rsp),%rdx leaq 96(%rsp),%rbx @@ -3766,10 +3767,10 @@ _CET_ENDBR movq 16+0(%rsp),%r11 movq 24+0(%rsp),%r12 leaq 0(%rsp),%rdi - call __ecp_nistz256_mul_montx + call __ecp_nistz256_mul_montx // p256_mul_mont(S, S, in_x); leaq 128(%rsp),%rdi - call __ecp_nistz256_mul_by_2x + call __ecp_nistz256_mul_by_2x // p256_mul_by_2(tmp0, S); movq 0+32(%rsp),%rdx movq 8+32(%rsp),%r14 @@ -3777,27 +3778,27 @@ _CET_ENDBR movq 16+32(%rsp),%r15 movq 24+32(%rsp),%r8 movq %xmm0,%rdi - call __ecp_nistz256_sqr_montx + call __ecp_nistz256_sqr_montx // p256_sqr_mont(res_x, M); leaq 128(%rsp),%rbx - movq %r14,%r8 + movq %r14,%r8 // harmonize sqr output and sub input movq %r15,%r9 movq %rsi,%r14 movq %rbp,%r15 - call __ecp_nistz256_sub_fromx + call __ecp_nistz256_sub_fromx // p256_sub(res_x, res_x, tmp0); movq 0+0(%rsp),%rax movq 0+8(%rsp),%rbp movq 0+16(%rsp),%rcx - movq 0+24(%rsp),%r10 + movq 0+24(%rsp),%r10 // "4-5-0-1" order leaq 0(%rsp),%rdi - call __ecp_nistz256_subx + call __ecp_nistz256_subx // p256_sub(S, S, res_x); movq 32(%rsp),%rdx leaq 32(%rsp),%rbx - movq %r12,%r14 + movq %r12,%r14 // harmonize sub output and mul input xorl %ecx,%ecx - movq %r12,0+0(%rsp) + movq %r12,0+0(%rsp) // have to save:-( movq %r13,%r10 movq %r13,0+8(%rsp) cmovzq %r8,%r11 @@ -3807,11 +3808,11 @@ _CET_ENDBR movq %r9,0+24(%rsp) movq %r14,%r9 leaq 0(%rsp),%rdi - call __ecp_nistz256_mul_montx + call __ecp_nistz256_mul_montx // p256_mul_mont(S, S, M); movq %xmm1,%rbx movq %xmm1,%rdi - call __ecp_nistz256_sub_fromx + call __ecp_nistz256_sub_fromx // p256_sub(res_y, S, res_y); leaq 160+56(%rsp),%rsi .cfi_def_cfa %rsi,8 @@ -3862,14 +3863,14 @@ _CET_ENDBR .cfi_adjust_cfa_offset 32*18+8 .Lpoint_addx_body: - movdqu 0(%rsi),%xmm0 + movdqu 0(%rsi),%xmm0 // copy *(P256_POINT *)%rsi movdqu 16(%rsi),%xmm1 movdqu 32(%rsi),%xmm2 movdqu 48(%rsi),%xmm3 movdqu 64(%rsi),%xmm4 movdqu 80(%rsi),%xmm5 - movq %rsi,%rbx - movq %rdx,%rsi + movq %rsi,%rbx // reassign + movq %rdx,%rsi // reassign movdqa %xmm0,384(%rsp) movdqa %xmm1,384+16(%rsp) movdqa %xmm2,416(%rsp) @@ -3878,54 +3879,54 @@ _CET_ENDBR movdqa %xmm5,448+16(%rsp) por %xmm4,%xmm5 - movdqu 0(%rsi),%xmm0 + movdqu 0(%rsi),%xmm0 // copy *(P256_POINT *)%rbx pshufd $0xb1,%xmm5,%xmm3 movdqu 16(%rsi),%xmm1 movdqu 32(%rsi),%xmm2 por %xmm3,%xmm5 movdqu 48(%rsi),%xmm3 - movq 64+0(%rsi),%rdx + movq 64+0(%rsi),%rdx // load original in2_z movq 64+8(%rsi),%r14 movq 64+16(%rsi),%r15 movq 64+24(%rsi),%r8 movdqa %xmm0,480(%rsp) pshufd $0x1e,%xmm5,%xmm4 movdqa %xmm1,480+16(%rsp) - movdqu 64(%rsi),%xmm0 + movdqu 64(%rsi),%xmm0 // in2_z again movdqu 80(%rsi),%xmm1 movdqa %xmm2,512(%rsp) movdqa %xmm3,512+16(%rsp) por %xmm4,%xmm5 pxor %xmm4,%xmm4 por %xmm0,%xmm1 - movq %rdi,%xmm0 + movq %rdi,%xmm0 // save %rdi - leaq 64-128(%rsi),%rsi - movq %rdx,544+0(%rsp) + leaq 64-128(%rsi),%rsi // %rsi is still valid + movq %rdx,544+0(%rsp) // make in2_z copy movq %r14,544+8(%rsp) movq %r15,544+16(%rsp) movq %r8,544+24(%rsp) - leaq 96(%rsp),%rdi - call __ecp_nistz256_sqr_montx + leaq 96(%rsp),%rdi // Z2^2 + call __ecp_nistz256_sqr_montx // p256_sqr_mont(Z2sqr, in2_z); pcmpeqd %xmm4,%xmm5 pshufd $0xb1,%xmm1,%xmm4 por %xmm1,%xmm4 - pshufd $0,%xmm5,%xmm5 + pshufd $0,%xmm5,%xmm5 // in1infty pshufd $0x1e,%xmm4,%xmm3 por %xmm3,%xmm4 pxor %xmm3,%xmm3 pcmpeqd %xmm3,%xmm4 - pshufd $0,%xmm4,%xmm4 - movq 64+0(%rbx),%rdx + pshufd $0,%xmm4,%xmm4 // in2infty + movq 64+0(%rbx),%rdx // load original in1_z movq 64+8(%rbx),%r14 movq 64+16(%rbx),%r15 movq 64+24(%rbx),%r8 movq %rbx,%xmm1 leaq 64-128(%rbx),%rsi - leaq 32(%rsp),%rdi - call __ecp_nistz256_sqr_montx + leaq 32(%rsp),%rdi // Z1^2 + call __ecp_nistz256_sqr_montx // p256_sqr_mont(Z1sqr, in1_z); movq 544(%rsp),%rdx leaq 544(%rsp),%rbx @@ -3934,8 +3935,8 @@ _CET_ENDBR leaq -128+96(%rsp),%rsi movq 16+96(%rsp),%r11 movq 24+96(%rsp),%r12 - leaq 224(%rsp),%rdi - call __ecp_nistz256_mul_montx + leaq 224(%rsp),%rdi // S1 = Z2^3 + call __ecp_nistz256_mul_montx // p256_mul_mont(S1, Z2sqr, in2_z); movq 448(%rsp),%rdx leaq 448(%rsp),%rbx @@ -3944,8 +3945,8 @@ _CET_ENDBR leaq -128+32(%rsp),%rsi movq 16+32(%rsp),%r11 movq 24+32(%rsp),%r12 - leaq 256(%rsp),%rdi - call __ecp_nistz256_mul_montx + leaq 256(%rsp),%rdi // S2 = Z1^3 + call __ecp_nistz256_mul_montx // p256_mul_mont(S2, Z1sqr, in1_z); movq 416(%rsp),%rdx leaq 416(%rsp),%rbx @@ -3954,8 +3955,8 @@ _CET_ENDBR leaq -128+224(%rsp),%rsi movq 16+224(%rsp),%r11 movq 24+224(%rsp),%r12 - leaq 224(%rsp),%rdi - call __ecp_nistz256_mul_montx + leaq 224(%rsp),%rdi // S1 = Y1*Z2^3 + call __ecp_nistz256_mul_montx // p256_mul_mont(S1, S1, in1_y); movq 512(%rsp),%rdx leaq 512(%rsp),%rbx @@ -3964,18 +3965,18 @@ _CET_ENDBR leaq -128+256(%rsp),%rsi movq 16+256(%rsp),%r11 movq 24+256(%rsp),%r12 - leaq 256(%rsp),%rdi - call __ecp_nistz256_mul_montx + leaq 256(%rsp),%rdi // S2 = Y2*Z1^3 + call __ecp_nistz256_mul_montx // p256_mul_mont(S2, S2, in2_y); leaq 224(%rsp),%rbx - leaq 64(%rsp),%rdi - call __ecp_nistz256_sub_fromx + leaq 64(%rsp),%rdi // R = S2 - S1 + call __ecp_nistz256_sub_fromx // p256_sub(R, S2, S1); - orq %r13,%r12 + orq %r13,%r12 // see if result is zero movdqa %xmm4,%xmm2 orq %r8,%r12 orq %r9,%r12 - por %xmm5,%xmm2 + por %xmm5,%xmm2 // in1infty || in2infty movq %r12,%xmm3 movq 384(%rsp),%rdx @@ -3985,8 +3986,8 @@ _CET_ENDBR leaq -128+96(%rsp),%rsi movq 16+96(%rsp),%r11 movq 24+96(%rsp),%r12 - leaq 160(%rsp),%rdi - call __ecp_nistz256_mul_montx + leaq 160(%rsp),%rdi // U1 = X1*Z2^2 + call __ecp_nistz256_mul_montx // p256_mul_mont(U1, in1_x, Z2sqr); movq 480(%rsp),%rdx leaq 480(%rsp),%rbx @@ -3995,34 +3996,34 @@ _CET_ENDBR leaq -128+32(%rsp),%rsi movq 16+32(%rsp),%r11 movq 24+32(%rsp),%r12 - leaq 192(%rsp),%rdi - call __ecp_nistz256_mul_montx + leaq 192(%rsp),%rdi // U2 = X2*Z1^2 + call __ecp_nistz256_mul_montx // p256_mul_mont(U2, in2_x, Z1sqr); leaq 160(%rsp),%rbx - leaq 0(%rsp),%rdi - call __ecp_nistz256_sub_fromx + leaq 0(%rsp),%rdi // H = U2 - U1 + call __ecp_nistz256_sub_fromx // p256_sub(H, U2, U1); - orq %r13,%r12 + orq %r13,%r12 // see if result is zero orq %r8,%r12 - orq %r9,%r12 + orq %r9,%r12 // movq %xmm2,%r8 movq %xmm3,%r9 orq %r8,%r12 -.byte 0x3e - jnz .Ladd_proceedx - - +.byte 0x3e // predict taken + jnz .Ladd_proceedx // +// We now know A = B or A = -B and neither is infinity. Compare the +// y-coordinates via S1 and S2. testq %r9,%r9 - jz .Ladd_doublex - - - - - - - movq %xmm0,%rdi + jz .Ladd_doublex // is_equal(S1, S2) + +// A = -B, so the result is infinity. +// +// TODO(davidben): Does .Ladd_proceed handle this case? It seems to, in +// which case we should eliminate this special-case and simplify the +// timing analysis. + movq %xmm0,%rdi // restore %rdi pxor %xmm0,%xmm0 movdqu %xmm0,0(%rdi) movdqu %xmm0,16(%rdi) @@ -4034,9 +4035,9 @@ _CET_ENDBR .align 32 .Ladd_doublex: - movq %xmm1,%rsi - movq %xmm0,%rdi - addq $416,%rsp + movq %xmm1,%rsi // restore %rsi + movq %xmm0,%rdi // restore %rdi + addq $416,%rsp // difference in frame sizes .cfi_adjust_cfa_offset -416 jmp .Lpoint_double_shortcutx .cfi_adjust_cfa_offset 416 @@ -4048,8 +4049,8 @@ _CET_ENDBR leaq -128+64(%rsp),%rsi movq 16+64(%rsp),%r15 movq 24+64(%rsp),%r8 - leaq 96(%rsp),%rdi - call __ecp_nistz256_sqr_montx + leaq 96(%rsp),%rdi // R^2 + call __ecp_nistz256_sqr_montx // p256_sqr_mont(Rsqr, R); movq 448(%rsp),%rdx leaq 448(%rsp),%rbx @@ -4058,16 +4059,16 @@ _CET_ENDBR leaq -128+0(%rsp),%rsi movq 16+0(%rsp),%r11 movq 24+0(%rsp),%r12 - leaq 352(%rsp),%rdi - call __ecp_nistz256_mul_montx + leaq 352(%rsp),%rdi // Z3 = H*Z1*Z2 + call __ecp_nistz256_mul_montx // p256_mul_mont(res_z, H, in1_z); movq 0+0(%rsp),%rdx movq 8+0(%rsp),%r14 leaq -128+0(%rsp),%rsi movq 16+0(%rsp),%r15 movq 24+0(%rsp),%r8 - leaq 32(%rsp),%rdi - call __ecp_nistz256_sqr_montx + leaq 32(%rsp),%rdi // H^2 + call __ecp_nistz256_sqr_montx // p256_sqr_mont(Hsqr, H); movq 544(%rsp),%rdx leaq 544(%rsp),%rbx @@ -4076,8 +4077,8 @@ _CET_ENDBR leaq -128+352(%rsp),%rsi movq 16+352(%rsp),%r11 movq 24+352(%rsp),%r12 - leaq 352(%rsp),%rdi - call __ecp_nistz256_mul_montx + leaq 352(%rsp),%rdi // Z3 = H*Z1*Z2 + call __ecp_nistz256_mul_montx // p256_mul_mont(res_z, res_z, in2_z); movq 0(%rsp),%rdx leaq 0(%rsp),%rbx @@ -4086,8 +4087,8 @@ _CET_ENDBR leaq -128+32(%rsp),%rsi movq 16+32(%rsp),%r11 movq 24+32(%rsp),%r12 - leaq 128(%rsp),%rdi - call __ecp_nistz256_mul_montx + leaq 128(%rsp),%rdi // H^3 + call __ecp_nistz256_mul_montx // p256_mul_mont(Hcub, Hsqr, H); movq 160(%rsp),%rdx leaq 160(%rsp),%rbx @@ -4096,14 +4097,14 @@ _CET_ENDBR leaq -128+32(%rsp),%rsi movq 16+32(%rsp),%r11 movq 24+32(%rsp),%r12 - leaq 192(%rsp),%rdi - call __ecp_nistz256_mul_montx - - - + leaq 192(%rsp),%rdi // U1*H^2 + call __ecp_nistz256_mul_montx // p256_mul_mont(U2, U1, Hsqr); +// lea 192(%rsp), %rsi +// lea 32(%rsp), %rdi # 2*U1*H^2 +// call __ecp_nistz256_mul_by_2 # ecp_nistz256_mul_by_2(Hsqr, U2); xorq %r11,%r11 - addq %r12,%r12 + addq %r12,%r12 // a0:a3+a0:a3 leaq 96(%rsp),%rsi adcq %r13,%r13 movq %r12,%rax @@ -4129,11 +4130,11 @@ _CET_ENDBR cmovcq %r10,%r9 movq 24(%rsi),%r10 - call __ecp_nistz256_subx + call __ecp_nistz256_subx // p256_sub(res_x, Rsqr, Hsqr); leaq 128(%rsp),%rbx leaq 288(%rsp),%rdi - call __ecp_nistz256_sub_fromx + call __ecp_nistz256_sub_fromx // p256_sub(res_x, res_x, Hcub); movq 192+0(%rsp),%rax movq 192+8(%rsp),%rbp @@ -4141,10 +4142,10 @@ _CET_ENDBR movq 192+24(%rsp),%r10 leaq 320(%rsp),%rdi - call __ecp_nistz256_subx + call __ecp_nistz256_subx // p256_sub(res_y, U2, res_x); - movq %r12,0(%rdi) - movq %r13,8(%rdi) + movq %r12,0(%rdi) // save the result, as + movq %r13,8(%rdi) // __ecp_nistz256_sub doesn't movq %r8,16(%rdi) movq %r9,24(%rdi) movq 128(%rsp),%rdx @@ -4155,7 +4156,7 @@ _CET_ENDBR movq 16+224(%rsp),%r11 movq 24+224(%rsp),%r12 leaq 256(%rsp),%rdi - call __ecp_nistz256_mul_montx + call __ecp_nistz256_mul_montx // p256_mul_mont(S2, S1, Hcub); movq 320(%rsp),%rdx leaq 320(%rsp),%rbx @@ -4165,15 +4166,15 @@ _CET_ENDBR movq 16+64(%rsp),%r11 movq 24+64(%rsp),%r12 leaq 320(%rsp),%rdi - call __ecp_nistz256_mul_montx + call __ecp_nistz256_mul_montx // p256_mul_mont(res_y, R, res_y); leaq 256(%rsp),%rbx leaq 320(%rsp),%rdi - call __ecp_nistz256_sub_fromx + call __ecp_nistz256_sub_fromx // p256_sub(res_y, res_y, S2); - movq %xmm0,%rdi + movq %xmm0,%rdi // restore %rdi - movdqa %xmm5,%xmm0 + movdqa %xmm5,%xmm0 // copy_conditional(res_z, in2_z, in1infty); movdqa %xmm5,%xmm1 pandn 352(%rsp),%xmm0 movdqa %xmm5,%xmm2 @@ -4184,7 +4185,7 @@ _CET_ENDBR por %xmm0,%xmm2 por %xmm1,%xmm3 - movdqa %xmm4,%xmm0 + movdqa %xmm4,%xmm0 // copy_conditional(res_z, in1_z, in2infty); movdqa %xmm4,%xmm1 pandn %xmm2,%xmm0 movdqa %xmm4,%xmm2 @@ -4197,7 +4198,7 @@ _CET_ENDBR movdqu %xmm2,64(%rdi) movdqu %xmm3,80(%rdi) - movdqa %xmm5,%xmm0 + movdqa %xmm5,%xmm0 // copy_conditional(res_x, in2_x, in1infty); movdqa %xmm5,%xmm1 pandn 288(%rsp),%xmm0 movdqa %xmm5,%xmm2 @@ -4208,7 +4209,7 @@ _CET_ENDBR por %xmm0,%xmm2 por %xmm1,%xmm3 - movdqa %xmm4,%xmm0 + movdqa %xmm4,%xmm0 // copy_conditional(res_x, in1_x, in2infty); movdqa %xmm4,%xmm1 pandn %xmm2,%xmm0 movdqa %xmm4,%xmm2 @@ -4221,7 +4222,7 @@ _CET_ENDBR movdqu %xmm2,0(%rdi) movdqu %xmm3,16(%rdi) - movdqa %xmm5,%xmm0 + movdqa %xmm5,%xmm0 // copy_conditional(res_y, in2_y, in1infty); movdqa %xmm5,%xmm1 pandn 320(%rsp),%xmm0 movdqa %xmm5,%xmm2 @@ -4232,7 +4233,7 @@ _CET_ENDBR por %xmm0,%xmm2 por %xmm1,%xmm3 - movdqa %xmm4,%xmm0 + movdqa %xmm4,%xmm0 // copy_conditional(res_y, in1_y, in2infty); movdqa %xmm4,%xmm1 pandn %xmm2,%xmm0 movdqa %xmm4,%xmm2 @@ -4295,14 +4296,14 @@ _CET_ENDBR .cfi_adjust_cfa_offset 32*15+8 .Ladd_affinex_body: - movdqu 0(%rsi),%xmm0 - movq %rdx,%rbx + movdqu 0(%rsi),%xmm0 // copy *(P256_POINT *)%rsi + movq %rdx,%rbx // reassign movdqu 16(%rsi),%xmm1 movdqu 32(%rsi),%xmm2 movdqu 48(%rsi),%xmm3 movdqu 64(%rsi),%xmm4 movdqu 80(%rsi),%xmm5 - movq 64+0(%rsi),%rdx + movq 64+0(%rsi),%rdx // load original in1_z movq 64+8(%rsi),%r14 movq 64+16(%rsi),%r15 movq 64+24(%rsi),%r8 @@ -4314,7 +4315,7 @@ _CET_ENDBR movdqa %xmm5,384+16(%rsp) por %xmm4,%xmm5 - movdqu 0(%rbx),%xmm0 + movdqu 0(%rbx),%xmm0 // copy *(P256_POINT_AFFINE *)%rbx pshufd $0xb1,%xmm5,%xmm3 movdqu 16(%rbx),%xmm1 movdqu 32(%rbx),%xmm2 @@ -4324,7 +4325,7 @@ _CET_ENDBR pshufd $0x1e,%xmm5,%xmm4 movdqa %xmm1,416+16(%rsp) por %xmm0,%xmm1 - movq %rdi,%xmm0 + movq %rdi,%xmm0 // save %rdi movdqa %xmm2,448(%rsp) movdqa %xmm3,448+16(%rsp) por %xmm2,%xmm3 @@ -4332,33 +4333,33 @@ _CET_ENDBR pxor %xmm4,%xmm4 por %xmm1,%xmm3 - leaq 64-128(%rsi),%rsi - leaq 32(%rsp),%rdi - call __ecp_nistz256_sqr_montx + leaq 64-128(%rsi),%rsi // %rsi is still valid + leaq 32(%rsp),%rdi // Z1^2 + call __ecp_nistz256_sqr_montx // p256_sqr_mont(Z1sqr, in1_z); pcmpeqd %xmm4,%xmm5 pshufd $0xb1,%xmm3,%xmm4 - movq 0(%rbx),%rdx - - movq %r12,%r9 + movq 0(%rbx),%rdx // %rbx is still valid +// lea 0x00(%rbx), %rbx + movq %r12,%r9 // harmonize sqr output and mul input por %xmm3,%xmm4 - pshufd $0,%xmm5,%xmm5 + pshufd $0,%xmm5,%xmm5 // in1infty pshufd $0x1e,%xmm4,%xmm3 movq %r13,%r10 por %xmm3,%xmm4 pxor %xmm3,%xmm3 movq %r14,%r11 pcmpeqd %xmm3,%xmm4 - pshufd $0,%xmm4,%xmm4 + pshufd $0,%xmm4,%xmm4 // in2infty leaq 32-128(%rsp),%rsi movq %r15,%r12 - leaq 0(%rsp),%rdi - call __ecp_nistz256_mul_montx + leaq 0(%rsp),%rdi // U2 = X2*Z1^2 + call __ecp_nistz256_mul_montx // p256_mul_mont(U2, Z1sqr, in2_x); leaq 320(%rsp),%rbx - leaq 64(%rsp),%rdi - call __ecp_nistz256_sub_fromx + leaq 64(%rsp),%rdi // H = U2 - U1 + call __ecp_nistz256_sub_fromx // p256_sub(H, U2, in1_x); movq 384(%rsp),%rdx leaq 384(%rsp),%rbx @@ -4367,8 +4368,8 @@ _CET_ENDBR leaq -128+32(%rsp),%rsi movq 16+32(%rsp),%r11 movq 24+32(%rsp),%r12 - leaq 32(%rsp),%rdi - call __ecp_nistz256_mul_montx + leaq 32(%rsp),%rdi // S2 = Z1^3 + call __ecp_nistz256_mul_montx // p256_mul_mont(S2, Z1sqr, in1_z); movq 384(%rsp),%rdx leaq 384(%rsp),%rbx @@ -4377,8 +4378,8 @@ _CET_ENDBR leaq -128+64(%rsp),%rsi movq 16+64(%rsp),%r11 movq 24+64(%rsp),%r12 - leaq 288(%rsp),%rdi - call __ecp_nistz256_mul_montx + leaq 288(%rsp),%rdi // Z3 = H*Z1*Z2 + call __ecp_nistz256_mul_montx // p256_mul_mont(res_z, H, in1_z); movq 448(%rsp),%rdx leaq 448(%rsp),%rbx @@ -4387,28 +4388,28 @@ _CET_ENDBR leaq -128+32(%rsp),%rsi movq 16+32(%rsp),%r11 movq 24+32(%rsp),%r12 - leaq 32(%rsp),%rdi - call __ecp_nistz256_mul_montx + leaq 32(%rsp),%rdi // S2 = Y2*Z1^3 + call __ecp_nistz256_mul_montx // p256_mul_mont(S2, S2, in2_y); leaq 352(%rsp),%rbx - leaq 96(%rsp),%rdi - call __ecp_nistz256_sub_fromx + leaq 96(%rsp),%rdi // R = S2 - S1 + call __ecp_nistz256_sub_fromx // p256_sub(R, S2, in1_y); movq 0+64(%rsp),%rdx movq 8+64(%rsp),%r14 leaq -128+64(%rsp),%rsi movq 16+64(%rsp),%r15 movq 24+64(%rsp),%r8 - leaq 128(%rsp),%rdi - call __ecp_nistz256_sqr_montx + leaq 128(%rsp),%rdi // H^2 + call __ecp_nistz256_sqr_montx // p256_sqr_mont(Hsqr, H); movq 0+96(%rsp),%rdx movq 8+96(%rsp),%r14 leaq -128+96(%rsp),%rsi movq 16+96(%rsp),%r15 movq 24+96(%rsp),%r8 - leaq 192(%rsp),%rdi - call __ecp_nistz256_sqr_montx + leaq 192(%rsp),%rdi // R^2 + call __ecp_nistz256_sqr_montx // p256_sqr_mont(Rsqr, R); movq 128(%rsp),%rdx leaq 128(%rsp),%rbx @@ -4417,8 +4418,8 @@ _CET_ENDBR leaq -128+64(%rsp),%rsi movq 16+64(%rsp),%r11 movq 24+64(%rsp),%r12 - leaq 160(%rsp),%rdi - call __ecp_nistz256_mul_montx + leaq 160(%rsp),%rdi // H^3 + call __ecp_nistz256_mul_montx // p256_mul_mont(Hcub, Hsqr, H); movq 320(%rsp),%rdx leaq 320(%rsp),%rbx @@ -4427,14 +4428,14 @@ _CET_ENDBR leaq -128+128(%rsp),%rsi movq 16+128(%rsp),%r11 movq 24+128(%rsp),%r12 - leaq 0(%rsp),%rdi - call __ecp_nistz256_mul_montx - - - + leaq 0(%rsp),%rdi // U1*H^2 + call __ecp_nistz256_mul_montx // p256_mul_mont(U2, in1_x, Hsqr); +// lea 0(%rsp), %rsi +// lea 128(%rsp), %rdi # 2*U1*H^2 +// call __ecp_nistz256_mul_by_2 # ecp_nistz256_mul_by_2(Hsqr, U2); xorq %r11,%r11 - addq %r12,%r12 + addq %r12,%r12 // a0:a3+a0:a3 leaq 192(%rsp),%rsi adcq %r13,%r13 movq %r12,%rax @@ -4460,11 +4461,11 @@ _CET_ENDBR cmovcq %r10,%r9 movq 24(%rsi),%r10 - call __ecp_nistz256_subx + call __ecp_nistz256_subx // p256_sub(res_x, Rsqr, Hsqr); leaq 160(%rsp),%rbx leaq 224(%rsp),%rdi - call __ecp_nistz256_sub_fromx + call __ecp_nistz256_sub_fromx // p256_sub(res_x, res_x, Hcub); movq 0+0(%rsp),%rax movq 0+8(%rsp),%rbp @@ -4472,10 +4473,10 @@ _CET_ENDBR movq 0+24(%rsp),%r10 leaq 64(%rsp),%rdi - call __ecp_nistz256_subx + call __ecp_nistz256_subx // p256_sub(H, U2, res_x); - movq %r12,0(%rdi) - movq %r13,8(%rdi) + movq %r12,0(%rdi) // save the result, as + movq %r13,8(%rdi) // __ecp_nistz256_sub doesn't movq %r8,16(%rdi) movq %r9,24(%rdi) movq 352(%rsp),%rdx @@ -4486,7 +4487,7 @@ _CET_ENDBR movq 16+160(%rsp),%r11 movq 24+160(%rsp),%r12 leaq 32(%rsp),%rdi - call __ecp_nistz256_mul_montx + call __ecp_nistz256_mul_montx // p256_mul_mont(S2, Hcub, in1_y); movq 96(%rsp),%rdx leaq 96(%rsp),%rbx @@ -4496,15 +4497,15 @@ _CET_ENDBR movq 16+64(%rsp),%r11 movq 24+64(%rsp),%r12 leaq 64(%rsp),%rdi - call __ecp_nistz256_mul_montx + call __ecp_nistz256_mul_montx // p256_mul_mont(H, H, R); leaq 32(%rsp),%rbx leaq 256(%rsp),%rdi - call __ecp_nistz256_sub_fromx + call __ecp_nistz256_sub_fromx // p256_sub(res_y, H, S2); - movq %xmm0,%rdi + movq %xmm0,%rdi // restore %rdi - movdqa %xmm5,%xmm0 + movdqa %xmm5,%xmm0 // copy_conditional(res_z, ONE, in1infty); movdqa %xmm5,%xmm1 pandn 288(%rsp),%xmm0 movdqa %xmm5,%xmm2 @@ -4515,7 +4516,7 @@ _CET_ENDBR por %xmm0,%xmm2 por %xmm1,%xmm3 - movdqa %xmm4,%xmm0 + movdqa %xmm4,%xmm0 // copy_conditional(res_z, in1_z, in2infty); movdqa %xmm4,%xmm1 pandn %xmm2,%xmm0 movdqa %xmm4,%xmm2 @@ -4528,7 +4529,7 @@ _CET_ENDBR movdqu %xmm2,64(%rdi) movdqu %xmm3,80(%rdi) - movdqa %xmm5,%xmm0 + movdqa %xmm5,%xmm0 // copy_conditional(res_x, in2_x, in1infty); movdqa %xmm5,%xmm1 pandn 224(%rsp),%xmm0 movdqa %xmm5,%xmm2 @@ -4539,7 +4540,7 @@ _CET_ENDBR por %xmm0,%xmm2 por %xmm1,%xmm3 - movdqa %xmm4,%xmm0 + movdqa %xmm4,%xmm0 // copy_conditional(res_x, in1_x, in2infty); movdqa %xmm4,%xmm1 pandn %xmm2,%xmm0 movdqa %xmm4,%xmm2 @@ -4552,7 +4553,7 @@ _CET_ENDBR movdqu %xmm2,0(%rdi) movdqu %xmm3,16(%rdi) - movdqa %xmm5,%xmm0 + movdqa %xmm5,%xmm0 // copy_conditional(res_y, in2_y, in1infty); movdqa %xmm5,%xmm1 pandn 256(%rsp),%xmm0 movdqa %xmm5,%xmm2 @@ -4563,7 +4564,7 @@ _CET_ENDBR por %xmm0,%xmm2 por %xmm1,%xmm3 - movdqa %xmm4,%xmm0 + movdqa %xmm4,%xmm0 // copy_conditional(res_y, in1_y, in2infty); movdqa %xmm4,%xmm1 pandn %xmm2,%xmm0 movdqa %xmm4,%xmm2 diff --git a/third_party/boringssl/gen/bcm/p256-x86_64-asm-win.asm b/third_party/boringssl/gen/bcm/p256-x86_64-asm-win.asm index 729e8ecef..c82ea790f 100644 --- a/third_party/boringssl/gen/bcm/p256-x86_64-asm-win.asm +++ b/third_party/boringssl/gen/bcm/p256-x86_64-asm-win.asm @@ -14,9 +14,10 @@ default rel section .text code align=64 - +; The polynomial section .rdata rdata align=8 ALIGN 64 +p256_constants: $L$poly: DQ 0xffffffffffffffff,0x00000000ffffffff,0x0000000000000000,0xffffffff00000001 @@ -29,16 +30,16 @@ $L$Three: $L$ONE_mont: DQ 0x0000000000000001,0xffffffff00000000,0xffffffffffffffff,0x00000000fffffffe - +; Constants for computations modulo ord(p256) $L$ord: DQ 0xf3b9cac2fc632551,0xbce6faada7179e84,0xffffffffffffffff,0xffffffff00000000 $L$ordK: DQ 0xccd1c8aaee00bc4f -section .text - - +section .text code align=64 +; ############################################################################### +; void ecp_nistz256_neg(uint64_t res[4], uint64_t a[4]); global ecp_nistz256_neg ALIGN 32 @@ -103,11 +104,11 @@ $L$neg_epilogue: ret $L$SEH_end_ecp_nistz256_neg: - - - - - +; ############################################################################### +; void ecp_nistz256_ord_mul_mont( +; uint64_t res[4], +; uint64_t a[4], +; uint64_t b[4]); global ecp_nistz256_ord_mul_mont_nohw @@ -143,7 +144,7 @@ $L$ord_mul_body: lea r14,[$L$ord] mov r15,QWORD[$L$ordK] - +; ################################ * b[0] mov rcx,rax mul QWORD[rsi] mov r8,rax @@ -171,16 +172,16 @@ $L$ord_mul_body: adc rdx,0 mov r12,rdx - +; ################################ First reduction step mul QWORD[r14] mov rbp,r8 - add r13,rax + add r13,rax ; guaranteed to be zero mov rax,r8 adc rdx,0 mov rcx,rdx sub r10,r8 - sbb r8,0 + sbb r8,0 ; can't borrow mul QWORD[8+r14] add r9,rcx @@ -189,19 +190,19 @@ $L$ord_mul_body: mov rax,rbp adc r10,rdx mov rdx,rbp - adc r8,0 + adc r8,0 ; can't overflow shl rax,32 shr rdx,32 sub r11,rax mov rax,QWORD[8+rbx] - sbb rbp,rdx + sbb rbp,rdx ; can't borrow add r11,r8 adc r12,rbp adc r13,0 - +; ################################ * b[1] mov rcx,rax mul QWORD[rsi] add r9,rax @@ -237,15 +238,15 @@ $L$ord_mul_body: adc r13,rdx adc r8,0 - +; ################################ Second reduction step mul QWORD[r14] mov rbp,r9 - add rcx,rax + add rcx,rax ; guaranteed to be zero mov rax,r9 adc rcx,rdx sub r11,r9 - sbb r9,0 + sbb r9,0 ; can't borrow mul QWORD[8+r14] add r10,rcx @@ -254,19 +255,19 @@ $L$ord_mul_body: mov rax,rbp adc r11,rdx mov rdx,rbp - adc r9,0 + adc r9,0 ; can't overflow shl rax,32 shr rdx,32 sub r12,rax mov rax,QWORD[16+rbx] - sbb rbp,rdx + sbb rbp,rdx ; can't borrow add r12,r9 adc r13,rbp adc r8,0 - +; ################################# * b[2] mov rcx,rax mul QWORD[rsi] add r10,rax @@ -302,15 +303,15 @@ $L$ord_mul_body: adc r8,rdx adc r9,0 - +; ################################ Third reduction step mul QWORD[r14] mov rbp,r10 - add rcx,rax + add rcx,rax ; guaranteed to be zero mov rax,r10 adc rcx,rdx sub r12,r10 - sbb r10,0 + sbb r10,0 ; can't borrow mul QWORD[8+r14] add r11,rcx @@ -319,19 +320,19 @@ $L$ord_mul_body: mov rax,rbp adc r12,rdx mov rdx,rbp - adc r10,0 + adc r10,0 ; can't overflow shl rax,32 shr rdx,32 sub r13,rax mov rax,QWORD[24+rbx] - sbb rbp,rdx + sbb rbp,rdx ; can't borrow add r13,r10 adc r8,rbp adc r9,0 - +; ################################ * b[3] mov rcx,rax mul QWORD[rsi] add r11,rax @@ -367,15 +368,15 @@ $L$ord_mul_body: adc r9,rdx adc r10,0 - +; ################################ Last reduction step mul QWORD[r14] mov rbp,r11 - add rcx,rax + add rcx,rax ; guaranteed to be zero mov rax,r11 adc rcx,rdx sub r13,r11 - sbb r11,0 + sbb r11,0 ; can't borrow mul QWORD[8+r14] add r12,rcx @@ -384,18 +385,18 @@ $L$ord_mul_body: mov rax,rbp adc r13,rdx mov rdx,rbp - adc r11,0 + adc r11,0 ; can't overflow shl rax,32 shr rdx,32 sub r8,rax - sbb rbp,rdx + sbb rbp,rdx ; can't borrow add r8,r11 adc r9,rbp adc r10,0 - +; ################################ Subtract ord mov rsi,r12 sub r12,QWORD[r14] mov r11,r13 @@ -437,11 +438,11 @@ $L$ord_mul_epilogue: $L$SEH_end_ecp_nistz256_ord_mul_mont_nohw: - - - - - +; ############################################################################### +; void ecp_nistz256_ord_sqr_mont( +; uint64_t res[4], +; uint64_t a[4], +; uint64_t rep); global ecp_nistz256_ord_sqr_mont_nohw @@ -476,56 +477,56 @@ $L$ord_sqr_body: mov rax,QWORD[8+rsi] mov r14,QWORD[16+rsi] mov r15,QWORD[24+rsi] - lea rsi,[$L$ord] + lea rsi,[$L$ord] ; pointer to modulus mov rbx,rdx jmp NEAR $L$oop_ord_sqr ALIGN 32 $L$oop_ord_sqr: - - mov rbp,rax - mul r8 +; ################################ a[1:] * a[0] + mov rbp,rax ; put aside a[1] + mul r8 ; a[1] * a[0] mov r9,rax - movq xmm1,rbp + movq xmm1,rbp ; offload a[1] mov rax,r14 mov r10,rdx - mul r8 + mul r8 ; a[2] * a[0] add r10,rax mov rax,r15 - movq xmm2,r14 + movq xmm2,r14 ; offload a[2] adc rdx,0 mov r11,rdx - mul r8 + mul r8 ; a[3] * a[0] add r11,rax mov rax,r15 - movq xmm3,r15 + movq xmm3,r15 ; offload a[3] adc rdx,0 mov r12,rdx - - mul r14 +; ################################ a[3] * a[2] + mul r14 ; a[3] * a[2] mov r13,rax mov rax,r14 mov r14,rdx - - mul rbp +; ################################ a[2:] * a[1] + mul rbp ; a[2] * a[1] add r11,rax mov rax,r15 adc rdx,0 mov r15,rdx - mul rbp + mul rbp ; a[3] * a[1] add r12,rax adc rdx,0 add r12,r15 adc r13,rdx - adc r14,0 - + adc r14,0 ; can't overflow +; ################################ *2 xor r15,r15 mov rax,r8 add r9,r9 @@ -536,20 +537,20 @@ $L$oop_ord_sqr: adc r14,r14 adc r15,0 - - mul rax +; ################################ Missing products + mul rax ; a[0] * a[0] mov r8,rax movq rax,xmm1 mov rbp,rdx - mul rax + mul rax ; a[1] * a[1] add r9,rbp adc r10,rax movq rax,xmm2 adc rdx,0 mov rbp,rdx - mul rax + mul rax ; a[2] * a[2] add r11,rbp adc r12,rax movq rax,xmm3 @@ -557,23 +558,23 @@ $L$oop_ord_sqr: mov rbp,rdx mov rcx,r8 - imul r8,QWORD[32+rsi] + imul r8,QWORD[32+rsi] ; *= .LordK - mul rax + mul rax ; a[3] * a[3] add r13,rbp adc r14,rax - mov rax,QWORD[rsi] - adc r15,rdx - + mov rax,QWORD[rsi] ; modulus[0] + adc r15,rdx ; can't overflow +; ################################ First reduction step mul r8 mov rbp,r8 - add rcx,rax - mov rax,QWORD[8+rsi] + add rcx,rax ; guaranteed to be zero + mov rax,QWORD[8+rsi] ; modulus[1] adc rcx,rdx sub r10,r8 - sbb rbp,0 + sbb rbp,0 ; can't borrow mul r8 add r9,rcx @@ -582,29 +583,29 @@ $L$oop_ord_sqr: mov rax,r8 adc r10,rdx mov rdx,r8 - adc rbp,0 + adc rbp,0 ; can't overflow mov rcx,r9 - imul r9,QWORD[32+rsi] + imul r9,QWORD[32+rsi] ; *= .LordK shl rax,32 shr rdx,32 sub r11,rax mov rax,QWORD[rsi] - sbb r8,rdx + sbb r8,rdx ; can't borrow add r11,rbp - adc r8,0 - + adc r8,0 ; can't overflow +; ################################ Second reduction step mul r9 mov rbp,r9 - add rcx,rax + add rcx,rax ; guaranteed to be zero mov rax,QWORD[8+rsi] adc rcx,rdx sub r11,r9 - sbb rbp,0 + sbb rbp,0 ; can't borrow mul r9 add r10,rcx @@ -613,29 +614,29 @@ $L$oop_ord_sqr: mov rax,r9 adc r11,rdx mov rdx,r9 - adc rbp,0 + adc rbp,0 ; can't overflow mov rcx,r10 - imul r10,QWORD[32+rsi] + imul r10,QWORD[32+rsi] ; *= .LordK shl rax,32 shr rdx,32 sub r8,rax mov rax,QWORD[rsi] - sbb r9,rdx + sbb r9,rdx ; can't borrow add r8,rbp - adc r9,0 - + adc r9,0 ; can't overflow +; ################################ Third reduction step mul r10 mov rbp,r10 - add rcx,rax + add rcx,rax ; guaranteed to be zero mov rax,QWORD[8+rsi] adc rcx,rdx sub r8,r10 - sbb rbp,0 + sbb rbp,0 ; can't borrow mul r10 add r11,rcx @@ -644,29 +645,29 @@ $L$oop_ord_sqr: mov rax,r10 adc r8,rdx mov rdx,r10 - adc rbp,0 + adc rbp,0 ; can't overflow mov rcx,r11 - imul r11,QWORD[32+rsi] + imul r11,QWORD[32+rsi] ; *= .LordK shl rax,32 shr rdx,32 sub r9,rax mov rax,QWORD[rsi] - sbb r10,rdx + sbb r10,rdx ; can't borrow add r9,rbp - adc r10,0 - + adc r10,0 ; can't overflow +; ################################ Last reduction step mul r11 mov rbp,r11 - add rcx,rax + add rcx,rax ; guaranteed to be zero mov rax,QWORD[8+rsi] adc rcx,rdx sub r9,r11 - sbb rbp,0 + sbb rbp,0 ; can't borrow mul r11 add r8,rcx @@ -675,17 +676,17 @@ $L$oop_ord_sqr: mov rax,r11 adc r9,rdx mov rdx,r11 - adc rbp,0 + adc rbp,0 ; can't overflow shl rax,32 shr rdx,32 sub r10,rax - sbb r11,rdx + sbb r11,rdx ; can't borrow add r10,rbp - adc r11,0 - + adc r11,0 ; can't overflow +; ################################ Add bits [511:256] of the sqr result xor rdx,rdx add r8,r12 adc r9,r13 @@ -695,7 +696,7 @@ $L$oop_ord_sqr: mov rax,r9 adc rdx,0 - +; ################################ Compare to modulus sub r8,QWORD[rsi] mov r14,r10 sbb r9,QWORD[8+rsi] @@ -740,7 +741,7 @@ $L$ord_sqr_epilogue: ret $L$SEH_end_ecp_nistz256_ord_sqr_mont_nohw: - +; ############################################################################### global ecp_nistz256_ord_mul_mont_adx ALIGN 32 @@ -777,11 +778,11 @@ $L$ord_mulx_body: mov r10,QWORD[8+rsi] mov r11,QWORD[16+rsi] mov r12,QWORD[24+rsi] - lea rsi,[((-128))+rsi] + lea rsi,[((-128))+rsi] ; control u-op density lea r14,[(($L$ord-128))] mov r15,QWORD[$L$ordK] - +; ################################ Multiply by b[0] mulx r9,r8,r9 mulx r10,rcx,r10 mulx r11,rbp,r11 @@ -793,10 +794,10 @@ $L$ord_mulx_body: adc r11,rcx adc r12,0 - - xor r13,r13 +; ################################ reduction + xor r13,r13 ; %r13=0, cf=0, of=0 mulx rbp,rcx,QWORD[((0+128))+r14] - adcx r8,rcx + adcx r8,rcx ; guaranteed to be zero adox r9,rbp mulx rbp,rcx,QWORD[((8+128))+r14] @@ -813,9 +814,9 @@ $L$ord_mulx_body: adox r12,rbp adcx r12,r8 adox r13,r8 - adc r13,0 - + adc r13,0 ; cf=0, of=0 +; ################################ Multiply by b[1] mulx rbp,rcx,QWORD[((0+128))+rsi] adcx r9,rcx adox r10,rbp @@ -836,11 +837,11 @@ $L$ord_mulx_body: adcx r13,r8 adox r8,r8 - adc r8,0 - + adc r8,0 ; cf=0, of=0 +; ################################ reduction mulx rbp,rcx,QWORD[((0+128))+r14] - adcx r9,rcx + adcx r9,rcx ; guaranteed to be zero adox r10,rbp mulx rbp,rcx,QWORD[((8+128))+r14] @@ -857,9 +858,9 @@ $L$ord_mulx_body: adox r13,rbp adcx r13,r9 adox r8,r9 - adc r8,0 - + adc r8,0 ; cf=0, of=0 +; ################################ Multiply by b[2] mulx rbp,rcx,QWORD[((0+128))+rsi] adcx r10,rcx adox r11,rbp @@ -880,11 +881,11 @@ $L$ord_mulx_body: adcx r8,r9 adox r9,r9 - adc r9,0 - + adc r9,0 ; cf=0, of=0 +; ################################ reduction mulx rbp,rcx,QWORD[((0+128))+r14] - adcx r10,rcx + adcx r10,rcx ; guaranteed to be zero adox r11,rbp mulx rbp,rcx,QWORD[((8+128))+r14] @@ -901,9 +902,9 @@ $L$ord_mulx_body: adox r8,rbp adcx r8,r10 adox r9,r10 - adc r9,0 - + adc r9,0 ; cf=0, of=0 +; ################################ Multiply by b[3] mulx rbp,rcx,QWORD[((0+128))+rsi] adcx r11,rcx adox r12,rbp @@ -924,11 +925,11 @@ $L$ord_mulx_body: adcx r9,r10 adox r10,r10 - adc r10,0 - + adc r10,0 ; cf=0, of=0 +; ################################ reduction mulx rbp,rcx,QWORD[((0+128))+r14] - adcx r11,rcx + adcx r11,rcx ; guaranteed to be zero adox r12,rbp mulx rbp,rcx,QWORD[((8+128))+r14] @@ -949,8 +950,8 @@ $L$ord_mulx_body: adox r10,r11 adc r10,0 - - +; ################################ +; Branch-less conditional subtraction of P mov rcx,r8 sub r12,QWORD[r14] sbb r13,QWORD[8+r14] @@ -1030,38 +1031,38 @@ $L$ord_sqrx_body: ALIGN 32 $L$oop_ord_sqrx: - mulx r10,r9,r14 - mulx r11,rcx,r15 - mov rax,rdx - movq xmm1,r14 - mulx r12,rbp,r8 + mulx r10,r9,r14 ; a[0]*a[1] + mulx r11,rcx,r15 ; a[0]*a[2] + mov rax,rdx ; offload a[0] + movq xmm1,r14 ; offload a[1] + mulx r12,rbp,r8 ; a[0]*a[3] mov rdx,r14 add r10,rcx - movq xmm2,r15 + movq xmm2,r15 ; offload a[2] adc r11,rbp adc r12,0 - xor r13,r13 - - mulx rbp,rcx,r15 + xor r13,r13 ; %r13=0,cf=0,of=0 +; ################################ + mulx rbp,rcx,r15 ; a[1]*a[2] adcx r11,rcx adox r12,rbp - mulx rbp,rcx,r8 + mulx rbp,rcx,r8 ; a[1]*a[3] mov rdx,r15 adcx r12,rcx adox r13,rbp adc r13,0 - - mulx r14,rcx,r8 +; ################################ + mulx r14,rcx,r8 ; a[2]*a[3] mov rdx,rax - movq xmm3,r8 - xor r15,r15 - adcx r9,r9 + movq xmm3,r8 ; offload a[3] + xor r15,r15 ; %r15=0,cf=0,of=0 + adcx r9,r9 ; acc1:6<<1 adox r13,rcx adcx r10,r10 - adox r14,r15 - + adox r14,r15 ; of=0 +; ################################ a[i]*a[i] mulx rbp,r8,rdx movq rdx,xmm1 adcx r11,r11 @@ -1083,13 +1084,13 @@ $L$oop_ord_sqrx: adox r14,rcx adox r15,rax - +; ################################ reduction mov rdx,r8 mulx rcx,rdx,QWORD[32+rsi] - xor rax,rax + xor rax,rax ; cf=0, of=0 mulx rbp,rcx,QWORD[rsi] - adcx r8,rcx + adcx r8,rcx ; guaranteed to be zero adox r9,rbp mulx rbp,rcx,QWORD[8+rsi] adcx r9,rcx @@ -1099,15 +1100,15 @@ $L$oop_ord_sqrx: adox r11,rbp mulx rbp,rcx,QWORD[24+rsi] adcx r11,rcx - adox r8,rbp - adcx r8,rax - + adox r8,rbp ; of=0 + adcx r8,rax ; cf=0 +; ################################ mov rdx,r9 mulx rcx,rdx,QWORD[32+rsi] mulx rbp,rcx,QWORD[rsi] - adox r9,rcx + adox r9,rcx ; guaranteed to be zero adcx r10,rbp mulx rbp,rcx,QWORD[8+rsi] adox r10,rcx @@ -1117,15 +1118,15 @@ $L$oop_ord_sqrx: adcx r8,rbp mulx rbp,rcx,QWORD[24+rsi] adox r8,rcx - adcx r9,rbp - adox r9,rax - + adcx r9,rbp ; cf=0 + adox r9,rax ; of=0 +; ################################ mov rdx,r10 mulx rcx,rdx,QWORD[32+rsi] mulx rbp,rcx,QWORD[rsi] - adcx r10,rcx + adcx r10,rcx ; guaranteed to be zero adox r11,rbp mulx rbp,rcx,QWORD[8+rsi] adcx r11,rcx @@ -1135,15 +1136,15 @@ $L$oop_ord_sqrx: adox r9,rbp mulx rbp,rcx,QWORD[24+rsi] adcx r9,rcx - adox r10,rbp - adcx r10,rax - + adox r10,rbp ; of=0 + adcx r10,rax ; cf=0 +; ################################ mov rdx,r11 mulx rcx,rdx,QWORD[32+rsi] mulx rbp,rcx,QWORD[rsi] - adox r11,rcx + adox r11,rcx ; guaranteed to be zero adcx r8,rbp mulx rbp,rcx,QWORD[8+rsi] adox r8,rcx @@ -1156,8 +1157,8 @@ $L$oop_ord_sqrx: adcx r11,rbp adox r11,rax - - add r12,r8 +; ################################ accumulate upper half + add r12,r8 ; add %r12, %r8 adc r9,r13 mov rdx,r12 adc r10,r14 @@ -1165,7 +1166,7 @@ $L$oop_ord_sqrx: mov r14,r9 adc rax,0 - +; ################################ compare to modulus sub r12,QWORD[rsi] mov r15,r10 sbb r9,QWORD[8+rsi] @@ -1210,11 +1211,11 @@ $L$ord_sqrx_epilogue: ret $L$SEH_end_ecp_nistz256_ord_sqr_mont_adx: - - - - - +; ############################################################################### +; void ecp_nistz256_mul_mont( +; uint64_t res[4], +; uint64_t a[4], +; uint64_t b[4]); global ecp_nistz256_mul_mont_nohw @@ -1278,8 +1279,8 @@ $L$SEH_end_ecp_nistz256_mul_mont_nohw: ALIGN 32 __ecp_nistz256_mul_montq: - - +; ####################################################################### +; Multiply a by b[0] mov rbp,rax mul r9 mov r14,QWORD[(($L$poly+8))] @@ -1307,20 +1308,20 @@ __ecp_nistz256_mul_montq: xor r13,r13 mov r12,rdx - - - - - - - - +; ####################################################################### +; First reduction step +; Basically now we want to multiply acc[0] by p256, +; and add the result to the acc. +; Due to the special form of p256 we do some optimizations +; +; acc[0] x p256[0..1] = acc[0] x 2^96 - acc[0] +; then we add acc[0] and get acc[0] x 2^96 mov rbp,r8 shl r8,32 mul r15 shr rbp,32 - add r9,r8 + add r9,r8 ; +=acc[0]<<96 adc r10,rbp adc r11,rax mov rax,QWORD[8+rbx] @@ -1328,8 +1329,8 @@ __ecp_nistz256_mul_montq: adc r13,0 xor r8,r8 - - +; ####################################################################### +; Multiply by b[1] mov rbp,rax mul QWORD[rsi] add r9,rax @@ -1361,8 +1362,8 @@ __ecp_nistz256_mul_montq: adc r13,rdx adc r8,0 - - +; ####################################################################### +; Second reduction step mov rbp,r9 shl r9,32 mul r15 @@ -1375,8 +1376,8 @@ __ecp_nistz256_mul_montq: adc r8,0 xor r9,r9 - - +; ####################################################################### +; Multiply by b[2] mov rbp,rax mul QWORD[rsi] add r10,rax @@ -1408,8 +1409,8 @@ __ecp_nistz256_mul_montq: adc r8,rdx adc r9,0 - - +; ####################################################################### +; Third reduction step mov rbp,r10 shl r10,32 mul r15 @@ -1422,8 +1423,8 @@ __ecp_nistz256_mul_montq: adc r9,0 xor r10,r10 - - +; ####################################################################### +; Multiply by b[3] mov rbp,rax mul QWORD[rsi] add r11,rax @@ -1455,8 +1456,8 @@ __ecp_nistz256_mul_montq: adc r9,rdx adc r10,0 - - +; ####################################################################### +; Final reduction step mov rbp,r11 shl r11,32 mul r15 @@ -1469,14 +1470,14 @@ __ecp_nistz256_mul_montq: mov rbp,r13 adc r10,0 - - - sub r12,-1 +; ####################################################################### +; Branch-less conditional subtraction of P + sub r12,-1 ; .Lpoly[0] mov rbx,r8 - sbb r13,r14 - sbb r8,0 + sbb r13,r14 ; .Lpoly[1] + sbb r8,0 ; .Lpoly[2] mov rdx,r9 - sbb r9,r15 + sbb r9,r15 ; .Lpoly[3] sbb r10,0 cmovc r12,rcx @@ -1492,13 +1493,13 @@ __ecp_nistz256_mul_montq: +; ############################################################################### +; void ecp_nistz256_sqr_mont( +; uint64_t res[4], +; uint64_t a[4]); - - - - - - +; we optimize the square according to S.Gueron and V.Krasnov, +; "Speeding up Big-Number Squaring" global ecp_nistz256_sqr_mont_nohw ALIGN 32 @@ -1559,31 +1560,31 @@ ALIGN 32 __ecp_nistz256_sqr_montq: mov r13,rax - mul r14 + mul r14 ; a[1]*a[0] mov r9,rax mov rax,r15 mov r10,rdx - mul r13 + mul r13 ; a[0]*a[2] add r10,rax mov rax,r8 adc rdx,0 mov r11,rdx - mul r13 + mul r13 ; a[0]*a[3] add r11,rax mov rax,r15 adc rdx,0 mov r12,rdx - - mul r14 +; ################################ + mul r14 ; a[1]*a[2] add r11,rax mov rax,r8 adc rdx,0 mov rbp,rdx - mul r14 + mul r14 ; a[1]*a[3] add r12,rax mov rax,r8 adc rdx,0 @@ -1591,15 +1592,15 @@ __ecp_nistz256_sqr_montq: mov r13,rdx adc r13,0 - - mul r15 +; ################################ + mul r15 ; a[2]*a[3] xor r15,r15 add r13,rax mov rax,QWORD[rsi] mov r14,rdx adc r14,0 - add r9,r9 + add r9,r9 ; acc1:6<<1 adc r10,r10 adc r11,r11 adc r12,r12 @@ -1635,21 +1636,21 @@ __ecp_nistz256_sqr_montq: mov rsi,QWORD[(($L$poly+8))] mov rbp,QWORD[(($L$poly+24))] - - - +; ######################################### +; Now the reduction +; First iteration mov rcx,r8 shl r8,32 mul rbp shr rcx,32 - add r9,r8 + add r9,r8 ; +=acc[0]<<96 adc r10,rcx adc r11,rax mov rax,r9 adc rdx,0 - - +; ######################################### +; Second iteration mov rcx,r9 shl r9,32 mov r8,rdx @@ -1661,8 +1662,8 @@ __ecp_nistz256_sqr_montq: mov rax,r10 adc rdx,0 - - +; ######################################### +; Third iteration mov rcx,r10 shl r10,32 mov r9,rdx @@ -1674,8 +1675,8 @@ __ecp_nistz256_sqr_montq: mov rax,r11 adc rdx,0 - - +; ########################################## +; Last iteration mov rcx,r11 shl r11,32 mov r10,rdx @@ -1687,8 +1688,8 @@ __ecp_nistz256_sqr_montq: adc rdx,0 xor r11,r11 - - +; ########################################### +; Add the rest of the acc add r12,r8 adc r13,r9 mov r8,r12 @@ -1697,12 +1698,12 @@ __ecp_nistz256_sqr_montq: mov r9,r13 adc r11,0 - sub r12,-1 + sub r12,-1 ; .Lpoly[0] mov r10,r14 - sbb r13,rsi - sbb r14,0 + sbb r13,rsi ; .Lpoly[1] + sbb r14,0 ; .Lpoly[2] mov rcx,r15 - sbb r15,rbp + sbb r15,rbp ; .Lpoly[3] sbb r11,0 cmovc r12,r8 @@ -1751,7 +1752,7 @@ $L$mulx_body: mov r10,QWORD[8+rsi] mov r11,QWORD[16+rsi] mov r12,QWORD[24+rsi] - lea rsi,[((-128))+rsi] + lea rsi,[((-128))+rsi] ; control u-op density call __ecp_nistz256_mul_montx @@ -1780,12 +1781,12 @@ $L$SEH_end_ecp_nistz256_mul_mont_adx: ALIGN 32 __ecp_nistz256_mul_montx: - - +; ####################################################################### +; Multiply by b[0] mulx r9,r8,r9 mulx r10,rcx,r10 mov r14,32 - xor r13,r13 + xor r13,r13 ; cf=0 mulx r11,rbp,r11 mov r15,QWORD[(($L$poly+24))] adc r9,rcx @@ -1797,8 +1798,8 @@ __ecp_nistz256_mul_montx: shrx rcx,r8,r14 adc r12,0 - - +; ####################################################################### +; First reduction step add r9,rbp adc r10,rcx @@ -1807,10 +1808,10 @@ __ecp_nistz256_mul_montx: adc r11,rcx adc r12,rbp adc r13,0 - xor r8,r8 - - + xor r8,r8 ; %r8=0,cf=0,of=0 +; ####################################################################### +; Multiply by b[1] mulx rbp,rcx,QWORD[((0+128))+rsi] adcx r9,rcx adox r10,rbp @@ -1834,8 +1835,8 @@ __ecp_nistz256_mul_montx: adox r8,r8 adc r8,0 - - +; ####################################################################### +; Second reduction step add r10,rcx adc r11,rbp @@ -1844,10 +1845,10 @@ __ecp_nistz256_mul_montx: adc r12,rcx adc r13,rbp adc r8,0 - xor r9,r9 - - + xor r9,r9 ; %r9=0,cf=0,of=0 +; ####################################################################### +; Multiply by b[2] mulx rbp,rcx,QWORD[((0+128))+rsi] adcx r10,rcx adox r11,rbp @@ -1871,8 +1872,8 @@ __ecp_nistz256_mul_montx: adox r9,r9 adc r9,0 - - +; ####################################################################### +; Third reduction step add r11,rcx adc r12,rbp @@ -1881,10 +1882,10 @@ __ecp_nistz256_mul_montx: adc r13,rcx adc r8,rbp adc r9,0 - xor r10,r10 - - + xor r10,r10 ; %r10=0,cf=0,of=0 +; ####################################################################### +; Multiply by b[3] mulx rbp,rcx,QWORD[((0+128))+rsi] adcx r11,rcx adox r12,rbp @@ -1908,8 +1909,8 @@ __ecp_nistz256_mul_montx: adox r10,r10 adc r10,0 - - +; ####################################################################### +; Fourth reduction step add r12,rcx adc r13,rbp @@ -1921,15 +1922,15 @@ __ecp_nistz256_mul_montx: adc r9,rbp adc r10,0 - - +; ####################################################################### +; Branch-less conditional subtraction of P xor eax,eax mov rcx,r8 - sbb r12,-1 - sbb r13,r14 - sbb r8,0 + sbb r12,-1 ; .Lpoly[0] + sbb r13,r14 ; .Lpoly[1] + sbb r8,0 ; .Lpoly[2] mov rbp,r9 - sbb r9,r15 + sbb r9,r15 ; .Lpoly[3] sbb r10,0 cmovc r12,rbx @@ -1976,7 +1977,7 @@ $L$sqrx_body: mov r14,QWORD[8+rsi] mov r15,QWORD[16+rsi] mov r8,QWORD[24+rsi] - lea rsi,[((-128))+rsi] + lea rsi,[((-128))+rsi] ; control u-op density call __ecp_nistz256_sqr_montx @@ -2005,35 +2006,35 @@ $L$SEH_end_ecp_nistz256_sqr_mont_adx: ALIGN 32 __ecp_nistz256_sqr_montx: - mulx r10,r9,r14 - mulx r11,rcx,r15 + mulx r10,r9,r14 ; a[0]*a[1] + mulx r11,rcx,r15 ; a[0]*a[2] xor eax,eax adc r10,rcx - mulx r12,rbp,r8 + mulx r12,rbp,r8 ; a[0]*a[3] mov rdx,r14 adc r11,rbp adc r12,0 - xor r13,r13 + xor r13,r13 ; %r13=0,cf=0,of=0 - - mulx rbp,rcx,r15 +; ################################ + mulx rbp,rcx,r15 ; a[1]*a[2] adcx r11,rcx adox r12,rbp - mulx rbp,rcx,r8 + mulx rbp,rcx,r8 ; a[1]*a[3] mov rdx,r15 adcx r12,rcx adox r13,rbp adc r13,0 - - mulx r14,rcx,r8 +; ################################ + mulx r14,rcx,r8 ; a[2]*a[3] mov rdx,QWORD[((0+128))+rsi] - xor r15,r15 - adcx r9,r9 + xor r15,r15 ; %r15=0,cf=0,of=0 + adcx r9,r9 ; acc1:6<<1 adox r13,rcx adcx r10,r10 - adox r14,r15 + adox r14,r15 ; of=0 mulx rbp,r8,rdx mov rdx,QWORD[((8+128))+rsi] @@ -2062,7 +2063,7 @@ __ecp_nistz256_sqr_montx: shrx rax,r8,rsi mov rbp,rdx - +; reduction step 1 add r9,rcx adc r10,rax @@ -2072,7 +2073,7 @@ __ecp_nistz256_sqr_montx: adc r8,0 shrx rax,r9,rsi - +; reduction step 2 add r10,rcx adc r11,rax @@ -2082,7 +2083,7 @@ __ecp_nistz256_sqr_montx: adc r9,0 shrx rax,r10,rsi - +; reduction step 3 add r11,rcx adc r8,rax @@ -2092,7 +2093,7 @@ __ecp_nistz256_sqr_montx: adc r10,0 shrx rax,r11,rsi - +; reduction step 4 add r8,rcx adc r9,rax @@ -2101,7 +2102,7 @@ __ecp_nistz256_sqr_montx: adc r11,0 xor rdx,rdx - add r12,r8 + add r12,r8 ; accumulate upper half mov rsi,QWORD[(($L$poly+8))] adc r13,r9 mov r8,r12 @@ -2110,12 +2111,12 @@ __ecp_nistz256_sqr_montx: mov r9,r13 adc rdx,0 - sub r12,-1 + sub r12,-1 ; .Lpoly[0] mov r10,r14 - sbb r13,rsi - sbb r14,0 + sbb r13,rsi ; .Lpoly[1] + sbb r14,0 ; .Lpoly[2] mov r11,r15 - sbb r15,rbp + sbb r15,rbp ; .Lpoly[3] sbb rdx,0 cmovc r12,r8 @@ -2130,8 +2131,8 @@ __ecp_nistz256_sqr_montx: ret - - +; ############################################################################### +; void ecp_nistz256_select_w5_nohw(uint64_t *val, uint64_t *in_t, int index); global ecp_nistz256_select_w5_nohw ALIGN 32 @@ -2140,17 +2141,17 @@ ecp_nistz256_select_w5_nohw: _CET_ENDBR lea rax,[((-136))+rsp] $L$SEH_begin_ecp_nistz256_select_w5_nohw: - DB 0x48,0x8d,0x60,0xe0 - DB 0x0f,0x29,0x70,0xe0 - DB 0x0f,0x29,0x78,0xf0 - DB 0x44,0x0f,0x29,0x00 - DB 0x44,0x0f,0x29,0x48,0x10 - DB 0x44,0x0f,0x29,0x50,0x20 - DB 0x44,0x0f,0x29,0x58,0x30 - DB 0x44,0x0f,0x29,0x60,0x40 - DB 0x44,0x0f,0x29,0x68,0x50 - DB 0x44,0x0f,0x29,0x70,0x60 - DB 0x44,0x0f,0x29,0x78,0x70 + DB 0x48,0x8d,0x60,0xe0 ; lea -0x20(%rax), %rsp + DB 0x0f,0x29,0x70,0xe0 ; movaps %xmm6, -0x20(%rax) + DB 0x0f,0x29,0x78,0xf0 ; movaps %xmm7, -0x10(%rax) + DB 0x44,0x0f,0x29,0x00 ; movaps %xmm8, 0(%rax) + DB 0x44,0x0f,0x29,0x48,0x10 ; movaps %xmm9, 0x10(%rax) + DB 0x44,0x0f,0x29,0x50,0x20 ; movaps %xmm10, 0x20(%rax) + DB 0x44,0x0f,0x29,0x58,0x30 ; movaps %xmm11, 0x30(%rax) + DB 0x44,0x0f,0x29,0x60,0x40 ; movaps %xmm12, 0x40(%rax) + DB 0x44,0x0f,0x29,0x68,0x50 ; movaps %xmm13, 0x50(%rax) + DB 0x44,0x0f,0x29,0x70,0x60 ; movaps %xmm14, 0x60(%rax) + DB 0x44,0x0f,0x29,0x78,0x70 ; movaps %xmm15, 0x70(%rax) movdqa xmm0,XMMWORD[$L$One] movd xmm1,r8d @@ -2217,8 +2218,8 @@ $L$select_loop_sse_w5: $L$SEH_end_ecp_nistz256_select_w5_nohw: - - +; ############################################################################### +; void ecp_nistz256_select_w7_nohw(uint64_t *val, uint64_t *in_t, int index); global ecp_nistz256_select_w7_nohw ALIGN 32 @@ -2227,17 +2228,17 @@ ecp_nistz256_select_w7_nohw: _CET_ENDBR lea rax,[((-136))+rsp] $L$SEH_begin_ecp_nistz256_select_w7_nohw: - DB 0x48,0x8d,0x60,0xe0 - DB 0x0f,0x29,0x70,0xe0 - DB 0x0f,0x29,0x78,0xf0 - DB 0x44,0x0f,0x29,0x00 - DB 0x44,0x0f,0x29,0x48,0x10 - DB 0x44,0x0f,0x29,0x50,0x20 - DB 0x44,0x0f,0x29,0x58,0x30 - DB 0x44,0x0f,0x29,0x60,0x40 - DB 0x44,0x0f,0x29,0x68,0x50 - DB 0x44,0x0f,0x29,0x70,0x60 - DB 0x44,0x0f,0x29,0x78,0x70 + DB 0x48,0x8d,0x60,0xe0 ; lea -0x20(%rax), %rsp + DB 0x0f,0x29,0x70,0xe0 ; movaps %xmm6, -0x20(%rax) + DB 0x0f,0x29,0x78,0xf0 ; movaps %xmm7, -0x10(%rax) + DB 0x44,0x0f,0x29,0x00 ; movaps %xmm8, 0(%rax) + DB 0x44,0x0f,0x29,0x48,0x10 ; movaps %xmm9, 0x10(%rax) + DB 0x44,0x0f,0x29,0x50,0x20 ; movaps %xmm10, 0x20(%rax) + DB 0x44,0x0f,0x29,0x58,0x30 ; movaps %xmm11, 0x30(%rax) + DB 0x44,0x0f,0x29,0x60,0x40 ; movaps %xmm12, 0x40(%rax) + DB 0x44,0x0f,0x29,0x68,0x50 ; movaps %xmm13, 0x50(%rax) + DB 0x44,0x0f,0x29,0x70,0x60 ; movaps %xmm14, 0x60(%rax) + DB 0x44,0x0f,0x29,0x78,0x70 ; movaps %xmm15, 0x70(%rax) movdqa xmm8,XMMWORD[$L$One] movd xmm1,r8d @@ -2292,8 +2293,8 @@ $L$select_loop_sse_w7: $L$SEH_end_ecp_nistz256_select_w7_nohw: - - +; ############################################################################### +; void ecp_nistz256_select_w5_avx2(uint64_t *val, uint64_t *in_t, int index); global ecp_nistz256_select_w5_avx2 ALIGN 32 @@ -2304,17 +2305,17 @@ _CET_ENDBR lea rax,[((-136))+rsp] mov r11,rsp $L$SEH_begin_ecp_nistz256_select_w5_avx2: - DB 0x48,0x8d,0x60,0xe0 - DB 0xc5,0xf8,0x29,0x70,0xe0 - DB 0xc5,0xf8,0x29,0x78,0xf0 - DB 0xc5,0x78,0x29,0x40,0x00 - DB 0xc5,0x78,0x29,0x48,0x10 - DB 0xc5,0x78,0x29,0x50,0x20 - DB 0xc5,0x78,0x29,0x58,0x30 - DB 0xc5,0x78,0x29,0x60,0x40 - DB 0xc5,0x78,0x29,0x68,0x50 - DB 0xc5,0x78,0x29,0x70,0x60 - DB 0xc5,0x78,0x29,0x78,0x70 + DB 0x48,0x8d,0x60,0xe0 ; lea -0x20(%rax), %rsp + DB 0xc5,0xf8,0x29,0x70,0xe0 ; vmovaps %xmm6, -0x20(%rax) + DB 0xc5,0xf8,0x29,0x78,0xf0 ; vmovaps %xmm7, -0x10(%rax) + DB 0xc5,0x78,0x29,0x40,0x00 ; vmovaps %xmm8, 8(%rax) + DB 0xc5,0x78,0x29,0x48,0x10 ; vmovaps %xmm9, 0x10(%rax) + DB 0xc5,0x78,0x29,0x50,0x20 ; vmovaps %xmm10, 0x20(%rax) + DB 0xc5,0x78,0x29,0x58,0x30 ; vmovaps %xmm11, 0x30(%rax) + DB 0xc5,0x78,0x29,0x60,0x40 ; vmovaps %xmm12, 0x40(%rax) + DB 0xc5,0x78,0x29,0x68,0x50 ; vmovaps %xmm13, 0x50(%rax) + DB 0xc5,0x78,0x29,0x70,0x60 ; vmovaps %xmm14, 0x60(%rax) + DB 0xc5,0x78,0x29,0x78,0x70 ; vmovaps %xmm15, 0x70(%rax) vmovdqa ymm0,YMMWORD[$L$Two] vpxor ymm2,ymm2,ymm2 @@ -2382,8 +2383,8 @@ $L$select_loop_avx2_w5: $L$SEH_end_ecp_nistz256_select_w5_avx2: - - +; ############################################################################### +; void ecp_nistz256_select_w7_avx2(uint64_t *val, uint64_t *in_t, int index); global ecp_nistz256_select_w7_avx2 ALIGN 32 @@ -2394,17 +2395,17 @@ _CET_ENDBR mov r11,rsp lea rax,[((-136))+rsp] $L$SEH_begin_ecp_nistz256_select_w7_avx2: - DB 0x48,0x8d,0x60,0xe0 - DB 0xc5,0xf8,0x29,0x70,0xe0 - DB 0xc5,0xf8,0x29,0x78,0xf0 - DB 0xc5,0x78,0x29,0x40,0x00 - DB 0xc5,0x78,0x29,0x48,0x10 - DB 0xc5,0x78,0x29,0x50,0x20 - DB 0xc5,0x78,0x29,0x58,0x30 - DB 0xc5,0x78,0x29,0x60,0x40 - DB 0xc5,0x78,0x29,0x68,0x50 - DB 0xc5,0x78,0x29,0x70,0x60 - DB 0xc5,0x78,0x29,0x78,0x70 + DB 0x48,0x8d,0x60,0xe0 ; lea -0x20(%rax), %rsp + DB 0xc5,0xf8,0x29,0x70,0xe0 ; vmovaps %xmm6, -0x20(%rax) + DB 0xc5,0xf8,0x29,0x78,0xf0 ; vmovaps %xmm7, -0x10(%rax) + DB 0xc5,0x78,0x29,0x40,0x00 ; vmovaps %xmm8, 8(%rax) + DB 0xc5,0x78,0x29,0x48,0x10 ; vmovaps %xmm9, 0x10(%rax) + DB 0xc5,0x78,0x29,0x50,0x20 ; vmovaps %xmm10, 0x20(%rax) + DB 0xc5,0x78,0x29,0x58,0x30 ; vmovaps %xmm11, 0x30(%rax) + DB 0xc5,0x78,0x29,0x60,0x40 ; vmovaps %xmm12, 0x40(%rax) + DB 0xc5,0x78,0x29,0x68,0x50 ; vmovaps %xmm13, 0x50(%rax) + DB 0xc5,0x78,0x29,0x70,0x60 ; vmovaps %xmm14, 0x60(%rax) + DB 0xc5,0x78,0x29,0x78,0x70 ; vmovaps %xmm15, 0x70(%rax) vmovdqa ymm0,YMMWORD[$L$Three] vpxor ymm2,ymm2,ymm2 @@ -2416,7 +2417,7 @@ $L$SEH_begin_ecp_nistz256_select_w7_avx2: vmovd xmm1,r8d vpermd ymm1,ymm2,ymm1 - +; Skip index = 0, because it is implicitly the point at infinity mov rax,21 $L$select_loop_avx2_w7: @@ -2587,7 +2588,7 @@ ALIGN 32 __ecp_nistz256_mul_by_2q: xor r11,r11 - add r12,r12 + add r12,r12 ; a0:a3+a0:a3 adc r13,r13 mov rax,r12 adc r8,r8 @@ -2646,10 +2647,10 @@ _CET_ENDBR $L$point_doubleq_body: $L$point_double_shortcutq: - movdqu xmm0,XMMWORD[rsi] - mov rbx,rsi + movdqu xmm0,XMMWORD[rsi] ; copy *(P256_POINT *)%rsi.x + mov rbx,rsi ; backup copy movdqu xmm1,XMMWORD[16+rsi] - mov r12,QWORD[((32+0))+rsi] + mov r12,QWORD[((32+0))+rsi] ; load in_y in "5-4-0-1" order mov r13,QWORD[((32+8))+rsi] mov r8,QWORD[((32+16))+rsi] mov r9,QWORD[((32+24))+rsi] @@ -2664,7 +2665,7 @@ $L$point_double_shortcutq: movq xmm2,r11 lea rdi,[rsp] - call __ecp_nistz256_mul_by_2q + call __ecp_nistz256_mul_by_2q ; p256_mul_by_2(S, in_y); mov rax,QWORD[((64+0))+rsi] mov r14,QWORD[((64+8))+rsi] @@ -2672,7 +2673,7 @@ $L$point_double_shortcutq: mov r8,QWORD[((64+24))+rsi] lea rsi,[((64-0))+rsi] lea rdi,[64+rsp] - call __ecp_nistz256_sqr_montq + call __ecp_nistz256_sqr_montq ; p256_sqr_mont(Zsqr, in_z); mov rax,QWORD[((0+0))+rsp] mov r14,QWORD[((8+0))+rsp] @@ -2680,9 +2681,9 @@ $L$point_double_shortcutq: mov r15,QWORD[((16+0))+rsp] mov r8,QWORD[((24+0))+rsp] lea rdi,[rsp] - call __ecp_nistz256_sqr_montq + call __ecp_nistz256_sqr_montq ; p256_sqr_mont(S, S); - mov rax,QWORD[32+rbx] + mov rax,QWORD[32+rbx] ; %rbx is still valid mov r9,QWORD[((64+0))+rbx] mov r10,QWORD[((64+8))+rbx] mov r11,QWORD[((64+16))+rbx] @@ -2690,24 +2691,24 @@ $L$point_double_shortcutq: lea rsi,[((64-0))+rbx] lea rbx,[32+rbx] movq rdi,xmm2 - call __ecp_nistz256_mul_montq - call __ecp_nistz256_mul_by_2q + call __ecp_nistz256_mul_montq ; p256_mul_mont(res_z, in_z, in_y); + call __ecp_nistz256_mul_by_2q ; p256_mul_by_2(res_z, res_z); - mov r12,QWORD[((96+0))+rsp] + mov r12,QWORD[((96+0))+rsp] ; "5-4-0-1" order mov r13,QWORD[((96+8))+rsp] lea rbx,[64+rsp] mov r8,QWORD[((96+16))+rsp] mov r9,QWORD[((96+24))+rsp] lea rdi,[32+rsp] - call __ecp_nistz256_add_toq + call __ecp_nistz256_add_toq ; p256_add(M, in_x, Zsqr); - mov r12,QWORD[((96+0))+rsp] + mov r12,QWORD[((96+0))+rsp] ; "5-4-0-1" order mov r13,QWORD[((96+8))+rsp] lea rbx,[64+rsp] mov r8,QWORD[((96+16))+rsp] mov r9,QWORD[((96+24))+rsp] lea rdi,[64+rsp] - call __ecp_nistz256_sub_fromq + call __ecp_nistz256_sub_fromq ; p256_sub(Zsqr, in_x, Zsqr); mov rax,QWORD[((0+0))+rsp] mov r14,QWORD[((8+0))+rsp] @@ -2715,7 +2716,7 @@ $L$point_double_shortcutq: mov r15,QWORD[((16+0))+rsp] mov r8,QWORD[((24+0))+rsp] movq rdi,xmm1 - call __ecp_nistz256_sqr_montq + call __ecp_nistz256_sqr_montq ; p256_sqr_mont(res_y, S); xor r9,r9 mov rax,r12 add r12,-1 @@ -2726,7 +2727,7 @@ $L$point_double_shortcutq: mov r8,r15 adc r15,rbp adc r9,0 - xor rsi,rsi + xor rsi,rsi ; borrow %rsi test rax,1 cmovz r12,rax @@ -2735,7 +2736,7 @@ $L$point_double_shortcutq: cmovz r15,r8 cmovz r9,rsi - mov rax,r13 + mov rax,r13 ; a0:a3>>1 shr r12,1 shl rax,63 mov r10,r14 @@ -2762,14 +2763,14 @@ $L$point_double_shortcutq: mov r11,QWORD[((16+32))+rsp] mov r12,QWORD[((24+32))+rsp] lea rdi,[32+rsp] - call __ecp_nistz256_mul_montq + call __ecp_nistz256_mul_montq ; p256_mul_mont(M, M, Zsqr); lea rdi,[128+rsp] call __ecp_nistz256_mul_by_2q lea rbx,[32+rsp] lea rdi,[32+rsp] - call __ecp_nistz256_add_toq + call __ecp_nistz256_add_toq ; p256_mul_by_3(M, M); mov rax,QWORD[96+rsp] lea rbx,[96+rsp] @@ -2779,10 +2780,10 @@ $L$point_double_shortcutq: mov r11,QWORD[((16+0))+rsp] mov r12,QWORD[((24+0))+rsp] lea rdi,[rsp] - call __ecp_nistz256_mul_montq + call __ecp_nistz256_mul_montq ; p256_mul_mont(S, S, in_x); lea rdi,[128+rsp] - call __ecp_nistz256_mul_by_2q + call __ecp_nistz256_mul_by_2q ; p256_mul_by_2(tmp0, S); mov rax,QWORD[((0+32))+rsp] mov r14,QWORD[((8+32))+rsp] @@ -2790,27 +2791,27 @@ $L$point_double_shortcutq: mov r15,QWORD[((16+32))+rsp] mov r8,QWORD[((24+32))+rsp] movq rdi,xmm0 - call __ecp_nistz256_sqr_montq + call __ecp_nistz256_sqr_montq ; p256_sqr_mont(res_x, M); lea rbx,[128+rsp] - mov r8,r14 + mov r8,r14 ; harmonize sqr output and sub input mov r9,r15 mov r14,rsi mov r15,rbp - call __ecp_nistz256_sub_fromq + call __ecp_nistz256_sub_fromq ; p256_sub(res_x, res_x, tmp0); mov rax,QWORD[((0+0))+rsp] mov rbp,QWORD[((0+8))+rsp] mov rcx,QWORD[((0+16))+rsp] - mov r10,QWORD[((0+24))+rsp] + mov r10,QWORD[((0+24))+rsp] ; "4-5-0-1" order lea rdi,[rsp] - call __ecp_nistz256_subq + call __ecp_nistz256_subq ; p256_sub(S, S, res_x); mov rax,QWORD[32+rsp] lea rbx,[32+rsp] - mov r14,r12 + mov r14,r12 ; harmonize sub output and mul input xor ecx,ecx - mov QWORD[((0+0))+rsp],r12 + mov QWORD[((0+0))+rsp],r12 ; have to save:-( mov r10,r13 mov QWORD[((0+8))+rsp],r13 cmovz r11,r8 @@ -2820,11 +2821,11 @@ $L$point_double_shortcutq: mov QWORD[((0+24))+rsp],r9 mov r9,r14 lea rdi,[rsp] - call __ecp_nistz256_mul_montq + call __ecp_nistz256_mul_montq ; p256_mul_mont(S, S, M); movq rbx,xmm1 movq rdi,xmm1 - call __ecp_nistz256_sub_fromq + call __ecp_nistz256_sub_fromq ; p256_sub(res_y, S, res_y); lea rsi,[((160+56))+rsp] @@ -2879,14 +2880,14 @@ _CET_ENDBR $L$point_addq_body: - movdqu xmm0,XMMWORD[rsi] + movdqu xmm0,XMMWORD[rsi] ; copy *(P256_POINT *)%rsi movdqu xmm1,XMMWORD[16+rsi] movdqu xmm2,XMMWORD[32+rsi] movdqu xmm3,XMMWORD[48+rsi] movdqu xmm4,XMMWORD[64+rsi] movdqu xmm5,XMMWORD[80+rsi] - mov rbx,rsi - mov rsi,rdx + mov rbx,rsi ; reassign + mov rsi,rdx ; reassign movdqa XMMWORD[384+rsp],xmm0 movdqa XMMWORD[(384+16)+rsp],xmm1 movdqa XMMWORD[416+rsp],xmm2 @@ -2895,54 +2896,54 @@ $L$point_addq_body: movdqa XMMWORD[(448+16)+rsp],xmm5 por xmm5,xmm4 - movdqu xmm0,XMMWORD[rsi] + movdqu xmm0,XMMWORD[rsi] ; copy *(P256_POINT *)%rbx pshufd xmm3,xmm5,0xb1 movdqu xmm1,XMMWORD[16+rsi] movdqu xmm2,XMMWORD[32+rsi] por xmm5,xmm3 movdqu xmm3,XMMWORD[48+rsi] - mov rax,QWORD[((64+0))+rsi] + mov rax,QWORD[((64+0))+rsi] ; load original in2_z mov r14,QWORD[((64+8))+rsi] mov r15,QWORD[((64+16))+rsi] mov r8,QWORD[((64+24))+rsi] movdqa XMMWORD[480+rsp],xmm0 pshufd xmm4,xmm5,0x1e movdqa XMMWORD[(480+16)+rsp],xmm1 - movdqu xmm0,XMMWORD[64+rsi] + movdqu xmm0,XMMWORD[64+rsi] ; in2_z again movdqu xmm1,XMMWORD[80+rsi] movdqa XMMWORD[512+rsp],xmm2 movdqa XMMWORD[(512+16)+rsp],xmm3 por xmm5,xmm4 pxor xmm4,xmm4 por xmm1,xmm0 - movq xmm0,rdi + movq xmm0,rdi ; save %rdi - lea rsi,[((64-0))+rsi] - mov QWORD[((544+0))+rsp],rax + lea rsi,[((64-0))+rsi] ; %rsi is still valid + mov QWORD[((544+0))+rsp],rax ; make in2_z copy mov QWORD[((544+8))+rsp],r14 mov QWORD[((544+16))+rsp],r15 mov QWORD[((544+24))+rsp],r8 - lea rdi,[96+rsp] - call __ecp_nistz256_sqr_montq + lea rdi,[96+rsp] ; Z2^2 + call __ecp_nistz256_sqr_montq ; p256_sqr_mont(Z2sqr, in2_z); pcmpeqd xmm5,xmm4 pshufd xmm4,xmm1,0xb1 por xmm4,xmm1 - pshufd xmm5,xmm5,0 + pshufd xmm5,xmm5,0 ; in1infty pshufd xmm3,xmm4,0x1e por xmm4,xmm3 pxor xmm3,xmm3 pcmpeqd xmm4,xmm3 - pshufd xmm4,xmm4,0 - mov rax,QWORD[((64+0))+rbx] + pshufd xmm4,xmm4,0 ; in2infty + mov rax,QWORD[((64+0))+rbx] ; load original in1_z mov r14,QWORD[((64+8))+rbx] mov r15,QWORD[((64+16))+rbx] mov r8,QWORD[((64+24))+rbx] movq xmm1,rbx lea rsi,[((64-0))+rbx] - lea rdi,[32+rsp] - call __ecp_nistz256_sqr_montq + lea rdi,[32+rsp] ; Z1^2 + call __ecp_nistz256_sqr_montq ; p256_sqr_mont(Z1sqr, in1_z); mov rax,QWORD[544+rsp] lea rbx,[544+rsp] @@ -2951,8 +2952,8 @@ $L$point_addq_body: lea rsi,[((0+96))+rsp] mov r11,QWORD[((16+96))+rsp] mov r12,QWORD[((24+96))+rsp] - lea rdi,[224+rsp] - call __ecp_nistz256_mul_montq + lea rdi,[224+rsp] ; S1 = Z2^3 + call __ecp_nistz256_mul_montq ; p256_mul_mont(S1, Z2sqr, in2_z); mov rax,QWORD[448+rsp] lea rbx,[448+rsp] @@ -2961,8 +2962,8 @@ $L$point_addq_body: lea rsi,[((0+32))+rsp] mov r11,QWORD[((16+32))+rsp] mov r12,QWORD[((24+32))+rsp] - lea rdi,[256+rsp] - call __ecp_nistz256_mul_montq + lea rdi,[256+rsp] ; S2 = Z1^3 + call __ecp_nistz256_mul_montq ; p256_mul_mont(S2, Z1sqr, in1_z); mov rax,QWORD[416+rsp] lea rbx,[416+rsp] @@ -2971,8 +2972,8 @@ $L$point_addq_body: lea rsi,[((0+224))+rsp] mov r11,QWORD[((16+224))+rsp] mov r12,QWORD[((24+224))+rsp] - lea rdi,[224+rsp] - call __ecp_nistz256_mul_montq + lea rdi,[224+rsp] ; S1 = Y1*Z2^3 + call __ecp_nistz256_mul_montq ; p256_mul_mont(S1, S1, in1_y); mov rax,QWORD[512+rsp] lea rbx,[512+rsp] @@ -2981,18 +2982,18 @@ $L$point_addq_body: lea rsi,[((0+256))+rsp] mov r11,QWORD[((16+256))+rsp] mov r12,QWORD[((24+256))+rsp] - lea rdi,[256+rsp] - call __ecp_nistz256_mul_montq + lea rdi,[256+rsp] ; S2 = Y2*Z1^3 + call __ecp_nistz256_mul_montq ; p256_mul_mont(S2, S2, in2_y); lea rbx,[224+rsp] - lea rdi,[64+rsp] - call __ecp_nistz256_sub_fromq + lea rdi,[64+rsp] ; R = S2 - S1 + call __ecp_nistz256_sub_fromq ; p256_sub(R, S2, S1); - or r12,r13 + or r12,r13 ; see if result is zero movdqa xmm2,xmm4 or r12,r8 or r12,r9 - por xmm2,xmm5 + por xmm2,xmm5 ; in1infty || in2infty movq xmm3,r12 mov rax,QWORD[384+rsp] @@ -3002,8 +3003,8 @@ $L$point_addq_body: lea rsi,[((0+96))+rsp] mov r11,QWORD[((16+96))+rsp] mov r12,QWORD[((24+96))+rsp] - lea rdi,[160+rsp] - call __ecp_nistz256_mul_montq + lea rdi,[160+rsp] ; U1 = X1*Z2^2 + call __ecp_nistz256_mul_montq ; p256_mul_mont(U1, in1_x, Z2sqr); mov rax,QWORD[480+rsp] lea rbx,[480+rsp] @@ -3012,34 +3013,34 @@ $L$point_addq_body: lea rsi,[((0+32))+rsp] mov r11,QWORD[((16+32))+rsp] mov r12,QWORD[((24+32))+rsp] - lea rdi,[192+rsp] - call __ecp_nistz256_mul_montq + lea rdi,[192+rsp] ; U2 = X2*Z1^2 + call __ecp_nistz256_mul_montq ; p256_mul_mont(U2, in2_x, Z1sqr); lea rbx,[160+rsp] - lea rdi,[rsp] - call __ecp_nistz256_sub_fromq + lea rdi,[rsp] ; H = U2 - U1 + call __ecp_nistz256_sub_fromq ; p256_sub(H, U2, U1); - or r12,r13 + or r12,r13 ; see if result is zero or r12,r8 - or r12,r9 + or r12,r9 ; !is_equal(U1, U2) movq r8,xmm2 movq r9,xmm3 or r12,r8 - DB 0x3e - jnz NEAR $L$add_proceedq - - + DB 0x3e ; predict taken + jnz NEAR $L$add_proceedq ; !is_equal(U1, U2) || in1infty || in2infty +; We now know A = B or A = -B and neither is infinity. Compare the +; y-coordinates via S1 and S2. test r9,r9 - jz NEAR $L$add_doubleq - - - - - - - movq rdi,xmm0 + jz NEAR $L$add_doubleq ; is_equal(S1, S2) + +; A = -B, so the result is infinity. +; +; TODO(davidben): Does .Ladd_proceed handle this case? It seems to, in +; which case we should eliminate this special-case and simplify the +; timing analysis. + movq rdi,xmm0 ; restore %rdi pxor xmm0,xmm0 movdqu XMMWORD[rdi],xmm0 movdqu XMMWORD[16+rdi],xmm0 @@ -3051,9 +3052,9 @@ $L$point_addq_body: ALIGN 32 $L$add_doubleq: - movq rsi,xmm1 - movq rdi,xmm0 - add rsp,416 + movq rsi,xmm1 ; restore %rsi + movq rdi,xmm0 ; restore %rdi + add rsp,416 ; difference in frame sizes jmp NEAR $L$point_double_shortcutq @@ -3065,8 +3066,8 @@ $L$add_proceedq: lea rsi,[((0+64))+rsp] mov r15,QWORD[((16+64))+rsp] mov r8,QWORD[((24+64))+rsp] - lea rdi,[96+rsp] - call __ecp_nistz256_sqr_montq + lea rdi,[96+rsp] ; R^2 + call __ecp_nistz256_sqr_montq ; p256_sqr_mont(Rsqr, R); mov rax,QWORD[448+rsp] lea rbx,[448+rsp] @@ -3075,16 +3076,16 @@ $L$add_proceedq: lea rsi,[((0+0))+rsp] mov r11,QWORD[((16+0))+rsp] mov r12,QWORD[((24+0))+rsp] - lea rdi,[352+rsp] - call __ecp_nistz256_mul_montq + lea rdi,[352+rsp] ; Z3 = H*Z1*Z2 + call __ecp_nistz256_mul_montq ; p256_mul_mont(res_z, H, in1_z); mov rax,QWORD[((0+0))+rsp] mov r14,QWORD[((8+0))+rsp] lea rsi,[((0+0))+rsp] mov r15,QWORD[((16+0))+rsp] mov r8,QWORD[((24+0))+rsp] - lea rdi,[32+rsp] - call __ecp_nistz256_sqr_montq + lea rdi,[32+rsp] ; H^2 + call __ecp_nistz256_sqr_montq ; p256_sqr_mont(Hsqr, H); mov rax,QWORD[544+rsp] lea rbx,[544+rsp] @@ -3093,8 +3094,8 @@ $L$add_proceedq: lea rsi,[((0+352))+rsp] mov r11,QWORD[((16+352))+rsp] mov r12,QWORD[((24+352))+rsp] - lea rdi,[352+rsp] - call __ecp_nistz256_mul_montq + lea rdi,[352+rsp] ; Z3 = H*Z1*Z2 + call __ecp_nistz256_mul_montq ; p256_mul_mont(res_z, res_z, in2_z); mov rax,QWORD[rsp] lea rbx,[rsp] @@ -3103,8 +3104,8 @@ $L$add_proceedq: lea rsi,[((0+32))+rsp] mov r11,QWORD[((16+32))+rsp] mov r12,QWORD[((24+32))+rsp] - lea rdi,[128+rsp] - call __ecp_nistz256_mul_montq + lea rdi,[128+rsp] ; H^3 + call __ecp_nistz256_mul_montq ; p256_mul_mont(Hcub, Hsqr, H); mov rax,QWORD[160+rsp] lea rbx,[160+rsp] @@ -3113,14 +3114,14 @@ $L$add_proceedq: lea rsi,[((0+32))+rsp] mov r11,QWORD[((16+32))+rsp] mov r12,QWORD[((24+32))+rsp] - lea rdi,[192+rsp] - call __ecp_nistz256_mul_montq - - - + lea rdi,[192+rsp] ; U1*H^2 + call __ecp_nistz256_mul_montq ; p256_mul_mont(U2, U1, Hsqr); +; lea 192(%rsp), %rsi +; lea 32(%rsp), %rdi # 2*U1*H^2 +; call __ecp_nistz256_mul_by_2 # ecp_nistz256_mul_by_2(Hsqr, U2); xor r11,r11 - add r12,r12 + add r12,r12 ; a0:a3+a0:a3 lea rsi,[96+rsp] adc r13,r13 mov rax,r12 @@ -3146,11 +3147,11 @@ $L$add_proceedq: cmovc r9,r10 mov r10,QWORD[24+rsi] - call __ecp_nistz256_subq + call __ecp_nistz256_subq ; p256_sub(res_x, Rsqr, Hsqr); lea rbx,[128+rsp] lea rdi,[288+rsp] - call __ecp_nistz256_sub_fromq + call __ecp_nistz256_sub_fromq ; p256_sub(res_x, res_x, Hcub); mov rax,QWORD[((192+0))+rsp] mov rbp,QWORD[((192+8))+rsp] @@ -3158,10 +3159,10 @@ $L$add_proceedq: mov r10,QWORD[((192+24))+rsp] lea rdi,[320+rsp] - call __ecp_nistz256_subq + call __ecp_nistz256_subq ; p256_sub(res_y, U2, res_x); - mov QWORD[rdi],r12 - mov QWORD[8+rdi],r13 + mov QWORD[rdi],r12 ; save the result, as + mov QWORD[8+rdi],r13 ; __ecp_nistz256_sub doesn't mov QWORD[16+rdi],r8 mov QWORD[24+rdi],r9 mov rax,QWORD[128+rsp] @@ -3172,7 +3173,7 @@ $L$add_proceedq: mov r11,QWORD[((16+224))+rsp] mov r12,QWORD[((24+224))+rsp] lea rdi,[256+rsp] - call __ecp_nistz256_mul_montq + call __ecp_nistz256_mul_montq ; p256_mul_mont(S2, S1, Hcub); mov rax,QWORD[320+rsp] lea rbx,[320+rsp] @@ -3182,15 +3183,15 @@ $L$add_proceedq: mov r11,QWORD[((16+64))+rsp] mov r12,QWORD[((24+64))+rsp] lea rdi,[320+rsp] - call __ecp_nistz256_mul_montq + call __ecp_nistz256_mul_montq ; p256_mul_mont(res_y, R, res_y); lea rbx,[256+rsp] lea rdi,[320+rsp] - call __ecp_nistz256_sub_fromq + call __ecp_nistz256_sub_fromq ; p256_sub(res_y, res_y, S2); - movq rdi,xmm0 + movq rdi,xmm0 ; restore %rdi - movdqa xmm0,xmm5 + movdqa xmm0,xmm5 ; copy_conditional(res_z, in2_z, in1infty); movdqa xmm1,xmm5 pandn xmm0,XMMWORD[352+rsp] movdqa xmm2,xmm5 @@ -3201,7 +3202,7 @@ $L$add_proceedq: por xmm2,xmm0 por xmm3,xmm1 - movdqa xmm0,xmm4 + movdqa xmm0,xmm4 ; copy_conditional(res_z, in1_z, in2infty); movdqa xmm1,xmm4 pandn xmm0,xmm2 movdqa xmm2,xmm4 @@ -3214,7 +3215,7 @@ $L$add_proceedq: movdqu XMMWORD[64+rdi],xmm2 movdqu XMMWORD[80+rdi],xmm3 - movdqa xmm0,xmm5 + movdqa xmm0,xmm5 ; copy_conditional(res_x, in2_x, in1infty); movdqa xmm1,xmm5 pandn xmm0,XMMWORD[288+rsp] movdqa xmm2,xmm5 @@ -3225,7 +3226,7 @@ $L$add_proceedq: por xmm2,xmm0 por xmm3,xmm1 - movdqa xmm0,xmm4 + movdqa xmm0,xmm4 ; copy_conditional(res_x, in1_x, in2infty); movdqa xmm1,xmm4 pandn xmm0,xmm2 movdqa xmm2,xmm4 @@ -3238,7 +3239,7 @@ $L$add_proceedq: movdqu XMMWORD[rdi],xmm2 movdqu XMMWORD[16+rdi],xmm3 - movdqa xmm0,xmm5 + movdqa xmm0,xmm5 ; copy_conditional(res_y, in2_y, in1infty); movdqa xmm1,xmm5 pandn xmm0,XMMWORD[320+rsp] movdqa xmm2,xmm5 @@ -3249,7 +3250,7 @@ $L$add_proceedq: por xmm2,xmm0 por xmm3,xmm1 - movdqa xmm0,xmm4 + movdqa xmm0,xmm4 ; copy_conditional(res_y, in1_y, in2infty); movdqa xmm1,xmm4 pandn xmm0,xmm2 movdqa xmm2,xmm4 @@ -3316,14 +3317,14 @@ _CET_ENDBR $L$add_affineq_body: - movdqu xmm0,XMMWORD[rsi] - mov rbx,rdx + movdqu xmm0,XMMWORD[rsi] ; copy *(P256_POINT *)%rsi + mov rbx,rdx ; reassign movdqu xmm1,XMMWORD[16+rsi] movdqu xmm2,XMMWORD[32+rsi] movdqu xmm3,XMMWORD[48+rsi] movdqu xmm4,XMMWORD[64+rsi] movdqu xmm5,XMMWORD[80+rsi] - mov rax,QWORD[((64+0))+rsi] + mov rax,QWORD[((64+0))+rsi] ; load original in1_z mov r14,QWORD[((64+8))+rsi] mov r15,QWORD[((64+16))+rsi] mov r8,QWORD[((64+24))+rsi] @@ -3335,7 +3336,7 @@ $L$add_affineq_body: movdqa XMMWORD[(384+16)+rsp],xmm5 por xmm5,xmm4 - movdqu xmm0,XMMWORD[rbx] + movdqu xmm0,XMMWORD[rbx] ; copy *(P256_POINT_AFFINE *)%rbx pshufd xmm3,xmm5,0xb1 movdqu xmm1,XMMWORD[16+rbx] movdqu xmm2,XMMWORD[32+rbx] @@ -3345,7 +3346,7 @@ $L$add_affineq_body: pshufd xmm4,xmm5,0x1e movdqa XMMWORD[(416+16)+rsp],xmm1 por xmm1,xmm0 - movq xmm0,rdi + movq xmm0,rdi ; save %rdi movdqa XMMWORD[448+rsp],xmm2 movdqa XMMWORD[(448+16)+rsp],xmm3 por xmm3,xmm2 @@ -3353,33 +3354,33 @@ $L$add_affineq_body: pxor xmm4,xmm4 por xmm3,xmm1 - lea rsi,[((64-0))+rsi] - lea rdi,[32+rsp] - call __ecp_nistz256_sqr_montq + lea rsi,[((64-0))+rsi] ; %rsi is still valid + lea rdi,[32+rsp] ; Z1^2 + call __ecp_nistz256_sqr_montq ; p256_sqr_mont(Z1sqr, in1_z); pcmpeqd xmm5,xmm4 pshufd xmm4,xmm3,0xb1 - mov rax,QWORD[rbx] - - mov r9,r12 + mov rax,QWORD[rbx] ; %rbx is still valid +; lea 0x00(%rbx), %rbx + mov r9,r12 ; harmonize sqr output and mul input por xmm4,xmm3 - pshufd xmm5,xmm5,0 + pshufd xmm5,xmm5,0 ; in1infty pshufd xmm3,xmm4,0x1e mov r10,r13 por xmm4,xmm3 pxor xmm3,xmm3 mov r11,r14 pcmpeqd xmm4,xmm3 - pshufd xmm4,xmm4,0 + pshufd xmm4,xmm4,0 ; in2infty lea rsi,[((32-0))+rsp] mov r12,r15 - lea rdi,[rsp] - call __ecp_nistz256_mul_montq + lea rdi,[rsp] ; U2 = X2*Z1^2 + call __ecp_nistz256_mul_montq ; p256_mul_mont(U2, Z1sqr, in2_x); lea rbx,[320+rsp] - lea rdi,[64+rsp] - call __ecp_nistz256_sub_fromq + lea rdi,[64+rsp] ; H = U2 - U1 + call __ecp_nistz256_sub_fromq ; p256_sub(H, U2, in1_x); mov rax,QWORD[384+rsp] lea rbx,[384+rsp] @@ -3388,8 +3389,8 @@ $L$add_affineq_body: lea rsi,[((0+32))+rsp] mov r11,QWORD[((16+32))+rsp] mov r12,QWORD[((24+32))+rsp] - lea rdi,[32+rsp] - call __ecp_nistz256_mul_montq + lea rdi,[32+rsp] ; S2 = Z1^3 + call __ecp_nistz256_mul_montq ; p256_mul_mont(S2, Z1sqr, in1_z); mov rax,QWORD[384+rsp] lea rbx,[384+rsp] @@ -3398,8 +3399,8 @@ $L$add_affineq_body: lea rsi,[((0+64))+rsp] mov r11,QWORD[((16+64))+rsp] mov r12,QWORD[((24+64))+rsp] - lea rdi,[288+rsp] - call __ecp_nistz256_mul_montq + lea rdi,[288+rsp] ; Z3 = H*Z1*Z2 + call __ecp_nistz256_mul_montq ; p256_mul_mont(res_z, H, in1_z); mov rax,QWORD[448+rsp] lea rbx,[448+rsp] @@ -3408,28 +3409,28 @@ $L$add_affineq_body: lea rsi,[((0+32))+rsp] mov r11,QWORD[((16+32))+rsp] mov r12,QWORD[((24+32))+rsp] - lea rdi,[32+rsp] - call __ecp_nistz256_mul_montq + lea rdi,[32+rsp] ; S2 = Y2*Z1^3 + call __ecp_nistz256_mul_montq ; p256_mul_mont(S2, S2, in2_y); lea rbx,[352+rsp] - lea rdi,[96+rsp] - call __ecp_nistz256_sub_fromq + lea rdi,[96+rsp] ; R = S2 - S1 + call __ecp_nistz256_sub_fromq ; p256_sub(R, S2, in1_y); mov rax,QWORD[((0+64))+rsp] mov r14,QWORD[((8+64))+rsp] lea rsi,[((0+64))+rsp] mov r15,QWORD[((16+64))+rsp] mov r8,QWORD[((24+64))+rsp] - lea rdi,[128+rsp] - call __ecp_nistz256_sqr_montq + lea rdi,[128+rsp] ; H^2 + call __ecp_nistz256_sqr_montq ; p256_sqr_mont(Hsqr, H); mov rax,QWORD[((0+96))+rsp] mov r14,QWORD[((8+96))+rsp] lea rsi,[((0+96))+rsp] mov r15,QWORD[((16+96))+rsp] mov r8,QWORD[((24+96))+rsp] - lea rdi,[192+rsp] - call __ecp_nistz256_sqr_montq + lea rdi,[192+rsp] ; R^2 + call __ecp_nistz256_sqr_montq ; p256_sqr_mont(Rsqr, R); mov rax,QWORD[128+rsp] lea rbx,[128+rsp] @@ -3438,8 +3439,8 @@ $L$add_affineq_body: lea rsi,[((0+64))+rsp] mov r11,QWORD[((16+64))+rsp] mov r12,QWORD[((24+64))+rsp] - lea rdi,[160+rsp] - call __ecp_nistz256_mul_montq + lea rdi,[160+rsp] ; H^3 + call __ecp_nistz256_mul_montq ; p256_mul_mont(Hcub, Hsqr, H); mov rax,QWORD[320+rsp] lea rbx,[320+rsp] @@ -3448,14 +3449,14 @@ $L$add_affineq_body: lea rsi,[((0+128))+rsp] mov r11,QWORD[((16+128))+rsp] mov r12,QWORD[((24+128))+rsp] - lea rdi,[rsp] - call __ecp_nistz256_mul_montq - - - + lea rdi,[rsp] ; U1*H^2 + call __ecp_nistz256_mul_montq ; p256_mul_mont(U2, in1_x, Hsqr); +; lea 0(%rsp), %rsi +; lea 128(%rsp), %rdi # 2*U1*H^2 +; call __ecp_nistz256_mul_by_2 # ecp_nistz256_mul_by_2(Hsqr, U2); xor r11,r11 - add r12,r12 + add r12,r12 ; a0:a3+a0:a3 lea rsi,[192+rsp] adc r13,r13 mov rax,r12 @@ -3481,11 +3482,11 @@ $L$add_affineq_body: cmovc r9,r10 mov r10,QWORD[24+rsi] - call __ecp_nistz256_subq + call __ecp_nistz256_subq ; p256_sub(res_x, Rsqr, Hsqr); lea rbx,[160+rsp] lea rdi,[224+rsp] - call __ecp_nistz256_sub_fromq + call __ecp_nistz256_sub_fromq ; p256_sub(res_x, res_x, Hcub); mov rax,QWORD[((0+0))+rsp] mov rbp,QWORD[((0+8))+rsp] @@ -3493,10 +3494,10 @@ $L$add_affineq_body: mov r10,QWORD[((0+24))+rsp] lea rdi,[64+rsp] - call __ecp_nistz256_subq + call __ecp_nistz256_subq ; p256_sub(H, U2, res_x); - mov QWORD[rdi],r12 - mov QWORD[8+rdi],r13 + mov QWORD[rdi],r12 ; save the result, as + mov QWORD[8+rdi],r13 ; __ecp_nistz256_sub doesn't mov QWORD[16+rdi],r8 mov QWORD[24+rdi],r9 mov rax,QWORD[352+rsp] @@ -3507,7 +3508,7 @@ $L$add_affineq_body: mov r11,QWORD[((16+160))+rsp] mov r12,QWORD[((24+160))+rsp] lea rdi,[32+rsp] - call __ecp_nistz256_mul_montq + call __ecp_nistz256_mul_montq ; p256_mul_mont(S2, Hcub, in1_y); mov rax,QWORD[96+rsp] lea rbx,[96+rsp] @@ -3517,15 +3518,15 @@ $L$add_affineq_body: mov r11,QWORD[((16+64))+rsp] mov r12,QWORD[((24+64))+rsp] lea rdi,[64+rsp] - call __ecp_nistz256_mul_montq + call __ecp_nistz256_mul_montq ; p256_mul_mont(H, H, R); lea rbx,[32+rsp] lea rdi,[256+rsp] - call __ecp_nistz256_sub_fromq + call __ecp_nistz256_sub_fromq ; p256_sub(res_y, H, S2); - movq rdi,xmm0 + movq rdi,xmm0 ; restore %rdi - movdqa xmm0,xmm5 + movdqa xmm0,xmm5 ; copy_conditional(res_z, ONE, in1infty); movdqa xmm1,xmm5 pandn xmm0,XMMWORD[288+rsp] movdqa xmm2,xmm5 @@ -3536,7 +3537,7 @@ $L$add_affineq_body: por xmm2,xmm0 por xmm3,xmm1 - movdqa xmm0,xmm4 + movdqa xmm0,xmm4 ; copy_conditional(res_z, in1_z, in2infty); movdqa xmm1,xmm4 pandn xmm0,xmm2 movdqa xmm2,xmm4 @@ -3549,7 +3550,7 @@ $L$add_affineq_body: movdqu XMMWORD[64+rdi],xmm2 movdqu XMMWORD[80+rdi],xmm3 - movdqa xmm0,xmm5 + movdqa xmm0,xmm5 ; copy_conditional(res_x, in2_x, in1infty); movdqa xmm1,xmm5 pandn xmm0,XMMWORD[224+rsp] movdqa xmm2,xmm5 @@ -3560,7 +3561,7 @@ $L$add_affineq_body: por xmm2,xmm0 por xmm3,xmm1 - movdqa xmm0,xmm4 + movdqa xmm0,xmm4 ; copy_conditional(res_x, in1_x, in2infty); movdqa xmm1,xmm4 pandn xmm0,xmm2 movdqa xmm2,xmm4 @@ -3573,7 +3574,7 @@ $L$add_affineq_body: movdqu XMMWORD[rdi],xmm2 movdqu XMMWORD[16+rdi],xmm3 - movdqa xmm0,xmm5 + movdqa xmm0,xmm5 ; copy_conditional(res_y, in2_y, in1infty); movdqa xmm1,xmm5 pandn xmm0,XMMWORD[256+rsp] movdqa xmm2,xmm5 @@ -3584,7 +3585,7 @@ $L$add_affineq_body: por xmm2,xmm0 por xmm3,xmm1 - movdqa xmm0,xmm4 + movdqa xmm0,xmm4 ; copy_conditional(res_y, in1_y, in2infty); movdqa xmm1,xmm4 pandn xmm0,xmm2 movdqa xmm2,xmm4 @@ -3725,7 +3726,7 @@ ALIGN 32 __ecp_nistz256_mul_by_2x: xor r11,r11 - adc r12,r12 + adc r12,r12 ; a0:a3+a0:a3 adc r13,r13 mov rax,r12 adc r8,r8 @@ -3785,10 +3786,10 @@ _CET_ENDBR $L$point_doublex_body: $L$point_double_shortcutx: - movdqu xmm0,XMMWORD[rsi] - mov rbx,rsi + movdqu xmm0,XMMWORD[rsi] ; copy *(P256_POINT *)%rsi.x + mov rbx,rsi ; backup copy movdqu xmm1,XMMWORD[16+rsi] - mov r12,QWORD[((32+0))+rsi] + mov r12,QWORD[((32+0))+rsi] ; load in_y in "5-4-0-1" order mov r13,QWORD[((32+8))+rsi] mov r8,QWORD[((32+16))+rsi] mov r9,QWORD[((32+24))+rsi] @@ -3803,7 +3804,7 @@ $L$point_double_shortcutx: movq xmm2,r11 lea rdi,[rsp] - call __ecp_nistz256_mul_by_2x + call __ecp_nistz256_mul_by_2x ; p256_mul_by_2(S, in_y); mov rdx,QWORD[((64+0))+rsi] mov r14,QWORD[((64+8))+rsi] @@ -3811,7 +3812,7 @@ $L$point_double_shortcutx: mov r8,QWORD[((64+24))+rsi] lea rsi,[((64-128))+rsi] lea rdi,[64+rsp] - call __ecp_nistz256_sqr_montx + call __ecp_nistz256_sqr_montx ; p256_sqr_mont(Zsqr, in_z); mov rdx,QWORD[((0+0))+rsp] mov r14,QWORD[((8+0))+rsp] @@ -3819,9 +3820,9 @@ $L$point_double_shortcutx: mov r15,QWORD[((16+0))+rsp] mov r8,QWORD[((24+0))+rsp] lea rdi,[rsp] - call __ecp_nistz256_sqr_montx + call __ecp_nistz256_sqr_montx ; p256_sqr_mont(S, S); - mov rdx,QWORD[32+rbx] + mov rdx,QWORD[32+rbx] ; %rbx is still valid mov r9,QWORD[((64+0))+rbx] mov r10,QWORD[((64+8))+rbx] mov r11,QWORD[((64+16))+rbx] @@ -3829,24 +3830,24 @@ $L$point_double_shortcutx: lea rsi,[((64-128))+rbx] lea rbx,[32+rbx] movq rdi,xmm2 - call __ecp_nistz256_mul_montx - call __ecp_nistz256_mul_by_2x + call __ecp_nistz256_mul_montx ; p256_mul_mont(res_z, in_z, in_y); + call __ecp_nistz256_mul_by_2x ; p256_mul_by_2(res_z, res_z); - mov r12,QWORD[((96+0))+rsp] + mov r12,QWORD[((96+0))+rsp] ; "5-4-0-1" order mov r13,QWORD[((96+8))+rsp] lea rbx,[64+rsp] mov r8,QWORD[((96+16))+rsp] mov r9,QWORD[((96+24))+rsp] lea rdi,[32+rsp] - call __ecp_nistz256_add_tox + call __ecp_nistz256_add_tox ; p256_add(M, in_x, Zsqr); - mov r12,QWORD[((96+0))+rsp] + mov r12,QWORD[((96+0))+rsp] ; "5-4-0-1" order mov r13,QWORD[((96+8))+rsp] lea rbx,[64+rsp] mov r8,QWORD[((96+16))+rsp] mov r9,QWORD[((96+24))+rsp] lea rdi,[64+rsp] - call __ecp_nistz256_sub_fromx + call __ecp_nistz256_sub_fromx ; p256_sub(Zsqr, in_x, Zsqr); mov rdx,QWORD[((0+0))+rsp] mov r14,QWORD[((8+0))+rsp] @@ -3854,7 +3855,7 @@ $L$point_double_shortcutx: mov r15,QWORD[((16+0))+rsp] mov r8,QWORD[((24+0))+rsp] movq rdi,xmm1 - call __ecp_nistz256_sqr_montx + call __ecp_nistz256_sqr_montx ; p256_sqr_mont(res_y, S); xor r9,r9 mov rax,r12 add r12,-1 @@ -3865,7 +3866,7 @@ $L$point_double_shortcutx: mov r8,r15 adc r15,rbp adc r9,0 - xor rsi,rsi + xor rsi,rsi ; borrow %rsi test rax,1 cmovz r12,rax @@ -3874,7 +3875,7 @@ $L$point_double_shortcutx: cmovz r15,r8 cmovz r9,rsi - mov rax,r13 + mov rax,r13 ; a0:a3>>1 shr r12,1 shl rax,63 mov r10,r14 @@ -3901,14 +3902,14 @@ $L$point_double_shortcutx: mov r11,QWORD[((16+32))+rsp] mov r12,QWORD[((24+32))+rsp] lea rdi,[32+rsp] - call __ecp_nistz256_mul_montx + call __ecp_nistz256_mul_montx ; p256_mul_mont(M, M, Zsqr); lea rdi,[128+rsp] call __ecp_nistz256_mul_by_2x lea rbx,[32+rsp] lea rdi,[32+rsp] - call __ecp_nistz256_add_tox + call __ecp_nistz256_add_tox ; p256_mul_by_3(M, M); mov rdx,QWORD[96+rsp] lea rbx,[96+rsp] @@ -3918,10 +3919,10 @@ $L$point_double_shortcutx: mov r11,QWORD[((16+0))+rsp] mov r12,QWORD[((24+0))+rsp] lea rdi,[rsp] - call __ecp_nistz256_mul_montx + call __ecp_nistz256_mul_montx ; p256_mul_mont(S, S, in_x); lea rdi,[128+rsp] - call __ecp_nistz256_mul_by_2x + call __ecp_nistz256_mul_by_2x ; p256_mul_by_2(tmp0, S); mov rdx,QWORD[((0+32))+rsp] mov r14,QWORD[((8+32))+rsp] @@ -3929,27 +3930,27 @@ $L$point_double_shortcutx: mov r15,QWORD[((16+32))+rsp] mov r8,QWORD[((24+32))+rsp] movq rdi,xmm0 - call __ecp_nistz256_sqr_montx + call __ecp_nistz256_sqr_montx ; p256_sqr_mont(res_x, M); lea rbx,[128+rsp] - mov r8,r14 + mov r8,r14 ; harmonize sqr output and sub input mov r9,r15 mov r14,rsi mov r15,rbp - call __ecp_nistz256_sub_fromx + call __ecp_nistz256_sub_fromx ; p256_sub(res_x, res_x, tmp0); mov rax,QWORD[((0+0))+rsp] mov rbp,QWORD[((0+8))+rsp] mov rcx,QWORD[((0+16))+rsp] - mov r10,QWORD[((0+24))+rsp] + mov r10,QWORD[((0+24))+rsp] ; "4-5-0-1" order lea rdi,[rsp] - call __ecp_nistz256_subx + call __ecp_nistz256_subx ; p256_sub(S, S, res_x); mov rdx,QWORD[32+rsp] lea rbx,[32+rsp] - mov r14,r12 + mov r14,r12 ; harmonize sub output and mul input xor ecx,ecx - mov QWORD[((0+0))+rsp],r12 + mov QWORD[((0+0))+rsp],r12 ; have to save:-( mov r10,r13 mov QWORD[((0+8))+rsp],r13 cmovz r11,r8 @@ -3959,11 +3960,11 @@ $L$point_double_shortcutx: mov QWORD[((0+24))+rsp],r9 mov r9,r14 lea rdi,[rsp] - call __ecp_nistz256_mul_montx + call __ecp_nistz256_mul_montx ; p256_mul_mont(S, S, M); movq rbx,xmm1 movq rdi,xmm1 - call __ecp_nistz256_sub_fromx + call __ecp_nistz256_sub_fromx ; p256_sub(res_y, S, res_y); lea rsi,[((160+56))+rsp] @@ -4018,14 +4019,14 @@ _CET_ENDBR $L$point_addx_body: - movdqu xmm0,XMMWORD[rsi] + movdqu xmm0,XMMWORD[rsi] ; copy *(P256_POINT *)%rsi movdqu xmm1,XMMWORD[16+rsi] movdqu xmm2,XMMWORD[32+rsi] movdqu xmm3,XMMWORD[48+rsi] movdqu xmm4,XMMWORD[64+rsi] movdqu xmm5,XMMWORD[80+rsi] - mov rbx,rsi - mov rsi,rdx + mov rbx,rsi ; reassign + mov rsi,rdx ; reassign movdqa XMMWORD[384+rsp],xmm0 movdqa XMMWORD[(384+16)+rsp],xmm1 movdqa XMMWORD[416+rsp],xmm2 @@ -4034,54 +4035,54 @@ $L$point_addx_body: movdqa XMMWORD[(448+16)+rsp],xmm5 por xmm5,xmm4 - movdqu xmm0,XMMWORD[rsi] + movdqu xmm0,XMMWORD[rsi] ; copy *(P256_POINT *)%rbx pshufd xmm3,xmm5,0xb1 movdqu xmm1,XMMWORD[16+rsi] movdqu xmm2,XMMWORD[32+rsi] por xmm5,xmm3 movdqu xmm3,XMMWORD[48+rsi] - mov rdx,QWORD[((64+0))+rsi] + mov rdx,QWORD[((64+0))+rsi] ; load original in2_z mov r14,QWORD[((64+8))+rsi] mov r15,QWORD[((64+16))+rsi] mov r8,QWORD[((64+24))+rsi] movdqa XMMWORD[480+rsp],xmm0 pshufd xmm4,xmm5,0x1e movdqa XMMWORD[(480+16)+rsp],xmm1 - movdqu xmm0,XMMWORD[64+rsi] + movdqu xmm0,XMMWORD[64+rsi] ; in2_z again movdqu xmm1,XMMWORD[80+rsi] movdqa XMMWORD[512+rsp],xmm2 movdqa XMMWORD[(512+16)+rsp],xmm3 por xmm5,xmm4 pxor xmm4,xmm4 por xmm1,xmm0 - movq xmm0,rdi + movq xmm0,rdi ; save %rdi - lea rsi,[((64-128))+rsi] - mov QWORD[((544+0))+rsp],rdx + lea rsi,[((64-128))+rsi] ; %rsi is still valid + mov QWORD[((544+0))+rsp],rdx ; make in2_z copy mov QWORD[((544+8))+rsp],r14 mov QWORD[((544+16))+rsp],r15 mov QWORD[((544+24))+rsp],r8 - lea rdi,[96+rsp] - call __ecp_nistz256_sqr_montx + lea rdi,[96+rsp] ; Z2^2 + call __ecp_nistz256_sqr_montx ; p256_sqr_mont(Z2sqr, in2_z); pcmpeqd xmm5,xmm4 pshufd xmm4,xmm1,0xb1 por xmm4,xmm1 - pshufd xmm5,xmm5,0 + pshufd xmm5,xmm5,0 ; in1infty pshufd xmm3,xmm4,0x1e por xmm4,xmm3 pxor xmm3,xmm3 pcmpeqd xmm4,xmm3 - pshufd xmm4,xmm4,0 - mov rdx,QWORD[((64+0))+rbx] + pshufd xmm4,xmm4,0 ; in2infty + mov rdx,QWORD[((64+0))+rbx] ; load original in1_z mov r14,QWORD[((64+8))+rbx] mov r15,QWORD[((64+16))+rbx] mov r8,QWORD[((64+24))+rbx] movq xmm1,rbx lea rsi,[((64-128))+rbx] - lea rdi,[32+rsp] - call __ecp_nistz256_sqr_montx + lea rdi,[32+rsp] ; Z1^2 + call __ecp_nistz256_sqr_montx ; p256_sqr_mont(Z1sqr, in1_z); mov rdx,QWORD[544+rsp] lea rbx,[544+rsp] @@ -4090,8 +4091,8 @@ $L$point_addx_body: lea rsi,[((-128+96))+rsp] mov r11,QWORD[((16+96))+rsp] mov r12,QWORD[((24+96))+rsp] - lea rdi,[224+rsp] - call __ecp_nistz256_mul_montx + lea rdi,[224+rsp] ; S1 = Z2^3 + call __ecp_nistz256_mul_montx ; p256_mul_mont(S1, Z2sqr, in2_z); mov rdx,QWORD[448+rsp] lea rbx,[448+rsp] @@ -4100,8 +4101,8 @@ $L$point_addx_body: lea rsi,[((-128+32))+rsp] mov r11,QWORD[((16+32))+rsp] mov r12,QWORD[((24+32))+rsp] - lea rdi,[256+rsp] - call __ecp_nistz256_mul_montx + lea rdi,[256+rsp] ; S2 = Z1^3 + call __ecp_nistz256_mul_montx ; p256_mul_mont(S2, Z1sqr, in1_z); mov rdx,QWORD[416+rsp] lea rbx,[416+rsp] @@ -4110,8 +4111,8 @@ $L$point_addx_body: lea rsi,[((-128+224))+rsp] mov r11,QWORD[((16+224))+rsp] mov r12,QWORD[((24+224))+rsp] - lea rdi,[224+rsp] - call __ecp_nistz256_mul_montx + lea rdi,[224+rsp] ; S1 = Y1*Z2^3 + call __ecp_nistz256_mul_montx ; p256_mul_mont(S1, S1, in1_y); mov rdx,QWORD[512+rsp] lea rbx,[512+rsp] @@ -4120,18 +4121,18 @@ $L$point_addx_body: lea rsi,[((-128+256))+rsp] mov r11,QWORD[((16+256))+rsp] mov r12,QWORD[((24+256))+rsp] - lea rdi,[256+rsp] - call __ecp_nistz256_mul_montx + lea rdi,[256+rsp] ; S2 = Y2*Z1^3 + call __ecp_nistz256_mul_montx ; p256_mul_mont(S2, S2, in2_y); lea rbx,[224+rsp] - lea rdi,[64+rsp] - call __ecp_nistz256_sub_fromx + lea rdi,[64+rsp] ; R = S2 - S1 + call __ecp_nistz256_sub_fromx ; p256_sub(R, S2, S1); - or r12,r13 + or r12,r13 ; see if result is zero movdqa xmm2,xmm4 or r12,r8 or r12,r9 - por xmm2,xmm5 + por xmm2,xmm5 ; in1infty || in2infty movq xmm3,r12 mov rdx,QWORD[384+rsp] @@ -4141,8 +4142,8 @@ $L$point_addx_body: lea rsi,[((-128+96))+rsp] mov r11,QWORD[((16+96))+rsp] mov r12,QWORD[((24+96))+rsp] - lea rdi,[160+rsp] - call __ecp_nistz256_mul_montx + lea rdi,[160+rsp] ; U1 = X1*Z2^2 + call __ecp_nistz256_mul_montx ; p256_mul_mont(U1, in1_x, Z2sqr); mov rdx,QWORD[480+rsp] lea rbx,[480+rsp] @@ -4151,34 +4152,34 @@ $L$point_addx_body: lea rsi,[((-128+32))+rsp] mov r11,QWORD[((16+32))+rsp] mov r12,QWORD[((24+32))+rsp] - lea rdi,[192+rsp] - call __ecp_nistz256_mul_montx + lea rdi,[192+rsp] ; U2 = X2*Z1^2 + call __ecp_nistz256_mul_montx ; p256_mul_mont(U2, in2_x, Z1sqr); lea rbx,[160+rsp] - lea rdi,[rsp] - call __ecp_nistz256_sub_fromx + lea rdi,[rsp] ; H = U2 - U1 + call __ecp_nistz256_sub_fromx ; p256_sub(H, U2, U1); - or r12,r13 + or r12,r13 ; see if result is zero or r12,r8 - or r12,r9 + or r12,r9 ; !is_equal(U1, U2) movq r8,xmm2 movq r9,xmm3 or r12,r8 - DB 0x3e - jnz NEAR $L$add_proceedx - - + DB 0x3e ; predict taken + jnz NEAR $L$add_proceedx ; !is_equal(U1, U2) || in1infty || in2infty +; We now know A = B or A = -B and neither is infinity. Compare the +; y-coordinates via S1 and S2. test r9,r9 - jz NEAR $L$add_doublex - - - - - - - movq rdi,xmm0 + jz NEAR $L$add_doublex ; is_equal(S1, S2) + +; A = -B, so the result is infinity. +; +; TODO(davidben): Does .Ladd_proceed handle this case? It seems to, in +; which case we should eliminate this special-case and simplify the +; timing analysis. + movq rdi,xmm0 ; restore %rdi pxor xmm0,xmm0 movdqu XMMWORD[rdi],xmm0 movdqu XMMWORD[16+rdi],xmm0 @@ -4190,9 +4191,9 @@ $L$point_addx_body: ALIGN 32 $L$add_doublex: - movq rsi,xmm1 - movq rdi,xmm0 - add rsp,416 + movq rsi,xmm1 ; restore %rsi + movq rdi,xmm0 ; restore %rdi + add rsp,416 ; difference in frame sizes jmp NEAR $L$point_double_shortcutx @@ -4204,8 +4205,8 @@ $L$add_proceedx: lea rsi,[((-128+64))+rsp] mov r15,QWORD[((16+64))+rsp] mov r8,QWORD[((24+64))+rsp] - lea rdi,[96+rsp] - call __ecp_nistz256_sqr_montx + lea rdi,[96+rsp] ; R^2 + call __ecp_nistz256_sqr_montx ; p256_sqr_mont(Rsqr, R); mov rdx,QWORD[448+rsp] lea rbx,[448+rsp] @@ -4214,16 +4215,16 @@ $L$add_proceedx: lea rsi,[((-128+0))+rsp] mov r11,QWORD[((16+0))+rsp] mov r12,QWORD[((24+0))+rsp] - lea rdi,[352+rsp] - call __ecp_nistz256_mul_montx + lea rdi,[352+rsp] ; Z3 = H*Z1*Z2 + call __ecp_nistz256_mul_montx ; p256_mul_mont(res_z, H, in1_z); mov rdx,QWORD[((0+0))+rsp] mov r14,QWORD[((8+0))+rsp] lea rsi,[((-128+0))+rsp] mov r15,QWORD[((16+0))+rsp] mov r8,QWORD[((24+0))+rsp] - lea rdi,[32+rsp] - call __ecp_nistz256_sqr_montx + lea rdi,[32+rsp] ; H^2 + call __ecp_nistz256_sqr_montx ; p256_sqr_mont(Hsqr, H); mov rdx,QWORD[544+rsp] lea rbx,[544+rsp] @@ -4232,8 +4233,8 @@ $L$add_proceedx: lea rsi,[((-128+352))+rsp] mov r11,QWORD[((16+352))+rsp] mov r12,QWORD[((24+352))+rsp] - lea rdi,[352+rsp] - call __ecp_nistz256_mul_montx + lea rdi,[352+rsp] ; Z3 = H*Z1*Z2 + call __ecp_nistz256_mul_montx ; p256_mul_mont(res_z, res_z, in2_z); mov rdx,QWORD[rsp] lea rbx,[rsp] @@ -4242,8 +4243,8 @@ $L$add_proceedx: lea rsi,[((-128+32))+rsp] mov r11,QWORD[((16+32))+rsp] mov r12,QWORD[((24+32))+rsp] - lea rdi,[128+rsp] - call __ecp_nistz256_mul_montx + lea rdi,[128+rsp] ; H^3 + call __ecp_nistz256_mul_montx ; p256_mul_mont(Hcub, Hsqr, H); mov rdx,QWORD[160+rsp] lea rbx,[160+rsp] @@ -4252,14 +4253,14 @@ $L$add_proceedx: lea rsi,[((-128+32))+rsp] mov r11,QWORD[((16+32))+rsp] mov r12,QWORD[((24+32))+rsp] - lea rdi,[192+rsp] - call __ecp_nistz256_mul_montx - - - + lea rdi,[192+rsp] ; U1*H^2 + call __ecp_nistz256_mul_montx ; p256_mul_mont(U2, U1, Hsqr); +; lea 192(%rsp), %rsi +; lea 32(%rsp), %rdi # 2*U1*H^2 +; call __ecp_nistz256_mul_by_2 # ecp_nistz256_mul_by_2(Hsqr, U2); xor r11,r11 - add r12,r12 + add r12,r12 ; a0:a3+a0:a3 lea rsi,[96+rsp] adc r13,r13 mov rax,r12 @@ -4285,11 +4286,11 @@ $L$add_proceedx: cmovc r9,r10 mov r10,QWORD[24+rsi] - call __ecp_nistz256_subx + call __ecp_nistz256_subx ; p256_sub(res_x, Rsqr, Hsqr); lea rbx,[128+rsp] lea rdi,[288+rsp] - call __ecp_nistz256_sub_fromx + call __ecp_nistz256_sub_fromx ; p256_sub(res_x, res_x, Hcub); mov rax,QWORD[((192+0))+rsp] mov rbp,QWORD[((192+8))+rsp] @@ -4297,10 +4298,10 @@ $L$add_proceedx: mov r10,QWORD[((192+24))+rsp] lea rdi,[320+rsp] - call __ecp_nistz256_subx + call __ecp_nistz256_subx ; p256_sub(res_y, U2, res_x); - mov QWORD[rdi],r12 - mov QWORD[8+rdi],r13 + mov QWORD[rdi],r12 ; save the result, as + mov QWORD[8+rdi],r13 ; __ecp_nistz256_sub doesn't mov QWORD[16+rdi],r8 mov QWORD[24+rdi],r9 mov rdx,QWORD[128+rsp] @@ -4311,7 +4312,7 @@ $L$add_proceedx: mov r11,QWORD[((16+224))+rsp] mov r12,QWORD[((24+224))+rsp] lea rdi,[256+rsp] - call __ecp_nistz256_mul_montx + call __ecp_nistz256_mul_montx ; p256_mul_mont(S2, S1, Hcub); mov rdx,QWORD[320+rsp] lea rbx,[320+rsp] @@ -4321,15 +4322,15 @@ $L$add_proceedx: mov r11,QWORD[((16+64))+rsp] mov r12,QWORD[((24+64))+rsp] lea rdi,[320+rsp] - call __ecp_nistz256_mul_montx + call __ecp_nistz256_mul_montx ; p256_mul_mont(res_y, R, res_y); lea rbx,[256+rsp] lea rdi,[320+rsp] - call __ecp_nistz256_sub_fromx + call __ecp_nistz256_sub_fromx ; p256_sub(res_y, res_y, S2); - movq rdi,xmm0 + movq rdi,xmm0 ; restore %rdi - movdqa xmm0,xmm5 + movdqa xmm0,xmm5 ; copy_conditional(res_z, in2_z, in1infty); movdqa xmm1,xmm5 pandn xmm0,XMMWORD[352+rsp] movdqa xmm2,xmm5 @@ -4340,7 +4341,7 @@ $L$add_proceedx: por xmm2,xmm0 por xmm3,xmm1 - movdqa xmm0,xmm4 + movdqa xmm0,xmm4 ; copy_conditional(res_z, in1_z, in2infty); movdqa xmm1,xmm4 pandn xmm0,xmm2 movdqa xmm2,xmm4 @@ -4353,7 +4354,7 @@ $L$add_proceedx: movdqu XMMWORD[64+rdi],xmm2 movdqu XMMWORD[80+rdi],xmm3 - movdqa xmm0,xmm5 + movdqa xmm0,xmm5 ; copy_conditional(res_x, in2_x, in1infty); movdqa xmm1,xmm5 pandn xmm0,XMMWORD[288+rsp] movdqa xmm2,xmm5 @@ -4364,7 +4365,7 @@ $L$add_proceedx: por xmm2,xmm0 por xmm3,xmm1 - movdqa xmm0,xmm4 + movdqa xmm0,xmm4 ; copy_conditional(res_x, in1_x, in2infty); movdqa xmm1,xmm4 pandn xmm0,xmm2 movdqa xmm2,xmm4 @@ -4377,7 +4378,7 @@ $L$add_proceedx: movdqu XMMWORD[rdi],xmm2 movdqu XMMWORD[16+rdi],xmm3 - movdqa xmm0,xmm5 + movdqa xmm0,xmm5 ; copy_conditional(res_y, in2_y, in1infty); movdqa xmm1,xmm5 pandn xmm0,XMMWORD[320+rsp] movdqa xmm2,xmm5 @@ -4388,7 +4389,7 @@ $L$add_proceedx: por xmm2,xmm0 por xmm3,xmm1 - movdqa xmm0,xmm4 + movdqa xmm0,xmm4 ; copy_conditional(res_y, in1_y, in2infty); movdqa xmm1,xmm4 pandn xmm0,xmm2 movdqa xmm2,xmm4 @@ -4455,14 +4456,14 @@ _CET_ENDBR $L$add_affinex_body: - movdqu xmm0,XMMWORD[rsi] - mov rbx,rdx + movdqu xmm0,XMMWORD[rsi] ; copy *(P256_POINT *)%rsi + mov rbx,rdx ; reassign movdqu xmm1,XMMWORD[16+rsi] movdqu xmm2,XMMWORD[32+rsi] movdqu xmm3,XMMWORD[48+rsi] movdqu xmm4,XMMWORD[64+rsi] movdqu xmm5,XMMWORD[80+rsi] - mov rdx,QWORD[((64+0))+rsi] + mov rdx,QWORD[((64+0))+rsi] ; load original in1_z mov r14,QWORD[((64+8))+rsi] mov r15,QWORD[((64+16))+rsi] mov r8,QWORD[((64+24))+rsi] @@ -4474,7 +4475,7 @@ $L$add_affinex_body: movdqa XMMWORD[(384+16)+rsp],xmm5 por xmm5,xmm4 - movdqu xmm0,XMMWORD[rbx] + movdqu xmm0,XMMWORD[rbx] ; copy *(P256_POINT_AFFINE *)%rbx pshufd xmm3,xmm5,0xb1 movdqu xmm1,XMMWORD[16+rbx] movdqu xmm2,XMMWORD[32+rbx] @@ -4484,7 +4485,7 @@ $L$add_affinex_body: pshufd xmm4,xmm5,0x1e movdqa XMMWORD[(416+16)+rsp],xmm1 por xmm1,xmm0 - movq xmm0,rdi + movq xmm0,rdi ; save %rdi movdqa XMMWORD[448+rsp],xmm2 movdqa XMMWORD[(448+16)+rsp],xmm3 por xmm3,xmm2 @@ -4492,33 +4493,33 @@ $L$add_affinex_body: pxor xmm4,xmm4 por xmm3,xmm1 - lea rsi,[((64-128))+rsi] - lea rdi,[32+rsp] - call __ecp_nistz256_sqr_montx + lea rsi,[((64-128))+rsi] ; %rsi is still valid + lea rdi,[32+rsp] ; Z1^2 + call __ecp_nistz256_sqr_montx ; p256_sqr_mont(Z1sqr, in1_z); pcmpeqd xmm5,xmm4 pshufd xmm4,xmm3,0xb1 - mov rdx,QWORD[rbx] - - mov r9,r12 + mov rdx,QWORD[rbx] ; %rbx is still valid +; lea 0x00(%rbx), %rbx + mov r9,r12 ; harmonize sqr output and mul input por xmm4,xmm3 - pshufd xmm5,xmm5,0 + pshufd xmm5,xmm5,0 ; in1infty pshufd xmm3,xmm4,0x1e mov r10,r13 por xmm4,xmm3 pxor xmm3,xmm3 mov r11,r14 pcmpeqd xmm4,xmm3 - pshufd xmm4,xmm4,0 + pshufd xmm4,xmm4,0 ; in2infty lea rsi,[((32-128))+rsp] mov r12,r15 - lea rdi,[rsp] - call __ecp_nistz256_mul_montx + lea rdi,[rsp] ; U2 = X2*Z1^2 + call __ecp_nistz256_mul_montx ; p256_mul_mont(U2, Z1sqr, in2_x); lea rbx,[320+rsp] - lea rdi,[64+rsp] - call __ecp_nistz256_sub_fromx + lea rdi,[64+rsp] ; H = U2 - U1 + call __ecp_nistz256_sub_fromx ; p256_sub(H, U2, in1_x); mov rdx,QWORD[384+rsp] lea rbx,[384+rsp] @@ -4527,8 +4528,8 @@ $L$add_affinex_body: lea rsi,[((-128+32))+rsp] mov r11,QWORD[((16+32))+rsp] mov r12,QWORD[((24+32))+rsp] - lea rdi,[32+rsp] - call __ecp_nistz256_mul_montx + lea rdi,[32+rsp] ; S2 = Z1^3 + call __ecp_nistz256_mul_montx ; p256_mul_mont(S2, Z1sqr, in1_z); mov rdx,QWORD[384+rsp] lea rbx,[384+rsp] @@ -4537,8 +4538,8 @@ $L$add_affinex_body: lea rsi,[((-128+64))+rsp] mov r11,QWORD[((16+64))+rsp] mov r12,QWORD[((24+64))+rsp] - lea rdi,[288+rsp] - call __ecp_nistz256_mul_montx + lea rdi,[288+rsp] ; Z3 = H*Z1*Z2 + call __ecp_nistz256_mul_montx ; p256_mul_mont(res_z, H, in1_z); mov rdx,QWORD[448+rsp] lea rbx,[448+rsp] @@ -4547,28 +4548,28 @@ $L$add_affinex_body: lea rsi,[((-128+32))+rsp] mov r11,QWORD[((16+32))+rsp] mov r12,QWORD[((24+32))+rsp] - lea rdi,[32+rsp] - call __ecp_nistz256_mul_montx + lea rdi,[32+rsp] ; S2 = Y2*Z1^3 + call __ecp_nistz256_mul_montx ; p256_mul_mont(S2, S2, in2_y); lea rbx,[352+rsp] - lea rdi,[96+rsp] - call __ecp_nistz256_sub_fromx + lea rdi,[96+rsp] ; R = S2 - S1 + call __ecp_nistz256_sub_fromx ; p256_sub(R, S2, in1_y); mov rdx,QWORD[((0+64))+rsp] mov r14,QWORD[((8+64))+rsp] lea rsi,[((-128+64))+rsp] mov r15,QWORD[((16+64))+rsp] mov r8,QWORD[((24+64))+rsp] - lea rdi,[128+rsp] - call __ecp_nistz256_sqr_montx + lea rdi,[128+rsp] ; H^2 + call __ecp_nistz256_sqr_montx ; p256_sqr_mont(Hsqr, H); mov rdx,QWORD[((0+96))+rsp] mov r14,QWORD[((8+96))+rsp] lea rsi,[((-128+96))+rsp] mov r15,QWORD[((16+96))+rsp] mov r8,QWORD[((24+96))+rsp] - lea rdi,[192+rsp] - call __ecp_nistz256_sqr_montx + lea rdi,[192+rsp] ; R^2 + call __ecp_nistz256_sqr_montx ; p256_sqr_mont(Rsqr, R); mov rdx,QWORD[128+rsp] lea rbx,[128+rsp] @@ -4577,8 +4578,8 @@ $L$add_affinex_body: lea rsi,[((-128+64))+rsp] mov r11,QWORD[((16+64))+rsp] mov r12,QWORD[((24+64))+rsp] - lea rdi,[160+rsp] - call __ecp_nistz256_mul_montx + lea rdi,[160+rsp] ; H^3 + call __ecp_nistz256_mul_montx ; p256_mul_mont(Hcub, Hsqr, H); mov rdx,QWORD[320+rsp] lea rbx,[320+rsp] @@ -4587,14 +4588,14 @@ $L$add_affinex_body: lea rsi,[((-128+128))+rsp] mov r11,QWORD[((16+128))+rsp] mov r12,QWORD[((24+128))+rsp] - lea rdi,[rsp] - call __ecp_nistz256_mul_montx - - - + lea rdi,[rsp] ; U1*H^2 + call __ecp_nistz256_mul_montx ; p256_mul_mont(U2, in1_x, Hsqr); +; lea 0(%rsp), %rsi +; lea 128(%rsp), %rdi # 2*U1*H^2 +; call __ecp_nistz256_mul_by_2 # ecp_nistz256_mul_by_2(Hsqr, U2); xor r11,r11 - add r12,r12 + add r12,r12 ; a0:a3+a0:a3 lea rsi,[192+rsp] adc r13,r13 mov rax,r12 @@ -4620,11 +4621,11 @@ $L$add_affinex_body: cmovc r9,r10 mov r10,QWORD[24+rsi] - call __ecp_nistz256_subx + call __ecp_nistz256_subx ; p256_sub(res_x, Rsqr, Hsqr); lea rbx,[160+rsp] lea rdi,[224+rsp] - call __ecp_nistz256_sub_fromx + call __ecp_nistz256_sub_fromx ; p256_sub(res_x, res_x, Hcub); mov rax,QWORD[((0+0))+rsp] mov rbp,QWORD[((0+8))+rsp] @@ -4632,10 +4633,10 @@ $L$add_affinex_body: mov r10,QWORD[((0+24))+rsp] lea rdi,[64+rsp] - call __ecp_nistz256_subx + call __ecp_nistz256_subx ; p256_sub(H, U2, res_x); - mov QWORD[rdi],r12 - mov QWORD[8+rdi],r13 + mov QWORD[rdi],r12 ; save the result, as + mov QWORD[8+rdi],r13 ; __ecp_nistz256_sub doesn't mov QWORD[16+rdi],r8 mov QWORD[24+rdi],r9 mov rdx,QWORD[352+rsp] @@ -4646,7 +4647,7 @@ $L$add_affinex_body: mov r11,QWORD[((16+160))+rsp] mov r12,QWORD[((24+160))+rsp] lea rdi,[32+rsp] - call __ecp_nistz256_mul_montx + call __ecp_nistz256_mul_montx ; p256_mul_mont(S2, Hcub, in1_y); mov rdx,QWORD[96+rsp] lea rbx,[96+rsp] @@ -4656,15 +4657,15 @@ $L$add_affinex_body: mov r11,QWORD[((16+64))+rsp] mov r12,QWORD[((24+64))+rsp] lea rdi,[64+rsp] - call __ecp_nistz256_mul_montx + call __ecp_nistz256_mul_montx ; p256_mul_mont(H, H, R); lea rbx,[32+rsp] lea rdi,[256+rsp] - call __ecp_nistz256_sub_fromx + call __ecp_nistz256_sub_fromx ; p256_sub(res_y, H, S2); - movq rdi,xmm0 + movq rdi,xmm0 ; restore %rdi - movdqa xmm0,xmm5 + movdqa xmm0,xmm5 ; copy_conditional(res_z, ONE, in1infty); movdqa xmm1,xmm5 pandn xmm0,XMMWORD[288+rsp] movdqa xmm2,xmm5 @@ -4675,7 +4676,7 @@ $L$add_affinex_body: por xmm2,xmm0 por xmm3,xmm1 - movdqa xmm0,xmm4 + movdqa xmm0,xmm4 ; copy_conditional(res_z, in1_z, in2infty); movdqa xmm1,xmm4 pandn xmm0,xmm2 movdqa xmm2,xmm4 @@ -4688,7 +4689,7 @@ $L$add_affinex_body: movdqu XMMWORD[64+rdi],xmm2 movdqu XMMWORD[80+rdi],xmm3 - movdqa xmm0,xmm5 + movdqa xmm0,xmm5 ; copy_conditional(res_x, in2_x, in1infty); movdqa xmm1,xmm5 pandn xmm0,XMMWORD[224+rsp] movdqa xmm2,xmm5 @@ -4699,7 +4700,7 @@ $L$add_affinex_body: por xmm2,xmm0 por xmm3,xmm1 - movdqa xmm0,xmm4 + movdqa xmm0,xmm4 ; copy_conditional(res_x, in1_x, in2infty); movdqa xmm1,xmm4 pandn xmm0,xmm2 movdqa xmm2,xmm4 @@ -4712,7 +4713,7 @@ $L$add_affinex_body: movdqu XMMWORD[rdi],xmm2 movdqu XMMWORD[16+rdi],xmm3 - movdqa xmm0,xmm5 + movdqa xmm0,xmm5 ; copy_conditional(res_y, in2_y, in1infty); movdqa xmm1,xmm5 pandn xmm0,XMMWORD[256+rsp] movdqa xmm2,xmm5 @@ -4723,7 +4724,7 @@ $L$add_affinex_body: por xmm2,xmm0 por xmm3,xmm1 - movdqa xmm0,xmm4 + movdqa xmm0,xmm4 ; copy_conditional(res_y, in1_y, in2infty); movdqa xmm1,xmm4 pandn xmm0,xmm2 movdqa xmm2,xmm4 @@ -4774,30 +4775,30 @@ short_handler: pushfq sub rsp,64 - mov rax,QWORD[120+r8] - mov rbx,QWORD[248+r8] + mov rax,QWORD[120+r8] ; pull context->Rax + mov rbx,QWORD[248+r8] ; pull context->Rip - mov rsi,QWORD[8+r9] - mov r11,QWORD[56+r9] + mov rsi,QWORD[8+r9] ; disp->ImageBase + mov r11,QWORD[56+r9] ; disp->HandlerData - mov r10d,DWORD[r11] - lea r10,[r10*1+rsi] - cmp rbx,r10 + mov r10d,DWORD[r11] ; HandlerData[0] + lea r10,[r10*1+rsi] ; end of prologue label + cmp rbx,r10 ; context->RipRsp - mov r10d,DWORD[4+r11] - lea r10,[r10*1+rsi] - cmp rbx,r10 + mov r10d,DWORD[4+r11] ; HandlerData[1] + lea r10,[r10*1+rsi] ; epilogue label + cmp rbx,r10 ; context->Rip>=epilogue label jae NEAR $L$common_seh_tail lea rax,[16+rax] mov r12,QWORD[((-8))+rax] mov r13,QWORD[((-16))+rax] - mov QWORD[216+r8],r12 - mov QWORD[224+r8],r13 + mov QWORD[216+r8],r12 ; restore context->R12 + mov QWORD[224+r8],r13 ; restore context->R13 jmp NEAR $L$common_seh_tail @@ -4816,25 +4817,25 @@ full_handler: pushfq sub rsp,64 - mov rax,QWORD[120+r8] - mov rbx,QWORD[248+r8] + mov rax,QWORD[120+r8] ; pull context->Rax + mov rbx,QWORD[248+r8] ; pull context->Rip - mov rsi,QWORD[8+r9] - mov r11,QWORD[56+r9] + mov rsi,QWORD[8+r9] ; disp->ImageBase + mov r11,QWORD[56+r9] ; disp->HandlerData - mov r10d,DWORD[r11] - lea r10,[r10*1+rsi] - cmp rbx,r10 + mov r10d,DWORD[r11] ; HandlerData[0] + lea r10,[r10*1+rsi] ; end of prologue label + cmp rbx,r10 ; context->RipRsp - mov r10d,DWORD[4+r11] - lea r10,[r10*1+rsi] - cmp rbx,r10 + mov r10d,DWORD[4+r11] ; HandlerData[1] + lea r10,[r10*1+rsi] ; epilogue label + cmp rbx,r10 ; context->Rip>=epilogue label jae NEAR $L$common_seh_tail - mov r10d,DWORD[8+r11] + mov r10d,DWORD[8+r11] ; HandlerData[2] lea rax,[r10*1+rax] mov rbp,QWORD[((-8))+rax] @@ -4843,40 +4844,40 @@ full_handler: mov r13,QWORD[((-32))+rax] mov r14,QWORD[((-40))+rax] mov r15,QWORD[((-48))+rax] - mov QWORD[144+r8],rbx - mov QWORD[160+r8],rbp - mov QWORD[216+r8],r12 - mov QWORD[224+r8],r13 - mov QWORD[232+r8],r14 - mov QWORD[240+r8],r15 + mov QWORD[144+r8],rbx ; restore context->Rbx + mov QWORD[160+r8],rbp ; restore context->Rbp + mov QWORD[216+r8],r12 ; restore context->R12 + mov QWORD[224+r8],r13 ; restore context->R13 + mov QWORD[232+r8],r14 ; restore context->R14 + mov QWORD[240+r8],r15 ; restore context->R15 $L$common_seh_tail: mov rdi,QWORD[8+rax] mov rsi,QWORD[16+rax] - mov QWORD[152+r8],rax - mov QWORD[168+r8],rsi - mov QWORD[176+r8],rdi + mov QWORD[152+r8],rax ; restore context->Rsp + mov QWORD[168+r8],rsi ; restore context->Rsi + mov QWORD[176+r8],rdi ; restore context->Rdi - mov rdi,QWORD[40+r9] - mov rsi,r8 - mov ecx,154 - DD 0xa548f3fc + mov rdi,QWORD[40+r9] ; disp->ContextRecord + mov rsi,r8 ; context + mov ecx,154 ; sizeof(CONTEXT) + DD 0xa548f3fc ; cld; rep movsq mov rsi,r9 - xor rcx,rcx - mov rdx,QWORD[8+rsi] - mov r8,QWORD[rsi] - mov r9,QWORD[16+rsi] - mov r10,QWORD[40+rsi] - lea r11,[56+rsi] - lea r12,[24+rsi] - mov QWORD[32+rsp],r10 - mov QWORD[40+rsp],r11 - mov QWORD[48+rsp],r12 - mov QWORD[56+rsp],rcx + xor rcx,rcx ; arg1, UNW_FLAG_NHANDLER + mov rdx,QWORD[8+rsi] ; arg2, disp->ImageBase + mov r8,QWORD[rsi] ; arg3, disp->ControlPc + mov r9,QWORD[16+rsi] ; arg4, disp->FunctionEntry + mov r10,QWORD[40+rsi] ; disp->ContextRecord + lea r11,[56+rsi] ; &disp->HandlerData + lea r12,[24+rsi] ; &disp->EstablisherFrame + mov QWORD[32+rsp],r10 ; arg5 + mov QWORD[40+rsp],r11 ; arg6 + mov QWORD[48+rsp],r12 ; arg7 + mov QWORD[56+rsp],rcx ; arg8, (NULL) call QWORD[__imp_RtlVirtualUnwind] - mov eax,1 + mov eax,1 ; ExceptionContinueSearch add rsp,64 popfq pop r15 @@ -4966,106 +4967,106 @@ ALIGN 8 $L$SEH_info_ecp_nistz256_neg: DB 9,0,0,0 DD short_handler wrt ..imagebase - DD $L$neg_body wrt ..imagebase,$L$neg_epilogue wrt ..imagebase + DD $L$neg_body wrt ..imagebase,$L$neg_epilogue wrt ..imagebase ; HandlerData[] $L$SEH_info_ecp_nistz256_ord_mul_mont_nohw: DB 9,0,0,0 DD full_handler wrt ..imagebase - DD $L$ord_mul_body wrt ..imagebase,$L$ord_mul_epilogue wrt ..imagebase + DD $L$ord_mul_body wrt ..imagebase,$L$ord_mul_epilogue wrt ..imagebase ; HandlerData[] DD 48,0 $L$SEH_info_ecp_nistz256_ord_sqr_mont_nohw: DB 9,0,0,0 DD full_handler wrt ..imagebase - DD $L$ord_sqr_body wrt ..imagebase,$L$ord_sqr_epilogue wrt ..imagebase + DD $L$ord_sqr_body wrt ..imagebase,$L$ord_sqr_epilogue wrt ..imagebase ; HandlerData[] DD 48,0 $L$SEH_info_ecp_nistz256_ord_mul_mont_adx: DB 9,0,0,0 DD full_handler wrt ..imagebase - DD $L$ord_mulx_body wrt ..imagebase,$L$ord_mulx_epilogue wrt ..imagebase + DD $L$ord_mulx_body wrt ..imagebase,$L$ord_mulx_epilogue wrt ..imagebase ; HandlerData[] DD 48,0 $L$SEH_info_ecp_nistz256_ord_sqr_mont_adx: DB 9,0,0,0 DD full_handler wrt ..imagebase - DD $L$ord_sqrx_body wrt ..imagebase,$L$ord_sqrx_epilogue wrt ..imagebase + DD $L$ord_sqrx_body wrt ..imagebase,$L$ord_sqrx_epilogue wrt ..imagebase ; HandlerData[] DD 48,0 $L$SEH_info_ecp_nistz256_mul_mont_nohw: DB 9,0,0,0 DD full_handler wrt ..imagebase - DD $L$mul_body wrt ..imagebase,$L$mul_epilogue wrt ..imagebase + DD $L$mul_body wrt ..imagebase,$L$mul_epilogue wrt ..imagebase ; HandlerData[] DD 48,0 $L$SEH_info_ecp_nistz256_sqr_mont_nohw: DB 9,0,0,0 DD full_handler wrt ..imagebase - DD $L$sqr_body wrt ..imagebase,$L$sqr_epilogue wrt ..imagebase + DD $L$sqr_body wrt ..imagebase,$L$sqr_epilogue wrt ..imagebase ; HandlerData[] DD 48,0 $L$SEH_info_ecp_nistz256_mul_mont_adx: DB 9,0,0,0 DD full_handler wrt ..imagebase - DD $L$mulx_body wrt ..imagebase,$L$mulx_epilogue wrt ..imagebase + DD $L$mulx_body wrt ..imagebase,$L$mulx_epilogue wrt ..imagebase ; HandlerData[] DD 48,0 $L$SEH_info_ecp_nistz256_sqr_mont_adx: DB 9,0,0,0 DD full_handler wrt ..imagebase - DD $L$sqrx_body wrt ..imagebase,$L$sqrx_epilogue wrt ..imagebase + DD $L$sqrx_body wrt ..imagebase,$L$sqrx_epilogue wrt ..imagebase ; HandlerData[] DD 48,0 $L$SEH_info_ecp_nistz256_select_wX_nohw: DB 0x01,0x33,0x16,0x00 - DB 0x33,0xf8,0x09,0x00 - DB 0x2e,0xe8,0x08,0x00 - DB 0x29,0xd8,0x07,0x00 - DB 0x24,0xc8,0x06,0x00 - DB 0x1f,0xb8,0x05,0x00 - DB 0x1a,0xa8,0x04,0x00 - DB 0x15,0x98,0x03,0x00 - DB 0x10,0x88,0x02,0x00 - DB 0x0c,0x78,0x01,0x00 - DB 0x08,0x68,0x00,0x00 - DB 0x04,0x01,0x15,0x00 + DB 0x33,0xf8,0x09,0x00 ; movaps 0x90(rsp),xmm15 + DB 0x2e,0xe8,0x08,0x00 ; movaps 0x80(rsp),xmm14 + DB 0x29,0xd8,0x07,0x00 ; movaps 0x70(rsp),xmm13 + DB 0x24,0xc8,0x06,0x00 ; movaps 0x60(rsp),xmm12 + DB 0x1f,0xb8,0x05,0x00 ; movaps 0x50(rsp),xmm11 + DB 0x1a,0xa8,0x04,0x00 ; movaps 0x40(rsp),xmm10 + DB 0x15,0x98,0x03,0x00 ; movaps 0x30(rsp),xmm9 + DB 0x10,0x88,0x02,0x00 ; movaps 0x20(rsp),xmm8 + DB 0x0c,0x78,0x01,0x00 ; movaps 0x10(rsp),xmm7 + DB 0x08,0x68,0x00,0x00 ; movaps 0x00(rsp),xmm6 + DB 0x04,0x01,0x15,0x00 ; sub rsp,0xa8 ALIGN 8 $L$SEH_info_ecp_nistz256_select_wX_avx2: DB 0x01,0x36,0x17,0x0b - DB 0x36,0xf8,0x09,0x00 - DB 0x31,0xe8,0x08,0x00 - DB 0x2c,0xd8,0x07,0x00 - DB 0x27,0xc8,0x06,0x00 - DB 0x22,0xb8,0x05,0x00 - DB 0x1d,0xa8,0x04,0x00 - DB 0x18,0x98,0x03,0x00 - DB 0x13,0x88,0x02,0x00 - DB 0x0e,0x78,0x01,0x00 - DB 0x09,0x68,0x00,0x00 - DB 0x04,0x01,0x15,0x00 - DB 0x00,0xb3,0x00,0x00 + DB 0x36,0xf8,0x09,0x00 ; vmovaps 0x90(rsp),xmm15 + DB 0x31,0xe8,0x08,0x00 ; vmovaps 0x80(rsp),xmm14 + DB 0x2c,0xd8,0x07,0x00 ; vmovaps 0x70(rsp),xmm13 + DB 0x27,0xc8,0x06,0x00 ; vmovaps 0x60(rsp),xmm12 + DB 0x22,0xb8,0x05,0x00 ; vmovaps 0x50(rsp),xmm11 + DB 0x1d,0xa8,0x04,0x00 ; vmovaps 0x40(rsp),xmm10 + DB 0x18,0x98,0x03,0x00 ; vmovaps 0x30(rsp),xmm9 + DB 0x13,0x88,0x02,0x00 ; vmovaps 0x20(rsp),xmm8 + DB 0x0e,0x78,0x01,0x00 ; vmovaps 0x10(rsp),xmm7 + DB 0x09,0x68,0x00,0x00 ; vmovaps 0x00(rsp),xmm6 + DB 0x04,0x01,0x15,0x00 ; sub rsp,0xa8 + DB 0x00,0xb3,0x00,0x00 ; set_frame r11 ALIGN 8 $L$SEH_info_ecp_nistz256_point_double_nohw: DB 9,0,0,0 DD full_handler wrt ..imagebase - DD $L$point_doubleq_body wrt ..imagebase,$L$point_doubleq_epilogue wrt ..imagebase + DD $L$point_doubleq_body wrt ..imagebase,$L$point_doubleq_epilogue wrt ..imagebase ; HandlerData[] DD 32*5+56,0 $L$SEH_info_ecp_nistz256_point_add_nohw: DB 9,0,0,0 DD full_handler wrt ..imagebase - DD $L$point_addq_body wrt ..imagebase,$L$point_addq_epilogue wrt ..imagebase + DD $L$point_addq_body wrt ..imagebase,$L$point_addq_epilogue wrt ..imagebase ; HandlerData[] DD 32*18+56,0 $L$SEH_info_ecp_nistz256_point_add_affine_nohw: DB 9,0,0,0 DD full_handler wrt ..imagebase - DD $L$add_affineq_body wrt ..imagebase,$L$add_affineq_epilogue wrt ..imagebase + DD $L$add_affineq_body wrt ..imagebase,$L$add_affineq_epilogue wrt ..imagebase ; HandlerData[] DD 32*15+56,0 ALIGN 8 $L$SEH_info_ecp_nistz256_point_double_adx: DB 9,0,0,0 DD full_handler wrt ..imagebase - DD $L$point_doublex_body wrt ..imagebase,$L$point_doublex_epilogue wrt ..imagebase + DD $L$point_doublex_body wrt ..imagebase,$L$point_doublex_epilogue wrt ..imagebase ; HandlerData[] DD 32*5+56,0 $L$SEH_info_ecp_nistz256_point_add_adx: DB 9,0,0,0 DD full_handler wrt ..imagebase - DD $L$point_addx_body wrt ..imagebase,$L$point_addx_epilogue wrt ..imagebase + DD $L$point_addx_body wrt ..imagebase,$L$point_addx_epilogue wrt ..imagebase ; HandlerData[] DD 32*18+56,0 $L$SEH_info_ecp_nistz256_point_add_affine_adx: DB 9,0,0,0 DD full_handler wrt ..imagebase - DD $L$add_affinex_body wrt ..imagebase,$L$add_affinex_epilogue wrt ..imagebase + DD $L$add_affinex_body wrt ..imagebase,$L$add_affinex_epilogue wrt ..imagebase ; HandlerData[] DD 32*15+56,0 %else ; Work around https://bugzilla.nasm.us/show_bug.cgi?id=3392738 diff --git a/third_party/boringssl/gen/bcm/p256_beeu-x86_64-asm-apple.S b/third_party/boringssl/gen/bcm/p256_beeu-x86_64-asm-apple.S index 35461b1f8..13bedb242 100644 --- a/third_party/boringssl/gen/bcm/p256_beeu-x86_64-asm-apple.S +++ b/third_party/boringssl/gen/bcm/p256_beeu-x86_64-asm-apple.S @@ -32,7 +32,7 @@ _CET_ENDBR movq %rdi,0(%rsp) - +// X=1, Y=0 movq $1,%r8 xorq %r9,%r9 xorq %r10,%r10 @@ -45,7 +45,7 @@ _CET_ENDBR xorq %r15,%r15 xorq %rbp,%rbp - +// Copy a/n into B/A on the stack. vmovdqu 0(%rsi),%xmm0 vmovdqu 16(%rsi),%xmm1 vmovdqu %xmm0,48(%rsp) @@ -65,23 +65,23 @@ L$beeu_loop: jz L$beeu_loop_end +// 0 < B < |n|, +// 0 < A <= |n|, +// (1) X*a == B (mod |n|), +// (2) (-1)*Y*a == A (mod |n|) - - - - - - - +// Now divide B by the maximum possible power of two in the +// integers, and divide X by the same value mod |n|. When we're +// done, (1) still holds. movq $1,%rcx - +// Note that B > 0 L$beeu_shift_loop_XB: movq %rcx,%rbx andq 48(%rsp),%rbx jnz L$beeu_shift_loop_end_XB - +// Ensure X is even and divide by two. movq $1,%rbx andq %r8,%rbx jz L$shift1_0 @@ -100,10 +100,10 @@ L$shift1_0: shlq $1,%rcx - - - - +// Test wraparound of the shift parameter. The probability to have 32 zeroes +// in a row is small Therefore having the value below equal $0x8000000 or +// $0x8000 does not affect the performance. We choose 0x8000000 because it +// is the maximal immediate value possible. cmpq $0x8000000,%rcx jne L$beeu_shift_loop_XB @@ -112,8 +112,8 @@ L$beeu_shift_loop_end_XB: testq %rcx,%rcx jz L$beeu_no_shift_XB - - +// Copy shifted values. +// Remember not to override t3=rcx movq 8+48(%rsp),%rax movq 16+48(%rsp),%rbx movq 24+48(%rsp),%rsi @@ -127,16 +127,16 @@ L$beeu_shift_loop_end_XB: L$beeu_no_shift_XB: - +// Same for A and Y. Afterwards, (2) still holds. movq $1,%rcx - +// Note that A > 0 L$beeu_shift_loop_YA: movq %rcx,%rbx andq 16(%rsp),%rbx jnz L$beeu_shift_loop_end_YA - +// Ensure X is even and divide by two. movq $1,%rbx andq %r12,%rbx jz L$shift1_1 @@ -155,10 +155,10 @@ L$shift1_1: shlq $1,%rcx - - - - +// Test wraparound of the shift parameter. The probability to have 32 zeroes +// in a row is small therefore having the value below equal $0x8000000 or +// $0x8000 Does not affect the performance. We choose 0x8000000 because it +// is the maximal immediate value possible. cmpq $0x8000000,%rcx jne L$beeu_shift_loop_YA @@ -167,8 +167,8 @@ L$beeu_shift_loop_end_YA: testq %rcx,%rcx jz L$beeu_no_shift_YA - - +// Copy shifted values. +// Remember not to override t3=rcx movq 8+16(%rsp),%rax movq 16+16(%rsp),%rbx movq 24+16(%rsp),%rsi @@ -182,7 +182,7 @@ L$beeu_shift_loop_end_YA: L$beeu_no_shift_YA: - +// T = B-A (A,B < 2^256) movq 48(%rsp),%rax movq 56(%rsp),%rbx movq 64(%rsp),%rsi @@ -190,10 +190,10 @@ L$beeu_no_shift_YA: subq 16(%rsp),%rax sbbq 24(%rsp),%rbx sbbq 32(%rsp),%rsi - sbbq 40(%rsp),%rcx + sbbq 40(%rsp),%rcx // borrow from shift jnc L$beeu_B_bigger_than_A - +// A = A - B movq 16(%rsp),%rax movq 24(%rsp),%rbx movq 32(%rsp),%rsi @@ -207,7 +207,7 @@ L$beeu_no_shift_YA: movq %rsi,32(%rsp) movq %rcx,40(%rsp) - +// Y = Y + X addq %r8,%r12 adcq %r9,%r13 adcq %r10,%r14 @@ -216,13 +216,13 @@ L$beeu_no_shift_YA: jmp L$beeu_loop L$beeu_B_bigger_than_A: - +// B = T = B - A movq %rax,48(%rsp) movq %rbx,56(%rsp) movq %rsi,64(%rsp) movq %rcx,72(%rsp) - +// X = Y + X addq %r12,%r8 adcq %r13,%r9 adcq %r14,%r10 @@ -232,21 +232,21 @@ L$beeu_B_bigger_than_A: jmp L$beeu_loop L$beeu_loop_end: +// The Euclid's algorithm loop ends when A == beeu(a,n); +// Therefore (-1)*Y*a == A (mod |n|), Y>0 - - - +// Verify that A = 1 ==> (-1)*Y*a = A = 1 (mod |n|) movq 16(%rsp),%rbx subq $1,%rbx orq 24(%rsp),%rbx orq 32(%rsp),%rbx orq 40(%rsp),%rbx - +// If not, fail. jnz L$beeu_err - - - +// From this point on, we no longer need X +// Therefore we use it as a temporary storage. +// X = n movq 0(%rdx),%r8 movq 8(%rdx),%r9 movq 16(%rdx),%r10 @@ -260,28 +260,28 @@ L$beeu_reduction_loop: movq %r15,40(%rsp) movq %rbp,48(%rsp) - +// If Y>n ==> Y=Y-n subq %r8,%r12 sbbq %r9,%r13 sbbq %r10,%r14 sbbq %r11,%r15 sbbq $0,%rbp - +// Choose old Y or new Y cmovcq 16(%rsp),%r12 cmovcq 24(%rsp),%r13 cmovcq 32(%rsp),%r14 cmovcq 40(%rsp),%r15 jnc L$beeu_reduction_loop - +// X = n - Y (n, Y < 2^256), (Cancel the (-1)) subq %r12,%r8 sbbq %r13,%r9 sbbq %r14,%r10 sbbq %r15,%r11 L$beeu_save: - +// Save the inverse(<2^256) to out. movq 0(%rsp),%rdi movq %r8,0(%rdi) @@ -289,12 +289,12 @@ L$beeu_save: movq %r10,16(%rdi) movq %r11,24(%rdi) - +// Return 1. movq $1,%rax jmp L$beeu_finish L$beeu_err: - +// Return 0. xorq %rax,%rax L$beeu_finish: diff --git a/third_party/boringssl/gen/bcm/p256_beeu-x86_64-asm-linux.S b/third_party/boringssl/gen/bcm/p256_beeu-x86_64-asm-linux.S index 1056268cd..ee5a6f1f1 100644 --- a/third_party/boringssl/gen/bcm/p256_beeu-x86_64-asm-linux.S +++ b/third_party/boringssl/gen/bcm/p256_beeu-x86_64-asm-linux.S @@ -39,7 +39,7 @@ _CET_ENDBR .cfi_adjust_cfa_offset 80 movq %rdi,0(%rsp) - +// X=1, Y=0 movq $1,%r8 xorq %r9,%r9 xorq %r10,%r10 @@ -52,7 +52,7 @@ _CET_ENDBR xorq %r15,%r15 xorq %rbp,%rbp - +// Copy a/n into B/A on the stack. vmovdqu 0(%rsi),%xmm0 vmovdqu 16(%rsi),%xmm1 vmovdqu %xmm0,48(%rsp) @@ -72,23 +72,23 @@ _CET_ENDBR jz .Lbeeu_loop_end +// 0 < B < |n|, +// 0 < A <= |n|, +// (1) X*a == B (mod |n|), +// (2) (-1)*Y*a == A (mod |n|) - - - - - - - +// Now divide B by the maximum possible power of two in the +// integers, and divide X by the same value mod |n|. When we're +// done, (1) still holds. movq $1,%rcx - +// Note that B > 0 .Lbeeu_shift_loop_XB: movq %rcx,%rbx andq 48(%rsp),%rbx jnz .Lbeeu_shift_loop_end_XB - +// Ensure X is even and divide by two. movq $1,%rbx andq %r8,%rbx jz .Lshift1_0 @@ -107,10 +107,10 @@ _CET_ENDBR shlq $1,%rcx - - - - +// Test wraparound of the shift parameter. The probability to have 32 zeroes +// in a row is small Therefore having the value below equal $0x8000000 or +// $0x8000 does not affect the performance. We choose 0x8000000 because it +// is the maximal immediate value possible. cmpq $0x8000000,%rcx jne .Lbeeu_shift_loop_XB @@ -119,8 +119,8 @@ _CET_ENDBR testq %rcx,%rcx jz .Lbeeu_no_shift_XB - - +// Copy shifted values. +// Remember not to override t3=rcx movq 8+48(%rsp),%rax movq 16+48(%rsp),%rbx movq 24+48(%rsp),%rsi @@ -134,16 +134,16 @@ _CET_ENDBR .Lbeeu_no_shift_XB: - +// Same for A and Y. Afterwards, (2) still holds. movq $1,%rcx - +// Note that A > 0 .Lbeeu_shift_loop_YA: movq %rcx,%rbx andq 16(%rsp),%rbx jnz .Lbeeu_shift_loop_end_YA - +// Ensure X is even and divide by two. movq $1,%rbx andq %r12,%rbx jz .Lshift1_1 @@ -162,10 +162,10 @@ _CET_ENDBR shlq $1,%rcx - - - - +// Test wraparound of the shift parameter. The probability to have 32 zeroes +// in a row is small therefore having the value below equal $0x8000000 or +// $0x8000 Does not affect the performance. We choose 0x8000000 because it +// is the maximal immediate value possible. cmpq $0x8000000,%rcx jne .Lbeeu_shift_loop_YA @@ -174,8 +174,8 @@ _CET_ENDBR testq %rcx,%rcx jz .Lbeeu_no_shift_YA - - +// Copy shifted values. +// Remember not to override t3=rcx movq 8+16(%rsp),%rax movq 16+16(%rsp),%rbx movq 24+16(%rsp),%rsi @@ -189,7 +189,7 @@ _CET_ENDBR .Lbeeu_no_shift_YA: - +// T = B-A (A,B < 2^256) movq 48(%rsp),%rax movq 56(%rsp),%rbx movq 64(%rsp),%rsi @@ -197,10 +197,10 @@ _CET_ENDBR subq 16(%rsp),%rax sbbq 24(%rsp),%rbx sbbq 32(%rsp),%rsi - sbbq 40(%rsp),%rcx + sbbq 40(%rsp),%rcx // borrow from shift jnc .Lbeeu_B_bigger_than_A - +// A = A - B movq 16(%rsp),%rax movq 24(%rsp),%rbx movq 32(%rsp),%rsi @@ -214,7 +214,7 @@ _CET_ENDBR movq %rsi,32(%rsp) movq %rcx,40(%rsp) - +// Y = Y + X addq %r8,%r12 adcq %r9,%r13 adcq %r10,%r14 @@ -223,13 +223,13 @@ _CET_ENDBR jmp .Lbeeu_loop .Lbeeu_B_bigger_than_A: - +// B = T = B - A movq %rax,48(%rsp) movq %rbx,56(%rsp) movq %rsi,64(%rsp) movq %rcx,72(%rsp) - +// X = Y + X addq %r12,%r8 adcq %r13,%r9 adcq %r14,%r10 @@ -239,21 +239,21 @@ _CET_ENDBR jmp .Lbeeu_loop .Lbeeu_loop_end: +// The Euclid's algorithm loop ends when A == beeu(a,n); +// Therefore (-1)*Y*a == A (mod |n|), Y>0 - - - +// Verify that A = 1 ==> (-1)*Y*a = A = 1 (mod |n|) movq 16(%rsp),%rbx subq $1,%rbx orq 24(%rsp),%rbx orq 32(%rsp),%rbx orq 40(%rsp),%rbx - +// If not, fail. jnz .Lbeeu_err - - - +// From this point on, we no longer need X +// Therefore we use it as a temporary storage. +// X = n movq 0(%rdx),%r8 movq 8(%rdx),%r9 movq 16(%rdx),%r10 @@ -267,28 +267,28 @@ _CET_ENDBR movq %r15,40(%rsp) movq %rbp,48(%rsp) - +// If Y>n ==> Y=Y-n subq %r8,%r12 sbbq %r9,%r13 sbbq %r10,%r14 sbbq %r11,%r15 sbbq $0,%rbp - +// Choose old Y or new Y cmovcq 16(%rsp),%r12 cmovcq 24(%rsp),%r13 cmovcq 32(%rsp),%r14 cmovcq 40(%rsp),%r15 jnc .Lbeeu_reduction_loop - +// X = n - Y (n, Y < 2^256), (Cancel the (-1)) subq %r12,%r8 sbbq %r13,%r9 sbbq %r14,%r10 sbbq %r15,%r11 .Lbeeu_save: - +// Save the inverse(<2^256) to out. movq 0(%rsp),%rdi movq %r8,0(%rdi) @@ -296,12 +296,12 @@ _CET_ENDBR movq %r10,16(%rdi) movq %r11,24(%rdi) - +// Return 1. movq $1,%rax jmp .Lbeeu_finish .Lbeeu_err: - +// Return 0. xorq %rax,%rax .Lbeeu_finish: diff --git a/third_party/boringssl/gen/bcm/p256_beeu-x86_64-asm-win.asm b/third_party/boringssl/gen/bcm/p256_beeu-x86_64-asm-win.asm index 5586c39da..85bffc5b3 100644 --- a/third_party/boringssl/gen/bcm/p256_beeu-x86_64-asm-win.asm +++ b/third_party/boringssl/gen/bcm/p256_beeu-x86_64-asm-win.asm @@ -51,7 +51,7 @@ _CET_ENDBR mov QWORD[rsp],rdi - +; X=1, Y=0 mov r8,1 xor r9,r9 xor r10,r10 @@ -64,7 +64,7 @@ _CET_ENDBR xor r15,r15 xor rbp,rbp - +; Copy a/n into B/A on the stack. vmovdqu xmm0,XMMWORD[rsi] vmovdqu xmm1,XMMWORD[16+rsi] vmovdqu XMMWORD[48+rsp],xmm0 @@ -84,23 +84,23 @@ $L$beeu_loop: jz NEAR $L$beeu_loop_end +; 0 < B < |n|, +; 0 < A <= |n|, +; (1) X*a == B (mod |n|), +; (2) (-1)*Y*a == A (mod |n|) - - - - - - - +; Now divide B by the maximum possible power of two in the +; integers, and divide X by the same value mod |n|. When we're +; done, (1) still holds. mov rcx,1 - +; Note that B > 0 $L$beeu_shift_loop_XB: mov rbx,rcx and rbx,QWORD[48+rsp] jnz NEAR $L$beeu_shift_loop_end_XB - +; Ensure X is even and divide by two. mov rbx,1 and rbx,r8 jz NEAR $L$shift1_0 @@ -119,10 +119,10 @@ $L$shift1_0: shl rcx,1 - - - - +; Test wraparound of the shift parameter. The probability to have 32 zeroes +; in a row is small Therefore having the value below equal $0x8000000 or +; $0x8000 does not affect the performance. We choose 0x8000000 because it +; is the maximal immediate value possible. cmp rcx,0x8000000 jne NEAR $L$beeu_shift_loop_XB @@ -131,8 +131,8 @@ $L$beeu_shift_loop_end_XB: test rcx,rcx jz NEAR $L$beeu_no_shift_XB - - +; Copy shifted values. +; Remember not to override t3=rcx mov rax,QWORD[((8+48))+rsp] mov rbx,QWORD[((16+48))+rsp] mov rsi,QWORD[((24+48))+rsp] @@ -146,16 +146,16 @@ $L$beeu_shift_loop_end_XB: $L$beeu_no_shift_XB: - +; Same for A and Y. Afterwards, (2) still holds. mov rcx,1 - +; Note that A > 0 $L$beeu_shift_loop_YA: mov rbx,rcx and rbx,QWORD[16+rsp] jnz NEAR $L$beeu_shift_loop_end_YA - +; Ensure X is even and divide by two. mov rbx,1 and rbx,r12 jz NEAR $L$shift1_1 @@ -174,10 +174,10 @@ $L$shift1_1: shl rcx,1 - - - - +; Test wraparound of the shift parameter. The probability to have 32 zeroes +; in a row is small therefore having the value below equal $0x8000000 or +; $0x8000 Does not affect the performance. We choose 0x8000000 because it +; is the maximal immediate value possible. cmp rcx,0x8000000 jne NEAR $L$beeu_shift_loop_YA @@ -186,8 +186,8 @@ $L$beeu_shift_loop_end_YA: test rcx,rcx jz NEAR $L$beeu_no_shift_YA - - +; Copy shifted values. +; Remember not to override t3=rcx mov rax,QWORD[((8+16))+rsp] mov rbx,QWORD[((16+16))+rsp] mov rsi,QWORD[((24+16))+rsp] @@ -201,7 +201,7 @@ $L$beeu_shift_loop_end_YA: $L$beeu_no_shift_YA: - +; T = B-A (A,B < 2^256) mov rax,QWORD[48+rsp] mov rbx,QWORD[56+rsp] mov rsi,QWORD[64+rsp] @@ -209,10 +209,10 @@ $L$beeu_no_shift_YA: sub rax,QWORD[16+rsp] sbb rbx,QWORD[24+rsp] sbb rsi,QWORD[32+rsp] - sbb rcx,QWORD[40+rsp] + sbb rcx,QWORD[40+rsp] ; borrow from shift jnc NEAR $L$beeu_B_bigger_than_A - +; A = A - B mov rax,QWORD[16+rsp] mov rbx,QWORD[24+rsp] mov rsi,QWORD[32+rsp] @@ -226,7 +226,7 @@ $L$beeu_no_shift_YA: mov QWORD[32+rsp],rsi mov QWORD[40+rsp],rcx - +; Y = Y + X add r12,r8 adc r13,r9 adc r14,r10 @@ -235,13 +235,13 @@ $L$beeu_no_shift_YA: jmp NEAR $L$beeu_loop $L$beeu_B_bigger_than_A: - +; B = T = B - A mov QWORD[48+rsp],rax mov QWORD[56+rsp],rbx mov QWORD[64+rsp],rsi mov QWORD[72+rsp],rcx - +; X = Y + X add r8,r12 adc r9,r13 adc r10,r14 @@ -251,21 +251,21 @@ $L$beeu_B_bigger_than_A: jmp NEAR $L$beeu_loop $L$beeu_loop_end: +; The Euclid's algorithm loop ends when A == beeu(a,n); +; Therefore (-1)*Y*a == A (mod |n|), Y>0 - - - +; Verify that A = 1 ==> (-1)*Y*a = A = 1 (mod |n|) mov rbx,QWORD[16+rsp] sub rbx,1 or rbx,QWORD[24+rsp] or rbx,QWORD[32+rsp] or rbx,QWORD[40+rsp] - +; If not, fail. jnz NEAR $L$beeu_err - - - +; From this point on, we no longer need X +; Therefore we use it as a temporary storage. +; X = n mov r8,QWORD[rdx] mov r9,QWORD[8+rdx] mov r10,QWORD[16+rdx] @@ -279,28 +279,28 @@ $L$beeu_reduction_loop: mov QWORD[40+rsp],r15 mov QWORD[48+rsp],rbp - +; If Y>n ==> Y=Y-n sub r12,r8 sbb r13,r9 sbb r14,r10 sbb r15,r11 sbb rbp,0 - +; Choose old Y or new Y cmovc r12,QWORD[16+rsp] cmovc r13,QWORD[24+rsp] cmovc r14,QWORD[32+rsp] cmovc r15,QWORD[40+rsp] jnc NEAR $L$beeu_reduction_loop - +; X = n - Y (n, Y < 2^256), (Cancel the (-1)) sub r8,r12 sbb r9,r13 sbb r10,r14 sbb r11,r15 $L$beeu_save: - +; Save the inverse(<2^256) to out. mov rdi,QWORD[rsp] mov QWORD[rdi],r8 @@ -308,12 +308,12 @@ $L$beeu_save: mov QWORD[16+rdi],r10 mov QWORD[24+rdi],r11 - +; Return 1. mov rax,1 jmp NEAR $L$beeu_finish $L$beeu_err: - +; Return 0. xor rax,rax $L$beeu_finish: diff --git a/third_party/boringssl/gen/bcm/rdrand-x86_64-apple.S b/third_party/boringssl/gen/bcm/rdrand-x86_64-apple.S index 4f990d957..f995fd02a 100644 --- a/third_party/boringssl/gen/bcm/rdrand-x86_64-apple.S +++ b/third_party/boringssl/gen/bcm/rdrand-x86_64-apple.S @@ -6,9 +6,9 @@ #if !defined(OPENSSL_NO_ASM) && defined(OPENSSL_X86_64) && defined(__APPLE__) .text - - - +// CRYPTO_rdrand writes eight bytes of random data from the hardware RNG to +// |out|. It returns one on success or zero on hardware failure. +// int CRYPTO_rdrand(uint8_t out[8]); .globl _CRYPTO_rdrand .private_extern _CRYPTO_rdrand @@ -18,21 +18,22 @@ _CRYPTO_rdrand: _CET_ENDBR xorq %rax,%rax rdrand %rdx - +// An add-with-carry of zero effectively sets %rax to the carry flag. adcq %rax,%rax movq %rdx,0(%rdi) ret - - - - +// CRYPTO_rdrand_multiple8_buf fills |len| bytes at |buf| with random data from +// the hardware RNG. The |len| argument must be a multiple of eight. It returns +// one on success and zero on hardware failure. +// int CRYPTO_rdrand_multiple8_buf(uint8_t *buf, size_t len); .globl _CRYPTO_rdrand_multiple8_buf .private_extern _CRYPTO_rdrand_multiple8_buf .p2align 4 +.alt_entry _CRYPTO_rdrand_multiple8_buf _CRYPTO_rdrand_multiple8_buf: _CET_ENDBR diff --git a/third_party/boringssl/gen/bcm/rdrand-x86_64-linux.S b/third_party/boringssl/gen/bcm/rdrand-x86_64-linux.S index 52a1eb207..285930048 100644 --- a/third_party/boringssl/gen/bcm/rdrand-x86_64-linux.S +++ b/third_party/boringssl/gen/bcm/rdrand-x86_64-linux.S @@ -6,9 +6,9 @@ #if !defined(OPENSSL_NO_ASM) && defined(OPENSSL_X86_64) && defined(__ELF__) .text - - - +// CRYPTO_rdrand writes eight bytes of random data from the hardware RNG to +// |out|. It returns one on success or zero on hardware failure. +// int CRYPTO_rdrand(uint8_t out[8]); .globl CRYPTO_rdrand .hidden CRYPTO_rdrand .type CRYPTO_rdrand,@function @@ -18,17 +18,17 @@ CRYPTO_rdrand: _CET_ENDBR xorq %rax,%rax rdrand %rdx - +// An add-with-carry of zero effectively sets %rax to the carry flag. adcq %rax,%rax movq %rdx,0(%rdi) ret .cfi_endproc .size CRYPTO_rdrand,.-CRYPTO_rdrand - - - - +// CRYPTO_rdrand_multiple8_buf fills |len| bytes at |buf| with random data from +// the hardware RNG. The |len| argument must be a multiple of eight. It returns +// one on success and zero on hardware failure. +// int CRYPTO_rdrand_multiple8_buf(uint8_t *buf, size_t len); .globl CRYPTO_rdrand_multiple8_buf .hidden CRYPTO_rdrand_multiple8_buf .type CRYPTO_rdrand_multiple8_buf,@function diff --git a/third_party/boringssl/gen/bcm/rdrand-x86_64-win.asm b/third_party/boringssl/gen/bcm/rdrand-x86_64-win.asm index 39d402f23..d83806529 100644 --- a/third_party/boringssl/gen/bcm/rdrand-x86_64-win.asm +++ b/third_party/boringssl/gen/bcm/rdrand-x86_64-win.asm @@ -14,9 +14,9 @@ default rel section .text code align=64 - - - +; CRYPTO_rdrand writes eight bytes of random data from the hardware RNG to +; |out|. It returns one on success or zero on hardware failure. +; int CRYPTO_rdrand(uint8_t out[8]); global CRYPTO_rdrand ALIGN 16 @@ -25,17 +25,17 @@ CRYPTO_rdrand: _CET_ENDBR xor rax,rax rdrand r8 - +; An add-with-carry of zero effectively sets %rax to the carry flag. adc rax,rax mov QWORD[rcx],r8 ret - - - - +; CRYPTO_rdrand_multiple8_buf fills |len| bytes at |buf| with random data from +; the hardware RNG. The |len| argument must be a multiple of eight. It returns +; one on success and zero on hardware failure. +; int CRYPTO_rdrand_multiple8_buf(uint8_t *buf, size_t len); global CRYPTO_rdrand_multiple8_buf ALIGN 16 diff --git a/third_party/boringssl/gen/bcm/rsaz-avx2-apple.S b/third_party/boringssl/gen/bcm/rsaz-avx2-apple.S index 36723091c..0dac92a3f 100644 --- a/third_party/boringssl/gen/bcm/rsaz-avx2-apple.S +++ b/third_party/boringssl/gen/bcm/rsaz-avx2-apple.S @@ -10,7 +10,7 @@ .private_extern _rsaz_1024_sqr_avx2 .p2align 6 -_rsaz_1024_sqr_avx2: +_rsaz_1024_sqr_avx2: // 702 cycles, 14% faster than rsaz_1024_mul_avx2 _CET_ENDBR leaq (%rsp),%rax @@ -30,23 +30,23 @@ _CET_ENDBR vzeroupper movq %rax,%rbp - movq %rdx,%r13 + movq %rdx,%r13 // reassigned argument subq $832,%rsp movq %r13,%r15 - subq $-128,%rdi + subq $-128,%rdi // size optimization subq $-128,%rsi subq $-128,%r13 - andq $4095,%r15 + andq $4095,%r15 // see if %r13 crosses page addq $320,%r15 shrq $12,%r15 vpxor %ymm9,%ymm9,%ymm9 jz L$sqr_1024_no_n_copy - - - - +// unaligned 256-bit load that crosses page boundary can +// cause >2x performance degradation here, so if %r13 does +// cross page boundary, copy it to stack and make sure stack +// frame doesn't... subq $320,%rsp vmovdqu 0-128(%r13),%ymm0 andq $-2048,%rsp @@ -68,7 +68,7 @@ _CET_ENDBR vmovdqu %ymm6,192-128(%r13) vmovdqu %ymm7,224-128(%r13) vmovdqu %ymm8,256-128(%r13) - vmovdqu %ymm9,288-128(%r13) + vmovdqu %ymm9,288-128(%r13) // %ymm9 is zero L$sqr_1024_no_n_copy: andq $-1024,%rsp @@ -82,18 +82,18 @@ L$sqr_1024_no_n_copy: vmovdqu 224-128(%rsi),%ymm7 vmovdqu 256-128(%rsi),%ymm8 - leaq 192(%rsp),%rbx + leaq 192(%rsp),%rbx // 64+128=192 vmovdqu L$and_mask(%rip),%ymm15 jmp L$OOP_GRANDE_SQR_1024 .p2align 5 L$OOP_GRANDE_SQR_1024: - leaq 576+128(%rsp),%r9 - leaq 448(%rsp),%r12 - - - + leaq 576+128(%rsp),%r9 // size optimization + leaq 448(%rsp),%r12 // 64+128+256=448 +// the squaring is performed as described in Variant B of +// "Speeding up Big-Number Squaring", so start by calculating +// the A*2=A+A vector vpaddq %ymm1,%ymm1,%ymm1 vpbroadcastq 0-128(%rsi),%ymm10 vpaddq %ymm2,%ymm2,%ymm2 @@ -115,7 +115,7 @@ L$OOP_GRANDE_SQR_1024: vpmuludq 0-128(%rsi),%ymm10,%ymm0 vpbroadcastq 32-128(%rsi),%ymm11 - vmovdqu %ymm9,288-192(%rbx) + vmovdqu %ymm9,288-192(%rbx) // zero upper half vpmuludq %ymm10,%ymm1,%ymm1 vmovdqu %ymm9,320-448(%r12) vpmuludq %ymm10,%ymm2,%ymm2 @@ -252,10 +252,10 @@ L$sqr_entry_1024: vpmuludq 192-128(%rsi),%ymm10,%ymm12 vpaddq %ymm12,%ymm3,%ymm3 vpmuludq 192-128(%r9),%ymm10,%ymm14 - vpbroadcastq 256-128(%r15),%ymm0 + vpbroadcastq 256-128(%r15),%ymm0 // borrow %ymm0 for %ymm10 vpaddq %ymm14,%ymm4,%ymm4 vpmuludq 224-128(%r9),%ymm10,%ymm5 - vpbroadcastq 0+8-128(%r15),%ymm10 + vpbroadcastq 0+8-128(%r15),%ymm10 // for next iteration vpaddq 448-448(%r12),%ymm5,%ymm5 vmovdqu %ymm3,384-448(%r12) @@ -276,11 +276,11 @@ L$sqr_entry_1024: decl %r14d jnz L$OOP_SQR_1024 - - vmovdqu 256(%rsp),%ymm8 - vmovdqu 288(%rsp),%ymm1 - vmovdqu 320(%rsp),%ymm2 - leaq 192(%rsp),%rbx +// we need to fix indices 32-39 to avoid overflow + vmovdqu 256(%rsp),%ymm8 // 32*8-192(%rbx), + vmovdqu 288(%rsp),%ymm1 // 32*9-192(%rbx) + vmovdqu 320(%rsp),%ymm2 // 32*10-192(%rbx) + leaq 192(%rsp),%rbx // 64+128=192 vpsrlq $29,%ymm8,%ymm14 vpand %ymm15,%ymm8,%ymm8 @@ -376,17 +376,17 @@ L$OOP_REDUCE_1024: vpaddq %ymm10,%ymm7,%ymm7 vpmuludq 256-128(%r13),%ymm12,%ymm14 vmovd %eax,%xmm12 - +// vmovdqu 32*1-8-128(%r13), %ymm11 # moved below vpaddq %ymm14,%ymm8,%ymm8 - +// vmovdqu 32*2-8-128(%r13), %ymm10 # moved below vpbroadcastq %xmm12,%ymm12 - vpmuludq 32-8-128(%r13),%ymm13,%ymm11 + vpmuludq 32-8-128(%r13),%ymm13,%ymm11 // see above vmovdqu 96-8-128(%r13),%ymm14 movq %rax,%rdx imulq -128(%r13),%rax vpaddq %ymm11,%ymm1,%ymm1 - vpmuludq 64-8-128(%r13),%ymm13,%ymm10 + vpmuludq 64-8-128(%r13),%ymm13,%ymm10 // see above vmovdqu 128-8-128(%r13),%ymm11 addq %rax,%r11 movq %rdx,%rax @@ -405,7 +405,7 @@ L$OOP_REDUCE_1024: imull %ecx,%eax vpaddq %ymm11,%ymm4,%ymm4 vpmuludq %ymm13,%ymm10,%ymm10 -.byte 0xc4,0x41,0x7e,0x6f,0x9d,0x58,0x00,0x00,0x00 +.byte 0xc4,0x41,0x7e,0x6f,0x9d,0x58,0x00,0x00,0x00 // vmovdqu 32*7-8-128(%r13), %ymm11 andl $0x1fffffff,%eax vpaddq %ymm10,%ymm5,%ymm5 vpmuludq %ymm13,%ymm14,%ymm14 @@ -413,7 +413,7 @@ L$OOP_REDUCE_1024: vpaddq %ymm14,%ymm6,%ymm6 vpmuludq %ymm13,%ymm11,%ymm11 vmovdqu 288-8-128(%r13),%ymm9 - vmovd %eax,%xmm0 + vmovd %eax,%xmm0 // borrow ACC0 for Y2 imulq -128(%r13),%rax vpaddq %ymm11,%ymm7,%ymm7 vpmuludq %ymm13,%ymm10,%ymm10 @@ -430,14 +430,14 @@ L$OOP_REDUCE_1024: vpaddq %ymm14,%ymm1,%ymm1 vpmuludq %ymm0,%ymm13,%ymm13 vpmuludq %ymm12,%ymm11,%ymm11 -.byte 0xc4,0x41,0x7e,0x6f,0xb5,0xf0,0xff,0xff,0xff +.byte 0xc4,0x41,0x7e,0x6f,0xb5,0xf0,0xff,0xff,0xff // vmovdqu 32*4-16-128(%r13), %ymm14 vpaddq %ymm1,%ymm13,%ymm13 vpaddq %ymm11,%ymm2,%ymm2 vpmuludq %ymm12,%ymm10,%ymm10 vmovdqu 160-16-128(%r13),%ymm11 .byte 0x67 vmovq %xmm13,%rax - vmovdqu %ymm13,(%rsp) + vmovdqu %ymm13,(%rsp) // transfer %r9-%r12 vpaddq %ymm10,%ymm3,%ymm3 vpmuludq %ymm12,%ymm14,%ymm14 vmovdqu 192-16-128(%r13),%ymm10 @@ -454,7 +454,7 @@ L$OOP_REDUCE_1024: addq %r12,%rax vpaddq %ymm14,%ymm7,%ymm7 vpmuludq %ymm12,%ymm11,%ymm11 - +// vmovdqu 32*2-24-128(%r13), %ymm14 # moved below movq %rax,%r9 imull %ecx,%eax vpaddq %ymm11,%ymm8,%ymm8 @@ -466,7 +466,7 @@ L$OOP_REDUCE_1024: vpaddq %ymm10,%ymm9,%ymm9 vpbroadcastq %xmm12,%ymm12 - vpmuludq 64-24-128(%r13),%ymm0,%ymm14 + vpmuludq 64-24-128(%r13),%ymm0,%ymm14 // see above vmovdqu 128-24-128(%r13),%ymm10 movq %rax,%rdx imulq -128(%r13),%rax @@ -489,7 +489,7 @@ L$OOP_REDUCE_1024: vpaddq %ymm10,%ymm4,%ymm3 vpmuludq %ymm0,%ymm14,%ymm14 vmovdqu 224-24-128(%r13),%ymm10 - imulq 24-128(%r13),%rdx + imulq 24-128(%r13),%rdx // future %r12 addq %rax,%r11 leaq (%r9,%r10,1),%rax vpaddq %ymm14,%ymm5,%ymm4 @@ -512,7 +512,7 @@ L$OOP_REDUCE_1024: decl %r14d jnz L$OOP_REDUCE_1024 - leaq 448(%rsp),%r12 + leaq 448(%rsp),%r12 // size optimization vpaddq %ymm9,%ymm13,%ymm0 vpxor %ymm9,%ymm9,%ymm9 @@ -655,7 +655,7 @@ L$OOP_REDUCE_1024: movq -8(%rax),%rbx - leaq (%rax),%rsp + leaq (%rax),%rsp // restore %rsp L$sqr_1024_epilogue: ret @@ -665,6 +665,7 @@ L$sqr_1024_epilogue: .private_extern _rsaz_1024_mul_avx2 .p2align 6 +.alt_entry _rsaz_1024_mul_avx2 _rsaz_1024_mul_avx2: _CET_ENDBR @@ -685,14 +686,14 @@ _CET_ENDBR movq %rax,%rbp vzeroall - movq %rdx,%r13 + movq %rdx,%r13 // reassigned argument subq $64,%rsp - - - - - +// unaligned 256-bit load that crosses page boundary can +// cause severe performance degradation here, so if %rsi does +// cross page boundary, swap it with %r13 [meaning that caller +// is advised to lay down %rsi and %r13 next to each other, so +// that only one can cross page boundary]. .byte 0x67,0x67 movq %rsi,%r15 andq $4095,%r15 @@ -703,20 +704,20 @@ _CET_ENDBR cmovnzq %r15,%r13 movq %rcx,%r15 - subq $-128,%rsi + subq $-128,%rsi // size optimization subq $-128,%rcx subq $-128,%rdi - andq $4095,%r15 + andq $4095,%r15 // see if %rcx crosses page addq $320,%r15 .byte 0x67,0x67 shrq $12,%r15 jz L$mul_1024_no_n_copy - - - - +// unaligned 256-bit load that crosses page boundary can +// cause severe performance degradation here, so if %rcx does +// cross page boundary, copy it to stack and make sure stack +// frame doesn't... subq $320,%rsp vmovdqu 0-128(%rcx),%ymm0 andq $-512,%rsp @@ -747,13 +748,13 @@ _CET_ENDBR vpxor %ymm7,%ymm7,%ymm7 vmovdqu %ymm8,256-128(%rcx) vmovdqa %ymm0,%ymm8 - vmovdqu %ymm9,288-128(%rcx) + vmovdqu %ymm9,288-128(%rcx) // %ymm9 is zero after vzeroall L$mul_1024_no_n_copy: andq $-64,%rsp movq (%r13),%rbx vpbroadcastq (%r13),%ymm10 - vmovdqu %ymm0,(%rsp) + vmovdqu %ymm0,(%rsp) // clear top of stack xorq %r9,%r9 .byte 0x67 xorq %r10,%r10 @@ -762,12 +763,12 @@ L$mul_1024_no_n_copy: vmovdqu L$and_mask(%rip),%ymm15 movl $9,%r14d - vmovdqu %ymm9,288-128(%rdi) + vmovdqu %ymm9,288-128(%rdi) // %ymm9 is zero after vzeroall jmp L$oop_mul_1024 .p2align 5 L$oop_mul_1024: - vpsrlq $29,%ymm3,%ymm9 + vpsrlq $29,%ymm3,%ymm9 // correct %ymm3(*) movq %rbx,%rax imulq -128(%rsi),%rax addq %r9,%rax @@ -793,7 +794,7 @@ L$oop_mul_1024: vpbroadcastq %xmm11,%ymm11 vpaddq %ymm12,%ymm2,%ymm2 vpmuludq 96-128(%rsi),%ymm10,%ymm13 - vpand %ymm15,%ymm3,%ymm3 + vpand %ymm15,%ymm3,%ymm3 // correct %ymm3 vpaddq %ymm13,%ymm3,%ymm3 vpmuludq 128-128(%rsi),%ymm10,%ymm0 vpaddq %ymm0,%ymm4,%ymm4 @@ -802,7 +803,7 @@ L$oop_mul_1024: vpmuludq 192-128(%rsi),%ymm10,%ymm13 vpaddq %ymm13,%ymm6,%ymm6 vpmuludq 224-128(%rsi),%ymm10,%ymm0 - vpermq $0x93,%ymm9,%ymm9 + vpermq $0x93,%ymm9,%ymm9 // correct %ymm3 vpaddq %ymm0,%ymm7,%ymm7 vpmuludq 256-128(%rsi),%ymm10,%ymm12 vpbroadcastq 8(%r13),%ymm10 @@ -836,10 +837,10 @@ L$oop_mul_1024: vpmuludq 192-128(%rcx),%ymm11,%ymm12 vpaddq %ymm12,%ymm6,%ymm6 vpmuludq 224-128(%rcx),%ymm11,%ymm13 - vpblendd $3,%ymm14,%ymm9,%ymm12 + vpblendd $3,%ymm14,%ymm9,%ymm12 // correct %ymm3 vpaddq %ymm13,%ymm7,%ymm7 vpmuludq 256-128(%rcx),%ymm11,%ymm0 - vpaddq %ymm12,%ymm3,%ymm3 + vpaddq %ymm12,%ymm3,%ymm3 // correct %ymm3 vpaddq %ymm0,%ymm8,%ymm8 movq %rbx,%rax @@ -852,9 +853,9 @@ L$oop_mul_1024: vmovdqu -8+64-128(%rsi),%ymm13 movq %r10,%rax - vpblendd $0xfc,%ymm14,%ymm9,%ymm9 + vpblendd $0xfc,%ymm14,%ymm9,%ymm9 // correct %ymm3 imull %r8d,%eax - vpaddq %ymm9,%ymm4,%ymm4 + vpaddq %ymm9,%ymm4,%ymm4 // correct %ymm3 andl $0x1fffffff,%eax imulq 16-128(%rsi),%rbx @@ -1042,7 +1043,7 @@ L$oop_mul_1024: vpmuludq %ymm10,%ymm13,%ymm13 vpbroadcastq 32(%r13),%ymm10 vpaddq %ymm13,%ymm9,%ymm9 - addq $32,%r13 + addq $32,%r13 // %r13++ vmovdqu -24+32-128(%rcx),%ymm0 imulq -128(%rcx),%rax @@ -1053,9 +1054,9 @@ L$oop_mul_1024: vpmuludq %ymm11,%ymm0,%ymm0 vmovq %xmm10,%rbx vmovdqu -24+96-128(%rcx),%ymm13 - vpaddq %ymm0,%ymm1,%ymm0 + vpaddq %ymm0,%ymm1,%ymm0 // %ymm0==%ymm0 vpmuludq %ymm11,%ymm12,%ymm12 - vmovdqu %ymm0,(%rsp) + vmovdqu %ymm0,(%rsp) // transfer %r9-%r12 vpaddq %ymm12,%ymm2,%ymm1 vmovdqu -24+128-128(%rcx),%ymm0 vpmuludq %ymm11,%ymm13,%ymm13 @@ -1212,7 +1213,7 @@ L$oop_mul_1024: movq -8(%rax),%rbx - leaq (%rax),%rsp + leaq (%rax),%rsp // restore %rsp L$mul_1024_epilogue: ret @@ -1222,10 +1223,11 @@ L$mul_1024_epilogue: .private_extern _rsaz_1024_red2norm_avx2 .p2align 5 +.alt_entry _rsaz_1024_red2norm_avx2 _rsaz_1024_red2norm_avx2: _CET_ENDBR - subq $-128,%rsi + subq $-128,%rsi // size optimization xorq %rax,%rax movq -128(%rsi),%r8 movq -120(%rsi),%r9 @@ -1238,7 +1240,7 @@ _CET_ENDBR addq %r8,%rax addq %r9,%rax addq %r10,%rax - adcq $0,%r11 + adcq $0,%r11 // consume eventual carry movq %rax,0(%rdi) movq %r11,%rax movq -104(%rsi),%r8 @@ -1249,7 +1251,7 @@ _CET_ENDBR shrq $12,%r10 addq %r8,%rax addq %r9,%rax - adcq $0,%r10 + adcq $0,%r10 // consume eventual carry movq %rax,8(%rdi) movq %r10,%rax movq -88(%rsi),%r11 @@ -1260,7 +1262,7 @@ _CET_ENDBR shrq $18,%r9 addq %r11,%rax addq %r8,%rax - adcq $0,%r9 + adcq $0,%r9 // consume eventual carry movq %rax,16(%rdi) movq %r9,%rax movq -72(%rsi),%r10 @@ -1271,7 +1273,7 @@ _CET_ENDBR shrq $24,%r8 addq %r10,%rax addq %r11,%rax - adcq $0,%r8 + adcq $0,%r8 // consume eventual carry movq %rax,24(%rdi) movq %r8,%rax movq -56(%rsi),%r9 @@ -1285,7 +1287,7 @@ _CET_ENDBR addq %r9,%rax addq %r10,%rax addq %r11,%rax - adcq $0,%r8 + adcq $0,%r8 // consume eventual carry movq %rax,32(%rdi) movq %r8,%rax movq -32(%rsi),%r9 @@ -1296,7 +1298,7 @@ _CET_ENDBR shrq $7,%r11 addq %r9,%rax addq %r10,%rax - adcq $0,%r11 + adcq $0,%r11 // consume eventual carry movq %rax,40(%rdi) movq %r11,%rax movq -16(%rsi),%r8 @@ -1307,7 +1309,7 @@ _CET_ENDBR shrq $13,%r10 addq %r8,%rax addq %r9,%rax - adcq $0,%r10 + adcq $0,%r10 // consume eventual carry movq %rax,48(%rdi) movq %r10,%rax movq 0(%rsi),%r11 @@ -1318,7 +1320,7 @@ _CET_ENDBR shrq $19,%r9 addq %r11,%rax addq %r8,%rax - adcq $0,%r9 + adcq $0,%r9 // consume eventual carry movq %rax,56(%rdi) movq %r9,%rax movq 16(%rsi),%r10 @@ -1329,7 +1331,7 @@ _CET_ENDBR shrq $25,%r8 addq %r10,%rax addq %r11,%rax - adcq $0,%r8 + adcq $0,%r8 // consume eventual carry movq %rax,64(%rdi) movq %r8,%rax movq 32(%rsi),%r9 @@ -1343,7 +1345,7 @@ _CET_ENDBR addq %r9,%rax addq %r10,%rax addq %r11,%rax - adcq $0,%r8 + adcq $0,%r8 // consume eventual carry movq %rax,72(%rdi) movq %r8,%rax movq 56(%rsi),%r9 @@ -1354,7 +1356,7 @@ _CET_ENDBR shrq $8,%r11 addq %r9,%rax addq %r10,%rax - adcq $0,%r11 + adcq $0,%r11 // consume eventual carry movq %rax,80(%rdi) movq %r11,%rax movq 72(%rsi),%r8 @@ -1365,7 +1367,7 @@ _CET_ENDBR shrq $14,%r10 addq %r8,%rax addq %r9,%rax - adcq $0,%r10 + adcq $0,%r10 // consume eventual carry movq %rax,88(%rdi) movq %r10,%rax movq 88(%rsi),%r11 @@ -1376,7 +1378,7 @@ _CET_ENDBR shrq $20,%r9 addq %r11,%rax addq %r8,%rax - adcq $0,%r9 + adcq $0,%r9 // consume eventual carry movq %rax,96(%rdi) movq %r9,%rax movq 104(%rsi),%r10 @@ -1387,7 +1389,7 @@ _CET_ENDBR shrq $26,%r8 addq %r10,%rax addq %r11,%rax - adcq $0,%r8 + adcq $0,%r8 // consume eventual carry movq %rax,104(%rdi) movq %r8,%rax movq 120(%rsi),%r9 @@ -1401,7 +1403,7 @@ _CET_ENDBR addq %r9,%rax addq %r10,%rax addq %r11,%rax - adcq $0,%r8 + adcq $0,%r8 // consume eventual carry movq %rax,112(%rdi) movq %r8,%rax movq 144(%rsi),%r9 @@ -1412,7 +1414,7 @@ _CET_ENDBR shrq $9,%r11 addq %r9,%rax addq %r10,%rax - adcq $0,%r11 + adcq $0,%r11 // consume eventual carry movq %rax,120(%rdi) movq %r11,%rax ret @@ -1423,20 +1425,21 @@ _CET_ENDBR .private_extern _rsaz_1024_norm2red_avx2 .p2align 5 +.alt_entry _rsaz_1024_norm2red_avx2 _rsaz_1024_norm2red_avx2: _CET_ENDBR - subq $-128,%rdi + subq $-128,%rdi // size optimization movq (%rsi),%r8 movl $0x1fffffff,%eax movq 8(%rsi),%r9 movq %r8,%r11 shrq $0,%r11 - andq %rax,%r11 + andq %rax,%r11 // &0x1fffffff movq %r11,-128(%rdi) movq %r8,%r10 shrq $29,%r10 - andq %rax,%r10 + andq %rax,%r10 // &0x1fffffff movq %r10,-120(%rdi) shrdq $58,%r9,%r8 andq %rax,%r8 @@ -1444,7 +1447,7 @@ _CET_ENDBR movq 16(%rsi),%r10 movq %r9,%r8 shrq $23,%r8 - andq %rax,%r8 + andq %rax,%r8 // &0x1fffffff movq %r8,-104(%rdi) shrdq $52,%r10,%r9 andq %rax,%r9 @@ -1452,7 +1455,7 @@ _CET_ENDBR movq 24(%rsi),%r11 movq %r10,%r9 shrq $17,%r9 - andq %rax,%r9 + andq %rax,%r9 // &0x1fffffff movq %r9,-88(%rdi) shrdq $46,%r11,%r10 andq %rax,%r10 @@ -1460,7 +1463,7 @@ _CET_ENDBR movq 32(%rsi),%r8 movq %r11,%r10 shrq $11,%r10 - andq %rax,%r10 + andq %rax,%r10 // &0x1fffffff movq %r10,-72(%rdi) shrdq $40,%r8,%r11 andq %rax,%r11 @@ -1468,11 +1471,11 @@ _CET_ENDBR movq 40(%rsi),%r9 movq %r8,%r11 shrq $5,%r11 - andq %rax,%r11 + andq %rax,%r11 // &0x1fffffff movq %r11,-56(%rdi) movq %r8,%r10 shrq $34,%r10 - andq %rax,%r10 + andq %rax,%r10 // &0x1fffffff movq %r10,-48(%rdi) shrdq $63,%r9,%r8 andq %rax,%r8 @@ -1480,7 +1483,7 @@ _CET_ENDBR movq 48(%rsi),%r10 movq %r9,%r8 shrq $28,%r8 - andq %rax,%r8 + andq %rax,%r8 // &0x1fffffff movq %r8,-32(%rdi) shrdq $57,%r10,%r9 andq %rax,%r9 @@ -1488,7 +1491,7 @@ _CET_ENDBR movq 56(%rsi),%r11 movq %r10,%r9 shrq $22,%r9 - andq %rax,%r9 + andq %rax,%r9 // &0x1fffffff movq %r9,-16(%rdi) shrdq $51,%r11,%r10 andq %rax,%r10 @@ -1496,7 +1499,7 @@ _CET_ENDBR movq 64(%rsi),%r8 movq %r11,%r10 shrq $16,%r10 - andq %rax,%r10 + andq %rax,%r10 // &0x1fffffff movq %r10,0(%rdi) shrdq $45,%r8,%r11 andq %rax,%r11 @@ -1504,7 +1507,7 @@ _CET_ENDBR movq 72(%rsi),%r9 movq %r8,%r11 shrq $10,%r11 - andq %rax,%r11 + andq %rax,%r11 // &0x1fffffff movq %r11,16(%rdi) shrdq $39,%r9,%r8 andq %rax,%r8 @@ -1512,11 +1515,11 @@ _CET_ENDBR movq 80(%rsi),%r10 movq %r9,%r8 shrq $4,%r8 - andq %rax,%r8 + andq %rax,%r8 // &0x1fffffff movq %r8,32(%rdi) movq %r9,%r11 shrq $33,%r11 - andq %rax,%r11 + andq %rax,%r11 // &0x1fffffff movq %r11,40(%rdi) shrdq $62,%r10,%r9 andq %rax,%r9 @@ -1524,7 +1527,7 @@ _CET_ENDBR movq 88(%rsi),%r11 movq %r10,%r9 shrq $27,%r9 - andq %rax,%r9 + andq %rax,%r9 // &0x1fffffff movq %r9,56(%rdi) shrdq $56,%r11,%r10 andq %rax,%r10 @@ -1532,7 +1535,7 @@ _CET_ENDBR movq 96(%rsi),%r8 movq %r11,%r10 shrq $21,%r10 - andq %rax,%r10 + andq %rax,%r10 // &0x1fffffff movq %r10,72(%rdi) shrdq $50,%r8,%r11 andq %rax,%r11 @@ -1540,7 +1543,7 @@ _CET_ENDBR movq 104(%rsi),%r9 movq %r8,%r11 shrq $15,%r11 - andq %rax,%r11 + andq %rax,%r11 // &0x1fffffff movq %r11,88(%rdi) shrdq $44,%r9,%r8 andq %rax,%r8 @@ -1548,7 +1551,7 @@ _CET_ENDBR movq 112(%rsi),%r10 movq %r9,%r8 shrq $9,%r8 - andq %rax,%r8 + andq %rax,%r8 // &0x1fffffff movq %r8,104(%rdi) shrdq $38,%r10,%r9 andq %rax,%r9 @@ -1556,11 +1559,11 @@ _CET_ENDBR movq 120(%rsi),%r11 movq %r10,%r9 shrq $3,%r9 - andq %rax,%r9 + andq %rax,%r9 // &0x1fffffff movq %r9,120(%rdi) movq %r10,%r8 shrq $32,%r8 - andq %rax,%r8 + andq %rax,%r8 // &0x1fffffff movq %r8,128(%rdi) shrdq $61,%r11,%r10 andq %rax,%r10 @@ -1568,12 +1571,12 @@ _CET_ENDBR xorq %r8,%r8 movq %r11,%r10 shrq $26,%r10 - andq %rax,%r10 + andq %rax,%r10 // &0x1fffffff movq %r10,144(%rdi) shrdq $55,%r8,%r11 andq %rax,%r11 movq %r11,152(%rdi) - movq %r8,160(%rdi) + movq %r8,160(%rdi) // zero movq %r8,168(%rdi) movq %r8,176(%rdi) movq %r8,184(%rdi) @@ -1584,6 +1587,7 @@ _CET_ENDBR .private_extern _rsaz_1024_scatter5_avx2 .p2align 5 +.alt_entry _rsaz_1024_scatter5_avx2 _rsaz_1024_scatter5_avx2: _CET_ENDBR @@ -1613,6 +1617,7 @@ L$oop_scatter_1024: .private_extern _rsaz_1024_gather5_avx2 .p2align 5 +.alt_entry _rsaz_1024_gather5_avx2 _rsaz_1024_gather5_avx2: _CET_ENDBR @@ -1622,7 +1627,7 @@ _CET_ENDBR leaq -256(%rsp),%rsp andq $-32,%rsp leaq L$inc(%rip),%r10 - leaq -128(%rsp),%rax + leaq -128(%rsp),%rax // control u-op density vmovd %edx,%xmm4 vmovdqa (%r10),%ymm0 @@ -1669,7 +1674,7 @@ _CET_ENDBR vpcmpeqd %ymm4,%ymm14,%ymm14 vpcmpeqd %ymm4,%ymm15,%ymm15 - vmovdqa -32(%r10),%ymm7 + vmovdqa -32(%r10),%ymm7 // .Lgather_permd leaq 128(%rsi),%rsi movl $9,%edx @@ -1715,7 +1720,7 @@ L$oop_gather_1024: vpor %ymm3,%ymm5,%ymm5 vpor %ymm5,%ymm4,%ymm4 - vextracti128 $1,%ymm4,%xmm5 + vextracti128 $1,%ymm4,%xmm5 // upper half is cleared vpor %xmm4,%xmm5,%xmm5 vpermd %ymm5,%ymm7,%ymm5 vmovdqu %ymm5,(%rdi) @@ -1733,7 +1738,9 @@ L$oop_gather_1024: L$SEH_end_rsaz_1024_gather5: .section __DATA,__const + .p2align 6 +rsaz_avx2_constants: L$and_mask: .quad 0x1fffffff,0x1fffffff,0x1fffffff,0x1fffffff L$scatter_permd: diff --git a/third_party/boringssl/gen/bcm/rsaz-avx2-linux.S b/third_party/boringssl/gen/bcm/rsaz-avx2-linux.S index 65a6c2e8c..dda873fd0 100644 --- a/third_party/boringssl/gen/bcm/rsaz-avx2-linux.S +++ b/third_party/boringssl/gen/bcm/rsaz-avx2-linux.S @@ -10,7 +10,7 @@ .hidden rsaz_1024_sqr_avx2 .type rsaz_1024_sqr_avx2,@function .align 64 -rsaz_1024_sqr_avx2: +rsaz_1024_sqr_avx2: // 702 cycles, 14% faster than rsaz_1024_mul_avx2 .cfi_startproc _CET_ENDBR leaq (%rsp),%rax @@ -30,23 +30,23 @@ _CET_ENDBR vzeroupper movq %rax,%rbp .cfi_def_cfa_register %rbp - movq %rdx,%r13 + movq %rdx,%r13 // reassigned argument subq $832,%rsp movq %r13,%r15 - subq $-128,%rdi + subq $-128,%rdi // size optimization subq $-128,%rsi subq $-128,%r13 - andq $4095,%r15 + andq $4095,%r15 // see if %r13 crosses page addq $320,%r15 shrq $12,%r15 vpxor %ymm9,%ymm9,%ymm9 jz .Lsqr_1024_no_n_copy - - - - +// unaligned 256-bit load that crosses page boundary can +// cause >2x performance degradation here, so if %r13 does +// cross page boundary, copy it to stack and make sure stack +// frame doesn't... subq $320,%rsp vmovdqu 0-128(%r13),%ymm0 andq $-2048,%rsp @@ -68,7 +68,7 @@ _CET_ENDBR vmovdqu %ymm6,192-128(%r13) vmovdqu %ymm7,224-128(%r13) vmovdqu %ymm8,256-128(%r13) - vmovdqu %ymm9,288-128(%r13) + vmovdqu %ymm9,288-128(%r13) // %ymm9 is zero .Lsqr_1024_no_n_copy: andq $-1024,%rsp @@ -82,18 +82,18 @@ _CET_ENDBR vmovdqu 224-128(%rsi),%ymm7 vmovdqu 256-128(%rsi),%ymm8 - leaq 192(%rsp),%rbx + leaq 192(%rsp),%rbx // 64+128=192 vmovdqu .Land_mask(%rip),%ymm15 jmp .LOOP_GRANDE_SQR_1024 .align 32 .LOOP_GRANDE_SQR_1024: - leaq 576+128(%rsp),%r9 - leaq 448(%rsp),%r12 - - - + leaq 576+128(%rsp),%r9 // size optimization + leaq 448(%rsp),%r12 // 64+128+256=448 +// the squaring is performed as described in Variant B of +// "Speeding up Big-Number Squaring", so start by calculating +// the A*2=A+A vector vpaddq %ymm1,%ymm1,%ymm1 vpbroadcastq 0-128(%rsi),%ymm10 vpaddq %ymm2,%ymm2,%ymm2 @@ -115,7 +115,7 @@ _CET_ENDBR vpmuludq 0-128(%rsi),%ymm10,%ymm0 vpbroadcastq 32-128(%rsi),%ymm11 - vmovdqu %ymm9,288-192(%rbx) + vmovdqu %ymm9,288-192(%rbx) // zero upper half vpmuludq %ymm10,%ymm1,%ymm1 vmovdqu %ymm9,320-448(%r12) vpmuludq %ymm10,%ymm2,%ymm2 @@ -252,10 +252,10 @@ _CET_ENDBR vpmuludq 192-128(%rsi),%ymm10,%ymm12 vpaddq %ymm12,%ymm3,%ymm3 vpmuludq 192-128(%r9),%ymm10,%ymm14 - vpbroadcastq 256-128(%r15),%ymm0 + vpbroadcastq 256-128(%r15),%ymm0 // borrow %ymm0 for %ymm10 vpaddq %ymm14,%ymm4,%ymm4 vpmuludq 224-128(%r9),%ymm10,%ymm5 - vpbroadcastq 0+8-128(%r15),%ymm10 + vpbroadcastq 0+8-128(%r15),%ymm10 // for next iteration vpaddq 448-448(%r12),%ymm5,%ymm5 vmovdqu %ymm3,384-448(%r12) @@ -276,11 +276,11 @@ _CET_ENDBR decl %r14d jnz .LOOP_SQR_1024 - - vmovdqu 256(%rsp),%ymm8 - vmovdqu 288(%rsp),%ymm1 - vmovdqu 320(%rsp),%ymm2 - leaq 192(%rsp),%rbx +// we need to fix indices 32-39 to avoid overflow + vmovdqu 256(%rsp),%ymm8 // 32*8-192(%rbx), + vmovdqu 288(%rsp),%ymm1 // 32*9-192(%rbx) + vmovdqu 320(%rsp),%ymm2 // 32*10-192(%rbx) + leaq 192(%rsp),%rbx // 64+128=192 vpsrlq $29,%ymm8,%ymm14 vpand %ymm15,%ymm8,%ymm8 @@ -376,17 +376,17 @@ _CET_ENDBR vpaddq %ymm10,%ymm7,%ymm7 vpmuludq 256-128(%r13),%ymm12,%ymm14 vmovd %eax,%xmm12 - +// vmovdqu 32*1-8-128(%r13), %ymm11 # moved below vpaddq %ymm14,%ymm8,%ymm8 - +// vmovdqu 32*2-8-128(%r13), %ymm10 # moved below vpbroadcastq %xmm12,%ymm12 - vpmuludq 32-8-128(%r13),%ymm13,%ymm11 + vpmuludq 32-8-128(%r13),%ymm13,%ymm11 // see above vmovdqu 96-8-128(%r13),%ymm14 movq %rax,%rdx imulq -128(%r13),%rax vpaddq %ymm11,%ymm1,%ymm1 - vpmuludq 64-8-128(%r13),%ymm13,%ymm10 + vpmuludq 64-8-128(%r13),%ymm13,%ymm10 // see above vmovdqu 128-8-128(%r13),%ymm11 addq %rax,%r11 movq %rdx,%rax @@ -405,7 +405,7 @@ _CET_ENDBR imull %ecx,%eax vpaddq %ymm11,%ymm4,%ymm4 vpmuludq %ymm13,%ymm10,%ymm10 -.byte 0xc4,0x41,0x7e,0x6f,0x9d,0x58,0x00,0x00,0x00 +.byte 0xc4,0x41,0x7e,0x6f,0x9d,0x58,0x00,0x00,0x00 // vmovdqu 32*7-8-128(%r13), %ymm11 andl $0x1fffffff,%eax vpaddq %ymm10,%ymm5,%ymm5 vpmuludq %ymm13,%ymm14,%ymm14 @@ -413,7 +413,7 @@ _CET_ENDBR vpaddq %ymm14,%ymm6,%ymm6 vpmuludq %ymm13,%ymm11,%ymm11 vmovdqu 288-8-128(%r13),%ymm9 - vmovd %eax,%xmm0 + vmovd %eax,%xmm0 // borrow ACC0 for Y2 imulq -128(%r13),%rax vpaddq %ymm11,%ymm7,%ymm7 vpmuludq %ymm13,%ymm10,%ymm10 @@ -430,14 +430,14 @@ _CET_ENDBR vpaddq %ymm14,%ymm1,%ymm1 vpmuludq %ymm0,%ymm13,%ymm13 vpmuludq %ymm12,%ymm11,%ymm11 -.byte 0xc4,0x41,0x7e,0x6f,0xb5,0xf0,0xff,0xff,0xff +.byte 0xc4,0x41,0x7e,0x6f,0xb5,0xf0,0xff,0xff,0xff // vmovdqu 32*4-16-128(%r13), %ymm14 vpaddq %ymm1,%ymm13,%ymm13 vpaddq %ymm11,%ymm2,%ymm2 vpmuludq %ymm12,%ymm10,%ymm10 vmovdqu 160-16-128(%r13),%ymm11 .byte 0x67 vmovq %xmm13,%rax - vmovdqu %ymm13,(%rsp) + vmovdqu %ymm13,(%rsp) // transfer %r9-%r12 vpaddq %ymm10,%ymm3,%ymm3 vpmuludq %ymm12,%ymm14,%ymm14 vmovdqu 192-16-128(%r13),%ymm10 @@ -454,7 +454,7 @@ _CET_ENDBR addq %r12,%rax vpaddq %ymm14,%ymm7,%ymm7 vpmuludq %ymm12,%ymm11,%ymm11 - +// vmovdqu 32*2-24-128(%r13), %ymm14 # moved below movq %rax,%r9 imull %ecx,%eax vpaddq %ymm11,%ymm8,%ymm8 @@ -466,7 +466,7 @@ _CET_ENDBR vpaddq %ymm10,%ymm9,%ymm9 vpbroadcastq %xmm12,%ymm12 - vpmuludq 64-24-128(%r13),%ymm0,%ymm14 + vpmuludq 64-24-128(%r13),%ymm0,%ymm14 // see above vmovdqu 128-24-128(%r13),%ymm10 movq %rax,%rdx imulq -128(%r13),%rax @@ -489,7 +489,7 @@ _CET_ENDBR vpaddq %ymm10,%ymm4,%ymm3 vpmuludq %ymm0,%ymm14,%ymm14 vmovdqu 224-24-128(%r13),%ymm10 - imulq 24-128(%r13),%rdx + imulq 24-128(%r13),%rdx // future %r12 addq %rax,%r11 leaq (%r9,%r10,1),%rax vpaddq %ymm14,%ymm5,%ymm4 @@ -512,7 +512,7 @@ _CET_ENDBR decl %r14d jnz .LOOP_REDUCE_1024 - leaq 448(%rsp),%r12 + leaq 448(%rsp),%r12 // size optimization vpaddq %ymm9,%ymm13,%ymm0 vpxor %ymm9,%ymm9,%ymm9 @@ -655,7 +655,7 @@ _CET_ENDBR .cfi_restore %rbp movq -8(%rax),%rbx .cfi_restore %rbx - leaq (%rax),%rsp + leaq (%rax),%rsp // restore %rsp .cfi_def_cfa_register %rsp .Lsqr_1024_epilogue: ret @@ -685,14 +685,14 @@ _CET_ENDBR movq %rax,%rbp .cfi_def_cfa_register %rbp vzeroall - movq %rdx,%r13 + movq %rdx,%r13 // reassigned argument subq $64,%rsp - - - - - +// unaligned 256-bit load that crosses page boundary can +// cause severe performance degradation here, so if %rsi does +// cross page boundary, swap it with %r13 [meaning that caller +// is advised to lay down %rsi and %r13 next to each other, so +// that only one can cross page boundary]. .byte 0x67,0x67 movq %rsi,%r15 andq $4095,%r15 @@ -703,20 +703,20 @@ _CET_ENDBR cmovnzq %r15,%r13 movq %rcx,%r15 - subq $-128,%rsi + subq $-128,%rsi // size optimization subq $-128,%rcx subq $-128,%rdi - andq $4095,%r15 + andq $4095,%r15 // see if %rcx crosses page addq $320,%r15 .byte 0x67,0x67 shrq $12,%r15 jz .Lmul_1024_no_n_copy - - - - +// unaligned 256-bit load that crosses page boundary can +// cause severe performance degradation here, so if %rcx does +// cross page boundary, copy it to stack and make sure stack +// frame doesn't... subq $320,%rsp vmovdqu 0-128(%rcx),%ymm0 andq $-512,%rsp @@ -747,13 +747,13 @@ _CET_ENDBR vpxor %ymm7,%ymm7,%ymm7 vmovdqu %ymm8,256-128(%rcx) vmovdqa %ymm0,%ymm8 - vmovdqu %ymm9,288-128(%rcx) + vmovdqu %ymm9,288-128(%rcx) // %ymm9 is zero after vzeroall .Lmul_1024_no_n_copy: andq $-64,%rsp movq (%r13),%rbx vpbroadcastq (%r13),%ymm10 - vmovdqu %ymm0,(%rsp) + vmovdqu %ymm0,(%rsp) // clear top of stack xorq %r9,%r9 .byte 0x67 xorq %r10,%r10 @@ -762,12 +762,12 @@ _CET_ENDBR vmovdqu .Land_mask(%rip),%ymm15 movl $9,%r14d - vmovdqu %ymm9,288-128(%rdi) + vmovdqu %ymm9,288-128(%rdi) // %ymm9 is zero after vzeroall jmp .Loop_mul_1024 .align 32 .Loop_mul_1024: - vpsrlq $29,%ymm3,%ymm9 + vpsrlq $29,%ymm3,%ymm9 // correct %ymm3(*) movq %rbx,%rax imulq -128(%rsi),%rax addq %r9,%rax @@ -793,7 +793,7 @@ _CET_ENDBR vpbroadcastq %xmm11,%ymm11 vpaddq %ymm12,%ymm2,%ymm2 vpmuludq 96-128(%rsi),%ymm10,%ymm13 - vpand %ymm15,%ymm3,%ymm3 + vpand %ymm15,%ymm3,%ymm3 // correct %ymm3 vpaddq %ymm13,%ymm3,%ymm3 vpmuludq 128-128(%rsi),%ymm10,%ymm0 vpaddq %ymm0,%ymm4,%ymm4 @@ -802,7 +802,7 @@ _CET_ENDBR vpmuludq 192-128(%rsi),%ymm10,%ymm13 vpaddq %ymm13,%ymm6,%ymm6 vpmuludq 224-128(%rsi),%ymm10,%ymm0 - vpermq $0x93,%ymm9,%ymm9 + vpermq $0x93,%ymm9,%ymm9 // correct %ymm3 vpaddq %ymm0,%ymm7,%ymm7 vpmuludq 256-128(%rsi),%ymm10,%ymm12 vpbroadcastq 8(%r13),%ymm10 @@ -836,10 +836,10 @@ _CET_ENDBR vpmuludq 192-128(%rcx),%ymm11,%ymm12 vpaddq %ymm12,%ymm6,%ymm6 vpmuludq 224-128(%rcx),%ymm11,%ymm13 - vpblendd $3,%ymm14,%ymm9,%ymm12 + vpblendd $3,%ymm14,%ymm9,%ymm12 // correct %ymm3 vpaddq %ymm13,%ymm7,%ymm7 vpmuludq 256-128(%rcx),%ymm11,%ymm0 - vpaddq %ymm12,%ymm3,%ymm3 + vpaddq %ymm12,%ymm3,%ymm3 // correct %ymm3 vpaddq %ymm0,%ymm8,%ymm8 movq %rbx,%rax @@ -852,9 +852,9 @@ _CET_ENDBR vmovdqu -8+64-128(%rsi),%ymm13 movq %r10,%rax - vpblendd $0xfc,%ymm14,%ymm9,%ymm9 + vpblendd $0xfc,%ymm14,%ymm9,%ymm9 // correct %ymm3 imull %r8d,%eax - vpaddq %ymm9,%ymm4,%ymm4 + vpaddq %ymm9,%ymm4,%ymm4 // correct %ymm3 andl $0x1fffffff,%eax imulq 16-128(%rsi),%rbx @@ -1042,7 +1042,7 @@ _CET_ENDBR vpmuludq %ymm10,%ymm13,%ymm13 vpbroadcastq 32(%r13),%ymm10 vpaddq %ymm13,%ymm9,%ymm9 - addq $32,%r13 + addq $32,%r13 // %r13++ vmovdqu -24+32-128(%rcx),%ymm0 imulq -128(%rcx),%rax @@ -1053,9 +1053,9 @@ _CET_ENDBR vpmuludq %ymm11,%ymm0,%ymm0 vmovq %xmm10,%rbx vmovdqu -24+96-128(%rcx),%ymm13 - vpaddq %ymm0,%ymm1,%ymm0 + vpaddq %ymm0,%ymm1,%ymm0 // %ymm0==%ymm0 vpmuludq %ymm11,%ymm12,%ymm12 - vmovdqu %ymm0,(%rsp) + vmovdqu %ymm0,(%rsp) // transfer %r9-%r12 vpaddq %ymm12,%ymm2,%ymm1 vmovdqu -24+128-128(%rcx),%ymm0 vpmuludq %ymm11,%ymm13,%ymm13 @@ -1212,7 +1212,7 @@ _CET_ENDBR .cfi_restore %rbp movq -8(%rax),%rbx .cfi_restore %rbx - leaq (%rax),%rsp + leaq (%rax),%rsp // restore %rsp .cfi_def_cfa_register %rsp .Lmul_1024_epilogue: ret @@ -1225,7 +1225,7 @@ _CET_ENDBR rsaz_1024_red2norm_avx2: .cfi_startproc _CET_ENDBR - subq $-128,%rsi + subq $-128,%rsi // size optimization xorq %rax,%rax movq -128(%rsi),%r8 movq -120(%rsi),%r9 @@ -1238,7 +1238,7 @@ _CET_ENDBR addq %r8,%rax addq %r9,%rax addq %r10,%rax - adcq $0,%r11 + adcq $0,%r11 // consume eventual carry movq %rax,0(%rdi) movq %r11,%rax movq -104(%rsi),%r8 @@ -1249,7 +1249,7 @@ _CET_ENDBR shrq $12,%r10 addq %r8,%rax addq %r9,%rax - adcq $0,%r10 + adcq $0,%r10 // consume eventual carry movq %rax,8(%rdi) movq %r10,%rax movq -88(%rsi),%r11 @@ -1260,7 +1260,7 @@ _CET_ENDBR shrq $18,%r9 addq %r11,%rax addq %r8,%rax - adcq $0,%r9 + adcq $0,%r9 // consume eventual carry movq %rax,16(%rdi) movq %r9,%rax movq -72(%rsi),%r10 @@ -1271,7 +1271,7 @@ _CET_ENDBR shrq $24,%r8 addq %r10,%rax addq %r11,%rax - adcq $0,%r8 + adcq $0,%r8 // consume eventual carry movq %rax,24(%rdi) movq %r8,%rax movq -56(%rsi),%r9 @@ -1285,7 +1285,7 @@ _CET_ENDBR addq %r9,%rax addq %r10,%rax addq %r11,%rax - adcq $0,%r8 + adcq $0,%r8 // consume eventual carry movq %rax,32(%rdi) movq %r8,%rax movq -32(%rsi),%r9 @@ -1296,7 +1296,7 @@ _CET_ENDBR shrq $7,%r11 addq %r9,%rax addq %r10,%rax - adcq $0,%r11 + adcq $0,%r11 // consume eventual carry movq %rax,40(%rdi) movq %r11,%rax movq -16(%rsi),%r8 @@ -1307,7 +1307,7 @@ _CET_ENDBR shrq $13,%r10 addq %r8,%rax addq %r9,%rax - adcq $0,%r10 + adcq $0,%r10 // consume eventual carry movq %rax,48(%rdi) movq %r10,%rax movq 0(%rsi),%r11 @@ -1318,7 +1318,7 @@ _CET_ENDBR shrq $19,%r9 addq %r11,%rax addq %r8,%rax - adcq $0,%r9 + adcq $0,%r9 // consume eventual carry movq %rax,56(%rdi) movq %r9,%rax movq 16(%rsi),%r10 @@ -1329,7 +1329,7 @@ _CET_ENDBR shrq $25,%r8 addq %r10,%rax addq %r11,%rax - adcq $0,%r8 + adcq $0,%r8 // consume eventual carry movq %rax,64(%rdi) movq %r8,%rax movq 32(%rsi),%r9 @@ -1343,7 +1343,7 @@ _CET_ENDBR addq %r9,%rax addq %r10,%rax addq %r11,%rax - adcq $0,%r8 + adcq $0,%r8 // consume eventual carry movq %rax,72(%rdi) movq %r8,%rax movq 56(%rsi),%r9 @@ -1354,7 +1354,7 @@ _CET_ENDBR shrq $8,%r11 addq %r9,%rax addq %r10,%rax - adcq $0,%r11 + adcq $0,%r11 // consume eventual carry movq %rax,80(%rdi) movq %r11,%rax movq 72(%rsi),%r8 @@ -1365,7 +1365,7 @@ _CET_ENDBR shrq $14,%r10 addq %r8,%rax addq %r9,%rax - adcq $0,%r10 + adcq $0,%r10 // consume eventual carry movq %rax,88(%rdi) movq %r10,%rax movq 88(%rsi),%r11 @@ -1376,7 +1376,7 @@ _CET_ENDBR shrq $20,%r9 addq %r11,%rax addq %r8,%rax - adcq $0,%r9 + adcq $0,%r9 // consume eventual carry movq %rax,96(%rdi) movq %r9,%rax movq 104(%rsi),%r10 @@ -1387,7 +1387,7 @@ _CET_ENDBR shrq $26,%r8 addq %r10,%rax addq %r11,%rax - adcq $0,%r8 + adcq $0,%r8 // consume eventual carry movq %rax,104(%rdi) movq %r8,%rax movq 120(%rsi),%r9 @@ -1401,7 +1401,7 @@ _CET_ENDBR addq %r9,%rax addq %r10,%rax addq %r11,%rax - adcq $0,%r8 + adcq $0,%r8 // consume eventual carry movq %rax,112(%rdi) movq %r8,%rax movq 144(%rsi),%r9 @@ -1412,7 +1412,7 @@ _CET_ENDBR shrq $9,%r11 addq %r9,%rax addq %r10,%rax - adcq $0,%r11 + adcq $0,%r11 // consume eventual carry movq %rax,120(%rdi) movq %r11,%rax ret @@ -1426,17 +1426,17 @@ _CET_ENDBR rsaz_1024_norm2red_avx2: .cfi_startproc _CET_ENDBR - subq $-128,%rdi + subq $-128,%rdi // size optimization movq (%rsi),%r8 movl $0x1fffffff,%eax movq 8(%rsi),%r9 movq %r8,%r11 shrq $0,%r11 - andq %rax,%r11 + andq %rax,%r11 // &0x1fffffff movq %r11,-128(%rdi) movq %r8,%r10 shrq $29,%r10 - andq %rax,%r10 + andq %rax,%r10 // &0x1fffffff movq %r10,-120(%rdi) shrdq $58,%r9,%r8 andq %rax,%r8 @@ -1444,7 +1444,7 @@ _CET_ENDBR movq 16(%rsi),%r10 movq %r9,%r8 shrq $23,%r8 - andq %rax,%r8 + andq %rax,%r8 // &0x1fffffff movq %r8,-104(%rdi) shrdq $52,%r10,%r9 andq %rax,%r9 @@ -1452,7 +1452,7 @@ _CET_ENDBR movq 24(%rsi),%r11 movq %r10,%r9 shrq $17,%r9 - andq %rax,%r9 + andq %rax,%r9 // &0x1fffffff movq %r9,-88(%rdi) shrdq $46,%r11,%r10 andq %rax,%r10 @@ -1460,7 +1460,7 @@ _CET_ENDBR movq 32(%rsi),%r8 movq %r11,%r10 shrq $11,%r10 - andq %rax,%r10 + andq %rax,%r10 // &0x1fffffff movq %r10,-72(%rdi) shrdq $40,%r8,%r11 andq %rax,%r11 @@ -1468,11 +1468,11 @@ _CET_ENDBR movq 40(%rsi),%r9 movq %r8,%r11 shrq $5,%r11 - andq %rax,%r11 + andq %rax,%r11 // &0x1fffffff movq %r11,-56(%rdi) movq %r8,%r10 shrq $34,%r10 - andq %rax,%r10 + andq %rax,%r10 // &0x1fffffff movq %r10,-48(%rdi) shrdq $63,%r9,%r8 andq %rax,%r8 @@ -1480,7 +1480,7 @@ _CET_ENDBR movq 48(%rsi),%r10 movq %r9,%r8 shrq $28,%r8 - andq %rax,%r8 + andq %rax,%r8 // &0x1fffffff movq %r8,-32(%rdi) shrdq $57,%r10,%r9 andq %rax,%r9 @@ -1488,7 +1488,7 @@ _CET_ENDBR movq 56(%rsi),%r11 movq %r10,%r9 shrq $22,%r9 - andq %rax,%r9 + andq %rax,%r9 // &0x1fffffff movq %r9,-16(%rdi) shrdq $51,%r11,%r10 andq %rax,%r10 @@ -1496,7 +1496,7 @@ _CET_ENDBR movq 64(%rsi),%r8 movq %r11,%r10 shrq $16,%r10 - andq %rax,%r10 + andq %rax,%r10 // &0x1fffffff movq %r10,0(%rdi) shrdq $45,%r8,%r11 andq %rax,%r11 @@ -1504,7 +1504,7 @@ _CET_ENDBR movq 72(%rsi),%r9 movq %r8,%r11 shrq $10,%r11 - andq %rax,%r11 + andq %rax,%r11 // &0x1fffffff movq %r11,16(%rdi) shrdq $39,%r9,%r8 andq %rax,%r8 @@ -1512,11 +1512,11 @@ _CET_ENDBR movq 80(%rsi),%r10 movq %r9,%r8 shrq $4,%r8 - andq %rax,%r8 + andq %rax,%r8 // &0x1fffffff movq %r8,32(%rdi) movq %r9,%r11 shrq $33,%r11 - andq %rax,%r11 + andq %rax,%r11 // &0x1fffffff movq %r11,40(%rdi) shrdq $62,%r10,%r9 andq %rax,%r9 @@ -1524,7 +1524,7 @@ _CET_ENDBR movq 88(%rsi),%r11 movq %r10,%r9 shrq $27,%r9 - andq %rax,%r9 + andq %rax,%r9 // &0x1fffffff movq %r9,56(%rdi) shrdq $56,%r11,%r10 andq %rax,%r10 @@ -1532,7 +1532,7 @@ _CET_ENDBR movq 96(%rsi),%r8 movq %r11,%r10 shrq $21,%r10 - andq %rax,%r10 + andq %rax,%r10 // &0x1fffffff movq %r10,72(%rdi) shrdq $50,%r8,%r11 andq %rax,%r11 @@ -1540,7 +1540,7 @@ _CET_ENDBR movq 104(%rsi),%r9 movq %r8,%r11 shrq $15,%r11 - andq %rax,%r11 + andq %rax,%r11 // &0x1fffffff movq %r11,88(%rdi) shrdq $44,%r9,%r8 andq %rax,%r8 @@ -1548,7 +1548,7 @@ _CET_ENDBR movq 112(%rsi),%r10 movq %r9,%r8 shrq $9,%r8 - andq %rax,%r8 + andq %rax,%r8 // &0x1fffffff movq %r8,104(%rdi) shrdq $38,%r10,%r9 andq %rax,%r9 @@ -1556,11 +1556,11 @@ _CET_ENDBR movq 120(%rsi),%r11 movq %r10,%r9 shrq $3,%r9 - andq %rax,%r9 + andq %rax,%r9 // &0x1fffffff movq %r9,120(%rdi) movq %r10,%r8 shrq $32,%r8 - andq %rax,%r8 + andq %rax,%r8 // &0x1fffffff movq %r8,128(%rdi) shrdq $61,%r11,%r10 andq %rax,%r10 @@ -1568,12 +1568,12 @@ _CET_ENDBR xorq %r8,%r8 movq %r11,%r10 shrq $26,%r10 - andq %rax,%r10 + andq %rax,%r10 // &0x1fffffff movq %r10,144(%rdi) shrdq $55,%r8,%r11 andq %rax,%r11 movq %r11,152(%rdi) - movq %r8,160(%rdi) + movq %r8,160(%rdi) // zero movq %r8,168(%rdi) movq %r8,176(%rdi) movq %r8,184(%rdi) @@ -1622,7 +1622,7 @@ _CET_ENDBR leaq -256(%rsp),%rsp andq $-32,%rsp leaq .Linc(%rip),%r10 - leaq -128(%rsp),%rax + leaq -128(%rsp),%rax // control u-op density vmovd %edx,%xmm4 vmovdqa (%r10),%ymm0 @@ -1669,7 +1669,7 @@ _CET_ENDBR vpcmpeqd %ymm4,%ymm14,%ymm14 vpcmpeqd %ymm4,%ymm15,%ymm15 - vmovdqa -32(%r10),%ymm7 + vmovdqa -32(%r10),%ymm7 // .Lgather_permd leaq 128(%rsi),%rsi movl $9,%edx @@ -1715,7 +1715,7 @@ _CET_ENDBR vpor %ymm3,%ymm5,%ymm5 vpor %ymm5,%ymm4,%ymm4 - vextracti128 $1,%ymm4,%xmm5 + vextracti128 $1,%ymm4,%xmm5 // upper half is cleared vpor %xmm4,%xmm5,%xmm5 vpermd %ymm5,%ymm7,%ymm5 vmovdqu %ymm5,(%rdi) @@ -1733,7 +1733,9 @@ _CET_ENDBR .LSEH_end_rsaz_1024_gather5: .size rsaz_1024_gather5_avx2,.-rsaz_1024_gather5_avx2 .section .rodata + .align 64 +rsaz_avx2_constants: .Land_mask: .quad 0x1fffffff,0x1fffffff,0x1fffffff,0x1fffffff .Lscatter_permd: diff --git a/third_party/boringssl/gen/bcm/rsaz-avx2-win.asm b/third_party/boringssl/gen/bcm/rsaz-avx2-win.asm index a518277ce..4272093cf 100644 --- a/third_party/boringssl/gen/bcm/rsaz-avx2-win.asm +++ b/third_party/boringssl/gen/bcm/rsaz-avx2-win.asm @@ -28,7 +28,7 @@ $L$SEH_begin_rsaz_1024_sqr_avx2: mov rcx,r9 mov r8,QWORD[40+rsp] - +; 702 cycles, 14% faster than rsaz_1024_mul_avx2 _CET_ENDBR lea rax,[rsp] @@ -60,23 +60,23 @@ _CET_ENDBR $L$sqr_1024_body: mov rbp,rax - mov r13,rdx + mov r13,rdx ; reassigned argument sub rsp,832 mov r15,r13 - sub rdi,-128 + sub rdi,-128 ; size optimization sub rsi,-128 sub r13,-128 - and r15,4095 + and r15,4095 ; see if %r13 crosses page add r15,32*10 shr r15,12 vpxor ymm9,ymm9,ymm9 jz NEAR $L$sqr_1024_no_n_copy - - - - +; unaligned 256-bit load that crosses page boundary can +; cause >2x performance degradation here, so if %r13 does +; cross page boundary, copy it to stack and make sure stack +; frame doesn't... sub rsp,32*10 vmovdqu ymm0,YMMWORD[((0-128))+r13] and rsp,-2048 @@ -98,7 +98,7 @@ $L$sqr_1024_body: vmovdqu YMMWORD[(192-128)+r13],ymm6 vmovdqu YMMWORD[(224-128)+r13],ymm7 vmovdqu YMMWORD[(256-128)+r13],ymm8 - vmovdqu YMMWORD[(288-128)+r13],ymm9 + vmovdqu YMMWORD[(288-128)+r13],ymm9 ; %ymm9 is zero $L$sqr_1024_no_n_copy: and rsp,-1024 @@ -112,18 +112,18 @@ $L$sqr_1024_no_n_copy: vmovdqu ymm7,YMMWORD[((224-128))+rsi] vmovdqu ymm8,YMMWORD[((256-128))+rsi] - lea rbx,[192+rsp] + lea rbx,[192+rsp] ; 64+128=192 vmovdqu ymm15,YMMWORD[$L$and_mask] jmp NEAR $L$OOP_GRANDE_SQR_1024 ALIGN 32 $L$OOP_GRANDE_SQR_1024: - lea r9,[((576+128))+rsp] - lea r12,[448+rsp] - - - + lea r9,[((576+128))+rsp] ; size optimization + lea r12,[448+rsp] ; 64+128+256=448 +; the squaring is performed as described in Variant B of +; "Speeding up Big-Number Squaring", so start by calculating +; the A*2=A+A vector vpaddq ymm1,ymm1,ymm1 vpbroadcastq ymm10,QWORD[((0-128))+rsi] vpaddq ymm2,ymm2,ymm2 @@ -145,7 +145,7 @@ $L$OOP_GRANDE_SQR_1024: vpmuludq ymm0,ymm10,YMMWORD[((0-128))+rsi] vpbroadcastq ymm11,QWORD[((32-128))+rsi] - vmovdqu YMMWORD[(288-192)+rbx],ymm9 + vmovdqu YMMWORD[(288-192)+rbx],ymm9 ; zero upper half vpmuludq ymm1,ymm1,ymm10 vmovdqu YMMWORD[(320-448)+r12],ymm9 vpmuludq ymm2,ymm2,ymm10 @@ -282,10 +282,10 @@ $L$sqr_entry_1024: vpmuludq ymm12,ymm10,YMMWORD[((192-128))+rsi] vpaddq ymm3,ymm3,ymm12 vpmuludq ymm14,ymm10,YMMWORD[((192-128))+r9] - vpbroadcastq ymm0,QWORD[((256-128))+r15] + vpbroadcastq ymm0,QWORD[((256-128))+r15] ; borrow %ymm0 for %ymm10 vpaddq ymm4,ymm4,ymm14 vpmuludq ymm5,ymm10,YMMWORD[((224-128))+r9] - vpbroadcastq ymm10,QWORD[((0+8-128))+r15] + vpbroadcastq ymm10,QWORD[((0+8-128))+r15] ; for next iteration vpaddq ymm5,ymm5,YMMWORD[((448-448))+r12] vmovdqu YMMWORD[(384-448)+r12],ymm3 @@ -306,11 +306,11 @@ $L$sqr_entry_1024: dec r14d jnz NEAR $L$OOP_SQR_1024 - - vmovdqu ymm8,YMMWORD[256+rsp] - vmovdqu ymm1,YMMWORD[288+rsp] - vmovdqu ymm2,YMMWORD[320+rsp] - lea rbx,[192+rsp] +; we need to fix indices 32-39 to avoid overflow + vmovdqu ymm8,YMMWORD[256+rsp] ; 32*8-192(%rbx), + vmovdqu ymm1,YMMWORD[288+rsp] ; 32*9-192(%rbx) + vmovdqu ymm2,YMMWORD[320+rsp] ; 32*10-192(%rbx) + lea rbx,[192+rsp] ; 64+128=192 vpsrlq ymm14,ymm8,29 vpand ymm8,ymm8,ymm15 @@ -406,17 +406,17 @@ $L$OOP_REDUCE_1024: vpaddq ymm7,ymm7,ymm10 vpmuludq ymm14,ymm12,YMMWORD[((256-128))+r13] vmovd xmm12,eax - +; vmovdqu 32*1-8-128(%r13), %ymm11 # moved below vpaddq ymm8,ymm8,ymm14 - +; vmovdqu 32*2-8-128(%r13), %ymm10 # moved below vpbroadcastq ymm12,xmm12 - vpmuludq ymm11,ymm13,YMMWORD[((32-8-128))+r13] + vpmuludq ymm11,ymm13,YMMWORD[((32-8-128))+r13] ; see above vmovdqu ymm14,YMMWORD[((96-8-128))+r13] mov rdx,rax imul rax,QWORD[((-128))+r13] vpaddq ymm1,ymm1,ymm11 - vpmuludq ymm10,ymm13,YMMWORD[((64-8-128))+r13] + vpmuludq ymm10,ymm13,YMMWORD[((64-8-128))+r13] ; see above vmovdqu ymm11,YMMWORD[((128-8-128))+r13] add r11,rax mov rax,rdx @@ -435,7 +435,7 @@ $L$OOP_REDUCE_1024: imul eax,ecx vpaddq ymm4,ymm4,ymm11 vpmuludq ymm10,ymm10,ymm13 - DB 0xc4,0x41,0x7e,0x6f,0x9d,0x58,0x00,0x00,0x00 + DB 0xc4,0x41,0x7e,0x6f,0x9d,0x58,0x00,0x00,0x00 ; vmovdqu 32*7-8-128(%r13), %ymm11 and eax,0x1fffffff vpaddq ymm5,ymm5,ymm10 vpmuludq ymm14,ymm14,ymm13 @@ -443,7 +443,7 @@ $L$OOP_REDUCE_1024: vpaddq ymm6,ymm6,ymm14 vpmuludq ymm11,ymm11,ymm13 vmovdqu ymm9,YMMWORD[((288-8-128))+r13] - vmovd xmm0,eax + vmovd xmm0,eax ; borrow ACC0 for Y2 imul rax,QWORD[((-128))+r13] vpaddq ymm7,ymm7,ymm11 vpmuludq ymm10,ymm10,ymm13 @@ -460,14 +460,14 @@ $L$OOP_REDUCE_1024: vpaddq ymm1,ymm1,ymm14 vpmuludq ymm13,ymm13,ymm0 vpmuludq ymm11,ymm11,ymm12 - DB 0xc4,0x41,0x7e,0x6f,0xb5,0xf0,0xff,0xff,0xff + DB 0xc4,0x41,0x7e,0x6f,0xb5,0xf0,0xff,0xff,0xff ; vmovdqu 32*4-16-128(%r13), %ymm14 vpaddq ymm13,ymm13,ymm1 vpaddq ymm2,ymm2,ymm11 vpmuludq ymm10,ymm10,ymm12 vmovdqu ymm11,YMMWORD[((160-16-128))+r13] DB 0x67 vmovq rax,xmm13 - vmovdqu YMMWORD[rsp],ymm13 + vmovdqu YMMWORD[rsp],ymm13 ; transfer %r9-%r12 vpaddq ymm3,ymm3,ymm10 vpmuludq ymm14,ymm14,ymm12 vmovdqu ymm10,YMMWORD[((192-16-128))+r13] @@ -484,7 +484,7 @@ $L$OOP_REDUCE_1024: add rax,r12 vpaddq ymm7,ymm7,ymm14 vpmuludq ymm11,ymm11,ymm12 - +; vmovdqu 32*2-24-128(%r13), %ymm14 # moved below mov r9,rax imul eax,ecx vpaddq ymm8,ymm8,ymm11 @@ -496,7 +496,7 @@ $L$OOP_REDUCE_1024: vpaddq ymm9,ymm9,ymm10 vpbroadcastq ymm12,xmm12 - vpmuludq ymm14,ymm0,YMMWORD[((64-24-128))+r13] + vpmuludq ymm14,ymm0,YMMWORD[((64-24-128))+r13] ; see above vmovdqu ymm10,YMMWORD[((128-24-128))+r13] mov rdx,rax imul rax,QWORD[((-128))+r13] @@ -519,7 +519,7 @@ $L$OOP_REDUCE_1024: vpaddq ymm3,ymm4,ymm10 vpmuludq ymm14,ymm14,ymm0 vmovdqu ymm10,YMMWORD[((224-24-128))+r13] - imul rdx,QWORD[((24-128))+r13] + imul rdx,QWORD[((24-128))+r13] ; future %r12 add r11,rax lea rax,[r10*1+r9] vpaddq ymm4,ymm5,ymm14 @@ -542,7 +542,7 @@ $L$OOP_REDUCE_1024: dec r14d jnz NEAR $L$OOP_REDUCE_1024 - lea r12,[448+rsp] + lea r12,[448+rsp] ; size optimization vpaddq ymm0,ymm13,ymm9 vpxor ymm9,ymm9,ymm9 @@ -696,7 +696,7 @@ $L$sqr_1024_in_tail: mov rbx,QWORD[((-8))+rax] - lea rsp,[rax] + lea rsp,[rax] ; restore %rsp $L$sqr_1024_epilogue: mov rdi,QWORD[8+rsp] ;WIN64 epilogue @@ -751,14 +751,14 @@ $L$mul_1024_body: mov rbp,rax vzeroall - mov r13,rdx + mov r13,rdx ; reassigned argument sub rsp,64 - - - - - +; unaligned 256-bit load that crosses page boundary can +; cause severe performance degradation here, so if %rsi does +; cross page boundary, swap it with %r13 [meaning that caller +; is advised to lay down %rsi and %r13 next to each other, so +; that only one can cross page boundary]. DB 0x67,0x67 mov r15,rsi and r15,4095 @@ -769,20 +769,20 @@ $L$mul_1024_body: cmovnz r13,r15 mov r15,rcx - sub rsi,-128 + sub rsi,-128 ; size optimization sub rcx,-128 sub rdi,-128 - and r15,4095 + and r15,4095 ; see if %rcx crosses page add r15,32*10 DB 0x67,0x67 shr r15,12 jz NEAR $L$mul_1024_no_n_copy - - - - +; unaligned 256-bit load that crosses page boundary can +; cause severe performance degradation here, so if %rcx does +; cross page boundary, copy it to stack and make sure stack +; frame doesn't... sub rsp,32*10 vmovdqu ymm0,YMMWORD[((0-128))+rcx] and rsp,-512 @@ -813,13 +813,13 @@ $L$mul_1024_body: vpxor ymm7,ymm7,ymm7 vmovdqu YMMWORD[(256-128)+rcx],ymm8 vmovdqa ymm8,ymm0 - vmovdqu YMMWORD[(288-128)+rcx],ymm9 + vmovdqu YMMWORD[(288-128)+rcx],ymm9 ; %ymm9 is zero after vzeroall $L$mul_1024_no_n_copy: and rsp,-64 mov rbx,QWORD[r13] vpbroadcastq ymm10,QWORD[r13] - vmovdqu YMMWORD[rsp],ymm0 + vmovdqu YMMWORD[rsp],ymm0 ; clear top of stack xor r9,r9 DB 0x67 xor r10,r10 @@ -828,12 +828,12 @@ $L$mul_1024_no_n_copy: vmovdqu ymm15,YMMWORD[$L$and_mask] mov r14d,9 - vmovdqu YMMWORD[(288-128)+rdi],ymm9 + vmovdqu YMMWORD[(288-128)+rdi],ymm9 ; %ymm9 is zero after vzeroall jmp NEAR $L$oop_mul_1024 ALIGN 32 $L$oop_mul_1024: - vpsrlq ymm9,ymm3,29 + vpsrlq ymm9,ymm3,29 ; correct %ymm3(*) mov rax,rbx imul rax,QWORD[((-128))+rsi] add rax,r9 @@ -859,7 +859,7 @@ $L$oop_mul_1024: vpbroadcastq ymm11,xmm11 vpaddq ymm2,ymm2,ymm12 vpmuludq ymm13,ymm10,YMMWORD[((96-128))+rsi] - vpand ymm3,ymm3,ymm15 + vpand ymm3,ymm3,ymm15 ; correct %ymm3 vpaddq ymm3,ymm3,ymm13 vpmuludq ymm0,ymm10,YMMWORD[((128-128))+rsi] vpaddq ymm4,ymm4,ymm0 @@ -868,7 +868,7 @@ $L$oop_mul_1024: vpmuludq ymm13,ymm10,YMMWORD[((192-128))+rsi] vpaddq ymm6,ymm6,ymm13 vpmuludq ymm0,ymm10,YMMWORD[((224-128))+rsi] - vpermq ymm9,ymm9,0x93 + vpermq ymm9,ymm9,0x93 ; correct %ymm3 vpaddq ymm7,ymm7,ymm0 vpmuludq ymm12,ymm10,YMMWORD[((256-128))+rsi] vpbroadcastq ymm10,QWORD[8+r13] @@ -902,10 +902,10 @@ $L$oop_mul_1024: vpmuludq ymm12,ymm11,YMMWORD[((192-128))+rcx] vpaddq ymm6,ymm6,ymm12 vpmuludq ymm13,ymm11,YMMWORD[((224-128))+rcx] - vpblendd ymm12,ymm9,ymm14,3 + vpblendd ymm12,ymm9,ymm14,3 ; correct %ymm3 vpaddq ymm7,ymm7,ymm13 vpmuludq ymm0,ymm11,YMMWORD[((256-128))+rcx] - vpaddq ymm3,ymm3,ymm12 + vpaddq ymm3,ymm3,ymm12 ; correct %ymm3 vpaddq ymm8,ymm8,ymm0 mov rax,rbx @@ -918,9 +918,9 @@ $L$oop_mul_1024: vmovdqu ymm13,YMMWORD[((-8+64-128))+rsi] mov rax,r10 - vpblendd ymm9,ymm9,ymm14,0xfc + vpblendd ymm9,ymm9,ymm14,0xfc ; correct %ymm3 imul eax,r8d - vpaddq ymm4,ymm4,ymm9 + vpaddq ymm4,ymm4,ymm9 ; correct %ymm3 and eax,0x1fffffff imul rbx,QWORD[((16-128))+rsi] @@ -1108,7 +1108,7 @@ $L$oop_mul_1024: vpmuludq ymm13,ymm13,ymm10 vpbroadcastq ymm10,QWORD[32+r13] vpaddq ymm9,ymm9,ymm13 - add r13,32 + add r13,32 ; %r13++ vmovdqu ymm0,YMMWORD[((-24+32-128))+rcx] imul rax,QWORD[((-128))+rcx] @@ -1119,9 +1119,9 @@ $L$oop_mul_1024: vpmuludq ymm0,ymm0,ymm11 vmovq rbx,xmm10 vmovdqu ymm13,YMMWORD[((-24+96-128))+rcx] - vpaddq ymm0,ymm1,ymm0 + vpaddq ymm0,ymm1,ymm0 ; %ymm0==%ymm0 vpmuludq ymm12,ymm12,ymm11 - vmovdqu YMMWORD[rsp],ymm0 + vmovdqu YMMWORD[rsp],ymm0 ; transfer %r9-%r12 vpaddq ymm1,ymm2,ymm12 vmovdqu ymm0,YMMWORD[((-24+128-128))+rcx] vpmuludq ymm13,ymm13,ymm11 @@ -1289,7 +1289,7 @@ $L$mul_1024_in_tail: mov rbx,QWORD[((-8))+rax] - lea rsp,[rax] + lea rsp,[rax] ; restore %rsp $L$mul_1024_epilogue: mov rdi,QWORD[8+rsp] ;WIN64 epilogue @@ -1303,7 +1303,7 @@ ALIGN 32 rsaz_1024_red2norm_avx2: _CET_ENDBR - sub rdx,-128 + sub rdx,-128 ; size optimization xor rax,rax mov r8,QWORD[((-128))+rdx] mov r9,QWORD[((-120))+rdx] @@ -1316,7 +1316,7 @@ _CET_ENDBR add rax,r8 add rax,r9 add rax,r10 - adc r11,0 + adc r11,0 ; consume eventual carry mov QWORD[rcx],rax mov rax,r11 mov r8,QWORD[((-104))+rdx] @@ -1327,7 +1327,7 @@ _CET_ENDBR shr r10,12 add rax,r8 add rax,r9 - adc r10,0 + adc r10,0 ; consume eventual carry mov QWORD[8+rcx],rax mov rax,r10 mov r11,QWORD[((-88))+rdx] @@ -1338,7 +1338,7 @@ _CET_ENDBR shr r9,18 add rax,r11 add rax,r8 - adc r9,0 + adc r9,0 ; consume eventual carry mov QWORD[16+rcx],rax mov rax,r9 mov r10,QWORD[((-72))+rdx] @@ -1349,7 +1349,7 @@ _CET_ENDBR shr r8,24 add rax,r10 add rax,r11 - adc r8,0 + adc r8,0 ; consume eventual carry mov QWORD[24+rcx],rax mov rax,r8 mov r9,QWORD[((-56))+rdx] @@ -1363,7 +1363,7 @@ _CET_ENDBR add rax,r9 add rax,r10 add rax,r11 - adc r8,0 + adc r8,0 ; consume eventual carry mov QWORD[32+rcx],rax mov rax,r8 mov r9,QWORD[((-32))+rdx] @@ -1374,7 +1374,7 @@ _CET_ENDBR shr r11,7 add rax,r9 add rax,r10 - adc r11,0 + adc r11,0 ; consume eventual carry mov QWORD[40+rcx],rax mov rax,r11 mov r8,QWORD[((-16))+rdx] @@ -1385,7 +1385,7 @@ _CET_ENDBR shr r10,13 add rax,r8 add rax,r9 - adc r10,0 + adc r10,0 ; consume eventual carry mov QWORD[48+rcx],rax mov rax,r10 mov r11,QWORD[rdx] @@ -1396,7 +1396,7 @@ _CET_ENDBR shr r9,19 add rax,r11 add rax,r8 - adc r9,0 + adc r9,0 ; consume eventual carry mov QWORD[56+rcx],rax mov rax,r9 mov r10,QWORD[16+rdx] @@ -1407,7 +1407,7 @@ _CET_ENDBR shr r8,25 add rax,r10 add rax,r11 - adc r8,0 + adc r8,0 ; consume eventual carry mov QWORD[64+rcx],rax mov rax,r8 mov r9,QWORD[32+rdx] @@ -1421,7 +1421,7 @@ _CET_ENDBR add rax,r9 add rax,r10 add rax,r11 - adc r8,0 + adc r8,0 ; consume eventual carry mov QWORD[72+rcx],rax mov rax,r8 mov r9,QWORD[56+rdx] @@ -1432,7 +1432,7 @@ _CET_ENDBR shr r11,8 add rax,r9 add rax,r10 - adc r11,0 + adc r11,0 ; consume eventual carry mov QWORD[80+rcx],rax mov rax,r11 mov r8,QWORD[72+rdx] @@ -1443,7 +1443,7 @@ _CET_ENDBR shr r10,14 add rax,r8 add rax,r9 - adc r10,0 + adc r10,0 ; consume eventual carry mov QWORD[88+rcx],rax mov rax,r10 mov r11,QWORD[88+rdx] @@ -1454,7 +1454,7 @@ _CET_ENDBR shr r9,20 add rax,r11 add rax,r8 - adc r9,0 + adc r9,0 ; consume eventual carry mov QWORD[96+rcx],rax mov rax,r9 mov r10,QWORD[104+rdx] @@ -1465,7 +1465,7 @@ _CET_ENDBR shr r8,26 add rax,r10 add rax,r11 - adc r8,0 + adc r8,0 ; consume eventual carry mov QWORD[104+rcx],rax mov rax,r8 mov r9,QWORD[120+rdx] @@ -1479,7 +1479,7 @@ _CET_ENDBR add rax,r9 add rax,r10 add rax,r11 - adc r8,0 + adc r8,0 ; consume eventual carry mov QWORD[112+rcx],rax mov rax,r8 mov r9,QWORD[144+rdx] @@ -1490,7 +1490,7 @@ _CET_ENDBR shr r11,9 add rax,r9 add rax,r10 - adc r11,0 + adc r11,0 ; consume eventual carry mov QWORD[120+rcx],rax mov rax,r11 ret @@ -1503,17 +1503,17 @@ ALIGN 32 rsaz_1024_norm2red_avx2: _CET_ENDBR - sub rcx,-128 + sub rcx,-128 ; size optimization mov r8,QWORD[rdx] mov eax,0x1fffffff mov r9,QWORD[8+rdx] mov r11,r8 shr r11,0 - and r11,rax + and r11,rax ; &0x1fffffff mov QWORD[((-128))+rcx],r11 mov r10,r8 shr r10,29 - and r10,rax + and r10,rax ; &0x1fffffff mov QWORD[((-120))+rcx],r10 shrd r8,r9,58 and r8,rax @@ -1521,7 +1521,7 @@ _CET_ENDBR mov r10,QWORD[16+rdx] mov r8,r9 shr r8,23 - and r8,rax + and r8,rax ; &0x1fffffff mov QWORD[((-104))+rcx],r8 shrd r9,r10,52 and r9,rax @@ -1529,7 +1529,7 @@ _CET_ENDBR mov r11,QWORD[24+rdx] mov r9,r10 shr r9,17 - and r9,rax + and r9,rax ; &0x1fffffff mov QWORD[((-88))+rcx],r9 shrd r10,r11,46 and r10,rax @@ -1537,7 +1537,7 @@ _CET_ENDBR mov r8,QWORD[32+rdx] mov r10,r11 shr r10,11 - and r10,rax + and r10,rax ; &0x1fffffff mov QWORD[((-72))+rcx],r10 shrd r11,r8,40 and r11,rax @@ -1545,11 +1545,11 @@ _CET_ENDBR mov r9,QWORD[40+rdx] mov r11,r8 shr r11,5 - and r11,rax + and r11,rax ; &0x1fffffff mov QWORD[((-56))+rcx],r11 mov r10,r8 shr r10,34 - and r10,rax + and r10,rax ; &0x1fffffff mov QWORD[((-48))+rcx],r10 shrd r8,r9,63 and r8,rax @@ -1557,7 +1557,7 @@ _CET_ENDBR mov r10,QWORD[48+rdx] mov r8,r9 shr r8,28 - and r8,rax + and r8,rax ; &0x1fffffff mov QWORD[((-32))+rcx],r8 shrd r9,r10,57 and r9,rax @@ -1565,7 +1565,7 @@ _CET_ENDBR mov r11,QWORD[56+rdx] mov r9,r10 shr r9,22 - and r9,rax + and r9,rax ; &0x1fffffff mov QWORD[((-16))+rcx],r9 shrd r10,r11,51 and r10,rax @@ -1573,7 +1573,7 @@ _CET_ENDBR mov r8,QWORD[64+rdx] mov r10,r11 shr r10,16 - and r10,rax + and r10,rax ; &0x1fffffff mov QWORD[rcx],r10 shrd r11,r8,45 and r11,rax @@ -1581,7 +1581,7 @@ _CET_ENDBR mov r9,QWORD[72+rdx] mov r11,r8 shr r11,10 - and r11,rax + and r11,rax ; &0x1fffffff mov QWORD[16+rcx],r11 shrd r8,r9,39 and r8,rax @@ -1589,11 +1589,11 @@ _CET_ENDBR mov r10,QWORD[80+rdx] mov r8,r9 shr r8,4 - and r8,rax + and r8,rax ; &0x1fffffff mov QWORD[32+rcx],r8 mov r11,r9 shr r11,33 - and r11,rax + and r11,rax ; &0x1fffffff mov QWORD[40+rcx],r11 shrd r9,r10,62 and r9,rax @@ -1601,7 +1601,7 @@ _CET_ENDBR mov r11,QWORD[88+rdx] mov r9,r10 shr r9,27 - and r9,rax + and r9,rax ; &0x1fffffff mov QWORD[56+rcx],r9 shrd r10,r11,56 and r10,rax @@ -1609,7 +1609,7 @@ _CET_ENDBR mov r8,QWORD[96+rdx] mov r10,r11 shr r10,21 - and r10,rax + and r10,rax ; &0x1fffffff mov QWORD[72+rcx],r10 shrd r11,r8,50 and r11,rax @@ -1617,7 +1617,7 @@ _CET_ENDBR mov r9,QWORD[104+rdx] mov r11,r8 shr r11,15 - and r11,rax + and r11,rax ; &0x1fffffff mov QWORD[88+rcx],r11 shrd r8,r9,44 and r8,rax @@ -1625,7 +1625,7 @@ _CET_ENDBR mov r10,QWORD[112+rdx] mov r8,r9 shr r8,9 - and r8,rax + and r8,rax ; &0x1fffffff mov QWORD[104+rcx],r8 shrd r9,r10,38 and r9,rax @@ -1633,11 +1633,11 @@ _CET_ENDBR mov r11,QWORD[120+rdx] mov r9,r10 shr r9,3 - and r9,rax + and r9,rax ; &0x1fffffff mov QWORD[120+rcx],r9 mov r8,r10 shr r8,32 - and r8,rax + and r8,rax ; &0x1fffffff mov QWORD[128+rcx],r8 shrd r10,r11,61 and r10,rax @@ -1645,12 +1645,12 @@ _CET_ENDBR xor r8,r8 mov r10,r11 shr r10,26 - and r10,rax + and r10,rax ; &0x1fffffff mov QWORD[144+rcx],r10 shrd r11,r8,55 and r11,rax mov QWORD[152+rcx],r11 - mov QWORD[160+rcx],r8 + mov QWORD[160+rcx],r8 ; zero mov QWORD[168+rcx],r8 mov QWORD[176+rcx],r8 mov QWORD[184+rcx],r8 @@ -1696,22 +1696,22 @@ _CET_ENDBR lea rax,[((-136))+rsp] $L$SEH_begin_rsaz_1024_gather5: - - DB 0x48,0x8d,0x60,0xe0 - DB 0xc5,0xf8,0x29,0x70,0xe0 - DB 0xc5,0xf8,0x29,0x78,0xf0 - DB 0xc5,0x78,0x29,0x40,0x00 - DB 0xc5,0x78,0x29,0x48,0x10 - DB 0xc5,0x78,0x29,0x50,0x20 - DB 0xc5,0x78,0x29,0x58,0x30 - DB 0xc5,0x78,0x29,0x60,0x40 - DB 0xc5,0x78,0x29,0x68,0x50 - DB 0xc5,0x78,0x29,0x70,0x60 - DB 0xc5,0x78,0x29,0x78,0x70 +; I can't trust assembler to use specific encoding:-( + DB 0x48,0x8d,0x60,0xe0 ; lea -0x20(%rax),%rsp + DB 0xc5,0xf8,0x29,0x70,0xe0 ; vmovaps %xmm6,-0x20(%rax) + DB 0xc5,0xf8,0x29,0x78,0xf0 ; vmovaps %xmm7,-0x10(%rax) + DB 0xc5,0x78,0x29,0x40,0x00 ; vmovaps %xmm8,0(%rax) + DB 0xc5,0x78,0x29,0x48,0x10 ; vmovaps %xmm9,0x10(%rax) + DB 0xc5,0x78,0x29,0x50,0x20 ; vmovaps %xmm10,0x20(%rax) + DB 0xc5,0x78,0x29,0x58,0x30 ; vmovaps %xmm11,0x30(%rax) + DB 0xc5,0x78,0x29,0x60,0x40 ; vmovaps %xmm12,0x40(%rax) + DB 0xc5,0x78,0x29,0x68,0x50 ; vmovaps %xmm13,0x50(%rax) + DB 0xc5,0x78,0x29,0x70,0x60 ; vmovaps %xmm14,0x60(%rax) + DB 0xc5,0x78,0x29,0x78,0x70 ; vmovaps %xmm15,0x70(%rax) lea rsp,[((-256))+rsp] and rsp,-32 lea r10,[$L$inc] - lea rax,[((-128))+rsp] + lea rax,[((-128))+rsp] ; control u-op density vmovd xmm4,r8d vmovdqa ymm0,YMMWORD[r10] @@ -1758,7 +1758,7 @@ $L$SEH_begin_rsaz_1024_gather5: vpcmpeqd ymm14,ymm14,ymm4 vpcmpeqd ymm15,ymm15,ymm4 - vmovdqa ymm7,YMMWORD[((-32))+r10] + vmovdqa ymm7,YMMWORD[((-32))+r10] ; .Lgather_permd lea rdx,[128+rdx] mov r8d,9 @@ -1804,7 +1804,7 @@ $L$oop_gather_1024: vpor ymm5,ymm5,ymm3 vpor ymm4,ymm4,ymm5 - vextracti128 xmm5,ymm4,1 + vextracti128 xmm5,ymm4,1 ; upper half is cleared vpor xmm5,xmm5,xmm4 vpermd ymm5,ymm7,ymm5 vmovdqu YMMWORD[rcx],ymm5 @@ -1832,7 +1832,9 @@ $L$oop_gather_1024: $L$SEH_end_rsaz_1024_gather5: section .rdata rdata align=8 + ALIGN 64 +rsaz_avx2_constants: $L$and_mask: DQ 0x1fffffff,0x1fffffff,0x1fffffff,0x1fffffff $L$scatter_permd: @@ -1844,7 +1846,7 @@ $L$inc: DD 2,2,2,2,3,3,3,3 DD 4,4,4,4,4,4,4,4 ALIGN 64 -section .text +section .text code align=64 EXTERN __imp_RtlVirtualUnwind @@ -1861,27 +1863,27 @@ rsaz_se_handler: pushfq sub rsp,64 - mov rax,QWORD[120+r8] - mov rbx,QWORD[248+r8] + mov rax,QWORD[120+r8] ; pull context->Rax + mov rbx,QWORD[248+r8] ; pull context->Rip - mov rsi,QWORD[8+r9] - mov r11,QWORD[56+r9] + mov rsi,QWORD[8+r9] ; disp->ImageBase + mov r11,QWORD[56+r9] ; disp->HandlerData - mov r10d,DWORD[r11] - lea r10,[r10*1+rsi] - cmp rbx,r10 + mov r10d,DWORD[r11] ; HandlerData[0] + lea r10,[r10*1+rsi] ; prologue label + cmp rbx,r10 ; context->RipRip>=epilogue label jae NEAR $L$common_seh_tail - mov rbp,QWORD[160+r8] + mov rbp,QWORD[160+r8] ; pull context->Rbp - mov r10d,DWORD[8+r11] - lea r10,[r10*1+rsi] - cmp rbx,r10 + mov r10d,DWORD[8+r11] ; HandlerData[2] + lea r10,[r10*1+rsi] ; "in tail" label + cmp rbx,r10 ; context->Rip>="in tail" label cmovc rax,rbp mov r15,QWORD[((-48))+rax] @@ -1897,38 +1899,38 @@ rsaz_se_handler: mov QWORD[160+r8],rbp mov QWORD[144+r8],rbx - lea rsi,[((-216))+rax] - lea rdi,[512+r8] - mov ecx,20 - DD 0xa548f3fc + lea rsi,[((-216))+rax] ; %xmm save area + lea rdi,[512+r8] ; & context.Xmm6 + mov ecx,20 ; 10*sizeof(%xmm0)/sizeof(%rax) + DD 0xa548f3fc ; cld; rep movsq $L$common_seh_tail: mov rdi,QWORD[8+rax] mov rsi,QWORD[16+rax] - mov QWORD[152+r8],rax - mov QWORD[168+r8],rsi - mov QWORD[176+r8],rdi + mov QWORD[152+r8],rax ; restore context->Rsp + mov QWORD[168+r8],rsi ; restore context->Rsi + mov QWORD[176+r8],rdi ; restore context->Rdi - mov rdi,QWORD[40+r9] - mov rsi,r8 - mov ecx,154 - DD 0xa548f3fc + mov rdi,QWORD[40+r9] ; disp->ContextRecord + mov rsi,r8 ; context + mov ecx,154 ; sizeof(CONTEXT) + DD 0xa548f3fc ; cld; rep movsq mov rsi,r9 - xor rcx,rcx - mov rdx,QWORD[8+rsi] - mov r8,QWORD[rsi] - mov r9,QWORD[16+rsi] - mov r10,QWORD[40+rsi] - lea r11,[56+rsi] - lea r12,[24+rsi] - mov QWORD[32+rsp],r10 - mov QWORD[40+rsp],r11 - mov QWORD[48+rsp],r12 - mov QWORD[56+rsp],rcx + xor rcx,rcx ; arg1, UNW_FLAG_NHANDLER + mov rdx,QWORD[8+rsi] ; arg2, disp->ImageBase + mov r8,QWORD[rsi] ; arg3, disp->ControlPc + mov r9,QWORD[16+rsi] ; arg4, disp->FunctionEntry + mov r10,QWORD[40+rsi] ; disp->ContextRecord + lea r11,[56+rsi] ; &disp->HandlerData + lea r12,[24+rsi] ; &disp->EstablisherFrame + mov QWORD[32+rsp],r10 ; arg5 + mov QWORD[40+rsp],r11 ; arg6 + mov QWORD[48+rsp],r12 ; arg7 + mov QWORD[56+rsp],rcx ; arg8, (NULL) call QWORD[__imp_RtlVirtualUnwind] - mov eax,1 + mov eax,1 ; ExceptionContinueSearch add rsp,64 popfq pop r15 @@ -1969,18 +1971,18 @@ $L$SEH_info_rsaz_1024_mul_avx2: DD 0 $L$SEH_info_rsaz_1024_gather5: DB 0x01,0x36,0x17,0x0b - DB 0x36,0xf8,0x09,0x00 - DB 0x31,0xe8,0x08,0x00 - DB 0x2c,0xd8,0x07,0x00 - DB 0x27,0xc8,0x06,0x00 - DB 0x22,0xb8,0x05,0x00 - DB 0x1d,0xa8,0x04,0x00 - DB 0x18,0x98,0x03,0x00 - DB 0x13,0x88,0x02,0x00 - DB 0x0e,0x78,0x01,0x00 - DB 0x09,0x68,0x00,0x00 - DB 0x04,0x01,0x15,0x00 - DB 0x00,0xb3,0x00,0x00 + DB 0x36,0xf8,0x09,0x00 ; vmovaps 0x90(rsp),xmm15 + DB 0x31,0xe8,0x08,0x00 ; vmovaps 0x80(rsp),xmm14 + DB 0x2c,0xd8,0x07,0x00 ; vmovaps 0x70(rsp),xmm13 + DB 0x27,0xc8,0x06,0x00 ; vmovaps 0x60(rsp),xmm12 + DB 0x22,0xb8,0x05,0x00 ; vmovaps 0x50(rsp),xmm11 + DB 0x1d,0xa8,0x04,0x00 ; vmovaps 0x40(rsp),xmm10 + DB 0x18,0x98,0x03,0x00 ; vmovaps 0x30(rsp),xmm9 + DB 0x13,0x88,0x02,0x00 ; vmovaps 0x20(rsp),xmm8 + DB 0x0e,0x78,0x01,0x00 ; vmovaps 0x10(rsp),xmm7 + DB 0x09,0x68,0x00,0x00 ; vmovaps 0x00(rsp),xmm6 + DB 0x04,0x01,0x15,0x00 ; sub rsp,0xa8 + DB 0x00,0xb3,0x00,0x00 ; set_frame r11 %else ; Work around https://bugzilla.nasm.us/show_bug.cgi?id=3392738 ret diff --git a/third_party/boringssl/gen/bcm/sha1-armv8-apple.S b/third_party/boringssl/gen/bcm/sha1-armv8-apple.S index 5744697fc..547ca655e 100644 --- a/third_party/boringssl/gen/bcm/sha1-armv8-apple.S +++ b/third_party/boringssl/gen/bcm/sha1-armv8-apple.S @@ -1073,6 +1073,7 @@ Loop: .private_extern _sha1_block_data_order_hw .align 6 +.alt_entry _sha1_block_data_order_hw _sha1_block_data_order_hw: // Armv8.3-A PAuth: even though x30 is pushed to stack it is not popped later. AARCH64_VALID_CALL_TARGET @@ -1204,7 +1205,9 @@ Loop_hw: ret .section __TEXT,__const + .align 6 +sha1_constants: Lconst: .long 0x5a827999,0x5a827999,0x5a827999,0x5a827999 //K_00_19 .long 0x6ed9eba1,0x6ed9eba1,0x6ed9eba1,0x6ed9eba1 //K_20_39 diff --git a/third_party/boringssl/gen/bcm/sha1-armv8-linux.S b/third_party/boringssl/gen/bcm/sha1-armv8-linux.S index d6fb410f6..9afcfdf26 100644 --- a/third_party/boringssl/gen/bcm/sha1-armv8-linux.S +++ b/third_party/boringssl/gen/bcm/sha1-armv8-linux.S @@ -1204,7 +1204,9 @@ sha1_block_data_order_hw: ret .size sha1_block_data_order_hw,.-sha1_block_data_order_hw .section .rodata + .align 6 +sha1_constants: .Lconst: .long 0x5a827999,0x5a827999,0x5a827999,0x5a827999 //K_00_19 .long 0x6ed9eba1,0x6ed9eba1,0x6ed9eba1,0x6ed9eba1 //K_20_39 diff --git a/third_party/boringssl/gen/bcm/sha1-armv8-win.S b/third_party/boringssl/gen/bcm/sha1-armv8-win.S index b8161b929..ccff4e886 100644 --- a/third_party/boringssl/gen/bcm/sha1-armv8-win.S +++ b/third_party/boringssl/gen/bcm/sha1-armv8-win.S @@ -1208,7 +1208,9 @@ Loop_hw: ret .section .rodata + .align 6 +sha1_constants: Lconst: .long 0x5a827999,0x5a827999,0x5a827999,0x5a827999 //K_00_19 .long 0x6ed9eba1,0x6ed9eba1,0x6ed9eba1,0x6ed9eba1 //K_20_39 diff --git a/third_party/boringssl/gen/bcm/sha1-x86_64-apple.S b/third_party/boringssl/gen/bcm/sha1-x86_64-apple.S index 32b3bc76c..64ab491f2 100644 --- a/third_party/boringssl/gen/bcm/sha1-x86_64-apple.S +++ b/third_party/boringssl/gen/bcm/sha1-x86_64-apple.S @@ -25,11 +25,11 @@ _CET_ENDBR pushq %r14 - movq %rdi,%r8 + movq %rdi,%r8 // reassigned argument subq $72,%rsp - movq %rsi,%r9 + movq %rsi,%r9 // reassigned argument andq $-64,%rsp - movq %rdx,%r10 + movq %rdx,%r10 // reassigned argument movq %rax,64(%rsp) L$prologue: @@ -1247,37 +1247,38 @@ L$epilogue: .private_extern _sha1_block_data_order_hw .p2align 5 +.alt_entry _sha1_block_data_order_hw _sha1_block_data_order_hw: _CET_ENDBR movdqu (%rdi),%xmm0 movd 16(%rdi),%xmm1 - movdqa K_XX_XX+160(%rip),%xmm3 + movdqa K_XX_XX+160(%rip),%xmm3 // byte-n-word swap movdqu (%rsi),%xmm4 - pshufd $27,%xmm0,%xmm0 + pshufd $27,%xmm0,%xmm0 // flip word order movdqu 16(%rsi),%xmm5 - pshufd $27,%xmm1,%xmm1 + pshufd $27,%xmm1,%xmm1 // flip word order movdqu 32(%rsi),%xmm6 pshufb %xmm3,%xmm4 movdqu 48(%rsi),%xmm7 pshufb %xmm3,%xmm5 pshufb %xmm3,%xmm6 - movdqa %xmm1,%xmm9 + movdqa %xmm1,%xmm9 // offload %xmm1 pshufb %xmm3,%xmm7 jmp L$oop_shaext .p2align 4 L$oop_shaext: decq %rdx - leaq 64(%rsi),%r8 + leaq 64(%rsi),%r8 // next input block paddd %xmm4,%xmm1 cmovneq %r8,%rsi prefetcht0 512(%rsi) - movdqa %xmm0,%xmm8 + movdqa %xmm0,%xmm8 // offload %xmm0 sha1msg1 %xmm5,%xmm4 movdqa %xmm0,%xmm2 - sha1rnds4 $0,%xmm1,%xmm0 + sha1rnds4 $0,%xmm1,%xmm0 // 0-3... sha1nexte %xmm5,%xmm2 pxor %xmm6,%xmm4 sha1msg1 %xmm6,%xmm5 @@ -1290,7 +1291,7 @@ L$oop_shaext: sha1msg2 %xmm4,%xmm5 sha1msg1 %xmm7,%xmm6 movdqa %xmm0,%xmm2 - sha1rnds4 $0,%xmm1,%xmm0 + sha1rnds4 $0,%xmm1,%xmm0 // 0-3... sha1nexte %xmm7,%xmm2 pxor %xmm4,%xmm6 sha1msg1 %xmm4,%xmm7 @@ -1303,7 +1304,7 @@ L$oop_shaext: sha1msg2 %xmm6,%xmm7 sha1msg1 %xmm5,%xmm4 movdqa %xmm0,%xmm2 - sha1rnds4 $0,%xmm1,%xmm0 + sha1rnds4 $0,%xmm1,%xmm0 // 0-3... sha1nexte %xmm5,%xmm2 pxor %xmm6,%xmm4 sha1msg1 %xmm6,%xmm5 @@ -1316,7 +1317,7 @@ L$oop_shaext: sha1msg2 %xmm4,%xmm5 sha1msg1 %xmm7,%xmm6 movdqa %xmm0,%xmm2 - sha1rnds4 $1,%xmm1,%xmm0 + sha1rnds4 $1,%xmm1,%xmm0 // 0-3... sha1nexte %xmm7,%xmm2 pxor %xmm4,%xmm6 sha1msg1 %xmm4,%xmm7 @@ -1329,7 +1330,7 @@ L$oop_shaext: sha1msg2 %xmm6,%xmm7 sha1msg1 %xmm5,%xmm4 movdqa %xmm0,%xmm2 - sha1rnds4 $1,%xmm1,%xmm0 + sha1rnds4 $1,%xmm1,%xmm0 // 0-3... sha1nexte %xmm5,%xmm2 pxor %xmm6,%xmm4 sha1msg1 %xmm6,%xmm5 @@ -1342,7 +1343,7 @@ L$oop_shaext: sha1msg2 %xmm4,%xmm5 sha1msg1 %xmm7,%xmm6 movdqa %xmm0,%xmm2 - sha1rnds4 $2,%xmm1,%xmm0 + sha1rnds4 $2,%xmm1,%xmm0 // 0-3... sha1nexte %xmm7,%xmm2 pxor %xmm4,%xmm6 sha1msg1 %xmm4,%xmm7 @@ -1355,7 +1356,7 @@ L$oop_shaext: sha1msg2 %xmm6,%xmm7 sha1msg1 %xmm5,%xmm4 movdqa %xmm0,%xmm2 - sha1rnds4 $2,%xmm1,%xmm0 + sha1rnds4 $2,%xmm1,%xmm0 // 0-3... sha1nexte %xmm5,%xmm2 pxor %xmm6,%xmm4 sha1msg1 %xmm6,%xmm5 @@ -1368,7 +1369,7 @@ L$oop_shaext: sha1msg2 %xmm4,%xmm5 sha1msg1 %xmm7,%xmm6 movdqa %xmm0,%xmm2 - sha1rnds4 $2,%xmm1,%xmm0 + sha1rnds4 $2,%xmm1,%xmm0 // 0-3... sha1nexte %xmm7,%xmm2 pxor %xmm4,%xmm6 sha1msg1 %xmm4,%xmm7 @@ -1381,30 +1382,30 @@ L$oop_shaext: sha1msg2 %xmm6,%xmm7 movdqu (%rsi),%xmm4 movdqa %xmm0,%xmm2 - sha1rnds4 $3,%xmm1,%xmm0 + sha1rnds4 $3,%xmm1,%xmm0 // 64-67 sha1nexte %xmm5,%xmm2 movdqu 16(%rsi),%xmm5 pshufb %xmm3,%xmm4 movdqa %xmm0,%xmm1 - sha1rnds4 $3,%xmm2,%xmm0 + sha1rnds4 $3,%xmm2,%xmm0 // 68-71 sha1nexte %xmm6,%xmm1 movdqu 32(%rsi),%xmm6 pshufb %xmm3,%xmm5 movdqa %xmm0,%xmm2 - sha1rnds4 $3,%xmm1,%xmm0 + sha1rnds4 $3,%xmm1,%xmm0 // 72-75 sha1nexte %xmm7,%xmm2 movdqu 48(%rsi),%xmm7 pshufb %xmm3,%xmm6 movdqa %xmm0,%xmm1 - sha1rnds4 $3,%xmm2,%xmm0 + sha1rnds4 $3,%xmm2,%xmm0 // 76-79 sha1nexte %xmm9,%xmm1 pshufb %xmm3,%xmm7 paddd %xmm8,%xmm0 - movdqa %xmm1,%xmm9 + movdqa %xmm1,%xmm9 // offload %xmm1 jnz L$oop_shaext @@ -1419,10 +1420,11 @@ L$oop_shaext: .private_extern _sha1_block_data_order_ssse3 .p2align 4 +.alt_entry _sha1_block_data_order_ssse3 _sha1_block_data_order_ssse3: _CET_ENDBR - movq %rsp,%r11 + movq %rsp,%r11 // frame pointer pushq %rbx @@ -1430,46 +1432,46 @@ _CET_ENDBR pushq %r12 - pushq %r13 + pushq %r13 // redundant, done to share Win64 SE handler pushq %r14 leaq -64(%rsp),%rsp andq $-64,%rsp - movq %rdi,%r8 - movq %rsi,%r9 - movq %rdx,%r10 + movq %rdi,%r8 // reassigned argument + movq %rsi,%r9 // reassigned argument + movq %rdx,%r10 // reassigned argument shlq $6,%r10 addq %r9,%r10 leaq K_XX_XX+64(%rip),%r14 - movl 0(%r8),%eax + movl 0(%r8),%eax // load context movl 4(%r8),%ebx movl 8(%r8),%ecx movl 12(%r8),%edx - movl %ebx,%esi + movl %ebx,%esi // magic seed movl 16(%r8),%ebp movl %ecx,%edi xorl %edx,%edi andl %edi,%esi - movdqa 64(%r14),%xmm6 - movdqa -64(%r14),%xmm9 - movdqu 0(%r9),%xmm0 + movdqa 64(%r14),%xmm6 // pbswap mask + movdqa -64(%r14),%xmm9 // K_00_19 + movdqu 0(%r9),%xmm0 // load input to %xmm[0-3] movdqu 16(%r9),%xmm1 movdqu 32(%r9),%xmm2 movdqu 48(%r9),%xmm3 - pshufb %xmm6,%xmm0 + pshufb %xmm6,%xmm0 // byte swap pshufb %xmm6,%xmm1 pshufb %xmm6,%xmm2 addq $64,%r9 - paddd %xmm9,%xmm0 + paddd %xmm9,%xmm0 // add K_00_19 pshufb %xmm6,%xmm3 paddd %xmm9,%xmm1 paddd %xmm9,%xmm2 - movdqa %xmm0,0(%rsp) - psubd %xmm9,%xmm0 + movdqa %xmm0,0(%rsp) // X[]+K xfer to IALU + psubd %xmm9,%xmm0 // restore X[] movdqa %xmm1,16(%rsp) psubd %xmm9,%xmm1 movdqa %xmm2,32(%rsp) @@ -2466,14 +2468,14 @@ L$oop_ssse3: addl %edi,%eax rorl $7,%ecx addl %ebx,%eax - addl 0(%r8),%eax + addl 0(%r8),%eax // update context addl 4(%r8),%esi addl 8(%r8),%ecx addl 12(%r8),%edx movl %eax,0(%r8) addl 16(%r8),%ebp movl %esi,4(%r8) - movl %esi,%ebx + movl %esi,%ebx // magic seed movl %ecx,8(%r8) movl %ecx,%edi movl %edx,12(%r8) @@ -2579,7 +2581,7 @@ L$done_ssse3: addl %edi,%eax rorl $7,%ecx addl %ebx,%eax - addl 0(%r8),%eax + addl 0(%r8),%eax // update context addl 4(%r8),%esi addl 8(%r8),%ecx movl %eax,0(%r8) @@ -2609,6 +2611,7 @@ L$epilogue_ssse3: .private_extern _sha1_block_data_order_avx .p2align 4 +.alt_entry _sha1_block_data_order_avx _sha1_block_data_order_avx: _CET_ENDBR @@ -2620,46 +2623,46 @@ _CET_ENDBR pushq %r12 - pushq %r13 + pushq %r13 // redundant, done to share Win64 SE handler pushq %r14 leaq -64(%rsp),%rsp vzeroupper andq $-64,%rsp - movq %rdi,%r8 - movq %rsi,%r9 - movq %rdx,%r10 + movq %rdi,%r8 // reassigned argument + movq %rsi,%r9 // reassigned argument + movq %rdx,%r10 // reassigned argument shlq $6,%r10 addq %r9,%r10 leaq K_XX_XX+64(%rip),%r14 - movl 0(%r8),%eax + movl 0(%r8),%eax // load context movl 4(%r8),%ebx movl 8(%r8),%ecx movl 12(%r8),%edx - movl %ebx,%esi + movl %ebx,%esi // magic seed movl 16(%r8),%ebp movl %ecx,%edi xorl %edx,%edi andl %edi,%esi - vmovdqa 64(%r14),%xmm6 - vmovdqa -64(%r14),%xmm11 - vmovdqu 0(%r9),%xmm0 + vmovdqa 64(%r14),%xmm6 // pbswap mask + vmovdqa -64(%r14),%xmm11 // K_00_19 + vmovdqu 0(%r9),%xmm0 // load input to %xmm[0-3] vmovdqu 16(%r9),%xmm1 vmovdqu 32(%r9),%xmm2 vmovdqu 48(%r9),%xmm3 - vpshufb %xmm6,%xmm0,%xmm0 + vpshufb %xmm6,%xmm0,%xmm0 // byte swap addq $64,%r9 vpshufb %xmm6,%xmm1,%xmm1 vpshufb %xmm6,%xmm2,%xmm2 vpshufb %xmm6,%xmm3,%xmm3 - vpaddd %xmm11,%xmm0,%xmm4 + vpaddd %xmm11,%xmm0,%xmm4 // add K_00_19 vpaddd %xmm11,%xmm1,%xmm5 vpaddd %xmm11,%xmm2,%xmm6 - vmovdqa %xmm4,0(%rsp) + vmovdqa %xmm4,0(%rsp) // X[]+K xfer to IALU vmovdqa %xmm5,16(%rsp) vmovdqa %xmm6,32(%rsp) jmp L$oop_avx @@ -3594,14 +3597,14 @@ L$oop_avx: addl %edi,%eax shrdl $7,%ecx,%ecx addl %ebx,%eax - addl 0(%r8),%eax + addl 0(%r8),%eax // update context addl 4(%r8),%esi addl 8(%r8),%ecx addl 12(%r8),%edx movl %eax,0(%r8) addl 16(%r8),%ebp movl %esi,4(%r8) - movl %esi,%ebx + movl %esi,%ebx // magic seed movl %ecx,8(%r8) movl %ecx,%edi movl %edx,12(%r8) @@ -3709,7 +3712,7 @@ L$done_avx: addl %ebx,%eax vzeroupper - addl 0(%r8),%eax + addl 0(%r8),%eax // update context addl 4(%r8),%esi addl 8(%r8),%ecx movl %eax,0(%r8) @@ -3739,6 +3742,7 @@ L$epilogue_avx: .private_extern _sha1_block_data_order_avx2 .p2align 4 +.alt_entry _sha1_block_data_order_avx2 _sha1_block_data_order_avx2: _CET_ENDBR @@ -3755,9 +3759,9 @@ _CET_ENDBR pushq %r14 vzeroupper - movq %rdi,%r8 - movq %rsi,%r9 - movq %rdx,%r10 + movq %rdi,%r8 // reassigned argument + movq %rsi,%r9 // reassigned argument + movq %rdx,%r10 // reassigned argument leaq -640(%rsp),%rsp shlq $6,%r10 @@ -3766,14 +3770,14 @@ _CET_ENDBR addq %r9,%r10 leaq K_XX_XX+64(%rip),%r14 - movl 0(%r8),%eax + movl 0(%r8),%eax // load context cmpq %r10,%r13 - cmovaeq %r9,%r13 + cmovaeq %r9,%r13 // next or same block movl 4(%r8),%ebp movl 8(%r8),%ecx movl 12(%r8),%edx movl 16(%r8),%esi - vmovdqu 64(%r14),%ymm6 + vmovdqu 64(%r14),%ymm6 // pbswap mask vmovdqu (%r9),%xmm0 vmovdqu 16(%r9),%xmm1 @@ -3787,12 +3791,12 @@ _CET_ENDBR vpshufb %ymm6,%ymm1,%ymm1 vinserti128 $1,48(%r13),%ymm3,%ymm3 vpshufb %ymm6,%ymm2,%ymm2 - vmovdqu -64(%r14),%ymm11 + vmovdqu -64(%r14),%ymm11 // K_00_19 vpshufb %ymm6,%ymm3,%ymm3 - vpaddd %ymm11,%ymm0,%ymm4 + vpaddd %ymm11,%ymm0,%ymm4 // add K_00_19 vpaddd %ymm11,%ymm1,%ymm5 - vmovdqu %ymm4,0(%rsp) + vmovdqu %ymm4,0(%rsp) // X[]+K xfer to IALU vpaddd %ymm11,%ymm2,%ymm6 vmovdqu %ymm5,32(%rsp) vpaddd %ymm11,%ymm3,%ymm7 @@ -3867,7 +3871,7 @@ L$oop_avx2: andnl %edx,%ebp,%edi andl %ecx,%ebp xorl %edi,%ebp - jmp L$align32_1 + jmp L$align32_1 // see "Decoded ICache" in manual .p2align 5 L$align32_1: vpalignr $8,%ymm6,%ymm7,%ymm8 @@ -4246,7 +4250,7 @@ L$align32_1: xorl %esi,%ecx addl %r12d,%ebx andl %edi,%ecx - jmp L$align32_2 + jmp L$align32_2 // see "Decoded ICache" in manual .p2align 5 L$align32_2: vpalignr $8,%ymm6,%ymm7,%ymm8 @@ -4622,38 +4626,38 @@ L$align32_2: rorxl $27,%esi,%r12d addl %r12d,%edx leaq 128(%r9),%r13 - leaq 128(%r9),%rdi + leaq 128(%r9),%rdi // borrow %edi cmpq %r10,%r13 - cmovaeq %r9,%r13 + cmovaeq %r9,%r13 // next or previous block - - addl 0(%r8),%edx +// output is d-e-[a]-f-b-c => A=d,F=e,C=f,D=b,E=c + addl 0(%r8),%edx // update context addl 4(%r8),%esi addl 8(%r8),%ebp movl %edx,0(%r8) addl 12(%r8),%ebx movl %esi,4(%r8) - movl %edx,%eax + movl %edx,%eax // A=d addl 16(%r8),%ecx movl %ebp,%r12d movl %ebp,8(%r8) - movl %ebx,%edx - + movl %ebx,%edx // D=b +// xchg %ecx,%ebp # F=c, C=f movl %ebx,12(%r8) - movl %esi,%ebp + movl %esi,%ebp // F=e movl %ecx,16(%r8) - - movl %ecx,%esi - movl %r12d,%ecx - +// mov %ebp,16(%r8) + movl %ecx,%esi // E=c + movl %r12d,%ecx // C=f +// xchg %ebp,%esi # E=c, F=e cmpq %r10,%r9 je L$done_avx2 - vmovdqu 64(%r14),%ymm6 - cmpq %r10,%rdi + vmovdqu 64(%r14),%ymm6 // pbswap mask + cmpq %r10,%rdi // borrowed %edi ja L$ast_avx2 - vmovdqu -64(%rdi),%xmm0 + vmovdqu -64(%rdi),%xmm0 // low part of %ymm0 vmovdqu -48(%rdi),%xmm1 vmovdqu -32(%rdi),%xmm2 vmovdqu -16(%rdi),%xmm3 @@ -5033,7 +5037,7 @@ L$ast_avx2: xorl %eax,%edx addl %r12d,%ecx andl %edi,%edx - jmp L$align32_3 + jmp L$align32_3 // see "Decoded ICache" in manual .p2align 5 L$align32_3: vmovdqu %ymm6,64(%rsp) @@ -5388,26 +5392,26 @@ L$align32_3: addl %r12d,%edx leaq 128(%rsp),%r13 - - addl 0(%r8),%edx +// output is d-e-[a]-f-b-c => A=d,F=e,C=f,D=b,E=c + addl 0(%r8),%edx // update context addl 4(%r8),%esi addl 8(%r8),%ebp movl %edx,0(%r8) addl 12(%r8),%ebx movl %esi,4(%r8) - movl %edx,%eax + movl %edx,%eax // A=d addl 16(%r8),%ecx movl %ebp,%r12d movl %ebp,8(%r8) - movl %ebx,%edx - + movl %ebx,%edx // D=b +// xchg %ecx,%ebp # F=c, C=f movl %ebx,12(%r8) - movl %esi,%ebp + movl %esi,%ebp // F=e movl %ecx,16(%r8) - - movl %ecx,%esi - movl %r12d,%ecx - +// mov %ebp,16(%r8) + movl %ecx,%esi // E=c + movl %r12d,%ecx // C=f +// xchg %ebp,%esi # E=c, F=e cmpq %r10,%r9 jbe L$oop_avx2 @@ -5433,16 +5437,16 @@ L$epilogue_avx2: .section __DATA,__const .p2align 6 K_XX_XX: -.long 0x5a827999,0x5a827999,0x5a827999,0x5a827999 -.long 0x5a827999,0x5a827999,0x5a827999,0x5a827999 -.long 0x6ed9eba1,0x6ed9eba1,0x6ed9eba1,0x6ed9eba1 -.long 0x6ed9eba1,0x6ed9eba1,0x6ed9eba1,0x6ed9eba1 -.long 0x8f1bbcdc,0x8f1bbcdc,0x8f1bbcdc,0x8f1bbcdc -.long 0x8f1bbcdc,0x8f1bbcdc,0x8f1bbcdc,0x8f1bbcdc -.long 0xca62c1d6,0xca62c1d6,0xca62c1d6,0xca62c1d6 -.long 0xca62c1d6,0xca62c1d6,0xca62c1d6,0xca62c1d6 -.long 0x00010203,0x04050607,0x08090a0b,0x0c0d0e0f -.long 0x00010203,0x04050607,0x08090a0b,0x0c0d0e0f +.long 0x5a827999,0x5a827999,0x5a827999,0x5a827999 // K_00_19 +.long 0x5a827999,0x5a827999,0x5a827999,0x5a827999 // K_00_19 +.long 0x6ed9eba1,0x6ed9eba1,0x6ed9eba1,0x6ed9eba1 // K_20_39 +.long 0x6ed9eba1,0x6ed9eba1,0x6ed9eba1,0x6ed9eba1 // K_20_39 +.long 0x8f1bbcdc,0x8f1bbcdc,0x8f1bbcdc,0x8f1bbcdc // K_40_59 +.long 0x8f1bbcdc,0x8f1bbcdc,0x8f1bbcdc,0x8f1bbcdc // K_40_59 +.long 0xca62c1d6,0xca62c1d6,0xca62c1d6,0xca62c1d6 // K_60_79 +.long 0xca62c1d6,0xca62c1d6,0xca62c1d6,0xca62c1d6 // K_60_79 +.long 0x00010203,0x04050607,0x08090a0b,0x0c0d0e0f // pbswap mask +.long 0x00010203,0x04050607,0x08090a0b,0x0c0d0e0f // pbswap mask .byte 0xf,0xe,0xd,0xc,0xb,0xa,0x9,0x8,0x7,0x6,0x5,0x4,0x3,0x2,0x1,0x0 .byte 83,72,65,49,32,98,108,111,99,107,32,116,114,97,110,115,102,111,114,109,32,102,111,114,32,120,56,54,95,54,52,44,32,67,82,89,80,84,79,71,65,77,83,32,98,121,32,60,97,112,112,114,111,64,111,112,101,110,115,115,108,46,111,114,103,62,0 .p2align 6 diff --git a/third_party/boringssl/gen/bcm/sha1-x86_64-linux.S b/third_party/boringssl/gen/bcm/sha1-x86_64-linux.S index 1f4807a70..005d4c3f6 100644 --- a/third_party/boringssl/gen/bcm/sha1-x86_64-linux.S +++ b/third_party/boringssl/gen/bcm/sha1-x86_64-linux.S @@ -25,11 +25,11 @@ _CET_ENDBR .cfi_offset %r13,-40 pushq %r14 .cfi_offset %r14,-48 - movq %rdi,%r8 + movq %rdi,%r8 // reassigned argument subq $72,%rsp - movq %rsi,%r9 + movq %rsi,%r9 // reassigned argument andq $-64,%rsp - movq %rdx,%r10 + movq %rdx,%r10 // reassigned argument movq %rax,64(%rsp) .cfi_escape 0x0f,0x06,0x77,0xc0,0x00,0x06,0x23,0x08 .Lprologue: @@ -1252,32 +1252,32 @@ sha1_block_data_order_hw: _CET_ENDBR movdqu (%rdi),%xmm0 movd 16(%rdi),%xmm1 - movdqa K_XX_XX+160(%rip),%xmm3 + movdqa K_XX_XX+160(%rip),%xmm3 // byte-n-word swap movdqu (%rsi),%xmm4 - pshufd $27,%xmm0,%xmm0 + pshufd $27,%xmm0,%xmm0 // flip word order movdqu 16(%rsi),%xmm5 - pshufd $27,%xmm1,%xmm1 + pshufd $27,%xmm1,%xmm1 // flip word order movdqu 32(%rsi),%xmm6 pshufb %xmm3,%xmm4 movdqu 48(%rsi),%xmm7 pshufb %xmm3,%xmm5 pshufb %xmm3,%xmm6 - movdqa %xmm1,%xmm9 + movdqa %xmm1,%xmm9 // offload %xmm1 pshufb %xmm3,%xmm7 jmp .Loop_shaext .align 16 .Loop_shaext: decq %rdx - leaq 64(%rsi),%r8 + leaq 64(%rsi),%r8 // next input block paddd %xmm4,%xmm1 cmovneq %r8,%rsi prefetcht0 512(%rsi) - movdqa %xmm0,%xmm8 + movdqa %xmm0,%xmm8 // offload %xmm0 sha1msg1 %xmm5,%xmm4 movdqa %xmm0,%xmm2 - sha1rnds4 $0,%xmm1,%xmm0 + sha1rnds4 $0,%xmm1,%xmm0 // 0-3... sha1nexte %xmm5,%xmm2 pxor %xmm6,%xmm4 sha1msg1 %xmm6,%xmm5 @@ -1290,7 +1290,7 @@ _CET_ENDBR sha1msg2 %xmm4,%xmm5 sha1msg1 %xmm7,%xmm6 movdqa %xmm0,%xmm2 - sha1rnds4 $0,%xmm1,%xmm0 + sha1rnds4 $0,%xmm1,%xmm0 // 0-3... sha1nexte %xmm7,%xmm2 pxor %xmm4,%xmm6 sha1msg1 %xmm4,%xmm7 @@ -1303,7 +1303,7 @@ _CET_ENDBR sha1msg2 %xmm6,%xmm7 sha1msg1 %xmm5,%xmm4 movdqa %xmm0,%xmm2 - sha1rnds4 $0,%xmm1,%xmm0 + sha1rnds4 $0,%xmm1,%xmm0 // 0-3... sha1nexte %xmm5,%xmm2 pxor %xmm6,%xmm4 sha1msg1 %xmm6,%xmm5 @@ -1316,7 +1316,7 @@ _CET_ENDBR sha1msg2 %xmm4,%xmm5 sha1msg1 %xmm7,%xmm6 movdqa %xmm0,%xmm2 - sha1rnds4 $1,%xmm1,%xmm0 + sha1rnds4 $1,%xmm1,%xmm0 // 0-3... sha1nexte %xmm7,%xmm2 pxor %xmm4,%xmm6 sha1msg1 %xmm4,%xmm7 @@ -1329,7 +1329,7 @@ _CET_ENDBR sha1msg2 %xmm6,%xmm7 sha1msg1 %xmm5,%xmm4 movdqa %xmm0,%xmm2 - sha1rnds4 $1,%xmm1,%xmm0 + sha1rnds4 $1,%xmm1,%xmm0 // 0-3... sha1nexte %xmm5,%xmm2 pxor %xmm6,%xmm4 sha1msg1 %xmm6,%xmm5 @@ -1342,7 +1342,7 @@ _CET_ENDBR sha1msg2 %xmm4,%xmm5 sha1msg1 %xmm7,%xmm6 movdqa %xmm0,%xmm2 - sha1rnds4 $2,%xmm1,%xmm0 + sha1rnds4 $2,%xmm1,%xmm0 // 0-3... sha1nexte %xmm7,%xmm2 pxor %xmm4,%xmm6 sha1msg1 %xmm4,%xmm7 @@ -1355,7 +1355,7 @@ _CET_ENDBR sha1msg2 %xmm6,%xmm7 sha1msg1 %xmm5,%xmm4 movdqa %xmm0,%xmm2 - sha1rnds4 $2,%xmm1,%xmm0 + sha1rnds4 $2,%xmm1,%xmm0 // 0-3... sha1nexte %xmm5,%xmm2 pxor %xmm6,%xmm4 sha1msg1 %xmm6,%xmm5 @@ -1368,7 +1368,7 @@ _CET_ENDBR sha1msg2 %xmm4,%xmm5 sha1msg1 %xmm7,%xmm6 movdqa %xmm0,%xmm2 - sha1rnds4 $2,%xmm1,%xmm0 + sha1rnds4 $2,%xmm1,%xmm0 // 0-3... sha1nexte %xmm7,%xmm2 pxor %xmm4,%xmm6 sha1msg1 %xmm4,%xmm7 @@ -1381,30 +1381,30 @@ _CET_ENDBR sha1msg2 %xmm6,%xmm7 movdqu (%rsi),%xmm4 movdqa %xmm0,%xmm2 - sha1rnds4 $3,%xmm1,%xmm0 + sha1rnds4 $3,%xmm1,%xmm0 // 64-67 sha1nexte %xmm5,%xmm2 movdqu 16(%rsi),%xmm5 pshufb %xmm3,%xmm4 movdqa %xmm0,%xmm1 - sha1rnds4 $3,%xmm2,%xmm0 + sha1rnds4 $3,%xmm2,%xmm0 // 68-71 sha1nexte %xmm6,%xmm1 movdqu 32(%rsi),%xmm6 pshufb %xmm3,%xmm5 movdqa %xmm0,%xmm2 - sha1rnds4 $3,%xmm1,%xmm0 + sha1rnds4 $3,%xmm1,%xmm0 // 72-75 sha1nexte %xmm7,%xmm2 movdqu 48(%rsi),%xmm7 pshufb %xmm3,%xmm6 movdqa %xmm0,%xmm1 - sha1rnds4 $3,%xmm2,%xmm0 + sha1rnds4 $3,%xmm2,%xmm0 // 76-79 sha1nexte %xmm9,%xmm1 pshufb %xmm3,%xmm7 paddd %xmm8,%xmm0 - movdqa %xmm1,%xmm9 + movdqa %xmm1,%xmm9 // offload %xmm1 jnz .Loop_shaext @@ -1422,7 +1422,7 @@ _CET_ENDBR sha1_block_data_order_ssse3: .cfi_startproc _CET_ENDBR - movq %rsp,%r11 + movq %rsp,%r11 // frame pointer .cfi_def_cfa_register %r11 pushq %rbx .cfi_offset %rbx,-16 @@ -1430,46 +1430,46 @@ _CET_ENDBR .cfi_offset %rbp,-24 pushq %r12 .cfi_offset %r12,-32 - pushq %r13 + pushq %r13 // redundant, done to share Win64 SE handler .cfi_offset %r13,-40 pushq %r14 .cfi_offset %r14,-48 leaq -64(%rsp),%rsp andq $-64,%rsp - movq %rdi,%r8 - movq %rsi,%r9 - movq %rdx,%r10 + movq %rdi,%r8 // reassigned argument + movq %rsi,%r9 // reassigned argument + movq %rdx,%r10 // reassigned argument shlq $6,%r10 addq %r9,%r10 leaq K_XX_XX+64(%rip),%r14 - movl 0(%r8),%eax + movl 0(%r8),%eax // load context movl 4(%r8),%ebx movl 8(%r8),%ecx movl 12(%r8),%edx - movl %ebx,%esi + movl %ebx,%esi // magic seed movl 16(%r8),%ebp movl %ecx,%edi xorl %edx,%edi andl %edi,%esi - movdqa 64(%r14),%xmm6 - movdqa -64(%r14),%xmm9 - movdqu 0(%r9),%xmm0 + movdqa 64(%r14),%xmm6 // pbswap mask + movdqa -64(%r14),%xmm9 // K_00_19 + movdqu 0(%r9),%xmm0 // load input to %xmm[0-3] movdqu 16(%r9),%xmm1 movdqu 32(%r9),%xmm2 movdqu 48(%r9),%xmm3 - pshufb %xmm6,%xmm0 + pshufb %xmm6,%xmm0 // byte swap pshufb %xmm6,%xmm1 pshufb %xmm6,%xmm2 addq $64,%r9 - paddd %xmm9,%xmm0 + paddd %xmm9,%xmm0 // add K_00_19 pshufb %xmm6,%xmm3 paddd %xmm9,%xmm1 paddd %xmm9,%xmm2 - movdqa %xmm0,0(%rsp) - psubd %xmm9,%xmm0 + movdqa %xmm0,0(%rsp) // X[]+K xfer to IALU + psubd %xmm9,%xmm0 // restore X[] movdqa %xmm1,16(%rsp) psubd %xmm9,%xmm1 movdqa %xmm2,32(%rsp) @@ -2466,14 +2466,14 @@ _CET_ENDBR addl %edi,%eax rorl $7,%ecx addl %ebx,%eax - addl 0(%r8),%eax + addl 0(%r8),%eax // update context addl 4(%r8),%esi addl 8(%r8),%ecx addl 12(%r8),%edx movl %eax,0(%r8) addl 16(%r8),%ebp movl %esi,4(%r8) - movl %esi,%ebx + movl %esi,%ebx // magic seed movl %ecx,8(%r8) movl %ecx,%edi movl %edx,12(%r8) @@ -2579,7 +2579,7 @@ _CET_ENDBR addl %edi,%eax rorl $7,%ecx addl %ebx,%eax - addl 0(%r8),%eax + addl 0(%r8),%eax // update context addl 4(%r8),%esi addl 8(%r8),%ecx movl %eax,0(%r8) @@ -2620,46 +2620,46 @@ _CET_ENDBR .cfi_offset %rbp,-24 pushq %r12 .cfi_offset %r12,-32 - pushq %r13 + pushq %r13 // redundant, done to share Win64 SE handler .cfi_offset %r13,-40 pushq %r14 .cfi_offset %r14,-48 leaq -64(%rsp),%rsp vzeroupper andq $-64,%rsp - movq %rdi,%r8 - movq %rsi,%r9 - movq %rdx,%r10 + movq %rdi,%r8 // reassigned argument + movq %rsi,%r9 // reassigned argument + movq %rdx,%r10 // reassigned argument shlq $6,%r10 addq %r9,%r10 leaq K_XX_XX+64(%rip),%r14 - movl 0(%r8),%eax + movl 0(%r8),%eax // load context movl 4(%r8),%ebx movl 8(%r8),%ecx movl 12(%r8),%edx - movl %ebx,%esi + movl %ebx,%esi // magic seed movl 16(%r8),%ebp movl %ecx,%edi xorl %edx,%edi andl %edi,%esi - vmovdqa 64(%r14),%xmm6 - vmovdqa -64(%r14),%xmm11 - vmovdqu 0(%r9),%xmm0 + vmovdqa 64(%r14),%xmm6 // pbswap mask + vmovdqa -64(%r14),%xmm11 // K_00_19 + vmovdqu 0(%r9),%xmm0 // load input to %xmm[0-3] vmovdqu 16(%r9),%xmm1 vmovdqu 32(%r9),%xmm2 vmovdqu 48(%r9),%xmm3 - vpshufb %xmm6,%xmm0,%xmm0 + vpshufb %xmm6,%xmm0,%xmm0 // byte swap addq $64,%r9 vpshufb %xmm6,%xmm1,%xmm1 vpshufb %xmm6,%xmm2,%xmm2 vpshufb %xmm6,%xmm3,%xmm3 - vpaddd %xmm11,%xmm0,%xmm4 + vpaddd %xmm11,%xmm0,%xmm4 // add K_00_19 vpaddd %xmm11,%xmm1,%xmm5 vpaddd %xmm11,%xmm2,%xmm6 - vmovdqa %xmm4,0(%rsp) + vmovdqa %xmm4,0(%rsp) // X[]+K xfer to IALU vmovdqa %xmm5,16(%rsp) vmovdqa %xmm6,32(%rsp) jmp .Loop_avx @@ -3594,14 +3594,14 @@ _CET_ENDBR addl %edi,%eax shrdl $7,%ecx,%ecx addl %ebx,%eax - addl 0(%r8),%eax + addl 0(%r8),%eax // update context addl 4(%r8),%esi addl 8(%r8),%ecx addl 12(%r8),%edx movl %eax,0(%r8) addl 16(%r8),%ebp movl %esi,4(%r8) - movl %esi,%ebx + movl %esi,%ebx // magic seed movl %ecx,8(%r8) movl %ecx,%edi movl %edx,12(%r8) @@ -3709,7 +3709,7 @@ _CET_ENDBR addl %ebx,%eax vzeroupper - addl 0(%r8),%eax + addl 0(%r8),%eax // update context addl 4(%r8),%esi addl 8(%r8),%ecx movl %eax,0(%r8) @@ -3755,9 +3755,9 @@ _CET_ENDBR pushq %r14 .cfi_offset %r14,-48 vzeroupper - movq %rdi,%r8 - movq %rsi,%r9 - movq %rdx,%r10 + movq %rdi,%r8 // reassigned argument + movq %rsi,%r9 // reassigned argument + movq %rdx,%r10 // reassigned argument leaq -640(%rsp),%rsp shlq $6,%r10 @@ -3766,14 +3766,14 @@ _CET_ENDBR addq %r9,%r10 leaq K_XX_XX+64(%rip),%r14 - movl 0(%r8),%eax + movl 0(%r8),%eax // load context cmpq %r10,%r13 - cmovaeq %r9,%r13 + cmovaeq %r9,%r13 // next or same block movl 4(%r8),%ebp movl 8(%r8),%ecx movl 12(%r8),%edx movl 16(%r8),%esi - vmovdqu 64(%r14),%ymm6 + vmovdqu 64(%r14),%ymm6 // pbswap mask vmovdqu (%r9),%xmm0 vmovdqu 16(%r9),%xmm1 @@ -3787,12 +3787,12 @@ _CET_ENDBR vpshufb %ymm6,%ymm1,%ymm1 vinserti128 $1,48(%r13),%ymm3,%ymm3 vpshufb %ymm6,%ymm2,%ymm2 - vmovdqu -64(%r14),%ymm11 + vmovdqu -64(%r14),%ymm11 // K_00_19 vpshufb %ymm6,%ymm3,%ymm3 - vpaddd %ymm11,%ymm0,%ymm4 + vpaddd %ymm11,%ymm0,%ymm4 // add K_00_19 vpaddd %ymm11,%ymm1,%ymm5 - vmovdqu %ymm4,0(%rsp) + vmovdqu %ymm4,0(%rsp) // X[]+K xfer to IALU vpaddd %ymm11,%ymm2,%ymm6 vmovdqu %ymm5,32(%rsp) vpaddd %ymm11,%ymm3,%ymm7 @@ -3867,7 +3867,7 @@ _CET_ENDBR andnl %edx,%ebp,%edi andl %ecx,%ebp xorl %edi,%ebp - jmp .Lalign32_1 + jmp .Lalign32_1 // see "Decoded ICache" in manual .align 32 .Lalign32_1: vpalignr $8,%ymm6,%ymm7,%ymm8 @@ -4246,7 +4246,7 @@ _CET_ENDBR xorl %esi,%ecx addl %r12d,%ebx andl %edi,%ecx - jmp .Lalign32_2 + jmp .Lalign32_2 // see "Decoded ICache" in manual .align 32 .Lalign32_2: vpalignr $8,%ymm6,%ymm7,%ymm8 @@ -4622,38 +4622,38 @@ _CET_ENDBR rorxl $27,%esi,%r12d addl %r12d,%edx leaq 128(%r9),%r13 - leaq 128(%r9),%rdi + leaq 128(%r9),%rdi // borrow %edi cmpq %r10,%r13 - cmovaeq %r9,%r13 + cmovaeq %r9,%r13 // next or previous block - - addl 0(%r8),%edx +// output is d-e-[a]-f-b-c => A=d,F=e,C=f,D=b,E=c + addl 0(%r8),%edx // update context addl 4(%r8),%esi addl 8(%r8),%ebp movl %edx,0(%r8) addl 12(%r8),%ebx movl %esi,4(%r8) - movl %edx,%eax + movl %edx,%eax // A=d addl 16(%r8),%ecx movl %ebp,%r12d movl %ebp,8(%r8) - movl %ebx,%edx - + movl %ebx,%edx // D=b +// xchg %ecx,%ebp # F=c, C=f movl %ebx,12(%r8) - movl %esi,%ebp + movl %esi,%ebp // F=e movl %ecx,16(%r8) - - movl %ecx,%esi - movl %r12d,%ecx - +// mov %ebp,16(%r8) + movl %ecx,%esi // E=c + movl %r12d,%ecx // C=f +// xchg %ebp,%esi # E=c, F=e cmpq %r10,%r9 je .Ldone_avx2 - vmovdqu 64(%r14),%ymm6 - cmpq %r10,%rdi + vmovdqu 64(%r14),%ymm6 // pbswap mask + cmpq %r10,%rdi // borrowed %edi ja .Last_avx2 - vmovdqu -64(%rdi),%xmm0 + vmovdqu -64(%rdi),%xmm0 // low part of %ymm0 vmovdqu -48(%rdi),%xmm1 vmovdqu -32(%rdi),%xmm2 vmovdqu -16(%rdi),%xmm3 @@ -5033,7 +5033,7 @@ _CET_ENDBR xorl %eax,%edx addl %r12d,%ecx andl %edi,%edx - jmp .Lalign32_3 + jmp .Lalign32_3 // see "Decoded ICache" in manual .align 32 .Lalign32_3: vmovdqu %ymm6,64(%rsp) @@ -5388,26 +5388,26 @@ _CET_ENDBR addl %r12d,%edx leaq 128(%rsp),%r13 - - addl 0(%r8),%edx +// output is d-e-[a]-f-b-c => A=d,F=e,C=f,D=b,E=c + addl 0(%r8),%edx // update context addl 4(%r8),%esi addl 8(%r8),%ebp movl %edx,0(%r8) addl 12(%r8),%ebx movl %esi,4(%r8) - movl %edx,%eax + movl %edx,%eax // A=d addl 16(%r8),%ecx movl %ebp,%r12d movl %ebp,8(%r8) - movl %ebx,%edx - + movl %ebx,%edx // D=b +// xchg %ecx,%ebp # F=c, C=f movl %ebx,12(%r8) - movl %esi,%ebp + movl %esi,%ebp // F=e movl %ecx,16(%r8) - - movl %ecx,%esi - movl %r12d,%ecx - +// mov %ebp,16(%r8) + movl %ecx,%esi // E=c + movl %r12d,%ecx // C=f +// xchg %ebp,%esi # E=c, F=e cmpq %r10,%r9 jbe .Loop_avx2 @@ -5433,16 +5433,16 @@ _CET_ENDBR .section .rodata .align 64 K_XX_XX: -.long 0x5a827999,0x5a827999,0x5a827999,0x5a827999 -.long 0x5a827999,0x5a827999,0x5a827999,0x5a827999 -.long 0x6ed9eba1,0x6ed9eba1,0x6ed9eba1,0x6ed9eba1 -.long 0x6ed9eba1,0x6ed9eba1,0x6ed9eba1,0x6ed9eba1 -.long 0x8f1bbcdc,0x8f1bbcdc,0x8f1bbcdc,0x8f1bbcdc -.long 0x8f1bbcdc,0x8f1bbcdc,0x8f1bbcdc,0x8f1bbcdc -.long 0xca62c1d6,0xca62c1d6,0xca62c1d6,0xca62c1d6 -.long 0xca62c1d6,0xca62c1d6,0xca62c1d6,0xca62c1d6 -.long 0x00010203,0x04050607,0x08090a0b,0x0c0d0e0f -.long 0x00010203,0x04050607,0x08090a0b,0x0c0d0e0f +.long 0x5a827999,0x5a827999,0x5a827999,0x5a827999 // K_00_19 +.long 0x5a827999,0x5a827999,0x5a827999,0x5a827999 // K_00_19 +.long 0x6ed9eba1,0x6ed9eba1,0x6ed9eba1,0x6ed9eba1 // K_20_39 +.long 0x6ed9eba1,0x6ed9eba1,0x6ed9eba1,0x6ed9eba1 // K_20_39 +.long 0x8f1bbcdc,0x8f1bbcdc,0x8f1bbcdc,0x8f1bbcdc // K_40_59 +.long 0x8f1bbcdc,0x8f1bbcdc,0x8f1bbcdc,0x8f1bbcdc // K_40_59 +.long 0xca62c1d6,0xca62c1d6,0xca62c1d6,0xca62c1d6 // K_60_79 +.long 0xca62c1d6,0xca62c1d6,0xca62c1d6,0xca62c1d6 // K_60_79 +.long 0x00010203,0x04050607,0x08090a0b,0x0c0d0e0f // pbswap mask +.long 0x00010203,0x04050607,0x08090a0b,0x0c0d0e0f // pbswap mask .byte 0xf,0xe,0xd,0xc,0xb,0xa,0x9,0x8,0x7,0x6,0x5,0x4,0x3,0x2,0x1,0x0 .byte 83,72,65,49,32,98,108,111,99,107,32,116,114,97,110,115,102,111,114,109,32,102,111,114,32,120,56,54,95,54,52,44,32,67,82,89,80,84,79,71,65,77,83,32,98,121,32,60,97,112,112,114,111,64,111,112,101,110,115,115,108,46,111,114,103,62,0 .align 64 diff --git a/third_party/boringssl/gen/bcm/sha1-x86_64-win.asm b/third_party/boringssl/gen/bcm/sha1-x86_64-win.asm index 128fc3763..b3a9225ee 100644 --- a/third_party/boringssl/gen/bcm/sha1-x86_64-win.asm +++ b/third_party/boringssl/gen/bcm/sha1-x86_64-win.asm @@ -41,11 +41,11 @@ _CET_ENDBR push r14 - mov r8,rdi + mov r8,rdi ; reassigned argument sub rsp,72 - mov r9,rsi + mov r9,rsi ; reassigned argument and rsp,-64 - mov r10,rdx + mov r10,rdx ; reassigned argument mov QWORD[64+rsp],rax $L$prologue: @@ -1284,32 +1284,32 @@ _CET_ENDBR $L$prologue_shaext: movdqu xmm0,XMMWORD[rdi] movd xmm1,DWORD[16+rdi] - movdqa xmm3,XMMWORD[((K_XX_XX+160))] + movdqa xmm3,XMMWORD[((K_XX_XX+160))] ; byte-n-word swap movdqu xmm4,XMMWORD[rsi] - pshufd xmm0,xmm0,27 + pshufd xmm0,xmm0,27 ; flip word order movdqu xmm5,XMMWORD[16+rsi] - pshufd xmm1,xmm1,27 + pshufd xmm1,xmm1,27 ; flip word order movdqu xmm6,XMMWORD[32+rsi] pshufb xmm4,xmm3 movdqu xmm7,XMMWORD[48+rsi] pshufb xmm5,xmm3 pshufb xmm6,xmm3 - movdqa xmm9,xmm1 + movdqa xmm9,xmm1 ; offload %xmm1 pshufb xmm7,xmm3 jmp NEAR $L$oop_shaext ALIGN 16 $L$oop_shaext: dec rdx - lea r8,[64+rsi] + lea r8,[64+rsi] ; next input block paddd xmm1,xmm4 cmovne rsi,r8 prefetcht0 [512+rsi] - movdqa xmm8,xmm0 + movdqa xmm8,xmm0 ; offload %xmm0 sha1msg1 xmm4,xmm5 movdqa xmm2,xmm0 - sha1rnds4 xmm0,xmm1,0 + sha1rnds4 xmm0,xmm1,0 ; 0-3... sha1nexte xmm2,xmm5 pxor xmm4,xmm6 sha1msg1 xmm5,xmm6 @@ -1322,7 +1322,7 @@ $L$oop_shaext: sha1msg2 xmm5,xmm4 sha1msg1 xmm6,xmm7 movdqa xmm2,xmm0 - sha1rnds4 xmm0,xmm1,0 + sha1rnds4 xmm0,xmm1,0 ; 0-3... sha1nexte xmm2,xmm7 pxor xmm6,xmm4 sha1msg1 xmm7,xmm4 @@ -1335,7 +1335,7 @@ $L$oop_shaext: sha1msg2 xmm7,xmm6 sha1msg1 xmm4,xmm5 movdqa xmm2,xmm0 - sha1rnds4 xmm0,xmm1,0 + sha1rnds4 xmm0,xmm1,0 ; 0-3... sha1nexte xmm2,xmm5 pxor xmm4,xmm6 sha1msg1 xmm5,xmm6 @@ -1348,7 +1348,7 @@ $L$oop_shaext: sha1msg2 xmm5,xmm4 sha1msg1 xmm6,xmm7 movdqa xmm2,xmm0 - sha1rnds4 xmm0,xmm1,1 + sha1rnds4 xmm0,xmm1,1 ; 0-3... sha1nexte xmm2,xmm7 pxor xmm6,xmm4 sha1msg1 xmm7,xmm4 @@ -1361,7 +1361,7 @@ $L$oop_shaext: sha1msg2 xmm7,xmm6 sha1msg1 xmm4,xmm5 movdqa xmm2,xmm0 - sha1rnds4 xmm0,xmm1,1 + sha1rnds4 xmm0,xmm1,1 ; 0-3... sha1nexte xmm2,xmm5 pxor xmm4,xmm6 sha1msg1 xmm5,xmm6 @@ -1374,7 +1374,7 @@ $L$oop_shaext: sha1msg2 xmm5,xmm4 sha1msg1 xmm6,xmm7 movdqa xmm2,xmm0 - sha1rnds4 xmm0,xmm1,2 + sha1rnds4 xmm0,xmm1,2 ; 0-3... sha1nexte xmm2,xmm7 pxor xmm6,xmm4 sha1msg1 xmm7,xmm4 @@ -1387,7 +1387,7 @@ $L$oop_shaext: sha1msg2 xmm7,xmm6 sha1msg1 xmm4,xmm5 movdqa xmm2,xmm0 - sha1rnds4 xmm0,xmm1,2 + sha1rnds4 xmm0,xmm1,2 ; 0-3... sha1nexte xmm2,xmm5 pxor xmm4,xmm6 sha1msg1 xmm5,xmm6 @@ -1400,7 +1400,7 @@ $L$oop_shaext: sha1msg2 xmm5,xmm4 sha1msg1 xmm6,xmm7 movdqa xmm2,xmm0 - sha1rnds4 xmm0,xmm1,2 + sha1rnds4 xmm0,xmm1,2 ; 0-3... sha1nexte xmm2,xmm7 pxor xmm6,xmm4 sha1msg1 xmm7,xmm4 @@ -1413,30 +1413,30 @@ $L$oop_shaext: sha1msg2 xmm7,xmm6 movdqu xmm4,XMMWORD[rsi] movdqa xmm2,xmm0 - sha1rnds4 xmm0,xmm1,3 + sha1rnds4 xmm0,xmm1,3 ; 64-67 sha1nexte xmm2,xmm5 movdqu xmm5,XMMWORD[16+rsi] pshufb xmm4,xmm3 movdqa xmm1,xmm0 - sha1rnds4 xmm0,xmm2,3 + sha1rnds4 xmm0,xmm2,3 ; 68-71 sha1nexte xmm1,xmm6 movdqu xmm6,XMMWORD[32+rsi] pshufb xmm5,xmm3 movdqa xmm2,xmm0 - sha1rnds4 xmm0,xmm1,3 + sha1rnds4 xmm0,xmm1,3 ; 72-75 sha1nexte xmm2,xmm7 movdqu xmm7,XMMWORD[48+rsi] pshufb xmm6,xmm3 movdqa xmm1,xmm0 - sha1rnds4 xmm0,xmm2,3 + sha1rnds4 xmm0,xmm2,3 ; 76-79 sha1nexte xmm1,xmm9 pshufb xmm7,xmm3 paddd xmm0,xmm8 - movdqa xmm9,xmm1 + movdqa xmm9,xmm1 ; offload %xmm1 jnz NEAR $L$oop_shaext @@ -1470,7 +1470,7 @@ $L$SEH_begin_sha1_block_data_order_ssse3: _CET_ENDBR - mov r11,rsp + mov r11,rsp ; frame pointer push rbx @@ -1478,7 +1478,7 @@ _CET_ENDBR push r12 - push r13 + push r13 ; redundant, done to share Win64 SE handler push r14 @@ -1491,40 +1491,40 @@ _CET_ENDBR movaps XMMWORD[(-40-16)+r11],xmm11 $L$prologue_ssse3: and rsp,-64 - mov r8,rdi - mov r9,rsi - mov r10,rdx + mov r8,rdi ; reassigned argument + mov r9,rsi ; reassigned argument + mov r10,rdx ; reassigned argument shl r10,6 add r10,r9 lea r14,[((K_XX_XX+64))] - mov eax,DWORD[r8] + mov eax,DWORD[r8] ; load context mov ebx,DWORD[4+r8] mov ecx,DWORD[8+r8] mov edx,DWORD[12+r8] - mov esi,ebx + mov esi,ebx ; magic seed mov ebp,DWORD[16+r8] mov edi,ecx xor edi,edx and esi,edi - movdqa xmm6,XMMWORD[64+r14] - movdqa xmm9,XMMWORD[((-64))+r14] - movdqu xmm0,XMMWORD[r9] + movdqa xmm6,XMMWORD[64+r14] ; pbswap mask + movdqa xmm9,XMMWORD[((-64))+r14] ; K_00_19 + movdqu xmm0,XMMWORD[r9] ; load input to %xmm[0-3] movdqu xmm1,XMMWORD[16+r9] movdqu xmm2,XMMWORD[32+r9] movdqu xmm3,XMMWORD[48+r9] - pshufb xmm0,xmm6 + pshufb xmm0,xmm6 ; byte swap pshufb xmm1,xmm6 pshufb xmm2,xmm6 add r9,64 - paddd xmm0,xmm9 + paddd xmm0,xmm9 ; add K_00_19 pshufb xmm3,xmm6 paddd xmm1,xmm9 paddd xmm2,xmm9 - movdqa XMMWORD[rsp],xmm0 - psubd xmm0,xmm9 + movdqa XMMWORD[rsp],xmm0 ; X[]+K xfer to IALU + psubd xmm0,xmm9 ; restore X[] movdqa XMMWORD[16+rsp],xmm1 psubd xmm1,xmm9 movdqa XMMWORD[32+rsp],xmm2 @@ -2521,14 +2521,14 @@ $L$oop_ssse3: add eax,edi ror ecx,7 add eax,ebx - add eax,DWORD[r8] + add eax,DWORD[r8] ; update context add esi,DWORD[4+r8] add ecx,DWORD[8+r8] add edx,DWORD[12+r8] mov DWORD[r8],eax add ebp,DWORD[16+r8] mov DWORD[4+r8],esi - mov ebx,esi + mov ebx,esi ; magic seed mov DWORD[8+r8],ecx mov edi,ecx mov DWORD[12+r8],edx @@ -2634,7 +2634,7 @@ $L$done_ssse3: add eax,edi ror ecx,7 add eax,ebx - add eax,DWORD[r8] + add eax,DWORD[r8] ; update context add esi,DWORD[4+r8] add ecx,DWORD[8+r8] mov DWORD[r8],eax @@ -2691,7 +2691,7 @@ _CET_ENDBR push r12 - push r13 + push r13 ; redundant, done to share Win64 SE handler push r14 @@ -2705,39 +2705,39 @@ _CET_ENDBR vmovaps XMMWORD[(-40-16)+r11],xmm11 $L$prologue_avx: and rsp,-64 - mov r8,rdi - mov r9,rsi - mov r10,rdx + mov r8,rdi ; reassigned argument + mov r9,rsi ; reassigned argument + mov r10,rdx ; reassigned argument shl r10,6 add r10,r9 lea r14,[((K_XX_XX+64))] - mov eax,DWORD[r8] + mov eax,DWORD[r8] ; load context mov ebx,DWORD[4+r8] mov ecx,DWORD[8+r8] mov edx,DWORD[12+r8] - mov esi,ebx + mov esi,ebx ; magic seed mov ebp,DWORD[16+r8] mov edi,ecx xor edi,edx and esi,edi - vmovdqa xmm6,XMMWORD[64+r14] - vmovdqa xmm11,XMMWORD[((-64))+r14] - vmovdqu xmm0,XMMWORD[r9] + vmovdqa xmm6,XMMWORD[64+r14] ; pbswap mask + vmovdqa xmm11,XMMWORD[((-64))+r14] ; K_00_19 + vmovdqu xmm0,XMMWORD[r9] ; load input to %xmm[0-3] vmovdqu xmm1,XMMWORD[16+r9] vmovdqu xmm2,XMMWORD[32+r9] vmovdqu xmm3,XMMWORD[48+r9] - vpshufb xmm0,xmm0,xmm6 + vpshufb xmm0,xmm0,xmm6 ; byte swap add r9,64 vpshufb xmm1,xmm1,xmm6 vpshufb xmm2,xmm2,xmm6 vpshufb xmm3,xmm3,xmm6 - vpaddd xmm4,xmm0,xmm11 + vpaddd xmm4,xmm0,xmm11 ; add K_00_19 vpaddd xmm5,xmm1,xmm11 vpaddd xmm6,xmm2,xmm11 - vmovdqa XMMWORD[rsp],xmm4 + vmovdqa XMMWORD[rsp],xmm4 ; X[]+K xfer to IALU vmovdqa XMMWORD[16+rsp],xmm5 vmovdqa XMMWORD[32+rsp],xmm6 jmp NEAR $L$oop_avx @@ -3672,14 +3672,14 @@ $L$oop_avx: add eax,edi shrd ecx,ecx,7 add eax,ebx - add eax,DWORD[r8] + add eax,DWORD[r8] ; update context add esi,DWORD[4+r8] add ecx,DWORD[8+r8] add edx,DWORD[12+r8] mov DWORD[r8],eax add ebp,DWORD[16+r8] mov DWORD[4+r8],esi - mov ebx,esi + mov ebx,esi ; magic seed mov DWORD[8+r8],ecx mov edi,ecx mov DWORD[12+r8],edx @@ -3787,7 +3787,7 @@ $L$done_avx: add eax,ebx vzeroupper - add eax,DWORD[r8] + add eax,DWORD[r8] ; update context add esi,DWORD[4+r8] add ecx,DWORD[8+r8] mov DWORD[r8],eax @@ -3857,9 +3857,9 @@ _CET_ENDBR vmovaps XMMWORD[(-40-32)+r11],xmm10 vmovaps XMMWORD[(-40-16)+r11],xmm11 $L$prologue_avx2: - mov r8,rdi - mov r9,rsi - mov r10,rdx + mov r8,rdi ; reassigned argument + mov r9,rsi ; reassigned argument + mov r10,rdx ; reassigned argument lea rsp,[((-640))+rsp] shl r10,6 @@ -3868,14 +3868,14 @@ $L$prologue_avx2: add r10,r9 lea r14,[((K_XX_XX+64))] - mov eax,DWORD[r8] + mov eax,DWORD[r8] ; load context cmp r13,r10 - cmovae r13,r9 + cmovae r13,r9 ; next or same block mov ebp,DWORD[4+r8] mov ecx,DWORD[8+r8] mov edx,DWORD[12+r8] mov esi,DWORD[16+r8] - vmovdqu ymm6,YMMWORD[64+r14] + vmovdqu ymm6,YMMWORD[64+r14] ; pbswap mask vmovdqu xmm0,XMMWORD[r9] vmovdqu xmm1,XMMWORD[16+r9] @@ -3889,12 +3889,12 @@ $L$prologue_avx2: vpshufb ymm1,ymm1,ymm6 vinserti128 ymm3,ymm3,XMMWORD[48+r13],1 vpshufb ymm2,ymm2,ymm6 - vmovdqu ymm11,YMMWORD[((-64))+r14] + vmovdqu ymm11,YMMWORD[((-64))+r14] ; K_00_19 vpshufb ymm3,ymm3,ymm6 - vpaddd ymm4,ymm0,ymm11 + vpaddd ymm4,ymm0,ymm11 ; add K_00_19 vpaddd ymm5,ymm1,ymm11 - vmovdqu YMMWORD[rsp],ymm4 + vmovdqu YMMWORD[rsp],ymm4 ; X[]+K xfer to IALU vpaddd ymm6,ymm2,ymm11 vmovdqu YMMWORD[32+rsp],ymm5 vpaddd ymm7,ymm3,ymm11 @@ -3969,7 +3969,7 @@ $L$oop_avx2: andn edi,ebp,edx and ebp,ecx xor ebp,edi - jmp NEAR $L$align32_1 + jmp NEAR $L$align32_1 ; see "Decoded ICache" in manual ALIGN 32 $L$align32_1: vpalignr ymm8,ymm7,ymm6,8 @@ -4348,7 +4348,7 @@ $L$align32_1: xor ecx,esi add ebx,r12d and ecx,edi - jmp NEAR $L$align32_2 + jmp NEAR $L$align32_2 ; see "Decoded ICache" in manual ALIGN 32 $L$align32_2: vpalignr ymm8,ymm7,ymm6,8 @@ -4724,38 +4724,38 @@ $L$align32_2: rorx r12d,esi,27 add edx,r12d lea r13,[128+r9] - lea rdi,[128+r9] + lea rdi,[128+r9] ; borrow %edi cmp r13,r10 - cmovae r13,r9 - + cmovae r13,r9 ; next or previous block - add edx,DWORD[r8] +; output is d-e-[a]-f-b-c => A=d,F=e,C=f,D=b,E=c + add edx,DWORD[r8] ; update context add esi,DWORD[4+r8] add ebp,DWORD[8+r8] mov DWORD[r8],edx add ebx,DWORD[12+r8] mov DWORD[4+r8],esi - mov eax,edx + mov eax,edx ; A=d add ecx,DWORD[16+r8] mov r12d,ebp mov DWORD[8+r8],ebp - mov edx,ebx - + mov edx,ebx ; D=b +; xchg %ecx,%ebp # F=c, C=f mov DWORD[12+r8],ebx - mov ebp,esi + mov ebp,esi ; F=e mov DWORD[16+r8],ecx - - mov esi,ecx - mov ecx,r12d - +; mov %ebp,16(%r8) + mov esi,ecx ; E=c + mov ecx,r12d ; C=f +; xchg %ebp,%esi # E=c, F=e cmp r9,r10 je NEAR $L$done_avx2 - vmovdqu ymm6,YMMWORD[64+r14] - cmp rdi,r10 + vmovdqu ymm6,YMMWORD[64+r14] ; pbswap mask + cmp rdi,r10 ; borrowed %edi ja NEAR $L$ast_avx2 - vmovdqu xmm0,XMMWORD[((-64))+rdi] + vmovdqu xmm0,XMMWORD[((-64))+rdi] ; low part of %ymm0 vmovdqu xmm1,XMMWORD[((-48))+rdi] vmovdqu xmm2,XMMWORD[((-32))+rdi] vmovdqu xmm3,XMMWORD[((-16))+rdi] @@ -5135,7 +5135,7 @@ $L$ast_avx2: xor edx,eax add ecx,r12d and edx,edi - jmp NEAR $L$align32_3 + jmp NEAR $L$align32_3 ; see "Decoded ICache" in manual ALIGN 32 $L$align32_3: vmovdqu YMMWORD[64+rsp],ymm6 @@ -5490,26 +5490,26 @@ $L$align32_3: add edx,r12d lea r13,[128+rsp] - - add edx,DWORD[r8] +; output is d-e-[a]-f-b-c => A=d,F=e,C=f,D=b,E=c + add edx,DWORD[r8] ; update context add esi,DWORD[4+r8] add ebp,DWORD[8+r8] mov DWORD[r8],edx add ebx,DWORD[12+r8] mov DWORD[4+r8],esi - mov eax,edx + mov eax,edx ; A=d add ecx,DWORD[16+r8] mov r12d,ebp mov DWORD[8+r8],ebp - mov edx,ebx - + mov edx,ebx ; D=b +; xchg %ecx,%ebp # F=c, C=f mov DWORD[12+r8],ebx - mov ebp,esi + mov ebp,esi ; F=e mov DWORD[16+r8],ecx - - mov esi,ecx - mov ecx,r12d - +; mov %ebp,16(%r8) + mov esi,ecx ; E=c + mov ecx,r12d ; C=f +; xchg %ebp,%esi # E=c, F=e cmp r9,r10 jbe NEAR $L$oop_avx2 @@ -5543,16 +5543,16 @@ $L$SEH_end_sha1_block_data_order_avx2: section .rdata rdata align=8 ALIGN 64 K_XX_XX: - DD 0x5a827999,0x5a827999,0x5a827999,0x5a827999 - DD 0x5a827999,0x5a827999,0x5a827999,0x5a827999 - DD 0x6ed9eba1,0x6ed9eba1,0x6ed9eba1,0x6ed9eba1 - DD 0x6ed9eba1,0x6ed9eba1,0x6ed9eba1,0x6ed9eba1 - DD 0x8f1bbcdc,0x8f1bbcdc,0x8f1bbcdc,0x8f1bbcdc - DD 0x8f1bbcdc,0x8f1bbcdc,0x8f1bbcdc,0x8f1bbcdc - DD 0xca62c1d6,0xca62c1d6,0xca62c1d6,0xca62c1d6 - DD 0xca62c1d6,0xca62c1d6,0xca62c1d6,0xca62c1d6 - DD 0x00010203,0x04050607,0x08090a0b,0x0c0d0e0f - DD 0x00010203,0x04050607,0x08090a0b,0x0c0d0e0f + DD 0x5a827999,0x5a827999,0x5a827999,0x5a827999 ; K_00_19 + DD 0x5a827999,0x5a827999,0x5a827999,0x5a827999 ; K_00_19 + DD 0x6ed9eba1,0x6ed9eba1,0x6ed9eba1,0x6ed9eba1 ; K_20_39 + DD 0x6ed9eba1,0x6ed9eba1,0x6ed9eba1,0x6ed9eba1 ; K_20_39 + DD 0x8f1bbcdc,0x8f1bbcdc,0x8f1bbcdc,0x8f1bbcdc ; K_40_59 + DD 0x8f1bbcdc,0x8f1bbcdc,0x8f1bbcdc,0x8f1bbcdc ; K_40_59 + DD 0xca62c1d6,0xca62c1d6,0xca62c1d6,0xca62c1d6 ; K_60_79 + DD 0xca62c1d6,0xca62c1d6,0xca62c1d6,0xca62c1d6 ; K_60_79 + DD 0x00010203,0x04050607,0x08090a0b,0x0c0d0e0f ; pbswap mask + DD 0x00010203,0x04050607,0x08090a0b,0x0c0d0e0f ; pbswap mask DB 0xf,0xe,0xd,0xc,0xb,0xa,0x9,0x8,0x7,0x6,0x5,0x4,0x3,0x2,0x1,0x0 DB 83,72,65,49,32,98,108,111,99,107,32,116,114,97,110,115 DB 102,111,114,109,32,102,111,114,32,120,56,54,95,54,52,44 @@ -5560,7 +5560,7 @@ K_XX_XX: DB 97,112,112,114,111,64,111,112,101,110,115,115,108,46,111,114 DB 103,62,0 ALIGN 64 -section .text +section .text code align=64 EXTERN __imp_RtlVirtualUnwind @@ -5577,31 +5577,31 @@ se_handler: pushfq sub rsp,64 - mov rax,QWORD[120+r8] - mov rbx,QWORD[248+r8] + mov rax,QWORD[120+r8] ; pull context->Rax + mov rbx,QWORD[248+r8] ; pull context->Rip lea r10,[$L$prologue] - cmp rbx,r10 + cmp rbx,r10 ; context->Rip<.Lprologue jb NEAR $L$common_seh_tail - mov rax,QWORD[152+r8] + mov rax,QWORD[152+r8] ; pull context->Rsp lea r10,[$L$epilogue] - cmp rbx,r10 + cmp rbx,r10 ; context->Rip>=.Lepilogue jae NEAR $L$common_seh_tail - mov rax,QWORD[64+rax] + mov rax,QWORD[64+rax] ; pull saved stack pointer mov rbx,QWORD[((-8))+rax] mov rbp,QWORD[((-16))+rax] mov r12,QWORD[((-24))+rax] mov r13,QWORD[((-32))+rax] mov r14,QWORD[((-40))+rax] - mov QWORD[144+r8],rbx - mov QWORD[160+r8],rbp - mov QWORD[216+r8],r12 - mov QWORD[224+r8],r13 - mov QWORD[232+r8],r14 + mov QWORD[144+r8],rbx ; restore context->Rbx + mov QWORD[160+r8],rbp ; restore context->Rbp + mov QWORD[216+r8],r12 ; restore context->R12 + mov QWORD[224+r8],r13 ; restore context->R13 + mov QWORD[232+r8],r14 ; restore context->R14 jmp NEAR $L$common_seh_tail @@ -5619,21 +5619,21 @@ shaext_handler: pushfq sub rsp,64 - mov rax,QWORD[120+r8] - mov rbx,QWORD[248+r8] + mov rax,QWORD[120+r8] ; pull context->Rax + mov rbx,QWORD[248+r8] ; pull context->Rip lea r10,[$L$prologue_shaext] - cmp rbx,r10 + cmp rbx,r10 ; context->Rip<.Lprologue jb NEAR $L$common_seh_tail lea r10,[$L$epilogue_shaext] - cmp rbx,r10 + cmp rbx,r10 ; context->Rip>=.Lepilogue jae NEAR $L$common_seh_tail lea rsi,[((-8-64))+rax] - lea rdi,[512+r8] + lea rdi,[512+r8] ; &context.Xmm6 mov ecx,8 - DD 0xa548f3fc + DD 0xa548f3fc ; cld; rep movsq jmp NEAR $L$common_seh_tail @@ -5651,67 +5651,67 @@ ssse3_handler: pushfq sub rsp,64 - mov rax,QWORD[120+r8] - mov rbx,QWORD[248+r8] + mov rax,QWORD[120+r8] ; pull context->Rax + mov rbx,QWORD[248+r8] ; pull context->Rip - mov rsi,QWORD[8+r9] - mov r11,QWORD[56+r9] + mov rsi,QWORD[8+r9] ; disp->ImageBase + mov r11,QWORD[56+r9] ; disp->HandlerData - mov r10d,DWORD[r11] - lea r10,[r10*1+rsi] - cmp rbx,r10 + mov r10d,DWORD[r11] ; HandlerData[0] + lea r10,[r10*1+rsi] ; prologue label + cmp rbx,r10 ; context->RipR11 - mov r10d,DWORD[4+r11] - lea r10,[r10*1+rsi] - cmp rbx,r10 + mov r10d,DWORD[4+r11] ; HandlerData[1] + lea r10,[r10*1+rsi] ; epilogue label + cmp rbx,r10 ; context->Rip>=epilogue label jae NEAR $L$common_seh_tail lea rsi,[((-40-96))+rax] - lea rdi,[512+r8] + lea rdi,[512+r8] ; &context.Xmm6 mov ecx,12 - DD 0xa548f3fc + DD 0xa548f3fc ; cld; rep movsq mov rbx,QWORD[((-8))+rax] mov rbp,QWORD[((-16))+rax] mov r12,QWORD[((-24))+rax] mov r13,QWORD[((-32))+rax] mov r14,QWORD[((-40))+rax] - mov QWORD[144+r8],rbx - mov QWORD[160+r8],rbp - mov QWORD[216+r8],r12 - mov QWORD[224+r8],r13 - mov QWORD[232+r8],r14 + mov QWORD[144+r8],rbx ; restore context->Rbx + mov QWORD[160+r8],rbp ; restore context->Rbp + mov QWORD[216+r8],r12 ; restore context->R12 + mov QWORD[224+r8],r13 ; restore context->R13 + mov QWORD[232+r8],r14 ; restore context->R14 $L$common_seh_tail: mov rdi,QWORD[8+rax] mov rsi,QWORD[16+rax] - mov QWORD[152+r8],rax - mov QWORD[168+r8],rsi - mov QWORD[176+r8],rdi + mov QWORD[152+r8],rax ; restore context->Rsp + mov QWORD[168+r8],rsi ; restore context->Rsi + mov QWORD[176+r8],rdi ; restore context->Rdi - mov rdi,QWORD[40+r9] - mov rsi,r8 - mov ecx,154 - DD 0xa548f3fc + mov rdi,QWORD[40+r9] ; disp->ContextRecord + mov rsi,r8 ; context + mov ecx,154 ; sizeof(CONTEXT) + DD 0xa548f3fc ; cld; rep movsq mov rsi,r9 - xor rcx,rcx - mov rdx,QWORD[8+rsi] - mov r8,QWORD[rsi] - mov r9,QWORD[16+rsi] - mov r10,QWORD[40+rsi] - lea r11,[56+rsi] - lea r12,[24+rsi] - mov QWORD[32+rsp],r10 - mov QWORD[40+rsp],r11 - mov QWORD[48+rsp],r12 - mov QWORD[56+rsp],rcx + xor rcx,rcx ; arg1, UNW_FLAG_NHANDLER + mov rdx,QWORD[8+rsi] ; arg2, disp->ImageBase + mov r8,QWORD[rsi] ; arg3, disp->ControlPc + mov r9,QWORD[16+rsi] ; arg4, disp->FunctionEntry + mov r10,QWORD[40+rsi] ; disp->ContextRecord + lea r11,[56+rsi] ; &disp->HandlerData + lea r12,[24+rsi] ; &disp->EstablisherFrame + mov QWORD[32+rsp],r10 ; arg5 + mov QWORD[40+rsp],r11 ; arg6 + mov QWORD[48+rsp],r12 ; arg7 + mov QWORD[56+rsp],rcx ; arg8, (NULL) call QWORD[__imp_RtlVirtualUnwind] - mov eax,1 + mov eax,1 ; ExceptionContinueSearch add rsp,64 popfq pop r15 @@ -5753,15 +5753,15 @@ $L$SEH_info_sha1_block_data_order_hw: $L$SEH_info_sha1_block_data_order_ssse3: DB 9,0,0,0 DD ssse3_handler wrt ..imagebase - DD $L$prologue_ssse3 wrt ..imagebase,$L$epilogue_ssse3 wrt ..imagebase + DD $L$prologue_ssse3 wrt ..imagebase,$L$epilogue_ssse3 wrt ..imagebase ; HandlerData[] $L$SEH_info_sha1_block_data_order_avx: DB 9,0,0,0 DD ssse3_handler wrt ..imagebase - DD $L$prologue_avx wrt ..imagebase,$L$epilogue_avx wrt ..imagebase + DD $L$prologue_avx wrt ..imagebase,$L$epilogue_avx wrt ..imagebase ; HandlerData[] $L$SEH_info_sha1_block_data_order_avx2: DB 9,0,0,0 DD ssse3_handler wrt ..imagebase - DD $L$prologue_avx2 wrt ..imagebase,$L$epilogue_avx2 wrt ..imagebase + DD $L$prologue_avx2 wrt ..imagebase,$L$epilogue_avx2 wrt ..imagebase ; HandlerData[] %else ; Work around https://bugzilla.nasm.us/show_bug.cgi?id=3392738 ret diff --git a/third_party/boringssl/gen/bcm/sha256-armv8-apple.S b/third_party/boringssl/gen/bcm/sha256-armv8-apple.S index 0cb6f72be..eb4af0a83 100644 --- a/third_party/boringssl/gen/bcm/sha256-armv8-apple.S +++ b/third_party/boringssl/gen/bcm/sha256-armv8-apple.S @@ -1020,7 +1020,9 @@ Loop_16_xx: .section __TEXT,__const + .align 6 +sha256_constants: LK256: .long 0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5 @@ -1050,6 +1052,7 @@ LK256: .private_extern _sha256_block_data_order_hw .align 6 +.alt_entry _sha256_block_data_order_hw _sha256_block_data_order_hw: // Armv8.3-A PAuth: even though x30 is pushed to stack it is not popped later. AARCH64_VALID_CALL_TARGET diff --git a/third_party/boringssl/gen/bcm/sha256-armv8-linux.S b/third_party/boringssl/gen/bcm/sha256-armv8-linux.S index d2901be09..072263a64 100644 --- a/third_party/boringssl/gen/bcm/sha256-armv8-linux.S +++ b/third_party/boringssl/gen/bcm/sha256-armv8-linux.S @@ -1020,7 +1020,9 @@ sha256_block_data_order_nohw: .size sha256_block_data_order_nohw,.-sha256_block_data_order_nohw .section .rodata + .align 6 +sha256_constants: .type .LK256,%object .LK256: .long 0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5 diff --git a/third_party/boringssl/gen/bcm/sha256-armv8-win.S b/third_party/boringssl/gen/bcm/sha256-armv8-win.S index c603cf8b9..ca668894f 100644 --- a/third_party/boringssl/gen/bcm/sha256-armv8-win.S +++ b/third_party/boringssl/gen/bcm/sha256-armv8-win.S @@ -1022,7 +1022,9 @@ Loop_16_xx: .section .rodata + .align 6 +sha256_constants: LK256: .long 0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5 diff --git a/third_party/boringssl/gen/bcm/sha256-x86_64-apple.S b/third_party/boringssl/gen/bcm/sha256-x86_64-apple.S index 367f0d334..9400bc5fd 100644 --- a/third_party/boringssl/gen/bcm/sha256-x86_64-apple.S +++ b/third_party/boringssl/gen/bcm/sha256-x86_64-apple.S @@ -13,7 +13,7 @@ _sha256_block_data_order_nohw: _CET_ENDBR - movq %rsp,%rax + movq %rsp,%rax // copy %rsp pushq %rbx @@ -27,14 +27,14 @@ _CET_ENDBR pushq %r15 - shlq $4,%rdx + shlq $4,%rdx // num*16 subq $64+32,%rsp - leaq (%rsi,%rdx,4),%rdx - andq $-64,%rsp - movq %rdi,64+0(%rsp) - movq %rsi,64+8(%rsp) - movq %rdx,64+16(%rsp) - movq %rax,88(%rsp) + leaq (%rsi,%rdx,4),%rdx // inp+num*16*4 + andq $-64,%rsp // align stack frame + movq %rdi,64+0(%rsp) // save ctx, 1st arg + movq %rsi,64+8(%rsp) // save inp, 2nd arh + movq %rdx,64+16(%rsp) // save end pointer, "3rd" arg + movq %rax,88(%rsp) // save copy of %rsp L$prologue: @@ -52,7 +52,7 @@ L$prologue: L$loop: movl %ebx,%edi leaq K256(%rip),%rbp - xorl %ecx,%edi + xorl %ecx,%edi // magic movl 0(%rsi),%r12d movl %r8d,%r13d movl %eax,%r14d @@ -62,38 +62,38 @@ L$loop: xorl %r8d,%r13d rorl $9,%r14d - xorl %r10d,%r15d + xorl %r10d,%r15d // f^g movl %r12d,0(%rsp) xorl %eax,%r14d - andl %r8d,%r15d + andl %r8d,%r15d // (f^g)&e rorl $5,%r13d - addl %r11d,%r12d - xorl %r10d,%r15d + addl %r11d,%r12d // T1+=h + xorl %r10d,%r15d // Ch(e,f,g)=((f^g)&e)^g rorl $11,%r14d xorl %r8d,%r13d - addl %r15d,%r12d + addl %r15d,%r12d // T1+=Ch(e,f,g) movl %eax,%r15d - addl (%rbp),%r12d + addl (%rbp),%r12d // T1+=K[round] xorl %eax,%r14d - xorl %ebx,%r15d - rorl $6,%r13d + xorl %ebx,%r15d // a^b, b^c in next round + rorl $6,%r13d // Sigma1(e) movl %ebx,%r11d andl %r15d,%edi - rorl $2,%r14d - addl %r13d,%r12d + rorl $2,%r14d // Sigma0(a) + addl %r13d,%r12d // T1+=Sigma1(e) - xorl %edi,%r11d - addl %r12d,%edx - addl %r12d,%r11d + xorl %edi,%r11d // h=Maj(a,b,c)=Ch(a^b,c,b) + addl %r12d,%edx // d+=T1 + addl %r12d,%r11d // h+=T1 - leaq 4(%rbp),%rbp - addl %r14d,%r11d + leaq 4(%rbp),%rbp // round++ + addl %r14d,%r11d // h+=Sigma0(a) movl 4(%rsi),%r12d movl %edx,%r13d movl %r11d,%r14d @@ -103,38 +103,38 @@ L$loop: xorl %edx,%r13d rorl $9,%r14d - xorl %r9d,%edi + xorl %r9d,%edi // f^g movl %r12d,4(%rsp) xorl %r11d,%r14d - andl %edx,%edi + andl %edx,%edi // (f^g)&e rorl $5,%r13d - addl %r10d,%r12d - xorl %r9d,%edi + addl %r10d,%r12d // T1+=h + xorl %r9d,%edi // Ch(e,f,g)=((f^g)&e)^g rorl $11,%r14d xorl %edx,%r13d - addl %edi,%r12d + addl %edi,%r12d // T1+=Ch(e,f,g) movl %r11d,%edi - addl (%rbp),%r12d + addl (%rbp),%r12d // T1+=K[round] xorl %r11d,%r14d - xorl %eax,%edi - rorl $6,%r13d + xorl %eax,%edi // a^b, b^c in next round + rorl $6,%r13d // Sigma1(e) movl %eax,%r10d andl %edi,%r15d - rorl $2,%r14d - addl %r13d,%r12d + rorl $2,%r14d // Sigma0(a) + addl %r13d,%r12d // T1+=Sigma1(e) - xorl %r15d,%r10d - addl %r12d,%ecx - addl %r12d,%r10d + xorl %r15d,%r10d // h=Maj(a,b,c)=Ch(a^b,c,b) + addl %r12d,%ecx // d+=T1 + addl %r12d,%r10d // h+=T1 - leaq 4(%rbp),%rbp - addl %r14d,%r10d + leaq 4(%rbp),%rbp // round++ + addl %r14d,%r10d // h+=Sigma0(a) movl 8(%rsi),%r12d movl %ecx,%r13d movl %r10d,%r14d @@ -144,38 +144,38 @@ L$loop: xorl %ecx,%r13d rorl $9,%r14d - xorl %r8d,%r15d + xorl %r8d,%r15d // f^g movl %r12d,8(%rsp) xorl %r10d,%r14d - andl %ecx,%r15d + andl %ecx,%r15d // (f^g)&e rorl $5,%r13d - addl %r9d,%r12d - xorl %r8d,%r15d + addl %r9d,%r12d // T1+=h + xorl %r8d,%r15d // Ch(e,f,g)=((f^g)&e)^g rorl $11,%r14d xorl %ecx,%r13d - addl %r15d,%r12d + addl %r15d,%r12d // T1+=Ch(e,f,g) movl %r10d,%r15d - addl (%rbp),%r12d + addl (%rbp),%r12d // T1+=K[round] xorl %r10d,%r14d - xorl %r11d,%r15d - rorl $6,%r13d + xorl %r11d,%r15d // a^b, b^c in next round + rorl $6,%r13d // Sigma1(e) movl %r11d,%r9d andl %r15d,%edi - rorl $2,%r14d - addl %r13d,%r12d + rorl $2,%r14d // Sigma0(a) + addl %r13d,%r12d // T1+=Sigma1(e) - xorl %edi,%r9d - addl %r12d,%ebx - addl %r12d,%r9d + xorl %edi,%r9d // h=Maj(a,b,c)=Ch(a^b,c,b) + addl %r12d,%ebx // d+=T1 + addl %r12d,%r9d // h+=T1 - leaq 4(%rbp),%rbp - addl %r14d,%r9d + leaq 4(%rbp),%rbp // round++ + addl %r14d,%r9d // h+=Sigma0(a) movl 12(%rsi),%r12d movl %ebx,%r13d movl %r9d,%r14d @@ -185,38 +185,38 @@ L$loop: xorl %ebx,%r13d rorl $9,%r14d - xorl %edx,%edi + xorl %edx,%edi // f^g movl %r12d,12(%rsp) xorl %r9d,%r14d - andl %ebx,%edi + andl %ebx,%edi // (f^g)&e rorl $5,%r13d - addl %r8d,%r12d - xorl %edx,%edi + addl %r8d,%r12d // T1+=h + xorl %edx,%edi // Ch(e,f,g)=((f^g)&e)^g rorl $11,%r14d xorl %ebx,%r13d - addl %edi,%r12d + addl %edi,%r12d // T1+=Ch(e,f,g) movl %r9d,%edi - addl (%rbp),%r12d + addl (%rbp),%r12d // T1+=K[round] xorl %r9d,%r14d - xorl %r10d,%edi - rorl $6,%r13d + xorl %r10d,%edi // a^b, b^c in next round + rorl $6,%r13d // Sigma1(e) movl %r10d,%r8d andl %edi,%r15d - rorl $2,%r14d - addl %r13d,%r12d + rorl $2,%r14d // Sigma0(a) + addl %r13d,%r12d // T1+=Sigma1(e) - xorl %r15d,%r8d - addl %r12d,%eax - addl %r12d,%r8d + xorl %r15d,%r8d // h=Maj(a,b,c)=Ch(a^b,c,b) + addl %r12d,%eax // d+=T1 + addl %r12d,%r8d // h+=T1 - leaq 20(%rbp),%rbp - addl %r14d,%r8d + leaq 20(%rbp),%rbp // round++ + addl %r14d,%r8d // h+=Sigma0(a) movl 16(%rsi),%r12d movl %eax,%r13d movl %r8d,%r14d @@ -226,38 +226,38 @@ L$loop: xorl %eax,%r13d rorl $9,%r14d - xorl %ecx,%r15d + xorl %ecx,%r15d // f^g movl %r12d,16(%rsp) xorl %r8d,%r14d - andl %eax,%r15d + andl %eax,%r15d // (f^g)&e rorl $5,%r13d - addl %edx,%r12d - xorl %ecx,%r15d + addl %edx,%r12d // T1+=h + xorl %ecx,%r15d // Ch(e,f,g)=((f^g)&e)^g rorl $11,%r14d xorl %eax,%r13d - addl %r15d,%r12d + addl %r15d,%r12d // T1+=Ch(e,f,g) movl %r8d,%r15d - addl (%rbp),%r12d + addl (%rbp),%r12d // T1+=K[round] xorl %r8d,%r14d - xorl %r9d,%r15d - rorl $6,%r13d + xorl %r9d,%r15d // a^b, b^c in next round + rorl $6,%r13d // Sigma1(e) movl %r9d,%edx andl %r15d,%edi - rorl $2,%r14d - addl %r13d,%r12d + rorl $2,%r14d // Sigma0(a) + addl %r13d,%r12d // T1+=Sigma1(e) - xorl %edi,%edx - addl %r12d,%r11d - addl %r12d,%edx + xorl %edi,%edx // h=Maj(a,b,c)=Ch(a^b,c,b) + addl %r12d,%r11d // d+=T1 + addl %r12d,%edx // h+=T1 - leaq 4(%rbp),%rbp - addl %r14d,%edx + leaq 4(%rbp),%rbp // round++ + addl %r14d,%edx // h+=Sigma0(a) movl 20(%rsi),%r12d movl %r11d,%r13d movl %edx,%r14d @@ -267,38 +267,38 @@ L$loop: xorl %r11d,%r13d rorl $9,%r14d - xorl %ebx,%edi + xorl %ebx,%edi // f^g movl %r12d,20(%rsp) xorl %edx,%r14d - andl %r11d,%edi + andl %r11d,%edi // (f^g)&e rorl $5,%r13d - addl %ecx,%r12d - xorl %ebx,%edi + addl %ecx,%r12d // T1+=h + xorl %ebx,%edi // Ch(e,f,g)=((f^g)&e)^g rorl $11,%r14d xorl %r11d,%r13d - addl %edi,%r12d + addl %edi,%r12d // T1+=Ch(e,f,g) movl %edx,%edi - addl (%rbp),%r12d + addl (%rbp),%r12d // T1+=K[round] xorl %edx,%r14d - xorl %r8d,%edi - rorl $6,%r13d + xorl %r8d,%edi // a^b, b^c in next round + rorl $6,%r13d // Sigma1(e) movl %r8d,%ecx andl %edi,%r15d - rorl $2,%r14d - addl %r13d,%r12d + rorl $2,%r14d // Sigma0(a) + addl %r13d,%r12d // T1+=Sigma1(e) - xorl %r15d,%ecx - addl %r12d,%r10d - addl %r12d,%ecx + xorl %r15d,%ecx // h=Maj(a,b,c)=Ch(a^b,c,b) + addl %r12d,%r10d // d+=T1 + addl %r12d,%ecx // h+=T1 - leaq 4(%rbp),%rbp - addl %r14d,%ecx + leaq 4(%rbp),%rbp // round++ + addl %r14d,%ecx // h+=Sigma0(a) movl 24(%rsi),%r12d movl %r10d,%r13d movl %ecx,%r14d @@ -308,38 +308,38 @@ L$loop: xorl %r10d,%r13d rorl $9,%r14d - xorl %eax,%r15d + xorl %eax,%r15d // f^g movl %r12d,24(%rsp) xorl %ecx,%r14d - andl %r10d,%r15d + andl %r10d,%r15d // (f^g)&e rorl $5,%r13d - addl %ebx,%r12d - xorl %eax,%r15d + addl %ebx,%r12d // T1+=h + xorl %eax,%r15d // Ch(e,f,g)=((f^g)&e)^g rorl $11,%r14d xorl %r10d,%r13d - addl %r15d,%r12d + addl %r15d,%r12d // T1+=Ch(e,f,g) movl %ecx,%r15d - addl (%rbp),%r12d + addl (%rbp),%r12d // T1+=K[round] xorl %ecx,%r14d - xorl %edx,%r15d - rorl $6,%r13d + xorl %edx,%r15d // a^b, b^c in next round + rorl $6,%r13d // Sigma1(e) movl %edx,%ebx andl %r15d,%edi - rorl $2,%r14d - addl %r13d,%r12d + rorl $2,%r14d // Sigma0(a) + addl %r13d,%r12d // T1+=Sigma1(e) - xorl %edi,%ebx - addl %r12d,%r9d - addl %r12d,%ebx + xorl %edi,%ebx // h=Maj(a,b,c)=Ch(a^b,c,b) + addl %r12d,%r9d // d+=T1 + addl %r12d,%ebx // h+=T1 - leaq 4(%rbp),%rbp - addl %r14d,%ebx + leaq 4(%rbp),%rbp // round++ + addl %r14d,%ebx // h+=Sigma0(a) movl 28(%rsi),%r12d movl %r9d,%r13d movl %ebx,%r14d @@ -349,38 +349,38 @@ L$loop: xorl %r9d,%r13d rorl $9,%r14d - xorl %r11d,%edi + xorl %r11d,%edi // f^g movl %r12d,28(%rsp) xorl %ebx,%r14d - andl %r9d,%edi + andl %r9d,%edi // (f^g)&e rorl $5,%r13d - addl %eax,%r12d - xorl %r11d,%edi + addl %eax,%r12d // T1+=h + xorl %r11d,%edi // Ch(e,f,g)=((f^g)&e)^g rorl $11,%r14d xorl %r9d,%r13d - addl %edi,%r12d + addl %edi,%r12d // T1+=Ch(e,f,g) movl %ebx,%edi - addl (%rbp),%r12d + addl (%rbp),%r12d // T1+=K[round] xorl %ebx,%r14d - xorl %ecx,%edi - rorl $6,%r13d + xorl %ecx,%edi // a^b, b^c in next round + rorl $6,%r13d // Sigma1(e) movl %ecx,%eax andl %edi,%r15d - rorl $2,%r14d - addl %r13d,%r12d + rorl $2,%r14d // Sigma0(a) + addl %r13d,%r12d // T1+=Sigma1(e) - xorl %r15d,%eax - addl %r12d,%r8d - addl %r12d,%eax + xorl %r15d,%eax // h=Maj(a,b,c)=Ch(a^b,c,b) + addl %r12d,%r8d // d+=T1 + addl %r12d,%eax // h+=T1 - leaq 20(%rbp),%rbp - addl %r14d,%eax + leaq 20(%rbp),%rbp // round++ + addl %r14d,%eax // h+=Sigma0(a) movl 32(%rsi),%r12d movl %r8d,%r13d movl %eax,%r14d @@ -390,38 +390,38 @@ L$loop: xorl %r8d,%r13d rorl $9,%r14d - xorl %r10d,%r15d + xorl %r10d,%r15d // f^g movl %r12d,32(%rsp) xorl %eax,%r14d - andl %r8d,%r15d + andl %r8d,%r15d // (f^g)&e rorl $5,%r13d - addl %r11d,%r12d - xorl %r10d,%r15d + addl %r11d,%r12d // T1+=h + xorl %r10d,%r15d // Ch(e,f,g)=((f^g)&e)^g rorl $11,%r14d xorl %r8d,%r13d - addl %r15d,%r12d + addl %r15d,%r12d // T1+=Ch(e,f,g) movl %eax,%r15d - addl (%rbp),%r12d + addl (%rbp),%r12d // T1+=K[round] xorl %eax,%r14d - xorl %ebx,%r15d - rorl $6,%r13d + xorl %ebx,%r15d // a^b, b^c in next round + rorl $6,%r13d // Sigma1(e) movl %ebx,%r11d andl %r15d,%edi - rorl $2,%r14d - addl %r13d,%r12d + rorl $2,%r14d // Sigma0(a) + addl %r13d,%r12d // T1+=Sigma1(e) - xorl %edi,%r11d - addl %r12d,%edx - addl %r12d,%r11d + xorl %edi,%r11d // h=Maj(a,b,c)=Ch(a^b,c,b) + addl %r12d,%edx // d+=T1 + addl %r12d,%r11d // h+=T1 - leaq 4(%rbp),%rbp - addl %r14d,%r11d + leaq 4(%rbp),%rbp // round++ + addl %r14d,%r11d // h+=Sigma0(a) movl 36(%rsi),%r12d movl %edx,%r13d movl %r11d,%r14d @@ -431,38 +431,38 @@ L$loop: xorl %edx,%r13d rorl $9,%r14d - xorl %r9d,%edi + xorl %r9d,%edi // f^g movl %r12d,36(%rsp) xorl %r11d,%r14d - andl %edx,%edi + andl %edx,%edi // (f^g)&e rorl $5,%r13d - addl %r10d,%r12d - xorl %r9d,%edi + addl %r10d,%r12d // T1+=h + xorl %r9d,%edi // Ch(e,f,g)=((f^g)&e)^g rorl $11,%r14d xorl %edx,%r13d - addl %edi,%r12d + addl %edi,%r12d // T1+=Ch(e,f,g) movl %r11d,%edi - addl (%rbp),%r12d + addl (%rbp),%r12d // T1+=K[round] xorl %r11d,%r14d - xorl %eax,%edi - rorl $6,%r13d + xorl %eax,%edi // a^b, b^c in next round + rorl $6,%r13d // Sigma1(e) movl %eax,%r10d andl %edi,%r15d - rorl $2,%r14d - addl %r13d,%r12d + rorl $2,%r14d // Sigma0(a) + addl %r13d,%r12d // T1+=Sigma1(e) - xorl %r15d,%r10d - addl %r12d,%ecx - addl %r12d,%r10d + xorl %r15d,%r10d // h=Maj(a,b,c)=Ch(a^b,c,b) + addl %r12d,%ecx // d+=T1 + addl %r12d,%r10d // h+=T1 - leaq 4(%rbp),%rbp - addl %r14d,%r10d + leaq 4(%rbp),%rbp // round++ + addl %r14d,%r10d // h+=Sigma0(a) movl 40(%rsi),%r12d movl %ecx,%r13d movl %r10d,%r14d @@ -472,38 +472,38 @@ L$loop: xorl %ecx,%r13d rorl $9,%r14d - xorl %r8d,%r15d + xorl %r8d,%r15d // f^g movl %r12d,40(%rsp) xorl %r10d,%r14d - andl %ecx,%r15d + andl %ecx,%r15d // (f^g)&e rorl $5,%r13d - addl %r9d,%r12d - xorl %r8d,%r15d + addl %r9d,%r12d // T1+=h + xorl %r8d,%r15d // Ch(e,f,g)=((f^g)&e)^g rorl $11,%r14d xorl %ecx,%r13d - addl %r15d,%r12d + addl %r15d,%r12d // T1+=Ch(e,f,g) movl %r10d,%r15d - addl (%rbp),%r12d + addl (%rbp),%r12d // T1+=K[round] xorl %r10d,%r14d - xorl %r11d,%r15d - rorl $6,%r13d + xorl %r11d,%r15d // a^b, b^c in next round + rorl $6,%r13d // Sigma1(e) movl %r11d,%r9d andl %r15d,%edi - rorl $2,%r14d - addl %r13d,%r12d + rorl $2,%r14d // Sigma0(a) + addl %r13d,%r12d // T1+=Sigma1(e) - xorl %edi,%r9d - addl %r12d,%ebx - addl %r12d,%r9d + xorl %edi,%r9d // h=Maj(a,b,c)=Ch(a^b,c,b) + addl %r12d,%ebx // d+=T1 + addl %r12d,%r9d // h+=T1 - leaq 4(%rbp),%rbp - addl %r14d,%r9d + leaq 4(%rbp),%rbp // round++ + addl %r14d,%r9d // h+=Sigma0(a) movl 44(%rsi),%r12d movl %ebx,%r13d movl %r9d,%r14d @@ -513,38 +513,38 @@ L$loop: xorl %ebx,%r13d rorl $9,%r14d - xorl %edx,%edi + xorl %edx,%edi // f^g movl %r12d,44(%rsp) xorl %r9d,%r14d - andl %ebx,%edi + andl %ebx,%edi // (f^g)&e rorl $5,%r13d - addl %r8d,%r12d - xorl %edx,%edi + addl %r8d,%r12d // T1+=h + xorl %edx,%edi // Ch(e,f,g)=((f^g)&e)^g rorl $11,%r14d xorl %ebx,%r13d - addl %edi,%r12d + addl %edi,%r12d // T1+=Ch(e,f,g) movl %r9d,%edi - addl (%rbp),%r12d + addl (%rbp),%r12d // T1+=K[round] xorl %r9d,%r14d - xorl %r10d,%edi - rorl $6,%r13d + xorl %r10d,%edi // a^b, b^c in next round + rorl $6,%r13d // Sigma1(e) movl %r10d,%r8d andl %edi,%r15d - rorl $2,%r14d - addl %r13d,%r12d + rorl $2,%r14d // Sigma0(a) + addl %r13d,%r12d // T1+=Sigma1(e) - xorl %r15d,%r8d - addl %r12d,%eax - addl %r12d,%r8d + xorl %r15d,%r8d // h=Maj(a,b,c)=Ch(a^b,c,b) + addl %r12d,%eax // d+=T1 + addl %r12d,%r8d // h+=T1 - leaq 20(%rbp),%rbp - addl %r14d,%r8d + leaq 20(%rbp),%rbp // round++ + addl %r14d,%r8d // h+=Sigma0(a) movl 48(%rsi),%r12d movl %eax,%r13d movl %r8d,%r14d @@ -554,38 +554,38 @@ L$loop: xorl %eax,%r13d rorl $9,%r14d - xorl %ecx,%r15d + xorl %ecx,%r15d // f^g movl %r12d,48(%rsp) xorl %r8d,%r14d - andl %eax,%r15d + andl %eax,%r15d // (f^g)&e rorl $5,%r13d - addl %edx,%r12d - xorl %ecx,%r15d + addl %edx,%r12d // T1+=h + xorl %ecx,%r15d // Ch(e,f,g)=((f^g)&e)^g rorl $11,%r14d xorl %eax,%r13d - addl %r15d,%r12d + addl %r15d,%r12d // T1+=Ch(e,f,g) movl %r8d,%r15d - addl (%rbp),%r12d + addl (%rbp),%r12d // T1+=K[round] xorl %r8d,%r14d - xorl %r9d,%r15d - rorl $6,%r13d + xorl %r9d,%r15d // a^b, b^c in next round + rorl $6,%r13d // Sigma1(e) movl %r9d,%edx andl %r15d,%edi - rorl $2,%r14d - addl %r13d,%r12d + rorl $2,%r14d // Sigma0(a) + addl %r13d,%r12d // T1+=Sigma1(e) - xorl %edi,%edx - addl %r12d,%r11d - addl %r12d,%edx + xorl %edi,%edx // h=Maj(a,b,c)=Ch(a^b,c,b) + addl %r12d,%r11d // d+=T1 + addl %r12d,%edx // h+=T1 - leaq 4(%rbp),%rbp - addl %r14d,%edx + leaq 4(%rbp),%rbp // round++ + addl %r14d,%edx // h+=Sigma0(a) movl 52(%rsi),%r12d movl %r11d,%r13d movl %edx,%r14d @@ -595,38 +595,38 @@ L$loop: xorl %r11d,%r13d rorl $9,%r14d - xorl %ebx,%edi + xorl %ebx,%edi // f^g movl %r12d,52(%rsp) xorl %edx,%r14d - andl %r11d,%edi + andl %r11d,%edi // (f^g)&e rorl $5,%r13d - addl %ecx,%r12d - xorl %ebx,%edi + addl %ecx,%r12d // T1+=h + xorl %ebx,%edi // Ch(e,f,g)=((f^g)&e)^g rorl $11,%r14d xorl %r11d,%r13d - addl %edi,%r12d + addl %edi,%r12d // T1+=Ch(e,f,g) movl %edx,%edi - addl (%rbp),%r12d + addl (%rbp),%r12d // T1+=K[round] xorl %edx,%r14d - xorl %r8d,%edi - rorl $6,%r13d + xorl %r8d,%edi // a^b, b^c in next round + rorl $6,%r13d // Sigma1(e) movl %r8d,%ecx andl %edi,%r15d - rorl $2,%r14d - addl %r13d,%r12d + rorl $2,%r14d // Sigma0(a) + addl %r13d,%r12d // T1+=Sigma1(e) - xorl %r15d,%ecx - addl %r12d,%r10d - addl %r12d,%ecx + xorl %r15d,%ecx // h=Maj(a,b,c)=Ch(a^b,c,b) + addl %r12d,%r10d // d+=T1 + addl %r12d,%ecx // h+=T1 - leaq 4(%rbp),%rbp - addl %r14d,%ecx + leaq 4(%rbp),%rbp // round++ + addl %r14d,%ecx // h+=Sigma0(a) movl 56(%rsi),%r12d movl %r10d,%r13d movl %ecx,%r14d @@ -636,38 +636,38 @@ L$loop: xorl %r10d,%r13d rorl $9,%r14d - xorl %eax,%r15d + xorl %eax,%r15d // f^g movl %r12d,56(%rsp) xorl %ecx,%r14d - andl %r10d,%r15d + andl %r10d,%r15d // (f^g)&e rorl $5,%r13d - addl %ebx,%r12d - xorl %eax,%r15d + addl %ebx,%r12d // T1+=h + xorl %eax,%r15d // Ch(e,f,g)=((f^g)&e)^g rorl $11,%r14d xorl %r10d,%r13d - addl %r15d,%r12d + addl %r15d,%r12d // T1+=Ch(e,f,g) movl %ecx,%r15d - addl (%rbp),%r12d + addl (%rbp),%r12d // T1+=K[round] xorl %ecx,%r14d - xorl %edx,%r15d - rorl $6,%r13d + xorl %edx,%r15d // a^b, b^c in next round + rorl $6,%r13d // Sigma1(e) movl %edx,%ebx andl %r15d,%edi - rorl $2,%r14d - addl %r13d,%r12d + rorl $2,%r14d // Sigma0(a) + addl %r13d,%r12d // T1+=Sigma1(e) - xorl %edi,%ebx - addl %r12d,%r9d - addl %r12d,%ebx + xorl %edi,%ebx // h=Maj(a,b,c)=Ch(a^b,c,b) + addl %r12d,%r9d // d+=T1 + addl %r12d,%ebx // h+=T1 - leaq 4(%rbp),%rbp - addl %r14d,%ebx + leaq 4(%rbp),%rbp // round++ + addl %r14d,%ebx // h+=Sigma0(a) movl 60(%rsi),%r12d movl %r9d,%r13d movl %ebx,%r14d @@ -677,37 +677,37 @@ L$loop: xorl %r9d,%r13d rorl $9,%r14d - xorl %r11d,%edi + xorl %r11d,%edi // f^g movl %r12d,60(%rsp) xorl %ebx,%r14d - andl %r9d,%edi + andl %r9d,%edi // (f^g)&e rorl $5,%r13d - addl %eax,%r12d - xorl %r11d,%edi + addl %eax,%r12d // T1+=h + xorl %r11d,%edi // Ch(e,f,g)=((f^g)&e)^g rorl $11,%r14d xorl %r9d,%r13d - addl %edi,%r12d + addl %edi,%r12d // T1+=Ch(e,f,g) movl %ebx,%edi - addl (%rbp),%r12d + addl (%rbp),%r12d // T1+=K[round] xorl %ebx,%r14d - xorl %ecx,%edi - rorl $6,%r13d + xorl %ecx,%edi // a^b, b^c in next round + rorl $6,%r13d // Sigma1(e) movl %ecx,%eax andl %edi,%r15d - rorl $2,%r14d - addl %r13d,%r12d + rorl $2,%r14d // Sigma0(a) + addl %r13d,%r12d // T1+=Sigma1(e) - xorl %r15d,%eax - addl %r12d,%r8d - addl %r12d,%eax + xorl %r15d,%eax // h=Maj(a,b,c)=Ch(a^b,c,b) + addl %r12d,%r8d // d+=T1 + addl %r12d,%eax // h+=T1 - leaq 20(%rbp),%rbp + leaq 20(%rbp),%rbp // round++ jmp L$rounds_16_xx .p2align 4 L$rounds_16_xx: @@ -716,7 +716,7 @@ L$rounds_16_xx: movl %r13d,%r12d rorl $11,%r13d - addl %r14d,%eax + addl %r14d,%eax // modulo-scheduled h+=Sigma0(a) movl %r15d,%r14d rorl $2,%r15d @@ -727,8 +727,8 @@ L$rounds_16_xx: shrl $10,%r14d rorl $17,%r15d - xorl %r13d,%r12d - xorl %r14d,%r15d + xorl %r13d,%r12d // sigma0(X[(i+1)&0xf]) + xorl %r14d,%r15d // sigma1(X[(i+14)&0xf]) addl 36(%rsp),%r12d addl 0(%rsp),%r12d @@ -740,43 +740,43 @@ L$rounds_16_xx: xorl %r8d,%r13d rorl $9,%r14d - xorl %r10d,%r15d + xorl %r10d,%r15d // f^g movl %r12d,0(%rsp) xorl %eax,%r14d - andl %r8d,%r15d + andl %r8d,%r15d // (f^g)&e rorl $5,%r13d - addl %r11d,%r12d - xorl %r10d,%r15d + addl %r11d,%r12d // T1+=h + xorl %r10d,%r15d // Ch(e,f,g)=((f^g)&e)^g rorl $11,%r14d xorl %r8d,%r13d - addl %r15d,%r12d + addl %r15d,%r12d // T1+=Ch(e,f,g) movl %eax,%r15d - addl (%rbp),%r12d + addl (%rbp),%r12d // T1+=K[round] xorl %eax,%r14d - xorl %ebx,%r15d - rorl $6,%r13d + xorl %ebx,%r15d // a^b, b^c in next round + rorl $6,%r13d // Sigma1(e) movl %ebx,%r11d andl %r15d,%edi - rorl $2,%r14d - addl %r13d,%r12d + rorl $2,%r14d // Sigma0(a) + addl %r13d,%r12d // T1+=Sigma1(e) - xorl %edi,%r11d - addl %r12d,%edx - addl %r12d,%r11d + xorl %edi,%r11d // h=Maj(a,b,c)=Ch(a^b,c,b) + addl %r12d,%edx // d+=T1 + addl %r12d,%r11d // h+=T1 - leaq 4(%rbp),%rbp + leaq 4(%rbp),%rbp // round++ movl 8(%rsp),%r13d movl 60(%rsp),%edi movl %r13d,%r12d rorl $11,%r13d - addl %r14d,%r11d + addl %r14d,%r11d // modulo-scheduled h+=Sigma0(a) movl %edi,%r14d rorl $2,%edi @@ -787,8 +787,8 @@ L$rounds_16_xx: shrl $10,%r14d rorl $17,%edi - xorl %r13d,%r12d - xorl %r14d,%edi + xorl %r13d,%r12d // sigma0(X[(i+1)&0xf]) + xorl %r14d,%edi // sigma1(X[(i+14)&0xf]) addl 40(%rsp),%r12d addl 4(%rsp),%r12d @@ -800,43 +800,43 @@ L$rounds_16_xx: xorl %edx,%r13d rorl $9,%r14d - xorl %r9d,%edi + xorl %r9d,%edi // f^g movl %r12d,4(%rsp) xorl %r11d,%r14d - andl %edx,%edi + andl %edx,%edi // (f^g)&e rorl $5,%r13d - addl %r10d,%r12d - xorl %r9d,%edi + addl %r10d,%r12d // T1+=h + xorl %r9d,%edi // Ch(e,f,g)=((f^g)&e)^g rorl $11,%r14d xorl %edx,%r13d - addl %edi,%r12d + addl %edi,%r12d // T1+=Ch(e,f,g) movl %r11d,%edi - addl (%rbp),%r12d + addl (%rbp),%r12d // T1+=K[round] xorl %r11d,%r14d - xorl %eax,%edi - rorl $6,%r13d + xorl %eax,%edi // a^b, b^c in next round + rorl $6,%r13d // Sigma1(e) movl %eax,%r10d andl %edi,%r15d - rorl $2,%r14d - addl %r13d,%r12d + rorl $2,%r14d // Sigma0(a) + addl %r13d,%r12d // T1+=Sigma1(e) - xorl %r15d,%r10d - addl %r12d,%ecx - addl %r12d,%r10d + xorl %r15d,%r10d // h=Maj(a,b,c)=Ch(a^b,c,b) + addl %r12d,%ecx // d+=T1 + addl %r12d,%r10d // h+=T1 - leaq 4(%rbp),%rbp + leaq 4(%rbp),%rbp // round++ movl 12(%rsp),%r13d movl 0(%rsp),%r15d movl %r13d,%r12d rorl $11,%r13d - addl %r14d,%r10d + addl %r14d,%r10d // modulo-scheduled h+=Sigma0(a) movl %r15d,%r14d rorl $2,%r15d @@ -847,8 +847,8 @@ L$rounds_16_xx: shrl $10,%r14d rorl $17,%r15d - xorl %r13d,%r12d - xorl %r14d,%r15d + xorl %r13d,%r12d // sigma0(X[(i+1)&0xf]) + xorl %r14d,%r15d // sigma1(X[(i+14)&0xf]) addl 44(%rsp),%r12d addl 8(%rsp),%r12d @@ -860,43 +860,43 @@ L$rounds_16_xx: xorl %ecx,%r13d rorl $9,%r14d - xorl %r8d,%r15d + xorl %r8d,%r15d // f^g movl %r12d,8(%rsp) xorl %r10d,%r14d - andl %ecx,%r15d + andl %ecx,%r15d // (f^g)&e rorl $5,%r13d - addl %r9d,%r12d - xorl %r8d,%r15d + addl %r9d,%r12d // T1+=h + xorl %r8d,%r15d // Ch(e,f,g)=((f^g)&e)^g rorl $11,%r14d xorl %ecx,%r13d - addl %r15d,%r12d + addl %r15d,%r12d // T1+=Ch(e,f,g) movl %r10d,%r15d - addl (%rbp),%r12d + addl (%rbp),%r12d // T1+=K[round] xorl %r10d,%r14d - xorl %r11d,%r15d - rorl $6,%r13d + xorl %r11d,%r15d // a^b, b^c in next round + rorl $6,%r13d // Sigma1(e) movl %r11d,%r9d andl %r15d,%edi - rorl $2,%r14d - addl %r13d,%r12d + rorl $2,%r14d // Sigma0(a) + addl %r13d,%r12d // T1+=Sigma1(e) - xorl %edi,%r9d - addl %r12d,%ebx - addl %r12d,%r9d + xorl %edi,%r9d // h=Maj(a,b,c)=Ch(a^b,c,b) + addl %r12d,%ebx // d+=T1 + addl %r12d,%r9d // h+=T1 - leaq 4(%rbp),%rbp + leaq 4(%rbp),%rbp // round++ movl 16(%rsp),%r13d movl 4(%rsp),%edi movl %r13d,%r12d rorl $11,%r13d - addl %r14d,%r9d + addl %r14d,%r9d // modulo-scheduled h+=Sigma0(a) movl %edi,%r14d rorl $2,%edi @@ -907,8 +907,8 @@ L$rounds_16_xx: shrl $10,%r14d rorl $17,%edi - xorl %r13d,%r12d - xorl %r14d,%edi + xorl %r13d,%r12d // sigma0(X[(i+1)&0xf]) + xorl %r14d,%edi // sigma1(X[(i+14)&0xf]) addl 48(%rsp),%r12d addl 12(%rsp),%r12d @@ -920,43 +920,43 @@ L$rounds_16_xx: xorl %ebx,%r13d rorl $9,%r14d - xorl %edx,%edi + xorl %edx,%edi // f^g movl %r12d,12(%rsp) xorl %r9d,%r14d - andl %ebx,%edi + andl %ebx,%edi // (f^g)&e rorl $5,%r13d - addl %r8d,%r12d - xorl %edx,%edi + addl %r8d,%r12d // T1+=h + xorl %edx,%edi // Ch(e,f,g)=((f^g)&e)^g rorl $11,%r14d xorl %ebx,%r13d - addl %edi,%r12d + addl %edi,%r12d // T1+=Ch(e,f,g) movl %r9d,%edi - addl (%rbp),%r12d + addl (%rbp),%r12d // T1+=K[round] xorl %r9d,%r14d - xorl %r10d,%edi - rorl $6,%r13d + xorl %r10d,%edi // a^b, b^c in next round + rorl $6,%r13d // Sigma1(e) movl %r10d,%r8d andl %edi,%r15d - rorl $2,%r14d - addl %r13d,%r12d + rorl $2,%r14d // Sigma0(a) + addl %r13d,%r12d // T1+=Sigma1(e) - xorl %r15d,%r8d - addl %r12d,%eax - addl %r12d,%r8d + xorl %r15d,%r8d // h=Maj(a,b,c)=Ch(a^b,c,b) + addl %r12d,%eax // d+=T1 + addl %r12d,%r8d // h+=T1 - leaq 20(%rbp),%rbp + leaq 20(%rbp),%rbp // round++ movl 20(%rsp),%r13d movl 8(%rsp),%r15d movl %r13d,%r12d rorl $11,%r13d - addl %r14d,%r8d + addl %r14d,%r8d // modulo-scheduled h+=Sigma0(a) movl %r15d,%r14d rorl $2,%r15d @@ -967,8 +967,8 @@ L$rounds_16_xx: shrl $10,%r14d rorl $17,%r15d - xorl %r13d,%r12d - xorl %r14d,%r15d + xorl %r13d,%r12d // sigma0(X[(i+1)&0xf]) + xorl %r14d,%r15d // sigma1(X[(i+14)&0xf]) addl 52(%rsp),%r12d addl 16(%rsp),%r12d @@ -980,43 +980,43 @@ L$rounds_16_xx: xorl %eax,%r13d rorl $9,%r14d - xorl %ecx,%r15d + xorl %ecx,%r15d // f^g movl %r12d,16(%rsp) xorl %r8d,%r14d - andl %eax,%r15d + andl %eax,%r15d // (f^g)&e rorl $5,%r13d - addl %edx,%r12d - xorl %ecx,%r15d + addl %edx,%r12d // T1+=h + xorl %ecx,%r15d // Ch(e,f,g)=((f^g)&e)^g rorl $11,%r14d xorl %eax,%r13d - addl %r15d,%r12d + addl %r15d,%r12d // T1+=Ch(e,f,g) movl %r8d,%r15d - addl (%rbp),%r12d + addl (%rbp),%r12d // T1+=K[round] xorl %r8d,%r14d - xorl %r9d,%r15d - rorl $6,%r13d + xorl %r9d,%r15d // a^b, b^c in next round + rorl $6,%r13d // Sigma1(e) movl %r9d,%edx andl %r15d,%edi - rorl $2,%r14d - addl %r13d,%r12d + rorl $2,%r14d // Sigma0(a) + addl %r13d,%r12d // T1+=Sigma1(e) - xorl %edi,%edx - addl %r12d,%r11d - addl %r12d,%edx + xorl %edi,%edx // h=Maj(a,b,c)=Ch(a^b,c,b) + addl %r12d,%r11d // d+=T1 + addl %r12d,%edx // h+=T1 - leaq 4(%rbp),%rbp + leaq 4(%rbp),%rbp // round++ movl 24(%rsp),%r13d movl 12(%rsp),%edi movl %r13d,%r12d rorl $11,%r13d - addl %r14d,%edx + addl %r14d,%edx // modulo-scheduled h+=Sigma0(a) movl %edi,%r14d rorl $2,%edi @@ -1027,8 +1027,8 @@ L$rounds_16_xx: shrl $10,%r14d rorl $17,%edi - xorl %r13d,%r12d - xorl %r14d,%edi + xorl %r13d,%r12d // sigma0(X[(i+1)&0xf]) + xorl %r14d,%edi // sigma1(X[(i+14)&0xf]) addl 56(%rsp),%r12d addl 20(%rsp),%r12d @@ -1040,43 +1040,43 @@ L$rounds_16_xx: xorl %r11d,%r13d rorl $9,%r14d - xorl %ebx,%edi + xorl %ebx,%edi // f^g movl %r12d,20(%rsp) xorl %edx,%r14d - andl %r11d,%edi + andl %r11d,%edi // (f^g)&e rorl $5,%r13d - addl %ecx,%r12d - xorl %ebx,%edi + addl %ecx,%r12d // T1+=h + xorl %ebx,%edi // Ch(e,f,g)=((f^g)&e)^g rorl $11,%r14d xorl %r11d,%r13d - addl %edi,%r12d + addl %edi,%r12d // T1+=Ch(e,f,g) movl %edx,%edi - addl (%rbp),%r12d + addl (%rbp),%r12d // T1+=K[round] xorl %edx,%r14d - xorl %r8d,%edi - rorl $6,%r13d + xorl %r8d,%edi // a^b, b^c in next round + rorl $6,%r13d // Sigma1(e) movl %r8d,%ecx andl %edi,%r15d - rorl $2,%r14d - addl %r13d,%r12d + rorl $2,%r14d // Sigma0(a) + addl %r13d,%r12d // T1+=Sigma1(e) - xorl %r15d,%ecx - addl %r12d,%r10d - addl %r12d,%ecx + xorl %r15d,%ecx // h=Maj(a,b,c)=Ch(a^b,c,b) + addl %r12d,%r10d // d+=T1 + addl %r12d,%ecx // h+=T1 - leaq 4(%rbp),%rbp + leaq 4(%rbp),%rbp // round++ movl 28(%rsp),%r13d movl 16(%rsp),%r15d movl %r13d,%r12d rorl $11,%r13d - addl %r14d,%ecx + addl %r14d,%ecx // modulo-scheduled h+=Sigma0(a) movl %r15d,%r14d rorl $2,%r15d @@ -1087,8 +1087,8 @@ L$rounds_16_xx: shrl $10,%r14d rorl $17,%r15d - xorl %r13d,%r12d - xorl %r14d,%r15d + xorl %r13d,%r12d // sigma0(X[(i+1)&0xf]) + xorl %r14d,%r15d // sigma1(X[(i+14)&0xf]) addl 60(%rsp),%r12d addl 24(%rsp),%r12d @@ -1100,43 +1100,43 @@ L$rounds_16_xx: xorl %r10d,%r13d rorl $9,%r14d - xorl %eax,%r15d + xorl %eax,%r15d // f^g movl %r12d,24(%rsp) xorl %ecx,%r14d - andl %r10d,%r15d + andl %r10d,%r15d // (f^g)&e rorl $5,%r13d - addl %ebx,%r12d - xorl %eax,%r15d + addl %ebx,%r12d // T1+=h + xorl %eax,%r15d // Ch(e,f,g)=((f^g)&e)^g rorl $11,%r14d xorl %r10d,%r13d - addl %r15d,%r12d + addl %r15d,%r12d // T1+=Ch(e,f,g) movl %ecx,%r15d - addl (%rbp),%r12d + addl (%rbp),%r12d // T1+=K[round] xorl %ecx,%r14d - xorl %edx,%r15d - rorl $6,%r13d + xorl %edx,%r15d // a^b, b^c in next round + rorl $6,%r13d // Sigma1(e) movl %edx,%ebx andl %r15d,%edi - rorl $2,%r14d - addl %r13d,%r12d + rorl $2,%r14d // Sigma0(a) + addl %r13d,%r12d // T1+=Sigma1(e) - xorl %edi,%ebx - addl %r12d,%r9d - addl %r12d,%ebx + xorl %edi,%ebx // h=Maj(a,b,c)=Ch(a^b,c,b) + addl %r12d,%r9d // d+=T1 + addl %r12d,%ebx // h+=T1 - leaq 4(%rbp),%rbp + leaq 4(%rbp),%rbp // round++ movl 32(%rsp),%r13d movl 20(%rsp),%edi movl %r13d,%r12d rorl $11,%r13d - addl %r14d,%ebx + addl %r14d,%ebx // modulo-scheduled h+=Sigma0(a) movl %edi,%r14d rorl $2,%edi @@ -1147,8 +1147,8 @@ L$rounds_16_xx: shrl $10,%r14d rorl $17,%edi - xorl %r13d,%r12d - xorl %r14d,%edi + xorl %r13d,%r12d // sigma0(X[(i+1)&0xf]) + xorl %r14d,%edi // sigma1(X[(i+14)&0xf]) addl 0(%rsp),%r12d addl 28(%rsp),%r12d @@ -1160,43 +1160,43 @@ L$rounds_16_xx: xorl %r9d,%r13d rorl $9,%r14d - xorl %r11d,%edi + xorl %r11d,%edi // f^g movl %r12d,28(%rsp) xorl %ebx,%r14d - andl %r9d,%edi + andl %r9d,%edi // (f^g)&e rorl $5,%r13d - addl %eax,%r12d - xorl %r11d,%edi + addl %eax,%r12d // T1+=h + xorl %r11d,%edi // Ch(e,f,g)=((f^g)&e)^g rorl $11,%r14d xorl %r9d,%r13d - addl %edi,%r12d + addl %edi,%r12d // T1+=Ch(e,f,g) movl %ebx,%edi - addl (%rbp),%r12d + addl (%rbp),%r12d // T1+=K[round] xorl %ebx,%r14d - xorl %ecx,%edi - rorl $6,%r13d + xorl %ecx,%edi // a^b, b^c in next round + rorl $6,%r13d // Sigma1(e) movl %ecx,%eax andl %edi,%r15d - rorl $2,%r14d - addl %r13d,%r12d + rorl $2,%r14d // Sigma0(a) + addl %r13d,%r12d // T1+=Sigma1(e) - xorl %r15d,%eax - addl %r12d,%r8d - addl %r12d,%eax + xorl %r15d,%eax // h=Maj(a,b,c)=Ch(a^b,c,b) + addl %r12d,%r8d // d+=T1 + addl %r12d,%eax // h+=T1 - leaq 20(%rbp),%rbp + leaq 20(%rbp),%rbp // round++ movl 36(%rsp),%r13d movl 24(%rsp),%r15d movl %r13d,%r12d rorl $11,%r13d - addl %r14d,%eax + addl %r14d,%eax // modulo-scheduled h+=Sigma0(a) movl %r15d,%r14d rorl $2,%r15d @@ -1207,8 +1207,8 @@ L$rounds_16_xx: shrl $10,%r14d rorl $17,%r15d - xorl %r13d,%r12d - xorl %r14d,%r15d + xorl %r13d,%r12d // sigma0(X[(i+1)&0xf]) + xorl %r14d,%r15d // sigma1(X[(i+14)&0xf]) addl 4(%rsp),%r12d addl 32(%rsp),%r12d @@ -1220,43 +1220,43 @@ L$rounds_16_xx: xorl %r8d,%r13d rorl $9,%r14d - xorl %r10d,%r15d + xorl %r10d,%r15d // f^g movl %r12d,32(%rsp) xorl %eax,%r14d - andl %r8d,%r15d + andl %r8d,%r15d // (f^g)&e rorl $5,%r13d - addl %r11d,%r12d - xorl %r10d,%r15d + addl %r11d,%r12d // T1+=h + xorl %r10d,%r15d // Ch(e,f,g)=((f^g)&e)^g rorl $11,%r14d xorl %r8d,%r13d - addl %r15d,%r12d + addl %r15d,%r12d // T1+=Ch(e,f,g) movl %eax,%r15d - addl (%rbp),%r12d + addl (%rbp),%r12d // T1+=K[round] xorl %eax,%r14d - xorl %ebx,%r15d - rorl $6,%r13d + xorl %ebx,%r15d // a^b, b^c in next round + rorl $6,%r13d // Sigma1(e) movl %ebx,%r11d andl %r15d,%edi - rorl $2,%r14d - addl %r13d,%r12d + rorl $2,%r14d // Sigma0(a) + addl %r13d,%r12d // T1+=Sigma1(e) - xorl %edi,%r11d - addl %r12d,%edx - addl %r12d,%r11d + xorl %edi,%r11d // h=Maj(a,b,c)=Ch(a^b,c,b) + addl %r12d,%edx // d+=T1 + addl %r12d,%r11d // h+=T1 - leaq 4(%rbp),%rbp + leaq 4(%rbp),%rbp // round++ movl 40(%rsp),%r13d movl 28(%rsp),%edi movl %r13d,%r12d rorl $11,%r13d - addl %r14d,%r11d + addl %r14d,%r11d // modulo-scheduled h+=Sigma0(a) movl %edi,%r14d rorl $2,%edi @@ -1267,8 +1267,8 @@ L$rounds_16_xx: shrl $10,%r14d rorl $17,%edi - xorl %r13d,%r12d - xorl %r14d,%edi + xorl %r13d,%r12d // sigma0(X[(i+1)&0xf]) + xorl %r14d,%edi // sigma1(X[(i+14)&0xf]) addl 8(%rsp),%r12d addl 36(%rsp),%r12d @@ -1280,43 +1280,43 @@ L$rounds_16_xx: xorl %edx,%r13d rorl $9,%r14d - xorl %r9d,%edi + xorl %r9d,%edi // f^g movl %r12d,36(%rsp) xorl %r11d,%r14d - andl %edx,%edi + andl %edx,%edi // (f^g)&e rorl $5,%r13d - addl %r10d,%r12d - xorl %r9d,%edi + addl %r10d,%r12d // T1+=h + xorl %r9d,%edi // Ch(e,f,g)=((f^g)&e)^g rorl $11,%r14d xorl %edx,%r13d - addl %edi,%r12d + addl %edi,%r12d // T1+=Ch(e,f,g) movl %r11d,%edi - addl (%rbp),%r12d + addl (%rbp),%r12d // T1+=K[round] xorl %r11d,%r14d - xorl %eax,%edi - rorl $6,%r13d + xorl %eax,%edi // a^b, b^c in next round + rorl $6,%r13d // Sigma1(e) movl %eax,%r10d andl %edi,%r15d - rorl $2,%r14d - addl %r13d,%r12d + rorl $2,%r14d // Sigma0(a) + addl %r13d,%r12d // T1+=Sigma1(e) - xorl %r15d,%r10d - addl %r12d,%ecx - addl %r12d,%r10d + xorl %r15d,%r10d // h=Maj(a,b,c)=Ch(a^b,c,b) + addl %r12d,%ecx // d+=T1 + addl %r12d,%r10d // h+=T1 - leaq 4(%rbp),%rbp + leaq 4(%rbp),%rbp // round++ movl 44(%rsp),%r13d movl 32(%rsp),%r15d movl %r13d,%r12d rorl $11,%r13d - addl %r14d,%r10d + addl %r14d,%r10d // modulo-scheduled h+=Sigma0(a) movl %r15d,%r14d rorl $2,%r15d @@ -1327,8 +1327,8 @@ L$rounds_16_xx: shrl $10,%r14d rorl $17,%r15d - xorl %r13d,%r12d - xorl %r14d,%r15d + xorl %r13d,%r12d // sigma0(X[(i+1)&0xf]) + xorl %r14d,%r15d // sigma1(X[(i+14)&0xf]) addl 12(%rsp),%r12d addl 40(%rsp),%r12d @@ -1340,43 +1340,43 @@ L$rounds_16_xx: xorl %ecx,%r13d rorl $9,%r14d - xorl %r8d,%r15d + xorl %r8d,%r15d // f^g movl %r12d,40(%rsp) xorl %r10d,%r14d - andl %ecx,%r15d + andl %ecx,%r15d // (f^g)&e rorl $5,%r13d - addl %r9d,%r12d - xorl %r8d,%r15d + addl %r9d,%r12d // T1+=h + xorl %r8d,%r15d // Ch(e,f,g)=((f^g)&e)^g rorl $11,%r14d xorl %ecx,%r13d - addl %r15d,%r12d + addl %r15d,%r12d // T1+=Ch(e,f,g) movl %r10d,%r15d - addl (%rbp),%r12d + addl (%rbp),%r12d // T1+=K[round] xorl %r10d,%r14d - xorl %r11d,%r15d - rorl $6,%r13d + xorl %r11d,%r15d // a^b, b^c in next round + rorl $6,%r13d // Sigma1(e) movl %r11d,%r9d andl %r15d,%edi - rorl $2,%r14d - addl %r13d,%r12d + rorl $2,%r14d // Sigma0(a) + addl %r13d,%r12d // T1+=Sigma1(e) - xorl %edi,%r9d - addl %r12d,%ebx - addl %r12d,%r9d + xorl %edi,%r9d // h=Maj(a,b,c)=Ch(a^b,c,b) + addl %r12d,%ebx // d+=T1 + addl %r12d,%r9d // h+=T1 - leaq 4(%rbp),%rbp + leaq 4(%rbp),%rbp // round++ movl 48(%rsp),%r13d movl 36(%rsp),%edi movl %r13d,%r12d rorl $11,%r13d - addl %r14d,%r9d + addl %r14d,%r9d // modulo-scheduled h+=Sigma0(a) movl %edi,%r14d rorl $2,%edi @@ -1387,8 +1387,8 @@ L$rounds_16_xx: shrl $10,%r14d rorl $17,%edi - xorl %r13d,%r12d - xorl %r14d,%edi + xorl %r13d,%r12d // sigma0(X[(i+1)&0xf]) + xorl %r14d,%edi // sigma1(X[(i+14)&0xf]) addl 16(%rsp),%r12d addl 44(%rsp),%r12d @@ -1400,43 +1400,43 @@ L$rounds_16_xx: xorl %ebx,%r13d rorl $9,%r14d - xorl %edx,%edi + xorl %edx,%edi // f^g movl %r12d,44(%rsp) xorl %r9d,%r14d - andl %ebx,%edi + andl %ebx,%edi // (f^g)&e rorl $5,%r13d - addl %r8d,%r12d - xorl %edx,%edi + addl %r8d,%r12d // T1+=h + xorl %edx,%edi // Ch(e,f,g)=((f^g)&e)^g rorl $11,%r14d xorl %ebx,%r13d - addl %edi,%r12d + addl %edi,%r12d // T1+=Ch(e,f,g) movl %r9d,%edi - addl (%rbp),%r12d + addl (%rbp),%r12d // T1+=K[round] xorl %r9d,%r14d - xorl %r10d,%edi - rorl $6,%r13d + xorl %r10d,%edi // a^b, b^c in next round + rorl $6,%r13d // Sigma1(e) movl %r10d,%r8d andl %edi,%r15d - rorl $2,%r14d - addl %r13d,%r12d + rorl $2,%r14d // Sigma0(a) + addl %r13d,%r12d // T1+=Sigma1(e) - xorl %r15d,%r8d - addl %r12d,%eax - addl %r12d,%r8d + xorl %r15d,%r8d // h=Maj(a,b,c)=Ch(a^b,c,b) + addl %r12d,%eax // d+=T1 + addl %r12d,%r8d // h+=T1 - leaq 20(%rbp),%rbp + leaq 20(%rbp),%rbp // round++ movl 52(%rsp),%r13d movl 40(%rsp),%r15d movl %r13d,%r12d rorl $11,%r13d - addl %r14d,%r8d + addl %r14d,%r8d // modulo-scheduled h+=Sigma0(a) movl %r15d,%r14d rorl $2,%r15d @@ -1447,8 +1447,8 @@ L$rounds_16_xx: shrl $10,%r14d rorl $17,%r15d - xorl %r13d,%r12d - xorl %r14d,%r15d + xorl %r13d,%r12d // sigma0(X[(i+1)&0xf]) + xorl %r14d,%r15d // sigma1(X[(i+14)&0xf]) addl 20(%rsp),%r12d addl 48(%rsp),%r12d @@ -1460,43 +1460,43 @@ L$rounds_16_xx: xorl %eax,%r13d rorl $9,%r14d - xorl %ecx,%r15d + xorl %ecx,%r15d // f^g movl %r12d,48(%rsp) xorl %r8d,%r14d - andl %eax,%r15d + andl %eax,%r15d // (f^g)&e rorl $5,%r13d - addl %edx,%r12d - xorl %ecx,%r15d + addl %edx,%r12d // T1+=h + xorl %ecx,%r15d // Ch(e,f,g)=((f^g)&e)^g rorl $11,%r14d xorl %eax,%r13d - addl %r15d,%r12d + addl %r15d,%r12d // T1+=Ch(e,f,g) movl %r8d,%r15d - addl (%rbp),%r12d + addl (%rbp),%r12d // T1+=K[round] xorl %r8d,%r14d - xorl %r9d,%r15d - rorl $6,%r13d + xorl %r9d,%r15d // a^b, b^c in next round + rorl $6,%r13d // Sigma1(e) movl %r9d,%edx andl %r15d,%edi - rorl $2,%r14d - addl %r13d,%r12d + rorl $2,%r14d // Sigma0(a) + addl %r13d,%r12d // T1+=Sigma1(e) - xorl %edi,%edx - addl %r12d,%r11d - addl %r12d,%edx + xorl %edi,%edx // h=Maj(a,b,c)=Ch(a^b,c,b) + addl %r12d,%r11d // d+=T1 + addl %r12d,%edx // h+=T1 - leaq 4(%rbp),%rbp + leaq 4(%rbp),%rbp // round++ movl 56(%rsp),%r13d movl 44(%rsp),%edi movl %r13d,%r12d rorl $11,%r13d - addl %r14d,%edx + addl %r14d,%edx // modulo-scheduled h+=Sigma0(a) movl %edi,%r14d rorl $2,%edi @@ -1507,8 +1507,8 @@ L$rounds_16_xx: shrl $10,%r14d rorl $17,%edi - xorl %r13d,%r12d - xorl %r14d,%edi + xorl %r13d,%r12d // sigma0(X[(i+1)&0xf]) + xorl %r14d,%edi // sigma1(X[(i+14)&0xf]) addl 24(%rsp),%r12d addl 52(%rsp),%r12d @@ -1520,43 +1520,43 @@ L$rounds_16_xx: xorl %r11d,%r13d rorl $9,%r14d - xorl %ebx,%edi + xorl %ebx,%edi // f^g movl %r12d,52(%rsp) xorl %edx,%r14d - andl %r11d,%edi + andl %r11d,%edi // (f^g)&e rorl $5,%r13d - addl %ecx,%r12d - xorl %ebx,%edi + addl %ecx,%r12d // T1+=h + xorl %ebx,%edi // Ch(e,f,g)=((f^g)&e)^g rorl $11,%r14d xorl %r11d,%r13d - addl %edi,%r12d + addl %edi,%r12d // T1+=Ch(e,f,g) movl %edx,%edi - addl (%rbp),%r12d + addl (%rbp),%r12d // T1+=K[round] xorl %edx,%r14d - xorl %r8d,%edi - rorl $6,%r13d + xorl %r8d,%edi // a^b, b^c in next round + rorl $6,%r13d // Sigma1(e) movl %r8d,%ecx andl %edi,%r15d - rorl $2,%r14d - addl %r13d,%r12d + rorl $2,%r14d // Sigma0(a) + addl %r13d,%r12d // T1+=Sigma1(e) - xorl %r15d,%ecx - addl %r12d,%r10d - addl %r12d,%ecx + xorl %r15d,%ecx // h=Maj(a,b,c)=Ch(a^b,c,b) + addl %r12d,%r10d // d+=T1 + addl %r12d,%ecx // h+=T1 - leaq 4(%rbp),%rbp + leaq 4(%rbp),%rbp // round++ movl 60(%rsp),%r13d movl 48(%rsp),%r15d movl %r13d,%r12d rorl $11,%r13d - addl %r14d,%ecx + addl %r14d,%ecx // modulo-scheduled h+=Sigma0(a) movl %r15d,%r14d rorl $2,%r15d @@ -1567,8 +1567,8 @@ L$rounds_16_xx: shrl $10,%r14d rorl $17,%r15d - xorl %r13d,%r12d - xorl %r14d,%r15d + xorl %r13d,%r12d // sigma0(X[(i+1)&0xf]) + xorl %r14d,%r15d // sigma1(X[(i+14)&0xf]) addl 28(%rsp),%r12d addl 56(%rsp),%r12d @@ -1580,43 +1580,43 @@ L$rounds_16_xx: xorl %r10d,%r13d rorl $9,%r14d - xorl %eax,%r15d + xorl %eax,%r15d // f^g movl %r12d,56(%rsp) xorl %ecx,%r14d - andl %r10d,%r15d + andl %r10d,%r15d // (f^g)&e rorl $5,%r13d - addl %ebx,%r12d - xorl %eax,%r15d + addl %ebx,%r12d // T1+=h + xorl %eax,%r15d // Ch(e,f,g)=((f^g)&e)^g rorl $11,%r14d xorl %r10d,%r13d - addl %r15d,%r12d + addl %r15d,%r12d // T1+=Ch(e,f,g) movl %ecx,%r15d - addl (%rbp),%r12d + addl (%rbp),%r12d // T1+=K[round] xorl %ecx,%r14d - xorl %edx,%r15d - rorl $6,%r13d + xorl %edx,%r15d // a^b, b^c in next round + rorl $6,%r13d // Sigma1(e) movl %edx,%ebx andl %r15d,%edi - rorl $2,%r14d - addl %r13d,%r12d + rorl $2,%r14d // Sigma0(a) + addl %r13d,%r12d // T1+=Sigma1(e) - xorl %edi,%ebx - addl %r12d,%r9d - addl %r12d,%ebx + xorl %edi,%ebx // h=Maj(a,b,c)=Ch(a^b,c,b) + addl %r12d,%r9d // d+=T1 + addl %r12d,%ebx // h+=T1 - leaq 4(%rbp),%rbp + leaq 4(%rbp),%rbp // round++ movl 0(%rsp),%r13d movl 52(%rsp),%edi movl %r13d,%r12d rorl $11,%r13d - addl %r14d,%ebx + addl %r14d,%ebx // modulo-scheduled h+=Sigma0(a) movl %edi,%r14d rorl $2,%edi @@ -1627,8 +1627,8 @@ L$rounds_16_xx: shrl $10,%r14d rorl $17,%edi - xorl %r13d,%r12d - xorl %r14d,%edi + xorl %r13d,%r12d // sigma0(X[(i+1)&0xf]) + xorl %r14d,%edi // sigma1(X[(i+14)&0xf]) addl 32(%rsp),%r12d addl 60(%rsp),%r12d @@ -1640,42 +1640,42 @@ L$rounds_16_xx: xorl %r9d,%r13d rorl $9,%r14d - xorl %r11d,%edi + xorl %r11d,%edi // f^g movl %r12d,60(%rsp) xorl %ebx,%r14d - andl %r9d,%edi + andl %r9d,%edi // (f^g)&e rorl $5,%r13d - addl %eax,%r12d - xorl %r11d,%edi + addl %eax,%r12d // T1+=h + xorl %r11d,%edi // Ch(e,f,g)=((f^g)&e)^g rorl $11,%r14d xorl %r9d,%r13d - addl %edi,%r12d + addl %edi,%r12d // T1+=Ch(e,f,g) movl %ebx,%edi - addl (%rbp),%r12d + addl (%rbp),%r12d // T1+=K[round] xorl %ebx,%r14d - xorl %ecx,%edi - rorl $6,%r13d + xorl %ecx,%edi // a^b, b^c in next round + rorl $6,%r13d // Sigma1(e) movl %ecx,%eax andl %edi,%r15d - rorl $2,%r14d - addl %r13d,%r12d + rorl $2,%r14d // Sigma0(a) + addl %r13d,%r12d // T1+=Sigma1(e) - xorl %r15d,%eax - addl %r12d,%r8d - addl %r12d,%eax + xorl %r15d,%eax // h=Maj(a,b,c)=Ch(a^b,c,b) + addl %r12d,%r8d // d+=T1 + addl %r12d,%eax // h+=T1 - leaq 20(%rbp),%rbp + leaq 20(%rbp),%rbp // round++ cmpb $0,3(%rbp) jnz L$rounds_16_xx movq 64+0(%rsp),%rdi - addl %r14d,%eax + addl %r14d,%eax // modulo-scheduled h+=Sigma0(a) leaq 64(%rsi),%rsi addl 0(%rdi),%eax @@ -1768,20 +1768,21 @@ K256: .private_extern _sha256_block_data_order_hw .p2align 6 +.alt_entry _sha256_block_data_order_hw _sha256_block_data_order_hw: _CET_ENDBR leaq K256+128(%rip),%rcx - movdqu (%rdi),%xmm1 - movdqu 16(%rdi),%xmm2 - movdqa 512-128(%rcx),%xmm7 - - pshufd $0x1b,%xmm1,%xmm0 - pshufd $0xb1,%xmm1,%xmm1 - pshufd $0x1b,%xmm2,%xmm2 - movdqa %xmm7,%xmm8 - palignr $8,%xmm2,%xmm1 - punpcklqdq %xmm0,%xmm2 + movdqu (%rdi),%xmm1 // DCBA + movdqu 16(%rdi),%xmm2 // HGFE + movdqa 512-128(%rcx),%xmm7 // byte swap mask + + pshufd $0x1b,%xmm1,%xmm0 // ABCD + pshufd $0xb1,%xmm1,%xmm1 // CDAB + pshufd $0x1b,%xmm2,%xmm2 // EFGH + movdqa %xmm7,%xmm8 // offload + palignr $8,%xmm2,%xmm1 // ABEF + punpcklqdq %xmm0,%xmm2 // CDGH jmp L$oop_shaext .p2align 4 @@ -1795,17 +1796,17 @@ L$oop_shaext: movdqa 0-128(%rcx),%xmm0 paddd %xmm3,%xmm0 pshufb %xmm7,%xmm4 - movdqa %xmm2,%xmm10 - sha256rnds2 %xmm1,%xmm2 + movdqa %xmm2,%xmm10 // offload + sha256rnds2 %xmm1,%xmm2 // 0-3 pshufd $0x0e,%xmm0,%xmm0 nop - movdqa %xmm1,%xmm9 + movdqa %xmm1,%xmm9 // offload sha256rnds2 %xmm2,%xmm1 movdqa 32-128(%rcx),%xmm0 paddd %xmm4,%xmm0 pshufb %xmm7,%xmm5 - sha256rnds2 %xmm1,%xmm2 + sha256rnds2 %xmm1,%xmm2 // 4-7 pshufd $0x0e,%xmm0,%xmm0 leaq 64(%rsi),%rsi sha256msg1 %xmm4,%xmm3 @@ -1814,7 +1815,7 @@ L$oop_shaext: movdqa 64-128(%rcx),%xmm0 paddd %xmm5,%xmm0 pshufb %xmm7,%xmm6 - sha256rnds2 %xmm1,%xmm2 + sha256rnds2 %xmm1,%xmm2 // 8-11 pshufd $0x0e,%xmm0,%xmm0 movdqa %xmm6,%xmm7 palignr $4,%xmm5,%xmm7 @@ -1826,7 +1827,7 @@ L$oop_shaext: movdqa 96-128(%rcx),%xmm0 paddd %xmm6,%xmm0 sha256msg2 %xmm6,%xmm3 - sha256rnds2 %xmm1,%xmm2 + sha256rnds2 %xmm1,%xmm2 // 12-15 pshufd $0x0e,%xmm0,%xmm0 movdqa %xmm3,%xmm7 palignr $4,%xmm6,%xmm7 @@ -1837,7 +1838,7 @@ L$oop_shaext: movdqa 128-128(%rcx),%xmm0 paddd %xmm3,%xmm0 sha256msg2 %xmm3,%xmm4 - sha256rnds2 %xmm1,%xmm2 + sha256rnds2 %xmm1,%xmm2 // 16-19... pshufd $0x0e,%xmm0,%xmm0 movdqa %xmm4,%xmm7 palignr $4,%xmm3,%xmm7 @@ -1848,7 +1849,7 @@ L$oop_shaext: movdqa 160-128(%rcx),%xmm0 paddd %xmm4,%xmm0 sha256msg2 %xmm4,%xmm5 - sha256rnds2 %xmm1,%xmm2 + sha256rnds2 %xmm1,%xmm2 // 16-19... pshufd $0x0e,%xmm0,%xmm0 movdqa %xmm5,%xmm7 palignr $4,%xmm4,%xmm7 @@ -1859,7 +1860,7 @@ L$oop_shaext: movdqa 192-128(%rcx),%xmm0 paddd %xmm5,%xmm0 sha256msg2 %xmm5,%xmm6 - sha256rnds2 %xmm1,%xmm2 + sha256rnds2 %xmm1,%xmm2 // 16-19... pshufd $0x0e,%xmm0,%xmm0 movdqa %xmm6,%xmm7 palignr $4,%xmm5,%xmm7 @@ -1870,7 +1871,7 @@ L$oop_shaext: movdqa 224-128(%rcx),%xmm0 paddd %xmm6,%xmm0 sha256msg2 %xmm6,%xmm3 - sha256rnds2 %xmm1,%xmm2 + sha256rnds2 %xmm1,%xmm2 // 16-19... pshufd $0x0e,%xmm0,%xmm0 movdqa %xmm3,%xmm7 palignr $4,%xmm6,%xmm7 @@ -1881,7 +1882,7 @@ L$oop_shaext: movdqa 256-128(%rcx),%xmm0 paddd %xmm3,%xmm0 sha256msg2 %xmm3,%xmm4 - sha256rnds2 %xmm1,%xmm2 + sha256rnds2 %xmm1,%xmm2 // 16-19... pshufd $0x0e,%xmm0,%xmm0 movdqa %xmm4,%xmm7 palignr $4,%xmm3,%xmm7 @@ -1892,7 +1893,7 @@ L$oop_shaext: movdqa 288-128(%rcx),%xmm0 paddd %xmm4,%xmm0 sha256msg2 %xmm4,%xmm5 - sha256rnds2 %xmm1,%xmm2 + sha256rnds2 %xmm1,%xmm2 // 16-19... pshufd $0x0e,%xmm0,%xmm0 movdqa %xmm5,%xmm7 palignr $4,%xmm4,%xmm7 @@ -1903,7 +1904,7 @@ L$oop_shaext: movdqa 320-128(%rcx),%xmm0 paddd %xmm5,%xmm0 sha256msg2 %xmm5,%xmm6 - sha256rnds2 %xmm1,%xmm2 + sha256rnds2 %xmm1,%xmm2 // 16-19... pshufd $0x0e,%xmm0,%xmm0 movdqa %xmm6,%xmm7 palignr $4,%xmm5,%xmm7 @@ -1914,7 +1915,7 @@ L$oop_shaext: movdqa 352-128(%rcx),%xmm0 paddd %xmm6,%xmm0 sha256msg2 %xmm6,%xmm3 - sha256rnds2 %xmm1,%xmm2 + sha256rnds2 %xmm1,%xmm2 // 16-19... pshufd $0x0e,%xmm0,%xmm0 movdqa %xmm3,%xmm7 palignr $4,%xmm6,%xmm7 @@ -1925,7 +1926,7 @@ L$oop_shaext: movdqa 384-128(%rcx),%xmm0 paddd %xmm3,%xmm0 sha256msg2 %xmm3,%xmm4 - sha256rnds2 %xmm1,%xmm2 + sha256rnds2 %xmm1,%xmm2 // 16-19... pshufd $0x0e,%xmm0,%xmm0 movdqa %xmm4,%xmm7 palignr $4,%xmm3,%xmm7 @@ -1936,7 +1937,7 @@ L$oop_shaext: movdqa 416-128(%rcx),%xmm0 paddd %xmm4,%xmm0 sha256msg2 %xmm4,%xmm5 - sha256rnds2 %xmm1,%xmm2 + sha256rnds2 %xmm1,%xmm2 // 52-55 pshufd $0x0e,%xmm0,%xmm0 movdqa %xmm5,%xmm7 palignr $4,%xmm4,%xmm7 @@ -1945,7 +1946,7 @@ L$oop_shaext: movdqa 448-128(%rcx),%xmm0 paddd %xmm5,%xmm0 - sha256rnds2 %xmm1,%xmm2 + sha256rnds2 %xmm1,%xmm2 // 56-59 pshufd $0x0e,%xmm0,%xmm0 sha256msg2 %xmm5,%xmm6 movdqa %xmm8,%xmm7 @@ -1954,7 +1955,7 @@ L$oop_shaext: movdqa 480-128(%rcx),%xmm0 paddd %xmm6,%xmm0 nop - sha256rnds2 %xmm1,%xmm2 + sha256rnds2 %xmm1,%xmm2 // 60-63 pshufd $0x0e,%xmm0,%xmm0 decq %rdx nop @@ -1964,11 +1965,11 @@ L$oop_shaext: paddd %xmm9,%xmm1 jnz L$oop_shaext - pshufd $0xb1,%xmm2,%xmm2 - pshufd $0x1b,%xmm1,%xmm7 - pshufd $0xb1,%xmm1,%xmm1 - punpckhqdq %xmm2,%xmm1 - palignr $8,%xmm7,%xmm2 + pshufd $0xb1,%xmm2,%xmm2 // DCHG + pshufd $0x1b,%xmm1,%xmm7 // FEBA + pshufd $0xb1,%xmm1,%xmm1 // BAFE + punpckhqdq %xmm2,%xmm1 // DCBA + palignr $8,%xmm7,%xmm2 // HGFE movdqu %xmm1,(%rdi) movdqu %xmm2,16(%rdi) @@ -1979,10 +1980,11 @@ L$oop_shaext: .private_extern _sha256_block_data_order_ssse3 .p2align 6 +.alt_entry _sha256_block_data_order_ssse3 _sha256_block_data_order_ssse3: _CET_ENDBR - movq %rsp,%rax + movq %rsp,%rax // copy %rsp pushq %rbx @@ -1996,14 +1998,14 @@ _CET_ENDBR pushq %r15 - shlq $4,%rdx + shlq $4,%rdx // num*16 subq $96,%rsp - leaq (%rsi,%rdx,4),%rdx - andq $-64,%rsp - movq %rdi,64+0(%rsp) - movq %rsi,64+8(%rsp) - movq %rdx,64+16(%rsp) - movq %rax,88(%rsp) + leaq (%rsi,%rdx,4),%rdx // inp+num*16*4 + andq $-64,%rsp // align stack frame + movq %rdi,64+0(%rsp) // save ctx, 1st arg + movq %rsi,64+8(%rsp) // save inp, 2nd arh + movq %rdx,64+16(%rsp) // save end pointer, "3rd" arg + movq %rax,88(%rsp) // save copy of %rsp L$prologue_ssse3: @@ -2015,8 +2017,8 @@ L$prologue_ssse3: movl 20(%rdi),%r9d movl 24(%rdi),%r10d movl 28(%rdi),%r11d - - +// movdqa K256+512+32(%rip),%xmm8 +// movdqa K256+512+64(%rip),%xmm9 jmp L$loop_ssse3 .p2align 4 L$loop_ssse3: @@ -2043,14 +2045,14 @@ L$loop_ssse3: movdqa %xmm5,16(%rsp) movl %ebx,%edi movdqa %xmm6,32(%rsp) - xorl %ecx,%edi + xorl %ecx,%edi // magic movdqa %xmm7,48(%rsp) movl %r8d,%r13d jmp L$ssse3_00_47 .p2align 4 L$ssse3_00_47: - subq $-128,%rbp + subq $-128,%rbp // size optimization rorl $14,%r13d movdqa %xmm1,%xmm4 movl %r14d,%eax @@ -3094,10 +3096,11 @@ L$epilogue_ssse3: .private_extern _sha256_block_data_order_avx .p2align 6 +.alt_entry _sha256_block_data_order_avx _sha256_block_data_order_avx: _CET_ENDBR - movq %rsp,%rax + movq %rsp,%rax // copy %rsp pushq %rbx @@ -3111,14 +3114,14 @@ _CET_ENDBR pushq %r15 - shlq $4,%rdx + shlq $4,%rdx // num*16 subq $96,%rsp - leaq (%rsi,%rdx,4),%rdx - andq $-64,%rsp - movq %rdi,64+0(%rsp) - movq %rsi,64+8(%rsp) - movq %rdx,64+16(%rsp) - movq %rax,88(%rsp) + leaq (%rsi,%rdx,4),%rdx // inp+num*16*4 + andq $-64,%rsp // align stack frame + movq %rdi,64+0(%rsp) // save ctx, 1st arg + movq %rsi,64+8(%rsp) // save inp, 2nd arh + movq %rdx,64+16(%rsp) // save end pointer, "3rd" arg + movq %rax,88(%rsp) // save copy of %rsp L$prologue_avx: @@ -3155,14 +3158,14 @@ L$loop_avx: vmovdqa %xmm5,16(%rsp) movl %ebx,%edi vmovdqa %xmm6,32(%rsp) - xorl %ecx,%edi + xorl %ecx,%edi // magic vmovdqa %xmm7,48(%rsp) movl %r8d,%r13d jmp L$avx_00_47 .p2align 4 L$avx_00_47: - subq $-128,%rbp + subq $-128,%rbp // size optimization vpalignr $4,%xmm0,%xmm1,%xmm4 shrdl $14,%r13d,%r13d movl %r14d,%eax diff --git a/third_party/boringssl/gen/bcm/sha256-x86_64-linux.S b/third_party/boringssl/gen/bcm/sha256-x86_64-linux.S index 938f53165..6bd2b1428 100644 --- a/third_party/boringssl/gen/bcm/sha256-x86_64-linux.S +++ b/third_party/boringssl/gen/bcm/sha256-x86_64-linux.S @@ -13,7 +13,7 @@ sha256_block_data_order_nohw: .cfi_startproc _CET_ENDBR - movq %rsp,%rax + movq %rsp,%rax // copy %rsp .cfi_def_cfa_register %rax pushq %rbx .cfi_offset %rbx,-16 @@ -27,14 +27,14 @@ _CET_ENDBR .cfi_offset %r14,-48 pushq %r15 .cfi_offset %r15,-56 - shlq $4,%rdx + shlq $4,%rdx // num*16 subq $64+32,%rsp - leaq (%rsi,%rdx,4),%rdx - andq $-64,%rsp - movq %rdi,64+0(%rsp) - movq %rsi,64+8(%rsp) - movq %rdx,64+16(%rsp) - movq %rax,88(%rsp) + leaq (%rsi,%rdx,4),%rdx // inp+num*16*4 + andq $-64,%rsp // align stack frame + movq %rdi,64+0(%rsp) // save ctx, 1st arg + movq %rsi,64+8(%rsp) // save inp, 2nd arh + movq %rdx,64+16(%rsp) // save end pointer, "3rd" arg + movq %rax,88(%rsp) // save copy of %rsp .cfi_escape 0x0f,0x06,0x77,0xd8,0x00,0x06,0x23,0x08 .Lprologue: @@ -52,7 +52,7 @@ _CET_ENDBR .Lloop: movl %ebx,%edi leaq K256(%rip),%rbp - xorl %ecx,%edi + xorl %ecx,%edi // magic movl 0(%rsi),%r12d movl %r8d,%r13d movl %eax,%r14d @@ -62,38 +62,38 @@ _CET_ENDBR xorl %r8d,%r13d rorl $9,%r14d - xorl %r10d,%r15d + xorl %r10d,%r15d // f^g movl %r12d,0(%rsp) xorl %eax,%r14d - andl %r8d,%r15d + andl %r8d,%r15d // (f^g)&e rorl $5,%r13d - addl %r11d,%r12d - xorl %r10d,%r15d + addl %r11d,%r12d // T1+=h + xorl %r10d,%r15d // Ch(e,f,g)=((f^g)&e)^g rorl $11,%r14d xorl %r8d,%r13d - addl %r15d,%r12d + addl %r15d,%r12d // T1+=Ch(e,f,g) movl %eax,%r15d - addl (%rbp),%r12d + addl (%rbp),%r12d // T1+=K[round] xorl %eax,%r14d - xorl %ebx,%r15d - rorl $6,%r13d + xorl %ebx,%r15d // a^b, b^c in next round + rorl $6,%r13d // Sigma1(e) movl %ebx,%r11d andl %r15d,%edi - rorl $2,%r14d - addl %r13d,%r12d + rorl $2,%r14d // Sigma0(a) + addl %r13d,%r12d // T1+=Sigma1(e) - xorl %edi,%r11d - addl %r12d,%edx - addl %r12d,%r11d + xorl %edi,%r11d // h=Maj(a,b,c)=Ch(a^b,c,b) + addl %r12d,%edx // d+=T1 + addl %r12d,%r11d // h+=T1 - leaq 4(%rbp),%rbp - addl %r14d,%r11d + leaq 4(%rbp),%rbp // round++ + addl %r14d,%r11d // h+=Sigma0(a) movl 4(%rsi),%r12d movl %edx,%r13d movl %r11d,%r14d @@ -103,38 +103,38 @@ _CET_ENDBR xorl %edx,%r13d rorl $9,%r14d - xorl %r9d,%edi + xorl %r9d,%edi // f^g movl %r12d,4(%rsp) xorl %r11d,%r14d - andl %edx,%edi + andl %edx,%edi // (f^g)&e rorl $5,%r13d - addl %r10d,%r12d - xorl %r9d,%edi + addl %r10d,%r12d // T1+=h + xorl %r9d,%edi // Ch(e,f,g)=((f^g)&e)^g rorl $11,%r14d xorl %edx,%r13d - addl %edi,%r12d + addl %edi,%r12d // T1+=Ch(e,f,g) movl %r11d,%edi - addl (%rbp),%r12d + addl (%rbp),%r12d // T1+=K[round] xorl %r11d,%r14d - xorl %eax,%edi - rorl $6,%r13d + xorl %eax,%edi // a^b, b^c in next round + rorl $6,%r13d // Sigma1(e) movl %eax,%r10d andl %edi,%r15d - rorl $2,%r14d - addl %r13d,%r12d + rorl $2,%r14d // Sigma0(a) + addl %r13d,%r12d // T1+=Sigma1(e) - xorl %r15d,%r10d - addl %r12d,%ecx - addl %r12d,%r10d + xorl %r15d,%r10d // h=Maj(a,b,c)=Ch(a^b,c,b) + addl %r12d,%ecx // d+=T1 + addl %r12d,%r10d // h+=T1 - leaq 4(%rbp),%rbp - addl %r14d,%r10d + leaq 4(%rbp),%rbp // round++ + addl %r14d,%r10d // h+=Sigma0(a) movl 8(%rsi),%r12d movl %ecx,%r13d movl %r10d,%r14d @@ -144,38 +144,38 @@ _CET_ENDBR xorl %ecx,%r13d rorl $9,%r14d - xorl %r8d,%r15d + xorl %r8d,%r15d // f^g movl %r12d,8(%rsp) xorl %r10d,%r14d - andl %ecx,%r15d + andl %ecx,%r15d // (f^g)&e rorl $5,%r13d - addl %r9d,%r12d - xorl %r8d,%r15d + addl %r9d,%r12d // T1+=h + xorl %r8d,%r15d // Ch(e,f,g)=((f^g)&e)^g rorl $11,%r14d xorl %ecx,%r13d - addl %r15d,%r12d + addl %r15d,%r12d // T1+=Ch(e,f,g) movl %r10d,%r15d - addl (%rbp),%r12d + addl (%rbp),%r12d // T1+=K[round] xorl %r10d,%r14d - xorl %r11d,%r15d - rorl $6,%r13d + xorl %r11d,%r15d // a^b, b^c in next round + rorl $6,%r13d // Sigma1(e) movl %r11d,%r9d andl %r15d,%edi - rorl $2,%r14d - addl %r13d,%r12d + rorl $2,%r14d // Sigma0(a) + addl %r13d,%r12d // T1+=Sigma1(e) - xorl %edi,%r9d - addl %r12d,%ebx - addl %r12d,%r9d + xorl %edi,%r9d // h=Maj(a,b,c)=Ch(a^b,c,b) + addl %r12d,%ebx // d+=T1 + addl %r12d,%r9d // h+=T1 - leaq 4(%rbp),%rbp - addl %r14d,%r9d + leaq 4(%rbp),%rbp // round++ + addl %r14d,%r9d // h+=Sigma0(a) movl 12(%rsi),%r12d movl %ebx,%r13d movl %r9d,%r14d @@ -185,38 +185,38 @@ _CET_ENDBR xorl %ebx,%r13d rorl $9,%r14d - xorl %edx,%edi + xorl %edx,%edi // f^g movl %r12d,12(%rsp) xorl %r9d,%r14d - andl %ebx,%edi + andl %ebx,%edi // (f^g)&e rorl $5,%r13d - addl %r8d,%r12d - xorl %edx,%edi + addl %r8d,%r12d // T1+=h + xorl %edx,%edi // Ch(e,f,g)=((f^g)&e)^g rorl $11,%r14d xorl %ebx,%r13d - addl %edi,%r12d + addl %edi,%r12d // T1+=Ch(e,f,g) movl %r9d,%edi - addl (%rbp),%r12d + addl (%rbp),%r12d // T1+=K[round] xorl %r9d,%r14d - xorl %r10d,%edi - rorl $6,%r13d + xorl %r10d,%edi // a^b, b^c in next round + rorl $6,%r13d // Sigma1(e) movl %r10d,%r8d andl %edi,%r15d - rorl $2,%r14d - addl %r13d,%r12d + rorl $2,%r14d // Sigma0(a) + addl %r13d,%r12d // T1+=Sigma1(e) - xorl %r15d,%r8d - addl %r12d,%eax - addl %r12d,%r8d + xorl %r15d,%r8d // h=Maj(a,b,c)=Ch(a^b,c,b) + addl %r12d,%eax // d+=T1 + addl %r12d,%r8d // h+=T1 - leaq 20(%rbp),%rbp - addl %r14d,%r8d + leaq 20(%rbp),%rbp // round++ + addl %r14d,%r8d // h+=Sigma0(a) movl 16(%rsi),%r12d movl %eax,%r13d movl %r8d,%r14d @@ -226,38 +226,38 @@ _CET_ENDBR xorl %eax,%r13d rorl $9,%r14d - xorl %ecx,%r15d + xorl %ecx,%r15d // f^g movl %r12d,16(%rsp) xorl %r8d,%r14d - andl %eax,%r15d + andl %eax,%r15d // (f^g)&e rorl $5,%r13d - addl %edx,%r12d - xorl %ecx,%r15d + addl %edx,%r12d // T1+=h + xorl %ecx,%r15d // Ch(e,f,g)=((f^g)&e)^g rorl $11,%r14d xorl %eax,%r13d - addl %r15d,%r12d + addl %r15d,%r12d // T1+=Ch(e,f,g) movl %r8d,%r15d - addl (%rbp),%r12d + addl (%rbp),%r12d // T1+=K[round] xorl %r8d,%r14d - xorl %r9d,%r15d - rorl $6,%r13d + xorl %r9d,%r15d // a^b, b^c in next round + rorl $6,%r13d // Sigma1(e) movl %r9d,%edx andl %r15d,%edi - rorl $2,%r14d - addl %r13d,%r12d + rorl $2,%r14d // Sigma0(a) + addl %r13d,%r12d // T1+=Sigma1(e) - xorl %edi,%edx - addl %r12d,%r11d - addl %r12d,%edx + xorl %edi,%edx // h=Maj(a,b,c)=Ch(a^b,c,b) + addl %r12d,%r11d // d+=T1 + addl %r12d,%edx // h+=T1 - leaq 4(%rbp),%rbp - addl %r14d,%edx + leaq 4(%rbp),%rbp // round++ + addl %r14d,%edx // h+=Sigma0(a) movl 20(%rsi),%r12d movl %r11d,%r13d movl %edx,%r14d @@ -267,38 +267,38 @@ _CET_ENDBR xorl %r11d,%r13d rorl $9,%r14d - xorl %ebx,%edi + xorl %ebx,%edi // f^g movl %r12d,20(%rsp) xorl %edx,%r14d - andl %r11d,%edi + andl %r11d,%edi // (f^g)&e rorl $5,%r13d - addl %ecx,%r12d - xorl %ebx,%edi + addl %ecx,%r12d // T1+=h + xorl %ebx,%edi // Ch(e,f,g)=((f^g)&e)^g rorl $11,%r14d xorl %r11d,%r13d - addl %edi,%r12d + addl %edi,%r12d // T1+=Ch(e,f,g) movl %edx,%edi - addl (%rbp),%r12d + addl (%rbp),%r12d // T1+=K[round] xorl %edx,%r14d - xorl %r8d,%edi - rorl $6,%r13d + xorl %r8d,%edi // a^b, b^c in next round + rorl $6,%r13d // Sigma1(e) movl %r8d,%ecx andl %edi,%r15d - rorl $2,%r14d - addl %r13d,%r12d + rorl $2,%r14d // Sigma0(a) + addl %r13d,%r12d // T1+=Sigma1(e) - xorl %r15d,%ecx - addl %r12d,%r10d - addl %r12d,%ecx + xorl %r15d,%ecx // h=Maj(a,b,c)=Ch(a^b,c,b) + addl %r12d,%r10d // d+=T1 + addl %r12d,%ecx // h+=T1 - leaq 4(%rbp),%rbp - addl %r14d,%ecx + leaq 4(%rbp),%rbp // round++ + addl %r14d,%ecx // h+=Sigma0(a) movl 24(%rsi),%r12d movl %r10d,%r13d movl %ecx,%r14d @@ -308,38 +308,38 @@ _CET_ENDBR xorl %r10d,%r13d rorl $9,%r14d - xorl %eax,%r15d + xorl %eax,%r15d // f^g movl %r12d,24(%rsp) xorl %ecx,%r14d - andl %r10d,%r15d + andl %r10d,%r15d // (f^g)&e rorl $5,%r13d - addl %ebx,%r12d - xorl %eax,%r15d + addl %ebx,%r12d // T1+=h + xorl %eax,%r15d // Ch(e,f,g)=((f^g)&e)^g rorl $11,%r14d xorl %r10d,%r13d - addl %r15d,%r12d + addl %r15d,%r12d // T1+=Ch(e,f,g) movl %ecx,%r15d - addl (%rbp),%r12d + addl (%rbp),%r12d // T1+=K[round] xorl %ecx,%r14d - xorl %edx,%r15d - rorl $6,%r13d + xorl %edx,%r15d // a^b, b^c in next round + rorl $6,%r13d // Sigma1(e) movl %edx,%ebx andl %r15d,%edi - rorl $2,%r14d - addl %r13d,%r12d + rorl $2,%r14d // Sigma0(a) + addl %r13d,%r12d // T1+=Sigma1(e) - xorl %edi,%ebx - addl %r12d,%r9d - addl %r12d,%ebx + xorl %edi,%ebx // h=Maj(a,b,c)=Ch(a^b,c,b) + addl %r12d,%r9d // d+=T1 + addl %r12d,%ebx // h+=T1 - leaq 4(%rbp),%rbp - addl %r14d,%ebx + leaq 4(%rbp),%rbp // round++ + addl %r14d,%ebx // h+=Sigma0(a) movl 28(%rsi),%r12d movl %r9d,%r13d movl %ebx,%r14d @@ -349,38 +349,38 @@ _CET_ENDBR xorl %r9d,%r13d rorl $9,%r14d - xorl %r11d,%edi + xorl %r11d,%edi // f^g movl %r12d,28(%rsp) xorl %ebx,%r14d - andl %r9d,%edi + andl %r9d,%edi // (f^g)&e rorl $5,%r13d - addl %eax,%r12d - xorl %r11d,%edi + addl %eax,%r12d // T1+=h + xorl %r11d,%edi // Ch(e,f,g)=((f^g)&e)^g rorl $11,%r14d xorl %r9d,%r13d - addl %edi,%r12d + addl %edi,%r12d // T1+=Ch(e,f,g) movl %ebx,%edi - addl (%rbp),%r12d + addl (%rbp),%r12d // T1+=K[round] xorl %ebx,%r14d - xorl %ecx,%edi - rorl $6,%r13d + xorl %ecx,%edi // a^b, b^c in next round + rorl $6,%r13d // Sigma1(e) movl %ecx,%eax andl %edi,%r15d - rorl $2,%r14d - addl %r13d,%r12d + rorl $2,%r14d // Sigma0(a) + addl %r13d,%r12d // T1+=Sigma1(e) - xorl %r15d,%eax - addl %r12d,%r8d - addl %r12d,%eax + xorl %r15d,%eax // h=Maj(a,b,c)=Ch(a^b,c,b) + addl %r12d,%r8d // d+=T1 + addl %r12d,%eax // h+=T1 - leaq 20(%rbp),%rbp - addl %r14d,%eax + leaq 20(%rbp),%rbp // round++ + addl %r14d,%eax // h+=Sigma0(a) movl 32(%rsi),%r12d movl %r8d,%r13d movl %eax,%r14d @@ -390,38 +390,38 @@ _CET_ENDBR xorl %r8d,%r13d rorl $9,%r14d - xorl %r10d,%r15d + xorl %r10d,%r15d // f^g movl %r12d,32(%rsp) xorl %eax,%r14d - andl %r8d,%r15d + andl %r8d,%r15d // (f^g)&e rorl $5,%r13d - addl %r11d,%r12d - xorl %r10d,%r15d + addl %r11d,%r12d // T1+=h + xorl %r10d,%r15d // Ch(e,f,g)=((f^g)&e)^g rorl $11,%r14d xorl %r8d,%r13d - addl %r15d,%r12d + addl %r15d,%r12d // T1+=Ch(e,f,g) movl %eax,%r15d - addl (%rbp),%r12d + addl (%rbp),%r12d // T1+=K[round] xorl %eax,%r14d - xorl %ebx,%r15d - rorl $6,%r13d + xorl %ebx,%r15d // a^b, b^c in next round + rorl $6,%r13d // Sigma1(e) movl %ebx,%r11d andl %r15d,%edi - rorl $2,%r14d - addl %r13d,%r12d + rorl $2,%r14d // Sigma0(a) + addl %r13d,%r12d // T1+=Sigma1(e) - xorl %edi,%r11d - addl %r12d,%edx - addl %r12d,%r11d + xorl %edi,%r11d // h=Maj(a,b,c)=Ch(a^b,c,b) + addl %r12d,%edx // d+=T1 + addl %r12d,%r11d // h+=T1 - leaq 4(%rbp),%rbp - addl %r14d,%r11d + leaq 4(%rbp),%rbp // round++ + addl %r14d,%r11d // h+=Sigma0(a) movl 36(%rsi),%r12d movl %edx,%r13d movl %r11d,%r14d @@ -431,38 +431,38 @@ _CET_ENDBR xorl %edx,%r13d rorl $9,%r14d - xorl %r9d,%edi + xorl %r9d,%edi // f^g movl %r12d,36(%rsp) xorl %r11d,%r14d - andl %edx,%edi + andl %edx,%edi // (f^g)&e rorl $5,%r13d - addl %r10d,%r12d - xorl %r9d,%edi + addl %r10d,%r12d // T1+=h + xorl %r9d,%edi // Ch(e,f,g)=((f^g)&e)^g rorl $11,%r14d xorl %edx,%r13d - addl %edi,%r12d + addl %edi,%r12d // T1+=Ch(e,f,g) movl %r11d,%edi - addl (%rbp),%r12d + addl (%rbp),%r12d // T1+=K[round] xorl %r11d,%r14d - xorl %eax,%edi - rorl $6,%r13d + xorl %eax,%edi // a^b, b^c in next round + rorl $6,%r13d // Sigma1(e) movl %eax,%r10d andl %edi,%r15d - rorl $2,%r14d - addl %r13d,%r12d + rorl $2,%r14d // Sigma0(a) + addl %r13d,%r12d // T1+=Sigma1(e) - xorl %r15d,%r10d - addl %r12d,%ecx - addl %r12d,%r10d + xorl %r15d,%r10d // h=Maj(a,b,c)=Ch(a^b,c,b) + addl %r12d,%ecx // d+=T1 + addl %r12d,%r10d // h+=T1 - leaq 4(%rbp),%rbp - addl %r14d,%r10d + leaq 4(%rbp),%rbp // round++ + addl %r14d,%r10d // h+=Sigma0(a) movl 40(%rsi),%r12d movl %ecx,%r13d movl %r10d,%r14d @@ -472,38 +472,38 @@ _CET_ENDBR xorl %ecx,%r13d rorl $9,%r14d - xorl %r8d,%r15d + xorl %r8d,%r15d // f^g movl %r12d,40(%rsp) xorl %r10d,%r14d - andl %ecx,%r15d + andl %ecx,%r15d // (f^g)&e rorl $5,%r13d - addl %r9d,%r12d - xorl %r8d,%r15d + addl %r9d,%r12d // T1+=h + xorl %r8d,%r15d // Ch(e,f,g)=((f^g)&e)^g rorl $11,%r14d xorl %ecx,%r13d - addl %r15d,%r12d + addl %r15d,%r12d // T1+=Ch(e,f,g) movl %r10d,%r15d - addl (%rbp),%r12d + addl (%rbp),%r12d // T1+=K[round] xorl %r10d,%r14d - xorl %r11d,%r15d - rorl $6,%r13d + xorl %r11d,%r15d // a^b, b^c in next round + rorl $6,%r13d // Sigma1(e) movl %r11d,%r9d andl %r15d,%edi - rorl $2,%r14d - addl %r13d,%r12d + rorl $2,%r14d // Sigma0(a) + addl %r13d,%r12d // T1+=Sigma1(e) - xorl %edi,%r9d - addl %r12d,%ebx - addl %r12d,%r9d + xorl %edi,%r9d // h=Maj(a,b,c)=Ch(a^b,c,b) + addl %r12d,%ebx // d+=T1 + addl %r12d,%r9d // h+=T1 - leaq 4(%rbp),%rbp - addl %r14d,%r9d + leaq 4(%rbp),%rbp // round++ + addl %r14d,%r9d // h+=Sigma0(a) movl 44(%rsi),%r12d movl %ebx,%r13d movl %r9d,%r14d @@ -513,38 +513,38 @@ _CET_ENDBR xorl %ebx,%r13d rorl $9,%r14d - xorl %edx,%edi + xorl %edx,%edi // f^g movl %r12d,44(%rsp) xorl %r9d,%r14d - andl %ebx,%edi + andl %ebx,%edi // (f^g)&e rorl $5,%r13d - addl %r8d,%r12d - xorl %edx,%edi + addl %r8d,%r12d // T1+=h + xorl %edx,%edi // Ch(e,f,g)=((f^g)&e)^g rorl $11,%r14d xorl %ebx,%r13d - addl %edi,%r12d + addl %edi,%r12d // T1+=Ch(e,f,g) movl %r9d,%edi - addl (%rbp),%r12d + addl (%rbp),%r12d // T1+=K[round] xorl %r9d,%r14d - xorl %r10d,%edi - rorl $6,%r13d + xorl %r10d,%edi // a^b, b^c in next round + rorl $6,%r13d // Sigma1(e) movl %r10d,%r8d andl %edi,%r15d - rorl $2,%r14d - addl %r13d,%r12d + rorl $2,%r14d // Sigma0(a) + addl %r13d,%r12d // T1+=Sigma1(e) - xorl %r15d,%r8d - addl %r12d,%eax - addl %r12d,%r8d + xorl %r15d,%r8d // h=Maj(a,b,c)=Ch(a^b,c,b) + addl %r12d,%eax // d+=T1 + addl %r12d,%r8d // h+=T1 - leaq 20(%rbp),%rbp - addl %r14d,%r8d + leaq 20(%rbp),%rbp // round++ + addl %r14d,%r8d // h+=Sigma0(a) movl 48(%rsi),%r12d movl %eax,%r13d movl %r8d,%r14d @@ -554,38 +554,38 @@ _CET_ENDBR xorl %eax,%r13d rorl $9,%r14d - xorl %ecx,%r15d + xorl %ecx,%r15d // f^g movl %r12d,48(%rsp) xorl %r8d,%r14d - andl %eax,%r15d + andl %eax,%r15d // (f^g)&e rorl $5,%r13d - addl %edx,%r12d - xorl %ecx,%r15d + addl %edx,%r12d // T1+=h + xorl %ecx,%r15d // Ch(e,f,g)=((f^g)&e)^g rorl $11,%r14d xorl %eax,%r13d - addl %r15d,%r12d + addl %r15d,%r12d // T1+=Ch(e,f,g) movl %r8d,%r15d - addl (%rbp),%r12d + addl (%rbp),%r12d // T1+=K[round] xorl %r8d,%r14d - xorl %r9d,%r15d - rorl $6,%r13d + xorl %r9d,%r15d // a^b, b^c in next round + rorl $6,%r13d // Sigma1(e) movl %r9d,%edx andl %r15d,%edi - rorl $2,%r14d - addl %r13d,%r12d + rorl $2,%r14d // Sigma0(a) + addl %r13d,%r12d // T1+=Sigma1(e) - xorl %edi,%edx - addl %r12d,%r11d - addl %r12d,%edx + xorl %edi,%edx // h=Maj(a,b,c)=Ch(a^b,c,b) + addl %r12d,%r11d // d+=T1 + addl %r12d,%edx // h+=T1 - leaq 4(%rbp),%rbp - addl %r14d,%edx + leaq 4(%rbp),%rbp // round++ + addl %r14d,%edx // h+=Sigma0(a) movl 52(%rsi),%r12d movl %r11d,%r13d movl %edx,%r14d @@ -595,38 +595,38 @@ _CET_ENDBR xorl %r11d,%r13d rorl $9,%r14d - xorl %ebx,%edi + xorl %ebx,%edi // f^g movl %r12d,52(%rsp) xorl %edx,%r14d - andl %r11d,%edi + andl %r11d,%edi // (f^g)&e rorl $5,%r13d - addl %ecx,%r12d - xorl %ebx,%edi + addl %ecx,%r12d // T1+=h + xorl %ebx,%edi // Ch(e,f,g)=((f^g)&e)^g rorl $11,%r14d xorl %r11d,%r13d - addl %edi,%r12d + addl %edi,%r12d // T1+=Ch(e,f,g) movl %edx,%edi - addl (%rbp),%r12d + addl (%rbp),%r12d // T1+=K[round] xorl %edx,%r14d - xorl %r8d,%edi - rorl $6,%r13d + xorl %r8d,%edi // a^b, b^c in next round + rorl $6,%r13d // Sigma1(e) movl %r8d,%ecx andl %edi,%r15d - rorl $2,%r14d - addl %r13d,%r12d + rorl $2,%r14d // Sigma0(a) + addl %r13d,%r12d // T1+=Sigma1(e) - xorl %r15d,%ecx - addl %r12d,%r10d - addl %r12d,%ecx + xorl %r15d,%ecx // h=Maj(a,b,c)=Ch(a^b,c,b) + addl %r12d,%r10d // d+=T1 + addl %r12d,%ecx // h+=T1 - leaq 4(%rbp),%rbp - addl %r14d,%ecx + leaq 4(%rbp),%rbp // round++ + addl %r14d,%ecx // h+=Sigma0(a) movl 56(%rsi),%r12d movl %r10d,%r13d movl %ecx,%r14d @@ -636,38 +636,38 @@ _CET_ENDBR xorl %r10d,%r13d rorl $9,%r14d - xorl %eax,%r15d + xorl %eax,%r15d // f^g movl %r12d,56(%rsp) xorl %ecx,%r14d - andl %r10d,%r15d + andl %r10d,%r15d // (f^g)&e rorl $5,%r13d - addl %ebx,%r12d - xorl %eax,%r15d + addl %ebx,%r12d // T1+=h + xorl %eax,%r15d // Ch(e,f,g)=((f^g)&e)^g rorl $11,%r14d xorl %r10d,%r13d - addl %r15d,%r12d + addl %r15d,%r12d // T1+=Ch(e,f,g) movl %ecx,%r15d - addl (%rbp),%r12d + addl (%rbp),%r12d // T1+=K[round] xorl %ecx,%r14d - xorl %edx,%r15d - rorl $6,%r13d + xorl %edx,%r15d // a^b, b^c in next round + rorl $6,%r13d // Sigma1(e) movl %edx,%ebx andl %r15d,%edi - rorl $2,%r14d - addl %r13d,%r12d + rorl $2,%r14d // Sigma0(a) + addl %r13d,%r12d // T1+=Sigma1(e) - xorl %edi,%ebx - addl %r12d,%r9d - addl %r12d,%ebx + xorl %edi,%ebx // h=Maj(a,b,c)=Ch(a^b,c,b) + addl %r12d,%r9d // d+=T1 + addl %r12d,%ebx // h+=T1 - leaq 4(%rbp),%rbp - addl %r14d,%ebx + leaq 4(%rbp),%rbp // round++ + addl %r14d,%ebx // h+=Sigma0(a) movl 60(%rsi),%r12d movl %r9d,%r13d movl %ebx,%r14d @@ -677,37 +677,37 @@ _CET_ENDBR xorl %r9d,%r13d rorl $9,%r14d - xorl %r11d,%edi + xorl %r11d,%edi // f^g movl %r12d,60(%rsp) xorl %ebx,%r14d - andl %r9d,%edi + andl %r9d,%edi // (f^g)&e rorl $5,%r13d - addl %eax,%r12d - xorl %r11d,%edi + addl %eax,%r12d // T1+=h + xorl %r11d,%edi // Ch(e,f,g)=((f^g)&e)^g rorl $11,%r14d xorl %r9d,%r13d - addl %edi,%r12d + addl %edi,%r12d // T1+=Ch(e,f,g) movl %ebx,%edi - addl (%rbp),%r12d + addl (%rbp),%r12d // T1+=K[round] xorl %ebx,%r14d - xorl %ecx,%edi - rorl $6,%r13d + xorl %ecx,%edi // a^b, b^c in next round + rorl $6,%r13d // Sigma1(e) movl %ecx,%eax andl %edi,%r15d - rorl $2,%r14d - addl %r13d,%r12d + rorl $2,%r14d // Sigma0(a) + addl %r13d,%r12d // T1+=Sigma1(e) - xorl %r15d,%eax - addl %r12d,%r8d - addl %r12d,%eax + xorl %r15d,%eax // h=Maj(a,b,c)=Ch(a^b,c,b) + addl %r12d,%r8d // d+=T1 + addl %r12d,%eax // h+=T1 - leaq 20(%rbp),%rbp + leaq 20(%rbp),%rbp // round++ jmp .Lrounds_16_xx .align 16 .Lrounds_16_xx: @@ -716,7 +716,7 @@ _CET_ENDBR movl %r13d,%r12d rorl $11,%r13d - addl %r14d,%eax + addl %r14d,%eax // modulo-scheduled h+=Sigma0(a) movl %r15d,%r14d rorl $2,%r15d @@ -727,8 +727,8 @@ _CET_ENDBR shrl $10,%r14d rorl $17,%r15d - xorl %r13d,%r12d - xorl %r14d,%r15d + xorl %r13d,%r12d // sigma0(X[(i+1)&0xf]) + xorl %r14d,%r15d // sigma1(X[(i+14)&0xf]) addl 36(%rsp),%r12d addl 0(%rsp),%r12d @@ -740,43 +740,43 @@ _CET_ENDBR xorl %r8d,%r13d rorl $9,%r14d - xorl %r10d,%r15d + xorl %r10d,%r15d // f^g movl %r12d,0(%rsp) xorl %eax,%r14d - andl %r8d,%r15d + andl %r8d,%r15d // (f^g)&e rorl $5,%r13d - addl %r11d,%r12d - xorl %r10d,%r15d + addl %r11d,%r12d // T1+=h + xorl %r10d,%r15d // Ch(e,f,g)=((f^g)&e)^g rorl $11,%r14d xorl %r8d,%r13d - addl %r15d,%r12d + addl %r15d,%r12d // T1+=Ch(e,f,g) movl %eax,%r15d - addl (%rbp),%r12d + addl (%rbp),%r12d // T1+=K[round] xorl %eax,%r14d - xorl %ebx,%r15d - rorl $6,%r13d + xorl %ebx,%r15d // a^b, b^c in next round + rorl $6,%r13d // Sigma1(e) movl %ebx,%r11d andl %r15d,%edi - rorl $2,%r14d - addl %r13d,%r12d + rorl $2,%r14d // Sigma0(a) + addl %r13d,%r12d // T1+=Sigma1(e) - xorl %edi,%r11d - addl %r12d,%edx - addl %r12d,%r11d + xorl %edi,%r11d // h=Maj(a,b,c)=Ch(a^b,c,b) + addl %r12d,%edx // d+=T1 + addl %r12d,%r11d // h+=T1 - leaq 4(%rbp),%rbp + leaq 4(%rbp),%rbp // round++ movl 8(%rsp),%r13d movl 60(%rsp),%edi movl %r13d,%r12d rorl $11,%r13d - addl %r14d,%r11d + addl %r14d,%r11d // modulo-scheduled h+=Sigma0(a) movl %edi,%r14d rorl $2,%edi @@ -787,8 +787,8 @@ _CET_ENDBR shrl $10,%r14d rorl $17,%edi - xorl %r13d,%r12d - xorl %r14d,%edi + xorl %r13d,%r12d // sigma0(X[(i+1)&0xf]) + xorl %r14d,%edi // sigma1(X[(i+14)&0xf]) addl 40(%rsp),%r12d addl 4(%rsp),%r12d @@ -800,43 +800,43 @@ _CET_ENDBR xorl %edx,%r13d rorl $9,%r14d - xorl %r9d,%edi + xorl %r9d,%edi // f^g movl %r12d,4(%rsp) xorl %r11d,%r14d - andl %edx,%edi + andl %edx,%edi // (f^g)&e rorl $5,%r13d - addl %r10d,%r12d - xorl %r9d,%edi + addl %r10d,%r12d // T1+=h + xorl %r9d,%edi // Ch(e,f,g)=((f^g)&e)^g rorl $11,%r14d xorl %edx,%r13d - addl %edi,%r12d + addl %edi,%r12d // T1+=Ch(e,f,g) movl %r11d,%edi - addl (%rbp),%r12d + addl (%rbp),%r12d // T1+=K[round] xorl %r11d,%r14d - xorl %eax,%edi - rorl $6,%r13d + xorl %eax,%edi // a^b, b^c in next round + rorl $6,%r13d // Sigma1(e) movl %eax,%r10d andl %edi,%r15d - rorl $2,%r14d - addl %r13d,%r12d + rorl $2,%r14d // Sigma0(a) + addl %r13d,%r12d // T1+=Sigma1(e) - xorl %r15d,%r10d - addl %r12d,%ecx - addl %r12d,%r10d + xorl %r15d,%r10d // h=Maj(a,b,c)=Ch(a^b,c,b) + addl %r12d,%ecx // d+=T1 + addl %r12d,%r10d // h+=T1 - leaq 4(%rbp),%rbp + leaq 4(%rbp),%rbp // round++ movl 12(%rsp),%r13d movl 0(%rsp),%r15d movl %r13d,%r12d rorl $11,%r13d - addl %r14d,%r10d + addl %r14d,%r10d // modulo-scheduled h+=Sigma0(a) movl %r15d,%r14d rorl $2,%r15d @@ -847,8 +847,8 @@ _CET_ENDBR shrl $10,%r14d rorl $17,%r15d - xorl %r13d,%r12d - xorl %r14d,%r15d + xorl %r13d,%r12d // sigma0(X[(i+1)&0xf]) + xorl %r14d,%r15d // sigma1(X[(i+14)&0xf]) addl 44(%rsp),%r12d addl 8(%rsp),%r12d @@ -860,43 +860,43 @@ _CET_ENDBR xorl %ecx,%r13d rorl $9,%r14d - xorl %r8d,%r15d + xorl %r8d,%r15d // f^g movl %r12d,8(%rsp) xorl %r10d,%r14d - andl %ecx,%r15d + andl %ecx,%r15d // (f^g)&e rorl $5,%r13d - addl %r9d,%r12d - xorl %r8d,%r15d + addl %r9d,%r12d // T1+=h + xorl %r8d,%r15d // Ch(e,f,g)=((f^g)&e)^g rorl $11,%r14d xorl %ecx,%r13d - addl %r15d,%r12d + addl %r15d,%r12d // T1+=Ch(e,f,g) movl %r10d,%r15d - addl (%rbp),%r12d + addl (%rbp),%r12d // T1+=K[round] xorl %r10d,%r14d - xorl %r11d,%r15d - rorl $6,%r13d + xorl %r11d,%r15d // a^b, b^c in next round + rorl $6,%r13d // Sigma1(e) movl %r11d,%r9d andl %r15d,%edi - rorl $2,%r14d - addl %r13d,%r12d + rorl $2,%r14d // Sigma0(a) + addl %r13d,%r12d // T1+=Sigma1(e) - xorl %edi,%r9d - addl %r12d,%ebx - addl %r12d,%r9d + xorl %edi,%r9d // h=Maj(a,b,c)=Ch(a^b,c,b) + addl %r12d,%ebx // d+=T1 + addl %r12d,%r9d // h+=T1 - leaq 4(%rbp),%rbp + leaq 4(%rbp),%rbp // round++ movl 16(%rsp),%r13d movl 4(%rsp),%edi movl %r13d,%r12d rorl $11,%r13d - addl %r14d,%r9d + addl %r14d,%r9d // modulo-scheduled h+=Sigma0(a) movl %edi,%r14d rorl $2,%edi @@ -907,8 +907,8 @@ _CET_ENDBR shrl $10,%r14d rorl $17,%edi - xorl %r13d,%r12d - xorl %r14d,%edi + xorl %r13d,%r12d // sigma0(X[(i+1)&0xf]) + xorl %r14d,%edi // sigma1(X[(i+14)&0xf]) addl 48(%rsp),%r12d addl 12(%rsp),%r12d @@ -920,43 +920,43 @@ _CET_ENDBR xorl %ebx,%r13d rorl $9,%r14d - xorl %edx,%edi + xorl %edx,%edi // f^g movl %r12d,12(%rsp) xorl %r9d,%r14d - andl %ebx,%edi + andl %ebx,%edi // (f^g)&e rorl $5,%r13d - addl %r8d,%r12d - xorl %edx,%edi + addl %r8d,%r12d // T1+=h + xorl %edx,%edi // Ch(e,f,g)=((f^g)&e)^g rorl $11,%r14d xorl %ebx,%r13d - addl %edi,%r12d + addl %edi,%r12d // T1+=Ch(e,f,g) movl %r9d,%edi - addl (%rbp),%r12d + addl (%rbp),%r12d // T1+=K[round] xorl %r9d,%r14d - xorl %r10d,%edi - rorl $6,%r13d + xorl %r10d,%edi // a^b, b^c in next round + rorl $6,%r13d // Sigma1(e) movl %r10d,%r8d andl %edi,%r15d - rorl $2,%r14d - addl %r13d,%r12d + rorl $2,%r14d // Sigma0(a) + addl %r13d,%r12d // T1+=Sigma1(e) - xorl %r15d,%r8d - addl %r12d,%eax - addl %r12d,%r8d + xorl %r15d,%r8d // h=Maj(a,b,c)=Ch(a^b,c,b) + addl %r12d,%eax // d+=T1 + addl %r12d,%r8d // h+=T1 - leaq 20(%rbp),%rbp + leaq 20(%rbp),%rbp // round++ movl 20(%rsp),%r13d movl 8(%rsp),%r15d movl %r13d,%r12d rorl $11,%r13d - addl %r14d,%r8d + addl %r14d,%r8d // modulo-scheduled h+=Sigma0(a) movl %r15d,%r14d rorl $2,%r15d @@ -967,8 +967,8 @@ _CET_ENDBR shrl $10,%r14d rorl $17,%r15d - xorl %r13d,%r12d - xorl %r14d,%r15d + xorl %r13d,%r12d // sigma0(X[(i+1)&0xf]) + xorl %r14d,%r15d // sigma1(X[(i+14)&0xf]) addl 52(%rsp),%r12d addl 16(%rsp),%r12d @@ -980,43 +980,43 @@ _CET_ENDBR xorl %eax,%r13d rorl $9,%r14d - xorl %ecx,%r15d + xorl %ecx,%r15d // f^g movl %r12d,16(%rsp) xorl %r8d,%r14d - andl %eax,%r15d + andl %eax,%r15d // (f^g)&e rorl $5,%r13d - addl %edx,%r12d - xorl %ecx,%r15d + addl %edx,%r12d // T1+=h + xorl %ecx,%r15d // Ch(e,f,g)=((f^g)&e)^g rorl $11,%r14d xorl %eax,%r13d - addl %r15d,%r12d + addl %r15d,%r12d // T1+=Ch(e,f,g) movl %r8d,%r15d - addl (%rbp),%r12d + addl (%rbp),%r12d // T1+=K[round] xorl %r8d,%r14d - xorl %r9d,%r15d - rorl $6,%r13d + xorl %r9d,%r15d // a^b, b^c in next round + rorl $6,%r13d // Sigma1(e) movl %r9d,%edx andl %r15d,%edi - rorl $2,%r14d - addl %r13d,%r12d + rorl $2,%r14d // Sigma0(a) + addl %r13d,%r12d // T1+=Sigma1(e) - xorl %edi,%edx - addl %r12d,%r11d - addl %r12d,%edx + xorl %edi,%edx // h=Maj(a,b,c)=Ch(a^b,c,b) + addl %r12d,%r11d // d+=T1 + addl %r12d,%edx // h+=T1 - leaq 4(%rbp),%rbp + leaq 4(%rbp),%rbp // round++ movl 24(%rsp),%r13d movl 12(%rsp),%edi movl %r13d,%r12d rorl $11,%r13d - addl %r14d,%edx + addl %r14d,%edx // modulo-scheduled h+=Sigma0(a) movl %edi,%r14d rorl $2,%edi @@ -1027,8 +1027,8 @@ _CET_ENDBR shrl $10,%r14d rorl $17,%edi - xorl %r13d,%r12d - xorl %r14d,%edi + xorl %r13d,%r12d // sigma0(X[(i+1)&0xf]) + xorl %r14d,%edi // sigma1(X[(i+14)&0xf]) addl 56(%rsp),%r12d addl 20(%rsp),%r12d @@ -1040,43 +1040,43 @@ _CET_ENDBR xorl %r11d,%r13d rorl $9,%r14d - xorl %ebx,%edi + xorl %ebx,%edi // f^g movl %r12d,20(%rsp) xorl %edx,%r14d - andl %r11d,%edi + andl %r11d,%edi // (f^g)&e rorl $5,%r13d - addl %ecx,%r12d - xorl %ebx,%edi + addl %ecx,%r12d // T1+=h + xorl %ebx,%edi // Ch(e,f,g)=((f^g)&e)^g rorl $11,%r14d xorl %r11d,%r13d - addl %edi,%r12d + addl %edi,%r12d // T1+=Ch(e,f,g) movl %edx,%edi - addl (%rbp),%r12d + addl (%rbp),%r12d // T1+=K[round] xorl %edx,%r14d - xorl %r8d,%edi - rorl $6,%r13d + xorl %r8d,%edi // a^b, b^c in next round + rorl $6,%r13d // Sigma1(e) movl %r8d,%ecx andl %edi,%r15d - rorl $2,%r14d - addl %r13d,%r12d + rorl $2,%r14d // Sigma0(a) + addl %r13d,%r12d // T1+=Sigma1(e) - xorl %r15d,%ecx - addl %r12d,%r10d - addl %r12d,%ecx + xorl %r15d,%ecx // h=Maj(a,b,c)=Ch(a^b,c,b) + addl %r12d,%r10d // d+=T1 + addl %r12d,%ecx // h+=T1 - leaq 4(%rbp),%rbp + leaq 4(%rbp),%rbp // round++ movl 28(%rsp),%r13d movl 16(%rsp),%r15d movl %r13d,%r12d rorl $11,%r13d - addl %r14d,%ecx + addl %r14d,%ecx // modulo-scheduled h+=Sigma0(a) movl %r15d,%r14d rorl $2,%r15d @@ -1087,8 +1087,8 @@ _CET_ENDBR shrl $10,%r14d rorl $17,%r15d - xorl %r13d,%r12d - xorl %r14d,%r15d + xorl %r13d,%r12d // sigma0(X[(i+1)&0xf]) + xorl %r14d,%r15d // sigma1(X[(i+14)&0xf]) addl 60(%rsp),%r12d addl 24(%rsp),%r12d @@ -1100,43 +1100,43 @@ _CET_ENDBR xorl %r10d,%r13d rorl $9,%r14d - xorl %eax,%r15d + xorl %eax,%r15d // f^g movl %r12d,24(%rsp) xorl %ecx,%r14d - andl %r10d,%r15d + andl %r10d,%r15d // (f^g)&e rorl $5,%r13d - addl %ebx,%r12d - xorl %eax,%r15d + addl %ebx,%r12d // T1+=h + xorl %eax,%r15d // Ch(e,f,g)=((f^g)&e)^g rorl $11,%r14d xorl %r10d,%r13d - addl %r15d,%r12d + addl %r15d,%r12d // T1+=Ch(e,f,g) movl %ecx,%r15d - addl (%rbp),%r12d + addl (%rbp),%r12d // T1+=K[round] xorl %ecx,%r14d - xorl %edx,%r15d - rorl $6,%r13d + xorl %edx,%r15d // a^b, b^c in next round + rorl $6,%r13d // Sigma1(e) movl %edx,%ebx andl %r15d,%edi - rorl $2,%r14d - addl %r13d,%r12d + rorl $2,%r14d // Sigma0(a) + addl %r13d,%r12d // T1+=Sigma1(e) - xorl %edi,%ebx - addl %r12d,%r9d - addl %r12d,%ebx + xorl %edi,%ebx // h=Maj(a,b,c)=Ch(a^b,c,b) + addl %r12d,%r9d // d+=T1 + addl %r12d,%ebx // h+=T1 - leaq 4(%rbp),%rbp + leaq 4(%rbp),%rbp // round++ movl 32(%rsp),%r13d movl 20(%rsp),%edi movl %r13d,%r12d rorl $11,%r13d - addl %r14d,%ebx + addl %r14d,%ebx // modulo-scheduled h+=Sigma0(a) movl %edi,%r14d rorl $2,%edi @@ -1147,8 +1147,8 @@ _CET_ENDBR shrl $10,%r14d rorl $17,%edi - xorl %r13d,%r12d - xorl %r14d,%edi + xorl %r13d,%r12d // sigma0(X[(i+1)&0xf]) + xorl %r14d,%edi // sigma1(X[(i+14)&0xf]) addl 0(%rsp),%r12d addl 28(%rsp),%r12d @@ -1160,43 +1160,43 @@ _CET_ENDBR xorl %r9d,%r13d rorl $9,%r14d - xorl %r11d,%edi + xorl %r11d,%edi // f^g movl %r12d,28(%rsp) xorl %ebx,%r14d - andl %r9d,%edi + andl %r9d,%edi // (f^g)&e rorl $5,%r13d - addl %eax,%r12d - xorl %r11d,%edi + addl %eax,%r12d // T1+=h + xorl %r11d,%edi // Ch(e,f,g)=((f^g)&e)^g rorl $11,%r14d xorl %r9d,%r13d - addl %edi,%r12d + addl %edi,%r12d // T1+=Ch(e,f,g) movl %ebx,%edi - addl (%rbp),%r12d + addl (%rbp),%r12d // T1+=K[round] xorl %ebx,%r14d - xorl %ecx,%edi - rorl $6,%r13d + xorl %ecx,%edi // a^b, b^c in next round + rorl $6,%r13d // Sigma1(e) movl %ecx,%eax andl %edi,%r15d - rorl $2,%r14d - addl %r13d,%r12d + rorl $2,%r14d // Sigma0(a) + addl %r13d,%r12d // T1+=Sigma1(e) - xorl %r15d,%eax - addl %r12d,%r8d - addl %r12d,%eax + xorl %r15d,%eax // h=Maj(a,b,c)=Ch(a^b,c,b) + addl %r12d,%r8d // d+=T1 + addl %r12d,%eax // h+=T1 - leaq 20(%rbp),%rbp + leaq 20(%rbp),%rbp // round++ movl 36(%rsp),%r13d movl 24(%rsp),%r15d movl %r13d,%r12d rorl $11,%r13d - addl %r14d,%eax + addl %r14d,%eax // modulo-scheduled h+=Sigma0(a) movl %r15d,%r14d rorl $2,%r15d @@ -1207,8 +1207,8 @@ _CET_ENDBR shrl $10,%r14d rorl $17,%r15d - xorl %r13d,%r12d - xorl %r14d,%r15d + xorl %r13d,%r12d // sigma0(X[(i+1)&0xf]) + xorl %r14d,%r15d // sigma1(X[(i+14)&0xf]) addl 4(%rsp),%r12d addl 32(%rsp),%r12d @@ -1220,43 +1220,43 @@ _CET_ENDBR xorl %r8d,%r13d rorl $9,%r14d - xorl %r10d,%r15d + xorl %r10d,%r15d // f^g movl %r12d,32(%rsp) xorl %eax,%r14d - andl %r8d,%r15d + andl %r8d,%r15d // (f^g)&e rorl $5,%r13d - addl %r11d,%r12d - xorl %r10d,%r15d + addl %r11d,%r12d // T1+=h + xorl %r10d,%r15d // Ch(e,f,g)=((f^g)&e)^g rorl $11,%r14d xorl %r8d,%r13d - addl %r15d,%r12d + addl %r15d,%r12d // T1+=Ch(e,f,g) movl %eax,%r15d - addl (%rbp),%r12d + addl (%rbp),%r12d // T1+=K[round] xorl %eax,%r14d - xorl %ebx,%r15d - rorl $6,%r13d + xorl %ebx,%r15d // a^b, b^c in next round + rorl $6,%r13d // Sigma1(e) movl %ebx,%r11d andl %r15d,%edi - rorl $2,%r14d - addl %r13d,%r12d + rorl $2,%r14d // Sigma0(a) + addl %r13d,%r12d // T1+=Sigma1(e) - xorl %edi,%r11d - addl %r12d,%edx - addl %r12d,%r11d + xorl %edi,%r11d // h=Maj(a,b,c)=Ch(a^b,c,b) + addl %r12d,%edx // d+=T1 + addl %r12d,%r11d // h+=T1 - leaq 4(%rbp),%rbp + leaq 4(%rbp),%rbp // round++ movl 40(%rsp),%r13d movl 28(%rsp),%edi movl %r13d,%r12d rorl $11,%r13d - addl %r14d,%r11d + addl %r14d,%r11d // modulo-scheduled h+=Sigma0(a) movl %edi,%r14d rorl $2,%edi @@ -1267,8 +1267,8 @@ _CET_ENDBR shrl $10,%r14d rorl $17,%edi - xorl %r13d,%r12d - xorl %r14d,%edi + xorl %r13d,%r12d // sigma0(X[(i+1)&0xf]) + xorl %r14d,%edi // sigma1(X[(i+14)&0xf]) addl 8(%rsp),%r12d addl 36(%rsp),%r12d @@ -1280,43 +1280,43 @@ _CET_ENDBR xorl %edx,%r13d rorl $9,%r14d - xorl %r9d,%edi + xorl %r9d,%edi // f^g movl %r12d,36(%rsp) xorl %r11d,%r14d - andl %edx,%edi + andl %edx,%edi // (f^g)&e rorl $5,%r13d - addl %r10d,%r12d - xorl %r9d,%edi + addl %r10d,%r12d // T1+=h + xorl %r9d,%edi // Ch(e,f,g)=((f^g)&e)^g rorl $11,%r14d xorl %edx,%r13d - addl %edi,%r12d + addl %edi,%r12d // T1+=Ch(e,f,g) movl %r11d,%edi - addl (%rbp),%r12d + addl (%rbp),%r12d // T1+=K[round] xorl %r11d,%r14d - xorl %eax,%edi - rorl $6,%r13d + xorl %eax,%edi // a^b, b^c in next round + rorl $6,%r13d // Sigma1(e) movl %eax,%r10d andl %edi,%r15d - rorl $2,%r14d - addl %r13d,%r12d + rorl $2,%r14d // Sigma0(a) + addl %r13d,%r12d // T1+=Sigma1(e) - xorl %r15d,%r10d - addl %r12d,%ecx - addl %r12d,%r10d + xorl %r15d,%r10d // h=Maj(a,b,c)=Ch(a^b,c,b) + addl %r12d,%ecx // d+=T1 + addl %r12d,%r10d // h+=T1 - leaq 4(%rbp),%rbp + leaq 4(%rbp),%rbp // round++ movl 44(%rsp),%r13d movl 32(%rsp),%r15d movl %r13d,%r12d rorl $11,%r13d - addl %r14d,%r10d + addl %r14d,%r10d // modulo-scheduled h+=Sigma0(a) movl %r15d,%r14d rorl $2,%r15d @@ -1327,8 +1327,8 @@ _CET_ENDBR shrl $10,%r14d rorl $17,%r15d - xorl %r13d,%r12d - xorl %r14d,%r15d + xorl %r13d,%r12d // sigma0(X[(i+1)&0xf]) + xorl %r14d,%r15d // sigma1(X[(i+14)&0xf]) addl 12(%rsp),%r12d addl 40(%rsp),%r12d @@ -1340,43 +1340,43 @@ _CET_ENDBR xorl %ecx,%r13d rorl $9,%r14d - xorl %r8d,%r15d + xorl %r8d,%r15d // f^g movl %r12d,40(%rsp) xorl %r10d,%r14d - andl %ecx,%r15d + andl %ecx,%r15d // (f^g)&e rorl $5,%r13d - addl %r9d,%r12d - xorl %r8d,%r15d + addl %r9d,%r12d // T1+=h + xorl %r8d,%r15d // Ch(e,f,g)=((f^g)&e)^g rorl $11,%r14d xorl %ecx,%r13d - addl %r15d,%r12d + addl %r15d,%r12d // T1+=Ch(e,f,g) movl %r10d,%r15d - addl (%rbp),%r12d + addl (%rbp),%r12d // T1+=K[round] xorl %r10d,%r14d - xorl %r11d,%r15d - rorl $6,%r13d + xorl %r11d,%r15d // a^b, b^c in next round + rorl $6,%r13d // Sigma1(e) movl %r11d,%r9d andl %r15d,%edi - rorl $2,%r14d - addl %r13d,%r12d + rorl $2,%r14d // Sigma0(a) + addl %r13d,%r12d // T1+=Sigma1(e) - xorl %edi,%r9d - addl %r12d,%ebx - addl %r12d,%r9d + xorl %edi,%r9d // h=Maj(a,b,c)=Ch(a^b,c,b) + addl %r12d,%ebx // d+=T1 + addl %r12d,%r9d // h+=T1 - leaq 4(%rbp),%rbp + leaq 4(%rbp),%rbp // round++ movl 48(%rsp),%r13d movl 36(%rsp),%edi movl %r13d,%r12d rorl $11,%r13d - addl %r14d,%r9d + addl %r14d,%r9d // modulo-scheduled h+=Sigma0(a) movl %edi,%r14d rorl $2,%edi @@ -1387,8 +1387,8 @@ _CET_ENDBR shrl $10,%r14d rorl $17,%edi - xorl %r13d,%r12d - xorl %r14d,%edi + xorl %r13d,%r12d // sigma0(X[(i+1)&0xf]) + xorl %r14d,%edi // sigma1(X[(i+14)&0xf]) addl 16(%rsp),%r12d addl 44(%rsp),%r12d @@ -1400,43 +1400,43 @@ _CET_ENDBR xorl %ebx,%r13d rorl $9,%r14d - xorl %edx,%edi + xorl %edx,%edi // f^g movl %r12d,44(%rsp) xorl %r9d,%r14d - andl %ebx,%edi + andl %ebx,%edi // (f^g)&e rorl $5,%r13d - addl %r8d,%r12d - xorl %edx,%edi + addl %r8d,%r12d // T1+=h + xorl %edx,%edi // Ch(e,f,g)=((f^g)&e)^g rorl $11,%r14d xorl %ebx,%r13d - addl %edi,%r12d + addl %edi,%r12d // T1+=Ch(e,f,g) movl %r9d,%edi - addl (%rbp),%r12d + addl (%rbp),%r12d // T1+=K[round] xorl %r9d,%r14d - xorl %r10d,%edi - rorl $6,%r13d + xorl %r10d,%edi // a^b, b^c in next round + rorl $6,%r13d // Sigma1(e) movl %r10d,%r8d andl %edi,%r15d - rorl $2,%r14d - addl %r13d,%r12d + rorl $2,%r14d // Sigma0(a) + addl %r13d,%r12d // T1+=Sigma1(e) - xorl %r15d,%r8d - addl %r12d,%eax - addl %r12d,%r8d + xorl %r15d,%r8d // h=Maj(a,b,c)=Ch(a^b,c,b) + addl %r12d,%eax // d+=T1 + addl %r12d,%r8d // h+=T1 - leaq 20(%rbp),%rbp + leaq 20(%rbp),%rbp // round++ movl 52(%rsp),%r13d movl 40(%rsp),%r15d movl %r13d,%r12d rorl $11,%r13d - addl %r14d,%r8d + addl %r14d,%r8d // modulo-scheduled h+=Sigma0(a) movl %r15d,%r14d rorl $2,%r15d @@ -1447,8 +1447,8 @@ _CET_ENDBR shrl $10,%r14d rorl $17,%r15d - xorl %r13d,%r12d - xorl %r14d,%r15d + xorl %r13d,%r12d // sigma0(X[(i+1)&0xf]) + xorl %r14d,%r15d // sigma1(X[(i+14)&0xf]) addl 20(%rsp),%r12d addl 48(%rsp),%r12d @@ -1460,43 +1460,43 @@ _CET_ENDBR xorl %eax,%r13d rorl $9,%r14d - xorl %ecx,%r15d + xorl %ecx,%r15d // f^g movl %r12d,48(%rsp) xorl %r8d,%r14d - andl %eax,%r15d + andl %eax,%r15d // (f^g)&e rorl $5,%r13d - addl %edx,%r12d - xorl %ecx,%r15d + addl %edx,%r12d // T1+=h + xorl %ecx,%r15d // Ch(e,f,g)=((f^g)&e)^g rorl $11,%r14d xorl %eax,%r13d - addl %r15d,%r12d + addl %r15d,%r12d // T1+=Ch(e,f,g) movl %r8d,%r15d - addl (%rbp),%r12d + addl (%rbp),%r12d // T1+=K[round] xorl %r8d,%r14d - xorl %r9d,%r15d - rorl $6,%r13d + xorl %r9d,%r15d // a^b, b^c in next round + rorl $6,%r13d // Sigma1(e) movl %r9d,%edx andl %r15d,%edi - rorl $2,%r14d - addl %r13d,%r12d + rorl $2,%r14d // Sigma0(a) + addl %r13d,%r12d // T1+=Sigma1(e) - xorl %edi,%edx - addl %r12d,%r11d - addl %r12d,%edx + xorl %edi,%edx // h=Maj(a,b,c)=Ch(a^b,c,b) + addl %r12d,%r11d // d+=T1 + addl %r12d,%edx // h+=T1 - leaq 4(%rbp),%rbp + leaq 4(%rbp),%rbp // round++ movl 56(%rsp),%r13d movl 44(%rsp),%edi movl %r13d,%r12d rorl $11,%r13d - addl %r14d,%edx + addl %r14d,%edx // modulo-scheduled h+=Sigma0(a) movl %edi,%r14d rorl $2,%edi @@ -1507,8 +1507,8 @@ _CET_ENDBR shrl $10,%r14d rorl $17,%edi - xorl %r13d,%r12d - xorl %r14d,%edi + xorl %r13d,%r12d // sigma0(X[(i+1)&0xf]) + xorl %r14d,%edi // sigma1(X[(i+14)&0xf]) addl 24(%rsp),%r12d addl 52(%rsp),%r12d @@ -1520,43 +1520,43 @@ _CET_ENDBR xorl %r11d,%r13d rorl $9,%r14d - xorl %ebx,%edi + xorl %ebx,%edi // f^g movl %r12d,52(%rsp) xorl %edx,%r14d - andl %r11d,%edi + andl %r11d,%edi // (f^g)&e rorl $5,%r13d - addl %ecx,%r12d - xorl %ebx,%edi + addl %ecx,%r12d // T1+=h + xorl %ebx,%edi // Ch(e,f,g)=((f^g)&e)^g rorl $11,%r14d xorl %r11d,%r13d - addl %edi,%r12d + addl %edi,%r12d // T1+=Ch(e,f,g) movl %edx,%edi - addl (%rbp),%r12d + addl (%rbp),%r12d // T1+=K[round] xorl %edx,%r14d - xorl %r8d,%edi - rorl $6,%r13d + xorl %r8d,%edi // a^b, b^c in next round + rorl $6,%r13d // Sigma1(e) movl %r8d,%ecx andl %edi,%r15d - rorl $2,%r14d - addl %r13d,%r12d + rorl $2,%r14d // Sigma0(a) + addl %r13d,%r12d // T1+=Sigma1(e) - xorl %r15d,%ecx - addl %r12d,%r10d - addl %r12d,%ecx + xorl %r15d,%ecx // h=Maj(a,b,c)=Ch(a^b,c,b) + addl %r12d,%r10d // d+=T1 + addl %r12d,%ecx // h+=T1 - leaq 4(%rbp),%rbp + leaq 4(%rbp),%rbp // round++ movl 60(%rsp),%r13d movl 48(%rsp),%r15d movl %r13d,%r12d rorl $11,%r13d - addl %r14d,%ecx + addl %r14d,%ecx // modulo-scheduled h+=Sigma0(a) movl %r15d,%r14d rorl $2,%r15d @@ -1567,8 +1567,8 @@ _CET_ENDBR shrl $10,%r14d rorl $17,%r15d - xorl %r13d,%r12d - xorl %r14d,%r15d + xorl %r13d,%r12d // sigma0(X[(i+1)&0xf]) + xorl %r14d,%r15d // sigma1(X[(i+14)&0xf]) addl 28(%rsp),%r12d addl 56(%rsp),%r12d @@ -1580,43 +1580,43 @@ _CET_ENDBR xorl %r10d,%r13d rorl $9,%r14d - xorl %eax,%r15d + xorl %eax,%r15d // f^g movl %r12d,56(%rsp) xorl %ecx,%r14d - andl %r10d,%r15d + andl %r10d,%r15d // (f^g)&e rorl $5,%r13d - addl %ebx,%r12d - xorl %eax,%r15d + addl %ebx,%r12d // T1+=h + xorl %eax,%r15d // Ch(e,f,g)=((f^g)&e)^g rorl $11,%r14d xorl %r10d,%r13d - addl %r15d,%r12d + addl %r15d,%r12d // T1+=Ch(e,f,g) movl %ecx,%r15d - addl (%rbp),%r12d + addl (%rbp),%r12d // T1+=K[round] xorl %ecx,%r14d - xorl %edx,%r15d - rorl $6,%r13d + xorl %edx,%r15d // a^b, b^c in next round + rorl $6,%r13d // Sigma1(e) movl %edx,%ebx andl %r15d,%edi - rorl $2,%r14d - addl %r13d,%r12d + rorl $2,%r14d // Sigma0(a) + addl %r13d,%r12d // T1+=Sigma1(e) - xorl %edi,%ebx - addl %r12d,%r9d - addl %r12d,%ebx + xorl %edi,%ebx // h=Maj(a,b,c)=Ch(a^b,c,b) + addl %r12d,%r9d // d+=T1 + addl %r12d,%ebx // h+=T1 - leaq 4(%rbp),%rbp + leaq 4(%rbp),%rbp // round++ movl 0(%rsp),%r13d movl 52(%rsp),%edi movl %r13d,%r12d rorl $11,%r13d - addl %r14d,%ebx + addl %r14d,%ebx // modulo-scheduled h+=Sigma0(a) movl %edi,%r14d rorl $2,%edi @@ -1627,8 +1627,8 @@ _CET_ENDBR shrl $10,%r14d rorl $17,%edi - xorl %r13d,%r12d - xorl %r14d,%edi + xorl %r13d,%r12d // sigma0(X[(i+1)&0xf]) + xorl %r14d,%edi // sigma1(X[(i+14)&0xf]) addl 32(%rsp),%r12d addl 60(%rsp),%r12d @@ -1640,42 +1640,42 @@ _CET_ENDBR xorl %r9d,%r13d rorl $9,%r14d - xorl %r11d,%edi + xorl %r11d,%edi // f^g movl %r12d,60(%rsp) xorl %ebx,%r14d - andl %r9d,%edi + andl %r9d,%edi // (f^g)&e rorl $5,%r13d - addl %eax,%r12d - xorl %r11d,%edi + addl %eax,%r12d // T1+=h + xorl %r11d,%edi // Ch(e,f,g)=((f^g)&e)^g rorl $11,%r14d xorl %r9d,%r13d - addl %edi,%r12d + addl %edi,%r12d // T1+=Ch(e,f,g) movl %ebx,%edi - addl (%rbp),%r12d + addl (%rbp),%r12d // T1+=K[round] xorl %ebx,%r14d - xorl %ecx,%edi - rorl $6,%r13d + xorl %ecx,%edi // a^b, b^c in next round + rorl $6,%r13d // Sigma1(e) movl %ecx,%eax andl %edi,%r15d - rorl $2,%r14d - addl %r13d,%r12d + rorl $2,%r14d // Sigma0(a) + addl %r13d,%r12d // T1+=Sigma1(e) - xorl %r15d,%eax - addl %r12d,%r8d - addl %r12d,%eax + xorl %r15d,%eax // h=Maj(a,b,c)=Ch(a^b,c,b) + addl %r12d,%r8d // d+=T1 + addl %r12d,%eax // h+=T1 - leaq 20(%rbp),%rbp + leaq 20(%rbp),%rbp // round++ cmpb $0,3(%rbp) jnz .Lrounds_16_xx movq 64+0(%rsp),%rdi - addl %r14d,%eax + addl %r14d,%eax // modulo-scheduled h+=Sigma0(a) leaq 64(%rsi),%rsi addl 0(%rdi),%eax @@ -1772,16 +1772,16 @@ sha256_block_data_order_hw: .cfi_startproc _CET_ENDBR leaq K256+128(%rip),%rcx - movdqu (%rdi),%xmm1 - movdqu 16(%rdi),%xmm2 - movdqa 512-128(%rcx),%xmm7 - - pshufd $0x1b,%xmm1,%xmm0 - pshufd $0xb1,%xmm1,%xmm1 - pshufd $0x1b,%xmm2,%xmm2 - movdqa %xmm7,%xmm8 - palignr $8,%xmm2,%xmm1 - punpcklqdq %xmm0,%xmm2 + movdqu (%rdi),%xmm1 // DCBA + movdqu 16(%rdi),%xmm2 // HGFE + movdqa 512-128(%rcx),%xmm7 // byte swap mask + + pshufd $0x1b,%xmm1,%xmm0 // ABCD + pshufd $0xb1,%xmm1,%xmm1 // CDAB + pshufd $0x1b,%xmm2,%xmm2 // EFGH + movdqa %xmm7,%xmm8 // offload + palignr $8,%xmm2,%xmm1 // ABEF + punpcklqdq %xmm0,%xmm2 // CDGH jmp .Loop_shaext .align 16 @@ -1795,17 +1795,17 @@ _CET_ENDBR movdqa 0-128(%rcx),%xmm0 paddd %xmm3,%xmm0 pshufb %xmm7,%xmm4 - movdqa %xmm2,%xmm10 - sha256rnds2 %xmm1,%xmm2 + movdqa %xmm2,%xmm10 // offload + sha256rnds2 %xmm1,%xmm2 // 0-3 pshufd $0x0e,%xmm0,%xmm0 nop - movdqa %xmm1,%xmm9 + movdqa %xmm1,%xmm9 // offload sha256rnds2 %xmm2,%xmm1 movdqa 32-128(%rcx),%xmm0 paddd %xmm4,%xmm0 pshufb %xmm7,%xmm5 - sha256rnds2 %xmm1,%xmm2 + sha256rnds2 %xmm1,%xmm2 // 4-7 pshufd $0x0e,%xmm0,%xmm0 leaq 64(%rsi),%rsi sha256msg1 %xmm4,%xmm3 @@ -1814,7 +1814,7 @@ _CET_ENDBR movdqa 64-128(%rcx),%xmm0 paddd %xmm5,%xmm0 pshufb %xmm7,%xmm6 - sha256rnds2 %xmm1,%xmm2 + sha256rnds2 %xmm1,%xmm2 // 8-11 pshufd $0x0e,%xmm0,%xmm0 movdqa %xmm6,%xmm7 palignr $4,%xmm5,%xmm7 @@ -1826,7 +1826,7 @@ _CET_ENDBR movdqa 96-128(%rcx),%xmm0 paddd %xmm6,%xmm0 sha256msg2 %xmm6,%xmm3 - sha256rnds2 %xmm1,%xmm2 + sha256rnds2 %xmm1,%xmm2 // 12-15 pshufd $0x0e,%xmm0,%xmm0 movdqa %xmm3,%xmm7 palignr $4,%xmm6,%xmm7 @@ -1837,7 +1837,7 @@ _CET_ENDBR movdqa 128-128(%rcx),%xmm0 paddd %xmm3,%xmm0 sha256msg2 %xmm3,%xmm4 - sha256rnds2 %xmm1,%xmm2 + sha256rnds2 %xmm1,%xmm2 // 16-19... pshufd $0x0e,%xmm0,%xmm0 movdqa %xmm4,%xmm7 palignr $4,%xmm3,%xmm7 @@ -1848,7 +1848,7 @@ _CET_ENDBR movdqa 160-128(%rcx),%xmm0 paddd %xmm4,%xmm0 sha256msg2 %xmm4,%xmm5 - sha256rnds2 %xmm1,%xmm2 + sha256rnds2 %xmm1,%xmm2 // 16-19... pshufd $0x0e,%xmm0,%xmm0 movdqa %xmm5,%xmm7 palignr $4,%xmm4,%xmm7 @@ -1859,7 +1859,7 @@ _CET_ENDBR movdqa 192-128(%rcx),%xmm0 paddd %xmm5,%xmm0 sha256msg2 %xmm5,%xmm6 - sha256rnds2 %xmm1,%xmm2 + sha256rnds2 %xmm1,%xmm2 // 16-19... pshufd $0x0e,%xmm0,%xmm0 movdqa %xmm6,%xmm7 palignr $4,%xmm5,%xmm7 @@ -1870,7 +1870,7 @@ _CET_ENDBR movdqa 224-128(%rcx),%xmm0 paddd %xmm6,%xmm0 sha256msg2 %xmm6,%xmm3 - sha256rnds2 %xmm1,%xmm2 + sha256rnds2 %xmm1,%xmm2 // 16-19... pshufd $0x0e,%xmm0,%xmm0 movdqa %xmm3,%xmm7 palignr $4,%xmm6,%xmm7 @@ -1881,7 +1881,7 @@ _CET_ENDBR movdqa 256-128(%rcx),%xmm0 paddd %xmm3,%xmm0 sha256msg2 %xmm3,%xmm4 - sha256rnds2 %xmm1,%xmm2 + sha256rnds2 %xmm1,%xmm2 // 16-19... pshufd $0x0e,%xmm0,%xmm0 movdqa %xmm4,%xmm7 palignr $4,%xmm3,%xmm7 @@ -1892,7 +1892,7 @@ _CET_ENDBR movdqa 288-128(%rcx),%xmm0 paddd %xmm4,%xmm0 sha256msg2 %xmm4,%xmm5 - sha256rnds2 %xmm1,%xmm2 + sha256rnds2 %xmm1,%xmm2 // 16-19... pshufd $0x0e,%xmm0,%xmm0 movdqa %xmm5,%xmm7 palignr $4,%xmm4,%xmm7 @@ -1903,7 +1903,7 @@ _CET_ENDBR movdqa 320-128(%rcx),%xmm0 paddd %xmm5,%xmm0 sha256msg2 %xmm5,%xmm6 - sha256rnds2 %xmm1,%xmm2 + sha256rnds2 %xmm1,%xmm2 // 16-19... pshufd $0x0e,%xmm0,%xmm0 movdqa %xmm6,%xmm7 palignr $4,%xmm5,%xmm7 @@ -1914,7 +1914,7 @@ _CET_ENDBR movdqa 352-128(%rcx),%xmm0 paddd %xmm6,%xmm0 sha256msg2 %xmm6,%xmm3 - sha256rnds2 %xmm1,%xmm2 + sha256rnds2 %xmm1,%xmm2 // 16-19... pshufd $0x0e,%xmm0,%xmm0 movdqa %xmm3,%xmm7 palignr $4,%xmm6,%xmm7 @@ -1925,7 +1925,7 @@ _CET_ENDBR movdqa 384-128(%rcx),%xmm0 paddd %xmm3,%xmm0 sha256msg2 %xmm3,%xmm4 - sha256rnds2 %xmm1,%xmm2 + sha256rnds2 %xmm1,%xmm2 // 16-19... pshufd $0x0e,%xmm0,%xmm0 movdqa %xmm4,%xmm7 palignr $4,%xmm3,%xmm7 @@ -1936,7 +1936,7 @@ _CET_ENDBR movdqa 416-128(%rcx),%xmm0 paddd %xmm4,%xmm0 sha256msg2 %xmm4,%xmm5 - sha256rnds2 %xmm1,%xmm2 + sha256rnds2 %xmm1,%xmm2 // 52-55 pshufd $0x0e,%xmm0,%xmm0 movdqa %xmm5,%xmm7 palignr $4,%xmm4,%xmm7 @@ -1945,7 +1945,7 @@ _CET_ENDBR movdqa 448-128(%rcx),%xmm0 paddd %xmm5,%xmm0 - sha256rnds2 %xmm1,%xmm2 + sha256rnds2 %xmm1,%xmm2 // 56-59 pshufd $0x0e,%xmm0,%xmm0 sha256msg2 %xmm5,%xmm6 movdqa %xmm8,%xmm7 @@ -1954,7 +1954,7 @@ _CET_ENDBR movdqa 480-128(%rcx),%xmm0 paddd %xmm6,%xmm0 nop - sha256rnds2 %xmm1,%xmm2 + sha256rnds2 %xmm1,%xmm2 // 60-63 pshufd $0x0e,%xmm0,%xmm0 decq %rdx nop @@ -1964,11 +1964,11 @@ _CET_ENDBR paddd %xmm9,%xmm1 jnz .Loop_shaext - pshufd $0xb1,%xmm2,%xmm2 - pshufd $0x1b,%xmm1,%xmm7 - pshufd $0xb1,%xmm1,%xmm1 - punpckhqdq %xmm2,%xmm1 - palignr $8,%xmm7,%xmm2 + pshufd $0xb1,%xmm2,%xmm2 // DCHG + pshufd $0x1b,%xmm1,%xmm7 // FEBA + pshufd $0xb1,%xmm1,%xmm1 // BAFE + punpckhqdq %xmm2,%xmm1 // DCBA + palignr $8,%xmm7,%xmm2 // HGFE movdqu %xmm1,(%rdi) movdqu %xmm2,16(%rdi) @@ -1982,7 +1982,7 @@ _CET_ENDBR sha256_block_data_order_ssse3: .cfi_startproc _CET_ENDBR - movq %rsp,%rax + movq %rsp,%rax // copy %rsp .cfi_def_cfa_register %rax pushq %rbx .cfi_offset %rbx,-16 @@ -1996,14 +1996,14 @@ _CET_ENDBR .cfi_offset %r14,-48 pushq %r15 .cfi_offset %r15,-56 - shlq $4,%rdx + shlq $4,%rdx // num*16 subq $96,%rsp - leaq (%rsi,%rdx,4),%rdx - andq $-64,%rsp - movq %rdi,64+0(%rsp) - movq %rsi,64+8(%rsp) - movq %rdx,64+16(%rsp) - movq %rax,88(%rsp) + leaq (%rsi,%rdx,4),%rdx // inp+num*16*4 + andq $-64,%rsp // align stack frame + movq %rdi,64+0(%rsp) // save ctx, 1st arg + movq %rsi,64+8(%rsp) // save inp, 2nd arh + movq %rdx,64+16(%rsp) // save end pointer, "3rd" arg + movq %rax,88(%rsp) // save copy of %rsp .cfi_escape 0x0f,0x06,0x77,0xd8,0x00,0x06,0x23,0x08 .Lprologue_ssse3: @@ -2015,8 +2015,8 @@ _CET_ENDBR movl 20(%rdi),%r9d movl 24(%rdi),%r10d movl 28(%rdi),%r11d - - +// movdqa K256+512+32(%rip),%xmm8 +// movdqa K256+512+64(%rip),%xmm9 jmp .Lloop_ssse3 .align 16 .Lloop_ssse3: @@ -2043,14 +2043,14 @@ _CET_ENDBR movdqa %xmm5,16(%rsp) movl %ebx,%edi movdqa %xmm6,32(%rsp) - xorl %ecx,%edi + xorl %ecx,%edi // magic movdqa %xmm7,48(%rsp) movl %r8d,%r13d jmp .Lssse3_00_47 .align 16 .Lssse3_00_47: - subq $-128,%rbp + subq $-128,%rbp // size optimization rorl $14,%r13d movdqa %xmm1,%xmm4 movl %r14d,%eax @@ -3097,7 +3097,7 @@ _CET_ENDBR sha256_block_data_order_avx: .cfi_startproc _CET_ENDBR - movq %rsp,%rax + movq %rsp,%rax // copy %rsp .cfi_def_cfa_register %rax pushq %rbx .cfi_offset %rbx,-16 @@ -3111,14 +3111,14 @@ _CET_ENDBR .cfi_offset %r14,-48 pushq %r15 .cfi_offset %r15,-56 - shlq $4,%rdx + shlq $4,%rdx // num*16 subq $96,%rsp - leaq (%rsi,%rdx,4),%rdx - andq $-64,%rsp - movq %rdi,64+0(%rsp) - movq %rsi,64+8(%rsp) - movq %rdx,64+16(%rsp) - movq %rax,88(%rsp) + leaq (%rsi,%rdx,4),%rdx // inp+num*16*4 + andq $-64,%rsp // align stack frame + movq %rdi,64+0(%rsp) // save ctx, 1st arg + movq %rsi,64+8(%rsp) // save inp, 2nd arh + movq %rdx,64+16(%rsp) // save end pointer, "3rd" arg + movq %rax,88(%rsp) // save copy of %rsp .cfi_escape 0x0f,0x06,0x77,0xd8,0x00,0x06,0x23,0x08 .Lprologue_avx: @@ -3155,14 +3155,14 @@ _CET_ENDBR vmovdqa %xmm5,16(%rsp) movl %ebx,%edi vmovdqa %xmm6,32(%rsp) - xorl %ecx,%edi + xorl %ecx,%edi // magic vmovdqa %xmm7,48(%rsp) movl %r8d,%r13d jmp .Lavx_00_47 .align 16 .Lavx_00_47: - subq $-128,%rbp + subq $-128,%rbp // size optimization vpalignr $4,%xmm0,%xmm1,%xmm4 shrdl $14,%r13d,%r13d movl %r14d,%eax diff --git a/third_party/boringssl/gen/bcm/sha256-x86_64-win.asm b/third_party/boringssl/gen/bcm/sha256-x86_64-win.asm index 61b11d8ae..7f3a21ba3 100644 --- a/third_party/boringssl/gen/bcm/sha256-x86_64-win.asm +++ b/third_party/boringssl/gen/bcm/sha256-x86_64-win.asm @@ -29,7 +29,7 @@ $L$SEH_begin_sha256_block_data_order_nohw: _CET_ENDBR - mov rax,rsp + mov rax,rsp ; copy %rsp push rbx @@ -43,14 +43,14 @@ _CET_ENDBR push r15 - shl rdx,4 + shl rdx,4 ; num*16 sub rsp,16*4+4*8 - lea rdx,[rdx*4+rsi] - and rsp,-64 - mov QWORD[((64+0))+rsp],rdi - mov QWORD[((64+8))+rsp],rsi - mov QWORD[((64+16))+rsp],rdx - mov QWORD[88+rsp],rax + lea rdx,[rdx*4+rsi] ; inp+num*16*4 + and rsp,-64 ; align stack frame + mov QWORD[((64+0))+rsp],rdi ; save ctx, 1st arg + mov QWORD[((64+8))+rsp],rsi ; save inp, 2nd arh + mov QWORD[((64+16))+rsp],rdx ; save end pointer, "3rd" arg + mov QWORD[88+rsp],rax ; save copy of %rsp $L$prologue: @@ -68,7 +68,7 @@ ALIGN 16 $L$loop: mov edi,ebx lea rbp,[K256] - xor edi,ecx + xor edi,ecx ; magic mov r12d,DWORD[rsi] mov r13d,r8d mov r14d,eax @@ -78,38 +78,38 @@ $L$loop: xor r13d,r8d ror r14d,9 - xor r15d,r10d + xor r15d,r10d ; f^g mov DWORD[rsp],r12d xor r14d,eax - and r15d,r8d + and r15d,r8d ; (f^g)&e ror r13d,5 - add r12d,r11d - xor r15d,r10d + add r12d,r11d ; T1+=h + xor r15d,r10d ; Ch(e,f,g)=((f^g)&e)^g ror r14d,11 xor r13d,r8d - add r12d,r15d + add r12d,r15d ; T1+=Ch(e,f,g) mov r15d,eax - add r12d,DWORD[rbp] + add r12d,DWORD[rbp] ; T1+=K[round] xor r14d,eax - xor r15d,ebx - ror r13d,6 + xor r15d,ebx ; a^b, b^c in next round + ror r13d,6 ; Sigma1(e) mov r11d,ebx and edi,r15d - ror r14d,2 - add r12d,r13d + ror r14d,2 ; Sigma0(a) + add r12d,r13d ; T1+=Sigma1(e) - xor r11d,edi - add edx,r12d - add r11d,r12d + xor r11d,edi ; h=Maj(a,b,c)=Ch(a^b,c,b) + add edx,r12d ; d+=T1 + add r11d,r12d ; h+=T1 - lea rbp,[4+rbp] - add r11d,r14d + lea rbp,[4+rbp] ; round++ + add r11d,r14d ; h+=Sigma0(a) mov r12d,DWORD[4+rsi] mov r13d,edx mov r14d,r11d @@ -119,38 +119,38 @@ $L$loop: xor r13d,edx ror r14d,9 - xor edi,r9d + xor edi,r9d ; f^g mov DWORD[4+rsp],r12d xor r14d,r11d - and edi,edx + and edi,edx ; (f^g)&e ror r13d,5 - add r12d,r10d - xor edi,r9d + add r12d,r10d ; T1+=h + xor edi,r9d ; Ch(e,f,g)=((f^g)&e)^g ror r14d,11 xor r13d,edx - add r12d,edi + add r12d,edi ; T1+=Ch(e,f,g) mov edi,r11d - add r12d,DWORD[rbp] + add r12d,DWORD[rbp] ; T1+=K[round] xor r14d,r11d - xor edi,eax - ror r13d,6 + xor edi,eax ; a^b, b^c in next round + ror r13d,6 ; Sigma1(e) mov r10d,eax and r15d,edi - ror r14d,2 - add r12d,r13d + ror r14d,2 ; Sigma0(a) + add r12d,r13d ; T1+=Sigma1(e) - xor r10d,r15d - add ecx,r12d - add r10d,r12d + xor r10d,r15d ; h=Maj(a,b,c)=Ch(a^b,c,b) + add ecx,r12d ; d+=T1 + add r10d,r12d ; h+=T1 - lea rbp,[4+rbp] - add r10d,r14d + lea rbp,[4+rbp] ; round++ + add r10d,r14d ; h+=Sigma0(a) mov r12d,DWORD[8+rsi] mov r13d,ecx mov r14d,r10d @@ -160,38 +160,38 @@ $L$loop: xor r13d,ecx ror r14d,9 - xor r15d,r8d + xor r15d,r8d ; f^g mov DWORD[8+rsp],r12d xor r14d,r10d - and r15d,ecx + and r15d,ecx ; (f^g)&e ror r13d,5 - add r12d,r9d - xor r15d,r8d + add r12d,r9d ; T1+=h + xor r15d,r8d ; Ch(e,f,g)=((f^g)&e)^g ror r14d,11 xor r13d,ecx - add r12d,r15d + add r12d,r15d ; T1+=Ch(e,f,g) mov r15d,r10d - add r12d,DWORD[rbp] + add r12d,DWORD[rbp] ; T1+=K[round] xor r14d,r10d - xor r15d,r11d - ror r13d,6 + xor r15d,r11d ; a^b, b^c in next round + ror r13d,6 ; Sigma1(e) mov r9d,r11d and edi,r15d - ror r14d,2 - add r12d,r13d + ror r14d,2 ; Sigma0(a) + add r12d,r13d ; T1+=Sigma1(e) - xor r9d,edi - add ebx,r12d - add r9d,r12d + xor r9d,edi ; h=Maj(a,b,c)=Ch(a^b,c,b) + add ebx,r12d ; d+=T1 + add r9d,r12d ; h+=T1 - lea rbp,[4+rbp] - add r9d,r14d + lea rbp,[4+rbp] ; round++ + add r9d,r14d ; h+=Sigma0(a) mov r12d,DWORD[12+rsi] mov r13d,ebx mov r14d,r9d @@ -201,38 +201,38 @@ $L$loop: xor r13d,ebx ror r14d,9 - xor edi,edx + xor edi,edx ; f^g mov DWORD[12+rsp],r12d xor r14d,r9d - and edi,ebx + and edi,ebx ; (f^g)&e ror r13d,5 - add r12d,r8d - xor edi,edx + add r12d,r8d ; T1+=h + xor edi,edx ; Ch(e,f,g)=((f^g)&e)^g ror r14d,11 xor r13d,ebx - add r12d,edi + add r12d,edi ; T1+=Ch(e,f,g) mov edi,r9d - add r12d,DWORD[rbp] + add r12d,DWORD[rbp] ; T1+=K[round] xor r14d,r9d - xor edi,r10d - ror r13d,6 + xor edi,r10d ; a^b, b^c in next round + ror r13d,6 ; Sigma1(e) mov r8d,r10d and r15d,edi - ror r14d,2 - add r12d,r13d + ror r14d,2 ; Sigma0(a) + add r12d,r13d ; T1+=Sigma1(e) - xor r8d,r15d - add eax,r12d - add r8d,r12d + xor r8d,r15d ; h=Maj(a,b,c)=Ch(a^b,c,b) + add eax,r12d ; d+=T1 + add r8d,r12d ; h+=T1 - lea rbp,[20+rbp] - add r8d,r14d + lea rbp,[20+rbp] ; round++ + add r8d,r14d ; h+=Sigma0(a) mov r12d,DWORD[16+rsi] mov r13d,eax mov r14d,r8d @@ -242,38 +242,38 @@ $L$loop: xor r13d,eax ror r14d,9 - xor r15d,ecx + xor r15d,ecx ; f^g mov DWORD[16+rsp],r12d xor r14d,r8d - and r15d,eax + and r15d,eax ; (f^g)&e ror r13d,5 - add r12d,edx - xor r15d,ecx + add r12d,edx ; T1+=h + xor r15d,ecx ; Ch(e,f,g)=((f^g)&e)^g ror r14d,11 xor r13d,eax - add r12d,r15d + add r12d,r15d ; T1+=Ch(e,f,g) mov r15d,r8d - add r12d,DWORD[rbp] + add r12d,DWORD[rbp] ; T1+=K[round] xor r14d,r8d - xor r15d,r9d - ror r13d,6 + xor r15d,r9d ; a^b, b^c in next round + ror r13d,6 ; Sigma1(e) mov edx,r9d and edi,r15d - ror r14d,2 - add r12d,r13d + ror r14d,2 ; Sigma0(a) + add r12d,r13d ; T1+=Sigma1(e) - xor edx,edi - add r11d,r12d - add edx,r12d + xor edx,edi ; h=Maj(a,b,c)=Ch(a^b,c,b) + add r11d,r12d ; d+=T1 + add edx,r12d ; h+=T1 - lea rbp,[4+rbp] - add edx,r14d + lea rbp,[4+rbp] ; round++ + add edx,r14d ; h+=Sigma0(a) mov r12d,DWORD[20+rsi] mov r13d,r11d mov r14d,edx @@ -283,38 +283,38 @@ $L$loop: xor r13d,r11d ror r14d,9 - xor edi,ebx + xor edi,ebx ; f^g mov DWORD[20+rsp],r12d xor r14d,edx - and edi,r11d + and edi,r11d ; (f^g)&e ror r13d,5 - add r12d,ecx - xor edi,ebx + add r12d,ecx ; T1+=h + xor edi,ebx ; Ch(e,f,g)=((f^g)&e)^g ror r14d,11 xor r13d,r11d - add r12d,edi + add r12d,edi ; T1+=Ch(e,f,g) mov edi,edx - add r12d,DWORD[rbp] + add r12d,DWORD[rbp] ; T1+=K[round] xor r14d,edx - xor edi,r8d - ror r13d,6 + xor edi,r8d ; a^b, b^c in next round + ror r13d,6 ; Sigma1(e) mov ecx,r8d and r15d,edi - ror r14d,2 - add r12d,r13d + ror r14d,2 ; Sigma0(a) + add r12d,r13d ; T1+=Sigma1(e) - xor ecx,r15d - add r10d,r12d - add ecx,r12d + xor ecx,r15d ; h=Maj(a,b,c)=Ch(a^b,c,b) + add r10d,r12d ; d+=T1 + add ecx,r12d ; h+=T1 - lea rbp,[4+rbp] - add ecx,r14d + lea rbp,[4+rbp] ; round++ + add ecx,r14d ; h+=Sigma0(a) mov r12d,DWORD[24+rsi] mov r13d,r10d mov r14d,ecx @@ -324,38 +324,38 @@ $L$loop: xor r13d,r10d ror r14d,9 - xor r15d,eax + xor r15d,eax ; f^g mov DWORD[24+rsp],r12d xor r14d,ecx - and r15d,r10d + and r15d,r10d ; (f^g)&e ror r13d,5 - add r12d,ebx - xor r15d,eax + add r12d,ebx ; T1+=h + xor r15d,eax ; Ch(e,f,g)=((f^g)&e)^g ror r14d,11 xor r13d,r10d - add r12d,r15d + add r12d,r15d ; T1+=Ch(e,f,g) mov r15d,ecx - add r12d,DWORD[rbp] + add r12d,DWORD[rbp] ; T1+=K[round] xor r14d,ecx - xor r15d,edx - ror r13d,6 + xor r15d,edx ; a^b, b^c in next round + ror r13d,6 ; Sigma1(e) mov ebx,edx and edi,r15d - ror r14d,2 - add r12d,r13d + ror r14d,2 ; Sigma0(a) + add r12d,r13d ; T1+=Sigma1(e) - xor ebx,edi - add r9d,r12d - add ebx,r12d + xor ebx,edi ; h=Maj(a,b,c)=Ch(a^b,c,b) + add r9d,r12d ; d+=T1 + add ebx,r12d ; h+=T1 - lea rbp,[4+rbp] - add ebx,r14d + lea rbp,[4+rbp] ; round++ + add ebx,r14d ; h+=Sigma0(a) mov r12d,DWORD[28+rsi] mov r13d,r9d mov r14d,ebx @@ -365,38 +365,38 @@ $L$loop: xor r13d,r9d ror r14d,9 - xor edi,r11d + xor edi,r11d ; f^g mov DWORD[28+rsp],r12d xor r14d,ebx - and edi,r9d + and edi,r9d ; (f^g)&e ror r13d,5 - add r12d,eax - xor edi,r11d + add r12d,eax ; T1+=h + xor edi,r11d ; Ch(e,f,g)=((f^g)&e)^g ror r14d,11 xor r13d,r9d - add r12d,edi + add r12d,edi ; T1+=Ch(e,f,g) mov edi,ebx - add r12d,DWORD[rbp] + add r12d,DWORD[rbp] ; T1+=K[round] xor r14d,ebx - xor edi,ecx - ror r13d,6 + xor edi,ecx ; a^b, b^c in next round + ror r13d,6 ; Sigma1(e) mov eax,ecx and r15d,edi - ror r14d,2 - add r12d,r13d + ror r14d,2 ; Sigma0(a) + add r12d,r13d ; T1+=Sigma1(e) - xor eax,r15d - add r8d,r12d - add eax,r12d + xor eax,r15d ; h=Maj(a,b,c)=Ch(a^b,c,b) + add r8d,r12d ; d+=T1 + add eax,r12d ; h+=T1 - lea rbp,[20+rbp] - add eax,r14d + lea rbp,[20+rbp] ; round++ + add eax,r14d ; h+=Sigma0(a) mov r12d,DWORD[32+rsi] mov r13d,r8d mov r14d,eax @@ -406,38 +406,38 @@ $L$loop: xor r13d,r8d ror r14d,9 - xor r15d,r10d + xor r15d,r10d ; f^g mov DWORD[32+rsp],r12d xor r14d,eax - and r15d,r8d + and r15d,r8d ; (f^g)&e ror r13d,5 - add r12d,r11d - xor r15d,r10d + add r12d,r11d ; T1+=h + xor r15d,r10d ; Ch(e,f,g)=((f^g)&e)^g ror r14d,11 xor r13d,r8d - add r12d,r15d + add r12d,r15d ; T1+=Ch(e,f,g) mov r15d,eax - add r12d,DWORD[rbp] + add r12d,DWORD[rbp] ; T1+=K[round] xor r14d,eax - xor r15d,ebx - ror r13d,6 + xor r15d,ebx ; a^b, b^c in next round + ror r13d,6 ; Sigma1(e) mov r11d,ebx and edi,r15d - ror r14d,2 - add r12d,r13d + ror r14d,2 ; Sigma0(a) + add r12d,r13d ; T1+=Sigma1(e) - xor r11d,edi - add edx,r12d - add r11d,r12d + xor r11d,edi ; h=Maj(a,b,c)=Ch(a^b,c,b) + add edx,r12d ; d+=T1 + add r11d,r12d ; h+=T1 - lea rbp,[4+rbp] - add r11d,r14d + lea rbp,[4+rbp] ; round++ + add r11d,r14d ; h+=Sigma0(a) mov r12d,DWORD[36+rsi] mov r13d,edx mov r14d,r11d @@ -447,38 +447,38 @@ $L$loop: xor r13d,edx ror r14d,9 - xor edi,r9d + xor edi,r9d ; f^g mov DWORD[36+rsp],r12d xor r14d,r11d - and edi,edx + and edi,edx ; (f^g)&e ror r13d,5 - add r12d,r10d - xor edi,r9d + add r12d,r10d ; T1+=h + xor edi,r9d ; Ch(e,f,g)=((f^g)&e)^g ror r14d,11 xor r13d,edx - add r12d,edi + add r12d,edi ; T1+=Ch(e,f,g) mov edi,r11d - add r12d,DWORD[rbp] + add r12d,DWORD[rbp] ; T1+=K[round] xor r14d,r11d - xor edi,eax - ror r13d,6 + xor edi,eax ; a^b, b^c in next round + ror r13d,6 ; Sigma1(e) mov r10d,eax and r15d,edi - ror r14d,2 - add r12d,r13d + ror r14d,2 ; Sigma0(a) + add r12d,r13d ; T1+=Sigma1(e) - xor r10d,r15d - add ecx,r12d - add r10d,r12d + xor r10d,r15d ; h=Maj(a,b,c)=Ch(a^b,c,b) + add ecx,r12d ; d+=T1 + add r10d,r12d ; h+=T1 - lea rbp,[4+rbp] - add r10d,r14d + lea rbp,[4+rbp] ; round++ + add r10d,r14d ; h+=Sigma0(a) mov r12d,DWORD[40+rsi] mov r13d,ecx mov r14d,r10d @@ -488,38 +488,38 @@ $L$loop: xor r13d,ecx ror r14d,9 - xor r15d,r8d + xor r15d,r8d ; f^g mov DWORD[40+rsp],r12d xor r14d,r10d - and r15d,ecx + and r15d,ecx ; (f^g)&e ror r13d,5 - add r12d,r9d - xor r15d,r8d + add r12d,r9d ; T1+=h + xor r15d,r8d ; Ch(e,f,g)=((f^g)&e)^g ror r14d,11 xor r13d,ecx - add r12d,r15d + add r12d,r15d ; T1+=Ch(e,f,g) mov r15d,r10d - add r12d,DWORD[rbp] + add r12d,DWORD[rbp] ; T1+=K[round] xor r14d,r10d - xor r15d,r11d - ror r13d,6 + xor r15d,r11d ; a^b, b^c in next round + ror r13d,6 ; Sigma1(e) mov r9d,r11d and edi,r15d - ror r14d,2 - add r12d,r13d + ror r14d,2 ; Sigma0(a) + add r12d,r13d ; T1+=Sigma1(e) - xor r9d,edi - add ebx,r12d - add r9d,r12d + xor r9d,edi ; h=Maj(a,b,c)=Ch(a^b,c,b) + add ebx,r12d ; d+=T1 + add r9d,r12d ; h+=T1 - lea rbp,[4+rbp] - add r9d,r14d + lea rbp,[4+rbp] ; round++ + add r9d,r14d ; h+=Sigma0(a) mov r12d,DWORD[44+rsi] mov r13d,ebx mov r14d,r9d @@ -529,38 +529,38 @@ $L$loop: xor r13d,ebx ror r14d,9 - xor edi,edx + xor edi,edx ; f^g mov DWORD[44+rsp],r12d xor r14d,r9d - and edi,ebx + and edi,ebx ; (f^g)&e ror r13d,5 - add r12d,r8d - xor edi,edx + add r12d,r8d ; T1+=h + xor edi,edx ; Ch(e,f,g)=((f^g)&e)^g ror r14d,11 xor r13d,ebx - add r12d,edi + add r12d,edi ; T1+=Ch(e,f,g) mov edi,r9d - add r12d,DWORD[rbp] + add r12d,DWORD[rbp] ; T1+=K[round] xor r14d,r9d - xor edi,r10d - ror r13d,6 + xor edi,r10d ; a^b, b^c in next round + ror r13d,6 ; Sigma1(e) mov r8d,r10d and r15d,edi - ror r14d,2 - add r12d,r13d + ror r14d,2 ; Sigma0(a) + add r12d,r13d ; T1+=Sigma1(e) - xor r8d,r15d - add eax,r12d - add r8d,r12d + xor r8d,r15d ; h=Maj(a,b,c)=Ch(a^b,c,b) + add eax,r12d ; d+=T1 + add r8d,r12d ; h+=T1 - lea rbp,[20+rbp] - add r8d,r14d + lea rbp,[20+rbp] ; round++ + add r8d,r14d ; h+=Sigma0(a) mov r12d,DWORD[48+rsi] mov r13d,eax mov r14d,r8d @@ -570,38 +570,38 @@ $L$loop: xor r13d,eax ror r14d,9 - xor r15d,ecx + xor r15d,ecx ; f^g mov DWORD[48+rsp],r12d xor r14d,r8d - and r15d,eax + and r15d,eax ; (f^g)&e ror r13d,5 - add r12d,edx - xor r15d,ecx + add r12d,edx ; T1+=h + xor r15d,ecx ; Ch(e,f,g)=((f^g)&e)^g ror r14d,11 xor r13d,eax - add r12d,r15d + add r12d,r15d ; T1+=Ch(e,f,g) mov r15d,r8d - add r12d,DWORD[rbp] + add r12d,DWORD[rbp] ; T1+=K[round] xor r14d,r8d - xor r15d,r9d - ror r13d,6 + xor r15d,r9d ; a^b, b^c in next round + ror r13d,6 ; Sigma1(e) mov edx,r9d and edi,r15d - ror r14d,2 - add r12d,r13d + ror r14d,2 ; Sigma0(a) + add r12d,r13d ; T1+=Sigma1(e) - xor edx,edi - add r11d,r12d - add edx,r12d + xor edx,edi ; h=Maj(a,b,c)=Ch(a^b,c,b) + add r11d,r12d ; d+=T1 + add edx,r12d ; h+=T1 - lea rbp,[4+rbp] - add edx,r14d + lea rbp,[4+rbp] ; round++ + add edx,r14d ; h+=Sigma0(a) mov r12d,DWORD[52+rsi] mov r13d,r11d mov r14d,edx @@ -611,38 +611,38 @@ $L$loop: xor r13d,r11d ror r14d,9 - xor edi,ebx + xor edi,ebx ; f^g mov DWORD[52+rsp],r12d xor r14d,edx - and edi,r11d + and edi,r11d ; (f^g)&e ror r13d,5 - add r12d,ecx - xor edi,ebx + add r12d,ecx ; T1+=h + xor edi,ebx ; Ch(e,f,g)=((f^g)&e)^g ror r14d,11 xor r13d,r11d - add r12d,edi + add r12d,edi ; T1+=Ch(e,f,g) mov edi,edx - add r12d,DWORD[rbp] + add r12d,DWORD[rbp] ; T1+=K[round] xor r14d,edx - xor edi,r8d - ror r13d,6 + xor edi,r8d ; a^b, b^c in next round + ror r13d,6 ; Sigma1(e) mov ecx,r8d and r15d,edi - ror r14d,2 - add r12d,r13d + ror r14d,2 ; Sigma0(a) + add r12d,r13d ; T1+=Sigma1(e) - xor ecx,r15d - add r10d,r12d - add ecx,r12d + xor ecx,r15d ; h=Maj(a,b,c)=Ch(a^b,c,b) + add r10d,r12d ; d+=T1 + add ecx,r12d ; h+=T1 - lea rbp,[4+rbp] - add ecx,r14d + lea rbp,[4+rbp] ; round++ + add ecx,r14d ; h+=Sigma0(a) mov r12d,DWORD[56+rsi] mov r13d,r10d mov r14d,ecx @@ -652,38 +652,38 @@ $L$loop: xor r13d,r10d ror r14d,9 - xor r15d,eax + xor r15d,eax ; f^g mov DWORD[56+rsp],r12d xor r14d,ecx - and r15d,r10d + and r15d,r10d ; (f^g)&e ror r13d,5 - add r12d,ebx - xor r15d,eax + add r12d,ebx ; T1+=h + xor r15d,eax ; Ch(e,f,g)=((f^g)&e)^g ror r14d,11 xor r13d,r10d - add r12d,r15d + add r12d,r15d ; T1+=Ch(e,f,g) mov r15d,ecx - add r12d,DWORD[rbp] + add r12d,DWORD[rbp] ; T1+=K[round] xor r14d,ecx - xor r15d,edx - ror r13d,6 + xor r15d,edx ; a^b, b^c in next round + ror r13d,6 ; Sigma1(e) mov ebx,edx and edi,r15d - ror r14d,2 - add r12d,r13d + ror r14d,2 ; Sigma0(a) + add r12d,r13d ; T1+=Sigma1(e) - xor ebx,edi - add r9d,r12d - add ebx,r12d + xor ebx,edi ; h=Maj(a,b,c)=Ch(a^b,c,b) + add r9d,r12d ; d+=T1 + add ebx,r12d ; h+=T1 - lea rbp,[4+rbp] - add ebx,r14d + lea rbp,[4+rbp] ; round++ + add ebx,r14d ; h+=Sigma0(a) mov r12d,DWORD[60+rsi] mov r13d,r9d mov r14d,ebx @@ -693,37 +693,37 @@ $L$loop: xor r13d,r9d ror r14d,9 - xor edi,r11d + xor edi,r11d ; f^g mov DWORD[60+rsp],r12d xor r14d,ebx - and edi,r9d + and edi,r9d ; (f^g)&e ror r13d,5 - add r12d,eax - xor edi,r11d + add r12d,eax ; T1+=h + xor edi,r11d ; Ch(e,f,g)=((f^g)&e)^g ror r14d,11 xor r13d,r9d - add r12d,edi + add r12d,edi ; T1+=Ch(e,f,g) mov edi,ebx - add r12d,DWORD[rbp] + add r12d,DWORD[rbp] ; T1+=K[round] xor r14d,ebx - xor edi,ecx - ror r13d,6 + xor edi,ecx ; a^b, b^c in next round + ror r13d,6 ; Sigma1(e) mov eax,ecx and r15d,edi - ror r14d,2 - add r12d,r13d + ror r14d,2 ; Sigma0(a) + add r12d,r13d ; T1+=Sigma1(e) - xor eax,r15d - add r8d,r12d - add eax,r12d + xor eax,r15d ; h=Maj(a,b,c)=Ch(a^b,c,b) + add r8d,r12d ; d+=T1 + add eax,r12d ; h+=T1 - lea rbp,[20+rbp] + lea rbp,[20+rbp] ; round++ jmp NEAR $L$rounds_16_xx ALIGN 16 $L$rounds_16_xx: @@ -732,7 +732,7 @@ $L$rounds_16_xx: mov r12d,r13d ror r13d,11 - add eax,r14d + add eax,r14d ; modulo-scheduled h+=Sigma0(a) mov r14d,r15d ror r15d,2 @@ -743,8 +743,8 @@ $L$rounds_16_xx: shr r14d,10 ror r15d,17 - xor r12d,r13d - xor r15d,r14d + xor r12d,r13d ; sigma0(X[(i+1)&0xf]) + xor r15d,r14d ; sigma1(X[(i+14)&0xf]) add r12d,DWORD[36+rsp] add r12d,DWORD[rsp] @@ -756,43 +756,43 @@ $L$rounds_16_xx: xor r13d,r8d ror r14d,9 - xor r15d,r10d + xor r15d,r10d ; f^g mov DWORD[rsp],r12d xor r14d,eax - and r15d,r8d + and r15d,r8d ; (f^g)&e ror r13d,5 - add r12d,r11d - xor r15d,r10d + add r12d,r11d ; T1+=h + xor r15d,r10d ; Ch(e,f,g)=((f^g)&e)^g ror r14d,11 xor r13d,r8d - add r12d,r15d + add r12d,r15d ; T1+=Ch(e,f,g) mov r15d,eax - add r12d,DWORD[rbp] + add r12d,DWORD[rbp] ; T1+=K[round] xor r14d,eax - xor r15d,ebx - ror r13d,6 + xor r15d,ebx ; a^b, b^c in next round + ror r13d,6 ; Sigma1(e) mov r11d,ebx and edi,r15d - ror r14d,2 - add r12d,r13d + ror r14d,2 ; Sigma0(a) + add r12d,r13d ; T1+=Sigma1(e) - xor r11d,edi - add edx,r12d - add r11d,r12d + xor r11d,edi ; h=Maj(a,b,c)=Ch(a^b,c,b) + add edx,r12d ; d+=T1 + add r11d,r12d ; h+=T1 - lea rbp,[4+rbp] + lea rbp,[4+rbp] ; round++ mov r13d,DWORD[8+rsp] mov edi,DWORD[60+rsp] mov r12d,r13d ror r13d,11 - add r11d,r14d + add r11d,r14d ; modulo-scheduled h+=Sigma0(a) mov r14d,edi ror edi,2 @@ -803,8 +803,8 @@ $L$rounds_16_xx: shr r14d,10 ror edi,17 - xor r12d,r13d - xor edi,r14d + xor r12d,r13d ; sigma0(X[(i+1)&0xf]) + xor edi,r14d ; sigma1(X[(i+14)&0xf]) add r12d,DWORD[40+rsp] add r12d,DWORD[4+rsp] @@ -816,43 +816,43 @@ $L$rounds_16_xx: xor r13d,edx ror r14d,9 - xor edi,r9d + xor edi,r9d ; f^g mov DWORD[4+rsp],r12d xor r14d,r11d - and edi,edx + and edi,edx ; (f^g)&e ror r13d,5 - add r12d,r10d - xor edi,r9d + add r12d,r10d ; T1+=h + xor edi,r9d ; Ch(e,f,g)=((f^g)&e)^g ror r14d,11 xor r13d,edx - add r12d,edi + add r12d,edi ; T1+=Ch(e,f,g) mov edi,r11d - add r12d,DWORD[rbp] + add r12d,DWORD[rbp] ; T1+=K[round] xor r14d,r11d - xor edi,eax - ror r13d,6 + xor edi,eax ; a^b, b^c in next round + ror r13d,6 ; Sigma1(e) mov r10d,eax and r15d,edi - ror r14d,2 - add r12d,r13d + ror r14d,2 ; Sigma0(a) + add r12d,r13d ; T1+=Sigma1(e) - xor r10d,r15d - add ecx,r12d - add r10d,r12d + xor r10d,r15d ; h=Maj(a,b,c)=Ch(a^b,c,b) + add ecx,r12d ; d+=T1 + add r10d,r12d ; h+=T1 - lea rbp,[4+rbp] + lea rbp,[4+rbp] ; round++ mov r13d,DWORD[12+rsp] mov r15d,DWORD[rsp] mov r12d,r13d ror r13d,11 - add r10d,r14d + add r10d,r14d ; modulo-scheduled h+=Sigma0(a) mov r14d,r15d ror r15d,2 @@ -863,8 +863,8 @@ $L$rounds_16_xx: shr r14d,10 ror r15d,17 - xor r12d,r13d - xor r15d,r14d + xor r12d,r13d ; sigma0(X[(i+1)&0xf]) + xor r15d,r14d ; sigma1(X[(i+14)&0xf]) add r12d,DWORD[44+rsp] add r12d,DWORD[8+rsp] @@ -876,43 +876,43 @@ $L$rounds_16_xx: xor r13d,ecx ror r14d,9 - xor r15d,r8d + xor r15d,r8d ; f^g mov DWORD[8+rsp],r12d xor r14d,r10d - and r15d,ecx + and r15d,ecx ; (f^g)&e ror r13d,5 - add r12d,r9d - xor r15d,r8d + add r12d,r9d ; T1+=h + xor r15d,r8d ; Ch(e,f,g)=((f^g)&e)^g ror r14d,11 xor r13d,ecx - add r12d,r15d + add r12d,r15d ; T1+=Ch(e,f,g) mov r15d,r10d - add r12d,DWORD[rbp] + add r12d,DWORD[rbp] ; T1+=K[round] xor r14d,r10d - xor r15d,r11d - ror r13d,6 + xor r15d,r11d ; a^b, b^c in next round + ror r13d,6 ; Sigma1(e) mov r9d,r11d and edi,r15d - ror r14d,2 - add r12d,r13d + ror r14d,2 ; Sigma0(a) + add r12d,r13d ; T1+=Sigma1(e) - xor r9d,edi - add ebx,r12d - add r9d,r12d + xor r9d,edi ; h=Maj(a,b,c)=Ch(a^b,c,b) + add ebx,r12d ; d+=T1 + add r9d,r12d ; h+=T1 - lea rbp,[4+rbp] + lea rbp,[4+rbp] ; round++ mov r13d,DWORD[16+rsp] mov edi,DWORD[4+rsp] mov r12d,r13d ror r13d,11 - add r9d,r14d + add r9d,r14d ; modulo-scheduled h+=Sigma0(a) mov r14d,edi ror edi,2 @@ -923,8 +923,8 @@ $L$rounds_16_xx: shr r14d,10 ror edi,17 - xor r12d,r13d - xor edi,r14d + xor r12d,r13d ; sigma0(X[(i+1)&0xf]) + xor edi,r14d ; sigma1(X[(i+14)&0xf]) add r12d,DWORD[48+rsp] add r12d,DWORD[12+rsp] @@ -936,43 +936,43 @@ $L$rounds_16_xx: xor r13d,ebx ror r14d,9 - xor edi,edx + xor edi,edx ; f^g mov DWORD[12+rsp],r12d xor r14d,r9d - and edi,ebx + and edi,ebx ; (f^g)&e ror r13d,5 - add r12d,r8d - xor edi,edx + add r12d,r8d ; T1+=h + xor edi,edx ; Ch(e,f,g)=((f^g)&e)^g ror r14d,11 xor r13d,ebx - add r12d,edi + add r12d,edi ; T1+=Ch(e,f,g) mov edi,r9d - add r12d,DWORD[rbp] + add r12d,DWORD[rbp] ; T1+=K[round] xor r14d,r9d - xor edi,r10d - ror r13d,6 + xor edi,r10d ; a^b, b^c in next round + ror r13d,6 ; Sigma1(e) mov r8d,r10d and r15d,edi - ror r14d,2 - add r12d,r13d + ror r14d,2 ; Sigma0(a) + add r12d,r13d ; T1+=Sigma1(e) - xor r8d,r15d - add eax,r12d - add r8d,r12d + xor r8d,r15d ; h=Maj(a,b,c)=Ch(a^b,c,b) + add eax,r12d ; d+=T1 + add r8d,r12d ; h+=T1 - lea rbp,[20+rbp] + lea rbp,[20+rbp] ; round++ mov r13d,DWORD[20+rsp] mov r15d,DWORD[8+rsp] mov r12d,r13d ror r13d,11 - add r8d,r14d + add r8d,r14d ; modulo-scheduled h+=Sigma0(a) mov r14d,r15d ror r15d,2 @@ -983,8 +983,8 @@ $L$rounds_16_xx: shr r14d,10 ror r15d,17 - xor r12d,r13d - xor r15d,r14d + xor r12d,r13d ; sigma0(X[(i+1)&0xf]) + xor r15d,r14d ; sigma1(X[(i+14)&0xf]) add r12d,DWORD[52+rsp] add r12d,DWORD[16+rsp] @@ -996,43 +996,43 @@ $L$rounds_16_xx: xor r13d,eax ror r14d,9 - xor r15d,ecx + xor r15d,ecx ; f^g mov DWORD[16+rsp],r12d xor r14d,r8d - and r15d,eax + and r15d,eax ; (f^g)&e ror r13d,5 - add r12d,edx - xor r15d,ecx + add r12d,edx ; T1+=h + xor r15d,ecx ; Ch(e,f,g)=((f^g)&e)^g ror r14d,11 xor r13d,eax - add r12d,r15d + add r12d,r15d ; T1+=Ch(e,f,g) mov r15d,r8d - add r12d,DWORD[rbp] + add r12d,DWORD[rbp] ; T1+=K[round] xor r14d,r8d - xor r15d,r9d - ror r13d,6 + xor r15d,r9d ; a^b, b^c in next round + ror r13d,6 ; Sigma1(e) mov edx,r9d and edi,r15d - ror r14d,2 - add r12d,r13d + ror r14d,2 ; Sigma0(a) + add r12d,r13d ; T1+=Sigma1(e) - xor edx,edi - add r11d,r12d - add edx,r12d + xor edx,edi ; h=Maj(a,b,c)=Ch(a^b,c,b) + add r11d,r12d ; d+=T1 + add edx,r12d ; h+=T1 - lea rbp,[4+rbp] + lea rbp,[4+rbp] ; round++ mov r13d,DWORD[24+rsp] mov edi,DWORD[12+rsp] mov r12d,r13d ror r13d,11 - add edx,r14d + add edx,r14d ; modulo-scheduled h+=Sigma0(a) mov r14d,edi ror edi,2 @@ -1043,8 +1043,8 @@ $L$rounds_16_xx: shr r14d,10 ror edi,17 - xor r12d,r13d - xor edi,r14d + xor r12d,r13d ; sigma0(X[(i+1)&0xf]) + xor edi,r14d ; sigma1(X[(i+14)&0xf]) add r12d,DWORD[56+rsp] add r12d,DWORD[20+rsp] @@ -1056,43 +1056,43 @@ $L$rounds_16_xx: xor r13d,r11d ror r14d,9 - xor edi,ebx + xor edi,ebx ; f^g mov DWORD[20+rsp],r12d xor r14d,edx - and edi,r11d + and edi,r11d ; (f^g)&e ror r13d,5 - add r12d,ecx - xor edi,ebx + add r12d,ecx ; T1+=h + xor edi,ebx ; Ch(e,f,g)=((f^g)&e)^g ror r14d,11 xor r13d,r11d - add r12d,edi + add r12d,edi ; T1+=Ch(e,f,g) mov edi,edx - add r12d,DWORD[rbp] + add r12d,DWORD[rbp] ; T1+=K[round] xor r14d,edx - xor edi,r8d - ror r13d,6 + xor edi,r8d ; a^b, b^c in next round + ror r13d,6 ; Sigma1(e) mov ecx,r8d and r15d,edi - ror r14d,2 - add r12d,r13d + ror r14d,2 ; Sigma0(a) + add r12d,r13d ; T1+=Sigma1(e) - xor ecx,r15d - add r10d,r12d - add ecx,r12d + xor ecx,r15d ; h=Maj(a,b,c)=Ch(a^b,c,b) + add r10d,r12d ; d+=T1 + add ecx,r12d ; h+=T1 - lea rbp,[4+rbp] + lea rbp,[4+rbp] ; round++ mov r13d,DWORD[28+rsp] mov r15d,DWORD[16+rsp] mov r12d,r13d ror r13d,11 - add ecx,r14d + add ecx,r14d ; modulo-scheduled h+=Sigma0(a) mov r14d,r15d ror r15d,2 @@ -1103,8 +1103,8 @@ $L$rounds_16_xx: shr r14d,10 ror r15d,17 - xor r12d,r13d - xor r15d,r14d + xor r12d,r13d ; sigma0(X[(i+1)&0xf]) + xor r15d,r14d ; sigma1(X[(i+14)&0xf]) add r12d,DWORD[60+rsp] add r12d,DWORD[24+rsp] @@ -1116,43 +1116,43 @@ $L$rounds_16_xx: xor r13d,r10d ror r14d,9 - xor r15d,eax + xor r15d,eax ; f^g mov DWORD[24+rsp],r12d xor r14d,ecx - and r15d,r10d + and r15d,r10d ; (f^g)&e ror r13d,5 - add r12d,ebx - xor r15d,eax + add r12d,ebx ; T1+=h + xor r15d,eax ; Ch(e,f,g)=((f^g)&e)^g ror r14d,11 xor r13d,r10d - add r12d,r15d + add r12d,r15d ; T1+=Ch(e,f,g) mov r15d,ecx - add r12d,DWORD[rbp] + add r12d,DWORD[rbp] ; T1+=K[round] xor r14d,ecx - xor r15d,edx - ror r13d,6 + xor r15d,edx ; a^b, b^c in next round + ror r13d,6 ; Sigma1(e) mov ebx,edx and edi,r15d - ror r14d,2 - add r12d,r13d + ror r14d,2 ; Sigma0(a) + add r12d,r13d ; T1+=Sigma1(e) - xor ebx,edi - add r9d,r12d - add ebx,r12d + xor ebx,edi ; h=Maj(a,b,c)=Ch(a^b,c,b) + add r9d,r12d ; d+=T1 + add ebx,r12d ; h+=T1 - lea rbp,[4+rbp] + lea rbp,[4+rbp] ; round++ mov r13d,DWORD[32+rsp] mov edi,DWORD[20+rsp] mov r12d,r13d ror r13d,11 - add ebx,r14d + add ebx,r14d ; modulo-scheduled h+=Sigma0(a) mov r14d,edi ror edi,2 @@ -1163,8 +1163,8 @@ $L$rounds_16_xx: shr r14d,10 ror edi,17 - xor r12d,r13d - xor edi,r14d + xor r12d,r13d ; sigma0(X[(i+1)&0xf]) + xor edi,r14d ; sigma1(X[(i+14)&0xf]) add r12d,DWORD[rsp] add r12d,DWORD[28+rsp] @@ -1176,43 +1176,43 @@ $L$rounds_16_xx: xor r13d,r9d ror r14d,9 - xor edi,r11d + xor edi,r11d ; f^g mov DWORD[28+rsp],r12d xor r14d,ebx - and edi,r9d + and edi,r9d ; (f^g)&e ror r13d,5 - add r12d,eax - xor edi,r11d + add r12d,eax ; T1+=h + xor edi,r11d ; Ch(e,f,g)=((f^g)&e)^g ror r14d,11 xor r13d,r9d - add r12d,edi + add r12d,edi ; T1+=Ch(e,f,g) mov edi,ebx - add r12d,DWORD[rbp] + add r12d,DWORD[rbp] ; T1+=K[round] xor r14d,ebx - xor edi,ecx - ror r13d,6 + xor edi,ecx ; a^b, b^c in next round + ror r13d,6 ; Sigma1(e) mov eax,ecx and r15d,edi - ror r14d,2 - add r12d,r13d + ror r14d,2 ; Sigma0(a) + add r12d,r13d ; T1+=Sigma1(e) - xor eax,r15d - add r8d,r12d - add eax,r12d + xor eax,r15d ; h=Maj(a,b,c)=Ch(a^b,c,b) + add r8d,r12d ; d+=T1 + add eax,r12d ; h+=T1 - lea rbp,[20+rbp] + lea rbp,[20+rbp] ; round++ mov r13d,DWORD[36+rsp] mov r15d,DWORD[24+rsp] mov r12d,r13d ror r13d,11 - add eax,r14d + add eax,r14d ; modulo-scheduled h+=Sigma0(a) mov r14d,r15d ror r15d,2 @@ -1223,8 +1223,8 @@ $L$rounds_16_xx: shr r14d,10 ror r15d,17 - xor r12d,r13d - xor r15d,r14d + xor r12d,r13d ; sigma0(X[(i+1)&0xf]) + xor r15d,r14d ; sigma1(X[(i+14)&0xf]) add r12d,DWORD[4+rsp] add r12d,DWORD[32+rsp] @@ -1236,43 +1236,43 @@ $L$rounds_16_xx: xor r13d,r8d ror r14d,9 - xor r15d,r10d + xor r15d,r10d ; f^g mov DWORD[32+rsp],r12d xor r14d,eax - and r15d,r8d + and r15d,r8d ; (f^g)&e ror r13d,5 - add r12d,r11d - xor r15d,r10d + add r12d,r11d ; T1+=h + xor r15d,r10d ; Ch(e,f,g)=((f^g)&e)^g ror r14d,11 xor r13d,r8d - add r12d,r15d + add r12d,r15d ; T1+=Ch(e,f,g) mov r15d,eax - add r12d,DWORD[rbp] + add r12d,DWORD[rbp] ; T1+=K[round] xor r14d,eax - xor r15d,ebx - ror r13d,6 + xor r15d,ebx ; a^b, b^c in next round + ror r13d,6 ; Sigma1(e) mov r11d,ebx and edi,r15d - ror r14d,2 - add r12d,r13d + ror r14d,2 ; Sigma0(a) + add r12d,r13d ; T1+=Sigma1(e) - xor r11d,edi - add edx,r12d - add r11d,r12d + xor r11d,edi ; h=Maj(a,b,c)=Ch(a^b,c,b) + add edx,r12d ; d+=T1 + add r11d,r12d ; h+=T1 - lea rbp,[4+rbp] + lea rbp,[4+rbp] ; round++ mov r13d,DWORD[40+rsp] mov edi,DWORD[28+rsp] mov r12d,r13d ror r13d,11 - add r11d,r14d + add r11d,r14d ; modulo-scheduled h+=Sigma0(a) mov r14d,edi ror edi,2 @@ -1283,8 +1283,8 @@ $L$rounds_16_xx: shr r14d,10 ror edi,17 - xor r12d,r13d - xor edi,r14d + xor r12d,r13d ; sigma0(X[(i+1)&0xf]) + xor edi,r14d ; sigma1(X[(i+14)&0xf]) add r12d,DWORD[8+rsp] add r12d,DWORD[36+rsp] @@ -1296,43 +1296,43 @@ $L$rounds_16_xx: xor r13d,edx ror r14d,9 - xor edi,r9d + xor edi,r9d ; f^g mov DWORD[36+rsp],r12d xor r14d,r11d - and edi,edx + and edi,edx ; (f^g)&e ror r13d,5 - add r12d,r10d - xor edi,r9d + add r12d,r10d ; T1+=h + xor edi,r9d ; Ch(e,f,g)=((f^g)&e)^g ror r14d,11 xor r13d,edx - add r12d,edi + add r12d,edi ; T1+=Ch(e,f,g) mov edi,r11d - add r12d,DWORD[rbp] + add r12d,DWORD[rbp] ; T1+=K[round] xor r14d,r11d - xor edi,eax - ror r13d,6 + xor edi,eax ; a^b, b^c in next round + ror r13d,6 ; Sigma1(e) mov r10d,eax and r15d,edi - ror r14d,2 - add r12d,r13d + ror r14d,2 ; Sigma0(a) + add r12d,r13d ; T1+=Sigma1(e) - xor r10d,r15d - add ecx,r12d - add r10d,r12d + xor r10d,r15d ; h=Maj(a,b,c)=Ch(a^b,c,b) + add ecx,r12d ; d+=T1 + add r10d,r12d ; h+=T1 - lea rbp,[4+rbp] + lea rbp,[4+rbp] ; round++ mov r13d,DWORD[44+rsp] mov r15d,DWORD[32+rsp] mov r12d,r13d ror r13d,11 - add r10d,r14d + add r10d,r14d ; modulo-scheduled h+=Sigma0(a) mov r14d,r15d ror r15d,2 @@ -1343,8 +1343,8 @@ $L$rounds_16_xx: shr r14d,10 ror r15d,17 - xor r12d,r13d - xor r15d,r14d + xor r12d,r13d ; sigma0(X[(i+1)&0xf]) + xor r15d,r14d ; sigma1(X[(i+14)&0xf]) add r12d,DWORD[12+rsp] add r12d,DWORD[40+rsp] @@ -1356,43 +1356,43 @@ $L$rounds_16_xx: xor r13d,ecx ror r14d,9 - xor r15d,r8d + xor r15d,r8d ; f^g mov DWORD[40+rsp],r12d xor r14d,r10d - and r15d,ecx + and r15d,ecx ; (f^g)&e ror r13d,5 - add r12d,r9d - xor r15d,r8d + add r12d,r9d ; T1+=h + xor r15d,r8d ; Ch(e,f,g)=((f^g)&e)^g ror r14d,11 xor r13d,ecx - add r12d,r15d + add r12d,r15d ; T1+=Ch(e,f,g) mov r15d,r10d - add r12d,DWORD[rbp] + add r12d,DWORD[rbp] ; T1+=K[round] xor r14d,r10d - xor r15d,r11d - ror r13d,6 + xor r15d,r11d ; a^b, b^c in next round + ror r13d,6 ; Sigma1(e) mov r9d,r11d and edi,r15d - ror r14d,2 - add r12d,r13d + ror r14d,2 ; Sigma0(a) + add r12d,r13d ; T1+=Sigma1(e) - xor r9d,edi - add ebx,r12d - add r9d,r12d + xor r9d,edi ; h=Maj(a,b,c)=Ch(a^b,c,b) + add ebx,r12d ; d+=T1 + add r9d,r12d ; h+=T1 - lea rbp,[4+rbp] + lea rbp,[4+rbp] ; round++ mov r13d,DWORD[48+rsp] mov edi,DWORD[36+rsp] mov r12d,r13d ror r13d,11 - add r9d,r14d + add r9d,r14d ; modulo-scheduled h+=Sigma0(a) mov r14d,edi ror edi,2 @@ -1403,8 +1403,8 @@ $L$rounds_16_xx: shr r14d,10 ror edi,17 - xor r12d,r13d - xor edi,r14d + xor r12d,r13d ; sigma0(X[(i+1)&0xf]) + xor edi,r14d ; sigma1(X[(i+14)&0xf]) add r12d,DWORD[16+rsp] add r12d,DWORD[44+rsp] @@ -1416,43 +1416,43 @@ $L$rounds_16_xx: xor r13d,ebx ror r14d,9 - xor edi,edx + xor edi,edx ; f^g mov DWORD[44+rsp],r12d xor r14d,r9d - and edi,ebx + and edi,ebx ; (f^g)&e ror r13d,5 - add r12d,r8d - xor edi,edx + add r12d,r8d ; T1+=h + xor edi,edx ; Ch(e,f,g)=((f^g)&e)^g ror r14d,11 xor r13d,ebx - add r12d,edi + add r12d,edi ; T1+=Ch(e,f,g) mov edi,r9d - add r12d,DWORD[rbp] + add r12d,DWORD[rbp] ; T1+=K[round] xor r14d,r9d - xor edi,r10d - ror r13d,6 + xor edi,r10d ; a^b, b^c in next round + ror r13d,6 ; Sigma1(e) mov r8d,r10d and r15d,edi - ror r14d,2 - add r12d,r13d + ror r14d,2 ; Sigma0(a) + add r12d,r13d ; T1+=Sigma1(e) - xor r8d,r15d - add eax,r12d - add r8d,r12d + xor r8d,r15d ; h=Maj(a,b,c)=Ch(a^b,c,b) + add eax,r12d ; d+=T1 + add r8d,r12d ; h+=T1 - lea rbp,[20+rbp] + lea rbp,[20+rbp] ; round++ mov r13d,DWORD[52+rsp] mov r15d,DWORD[40+rsp] mov r12d,r13d ror r13d,11 - add r8d,r14d + add r8d,r14d ; modulo-scheduled h+=Sigma0(a) mov r14d,r15d ror r15d,2 @@ -1463,8 +1463,8 @@ $L$rounds_16_xx: shr r14d,10 ror r15d,17 - xor r12d,r13d - xor r15d,r14d + xor r12d,r13d ; sigma0(X[(i+1)&0xf]) + xor r15d,r14d ; sigma1(X[(i+14)&0xf]) add r12d,DWORD[20+rsp] add r12d,DWORD[48+rsp] @@ -1476,43 +1476,43 @@ $L$rounds_16_xx: xor r13d,eax ror r14d,9 - xor r15d,ecx + xor r15d,ecx ; f^g mov DWORD[48+rsp],r12d xor r14d,r8d - and r15d,eax + and r15d,eax ; (f^g)&e ror r13d,5 - add r12d,edx - xor r15d,ecx + add r12d,edx ; T1+=h + xor r15d,ecx ; Ch(e,f,g)=((f^g)&e)^g ror r14d,11 xor r13d,eax - add r12d,r15d + add r12d,r15d ; T1+=Ch(e,f,g) mov r15d,r8d - add r12d,DWORD[rbp] + add r12d,DWORD[rbp] ; T1+=K[round] xor r14d,r8d - xor r15d,r9d - ror r13d,6 + xor r15d,r9d ; a^b, b^c in next round + ror r13d,6 ; Sigma1(e) mov edx,r9d and edi,r15d - ror r14d,2 - add r12d,r13d + ror r14d,2 ; Sigma0(a) + add r12d,r13d ; T1+=Sigma1(e) - xor edx,edi - add r11d,r12d - add edx,r12d + xor edx,edi ; h=Maj(a,b,c)=Ch(a^b,c,b) + add r11d,r12d ; d+=T1 + add edx,r12d ; h+=T1 - lea rbp,[4+rbp] + lea rbp,[4+rbp] ; round++ mov r13d,DWORD[56+rsp] mov edi,DWORD[44+rsp] mov r12d,r13d ror r13d,11 - add edx,r14d + add edx,r14d ; modulo-scheduled h+=Sigma0(a) mov r14d,edi ror edi,2 @@ -1523,8 +1523,8 @@ $L$rounds_16_xx: shr r14d,10 ror edi,17 - xor r12d,r13d - xor edi,r14d + xor r12d,r13d ; sigma0(X[(i+1)&0xf]) + xor edi,r14d ; sigma1(X[(i+14)&0xf]) add r12d,DWORD[24+rsp] add r12d,DWORD[52+rsp] @@ -1536,43 +1536,43 @@ $L$rounds_16_xx: xor r13d,r11d ror r14d,9 - xor edi,ebx + xor edi,ebx ; f^g mov DWORD[52+rsp],r12d xor r14d,edx - and edi,r11d + and edi,r11d ; (f^g)&e ror r13d,5 - add r12d,ecx - xor edi,ebx + add r12d,ecx ; T1+=h + xor edi,ebx ; Ch(e,f,g)=((f^g)&e)^g ror r14d,11 xor r13d,r11d - add r12d,edi + add r12d,edi ; T1+=Ch(e,f,g) mov edi,edx - add r12d,DWORD[rbp] + add r12d,DWORD[rbp] ; T1+=K[round] xor r14d,edx - xor edi,r8d - ror r13d,6 + xor edi,r8d ; a^b, b^c in next round + ror r13d,6 ; Sigma1(e) mov ecx,r8d and r15d,edi - ror r14d,2 - add r12d,r13d + ror r14d,2 ; Sigma0(a) + add r12d,r13d ; T1+=Sigma1(e) - xor ecx,r15d - add r10d,r12d - add ecx,r12d + xor ecx,r15d ; h=Maj(a,b,c)=Ch(a^b,c,b) + add r10d,r12d ; d+=T1 + add ecx,r12d ; h+=T1 - lea rbp,[4+rbp] + lea rbp,[4+rbp] ; round++ mov r13d,DWORD[60+rsp] mov r15d,DWORD[48+rsp] mov r12d,r13d ror r13d,11 - add ecx,r14d + add ecx,r14d ; modulo-scheduled h+=Sigma0(a) mov r14d,r15d ror r15d,2 @@ -1583,8 +1583,8 @@ $L$rounds_16_xx: shr r14d,10 ror r15d,17 - xor r12d,r13d - xor r15d,r14d + xor r12d,r13d ; sigma0(X[(i+1)&0xf]) + xor r15d,r14d ; sigma1(X[(i+14)&0xf]) add r12d,DWORD[28+rsp] add r12d,DWORD[56+rsp] @@ -1596,43 +1596,43 @@ $L$rounds_16_xx: xor r13d,r10d ror r14d,9 - xor r15d,eax + xor r15d,eax ; f^g mov DWORD[56+rsp],r12d xor r14d,ecx - and r15d,r10d + and r15d,r10d ; (f^g)&e ror r13d,5 - add r12d,ebx - xor r15d,eax + add r12d,ebx ; T1+=h + xor r15d,eax ; Ch(e,f,g)=((f^g)&e)^g ror r14d,11 xor r13d,r10d - add r12d,r15d + add r12d,r15d ; T1+=Ch(e,f,g) mov r15d,ecx - add r12d,DWORD[rbp] + add r12d,DWORD[rbp] ; T1+=K[round] xor r14d,ecx - xor r15d,edx - ror r13d,6 + xor r15d,edx ; a^b, b^c in next round + ror r13d,6 ; Sigma1(e) mov ebx,edx and edi,r15d - ror r14d,2 - add r12d,r13d + ror r14d,2 ; Sigma0(a) + add r12d,r13d ; T1+=Sigma1(e) - xor ebx,edi - add r9d,r12d - add ebx,r12d + xor ebx,edi ; h=Maj(a,b,c)=Ch(a^b,c,b) + add r9d,r12d ; d+=T1 + add ebx,r12d ; h+=T1 - lea rbp,[4+rbp] + lea rbp,[4+rbp] ; round++ mov r13d,DWORD[rsp] mov edi,DWORD[52+rsp] mov r12d,r13d ror r13d,11 - add ebx,r14d + add ebx,r14d ; modulo-scheduled h+=Sigma0(a) mov r14d,edi ror edi,2 @@ -1643,8 +1643,8 @@ $L$rounds_16_xx: shr r14d,10 ror edi,17 - xor r12d,r13d - xor edi,r14d + xor r12d,r13d ; sigma0(X[(i+1)&0xf]) + xor edi,r14d ; sigma1(X[(i+14)&0xf]) add r12d,DWORD[32+rsp] add r12d,DWORD[60+rsp] @@ -1656,42 +1656,42 @@ $L$rounds_16_xx: xor r13d,r9d ror r14d,9 - xor edi,r11d + xor edi,r11d ; f^g mov DWORD[60+rsp],r12d xor r14d,ebx - and edi,r9d + and edi,r9d ; (f^g)&e ror r13d,5 - add r12d,eax - xor edi,r11d + add r12d,eax ; T1+=h + xor edi,r11d ; Ch(e,f,g)=((f^g)&e)^g ror r14d,11 xor r13d,r9d - add r12d,edi + add r12d,edi ; T1+=Ch(e,f,g) mov edi,ebx - add r12d,DWORD[rbp] + add r12d,DWORD[rbp] ; T1+=K[round] xor r14d,ebx - xor edi,ecx - ror r13d,6 + xor edi,ecx ; a^b, b^c in next round + ror r13d,6 ; Sigma1(e) mov eax,ecx and r15d,edi - ror r14d,2 - add r12d,r13d + ror r14d,2 ; Sigma0(a) + add r12d,r13d ; T1+=Sigma1(e) - xor eax,r15d - add r8d,r12d - add eax,r12d + xor eax,r15d ; h=Maj(a,b,c)=Ch(a^b,c,b) + add r8d,r12d ; d+=T1 + add eax,r12d ; h+=T1 - lea rbp,[20+rbp] + lea rbp,[20+rbp] ; round++ cmp BYTE[3+rbp],0 jnz NEAR $L$rounds_16_xx mov rdi,QWORD[((64+0))+rsp] - add eax,r14d + add eax,r14d ; modulo-scheduled h+=Sigma0(a) lea rsi,[64+rsi] add eax,DWORD[rdi] @@ -1785,7 +1785,7 @@ K256: DB 52,44,32,67,82,89,80,84,79,71,65,77,83,32,98,121 DB 32,60,97,112,112,114,111,64,111,112,101,110,115,115,108,46 DB 111,114,103,62,0 -section .text +section .text code align=64 global sha256_block_data_order_hw @@ -1810,16 +1810,16 @@ _CET_ENDBR movaps XMMWORD[(-8-16)+rax],xmm10 $L$prologue_shaext: lea rcx,[((K256+128))] - movdqu xmm1,XMMWORD[rdi] - movdqu xmm2,XMMWORD[16+rdi] - movdqa xmm7,XMMWORD[((512-128))+rcx] - - pshufd xmm0,xmm1,0x1b - pshufd xmm1,xmm1,0xb1 - pshufd xmm2,xmm2,0x1b - movdqa xmm8,xmm7 - palignr xmm1,xmm2,8 - punpcklqdq xmm2,xmm0 + movdqu xmm1,XMMWORD[rdi] ; DCBA + movdqu xmm2,XMMWORD[16+rdi] ; HGFE + movdqa xmm7,XMMWORD[((512-128))+rcx] ; byte swap mask + + pshufd xmm0,xmm1,0x1b ; ABCD + pshufd xmm1,xmm1,0xb1 ; CDAB + pshufd xmm2,xmm2,0x1b ; EFGH + movdqa xmm8,xmm7 ; offload + palignr xmm1,xmm2,8 ; ABEF + punpcklqdq xmm2,xmm0 ; CDGH jmp NEAR $L$oop_shaext ALIGN 16 @@ -1833,17 +1833,17 @@ $L$oop_shaext: movdqa xmm0,XMMWORD[((0-128))+rcx] paddd xmm0,xmm3 pshufb xmm4,xmm7 - movdqa xmm10,xmm2 - sha256rnds2 xmm2,xmm1 + movdqa xmm10,xmm2 ; offload + sha256rnds2 xmm2,xmm1 ; 0-3 pshufd xmm0,xmm0,0x0e nop - movdqa xmm9,xmm1 + movdqa xmm9,xmm1 ; offload sha256rnds2 xmm1,xmm2 movdqa xmm0,XMMWORD[((32-128))+rcx] paddd xmm0,xmm4 pshufb xmm5,xmm7 - sha256rnds2 xmm2,xmm1 + sha256rnds2 xmm2,xmm1 ; 4-7 pshufd xmm0,xmm0,0x0e lea rsi,[64+rsi] sha256msg1 xmm3,xmm4 @@ -1852,7 +1852,7 @@ $L$oop_shaext: movdqa xmm0,XMMWORD[((64-128))+rcx] paddd xmm0,xmm5 pshufb xmm6,xmm7 - sha256rnds2 xmm2,xmm1 + sha256rnds2 xmm2,xmm1 ; 8-11 pshufd xmm0,xmm0,0x0e movdqa xmm7,xmm6 palignr xmm7,xmm5,4 @@ -1864,7 +1864,7 @@ $L$oop_shaext: movdqa xmm0,XMMWORD[((96-128))+rcx] paddd xmm0,xmm6 sha256msg2 xmm3,xmm6 - sha256rnds2 xmm2,xmm1 + sha256rnds2 xmm2,xmm1 ; 12-15 pshufd xmm0,xmm0,0x0e movdqa xmm7,xmm3 palignr xmm7,xmm6,4 @@ -1875,7 +1875,7 @@ $L$oop_shaext: movdqa xmm0,XMMWORD[((128-128))+rcx] paddd xmm0,xmm3 sha256msg2 xmm4,xmm3 - sha256rnds2 xmm2,xmm1 + sha256rnds2 xmm2,xmm1 ; 16-19... pshufd xmm0,xmm0,0x0e movdqa xmm7,xmm4 palignr xmm7,xmm3,4 @@ -1886,7 +1886,7 @@ $L$oop_shaext: movdqa xmm0,XMMWORD[((160-128))+rcx] paddd xmm0,xmm4 sha256msg2 xmm5,xmm4 - sha256rnds2 xmm2,xmm1 + sha256rnds2 xmm2,xmm1 ; 16-19... pshufd xmm0,xmm0,0x0e movdqa xmm7,xmm5 palignr xmm7,xmm4,4 @@ -1897,7 +1897,7 @@ $L$oop_shaext: movdqa xmm0,XMMWORD[((192-128))+rcx] paddd xmm0,xmm5 sha256msg2 xmm6,xmm5 - sha256rnds2 xmm2,xmm1 + sha256rnds2 xmm2,xmm1 ; 16-19... pshufd xmm0,xmm0,0x0e movdqa xmm7,xmm6 palignr xmm7,xmm5,4 @@ -1908,7 +1908,7 @@ $L$oop_shaext: movdqa xmm0,XMMWORD[((224-128))+rcx] paddd xmm0,xmm6 sha256msg2 xmm3,xmm6 - sha256rnds2 xmm2,xmm1 + sha256rnds2 xmm2,xmm1 ; 16-19... pshufd xmm0,xmm0,0x0e movdqa xmm7,xmm3 palignr xmm7,xmm6,4 @@ -1919,7 +1919,7 @@ $L$oop_shaext: movdqa xmm0,XMMWORD[((256-128))+rcx] paddd xmm0,xmm3 sha256msg2 xmm4,xmm3 - sha256rnds2 xmm2,xmm1 + sha256rnds2 xmm2,xmm1 ; 16-19... pshufd xmm0,xmm0,0x0e movdqa xmm7,xmm4 palignr xmm7,xmm3,4 @@ -1930,7 +1930,7 @@ $L$oop_shaext: movdqa xmm0,XMMWORD[((288-128))+rcx] paddd xmm0,xmm4 sha256msg2 xmm5,xmm4 - sha256rnds2 xmm2,xmm1 + sha256rnds2 xmm2,xmm1 ; 16-19... pshufd xmm0,xmm0,0x0e movdqa xmm7,xmm5 palignr xmm7,xmm4,4 @@ -1941,7 +1941,7 @@ $L$oop_shaext: movdqa xmm0,XMMWORD[((320-128))+rcx] paddd xmm0,xmm5 sha256msg2 xmm6,xmm5 - sha256rnds2 xmm2,xmm1 + sha256rnds2 xmm2,xmm1 ; 16-19... pshufd xmm0,xmm0,0x0e movdqa xmm7,xmm6 palignr xmm7,xmm5,4 @@ -1952,7 +1952,7 @@ $L$oop_shaext: movdqa xmm0,XMMWORD[((352-128))+rcx] paddd xmm0,xmm6 sha256msg2 xmm3,xmm6 - sha256rnds2 xmm2,xmm1 + sha256rnds2 xmm2,xmm1 ; 16-19... pshufd xmm0,xmm0,0x0e movdqa xmm7,xmm3 palignr xmm7,xmm6,4 @@ -1963,7 +1963,7 @@ $L$oop_shaext: movdqa xmm0,XMMWORD[((384-128))+rcx] paddd xmm0,xmm3 sha256msg2 xmm4,xmm3 - sha256rnds2 xmm2,xmm1 + sha256rnds2 xmm2,xmm1 ; 16-19... pshufd xmm0,xmm0,0x0e movdqa xmm7,xmm4 palignr xmm7,xmm3,4 @@ -1974,7 +1974,7 @@ $L$oop_shaext: movdqa xmm0,XMMWORD[((416-128))+rcx] paddd xmm0,xmm4 sha256msg2 xmm5,xmm4 - sha256rnds2 xmm2,xmm1 + sha256rnds2 xmm2,xmm1 ; 52-55 pshufd xmm0,xmm0,0x0e movdqa xmm7,xmm5 palignr xmm7,xmm4,4 @@ -1983,7 +1983,7 @@ $L$oop_shaext: movdqa xmm0,XMMWORD[((448-128))+rcx] paddd xmm0,xmm5 - sha256rnds2 xmm2,xmm1 + sha256rnds2 xmm2,xmm1 ; 56-59 pshufd xmm0,xmm0,0x0e sha256msg2 xmm6,xmm5 movdqa xmm7,xmm8 @@ -1992,7 +1992,7 @@ $L$oop_shaext: movdqa xmm0,XMMWORD[((480-128))+rcx] paddd xmm0,xmm6 nop - sha256rnds2 xmm2,xmm1 + sha256rnds2 xmm2,xmm1 ; 60-63 pshufd xmm0,xmm0,0x0e dec rdx nop @@ -2002,11 +2002,11 @@ $L$oop_shaext: paddd xmm1,xmm9 jnz NEAR $L$oop_shaext - pshufd xmm2,xmm2,0xb1 - pshufd xmm7,xmm1,0x1b - pshufd xmm1,xmm1,0xb1 - punpckhqdq xmm1,xmm2 - palignr xmm2,xmm7,8 + pshufd xmm2,xmm2,0xb1 ; DCHG + pshufd xmm7,xmm1,0x1b ; FEBA + pshufd xmm1,xmm1,0xb1 ; BAFE + punpckhqdq xmm1,xmm2 ; DCBA + palignr xmm2,xmm7,8 ; HGFE movdqu XMMWORD[rdi],xmm1 movdqu XMMWORD[16+rdi],xmm2 @@ -2037,7 +2037,7 @@ $L$SEH_begin_sha256_block_data_order_ssse3: _CET_ENDBR - mov rax,rsp + mov rax,rsp ; copy %rsp push rbx @@ -2051,14 +2051,14 @@ _CET_ENDBR push r15 - shl rdx,4 + shl rdx,4 ; num*16 sub rsp,160 - lea rdx,[rdx*4+rsi] - and rsp,-64 - mov QWORD[((64+0))+rsp],rdi - mov QWORD[((64+8))+rsp],rsi - mov QWORD[((64+16))+rsp],rdx - mov QWORD[88+rsp],rax + lea rdx,[rdx*4+rsi] ; inp+num*16*4 + and rsp,-64 ; align stack frame + mov QWORD[((64+0))+rsp],rdi ; save ctx, 1st arg + mov QWORD[((64+8))+rsp],rsi ; save inp, 2nd arh + mov QWORD[((64+16))+rsp],rdx ; save end pointer, "3rd" arg + mov QWORD[88+rsp],rax ; save copy of %rsp movaps XMMWORD[(64+32)+rsp],xmm6 movaps XMMWORD[(64+48)+rsp],xmm7 @@ -2074,8 +2074,8 @@ $L$prologue_ssse3: mov r9d,DWORD[20+rdi] mov r10d,DWORD[24+rdi] mov r11d,DWORD[28+rdi] - - +; movdqa K256+512+32(%rip),%xmm8 +; movdqa K256+512+64(%rip),%xmm9 jmp NEAR $L$loop_ssse3 ALIGN 16 $L$loop_ssse3: @@ -2102,14 +2102,14 @@ $L$loop_ssse3: movdqa XMMWORD[16+rsp],xmm5 mov edi,ebx movdqa XMMWORD[32+rsp],xmm6 - xor edi,ecx + xor edi,ecx ; magic movdqa XMMWORD[48+rsp],xmm7 mov r13d,r8d jmp NEAR $L$ssse3_00_47 ALIGN 16 $L$ssse3_00_47: - sub rbp,-128 + sub rbp,-128 ; size optimization ror r13d,14 movdqa xmm4,xmm1 mov eax,r14d @@ -3170,7 +3170,7 @@ $L$SEH_begin_sha256_block_data_order_avx: _CET_ENDBR - mov rax,rsp + mov rax,rsp ; copy %rsp push rbx @@ -3184,14 +3184,14 @@ _CET_ENDBR push r15 - shl rdx,4 + shl rdx,4 ; num*16 sub rsp,160 - lea rdx,[rdx*4+rsi] - and rsp,-64 - mov QWORD[((64+0))+rsp],rdi - mov QWORD[((64+8))+rsp],rsi - mov QWORD[((64+16))+rsp],rdx - mov QWORD[88+rsp],rax + lea rdx,[rdx*4+rsi] ; inp+num*16*4 + and rsp,-64 ; align stack frame + mov QWORD[((64+0))+rsp],rdi ; save ctx, 1st arg + mov QWORD[((64+8))+rsp],rsi ; save inp, 2nd arh + mov QWORD[((64+16))+rsp],rdx ; save end pointer, "3rd" arg + mov QWORD[88+rsp],rax ; save copy of %rsp movaps XMMWORD[(64+32)+rsp],xmm6 movaps XMMWORD[(64+48)+rsp],xmm7 @@ -3232,14 +3232,14 @@ $L$loop_avx: vmovdqa XMMWORD[16+rsp],xmm5 mov edi,ebx vmovdqa XMMWORD[32+rsp],xmm6 - xor edi,ecx + xor edi,ecx ; magic vmovdqa XMMWORD[48+rsp],xmm7 mov r13d,r8d jmp NEAR $L$avx_00_47 ALIGN 16 $L$avx_00_47: - sub rbp,-128 + sub rbp,-128 ; size optimization vpalignr xmm4,xmm1,xmm0,4 shrd r13d,r13d,14 mov eax,r14d @@ -4265,25 +4265,25 @@ se_handler: pushfq sub rsp,64 - mov rax,QWORD[120+r8] - mov rbx,QWORD[248+r8] + mov rax,QWORD[120+r8] ; pull context->Rax + mov rbx,QWORD[248+r8] ; pull context->Rip - mov rsi,QWORD[8+r9] - mov r11,QWORD[56+r9] + mov rsi,QWORD[8+r9] ; disp->ImageBase + mov r11,QWORD[56+r9] ; disp->HanderlData - mov r10d,DWORD[r11] - lea r10,[r10*1+rsi] - cmp rbx,r10 + mov r10d,DWORD[r11] ; HandlerData[0] + lea r10,[r10*1+rsi] ; prologue label + cmp rbx,r10 ; context->RipRsp - mov r10d,DWORD[4+r11] - lea r10,[r10*1+rsi] - cmp rbx,r10 + mov r10d,DWORD[4+r11] ; HandlerData[1] + lea r10,[r10*1+rsi] ; epilogue label + cmp rbx,r10 ; context->Rip>=epilogue label jae NEAR $L$in_prologue - mov rsi,rax - mov rax,QWORD[((64+24))+rax] + mov rsi,rax ; put aside Rsp + mov rax,QWORD[((64+24))+rax] ; pull 88(%rsp) mov rbx,QWORD[((-8))+rax] mov rbp,QWORD[((-16))+rax] @@ -4291,49 +4291,49 @@ se_handler: mov r13,QWORD[((-32))+rax] mov r14,QWORD[((-40))+rax] mov r15,QWORD[((-48))+rax] - mov QWORD[144+r8],rbx - mov QWORD[160+r8],rbp - mov QWORD[216+r8],r12 - mov QWORD[224+r8],r13 - mov QWORD[232+r8],r14 - mov QWORD[240+r8],r15 + mov QWORD[144+r8],rbx ; restore context->Rbx + mov QWORD[160+r8],rbp ; restore context->Rbp + mov QWORD[216+r8],r12 ; restore context->R12 + mov QWORD[224+r8],r13 ; restore context->R13 + mov QWORD[232+r8],r14 ; restore context->R14 + mov QWORD[240+r8],r15 ; restore context->R15 lea r10,[$L$epilogue] cmp rbx,r10 - jb NEAR $L$in_prologue + jb NEAR $L$in_prologue ; non-AVX code - lea rsi,[((64+32))+rsi] - lea rdi,[512+r8] + lea rsi,[((64+32))+rsi] ; Xmm6- save area + lea rdi,[512+r8] ; &context.Xmm6 mov ecx,8 - DD 0xa548f3fc + DD 0xa548f3fc ; cld; rep movsq $L$in_prologue: mov rdi,QWORD[8+rax] mov rsi,QWORD[16+rax] - mov QWORD[152+r8],rax - mov QWORD[168+r8],rsi - mov QWORD[176+r8],rdi + mov QWORD[152+r8],rax ; restore context->Rsp + mov QWORD[168+r8],rsi ; restore context->Rsi + mov QWORD[176+r8],rdi ; restore context->Rdi - mov rdi,QWORD[40+r9] - mov rsi,r8 - mov ecx,154 - DD 0xa548f3fc + mov rdi,QWORD[40+r9] ; disp->ContextRecord + mov rsi,r8 ; context + mov ecx,154 ; sizeof(CONTEXT) + DD 0xa548f3fc ; cld; rep movsq mov rsi,r9 - xor rcx,rcx - mov rdx,QWORD[8+rsi] - mov r8,QWORD[rsi] - mov r9,QWORD[16+rsi] - mov r10,QWORD[40+rsi] - lea r11,[56+rsi] - lea r12,[24+rsi] - mov QWORD[32+rsp],r10 - mov QWORD[40+rsp],r11 - mov QWORD[48+rsp],r12 - mov QWORD[56+rsp],rcx + xor rcx,rcx ; arg1, UNW_FLAG_NHANDLER + mov rdx,QWORD[8+rsi] ; arg2, disp->ImageBase + mov r8,QWORD[rsi] ; arg3, disp->ControlPc + mov r9,QWORD[16+rsi] ; arg4, disp->FunctionEntry + mov r10,QWORD[40+rsi] ; disp->ContextRecord + lea r11,[56+rsi] ; &disp->HandlerData + lea r12,[24+rsi] ; &disp->EstablisherFrame + mov QWORD[32+rsp],r10 ; arg5 + mov QWORD[40+rsp],r11 ; arg6 + mov QWORD[48+rsp],r12 ; arg7 + mov QWORD[56+rsp],rcx ; arg8, (NULL) call QWORD[__imp_RtlVirtualUnwind] - mov eax,1 + mov eax,1 ; ExceptionContinueSearch add rsp,64 popfq pop r15 @@ -4360,21 +4360,21 @@ shaext_handler: pushfq sub rsp,64 - mov rax,QWORD[120+r8] - mov rbx,QWORD[248+r8] + mov rax,QWORD[120+r8] ; pull context->Rax + mov rbx,QWORD[248+r8] ; pull context->Rip lea r10,[$L$prologue_shaext] - cmp rbx,r10 + cmp rbx,r10 ; context->Rip<.Lprologue jb NEAR $L$in_prologue lea r10,[$L$epilogue_shaext] - cmp rbx,r10 + cmp rbx,r10 ; context->Rip>=.Lepilogue jae NEAR $L$in_prologue lea rsi,[((-8-80))+rax] - lea rdi,[512+r8] + lea rdi,[512+r8] ; &context.Xmm6 mov ecx,10 - DD 0xa548f3fc + DD 0xa548f3fc ; cld; rep movsq jmp NEAR $L$in_prologue @@ -4397,18 +4397,18 @@ ALIGN 8 $L$SEH_info_sha256_block_data_order_nohw: DB 9,0,0,0 DD se_handler wrt ..imagebase - DD $L$prologue wrt ..imagebase,$L$epilogue wrt ..imagebase + DD $L$prologue wrt ..imagebase,$L$epilogue wrt ..imagebase ; HandlerData[] $L$SEH_info_sha256_block_data_order_hw: DB 9,0,0,0 DD shaext_handler wrt ..imagebase $L$SEH_info_sha256_block_data_order_ssse3: DB 9,0,0,0 DD se_handler wrt ..imagebase - DD $L$prologue_ssse3 wrt ..imagebase,$L$epilogue_ssse3 wrt ..imagebase + DD $L$prologue_ssse3 wrt ..imagebase,$L$epilogue_ssse3 wrt ..imagebase ; HandlerData[] $L$SEH_info_sha256_block_data_order_avx: DB 9,0,0,0 DD se_handler wrt ..imagebase - DD $L$prologue_avx wrt ..imagebase,$L$epilogue_avx wrt ..imagebase + DD $L$prologue_avx wrt ..imagebase,$L$epilogue_avx wrt ..imagebase ; HandlerData[] %else ; Work around https://bugzilla.nasm.us/show_bug.cgi?id=3392738 ret diff --git a/third_party/boringssl/gen/bcm/sha512-armv8-apple.S b/third_party/boringssl/gen/bcm/sha512-armv8-apple.S index b6403a9cb..173a769ed 100644 --- a/third_party/boringssl/gen/bcm/sha512-armv8-apple.S +++ b/third_party/boringssl/gen/bcm/sha512-armv8-apple.S @@ -1020,7 +1020,9 @@ Loop_16_xx: .section __TEXT,__const + .align 6 +sha512_constants: LK512: .quad 0x428a2f98d728ae22,0x7137449123ef65cd @@ -1074,6 +1076,7 @@ LK512: .private_extern _sha512_block_data_order_hw .align 6 +.alt_entry _sha512_block_data_order_hw _sha512_block_data_order_hw: // Armv8.3-A PAuth: even though x30 is pushed to stack it is not popped later. AARCH64_VALID_CALL_TARGET diff --git a/third_party/boringssl/gen/bcm/sha512-armv8-linux.S b/third_party/boringssl/gen/bcm/sha512-armv8-linux.S index f046a5c4b..451d696d4 100644 --- a/third_party/boringssl/gen/bcm/sha512-armv8-linux.S +++ b/third_party/boringssl/gen/bcm/sha512-armv8-linux.S @@ -1020,7 +1020,9 @@ sha512_block_data_order_nohw: .size sha512_block_data_order_nohw,.-sha512_block_data_order_nohw .section .rodata + .align 6 +sha512_constants: .type .LK512,%object .LK512: .quad 0x428a2f98d728ae22,0x7137449123ef65cd diff --git a/third_party/boringssl/gen/bcm/sha512-armv8-win.S b/third_party/boringssl/gen/bcm/sha512-armv8-win.S index 0d5ad5b9f..a6a5be040 100644 --- a/third_party/boringssl/gen/bcm/sha512-armv8-win.S +++ b/third_party/boringssl/gen/bcm/sha512-armv8-win.S @@ -1022,7 +1022,9 @@ Loop_16_xx: .section .rodata + .align 6 +sha512_constants: LK512: .quad 0x428a2f98d728ae22,0x7137449123ef65cd diff --git a/third_party/boringssl/gen/bcm/sha512-x86_64-apple.S b/third_party/boringssl/gen/bcm/sha512-x86_64-apple.S index 58f27a48a..5524b9111 100644 --- a/third_party/boringssl/gen/bcm/sha512-x86_64-apple.S +++ b/third_party/boringssl/gen/bcm/sha512-x86_64-apple.S @@ -13,7 +13,7 @@ _sha512_block_data_order_nohw: _CET_ENDBR - movq %rsp,%rax + movq %rsp,%rax // copy %rsp pushq %rbx @@ -27,14 +27,14 @@ _CET_ENDBR pushq %r15 - shlq $4,%rdx + shlq $4,%rdx // num*16 subq $128+32,%rsp - leaq (%rsi,%rdx,8),%rdx - andq $-64,%rsp - movq %rdi,128+0(%rsp) - movq %rsi,128+8(%rsp) - movq %rdx,128+16(%rsp) - movq %rax,152(%rsp) + leaq (%rsi,%rdx,8),%rdx // inp+num*16*8 + andq $-64,%rsp // align stack frame + movq %rdi,128+0(%rsp) // save ctx, 1st arg + movq %rsi,128+8(%rsp) // save inp, 2nd arh + movq %rdx,128+16(%rsp) // save end pointer, "3rd" arg + movq %rax,152(%rsp) // save copy of %rsp L$prologue: @@ -52,7 +52,7 @@ L$prologue: L$loop: movq %rbx,%rdi leaq K512(%rip),%rbp - xorq %rcx,%rdi + xorq %rcx,%rdi // magic movq 0(%rsi),%r12 movq %r8,%r13 movq %rax,%r14 @@ -62,38 +62,38 @@ L$loop: xorq %r8,%r13 rorq $5,%r14 - xorq %r10,%r15 + xorq %r10,%r15 // f^g movq %r12,0(%rsp) xorq %rax,%r14 - andq %r8,%r15 + andq %r8,%r15 // (f^g)&e rorq $4,%r13 - addq %r11,%r12 - xorq %r10,%r15 + addq %r11,%r12 // T1+=h + xorq %r10,%r15 // Ch(e,f,g)=((f^g)&e)^g rorq $6,%r14 xorq %r8,%r13 - addq %r15,%r12 + addq %r15,%r12 // T1+=Ch(e,f,g) movq %rax,%r15 - addq (%rbp),%r12 + addq (%rbp),%r12 // T1+=K[round] xorq %rax,%r14 - xorq %rbx,%r15 - rorq $14,%r13 + xorq %rbx,%r15 // a^b, b^c in next round + rorq $14,%r13 // Sigma1(e) movq %rbx,%r11 andq %r15,%rdi - rorq $28,%r14 - addq %r13,%r12 + rorq $28,%r14 // Sigma0(a) + addq %r13,%r12 // T1+=Sigma1(e) - xorq %rdi,%r11 - addq %r12,%rdx - addq %r12,%r11 + xorq %rdi,%r11 // h=Maj(a,b,c)=Ch(a^b,c,b) + addq %r12,%rdx // d+=T1 + addq %r12,%r11 // h+=T1 - leaq 8(%rbp),%rbp - addq %r14,%r11 + leaq 8(%rbp),%rbp // round++ + addq %r14,%r11 // h+=Sigma0(a) movq 8(%rsi),%r12 movq %rdx,%r13 movq %r11,%r14 @@ -103,38 +103,38 @@ L$loop: xorq %rdx,%r13 rorq $5,%r14 - xorq %r9,%rdi + xorq %r9,%rdi // f^g movq %r12,8(%rsp) xorq %r11,%r14 - andq %rdx,%rdi + andq %rdx,%rdi // (f^g)&e rorq $4,%r13 - addq %r10,%r12 - xorq %r9,%rdi + addq %r10,%r12 // T1+=h + xorq %r9,%rdi // Ch(e,f,g)=((f^g)&e)^g rorq $6,%r14 xorq %rdx,%r13 - addq %rdi,%r12 + addq %rdi,%r12 // T1+=Ch(e,f,g) movq %r11,%rdi - addq (%rbp),%r12 + addq (%rbp),%r12 // T1+=K[round] xorq %r11,%r14 - xorq %rax,%rdi - rorq $14,%r13 + xorq %rax,%rdi // a^b, b^c in next round + rorq $14,%r13 // Sigma1(e) movq %rax,%r10 andq %rdi,%r15 - rorq $28,%r14 - addq %r13,%r12 + rorq $28,%r14 // Sigma0(a) + addq %r13,%r12 // T1+=Sigma1(e) - xorq %r15,%r10 - addq %r12,%rcx - addq %r12,%r10 + xorq %r15,%r10 // h=Maj(a,b,c)=Ch(a^b,c,b) + addq %r12,%rcx // d+=T1 + addq %r12,%r10 // h+=T1 - leaq 24(%rbp),%rbp - addq %r14,%r10 + leaq 24(%rbp),%rbp // round++ + addq %r14,%r10 // h+=Sigma0(a) movq 16(%rsi),%r12 movq %rcx,%r13 movq %r10,%r14 @@ -144,38 +144,38 @@ L$loop: xorq %rcx,%r13 rorq $5,%r14 - xorq %r8,%r15 + xorq %r8,%r15 // f^g movq %r12,16(%rsp) xorq %r10,%r14 - andq %rcx,%r15 + andq %rcx,%r15 // (f^g)&e rorq $4,%r13 - addq %r9,%r12 - xorq %r8,%r15 + addq %r9,%r12 // T1+=h + xorq %r8,%r15 // Ch(e,f,g)=((f^g)&e)^g rorq $6,%r14 xorq %rcx,%r13 - addq %r15,%r12 + addq %r15,%r12 // T1+=Ch(e,f,g) movq %r10,%r15 - addq (%rbp),%r12 + addq (%rbp),%r12 // T1+=K[round] xorq %r10,%r14 - xorq %r11,%r15 - rorq $14,%r13 + xorq %r11,%r15 // a^b, b^c in next round + rorq $14,%r13 // Sigma1(e) movq %r11,%r9 andq %r15,%rdi - rorq $28,%r14 - addq %r13,%r12 + rorq $28,%r14 // Sigma0(a) + addq %r13,%r12 // T1+=Sigma1(e) - xorq %rdi,%r9 - addq %r12,%rbx - addq %r12,%r9 + xorq %rdi,%r9 // h=Maj(a,b,c)=Ch(a^b,c,b) + addq %r12,%rbx // d+=T1 + addq %r12,%r9 // h+=T1 - leaq 8(%rbp),%rbp - addq %r14,%r9 + leaq 8(%rbp),%rbp // round++ + addq %r14,%r9 // h+=Sigma0(a) movq 24(%rsi),%r12 movq %rbx,%r13 movq %r9,%r14 @@ -185,38 +185,38 @@ L$loop: xorq %rbx,%r13 rorq $5,%r14 - xorq %rdx,%rdi + xorq %rdx,%rdi // f^g movq %r12,24(%rsp) xorq %r9,%r14 - andq %rbx,%rdi + andq %rbx,%rdi // (f^g)&e rorq $4,%r13 - addq %r8,%r12 - xorq %rdx,%rdi + addq %r8,%r12 // T1+=h + xorq %rdx,%rdi // Ch(e,f,g)=((f^g)&e)^g rorq $6,%r14 xorq %rbx,%r13 - addq %rdi,%r12 + addq %rdi,%r12 // T1+=Ch(e,f,g) movq %r9,%rdi - addq (%rbp),%r12 + addq (%rbp),%r12 // T1+=K[round] xorq %r9,%r14 - xorq %r10,%rdi - rorq $14,%r13 + xorq %r10,%rdi // a^b, b^c in next round + rorq $14,%r13 // Sigma1(e) movq %r10,%r8 andq %rdi,%r15 - rorq $28,%r14 - addq %r13,%r12 + rorq $28,%r14 // Sigma0(a) + addq %r13,%r12 // T1+=Sigma1(e) - xorq %r15,%r8 - addq %r12,%rax - addq %r12,%r8 + xorq %r15,%r8 // h=Maj(a,b,c)=Ch(a^b,c,b) + addq %r12,%rax // d+=T1 + addq %r12,%r8 // h+=T1 - leaq 24(%rbp),%rbp - addq %r14,%r8 + leaq 24(%rbp),%rbp // round++ + addq %r14,%r8 // h+=Sigma0(a) movq 32(%rsi),%r12 movq %rax,%r13 movq %r8,%r14 @@ -226,38 +226,38 @@ L$loop: xorq %rax,%r13 rorq $5,%r14 - xorq %rcx,%r15 + xorq %rcx,%r15 // f^g movq %r12,32(%rsp) xorq %r8,%r14 - andq %rax,%r15 + andq %rax,%r15 // (f^g)&e rorq $4,%r13 - addq %rdx,%r12 - xorq %rcx,%r15 + addq %rdx,%r12 // T1+=h + xorq %rcx,%r15 // Ch(e,f,g)=((f^g)&e)^g rorq $6,%r14 xorq %rax,%r13 - addq %r15,%r12 + addq %r15,%r12 // T1+=Ch(e,f,g) movq %r8,%r15 - addq (%rbp),%r12 + addq (%rbp),%r12 // T1+=K[round] xorq %r8,%r14 - xorq %r9,%r15 - rorq $14,%r13 + xorq %r9,%r15 // a^b, b^c in next round + rorq $14,%r13 // Sigma1(e) movq %r9,%rdx andq %r15,%rdi - rorq $28,%r14 - addq %r13,%r12 + rorq $28,%r14 // Sigma0(a) + addq %r13,%r12 // T1+=Sigma1(e) - xorq %rdi,%rdx - addq %r12,%r11 - addq %r12,%rdx + xorq %rdi,%rdx // h=Maj(a,b,c)=Ch(a^b,c,b) + addq %r12,%r11 // d+=T1 + addq %r12,%rdx // h+=T1 - leaq 8(%rbp),%rbp - addq %r14,%rdx + leaq 8(%rbp),%rbp // round++ + addq %r14,%rdx // h+=Sigma0(a) movq 40(%rsi),%r12 movq %r11,%r13 movq %rdx,%r14 @@ -267,38 +267,38 @@ L$loop: xorq %r11,%r13 rorq $5,%r14 - xorq %rbx,%rdi + xorq %rbx,%rdi // f^g movq %r12,40(%rsp) xorq %rdx,%r14 - andq %r11,%rdi + andq %r11,%rdi // (f^g)&e rorq $4,%r13 - addq %rcx,%r12 - xorq %rbx,%rdi + addq %rcx,%r12 // T1+=h + xorq %rbx,%rdi // Ch(e,f,g)=((f^g)&e)^g rorq $6,%r14 xorq %r11,%r13 - addq %rdi,%r12 + addq %rdi,%r12 // T1+=Ch(e,f,g) movq %rdx,%rdi - addq (%rbp),%r12 + addq (%rbp),%r12 // T1+=K[round] xorq %rdx,%r14 - xorq %r8,%rdi - rorq $14,%r13 + xorq %r8,%rdi // a^b, b^c in next round + rorq $14,%r13 // Sigma1(e) movq %r8,%rcx andq %rdi,%r15 - rorq $28,%r14 - addq %r13,%r12 + rorq $28,%r14 // Sigma0(a) + addq %r13,%r12 // T1+=Sigma1(e) - xorq %r15,%rcx - addq %r12,%r10 - addq %r12,%rcx + xorq %r15,%rcx // h=Maj(a,b,c)=Ch(a^b,c,b) + addq %r12,%r10 // d+=T1 + addq %r12,%rcx // h+=T1 - leaq 24(%rbp),%rbp - addq %r14,%rcx + leaq 24(%rbp),%rbp // round++ + addq %r14,%rcx // h+=Sigma0(a) movq 48(%rsi),%r12 movq %r10,%r13 movq %rcx,%r14 @@ -308,38 +308,38 @@ L$loop: xorq %r10,%r13 rorq $5,%r14 - xorq %rax,%r15 + xorq %rax,%r15 // f^g movq %r12,48(%rsp) xorq %rcx,%r14 - andq %r10,%r15 + andq %r10,%r15 // (f^g)&e rorq $4,%r13 - addq %rbx,%r12 - xorq %rax,%r15 + addq %rbx,%r12 // T1+=h + xorq %rax,%r15 // Ch(e,f,g)=((f^g)&e)^g rorq $6,%r14 xorq %r10,%r13 - addq %r15,%r12 + addq %r15,%r12 // T1+=Ch(e,f,g) movq %rcx,%r15 - addq (%rbp),%r12 + addq (%rbp),%r12 // T1+=K[round] xorq %rcx,%r14 - xorq %rdx,%r15 - rorq $14,%r13 + xorq %rdx,%r15 // a^b, b^c in next round + rorq $14,%r13 // Sigma1(e) movq %rdx,%rbx andq %r15,%rdi - rorq $28,%r14 - addq %r13,%r12 + rorq $28,%r14 // Sigma0(a) + addq %r13,%r12 // T1+=Sigma1(e) - xorq %rdi,%rbx - addq %r12,%r9 - addq %r12,%rbx + xorq %rdi,%rbx // h=Maj(a,b,c)=Ch(a^b,c,b) + addq %r12,%r9 // d+=T1 + addq %r12,%rbx // h+=T1 - leaq 8(%rbp),%rbp - addq %r14,%rbx + leaq 8(%rbp),%rbp // round++ + addq %r14,%rbx // h+=Sigma0(a) movq 56(%rsi),%r12 movq %r9,%r13 movq %rbx,%r14 @@ -349,38 +349,38 @@ L$loop: xorq %r9,%r13 rorq $5,%r14 - xorq %r11,%rdi + xorq %r11,%rdi // f^g movq %r12,56(%rsp) xorq %rbx,%r14 - andq %r9,%rdi + andq %r9,%rdi // (f^g)&e rorq $4,%r13 - addq %rax,%r12 - xorq %r11,%rdi + addq %rax,%r12 // T1+=h + xorq %r11,%rdi // Ch(e,f,g)=((f^g)&e)^g rorq $6,%r14 xorq %r9,%r13 - addq %rdi,%r12 + addq %rdi,%r12 // T1+=Ch(e,f,g) movq %rbx,%rdi - addq (%rbp),%r12 + addq (%rbp),%r12 // T1+=K[round] xorq %rbx,%r14 - xorq %rcx,%rdi - rorq $14,%r13 + xorq %rcx,%rdi // a^b, b^c in next round + rorq $14,%r13 // Sigma1(e) movq %rcx,%rax andq %rdi,%r15 - rorq $28,%r14 - addq %r13,%r12 + rorq $28,%r14 // Sigma0(a) + addq %r13,%r12 // T1+=Sigma1(e) - xorq %r15,%rax - addq %r12,%r8 - addq %r12,%rax + xorq %r15,%rax // h=Maj(a,b,c)=Ch(a^b,c,b) + addq %r12,%r8 // d+=T1 + addq %r12,%rax // h+=T1 - leaq 24(%rbp),%rbp - addq %r14,%rax + leaq 24(%rbp),%rbp // round++ + addq %r14,%rax // h+=Sigma0(a) movq 64(%rsi),%r12 movq %r8,%r13 movq %rax,%r14 @@ -390,38 +390,38 @@ L$loop: xorq %r8,%r13 rorq $5,%r14 - xorq %r10,%r15 + xorq %r10,%r15 // f^g movq %r12,64(%rsp) xorq %rax,%r14 - andq %r8,%r15 + andq %r8,%r15 // (f^g)&e rorq $4,%r13 - addq %r11,%r12 - xorq %r10,%r15 + addq %r11,%r12 // T1+=h + xorq %r10,%r15 // Ch(e,f,g)=((f^g)&e)^g rorq $6,%r14 xorq %r8,%r13 - addq %r15,%r12 + addq %r15,%r12 // T1+=Ch(e,f,g) movq %rax,%r15 - addq (%rbp),%r12 + addq (%rbp),%r12 // T1+=K[round] xorq %rax,%r14 - xorq %rbx,%r15 - rorq $14,%r13 + xorq %rbx,%r15 // a^b, b^c in next round + rorq $14,%r13 // Sigma1(e) movq %rbx,%r11 andq %r15,%rdi - rorq $28,%r14 - addq %r13,%r12 + rorq $28,%r14 // Sigma0(a) + addq %r13,%r12 // T1+=Sigma1(e) - xorq %rdi,%r11 - addq %r12,%rdx - addq %r12,%r11 + xorq %rdi,%r11 // h=Maj(a,b,c)=Ch(a^b,c,b) + addq %r12,%rdx // d+=T1 + addq %r12,%r11 // h+=T1 - leaq 8(%rbp),%rbp - addq %r14,%r11 + leaq 8(%rbp),%rbp // round++ + addq %r14,%r11 // h+=Sigma0(a) movq 72(%rsi),%r12 movq %rdx,%r13 movq %r11,%r14 @@ -431,38 +431,38 @@ L$loop: xorq %rdx,%r13 rorq $5,%r14 - xorq %r9,%rdi + xorq %r9,%rdi // f^g movq %r12,72(%rsp) xorq %r11,%r14 - andq %rdx,%rdi + andq %rdx,%rdi // (f^g)&e rorq $4,%r13 - addq %r10,%r12 - xorq %r9,%rdi + addq %r10,%r12 // T1+=h + xorq %r9,%rdi // Ch(e,f,g)=((f^g)&e)^g rorq $6,%r14 xorq %rdx,%r13 - addq %rdi,%r12 + addq %rdi,%r12 // T1+=Ch(e,f,g) movq %r11,%rdi - addq (%rbp),%r12 + addq (%rbp),%r12 // T1+=K[round] xorq %r11,%r14 - xorq %rax,%rdi - rorq $14,%r13 + xorq %rax,%rdi // a^b, b^c in next round + rorq $14,%r13 // Sigma1(e) movq %rax,%r10 andq %rdi,%r15 - rorq $28,%r14 - addq %r13,%r12 + rorq $28,%r14 // Sigma0(a) + addq %r13,%r12 // T1+=Sigma1(e) - xorq %r15,%r10 - addq %r12,%rcx - addq %r12,%r10 + xorq %r15,%r10 // h=Maj(a,b,c)=Ch(a^b,c,b) + addq %r12,%rcx // d+=T1 + addq %r12,%r10 // h+=T1 - leaq 24(%rbp),%rbp - addq %r14,%r10 + leaq 24(%rbp),%rbp // round++ + addq %r14,%r10 // h+=Sigma0(a) movq 80(%rsi),%r12 movq %rcx,%r13 movq %r10,%r14 @@ -472,38 +472,38 @@ L$loop: xorq %rcx,%r13 rorq $5,%r14 - xorq %r8,%r15 + xorq %r8,%r15 // f^g movq %r12,80(%rsp) xorq %r10,%r14 - andq %rcx,%r15 + andq %rcx,%r15 // (f^g)&e rorq $4,%r13 - addq %r9,%r12 - xorq %r8,%r15 + addq %r9,%r12 // T1+=h + xorq %r8,%r15 // Ch(e,f,g)=((f^g)&e)^g rorq $6,%r14 xorq %rcx,%r13 - addq %r15,%r12 + addq %r15,%r12 // T1+=Ch(e,f,g) movq %r10,%r15 - addq (%rbp),%r12 + addq (%rbp),%r12 // T1+=K[round] xorq %r10,%r14 - xorq %r11,%r15 - rorq $14,%r13 + xorq %r11,%r15 // a^b, b^c in next round + rorq $14,%r13 // Sigma1(e) movq %r11,%r9 andq %r15,%rdi - rorq $28,%r14 - addq %r13,%r12 + rorq $28,%r14 // Sigma0(a) + addq %r13,%r12 // T1+=Sigma1(e) - xorq %rdi,%r9 - addq %r12,%rbx - addq %r12,%r9 + xorq %rdi,%r9 // h=Maj(a,b,c)=Ch(a^b,c,b) + addq %r12,%rbx // d+=T1 + addq %r12,%r9 // h+=T1 - leaq 8(%rbp),%rbp - addq %r14,%r9 + leaq 8(%rbp),%rbp // round++ + addq %r14,%r9 // h+=Sigma0(a) movq 88(%rsi),%r12 movq %rbx,%r13 movq %r9,%r14 @@ -513,38 +513,38 @@ L$loop: xorq %rbx,%r13 rorq $5,%r14 - xorq %rdx,%rdi + xorq %rdx,%rdi // f^g movq %r12,88(%rsp) xorq %r9,%r14 - andq %rbx,%rdi + andq %rbx,%rdi // (f^g)&e rorq $4,%r13 - addq %r8,%r12 - xorq %rdx,%rdi + addq %r8,%r12 // T1+=h + xorq %rdx,%rdi // Ch(e,f,g)=((f^g)&e)^g rorq $6,%r14 xorq %rbx,%r13 - addq %rdi,%r12 + addq %rdi,%r12 // T1+=Ch(e,f,g) movq %r9,%rdi - addq (%rbp),%r12 + addq (%rbp),%r12 // T1+=K[round] xorq %r9,%r14 - xorq %r10,%rdi - rorq $14,%r13 + xorq %r10,%rdi // a^b, b^c in next round + rorq $14,%r13 // Sigma1(e) movq %r10,%r8 andq %rdi,%r15 - rorq $28,%r14 - addq %r13,%r12 + rorq $28,%r14 // Sigma0(a) + addq %r13,%r12 // T1+=Sigma1(e) - xorq %r15,%r8 - addq %r12,%rax - addq %r12,%r8 + xorq %r15,%r8 // h=Maj(a,b,c)=Ch(a^b,c,b) + addq %r12,%rax // d+=T1 + addq %r12,%r8 // h+=T1 - leaq 24(%rbp),%rbp - addq %r14,%r8 + leaq 24(%rbp),%rbp // round++ + addq %r14,%r8 // h+=Sigma0(a) movq 96(%rsi),%r12 movq %rax,%r13 movq %r8,%r14 @@ -554,38 +554,38 @@ L$loop: xorq %rax,%r13 rorq $5,%r14 - xorq %rcx,%r15 + xorq %rcx,%r15 // f^g movq %r12,96(%rsp) xorq %r8,%r14 - andq %rax,%r15 + andq %rax,%r15 // (f^g)&e rorq $4,%r13 - addq %rdx,%r12 - xorq %rcx,%r15 + addq %rdx,%r12 // T1+=h + xorq %rcx,%r15 // Ch(e,f,g)=((f^g)&e)^g rorq $6,%r14 xorq %rax,%r13 - addq %r15,%r12 + addq %r15,%r12 // T1+=Ch(e,f,g) movq %r8,%r15 - addq (%rbp),%r12 + addq (%rbp),%r12 // T1+=K[round] xorq %r8,%r14 - xorq %r9,%r15 - rorq $14,%r13 + xorq %r9,%r15 // a^b, b^c in next round + rorq $14,%r13 // Sigma1(e) movq %r9,%rdx andq %r15,%rdi - rorq $28,%r14 - addq %r13,%r12 + rorq $28,%r14 // Sigma0(a) + addq %r13,%r12 // T1+=Sigma1(e) - xorq %rdi,%rdx - addq %r12,%r11 - addq %r12,%rdx + xorq %rdi,%rdx // h=Maj(a,b,c)=Ch(a^b,c,b) + addq %r12,%r11 // d+=T1 + addq %r12,%rdx // h+=T1 - leaq 8(%rbp),%rbp - addq %r14,%rdx + leaq 8(%rbp),%rbp // round++ + addq %r14,%rdx // h+=Sigma0(a) movq 104(%rsi),%r12 movq %r11,%r13 movq %rdx,%r14 @@ -595,38 +595,38 @@ L$loop: xorq %r11,%r13 rorq $5,%r14 - xorq %rbx,%rdi + xorq %rbx,%rdi // f^g movq %r12,104(%rsp) xorq %rdx,%r14 - andq %r11,%rdi + andq %r11,%rdi // (f^g)&e rorq $4,%r13 - addq %rcx,%r12 - xorq %rbx,%rdi + addq %rcx,%r12 // T1+=h + xorq %rbx,%rdi // Ch(e,f,g)=((f^g)&e)^g rorq $6,%r14 xorq %r11,%r13 - addq %rdi,%r12 + addq %rdi,%r12 // T1+=Ch(e,f,g) movq %rdx,%rdi - addq (%rbp),%r12 + addq (%rbp),%r12 // T1+=K[round] xorq %rdx,%r14 - xorq %r8,%rdi - rorq $14,%r13 + xorq %r8,%rdi // a^b, b^c in next round + rorq $14,%r13 // Sigma1(e) movq %r8,%rcx andq %rdi,%r15 - rorq $28,%r14 - addq %r13,%r12 + rorq $28,%r14 // Sigma0(a) + addq %r13,%r12 // T1+=Sigma1(e) - xorq %r15,%rcx - addq %r12,%r10 - addq %r12,%rcx + xorq %r15,%rcx // h=Maj(a,b,c)=Ch(a^b,c,b) + addq %r12,%r10 // d+=T1 + addq %r12,%rcx // h+=T1 - leaq 24(%rbp),%rbp - addq %r14,%rcx + leaq 24(%rbp),%rbp // round++ + addq %r14,%rcx // h+=Sigma0(a) movq 112(%rsi),%r12 movq %r10,%r13 movq %rcx,%r14 @@ -636,38 +636,38 @@ L$loop: xorq %r10,%r13 rorq $5,%r14 - xorq %rax,%r15 + xorq %rax,%r15 // f^g movq %r12,112(%rsp) xorq %rcx,%r14 - andq %r10,%r15 + andq %r10,%r15 // (f^g)&e rorq $4,%r13 - addq %rbx,%r12 - xorq %rax,%r15 + addq %rbx,%r12 // T1+=h + xorq %rax,%r15 // Ch(e,f,g)=((f^g)&e)^g rorq $6,%r14 xorq %r10,%r13 - addq %r15,%r12 + addq %r15,%r12 // T1+=Ch(e,f,g) movq %rcx,%r15 - addq (%rbp),%r12 + addq (%rbp),%r12 // T1+=K[round] xorq %rcx,%r14 - xorq %rdx,%r15 - rorq $14,%r13 + xorq %rdx,%r15 // a^b, b^c in next round + rorq $14,%r13 // Sigma1(e) movq %rdx,%rbx andq %r15,%rdi - rorq $28,%r14 - addq %r13,%r12 + rorq $28,%r14 // Sigma0(a) + addq %r13,%r12 // T1+=Sigma1(e) - xorq %rdi,%rbx - addq %r12,%r9 - addq %r12,%rbx + xorq %rdi,%rbx // h=Maj(a,b,c)=Ch(a^b,c,b) + addq %r12,%r9 // d+=T1 + addq %r12,%rbx // h+=T1 - leaq 8(%rbp),%rbp - addq %r14,%rbx + leaq 8(%rbp),%rbp // round++ + addq %r14,%rbx // h+=Sigma0(a) movq 120(%rsi),%r12 movq %r9,%r13 movq %rbx,%r14 @@ -677,37 +677,37 @@ L$loop: xorq %r9,%r13 rorq $5,%r14 - xorq %r11,%rdi + xorq %r11,%rdi // f^g movq %r12,120(%rsp) xorq %rbx,%r14 - andq %r9,%rdi + andq %r9,%rdi // (f^g)&e rorq $4,%r13 - addq %rax,%r12 - xorq %r11,%rdi + addq %rax,%r12 // T1+=h + xorq %r11,%rdi // Ch(e,f,g)=((f^g)&e)^g rorq $6,%r14 xorq %r9,%r13 - addq %rdi,%r12 + addq %rdi,%r12 // T1+=Ch(e,f,g) movq %rbx,%rdi - addq (%rbp),%r12 + addq (%rbp),%r12 // T1+=K[round] xorq %rbx,%r14 - xorq %rcx,%rdi - rorq $14,%r13 + xorq %rcx,%rdi // a^b, b^c in next round + rorq $14,%r13 // Sigma1(e) movq %rcx,%rax andq %rdi,%r15 - rorq $28,%r14 - addq %r13,%r12 + rorq $28,%r14 // Sigma0(a) + addq %r13,%r12 // T1+=Sigma1(e) - xorq %r15,%rax - addq %r12,%r8 - addq %r12,%rax + xorq %r15,%rax // h=Maj(a,b,c)=Ch(a^b,c,b) + addq %r12,%r8 // d+=T1 + addq %r12,%rax // h+=T1 - leaq 24(%rbp),%rbp + leaq 24(%rbp),%rbp // round++ jmp L$rounds_16_xx .p2align 4 L$rounds_16_xx: @@ -716,7 +716,7 @@ L$rounds_16_xx: movq %r13,%r12 rorq $7,%r13 - addq %r14,%rax + addq %r14,%rax // modulo-scheduled h+=Sigma0(a) movq %r15,%r14 rorq $42,%r15 @@ -727,8 +727,8 @@ L$rounds_16_xx: shrq $6,%r14 rorq $19,%r15 - xorq %r13,%r12 - xorq %r14,%r15 + xorq %r13,%r12 // sigma0(X[(i+1)&0xf]) + xorq %r14,%r15 // sigma1(X[(i+14)&0xf]) addq 72(%rsp),%r12 addq 0(%rsp),%r12 @@ -740,43 +740,43 @@ L$rounds_16_xx: xorq %r8,%r13 rorq $5,%r14 - xorq %r10,%r15 + xorq %r10,%r15 // f^g movq %r12,0(%rsp) xorq %rax,%r14 - andq %r8,%r15 + andq %r8,%r15 // (f^g)&e rorq $4,%r13 - addq %r11,%r12 - xorq %r10,%r15 + addq %r11,%r12 // T1+=h + xorq %r10,%r15 // Ch(e,f,g)=((f^g)&e)^g rorq $6,%r14 xorq %r8,%r13 - addq %r15,%r12 + addq %r15,%r12 // T1+=Ch(e,f,g) movq %rax,%r15 - addq (%rbp),%r12 + addq (%rbp),%r12 // T1+=K[round] xorq %rax,%r14 - xorq %rbx,%r15 - rorq $14,%r13 + xorq %rbx,%r15 // a^b, b^c in next round + rorq $14,%r13 // Sigma1(e) movq %rbx,%r11 andq %r15,%rdi - rorq $28,%r14 - addq %r13,%r12 + rorq $28,%r14 // Sigma0(a) + addq %r13,%r12 // T1+=Sigma1(e) - xorq %rdi,%r11 - addq %r12,%rdx - addq %r12,%r11 + xorq %rdi,%r11 // h=Maj(a,b,c)=Ch(a^b,c,b) + addq %r12,%rdx // d+=T1 + addq %r12,%r11 // h+=T1 - leaq 8(%rbp),%rbp + leaq 8(%rbp),%rbp // round++ movq 16(%rsp),%r13 movq 120(%rsp),%rdi movq %r13,%r12 rorq $7,%r13 - addq %r14,%r11 + addq %r14,%r11 // modulo-scheduled h+=Sigma0(a) movq %rdi,%r14 rorq $42,%rdi @@ -787,8 +787,8 @@ L$rounds_16_xx: shrq $6,%r14 rorq $19,%rdi - xorq %r13,%r12 - xorq %r14,%rdi + xorq %r13,%r12 // sigma0(X[(i+1)&0xf]) + xorq %r14,%rdi // sigma1(X[(i+14)&0xf]) addq 80(%rsp),%r12 addq 8(%rsp),%r12 @@ -800,43 +800,43 @@ L$rounds_16_xx: xorq %rdx,%r13 rorq $5,%r14 - xorq %r9,%rdi + xorq %r9,%rdi // f^g movq %r12,8(%rsp) xorq %r11,%r14 - andq %rdx,%rdi + andq %rdx,%rdi // (f^g)&e rorq $4,%r13 - addq %r10,%r12 - xorq %r9,%rdi + addq %r10,%r12 // T1+=h + xorq %r9,%rdi // Ch(e,f,g)=((f^g)&e)^g rorq $6,%r14 xorq %rdx,%r13 - addq %rdi,%r12 + addq %rdi,%r12 // T1+=Ch(e,f,g) movq %r11,%rdi - addq (%rbp),%r12 + addq (%rbp),%r12 // T1+=K[round] xorq %r11,%r14 - xorq %rax,%rdi - rorq $14,%r13 + xorq %rax,%rdi // a^b, b^c in next round + rorq $14,%r13 // Sigma1(e) movq %rax,%r10 andq %rdi,%r15 - rorq $28,%r14 - addq %r13,%r12 + rorq $28,%r14 // Sigma0(a) + addq %r13,%r12 // T1+=Sigma1(e) - xorq %r15,%r10 - addq %r12,%rcx - addq %r12,%r10 + xorq %r15,%r10 // h=Maj(a,b,c)=Ch(a^b,c,b) + addq %r12,%rcx // d+=T1 + addq %r12,%r10 // h+=T1 - leaq 24(%rbp),%rbp + leaq 24(%rbp),%rbp // round++ movq 24(%rsp),%r13 movq 0(%rsp),%r15 movq %r13,%r12 rorq $7,%r13 - addq %r14,%r10 + addq %r14,%r10 // modulo-scheduled h+=Sigma0(a) movq %r15,%r14 rorq $42,%r15 @@ -847,8 +847,8 @@ L$rounds_16_xx: shrq $6,%r14 rorq $19,%r15 - xorq %r13,%r12 - xorq %r14,%r15 + xorq %r13,%r12 // sigma0(X[(i+1)&0xf]) + xorq %r14,%r15 // sigma1(X[(i+14)&0xf]) addq 88(%rsp),%r12 addq 16(%rsp),%r12 @@ -860,43 +860,43 @@ L$rounds_16_xx: xorq %rcx,%r13 rorq $5,%r14 - xorq %r8,%r15 + xorq %r8,%r15 // f^g movq %r12,16(%rsp) xorq %r10,%r14 - andq %rcx,%r15 + andq %rcx,%r15 // (f^g)&e rorq $4,%r13 - addq %r9,%r12 - xorq %r8,%r15 + addq %r9,%r12 // T1+=h + xorq %r8,%r15 // Ch(e,f,g)=((f^g)&e)^g rorq $6,%r14 xorq %rcx,%r13 - addq %r15,%r12 + addq %r15,%r12 // T1+=Ch(e,f,g) movq %r10,%r15 - addq (%rbp),%r12 + addq (%rbp),%r12 // T1+=K[round] xorq %r10,%r14 - xorq %r11,%r15 - rorq $14,%r13 + xorq %r11,%r15 // a^b, b^c in next round + rorq $14,%r13 // Sigma1(e) movq %r11,%r9 andq %r15,%rdi - rorq $28,%r14 - addq %r13,%r12 + rorq $28,%r14 // Sigma0(a) + addq %r13,%r12 // T1+=Sigma1(e) - xorq %rdi,%r9 - addq %r12,%rbx - addq %r12,%r9 + xorq %rdi,%r9 // h=Maj(a,b,c)=Ch(a^b,c,b) + addq %r12,%rbx // d+=T1 + addq %r12,%r9 // h+=T1 - leaq 8(%rbp),%rbp + leaq 8(%rbp),%rbp // round++ movq 32(%rsp),%r13 movq 8(%rsp),%rdi movq %r13,%r12 rorq $7,%r13 - addq %r14,%r9 + addq %r14,%r9 // modulo-scheduled h+=Sigma0(a) movq %rdi,%r14 rorq $42,%rdi @@ -907,8 +907,8 @@ L$rounds_16_xx: shrq $6,%r14 rorq $19,%rdi - xorq %r13,%r12 - xorq %r14,%rdi + xorq %r13,%r12 // sigma0(X[(i+1)&0xf]) + xorq %r14,%rdi // sigma1(X[(i+14)&0xf]) addq 96(%rsp),%r12 addq 24(%rsp),%r12 @@ -920,43 +920,43 @@ L$rounds_16_xx: xorq %rbx,%r13 rorq $5,%r14 - xorq %rdx,%rdi + xorq %rdx,%rdi // f^g movq %r12,24(%rsp) xorq %r9,%r14 - andq %rbx,%rdi + andq %rbx,%rdi // (f^g)&e rorq $4,%r13 - addq %r8,%r12 - xorq %rdx,%rdi + addq %r8,%r12 // T1+=h + xorq %rdx,%rdi // Ch(e,f,g)=((f^g)&e)^g rorq $6,%r14 xorq %rbx,%r13 - addq %rdi,%r12 + addq %rdi,%r12 // T1+=Ch(e,f,g) movq %r9,%rdi - addq (%rbp),%r12 + addq (%rbp),%r12 // T1+=K[round] xorq %r9,%r14 - xorq %r10,%rdi - rorq $14,%r13 + xorq %r10,%rdi // a^b, b^c in next round + rorq $14,%r13 // Sigma1(e) movq %r10,%r8 andq %rdi,%r15 - rorq $28,%r14 - addq %r13,%r12 + rorq $28,%r14 // Sigma0(a) + addq %r13,%r12 // T1+=Sigma1(e) - xorq %r15,%r8 - addq %r12,%rax - addq %r12,%r8 + xorq %r15,%r8 // h=Maj(a,b,c)=Ch(a^b,c,b) + addq %r12,%rax // d+=T1 + addq %r12,%r8 // h+=T1 - leaq 24(%rbp),%rbp + leaq 24(%rbp),%rbp // round++ movq 40(%rsp),%r13 movq 16(%rsp),%r15 movq %r13,%r12 rorq $7,%r13 - addq %r14,%r8 + addq %r14,%r8 // modulo-scheduled h+=Sigma0(a) movq %r15,%r14 rorq $42,%r15 @@ -967,8 +967,8 @@ L$rounds_16_xx: shrq $6,%r14 rorq $19,%r15 - xorq %r13,%r12 - xorq %r14,%r15 + xorq %r13,%r12 // sigma0(X[(i+1)&0xf]) + xorq %r14,%r15 // sigma1(X[(i+14)&0xf]) addq 104(%rsp),%r12 addq 32(%rsp),%r12 @@ -980,43 +980,43 @@ L$rounds_16_xx: xorq %rax,%r13 rorq $5,%r14 - xorq %rcx,%r15 + xorq %rcx,%r15 // f^g movq %r12,32(%rsp) xorq %r8,%r14 - andq %rax,%r15 + andq %rax,%r15 // (f^g)&e rorq $4,%r13 - addq %rdx,%r12 - xorq %rcx,%r15 + addq %rdx,%r12 // T1+=h + xorq %rcx,%r15 // Ch(e,f,g)=((f^g)&e)^g rorq $6,%r14 xorq %rax,%r13 - addq %r15,%r12 + addq %r15,%r12 // T1+=Ch(e,f,g) movq %r8,%r15 - addq (%rbp),%r12 + addq (%rbp),%r12 // T1+=K[round] xorq %r8,%r14 - xorq %r9,%r15 - rorq $14,%r13 + xorq %r9,%r15 // a^b, b^c in next round + rorq $14,%r13 // Sigma1(e) movq %r9,%rdx andq %r15,%rdi - rorq $28,%r14 - addq %r13,%r12 + rorq $28,%r14 // Sigma0(a) + addq %r13,%r12 // T1+=Sigma1(e) - xorq %rdi,%rdx - addq %r12,%r11 - addq %r12,%rdx + xorq %rdi,%rdx // h=Maj(a,b,c)=Ch(a^b,c,b) + addq %r12,%r11 // d+=T1 + addq %r12,%rdx // h+=T1 - leaq 8(%rbp),%rbp + leaq 8(%rbp),%rbp // round++ movq 48(%rsp),%r13 movq 24(%rsp),%rdi movq %r13,%r12 rorq $7,%r13 - addq %r14,%rdx + addq %r14,%rdx // modulo-scheduled h+=Sigma0(a) movq %rdi,%r14 rorq $42,%rdi @@ -1027,8 +1027,8 @@ L$rounds_16_xx: shrq $6,%r14 rorq $19,%rdi - xorq %r13,%r12 - xorq %r14,%rdi + xorq %r13,%r12 // sigma0(X[(i+1)&0xf]) + xorq %r14,%rdi // sigma1(X[(i+14)&0xf]) addq 112(%rsp),%r12 addq 40(%rsp),%r12 @@ -1040,43 +1040,43 @@ L$rounds_16_xx: xorq %r11,%r13 rorq $5,%r14 - xorq %rbx,%rdi + xorq %rbx,%rdi // f^g movq %r12,40(%rsp) xorq %rdx,%r14 - andq %r11,%rdi + andq %r11,%rdi // (f^g)&e rorq $4,%r13 - addq %rcx,%r12 - xorq %rbx,%rdi + addq %rcx,%r12 // T1+=h + xorq %rbx,%rdi // Ch(e,f,g)=((f^g)&e)^g rorq $6,%r14 xorq %r11,%r13 - addq %rdi,%r12 + addq %rdi,%r12 // T1+=Ch(e,f,g) movq %rdx,%rdi - addq (%rbp),%r12 + addq (%rbp),%r12 // T1+=K[round] xorq %rdx,%r14 - xorq %r8,%rdi - rorq $14,%r13 + xorq %r8,%rdi // a^b, b^c in next round + rorq $14,%r13 // Sigma1(e) movq %r8,%rcx andq %rdi,%r15 - rorq $28,%r14 - addq %r13,%r12 + rorq $28,%r14 // Sigma0(a) + addq %r13,%r12 // T1+=Sigma1(e) - xorq %r15,%rcx - addq %r12,%r10 - addq %r12,%rcx + xorq %r15,%rcx // h=Maj(a,b,c)=Ch(a^b,c,b) + addq %r12,%r10 // d+=T1 + addq %r12,%rcx // h+=T1 - leaq 24(%rbp),%rbp + leaq 24(%rbp),%rbp // round++ movq 56(%rsp),%r13 movq 32(%rsp),%r15 movq %r13,%r12 rorq $7,%r13 - addq %r14,%rcx + addq %r14,%rcx // modulo-scheduled h+=Sigma0(a) movq %r15,%r14 rorq $42,%r15 @@ -1087,8 +1087,8 @@ L$rounds_16_xx: shrq $6,%r14 rorq $19,%r15 - xorq %r13,%r12 - xorq %r14,%r15 + xorq %r13,%r12 // sigma0(X[(i+1)&0xf]) + xorq %r14,%r15 // sigma1(X[(i+14)&0xf]) addq 120(%rsp),%r12 addq 48(%rsp),%r12 @@ -1100,43 +1100,43 @@ L$rounds_16_xx: xorq %r10,%r13 rorq $5,%r14 - xorq %rax,%r15 + xorq %rax,%r15 // f^g movq %r12,48(%rsp) xorq %rcx,%r14 - andq %r10,%r15 + andq %r10,%r15 // (f^g)&e rorq $4,%r13 - addq %rbx,%r12 - xorq %rax,%r15 + addq %rbx,%r12 // T1+=h + xorq %rax,%r15 // Ch(e,f,g)=((f^g)&e)^g rorq $6,%r14 xorq %r10,%r13 - addq %r15,%r12 + addq %r15,%r12 // T1+=Ch(e,f,g) movq %rcx,%r15 - addq (%rbp),%r12 + addq (%rbp),%r12 // T1+=K[round] xorq %rcx,%r14 - xorq %rdx,%r15 - rorq $14,%r13 + xorq %rdx,%r15 // a^b, b^c in next round + rorq $14,%r13 // Sigma1(e) movq %rdx,%rbx andq %r15,%rdi - rorq $28,%r14 - addq %r13,%r12 + rorq $28,%r14 // Sigma0(a) + addq %r13,%r12 // T1+=Sigma1(e) - xorq %rdi,%rbx - addq %r12,%r9 - addq %r12,%rbx + xorq %rdi,%rbx // h=Maj(a,b,c)=Ch(a^b,c,b) + addq %r12,%r9 // d+=T1 + addq %r12,%rbx // h+=T1 - leaq 8(%rbp),%rbp + leaq 8(%rbp),%rbp // round++ movq 64(%rsp),%r13 movq 40(%rsp),%rdi movq %r13,%r12 rorq $7,%r13 - addq %r14,%rbx + addq %r14,%rbx // modulo-scheduled h+=Sigma0(a) movq %rdi,%r14 rorq $42,%rdi @@ -1147,8 +1147,8 @@ L$rounds_16_xx: shrq $6,%r14 rorq $19,%rdi - xorq %r13,%r12 - xorq %r14,%rdi + xorq %r13,%r12 // sigma0(X[(i+1)&0xf]) + xorq %r14,%rdi // sigma1(X[(i+14)&0xf]) addq 0(%rsp),%r12 addq 56(%rsp),%r12 @@ -1160,43 +1160,43 @@ L$rounds_16_xx: xorq %r9,%r13 rorq $5,%r14 - xorq %r11,%rdi + xorq %r11,%rdi // f^g movq %r12,56(%rsp) xorq %rbx,%r14 - andq %r9,%rdi + andq %r9,%rdi // (f^g)&e rorq $4,%r13 - addq %rax,%r12 - xorq %r11,%rdi + addq %rax,%r12 // T1+=h + xorq %r11,%rdi // Ch(e,f,g)=((f^g)&e)^g rorq $6,%r14 xorq %r9,%r13 - addq %rdi,%r12 + addq %rdi,%r12 // T1+=Ch(e,f,g) movq %rbx,%rdi - addq (%rbp),%r12 + addq (%rbp),%r12 // T1+=K[round] xorq %rbx,%r14 - xorq %rcx,%rdi - rorq $14,%r13 + xorq %rcx,%rdi // a^b, b^c in next round + rorq $14,%r13 // Sigma1(e) movq %rcx,%rax andq %rdi,%r15 - rorq $28,%r14 - addq %r13,%r12 + rorq $28,%r14 // Sigma0(a) + addq %r13,%r12 // T1+=Sigma1(e) - xorq %r15,%rax - addq %r12,%r8 - addq %r12,%rax + xorq %r15,%rax // h=Maj(a,b,c)=Ch(a^b,c,b) + addq %r12,%r8 // d+=T1 + addq %r12,%rax // h+=T1 - leaq 24(%rbp),%rbp + leaq 24(%rbp),%rbp // round++ movq 72(%rsp),%r13 movq 48(%rsp),%r15 movq %r13,%r12 rorq $7,%r13 - addq %r14,%rax + addq %r14,%rax // modulo-scheduled h+=Sigma0(a) movq %r15,%r14 rorq $42,%r15 @@ -1207,8 +1207,8 @@ L$rounds_16_xx: shrq $6,%r14 rorq $19,%r15 - xorq %r13,%r12 - xorq %r14,%r15 + xorq %r13,%r12 // sigma0(X[(i+1)&0xf]) + xorq %r14,%r15 // sigma1(X[(i+14)&0xf]) addq 8(%rsp),%r12 addq 64(%rsp),%r12 @@ -1220,43 +1220,43 @@ L$rounds_16_xx: xorq %r8,%r13 rorq $5,%r14 - xorq %r10,%r15 + xorq %r10,%r15 // f^g movq %r12,64(%rsp) xorq %rax,%r14 - andq %r8,%r15 + andq %r8,%r15 // (f^g)&e rorq $4,%r13 - addq %r11,%r12 - xorq %r10,%r15 + addq %r11,%r12 // T1+=h + xorq %r10,%r15 // Ch(e,f,g)=((f^g)&e)^g rorq $6,%r14 xorq %r8,%r13 - addq %r15,%r12 + addq %r15,%r12 // T1+=Ch(e,f,g) movq %rax,%r15 - addq (%rbp),%r12 + addq (%rbp),%r12 // T1+=K[round] xorq %rax,%r14 - xorq %rbx,%r15 - rorq $14,%r13 + xorq %rbx,%r15 // a^b, b^c in next round + rorq $14,%r13 // Sigma1(e) movq %rbx,%r11 andq %r15,%rdi - rorq $28,%r14 - addq %r13,%r12 + rorq $28,%r14 // Sigma0(a) + addq %r13,%r12 // T1+=Sigma1(e) - xorq %rdi,%r11 - addq %r12,%rdx - addq %r12,%r11 + xorq %rdi,%r11 // h=Maj(a,b,c)=Ch(a^b,c,b) + addq %r12,%rdx // d+=T1 + addq %r12,%r11 // h+=T1 - leaq 8(%rbp),%rbp + leaq 8(%rbp),%rbp // round++ movq 80(%rsp),%r13 movq 56(%rsp),%rdi movq %r13,%r12 rorq $7,%r13 - addq %r14,%r11 + addq %r14,%r11 // modulo-scheduled h+=Sigma0(a) movq %rdi,%r14 rorq $42,%rdi @@ -1267,8 +1267,8 @@ L$rounds_16_xx: shrq $6,%r14 rorq $19,%rdi - xorq %r13,%r12 - xorq %r14,%rdi + xorq %r13,%r12 // sigma0(X[(i+1)&0xf]) + xorq %r14,%rdi // sigma1(X[(i+14)&0xf]) addq 16(%rsp),%r12 addq 72(%rsp),%r12 @@ -1280,43 +1280,43 @@ L$rounds_16_xx: xorq %rdx,%r13 rorq $5,%r14 - xorq %r9,%rdi + xorq %r9,%rdi // f^g movq %r12,72(%rsp) xorq %r11,%r14 - andq %rdx,%rdi + andq %rdx,%rdi // (f^g)&e rorq $4,%r13 - addq %r10,%r12 - xorq %r9,%rdi + addq %r10,%r12 // T1+=h + xorq %r9,%rdi // Ch(e,f,g)=((f^g)&e)^g rorq $6,%r14 xorq %rdx,%r13 - addq %rdi,%r12 + addq %rdi,%r12 // T1+=Ch(e,f,g) movq %r11,%rdi - addq (%rbp),%r12 + addq (%rbp),%r12 // T1+=K[round] xorq %r11,%r14 - xorq %rax,%rdi - rorq $14,%r13 + xorq %rax,%rdi // a^b, b^c in next round + rorq $14,%r13 // Sigma1(e) movq %rax,%r10 andq %rdi,%r15 - rorq $28,%r14 - addq %r13,%r12 + rorq $28,%r14 // Sigma0(a) + addq %r13,%r12 // T1+=Sigma1(e) - xorq %r15,%r10 - addq %r12,%rcx - addq %r12,%r10 + xorq %r15,%r10 // h=Maj(a,b,c)=Ch(a^b,c,b) + addq %r12,%rcx // d+=T1 + addq %r12,%r10 // h+=T1 - leaq 24(%rbp),%rbp + leaq 24(%rbp),%rbp // round++ movq 88(%rsp),%r13 movq 64(%rsp),%r15 movq %r13,%r12 rorq $7,%r13 - addq %r14,%r10 + addq %r14,%r10 // modulo-scheduled h+=Sigma0(a) movq %r15,%r14 rorq $42,%r15 @@ -1327,8 +1327,8 @@ L$rounds_16_xx: shrq $6,%r14 rorq $19,%r15 - xorq %r13,%r12 - xorq %r14,%r15 + xorq %r13,%r12 // sigma0(X[(i+1)&0xf]) + xorq %r14,%r15 // sigma1(X[(i+14)&0xf]) addq 24(%rsp),%r12 addq 80(%rsp),%r12 @@ -1340,43 +1340,43 @@ L$rounds_16_xx: xorq %rcx,%r13 rorq $5,%r14 - xorq %r8,%r15 + xorq %r8,%r15 // f^g movq %r12,80(%rsp) xorq %r10,%r14 - andq %rcx,%r15 + andq %rcx,%r15 // (f^g)&e rorq $4,%r13 - addq %r9,%r12 - xorq %r8,%r15 + addq %r9,%r12 // T1+=h + xorq %r8,%r15 // Ch(e,f,g)=((f^g)&e)^g rorq $6,%r14 xorq %rcx,%r13 - addq %r15,%r12 + addq %r15,%r12 // T1+=Ch(e,f,g) movq %r10,%r15 - addq (%rbp),%r12 + addq (%rbp),%r12 // T1+=K[round] xorq %r10,%r14 - xorq %r11,%r15 - rorq $14,%r13 + xorq %r11,%r15 // a^b, b^c in next round + rorq $14,%r13 // Sigma1(e) movq %r11,%r9 andq %r15,%rdi - rorq $28,%r14 - addq %r13,%r12 + rorq $28,%r14 // Sigma0(a) + addq %r13,%r12 // T1+=Sigma1(e) - xorq %rdi,%r9 - addq %r12,%rbx - addq %r12,%r9 + xorq %rdi,%r9 // h=Maj(a,b,c)=Ch(a^b,c,b) + addq %r12,%rbx // d+=T1 + addq %r12,%r9 // h+=T1 - leaq 8(%rbp),%rbp + leaq 8(%rbp),%rbp // round++ movq 96(%rsp),%r13 movq 72(%rsp),%rdi movq %r13,%r12 rorq $7,%r13 - addq %r14,%r9 + addq %r14,%r9 // modulo-scheduled h+=Sigma0(a) movq %rdi,%r14 rorq $42,%rdi @@ -1387,8 +1387,8 @@ L$rounds_16_xx: shrq $6,%r14 rorq $19,%rdi - xorq %r13,%r12 - xorq %r14,%rdi + xorq %r13,%r12 // sigma0(X[(i+1)&0xf]) + xorq %r14,%rdi // sigma1(X[(i+14)&0xf]) addq 32(%rsp),%r12 addq 88(%rsp),%r12 @@ -1400,43 +1400,43 @@ L$rounds_16_xx: xorq %rbx,%r13 rorq $5,%r14 - xorq %rdx,%rdi + xorq %rdx,%rdi // f^g movq %r12,88(%rsp) xorq %r9,%r14 - andq %rbx,%rdi + andq %rbx,%rdi // (f^g)&e rorq $4,%r13 - addq %r8,%r12 - xorq %rdx,%rdi + addq %r8,%r12 // T1+=h + xorq %rdx,%rdi // Ch(e,f,g)=((f^g)&e)^g rorq $6,%r14 xorq %rbx,%r13 - addq %rdi,%r12 + addq %rdi,%r12 // T1+=Ch(e,f,g) movq %r9,%rdi - addq (%rbp),%r12 + addq (%rbp),%r12 // T1+=K[round] xorq %r9,%r14 - xorq %r10,%rdi - rorq $14,%r13 + xorq %r10,%rdi // a^b, b^c in next round + rorq $14,%r13 // Sigma1(e) movq %r10,%r8 andq %rdi,%r15 - rorq $28,%r14 - addq %r13,%r12 + rorq $28,%r14 // Sigma0(a) + addq %r13,%r12 // T1+=Sigma1(e) - xorq %r15,%r8 - addq %r12,%rax - addq %r12,%r8 + xorq %r15,%r8 // h=Maj(a,b,c)=Ch(a^b,c,b) + addq %r12,%rax // d+=T1 + addq %r12,%r8 // h+=T1 - leaq 24(%rbp),%rbp + leaq 24(%rbp),%rbp // round++ movq 104(%rsp),%r13 movq 80(%rsp),%r15 movq %r13,%r12 rorq $7,%r13 - addq %r14,%r8 + addq %r14,%r8 // modulo-scheduled h+=Sigma0(a) movq %r15,%r14 rorq $42,%r15 @@ -1447,8 +1447,8 @@ L$rounds_16_xx: shrq $6,%r14 rorq $19,%r15 - xorq %r13,%r12 - xorq %r14,%r15 + xorq %r13,%r12 // sigma0(X[(i+1)&0xf]) + xorq %r14,%r15 // sigma1(X[(i+14)&0xf]) addq 40(%rsp),%r12 addq 96(%rsp),%r12 @@ -1460,43 +1460,43 @@ L$rounds_16_xx: xorq %rax,%r13 rorq $5,%r14 - xorq %rcx,%r15 + xorq %rcx,%r15 // f^g movq %r12,96(%rsp) xorq %r8,%r14 - andq %rax,%r15 + andq %rax,%r15 // (f^g)&e rorq $4,%r13 - addq %rdx,%r12 - xorq %rcx,%r15 + addq %rdx,%r12 // T1+=h + xorq %rcx,%r15 // Ch(e,f,g)=((f^g)&e)^g rorq $6,%r14 xorq %rax,%r13 - addq %r15,%r12 + addq %r15,%r12 // T1+=Ch(e,f,g) movq %r8,%r15 - addq (%rbp),%r12 + addq (%rbp),%r12 // T1+=K[round] xorq %r8,%r14 - xorq %r9,%r15 - rorq $14,%r13 + xorq %r9,%r15 // a^b, b^c in next round + rorq $14,%r13 // Sigma1(e) movq %r9,%rdx andq %r15,%rdi - rorq $28,%r14 - addq %r13,%r12 + rorq $28,%r14 // Sigma0(a) + addq %r13,%r12 // T1+=Sigma1(e) - xorq %rdi,%rdx - addq %r12,%r11 - addq %r12,%rdx + xorq %rdi,%rdx // h=Maj(a,b,c)=Ch(a^b,c,b) + addq %r12,%r11 // d+=T1 + addq %r12,%rdx // h+=T1 - leaq 8(%rbp),%rbp + leaq 8(%rbp),%rbp // round++ movq 112(%rsp),%r13 movq 88(%rsp),%rdi movq %r13,%r12 rorq $7,%r13 - addq %r14,%rdx + addq %r14,%rdx // modulo-scheduled h+=Sigma0(a) movq %rdi,%r14 rorq $42,%rdi @@ -1507,8 +1507,8 @@ L$rounds_16_xx: shrq $6,%r14 rorq $19,%rdi - xorq %r13,%r12 - xorq %r14,%rdi + xorq %r13,%r12 // sigma0(X[(i+1)&0xf]) + xorq %r14,%rdi // sigma1(X[(i+14)&0xf]) addq 48(%rsp),%r12 addq 104(%rsp),%r12 @@ -1520,43 +1520,43 @@ L$rounds_16_xx: xorq %r11,%r13 rorq $5,%r14 - xorq %rbx,%rdi + xorq %rbx,%rdi // f^g movq %r12,104(%rsp) xorq %rdx,%r14 - andq %r11,%rdi + andq %r11,%rdi // (f^g)&e rorq $4,%r13 - addq %rcx,%r12 - xorq %rbx,%rdi + addq %rcx,%r12 // T1+=h + xorq %rbx,%rdi // Ch(e,f,g)=((f^g)&e)^g rorq $6,%r14 xorq %r11,%r13 - addq %rdi,%r12 + addq %rdi,%r12 // T1+=Ch(e,f,g) movq %rdx,%rdi - addq (%rbp),%r12 + addq (%rbp),%r12 // T1+=K[round] xorq %rdx,%r14 - xorq %r8,%rdi - rorq $14,%r13 + xorq %r8,%rdi // a^b, b^c in next round + rorq $14,%r13 // Sigma1(e) movq %r8,%rcx andq %rdi,%r15 - rorq $28,%r14 - addq %r13,%r12 + rorq $28,%r14 // Sigma0(a) + addq %r13,%r12 // T1+=Sigma1(e) - xorq %r15,%rcx - addq %r12,%r10 - addq %r12,%rcx + xorq %r15,%rcx // h=Maj(a,b,c)=Ch(a^b,c,b) + addq %r12,%r10 // d+=T1 + addq %r12,%rcx // h+=T1 - leaq 24(%rbp),%rbp + leaq 24(%rbp),%rbp // round++ movq 120(%rsp),%r13 movq 96(%rsp),%r15 movq %r13,%r12 rorq $7,%r13 - addq %r14,%rcx + addq %r14,%rcx // modulo-scheduled h+=Sigma0(a) movq %r15,%r14 rorq $42,%r15 @@ -1567,8 +1567,8 @@ L$rounds_16_xx: shrq $6,%r14 rorq $19,%r15 - xorq %r13,%r12 - xorq %r14,%r15 + xorq %r13,%r12 // sigma0(X[(i+1)&0xf]) + xorq %r14,%r15 // sigma1(X[(i+14)&0xf]) addq 56(%rsp),%r12 addq 112(%rsp),%r12 @@ -1580,43 +1580,43 @@ L$rounds_16_xx: xorq %r10,%r13 rorq $5,%r14 - xorq %rax,%r15 + xorq %rax,%r15 // f^g movq %r12,112(%rsp) xorq %rcx,%r14 - andq %r10,%r15 + andq %r10,%r15 // (f^g)&e rorq $4,%r13 - addq %rbx,%r12 - xorq %rax,%r15 + addq %rbx,%r12 // T1+=h + xorq %rax,%r15 // Ch(e,f,g)=((f^g)&e)^g rorq $6,%r14 xorq %r10,%r13 - addq %r15,%r12 + addq %r15,%r12 // T1+=Ch(e,f,g) movq %rcx,%r15 - addq (%rbp),%r12 + addq (%rbp),%r12 // T1+=K[round] xorq %rcx,%r14 - xorq %rdx,%r15 - rorq $14,%r13 + xorq %rdx,%r15 // a^b, b^c in next round + rorq $14,%r13 // Sigma1(e) movq %rdx,%rbx andq %r15,%rdi - rorq $28,%r14 - addq %r13,%r12 + rorq $28,%r14 // Sigma0(a) + addq %r13,%r12 // T1+=Sigma1(e) - xorq %rdi,%rbx - addq %r12,%r9 - addq %r12,%rbx + xorq %rdi,%rbx // h=Maj(a,b,c)=Ch(a^b,c,b) + addq %r12,%r9 // d+=T1 + addq %r12,%rbx // h+=T1 - leaq 8(%rbp),%rbp + leaq 8(%rbp),%rbp // round++ movq 0(%rsp),%r13 movq 104(%rsp),%rdi movq %r13,%r12 rorq $7,%r13 - addq %r14,%rbx + addq %r14,%rbx // modulo-scheduled h+=Sigma0(a) movq %rdi,%r14 rorq $42,%rdi @@ -1627,8 +1627,8 @@ L$rounds_16_xx: shrq $6,%r14 rorq $19,%rdi - xorq %r13,%r12 - xorq %r14,%rdi + xorq %r13,%r12 // sigma0(X[(i+1)&0xf]) + xorq %r14,%rdi // sigma1(X[(i+14)&0xf]) addq 64(%rsp),%r12 addq 120(%rsp),%r12 @@ -1640,42 +1640,42 @@ L$rounds_16_xx: xorq %r9,%r13 rorq $5,%r14 - xorq %r11,%rdi + xorq %r11,%rdi // f^g movq %r12,120(%rsp) xorq %rbx,%r14 - andq %r9,%rdi + andq %r9,%rdi // (f^g)&e rorq $4,%r13 - addq %rax,%r12 - xorq %r11,%rdi + addq %rax,%r12 // T1+=h + xorq %r11,%rdi // Ch(e,f,g)=((f^g)&e)^g rorq $6,%r14 xorq %r9,%r13 - addq %rdi,%r12 + addq %rdi,%r12 // T1+=Ch(e,f,g) movq %rbx,%rdi - addq (%rbp),%r12 + addq (%rbp),%r12 // T1+=K[round] xorq %rbx,%r14 - xorq %rcx,%rdi - rorq $14,%r13 + xorq %rcx,%rdi // a^b, b^c in next round + rorq $14,%r13 // Sigma1(e) movq %rcx,%rax andq %rdi,%r15 - rorq $28,%r14 - addq %r13,%r12 + rorq $28,%r14 // Sigma0(a) + addq %r13,%r12 // T1+=Sigma1(e) - xorq %r15,%rax - addq %r12,%r8 - addq %r12,%rax + xorq %r15,%rax // h=Maj(a,b,c)=Ch(a^b,c,b) + addq %r12,%r8 // d+=T1 + addq %r12,%rax // h+=T1 - leaq 24(%rbp),%rbp + leaq 24(%rbp),%rbp // round++ cmpb $0,7(%rbp) jnz L$rounds_16_xx movq 128+0(%rsp),%rdi - addq %r14,%rax + addq %r14,%rax // modulo-scheduled h+=Sigma0(a) leaq 128(%rsi),%rsi addq 0(%rdi),%rax @@ -1812,10 +1812,11 @@ K512: .private_extern _sha512_block_data_order_avx .p2align 6 +.alt_entry _sha512_block_data_order_avx _sha512_block_data_order_avx: _CET_ENDBR - movq %rsp,%rax + movq %rsp,%rax // copy %rsp pushq %rbx @@ -1829,14 +1830,14 @@ _CET_ENDBR pushq %r15 - shlq $4,%rdx + shlq $4,%rdx // num*16 subq $160,%rsp - leaq (%rsi,%rdx,8),%rdx - andq $-64,%rsp - movq %rdi,128+0(%rsp) - movq %rsi,128+8(%rsp) - movq %rdx,128+16(%rsp) - movq %rax,152(%rsp) + leaq (%rsi,%rdx,8),%rdx // inp+num*16*8 + andq $-64,%rsp // align stack frame + movq %rdi,128+0(%rsp) // save ctx, 1st arg + movq %rsi,128+8(%rsp) // save inp, 2nd arh + movq %rdx,128+16(%rsp) // save end pointer, "3rd" arg + movq %rax,152(%rsp) // save copy of %rsp L$prologue_avx: @@ -1854,7 +1855,7 @@ L$prologue_avx: L$loop_avx: vmovdqa K512+1280(%rip),%xmm11 vmovdqu 0(%rsi),%xmm0 - leaq K512+128(%rip),%rbp + leaq K512+128(%rip),%rbp // size optimization vmovdqu 16(%rsi),%xmm1 vmovdqu 32(%rsi),%xmm2 vpshufb %xmm11,%xmm0,%xmm0 @@ -1887,7 +1888,7 @@ L$loop_avx: vmovdqa %xmm9,80(%rsp) movq %rbx,%rdi vmovdqa %xmm10,96(%rsp) - xorq %rcx,%rdi + xorq %rcx,%rdi // magic vmovdqa %xmm11,112(%rsp) movq %r8,%r13 jmp L$avx_00_47 diff --git a/third_party/boringssl/gen/bcm/sha512-x86_64-linux.S b/third_party/boringssl/gen/bcm/sha512-x86_64-linux.S index bbef9430e..f701e8856 100644 --- a/third_party/boringssl/gen/bcm/sha512-x86_64-linux.S +++ b/third_party/boringssl/gen/bcm/sha512-x86_64-linux.S @@ -13,7 +13,7 @@ sha512_block_data_order_nohw: .cfi_startproc _CET_ENDBR - movq %rsp,%rax + movq %rsp,%rax // copy %rsp .cfi_def_cfa_register %rax pushq %rbx .cfi_offset %rbx,-16 @@ -27,14 +27,14 @@ _CET_ENDBR .cfi_offset %r14,-48 pushq %r15 .cfi_offset %r15,-56 - shlq $4,%rdx + shlq $4,%rdx // num*16 subq $128+32,%rsp - leaq (%rsi,%rdx,8),%rdx - andq $-64,%rsp - movq %rdi,128+0(%rsp) - movq %rsi,128+8(%rsp) - movq %rdx,128+16(%rsp) - movq %rax,152(%rsp) + leaq (%rsi,%rdx,8),%rdx // inp+num*16*8 + andq $-64,%rsp // align stack frame + movq %rdi,128+0(%rsp) // save ctx, 1st arg + movq %rsi,128+8(%rsp) // save inp, 2nd arh + movq %rdx,128+16(%rsp) // save end pointer, "3rd" arg + movq %rax,152(%rsp) // save copy of %rsp .cfi_escape 0x0f,0x06,0x77,0x98,0x01,0x06,0x23,0x08 .Lprologue: @@ -52,7 +52,7 @@ _CET_ENDBR .Lloop: movq %rbx,%rdi leaq K512(%rip),%rbp - xorq %rcx,%rdi + xorq %rcx,%rdi // magic movq 0(%rsi),%r12 movq %r8,%r13 movq %rax,%r14 @@ -62,38 +62,38 @@ _CET_ENDBR xorq %r8,%r13 rorq $5,%r14 - xorq %r10,%r15 + xorq %r10,%r15 // f^g movq %r12,0(%rsp) xorq %rax,%r14 - andq %r8,%r15 + andq %r8,%r15 // (f^g)&e rorq $4,%r13 - addq %r11,%r12 - xorq %r10,%r15 + addq %r11,%r12 // T1+=h + xorq %r10,%r15 // Ch(e,f,g)=((f^g)&e)^g rorq $6,%r14 xorq %r8,%r13 - addq %r15,%r12 + addq %r15,%r12 // T1+=Ch(e,f,g) movq %rax,%r15 - addq (%rbp),%r12 + addq (%rbp),%r12 // T1+=K[round] xorq %rax,%r14 - xorq %rbx,%r15 - rorq $14,%r13 + xorq %rbx,%r15 // a^b, b^c in next round + rorq $14,%r13 // Sigma1(e) movq %rbx,%r11 andq %r15,%rdi - rorq $28,%r14 - addq %r13,%r12 + rorq $28,%r14 // Sigma0(a) + addq %r13,%r12 // T1+=Sigma1(e) - xorq %rdi,%r11 - addq %r12,%rdx - addq %r12,%r11 + xorq %rdi,%r11 // h=Maj(a,b,c)=Ch(a^b,c,b) + addq %r12,%rdx // d+=T1 + addq %r12,%r11 // h+=T1 - leaq 8(%rbp),%rbp - addq %r14,%r11 + leaq 8(%rbp),%rbp // round++ + addq %r14,%r11 // h+=Sigma0(a) movq 8(%rsi),%r12 movq %rdx,%r13 movq %r11,%r14 @@ -103,38 +103,38 @@ _CET_ENDBR xorq %rdx,%r13 rorq $5,%r14 - xorq %r9,%rdi + xorq %r9,%rdi // f^g movq %r12,8(%rsp) xorq %r11,%r14 - andq %rdx,%rdi + andq %rdx,%rdi // (f^g)&e rorq $4,%r13 - addq %r10,%r12 - xorq %r9,%rdi + addq %r10,%r12 // T1+=h + xorq %r9,%rdi // Ch(e,f,g)=((f^g)&e)^g rorq $6,%r14 xorq %rdx,%r13 - addq %rdi,%r12 + addq %rdi,%r12 // T1+=Ch(e,f,g) movq %r11,%rdi - addq (%rbp),%r12 + addq (%rbp),%r12 // T1+=K[round] xorq %r11,%r14 - xorq %rax,%rdi - rorq $14,%r13 + xorq %rax,%rdi // a^b, b^c in next round + rorq $14,%r13 // Sigma1(e) movq %rax,%r10 andq %rdi,%r15 - rorq $28,%r14 - addq %r13,%r12 + rorq $28,%r14 // Sigma0(a) + addq %r13,%r12 // T1+=Sigma1(e) - xorq %r15,%r10 - addq %r12,%rcx - addq %r12,%r10 + xorq %r15,%r10 // h=Maj(a,b,c)=Ch(a^b,c,b) + addq %r12,%rcx // d+=T1 + addq %r12,%r10 // h+=T1 - leaq 24(%rbp),%rbp - addq %r14,%r10 + leaq 24(%rbp),%rbp // round++ + addq %r14,%r10 // h+=Sigma0(a) movq 16(%rsi),%r12 movq %rcx,%r13 movq %r10,%r14 @@ -144,38 +144,38 @@ _CET_ENDBR xorq %rcx,%r13 rorq $5,%r14 - xorq %r8,%r15 + xorq %r8,%r15 // f^g movq %r12,16(%rsp) xorq %r10,%r14 - andq %rcx,%r15 + andq %rcx,%r15 // (f^g)&e rorq $4,%r13 - addq %r9,%r12 - xorq %r8,%r15 + addq %r9,%r12 // T1+=h + xorq %r8,%r15 // Ch(e,f,g)=((f^g)&e)^g rorq $6,%r14 xorq %rcx,%r13 - addq %r15,%r12 + addq %r15,%r12 // T1+=Ch(e,f,g) movq %r10,%r15 - addq (%rbp),%r12 + addq (%rbp),%r12 // T1+=K[round] xorq %r10,%r14 - xorq %r11,%r15 - rorq $14,%r13 + xorq %r11,%r15 // a^b, b^c in next round + rorq $14,%r13 // Sigma1(e) movq %r11,%r9 andq %r15,%rdi - rorq $28,%r14 - addq %r13,%r12 + rorq $28,%r14 // Sigma0(a) + addq %r13,%r12 // T1+=Sigma1(e) - xorq %rdi,%r9 - addq %r12,%rbx - addq %r12,%r9 + xorq %rdi,%r9 // h=Maj(a,b,c)=Ch(a^b,c,b) + addq %r12,%rbx // d+=T1 + addq %r12,%r9 // h+=T1 - leaq 8(%rbp),%rbp - addq %r14,%r9 + leaq 8(%rbp),%rbp // round++ + addq %r14,%r9 // h+=Sigma0(a) movq 24(%rsi),%r12 movq %rbx,%r13 movq %r9,%r14 @@ -185,38 +185,38 @@ _CET_ENDBR xorq %rbx,%r13 rorq $5,%r14 - xorq %rdx,%rdi + xorq %rdx,%rdi // f^g movq %r12,24(%rsp) xorq %r9,%r14 - andq %rbx,%rdi + andq %rbx,%rdi // (f^g)&e rorq $4,%r13 - addq %r8,%r12 - xorq %rdx,%rdi + addq %r8,%r12 // T1+=h + xorq %rdx,%rdi // Ch(e,f,g)=((f^g)&e)^g rorq $6,%r14 xorq %rbx,%r13 - addq %rdi,%r12 + addq %rdi,%r12 // T1+=Ch(e,f,g) movq %r9,%rdi - addq (%rbp),%r12 + addq (%rbp),%r12 // T1+=K[round] xorq %r9,%r14 - xorq %r10,%rdi - rorq $14,%r13 + xorq %r10,%rdi // a^b, b^c in next round + rorq $14,%r13 // Sigma1(e) movq %r10,%r8 andq %rdi,%r15 - rorq $28,%r14 - addq %r13,%r12 + rorq $28,%r14 // Sigma0(a) + addq %r13,%r12 // T1+=Sigma1(e) - xorq %r15,%r8 - addq %r12,%rax - addq %r12,%r8 + xorq %r15,%r8 // h=Maj(a,b,c)=Ch(a^b,c,b) + addq %r12,%rax // d+=T1 + addq %r12,%r8 // h+=T1 - leaq 24(%rbp),%rbp - addq %r14,%r8 + leaq 24(%rbp),%rbp // round++ + addq %r14,%r8 // h+=Sigma0(a) movq 32(%rsi),%r12 movq %rax,%r13 movq %r8,%r14 @@ -226,38 +226,38 @@ _CET_ENDBR xorq %rax,%r13 rorq $5,%r14 - xorq %rcx,%r15 + xorq %rcx,%r15 // f^g movq %r12,32(%rsp) xorq %r8,%r14 - andq %rax,%r15 + andq %rax,%r15 // (f^g)&e rorq $4,%r13 - addq %rdx,%r12 - xorq %rcx,%r15 + addq %rdx,%r12 // T1+=h + xorq %rcx,%r15 // Ch(e,f,g)=((f^g)&e)^g rorq $6,%r14 xorq %rax,%r13 - addq %r15,%r12 + addq %r15,%r12 // T1+=Ch(e,f,g) movq %r8,%r15 - addq (%rbp),%r12 + addq (%rbp),%r12 // T1+=K[round] xorq %r8,%r14 - xorq %r9,%r15 - rorq $14,%r13 + xorq %r9,%r15 // a^b, b^c in next round + rorq $14,%r13 // Sigma1(e) movq %r9,%rdx andq %r15,%rdi - rorq $28,%r14 - addq %r13,%r12 + rorq $28,%r14 // Sigma0(a) + addq %r13,%r12 // T1+=Sigma1(e) - xorq %rdi,%rdx - addq %r12,%r11 - addq %r12,%rdx + xorq %rdi,%rdx // h=Maj(a,b,c)=Ch(a^b,c,b) + addq %r12,%r11 // d+=T1 + addq %r12,%rdx // h+=T1 - leaq 8(%rbp),%rbp - addq %r14,%rdx + leaq 8(%rbp),%rbp // round++ + addq %r14,%rdx // h+=Sigma0(a) movq 40(%rsi),%r12 movq %r11,%r13 movq %rdx,%r14 @@ -267,38 +267,38 @@ _CET_ENDBR xorq %r11,%r13 rorq $5,%r14 - xorq %rbx,%rdi + xorq %rbx,%rdi // f^g movq %r12,40(%rsp) xorq %rdx,%r14 - andq %r11,%rdi + andq %r11,%rdi // (f^g)&e rorq $4,%r13 - addq %rcx,%r12 - xorq %rbx,%rdi + addq %rcx,%r12 // T1+=h + xorq %rbx,%rdi // Ch(e,f,g)=((f^g)&e)^g rorq $6,%r14 xorq %r11,%r13 - addq %rdi,%r12 + addq %rdi,%r12 // T1+=Ch(e,f,g) movq %rdx,%rdi - addq (%rbp),%r12 + addq (%rbp),%r12 // T1+=K[round] xorq %rdx,%r14 - xorq %r8,%rdi - rorq $14,%r13 + xorq %r8,%rdi // a^b, b^c in next round + rorq $14,%r13 // Sigma1(e) movq %r8,%rcx andq %rdi,%r15 - rorq $28,%r14 - addq %r13,%r12 + rorq $28,%r14 // Sigma0(a) + addq %r13,%r12 // T1+=Sigma1(e) - xorq %r15,%rcx - addq %r12,%r10 - addq %r12,%rcx + xorq %r15,%rcx // h=Maj(a,b,c)=Ch(a^b,c,b) + addq %r12,%r10 // d+=T1 + addq %r12,%rcx // h+=T1 - leaq 24(%rbp),%rbp - addq %r14,%rcx + leaq 24(%rbp),%rbp // round++ + addq %r14,%rcx // h+=Sigma0(a) movq 48(%rsi),%r12 movq %r10,%r13 movq %rcx,%r14 @@ -308,38 +308,38 @@ _CET_ENDBR xorq %r10,%r13 rorq $5,%r14 - xorq %rax,%r15 + xorq %rax,%r15 // f^g movq %r12,48(%rsp) xorq %rcx,%r14 - andq %r10,%r15 + andq %r10,%r15 // (f^g)&e rorq $4,%r13 - addq %rbx,%r12 - xorq %rax,%r15 + addq %rbx,%r12 // T1+=h + xorq %rax,%r15 // Ch(e,f,g)=((f^g)&e)^g rorq $6,%r14 xorq %r10,%r13 - addq %r15,%r12 + addq %r15,%r12 // T1+=Ch(e,f,g) movq %rcx,%r15 - addq (%rbp),%r12 + addq (%rbp),%r12 // T1+=K[round] xorq %rcx,%r14 - xorq %rdx,%r15 - rorq $14,%r13 + xorq %rdx,%r15 // a^b, b^c in next round + rorq $14,%r13 // Sigma1(e) movq %rdx,%rbx andq %r15,%rdi - rorq $28,%r14 - addq %r13,%r12 + rorq $28,%r14 // Sigma0(a) + addq %r13,%r12 // T1+=Sigma1(e) - xorq %rdi,%rbx - addq %r12,%r9 - addq %r12,%rbx + xorq %rdi,%rbx // h=Maj(a,b,c)=Ch(a^b,c,b) + addq %r12,%r9 // d+=T1 + addq %r12,%rbx // h+=T1 - leaq 8(%rbp),%rbp - addq %r14,%rbx + leaq 8(%rbp),%rbp // round++ + addq %r14,%rbx // h+=Sigma0(a) movq 56(%rsi),%r12 movq %r9,%r13 movq %rbx,%r14 @@ -349,38 +349,38 @@ _CET_ENDBR xorq %r9,%r13 rorq $5,%r14 - xorq %r11,%rdi + xorq %r11,%rdi // f^g movq %r12,56(%rsp) xorq %rbx,%r14 - andq %r9,%rdi + andq %r9,%rdi // (f^g)&e rorq $4,%r13 - addq %rax,%r12 - xorq %r11,%rdi + addq %rax,%r12 // T1+=h + xorq %r11,%rdi // Ch(e,f,g)=((f^g)&e)^g rorq $6,%r14 xorq %r9,%r13 - addq %rdi,%r12 + addq %rdi,%r12 // T1+=Ch(e,f,g) movq %rbx,%rdi - addq (%rbp),%r12 + addq (%rbp),%r12 // T1+=K[round] xorq %rbx,%r14 - xorq %rcx,%rdi - rorq $14,%r13 + xorq %rcx,%rdi // a^b, b^c in next round + rorq $14,%r13 // Sigma1(e) movq %rcx,%rax andq %rdi,%r15 - rorq $28,%r14 - addq %r13,%r12 + rorq $28,%r14 // Sigma0(a) + addq %r13,%r12 // T1+=Sigma1(e) - xorq %r15,%rax - addq %r12,%r8 - addq %r12,%rax + xorq %r15,%rax // h=Maj(a,b,c)=Ch(a^b,c,b) + addq %r12,%r8 // d+=T1 + addq %r12,%rax // h+=T1 - leaq 24(%rbp),%rbp - addq %r14,%rax + leaq 24(%rbp),%rbp // round++ + addq %r14,%rax // h+=Sigma0(a) movq 64(%rsi),%r12 movq %r8,%r13 movq %rax,%r14 @@ -390,38 +390,38 @@ _CET_ENDBR xorq %r8,%r13 rorq $5,%r14 - xorq %r10,%r15 + xorq %r10,%r15 // f^g movq %r12,64(%rsp) xorq %rax,%r14 - andq %r8,%r15 + andq %r8,%r15 // (f^g)&e rorq $4,%r13 - addq %r11,%r12 - xorq %r10,%r15 + addq %r11,%r12 // T1+=h + xorq %r10,%r15 // Ch(e,f,g)=((f^g)&e)^g rorq $6,%r14 xorq %r8,%r13 - addq %r15,%r12 + addq %r15,%r12 // T1+=Ch(e,f,g) movq %rax,%r15 - addq (%rbp),%r12 + addq (%rbp),%r12 // T1+=K[round] xorq %rax,%r14 - xorq %rbx,%r15 - rorq $14,%r13 + xorq %rbx,%r15 // a^b, b^c in next round + rorq $14,%r13 // Sigma1(e) movq %rbx,%r11 andq %r15,%rdi - rorq $28,%r14 - addq %r13,%r12 + rorq $28,%r14 // Sigma0(a) + addq %r13,%r12 // T1+=Sigma1(e) - xorq %rdi,%r11 - addq %r12,%rdx - addq %r12,%r11 + xorq %rdi,%r11 // h=Maj(a,b,c)=Ch(a^b,c,b) + addq %r12,%rdx // d+=T1 + addq %r12,%r11 // h+=T1 - leaq 8(%rbp),%rbp - addq %r14,%r11 + leaq 8(%rbp),%rbp // round++ + addq %r14,%r11 // h+=Sigma0(a) movq 72(%rsi),%r12 movq %rdx,%r13 movq %r11,%r14 @@ -431,38 +431,38 @@ _CET_ENDBR xorq %rdx,%r13 rorq $5,%r14 - xorq %r9,%rdi + xorq %r9,%rdi // f^g movq %r12,72(%rsp) xorq %r11,%r14 - andq %rdx,%rdi + andq %rdx,%rdi // (f^g)&e rorq $4,%r13 - addq %r10,%r12 - xorq %r9,%rdi + addq %r10,%r12 // T1+=h + xorq %r9,%rdi // Ch(e,f,g)=((f^g)&e)^g rorq $6,%r14 xorq %rdx,%r13 - addq %rdi,%r12 + addq %rdi,%r12 // T1+=Ch(e,f,g) movq %r11,%rdi - addq (%rbp),%r12 + addq (%rbp),%r12 // T1+=K[round] xorq %r11,%r14 - xorq %rax,%rdi - rorq $14,%r13 + xorq %rax,%rdi // a^b, b^c in next round + rorq $14,%r13 // Sigma1(e) movq %rax,%r10 andq %rdi,%r15 - rorq $28,%r14 - addq %r13,%r12 + rorq $28,%r14 // Sigma0(a) + addq %r13,%r12 // T1+=Sigma1(e) - xorq %r15,%r10 - addq %r12,%rcx - addq %r12,%r10 + xorq %r15,%r10 // h=Maj(a,b,c)=Ch(a^b,c,b) + addq %r12,%rcx // d+=T1 + addq %r12,%r10 // h+=T1 - leaq 24(%rbp),%rbp - addq %r14,%r10 + leaq 24(%rbp),%rbp // round++ + addq %r14,%r10 // h+=Sigma0(a) movq 80(%rsi),%r12 movq %rcx,%r13 movq %r10,%r14 @@ -472,38 +472,38 @@ _CET_ENDBR xorq %rcx,%r13 rorq $5,%r14 - xorq %r8,%r15 + xorq %r8,%r15 // f^g movq %r12,80(%rsp) xorq %r10,%r14 - andq %rcx,%r15 + andq %rcx,%r15 // (f^g)&e rorq $4,%r13 - addq %r9,%r12 - xorq %r8,%r15 + addq %r9,%r12 // T1+=h + xorq %r8,%r15 // Ch(e,f,g)=((f^g)&e)^g rorq $6,%r14 xorq %rcx,%r13 - addq %r15,%r12 + addq %r15,%r12 // T1+=Ch(e,f,g) movq %r10,%r15 - addq (%rbp),%r12 + addq (%rbp),%r12 // T1+=K[round] xorq %r10,%r14 - xorq %r11,%r15 - rorq $14,%r13 + xorq %r11,%r15 // a^b, b^c in next round + rorq $14,%r13 // Sigma1(e) movq %r11,%r9 andq %r15,%rdi - rorq $28,%r14 - addq %r13,%r12 + rorq $28,%r14 // Sigma0(a) + addq %r13,%r12 // T1+=Sigma1(e) - xorq %rdi,%r9 - addq %r12,%rbx - addq %r12,%r9 + xorq %rdi,%r9 // h=Maj(a,b,c)=Ch(a^b,c,b) + addq %r12,%rbx // d+=T1 + addq %r12,%r9 // h+=T1 - leaq 8(%rbp),%rbp - addq %r14,%r9 + leaq 8(%rbp),%rbp // round++ + addq %r14,%r9 // h+=Sigma0(a) movq 88(%rsi),%r12 movq %rbx,%r13 movq %r9,%r14 @@ -513,38 +513,38 @@ _CET_ENDBR xorq %rbx,%r13 rorq $5,%r14 - xorq %rdx,%rdi + xorq %rdx,%rdi // f^g movq %r12,88(%rsp) xorq %r9,%r14 - andq %rbx,%rdi + andq %rbx,%rdi // (f^g)&e rorq $4,%r13 - addq %r8,%r12 - xorq %rdx,%rdi + addq %r8,%r12 // T1+=h + xorq %rdx,%rdi // Ch(e,f,g)=((f^g)&e)^g rorq $6,%r14 xorq %rbx,%r13 - addq %rdi,%r12 + addq %rdi,%r12 // T1+=Ch(e,f,g) movq %r9,%rdi - addq (%rbp),%r12 + addq (%rbp),%r12 // T1+=K[round] xorq %r9,%r14 - xorq %r10,%rdi - rorq $14,%r13 + xorq %r10,%rdi // a^b, b^c in next round + rorq $14,%r13 // Sigma1(e) movq %r10,%r8 andq %rdi,%r15 - rorq $28,%r14 - addq %r13,%r12 + rorq $28,%r14 // Sigma0(a) + addq %r13,%r12 // T1+=Sigma1(e) - xorq %r15,%r8 - addq %r12,%rax - addq %r12,%r8 + xorq %r15,%r8 // h=Maj(a,b,c)=Ch(a^b,c,b) + addq %r12,%rax // d+=T1 + addq %r12,%r8 // h+=T1 - leaq 24(%rbp),%rbp - addq %r14,%r8 + leaq 24(%rbp),%rbp // round++ + addq %r14,%r8 // h+=Sigma0(a) movq 96(%rsi),%r12 movq %rax,%r13 movq %r8,%r14 @@ -554,38 +554,38 @@ _CET_ENDBR xorq %rax,%r13 rorq $5,%r14 - xorq %rcx,%r15 + xorq %rcx,%r15 // f^g movq %r12,96(%rsp) xorq %r8,%r14 - andq %rax,%r15 + andq %rax,%r15 // (f^g)&e rorq $4,%r13 - addq %rdx,%r12 - xorq %rcx,%r15 + addq %rdx,%r12 // T1+=h + xorq %rcx,%r15 // Ch(e,f,g)=((f^g)&e)^g rorq $6,%r14 xorq %rax,%r13 - addq %r15,%r12 + addq %r15,%r12 // T1+=Ch(e,f,g) movq %r8,%r15 - addq (%rbp),%r12 + addq (%rbp),%r12 // T1+=K[round] xorq %r8,%r14 - xorq %r9,%r15 - rorq $14,%r13 + xorq %r9,%r15 // a^b, b^c in next round + rorq $14,%r13 // Sigma1(e) movq %r9,%rdx andq %r15,%rdi - rorq $28,%r14 - addq %r13,%r12 + rorq $28,%r14 // Sigma0(a) + addq %r13,%r12 // T1+=Sigma1(e) - xorq %rdi,%rdx - addq %r12,%r11 - addq %r12,%rdx + xorq %rdi,%rdx // h=Maj(a,b,c)=Ch(a^b,c,b) + addq %r12,%r11 // d+=T1 + addq %r12,%rdx // h+=T1 - leaq 8(%rbp),%rbp - addq %r14,%rdx + leaq 8(%rbp),%rbp // round++ + addq %r14,%rdx // h+=Sigma0(a) movq 104(%rsi),%r12 movq %r11,%r13 movq %rdx,%r14 @@ -595,38 +595,38 @@ _CET_ENDBR xorq %r11,%r13 rorq $5,%r14 - xorq %rbx,%rdi + xorq %rbx,%rdi // f^g movq %r12,104(%rsp) xorq %rdx,%r14 - andq %r11,%rdi + andq %r11,%rdi // (f^g)&e rorq $4,%r13 - addq %rcx,%r12 - xorq %rbx,%rdi + addq %rcx,%r12 // T1+=h + xorq %rbx,%rdi // Ch(e,f,g)=((f^g)&e)^g rorq $6,%r14 xorq %r11,%r13 - addq %rdi,%r12 + addq %rdi,%r12 // T1+=Ch(e,f,g) movq %rdx,%rdi - addq (%rbp),%r12 + addq (%rbp),%r12 // T1+=K[round] xorq %rdx,%r14 - xorq %r8,%rdi - rorq $14,%r13 + xorq %r8,%rdi // a^b, b^c in next round + rorq $14,%r13 // Sigma1(e) movq %r8,%rcx andq %rdi,%r15 - rorq $28,%r14 - addq %r13,%r12 + rorq $28,%r14 // Sigma0(a) + addq %r13,%r12 // T1+=Sigma1(e) - xorq %r15,%rcx - addq %r12,%r10 - addq %r12,%rcx + xorq %r15,%rcx // h=Maj(a,b,c)=Ch(a^b,c,b) + addq %r12,%r10 // d+=T1 + addq %r12,%rcx // h+=T1 - leaq 24(%rbp),%rbp - addq %r14,%rcx + leaq 24(%rbp),%rbp // round++ + addq %r14,%rcx // h+=Sigma0(a) movq 112(%rsi),%r12 movq %r10,%r13 movq %rcx,%r14 @@ -636,38 +636,38 @@ _CET_ENDBR xorq %r10,%r13 rorq $5,%r14 - xorq %rax,%r15 + xorq %rax,%r15 // f^g movq %r12,112(%rsp) xorq %rcx,%r14 - andq %r10,%r15 + andq %r10,%r15 // (f^g)&e rorq $4,%r13 - addq %rbx,%r12 - xorq %rax,%r15 + addq %rbx,%r12 // T1+=h + xorq %rax,%r15 // Ch(e,f,g)=((f^g)&e)^g rorq $6,%r14 xorq %r10,%r13 - addq %r15,%r12 + addq %r15,%r12 // T1+=Ch(e,f,g) movq %rcx,%r15 - addq (%rbp),%r12 + addq (%rbp),%r12 // T1+=K[round] xorq %rcx,%r14 - xorq %rdx,%r15 - rorq $14,%r13 + xorq %rdx,%r15 // a^b, b^c in next round + rorq $14,%r13 // Sigma1(e) movq %rdx,%rbx andq %r15,%rdi - rorq $28,%r14 - addq %r13,%r12 + rorq $28,%r14 // Sigma0(a) + addq %r13,%r12 // T1+=Sigma1(e) - xorq %rdi,%rbx - addq %r12,%r9 - addq %r12,%rbx + xorq %rdi,%rbx // h=Maj(a,b,c)=Ch(a^b,c,b) + addq %r12,%r9 // d+=T1 + addq %r12,%rbx // h+=T1 - leaq 8(%rbp),%rbp - addq %r14,%rbx + leaq 8(%rbp),%rbp // round++ + addq %r14,%rbx // h+=Sigma0(a) movq 120(%rsi),%r12 movq %r9,%r13 movq %rbx,%r14 @@ -677,37 +677,37 @@ _CET_ENDBR xorq %r9,%r13 rorq $5,%r14 - xorq %r11,%rdi + xorq %r11,%rdi // f^g movq %r12,120(%rsp) xorq %rbx,%r14 - andq %r9,%rdi + andq %r9,%rdi // (f^g)&e rorq $4,%r13 - addq %rax,%r12 - xorq %r11,%rdi + addq %rax,%r12 // T1+=h + xorq %r11,%rdi // Ch(e,f,g)=((f^g)&e)^g rorq $6,%r14 xorq %r9,%r13 - addq %rdi,%r12 + addq %rdi,%r12 // T1+=Ch(e,f,g) movq %rbx,%rdi - addq (%rbp),%r12 + addq (%rbp),%r12 // T1+=K[round] xorq %rbx,%r14 - xorq %rcx,%rdi - rorq $14,%r13 + xorq %rcx,%rdi // a^b, b^c in next round + rorq $14,%r13 // Sigma1(e) movq %rcx,%rax andq %rdi,%r15 - rorq $28,%r14 - addq %r13,%r12 + rorq $28,%r14 // Sigma0(a) + addq %r13,%r12 // T1+=Sigma1(e) - xorq %r15,%rax - addq %r12,%r8 - addq %r12,%rax + xorq %r15,%rax // h=Maj(a,b,c)=Ch(a^b,c,b) + addq %r12,%r8 // d+=T1 + addq %r12,%rax // h+=T1 - leaq 24(%rbp),%rbp + leaq 24(%rbp),%rbp // round++ jmp .Lrounds_16_xx .align 16 .Lrounds_16_xx: @@ -716,7 +716,7 @@ _CET_ENDBR movq %r13,%r12 rorq $7,%r13 - addq %r14,%rax + addq %r14,%rax // modulo-scheduled h+=Sigma0(a) movq %r15,%r14 rorq $42,%r15 @@ -727,8 +727,8 @@ _CET_ENDBR shrq $6,%r14 rorq $19,%r15 - xorq %r13,%r12 - xorq %r14,%r15 + xorq %r13,%r12 // sigma0(X[(i+1)&0xf]) + xorq %r14,%r15 // sigma1(X[(i+14)&0xf]) addq 72(%rsp),%r12 addq 0(%rsp),%r12 @@ -740,43 +740,43 @@ _CET_ENDBR xorq %r8,%r13 rorq $5,%r14 - xorq %r10,%r15 + xorq %r10,%r15 // f^g movq %r12,0(%rsp) xorq %rax,%r14 - andq %r8,%r15 + andq %r8,%r15 // (f^g)&e rorq $4,%r13 - addq %r11,%r12 - xorq %r10,%r15 + addq %r11,%r12 // T1+=h + xorq %r10,%r15 // Ch(e,f,g)=((f^g)&e)^g rorq $6,%r14 xorq %r8,%r13 - addq %r15,%r12 + addq %r15,%r12 // T1+=Ch(e,f,g) movq %rax,%r15 - addq (%rbp),%r12 + addq (%rbp),%r12 // T1+=K[round] xorq %rax,%r14 - xorq %rbx,%r15 - rorq $14,%r13 + xorq %rbx,%r15 // a^b, b^c in next round + rorq $14,%r13 // Sigma1(e) movq %rbx,%r11 andq %r15,%rdi - rorq $28,%r14 - addq %r13,%r12 + rorq $28,%r14 // Sigma0(a) + addq %r13,%r12 // T1+=Sigma1(e) - xorq %rdi,%r11 - addq %r12,%rdx - addq %r12,%r11 + xorq %rdi,%r11 // h=Maj(a,b,c)=Ch(a^b,c,b) + addq %r12,%rdx // d+=T1 + addq %r12,%r11 // h+=T1 - leaq 8(%rbp),%rbp + leaq 8(%rbp),%rbp // round++ movq 16(%rsp),%r13 movq 120(%rsp),%rdi movq %r13,%r12 rorq $7,%r13 - addq %r14,%r11 + addq %r14,%r11 // modulo-scheduled h+=Sigma0(a) movq %rdi,%r14 rorq $42,%rdi @@ -787,8 +787,8 @@ _CET_ENDBR shrq $6,%r14 rorq $19,%rdi - xorq %r13,%r12 - xorq %r14,%rdi + xorq %r13,%r12 // sigma0(X[(i+1)&0xf]) + xorq %r14,%rdi // sigma1(X[(i+14)&0xf]) addq 80(%rsp),%r12 addq 8(%rsp),%r12 @@ -800,43 +800,43 @@ _CET_ENDBR xorq %rdx,%r13 rorq $5,%r14 - xorq %r9,%rdi + xorq %r9,%rdi // f^g movq %r12,8(%rsp) xorq %r11,%r14 - andq %rdx,%rdi + andq %rdx,%rdi // (f^g)&e rorq $4,%r13 - addq %r10,%r12 - xorq %r9,%rdi + addq %r10,%r12 // T1+=h + xorq %r9,%rdi // Ch(e,f,g)=((f^g)&e)^g rorq $6,%r14 xorq %rdx,%r13 - addq %rdi,%r12 + addq %rdi,%r12 // T1+=Ch(e,f,g) movq %r11,%rdi - addq (%rbp),%r12 + addq (%rbp),%r12 // T1+=K[round] xorq %r11,%r14 - xorq %rax,%rdi - rorq $14,%r13 + xorq %rax,%rdi // a^b, b^c in next round + rorq $14,%r13 // Sigma1(e) movq %rax,%r10 andq %rdi,%r15 - rorq $28,%r14 - addq %r13,%r12 + rorq $28,%r14 // Sigma0(a) + addq %r13,%r12 // T1+=Sigma1(e) - xorq %r15,%r10 - addq %r12,%rcx - addq %r12,%r10 + xorq %r15,%r10 // h=Maj(a,b,c)=Ch(a^b,c,b) + addq %r12,%rcx // d+=T1 + addq %r12,%r10 // h+=T1 - leaq 24(%rbp),%rbp + leaq 24(%rbp),%rbp // round++ movq 24(%rsp),%r13 movq 0(%rsp),%r15 movq %r13,%r12 rorq $7,%r13 - addq %r14,%r10 + addq %r14,%r10 // modulo-scheduled h+=Sigma0(a) movq %r15,%r14 rorq $42,%r15 @@ -847,8 +847,8 @@ _CET_ENDBR shrq $6,%r14 rorq $19,%r15 - xorq %r13,%r12 - xorq %r14,%r15 + xorq %r13,%r12 // sigma0(X[(i+1)&0xf]) + xorq %r14,%r15 // sigma1(X[(i+14)&0xf]) addq 88(%rsp),%r12 addq 16(%rsp),%r12 @@ -860,43 +860,43 @@ _CET_ENDBR xorq %rcx,%r13 rorq $5,%r14 - xorq %r8,%r15 + xorq %r8,%r15 // f^g movq %r12,16(%rsp) xorq %r10,%r14 - andq %rcx,%r15 + andq %rcx,%r15 // (f^g)&e rorq $4,%r13 - addq %r9,%r12 - xorq %r8,%r15 + addq %r9,%r12 // T1+=h + xorq %r8,%r15 // Ch(e,f,g)=((f^g)&e)^g rorq $6,%r14 xorq %rcx,%r13 - addq %r15,%r12 + addq %r15,%r12 // T1+=Ch(e,f,g) movq %r10,%r15 - addq (%rbp),%r12 + addq (%rbp),%r12 // T1+=K[round] xorq %r10,%r14 - xorq %r11,%r15 - rorq $14,%r13 + xorq %r11,%r15 // a^b, b^c in next round + rorq $14,%r13 // Sigma1(e) movq %r11,%r9 andq %r15,%rdi - rorq $28,%r14 - addq %r13,%r12 + rorq $28,%r14 // Sigma0(a) + addq %r13,%r12 // T1+=Sigma1(e) - xorq %rdi,%r9 - addq %r12,%rbx - addq %r12,%r9 + xorq %rdi,%r9 // h=Maj(a,b,c)=Ch(a^b,c,b) + addq %r12,%rbx // d+=T1 + addq %r12,%r9 // h+=T1 - leaq 8(%rbp),%rbp + leaq 8(%rbp),%rbp // round++ movq 32(%rsp),%r13 movq 8(%rsp),%rdi movq %r13,%r12 rorq $7,%r13 - addq %r14,%r9 + addq %r14,%r9 // modulo-scheduled h+=Sigma0(a) movq %rdi,%r14 rorq $42,%rdi @@ -907,8 +907,8 @@ _CET_ENDBR shrq $6,%r14 rorq $19,%rdi - xorq %r13,%r12 - xorq %r14,%rdi + xorq %r13,%r12 // sigma0(X[(i+1)&0xf]) + xorq %r14,%rdi // sigma1(X[(i+14)&0xf]) addq 96(%rsp),%r12 addq 24(%rsp),%r12 @@ -920,43 +920,43 @@ _CET_ENDBR xorq %rbx,%r13 rorq $5,%r14 - xorq %rdx,%rdi + xorq %rdx,%rdi // f^g movq %r12,24(%rsp) xorq %r9,%r14 - andq %rbx,%rdi + andq %rbx,%rdi // (f^g)&e rorq $4,%r13 - addq %r8,%r12 - xorq %rdx,%rdi + addq %r8,%r12 // T1+=h + xorq %rdx,%rdi // Ch(e,f,g)=((f^g)&e)^g rorq $6,%r14 xorq %rbx,%r13 - addq %rdi,%r12 + addq %rdi,%r12 // T1+=Ch(e,f,g) movq %r9,%rdi - addq (%rbp),%r12 + addq (%rbp),%r12 // T1+=K[round] xorq %r9,%r14 - xorq %r10,%rdi - rorq $14,%r13 + xorq %r10,%rdi // a^b, b^c in next round + rorq $14,%r13 // Sigma1(e) movq %r10,%r8 andq %rdi,%r15 - rorq $28,%r14 - addq %r13,%r12 + rorq $28,%r14 // Sigma0(a) + addq %r13,%r12 // T1+=Sigma1(e) - xorq %r15,%r8 - addq %r12,%rax - addq %r12,%r8 + xorq %r15,%r8 // h=Maj(a,b,c)=Ch(a^b,c,b) + addq %r12,%rax // d+=T1 + addq %r12,%r8 // h+=T1 - leaq 24(%rbp),%rbp + leaq 24(%rbp),%rbp // round++ movq 40(%rsp),%r13 movq 16(%rsp),%r15 movq %r13,%r12 rorq $7,%r13 - addq %r14,%r8 + addq %r14,%r8 // modulo-scheduled h+=Sigma0(a) movq %r15,%r14 rorq $42,%r15 @@ -967,8 +967,8 @@ _CET_ENDBR shrq $6,%r14 rorq $19,%r15 - xorq %r13,%r12 - xorq %r14,%r15 + xorq %r13,%r12 // sigma0(X[(i+1)&0xf]) + xorq %r14,%r15 // sigma1(X[(i+14)&0xf]) addq 104(%rsp),%r12 addq 32(%rsp),%r12 @@ -980,43 +980,43 @@ _CET_ENDBR xorq %rax,%r13 rorq $5,%r14 - xorq %rcx,%r15 + xorq %rcx,%r15 // f^g movq %r12,32(%rsp) xorq %r8,%r14 - andq %rax,%r15 + andq %rax,%r15 // (f^g)&e rorq $4,%r13 - addq %rdx,%r12 - xorq %rcx,%r15 + addq %rdx,%r12 // T1+=h + xorq %rcx,%r15 // Ch(e,f,g)=((f^g)&e)^g rorq $6,%r14 xorq %rax,%r13 - addq %r15,%r12 + addq %r15,%r12 // T1+=Ch(e,f,g) movq %r8,%r15 - addq (%rbp),%r12 + addq (%rbp),%r12 // T1+=K[round] xorq %r8,%r14 - xorq %r9,%r15 - rorq $14,%r13 + xorq %r9,%r15 // a^b, b^c in next round + rorq $14,%r13 // Sigma1(e) movq %r9,%rdx andq %r15,%rdi - rorq $28,%r14 - addq %r13,%r12 + rorq $28,%r14 // Sigma0(a) + addq %r13,%r12 // T1+=Sigma1(e) - xorq %rdi,%rdx - addq %r12,%r11 - addq %r12,%rdx + xorq %rdi,%rdx // h=Maj(a,b,c)=Ch(a^b,c,b) + addq %r12,%r11 // d+=T1 + addq %r12,%rdx // h+=T1 - leaq 8(%rbp),%rbp + leaq 8(%rbp),%rbp // round++ movq 48(%rsp),%r13 movq 24(%rsp),%rdi movq %r13,%r12 rorq $7,%r13 - addq %r14,%rdx + addq %r14,%rdx // modulo-scheduled h+=Sigma0(a) movq %rdi,%r14 rorq $42,%rdi @@ -1027,8 +1027,8 @@ _CET_ENDBR shrq $6,%r14 rorq $19,%rdi - xorq %r13,%r12 - xorq %r14,%rdi + xorq %r13,%r12 // sigma0(X[(i+1)&0xf]) + xorq %r14,%rdi // sigma1(X[(i+14)&0xf]) addq 112(%rsp),%r12 addq 40(%rsp),%r12 @@ -1040,43 +1040,43 @@ _CET_ENDBR xorq %r11,%r13 rorq $5,%r14 - xorq %rbx,%rdi + xorq %rbx,%rdi // f^g movq %r12,40(%rsp) xorq %rdx,%r14 - andq %r11,%rdi + andq %r11,%rdi // (f^g)&e rorq $4,%r13 - addq %rcx,%r12 - xorq %rbx,%rdi + addq %rcx,%r12 // T1+=h + xorq %rbx,%rdi // Ch(e,f,g)=((f^g)&e)^g rorq $6,%r14 xorq %r11,%r13 - addq %rdi,%r12 + addq %rdi,%r12 // T1+=Ch(e,f,g) movq %rdx,%rdi - addq (%rbp),%r12 + addq (%rbp),%r12 // T1+=K[round] xorq %rdx,%r14 - xorq %r8,%rdi - rorq $14,%r13 + xorq %r8,%rdi // a^b, b^c in next round + rorq $14,%r13 // Sigma1(e) movq %r8,%rcx andq %rdi,%r15 - rorq $28,%r14 - addq %r13,%r12 + rorq $28,%r14 // Sigma0(a) + addq %r13,%r12 // T1+=Sigma1(e) - xorq %r15,%rcx - addq %r12,%r10 - addq %r12,%rcx + xorq %r15,%rcx // h=Maj(a,b,c)=Ch(a^b,c,b) + addq %r12,%r10 // d+=T1 + addq %r12,%rcx // h+=T1 - leaq 24(%rbp),%rbp + leaq 24(%rbp),%rbp // round++ movq 56(%rsp),%r13 movq 32(%rsp),%r15 movq %r13,%r12 rorq $7,%r13 - addq %r14,%rcx + addq %r14,%rcx // modulo-scheduled h+=Sigma0(a) movq %r15,%r14 rorq $42,%r15 @@ -1087,8 +1087,8 @@ _CET_ENDBR shrq $6,%r14 rorq $19,%r15 - xorq %r13,%r12 - xorq %r14,%r15 + xorq %r13,%r12 // sigma0(X[(i+1)&0xf]) + xorq %r14,%r15 // sigma1(X[(i+14)&0xf]) addq 120(%rsp),%r12 addq 48(%rsp),%r12 @@ -1100,43 +1100,43 @@ _CET_ENDBR xorq %r10,%r13 rorq $5,%r14 - xorq %rax,%r15 + xorq %rax,%r15 // f^g movq %r12,48(%rsp) xorq %rcx,%r14 - andq %r10,%r15 + andq %r10,%r15 // (f^g)&e rorq $4,%r13 - addq %rbx,%r12 - xorq %rax,%r15 + addq %rbx,%r12 // T1+=h + xorq %rax,%r15 // Ch(e,f,g)=((f^g)&e)^g rorq $6,%r14 xorq %r10,%r13 - addq %r15,%r12 + addq %r15,%r12 // T1+=Ch(e,f,g) movq %rcx,%r15 - addq (%rbp),%r12 + addq (%rbp),%r12 // T1+=K[round] xorq %rcx,%r14 - xorq %rdx,%r15 - rorq $14,%r13 + xorq %rdx,%r15 // a^b, b^c in next round + rorq $14,%r13 // Sigma1(e) movq %rdx,%rbx andq %r15,%rdi - rorq $28,%r14 - addq %r13,%r12 + rorq $28,%r14 // Sigma0(a) + addq %r13,%r12 // T1+=Sigma1(e) - xorq %rdi,%rbx - addq %r12,%r9 - addq %r12,%rbx + xorq %rdi,%rbx // h=Maj(a,b,c)=Ch(a^b,c,b) + addq %r12,%r9 // d+=T1 + addq %r12,%rbx // h+=T1 - leaq 8(%rbp),%rbp + leaq 8(%rbp),%rbp // round++ movq 64(%rsp),%r13 movq 40(%rsp),%rdi movq %r13,%r12 rorq $7,%r13 - addq %r14,%rbx + addq %r14,%rbx // modulo-scheduled h+=Sigma0(a) movq %rdi,%r14 rorq $42,%rdi @@ -1147,8 +1147,8 @@ _CET_ENDBR shrq $6,%r14 rorq $19,%rdi - xorq %r13,%r12 - xorq %r14,%rdi + xorq %r13,%r12 // sigma0(X[(i+1)&0xf]) + xorq %r14,%rdi // sigma1(X[(i+14)&0xf]) addq 0(%rsp),%r12 addq 56(%rsp),%r12 @@ -1160,43 +1160,43 @@ _CET_ENDBR xorq %r9,%r13 rorq $5,%r14 - xorq %r11,%rdi + xorq %r11,%rdi // f^g movq %r12,56(%rsp) xorq %rbx,%r14 - andq %r9,%rdi + andq %r9,%rdi // (f^g)&e rorq $4,%r13 - addq %rax,%r12 - xorq %r11,%rdi + addq %rax,%r12 // T1+=h + xorq %r11,%rdi // Ch(e,f,g)=((f^g)&e)^g rorq $6,%r14 xorq %r9,%r13 - addq %rdi,%r12 + addq %rdi,%r12 // T1+=Ch(e,f,g) movq %rbx,%rdi - addq (%rbp),%r12 + addq (%rbp),%r12 // T1+=K[round] xorq %rbx,%r14 - xorq %rcx,%rdi - rorq $14,%r13 + xorq %rcx,%rdi // a^b, b^c in next round + rorq $14,%r13 // Sigma1(e) movq %rcx,%rax andq %rdi,%r15 - rorq $28,%r14 - addq %r13,%r12 + rorq $28,%r14 // Sigma0(a) + addq %r13,%r12 // T1+=Sigma1(e) - xorq %r15,%rax - addq %r12,%r8 - addq %r12,%rax + xorq %r15,%rax // h=Maj(a,b,c)=Ch(a^b,c,b) + addq %r12,%r8 // d+=T1 + addq %r12,%rax // h+=T1 - leaq 24(%rbp),%rbp + leaq 24(%rbp),%rbp // round++ movq 72(%rsp),%r13 movq 48(%rsp),%r15 movq %r13,%r12 rorq $7,%r13 - addq %r14,%rax + addq %r14,%rax // modulo-scheduled h+=Sigma0(a) movq %r15,%r14 rorq $42,%r15 @@ -1207,8 +1207,8 @@ _CET_ENDBR shrq $6,%r14 rorq $19,%r15 - xorq %r13,%r12 - xorq %r14,%r15 + xorq %r13,%r12 // sigma0(X[(i+1)&0xf]) + xorq %r14,%r15 // sigma1(X[(i+14)&0xf]) addq 8(%rsp),%r12 addq 64(%rsp),%r12 @@ -1220,43 +1220,43 @@ _CET_ENDBR xorq %r8,%r13 rorq $5,%r14 - xorq %r10,%r15 + xorq %r10,%r15 // f^g movq %r12,64(%rsp) xorq %rax,%r14 - andq %r8,%r15 + andq %r8,%r15 // (f^g)&e rorq $4,%r13 - addq %r11,%r12 - xorq %r10,%r15 + addq %r11,%r12 // T1+=h + xorq %r10,%r15 // Ch(e,f,g)=((f^g)&e)^g rorq $6,%r14 xorq %r8,%r13 - addq %r15,%r12 + addq %r15,%r12 // T1+=Ch(e,f,g) movq %rax,%r15 - addq (%rbp),%r12 + addq (%rbp),%r12 // T1+=K[round] xorq %rax,%r14 - xorq %rbx,%r15 - rorq $14,%r13 + xorq %rbx,%r15 // a^b, b^c in next round + rorq $14,%r13 // Sigma1(e) movq %rbx,%r11 andq %r15,%rdi - rorq $28,%r14 - addq %r13,%r12 + rorq $28,%r14 // Sigma0(a) + addq %r13,%r12 // T1+=Sigma1(e) - xorq %rdi,%r11 - addq %r12,%rdx - addq %r12,%r11 + xorq %rdi,%r11 // h=Maj(a,b,c)=Ch(a^b,c,b) + addq %r12,%rdx // d+=T1 + addq %r12,%r11 // h+=T1 - leaq 8(%rbp),%rbp + leaq 8(%rbp),%rbp // round++ movq 80(%rsp),%r13 movq 56(%rsp),%rdi movq %r13,%r12 rorq $7,%r13 - addq %r14,%r11 + addq %r14,%r11 // modulo-scheduled h+=Sigma0(a) movq %rdi,%r14 rorq $42,%rdi @@ -1267,8 +1267,8 @@ _CET_ENDBR shrq $6,%r14 rorq $19,%rdi - xorq %r13,%r12 - xorq %r14,%rdi + xorq %r13,%r12 // sigma0(X[(i+1)&0xf]) + xorq %r14,%rdi // sigma1(X[(i+14)&0xf]) addq 16(%rsp),%r12 addq 72(%rsp),%r12 @@ -1280,43 +1280,43 @@ _CET_ENDBR xorq %rdx,%r13 rorq $5,%r14 - xorq %r9,%rdi + xorq %r9,%rdi // f^g movq %r12,72(%rsp) xorq %r11,%r14 - andq %rdx,%rdi + andq %rdx,%rdi // (f^g)&e rorq $4,%r13 - addq %r10,%r12 - xorq %r9,%rdi + addq %r10,%r12 // T1+=h + xorq %r9,%rdi // Ch(e,f,g)=((f^g)&e)^g rorq $6,%r14 xorq %rdx,%r13 - addq %rdi,%r12 + addq %rdi,%r12 // T1+=Ch(e,f,g) movq %r11,%rdi - addq (%rbp),%r12 + addq (%rbp),%r12 // T1+=K[round] xorq %r11,%r14 - xorq %rax,%rdi - rorq $14,%r13 + xorq %rax,%rdi // a^b, b^c in next round + rorq $14,%r13 // Sigma1(e) movq %rax,%r10 andq %rdi,%r15 - rorq $28,%r14 - addq %r13,%r12 + rorq $28,%r14 // Sigma0(a) + addq %r13,%r12 // T1+=Sigma1(e) - xorq %r15,%r10 - addq %r12,%rcx - addq %r12,%r10 + xorq %r15,%r10 // h=Maj(a,b,c)=Ch(a^b,c,b) + addq %r12,%rcx // d+=T1 + addq %r12,%r10 // h+=T1 - leaq 24(%rbp),%rbp + leaq 24(%rbp),%rbp // round++ movq 88(%rsp),%r13 movq 64(%rsp),%r15 movq %r13,%r12 rorq $7,%r13 - addq %r14,%r10 + addq %r14,%r10 // modulo-scheduled h+=Sigma0(a) movq %r15,%r14 rorq $42,%r15 @@ -1327,8 +1327,8 @@ _CET_ENDBR shrq $6,%r14 rorq $19,%r15 - xorq %r13,%r12 - xorq %r14,%r15 + xorq %r13,%r12 // sigma0(X[(i+1)&0xf]) + xorq %r14,%r15 // sigma1(X[(i+14)&0xf]) addq 24(%rsp),%r12 addq 80(%rsp),%r12 @@ -1340,43 +1340,43 @@ _CET_ENDBR xorq %rcx,%r13 rorq $5,%r14 - xorq %r8,%r15 + xorq %r8,%r15 // f^g movq %r12,80(%rsp) xorq %r10,%r14 - andq %rcx,%r15 + andq %rcx,%r15 // (f^g)&e rorq $4,%r13 - addq %r9,%r12 - xorq %r8,%r15 + addq %r9,%r12 // T1+=h + xorq %r8,%r15 // Ch(e,f,g)=((f^g)&e)^g rorq $6,%r14 xorq %rcx,%r13 - addq %r15,%r12 + addq %r15,%r12 // T1+=Ch(e,f,g) movq %r10,%r15 - addq (%rbp),%r12 + addq (%rbp),%r12 // T1+=K[round] xorq %r10,%r14 - xorq %r11,%r15 - rorq $14,%r13 + xorq %r11,%r15 // a^b, b^c in next round + rorq $14,%r13 // Sigma1(e) movq %r11,%r9 andq %r15,%rdi - rorq $28,%r14 - addq %r13,%r12 + rorq $28,%r14 // Sigma0(a) + addq %r13,%r12 // T1+=Sigma1(e) - xorq %rdi,%r9 - addq %r12,%rbx - addq %r12,%r9 + xorq %rdi,%r9 // h=Maj(a,b,c)=Ch(a^b,c,b) + addq %r12,%rbx // d+=T1 + addq %r12,%r9 // h+=T1 - leaq 8(%rbp),%rbp + leaq 8(%rbp),%rbp // round++ movq 96(%rsp),%r13 movq 72(%rsp),%rdi movq %r13,%r12 rorq $7,%r13 - addq %r14,%r9 + addq %r14,%r9 // modulo-scheduled h+=Sigma0(a) movq %rdi,%r14 rorq $42,%rdi @@ -1387,8 +1387,8 @@ _CET_ENDBR shrq $6,%r14 rorq $19,%rdi - xorq %r13,%r12 - xorq %r14,%rdi + xorq %r13,%r12 // sigma0(X[(i+1)&0xf]) + xorq %r14,%rdi // sigma1(X[(i+14)&0xf]) addq 32(%rsp),%r12 addq 88(%rsp),%r12 @@ -1400,43 +1400,43 @@ _CET_ENDBR xorq %rbx,%r13 rorq $5,%r14 - xorq %rdx,%rdi + xorq %rdx,%rdi // f^g movq %r12,88(%rsp) xorq %r9,%r14 - andq %rbx,%rdi + andq %rbx,%rdi // (f^g)&e rorq $4,%r13 - addq %r8,%r12 - xorq %rdx,%rdi + addq %r8,%r12 // T1+=h + xorq %rdx,%rdi // Ch(e,f,g)=((f^g)&e)^g rorq $6,%r14 xorq %rbx,%r13 - addq %rdi,%r12 + addq %rdi,%r12 // T1+=Ch(e,f,g) movq %r9,%rdi - addq (%rbp),%r12 + addq (%rbp),%r12 // T1+=K[round] xorq %r9,%r14 - xorq %r10,%rdi - rorq $14,%r13 + xorq %r10,%rdi // a^b, b^c in next round + rorq $14,%r13 // Sigma1(e) movq %r10,%r8 andq %rdi,%r15 - rorq $28,%r14 - addq %r13,%r12 + rorq $28,%r14 // Sigma0(a) + addq %r13,%r12 // T1+=Sigma1(e) - xorq %r15,%r8 - addq %r12,%rax - addq %r12,%r8 + xorq %r15,%r8 // h=Maj(a,b,c)=Ch(a^b,c,b) + addq %r12,%rax // d+=T1 + addq %r12,%r8 // h+=T1 - leaq 24(%rbp),%rbp + leaq 24(%rbp),%rbp // round++ movq 104(%rsp),%r13 movq 80(%rsp),%r15 movq %r13,%r12 rorq $7,%r13 - addq %r14,%r8 + addq %r14,%r8 // modulo-scheduled h+=Sigma0(a) movq %r15,%r14 rorq $42,%r15 @@ -1447,8 +1447,8 @@ _CET_ENDBR shrq $6,%r14 rorq $19,%r15 - xorq %r13,%r12 - xorq %r14,%r15 + xorq %r13,%r12 // sigma0(X[(i+1)&0xf]) + xorq %r14,%r15 // sigma1(X[(i+14)&0xf]) addq 40(%rsp),%r12 addq 96(%rsp),%r12 @@ -1460,43 +1460,43 @@ _CET_ENDBR xorq %rax,%r13 rorq $5,%r14 - xorq %rcx,%r15 + xorq %rcx,%r15 // f^g movq %r12,96(%rsp) xorq %r8,%r14 - andq %rax,%r15 + andq %rax,%r15 // (f^g)&e rorq $4,%r13 - addq %rdx,%r12 - xorq %rcx,%r15 + addq %rdx,%r12 // T1+=h + xorq %rcx,%r15 // Ch(e,f,g)=((f^g)&e)^g rorq $6,%r14 xorq %rax,%r13 - addq %r15,%r12 + addq %r15,%r12 // T1+=Ch(e,f,g) movq %r8,%r15 - addq (%rbp),%r12 + addq (%rbp),%r12 // T1+=K[round] xorq %r8,%r14 - xorq %r9,%r15 - rorq $14,%r13 + xorq %r9,%r15 // a^b, b^c in next round + rorq $14,%r13 // Sigma1(e) movq %r9,%rdx andq %r15,%rdi - rorq $28,%r14 - addq %r13,%r12 + rorq $28,%r14 // Sigma0(a) + addq %r13,%r12 // T1+=Sigma1(e) - xorq %rdi,%rdx - addq %r12,%r11 - addq %r12,%rdx + xorq %rdi,%rdx // h=Maj(a,b,c)=Ch(a^b,c,b) + addq %r12,%r11 // d+=T1 + addq %r12,%rdx // h+=T1 - leaq 8(%rbp),%rbp + leaq 8(%rbp),%rbp // round++ movq 112(%rsp),%r13 movq 88(%rsp),%rdi movq %r13,%r12 rorq $7,%r13 - addq %r14,%rdx + addq %r14,%rdx // modulo-scheduled h+=Sigma0(a) movq %rdi,%r14 rorq $42,%rdi @@ -1507,8 +1507,8 @@ _CET_ENDBR shrq $6,%r14 rorq $19,%rdi - xorq %r13,%r12 - xorq %r14,%rdi + xorq %r13,%r12 // sigma0(X[(i+1)&0xf]) + xorq %r14,%rdi // sigma1(X[(i+14)&0xf]) addq 48(%rsp),%r12 addq 104(%rsp),%r12 @@ -1520,43 +1520,43 @@ _CET_ENDBR xorq %r11,%r13 rorq $5,%r14 - xorq %rbx,%rdi + xorq %rbx,%rdi // f^g movq %r12,104(%rsp) xorq %rdx,%r14 - andq %r11,%rdi + andq %r11,%rdi // (f^g)&e rorq $4,%r13 - addq %rcx,%r12 - xorq %rbx,%rdi + addq %rcx,%r12 // T1+=h + xorq %rbx,%rdi // Ch(e,f,g)=((f^g)&e)^g rorq $6,%r14 xorq %r11,%r13 - addq %rdi,%r12 + addq %rdi,%r12 // T1+=Ch(e,f,g) movq %rdx,%rdi - addq (%rbp),%r12 + addq (%rbp),%r12 // T1+=K[round] xorq %rdx,%r14 - xorq %r8,%rdi - rorq $14,%r13 + xorq %r8,%rdi // a^b, b^c in next round + rorq $14,%r13 // Sigma1(e) movq %r8,%rcx andq %rdi,%r15 - rorq $28,%r14 - addq %r13,%r12 + rorq $28,%r14 // Sigma0(a) + addq %r13,%r12 // T1+=Sigma1(e) - xorq %r15,%rcx - addq %r12,%r10 - addq %r12,%rcx + xorq %r15,%rcx // h=Maj(a,b,c)=Ch(a^b,c,b) + addq %r12,%r10 // d+=T1 + addq %r12,%rcx // h+=T1 - leaq 24(%rbp),%rbp + leaq 24(%rbp),%rbp // round++ movq 120(%rsp),%r13 movq 96(%rsp),%r15 movq %r13,%r12 rorq $7,%r13 - addq %r14,%rcx + addq %r14,%rcx // modulo-scheduled h+=Sigma0(a) movq %r15,%r14 rorq $42,%r15 @@ -1567,8 +1567,8 @@ _CET_ENDBR shrq $6,%r14 rorq $19,%r15 - xorq %r13,%r12 - xorq %r14,%r15 + xorq %r13,%r12 // sigma0(X[(i+1)&0xf]) + xorq %r14,%r15 // sigma1(X[(i+14)&0xf]) addq 56(%rsp),%r12 addq 112(%rsp),%r12 @@ -1580,43 +1580,43 @@ _CET_ENDBR xorq %r10,%r13 rorq $5,%r14 - xorq %rax,%r15 + xorq %rax,%r15 // f^g movq %r12,112(%rsp) xorq %rcx,%r14 - andq %r10,%r15 + andq %r10,%r15 // (f^g)&e rorq $4,%r13 - addq %rbx,%r12 - xorq %rax,%r15 + addq %rbx,%r12 // T1+=h + xorq %rax,%r15 // Ch(e,f,g)=((f^g)&e)^g rorq $6,%r14 xorq %r10,%r13 - addq %r15,%r12 + addq %r15,%r12 // T1+=Ch(e,f,g) movq %rcx,%r15 - addq (%rbp),%r12 + addq (%rbp),%r12 // T1+=K[round] xorq %rcx,%r14 - xorq %rdx,%r15 - rorq $14,%r13 + xorq %rdx,%r15 // a^b, b^c in next round + rorq $14,%r13 // Sigma1(e) movq %rdx,%rbx andq %r15,%rdi - rorq $28,%r14 - addq %r13,%r12 + rorq $28,%r14 // Sigma0(a) + addq %r13,%r12 // T1+=Sigma1(e) - xorq %rdi,%rbx - addq %r12,%r9 - addq %r12,%rbx + xorq %rdi,%rbx // h=Maj(a,b,c)=Ch(a^b,c,b) + addq %r12,%r9 // d+=T1 + addq %r12,%rbx // h+=T1 - leaq 8(%rbp),%rbp + leaq 8(%rbp),%rbp // round++ movq 0(%rsp),%r13 movq 104(%rsp),%rdi movq %r13,%r12 rorq $7,%r13 - addq %r14,%rbx + addq %r14,%rbx // modulo-scheduled h+=Sigma0(a) movq %rdi,%r14 rorq $42,%rdi @@ -1627,8 +1627,8 @@ _CET_ENDBR shrq $6,%r14 rorq $19,%rdi - xorq %r13,%r12 - xorq %r14,%rdi + xorq %r13,%r12 // sigma0(X[(i+1)&0xf]) + xorq %r14,%rdi // sigma1(X[(i+14)&0xf]) addq 64(%rsp),%r12 addq 120(%rsp),%r12 @@ -1640,42 +1640,42 @@ _CET_ENDBR xorq %r9,%r13 rorq $5,%r14 - xorq %r11,%rdi + xorq %r11,%rdi // f^g movq %r12,120(%rsp) xorq %rbx,%r14 - andq %r9,%rdi + andq %r9,%rdi // (f^g)&e rorq $4,%r13 - addq %rax,%r12 - xorq %r11,%rdi + addq %rax,%r12 // T1+=h + xorq %r11,%rdi // Ch(e,f,g)=((f^g)&e)^g rorq $6,%r14 xorq %r9,%r13 - addq %rdi,%r12 + addq %rdi,%r12 // T1+=Ch(e,f,g) movq %rbx,%rdi - addq (%rbp),%r12 + addq (%rbp),%r12 // T1+=K[round] xorq %rbx,%r14 - xorq %rcx,%rdi - rorq $14,%r13 + xorq %rcx,%rdi // a^b, b^c in next round + rorq $14,%r13 // Sigma1(e) movq %rcx,%rax andq %rdi,%r15 - rorq $28,%r14 - addq %r13,%r12 + rorq $28,%r14 // Sigma0(a) + addq %r13,%r12 // T1+=Sigma1(e) - xorq %r15,%rax - addq %r12,%r8 - addq %r12,%rax + xorq %r15,%rax // h=Maj(a,b,c)=Ch(a^b,c,b) + addq %r12,%r8 // d+=T1 + addq %r12,%rax // h+=T1 - leaq 24(%rbp),%rbp + leaq 24(%rbp),%rbp // round++ cmpb $0,7(%rbp) jnz .Lrounds_16_xx movq 128+0(%rsp),%rdi - addq %r14,%rax + addq %r14,%rax // modulo-scheduled h+=Sigma0(a) leaq 128(%rsi),%rsi addq 0(%rdi),%rax @@ -1815,7 +1815,7 @@ K512: sha512_block_data_order_avx: .cfi_startproc _CET_ENDBR - movq %rsp,%rax + movq %rsp,%rax // copy %rsp .cfi_def_cfa_register %rax pushq %rbx .cfi_offset %rbx,-16 @@ -1829,14 +1829,14 @@ _CET_ENDBR .cfi_offset %r14,-48 pushq %r15 .cfi_offset %r15,-56 - shlq $4,%rdx + shlq $4,%rdx // num*16 subq $160,%rsp - leaq (%rsi,%rdx,8),%rdx - andq $-64,%rsp - movq %rdi,128+0(%rsp) - movq %rsi,128+8(%rsp) - movq %rdx,128+16(%rsp) - movq %rax,152(%rsp) + leaq (%rsi,%rdx,8),%rdx // inp+num*16*8 + andq $-64,%rsp // align stack frame + movq %rdi,128+0(%rsp) // save ctx, 1st arg + movq %rsi,128+8(%rsp) // save inp, 2nd arh + movq %rdx,128+16(%rsp) // save end pointer, "3rd" arg + movq %rax,152(%rsp) // save copy of %rsp .cfi_escape 0x0f,0x06,0x77,0x98,0x01,0x06,0x23,0x08 .Lprologue_avx: @@ -1854,7 +1854,7 @@ _CET_ENDBR .Lloop_avx: vmovdqa K512+1280(%rip),%xmm11 vmovdqu 0(%rsi),%xmm0 - leaq K512+128(%rip),%rbp + leaq K512+128(%rip),%rbp // size optimization vmovdqu 16(%rsi),%xmm1 vmovdqu 32(%rsi),%xmm2 vpshufb %xmm11,%xmm0,%xmm0 @@ -1887,7 +1887,7 @@ _CET_ENDBR vmovdqa %xmm9,80(%rsp) movq %rbx,%rdi vmovdqa %xmm10,96(%rsp) - xorq %rcx,%rdi + xorq %rcx,%rdi // magic vmovdqa %xmm11,112(%rsp) movq %r8,%r13 jmp .Lavx_00_47 diff --git a/third_party/boringssl/gen/bcm/sha512-x86_64-win.asm b/third_party/boringssl/gen/bcm/sha512-x86_64-win.asm index 53cabe62c..9f79afb2a 100644 --- a/third_party/boringssl/gen/bcm/sha512-x86_64-win.asm +++ b/third_party/boringssl/gen/bcm/sha512-x86_64-win.asm @@ -29,7 +29,7 @@ $L$SEH_begin_sha512_block_data_order_nohw: _CET_ENDBR - mov rax,rsp + mov rax,rsp ; copy %rsp push rbx @@ -43,14 +43,14 @@ _CET_ENDBR push r15 - shl rdx,4 + shl rdx,4 ; num*16 sub rsp,16*8+4*8 - lea rdx,[rdx*8+rsi] - and rsp,-64 - mov QWORD[((128+0))+rsp],rdi - mov QWORD[((128+8))+rsp],rsi - mov QWORD[((128+16))+rsp],rdx - mov QWORD[152+rsp],rax + lea rdx,[rdx*8+rsi] ; inp+num*16*8 + and rsp,-64 ; align stack frame + mov QWORD[((128+0))+rsp],rdi ; save ctx, 1st arg + mov QWORD[((128+8))+rsp],rsi ; save inp, 2nd arh + mov QWORD[((128+16))+rsp],rdx ; save end pointer, "3rd" arg + mov QWORD[152+rsp],rax ; save copy of %rsp $L$prologue: @@ -68,7 +68,7 @@ ALIGN 16 $L$loop: mov rdi,rbx lea rbp,[K512] - xor rdi,rcx + xor rdi,rcx ; magic mov r12,QWORD[rsi] mov r13,r8 mov r14,rax @@ -78,38 +78,38 @@ $L$loop: xor r13,r8 ror r14,5 - xor r15,r10 + xor r15,r10 ; f^g mov QWORD[rsp],r12 xor r14,rax - and r15,r8 + and r15,r8 ; (f^g)&e ror r13,4 - add r12,r11 - xor r15,r10 + add r12,r11 ; T1+=h + xor r15,r10 ; Ch(e,f,g)=((f^g)&e)^g ror r14,6 xor r13,r8 - add r12,r15 + add r12,r15 ; T1+=Ch(e,f,g) mov r15,rax - add r12,QWORD[rbp] + add r12,QWORD[rbp] ; T1+=K[round] xor r14,rax - xor r15,rbx - ror r13,14 + xor r15,rbx ; a^b, b^c in next round + ror r13,14 ; Sigma1(e) mov r11,rbx and rdi,r15 - ror r14,28 - add r12,r13 + ror r14,28 ; Sigma0(a) + add r12,r13 ; T1+=Sigma1(e) - xor r11,rdi - add rdx,r12 - add r11,r12 + xor r11,rdi ; h=Maj(a,b,c)=Ch(a^b,c,b) + add rdx,r12 ; d+=T1 + add r11,r12 ; h+=T1 - lea rbp,[8+rbp] - add r11,r14 + lea rbp,[8+rbp] ; round++ + add r11,r14 ; h+=Sigma0(a) mov r12,QWORD[8+rsi] mov r13,rdx mov r14,r11 @@ -119,38 +119,38 @@ $L$loop: xor r13,rdx ror r14,5 - xor rdi,r9 + xor rdi,r9 ; f^g mov QWORD[8+rsp],r12 xor r14,r11 - and rdi,rdx + and rdi,rdx ; (f^g)&e ror r13,4 - add r12,r10 - xor rdi,r9 + add r12,r10 ; T1+=h + xor rdi,r9 ; Ch(e,f,g)=((f^g)&e)^g ror r14,6 xor r13,rdx - add r12,rdi + add r12,rdi ; T1+=Ch(e,f,g) mov rdi,r11 - add r12,QWORD[rbp] + add r12,QWORD[rbp] ; T1+=K[round] xor r14,r11 - xor rdi,rax - ror r13,14 + xor rdi,rax ; a^b, b^c in next round + ror r13,14 ; Sigma1(e) mov r10,rax and r15,rdi - ror r14,28 - add r12,r13 + ror r14,28 ; Sigma0(a) + add r12,r13 ; T1+=Sigma1(e) - xor r10,r15 - add rcx,r12 - add r10,r12 + xor r10,r15 ; h=Maj(a,b,c)=Ch(a^b,c,b) + add rcx,r12 ; d+=T1 + add r10,r12 ; h+=T1 - lea rbp,[24+rbp] - add r10,r14 + lea rbp,[24+rbp] ; round++ + add r10,r14 ; h+=Sigma0(a) mov r12,QWORD[16+rsi] mov r13,rcx mov r14,r10 @@ -160,38 +160,38 @@ $L$loop: xor r13,rcx ror r14,5 - xor r15,r8 + xor r15,r8 ; f^g mov QWORD[16+rsp],r12 xor r14,r10 - and r15,rcx + and r15,rcx ; (f^g)&e ror r13,4 - add r12,r9 - xor r15,r8 + add r12,r9 ; T1+=h + xor r15,r8 ; Ch(e,f,g)=((f^g)&e)^g ror r14,6 xor r13,rcx - add r12,r15 + add r12,r15 ; T1+=Ch(e,f,g) mov r15,r10 - add r12,QWORD[rbp] + add r12,QWORD[rbp] ; T1+=K[round] xor r14,r10 - xor r15,r11 - ror r13,14 + xor r15,r11 ; a^b, b^c in next round + ror r13,14 ; Sigma1(e) mov r9,r11 and rdi,r15 - ror r14,28 - add r12,r13 + ror r14,28 ; Sigma0(a) + add r12,r13 ; T1+=Sigma1(e) - xor r9,rdi - add rbx,r12 - add r9,r12 + xor r9,rdi ; h=Maj(a,b,c)=Ch(a^b,c,b) + add rbx,r12 ; d+=T1 + add r9,r12 ; h+=T1 - lea rbp,[8+rbp] - add r9,r14 + lea rbp,[8+rbp] ; round++ + add r9,r14 ; h+=Sigma0(a) mov r12,QWORD[24+rsi] mov r13,rbx mov r14,r9 @@ -201,38 +201,38 @@ $L$loop: xor r13,rbx ror r14,5 - xor rdi,rdx + xor rdi,rdx ; f^g mov QWORD[24+rsp],r12 xor r14,r9 - and rdi,rbx + and rdi,rbx ; (f^g)&e ror r13,4 - add r12,r8 - xor rdi,rdx + add r12,r8 ; T1+=h + xor rdi,rdx ; Ch(e,f,g)=((f^g)&e)^g ror r14,6 xor r13,rbx - add r12,rdi + add r12,rdi ; T1+=Ch(e,f,g) mov rdi,r9 - add r12,QWORD[rbp] + add r12,QWORD[rbp] ; T1+=K[round] xor r14,r9 - xor rdi,r10 - ror r13,14 + xor rdi,r10 ; a^b, b^c in next round + ror r13,14 ; Sigma1(e) mov r8,r10 and r15,rdi - ror r14,28 - add r12,r13 + ror r14,28 ; Sigma0(a) + add r12,r13 ; T1+=Sigma1(e) - xor r8,r15 - add rax,r12 - add r8,r12 + xor r8,r15 ; h=Maj(a,b,c)=Ch(a^b,c,b) + add rax,r12 ; d+=T1 + add r8,r12 ; h+=T1 - lea rbp,[24+rbp] - add r8,r14 + lea rbp,[24+rbp] ; round++ + add r8,r14 ; h+=Sigma0(a) mov r12,QWORD[32+rsi] mov r13,rax mov r14,r8 @@ -242,38 +242,38 @@ $L$loop: xor r13,rax ror r14,5 - xor r15,rcx + xor r15,rcx ; f^g mov QWORD[32+rsp],r12 xor r14,r8 - and r15,rax + and r15,rax ; (f^g)&e ror r13,4 - add r12,rdx - xor r15,rcx + add r12,rdx ; T1+=h + xor r15,rcx ; Ch(e,f,g)=((f^g)&e)^g ror r14,6 xor r13,rax - add r12,r15 + add r12,r15 ; T1+=Ch(e,f,g) mov r15,r8 - add r12,QWORD[rbp] + add r12,QWORD[rbp] ; T1+=K[round] xor r14,r8 - xor r15,r9 - ror r13,14 + xor r15,r9 ; a^b, b^c in next round + ror r13,14 ; Sigma1(e) mov rdx,r9 and rdi,r15 - ror r14,28 - add r12,r13 + ror r14,28 ; Sigma0(a) + add r12,r13 ; T1+=Sigma1(e) - xor rdx,rdi - add r11,r12 - add rdx,r12 + xor rdx,rdi ; h=Maj(a,b,c)=Ch(a^b,c,b) + add r11,r12 ; d+=T1 + add rdx,r12 ; h+=T1 - lea rbp,[8+rbp] - add rdx,r14 + lea rbp,[8+rbp] ; round++ + add rdx,r14 ; h+=Sigma0(a) mov r12,QWORD[40+rsi] mov r13,r11 mov r14,rdx @@ -283,38 +283,38 @@ $L$loop: xor r13,r11 ror r14,5 - xor rdi,rbx + xor rdi,rbx ; f^g mov QWORD[40+rsp],r12 xor r14,rdx - and rdi,r11 + and rdi,r11 ; (f^g)&e ror r13,4 - add r12,rcx - xor rdi,rbx + add r12,rcx ; T1+=h + xor rdi,rbx ; Ch(e,f,g)=((f^g)&e)^g ror r14,6 xor r13,r11 - add r12,rdi + add r12,rdi ; T1+=Ch(e,f,g) mov rdi,rdx - add r12,QWORD[rbp] + add r12,QWORD[rbp] ; T1+=K[round] xor r14,rdx - xor rdi,r8 - ror r13,14 + xor rdi,r8 ; a^b, b^c in next round + ror r13,14 ; Sigma1(e) mov rcx,r8 and r15,rdi - ror r14,28 - add r12,r13 + ror r14,28 ; Sigma0(a) + add r12,r13 ; T1+=Sigma1(e) - xor rcx,r15 - add r10,r12 - add rcx,r12 + xor rcx,r15 ; h=Maj(a,b,c)=Ch(a^b,c,b) + add r10,r12 ; d+=T1 + add rcx,r12 ; h+=T1 - lea rbp,[24+rbp] - add rcx,r14 + lea rbp,[24+rbp] ; round++ + add rcx,r14 ; h+=Sigma0(a) mov r12,QWORD[48+rsi] mov r13,r10 mov r14,rcx @@ -324,38 +324,38 @@ $L$loop: xor r13,r10 ror r14,5 - xor r15,rax + xor r15,rax ; f^g mov QWORD[48+rsp],r12 xor r14,rcx - and r15,r10 + and r15,r10 ; (f^g)&e ror r13,4 - add r12,rbx - xor r15,rax + add r12,rbx ; T1+=h + xor r15,rax ; Ch(e,f,g)=((f^g)&e)^g ror r14,6 xor r13,r10 - add r12,r15 + add r12,r15 ; T1+=Ch(e,f,g) mov r15,rcx - add r12,QWORD[rbp] + add r12,QWORD[rbp] ; T1+=K[round] xor r14,rcx - xor r15,rdx - ror r13,14 + xor r15,rdx ; a^b, b^c in next round + ror r13,14 ; Sigma1(e) mov rbx,rdx and rdi,r15 - ror r14,28 - add r12,r13 + ror r14,28 ; Sigma0(a) + add r12,r13 ; T1+=Sigma1(e) - xor rbx,rdi - add r9,r12 - add rbx,r12 + xor rbx,rdi ; h=Maj(a,b,c)=Ch(a^b,c,b) + add r9,r12 ; d+=T1 + add rbx,r12 ; h+=T1 - lea rbp,[8+rbp] - add rbx,r14 + lea rbp,[8+rbp] ; round++ + add rbx,r14 ; h+=Sigma0(a) mov r12,QWORD[56+rsi] mov r13,r9 mov r14,rbx @@ -365,38 +365,38 @@ $L$loop: xor r13,r9 ror r14,5 - xor rdi,r11 + xor rdi,r11 ; f^g mov QWORD[56+rsp],r12 xor r14,rbx - and rdi,r9 + and rdi,r9 ; (f^g)&e ror r13,4 - add r12,rax - xor rdi,r11 + add r12,rax ; T1+=h + xor rdi,r11 ; Ch(e,f,g)=((f^g)&e)^g ror r14,6 xor r13,r9 - add r12,rdi + add r12,rdi ; T1+=Ch(e,f,g) mov rdi,rbx - add r12,QWORD[rbp] + add r12,QWORD[rbp] ; T1+=K[round] xor r14,rbx - xor rdi,rcx - ror r13,14 + xor rdi,rcx ; a^b, b^c in next round + ror r13,14 ; Sigma1(e) mov rax,rcx and r15,rdi - ror r14,28 - add r12,r13 + ror r14,28 ; Sigma0(a) + add r12,r13 ; T1+=Sigma1(e) - xor rax,r15 - add r8,r12 - add rax,r12 + xor rax,r15 ; h=Maj(a,b,c)=Ch(a^b,c,b) + add r8,r12 ; d+=T1 + add rax,r12 ; h+=T1 - lea rbp,[24+rbp] - add rax,r14 + lea rbp,[24+rbp] ; round++ + add rax,r14 ; h+=Sigma0(a) mov r12,QWORD[64+rsi] mov r13,r8 mov r14,rax @@ -406,38 +406,38 @@ $L$loop: xor r13,r8 ror r14,5 - xor r15,r10 + xor r15,r10 ; f^g mov QWORD[64+rsp],r12 xor r14,rax - and r15,r8 + and r15,r8 ; (f^g)&e ror r13,4 - add r12,r11 - xor r15,r10 + add r12,r11 ; T1+=h + xor r15,r10 ; Ch(e,f,g)=((f^g)&e)^g ror r14,6 xor r13,r8 - add r12,r15 + add r12,r15 ; T1+=Ch(e,f,g) mov r15,rax - add r12,QWORD[rbp] + add r12,QWORD[rbp] ; T1+=K[round] xor r14,rax - xor r15,rbx - ror r13,14 + xor r15,rbx ; a^b, b^c in next round + ror r13,14 ; Sigma1(e) mov r11,rbx and rdi,r15 - ror r14,28 - add r12,r13 + ror r14,28 ; Sigma0(a) + add r12,r13 ; T1+=Sigma1(e) - xor r11,rdi - add rdx,r12 - add r11,r12 + xor r11,rdi ; h=Maj(a,b,c)=Ch(a^b,c,b) + add rdx,r12 ; d+=T1 + add r11,r12 ; h+=T1 - lea rbp,[8+rbp] - add r11,r14 + lea rbp,[8+rbp] ; round++ + add r11,r14 ; h+=Sigma0(a) mov r12,QWORD[72+rsi] mov r13,rdx mov r14,r11 @@ -447,38 +447,38 @@ $L$loop: xor r13,rdx ror r14,5 - xor rdi,r9 + xor rdi,r9 ; f^g mov QWORD[72+rsp],r12 xor r14,r11 - and rdi,rdx + and rdi,rdx ; (f^g)&e ror r13,4 - add r12,r10 - xor rdi,r9 + add r12,r10 ; T1+=h + xor rdi,r9 ; Ch(e,f,g)=((f^g)&e)^g ror r14,6 xor r13,rdx - add r12,rdi + add r12,rdi ; T1+=Ch(e,f,g) mov rdi,r11 - add r12,QWORD[rbp] + add r12,QWORD[rbp] ; T1+=K[round] xor r14,r11 - xor rdi,rax - ror r13,14 + xor rdi,rax ; a^b, b^c in next round + ror r13,14 ; Sigma1(e) mov r10,rax and r15,rdi - ror r14,28 - add r12,r13 + ror r14,28 ; Sigma0(a) + add r12,r13 ; T1+=Sigma1(e) - xor r10,r15 - add rcx,r12 - add r10,r12 + xor r10,r15 ; h=Maj(a,b,c)=Ch(a^b,c,b) + add rcx,r12 ; d+=T1 + add r10,r12 ; h+=T1 - lea rbp,[24+rbp] - add r10,r14 + lea rbp,[24+rbp] ; round++ + add r10,r14 ; h+=Sigma0(a) mov r12,QWORD[80+rsi] mov r13,rcx mov r14,r10 @@ -488,38 +488,38 @@ $L$loop: xor r13,rcx ror r14,5 - xor r15,r8 + xor r15,r8 ; f^g mov QWORD[80+rsp],r12 xor r14,r10 - and r15,rcx + and r15,rcx ; (f^g)&e ror r13,4 - add r12,r9 - xor r15,r8 + add r12,r9 ; T1+=h + xor r15,r8 ; Ch(e,f,g)=((f^g)&e)^g ror r14,6 xor r13,rcx - add r12,r15 + add r12,r15 ; T1+=Ch(e,f,g) mov r15,r10 - add r12,QWORD[rbp] + add r12,QWORD[rbp] ; T1+=K[round] xor r14,r10 - xor r15,r11 - ror r13,14 + xor r15,r11 ; a^b, b^c in next round + ror r13,14 ; Sigma1(e) mov r9,r11 and rdi,r15 - ror r14,28 - add r12,r13 + ror r14,28 ; Sigma0(a) + add r12,r13 ; T1+=Sigma1(e) - xor r9,rdi - add rbx,r12 - add r9,r12 + xor r9,rdi ; h=Maj(a,b,c)=Ch(a^b,c,b) + add rbx,r12 ; d+=T1 + add r9,r12 ; h+=T1 - lea rbp,[8+rbp] - add r9,r14 + lea rbp,[8+rbp] ; round++ + add r9,r14 ; h+=Sigma0(a) mov r12,QWORD[88+rsi] mov r13,rbx mov r14,r9 @@ -529,38 +529,38 @@ $L$loop: xor r13,rbx ror r14,5 - xor rdi,rdx + xor rdi,rdx ; f^g mov QWORD[88+rsp],r12 xor r14,r9 - and rdi,rbx + and rdi,rbx ; (f^g)&e ror r13,4 - add r12,r8 - xor rdi,rdx + add r12,r8 ; T1+=h + xor rdi,rdx ; Ch(e,f,g)=((f^g)&e)^g ror r14,6 xor r13,rbx - add r12,rdi + add r12,rdi ; T1+=Ch(e,f,g) mov rdi,r9 - add r12,QWORD[rbp] + add r12,QWORD[rbp] ; T1+=K[round] xor r14,r9 - xor rdi,r10 - ror r13,14 + xor rdi,r10 ; a^b, b^c in next round + ror r13,14 ; Sigma1(e) mov r8,r10 and r15,rdi - ror r14,28 - add r12,r13 + ror r14,28 ; Sigma0(a) + add r12,r13 ; T1+=Sigma1(e) - xor r8,r15 - add rax,r12 - add r8,r12 + xor r8,r15 ; h=Maj(a,b,c)=Ch(a^b,c,b) + add rax,r12 ; d+=T1 + add r8,r12 ; h+=T1 - lea rbp,[24+rbp] - add r8,r14 + lea rbp,[24+rbp] ; round++ + add r8,r14 ; h+=Sigma0(a) mov r12,QWORD[96+rsi] mov r13,rax mov r14,r8 @@ -570,38 +570,38 @@ $L$loop: xor r13,rax ror r14,5 - xor r15,rcx + xor r15,rcx ; f^g mov QWORD[96+rsp],r12 xor r14,r8 - and r15,rax + and r15,rax ; (f^g)&e ror r13,4 - add r12,rdx - xor r15,rcx + add r12,rdx ; T1+=h + xor r15,rcx ; Ch(e,f,g)=((f^g)&e)^g ror r14,6 xor r13,rax - add r12,r15 + add r12,r15 ; T1+=Ch(e,f,g) mov r15,r8 - add r12,QWORD[rbp] + add r12,QWORD[rbp] ; T1+=K[round] xor r14,r8 - xor r15,r9 - ror r13,14 + xor r15,r9 ; a^b, b^c in next round + ror r13,14 ; Sigma1(e) mov rdx,r9 and rdi,r15 - ror r14,28 - add r12,r13 + ror r14,28 ; Sigma0(a) + add r12,r13 ; T1+=Sigma1(e) - xor rdx,rdi - add r11,r12 - add rdx,r12 + xor rdx,rdi ; h=Maj(a,b,c)=Ch(a^b,c,b) + add r11,r12 ; d+=T1 + add rdx,r12 ; h+=T1 - lea rbp,[8+rbp] - add rdx,r14 + lea rbp,[8+rbp] ; round++ + add rdx,r14 ; h+=Sigma0(a) mov r12,QWORD[104+rsi] mov r13,r11 mov r14,rdx @@ -611,38 +611,38 @@ $L$loop: xor r13,r11 ror r14,5 - xor rdi,rbx + xor rdi,rbx ; f^g mov QWORD[104+rsp],r12 xor r14,rdx - and rdi,r11 + and rdi,r11 ; (f^g)&e ror r13,4 - add r12,rcx - xor rdi,rbx + add r12,rcx ; T1+=h + xor rdi,rbx ; Ch(e,f,g)=((f^g)&e)^g ror r14,6 xor r13,r11 - add r12,rdi + add r12,rdi ; T1+=Ch(e,f,g) mov rdi,rdx - add r12,QWORD[rbp] + add r12,QWORD[rbp] ; T1+=K[round] xor r14,rdx - xor rdi,r8 - ror r13,14 + xor rdi,r8 ; a^b, b^c in next round + ror r13,14 ; Sigma1(e) mov rcx,r8 and r15,rdi - ror r14,28 - add r12,r13 + ror r14,28 ; Sigma0(a) + add r12,r13 ; T1+=Sigma1(e) - xor rcx,r15 - add r10,r12 - add rcx,r12 + xor rcx,r15 ; h=Maj(a,b,c)=Ch(a^b,c,b) + add r10,r12 ; d+=T1 + add rcx,r12 ; h+=T1 - lea rbp,[24+rbp] - add rcx,r14 + lea rbp,[24+rbp] ; round++ + add rcx,r14 ; h+=Sigma0(a) mov r12,QWORD[112+rsi] mov r13,r10 mov r14,rcx @@ -652,38 +652,38 @@ $L$loop: xor r13,r10 ror r14,5 - xor r15,rax + xor r15,rax ; f^g mov QWORD[112+rsp],r12 xor r14,rcx - and r15,r10 + and r15,r10 ; (f^g)&e ror r13,4 - add r12,rbx - xor r15,rax + add r12,rbx ; T1+=h + xor r15,rax ; Ch(e,f,g)=((f^g)&e)^g ror r14,6 xor r13,r10 - add r12,r15 + add r12,r15 ; T1+=Ch(e,f,g) mov r15,rcx - add r12,QWORD[rbp] + add r12,QWORD[rbp] ; T1+=K[round] xor r14,rcx - xor r15,rdx - ror r13,14 + xor r15,rdx ; a^b, b^c in next round + ror r13,14 ; Sigma1(e) mov rbx,rdx and rdi,r15 - ror r14,28 - add r12,r13 + ror r14,28 ; Sigma0(a) + add r12,r13 ; T1+=Sigma1(e) - xor rbx,rdi - add r9,r12 - add rbx,r12 + xor rbx,rdi ; h=Maj(a,b,c)=Ch(a^b,c,b) + add r9,r12 ; d+=T1 + add rbx,r12 ; h+=T1 - lea rbp,[8+rbp] - add rbx,r14 + lea rbp,[8+rbp] ; round++ + add rbx,r14 ; h+=Sigma0(a) mov r12,QWORD[120+rsi] mov r13,r9 mov r14,rbx @@ -693,37 +693,37 @@ $L$loop: xor r13,r9 ror r14,5 - xor rdi,r11 + xor rdi,r11 ; f^g mov QWORD[120+rsp],r12 xor r14,rbx - and rdi,r9 + and rdi,r9 ; (f^g)&e ror r13,4 - add r12,rax - xor rdi,r11 + add r12,rax ; T1+=h + xor rdi,r11 ; Ch(e,f,g)=((f^g)&e)^g ror r14,6 xor r13,r9 - add r12,rdi + add r12,rdi ; T1+=Ch(e,f,g) mov rdi,rbx - add r12,QWORD[rbp] + add r12,QWORD[rbp] ; T1+=K[round] xor r14,rbx - xor rdi,rcx - ror r13,14 + xor rdi,rcx ; a^b, b^c in next round + ror r13,14 ; Sigma1(e) mov rax,rcx and r15,rdi - ror r14,28 - add r12,r13 + ror r14,28 ; Sigma0(a) + add r12,r13 ; T1+=Sigma1(e) - xor rax,r15 - add r8,r12 - add rax,r12 + xor rax,r15 ; h=Maj(a,b,c)=Ch(a^b,c,b) + add r8,r12 ; d+=T1 + add rax,r12 ; h+=T1 - lea rbp,[24+rbp] + lea rbp,[24+rbp] ; round++ jmp NEAR $L$rounds_16_xx ALIGN 16 $L$rounds_16_xx: @@ -732,7 +732,7 @@ $L$rounds_16_xx: mov r12,r13 ror r13,7 - add rax,r14 + add rax,r14 ; modulo-scheduled h+=Sigma0(a) mov r14,r15 ror r15,42 @@ -743,8 +743,8 @@ $L$rounds_16_xx: shr r14,6 ror r15,19 - xor r12,r13 - xor r15,r14 + xor r12,r13 ; sigma0(X[(i+1)&0xf]) + xor r15,r14 ; sigma1(X[(i+14)&0xf]) add r12,QWORD[72+rsp] add r12,QWORD[rsp] @@ -756,43 +756,43 @@ $L$rounds_16_xx: xor r13,r8 ror r14,5 - xor r15,r10 + xor r15,r10 ; f^g mov QWORD[rsp],r12 xor r14,rax - and r15,r8 + and r15,r8 ; (f^g)&e ror r13,4 - add r12,r11 - xor r15,r10 + add r12,r11 ; T1+=h + xor r15,r10 ; Ch(e,f,g)=((f^g)&e)^g ror r14,6 xor r13,r8 - add r12,r15 + add r12,r15 ; T1+=Ch(e,f,g) mov r15,rax - add r12,QWORD[rbp] + add r12,QWORD[rbp] ; T1+=K[round] xor r14,rax - xor r15,rbx - ror r13,14 + xor r15,rbx ; a^b, b^c in next round + ror r13,14 ; Sigma1(e) mov r11,rbx and rdi,r15 - ror r14,28 - add r12,r13 + ror r14,28 ; Sigma0(a) + add r12,r13 ; T1+=Sigma1(e) - xor r11,rdi - add rdx,r12 - add r11,r12 + xor r11,rdi ; h=Maj(a,b,c)=Ch(a^b,c,b) + add rdx,r12 ; d+=T1 + add r11,r12 ; h+=T1 - lea rbp,[8+rbp] + lea rbp,[8+rbp] ; round++ mov r13,QWORD[16+rsp] mov rdi,QWORD[120+rsp] mov r12,r13 ror r13,7 - add r11,r14 + add r11,r14 ; modulo-scheduled h+=Sigma0(a) mov r14,rdi ror rdi,42 @@ -803,8 +803,8 @@ $L$rounds_16_xx: shr r14,6 ror rdi,19 - xor r12,r13 - xor rdi,r14 + xor r12,r13 ; sigma0(X[(i+1)&0xf]) + xor rdi,r14 ; sigma1(X[(i+14)&0xf]) add r12,QWORD[80+rsp] add r12,QWORD[8+rsp] @@ -816,43 +816,43 @@ $L$rounds_16_xx: xor r13,rdx ror r14,5 - xor rdi,r9 + xor rdi,r9 ; f^g mov QWORD[8+rsp],r12 xor r14,r11 - and rdi,rdx + and rdi,rdx ; (f^g)&e ror r13,4 - add r12,r10 - xor rdi,r9 + add r12,r10 ; T1+=h + xor rdi,r9 ; Ch(e,f,g)=((f^g)&e)^g ror r14,6 xor r13,rdx - add r12,rdi + add r12,rdi ; T1+=Ch(e,f,g) mov rdi,r11 - add r12,QWORD[rbp] + add r12,QWORD[rbp] ; T1+=K[round] xor r14,r11 - xor rdi,rax - ror r13,14 + xor rdi,rax ; a^b, b^c in next round + ror r13,14 ; Sigma1(e) mov r10,rax and r15,rdi - ror r14,28 - add r12,r13 + ror r14,28 ; Sigma0(a) + add r12,r13 ; T1+=Sigma1(e) - xor r10,r15 - add rcx,r12 - add r10,r12 + xor r10,r15 ; h=Maj(a,b,c)=Ch(a^b,c,b) + add rcx,r12 ; d+=T1 + add r10,r12 ; h+=T1 - lea rbp,[24+rbp] + lea rbp,[24+rbp] ; round++ mov r13,QWORD[24+rsp] mov r15,QWORD[rsp] mov r12,r13 ror r13,7 - add r10,r14 + add r10,r14 ; modulo-scheduled h+=Sigma0(a) mov r14,r15 ror r15,42 @@ -863,8 +863,8 @@ $L$rounds_16_xx: shr r14,6 ror r15,19 - xor r12,r13 - xor r15,r14 + xor r12,r13 ; sigma0(X[(i+1)&0xf]) + xor r15,r14 ; sigma1(X[(i+14)&0xf]) add r12,QWORD[88+rsp] add r12,QWORD[16+rsp] @@ -876,43 +876,43 @@ $L$rounds_16_xx: xor r13,rcx ror r14,5 - xor r15,r8 + xor r15,r8 ; f^g mov QWORD[16+rsp],r12 xor r14,r10 - and r15,rcx + and r15,rcx ; (f^g)&e ror r13,4 - add r12,r9 - xor r15,r8 + add r12,r9 ; T1+=h + xor r15,r8 ; Ch(e,f,g)=((f^g)&e)^g ror r14,6 xor r13,rcx - add r12,r15 + add r12,r15 ; T1+=Ch(e,f,g) mov r15,r10 - add r12,QWORD[rbp] + add r12,QWORD[rbp] ; T1+=K[round] xor r14,r10 - xor r15,r11 - ror r13,14 + xor r15,r11 ; a^b, b^c in next round + ror r13,14 ; Sigma1(e) mov r9,r11 and rdi,r15 - ror r14,28 - add r12,r13 + ror r14,28 ; Sigma0(a) + add r12,r13 ; T1+=Sigma1(e) - xor r9,rdi - add rbx,r12 - add r9,r12 + xor r9,rdi ; h=Maj(a,b,c)=Ch(a^b,c,b) + add rbx,r12 ; d+=T1 + add r9,r12 ; h+=T1 - lea rbp,[8+rbp] + lea rbp,[8+rbp] ; round++ mov r13,QWORD[32+rsp] mov rdi,QWORD[8+rsp] mov r12,r13 ror r13,7 - add r9,r14 + add r9,r14 ; modulo-scheduled h+=Sigma0(a) mov r14,rdi ror rdi,42 @@ -923,8 +923,8 @@ $L$rounds_16_xx: shr r14,6 ror rdi,19 - xor r12,r13 - xor rdi,r14 + xor r12,r13 ; sigma0(X[(i+1)&0xf]) + xor rdi,r14 ; sigma1(X[(i+14)&0xf]) add r12,QWORD[96+rsp] add r12,QWORD[24+rsp] @@ -936,43 +936,43 @@ $L$rounds_16_xx: xor r13,rbx ror r14,5 - xor rdi,rdx + xor rdi,rdx ; f^g mov QWORD[24+rsp],r12 xor r14,r9 - and rdi,rbx + and rdi,rbx ; (f^g)&e ror r13,4 - add r12,r8 - xor rdi,rdx + add r12,r8 ; T1+=h + xor rdi,rdx ; Ch(e,f,g)=((f^g)&e)^g ror r14,6 xor r13,rbx - add r12,rdi + add r12,rdi ; T1+=Ch(e,f,g) mov rdi,r9 - add r12,QWORD[rbp] + add r12,QWORD[rbp] ; T1+=K[round] xor r14,r9 - xor rdi,r10 - ror r13,14 + xor rdi,r10 ; a^b, b^c in next round + ror r13,14 ; Sigma1(e) mov r8,r10 and r15,rdi - ror r14,28 - add r12,r13 + ror r14,28 ; Sigma0(a) + add r12,r13 ; T1+=Sigma1(e) - xor r8,r15 - add rax,r12 - add r8,r12 + xor r8,r15 ; h=Maj(a,b,c)=Ch(a^b,c,b) + add rax,r12 ; d+=T1 + add r8,r12 ; h+=T1 - lea rbp,[24+rbp] + lea rbp,[24+rbp] ; round++ mov r13,QWORD[40+rsp] mov r15,QWORD[16+rsp] mov r12,r13 ror r13,7 - add r8,r14 + add r8,r14 ; modulo-scheduled h+=Sigma0(a) mov r14,r15 ror r15,42 @@ -983,8 +983,8 @@ $L$rounds_16_xx: shr r14,6 ror r15,19 - xor r12,r13 - xor r15,r14 + xor r12,r13 ; sigma0(X[(i+1)&0xf]) + xor r15,r14 ; sigma1(X[(i+14)&0xf]) add r12,QWORD[104+rsp] add r12,QWORD[32+rsp] @@ -996,43 +996,43 @@ $L$rounds_16_xx: xor r13,rax ror r14,5 - xor r15,rcx + xor r15,rcx ; f^g mov QWORD[32+rsp],r12 xor r14,r8 - and r15,rax + and r15,rax ; (f^g)&e ror r13,4 - add r12,rdx - xor r15,rcx + add r12,rdx ; T1+=h + xor r15,rcx ; Ch(e,f,g)=((f^g)&e)^g ror r14,6 xor r13,rax - add r12,r15 + add r12,r15 ; T1+=Ch(e,f,g) mov r15,r8 - add r12,QWORD[rbp] + add r12,QWORD[rbp] ; T1+=K[round] xor r14,r8 - xor r15,r9 - ror r13,14 + xor r15,r9 ; a^b, b^c in next round + ror r13,14 ; Sigma1(e) mov rdx,r9 and rdi,r15 - ror r14,28 - add r12,r13 + ror r14,28 ; Sigma0(a) + add r12,r13 ; T1+=Sigma1(e) - xor rdx,rdi - add r11,r12 - add rdx,r12 + xor rdx,rdi ; h=Maj(a,b,c)=Ch(a^b,c,b) + add r11,r12 ; d+=T1 + add rdx,r12 ; h+=T1 - lea rbp,[8+rbp] + lea rbp,[8+rbp] ; round++ mov r13,QWORD[48+rsp] mov rdi,QWORD[24+rsp] mov r12,r13 ror r13,7 - add rdx,r14 + add rdx,r14 ; modulo-scheduled h+=Sigma0(a) mov r14,rdi ror rdi,42 @@ -1043,8 +1043,8 @@ $L$rounds_16_xx: shr r14,6 ror rdi,19 - xor r12,r13 - xor rdi,r14 + xor r12,r13 ; sigma0(X[(i+1)&0xf]) + xor rdi,r14 ; sigma1(X[(i+14)&0xf]) add r12,QWORD[112+rsp] add r12,QWORD[40+rsp] @@ -1056,43 +1056,43 @@ $L$rounds_16_xx: xor r13,r11 ror r14,5 - xor rdi,rbx + xor rdi,rbx ; f^g mov QWORD[40+rsp],r12 xor r14,rdx - and rdi,r11 + and rdi,r11 ; (f^g)&e ror r13,4 - add r12,rcx - xor rdi,rbx + add r12,rcx ; T1+=h + xor rdi,rbx ; Ch(e,f,g)=((f^g)&e)^g ror r14,6 xor r13,r11 - add r12,rdi + add r12,rdi ; T1+=Ch(e,f,g) mov rdi,rdx - add r12,QWORD[rbp] + add r12,QWORD[rbp] ; T1+=K[round] xor r14,rdx - xor rdi,r8 - ror r13,14 + xor rdi,r8 ; a^b, b^c in next round + ror r13,14 ; Sigma1(e) mov rcx,r8 and r15,rdi - ror r14,28 - add r12,r13 + ror r14,28 ; Sigma0(a) + add r12,r13 ; T1+=Sigma1(e) - xor rcx,r15 - add r10,r12 - add rcx,r12 + xor rcx,r15 ; h=Maj(a,b,c)=Ch(a^b,c,b) + add r10,r12 ; d+=T1 + add rcx,r12 ; h+=T1 - lea rbp,[24+rbp] + lea rbp,[24+rbp] ; round++ mov r13,QWORD[56+rsp] mov r15,QWORD[32+rsp] mov r12,r13 ror r13,7 - add rcx,r14 + add rcx,r14 ; modulo-scheduled h+=Sigma0(a) mov r14,r15 ror r15,42 @@ -1103,8 +1103,8 @@ $L$rounds_16_xx: shr r14,6 ror r15,19 - xor r12,r13 - xor r15,r14 + xor r12,r13 ; sigma0(X[(i+1)&0xf]) + xor r15,r14 ; sigma1(X[(i+14)&0xf]) add r12,QWORD[120+rsp] add r12,QWORD[48+rsp] @@ -1116,43 +1116,43 @@ $L$rounds_16_xx: xor r13,r10 ror r14,5 - xor r15,rax + xor r15,rax ; f^g mov QWORD[48+rsp],r12 xor r14,rcx - and r15,r10 + and r15,r10 ; (f^g)&e ror r13,4 - add r12,rbx - xor r15,rax + add r12,rbx ; T1+=h + xor r15,rax ; Ch(e,f,g)=((f^g)&e)^g ror r14,6 xor r13,r10 - add r12,r15 + add r12,r15 ; T1+=Ch(e,f,g) mov r15,rcx - add r12,QWORD[rbp] + add r12,QWORD[rbp] ; T1+=K[round] xor r14,rcx - xor r15,rdx - ror r13,14 + xor r15,rdx ; a^b, b^c in next round + ror r13,14 ; Sigma1(e) mov rbx,rdx and rdi,r15 - ror r14,28 - add r12,r13 + ror r14,28 ; Sigma0(a) + add r12,r13 ; T1+=Sigma1(e) - xor rbx,rdi - add r9,r12 - add rbx,r12 + xor rbx,rdi ; h=Maj(a,b,c)=Ch(a^b,c,b) + add r9,r12 ; d+=T1 + add rbx,r12 ; h+=T1 - lea rbp,[8+rbp] + lea rbp,[8+rbp] ; round++ mov r13,QWORD[64+rsp] mov rdi,QWORD[40+rsp] mov r12,r13 ror r13,7 - add rbx,r14 + add rbx,r14 ; modulo-scheduled h+=Sigma0(a) mov r14,rdi ror rdi,42 @@ -1163,8 +1163,8 @@ $L$rounds_16_xx: shr r14,6 ror rdi,19 - xor r12,r13 - xor rdi,r14 + xor r12,r13 ; sigma0(X[(i+1)&0xf]) + xor rdi,r14 ; sigma1(X[(i+14)&0xf]) add r12,QWORD[rsp] add r12,QWORD[56+rsp] @@ -1176,43 +1176,43 @@ $L$rounds_16_xx: xor r13,r9 ror r14,5 - xor rdi,r11 + xor rdi,r11 ; f^g mov QWORD[56+rsp],r12 xor r14,rbx - and rdi,r9 + and rdi,r9 ; (f^g)&e ror r13,4 - add r12,rax - xor rdi,r11 + add r12,rax ; T1+=h + xor rdi,r11 ; Ch(e,f,g)=((f^g)&e)^g ror r14,6 xor r13,r9 - add r12,rdi + add r12,rdi ; T1+=Ch(e,f,g) mov rdi,rbx - add r12,QWORD[rbp] + add r12,QWORD[rbp] ; T1+=K[round] xor r14,rbx - xor rdi,rcx - ror r13,14 + xor rdi,rcx ; a^b, b^c in next round + ror r13,14 ; Sigma1(e) mov rax,rcx and r15,rdi - ror r14,28 - add r12,r13 + ror r14,28 ; Sigma0(a) + add r12,r13 ; T1+=Sigma1(e) - xor rax,r15 - add r8,r12 - add rax,r12 + xor rax,r15 ; h=Maj(a,b,c)=Ch(a^b,c,b) + add r8,r12 ; d+=T1 + add rax,r12 ; h+=T1 - lea rbp,[24+rbp] + lea rbp,[24+rbp] ; round++ mov r13,QWORD[72+rsp] mov r15,QWORD[48+rsp] mov r12,r13 ror r13,7 - add rax,r14 + add rax,r14 ; modulo-scheduled h+=Sigma0(a) mov r14,r15 ror r15,42 @@ -1223,8 +1223,8 @@ $L$rounds_16_xx: shr r14,6 ror r15,19 - xor r12,r13 - xor r15,r14 + xor r12,r13 ; sigma0(X[(i+1)&0xf]) + xor r15,r14 ; sigma1(X[(i+14)&0xf]) add r12,QWORD[8+rsp] add r12,QWORD[64+rsp] @@ -1236,43 +1236,43 @@ $L$rounds_16_xx: xor r13,r8 ror r14,5 - xor r15,r10 + xor r15,r10 ; f^g mov QWORD[64+rsp],r12 xor r14,rax - and r15,r8 + and r15,r8 ; (f^g)&e ror r13,4 - add r12,r11 - xor r15,r10 + add r12,r11 ; T1+=h + xor r15,r10 ; Ch(e,f,g)=((f^g)&e)^g ror r14,6 xor r13,r8 - add r12,r15 + add r12,r15 ; T1+=Ch(e,f,g) mov r15,rax - add r12,QWORD[rbp] + add r12,QWORD[rbp] ; T1+=K[round] xor r14,rax - xor r15,rbx - ror r13,14 + xor r15,rbx ; a^b, b^c in next round + ror r13,14 ; Sigma1(e) mov r11,rbx and rdi,r15 - ror r14,28 - add r12,r13 + ror r14,28 ; Sigma0(a) + add r12,r13 ; T1+=Sigma1(e) - xor r11,rdi - add rdx,r12 - add r11,r12 + xor r11,rdi ; h=Maj(a,b,c)=Ch(a^b,c,b) + add rdx,r12 ; d+=T1 + add r11,r12 ; h+=T1 - lea rbp,[8+rbp] + lea rbp,[8+rbp] ; round++ mov r13,QWORD[80+rsp] mov rdi,QWORD[56+rsp] mov r12,r13 ror r13,7 - add r11,r14 + add r11,r14 ; modulo-scheduled h+=Sigma0(a) mov r14,rdi ror rdi,42 @@ -1283,8 +1283,8 @@ $L$rounds_16_xx: shr r14,6 ror rdi,19 - xor r12,r13 - xor rdi,r14 + xor r12,r13 ; sigma0(X[(i+1)&0xf]) + xor rdi,r14 ; sigma1(X[(i+14)&0xf]) add r12,QWORD[16+rsp] add r12,QWORD[72+rsp] @@ -1296,43 +1296,43 @@ $L$rounds_16_xx: xor r13,rdx ror r14,5 - xor rdi,r9 + xor rdi,r9 ; f^g mov QWORD[72+rsp],r12 xor r14,r11 - and rdi,rdx + and rdi,rdx ; (f^g)&e ror r13,4 - add r12,r10 - xor rdi,r9 + add r12,r10 ; T1+=h + xor rdi,r9 ; Ch(e,f,g)=((f^g)&e)^g ror r14,6 xor r13,rdx - add r12,rdi + add r12,rdi ; T1+=Ch(e,f,g) mov rdi,r11 - add r12,QWORD[rbp] + add r12,QWORD[rbp] ; T1+=K[round] xor r14,r11 - xor rdi,rax - ror r13,14 + xor rdi,rax ; a^b, b^c in next round + ror r13,14 ; Sigma1(e) mov r10,rax and r15,rdi - ror r14,28 - add r12,r13 + ror r14,28 ; Sigma0(a) + add r12,r13 ; T1+=Sigma1(e) - xor r10,r15 - add rcx,r12 - add r10,r12 + xor r10,r15 ; h=Maj(a,b,c)=Ch(a^b,c,b) + add rcx,r12 ; d+=T1 + add r10,r12 ; h+=T1 - lea rbp,[24+rbp] + lea rbp,[24+rbp] ; round++ mov r13,QWORD[88+rsp] mov r15,QWORD[64+rsp] mov r12,r13 ror r13,7 - add r10,r14 + add r10,r14 ; modulo-scheduled h+=Sigma0(a) mov r14,r15 ror r15,42 @@ -1343,8 +1343,8 @@ $L$rounds_16_xx: shr r14,6 ror r15,19 - xor r12,r13 - xor r15,r14 + xor r12,r13 ; sigma0(X[(i+1)&0xf]) + xor r15,r14 ; sigma1(X[(i+14)&0xf]) add r12,QWORD[24+rsp] add r12,QWORD[80+rsp] @@ -1356,43 +1356,43 @@ $L$rounds_16_xx: xor r13,rcx ror r14,5 - xor r15,r8 + xor r15,r8 ; f^g mov QWORD[80+rsp],r12 xor r14,r10 - and r15,rcx + and r15,rcx ; (f^g)&e ror r13,4 - add r12,r9 - xor r15,r8 + add r12,r9 ; T1+=h + xor r15,r8 ; Ch(e,f,g)=((f^g)&e)^g ror r14,6 xor r13,rcx - add r12,r15 + add r12,r15 ; T1+=Ch(e,f,g) mov r15,r10 - add r12,QWORD[rbp] + add r12,QWORD[rbp] ; T1+=K[round] xor r14,r10 - xor r15,r11 - ror r13,14 + xor r15,r11 ; a^b, b^c in next round + ror r13,14 ; Sigma1(e) mov r9,r11 and rdi,r15 - ror r14,28 - add r12,r13 + ror r14,28 ; Sigma0(a) + add r12,r13 ; T1+=Sigma1(e) - xor r9,rdi - add rbx,r12 - add r9,r12 + xor r9,rdi ; h=Maj(a,b,c)=Ch(a^b,c,b) + add rbx,r12 ; d+=T1 + add r9,r12 ; h+=T1 - lea rbp,[8+rbp] + lea rbp,[8+rbp] ; round++ mov r13,QWORD[96+rsp] mov rdi,QWORD[72+rsp] mov r12,r13 ror r13,7 - add r9,r14 + add r9,r14 ; modulo-scheduled h+=Sigma0(a) mov r14,rdi ror rdi,42 @@ -1403,8 +1403,8 @@ $L$rounds_16_xx: shr r14,6 ror rdi,19 - xor r12,r13 - xor rdi,r14 + xor r12,r13 ; sigma0(X[(i+1)&0xf]) + xor rdi,r14 ; sigma1(X[(i+14)&0xf]) add r12,QWORD[32+rsp] add r12,QWORD[88+rsp] @@ -1416,43 +1416,43 @@ $L$rounds_16_xx: xor r13,rbx ror r14,5 - xor rdi,rdx + xor rdi,rdx ; f^g mov QWORD[88+rsp],r12 xor r14,r9 - and rdi,rbx + and rdi,rbx ; (f^g)&e ror r13,4 - add r12,r8 - xor rdi,rdx + add r12,r8 ; T1+=h + xor rdi,rdx ; Ch(e,f,g)=((f^g)&e)^g ror r14,6 xor r13,rbx - add r12,rdi + add r12,rdi ; T1+=Ch(e,f,g) mov rdi,r9 - add r12,QWORD[rbp] + add r12,QWORD[rbp] ; T1+=K[round] xor r14,r9 - xor rdi,r10 - ror r13,14 + xor rdi,r10 ; a^b, b^c in next round + ror r13,14 ; Sigma1(e) mov r8,r10 and r15,rdi - ror r14,28 - add r12,r13 + ror r14,28 ; Sigma0(a) + add r12,r13 ; T1+=Sigma1(e) - xor r8,r15 - add rax,r12 - add r8,r12 + xor r8,r15 ; h=Maj(a,b,c)=Ch(a^b,c,b) + add rax,r12 ; d+=T1 + add r8,r12 ; h+=T1 - lea rbp,[24+rbp] + lea rbp,[24+rbp] ; round++ mov r13,QWORD[104+rsp] mov r15,QWORD[80+rsp] mov r12,r13 ror r13,7 - add r8,r14 + add r8,r14 ; modulo-scheduled h+=Sigma0(a) mov r14,r15 ror r15,42 @@ -1463,8 +1463,8 @@ $L$rounds_16_xx: shr r14,6 ror r15,19 - xor r12,r13 - xor r15,r14 + xor r12,r13 ; sigma0(X[(i+1)&0xf]) + xor r15,r14 ; sigma1(X[(i+14)&0xf]) add r12,QWORD[40+rsp] add r12,QWORD[96+rsp] @@ -1476,43 +1476,43 @@ $L$rounds_16_xx: xor r13,rax ror r14,5 - xor r15,rcx + xor r15,rcx ; f^g mov QWORD[96+rsp],r12 xor r14,r8 - and r15,rax + and r15,rax ; (f^g)&e ror r13,4 - add r12,rdx - xor r15,rcx + add r12,rdx ; T1+=h + xor r15,rcx ; Ch(e,f,g)=((f^g)&e)^g ror r14,6 xor r13,rax - add r12,r15 + add r12,r15 ; T1+=Ch(e,f,g) mov r15,r8 - add r12,QWORD[rbp] + add r12,QWORD[rbp] ; T1+=K[round] xor r14,r8 - xor r15,r9 - ror r13,14 + xor r15,r9 ; a^b, b^c in next round + ror r13,14 ; Sigma1(e) mov rdx,r9 and rdi,r15 - ror r14,28 - add r12,r13 + ror r14,28 ; Sigma0(a) + add r12,r13 ; T1+=Sigma1(e) - xor rdx,rdi - add r11,r12 - add rdx,r12 + xor rdx,rdi ; h=Maj(a,b,c)=Ch(a^b,c,b) + add r11,r12 ; d+=T1 + add rdx,r12 ; h+=T1 - lea rbp,[8+rbp] + lea rbp,[8+rbp] ; round++ mov r13,QWORD[112+rsp] mov rdi,QWORD[88+rsp] mov r12,r13 ror r13,7 - add rdx,r14 + add rdx,r14 ; modulo-scheduled h+=Sigma0(a) mov r14,rdi ror rdi,42 @@ -1523,8 +1523,8 @@ $L$rounds_16_xx: shr r14,6 ror rdi,19 - xor r12,r13 - xor rdi,r14 + xor r12,r13 ; sigma0(X[(i+1)&0xf]) + xor rdi,r14 ; sigma1(X[(i+14)&0xf]) add r12,QWORD[48+rsp] add r12,QWORD[104+rsp] @@ -1536,43 +1536,43 @@ $L$rounds_16_xx: xor r13,r11 ror r14,5 - xor rdi,rbx + xor rdi,rbx ; f^g mov QWORD[104+rsp],r12 xor r14,rdx - and rdi,r11 + and rdi,r11 ; (f^g)&e ror r13,4 - add r12,rcx - xor rdi,rbx + add r12,rcx ; T1+=h + xor rdi,rbx ; Ch(e,f,g)=((f^g)&e)^g ror r14,6 xor r13,r11 - add r12,rdi + add r12,rdi ; T1+=Ch(e,f,g) mov rdi,rdx - add r12,QWORD[rbp] + add r12,QWORD[rbp] ; T1+=K[round] xor r14,rdx - xor rdi,r8 - ror r13,14 + xor rdi,r8 ; a^b, b^c in next round + ror r13,14 ; Sigma1(e) mov rcx,r8 and r15,rdi - ror r14,28 - add r12,r13 + ror r14,28 ; Sigma0(a) + add r12,r13 ; T1+=Sigma1(e) - xor rcx,r15 - add r10,r12 - add rcx,r12 + xor rcx,r15 ; h=Maj(a,b,c)=Ch(a^b,c,b) + add r10,r12 ; d+=T1 + add rcx,r12 ; h+=T1 - lea rbp,[24+rbp] + lea rbp,[24+rbp] ; round++ mov r13,QWORD[120+rsp] mov r15,QWORD[96+rsp] mov r12,r13 ror r13,7 - add rcx,r14 + add rcx,r14 ; modulo-scheduled h+=Sigma0(a) mov r14,r15 ror r15,42 @@ -1583,8 +1583,8 @@ $L$rounds_16_xx: shr r14,6 ror r15,19 - xor r12,r13 - xor r15,r14 + xor r12,r13 ; sigma0(X[(i+1)&0xf]) + xor r15,r14 ; sigma1(X[(i+14)&0xf]) add r12,QWORD[56+rsp] add r12,QWORD[112+rsp] @@ -1596,43 +1596,43 @@ $L$rounds_16_xx: xor r13,r10 ror r14,5 - xor r15,rax + xor r15,rax ; f^g mov QWORD[112+rsp],r12 xor r14,rcx - and r15,r10 + and r15,r10 ; (f^g)&e ror r13,4 - add r12,rbx - xor r15,rax + add r12,rbx ; T1+=h + xor r15,rax ; Ch(e,f,g)=((f^g)&e)^g ror r14,6 xor r13,r10 - add r12,r15 + add r12,r15 ; T1+=Ch(e,f,g) mov r15,rcx - add r12,QWORD[rbp] + add r12,QWORD[rbp] ; T1+=K[round] xor r14,rcx - xor r15,rdx - ror r13,14 + xor r15,rdx ; a^b, b^c in next round + ror r13,14 ; Sigma1(e) mov rbx,rdx and rdi,r15 - ror r14,28 - add r12,r13 + ror r14,28 ; Sigma0(a) + add r12,r13 ; T1+=Sigma1(e) - xor rbx,rdi - add r9,r12 - add rbx,r12 + xor rbx,rdi ; h=Maj(a,b,c)=Ch(a^b,c,b) + add r9,r12 ; d+=T1 + add rbx,r12 ; h+=T1 - lea rbp,[8+rbp] + lea rbp,[8+rbp] ; round++ mov r13,QWORD[rsp] mov rdi,QWORD[104+rsp] mov r12,r13 ror r13,7 - add rbx,r14 + add rbx,r14 ; modulo-scheduled h+=Sigma0(a) mov r14,rdi ror rdi,42 @@ -1643,8 +1643,8 @@ $L$rounds_16_xx: shr r14,6 ror rdi,19 - xor r12,r13 - xor rdi,r14 + xor r12,r13 ; sigma0(X[(i+1)&0xf]) + xor rdi,r14 ; sigma1(X[(i+14)&0xf]) add r12,QWORD[64+rsp] add r12,QWORD[120+rsp] @@ -1656,42 +1656,42 @@ $L$rounds_16_xx: xor r13,r9 ror r14,5 - xor rdi,r11 + xor rdi,r11 ; f^g mov QWORD[120+rsp],r12 xor r14,rbx - and rdi,r9 + and rdi,r9 ; (f^g)&e ror r13,4 - add r12,rax - xor rdi,r11 + add r12,rax ; T1+=h + xor rdi,r11 ; Ch(e,f,g)=((f^g)&e)^g ror r14,6 xor r13,r9 - add r12,rdi + add r12,rdi ; T1+=Ch(e,f,g) mov rdi,rbx - add r12,QWORD[rbp] + add r12,QWORD[rbp] ; T1+=K[round] xor r14,rbx - xor rdi,rcx - ror r13,14 + xor rdi,rcx ; a^b, b^c in next round + ror r13,14 ; Sigma1(e) mov rax,rcx and r15,rdi - ror r14,28 - add r12,r13 + ror r14,28 ; Sigma0(a) + add r12,r13 ; T1+=Sigma1(e) - xor rax,r15 - add r8,r12 - add rax,r12 + xor rax,r15 ; h=Maj(a,b,c)=Ch(a^b,c,b) + add r8,r12 ; d+=T1 + add rax,r12 ; h+=T1 - lea rbp,[24+rbp] + lea rbp,[24+rbp] ; round++ cmp BYTE[7+rbp],0 jnz NEAR $L$rounds_16_xx mov rdi,QWORD[((128+0))+rsp] - add rax,r14 + add rax,r14 ; modulo-scheduled h+=Sigma0(a) lea rsi,[128+rsi] add rax,QWORD[rdi] @@ -1829,7 +1829,7 @@ K512: DB 52,44,32,67,82,89,80,84,79,71,65,77,83,32,98,121 DB 32,60,97,112,112,114,111,64,111,112,101,110,115,115,108,46 DB 111,114,103,62,0 -section .text +section .text code align=64 global sha512_block_data_order_avx @@ -1846,7 +1846,7 @@ $L$SEH_begin_sha512_block_data_order_avx: _CET_ENDBR - mov rax,rsp + mov rax,rsp ; copy %rsp push rbx @@ -1860,14 +1860,14 @@ _CET_ENDBR push r15 - shl rdx,4 + shl rdx,4 ; num*16 sub rsp,256 - lea rdx,[rdx*8+rsi] - and rsp,-64 - mov QWORD[((128+0))+rsp],rdi - mov QWORD[((128+8))+rsp],rsi - mov QWORD[((128+16))+rsp],rdx - mov QWORD[152+rsp],rax + lea rdx,[rdx*8+rsi] ; inp+num*16*8 + and rsp,-64 ; align stack frame + mov QWORD[((128+0))+rsp],rdi ; save ctx, 1st arg + mov QWORD[((128+8))+rsp],rsi ; save inp, 2nd arh + mov QWORD[((128+16))+rsp],rdx ; save end pointer, "3rd" arg + mov QWORD[152+rsp],rax ; save copy of %rsp movaps XMMWORD[(128+32)+rsp],xmm6 movaps XMMWORD[(128+48)+rsp],xmm7 @@ -1891,7 +1891,7 @@ ALIGN 16 $L$loop_avx: vmovdqa xmm11,XMMWORD[((K512+1280))] vmovdqu xmm0,XMMWORD[rsi] - lea rbp,[((K512+128))] + lea rbp,[((K512+128))] ; size optimization vmovdqu xmm1,XMMWORD[16+rsi] vmovdqu xmm2,XMMWORD[32+rsi] vpshufb xmm0,xmm0,xmm11 @@ -1924,7 +1924,7 @@ $L$loop_avx: vmovdqa XMMWORD[80+rsp],xmm9 mov rdi,rbx vmovdqa XMMWORD[96+rsp],xmm10 - xor rdi,rcx + xor rdi,rcx ; magic vmovdqa XMMWORD[112+rsp],xmm11 mov r13,r8 jmp NEAR $L$avx_00_47 @@ -3035,25 +3035,25 @@ se_handler: pushfq sub rsp,64 - mov rax,QWORD[120+r8] - mov rbx,QWORD[248+r8] + mov rax,QWORD[120+r8] ; pull context->Rax + mov rbx,QWORD[248+r8] ; pull context->Rip - mov rsi,QWORD[8+r9] - mov r11,QWORD[56+r9] + mov rsi,QWORD[8+r9] ; disp->ImageBase + mov r11,QWORD[56+r9] ; disp->HanderlData - mov r10d,DWORD[r11] - lea r10,[r10*1+rsi] - cmp rbx,r10 + mov r10d,DWORD[r11] ; HandlerData[0] + lea r10,[r10*1+rsi] ; prologue label + cmp rbx,r10 ; context->RipRsp - mov r10d,DWORD[4+r11] - lea r10,[r10*1+rsi] - cmp rbx,r10 + mov r10d,DWORD[4+r11] ; HandlerData[1] + lea r10,[r10*1+rsi] ; epilogue label + cmp rbx,r10 ; context->Rip>=epilogue label jae NEAR $L$in_prologue - mov rsi,rax - mov rax,QWORD[((128+24))+rax] + mov rsi,rax ; put aside Rsp + mov rax,QWORD[((128+24))+rax] ; pull 152(%rsp) mov rbx,QWORD[((-8))+rax] mov rbp,QWORD[((-16))+rax] @@ -3061,49 +3061,49 @@ se_handler: mov r13,QWORD[((-32))+rax] mov r14,QWORD[((-40))+rax] mov r15,QWORD[((-48))+rax] - mov QWORD[144+r8],rbx - mov QWORD[160+r8],rbp - mov QWORD[216+r8],r12 - mov QWORD[224+r8],r13 - mov QWORD[232+r8],r14 - mov QWORD[240+r8],r15 + mov QWORD[144+r8],rbx ; restore context->Rbx + mov QWORD[160+r8],rbp ; restore context->Rbp + mov QWORD[216+r8],r12 ; restore context->R12 + mov QWORD[224+r8],r13 ; restore context->R13 + mov QWORD[232+r8],r14 ; restore context->R14 + mov QWORD[240+r8],r15 ; restore context->R15 lea r10,[$L$epilogue] cmp rbx,r10 - jb NEAR $L$in_prologue + jb NEAR $L$in_prologue ; non-AVX code - lea rsi,[((128+32))+rsi] - lea rdi,[512+r8] + lea rsi,[((128+32))+rsi] ; Xmm6- save area + lea rdi,[512+r8] ; &context.Xmm6 mov ecx,12 - DD 0xa548f3fc + DD 0xa548f3fc ; cld; rep movsq $L$in_prologue: mov rdi,QWORD[8+rax] mov rsi,QWORD[16+rax] - mov QWORD[152+r8],rax - mov QWORD[168+r8],rsi - mov QWORD[176+r8],rdi + mov QWORD[152+r8],rax ; restore context->Rsp + mov QWORD[168+r8],rsi ; restore context->Rsi + mov QWORD[176+r8],rdi ; restore context->Rdi - mov rdi,QWORD[40+r9] - mov rsi,r8 - mov ecx,154 - DD 0xa548f3fc + mov rdi,QWORD[40+r9] ; disp->ContextRecord + mov rsi,r8 ; context + mov ecx,154 ; sizeof(CONTEXT) + DD 0xa548f3fc ; cld; rep movsq mov rsi,r9 - xor rcx,rcx - mov rdx,QWORD[8+rsi] - mov r8,QWORD[rsi] - mov r9,QWORD[16+rsi] - mov r10,QWORD[40+rsi] - lea r11,[56+rsi] - lea r12,[24+rsi] - mov QWORD[32+rsp],r10 - mov QWORD[40+rsp],r11 - mov QWORD[48+rsp],r12 - mov QWORD[56+rsp],rcx + xor rcx,rcx ; arg1, UNW_FLAG_NHANDLER + mov rdx,QWORD[8+rsi] ; arg2, disp->ImageBase + mov r8,QWORD[rsi] ; arg3, disp->ControlPc + mov r9,QWORD[16+rsi] ; arg4, disp->FunctionEntry + mov r10,QWORD[40+rsi] ; disp->ContextRecord + lea r11,[56+rsi] ; &disp->HandlerData + lea r12,[24+rsi] ; &disp->EstablisherFrame + mov QWORD[32+rsp],r10 ; arg5 + mov QWORD[40+rsp],r11 ; arg6 + mov QWORD[48+rsp],r12 ; arg7 + mov QWORD[56+rsp],rcx ; arg8, (NULL) call QWORD[__imp_RtlVirtualUnwind] - mov eax,1 + mov eax,1 ; ExceptionContinueSearch add rsp,64 popfq pop r15 @@ -3129,11 +3129,11 @@ ALIGN 8 $L$SEH_info_sha512_block_data_order_nohw: DB 9,0,0,0 DD se_handler wrt ..imagebase - DD $L$prologue wrt ..imagebase,$L$epilogue wrt ..imagebase + DD $L$prologue wrt ..imagebase,$L$epilogue wrt ..imagebase ; HandlerData[] $L$SEH_info_sha512_block_data_order_avx: DB 9,0,0,0 DD se_handler wrt ..imagebase - DD $L$prologue_avx wrt ..imagebase,$L$epilogue_avx wrt ..imagebase + DD $L$prologue_avx wrt ..imagebase,$L$epilogue_avx wrt ..imagebase ; HandlerData[] %else ; Work around https://bugzilla.nasm.us/show_bug.cgi?id=3392738 ret diff --git a/third_party/boringssl/gen/bcm/vpaes-armv8-apple.S b/third_party/boringssl/gen/bcm/vpaes-armv8-apple.S index d932f510a..171c29860 100644 --- a/third_party/boringssl/gen/bcm/vpaes-armv8-apple.S +++ b/third_party/boringssl/gen/bcm/vpaes-armv8-apple.S @@ -133,6 +133,7 @@ _vpaes_encrypt_preheat: ## .align 4 +.alt_entry _vpaes_encrypt_core _vpaes_encrypt_core: mov x9, x2 ldr w8, [x2,#240] // pull rounds @@ -205,6 +206,7 @@ Lenc_entry: .private_extern _vpaes_encrypt .align 4 +.alt_entry _vpaes_encrypt _vpaes_encrypt: AARCH64_SIGN_LINK_REGISTER stp x29,x30,[sp,#-16]! @@ -222,6 +224,7 @@ _vpaes_encrypt: .align 4 +.alt_entry _vpaes_encrypt_2x _vpaes_encrypt_2x: mov x9, x2 ldr w8, [x2,#240] // pull rounds @@ -328,6 +331,7 @@ Lenc_2x_entry: .align 4 +.alt_entry _vpaes_decrypt_preheat _vpaes_decrypt_preheat: adrp x10, Lk_inv@PAGE add x10, x10, Lk_inv@PAGEOFF @@ -348,6 +352,7 @@ _vpaes_decrypt_preheat: ## .align 4 +.alt_entry _vpaes_decrypt_core _vpaes_decrypt_core: mov x9, x2 ldr w8, [x2,#240] // pull rounds @@ -444,6 +449,7 @@ Ldec_entry: .private_extern _vpaes_decrypt .align 4 +.alt_entry _vpaes_decrypt _vpaes_decrypt: AARCH64_SIGN_LINK_REGISTER stp x29,x30,[sp,#-16]! @@ -462,6 +468,7 @@ _vpaes_decrypt: // v14-v15 input, v0-v1 output .align 4 +.alt_entry _vpaes_decrypt_2x _vpaes_decrypt_2x: mov x9, x2 ldr w8, [x2,#240] // pull rounds @@ -602,6 +609,7 @@ Ldec_2x_entry: ######################################################## .align 4 +.alt_entry _vpaes_key_preheat _vpaes_key_preheat: adrp x10, Lk_inv@PAGE add x10, x10, Lk_inv@PAGEOFF @@ -624,6 +632,7 @@ _vpaes_key_preheat: .align 4 +.alt_entry _vpaes_schedule_core _vpaes_schedule_core: AARCH64_SIGN_LINK_REGISTER stp x29, x30, [sp,#-16]! @@ -815,6 +824,7 @@ Lschedule_mangle_last_dec: ## .align 4 +.alt_entry _vpaes_schedule_192_smear _vpaes_schedule_192_smear: movi v1.16b, #0 dup v0.4s, v7.s[3] @@ -848,6 +858,7 @@ _vpaes_schedule_192_smear: ## .align 4 +.alt_entry _vpaes_schedule_round _vpaes_schedule_round: // extract rcon from xmm8 movi v4.16b, #0 // vpxor %xmm4, %xmm4, %xmm4 @@ -862,6 +873,7 @@ _vpaes_schedule_round: // fall through... // low round: same as high round, but no rotation and no rcon. +.alt_entry _vpaes_schedule_low_round _vpaes_schedule_low_round: // smear xmm7 ext v1.16b, v4.16b, v7.16b, #12 // vpslldq $4, %xmm7, %xmm1 @@ -904,6 +916,7 @@ _vpaes_schedule_low_round: ## .align 4 +.alt_entry _vpaes_schedule_transform _vpaes_schedule_transform: and v1.16b, v0.16b, v17.16b // vpand %xmm9, %xmm0, %xmm1 ushr v0.16b, v0.16b, #4 // vpsrlb $4, %xmm0, %xmm0 @@ -940,6 +953,7 @@ _vpaes_schedule_transform: ## .align 4 +.alt_entry _vpaes_schedule_mangle _vpaes_schedule_mangle: mov v4.16b, v0.16b // vmovdqa %xmm0, %xmm4 # save xmm0 for later // vmovdqa .Lk_mc_forward(%rip),%xmm5 @@ -1008,6 +1022,7 @@ Lschedule_mangle_both: .private_extern _vpaes_set_encrypt_key .align 4 +.alt_entry _vpaes_set_encrypt_key _vpaes_set_encrypt_key: AARCH64_SIGN_LINK_REGISTER stp x29,x30,[sp,#-16]! @@ -1033,6 +1048,7 @@ _vpaes_set_encrypt_key: .private_extern _vpaes_set_decrypt_key .align 4 +.alt_entry _vpaes_set_decrypt_key _vpaes_set_decrypt_key: AARCH64_SIGN_LINK_REGISTER stp x29,x30,[sp,#-16]! @@ -1061,11 +1077,12 @@ _vpaes_set_decrypt_key: .private_extern _vpaes_cbc_encrypt .align 4 +.alt_entry _vpaes_cbc_encrypt _vpaes_cbc_encrypt: AARCH64_SIGN_LINK_REGISTER cbz x2, Lcbc_abort cmp w5, #0 // check direction - b.eq vpaes_cbc_decrypt + b.eq Lcbc_decrypt stp x29,x30,[sp,#-16]! add x29,sp,#0 @@ -1093,11 +1110,9 @@ Lcbc_abort: AARCH64_VALIDATE_LINK_REGISTER ret - - .align 4 -vpaes_cbc_decrypt: - // Not adding AARCH64_SIGN_LINK_REGISTER here because vpaes_cbc_decrypt is jumped to +Lcbc_decrypt: + // Not adding AARCH64_SIGN_LINK_REGISTER here because .Lcbc_decrypt is jumped to // only from vpaes_cbc_encrypt which has already signed the return address. stp x29,x30,[sp,#-16]! add x29,sp,#0 @@ -1147,6 +1162,7 @@ Lcbc_dec_done: .private_extern _vpaes_ctr32_encrypt_blocks .align 4 +.alt_entry _vpaes_ctr32_encrypt_blocks _vpaes_ctr32_encrypt_blocks: AARCH64_SIGN_LINK_REGISTER stp x29,x30,[sp,#-16]! diff --git a/third_party/boringssl/gen/bcm/vpaes-armv8-linux.S b/third_party/boringssl/gen/bcm/vpaes-armv8-linux.S index 010ccd2a2..d043fc8a7 100644 --- a/third_party/boringssl/gen/bcm/vpaes-armv8-linux.S +++ b/third_party/boringssl/gen/bcm/vpaes-armv8-linux.S @@ -1065,7 +1065,7 @@ vpaes_cbc_encrypt: AARCH64_SIGN_LINK_REGISTER cbz x2, .Lcbc_abort cmp w5, #0 // check direction - b.eq vpaes_cbc_decrypt + b.eq .Lcbc_decrypt stp x29,x30,[sp,#-16]! add x29,sp,#0 @@ -1092,12 +1092,10 @@ vpaes_cbc_encrypt: .Lcbc_abort: AARCH64_VALIDATE_LINK_REGISTER ret -.size vpaes_cbc_encrypt,.-vpaes_cbc_encrypt -.type vpaes_cbc_decrypt,%function .align 4 -vpaes_cbc_decrypt: - // Not adding AARCH64_SIGN_LINK_REGISTER here because vpaes_cbc_decrypt is jumped to +.Lcbc_decrypt: + // Not adding AARCH64_SIGN_LINK_REGISTER here because .Lcbc_decrypt is jumped to // only from vpaes_cbc_encrypt which has already signed the return address. stp x29,x30,[sp,#-16]! add x29,sp,#0 @@ -1142,7 +1140,7 @@ vpaes_cbc_decrypt: ldp x29,x30,[sp],#16 AARCH64_VALIDATE_LINK_REGISTER ret -.size vpaes_cbc_decrypt,.-vpaes_cbc_decrypt +.size vpaes_cbc_encrypt,.-vpaes_cbc_encrypt .globl vpaes_ctr32_encrypt_blocks .hidden vpaes_ctr32_encrypt_blocks .type vpaes_ctr32_encrypt_blocks,%function diff --git a/third_party/boringssl/gen/bcm/vpaes-armv8-win.S b/third_party/boringssl/gen/bcm/vpaes-armv8-win.S index 6160e9f70..762863486 100644 --- a/third_party/boringssl/gen/bcm/vpaes-armv8-win.S +++ b/third_party/boringssl/gen/bcm/vpaes-armv8-win.S @@ -1099,7 +1099,7 @@ vpaes_cbc_encrypt: AARCH64_SIGN_LINK_REGISTER cbz x2, Lcbc_abort cmp w5, #0 // check direction - b.eq vpaes_cbc_decrypt + b.eq Lcbc_decrypt stp x29,x30,[sp,#-16]! add x29,sp,#0 @@ -1127,13 +1127,9 @@ Lcbc_abort: AARCH64_VALIDATE_LINK_REGISTER ret - -.def vpaes_cbc_decrypt - .type 32 -.endef .align 4 -vpaes_cbc_decrypt: - // Not adding AARCH64_SIGN_LINK_REGISTER here because vpaes_cbc_decrypt is jumped to +Lcbc_decrypt: + // Not adding AARCH64_SIGN_LINK_REGISTER here because .Lcbc_decrypt is jumped to // only from vpaes_cbc_encrypt which has already signed the return address. stp x29,x30,[sp,#-16]! add x29,sp,#0 diff --git a/third_party/boringssl/gen/bcm/vpaes-x86_64-apple.S b/third_party/boringssl/gen/bcm/vpaes-x86_64-apple.S index bfcc030fe..38cfd00d0 100644 --- a/third_party/boringssl/gen/bcm/vpaes-x86_64-apple.S +++ b/third_party/boringssl/gen/bcm/vpaes-x86_64-apple.S @@ -6,21 +6,21 @@ #if !defined(OPENSSL_NO_ASM) && defined(OPENSSL_X86_64) && defined(__APPLE__) .text - - - - - - - - - - - - - - - +// +// _aes_encrypt_core +// +// AES-encrypt %xmm0. +// +// Inputs: +// %xmm0 = input +// %xmm9-%xmm15 as in _vpaes_preheat +// (%rdx) = scheduled keys +// +// Output in %xmm0 +// Clobbers %xmm1-%xmm5, %r9, %r10, %r11, %rax +// Preserves %xmm6 - %xmm8 so you get some local vectors +// +// .p2align 4 _vpaes_encrypt_core: @@ -29,13 +29,13 @@ _vpaes_encrypt_core: movq $16,%r11 movl 240(%rdx),%eax movdqa %xmm9,%xmm1 - movdqa L$k_ipt(%rip),%xmm2 + movdqa L$k_ipt(%rip),%xmm2 // iptlo pandn %xmm0,%xmm1 - movdqu (%r9),%xmm5 + movdqu (%r9),%xmm5 // round0 key psrld $4,%xmm1 pand %xmm9,%xmm0 pshufb %xmm0,%xmm2 - movdqa L$k_ipt+16(%rip),%xmm0 + movdqa L$k_ipt+16(%rip),%xmm0 // ipthi pshufb %xmm1,%xmm0 pxor %xmm5,%xmm2 addq $16,%r9 @@ -45,100 +45,101 @@ _vpaes_encrypt_core: .p2align 4 L$enc_loop: - - movdqa %xmm13,%xmm4 - movdqa %xmm12,%xmm0 - pshufb %xmm2,%xmm4 - pshufb %xmm3,%xmm0 - pxor %xmm5,%xmm4 - movdqa %xmm15,%xmm5 - pxor %xmm4,%xmm0 - movdqa -64(%r11,%r10,1),%xmm1 - pshufb %xmm2,%xmm5 - movdqa (%r11,%r10,1),%xmm4 - movdqa %xmm14,%xmm2 - pshufb %xmm3,%xmm2 - movdqa %xmm0,%xmm3 - pxor %xmm5,%xmm2 - pshufb %xmm1,%xmm0 - addq $16,%r9 - pxor %xmm2,%xmm0 - pshufb %xmm4,%xmm3 - addq $16,%r11 - pxor %xmm0,%xmm3 - pshufb %xmm1,%xmm0 - andq $0x30,%r11 - subq $1,%rax - pxor %xmm3,%xmm0 +// middle of middle round + movdqa %xmm13,%xmm4 // 4 : sb1u + movdqa %xmm12,%xmm0 // 0 : sb1t + pshufb %xmm2,%xmm4 // 4 = sb1u + pshufb %xmm3,%xmm0 // 0 = sb1t + pxor %xmm5,%xmm4 // 4 = sb1u + k + movdqa %xmm15,%xmm5 // 4 : sb2u + pxor %xmm4,%xmm0 // 0 = A + movdqa -64(%r11,%r10,1),%xmm1 // .Lk_mc_forward[] + pshufb %xmm2,%xmm5 // 4 = sb2u + movdqa (%r11,%r10,1),%xmm4 // .Lk_mc_backward[] + movdqa %xmm14,%xmm2 // 2 : sb2t + pshufb %xmm3,%xmm2 // 2 = sb2t + movdqa %xmm0,%xmm3 // 3 = A + pxor %xmm5,%xmm2 // 2 = 2A + pshufb %xmm1,%xmm0 // 0 = B + addq $16,%r9 // next key + pxor %xmm2,%xmm0 // 0 = 2A+B + pshufb %xmm4,%xmm3 // 3 = D + addq $16,%r11 // next mc + pxor %xmm0,%xmm3 // 3 = 2A+B+D + pshufb %xmm1,%xmm0 // 0 = 2B+C + andq $0x30,%r11 // ... mod 4 + subq $1,%rax // nr-- + pxor %xmm3,%xmm0 // 0 = 2A+3B+C+D L$enc_entry: - - movdqa %xmm9,%xmm1 - movdqa %xmm11,%xmm5 - pandn %xmm0,%xmm1 - psrld $4,%xmm1 - pand %xmm9,%xmm0 - pshufb %xmm0,%xmm5 - movdqa %xmm10,%xmm3 - pxor %xmm1,%xmm0 - pshufb %xmm1,%xmm3 - movdqa %xmm10,%xmm4 - pxor %xmm5,%xmm3 - pshufb %xmm0,%xmm4 - movdqa %xmm10,%xmm2 - pxor %xmm5,%xmm4 - pshufb %xmm3,%xmm2 - movdqa %xmm10,%xmm3 - pxor %xmm0,%xmm2 - pshufb %xmm4,%xmm3 +// top of round + movdqa %xmm9,%xmm1 // 1 : i + movdqa %xmm11,%xmm5 // 2 : a/k + pandn %xmm0,%xmm1 // 1 = i<<4 + psrld $4,%xmm1 // 1 = i + pand %xmm9,%xmm0 // 0 = k + pshufb %xmm0,%xmm5 // 2 = a/k + movdqa %xmm10,%xmm3 // 3 : 1/i + pxor %xmm1,%xmm0 // 0 = j + pshufb %xmm1,%xmm3 // 3 = 1/i + movdqa %xmm10,%xmm4 // 4 : 1/j + pxor %xmm5,%xmm3 // 3 = iak = 1/i + a/k + pshufb %xmm0,%xmm4 // 4 = 1/j + movdqa %xmm10,%xmm2 // 2 : 1/iak + pxor %xmm5,%xmm4 // 4 = jak = 1/j + a/k + pshufb %xmm3,%xmm2 // 2 = 1/iak + movdqa %xmm10,%xmm3 // 3 : 1/jak + pxor %xmm0,%xmm2 // 2 = io + pshufb %xmm4,%xmm3 // 3 = 1/jak movdqu (%r9),%xmm5 - pxor %xmm1,%xmm3 + pxor %xmm1,%xmm3 // 3 = jo jnz L$enc_loop - - movdqa -96(%r10),%xmm4 - movdqa -80(%r10),%xmm0 - pshufb %xmm2,%xmm4 - pxor %xmm5,%xmm4 - pshufb %xmm3,%xmm0 - movdqa 64(%r11,%r10,1),%xmm1 - pxor %xmm4,%xmm0 +// middle of last round + movdqa -96(%r10),%xmm4 // 3 : sbou .Lk_sbo + movdqa -80(%r10),%xmm0 // 0 : sbot .Lk_sbo+16 + pshufb %xmm2,%xmm4 // 4 = sbou + pxor %xmm5,%xmm4 // 4 = sb1u + k + pshufb %xmm3,%xmm0 // 0 = sb1t + movdqa 64(%r11,%r10,1),%xmm1 // .Lk_sr[] + pxor %xmm4,%xmm0 // 0 = A pshufb %xmm1,%xmm0 ret - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +// +// _aes_encrypt_core_2x +// +// AES-encrypt %xmm0 and %xmm6 in parallel. +// +// Inputs: +// %xmm0 and %xmm6 = input +// %xmm9 and %xmm10 as in _vpaes_preheat +// (%rdx) = scheduled keys +// +// Output in %xmm0 and %xmm6 +// Clobbers %xmm1-%xmm5, %xmm7, %xmm8, %xmm11-%xmm13, %r9, %r10, %r11, %rax +// Preserves %xmm14 and %xmm15 +// +// This function stitches two parallel instances of _vpaes_encrypt_core. x86_64 +// provides 16 XMM registers. _vpaes_encrypt_core computes over six registers +// (%xmm0-%xmm5) and additionally uses seven registers with preloaded constants +// from _vpaes_preheat (%xmm9-%xmm15). This does not quite fit two instances, +// so we spill some of %xmm9 through %xmm15 back to memory. We keep %xmm9 and +// %xmm10 in registers as these values are used several times in a row. The +// remainder are read once per round and are spilled to memory. This leaves two +// registers preserved for the caller. +// +// Thus, of the two _vpaes_encrypt_core instances, the first uses (%xmm0-%xmm5) +// as before. The second uses %xmm6-%xmm8,%xmm11-%xmm13. (Add 6 to %xmm2 and +// below. Add 8 to %xmm3 and up.) Instructions in the second instance are +// indented by one space. +// +// .p2align 4 +.alt_entry _vpaes_encrypt_core_2x _vpaes_encrypt_core_2x: movq %rdx,%r9 @@ -146,19 +147,19 @@ _vpaes_encrypt_core_2x: movl 240(%rdx),%eax movdqa %xmm9,%xmm1 movdqa %xmm9,%xmm7 - movdqa L$k_ipt(%rip),%xmm2 + movdqa L$k_ipt(%rip),%xmm2 // iptlo movdqa %xmm2,%xmm8 pandn %xmm0,%xmm1 pandn %xmm6,%xmm7 - movdqu (%r9),%xmm5 - + movdqu (%r9),%xmm5 // round0 key +// Also use %xmm5 in the second instance. psrld $4,%xmm1 psrld $4,%xmm7 pand %xmm9,%xmm0 pand %xmm9,%xmm6 pshufb %xmm0,%xmm2 pshufb %xmm6,%xmm8 - movdqa L$k_ipt+16(%rip),%xmm0 + movdqa L$k_ipt+16(%rip),%xmm0 // ipthi movdqa %xmm0,%xmm6 pshufb %xmm1,%xmm0 pshufb %xmm7,%xmm6 @@ -172,110 +173,110 @@ _vpaes_encrypt_core_2x: .p2align 4 L$enc2x_loop: - - movdqa L$k_sb1(%rip),%xmm4 - movdqa L$k_sb1+16(%rip),%xmm0 +// middle of middle round + movdqa L$k_sb1(%rip),%xmm4 // 4 : sb1u + movdqa L$k_sb1+16(%rip),%xmm0 // 0 : sb1t movdqa %xmm4,%xmm12 movdqa %xmm0,%xmm6 - pshufb %xmm2,%xmm4 + pshufb %xmm2,%xmm4 // 4 = sb1u pshufb %xmm8,%xmm12 - pshufb %xmm3,%xmm0 + pshufb %xmm3,%xmm0 // 0 = sb1t pshufb %xmm11,%xmm6 - pxor %xmm5,%xmm4 + pxor %xmm5,%xmm4 // 4 = sb1u + k pxor %xmm5,%xmm12 - movdqa L$k_sb2(%rip),%xmm5 + movdqa L$k_sb2(%rip),%xmm5 // 4 : sb2u movdqa %xmm5,%xmm13 - pxor %xmm4,%xmm0 + pxor %xmm4,%xmm0 // 0 = A pxor %xmm12,%xmm6 - movdqa -64(%r11,%r10,1),%xmm1 - - pshufb %xmm2,%xmm5 + movdqa -64(%r11,%r10,1),%xmm1 // .Lk_mc_forward[] +// Also use %xmm1 in the second instance. + pshufb %xmm2,%xmm5 // 4 = sb2u pshufb %xmm8,%xmm13 - movdqa (%r11,%r10,1),%xmm4 - - movdqa L$k_sb2+16(%rip),%xmm2 + movdqa (%r11,%r10,1),%xmm4 // .Lk_mc_backward[] +// Also use %xmm4 in the second instance. + movdqa L$k_sb2+16(%rip),%xmm2 // 2 : sb2t movdqa %xmm2,%xmm8 - pshufb %xmm3,%xmm2 + pshufb %xmm3,%xmm2 // 2 = sb2t pshufb %xmm11,%xmm8 - movdqa %xmm0,%xmm3 + movdqa %xmm0,%xmm3 // 3 = A movdqa %xmm6,%xmm11 - pxor %xmm5,%xmm2 + pxor %xmm5,%xmm2 // 2 = 2A pxor %xmm13,%xmm8 - pshufb %xmm1,%xmm0 + pshufb %xmm1,%xmm0 // 0 = B pshufb %xmm1,%xmm6 - addq $16,%r9 - pxor %xmm2,%xmm0 + addq $16,%r9 // next key + pxor %xmm2,%xmm0 // 0 = 2A+B pxor %xmm8,%xmm6 - pshufb %xmm4,%xmm3 + pshufb %xmm4,%xmm3 // 3 = D pshufb %xmm4,%xmm11 - addq $16,%r11 - pxor %xmm0,%xmm3 + addq $16,%r11 // next mc + pxor %xmm0,%xmm3 // 3 = 2A+B+D pxor %xmm6,%xmm11 - pshufb %xmm1,%xmm0 + pshufb %xmm1,%xmm0 // 0 = 2B+C pshufb %xmm1,%xmm6 - andq $0x30,%r11 - subq $1,%rax - pxor %xmm3,%xmm0 + andq $0x30,%r11 // ... mod 4 + subq $1,%rax // nr-- + pxor %xmm3,%xmm0 // 0 = 2A+3B+C+D pxor %xmm11,%xmm6 L$enc2x_entry: - - movdqa %xmm9,%xmm1 +// top of round + movdqa %xmm9,%xmm1 // 1 : i movdqa %xmm9,%xmm7 - movdqa L$k_inv+16(%rip),%xmm5 + movdqa L$k_inv+16(%rip),%xmm5 // 2 : a/k movdqa %xmm5,%xmm13 - pandn %xmm0,%xmm1 + pandn %xmm0,%xmm1 // 1 = i<<4 pandn %xmm6,%xmm7 - psrld $4,%xmm1 + psrld $4,%xmm1 // 1 = i psrld $4,%xmm7 - pand %xmm9,%xmm0 + pand %xmm9,%xmm0 // 0 = k pand %xmm9,%xmm6 - pshufb %xmm0,%xmm5 + pshufb %xmm0,%xmm5 // 2 = a/k pshufb %xmm6,%xmm13 - movdqa %xmm10,%xmm3 + movdqa %xmm10,%xmm3 // 3 : 1/i movdqa %xmm10,%xmm11 - pxor %xmm1,%xmm0 + pxor %xmm1,%xmm0 // 0 = j pxor %xmm7,%xmm6 - pshufb %xmm1,%xmm3 + pshufb %xmm1,%xmm3 // 3 = 1/i pshufb %xmm7,%xmm11 - movdqa %xmm10,%xmm4 + movdqa %xmm10,%xmm4 // 4 : 1/j movdqa %xmm10,%xmm12 - pxor %xmm5,%xmm3 + pxor %xmm5,%xmm3 // 3 = iak = 1/i + a/k pxor %xmm13,%xmm11 - pshufb %xmm0,%xmm4 + pshufb %xmm0,%xmm4 // 4 = 1/j pshufb %xmm6,%xmm12 - movdqa %xmm10,%xmm2 + movdqa %xmm10,%xmm2 // 2 : 1/iak movdqa %xmm10,%xmm8 - pxor %xmm5,%xmm4 + pxor %xmm5,%xmm4 // 4 = jak = 1/j + a/k pxor %xmm13,%xmm12 - pshufb %xmm3,%xmm2 + pshufb %xmm3,%xmm2 // 2 = 1/iak pshufb %xmm11,%xmm8 - movdqa %xmm10,%xmm3 + movdqa %xmm10,%xmm3 // 3 : 1/jak movdqa %xmm10,%xmm11 - pxor %xmm0,%xmm2 + pxor %xmm0,%xmm2 // 2 = io pxor %xmm6,%xmm8 - pshufb %xmm4,%xmm3 + pshufb %xmm4,%xmm3 // 3 = 1/jak pshufb %xmm12,%xmm11 movdqu (%r9),%xmm5 - - pxor %xmm1,%xmm3 +// Also use %xmm5 in the second instance. + pxor %xmm1,%xmm3 // 3 = jo pxor %xmm7,%xmm11 jnz L$enc2x_loop - - movdqa -96(%r10),%xmm4 - movdqa -80(%r10),%xmm0 +// middle of last round + movdqa -96(%r10),%xmm4 // 3 : sbou .Lk_sbo + movdqa -80(%r10),%xmm0 // 0 : sbot .Lk_sbo+16 movdqa %xmm4,%xmm12 movdqa %xmm0,%xmm6 - pshufb %xmm2,%xmm4 + pshufb %xmm2,%xmm4 // 4 = sbou pshufb %xmm8,%xmm12 - pxor %xmm5,%xmm4 + pxor %xmm5,%xmm4 // 4 = sb1u + k pxor %xmm5,%xmm12 - pshufb %xmm3,%xmm0 + pshufb %xmm3,%xmm0 // 0 = sb1t pshufb %xmm11,%xmm6 - movdqa 64(%r11,%r10,1),%xmm1 - - pxor %xmm4,%xmm0 + movdqa 64(%r11,%r10,1),%xmm1 // .Lk_sr[] +// Also use %xmm1 in the second instance. + pxor %xmm4,%xmm0 // 0 = A pxor %xmm12,%xmm6 pshufb %xmm1,%xmm0 pshufb %xmm1,%xmm6 @@ -283,27 +284,28 @@ L$enc2x_entry: - - - - - +// +// Decryption core +// +// Same API as encryption core. +// .p2align 4 +.alt_entry _vpaes_decrypt_core _vpaes_decrypt_core: - movq %rdx,%r9 + movq %rdx,%r9 // load key movl 240(%rdx),%eax movdqa %xmm9,%xmm1 - movdqa L$k_dipt(%rip),%xmm2 + movdqa L$k_dipt(%rip),%xmm2 // iptlo pandn %xmm0,%xmm1 movq %rax,%r11 psrld $4,%xmm1 - movdqu (%r9),%xmm5 + movdqu (%r9),%xmm5 // round0 key shlq $4,%r11 pand %xmm9,%xmm0 pshufb %xmm0,%xmm2 - movdqa L$k_dipt+16(%rip),%xmm0 + movdqa L$k_dipt+16(%rip),%xmm0 // ipthi xorq $0x30,%r11 leaq L$k_dsbd(%rip),%r10 pshufb %xmm1,%xmm0 @@ -317,99 +319,100 @@ _vpaes_decrypt_core: .p2align 4 L$dec_loop: - - - - movdqa -32(%r10),%xmm4 - movdqa -16(%r10),%xmm1 - pshufb %xmm2,%xmm4 - pshufb %xmm3,%xmm1 +// +// Inverse mix columns +// + movdqa -32(%r10),%xmm4 // 4 : sb9u + movdqa -16(%r10),%xmm1 // 0 : sb9t + pshufb %xmm2,%xmm4 // 4 = sb9u + pshufb %xmm3,%xmm1 // 0 = sb9t pxor %xmm4,%xmm0 - movdqa 0(%r10),%xmm4 - pxor %xmm1,%xmm0 - movdqa 16(%r10),%xmm1 - - pshufb %xmm2,%xmm4 - pshufb %xmm5,%xmm0 - pshufb %xmm3,%xmm1 - pxor %xmm4,%xmm0 - movdqa 32(%r10),%xmm4 - pxor %xmm1,%xmm0 - movdqa 48(%r10),%xmm1 - - pshufb %xmm2,%xmm4 - pshufb %xmm5,%xmm0 - pshufb %xmm3,%xmm1 - pxor %xmm4,%xmm0 - movdqa 64(%r10),%xmm4 - pxor %xmm1,%xmm0 - movdqa 80(%r10),%xmm1 - - pshufb %xmm2,%xmm4 - pshufb %xmm5,%xmm0 - pshufb %xmm3,%xmm1 - pxor %xmm4,%xmm0 - addq $16,%r9 + movdqa 0(%r10),%xmm4 // 4 : sbdu + pxor %xmm1,%xmm0 // 0 = ch + movdqa 16(%r10),%xmm1 // 0 : sbdt + + pshufb %xmm2,%xmm4 // 4 = sbdu + pshufb %xmm5,%xmm0 // MC ch + pshufb %xmm3,%xmm1 // 0 = sbdt + pxor %xmm4,%xmm0 // 4 = ch + movdqa 32(%r10),%xmm4 // 4 : sbbu + pxor %xmm1,%xmm0 // 0 = ch + movdqa 48(%r10),%xmm1 // 0 : sbbt + + pshufb %xmm2,%xmm4 // 4 = sbbu + pshufb %xmm5,%xmm0 // MC ch + pshufb %xmm3,%xmm1 // 0 = sbbt + pxor %xmm4,%xmm0 // 4 = ch + movdqa 64(%r10),%xmm4 // 4 : sbeu + pxor %xmm1,%xmm0 // 0 = ch + movdqa 80(%r10),%xmm1 // 0 : sbet + + pshufb %xmm2,%xmm4 // 4 = sbeu + pshufb %xmm5,%xmm0 // MC ch + pshufb %xmm3,%xmm1 // 0 = sbet + pxor %xmm4,%xmm0 // 4 = ch + addq $16,%r9 // next round key palignr $12,%xmm5,%xmm5 - pxor %xmm1,%xmm0 - subq $1,%rax + pxor %xmm1,%xmm0 // 0 = ch + subq $1,%rax // nr-- L$dec_entry: - - movdqa %xmm9,%xmm1 - pandn %xmm0,%xmm1 - movdqa %xmm11,%xmm2 - psrld $4,%xmm1 - pand %xmm9,%xmm0 - pshufb %xmm0,%xmm2 - movdqa %xmm10,%xmm3 - pxor %xmm1,%xmm0 - pshufb %xmm1,%xmm3 - movdqa %xmm10,%xmm4 - pxor %xmm2,%xmm3 - pshufb %xmm0,%xmm4 - pxor %xmm2,%xmm4 - movdqa %xmm10,%xmm2 - pshufb %xmm3,%xmm2 - movdqa %xmm10,%xmm3 - pxor %xmm0,%xmm2 - pshufb %xmm4,%xmm3 +// top of round + movdqa %xmm9,%xmm1 // 1 : i + pandn %xmm0,%xmm1 // 1 = i<<4 + movdqa %xmm11,%xmm2 // 2 : a/k + psrld $4,%xmm1 // 1 = i + pand %xmm9,%xmm0 // 0 = k + pshufb %xmm0,%xmm2 // 2 = a/k + movdqa %xmm10,%xmm3 // 3 : 1/i + pxor %xmm1,%xmm0 // 0 = j + pshufb %xmm1,%xmm3 // 3 = 1/i + movdqa %xmm10,%xmm4 // 4 : 1/j + pxor %xmm2,%xmm3 // 3 = iak = 1/i + a/k + pshufb %xmm0,%xmm4 // 4 = 1/j + pxor %xmm2,%xmm4 // 4 = jak = 1/j + a/k + movdqa %xmm10,%xmm2 // 2 : 1/iak + pshufb %xmm3,%xmm2 // 2 = 1/iak + movdqa %xmm10,%xmm3 // 3 : 1/jak + pxor %xmm0,%xmm2 // 2 = io + pshufb %xmm4,%xmm3 // 3 = 1/jak movdqu (%r9),%xmm0 - pxor %xmm1,%xmm3 + pxor %xmm1,%xmm3 // 3 = jo jnz L$dec_loop - - movdqa 96(%r10),%xmm4 - pshufb %xmm2,%xmm4 - pxor %xmm0,%xmm4 - movdqa 112(%r10),%xmm0 - movdqa -352(%r11),%xmm2 - pshufb %xmm3,%xmm0 - pxor %xmm4,%xmm0 +// middle of last round + movdqa 96(%r10),%xmm4 // 3 : sbou + pshufb %xmm2,%xmm4 // 4 = sbou + pxor %xmm0,%xmm4 // 4 = sb1u + k + movdqa 112(%r10),%xmm0 // 0 : sbot + movdqa -352(%r11),%xmm2 // .Lk_sr-.Lk_dsbd=-0x160 + pshufb %xmm3,%xmm0 // 0 = sb1t + pxor %xmm4,%xmm0 // 0 = A pshufb %xmm2,%xmm0 ret - - - - - +// ###################################################### +// ## +// AES key schedule ## +// ## +// ###################################################### .p2align 4 +.alt_entry _vpaes_schedule_core _vpaes_schedule_core: +// rdi = key +// rsi = size in bits +// rdx = buffer +// rcx = direction. 0=encrypt, 1=decrypt + call _vpaes_preheat // load the tables + movdqa L$k_rcon(%rip),%xmm8 // load rcon + movdqu (%rdi),%xmm0 // load key (unaligned) - - - - call _vpaes_preheat - movdqa L$k_rcon(%rip),%xmm8 - movdqu (%rdi),%xmm0 - - +// input transform movdqa %xmm0,%xmm3 leaq L$k_ipt(%rip),%r11 call _vpaes_schedule_transform @@ -419,12 +422,12 @@ _vpaes_schedule_core: testq %rcx,%rcx jnz L$schedule_am_decrypting - +// encrypting, output zeroth round key after transform movdqu %xmm0,(%rdx) jmp L$schedule_go L$schedule_am_decrypting: - +// decrypting, output zeroth round key after shiftrows movdqa (%r8,%r10,1),%xmm1 pshufb %xmm1,%xmm3 movdqu %xmm3,(%rdx) @@ -434,16 +437,16 @@ L$schedule_go: cmpl $192,%esi ja L$schedule_256 je L$schedule_192 - - - - - - - - - - +// 128: fall though + +// +// .schedule_128 +// +// 128-bit specific part of key schedule. +// +// This schedule is really simple, because all its parts +// are accomplished by the subroutines. +// L$schedule_128: movl $10,%esi @@ -451,73 +454,73 @@ L$oop_schedule_128: call _vpaes_schedule_round decq %rsi jz L$schedule_mangle_last - call _vpaes_schedule_mangle + call _vpaes_schedule_mangle // write output jmp L$oop_schedule_128 - - - - - - - - - - - - - - - +// +// .aes_schedule_192 +// +// 192-bit specific part of key schedule. +// +// The main body of this schedule is the same as the 128-bit +// schedule, but with more smearing. The long, high side is +// stored in %xmm7 as before, and the short, low side is in +// the high bits of %xmm6. +// +// This schedule is somewhat nastier, however, because each +// round produces 192 bits of key material, or 1.5 round keys. +// Therefore, on each cycle we do 2 rounds and produce 3 round +// keys. +// .p2align 4 L$schedule_192: - movdqu 8(%rdi),%xmm0 - call _vpaes_schedule_transform - movdqa %xmm0,%xmm6 - pxor %xmm4,%xmm4 - movhlps %xmm4,%xmm6 + movdqu 8(%rdi),%xmm0 // load key part 2 (very unaligned) + call _vpaes_schedule_transform // input transform + movdqa %xmm0,%xmm6 // save short part + pxor %xmm4,%xmm4 // clear 4 + movhlps %xmm4,%xmm6 // clobber low side with zeros movl $4,%esi L$oop_schedule_192: call _vpaes_schedule_round palignr $8,%xmm6,%xmm0 - call _vpaes_schedule_mangle + call _vpaes_schedule_mangle // save key n call _vpaes_schedule_192_smear - call _vpaes_schedule_mangle + call _vpaes_schedule_mangle // save key n+1 call _vpaes_schedule_round decq %rsi jz L$schedule_mangle_last - call _vpaes_schedule_mangle + call _vpaes_schedule_mangle // save key n+2 call _vpaes_schedule_192_smear jmp L$oop_schedule_192 - - - - - - - - - - +// +// .aes_schedule_256 +// +// 256-bit specific part of key schedule. +// +// The structure here is very similar to the 128-bit +// schedule, but with an additional "low side" in +// %xmm6. The low side's rounds are the same as the +// high side's, except no rcon and no rotation. +// .p2align 4 L$schedule_256: - movdqu 16(%rdi),%xmm0 - call _vpaes_schedule_transform + movdqu 16(%rdi),%xmm0 // load key part 2 (unaligned) + call _vpaes_schedule_transform // input transform movl $7,%esi L$oop_schedule_256: - call _vpaes_schedule_mangle - movdqa %xmm0,%xmm6 - + call _vpaes_schedule_mangle // output low result + movdqa %xmm0,%xmm6 // save cur_lo in xmm6 +// high round call _vpaes_schedule_round decq %rsi jz L$schedule_mangle_last call _vpaes_schedule_mangle - +// low round. swap xmm7 and xmm6 pshufd $0xFF,%xmm0,%xmm0 movdqa %xmm7,%xmm5 movdqa %xmm6,%xmm7 @@ -527,36 +530,36 @@ L$oop_schedule_256: jmp L$oop_schedule_256 - - - - - - - - - - +// +// .aes_schedule_mangle_last +// +// Mangler for last round of key schedule +// Mangles %xmm0 +// when encrypting, outputs out(%xmm0) ^ 63 +// when decrypting, outputs unskew(%xmm0) +// +// Always called right before return... jumps to cleanup and exits +// .p2align 4 L$schedule_mangle_last: - - leaq L$k_deskew(%rip),%r11 +// schedule last round key from xmm0 + leaq L$k_deskew(%rip),%r11 // prepare to deskew testq %rcx,%rcx jnz L$schedule_mangle_last_dec - +// encrypting movdqa (%r8,%r10,1),%xmm1 - pshufb %xmm1,%xmm0 - leaq L$k_opt(%rip),%r11 + pshufb %xmm1,%xmm0 // output permute + leaq L$k_opt(%rip),%r11 // prepare to output transform addq $32,%rdx L$schedule_mangle_last_dec: addq $-16,%rdx pxor L$k_s63(%rip),%xmm0 - call _vpaes_schedule_transform - movdqu %xmm0,(%rdx) - + call _vpaes_schedule_transform // output transform + movdqu %xmm0,(%rdx) // save last key +// cleanup pxor %xmm0,%xmm0 pxor %xmm1,%xmm1 pxor %xmm2,%xmm2 @@ -569,72 +572,75 @@ L$schedule_mangle_last_dec: - - - - - - - - - - - - - - +// +// .aes_schedule_192_smear +// +// Smear the short, low side in the 192-bit key schedule. +// +// Inputs: +// %xmm7: high side, b a x y +// %xmm6: low side, d c 0 0 +// %xmm13: 0 +// +// Outputs: +// %xmm6: b+c+d b+c 0 0 +// %xmm0: b+c+d b+c b a +// .p2align 4 +.alt_entry _vpaes_schedule_192_smear _vpaes_schedule_192_smear: - pshufd $0x80,%xmm6,%xmm1 - pshufd $0xFE,%xmm7,%xmm0 - pxor %xmm1,%xmm6 + pshufd $0x80,%xmm6,%xmm1 // d c 0 0 -> c 0 0 0 + pshufd $0xFE,%xmm7,%xmm0 // b a _ _ -> b b b a + pxor %xmm1,%xmm6 // -> c+d c 0 0 pxor %xmm1,%xmm1 - pxor %xmm0,%xmm6 + pxor %xmm0,%xmm6 // -> b+c+d b+c b a movdqa %xmm6,%xmm0 - movhlps %xmm1,%xmm6 + movhlps %xmm1,%xmm6 // clobber low side with zeros ret - - - - - - - - - - - - - - - - - - +// +// .aes_schedule_round +// +// Runs one main round of the key schedule on %xmm0, %xmm7 +// +// Specifically, runs subbytes on the high dword of %xmm0 +// then rotates it by one byte and xors into the low dword of +// %xmm7. +// +// Adds rcon from low byte of %xmm8, then rotates %xmm8 for +// next rcon. +// +// Smears the dwords of %xmm7 by xoring the low into the +// second low, result into third, result into highest. +// +// Returns results in %xmm7 = %xmm0. +// Clobbers %xmm1-%xmm4, %r11. +// .p2align 4 +.alt_entry _vpaes_schedule_round _vpaes_schedule_round: - +// extract rcon from xmm8 pxor %xmm1,%xmm1 palignr $15,%xmm8,%xmm1 palignr $15,%xmm8,%xmm8 pxor %xmm1,%xmm7 - +// rotate pshufd $0xFF,%xmm0,%xmm0 palignr $1,%xmm0,%xmm0 +// fall through... - - +// low round: same as high round, but no rotation and no rcon. +.alt_entry _vpaes_schedule_low_round _vpaes_schedule_low_round: - +// smear xmm7 movdqa %xmm7,%xmm1 pslldq $4,%xmm7 pxor %xmm1,%xmm7 @@ -643,98 +649,100 @@ _vpaes_schedule_low_round: pxor %xmm1,%xmm7 pxor L$k_s63(%rip),%xmm7 - +// subbytes movdqa %xmm9,%xmm1 pandn %xmm0,%xmm1 - psrld $4,%xmm1 - pand %xmm9,%xmm0 - movdqa %xmm11,%xmm2 - pshufb %xmm0,%xmm2 - pxor %xmm1,%xmm0 - movdqa %xmm10,%xmm3 - pshufb %xmm1,%xmm3 - pxor %xmm2,%xmm3 - movdqa %xmm10,%xmm4 - pshufb %xmm0,%xmm4 - pxor %xmm2,%xmm4 - movdqa %xmm10,%xmm2 - pshufb %xmm3,%xmm2 - pxor %xmm0,%xmm2 - movdqa %xmm10,%xmm3 - pshufb %xmm4,%xmm3 - pxor %xmm1,%xmm3 - movdqa %xmm13,%xmm4 - pshufb %xmm2,%xmm4 - movdqa %xmm12,%xmm0 - pshufb %xmm3,%xmm0 - pxor %xmm4,%xmm0 - - + psrld $4,%xmm1 // 1 = i + pand %xmm9,%xmm0 // 0 = k + movdqa %xmm11,%xmm2 // 2 : a/k + pshufb %xmm0,%xmm2 // 2 = a/k + pxor %xmm1,%xmm0 // 0 = j + movdqa %xmm10,%xmm3 // 3 : 1/i + pshufb %xmm1,%xmm3 // 3 = 1/i + pxor %xmm2,%xmm3 // 3 = iak = 1/i + a/k + movdqa %xmm10,%xmm4 // 4 : 1/j + pshufb %xmm0,%xmm4 // 4 = 1/j + pxor %xmm2,%xmm4 // 4 = jak = 1/j + a/k + movdqa %xmm10,%xmm2 // 2 : 1/iak + pshufb %xmm3,%xmm2 // 2 = 1/iak + pxor %xmm0,%xmm2 // 2 = io + movdqa %xmm10,%xmm3 // 3 : 1/jak + pshufb %xmm4,%xmm3 // 3 = 1/jak + pxor %xmm1,%xmm3 // 3 = jo + movdqa %xmm13,%xmm4 // 4 : sbou + pshufb %xmm2,%xmm4 // 4 = sbou + movdqa %xmm12,%xmm0 // 0 : sbot + pshufb %xmm3,%xmm0 // 0 = sb1t + pxor %xmm4,%xmm0 // 0 = sbox output + +// add in smeared stuff pxor %xmm7,%xmm0 movdqa %xmm0,%xmm7 ret - - - - - - - - - +// +// .aes_schedule_transform +// +// Linear-transform %xmm0 according to tables at (%r11) +// +// Requires that %xmm9 = 0x0F0F... as in preheat +// Output in %xmm0 +// Clobbers %xmm1, %xmm2 +// .p2align 4 +.alt_entry _vpaes_schedule_transform _vpaes_schedule_transform: movdqa %xmm9,%xmm1 pandn %xmm0,%xmm1 psrld $4,%xmm1 pand %xmm9,%xmm0 - movdqa (%r11),%xmm2 + movdqa (%r11),%xmm2 // lo pshufb %xmm0,%xmm2 - movdqa 16(%r11),%xmm0 + movdqa 16(%r11),%xmm0 // hi pshufb %xmm1,%xmm0 pxor %xmm2,%xmm0 ret - - - - - - - - - - - - - - - - - - - - - - - +// +// .aes_schedule_mangle +// +// Mangle xmm0 from (basis-transformed) standard version +// to our version. +// +// On encrypt, +// xor with 0x63 +// multiply by circulant 0,1,1,1 +// apply shiftrows transform +// +// On decrypt, +// xor with 0x63 +// multiply by "inverse mixcolumns" circulant E,B,D,9 +// deskew +// apply shiftrows transform +// +// +// Writes out to (%rdx), and increments or decrements it +// Keeps track of round number mod 4 in %r8 +// Preserves xmm0 +// Clobbers xmm1-xmm5 +// .p2align 4 +.alt_entry _vpaes_schedule_mangle _vpaes_schedule_mangle: - movdqa %xmm0,%xmm4 + movdqa %xmm0,%xmm4 // save xmm0 for later movdqa L$k_mc_forward(%rip),%xmm5 testq %rcx,%rcx jnz L$schedule_mangle_dec - +// encrypting addq $16,%rdx pxor L$k_s63(%rip),%xmm4 pshufb %xmm5,%xmm4 @@ -747,12 +755,12 @@ _vpaes_schedule_mangle: jmp L$schedule_mangle_both .p2align 4 L$schedule_mangle_dec: - +// inverse mix columns leaq L$k_dksd(%rip),%r11 movdqa %xmm9,%xmm1 pandn %xmm4,%xmm1 - psrld $4,%xmm1 - pand %xmm9,%xmm4 + psrld $4,%xmm1 // 1 = hi + pand %xmm9,%xmm4 // 4 = lo movdqa 0(%r11),%xmm2 pshufb %xmm4,%xmm2 @@ -796,13 +804,14 @@ L$schedule_mangle_both: - - - +// +// Interface to OpenSSL +// .globl _vpaes_set_encrypt_key .private_extern _vpaes_set_encrypt_key .p2align 4 +.alt_entry _vpaes_set_encrypt_key _vpaes_set_encrypt_key: _CET_ENDBR @@ -814,7 +823,7 @@ _CET_ENDBR movl %esi,%eax shrl $5,%eax addl $5,%eax - movl %eax,240(%rdx) + movl %eax,240(%rdx) // AES_KEY->rounds = nbits/32+5; movl $0,%ecx movl $0x30,%r8d @@ -828,13 +837,14 @@ _CET_ENDBR .private_extern _vpaes_set_decrypt_key .p2align 4 +.alt_entry _vpaes_set_decrypt_key _vpaes_set_decrypt_key: _CET_ENDBR movl %esi,%eax shrl $5,%eax addl $5,%eax - movl %eax,240(%rdx) + movl %eax,240(%rdx) // AES_KEY->rounds = nbits/32+5; shll $4,%eax leaq 16(%rdx,%rax,1),%rdx @@ -842,7 +852,7 @@ _CET_ENDBR movl %esi,%r8d shrl $1,%r8d andl $32,%r8d - xorl $32,%r8d + xorl $32,%r8d // nbits==192?0:32 call _vpaes_schedule_core xorl %eax,%eax ret @@ -853,6 +863,7 @@ _CET_ENDBR .private_extern _vpaes_encrypt .p2align 4 +.alt_entry _vpaes_encrypt _vpaes_encrypt: _CET_ENDBR @@ -872,6 +883,7 @@ _CET_ENDBR .private_extern _vpaes_decrypt .p2align 4 +.alt_entry _vpaes_decrypt _vpaes_decrypt: _CET_ENDBR @@ -886,13 +898,14 @@ _CET_ENDBR .private_extern _vpaes_cbc_encrypt .p2align 4 +.alt_entry _vpaes_cbc_encrypt _vpaes_cbc_encrypt: _CET_ENDBR xchgq %rcx,%rdx subq $16,%rcx jc L$cbc_abort - movdqu (%r8),%xmm6 + movdqu (%r8),%xmm6 // load IV subq %rdi,%rsi call _vpaes_preheat cmpl $0,%r9d @@ -921,7 +934,7 @@ L$cbc_dec_loop: subq $16,%rcx jnc L$cbc_dec_loop L$cbc_done: - movdqu %xmm6,(%r8) + movdqu %xmm6,(%r8) // save IV L$cbc_abort: ret @@ -930,16 +943,17 @@ L$cbc_abort: .private_extern _vpaes_ctr32_encrypt_blocks .p2align 4 +.alt_entry _vpaes_ctr32_encrypt_blocks _vpaes_ctr32_encrypt_blocks: _CET_ENDBR - +// _vpaes_encrypt_core and _vpaes_encrypt_core_2x expect the key in %rdx. xchgq %rcx,%rdx testq %rcx,%rcx jz L$ctr32_abort - movdqu (%r8),%xmm0 + movdqu (%r8),%xmm0 // Load IV. movdqa L$ctr_add_one(%rip),%xmm8 - subq %rdi,%rsi + subq %rdi,%rsi // This allows only incrementing %rdi. call _vpaes_preheat movdqa %xmm0,%xmm6 pshufb L$rev_ctr(%rip),%xmm6 @@ -947,9 +961,9 @@ _CET_ENDBR testq $1,%rcx jz L$ctr32_prep_loop - - - movdqu (%rdi),%xmm7 +// Handle one block so the remaining block count is even for +// _vpaes_encrypt_core_2x. + movdqu (%rdi),%xmm7 // Load input. call _vpaes_encrypt_core pxor %xmm7,%xmm0 paddd %xmm8,%xmm6 @@ -959,29 +973,29 @@ _CET_ENDBR jz L$ctr32_done L$ctr32_prep_loop: - - +// _vpaes_encrypt_core_2x leaves only %xmm14 and %xmm15 as spare +// registers. We maintain two byte-swapped counters in them. movdqa %xmm6,%xmm14 movdqa %xmm6,%xmm15 paddd %xmm8,%xmm15 L$ctr32_loop: - movdqa L$rev_ctr(%rip),%xmm1 + movdqa L$rev_ctr(%rip),%xmm1 // Set up counters. movdqa %xmm14,%xmm0 movdqa %xmm15,%xmm6 pshufb %xmm1,%xmm0 pshufb %xmm1,%xmm6 call _vpaes_encrypt_core_2x - movdqu (%rdi),%xmm1 + movdqu (%rdi),%xmm1 // Load input. movdqu 16(%rdi),%xmm2 movdqa L$ctr_add_two(%rip),%xmm3 - pxor %xmm1,%xmm0 + pxor %xmm1,%xmm0 // XOR input. pxor %xmm2,%xmm6 - paddd %xmm3,%xmm14 + paddd %xmm3,%xmm14 // Increment counters. paddd %xmm3,%xmm15 - movdqu %xmm0,(%rsi,%rdi,1) + movdqu %xmm0,(%rsi,%rdi,1) // Write output. movdqu %xmm6,16(%rsi,%rdi,1) - subq $2,%rcx + subq $2,%rcx // Advance loop. leaq 32(%rdi),%rdi jnz L$ctr32_loop @@ -990,135 +1004,136 @@ L$ctr32_abort: ret - - - - - - +// +// _aes_preheat +// +// Fills register %r10 -> .aes_consts (so you can -fPIC) +// and %xmm9-%xmm15 as specified below. +// .p2align 4 +.alt_entry _vpaes_preheat _vpaes_preheat: leaq L$k_s0F(%rip),%r10 - movdqa -32(%r10),%xmm10 - movdqa -16(%r10),%xmm11 - movdqa 0(%r10),%xmm9 - movdqa 48(%r10),%xmm13 - movdqa 64(%r10),%xmm12 - movdqa 80(%r10),%xmm15 - movdqa 96(%r10),%xmm14 + movdqa -32(%r10),%xmm10 // .Lk_inv + movdqa -16(%r10),%xmm11 // .Lk_inv+16 + movdqa 0(%r10),%xmm9 // .Lk_s0F + movdqa 48(%r10),%xmm13 // .Lk_sb1 + movdqa 64(%r10),%xmm12 // .Lk_sb1+16 + movdqa 80(%r10),%xmm15 // .Lk_sb2 + movdqa 96(%r10),%xmm14 // .Lk_sb2+16 ret - - - - - +// ###################################################### +// ## +// Constants ## +// ## +// ###################################################### .section __DATA,__const .p2align 6 _vpaes_consts: -L$k_inv: +L$k_inv: // inv, inva .quad 0x0E05060F0D080180, 0x040703090A0B0C02 .quad 0x01040A060F0B0780, 0x030D0E0C02050809 -L$k_s0F: +L$k_s0F: // s0F .quad 0x0F0F0F0F0F0F0F0F, 0x0F0F0F0F0F0F0F0F -L$k_ipt: +L$k_ipt: // input transform (lo, hi) .quad 0xC2B2E8985A2A7000, 0xCABAE09052227808 .quad 0x4C01307D317C4D00, 0xCD80B1FCB0FDCC81 -L$k_sb1: +L$k_sb1: // sb1u, sb1t .quad 0xB19BE18FCB503E00, 0xA5DF7A6E142AF544 .quad 0x3618D415FAE22300, 0x3BF7CCC10D2ED9EF -L$k_sb2: +L$k_sb2: // sb2u, sb2t .quad 0xE27A93C60B712400, 0x5EB7E955BC982FCD .quad 0x69EB88400AE12900, 0xC2A163C8AB82234A -L$k_sbo: +L$k_sbo: // sbou, sbot .quad 0xD0D26D176FBDC700, 0x15AABF7AC502A878 .quad 0xCFE474A55FBB6A00, 0x8E1E90D1412B35FA -L$k_mc_forward: +L$k_mc_forward: // mc_forward .quad 0x0407060500030201, 0x0C0F0E0D080B0A09 .quad 0x080B0A0904070605, 0x000302010C0F0E0D .quad 0x0C0F0E0D080B0A09, 0x0407060500030201 .quad 0x000302010C0F0E0D, 0x080B0A0904070605 -L$k_mc_backward: +L$k_mc_backward: // mc_backward .quad 0x0605040702010003, 0x0E0D0C0F0A09080B .quad 0x020100030E0D0C0F, 0x0A09080B06050407 .quad 0x0E0D0C0F0A09080B, 0x0605040702010003 .quad 0x0A09080B06050407, 0x020100030E0D0C0F -L$k_sr: +L$k_sr: // sr .quad 0x0706050403020100, 0x0F0E0D0C0B0A0908 .quad 0x030E09040F0A0500, 0x0B06010C07020D08 .quad 0x0F060D040B020900, 0x070E050C030A0108 .quad 0x0B0E0104070A0D00, 0x0306090C0F020508 -L$k_rcon: +L$k_rcon: // rcon .quad 0x1F8391B9AF9DEEB6, 0x702A98084D7C7D81 -L$k_s63: +L$k_s63: // s63: all equal to 0x63 transformed .quad 0x5B5B5B5B5B5B5B5B, 0x5B5B5B5B5B5B5B5B -L$k_opt: +L$k_opt: // output transform .quad 0xFF9F4929D6B66000, 0xF7974121DEBE6808 .quad 0x01EDBD5150BCEC00, 0xE10D5DB1B05C0CE0 -L$k_deskew: +L$k_deskew: // deskew tables: inverts the sbox's "skew" .quad 0x07E4A34047A4E300, 0x1DFEB95A5DBEF91A .quad 0x5F36B5DC83EA6900, 0x2841C2ABF49D1E77 - - - - -L$k_dksd: +// +// Decryption stuff +// Key schedule constants +// +L$k_dksd: // decryption key schedule: invskew x*D .quad 0xFEB91A5DA3E44700, 0x0740E3A45A1DBEF9 .quad 0x41C277F4B5368300, 0x5FDC69EAAB289D1E -L$k_dksb: +L$k_dksb: // decryption key schedule: invskew x*B .quad 0x9A4FCA1F8550D500, 0x03D653861CC94C99 .quad 0x115BEDA7B6FC4A00, 0xD993256F7E3482C8 -L$k_dkse: +L$k_dkse: // decryption key schedule: invskew x*E + 0x63 .quad 0xD5031CCA1FC9D600, 0x53859A4C994F5086 .quad 0xA23196054FDC7BE8, 0xCD5EF96A20B31487 -L$k_dks9: +L$k_dks9: // decryption key schedule: invskew x*9 .quad 0xB6116FC87ED9A700, 0x4AED933482255BFC .quad 0x4576516227143300, 0x8BB89FACE9DAFDCE - - - - -L$k_dipt: +// +// Decryption stuff +// Round function constants +// +L$k_dipt: // decryption input transform .quad 0x0F505B040B545F00, 0x154A411E114E451A .quad 0x86E383E660056500, 0x12771772F491F194 -L$k_dsb9: +L$k_dsb9: // decryption sbox output *9*u, *9*t .quad 0x851C03539A86D600, 0xCAD51F504F994CC9 .quad 0xC03B1789ECD74900, 0x725E2C9EB2FBA565 -L$k_dsbd: +L$k_dsbd: // decryption sbox output *D*u, *D*t .quad 0x7D57CCDFE6B1A200, 0xF56E9B13882A4439 .quad 0x3CE2FAF724C6CB00, 0x2931180D15DEEFD3 -L$k_dsbb: +L$k_dsbb: // decryption sbox output *B*u, *B*t .quad 0xD022649296B44200, 0x602646F6B0F2D404 .quad 0xC19498A6CD596700, 0xF3FF0C3E3255AA6B -L$k_dsbe: +L$k_dsbe: // decryption sbox output *E*u, *E*t .quad 0x46F2929626D4D000, 0x2242600464B4F6B0 .quad 0x0C55A6CDFFAAC100, 0x9467F36B98593E32 -L$k_dsbo: +L$k_dsbo: // decryption sbox final output .quad 0x1387EA537EF94000, 0xC7AA6DB9D4943E2D .quad 0x12D7560F93441D00, 0xCA4B8159D8C58E9C - +// .Lrev_ctr is a permutation which byte-swaps the counter portion of the IV. L$rev_ctr: .quad 0x0706050403020100, 0x0c0d0e0f0b0a0908 - - +// .Lctr_add_* may be added to a byte-swapped xmm register to increment the +// counter. The register must be byte-swapped again to form the actual input. L$ctr_add_one: .quad 0x0000000000000000, 0x0000000100000000 L$ctr_add_two: diff --git a/third_party/boringssl/gen/bcm/vpaes-x86_64-linux.S b/third_party/boringssl/gen/bcm/vpaes-x86_64-linux.S index e7884646b..db1f9fcfc 100644 --- a/third_party/boringssl/gen/bcm/vpaes-x86_64-linux.S +++ b/third_party/boringssl/gen/bcm/vpaes-x86_64-linux.S @@ -6,21 +6,21 @@ #if !defined(OPENSSL_NO_ASM) && defined(OPENSSL_X86_64) && defined(__ELF__) .text - - - - - - - - - - - - - - - +// +// _aes_encrypt_core +// +// AES-encrypt %xmm0. +// +// Inputs: +// %xmm0 = input +// %xmm9-%xmm15 as in _vpaes_preheat +// (%rdx) = scheduled keys +// +// Output in %xmm0 +// Clobbers %xmm1-%xmm5, %r9, %r10, %r11, %rax +// Preserves %xmm6 - %xmm8 so you get some local vectors +// +// .type _vpaes_encrypt_core,@function .align 16 _vpaes_encrypt_core: @@ -29,13 +29,13 @@ _vpaes_encrypt_core: movq $16,%r11 movl 240(%rdx),%eax movdqa %xmm9,%xmm1 - movdqa .Lk_ipt(%rip),%xmm2 + movdqa .Lk_ipt(%rip),%xmm2 // iptlo pandn %xmm0,%xmm1 - movdqu (%r9),%xmm5 + movdqu (%r9),%xmm5 // round0 key psrld $4,%xmm1 pand %xmm9,%xmm0 pshufb %xmm0,%xmm2 - movdqa .Lk_ipt+16(%rip),%xmm0 + movdqa .Lk_ipt+16(%rip),%xmm0 // ipthi pshufb %xmm1,%xmm0 pxor %xmm5,%xmm2 addq $16,%r9 @@ -45,98 +45,98 @@ _vpaes_encrypt_core: .align 16 .Lenc_loop: - - movdqa %xmm13,%xmm4 - movdqa %xmm12,%xmm0 - pshufb %xmm2,%xmm4 - pshufb %xmm3,%xmm0 - pxor %xmm5,%xmm4 - movdqa %xmm15,%xmm5 - pxor %xmm4,%xmm0 - movdqa -64(%r11,%r10,1),%xmm1 - pshufb %xmm2,%xmm5 - movdqa (%r11,%r10,1),%xmm4 - movdqa %xmm14,%xmm2 - pshufb %xmm3,%xmm2 - movdqa %xmm0,%xmm3 - pxor %xmm5,%xmm2 - pshufb %xmm1,%xmm0 - addq $16,%r9 - pxor %xmm2,%xmm0 - pshufb %xmm4,%xmm3 - addq $16,%r11 - pxor %xmm0,%xmm3 - pshufb %xmm1,%xmm0 - andq $0x30,%r11 - subq $1,%rax - pxor %xmm3,%xmm0 +// middle of middle round + movdqa %xmm13,%xmm4 // 4 : sb1u + movdqa %xmm12,%xmm0 // 0 : sb1t + pshufb %xmm2,%xmm4 // 4 = sb1u + pshufb %xmm3,%xmm0 // 0 = sb1t + pxor %xmm5,%xmm4 // 4 = sb1u + k + movdqa %xmm15,%xmm5 // 4 : sb2u + pxor %xmm4,%xmm0 // 0 = A + movdqa -64(%r11,%r10,1),%xmm1 // .Lk_mc_forward[] + pshufb %xmm2,%xmm5 // 4 = sb2u + movdqa (%r11,%r10,1),%xmm4 // .Lk_mc_backward[] + movdqa %xmm14,%xmm2 // 2 : sb2t + pshufb %xmm3,%xmm2 // 2 = sb2t + movdqa %xmm0,%xmm3 // 3 = A + pxor %xmm5,%xmm2 // 2 = 2A + pshufb %xmm1,%xmm0 // 0 = B + addq $16,%r9 // next key + pxor %xmm2,%xmm0 // 0 = 2A+B + pshufb %xmm4,%xmm3 // 3 = D + addq $16,%r11 // next mc + pxor %xmm0,%xmm3 // 3 = 2A+B+D + pshufb %xmm1,%xmm0 // 0 = 2B+C + andq $0x30,%r11 // ... mod 4 + subq $1,%rax // nr-- + pxor %xmm3,%xmm0 // 0 = 2A+3B+C+D .Lenc_entry: - - movdqa %xmm9,%xmm1 - movdqa %xmm11,%xmm5 - pandn %xmm0,%xmm1 - psrld $4,%xmm1 - pand %xmm9,%xmm0 - pshufb %xmm0,%xmm5 - movdqa %xmm10,%xmm3 - pxor %xmm1,%xmm0 - pshufb %xmm1,%xmm3 - movdqa %xmm10,%xmm4 - pxor %xmm5,%xmm3 - pshufb %xmm0,%xmm4 - movdqa %xmm10,%xmm2 - pxor %xmm5,%xmm4 - pshufb %xmm3,%xmm2 - movdqa %xmm10,%xmm3 - pxor %xmm0,%xmm2 - pshufb %xmm4,%xmm3 +// top of round + movdqa %xmm9,%xmm1 // 1 : i + movdqa %xmm11,%xmm5 // 2 : a/k + pandn %xmm0,%xmm1 // 1 = i<<4 + psrld $4,%xmm1 // 1 = i + pand %xmm9,%xmm0 // 0 = k + pshufb %xmm0,%xmm5 // 2 = a/k + movdqa %xmm10,%xmm3 // 3 : 1/i + pxor %xmm1,%xmm0 // 0 = j + pshufb %xmm1,%xmm3 // 3 = 1/i + movdqa %xmm10,%xmm4 // 4 : 1/j + pxor %xmm5,%xmm3 // 3 = iak = 1/i + a/k + pshufb %xmm0,%xmm4 // 4 = 1/j + movdqa %xmm10,%xmm2 // 2 : 1/iak + pxor %xmm5,%xmm4 // 4 = jak = 1/j + a/k + pshufb %xmm3,%xmm2 // 2 = 1/iak + movdqa %xmm10,%xmm3 // 3 : 1/jak + pxor %xmm0,%xmm2 // 2 = io + pshufb %xmm4,%xmm3 // 3 = 1/jak movdqu (%r9),%xmm5 - pxor %xmm1,%xmm3 + pxor %xmm1,%xmm3 // 3 = jo jnz .Lenc_loop - - movdqa -96(%r10),%xmm4 - movdqa -80(%r10),%xmm0 - pshufb %xmm2,%xmm4 - pxor %xmm5,%xmm4 - pshufb %xmm3,%xmm0 - movdqa 64(%r11,%r10,1),%xmm1 - pxor %xmm4,%xmm0 +// middle of last round + movdqa -96(%r10),%xmm4 // 3 : sbou .Lk_sbo + movdqa -80(%r10),%xmm0 // 0 : sbot .Lk_sbo+16 + pshufb %xmm2,%xmm4 // 4 = sbou + pxor %xmm5,%xmm4 // 4 = sb1u + k + pshufb %xmm3,%xmm0 // 0 = sb1t + movdqa 64(%r11,%r10,1),%xmm1 // .Lk_sr[] + pxor %xmm4,%xmm0 // 0 = A pshufb %xmm1,%xmm0 ret .cfi_endproc .size _vpaes_encrypt_core,.-_vpaes_encrypt_core - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +// +// _aes_encrypt_core_2x +// +// AES-encrypt %xmm0 and %xmm6 in parallel. +// +// Inputs: +// %xmm0 and %xmm6 = input +// %xmm9 and %xmm10 as in _vpaes_preheat +// (%rdx) = scheduled keys +// +// Output in %xmm0 and %xmm6 +// Clobbers %xmm1-%xmm5, %xmm7, %xmm8, %xmm11-%xmm13, %r9, %r10, %r11, %rax +// Preserves %xmm14 and %xmm15 +// +// This function stitches two parallel instances of _vpaes_encrypt_core. x86_64 +// provides 16 XMM registers. _vpaes_encrypt_core computes over six registers +// (%xmm0-%xmm5) and additionally uses seven registers with preloaded constants +// from _vpaes_preheat (%xmm9-%xmm15). This does not quite fit two instances, +// so we spill some of %xmm9 through %xmm15 back to memory. We keep %xmm9 and +// %xmm10 in registers as these values are used several times in a row. The +// remainder are read once per round and are spilled to memory. This leaves two +// registers preserved for the caller. +// +// Thus, of the two _vpaes_encrypt_core instances, the first uses (%xmm0-%xmm5) +// as before. The second uses %xmm6-%xmm8,%xmm11-%xmm13. (Add 6 to %xmm2 and +// below. Add 8 to %xmm3 and up.) Instructions in the second instance are +// indented by one space. +// +// .type _vpaes_encrypt_core_2x,@function .align 16 _vpaes_encrypt_core_2x: @@ -146,19 +146,19 @@ _vpaes_encrypt_core_2x: movl 240(%rdx),%eax movdqa %xmm9,%xmm1 movdqa %xmm9,%xmm7 - movdqa .Lk_ipt(%rip),%xmm2 + movdqa .Lk_ipt(%rip),%xmm2 // iptlo movdqa %xmm2,%xmm8 pandn %xmm0,%xmm1 pandn %xmm6,%xmm7 - movdqu (%r9),%xmm5 - + movdqu (%r9),%xmm5 // round0 key +// Also use %xmm5 in the second instance. psrld $4,%xmm1 psrld $4,%xmm7 pand %xmm9,%xmm0 pand %xmm9,%xmm6 pshufb %xmm0,%xmm2 pshufb %xmm6,%xmm8 - movdqa .Lk_ipt+16(%rip),%xmm0 + movdqa .Lk_ipt+16(%rip),%xmm0 // ipthi movdqa %xmm0,%xmm6 pshufb %xmm1,%xmm0 pshufb %xmm7,%xmm6 @@ -172,110 +172,110 @@ _vpaes_encrypt_core_2x: .align 16 .Lenc2x_loop: - - movdqa .Lk_sb1(%rip),%xmm4 - movdqa .Lk_sb1+16(%rip),%xmm0 +// middle of middle round + movdqa .Lk_sb1(%rip),%xmm4 // 4 : sb1u + movdqa .Lk_sb1+16(%rip),%xmm0 // 0 : sb1t movdqa %xmm4,%xmm12 movdqa %xmm0,%xmm6 - pshufb %xmm2,%xmm4 + pshufb %xmm2,%xmm4 // 4 = sb1u pshufb %xmm8,%xmm12 - pshufb %xmm3,%xmm0 + pshufb %xmm3,%xmm0 // 0 = sb1t pshufb %xmm11,%xmm6 - pxor %xmm5,%xmm4 + pxor %xmm5,%xmm4 // 4 = sb1u + k pxor %xmm5,%xmm12 - movdqa .Lk_sb2(%rip),%xmm5 + movdqa .Lk_sb2(%rip),%xmm5 // 4 : sb2u movdqa %xmm5,%xmm13 - pxor %xmm4,%xmm0 + pxor %xmm4,%xmm0 // 0 = A pxor %xmm12,%xmm6 - movdqa -64(%r11,%r10,1),%xmm1 - - pshufb %xmm2,%xmm5 + movdqa -64(%r11,%r10,1),%xmm1 // .Lk_mc_forward[] +// Also use %xmm1 in the second instance. + pshufb %xmm2,%xmm5 // 4 = sb2u pshufb %xmm8,%xmm13 - movdqa (%r11,%r10,1),%xmm4 - - movdqa .Lk_sb2+16(%rip),%xmm2 + movdqa (%r11,%r10,1),%xmm4 // .Lk_mc_backward[] +// Also use %xmm4 in the second instance. + movdqa .Lk_sb2+16(%rip),%xmm2 // 2 : sb2t movdqa %xmm2,%xmm8 - pshufb %xmm3,%xmm2 + pshufb %xmm3,%xmm2 // 2 = sb2t pshufb %xmm11,%xmm8 - movdqa %xmm0,%xmm3 + movdqa %xmm0,%xmm3 // 3 = A movdqa %xmm6,%xmm11 - pxor %xmm5,%xmm2 + pxor %xmm5,%xmm2 // 2 = 2A pxor %xmm13,%xmm8 - pshufb %xmm1,%xmm0 + pshufb %xmm1,%xmm0 // 0 = B pshufb %xmm1,%xmm6 - addq $16,%r9 - pxor %xmm2,%xmm0 + addq $16,%r9 // next key + pxor %xmm2,%xmm0 // 0 = 2A+B pxor %xmm8,%xmm6 - pshufb %xmm4,%xmm3 + pshufb %xmm4,%xmm3 // 3 = D pshufb %xmm4,%xmm11 - addq $16,%r11 - pxor %xmm0,%xmm3 + addq $16,%r11 // next mc + pxor %xmm0,%xmm3 // 3 = 2A+B+D pxor %xmm6,%xmm11 - pshufb %xmm1,%xmm0 + pshufb %xmm1,%xmm0 // 0 = 2B+C pshufb %xmm1,%xmm6 - andq $0x30,%r11 - subq $1,%rax - pxor %xmm3,%xmm0 + andq $0x30,%r11 // ... mod 4 + subq $1,%rax // nr-- + pxor %xmm3,%xmm0 // 0 = 2A+3B+C+D pxor %xmm11,%xmm6 .Lenc2x_entry: - - movdqa %xmm9,%xmm1 +// top of round + movdqa %xmm9,%xmm1 // 1 : i movdqa %xmm9,%xmm7 - movdqa .Lk_inv+16(%rip),%xmm5 + movdqa .Lk_inv+16(%rip),%xmm5 // 2 : a/k movdqa %xmm5,%xmm13 - pandn %xmm0,%xmm1 + pandn %xmm0,%xmm1 // 1 = i<<4 pandn %xmm6,%xmm7 - psrld $4,%xmm1 + psrld $4,%xmm1 // 1 = i psrld $4,%xmm7 - pand %xmm9,%xmm0 + pand %xmm9,%xmm0 // 0 = k pand %xmm9,%xmm6 - pshufb %xmm0,%xmm5 + pshufb %xmm0,%xmm5 // 2 = a/k pshufb %xmm6,%xmm13 - movdqa %xmm10,%xmm3 + movdqa %xmm10,%xmm3 // 3 : 1/i movdqa %xmm10,%xmm11 - pxor %xmm1,%xmm0 + pxor %xmm1,%xmm0 // 0 = j pxor %xmm7,%xmm6 - pshufb %xmm1,%xmm3 + pshufb %xmm1,%xmm3 // 3 = 1/i pshufb %xmm7,%xmm11 - movdqa %xmm10,%xmm4 + movdqa %xmm10,%xmm4 // 4 : 1/j movdqa %xmm10,%xmm12 - pxor %xmm5,%xmm3 + pxor %xmm5,%xmm3 // 3 = iak = 1/i + a/k pxor %xmm13,%xmm11 - pshufb %xmm0,%xmm4 + pshufb %xmm0,%xmm4 // 4 = 1/j pshufb %xmm6,%xmm12 - movdqa %xmm10,%xmm2 + movdqa %xmm10,%xmm2 // 2 : 1/iak movdqa %xmm10,%xmm8 - pxor %xmm5,%xmm4 + pxor %xmm5,%xmm4 // 4 = jak = 1/j + a/k pxor %xmm13,%xmm12 - pshufb %xmm3,%xmm2 + pshufb %xmm3,%xmm2 // 2 = 1/iak pshufb %xmm11,%xmm8 - movdqa %xmm10,%xmm3 + movdqa %xmm10,%xmm3 // 3 : 1/jak movdqa %xmm10,%xmm11 - pxor %xmm0,%xmm2 + pxor %xmm0,%xmm2 // 2 = io pxor %xmm6,%xmm8 - pshufb %xmm4,%xmm3 + pshufb %xmm4,%xmm3 // 3 = 1/jak pshufb %xmm12,%xmm11 movdqu (%r9),%xmm5 - - pxor %xmm1,%xmm3 +// Also use %xmm5 in the second instance. + pxor %xmm1,%xmm3 // 3 = jo pxor %xmm7,%xmm11 jnz .Lenc2x_loop - - movdqa -96(%r10),%xmm4 - movdqa -80(%r10),%xmm0 +// middle of last round + movdqa -96(%r10),%xmm4 // 3 : sbou .Lk_sbo + movdqa -80(%r10),%xmm0 // 0 : sbot .Lk_sbo+16 movdqa %xmm4,%xmm12 movdqa %xmm0,%xmm6 - pshufb %xmm2,%xmm4 + pshufb %xmm2,%xmm4 // 4 = sbou pshufb %xmm8,%xmm12 - pxor %xmm5,%xmm4 + pxor %xmm5,%xmm4 // 4 = sb1u + k pxor %xmm5,%xmm12 - pshufb %xmm3,%xmm0 + pshufb %xmm3,%xmm0 // 0 = sb1t pshufb %xmm11,%xmm6 - movdqa 64(%r11,%r10,1),%xmm1 - - pxor %xmm4,%xmm0 + movdqa 64(%r11,%r10,1),%xmm1 // .Lk_sr[] +// Also use %xmm1 in the second instance. + pxor %xmm4,%xmm0 // 0 = A pxor %xmm12,%xmm6 pshufb %xmm1,%xmm0 pshufb %xmm1,%xmm6 @@ -283,27 +283,27 @@ _vpaes_encrypt_core_2x: .cfi_endproc .size _vpaes_encrypt_core_2x,.-_vpaes_encrypt_core_2x - - - - - +// +// Decryption core +// +// Same API as encryption core. +// .type _vpaes_decrypt_core,@function .align 16 _vpaes_decrypt_core: .cfi_startproc - movq %rdx,%r9 + movq %rdx,%r9 // load key movl 240(%rdx),%eax movdqa %xmm9,%xmm1 - movdqa .Lk_dipt(%rip),%xmm2 + movdqa .Lk_dipt(%rip),%xmm2 // iptlo pandn %xmm0,%xmm1 movq %rax,%r11 psrld $4,%xmm1 - movdqu (%r9),%xmm5 + movdqu (%r9),%xmm5 // round0 key shlq $4,%r11 pand %xmm9,%xmm0 pshufb %xmm0,%xmm2 - movdqa .Lk_dipt+16(%rip),%xmm0 + movdqa .Lk_dipt+16(%rip),%xmm0 // ipthi xorq $0x30,%r11 leaq .Lk_dsbd(%rip),%r10 pshufb %xmm1,%xmm0 @@ -317,99 +317,99 @@ _vpaes_decrypt_core: .align 16 .Ldec_loop: - - - - movdqa -32(%r10),%xmm4 - movdqa -16(%r10),%xmm1 - pshufb %xmm2,%xmm4 - pshufb %xmm3,%xmm1 +// +// Inverse mix columns +// + movdqa -32(%r10),%xmm4 // 4 : sb9u + movdqa -16(%r10),%xmm1 // 0 : sb9t + pshufb %xmm2,%xmm4 // 4 = sb9u + pshufb %xmm3,%xmm1 // 0 = sb9t pxor %xmm4,%xmm0 - movdqa 0(%r10),%xmm4 - pxor %xmm1,%xmm0 - movdqa 16(%r10),%xmm1 - - pshufb %xmm2,%xmm4 - pshufb %xmm5,%xmm0 - pshufb %xmm3,%xmm1 - pxor %xmm4,%xmm0 - movdqa 32(%r10),%xmm4 - pxor %xmm1,%xmm0 - movdqa 48(%r10),%xmm1 - - pshufb %xmm2,%xmm4 - pshufb %xmm5,%xmm0 - pshufb %xmm3,%xmm1 - pxor %xmm4,%xmm0 - movdqa 64(%r10),%xmm4 - pxor %xmm1,%xmm0 - movdqa 80(%r10),%xmm1 - - pshufb %xmm2,%xmm4 - pshufb %xmm5,%xmm0 - pshufb %xmm3,%xmm1 - pxor %xmm4,%xmm0 - addq $16,%r9 + movdqa 0(%r10),%xmm4 // 4 : sbdu + pxor %xmm1,%xmm0 // 0 = ch + movdqa 16(%r10),%xmm1 // 0 : sbdt + + pshufb %xmm2,%xmm4 // 4 = sbdu + pshufb %xmm5,%xmm0 // MC ch + pshufb %xmm3,%xmm1 // 0 = sbdt + pxor %xmm4,%xmm0 // 4 = ch + movdqa 32(%r10),%xmm4 // 4 : sbbu + pxor %xmm1,%xmm0 // 0 = ch + movdqa 48(%r10),%xmm1 // 0 : sbbt + + pshufb %xmm2,%xmm4 // 4 = sbbu + pshufb %xmm5,%xmm0 // MC ch + pshufb %xmm3,%xmm1 // 0 = sbbt + pxor %xmm4,%xmm0 // 4 = ch + movdqa 64(%r10),%xmm4 // 4 : sbeu + pxor %xmm1,%xmm0 // 0 = ch + movdqa 80(%r10),%xmm1 // 0 : sbet + + pshufb %xmm2,%xmm4 // 4 = sbeu + pshufb %xmm5,%xmm0 // MC ch + pshufb %xmm3,%xmm1 // 0 = sbet + pxor %xmm4,%xmm0 // 4 = ch + addq $16,%r9 // next round key palignr $12,%xmm5,%xmm5 - pxor %xmm1,%xmm0 - subq $1,%rax + pxor %xmm1,%xmm0 // 0 = ch + subq $1,%rax // nr-- .Ldec_entry: - - movdqa %xmm9,%xmm1 - pandn %xmm0,%xmm1 - movdqa %xmm11,%xmm2 - psrld $4,%xmm1 - pand %xmm9,%xmm0 - pshufb %xmm0,%xmm2 - movdqa %xmm10,%xmm3 - pxor %xmm1,%xmm0 - pshufb %xmm1,%xmm3 - movdqa %xmm10,%xmm4 - pxor %xmm2,%xmm3 - pshufb %xmm0,%xmm4 - pxor %xmm2,%xmm4 - movdqa %xmm10,%xmm2 - pshufb %xmm3,%xmm2 - movdqa %xmm10,%xmm3 - pxor %xmm0,%xmm2 - pshufb %xmm4,%xmm3 +// top of round + movdqa %xmm9,%xmm1 // 1 : i + pandn %xmm0,%xmm1 // 1 = i<<4 + movdqa %xmm11,%xmm2 // 2 : a/k + psrld $4,%xmm1 // 1 = i + pand %xmm9,%xmm0 // 0 = k + pshufb %xmm0,%xmm2 // 2 = a/k + movdqa %xmm10,%xmm3 // 3 : 1/i + pxor %xmm1,%xmm0 // 0 = j + pshufb %xmm1,%xmm3 // 3 = 1/i + movdqa %xmm10,%xmm4 // 4 : 1/j + pxor %xmm2,%xmm3 // 3 = iak = 1/i + a/k + pshufb %xmm0,%xmm4 // 4 = 1/j + pxor %xmm2,%xmm4 // 4 = jak = 1/j + a/k + movdqa %xmm10,%xmm2 // 2 : 1/iak + pshufb %xmm3,%xmm2 // 2 = 1/iak + movdqa %xmm10,%xmm3 // 3 : 1/jak + pxor %xmm0,%xmm2 // 2 = io + pshufb %xmm4,%xmm3 // 3 = 1/jak movdqu (%r9),%xmm0 - pxor %xmm1,%xmm3 + pxor %xmm1,%xmm3 // 3 = jo jnz .Ldec_loop - - movdqa 96(%r10),%xmm4 - pshufb %xmm2,%xmm4 - pxor %xmm0,%xmm4 - movdqa 112(%r10),%xmm0 - movdqa -352(%r11),%xmm2 - pshufb %xmm3,%xmm0 - pxor %xmm4,%xmm0 +// middle of last round + movdqa 96(%r10),%xmm4 // 3 : sbou + pshufb %xmm2,%xmm4 // 4 = sbou + pxor %xmm0,%xmm4 // 4 = sb1u + k + movdqa 112(%r10),%xmm0 // 0 : sbot + movdqa -352(%r11),%xmm2 // .Lk_sr-.Lk_dsbd=-0x160 + pshufb %xmm3,%xmm0 // 0 = sb1t + pxor %xmm4,%xmm0 // 0 = A pshufb %xmm2,%xmm0 ret .cfi_endproc .size _vpaes_decrypt_core,.-_vpaes_decrypt_core - - - - - +// ###################################################### +// ## +// AES key schedule ## +// ## +// ###################################################### .type _vpaes_schedule_core,@function .align 16 _vpaes_schedule_core: .cfi_startproc +// rdi = key +// rsi = size in bits +// rdx = buffer +// rcx = direction. 0=encrypt, 1=decrypt + call _vpaes_preheat // load the tables + movdqa .Lk_rcon(%rip),%xmm8 // load rcon + movdqu (%rdi),%xmm0 // load key (unaligned) - - - - call _vpaes_preheat - movdqa .Lk_rcon(%rip),%xmm8 - movdqu (%rdi),%xmm0 - - +// input transform movdqa %xmm0,%xmm3 leaq .Lk_ipt(%rip),%r11 call _vpaes_schedule_transform @@ -419,12 +419,12 @@ _vpaes_schedule_core: testq %rcx,%rcx jnz .Lschedule_am_decrypting - +// encrypting, output zeroth round key after transform movdqu %xmm0,(%rdx) jmp .Lschedule_go .Lschedule_am_decrypting: - +// decrypting, output zeroth round key after shiftrows movdqa (%r8,%r10,1),%xmm1 pshufb %xmm1,%xmm3 movdqu %xmm3,(%rdx) @@ -434,16 +434,16 @@ _vpaes_schedule_core: cmpl $192,%esi ja .Lschedule_256 je .Lschedule_192 - - - - - - - - - - +// 128: fall though + +// +// .schedule_128 +// +// 128-bit specific part of key schedule. +// +// This schedule is really simple, because all its parts +// are accomplished by the subroutines. +// .Lschedule_128: movl $10,%esi @@ -451,73 +451,73 @@ _vpaes_schedule_core: call _vpaes_schedule_round decq %rsi jz .Lschedule_mangle_last - call _vpaes_schedule_mangle + call _vpaes_schedule_mangle // write output jmp .Loop_schedule_128 - - - - - - - - - - - - - - - +// +// .aes_schedule_192 +// +// 192-bit specific part of key schedule. +// +// The main body of this schedule is the same as the 128-bit +// schedule, but with more smearing. The long, high side is +// stored in %xmm7 as before, and the short, low side is in +// the high bits of %xmm6. +// +// This schedule is somewhat nastier, however, because each +// round produces 192 bits of key material, or 1.5 round keys. +// Therefore, on each cycle we do 2 rounds and produce 3 round +// keys. +// .align 16 .Lschedule_192: - movdqu 8(%rdi),%xmm0 - call _vpaes_schedule_transform - movdqa %xmm0,%xmm6 - pxor %xmm4,%xmm4 - movhlps %xmm4,%xmm6 + movdqu 8(%rdi),%xmm0 // load key part 2 (very unaligned) + call _vpaes_schedule_transform // input transform + movdqa %xmm0,%xmm6 // save short part + pxor %xmm4,%xmm4 // clear 4 + movhlps %xmm4,%xmm6 // clobber low side with zeros movl $4,%esi .Loop_schedule_192: call _vpaes_schedule_round palignr $8,%xmm6,%xmm0 - call _vpaes_schedule_mangle + call _vpaes_schedule_mangle // save key n call _vpaes_schedule_192_smear - call _vpaes_schedule_mangle + call _vpaes_schedule_mangle // save key n+1 call _vpaes_schedule_round decq %rsi jz .Lschedule_mangle_last - call _vpaes_schedule_mangle + call _vpaes_schedule_mangle // save key n+2 call _vpaes_schedule_192_smear jmp .Loop_schedule_192 - - - - - - - - - - +// +// .aes_schedule_256 +// +// 256-bit specific part of key schedule. +// +// The structure here is very similar to the 128-bit +// schedule, but with an additional "low side" in +// %xmm6. The low side's rounds are the same as the +// high side's, except no rcon and no rotation. +// .align 16 .Lschedule_256: - movdqu 16(%rdi),%xmm0 - call _vpaes_schedule_transform + movdqu 16(%rdi),%xmm0 // load key part 2 (unaligned) + call _vpaes_schedule_transform // input transform movl $7,%esi .Loop_schedule_256: - call _vpaes_schedule_mangle - movdqa %xmm0,%xmm6 - + call _vpaes_schedule_mangle // output low result + movdqa %xmm0,%xmm6 // save cur_lo in xmm6 +// high round call _vpaes_schedule_round decq %rsi jz .Lschedule_mangle_last call _vpaes_schedule_mangle - +// low round. swap xmm7 and xmm6 pshufd $0xFF,%xmm0,%xmm0 movdqa %xmm7,%xmm5 movdqa %xmm6,%xmm7 @@ -527,36 +527,36 @@ _vpaes_schedule_core: jmp .Loop_schedule_256 - - - - - - - - - - +// +// .aes_schedule_mangle_last +// +// Mangler for last round of key schedule +// Mangles %xmm0 +// when encrypting, outputs out(%xmm0) ^ 63 +// when decrypting, outputs unskew(%xmm0) +// +// Always called right before return... jumps to cleanup and exits +// .align 16 .Lschedule_mangle_last: - - leaq .Lk_deskew(%rip),%r11 +// schedule last round key from xmm0 + leaq .Lk_deskew(%rip),%r11 // prepare to deskew testq %rcx,%rcx jnz .Lschedule_mangle_last_dec - +// encrypting movdqa (%r8,%r10,1),%xmm1 - pshufb %xmm1,%xmm0 - leaq .Lk_opt(%rip),%r11 + pshufb %xmm1,%xmm0 // output permute + leaq .Lk_opt(%rip),%r11 // prepare to output transform addq $32,%rdx .Lschedule_mangle_last_dec: addq $-16,%rdx pxor .Lk_s63(%rip),%xmm0 - call _vpaes_schedule_transform - movdqu %xmm0,(%rdx) - + call _vpaes_schedule_transform // output transform + movdqu %xmm0,(%rdx) // save last key +// cleanup pxor %xmm0,%xmm0 pxor %xmm1,%xmm1 pxor %xmm2,%xmm2 @@ -569,72 +569,72 @@ _vpaes_schedule_core: .cfi_endproc .size _vpaes_schedule_core,.-_vpaes_schedule_core - - - - - - - - - - - - - - +// +// .aes_schedule_192_smear +// +// Smear the short, low side in the 192-bit key schedule. +// +// Inputs: +// %xmm7: high side, b a x y +// %xmm6: low side, d c 0 0 +// %xmm13: 0 +// +// Outputs: +// %xmm6: b+c+d b+c 0 0 +// %xmm0: b+c+d b+c b a +// .type _vpaes_schedule_192_smear,@function .align 16 _vpaes_schedule_192_smear: .cfi_startproc - pshufd $0x80,%xmm6,%xmm1 - pshufd $0xFE,%xmm7,%xmm0 - pxor %xmm1,%xmm6 + pshufd $0x80,%xmm6,%xmm1 // d c 0 0 -> c 0 0 0 + pshufd $0xFE,%xmm7,%xmm0 // b a _ _ -> b b b a + pxor %xmm1,%xmm6 // -> c+d c 0 0 pxor %xmm1,%xmm1 - pxor %xmm0,%xmm6 + pxor %xmm0,%xmm6 // -> b+c+d b+c b a movdqa %xmm6,%xmm0 - movhlps %xmm1,%xmm6 + movhlps %xmm1,%xmm6 // clobber low side with zeros ret .cfi_endproc .size _vpaes_schedule_192_smear,.-_vpaes_schedule_192_smear - - - - - - - - - - - - - - - - - - +// +// .aes_schedule_round +// +// Runs one main round of the key schedule on %xmm0, %xmm7 +// +// Specifically, runs subbytes on the high dword of %xmm0 +// then rotates it by one byte and xors into the low dword of +// %xmm7. +// +// Adds rcon from low byte of %xmm8, then rotates %xmm8 for +// next rcon. +// +// Smears the dwords of %xmm7 by xoring the low into the +// second low, result into third, result into highest. +// +// Returns results in %xmm7 = %xmm0. +// Clobbers %xmm1-%xmm4, %r11. +// .type _vpaes_schedule_round,@function .align 16 _vpaes_schedule_round: .cfi_startproc - +// extract rcon from xmm8 pxor %xmm1,%xmm1 palignr $15,%xmm8,%xmm1 palignr $15,%xmm8,%xmm8 pxor %xmm1,%xmm7 - +// rotate pshufd $0xFF,%xmm0,%xmm0 palignr $1,%xmm0,%xmm0 +// fall through... - - +// low round: same as high round, but no rotation and no rcon. _vpaes_schedule_low_round: - +// smear xmm7 movdqa %xmm7,%xmm1 pslldq $4,%xmm7 pxor %xmm1,%xmm7 @@ -643,48 +643,48 @@ _vpaes_schedule_low_round: pxor %xmm1,%xmm7 pxor .Lk_s63(%rip),%xmm7 - +// subbytes movdqa %xmm9,%xmm1 pandn %xmm0,%xmm1 - psrld $4,%xmm1 - pand %xmm9,%xmm0 - movdqa %xmm11,%xmm2 - pshufb %xmm0,%xmm2 - pxor %xmm1,%xmm0 - movdqa %xmm10,%xmm3 - pshufb %xmm1,%xmm3 - pxor %xmm2,%xmm3 - movdqa %xmm10,%xmm4 - pshufb %xmm0,%xmm4 - pxor %xmm2,%xmm4 - movdqa %xmm10,%xmm2 - pshufb %xmm3,%xmm2 - pxor %xmm0,%xmm2 - movdqa %xmm10,%xmm3 - pshufb %xmm4,%xmm3 - pxor %xmm1,%xmm3 - movdqa %xmm13,%xmm4 - pshufb %xmm2,%xmm4 - movdqa %xmm12,%xmm0 - pshufb %xmm3,%xmm0 - pxor %xmm4,%xmm0 - - + psrld $4,%xmm1 // 1 = i + pand %xmm9,%xmm0 // 0 = k + movdqa %xmm11,%xmm2 // 2 : a/k + pshufb %xmm0,%xmm2 // 2 = a/k + pxor %xmm1,%xmm0 // 0 = j + movdqa %xmm10,%xmm3 // 3 : 1/i + pshufb %xmm1,%xmm3 // 3 = 1/i + pxor %xmm2,%xmm3 // 3 = iak = 1/i + a/k + movdqa %xmm10,%xmm4 // 4 : 1/j + pshufb %xmm0,%xmm4 // 4 = 1/j + pxor %xmm2,%xmm4 // 4 = jak = 1/j + a/k + movdqa %xmm10,%xmm2 // 2 : 1/iak + pshufb %xmm3,%xmm2 // 2 = 1/iak + pxor %xmm0,%xmm2 // 2 = io + movdqa %xmm10,%xmm3 // 3 : 1/jak + pshufb %xmm4,%xmm3 // 3 = 1/jak + pxor %xmm1,%xmm3 // 3 = jo + movdqa %xmm13,%xmm4 // 4 : sbou + pshufb %xmm2,%xmm4 // 4 = sbou + movdqa %xmm12,%xmm0 // 0 : sbot + pshufb %xmm3,%xmm0 // 0 = sb1t + pxor %xmm4,%xmm0 // 0 = sbox output + +// add in smeared stuff pxor %xmm7,%xmm0 movdqa %xmm0,%xmm7 ret .cfi_endproc .size _vpaes_schedule_round,.-_vpaes_schedule_round - - - - - - - - - +// +// .aes_schedule_transform +// +// Linear-transform %xmm0 according to tables at (%r11) +// +// Requires that %xmm9 = 0x0F0F... as in preheat +// Output in %xmm0 +// Clobbers %xmm1, %xmm2 +// .type _vpaes_schedule_transform,@function .align 16 _vpaes_schedule_transform: @@ -693,48 +693,48 @@ _vpaes_schedule_transform: pandn %xmm0,%xmm1 psrld $4,%xmm1 pand %xmm9,%xmm0 - movdqa (%r11),%xmm2 + movdqa (%r11),%xmm2 // lo pshufb %xmm0,%xmm2 - movdqa 16(%r11),%xmm0 + movdqa 16(%r11),%xmm0 // hi pshufb %xmm1,%xmm0 pxor %xmm2,%xmm0 ret .cfi_endproc .size _vpaes_schedule_transform,.-_vpaes_schedule_transform - - - - - - - - - - - - - - - - - - - - - - - +// +// .aes_schedule_mangle +// +// Mangle xmm0 from (basis-transformed) standard version +// to our version. +// +// On encrypt, +// xor with 0x63 +// multiply by circulant 0,1,1,1 +// apply shiftrows transform +// +// On decrypt, +// xor with 0x63 +// multiply by "inverse mixcolumns" circulant E,B,D,9 +// deskew +// apply shiftrows transform +// +// +// Writes out to (%rdx), and increments or decrements it +// Keeps track of round number mod 4 in %r8 +// Preserves xmm0 +// Clobbers xmm1-xmm5 +// .type _vpaes_schedule_mangle,@function .align 16 _vpaes_schedule_mangle: .cfi_startproc - movdqa %xmm0,%xmm4 + movdqa %xmm0,%xmm4 // save xmm0 for later movdqa .Lk_mc_forward(%rip),%xmm5 testq %rcx,%rcx jnz .Lschedule_mangle_dec - +// encrypting addq $16,%rdx pxor .Lk_s63(%rip),%xmm4 pshufb %xmm5,%xmm4 @@ -747,12 +747,12 @@ _vpaes_schedule_mangle: jmp .Lschedule_mangle_both .align 16 .Lschedule_mangle_dec: - +// inverse mix columns leaq .Lk_dksd(%rip),%r11 movdqa %xmm9,%xmm1 pandn %xmm4,%xmm1 - psrld $4,%xmm1 - pand %xmm9,%xmm4 + psrld $4,%xmm1 // 1 = hi + pand %xmm9,%xmm4 // 4 = lo movdqa 0(%r11),%xmm2 pshufb %xmm4,%xmm2 @@ -796,9 +796,9 @@ _vpaes_schedule_mangle: .cfi_endproc .size _vpaes_schedule_mangle,.-_vpaes_schedule_mangle - - - +// +// Interface to OpenSSL +// .globl vpaes_set_encrypt_key .hidden vpaes_set_encrypt_key .type vpaes_set_encrypt_key,@function @@ -815,7 +815,7 @@ _CET_ENDBR movl %esi,%eax shrl $5,%eax addl $5,%eax - movl %eax,240(%rdx) + movl %eax,240(%rdx) // AES_KEY->rounds = nbits/32+5; movl $0,%ecx movl $0x30,%r8d @@ -835,7 +835,7 @@ _CET_ENDBR movl %esi,%eax shrl $5,%eax addl $5,%eax - movl %eax,240(%rdx) + movl %eax,240(%rdx) // AES_KEY->rounds = nbits/32+5; shll $4,%eax leaq 16(%rdx,%rax,1),%rdx @@ -843,7 +843,7 @@ _CET_ENDBR movl %esi,%r8d shrl $1,%r8d andl $32,%r8d - xorl $32,%r8d + xorl $32,%r8d // nbits==192?0:32 call _vpaes_schedule_core xorl %eax,%eax ret @@ -894,7 +894,7 @@ _CET_ENDBR xchgq %rcx,%rdx subq $16,%rcx jc .Lcbc_abort - movdqu (%r8),%xmm6 + movdqu (%r8),%xmm6 // load IV subq %rdi,%rsi call _vpaes_preheat cmpl $0,%r9d @@ -923,7 +923,7 @@ _CET_ENDBR subq $16,%rcx jnc .Lcbc_dec_loop .Lcbc_done: - movdqu %xmm6,(%r8) + movdqu %xmm6,(%r8) // save IV .Lcbc_abort: ret .cfi_endproc @@ -935,13 +935,13 @@ _CET_ENDBR vpaes_ctr32_encrypt_blocks: .cfi_startproc _CET_ENDBR - +// _vpaes_encrypt_core and _vpaes_encrypt_core_2x expect the key in %rdx. xchgq %rcx,%rdx testq %rcx,%rcx jz .Lctr32_abort - movdqu (%r8),%xmm0 + movdqu (%r8),%xmm0 // Load IV. movdqa .Lctr_add_one(%rip),%xmm8 - subq %rdi,%rsi + subq %rdi,%rsi // This allows only incrementing %rdi. call _vpaes_preheat movdqa %xmm0,%xmm6 pshufb .Lrev_ctr(%rip),%xmm6 @@ -949,9 +949,9 @@ _CET_ENDBR testq $1,%rcx jz .Lctr32_prep_loop - - - movdqu (%rdi),%xmm7 +// Handle one block so the remaining block count is even for +// _vpaes_encrypt_core_2x. + movdqu (%rdi),%xmm7 // Load input. call _vpaes_encrypt_core pxor %xmm7,%xmm0 paddd %xmm8,%xmm6 @@ -961,29 +961,29 @@ _CET_ENDBR jz .Lctr32_done .Lctr32_prep_loop: - - +// _vpaes_encrypt_core_2x leaves only %xmm14 and %xmm15 as spare +// registers. We maintain two byte-swapped counters in them. movdqa %xmm6,%xmm14 movdqa %xmm6,%xmm15 paddd %xmm8,%xmm15 .Lctr32_loop: - movdqa .Lrev_ctr(%rip),%xmm1 + movdqa .Lrev_ctr(%rip),%xmm1 // Set up counters. movdqa %xmm14,%xmm0 movdqa %xmm15,%xmm6 pshufb %xmm1,%xmm0 pshufb %xmm1,%xmm6 call _vpaes_encrypt_core_2x - movdqu (%rdi),%xmm1 + movdqu (%rdi),%xmm1 // Load input. movdqu 16(%rdi),%xmm2 movdqa .Lctr_add_two(%rip),%xmm3 - pxor %xmm1,%xmm0 + pxor %xmm1,%xmm0 // XOR input. pxor %xmm2,%xmm6 - paddd %xmm3,%xmm14 + paddd %xmm3,%xmm14 // Increment counters. paddd %xmm3,%xmm15 - movdqu %xmm0,(%rsi,%rdi,1) + movdqu %xmm0,(%rsi,%rdi,1) // Write output. movdqu %xmm6,16(%rsi,%rdi,1) - subq $2,%rcx + subq $2,%rcx // Advance loop. leaq 32(%rdi),%rdi jnz .Lctr32_loop @@ -992,135 +992,135 @@ _CET_ENDBR ret .cfi_endproc .size vpaes_ctr32_encrypt_blocks,.-vpaes_ctr32_encrypt_blocks - - - - - - +// +// _aes_preheat +// +// Fills register %r10 -> .aes_consts (so you can -fPIC) +// and %xmm9-%xmm15 as specified below. +// .type _vpaes_preheat,@function .align 16 _vpaes_preheat: .cfi_startproc leaq .Lk_s0F(%rip),%r10 - movdqa -32(%r10),%xmm10 - movdqa -16(%r10),%xmm11 - movdqa 0(%r10),%xmm9 - movdqa 48(%r10),%xmm13 - movdqa 64(%r10),%xmm12 - movdqa 80(%r10),%xmm15 - movdqa 96(%r10),%xmm14 + movdqa -32(%r10),%xmm10 // .Lk_inv + movdqa -16(%r10),%xmm11 // .Lk_inv+16 + movdqa 0(%r10),%xmm9 // .Lk_s0F + movdqa 48(%r10),%xmm13 // .Lk_sb1 + movdqa 64(%r10),%xmm12 // .Lk_sb1+16 + movdqa 80(%r10),%xmm15 // .Lk_sb2 + movdqa 96(%r10),%xmm14 // .Lk_sb2+16 ret .cfi_endproc .size _vpaes_preheat,.-_vpaes_preheat - - - - - +// ###################################################### +// ## +// Constants ## +// ## +// ###################################################### .type _vpaes_consts,@object .section .rodata .align 64 _vpaes_consts: -.Lk_inv: +.Lk_inv: // inv, inva .quad 0x0E05060F0D080180, 0x040703090A0B0C02 .quad 0x01040A060F0B0780, 0x030D0E0C02050809 -.Lk_s0F: +.Lk_s0F: // s0F .quad 0x0F0F0F0F0F0F0F0F, 0x0F0F0F0F0F0F0F0F -.Lk_ipt: +.Lk_ipt: // input transform (lo, hi) .quad 0xC2B2E8985A2A7000, 0xCABAE09052227808 .quad 0x4C01307D317C4D00, 0xCD80B1FCB0FDCC81 -.Lk_sb1: +.Lk_sb1: // sb1u, sb1t .quad 0xB19BE18FCB503E00, 0xA5DF7A6E142AF544 .quad 0x3618D415FAE22300, 0x3BF7CCC10D2ED9EF -.Lk_sb2: +.Lk_sb2: // sb2u, sb2t .quad 0xE27A93C60B712400, 0x5EB7E955BC982FCD .quad 0x69EB88400AE12900, 0xC2A163C8AB82234A -.Lk_sbo: +.Lk_sbo: // sbou, sbot .quad 0xD0D26D176FBDC700, 0x15AABF7AC502A878 .quad 0xCFE474A55FBB6A00, 0x8E1E90D1412B35FA -.Lk_mc_forward: +.Lk_mc_forward: // mc_forward .quad 0x0407060500030201, 0x0C0F0E0D080B0A09 .quad 0x080B0A0904070605, 0x000302010C0F0E0D .quad 0x0C0F0E0D080B0A09, 0x0407060500030201 .quad 0x000302010C0F0E0D, 0x080B0A0904070605 -.Lk_mc_backward: +.Lk_mc_backward: // mc_backward .quad 0x0605040702010003, 0x0E0D0C0F0A09080B .quad 0x020100030E0D0C0F, 0x0A09080B06050407 .quad 0x0E0D0C0F0A09080B, 0x0605040702010003 .quad 0x0A09080B06050407, 0x020100030E0D0C0F -.Lk_sr: +.Lk_sr: // sr .quad 0x0706050403020100, 0x0F0E0D0C0B0A0908 .quad 0x030E09040F0A0500, 0x0B06010C07020D08 .quad 0x0F060D040B020900, 0x070E050C030A0108 .quad 0x0B0E0104070A0D00, 0x0306090C0F020508 -.Lk_rcon: +.Lk_rcon: // rcon .quad 0x1F8391B9AF9DEEB6, 0x702A98084D7C7D81 -.Lk_s63: +.Lk_s63: // s63: all equal to 0x63 transformed .quad 0x5B5B5B5B5B5B5B5B, 0x5B5B5B5B5B5B5B5B -.Lk_opt: +.Lk_opt: // output transform .quad 0xFF9F4929D6B66000, 0xF7974121DEBE6808 .quad 0x01EDBD5150BCEC00, 0xE10D5DB1B05C0CE0 -.Lk_deskew: +.Lk_deskew: // deskew tables: inverts the sbox's "skew" .quad 0x07E4A34047A4E300, 0x1DFEB95A5DBEF91A .quad 0x5F36B5DC83EA6900, 0x2841C2ABF49D1E77 - - - - -.Lk_dksd: +// +// Decryption stuff +// Key schedule constants +// +.Lk_dksd: // decryption key schedule: invskew x*D .quad 0xFEB91A5DA3E44700, 0x0740E3A45A1DBEF9 .quad 0x41C277F4B5368300, 0x5FDC69EAAB289D1E -.Lk_dksb: +.Lk_dksb: // decryption key schedule: invskew x*B .quad 0x9A4FCA1F8550D500, 0x03D653861CC94C99 .quad 0x115BEDA7B6FC4A00, 0xD993256F7E3482C8 -.Lk_dkse: +.Lk_dkse: // decryption key schedule: invskew x*E + 0x63 .quad 0xD5031CCA1FC9D600, 0x53859A4C994F5086 .quad 0xA23196054FDC7BE8, 0xCD5EF96A20B31487 -.Lk_dks9: +.Lk_dks9: // decryption key schedule: invskew x*9 .quad 0xB6116FC87ED9A700, 0x4AED933482255BFC .quad 0x4576516227143300, 0x8BB89FACE9DAFDCE - - - - -.Lk_dipt: +// +// Decryption stuff +// Round function constants +// +.Lk_dipt: // decryption input transform .quad 0x0F505B040B545F00, 0x154A411E114E451A .quad 0x86E383E660056500, 0x12771772F491F194 -.Lk_dsb9: +.Lk_dsb9: // decryption sbox output *9*u, *9*t .quad 0x851C03539A86D600, 0xCAD51F504F994CC9 .quad 0xC03B1789ECD74900, 0x725E2C9EB2FBA565 -.Lk_dsbd: +.Lk_dsbd: // decryption sbox output *D*u, *D*t .quad 0x7D57CCDFE6B1A200, 0xF56E9B13882A4439 .quad 0x3CE2FAF724C6CB00, 0x2931180D15DEEFD3 -.Lk_dsbb: +.Lk_dsbb: // decryption sbox output *B*u, *B*t .quad 0xD022649296B44200, 0x602646F6B0F2D404 .quad 0xC19498A6CD596700, 0xF3FF0C3E3255AA6B -.Lk_dsbe: +.Lk_dsbe: // decryption sbox output *E*u, *E*t .quad 0x46F2929626D4D000, 0x2242600464B4F6B0 .quad 0x0C55A6CDFFAAC100, 0x9467F36B98593E32 -.Lk_dsbo: +.Lk_dsbo: // decryption sbox final output .quad 0x1387EA537EF94000, 0xC7AA6DB9D4943E2D .quad 0x12D7560F93441D00, 0xCA4B8159D8C58E9C - +// .Lrev_ctr is a permutation which byte-swaps the counter portion of the IV. .Lrev_ctr: .quad 0x0706050403020100, 0x0c0d0e0f0b0a0908 - - +// .Lctr_add_* may be added to a byte-swapped xmm register to increment the +// counter. The register must be byte-swapped again to form the actual input. .Lctr_add_one: .quad 0x0000000000000000, 0x0000000100000000 .Lctr_add_two: diff --git a/third_party/boringssl/gen/bcm/vpaes-x86_64-win.asm b/third_party/boringssl/gen/bcm/vpaes-x86_64-win.asm index cddb9fa32..cea219570 100644 --- a/third_party/boringssl/gen/bcm/vpaes-x86_64-win.asm +++ b/third_party/boringssl/gen/bcm/vpaes-x86_64-win.asm @@ -14,21 +14,21 @@ default rel section .text code align=64 - - - - - - - - - - - - - - - +; # +; # _aes_encrypt_core +; # +; # AES-encrypt %xmm0. +; # +; # Inputs: +; # %xmm0 = input +; # %xmm9-%xmm15 as in _vpaes_preheat +; # (%rdx) = scheduled keys +; # +; # Output in %xmm0 +; # Clobbers %xmm1-%xmm5, %r9, %r10, %r11, %rax +; # Preserves %xmm6 - %xmm8 so you get some local vectors +; # +; # ALIGN 16 _vpaes_encrypt_core: @@ -37,13 +37,13 @@ _vpaes_encrypt_core: mov r11,16 mov eax,DWORD[240+rdx] movdqa xmm1,xmm9 - movdqa xmm2,XMMWORD[$L$k_ipt] + movdqa xmm2,XMMWORD[$L$k_ipt] ; iptlo pandn xmm1,xmm0 - movdqu xmm5,XMMWORD[r9] + movdqu xmm5,XMMWORD[r9] ; round0 key psrld xmm1,4 pand xmm0,xmm9 pshufb xmm2,xmm0 - movdqa xmm0,XMMWORD[(($L$k_ipt+16))] + movdqa xmm0,XMMWORD[(($L$k_ipt+16))] ; ipthi pshufb xmm0,xmm1 pxor xmm2,xmm5 add r9,16 @@ -53,98 +53,98 @@ _vpaes_encrypt_core: ALIGN 16 $L$enc_loop: - - movdqa xmm4,xmm13 - movdqa xmm0,xmm12 - pshufb xmm4,xmm2 - pshufb xmm0,xmm3 - pxor xmm4,xmm5 - movdqa xmm5,xmm15 - pxor xmm0,xmm4 - movdqa xmm1,XMMWORD[((-64))+r10*1+r11] - pshufb xmm5,xmm2 - movdqa xmm4,XMMWORD[r10*1+r11] - movdqa xmm2,xmm14 - pshufb xmm2,xmm3 - movdqa xmm3,xmm0 - pxor xmm2,xmm5 - pshufb xmm0,xmm1 - add r9,16 - pxor xmm0,xmm2 - pshufb xmm3,xmm4 - add r11,16 - pxor xmm3,xmm0 - pshufb xmm0,xmm1 - and r11,0x30 - sub rax,1 - pxor xmm0,xmm3 +; middle of middle round + movdqa xmm4,xmm13 ; 4 : sb1u + movdqa xmm0,xmm12 ; 0 : sb1t + pshufb xmm4,xmm2 ; 4 = sb1u + pshufb xmm0,xmm3 ; 0 = sb1t + pxor xmm4,xmm5 ; 4 = sb1u + k + movdqa xmm5,xmm15 ; 4 : sb2u + pxor xmm0,xmm4 ; 0 = A + movdqa xmm1,XMMWORD[((-64))+r10*1+r11] ; .Lk_mc_forward[] + pshufb xmm5,xmm2 ; 4 = sb2u + movdqa xmm4,XMMWORD[r10*1+r11] ; .Lk_mc_backward[] + movdqa xmm2,xmm14 ; 2 : sb2t + pshufb xmm2,xmm3 ; 2 = sb2t + movdqa xmm3,xmm0 ; 3 = A + pxor xmm2,xmm5 ; 2 = 2A + pshufb xmm0,xmm1 ; 0 = B + add r9,16 ; next key + pxor xmm0,xmm2 ; 0 = 2A+B + pshufb xmm3,xmm4 ; 3 = D + add r11,16 ; next mc + pxor xmm3,xmm0 ; 3 = 2A+B+D + pshufb xmm0,xmm1 ; 0 = 2B+C + and r11,0x30 ; ... mod 4 + sub rax,1 ; nr-- + pxor xmm0,xmm3 ; 0 = 2A+3B+C+D $L$enc_entry: - - movdqa xmm1,xmm9 - movdqa xmm5,xmm11 - pandn xmm1,xmm0 - psrld xmm1,4 - pand xmm0,xmm9 - pshufb xmm5,xmm0 - movdqa xmm3,xmm10 - pxor xmm0,xmm1 - pshufb xmm3,xmm1 - movdqa xmm4,xmm10 - pxor xmm3,xmm5 - pshufb xmm4,xmm0 - movdqa xmm2,xmm10 - pxor xmm4,xmm5 - pshufb xmm2,xmm3 - movdqa xmm3,xmm10 - pxor xmm2,xmm0 - pshufb xmm3,xmm4 +; top of round + movdqa xmm1,xmm9 ; 1 : i + movdqa xmm5,xmm11 ; 2 : a/k + pandn xmm1,xmm0 ; 1 = i<<4 + psrld xmm1,4 ; 1 = i + pand xmm0,xmm9 ; 0 = k + pshufb xmm5,xmm0 ; 2 = a/k + movdqa xmm3,xmm10 ; 3 : 1/i + pxor xmm0,xmm1 ; 0 = j + pshufb xmm3,xmm1 ; 3 = 1/i + movdqa xmm4,xmm10 ; 4 : 1/j + pxor xmm3,xmm5 ; 3 = iak = 1/i + a/k + pshufb xmm4,xmm0 ; 4 = 1/j + movdqa xmm2,xmm10 ; 2 : 1/iak + pxor xmm4,xmm5 ; 4 = jak = 1/j + a/k + pshufb xmm2,xmm3 ; 2 = 1/iak + movdqa xmm3,xmm10 ; 3 : 1/jak + pxor xmm2,xmm0 ; 2 = io + pshufb xmm3,xmm4 ; 3 = 1/jak movdqu xmm5,XMMWORD[r9] - pxor xmm3,xmm1 + pxor xmm3,xmm1 ; 3 = jo jnz NEAR $L$enc_loop - - movdqa xmm4,XMMWORD[((-96))+r10] - movdqa xmm0,XMMWORD[((-80))+r10] - pshufb xmm4,xmm2 - pxor xmm4,xmm5 - pshufb xmm0,xmm3 - movdqa xmm1,XMMWORD[64+r10*1+r11] - pxor xmm0,xmm4 +; middle of last round + movdqa xmm4,XMMWORD[((-96))+r10] ; 3 : sbou .Lk_sbo + movdqa xmm0,XMMWORD[((-80))+r10] ; 0 : sbot .Lk_sbo+16 + pshufb xmm4,xmm2 ; 4 = sbou + pxor xmm4,xmm5 ; 4 = sb1u + k + pshufb xmm0,xmm3 ; 0 = sb1t + movdqa xmm1,XMMWORD[64+r10*1+r11] ; .Lk_sr[] + pxor xmm0,xmm4 ; 0 = A pshufb xmm0,xmm1 ret - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +; # +; # _aes_encrypt_core_2x +; # +; # AES-encrypt %xmm0 and %xmm6 in parallel. +; # +; # Inputs: +; # %xmm0 and %xmm6 = input +; # %xmm9 and %xmm10 as in _vpaes_preheat +; # (%rdx) = scheduled keys +; # +; # Output in %xmm0 and %xmm6 +; # Clobbers %xmm1-%xmm5, %xmm7, %xmm8, %xmm11-%xmm13, %r9, %r10, %r11, %rax +; # Preserves %xmm14 and %xmm15 +; # +; # This function stitches two parallel instances of _vpaes_encrypt_core. x86_64 +; # provides 16 XMM registers. _vpaes_encrypt_core computes over six registers +; # (%xmm0-%xmm5) and additionally uses seven registers with preloaded constants +; # from _vpaes_preheat (%xmm9-%xmm15). This does not quite fit two instances, +; # so we spill some of %xmm9 through %xmm15 back to memory. We keep %xmm9 and +; # %xmm10 in registers as these values are used several times in a row. The +; # remainder are read once per round and are spilled to memory. This leaves two +; # registers preserved for the caller. +; # +; # Thus, of the two _vpaes_encrypt_core instances, the first uses (%xmm0-%xmm5) +; # as before. The second uses %xmm6-%xmm8,%xmm11-%xmm13. (Add 6 to %xmm2 and +; # below. Add 8 to %xmm3 and up.) Instructions in the second instance are +; # indented by one space. +; # +; # ALIGN 16 _vpaes_encrypt_core_2x: @@ -154,19 +154,19 @@ _vpaes_encrypt_core_2x: mov eax,DWORD[240+rdx] movdqa xmm1,xmm9 movdqa xmm7,xmm9 - movdqa xmm2,XMMWORD[$L$k_ipt] + movdqa xmm2,XMMWORD[$L$k_ipt] ; iptlo movdqa xmm8,xmm2 pandn xmm1,xmm0 pandn xmm7,xmm6 - movdqu xmm5,XMMWORD[r9] - + movdqu xmm5,XMMWORD[r9] ; round0 key +; Also use %xmm5 in the second instance. psrld xmm1,4 psrld xmm7,4 pand xmm0,xmm9 pand xmm6,xmm9 pshufb xmm2,xmm0 pshufb xmm8,xmm6 - movdqa xmm0,XMMWORD[(($L$k_ipt+16))] + movdqa xmm0,XMMWORD[(($L$k_ipt+16))] ; ipthi movdqa xmm6,xmm0 pshufb xmm0,xmm1 pshufb xmm6,xmm7 @@ -180,110 +180,110 @@ _vpaes_encrypt_core_2x: ALIGN 16 $L$enc2x_loop: - - movdqa xmm4,XMMWORD[$L$k_sb1] - movdqa xmm0,XMMWORD[(($L$k_sb1+16))] +; middle of middle round + movdqa xmm4,XMMWORD[$L$k_sb1] ; 4 : sb1u + movdqa xmm0,XMMWORD[(($L$k_sb1+16))] ; 0 : sb1t movdqa xmm12,xmm4 movdqa xmm6,xmm0 - pshufb xmm4,xmm2 + pshufb xmm4,xmm2 ; 4 = sb1u pshufb xmm12,xmm8 - pshufb xmm0,xmm3 + pshufb xmm0,xmm3 ; 0 = sb1t pshufb xmm6,xmm11 - pxor xmm4,xmm5 + pxor xmm4,xmm5 ; 4 = sb1u + k pxor xmm12,xmm5 - movdqa xmm5,XMMWORD[$L$k_sb2] + movdqa xmm5,XMMWORD[$L$k_sb2] ; 4 : sb2u movdqa xmm13,xmm5 - pxor xmm0,xmm4 + pxor xmm0,xmm4 ; 0 = A pxor xmm6,xmm12 - movdqa xmm1,XMMWORD[((-64))+r10*1+r11] - - pshufb xmm5,xmm2 + movdqa xmm1,XMMWORD[((-64))+r10*1+r11] ; .Lk_mc_forward[] +; Also use %xmm1 in the second instance. + pshufb xmm5,xmm2 ; 4 = sb2u pshufb xmm13,xmm8 - movdqa xmm4,XMMWORD[r10*1+r11] - - movdqa xmm2,XMMWORD[(($L$k_sb2+16))] + movdqa xmm4,XMMWORD[r10*1+r11] ; .Lk_mc_backward[] +; Also use %xmm4 in the second instance. + movdqa xmm2,XMMWORD[(($L$k_sb2+16))] ; 2 : sb2t movdqa xmm8,xmm2 - pshufb xmm2,xmm3 + pshufb xmm2,xmm3 ; 2 = sb2t pshufb xmm8,xmm11 - movdqa xmm3,xmm0 + movdqa xmm3,xmm0 ; 3 = A movdqa xmm11,xmm6 - pxor xmm2,xmm5 + pxor xmm2,xmm5 ; 2 = 2A pxor xmm8,xmm13 - pshufb xmm0,xmm1 + pshufb xmm0,xmm1 ; 0 = B pshufb xmm6,xmm1 - add r9,16 - pxor xmm0,xmm2 + add r9,16 ; next key + pxor xmm0,xmm2 ; 0 = 2A+B pxor xmm6,xmm8 - pshufb xmm3,xmm4 + pshufb xmm3,xmm4 ; 3 = D pshufb xmm11,xmm4 - add r11,16 - pxor xmm3,xmm0 + add r11,16 ; next mc + pxor xmm3,xmm0 ; 3 = 2A+B+D pxor xmm11,xmm6 - pshufb xmm0,xmm1 + pshufb xmm0,xmm1 ; 0 = 2B+C pshufb xmm6,xmm1 - and r11,0x30 - sub rax,1 - pxor xmm0,xmm3 + and r11,0x30 ; ... mod 4 + sub rax,1 ; nr-- + pxor xmm0,xmm3 ; 0 = 2A+3B+C+D pxor xmm6,xmm11 $L$enc2x_entry: - - movdqa xmm1,xmm9 +; top of round + movdqa xmm1,xmm9 ; 1 : i movdqa xmm7,xmm9 - movdqa xmm5,XMMWORD[(($L$k_inv+16))] + movdqa xmm5,XMMWORD[(($L$k_inv+16))] ; 2 : a/k movdqa xmm13,xmm5 - pandn xmm1,xmm0 + pandn xmm1,xmm0 ; 1 = i<<4 pandn xmm7,xmm6 - psrld xmm1,4 + psrld xmm1,4 ; 1 = i psrld xmm7,4 - pand xmm0,xmm9 + pand xmm0,xmm9 ; 0 = k pand xmm6,xmm9 - pshufb xmm5,xmm0 + pshufb xmm5,xmm0 ; 2 = a/k pshufb xmm13,xmm6 - movdqa xmm3,xmm10 + movdqa xmm3,xmm10 ; 3 : 1/i movdqa xmm11,xmm10 - pxor xmm0,xmm1 + pxor xmm0,xmm1 ; 0 = j pxor xmm6,xmm7 - pshufb xmm3,xmm1 + pshufb xmm3,xmm1 ; 3 = 1/i pshufb xmm11,xmm7 - movdqa xmm4,xmm10 + movdqa xmm4,xmm10 ; 4 : 1/j movdqa xmm12,xmm10 - pxor xmm3,xmm5 + pxor xmm3,xmm5 ; 3 = iak = 1/i + a/k pxor xmm11,xmm13 - pshufb xmm4,xmm0 + pshufb xmm4,xmm0 ; 4 = 1/j pshufb xmm12,xmm6 - movdqa xmm2,xmm10 + movdqa xmm2,xmm10 ; 2 : 1/iak movdqa xmm8,xmm10 - pxor xmm4,xmm5 + pxor xmm4,xmm5 ; 4 = jak = 1/j + a/k pxor xmm12,xmm13 - pshufb xmm2,xmm3 + pshufb xmm2,xmm3 ; 2 = 1/iak pshufb xmm8,xmm11 - movdqa xmm3,xmm10 + movdqa xmm3,xmm10 ; 3 : 1/jak movdqa xmm11,xmm10 - pxor xmm2,xmm0 + pxor xmm2,xmm0 ; 2 = io pxor xmm8,xmm6 - pshufb xmm3,xmm4 + pshufb xmm3,xmm4 ; 3 = 1/jak pshufb xmm11,xmm12 movdqu xmm5,XMMWORD[r9] - - pxor xmm3,xmm1 +; Also use %xmm5 in the second instance. + pxor xmm3,xmm1 ; 3 = jo pxor xmm11,xmm7 jnz NEAR $L$enc2x_loop - - movdqa xmm4,XMMWORD[((-96))+r10] - movdqa xmm0,XMMWORD[((-80))+r10] +; middle of last round + movdqa xmm4,XMMWORD[((-96))+r10] ; 3 : sbou .Lk_sbo + movdqa xmm0,XMMWORD[((-80))+r10] ; 0 : sbot .Lk_sbo+16 movdqa xmm12,xmm4 movdqa xmm6,xmm0 - pshufb xmm4,xmm2 + pshufb xmm4,xmm2 ; 4 = sbou pshufb xmm12,xmm8 - pxor xmm4,xmm5 + pxor xmm4,xmm5 ; 4 = sb1u + k pxor xmm12,xmm5 - pshufb xmm0,xmm3 + pshufb xmm0,xmm3 ; 0 = sb1t pshufb xmm6,xmm11 - movdqa xmm1,XMMWORD[64+r10*1+r11] - - pxor xmm0,xmm4 + movdqa xmm1,XMMWORD[64+r10*1+r11] ; .Lk_sr[] +; Also use %xmm1 in the second instance. + pxor xmm0,xmm4 ; 0 = A pxor xmm6,xmm12 pshufb xmm0,xmm1 pshufb xmm6,xmm1 @@ -291,27 +291,27 @@ $L$enc2x_entry: - - - - - +; # +; # Decryption core +; # +; # Same API as encryption core. +; # ALIGN 16 _vpaes_decrypt_core: - mov r9,rdx + mov r9,rdx ; load key mov eax,DWORD[240+rdx] movdqa xmm1,xmm9 - movdqa xmm2,XMMWORD[$L$k_dipt] + movdqa xmm2,XMMWORD[$L$k_dipt] ; iptlo pandn xmm1,xmm0 mov r11,rax psrld xmm1,4 - movdqu xmm5,XMMWORD[r9] + movdqu xmm5,XMMWORD[r9] ; round0 key shl r11,4 pand xmm0,xmm9 pshufb xmm2,xmm0 - movdqa xmm0,XMMWORD[(($L$k_dipt+16))] + movdqa xmm0,XMMWORD[(($L$k_dipt+16))] ; ipthi xor r11,0x30 lea r10,[$L$k_dsbd] pshufb xmm0,xmm1 @@ -325,99 +325,99 @@ _vpaes_decrypt_core: ALIGN 16 $L$dec_loop: - - - - movdqa xmm4,XMMWORD[((-32))+r10] - movdqa xmm1,XMMWORD[((-16))+r10] - pshufb xmm4,xmm2 - pshufb xmm1,xmm3 +; # +; # Inverse mix columns +; # + movdqa xmm4,XMMWORD[((-32))+r10] ; 4 : sb9u + movdqa xmm1,XMMWORD[((-16))+r10] ; 0 : sb9t + pshufb xmm4,xmm2 ; 4 = sb9u + pshufb xmm1,xmm3 ; 0 = sb9t pxor xmm0,xmm4 - movdqa xmm4,XMMWORD[r10] - pxor xmm0,xmm1 - movdqa xmm1,XMMWORD[16+r10] - - pshufb xmm4,xmm2 - pshufb xmm0,xmm5 - pshufb xmm1,xmm3 - pxor xmm0,xmm4 - movdqa xmm4,XMMWORD[32+r10] - pxor xmm0,xmm1 - movdqa xmm1,XMMWORD[48+r10] - - pshufb xmm4,xmm2 - pshufb xmm0,xmm5 - pshufb xmm1,xmm3 - pxor xmm0,xmm4 - movdqa xmm4,XMMWORD[64+r10] - pxor xmm0,xmm1 - movdqa xmm1,XMMWORD[80+r10] - - pshufb xmm4,xmm2 - pshufb xmm0,xmm5 - pshufb xmm1,xmm3 - pxor xmm0,xmm4 - add r9,16 + movdqa xmm4,XMMWORD[r10] ; 4 : sbdu + pxor xmm0,xmm1 ; 0 = ch + movdqa xmm1,XMMWORD[16+r10] ; 0 : sbdt + + pshufb xmm4,xmm2 ; 4 = sbdu + pshufb xmm0,xmm5 ; MC ch + pshufb xmm1,xmm3 ; 0 = sbdt + pxor xmm0,xmm4 ; 4 = ch + movdqa xmm4,XMMWORD[32+r10] ; 4 : sbbu + pxor xmm0,xmm1 ; 0 = ch + movdqa xmm1,XMMWORD[48+r10] ; 0 : sbbt + + pshufb xmm4,xmm2 ; 4 = sbbu + pshufb xmm0,xmm5 ; MC ch + pshufb xmm1,xmm3 ; 0 = sbbt + pxor xmm0,xmm4 ; 4 = ch + movdqa xmm4,XMMWORD[64+r10] ; 4 : sbeu + pxor xmm0,xmm1 ; 0 = ch + movdqa xmm1,XMMWORD[80+r10] ; 0 : sbet + + pshufb xmm4,xmm2 ; 4 = sbeu + pshufb xmm0,xmm5 ; MC ch + pshufb xmm1,xmm3 ; 0 = sbet + pxor xmm0,xmm4 ; 4 = ch + add r9,16 ; next round key palignr xmm5,xmm5,12 - pxor xmm0,xmm1 - sub rax,1 + pxor xmm0,xmm1 ; 0 = ch + sub rax,1 ; nr-- $L$dec_entry: - - movdqa xmm1,xmm9 - pandn xmm1,xmm0 - movdqa xmm2,xmm11 - psrld xmm1,4 - pand xmm0,xmm9 - pshufb xmm2,xmm0 - movdqa xmm3,xmm10 - pxor xmm0,xmm1 - pshufb xmm3,xmm1 - movdqa xmm4,xmm10 - pxor xmm3,xmm2 - pshufb xmm4,xmm0 - pxor xmm4,xmm2 - movdqa xmm2,xmm10 - pshufb xmm2,xmm3 - movdqa xmm3,xmm10 - pxor xmm2,xmm0 - pshufb xmm3,xmm4 +; top of round + movdqa xmm1,xmm9 ; 1 : i + pandn xmm1,xmm0 ; 1 = i<<4 + movdqa xmm2,xmm11 ; 2 : a/k + psrld xmm1,4 ; 1 = i + pand xmm0,xmm9 ; 0 = k + pshufb xmm2,xmm0 ; 2 = a/k + movdqa xmm3,xmm10 ; 3 : 1/i + pxor xmm0,xmm1 ; 0 = j + pshufb xmm3,xmm1 ; 3 = 1/i + movdqa xmm4,xmm10 ; 4 : 1/j + pxor xmm3,xmm2 ; 3 = iak = 1/i + a/k + pshufb xmm4,xmm0 ; 4 = 1/j + pxor xmm4,xmm2 ; 4 = jak = 1/j + a/k + movdqa xmm2,xmm10 ; 2 : 1/iak + pshufb xmm2,xmm3 ; 2 = 1/iak + movdqa xmm3,xmm10 ; 3 : 1/jak + pxor xmm2,xmm0 ; 2 = io + pshufb xmm3,xmm4 ; 3 = 1/jak movdqu xmm0,XMMWORD[r9] - pxor xmm3,xmm1 + pxor xmm3,xmm1 ; 3 = jo jnz NEAR $L$dec_loop - - movdqa xmm4,XMMWORD[96+r10] - pshufb xmm4,xmm2 - pxor xmm4,xmm0 - movdqa xmm0,XMMWORD[112+r10] - movdqa xmm2,XMMWORD[((-352))+r11] - pshufb xmm0,xmm3 - pxor xmm0,xmm4 +; middle of last round + movdqa xmm4,XMMWORD[96+r10] ; 3 : sbou + pshufb xmm4,xmm2 ; 4 = sbou + pxor xmm4,xmm0 ; 4 = sb1u + k + movdqa xmm0,XMMWORD[112+r10] ; 0 : sbot + movdqa xmm2,XMMWORD[((-352))+r11] ; .Lk_sr-.Lk_dsbd=-0x160 + pshufb xmm0,xmm3 ; 0 = sb1t + pxor xmm0,xmm4 ; 0 = A pshufb xmm0,xmm2 ret - - - - - +; ####################################################### +; # ## +; # AES key schedule ## +; # ## +; ####################################################### ALIGN 16 _vpaes_schedule_core: +; rdi = key +; rsi = size in bits +; rdx = buffer +; rcx = direction. 0=encrypt, 1=decrypt + call _vpaes_preheat ; load the tables + movdqa xmm8,XMMWORD[$L$k_rcon] ; load rcon + movdqu xmm0,XMMWORD[rdi] ; load key (unaligned) - - - - call _vpaes_preheat - movdqa xmm8,XMMWORD[$L$k_rcon] - movdqu xmm0,XMMWORD[rdi] - - +; input transform movdqa xmm3,xmm0 lea r11,[$L$k_ipt] call _vpaes_schedule_transform @@ -427,12 +427,12 @@ _vpaes_schedule_core: test rcx,rcx jnz NEAR $L$schedule_am_decrypting - +; encrypting, output zeroth round key after transform movdqu XMMWORD[rdx],xmm0 jmp NEAR $L$schedule_go $L$schedule_am_decrypting: - +; decrypting, output zeroth round key after shiftrows movdqa xmm1,XMMWORD[r10*1+r8] pshufb xmm3,xmm1 movdqu XMMWORD[rdx],xmm3 @@ -442,16 +442,16 @@ $L$schedule_go: cmp esi,192 ja NEAR $L$schedule_256 je NEAR $L$schedule_192 - - - - - - - - - - +; 128: fall though + +; # +; # .schedule_128 +; # +; # 128-bit specific part of key schedule. +; # +; # This schedule is really simple, because all its parts +; # are accomplished by the subroutines. +; # $L$schedule_128: mov esi,10 @@ -459,73 +459,73 @@ $L$oop_schedule_128: call _vpaes_schedule_round dec rsi jz NEAR $L$schedule_mangle_last - call _vpaes_schedule_mangle + call _vpaes_schedule_mangle ; write output jmp NEAR $L$oop_schedule_128 - - - - - - - - - - - - - - - +; # +; # .aes_schedule_192 +; # +; # 192-bit specific part of key schedule. +; # +; # The main body of this schedule is the same as the 128-bit +; # schedule, but with more smearing. The long, high side is +; # stored in %xmm7 as before, and the short, low side is in +; # the high bits of %xmm6. +; # +; # This schedule is somewhat nastier, however, because each +; # round produces 192 bits of key material, or 1.5 round keys. +; # Therefore, on each cycle we do 2 rounds and produce 3 round +; # keys. +; # ALIGN 16 $L$schedule_192: - movdqu xmm0,XMMWORD[8+rdi] - call _vpaes_schedule_transform - movdqa xmm6,xmm0 - pxor xmm4,xmm4 - movhlps xmm6,xmm4 + movdqu xmm0,XMMWORD[8+rdi] ; load key part 2 (very unaligned) + call _vpaes_schedule_transform ; input transform + movdqa xmm6,xmm0 ; save short part + pxor xmm4,xmm4 ; clear 4 + movhlps xmm6,xmm4 ; clobber low side with zeros mov esi,4 $L$oop_schedule_192: call _vpaes_schedule_round palignr xmm0,xmm6,8 - call _vpaes_schedule_mangle + call _vpaes_schedule_mangle ; save key n call _vpaes_schedule_192_smear - call _vpaes_schedule_mangle + call _vpaes_schedule_mangle ; save key n+1 call _vpaes_schedule_round dec rsi jz NEAR $L$schedule_mangle_last - call _vpaes_schedule_mangle + call _vpaes_schedule_mangle ; save key n+2 call _vpaes_schedule_192_smear jmp NEAR $L$oop_schedule_192 - - - - - - - - - - +; # +; # .aes_schedule_256 +; # +; # 256-bit specific part of key schedule. +; # +; # The structure here is very similar to the 128-bit +; # schedule, but with an additional "low side" in +; # %xmm6. The low side's rounds are the same as the +; # high side's, except no rcon and no rotation. +; # ALIGN 16 $L$schedule_256: - movdqu xmm0,XMMWORD[16+rdi] - call _vpaes_schedule_transform + movdqu xmm0,XMMWORD[16+rdi] ; load key part 2 (unaligned) + call _vpaes_schedule_transform ; input transform mov esi,7 $L$oop_schedule_256: - call _vpaes_schedule_mangle - movdqa xmm6,xmm0 - + call _vpaes_schedule_mangle ; output low result + movdqa xmm6,xmm0 ; save cur_lo in xmm6 +; high round call _vpaes_schedule_round dec rsi jz NEAR $L$schedule_mangle_last call _vpaes_schedule_mangle - +; low round. swap xmm7 and xmm6 pshufd xmm0,xmm0,0xFF movdqa xmm5,xmm7 movdqa xmm7,xmm6 @@ -535,36 +535,36 @@ $L$oop_schedule_256: jmp NEAR $L$oop_schedule_256 - - - - - - - - - - +; # +; # .aes_schedule_mangle_last +; # +; # Mangler for last round of key schedule +; # Mangles %xmm0 +; # when encrypting, outputs out(%xmm0) ^ 63 +; # when decrypting, outputs unskew(%xmm0) +; # +; # Always called right before return... jumps to cleanup and exits +; # ALIGN 16 $L$schedule_mangle_last: - - lea r11,[$L$k_deskew] +; schedule last round key from xmm0 + lea r11,[$L$k_deskew] ; prepare to deskew test rcx,rcx jnz NEAR $L$schedule_mangle_last_dec - +; encrypting movdqa xmm1,XMMWORD[r10*1+r8] - pshufb xmm0,xmm1 - lea r11,[$L$k_opt] + pshufb xmm0,xmm1 ; output permute + lea r11,[$L$k_opt] ; prepare to output transform add rdx,32 $L$schedule_mangle_last_dec: add rdx,-16 pxor xmm0,XMMWORD[$L$k_s63] - call _vpaes_schedule_transform - movdqu XMMWORD[rdx],xmm0 - + call _vpaes_schedule_transform ; output transform + movdqu XMMWORD[rdx],xmm0 ; save last key +; cleanup pxor xmm0,xmm0 pxor xmm1,xmm1 pxor xmm2,xmm2 @@ -577,72 +577,72 @@ $L$schedule_mangle_last_dec: - - - - - - - - - - - - - - +; # +; # .aes_schedule_192_smear +; # +; # Smear the short, low side in the 192-bit key schedule. +; # +; # Inputs: +; # %xmm7: high side, b a x y +; # %xmm6: low side, d c 0 0 +; # %xmm13: 0 +; # +; # Outputs: +; # %xmm6: b+c+d b+c 0 0 +; # %xmm0: b+c+d b+c b a +; # ALIGN 16 _vpaes_schedule_192_smear: - pshufd xmm1,xmm6,0x80 - pshufd xmm0,xmm7,0xFE - pxor xmm6,xmm1 + pshufd xmm1,xmm6,0x80 ; d c 0 0 -> c 0 0 0 + pshufd xmm0,xmm7,0xFE ; b a _ _ -> b b b a + pxor xmm6,xmm1 ; -> c+d c 0 0 pxor xmm1,xmm1 - pxor xmm6,xmm0 + pxor xmm6,xmm0 ; -> b+c+d b+c b a movdqa xmm0,xmm6 - movhlps xmm6,xmm1 + movhlps xmm6,xmm1 ; clobber low side with zeros ret - - - - - - - - - - - - - - - - - - +; # +; # .aes_schedule_round +; # +; # Runs one main round of the key schedule on %xmm0, %xmm7 +; # +; # Specifically, runs subbytes on the high dword of %xmm0 +; # then rotates it by one byte and xors into the low dword of +; # %xmm7. +; # +; # Adds rcon from low byte of %xmm8, then rotates %xmm8 for +; # next rcon. +; # +; # Smears the dwords of %xmm7 by xoring the low into the +; # second low, result into third, result into highest. +; # +; # Returns results in %xmm7 = %xmm0. +; # Clobbers %xmm1-%xmm4, %r11. +; # ALIGN 16 _vpaes_schedule_round: - +; extract rcon from xmm8 pxor xmm1,xmm1 palignr xmm1,xmm8,15 palignr xmm8,xmm8,15 pxor xmm7,xmm1 - +; rotate pshufd xmm0,xmm0,0xFF palignr xmm0,xmm0,1 +; fall through... - - +; low round: same as high round, but no rotation and no rcon. _vpaes_schedule_low_round: - +; smear xmm7 movdqa xmm1,xmm7 pslldq xmm7,4 pxor xmm7,xmm1 @@ -651,48 +651,48 @@ _vpaes_schedule_low_round: pxor xmm7,xmm1 pxor xmm7,XMMWORD[$L$k_s63] - +; subbytes movdqa xmm1,xmm9 pandn xmm1,xmm0 - psrld xmm1,4 - pand xmm0,xmm9 - movdqa xmm2,xmm11 - pshufb xmm2,xmm0 - pxor xmm0,xmm1 - movdqa xmm3,xmm10 - pshufb xmm3,xmm1 - pxor xmm3,xmm2 - movdqa xmm4,xmm10 - pshufb xmm4,xmm0 - pxor xmm4,xmm2 - movdqa xmm2,xmm10 - pshufb xmm2,xmm3 - pxor xmm2,xmm0 - movdqa xmm3,xmm10 - pshufb xmm3,xmm4 - pxor xmm3,xmm1 - movdqa xmm4,xmm13 - pshufb xmm4,xmm2 - movdqa xmm0,xmm12 - pshufb xmm0,xmm3 - pxor xmm0,xmm4 - - + psrld xmm1,4 ; 1 = i + pand xmm0,xmm9 ; 0 = k + movdqa xmm2,xmm11 ; 2 : a/k + pshufb xmm2,xmm0 ; 2 = a/k + pxor xmm0,xmm1 ; 0 = j + movdqa xmm3,xmm10 ; 3 : 1/i + pshufb xmm3,xmm1 ; 3 = 1/i + pxor xmm3,xmm2 ; 3 = iak = 1/i + a/k + movdqa xmm4,xmm10 ; 4 : 1/j + pshufb xmm4,xmm0 ; 4 = 1/j + pxor xmm4,xmm2 ; 4 = jak = 1/j + a/k + movdqa xmm2,xmm10 ; 2 : 1/iak + pshufb xmm2,xmm3 ; 2 = 1/iak + pxor xmm2,xmm0 ; 2 = io + movdqa xmm3,xmm10 ; 3 : 1/jak + pshufb xmm3,xmm4 ; 3 = 1/jak + pxor xmm3,xmm1 ; 3 = jo + movdqa xmm4,xmm13 ; 4 : sbou + pshufb xmm4,xmm2 ; 4 = sbou + movdqa xmm0,xmm12 ; 0 : sbot + pshufb xmm0,xmm3 ; 0 = sb1t + pxor xmm0,xmm4 ; 0 = sbox output + +; add in smeared stuff pxor xmm0,xmm7 movdqa xmm7,xmm0 ret - - - - - - - - - +; # +; # .aes_schedule_transform +; # +; # Linear-transform %xmm0 according to tables at (%r11) +; # +; # Requires that %xmm9 = 0x0F0F... as in preheat +; # Output in %xmm0 +; # Clobbers %xmm1, %xmm2 +; # ALIGN 16 _vpaes_schedule_transform: @@ -701,48 +701,48 @@ _vpaes_schedule_transform: pandn xmm1,xmm0 psrld xmm1,4 pand xmm0,xmm9 - movdqa xmm2,XMMWORD[r11] + movdqa xmm2,XMMWORD[r11] ; lo pshufb xmm2,xmm0 - movdqa xmm0,XMMWORD[16+r11] + movdqa xmm0,XMMWORD[16+r11] ; hi pshufb xmm0,xmm1 pxor xmm0,xmm2 ret - - - - - - - - - - - - - - - - - - - - - - - +; # +; # .aes_schedule_mangle +; # +; # Mangle xmm0 from (basis-transformed) standard version +; # to our version. +; # +; # On encrypt, +; # xor with 0x63 +; # multiply by circulant 0,1,1,1 +; # apply shiftrows transform +; # +; # On decrypt, +; # xor with 0x63 +; # multiply by "inverse mixcolumns" circulant E,B,D,9 +; # deskew +; # apply shiftrows transform +; # +; # +; # Writes out to (%rdx), and increments or decrements it +; # Keeps track of round number mod 4 in %r8 +; # Preserves xmm0 +; # Clobbers xmm1-xmm5 +; # ALIGN 16 _vpaes_schedule_mangle: - movdqa xmm4,xmm0 + movdqa xmm4,xmm0 ; save xmm0 for later movdqa xmm5,XMMWORD[$L$k_mc_forward] test rcx,rcx jnz NEAR $L$schedule_mangle_dec - +; encrypting add rdx,16 pxor xmm4,XMMWORD[$L$k_s63] pshufb xmm4,xmm5 @@ -755,12 +755,12 @@ _vpaes_schedule_mangle: jmp NEAR $L$schedule_mangle_both ALIGN 16 $L$schedule_mangle_dec: - +; inverse mix columns lea r11,[$L$k_dksd] movdqa xmm1,xmm9 pandn xmm1,xmm4 - psrld xmm1,4 - pand xmm4,xmm9 + psrld xmm1,4 ; 1 = hi + pand xmm4,xmm9 ; 4 = lo movdqa xmm2,XMMWORD[r11] pshufb xmm2,xmm4 @@ -804,9 +804,9 @@ $L$schedule_mangle_both: - - - +; +; Interface to OpenSSL +; global vpaes_set_encrypt_key ALIGN 16 @@ -842,7 +842,7 @@ $L$enc_key_body: mov eax,esi shr eax,5 add eax,5 - mov DWORD[240+rdx],eax + mov DWORD[240+rdx],eax ; AES_KEY->rounds = nbits/32+5; mov ecx,0 mov r8d,0x30 @@ -896,7 +896,7 @@ $L$dec_key_body: mov eax,esi shr eax,5 add eax,5 - mov DWORD[240+rdx],eax + mov DWORD[240+rdx],eax ; AES_KEY->rounds = nbits/32+5; shl eax,4 lea rdx,[16+rax*1+rdx] @@ -904,7 +904,7 @@ $L$dec_key_body: mov r8d,esi shr r8d,1 and r8d,32 - xor r8d,32 + xor r8d,32 ; nbits==192?0:32 call _vpaes_schedule_core movaps xmm6,XMMWORD[16+rsp] movaps xmm7,XMMWORD[32+rsp] @@ -1059,7 +1059,7 @@ _CET_ENDBR movaps XMMWORD[144+rsp],xmm14 movaps XMMWORD[160+rsp],xmm15 $L$cbc_body: - movdqu xmm6,XMMWORD[r8] + movdqu xmm6,XMMWORD[r8] ; load IV sub rsi,rdi call _vpaes_preheat cmp r9d,0 @@ -1088,7 +1088,7 @@ $L$cbc_dec_loop: sub rcx,16 jnc NEAR $L$cbc_dec_loop $L$cbc_done: - movdqu XMMWORD[r8],xmm6 + movdqu XMMWORD[r8],xmm6 ; save IV movaps xmm6,XMMWORD[16+rsp] movaps xmm7,XMMWORD[32+rsp] movaps xmm8,XMMWORD[48+rsp] @@ -1124,7 +1124,7 @@ $L$SEH_begin_vpaes_ctr32_encrypt_blocks: _CET_ENDBR - +; _vpaes_encrypt_core and _vpaes_encrypt_core_2x expect the key in %rdx. xchg rdx,rcx test rcx,rcx jz NEAR $L$ctr32_abort @@ -1140,9 +1140,9 @@ _CET_ENDBR movaps XMMWORD[144+rsp],xmm14 movaps XMMWORD[160+rsp],xmm15 $L$ctr32_body: - movdqu xmm0,XMMWORD[r8] + movdqu xmm0,XMMWORD[r8] ; Load IV. movdqa xmm8,XMMWORD[$L$ctr_add_one] - sub rsi,rdi + sub rsi,rdi ; This allows only incrementing %rdi. call _vpaes_preheat movdqa xmm6,xmm0 pshufb xmm6,XMMWORD[$L$rev_ctr] @@ -1150,9 +1150,9 @@ $L$ctr32_body: test rcx,1 jz NEAR $L$ctr32_prep_loop - - - movdqu xmm7,XMMWORD[rdi] +; Handle one block so the remaining block count is even for +; _vpaes_encrypt_core_2x. + movdqu xmm7,XMMWORD[rdi] ; Load input. call _vpaes_encrypt_core pxor xmm0,xmm7 paddd xmm6,xmm8 @@ -1162,29 +1162,29 @@ $L$ctr32_body: jz NEAR $L$ctr32_done $L$ctr32_prep_loop: - - +; _vpaes_encrypt_core_2x leaves only %xmm14 and %xmm15 as spare +; registers. We maintain two byte-swapped counters in them. movdqa xmm14,xmm6 movdqa xmm15,xmm6 paddd xmm15,xmm8 $L$ctr32_loop: - movdqa xmm1,XMMWORD[$L$rev_ctr] + movdqa xmm1,XMMWORD[$L$rev_ctr] ; Set up counters. movdqa xmm0,xmm14 movdqa xmm6,xmm15 pshufb xmm0,xmm1 pshufb xmm6,xmm1 call _vpaes_encrypt_core_2x - movdqu xmm1,XMMWORD[rdi] + movdqu xmm1,XMMWORD[rdi] ; Load input. movdqu xmm2,XMMWORD[16+rdi] movdqa xmm3,XMMWORD[$L$ctr_add_two] - pxor xmm0,xmm1 + pxor xmm0,xmm1 ; XOR input. pxor xmm6,xmm2 - paddd xmm14,xmm3 + paddd xmm14,xmm3 ; Increment counters. paddd xmm15,xmm3 - movdqu XMMWORD[rdi*1+rsi],xmm0 + movdqu XMMWORD[rdi*1+rsi],xmm0 ; Write output. movdqu XMMWORD[16+rdi*1+rsi],xmm6 - sub rcx,2 + sub rcx,2 ; Advance loop. lea rdi,[32+rdi] jnz NEAR $L$ctr32_loop @@ -1207,135 +1207,135 @@ $L$ctr32_abort: ret $L$SEH_end_vpaes_ctr32_encrypt_blocks: - - - - - - +; # +; # _aes_preheat +; # +; # Fills register %r10 -> .aes_consts (so you can -fPIC) +; # and %xmm9-%xmm15 as specified below. +; # ALIGN 16 _vpaes_preheat: lea r10,[$L$k_s0F] - movdqa xmm10,XMMWORD[((-32))+r10] - movdqa xmm11,XMMWORD[((-16))+r10] - movdqa xmm9,XMMWORD[r10] - movdqa xmm13,XMMWORD[48+r10] - movdqa xmm12,XMMWORD[64+r10] - movdqa xmm15,XMMWORD[80+r10] - movdqa xmm14,XMMWORD[96+r10] + movdqa xmm10,XMMWORD[((-32))+r10] ; .Lk_inv + movdqa xmm11,XMMWORD[((-16))+r10] ; .Lk_inv+16 + movdqa xmm9,XMMWORD[r10] ; .Lk_s0F + movdqa xmm13,XMMWORD[48+r10] ; .Lk_sb1 + movdqa xmm12,XMMWORD[64+r10] ; .Lk_sb1+16 + movdqa xmm15,XMMWORD[80+r10] ; .Lk_sb2 + movdqa xmm14,XMMWORD[96+r10] ; .Lk_sb2+16 ret - - - - - +; ####################################################### +; # ## +; # Constants ## +; # ## +; ####################################################### section .rdata rdata align=8 ALIGN 64 _vpaes_consts: -$L$k_inv: +$L$k_inv: ; inv, inva DQ 0x0E05060F0D080180,0x040703090A0B0C02 DQ 0x01040A060F0B0780,0x030D0E0C02050809 -$L$k_s0F: +$L$k_s0F: ; s0F DQ 0x0F0F0F0F0F0F0F0F,0x0F0F0F0F0F0F0F0F -$L$k_ipt: +$L$k_ipt: ; input transform (lo, hi) DQ 0xC2B2E8985A2A7000,0xCABAE09052227808 DQ 0x4C01307D317C4D00,0xCD80B1FCB0FDCC81 -$L$k_sb1: +$L$k_sb1: ; sb1u, sb1t DQ 0xB19BE18FCB503E00,0xA5DF7A6E142AF544 DQ 0x3618D415FAE22300,0x3BF7CCC10D2ED9EF -$L$k_sb2: +$L$k_sb2: ; sb2u, sb2t DQ 0xE27A93C60B712400,0x5EB7E955BC982FCD DQ 0x69EB88400AE12900,0xC2A163C8AB82234A -$L$k_sbo: +$L$k_sbo: ; sbou, sbot DQ 0xD0D26D176FBDC700,0x15AABF7AC502A878 DQ 0xCFE474A55FBB6A00,0x8E1E90D1412B35FA -$L$k_mc_forward: +$L$k_mc_forward: ; mc_forward DQ 0x0407060500030201,0x0C0F0E0D080B0A09 DQ 0x080B0A0904070605,0x000302010C0F0E0D DQ 0x0C0F0E0D080B0A09,0x0407060500030201 DQ 0x000302010C0F0E0D,0x080B0A0904070605 -$L$k_mc_backward: +$L$k_mc_backward: ; mc_backward DQ 0x0605040702010003,0x0E0D0C0F0A09080B DQ 0x020100030E0D0C0F,0x0A09080B06050407 DQ 0x0E0D0C0F0A09080B,0x0605040702010003 DQ 0x0A09080B06050407,0x020100030E0D0C0F -$L$k_sr: +$L$k_sr: ; sr DQ 0x0706050403020100,0x0F0E0D0C0B0A0908 DQ 0x030E09040F0A0500,0x0B06010C07020D08 DQ 0x0F060D040B020900,0x070E050C030A0108 DQ 0x0B0E0104070A0D00,0x0306090C0F020508 -$L$k_rcon: +$L$k_rcon: ; rcon DQ 0x1F8391B9AF9DEEB6,0x702A98084D7C7D81 -$L$k_s63: +$L$k_s63: ; s63: all equal to 0x63 transformed DQ 0x5B5B5B5B5B5B5B5B,0x5B5B5B5B5B5B5B5B -$L$k_opt: +$L$k_opt: ; output transform DQ 0xFF9F4929D6B66000,0xF7974121DEBE6808 DQ 0x01EDBD5150BCEC00,0xE10D5DB1B05C0CE0 -$L$k_deskew: +$L$k_deskew: ; deskew tables: inverts the sbox's "skew" DQ 0x07E4A34047A4E300,0x1DFEB95A5DBEF91A DQ 0x5F36B5DC83EA6900,0x2841C2ABF49D1E77 - - - - -$L$k_dksd: +; # +; # Decryption stuff +; # Key schedule constants +; # +$L$k_dksd: ; decryption key schedule: invskew x*D DQ 0xFEB91A5DA3E44700,0x0740E3A45A1DBEF9 DQ 0x41C277F4B5368300,0x5FDC69EAAB289D1E -$L$k_dksb: +$L$k_dksb: ; decryption key schedule: invskew x*B DQ 0x9A4FCA1F8550D500,0x03D653861CC94C99 DQ 0x115BEDA7B6FC4A00,0xD993256F7E3482C8 -$L$k_dkse: +$L$k_dkse: ; decryption key schedule: invskew x*E + 0x63 DQ 0xD5031CCA1FC9D600,0x53859A4C994F5086 DQ 0xA23196054FDC7BE8,0xCD5EF96A20B31487 -$L$k_dks9: +$L$k_dks9: ; decryption key schedule: invskew x*9 DQ 0xB6116FC87ED9A700,0x4AED933482255BFC DQ 0x4576516227143300,0x8BB89FACE9DAFDCE - - - - -$L$k_dipt: +; # +; # Decryption stuff +; # Round function constants +; # +$L$k_dipt: ; decryption input transform DQ 0x0F505B040B545F00,0x154A411E114E451A DQ 0x86E383E660056500,0x12771772F491F194 -$L$k_dsb9: +$L$k_dsb9: ; decryption sbox output *9*u, *9*t DQ 0x851C03539A86D600,0xCAD51F504F994CC9 DQ 0xC03B1789ECD74900,0x725E2C9EB2FBA565 -$L$k_dsbd: +$L$k_dsbd: ; decryption sbox output *D*u, *D*t DQ 0x7D57CCDFE6B1A200,0xF56E9B13882A4439 DQ 0x3CE2FAF724C6CB00,0x2931180D15DEEFD3 -$L$k_dsbb: +$L$k_dsbb: ; decryption sbox output *B*u, *B*t DQ 0xD022649296B44200,0x602646F6B0F2D404 DQ 0xC19498A6CD596700,0xF3FF0C3E3255AA6B -$L$k_dsbe: +$L$k_dsbe: ; decryption sbox output *E*u, *E*t DQ 0x46F2929626D4D000,0x2242600464B4F6B0 DQ 0x0C55A6CDFFAAC100,0x9467F36B98593E32 -$L$k_dsbo: +$L$k_dsbo: ; decryption sbox final output DQ 0x1387EA537EF94000,0xC7AA6DB9D4943E2D DQ 0x12D7560F93441D00,0xCA4B8159D8C58E9C - +; .Lrev_ctr is a permutation which byte-swaps the counter portion of the IV. $L$rev_ctr: DQ 0x0706050403020100,0x0c0d0e0f0b0a0908 - - +; .Lctr_add_* may be added to a byte-swapped xmm register to increment the +; counter. The register must be byte-swapped again to form the actual input. $L$ctr_add_one: DQ 0x0000000000000000,0x0000000100000000 $L$ctr_add_two: @@ -1348,7 +1348,7 @@ $L$ctr_add_two: DB 85,110,105,118,101,114,115,105,116,121,41,0 ALIGN 64 -section .text +section .text code align=64 EXTERN __imp_RtlVirtualUnwind @@ -1365,57 +1365,57 @@ se_handler: pushfq sub rsp,64 - mov rax,QWORD[120+r8] - mov rbx,QWORD[248+r8] + mov rax,QWORD[120+r8] ; pull context->Rax + mov rbx,QWORD[248+r8] ; pull context->Rip - mov rsi,QWORD[8+r9] - mov r11,QWORD[56+r9] + mov rsi,QWORD[8+r9] ; disp->ImageBase + mov r11,QWORD[56+r9] ; disp->HandlerData - mov r10d,DWORD[r11] - lea r10,[r10*1+rsi] - cmp rbx,r10 + mov r10d,DWORD[r11] ; HandlerData[0] + lea r10,[r10*1+rsi] ; prologue label + cmp rbx,r10 ; context->RipRsp - mov r10d,DWORD[4+r11] - lea r10,[r10*1+rsi] - cmp rbx,r10 + mov r10d,DWORD[4+r11] ; HandlerData[1] + lea r10,[r10*1+rsi] ; epilogue label + cmp rbx,r10 ; context->Rip>=epilogue label jae NEAR $L$in_prologue - lea rsi,[16+rax] - lea rdi,[512+r8] - mov ecx,20 - DD 0xa548f3fc - lea rax,[184+rax] + lea rsi,[16+rax] ; %xmm save area + lea rdi,[512+r8] ; &context.Xmm6 + mov ecx,20 ; 10*sizeof(%xmm0)/sizeof(%rax) + DD 0xa548f3fc ; cld; rep movsq + lea rax,[184+rax] ; adjust stack pointer $L$in_prologue: mov rdi,QWORD[8+rax] mov rsi,QWORD[16+rax] - mov QWORD[152+r8],rax - mov QWORD[168+r8],rsi - mov QWORD[176+r8],rdi + mov QWORD[152+r8],rax ; restore context->Rsp + mov QWORD[168+r8],rsi ; restore context->Rsi + mov QWORD[176+r8],rdi ; restore context->Rdi - mov rdi,QWORD[40+r9] - mov rsi,r8 - mov ecx,154 - DD 0xa548f3fc + mov rdi,QWORD[40+r9] ; disp->ContextRecord + mov rsi,r8 ; context + mov ecx,154 ; sizeof(CONTEXT) + DD 0xa548f3fc ; cld; rep movsq mov rsi,r9 - xor rcx,rcx - mov rdx,QWORD[8+rsi] - mov r8,QWORD[rsi] - mov r9,QWORD[16+rsi] - mov r10,QWORD[40+rsi] - lea r11,[56+rsi] - lea r12,[24+rsi] - mov QWORD[32+rsp],r10 - mov QWORD[40+rsp],r11 - mov QWORD[48+rsp],r12 - mov QWORD[56+rsp],rcx + xor rcx,rcx ; arg1, UNW_FLAG_NHANDLER + mov rdx,QWORD[8+rsi] ; arg2, disp->ImageBase + mov r8,QWORD[rsi] ; arg3, disp->ControlPc + mov r9,QWORD[16+rsi] ; arg4, disp->FunctionEntry + mov r10,QWORD[40+rsi] ; disp->ContextRecord + lea r11,[56+rsi] ; &disp->HandlerData + lea r12,[24+rsi] ; &disp->EstablisherFrame + mov QWORD[32+rsp],r10 ; arg5 + mov QWORD[40+rsp],r11 ; arg6 + mov QWORD[48+rsp],r12 ; arg7 + mov QWORD[56+rsp],rcx ; arg8, (NULL) call QWORD[__imp_RtlVirtualUnwind] - mov eax,1 + mov eax,1 ; ExceptionContinueSearch add rsp,64 popfq pop r15 @@ -1460,27 +1460,27 @@ ALIGN 8 $L$SEH_info_vpaes_set_encrypt_key: DB 9,0,0,0 DD se_handler wrt ..imagebase - DD $L$enc_key_body wrt ..imagebase,$L$enc_key_epilogue wrt ..imagebase + DD $L$enc_key_body wrt ..imagebase,$L$enc_key_epilogue wrt ..imagebase ; HandlerData[] $L$SEH_info_vpaes_set_decrypt_key: DB 9,0,0,0 DD se_handler wrt ..imagebase - DD $L$dec_key_body wrt ..imagebase,$L$dec_key_epilogue wrt ..imagebase + DD $L$dec_key_body wrt ..imagebase,$L$dec_key_epilogue wrt ..imagebase ; HandlerData[] $L$SEH_info_vpaes_encrypt: DB 9,0,0,0 DD se_handler wrt ..imagebase - DD $L$enc_body wrt ..imagebase,$L$enc_epilogue wrt ..imagebase + DD $L$enc_body wrt ..imagebase,$L$enc_epilogue wrt ..imagebase ; HandlerData[] $L$SEH_info_vpaes_decrypt: DB 9,0,0,0 DD se_handler wrt ..imagebase - DD $L$dec_body wrt ..imagebase,$L$dec_epilogue wrt ..imagebase + DD $L$dec_body wrt ..imagebase,$L$dec_epilogue wrt ..imagebase ; HandlerData[] $L$SEH_info_vpaes_cbc_encrypt: DB 9,0,0,0 DD se_handler wrt ..imagebase - DD $L$cbc_body wrt ..imagebase,$L$cbc_epilogue wrt ..imagebase + DD $L$cbc_body wrt ..imagebase,$L$cbc_epilogue wrt ..imagebase ; HandlerData[] $L$SEH_info_vpaes_ctr32_encrypt_blocks: DB 9,0,0,0 DD se_handler wrt ..imagebase - DD $L$ctr32_body wrt ..imagebase,$L$ctr32_epilogue wrt ..imagebase + DD $L$ctr32_body wrt ..imagebase,$L$ctr32_epilogue wrt ..imagebase ; HandlerData[] %else ; Work around https://bugzilla.nasm.us/show_bug.cgi?id=3392738 ret diff --git a/third_party/boringssl/gen/bcm/x86_64-mont-apple.S b/third_party/boringssl/gen/bcm/x86_64-mont-apple.S index 27a168d7e..714cd122e 100644 --- a/third_party/boringssl/gen/bcm/x86_64-mont-apple.S +++ b/third_party/boringssl/gen/bcm/x86_64-mont-apple.S @@ -31,18 +31,18 @@ _CET_ENDBR negq %r9 movq %rsp,%r11 - leaq -16(%rsp,%r9,8),%r10 - negq %r9 - andq $-1024,%r10 - - - - - - - - - + leaq -16(%rsp,%r9,8),%r10 // future alloca(8*(num+2)) + negq %r9 // restore %r9 + andq $-1024,%r10 // minimize TLB usage + +// An OS-agnostic version of __chkstk. +// +// Some OSes (Windows) insist on stack being "wired" to +// physical memory in strictly sequential manner, i.e. if stack +// allocation spans two pages, then reference to farmost one can +// be punishable by SEGV. But page walking can do good even on +// other OSes, because it guarantees that villain thread hits +// the guard page before it can make damage to innocent one... subq %r10,%r11 andq $-4096,%r11 leaq (%r10,%r11,1),%rsp @@ -59,32 +59,32 @@ L$mul_page_walk: ja L$mul_page_walk L$mul_page_walk_done: - movq %rax,8(%rsp,%r9,8) + movq %rax,8(%rsp,%r9,8) // tp[num+1]=%rsp L$mul_body: - movq %rdx,%r12 - movq (%r8),%r8 - movq (%r12),%rbx + movq %rdx,%r12 // reassign %rdx + movq (%r8),%r8 // pull n0[0] value + movq (%r12),%rbx // m0=bp[0] movq (%rsi),%rax - xorq %r14,%r14 - xorq %r15,%r15 + xorq %r14,%r14 // i=0 + xorq %r15,%r15 // j=0 movq %r8,%rbp - mulq %rbx + mulq %rbx // ap[0]*bp[0] movq %rax,%r10 movq (%rcx),%rax - imulq %r10,%rbp + imulq %r10,%rbp // "tp[0]"*n0 movq %rdx,%r11 - mulq %rbp - addq %rax,%r10 + mulq %rbp // np[0]*m1 + addq %rax,%r10 // discarded movq 8(%rsi),%rax adcq $0,%rdx movq %rdx,%r13 - leaq 1(%r15),%r15 + leaq 1(%r15),%r15 // j++ jmp L$1st_enter .p2align 4 @@ -92,30 +92,30 @@ L$1st: addq %rax,%r13 movq (%rsi,%r15,8),%rax adcq $0,%rdx - addq %r11,%r13 + addq %r11,%r13 // np[j]*m1+ap[j]*bp[0] movq %r10,%r11 adcq $0,%rdx - movq %r13,-16(%rsp,%r15,8) + movq %r13,-16(%rsp,%r15,8) // tp[j-1] movq %rdx,%r13 L$1st_enter: - mulq %rbx + mulq %rbx // ap[j]*bp[0] addq %rax,%r11 movq (%rcx,%r15,8),%rax adcq $0,%rdx - leaq 1(%r15),%r15 + leaq 1(%r15),%r15 // j++ movq %rdx,%r10 - mulq %rbp + mulq %rbp // np[j]*m1 cmpq %r9,%r15 jne L$1st addq %rax,%r13 - movq (%rsi),%rax + movq (%rsi),%rax // ap[0] adcq $0,%rdx - addq %r11,%r13 + addq %r11,%r13 // np[j]*m1+ap[j]*bp[0] adcq $0,%rdx - movq %r13,-16(%rsp,%r15,8) + movq %r13,-16(%rsp,%r15,8) // tp[j-1] movq %rdx,%r13 movq %r10,%r11 @@ -123,32 +123,32 @@ L$1st_enter: addq %r11,%r13 adcq $0,%rdx movq %r13,-8(%rsp,%r9,8) - movq %rdx,(%rsp,%r9,8) + movq %rdx,(%rsp,%r9,8) // store upmost overflow bit - leaq 1(%r14),%r14 + leaq 1(%r14),%r14 // i++ jmp L$outer .p2align 4 L$outer: - movq (%r12,%r14,8),%rbx - xorq %r15,%r15 + movq (%r12,%r14,8),%rbx // m0=bp[i] + xorq %r15,%r15 // j=0 movq %r8,%rbp movq (%rsp),%r10 - mulq %rbx - addq %rax,%r10 + mulq %rbx // ap[0]*bp[i] + addq %rax,%r10 // ap[0]*bp[i]+tp[0] movq (%rcx),%rax adcq $0,%rdx - imulq %r10,%rbp + imulq %r10,%rbp // tp[0]*n0 movq %rdx,%r11 - mulq %rbp - addq %rax,%r10 + mulq %rbp // np[0]*m1 + addq %rax,%r10 // discarded movq 8(%rsi),%rax adcq $0,%rdx - movq 8(%rsp),%r10 + movq 8(%rsp),%r10 // tp[1] movq %rdx,%r13 - leaq 1(%r15),%r15 + leaq 1(%r15),%r15 // j++ jmp L$inner_enter .p2align 4 @@ -156,80 +156,80 @@ L$inner: addq %rax,%r13 movq (%rsi,%r15,8),%rax adcq $0,%rdx - addq %r10,%r13 + addq %r10,%r13 // np[j]*m1+ap[j]*bp[i]+tp[j] movq (%rsp,%r15,8),%r10 adcq $0,%rdx - movq %r13,-16(%rsp,%r15,8) + movq %r13,-16(%rsp,%r15,8) // tp[j-1] movq %rdx,%r13 L$inner_enter: - mulq %rbx + mulq %rbx // ap[j]*bp[i] addq %rax,%r11 movq (%rcx,%r15,8),%rax adcq $0,%rdx - addq %r11,%r10 + addq %r11,%r10 // ap[j]*bp[i]+tp[j] movq %rdx,%r11 adcq $0,%r11 - leaq 1(%r15),%r15 + leaq 1(%r15),%r15 // j++ - mulq %rbp + mulq %rbp // np[j]*m1 cmpq %r9,%r15 jne L$inner addq %rax,%r13 - movq (%rsi),%rax + movq (%rsi),%rax // ap[0] adcq $0,%rdx - addq %r10,%r13 + addq %r10,%r13 // np[j]*m1+ap[j]*bp[i]+tp[j] movq (%rsp,%r15,8),%r10 adcq $0,%rdx - movq %r13,-16(%rsp,%r15,8) + movq %r13,-16(%rsp,%r15,8) // tp[j-1] movq %rdx,%r13 xorq %rdx,%rdx addq %r11,%r13 adcq $0,%rdx - addq %r10,%r13 + addq %r10,%r13 // pull upmost overflow bit adcq $0,%rdx movq %r13,-8(%rsp,%r9,8) - movq %rdx,(%rsp,%r9,8) + movq %rdx,(%rsp,%r9,8) // store upmost overflow bit - leaq 1(%r14),%r14 + leaq 1(%r14),%r14 // i++ cmpq %r9,%r14 jb L$outer - xorq %r14,%r14 - movq (%rsp),%rax - movq %r9,%r15 + xorq %r14,%r14 // i=0 and clear CF + movq (%rsp),%rax // tp[0] + movq %r9,%r15 // j=num .p2align 4 L$sub: sbbq (%rcx,%r14,8),%rax - movq %rax,(%rdi,%r14,8) - movq 8(%rsp,%r14,8),%rax - leaq 1(%r14),%r14 - decq %r15 + movq %rax,(%rdi,%r14,8) // rp[i]=tp[i]-np[i] + movq 8(%rsp,%r14,8),%rax // tp[i+1] + leaq 1(%r14),%r14 // i++ + decq %r15 // doesn't affect CF jnz L$sub - sbbq $0,%rax + sbbq $0,%rax // handle upmost overflow bit movq $-1,%rbx - xorq %rax,%rbx + xorq %rax,%rbx // not %rax xorq %r14,%r14 - movq %r9,%r15 + movq %r9,%r15 // j=num -L$copy: +L$copy: // conditional copy movq (%rdi,%r14,8),%rcx movq (%rsp,%r14,8),%rdx andq %rbx,%rcx andq %rax,%rdx - movq %r9,(%rsp,%r14,8) + movq %r9,(%rsp,%r14,8) // zap temporary vector orq %rcx,%rdx - movq %rdx,(%rdi,%r14,8) + movq %rdx,(%rdi,%r14,8) // rp[i]=tp[i] leaq 1(%r14),%r14 subq $1,%r15 jnz L$copy - movq 8(%rsp,%r9,8),%rsi - + movq 8(%rsp,%r9,8),%rsi // restore %rsp +// No return value movq -48(%rsi),%r15 movq -40(%rsi),%r14 @@ -252,6 +252,7 @@ L$mul_epilogue: .private_extern _bn_mul4x_mont .p2align 4 +.alt_entry _bn_mul4x_mont _bn_mul4x_mont: _CET_ENDBR @@ -273,9 +274,9 @@ _CET_ENDBR negq %r9 movq %rsp,%r11 - leaq -32(%rsp,%r9,8),%r10 - negq %r9 - andq $-1024,%r10 + leaq -32(%rsp,%r9,8),%r10 // future alloca(8*(num+4)) + negq %r9 // restore + andq $-1024,%r10 // minimize TLB usage subq %r10,%r11 andq $-4096,%r11 @@ -292,28 +293,28 @@ L$mul4x_page_walk: ja L$mul4x_page_walk L$mul4x_page_walk_done: - movq %rax,8(%rsp,%r9,8) + movq %rax,8(%rsp,%r9,8) // tp[num+1]=%rsp L$mul4x_body: - movq %rdi,16(%rsp,%r9,8) - movq %rdx,%r12 - movq (%r8),%r8 - movq (%r12),%rbx + movq %rdi,16(%rsp,%r9,8) // tp[num+2]=%rdi + movq %rdx,%r12 // reassign %r12 + movq (%r8),%r8 // pull n0[0] value + movq (%r12),%rbx // m0=bp[0] movq (%rsi),%rax - xorq %r14,%r14 - xorq %r15,%r15 + xorq %r14,%r14 // i=0 + xorq %r15,%r15 // j=0 movq %r8,%rbp - mulq %rbx + mulq %rbx // ap[0]*bp[0] movq %rax,%r10 movq (%rcx),%rax - imulq %r10,%rbp + imulq %r10,%rbp // "tp[0]"*n0 movq %rdx,%r11 - mulq %rbp - addq %rax,%r10 + mulq %rbp // np[0]*m1 + addq %rax,%r10 // discarded movq 8(%rsi),%rax adcq $0,%rdx movq %rdx,%rdi @@ -329,171 +330,171 @@ L$mul4x_body: movq 16(%rsi),%rax adcq $0,%rdx addq %r11,%rdi - leaq 4(%r15),%r15 + leaq 4(%r15),%r15 // j++ adcq $0,%rdx movq %rdi,(%rsp) movq %rdx,%r13 jmp L$1st4x .p2align 4 L$1st4x: - mulq %rbx + mulq %rbx // ap[j]*bp[0] addq %rax,%r10 movq -16(%rcx,%r15,8),%rax adcq $0,%rdx movq %rdx,%r11 - mulq %rbp + mulq %rbp // np[j]*m1 addq %rax,%r13 movq -8(%rsi,%r15,8),%rax adcq $0,%rdx - addq %r10,%r13 + addq %r10,%r13 // np[j]*m1+ap[j]*bp[0] adcq $0,%rdx - movq %r13,-24(%rsp,%r15,8) + movq %r13,-24(%rsp,%r15,8) // tp[j-1] movq %rdx,%rdi - mulq %rbx + mulq %rbx // ap[j]*bp[0] addq %rax,%r11 movq -8(%rcx,%r15,8),%rax adcq $0,%rdx movq %rdx,%r10 - mulq %rbp + mulq %rbp // np[j]*m1 addq %rax,%rdi movq (%rsi,%r15,8),%rax adcq $0,%rdx - addq %r11,%rdi + addq %r11,%rdi // np[j]*m1+ap[j]*bp[0] adcq $0,%rdx - movq %rdi,-16(%rsp,%r15,8) + movq %rdi,-16(%rsp,%r15,8) // tp[j-1] movq %rdx,%r13 - mulq %rbx + mulq %rbx // ap[j]*bp[0] addq %rax,%r10 movq (%rcx,%r15,8),%rax adcq $0,%rdx movq %rdx,%r11 - mulq %rbp + mulq %rbp // np[j]*m1 addq %rax,%r13 movq 8(%rsi,%r15,8),%rax adcq $0,%rdx - addq %r10,%r13 + addq %r10,%r13 // np[j]*m1+ap[j]*bp[0] adcq $0,%rdx - movq %r13,-8(%rsp,%r15,8) + movq %r13,-8(%rsp,%r15,8) // tp[j-1] movq %rdx,%rdi - mulq %rbx + mulq %rbx // ap[j]*bp[0] addq %rax,%r11 movq 8(%rcx,%r15,8),%rax adcq $0,%rdx - leaq 4(%r15),%r15 + leaq 4(%r15),%r15 // j++ movq %rdx,%r10 - mulq %rbp + mulq %rbp // np[j]*m1 addq %rax,%rdi movq -16(%rsi,%r15,8),%rax adcq $0,%rdx - addq %r11,%rdi + addq %r11,%rdi // np[j]*m1+ap[j]*bp[0] adcq $0,%rdx - movq %rdi,-32(%rsp,%r15,8) + movq %rdi,-32(%rsp,%r15,8) // tp[j-1] movq %rdx,%r13 cmpq %r9,%r15 jb L$1st4x - mulq %rbx + mulq %rbx // ap[j]*bp[0] addq %rax,%r10 movq -16(%rcx,%r15,8),%rax adcq $0,%rdx movq %rdx,%r11 - mulq %rbp + mulq %rbp // np[j]*m1 addq %rax,%r13 movq -8(%rsi,%r15,8),%rax adcq $0,%rdx - addq %r10,%r13 + addq %r10,%r13 // np[j]*m1+ap[j]*bp[0] adcq $0,%rdx - movq %r13,-24(%rsp,%r15,8) + movq %r13,-24(%rsp,%r15,8) // tp[j-1] movq %rdx,%rdi - mulq %rbx + mulq %rbx // ap[j]*bp[0] addq %rax,%r11 movq -8(%rcx,%r15,8),%rax adcq $0,%rdx movq %rdx,%r10 - mulq %rbp + mulq %rbp // np[j]*m1 addq %rax,%rdi - movq (%rsi),%rax + movq (%rsi),%rax // ap[0] adcq $0,%rdx - addq %r11,%rdi + addq %r11,%rdi // np[j]*m1+ap[j]*bp[0] adcq $0,%rdx - movq %rdi,-16(%rsp,%r15,8) + movq %rdi,-16(%rsp,%r15,8) // tp[j-1] movq %rdx,%r13 xorq %rdi,%rdi addq %r10,%r13 adcq $0,%rdi movq %r13,-8(%rsp,%r15,8) - movq %rdi,(%rsp,%r15,8) + movq %rdi,(%rsp,%r15,8) // store upmost overflow bit - leaq 1(%r14),%r14 + leaq 1(%r14),%r14 // i++ .p2align 2 L$outer4x: - movq (%r12,%r14,8),%rbx - xorq %r15,%r15 + movq (%r12,%r14,8),%rbx // m0=bp[i] + xorq %r15,%r15 // j=0 movq (%rsp),%r10 movq %r8,%rbp - mulq %rbx - addq %rax,%r10 + mulq %rbx // ap[0]*bp[i] + addq %rax,%r10 // ap[0]*bp[i]+tp[0] movq (%rcx),%rax adcq $0,%rdx - imulq %r10,%rbp + imulq %r10,%rbp // tp[0]*n0 movq %rdx,%r11 - mulq %rbp - addq %rax,%r10 + mulq %rbp // np[0]*m1 + addq %rax,%r10 // "%r13", discarded movq 8(%rsi),%rax adcq $0,%rdx movq %rdx,%rdi - mulq %rbx + mulq %rbx // ap[j]*bp[i] addq %rax,%r11 movq 8(%rcx),%rax adcq $0,%rdx - addq 8(%rsp),%r11 + addq 8(%rsp),%r11 // +tp[1] adcq $0,%rdx movq %rdx,%r10 - mulq %rbp + mulq %rbp // np[j]*m1 addq %rax,%rdi movq 16(%rsi),%rax adcq $0,%rdx - addq %r11,%rdi - leaq 4(%r15),%r15 + addq %r11,%rdi // np[j]*m1+ap[j]*bp[i]+tp[j] + leaq 4(%r15),%r15 // j+=2 adcq $0,%rdx - movq %rdi,(%rsp) + movq %rdi,(%rsp) // tp[j-1] movq %rdx,%r13 jmp L$inner4x .p2align 4 L$inner4x: - mulq %rbx + mulq %rbx // ap[j]*bp[i] addq %rax,%r10 movq -16(%rcx,%r15,8),%rax adcq $0,%rdx - addq -16(%rsp,%r15,8),%r10 + addq -16(%rsp,%r15,8),%r10 // ap[j]*bp[i]+tp[j] adcq $0,%rdx movq %rdx,%r11 - mulq %rbp + mulq %rbp // np[j]*m1 addq %rax,%r13 movq -8(%rsi,%r15,8),%rax adcq $0,%rdx addq %r10,%r13 adcq $0,%rdx - movq %r13,-24(%rsp,%r15,8) + movq %r13,-24(%rsp,%r15,8) // tp[j-1] movq %rdx,%rdi - mulq %rbx + mulq %rbx // ap[j]*bp[i] addq %rax,%r11 movq -8(%rcx,%r15,8),%rax adcq $0,%rdx @@ -501,148 +502,148 @@ L$inner4x: adcq $0,%rdx movq %rdx,%r10 - mulq %rbp + mulq %rbp // np[j]*m1 addq %rax,%rdi movq (%rsi,%r15,8),%rax adcq $0,%rdx addq %r11,%rdi adcq $0,%rdx - movq %rdi,-16(%rsp,%r15,8) + movq %rdi,-16(%rsp,%r15,8) // tp[j-1] movq %rdx,%r13 - mulq %rbx + mulq %rbx // ap[j]*bp[i] addq %rax,%r10 movq (%rcx,%r15,8),%rax adcq $0,%rdx - addq (%rsp,%r15,8),%r10 + addq (%rsp,%r15,8),%r10 // ap[j]*bp[i]+tp[j] adcq $0,%rdx movq %rdx,%r11 - mulq %rbp + mulq %rbp // np[j]*m1 addq %rax,%r13 movq 8(%rsi,%r15,8),%rax adcq $0,%rdx addq %r10,%r13 adcq $0,%rdx - movq %r13,-8(%rsp,%r15,8) + movq %r13,-8(%rsp,%r15,8) // tp[j-1] movq %rdx,%rdi - mulq %rbx + mulq %rbx // ap[j]*bp[i] addq %rax,%r11 movq 8(%rcx,%r15,8),%rax adcq $0,%rdx addq 8(%rsp,%r15,8),%r11 adcq $0,%rdx - leaq 4(%r15),%r15 + leaq 4(%r15),%r15 // j++ movq %rdx,%r10 - mulq %rbp + mulq %rbp // np[j]*m1 addq %rax,%rdi movq -16(%rsi,%r15,8),%rax adcq $0,%rdx addq %r11,%rdi adcq $0,%rdx - movq %rdi,-32(%rsp,%r15,8) + movq %rdi,-32(%rsp,%r15,8) // tp[j-1] movq %rdx,%r13 cmpq %r9,%r15 jb L$inner4x - mulq %rbx + mulq %rbx // ap[j]*bp[i] addq %rax,%r10 movq -16(%rcx,%r15,8),%rax adcq $0,%rdx - addq -16(%rsp,%r15,8),%r10 + addq -16(%rsp,%r15,8),%r10 // ap[j]*bp[i]+tp[j] adcq $0,%rdx movq %rdx,%r11 - mulq %rbp + mulq %rbp // np[j]*m1 addq %rax,%r13 movq -8(%rsi,%r15,8),%rax adcq $0,%rdx addq %r10,%r13 adcq $0,%rdx - movq %r13,-24(%rsp,%r15,8) + movq %r13,-24(%rsp,%r15,8) // tp[j-1] movq %rdx,%rdi - mulq %rbx + mulq %rbx // ap[j]*bp[i] addq %rax,%r11 movq -8(%rcx,%r15,8),%rax adcq $0,%rdx addq -8(%rsp,%r15,8),%r11 adcq $0,%rdx - leaq 1(%r14),%r14 + leaq 1(%r14),%r14 // i++ movq %rdx,%r10 - mulq %rbp + mulq %rbp // np[j]*m1 addq %rax,%rdi - movq (%rsi),%rax + movq (%rsi),%rax // ap[0] adcq $0,%rdx addq %r11,%rdi adcq $0,%rdx - movq %rdi,-16(%rsp,%r15,8) + movq %rdi,-16(%rsp,%r15,8) // tp[j-1] movq %rdx,%r13 xorq %rdi,%rdi addq %r10,%r13 adcq $0,%rdi - addq (%rsp,%r9,8),%r13 + addq (%rsp,%r9,8),%r13 // pull upmost overflow bit adcq $0,%rdi movq %r13,-8(%rsp,%r15,8) - movq %rdi,(%rsp,%r15,8) + movq %rdi,(%rsp,%r15,8) // store upmost overflow bit cmpq %r9,%r14 jb L$outer4x - movq 16(%rsp,%r9,8),%rdi + movq 16(%rsp,%r9,8),%rdi // restore %rdi leaq -4(%r9),%r15 - movq 0(%rsp),%rax - movq 8(%rsp),%rdx - shrq $2,%r15 - leaq (%rsp),%rsi - xorq %r14,%r14 + movq 0(%rsp),%rax // tp[0] + movq 8(%rsp),%rdx // tp[1] + shrq $2,%r15 // j=num/4-1 + leaq (%rsp),%rsi // borrow ap for tp + xorq %r14,%r14 // i=0 and clear CF subq 0(%rcx),%rax - movq 16(%rsi),%rbx - movq 24(%rsi),%rbp + movq 16(%rsi),%rbx // tp[2] + movq 24(%rsi),%rbp // tp[3] sbbq 8(%rcx),%rdx L$sub4x: - movq %rax,0(%rdi,%r14,8) - movq %rdx,8(%rdi,%r14,8) + movq %rax,0(%rdi,%r14,8) // rp[i]=tp[i]-np[i] + movq %rdx,8(%rdi,%r14,8) // rp[i]=tp[i]-np[i] sbbq 16(%rcx,%r14,8),%rbx - movq 32(%rsi,%r14,8),%rax + movq 32(%rsi,%r14,8),%rax // tp[i+1] movq 40(%rsi,%r14,8),%rdx sbbq 24(%rcx,%r14,8),%rbp - movq %rbx,16(%rdi,%r14,8) - movq %rbp,24(%rdi,%r14,8) + movq %rbx,16(%rdi,%r14,8) // rp[i]=tp[i]-np[i] + movq %rbp,24(%rdi,%r14,8) // rp[i]=tp[i]-np[i] sbbq 32(%rcx,%r14,8),%rax movq 48(%rsi,%r14,8),%rbx movq 56(%rsi,%r14,8),%rbp sbbq 40(%rcx,%r14,8),%rdx - leaq 4(%r14),%r14 - decq %r15 + leaq 4(%r14),%r14 // i++ + decq %r15 // doesn't affect CF jnz L$sub4x - movq %rax,0(%rdi,%r14,8) - movq 32(%rsi,%r14,8),%rax + movq %rax,0(%rdi,%r14,8) // rp[i]=tp[i]-np[i] + movq 32(%rsi,%r14,8),%rax // load overflow bit sbbq 16(%rcx,%r14,8),%rbx - movq %rdx,8(%rdi,%r14,8) + movq %rdx,8(%rdi,%r14,8) // rp[i]=tp[i]-np[i] sbbq 24(%rcx,%r14,8),%rbp - movq %rbx,16(%rdi,%r14,8) + movq %rbx,16(%rdi,%r14,8) // rp[i]=tp[i]-np[i] - sbbq $0,%rax - movq %rbp,24(%rdi,%r14,8) + sbbq $0,%rax // handle upmost overflow bit + movq %rbp,24(%rdi,%r14,8) // rp[i]=tp[i]-np[i] pxor %xmm0,%xmm0 movq %rax,%xmm4 pcmpeqd %xmm5,%xmm5 pshufd $0,%xmm4,%xmm4 movq %r9,%r15 pxor %xmm4,%xmm5 - shrq $2,%r15 - xorl %eax,%eax + shrq $2,%r15 // j=num/4 + xorl %eax,%eax // i=0 jmp L$copy4x .p2align 4 -L$copy4x: +L$copy4x: // conditional copy movdqa (%rsp,%rax,1),%xmm1 movdqu (%rdi,%rax,1),%xmm2 pand %xmm4,%xmm1 @@ -660,9 +661,9 @@ L$copy4x: leaq 32(%rax),%rax decq %r15 jnz L$copy4x - movq 8(%rsp,%r9,8),%rsi - + movq 8(%rsp,%r9,8),%rsi // restore %rsp +// No return value movq -48(%rsi),%r15 movq -40(%rsi),%r14 @@ -681,13 +682,14 @@ L$mul4x_epilogue: ret - - +// see x86_64-mont5 module +// see x86_64-mont5 module .globl _bn_sqr8x_mont .private_extern _bn_sqr8x_mont .p2align 5 +.alt_entry _bn_sqr8x_mont _bn_sqr8x_mont: _CET_ENDBR @@ -709,30 +711,30 @@ _CET_ENDBR L$sqr8x_prologue: movl %r9d,%r10d - shll $3,%r9d - shlq $3+2,%r10 + shll $3,%r9d // convert %r9 to bytes + shlq $3+2,%r10 // 4*%r9 negq %r9 - - - - - +// ############################################################# +// ensure that stack frame doesn't alias with %rsi modulo +// 4096. this is done to allow memory disambiguation logic +// do its job. +// leaq -64(%rsp,%r9,2),%r11 movq %rsp,%rbp - movq (%r8),%r8 + movq (%r8),%r8 // *n0 subq %rsi,%r11 andq $4095,%r11 cmpq %r11,%r10 jb L$sqr8x_sp_alt - subq %r11,%rbp - leaq -64(%rbp,%r9,2),%rbp + subq %r11,%rbp // align with %rsi + leaq -64(%rbp,%r9,2),%rbp // future alloca(frame+2*%r9) jmp L$sqr8x_sp_done .p2align 5 L$sqr8x_sp_alt: - leaq 4096-64(,%r9,2),%r10 - leaq -64(%rbp,%r9,2),%rbp + leaq 4096-64(,%r9,2),%r10 // 4096-frame-2*%r9 + leaq -64(%rbp,%r9,2),%rbp // future alloca(frame+2*%r9) subq %r10,%r11 movq $0,%r10 cmovcq %r10,%r11 @@ -760,41 +762,41 @@ L$sqr8x_page_walk_done: negq %r9 movq %r8,32(%rsp) - movq %rax,40(%rsp) + movq %rax,40(%rsp) // save original %rsp L$sqr8x_body: - movq %rcx,%xmm2 + movq %rcx,%xmm2 // save pointer to modulus pxor %xmm0,%xmm0 - movq %rdi,%xmm1 - movq %r10,%xmm3 + movq %rdi,%xmm1 // save %rdi + movq %r10,%xmm3 // -%r9 testq %rdx,%rdx jz L$sqr8x_nox - call _bn_sqrx8x_internal - - - - + call _bn_sqrx8x_internal // see x86_64-mont5 module +// %rax top-most carry +// %rbp nptr +// %rcx -8*num +// %r8 end of tp[2*num] leaq (%r8,%rcx,1),%rbx movq %rcx,%r9 movq %rcx,%rdx movq %xmm1,%rdi - sarq $3+2,%rcx + sarq $3+2,%rcx // %cf=0 jmp L$sqr8x_sub .p2align 5 L$sqr8x_nox: - call _bn_sqr8x_internal - - - - + call _bn_sqr8x_internal // see x86_64-mont5 module +// %rax top-most carry +// %rbp nptr +// %r8 -8*num +// %rdi end of tp[2*num] leaq (%rdi,%r9,1),%rbx movq %r9,%rcx movq %r9,%rdx movq %xmm1,%rdi - sarq $3+2,%rcx + sarq $3+2,%rcx // %cf=0 jmp L$sqr8x_sub .p2align 5 @@ -814,17 +816,17 @@ L$sqr8x_sub: movq %r14,16(%rdi) movq %r15,24(%rdi) leaq 32(%rdi),%rdi - incq %rcx + incq %rcx // preserves %cf jnz L$sqr8x_sub - sbbq $0,%rax - leaq (%rbx,%r9,1),%rbx - leaq (%rdi,%r9,1),%rdi + sbbq $0,%rax // top-most carry + leaq (%rbx,%r9,1),%rbx // rewind + leaq (%rdi,%r9,1),%rdi // rewind movq %rax,%xmm1 pxor %xmm0,%xmm0 pshufd $0,%xmm1,%xmm1 - movq 40(%rsp),%rsi + movq 40(%rsp),%rsi // restore %rsp jmp L$sqr8x_cond_copy @@ -836,7 +838,7 @@ L$sqr8x_cond_copy: movdqu 0(%rdi),%xmm4 movdqu 16(%rdi),%xmm5 leaq 32(%rdi),%rdi - movdqa %xmm0,-32(%rbx) + movdqa %xmm0,-32(%rbx) // zero tp movdqa %xmm0,-16(%rbx) movdqa %xmm0,-32(%rbx,%rdx,1) movdqa %xmm0,-16(%rbx,%rdx,1) @@ -853,7 +855,7 @@ L$sqr8x_cond_copy: addq $32,%r9 jnz L$sqr8x_cond_copy - +// No return value movq -48(%rsi),%r15 movq -40(%rsi),%r14 @@ -876,6 +878,7 @@ L$sqr8x_epilogue: .private_extern _bn_mulx4x_mont .p2align 5 +.alt_entry _bn_mulx4x_mont _bn_mulx4x_mont: _CET_ENDBR @@ -895,11 +898,11 @@ _CET_ENDBR L$mulx4x_prologue: - shll $3,%r9d + shll $3,%r9d // convert %r9 to bytes xorq %r10,%r10 - subq %r9,%r10 - movq (%r8),%r8 - leaq -72(%rsp,%r10,1),%rbp + subq %r9,%r10 // -%r9 + movq (%r8),%r8 // *n0 + leaq -72(%rsp,%r10,1),%rbp // future alloca(frame+%r9+8) andq $-128,%rbp movq %rsp,%r11 subq %rbp,%r11 @@ -919,62 +922,62 @@ L$mulx4x_page_walk: L$mulx4x_page_walk_done: leaq (%rdx,%r9,1),%r10 - - - - - - - - - - - - - movq %r9,0(%rsp) +// ############################################################# +// Stack layout +// +0 num +// +8 off-loaded &b[i] +// +16 end of b[num] +// +24 saved n0 +// +32 saved rp +// +40 saved %rsp +// +48 inner counter +// +56 +// +64 tmp[num+1] +// + movq %r9,0(%rsp) // save %r9 shrq $5,%r9 - movq %r10,16(%rsp) + movq %r10,16(%rsp) // end of b[num] subq $1,%r9 - movq %r8,24(%rsp) - movq %rdi,32(%rsp) - movq %rax,40(%rsp) + movq %r8,24(%rsp) // save *n0 + movq %rdi,32(%rsp) // save %rdi + movq %rax,40(%rsp) // save original %rsp - movq %r9,48(%rsp) + movq %r9,48(%rsp) // inner counter jmp L$mulx4x_body .p2align 5 L$mulx4x_body: leaq 8(%rdx),%rdi - movq (%rdx),%rdx + movq (%rdx),%rdx // b[0], %rdx==%rdx actually leaq 64+32(%rsp),%rbx movq %rdx,%r9 - mulxq 0(%rsi),%r8,%rax - mulxq 8(%rsi),%r11,%r14 + mulxq 0(%rsi),%r8,%rax // a[0]*b[0] + mulxq 8(%rsi),%r11,%r14 // a[1]*b[0] addq %rax,%r11 - movq %rdi,8(%rsp) - mulxq 16(%rsi),%r12,%r13 + movq %rdi,8(%rsp) // off-load &b[i] + mulxq 16(%rsi),%r12,%r13 // ... adcq %r14,%r12 adcq $0,%r13 - movq %r8,%rdi - imulq 24(%rsp),%r8 - xorq %rbp,%rbp + movq %r8,%rdi // borrow %rdi + imulq 24(%rsp),%r8 // "t[0]"*n0 + xorq %rbp,%rbp // cf=0, of=0 mulxq 24(%rsi),%rax,%r14 movq %r8,%rdx leaq 32(%rsi),%rsi adcxq %rax,%r13 - adcxq %rbp,%r14 + adcxq %rbp,%r14 // cf=0 mulxq 0(%rcx),%rax,%r10 - adcxq %rax,%rdi + adcxq %rax,%rdi // discarded adoxq %r11,%r10 mulxq 8(%rcx),%rax,%r11 adcxq %rax,%r10 adoxq %r12,%r11 -.byte 0xc4,0x62,0xfb,0xf6,0xa1,0x10,0x00,0x00,0x00 - movq 48(%rsp),%rdi +.byte 0xc4,0x62,0xfb,0xf6,0xa1,0x10,0x00,0x00,0x00 // mulx 2*8(%rcx),%rax,%r12 + movq 48(%rsp),%rdi // counter value movq %r10,-32(%rbx) adcxq %rax,%r11 adoxq %r13,%r12 @@ -982,7 +985,7 @@ L$mulx4x_body: movq %r9,%rdx movq %r11,-24(%rbx) adcxq %rax,%r12 - adoxq %rbp,%r15 + adoxq %rbp,%r15 // of=0 leaq 32(%rcx),%rcx movq %r12,-16(%rbx) @@ -990,18 +993,18 @@ L$mulx4x_body: .p2align 5 L$mulx4x_1st: - adcxq %rbp,%r15 - mulxq 0(%rsi),%r10,%rax + adcxq %rbp,%r15 // cf=0, modulo-scheduled + mulxq 0(%rsi),%r10,%rax // a[4]*b[0] adcxq %r14,%r10 - mulxq 8(%rsi),%r11,%r14 + mulxq 8(%rsi),%r11,%r14 // a[5]*b[0] adcxq %rax,%r11 - mulxq 16(%rsi),%r12,%rax + mulxq 16(%rsi),%r12,%rax // ... adcxq %r14,%r12 mulxq 24(%rsi),%r13,%r14 .byte 0x67,0x67 movq %r8,%rdx adcxq %rax,%r13 - adcxq %rbp,%r14 + adcxq %rbp,%r14 // cf=0 leaq 32(%rsi),%rsi leaq 32(%rbx),%rbx @@ -1025,43 +1028,43 @@ L$mulx4x_1st: leaq 32(%rcx),%rcx movq %r13,-16(%rbx) - decq %rdi + decq %rdi // of=0, pass cf jnz L$mulx4x_1st - movq 0(%rsp),%rax - movq 8(%rsp),%rdi - adcq %rbp,%r15 + movq 0(%rsp),%rax // load num + movq 8(%rsp),%rdi // re-load &b[i] + adcq %rbp,%r15 // modulo-scheduled addq %r15,%r14 - sbbq %r15,%r15 + sbbq %r15,%r15 // top-most carry movq %r14,-8(%rbx) jmp L$mulx4x_outer .p2align 5 L$mulx4x_outer: - movq (%rdi),%rdx - leaq 8(%rdi),%rdi - subq %rax,%rsi - movq %r15,(%rbx) + movq (%rdi),%rdx // b[i] + leaq 8(%rdi),%rdi // b++ + subq %rax,%rsi // rewind %rsi + movq %r15,(%rbx) // save top-most carry leaq 64+32(%rsp),%rbx - subq %rax,%rcx + subq %rax,%rcx // rewind %rcx - mulxq 0(%rsi),%r8,%r11 - xorl %ebp,%ebp + mulxq 0(%rsi),%r8,%r11 // a[0]*b[i] + xorl %ebp,%ebp // xor %rbp,%rbp # cf=0, of=0 movq %rdx,%r9 - mulxq 8(%rsi),%r14,%r12 + mulxq 8(%rsi),%r14,%r12 // a[1]*b[i] adoxq -32(%rbx),%r8 adcxq %r14,%r11 - mulxq 16(%rsi),%r15,%r13 + mulxq 16(%rsi),%r15,%r13 // ... adoxq -24(%rbx),%r11 adcxq %r15,%r12 adoxq -16(%rbx),%r12 adcxq %rbp,%r13 adoxq %rbp,%r13 - movq %rdi,8(%rsp) + movq %rdi,8(%rsp) // off-load &b[i] movq %r8,%r15 - imulq 24(%rsp),%r8 - xorl %ebp,%ebp + imulq 24(%rsp),%r8 // "t[0]"*n0 + xorl %ebp,%ebp // xor %rbp,%rbp # cf=0, of=0 mulxq 24(%rsi),%rax,%r14 movq %r8,%rdx @@ -1072,7 +1075,7 @@ L$mulx4x_outer: adoxq %rbp,%r14 mulxq 0(%rcx),%rax,%r10 - adcxq %rax,%r15 + adcxq %rax,%r15 // discarded adoxq %r11,%r10 mulxq 8(%rcx),%rax,%r11 adcxq %rax,%r10 @@ -1086,21 +1089,21 @@ L$mulx4x_outer: movq %r11,-24(%rbx) leaq 32(%rcx),%rcx adcxq %rax,%r12 - adoxq %rbp,%r15 - movq 48(%rsp),%rdi + adoxq %rbp,%r15 // of=0 + movq 48(%rsp),%rdi // counter value movq %r12,-16(%rbx) jmp L$mulx4x_inner .p2align 5 L$mulx4x_inner: - mulxq 0(%rsi),%r10,%rax - adcxq %rbp,%r15 + mulxq 0(%rsi),%r10,%rax // a[4]*b[i] + adcxq %rbp,%r15 // cf=0, modulo-scheduled adoxq %r14,%r10 - mulxq 8(%rsi),%r11,%r14 + mulxq 8(%rsi),%r11,%r14 // a[5]*b[i] adcxq 0(%rbx),%r10 adoxq %rax,%r11 - mulxq 16(%rsi),%r12,%rax + mulxq 16(%rsi),%r12,%rax // ... adcxq 8(%rbx),%r11 adoxq %r14,%r12 mulxq 24(%rsi),%r13,%r14 @@ -1108,10 +1111,10 @@ L$mulx4x_inner: adcxq 16(%rbx),%r12 adoxq %rax,%r13 adcxq 24(%rbx),%r13 - adoxq %rbp,%r14 + adoxq %rbp,%r14 // of=0 leaq 32(%rsi),%rsi leaq 32(%rbx),%rbx - adcxq %rbp,%r14 + adcxq %rbp,%r14 // cf=0 adoxq %r15,%r10 mulxq 0(%rcx),%rax,%r15 @@ -1133,26 +1136,26 @@ L$mulx4x_inner: leaq 32(%rcx),%rcx movq %r13,-16(%rbx) - decq %rdi + decq %rdi // of=0, pass cf jnz L$mulx4x_inner - movq 0(%rsp),%rax - movq 8(%rsp),%rdi - adcq %rbp,%r15 - subq 0(%rbx),%rbp + movq 0(%rsp),%rax // load num + movq 8(%rsp),%rdi // re-load &b[i] + adcq %rbp,%r15 // modulo-scheduled + subq 0(%rbx),%rbp // pull top-most carry adcq %r15,%r14 - sbbq %r15,%r15 + sbbq %r15,%r15 // top-most carry movq %r14,-8(%rbx) cmpq 16(%rsp),%rdi jne L$mulx4x_outer leaq 64(%rsp),%rbx - subq %rax,%rcx + subq %rax,%rcx // rewind %rcx negq %r15 movq %rax,%rdx - shrq $3+2,%rax - movq 32(%rsp),%rdi + shrq $3+2,%rax // %cf=0 + movq 32(%rsp),%rdi // restore rp jmp L$mulx4x_sub .p2align 5 @@ -1172,17 +1175,17 @@ L$mulx4x_sub: movq %r13,16(%rdi) movq %r14,24(%rdi) leaq 32(%rdi),%rdi - decq %rax + decq %rax // preserves %cf jnz L$mulx4x_sub - sbbq $0,%r15 + sbbq $0,%r15 // top-most carry leaq 64(%rsp),%rbx - subq %rdx,%rdi + subq %rdx,%rdi // rewind movq %r15,%xmm1 pxor %xmm0,%xmm0 pshufd $0,%xmm1,%xmm1 - movq 40(%rsp),%rsi + movq 40(%rsp),%rsi // restore %rsp jmp L$mulx4x_cond_copy @@ -1194,7 +1197,7 @@ L$mulx4x_cond_copy: movdqu 0(%rdi),%xmm4 movdqu 16(%rdi),%xmm5 leaq 32(%rdi),%rdi - movdqa %xmm0,-32(%rbx) + movdqa %xmm0,-32(%rbx) // zero tp movdqa %xmm0,-16(%rbx) pcmpeqd %xmm1,%xmm0 pand %xmm1,%xmm2 @@ -1211,7 +1214,7 @@ L$mulx4x_cond_copy: movq %rdx,(%rbx) - +// No return value movq -48(%rsi),%r15 movq -40(%rsi),%r14 diff --git a/third_party/boringssl/gen/bcm/x86_64-mont-linux.S b/third_party/boringssl/gen/bcm/x86_64-mont-linux.S index 51c4b6cf4..b8630ea30 100644 --- a/third_party/boringssl/gen/bcm/x86_64-mont-linux.S +++ b/third_party/boringssl/gen/bcm/x86_64-mont-linux.S @@ -31,18 +31,18 @@ _CET_ENDBR negq %r9 movq %rsp,%r11 - leaq -16(%rsp,%r9,8),%r10 - negq %r9 - andq $-1024,%r10 - - - - - - - - - + leaq -16(%rsp,%r9,8),%r10 // future alloca(8*(num+2)) + negq %r9 // restore %r9 + andq $-1024,%r10 // minimize TLB usage + +// An OS-agnostic version of __chkstk. +// +// Some OSes (Windows) insist on stack being "wired" to +// physical memory in strictly sequential manner, i.e. if stack +// allocation spans two pages, then reference to farmost one can +// be punishable by SEGV. But page walking can do good even on +// other OSes, because it guarantees that villain thread hits +// the guard page before it can make damage to innocent one... subq %r10,%r11 andq $-4096,%r11 leaq (%r10,%r11,1),%rsp @@ -59,32 +59,32 @@ _CET_ENDBR ja .Lmul_page_walk .Lmul_page_walk_done: - movq %rax,8(%rsp,%r9,8) + movq %rax,8(%rsp,%r9,8) // tp[num+1]=%rsp .cfi_escape 0x0f,0x0a,0x77,0x08,0x79,0x00,0x38,0x1e,0x22,0x06,0x23,0x08 .Lmul_body: - movq %rdx,%r12 - movq (%r8),%r8 - movq (%r12),%rbx + movq %rdx,%r12 // reassign %rdx + movq (%r8),%r8 // pull n0[0] value + movq (%r12),%rbx // m0=bp[0] movq (%rsi),%rax - xorq %r14,%r14 - xorq %r15,%r15 + xorq %r14,%r14 // i=0 + xorq %r15,%r15 // j=0 movq %r8,%rbp - mulq %rbx + mulq %rbx // ap[0]*bp[0] movq %rax,%r10 movq (%rcx),%rax - imulq %r10,%rbp + imulq %r10,%rbp // "tp[0]"*n0 movq %rdx,%r11 - mulq %rbp - addq %rax,%r10 + mulq %rbp // np[0]*m1 + addq %rax,%r10 // discarded movq 8(%rsi),%rax adcq $0,%rdx movq %rdx,%r13 - leaq 1(%r15),%r15 + leaq 1(%r15),%r15 // j++ jmp .L1st_enter .align 16 @@ -92,30 +92,30 @@ _CET_ENDBR addq %rax,%r13 movq (%rsi,%r15,8),%rax adcq $0,%rdx - addq %r11,%r13 + addq %r11,%r13 // np[j]*m1+ap[j]*bp[0] movq %r10,%r11 adcq $0,%rdx - movq %r13,-16(%rsp,%r15,8) + movq %r13,-16(%rsp,%r15,8) // tp[j-1] movq %rdx,%r13 .L1st_enter: - mulq %rbx + mulq %rbx // ap[j]*bp[0] addq %rax,%r11 movq (%rcx,%r15,8),%rax adcq $0,%rdx - leaq 1(%r15),%r15 + leaq 1(%r15),%r15 // j++ movq %rdx,%r10 - mulq %rbp + mulq %rbp // np[j]*m1 cmpq %r9,%r15 jne .L1st addq %rax,%r13 - movq (%rsi),%rax + movq (%rsi),%rax // ap[0] adcq $0,%rdx - addq %r11,%r13 + addq %r11,%r13 // np[j]*m1+ap[j]*bp[0] adcq $0,%rdx - movq %r13,-16(%rsp,%r15,8) + movq %r13,-16(%rsp,%r15,8) // tp[j-1] movq %rdx,%r13 movq %r10,%r11 @@ -123,32 +123,32 @@ _CET_ENDBR addq %r11,%r13 adcq $0,%rdx movq %r13,-8(%rsp,%r9,8) - movq %rdx,(%rsp,%r9,8) + movq %rdx,(%rsp,%r9,8) // store upmost overflow bit - leaq 1(%r14),%r14 + leaq 1(%r14),%r14 // i++ jmp .Louter .align 16 .Louter: - movq (%r12,%r14,8),%rbx - xorq %r15,%r15 + movq (%r12,%r14,8),%rbx // m0=bp[i] + xorq %r15,%r15 // j=0 movq %r8,%rbp movq (%rsp),%r10 - mulq %rbx - addq %rax,%r10 + mulq %rbx // ap[0]*bp[i] + addq %rax,%r10 // ap[0]*bp[i]+tp[0] movq (%rcx),%rax adcq $0,%rdx - imulq %r10,%rbp + imulq %r10,%rbp // tp[0]*n0 movq %rdx,%r11 - mulq %rbp - addq %rax,%r10 + mulq %rbp // np[0]*m1 + addq %rax,%r10 // discarded movq 8(%rsi),%rax adcq $0,%rdx - movq 8(%rsp),%r10 + movq 8(%rsp),%r10 // tp[1] movq %rdx,%r13 - leaq 1(%r15),%r15 + leaq 1(%r15),%r15 // j++ jmp .Linner_enter .align 16 @@ -156,80 +156,80 @@ _CET_ENDBR addq %rax,%r13 movq (%rsi,%r15,8),%rax adcq $0,%rdx - addq %r10,%r13 + addq %r10,%r13 // np[j]*m1+ap[j]*bp[i]+tp[j] movq (%rsp,%r15,8),%r10 adcq $0,%rdx - movq %r13,-16(%rsp,%r15,8) + movq %r13,-16(%rsp,%r15,8) // tp[j-1] movq %rdx,%r13 .Linner_enter: - mulq %rbx + mulq %rbx // ap[j]*bp[i] addq %rax,%r11 movq (%rcx,%r15,8),%rax adcq $0,%rdx - addq %r11,%r10 + addq %r11,%r10 // ap[j]*bp[i]+tp[j] movq %rdx,%r11 adcq $0,%r11 - leaq 1(%r15),%r15 + leaq 1(%r15),%r15 // j++ - mulq %rbp + mulq %rbp // np[j]*m1 cmpq %r9,%r15 jne .Linner addq %rax,%r13 - movq (%rsi),%rax + movq (%rsi),%rax // ap[0] adcq $0,%rdx - addq %r10,%r13 + addq %r10,%r13 // np[j]*m1+ap[j]*bp[i]+tp[j] movq (%rsp,%r15,8),%r10 adcq $0,%rdx - movq %r13,-16(%rsp,%r15,8) + movq %r13,-16(%rsp,%r15,8) // tp[j-1] movq %rdx,%r13 xorq %rdx,%rdx addq %r11,%r13 adcq $0,%rdx - addq %r10,%r13 + addq %r10,%r13 // pull upmost overflow bit adcq $0,%rdx movq %r13,-8(%rsp,%r9,8) - movq %rdx,(%rsp,%r9,8) + movq %rdx,(%rsp,%r9,8) // store upmost overflow bit - leaq 1(%r14),%r14 + leaq 1(%r14),%r14 // i++ cmpq %r9,%r14 jb .Louter - xorq %r14,%r14 - movq (%rsp),%rax - movq %r9,%r15 + xorq %r14,%r14 // i=0 and clear CF + movq (%rsp),%rax // tp[0] + movq %r9,%r15 // j=num .align 16 .Lsub: sbbq (%rcx,%r14,8),%rax - movq %rax,(%rdi,%r14,8) - movq 8(%rsp,%r14,8),%rax - leaq 1(%r14),%r14 - decq %r15 + movq %rax,(%rdi,%r14,8) // rp[i]=tp[i]-np[i] + movq 8(%rsp,%r14,8),%rax // tp[i+1] + leaq 1(%r14),%r14 // i++ + decq %r15 // doesn't affect CF jnz .Lsub - sbbq $0,%rax + sbbq $0,%rax // handle upmost overflow bit movq $-1,%rbx - xorq %rax,%rbx + xorq %rax,%rbx // not %rax xorq %r14,%r14 - movq %r9,%r15 + movq %r9,%r15 // j=num -.Lcopy: +.Lcopy: // conditional copy movq (%rdi,%r14,8),%rcx movq (%rsp,%r14,8),%rdx andq %rbx,%rcx andq %rax,%rdx - movq %r9,(%rsp,%r14,8) + movq %r9,(%rsp,%r14,8) // zap temporary vector orq %rcx,%rdx - movq %rdx,(%rdi,%r14,8) + movq %rdx,(%rdi,%r14,8) // rp[i]=tp[i] leaq 1(%r14),%r14 subq $1,%r15 jnz .Lcopy - movq 8(%rsp,%r9,8),%rsi + movq 8(%rsp,%r9,8),%rsi // restore %rsp .cfi_def_cfa %rsi,8 - +// No return value movq -48(%rsi),%r15 .cfi_restore %r15 movq -40(%rsi),%r14 @@ -273,9 +273,9 @@ _CET_ENDBR negq %r9 movq %rsp,%r11 - leaq -32(%rsp,%r9,8),%r10 - negq %r9 - andq $-1024,%r10 + leaq -32(%rsp,%r9,8),%r10 // future alloca(8*(num+4)) + negq %r9 // restore + andq $-1024,%r10 // minimize TLB usage subq %r10,%r11 andq $-4096,%r11 @@ -292,28 +292,28 @@ _CET_ENDBR ja .Lmul4x_page_walk .Lmul4x_page_walk_done: - movq %rax,8(%rsp,%r9,8) + movq %rax,8(%rsp,%r9,8) // tp[num+1]=%rsp .cfi_escape 0x0f,0x0a,0x77,0x08,0x79,0x00,0x38,0x1e,0x22,0x06,0x23,0x08 .Lmul4x_body: - movq %rdi,16(%rsp,%r9,8) - movq %rdx,%r12 - movq (%r8),%r8 - movq (%r12),%rbx + movq %rdi,16(%rsp,%r9,8) // tp[num+2]=%rdi + movq %rdx,%r12 // reassign %r12 + movq (%r8),%r8 // pull n0[0] value + movq (%r12),%rbx // m0=bp[0] movq (%rsi),%rax - xorq %r14,%r14 - xorq %r15,%r15 + xorq %r14,%r14 // i=0 + xorq %r15,%r15 // j=0 movq %r8,%rbp - mulq %rbx + mulq %rbx // ap[0]*bp[0] movq %rax,%r10 movq (%rcx),%rax - imulq %r10,%rbp + imulq %r10,%rbp // "tp[0]"*n0 movq %rdx,%r11 - mulq %rbp - addq %rax,%r10 + mulq %rbp // np[0]*m1 + addq %rax,%r10 // discarded movq 8(%rsi),%rax adcq $0,%rdx movq %rdx,%rdi @@ -329,171 +329,171 @@ _CET_ENDBR movq 16(%rsi),%rax adcq $0,%rdx addq %r11,%rdi - leaq 4(%r15),%r15 + leaq 4(%r15),%r15 // j++ adcq $0,%rdx movq %rdi,(%rsp) movq %rdx,%r13 jmp .L1st4x .align 16 .L1st4x: - mulq %rbx + mulq %rbx // ap[j]*bp[0] addq %rax,%r10 movq -16(%rcx,%r15,8),%rax adcq $0,%rdx movq %rdx,%r11 - mulq %rbp + mulq %rbp // np[j]*m1 addq %rax,%r13 movq -8(%rsi,%r15,8),%rax adcq $0,%rdx - addq %r10,%r13 + addq %r10,%r13 // np[j]*m1+ap[j]*bp[0] adcq $0,%rdx - movq %r13,-24(%rsp,%r15,8) + movq %r13,-24(%rsp,%r15,8) // tp[j-1] movq %rdx,%rdi - mulq %rbx + mulq %rbx // ap[j]*bp[0] addq %rax,%r11 movq -8(%rcx,%r15,8),%rax adcq $0,%rdx movq %rdx,%r10 - mulq %rbp + mulq %rbp // np[j]*m1 addq %rax,%rdi movq (%rsi,%r15,8),%rax adcq $0,%rdx - addq %r11,%rdi + addq %r11,%rdi // np[j]*m1+ap[j]*bp[0] adcq $0,%rdx - movq %rdi,-16(%rsp,%r15,8) + movq %rdi,-16(%rsp,%r15,8) // tp[j-1] movq %rdx,%r13 - mulq %rbx + mulq %rbx // ap[j]*bp[0] addq %rax,%r10 movq (%rcx,%r15,8),%rax adcq $0,%rdx movq %rdx,%r11 - mulq %rbp + mulq %rbp // np[j]*m1 addq %rax,%r13 movq 8(%rsi,%r15,8),%rax adcq $0,%rdx - addq %r10,%r13 + addq %r10,%r13 // np[j]*m1+ap[j]*bp[0] adcq $0,%rdx - movq %r13,-8(%rsp,%r15,8) + movq %r13,-8(%rsp,%r15,8) // tp[j-1] movq %rdx,%rdi - mulq %rbx + mulq %rbx // ap[j]*bp[0] addq %rax,%r11 movq 8(%rcx,%r15,8),%rax adcq $0,%rdx - leaq 4(%r15),%r15 + leaq 4(%r15),%r15 // j++ movq %rdx,%r10 - mulq %rbp + mulq %rbp // np[j]*m1 addq %rax,%rdi movq -16(%rsi,%r15,8),%rax adcq $0,%rdx - addq %r11,%rdi + addq %r11,%rdi // np[j]*m1+ap[j]*bp[0] adcq $0,%rdx - movq %rdi,-32(%rsp,%r15,8) + movq %rdi,-32(%rsp,%r15,8) // tp[j-1] movq %rdx,%r13 cmpq %r9,%r15 jb .L1st4x - mulq %rbx + mulq %rbx // ap[j]*bp[0] addq %rax,%r10 movq -16(%rcx,%r15,8),%rax adcq $0,%rdx movq %rdx,%r11 - mulq %rbp + mulq %rbp // np[j]*m1 addq %rax,%r13 movq -8(%rsi,%r15,8),%rax adcq $0,%rdx - addq %r10,%r13 + addq %r10,%r13 // np[j]*m1+ap[j]*bp[0] adcq $0,%rdx - movq %r13,-24(%rsp,%r15,8) + movq %r13,-24(%rsp,%r15,8) // tp[j-1] movq %rdx,%rdi - mulq %rbx + mulq %rbx // ap[j]*bp[0] addq %rax,%r11 movq -8(%rcx,%r15,8),%rax adcq $0,%rdx movq %rdx,%r10 - mulq %rbp + mulq %rbp // np[j]*m1 addq %rax,%rdi - movq (%rsi),%rax + movq (%rsi),%rax // ap[0] adcq $0,%rdx - addq %r11,%rdi + addq %r11,%rdi // np[j]*m1+ap[j]*bp[0] adcq $0,%rdx - movq %rdi,-16(%rsp,%r15,8) + movq %rdi,-16(%rsp,%r15,8) // tp[j-1] movq %rdx,%r13 xorq %rdi,%rdi addq %r10,%r13 adcq $0,%rdi movq %r13,-8(%rsp,%r15,8) - movq %rdi,(%rsp,%r15,8) + movq %rdi,(%rsp,%r15,8) // store upmost overflow bit - leaq 1(%r14),%r14 + leaq 1(%r14),%r14 // i++ .align 4 .Louter4x: - movq (%r12,%r14,8),%rbx - xorq %r15,%r15 + movq (%r12,%r14,8),%rbx // m0=bp[i] + xorq %r15,%r15 // j=0 movq (%rsp),%r10 movq %r8,%rbp - mulq %rbx - addq %rax,%r10 + mulq %rbx // ap[0]*bp[i] + addq %rax,%r10 // ap[0]*bp[i]+tp[0] movq (%rcx),%rax adcq $0,%rdx - imulq %r10,%rbp + imulq %r10,%rbp // tp[0]*n0 movq %rdx,%r11 - mulq %rbp - addq %rax,%r10 + mulq %rbp // np[0]*m1 + addq %rax,%r10 // "%r13", discarded movq 8(%rsi),%rax adcq $0,%rdx movq %rdx,%rdi - mulq %rbx + mulq %rbx // ap[j]*bp[i] addq %rax,%r11 movq 8(%rcx),%rax adcq $0,%rdx - addq 8(%rsp),%r11 + addq 8(%rsp),%r11 // +tp[1] adcq $0,%rdx movq %rdx,%r10 - mulq %rbp + mulq %rbp // np[j]*m1 addq %rax,%rdi movq 16(%rsi),%rax adcq $0,%rdx - addq %r11,%rdi - leaq 4(%r15),%r15 + addq %r11,%rdi // np[j]*m1+ap[j]*bp[i]+tp[j] + leaq 4(%r15),%r15 // j+=2 adcq $0,%rdx - movq %rdi,(%rsp) + movq %rdi,(%rsp) // tp[j-1] movq %rdx,%r13 jmp .Linner4x .align 16 .Linner4x: - mulq %rbx + mulq %rbx // ap[j]*bp[i] addq %rax,%r10 movq -16(%rcx,%r15,8),%rax adcq $0,%rdx - addq -16(%rsp,%r15,8),%r10 + addq -16(%rsp,%r15,8),%r10 // ap[j]*bp[i]+tp[j] adcq $0,%rdx movq %rdx,%r11 - mulq %rbp + mulq %rbp // np[j]*m1 addq %rax,%r13 movq -8(%rsi,%r15,8),%rax adcq $0,%rdx addq %r10,%r13 adcq $0,%rdx - movq %r13,-24(%rsp,%r15,8) + movq %r13,-24(%rsp,%r15,8) // tp[j-1] movq %rdx,%rdi - mulq %rbx + mulq %rbx // ap[j]*bp[i] addq %rax,%r11 movq -8(%rcx,%r15,8),%rax adcq $0,%rdx @@ -501,148 +501,148 @@ _CET_ENDBR adcq $0,%rdx movq %rdx,%r10 - mulq %rbp + mulq %rbp // np[j]*m1 addq %rax,%rdi movq (%rsi,%r15,8),%rax adcq $0,%rdx addq %r11,%rdi adcq $0,%rdx - movq %rdi,-16(%rsp,%r15,8) + movq %rdi,-16(%rsp,%r15,8) // tp[j-1] movq %rdx,%r13 - mulq %rbx + mulq %rbx // ap[j]*bp[i] addq %rax,%r10 movq (%rcx,%r15,8),%rax adcq $0,%rdx - addq (%rsp,%r15,8),%r10 + addq (%rsp,%r15,8),%r10 // ap[j]*bp[i]+tp[j] adcq $0,%rdx movq %rdx,%r11 - mulq %rbp + mulq %rbp // np[j]*m1 addq %rax,%r13 movq 8(%rsi,%r15,8),%rax adcq $0,%rdx addq %r10,%r13 adcq $0,%rdx - movq %r13,-8(%rsp,%r15,8) + movq %r13,-8(%rsp,%r15,8) // tp[j-1] movq %rdx,%rdi - mulq %rbx + mulq %rbx // ap[j]*bp[i] addq %rax,%r11 movq 8(%rcx,%r15,8),%rax adcq $0,%rdx addq 8(%rsp,%r15,8),%r11 adcq $0,%rdx - leaq 4(%r15),%r15 + leaq 4(%r15),%r15 // j++ movq %rdx,%r10 - mulq %rbp + mulq %rbp // np[j]*m1 addq %rax,%rdi movq -16(%rsi,%r15,8),%rax adcq $0,%rdx addq %r11,%rdi adcq $0,%rdx - movq %rdi,-32(%rsp,%r15,8) + movq %rdi,-32(%rsp,%r15,8) // tp[j-1] movq %rdx,%r13 cmpq %r9,%r15 jb .Linner4x - mulq %rbx + mulq %rbx // ap[j]*bp[i] addq %rax,%r10 movq -16(%rcx,%r15,8),%rax adcq $0,%rdx - addq -16(%rsp,%r15,8),%r10 + addq -16(%rsp,%r15,8),%r10 // ap[j]*bp[i]+tp[j] adcq $0,%rdx movq %rdx,%r11 - mulq %rbp + mulq %rbp // np[j]*m1 addq %rax,%r13 movq -8(%rsi,%r15,8),%rax adcq $0,%rdx addq %r10,%r13 adcq $0,%rdx - movq %r13,-24(%rsp,%r15,8) + movq %r13,-24(%rsp,%r15,8) // tp[j-1] movq %rdx,%rdi - mulq %rbx + mulq %rbx // ap[j]*bp[i] addq %rax,%r11 movq -8(%rcx,%r15,8),%rax adcq $0,%rdx addq -8(%rsp,%r15,8),%r11 adcq $0,%rdx - leaq 1(%r14),%r14 + leaq 1(%r14),%r14 // i++ movq %rdx,%r10 - mulq %rbp + mulq %rbp // np[j]*m1 addq %rax,%rdi - movq (%rsi),%rax + movq (%rsi),%rax // ap[0] adcq $0,%rdx addq %r11,%rdi adcq $0,%rdx - movq %rdi,-16(%rsp,%r15,8) + movq %rdi,-16(%rsp,%r15,8) // tp[j-1] movq %rdx,%r13 xorq %rdi,%rdi addq %r10,%r13 adcq $0,%rdi - addq (%rsp,%r9,8),%r13 + addq (%rsp,%r9,8),%r13 // pull upmost overflow bit adcq $0,%rdi movq %r13,-8(%rsp,%r15,8) - movq %rdi,(%rsp,%r15,8) + movq %rdi,(%rsp,%r15,8) // store upmost overflow bit cmpq %r9,%r14 jb .Louter4x - movq 16(%rsp,%r9,8),%rdi + movq 16(%rsp,%r9,8),%rdi // restore %rdi leaq -4(%r9),%r15 - movq 0(%rsp),%rax - movq 8(%rsp),%rdx - shrq $2,%r15 - leaq (%rsp),%rsi - xorq %r14,%r14 + movq 0(%rsp),%rax // tp[0] + movq 8(%rsp),%rdx // tp[1] + shrq $2,%r15 // j=num/4-1 + leaq (%rsp),%rsi // borrow ap for tp + xorq %r14,%r14 // i=0 and clear CF subq 0(%rcx),%rax - movq 16(%rsi),%rbx - movq 24(%rsi),%rbp + movq 16(%rsi),%rbx // tp[2] + movq 24(%rsi),%rbp // tp[3] sbbq 8(%rcx),%rdx .Lsub4x: - movq %rax,0(%rdi,%r14,8) - movq %rdx,8(%rdi,%r14,8) + movq %rax,0(%rdi,%r14,8) // rp[i]=tp[i]-np[i] + movq %rdx,8(%rdi,%r14,8) // rp[i]=tp[i]-np[i] sbbq 16(%rcx,%r14,8),%rbx - movq 32(%rsi,%r14,8),%rax + movq 32(%rsi,%r14,8),%rax // tp[i+1] movq 40(%rsi,%r14,8),%rdx sbbq 24(%rcx,%r14,8),%rbp - movq %rbx,16(%rdi,%r14,8) - movq %rbp,24(%rdi,%r14,8) + movq %rbx,16(%rdi,%r14,8) // rp[i]=tp[i]-np[i] + movq %rbp,24(%rdi,%r14,8) // rp[i]=tp[i]-np[i] sbbq 32(%rcx,%r14,8),%rax movq 48(%rsi,%r14,8),%rbx movq 56(%rsi,%r14,8),%rbp sbbq 40(%rcx,%r14,8),%rdx - leaq 4(%r14),%r14 - decq %r15 + leaq 4(%r14),%r14 // i++ + decq %r15 // doesn't affect CF jnz .Lsub4x - movq %rax,0(%rdi,%r14,8) - movq 32(%rsi,%r14,8),%rax + movq %rax,0(%rdi,%r14,8) // rp[i]=tp[i]-np[i] + movq 32(%rsi,%r14,8),%rax // load overflow bit sbbq 16(%rcx,%r14,8),%rbx - movq %rdx,8(%rdi,%r14,8) + movq %rdx,8(%rdi,%r14,8) // rp[i]=tp[i]-np[i] sbbq 24(%rcx,%r14,8),%rbp - movq %rbx,16(%rdi,%r14,8) + movq %rbx,16(%rdi,%r14,8) // rp[i]=tp[i]-np[i] - sbbq $0,%rax - movq %rbp,24(%rdi,%r14,8) + sbbq $0,%rax // handle upmost overflow bit + movq %rbp,24(%rdi,%r14,8) // rp[i]=tp[i]-np[i] pxor %xmm0,%xmm0 movq %rax,%xmm4 pcmpeqd %xmm5,%xmm5 pshufd $0,%xmm4,%xmm4 movq %r9,%r15 pxor %xmm4,%xmm5 - shrq $2,%r15 - xorl %eax,%eax + shrq $2,%r15 // j=num/4 + xorl %eax,%eax // i=0 jmp .Lcopy4x .align 16 -.Lcopy4x: +.Lcopy4x: // conditional copy movdqa (%rsp,%rax,1),%xmm1 movdqu (%rdi,%rax,1),%xmm2 pand %xmm4,%xmm1 @@ -660,9 +660,9 @@ _CET_ENDBR leaq 32(%rax),%rax decq %r15 jnz .Lcopy4x - movq 8(%rsp,%r9,8),%rsi + movq 8(%rsp,%r9,8),%rsi // restore %rsp .cfi_def_cfa %rsi, 8 - +// No return value movq -48(%rsi),%r15 .cfi_restore %r15 movq -40(%rsi),%r14 @@ -682,9 +682,9 @@ _CET_ENDBR .cfi_endproc .size bn_mul4x_mont,.-bn_mul4x_mont .extern bn_sqrx8x_internal -.hidden bn_sqrx8x_internal +.hidden bn_sqrx8x_internal // see x86_64-mont5 module .extern bn_sqr8x_internal -.hidden bn_sqr8x_internal +.hidden bn_sqr8x_internal // see x86_64-mont5 module .globl bn_sqr8x_mont .hidden bn_sqr8x_mont @@ -711,30 +711,30 @@ _CET_ENDBR .Lsqr8x_prologue: movl %r9d,%r10d - shll $3,%r9d - shlq $3+2,%r10 + shll $3,%r9d // convert %r9 to bytes + shlq $3+2,%r10 // 4*%r9 negq %r9 - - - - - +// ############################################################# +// ensure that stack frame doesn't alias with %rsi modulo +// 4096. this is done to allow memory disambiguation logic +// do its job. +// leaq -64(%rsp,%r9,2),%r11 movq %rsp,%rbp - movq (%r8),%r8 + movq (%r8),%r8 // *n0 subq %rsi,%r11 andq $4095,%r11 cmpq %r11,%r10 jb .Lsqr8x_sp_alt - subq %r11,%rbp - leaq -64(%rbp,%r9,2),%rbp + subq %r11,%rbp // align with %rsi + leaq -64(%rbp,%r9,2),%rbp // future alloca(frame+2*%r9) jmp .Lsqr8x_sp_done .align 32 .Lsqr8x_sp_alt: - leaq 4096-64(,%r9,2),%r10 - leaq -64(%rbp,%r9,2),%rbp + leaq 4096-64(,%r9,2),%r10 // 4096-frame-2*%r9 + leaq -64(%rbp,%r9,2),%rbp // future alloca(frame+2*%r9) subq %r10,%r11 movq $0,%r10 cmovcq %r10,%r11 @@ -762,41 +762,41 @@ _CET_ENDBR negq %r9 movq %r8,32(%rsp) - movq %rax,40(%rsp) + movq %rax,40(%rsp) // save original %rsp .cfi_escape 0x0f,0x05,0x77,0x28,0x06,0x23,0x08 .Lsqr8x_body: - movq %rcx,%xmm2 + movq %rcx,%xmm2 // save pointer to modulus pxor %xmm0,%xmm0 - movq %rdi,%xmm1 - movq %r10,%xmm3 + movq %rdi,%xmm1 // save %rdi + movq %r10,%xmm3 // -%r9 testq %rdx,%rdx jz .Lsqr8x_nox - call bn_sqrx8x_internal - - - - + call bn_sqrx8x_internal // see x86_64-mont5 module +// %rax top-most carry +// %rbp nptr +// %rcx -8*num +// %r8 end of tp[2*num] leaq (%r8,%rcx,1),%rbx movq %rcx,%r9 movq %rcx,%rdx movq %xmm1,%rdi - sarq $3+2,%rcx + sarq $3+2,%rcx // %cf=0 jmp .Lsqr8x_sub .align 32 .Lsqr8x_nox: - call bn_sqr8x_internal - - - - + call bn_sqr8x_internal // see x86_64-mont5 module +// %rax top-most carry +// %rbp nptr +// %r8 -8*num +// %rdi end of tp[2*num] leaq (%rdi,%r9,1),%rbx movq %r9,%rcx movq %r9,%rdx movq %xmm1,%rdi - sarq $3+2,%rcx + sarq $3+2,%rcx // %cf=0 jmp .Lsqr8x_sub .align 32 @@ -816,17 +816,17 @@ _CET_ENDBR movq %r14,16(%rdi) movq %r15,24(%rdi) leaq 32(%rdi),%rdi - incq %rcx + incq %rcx // preserves %cf jnz .Lsqr8x_sub - sbbq $0,%rax - leaq (%rbx,%r9,1),%rbx - leaq (%rdi,%r9,1),%rdi + sbbq $0,%rax // top-most carry + leaq (%rbx,%r9,1),%rbx // rewind + leaq (%rdi,%r9,1),%rdi // rewind movq %rax,%xmm1 pxor %xmm0,%xmm0 pshufd $0,%xmm1,%xmm1 - movq 40(%rsp),%rsi + movq 40(%rsp),%rsi // restore %rsp .cfi_def_cfa %rsi,8 jmp .Lsqr8x_cond_copy @@ -838,7 +838,7 @@ _CET_ENDBR movdqu 0(%rdi),%xmm4 movdqu 16(%rdi),%xmm5 leaq 32(%rdi),%rdi - movdqa %xmm0,-32(%rbx) + movdqa %xmm0,-32(%rbx) // zero tp movdqa %xmm0,-16(%rbx) movdqa %xmm0,-32(%rbx,%rdx,1) movdqa %xmm0,-16(%rbx,%rdx,1) @@ -855,7 +855,7 @@ _CET_ENDBR addq $32,%r9 jnz .Lsqr8x_cond_copy - +// No return value movq -48(%rsi),%r15 .cfi_restore %r15 movq -40(%rsi),%r14 @@ -897,11 +897,11 @@ _CET_ENDBR .cfi_offset %r15,-56 .Lmulx4x_prologue: - shll $3,%r9d + shll $3,%r9d // convert %r9 to bytes xorq %r10,%r10 - subq %r9,%r10 - movq (%r8),%r8 - leaq -72(%rsp,%r10,1),%rbp + subq %r9,%r10 // -%r9 + movq (%r8),%r8 // *n0 + leaq -72(%rsp,%r10,1),%rbp // future alloca(frame+%r9+8) andq $-128,%rbp movq %rsp,%r11 subq %rbp,%r11 @@ -921,62 +921,62 @@ _CET_ENDBR .Lmulx4x_page_walk_done: leaq (%rdx,%r9,1),%r10 - - - - - - - - - - - - - movq %r9,0(%rsp) +// ############################################################# +// Stack layout +// +0 num +// +8 off-loaded &b[i] +// +16 end of b[num] +// +24 saved n0 +// +32 saved rp +// +40 saved %rsp +// +48 inner counter +// +56 +// +64 tmp[num+1] +// + movq %r9,0(%rsp) // save %r9 shrq $5,%r9 - movq %r10,16(%rsp) + movq %r10,16(%rsp) // end of b[num] subq $1,%r9 - movq %r8,24(%rsp) - movq %rdi,32(%rsp) - movq %rax,40(%rsp) + movq %r8,24(%rsp) // save *n0 + movq %rdi,32(%rsp) // save %rdi + movq %rax,40(%rsp) // save original %rsp .cfi_escape 0x0f,0x05,0x77,0x28,0x06,0x23,0x08 - movq %r9,48(%rsp) + movq %r9,48(%rsp) // inner counter jmp .Lmulx4x_body .align 32 .Lmulx4x_body: leaq 8(%rdx),%rdi - movq (%rdx),%rdx + movq (%rdx),%rdx // b[0], %rdx==%rdx actually leaq 64+32(%rsp),%rbx movq %rdx,%r9 - mulxq 0(%rsi),%r8,%rax - mulxq 8(%rsi),%r11,%r14 + mulxq 0(%rsi),%r8,%rax // a[0]*b[0] + mulxq 8(%rsi),%r11,%r14 // a[1]*b[0] addq %rax,%r11 - movq %rdi,8(%rsp) - mulxq 16(%rsi),%r12,%r13 + movq %rdi,8(%rsp) // off-load &b[i] + mulxq 16(%rsi),%r12,%r13 // ... adcq %r14,%r12 adcq $0,%r13 - movq %r8,%rdi - imulq 24(%rsp),%r8 - xorq %rbp,%rbp + movq %r8,%rdi // borrow %rdi + imulq 24(%rsp),%r8 // "t[0]"*n0 + xorq %rbp,%rbp // cf=0, of=0 mulxq 24(%rsi),%rax,%r14 movq %r8,%rdx leaq 32(%rsi),%rsi adcxq %rax,%r13 - adcxq %rbp,%r14 + adcxq %rbp,%r14 // cf=0 mulxq 0(%rcx),%rax,%r10 - adcxq %rax,%rdi + adcxq %rax,%rdi // discarded adoxq %r11,%r10 mulxq 8(%rcx),%rax,%r11 adcxq %rax,%r10 adoxq %r12,%r11 -.byte 0xc4,0x62,0xfb,0xf6,0xa1,0x10,0x00,0x00,0x00 - movq 48(%rsp),%rdi +.byte 0xc4,0x62,0xfb,0xf6,0xa1,0x10,0x00,0x00,0x00 // mulx 2*8(%rcx),%rax,%r12 + movq 48(%rsp),%rdi // counter value movq %r10,-32(%rbx) adcxq %rax,%r11 adoxq %r13,%r12 @@ -984,7 +984,7 @@ _CET_ENDBR movq %r9,%rdx movq %r11,-24(%rbx) adcxq %rax,%r12 - adoxq %rbp,%r15 + adoxq %rbp,%r15 // of=0 leaq 32(%rcx),%rcx movq %r12,-16(%rbx) @@ -992,18 +992,18 @@ _CET_ENDBR .align 32 .Lmulx4x_1st: - adcxq %rbp,%r15 - mulxq 0(%rsi),%r10,%rax + adcxq %rbp,%r15 // cf=0, modulo-scheduled + mulxq 0(%rsi),%r10,%rax // a[4]*b[0] adcxq %r14,%r10 - mulxq 8(%rsi),%r11,%r14 + mulxq 8(%rsi),%r11,%r14 // a[5]*b[0] adcxq %rax,%r11 - mulxq 16(%rsi),%r12,%rax + mulxq 16(%rsi),%r12,%rax // ... adcxq %r14,%r12 mulxq 24(%rsi),%r13,%r14 .byte 0x67,0x67 movq %r8,%rdx adcxq %rax,%r13 - adcxq %rbp,%r14 + adcxq %rbp,%r14 // cf=0 leaq 32(%rsi),%rsi leaq 32(%rbx),%rbx @@ -1027,43 +1027,43 @@ _CET_ENDBR leaq 32(%rcx),%rcx movq %r13,-16(%rbx) - decq %rdi + decq %rdi // of=0, pass cf jnz .Lmulx4x_1st - movq 0(%rsp),%rax - movq 8(%rsp),%rdi - adcq %rbp,%r15 + movq 0(%rsp),%rax // load num + movq 8(%rsp),%rdi // re-load &b[i] + adcq %rbp,%r15 // modulo-scheduled addq %r15,%r14 - sbbq %r15,%r15 + sbbq %r15,%r15 // top-most carry movq %r14,-8(%rbx) jmp .Lmulx4x_outer .align 32 .Lmulx4x_outer: - movq (%rdi),%rdx - leaq 8(%rdi),%rdi - subq %rax,%rsi - movq %r15,(%rbx) + movq (%rdi),%rdx // b[i] + leaq 8(%rdi),%rdi // b++ + subq %rax,%rsi // rewind %rsi + movq %r15,(%rbx) // save top-most carry leaq 64+32(%rsp),%rbx - subq %rax,%rcx + subq %rax,%rcx // rewind %rcx - mulxq 0(%rsi),%r8,%r11 - xorl %ebp,%ebp + mulxq 0(%rsi),%r8,%r11 // a[0]*b[i] + xorl %ebp,%ebp // xor %rbp,%rbp # cf=0, of=0 movq %rdx,%r9 - mulxq 8(%rsi),%r14,%r12 + mulxq 8(%rsi),%r14,%r12 // a[1]*b[i] adoxq -32(%rbx),%r8 adcxq %r14,%r11 - mulxq 16(%rsi),%r15,%r13 + mulxq 16(%rsi),%r15,%r13 // ... adoxq -24(%rbx),%r11 adcxq %r15,%r12 adoxq -16(%rbx),%r12 adcxq %rbp,%r13 adoxq %rbp,%r13 - movq %rdi,8(%rsp) + movq %rdi,8(%rsp) // off-load &b[i] movq %r8,%r15 - imulq 24(%rsp),%r8 - xorl %ebp,%ebp + imulq 24(%rsp),%r8 // "t[0]"*n0 + xorl %ebp,%ebp // xor %rbp,%rbp # cf=0, of=0 mulxq 24(%rsi),%rax,%r14 movq %r8,%rdx @@ -1074,7 +1074,7 @@ _CET_ENDBR adoxq %rbp,%r14 mulxq 0(%rcx),%rax,%r10 - adcxq %rax,%r15 + adcxq %rax,%r15 // discarded adoxq %r11,%r10 mulxq 8(%rcx),%rax,%r11 adcxq %rax,%r10 @@ -1088,21 +1088,21 @@ _CET_ENDBR movq %r11,-24(%rbx) leaq 32(%rcx),%rcx adcxq %rax,%r12 - adoxq %rbp,%r15 - movq 48(%rsp),%rdi + adoxq %rbp,%r15 // of=0 + movq 48(%rsp),%rdi // counter value movq %r12,-16(%rbx) jmp .Lmulx4x_inner .align 32 .Lmulx4x_inner: - mulxq 0(%rsi),%r10,%rax - adcxq %rbp,%r15 + mulxq 0(%rsi),%r10,%rax // a[4]*b[i] + adcxq %rbp,%r15 // cf=0, modulo-scheduled adoxq %r14,%r10 - mulxq 8(%rsi),%r11,%r14 + mulxq 8(%rsi),%r11,%r14 // a[5]*b[i] adcxq 0(%rbx),%r10 adoxq %rax,%r11 - mulxq 16(%rsi),%r12,%rax + mulxq 16(%rsi),%r12,%rax // ... adcxq 8(%rbx),%r11 adoxq %r14,%r12 mulxq 24(%rsi),%r13,%r14 @@ -1110,10 +1110,10 @@ _CET_ENDBR adcxq 16(%rbx),%r12 adoxq %rax,%r13 adcxq 24(%rbx),%r13 - adoxq %rbp,%r14 + adoxq %rbp,%r14 // of=0 leaq 32(%rsi),%rsi leaq 32(%rbx),%rbx - adcxq %rbp,%r14 + adcxq %rbp,%r14 // cf=0 adoxq %r15,%r10 mulxq 0(%rcx),%rax,%r15 @@ -1135,26 +1135,26 @@ _CET_ENDBR leaq 32(%rcx),%rcx movq %r13,-16(%rbx) - decq %rdi + decq %rdi // of=0, pass cf jnz .Lmulx4x_inner - movq 0(%rsp),%rax - movq 8(%rsp),%rdi - adcq %rbp,%r15 - subq 0(%rbx),%rbp + movq 0(%rsp),%rax // load num + movq 8(%rsp),%rdi // re-load &b[i] + adcq %rbp,%r15 // modulo-scheduled + subq 0(%rbx),%rbp // pull top-most carry adcq %r15,%r14 - sbbq %r15,%r15 + sbbq %r15,%r15 // top-most carry movq %r14,-8(%rbx) cmpq 16(%rsp),%rdi jne .Lmulx4x_outer leaq 64(%rsp),%rbx - subq %rax,%rcx + subq %rax,%rcx // rewind %rcx negq %r15 movq %rax,%rdx - shrq $3+2,%rax - movq 32(%rsp),%rdi + shrq $3+2,%rax // %cf=0 + movq 32(%rsp),%rdi // restore rp jmp .Lmulx4x_sub .align 32 @@ -1174,17 +1174,17 @@ _CET_ENDBR movq %r13,16(%rdi) movq %r14,24(%rdi) leaq 32(%rdi),%rdi - decq %rax + decq %rax // preserves %cf jnz .Lmulx4x_sub - sbbq $0,%r15 + sbbq $0,%r15 // top-most carry leaq 64(%rsp),%rbx - subq %rdx,%rdi + subq %rdx,%rdi // rewind movq %r15,%xmm1 pxor %xmm0,%xmm0 pshufd $0,%xmm1,%xmm1 - movq 40(%rsp),%rsi + movq 40(%rsp),%rsi // restore %rsp .cfi_def_cfa %rsi,8 jmp .Lmulx4x_cond_copy @@ -1196,7 +1196,7 @@ _CET_ENDBR movdqu 0(%rdi),%xmm4 movdqu 16(%rdi),%xmm5 leaq 32(%rdi),%rdi - movdqa %xmm0,-32(%rbx) + movdqa %xmm0,-32(%rbx) // zero tp movdqa %xmm0,-16(%rbx) pcmpeqd %xmm1,%xmm0 pand %xmm1,%xmm2 @@ -1213,7 +1213,7 @@ _CET_ENDBR movq %rdx,(%rbx) - +// No return value movq -48(%rsi),%r15 .cfi_restore %r15 movq -40(%rsi),%r14 diff --git a/third_party/boringssl/gen/bcm/x86_64-mont-win.asm b/third_party/boringssl/gen/bcm/x86_64-mont-win.asm index 721d5dc64..5ff35fefb 100644 --- a/third_party/boringssl/gen/bcm/x86_64-mont-win.asm +++ b/third_party/boringssl/gen/bcm/x86_64-mont-win.asm @@ -50,18 +50,18 @@ _CET_ENDBR neg r9 mov r11,rsp - lea r10,[((-16))+r9*8+rsp] - neg r9 - and r10,-1024 - - - - - - - - - + lea r10,[((-16))+r9*8+rsp] ; future alloca(8*(num+2)) + neg r9 ; restore %r9 + and r10,-1024 ; minimize TLB usage + +; An OS-agnostic version of __chkstk. +; +; Some OSes (Windows) insist on stack being "wired" to +; physical memory in strictly sequential manner, i.e. if stack +; allocation spans two pages, then reference to farmost one can +; be punishable by SEGV. But page walking can do good even on +; other OSes, because it guarantees that villain thread hits +; the guard page before it can make damage to innocent one... sub r11,r10 and r11,-4096 lea rsp,[r11*1+r10] @@ -78,32 +78,32 @@ $L$mul_page_walk: ja NEAR $L$mul_page_walk $L$mul_page_walk_done: - mov QWORD[8+r9*8+rsp],rax + mov QWORD[8+r9*8+rsp],rax ; tp[num+1]=%rsp $L$mul_body: - mov r12,rdx - mov r8,QWORD[r8] - mov rbx,QWORD[r12] + mov r12,rdx ; reassign %rdx + mov r8,QWORD[r8] ; pull n0[0] value + mov rbx,QWORD[r12] ; m0=bp[0] mov rax,QWORD[rsi] - xor r14,r14 - xor r15,r15 + xor r14,r14 ; i=0 + xor r15,r15 ; j=0 mov rbp,r8 - mul rbx + mul rbx ; ap[0]*bp[0] mov r10,rax mov rax,QWORD[rcx] - imul rbp,r10 + imul rbp,r10 ; "tp[0]"*n0 mov r11,rdx - mul rbp - add r10,rax + mul rbp ; np[0]*m1 + add r10,rax ; discarded mov rax,QWORD[8+rsi] adc rdx,0 mov r13,rdx - lea r15,[1+r15] + lea r15,[1+r15] ; j++ jmp NEAR $L$1st_enter ALIGN 16 @@ -111,30 +111,30 @@ $L$1st: add r13,rax mov rax,QWORD[r15*8+rsi] adc rdx,0 - add r13,r11 + add r13,r11 ; np[j]*m1+ap[j]*bp[0] mov r11,r10 adc rdx,0 - mov QWORD[((-16))+r15*8+rsp],r13 + mov QWORD[((-16))+r15*8+rsp],r13 ; tp[j-1] mov r13,rdx $L$1st_enter: - mul rbx + mul rbx ; ap[j]*bp[0] add r11,rax mov rax,QWORD[r15*8+rcx] adc rdx,0 - lea r15,[1+r15] + lea r15,[1+r15] ; j++ mov r10,rdx - mul rbp + mul rbp ; np[j]*m1 cmp r15,r9 jne NEAR $L$1st add r13,rax - mov rax,QWORD[rsi] + mov rax,QWORD[rsi] ; ap[0] adc rdx,0 - add r13,r11 + add r13,r11 ; np[j]*m1+ap[j]*bp[0] adc rdx,0 - mov QWORD[((-16))+r15*8+rsp],r13 + mov QWORD[((-16))+r15*8+rsp],r13 ; tp[j-1] mov r13,rdx mov r11,r10 @@ -142,32 +142,32 @@ $L$1st_enter: add r13,r11 adc rdx,0 mov QWORD[((-8))+r9*8+rsp],r13 - mov QWORD[r9*8+rsp],rdx + mov QWORD[r9*8+rsp],rdx ; store upmost overflow bit - lea r14,[1+r14] + lea r14,[1+r14] ; i++ jmp NEAR $L$outer ALIGN 16 $L$outer: - mov rbx,QWORD[r14*8+r12] - xor r15,r15 + mov rbx,QWORD[r14*8+r12] ; m0=bp[i] + xor r15,r15 ; j=0 mov rbp,r8 mov r10,QWORD[rsp] - mul rbx - add r10,rax + mul rbx ; ap[0]*bp[i] + add r10,rax ; ap[0]*bp[i]+tp[0] mov rax,QWORD[rcx] adc rdx,0 - imul rbp,r10 + imul rbp,r10 ; tp[0]*n0 mov r11,rdx - mul rbp - add r10,rax + mul rbp ; np[0]*m1 + add r10,rax ; discarded mov rax,QWORD[8+rsi] adc rdx,0 - mov r10,QWORD[8+rsp] + mov r10,QWORD[8+rsp] ; tp[1] mov r13,rdx - lea r15,[1+r15] + lea r15,[1+r15] ; j++ jmp NEAR $L$inner_enter ALIGN 16 @@ -175,80 +175,80 @@ $L$inner: add r13,rax mov rax,QWORD[r15*8+rsi] adc rdx,0 - add r13,r10 + add r13,r10 ; np[j]*m1+ap[j]*bp[i]+tp[j] mov r10,QWORD[r15*8+rsp] adc rdx,0 - mov QWORD[((-16))+r15*8+rsp],r13 + mov QWORD[((-16))+r15*8+rsp],r13 ; tp[j-1] mov r13,rdx $L$inner_enter: - mul rbx + mul rbx ; ap[j]*bp[i] add r11,rax mov rax,QWORD[r15*8+rcx] adc rdx,0 - add r10,r11 + add r10,r11 ; ap[j]*bp[i]+tp[j] mov r11,rdx adc r11,0 - lea r15,[1+r15] + lea r15,[1+r15] ; j++ - mul rbp + mul rbp ; np[j]*m1 cmp r15,r9 jne NEAR $L$inner add r13,rax - mov rax,QWORD[rsi] + mov rax,QWORD[rsi] ; ap[0] adc rdx,0 - add r13,r10 + add r13,r10 ; np[j]*m1+ap[j]*bp[i]+tp[j] mov r10,QWORD[r15*8+rsp] adc rdx,0 - mov QWORD[((-16))+r15*8+rsp],r13 + mov QWORD[((-16))+r15*8+rsp],r13 ; tp[j-1] mov r13,rdx xor rdx,rdx add r13,r11 adc rdx,0 - add r13,r10 + add r13,r10 ; pull upmost overflow bit adc rdx,0 mov QWORD[((-8))+r9*8+rsp],r13 - mov QWORD[r9*8+rsp],rdx + mov QWORD[r9*8+rsp],rdx ; store upmost overflow bit - lea r14,[1+r14] + lea r14,[1+r14] ; i++ cmp r14,r9 jb NEAR $L$outer - xor r14,r14 - mov rax,QWORD[rsp] - mov r15,r9 + xor r14,r14 ; i=0 and clear CF! + mov rax,QWORD[rsp] ; tp[0] + mov r15,r9 ; j=num ALIGN 16 $L$sub: sbb rax,QWORD[r14*8+rcx] - mov QWORD[r14*8+rdi],rax - mov rax,QWORD[8+r14*8+rsp] - lea r14,[1+r14] - dec r15 + mov QWORD[r14*8+rdi],rax ; rp[i]=tp[i]-np[i] + mov rax,QWORD[8+r14*8+rsp] ; tp[i+1] + lea r14,[1+r14] ; i++ + dec r15 ; doesn't affect CF! jnz NEAR $L$sub - sbb rax,0 + sbb rax,0 ; handle upmost overflow bit mov rbx,-1 - xor rbx,rax + xor rbx,rax ; not %rax xor r14,r14 - mov r15,r9 + mov r15,r9 ; j=num -$L$copy: +$L$copy: ; conditional copy mov rcx,QWORD[r14*8+rdi] mov rdx,QWORD[r14*8+rsp] and rcx,rbx and rdx,rax - mov QWORD[r14*8+rsp],r9 + mov QWORD[r14*8+rsp],r9 ; zap temporary vector or rdx,rcx - mov QWORD[r14*8+rdi],rdx + mov QWORD[r14*8+rdi],rdx ; rp[i]=tp[i] lea r14,[1+r14] sub r15,1 jnz NEAR $L$copy - mov rsi,QWORD[8+r9*8+rsp] - + mov rsi,QWORD[8+r9*8+rsp] ; restore %rsp +; No return value mov r15,QWORD[((-48))+rsi] mov r14,QWORD[((-40))+rsi] @@ -305,9 +305,9 @@ _CET_ENDBR neg r9 mov r11,rsp - lea r10,[((-32))+r9*8+rsp] - neg r9 - and r10,-1024 + lea r10,[((-32))+r9*8+rsp] ; future alloca(8*(num+4)) + neg r9 ; restore + and r10,-1024 ; minimize TLB usage sub r11,r10 and r11,-4096 @@ -324,28 +324,28 @@ $L$mul4x_page_walk: ja NEAR $L$mul4x_page_walk $L$mul4x_page_walk_done: - mov QWORD[8+r9*8+rsp],rax + mov QWORD[8+r9*8+rsp],rax ; tp[num+1]=%rsp $L$mul4x_body: - mov QWORD[16+r9*8+rsp],rdi - mov r12,rdx - mov r8,QWORD[r8] - mov rbx,QWORD[r12] + mov QWORD[16+r9*8+rsp],rdi ; tp[num+2]=%rdi + mov r12,rdx ; reassign %r12 + mov r8,QWORD[r8] ; pull n0[0] value + mov rbx,QWORD[r12] ; m0=bp[0] mov rax,QWORD[rsi] - xor r14,r14 - xor r15,r15 + xor r14,r14 ; i=0 + xor r15,r15 ; j=0 mov rbp,r8 - mul rbx + mul rbx ; ap[0]*bp[0] mov r10,rax mov rax,QWORD[rcx] - imul rbp,r10 + imul rbp,r10 ; "tp[0]"*n0 mov r11,rdx - mul rbp - add r10,rax + mul rbp ; np[0]*m1 + add r10,rax ; discarded mov rax,QWORD[8+rsi] adc rdx,0 mov rdi,rdx @@ -361,171 +361,171 @@ $L$mul4x_body: mov rax,QWORD[16+rsi] adc rdx,0 add rdi,r11 - lea r15,[4+r15] + lea r15,[4+r15] ; j++ adc rdx,0 mov QWORD[rsp],rdi mov r13,rdx jmp NEAR $L$1st4x ALIGN 16 $L$1st4x: - mul rbx + mul rbx ; ap[j]*bp[0] add r10,rax mov rax,QWORD[((-16))+r15*8+rcx] adc rdx,0 mov r11,rdx - mul rbp + mul rbp ; np[j]*m1 add r13,rax mov rax,QWORD[((-8))+r15*8+rsi] adc rdx,0 - add r13,r10 + add r13,r10 ; np[j]*m1+ap[j]*bp[0] adc rdx,0 - mov QWORD[((-24))+r15*8+rsp],r13 + mov QWORD[((-24))+r15*8+rsp],r13 ; tp[j-1] mov rdi,rdx - mul rbx + mul rbx ; ap[j]*bp[0] add r11,rax mov rax,QWORD[((-8))+r15*8+rcx] adc rdx,0 mov r10,rdx - mul rbp + mul rbp ; np[j]*m1 add rdi,rax mov rax,QWORD[r15*8+rsi] adc rdx,0 - add rdi,r11 + add rdi,r11 ; np[j]*m1+ap[j]*bp[0] adc rdx,0 - mov QWORD[((-16))+r15*8+rsp],rdi + mov QWORD[((-16))+r15*8+rsp],rdi ; tp[j-1] mov r13,rdx - mul rbx + mul rbx ; ap[j]*bp[0] add r10,rax mov rax,QWORD[r15*8+rcx] adc rdx,0 mov r11,rdx - mul rbp + mul rbp ; np[j]*m1 add r13,rax mov rax,QWORD[8+r15*8+rsi] adc rdx,0 - add r13,r10 + add r13,r10 ; np[j]*m1+ap[j]*bp[0] adc rdx,0 - mov QWORD[((-8))+r15*8+rsp],r13 + mov QWORD[((-8))+r15*8+rsp],r13 ; tp[j-1] mov rdi,rdx - mul rbx + mul rbx ; ap[j]*bp[0] add r11,rax mov rax,QWORD[8+r15*8+rcx] adc rdx,0 - lea r15,[4+r15] + lea r15,[4+r15] ; j++ mov r10,rdx - mul rbp + mul rbp ; np[j]*m1 add rdi,rax mov rax,QWORD[((-16))+r15*8+rsi] adc rdx,0 - add rdi,r11 + add rdi,r11 ; np[j]*m1+ap[j]*bp[0] adc rdx,0 - mov QWORD[((-32))+r15*8+rsp],rdi + mov QWORD[((-32))+r15*8+rsp],rdi ; tp[j-1] mov r13,rdx cmp r15,r9 jb NEAR $L$1st4x - mul rbx + mul rbx ; ap[j]*bp[0] add r10,rax mov rax,QWORD[((-16))+r15*8+rcx] adc rdx,0 mov r11,rdx - mul rbp + mul rbp ; np[j]*m1 add r13,rax mov rax,QWORD[((-8))+r15*8+rsi] adc rdx,0 - add r13,r10 + add r13,r10 ; np[j]*m1+ap[j]*bp[0] adc rdx,0 - mov QWORD[((-24))+r15*8+rsp],r13 + mov QWORD[((-24))+r15*8+rsp],r13 ; tp[j-1] mov rdi,rdx - mul rbx + mul rbx ; ap[j]*bp[0] add r11,rax mov rax,QWORD[((-8))+r15*8+rcx] adc rdx,0 mov r10,rdx - mul rbp + mul rbp ; np[j]*m1 add rdi,rax - mov rax,QWORD[rsi] + mov rax,QWORD[rsi] ; ap[0] adc rdx,0 - add rdi,r11 + add rdi,r11 ; np[j]*m1+ap[j]*bp[0] adc rdx,0 - mov QWORD[((-16))+r15*8+rsp],rdi + mov QWORD[((-16))+r15*8+rsp],rdi ; tp[j-1] mov r13,rdx xor rdi,rdi add r13,r10 adc rdi,0 mov QWORD[((-8))+r15*8+rsp],r13 - mov QWORD[r15*8+rsp],rdi + mov QWORD[r15*8+rsp],rdi ; store upmost overflow bit - lea r14,[1+r14] + lea r14,[1+r14] ; i++ ALIGN 4 $L$outer4x: - mov rbx,QWORD[r14*8+r12] - xor r15,r15 + mov rbx,QWORD[r14*8+r12] ; m0=bp[i] + xor r15,r15 ; j=0 mov r10,QWORD[rsp] mov rbp,r8 - mul rbx - add r10,rax + mul rbx ; ap[0]*bp[i] + add r10,rax ; ap[0]*bp[i]+tp[0] mov rax,QWORD[rcx] adc rdx,0 - imul rbp,r10 + imul rbp,r10 ; tp[0]*n0 mov r11,rdx - mul rbp - add r10,rax + mul rbp ; np[0]*m1 + add r10,rax ; "%r13", discarded mov rax,QWORD[8+rsi] adc rdx,0 mov rdi,rdx - mul rbx + mul rbx ; ap[j]*bp[i] add r11,rax mov rax,QWORD[8+rcx] adc rdx,0 - add r11,QWORD[8+rsp] + add r11,QWORD[8+rsp] ; +tp[1] adc rdx,0 mov r10,rdx - mul rbp + mul rbp ; np[j]*m1 add rdi,rax mov rax,QWORD[16+rsi] adc rdx,0 - add rdi,r11 - lea r15,[4+r15] + add rdi,r11 ; np[j]*m1+ap[j]*bp[i]+tp[j] + lea r15,[4+r15] ; j+=2 adc rdx,0 - mov QWORD[rsp],rdi + mov QWORD[rsp],rdi ; tp[j-1] mov r13,rdx jmp NEAR $L$inner4x ALIGN 16 $L$inner4x: - mul rbx + mul rbx ; ap[j]*bp[i] add r10,rax mov rax,QWORD[((-16))+r15*8+rcx] adc rdx,0 - add r10,QWORD[((-16))+r15*8+rsp] + add r10,QWORD[((-16))+r15*8+rsp] ; ap[j]*bp[i]+tp[j] adc rdx,0 mov r11,rdx - mul rbp + mul rbp ; np[j]*m1 add r13,rax mov rax,QWORD[((-8))+r15*8+rsi] adc rdx,0 add r13,r10 adc rdx,0 - mov QWORD[((-24))+r15*8+rsp],r13 + mov QWORD[((-24))+r15*8+rsp],r13 ; tp[j-1] mov rdi,rdx - mul rbx + mul rbx ; ap[j]*bp[i] add r11,rax mov rax,QWORD[((-8))+r15*8+rcx] adc rdx,0 @@ -533,148 +533,148 @@ $L$inner4x: adc rdx,0 mov r10,rdx - mul rbp + mul rbp ; np[j]*m1 add rdi,rax mov rax,QWORD[r15*8+rsi] adc rdx,0 add rdi,r11 adc rdx,0 - mov QWORD[((-16))+r15*8+rsp],rdi + mov QWORD[((-16))+r15*8+rsp],rdi ; tp[j-1] mov r13,rdx - mul rbx + mul rbx ; ap[j]*bp[i] add r10,rax mov rax,QWORD[r15*8+rcx] adc rdx,0 - add r10,QWORD[r15*8+rsp] + add r10,QWORD[r15*8+rsp] ; ap[j]*bp[i]+tp[j] adc rdx,0 mov r11,rdx - mul rbp + mul rbp ; np[j]*m1 add r13,rax mov rax,QWORD[8+r15*8+rsi] adc rdx,0 add r13,r10 adc rdx,0 - mov QWORD[((-8))+r15*8+rsp],r13 + mov QWORD[((-8))+r15*8+rsp],r13 ; tp[j-1] mov rdi,rdx - mul rbx + mul rbx ; ap[j]*bp[i] add r11,rax mov rax,QWORD[8+r15*8+rcx] adc rdx,0 add r11,QWORD[8+r15*8+rsp] adc rdx,0 - lea r15,[4+r15] + lea r15,[4+r15] ; j++ mov r10,rdx - mul rbp + mul rbp ; np[j]*m1 add rdi,rax mov rax,QWORD[((-16))+r15*8+rsi] adc rdx,0 add rdi,r11 adc rdx,0 - mov QWORD[((-32))+r15*8+rsp],rdi + mov QWORD[((-32))+r15*8+rsp],rdi ; tp[j-1] mov r13,rdx cmp r15,r9 jb NEAR $L$inner4x - mul rbx + mul rbx ; ap[j]*bp[i] add r10,rax mov rax,QWORD[((-16))+r15*8+rcx] adc rdx,0 - add r10,QWORD[((-16))+r15*8+rsp] + add r10,QWORD[((-16))+r15*8+rsp] ; ap[j]*bp[i]+tp[j] adc rdx,0 mov r11,rdx - mul rbp + mul rbp ; np[j]*m1 add r13,rax mov rax,QWORD[((-8))+r15*8+rsi] adc rdx,0 add r13,r10 adc rdx,0 - mov QWORD[((-24))+r15*8+rsp],r13 + mov QWORD[((-24))+r15*8+rsp],r13 ; tp[j-1] mov rdi,rdx - mul rbx + mul rbx ; ap[j]*bp[i] add r11,rax mov rax,QWORD[((-8))+r15*8+rcx] adc rdx,0 add r11,QWORD[((-8))+r15*8+rsp] adc rdx,0 - lea r14,[1+r14] + lea r14,[1+r14] ; i++ mov r10,rdx - mul rbp + mul rbp ; np[j]*m1 add rdi,rax - mov rax,QWORD[rsi] + mov rax,QWORD[rsi] ; ap[0] adc rdx,0 add rdi,r11 adc rdx,0 - mov QWORD[((-16))+r15*8+rsp],rdi + mov QWORD[((-16))+r15*8+rsp],rdi ; tp[j-1] mov r13,rdx xor rdi,rdi add r13,r10 adc rdi,0 - add r13,QWORD[r9*8+rsp] + add r13,QWORD[r9*8+rsp] ; pull upmost overflow bit adc rdi,0 mov QWORD[((-8))+r15*8+rsp],r13 - mov QWORD[r15*8+rsp],rdi + mov QWORD[r15*8+rsp],rdi ; store upmost overflow bit cmp r14,r9 jb NEAR $L$outer4x - mov rdi,QWORD[16+r9*8+rsp] + mov rdi,QWORD[16+r9*8+rsp] ; restore %rdi lea r15,[((-4))+r9] - mov rax,QWORD[rsp] - mov rdx,QWORD[8+rsp] - shr r15,2 - lea rsi,[rsp] - xor r14,r14 + mov rax,QWORD[rsp] ; tp[0] + mov rdx,QWORD[8+rsp] ; tp[1] + shr r15,2 ; j=num/4-1 + lea rsi,[rsp] ; borrow ap for tp + xor r14,r14 ; i=0 and clear CF! sub rax,QWORD[rcx] - mov rbx,QWORD[16+rsi] - mov rbp,QWORD[24+rsi] + mov rbx,QWORD[16+rsi] ; tp[2] + mov rbp,QWORD[24+rsi] ; tp[3] sbb rdx,QWORD[8+rcx] $L$sub4x: - mov QWORD[r14*8+rdi],rax - mov QWORD[8+r14*8+rdi],rdx + mov QWORD[r14*8+rdi],rax ; rp[i]=tp[i]-np[i] + mov QWORD[8+r14*8+rdi],rdx ; rp[i]=tp[i]-np[i] sbb rbx,QWORD[16+r14*8+rcx] - mov rax,QWORD[32+r14*8+rsi] + mov rax,QWORD[32+r14*8+rsi] ; tp[i+1] mov rdx,QWORD[40+r14*8+rsi] sbb rbp,QWORD[24+r14*8+rcx] - mov QWORD[16+r14*8+rdi],rbx - mov QWORD[24+r14*8+rdi],rbp + mov QWORD[16+r14*8+rdi],rbx ; rp[i]=tp[i]-np[i] + mov QWORD[24+r14*8+rdi],rbp ; rp[i]=tp[i]-np[i] sbb rax,QWORD[32+r14*8+rcx] mov rbx,QWORD[48+r14*8+rsi] mov rbp,QWORD[56+r14*8+rsi] sbb rdx,QWORD[40+r14*8+rcx] - lea r14,[4+r14] - dec r15 + lea r14,[4+r14] ; i++ + dec r15 ; doesn't affect CF! jnz NEAR $L$sub4x - mov QWORD[r14*8+rdi],rax - mov rax,QWORD[32+r14*8+rsi] + mov QWORD[r14*8+rdi],rax ; rp[i]=tp[i]-np[i] + mov rax,QWORD[32+r14*8+rsi] ; load overflow bit sbb rbx,QWORD[16+r14*8+rcx] - mov QWORD[8+r14*8+rdi],rdx + mov QWORD[8+r14*8+rdi],rdx ; rp[i]=tp[i]-np[i] sbb rbp,QWORD[24+r14*8+rcx] - mov QWORD[16+r14*8+rdi],rbx + mov QWORD[16+r14*8+rdi],rbx ; rp[i]=tp[i]-np[i] - sbb rax,0 - mov QWORD[24+r14*8+rdi],rbp + sbb rax,0 ; handle upmost overflow bit + mov QWORD[24+r14*8+rdi],rbp ; rp[i]=tp[i]-np[i] pxor xmm0,xmm0 movq xmm4,rax pcmpeqd xmm5,xmm5 pshufd xmm4,xmm4,0 mov r15,r9 pxor xmm5,xmm4 - shr r15,2 - xor eax,eax + shr r15,2 ; j=num/4 + xor eax,eax ; i=0 jmp NEAR $L$copy4x ALIGN 16 -$L$copy4x: +$L$copy4x: ; conditional copy movdqa xmm1,XMMWORD[rax*1+rsp] movdqu xmm2,XMMWORD[rax*1+rdi] pand xmm1,xmm4 @@ -692,9 +692,9 @@ $L$copy4x: lea rax,[32+rax] dec r15 jnz NEAR $L$copy4x - mov rsi,QWORD[8+r9*8+rsp] - + mov rsi,QWORD[8+r9*8+rsp] ; restore %rsp +; No return value mov r15,QWORD[((-48))+rsi] mov r14,QWORD[((-40))+rsi] @@ -715,8 +715,8 @@ $L$mul4x_epilogue: ret $L$SEH_end_bn_mul4x_mont: -EXTERN bn_sqrx8x_internal -EXTERN bn_sqr8x_internal +EXTERN bn_sqrx8x_internal ; see x86_64-mont5 module +EXTERN bn_sqr8x_internal ; see x86_64-mont5 module global bn_sqr8x_mont @@ -754,30 +754,30 @@ _CET_ENDBR $L$sqr8x_prologue: mov r10d,r9d - shl r9d,3 - shl r10,3+2 + shl r9d,3 ; convert %r9 to bytes + shl r10,3+2 ; 4*%r9 neg r9 - - - - - +; ############################################################# +; ensure that stack frame doesn't alias with %rsi modulo +; 4096. this is done to allow memory disambiguation logic +; do its job. +; lea r11,[((-64))+r9*2+rsp] mov rbp,rsp - mov r8,QWORD[r8] + mov r8,QWORD[r8] ; *n0 sub r11,rsi and r11,4095 cmp r10,r11 jb NEAR $L$sqr8x_sp_alt - sub rbp,r11 - lea rbp,[((-64))+r9*2+rbp] + sub rbp,r11 ; align with %rsi + lea rbp,[((-64))+r9*2+rbp] ; future alloca(frame+2*%r9) jmp NEAR $L$sqr8x_sp_done ALIGN 32 $L$sqr8x_sp_alt: - lea r10,[((4096-64))+r9*2] - lea rbp,[((-64))+r9*2+rbp] + lea r10,[((4096-64))+r9*2] ; 4096-frame-2*%r9 + lea rbp,[((-64))+r9*2+rbp] ; future alloca(frame+2*%r9) sub r11,r10 mov r10,0 cmovc r11,r10 @@ -805,41 +805,41 @@ $L$sqr8x_page_walk_done: neg r9 mov QWORD[32+rsp],r8 - mov QWORD[40+rsp],rax + mov QWORD[40+rsp],rax ; save original %rsp $L$sqr8x_body: - movq xmm2,rcx + movq xmm2,rcx ; save pointer to modulus pxor xmm0,xmm0 - movq xmm1,rdi - movq xmm3,r10 + movq xmm1,rdi ; save %rdi + movq xmm3,r10 ; -%r9 test rdx,rdx jz NEAR $L$sqr8x_nox - call bn_sqrx8x_internal - - - - + call bn_sqrx8x_internal ; see x86_64-mont5 module +; %rax top-most carry +; %rbp nptr +; %rcx -8*num +; %r8 end of tp[2*num] lea rbx,[rcx*1+r8] mov r9,rcx mov rdx,rcx movq rdi,xmm1 - sar rcx,3+2 + sar rcx,3+2 ; %cf=0 jmp NEAR $L$sqr8x_sub ALIGN 32 $L$sqr8x_nox: - call bn_sqr8x_internal - - - - + call bn_sqr8x_internal ; see x86_64-mont5 module +; %rax top-most carry +; %rbp nptr +; %r8 -8*num +; %rdi end of tp[2*num] lea rbx,[r9*1+rdi] mov rcx,r9 mov rdx,r9 movq rdi,xmm1 - sar rcx,3+2 + sar rcx,3+2 ; %cf=0 jmp NEAR $L$sqr8x_sub ALIGN 32 @@ -859,17 +859,17 @@ $L$sqr8x_sub: mov QWORD[16+rdi],r14 mov QWORD[24+rdi],r15 lea rdi,[32+rdi] - inc rcx + inc rcx ; preserves %cf jnz NEAR $L$sqr8x_sub - sbb rax,0 - lea rbx,[r9*1+rbx] - lea rdi,[r9*1+rdi] + sbb rax,0 ; top-most carry + lea rbx,[r9*1+rbx] ; rewind + lea rdi,[r9*1+rdi] ; rewind movq xmm1,rax pxor xmm0,xmm0 pshufd xmm1,xmm1,0 - mov rsi,QWORD[40+rsp] + mov rsi,QWORD[40+rsp] ; restore %rsp jmp NEAR $L$sqr8x_cond_copy @@ -881,7 +881,7 @@ $L$sqr8x_cond_copy: movdqu xmm4,XMMWORD[rdi] movdqu xmm5,XMMWORD[16+rdi] lea rdi,[32+rdi] - movdqa XMMWORD[(-32)+rbx],xmm0 + movdqa XMMWORD[(-32)+rbx],xmm0 ; zero tp movdqa XMMWORD[(-16)+rbx],xmm0 movdqa XMMWORD[(-32)+rdx*1+rbx],xmm0 movdqa XMMWORD[(-16)+rdx*1+rbx],xmm0 @@ -898,7 +898,7 @@ $L$sqr8x_cond_copy: add r9,32 jnz NEAR $L$sqr8x_cond_copy - +; No return value mov r15,QWORD[((-48))+rsi] mov r14,QWORD[((-40))+rsi] @@ -953,11 +953,11 @@ _CET_ENDBR $L$mulx4x_prologue: - shl r9d,3 + shl r9d,3 ; convert %r9 to bytes xor r10,r10 - sub r10,r9 - mov r8,QWORD[r8] - lea rbp,[((-72))+r10*1+rsp] + sub r10,r9 ; -%r9 + mov r8,QWORD[r8] ; *n0 + lea rbp,[((-72))+r10*1+rsp] ; future alloca(frame+%r9+8) and rbp,-128 mov r11,rsp sub r11,rbp @@ -977,62 +977,62 @@ $L$mulx4x_page_walk: $L$mulx4x_page_walk_done: lea r10,[r9*1+rdx] - - - - - - - - - - - - - mov QWORD[rsp],r9 +; ############################################################# +; Stack layout +; +0 num +; +8 off-loaded &b[i] +; +16 end of b[num] +; +24 saved n0 +; +32 saved rp +; +40 saved %rsp +; +48 inner counter +; +56 +; +64 tmp[num+1] +; + mov QWORD[rsp],r9 ; save %r9 shr r9,5 - mov QWORD[16+rsp],r10 + mov QWORD[16+rsp],r10 ; end of b[num] sub r9,1 - mov QWORD[24+rsp],r8 - mov QWORD[32+rsp],rdi - mov QWORD[40+rsp],rax + mov QWORD[24+rsp],r8 ; save *n0 + mov QWORD[32+rsp],rdi ; save %rdi + mov QWORD[40+rsp],rax ; save original %rsp - mov QWORD[48+rsp],r9 + mov QWORD[48+rsp],r9 ; inner counter jmp NEAR $L$mulx4x_body ALIGN 32 $L$mulx4x_body: lea rdi,[8+rdx] - mov rdx,QWORD[rdx] + mov rdx,QWORD[rdx] ; b[0], %rdx==%rdx actually lea rbx,[((64+32))+rsp] mov r9,rdx - mulx rax,r8,QWORD[rsi] - mulx r14,r11,QWORD[8+rsi] + mulx rax,r8,QWORD[rsi] ; a[0]*b[0] + mulx r14,r11,QWORD[8+rsi] ; a[1]*b[0] add r11,rax - mov QWORD[8+rsp],rdi - mulx r13,r12,QWORD[16+rsi] + mov QWORD[8+rsp],rdi ; off-load &b[i] + mulx r13,r12,QWORD[16+rsi] ; ... adc r12,r14 adc r13,0 - mov rdi,r8 - imul r8,QWORD[24+rsp] - xor rbp,rbp + mov rdi,r8 ; borrow %rdi + imul r8,QWORD[24+rsp] ; "t[0]"*n0 + xor rbp,rbp ; cf=0, of=0 mulx r14,rax,QWORD[24+rsi] mov rdx,r8 lea rsi,[32+rsi] adcx r13,rax - adcx r14,rbp + adcx r14,rbp ; cf=0 mulx r10,rax,QWORD[rcx] - adcx rdi,rax + adcx rdi,rax ; discarded adox r10,r11 mulx r11,rax,QWORD[8+rcx] adcx r10,rax adox r11,r12 - DB 0xc4,0x62,0xfb,0xf6,0xa1,0x10,0x00,0x00,0x00 - mov rdi,QWORD[48+rsp] + DB 0xc4,0x62,0xfb,0xf6,0xa1,0x10,0x00,0x00,0x00 ; mulx 2*8(%rcx),%rax,%r12 + mov rdi,QWORD[48+rsp] ; counter value mov QWORD[((-32))+rbx],r10 adcx r11,rax adox r12,r13 @@ -1040,7 +1040,7 @@ $L$mulx4x_body: mov rdx,r9 mov QWORD[((-24))+rbx],r11 adcx r12,rax - adox r15,rbp + adox r15,rbp ; of=0 lea rcx,[32+rcx] mov QWORD[((-16))+rbx],r12 @@ -1048,18 +1048,18 @@ $L$mulx4x_body: ALIGN 32 $L$mulx4x_1st: - adcx r15,rbp - mulx rax,r10,QWORD[rsi] + adcx r15,rbp ; cf=0, modulo-scheduled + mulx rax,r10,QWORD[rsi] ; a[4]*b[0] adcx r10,r14 - mulx r14,r11,QWORD[8+rsi] + mulx r14,r11,QWORD[8+rsi] ; a[5]*b[0] adcx r11,rax - mulx rax,r12,QWORD[16+rsi] + mulx rax,r12,QWORD[16+rsi] ; ... adcx r12,r14 mulx r14,r13,QWORD[24+rsi] DB 0x67,0x67 mov rdx,r8 adcx r13,rax - adcx r14,rbp + adcx r14,rbp ; cf=0 lea rsi,[32+rsi] lea rbx,[32+rbx] @@ -1083,43 +1083,43 @@ $L$mulx4x_1st: lea rcx,[32+rcx] mov QWORD[((-16))+rbx],r13 - dec rdi + dec rdi ; of=0, pass cf jnz NEAR $L$mulx4x_1st - mov rax,QWORD[rsp] - mov rdi,QWORD[8+rsp] - adc r15,rbp + mov rax,QWORD[rsp] ; load num + mov rdi,QWORD[8+rsp] ; re-load &b[i] + adc r15,rbp ; modulo-scheduled add r14,r15 - sbb r15,r15 + sbb r15,r15 ; top-most carry mov QWORD[((-8))+rbx],r14 jmp NEAR $L$mulx4x_outer ALIGN 32 $L$mulx4x_outer: - mov rdx,QWORD[rdi] - lea rdi,[8+rdi] - sub rsi,rax - mov QWORD[rbx],r15 + mov rdx,QWORD[rdi] ; b[i] + lea rdi,[8+rdi] ; b++ + sub rsi,rax ; rewind %rsi + mov QWORD[rbx],r15 ; save top-most carry lea rbx,[((64+32))+rsp] - sub rcx,rax + sub rcx,rax ; rewind %rcx - mulx r11,r8,QWORD[rsi] - xor ebp,ebp + mulx r11,r8,QWORD[rsi] ; a[0]*b[i] + xor ebp,ebp ; xor %rbp,%rbp # cf=0, of=0 mov r9,rdx - mulx r12,r14,QWORD[8+rsi] + mulx r12,r14,QWORD[8+rsi] ; a[1]*b[i] adox r8,QWORD[((-32))+rbx] adcx r11,r14 - mulx r13,r15,QWORD[16+rsi] + mulx r13,r15,QWORD[16+rsi] ; ... adox r11,QWORD[((-24))+rbx] adcx r12,r15 adox r12,QWORD[((-16))+rbx] adcx r13,rbp adox r13,rbp - mov QWORD[8+rsp],rdi + mov QWORD[8+rsp],rdi ; off-load &b[i] mov r15,r8 - imul r8,QWORD[24+rsp] - xor ebp,ebp + imul r8,QWORD[24+rsp] ; "t[0]"*n0 + xor ebp,ebp ; xor %rbp,%rbp # cf=0, of=0 mulx r14,rax,QWORD[24+rsi] mov rdx,r8 @@ -1130,7 +1130,7 @@ $L$mulx4x_outer: adox r14,rbp mulx r10,rax,QWORD[rcx] - adcx r15,rax + adcx r15,rax ; discarded adox r10,r11 mulx r11,rax,QWORD[8+rcx] adcx r10,rax @@ -1144,21 +1144,21 @@ $L$mulx4x_outer: mov QWORD[((-24))+rbx],r11 lea rcx,[32+rcx] adcx r12,rax - adox r15,rbp - mov rdi,QWORD[48+rsp] + adox r15,rbp ; of=0 + mov rdi,QWORD[48+rsp] ; counter value mov QWORD[((-16))+rbx],r12 jmp NEAR $L$mulx4x_inner ALIGN 32 $L$mulx4x_inner: - mulx rax,r10,QWORD[rsi] - adcx r15,rbp + mulx rax,r10,QWORD[rsi] ; a[4]*b[i] + adcx r15,rbp ; cf=0, modulo-scheduled adox r10,r14 - mulx r14,r11,QWORD[8+rsi] + mulx r14,r11,QWORD[8+rsi] ; a[5]*b[i] adcx r10,QWORD[rbx] adox r11,rax - mulx rax,r12,QWORD[16+rsi] + mulx rax,r12,QWORD[16+rsi] ; ... adcx r11,QWORD[8+rbx] adox r12,r14 mulx r14,r13,QWORD[24+rsi] @@ -1166,10 +1166,10 @@ $L$mulx4x_inner: adcx r12,QWORD[16+rbx] adox r13,rax adcx r13,QWORD[24+rbx] - adox r14,rbp + adox r14,rbp ; of=0 lea rsi,[32+rsi] lea rbx,[32+rbx] - adcx r14,rbp + adcx r14,rbp ; cf=0 adox r10,r15 mulx r15,rax,QWORD[rcx] @@ -1191,26 +1191,26 @@ $L$mulx4x_inner: lea rcx,[32+rcx] mov QWORD[((-16))+rbx],r13 - dec rdi + dec rdi ; of=0, pass cf jnz NEAR $L$mulx4x_inner - mov rax,QWORD[rsp] - mov rdi,QWORD[8+rsp] - adc r15,rbp - sub rbp,QWORD[rbx] + mov rax,QWORD[rsp] ; load num + mov rdi,QWORD[8+rsp] ; re-load &b[i] + adc r15,rbp ; modulo-scheduled + sub rbp,QWORD[rbx] ; pull top-most carry adc r14,r15 - sbb r15,r15 + sbb r15,r15 ; top-most carry mov QWORD[((-8))+rbx],r14 cmp rdi,QWORD[16+rsp] jne NEAR $L$mulx4x_outer lea rbx,[64+rsp] - sub rcx,rax + sub rcx,rax ; rewind %rcx neg r15 mov rdx,rax - shr rax,3+2 - mov rdi,QWORD[32+rsp] + shr rax,3+2 ; %cf=0 + mov rdi,QWORD[32+rsp] ; restore rp jmp NEAR $L$mulx4x_sub ALIGN 32 @@ -1230,17 +1230,17 @@ $L$mulx4x_sub: mov QWORD[16+rdi],r13 mov QWORD[24+rdi],r14 lea rdi,[32+rdi] - dec rax + dec rax ; preserves %cf jnz NEAR $L$mulx4x_sub - sbb r15,0 + sbb r15,0 ; top-most carry lea rbx,[64+rsp] - sub rdi,rdx + sub rdi,rdx ; rewind movq xmm1,r15 pxor xmm0,xmm0 pshufd xmm1,xmm1,0 - mov rsi,QWORD[40+rsp] + mov rsi,QWORD[40+rsp] ; restore %rsp jmp NEAR $L$mulx4x_cond_copy @@ -1252,7 +1252,7 @@ $L$mulx4x_cond_copy: movdqu xmm4,XMMWORD[rdi] movdqu xmm5,XMMWORD[16+rdi] lea rdi,[32+rdi] - movdqa XMMWORD[(-32)+rbx],xmm0 + movdqa XMMWORD[(-32)+rbx],xmm0 ; zero tp movdqa XMMWORD[(-16)+rbx],xmm0 pcmpeqd xmm0,xmm1 pand xmm2,xmm1 @@ -1269,7 +1269,7 @@ $L$mulx4x_cond_copy: mov QWORD[rbx],rdx - +; No return value mov r15,QWORD[((-48))+rsi] mov r14,QWORD[((-40))+rsi] @@ -1311,26 +1311,26 @@ mul_handler: pushfq sub rsp,64 - mov rax,QWORD[120+r8] - mov rbx,QWORD[248+r8] + mov rax,QWORD[120+r8] ; pull context->Rax + mov rbx,QWORD[248+r8] ; pull context->Rip - mov rsi,QWORD[8+r9] - mov r11,QWORD[56+r9] + mov rsi,QWORD[8+r9] ; disp->ImageBase + mov r11,QWORD[56+r9] ; disp->HandlerData - mov r10d,DWORD[r11] - lea r10,[r10*1+rsi] - cmp rbx,r10 + mov r10d,DWORD[r11] ; HandlerData[0] + lea r10,[r10*1+rsi] ; end of prologue label + cmp rbx,r10 ; context->RipRsp - mov r10d,DWORD[4+r11] - lea r10,[r10*1+rsi] - cmp rbx,r10 + mov r10d,DWORD[4+r11] ; HandlerData[1] + lea r10,[r10*1+rsi] ; epilogue label + cmp rbx,r10 ; context->Rip>=epilogue label jae NEAR $L$common_seh_tail - mov r10,QWORD[192+r8] - mov rax,QWORD[8+r10*8+rax] + mov r10,QWORD[192+r8] ; pull %r9 + mov rax,QWORD[8+r10*8+rax] ; pull saved stack pointer jmp NEAR $L$common_pop_regs @@ -1349,30 +1349,30 @@ sqr_handler: pushfq sub rsp,64 - mov rax,QWORD[120+r8] - mov rbx,QWORD[248+r8] + mov rax,QWORD[120+r8] ; pull context->Rax + mov rbx,QWORD[248+r8] ; pull context->Rip - mov rsi,QWORD[8+r9] - mov r11,QWORD[56+r9] + mov rsi,QWORD[8+r9] ; disp->ImageBase + mov r11,QWORD[56+r9] ; disp->HandlerData - mov r10d,DWORD[r11] - lea r10,[r10*1+rsi] - cmp rbx,r10 + mov r10d,DWORD[r11] ; HandlerData[0] + lea r10,[r10*1+rsi] ; end of prologue label + cmp rbx,r10 ; context->Rip<.Lsqr_prologue jb NEAR $L$common_seh_tail - mov r10d,DWORD[4+r11] - lea r10,[r10*1+rsi] - cmp rbx,r10 + mov r10d,DWORD[4+r11] ; HandlerData[1] + lea r10,[r10*1+rsi] ; body label + cmp rbx,r10 ; context->Rip<.Lsqr_body jb NEAR $L$common_pop_regs - mov rax,QWORD[152+r8] + mov rax,QWORD[152+r8] ; pull context->Rsp - mov r10d,DWORD[8+r11] - lea r10,[r10*1+rsi] - cmp rbx,r10 + mov r10d,DWORD[8+r11] ; HandlerData[2] + lea r10,[r10*1+rsi] ; epilogue label + cmp rbx,r10 ; context->Rip>=.Lsqr_epilogue jae NEAR $L$common_seh_tail - mov rax,QWORD[40+rax] + mov rax,QWORD[40+rax] ; pull saved stack pointer $L$common_pop_regs: mov rbx,QWORD[((-8))+rax] @@ -1381,40 +1381,40 @@ $L$common_pop_regs: mov r13,QWORD[((-32))+rax] mov r14,QWORD[((-40))+rax] mov r15,QWORD[((-48))+rax] - mov QWORD[144+r8],rbx - mov QWORD[160+r8],rbp - mov QWORD[216+r8],r12 - mov QWORD[224+r8],r13 - mov QWORD[232+r8],r14 - mov QWORD[240+r8],r15 + mov QWORD[144+r8],rbx ; restore context->Rbx + mov QWORD[160+r8],rbp ; restore context->Rbp + mov QWORD[216+r8],r12 ; restore context->R12 + mov QWORD[224+r8],r13 ; restore context->R13 + mov QWORD[232+r8],r14 ; restore context->R14 + mov QWORD[240+r8],r15 ; restore context->R15 $L$common_seh_tail: mov rdi,QWORD[8+rax] mov rsi,QWORD[16+rax] - mov QWORD[152+r8],rax - mov QWORD[168+r8],rsi - mov QWORD[176+r8],rdi + mov QWORD[152+r8],rax ; restore context->Rsp + mov QWORD[168+r8],rsi ; restore context->Rsi + mov QWORD[176+r8],rdi ; restore context->Rdi - mov rdi,QWORD[40+r9] - mov rsi,r8 - mov ecx,154 - DD 0xa548f3fc + mov rdi,QWORD[40+r9] ; disp->ContextRecord + mov rsi,r8 ; context + mov ecx,154 ; sizeof(CONTEXT) + DD 0xa548f3fc ; cld; rep movsq mov rsi,r9 - xor rcx,rcx - mov rdx,QWORD[8+rsi] - mov r8,QWORD[rsi] - mov r9,QWORD[16+rsi] - mov r10,QWORD[40+rsi] - lea r11,[56+rsi] - lea r12,[24+rsi] - mov QWORD[32+rsp],r10 - mov QWORD[40+rsp],r11 - mov QWORD[48+rsp],r12 - mov QWORD[56+rsp],rcx + xor rcx,rcx ; arg1, UNW_FLAG_NHANDLER + mov rdx,QWORD[8+rsi] ; arg2, disp->ImageBase + mov r8,QWORD[rsi] ; arg3, disp->ControlPc + mov r9,QWORD[16+rsi] ; arg4, disp->FunctionEntry + mov r10,QWORD[40+rsi] ; disp->ContextRecord + lea r11,[56+rsi] ; &disp->HandlerData + lea r12,[24+rsi] ; &disp->EstablisherFrame + mov QWORD[32+rsp],r10 ; arg5 + mov QWORD[40+rsp],r11 ; arg6 + mov QWORD[48+rsp],r12 ; arg7 + mov QWORD[56+rsp],rcx ; arg8, (NULL) call QWORD[__imp_RtlVirtualUnwind] - mov eax,1 + mov eax,1 ; ExceptionContinueSearch add rsp,64 popfq pop r15 @@ -1449,20 +1449,20 @@ ALIGN 8 $L$SEH_info_bn_mul_mont_nohw: DB 9,0,0,0 DD mul_handler wrt ..imagebase - DD $L$mul_body wrt ..imagebase,$L$mul_epilogue wrt ..imagebase + DD $L$mul_body wrt ..imagebase,$L$mul_epilogue wrt ..imagebase ; HandlerData[] $L$SEH_info_bn_mul4x_mont: DB 9,0,0,0 DD mul_handler wrt ..imagebase - DD $L$mul4x_body wrt ..imagebase,$L$mul4x_epilogue wrt ..imagebase + DD $L$mul4x_body wrt ..imagebase,$L$mul4x_epilogue wrt ..imagebase ; HandlerData[] $L$SEH_info_bn_sqr8x_mont: DB 9,0,0,0 DD sqr_handler wrt ..imagebase - DD $L$sqr8x_prologue wrt ..imagebase,$L$sqr8x_body wrt ..imagebase,$L$sqr8x_epilogue wrt ..imagebase + DD $L$sqr8x_prologue wrt ..imagebase,$L$sqr8x_body wrt ..imagebase,$L$sqr8x_epilogue wrt ..imagebase ; HandlerData[] ALIGN 8 $L$SEH_info_bn_mulx4x_mont: DB 9,0,0,0 DD sqr_handler wrt ..imagebase - DD $L$mulx4x_prologue wrt ..imagebase,$L$mulx4x_body wrt ..imagebase,$L$mulx4x_epilogue wrt ..imagebase + DD $L$mulx4x_prologue wrt ..imagebase,$L$mulx4x_body wrt ..imagebase,$L$mulx4x_epilogue wrt ..imagebase ; HandlerData[] ALIGN 8 %else ; Work around https://bugzilla.nasm.us/show_bug.cgi?id=3392738 diff --git a/third_party/boringssl/gen/bcm/x86_64-mont5-apple.S b/third_party/boringssl/gen/bcm/x86_64-mont5-apple.S index cd7d797b5..ae91a95a4 100644 --- a/third_party/boringssl/gen/bcm/x86_64-mont5-apple.S +++ b/third_party/boringssl/gen/bcm/x86_64-mont5-apple.S @@ -13,12 +13,12 @@ _bn_mul_mont_gather5_nohw: _CET_ENDBR - - +// num is declared as an int, a 32-bit parameter, so the upper half is +// undefined. Zero the upper half to normalize it. movl %r9d,%r9d movq %rsp,%rax - movd 8(%rsp),%xmm5 + movd 8(%rsp),%xmm5 // load 7th argument pushq %rbx pushq %rbp @@ -34,18 +34,18 @@ _CET_ENDBR negq %r9 movq %rsp,%r11 - leaq -280(%rsp,%r9,8),%r10 - negq %r9 - andq $-1024,%r10 - - - - - - - - - + leaq -280(%rsp,%r9,8),%r10 // future alloca(8*(num+2)+256+8) + negq %r9 // restore %r9 + andq $-1024,%r10 // minimize TLB usage + +// An OS-agnostic version of __chkstk. +// +// Some OSes (Windows) insist on stack being "wired" to +// physical memory in strictly sequential manner, i.e. if stack +// allocation spans two pages, then reference to farmost one can +// be punishable by SEGV. But page walking can do good even on +// other OSes, because it guarantees that villain thread hits +// the guard page before it can make damage to innocent one... subq %r10,%r11 andq $-4096,%r11 leaq (%r10,%r11,1),%rsp @@ -62,35 +62,35 @@ L$mul_page_walk: L$mul_page_walk_done: leaq L$inc(%rip),%r10 - movq %rax,8(%rsp,%r9,8) + movq %rax,8(%rsp,%r9,8) // tp[num+1]=%rsp L$mul_body: - leaq 128(%rdx),%r12 - movdqa 0(%r10),%xmm0 - movdqa 16(%r10),%xmm1 - leaq 24-112(%rsp,%r9,8),%r10 + leaq 128(%rdx),%r12 // reassign %rdx (+size optimization) + movdqa 0(%r10),%xmm0 // 00000001000000010000000000000000 + movdqa 16(%r10),%xmm1 // 00000002000000020000000200000002 + leaq 24-112(%rsp,%r9,8),%r10 // place the mask after tp[num+3] (+ICache optimization) andq $-16,%r10 - pshufd $0,%xmm5,%xmm5 + pshufd $0,%xmm5,%xmm5 // broadcast index movdqa %xmm1,%xmm4 movdqa %xmm1,%xmm2 paddd %xmm0,%xmm1 - pcmpeqd %xmm5,%xmm0 + pcmpeqd %xmm5,%xmm0 // compare to 1,0 .byte 0x67 movdqa %xmm4,%xmm3 paddd %xmm1,%xmm2 - pcmpeqd %xmm5,%xmm1 + pcmpeqd %xmm5,%xmm1 // compare to 3,2 movdqa %xmm0,112(%r10) movdqa %xmm4,%xmm0 paddd %xmm2,%xmm3 - pcmpeqd %xmm5,%xmm2 + pcmpeqd %xmm5,%xmm2 // compare to 5,4 movdqa %xmm1,128(%r10) movdqa %xmm4,%xmm1 paddd %xmm3,%xmm0 - pcmpeqd %xmm5,%xmm3 + pcmpeqd %xmm5,%xmm3 // compare to 7,6 movdqa %xmm2,144(%r10) movdqa %xmm4,%xmm2 @@ -99,17 +99,17 @@ L$mul_body: movdqa %xmm3,160(%r10) movdqa %xmm4,%xmm3 paddd %xmm1,%xmm2 - pcmpeqd %xmm5,%xmm1 + pcmpeqd %xmm5,%xmm1 // compare to 3,2 movdqa %xmm0,176(%r10) movdqa %xmm4,%xmm0 paddd %xmm2,%xmm3 - pcmpeqd %xmm5,%xmm2 + pcmpeqd %xmm5,%xmm2 // compare to 5,4 movdqa %xmm1,192(%r10) movdqa %xmm4,%xmm1 paddd %xmm3,%xmm0 - pcmpeqd %xmm5,%xmm3 + pcmpeqd %xmm5,%xmm3 // compare to 7,6 movdqa %xmm2,208(%r10) movdqa %xmm4,%xmm2 @@ -118,17 +118,17 @@ L$mul_body: movdqa %xmm3,224(%r10) movdqa %xmm4,%xmm3 paddd %xmm1,%xmm2 - pcmpeqd %xmm5,%xmm1 + pcmpeqd %xmm5,%xmm1 // compare to 3,2 movdqa %xmm0,240(%r10) movdqa %xmm4,%xmm0 paddd %xmm2,%xmm3 - pcmpeqd %xmm5,%xmm2 + pcmpeqd %xmm5,%xmm2 // compare to 5,4 movdqa %xmm1,256(%r10) movdqa %xmm4,%xmm1 paddd %xmm3,%xmm0 - pcmpeqd %xmm5,%xmm3 + pcmpeqd %xmm5,%xmm3 // compare to 7,6 movdqa %xmm2,272(%r10) movdqa %xmm4,%xmm2 @@ -147,7 +147,7 @@ L$mul_body: pcmpeqd %xmm5,%xmm3 movdqa %xmm2,336(%r10) - pand 64(%r12),%xmm0 + pand 64(%r12),%xmm0 // while it's still in register pand 80(%r12),%xmm1 pand 96(%r12),%xmm2 @@ -192,33 +192,33 @@ L$mul_body: por %xmm2,%xmm0 por %xmm3,%xmm1 por %xmm1,%xmm0 - - pshufd $0x4e,%xmm0,%xmm1 +// Combine the upper and lower halves of %xmm0. + pshufd $0x4e,%xmm0,%xmm1 // Swap upper and lower halves. por %xmm1,%xmm0 leaq 256(%r12),%r12 - movq %xmm0,%rbx + movq %xmm0,%rbx // m0=bp[0] - movq (%r8),%r8 + movq (%r8),%r8 // pull n0[0] value movq (%rsi),%rax - xorq %r14,%r14 - xorq %r15,%r15 + xorq %r14,%r14 // i=0 + xorq %r15,%r15 // j=0 movq %r8,%rbp - mulq %rbx + mulq %rbx // ap[0]*bp[0] movq %rax,%r10 movq (%rcx),%rax - imulq %r10,%rbp + imulq %r10,%rbp // "tp[0]"*n0 movq %rdx,%r11 - mulq %rbp - addq %rax,%r10 + mulq %rbp // np[0]*m1 + addq %rax,%r10 // discarded movq 8(%rsi),%rax adcq $0,%rdx movq %rdx,%r13 - leaq 1(%r15),%r15 + leaq 1(%r15),%r15 // j++ jmp L$1st_enter .p2align 4 @@ -226,30 +226,30 @@ L$1st: addq %rax,%r13 movq (%rsi,%r15,8),%rax adcq $0,%rdx - addq %r11,%r13 + addq %r11,%r13 // np[j]*m1+ap[j]*bp[0] movq %r10,%r11 adcq $0,%rdx - movq %r13,-16(%rsp,%r15,8) + movq %r13,-16(%rsp,%r15,8) // tp[j-1] movq %rdx,%r13 L$1st_enter: - mulq %rbx + mulq %rbx // ap[j]*bp[0] addq %rax,%r11 movq (%rcx,%r15,8),%rax adcq $0,%rdx - leaq 1(%r15),%r15 + leaq 1(%r15),%r15 // j++ movq %rdx,%r10 - mulq %rbp + mulq %rbp // np[j]*m1 cmpq %r9,%r15 - jne L$1st - + jne L$1st // note that upon exit %r15==%r9, so +// they can be used interchangeably addq %rax,%r13 adcq $0,%rdx - addq %r11,%r13 + addq %r11,%r13 // np[j]*m1+ap[j]*bp[0] adcq $0,%rdx - movq %r13,-16(%rsp,%r9,8) + movq %r13,-16(%rsp,%r9,8) // tp[num-1] movq %rdx,%r13 movq %r10,%r11 @@ -257,13 +257,13 @@ L$1st_enter: addq %r11,%r13 adcq $0,%rdx movq %r13,-8(%rsp,%r9,8) - movq %rdx,(%rsp,%r9,8) + movq %rdx,(%rsp,%r9,8) // store upmost overflow bit - leaq 1(%r14),%r14 + leaq 1(%r14),%r14 // i++ jmp L$outer .p2align 4 L$outer: - leaq 24+128(%rsp,%r9,8),%rdx + leaq 24+128(%rsp,%r9,8),%rdx // where 256-byte mask is (+size optimization) andq $-16,%rdx pxor %xmm4,%xmm4 pxor %xmm5,%xmm5 @@ -316,34 +316,34 @@ L$outer: por %xmm2,%xmm4 por %xmm3,%xmm5 por %xmm5,%xmm4 - - pshufd $0x4e,%xmm4,%xmm0 +// Combine the upper and lower halves of %xmm4 as %xmm0. + pshufd $0x4e,%xmm4,%xmm0 // Swap upper and lower halves. por %xmm4,%xmm0 leaq 256(%r12),%r12 - movq (%rsi),%rax - movq %xmm0,%rbx + movq (%rsi),%rax // ap[0] + movq %xmm0,%rbx // m0=bp[i] - xorq %r15,%r15 + xorq %r15,%r15 // j=0 movq %r8,%rbp movq (%rsp),%r10 - mulq %rbx - addq %rax,%r10 + mulq %rbx // ap[0]*bp[i] + addq %rax,%r10 // ap[0]*bp[i]+tp[0] movq (%rcx),%rax adcq $0,%rdx - imulq %r10,%rbp + imulq %r10,%rbp // tp[0]*n0 movq %rdx,%r11 - mulq %rbp - addq %rax,%r10 + mulq %rbp // np[0]*m1 + addq %rax,%r10 // discarded movq 8(%rsi),%rax adcq $0,%rdx - movq 8(%rsp),%r10 + movq 8(%rsp),%r10 // tp[1] movq %rdx,%r13 - leaq 1(%r15),%r15 + leaq 1(%r15),%r15 // j++ jmp L$inner_enter .p2align 4 @@ -351,78 +351,78 @@ L$inner: addq %rax,%r13 movq (%rsi,%r15,8),%rax adcq $0,%rdx - addq %r10,%r13 + addq %r10,%r13 // np[j]*m1+ap[j]*bp[i]+tp[j] movq (%rsp,%r15,8),%r10 adcq $0,%rdx - movq %r13,-16(%rsp,%r15,8) + movq %r13,-16(%rsp,%r15,8) // tp[j-1] movq %rdx,%r13 L$inner_enter: - mulq %rbx + mulq %rbx // ap[j]*bp[i] addq %rax,%r11 movq (%rcx,%r15,8),%rax adcq $0,%rdx - addq %r11,%r10 + addq %r11,%r10 // ap[j]*bp[i]+tp[j] movq %rdx,%r11 adcq $0,%r11 - leaq 1(%r15),%r15 + leaq 1(%r15),%r15 // j++ - mulq %rbp + mulq %rbp // np[j]*m1 cmpq %r9,%r15 - jne L$inner - + jne L$inner // note that upon exit %r15==%r9, so +// they can be used interchangeably addq %rax,%r13 adcq $0,%rdx - addq %r10,%r13 + addq %r10,%r13 // np[j]*m1+ap[j]*bp[i]+tp[j] movq (%rsp,%r9,8),%r10 adcq $0,%rdx - movq %r13,-16(%rsp,%r9,8) + movq %r13,-16(%rsp,%r9,8) // tp[num-1] movq %rdx,%r13 xorq %rdx,%rdx addq %r11,%r13 adcq $0,%rdx - addq %r10,%r13 + addq %r10,%r13 // pull upmost overflow bit adcq $0,%rdx movq %r13,-8(%rsp,%r9,8) - movq %rdx,(%rsp,%r9,8) + movq %rdx,(%rsp,%r9,8) // store upmost overflow bit - leaq 1(%r14),%r14 + leaq 1(%r14),%r14 // i++ cmpq %r9,%r14 jb L$outer - xorq %r14,%r14 - movq (%rsp),%rax - leaq (%rsp),%rsi - movq %r9,%r15 + xorq %r14,%r14 // i=0 and clear CF + movq (%rsp),%rax // tp[0] + leaq (%rsp),%rsi // borrow ap for tp + movq %r9,%r15 // j=num jmp L$sub .p2align 4 L$sub: sbbq (%rcx,%r14,8),%rax - movq %rax,(%rdi,%r14,8) - movq 8(%rsi,%r14,8),%rax - leaq 1(%r14),%r14 - decq %r15 + movq %rax,(%rdi,%r14,8) // rp[i]=tp[i]-np[i] + movq 8(%rsi,%r14,8),%rax // tp[i+1] + leaq 1(%r14),%r14 // i++ + decq %r15 // doesn't affect CF jnz L$sub - sbbq $0,%rax + sbbq $0,%rax // handle upmost overflow bit movq $-1,%rbx xorq %rax,%rbx xorq %r14,%r14 - movq %r9,%r15 + movq %r9,%r15 // j=num -L$copy: +L$copy: // conditional copy movq (%rdi,%r14,8),%rcx movq (%rsp,%r14,8),%rdx andq %rbx,%rcx andq %rax,%rdx - movq %r14,(%rsp,%r14,8) + movq %r14,(%rsp,%r14,8) // zap temporary vector orq %rcx,%rdx - movq %rdx,(%rdi,%r14,8) + movq %rdx,(%rdi,%r14,8) // rp[i]=tp[i] leaq 1(%r14),%r14 subq $1,%r15 jnz L$copy - movq 8(%rsp,%r9,8),%rsi + movq 8(%rsp,%r9,8),%rsi // restore %rsp movq $1,%rax @@ -448,6 +448,7 @@ L$mul_epilogue: .private_extern _bn_mul4x_mont_gather5 .p2align 5 +.alt_entry _bn_mul4x_mont_gather5 _bn_mul4x_mont_gather5: _CET_ENDBR @@ -469,36 +470,36 @@ _CET_ENDBR L$mul4x_prologue: .byte 0x67 - - - - shll $3,%r9d - leaq (%r9,%r9,2),%r10 - negq %r9 - - - - - - - - - - +// num is declared as an int, a 32-bit parameter, so the upper half is +// undefined. It is important that this write to %r9, which zeros the +// upper half, predates the first access. + shll $3,%r9d // convert %r9 to bytes + leaq (%r9,%r9,2),%r10 // 3*%r9 in bytes + negq %r9 // -%r9 + +// ############################################################# +// Ensure that stack frame doesn't alias with +3*%r9 +// modulo 4096, which covers ret[num], am[num] and n[num] +// (see bn_exp.c). This is done to allow memory disambiguation +// logic do its magic. [Extra [num] is allocated in order +// to align with bn_power5's frame, which is cleansed after +// completing exponentiation. Extra 256 bytes is for power mask +// calculated from 7th argument, the index.] +// leaq -320(%rsp,%r9,2),%r11 movq %rsp,%rbp subq %rdi,%r11 andq $4095,%r11 cmpq %r11,%r10 jb L$mul4xsp_alt - subq %r11,%rbp - leaq -320(%rbp,%r9,2),%rbp + subq %r11,%rbp // align with %rdi + leaq -320(%rbp,%r9,2),%rbp // future alloca(frame+2*num*8+256) jmp L$mul4xsp_done .p2align 5 L$mul4xsp_alt: leaq 4096-320(,%r9,2),%r10 - leaq -320(%rbp,%r9,2),%rbp + leaq -320(%rbp,%r9,2),%rbp // future alloca(frame+2*num*8+256) subq %r10,%r11 movq $0,%r10 cmovcq %r10,%r11 @@ -529,7 +530,7 @@ L$mul4x_body: call mul4x_internal - movq 40(%rsp),%rsi + movq 40(%rsp),%rsi // restore %rsp movq $1,%rax @@ -554,38 +555,39 @@ L$mul4x_epilogue: .p2align 5 +.alt_entry mul4x_internal mul4x_internal: - shlq $5,%r9 - movd 8(%rax),%xmm5 + shlq $5,%r9 // %r9 was in bytes + movd 8(%rax),%xmm5 // load 7th argument, index leaq L$inc(%rip),%rax - leaq 128(%rdx,%r9,1),%r13 - shrq $5,%r9 - movdqa 0(%rax),%xmm0 - movdqa 16(%rax),%xmm1 - leaq 88-112(%rsp,%r9,1),%r10 - leaq 128(%rdx),%r12 - - pshufd $0,%xmm5,%xmm5 + leaq 128(%rdx,%r9,1),%r13 // end of powers table (+size optimization) + shrq $5,%r9 // restore %r9 + movdqa 0(%rax),%xmm0 // 00000001000000010000000000000000 + movdqa 16(%rax),%xmm1 // 00000002000000020000000200000002 + leaq 88-112(%rsp,%r9,1),%r10 // place the mask after tp[num+1] (+ICache optimization) + leaq 128(%rdx),%r12 // size optimization + + pshufd $0,%xmm5,%xmm5 // broadcast index movdqa %xmm1,%xmm4 .byte 0x67,0x67 movdqa %xmm1,%xmm2 paddd %xmm0,%xmm1 - pcmpeqd %xmm5,%xmm0 + pcmpeqd %xmm5,%xmm0 // compare to 1,0 .byte 0x67 movdqa %xmm4,%xmm3 paddd %xmm1,%xmm2 - pcmpeqd %xmm5,%xmm1 + pcmpeqd %xmm5,%xmm1 // compare to 3,2 movdqa %xmm0,112(%r10) movdqa %xmm4,%xmm0 paddd %xmm2,%xmm3 - pcmpeqd %xmm5,%xmm2 + pcmpeqd %xmm5,%xmm2 // compare to 5,4 movdqa %xmm1,128(%r10) movdqa %xmm4,%xmm1 paddd %xmm3,%xmm0 - pcmpeqd %xmm5,%xmm3 + pcmpeqd %xmm5,%xmm3 // compare to 7,6 movdqa %xmm2,144(%r10) movdqa %xmm4,%xmm2 @@ -594,17 +596,17 @@ mul4x_internal: movdqa %xmm3,160(%r10) movdqa %xmm4,%xmm3 paddd %xmm1,%xmm2 - pcmpeqd %xmm5,%xmm1 + pcmpeqd %xmm5,%xmm1 // compare to 3,2 movdqa %xmm0,176(%r10) movdqa %xmm4,%xmm0 paddd %xmm2,%xmm3 - pcmpeqd %xmm5,%xmm2 + pcmpeqd %xmm5,%xmm2 // compare to 5,4 movdqa %xmm1,192(%r10) movdqa %xmm4,%xmm1 paddd %xmm3,%xmm0 - pcmpeqd %xmm5,%xmm3 + pcmpeqd %xmm5,%xmm3 // compare to 7,6 movdqa %xmm2,208(%r10) movdqa %xmm4,%xmm2 @@ -613,17 +615,17 @@ mul4x_internal: movdqa %xmm3,224(%r10) movdqa %xmm4,%xmm3 paddd %xmm1,%xmm2 - pcmpeqd %xmm5,%xmm1 + pcmpeqd %xmm5,%xmm1 // compare to 3,2 movdqa %xmm0,240(%r10) movdqa %xmm4,%xmm0 paddd %xmm2,%xmm3 - pcmpeqd %xmm5,%xmm2 + pcmpeqd %xmm5,%xmm2 // compare to 5,4 movdqa %xmm1,256(%r10) movdqa %xmm4,%xmm1 paddd %xmm3,%xmm0 - pcmpeqd %xmm5,%xmm3 + pcmpeqd %xmm5,%xmm3 // compare to 7,6 movdqa %xmm2,272(%r10) movdqa %xmm4,%xmm2 @@ -642,7 +644,7 @@ mul4x_internal: pcmpeqd %xmm5,%xmm3 movdqa %xmm2,336(%r10) - pand 64(%r12),%xmm0 + pand 64(%r12),%xmm0 // while it's still in register pand 80(%r12),%xmm1 pand 96(%r12),%xmm2 @@ -687,31 +689,31 @@ mul4x_internal: por %xmm2,%xmm0 por %xmm3,%xmm1 por %xmm1,%xmm0 - - pshufd $0x4e,%xmm0,%xmm1 +// Combine the upper and lower halves of %xmm0. + pshufd $0x4e,%xmm0,%xmm1 // Swap upper and lower halves. por %xmm1,%xmm0 leaq 256(%r12),%r12 - movq %xmm0,%rbx + movq %xmm0,%rbx // m0=bp[0] - movq %r13,16+8(%rsp) - movq %rdi,56+8(%rsp) + movq %r13,16+8(%rsp) // save end of b[num] + movq %rdi,56+8(%rsp) // save %rdi - movq (%r8),%r8 + movq (%r8),%r8 // pull n0[0] value movq (%rsi),%rax - leaq (%rsi,%r9,1),%rsi + leaq (%rsi,%r9,1),%rsi // end of a[num] negq %r9 movq %r8,%rbp - mulq %rbx + mulq %rbx // ap[0]*bp[0] movq %rax,%r10 movq (%rcx),%rax - imulq %r10,%rbp + imulq %r10,%rbp // "tp[0]"*n0 leaq 64+8(%rsp),%r14 movq %rdx,%r11 - mulq %rbp - addq %rax,%r10 + mulq %rbp // np[0]*m1 + addq %rax,%r10 // discarded movq 8(%rsi,%r9,1),%rax adcq $0,%rdx movq %rdx,%rdi @@ -727,7 +729,7 @@ mul4x_internal: movq 16(%rsi,%r9,1),%rax adcq $0,%rdx addq %r11,%rdi - leaq 32(%r9),%r15 + leaq 32(%r9),%r15 // j=4 leaq 32(%rcx),%rcx adcq $0,%rdx movq %rdi,(%r14) @@ -736,103 +738,103 @@ mul4x_internal: .p2align 5 L$1st4x: - mulq %rbx + mulq %rbx // ap[j]*bp[0] addq %rax,%r10 movq -16(%rcx),%rax leaq 32(%r14),%r14 adcq $0,%rdx movq %rdx,%r11 - mulq %rbp + mulq %rbp // np[j]*m1 addq %rax,%r13 movq -8(%rsi,%r15,1),%rax adcq $0,%rdx - addq %r10,%r13 + addq %r10,%r13 // np[j]*m1+ap[j]*bp[0] adcq $0,%rdx - movq %r13,-24(%r14) + movq %r13,-24(%r14) // tp[j-1] movq %rdx,%rdi - mulq %rbx + mulq %rbx // ap[j]*bp[0] addq %rax,%r11 movq -8(%rcx),%rax adcq $0,%rdx movq %rdx,%r10 - mulq %rbp + mulq %rbp // np[j]*m1 addq %rax,%rdi movq (%rsi,%r15,1),%rax adcq $0,%rdx - addq %r11,%rdi + addq %r11,%rdi // np[j]*m1+ap[j]*bp[0] adcq $0,%rdx - movq %rdi,-16(%r14) + movq %rdi,-16(%r14) // tp[j-1] movq %rdx,%r13 - mulq %rbx + mulq %rbx // ap[j]*bp[0] addq %rax,%r10 movq 0(%rcx),%rax adcq $0,%rdx movq %rdx,%r11 - mulq %rbp + mulq %rbp // np[j]*m1 addq %rax,%r13 movq 8(%rsi,%r15,1),%rax adcq $0,%rdx - addq %r10,%r13 + addq %r10,%r13 // np[j]*m1+ap[j]*bp[0] adcq $0,%rdx - movq %r13,-8(%r14) + movq %r13,-8(%r14) // tp[j-1] movq %rdx,%rdi - mulq %rbx + mulq %rbx // ap[j]*bp[0] addq %rax,%r11 movq 8(%rcx),%rax adcq $0,%rdx movq %rdx,%r10 - mulq %rbp + mulq %rbp // np[j]*m1 addq %rax,%rdi movq 16(%rsi,%r15,1),%rax adcq $0,%rdx - addq %r11,%rdi + addq %r11,%rdi // np[j]*m1+ap[j]*bp[0] leaq 32(%rcx),%rcx adcq $0,%rdx - movq %rdi,(%r14) + movq %rdi,(%r14) // tp[j-1] movq %rdx,%r13 - addq $32,%r15 + addq $32,%r15 // j+=4 jnz L$1st4x - mulq %rbx + mulq %rbx // ap[j]*bp[0] addq %rax,%r10 movq -16(%rcx),%rax leaq 32(%r14),%r14 adcq $0,%rdx movq %rdx,%r11 - mulq %rbp + mulq %rbp // np[j]*m1 addq %rax,%r13 movq -8(%rsi),%rax adcq $0,%rdx - addq %r10,%r13 + addq %r10,%r13 // np[j]*m1+ap[j]*bp[0] adcq $0,%rdx - movq %r13,-24(%r14) + movq %r13,-24(%r14) // tp[j-1] movq %rdx,%rdi - mulq %rbx + mulq %rbx // ap[j]*bp[0] addq %rax,%r11 movq -8(%rcx),%rax adcq $0,%rdx movq %rdx,%r10 - mulq %rbp + mulq %rbp // np[j]*m1 addq %rax,%rdi - movq (%rsi,%r9,1),%rax + movq (%rsi,%r9,1),%rax // ap[0] adcq $0,%rdx - addq %r11,%rdi + addq %r11,%rdi // np[j]*m1+ap[j]*bp[0] adcq $0,%rdx - movq %rdi,-16(%r14) + movq %rdi,-16(%r14) // tp[j-1] movq %rdx,%r13 - leaq (%rcx,%r9,1),%rcx + leaq (%rcx,%r9,1),%rcx // rewind %rcx xorq %rdi,%rdi addq %r10,%r13 @@ -843,7 +845,7 @@ L$1st4x: .p2align 5 L$outer4x: - leaq 16+128(%r14),%rdx + leaq 16+128(%r14),%rdx // where 256-byte mask is (+size optimization) pxor %xmm4,%xmm4 pxor %xmm5,%xmm5 movdqa -128(%r12),%xmm0 @@ -895,45 +897,45 @@ L$outer4x: por %xmm2,%xmm4 por %xmm3,%xmm5 por %xmm5,%xmm4 - - pshufd $0x4e,%xmm4,%xmm0 +// Combine the upper and lower halves of %xmm4 as %xmm0. + pshufd $0x4e,%xmm4,%xmm0 // Swap upper and lower halves. por %xmm4,%xmm0 leaq 256(%r12),%r12 - movq %xmm0,%rbx + movq %xmm0,%rbx // m0=bp[i] movq (%r14,%r9,1),%r10 movq %r8,%rbp - mulq %rbx - addq %rax,%r10 + mulq %rbx // ap[0]*bp[i] + addq %rax,%r10 // ap[0]*bp[i]+tp[0] movq (%rcx),%rax adcq $0,%rdx - imulq %r10,%rbp + imulq %r10,%rbp // tp[0]*n0 movq %rdx,%r11 - movq %rdi,(%r14) + movq %rdi,(%r14) // store upmost overflow bit - leaq (%r14,%r9,1),%r14 + leaq (%r14,%r9,1),%r14 // rewind %r14 - mulq %rbp - addq %rax,%r10 + mulq %rbp // np[0]*m1 + addq %rax,%r10 // "%r13", discarded movq 8(%rsi,%r9,1),%rax adcq $0,%rdx movq %rdx,%rdi - mulq %rbx + mulq %rbx // ap[j]*bp[i] addq %rax,%r11 movq 8(%rcx),%rax adcq $0,%rdx - addq 8(%r14),%r11 + addq 8(%r14),%r11 // +tp[1] adcq $0,%rdx movq %rdx,%r10 - mulq %rbp + mulq %rbp // np[j]*m1 addq %rax,%rdi movq 16(%rsi,%r9,1),%rax adcq $0,%rdx - addq %r11,%rdi - leaq 32(%r9),%r15 + addq %r11,%rdi // np[j]*m1+ap[j]*bp[i]+tp[j] + leaq 32(%r9),%r15 // j=4 leaq 32(%rcx),%rcx adcq $0,%rdx movq %rdx,%r13 @@ -941,25 +943,25 @@ L$outer4x: .p2align 5 L$inner4x: - mulq %rbx + mulq %rbx // ap[j]*bp[i] addq %rax,%r10 movq -16(%rcx),%rax adcq $0,%rdx - addq 16(%r14),%r10 + addq 16(%r14),%r10 // ap[j]*bp[i]+tp[j] leaq 32(%r14),%r14 adcq $0,%rdx movq %rdx,%r11 - mulq %rbp + mulq %rbp // np[j]*m1 addq %rax,%r13 movq -8(%rsi,%r15,1),%rax adcq $0,%rdx addq %r10,%r13 adcq $0,%rdx - movq %rdi,-32(%r14) + movq %rdi,-32(%r14) // tp[j-1] movq %rdx,%rdi - mulq %rbx + mulq %rbx // ap[j]*bp[i] addq %rax,%r11 movq -8(%rcx),%rax adcq $0,%rdx @@ -967,33 +969,33 @@ L$inner4x: adcq $0,%rdx movq %rdx,%r10 - mulq %rbp + mulq %rbp // np[j]*m1 addq %rax,%rdi movq (%rsi,%r15,1),%rax adcq $0,%rdx addq %r11,%rdi adcq $0,%rdx - movq %r13,-24(%r14) + movq %r13,-24(%r14) // tp[j-1] movq %rdx,%r13 - mulq %rbx + mulq %rbx // ap[j]*bp[i] addq %rax,%r10 movq 0(%rcx),%rax adcq $0,%rdx - addq (%r14),%r10 + addq (%r14),%r10 // ap[j]*bp[i]+tp[j] adcq $0,%rdx movq %rdx,%r11 - mulq %rbp + mulq %rbp // np[j]*m1 addq %rax,%r13 movq 8(%rsi,%r15,1),%rax adcq $0,%rdx addq %r10,%r13 adcq $0,%rdx - movq %rdi,-16(%r14) + movq %rdi,-16(%r14) // tp[j-1] movq %rdx,%rdi - mulq %rbx + mulq %rbx // ap[j]*bp[i] addq %rax,%r11 movq 8(%rcx),%rax adcq $0,%rdx @@ -1001,38 +1003,38 @@ L$inner4x: adcq $0,%rdx movq %rdx,%r10 - mulq %rbp + mulq %rbp // np[j]*m1 addq %rax,%rdi movq 16(%rsi,%r15,1),%rax adcq $0,%rdx addq %r11,%rdi leaq 32(%rcx),%rcx adcq $0,%rdx - movq %r13,-8(%r14) + movq %r13,-8(%r14) // tp[j-1] movq %rdx,%r13 - addq $32,%r15 + addq $32,%r15 // j+=4 jnz L$inner4x - mulq %rbx + mulq %rbx // ap[j]*bp[i] addq %rax,%r10 movq -16(%rcx),%rax adcq $0,%rdx - addq 16(%r14),%r10 + addq 16(%r14),%r10 // ap[j]*bp[i]+tp[j] leaq 32(%r14),%r14 adcq $0,%rdx movq %rdx,%r11 - mulq %rbp + mulq %rbp // np[j]*m1 addq %rax,%r13 movq -8(%rsi),%rax adcq $0,%rdx addq %r10,%r13 adcq $0,%rdx - movq %rdi,-32(%r14) + movq %rdi,-32(%r14) // tp[j-1] movq %rdx,%rdi - mulq %rbx + mulq %rbx // ap[j]*bp[i] addq %rax,%r11 movq %rbp,%rax movq -8(%rcx),%rbp @@ -1041,39 +1043,39 @@ L$inner4x: adcq $0,%rdx movq %rdx,%r10 - mulq %rbp + mulq %rbp // np[j]*m1 addq %rax,%rdi - movq (%rsi,%r9,1),%rax + movq (%rsi,%r9,1),%rax // ap[0] adcq $0,%rdx addq %r11,%rdi adcq $0,%rdx - movq %r13,-24(%r14) + movq %r13,-24(%r14) // tp[j-1] movq %rdx,%r13 - movq %rdi,-16(%r14) - leaq (%rcx,%r9,1),%rcx + movq %rdi,-16(%r14) // tp[j-1] + leaq (%rcx,%r9,1),%rcx // rewind %rcx xorq %rdi,%rdi addq %r10,%r13 adcq $0,%rdi - addq (%r14),%r13 - adcq $0,%rdi + addq (%r14),%r13 // pull upmost overflow bit + adcq $0,%rdi // upmost overflow bit movq %r13,-8(%r14) cmpq 16+8(%rsp),%r12 jb L$outer4x xorq %rax,%rax - subq %r13,%rbp - adcq %r15,%r15 + subq %r13,%rbp // compare top-most words + adcq %r15,%r15 // %r15 is zero orq %r15,%rdi - subq %rdi,%rax - leaq (%r14,%r9,1),%rbx + subq %rdi,%rax // %rax=-%rdi + leaq (%r14,%r9,1),%rbx // tptr in .sqr4x_sub movq (%rcx),%r12 - leaq (%rcx),%rbp + leaq (%rcx),%rbp // nptr in .sqr4x_sub movq %r9,%rcx sarq $3+2,%rcx - movq 56+8(%rsp),%rdi - decq %r12 + movq 56+8(%rsp),%rdi // rptr in .sqr4x_sub + decq %r12 // so that after 'not' we get -n[0] xorq %r10,%r10 movq 8(%rbp),%r13 movq 16(%rbp),%r14 @@ -1085,6 +1087,7 @@ L$inner4x: .private_extern _bn_power5_nohw .p2align 5 +.alt_entry _bn_power5_nohw _bn_power5_nohw: _CET_ENDBR @@ -1104,35 +1107,35 @@ _CET_ENDBR L$power5_prologue: - - - - shll $3,%r9d - leal (%r9,%r9,2),%r10d +// num is declared as an int, a 32-bit parameter, so the upper half is +// undefined. It is important that this write to %r9, which zeros the +// upper half, come before the first access. + shll $3,%r9d // convert %r9 to bytes + leal (%r9,%r9,2),%r10d // 3*%r9 negq %r9 - movq (%r8),%r8 - - - - - - - - + movq (%r8),%r8 // *n0 + +// ############################################################# +// Ensure that stack frame doesn't alias with %rdi+3*%r9 +// modulo 4096, which covers ret[num], am[num] and n[num] +// (see bn_exp.c). This is done to allow memory disambiguation +// logic do its magic. [Extra 256 bytes is for power mask +// calculated from 7th argument, the index.] +// leaq -320(%rsp,%r9,2),%r11 movq %rsp,%rbp subq %rdi,%r11 andq $4095,%r11 cmpq %r11,%r10 jb L$pwr_sp_alt - subq %r11,%rbp - leaq -320(%rbp,%r9,2),%rbp + subq %r11,%rbp // align with %rsi + leaq -320(%rbp,%r9,2),%rbp // future alloca(frame+2*num*8+256) jmp L$pwr_sp_done .p2align 5 L$pwr_sp_alt: leaq 4096-320(,%r9,2),%r10 - leaq -320(%rbp,%r9,2),%rbp + leaq -320(%rbp,%r9,2),%rbp // future alloca(frame+2*num*8+256) subq %r10,%r11 movq $0,%r10 cmovcq %r10,%r11 @@ -1158,22 +1161,22 @@ L$pwr_page_walk_done: movq %r9,%r10 negq %r9 - - - - - - - - - +// ############################################################# +// Stack layout +// +// +0 saved %r9, used in reduction section +// +8 &t[2*%r9], used in reduction section +// +32 saved *n0 +// +40 saved %rsp +// +48 t[2*%r9] +// movq %r8,32(%rsp) - movq %rax,40(%rsp) + movq %rax,40(%rsp) // save original %rsp L$power5_body: - movq %rdi,%xmm1 - movq %rcx,%xmm2 - movq %r10,%xmm3 + movq %rdi,%xmm1 // save %rdi, used in sqr8x + movq %rcx,%xmm2 // save %rcx + movq %r10,%xmm3 // -%r9, used in sqr8x movq %rdx,%xmm4 call __bn_sqr8x_internal @@ -1195,7 +1198,7 @@ L$power5_body: call mul4x_internal - movq 40(%rsp),%rsi + movq 40(%rsp),%rsi // restore %rsp movq $1,%rax movq -48(%rsi),%r15 @@ -1222,265 +1225,267 @@ L$power5_epilogue: .private_extern _bn_sqr8x_internal .p2align 5 +.alt_entry _bn_sqr8x_internal _bn_sqr8x_internal: +.alt_entry __bn_sqr8x_internal __bn_sqr8x_internal: _CET_ENDBR - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - leaq 32(%r10),%rbp - leaq (%rsi,%r9,1),%rsi - - movq %r9,%rcx - - - movq -32(%rsi,%rbp,1),%r14 - leaq 48+8(%rsp,%r9,2),%rdi - movq -24(%rsi,%rbp,1),%rax - leaq -32(%rdi,%rbp,1),%rdi - movq -16(%rsi,%rbp,1),%rbx +// ############################################################# +// Squaring part: +// +// a) multiply-n-add everything but a[i]*a[i]; +// b) shift result of a) by 1 to the left and accumulate +// a[i]*a[i] products; +// +// ############################################################# +// a[1]a[0] +// a[2]a[0] +// a[3]a[0] +// a[2]a[1] +// a[4]a[0] +// a[3]a[1] +// a[5]a[0] +// a[4]a[1] +// a[3]a[2] +// a[6]a[0] +// a[5]a[1] +// a[4]a[2] +// a[7]a[0] +// a[6]a[1] +// a[5]a[2] +// a[4]a[3] +// a[7]a[1] +// a[6]a[2] +// a[5]a[3] +// a[7]a[2] +// a[6]a[3] +// a[5]a[4] +// a[7]a[3] +// a[6]a[4] +// a[7]a[4] +// a[6]a[5] +// a[7]a[5] +// a[7]a[6] +// a[1]a[0] +// a[2]a[0] +// a[3]a[0] +// a[4]a[0] +// a[5]a[0] +// a[6]a[0] +// a[7]a[0] +// a[2]a[1] +// a[3]a[1] +// a[4]a[1] +// a[5]a[1] +// a[6]a[1] +// a[7]a[1] +// a[3]a[2] +// a[4]a[2] +// a[5]a[2] +// a[6]a[2] +// a[7]a[2] +// a[4]a[3] +// a[5]a[3] +// a[6]a[3] +// a[7]a[3] +// a[5]a[4] +// a[6]a[4] +// a[7]a[4] +// a[6]a[5] +// a[7]a[5] +// a[7]a[6] +// a[0]a[0] +// a[1]a[1] +// a[2]a[2] +// a[3]a[3] +// a[4]a[4] +// a[5]a[5] +// a[6]a[6] +// a[7]a[7] + + leaq 32(%r10),%rbp // %rbp=-(%r9-32) + leaq (%rsi,%r9,1),%rsi // end of a[] buffer, (%rsi,%rbp)=&ap[2] + + movq %r9,%rcx // %rcx=%r9 + +// comments apply to %r9==8 case + movq -32(%rsi,%rbp,1),%r14 // a[0] + leaq 48+8(%rsp,%r9,2),%rdi // end of tp[] buffer, &tp[2*%r9] + movq -24(%rsi,%rbp,1),%rax // a[1] + leaq -32(%rdi,%rbp,1),%rdi // end of tp[] window, &tp[2*%r9-"%rbp"] + movq -16(%rsi,%rbp,1),%rbx // a[2] movq %rax,%r15 - mulq %r14 - movq %rax,%r10 - movq %rbx,%rax + mulq %r14 // a[1]*a[0] + movq %rax,%r10 // a[1]*a[0] + movq %rbx,%rax // a[2] movq %rdx,%r11 - movq %r10,-24(%rdi,%rbp,1) + movq %r10,-24(%rdi,%rbp,1) // t[1] - mulq %r14 + mulq %r14 // a[2]*a[0] addq %rax,%r11 movq %rbx,%rax adcq $0,%rdx - movq %r11,-16(%rdi,%rbp,1) + movq %r11,-16(%rdi,%rbp,1) // t[2] movq %rdx,%r10 - movq -8(%rsi,%rbp,1),%rbx - mulq %r15 - movq %rax,%r12 + movq -8(%rsi,%rbp,1),%rbx // a[3] + mulq %r15 // a[2]*a[1] + movq %rax,%r12 // a[2]*a[1]+t[3] movq %rbx,%rax movq %rdx,%r13 leaq (%rbp),%rcx - mulq %r14 - addq %rax,%r10 + mulq %r14 // a[3]*a[0] + addq %rax,%r10 // a[3]*a[0]+a[2]*a[1]+t[3] movq %rbx,%rax movq %rdx,%r11 adcq $0,%r11 addq %r12,%r10 adcq $0,%r11 - movq %r10,-8(%rdi,%rcx,1) + movq %r10,-8(%rdi,%rcx,1) // t[3] jmp L$sqr4x_1st .p2align 5 L$sqr4x_1st: - movq (%rsi,%rcx,1),%rbx - mulq %r15 - addq %rax,%r13 + movq (%rsi,%rcx,1),%rbx // a[4] + mulq %r15 // a[3]*a[1] + addq %rax,%r13 // a[3]*a[1]+t[4] movq %rbx,%rax movq %rdx,%r12 adcq $0,%r12 - mulq %r14 - addq %rax,%r11 - movq %rbx,%rax - movq 8(%rsi,%rcx,1),%rbx + mulq %r14 // a[4]*a[0] + addq %rax,%r11 // a[4]*a[0]+a[3]*a[1]+t[4] + movq %rbx,%rax // a[3] + movq 8(%rsi,%rcx,1),%rbx // a[5] movq %rdx,%r10 adcq $0,%r10 addq %r13,%r11 adcq $0,%r10 - mulq %r15 - addq %rax,%r12 + mulq %r15 // a[4]*a[3] + addq %rax,%r12 // a[4]*a[3]+t[5] movq %rbx,%rax - movq %r11,(%rdi,%rcx,1) + movq %r11,(%rdi,%rcx,1) // t[4] movq %rdx,%r13 adcq $0,%r13 - mulq %r14 - addq %rax,%r10 + mulq %r14 // a[5]*a[2] + addq %rax,%r10 // a[5]*a[2]+a[4]*a[3]+t[5] movq %rbx,%rax - movq 16(%rsi,%rcx,1),%rbx + movq 16(%rsi,%rcx,1),%rbx // a[6] movq %rdx,%r11 adcq $0,%r11 addq %r12,%r10 adcq $0,%r11 - mulq %r15 - addq %rax,%r13 + mulq %r15 // a[5]*a[3] + addq %rax,%r13 // a[5]*a[3]+t[6] movq %rbx,%rax - movq %r10,8(%rdi,%rcx,1) + movq %r10,8(%rdi,%rcx,1) // t[5] movq %rdx,%r12 adcq $0,%r12 - mulq %r14 - addq %rax,%r11 - movq %rbx,%rax - movq 24(%rsi,%rcx,1),%rbx + mulq %r14 // a[6]*a[2] + addq %rax,%r11 // a[6]*a[2]+a[5]*a[3]+t[6] + movq %rbx,%rax // a[3] + movq 24(%rsi,%rcx,1),%rbx // a[7] movq %rdx,%r10 adcq $0,%r10 addq %r13,%r11 adcq $0,%r10 - mulq %r15 - addq %rax,%r12 + mulq %r15 // a[6]*a[5] + addq %rax,%r12 // a[6]*a[5]+t[7] movq %rbx,%rax - movq %r11,16(%rdi,%rcx,1) + movq %r11,16(%rdi,%rcx,1) // t[6] movq %rdx,%r13 adcq $0,%r13 leaq 32(%rcx),%rcx - mulq %r14 - addq %rax,%r10 + mulq %r14 // a[7]*a[4] + addq %rax,%r10 // a[7]*a[4]+a[6]*a[5]+t[6] movq %rbx,%rax movq %rdx,%r11 adcq $0,%r11 addq %r12,%r10 adcq $0,%r11 - movq %r10,-8(%rdi,%rcx,1) + movq %r10,-8(%rdi,%rcx,1) // t[7] cmpq $0,%rcx jne L$sqr4x_1st - mulq %r15 + mulq %r15 // a[7]*a[5] addq %rax,%r13 leaq 16(%rbp),%rbp adcq $0,%rdx addq %r11,%r13 adcq $0,%rdx - movq %r13,(%rdi) + movq %r13,(%rdi) // t[8] movq %rdx,%r12 - movq %rdx,8(%rdi) + movq %rdx,8(%rdi) // t[9] jmp L$sqr4x_outer .p2align 5 -L$sqr4x_outer: - movq -32(%rsi,%rbp,1),%r14 - leaq 48+8(%rsp,%r9,2),%rdi - movq -24(%rsi,%rbp,1),%rax - leaq -32(%rdi,%rbp,1),%rdi - movq -16(%rsi,%rbp,1),%rbx +L$sqr4x_outer: // comments apply to %r9==6 case + movq -32(%rsi,%rbp,1),%r14 // a[0] + leaq 48+8(%rsp,%r9,2),%rdi // end of tp[] buffer, &tp[2*%r9] + movq -24(%rsi,%rbp,1),%rax // a[1] + leaq -32(%rdi,%rbp,1),%rdi // end of tp[] window, &tp[2*%r9-"%rbp"] + movq -16(%rsi,%rbp,1),%rbx // a[2] movq %rax,%r15 - mulq %r14 - movq -24(%rdi,%rbp,1),%r10 - addq %rax,%r10 - movq %rbx,%rax + mulq %r14 // a[1]*a[0] + movq -24(%rdi,%rbp,1),%r10 // t[1] + addq %rax,%r10 // a[1]*a[0]+t[1] + movq %rbx,%rax // a[2] adcq $0,%rdx - movq %r10,-24(%rdi,%rbp,1) + movq %r10,-24(%rdi,%rbp,1) // t[1] movq %rdx,%r11 - mulq %r14 + mulq %r14 // a[2]*a[0] addq %rax,%r11 movq %rbx,%rax adcq $0,%rdx - addq -16(%rdi,%rbp,1),%r11 + addq -16(%rdi,%rbp,1),%r11 // a[2]*a[0]+t[2] movq %rdx,%r10 adcq $0,%r10 - movq %r11,-16(%rdi,%rbp,1) + movq %r11,-16(%rdi,%rbp,1) // t[2] xorq %r12,%r12 - movq -8(%rsi,%rbp,1),%rbx - mulq %r15 - addq %rax,%r12 + movq -8(%rsi,%rbp,1),%rbx // a[3] + mulq %r15 // a[2]*a[1] + addq %rax,%r12 // a[2]*a[1]+t[3] movq %rbx,%rax adcq $0,%rdx addq -8(%rdi,%rbp,1),%r12 movq %rdx,%r13 adcq $0,%r13 - mulq %r14 - addq %rax,%r10 + mulq %r14 // a[3]*a[0] + addq %rax,%r10 // a[3]*a[0]+a[2]*a[1]+t[3] movq %rbx,%rax adcq $0,%rdx addq %r12,%r10 movq %rdx,%r11 adcq $0,%r11 - movq %r10,-8(%rdi,%rbp,1) + movq %r10,-8(%rdi,%rbp,1) // t[3] leaq (%rbp),%rcx jmp L$sqr4x_inner .p2align 5 L$sqr4x_inner: - movq (%rsi,%rcx,1),%rbx - mulq %r15 - addq %rax,%r13 + movq (%rsi,%rcx,1),%rbx // a[4] + mulq %r15 // a[3]*a[1] + addq %rax,%r13 // a[3]*a[1]+t[4] movq %rbx,%rax movq %rdx,%r12 adcq $0,%r12 @@ -1488,272 +1493,273 @@ L$sqr4x_inner: adcq $0,%r12 .byte 0x67 - mulq %r14 - addq %rax,%r11 - movq %rbx,%rax - movq 8(%rsi,%rcx,1),%rbx + mulq %r14 // a[4]*a[0] + addq %rax,%r11 // a[4]*a[0]+a[3]*a[1]+t[4] + movq %rbx,%rax // a[3] + movq 8(%rsi,%rcx,1),%rbx // a[5] movq %rdx,%r10 adcq $0,%r10 addq %r13,%r11 adcq $0,%r10 - mulq %r15 - addq %rax,%r12 - movq %r11,(%rdi,%rcx,1) + mulq %r15 // a[4]*a[3] + addq %rax,%r12 // a[4]*a[3]+t[5] + movq %r11,(%rdi,%rcx,1) // t[4] movq %rbx,%rax movq %rdx,%r13 adcq $0,%r13 addq 8(%rdi,%rcx,1),%r12 - leaq 16(%rcx),%rcx + leaq 16(%rcx),%rcx // j++ adcq $0,%r13 - mulq %r14 - addq %rax,%r10 + mulq %r14 // a[5]*a[2] + addq %rax,%r10 // a[5]*a[2]+a[4]*a[3]+t[5] movq %rbx,%rax adcq $0,%rdx addq %r12,%r10 movq %rdx,%r11 adcq $0,%r11 - movq %r10,-8(%rdi,%rcx,1) + movq %r10,-8(%rdi,%rcx,1) // t[5], "preloaded t[1]" below cmpq $0,%rcx jne L$sqr4x_inner .byte 0x67 - mulq %r15 + mulq %r15 // a[5]*a[3] addq %rax,%r13 adcq $0,%rdx addq %r11,%r13 adcq $0,%rdx - movq %r13,(%rdi) + movq %r13,(%rdi) // t[6], "preloaded t[2]" below movq %rdx,%r12 - movq %rdx,8(%rdi) + movq %rdx,8(%rdi) // t[7], "preloaded t[3]" below addq $16,%rbp jnz L$sqr4x_outer - - movq -32(%rsi),%r14 - leaq 48+8(%rsp,%r9,2),%rdi - movq -24(%rsi),%rax - leaq -32(%rdi,%rbp,1),%rdi - movq -16(%rsi),%rbx +// comments apply to %r9==4 case + movq -32(%rsi),%r14 // a[0] + leaq 48+8(%rsp,%r9,2),%rdi // end of tp[] buffer, &tp[2*%r9] + movq -24(%rsi),%rax // a[1] + leaq -32(%rdi,%rbp,1),%rdi // end of tp[] window, &tp[2*%r9-"%rbp"] + movq -16(%rsi),%rbx // a[2] movq %rax,%r15 - mulq %r14 - addq %rax,%r10 - movq %rbx,%rax + mulq %r14 // a[1]*a[0] + addq %rax,%r10 // a[1]*a[0]+t[1], preloaded t[1] + movq %rbx,%rax // a[2] movq %rdx,%r11 adcq $0,%r11 - mulq %r14 + mulq %r14 // a[2]*a[0] addq %rax,%r11 movq %rbx,%rax - movq %r10,-24(%rdi) + movq %r10,-24(%rdi) // t[1] movq %rdx,%r10 adcq $0,%r10 - addq %r13,%r11 - movq -8(%rsi),%rbx + addq %r13,%r11 // a[2]*a[0]+t[2], preloaded t[2] + movq -8(%rsi),%rbx // a[3] adcq $0,%r10 - mulq %r15 - addq %rax,%r12 + mulq %r15 // a[2]*a[1] + addq %rax,%r12 // a[2]*a[1]+t[3], preloaded t[3] movq %rbx,%rax - movq %r11,-16(%rdi) + movq %r11,-16(%rdi) // t[2] movq %rdx,%r13 adcq $0,%r13 - mulq %r14 - addq %rax,%r10 + mulq %r14 // a[3]*a[0] + addq %rax,%r10 // a[3]*a[0]+a[2]*a[1]+t[3] movq %rbx,%rax movq %rdx,%r11 adcq $0,%r11 addq %r12,%r10 adcq $0,%r11 - movq %r10,-8(%rdi) + movq %r10,-8(%rdi) // t[3] - mulq %r15 + mulq %r15 // a[3]*a[1] addq %rax,%r13 - movq -16(%rsi),%rax + movq -16(%rsi),%rax // a[2] adcq $0,%rdx addq %r11,%r13 adcq $0,%rdx - movq %r13,(%rdi) + movq %r13,(%rdi) // t[4] movq %rdx,%r12 - movq %rdx,8(%rdi) + movq %rdx,8(%rdi) // t[5] - mulq %rbx + mulq %rbx // a[2]*a[3] addq $16,%rbp xorq %r14,%r14 - subq %r9,%rbp + subq %r9,%rbp // %rbp=16-%r9 xorq %r15,%r15 - addq %r12,%rax + addq %r12,%rax // t[5] adcq $0,%rdx - movq %rax,8(%rdi) - movq %rdx,16(%rdi) - movq %r15,24(%rdi) + movq %rax,8(%rdi) // t[5] + movq %rdx,16(%rdi) // t[6] + movq %r15,24(%rdi) // t[7] - movq -16(%rsi,%rbp,1),%rax + movq -16(%rsi,%rbp,1),%rax // a[0] leaq 48+8(%rsp),%rdi - xorq %r10,%r10 - movq 8(%rdi),%r11 + xorq %r10,%r10 // t[0] + movq 8(%rdi),%r11 // t[1] - leaq (%r14,%r10,2),%r12 + leaq (%r14,%r10,2),%r12 // t[2*i]<<1 | shift shrq $63,%r10 - leaq (%rcx,%r11,2),%r13 + leaq (%rcx,%r11,2),%r13 // t[2*i+1]<<1 | shrq $63,%r11 - orq %r10,%r13 - movq 16(%rdi),%r10 - movq %r11,%r14 - mulq %rax - negq %r15 - movq 24(%rdi),%r11 + orq %r10,%r13 // | t[2*i]>>63 + movq 16(%rdi),%r10 // t[2*i+2] # prefetch + movq %r11,%r14 // shift=t[2*i+1]>>63 + mulq %rax // a[i]*a[i] + negq %r15 // mov %r15,cf + movq 24(%rdi),%r11 // t[2*i+2+1] # prefetch adcq %rax,%r12 - movq -8(%rsi,%rbp,1),%rax + movq -8(%rsi,%rbp,1),%rax // a[i+1] # prefetch movq %r12,(%rdi) adcq %rdx,%r13 - leaq (%r14,%r10,2),%rbx + leaq (%r14,%r10,2),%rbx // t[2*i]<<1 | shift movq %r13,8(%rdi) - sbbq %r15,%r15 + sbbq %r15,%r15 // mov cf,%r15 shrq $63,%r10 - leaq (%rcx,%r11,2),%r8 + leaq (%rcx,%r11,2),%r8 // t[2*i+1]<<1 | shrq $63,%r11 - orq %r10,%r8 - movq 32(%rdi),%r10 - movq %r11,%r14 - mulq %rax - negq %r15 - movq 40(%rdi),%r11 + orq %r10,%r8 // | t[2*i]>>63 + movq 32(%rdi),%r10 // t[2*i+2] # prefetch + movq %r11,%r14 // shift=t[2*i+1]>>63 + mulq %rax // a[i]*a[i] + negq %r15 // mov %r15,cf + movq 40(%rdi),%r11 // t[2*i+2+1] # prefetch adcq %rax,%rbx - movq 0(%rsi,%rbp,1),%rax + movq 0(%rsi,%rbp,1),%rax // a[i+1] # prefetch movq %rbx,16(%rdi) adcq %rdx,%r8 leaq 16(%rbp),%rbp movq %r8,24(%rdi) - sbbq %r15,%r15 + sbbq %r15,%r15 // mov cf,%r15 leaq 64(%rdi),%rdi jmp L$sqr4x_shift_n_add .p2align 5 L$sqr4x_shift_n_add: - leaq (%r14,%r10,2),%r12 + leaq (%r14,%r10,2),%r12 // t[2*i]<<1 | shift shrq $63,%r10 - leaq (%rcx,%r11,2),%r13 + leaq (%rcx,%r11,2),%r13 // t[2*i+1]<<1 | shrq $63,%r11 - orq %r10,%r13 - movq -16(%rdi),%r10 - movq %r11,%r14 - mulq %rax - negq %r15 - movq -8(%rdi),%r11 + orq %r10,%r13 // | t[2*i]>>63 + movq -16(%rdi),%r10 // t[2*i+2] # prefetch + movq %r11,%r14 // shift=t[2*i+1]>>63 + mulq %rax // a[i]*a[i] + negq %r15 // mov %r15,cf + movq -8(%rdi),%r11 // t[2*i+2+1] # prefetch adcq %rax,%r12 - movq -8(%rsi,%rbp,1),%rax + movq -8(%rsi,%rbp,1),%rax // a[i+1] # prefetch movq %r12,-32(%rdi) adcq %rdx,%r13 - leaq (%r14,%r10,2),%rbx + leaq (%r14,%r10,2),%rbx // t[2*i]<<1 | shift movq %r13,-24(%rdi) - sbbq %r15,%r15 + sbbq %r15,%r15 // mov cf,%r15 shrq $63,%r10 - leaq (%rcx,%r11,2),%r8 + leaq (%rcx,%r11,2),%r8 // t[2*i+1]<<1 | shrq $63,%r11 - orq %r10,%r8 - movq 0(%rdi),%r10 - movq %r11,%r14 - mulq %rax - negq %r15 - movq 8(%rdi),%r11 + orq %r10,%r8 // | t[2*i]>>63 + movq 0(%rdi),%r10 // t[2*i+2] # prefetch + movq %r11,%r14 // shift=t[2*i+1]>>63 + mulq %rax // a[i]*a[i] + negq %r15 // mov %r15,cf + movq 8(%rdi),%r11 // t[2*i+2+1] # prefetch adcq %rax,%rbx - movq 0(%rsi,%rbp,1),%rax + movq 0(%rsi,%rbp,1),%rax // a[i+1] # prefetch movq %rbx,-16(%rdi) adcq %rdx,%r8 - leaq (%r14,%r10,2),%r12 + leaq (%r14,%r10,2),%r12 // t[2*i]<<1 | shift movq %r8,-8(%rdi) - sbbq %r15,%r15 + sbbq %r15,%r15 // mov cf,%r15 shrq $63,%r10 - leaq (%rcx,%r11,2),%r13 + leaq (%rcx,%r11,2),%r13 // t[2*i+1]<<1 | shrq $63,%r11 - orq %r10,%r13 - movq 16(%rdi),%r10 - movq %r11,%r14 - mulq %rax - negq %r15 - movq 24(%rdi),%r11 + orq %r10,%r13 // | t[2*i]>>63 + movq 16(%rdi),%r10 // t[2*i+2] # prefetch + movq %r11,%r14 // shift=t[2*i+1]>>63 + mulq %rax // a[i]*a[i] + negq %r15 // mov %r15,cf + movq 24(%rdi),%r11 // t[2*i+2+1] # prefetch adcq %rax,%r12 - movq 8(%rsi,%rbp,1),%rax + movq 8(%rsi,%rbp,1),%rax // a[i+1] # prefetch movq %r12,0(%rdi) adcq %rdx,%r13 - leaq (%r14,%r10,2),%rbx + leaq (%r14,%r10,2),%rbx // t[2*i]<<1 | shift movq %r13,8(%rdi) - sbbq %r15,%r15 + sbbq %r15,%r15 // mov cf,%r15 shrq $63,%r10 - leaq (%rcx,%r11,2),%r8 + leaq (%rcx,%r11,2),%r8 // t[2*i+1]<<1 | shrq $63,%r11 - orq %r10,%r8 - movq 32(%rdi),%r10 - movq %r11,%r14 - mulq %rax - negq %r15 - movq 40(%rdi),%r11 + orq %r10,%r8 // | t[2*i]>>63 + movq 32(%rdi),%r10 // t[2*i+2] # prefetch + movq %r11,%r14 // shift=t[2*i+1]>>63 + mulq %rax // a[i]*a[i] + negq %r15 // mov %r15,cf + movq 40(%rdi),%r11 // t[2*i+2+1] # prefetch adcq %rax,%rbx - movq 16(%rsi,%rbp,1),%rax + movq 16(%rsi,%rbp,1),%rax // a[i+1] # prefetch movq %rbx,16(%rdi) adcq %rdx,%r8 movq %r8,24(%rdi) - sbbq %r15,%r15 + sbbq %r15,%r15 // mov cf,%r15 leaq 64(%rdi),%rdi addq $32,%rbp jnz L$sqr4x_shift_n_add - leaq (%r14,%r10,2),%r12 + leaq (%r14,%r10,2),%r12 // t[2*i]<<1 | shift .byte 0x67 shrq $63,%r10 - leaq (%rcx,%r11,2),%r13 + leaq (%rcx,%r11,2),%r13 // t[2*i+1]<<1 | shrq $63,%r11 - orq %r10,%r13 - movq -16(%rdi),%r10 - movq %r11,%r14 - mulq %rax - negq %r15 - movq -8(%rdi),%r11 + orq %r10,%r13 // | t[2*i]>>63 + movq -16(%rdi),%r10 // t[2*i+2] # prefetch + movq %r11,%r14 // shift=t[2*i+1]>>63 + mulq %rax // a[i]*a[i] + negq %r15 // mov %r15,cf + movq -8(%rdi),%r11 // t[2*i+2+1] # prefetch adcq %rax,%r12 - movq -8(%rsi),%rax + movq -8(%rsi),%rax // a[i+1] # prefetch movq %r12,-32(%rdi) adcq %rdx,%r13 - leaq (%r14,%r10,2),%rbx + leaq (%r14,%r10,2),%rbx // t[2*i]<<1|shift movq %r13,-24(%rdi) - sbbq %r15,%r15 + sbbq %r15,%r15 // mov cf,%r15 shrq $63,%r10 - leaq (%rcx,%r11,2),%r8 + leaq (%rcx,%r11,2),%r8 // t[2*i+1]<<1 | shrq $63,%r11 - orq %r10,%r8 - mulq %rax - negq %r15 + orq %r10,%r8 // | t[2*i]>>63 + mulq %rax // a[i]*a[i] + negq %r15 // mov %r15,cf adcq %rax,%rbx adcq %rdx,%r8 movq %rbx,-16(%rdi) movq %r8,-8(%rdi) movq %xmm2,%rbp +.alt_entry __bn_sqr8x_reduction __bn_sqr8x_reduction: xorq %rax,%rax - leaq (%r9,%rbp,1),%rcx - leaq 48+8(%rsp,%r9,2),%rdx + leaq (%r9,%rbp,1),%rcx // end of n[] + leaq 48+8(%rsp,%r9,2),%rdx // end of t[] buffer movq %rcx,0+8(%rsp) - leaq 48+8(%rsp,%r9,1),%rdi + leaq 48+8(%rsp,%r9,1),%rdi // end of initial t[] window movq %rdx,8+8(%rsp) negq %r9 jmp L$8x_reduction_loop .p2align 5 L$8x_reduction_loop: - leaq (%rdi,%r9,1),%rdi + leaq (%rdi,%r9,1),%rdi // start of current t[] window .byte 0x66 movq 0(%rdi),%rbx movq 8(%rdi),%r9 @@ -1763,20 +1769,20 @@ L$8x_reduction_loop: movq 40(%rdi),%r13 movq 48(%rdi),%r14 movq 56(%rdi),%r15 - movq %rax,(%rdx) + movq %rax,(%rdx) // store top-most carry bit leaq 64(%rdi),%rdi .byte 0x67 movq %rbx,%r8 - imulq 32+8(%rsp),%rbx - movq 0(%rbp),%rax + imulq 32+8(%rsp),%rbx // n0*a[0] + movq 0(%rbp),%rax // n[0] movl $8,%ecx jmp L$8x_reduce .p2align 5 L$8x_reduce: mulq %rbx - movq 8(%rbp),%rax + movq 8(%rbp),%rax // n[1] negq %r8 movq %rdx,%r8 adcq $0,%r8 @@ -1786,7 +1792,7 @@ L$8x_reduce: movq 16(%rbp),%rax adcq $0,%rdx addq %r9,%r8 - movq %rbx,48-8+8(%rsp,%rcx,8) + movq %rbx,48-8+8(%rsp,%rcx,8) // put aside n0*a[i] movq %rdx,%r9 adcq $0,%r9 @@ -1795,7 +1801,7 @@ L$8x_reduce: movq 24(%rbp),%rax adcq $0,%rdx addq %r10,%r9 - movq 32+8(%rsp),%rsi + movq 32+8(%rsp),%rsi // pull n0, borrow %rsi movq %rdx,%r10 adcq $0,%r10 @@ -1803,7 +1809,7 @@ L$8x_reduce: addq %rax,%r11 movq 32(%rbp),%rax adcq $0,%rdx - imulq %r8,%rsi + imulq %r8,%rsi // modulo-scheduled addq %r11,%r10 movq %rdx,%r11 adcq $0,%r11 @@ -1833,9 +1839,9 @@ L$8x_reduce: adcq $0,%r14 mulq %rbx - movq %rsi,%rbx + movq %rsi,%rbx // n0*a[i] addq %rax,%r15 - movq 0(%rbp),%rax + movq 0(%rbp),%rax // n[0] adcq $0,%rdx addq %r15,%r14 movq %rdx,%r15 @@ -1846,8 +1852,8 @@ L$8x_reduce: leaq 64(%rbp),%rbp xorq %rax,%rax - movq 8+8(%rsp),%rdx - cmpq 0+8(%rsp),%rbp + movq 8+8(%rsp),%rdx // pull end of t[] + cmpq 0+8(%rsp),%rbp // end of n[]? jae L$8x_no_tail .byte 0x66 @@ -1859,9 +1865,9 @@ L$8x_reduce: adcq 40(%rdi),%r13 adcq 48(%rdi),%r14 adcq 56(%rdi),%r15 - sbbq %rsi,%rsi + sbbq %rsi,%rsi // top carry - movq 48+56+8(%rsp),%rbx + movq 48+56+8(%rsp),%rbx // pull n0*a[0] movl $8,%ecx movq 0(%rbp),%rax jmp L$8x_tail @@ -1871,7 +1877,7 @@ L$8x_tail: mulq %rbx addq %rax,%r8 movq 8(%rbp),%rax - movq %r8,(%rdi) + movq %r8,(%rdi) // save result movq %rdx,%r8 adcq $0,%r8 @@ -1880,7 +1886,7 @@ L$8x_tail: movq 16(%rbp),%rax adcq $0,%rdx addq %r9,%r8 - leaq 8(%rdi),%rdi + leaq 8(%rdi),%rdi // %rdi++ movq %rdx,%r9 adcq $0,%r9 @@ -1925,11 +1931,11 @@ L$8x_tail: adcq $0,%r14 mulq %rbx - movq 48-16+8(%rsp,%rcx,8),%rbx + movq 48-16+8(%rsp,%rcx,8),%rbx // pull n0*a[i] addq %rax,%r15 adcq $0,%rdx addq %r15,%r14 - movq 0(%rbp),%rax + movq 0(%rbp),%rax // pull n[0] movq %rdx,%r15 adcq $0,%r15 @@ -1937,13 +1943,13 @@ L$8x_tail: jnz L$8x_tail leaq 64(%rbp),%rbp - movq 8+8(%rsp),%rdx - cmpq 0+8(%rsp),%rbp - jae L$8x_tail_done + movq 8+8(%rsp),%rdx // pull end of t[] + cmpq 0+8(%rsp),%rbp // end of n[]? + jae L$8x_tail_done // break out of loop - movq 48+56+8(%rsp),%rbx + movq 48+56+8(%rsp),%rbx // pull n0*a[0] negq %rsi - movq 0(%rbp),%rax + movq 0(%rbp),%rax // pull n[0] adcq 0(%rdi),%r8 adcq 8(%rdi),%r9 adcq 16(%rdi),%r10 @@ -1952,7 +1958,7 @@ L$8x_tail: adcq 40(%rdi),%r13 adcq 48(%rdi),%r14 adcq 56(%rdi),%r15 - sbbq %rsi,%rsi + sbbq %rsi,%rsi // top carry movl $8,%ecx jmp L$8x_tail @@ -1960,7 +1966,7 @@ L$8x_tail: .p2align 5 L$8x_tail_done: xorq %rax,%rax - addq (%rdx),%r8 + addq (%rdx),%r8 // can this overflow? adcq $0,%r9 adcq $0,%r10 adcq $0,%r11 @@ -1980,15 +1986,15 @@ L$8x_no_tail: adcq 40(%rdi),%r13 adcq 48(%rdi),%r14 adcq 56(%rdi),%r15 - adcq $0,%rax - movq -8(%rbp),%rcx + adcq $0,%rax // top-most carry + movq -8(%rbp),%rcx // np[num-1] xorq %rsi,%rsi - movq %xmm2,%rbp + movq %xmm2,%rbp // restore %rbp - movq %r8,0(%rdi) + movq %r8,0(%rdi) // store top 512 bits movq %r9,8(%rdi) - movq %xmm3,%r9 + movq %xmm3,%r9 // %r9 is %r9, can't be moved upwards movq %r10,16(%rdi) movq %r11,24(%rdi) movq %r12,32(%rdi) @@ -1997,23 +2003,24 @@ L$8x_no_tail: movq %r15,56(%rdi) leaq 64(%rdi),%rdi - cmpq %rdx,%rdi + cmpq %rdx,%rdi // end of t[]? jb L$8x_reduction_loop ret .p2align 5 +.alt_entry __bn_post4x_internal __bn_post4x_internal: movq 0(%rbp),%r12 - leaq (%rdi,%r9,1),%rbx + leaq (%rdi,%r9,1),%rbx // %rdi was %rbx above movq %r9,%rcx - movq %xmm1,%rdi + movq %xmm1,%rdi // restore %rdi negq %rax - movq %xmm1,%rsi + movq %xmm1,%rsi // prepare for back-to-back call sarq $3+2,%rcx - decq %r12 + decq %r12 // so that after 'not' we get -n[0] xorq %r10,%r10 movq 8(%rbp),%r13 movq 16(%rbp),%r14 @@ -2037,7 +2044,7 @@ L$sqr4x_sub_entry: andq %rax,%r14 andq %rax,%r15 - negq %r10 + negq %r10 // mov %r10,%cf adcq 0(%rbx),%r12 adcq 8(%rbx),%r13 adcq 16(%rbx),%r14 @@ -2045,16 +2052,16 @@ L$sqr4x_sub_entry: movq %r12,0(%rdi) leaq 32(%rbx),%rbx movq %r13,8(%rdi) - sbbq %r10,%r10 + sbbq %r10,%r10 // mov %cf,%r10 movq %r14,16(%rdi) movq %r15,24(%rdi) leaq 32(%rdi),%rdi - incq %rcx + incq %rcx // pass %cf jnz L$sqr4x_sub - movq %r9,%r10 - negq %r9 + movq %r9,%r10 // prepare for back-to-back call + negq %r9 // restore %r9 ret @@ -2062,6 +2069,7 @@ L$sqr4x_sub_entry: .private_extern _bn_mulx4x_mont_gather5 .p2align 5 +.alt_entry _bn_mulx4x_mont_gather5 _bn_mulx4x_mont_gather5: _CET_ENDBR @@ -2081,42 +2089,42 @@ _CET_ENDBR L$mulx4x_prologue: - - - - shll $3,%r9d - leaq (%r9,%r9,2),%r10 - negq %r9 - movq (%r8),%r8 - - - - - - - - - - +// num is declared as an int, a 32-bit parameter, so the upper half is +// undefined. It is important that this write to %r9, which zeros the +// upper half, predates the first access. + shll $3,%r9d // convert %r9 to bytes + leaq (%r9,%r9,2),%r10 // 3*%r9 in bytes + negq %r9 // -%r9 + movq (%r8),%r8 // *n0 + +// ############################################################# +// Ensure that stack frame doesn't alias with +3*%r9 +// modulo 4096, which covers ret[num], am[num] and n[num] +// (see bn_exp.c). This is done to allow memory disambiguation +// logic do its magic. [Extra [num] is allocated in order +// to align with bn_power5's frame, which is cleansed after +// completing exponentiation. Extra 256 bytes is for power mask +// calculated from 7th argument, the index.] +// leaq -320(%rsp,%r9,2),%r11 movq %rsp,%rbp subq %rdi,%r11 andq $4095,%r11 cmpq %r11,%r10 jb L$mulx4xsp_alt - subq %r11,%rbp - leaq -320(%rbp,%r9,2),%rbp + subq %r11,%rbp // align with + leaq -320(%rbp,%r9,2),%rbp // future alloca(frame+2*%r9*8+256) jmp L$mulx4xsp_done L$mulx4xsp_alt: leaq 4096-320(,%r9,2),%r10 - leaq -320(%rbp,%r9,2),%rbp + leaq -320(%rbp,%r9,2),%rbp // future alloca(frame+2*%r9*8+256) subq %r10,%r11 movq $0,%r10 cmovcq %r10,%r11 subq %r11,%rbp L$mulx4xsp_done: - andq $-64,%rbp + andq $-64,%rbp // ensure alignment movq %rsp,%r11 subq %rbp,%r11 andq $-4096,%r11 @@ -2133,25 +2141,25 @@ L$mulx4x_page_walk: ja L$mulx4x_page_walk L$mulx4x_page_walk_done: - - - - - - - - - - - - - movq %r8,32(%rsp) - movq %rax,40(%rsp) +// ############################################################# +// Stack layout +// +0 -num +// +8 off-loaded &b[i] +// +16 end of b[num] +// +24 inner counter +// +32 saved n0 +// +40 saved %rsp +// +48 +// +56 saved rp +// +64 tmp[num+1] +// + movq %r8,32(%rsp) // save *n0 + movq %rax,40(%rsp) // save original %rsp L$mulx4x_body: call mulx4x_internal - movq 40(%rsp),%rsi + movq 40(%rsp),%rsi // restore %rsp movq $1,%rax @@ -2176,46 +2184,47 @@ L$mulx4x_epilogue: .p2align 5 +.alt_entry mulx4x_internal mulx4x_internal: - movq %r9,8(%rsp) + movq %r9,8(%rsp) // save -%r9 (it was in bytes) movq %r9,%r10 - negq %r9 + negq %r9 // restore %r9 shlq $5,%r9 - negq %r10 - leaq 128(%rdx,%r9,1),%r13 + negq %r10 // restore %r9 + leaq 128(%rdx,%r9,1),%r13 // end of powers table (+size optimization) shrq $5+5,%r9 - movd 8(%rax),%xmm5 + movd 8(%rax),%xmm5 // load 7th argument subq $1,%r9 leaq L$inc(%rip),%rax - movq %r13,16+8(%rsp) - movq %r9,24+8(%rsp) - movq %rdi,56+8(%rsp) - movdqa 0(%rax),%xmm0 - movdqa 16(%rax),%xmm1 - leaq 88-112(%rsp,%r10,1),%r10 - leaq 128(%rdx),%rdi - - pshufd $0,%xmm5,%xmm5 + movq %r13,16+8(%rsp) // end of b[num] + movq %r9,24+8(%rsp) // inner counter + movq %rdi,56+8(%rsp) // save %rdi + movdqa 0(%rax),%xmm0 // 00000001000000010000000000000000 + movdqa 16(%rax),%xmm1 // 00000002000000020000000200000002 + leaq 88-112(%rsp,%r10,1),%r10 // place the mask after tp[num+1] (+ICache optimization) + leaq 128(%rdx),%rdi // size optimization + + pshufd $0,%xmm5,%xmm5 // broadcast index movdqa %xmm1,%xmm4 .byte 0x67 movdqa %xmm1,%xmm2 .byte 0x67 paddd %xmm0,%xmm1 - pcmpeqd %xmm5,%xmm0 + pcmpeqd %xmm5,%xmm0 // compare to 1,0 movdqa %xmm4,%xmm3 paddd %xmm1,%xmm2 - pcmpeqd %xmm5,%xmm1 + pcmpeqd %xmm5,%xmm1 // compare to 3,2 movdqa %xmm0,112(%r10) movdqa %xmm4,%xmm0 paddd %xmm2,%xmm3 - pcmpeqd %xmm5,%xmm2 + pcmpeqd %xmm5,%xmm2 // compare to 5,4 movdqa %xmm1,128(%r10) movdqa %xmm4,%xmm1 paddd %xmm3,%xmm0 - pcmpeqd %xmm5,%xmm3 + pcmpeqd %xmm5,%xmm3 // compare to 7,6 movdqa %xmm2,144(%r10) movdqa %xmm4,%xmm2 @@ -2224,17 +2233,17 @@ mulx4x_internal: movdqa %xmm3,160(%r10) movdqa %xmm4,%xmm3 paddd %xmm1,%xmm2 - pcmpeqd %xmm5,%xmm1 + pcmpeqd %xmm5,%xmm1 // compare to 3,2 movdqa %xmm0,176(%r10) movdqa %xmm4,%xmm0 paddd %xmm2,%xmm3 - pcmpeqd %xmm5,%xmm2 + pcmpeqd %xmm5,%xmm2 // compare to 5,4 movdqa %xmm1,192(%r10) movdqa %xmm4,%xmm1 paddd %xmm3,%xmm0 - pcmpeqd %xmm5,%xmm3 + pcmpeqd %xmm5,%xmm3 // compare to 7,6 movdqa %xmm2,208(%r10) movdqa %xmm4,%xmm2 @@ -2243,17 +2252,17 @@ mulx4x_internal: movdqa %xmm3,224(%r10) movdqa %xmm4,%xmm3 paddd %xmm1,%xmm2 - pcmpeqd %xmm5,%xmm1 + pcmpeqd %xmm5,%xmm1 // compare to 3,2 movdqa %xmm0,240(%r10) movdqa %xmm4,%xmm0 paddd %xmm2,%xmm3 - pcmpeqd %xmm5,%xmm2 + pcmpeqd %xmm5,%xmm2 // compare to 5,4 movdqa %xmm1,256(%r10) movdqa %xmm4,%xmm1 paddd %xmm3,%xmm0 - pcmpeqd %xmm5,%xmm3 + pcmpeqd %xmm5,%xmm3 // compare to 7,6 movdqa %xmm2,272(%r10) movdqa %xmm4,%xmm2 @@ -2273,7 +2282,7 @@ mulx4x_internal: pcmpeqd %xmm5,%xmm3 movdqa %xmm2,336(%r10) - pand 64(%rdi),%xmm0 + pand 64(%rdi),%xmm0 // while it's still in register pand 80(%rdi),%xmm1 pand 96(%rdi),%xmm2 movdqa %xmm3,352(%r10) @@ -2317,41 +2326,41 @@ mulx4x_internal: por %xmm2,%xmm0 por %xmm3,%xmm1 pxor %xmm1,%xmm0 - - pshufd $0x4e,%xmm0,%xmm1 +// Combine the upper and lower halves of %xmm0. + pshufd $0x4e,%xmm0,%xmm1 // Swap upper and lower halves. por %xmm1,%xmm0 leaq 256(%rdi),%rdi - movq %xmm0,%rdx + movq %xmm0,%rdx // bp[0] leaq 64+32+8(%rsp),%rbx movq %rdx,%r9 - mulxq 0(%rsi),%r8,%rax - mulxq 8(%rsi),%r11,%r12 + mulxq 0(%rsi),%r8,%rax // a[0]*b[0] + mulxq 8(%rsi),%r11,%r12 // a[1]*b[0] addq %rax,%r11 - mulxq 16(%rsi),%rax,%r13 + mulxq 16(%rsi),%rax,%r13 // ... adcq %rax,%r12 adcq $0,%r13 mulxq 24(%rsi),%rax,%r14 movq %r8,%r15 - imulq 32+8(%rsp),%r8 - xorq %rbp,%rbp + imulq 32+8(%rsp),%r8 // "t[0]"*n0 + xorq %rbp,%rbp // cf=0, of=0 movq %r8,%rdx - movq %rdi,8+8(%rsp) + movq %rdi,8+8(%rsp) // off-load &b[i] leaq 32(%rsi),%rsi adcxq %rax,%r13 - adcxq %rbp,%r14 + adcxq %rbp,%r14 // cf=0 mulxq 0(%rcx),%rax,%r10 - adcxq %rax,%r15 + adcxq %rax,%r15 // discarded adoxq %r11,%r10 mulxq 8(%rcx),%rax,%r11 adcxq %rax,%r10 adoxq %r12,%r11 mulxq 16(%rcx),%rax,%r12 - movq 24+8(%rsp),%rdi + movq 24+8(%rsp),%rdi // counter value movq %r10,-32(%rbx) adcxq %rax,%r11 adoxq %r13,%r12 @@ -2359,25 +2368,25 @@ mulx4x_internal: movq %r9,%rdx movq %r11,-24(%rbx) adcxq %rax,%r12 - adoxq %rbp,%r15 + adoxq %rbp,%r15 // of=0 leaq 32(%rcx),%rcx movq %r12,-16(%rbx) jmp L$mulx4x_1st .p2align 5 L$mulx4x_1st: - adcxq %rbp,%r15 - mulxq 0(%rsi),%r10,%rax + adcxq %rbp,%r15 // cf=0, modulo-scheduled + mulxq 0(%rsi),%r10,%rax // a[4]*b[0] adcxq %r14,%r10 - mulxq 8(%rsi),%r11,%r14 + mulxq 8(%rsi),%r11,%r14 // a[5]*b[0] adcxq %rax,%r11 - mulxq 16(%rsi),%r12,%rax + mulxq 16(%rsi),%r12,%rax // ... adcxq %r14,%r12 mulxq 24(%rsi),%r13,%r14 .byte 0x67,0x67 movq %r8,%rdx adcxq %rax,%r13 - adcxq %rbp,%r14 + adcxq %rbp,%r14 // cf=0 leaq 32(%rsi),%rsi leaq 32(%rbx),%rbx @@ -2401,21 +2410,21 @@ L$mulx4x_1st: leaq 32(%rcx),%rcx movq %r13,-16(%rbx) - decq %rdi + decq %rdi // of=0, pass cf jnz L$mulx4x_1st - movq 8(%rsp),%rax - adcq %rbp,%r15 - leaq (%rsi,%rax,1),%rsi + movq 8(%rsp),%rax // load -num + adcq %rbp,%r15 // modulo-scheduled + leaq (%rsi,%rax,1),%rsi // rewind %rsi addq %r15,%r14 - movq 8+8(%rsp),%rdi - adcq %rbp,%rbp + movq 8+8(%rsp),%rdi // re-load &b[i] + adcq %rbp,%rbp // top-most carry movq %r14,-8(%rbx) jmp L$mulx4x_outer .p2align 5 L$mulx4x_outer: - leaq 16-256(%rbx),%r10 + leaq 16-256(%rbx),%r10 // where 256-byte mask is (+density control) pxor %xmm4,%xmm4 .byte 0x67,0x67 pxor %xmm5,%xmm5 @@ -2468,41 +2477,41 @@ L$mulx4x_outer: por %xmm2,%xmm4 por %xmm3,%xmm5 por %xmm5,%xmm4 - - pshufd $0x4e,%xmm4,%xmm0 +// Combine the upper and lower halves of %xmm4 as %xmm0. + pshufd $0x4e,%xmm4,%xmm0 // Swap upper and lower halves. por %xmm4,%xmm0 leaq 256(%rdi),%rdi - movq %xmm0,%rdx + movq %xmm0,%rdx // m0=bp[i] - movq %rbp,(%rbx) - leaq 32(%rbx,%rax,1),%rbx - mulxq 0(%rsi),%r8,%r11 - xorq %rbp,%rbp + movq %rbp,(%rbx) // save top-most carry + leaq 32(%rbx,%rax,1),%rbx // rewind %rbx + mulxq 0(%rsi),%r8,%r11 // a[0]*b[i] + xorq %rbp,%rbp // cf=0, of=0 movq %rdx,%r9 - mulxq 8(%rsi),%r14,%r12 - adoxq -32(%rbx),%r8 + mulxq 8(%rsi),%r14,%r12 // a[1]*b[i] + adoxq -32(%rbx),%r8 // +t[0] adcxq %r14,%r11 - mulxq 16(%rsi),%r15,%r13 + mulxq 16(%rsi),%r15,%r13 // ... adoxq -24(%rbx),%r11 adcxq %r15,%r12 mulxq 24(%rsi),%rdx,%r14 adoxq -16(%rbx),%r12 adcxq %rdx,%r13 - leaq (%rcx,%rax,1),%rcx + leaq (%rcx,%rax,1),%rcx // rewind %rcx leaq 32(%rsi),%rsi adoxq -8(%rbx),%r13 adcxq %rbp,%r14 adoxq %rbp,%r14 movq %r8,%r15 - imulq 32+8(%rsp),%r8 + imulq 32+8(%rsp),%r8 // "t[0]"*n0 movq %r8,%rdx - xorq %rbp,%rbp - movq %rdi,8+8(%rsp) + xorq %rbp,%rbp // cf=0, of=0 + movq %rdi,8+8(%rsp) // off-load &b[i] mulxq 0(%rcx),%rax,%r10 - adcxq %rax,%r15 + adcxq %rax,%r15 // discarded adoxq %r11,%r10 mulxq 8(%rcx),%rax,%r11 adcxq %rax,%r10 @@ -2512,24 +2521,24 @@ L$mulx4x_outer: adoxq %r13,%r12 mulxq 24(%rcx),%rax,%r15 movq %r9,%rdx - movq 24+8(%rsp),%rdi + movq 24+8(%rsp),%rdi // counter value movq %r10,-32(%rbx) adcxq %rax,%r12 movq %r11,-24(%rbx) - adoxq %rbp,%r15 + adoxq %rbp,%r15 // of=0 movq %r12,-16(%rbx) leaq 32(%rcx),%rcx jmp L$mulx4x_inner .p2align 5 L$mulx4x_inner: - mulxq 0(%rsi),%r10,%rax - adcxq %rbp,%r15 + mulxq 0(%rsi),%r10,%rax // a[4]*b[i] + adcxq %rbp,%r15 // cf=0, modulo-scheduled adoxq %r14,%r10 - mulxq 8(%rsi),%r11,%r14 + mulxq 8(%rsi),%r11,%r14 // a[5]*b[i] adcxq 0(%rbx),%r10 adoxq %rax,%r11 - mulxq 16(%rsi),%r12,%rax + mulxq 16(%rsi),%r12,%rax // ... adcxq 8(%rbx),%r11 adoxq %r14,%r12 mulxq 24(%rsi),%r13,%r14 @@ -2537,10 +2546,10 @@ L$mulx4x_inner: adcxq 16(%rbx),%r12 adoxq %rax,%r13 adcxq 24(%rbx),%r13 - adoxq %rbp,%r14 + adoxq %rbp,%r14 // of=0 leaq 32(%rsi),%rsi leaq 32(%rbx),%rbx - adcxq %rbp,%r14 + adcxq %rbp,%r14 // cf=0 adoxq %r15,%r10 mulxq 0(%rcx),%rax,%r15 @@ -2562,17 +2571,17 @@ L$mulx4x_inner: adoxq %rbp,%r15 movq %r13,-16(%rbx) - decq %rdi + decq %rdi // of=0, pass cf jnz L$mulx4x_inner - movq 0+8(%rsp),%rax - adcq %rbp,%r15 - subq 0(%rbx),%rdi - movq 8+8(%rsp),%rdi + movq 0+8(%rsp),%rax // load -num + adcq %rbp,%r15 // modulo-scheduled + subq 0(%rbx),%rdi // pull top-most carry to %cf + movq 8+8(%rsp),%rdi // re-load &b[i] movq 16+8(%rsp),%r10 adcq %r15,%r14 - leaq (%rsi,%rax,1),%rsi - adcq %rbp,%rbp + leaq (%rsi,%rax,1),%rsi // rewind %rsi + adcq %rbp,%rbp // top-most carry movq %r14,-8(%rbx) cmpq %r10,%rdi @@ -2581,29 +2590,30 @@ L$mulx4x_inner: movq -8(%rcx),%r10 movq %rbp,%r8 movq (%rcx,%rax,1),%r12 - leaq (%rcx,%rax,1),%rbp + leaq (%rcx,%rax,1),%rbp // rewind %rcx movq %rax,%rcx - leaq (%rbx,%rax,1),%rdi + leaq (%rbx,%rax,1),%rdi // rewind %rbx xorl %eax,%eax xorq %r15,%r15 - subq %r14,%r10 + subq %r14,%r10 // compare top-most words adcq %r15,%r15 orq %r15,%r8 sarq $3+2,%rcx - subq %r8,%rax - movq 56+8(%rsp),%rdx - decq %r12 + subq %r8,%rax // %rax=-%r8 + movq 56+8(%rsp),%rdx // restore rp + decq %r12 // so that after 'not' we get -n[0] movq 8(%rbp),%r13 xorq %r8,%r8 movq 16(%rbp),%r14 movq 24(%rbp),%r15 - jmp L$sqrx4x_sub_entry + jmp L$sqrx4x_sub_entry // common post-condition .globl _bn_powerx5 .private_extern _bn_powerx5 .p2align 5 +.alt_entry _bn_powerx5 _bn_powerx5: _CET_ENDBR @@ -2623,35 +2633,35 @@ _CET_ENDBR L$powerx5_prologue: - - - - shll $3,%r9d - leaq (%r9,%r9,2),%r10 +// num is declared as an int, a 32-bit parameter, so the upper half is +// undefined. It is important that this write to %r9, which zeros the +// upper half, predates the first access. + shll $3,%r9d // convert %r9 to bytes + leaq (%r9,%r9,2),%r10 // 3*%r9 in bytes negq %r9 - movq (%r8),%r8 - - - - - - - - + movq (%r8),%r8 // *n0 + +// ############################################################# +// Ensure that stack frame doesn't alias with %rdi+3*%r9 +// modulo 4096, which covers ret[num], am[num] and n[num] +// (see bn_exp.c). This is done to allow memory disambiguation +// logic do its magic. [Extra 256 bytes is for power mask +// calculated from 7th argument, the index.] +// leaq -320(%rsp,%r9,2),%r11 movq %rsp,%rbp subq %rdi,%r11 andq $4095,%r11 cmpq %r11,%r10 jb L$pwrx_sp_alt - subq %r11,%rbp - leaq -320(%rbp,%r9,2),%rbp + subq %r11,%rbp // align with %rsi + leaq -320(%rbp,%r9,2),%rbp // future alloca(frame+2*%r9*8+256) jmp L$pwrx_sp_done .p2align 5 L$pwrx_sp_alt: leaq 4096-320(,%r9,2),%r10 - leaq -320(%rbp,%r9,2),%rbp + leaq -320(%rbp,%r9,2),%rbp // alloca(frame+2*%r9*8+256) subq %r10,%r11 movq $0,%r10 cmovcq %r10,%r11 @@ -2677,24 +2687,24 @@ L$pwrx_page_walk_done: movq %r9,%r10 negq %r9 - - - - - - - - - - - +// ############################################################# +// Stack layout +// +// +0 saved %r9, used in reduction section +// +8 &t[2*%r9], used in reduction section +// +16 intermediate carry bit +// +24 top-most carry bit, used in reduction section +// +32 saved *n0 +// +40 saved %rsp +// +48 t[2*%r9] +// pxor %xmm0,%xmm0 - movq %rdi,%xmm1 - movq %rcx,%xmm2 - movq %r10,%xmm3 + movq %rdi,%xmm1 // save %rdi + movq %rcx,%xmm2 // save %rcx + movq %r10,%xmm3 // -%r9 movq %rdx,%xmm4 movq %r8,32(%rsp) - movq %rax,40(%rsp) + movq %rax,40(%rsp) // save original %rsp L$powerx5_body: @@ -2709,7 +2719,7 @@ L$powerx5_body: call __bn_sqrx8x_internal call __bn_postx4x_internal - movq %r10,%r9 + movq %r10,%r9 // -num movq %rsi,%rdi movq %xmm2,%rcx movq %xmm4,%rdx @@ -2717,7 +2727,7 @@ L$powerx5_body: call mulx4x_internal - movq 40(%rsp),%rsi + movq 40(%rsp),%rsi // restore %rsp movq $1,%rax @@ -2745,54 +2755,56 @@ L$powerx5_epilogue: .private_extern _bn_sqrx8x_internal .p2align 5 +.alt_entry _bn_sqrx8x_internal _bn_sqrx8x_internal: +.alt_entry __bn_sqrx8x_internal __bn_sqrx8x_internal: _CET_ENDBR - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +// ################################################################# +// Squaring part: +// +// a) multiply-n-add everything but a[i]*a[i]; +// b) shift result of a) by 1 to the left and accumulate +// a[i]*a[i] products; +// +// ################################################################# +// a[7]a[7]a[6]a[6]a[5]a[5]a[4]a[4]a[3]a[3]a[2]a[2]a[1]a[1]a[0]a[0] +// a[1]a[0] +// a[2]a[0] +// a[3]a[0] +// a[2]a[1] +// a[3]a[1] +// a[3]a[2] +// +// a[4]a[0] +// a[5]a[0] +// a[6]a[0] +// a[7]a[0] +// a[4]a[1] +// a[5]a[1] +// a[6]a[1] +// a[7]a[1] +// a[4]a[2] +// a[5]a[2] +// a[6]a[2] +// a[7]a[2] +// a[4]a[3] +// a[5]a[3] +// a[6]a[3] +// a[7]a[3] +// +// a[5]a[4] +// a[6]a[4] +// a[7]a[4] +// a[6]a[5] +// a[7]a[5] +// a[7]a[6] +// a[7]a[7]a[6]a[6]a[5]a[5]a[4]a[4]a[3]a[3]a[2]a[2]a[1]a[1]a[0]a[0] leaq 48+8(%rsp),%rdi leaq (%rsi,%r9,1),%rbp - movq %r9,0+8(%rsp) - movq %rbp,8+8(%rsp) + movq %r9,0+8(%rsp) // save %r9 + movq %rbp,8+8(%rsp) // save end of %rsi jmp L$sqr8x_zero_start .p2align 5 @@ -2803,7 +2815,7 @@ L$sqrx8x_zero: movdqa %xmm0,16(%rdi) movdqa %xmm0,32(%rdi) movdqa %xmm0,48(%rdi) -L$sqr8x_zero_start: +L$sqr8x_zero_start: // aligned at 32 movdqa %xmm0,64(%rdi) movdqa %xmm0,80(%rdi) movdqa %xmm0,96(%rdi) @@ -2812,8 +2824,8 @@ L$sqr8x_zero_start: subq $64,%r9 jnz L$sqrx8x_zero - movq 0(%rsi),%rdx - + movq 0(%rsi),%rdx // a[0], modulo-scheduled +// xor %r9,%r9 # t[1], ex-%r9, zero already xorq %r10,%r10 xorq %r11,%r11 xorq %r12,%r12 @@ -2821,21 +2833,21 @@ L$sqr8x_zero_start: xorq %r14,%r14 xorq %r15,%r15 leaq 48+8(%rsp),%rdi - xorq %rbp,%rbp + xorq %rbp,%rbp // cf=0, cf=0 jmp L$sqrx8x_outer_loop .p2align 5 L$sqrx8x_outer_loop: - mulxq 8(%rsi),%r8,%rax - adcxq %r9,%r8 + mulxq 8(%rsi),%r8,%rax // a[1]*a[0] + adcxq %r9,%r8 // a[1]*a[0]+=t[1] adoxq %rax,%r10 - mulxq 16(%rsi),%r9,%rax + mulxq 16(%rsi),%r9,%rax // a[2]*a[0] adcxq %r10,%r9 adoxq %rax,%r11 -.byte 0xc4,0xe2,0xab,0xf6,0x86,0x18,0x00,0x00,0x00 +.byte 0xc4,0xe2,0xab,0xf6,0x86,0x18,0x00,0x00,0x00 // mulx 3*8(%rsi),%r10,%rax # ... adcxq %r11,%r10 adoxq %rax,%r12 -.byte 0xc4,0xe2,0xa3,0xf6,0x86,0x20,0x00,0x00,0x00 +.byte 0xc4,0xe2,0xa3,0xf6,0x86,0x20,0x00,0x00,0x00 // mulx 4*8(%rsi),%r11,%rax adcxq %r12,%r11 adoxq %rax,%r13 mulxq 40(%rsi),%r12,%rax @@ -2845,97 +2857,97 @@ L$sqrx8x_outer_loop: adcxq %r14,%r13 adoxq %r15,%rax mulxq 56(%rsi),%r14,%r15 - movq 8(%rsi),%rdx + movq 8(%rsi),%rdx // a[1] adcxq %rax,%r14 adoxq %rbp,%r15 adcq 64(%rdi),%r15 - movq %r8,8(%rdi) - movq %r9,16(%rdi) - sbbq %rcx,%rcx - xorq %rbp,%rbp + movq %r8,8(%rdi) // t[1] + movq %r9,16(%rdi) // t[2] + sbbq %rcx,%rcx // mov %cf,%rcx + xorq %rbp,%rbp // cf=0, of=0 - mulxq 16(%rsi),%r8,%rbx - mulxq 24(%rsi),%r9,%rax + mulxq 16(%rsi),%r8,%rbx // a[2]*a[1] + mulxq 24(%rsi),%r9,%rax // a[3]*a[1] adcxq %r10,%r8 adoxq %rbx,%r9 - mulxq 32(%rsi),%r10,%rbx + mulxq 32(%rsi),%r10,%rbx // ... adcxq %r11,%r9 adoxq %rax,%r10 -.byte 0xc4,0xe2,0xa3,0xf6,0x86,0x28,0x00,0x00,0x00 +.byte 0xc4,0xe2,0xa3,0xf6,0x86,0x28,0x00,0x00,0x00 // mulx 5*8(%rsi),%r11,%rax adcxq %r12,%r10 adoxq %rbx,%r11 -.byte 0xc4,0xe2,0x9b,0xf6,0x9e,0x30,0x00,0x00,0x00 +.byte 0xc4,0xe2,0x9b,0xf6,0x9e,0x30,0x00,0x00,0x00 // mulx 6*8(%rsi),%r12,%rbx adcxq %r13,%r11 adoxq %r14,%r12 -.byte 0xc4,0x62,0x93,0xf6,0xb6,0x38,0x00,0x00,0x00 - movq 16(%rsi),%rdx +.byte 0xc4,0x62,0x93,0xf6,0xb6,0x38,0x00,0x00,0x00 // mulx 7*8(%rsi),%r13,%r14 + movq 16(%rsi),%rdx // a[2] adcxq %rax,%r12 adoxq %rbx,%r13 adcxq %r15,%r13 - adoxq %rbp,%r14 - adcxq %rbp,%r14 + adoxq %rbp,%r14 // of=0 + adcxq %rbp,%r14 // cf=0 - movq %r8,24(%rdi) - movq %r9,32(%rdi) + movq %r8,24(%rdi) // t[3] + movq %r9,32(%rdi) // t[4] - mulxq 24(%rsi),%r8,%rbx - mulxq 32(%rsi),%r9,%rax + mulxq 24(%rsi),%r8,%rbx // a[3]*a[2] + mulxq 32(%rsi),%r9,%rax // a[4]*a[2] adcxq %r10,%r8 adoxq %rbx,%r9 - mulxq 40(%rsi),%r10,%rbx + mulxq 40(%rsi),%r10,%rbx // ... adcxq %r11,%r9 adoxq %rax,%r10 -.byte 0xc4,0xe2,0xa3,0xf6,0x86,0x30,0x00,0x00,0x00 +.byte 0xc4,0xe2,0xa3,0xf6,0x86,0x30,0x00,0x00,0x00 // mulx 6*8(%rsi),%r11,%rax adcxq %r12,%r10 adoxq %r13,%r11 -.byte 0xc4,0x62,0x9b,0xf6,0xae,0x38,0x00,0x00,0x00 +.byte 0xc4,0x62,0x9b,0xf6,0xae,0x38,0x00,0x00,0x00 // mulx 7*8(%rsi),%r12,%r13 .byte 0x3e - movq 24(%rsi),%rdx + movq 24(%rsi),%rdx // a[3] adcxq %rbx,%r11 adoxq %rax,%r12 adcxq %r14,%r12 - movq %r8,40(%rdi) - movq %r9,48(%rdi) - mulxq 32(%rsi),%r8,%rax - adoxq %rbp,%r13 - adcxq %rbp,%r13 + movq %r8,40(%rdi) // t[5] + movq %r9,48(%rdi) // t[6] + mulxq 32(%rsi),%r8,%rax // a[4]*a[3] + adoxq %rbp,%r13 // of=0 + adcxq %rbp,%r13 // cf=0 - mulxq 40(%rsi),%r9,%rbx + mulxq 40(%rsi),%r9,%rbx // a[5]*a[3] adcxq %r10,%r8 adoxq %rax,%r9 - mulxq 48(%rsi),%r10,%rax + mulxq 48(%rsi),%r10,%rax // ... adcxq %r11,%r9 adoxq %r12,%r10 mulxq 56(%rsi),%r11,%r12 - movq 32(%rsi),%rdx - movq 40(%rsi),%r14 + movq 32(%rsi),%rdx // a[4] + movq 40(%rsi),%r14 // a[5] adcxq %rbx,%r10 adoxq %rax,%r11 - movq 48(%rsi),%r15 + movq 48(%rsi),%r15 // a[6] adcxq %r13,%r11 - adoxq %rbp,%r12 - adcxq %rbp,%r12 + adoxq %rbp,%r12 // of=0 + adcxq %rbp,%r12 // cf=0 - movq %r8,56(%rdi) - movq %r9,64(%rdi) + movq %r8,56(%rdi) // t[7] + movq %r9,64(%rdi) // t[8] - mulxq %r14,%r9,%rax - movq 56(%rsi),%r8 + mulxq %r14,%r9,%rax // a[5]*a[4] + movq 56(%rsi),%r8 // a[7] adcxq %r10,%r9 - mulxq %r15,%r10,%rbx + mulxq %r15,%r10,%rbx // a[6]*a[4] adoxq %rax,%r10 adcxq %r11,%r10 - mulxq %r8,%r11,%rax - movq %r14,%rdx + mulxq %r8,%r11,%rax // a[7]*a[4] + movq %r14,%rdx // a[5] adoxq %rbx,%r11 adcxq %r12,%r11 +// adox %rbp,%rax # of=0 + adcxq %rbp,%rax // cf=0 - adcxq %rbp,%rax - - mulxq %r15,%r14,%rbx - mulxq %r8,%r12,%r13 - movq %r15,%rdx + mulxq %r15,%r14,%rbx // a[6]*a[5] + mulxq %r8,%r12,%r13 // a[7]*a[5] + movq %r15,%rdx // a[6] leaq 64(%rsi),%rsi adcxq %r14,%r11 adoxq %rbx,%r12 @@ -2943,19 +2955,19 @@ L$sqrx8x_outer_loop: adoxq %rbp,%r13 .byte 0x67,0x67 - mulxq %r8,%r8,%r14 + mulxq %r8,%r8,%r14 // a[7]*a[6] adcxq %r8,%r13 adcxq %rbp,%r14 cmpq 8+8(%rsp),%rsi je L$sqrx8x_outer_break - negq %rcx + negq %rcx // mov %rcx,%cf movq $-8,%rcx movq %rbp,%r15 movq 64(%rdi),%r8 - adcxq 72(%rdi),%r9 - adcxq 80(%rdi),%r10 + adcxq 72(%rdi),%r9 // +=t[9] + adcxq 80(%rdi),%r10 // ... adcxq 88(%rdi),%r11 adcq 96(%rdi),%r12 adcq 104(%rdi),%r13 @@ -2963,24 +2975,24 @@ L$sqrx8x_outer_loop: adcq 120(%rdi),%r15 leaq (%rsi),%rbp leaq 128(%rdi),%rdi - sbbq %rax,%rax + sbbq %rax,%rax // mov %cf,%rcx - movq -64(%rsi),%rdx - movq %rax,16+8(%rsp) + movq -64(%rsi),%rdx // a[0] + movq %rax,16+8(%rsp) // offload %rcx movq %rdi,24+8(%rsp) - - xorl %eax,%eax +// lea 8*8(%rdi),%rdi # see 2*8*8(%rdi) above + xorl %eax,%eax // cf=0, of=0 jmp L$sqrx8x_loop .p2align 5 L$sqrx8x_loop: movq %r8,%rbx - mulxq 0(%rbp),%rax,%r8 - adcxq %rax,%rbx + mulxq 0(%rbp),%rax,%r8 // a[8]*a[i] + adcxq %rax,%rbx // +=t[8] adoxq %r9,%r8 - mulxq 8(%rbp),%rax,%r9 + mulxq 8(%rbp),%rax,%r9 // ... adcxq %rax,%r8 adoxq %r10,%r9 @@ -2992,7 +3004,7 @@ L$sqrx8x_loop: adcxq %rax,%r10 adoxq %r12,%r11 -.byte 0xc4,0x62,0xfb,0xf6,0xa5,0x20,0x00,0x00,0x00 +.byte 0xc4,0x62,0xfb,0xf6,0xa5,0x20,0x00,0x00,0x00 // mulx 4*8(%rbp),%rax,%r12 adcxq %rax,%r11 adoxq %r13,%r12 @@ -3001,27 +3013,27 @@ L$sqrx8x_loop: adoxq %r14,%r13 mulxq 48(%rbp),%rax,%r14 - movq %rbx,(%rdi,%rcx,8) + movq %rbx,(%rdi,%rcx,8) // store t[8+i] movl $0,%ebx adcxq %rax,%r13 adoxq %r15,%r14 -.byte 0xc4,0x62,0xfb,0xf6,0xbd,0x38,0x00,0x00,0x00 - movq 8(%rsi,%rcx,8),%rdx +.byte 0xc4,0x62,0xfb,0xf6,0xbd,0x38,0x00,0x00,0x00 // mulx 7*8(%rbp),%rax,%r15 + movq 8(%rsi,%rcx,8),%rdx // a[i] adcxq %rax,%r14 - adoxq %rbx,%r15 - adcxq %rbx,%r15 + adoxq %rbx,%r15 // %rbx is 0, of=0 + adcxq %rbx,%r15 // cf=0 .byte 0x67 - incq %rcx + incq %rcx // of=0 jnz L$sqrx8x_loop leaq 64(%rbp),%rbp movq $-8,%rcx - cmpq 8+8(%rsp),%rbp + cmpq 8+8(%rsp),%rbp // done? je L$sqrx8x_break - subq 16+8(%rsp),%rbx + subq 16+8(%rsp),%rbx // mov 16(%rsp),%cf .byte 0x66 movq -64(%rsi),%rdx adcxq 0(%rdi),%r8 @@ -3034,19 +3046,19 @@ L$sqrx8x_loop: adcq 56(%rdi),%r15 leaq 64(%rdi),%rdi .byte 0x67 - sbbq %rax,%rax - xorl %ebx,%ebx - movq %rax,16+8(%rsp) + sbbq %rax,%rax // mov %cf,%rax + xorl %ebx,%ebx // cf=0, of=0 + movq %rax,16+8(%rsp) // offload carry jmp L$sqrx8x_loop .p2align 5 L$sqrx8x_break: xorq %rbp,%rbp - subq 16+8(%rsp),%rbx + subq 16+8(%rsp),%rbx // mov 16(%rsp),%cf adcxq %rbp,%r8 - movq 24+8(%rsp),%rcx + movq 24+8(%rsp),%rcx // initial %rdi, borrow %rcx adcxq %rbp,%r9 - movq 0(%rsi),%rdx + movq 0(%rsi),%rdx // a[8], modulo-scheduled adcq $0,%r10 movq %r8,0(%rdi) adcq $0,%r11 @@ -3054,7 +3066,7 @@ L$sqrx8x_break: adcq $0,%r13 adcq $0,%r14 adcq $0,%r15 - cmpq %rcx,%rdi + cmpq %rcx,%rdi // cf=0, of=0 je L$sqrx8x_outer_loop movq %r9,8(%rdi) @@ -3076,57 +3088,57 @@ L$sqrx8x_break: .p2align 5 L$sqrx8x_outer_break: - movq %r9,72(%rdi) - movq %xmm3,%rcx - movq %r10,80(%rdi) + movq %r9,72(%rdi) // t[9] + movq %xmm3,%rcx // -%r9 + movq %r10,80(%rdi) // ... movq %r11,88(%rdi) movq %r12,96(%rdi) movq %r13,104(%rdi) movq %r14,112(%rdi) leaq 48+8(%rsp),%rdi - movq (%rsi,%rcx,1),%rdx + movq (%rsi,%rcx,1),%rdx // a[0] - movq 8(%rdi),%r11 - xorq %r10,%r10 - movq 0+8(%rsp),%r9 + movq 8(%rdi),%r11 // t[1] + xorq %r10,%r10 // t[0], of=0, cf=0 + movq 0+8(%rsp),%r9 // restore %r9 adoxq %r11,%r11 - movq 16(%rdi),%r12 - movq 24(%rdi),%r13 - + movq 16(%rdi),%r12 // t[2] # prefetch + movq 24(%rdi),%r13 // t[3] # prefetch +// jmp .Lsqrx4x_shift_n_add # happens to be aligned .p2align 5 L$sqrx4x_shift_n_add: mulxq %rdx,%rax,%rbx adoxq %r12,%r12 adcxq %r10,%rax -.byte 0x48,0x8b,0x94,0x0e,0x08,0x00,0x00,0x00 -.byte 0x4c,0x8b,0x97,0x20,0x00,0x00,0x00 +.byte 0x48,0x8b,0x94,0x0e,0x08,0x00,0x00,0x00 // mov 8(%rsi,%rcx),%rdx # a[i+1] # prefetch +.byte 0x4c,0x8b,0x97,0x20,0x00,0x00,0x00 // mov 32(%rdi),%r10 # t[2*i+4] # prefetch adoxq %r13,%r13 adcxq %r11,%rbx - movq 40(%rdi),%r11 + movq 40(%rdi),%r11 // t[2*i+4+1] # prefetch movq %rax,0(%rdi) movq %rbx,8(%rdi) mulxq %rdx,%rax,%rbx adoxq %r10,%r10 adcxq %r12,%rax - movq 16(%rsi,%rcx,1),%rdx - movq 48(%rdi),%r12 + movq 16(%rsi,%rcx,1),%rdx // a[i+2] # prefetch + movq 48(%rdi),%r12 // t[2*i+6] # prefetch adoxq %r11,%r11 adcxq %r13,%rbx - movq 56(%rdi),%r13 + movq 56(%rdi),%r13 // t[2*i+6+1] # prefetch movq %rax,16(%rdi) movq %rbx,24(%rdi) mulxq %rdx,%rax,%rbx adoxq %r12,%r12 adcxq %r10,%rax - movq 24(%rsi,%rcx,1),%rdx + movq 24(%rsi,%rcx,1),%rdx // a[i+3] # prefetch leaq 32(%rcx),%rcx - movq 64(%rdi),%r10 + movq 64(%rdi),%r10 // t[2*i+8] # prefetch adoxq %r13,%r13 adcxq %r11,%rbx - movq 72(%rdi),%r11 + movq 72(%rdi),%r11 // t[2*i+8+1] # prefetch movq %rax,32(%rdi) movq %rbx,40(%rdi) @@ -3134,11 +3146,11 @@ L$sqrx4x_shift_n_add: adoxq %r10,%r10 adcxq %r12,%rax jrcxz L$sqrx4x_shift_n_add_break -.byte 0x48,0x8b,0x94,0x0e,0x00,0x00,0x00,0x00 +.byte 0x48,0x8b,0x94,0x0e,0x00,0x00,0x00,0x00 // mov 0(%rsi,%rcx),%rdx # a[i+4] # prefetch adoxq %r11,%r11 adcxq %r13,%rbx - movq 80(%rdi),%r12 - movq 88(%rdi),%r13 + movq 80(%rdi),%r12 // t[2*i+10] # prefetch + movq 88(%rdi),%r13 // t[2*i+10+1] # prefetch movq %rax,48(%rdi) movq %rbx,56(%rdi) leaq 64(%rdi),%rdi @@ -3150,18 +3162,19 @@ L$sqrx4x_shift_n_add_break: adcxq %r13,%rbx movq %rax,48(%rdi) movq %rbx,56(%rdi) - leaq 64(%rdi),%rdi + leaq 64(%rdi),%rdi // end of t[] buffer movq %xmm2,%rbp +.alt_entry __bn_sqrx8x_reduction __bn_sqrx8x_reduction: - xorl %eax,%eax - movq 32+8(%rsp),%rbx - movq 48+8(%rsp),%rdx - leaq -64(%rbp,%r9,1),%rcx - - movq %rcx,0+8(%rsp) - movq %rdi,8+8(%rsp) - - leaq 48+8(%rsp),%rdi + xorl %eax,%eax // initial top-most carry bit + movq 32+8(%rsp),%rbx // n0 + movq 48+8(%rsp),%rdx // "%r8", 8*0(%rdi) + leaq -64(%rbp,%r9,1),%rcx // end of n[] +// lea 48+8(%rsp,%r9,2),%rdi # end of t[] buffer + movq %rcx,0+8(%rsp) // save end of n[] + movq %rdi,8+8(%rsp) // save end of t[] + + leaq 48+8(%rsp),%rdi // initial t[] window jmp L$sqrx8x_reduction_loop .p2align 5 @@ -3171,25 +3184,25 @@ L$sqrx8x_reduction_loop: movq 24(%rdi),%r11 movq 32(%rdi),%r12 movq %rdx,%r8 - imulq %rbx,%rdx + imulq %rbx,%rdx // n0*a[i] movq 40(%rdi),%r13 movq 48(%rdi),%r14 movq 56(%rdi),%r15 - movq %rax,24+8(%rsp) + movq %rax,24+8(%rsp) // store top-most carry bit leaq 64(%rdi),%rdi - xorq %rsi,%rsi + xorq %rsi,%rsi // cf=0,of=0 movq $-8,%rcx jmp L$sqrx8x_reduce .p2align 5 L$sqrx8x_reduce: movq %r8,%rbx - mulxq 0(%rbp),%rax,%r8 - adcxq %rbx,%rax + mulxq 0(%rbp),%rax,%r8 // n[0] + adcxq %rbx,%rax // discarded adoxq %r9,%r8 - mulxq 8(%rbp),%rbx,%r9 + mulxq 8(%rbp),%rbx,%r9 // n[1] adcxq %rbx,%r8 adoxq %r10,%r9 @@ -3201,15 +3214,15 @@ L$sqrx8x_reduce: adcxq %rbx,%r10 adoxq %r12,%r11 -.byte 0xc4,0x62,0xe3,0xf6,0xa5,0x20,0x00,0x00,0x00 +.byte 0xc4,0x62,0xe3,0xf6,0xa5,0x20,0x00,0x00,0x00 // mulx 8*4(%rbp),%rbx,%r12 movq %rdx,%rax movq %r8,%rdx adcxq %rbx,%r11 adoxq %r13,%r12 - mulxq 32+8(%rsp),%rbx,%rdx + mulxq 32+8(%rsp),%rbx,%rdx // %rdx discarded movq %rax,%rdx - movq %rax,64+48+8(%rsp,%rcx,8) + movq %rax,64+48+8(%rsp,%rcx,8) // put aside n0*a[i] mulxq 40(%rbp),%rax,%r13 adcxq %rax,%r12 @@ -3222,18 +3235,18 @@ L$sqrx8x_reduce: mulxq 56(%rbp),%rax,%r15 movq %rbx,%rdx adcxq %rax,%r14 - adoxq %rsi,%r15 - adcxq %rsi,%r15 + adoxq %rsi,%r15 // %rsi is 0 + adcxq %rsi,%r15 // cf=0 .byte 0x67,0x67,0x67 - incq %rcx + incq %rcx // of=0 jnz L$sqrx8x_reduce - movq %rsi,%rax - cmpq 0+8(%rsp),%rbp + movq %rsi,%rax // xor %rax,%rax + cmpq 0+8(%rsp),%rbp // end of n[]? jae L$sqrx8x_no_tail - movq 48+8(%rsp),%rdx + movq 48+8(%rsp),%rdx // pull n0*a[0] addq 0(%rdi),%r8 leaq 64(%rbp),%rbp movq $-8,%rcx @@ -3245,9 +3258,9 @@ L$sqrx8x_reduce: adcq 48(%rdi),%r14 adcq 56(%rdi),%r15 leaq 64(%rdi),%rdi - sbbq %rax,%rax + sbbq %rax,%rax // top carry - xorq %rsi,%rsi + xorq %rsi,%rsi // of=0, cf=0 movq %rax,16+8(%rsp) jmp L$sqrx8x_tail @@ -3270,7 +3283,7 @@ L$sqrx8x_tail: adcxq %rax,%r10 adoxq %r12,%r11 -.byte 0xc4,0x62,0xfb,0xf6,0xa5,0x20,0x00,0x00,0x00 +.byte 0xc4,0x62,0xfb,0xf6,0xa5,0x20,0x00,0x00,0x00 // mulx 8*4(%rbp),%rax,%r12 adcxq %rax,%r11 adoxq %r13,%r12 @@ -3283,21 +3296,21 @@ L$sqrx8x_tail: adoxq %r15,%r14 mulxq 56(%rbp),%rax,%r15 - movq 72+48+8(%rsp,%rcx,8),%rdx + movq 72+48+8(%rsp,%rcx,8),%rdx // pull n0*a[i] adcxq %rax,%r14 adoxq %rsi,%r15 - movq %rbx,(%rdi,%rcx,8) + movq %rbx,(%rdi,%rcx,8) // save result movq %r8,%rbx - adcxq %rsi,%r15 + adcxq %rsi,%r15 // cf=0 - incq %rcx + incq %rcx // of=0 jnz L$sqrx8x_tail - cmpq 0+8(%rsp),%rbp - jae L$sqrx8x_tail_done + cmpq 0+8(%rsp),%rbp // end of n[]? + jae L$sqrx8x_tail_done // break out of loop - subq 16+8(%rsp),%rsi - movq 48+8(%rsp),%rdx + subq 16+8(%rsp),%rsi // mov 16(%rsp),%cf + movq 48+8(%rsp),%rdx // pull n0*a[0] leaq 64(%rbp),%rbp adcq 0(%rdi),%r8 adcq 8(%rdi),%r9 @@ -3309,16 +3322,16 @@ L$sqrx8x_tail: adcq 56(%rdi),%r15 leaq 64(%rdi),%rdi sbbq %rax,%rax - subq $8,%rcx + subq $8,%rcx // mov $-8,%rcx - xorq %rsi,%rsi + xorq %rsi,%rsi // of=0, cf=0 movq %rax,16+8(%rsp) jmp L$sqrx8x_tail .p2align 5 L$sqrx8x_tail_done: xorq %rax,%rax - addq 24+8(%rsp),%r8 + addq 24+8(%rsp),%r8 // can this overflow? adcq $0,%r9 adcq $0,%r10 adcq $0,%r11 @@ -3328,26 +3341,26 @@ L$sqrx8x_tail_done: adcq $0,%r15 adcq $0,%rax - subq 16+8(%rsp),%rsi -L$sqrx8x_no_tail: + subq 16+8(%rsp),%rsi // mov 16(%rsp),%cf +L$sqrx8x_no_tail: // %cf is 0 if jumped here adcq 0(%rdi),%r8 movq %xmm3,%rcx adcq 8(%rdi),%r9 movq 56(%rbp),%rsi - movq %xmm2,%rbp + movq %xmm2,%rbp // restore %rbp adcq 16(%rdi),%r10 adcq 24(%rdi),%r11 adcq 32(%rdi),%r12 adcq 40(%rdi),%r13 adcq 48(%rdi),%r14 adcq 56(%rdi),%r15 - adcq $0,%rax + adcq $0,%rax // top-most carry - movq 32+8(%rsp),%rbx - movq 64(%rdi,%rcx,1),%rdx + movq 32+8(%rsp),%rbx // n0 + movq 64(%rdi,%rcx,1),%rdx // modulo-scheduled "%r8" - movq %r8,0(%rdi) - leaq 64(%rdi),%r8 + movq %r8,0(%rdi) // store top 512 bits + leaq 64(%rdi),%r8 // borrow %r8 movq %r9,8(%rdi) movq %r10,16(%rdi) movq %r11,24(%rdi) @@ -3356,25 +3369,26 @@ L$sqrx8x_no_tail: movq %r14,48(%rdi) movq %r15,56(%rdi) - leaq 64(%rdi,%rcx,1),%rdi - cmpq 8+8(%rsp),%r8 + leaq 64(%rdi,%rcx,1),%rdi // start of current t[] window + cmpq 8+8(%rsp),%r8 // end of t[]? jb L$sqrx8x_reduction_loop ret .p2align 5 +.alt_entry __bn_postx4x_internal __bn_postx4x_internal: movq 0(%rbp),%r12 - movq %rcx,%r10 - movq %rcx,%r9 + movq %rcx,%r10 // -%r9 + movq %rcx,%r9 // -%r9 negq %rax sarq $3+2,%rcx - - movq %xmm1,%rdx - movq %xmm1,%rsi - decq %r12 +// lea 48+8(%rsp,%r9),%rdi + movq %xmm1,%rdx // restore %rdx + movq %xmm1,%rsi // prepare for back-to-back call + decq %r12 // so that after 'not' we get -n[0] movq 8(%rbp),%r13 xorq %r8,%r8 movq 16(%rbp),%r14 @@ -3394,7 +3408,7 @@ L$sqrx4x_sub_entry: andnq %rax,%r14,%r14 andnq %rax,%r15,%r15 - negq %r8 + negq %r8 // mov %r8,%cf adcq 0(%rdi),%r12 adcq 8(%rdi),%r13 adcq 16(%rdi),%r14 @@ -3402,7 +3416,7 @@ L$sqrx4x_sub_entry: movq %r12,0(%rdx) leaq 32(%rdi),%rdi movq %r13,8(%rdx) - sbbq %r8,%r8 + sbbq %r8,%r8 // mov %cf,%r8 movq %r14,16(%rdx) movq %r15,24(%rdx) leaq 32(%rdx),%rdx @@ -3410,7 +3424,7 @@ L$sqrx4x_sub_entry: incq %rcx jnz L$sqrx4x_sub - negq %r9 + negq %r9 // restore %r9 ret @@ -3419,19 +3433,20 @@ L$sqrx4x_sub_entry: .private_extern _bn_scatter5 .p2align 4 +.alt_entry _bn_scatter5 _bn_scatter5: _CET_ENDBR cmpl $0,%esi jz L$scatter_epilogue - - - - - - - +// %rdx stores 32 entries, t0 through t31. Each entry has %esi words. +// They are interleaved in memory as follows: +// +// t0[0] t1[0] t2[0] ... t31[0] +// t0[1] t1[1] t2[1] ... t31[1] +// ... +// t0[%esi-1] t1[%esi-1] t2[%esi-1] ... t31[%esi-1] leaq (%rdx,%rcx,8),%rdx L$scatter: @@ -3450,99 +3465,100 @@ L$scatter_epilogue: .private_extern _bn_gather5 .p2align 5 +.alt_entry _bn_gather5 _bn_gather5: -L$SEH_begin_bn_gather5: +L$SEH_begin_bn_gather5: // Win64 thing, but harmless in other cases _CET_ENDBR +// I can't trust assembler to use specific encoding:-( +.byte 0x4c,0x8d,0x14,0x24 // lea (%rsp),%r10 -.byte 0x4c,0x8d,0x14,0x24 - -.byte 0x48,0x81,0xec,0x08,0x01,0x00,0x00 +.byte 0x48,0x81,0xec,0x08,0x01,0x00,0x00 // sub $0x108,%rsp leaq L$inc(%rip),%rax - andq $-16,%rsp + andq $-16,%rsp // shouldn't be formally required movd %ecx,%xmm5 - movdqa 0(%rax),%xmm0 - movdqa 16(%rax),%xmm1 - leaq 128(%rdx),%r11 - leaq 128(%rsp),%rax + movdqa 0(%rax),%xmm0 // 00000001000000010000000000000000 + movdqa 16(%rax),%xmm1 // 00000002000000020000000200000002 + leaq 128(%rdx),%r11 // size optimization + leaq 128(%rsp),%rax // size optimization - pshufd $0,%xmm5,%xmm5 + pshufd $0,%xmm5,%xmm5 // broadcast %ecx movdqa %xmm1,%xmm4 movdqa %xmm1,%xmm2 paddd %xmm0,%xmm1 - pcmpeqd %xmm5,%xmm0 + pcmpeqd %xmm5,%xmm0 // compare to 1,0 movdqa %xmm4,%xmm3 paddd %xmm1,%xmm2 - pcmpeqd %xmm5,%xmm1 + pcmpeqd %xmm5,%xmm1 // compare to 3,2 movdqa %xmm0,-128(%rax) movdqa %xmm4,%xmm0 paddd %xmm2,%xmm3 - pcmpeqd %xmm5,%xmm2 + pcmpeqd %xmm5,%xmm2 // compare to 5,4 movdqa %xmm1,-112(%rax) movdqa %xmm4,%xmm1 paddd %xmm3,%xmm0 - pcmpeqd %xmm5,%xmm3 + pcmpeqd %xmm5,%xmm3 // compare to 7,6 movdqa %xmm2,-96(%rax) movdqa %xmm4,%xmm2 paddd %xmm0,%xmm1 - pcmpeqd %xmm5,%xmm0 + pcmpeqd %xmm5,%xmm0 // compare to 1,0 movdqa %xmm3,-80(%rax) movdqa %xmm4,%xmm3 paddd %xmm1,%xmm2 - pcmpeqd %xmm5,%xmm1 + pcmpeqd %xmm5,%xmm1 // compare to 3,2 movdqa %xmm0,-64(%rax) movdqa %xmm4,%xmm0 paddd %xmm2,%xmm3 - pcmpeqd %xmm5,%xmm2 + pcmpeqd %xmm5,%xmm2 // compare to 5,4 movdqa %xmm1,-48(%rax) movdqa %xmm4,%xmm1 paddd %xmm3,%xmm0 - pcmpeqd %xmm5,%xmm3 + pcmpeqd %xmm5,%xmm3 // compare to 7,6 movdqa %xmm2,-32(%rax) movdqa %xmm4,%xmm2 paddd %xmm0,%xmm1 - pcmpeqd %xmm5,%xmm0 + pcmpeqd %xmm5,%xmm0 // compare to 1,0 movdqa %xmm3,-16(%rax) movdqa %xmm4,%xmm3 paddd %xmm1,%xmm2 - pcmpeqd %xmm5,%xmm1 + pcmpeqd %xmm5,%xmm1 // compare to 3,2 movdqa %xmm0,0(%rax) movdqa %xmm4,%xmm0 paddd %xmm2,%xmm3 - pcmpeqd %xmm5,%xmm2 + pcmpeqd %xmm5,%xmm2 // compare to 5,4 movdqa %xmm1,16(%rax) movdqa %xmm4,%xmm1 paddd %xmm3,%xmm0 - pcmpeqd %xmm5,%xmm3 + pcmpeqd %xmm5,%xmm3 // compare to 7,6 movdqa %xmm2,32(%rax) movdqa %xmm4,%xmm2 paddd %xmm0,%xmm1 - pcmpeqd %xmm5,%xmm0 + pcmpeqd %xmm5,%xmm0 // compare to 1,0 movdqa %xmm3,48(%rax) movdqa %xmm4,%xmm3 paddd %xmm1,%xmm2 - pcmpeqd %xmm5,%xmm1 + pcmpeqd %xmm5,%xmm1 // compare to 3,2 movdqa %xmm0,64(%rax) movdqa %xmm4,%xmm0 paddd %xmm2,%xmm3 - pcmpeqd %xmm5,%xmm2 + pcmpeqd %xmm5,%xmm2 // compare to 5,4 movdqa %xmm1,80(%rax) movdqa %xmm4,%xmm1 paddd %xmm3,%xmm0 - pcmpeqd %xmm5,%xmm3 + pcmpeqd %xmm5,%xmm3 // compare to 7,6 movdqa %xmm2,96(%rax) movdqa %xmm4,%xmm2 movdqa %xmm3,112(%rax) @@ -3602,10 +3618,10 @@ L$gather: por %xmm3,%xmm5 por %xmm5,%xmm4 leaq 256(%r11),%r11 - - pshufd $0x4e,%xmm4,%xmm0 +// Combine the upper and lower halves of %xmm0. + pshufd $0x4e,%xmm4,%xmm0 // Swap upper and lower halves. por %xmm4,%xmm0 - movq %xmm0,(%rdi) + movq %xmm0,(%rdi) // m0=bp[0] leaq 8(%rdi),%rdi subl $1,%esi jnz L$gather @@ -3617,7 +3633,9 @@ L$SEH_end_bn_gather5: .section __DATA,__const + .p2align 6 +mont5_increments: L$inc: .long 0,0, 1,1 .long 2,2, 2,2 diff --git a/third_party/boringssl/gen/bcm/x86_64-mont5-linux.S b/third_party/boringssl/gen/bcm/x86_64-mont5-linux.S index a147041df..67fdfe03e 100644 --- a/third_party/boringssl/gen/bcm/x86_64-mont5-linux.S +++ b/third_party/boringssl/gen/bcm/x86_64-mont5-linux.S @@ -13,12 +13,12 @@ bn_mul_mont_gather5_nohw: .cfi_startproc _CET_ENDBR - - +// num is declared as an int, a 32-bit parameter, so the upper half is +// undefined. Zero the upper half to normalize it. movl %r9d,%r9d movq %rsp,%rax .cfi_def_cfa_register %rax - movd 8(%rsp),%xmm5 + movd 8(%rsp),%xmm5 // load 7th argument pushq %rbx .cfi_offset %rbx,-16 pushq %rbp @@ -34,18 +34,18 @@ _CET_ENDBR negq %r9 movq %rsp,%r11 - leaq -280(%rsp,%r9,8),%r10 - negq %r9 - andq $-1024,%r10 - - - - - - - - - + leaq -280(%rsp,%r9,8),%r10 // future alloca(8*(num+2)+256+8) + negq %r9 // restore %r9 + andq $-1024,%r10 // minimize TLB usage + +// An OS-agnostic version of __chkstk. +// +// Some OSes (Windows) insist on stack being "wired" to +// physical memory in strictly sequential manner, i.e. if stack +// allocation spans two pages, then reference to farmost one can +// be punishable by SEGV. But page walking can do good even on +// other OSes, because it guarantees that villain thread hits +// the guard page before it can make damage to innocent one... subq %r10,%r11 andq $-4096,%r11 leaq (%r10,%r11,1),%rsp @@ -62,35 +62,35 @@ _CET_ENDBR .Lmul_page_walk_done: leaq .Linc(%rip),%r10 - movq %rax,8(%rsp,%r9,8) + movq %rax,8(%rsp,%r9,8) // tp[num+1]=%rsp .cfi_escape 0x0f,0x0a,0x77,0x08,0x79,0x00,0x38,0x1e,0x22,0x06,0x23,0x08 .Lmul_body: - leaq 128(%rdx),%r12 - movdqa 0(%r10),%xmm0 - movdqa 16(%r10),%xmm1 - leaq 24-112(%rsp,%r9,8),%r10 + leaq 128(%rdx),%r12 // reassign %rdx (+size optimization) + movdqa 0(%r10),%xmm0 // 00000001000000010000000000000000 + movdqa 16(%r10),%xmm1 // 00000002000000020000000200000002 + leaq 24-112(%rsp,%r9,8),%r10 // place the mask after tp[num+3] (+ICache optimization) andq $-16,%r10 - pshufd $0,%xmm5,%xmm5 + pshufd $0,%xmm5,%xmm5 // broadcast index movdqa %xmm1,%xmm4 movdqa %xmm1,%xmm2 paddd %xmm0,%xmm1 - pcmpeqd %xmm5,%xmm0 + pcmpeqd %xmm5,%xmm0 // compare to 1,0 .byte 0x67 movdqa %xmm4,%xmm3 paddd %xmm1,%xmm2 - pcmpeqd %xmm5,%xmm1 + pcmpeqd %xmm5,%xmm1 // compare to 3,2 movdqa %xmm0,112(%r10) movdqa %xmm4,%xmm0 paddd %xmm2,%xmm3 - pcmpeqd %xmm5,%xmm2 + pcmpeqd %xmm5,%xmm2 // compare to 5,4 movdqa %xmm1,128(%r10) movdqa %xmm4,%xmm1 paddd %xmm3,%xmm0 - pcmpeqd %xmm5,%xmm3 + pcmpeqd %xmm5,%xmm3 // compare to 7,6 movdqa %xmm2,144(%r10) movdqa %xmm4,%xmm2 @@ -99,17 +99,17 @@ _CET_ENDBR movdqa %xmm3,160(%r10) movdqa %xmm4,%xmm3 paddd %xmm1,%xmm2 - pcmpeqd %xmm5,%xmm1 + pcmpeqd %xmm5,%xmm1 // compare to 3,2 movdqa %xmm0,176(%r10) movdqa %xmm4,%xmm0 paddd %xmm2,%xmm3 - pcmpeqd %xmm5,%xmm2 + pcmpeqd %xmm5,%xmm2 // compare to 5,4 movdqa %xmm1,192(%r10) movdqa %xmm4,%xmm1 paddd %xmm3,%xmm0 - pcmpeqd %xmm5,%xmm3 + pcmpeqd %xmm5,%xmm3 // compare to 7,6 movdqa %xmm2,208(%r10) movdqa %xmm4,%xmm2 @@ -118,17 +118,17 @@ _CET_ENDBR movdqa %xmm3,224(%r10) movdqa %xmm4,%xmm3 paddd %xmm1,%xmm2 - pcmpeqd %xmm5,%xmm1 + pcmpeqd %xmm5,%xmm1 // compare to 3,2 movdqa %xmm0,240(%r10) movdqa %xmm4,%xmm0 paddd %xmm2,%xmm3 - pcmpeqd %xmm5,%xmm2 + pcmpeqd %xmm5,%xmm2 // compare to 5,4 movdqa %xmm1,256(%r10) movdqa %xmm4,%xmm1 paddd %xmm3,%xmm0 - pcmpeqd %xmm5,%xmm3 + pcmpeqd %xmm5,%xmm3 // compare to 7,6 movdqa %xmm2,272(%r10) movdqa %xmm4,%xmm2 @@ -147,7 +147,7 @@ _CET_ENDBR pcmpeqd %xmm5,%xmm3 movdqa %xmm2,336(%r10) - pand 64(%r12),%xmm0 + pand 64(%r12),%xmm0 // while it's still in register pand 80(%r12),%xmm1 pand 96(%r12),%xmm2 @@ -192,33 +192,33 @@ _CET_ENDBR por %xmm2,%xmm0 por %xmm3,%xmm1 por %xmm1,%xmm0 - - pshufd $0x4e,%xmm0,%xmm1 +// Combine the upper and lower halves of %xmm0. + pshufd $0x4e,%xmm0,%xmm1 // Swap upper and lower halves. por %xmm1,%xmm0 leaq 256(%r12),%r12 - movq %xmm0,%rbx + movq %xmm0,%rbx // m0=bp[0] - movq (%r8),%r8 + movq (%r8),%r8 // pull n0[0] value movq (%rsi),%rax - xorq %r14,%r14 - xorq %r15,%r15 + xorq %r14,%r14 // i=0 + xorq %r15,%r15 // j=0 movq %r8,%rbp - mulq %rbx + mulq %rbx // ap[0]*bp[0] movq %rax,%r10 movq (%rcx),%rax - imulq %r10,%rbp + imulq %r10,%rbp // "tp[0]"*n0 movq %rdx,%r11 - mulq %rbp - addq %rax,%r10 + mulq %rbp // np[0]*m1 + addq %rax,%r10 // discarded movq 8(%rsi),%rax adcq $0,%rdx movq %rdx,%r13 - leaq 1(%r15),%r15 + leaq 1(%r15),%r15 // j++ jmp .L1st_enter .align 16 @@ -226,30 +226,30 @@ _CET_ENDBR addq %rax,%r13 movq (%rsi,%r15,8),%rax adcq $0,%rdx - addq %r11,%r13 + addq %r11,%r13 // np[j]*m1+ap[j]*bp[0] movq %r10,%r11 adcq $0,%rdx - movq %r13,-16(%rsp,%r15,8) + movq %r13,-16(%rsp,%r15,8) // tp[j-1] movq %rdx,%r13 .L1st_enter: - mulq %rbx + mulq %rbx // ap[j]*bp[0] addq %rax,%r11 movq (%rcx,%r15,8),%rax adcq $0,%rdx - leaq 1(%r15),%r15 + leaq 1(%r15),%r15 // j++ movq %rdx,%r10 - mulq %rbp + mulq %rbp // np[j]*m1 cmpq %r9,%r15 - jne .L1st - + jne .L1st // note that upon exit %r15==%r9, so +// they can be used interchangeably addq %rax,%r13 adcq $0,%rdx - addq %r11,%r13 + addq %r11,%r13 // np[j]*m1+ap[j]*bp[0] adcq $0,%rdx - movq %r13,-16(%rsp,%r9,8) + movq %r13,-16(%rsp,%r9,8) // tp[num-1] movq %rdx,%r13 movq %r10,%r11 @@ -257,13 +257,13 @@ _CET_ENDBR addq %r11,%r13 adcq $0,%rdx movq %r13,-8(%rsp,%r9,8) - movq %rdx,(%rsp,%r9,8) + movq %rdx,(%rsp,%r9,8) // store upmost overflow bit - leaq 1(%r14),%r14 + leaq 1(%r14),%r14 // i++ jmp .Louter .align 16 .Louter: - leaq 24+128(%rsp,%r9,8),%rdx + leaq 24+128(%rsp,%r9,8),%rdx // where 256-byte mask is (+size optimization) andq $-16,%rdx pxor %xmm4,%xmm4 pxor %xmm5,%xmm5 @@ -316,34 +316,34 @@ _CET_ENDBR por %xmm2,%xmm4 por %xmm3,%xmm5 por %xmm5,%xmm4 - - pshufd $0x4e,%xmm4,%xmm0 +// Combine the upper and lower halves of %xmm4 as %xmm0. + pshufd $0x4e,%xmm4,%xmm0 // Swap upper and lower halves. por %xmm4,%xmm0 leaq 256(%r12),%r12 - movq (%rsi),%rax - movq %xmm0,%rbx + movq (%rsi),%rax // ap[0] + movq %xmm0,%rbx // m0=bp[i] - xorq %r15,%r15 + xorq %r15,%r15 // j=0 movq %r8,%rbp movq (%rsp),%r10 - mulq %rbx - addq %rax,%r10 + mulq %rbx // ap[0]*bp[i] + addq %rax,%r10 // ap[0]*bp[i]+tp[0] movq (%rcx),%rax adcq $0,%rdx - imulq %r10,%rbp + imulq %r10,%rbp // tp[0]*n0 movq %rdx,%r11 - mulq %rbp - addq %rax,%r10 + mulq %rbp // np[0]*m1 + addq %rax,%r10 // discarded movq 8(%rsi),%rax adcq $0,%rdx - movq 8(%rsp),%r10 + movq 8(%rsp),%r10 // tp[1] movq %rdx,%r13 - leaq 1(%r15),%r15 + leaq 1(%r15),%r15 // j++ jmp .Linner_enter .align 16 @@ -351,78 +351,78 @@ _CET_ENDBR addq %rax,%r13 movq (%rsi,%r15,8),%rax adcq $0,%rdx - addq %r10,%r13 + addq %r10,%r13 // np[j]*m1+ap[j]*bp[i]+tp[j] movq (%rsp,%r15,8),%r10 adcq $0,%rdx - movq %r13,-16(%rsp,%r15,8) + movq %r13,-16(%rsp,%r15,8) // tp[j-1] movq %rdx,%r13 .Linner_enter: - mulq %rbx + mulq %rbx // ap[j]*bp[i] addq %rax,%r11 movq (%rcx,%r15,8),%rax adcq $0,%rdx - addq %r11,%r10 + addq %r11,%r10 // ap[j]*bp[i]+tp[j] movq %rdx,%r11 adcq $0,%r11 - leaq 1(%r15),%r15 + leaq 1(%r15),%r15 // j++ - mulq %rbp + mulq %rbp // np[j]*m1 cmpq %r9,%r15 - jne .Linner - + jne .Linner // note that upon exit %r15==%r9, so +// they can be used interchangeably addq %rax,%r13 adcq $0,%rdx - addq %r10,%r13 + addq %r10,%r13 // np[j]*m1+ap[j]*bp[i]+tp[j] movq (%rsp,%r9,8),%r10 adcq $0,%rdx - movq %r13,-16(%rsp,%r9,8) + movq %r13,-16(%rsp,%r9,8) // tp[num-1] movq %rdx,%r13 xorq %rdx,%rdx addq %r11,%r13 adcq $0,%rdx - addq %r10,%r13 + addq %r10,%r13 // pull upmost overflow bit adcq $0,%rdx movq %r13,-8(%rsp,%r9,8) - movq %rdx,(%rsp,%r9,8) + movq %rdx,(%rsp,%r9,8) // store upmost overflow bit - leaq 1(%r14),%r14 + leaq 1(%r14),%r14 // i++ cmpq %r9,%r14 jb .Louter - xorq %r14,%r14 - movq (%rsp),%rax - leaq (%rsp),%rsi - movq %r9,%r15 + xorq %r14,%r14 // i=0 and clear CF + movq (%rsp),%rax // tp[0] + leaq (%rsp),%rsi // borrow ap for tp + movq %r9,%r15 // j=num jmp .Lsub .align 16 .Lsub: sbbq (%rcx,%r14,8),%rax - movq %rax,(%rdi,%r14,8) - movq 8(%rsi,%r14,8),%rax - leaq 1(%r14),%r14 - decq %r15 + movq %rax,(%rdi,%r14,8) // rp[i]=tp[i]-np[i] + movq 8(%rsi,%r14,8),%rax // tp[i+1] + leaq 1(%r14),%r14 // i++ + decq %r15 // doesn't affect CF jnz .Lsub - sbbq $0,%rax + sbbq $0,%rax // handle upmost overflow bit movq $-1,%rbx xorq %rax,%rbx xorq %r14,%r14 - movq %r9,%r15 + movq %r9,%r15 // j=num -.Lcopy: +.Lcopy: // conditional copy movq (%rdi,%r14,8),%rcx movq (%rsp,%r14,8),%rdx andq %rbx,%rcx andq %rax,%rdx - movq %r14,(%rsp,%r14,8) + movq %r14,(%rsp,%r14,8) // zap temporary vector orq %rcx,%rdx - movq %rdx,(%rdi,%r14,8) + movq %rdx,(%rdi,%r14,8) // rp[i]=tp[i] leaq 1(%r14),%r14 subq $1,%r15 jnz .Lcopy - movq 8(%rsp,%r9,8),%rsi + movq 8(%rsp,%r9,8),%rsi // restore %rsp .cfi_def_cfa %rsi,8 movq $1,%rax @@ -469,36 +469,36 @@ _CET_ENDBR .Lmul4x_prologue: .byte 0x67 - - - - shll $3,%r9d - leaq (%r9,%r9,2),%r10 - negq %r9 - - - - - - - - - - +// num is declared as an int, a 32-bit parameter, so the upper half is +// undefined. It is important that this write to %r9, which zeros the +// upper half, predates the first access. + shll $3,%r9d // convert %r9 to bytes + leaq (%r9,%r9,2),%r10 // 3*%r9 in bytes + negq %r9 // -%r9 + +// ############################################################# +// Ensure that stack frame doesn't alias with +3*%r9 +// modulo 4096, which covers ret[num], am[num] and n[num] +// (see bn_exp.c). This is done to allow memory disambiguation +// logic do its magic. [Extra [num] is allocated in order +// to align with bn_power5's frame, which is cleansed after +// completing exponentiation. Extra 256 bytes is for power mask +// calculated from 7th argument, the index.] +// leaq -320(%rsp,%r9,2),%r11 movq %rsp,%rbp subq %rdi,%r11 andq $4095,%r11 cmpq %r11,%r10 jb .Lmul4xsp_alt - subq %r11,%rbp - leaq -320(%rbp,%r9,2),%rbp + subq %r11,%rbp // align with %rdi + leaq -320(%rbp,%r9,2),%rbp // future alloca(frame+2*num*8+256) jmp .Lmul4xsp_done .align 32 .Lmul4xsp_alt: leaq 4096-320(,%r9,2),%r10 - leaq -320(%rbp,%r9,2),%rbp + leaq -320(%rbp,%r9,2),%rbp // future alloca(frame+2*num*8+256) subq %r10,%r11 movq $0,%r10 cmovcq %r10,%r11 @@ -529,7 +529,7 @@ _CET_ENDBR call mul4x_internal - movq 40(%rsp),%rsi + movq 40(%rsp),%rsi // restore %rsp .cfi_def_cfa %rsi,8 movq $1,%rax @@ -556,36 +556,36 @@ _CET_ENDBR .align 32 mul4x_internal: .cfi_startproc - shlq $5,%r9 - movd 8(%rax),%xmm5 + shlq $5,%r9 // %r9 was in bytes + movd 8(%rax),%xmm5 // load 7th argument, index leaq .Linc(%rip),%rax - leaq 128(%rdx,%r9,1),%r13 - shrq $5,%r9 - movdqa 0(%rax),%xmm0 - movdqa 16(%rax),%xmm1 - leaq 88-112(%rsp,%r9,1),%r10 - leaq 128(%rdx),%r12 - - pshufd $0,%xmm5,%xmm5 + leaq 128(%rdx,%r9,1),%r13 // end of powers table (+size optimization) + shrq $5,%r9 // restore %r9 + movdqa 0(%rax),%xmm0 // 00000001000000010000000000000000 + movdqa 16(%rax),%xmm1 // 00000002000000020000000200000002 + leaq 88-112(%rsp,%r9,1),%r10 // place the mask after tp[num+1] (+ICache optimization) + leaq 128(%rdx),%r12 // size optimization + + pshufd $0,%xmm5,%xmm5 // broadcast index movdqa %xmm1,%xmm4 .byte 0x67,0x67 movdqa %xmm1,%xmm2 paddd %xmm0,%xmm1 - pcmpeqd %xmm5,%xmm0 + pcmpeqd %xmm5,%xmm0 // compare to 1,0 .byte 0x67 movdqa %xmm4,%xmm3 paddd %xmm1,%xmm2 - pcmpeqd %xmm5,%xmm1 + pcmpeqd %xmm5,%xmm1 // compare to 3,2 movdqa %xmm0,112(%r10) movdqa %xmm4,%xmm0 paddd %xmm2,%xmm3 - pcmpeqd %xmm5,%xmm2 + pcmpeqd %xmm5,%xmm2 // compare to 5,4 movdqa %xmm1,128(%r10) movdqa %xmm4,%xmm1 paddd %xmm3,%xmm0 - pcmpeqd %xmm5,%xmm3 + pcmpeqd %xmm5,%xmm3 // compare to 7,6 movdqa %xmm2,144(%r10) movdqa %xmm4,%xmm2 @@ -594,17 +594,17 @@ mul4x_internal: movdqa %xmm3,160(%r10) movdqa %xmm4,%xmm3 paddd %xmm1,%xmm2 - pcmpeqd %xmm5,%xmm1 + pcmpeqd %xmm5,%xmm1 // compare to 3,2 movdqa %xmm0,176(%r10) movdqa %xmm4,%xmm0 paddd %xmm2,%xmm3 - pcmpeqd %xmm5,%xmm2 + pcmpeqd %xmm5,%xmm2 // compare to 5,4 movdqa %xmm1,192(%r10) movdqa %xmm4,%xmm1 paddd %xmm3,%xmm0 - pcmpeqd %xmm5,%xmm3 + pcmpeqd %xmm5,%xmm3 // compare to 7,6 movdqa %xmm2,208(%r10) movdqa %xmm4,%xmm2 @@ -613,17 +613,17 @@ mul4x_internal: movdqa %xmm3,224(%r10) movdqa %xmm4,%xmm3 paddd %xmm1,%xmm2 - pcmpeqd %xmm5,%xmm1 + pcmpeqd %xmm5,%xmm1 // compare to 3,2 movdqa %xmm0,240(%r10) movdqa %xmm4,%xmm0 paddd %xmm2,%xmm3 - pcmpeqd %xmm5,%xmm2 + pcmpeqd %xmm5,%xmm2 // compare to 5,4 movdqa %xmm1,256(%r10) movdqa %xmm4,%xmm1 paddd %xmm3,%xmm0 - pcmpeqd %xmm5,%xmm3 + pcmpeqd %xmm5,%xmm3 // compare to 7,6 movdqa %xmm2,272(%r10) movdqa %xmm4,%xmm2 @@ -642,7 +642,7 @@ mul4x_internal: pcmpeqd %xmm5,%xmm3 movdqa %xmm2,336(%r10) - pand 64(%r12),%xmm0 + pand 64(%r12),%xmm0 // while it's still in register pand 80(%r12),%xmm1 pand 96(%r12),%xmm2 @@ -687,31 +687,31 @@ mul4x_internal: por %xmm2,%xmm0 por %xmm3,%xmm1 por %xmm1,%xmm0 - - pshufd $0x4e,%xmm0,%xmm1 +// Combine the upper and lower halves of %xmm0. + pshufd $0x4e,%xmm0,%xmm1 // Swap upper and lower halves. por %xmm1,%xmm0 leaq 256(%r12),%r12 - movq %xmm0,%rbx + movq %xmm0,%rbx // m0=bp[0] - movq %r13,16+8(%rsp) - movq %rdi,56+8(%rsp) + movq %r13,16+8(%rsp) // save end of b[num] + movq %rdi,56+8(%rsp) // save %rdi - movq (%r8),%r8 + movq (%r8),%r8 // pull n0[0] value movq (%rsi),%rax - leaq (%rsi,%r9,1),%rsi + leaq (%rsi,%r9,1),%rsi // end of a[num] negq %r9 movq %r8,%rbp - mulq %rbx + mulq %rbx // ap[0]*bp[0] movq %rax,%r10 movq (%rcx),%rax - imulq %r10,%rbp + imulq %r10,%rbp // "tp[0]"*n0 leaq 64+8(%rsp),%r14 movq %rdx,%r11 - mulq %rbp - addq %rax,%r10 + mulq %rbp // np[0]*m1 + addq %rax,%r10 // discarded movq 8(%rsi,%r9,1),%rax adcq $0,%rdx movq %rdx,%rdi @@ -727,7 +727,7 @@ mul4x_internal: movq 16(%rsi,%r9,1),%rax adcq $0,%rdx addq %r11,%rdi - leaq 32(%r9),%r15 + leaq 32(%r9),%r15 // j=4 leaq 32(%rcx),%rcx adcq $0,%rdx movq %rdi,(%r14) @@ -736,103 +736,103 @@ mul4x_internal: .align 32 .L1st4x: - mulq %rbx + mulq %rbx // ap[j]*bp[0] addq %rax,%r10 movq -16(%rcx),%rax leaq 32(%r14),%r14 adcq $0,%rdx movq %rdx,%r11 - mulq %rbp + mulq %rbp // np[j]*m1 addq %rax,%r13 movq -8(%rsi,%r15,1),%rax adcq $0,%rdx - addq %r10,%r13 + addq %r10,%r13 // np[j]*m1+ap[j]*bp[0] adcq $0,%rdx - movq %r13,-24(%r14) + movq %r13,-24(%r14) // tp[j-1] movq %rdx,%rdi - mulq %rbx + mulq %rbx // ap[j]*bp[0] addq %rax,%r11 movq -8(%rcx),%rax adcq $0,%rdx movq %rdx,%r10 - mulq %rbp + mulq %rbp // np[j]*m1 addq %rax,%rdi movq (%rsi,%r15,1),%rax adcq $0,%rdx - addq %r11,%rdi + addq %r11,%rdi // np[j]*m1+ap[j]*bp[0] adcq $0,%rdx - movq %rdi,-16(%r14) + movq %rdi,-16(%r14) // tp[j-1] movq %rdx,%r13 - mulq %rbx + mulq %rbx // ap[j]*bp[0] addq %rax,%r10 movq 0(%rcx),%rax adcq $0,%rdx movq %rdx,%r11 - mulq %rbp + mulq %rbp // np[j]*m1 addq %rax,%r13 movq 8(%rsi,%r15,1),%rax adcq $0,%rdx - addq %r10,%r13 + addq %r10,%r13 // np[j]*m1+ap[j]*bp[0] adcq $0,%rdx - movq %r13,-8(%r14) + movq %r13,-8(%r14) // tp[j-1] movq %rdx,%rdi - mulq %rbx + mulq %rbx // ap[j]*bp[0] addq %rax,%r11 movq 8(%rcx),%rax adcq $0,%rdx movq %rdx,%r10 - mulq %rbp + mulq %rbp // np[j]*m1 addq %rax,%rdi movq 16(%rsi,%r15,1),%rax adcq $0,%rdx - addq %r11,%rdi + addq %r11,%rdi // np[j]*m1+ap[j]*bp[0] leaq 32(%rcx),%rcx adcq $0,%rdx - movq %rdi,(%r14) + movq %rdi,(%r14) // tp[j-1] movq %rdx,%r13 - addq $32,%r15 + addq $32,%r15 // j+=4 jnz .L1st4x - mulq %rbx + mulq %rbx // ap[j]*bp[0] addq %rax,%r10 movq -16(%rcx),%rax leaq 32(%r14),%r14 adcq $0,%rdx movq %rdx,%r11 - mulq %rbp + mulq %rbp // np[j]*m1 addq %rax,%r13 movq -8(%rsi),%rax adcq $0,%rdx - addq %r10,%r13 + addq %r10,%r13 // np[j]*m1+ap[j]*bp[0] adcq $0,%rdx - movq %r13,-24(%r14) + movq %r13,-24(%r14) // tp[j-1] movq %rdx,%rdi - mulq %rbx + mulq %rbx // ap[j]*bp[0] addq %rax,%r11 movq -8(%rcx),%rax adcq $0,%rdx movq %rdx,%r10 - mulq %rbp + mulq %rbp // np[j]*m1 addq %rax,%rdi - movq (%rsi,%r9,1),%rax + movq (%rsi,%r9,1),%rax // ap[0] adcq $0,%rdx - addq %r11,%rdi + addq %r11,%rdi // np[j]*m1+ap[j]*bp[0] adcq $0,%rdx - movq %rdi,-16(%r14) + movq %rdi,-16(%r14) // tp[j-1] movq %rdx,%r13 - leaq (%rcx,%r9,1),%rcx + leaq (%rcx,%r9,1),%rcx // rewind %rcx xorq %rdi,%rdi addq %r10,%r13 @@ -843,7 +843,7 @@ mul4x_internal: .align 32 .Louter4x: - leaq 16+128(%r14),%rdx + leaq 16+128(%r14),%rdx // where 256-byte mask is (+size optimization) pxor %xmm4,%xmm4 pxor %xmm5,%xmm5 movdqa -128(%r12),%xmm0 @@ -895,45 +895,45 @@ mul4x_internal: por %xmm2,%xmm4 por %xmm3,%xmm5 por %xmm5,%xmm4 - - pshufd $0x4e,%xmm4,%xmm0 +// Combine the upper and lower halves of %xmm4 as %xmm0. + pshufd $0x4e,%xmm4,%xmm0 // Swap upper and lower halves. por %xmm4,%xmm0 leaq 256(%r12),%r12 - movq %xmm0,%rbx + movq %xmm0,%rbx // m0=bp[i] movq (%r14,%r9,1),%r10 movq %r8,%rbp - mulq %rbx - addq %rax,%r10 + mulq %rbx // ap[0]*bp[i] + addq %rax,%r10 // ap[0]*bp[i]+tp[0] movq (%rcx),%rax adcq $0,%rdx - imulq %r10,%rbp + imulq %r10,%rbp // tp[0]*n0 movq %rdx,%r11 - movq %rdi,(%r14) + movq %rdi,(%r14) // store upmost overflow bit - leaq (%r14,%r9,1),%r14 + leaq (%r14,%r9,1),%r14 // rewind %r14 - mulq %rbp - addq %rax,%r10 + mulq %rbp // np[0]*m1 + addq %rax,%r10 // "%r13", discarded movq 8(%rsi,%r9,1),%rax adcq $0,%rdx movq %rdx,%rdi - mulq %rbx + mulq %rbx // ap[j]*bp[i] addq %rax,%r11 movq 8(%rcx),%rax adcq $0,%rdx - addq 8(%r14),%r11 + addq 8(%r14),%r11 // +tp[1] adcq $0,%rdx movq %rdx,%r10 - mulq %rbp + mulq %rbp // np[j]*m1 addq %rax,%rdi movq 16(%rsi,%r9,1),%rax adcq $0,%rdx - addq %r11,%rdi - leaq 32(%r9),%r15 + addq %r11,%rdi // np[j]*m1+ap[j]*bp[i]+tp[j] + leaq 32(%r9),%r15 // j=4 leaq 32(%rcx),%rcx adcq $0,%rdx movq %rdx,%r13 @@ -941,25 +941,25 @@ mul4x_internal: .align 32 .Linner4x: - mulq %rbx + mulq %rbx // ap[j]*bp[i] addq %rax,%r10 movq -16(%rcx),%rax adcq $0,%rdx - addq 16(%r14),%r10 + addq 16(%r14),%r10 // ap[j]*bp[i]+tp[j] leaq 32(%r14),%r14 adcq $0,%rdx movq %rdx,%r11 - mulq %rbp + mulq %rbp // np[j]*m1 addq %rax,%r13 movq -8(%rsi,%r15,1),%rax adcq $0,%rdx addq %r10,%r13 adcq $0,%rdx - movq %rdi,-32(%r14) + movq %rdi,-32(%r14) // tp[j-1] movq %rdx,%rdi - mulq %rbx + mulq %rbx // ap[j]*bp[i] addq %rax,%r11 movq -8(%rcx),%rax adcq $0,%rdx @@ -967,33 +967,33 @@ mul4x_internal: adcq $0,%rdx movq %rdx,%r10 - mulq %rbp + mulq %rbp // np[j]*m1 addq %rax,%rdi movq (%rsi,%r15,1),%rax adcq $0,%rdx addq %r11,%rdi adcq $0,%rdx - movq %r13,-24(%r14) + movq %r13,-24(%r14) // tp[j-1] movq %rdx,%r13 - mulq %rbx + mulq %rbx // ap[j]*bp[i] addq %rax,%r10 movq 0(%rcx),%rax adcq $0,%rdx - addq (%r14),%r10 + addq (%r14),%r10 // ap[j]*bp[i]+tp[j] adcq $0,%rdx movq %rdx,%r11 - mulq %rbp + mulq %rbp // np[j]*m1 addq %rax,%r13 movq 8(%rsi,%r15,1),%rax adcq $0,%rdx addq %r10,%r13 adcq $0,%rdx - movq %rdi,-16(%r14) + movq %rdi,-16(%r14) // tp[j-1] movq %rdx,%rdi - mulq %rbx + mulq %rbx // ap[j]*bp[i] addq %rax,%r11 movq 8(%rcx),%rax adcq $0,%rdx @@ -1001,38 +1001,38 @@ mul4x_internal: adcq $0,%rdx movq %rdx,%r10 - mulq %rbp + mulq %rbp // np[j]*m1 addq %rax,%rdi movq 16(%rsi,%r15,1),%rax adcq $0,%rdx addq %r11,%rdi leaq 32(%rcx),%rcx adcq $0,%rdx - movq %r13,-8(%r14) + movq %r13,-8(%r14) // tp[j-1] movq %rdx,%r13 - addq $32,%r15 + addq $32,%r15 // j+=4 jnz .Linner4x - mulq %rbx + mulq %rbx // ap[j]*bp[i] addq %rax,%r10 movq -16(%rcx),%rax adcq $0,%rdx - addq 16(%r14),%r10 + addq 16(%r14),%r10 // ap[j]*bp[i]+tp[j] leaq 32(%r14),%r14 adcq $0,%rdx movq %rdx,%r11 - mulq %rbp + mulq %rbp // np[j]*m1 addq %rax,%r13 movq -8(%rsi),%rax adcq $0,%rdx addq %r10,%r13 adcq $0,%rdx - movq %rdi,-32(%r14) + movq %rdi,-32(%r14) // tp[j-1] movq %rdx,%rdi - mulq %rbx + mulq %rbx // ap[j]*bp[i] addq %rax,%r11 movq %rbp,%rax movq -8(%rcx),%rbp @@ -1041,39 +1041,39 @@ mul4x_internal: adcq $0,%rdx movq %rdx,%r10 - mulq %rbp + mulq %rbp // np[j]*m1 addq %rax,%rdi - movq (%rsi,%r9,1),%rax + movq (%rsi,%r9,1),%rax // ap[0] adcq $0,%rdx addq %r11,%rdi adcq $0,%rdx - movq %r13,-24(%r14) + movq %r13,-24(%r14) // tp[j-1] movq %rdx,%r13 - movq %rdi,-16(%r14) - leaq (%rcx,%r9,1),%rcx + movq %rdi,-16(%r14) // tp[j-1] + leaq (%rcx,%r9,1),%rcx // rewind %rcx xorq %rdi,%rdi addq %r10,%r13 adcq $0,%rdi - addq (%r14),%r13 - adcq $0,%rdi + addq (%r14),%r13 // pull upmost overflow bit + adcq $0,%rdi // upmost overflow bit movq %r13,-8(%r14) cmpq 16+8(%rsp),%r12 jb .Louter4x xorq %rax,%rax - subq %r13,%rbp - adcq %r15,%r15 + subq %r13,%rbp // compare top-most words + adcq %r15,%r15 // %r15 is zero orq %r15,%rdi - subq %rdi,%rax - leaq (%r14,%r9,1),%rbx + subq %rdi,%rax // %rax=-%rdi + leaq (%r14,%r9,1),%rbx // tptr in .sqr4x_sub movq (%rcx),%r12 - leaq (%rcx),%rbp + leaq (%rcx),%rbp // nptr in .sqr4x_sub movq %r9,%rcx sarq $3+2,%rcx - movq 56+8(%rsp),%rdi - decq %r12 + movq 56+8(%rsp),%rdi // rptr in .sqr4x_sub + decq %r12 // so that after 'not' we get -n[0] xorq %r10,%r10 movq 8(%rbp),%r13 movq 16(%rbp),%r14 @@ -1104,35 +1104,35 @@ _CET_ENDBR .cfi_offset %r15,-56 .Lpower5_prologue: - - - - shll $3,%r9d - leal (%r9,%r9,2),%r10d +// num is declared as an int, a 32-bit parameter, so the upper half is +// undefined. It is important that this write to %r9, which zeros the +// upper half, come before the first access. + shll $3,%r9d // convert %r9 to bytes + leal (%r9,%r9,2),%r10d // 3*%r9 negq %r9 - movq (%r8),%r8 - - - - - - - - + movq (%r8),%r8 // *n0 + +// ############################################################# +// Ensure that stack frame doesn't alias with %rdi+3*%r9 +// modulo 4096, which covers ret[num], am[num] and n[num] +// (see bn_exp.c). This is done to allow memory disambiguation +// logic do its magic. [Extra 256 bytes is for power mask +// calculated from 7th argument, the index.] +// leaq -320(%rsp,%r9,2),%r11 movq %rsp,%rbp subq %rdi,%r11 andq $4095,%r11 cmpq %r11,%r10 jb .Lpwr_sp_alt - subq %r11,%rbp - leaq -320(%rbp,%r9,2),%rbp + subq %r11,%rbp // align with %rsi + leaq -320(%rbp,%r9,2),%rbp // future alloca(frame+2*num*8+256) jmp .Lpwr_sp_done .align 32 .Lpwr_sp_alt: leaq 4096-320(,%r9,2),%r10 - leaq -320(%rbp,%r9,2),%rbp + leaq -320(%rbp,%r9,2),%rbp // future alloca(frame+2*num*8+256) subq %r10,%r11 movq $0,%r10 cmovcq %r10,%r11 @@ -1158,22 +1158,22 @@ _CET_ENDBR movq %r9,%r10 negq %r9 - - - - - - - - - +// ############################################################# +// Stack layout +// +// +0 saved %r9, used in reduction section +// +8 &t[2*%r9], used in reduction section +// +32 saved *n0 +// +40 saved %rsp +// +48 t[2*%r9] +// movq %r8,32(%rsp) - movq %rax,40(%rsp) + movq %rax,40(%rsp) // save original %rsp .cfi_escape 0x0f,0x05,0x77,0x28,0x06,0x23,0x08 .Lpower5_body: - movq %rdi,%xmm1 - movq %rcx,%xmm2 - movq %r10,%xmm3 + movq %rdi,%xmm1 // save %rdi, used in sqr8x + movq %rcx,%xmm2 // save %rcx + movq %r10,%xmm3 // -%r9, used in sqr8x movq %rdx,%xmm4 call __bn_sqr8x_internal @@ -1195,7 +1195,7 @@ _CET_ENDBR call mul4x_internal - movq 40(%rsp),%rsi + movq 40(%rsp),%rsi // restore %rsp .cfi_def_cfa %rsi,8 movq $1,%rax movq -48(%rsi),%r15 @@ -1226,261 +1226,261 @@ bn_sqr8x_internal: __bn_sqr8x_internal: .cfi_startproc _CET_ENDBR - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - leaq 32(%r10),%rbp - leaq (%rsi,%r9,1),%rsi - - movq %r9,%rcx - - - movq -32(%rsi,%rbp,1),%r14 - leaq 48+8(%rsp,%r9,2),%rdi - movq -24(%rsi,%rbp,1),%rax - leaq -32(%rdi,%rbp,1),%rdi - movq -16(%rsi,%rbp,1),%rbx +// ############################################################# +// Squaring part: +// +// a) multiply-n-add everything but a[i]*a[i]; +// b) shift result of a) by 1 to the left and accumulate +// a[i]*a[i] products; +// +// ############################################################# +// a[1]a[0] +// a[2]a[0] +// a[3]a[0] +// a[2]a[1] +// a[4]a[0] +// a[3]a[1] +// a[5]a[0] +// a[4]a[1] +// a[3]a[2] +// a[6]a[0] +// a[5]a[1] +// a[4]a[2] +// a[7]a[0] +// a[6]a[1] +// a[5]a[2] +// a[4]a[3] +// a[7]a[1] +// a[6]a[2] +// a[5]a[3] +// a[7]a[2] +// a[6]a[3] +// a[5]a[4] +// a[7]a[3] +// a[6]a[4] +// a[7]a[4] +// a[6]a[5] +// a[7]a[5] +// a[7]a[6] +// a[1]a[0] +// a[2]a[0] +// a[3]a[0] +// a[4]a[0] +// a[5]a[0] +// a[6]a[0] +// a[7]a[0] +// a[2]a[1] +// a[3]a[1] +// a[4]a[1] +// a[5]a[1] +// a[6]a[1] +// a[7]a[1] +// a[3]a[2] +// a[4]a[2] +// a[5]a[2] +// a[6]a[2] +// a[7]a[2] +// a[4]a[3] +// a[5]a[3] +// a[6]a[3] +// a[7]a[3] +// a[5]a[4] +// a[6]a[4] +// a[7]a[4] +// a[6]a[5] +// a[7]a[5] +// a[7]a[6] +// a[0]a[0] +// a[1]a[1] +// a[2]a[2] +// a[3]a[3] +// a[4]a[4] +// a[5]a[5] +// a[6]a[6] +// a[7]a[7] + + leaq 32(%r10),%rbp // %rbp=-(%r9-32) + leaq (%rsi,%r9,1),%rsi // end of a[] buffer, (%rsi,%rbp)=&ap[2] + + movq %r9,%rcx // %rcx=%r9 + +// comments apply to %r9==8 case + movq -32(%rsi,%rbp,1),%r14 // a[0] + leaq 48+8(%rsp,%r9,2),%rdi // end of tp[] buffer, &tp[2*%r9] + movq -24(%rsi,%rbp,1),%rax // a[1] + leaq -32(%rdi,%rbp,1),%rdi // end of tp[] window, &tp[2*%r9-"%rbp"] + movq -16(%rsi,%rbp,1),%rbx // a[2] movq %rax,%r15 - mulq %r14 - movq %rax,%r10 - movq %rbx,%rax + mulq %r14 // a[1]*a[0] + movq %rax,%r10 // a[1]*a[0] + movq %rbx,%rax // a[2] movq %rdx,%r11 - movq %r10,-24(%rdi,%rbp,1) + movq %r10,-24(%rdi,%rbp,1) // t[1] - mulq %r14 + mulq %r14 // a[2]*a[0] addq %rax,%r11 movq %rbx,%rax adcq $0,%rdx - movq %r11,-16(%rdi,%rbp,1) + movq %r11,-16(%rdi,%rbp,1) // t[2] movq %rdx,%r10 - movq -8(%rsi,%rbp,1),%rbx - mulq %r15 - movq %rax,%r12 + movq -8(%rsi,%rbp,1),%rbx // a[3] + mulq %r15 // a[2]*a[1] + movq %rax,%r12 // a[2]*a[1]+t[3] movq %rbx,%rax movq %rdx,%r13 leaq (%rbp),%rcx - mulq %r14 - addq %rax,%r10 + mulq %r14 // a[3]*a[0] + addq %rax,%r10 // a[3]*a[0]+a[2]*a[1]+t[3] movq %rbx,%rax movq %rdx,%r11 adcq $0,%r11 addq %r12,%r10 adcq $0,%r11 - movq %r10,-8(%rdi,%rcx,1) + movq %r10,-8(%rdi,%rcx,1) // t[3] jmp .Lsqr4x_1st .align 32 .Lsqr4x_1st: - movq (%rsi,%rcx,1),%rbx - mulq %r15 - addq %rax,%r13 + movq (%rsi,%rcx,1),%rbx // a[4] + mulq %r15 // a[3]*a[1] + addq %rax,%r13 // a[3]*a[1]+t[4] movq %rbx,%rax movq %rdx,%r12 adcq $0,%r12 - mulq %r14 - addq %rax,%r11 - movq %rbx,%rax - movq 8(%rsi,%rcx,1),%rbx + mulq %r14 // a[4]*a[0] + addq %rax,%r11 // a[4]*a[0]+a[3]*a[1]+t[4] + movq %rbx,%rax // a[3] + movq 8(%rsi,%rcx,1),%rbx // a[5] movq %rdx,%r10 adcq $0,%r10 addq %r13,%r11 adcq $0,%r10 - mulq %r15 - addq %rax,%r12 + mulq %r15 // a[4]*a[3] + addq %rax,%r12 // a[4]*a[3]+t[5] movq %rbx,%rax - movq %r11,(%rdi,%rcx,1) + movq %r11,(%rdi,%rcx,1) // t[4] movq %rdx,%r13 adcq $0,%r13 - mulq %r14 - addq %rax,%r10 + mulq %r14 // a[5]*a[2] + addq %rax,%r10 // a[5]*a[2]+a[4]*a[3]+t[5] movq %rbx,%rax - movq 16(%rsi,%rcx,1),%rbx + movq 16(%rsi,%rcx,1),%rbx // a[6] movq %rdx,%r11 adcq $0,%r11 addq %r12,%r10 adcq $0,%r11 - mulq %r15 - addq %rax,%r13 + mulq %r15 // a[5]*a[3] + addq %rax,%r13 // a[5]*a[3]+t[6] movq %rbx,%rax - movq %r10,8(%rdi,%rcx,1) + movq %r10,8(%rdi,%rcx,1) // t[5] movq %rdx,%r12 adcq $0,%r12 - mulq %r14 - addq %rax,%r11 - movq %rbx,%rax - movq 24(%rsi,%rcx,1),%rbx + mulq %r14 // a[6]*a[2] + addq %rax,%r11 // a[6]*a[2]+a[5]*a[3]+t[6] + movq %rbx,%rax // a[3] + movq 24(%rsi,%rcx,1),%rbx // a[7] movq %rdx,%r10 adcq $0,%r10 addq %r13,%r11 adcq $0,%r10 - mulq %r15 - addq %rax,%r12 + mulq %r15 // a[6]*a[5] + addq %rax,%r12 // a[6]*a[5]+t[7] movq %rbx,%rax - movq %r11,16(%rdi,%rcx,1) + movq %r11,16(%rdi,%rcx,1) // t[6] movq %rdx,%r13 adcq $0,%r13 leaq 32(%rcx),%rcx - mulq %r14 - addq %rax,%r10 + mulq %r14 // a[7]*a[4] + addq %rax,%r10 // a[7]*a[4]+a[6]*a[5]+t[6] movq %rbx,%rax movq %rdx,%r11 adcq $0,%r11 addq %r12,%r10 adcq $0,%r11 - movq %r10,-8(%rdi,%rcx,1) + movq %r10,-8(%rdi,%rcx,1) // t[7] cmpq $0,%rcx jne .Lsqr4x_1st - mulq %r15 + mulq %r15 // a[7]*a[5] addq %rax,%r13 leaq 16(%rbp),%rbp adcq $0,%rdx addq %r11,%r13 adcq $0,%rdx - movq %r13,(%rdi) + movq %r13,(%rdi) // t[8] movq %rdx,%r12 - movq %rdx,8(%rdi) + movq %rdx,8(%rdi) // t[9] jmp .Lsqr4x_outer .align 32 -.Lsqr4x_outer: - movq -32(%rsi,%rbp,1),%r14 - leaq 48+8(%rsp,%r9,2),%rdi - movq -24(%rsi,%rbp,1),%rax - leaq -32(%rdi,%rbp,1),%rdi - movq -16(%rsi,%rbp,1),%rbx +.Lsqr4x_outer: // comments apply to %r9==6 case + movq -32(%rsi,%rbp,1),%r14 // a[0] + leaq 48+8(%rsp,%r9,2),%rdi // end of tp[] buffer, &tp[2*%r9] + movq -24(%rsi,%rbp,1),%rax // a[1] + leaq -32(%rdi,%rbp,1),%rdi // end of tp[] window, &tp[2*%r9-"%rbp"] + movq -16(%rsi,%rbp,1),%rbx // a[2] movq %rax,%r15 - mulq %r14 - movq -24(%rdi,%rbp,1),%r10 - addq %rax,%r10 - movq %rbx,%rax + mulq %r14 // a[1]*a[0] + movq -24(%rdi,%rbp,1),%r10 // t[1] + addq %rax,%r10 // a[1]*a[0]+t[1] + movq %rbx,%rax // a[2] adcq $0,%rdx - movq %r10,-24(%rdi,%rbp,1) + movq %r10,-24(%rdi,%rbp,1) // t[1] movq %rdx,%r11 - mulq %r14 + mulq %r14 // a[2]*a[0] addq %rax,%r11 movq %rbx,%rax adcq $0,%rdx - addq -16(%rdi,%rbp,1),%r11 + addq -16(%rdi,%rbp,1),%r11 // a[2]*a[0]+t[2] movq %rdx,%r10 adcq $0,%r10 - movq %r11,-16(%rdi,%rbp,1) + movq %r11,-16(%rdi,%rbp,1) // t[2] xorq %r12,%r12 - movq -8(%rsi,%rbp,1),%rbx - mulq %r15 - addq %rax,%r12 + movq -8(%rsi,%rbp,1),%rbx // a[3] + mulq %r15 // a[2]*a[1] + addq %rax,%r12 // a[2]*a[1]+t[3] movq %rbx,%rax adcq $0,%rdx addq -8(%rdi,%rbp,1),%r12 movq %rdx,%r13 adcq $0,%r13 - mulq %r14 - addq %rax,%r10 + mulq %r14 // a[3]*a[0] + addq %rax,%r10 // a[3]*a[0]+a[2]*a[1]+t[3] movq %rbx,%rax adcq $0,%rdx addq %r12,%r10 movq %rdx,%r11 adcq $0,%r11 - movq %r10,-8(%rdi,%rbp,1) + movq %r10,-8(%rdi,%rbp,1) // t[3] leaq (%rbp),%rcx jmp .Lsqr4x_inner .align 32 .Lsqr4x_inner: - movq (%rsi,%rcx,1),%rbx - mulq %r15 - addq %rax,%r13 + movq (%rsi,%rcx,1),%rbx // a[4] + mulq %r15 // a[3]*a[1] + addq %rax,%r13 // a[3]*a[1]+t[4] movq %rbx,%rax movq %rdx,%r12 adcq $0,%r12 @@ -1488,254 +1488,254 @@ _CET_ENDBR adcq $0,%r12 .byte 0x67 - mulq %r14 - addq %rax,%r11 - movq %rbx,%rax - movq 8(%rsi,%rcx,1),%rbx + mulq %r14 // a[4]*a[0] + addq %rax,%r11 // a[4]*a[0]+a[3]*a[1]+t[4] + movq %rbx,%rax // a[3] + movq 8(%rsi,%rcx,1),%rbx // a[5] movq %rdx,%r10 adcq $0,%r10 addq %r13,%r11 adcq $0,%r10 - mulq %r15 - addq %rax,%r12 - movq %r11,(%rdi,%rcx,1) + mulq %r15 // a[4]*a[3] + addq %rax,%r12 // a[4]*a[3]+t[5] + movq %r11,(%rdi,%rcx,1) // t[4] movq %rbx,%rax movq %rdx,%r13 adcq $0,%r13 addq 8(%rdi,%rcx,1),%r12 - leaq 16(%rcx),%rcx + leaq 16(%rcx),%rcx // j++ adcq $0,%r13 - mulq %r14 - addq %rax,%r10 + mulq %r14 // a[5]*a[2] + addq %rax,%r10 // a[5]*a[2]+a[4]*a[3]+t[5] movq %rbx,%rax adcq $0,%rdx addq %r12,%r10 movq %rdx,%r11 adcq $0,%r11 - movq %r10,-8(%rdi,%rcx,1) + movq %r10,-8(%rdi,%rcx,1) // t[5], "preloaded t[1]" below cmpq $0,%rcx jne .Lsqr4x_inner .byte 0x67 - mulq %r15 + mulq %r15 // a[5]*a[3] addq %rax,%r13 adcq $0,%rdx addq %r11,%r13 adcq $0,%rdx - movq %r13,(%rdi) + movq %r13,(%rdi) // t[6], "preloaded t[2]" below movq %rdx,%r12 - movq %rdx,8(%rdi) + movq %rdx,8(%rdi) // t[7], "preloaded t[3]" below addq $16,%rbp jnz .Lsqr4x_outer - - movq -32(%rsi),%r14 - leaq 48+8(%rsp,%r9,2),%rdi - movq -24(%rsi),%rax - leaq -32(%rdi,%rbp,1),%rdi - movq -16(%rsi),%rbx +// comments apply to %r9==4 case + movq -32(%rsi),%r14 // a[0] + leaq 48+8(%rsp,%r9,2),%rdi // end of tp[] buffer, &tp[2*%r9] + movq -24(%rsi),%rax // a[1] + leaq -32(%rdi,%rbp,1),%rdi // end of tp[] window, &tp[2*%r9-"%rbp"] + movq -16(%rsi),%rbx // a[2] movq %rax,%r15 - mulq %r14 - addq %rax,%r10 - movq %rbx,%rax + mulq %r14 // a[1]*a[0] + addq %rax,%r10 // a[1]*a[0]+t[1], preloaded t[1] + movq %rbx,%rax // a[2] movq %rdx,%r11 adcq $0,%r11 - mulq %r14 + mulq %r14 // a[2]*a[0] addq %rax,%r11 movq %rbx,%rax - movq %r10,-24(%rdi) + movq %r10,-24(%rdi) // t[1] movq %rdx,%r10 adcq $0,%r10 - addq %r13,%r11 - movq -8(%rsi),%rbx + addq %r13,%r11 // a[2]*a[0]+t[2], preloaded t[2] + movq -8(%rsi),%rbx // a[3] adcq $0,%r10 - mulq %r15 - addq %rax,%r12 + mulq %r15 // a[2]*a[1] + addq %rax,%r12 // a[2]*a[1]+t[3], preloaded t[3] movq %rbx,%rax - movq %r11,-16(%rdi) + movq %r11,-16(%rdi) // t[2] movq %rdx,%r13 adcq $0,%r13 - mulq %r14 - addq %rax,%r10 + mulq %r14 // a[3]*a[0] + addq %rax,%r10 // a[3]*a[0]+a[2]*a[1]+t[3] movq %rbx,%rax movq %rdx,%r11 adcq $0,%r11 addq %r12,%r10 adcq $0,%r11 - movq %r10,-8(%rdi) + movq %r10,-8(%rdi) // t[3] - mulq %r15 + mulq %r15 // a[3]*a[1] addq %rax,%r13 - movq -16(%rsi),%rax + movq -16(%rsi),%rax // a[2] adcq $0,%rdx addq %r11,%r13 adcq $0,%rdx - movq %r13,(%rdi) + movq %r13,(%rdi) // t[4] movq %rdx,%r12 - movq %rdx,8(%rdi) + movq %rdx,8(%rdi) // t[5] - mulq %rbx + mulq %rbx // a[2]*a[3] addq $16,%rbp xorq %r14,%r14 - subq %r9,%rbp + subq %r9,%rbp // %rbp=16-%r9 xorq %r15,%r15 - addq %r12,%rax + addq %r12,%rax // t[5] adcq $0,%rdx - movq %rax,8(%rdi) - movq %rdx,16(%rdi) - movq %r15,24(%rdi) + movq %rax,8(%rdi) // t[5] + movq %rdx,16(%rdi) // t[6] + movq %r15,24(%rdi) // t[7] - movq -16(%rsi,%rbp,1),%rax + movq -16(%rsi,%rbp,1),%rax // a[0] leaq 48+8(%rsp),%rdi - xorq %r10,%r10 - movq 8(%rdi),%r11 + xorq %r10,%r10 // t[0] + movq 8(%rdi),%r11 // t[1] - leaq (%r14,%r10,2),%r12 + leaq (%r14,%r10,2),%r12 // t[2*i]<<1 | shift shrq $63,%r10 - leaq (%rcx,%r11,2),%r13 + leaq (%rcx,%r11,2),%r13 // t[2*i+1]<<1 | shrq $63,%r11 - orq %r10,%r13 - movq 16(%rdi),%r10 - movq %r11,%r14 - mulq %rax - negq %r15 - movq 24(%rdi),%r11 + orq %r10,%r13 // | t[2*i]>>63 + movq 16(%rdi),%r10 // t[2*i+2] # prefetch + movq %r11,%r14 // shift=t[2*i+1]>>63 + mulq %rax // a[i]*a[i] + negq %r15 // mov %r15,cf + movq 24(%rdi),%r11 // t[2*i+2+1] # prefetch adcq %rax,%r12 - movq -8(%rsi,%rbp,1),%rax + movq -8(%rsi,%rbp,1),%rax // a[i+1] # prefetch movq %r12,(%rdi) adcq %rdx,%r13 - leaq (%r14,%r10,2),%rbx + leaq (%r14,%r10,2),%rbx // t[2*i]<<1 | shift movq %r13,8(%rdi) - sbbq %r15,%r15 + sbbq %r15,%r15 // mov cf,%r15 shrq $63,%r10 - leaq (%rcx,%r11,2),%r8 + leaq (%rcx,%r11,2),%r8 // t[2*i+1]<<1 | shrq $63,%r11 - orq %r10,%r8 - movq 32(%rdi),%r10 - movq %r11,%r14 - mulq %rax - negq %r15 - movq 40(%rdi),%r11 + orq %r10,%r8 // | t[2*i]>>63 + movq 32(%rdi),%r10 // t[2*i+2] # prefetch + movq %r11,%r14 // shift=t[2*i+1]>>63 + mulq %rax // a[i]*a[i] + negq %r15 // mov %r15,cf + movq 40(%rdi),%r11 // t[2*i+2+1] # prefetch adcq %rax,%rbx - movq 0(%rsi,%rbp,1),%rax + movq 0(%rsi,%rbp,1),%rax // a[i+1] # prefetch movq %rbx,16(%rdi) adcq %rdx,%r8 leaq 16(%rbp),%rbp movq %r8,24(%rdi) - sbbq %r15,%r15 + sbbq %r15,%r15 // mov cf,%r15 leaq 64(%rdi),%rdi jmp .Lsqr4x_shift_n_add .align 32 .Lsqr4x_shift_n_add: - leaq (%r14,%r10,2),%r12 + leaq (%r14,%r10,2),%r12 // t[2*i]<<1 | shift shrq $63,%r10 - leaq (%rcx,%r11,2),%r13 + leaq (%rcx,%r11,2),%r13 // t[2*i+1]<<1 | shrq $63,%r11 - orq %r10,%r13 - movq -16(%rdi),%r10 - movq %r11,%r14 - mulq %rax - negq %r15 - movq -8(%rdi),%r11 + orq %r10,%r13 // | t[2*i]>>63 + movq -16(%rdi),%r10 // t[2*i+2] # prefetch + movq %r11,%r14 // shift=t[2*i+1]>>63 + mulq %rax // a[i]*a[i] + negq %r15 // mov %r15,cf + movq -8(%rdi),%r11 // t[2*i+2+1] # prefetch adcq %rax,%r12 - movq -8(%rsi,%rbp,1),%rax + movq -8(%rsi,%rbp,1),%rax // a[i+1] # prefetch movq %r12,-32(%rdi) adcq %rdx,%r13 - leaq (%r14,%r10,2),%rbx + leaq (%r14,%r10,2),%rbx // t[2*i]<<1 | shift movq %r13,-24(%rdi) - sbbq %r15,%r15 + sbbq %r15,%r15 // mov cf,%r15 shrq $63,%r10 - leaq (%rcx,%r11,2),%r8 + leaq (%rcx,%r11,2),%r8 // t[2*i+1]<<1 | shrq $63,%r11 - orq %r10,%r8 - movq 0(%rdi),%r10 - movq %r11,%r14 - mulq %rax - negq %r15 - movq 8(%rdi),%r11 + orq %r10,%r8 // | t[2*i]>>63 + movq 0(%rdi),%r10 // t[2*i+2] # prefetch + movq %r11,%r14 // shift=t[2*i+1]>>63 + mulq %rax // a[i]*a[i] + negq %r15 // mov %r15,cf + movq 8(%rdi),%r11 // t[2*i+2+1] # prefetch adcq %rax,%rbx - movq 0(%rsi,%rbp,1),%rax + movq 0(%rsi,%rbp,1),%rax // a[i+1] # prefetch movq %rbx,-16(%rdi) adcq %rdx,%r8 - leaq (%r14,%r10,2),%r12 + leaq (%r14,%r10,2),%r12 // t[2*i]<<1 | shift movq %r8,-8(%rdi) - sbbq %r15,%r15 + sbbq %r15,%r15 // mov cf,%r15 shrq $63,%r10 - leaq (%rcx,%r11,2),%r13 + leaq (%rcx,%r11,2),%r13 // t[2*i+1]<<1 | shrq $63,%r11 - orq %r10,%r13 - movq 16(%rdi),%r10 - movq %r11,%r14 - mulq %rax - negq %r15 - movq 24(%rdi),%r11 + orq %r10,%r13 // | t[2*i]>>63 + movq 16(%rdi),%r10 // t[2*i+2] # prefetch + movq %r11,%r14 // shift=t[2*i+1]>>63 + mulq %rax // a[i]*a[i] + negq %r15 // mov %r15,cf + movq 24(%rdi),%r11 // t[2*i+2+1] # prefetch adcq %rax,%r12 - movq 8(%rsi,%rbp,1),%rax + movq 8(%rsi,%rbp,1),%rax // a[i+1] # prefetch movq %r12,0(%rdi) adcq %rdx,%r13 - leaq (%r14,%r10,2),%rbx + leaq (%r14,%r10,2),%rbx // t[2*i]<<1 | shift movq %r13,8(%rdi) - sbbq %r15,%r15 + sbbq %r15,%r15 // mov cf,%r15 shrq $63,%r10 - leaq (%rcx,%r11,2),%r8 + leaq (%rcx,%r11,2),%r8 // t[2*i+1]<<1 | shrq $63,%r11 - orq %r10,%r8 - movq 32(%rdi),%r10 - movq %r11,%r14 - mulq %rax - negq %r15 - movq 40(%rdi),%r11 + orq %r10,%r8 // | t[2*i]>>63 + movq 32(%rdi),%r10 // t[2*i+2] # prefetch + movq %r11,%r14 // shift=t[2*i+1]>>63 + mulq %rax // a[i]*a[i] + negq %r15 // mov %r15,cf + movq 40(%rdi),%r11 // t[2*i+2+1] # prefetch adcq %rax,%rbx - movq 16(%rsi,%rbp,1),%rax + movq 16(%rsi,%rbp,1),%rax // a[i+1] # prefetch movq %rbx,16(%rdi) adcq %rdx,%r8 movq %r8,24(%rdi) - sbbq %r15,%r15 + sbbq %r15,%r15 // mov cf,%r15 leaq 64(%rdi),%rdi addq $32,%rbp jnz .Lsqr4x_shift_n_add - leaq (%r14,%r10,2),%r12 + leaq (%r14,%r10,2),%r12 // t[2*i]<<1 | shift .byte 0x67 shrq $63,%r10 - leaq (%rcx,%r11,2),%r13 + leaq (%rcx,%r11,2),%r13 // t[2*i+1]<<1 | shrq $63,%r11 - orq %r10,%r13 - movq -16(%rdi),%r10 - movq %r11,%r14 - mulq %rax - negq %r15 - movq -8(%rdi),%r11 + orq %r10,%r13 // | t[2*i]>>63 + movq -16(%rdi),%r10 // t[2*i+2] # prefetch + movq %r11,%r14 // shift=t[2*i+1]>>63 + mulq %rax // a[i]*a[i] + negq %r15 // mov %r15,cf + movq -8(%rdi),%r11 // t[2*i+2+1] # prefetch adcq %rax,%r12 - movq -8(%rsi),%rax + movq -8(%rsi),%rax // a[i+1] # prefetch movq %r12,-32(%rdi) adcq %rdx,%r13 - leaq (%r14,%r10,2),%rbx + leaq (%r14,%r10,2),%rbx // t[2*i]<<1|shift movq %r13,-24(%rdi) - sbbq %r15,%r15 + sbbq %r15,%r15 // mov cf,%r15 shrq $63,%r10 - leaq (%rcx,%r11,2),%r8 + leaq (%rcx,%r11,2),%r8 // t[2*i+1]<<1 | shrq $63,%r11 - orq %r10,%r8 - mulq %rax - negq %r15 + orq %r10,%r8 // | t[2*i]>>63 + mulq %rax // a[i]*a[i] + negq %r15 // mov %r15,cf adcq %rax,%rbx adcq %rdx,%r8 movq %rbx,-16(%rdi) @@ -1743,17 +1743,17 @@ _CET_ENDBR movq %xmm2,%rbp __bn_sqr8x_reduction: xorq %rax,%rax - leaq (%r9,%rbp,1),%rcx - leaq 48+8(%rsp,%r9,2),%rdx + leaq (%r9,%rbp,1),%rcx // end of n[] + leaq 48+8(%rsp,%r9,2),%rdx // end of t[] buffer movq %rcx,0+8(%rsp) - leaq 48+8(%rsp,%r9,1),%rdi + leaq 48+8(%rsp,%r9,1),%rdi // end of initial t[] window movq %rdx,8+8(%rsp) negq %r9 jmp .L8x_reduction_loop .align 32 .L8x_reduction_loop: - leaq (%rdi,%r9,1),%rdi + leaq (%rdi,%r9,1),%rdi // start of current t[] window .byte 0x66 movq 0(%rdi),%rbx movq 8(%rdi),%r9 @@ -1763,20 +1763,20 @@ __bn_sqr8x_reduction: movq 40(%rdi),%r13 movq 48(%rdi),%r14 movq 56(%rdi),%r15 - movq %rax,(%rdx) + movq %rax,(%rdx) // store top-most carry bit leaq 64(%rdi),%rdi .byte 0x67 movq %rbx,%r8 - imulq 32+8(%rsp),%rbx - movq 0(%rbp),%rax + imulq 32+8(%rsp),%rbx // n0*a[0] + movq 0(%rbp),%rax // n[0] movl $8,%ecx jmp .L8x_reduce .align 32 .L8x_reduce: mulq %rbx - movq 8(%rbp),%rax + movq 8(%rbp),%rax // n[1] negq %r8 movq %rdx,%r8 adcq $0,%r8 @@ -1786,7 +1786,7 @@ __bn_sqr8x_reduction: movq 16(%rbp),%rax adcq $0,%rdx addq %r9,%r8 - movq %rbx,48-8+8(%rsp,%rcx,8) + movq %rbx,48-8+8(%rsp,%rcx,8) // put aside n0*a[i] movq %rdx,%r9 adcq $0,%r9 @@ -1795,7 +1795,7 @@ __bn_sqr8x_reduction: movq 24(%rbp),%rax adcq $0,%rdx addq %r10,%r9 - movq 32+8(%rsp),%rsi + movq 32+8(%rsp),%rsi // pull n0, borrow %rsi movq %rdx,%r10 adcq $0,%r10 @@ -1803,7 +1803,7 @@ __bn_sqr8x_reduction: addq %rax,%r11 movq 32(%rbp),%rax adcq $0,%rdx - imulq %r8,%rsi + imulq %r8,%rsi // modulo-scheduled addq %r11,%r10 movq %rdx,%r11 adcq $0,%r11 @@ -1833,9 +1833,9 @@ __bn_sqr8x_reduction: adcq $0,%r14 mulq %rbx - movq %rsi,%rbx + movq %rsi,%rbx // n0*a[i] addq %rax,%r15 - movq 0(%rbp),%rax + movq 0(%rbp),%rax // n[0] adcq $0,%rdx addq %r15,%r14 movq %rdx,%r15 @@ -1846,8 +1846,8 @@ __bn_sqr8x_reduction: leaq 64(%rbp),%rbp xorq %rax,%rax - movq 8+8(%rsp),%rdx - cmpq 0+8(%rsp),%rbp + movq 8+8(%rsp),%rdx // pull end of t[] + cmpq 0+8(%rsp),%rbp // end of n[]? jae .L8x_no_tail .byte 0x66 @@ -1859,9 +1859,9 @@ __bn_sqr8x_reduction: adcq 40(%rdi),%r13 adcq 48(%rdi),%r14 adcq 56(%rdi),%r15 - sbbq %rsi,%rsi + sbbq %rsi,%rsi // top carry - movq 48+56+8(%rsp),%rbx + movq 48+56+8(%rsp),%rbx // pull n0*a[0] movl $8,%ecx movq 0(%rbp),%rax jmp .L8x_tail @@ -1871,7 +1871,7 @@ __bn_sqr8x_reduction: mulq %rbx addq %rax,%r8 movq 8(%rbp),%rax - movq %r8,(%rdi) + movq %r8,(%rdi) // save result movq %rdx,%r8 adcq $0,%r8 @@ -1880,7 +1880,7 @@ __bn_sqr8x_reduction: movq 16(%rbp),%rax adcq $0,%rdx addq %r9,%r8 - leaq 8(%rdi),%rdi + leaq 8(%rdi),%rdi // %rdi++ movq %rdx,%r9 adcq $0,%r9 @@ -1925,11 +1925,11 @@ __bn_sqr8x_reduction: adcq $0,%r14 mulq %rbx - movq 48-16+8(%rsp,%rcx,8),%rbx + movq 48-16+8(%rsp,%rcx,8),%rbx // pull n0*a[i] addq %rax,%r15 adcq $0,%rdx addq %r15,%r14 - movq 0(%rbp),%rax + movq 0(%rbp),%rax // pull n[0] movq %rdx,%r15 adcq $0,%r15 @@ -1937,13 +1937,13 @@ __bn_sqr8x_reduction: jnz .L8x_tail leaq 64(%rbp),%rbp - movq 8+8(%rsp),%rdx - cmpq 0+8(%rsp),%rbp - jae .L8x_tail_done + movq 8+8(%rsp),%rdx // pull end of t[] + cmpq 0+8(%rsp),%rbp // end of n[]? + jae .L8x_tail_done // break out of loop - movq 48+56+8(%rsp),%rbx + movq 48+56+8(%rsp),%rbx // pull n0*a[0] negq %rsi - movq 0(%rbp),%rax + movq 0(%rbp),%rax // pull n[0] adcq 0(%rdi),%r8 adcq 8(%rdi),%r9 adcq 16(%rdi),%r10 @@ -1952,7 +1952,7 @@ __bn_sqr8x_reduction: adcq 40(%rdi),%r13 adcq 48(%rdi),%r14 adcq 56(%rdi),%r15 - sbbq %rsi,%rsi + sbbq %rsi,%rsi // top carry movl $8,%ecx jmp .L8x_tail @@ -1960,7 +1960,7 @@ __bn_sqr8x_reduction: .align 32 .L8x_tail_done: xorq %rax,%rax - addq (%rdx),%r8 + addq (%rdx),%r8 // can this overflow? adcq $0,%r9 adcq $0,%r10 adcq $0,%r11 @@ -1980,15 +1980,15 @@ __bn_sqr8x_reduction: adcq 40(%rdi),%r13 adcq 48(%rdi),%r14 adcq 56(%rdi),%r15 - adcq $0,%rax - movq -8(%rbp),%rcx + adcq $0,%rax // top-most carry + movq -8(%rbp),%rcx // np[num-1] xorq %rsi,%rsi - movq %xmm2,%rbp + movq %xmm2,%rbp // restore %rbp - movq %r8,0(%rdi) + movq %r8,0(%rdi) // store top 512 bits movq %r9,8(%rdi) - movq %xmm3,%r9 + movq %xmm3,%r9 // %r9 is %r9, can't be moved upwards movq %r10,16(%rdi) movq %r11,24(%rdi) movq %r12,32(%rdi) @@ -1997,7 +1997,7 @@ __bn_sqr8x_reduction: movq %r15,56(%rdi) leaq 64(%rdi),%rdi - cmpq %rdx,%rdi + cmpq %rdx,%rdi // end of t[]? jb .L8x_reduction_loop ret .cfi_endproc @@ -2007,13 +2007,13 @@ __bn_sqr8x_reduction: __bn_post4x_internal: .cfi_startproc movq 0(%rbp),%r12 - leaq (%rdi,%r9,1),%rbx + leaq (%rdi,%r9,1),%rbx // %rdi was %rbx above movq %r9,%rcx - movq %xmm1,%rdi + movq %xmm1,%rdi // restore %rdi negq %rax - movq %xmm1,%rsi + movq %xmm1,%rsi // prepare for back-to-back call sarq $3+2,%rcx - decq %r12 + decq %r12 // so that after 'not' we get -n[0] xorq %r10,%r10 movq 8(%rbp),%r13 movq 16(%rbp),%r14 @@ -2037,7 +2037,7 @@ __bn_post4x_internal: andq %rax,%r14 andq %rax,%r15 - negq %r10 + negq %r10 // mov %r10,%cf adcq 0(%rbx),%r12 adcq 8(%rbx),%r13 adcq 16(%rbx),%r14 @@ -2045,16 +2045,16 @@ __bn_post4x_internal: movq %r12,0(%rdi) leaq 32(%rbx),%rbx movq %r13,8(%rdi) - sbbq %r10,%r10 + sbbq %r10,%r10 // mov %cf,%r10 movq %r14,16(%rdi) movq %r15,24(%rdi) leaq 32(%rdi),%rdi - incq %rcx + incq %rcx // pass %cf jnz .Lsqr4x_sub - movq %r9,%r10 - negq %r9 + movq %r9,%r10 // prepare for back-to-back call + negq %r9 // restore %r9 ret .cfi_endproc .size __bn_post4x_internal,.-__bn_post4x_internal @@ -2081,42 +2081,42 @@ _CET_ENDBR .cfi_offset %r15,-56 .Lmulx4x_prologue: - - - - shll $3,%r9d - leaq (%r9,%r9,2),%r10 - negq %r9 - movq (%r8),%r8 - - - - - - - - - - +// num is declared as an int, a 32-bit parameter, so the upper half is +// undefined. It is important that this write to %r9, which zeros the +// upper half, predates the first access. + shll $3,%r9d // convert %r9 to bytes + leaq (%r9,%r9,2),%r10 // 3*%r9 in bytes + negq %r9 // -%r9 + movq (%r8),%r8 // *n0 + +// ############################################################# +// Ensure that stack frame doesn't alias with +3*%r9 +// modulo 4096, which covers ret[num], am[num] and n[num] +// (see bn_exp.c). This is done to allow memory disambiguation +// logic do its magic. [Extra [num] is allocated in order +// to align with bn_power5's frame, which is cleansed after +// completing exponentiation. Extra 256 bytes is for power mask +// calculated from 7th argument, the index.] +// leaq -320(%rsp,%r9,2),%r11 movq %rsp,%rbp subq %rdi,%r11 andq $4095,%r11 cmpq %r11,%r10 jb .Lmulx4xsp_alt - subq %r11,%rbp - leaq -320(%rbp,%r9,2),%rbp + subq %r11,%rbp // align with + leaq -320(%rbp,%r9,2),%rbp // future alloca(frame+2*%r9*8+256) jmp .Lmulx4xsp_done .Lmulx4xsp_alt: leaq 4096-320(,%r9,2),%r10 - leaq -320(%rbp,%r9,2),%rbp + leaq -320(%rbp,%r9,2),%rbp // future alloca(frame+2*%r9*8+256) subq %r10,%r11 movq $0,%r10 cmovcq %r10,%r11 subq %r11,%rbp .Lmulx4xsp_done: - andq $-64,%rbp + andq $-64,%rbp // ensure alignment movq %rsp,%r11 subq %rbp,%r11 andq $-4096,%r11 @@ -2133,25 +2133,25 @@ _CET_ENDBR ja .Lmulx4x_page_walk .Lmulx4x_page_walk_done: - - - - - - - - - - - - - movq %r8,32(%rsp) - movq %rax,40(%rsp) +// ############################################################# +// Stack layout +// +0 -num +// +8 off-loaded &b[i] +// +16 end of b[num] +// +24 inner counter +// +32 saved n0 +// +40 saved %rsp +// +48 +// +56 saved rp +// +64 tmp[num+1] +// + movq %r8,32(%rsp) // save *n0 + movq %rax,40(%rsp) // save original %rsp .cfi_escape 0x0f,0x05,0x77,0x28,0x06,0x23,0x08 .Lmulx4x_body: call mulx4x_internal - movq 40(%rsp),%rsi + movq 40(%rsp),%rsi // restore %rsp .cfi_def_cfa %rsi,8 movq $1,%rax @@ -2178,44 +2178,44 @@ _CET_ENDBR .align 32 mulx4x_internal: .cfi_startproc - movq %r9,8(%rsp) + movq %r9,8(%rsp) // save -%r9 (it was in bytes) movq %r9,%r10 - negq %r9 + negq %r9 // restore %r9 shlq $5,%r9 - negq %r10 - leaq 128(%rdx,%r9,1),%r13 + negq %r10 // restore %r9 + leaq 128(%rdx,%r9,1),%r13 // end of powers table (+size optimization) shrq $5+5,%r9 - movd 8(%rax),%xmm5 + movd 8(%rax),%xmm5 // load 7th argument subq $1,%r9 leaq .Linc(%rip),%rax - movq %r13,16+8(%rsp) - movq %r9,24+8(%rsp) - movq %rdi,56+8(%rsp) - movdqa 0(%rax),%xmm0 - movdqa 16(%rax),%xmm1 - leaq 88-112(%rsp,%r10,1),%r10 - leaq 128(%rdx),%rdi - - pshufd $0,%xmm5,%xmm5 + movq %r13,16+8(%rsp) // end of b[num] + movq %r9,24+8(%rsp) // inner counter + movq %rdi,56+8(%rsp) // save %rdi + movdqa 0(%rax),%xmm0 // 00000001000000010000000000000000 + movdqa 16(%rax),%xmm1 // 00000002000000020000000200000002 + leaq 88-112(%rsp,%r10,1),%r10 // place the mask after tp[num+1] (+ICache optimization) + leaq 128(%rdx),%rdi // size optimization + + pshufd $0,%xmm5,%xmm5 // broadcast index movdqa %xmm1,%xmm4 .byte 0x67 movdqa %xmm1,%xmm2 .byte 0x67 paddd %xmm0,%xmm1 - pcmpeqd %xmm5,%xmm0 + pcmpeqd %xmm5,%xmm0 // compare to 1,0 movdqa %xmm4,%xmm3 paddd %xmm1,%xmm2 - pcmpeqd %xmm5,%xmm1 + pcmpeqd %xmm5,%xmm1 // compare to 3,2 movdqa %xmm0,112(%r10) movdqa %xmm4,%xmm0 paddd %xmm2,%xmm3 - pcmpeqd %xmm5,%xmm2 + pcmpeqd %xmm5,%xmm2 // compare to 5,4 movdqa %xmm1,128(%r10) movdqa %xmm4,%xmm1 paddd %xmm3,%xmm0 - pcmpeqd %xmm5,%xmm3 + pcmpeqd %xmm5,%xmm3 // compare to 7,6 movdqa %xmm2,144(%r10) movdqa %xmm4,%xmm2 @@ -2224,17 +2224,17 @@ mulx4x_internal: movdqa %xmm3,160(%r10) movdqa %xmm4,%xmm3 paddd %xmm1,%xmm2 - pcmpeqd %xmm5,%xmm1 + pcmpeqd %xmm5,%xmm1 // compare to 3,2 movdqa %xmm0,176(%r10) movdqa %xmm4,%xmm0 paddd %xmm2,%xmm3 - pcmpeqd %xmm5,%xmm2 + pcmpeqd %xmm5,%xmm2 // compare to 5,4 movdqa %xmm1,192(%r10) movdqa %xmm4,%xmm1 paddd %xmm3,%xmm0 - pcmpeqd %xmm5,%xmm3 + pcmpeqd %xmm5,%xmm3 // compare to 7,6 movdqa %xmm2,208(%r10) movdqa %xmm4,%xmm2 @@ -2243,17 +2243,17 @@ mulx4x_internal: movdqa %xmm3,224(%r10) movdqa %xmm4,%xmm3 paddd %xmm1,%xmm2 - pcmpeqd %xmm5,%xmm1 + pcmpeqd %xmm5,%xmm1 // compare to 3,2 movdqa %xmm0,240(%r10) movdqa %xmm4,%xmm0 paddd %xmm2,%xmm3 - pcmpeqd %xmm5,%xmm2 + pcmpeqd %xmm5,%xmm2 // compare to 5,4 movdqa %xmm1,256(%r10) movdqa %xmm4,%xmm1 paddd %xmm3,%xmm0 - pcmpeqd %xmm5,%xmm3 + pcmpeqd %xmm5,%xmm3 // compare to 7,6 movdqa %xmm2,272(%r10) movdqa %xmm4,%xmm2 @@ -2273,7 +2273,7 @@ mulx4x_internal: pcmpeqd %xmm5,%xmm3 movdqa %xmm2,336(%r10) - pand 64(%rdi),%xmm0 + pand 64(%rdi),%xmm0 // while it's still in register pand 80(%rdi),%xmm1 pand 96(%rdi),%xmm2 movdqa %xmm3,352(%r10) @@ -2317,41 +2317,41 @@ mulx4x_internal: por %xmm2,%xmm0 por %xmm3,%xmm1 pxor %xmm1,%xmm0 - - pshufd $0x4e,%xmm0,%xmm1 +// Combine the upper and lower halves of %xmm0. + pshufd $0x4e,%xmm0,%xmm1 // Swap upper and lower halves. por %xmm1,%xmm0 leaq 256(%rdi),%rdi - movq %xmm0,%rdx + movq %xmm0,%rdx // bp[0] leaq 64+32+8(%rsp),%rbx movq %rdx,%r9 - mulxq 0(%rsi),%r8,%rax - mulxq 8(%rsi),%r11,%r12 + mulxq 0(%rsi),%r8,%rax // a[0]*b[0] + mulxq 8(%rsi),%r11,%r12 // a[1]*b[0] addq %rax,%r11 - mulxq 16(%rsi),%rax,%r13 + mulxq 16(%rsi),%rax,%r13 // ... adcq %rax,%r12 adcq $0,%r13 mulxq 24(%rsi),%rax,%r14 movq %r8,%r15 - imulq 32+8(%rsp),%r8 - xorq %rbp,%rbp + imulq 32+8(%rsp),%r8 // "t[0]"*n0 + xorq %rbp,%rbp // cf=0, of=0 movq %r8,%rdx - movq %rdi,8+8(%rsp) + movq %rdi,8+8(%rsp) // off-load &b[i] leaq 32(%rsi),%rsi adcxq %rax,%r13 - adcxq %rbp,%r14 + adcxq %rbp,%r14 // cf=0 mulxq 0(%rcx),%rax,%r10 - adcxq %rax,%r15 + adcxq %rax,%r15 // discarded adoxq %r11,%r10 mulxq 8(%rcx),%rax,%r11 adcxq %rax,%r10 adoxq %r12,%r11 mulxq 16(%rcx),%rax,%r12 - movq 24+8(%rsp),%rdi + movq 24+8(%rsp),%rdi // counter value movq %r10,-32(%rbx) adcxq %rax,%r11 adoxq %r13,%r12 @@ -2359,25 +2359,25 @@ mulx4x_internal: movq %r9,%rdx movq %r11,-24(%rbx) adcxq %rax,%r12 - adoxq %rbp,%r15 + adoxq %rbp,%r15 // of=0 leaq 32(%rcx),%rcx movq %r12,-16(%rbx) jmp .Lmulx4x_1st .align 32 .Lmulx4x_1st: - adcxq %rbp,%r15 - mulxq 0(%rsi),%r10,%rax + adcxq %rbp,%r15 // cf=0, modulo-scheduled + mulxq 0(%rsi),%r10,%rax // a[4]*b[0] adcxq %r14,%r10 - mulxq 8(%rsi),%r11,%r14 + mulxq 8(%rsi),%r11,%r14 // a[5]*b[0] adcxq %rax,%r11 - mulxq 16(%rsi),%r12,%rax + mulxq 16(%rsi),%r12,%rax // ... adcxq %r14,%r12 mulxq 24(%rsi),%r13,%r14 .byte 0x67,0x67 movq %r8,%rdx adcxq %rax,%r13 - adcxq %rbp,%r14 + adcxq %rbp,%r14 // cf=0 leaq 32(%rsi),%rsi leaq 32(%rbx),%rbx @@ -2401,21 +2401,21 @@ mulx4x_internal: leaq 32(%rcx),%rcx movq %r13,-16(%rbx) - decq %rdi + decq %rdi // of=0, pass cf jnz .Lmulx4x_1st - movq 8(%rsp),%rax - adcq %rbp,%r15 - leaq (%rsi,%rax,1),%rsi + movq 8(%rsp),%rax // load -num + adcq %rbp,%r15 // modulo-scheduled + leaq (%rsi,%rax,1),%rsi // rewind %rsi addq %r15,%r14 - movq 8+8(%rsp),%rdi - adcq %rbp,%rbp + movq 8+8(%rsp),%rdi // re-load &b[i] + adcq %rbp,%rbp // top-most carry movq %r14,-8(%rbx) jmp .Lmulx4x_outer .align 32 .Lmulx4x_outer: - leaq 16-256(%rbx),%r10 + leaq 16-256(%rbx),%r10 // where 256-byte mask is (+density control) pxor %xmm4,%xmm4 .byte 0x67,0x67 pxor %xmm5,%xmm5 @@ -2468,41 +2468,41 @@ mulx4x_internal: por %xmm2,%xmm4 por %xmm3,%xmm5 por %xmm5,%xmm4 - - pshufd $0x4e,%xmm4,%xmm0 +// Combine the upper and lower halves of %xmm4 as %xmm0. + pshufd $0x4e,%xmm4,%xmm0 // Swap upper and lower halves. por %xmm4,%xmm0 leaq 256(%rdi),%rdi - movq %xmm0,%rdx + movq %xmm0,%rdx // m0=bp[i] - movq %rbp,(%rbx) - leaq 32(%rbx,%rax,1),%rbx - mulxq 0(%rsi),%r8,%r11 - xorq %rbp,%rbp + movq %rbp,(%rbx) // save top-most carry + leaq 32(%rbx,%rax,1),%rbx // rewind %rbx + mulxq 0(%rsi),%r8,%r11 // a[0]*b[i] + xorq %rbp,%rbp // cf=0, of=0 movq %rdx,%r9 - mulxq 8(%rsi),%r14,%r12 - adoxq -32(%rbx),%r8 + mulxq 8(%rsi),%r14,%r12 // a[1]*b[i] + adoxq -32(%rbx),%r8 // +t[0] adcxq %r14,%r11 - mulxq 16(%rsi),%r15,%r13 + mulxq 16(%rsi),%r15,%r13 // ... adoxq -24(%rbx),%r11 adcxq %r15,%r12 mulxq 24(%rsi),%rdx,%r14 adoxq -16(%rbx),%r12 adcxq %rdx,%r13 - leaq (%rcx,%rax,1),%rcx + leaq (%rcx,%rax,1),%rcx // rewind %rcx leaq 32(%rsi),%rsi adoxq -8(%rbx),%r13 adcxq %rbp,%r14 adoxq %rbp,%r14 movq %r8,%r15 - imulq 32+8(%rsp),%r8 + imulq 32+8(%rsp),%r8 // "t[0]"*n0 movq %r8,%rdx - xorq %rbp,%rbp - movq %rdi,8+8(%rsp) + xorq %rbp,%rbp // cf=0, of=0 + movq %rdi,8+8(%rsp) // off-load &b[i] mulxq 0(%rcx),%rax,%r10 - adcxq %rax,%r15 + adcxq %rax,%r15 // discarded adoxq %r11,%r10 mulxq 8(%rcx),%rax,%r11 adcxq %rax,%r10 @@ -2512,24 +2512,24 @@ mulx4x_internal: adoxq %r13,%r12 mulxq 24(%rcx),%rax,%r15 movq %r9,%rdx - movq 24+8(%rsp),%rdi + movq 24+8(%rsp),%rdi // counter value movq %r10,-32(%rbx) adcxq %rax,%r12 movq %r11,-24(%rbx) - adoxq %rbp,%r15 + adoxq %rbp,%r15 // of=0 movq %r12,-16(%rbx) leaq 32(%rcx),%rcx jmp .Lmulx4x_inner .align 32 .Lmulx4x_inner: - mulxq 0(%rsi),%r10,%rax - adcxq %rbp,%r15 + mulxq 0(%rsi),%r10,%rax // a[4]*b[i] + adcxq %rbp,%r15 // cf=0, modulo-scheduled adoxq %r14,%r10 - mulxq 8(%rsi),%r11,%r14 + mulxq 8(%rsi),%r11,%r14 // a[5]*b[i] adcxq 0(%rbx),%r10 adoxq %rax,%r11 - mulxq 16(%rsi),%r12,%rax + mulxq 16(%rsi),%r12,%rax // ... adcxq 8(%rbx),%r11 adoxq %r14,%r12 mulxq 24(%rsi),%r13,%r14 @@ -2537,10 +2537,10 @@ mulx4x_internal: adcxq 16(%rbx),%r12 adoxq %rax,%r13 adcxq 24(%rbx),%r13 - adoxq %rbp,%r14 + adoxq %rbp,%r14 // of=0 leaq 32(%rsi),%rsi leaq 32(%rbx),%rbx - adcxq %rbp,%r14 + adcxq %rbp,%r14 // cf=0 adoxq %r15,%r10 mulxq 0(%rcx),%rax,%r15 @@ -2562,17 +2562,17 @@ mulx4x_internal: adoxq %rbp,%r15 movq %r13,-16(%rbx) - decq %rdi + decq %rdi // of=0, pass cf jnz .Lmulx4x_inner - movq 0+8(%rsp),%rax - adcq %rbp,%r15 - subq 0(%rbx),%rdi - movq 8+8(%rsp),%rdi + movq 0+8(%rsp),%rax // load -num + adcq %rbp,%r15 // modulo-scheduled + subq 0(%rbx),%rdi // pull top-most carry to %cf + movq 8+8(%rsp),%rdi // re-load &b[i] movq 16+8(%rsp),%r10 adcq %r15,%r14 - leaq (%rsi,%rax,1),%rsi - adcq %rbp,%rbp + leaq (%rsi,%rax,1),%rsi // rewind %rsi + adcq %rbp,%rbp // top-most carry movq %r14,-8(%rbx) cmpq %r10,%rdi @@ -2581,23 +2581,23 @@ mulx4x_internal: movq -8(%rcx),%r10 movq %rbp,%r8 movq (%rcx,%rax,1),%r12 - leaq (%rcx,%rax,1),%rbp + leaq (%rcx,%rax,1),%rbp // rewind %rcx movq %rax,%rcx - leaq (%rbx,%rax,1),%rdi + leaq (%rbx,%rax,1),%rdi // rewind %rbx xorl %eax,%eax xorq %r15,%r15 - subq %r14,%r10 + subq %r14,%r10 // compare top-most words adcq %r15,%r15 orq %r15,%r8 sarq $3+2,%rcx - subq %r8,%rax - movq 56+8(%rsp),%rdx - decq %r12 + subq %r8,%rax // %rax=-%r8 + movq 56+8(%rsp),%rdx // restore rp + decq %r12 // so that after 'not' we get -n[0] movq 8(%rbp),%r13 xorq %r8,%r8 movq 16(%rbp),%r14 movq 24(%rbp),%r15 - jmp .Lsqrx4x_sub_entry + jmp .Lsqrx4x_sub_entry // common post-condition .cfi_endproc .size mulx4x_internal,.-mulx4x_internal .globl bn_powerx5 @@ -2623,35 +2623,35 @@ _CET_ENDBR .cfi_offset %r15,-56 .Lpowerx5_prologue: - - - - shll $3,%r9d - leaq (%r9,%r9,2),%r10 +// num is declared as an int, a 32-bit parameter, so the upper half is +// undefined. It is important that this write to %r9, which zeros the +// upper half, predates the first access. + shll $3,%r9d // convert %r9 to bytes + leaq (%r9,%r9,2),%r10 // 3*%r9 in bytes negq %r9 - movq (%r8),%r8 - - - - - - - - + movq (%r8),%r8 // *n0 + +// ############################################################# +// Ensure that stack frame doesn't alias with %rdi+3*%r9 +// modulo 4096, which covers ret[num], am[num] and n[num] +// (see bn_exp.c). This is done to allow memory disambiguation +// logic do its magic. [Extra 256 bytes is for power mask +// calculated from 7th argument, the index.] +// leaq -320(%rsp,%r9,2),%r11 movq %rsp,%rbp subq %rdi,%r11 andq $4095,%r11 cmpq %r11,%r10 jb .Lpwrx_sp_alt - subq %r11,%rbp - leaq -320(%rbp,%r9,2),%rbp + subq %r11,%rbp // align with %rsi + leaq -320(%rbp,%r9,2),%rbp // future alloca(frame+2*%r9*8+256) jmp .Lpwrx_sp_done .align 32 .Lpwrx_sp_alt: leaq 4096-320(,%r9,2),%r10 - leaq -320(%rbp,%r9,2),%rbp + leaq -320(%rbp,%r9,2),%rbp // alloca(frame+2*%r9*8+256) subq %r10,%r11 movq $0,%r10 cmovcq %r10,%r11 @@ -2677,24 +2677,24 @@ _CET_ENDBR movq %r9,%r10 negq %r9 - - - - - - - - - - - +// ############################################################# +// Stack layout +// +// +0 saved %r9, used in reduction section +// +8 &t[2*%r9], used in reduction section +// +16 intermediate carry bit +// +24 top-most carry bit, used in reduction section +// +32 saved *n0 +// +40 saved %rsp +// +48 t[2*%r9] +// pxor %xmm0,%xmm0 - movq %rdi,%xmm1 - movq %rcx,%xmm2 - movq %r10,%xmm3 + movq %rdi,%xmm1 // save %rdi + movq %rcx,%xmm2 // save %rcx + movq %r10,%xmm3 // -%r9 movq %rdx,%xmm4 movq %r8,32(%rsp) - movq %rax,40(%rsp) + movq %rax,40(%rsp) // save original %rsp .cfi_escape 0x0f,0x05,0x77,0x28,0x06,0x23,0x08 .Lpowerx5_body: @@ -2709,7 +2709,7 @@ _CET_ENDBR call __bn_sqrx8x_internal call __bn_postx4x_internal - movq %r10,%r9 + movq %r10,%r9 // -num movq %rsi,%rdi movq %xmm2,%rcx movq %xmm4,%rdx @@ -2717,7 +2717,7 @@ _CET_ENDBR call mulx4x_internal - movq 40(%rsp),%rsi + movq 40(%rsp),%rsi // restore %rsp .cfi_def_cfa %rsi,8 movq $1,%rax @@ -2749,50 +2749,50 @@ bn_sqrx8x_internal: __bn_sqrx8x_internal: .cfi_startproc _CET_ENDBR - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +// ################################################################# +// Squaring part: +// +// a) multiply-n-add everything but a[i]*a[i]; +// b) shift result of a) by 1 to the left and accumulate +// a[i]*a[i] products; +// +// ################################################################# +// a[7]a[7]a[6]a[6]a[5]a[5]a[4]a[4]a[3]a[3]a[2]a[2]a[1]a[1]a[0]a[0] +// a[1]a[0] +// a[2]a[0] +// a[3]a[0] +// a[2]a[1] +// a[3]a[1] +// a[3]a[2] +// +// a[4]a[0] +// a[5]a[0] +// a[6]a[0] +// a[7]a[0] +// a[4]a[1] +// a[5]a[1] +// a[6]a[1] +// a[7]a[1] +// a[4]a[2] +// a[5]a[2] +// a[6]a[2] +// a[7]a[2] +// a[4]a[3] +// a[5]a[3] +// a[6]a[3] +// a[7]a[3] +// +// a[5]a[4] +// a[6]a[4] +// a[7]a[4] +// a[6]a[5] +// a[7]a[5] +// a[7]a[6] +// a[7]a[7]a[6]a[6]a[5]a[5]a[4]a[4]a[3]a[3]a[2]a[2]a[1]a[1]a[0]a[0] leaq 48+8(%rsp),%rdi leaq (%rsi,%r9,1),%rbp - movq %r9,0+8(%rsp) - movq %rbp,8+8(%rsp) + movq %r9,0+8(%rsp) // save %r9 + movq %rbp,8+8(%rsp) // save end of %rsi jmp .Lsqr8x_zero_start .align 32 @@ -2803,7 +2803,7 @@ _CET_ENDBR movdqa %xmm0,16(%rdi) movdqa %xmm0,32(%rdi) movdqa %xmm0,48(%rdi) -.Lsqr8x_zero_start: +.Lsqr8x_zero_start: // aligned at 32 movdqa %xmm0,64(%rdi) movdqa %xmm0,80(%rdi) movdqa %xmm0,96(%rdi) @@ -2812,8 +2812,8 @@ _CET_ENDBR subq $64,%r9 jnz .Lsqrx8x_zero - movq 0(%rsi),%rdx - + movq 0(%rsi),%rdx // a[0], modulo-scheduled +// xor %r9,%r9 # t[1], ex-%r9, zero already xorq %r10,%r10 xorq %r11,%r11 xorq %r12,%r12 @@ -2821,21 +2821,21 @@ _CET_ENDBR xorq %r14,%r14 xorq %r15,%r15 leaq 48+8(%rsp),%rdi - xorq %rbp,%rbp + xorq %rbp,%rbp // cf=0, cf=0 jmp .Lsqrx8x_outer_loop .align 32 .Lsqrx8x_outer_loop: - mulxq 8(%rsi),%r8,%rax - adcxq %r9,%r8 + mulxq 8(%rsi),%r8,%rax // a[1]*a[0] + adcxq %r9,%r8 // a[1]*a[0]+=t[1] adoxq %rax,%r10 - mulxq 16(%rsi),%r9,%rax + mulxq 16(%rsi),%r9,%rax // a[2]*a[0] adcxq %r10,%r9 adoxq %rax,%r11 -.byte 0xc4,0xe2,0xab,0xf6,0x86,0x18,0x00,0x00,0x00 +.byte 0xc4,0xe2,0xab,0xf6,0x86,0x18,0x00,0x00,0x00 // mulx 3*8(%rsi),%r10,%rax # ... adcxq %r11,%r10 adoxq %rax,%r12 -.byte 0xc4,0xe2,0xa3,0xf6,0x86,0x20,0x00,0x00,0x00 +.byte 0xc4,0xe2,0xa3,0xf6,0x86,0x20,0x00,0x00,0x00 // mulx 4*8(%rsi),%r11,%rax adcxq %r12,%r11 adoxq %rax,%r13 mulxq 40(%rsi),%r12,%rax @@ -2845,97 +2845,97 @@ _CET_ENDBR adcxq %r14,%r13 adoxq %r15,%rax mulxq 56(%rsi),%r14,%r15 - movq 8(%rsi),%rdx + movq 8(%rsi),%rdx // a[1] adcxq %rax,%r14 adoxq %rbp,%r15 adcq 64(%rdi),%r15 - movq %r8,8(%rdi) - movq %r9,16(%rdi) - sbbq %rcx,%rcx - xorq %rbp,%rbp + movq %r8,8(%rdi) // t[1] + movq %r9,16(%rdi) // t[2] + sbbq %rcx,%rcx // mov %cf,%rcx + xorq %rbp,%rbp // cf=0, of=0 - mulxq 16(%rsi),%r8,%rbx - mulxq 24(%rsi),%r9,%rax + mulxq 16(%rsi),%r8,%rbx // a[2]*a[1] + mulxq 24(%rsi),%r9,%rax // a[3]*a[1] adcxq %r10,%r8 adoxq %rbx,%r9 - mulxq 32(%rsi),%r10,%rbx + mulxq 32(%rsi),%r10,%rbx // ... adcxq %r11,%r9 adoxq %rax,%r10 -.byte 0xc4,0xe2,0xa3,0xf6,0x86,0x28,0x00,0x00,0x00 +.byte 0xc4,0xe2,0xa3,0xf6,0x86,0x28,0x00,0x00,0x00 // mulx 5*8(%rsi),%r11,%rax adcxq %r12,%r10 adoxq %rbx,%r11 -.byte 0xc4,0xe2,0x9b,0xf6,0x9e,0x30,0x00,0x00,0x00 +.byte 0xc4,0xe2,0x9b,0xf6,0x9e,0x30,0x00,0x00,0x00 // mulx 6*8(%rsi),%r12,%rbx adcxq %r13,%r11 adoxq %r14,%r12 -.byte 0xc4,0x62,0x93,0xf6,0xb6,0x38,0x00,0x00,0x00 - movq 16(%rsi),%rdx +.byte 0xc4,0x62,0x93,0xf6,0xb6,0x38,0x00,0x00,0x00 // mulx 7*8(%rsi),%r13,%r14 + movq 16(%rsi),%rdx // a[2] adcxq %rax,%r12 adoxq %rbx,%r13 adcxq %r15,%r13 - adoxq %rbp,%r14 - adcxq %rbp,%r14 + adoxq %rbp,%r14 // of=0 + adcxq %rbp,%r14 // cf=0 - movq %r8,24(%rdi) - movq %r9,32(%rdi) + movq %r8,24(%rdi) // t[3] + movq %r9,32(%rdi) // t[4] - mulxq 24(%rsi),%r8,%rbx - mulxq 32(%rsi),%r9,%rax + mulxq 24(%rsi),%r8,%rbx // a[3]*a[2] + mulxq 32(%rsi),%r9,%rax // a[4]*a[2] adcxq %r10,%r8 adoxq %rbx,%r9 - mulxq 40(%rsi),%r10,%rbx + mulxq 40(%rsi),%r10,%rbx // ... adcxq %r11,%r9 adoxq %rax,%r10 -.byte 0xc4,0xe2,0xa3,0xf6,0x86,0x30,0x00,0x00,0x00 +.byte 0xc4,0xe2,0xa3,0xf6,0x86,0x30,0x00,0x00,0x00 // mulx 6*8(%rsi),%r11,%rax adcxq %r12,%r10 adoxq %r13,%r11 -.byte 0xc4,0x62,0x9b,0xf6,0xae,0x38,0x00,0x00,0x00 +.byte 0xc4,0x62,0x9b,0xf6,0xae,0x38,0x00,0x00,0x00 // mulx 7*8(%rsi),%r12,%r13 .byte 0x3e - movq 24(%rsi),%rdx + movq 24(%rsi),%rdx // a[3] adcxq %rbx,%r11 adoxq %rax,%r12 adcxq %r14,%r12 - movq %r8,40(%rdi) - movq %r9,48(%rdi) - mulxq 32(%rsi),%r8,%rax - adoxq %rbp,%r13 - adcxq %rbp,%r13 + movq %r8,40(%rdi) // t[5] + movq %r9,48(%rdi) // t[6] + mulxq 32(%rsi),%r8,%rax // a[4]*a[3] + adoxq %rbp,%r13 // of=0 + adcxq %rbp,%r13 // cf=0 - mulxq 40(%rsi),%r9,%rbx + mulxq 40(%rsi),%r9,%rbx // a[5]*a[3] adcxq %r10,%r8 adoxq %rax,%r9 - mulxq 48(%rsi),%r10,%rax + mulxq 48(%rsi),%r10,%rax // ... adcxq %r11,%r9 adoxq %r12,%r10 mulxq 56(%rsi),%r11,%r12 - movq 32(%rsi),%rdx - movq 40(%rsi),%r14 + movq 32(%rsi),%rdx // a[4] + movq 40(%rsi),%r14 // a[5] adcxq %rbx,%r10 adoxq %rax,%r11 - movq 48(%rsi),%r15 + movq 48(%rsi),%r15 // a[6] adcxq %r13,%r11 - adoxq %rbp,%r12 - adcxq %rbp,%r12 + adoxq %rbp,%r12 // of=0 + adcxq %rbp,%r12 // cf=0 - movq %r8,56(%rdi) - movq %r9,64(%rdi) + movq %r8,56(%rdi) // t[7] + movq %r9,64(%rdi) // t[8] - mulxq %r14,%r9,%rax - movq 56(%rsi),%r8 + mulxq %r14,%r9,%rax // a[5]*a[4] + movq 56(%rsi),%r8 // a[7] adcxq %r10,%r9 - mulxq %r15,%r10,%rbx + mulxq %r15,%r10,%rbx // a[6]*a[4] adoxq %rax,%r10 adcxq %r11,%r10 - mulxq %r8,%r11,%rax - movq %r14,%rdx + mulxq %r8,%r11,%rax // a[7]*a[4] + movq %r14,%rdx // a[5] adoxq %rbx,%r11 adcxq %r12,%r11 +// adox %rbp,%rax # of=0 + adcxq %rbp,%rax // cf=0 - adcxq %rbp,%rax - - mulxq %r15,%r14,%rbx - mulxq %r8,%r12,%r13 - movq %r15,%rdx + mulxq %r15,%r14,%rbx // a[6]*a[5] + mulxq %r8,%r12,%r13 // a[7]*a[5] + movq %r15,%rdx // a[6] leaq 64(%rsi),%rsi adcxq %r14,%r11 adoxq %rbx,%r12 @@ -2943,19 +2943,19 @@ _CET_ENDBR adoxq %rbp,%r13 .byte 0x67,0x67 - mulxq %r8,%r8,%r14 + mulxq %r8,%r8,%r14 // a[7]*a[6] adcxq %r8,%r13 adcxq %rbp,%r14 cmpq 8+8(%rsp),%rsi je .Lsqrx8x_outer_break - negq %rcx + negq %rcx // mov %rcx,%cf movq $-8,%rcx movq %rbp,%r15 movq 64(%rdi),%r8 - adcxq 72(%rdi),%r9 - adcxq 80(%rdi),%r10 + adcxq 72(%rdi),%r9 // +=t[9] + adcxq 80(%rdi),%r10 // ... adcxq 88(%rdi),%r11 adcq 96(%rdi),%r12 adcq 104(%rdi),%r13 @@ -2963,24 +2963,24 @@ _CET_ENDBR adcq 120(%rdi),%r15 leaq (%rsi),%rbp leaq 128(%rdi),%rdi - sbbq %rax,%rax + sbbq %rax,%rax // mov %cf,%rcx - movq -64(%rsi),%rdx - movq %rax,16+8(%rsp) + movq -64(%rsi),%rdx // a[0] + movq %rax,16+8(%rsp) // offload %rcx movq %rdi,24+8(%rsp) - - xorl %eax,%eax +// lea 8*8(%rdi),%rdi # see 2*8*8(%rdi) above + xorl %eax,%eax // cf=0, of=0 jmp .Lsqrx8x_loop .align 32 .Lsqrx8x_loop: movq %r8,%rbx - mulxq 0(%rbp),%rax,%r8 - adcxq %rax,%rbx + mulxq 0(%rbp),%rax,%r8 // a[8]*a[i] + adcxq %rax,%rbx // +=t[8] adoxq %r9,%r8 - mulxq 8(%rbp),%rax,%r9 + mulxq 8(%rbp),%rax,%r9 // ... adcxq %rax,%r8 adoxq %r10,%r9 @@ -2992,7 +2992,7 @@ _CET_ENDBR adcxq %rax,%r10 adoxq %r12,%r11 -.byte 0xc4,0x62,0xfb,0xf6,0xa5,0x20,0x00,0x00,0x00 +.byte 0xc4,0x62,0xfb,0xf6,0xa5,0x20,0x00,0x00,0x00 // mulx 4*8(%rbp),%rax,%r12 adcxq %rax,%r11 adoxq %r13,%r12 @@ -3001,27 +3001,27 @@ _CET_ENDBR adoxq %r14,%r13 mulxq 48(%rbp),%rax,%r14 - movq %rbx,(%rdi,%rcx,8) + movq %rbx,(%rdi,%rcx,8) // store t[8+i] movl $0,%ebx adcxq %rax,%r13 adoxq %r15,%r14 -.byte 0xc4,0x62,0xfb,0xf6,0xbd,0x38,0x00,0x00,0x00 - movq 8(%rsi,%rcx,8),%rdx +.byte 0xc4,0x62,0xfb,0xf6,0xbd,0x38,0x00,0x00,0x00 // mulx 7*8(%rbp),%rax,%r15 + movq 8(%rsi,%rcx,8),%rdx // a[i] adcxq %rax,%r14 - adoxq %rbx,%r15 - adcxq %rbx,%r15 + adoxq %rbx,%r15 // %rbx is 0, of=0 + adcxq %rbx,%r15 // cf=0 .byte 0x67 - incq %rcx + incq %rcx // of=0 jnz .Lsqrx8x_loop leaq 64(%rbp),%rbp movq $-8,%rcx - cmpq 8+8(%rsp),%rbp + cmpq 8+8(%rsp),%rbp // done? je .Lsqrx8x_break - subq 16+8(%rsp),%rbx + subq 16+8(%rsp),%rbx // mov 16(%rsp),%cf .byte 0x66 movq -64(%rsi),%rdx adcxq 0(%rdi),%r8 @@ -3034,19 +3034,19 @@ _CET_ENDBR adcq 56(%rdi),%r15 leaq 64(%rdi),%rdi .byte 0x67 - sbbq %rax,%rax - xorl %ebx,%ebx - movq %rax,16+8(%rsp) + sbbq %rax,%rax // mov %cf,%rax + xorl %ebx,%ebx // cf=0, of=0 + movq %rax,16+8(%rsp) // offload carry jmp .Lsqrx8x_loop .align 32 .Lsqrx8x_break: xorq %rbp,%rbp - subq 16+8(%rsp),%rbx + subq 16+8(%rsp),%rbx // mov 16(%rsp),%cf adcxq %rbp,%r8 - movq 24+8(%rsp),%rcx + movq 24+8(%rsp),%rcx // initial %rdi, borrow %rcx adcxq %rbp,%r9 - movq 0(%rsi),%rdx + movq 0(%rsi),%rdx // a[8], modulo-scheduled adcq $0,%r10 movq %r8,0(%rdi) adcq $0,%r11 @@ -3054,7 +3054,7 @@ _CET_ENDBR adcq $0,%r13 adcq $0,%r14 adcq $0,%r15 - cmpq %rcx,%rdi + cmpq %rcx,%rdi // cf=0, of=0 je .Lsqrx8x_outer_loop movq %r9,8(%rdi) @@ -3076,57 +3076,57 @@ _CET_ENDBR .align 32 .Lsqrx8x_outer_break: - movq %r9,72(%rdi) - movq %xmm3,%rcx - movq %r10,80(%rdi) + movq %r9,72(%rdi) // t[9] + movq %xmm3,%rcx // -%r9 + movq %r10,80(%rdi) // ... movq %r11,88(%rdi) movq %r12,96(%rdi) movq %r13,104(%rdi) movq %r14,112(%rdi) leaq 48+8(%rsp),%rdi - movq (%rsi,%rcx,1),%rdx + movq (%rsi,%rcx,1),%rdx // a[0] - movq 8(%rdi),%r11 - xorq %r10,%r10 - movq 0+8(%rsp),%r9 + movq 8(%rdi),%r11 // t[1] + xorq %r10,%r10 // t[0], of=0, cf=0 + movq 0+8(%rsp),%r9 // restore %r9 adoxq %r11,%r11 - movq 16(%rdi),%r12 - movq 24(%rdi),%r13 - + movq 16(%rdi),%r12 // t[2] # prefetch + movq 24(%rdi),%r13 // t[3] # prefetch +// jmp .Lsqrx4x_shift_n_add # happens to be aligned .align 32 .Lsqrx4x_shift_n_add: mulxq %rdx,%rax,%rbx adoxq %r12,%r12 adcxq %r10,%rax -.byte 0x48,0x8b,0x94,0x0e,0x08,0x00,0x00,0x00 -.byte 0x4c,0x8b,0x97,0x20,0x00,0x00,0x00 +.byte 0x48,0x8b,0x94,0x0e,0x08,0x00,0x00,0x00 // mov 8(%rsi,%rcx),%rdx # a[i+1] # prefetch +.byte 0x4c,0x8b,0x97,0x20,0x00,0x00,0x00 // mov 32(%rdi),%r10 # t[2*i+4] # prefetch adoxq %r13,%r13 adcxq %r11,%rbx - movq 40(%rdi),%r11 + movq 40(%rdi),%r11 // t[2*i+4+1] # prefetch movq %rax,0(%rdi) movq %rbx,8(%rdi) mulxq %rdx,%rax,%rbx adoxq %r10,%r10 adcxq %r12,%rax - movq 16(%rsi,%rcx,1),%rdx - movq 48(%rdi),%r12 + movq 16(%rsi,%rcx,1),%rdx // a[i+2] # prefetch + movq 48(%rdi),%r12 // t[2*i+6] # prefetch adoxq %r11,%r11 adcxq %r13,%rbx - movq 56(%rdi),%r13 + movq 56(%rdi),%r13 // t[2*i+6+1] # prefetch movq %rax,16(%rdi) movq %rbx,24(%rdi) mulxq %rdx,%rax,%rbx adoxq %r12,%r12 adcxq %r10,%rax - movq 24(%rsi,%rcx,1),%rdx + movq 24(%rsi,%rcx,1),%rdx // a[i+3] # prefetch leaq 32(%rcx),%rcx - movq 64(%rdi),%r10 + movq 64(%rdi),%r10 // t[2*i+8] # prefetch adoxq %r13,%r13 adcxq %r11,%rbx - movq 72(%rdi),%r11 + movq 72(%rdi),%r11 // t[2*i+8+1] # prefetch movq %rax,32(%rdi) movq %rbx,40(%rdi) @@ -3134,11 +3134,11 @@ _CET_ENDBR adoxq %r10,%r10 adcxq %r12,%rax jrcxz .Lsqrx4x_shift_n_add_break -.byte 0x48,0x8b,0x94,0x0e,0x00,0x00,0x00,0x00 +.byte 0x48,0x8b,0x94,0x0e,0x00,0x00,0x00,0x00 // mov 0(%rsi,%rcx),%rdx # a[i+4] # prefetch adoxq %r11,%r11 adcxq %r13,%rbx - movq 80(%rdi),%r12 - movq 88(%rdi),%r13 + movq 80(%rdi),%r12 // t[2*i+10] # prefetch + movq 88(%rdi),%r13 // t[2*i+10+1] # prefetch movq %rax,48(%rdi) movq %rbx,56(%rdi) leaq 64(%rdi),%rdi @@ -3150,18 +3150,18 @@ _CET_ENDBR adcxq %r13,%rbx movq %rax,48(%rdi) movq %rbx,56(%rdi) - leaq 64(%rdi),%rdi + leaq 64(%rdi),%rdi // end of t[] buffer movq %xmm2,%rbp __bn_sqrx8x_reduction: - xorl %eax,%eax - movq 32+8(%rsp),%rbx - movq 48+8(%rsp),%rdx - leaq -64(%rbp,%r9,1),%rcx - - movq %rcx,0+8(%rsp) - movq %rdi,8+8(%rsp) - - leaq 48+8(%rsp),%rdi + xorl %eax,%eax // initial top-most carry bit + movq 32+8(%rsp),%rbx // n0 + movq 48+8(%rsp),%rdx // "%r8", 8*0(%rdi) + leaq -64(%rbp,%r9,1),%rcx // end of n[] +// lea 48+8(%rsp,%r9,2),%rdi # end of t[] buffer + movq %rcx,0+8(%rsp) // save end of n[] + movq %rdi,8+8(%rsp) // save end of t[] + + leaq 48+8(%rsp),%rdi // initial t[] window jmp .Lsqrx8x_reduction_loop .align 32 @@ -3171,25 +3171,25 @@ __bn_sqrx8x_reduction: movq 24(%rdi),%r11 movq 32(%rdi),%r12 movq %rdx,%r8 - imulq %rbx,%rdx + imulq %rbx,%rdx // n0*a[i] movq 40(%rdi),%r13 movq 48(%rdi),%r14 movq 56(%rdi),%r15 - movq %rax,24+8(%rsp) + movq %rax,24+8(%rsp) // store top-most carry bit leaq 64(%rdi),%rdi - xorq %rsi,%rsi + xorq %rsi,%rsi // cf=0,of=0 movq $-8,%rcx jmp .Lsqrx8x_reduce .align 32 .Lsqrx8x_reduce: movq %r8,%rbx - mulxq 0(%rbp),%rax,%r8 - adcxq %rbx,%rax + mulxq 0(%rbp),%rax,%r8 // n[0] + adcxq %rbx,%rax // discarded adoxq %r9,%r8 - mulxq 8(%rbp),%rbx,%r9 + mulxq 8(%rbp),%rbx,%r9 // n[1] adcxq %rbx,%r8 adoxq %r10,%r9 @@ -3201,15 +3201,15 @@ __bn_sqrx8x_reduction: adcxq %rbx,%r10 adoxq %r12,%r11 -.byte 0xc4,0x62,0xe3,0xf6,0xa5,0x20,0x00,0x00,0x00 +.byte 0xc4,0x62,0xe3,0xf6,0xa5,0x20,0x00,0x00,0x00 // mulx 8*4(%rbp),%rbx,%r12 movq %rdx,%rax movq %r8,%rdx adcxq %rbx,%r11 adoxq %r13,%r12 - mulxq 32+8(%rsp),%rbx,%rdx + mulxq 32+8(%rsp),%rbx,%rdx // %rdx discarded movq %rax,%rdx - movq %rax,64+48+8(%rsp,%rcx,8) + movq %rax,64+48+8(%rsp,%rcx,8) // put aside n0*a[i] mulxq 40(%rbp),%rax,%r13 adcxq %rax,%r12 @@ -3222,18 +3222,18 @@ __bn_sqrx8x_reduction: mulxq 56(%rbp),%rax,%r15 movq %rbx,%rdx adcxq %rax,%r14 - adoxq %rsi,%r15 - adcxq %rsi,%r15 + adoxq %rsi,%r15 // %rsi is 0 + adcxq %rsi,%r15 // cf=0 .byte 0x67,0x67,0x67 - incq %rcx + incq %rcx // of=0 jnz .Lsqrx8x_reduce - movq %rsi,%rax - cmpq 0+8(%rsp),%rbp + movq %rsi,%rax // xor %rax,%rax + cmpq 0+8(%rsp),%rbp // end of n[]? jae .Lsqrx8x_no_tail - movq 48+8(%rsp),%rdx + movq 48+8(%rsp),%rdx // pull n0*a[0] addq 0(%rdi),%r8 leaq 64(%rbp),%rbp movq $-8,%rcx @@ -3245,9 +3245,9 @@ __bn_sqrx8x_reduction: adcq 48(%rdi),%r14 adcq 56(%rdi),%r15 leaq 64(%rdi),%rdi - sbbq %rax,%rax + sbbq %rax,%rax // top carry - xorq %rsi,%rsi + xorq %rsi,%rsi // of=0, cf=0 movq %rax,16+8(%rsp) jmp .Lsqrx8x_tail @@ -3270,7 +3270,7 @@ __bn_sqrx8x_reduction: adcxq %rax,%r10 adoxq %r12,%r11 -.byte 0xc4,0x62,0xfb,0xf6,0xa5,0x20,0x00,0x00,0x00 +.byte 0xc4,0x62,0xfb,0xf6,0xa5,0x20,0x00,0x00,0x00 // mulx 8*4(%rbp),%rax,%r12 adcxq %rax,%r11 adoxq %r13,%r12 @@ -3283,21 +3283,21 @@ __bn_sqrx8x_reduction: adoxq %r15,%r14 mulxq 56(%rbp),%rax,%r15 - movq 72+48+8(%rsp,%rcx,8),%rdx + movq 72+48+8(%rsp,%rcx,8),%rdx // pull n0*a[i] adcxq %rax,%r14 adoxq %rsi,%r15 - movq %rbx,(%rdi,%rcx,8) + movq %rbx,(%rdi,%rcx,8) // save result movq %r8,%rbx - adcxq %rsi,%r15 + adcxq %rsi,%r15 // cf=0 - incq %rcx + incq %rcx // of=0 jnz .Lsqrx8x_tail - cmpq 0+8(%rsp),%rbp - jae .Lsqrx8x_tail_done + cmpq 0+8(%rsp),%rbp // end of n[]? + jae .Lsqrx8x_tail_done // break out of loop - subq 16+8(%rsp),%rsi - movq 48+8(%rsp),%rdx + subq 16+8(%rsp),%rsi // mov 16(%rsp),%cf + movq 48+8(%rsp),%rdx // pull n0*a[0] leaq 64(%rbp),%rbp adcq 0(%rdi),%r8 adcq 8(%rdi),%r9 @@ -3309,16 +3309,16 @@ __bn_sqrx8x_reduction: adcq 56(%rdi),%r15 leaq 64(%rdi),%rdi sbbq %rax,%rax - subq $8,%rcx + subq $8,%rcx // mov $-8,%rcx - xorq %rsi,%rsi + xorq %rsi,%rsi // of=0, cf=0 movq %rax,16+8(%rsp) jmp .Lsqrx8x_tail .align 32 .Lsqrx8x_tail_done: xorq %rax,%rax - addq 24+8(%rsp),%r8 + addq 24+8(%rsp),%r8 // can this overflow? adcq $0,%r9 adcq $0,%r10 adcq $0,%r11 @@ -3328,26 +3328,26 @@ __bn_sqrx8x_reduction: adcq $0,%r15 adcq $0,%rax - subq 16+8(%rsp),%rsi -.Lsqrx8x_no_tail: + subq 16+8(%rsp),%rsi // mov 16(%rsp),%cf +.Lsqrx8x_no_tail: // %cf is 0 if jumped here adcq 0(%rdi),%r8 movq %xmm3,%rcx adcq 8(%rdi),%r9 movq 56(%rbp),%rsi - movq %xmm2,%rbp + movq %xmm2,%rbp // restore %rbp adcq 16(%rdi),%r10 adcq 24(%rdi),%r11 adcq 32(%rdi),%r12 adcq 40(%rdi),%r13 adcq 48(%rdi),%r14 adcq 56(%rdi),%r15 - adcq $0,%rax + adcq $0,%rax // top-most carry - movq 32+8(%rsp),%rbx - movq 64(%rdi,%rcx,1),%rdx + movq 32+8(%rsp),%rbx // n0 + movq 64(%rdi,%rcx,1),%rdx // modulo-scheduled "%r8" - movq %r8,0(%rdi) - leaq 64(%rdi),%r8 + movq %r8,0(%rdi) // store top 512 bits + leaq 64(%rdi),%r8 // borrow %r8 movq %r9,8(%rdi) movq %r10,16(%rdi) movq %r11,24(%rdi) @@ -3356,8 +3356,8 @@ __bn_sqrx8x_reduction: movq %r14,48(%rdi) movq %r15,56(%rdi) - leaq 64(%rdi,%rcx,1),%rdi - cmpq 8+8(%rsp),%r8 + leaq 64(%rdi,%rcx,1),%rdi // start of current t[] window + cmpq 8+8(%rsp),%r8 // end of t[]? jb .Lsqrx8x_reduction_loop ret .cfi_endproc @@ -3367,14 +3367,14 @@ __bn_sqrx8x_reduction: __bn_postx4x_internal: .cfi_startproc movq 0(%rbp),%r12 - movq %rcx,%r10 - movq %rcx,%r9 + movq %rcx,%r10 // -%r9 + movq %rcx,%r9 // -%r9 negq %rax sarq $3+2,%rcx - - movq %xmm1,%rdx - movq %xmm1,%rsi - decq %r12 +// lea 48+8(%rsp,%r9),%rdi + movq %xmm1,%rdx // restore %rdx + movq %xmm1,%rsi // prepare for back-to-back call + decq %r12 // so that after 'not' we get -n[0] movq 8(%rbp),%r13 xorq %r8,%r8 movq 16(%rbp),%r14 @@ -3394,7 +3394,7 @@ __bn_postx4x_internal: andnq %rax,%r14,%r14 andnq %rax,%r15,%r15 - negq %r8 + negq %r8 // mov %r8,%cf adcq 0(%rdi),%r12 adcq 8(%rdi),%r13 adcq 16(%rdi),%r14 @@ -3402,7 +3402,7 @@ __bn_postx4x_internal: movq %r12,0(%rdx) leaq 32(%rdi),%rdi movq %r13,8(%rdx) - sbbq %r8,%r8 + sbbq %r8,%r8 // mov %cf,%r8 movq %r14,16(%rdx) movq %r15,24(%rdx) leaq 32(%rdx),%rdx @@ -3410,7 +3410,7 @@ __bn_postx4x_internal: incq %rcx jnz .Lsqrx4x_sub - negq %r9 + negq %r9 // restore %r9 ret .cfi_endproc @@ -3425,13 +3425,13 @@ _CET_ENDBR cmpl $0,%esi jz .Lscatter_epilogue - - - - - - - +// %rdx stores 32 entries, t0 through t31. Each entry has %esi words. +// They are interleaved in memory as follows: +// +// t0[0] t1[0] t2[0] ... t31[0] +// t0[1] t1[1] t2[1] ... t31[1] +// ... +// t0[%esi-1] t1[%esi-1] t2[%esi-1] ... t31[%esi-1] leaq (%rdx,%rcx,8),%rdx .Lscatter: @@ -3452,97 +3452,97 @@ _CET_ENDBR .align 32 bn_gather5: .cfi_startproc -.LSEH_begin_bn_gather5: +.LSEH_begin_bn_gather5: // Win64 thing, but harmless in other cases _CET_ENDBR - -.byte 0x4c,0x8d,0x14,0x24 +// I can't trust assembler to use specific encoding:-( +.byte 0x4c,0x8d,0x14,0x24 // lea (%rsp),%r10 .cfi_def_cfa_register %r10 -.byte 0x48,0x81,0xec,0x08,0x01,0x00,0x00 +.byte 0x48,0x81,0xec,0x08,0x01,0x00,0x00 // sub $0x108,%rsp leaq .Linc(%rip),%rax - andq $-16,%rsp + andq $-16,%rsp // shouldn't be formally required movd %ecx,%xmm5 - movdqa 0(%rax),%xmm0 - movdqa 16(%rax),%xmm1 - leaq 128(%rdx),%r11 - leaq 128(%rsp),%rax + movdqa 0(%rax),%xmm0 // 00000001000000010000000000000000 + movdqa 16(%rax),%xmm1 // 00000002000000020000000200000002 + leaq 128(%rdx),%r11 // size optimization + leaq 128(%rsp),%rax // size optimization - pshufd $0,%xmm5,%xmm5 + pshufd $0,%xmm5,%xmm5 // broadcast %ecx movdqa %xmm1,%xmm4 movdqa %xmm1,%xmm2 paddd %xmm0,%xmm1 - pcmpeqd %xmm5,%xmm0 + pcmpeqd %xmm5,%xmm0 // compare to 1,0 movdqa %xmm4,%xmm3 paddd %xmm1,%xmm2 - pcmpeqd %xmm5,%xmm1 + pcmpeqd %xmm5,%xmm1 // compare to 3,2 movdqa %xmm0,-128(%rax) movdqa %xmm4,%xmm0 paddd %xmm2,%xmm3 - pcmpeqd %xmm5,%xmm2 + pcmpeqd %xmm5,%xmm2 // compare to 5,4 movdqa %xmm1,-112(%rax) movdqa %xmm4,%xmm1 paddd %xmm3,%xmm0 - pcmpeqd %xmm5,%xmm3 + pcmpeqd %xmm5,%xmm3 // compare to 7,6 movdqa %xmm2,-96(%rax) movdqa %xmm4,%xmm2 paddd %xmm0,%xmm1 - pcmpeqd %xmm5,%xmm0 + pcmpeqd %xmm5,%xmm0 // compare to 1,0 movdqa %xmm3,-80(%rax) movdqa %xmm4,%xmm3 paddd %xmm1,%xmm2 - pcmpeqd %xmm5,%xmm1 + pcmpeqd %xmm5,%xmm1 // compare to 3,2 movdqa %xmm0,-64(%rax) movdqa %xmm4,%xmm0 paddd %xmm2,%xmm3 - pcmpeqd %xmm5,%xmm2 + pcmpeqd %xmm5,%xmm2 // compare to 5,4 movdqa %xmm1,-48(%rax) movdqa %xmm4,%xmm1 paddd %xmm3,%xmm0 - pcmpeqd %xmm5,%xmm3 + pcmpeqd %xmm5,%xmm3 // compare to 7,6 movdqa %xmm2,-32(%rax) movdqa %xmm4,%xmm2 paddd %xmm0,%xmm1 - pcmpeqd %xmm5,%xmm0 + pcmpeqd %xmm5,%xmm0 // compare to 1,0 movdqa %xmm3,-16(%rax) movdqa %xmm4,%xmm3 paddd %xmm1,%xmm2 - pcmpeqd %xmm5,%xmm1 + pcmpeqd %xmm5,%xmm1 // compare to 3,2 movdqa %xmm0,0(%rax) movdqa %xmm4,%xmm0 paddd %xmm2,%xmm3 - pcmpeqd %xmm5,%xmm2 + pcmpeqd %xmm5,%xmm2 // compare to 5,4 movdqa %xmm1,16(%rax) movdqa %xmm4,%xmm1 paddd %xmm3,%xmm0 - pcmpeqd %xmm5,%xmm3 + pcmpeqd %xmm5,%xmm3 // compare to 7,6 movdqa %xmm2,32(%rax) movdqa %xmm4,%xmm2 paddd %xmm0,%xmm1 - pcmpeqd %xmm5,%xmm0 + pcmpeqd %xmm5,%xmm0 // compare to 1,0 movdqa %xmm3,48(%rax) movdqa %xmm4,%xmm3 paddd %xmm1,%xmm2 - pcmpeqd %xmm5,%xmm1 + pcmpeqd %xmm5,%xmm1 // compare to 3,2 movdqa %xmm0,64(%rax) movdqa %xmm4,%xmm0 paddd %xmm2,%xmm3 - pcmpeqd %xmm5,%xmm2 + pcmpeqd %xmm5,%xmm2 // compare to 5,4 movdqa %xmm1,80(%rax) movdqa %xmm4,%xmm1 paddd %xmm3,%xmm0 - pcmpeqd %xmm5,%xmm3 + pcmpeqd %xmm5,%xmm3 // compare to 7,6 movdqa %xmm2,96(%rax) movdqa %xmm4,%xmm2 movdqa %xmm3,112(%rax) @@ -3602,10 +3602,10 @@ _CET_ENDBR por %xmm3,%xmm5 por %xmm5,%xmm4 leaq 256(%r11),%r11 - - pshufd $0x4e,%xmm4,%xmm0 +// Combine the upper and lower halves of %xmm0. + pshufd $0x4e,%xmm4,%xmm0 // Swap upper and lower halves. por %xmm4,%xmm0 - movq %xmm0,(%rdi) + movq %xmm0,(%rdi) // m0=bp[0] leaq 8(%rdi),%rdi subl $1,%esi jnz .Lgather @@ -3617,7 +3617,9 @@ _CET_ENDBR .cfi_endproc .size bn_gather5,.-bn_gather5 .section .rodata + .align 64 +mont5_increments: .Linc: .long 0,0, 1,1 .long 2,2, 2,2 diff --git a/third_party/boringssl/gen/bcm/x86_64-mont5-win.asm b/third_party/boringssl/gen/bcm/x86_64-mont5-win.asm index 5d5ec3642..4b73b80cc 100644 --- a/third_party/boringssl/gen/bcm/x86_64-mont5-win.asm +++ b/third_party/boringssl/gen/bcm/x86_64-mont5-win.asm @@ -32,12 +32,12 @@ $L$SEH_begin_bn_mul_mont_gather5_nohw: _CET_ENDBR - - +; num is declared as an int, a 32-bit parameter, so the upper half is +; undefined. Zero the upper half to normalize it. mov r9d,r9d mov rax,rsp - movd xmm5,DWORD[56+rsp] + movd xmm5,DWORD[56+rsp] ; load 7th argument push rbx push rbp @@ -53,18 +53,18 @@ _CET_ENDBR neg r9 mov r11,rsp - lea r10,[((-280))+r9*8+rsp] - neg r9 - and r10,-1024 - - - - - - - - - + lea r10,[((-280))+r9*8+rsp] ; future alloca(8*(num+2)+256+8) + neg r9 ; restore %r9 + and r10,-1024 ; minimize TLB usage + +; An OS-agnostic version of __chkstk. +; +; Some OSes (Windows) insist on stack being "wired" to +; physical memory in strictly sequential manner, i.e. if stack +; allocation spans two pages, then reference to farmost one can +; be punishable by SEGV. But page walking can do good even on +; other OSes, because it guarantees that villain thread hits +; the guard page before it can make damage to innocent one... sub r11,r10 and r11,-4096 lea rsp,[r11*1+r10] @@ -81,35 +81,35 @@ $L$mul_page_walk: $L$mul_page_walk_done: lea r10,[$L$inc] - mov QWORD[8+r9*8+rsp],rax + mov QWORD[8+r9*8+rsp],rax ; tp[num+1]=%rsp $L$mul_body: - lea r12,[128+rdx] - movdqa xmm0,XMMWORD[r10] - movdqa xmm1,XMMWORD[16+r10] - lea r10,[((24-112))+r9*8+rsp] + lea r12,[128+rdx] ; reassign %rdx (+size optimization) + movdqa xmm0,XMMWORD[r10] ; 00000001000000010000000000000000 + movdqa xmm1,XMMWORD[16+r10] ; 00000002000000020000000200000002 + lea r10,[((24-112))+r9*8+rsp] ; place the mask after tp[num+3] (+ICache optimization) and r10,-16 - pshufd xmm5,xmm5,0 + pshufd xmm5,xmm5,0 ; broadcast index movdqa xmm4,xmm1 movdqa xmm2,xmm1 paddd xmm1,xmm0 - pcmpeqd xmm0,xmm5 + pcmpeqd xmm0,xmm5 ; compare to 1,0 DB 0x67 movdqa xmm3,xmm4 paddd xmm2,xmm1 - pcmpeqd xmm1,xmm5 + pcmpeqd xmm1,xmm5 ; compare to 3,2 movdqa XMMWORD[112+r10],xmm0 movdqa xmm0,xmm4 paddd xmm3,xmm2 - pcmpeqd xmm2,xmm5 + pcmpeqd xmm2,xmm5 ; compare to 5,4 movdqa XMMWORD[128+r10],xmm1 movdqa xmm1,xmm4 paddd xmm0,xmm3 - pcmpeqd xmm3,xmm5 + pcmpeqd xmm3,xmm5 ; compare to 7,6 movdqa XMMWORD[144+r10],xmm2 movdqa xmm2,xmm4 @@ -118,17 +118,17 @@ $L$mul_body: movdqa XMMWORD[160+r10],xmm3 movdqa xmm3,xmm4 paddd xmm2,xmm1 - pcmpeqd xmm1,xmm5 + pcmpeqd xmm1,xmm5 ; compare to 3,2 movdqa XMMWORD[176+r10],xmm0 movdqa xmm0,xmm4 paddd xmm3,xmm2 - pcmpeqd xmm2,xmm5 + pcmpeqd xmm2,xmm5 ; compare to 5,4 movdqa XMMWORD[192+r10],xmm1 movdqa xmm1,xmm4 paddd xmm0,xmm3 - pcmpeqd xmm3,xmm5 + pcmpeqd xmm3,xmm5 ; compare to 7,6 movdqa XMMWORD[208+r10],xmm2 movdqa xmm2,xmm4 @@ -137,17 +137,17 @@ $L$mul_body: movdqa XMMWORD[224+r10],xmm3 movdqa xmm3,xmm4 paddd xmm2,xmm1 - pcmpeqd xmm1,xmm5 + pcmpeqd xmm1,xmm5 ; compare to 3,2 movdqa XMMWORD[240+r10],xmm0 movdqa xmm0,xmm4 paddd xmm3,xmm2 - pcmpeqd xmm2,xmm5 + pcmpeqd xmm2,xmm5 ; compare to 5,4 movdqa XMMWORD[256+r10],xmm1 movdqa xmm1,xmm4 paddd xmm0,xmm3 - pcmpeqd xmm3,xmm5 + pcmpeqd xmm3,xmm5 ; compare to 7,6 movdqa XMMWORD[272+r10],xmm2 movdqa xmm2,xmm4 @@ -166,7 +166,7 @@ $L$mul_body: pcmpeqd xmm3,xmm5 movdqa XMMWORD[336+r10],xmm2 - pand xmm0,XMMWORD[64+r12] + pand xmm0,XMMWORD[64+r12] ; while it's still in register pand xmm1,XMMWORD[80+r12] pand xmm2,XMMWORD[96+r12] @@ -211,33 +211,33 @@ $L$mul_body: por xmm0,xmm2 por xmm1,xmm3 por xmm0,xmm1 - - pshufd xmm1,xmm0,0x4e +; Combine the upper and lower halves of %xmm0. + pshufd xmm1,xmm0,0x4e ; Swap upper and lower halves. por xmm0,xmm1 lea r12,[256+r12] - movq rbx,xmm0 + movq rbx,xmm0 ; m0=bp[0] - mov r8,QWORD[r8] + mov r8,QWORD[r8] ; pull n0[0] value mov rax,QWORD[rsi] - xor r14,r14 - xor r15,r15 + xor r14,r14 ; i=0 + xor r15,r15 ; j=0 mov rbp,r8 - mul rbx + mul rbx ; ap[0]*bp[0] mov r10,rax mov rax,QWORD[rcx] - imul rbp,r10 + imul rbp,r10 ; "tp[0]"*n0 mov r11,rdx - mul rbp - add r10,rax + mul rbp ; np[0]*m1 + add r10,rax ; discarded mov rax,QWORD[8+rsi] adc rdx,0 mov r13,rdx - lea r15,[1+r15] + lea r15,[1+r15] ; j++ jmp NEAR $L$1st_enter ALIGN 16 @@ -245,30 +245,30 @@ $L$1st: add r13,rax mov rax,QWORD[r15*8+rsi] adc rdx,0 - add r13,r11 + add r13,r11 ; np[j]*m1+ap[j]*bp[0] mov r11,r10 adc rdx,0 - mov QWORD[((-16))+r15*8+rsp],r13 + mov QWORD[((-16))+r15*8+rsp],r13 ; tp[j-1] mov r13,rdx $L$1st_enter: - mul rbx + mul rbx ; ap[j]*bp[0] add r11,rax mov rax,QWORD[r15*8+rcx] adc rdx,0 - lea r15,[1+r15] + lea r15,[1+r15] ; j++ mov r10,rdx - mul rbp + mul rbp ; np[j]*m1 cmp r15,r9 - jne NEAR $L$1st - + jne NEAR $L$1st ; note that upon exit %r15==%r9, so +; they can be used interchangeably add r13,rax adc rdx,0 - add r13,r11 + add r13,r11 ; np[j]*m1+ap[j]*bp[0] adc rdx,0 - mov QWORD[((-16))+r9*8+rsp],r13 + mov QWORD[((-16))+r9*8+rsp],r13 ; tp[num-1] mov r13,rdx mov r11,r10 @@ -276,13 +276,13 @@ $L$1st_enter: add r13,r11 adc rdx,0 mov QWORD[((-8))+r9*8+rsp],r13 - mov QWORD[r9*8+rsp],rdx + mov QWORD[r9*8+rsp],rdx ; store upmost overflow bit - lea r14,[1+r14] + lea r14,[1+r14] ; i++ jmp NEAR $L$outer ALIGN 16 $L$outer: - lea rdx,[((24+128))+r9*8+rsp] + lea rdx,[((24+128))+r9*8+rsp] ; where 256-byte mask is (+size optimization) and rdx,-16 pxor xmm4,xmm4 pxor xmm5,xmm5 @@ -335,34 +335,34 @@ $L$outer: por xmm4,xmm2 por xmm5,xmm3 por xmm4,xmm5 - - pshufd xmm0,xmm4,0x4e +; Combine the upper and lower halves of %xmm4 as %xmm0. + pshufd xmm0,xmm4,0x4e ; Swap upper and lower halves. por xmm0,xmm4 lea r12,[256+r12] - mov rax,QWORD[rsi] - movq rbx,xmm0 + mov rax,QWORD[rsi] ; ap[0] + movq rbx,xmm0 ; m0=bp[i] - xor r15,r15 + xor r15,r15 ; j=0 mov rbp,r8 mov r10,QWORD[rsp] - mul rbx - add r10,rax + mul rbx ; ap[0]*bp[i] + add r10,rax ; ap[0]*bp[i]+tp[0] mov rax,QWORD[rcx] adc rdx,0 - imul rbp,r10 + imul rbp,r10 ; tp[0]*n0 mov r11,rdx - mul rbp - add r10,rax + mul rbp ; np[0]*m1 + add r10,rax ; discarded mov rax,QWORD[8+rsi] adc rdx,0 - mov r10,QWORD[8+rsp] + mov r10,QWORD[8+rsp] ; tp[1] mov r13,rdx - lea r15,[1+r15] + lea r15,[1+r15] ; j++ jmp NEAR $L$inner_enter ALIGN 16 @@ -370,78 +370,78 @@ $L$inner: add r13,rax mov rax,QWORD[r15*8+rsi] adc rdx,0 - add r13,r10 + add r13,r10 ; np[j]*m1+ap[j]*bp[i]+tp[j] mov r10,QWORD[r15*8+rsp] adc rdx,0 - mov QWORD[((-16))+r15*8+rsp],r13 + mov QWORD[((-16))+r15*8+rsp],r13 ; tp[j-1] mov r13,rdx $L$inner_enter: - mul rbx + mul rbx ; ap[j]*bp[i] add r11,rax mov rax,QWORD[r15*8+rcx] adc rdx,0 - add r10,r11 + add r10,r11 ; ap[j]*bp[i]+tp[j] mov r11,rdx adc r11,0 - lea r15,[1+r15] + lea r15,[1+r15] ; j++ - mul rbp + mul rbp ; np[j]*m1 cmp r15,r9 - jne NEAR $L$inner - + jne NEAR $L$inner ; note that upon exit %r15==%r9, so +; they can be used interchangeably add r13,rax adc rdx,0 - add r13,r10 + add r13,r10 ; np[j]*m1+ap[j]*bp[i]+tp[j] mov r10,QWORD[r9*8+rsp] adc rdx,0 - mov QWORD[((-16))+r9*8+rsp],r13 + mov QWORD[((-16))+r9*8+rsp],r13 ; tp[num-1] mov r13,rdx xor rdx,rdx add r13,r11 adc rdx,0 - add r13,r10 + add r13,r10 ; pull upmost overflow bit adc rdx,0 mov QWORD[((-8))+r9*8+rsp],r13 - mov QWORD[r9*8+rsp],rdx + mov QWORD[r9*8+rsp],rdx ; store upmost overflow bit - lea r14,[1+r14] + lea r14,[1+r14] ; i++ cmp r14,r9 jb NEAR $L$outer - xor r14,r14 - mov rax,QWORD[rsp] - lea rsi,[rsp] - mov r15,r9 + xor r14,r14 ; i=0 and clear CF! + mov rax,QWORD[rsp] ; tp[0] + lea rsi,[rsp] ; borrow ap for tp + mov r15,r9 ; j=num jmp NEAR $L$sub ALIGN 16 $L$sub: sbb rax,QWORD[r14*8+rcx] - mov QWORD[r14*8+rdi],rax - mov rax,QWORD[8+r14*8+rsi] - lea r14,[1+r14] - dec r15 + mov QWORD[r14*8+rdi],rax ; rp[i]=tp[i]-np[i] + mov rax,QWORD[8+r14*8+rsi] ; tp[i+1] + lea r14,[1+r14] ; i++ + dec r15 ; doesn't affect CF! jnz NEAR $L$sub - sbb rax,0 + sbb rax,0 ; handle upmost overflow bit mov rbx,-1 xor rbx,rax xor r14,r14 - mov r15,r9 + mov r15,r9 ; j=num -$L$copy: +$L$copy: ; conditional copy mov rcx,QWORD[r14*8+rdi] mov rdx,QWORD[r14*8+rsp] and rcx,rbx and rdx,rax - mov QWORD[r14*8+rsp],r14 + mov QWORD[r14*8+rsp],r14 ; zap temporary vector or rdx,rcx - mov QWORD[r14*8+rdi],rdx + mov QWORD[r14*8+rdi],rdx ; rp[i]=tp[i] lea r14,[1+r14] sub r15,1 jnz NEAR $L$copy - mov rsi,QWORD[8+r9*8+rsp] + mov rsi,QWORD[8+r9*8+rsp] ; restore %rsp mov rax,1 @@ -501,36 +501,36 @@ _CET_ENDBR $L$mul4x_prologue: DB 0x67 - - - - shl r9d,3 - lea r10,[r9*2+r9] - neg r9 - - - - - - - - - - +; num is declared as an int, a 32-bit parameter, so the upper half is +; undefined. It is important that this write to %r9, which zeros the +; upper half, predates the first access. + shl r9d,3 ; convert %r9 to bytes + lea r10,[r9*2+r9] ; 3*%r9 in bytes + neg r9 ; -%r9 + +; ############################################################# +; Ensure that stack frame doesn't alias with +3*%r9 +; modulo 4096, which covers ret[num], am[num] and n[num] +; (see bn_exp.c). This is done to allow memory disambiguation +; logic do its magic. [Extra [num] is allocated in order +; to align with bn_power5's frame, which is cleansed after +; completing exponentiation. Extra 256 bytes is for power mask +; calculated from 7th argument, the index.] +; lea r11,[((-320))+r9*2+rsp] mov rbp,rsp sub r11,rdi and r11,4095 cmp r10,r11 jb NEAR $L$mul4xsp_alt - sub rbp,r11 - lea rbp,[((-320))+r9*2+rbp] + sub rbp,r11 ; align with %rdi + lea rbp,[((-320))+r9*2+rbp] ; future alloca(frame+2*num*8+256) jmp NEAR $L$mul4xsp_done ALIGN 32 $L$mul4xsp_alt: lea r10,[((4096-320))+r9*2] - lea rbp,[((-320))+r9*2+rbp] + lea rbp,[((-320))+r9*2+rbp] ; future alloca(frame+2*num*8+256) sub r11,r10 mov r10,0 cmovc r11,r10 @@ -561,7 +561,7 @@ $L$mul4x_body: call mul4x_internal - mov rsi,QWORD[40+rsp] + mov rsi,QWORD[40+rsp] ; restore %rsp mov rax,1 @@ -590,36 +590,36 @@ $L$SEH_end_bn_mul4x_mont_gather5: ALIGN 32 mul4x_internal: - shl r9,5 - movd xmm5,DWORD[56+rax] + shl r9,5 ; %r9 was in bytes + movd xmm5,DWORD[56+rax] ; load 7th argument, index lea rax,[$L$inc] - lea r13,[128+r9*1+rdx] - shr r9,5 - movdqa xmm0,XMMWORD[rax] - movdqa xmm1,XMMWORD[16+rax] - lea r10,[((88-112))+r9*1+rsp] - lea r12,[128+rdx] - - pshufd xmm5,xmm5,0 + lea r13,[128+r9*1+rdx] ; end of powers table (+size optimization) + shr r9,5 ; restore %r9 + movdqa xmm0,XMMWORD[rax] ; 00000001000000010000000000000000 + movdqa xmm1,XMMWORD[16+rax] ; 00000002000000020000000200000002 + lea r10,[((88-112))+r9*1+rsp] ; place the mask after tp[num+1] (+ICache optimization) + lea r12,[128+rdx] ; size optimization + + pshufd xmm5,xmm5,0 ; broadcast index movdqa xmm4,xmm1 DB 0x67,0x67 movdqa xmm2,xmm1 paddd xmm1,xmm0 - pcmpeqd xmm0,xmm5 + pcmpeqd xmm0,xmm5 ; compare to 1,0 DB 0x67 movdqa xmm3,xmm4 paddd xmm2,xmm1 - pcmpeqd xmm1,xmm5 + pcmpeqd xmm1,xmm5 ; compare to 3,2 movdqa XMMWORD[112+r10],xmm0 movdqa xmm0,xmm4 paddd xmm3,xmm2 - pcmpeqd xmm2,xmm5 + pcmpeqd xmm2,xmm5 ; compare to 5,4 movdqa XMMWORD[128+r10],xmm1 movdqa xmm1,xmm4 paddd xmm0,xmm3 - pcmpeqd xmm3,xmm5 + pcmpeqd xmm3,xmm5 ; compare to 7,6 movdqa XMMWORD[144+r10],xmm2 movdqa xmm2,xmm4 @@ -628,17 +628,17 @@ mul4x_internal: movdqa XMMWORD[160+r10],xmm3 movdqa xmm3,xmm4 paddd xmm2,xmm1 - pcmpeqd xmm1,xmm5 + pcmpeqd xmm1,xmm5 ; compare to 3,2 movdqa XMMWORD[176+r10],xmm0 movdqa xmm0,xmm4 paddd xmm3,xmm2 - pcmpeqd xmm2,xmm5 + pcmpeqd xmm2,xmm5 ; compare to 5,4 movdqa XMMWORD[192+r10],xmm1 movdqa xmm1,xmm4 paddd xmm0,xmm3 - pcmpeqd xmm3,xmm5 + pcmpeqd xmm3,xmm5 ; compare to 7,6 movdqa XMMWORD[208+r10],xmm2 movdqa xmm2,xmm4 @@ -647,17 +647,17 @@ mul4x_internal: movdqa XMMWORD[224+r10],xmm3 movdqa xmm3,xmm4 paddd xmm2,xmm1 - pcmpeqd xmm1,xmm5 + pcmpeqd xmm1,xmm5 ; compare to 3,2 movdqa XMMWORD[240+r10],xmm0 movdqa xmm0,xmm4 paddd xmm3,xmm2 - pcmpeqd xmm2,xmm5 + pcmpeqd xmm2,xmm5 ; compare to 5,4 movdqa XMMWORD[256+r10],xmm1 movdqa xmm1,xmm4 paddd xmm0,xmm3 - pcmpeqd xmm3,xmm5 + pcmpeqd xmm3,xmm5 ; compare to 7,6 movdqa XMMWORD[272+r10],xmm2 movdqa xmm2,xmm4 @@ -676,7 +676,7 @@ mul4x_internal: pcmpeqd xmm3,xmm5 movdqa XMMWORD[336+r10],xmm2 - pand xmm0,XMMWORD[64+r12] + pand xmm0,XMMWORD[64+r12] ; while it's still in register pand xmm1,XMMWORD[80+r12] pand xmm2,XMMWORD[96+r12] @@ -721,31 +721,31 @@ mul4x_internal: por xmm0,xmm2 por xmm1,xmm3 por xmm0,xmm1 - - pshufd xmm1,xmm0,0x4e +; Combine the upper and lower halves of %xmm0. + pshufd xmm1,xmm0,0x4e ; Swap upper and lower halves. por xmm0,xmm1 lea r12,[256+r12] - movq rbx,xmm0 + movq rbx,xmm0 ; m0=bp[0] - mov QWORD[((16+8))+rsp],r13 - mov QWORD[((56+8))+rsp],rdi + mov QWORD[((16+8))+rsp],r13 ; save end of b[num] + mov QWORD[((56+8))+rsp],rdi ; save %rdi - mov r8,QWORD[r8] + mov r8,QWORD[r8] ; pull n0[0] value mov rax,QWORD[rsi] - lea rsi,[r9*1+rsi] + lea rsi,[r9*1+rsi] ; end of a[num] neg r9 mov rbp,r8 - mul rbx + mul rbx ; ap[0]*bp[0] mov r10,rax mov rax,QWORD[rcx] - imul rbp,r10 + imul rbp,r10 ; "tp[0]"*n0 lea r14,[((64+8))+rsp] mov r11,rdx - mul rbp - add r10,rax + mul rbp ; np[0]*m1 + add r10,rax ; discarded mov rax,QWORD[8+r9*1+rsi] adc rdx,0 mov rdi,rdx @@ -761,7 +761,7 @@ mul4x_internal: mov rax,QWORD[16+r9*1+rsi] adc rdx,0 add rdi,r11 - lea r15,[32+r9] + lea r15,[32+r9] ; j=4 lea rcx,[32+rcx] adc rdx,0 mov QWORD[r14],rdi @@ -770,103 +770,103 @@ mul4x_internal: ALIGN 32 $L$1st4x: - mul rbx + mul rbx ; ap[j]*bp[0] add r10,rax mov rax,QWORD[((-16))+rcx] lea r14,[32+r14] adc rdx,0 mov r11,rdx - mul rbp + mul rbp ; np[j]*m1 add r13,rax mov rax,QWORD[((-8))+r15*1+rsi] adc rdx,0 - add r13,r10 + add r13,r10 ; np[j]*m1+ap[j]*bp[0] adc rdx,0 - mov QWORD[((-24))+r14],r13 + mov QWORD[((-24))+r14],r13 ; tp[j-1] mov rdi,rdx - mul rbx + mul rbx ; ap[j]*bp[0] add r11,rax mov rax,QWORD[((-8))+rcx] adc rdx,0 mov r10,rdx - mul rbp + mul rbp ; np[j]*m1 add rdi,rax mov rax,QWORD[r15*1+rsi] adc rdx,0 - add rdi,r11 + add rdi,r11 ; np[j]*m1+ap[j]*bp[0] adc rdx,0 - mov QWORD[((-16))+r14],rdi + mov QWORD[((-16))+r14],rdi ; tp[j-1] mov r13,rdx - mul rbx + mul rbx ; ap[j]*bp[0] add r10,rax mov rax,QWORD[rcx] adc rdx,0 mov r11,rdx - mul rbp + mul rbp ; np[j]*m1 add r13,rax mov rax,QWORD[8+r15*1+rsi] adc rdx,0 - add r13,r10 + add r13,r10 ; np[j]*m1+ap[j]*bp[0] adc rdx,0 - mov QWORD[((-8))+r14],r13 + mov QWORD[((-8))+r14],r13 ; tp[j-1] mov rdi,rdx - mul rbx + mul rbx ; ap[j]*bp[0] add r11,rax mov rax,QWORD[8+rcx] adc rdx,0 mov r10,rdx - mul rbp + mul rbp ; np[j]*m1 add rdi,rax mov rax,QWORD[16+r15*1+rsi] adc rdx,0 - add rdi,r11 + add rdi,r11 ; np[j]*m1+ap[j]*bp[0] lea rcx,[32+rcx] adc rdx,0 - mov QWORD[r14],rdi + mov QWORD[r14],rdi ; tp[j-1] mov r13,rdx - add r15,32 + add r15,32 ; j+=4 jnz NEAR $L$1st4x - mul rbx + mul rbx ; ap[j]*bp[0] add r10,rax mov rax,QWORD[((-16))+rcx] lea r14,[32+r14] adc rdx,0 mov r11,rdx - mul rbp + mul rbp ; np[j]*m1 add r13,rax mov rax,QWORD[((-8))+rsi] adc rdx,0 - add r13,r10 + add r13,r10 ; np[j]*m1+ap[j]*bp[0] adc rdx,0 - mov QWORD[((-24))+r14],r13 + mov QWORD[((-24))+r14],r13 ; tp[j-1] mov rdi,rdx - mul rbx + mul rbx ; ap[j]*bp[0] add r11,rax mov rax,QWORD[((-8))+rcx] adc rdx,0 mov r10,rdx - mul rbp + mul rbp ; np[j]*m1 add rdi,rax - mov rax,QWORD[r9*1+rsi] + mov rax,QWORD[r9*1+rsi] ; ap[0] adc rdx,0 - add rdi,r11 + add rdi,r11 ; np[j]*m1+ap[j]*bp[0] adc rdx,0 - mov QWORD[((-16))+r14],rdi + mov QWORD[((-16))+r14],rdi ; tp[j-1] mov r13,rdx - lea rcx,[r9*1+rcx] + lea rcx,[r9*1+rcx] ; rewind %rcx xor rdi,rdi add r13,r10 @@ -877,7 +877,7 @@ $L$1st4x: ALIGN 32 $L$outer4x: - lea rdx,[((16+128))+r14] + lea rdx,[((16+128))+r14] ; where 256-byte mask is (+size optimization) pxor xmm4,xmm4 pxor xmm5,xmm5 movdqa xmm0,XMMWORD[((-128))+r12] @@ -929,45 +929,45 @@ $L$outer4x: por xmm4,xmm2 por xmm5,xmm3 por xmm4,xmm5 - - pshufd xmm0,xmm4,0x4e +; Combine the upper and lower halves of %xmm4 as %xmm0. + pshufd xmm0,xmm4,0x4e ; Swap upper and lower halves. por xmm0,xmm4 lea r12,[256+r12] - movq rbx,xmm0 + movq rbx,xmm0 ; m0=bp[i] mov r10,QWORD[r9*1+r14] mov rbp,r8 - mul rbx - add r10,rax + mul rbx ; ap[0]*bp[i] + add r10,rax ; ap[0]*bp[i]+tp[0] mov rax,QWORD[rcx] adc rdx,0 - imul rbp,r10 + imul rbp,r10 ; tp[0]*n0 mov r11,rdx - mov QWORD[r14],rdi + mov QWORD[r14],rdi ; store upmost overflow bit - lea r14,[r9*1+r14] + lea r14,[r9*1+r14] ; rewind %r14 - mul rbp - add r10,rax + mul rbp ; np[0]*m1 + add r10,rax ; "%r13", discarded mov rax,QWORD[8+r9*1+rsi] adc rdx,0 mov rdi,rdx - mul rbx + mul rbx ; ap[j]*bp[i] add r11,rax mov rax,QWORD[8+rcx] adc rdx,0 - add r11,QWORD[8+r14] + add r11,QWORD[8+r14] ; +tp[1] adc rdx,0 mov r10,rdx - mul rbp + mul rbp ; np[j]*m1 add rdi,rax mov rax,QWORD[16+r9*1+rsi] adc rdx,0 - add rdi,r11 - lea r15,[32+r9] + add rdi,r11 ; np[j]*m1+ap[j]*bp[i]+tp[j] + lea r15,[32+r9] ; j=4 lea rcx,[32+rcx] adc rdx,0 mov r13,rdx @@ -975,25 +975,25 @@ $L$outer4x: ALIGN 32 $L$inner4x: - mul rbx + mul rbx ; ap[j]*bp[i] add r10,rax mov rax,QWORD[((-16))+rcx] adc rdx,0 - add r10,QWORD[16+r14] + add r10,QWORD[16+r14] ; ap[j]*bp[i]+tp[j] lea r14,[32+r14] adc rdx,0 mov r11,rdx - mul rbp + mul rbp ; np[j]*m1 add r13,rax mov rax,QWORD[((-8))+r15*1+rsi] adc rdx,0 add r13,r10 adc rdx,0 - mov QWORD[((-32))+r14],rdi + mov QWORD[((-32))+r14],rdi ; tp[j-1] mov rdi,rdx - mul rbx + mul rbx ; ap[j]*bp[i] add r11,rax mov rax,QWORD[((-8))+rcx] adc rdx,0 @@ -1001,33 +1001,33 @@ $L$inner4x: adc rdx,0 mov r10,rdx - mul rbp + mul rbp ; np[j]*m1 add rdi,rax mov rax,QWORD[r15*1+rsi] adc rdx,0 add rdi,r11 adc rdx,0 - mov QWORD[((-24))+r14],r13 + mov QWORD[((-24))+r14],r13 ; tp[j-1] mov r13,rdx - mul rbx + mul rbx ; ap[j]*bp[i] add r10,rax mov rax,QWORD[rcx] adc rdx,0 - add r10,QWORD[r14] + add r10,QWORD[r14] ; ap[j]*bp[i]+tp[j] adc rdx,0 mov r11,rdx - mul rbp + mul rbp ; np[j]*m1 add r13,rax mov rax,QWORD[8+r15*1+rsi] adc rdx,0 add r13,r10 adc rdx,0 - mov QWORD[((-16))+r14],rdi + mov QWORD[((-16))+r14],rdi ; tp[j-1] mov rdi,rdx - mul rbx + mul rbx ; ap[j]*bp[i] add r11,rax mov rax,QWORD[8+rcx] adc rdx,0 @@ -1035,38 +1035,38 @@ $L$inner4x: adc rdx,0 mov r10,rdx - mul rbp + mul rbp ; np[j]*m1 add rdi,rax mov rax,QWORD[16+r15*1+rsi] adc rdx,0 add rdi,r11 lea rcx,[32+rcx] adc rdx,0 - mov QWORD[((-8))+r14],r13 + mov QWORD[((-8))+r14],r13 ; tp[j-1] mov r13,rdx - add r15,32 + add r15,32 ; j+=4 jnz NEAR $L$inner4x - mul rbx + mul rbx ; ap[j]*bp[i] add r10,rax mov rax,QWORD[((-16))+rcx] adc rdx,0 - add r10,QWORD[16+r14] + add r10,QWORD[16+r14] ; ap[j]*bp[i]+tp[j] lea r14,[32+r14] adc rdx,0 mov r11,rdx - mul rbp + mul rbp ; np[j]*m1 add r13,rax mov rax,QWORD[((-8))+rsi] adc rdx,0 add r13,r10 adc rdx,0 - mov QWORD[((-32))+r14],rdi + mov QWORD[((-32))+r14],rdi ; tp[j-1] mov rdi,rdx - mul rbx + mul rbx ; ap[j]*bp[i] add r11,rax mov rax,rbp mov rbp,QWORD[((-8))+rcx] @@ -1075,39 +1075,39 @@ $L$inner4x: adc rdx,0 mov r10,rdx - mul rbp + mul rbp ; np[j]*m1 add rdi,rax - mov rax,QWORD[r9*1+rsi] + mov rax,QWORD[r9*1+rsi] ; ap[0] adc rdx,0 add rdi,r11 adc rdx,0 - mov QWORD[((-24))+r14],r13 + mov QWORD[((-24))+r14],r13 ; tp[j-1] mov r13,rdx - mov QWORD[((-16))+r14],rdi - lea rcx,[r9*1+rcx] + mov QWORD[((-16))+r14],rdi ; tp[j-1] + lea rcx,[r9*1+rcx] ; rewind %rcx xor rdi,rdi add r13,r10 adc rdi,0 - add r13,QWORD[r14] - adc rdi,0 + add r13,QWORD[r14] ; pull upmost overflow bit + adc rdi,0 ; upmost overflow bit mov QWORD[((-8))+r14],r13 cmp r12,QWORD[((16+8))+rsp] jb NEAR $L$outer4x xor rax,rax - sub rbp,r13 - adc r15,r15 + sub rbp,r13 ; compare top-most words + adc r15,r15 ; %r15 is zero or rdi,r15 - sub rax,rdi - lea rbx,[r9*1+r14] + sub rax,rdi ; %rax=-%rdi + lea rbx,[r9*1+r14] ; tptr in .sqr4x_sub mov r12,QWORD[rcx] - lea rbp,[rcx] + lea rbp,[rcx] ; nptr in .sqr4x_sub mov rcx,r9 sar rcx,3+2 - mov rdi,QWORD[((56+8))+rsp] - dec r12 + mov rdi,QWORD[((56+8))+rsp] ; rptr in .sqr4x_sub + dec r12 ; so that after 'not' we get -n[0] xor r10,r10 mov r13,QWORD[8+rbp] mov r14,QWORD[16+rbp] @@ -1149,35 +1149,35 @@ _CET_ENDBR $L$power5_prologue: - - - - shl r9d,3 - lea r10d,[r9*2+r9] +; num is declared as an int, a 32-bit parameter, so the upper half is +; undefined. It is important that this write to %r9, which zeros the +; upper half, come before the first access. + shl r9d,3 ; convert %r9 to bytes + lea r10d,[r9*2+r9] ; 3*%r9 neg r9 - mov r8,QWORD[r8] - - - - - - - - + mov r8,QWORD[r8] ; *n0 + +; ############################################################# +; Ensure that stack frame doesn't alias with %rdi+3*%r9 +; modulo 4096, which covers ret[num], am[num] and n[num] +; (see bn_exp.c). This is done to allow memory disambiguation +; logic do its magic. [Extra 256 bytes is for power mask +; calculated from 7th argument, the index.] +; lea r11,[((-320))+r9*2+rsp] mov rbp,rsp sub r11,rdi and r11,4095 cmp r10,r11 jb NEAR $L$pwr_sp_alt - sub rbp,r11 - lea rbp,[((-320))+r9*2+rbp] + sub rbp,r11 ; align with %rsi + lea rbp,[((-320))+r9*2+rbp] ; future alloca(frame+2*num*8+256) jmp NEAR $L$pwr_sp_done ALIGN 32 $L$pwr_sp_alt: lea r10,[((4096-320))+r9*2] - lea rbp,[((-320))+r9*2+rbp] + lea rbp,[((-320))+r9*2+rbp] ; future alloca(frame+2*num*8+256) sub r11,r10 mov r10,0 cmovc r11,r10 @@ -1203,22 +1203,22 @@ $L$pwr_page_walk_done: mov r10,r9 neg r9 - - - - - - - - - +; ############################################################# +; Stack layout +; +; +0 saved %r9, used in reduction section +; +8 &t[2*%r9], used in reduction section +; +32 saved *n0 +; +40 saved %rsp +; +48 t[2*%r9] +; mov QWORD[32+rsp],r8 - mov QWORD[40+rsp],rax + mov QWORD[40+rsp],rax ; save original %rsp $L$power5_body: - movq xmm1,rdi - movq xmm2,rcx - movq xmm3,r10 + movq xmm1,rdi ; save %rdi, used in sqr8x + movq xmm2,rcx ; save %rcx + movq xmm3,r10 ; -%r9, used in sqr8x movq xmm4,rdx call __bn_sqr8x_internal @@ -1240,7 +1240,7 @@ $L$power5_body: call mul4x_internal - mov rsi,QWORD[40+rsp] + mov rsi,QWORD[40+rsp] ; restore %rsp mov rax,1 mov r15,QWORD[((-48))+rsi] @@ -1272,261 +1272,261 @@ bn_sqr8x_internal: __bn_sqr8x_internal: _CET_ENDBR - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - lea rbp,[32+r10] - lea rsi,[r9*1+rsi] - - mov rcx,r9 - - - mov r14,QWORD[((-32))+rbp*1+rsi] - lea rdi,[((48+8))+r9*2+rsp] - mov rax,QWORD[((-24))+rbp*1+rsi] - lea rdi,[((-32))+rbp*1+rdi] - mov rbx,QWORD[((-16))+rbp*1+rsi] +; ############################################################# +; Squaring part: +; +; a) multiply-n-add everything but a[i]*a[i]; +; b) shift result of a) by 1 to the left and accumulate +; a[i]*a[i] products; +; +; ############################################################# +; a[1]a[0] +; a[2]a[0] +; a[3]a[0] +; a[2]a[1] +; a[4]a[0] +; a[3]a[1] +; a[5]a[0] +; a[4]a[1] +; a[3]a[2] +; a[6]a[0] +; a[5]a[1] +; a[4]a[2] +; a[7]a[0] +; a[6]a[1] +; a[5]a[2] +; a[4]a[3] +; a[7]a[1] +; a[6]a[2] +; a[5]a[3] +; a[7]a[2] +; a[6]a[3] +; a[5]a[4] +; a[7]a[3] +; a[6]a[4] +; a[7]a[4] +; a[6]a[5] +; a[7]a[5] +; a[7]a[6] +; a[1]a[0] +; a[2]a[0] +; a[3]a[0] +; a[4]a[0] +; a[5]a[0] +; a[6]a[0] +; a[7]a[0] +; a[2]a[1] +; a[3]a[1] +; a[4]a[1] +; a[5]a[1] +; a[6]a[1] +; a[7]a[1] +; a[3]a[2] +; a[4]a[2] +; a[5]a[2] +; a[6]a[2] +; a[7]a[2] +; a[4]a[3] +; a[5]a[3] +; a[6]a[3] +; a[7]a[3] +; a[5]a[4] +; a[6]a[4] +; a[7]a[4] +; a[6]a[5] +; a[7]a[5] +; a[7]a[6] +; a[0]a[0] +; a[1]a[1] +; a[2]a[2] +; a[3]a[3] +; a[4]a[4] +; a[5]a[5] +; a[6]a[6] +; a[7]a[7] + + lea rbp,[32+r10] ; %rbp=-(%r9-32) + lea rsi,[r9*1+rsi] ; end of a[] buffer, (%rsi,%rbp)=&ap[2] + + mov rcx,r9 ; %rcx=%r9 + +; comments apply to %r9==8 case + mov r14,QWORD[((-32))+rbp*1+rsi] ; a[0] + lea rdi,[((48+8))+r9*2+rsp] ; end of tp[] buffer, &tp[2*%r9] + mov rax,QWORD[((-24))+rbp*1+rsi] ; a[1] + lea rdi,[((-32))+rbp*1+rdi] ; end of tp[] window, &tp[2*%r9-"%rbp"] + mov rbx,QWORD[((-16))+rbp*1+rsi] ; a[2] mov r15,rax - mul r14 - mov r10,rax - mov rax,rbx + mul r14 ; a[1]*a[0] + mov r10,rax ; a[1]*a[0] + mov rax,rbx ; a[2] mov r11,rdx - mov QWORD[((-24))+rbp*1+rdi],r10 + mov QWORD[((-24))+rbp*1+rdi],r10 ; t[1] - mul r14 + mul r14 ; a[2]*a[0] add r11,rax mov rax,rbx adc rdx,0 - mov QWORD[((-16))+rbp*1+rdi],r11 + mov QWORD[((-16))+rbp*1+rdi],r11 ; t[2] mov r10,rdx - mov rbx,QWORD[((-8))+rbp*1+rsi] - mul r15 - mov r12,rax + mov rbx,QWORD[((-8))+rbp*1+rsi] ; a[3] + mul r15 ; a[2]*a[1] + mov r12,rax ; a[2]*a[1]+t[3] mov rax,rbx mov r13,rdx lea rcx,[rbp] - mul r14 - add r10,rax + mul r14 ; a[3]*a[0] + add r10,rax ; a[3]*a[0]+a[2]*a[1]+t[3] mov rax,rbx mov r11,rdx adc r11,0 add r10,r12 adc r11,0 - mov QWORD[((-8))+rcx*1+rdi],r10 + mov QWORD[((-8))+rcx*1+rdi],r10 ; t[3] jmp NEAR $L$sqr4x_1st ALIGN 32 $L$sqr4x_1st: - mov rbx,QWORD[rcx*1+rsi] - mul r15 - add r13,rax + mov rbx,QWORD[rcx*1+rsi] ; a[4] + mul r15 ; a[3]*a[1] + add r13,rax ; a[3]*a[1]+t[4] mov rax,rbx mov r12,rdx adc r12,0 - mul r14 - add r11,rax - mov rax,rbx - mov rbx,QWORD[8+rcx*1+rsi] + mul r14 ; a[4]*a[0] + add r11,rax ; a[4]*a[0]+a[3]*a[1]+t[4] + mov rax,rbx ; a[3] + mov rbx,QWORD[8+rcx*1+rsi] ; a[5] mov r10,rdx adc r10,0 add r11,r13 adc r10,0 - mul r15 - add r12,rax + mul r15 ; a[4]*a[3] + add r12,rax ; a[4]*a[3]+t[5] mov rax,rbx - mov QWORD[rcx*1+rdi],r11 + mov QWORD[rcx*1+rdi],r11 ; t[4] mov r13,rdx adc r13,0 - mul r14 - add r10,rax + mul r14 ; a[5]*a[2] + add r10,rax ; a[5]*a[2]+a[4]*a[3]+t[5] mov rax,rbx - mov rbx,QWORD[16+rcx*1+rsi] + mov rbx,QWORD[16+rcx*1+rsi] ; a[6] mov r11,rdx adc r11,0 add r10,r12 adc r11,0 - mul r15 - add r13,rax + mul r15 ; a[5]*a[3] + add r13,rax ; a[5]*a[3]+t[6] mov rax,rbx - mov QWORD[8+rcx*1+rdi],r10 + mov QWORD[8+rcx*1+rdi],r10 ; t[5] mov r12,rdx adc r12,0 - mul r14 - add r11,rax - mov rax,rbx - mov rbx,QWORD[24+rcx*1+rsi] + mul r14 ; a[6]*a[2] + add r11,rax ; a[6]*a[2]+a[5]*a[3]+t[6] + mov rax,rbx ; a[3] + mov rbx,QWORD[24+rcx*1+rsi] ; a[7] mov r10,rdx adc r10,0 add r11,r13 adc r10,0 - mul r15 - add r12,rax + mul r15 ; a[6]*a[5] + add r12,rax ; a[6]*a[5]+t[7] mov rax,rbx - mov QWORD[16+rcx*1+rdi],r11 + mov QWORD[16+rcx*1+rdi],r11 ; t[6] mov r13,rdx adc r13,0 lea rcx,[32+rcx] - mul r14 - add r10,rax + mul r14 ; a[7]*a[4] + add r10,rax ; a[7]*a[4]+a[6]*a[5]+t[6] mov rax,rbx mov r11,rdx adc r11,0 add r10,r12 adc r11,0 - mov QWORD[((-8))+rcx*1+rdi],r10 + mov QWORD[((-8))+rcx*1+rdi],r10 ; t[7] cmp rcx,0 jne NEAR $L$sqr4x_1st - mul r15 + mul r15 ; a[7]*a[5] add r13,rax lea rbp,[16+rbp] adc rdx,0 add r13,r11 adc rdx,0 - mov QWORD[rdi],r13 + mov QWORD[rdi],r13 ; t[8] mov r12,rdx - mov QWORD[8+rdi],rdx + mov QWORD[8+rdi],rdx ; t[9] jmp NEAR $L$sqr4x_outer ALIGN 32 -$L$sqr4x_outer: - mov r14,QWORD[((-32))+rbp*1+rsi] - lea rdi,[((48+8))+r9*2+rsp] - mov rax,QWORD[((-24))+rbp*1+rsi] - lea rdi,[((-32))+rbp*1+rdi] - mov rbx,QWORD[((-16))+rbp*1+rsi] +$L$sqr4x_outer: ; comments apply to %r9==6 case + mov r14,QWORD[((-32))+rbp*1+rsi] ; a[0] + lea rdi,[((48+8))+r9*2+rsp] ; end of tp[] buffer, &tp[2*%r9] + mov rax,QWORD[((-24))+rbp*1+rsi] ; a[1] + lea rdi,[((-32))+rbp*1+rdi] ; end of tp[] window, &tp[2*%r9-"%rbp"] + mov rbx,QWORD[((-16))+rbp*1+rsi] ; a[2] mov r15,rax - mul r14 - mov r10,QWORD[((-24))+rbp*1+rdi] - add r10,rax - mov rax,rbx + mul r14 ; a[1]*a[0] + mov r10,QWORD[((-24))+rbp*1+rdi] ; t[1] + add r10,rax ; a[1]*a[0]+t[1] + mov rax,rbx ; a[2] adc rdx,0 - mov QWORD[((-24))+rbp*1+rdi],r10 + mov QWORD[((-24))+rbp*1+rdi],r10 ; t[1] mov r11,rdx - mul r14 + mul r14 ; a[2]*a[0] add r11,rax mov rax,rbx adc rdx,0 - add r11,QWORD[((-16))+rbp*1+rdi] + add r11,QWORD[((-16))+rbp*1+rdi] ; a[2]*a[0]+t[2] mov r10,rdx adc r10,0 - mov QWORD[((-16))+rbp*1+rdi],r11 + mov QWORD[((-16))+rbp*1+rdi],r11 ; t[2] xor r12,r12 - mov rbx,QWORD[((-8))+rbp*1+rsi] - mul r15 - add r12,rax + mov rbx,QWORD[((-8))+rbp*1+rsi] ; a[3] + mul r15 ; a[2]*a[1] + add r12,rax ; a[2]*a[1]+t[3] mov rax,rbx adc rdx,0 add r12,QWORD[((-8))+rbp*1+rdi] mov r13,rdx adc r13,0 - mul r14 - add r10,rax + mul r14 ; a[3]*a[0] + add r10,rax ; a[3]*a[0]+a[2]*a[1]+t[3] mov rax,rbx adc rdx,0 add r10,r12 mov r11,rdx adc r11,0 - mov QWORD[((-8))+rbp*1+rdi],r10 + mov QWORD[((-8))+rbp*1+rdi],r10 ; t[3] lea rcx,[rbp] jmp NEAR $L$sqr4x_inner ALIGN 32 $L$sqr4x_inner: - mov rbx,QWORD[rcx*1+rsi] - mul r15 - add r13,rax + mov rbx,QWORD[rcx*1+rsi] ; a[4] + mul r15 ; a[3]*a[1] + add r13,rax ; a[3]*a[1]+t[4] mov rax,rbx mov r12,rdx adc r12,0 @@ -1534,254 +1534,254 @@ $L$sqr4x_inner: adc r12,0 DB 0x67 - mul r14 - add r11,rax - mov rax,rbx - mov rbx,QWORD[8+rcx*1+rsi] + mul r14 ; a[4]*a[0] + add r11,rax ; a[4]*a[0]+a[3]*a[1]+t[4] + mov rax,rbx ; a[3] + mov rbx,QWORD[8+rcx*1+rsi] ; a[5] mov r10,rdx adc r10,0 add r11,r13 adc r10,0 - mul r15 - add r12,rax - mov QWORD[rcx*1+rdi],r11 + mul r15 ; a[4]*a[3] + add r12,rax ; a[4]*a[3]+t[5] + mov QWORD[rcx*1+rdi],r11 ; t[4] mov rax,rbx mov r13,rdx adc r13,0 add r12,QWORD[8+rcx*1+rdi] - lea rcx,[16+rcx] + lea rcx,[16+rcx] ; j++ adc r13,0 - mul r14 - add r10,rax + mul r14 ; a[5]*a[2] + add r10,rax ; a[5]*a[2]+a[4]*a[3]+t[5] mov rax,rbx adc rdx,0 add r10,r12 mov r11,rdx adc r11,0 - mov QWORD[((-8))+rcx*1+rdi],r10 + mov QWORD[((-8))+rcx*1+rdi],r10 ; t[5], "preloaded t[1]" below cmp rcx,0 jne NEAR $L$sqr4x_inner DB 0x67 - mul r15 + mul r15 ; a[5]*a[3] add r13,rax adc rdx,0 add r13,r11 adc rdx,0 - mov QWORD[rdi],r13 + mov QWORD[rdi],r13 ; t[6], "preloaded t[2]" below mov r12,rdx - mov QWORD[8+rdi],rdx + mov QWORD[8+rdi],rdx ; t[7], "preloaded t[3]" below add rbp,16 jnz NEAR $L$sqr4x_outer - - mov r14,QWORD[((-32))+rsi] - lea rdi,[((48+8))+r9*2+rsp] - mov rax,QWORD[((-24))+rsi] - lea rdi,[((-32))+rbp*1+rdi] - mov rbx,QWORD[((-16))+rsi] +; comments apply to %r9==4 case + mov r14,QWORD[((-32))+rsi] ; a[0] + lea rdi,[((48+8))+r9*2+rsp] ; end of tp[] buffer, &tp[2*%r9] + mov rax,QWORD[((-24))+rsi] ; a[1] + lea rdi,[((-32))+rbp*1+rdi] ; end of tp[] window, &tp[2*%r9-"%rbp"] + mov rbx,QWORD[((-16))+rsi] ; a[2] mov r15,rax - mul r14 - add r10,rax - mov rax,rbx + mul r14 ; a[1]*a[0] + add r10,rax ; a[1]*a[0]+t[1], preloaded t[1] + mov rax,rbx ; a[2] mov r11,rdx adc r11,0 - mul r14 + mul r14 ; a[2]*a[0] add r11,rax mov rax,rbx - mov QWORD[((-24))+rdi],r10 + mov QWORD[((-24))+rdi],r10 ; t[1] mov r10,rdx adc r10,0 - add r11,r13 - mov rbx,QWORD[((-8))+rsi] + add r11,r13 ; a[2]*a[0]+t[2], preloaded t[2] + mov rbx,QWORD[((-8))+rsi] ; a[3] adc r10,0 - mul r15 - add r12,rax + mul r15 ; a[2]*a[1] + add r12,rax ; a[2]*a[1]+t[3], preloaded t[3] mov rax,rbx - mov QWORD[((-16))+rdi],r11 + mov QWORD[((-16))+rdi],r11 ; t[2] mov r13,rdx adc r13,0 - mul r14 - add r10,rax + mul r14 ; a[3]*a[0] + add r10,rax ; a[3]*a[0]+a[2]*a[1]+t[3] mov rax,rbx mov r11,rdx adc r11,0 add r10,r12 adc r11,0 - mov QWORD[((-8))+rdi],r10 + mov QWORD[((-8))+rdi],r10 ; t[3] - mul r15 + mul r15 ; a[3]*a[1] add r13,rax - mov rax,QWORD[((-16))+rsi] + mov rax,QWORD[((-16))+rsi] ; a[2] adc rdx,0 add r13,r11 adc rdx,0 - mov QWORD[rdi],r13 + mov QWORD[rdi],r13 ; t[4] mov r12,rdx - mov QWORD[8+rdi],rdx + mov QWORD[8+rdi],rdx ; t[5] - mul rbx + mul rbx ; a[2]*a[3] add rbp,16 xor r14,r14 - sub rbp,r9 + sub rbp,r9 ; %rbp=16-%r9 xor r15,r15 - add rax,r12 + add rax,r12 ; t[5] adc rdx,0 - mov QWORD[8+rdi],rax - mov QWORD[16+rdi],rdx - mov QWORD[24+rdi],r15 + mov QWORD[8+rdi],rax ; t[5] + mov QWORD[16+rdi],rdx ; t[6] + mov QWORD[24+rdi],r15 ; t[7] - mov rax,QWORD[((-16))+rbp*1+rsi] + mov rax,QWORD[((-16))+rbp*1+rsi] ; a[0] lea rdi,[((48+8))+rsp] - xor r10,r10 - mov r11,QWORD[8+rdi] + xor r10,r10 ; t[0] + mov r11,QWORD[8+rdi] ; t[1] - lea r12,[r10*2+r14] + lea r12,[r10*2+r14] ; t[2*i]<<1 | shift shr r10,63 - lea r13,[r11*2+rcx] + lea r13,[r11*2+rcx] ; t[2*i+1]<<1 | shr r11,63 - or r13,r10 - mov r10,QWORD[16+rdi] - mov r14,r11 - mul rax - neg r15 - mov r11,QWORD[24+rdi] + or r13,r10 ; | t[2*i]>>63 + mov r10,QWORD[16+rdi] ; t[2*i+2] # prefetch + mov r14,r11 ; shift=t[2*i+1]>>63 + mul rax ; a[i]*a[i] + neg r15 ; mov %r15,cf + mov r11,QWORD[24+rdi] ; t[2*i+2+1] # prefetch adc r12,rax - mov rax,QWORD[((-8))+rbp*1+rsi] + mov rax,QWORD[((-8))+rbp*1+rsi] ; a[i+1] # prefetch mov QWORD[rdi],r12 adc r13,rdx - lea rbx,[r10*2+r14] + lea rbx,[r10*2+r14] ; t[2*i]<<1 | shift mov QWORD[8+rdi],r13 - sbb r15,r15 + sbb r15,r15 ; mov cf,%r15 shr r10,63 - lea r8,[r11*2+rcx] + lea r8,[r11*2+rcx] ; t[2*i+1]<<1 | shr r11,63 - or r8,r10 - mov r10,QWORD[32+rdi] - mov r14,r11 - mul rax - neg r15 - mov r11,QWORD[40+rdi] + or r8,r10 ; | t[2*i]>>63 + mov r10,QWORD[32+rdi] ; t[2*i+2] # prefetch + mov r14,r11 ; shift=t[2*i+1]>>63 + mul rax ; a[i]*a[i] + neg r15 ; mov %r15,cf + mov r11,QWORD[40+rdi] ; t[2*i+2+1] # prefetch adc rbx,rax - mov rax,QWORD[rbp*1+rsi] + mov rax,QWORD[rbp*1+rsi] ; a[i+1] # prefetch mov QWORD[16+rdi],rbx adc r8,rdx lea rbp,[16+rbp] mov QWORD[24+rdi],r8 - sbb r15,r15 + sbb r15,r15 ; mov cf,%r15 lea rdi,[64+rdi] jmp NEAR $L$sqr4x_shift_n_add ALIGN 32 $L$sqr4x_shift_n_add: - lea r12,[r10*2+r14] + lea r12,[r10*2+r14] ; t[2*i]<<1 | shift shr r10,63 - lea r13,[r11*2+rcx] + lea r13,[r11*2+rcx] ; t[2*i+1]<<1 | shr r11,63 - or r13,r10 - mov r10,QWORD[((-16))+rdi] - mov r14,r11 - mul rax - neg r15 - mov r11,QWORD[((-8))+rdi] + or r13,r10 ; | t[2*i]>>63 + mov r10,QWORD[((-16))+rdi] ; t[2*i+2] # prefetch + mov r14,r11 ; shift=t[2*i+1]>>63 + mul rax ; a[i]*a[i] + neg r15 ; mov %r15,cf + mov r11,QWORD[((-8))+rdi] ; t[2*i+2+1] # prefetch adc r12,rax - mov rax,QWORD[((-8))+rbp*1+rsi] + mov rax,QWORD[((-8))+rbp*1+rsi] ; a[i+1] # prefetch mov QWORD[((-32))+rdi],r12 adc r13,rdx - lea rbx,[r10*2+r14] + lea rbx,[r10*2+r14] ; t[2*i]<<1 | shift mov QWORD[((-24))+rdi],r13 - sbb r15,r15 + sbb r15,r15 ; mov cf,%r15 shr r10,63 - lea r8,[r11*2+rcx] + lea r8,[r11*2+rcx] ; t[2*i+1]<<1 | shr r11,63 - or r8,r10 - mov r10,QWORD[rdi] - mov r14,r11 - mul rax - neg r15 - mov r11,QWORD[8+rdi] + or r8,r10 ; | t[2*i]>>63 + mov r10,QWORD[rdi] ; t[2*i+2] # prefetch + mov r14,r11 ; shift=t[2*i+1]>>63 + mul rax ; a[i]*a[i] + neg r15 ; mov %r15,cf + mov r11,QWORD[8+rdi] ; t[2*i+2+1] # prefetch adc rbx,rax - mov rax,QWORD[rbp*1+rsi] + mov rax,QWORD[rbp*1+rsi] ; a[i+1] # prefetch mov QWORD[((-16))+rdi],rbx adc r8,rdx - lea r12,[r10*2+r14] + lea r12,[r10*2+r14] ; t[2*i]<<1 | shift mov QWORD[((-8))+rdi],r8 - sbb r15,r15 + sbb r15,r15 ; mov cf,%r15 shr r10,63 - lea r13,[r11*2+rcx] + lea r13,[r11*2+rcx] ; t[2*i+1]<<1 | shr r11,63 - or r13,r10 - mov r10,QWORD[16+rdi] - mov r14,r11 - mul rax - neg r15 - mov r11,QWORD[24+rdi] + or r13,r10 ; | t[2*i]>>63 + mov r10,QWORD[16+rdi] ; t[2*i+2] # prefetch + mov r14,r11 ; shift=t[2*i+1]>>63 + mul rax ; a[i]*a[i] + neg r15 ; mov %r15,cf + mov r11,QWORD[24+rdi] ; t[2*i+2+1] # prefetch adc r12,rax - mov rax,QWORD[8+rbp*1+rsi] + mov rax,QWORD[8+rbp*1+rsi] ; a[i+1] # prefetch mov QWORD[rdi],r12 adc r13,rdx - lea rbx,[r10*2+r14] + lea rbx,[r10*2+r14] ; t[2*i]<<1 | shift mov QWORD[8+rdi],r13 - sbb r15,r15 + sbb r15,r15 ; mov cf,%r15 shr r10,63 - lea r8,[r11*2+rcx] + lea r8,[r11*2+rcx] ; t[2*i+1]<<1 | shr r11,63 - or r8,r10 - mov r10,QWORD[32+rdi] - mov r14,r11 - mul rax - neg r15 - mov r11,QWORD[40+rdi] + or r8,r10 ; | t[2*i]>>63 + mov r10,QWORD[32+rdi] ; t[2*i+2] # prefetch + mov r14,r11 ; shift=t[2*i+1]>>63 + mul rax ; a[i]*a[i] + neg r15 ; mov %r15,cf + mov r11,QWORD[40+rdi] ; t[2*i+2+1] # prefetch adc rbx,rax - mov rax,QWORD[16+rbp*1+rsi] + mov rax,QWORD[16+rbp*1+rsi] ; a[i+1] # prefetch mov QWORD[16+rdi],rbx adc r8,rdx mov QWORD[24+rdi],r8 - sbb r15,r15 + sbb r15,r15 ; mov cf,%r15 lea rdi,[64+rdi] add rbp,32 jnz NEAR $L$sqr4x_shift_n_add - lea r12,[r10*2+r14] + lea r12,[r10*2+r14] ; t[2*i]<<1 | shift DB 0x67 shr r10,63 - lea r13,[r11*2+rcx] + lea r13,[r11*2+rcx] ; t[2*i+1]<<1 | shr r11,63 - or r13,r10 - mov r10,QWORD[((-16))+rdi] - mov r14,r11 - mul rax - neg r15 - mov r11,QWORD[((-8))+rdi] + or r13,r10 ; | t[2*i]>>63 + mov r10,QWORD[((-16))+rdi] ; t[2*i+2] # prefetch + mov r14,r11 ; shift=t[2*i+1]>>63 + mul rax ; a[i]*a[i] + neg r15 ; mov %r15,cf + mov r11,QWORD[((-8))+rdi] ; t[2*i+2+1] # prefetch adc r12,rax - mov rax,QWORD[((-8))+rsi] + mov rax,QWORD[((-8))+rsi] ; a[i+1] # prefetch mov QWORD[((-32))+rdi],r12 adc r13,rdx - lea rbx,[r10*2+r14] + lea rbx,[r10*2+r14] ; t[2*i]<<1|shift mov QWORD[((-24))+rdi],r13 - sbb r15,r15 + sbb r15,r15 ; mov cf,%r15 shr r10,63 - lea r8,[r11*2+rcx] + lea r8,[r11*2+rcx] ; t[2*i+1]<<1 | shr r11,63 - or r8,r10 - mul rax - neg r15 + or r8,r10 ; | t[2*i]>>63 + mul rax ; a[i]*a[i] + neg r15 ; mov %r15,cf adc rbx,rax adc r8,rdx mov QWORD[((-16))+rdi],rbx @@ -1789,17 +1789,17 @@ $L$sqr4x_shift_n_add: movq rbp,xmm2 __bn_sqr8x_reduction: xor rax,rax - lea rcx,[rbp*1+r9] - lea rdx,[((48+8))+r9*2+rsp] + lea rcx,[rbp*1+r9] ; end of n[] + lea rdx,[((48+8))+r9*2+rsp] ; end of t[] buffer mov QWORD[((0+8))+rsp],rcx - lea rdi,[((48+8))+r9*1+rsp] + lea rdi,[((48+8))+r9*1+rsp] ; end of initial t[] window mov QWORD[((8+8))+rsp],rdx neg r9 jmp NEAR $L$8x_reduction_loop ALIGN 32 $L$8x_reduction_loop: - lea rdi,[r9*1+rdi] + lea rdi,[r9*1+rdi] ; start of current t[] window DB 0x66 mov rbx,QWORD[rdi] mov r9,QWORD[8+rdi] @@ -1809,20 +1809,20 @@ $L$8x_reduction_loop: mov r13,QWORD[40+rdi] mov r14,QWORD[48+rdi] mov r15,QWORD[56+rdi] - mov QWORD[rdx],rax + mov QWORD[rdx],rax ; store top-most carry bit lea rdi,[64+rdi] DB 0x67 mov r8,rbx - imul rbx,QWORD[((32+8))+rsp] - mov rax,QWORD[rbp] + imul rbx,QWORD[((32+8))+rsp] ; n0*a[0] + mov rax,QWORD[rbp] ; n[0] mov ecx,8 jmp NEAR $L$8x_reduce ALIGN 32 $L$8x_reduce: mul rbx - mov rax,QWORD[8+rbp] + mov rax,QWORD[8+rbp] ; n[1] neg r8 mov r8,rdx adc r8,0 @@ -1832,7 +1832,7 @@ $L$8x_reduce: mov rax,QWORD[16+rbp] adc rdx,0 add r8,r9 - mov QWORD[((48-8+8))+rcx*8+rsp],rbx + mov QWORD[((48-8+8))+rcx*8+rsp],rbx ; put aside n0*a[i] mov r9,rdx adc r9,0 @@ -1841,7 +1841,7 @@ $L$8x_reduce: mov rax,QWORD[24+rbp] adc rdx,0 add r9,r10 - mov rsi,QWORD[((32+8))+rsp] + mov rsi,QWORD[((32+8))+rsp] ; pull n0, borrow %rsi mov r10,rdx adc r10,0 @@ -1849,7 +1849,7 @@ $L$8x_reduce: add r11,rax mov rax,QWORD[32+rbp] adc rdx,0 - imul rsi,r8 + imul rsi,r8 ; modulo-scheduled add r10,r11 mov r11,rdx adc r11,0 @@ -1879,9 +1879,9 @@ $L$8x_reduce: adc r14,0 mul rbx - mov rbx,rsi + mov rbx,rsi ; n0*a[i] add r15,rax - mov rax,QWORD[rbp] + mov rax,QWORD[rbp] ; n[0] adc rdx,0 add r14,r15 mov r15,rdx @@ -1892,8 +1892,8 @@ $L$8x_reduce: lea rbp,[64+rbp] xor rax,rax - mov rdx,QWORD[((8+8))+rsp] - cmp rbp,QWORD[((0+8))+rsp] + mov rdx,QWORD[((8+8))+rsp] ; pull end of t[] + cmp rbp,QWORD[((0+8))+rsp] ; end of n[]? jae NEAR $L$8x_no_tail DB 0x66 @@ -1905,9 +1905,9 @@ $L$8x_reduce: adc r13,QWORD[40+rdi] adc r14,QWORD[48+rdi] adc r15,QWORD[56+rdi] - sbb rsi,rsi + sbb rsi,rsi ; top carry - mov rbx,QWORD[((48+56+8))+rsp] + mov rbx,QWORD[((48+56+8))+rsp] ; pull n0*a[0] mov ecx,8 mov rax,QWORD[rbp] jmp NEAR $L$8x_tail @@ -1917,7 +1917,7 @@ $L$8x_tail: mul rbx add r8,rax mov rax,QWORD[8+rbp] - mov QWORD[rdi],r8 + mov QWORD[rdi],r8 ; save result mov r8,rdx adc r8,0 @@ -1926,7 +1926,7 @@ $L$8x_tail: mov rax,QWORD[16+rbp] adc rdx,0 add r8,r9 - lea rdi,[8+rdi] + lea rdi,[8+rdi] ; %rdi++ mov r9,rdx adc r9,0 @@ -1971,11 +1971,11 @@ $L$8x_tail: adc r14,0 mul rbx - mov rbx,QWORD[((48-16+8))+rcx*8+rsp] + mov rbx,QWORD[((48-16+8))+rcx*8+rsp] ; pull n0*a[i] add r15,rax adc rdx,0 add r14,r15 - mov rax,QWORD[rbp] + mov rax,QWORD[rbp] ; pull n[0] mov r15,rdx adc r15,0 @@ -1983,13 +1983,13 @@ $L$8x_tail: jnz NEAR $L$8x_tail lea rbp,[64+rbp] - mov rdx,QWORD[((8+8))+rsp] - cmp rbp,QWORD[((0+8))+rsp] - jae NEAR $L$8x_tail_done + mov rdx,QWORD[((8+8))+rsp] ; pull end of t[] + cmp rbp,QWORD[((0+8))+rsp] ; end of n[]? + jae NEAR $L$8x_tail_done ; break out of loop - mov rbx,QWORD[((48+56+8))+rsp] + mov rbx,QWORD[((48+56+8))+rsp] ; pull n0*a[0] neg rsi - mov rax,QWORD[rbp] + mov rax,QWORD[rbp] ; pull n[0] adc r8,QWORD[rdi] adc r9,QWORD[8+rdi] adc r10,QWORD[16+rdi] @@ -1998,7 +1998,7 @@ $L$8x_tail: adc r13,QWORD[40+rdi] adc r14,QWORD[48+rdi] adc r15,QWORD[56+rdi] - sbb rsi,rsi + sbb rsi,rsi ; top carry mov ecx,8 jmp NEAR $L$8x_tail @@ -2006,7 +2006,7 @@ $L$8x_tail: ALIGN 32 $L$8x_tail_done: xor rax,rax - add r8,QWORD[rdx] + add r8,QWORD[rdx] ; can this overflow? adc r9,0 adc r10,0 adc r11,0 @@ -2026,15 +2026,15 @@ $L$8x_no_tail: adc r13,QWORD[40+rdi] adc r14,QWORD[48+rdi] adc r15,QWORD[56+rdi] - adc rax,0 - mov rcx,QWORD[((-8))+rbp] + adc rax,0 ; top-most carry + mov rcx,QWORD[((-8))+rbp] ; np[num-1] xor rsi,rsi - movq rbp,xmm2 + movq rbp,xmm2 ; restore %rbp - mov QWORD[rdi],r8 + mov QWORD[rdi],r8 ; store top 512 bits mov QWORD[8+rdi],r9 - movq r9,xmm3 + movq r9,xmm3 ; %r9 is %r9, can't be moved upwards mov QWORD[16+rdi],r10 mov QWORD[24+rdi],r11 mov QWORD[32+rdi],r12 @@ -2043,7 +2043,7 @@ $L$8x_no_tail: mov QWORD[56+rdi],r15 lea rdi,[64+rdi] - cmp rdi,rdx + cmp rdi,rdx ; end of t[]? jb NEAR $L$8x_reduction_loop ret @@ -2053,13 +2053,13 @@ ALIGN 32 __bn_post4x_internal: mov r12,QWORD[rbp] - lea rbx,[r9*1+rdi] + lea rbx,[r9*1+rdi] ; %rdi was %rbx above mov rcx,r9 - movq rdi,xmm1 + movq rdi,xmm1 ; restore %rdi neg rax - movq rsi,xmm1 + movq rsi,xmm1 ; prepare for back-to-back call sar rcx,3+2 - dec r12 + dec r12 ; so that after 'not' we get -n[0] xor r10,r10 mov r13,QWORD[8+rbp] mov r14,QWORD[16+rbp] @@ -2083,7 +2083,7 @@ $L$sqr4x_sub_entry: and r14,rax and r15,rax - neg r10 + neg r10 ; mov %r10,%cf adc r12,QWORD[rbx] adc r13,QWORD[8+rbx] adc r14,QWORD[16+rbx] @@ -2091,16 +2091,16 @@ $L$sqr4x_sub_entry: mov QWORD[rdi],r12 lea rbx,[32+rbx] mov QWORD[8+rdi],r13 - sbb r10,r10 + sbb r10,r10 ; mov %cf,%r10 mov QWORD[16+rdi],r14 mov QWORD[24+rdi],r15 lea rdi,[32+rdi] - inc rcx + inc rcx ; pass %cf jnz NEAR $L$sqr4x_sub - mov r10,r9 - neg r9 + mov r10,r9 ; prepare for back-to-back call + neg r9 ; restore %r9 ret @@ -2138,42 +2138,42 @@ _CET_ENDBR $L$mulx4x_prologue: - - - - shl r9d,3 - lea r10,[r9*2+r9] - neg r9 - mov r8,QWORD[r8] - - - - - - - - - - +; num is declared as an int, a 32-bit parameter, so the upper half is +; undefined. It is important that this write to %r9, which zeros the +; upper half, predates the first access. + shl r9d,3 ; convert %r9 to bytes + lea r10,[r9*2+r9] ; 3*%r9 in bytes + neg r9 ; -%r9 + mov r8,QWORD[r8] ; *n0 + +; ############################################################# +; Ensure that stack frame doesn't alias with +3*%r9 +; modulo 4096, which covers ret[num], am[num] and n[num] +; (see bn_exp.c). This is done to allow memory disambiguation +; logic do its magic. [Extra [num] is allocated in order +; to align with bn_power5's frame, which is cleansed after +; completing exponentiation. Extra 256 bytes is for power mask +; calculated from 7th argument, the index.] +; lea r11,[((-320))+r9*2+rsp] mov rbp,rsp sub r11,rdi and r11,4095 cmp r10,r11 jb NEAR $L$mulx4xsp_alt - sub rbp,r11 - lea rbp,[((-320))+r9*2+rbp] + sub rbp,r11 ; align with + lea rbp,[((-320))+r9*2+rbp] ; future alloca(frame+2*%r9*8+256) jmp NEAR $L$mulx4xsp_done $L$mulx4xsp_alt: lea r10,[((4096-320))+r9*2] - lea rbp,[((-320))+r9*2+rbp] + lea rbp,[((-320))+r9*2+rbp] ; future alloca(frame+2*%r9*8+256) sub r11,r10 mov r10,0 cmovc r11,r10 sub rbp,r11 $L$mulx4xsp_done: - and rbp,-64 + and rbp,-64 ; ensure alignment mov r11,rsp sub r11,rbp and r11,-4096 @@ -2190,25 +2190,25 @@ $L$mulx4x_page_walk: ja NEAR $L$mulx4x_page_walk $L$mulx4x_page_walk_done: - - - - - - - - - - - - - mov QWORD[32+rsp],r8 - mov QWORD[40+rsp],rax +; ############################################################# +; Stack layout +; +0 -num +; +8 off-loaded &b[i] +; +16 end of b[num] +; +24 inner counter +; +32 saved n0 +; +40 saved %rsp +; +48 +; +56 saved rp +; +64 tmp[num+1] +; + mov QWORD[32+rsp],r8 ; save *n0 + mov QWORD[40+rsp],rax ; save original %rsp $L$mulx4x_body: call mulx4x_internal - mov rsi,QWORD[40+rsp] + mov rsi,QWORD[40+rsp] ; restore %rsp mov rax,1 @@ -2237,44 +2237,44 @@ $L$SEH_end_bn_mulx4x_mont_gather5: ALIGN 32 mulx4x_internal: - mov QWORD[8+rsp],r9 + mov QWORD[8+rsp],r9 ; save -%r9 (it was in bytes) mov r10,r9 - neg r9 + neg r9 ; restore %r9 shl r9,5 - neg r10 - lea r13,[128+r9*1+rdx] + neg r10 ; restore %r9 + lea r13,[128+r9*1+rdx] ; end of powers table (+size optimization) shr r9,5+5 - movd xmm5,DWORD[56+rax] + movd xmm5,DWORD[56+rax] ; load 7th argument sub r9,1 lea rax,[$L$inc] - mov QWORD[((16+8))+rsp],r13 - mov QWORD[((24+8))+rsp],r9 - mov QWORD[((56+8))+rsp],rdi - movdqa xmm0,XMMWORD[rax] - movdqa xmm1,XMMWORD[16+rax] - lea r10,[((88-112))+r10*1+rsp] - lea rdi,[128+rdx] - - pshufd xmm5,xmm5,0 + mov QWORD[((16+8))+rsp],r13 ; end of b[num] + mov QWORD[((24+8))+rsp],r9 ; inner counter + mov QWORD[((56+8))+rsp],rdi ; save %rdi + movdqa xmm0,XMMWORD[rax] ; 00000001000000010000000000000000 + movdqa xmm1,XMMWORD[16+rax] ; 00000002000000020000000200000002 + lea r10,[((88-112))+r10*1+rsp] ; place the mask after tp[num+1] (+ICache optimization) + lea rdi,[128+rdx] ; size optimization + + pshufd xmm5,xmm5,0 ; broadcast index movdqa xmm4,xmm1 DB 0x67 movdqa xmm2,xmm1 DB 0x67 paddd xmm1,xmm0 - pcmpeqd xmm0,xmm5 + pcmpeqd xmm0,xmm5 ; compare to 1,0 movdqa xmm3,xmm4 paddd xmm2,xmm1 - pcmpeqd xmm1,xmm5 + pcmpeqd xmm1,xmm5 ; compare to 3,2 movdqa XMMWORD[112+r10],xmm0 movdqa xmm0,xmm4 paddd xmm3,xmm2 - pcmpeqd xmm2,xmm5 + pcmpeqd xmm2,xmm5 ; compare to 5,4 movdqa XMMWORD[128+r10],xmm1 movdqa xmm1,xmm4 paddd xmm0,xmm3 - pcmpeqd xmm3,xmm5 + pcmpeqd xmm3,xmm5 ; compare to 7,6 movdqa XMMWORD[144+r10],xmm2 movdqa xmm2,xmm4 @@ -2283,17 +2283,17 @@ mulx4x_internal: movdqa XMMWORD[160+r10],xmm3 movdqa xmm3,xmm4 paddd xmm2,xmm1 - pcmpeqd xmm1,xmm5 + pcmpeqd xmm1,xmm5 ; compare to 3,2 movdqa XMMWORD[176+r10],xmm0 movdqa xmm0,xmm4 paddd xmm3,xmm2 - pcmpeqd xmm2,xmm5 + pcmpeqd xmm2,xmm5 ; compare to 5,4 movdqa XMMWORD[192+r10],xmm1 movdqa xmm1,xmm4 paddd xmm0,xmm3 - pcmpeqd xmm3,xmm5 + pcmpeqd xmm3,xmm5 ; compare to 7,6 movdqa XMMWORD[208+r10],xmm2 movdqa xmm2,xmm4 @@ -2302,17 +2302,17 @@ mulx4x_internal: movdqa XMMWORD[224+r10],xmm3 movdqa xmm3,xmm4 paddd xmm2,xmm1 - pcmpeqd xmm1,xmm5 + pcmpeqd xmm1,xmm5 ; compare to 3,2 movdqa XMMWORD[240+r10],xmm0 movdqa xmm0,xmm4 paddd xmm3,xmm2 - pcmpeqd xmm2,xmm5 + pcmpeqd xmm2,xmm5 ; compare to 5,4 movdqa XMMWORD[256+r10],xmm1 movdqa xmm1,xmm4 paddd xmm0,xmm3 - pcmpeqd xmm3,xmm5 + pcmpeqd xmm3,xmm5 ; compare to 7,6 movdqa XMMWORD[272+r10],xmm2 movdqa xmm2,xmm4 @@ -2332,7 +2332,7 @@ mulx4x_internal: pcmpeqd xmm3,xmm5 movdqa XMMWORD[336+r10],xmm2 - pand xmm0,XMMWORD[64+rdi] + pand xmm0,XMMWORD[64+rdi] ; while it's still in register pand xmm1,XMMWORD[80+rdi] pand xmm2,XMMWORD[96+rdi] movdqa XMMWORD[352+r10],xmm3 @@ -2376,41 +2376,41 @@ mulx4x_internal: por xmm0,xmm2 por xmm1,xmm3 pxor xmm0,xmm1 - - pshufd xmm1,xmm0,0x4e +; Combine the upper and lower halves of %xmm0. + pshufd xmm1,xmm0,0x4e ; Swap upper and lower halves. por xmm0,xmm1 lea rdi,[256+rdi] - movq rdx,xmm0 + movq rdx,xmm0 ; bp[0] lea rbx,[((64+32+8))+rsp] mov r9,rdx - mulx rax,r8,QWORD[rsi] - mulx r12,r11,QWORD[8+rsi] + mulx rax,r8,QWORD[rsi] ; a[0]*b[0] + mulx r12,r11,QWORD[8+rsi] ; a[1]*b[0] add r11,rax - mulx r13,rax,QWORD[16+rsi] + mulx r13,rax,QWORD[16+rsi] ; ... adc r12,rax adc r13,0 mulx r14,rax,QWORD[24+rsi] mov r15,r8 - imul r8,QWORD[((32+8))+rsp] - xor rbp,rbp + imul r8,QWORD[((32+8))+rsp] ; "t[0]"*n0 + xor rbp,rbp ; cf=0, of=0 mov rdx,r8 - mov QWORD[((8+8))+rsp],rdi + mov QWORD[((8+8))+rsp],rdi ; off-load &b[i] lea rsi,[32+rsi] adcx r13,rax - adcx r14,rbp + adcx r14,rbp ; cf=0 mulx r10,rax,QWORD[rcx] - adcx r15,rax + adcx r15,rax ; discarded adox r10,r11 mulx r11,rax,QWORD[8+rcx] adcx r10,rax adox r11,r12 mulx r12,rax,QWORD[16+rcx] - mov rdi,QWORD[((24+8))+rsp] + mov rdi,QWORD[((24+8))+rsp] ; counter value mov QWORD[((-32))+rbx],r10 adcx r11,rax adox r12,r13 @@ -2418,25 +2418,25 @@ mulx4x_internal: mov rdx,r9 mov QWORD[((-24))+rbx],r11 adcx r12,rax - adox r15,rbp + adox r15,rbp ; of=0 lea rcx,[32+rcx] mov QWORD[((-16))+rbx],r12 jmp NEAR $L$mulx4x_1st ALIGN 32 $L$mulx4x_1st: - adcx r15,rbp - mulx rax,r10,QWORD[rsi] + adcx r15,rbp ; cf=0, modulo-scheduled + mulx rax,r10,QWORD[rsi] ; a[4]*b[0] adcx r10,r14 - mulx r14,r11,QWORD[8+rsi] + mulx r14,r11,QWORD[8+rsi] ; a[5]*b[0] adcx r11,rax - mulx rax,r12,QWORD[16+rsi] + mulx rax,r12,QWORD[16+rsi] ; ... adcx r12,r14 mulx r14,r13,QWORD[24+rsi] DB 0x67,0x67 mov rdx,r8 adcx r13,rax - adcx r14,rbp + adcx r14,rbp ; cf=0 lea rsi,[32+rsi] lea rbx,[32+rbx] @@ -2460,21 +2460,21 @@ $L$mulx4x_1st: lea rcx,[32+rcx] mov QWORD[((-16))+rbx],r13 - dec rdi + dec rdi ; of=0, pass cf jnz NEAR $L$mulx4x_1st - mov rax,QWORD[8+rsp] - adc r15,rbp - lea rsi,[rax*1+rsi] + mov rax,QWORD[8+rsp] ; load -num + adc r15,rbp ; modulo-scheduled + lea rsi,[rax*1+rsi] ; rewind %rsi add r14,r15 - mov rdi,QWORD[((8+8))+rsp] - adc rbp,rbp + mov rdi,QWORD[((8+8))+rsp] ; re-load &b[i] + adc rbp,rbp ; top-most carry mov QWORD[((-8))+rbx],r14 jmp NEAR $L$mulx4x_outer ALIGN 32 $L$mulx4x_outer: - lea r10,[((16-256))+rbx] + lea r10,[((16-256))+rbx] ; where 256-byte mask is (+density control) pxor xmm4,xmm4 DB 0x67,0x67 pxor xmm5,xmm5 @@ -2527,41 +2527,41 @@ $L$mulx4x_outer: por xmm4,xmm2 por xmm5,xmm3 por xmm4,xmm5 - - pshufd xmm0,xmm4,0x4e +; Combine the upper and lower halves of %xmm4 as %xmm0. + pshufd xmm0,xmm4,0x4e ; Swap upper and lower halves. por xmm0,xmm4 lea rdi,[256+rdi] - movq rdx,xmm0 + movq rdx,xmm0 ; m0=bp[i] - mov QWORD[rbx],rbp - lea rbx,[32+rax*1+rbx] - mulx r11,r8,QWORD[rsi] - xor rbp,rbp + mov QWORD[rbx],rbp ; save top-most carry + lea rbx,[32+rax*1+rbx] ; rewind %rbx + mulx r11,r8,QWORD[rsi] ; a[0]*b[i] + xor rbp,rbp ; cf=0, of=0 mov r9,rdx - mulx r12,r14,QWORD[8+rsi] - adox r8,QWORD[((-32))+rbx] + mulx r12,r14,QWORD[8+rsi] ; a[1]*b[i] + adox r8,QWORD[((-32))+rbx] ; +t[0] adcx r11,r14 - mulx r13,r15,QWORD[16+rsi] + mulx r13,r15,QWORD[16+rsi] ; ... adox r11,QWORD[((-24))+rbx] adcx r12,r15 mulx r14,rdx,QWORD[24+rsi] adox r12,QWORD[((-16))+rbx] adcx r13,rdx - lea rcx,[rax*1+rcx] + lea rcx,[rax*1+rcx] ; rewind %rcx lea rsi,[32+rsi] adox r13,QWORD[((-8))+rbx] adcx r14,rbp adox r14,rbp mov r15,r8 - imul r8,QWORD[((32+8))+rsp] + imul r8,QWORD[((32+8))+rsp] ; "t[0]"*n0 mov rdx,r8 - xor rbp,rbp - mov QWORD[((8+8))+rsp],rdi + xor rbp,rbp ; cf=0, of=0 + mov QWORD[((8+8))+rsp],rdi ; off-load &b[i] mulx r10,rax,QWORD[rcx] - adcx r15,rax + adcx r15,rax ; discarded adox r10,r11 mulx r11,rax,QWORD[8+rcx] adcx r10,rax @@ -2571,24 +2571,24 @@ $L$mulx4x_outer: adox r12,r13 mulx r15,rax,QWORD[24+rcx] mov rdx,r9 - mov rdi,QWORD[((24+8))+rsp] + mov rdi,QWORD[((24+8))+rsp] ; counter value mov QWORD[((-32))+rbx],r10 adcx r12,rax mov QWORD[((-24))+rbx],r11 - adox r15,rbp + adox r15,rbp ; of=0 mov QWORD[((-16))+rbx],r12 lea rcx,[32+rcx] jmp NEAR $L$mulx4x_inner ALIGN 32 $L$mulx4x_inner: - mulx rax,r10,QWORD[rsi] - adcx r15,rbp + mulx rax,r10,QWORD[rsi] ; a[4]*b[i] + adcx r15,rbp ; cf=0, modulo-scheduled adox r10,r14 - mulx r14,r11,QWORD[8+rsi] + mulx r14,r11,QWORD[8+rsi] ; a[5]*b[i] adcx r10,QWORD[rbx] adox r11,rax - mulx rax,r12,QWORD[16+rsi] + mulx rax,r12,QWORD[16+rsi] ; ... adcx r11,QWORD[8+rbx] adox r12,r14 mulx r14,r13,QWORD[24+rsi] @@ -2596,10 +2596,10 @@ $L$mulx4x_inner: adcx r12,QWORD[16+rbx] adox r13,rax adcx r13,QWORD[24+rbx] - adox r14,rbp + adox r14,rbp ; of=0 lea rsi,[32+rsi] lea rbx,[32+rbx] - adcx r14,rbp + adcx r14,rbp ; cf=0 adox r10,r15 mulx r15,rax,QWORD[rcx] @@ -2621,17 +2621,17 @@ $L$mulx4x_inner: adox r15,rbp mov QWORD[((-16))+rbx],r13 - dec rdi + dec rdi ; of=0, pass cf jnz NEAR $L$mulx4x_inner - mov rax,QWORD[((0+8))+rsp] - adc r15,rbp - sub rdi,QWORD[rbx] - mov rdi,QWORD[((8+8))+rsp] + mov rax,QWORD[((0+8))+rsp] ; load -num + adc r15,rbp ; modulo-scheduled + sub rdi,QWORD[rbx] ; pull top-most carry to %cf + mov rdi,QWORD[((8+8))+rsp] ; re-load &b[i] mov r10,QWORD[((16+8))+rsp] adc r14,r15 - lea rsi,[rax*1+rsi] - adc rbp,rbp + lea rsi,[rax*1+rsi] ; rewind %rsi + adc rbp,rbp ; top-most carry mov QWORD[((-8))+rbx],r14 cmp rdi,r10 @@ -2640,23 +2640,23 @@ $L$mulx4x_inner: mov r10,QWORD[((-8))+rcx] mov r8,rbp mov r12,QWORD[rax*1+rcx] - lea rbp,[rax*1+rcx] + lea rbp,[rax*1+rcx] ; rewind %rcx mov rcx,rax - lea rdi,[rax*1+rbx] + lea rdi,[rax*1+rbx] ; rewind %rbx xor eax,eax xor r15,r15 - sub r10,r14 + sub r10,r14 ; compare top-most words adc r15,r15 or r8,r15 sar rcx,3+2 - sub rax,r8 - mov rdx,QWORD[((56+8))+rsp] - dec r12 + sub rax,r8 ; %rax=-%r8 + mov rdx,QWORD[((56+8))+rsp] ; restore rp + dec r12 ; so that after 'not' we get -n[0] mov r13,QWORD[8+rbp] xor r8,r8 mov r14,QWORD[16+rbp] mov r15,QWORD[24+rbp] - jmp NEAR $L$sqrx4x_sub_entry + jmp NEAR $L$sqrx4x_sub_entry ; common post-condition global bn_powerx5 @@ -2693,35 +2693,35 @@ _CET_ENDBR $L$powerx5_prologue: - - - - shl r9d,3 - lea r10,[r9*2+r9] +; num is declared as an int, a 32-bit parameter, so the upper half is +; undefined. It is important that this write to %r9, which zeros the +; upper half, predates the first access. + shl r9d,3 ; convert %r9 to bytes + lea r10,[r9*2+r9] ; 3*%r9 in bytes neg r9 - mov r8,QWORD[r8] - - - - - - - - + mov r8,QWORD[r8] ; *n0 + +; ############################################################# +; Ensure that stack frame doesn't alias with %rdi+3*%r9 +; modulo 4096, which covers ret[num], am[num] and n[num] +; (see bn_exp.c). This is done to allow memory disambiguation +; logic do its magic. [Extra 256 bytes is for power mask +; calculated from 7th argument, the index.] +; lea r11,[((-320))+r9*2+rsp] mov rbp,rsp sub r11,rdi and r11,4095 cmp r10,r11 jb NEAR $L$pwrx_sp_alt - sub rbp,r11 - lea rbp,[((-320))+r9*2+rbp] + sub rbp,r11 ; align with %rsi + lea rbp,[((-320))+r9*2+rbp] ; future alloca(frame+2*%r9*8+256) jmp NEAR $L$pwrx_sp_done ALIGN 32 $L$pwrx_sp_alt: lea r10,[((4096-320))+r9*2] - lea rbp,[((-320))+r9*2+rbp] + lea rbp,[((-320))+r9*2+rbp] ; alloca(frame+2*%r9*8+256) sub r11,r10 mov r10,0 cmovc r11,r10 @@ -2747,24 +2747,24 @@ $L$pwrx_page_walk_done: mov r10,r9 neg r9 - - - - - - - - - - - +; ############################################################# +; Stack layout +; +; +0 saved %r9, used in reduction section +; +8 &t[2*%r9], used in reduction section +; +16 intermediate carry bit +; +24 top-most carry bit, used in reduction section +; +32 saved *n0 +; +40 saved %rsp +; +48 t[2*%r9] +; pxor xmm0,xmm0 - movq xmm1,rdi - movq xmm2,rcx - movq xmm3,r10 + movq xmm1,rdi ; save %rdi + movq xmm2,rcx ; save %rcx + movq xmm3,r10 ; -%r9 movq xmm4,rdx mov QWORD[32+rsp],r8 - mov QWORD[40+rsp],rax + mov QWORD[40+rsp],rax ; save original %rsp $L$powerx5_body: @@ -2779,7 +2779,7 @@ $L$powerx5_body: call __bn_sqrx8x_internal call __bn_postx4x_internal - mov r9,r10 + mov r9,r10 ; -num mov rdi,rsi movq rcx,xmm2 movq rdx,xmm4 @@ -2787,7 +2787,7 @@ $L$powerx5_body: call mulx4x_internal - mov rsi,QWORD[40+rsp] + mov rsi,QWORD[40+rsp] ; restore %rsp mov rax,1 @@ -2820,50 +2820,50 @@ bn_sqrx8x_internal: __bn_sqrx8x_internal: _CET_ENDBR - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +; ################################################################# +; Squaring part: +; +; a) multiply-n-add everything but a[i]*a[i]; +; b) shift result of a) by 1 to the left and accumulate +; a[i]*a[i] products; +; +; ################################################################# +; a[7]a[7]a[6]a[6]a[5]a[5]a[4]a[4]a[3]a[3]a[2]a[2]a[1]a[1]a[0]a[0] +; a[1]a[0] +; a[2]a[0] +; a[3]a[0] +; a[2]a[1] +; a[3]a[1] +; a[3]a[2] +; +; a[4]a[0] +; a[5]a[0] +; a[6]a[0] +; a[7]a[0] +; a[4]a[1] +; a[5]a[1] +; a[6]a[1] +; a[7]a[1] +; a[4]a[2] +; a[5]a[2] +; a[6]a[2] +; a[7]a[2] +; a[4]a[3] +; a[5]a[3] +; a[6]a[3] +; a[7]a[3] +; +; a[5]a[4] +; a[6]a[4] +; a[7]a[4] +; a[6]a[5] +; a[7]a[5] +; a[7]a[6] +; a[7]a[7]a[6]a[6]a[5]a[5]a[4]a[4]a[3]a[3]a[2]a[2]a[1]a[1]a[0]a[0] lea rdi,[((48+8))+rsp] lea rbp,[r9*1+rsi] - mov QWORD[((0+8))+rsp],r9 - mov QWORD[((8+8))+rsp],rbp + mov QWORD[((0+8))+rsp],r9 ; save %r9 + mov QWORD[((8+8))+rsp],rbp ; save end of %rsi jmp NEAR $L$sqr8x_zero_start ALIGN 32 @@ -2874,7 +2874,7 @@ $L$sqrx8x_zero: movdqa XMMWORD[16+rdi],xmm0 movdqa XMMWORD[32+rdi],xmm0 movdqa XMMWORD[48+rdi],xmm0 -$L$sqr8x_zero_start: +$L$sqr8x_zero_start: ; aligned at 32 movdqa XMMWORD[64+rdi],xmm0 movdqa XMMWORD[80+rdi],xmm0 movdqa XMMWORD[96+rdi],xmm0 @@ -2883,8 +2883,8 @@ $L$sqr8x_zero_start: sub r9,64 jnz NEAR $L$sqrx8x_zero - mov rdx,QWORD[rsi] - + mov rdx,QWORD[rsi] ; a[0], modulo-scheduled +; xor %r9,%r9 # t[1], ex-%r9, zero already xor r10,r10 xor r11,r11 xor r12,r12 @@ -2892,21 +2892,21 @@ $L$sqr8x_zero_start: xor r14,r14 xor r15,r15 lea rdi,[((48+8))+rsp] - xor rbp,rbp + xor rbp,rbp ; cf=0, cf=0 jmp NEAR $L$sqrx8x_outer_loop ALIGN 32 $L$sqrx8x_outer_loop: - mulx rax,r8,QWORD[8+rsi] - adcx r8,r9 + mulx rax,r8,QWORD[8+rsi] ; a[1]*a[0] + adcx r8,r9 ; a[1]*a[0]+=t[1] adox r10,rax - mulx rax,r9,QWORD[16+rsi] + mulx rax,r9,QWORD[16+rsi] ; a[2]*a[0] adcx r9,r10 adox r11,rax - DB 0xc4,0xe2,0xab,0xf6,0x86,0x18,0x00,0x00,0x00 + DB 0xc4,0xe2,0xab,0xf6,0x86,0x18,0x00,0x00,0x00 ; mulx 3*8(%rsi),%r10,%rax # ... adcx r10,r11 adox r12,rax - DB 0xc4,0xe2,0xa3,0xf6,0x86,0x20,0x00,0x00,0x00 + DB 0xc4,0xe2,0xa3,0xf6,0x86,0x20,0x00,0x00,0x00 ; mulx 4*8(%rsi),%r11,%rax adcx r11,r12 adox r13,rax mulx rax,r12,QWORD[40+rsi] @@ -2916,97 +2916,97 @@ $L$sqrx8x_outer_loop: adcx r13,r14 adox rax,r15 mulx r15,r14,QWORD[56+rsi] - mov rdx,QWORD[8+rsi] + mov rdx,QWORD[8+rsi] ; a[1] adcx r14,rax adox r15,rbp adc r15,QWORD[64+rdi] - mov QWORD[8+rdi],r8 - mov QWORD[16+rdi],r9 - sbb rcx,rcx - xor rbp,rbp + mov QWORD[8+rdi],r8 ; t[1] + mov QWORD[16+rdi],r9 ; t[2] + sbb rcx,rcx ; mov %cf,%rcx + xor rbp,rbp ; cf=0, of=0 - mulx rbx,r8,QWORD[16+rsi] - mulx rax,r9,QWORD[24+rsi] + mulx rbx,r8,QWORD[16+rsi] ; a[2]*a[1] + mulx rax,r9,QWORD[24+rsi] ; a[3]*a[1] adcx r8,r10 adox r9,rbx - mulx rbx,r10,QWORD[32+rsi] + mulx rbx,r10,QWORD[32+rsi] ; ... adcx r9,r11 adox r10,rax - DB 0xc4,0xe2,0xa3,0xf6,0x86,0x28,0x00,0x00,0x00 + DB 0xc4,0xe2,0xa3,0xf6,0x86,0x28,0x00,0x00,0x00 ; mulx 5*8(%rsi),%r11,%rax adcx r10,r12 adox r11,rbx - DB 0xc4,0xe2,0x9b,0xf6,0x9e,0x30,0x00,0x00,0x00 + DB 0xc4,0xe2,0x9b,0xf6,0x9e,0x30,0x00,0x00,0x00 ; mulx 6*8(%rsi),%r12,%rbx adcx r11,r13 adox r12,r14 - DB 0xc4,0x62,0x93,0xf6,0xb6,0x38,0x00,0x00,0x00 - mov rdx,QWORD[16+rsi] + DB 0xc4,0x62,0x93,0xf6,0xb6,0x38,0x00,0x00,0x00 ; mulx 7*8(%rsi),%r13,%r14 + mov rdx,QWORD[16+rsi] ; a[2] adcx r12,rax adox r13,rbx adcx r13,r15 - adox r14,rbp - adcx r14,rbp + adox r14,rbp ; of=0 + adcx r14,rbp ; cf=0 - mov QWORD[24+rdi],r8 - mov QWORD[32+rdi],r9 + mov QWORD[24+rdi],r8 ; t[3] + mov QWORD[32+rdi],r9 ; t[4] - mulx rbx,r8,QWORD[24+rsi] - mulx rax,r9,QWORD[32+rsi] + mulx rbx,r8,QWORD[24+rsi] ; a[3]*a[2] + mulx rax,r9,QWORD[32+rsi] ; a[4]*a[2] adcx r8,r10 adox r9,rbx - mulx rbx,r10,QWORD[40+rsi] + mulx rbx,r10,QWORD[40+rsi] ; ... adcx r9,r11 adox r10,rax - DB 0xc4,0xe2,0xa3,0xf6,0x86,0x30,0x00,0x00,0x00 + DB 0xc4,0xe2,0xa3,0xf6,0x86,0x30,0x00,0x00,0x00 ; mulx 6*8(%rsi),%r11,%rax adcx r10,r12 adox r11,r13 - DB 0xc4,0x62,0x9b,0xf6,0xae,0x38,0x00,0x00,0x00 + DB 0xc4,0x62,0x9b,0xf6,0xae,0x38,0x00,0x00,0x00 ; mulx 7*8(%rsi),%r12,%r13 DB 0x3e - mov rdx,QWORD[24+rsi] + mov rdx,QWORD[24+rsi] ; a[3] adcx r11,rbx adox r12,rax adcx r12,r14 - mov QWORD[40+rdi],r8 - mov QWORD[48+rdi],r9 - mulx rax,r8,QWORD[32+rsi] - adox r13,rbp - adcx r13,rbp + mov QWORD[40+rdi],r8 ; t[5] + mov QWORD[48+rdi],r9 ; t[6] + mulx rax,r8,QWORD[32+rsi] ; a[4]*a[3] + adox r13,rbp ; of=0 + adcx r13,rbp ; cf=0 - mulx rbx,r9,QWORD[40+rsi] + mulx rbx,r9,QWORD[40+rsi] ; a[5]*a[3] adcx r8,r10 adox r9,rax - mulx rax,r10,QWORD[48+rsi] + mulx rax,r10,QWORD[48+rsi] ; ... adcx r9,r11 adox r10,r12 mulx r12,r11,QWORD[56+rsi] - mov rdx,QWORD[32+rsi] - mov r14,QWORD[40+rsi] + mov rdx,QWORD[32+rsi] ; a[4] + mov r14,QWORD[40+rsi] ; a[5] adcx r10,rbx adox r11,rax - mov r15,QWORD[48+rsi] + mov r15,QWORD[48+rsi] ; a[6] adcx r11,r13 - adox r12,rbp - adcx r12,rbp + adox r12,rbp ; of=0 + adcx r12,rbp ; cf=0 - mov QWORD[56+rdi],r8 - mov QWORD[64+rdi],r9 + mov QWORD[56+rdi],r8 ; t[7] + mov QWORD[64+rdi],r9 ; t[8] - mulx rax,r9,r14 - mov r8,QWORD[56+rsi] + mulx rax,r9,r14 ; a[5]*a[4] + mov r8,QWORD[56+rsi] ; a[7] adcx r9,r10 - mulx rbx,r10,r15 + mulx rbx,r10,r15 ; a[6]*a[4] adox r10,rax adcx r10,r11 - mulx rax,r11,r8 - mov rdx,r14 + mulx rax,r11,r8 ; a[7]*a[4] + mov rdx,r14 ; a[5] adox r11,rbx adcx r11,r12 +; adox %rbp,%rax # of=0 + adcx rax,rbp ; cf=0 - adcx rax,rbp - - mulx rbx,r14,r15 - mulx r13,r12,r8 - mov rdx,r15 + mulx rbx,r14,r15 ; a[6]*a[5] + mulx r13,r12,r8 ; a[7]*a[5] + mov rdx,r15 ; a[6] lea rsi,[64+rsi] adcx r11,r14 adox r12,rbx @@ -3014,19 +3014,19 @@ $L$sqrx8x_outer_loop: adox r13,rbp DB 0x67,0x67 - mulx r14,r8,r8 + mulx r14,r8,r8 ; a[7]*a[6] adcx r13,r8 adcx r14,rbp cmp rsi,QWORD[((8+8))+rsp] je NEAR $L$sqrx8x_outer_break - neg rcx + neg rcx ; mov %rcx,%cf mov rcx,-8 mov r15,rbp mov r8,QWORD[64+rdi] - adcx r9,QWORD[72+rdi] - adcx r10,QWORD[80+rdi] + adcx r9,QWORD[72+rdi] ; +=t[9] + adcx r10,QWORD[80+rdi] ; ... adcx r11,QWORD[88+rdi] adc r12,QWORD[96+rdi] adc r13,QWORD[104+rdi] @@ -3034,24 +3034,24 @@ $L$sqrx8x_outer_loop: adc r15,QWORD[120+rdi] lea rbp,[rsi] lea rdi,[128+rdi] - sbb rax,rax + sbb rax,rax ; mov %cf,%rcx - mov rdx,QWORD[((-64))+rsi] - mov QWORD[((16+8))+rsp],rax + mov rdx,QWORD[((-64))+rsi] ; a[0] + mov QWORD[((16+8))+rsp],rax ; offload %rcx mov QWORD[((24+8))+rsp],rdi - - xor eax,eax +; lea 8*8(%rdi),%rdi # see 2*8*8(%rdi) above + xor eax,eax ; cf=0, of=0 jmp NEAR $L$sqrx8x_loop ALIGN 32 $L$sqrx8x_loop: mov rbx,r8 - mulx r8,rax,QWORD[rbp] - adcx rbx,rax + mulx r8,rax,QWORD[rbp] ; a[8]*a[i] + adcx rbx,rax ; +=t[8] adox r8,r9 - mulx r9,rax,QWORD[8+rbp] + mulx r9,rax,QWORD[8+rbp] ; ... adcx r8,rax adox r9,r10 @@ -3063,7 +3063,7 @@ $L$sqrx8x_loop: adcx r10,rax adox r11,r12 - DB 0xc4,0x62,0xfb,0xf6,0xa5,0x20,0x00,0x00,0x00 + DB 0xc4,0x62,0xfb,0xf6,0xa5,0x20,0x00,0x00,0x00 ; mulx 4*8(%rbp),%rax,%r12 adcx r11,rax adox r12,r13 @@ -3072,27 +3072,27 @@ $L$sqrx8x_loop: adox r13,r14 mulx r14,rax,QWORD[48+rbp] - mov QWORD[rcx*8+rdi],rbx + mov QWORD[rcx*8+rdi],rbx ; store t[8+i] mov ebx,0 adcx r13,rax adox r14,r15 - DB 0xc4,0x62,0xfb,0xf6,0xbd,0x38,0x00,0x00,0x00 - mov rdx,QWORD[8+rcx*8+rsi] + DB 0xc4,0x62,0xfb,0xf6,0xbd,0x38,0x00,0x00,0x00 ; mulx 7*8(%rbp),%rax,%r15 + mov rdx,QWORD[8+rcx*8+rsi] ; a[i] adcx r14,rax - adox r15,rbx - adcx r15,rbx + adox r15,rbx ; %rbx is 0, of=0 + adcx r15,rbx ; cf=0 DB 0x67 - inc rcx + inc rcx ; of=0 jnz NEAR $L$sqrx8x_loop lea rbp,[64+rbp] mov rcx,-8 - cmp rbp,QWORD[((8+8))+rsp] + cmp rbp,QWORD[((8+8))+rsp] ; done? je NEAR $L$sqrx8x_break - sub rbx,QWORD[((16+8))+rsp] + sub rbx,QWORD[((16+8))+rsp] ; mov 16(%rsp),%cf DB 0x66 mov rdx,QWORD[((-64))+rsi] adcx r8,QWORD[rdi] @@ -3105,19 +3105,19 @@ $L$sqrx8x_loop: adc r15,QWORD[56+rdi] lea rdi,[64+rdi] DB 0x67 - sbb rax,rax - xor ebx,ebx - mov QWORD[((16+8))+rsp],rax + sbb rax,rax ; mov %cf,%rax + xor ebx,ebx ; cf=0, of=0 + mov QWORD[((16+8))+rsp],rax ; offload carry jmp NEAR $L$sqrx8x_loop ALIGN 32 $L$sqrx8x_break: xor rbp,rbp - sub rbx,QWORD[((16+8))+rsp] + sub rbx,QWORD[((16+8))+rsp] ; mov 16(%rsp),%cf adcx r8,rbp - mov rcx,QWORD[((24+8))+rsp] + mov rcx,QWORD[((24+8))+rsp] ; initial %rdi, borrow %rcx adcx r9,rbp - mov rdx,QWORD[rsi] + mov rdx,QWORD[rsi] ; a[8], modulo-scheduled adc r10,0 mov QWORD[rdi],r8 adc r11,0 @@ -3125,7 +3125,7 @@ $L$sqrx8x_break: adc r13,0 adc r14,0 adc r15,0 - cmp rdi,rcx + cmp rdi,rcx ; cf=0, of=0 je NEAR $L$sqrx8x_outer_loop mov QWORD[8+rdi],r9 @@ -3147,57 +3147,57 @@ $L$sqrx8x_break: ALIGN 32 $L$sqrx8x_outer_break: - mov QWORD[72+rdi],r9 - movq rcx,xmm3 - mov QWORD[80+rdi],r10 + mov QWORD[72+rdi],r9 ; t[9] + movq rcx,xmm3 ; -%r9 + mov QWORD[80+rdi],r10 ; ... mov QWORD[88+rdi],r11 mov QWORD[96+rdi],r12 mov QWORD[104+rdi],r13 mov QWORD[112+rdi],r14 lea rdi,[((48+8))+rsp] - mov rdx,QWORD[rcx*1+rsi] + mov rdx,QWORD[rcx*1+rsi] ; a[0] - mov r11,QWORD[8+rdi] - xor r10,r10 - mov r9,QWORD[((0+8))+rsp] + mov r11,QWORD[8+rdi] ; t[1] + xor r10,r10 ; t[0], of=0, cf=0 + mov r9,QWORD[((0+8))+rsp] ; restore %r9 adox r11,r11 - mov r12,QWORD[16+rdi] - mov r13,QWORD[24+rdi] - + mov r12,QWORD[16+rdi] ; t[2] # prefetch + mov r13,QWORD[24+rdi] ; t[3] # prefetch +; jmp .Lsqrx4x_shift_n_add # happens to be aligned ALIGN 32 $L$sqrx4x_shift_n_add: mulx rbx,rax,rdx adox r12,r12 adcx rax,r10 - DB 0x48,0x8b,0x94,0x0e,0x08,0x00,0x00,0x00 - DB 0x4c,0x8b,0x97,0x20,0x00,0x00,0x00 + DB 0x48,0x8b,0x94,0x0e,0x08,0x00,0x00,0x00 ; mov 8(%rsi,%rcx),%rdx # a[i+1] # prefetch + DB 0x4c,0x8b,0x97,0x20,0x00,0x00,0x00 ; mov 32(%rdi),%r10 # t[2*i+4] # prefetch adox r13,r13 adcx rbx,r11 - mov r11,QWORD[40+rdi] + mov r11,QWORD[40+rdi] ; t[2*i+4+1] # prefetch mov QWORD[rdi],rax mov QWORD[8+rdi],rbx mulx rbx,rax,rdx adox r10,r10 adcx rax,r12 - mov rdx,QWORD[16+rcx*1+rsi] - mov r12,QWORD[48+rdi] + mov rdx,QWORD[16+rcx*1+rsi] ; a[i+2] # prefetch + mov r12,QWORD[48+rdi] ; t[2*i+6] # prefetch adox r11,r11 adcx rbx,r13 - mov r13,QWORD[56+rdi] + mov r13,QWORD[56+rdi] ; t[2*i+6+1] # prefetch mov QWORD[16+rdi],rax mov QWORD[24+rdi],rbx mulx rbx,rax,rdx adox r12,r12 adcx rax,r10 - mov rdx,QWORD[24+rcx*1+rsi] + mov rdx,QWORD[24+rcx*1+rsi] ; a[i+3] # prefetch lea rcx,[32+rcx] - mov r10,QWORD[64+rdi] + mov r10,QWORD[64+rdi] ; t[2*i+8] # prefetch adox r13,r13 adcx rbx,r11 - mov r11,QWORD[72+rdi] + mov r11,QWORD[72+rdi] ; t[2*i+8+1] # prefetch mov QWORD[32+rdi],rax mov QWORD[40+rdi],rbx @@ -3205,11 +3205,11 @@ $L$sqrx4x_shift_n_add: adox r10,r10 adcx rax,r12 jrcxz $L$sqrx4x_shift_n_add_break - DB 0x48,0x8b,0x94,0x0e,0x00,0x00,0x00,0x00 + DB 0x48,0x8b,0x94,0x0e,0x00,0x00,0x00,0x00 ; mov 0(%rsi,%rcx),%rdx # a[i+4] # prefetch adox r11,r11 adcx rbx,r13 - mov r12,QWORD[80+rdi] - mov r13,QWORD[88+rdi] + mov r12,QWORD[80+rdi] ; t[2*i+10] # prefetch + mov r13,QWORD[88+rdi] ; t[2*i+10+1] # prefetch mov QWORD[48+rdi],rax mov QWORD[56+rdi],rbx lea rdi,[64+rdi] @@ -3221,18 +3221,18 @@ $L$sqrx4x_shift_n_add_break: adcx rbx,r13 mov QWORD[48+rdi],rax mov QWORD[56+rdi],rbx - lea rdi,[64+rdi] + lea rdi,[64+rdi] ; end of t[] buffer movq rbp,xmm2 __bn_sqrx8x_reduction: - xor eax,eax - mov rbx,QWORD[((32+8))+rsp] - mov rdx,QWORD[((48+8))+rsp] - lea rcx,[((-64))+r9*1+rbp] - - mov QWORD[((0+8))+rsp],rcx - mov QWORD[((8+8))+rsp],rdi - - lea rdi,[((48+8))+rsp] + xor eax,eax ; initial top-most carry bit + mov rbx,QWORD[((32+8))+rsp] ; n0 + mov rdx,QWORD[((48+8))+rsp] ; "%r8", 8*0(%rdi) + lea rcx,[((-64))+r9*1+rbp] ; end of n[] +; lea 48+8(%rsp,%r9,2),%rdi # end of t[] buffer + mov QWORD[((0+8))+rsp],rcx ; save end of n[] + mov QWORD[((8+8))+rsp],rdi ; save end of t[] + + lea rdi,[((48+8))+rsp] ; initial t[] window jmp NEAR $L$sqrx8x_reduction_loop ALIGN 32 @@ -3242,25 +3242,25 @@ $L$sqrx8x_reduction_loop: mov r11,QWORD[24+rdi] mov r12,QWORD[32+rdi] mov r8,rdx - imul rdx,rbx + imul rdx,rbx ; n0*a[i] mov r13,QWORD[40+rdi] mov r14,QWORD[48+rdi] mov r15,QWORD[56+rdi] - mov QWORD[((24+8))+rsp],rax + mov QWORD[((24+8))+rsp],rax ; store top-most carry bit lea rdi,[64+rdi] - xor rsi,rsi + xor rsi,rsi ; cf=0,of=0 mov rcx,-8 jmp NEAR $L$sqrx8x_reduce ALIGN 32 $L$sqrx8x_reduce: mov rbx,r8 - mulx r8,rax,QWORD[rbp] - adcx rax,rbx + mulx r8,rax,QWORD[rbp] ; n[0] + adcx rax,rbx ; discarded adox r8,r9 - mulx r9,rbx,QWORD[8+rbp] + mulx r9,rbx,QWORD[8+rbp] ; n[1] adcx r8,rbx adox r9,r10 @@ -3272,15 +3272,15 @@ $L$sqrx8x_reduce: adcx r10,rbx adox r11,r12 - DB 0xc4,0x62,0xe3,0xf6,0xa5,0x20,0x00,0x00,0x00 + DB 0xc4,0x62,0xe3,0xf6,0xa5,0x20,0x00,0x00,0x00 ; mulx 8*4(%rbp),%rbx,%r12 mov rax,rdx mov rdx,r8 adcx r11,rbx adox r12,r13 - mulx rdx,rbx,QWORD[((32+8))+rsp] + mulx rdx,rbx,QWORD[((32+8))+rsp] ; %rdx discarded mov rdx,rax - mov QWORD[((64+48+8))+rcx*8+rsp],rax + mov QWORD[((64+48+8))+rcx*8+rsp],rax ; put aside n0*a[i] mulx r13,rax,QWORD[40+rbp] adcx r12,rax @@ -3293,18 +3293,18 @@ $L$sqrx8x_reduce: mulx r15,rax,QWORD[56+rbp] mov rdx,rbx adcx r14,rax - adox r15,rsi - adcx r15,rsi + adox r15,rsi ; %rsi is 0 + adcx r15,rsi ; cf=0 DB 0x67,0x67,0x67 - inc rcx + inc rcx ; of=0 jnz NEAR $L$sqrx8x_reduce - mov rax,rsi - cmp rbp,QWORD[((0+8))+rsp] + mov rax,rsi ; xor %rax,%rax + cmp rbp,QWORD[((0+8))+rsp] ; end of n[]? jae NEAR $L$sqrx8x_no_tail - mov rdx,QWORD[((48+8))+rsp] + mov rdx,QWORD[((48+8))+rsp] ; pull n0*a[0] add r8,QWORD[rdi] lea rbp,[64+rbp] mov rcx,-8 @@ -3316,9 +3316,9 @@ $L$sqrx8x_reduce: adc r14,QWORD[48+rdi] adc r15,QWORD[56+rdi] lea rdi,[64+rdi] - sbb rax,rax + sbb rax,rax ; top carry - xor rsi,rsi + xor rsi,rsi ; of=0, cf=0 mov QWORD[((16+8))+rsp],rax jmp NEAR $L$sqrx8x_tail @@ -3341,7 +3341,7 @@ $L$sqrx8x_tail: adcx r10,rax adox r11,r12 - DB 0xc4,0x62,0xfb,0xf6,0xa5,0x20,0x00,0x00,0x00 + DB 0xc4,0x62,0xfb,0xf6,0xa5,0x20,0x00,0x00,0x00 ; mulx 8*4(%rbp),%rax,%r12 adcx r11,rax adox r12,r13 @@ -3354,21 +3354,21 @@ $L$sqrx8x_tail: adox r14,r15 mulx r15,rax,QWORD[56+rbp] - mov rdx,QWORD[((72+48+8))+rcx*8+rsp] + mov rdx,QWORD[((72+48+8))+rcx*8+rsp] ; pull n0*a[i] adcx r14,rax adox r15,rsi - mov QWORD[rcx*8+rdi],rbx + mov QWORD[rcx*8+rdi],rbx ; save result mov rbx,r8 - adcx r15,rsi + adcx r15,rsi ; cf=0 - inc rcx + inc rcx ; of=0 jnz NEAR $L$sqrx8x_tail - cmp rbp,QWORD[((0+8))+rsp] - jae NEAR $L$sqrx8x_tail_done + cmp rbp,QWORD[((0+8))+rsp] ; end of n[]? + jae NEAR $L$sqrx8x_tail_done ; break out of loop - sub rsi,QWORD[((16+8))+rsp] - mov rdx,QWORD[((48+8))+rsp] + sub rsi,QWORD[((16+8))+rsp] ; mov 16(%rsp),%cf + mov rdx,QWORD[((48+8))+rsp] ; pull n0*a[0] lea rbp,[64+rbp] adc r8,QWORD[rdi] adc r9,QWORD[8+rdi] @@ -3380,16 +3380,16 @@ $L$sqrx8x_tail: adc r15,QWORD[56+rdi] lea rdi,[64+rdi] sbb rax,rax - sub rcx,8 + sub rcx,8 ; mov $-8,%rcx - xor rsi,rsi + xor rsi,rsi ; of=0, cf=0 mov QWORD[((16+8))+rsp],rax jmp NEAR $L$sqrx8x_tail ALIGN 32 $L$sqrx8x_tail_done: xor rax,rax - add r8,QWORD[((24+8))+rsp] + add r8,QWORD[((24+8))+rsp] ; can this overflow? adc r9,0 adc r10,0 adc r11,0 @@ -3399,26 +3399,26 @@ $L$sqrx8x_tail_done: adc r15,0 adc rax,0 - sub rsi,QWORD[((16+8))+rsp] -$L$sqrx8x_no_tail: + sub rsi,QWORD[((16+8))+rsp] ; mov 16(%rsp),%cf +$L$sqrx8x_no_tail: ; %cf is 0 if jumped here adc r8,QWORD[rdi] movq rcx,xmm3 adc r9,QWORD[8+rdi] mov rsi,QWORD[56+rbp] - movq rbp,xmm2 + movq rbp,xmm2 ; restore %rbp adc r10,QWORD[16+rdi] adc r11,QWORD[24+rdi] adc r12,QWORD[32+rdi] adc r13,QWORD[40+rdi] adc r14,QWORD[48+rdi] adc r15,QWORD[56+rdi] - adc rax,0 + adc rax,0 ; top-most carry - mov rbx,QWORD[((32+8))+rsp] - mov rdx,QWORD[64+rcx*1+rdi] + mov rbx,QWORD[((32+8))+rsp] ; n0 + mov rdx,QWORD[64+rcx*1+rdi] ; modulo-scheduled "%r8" - mov QWORD[rdi],r8 - lea r8,[64+rdi] + mov QWORD[rdi],r8 ; store top 512 bits + lea r8,[64+rdi] ; borrow %r8 mov QWORD[8+rdi],r9 mov QWORD[16+rdi],r10 mov QWORD[24+rdi],r11 @@ -3427,8 +3427,8 @@ $L$sqrx8x_no_tail: mov QWORD[48+rdi],r14 mov QWORD[56+rdi],r15 - lea rdi,[64+rcx*1+rdi] - cmp r8,QWORD[((8+8))+rsp] + lea rdi,[64+rcx*1+rdi] ; start of current t[] window + cmp r8,QWORD[((8+8))+rsp] ; end of t[]? jb NEAR $L$sqrx8x_reduction_loop ret @@ -3438,14 +3438,14 @@ ALIGN 32 __bn_postx4x_internal: mov r12,QWORD[rbp] - mov r10,rcx - mov r9,rcx + mov r10,rcx ; -%r9 + mov r9,rcx ; -%r9 neg rax sar rcx,3+2 - - movq rdx,xmm1 - movq rsi,xmm1 - dec r12 +; lea 48+8(%rsp,%r9),%rdi + movq rdx,xmm1 ; restore %rdx + movq rsi,xmm1 ; prepare for back-to-back call + dec r12 ; so that after 'not' we get -n[0] mov r13,QWORD[8+rbp] xor r8,r8 mov r14,QWORD[16+rbp] @@ -3465,7 +3465,7 @@ $L$sqrx4x_sub_entry: andn r14,r14,rax andn r15,r15,rax - neg r8 + neg r8 ; mov %r8,%cf adc r12,QWORD[rdi] adc r13,QWORD[8+rdi] adc r14,QWORD[16+rdi] @@ -3473,7 +3473,7 @@ $L$sqrx4x_sub_entry: mov QWORD[rdx],r12 lea rdi,[32+rdi] mov QWORD[8+rdx],r13 - sbb r8,r8 + sbb r8,r8 ; mov %cf,%r8 mov QWORD[16+rdx],r14 mov QWORD[24+rdx],r15 lea rdx,[32+rdx] @@ -3481,7 +3481,7 @@ $L$sqrx4x_sub_entry: inc rcx jnz NEAR $L$sqrx4x_sub - neg r9 + neg r9 ; restore %r9 ret @@ -3495,13 +3495,13 @@ _CET_ENDBR cmp edx,0 jz NEAR $L$scatter_epilogue - - - - - - - +; %r8 stores 32 entries, t0 through t31. Each entry has %edx words. +; They are interleaved in memory as follows: +; +; t0[0] t1[0] t2[0] ... t31[0] +; t0[1] t1[1] t2[1] ... t31[1] +; ... +; t0[%edx-1] t1[%edx-1] t2[%edx-1] ... t31[%edx-1] lea r8,[r9*8+r8] $L$scatter: @@ -3521,97 +3521,97 @@ global bn_gather5 ALIGN 32 bn_gather5: -$L$SEH_begin_bn_gather5: +$L$SEH_begin_bn_gather5: ; Win64 thing, but harmless in other cases _CET_ENDBR +; I can't trust assembler to use specific encoding:-( + DB 0x4c,0x8d,0x14,0x24 ; lea (%rsp),%r10 - DB 0x4c,0x8d,0x14,0x24 - - DB 0x48,0x81,0xec,0x08,0x01,0x00,0x00 + DB 0x48,0x81,0xec,0x08,0x01,0x00,0x00 ; sub $0x108,%rsp lea rax,[$L$inc] - and rsp,-16 + and rsp,-16 ; shouldn't be formally required movd xmm5,r9d - movdqa xmm0,XMMWORD[rax] - movdqa xmm1,XMMWORD[16+rax] - lea r11,[128+r8] - lea rax,[128+rsp] + movdqa xmm0,XMMWORD[rax] ; 00000001000000010000000000000000 + movdqa xmm1,XMMWORD[16+rax] ; 00000002000000020000000200000002 + lea r11,[128+r8] ; size optimization + lea rax,[128+rsp] ; size optimization - pshufd xmm5,xmm5,0 + pshufd xmm5,xmm5,0 ; broadcast %r9d movdqa xmm4,xmm1 movdqa xmm2,xmm1 paddd xmm1,xmm0 - pcmpeqd xmm0,xmm5 + pcmpeqd xmm0,xmm5 ; compare to 1,0 movdqa xmm3,xmm4 paddd xmm2,xmm1 - pcmpeqd xmm1,xmm5 + pcmpeqd xmm1,xmm5 ; compare to 3,2 movdqa XMMWORD[(-128)+rax],xmm0 movdqa xmm0,xmm4 paddd xmm3,xmm2 - pcmpeqd xmm2,xmm5 + pcmpeqd xmm2,xmm5 ; compare to 5,4 movdqa XMMWORD[(-112)+rax],xmm1 movdqa xmm1,xmm4 paddd xmm0,xmm3 - pcmpeqd xmm3,xmm5 + pcmpeqd xmm3,xmm5 ; compare to 7,6 movdqa XMMWORD[(-96)+rax],xmm2 movdqa xmm2,xmm4 paddd xmm1,xmm0 - pcmpeqd xmm0,xmm5 + pcmpeqd xmm0,xmm5 ; compare to 1,0 movdqa XMMWORD[(-80)+rax],xmm3 movdqa xmm3,xmm4 paddd xmm2,xmm1 - pcmpeqd xmm1,xmm5 + pcmpeqd xmm1,xmm5 ; compare to 3,2 movdqa XMMWORD[(-64)+rax],xmm0 movdqa xmm0,xmm4 paddd xmm3,xmm2 - pcmpeqd xmm2,xmm5 + pcmpeqd xmm2,xmm5 ; compare to 5,4 movdqa XMMWORD[(-48)+rax],xmm1 movdqa xmm1,xmm4 paddd xmm0,xmm3 - pcmpeqd xmm3,xmm5 + pcmpeqd xmm3,xmm5 ; compare to 7,6 movdqa XMMWORD[(-32)+rax],xmm2 movdqa xmm2,xmm4 paddd xmm1,xmm0 - pcmpeqd xmm0,xmm5 + pcmpeqd xmm0,xmm5 ; compare to 1,0 movdqa XMMWORD[(-16)+rax],xmm3 movdqa xmm3,xmm4 paddd xmm2,xmm1 - pcmpeqd xmm1,xmm5 + pcmpeqd xmm1,xmm5 ; compare to 3,2 movdqa XMMWORD[rax],xmm0 movdqa xmm0,xmm4 paddd xmm3,xmm2 - pcmpeqd xmm2,xmm5 + pcmpeqd xmm2,xmm5 ; compare to 5,4 movdqa XMMWORD[16+rax],xmm1 movdqa xmm1,xmm4 paddd xmm0,xmm3 - pcmpeqd xmm3,xmm5 + pcmpeqd xmm3,xmm5 ; compare to 7,6 movdqa XMMWORD[32+rax],xmm2 movdqa xmm2,xmm4 paddd xmm1,xmm0 - pcmpeqd xmm0,xmm5 + pcmpeqd xmm0,xmm5 ; compare to 1,0 movdqa XMMWORD[48+rax],xmm3 movdqa xmm3,xmm4 paddd xmm2,xmm1 - pcmpeqd xmm1,xmm5 + pcmpeqd xmm1,xmm5 ; compare to 3,2 movdqa XMMWORD[64+rax],xmm0 movdqa xmm0,xmm4 paddd xmm3,xmm2 - pcmpeqd xmm2,xmm5 + pcmpeqd xmm2,xmm5 ; compare to 5,4 movdqa XMMWORD[80+rax],xmm1 movdqa xmm1,xmm4 paddd xmm0,xmm3 - pcmpeqd xmm3,xmm5 + pcmpeqd xmm3,xmm5 ; compare to 7,6 movdqa XMMWORD[96+rax],xmm2 movdqa xmm2,xmm4 movdqa XMMWORD[112+rax],xmm3 @@ -3671,10 +3671,10 @@ $L$gather: por xmm5,xmm3 por xmm4,xmm5 lea r11,[256+r11] - - pshufd xmm0,xmm4,0x4e +; Combine the upper and lower halves of %xmm0. + pshufd xmm0,xmm4,0x4e ; Swap upper and lower halves. por xmm0,xmm4 - movq QWORD[rcx],xmm0 + movq QWORD[rcx],xmm0 ; m0=bp[0] lea rcx,[8+rcx] sub edx,1 jnz NEAR $L$gather @@ -3686,7 +3686,9 @@ $L$SEH_end_bn_gather5: section .rdata rdata align=8 + ALIGN 64 +mont5_increments: $L$inc: DD 0,0,1,1 DD 2,2,2,2 @@ -3696,7 +3698,7 @@ $L$inc: DB 114,32,120,56,54,95,54,52,44,32,67,82,89,80,84,79 DB 71,65,77,83,32,98,121,32,60,97,112,112,114,111,64,111 DB 112,101,110,115,115,108,46,111,114,103,62,0 -section .text +section .text code align=64 EXTERN __imp_RtlVirtualUnwind @@ -3713,40 +3715,40 @@ mul_handler: pushfq sub rsp,64 - mov rax,QWORD[120+r8] - mov rbx,QWORD[248+r8] + mov rax,QWORD[120+r8] ; pull context->Rax + mov rbx,QWORD[248+r8] ; pull context->Rip - mov rsi,QWORD[8+r9] - mov r11,QWORD[56+r9] + mov rsi,QWORD[8+r9] ; disp->ImageBase + mov r11,QWORD[56+r9] ; disp->HandlerData - mov r10d,DWORD[r11] - lea r10,[r10*1+rsi] - cmp rbx,r10 + mov r10d,DWORD[r11] ; HandlerData[0] + lea r10,[r10*1+rsi] ; end of prologue label + cmp rbx,r10 ; context->RipRipRsp - mov r10d,DWORD[8+r11] - lea r10,[r10*1+rsi] - cmp rbx,r10 + mov r10d,DWORD[8+r11] ; HandlerData[2] + lea r10,[r10*1+rsi] ; epilogue label + cmp rbx,r10 ; context->Rip>=epilogue label jae NEAR $L$common_seh_tail lea r10,[$L$mul_epilogue] cmp rbx,r10 ja NEAR $L$body_40 - mov r10,QWORD[192+r8] - mov rax,QWORD[8+r10*8+rax] + mov r10,QWORD[192+r8] ; pull %r9 + mov rax,QWORD[8+r10*8+rax] ; pull saved stack pointer jmp NEAR $L$common_pop_regs $L$body_40: - mov rax,QWORD[40+rax] + mov rax,QWORD[40+rax] ; pull saved stack pointer $L$common_pop_regs: mov rbx,QWORD[((-8))+rax] mov rbp,QWORD[((-16))+rax] @@ -3754,40 +3756,40 @@ $L$common_pop_regs: mov r13,QWORD[((-32))+rax] mov r14,QWORD[((-40))+rax] mov r15,QWORD[((-48))+rax] - mov QWORD[144+r8],rbx - mov QWORD[160+r8],rbp - mov QWORD[216+r8],r12 - mov QWORD[224+r8],r13 - mov QWORD[232+r8],r14 - mov QWORD[240+r8],r15 + mov QWORD[144+r8],rbx ; restore context->Rbx + mov QWORD[160+r8],rbp ; restore context->Rbp + mov QWORD[216+r8],r12 ; restore context->R12 + mov QWORD[224+r8],r13 ; restore context->R13 + mov QWORD[232+r8],r14 ; restore context->R14 + mov QWORD[240+r8],r15 ; restore context->R15 $L$common_seh_tail: mov rdi,QWORD[8+rax] mov rsi,QWORD[16+rax] - mov QWORD[152+r8],rax - mov QWORD[168+r8],rsi - mov QWORD[176+r8],rdi + mov QWORD[152+r8],rax ; restore context->Rsp + mov QWORD[168+r8],rsi ; restore context->Rsi + mov QWORD[176+r8],rdi ; restore context->Rdi - mov rdi,QWORD[40+r9] - mov rsi,r8 - mov ecx,154 - DD 0xa548f3fc + mov rdi,QWORD[40+r9] ; disp->ContextRecord + mov rsi,r8 ; context + mov ecx,154 ; sizeof(CONTEXT) + DD 0xa548f3fc ; cld; rep movsq mov rsi,r9 - xor rcx,rcx - mov rdx,QWORD[8+rsi] - mov r8,QWORD[rsi] - mov r9,QWORD[16+rsi] - mov r10,QWORD[40+rsi] - lea r11,[56+rsi] - lea r12,[24+rsi] - mov QWORD[32+rsp],r10 - mov QWORD[40+rsp],r11 - mov QWORD[48+rsp],r12 - mov QWORD[56+rsp],rcx + xor rcx,rcx ; arg1, UNW_FLAG_NHANDLER + mov rdx,QWORD[8+rsi] ; arg2, disp->ImageBase + mov r8,QWORD[rsi] ; arg3, disp->ControlPc + mov r9,QWORD[16+rsi] ; arg4, disp->FunctionEntry + mov r10,QWORD[40+rsi] ; disp->ContextRecord + lea r11,[56+rsi] ; &disp->HandlerData + lea r12,[24+rsi] ; &disp->EstablisherFrame + mov QWORD[32+rsp],r10 ; arg5 + mov QWORD[40+rsp],r11 ; arg6 + mov QWORD[48+rsp],r12 ; arg7 + mov QWORD[56+rsp],rcx ; arg8, (NULL) call QWORD[__imp_RtlVirtualUnwind] - mov eax,1 + mov eax,1 ; ExceptionContinueSearch add rsp,64 popfq pop r15 @@ -3830,32 +3832,32 @@ ALIGN 8 $L$SEH_info_bn_mul_mont_gather5_nohw: DB 9,0,0,0 DD mul_handler wrt ..imagebase - DD $L$mul_body wrt ..imagebase,$L$mul_body wrt ..imagebase,$L$mul_epilogue wrt ..imagebase + DD $L$mul_body wrt ..imagebase,$L$mul_body wrt ..imagebase,$L$mul_epilogue wrt ..imagebase ; HandlerData[] ALIGN 8 $L$SEH_info_bn_mul4x_mont_gather5: DB 9,0,0,0 DD mul_handler wrt ..imagebase - DD $L$mul4x_prologue wrt ..imagebase,$L$mul4x_body wrt ..imagebase,$L$mul4x_epilogue wrt ..imagebase + DD $L$mul4x_prologue wrt ..imagebase,$L$mul4x_body wrt ..imagebase,$L$mul4x_epilogue wrt ..imagebase ; HandlerData[] ALIGN 8 $L$SEH_info_bn_power5_nohw: DB 9,0,0,0 DD mul_handler wrt ..imagebase - DD $L$power5_prologue wrt ..imagebase,$L$power5_body wrt ..imagebase,$L$power5_epilogue wrt ..imagebase + DD $L$power5_prologue wrt ..imagebase,$L$power5_body wrt ..imagebase,$L$power5_epilogue wrt ..imagebase ; HandlerData[] ALIGN 8 $L$SEH_info_bn_mulx4x_mont_gather5: DB 9,0,0,0 DD mul_handler wrt ..imagebase - DD $L$mulx4x_prologue wrt ..imagebase,$L$mulx4x_body wrt ..imagebase,$L$mulx4x_epilogue wrt ..imagebase + DD $L$mulx4x_prologue wrt ..imagebase,$L$mulx4x_body wrt ..imagebase,$L$mulx4x_epilogue wrt ..imagebase ; HandlerData[] ALIGN 8 $L$SEH_info_bn_powerx5: DB 9,0,0,0 DD mul_handler wrt ..imagebase - DD $L$powerx5_prologue wrt ..imagebase,$L$powerx5_body wrt ..imagebase,$L$powerx5_epilogue wrt ..imagebase + DD $L$powerx5_prologue wrt ..imagebase,$L$powerx5_body wrt ..imagebase,$L$powerx5_epilogue wrt ..imagebase ; HandlerData[] ALIGN 8 $L$SEH_info_bn_gather5: DB 0x01,0x0b,0x03,0x0a - DB 0x0b,0x01,0x21,0x00 - DB 0x04,0xa3,0x00,0x00 + DB 0x0b,0x01,0x21,0x00 ; sub rsp,0x108 + DB 0x04,0xa3,0x00,0x00 ; lea r10,(rsp) ALIGN 8 %else ; Work around https://bugzilla.nasm.us/show_bug.cgi?id=3392738 diff --git a/third_party/boringssl/gen/crypto/aes128gcmsiv-x86_64-apple.S b/third_party/boringssl/gen/crypto/aes128gcmsiv-x86_64-apple.S index 81e2f071b..eff851d31 100644 --- a/third_party/boringssl/gen/crypto/aes128gcmsiv-x86_64-apple.S +++ b/third_party/boringssl/gen/crypto/aes128gcmsiv-x86_64-apple.S @@ -9,33 +9,47 @@ .p2align 4 one: .quad 1,0 +.alt_entry two two: .quad 2,0 +.alt_entry three three: .quad 3,0 +.alt_entry four four: .quad 4,0 +.alt_entry five five: .quad 5,0 +.alt_entry six six: .quad 6,0 +.alt_entry seven seven: .quad 7,0 +.alt_entry eight eight: .quad 8,0 +.alt_entry OR_MASK OR_MASK: .long 0x00000000,0x00000000,0x00000000,0x80000000 +.alt_entry poly poly: .quad 0x1, 0xc200000000000000 +.alt_entry mask mask: .long 0x0c0f0e0d,0x0c0f0e0d,0x0c0f0e0d,0x0c0f0e0d +.alt_entry con1 con1: .long 1,1,1,1 +.alt_entry con2 con2: .long 0x1b,0x1b,0x1b,0x1b +.alt_entry con3 con3: .byte -1,-1,-1,-1,-1,-1,-1,-1,4,5,6,7,4,5,6,7 +.alt_entry and_mask and_mask: .long 0,0xffffffff, 0xffffffff, 0xffffffff .text @@ -69,26 +83,27 @@ GFMUL: .private_extern _aesgcmsiv_htable_init .p2align 4 +.alt_entry _aesgcmsiv_htable_init _aesgcmsiv_htable_init: _CET_ENDBR vmovdqa (%rsi),%xmm0 vmovdqa %xmm0,%xmm1 - vmovdqa %xmm0,(%rdi) + vmovdqa %xmm0,(%rdi) // H call GFMUL - vmovdqa %xmm0,16(%rdi) + vmovdqa %xmm0,16(%rdi) // H^2 call GFMUL - vmovdqa %xmm0,32(%rdi) + vmovdqa %xmm0,32(%rdi) // H^3 call GFMUL - vmovdqa %xmm0,48(%rdi) + vmovdqa %xmm0,48(%rdi) // H^4 call GFMUL - vmovdqa %xmm0,64(%rdi) + vmovdqa %xmm0,64(%rdi) // H^5 call GFMUL - vmovdqa %xmm0,80(%rdi) + vmovdqa %xmm0,80(%rdi) // H^6 call GFMUL - vmovdqa %xmm0,96(%rdi) + vmovdqa %xmm0,96(%rdi) // H^7 call GFMUL - vmovdqa %xmm0,112(%rdi) + vmovdqa %xmm0,112(%rdi) // H^8 ret @@ -96,22 +111,23 @@ _CET_ENDBR .private_extern _aesgcmsiv_htable6_init .p2align 4 +.alt_entry _aesgcmsiv_htable6_init _aesgcmsiv_htable6_init: _CET_ENDBR vmovdqa (%rsi),%xmm0 vmovdqa %xmm0,%xmm1 - vmovdqa %xmm0,(%rdi) + vmovdqa %xmm0,(%rdi) // H call GFMUL - vmovdqa %xmm0,16(%rdi) + vmovdqa %xmm0,16(%rdi) // H^2 call GFMUL - vmovdqa %xmm0,32(%rdi) + vmovdqa %xmm0,32(%rdi) // H^3 call GFMUL - vmovdqa %xmm0,48(%rdi) + vmovdqa %xmm0,48(%rdi) // H^4 call GFMUL - vmovdqa %xmm0,64(%rdi) + vmovdqa %xmm0,64(%rdi) // H^5 call GFMUL - vmovdqa %xmm0,80(%rdi) + vmovdqa %xmm0,80(%rdi) // H^6 ret @@ -119,6 +135,7 @@ _CET_ENDBR .private_extern _aesgcmsiv_htable_polyval .p2align 4 +.alt_entry _aesgcmsiv_htable_polyval _aesgcmsiv_htable_polyval: _CET_ENDBR @@ -129,8 +146,8 @@ _CET_ENDBR L$htable_polyval_start: vzeroall - - +// We hash 8 blocks each iteration. If the total number of blocks is not a +// multiple of 8, we first hash the leading n%8 blocks. movq %rdx,%r11 andq $127,%r11 @@ -142,7 +159,7 @@ L$htable_polyval_start: subq $16,%r11 - +// hash first prefix block vmovdqu (%rsi),%xmm0 vpxor %xmm1,%xmm0,%xmm0 @@ -157,12 +174,12 @@ L$htable_polyval_start: jnz L$htable_polyval_prefix_loop jmp L$htable_polyval_prefix_complete - +// hash remaining prefix blocks (up to 7 total prefix blocks) .p2align 6 L$htable_polyval_prefix_loop: subq $16,%r11 - vmovdqu (%rsi),%xmm0 + vmovdqu (%rsi),%xmm0 // next data block vpclmulqdq $0x00,(%rdi,%r11,1),%xmm0,%xmm6 vpxor %xmm6,%xmm3,%xmm3 @@ -189,10 +206,10 @@ L$htable_polyval_prefix_complete: jmp L$htable_polyval_main_loop L$htable_polyval_no_prefix: - - - - +// At this point we know the number of blocks is a multiple of 8. However, +// the reduction in the main loop includes a multiplication by x^(-128). In +// order to counter this, the existing tag needs to be multiplied by x^128. +// In practice, this just means that it is loaded into %xmm9, not %xmm1. vpxor %xmm1,%xmm1,%xmm1 vmovdqa (%rcx),%xmm9 @@ -201,7 +218,7 @@ L$htable_polyval_main_loop: subq $0x80,%rdx jb L$htable_polyval_out - vmovdqu 112(%rsi),%xmm0 + vmovdqu 112(%rsi),%xmm0 // Ii vpclmulqdq $0x01,(%rdi),%xmm0,%xmm5 vpclmulqdq $0x00,(%rdi),%xmm0,%xmm3 @@ -209,7 +226,7 @@ L$htable_polyval_main_loop: vpclmulqdq $0x10,(%rdi),%xmm0,%xmm6 vpxor %xmm6,%xmm5,%xmm5 - +// ######################################################## vmovdqu 96(%rsi),%xmm0 vpclmulqdq $0x01,16(%rdi),%xmm0,%xmm6 vpxor %xmm6,%xmm5,%xmm5 @@ -221,10 +238,10 @@ L$htable_polyval_main_loop: vpxor %xmm6,%xmm5,%xmm5 - +// ######################################################## vmovdqu 80(%rsi),%xmm0 - vpclmulqdq $0x10,poly(%rip),%xmm1,%xmm7 + vpclmulqdq $0x10,poly(%rip),%xmm1,%xmm7 // reduction stage 1a vpalignr $8,%xmm1,%xmm1,%xmm1 vpclmulqdq $0x01,32(%rdi),%xmm0,%xmm6 @@ -237,8 +254,8 @@ L$htable_polyval_main_loop: vpxor %xmm6,%xmm5,%xmm5 - vpxor %xmm7,%xmm1,%xmm1 - + vpxor %xmm7,%xmm1,%xmm1 // reduction stage 1b +// ######################################################## vmovdqu 64(%rsi),%xmm0 vpclmulqdq $0x01,48(%rdi),%xmm0,%xmm6 @@ -250,10 +267,10 @@ L$htable_polyval_main_loop: vpclmulqdq $0x10,48(%rdi),%xmm0,%xmm6 vpxor %xmm6,%xmm5,%xmm5 - +// ######################################################## vmovdqu 48(%rsi),%xmm0 - vpclmulqdq $0x10,poly(%rip),%xmm1,%xmm7 + vpclmulqdq $0x10,poly(%rip),%xmm1,%xmm7 // reduction stage 2a vpalignr $8,%xmm1,%xmm1,%xmm1 vpclmulqdq $0x01,64(%rdi),%xmm0,%xmm6 @@ -266,8 +283,8 @@ L$htable_polyval_main_loop: vpxor %xmm6,%xmm5,%xmm5 - vpxor %xmm7,%xmm1,%xmm1 - + vpxor %xmm7,%xmm1,%xmm1 // reduction stage 2b +// ######################################################## vmovdqu 32(%rsi),%xmm0 vpclmulqdq $0x01,80(%rdi),%xmm0,%xmm6 @@ -280,8 +297,8 @@ L$htable_polyval_main_loop: vpxor %xmm6,%xmm5,%xmm5 - vpxor %xmm9,%xmm1,%xmm1 - + vpxor %xmm9,%xmm1,%xmm1 // reduction finalize +// ######################################################## vmovdqu 16(%rsi),%xmm0 vpclmulqdq $0x01,96(%rdi),%xmm0,%xmm6 @@ -293,7 +310,7 @@ L$htable_polyval_main_loop: vpclmulqdq $0x10,96(%rdi),%xmm0,%xmm6 vpxor %xmm6,%xmm5,%xmm5 - +// ######################################################## vmovdqu 0(%rsi),%xmm0 vpxor %xmm1,%xmm0,%xmm0 @@ -306,7 +323,7 @@ L$htable_polyval_main_loop: vpclmulqdq $0x10,112(%rdi),%xmm0,%xmm6 vpxor %xmm6,%xmm5,%xmm5 - +// ######################################################## vpsrldq $8,%xmm5,%xmm6 vpslldq $8,%xmm5,%xmm5 @@ -316,7 +333,7 @@ L$htable_polyval_main_loop: leaq 128(%rsi),%rsi jmp L$htable_polyval_main_loop - +// ######################################################## L$htable_polyval_out: vpclmulqdq $0x10,poly(%rip),%xmm1,%xmm6 @@ -337,6 +354,7 @@ L$htable_polyval_out: .private_extern _aesgcmsiv_polyval_horner .p2align 4 +.alt_entry _aesgcmsiv_polyval_horner _aesgcmsiv_polyval_horner: _CET_ENDBR @@ -345,24 +363,24 @@ _CET_ENDBR ret L$polyval_horner_start: - - +// We will start with L GFMULS for POLYVAL(BIG_BUFFER) +// RES = GFMUL(RES, H) xorq %r10,%r10 - shlq $4,%rcx + shlq $4,%rcx // L contains number of bytes to process vmovdqa (%rsi),%xmm1 vmovdqa (%rdi),%xmm0 L$polyval_horner_loop: - vpxor (%rdx,%r10,1),%xmm0,%xmm0 - call GFMUL + vpxor (%rdx,%r10,1),%xmm0,%xmm0 // RES = RES + Xi + call GFMUL // RES = RES * H addq $16,%r10 cmpq %r10,%rcx jne L$polyval_horner_loop - +// calculation of T is complete. RES=T vmovdqa %xmm0,(%rdi) ret @@ -371,11 +389,12 @@ L$polyval_horner_loop: .private_extern _aes128gcmsiv_aes_ks .p2align 4 +.alt_entry _aes128gcmsiv_aes_ks _aes128gcmsiv_aes_ks: _CET_ENDBR - vmovdqu (%rdi),%xmm1 - vmovdqa %xmm1,(%rsi) + vmovdqu (%rdi),%xmm1 // xmm1 = user key + vmovdqa %xmm1,(%rsi) // rsi points to output vmovdqa con1(%rip),%xmm0 vmovdqa mask(%rip),%xmm15 @@ -383,9 +402,9 @@ _CET_ENDBR movq $8,%rax L$ks128_loop: - addq $16,%rsi + addq $16,%rsi // rsi points for next key subq $1,%rax - vpshufb %xmm15,%xmm1,%xmm2 + vpshufb %xmm15,%xmm1,%xmm2 // xmm2 = shuffled user key vaesenclast %xmm0,%xmm2,%xmm2 vpslld $1,%xmm0,%xmm0 vpslldq $4,%xmm1,%xmm3 @@ -428,6 +447,7 @@ L$ks128_loop: .private_extern _aes256gcmsiv_aes_ks .p2align 4 +.alt_entry _aes256gcmsiv_aes_ks _aes256gcmsiv_aes_ks: _CET_ENDBR @@ -476,22 +496,23 @@ L$ks256_loop: .private_extern _aes128gcmsiv_aes_ks_enc_x1 .p2align 4 +.alt_entry _aes128gcmsiv_aes_ks_enc_x1 _aes128gcmsiv_aes_ks_enc_x1: _CET_ENDBR - vmovdqa (%rcx),%xmm1 + vmovdqa (%rcx),%xmm1 // xmm1 = first 16 bytes of random key vmovdqa 0(%rdi),%xmm4 - vmovdqa %xmm1,(%rdx) + vmovdqa %xmm1,(%rdx) // KEY[0] = first 16 bytes of random key vpxor %xmm1,%xmm4,%xmm4 - vmovdqa con1(%rip),%xmm0 - vmovdqa mask(%rip),%xmm15 + vmovdqa con1(%rip),%xmm0 // xmm0 = 1,1,1,1 + vmovdqa mask(%rip),%xmm15 // xmm15 = mask - vpshufb %xmm15,%xmm1,%xmm2 + vpshufb %xmm15,%xmm1,%xmm2 // vaesenclast %xmm0,%xmm2,%xmm2 vpslld $1,%xmm0,%xmm0 - vpsllq $32,%xmm1,%xmm3 + vpsllq $32,%xmm1,%xmm3 // vpxor %xmm3,%xmm1,%xmm1 vpshufb con3(%rip),%xmm1,%xmm3 vpxor %xmm3,%xmm1,%xmm1 @@ -500,10 +521,10 @@ _CET_ENDBR vaesenc %xmm1,%xmm4,%xmm4 vmovdqa %xmm1,16(%rdx) - vpshufb %xmm15,%xmm1,%xmm2 + vpshufb %xmm15,%xmm1,%xmm2 // vaesenclast %xmm0,%xmm2,%xmm2 vpslld $1,%xmm0,%xmm0 - vpsllq $32,%xmm1,%xmm3 + vpsllq $32,%xmm1,%xmm3 // vpxor %xmm3,%xmm1,%xmm1 vpshufb con3(%rip),%xmm1,%xmm3 vpxor %xmm3,%xmm1,%xmm1 @@ -512,10 +533,10 @@ _CET_ENDBR vaesenc %xmm1,%xmm4,%xmm4 vmovdqa %xmm1,32(%rdx) - vpshufb %xmm15,%xmm1,%xmm2 + vpshufb %xmm15,%xmm1,%xmm2 // vaesenclast %xmm0,%xmm2,%xmm2 vpslld $1,%xmm0,%xmm0 - vpsllq $32,%xmm1,%xmm3 + vpsllq $32,%xmm1,%xmm3 // vpxor %xmm3,%xmm1,%xmm1 vpshufb con3(%rip),%xmm1,%xmm3 vpxor %xmm3,%xmm1,%xmm1 @@ -524,10 +545,10 @@ _CET_ENDBR vaesenc %xmm1,%xmm4,%xmm4 vmovdqa %xmm1,48(%rdx) - vpshufb %xmm15,%xmm1,%xmm2 + vpshufb %xmm15,%xmm1,%xmm2 // vaesenclast %xmm0,%xmm2,%xmm2 vpslld $1,%xmm0,%xmm0 - vpsllq $32,%xmm1,%xmm3 + vpsllq $32,%xmm1,%xmm3 // vpxor %xmm3,%xmm1,%xmm1 vpshufb con3(%rip),%xmm1,%xmm3 vpxor %xmm3,%xmm1,%xmm1 @@ -536,10 +557,10 @@ _CET_ENDBR vaesenc %xmm1,%xmm4,%xmm4 vmovdqa %xmm1,64(%rdx) - vpshufb %xmm15,%xmm1,%xmm2 + vpshufb %xmm15,%xmm1,%xmm2 // vaesenclast %xmm0,%xmm2,%xmm2 vpslld $1,%xmm0,%xmm0 - vpsllq $32,%xmm1,%xmm3 + vpsllq $32,%xmm1,%xmm3 // vpxor %xmm3,%xmm1,%xmm1 vpshufb con3(%rip),%xmm1,%xmm3 vpxor %xmm3,%xmm1,%xmm1 @@ -548,10 +569,10 @@ _CET_ENDBR vaesenc %xmm1,%xmm4,%xmm4 vmovdqa %xmm1,80(%rdx) - vpshufb %xmm15,%xmm1,%xmm2 + vpshufb %xmm15,%xmm1,%xmm2 // vaesenclast %xmm0,%xmm2,%xmm2 vpslld $1,%xmm0,%xmm0 - vpsllq $32,%xmm1,%xmm3 + vpsllq $32,%xmm1,%xmm3 // vpxor %xmm3,%xmm1,%xmm1 vpshufb con3(%rip),%xmm1,%xmm3 vpxor %xmm3,%xmm1,%xmm1 @@ -560,10 +581,10 @@ _CET_ENDBR vaesenc %xmm1,%xmm4,%xmm4 vmovdqa %xmm1,96(%rdx) - vpshufb %xmm15,%xmm1,%xmm2 + vpshufb %xmm15,%xmm1,%xmm2 // vaesenclast %xmm0,%xmm2,%xmm2 vpslld $1,%xmm0,%xmm0 - vpsllq $32,%xmm1,%xmm3 + vpsllq $32,%xmm1,%xmm3 // vpxor %xmm3,%xmm1,%xmm1 vpshufb con3(%rip),%xmm1,%xmm3 vpxor %xmm3,%xmm1,%xmm1 @@ -572,10 +593,10 @@ _CET_ENDBR vaesenc %xmm1,%xmm4,%xmm4 vmovdqa %xmm1,112(%rdx) - vpshufb %xmm15,%xmm1,%xmm2 + vpshufb %xmm15,%xmm1,%xmm2 // vaesenclast %xmm0,%xmm2,%xmm2 vpslld $1,%xmm0,%xmm0 - vpsllq $32,%xmm1,%xmm3 + vpsllq $32,%xmm1,%xmm3 // vpxor %xmm3,%xmm1,%xmm1 vpshufb con3(%rip),%xmm1,%xmm3 vpxor %xmm3,%xmm1,%xmm1 @@ -587,10 +608,10 @@ _CET_ENDBR vmovdqa con2(%rip),%xmm0 - vpshufb %xmm15,%xmm1,%xmm2 + vpshufb %xmm15,%xmm1,%xmm2 // vaesenclast %xmm0,%xmm2,%xmm2 vpslld $1,%xmm0,%xmm0 - vpsllq $32,%xmm1,%xmm3 + vpsllq $32,%xmm1,%xmm3 // vpxor %xmm3,%xmm1,%xmm1 vpshufb con3(%rip),%xmm1,%xmm3 vpxor %xmm3,%xmm1,%xmm1 @@ -599,9 +620,9 @@ _CET_ENDBR vaesenc %xmm1,%xmm4,%xmm4 vmovdqa %xmm1,144(%rdx) - vpshufb %xmm15,%xmm1,%xmm2 + vpshufb %xmm15,%xmm1,%xmm2 // vaesenclast %xmm0,%xmm2,%xmm2 - vpsllq $32,%xmm1,%xmm3 + vpsllq $32,%xmm1,%xmm3 // vpxor %xmm3,%xmm1,%xmm1 vpshufb con3(%rip),%xmm1,%xmm3 vpxor %xmm3,%xmm1,%xmm1 @@ -619,14 +640,15 @@ _CET_ENDBR .private_extern _aes128gcmsiv_kdf .p2align 4 +.alt_entry _aes128gcmsiv_kdf _aes128gcmsiv_kdf: _CET_ENDBR +// parameter 1: %rdi Pointer to NONCE +// parameter 2: %rsi Pointer to CT +// parameter 4: %rdx Pointer to keys - - - - vmovdqa (%rdx),%xmm1 + vmovdqa (%rdx),%xmm1 // xmm1 = first 16 bytes of random key vmovdqa 0(%rdi),%xmm9 vmovdqa and_mask(%rip),%xmm12 vmovdqa one(%rip),%xmm13 @@ -713,6 +735,7 @@ _CET_ENDBR .private_extern _aes128gcmsiv_enc_msg_x4 .p2align 4 +.alt_entry _aes128gcmsiv_enc_msg_x4 _aes128gcmsiv_enc_msg_x4: _CET_ENDBR @@ -726,20 +749,20 @@ L$128_enc_msg_x4_start: pushq %r13 - shrq $4,%r8 + shrq $4,%r8 // LEN = num of blocks movq %r8,%r10 shlq $62,%r10 shrq $62,%r10 - +// make IV from TAG vmovdqa (%rdx),%xmm15 - vpor OR_MASK(%rip),%xmm15,%xmm15 + vpor OR_MASK(%rip),%xmm15,%xmm15 // IV = [1]TAG[126...32][00..00] - vmovdqu four(%rip),%xmm4 - vmovdqa %xmm15,%xmm0 - vpaddd one(%rip),%xmm15,%xmm1 - vpaddd two(%rip),%xmm15,%xmm2 - vpaddd three(%rip),%xmm15,%xmm3 + vmovdqu four(%rip),%xmm4 // Register to increment counters + vmovdqa %xmm15,%xmm0 // CTR1 = TAG[1][127...32][00..00] + vpaddd one(%rip),%xmm15,%xmm1 // CTR2 = TAG[1][127...32][00..01] + vpaddd two(%rip),%xmm15,%xmm2 // CTR3 = TAG[1][127...32][00..02] + vpaddd three(%rip),%xmm15,%xmm3 // CTR4 = TAG[1][127...32][00..03] shrq $2,%r8 je L$128_enc_msg_x4_check_remainder @@ -827,7 +850,7 @@ L$128_enc_msg_x4_loop1: vaesenclast %xmm12,%xmm8,%xmm8 - +// XOR with Plaintext vpxor 0(%rdi),%xmm5,%xmm5 vpxor 16(%rdi),%xmm6,%xmm6 vpxor 32(%rdi),%xmm7,%xmm7 @@ -850,10 +873,10 @@ L$128_enc_msg_x4_check_remainder: je L$128_enc_msg_x4_out L$128_enc_msg_x4_loop2: - - +// enc each block separately +// CTR1 is the highest counter (even if no LOOP done) vmovdqa %xmm0,%xmm5 - vpaddd one(%rip),%xmm0,%xmm0 + vpaddd one(%rip),%xmm0,%xmm0 // inc counter vpxor (%rcx),%xmm5,%xmm5 vaesenc 16(%rcx),%xmm5,%xmm5 @@ -867,7 +890,7 @@ L$128_enc_msg_x4_loop2: vaesenc 144(%rcx),%xmm5,%xmm5 vaesenclast 160(%rcx),%xmm5,%xmm5 - +// XOR with plaintext vpxor (%rdi),%xmm5,%xmm5 vmovdqu %xmm5,(%rsi) @@ -889,6 +912,7 @@ L$128_enc_msg_x4_out: .private_extern _aes128gcmsiv_enc_msg_x8 .p2align 4 +.alt_entry _aes128gcmsiv_enc_msg_x8 _aes128gcmsiv_enc_msg_x8: _CET_ENDBR @@ -906,29 +930,29 @@ L$128_enc_msg_x8_start: movq %rsp,%rbp - +// Place in stack subq $128,%rsp andq $-64,%rsp - shrq $4,%r8 + shrq $4,%r8 // LEN = num of blocks movq %r8,%r10 shlq $61,%r10 shrq $61,%r10 - +// make IV from TAG vmovdqu (%rdx),%xmm1 - vpor OR_MASK(%rip),%xmm1,%xmm1 - + vpor OR_MASK(%rip),%xmm1,%xmm1 // TMP1= IV = [1]TAG[126...32][00..00] +// store counter8 in the stack vpaddd seven(%rip),%xmm1,%xmm0 - vmovdqu %xmm0,(%rsp) - vpaddd one(%rip),%xmm1,%xmm9 - vpaddd two(%rip),%xmm1,%xmm10 - vpaddd three(%rip),%xmm1,%xmm11 - vpaddd four(%rip),%xmm1,%xmm12 - vpaddd five(%rip),%xmm1,%xmm13 - vpaddd six(%rip),%xmm1,%xmm14 - vmovdqa %xmm1,%xmm0 + vmovdqu %xmm0,(%rsp) // CTR8 = TAG[127...32][00..07] + vpaddd one(%rip),%xmm1,%xmm9 // CTR2 = TAG[127...32][00..01] + vpaddd two(%rip),%xmm1,%xmm10 // CTR3 = TAG[127...32][00..02] + vpaddd three(%rip),%xmm1,%xmm11 // CTR4 = TAG[127...32][00..03] + vpaddd four(%rip),%xmm1,%xmm12 // CTR5 = TAG[127...32][00..04] + vpaddd five(%rip),%xmm1,%xmm13 // CTR6 = TAG[127...32][00..05] + vpaddd six(%rip),%xmm1,%xmm14 // CTR7 = TAG[127...32][00..06] + vmovdqa %xmm1,%xmm0 // CTR1 = TAG[127...32][00..00] shrq $3,%r8 je L$128_enc_msg_x8_check_remainder @@ -947,7 +971,7 @@ L$128_enc_msg_x8_loop1: vmovdqa %xmm12,%xmm5 vmovdqa %xmm13,%xmm6 vmovdqa %xmm14,%xmm7 - +// move from stack vmovdqu (%rsp),%xmm8 vpxor (%rcx),%xmm1,%xmm1 @@ -969,7 +993,7 @@ L$128_enc_msg_x8_loop1: vaesenc %xmm15,%xmm7,%xmm7 vaesenc %xmm15,%xmm8,%xmm8 - vmovdqu (%rsp),%xmm14 + vmovdqu (%rsp),%xmm14 // deal with CTR8 vpaddd eight(%rip),%xmm14,%xmm14 vmovdqu %xmm14,(%rsp) vmovdqu 32(%rcx),%xmm15 @@ -1070,7 +1094,7 @@ L$128_enc_msg_x8_loop1: vaesenclast %xmm15,%xmm8,%xmm8 - +// XOR with Plaintext vpxor 0(%rdi),%xmm1,%xmm1 vpxor 16(%rdi),%xmm2,%xmm2 vpxor 32(%rdi),%xmm3,%xmm3 @@ -1101,10 +1125,10 @@ L$128_enc_msg_x8_check_remainder: je L$128_enc_msg_x8_out L$128_enc_msg_x8_loop2: - - +// enc each block separately +// CTR1 is the highest counter (even if no LOOP done) vmovdqa %xmm0,%xmm1 - vpaddd one(%rip),%xmm0,%xmm0 + vpaddd one(%rip),%xmm0,%xmm0 // inc counter vpxor (%rcx),%xmm1,%xmm1 vaesenc 16(%rcx),%xmm1,%xmm1 @@ -1118,7 +1142,7 @@ L$128_enc_msg_x8_loop2: vaesenc 144(%rcx),%xmm1,%xmm1 vaesenclast 160(%rcx),%xmm1,%xmm1 - +// XOR with Plaintext vpxor (%rdi),%xmm1,%xmm1 vmovdqu %xmm1,(%rsi) @@ -1145,6 +1169,7 @@ L$128_enc_msg_x8_out: .private_extern _aes128gcmsiv_dec .p2align 4 +.alt_entry _aes128gcmsiv_dec _aes128gcmsiv_dec: _CET_ENDBR @@ -1155,10 +1180,10 @@ _CET_ENDBR L$128_dec_start: vzeroupper vmovdqa (%rdx),%xmm0 - - +// The claimed tag is provided after the current calculated tag value. +// CTRBLKs is made from it. vmovdqu 16(%rdx),%xmm15 - vpor OR_MASK(%rip),%xmm15,%xmm15 + vpor OR_MASK(%rip),%xmm15,%xmm15 // CTR = [1]TAG[126...32][00..00] movq %rdx,%rax leaq 32(%rax),%rax @@ -1166,11 +1191,11 @@ L$128_dec_start: andq $~15,%r9 - +// If less then 6 blocks, make singles cmpq $96,%r9 jb L$128_dec_loop2 - +// Decrypt the first six blocks subq $96,%r9 vmovdqa %xmm15,%xmm7 vpaddd one(%rip),%xmm7,%xmm8 @@ -1267,7 +1292,7 @@ L$128_dec_start: vaesenclast %xmm4,%xmm11,%xmm11 vaesenclast %xmm4,%xmm12,%xmm12 - +// XOR with CT vpxor 0(%rdi),%xmm7,%xmm7 vpxor 16(%rdi),%xmm8,%xmm8 vpxor 32(%rdi),%xmm9,%xmm9 @@ -1286,7 +1311,7 @@ L$128_dec_start: addq $96,%rsi jmp L$128_dec_loop1 - +// Decrypt 6 blocks each time while hashing previous 6 blocks .p2align 6 L$128_dec_loop1: cmpq $96,%r9 @@ -1598,9 +1623,9 @@ L$128_dec_finish_96: vpxor %xmm5,%xmm0,%xmm0 L$128_dec_loop2: +// Here we encrypt any remaining whole block - - +// if there are no whole blocks cmpq $16,%r9 jb L$128_dec_out subq $16,%r9 @@ -1639,6 +1664,7 @@ L$128_dec_out: .private_extern _aes128gcmsiv_ecb_enc_block .p2align 4 +.alt_entry _aes128gcmsiv_ecb_enc_block _aes128gcmsiv_ecb_enc_block: _CET_ENDBR @@ -1654,7 +1680,7 @@ _CET_ENDBR vaesenc 112(%rdx),%xmm1,%xmm1 vaesenc 128(%rdx),%xmm1,%xmm1 vaesenc 144(%rdx),%xmm1,%xmm1 - vaesenclast 160(%rdx),%xmm1,%xmm1 + vaesenclast 160(%rdx),%xmm1,%xmm1 // STATE_1 == IV vmovdqa %xmm1,(%rsi) @@ -1665,17 +1691,18 @@ _CET_ENDBR .private_extern _aes256gcmsiv_aes_ks_enc_x1 .p2align 4 +.alt_entry _aes256gcmsiv_aes_ks_enc_x1 _aes256gcmsiv_aes_ks_enc_x1: _CET_ENDBR - vmovdqa con1(%rip),%xmm0 - vmovdqa mask(%rip),%xmm15 + vmovdqa con1(%rip),%xmm0 // CON_MASK = 1,1,1,1 + vmovdqa mask(%rip),%xmm15 // MASK_256 vmovdqa (%rdi),%xmm8 - vmovdqa (%rcx),%xmm1 + vmovdqa (%rcx),%xmm1 // KEY_1 || KEY_2 [0..7] = user key vmovdqa 16(%rcx),%xmm3 vpxor %xmm1,%xmm8,%xmm8 vaesenc %xmm3,%xmm8,%xmm8 - vmovdqu %xmm1,(%rdx) + vmovdqu %xmm1,(%rdx) // First round key vmovdqu %xmm3,16(%rdx) vpxor %xmm14,%xmm14,%xmm14 @@ -1849,6 +1876,7 @@ _CET_ENDBR .private_extern _aes256gcmsiv_ecb_enc_block .p2align 4 +.alt_entry _aes256gcmsiv_ecb_enc_block _aes256gcmsiv_ecb_enc_block: _CET_ENDBR @@ -1867,7 +1895,7 @@ _CET_ENDBR vaesenc 176(%rdx),%xmm1,%xmm1 vaesenc 192(%rdx),%xmm1,%xmm1 vaesenc 208(%rdx),%xmm1,%xmm1 - vaesenclast 224(%rdx),%xmm1,%xmm1 + vaesenclast 224(%rdx),%xmm1,%xmm1 // %xmm1 == IV vmovdqa %xmm1,(%rsi) ret @@ -1876,6 +1904,7 @@ _CET_ENDBR .private_extern _aes256gcmsiv_enc_msg_x4 .p2align 4 +.alt_entry _aes256gcmsiv_enc_msg_x4 _aes256gcmsiv_enc_msg_x4: _CET_ENDBR @@ -1885,7 +1914,7 @@ _CET_ENDBR L$256_enc_msg_x4_start: movq %r8,%r10 - shrq $4,%r8 + shrq $4,%r8 // LEN = num of blocks shlq $60,%r10 jz L$256_enc_msg_x4_start2 addq $1,%r8 @@ -1895,15 +1924,15 @@ L$256_enc_msg_x4_start2: shlq $62,%r10 shrq $62,%r10 - +// make IV from TAG vmovdqa (%rdx),%xmm15 - vpor OR_MASK(%rip),%xmm15,%xmm15 + vpor OR_MASK(%rip),%xmm15,%xmm15 // IV = [1]TAG[126...32][00..00] - vmovdqa four(%rip),%xmm4 - vmovdqa %xmm15,%xmm0 - vpaddd one(%rip),%xmm15,%xmm1 - vpaddd two(%rip),%xmm15,%xmm2 - vpaddd three(%rip),%xmm15,%xmm3 + vmovdqa four(%rip),%xmm4 // Register to increment counters + vmovdqa %xmm15,%xmm0 // CTR1 = TAG[1][127...32][00..00] + vpaddd one(%rip),%xmm15,%xmm1 // CTR2 = TAG[1][127...32][00..01] + vpaddd two(%rip),%xmm15,%xmm2 // CTR3 = TAG[1][127...32][00..02] + vpaddd three(%rip),%xmm15,%xmm3 // CTR4 = TAG[1][127...32][00..03] shrq $2,%r8 je L$256_enc_msg_x4_check_remainder @@ -2015,7 +2044,7 @@ L$256_enc_msg_x4_loop1: vaesenclast %xmm12,%xmm8,%xmm8 - +// XOR with Plaintext vpxor 0(%rdi),%xmm5,%xmm5 vpxor 16(%rdi),%xmm6,%xmm6 vpxor 32(%rdi),%xmm7,%xmm7 @@ -2038,11 +2067,11 @@ L$256_enc_msg_x4_check_remainder: je L$256_enc_msg_x4_out L$256_enc_msg_x4_loop2: - - +// encrypt each block separately +// CTR1 is the highest counter (even if no LOOP done) vmovdqa %xmm0,%xmm5 - vpaddd one(%rip),%xmm0,%xmm0 + vpaddd one(%rip),%xmm0,%xmm0 // inc counter vpxor (%rcx),%xmm5,%xmm5 vaesenc 16(%rcx),%xmm5,%xmm5 vaesenc 32(%rcx),%xmm5,%xmm5 @@ -2059,7 +2088,7 @@ L$256_enc_msg_x4_loop2: vaesenc 208(%rcx),%xmm5,%xmm5 vaesenclast 224(%rcx),%xmm5,%xmm5 - +// XOR with Plaintext vpxor (%rdi),%xmm5,%xmm5 vmovdqu %xmm5,(%rsi) @@ -2078,6 +2107,7 @@ L$256_enc_msg_x4_out: .private_extern _aes256gcmsiv_enc_msg_x8 .p2align 4 +.alt_entry _aes256gcmsiv_enc_msg_x8 _aes256gcmsiv_enc_msg_x8: _CET_ENDBR @@ -2086,13 +2116,13 @@ _CET_ENDBR ret L$256_enc_msg_x8_start: - +// Place in stack movq %rsp,%r11 subq $16,%r11 andq $-64,%r11 movq %r8,%r10 - shrq $4,%r8 + shrq $4,%r8 // LEN = num of blocks shlq $60,%r10 jz L$256_enc_msg_x8_start2 addq $1,%r8 @@ -2102,20 +2132,20 @@ L$256_enc_msg_x8_start2: shlq $61,%r10 shrq $61,%r10 - +// Make IV from TAG vmovdqa (%rdx),%xmm1 - vpor OR_MASK(%rip),%xmm1,%xmm1 - + vpor OR_MASK(%rip),%xmm1,%xmm1 // TMP1= IV = [1]TAG[126...32][00..00] +// store counter8 on the stack vpaddd seven(%rip),%xmm1,%xmm0 - vmovdqa %xmm0,(%r11) - vpaddd one(%rip),%xmm1,%xmm9 - vpaddd two(%rip),%xmm1,%xmm10 - vpaddd three(%rip),%xmm1,%xmm11 - vpaddd four(%rip),%xmm1,%xmm12 - vpaddd five(%rip),%xmm1,%xmm13 - vpaddd six(%rip),%xmm1,%xmm14 - vmovdqa %xmm1,%xmm0 + vmovdqa %xmm0,(%r11) // CTR8 = TAG[127...32][00..07] + vpaddd one(%rip),%xmm1,%xmm9 // CTR2 = TAG[127...32][00..01] + vpaddd two(%rip),%xmm1,%xmm10 // CTR3 = TAG[127...32][00..02] + vpaddd three(%rip),%xmm1,%xmm11 // CTR4 = TAG[127...32][00..03] + vpaddd four(%rip),%xmm1,%xmm12 // CTR5 = TAG[127...32][00..04] + vpaddd five(%rip),%xmm1,%xmm13 // CTR6 = TAG[127...32][00..05] + vpaddd six(%rip),%xmm1,%xmm14 // CTR7 = TAG[127...32][00..06] + vmovdqa %xmm1,%xmm0 // CTR1 = TAG[127...32][00..00] shrq $3,%r8 jz L$256_enc_msg_x8_check_remainder @@ -2134,7 +2164,7 @@ L$256_enc_msg_x8_loop1: vmovdqa %xmm12,%xmm5 vmovdqa %xmm13,%xmm6 vmovdqa %xmm14,%xmm7 - +// move from stack vmovdqa (%r11),%xmm8 vpxor (%rcx),%xmm1,%xmm1 @@ -2156,7 +2186,7 @@ L$256_enc_msg_x8_loop1: vaesenc %xmm15,%xmm7,%xmm7 vaesenc %xmm15,%xmm8,%xmm8 - vmovdqa (%r11),%xmm14 + vmovdqa (%r11),%xmm14 // deal with CTR8 vpaddd eight(%rip),%xmm14,%xmm14 vmovdqa %xmm14,(%r11) vmovdqu 32(%rcx),%xmm15 @@ -2297,7 +2327,7 @@ L$256_enc_msg_x8_loop1: vaesenclast %xmm15,%xmm8,%xmm8 - +// XOR with Plaintext vpxor 0(%rdi),%xmm1,%xmm1 vpxor 16(%rdi),%xmm2,%xmm2 vpxor 32(%rdi),%xmm3,%xmm3 @@ -2328,8 +2358,8 @@ L$256_enc_msg_x8_check_remainder: je L$256_enc_msg_x8_out L$256_enc_msg_x8_loop2: - - +// encrypt each block separately +// CTR1 is the highest counter (even if no LOOP done) vmovdqa %xmm0,%xmm1 vpaddd one(%rip),%xmm0,%xmm0 @@ -2349,7 +2379,7 @@ L$256_enc_msg_x8_loop2: vaesenc 208(%rcx),%xmm1,%xmm1 vaesenclast 224(%rcx),%xmm1,%xmm1 - +// XOR with Plaintext vpxor (%rdi),%xmm1,%xmm1 vmovdqu %xmm1,(%rsi) @@ -2368,6 +2398,7 @@ L$256_enc_msg_x8_out: .private_extern _aes256gcmsiv_dec .p2align 4 +.alt_entry _aes256gcmsiv_dec _aes256gcmsiv_dec: _CET_ENDBR @@ -2378,10 +2409,10 @@ _CET_ENDBR L$256_dec_start: vzeroupper vmovdqa (%rdx),%xmm0 - - +// The claimed tag is provided after the current calculated tag value. +// CTRBLKs is made from it. vmovdqu 16(%rdx),%xmm15 - vpor OR_MASK(%rip),%xmm15,%xmm15 + vpor OR_MASK(%rip),%xmm15,%xmm15 // CTR = [1]TAG[126...32][00..00] movq %rdx,%rax leaq 32(%rax),%rax @@ -2389,11 +2420,11 @@ L$256_dec_start: andq $~15,%r9 - +// If less then 6 blocks, make singles cmpq $96,%r9 jb L$256_dec_loop2 - +// Decrypt the first six blocks subq $96,%r9 vmovdqa %xmm15,%xmm7 vpaddd one(%rip),%xmm7,%xmm8 @@ -2522,7 +2553,7 @@ L$256_dec_start: vaesenclast %xmm4,%xmm11,%xmm11 vaesenclast %xmm4,%xmm12,%xmm12 - +// XOR with CT vpxor 0(%rdi),%xmm7,%xmm7 vpxor 16(%rdi),%xmm8,%xmm8 vpxor 32(%rdi),%xmm9,%xmm9 @@ -2541,7 +2572,7 @@ L$256_dec_start: addq $96,%rsi jmp L$256_dec_loop1 - +// Decrypt 6 blocks each time while hashing previous 6 blocks .p2align 6 L$256_dec_loop1: cmpq $96,%r9 @@ -2885,9 +2916,9 @@ L$256_dec_finish_96: vpxor %xmm5,%xmm0,%xmm0 L$256_dec_loop2: +// Here we encrypt any remaining whole block - - +// if there are no whole blocks cmpq $16,%r9 jb L$256_dec_out subq $16,%r9 @@ -2930,14 +2961,15 @@ L$256_dec_out: .private_extern _aes256gcmsiv_kdf .p2align 4 +.alt_entry _aes256gcmsiv_kdf _aes256gcmsiv_kdf: _CET_ENDBR +// parameter 1: %rdi Pointer to NONCE +// parameter 2: %rsi Pointer to CT +// parameter 4: %rdx Pointer to keys - - - - vmovdqa (%rdx),%xmm1 + vmovdqa (%rdx),%xmm1 // xmm1 = first 16 bytes of random key vmovdqa 0(%rdi),%xmm4 vmovdqa and_mask(%rip),%xmm11 vmovdqa one(%rip),%xmm8 diff --git a/third_party/boringssl/gen/crypto/aes128gcmsiv-x86_64-linux.S b/third_party/boringssl/gen/crypto/aes128gcmsiv-x86_64-linux.S index a8de4a9ab..a0e304016 100644 --- a/third_party/boringssl/gen/crypto/aes128gcmsiv-x86_64-linux.S +++ b/third_party/boringssl/gen/crypto/aes128gcmsiv-x86_64-linux.S @@ -74,21 +74,21 @@ aesgcmsiv_htable_init: _CET_ENDBR vmovdqa (%rsi),%xmm0 vmovdqa %xmm0,%xmm1 - vmovdqa %xmm0,(%rdi) + vmovdqa %xmm0,(%rdi) // H call GFMUL - vmovdqa %xmm0,16(%rdi) + vmovdqa %xmm0,16(%rdi) // H^2 call GFMUL - vmovdqa %xmm0,32(%rdi) + vmovdqa %xmm0,32(%rdi) // H^3 call GFMUL - vmovdqa %xmm0,48(%rdi) + vmovdqa %xmm0,48(%rdi) // H^4 call GFMUL - vmovdqa %xmm0,64(%rdi) + vmovdqa %xmm0,64(%rdi) // H^5 call GFMUL - vmovdqa %xmm0,80(%rdi) + vmovdqa %xmm0,80(%rdi) // H^6 call GFMUL - vmovdqa %xmm0,96(%rdi) + vmovdqa %xmm0,96(%rdi) // H^7 call GFMUL - vmovdqa %xmm0,112(%rdi) + vmovdqa %xmm0,112(%rdi) // H^8 ret .cfi_endproc .size aesgcmsiv_htable_init, .-aesgcmsiv_htable_init @@ -101,17 +101,17 @@ aesgcmsiv_htable6_init: _CET_ENDBR vmovdqa (%rsi),%xmm0 vmovdqa %xmm0,%xmm1 - vmovdqa %xmm0,(%rdi) + vmovdqa %xmm0,(%rdi) // H call GFMUL - vmovdqa %xmm0,16(%rdi) + vmovdqa %xmm0,16(%rdi) // H^2 call GFMUL - vmovdqa %xmm0,32(%rdi) + vmovdqa %xmm0,32(%rdi) // H^3 call GFMUL - vmovdqa %xmm0,48(%rdi) + vmovdqa %xmm0,48(%rdi) // H^4 call GFMUL - vmovdqa %xmm0,64(%rdi) + vmovdqa %xmm0,64(%rdi) // H^5 call GFMUL - vmovdqa %xmm0,80(%rdi) + vmovdqa %xmm0,80(%rdi) // H^6 ret .cfi_endproc .size aesgcmsiv_htable6_init, .-aesgcmsiv_htable6_init @@ -129,8 +129,8 @@ _CET_ENDBR .Lhtable_polyval_start: vzeroall - - +// We hash 8 blocks each iteration. If the total number of blocks is not a +// multiple of 8, we first hash the leading n%8 blocks. movq %rdx,%r11 andq $127,%r11 @@ -142,7 +142,7 @@ _CET_ENDBR subq $16,%r11 - +// hash first prefix block vmovdqu (%rsi),%xmm0 vpxor %xmm1,%xmm0,%xmm0 @@ -157,12 +157,12 @@ _CET_ENDBR jnz .Lhtable_polyval_prefix_loop jmp .Lhtable_polyval_prefix_complete - +// hash remaining prefix blocks (up to 7 total prefix blocks) .align 64 .Lhtable_polyval_prefix_loop: subq $16,%r11 - vmovdqu (%rsi),%xmm0 + vmovdqu (%rsi),%xmm0 // next data block vpclmulqdq $0x00,(%rdi,%r11,1),%xmm0,%xmm6 vpxor %xmm6,%xmm3,%xmm3 @@ -189,10 +189,10 @@ _CET_ENDBR jmp .Lhtable_polyval_main_loop .Lhtable_polyval_no_prefix: - - - - +// At this point we know the number of blocks is a multiple of 8. However, +// the reduction in the main loop includes a multiplication by x^(-128). In +// order to counter this, the existing tag needs to be multiplied by x^128. +// In practice, this just means that it is loaded into %xmm9, not %xmm1. vpxor %xmm1,%xmm1,%xmm1 vmovdqa (%rcx),%xmm9 @@ -201,7 +201,7 @@ _CET_ENDBR subq $0x80,%rdx jb .Lhtable_polyval_out - vmovdqu 112(%rsi),%xmm0 + vmovdqu 112(%rsi),%xmm0 // Ii vpclmulqdq $0x01,(%rdi),%xmm0,%xmm5 vpclmulqdq $0x00,(%rdi),%xmm0,%xmm3 @@ -209,7 +209,7 @@ _CET_ENDBR vpclmulqdq $0x10,(%rdi),%xmm0,%xmm6 vpxor %xmm6,%xmm5,%xmm5 - +// ######################################################## vmovdqu 96(%rsi),%xmm0 vpclmulqdq $0x01,16(%rdi),%xmm0,%xmm6 vpxor %xmm6,%xmm5,%xmm5 @@ -221,10 +221,10 @@ _CET_ENDBR vpxor %xmm6,%xmm5,%xmm5 - +// ######################################################## vmovdqu 80(%rsi),%xmm0 - vpclmulqdq $0x10,poly(%rip),%xmm1,%xmm7 + vpclmulqdq $0x10,poly(%rip),%xmm1,%xmm7 // reduction stage 1a vpalignr $8,%xmm1,%xmm1,%xmm1 vpclmulqdq $0x01,32(%rdi),%xmm0,%xmm6 @@ -237,8 +237,8 @@ _CET_ENDBR vpxor %xmm6,%xmm5,%xmm5 - vpxor %xmm7,%xmm1,%xmm1 - + vpxor %xmm7,%xmm1,%xmm1 // reduction stage 1b +// ######################################################## vmovdqu 64(%rsi),%xmm0 vpclmulqdq $0x01,48(%rdi),%xmm0,%xmm6 @@ -250,10 +250,10 @@ _CET_ENDBR vpclmulqdq $0x10,48(%rdi),%xmm0,%xmm6 vpxor %xmm6,%xmm5,%xmm5 - +// ######################################################## vmovdqu 48(%rsi),%xmm0 - vpclmulqdq $0x10,poly(%rip),%xmm1,%xmm7 + vpclmulqdq $0x10,poly(%rip),%xmm1,%xmm7 // reduction stage 2a vpalignr $8,%xmm1,%xmm1,%xmm1 vpclmulqdq $0x01,64(%rdi),%xmm0,%xmm6 @@ -266,8 +266,8 @@ _CET_ENDBR vpxor %xmm6,%xmm5,%xmm5 - vpxor %xmm7,%xmm1,%xmm1 - + vpxor %xmm7,%xmm1,%xmm1 // reduction stage 2b +// ######################################################## vmovdqu 32(%rsi),%xmm0 vpclmulqdq $0x01,80(%rdi),%xmm0,%xmm6 @@ -280,8 +280,8 @@ _CET_ENDBR vpxor %xmm6,%xmm5,%xmm5 - vpxor %xmm9,%xmm1,%xmm1 - + vpxor %xmm9,%xmm1,%xmm1 // reduction finalize +// ######################################################## vmovdqu 16(%rsi),%xmm0 vpclmulqdq $0x01,96(%rdi),%xmm0,%xmm6 @@ -293,7 +293,7 @@ _CET_ENDBR vpclmulqdq $0x10,96(%rdi),%xmm0,%xmm6 vpxor %xmm6,%xmm5,%xmm5 - +// ######################################################## vmovdqu 0(%rsi),%xmm0 vpxor %xmm1,%xmm0,%xmm0 @@ -306,7 +306,7 @@ _CET_ENDBR vpclmulqdq $0x10,112(%rdi),%xmm0,%xmm6 vpxor %xmm6,%xmm5,%xmm5 - +// ######################################################## vpsrldq $8,%xmm5,%xmm6 vpslldq $8,%xmm5,%xmm5 @@ -316,7 +316,7 @@ _CET_ENDBR leaq 128(%rsi),%rsi jmp .Lhtable_polyval_main_loop - +// ######################################################## .Lhtable_polyval_out: vpclmulqdq $0x10,poly(%rip),%xmm1,%xmm6 @@ -345,24 +345,24 @@ _CET_ENDBR ret .Lpolyval_horner_start: - - +// We will start with L GFMULS for POLYVAL(BIG_BUFFER) +// RES = GFMUL(RES, H) xorq %r10,%r10 - shlq $4,%rcx + shlq $4,%rcx // L contains number of bytes to process vmovdqa (%rsi),%xmm1 vmovdqa (%rdi),%xmm0 .Lpolyval_horner_loop: - vpxor (%rdx,%r10,1),%xmm0,%xmm0 - call GFMUL + vpxor (%rdx,%r10,1),%xmm0,%xmm0 // RES = RES + Xi + call GFMUL // RES = RES * H addq $16,%r10 cmpq %r10,%rcx jne .Lpolyval_horner_loop - +// calculation of T is complete. RES=T vmovdqa %xmm0,(%rdi) ret .cfi_endproc @@ -374,8 +374,8 @@ _CET_ENDBR aes128gcmsiv_aes_ks: .cfi_startproc _CET_ENDBR - vmovdqu (%rdi),%xmm1 - vmovdqa %xmm1,(%rsi) + vmovdqu (%rdi),%xmm1 // xmm1 = user key + vmovdqa %xmm1,(%rsi) // rsi points to output vmovdqa con1(%rip),%xmm0 vmovdqa mask(%rip),%xmm15 @@ -383,9 +383,9 @@ _CET_ENDBR movq $8,%rax .Lks128_loop: - addq $16,%rsi + addq $16,%rsi // rsi points for next key subq $1,%rax - vpshufb %xmm15,%xmm1,%xmm2 + vpshufb %xmm15,%xmm1,%xmm2 // xmm2 = shuffled user key vaesenclast %xmm0,%xmm2,%xmm2 vpslld $1,%xmm0,%xmm0 vpslldq $4,%xmm1,%xmm3 @@ -479,19 +479,19 @@ _CET_ENDBR aes128gcmsiv_aes_ks_enc_x1: .cfi_startproc _CET_ENDBR - vmovdqa (%rcx),%xmm1 + vmovdqa (%rcx),%xmm1 // xmm1 = first 16 bytes of random key vmovdqa 0(%rdi),%xmm4 - vmovdqa %xmm1,(%rdx) + vmovdqa %xmm1,(%rdx) // KEY[0] = first 16 bytes of random key vpxor %xmm1,%xmm4,%xmm4 - vmovdqa con1(%rip),%xmm0 - vmovdqa mask(%rip),%xmm15 + vmovdqa con1(%rip),%xmm0 // xmm0 = 1,1,1,1 + vmovdqa mask(%rip),%xmm15 // xmm15 = mask - vpshufb %xmm15,%xmm1,%xmm2 + vpshufb %xmm15,%xmm1,%xmm2 // vaesenclast %xmm0,%xmm2,%xmm2 vpslld $1,%xmm0,%xmm0 - vpsllq $32,%xmm1,%xmm3 + vpsllq $32,%xmm1,%xmm3 // vpxor %xmm3,%xmm1,%xmm1 vpshufb con3(%rip),%xmm1,%xmm3 vpxor %xmm3,%xmm1,%xmm1 @@ -500,10 +500,10 @@ _CET_ENDBR vaesenc %xmm1,%xmm4,%xmm4 vmovdqa %xmm1,16(%rdx) - vpshufb %xmm15,%xmm1,%xmm2 + vpshufb %xmm15,%xmm1,%xmm2 // vaesenclast %xmm0,%xmm2,%xmm2 vpslld $1,%xmm0,%xmm0 - vpsllq $32,%xmm1,%xmm3 + vpsllq $32,%xmm1,%xmm3 // vpxor %xmm3,%xmm1,%xmm1 vpshufb con3(%rip),%xmm1,%xmm3 vpxor %xmm3,%xmm1,%xmm1 @@ -512,10 +512,10 @@ _CET_ENDBR vaesenc %xmm1,%xmm4,%xmm4 vmovdqa %xmm1,32(%rdx) - vpshufb %xmm15,%xmm1,%xmm2 + vpshufb %xmm15,%xmm1,%xmm2 // vaesenclast %xmm0,%xmm2,%xmm2 vpslld $1,%xmm0,%xmm0 - vpsllq $32,%xmm1,%xmm3 + vpsllq $32,%xmm1,%xmm3 // vpxor %xmm3,%xmm1,%xmm1 vpshufb con3(%rip),%xmm1,%xmm3 vpxor %xmm3,%xmm1,%xmm1 @@ -524,10 +524,10 @@ _CET_ENDBR vaesenc %xmm1,%xmm4,%xmm4 vmovdqa %xmm1,48(%rdx) - vpshufb %xmm15,%xmm1,%xmm2 + vpshufb %xmm15,%xmm1,%xmm2 // vaesenclast %xmm0,%xmm2,%xmm2 vpslld $1,%xmm0,%xmm0 - vpsllq $32,%xmm1,%xmm3 + vpsllq $32,%xmm1,%xmm3 // vpxor %xmm3,%xmm1,%xmm1 vpshufb con3(%rip),%xmm1,%xmm3 vpxor %xmm3,%xmm1,%xmm1 @@ -536,10 +536,10 @@ _CET_ENDBR vaesenc %xmm1,%xmm4,%xmm4 vmovdqa %xmm1,64(%rdx) - vpshufb %xmm15,%xmm1,%xmm2 + vpshufb %xmm15,%xmm1,%xmm2 // vaesenclast %xmm0,%xmm2,%xmm2 vpslld $1,%xmm0,%xmm0 - vpsllq $32,%xmm1,%xmm3 + vpsllq $32,%xmm1,%xmm3 // vpxor %xmm3,%xmm1,%xmm1 vpshufb con3(%rip),%xmm1,%xmm3 vpxor %xmm3,%xmm1,%xmm1 @@ -548,10 +548,10 @@ _CET_ENDBR vaesenc %xmm1,%xmm4,%xmm4 vmovdqa %xmm1,80(%rdx) - vpshufb %xmm15,%xmm1,%xmm2 + vpshufb %xmm15,%xmm1,%xmm2 // vaesenclast %xmm0,%xmm2,%xmm2 vpslld $1,%xmm0,%xmm0 - vpsllq $32,%xmm1,%xmm3 + vpsllq $32,%xmm1,%xmm3 // vpxor %xmm3,%xmm1,%xmm1 vpshufb con3(%rip),%xmm1,%xmm3 vpxor %xmm3,%xmm1,%xmm1 @@ -560,10 +560,10 @@ _CET_ENDBR vaesenc %xmm1,%xmm4,%xmm4 vmovdqa %xmm1,96(%rdx) - vpshufb %xmm15,%xmm1,%xmm2 + vpshufb %xmm15,%xmm1,%xmm2 // vaesenclast %xmm0,%xmm2,%xmm2 vpslld $1,%xmm0,%xmm0 - vpsllq $32,%xmm1,%xmm3 + vpsllq $32,%xmm1,%xmm3 // vpxor %xmm3,%xmm1,%xmm1 vpshufb con3(%rip),%xmm1,%xmm3 vpxor %xmm3,%xmm1,%xmm1 @@ -572,10 +572,10 @@ _CET_ENDBR vaesenc %xmm1,%xmm4,%xmm4 vmovdqa %xmm1,112(%rdx) - vpshufb %xmm15,%xmm1,%xmm2 + vpshufb %xmm15,%xmm1,%xmm2 // vaesenclast %xmm0,%xmm2,%xmm2 vpslld $1,%xmm0,%xmm0 - vpsllq $32,%xmm1,%xmm3 + vpsllq $32,%xmm1,%xmm3 // vpxor %xmm3,%xmm1,%xmm1 vpshufb con3(%rip),%xmm1,%xmm3 vpxor %xmm3,%xmm1,%xmm1 @@ -587,10 +587,10 @@ _CET_ENDBR vmovdqa con2(%rip),%xmm0 - vpshufb %xmm15,%xmm1,%xmm2 + vpshufb %xmm15,%xmm1,%xmm2 // vaesenclast %xmm0,%xmm2,%xmm2 vpslld $1,%xmm0,%xmm0 - vpsllq $32,%xmm1,%xmm3 + vpsllq $32,%xmm1,%xmm3 // vpxor %xmm3,%xmm1,%xmm1 vpshufb con3(%rip),%xmm1,%xmm3 vpxor %xmm3,%xmm1,%xmm1 @@ -599,9 +599,9 @@ _CET_ENDBR vaesenc %xmm1,%xmm4,%xmm4 vmovdqa %xmm1,144(%rdx) - vpshufb %xmm15,%xmm1,%xmm2 + vpshufb %xmm15,%xmm1,%xmm2 // vaesenclast %xmm0,%xmm2,%xmm2 - vpsllq $32,%xmm1,%xmm3 + vpsllq $32,%xmm1,%xmm3 // vpxor %xmm3,%xmm1,%xmm1 vpshufb con3(%rip),%xmm1,%xmm3 vpxor %xmm3,%xmm1,%xmm1 @@ -622,11 +622,11 @@ _CET_ENDBR aes128gcmsiv_kdf: .cfi_startproc _CET_ENDBR +// parameter 1: %rdi Pointer to NONCE +// parameter 2: %rsi Pointer to CT +// parameter 4: %rdx Pointer to keys - - - - vmovdqa (%rdx),%xmm1 + vmovdqa (%rdx),%xmm1 // xmm1 = first 16 bytes of random key vmovdqa 0(%rdi),%xmm9 vmovdqa and_mask(%rip),%xmm12 vmovdqa one(%rip),%xmm13 @@ -728,20 +728,20 @@ _CET_ENDBR .cfi_adjust_cfa_offset 8 .cfi_offset %r13,-24 - shrq $4,%r8 + shrq $4,%r8 // LEN = num of blocks movq %r8,%r10 shlq $62,%r10 shrq $62,%r10 - +// make IV from TAG vmovdqa (%rdx),%xmm15 - vpor OR_MASK(%rip),%xmm15,%xmm15 + vpor OR_MASK(%rip),%xmm15,%xmm15 // IV = [1]TAG[126...32][00..00] - vmovdqu four(%rip),%xmm4 - vmovdqa %xmm15,%xmm0 - vpaddd one(%rip),%xmm15,%xmm1 - vpaddd two(%rip),%xmm15,%xmm2 - vpaddd three(%rip),%xmm15,%xmm3 + vmovdqu four(%rip),%xmm4 // Register to increment counters + vmovdqa %xmm15,%xmm0 // CTR1 = TAG[1][127...32][00..00] + vpaddd one(%rip),%xmm15,%xmm1 // CTR2 = TAG[1][127...32][00..01] + vpaddd two(%rip),%xmm15,%xmm2 // CTR3 = TAG[1][127...32][00..02] + vpaddd three(%rip),%xmm15,%xmm3 // CTR4 = TAG[1][127...32][00..03] shrq $2,%r8 je .L128_enc_msg_x4_check_remainder @@ -829,7 +829,7 @@ _CET_ENDBR vaesenclast %xmm12,%xmm8,%xmm8 - +// XOR with Plaintext vpxor 0(%rdi),%xmm5,%xmm5 vpxor 16(%rdi),%xmm6,%xmm6 vpxor 32(%rdi),%xmm7,%xmm7 @@ -852,10 +852,10 @@ _CET_ENDBR je .L128_enc_msg_x4_out .L128_enc_msg_x4_loop2: - - +// enc each block separately +// CTR1 is the highest counter (even if no LOOP done) vmovdqa %xmm0,%xmm5 - vpaddd one(%rip),%xmm0,%xmm0 + vpaddd one(%rip),%xmm0,%xmm0 // inc counter vpxor (%rcx),%xmm5,%xmm5 vaesenc 16(%rcx),%xmm5,%xmm5 @@ -869,7 +869,7 @@ _CET_ENDBR vaesenc 144(%rcx),%xmm5,%xmm5 vaesenclast 160(%rcx),%xmm5,%xmm5 - +// XOR with plaintext vpxor (%rdi),%xmm5,%xmm5 vmovdqu %xmm5,(%rsi) @@ -913,29 +913,29 @@ _CET_ENDBR movq %rsp,%rbp .cfi_def_cfa_register rbp - +// Place in stack subq $128,%rsp andq $-64,%rsp - shrq $4,%r8 + shrq $4,%r8 // LEN = num of blocks movq %r8,%r10 shlq $61,%r10 shrq $61,%r10 - +// make IV from TAG vmovdqu (%rdx),%xmm1 - vpor OR_MASK(%rip),%xmm1,%xmm1 - + vpor OR_MASK(%rip),%xmm1,%xmm1 // TMP1= IV = [1]TAG[126...32][00..00] +// store counter8 in the stack vpaddd seven(%rip),%xmm1,%xmm0 - vmovdqu %xmm0,(%rsp) - vpaddd one(%rip),%xmm1,%xmm9 - vpaddd two(%rip),%xmm1,%xmm10 - vpaddd three(%rip),%xmm1,%xmm11 - vpaddd four(%rip),%xmm1,%xmm12 - vpaddd five(%rip),%xmm1,%xmm13 - vpaddd six(%rip),%xmm1,%xmm14 - vmovdqa %xmm1,%xmm0 + vmovdqu %xmm0,(%rsp) // CTR8 = TAG[127...32][00..07] + vpaddd one(%rip),%xmm1,%xmm9 // CTR2 = TAG[127...32][00..01] + vpaddd two(%rip),%xmm1,%xmm10 // CTR3 = TAG[127...32][00..02] + vpaddd three(%rip),%xmm1,%xmm11 // CTR4 = TAG[127...32][00..03] + vpaddd four(%rip),%xmm1,%xmm12 // CTR5 = TAG[127...32][00..04] + vpaddd five(%rip),%xmm1,%xmm13 // CTR6 = TAG[127...32][00..05] + vpaddd six(%rip),%xmm1,%xmm14 // CTR7 = TAG[127...32][00..06] + vmovdqa %xmm1,%xmm0 // CTR1 = TAG[127...32][00..00] shrq $3,%r8 je .L128_enc_msg_x8_check_remainder @@ -954,7 +954,7 @@ _CET_ENDBR vmovdqa %xmm12,%xmm5 vmovdqa %xmm13,%xmm6 vmovdqa %xmm14,%xmm7 - +// move from stack vmovdqu (%rsp),%xmm8 vpxor (%rcx),%xmm1,%xmm1 @@ -976,7 +976,7 @@ _CET_ENDBR vaesenc %xmm15,%xmm7,%xmm7 vaesenc %xmm15,%xmm8,%xmm8 - vmovdqu (%rsp),%xmm14 + vmovdqu (%rsp),%xmm14 // deal with CTR8 vpaddd eight(%rip),%xmm14,%xmm14 vmovdqu %xmm14,(%rsp) vmovdqu 32(%rcx),%xmm15 @@ -1077,7 +1077,7 @@ _CET_ENDBR vaesenclast %xmm15,%xmm8,%xmm8 - +// XOR with Plaintext vpxor 0(%rdi),%xmm1,%xmm1 vpxor 16(%rdi),%xmm2,%xmm2 vpxor 32(%rdi),%xmm3,%xmm3 @@ -1108,10 +1108,10 @@ _CET_ENDBR je .L128_enc_msg_x8_out .L128_enc_msg_x8_loop2: - - +// enc each block separately +// CTR1 is the highest counter (even if no LOOP done) vmovdqa %xmm0,%xmm1 - vpaddd one(%rip),%xmm0,%xmm0 + vpaddd one(%rip),%xmm0,%xmm0 // inc counter vpxor (%rcx),%xmm1,%xmm1 vaesenc 16(%rcx),%xmm1,%xmm1 @@ -1125,7 +1125,7 @@ _CET_ENDBR vaesenc 144(%rcx),%xmm1,%xmm1 vaesenclast 160(%rcx),%xmm1,%xmm1 - +// XOR with Plaintext vpxor (%rdi),%xmm1,%xmm1 vmovdqu %xmm1,(%rsi) @@ -1165,10 +1165,10 @@ _CET_ENDBR .L128_dec_start: vzeroupper vmovdqa (%rdx),%xmm0 - - +// The claimed tag is provided after the current calculated tag value. +// CTRBLKs is made from it. vmovdqu 16(%rdx),%xmm15 - vpor OR_MASK(%rip),%xmm15,%xmm15 + vpor OR_MASK(%rip),%xmm15,%xmm15 // CTR = [1]TAG[126...32][00..00] movq %rdx,%rax leaq 32(%rax),%rax @@ -1176,11 +1176,11 @@ _CET_ENDBR andq $~15,%r9 - +// If less then 6 blocks, make singles cmpq $96,%r9 jb .L128_dec_loop2 - +// Decrypt the first six blocks subq $96,%r9 vmovdqa %xmm15,%xmm7 vpaddd one(%rip),%xmm7,%xmm8 @@ -1277,7 +1277,7 @@ _CET_ENDBR vaesenclast %xmm4,%xmm11,%xmm11 vaesenclast %xmm4,%xmm12,%xmm12 - +// XOR with CT vpxor 0(%rdi),%xmm7,%xmm7 vpxor 16(%rdi),%xmm8,%xmm8 vpxor 32(%rdi),%xmm9,%xmm9 @@ -1296,7 +1296,7 @@ _CET_ENDBR addq $96,%rsi jmp .L128_dec_loop1 - +// Decrypt 6 blocks each time while hashing previous 6 blocks .align 64 .L128_dec_loop1: cmpq $96,%r9 @@ -1608,9 +1608,9 @@ _CET_ENDBR vpxor %xmm5,%xmm0,%xmm0 .L128_dec_loop2: +// Here we encrypt any remaining whole block - - +// if there are no whole blocks cmpq $16,%r9 jb .L128_dec_out subq $16,%r9 @@ -1664,7 +1664,7 @@ _CET_ENDBR vaesenc 112(%rdx),%xmm1,%xmm1 vaesenc 128(%rdx),%xmm1,%xmm1 vaesenc 144(%rdx),%xmm1,%xmm1 - vaesenclast 160(%rdx),%xmm1,%xmm1 + vaesenclast 160(%rdx),%xmm1,%xmm1 // STATE_1 == IV vmovdqa %xmm1,(%rsi) @@ -1678,14 +1678,14 @@ _CET_ENDBR aes256gcmsiv_aes_ks_enc_x1: .cfi_startproc _CET_ENDBR - vmovdqa con1(%rip),%xmm0 - vmovdqa mask(%rip),%xmm15 + vmovdqa con1(%rip),%xmm0 // CON_MASK = 1,1,1,1 + vmovdqa mask(%rip),%xmm15 // MASK_256 vmovdqa (%rdi),%xmm8 - vmovdqa (%rcx),%xmm1 + vmovdqa (%rcx),%xmm1 // KEY_1 || KEY_2 [0..7] = user key vmovdqa 16(%rcx),%xmm3 vpxor %xmm1,%xmm8,%xmm8 vaesenc %xmm3,%xmm8,%xmm8 - vmovdqu %xmm1,(%rdx) + vmovdqu %xmm1,(%rdx) // First round key vmovdqu %xmm3,16(%rdx) vpxor %xmm14,%xmm14,%xmm14 @@ -1877,7 +1877,7 @@ _CET_ENDBR vaesenc 176(%rdx),%xmm1,%xmm1 vaesenc 192(%rdx),%xmm1,%xmm1 vaesenc 208(%rdx),%xmm1,%xmm1 - vaesenclast 224(%rdx),%xmm1,%xmm1 + vaesenclast 224(%rdx),%xmm1,%xmm1 // %xmm1 == IV vmovdqa %xmm1,(%rsi) ret .cfi_endproc @@ -1895,7 +1895,7 @@ _CET_ENDBR .L256_enc_msg_x4_start: movq %r8,%r10 - shrq $4,%r8 + shrq $4,%r8 // LEN = num of blocks shlq $60,%r10 jz .L256_enc_msg_x4_start2 addq $1,%r8 @@ -1905,15 +1905,15 @@ _CET_ENDBR shlq $62,%r10 shrq $62,%r10 - +// make IV from TAG vmovdqa (%rdx),%xmm15 - vpor OR_MASK(%rip),%xmm15,%xmm15 + vpor OR_MASK(%rip),%xmm15,%xmm15 // IV = [1]TAG[126...32][00..00] - vmovdqa four(%rip),%xmm4 - vmovdqa %xmm15,%xmm0 - vpaddd one(%rip),%xmm15,%xmm1 - vpaddd two(%rip),%xmm15,%xmm2 - vpaddd three(%rip),%xmm15,%xmm3 + vmovdqa four(%rip),%xmm4 // Register to increment counters + vmovdqa %xmm15,%xmm0 // CTR1 = TAG[1][127...32][00..00] + vpaddd one(%rip),%xmm15,%xmm1 // CTR2 = TAG[1][127...32][00..01] + vpaddd two(%rip),%xmm15,%xmm2 // CTR3 = TAG[1][127...32][00..02] + vpaddd three(%rip),%xmm15,%xmm3 // CTR4 = TAG[1][127...32][00..03] shrq $2,%r8 je .L256_enc_msg_x4_check_remainder @@ -2025,7 +2025,7 @@ _CET_ENDBR vaesenclast %xmm12,%xmm8,%xmm8 - +// XOR with Plaintext vpxor 0(%rdi),%xmm5,%xmm5 vpxor 16(%rdi),%xmm6,%xmm6 vpxor 32(%rdi),%xmm7,%xmm7 @@ -2048,11 +2048,11 @@ _CET_ENDBR je .L256_enc_msg_x4_out .L256_enc_msg_x4_loop2: - - +// encrypt each block separately +// CTR1 is the highest counter (even if no LOOP done) vmovdqa %xmm0,%xmm5 - vpaddd one(%rip),%xmm0,%xmm0 + vpaddd one(%rip),%xmm0,%xmm0 // inc counter vpxor (%rcx),%xmm5,%xmm5 vaesenc 16(%rcx),%xmm5,%xmm5 vaesenc 32(%rcx),%xmm5,%xmm5 @@ -2069,7 +2069,7 @@ _CET_ENDBR vaesenc 208(%rcx),%xmm5,%xmm5 vaesenclast 224(%rcx),%xmm5,%xmm5 - +// XOR with Plaintext vpxor (%rdi),%xmm5,%xmm5 vmovdqu %xmm5,(%rsi) @@ -2096,13 +2096,13 @@ _CET_ENDBR ret .L256_enc_msg_x8_start: - +// Place in stack movq %rsp,%r11 subq $16,%r11 andq $-64,%r11 movq %r8,%r10 - shrq $4,%r8 + shrq $4,%r8 // LEN = num of blocks shlq $60,%r10 jz .L256_enc_msg_x8_start2 addq $1,%r8 @@ -2112,20 +2112,20 @@ _CET_ENDBR shlq $61,%r10 shrq $61,%r10 - +// Make IV from TAG vmovdqa (%rdx),%xmm1 - vpor OR_MASK(%rip),%xmm1,%xmm1 - + vpor OR_MASK(%rip),%xmm1,%xmm1 // TMP1= IV = [1]TAG[126...32][00..00] +// store counter8 on the stack vpaddd seven(%rip),%xmm1,%xmm0 - vmovdqa %xmm0,(%r11) - vpaddd one(%rip),%xmm1,%xmm9 - vpaddd two(%rip),%xmm1,%xmm10 - vpaddd three(%rip),%xmm1,%xmm11 - vpaddd four(%rip),%xmm1,%xmm12 - vpaddd five(%rip),%xmm1,%xmm13 - vpaddd six(%rip),%xmm1,%xmm14 - vmovdqa %xmm1,%xmm0 + vmovdqa %xmm0,(%r11) // CTR8 = TAG[127...32][00..07] + vpaddd one(%rip),%xmm1,%xmm9 // CTR2 = TAG[127...32][00..01] + vpaddd two(%rip),%xmm1,%xmm10 // CTR3 = TAG[127...32][00..02] + vpaddd three(%rip),%xmm1,%xmm11 // CTR4 = TAG[127...32][00..03] + vpaddd four(%rip),%xmm1,%xmm12 // CTR5 = TAG[127...32][00..04] + vpaddd five(%rip),%xmm1,%xmm13 // CTR6 = TAG[127...32][00..05] + vpaddd six(%rip),%xmm1,%xmm14 // CTR7 = TAG[127...32][00..06] + vmovdqa %xmm1,%xmm0 // CTR1 = TAG[127...32][00..00] shrq $3,%r8 jz .L256_enc_msg_x8_check_remainder @@ -2144,7 +2144,7 @@ _CET_ENDBR vmovdqa %xmm12,%xmm5 vmovdqa %xmm13,%xmm6 vmovdqa %xmm14,%xmm7 - +// move from stack vmovdqa (%r11),%xmm8 vpxor (%rcx),%xmm1,%xmm1 @@ -2166,7 +2166,7 @@ _CET_ENDBR vaesenc %xmm15,%xmm7,%xmm7 vaesenc %xmm15,%xmm8,%xmm8 - vmovdqa (%r11),%xmm14 + vmovdqa (%r11),%xmm14 // deal with CTR8 vpaddd eight(%rip),%xmm14,%xmm14 vmovdqa %xmm14,(%r11) vmovdqu 32(%rcx),%xmm15 @@ -2307,7 +2307,7 @@ _CET_ENDBR vaesenclast %xmm15,%xmm8,%xmm8 - +// XOR with Plaintext vpxor 0(%rdi),%xmm1,%xmm1 vpxor 16(%rdi),%xmm2,%xmm2 vpxor 32(%rdi),%xmm3,%xmm3 @@ -2338,8 +2338,8 @@ _CET_ENDBR je .L256_enc_msg_x8_out .L256_enc_msg_x8_loop2: - - +// encrypt each block separately +// CTR1 is the highest counter (even if no LOOP done) vmovdqa %xmm0,%xmm1 vpaddd one(%rip),%xmm0,%xmm0 @@ -2359,7 +2359,7 @@ _CET_ENDBR vaesenc 208(%rcx),%xmm1,%xmm1 vaesenclast 224(%rcx),%xmm1,%xmm1 - +// XOR with Plaintext vpxor (%rdi),%xmm1,%xmm1 vmovdqu %xmm1,(%rsi) @@ -2388,10 +2388,10 @@ _CET_ENDBR .L256_dec_start: vzeroupper vmovdqa (%rdx),%xmm0 - - +// The claimed tag is provided after the current calculated tag value. +// CTRBLKs is made from it. vmovdqu 16(%rdx),%xmm15 - vpor OR_MASK(%rip),%xmm15,%xmm15 + vpor OR_MASK(%rip),%xmm15,%xmm15 // CTR = [1]TAG[126...32][00..00] movq %rdx,%rax leaq 32(%rax),%rax @@ -2399,11 +2399,11 @@ _CET_ENDBR andq $~15,%r9 - +// If less then 6 blocks, make singles cmpq $96,%r9 jb .L256_dec_loop2 - +// Decrypt the first six blocks subq $96,%r9 vmovdqa %xmm15,%xmm7 vpaddd one(%rip),%xmm7,%xmm8 @@ -2532,7 +2532,7 @@ _CET_ENDBR vaesenclast %xmm4,%xmm11,%xmm11 vaesenclast %xmm4,%xmm12,%xmm12 - +// XOR with CT vpxor 0(%rdi),%xmm7,%xmm7 vpxor 16(%rdi),%xmm8,%xmm8 vpxor 32(%rdi),%xmm9,%xmm9 @@ -2551,7 +2551,7 @@ _CET_ENDBR addq $96,%rsi jmp .L256_dec_loop1 - +// Decrypt 6 blocks each time while hashing previous 6 blocks .align 64 .L256_dec_loop1: cmpq $96,%r9 @@ -2895,9 +2895,9 @@ _CET_ENDBR vpxor %xmm5,%xmm0,%xmm0 .L256_dec_loop2: +// Here we encrypt any remaining whole block - - +// if there are no whole blocks cmpq $16,%r9 jb .L256_dec_out subq $16,%r9 @@ -2943,11 +2943,11 @@ _CET_ENDBR aes256gcmsiv_kdf: .cfi_startproc _CET_ENDBR +// parameter 1: %rdi Pointer to NONCE +// parameter 2: %rsi Pointer to CT +// parameter 4: %rdx Pointer to keys - - - - vmovdqa (%rdx),%xmm1 + vmovdqa (%rdx),%xmm1 // xmm1 = first 16 bytes of random key vmovdqa 0(%rdi),%xmm4 vmovdqa and_mask(%rip),%xmm11 vmovdqa one(%rip),%xmm8 diff --git a/third_party/boringssl/gen/crypto/aes128gcmsiv-x86_64-win.asm b/third_party/boringssl/gen/crypto/aes128gcmsiv-x86_64-win.asm index e875c0a6e..224167715 100644 --- a/third_party/boringssl/gen/crypto/aes128gcmsiv-x86_64-win.asm +++ b/third_party/boringssl/gen/crypto/aes128gcmsiv-x86_64-win.asm @@ -89,21 +89,21 @@ $L$SEH_begin_aesgcmsiv_htable_init: _CET_ENDBR vmovdqa xmm0,XMMWORD[rsi] vmovdqa xmm1,xmm0 - vmovdqa XMMWORD[rdi],xmm0 + vmovdqa XMMWORD[rdi],xmm0 ; H call GFMUL - vmovdqa XMMWORD[16+rdi],xmm0 + vmovdqa XMMWORD[16+rdi],xmm0 ; H^2 call GFMUL - vmovdqa XMMWORD[32+rdi],xmm0 + vmovdqa XMMWORD[32+rdi],xmm0 ; H^3 call GFMUL - vmovdqa XMMWORD[48+rdi],xmm0 + vmovdqa XMMWORD[48+rdi],xmm0 ; H^4 call GFMUL - vmovdqa XMMWORD[64+rdi],xmm0 + vmovdqa XMMWORD[64+rdi],xmm0 ; H^5 call GFMUL - vmovdqa XMMWORD[80+rdi],xmm0 + vmovdqa XMMWORD[80+rdi],xmm0 ; H^6 call GFMUL - vmovdqa XMMWORD[96+rdi],xmm0 + vmovdqa XMMWORD[96+rdi],xmm0 ; H^7 call GFMUL - vmovdqa XMMWORD[112+rdi],xmm0 + vmovdqa XMMWORD[112+rdi],xmm0 ; H^8 mov rdi,QWORD[8+rsp] ;WIN64 epilogue mov rsi,QWORD[16+rsp] ret @@ -125,17 +125,17 @@ $L$SEH_begin_aesgcmsiv_htable6_init: _CET_ENDBR vmovdqa xmm0,XMMWORD[rsi] vmovdqa xmm1,xmm0 - vmovdqa XMMWORD[rdi],xmm0 + vmovdqa XMMWORD[rdi],xmm0 ; H call GFMUL - vmovdqa XMMWORD[16+rdi],xmm0 + vmovdqa XMMWORD[16+rdi],xmm0 ; H^2 call GFMUL - vmovdqa XMMWORD[32+rdi],xmm0 + vmovdqa XMMWORD[32+rdi],xmm0 ; H^3 call GFMUL - vmovdqa XMMWORD[48+rdi],xmm0 + vmovdqa XMMWORD[48+rdi],xmm0 ; H^4 call GFMUL - vmovdqa XMMWORD[64+rdi],xmm0 + vmovdqa XMMWORD[64+rdi],xmm0 ; H^5 call GFMUL - vmovdqa XMMWORD[80+rdi],xmm0 + vmovdqa XMMWORD[80+rdi],xmm0 ; H^6 mov rdi,QWORD[8+rsp] ;WIN64 epilogue mov rsi,QWORD[16+rsp] ret @@ -166,8 +166,8 @@ _CET_ENDBR $L$htable_polyval_start: vzeroall - - +; We hash 8 blocks each iteration. If the total number of blocks is not a +; multiple of 8, we first hash the leading n%8 blocks. mov r11,rdx and r11,127 @@ -179,7 +179,7 @@ $L$htable_polyval_start: sub r11,16 - +; hash first prefix block vmovdqu xmm0,XMMWORD[rsi] vpxor xmm0,xmm0,xmm1 @@ -194,12 +194,12 @@ $L$htable_polyval_start: jnz NEAR $L$htable_polyval_prefix_loop jmp NEAR $L$htable_polyval_prefix_complete - +; hash remaining prefix blocks (up to 7 total prefix blocks) ALIGN 64 $L$htable_polyval_prefix_loop: sub r11,16 - vmovdqu xmm0,XMMWORD[rsi] + vmovdqu xmm0,XMMWORD[rsi] ; next data block vpclmulqdq xmm6,xmm0,XMMWORD[r11*1+rdi],0x00 vpxor xmm3,xmm3,xmm6 @@ -226,10 +226,10 @@ $L$htable_polyval_prefix_complete: jmp NEAR $L$htable_polyval_main_loop $L$htable_polyval_no_prefix: - - - - +; At this point we know the number of blocks is a multiple of 8. However, +; the reduction in the main loop includes a multiplication by x^(-128). In +; order to counter this, the existing tag needs to be multiplied by x^128. +; In practice, this just means that it is loaded into %xmm9, not %xmm1. vpxor xmm1,xmm1,xmm1 vmovdqa xmm9,XMMWORD[rcx] @@ -238,7 +238,7 @@ $L$htable_polyval_main_loop: sub rdx,0x80 jb NEAR $L$htable_polyval_out - vmovdqu xmm0,XMMWORD[112+rsi] + vmovdqu xmm0,XMMWORD[112+rsi] ; Ii vpclmulqdq xmm5,xmm0,XMMWORD[rdi],0x01 vpclmulqdq xmm3,xmm0,XMMWORD[rdi],0x00 @@ -246,7 +246,7 @@ $L$htable_polyval_main_loop: vpclmulqdq xmm6,xmm0,XMMWORD[rdi],0x10 vpxor xmm5,xmm5,xmm6 - +; ######################################################## vmovdqu xmm0,XMMWORD[96+rsi] vpclmulqdq xmm6,xmm0,XMMWORD[16+rdi],0x01 vpxor xmm5,xmm5,xmm6 @@ -258,10 +258,10 @@ $L$htable_polyval_main_loop: vpxor xmm5,xmm5,xmm6 - +; ######################################################## vmovdqu xmm0,XMMWORD[80+rsi] - vpclmulqdq xmm7,xmm1,XMMWORD[poly],0x10 + vpclmulqdq xmm7,xmm1,XMMWORD[poly],0x10 ; reduction stage 1a vpalignr xmm1,xmm1,xmm1,8 vpclmulqdq xmm6,xmm0,XMMWORD[32+rdi],0x01 @@ -274,8 +274,8 @@ $L$htable_polyval_main_loop: vpxor xmm5,xmm5,xmm6 - vpxor xmm1,xmm1,xmm7 - + vpxor xmm1,xmm1,xmm7 ; reduction stage 1b +; ######################################################## vmovdqu xmm0,XMMWORD[64+rsi] vpclmulqdq xmm6,xmm0,XMMWORD[48+rdi],0x01 @@ -287,10 +287,10 @@ $L$htable_polyval_main_loop: vpclmulqdq xmm6,xmm0,XMMWORD[48+rdi],0x10 vpxor xmm5,xmm5,xmm6 - +; ######################################################## vmovdqu xmm0,XMMWORD[48+rsi] - vpclmulqdq xmm7,xmm1,XMMWORD[poly],0x10 + vpclmulqdq xmm7,xmm1,XMMWORD[poly],0x10 ; reduction stage 2a vpalignr xmm1,xmm1,xmm1,8 vpclmulqdq xmm6,xmm0,XMMWORD[64+rdi],0x01 @@ -303,8 +303,8 @@ $L$htable_polyval_main_loop: vpxor xmm5,xmm5,xmm6 - vpxor xmm1,xmm1,xmm7 - + vpxor xmm1,xmm1,xmm7 ; reduction stage 2b +; ######################################################## vmovdqu xmm0,XMMWORD[32+rsi] vpclmulqdq xmm6,xmm0,XMMWORD[80+rdi],0x01 @@ -317,8 +317,8 @@ $L$htable_polyval_main_loop: vpxor xmm5,xmm5,xmm6 - vpxor xmm1,xmm1,xmm9 - + vpxor xmm1,xmm1,xmm9 ; reduction finalize +; ######################################################## vmovdqu xmm0,XMMWORD[16+rsi] vpclmulqdq xmm6,xmm0,XMMWORD[96+rdi],0x01 @@ -330,7 +330,7 @@ $L$htable_polyval_main_loop: vpclmulqdq xmm6,xmm0,XMMWORD[96+rdi],0x10 vpxor xmm5,xmm5,xmm6 - +; ######################################################## vmovdqu xmm0,XMMWORD[rsi] vpxor xmm0,xmm0,xmm1 @@ -343,7 +343,7 @@ $L$htable_polyval_main_loop: vpclmulqdq xmm6,xmm0,XMMWORD[112+rdi],0x10 vpxor xmm5,xmm5,xmm6 - +; ######################################################## vpsrldq xmm6,xmm5,8 vpslldq xmm5,xmm5,8 @@ -353,7 +353,7 @@ $L$htable_polyval_main_loop: lea rsi,[128+rsi] jmp NEAR $L$htable_polyval_main_loop - +; ######################################################## $L$htable_polyval_out: vpclmulqdq xmm6,xmm1,XMMWORD[poly],0x10 @@ -395,24 +395,24 @@ _CET_ENDBR ret $L$polyval_horner_start: - - +; We will start with L GFMULS for POLYVAL(BIG_BUFFER) +; RES = GFMUL(RES, H) xor r10,r10 - shl rcx,4 + shl rcx,4 ; L contains number of bytes to process vmovdqa xmm1,XMMWORD[rsi] vmovdqa xmm0,XMMWORD[rdi] $L$polyval_horner_loop: - vpxor xmm0,xmm0,XMMWORD[r10*1+rdx] - call GFMUL + vpxor xmm0,xmm0,XMMWORD[r10*1+rdx] ; RES = RES + Xi + call GFMUL ; RES = RES * H add r10,16 cmp rcx,r10 jne NEAR $L$polyval_horner_loop - +; calculation of T is complete. RES=T vmovdqa XMMWORD[rdi],xmm0 mov rdi,QWORD[8+rsp] ;WIN64 epilogue mov rsi,QWORD[16+rsp] @@ -433,8 +433,8 @@ $L$SEH_begin_aes128gcmsiv_aes_ks: _CET_ENDBR - vmovdqu xmm1,XMMWORD[rdi] - vmovdqa XMMWORD[rsi],xmm1 + vmovdqu xmm1,XMMWORD[rdi] ; xmm1 = user key + vmovdqa XMMWORD[rsi],xmm1 ; rsi points to output vmovdqa xmm0,XMMWORD[con1] vmovdqa xmm15,XMMWORD[mask] @@ -442,9 +442,9 @@ _CET_ENDBR mov rax,8 $L$ks128_loop: - add rsi,16 + add rsi,16 ; rsi points for next key sub rax,1 - vpshufb xmm2,xmm1,xmm15 + vpshufb xmm2,xmm1,xmm15 ; xmm2 = shuffled user key vaesenclast xmm2,xmm2,xmm0 vpslld xmm0,xmm0,1 vpslldq xmm3,xmm1,4 @@ -558,19 +558,19 @@ $L$SEH_begin_aes128gcmsiv_aes_ks_enc_x1: _CET_ENDBR - vmovdqa xmm1,XMMWORD[rcx] + vmovdqa xmm1,XMMWORD[rcx] ; xmm1 = first 16 bytes of random key vmovdqa xmm4,XMMWORD[rdi] - vmovdqa XMMWORD[rdx],xmm1 + vmovdqa XMMWORD[rdx],xmm1 ; KEY[0] = first 16 bytes of random key vpxor xmm4,xmm4,xmm1 - vmovdqa xmm0,XMMWORD[con1] - vmovdqa xmm15,XMMWORD[mask] + vmovdqa xmm0,XMMWORD[con1] ; xmm0 = 1,1,1,1 + vmovdqa xmm15,XMMWORD[mask] ; xmm15 = mask - vpshufb xmm2,xmm1,xmm15 + vpshufb xmm2,xmm1,xmm15 ; !!saving mov instruction to xmm2 vaesenclast xmm2,xmm2,xmm0 vpslld xmm0,xmm0,1 - vpsllq xmm3,xmm1,32 + vpsllq xmm3,xmm1,32 ; !!saving mov instruction to xmm3 vpxor xmm1,xmm1,xmm3 vpshufb xmm3,xmm1,XMMWORD[con3] vpxor xmm1,xmm1,xmm3 @@ -579,10 +579,10 @@ _CET_ENDBR vaesenc xmm4,xmm4,xmm1 vmovdqa XMMWORD[16+rdx],xmm1 - vpshufb xmm2,xmm1,xmm15 + vpshufb xmm2,xmm1,xmm15 ; !!saving mov instruction to xmm2 vaesenclast xmm2,xmm2,xmm0 vpslld xmm0,xmm0,1 - vpsllq xmm3,xmm1,32 + vpsllq xmm3,xmm1,32 ; !!saving mov instruction to xmm3 vpxor xmm1,xmm1,xmm3 vpshufb xmm3,xmm1,XMMWORD[con3] vpxor xmm1,xmm1,xmm3 @@ -591,10 +591,10 @@ _CET_ENDBR vaesenc xmm4,xmm4,xmm1 vmovdqa XMMWORD[32+rdx],xmm1 - vpshufb xmm2,xmm1,xmm15 + vpshufb xmm2,xmm1,xmm15 ; !!saving mov instruction to xmm2 vaesenclast xmm2,xmm2,xmm0 vpslld xmm0,xmm0,1 - vpsllq xmm3,xmm1,32 + vpsllq xmm3,xmm1,32 ; !!saving mov instruction to xmm3 vpxor xmm1,xmm1,xmm3 vpshufb xmm3,xmm1,XMMWORD[con3] vpxor xmm1,xmm1,xmm3 @@ -603,10 +603,10 @@ _CET_ENDBR vaesenc xmm4,xmm4,xmm1 vmovdqa XMMWORD[48+rdx],xmm1 - vpshufb xmm2,xmm1,xmm15 + vpshufb xmm2,xmm1,xmm15 ; !!saving mov instruction to xmm2 vaesenclast xmm2,xmm2,xmm0 vpslld xmm0,xmm0,1 - vpsllq xmm3,xmm1,32 + vpsllq xmm3,xmm1,32 ; !!saving mov instruction to xmm3 vpxor xmm1,xmm1,xmm3 vpshufb xmm3,xmm1,XMMWORD[con3] vpxor xmm1,xmm1,xmm3 @@ -615,10 +615,10 @@ _CET_ENDBR vaesenc xmm4,xmm4,xmm1 vmovdqa XMMWORD[64+rdx],xmm1 - vpshufb xmm2,xmm1,xmm15 + vpshufb xmm2,xmm1,xmm15 ; !!saving mov instruction to xmm2 vaesenclast xmm2,xmm2,xmm0 vpslld xmm0,xmm0,1 - vpsllq xmm3,xmm1,32 + vpsllq xmm3,xmm1,32 ; !!saving mov instruction to xmm3 vpxor xmm1,xmm1,xmm3 vpshufb xmm3,xmm1,XMMWORD[con3] vpxor xmm1,xmm1,xmm3 @@ -627,10 +627,10 @@ _CET_ENDBR vaesenc xmm4,xmm4,xmm1 vmovdqa XMMWORD[80+rdx],xmm1 - vpshufb xmm2,xmm1,xmm15 + vpshufb xmm2,xmm1,xmm15 ; !!saving mov instruction to xmm2 vaesenclast xmm2,xmm2,xmm0 vpslld xmm0,xmm0,1 - vpsllq xmm3,xmm1,32 + vpsllq xmm3,xmm1,32 ; !!saving mov instruction to xmm3 vpxor xmm1,xmm1,xmm3 vpshufb xmm3,xmm1,XMMWORD[con3] vpxor xmm1,xmm1,xmm3 @@ -639,10 +639,10 @@ _CET_ENDBR vaesenc xmm4,xmm4,xmm1 vmovdqa XMMWORD[96+rdx],xmm1 - vpshufb xmm2,xmm1,xmm15 + vpshufb xmm2,xmm1,xmm15 ; !!saving mov instruction to xmm2 vaesenclast xmm2,xmm2,xmm0 vpslld xmm0,xmm0,1 - vpsllq xmm3,xmm1,32 + vpsllq xmm3,xmm1,32 ; !!saving mov instruction to xmm3 vpxor xmm1,xmm1,xmm3 vpshufb xmm3,xmm1,XMMWORD[con3] vpxor xmm1,xmm1,xmm3 @@ -651,10 +651,10 @@ _CET_ENDBR vaesenc xmm4,xmm4,xmm1 vmovdqa XMMWORD[112+rdx],xmm1 - vpshufb xmm2,xmm1,xmm15 + vpshufb xmm2,xmm1,xmm15 ; !!saving mov instruction to xmm2 vaesenclast xmm2,xmm2,xmm0 vpslld xmm0,xmm0,1 - vpsllq xmm3,xmm1,32 + vpsllq xmm3,xmm1,32 ; !!saving mov instruction to xmm3 vpxor xmm1,xmm1,xmm3 vpshufb xmm3,xmm1,XMMWORD[con3] vpxor xmm1,xmm1,xmm3 @@ -666,10 +666,10 @@ _CET_ENDBR vmovdqa xmm0,XMMWORD[con2] - vpshufb xmm2,xmm1,xmm15 + vpshufb xmm2,xmm1,xmm15 ; !!saving mov instruction to xmm2 vaesenclast xmm2,xmm2,xmm0 vpslld xmm0,xmm0,1 - vpsllq xmm3,xmm1,32 + vpsllq xmm3,xmm1,32 ; !!saving mov instruction to xmm3 vpxor xmm1,xmm1,xmm3 vpshufb xmm3,xmm1,XMMWORD[con3] vpxor xmm1,xmm1,xmm3 @@ -678,9 +678,9 @@ _CET_ENDBR vaesenc xmm4,xmm4,xmm1 vmovdqa XMMWORD[144+rdx],xmm1 - vpshufb xmm2,xmm1,xmm15 + vpshufb xmm2,xmm1,xmm15 ; !!saving mov instruction to xmm2 vaesenclast xmm2,xmm2,xmm0 - vpsllq xmm3,xmm1,32 + vpsllq xmm3,xmm1,32 ; !!saving mov instruction to xmm3 vpxor xmm1,xmm1,xmm3 vpshufb xmm3,xmm1,XMMWORD[con3] vpxor xmm1,xmm1,xmm3 @@ -711,11 +711,11 @@ $L$SEH_begin_aes128gcmsiv_kdf: _CET_ENDBR +; parameter 1: %rdi Pointer to NONCE +; parameter 2: %rsi Pointer to CT +; parameter 4: %rdx Pointer to keys - - - - vmovdqa xmm1,XMMWORD[rdx] + vmovdqa xmm1,XMMWORD[rdx] ; xmm1 = first 16 bytes of random key vmovdqa xmm9,XMMWORD[rdi] vmovdqa xmm12,XMMWORD[and_mask] vmovdqa xmm13,XMMWORD[one] @@ -829,20 +829,20 @@ $L$128_enc_msg_x4_start: push r13 - shr r8,4 + shr r8,4 ; LEN = num of blocks mov r10,r8 shl r10,62 shr r10,62 - +; make IV from TAG vmovdqa xmm15,XMMWORD[rdx] - vpor xmm15,xmm15,XMMWORD[OR_MASK] + vpor xmm15,xmm15,XMMWORD[OR_MASK] ; IV = [1]TAG[126...32][00..00] - vmovdqu xmm4,XMMWORD[four] - vmovdqa xmm0,xmm15 - vpaddd xmm1,xmm15,XMMWORD[one] - vpaddd xmm2,xmm15,XMMWORD[two] - vpaddd xmm3,xmm15,XMMWORD[three] + vmovdqu xmm4,XMMWORD[four] ; Register to increment counters + vmovdqa xmm0,xmm15 ; CTR1 = TAG[1][127...32][00..00] + vpaddd xmm1,xmm15,XMMWORD[one] ; CTR2 = TAG[1][127...32][00..01] + vpaddd xmm2,xmm15,XMMWORD[two] ; CTR3 = TAG[1][127...32][00..02] + vpaddd xmm3,xmm15,XMMWORD[three] ; CTR4 = TAG[1][127...32][00..03] shr r8,2 je NEAR $L$128_enc_msg_x4_check_remainder @@ -930,7 +930,7 @@ $L$128_enc_msg_x4_loop1: vaesenclast xmm8,xmm8,xmm12 - +; XOR with Plaintext vpxor xmm5,xmm5,XMMWORD[rdi] vpxor xmm6,xmm6,XMMWORD[16+rdi] vpxor xmm7,xmm7,XMMWORD[32+rdi] @@ -953,10 +953,10 @@ $L$128_enc_msg_x4_check_remainder: je NEAR $L$128_enc_msg_x4_out $L$128_enc_msg_x4_loop2: - - +; enc each block separately +; CTR1 is the highest counter (even if no LOOP done) vmovdqa xmm5,xmm0 - vpaddd xmm0,xmm0,XMMWORD[one] + vpaddd xmm0,xmm0,XMMWORD[one] ; inc counter vpxor xmm5,xmm5,XMMWORD[rcx] vaesenc xmm5,xmm5,XMMWORD[16+rcx] @@ -970,7 +970,7 @@ $L$128_enc_msg_x4_loop2: vaesenc xmm5,xmm5,XMMWORD[144+rcx] vaesenclast xmm5,xmm5,XMMWORD[160+rcx] - +; XOR with plaintext vpxor xmm5,xmm5,XMMWORD[rdi] vmovdqu XMMWORD[rsi],xmm5 @@ -1023,29 +1023,29 @@ $L$128_enc_msg_x8_start: mov rbp,rsp - +; Place in stack sub rsp,128 and rsp,-64 - shr r8,4 + shr r8,4 ; LEN = num of blocks mov r10,r8 shl r10,61 shr r10,61 - +; make IV from TAG vmovdqu xmm1,XMMWORD[rdx] - vpor xmm1,xmm1,XMMWORD[OR_MASK] - + vpor xmm1,xmm1,XMMWORD[OR_MASK] ; TMP1= IV = [1]TAG[126...32][00..00] +; store counter8 in the stack vpaddd xmm0,xmm1,XMMWORD[seven] - vmovdqu XMMWORD[rsp],xmm0 - vpaddd xmm9,xmm1,XMMWORD[one] - vpaddd xmm10,xmm1,XMMWORD[two] - vpaddd xmm11,xmm1,XMMWORD[three] - vpaddd xmm12,xmm1,XMMWORD[four] - vpaddd xmm13,xmm1,XMMWORD[five] - vpaddd xmm14,xmm1,XMMWORD[six] - vmovdqa xmm0,xmm1 + vmovdqu XMMWORD[rsp],xmm0 ; CTR8 = TAG[127...32][00..07] + vpaddd xmm9,xmm1,XMMWORD[one] ; CTR2 = TAG[127...32][00..01] + vpaddd xmm10,xmm1,XMMWORD[two] ; CTR3 = TAG[127...32][00..02] + vpaddd xmm11,xmm1,XMMWORD[three] ; CTR4 = TAG[127...32][00..03] + vpaddd xmm12,xmm1,XMMWORD[four] ; CTR5 = TAG[127...32][00..04] + vpaddd xmm13,xmm1,XMMWORD[five] ; CTR6 = TAG[127...32][00..05] + vpaddd xmm14,xmm1,XMMWORD[six] ; CTR7 = TAG[127...32][00..06] + vmovdqa xmm0,xmm1 ; CTR1 = TAG[127...32][00..00] shr r8,3 je NEAR $L$128_enc_msg_x8_check_remainder @@ -1064,7 +1064,7 @@ $L$128_enc_msg_x8_loop1: vmovdqa xmm5,xmm12 vmovdqa xmm6,xmm13 vmovdqa xmm7,xmm14 - +; move from stack vmovdqu xmm8,XMMWORD[rsp] vpxor xmm1,xmm1,XMMWORD[rcx] @@ -1086,7 +1086,7 @@ $L$128_enc_msg_x8_loop1: vaesenc xmm7,xmm7,xmm15 vaesenc xmm8,xmm8,xmm15 - vmovdqu xmm14,XMMWORD[rsp] + vmovdqu xmm14,XMMWORD[rsp] ; deal with CTR8 vpaddd xmm14,xmm14,XMMWORD[eight] vmovdqu XMMWORD[rsp],xmm14 vmovdqu xmm15,XMMWORD[32+rcx] @@ -1187,7 +1187,7 @@ $L$128_enc_msg_x8_loop1: vaesenclast xmm8,xmm8,xmm15 - +; XOR with Plaintext vpxor xmm1,xmm1,XMMWORD[rdi] vpxor xmm2,xmm2,XMMWORD[16+rdi] vpxor xmm3,xmm3,XMMWORD[32+rdi] @@ -1218,10 +1218,10 @@ $L$128_enc_msg_x8_check_remainder: je NEAR $L$128_enc_msg_x8_out $L$128_enc_msg_x8_loop2: - - +; enc each block separately +; CTR1 is the highest counter (even if no LOOP done) vmovdqa xmm1,xmm0 - vpaddd xmm0,xmm0,XMMWORD[one] + vpaddd xmm0,xmm0,XMMWORD[one] ; inc counter vpxor xmm1,xmm1,XMMWORD[rcx] vaesenc xmm1,xmm1,XMMWORD[16+rcx] @@ -1235,7 +1235,7 @@ $L$128_enc_msg_x8_loop2: vaesenc xmm1,xmm1,XMMWORD[144+rcx] vaesenclast xmm1,xmm1,XMMWORD[160+rcx] - +; XOR with Plaintext vpxor xmm1,xmm1,XMMWORD[rdi] vmovdqu XMMWORD[rsi],xmm1 @@ -1287,10 +1287,10 @@ _CET_ENDBR $L$128_dec_start: vzeroupper vmovdqa xmm0,XMMWORD[rdx] - - +; The claimed tag is provided after the current calculated tag value. +; CTRBLKs is made from it. vmovdqu xmm15,XMMWORD[16+rdx] - vpor xmm15,xmm15,XMMWORD[OR_MASK] + vpor xmm15,xmm15,XMMWORD[OR_MASK] ; CTR = [1]TAG[126...32][00..00] mov rax,rdx lea rax,[32+rax] @@ -1298,11 +1298,11 @@ $L$128_dec_start: and r9,~15 - +; If less then 6 blocks, make singles cmp r9,96 jb NEAR $L$128_dec_loop2 - +; Decrypt the first six blocks sub r9,96 vmovdqa xmm7,xmm15 vpaddd xmm8,xmm7,XMMWORD[one] @@ -1399,7 +1399,7 @@ $L$128_dec_start: vaesenclast xmm11,xmm11,xmm4 vaesenclast xmm12,xmm12,xmm4 - +; XOR with CT vpxor xmm7,xmm7,XMMWORD[rdi] vpxor xmm8,xmm8,XMMWORD[16+rdi] vpxor xmm9,xmm9,XMMWORD[32+rdi] @@ -1418,7 +1418,7 @@ $L$128_dec_start: add rsi,96 jmp NEAR $L$128_dec_loop1 - +; Decrypt 6 blocks each time while hashing previous 6 blocks ALIGN 64 $L$128_dec_loop1: cmp r9,96 @@ -1730,9 +1730,9 @@ $L$128_dec_finish_96: vpxor xmm0,xmm0,xmm5 $L$128_dec_loop2: +; Here we encrypt any remaining whole block - - +; if there are no whole blocks cmp r9,16 jb NEAR $L$128_dec_out sub r9,16 @@ -1796,7 +1796,7 @@ _CET_ENDBR vaesenc xmm1,xmm1,XMMWORD[112+rdx] vaesenc xmm1,xmm1,XMMWORD[128+rdx] vaesenc xmm1,xmm1,XMMWORD[144+rdx] - vaesenclast xmm1,xmm1,XMMWORD[160+rdx] + vaesenclast xmm1,xmm1,XMMWORD[160+rdx] ; STATE_1 == IV vmovdqa XMMWORD[rsi],xmm1 @@ -1821,14 +1821,14 @@ $L$SEH_begin_aes256gcmsiv_aes_ks_enc_x1: _CET_ENDBR - vmovdqa xmm0,XMMWORD[con1] - vmovdqa xmm15,XMMWORD[mask] + vmovdqa xmm0,XMMWORD[con1] ; CON_MASK = 1,1,1,1 + vmovdqa xmm15,XMMWORD[mask] ; MASK_256 vmovdqa xmm8,XMMWORD[rdi] - vmovdqa xmm1,XMMWORD[rcx] + vmovdqa xmm1,XMMWORD[rcx] ; KEY_1 || KEY_2 [0..7] = user key vmovdqa xmm3,XMMWORD[16+rcx] vpxor xmm8,xmm8,xmm1 vaesenc xmm8,xmm8,xmm3 - vmovdqu XMMWORD[rdx],xmm1 + vmovdqu XMMWORD[rdx],xmm1 ; First round key vmovdqu XMMWORD[16+rdx],xmm3 vpxor xmm14,xmm14,xmm14 @@ -2030,7 +2030,7 @@ _CET_ENDBR vaesenc xmm1,xmm1,XMMWORD[176+rdx] vaesenc xmm1,xmm1,XMMWORD[192+rdx] vaesenc xmm1,xmm1,XMMWORD[208+rdx] - vaesenclast xmm1,xmm1,XMMWORD[224+rdx] + vaesenclast xmm1,xmm1,XMMWORD[224+rdx] ; %xmm1 == IV vmovdqa XMMWORD[rsi],xmm1 mov rdi,QWORD[8+rsp] ;WIN64 epilogue mov rsi,QWORD[16+rsp] @@ -2062,7 +2062,7 @@ _CET_ENDBR $L$256_enc_msg_x4_start: mov r10,r8 - shr r8,4 + shr r8,4 ; LEN = num of blocks shl r10,60 jz NEAR $L$256_enc_msg_x4_start2 add r8,1 @@ -2072,15 +2072,15 @@ $L$256_enc_msg_x4_start2: shl r10,62 shr r10,62 - +; make IV from TAG vmovdqa xmm15,XMMWORD[rdx] - vpor xmm15,xmm15,XMMWORD[OR_MASK] + vpor xmm15,xmm15,XMMWORD[OR_MASK] ; IV = [1]TAG[126...32][00..00] - vmovdqa xmm4,XMMWORD[four] - vmovdqa xmm0,xmm15 - vpaddd xmm1,xmm15,XMMWORD[one] - vpaddd xmm2,xmm15,XMMWORD[two] - vpaddd xmm3,xmm15,XMMWORD[three] + vmovdqa xmm4,XMMWORD[four] ; Register to increment counters + vmovdqa xmm0,xmm15 ; CTR1 = TAG[1][127...32][00..00] + vpaddd xmm1,xmm15,XMMWORD[one] ; CTR2 = TAG[1][127...32][00..01] + vpaddd xmm2,xmm15,XMMWORD[two] ; CTR3 = TAG[1][127...32][00..02] + vpaddd xmm3,xmm15,XMMWORD[three] ; CTR4 = TAG[1][127...32][00..03] shr r8,2 je NEAR $L$256_enc_msg_x4_check_remainder @@ -2192,7 +2192,7 @@ $L$256_enc_msg_x4_loop1: vaesenclast xmm8,xmm8,xmm12 - +; XOR with Plaintext vpxor xmm5,xmm5,XMMWORD[rdi] vpxor xmm6,xmm6,XMMWORD[16+rdi] vpxor xmm7,xmm7,XMMWORD[32+rdi] @@ -2215,11 +2215,11 @@ $L$256_enc_msg_x4_check_remainder: je NEAR $L$256_enc_msg_x4_out $L$256_enc_msg_x4_loop2: - - +; encrypt each block separately +; CTR1 is the highest counter (even if no LOOP done) vmovdqa xmm5,xmm0 - vpaddd xmm0,xmm0,XMMWORD[one] + vpaddd xmm0,xmm0,XMMWORD[one] ; inc counter vpxor xmm5,xmm5,XMMWORD[rcx] vaesenc xmm5,xmm5,XMMWORD[16+rcx] vaesenc xmm5,xmm5,XMMWORD[32+rcx] @@ -2236,7 +2236,7 @@ $L$256_enc_msg_x4_loop2: vaesenc xmm5,xmm5,XMMWORD[208+rcx] vaesenclast xmm5,xmm5,XMMWORD[224+rcx] - +; XOR with Plaintext vpxor xmm5,xmm5,XMMWORD[rdi] vmovdqu XMMWORD[rsi],xmm5 @@ -2277,13 +2277,13 @@ _CET_ENDBR ret $L$256_enc_msg_x8_start: - +; Place in stack mov r11,rsp sub r11,16 and r11,-64 mov r10,r8 - shr r8,4 + shr r8,4 ; LEN = num of blocks shl r10,60 jz NEAR $L$256_enc_msg_x8_start2 add r8,1 @@ -2293,20 +2293,20 @@ $L$256_enc_msg_x8_start2: shl r10,61 shr r10,61 - +; Make IV from TAG vmovdqa xmm1,XMMWORD[rdx] - vpor xmm1,xmm1,XMMWORD[OR_MASK] - + vpor xmm1,xmm1,XMMWORD[OR_MASK] ; TMP1= IV = [1]TAG[126...32][00..00] +; store counter8 on the stack vpaddd xmm0,xmm1,XMMWORD[seven] - vmovdqa XMMWORD[r11],xmm0 - vpaddd xmm9,xmm1,XMMWORD[one] - vpaddd xmm10,xmm1,XMMWORD[two] - vpaddd xmm11,xmm1,XMMWORD[three] - vpaddd xmm12,xmm1,XMMWORD[four] - vpaddd xmm13,xmm1,XMMWORD[five] - vpaddd xmm14,xmm1,XMMWORD[six] - vmovdqa xmm0,xmm1 + vmovdqa XMMWORD[r11],xmm0 ; CTR8 = TAG[127...32][00..07] + vpaddd xmm9,xmm1,XMMWORD[one] ; CTR2 = TAG[127...32][00..01] + vpaddd xmm10,xmm1,XMMWORD[two] ; CTR3 = TAG[127...32][00..02] + vpaddd xmm11,xmm1,XMMWORD[three] ; CTR4 = TAG[127...32][00..03] + vpaddd xmm12,xmm1,XMMWORD[four] ; CTR5 = TAG[127...32][00..04] + vpaddd xmm13,xmm1,XMMWORD[five] ; CTR6 = TAG[127...32][00..05] + vpaddd xmm14,xmm1,XMMWORD[six] ; CTR7 = TAG[127...32][00..06] + vmovdqa xmm0,xmm1 ; CTR1 = TAG[127...32][00..00] shr r8,3 jz NEAR $L$256_enc_msg_x8_check_remainder @@ -2325,7 +2325,7 @@ $L$256_enc_msg_x8_loop1: vmovdqa xmm5,xmm12 vmovdqa xmm6,xmm13 vmovdqa xmm7,xmm14 - +; move from stack vmovdqa xmm8,XMMWORD[r11] vpxor xmm1,xmm1,XMMWORD[rcx] @@ -2347,7 +2347,7 @@ $L$256_enc_msg_x8_loop1: vaesenc xmm7,xmm7,xmm15 vaesenc xmm8,xmm8,xmm15 - vmovdqa xmm14,XMMWORD[r11] + vmovdqa xmm14,XMMWORD[r11] ; deal with CTR8 vpaddd xmm14,xmm14,XMMWORD[eight] vmovdqa XMMWORD[r11],xmm14 vmovdqu xmm15,XMMWORD[32+rcx] @@ -2488,7 +2488,7 @@ $L$256_enc_msg_x8_loop1: vaesenclast xmm8,xmm8,xmm15 - +; XOR with Plaintext vpxor xmm1,xmm1,XMMWORD[rdi] vpxor xmm2,xmm2,XMMWORD[16+rdi] vpxor xmm3,xmm3,XMMWORD[32+rdi] @@ -2519,8 +2519,8 @@ $L$256_enc_msg_x8_check_remainder: je NEAR $L$256_enc_msg_x8_out $L$256_enc_msg_x8_loop2: - - +; encrypt each block separately +; CTR1 is the highest counter (even if no LOOP done) vmovdqa xmm1,xmm0 vpaddd xmm0,xmm0,XMMWORD[one] @@ -2540,7 +2540,7 @@ $L$256_enc_msg_x8_loop2: vaesenc xmm1,xmm1,XMMWORD[208+rcx] vaesenclast xmm1,xmm1,XMMWORD[224+rcx] - +; XOR with Plaintext vpxor xmm1,xmm1,XMMWORD[rdi] vmovdqu XMMWORD[rsi],xmm1 @@ -2584,10 +2584,10 @@ _CET_ENDBR $L$256_dec_start: vzeroupper vmovdqa xmm0,XMMWORD[rdx] - - +; The claimed tag is provided after the current calculated tag value. +; CTRBLKs is made from it. vmovdqu xmm15,XMMWORD[16+rdx] - vpor xmm15,xmm15,XMMWORD[OR_MASK] + vpor xmm15,xmm15,XMMWORD[OR_MASK] ; CTR = [1]TAG[126...32][00..00] mov rax,rdx lea rax,[32+rax] @@ -2595,11 +2595,11 @@ $L$256_dec_start: and r9,~15 - +; If less then 6 blocks, make singles cmp r9,96 jb NEAR $L$256_dec_loop2 - +; Decrypt the first six blocks sub r9,96 vmovdqa xmm7,xmm15 vpaddd xmm8,xmm7,XMMWORD[one] @@ -2728,7 +2728,7 @@ $L$256_dec_start: vaesenclast xmm11,xmm11,xmm4 vaesenclast xmm12,xmm12,xmm4 - +; XOR with CT vpxor xmm7,xmm7,XMMWORD[rdi] vpxor xmm8,xmm8,XMMWORD[16+rdi] vpxor xmm9,xmm9,XMMWORD[32+rdi] @@ -2747,7 +2747,7 @@ $L$256_dec_start: add rsi,96 jmp NEAR $L$256_dec_loop1 - +; Decrypt 6 blocks each time while hashing previous 6 blocks ALIGN 64 $L$256_dec_loop1: cmp r9,96 @@ -3091,9 +3091,9 @@ $L$256_dec_finish_96: vpxor xmm0,xmm0,xmm5 $L$256_dec_loop2: +; Here we encrypt any remaining whole block - - +; if there are no whole blocks cmp r9,16 jb NEAR $L$256_dec_out sub r9,16 @@ -3149,11 +3149,11 @@ $L$SEH_begin_aes256gcmsiv_kdf: _CET_ENDBR +; parameter 1: %rdi Pointer to NONCE +; parameter 2: %rsi Pointer to CT +; parameter 4: %rdx Pointer to keys - - - - vmovdqa xmm1,XMMWORD[rdx] + vmovdqa xmm1,XMMWORD[rdx] ; xmm1 = first 16 bytes of random key vmovdqa xmm4,XMMWORD[rdi] vmovdqa xmm11,XMMWORD[and_mask] vmovdqa xmm8,XMMWORD[one] diff --git a/third_party/boringssl/gen/crypto/chacha-armv4-linux.S b/third_party/boringssl/gen/crypto/chacha-armv4-linux.S index 6532e00de..b144aadc2 100644 --- a/third_party/boringssl/gen/crypto/chacha-armv4-linux.S +++ b/third_party/boringssl/gen/crypto/chacha-armv4-linux.S @@ -23,6 +23,7 @@ #endif .align 5 +chacha_constants: .Lsigma: .long 0x61707865,0x3320646e,0x79622d32,0x6b206574 @ endian-neutral .Lone: diff --git a/third_party/boringssl/gen/crypto/chacha-armv8-apple.S b/third_party/boringssl/gen/crypto/chacha-armv8-apple.S index 452bf496c..63d957112 100644 --- a/third_party/boringssl/gen/crypto/chacha-armv8-apple.S +++ b/third_party/boringssl/gen/crypto/chacha-armv8-apple.S @@ -7,6 +7,7 @@ .section __TEXT,__const .align 5 +chacha_constants: Lsigma: .quad 0x3320646e61707865,0x6b20657479622d32 // endian-neutral Lone: @@ -298,6 +299,7 @@ Loop_tail: .private_extern _ChaCha20_ctr32_neon .align 5 +.alt_entry _ChaCha20_ctr32_neon _ChaCha20_ctr32_neon: AARCH64_SIGN_LINK_REGISTER stp x29,x30,[sp,#-96]! @@ -795,6 +797,7 @@ Ldone_neon: .align 5 +.alt_entry ChaCha20_512_neon ChaCha20_512_neon: AARCH64_SIGN_LINK_REGISTER stp x29,x30,[sp,#-96]! diff --git a/third_party/boringssl/gen/crypto/chacha-armv8-linux.S b/third_party/boringssl/gen/crypto/chacha-armv8-linux.S index 6ecff5377..c65b06d37 100644 --- a/third_party/boringssl/gen/crypto/chacha-armv8-linux.S +++ b/third_party/boringssl/gen/crypto/chacha-armv8-linux.S @@ -7,6 +7,7 @@ .section .rodata .align 5 +chacha_constants: .Lsigma: .quad 0x3320646e61707865,0x6b20657479622d32 // endian-neutral .Lone: diff --git a/third_party/boringssl/gen/crypto/chacha-armv8-win.S b/third_party/boringssl/gen/crypto/chacha-armv8-win.S index ea1da2827..62b7774e6 100644 --- a/third_party/boringssl/gen/crypto/chacha-armv8-win.S +++ b/third_party/boringssl/gen/crypto/chacha-armv8-win.S @@ -7,6 +7,7 @@ .section .rodata .align 5 +chacha_constants: Lsigma: .quad 0x3320646e61707865,0x6b20657479622d32 // endian-neutral Lone: diff --git a/third_party/boringssl/gen/crypto/chacha-x86_64-apple.S b/third_party/boringssl/gen/crypto/chacha-x86_64-apple.S index 09481e379..7fdeb82d9 100644 --- a/third_party/boringssl/gen/crypto/chacha-x86_64-apple.S +++ b/third_party/boringssl/gen/crypto/chacha-x86_64-apple.S @@ -7,7 +7,9 @@ .text .section __DATA,__const + .p2align 6 +chacha_constants: L$zero: .long 0,0,0,0 L$one: @@ -60,25 +62,25 @@ _CET_ENDBR L$ctr32_body: - +// movdqa .Lsigma(%rip),%xmm0 movdqu (%rcx),%xmm1 movdqu 16(%rcx),%xmm2 movdqu (%r8),%xmm3 movdqa L$one(%rip),%xmm4 - - movdqa %xmm1,16(%rsp) - movdqa %xmm2,32(%rsp) - movdqa %xmm3,48(%rsp) - movq %rdx,%rbp +// movdqa %xmm0,4*0(%rsp) # key[0] + movdqa %xmm1,16(%rsp) // key[1] + movdqa %xmm2,32(%rsp) // key[2] + movdqa %xmm3,48(%rsp) // key[3] + movq %rdx,%rbp // reassign %rdx jmp L$oop_outer .p2align 5 L$oop_outer: - movl $0x61707865,%eax - movl $0x3320646e,%ebx - movl $0x79622d32,%ecx - movl $0x6b206574,%edx + movl $0x61707865,%eax // 'expa' + movl $0x3320646e,%ebx // 'nd 3' + movl $0x79622d32,%ecx // '2-by' + movl $0x6b206574,%edx // 'te k' movl 16(%rsp),%r8d movl 20(%rsp),%r9d movl 24(%rsp),%r10d @@ -88,13 +90,13 @@ L$oop_outer: movl 56(%rsp),%r14d movl 60(%rsp),%r15d - movq %rbp,64+0(%rsp) + movq %rbp,64+0(%rsp) // save len movl $10,%ebp - movq %rsi,64+8(%rsp) - movq %xmm2,%rsi - movq %rdi,64+16(%rsp) + movq %rsi,64+8(%rsp) // save inp + movq %xmm2,%rsi // "%nox" + movq %rdi,64+16(%rsp) // save out movq %rsi,%rdi - shrq $32,%rdi + shrq $32,%rdi // "%nox" jmp L$oop .p2align 5 @@ -205,18 +207,18 @@ L$oop: roll $7,%r8d decl %ebp jnz L$oop - movl %edi,36(%rsp) + movl %edi,36(%rsp) // modulo-scheduled movl %esi,32(%rsp) - movq 64(%rsp),%rbp + movq 64(%rsp),%rbp // load len movdqa %xmm2,%xmm1 - movq 64+8(%rsp),%rsi - paddd %xmm4,%xmm3 - movq 64+16(%rsp),%rdi - - addl $0x61707865,%eax - addl $0x3320646e,%ebx - addl $0x79622d32,%ecx - addl $0x6b206574,%edx + movq 64+8(%rsp),%rsi // load inp + paddd %xmm4,%xmm3 // increment counter + movq 64+16(%rsp),%rdi // load out + + addl $0x61707865,%eax // 'expa' + addl $0x3320646e,%ebx // 'nd 3' + addl $0x79622d32,%ecx // '2-by' + addl $0x6b206574,%edx // 'te k' addl 16(%rsp),%r8d addl 20(%rsp),%r9d addl 24(%rsp),%r10d @@ -230,7 +232,7 @@ L$oop: cmpq $64,%rbp jb L$tail - xorl 0(%rsi),%eax + xorl 0(%rsi),%eax // xor with input xorl 4(%rsi),%ebx xorl 8(%rsi),%ecx xorl 12(%rsi),%edx @@ -243,13 +245,13 @@ L$oop: xorl 52(%rsi),%r13d xorl 56(%rsi),%r14d xorl 60(%rsi),%r15d - leaq 64(%rsi),%rsi + leaq 64(%rsi),%rsi // inp+=64 pxor %xmm1,%xmm0 movdqa %xmm2,32(%rsp) movd %xmm3,48(%rsp) - movl %eax,0(%rdi) + movl %eax,0(%rdi) // write output movl %ebx,4(%rdi) movl %ecx,8(%rdi) movl %edx,12(%rdi) @@ -262,7 +264,7 @@ L$oop: movl %r13d,52(%rdi) movl %r14d,56(%rdi) movl %r15d,60(%rdi) - leaq 64(%rdi),%rdi + leaq 64(%rdi),%rdi // out+=64 subq $64,%rbp jnz L$oop_outer @@ -319,10 +321,11 @@ L$no_data: .private_extern _ChaCha20_ctr32_ssse3 .p2align 5 +.alt_entry _ChaCha20_ctr32_ssse3 _ChaCha20_ctr32_ssse3: _CET_ENDBR - movq %rsp,%r9 + movq %rsp,%r9 // frame pointer subq $64+8,%rsp movdqa L$sigma(%rip),%xmm0 @@ -336,7 +339,7 @@ _CET_ENDBR movdqa %xmm1,16(%rsp) movdqa %xmm2,32(%rsp) movdqa %xmm3,48(%rsp) - movq $10,%r8 + movq $10,%r8 // reuse %r8 jmp L$oop_ssse3 .p2align 5 @@ -407,19 +410,19 @@ L$oop_ssse3: movdqu 0(%rsi),%xmm4 movdqu 16(%rsi),%xmm5 - pxor %xmm4,%xmm0 + pxor %xmm4,%xmm0 // xor with input movdqu 32(%rsi),%xmm4 pxor %xmm5,%xmm1 movdqu 48(%rsi),%xmm5 - leaq 64(%rsi),%rsi + leaq 64(%rsi),%rsi // inp+=64 pxor %xmm4,%xmm2 pxor %xmm5,%xmm3 - movdqu %xmm0,0(%rdi) + movdqu %xmm0,0(%rdi) // write output movdqu %xmm1,16(%rdi) movdqu %xmm2,32(%rdi) movdqu %xmm3,48(%rdi) - leaq 64(%rdi),%rdi + leaq 64(%rdi),%rdi // out+=64 subq $64,%rdx jnz L$oop_outer_ssse3 @@ -454,23 +457,24 @@ L$ssse3_epilogue: .private_extern _ChaCha20_ctr32_ssse3_4x .p2align 5 +.alt_entry _ChaCha20_ctr32_ssse3_4x _ChaCha20_ctr32_ssse3_4x: _CET_ENDBR - movq %rsp,%r9 + movq %rsp,%r9 // frame pointer subq $0x140+8,%rsp - movdqa L$sigma(%rip),%xmm11 - movdqu (%rcx),%xmm15 - movdqu 16(%rcx),%xmm7 - movdqu (%r8),%xmm3 - leaq 256(%rsp),%rcx + movdqa L$sigma(%rip),%xmm11 // key[0] + movdqu (%rcx),%xmm15 // key[1] + movdqu 16(%rcx),%xmm7 // key[2] + movdqu (%r8),%xmm3 // key[3] + leaq 256(%rsp),%rcx // size optimization leaq L$rot16(%rip),%r10 leaq L$rot24(%rip),%r11 - pshufd $0x00,%xmm11,%xmm8 + pshufd $0x00,%xmm11,%xmm8 // smash key by lanes... pshufd $0x55,%xmm11,%xmm9 - movdqa %xmm8,64(%rsp) + movdqa %xmm8,64(%rsp) // ... and offload pshufd $0xaa,%xmm11,%xmm10 movdqa %xmm9,80(%rsp) pshufd $0xff,%xmm11,%xmm11 @@ -486,18 +490,18 @@ _CET_ENDBR movdqa %xmm14,160-256(%rcx) movdqa %xmm15,176-256(%rcx) - pshufd $0x00,%xmm7,%xmm4 - pshufd $0x55,%xmm7,%xmm5 + pshufd $0x00,%xmm7,%xmm4 // "" + pshufd $0x55,%xmm7,%xmm5 // "" movdqa %xmm4,192-256(%rcx) - pshufd $0xaa,%xmm7,%xmm6 + pshufd $0xaa,%xmm7,%xmm6 // "" movdqa %xmm5,208-256(%rcx) - pshufd $0xff,%xmm7,%xmm7 + pshufd $0xff,%xmm7,%xmm7 // "" movdqa %xmm6,224-256(%rcx) movdqa %xmm7,240-256(%rcx) pshufd $0x00,%xmm3,%xmm0 pshufd $0x55,%xmm3,%xmm1 - paddd L$inc(%rip),%xmm0 + paddd L$inc(%rip),%xmm0 // don't save counters yet pshufd $0xaa,%xmm3,%xmm2 movdqa %xmm1,272-256(%rcx) pshufd $0xff,%xmm3,%xmm3 @@ -508,7 +512,7 @@ _CET_ENDBR .p2align 5 L$oop_outer4x: - movdqa 64(%rsp),%xmm8 + movdqa 64(%rsp),%xmm8 // re-load smashed key movdqa 80(%rsp),%xmm9 movdqa 96(%rsp),%xmm10 movdqa 112(%rsp),%xmm11 @@ -516,22 +520,22 @@ L$oop_outer4x: movdqa 144-256(%rcx),%xmm13 movdqa 160-256(%rcx),%xmm14 movdqa 176-256(%rcx),%xmm15 - movdqa 192-256(%rcx),%xmm4 - movdqa 208-256(%rcx),%xmm5 - movdqa 224-256(%rcx),%xmm6 - movdqa 240-256(%rcx),%xmm7 + movdqa 192-256(%rcx),%xmm4 // "" + movdqa 208-256(%rcx),%xmm5 // "" + movdqa 224-256(%rcx),%xmm6 // "" + movdqa 240-256(%rcx),%xmm7 // "" movdqa 256-256(%rcx),%xmm0 movdqa 272-256(%rcx),%xmm1 movdqa 288-256(%rcx),%xmm2 movdqa 304-256(%rcx),%xmm3 - paddd L$four(%rip),%xmm0 + paddd L$four(%rip),%xmm0 // next SIMD counters L$oop_enter4x: - movdqa %xmm6,32(%rsp) - movdqa %xmm7,48(%rsp) - movdqa (%r10),%xmm7 + movdqa %xmm6,32(%rsp) // SIMD equivalent of "%nox" + movdqa %xmm7,48(%rsp) // SIMD equivalent of "%nox" + movdqa (%r10),%xmm7 // .Lrot16(%rip) movl $10,%eax - movdqa %xmm0,256-256(%rcx) + movdqa %xmm0,256-256(%rcx) // save SIMD counters jmp L$oop4x .p2align 5 @@ -699,32 +703,32 @@ L$oop4x: decl %eax jnz L$oop4x - paddd 64(%rsp),%xmm8 + paddd 64(%rsp),%xmm8 // accumulate key material paddd 80(%rsp),%xmm9 paddd 96(%rsp),%xmm10 paddd 112(%rsp),%xmm11 - movdqa %xmm8,%xmm6 + movdqa %xmm8,%xmm6 // "de-interlace" data punpckldq %xmm9,%xmm8 movdqa %xmm10,%xmm7 punpckldq %xmm11,%xmm10 punpckhdq %xmm9,%xmm6 punpckhdq %xmm11,%xmm7 movdqa %xmm8,%xmm9 - punpcklqdq %xmm10,%xmm8 + punpcklqdq %xmm10,%xmm8 // "a0" movdqa %xmm6,%xmm11 - punpcklqdq %xmm7,%xmm6 - punpckhqdq %xmm10,%xmm9 - punpckhqdq %xmm7,%xmm11 + punpcklqdq %xmm7,%xmm6 // "a2" + punpckhqdq %xmm10,%xmm9 // "a1" + punpckhqdq %xmm7,%xmm11 // "a3" paddd 128-256(%rcx),%xmm12 paddd 144-256(%rcx),%xmm13 paddd 160-256(%rcx),%xmm14 paddd 176-256(%rcx),%xmm15 - movdqa %xmm8,0(%rsp) + movdqa %xmm8,0(%rsp) // offload movdqa %xmm9,16(%rsp) - movdqa 32(%rsp),%xmm8 - movdqa 48(%rsp),%xmm9 + movdqa 32(%rsp),%xmm8 // "xc2" + movdqa 48(%rsp),%xmm9 // "xc3" movdqa %xmm12,%xmm10 punpckldq %xmm13,%xmm12 @@ -733,17 +737,17 @@ L$oop4x: punpckhdq %xmm13,%xmm10 punpckhdq %xmm15,%xmm7 movdqa %xmm12,%xmm13 - punpcklqdq %xmm14,%xmm12 + punpcklqdq %xmm14,%xmm12 // "b0" movdqa %xmm10,%xmm15 - punpcklqdq %xmm7,%xmm10 - punpckhqdq %xmm14,%xmm13 - punpckhqdq %xmm7,%xmm15 + punpcklqdq %xmm7,%xmm10 // "b2" + punpckhqdq %xmm14,%xmm13 // "b1" + punpckhqdq %xmm7,%xmm15 // "b3" paddd 192-256(%rcx),%xmm4 paddd 208-256(%rcx),%xmm5 paddd 224-256(%rcx),%xmm8 paddd 240-256(%rcx),%xmm9 - movdqa %xmm6,32(%rsp) + movdqa %xmm6,32(%rsp) // keep offloading movdqa %xmm11,48(%rsp) movdqa %xmm4,%xmm14 @@ -753,11 +757,11 @@ L$oop4x: punpckhdq %xmm5,%xmm14 punpckhdq %xmm9,%xmm7 movdqa %xmm4,%xmm5 - punpcklqdq %xmm8,%xmm4 + punpcklqdq %xmm8,%xmm4 // "c0" movdqa %xmm14,%xmm9 - punpcklqdq %xmm7,%xmm14 - punpckhqdq %xmm8,%xmm5 - punpckhqdq %xmm7,%xmm9 + punpcklqdq %xmm7,%xmm14 // "c2" + punpckhqdq %xmm8,%xmm5 // "c1" + punpckhqdq %xmm7,%xmm9 // "c3" paddd 256-256(%rcx),%xmm0 paddd 272-256(%rcx),%xmm1 paddd 288-256(%rcx),%xmm2 @@ -770,19 +774,19 @@ L$oop4x: punpckhdq %xmm1,%xmm8 punpckhdq %xmm3,%xmm7 movdqa %xmm0,%xmm1 - punpcklqdq %xmm2,%xmm0 + punpcklqdq %xmm2,%xmm0 // "d0" movdqa %xmm8,%xmm3 - punpcklqdq %xmm7,%xmm8 - punpckhqdq %xmm2,%xmm1 - punpckhqdq %xmm7,%xmm3 + punpcklqdq %xmm7,%xmm8 // "d2" + punpckhqdq %xmm2,%xmm1 // "d1" + punpckhqdq %xmm7,%xmm3 // "d3" cmpq $256,%rdx jb L$tail4x - movdqu 0(%rsi),%xmm6 + movdqu 0(%rsi),%xmm6 // xor with input movdqu 16(%rsi),%xmm11 movdqu 32(%rsi),%xmm2 movdqu 48(%rsi),%xmm7 - pxor 0(%rsp),%xmm6 + pxor 0(%rsp),%xmm6 // is offloaded, remember? pxor %xmm12,%xmm11 pxor %xmm4,%xmm2 pxor %xmm0,%xmm7 @@ -795,7 +799,7 @@ L$oop4x: movdqu 96(%rsi),%xmm2 movdqu %xmm7,48(%rdi) movdqu 112(%rsi),%xmm7 - leaq 128(%rsi),%rsi + leaq 128(%rsi),%rsi // size optimization pxor 16(%rsp),%xmm6 pxor %xmm13,%xmm11 pxor %xmm5,%xmm2 @@ -808,7 +812,7 @@ L$oop4x: movdqu %xmm2,96(%rdi) movdqu 32(%rsi),%xmm2 movdqu %xmm7,112(%rdi) - leaq 128(%rdi),%rdi + leaq 128(%rdi),%rdi // size optimization movdqu 48(%rsi),%xmm7 pxor 32(%rsp),%xmm6 pxor %xmm10,%xmm11 @@ -823,7 +827,7 @@ L$oop4x: movdqu 96(%rsi),%xmm2 movdqu %xmm7,48(%rdi) movdqu 112(%rsi),%xmm7 - leaq 128(%rsi),%rsi + leaq 128(%rsi),%rsi // inp+=64*4 pxor 48(%rsp),%xmm6 pxor %xmm15,%xmm11 pxor %xmm9,%xmm2 @@ -832,7 +836,7 @@ L$oop4x: movdqu %xmm11,80(%rdi) movdqu %xmm2,96(%rdi) movdqu %xmm7,112(%rdi) - leaq 128(%rdi),%rdi + leaq 128(%rdi),%rdi // out+=64*4 subq $256,%rdx jnz L$oop_outer4x @@ -847,9 +851,9 @@ L$tail4x: cmpq $64,%rdx jae L$64_or_more4x - +// movdqa 0x00(%rsp),%xmm6 # is offloaded, remember? xorq %r10,%r10 - +// movdqa %xmm6,0x00(%rsp) movdqa %xmm12,16(%rsp) movdqa %xmm4,32(%rsp) movdqa %xmm0,48(%rsp) @@ -857,11 +861,11 @@ L$tail4x: .p2align 5 L$64_or_more4x: - movdqu 0(%rsi),%xmm6 + movdqu 0(%rsi),%xmm6 // xor with input movdqu 16(%rsi),%xmm11 movdqu 32(%rsi),%xmm2 movdqu 48(%rsi),%xmm7 - pxor 0(%rsp),%xmm6 + pxor 0(%rsp),%xmm6 // is offloaded, remember? pxor %xmm12,%xmm11 pxor %xmm4,%xmm2 pxor %xmm0,%xmm7 @@ -871,24 +875,24 @@ L$64_or_more4x: movdqu %xmm7,48(%rdi) je L$done4x - movdqa 16(%rsp),%xmm6 - leaq 64(%rsi),%rsi + movdqa 16(%rsp),%xmm6 // is offloaded, remember? + leaq 64(%rsi),%rsi // inp+=64*1 xorq %r10,%r10 movdqa %xmm6,0(%rsp) movdqa %xmm13,16(%rsp) - leaq 64(%rdi),%rdi + leaq 64(%rdi),%rdi // out+=64*1 movdqa %xmm5,32(%rsp) - subq $64,%rdx + subq $64,%rdx // len-=64*1 movdqa %xmm1,48(%rsp) jmp L$oop_tail4x .p2align 5 L$128_or_more4x: - movdqu 0(%rsi),%xmm6 + movdqu 0(%rsi),%xmm6 // xor with input movdqu 16(%rsi),%xmm11 movdqu 32(%rsi),%xmm2 movdqu 48(%rsi),%xmm7 - pxor 0(%rsp),%xmm6 + pxor 0(%rsp),%xmm6 // is offloaded, remember? pxor %xmm12,%xmm11 pxor %xmm4,%xmm2 pxor %xmm0,%xmm7 @@ -911,24 +915,24 @@ L$128_or_more4x: movdqu %xmm7,112(%rdi) je L$done4x - movdqa 32(%rsp),%xmm6 - leaq 128(%rsi),%rsi + movdqa 32(%rsp),%xmm6 // is offloaded, remember? + leaq 128(%rsi),%rsi // inp+=64*2 xorq %r10,%r10 movdqa %xmm6,0(%rsp) movdqa %xmm10,16(%rsp) - leaq 128(%rdi),%rdi + leaq 128(%rdi),%rdi // out+=64*2 movdqa %xmm14,32(%rsp) - subq $128,%rdx + subq $128,%rdx // len-=64*2 movdqa %xmm8,48(%rsp) jmp L$oop_tail4x .p2align 5 L$192_or_more4x: - movdqu 0(%rsi),%xmm6 + movdqu 0(%rsi),%xmm6 // xor with input movdqu 16(%rsi),%xmm11 movdqu 32(%rsi),%xmm2 movdqu 48(%rsi),%xmm7 - pxor 0(%rsp),%xmm6 + pxor 0(%rsp),%xmm6 // is offloaded, remember? pxor %xmm12,%xmm11 pxor %xmm4,%xmm2 pxor %xmm0,%xmm7 @@ -941,7 +945,7 @@ L$192_or_more4x: movdqu 96(%rsi),%xmm2 movdqu %xmm7,48(%rdi) movdqu 112(%rsi),%xmm7 - leaq 128(%rsi),%rsi + leaq 128(%rsi),%rsi // size optimization pxor 16(%rsp),%xmm6 pxor %xmm13,%xmm11 pxor %xmm5,%xmm2 @@ -954,7 +958,7 @@ L$192_or_more4x: movdqu %xmm2,96(%rdi) movdqu 32(%rsi),%xmm2 movdqu %xmm7,112(%rdi) - leaq 128(%rdi),%rdi + leaq 128(%rdi),%rdi // size optimization movdqu 48(%rsi),%xmm7 pxor 32(%rsp),%xmm6 pxor %xmm10,%xmm11 @@ -966,14 +970,14 @@ L$192_or_more4x: movdqu %xmm7,48(%rdi) je L$done4x - movdqa 48(%rsp),%xmm6 - leaq 64(%rsi),%rsi + movdqa 48(%rsp),%xmm6 // is offloaded, remember? + leaq 64(%rsi),%rsi // inp+=64*3 xorq %r10,%r10 movdqa %xmm6,0(%rsp) movdqa %xmm15,16(%rsp) - leaq 64(%rdi),%rdi + leaq 64(%rdi),%rdi // out+=64*3 movdqa %xmm9,32(%rsp) - subq $192,%rdx + subq $192,%rdx // len-=64*3 movdqa %xmm3,48(%rsp) L$oop_tail4x: @@ -996,36 +1000,37 @@ L$4x_epilogue: .private_extern _ChaCha20_ctr32_avx2 .p2align 5 +.alt_entry _ChaCha20_ctr32_avx2 _ChaCha20_ctr32_avx2: _CET_ENDBR - movq %rsp,%r9 + movq %rsp,%r9 // frame register subq $0x280+8,%rsp andq $-32,%rsp vzeroupper - - - - - - - - - - vbroadcasti128 L$sigma(%rip),%ymm11 - vbroadcasti128 (%rcx),%ymm3 - vbroadcasti128 16(%rcx),%ymm15 - vbroadcasti128 (%r8),%ymm7 - leaq 256(%rsp),%rcx - leaq 512(%rsp),%rax +// ############### stack layout +// +0x00 SIMD equivalent of %r12d +// ... +// +0x80 constant copy of key[0-2] smashed by lanes +// ... +// +0x200 SIMD counters (with nonce smashed by lanes) +// ... +// +0x280 + + vbroadcasti128 L$sigma(%rip),%ymm11 // key[0] + vbroadcasti128 (%rcx),%ymm3 // key[1] + vbroadcasti128 16(%rcx),%ymm15 // key[2] + vbroadcasti128 (%r8),%ymm7 // key[3] + leaq 256(%rsp),%rcx // size optimization + leaq 512(%rsp),%rax // size optimization leaq L$rot16(%rip),%r10 leaq L$rot24(%rip),%r11 - vpshufd $0x00,%ymm11,%ymm8 + vpshufd $0x00,%ymm11,%ymm8 // smash key by lanes... vpshufd $0x55,%ymm11,%ymm9 - vmovdqa %ymm8,128-256(%rcx) + vmovdqa %ymm8,128-256(%rcx) // ... and offload vpshufd $0xaa,%ymm11,%ymm10 vmovdqa %ymm9,160-256(%rcx) vpshufd $0xff,%ymm11,%ymm11 @@ -1041,18 +1046,18 @@ _CET_ENDBR vmovdqa %ymm2,320-256(%rcx) vmovdqa %ymm3,352-256(%rcx) - vpshufd $0x00,%ymm15,%ymm12 - vpshufd $0x55,%ymm15,%ymm13 + vpshufd $0x00,%ymm15,%ymm12 // "xc0" + vpshufd $0x55,%ymm15,%ymm13 // "xc1" vmovdqa %ymm12,384-512(%rax) - vpshufd $0xaa,%ymm15,%ymm14 + vpshufd $0xaa,%ymm15,%ymm14 // "xc2" vmovdqa %ymm13,416-512(%rax) - vpshufd $0xff,%ymm15,%ymm15 + vpshufd $0xff,%ymm15,%ymm15 // "xc3" vmovdqa %ymm14,448-512(%rax) vmovdqa %ymm15,480-512(%rax) vpshufd $0x00,%ymm7,%ymm4 vpshufd $0x55,%ymm7,%ymm5 - vpaddd L$incy(%rip),%ymm4,%ymm4 + vpaddd L$incy(%rip),%ymm4,%ymm4 // don't save counters yet vpshufd $0xaa,%ymm7,%ymm6 vmovdqa %ymm5,544-512(%rax) vpshufd $0xff,%ymm7,%ymm7 @@ -1063,7 +1068,7 @@ _CET_ENDBR .p2align 5 L$oop_outer8x: - vmovdqa 128-256(%rcx),%ymm8 + vmovdqa 128-256(%rcx),%ymm8 // re-load smashed key vmovdqa 160-256(%rcx),%ymm9 vmovdqa 192-256(%rcx),%ymm10 vmovdqa 224-256(%rcx),%ymm11 @@ -1071,21 +1076,21 @@ L$oop_outer8x: vmovdqa 288-256(%rcx),%ymm1 vmovdqa 320-256(%rcx),%ymm2 vmovdqa 352-256(%rcx),%ymm3 - vmovdqa 384-512(%rax),%ymm12 - vmovdqa 416-512(%rax),%ymm13 - vmovdqa 448-512(%rax),%ymm14 - vmovdqa 480-512(%rax),%ymm15 + vmovdqa 384-512(%rax),%ymm12 // "xc0" + vmovdqa 416-512(%rax),%ymm13 // "xc1" + vmovdqa 448-512(%rax),%ymm14 // "xc2" + vmovdqa 480-512(%rax),%ymm15 // "xc3" vmovdqa 512-512(%rax),%ymm4 vmovdqa 544-512(%rax),%ymm5 vmovdqa 576-512(%rax),%ymm6 vmovdqa 608-512(%rax),%ymm7 - vpaddd L$eight(%rip),%ymm4,%ymm4 + vpaddd L$eight(%rip),%ymm4,%ymm4 // next SIMD counters L$oop_enter8x: - vmovdqa %ymm14,64(%rsp) - vmovdqa %ymm15,96(%rsp) + vmovdqa %ymm14,64(%rsp) // SIMD equivalent of "%nox" + vmovdqa %ymm15,96(%rsp) // SIMD equivalent of "%nox" vbroadcasti128 (%r10),%ymm15 - vmovdqa %ymm4,512-512(%rax) + vmovdqa %ymm4,512-512(%rax) // save SIMD counters movl $10,%eax jmp L$oop8x @@ -1238,20 +1243,20 @@ L$oop8x: decl %eax jnz L$oop8x - leaq 512(%rsp),%rax - vpaddd 128-256(%rcx),%ymm8,%ymm8 + leaq 512(%rsp),%rax // size optimization + vpaddd 128-256(%rcx),%ymm8,%ymm8 // accumulate key vpaddd 160-256(%rcx),%ymm9,%ymm9 vpaddd 192-256(%rcx),%ymm10,%ymm10 vpaddd 224-256(%rcx),%ymm11,%ymm11 - vpunpckldq %ymm9,%ymm8,%ymm14 + vpunpckldq %ymm9,%ymm8,%ymm14 // "de-interlace" data vpunpckldq %ymm11,%ymm10,%ymm15 vpunpckhdq %ymm9,%ymm8,%ymm8 vpunpckhdq %ymm11,%ymm10,%ymm10 - vpunpcklqdq %ymm15,%ymm14,%ymm9 - vpunpckhqdq %ymm15,%ymm14,%ymm14 - vpunpcklqdq %ymm10,%ymm8,%ymm11 - vpunpckhqdq %ymm10,%ymm8,%ymm8 + vpunpcklqdq %ymm15,%ymm14,%ymm9 // "a0" + vpunpckhqdq %ymm15,%ymm14,%ymm14 // "a1" + vpunpcklqdq %ymm10,%ymm8,%ymm11 // "a2" + vpunpckhqdq %ymm10,%ymm8,%ymm8 // "a3" vpaddd 256-256(%rcx),%ymm0,%ymm0 vpaddd 288-256(%rcx),%ymm1,%ymm1 vpaddd 320-256(%rcx),%ymm2,%ymm2 @@ -1261,11 +1266,11 @@ L$oop8x: vpunpckldq %ymm3,%ymm2,%ymm15 vpunpckhdq %ymm1,%ymm0,%ymm0 vpunpckhdq %ymm3,%ymm2,%ymm2 - vpunpcklqdq %ymm15,%ymm10,%ymm1 - vpunpckhqdq %ymm15,%ymm10,%ymm10 - vpunpcklqdq %ymm2,%ymm0,%ymm3 - vpunpckhqdq %ymm2,%ymm0,%ymm0 - vperm2i128 $0x20,%ymm1,%ymm9,%ymm15 + vpunpcklqdq %ymm15,%ymm10,%ymm1 // "b0" + vpunpckhqdq %ymm15,%ymm10,%ymm10 // "b1" + vpunpcklqdq %ymm2,%ymm0,%ymm3 // "b2" + vpunpckhqdq %ymm2,%ymm0,%ymm0 // "b3" + vperm2i128 $0x20,%ymm1,%ymm9,%ymm15 // "de-interlace" further vperm2i128 $0x31,%ymm1,%ymm9,%ymm1 vperm2i128 $0x20,%ymm10,%ymm14,%ymm9 vperm2i128 $0x31,%ymm10,%ymm14,%ymm10 @@ -1273,10 +1278,10 @@ L$oop8x: vperm2i128 $0x31,%ymm3,%ymm11,%ymm3 vperm2i128 $0x20,%ymm0,%ymm8,%ymm11 vperm2i128 $0x31,%ymm0,%ymm8,%ymm0 - vmovdqa %ymm15,0(%rsp) + vmovdqa %ymm15,0(%rsp) // offload vmovdqa %ymm9,32(%rsp) - vmovdqa 64(%rsp),%ymm15 - vmovdqa 96(%rsp),%ymm9 + vmovdqa 64(%rsp),%ymm15 // %ymm15 + vmovdqa 96(%rsp),%ymm9 // %ymm9 vpaddd 384-512(%rax),%ymm12,%ymm12 vpaddd 416-512(%rax),%ymm13,%ymm13 @@ -1287,10 +1292,10 @@ L$oop8x: vpunpckldq %ymm9,%ymm15,%ymm8 vpunpckhdq %ymm13,%ymm12,%ymm12 vpunpckhdq %ymm9,%ymm15,%ymm15 - vpunpcklqdq %ymm8,%ymm2,%ymm13 - vpunpckhqdq %ymm8,%ymm2,%ymm2 - vpunpcklqdq %ymm15,%ymm12,%ymm9 - vpunpckhqdq %ymm15,%ymm12,%ymm12 + vpunpcklqdq %ymm8,%ymm2,%ymm13 // "c0" + vpunpckhqdq %ymm8,%ymm2,%ymm2 // "c1" + vpunpcklqdq %ymm15,%ymm12,%ymm9 // "c2" + vpunpckhqdq %ymm15,%ymm12,%ymm12 // "c3" vpaddd 512-512(%rax),%ymm4,%ymm4 vpaddd 544-512(%rax),%ymm5,%ymm5 vpaddd 576-512(%rax),%ymm6,%ymm6 @@ -1300,11 +1305,11 @@ L$oop8x: vpunpckldq %ymm7,%ymm6,%ymm8 vpunpckhdq %ymm5,%ymm4,%ymm4 vpunpckhdq %ymm7,%ymm6,%ymm6 - vpunpcklqdq %ymm8,%ymm15,%ymm5 - vpunpckhqdq %ymm8,%ymm15,%ymm15 - vpunpcklqdq %ymm6,%ymm4,%ymm7 - vpunpckhqdq %ymm6,%ymm4,%ymm4 - vperm2i128 $0x20,%ymm5,%ymm13,%ymm8 + vpunpcklqdq %ymm8,%ymm15,%ymm5 // "d0" + vpunpckhqdq %ymm8,%ymm15,%ymm15 // "d1" + vpunpcklqdq %ymm6,%ymm4,%ymm7 // "d2" + vpunpckhqdq %ymm6,%ymm4,%ymm4 // "d3" + vperm2i128 $0x20,%ymm5,%ymm13,%ymm8 // "de-interlace" further vperm2i128 $0x31,%ymm5,%ymm13,%ymm5 vperm2i128 $0x20,%ymm15,%ymm2,%ymm13 vperm2i128 $0x31,%ymm15,%ymm2,%ymm15 @@ -1312,55 +1317,55 @@ L$oop8x: vperm2i128 $0x31,%ymm7,%ymm9,%ymm7 vperm2i128 $0x20,%ymm4,%ymm12,%ymm9 vperm2i128 $0x31,%ymm4,%ymm12,%ymm4 - vmovdqa 0(%rsp),%ymm6 + vmovdqa 0(%rsp),%ymm6 // was offloaded, remember? vmovdqa 32(%rsp),%ymm12 cmpq $512,%rdx jb L$tail8x - vpxor 0(%rsi),%ymm6,%ymm6 + vpxor 0(%rsi),%ymm6,%ymm6 // xor with input vpxor 32(%rsi),%ymm8,%ymm8 vpxor 64(%rsi),%ymm1,%ymm1 vpxor 96(%rsi),%ymm5,%ymm5 - leaq 128(%rsi),%rsi + leaq 128(%rsi),%rsi // size optimization vmovdqu %ymm6,0(%rdi) vmovdqu %ymm8,32(%rdi) vmovdqu %ymm1,64(%rdi) vmovdqu %ymm5,96(%rdi) - leaq 128(%rdi),%rdi + leaq 128(%rdi),%rdi // size optimization vpxor 0(%rsi),%ymm12,%ymm12 vpxor 32(%rsi),%ymm13,%ymm13 vpxor 64(%rsi),%ymm10,%ymm10 vpxor 96(%rsi),%ymm15,%ymm15 - leaq 128(%rsi),%rsi + leaq 128(%rsi),%rsi // size optimization vmovdqu %ymm12,0(%rdi) vmovdqu %ymm13,32(%rdi) vmovdqu %ymm10,64(%rdi) vmovdqu %ymm15,96(%rdi) - leaq 128(%rdi),%rdi + leaq 128(%rdi),%rdi // size optimization vpxor 0(%rsi),%ymm14,%ymm14 vpxor 32(%rsi),%ymm2,%ymm2 vpxor 64(%rsi),%ymm3,%ymm3 vpxor 96(%rsi),%ymm7,%ymm7 - leaq 128(%rsi),%rsi + leaq 128(%rsi),%rsi // size optimization vmovdqu %ymm14,0(%rdi) vmovdqu %ymm2,32(%rdi) vmovdqu %ymm3,64(%rdi) vmovdqu %ymm7,96(%rdi) - leaq 128(%rdi),%rdi + leaq 128(%rdi),%rdi // size optimization vpxor 0(%rsi),%ymm11,%ymm11 vpxor 32(%rsi),%ymm9,%ymm9 vpxor 64(%rsi),%ymm0,%ymm0 vpxor 96(%rsi),%ymm4,%ymm4 - leaq 128(%rsi),%rsi + leaq 128(%rsi),%rsi // size optimization vmovdqu %ymm11,0(%rdi) vmovdqu %ymm9,32(%rdi) vmovdqu %ymm0,64(%rdi) vmovdqu %ymm4,96(%rdi) - leaq 128(%rdi),%rdi + leaq 128(%rdi),%rdi // size optimization subq $512,%rdx jnz L$oop_outer8x @@ -1390,23 +1395,23 @@ L$tail8x: .p2align 5 L$64_or_more8x: - vpxor 0(%rsi),%ymm6,%ymm6 + vpxor 0(%rsi),%ymm6,%ymm6 // xor with input vpxor 32(%rsi),%ymm8,%ymm8 vmovdqu %ymm6,0(%rdi) vmovdqu %ymm8,32(%rdi) je L$done8x - leaq 64(%rsi),%rsi + leaq 64(%rsi),%rsi // inp+=64*1 xorq %r10,%r10 vmovdqa %ymm1,0(%rsp) - leaq 64(%rdi),%rdi - subq $64,%rdx + leaq 64(%rdi),%rdi // out+=64*1 + subq $64,%rdx // len-=64*1 vmovdqa %ymm5,32(%rsp) jmp L$oop_tail8x .p2align 5 L$128_or_more8x: - vpxor 0(%rsi),%ymm6,%ymm6 + vpxor 0(%rsi),%ymm6,%ymm6 // xor with input vpxor 32(%rsi),%ymm8,%ymm8 vpxor 64(%rsi),%ymm1,%ymm1 vpxor 96(%rsi),%ymm5,%ymm5 @@ -1416,17 +1421,17 @@ L$128_or_more8x: vmovdqu %ymm5,96(%rdi) je L$done8x - leaq 128(%rsi),%rsi + leaq 128(%rsi),%rsi // inp+=64*2 xorq %r10,%r10 vmovdqa %ymm12,0(%rsp) - leaq 128(%rdi),%rdi - subq $128,%rdx + leaq 128(%rdi),%rdi // out+=64*2 + subq $128,%rdx // len-=64*2 vmovdqa %ymm13,32(%rsp) jmp L$oop_tail8x .p2align 5 L$192_or_more8x: - vpxor 0(%rsi),%ymm6,%ymm6 + vpxor 0(%rsi),%ymm6,%ymm6 // xor with input vpxor 32(%rsi),%ymm8,%ymm8 vpxor 64(%rsi),%ymm1,%ymm1 vpxor 96(%rsi),%ymm5,%ymm5 @@ -1440,17 +1445,17 @@ L$192_or_more8x: vmovdqu %ymm13,160(%rdi) je L$done8x - leaq 192(%rsi),%rsi + leaq 192(%rsi),%rsi // inp+=64*3 xorq %r10,%r10 vmovdqa %ymm10,0(%rsp) - leaq 192(%rdi),%rdi - subq $192,%rdx + leaq 192(%rdi),%rdi // out+=64*3 + subq $192,%rdx // len-=64*3 vmovdqa %ymm15,32(%rsp) jmp L$oop_tail8x .p2align 5 L$256_or_more8x: - vpxor 0(%rsi),%ymm6,%ymm6 + vpxor 0(%rsi),%ymm6,%ymm6 // xor with input vpxor 32(%rsi),%ymm8,%ymm8 vpxor 64(%rsi),%ymm1,%ymm1 vpxor 96(%rsi),%ymm5,%ymm5 @@ -1468,17 +1473,17 @@ L$256_or_more8x: vmovdqu %ymm15,224(%rdi) je L$done8x - leaq 256(%rsi),%rsi + leaq 256(%rsi),%rsi // inp+=64*4 xorq %r10,%r10 vmovdqa %ymm14,0(%rsp) - leaq 256(%rdi),%rdi - subq $256,%rdx + leaq 256(%rdi),%rdi // out+=64*4 + subq $256,%rdx // len-=64*4 vmovdqa %ymm2,32(%rsp) jmp L$oop_tail8x .p2align 5 L$320_or_more8x: - vpxor 0(%rsi),%ymm6,%ymm6 + vpxor 0(%rsi),%ymm6,%ymm6 // xor with input vpxor 32(%rsi),%ymm8,%ymm8 vpxor 64(%rsi),%ymm1,%ymm1 vpxor 96(%rsi),%ymm5,%ymm5 @@ -1500,17 +1505,17 @@ L$320_or_more8x: vmovdqu %ymm2,288(%rdi) je L$done8x - leaq 320(%rsi),%rsi + leaq 320(%rsi),%rsi // inp+=64*5 xorq %r10,%r10 vmovdqa %ymm3,0(%rsp) - leaq 320(%rdi),%rdi - subq $320,%rdx + leaq 320(%rdi),%rdi // out+=64*5 + subq $320,%rdx // len-=64*5 vmovdqa %ymm7,32(%rsp) jmp L$oop_tail8x .p2align 5 L$384_or_more8x: - vpxor 0(%rsi),%ymm6,%ymm6 + vpxor 0(%rsi),%ymm6,%ymm6 // xor with input vpxor 32(%rsi),%ymm8,%ymm8 vpxor 64(%rsi),%ymm1,%ymm1 vpxor 96(%rsi),%ymm5,%ymm5 @@ -1536,17 +1541,17 @@ L$384_or_more8x: vmovdqu %ymm7,352(%rdi) je L$done8x - leaq 384(%rsi),%rsi + leaq 384(%rsi),%rsi // inp+=64*6 xorq %r10,%r10 vmovdqa %ymm11,0(%rsp) - leaq 384(%rdi),%rdi - subq $384,%rdx + leaq 384(%rdi),%rdi // out+=64*6 + subq $384,%rdx // len-=64*6 vmovdqa %ymm9,32(%rsp) jmp L$oop_tail8x .p2align 5 L$448_or_more8x: - vpxor 0(%rsi),%ymm6,%ymm6 + vpxor 0(%rsi),%ymm6,%ymm6 // xor with input vpxor 32(%rsi),%ymm8,%ymm8 vpxor 64(%rsi),%ymm1,%ymm1 vpxor 96(%rsi),%ymm5,%ymm5 @@ -1576,11 +1581,11 @@ L$448_or_more8x: vmovdqu %ymm9,416(%rdi) je L$done8x - leaq 448(%rsi),%rsi + leaq 448(%rsi),%rsi // inp+=64*7 xorq %r10,%r10 vmovdqa %ymm0,0(%rsp) - leaq 448(%rdi),%rdi - subq $448,%rdx + leaq 448(%rdi),%rdi // out+=64*7 + subq $448,%rdx // len-=64*7 vmovdqa %ymm4,32(%rsp) L$oop_tail8x: diff --git a/third_party/boringssl/gen/crypto/chacha-x86_64-linux.S b/third_party/boringssl/gen/crypto/chacha-x86_64-linux.S index 8ea190d1f..b7ac9eb08 100644 --- a/third_party/boringssl/gen/crypto/chacha-x86_64-linux.S +++ b/third_party/boringssl/gen/crypto/chacha-x86_64-linux.S @@ -7,7 +7,9 @@ .text .section .rodata + .align 64 +chacha_constants: .Lzero: .long 0,0,0,0 .Lone: @@ -66,25 +68,25 @@ _CET_ENDBR .cfi_adjust_cfa_offset 88 .Lctr32_body: - +// movdqa .Lsigma(%rip),%xmm0 movdqu (%rcx),%xmm1 movdqu 16(%rcx),%xmm2 movdqu (%r8),%xmm3 movdqa .Lone(%rip),%xmm4 - - movdqa %xmm1,16(%rsp) - movdqa %xmm2,32(%rsp) - movdqa %xmm3,48(%rsp) - movq %rdx,%rbp +// movdqa %xmm0,4*0(%rsp) # key[0] + movdqa %xmm1,16(%rsp) // key[1] + movdqa %xmm2,32(%rsp) // key[2] + movdqa %xmm3,48(%rsp) // key[3] + movq %rdx,%rbp // reassign %rdx jmp .Loop_outer .align 32 .Loop_outer: - movl $0x61707865,%eax - movl $0x3320646e,%ebx - movl $0x79622d32,%ecx - movl $0x6b206574,%edx + movl $0x61707865,%eax // 'expa' + movl $0x3320646e,%ebx // 'nd 3' + movl $0x79622d32,%ecx // '2-by' + movl $0x6b206574,%edx // 'te k' movl 16(%rsp),%r8d movl 20(%rsp),%r9d movl 24(%rsp),%r10d @@ -94,13 +96,13 @@ _CET_ENDBR movl 56(%rsp),%r14d movl 60(%rsp),%r15d - movq %rbp,64+0(%rsp) + movq %rbp,64+0(%rsp) // save len movl $10,%ebp - movq %rsi,64+8(%rsp) - movq %xmm2,%rsi - movq %rdi,64+16(%rsp) + movq %rsi,64+8(%rsp) // save inp + movq %xmm2,%rsi // "%nox" + movq %rdi,64+16(%rsp) // save out movq %rsi,%rdi - shrq $32,%rdi + shrq $32,%rdi // "%nox" jmp .Loop .align 32 @@ -211,18 +213,18 @@ _CET_ENDBR roll $7,%r8d decl %ebp jnz .Loop - movl %edi,36(%rsp) + movl %edi,36(%rsp) // modulo-scheduled movl %esi,32(%rsp) - movq 64(%rsp),%rbp + movq 64(%rsp),%rbp // load len movdqa %xmm2,%xmm1 - movq 64+8(%rsp),%rsi - paddd %xmm4,%xmm3 - movq 64+16(%rsp),%rdi - - addl $0x61707865,%eax - addl $0x3320646e,%ebx - addl $0x79622d32,%ecx - addl $0x6b206574,%edx + movq 64+8(%rsp),%rsi // load inp + paddd %xmm4,%xmm3 // increment counter + movq 64+16(%rsp),%rdi // load out + + addl $0x61707865,%eax // 'expa' + addl $0x3320646e,%ebx // 'nd 3' + addl $0x79622d32,%ecx // '2-by' + addl $0x6b206574,%edx // 'te k' addl 16(%rsp),%r8d addl 20(%rsp),%r9d addl 24(%rsp),%r10d @@ -236,7 +238,7 @@ _CET_ENDBR cmpq $64,%rbp jb .Ltail - xorl 0(%rsi),%eax + xorl 0(%rsi),%eax // xor with input xorl 4(%rsi),%ebx xorl 8(%rsi),%ecx xorl 12(%rsi),%edx @@ -249,13 +251,13 @@ _CET_ENDBR xorl 52(%rsi),%r13d xorl 56(%rsi),%r14d xorl 60(%rsi),%r15d - leaq 64(%rsi),%rsi + leaq 64(%rsi),%rsi // inp+=64 pxor %xmm1,%xmm0 movdqa %xmm2,32(%rsp) movd %xmm3,48(%rsp) - movl %eax,0(%rdi) + movl %eax,0(%rdi) // write output movl %ebx,4(%rdi) movl %ecx,8(%rdi) movl %edx,12(%rdi) @@ -268,7 +270,7 @@ _CET_ENDBR movl %r13d,52(%rdi) movl %r14d,56(%rdi) movl %r15d,60(%rdi) - leaq 64(%rdi),%rdi + leaq 64(%rdi),%rdi // out+=64 subq $64,%rbp jnz .Loop_outer @@ -328,7 +330,7 @@ _CET_ENDBR ChaCha20_ctr32_ssse3: .cfi_startproc _CET_ENDBR - movq %rsp,%r9 + movq %rsp,%r9 // frame pointer .cfi_def_cfa_register r9 subq $64+8,%rsp movdqa .Lsigma(%rip),%xmm0 @@ -342,7 +344,7 @@ _CET_ENDBR movdqa %xmm1,16(%rsp) movdqa %xmm2,32(%rsp) movdqa %xmm3,48(%rsp) - movq $10,%r8 + movq $10,%r8 // reuse %r8 jmp .Loop_ssse3 .align 32 @@ -413,19 +415,19 @@ _CET_ENDBR movdqu 0(%rsi),%xmm4 movdqu 16(%rsi),%xmm5 - pxor %xmm4,%xmm0 + pxor %xmm4,%xmm0 // xor with input movdqu 32(%rsi),%xmm4 pxor %xmm5,%xmm1 movdqu 48(%rsi),%xmm5 - leaq 64(%rsi),%rsi + leaq 64(%rsi),%rsi // inp+=64 pxor %xmm4,%xmm2 pxor %xmm5,%xmm3 - movdqu %xmm0,0(%rdi) + movdqu %xmm0,0(%rdi) // write output movdqu %xmm1,16(%rdi) movdqu %xmm2,32(%rdi) movdqu %xmm3,48(%rdi) - leaq 64(%rdi),%rdi + leaq 64(%rdi),%rdi // out+=64 subq $64,%rdx jnz .Loop_outer_ssse3 @@ -463,20 +465,20 @@ _CET_ENDBR ChaCha20_ctr32_ssse3_4x: .cfi_startproc _CET_ENDBR - movq %rsp,%r9 + movq %rsp,%r9 // frame pointer .cfi_def_cfa_register r9 subq $0x140+8,%rsp - movdqa .Lsigma(%rip),%xmm11 - movdqu (%rcx),%xmm15 - movdqu 16(%rcx),%xmm7 - movdqu (%r8),%xmm3 - leaq 256(%rsp),%rcx + movdqa .Lsigma(%rip),%xmm11 // key[0] + movdqu (%rcx),%xmm15 // key[1] + movdqu 16(%rcx),%xmm7 // key[2] + movdqu (%r8),%xmm3 // key[3] + leaq 256(%rsp),%rcx // size optimization leaq .Lrot16(%rip),%r10 leaq .Lrot24(%rip),%r11 - pshufd $0x00,%xmm11,%xmm8 + pshufd $0x00,%xmm11,%xmm8 // smash key by lanes... pshufd $0x55,%xmm11,%xmm9 - movdqa %xmm8,64(%rsp) + movdqa %xmm8,64(%rsp) // ... and offload pshufd $0xaa,%xmm11,%xmm10 movdqa %xmm9,80(%rsp) pshufd $0xff,%xmm11,%xmm11 @@ -492,18 +494,18 @@ _CET_ENDBR movdqa %xmm14,160-256(%rcx) movdqa %xmm15,176-256(%rcx) - pshufd $0x00,%xmm7,%xmm4 - pshufd $0x55,%xmm7,%xmm5 + pshufd $0x00,%xmm7,%xmm4 // "" + pshufd $0x55,%xmm7,%xmm5 // "" movdqa %xmm4,192-256(%rcx) - pshufd $0xaa,%xmm7,%xmm6 + pshufd $0xaa,%xmm7,%xmm6 // "" movdqa %xmm5,208-256(%rcx) - pshufd $0xff,%xmm7,%xmm7 + pshufd $0xff,%xmm7,%xmm7 // "" movdqa %xmm6,224-256(%rcx) movdqa %xmm7,240-256(%rcx) pshufd $0x00,%xmm3,%xmm0 pshufd $0x55,%xmm3,%xmm1 - paddd .Linc(%rip),%xmm0 + paddd .Linc(%rip),%xmm0 // don't save counters yet pshufd $0xaa,%xmm3,%xmm2 movdqa %xmm1,272-256(%rcx) pshufd $0xff,%xmm3,%xmm3 @@ -514,7 +516,7 @@ _CET_ENDBR .align 32 .Loop_outer4x: - movdqa 64(%rsp),%xmm8 + movdqa 64(%rsp),%xmm8 // re-load smashed key movdqa 80(%rsp),%xmm9 movdqa 96(%rsp),%xmm10 movdqa 112(%rsp),%xmm11 @@ -522,22 +524,22 @@ _CET_ENDBR movdqa 144-256(%rcx),%xmm13 movdqa 160-256(%rcx),%xmm14 movdqa 176-256(%rcx),%xmm15 - movdqa 192-256(%rcx),%xmm4 - movdqa 208-256(%rcx),%xmm5 - movdqa 224-256(%rcx),%xmm6 - movdqa 240-256(%rcx),%xmm7 + movdqa 192-256(%rcx),%xmm4 // "" + movdqa 208-256(%rcx),%xmm5 // "" + movdqa 224-256(%rcx),%xmm6 // "" + movdqa 240-256(%rcx),%xmm7 // "" movdqa 256-256(%rcx),%xmm0 movdqa 272-256(%rcx),%xmm1 movdqa 288-256(%rcx),%xmm2 movdqa 304-256(%rcx),%xmm3 - paddd .Lfour(%rip),%xmm0 + paddd .Lfour(%rip),%xmm0 // next SIMD counters .Loop_enter4x: - movdqa %xmm6,32(%rsp) - movdqa %xmm7,48(%rsp) - movdqa (%r10),%xmm7 + movdqa %xmm6,32(%rsp) // SIMD equivalent of "%nox" + movdqa %xmm7,48(%rsp) // SIMD equivalent of "%nox" + movdqa (%r10),%xmm7 // .Lrot16(%rip) movl $10,%eax - movdqa %xmm0,256-256(%rcx) + movdqa %xmm0,256-256(%rcx) // save SIMD counters jmp .Loop4x .align 32 @@ -705,32 +707,32 @@ _CET_ENDBR decl %eax jnz .Loop4x - paddd 64(%rsp),%xmm8 + paddd 64(%rsp),%xmm8 // accumulate key material paddd 80(%rsp),%xmm9 paddd 96(%rsp),%xmm10 paddd 112(%rsp),%xmm11 - movdqa %xmm8,%xmm6 + movdqa %xmm8,%xmm6 // "de-interlace" data punpckldq %xmm9,%xmm8 movdqa %xmm10,%xmm7 punpckldq %xmm11,%xmm10 punpckhdq %xmm9,%xmm6 punpckhdq %xmm11,%xmm7 movdqa %xmm8,%xmm9 - punpcklqdq %xmm10,%xmm8 + punpcklqdq %xmm10,%xmm8 // "a0" movdqa %xmm6,%xmm11 - punpcklqdq %xmm7,%xmm6 - punpckhqdq %xmm10,%xmm9 - punpckhqdq %xmm7,%xmm11 + punpcklqdq %xmm7,%xmm6 // "a2" + punpckhqdq %xmm10,%xmm9 // "a1" + punpckhqdq %xmm7,%xmm11 // "a3" paddd 128-256(%rcx),%xmm12 paddd 144-256(%rcx),%xmm13 paddd 160-256(%rcx),%xmm14 paddd 176-256(%rcx),%xmm15 - movdqa %xmm8,0(%rsp) + movdqa %xmm8,0(%rsp) // offload movdqa %xmm9,16(%rsp) - movdqa 32(%rsp),%xmm8 - movdqa 48(%rsp),%xmm9 + movdqa 32(%rsp),%xmm8 // "xc2" + movdqa 48(%rsp),%xmm9 // "xc3" movdqa %xmm12,%xmm10 punpckldq %xmm13,%xmm12 @@ -739,17 +741,17 @@ _CET_ENDBR punpckhdq %xmm13,%xmm10 punpckhdq %xmm15,%xmm7 movdqa %xmm12,%xmm13 - punpcklqdq %xmm14,%xmm12 + punpcklqdq %xmm14,%xmm12 // "b0" movdqa %xmm10,%xmm15 - punpcklqdq %xmm7,%xmm10 - punpckhqdq %xmm14,%xmm13 - punpckhqdq %xmm7,%xmm15 + punpcklqdq %xmm7,%xmm10 // "b2" + punpckhqdq %xmm14,%xmm13 // "b1" + punpckhqdq %xmm7,%xmm15 // "b3" paddd 192-256(%rcx),%xmm4 paddd 208-256(%rcx),%xmm5 paddd 224-256(%rcx),%xmm8 paddd 240-256(%rcx),%xmm9 - movdqa %xmm6,32(%rsp) + movdqa %xmm6,32(%rsp) // keep offloading movdqa %xmm11,48(%rsp) movdqa %xmm4,%xmm14 @@ -759,11 +761,11 @@ _CET_ENDBR punpckhdq %xmm5,%xmm14 punpckhdq %xmm9,%xmm7 movdqa %xmm4,%xmm5 - punpcklqdq %xmm8,%xmm4 + punpcklqdq %xmm8,%xmm4 // "c0" movdqa %xmm14,%xmm9 - punpcklqdq %xmm7,%xmm14 - punpckhqdq %xmm8,%xmm5 - punpckhqdq %xmm7,%xmm9 + punpcklqdq %xmm7,%xmm14 // "c2" + punpckhqdq %xmm8,%xmm5 // "c1" + punpckhqdq %xmm7,%xmm9 // "c3" paddd 256-256(%rcx),%xmm0 paddd 272-256(%rcx),%xmm1 paddd 288-256(%rcx),%xmm2 @@ -776,19 +778,19 @@ _CET_ENDBR punpckhdq %xmm1,%xmm8 punpckhdq %xmm3,%xmm7 movdqa %xmm0,%xmm1 - punpcklqdq %xmm2,%xmm0 + punpcklqdq %xmm2,%xmm0 // "d0" movdqa %xmm8,%xmm3 - punpcklqdq %xmm7,%xmm8 - punpckhqdq %xmm2,%xmm1 - punpckhqdq %xmm7,%xmm3 + punpcklqdq %xmm7,%xmm8 // "d2" + punpckhqdq %xmm2,%xmm1 // "d1" + punpckhqdq %xmm7,%xmm3 // "d3" cmpq $256,%rdx jb .Ltail4x - movdqu 0(%rsi),%xmm6 + movdqu 0(%rsi),%xmm6 // xor with input movdqu 16(%rsi),%xmm11 movdqu 32(%rsi),%xmm2 movdqu 48(%rsi),%xmm7 - pxor 0(%rsp),%xmm6 + pxor 0(%rsp),%xmm6 // is offloaded, remember? pxor %xmm12,%xmm11 pxor %xmm4,%xmm2 pxor %xmm0,%xmm7 @@ -801,7 +803,7 @@ _CET_ENDBR movdqu 96(%rsi),%xmm2 movdqu %xmm7,48(%rdi) movdqu 112(%rsi),%xmm7 - leaq 128(%rsi),%rsi + leaq 128(%rsi),%rsi // size optimization pxor 16(%rsp),%xmm6 pxor %xmm13,%xmm11 pxor %xmm5,%xmm2 @@ -814,7 +816,7 @@ _CET_ENDBR movdqu %xmm2,96(%rdi) movdqu 32(%rsi),%xmm2 movdqu %xmm7,112(%rdi) - leaq 128(%rdi),%rdi + leaq 128(%rdi),%rdi // size optimization movdqu 48(%rsi),%xmm7 pxor 32(%rsp),%xmm6 pxor %xmm10,%xmm11 @@ -829,7 +831,7 @@ _CET_ENDBR movdqu 96(%rsi),%xmm2 movdqu %xmm7,48(%rdi) movdqu 112(%rsi),%xmm7 - leaq 128(%rsi),%rsi + leaq 128(%rsi),%rsi // inp+=64*4 pxor 48(%rsp),%xmm6 pxor %xmm15,%xmm11 pxor %xmm9,%xmm2 @@ -838,7 +840,7 @@ _CET_ENDBR movdqu %xmm11,80(%rdi) movdqu %xmm2,96(%rdi) movdqu %xmm7,112(%rdi) - leaq 128(%rdi),%rdi + leaq 128(%rdi),%rdi // out+=64*4 subq $256,%rdx jnz .Loop_outer4x @@ -853,9 +855,9 @@ _CET_ENDBR cmpq $64,%rdx jae .L64_or_more4x - +// movdqa 0x00(%rsp),%xmm6 # is offloaded, remember? xorq %r10,%r10 - +// movdqa %xmm6,0x00(%rsp) movdqa %xmm12,16(%rsp) movdqa %xmm4,32(%rsp) movdqa %xmm0,48(%rsp) @@ -863,11 +865,11 @@ _CET_ENDBR .align 32 .L64_or_more4x: - movdqu 0(%rsi),%xmm6 + movdqu 0(%rsi),%xmm6 // xor with input movdqu 16(%rsi),%xmm11 movdqu 32(%rsi),%xmm2 movdqu 48(%rsi),%xmm7 - pxor 0(%rsp),%xmm6 + pxor 0(%rsp),%xmm6 // is offloaded, remember? pxor %xmm12,%xmm11 pxor %xmm4,%xmm2 pxor %xmm0,%xmm7 @@ -877,24 +879,24 @@ _CET_ENDBR movdqu %xmm7,48(%rdi) je .Ldone4x - movdqa 16(%rsp),%xmm6 - leaq 64(%rsi),%rsi + movdqa 16(%rsp),%xmm6 // is offloaded, remember? + leaq 64(%rsi),%rsi // inp+=64*1 xorq %r10,%r10 movdqa %xmm6,0(%rsp) movdqa %xmm13,16(%rsp) - leaq 64(%rdi),%rdi + leaq 64(%rdi),%rdi // out+=64*1 movdqa %xmm5,32(%rsp) - subq $64,%rdx + subq $64,%rdx // len-=64*1 movdqa %xmm1,48(%rsp) jmp .Loop_tail4x .align 32 .L128_or_more4x: - movdqu 0(%rsi),%xmm6 + movdqu 0(%rsi),%xmm6 // xor with input movdqu 16(%rsi),%xmm11 movdqu 32(%rsi),%xmm2 movdqu 48(%rsi),%xmm7 - pxor 0(%rsp),%xmm6 + pxor 0(%rsp),%xmm6 // is offloaded, remember? pxor %xmm12,%xmm11 pxor %xmm4,%xmm2 pxor %xmm0,%xmm7 @@ -917,24 +919,24 @@ _CET_ENDBR movdqu %xmm7,112(%rdi) je .Ldone4x - movdqa 32(%rsp),%xmm6 - leaq 128(%rsi),%rsi + movdqa 32(%rsp),%xmm6 // is offloaded, remember? + leaq 128(%rsi),%rsi // inp+=64*2 xorq %r10,%r10 movdqa %xmm6,0(%rsp) movdqa %xmm10,16(%rsp) - leaq 128(%rdi),%rdi + leaq 128(%rdi),%rdi // out+=64*2 movdqa %xmm14,32(%rsp) - subq $128,%rdx + subq $128,%rdx // len-=64*2 movdqa %xmm8,48(%rsp) jmp .Loop_tail4x .align 32 .L192_or_more4x: - movdqu 0(%rsi),%xmm6 + movdqu 0(%rsi),%xmm6 // xor with input movdqu 16(%rsi),%xmm11 movdqu 32(%rsi),%xmm2 movdqu 48(%rsi),%xmm7 - pxor 0(%rsp),%xmm6 + pxor 0(%rsp),%xmm6 // is offloaded, remember? pxor %xmm12,%xmm11 pxor %xmm4,%xmm2 pxor %xmm0,%xmm7 @@ -947,7 +949,7 @@ _CET_ENDBR movdqu 96(%rsi),%xmm2 movdqu %xmm7,48(%rdi) movdqu 112(%rsi),%xmm7 - leaq 128(%rsi),%rsi + leaq 128(%rsi),%rsi // size optimization pxor 16(%rsp),%xmm6 pxor %xmm13,%xmm11 pxor %xmm5,%xmm2 @@ -960,7 +962,7 @@ _CET_ENDBR movdqu %xmm2,96(%rdi) movdqu 32(%rsi),%xmm2 movdqu %xmm7,112(%rdi) - leaq 128(%rdi),%rdi + leaq 128(%rdi),%rdi // size optimization movdqu 48(%rsi),%xmm7 pxor 32(%rsp),%xmm6 pxor %xmm10,%xmm11 @@ -972,14 +974,14 @@ _CET_ENDBR movdqu %xmm7,48(%rdi) je .Ldone4x - movdqa 48(%rsp),%xmm6 - leaq 64(%rsi),%rsi + movdqa 48(%rsp),%xmm6 // is offloaded, remember? + leaq 64(%rsi),%rsi // inp+=64*3 xorq %r10,%r10 movdqa %xmm6,0(%rsp) movdqa %xmm15,16(%rsp) - leaq 64(%rdi),%rdi + leaq 64(%rdi),%rdi // out+=64*3 movdqa %xmm9,32(%rsp) - subq $192,%rdx + subq $192,%rdx // len-=64*3 movdqa %xmm3,48(%rsp) .Loop_tail4x: @@ -1005,33 +1007,33 @@ _CET_ENDBR ChaCha20_ctr32_avx2: .cfi_startproc _CET_ENDBR - movq %rsp,%r9 + movq %rsp,%r9 // frame register .cfi_def_cfa_register r9 subq $0x280+8,%rsp andq $-32,%rsp vzeroupper - - - - - - - - - - vbroadcasti128 .Lsigma(%rip),%ymm11 - vbroadcasti128 (%rcx),%ymm3 - vbroadcasti128 16(%rcx),%ymm15 - vbroadcasti128 (%r8),%ymm7 - leaq 256(%rsp),%rcx - leaq 512(%rsp),%rax +// ############### stack layout +// +0x00 SIMD equivalent of %r12d +// ... +// +0x80 constant copy of key[0-2] smashed by lanes +// ... +// +0x200 SIMD counters (with nonce smashed by lanes) +// ... +// +0x280 + + vbroadcasti128 .Lsigma(%rip),%ymm11 // key[0] + vbroadcasti128 (%rcx),%ymm3 // key[1] + vbroadcasti128 16(%rcx),%ymm15 // key[2] + vbroadcasti128 (%r8),%ymm7 // key[3] + leaq 256(%rsp),%rcx // size optimization + leaq 512(%rsp),%rax // size optimization leaq .Lrot16(%rip),%r10 leaq .Lrot24(%rip),%r11 - vpshufd $0x00,%ymm11,%ymm8 + vpshufd $0x00,%ymm11,%ymm8 // smash key by lanes... vpshufd $0x55,%ymm11,%ymm9 - vmovdqa %ymm8,128-256(%rcx) + vmovdqa %ymm8,128-256(%rcx) // ... and offload vpshufd $0xaa,%ymm11,%ymm10 vmovdqa %ymm9,160-256(%rcx) vpshufd $0xff,%ymm11,%ymm11 @@ -1047,18 +1049,18 @@ _CET_ENDBR vmovdqa %ymm2,320-256(%rcx) vmovdqa %ymm3,352-256(%rcx) - vpshufd $0x00,%ymm15,%ymm12 - vpshufd $0x55,%ymm15,%ymm13 + vpshufd $0x00,%ymm15,%ymm12 // "xc0" + vpshufd $0x55,%ymm15,%ymm13 // "xc1" vmovdqa %ymm12,384-512(%rax) - vpshufd $0xaa,%ymm15,%ymm14 + vpshufd $0xaa,%ymm15,%ymm14 // "xc2" vmovdqa %ymm13,416-512(%rax) - vpshufd $0xff,%ymm15,%ymm15 + vpshufd $0xff,%ymm15,%ymm15 // "xc3" vmovdqa %ymm14,448-512(%rax) vmovdqa %ymm15,480-512(%rax) vpshufd $0x00,%ymm7,%ymm4 vpshufd $0x55,%ymm7,%ymm5 - vpaddd .Lincy(%rip),%ymm4,%ymm4 + vpaddd .Lincy(%rip),%ymm4,%ymm4 // don't save counters yet vpshufd $0xaa,%ymm7,%ymm6 vmovdqa %ymm5,544-512(%rax) vpshufd $0xff,%ymm7,%ymm7 @@ -1069,7 +1071,7 @@ _CET_ENDBR .align 32 .Loop_outer8x: - vmovdqa 128-256(%rcx),%ymm8 + vmovdqa 128-256(%rcx),%ymm8 // re-load smashed key vmovdqa 160-256(%rcx),%ymm9 vmovdqa 192-256(%rcx),%ymm10 vmovdqa 224-256(%rcx),%ymm11 @@ -1077,21 +1079,21 @@ _CET_ENDBR vmovdqa 288-256(%rcx),%ymm1 vmovdqa 320-256(%rcx),%ymm2 vmovdqa 352-256(%rcx),%ymm3 - vmovdqa 384-512(%rax),%ymm12 - vmovdqa 416-512(%rax),%ymm13 - vmovdqa 448-512(%rax),%ymm14 - vmovdqa 480-512(%rax),%ymm15 + vmovdqa 384-512(%rax),%ymm12 // "xc0" + vmovdqa 416-512(%rax),%ymm13 // "xc1" + vmovdqa 448-512(%rax),%ymm14 // "xc2" + vmovdqa 480-512(%rax),%ymm15 // "xc3" vmovdqa 512-512(%rax),%ymm4 vmovdqa 544-512(%rax),%ymm5 vmovdqa 576-512(%rax),%ymm6 vmovdqa 608-512(%rax),%ymm7 - vpaddd .Leight(%rip),%ymm4,%ymm4 + vpaddd .Leight(%rip),%ymm4,%ymm4 // next SIMD counters .Loop_enter8x: - vmovdqa %ymm14,64(%rsp) - vmovdqa %ymm15,96(%rsp) + vmovdqa %ymm14,64(%rsp) // SIMD equivalent of "%nox" + vmovdqa %ymm15,96(%rsp) // SIMD equivalent of "%nox" vbroadcasti128 (%r10),%ymm15 - vmovdqa %ymm4,512-512(%rax) + vmovdqa %ymm4,512-512(%rax) // save SIMD counters movl $10,%eax jmp .Loop8x @@ -1244,20 +1246,20 @@ _CET_ENDBR decl %eax jnz .Loop8x - leaq 512(%rsp),%rax - vpaddd 128-256(%rcx),%ymm8,%ymm8 + leaq 512(%rsp),%rax // size optimization + vpaddd 128-256(%rcx),%ymm8,%ymm8 // accumulate key vpaddd 160-256(%rcx),%ymm9,%ymm9 vpaddd 192-256(%rcx),%ymm10,%ymm10 vpaddd 224-256(%rcx),%ymm11,%ymm11 - vpunpckldq %ymm9,%ymm8,%ymm14 + vpunpckldq %ymm9,%ymm8,%ymm14 // "de-interlace" data vpunpckldq %ymm11,%ymm10,%ymm15 vpunpckhdq %ymm9,%ymm8,%ymm8 vpunpckhdq %ymm11,%ymm10,%ymm10 - vpunpcklqdq %ymm15,%ymm14,%ymm9 - vpunpckhqdq %ymm15,%ymm14,%ymm14 - vpunpcklqdq %ymm10,%ymm8,%ymm11 - vpunpckhqdq %ymm10,%ymm8,%ymm8 + vpunpcklqdq %ymm15,%ymm14,%ymm9 // "a0" + vpunpckhqdq %ymm15,%ymm14,%ymm14 // "a1" + vpunpcklqdq %ymm10,%ymm8,%ymm11 // "a2" + vpunpckhqdq %ymm10,%ymm8,%ymm8 // "a3" vpaddd 256-256(%rcx),%ymm0,%ymm0 vpaddd 288-256(%rcx),%ymm1,%ymm1 vpaddd 320-256(%rcx),%ymm2,%ymm2 @@ -1267,11 +1269,11 @@ _CET_ENDBR vpunpckldq %ymm3,%ymm2,%ymm15 vpunpckhdq %ymm1,%ymm0,%ymm0 vpunpckhdq %ymm3,%ymm2,%ymm2 - vpunpcklqdq %ymm15,%ymm10,%ymm1 - vpunpckhqdq %ymm15,%ymm10,%ymm10 - vpunpcklqdq %ymm2,%ymm0,%ymm3 - vpunpckhqdq %ymm2,%ymm0,%ymm0 - vperm2i128 $0x20,%ymm1,%ymm9,%ymm15 + vpunpcklqdq %ymm15,%ymm10,%ymm1 // "b0" + vpunpckhqdq %ymm15,%ymm10,%ymm10 // "b1" + vpunpcklqdq %ymm2,%ymm0,%ymm3 // "b2" + vpunpckhqdq %ymm2,%ymm0,%ymm0 // "b3" + vperm2i128 $0x20,%ymm1,%ymm9,%ymm15 // "de-interlace" further vperm2i128 $0x31,%ymm1,%ymm9,%ymm1 vperm2i128 $0x20,%ymm10,%ymm14,%ymm9 vperm2i128 $0x31,%ymm10,%ymm14,%ymm10 @@ -1279,10 +1281,10 @@ _CET_ENDBR vperm2i128 $0x31,%ymm3,%ymm11,%ymm3 vperm2i128 $0x20,%ymm0,%ymm8,%ymm11 vperm2i128 $0x31,%ymm0,%ymm8,%ymm0 - vmovdqa %ymm15,0(%rsp) + vmovdqa %ymm15,0(%rsp) // offload vmovdqa %ymm9,32(%rsp) - vmovdqa 64(%rsp),%ymm15 - vmovdqa 96(%rsp),%ymm9 + vmovdqa 64(%rsp),%ymm15 // %ymm15 + vmovdqa 96(%rsp),%ymm9 // %ymm9 vpaddd 384-512(%rax),%ymm12,%ymm12 vpaddd 416-512(%rax),%ymm13,%ymm13 @@ -1293,10 +1295,10 @@ _CET_ENDBR vpunpckldq %ymm9,%ymm15,%ymm8 vpunpckhdq %ymm13,%ymm12,%ymm12 vpunpckhdq %ymm9,%ymm15,%ymm15 - vpunpcklqdq %ymm8,%ymm2,%ymm13 - vpunpckhqdq %ymm8,%ymm2,%ymm2 - vpunpcklqdq %ymm15,%ymm12,%ymm9 - vpunpckhqdq %ymm15,%ymm12,%ymm12 + vpunpcklqdq %ymm8,%ymm2,%ymm13 // "c0" + vpunpckhqdq %ymm8,%ymm2,%ymm2 // "c1" + vpunpcklqdq %ymm15,%ymm12,%ymm9 // "c2" + vpunpckhqdq %ymm15,%ymm12,%ymm12 // "c3" vpaddd 512-512(%rax),%ymm4,%ymm4 vpaddd 544-512(%rax),%ymm5,%ymm5 vpaddd 576-512(%rax),%ymm6,%ymm6 @@ -1306,11 +1308,11 @@ _CET_ENDBR vpunpckldq %ymm7,%ymm6,%ymm8 vpunpckhdq %ymm5,%ymm4,%ymm4 vpunpckhdq %ymm7,%ymm6,%ymm6 - vpunpcklqdq %ymm8,%ymm15,%ymm5 - vpunpckhqdq %ymm8,%ymm15,%ymm15 - vpunpcklqdq %ymm6,%ymm4,%ymm7 - vpunpckhqdq %ymm6,%ymm4,%ymm4 - vperm2i128 $0x20,%ymm5,%ymm13,%ymm8 + vpunpcklqdq %ymm8,%ymm15,%ymm5 // "d0" + vpunpckhqdq %ymm8,%ymm15,%ymm15 // "d1" + vpunpcklqdq %ymm6,%ymm4,%ymm7 // "d2" + vpunpckhqdq %ymm6,%ymm4,%ymm4 // "d3" + vperm2i128 $0x20,%ymm5,%ymm13,%ymm8 // "de-interlace" further vperm2i128 $0x31,%ymm5,%ymm13,%ymm5 vperm2i128 $0x20,%ymm15,%ymm2,%ymm13 vperm2i128 $0x31,%ymm15,%ymm2,%ymm15 @@ -1318,55 +1320,55 @@ _CET_ENDBR vperm2i128 $0x31,%ymm7,%ymm9,%ymm7 vperm2i128 $0x20,%ymm4,%ymm12,%ymm9 vperm2i128 $0x31,%ymm4,%ymm12,%ymm4 - vmovdqa 0(%rsp),%ymm6 + vmovdqa 0(%rsp),%ymm6 // was offloaded, remember? vmovdqa 32(%rsp),%ymm12 cmpq $512,%rdx jb .Ltail8x - vpxor 0(%rsi),%ymm6,%ymm6 + vpxor 0(%rsi),%ymm6,%ymm6 // xor with input vpxor 32(%rsi),%ymm8,%ymm8 vpxor 64(%rsi),%ymm1,%ymm1 vpxor 96(%rsi),%ymm5,%ymm5 - leaq 128(%rsi),%rsi + leaq 128(%rsi),%rsi // size optimization vmovdqu %ymm6,0(%rdi) vmovdqu %ymm8,32(%rdi) vmovdqu %ymm1,64(%rdi) vmovdqu %ymm5,96(%rdi) - leaq 128(%rdi),%rdi + leaq 128(%rdi),%rdi // size optimization vpxor 0(%rsi),%ymm12,%ymm12 vpxor 32(%rsi),%ymm13,%ymm13 vpxor 64(%rsi),%ymm10,%ymm10 vpxor 96(%rsi),%ymm15,%ymm15 - leaq 128(%rsi),%rsi + leaq 128(%rsi),%rsi // size optimization vmovdqu %ymm12,0(%rdi) vmovdqu %ymm13,32(%rdi) vmovdqu %ymm10,64(%rdi) vmovdqu %ymm15,96(%rdi) - leaq 128(%rdi),%rdi + leaq 128(%rdi),%rdi // size optimization vpxor 0(%rsi),%ymm14,%ymm14 vpxor 32(%rsi),%ymm2,%ymm2 vpxor 64(%rsi),%ymm3,%ymm3 vpxor 96(%rsi),%ymm7,%ymm7 - leaq 128(%rsi),%rsi + leaq 128(%rsi),%rsi // size optimization vmovdqu %ymm14,0(%rdi) vmovdqu %ymm2,32(%rdi) vmovdqu %ymm3,64(%rdi) vmovdqu %ymm7,96(%rdi) - leaq 128(%rdi),%rdi + leaq 128(%rdi),%rdi // size optimization vpxor 0(%rsi),%ymm11,%ymm11 vpxor 32(%rsi),%ymm9,%ymm9 vpxor 64(%rsi),%ymm0,%ymm0 vpxor 96(%rsi),%ymm4,%ymm4 - leaq 128(%rsi),%rsi + leaq 128(%rsi),%rsi // size optimization vmovdqu %ymm11,0(%rdi) vmovdqu %ymm9,32(%rdi) vmovdqu %ymm0,64(%rdi) vmovdqu %ymm4,96(%rdi) - leaq 128(%rdi),%rdi + leaq 128(%rdi),%rdi // size optimization subq $512,%rdx jnz .Loop_outer8x @@ -1396,23 +1398,23 @@ _CET_ENDBR .align 32 .L64_or_more8x: - vpxor 0(%rsi),%ymm6,%ymm6 + vpxor 0(%rsi),%ymm6,%ymm6 // xor with input vpxor 32(%rsi),%ymm8,%ymm8 vmovdqu %ymm6,0(%rdi) vmovdqu %ymm8,32(%rdi) je .Ldone8x - leaq 64(%rsi),%rsi + leaq 64(%rsi),%rsi // inp+=64*1 xorq %r10,%r10 vmovdqa %ymm1,0(%rsp) - leaq 64(%rdi),%rdi - subq $64,%rdx + leaq 64(%rdi),%rdi // out+=64*1 + subq $64,%rdx // len-=64*1 vmovdqa %ymm5,32(%rsp) jmp .Loop_tail8x .align 32 .L128_or_more8x: - vpxor 0(%rsi),%ymm6,%ymm6 + vpxor 0(%rsi),%ymm6,%ymm6 // xor with input vpxor 32(%rsi),%ymm8,%ymm8 vpxor 64(%rsi),%ymm1,%ymm1 vpxor 96(%rsi),%ymm5,%ymm5 @@ -1422,17 +1424,17 @@ _CET_ENDBR vmovdqu %ymm5,96(%rdi) je .Ldone8x - leaq 128(%rsi),%rsi + leaq 128(%rsi),%rsi // inp+=64*2 xorq %r10,%r10 vmovdqa %ymm12,0(%rsp) - leaq 128(%rdi),%rdi - subq $128,%rdx + leaq 128(%rdi),%rdi // out+=64*2 + subq $128,%rdx // len-=64*2 vmovdqa %ymm13,32(%rsp) jmp .Loop_tail8x .align 32 .L192_or_more8x: - vpxor 0(%rsi),%ymm6,%ymm6 + vpxor 0(%rsi),%ymm6,%ymm6 // xor with input vpxor 32(%rsi),%ymm8,%ymm8 vpxor 64(%rsi),%ymm1,%ymm1 vpxor 96(%rsi),%ymm5,%ymm5 @@ -1446,17 +1448,17 @@ _CET_ENDBR vmovdqu %ymm13,160(%rdi) je .Ldone8x - leaq 192(%rsi),%rsi + leaq 192(%rsi),%rsi // inp+=64*3 xorq %r10,%r10 vmovdqa %ymm10,0(%rsp) - leaq 192(%rdi),%rdi - subq $192,%rdx + leaq 192(%rdi),%rdi // out+=64*3 + subq $192,%rdx // len-=64*3 vmovdqa %ymm15,32(%rsp) jmp .Loop_tail8x .align 32 .L256_or_more8x: - vpxor 0(%rsi),%ymm6,%ymm6 + vpxor 0(%rsi),%ymm6,%ymm6 // xor with input vpxor 32(%rsi),%ymm8,%ymm8 vpxor 64(%rsi),%ymm1,%ymm1 vpxor 96(%rsi),%ymm5,%ymm5 @@ -1474,17 +1476,17 @@ _CET_ENDBR vmovdqu %ymm15,224(%rdi) je .Ldone8x - leaq 256(%rsi),%rsi + leaq 256(%rsi),%rsi // inp+=64*4 xorq %r10,%r10 vmovdqa %ymm14,0(%rsp) - leaq 256(%rdi),%rdi - subq $256,%rdx + leaq 256(%rdi),%rdi // out+=64*4 + subq $256,%rdx // len-=64*4 vmovdqa %ymm2,32(%rsp) jmp .Loop_tail8x .align 32 .L320_or_more8x: - vpxor 0(%rsi),%ymm6,%ymm6 + vpxor 0(%rsi),%ymm6,%ymm6 // xor with input vpxor 32(%rsi),%ymm8,%ymm8 vpxor 64(%rsi),%ymm1,%ymm1 vpxor 96(%rsi),%ymm5,%ymm5 @@ -1506,17 +1508,17 @@ _CET_ENDBR vmovdqu %ymm2,288(%rdi) je .Ldone8x - leaq 320(%rsi),%rsi + leaq 320(%rsi),%rsi // inp+=64*5 xorq %r10,%r10 vmovdqa %ymm3,0(%rsp) - leaq 320(%rdi),%rdi - subq $320,%rdx + leaq 320(%rdi),%rdi // out+=64*5 + subq $320,%rdx // len-=64*5 vmovdqa %ymm7,32(%rsp) jmp .Loop_tail8x .align 32 .L384_or_more8x: - vpxor 0(%rsi),%ymm6,%ymm6 + vpxor 0(%rsi),%ymm6,%ymm6 // xor with input vpxor 32(%rsi),%ymm8,%ymm8 vpxor 64(%rsi),%ymm1,%ymm1 vpxor 96(%rsi),%ymm5,%ymm5 @@ -1542,17 +1544,17 @@ _CET_ENDBR vmovdqu %ymm7,352(%rdi) je .Ldone8x - leaq 384(%rsi),%rsi + leaq 384(%rsi),%rsi // inp+=64*6 xorq %r10,%r10 vmovdqa %ymm11,0(%rsp) - leaq 384(%rdi),%rdi - subq $384,%rdx + leaq 384(%rdi),%rdi // out+=64*6 + subq $384,%rdx // len-=64*6 vmovdqa %ymm9,32(%rsp) jmp .Loop_tail8x .align 32 .L448_or_more8x: - vpxor 0(%rsi),%ymm6,%ymm6 + vpxor 0(%rsi),%ymm6,%ymm6 // xor with input vpxor 32(%rsi),%ymm8,%ymm8 vpxor 64(%rsi),%ymm1,%ymm1 vpxor 96(%rsi),%ymm5,%ymm5 @@ -1582,11 +1584,11 @@ _CET_ENDBR vmovdqu %ymm9,416(%rdi) je .Ldone8x - leaq 448(%rsi),%rsi + leaq 448(%rsi),%rsi // inp+=64*7 xorq %r10,%r10 vmovdqa %ymm0,0(%rsp) - leaq 448(%rdi),%rdi - subq $448,%rdx + leaq 448(%rdi),%rdi // out+=64*7 + subq $448,%rdx // len-=64*7 vmovdqa %ymm4,32(%rsp) .Loop_tail8x: diff --git a/third_party/boringssl/gen/crypto/chacha-x86_64-win.asm b/third_party/boringssl/gen/crypto/chacha-x86_64-win.asm index ca5b73c50..9cd683b54 100644 --- a/third_party/boringssl/gen/crypto/chacha-x86_64-win.asm +++ b/third_party/boringssl/gen/crypto/chacha-x86_64-win.asm @@ -15,7 +15,9 @@ section .text code align=64 section .rdata rdata align=8 + ALIGN 64 +chacha_constants: $L$zero: DD 0,0,0,0 $L$one: @@ -48,7 +50,7 @@ $L$sixteen: DB 95,54,52,44,32,67,82,89,80,84,79,71,65,77,83,32 DB 98,121,32,60,97,112,112,114,111,64,111,112,101,110,115,115 DB 108,46,111,114,103,62,0 -section .text +section .text code align=64 global ChaCha20_ctr32_nohw @@ -83,25 +85,25 @@ _CET_ENDBR $L$ctr32_body: - +; movdqa .Lsigma(%rip),%xmm0 movdqu xmm1,XMMWORD[rcx] movdqu xmm2,XMMWORD[16+rcx] movdqu xmm3,XMMWORD[r8] movdqa xmm4,XMMWORD[$L$one] - - movdqa XMMWORD[16+rsp],xmm1 - movdqa XMMWORD[32+rsp],xmm2 - movdqa XMMWORD[48+rsp],xmm3 - mov rbp,rdx +; movdqa %xmm0,4*0(%rsp) # key[0] + movdqa XMMWORD[16+rsp],xmm1 ; key[1] + movdqa XMMWORD[32+rsp],xmm2 ; key[2] + movdqa XMMWORD[48+rsp],xmm3 ; key[3] + mov rbp,rdx ; reassign %rdx jmp NEAR $L$oop_outer ALIGN 32 $L$oop_outer: - mov eax,0x61707865 - mov ebx,0x3320646e - mov ecx,0x79622d32 - mov edx,0x6b206574 + mov eax,0x61707865 ; 'expa' + mov ebx,0x3320646e ; 'nd 3' + mov ecx,0x79622d32 ; '2-by' + mov edx,0x6b206574 ; 'te k' mov r8d,DWORD[16+rsp] mov r9d,DWORD[20+rsp] mov r10d,DWORD[24+rsp] @@ -111,13 +113,13 @@ $L$oop_outer: mov r14d,DWORD[56+rsp] mov r15d,DWORD[60+rsp] - mov QWORD[((64+0))+rsp],rbp + mov QWORD[((64+0))+rsp],rbp ; save len mov ebp,10 - mov QWORD[((64+8))+rsp],rsi - movq rsi,xmm2 - mov QWORD[((64+16))+rsp],rdi + mov QWORD[((64+8))+rsp],rsi ; save inp + movq rsi,xmm2 ; "%nox" + mov QWORD[((64+16))+rsp],rdi ; save out mov rdi,rsi - shr rdi,32 + shr rdi,32 ; "%nox" jmp NEAR $L$oop ALIGN 32 @@ -228,18 +230,18 @@ $L$oop: rol r8d,7 dec ebp jnz NEAR $L$oop - mov DWORD[36+rsp],edi + mov DWORD[36+rsp],edi ; modulo-scheduled mov DWORD[32+rsp],esi - mov rbp,QWORD[64+rsp] + mov rbp,QWORD[64+rsp] ; load len movdqa xmm1,xmm2 - mov rsi,QWORD[((64+8))+rsp] - paddd xmm3,xmm4 - mov rdi,QWORD[((64+16))+rsp] - - add eax,0x61707865 - add ebx,0x3320646e - add ecx,0x79622d32 - add edx,0x6b206574 + mov rsi,QWORD[((64+8))+rsp] ; load inp + paddd xmm3,xmm4 ; increment counter + mov rdi,QWORD[((64+16))+rsp] ; load out + + add eax,0x61707865 ; 'expa' + add ebx,0x3320646e ; 'nd 3' + add ecx,0x79622d32 ; '2-by' + add edx,0x6b206574 ; 'te k' add r8d,DWORD[16+rsp] add r9d,DWORD[20+rsp] add r10d,DWORD[24+rsp] @@ -253,7 +255,7 @@ $L$oop: cmp rbp,64 jb NEAR $L$tail - xor eax,DWORD[rsi] + xor eax,DWORD[rsi] ; xor with input xor ebx,DWORD[4+rsi] xor ecx,DWORD[8+rsi] xor edx,DWORD[12+rsi] @@ -266,13 +268,13 @@ $L$oop: xor r13d,DWORD[52+rsi] xor r14d,DWORD[56+rsi] xor r15d,DWORD[60+rsi] - lea rsi,[64+rsi] + lea rsi,[64+rsi] ; inp+=64 pxor xmm0,xmm1 movdqa XMMWORD[32+rsp],xmm2 movd DWORD[48+rsp],xmm3 - mov DWORD[rdi],eax + mov DWORD[rdi],eax ; write output mov DWORD[4+rdi],ebx mov DWORD[8+rdi],ecx mov DWORD[12+rdi],edx @@ -285,7 +287,7 @@ $L$oop: mov DWORD[52+rdi],r13d mov DWORD[56+rdi],r14d mov DWORD[60+rdi],r15d - lea rdi,[64+rdi] + lea rdi,[64+rdi] ; out+=64 sub rbp,64 jnz NEAR $L$oop_outer @@ -357,7 +359,7 @@ $L$SEH_begin_ChaCha20_ctr32_ssse3: _CET_ENDBR - mov r9,rsp + mov r9,rsp ; frame pointer sub rsp,64+40 movaps XMMWORD[(-40)+r9],xmm6 @@ -374,7 +376,7 @@ $L$ssse3_body: movdqa XMMWORD[16+rsp],xmm1 movdqa XMMWORD[32+rsp],xmm2 movdqa XMMWORD[48+rsp],xmm3 - mov r8,10 + mov r8,10 ; reuse %r8 jmp NEAR $L$oop_ssse3 ALIGN 32 @@ -445,19 +447,19 @@ $L$oop_ssse3: movdqu xmm4,XMMWORD[rsi] movdqu xmm5,XMMWORD[16+rsi] - pxor xmm0,xmm4 + pxor xmm0,xmm4 ; xor with input movdqu xmm4,XMMWORD[32+rsi] pxor xmm1,xmm5 movdqu xmm5,XMMWORD[48+rsi] - lea rsi,[64+rsi] + lea rsi,[64+rsi] ; inp+=64 pxor xmm2,xmm4 pxor xmm3,xmm5 - movdqu XMMWORD[rdi],xmm0 + movdqu XMMWORD[rdi],xmm0 ; write output movdqu XMMWORD[16+rdi],xmm1 movdqu XMMWORD[32+rdi],xmm2 movdqu XMMWORD[48+rdi],xmm3 - lea rdi,[64+rdi] + lea rdi,[64+rdi] ; out+=64 sub rdx,64 jnz NEAR $L$oop_outer_ssse3 @@ -509,7 +511,7 @@ $L$SEH_begin_ChaCha20_ctr32_ssse3_4x: _CET_ENDBR - mov r9,rsp + mov r9,rsp ; frame pointer sub rsp,0x140+168 movaps XMMWORD[(-168)+r9],xmm6 @@ -523,17 +525,17 @@ _CET_ENDBR movaps XMMWORD[(-40)+r9],xmm14 movaps XMMWORD[(-24)+r9],xmm15 $L$4x_body: - movdqa xmm11,XMMWORD[$L$sigma] - movdqu xmm15,XMMWORD[rcx] - movdqu xmm7,XMMWORD[16+rcx] - movdqu xmm3,XMMWORD[r8] - lea rcx,[256+rsp] + movdqa xmm11,XMMWORD[$L$sigma] ; key[0] + movdqu xmm15,XMMWORD[rcx] ; key[1] + movdqu xmm7,XMMWORD[16+rcx] ; key[2] + movdqu xmm3,XMMWORD[r8] ; key[3] + lea rcx,[256+rsp] ; size optimization lea r10,[$L$rot16] lea r11,[$L$rot24] - pshufd xmm8,xmm11,0x00 + pshufd xmm8,xmm11,0x00 ; smash key by lanes... pshufd xmm9,xmm11,0x55 - movdqa XMMWORD[64+rsp],xmm8 + movdqa XMMWORD[64+rsp],xmm8 ; ... and offload pshufd xmm10,xmm11,0xaa movdqa XMMWORD[80+rsp],xmm9 pshufd xmm11,xmm11,0xff @@ -549,18 +551,18 @@ $L$4x_body: movdqa XMMWORD[(160-256)+rcx],xmm14 movdqa XMMWORD[(176-256)+rcx],xmm15 - pshufd xmm4,xmm7,0x00 - pshufd xmm5,xmm7,0x55 + pshufd xmm4,xmm7,0x00 ; "" + pshufd xmm5,xmm7,0x55 ; "" movdqa XMMWORD[(192-256)+rcx],xmm4 - pshufd xmm6,xmm7,0xaa + pshufd xmm6,xmm7,0xaa ; "" movdqa XMMWORD[(208-256)+rcx],xmm5 - pshufd xmm7,xmm7,0xff + pshufd xmm7,xmm7,0xff ; "" movdqa XMMWORD[(224-256)+rcx],xmm6 movdqa XMMWORD[(240-256)+rcx],xmm7 pshufd xmm0,xmm3,0x00 pshufd xmm1,xmm3,0x55 - paddd xmm0,XMMWORD[$L$inc] + paddd xmm0,XMMWORD[$L$inc] ; don't save counters yet pshufd xmm2,xmm3,0xaa movdqa XMMWORD[(272-256)+rcx],xmm1 pshufd xmm3,xmm3,0xff @@ -571,7 +573,7 @@ $L$4x_body: ALIGN 32 $L$oop_outer4x: - movdqa xmm8,XMMWORD[64+rsp] + movdqa xmm8,XMMWORD[64+rsp] ; re-load smashed key movdqa xmm9,XMMWORD[80+rsp] movdqa xmm10,XMMWORD[96+rsp] movdqa xmm11,XMMWORD[112+rsp] @@ -579,22 +581,22 @@ $L$oop_outer4x: movdqa xmm13,XMMWORD[((144-256))+rcx] movdqa xmm14,XMMWORD[((160-256))+rcx] movdqa xmm15,XMMWORD[((176-256))+rcx] - movdqa xmm4,XMMWORD[((192-256))+rcx] - movdqa xmm5,XMMWORD[((208-256))+rcx] - movdqa xmm6,XMMWORD[((224-256))+rcx] - movdqa xmm7,XMMWORD[((240-256))+rcx] + movdqa xmm4,XMMWORD[((192-256))+rcx] ; "" + movdqa xmm5,XMMWORD[((208-256))+rcx] ; "" + movdqa xmm6,XMMWORD[((224-256))+rcx] ; "" + movdqa xmm7,XMMWORD[((240-256))+rcx] ; "" movdqa xmm0,XMMWORD[((256-256))+rcx] movdqa xmm1,XMMWORD[((272-256))+rcx] movdqa xmm2,XMMWORD[((288-256))+rcx] movdqa xmm3,XMMWORD[((304-256))+rcx] - paddd xmm0,XMMWORD[$L$four] + paddd xmm0,XMMWORD[$L$four] ; next SIMD counters $L$oop_enter4x: - movdqa XMMWORD[32+rsp],xmm6 - movdqa XMMWORD[48+rsp],xmm7 - movdqa xmm7,XMMWORD[r10] + movdqa XMMWORD[32+rsp],xmm6 ; SIMD equivalent of "%nox" + movdqa XMMWORD[48+rsp],xmm7 ; SIMD equivalent of "%nox" + movdqa xmm7,XMMWORD[r10] ; .Lrot16(%rip) mov eax,10 - movdqa XMMWORD[(256-256)+rcx],xmm0 + movdqa XMMWORD[(256-256)+rcx],xmm0 ; save SIMD counters jmp NEAR $L$oop4x ALIGN 32 @@ -762,32 +764,32 @@ $L$oop4x: dec eax jnz NEAR $L$oop4x - paddd xmm8,XMMWORD[64+rsp] + paddd xmm8,XMMWORD[64+rsp] ; accumulate key material paddd xmm9,XMMWORD[80+rsp] paddd xmm10,XMMWORD[96+rsp] paddd xmm11,XMMWORD[112+rsp] - movdqa xmm6,xmm8 + movdqa xmm6,xmm8 ; "de-interlace" data punpckldq xmm8,xmm9 movdqa xmm7,xmm10 punpckldq xmm10,xmm11 punpckhdq xmm6,xmm9 punpckhdq xmm7,xmm11 movdqa xmm9,xmm8 - punpcklqdq xmm8,xmm10 + punpcklqdq xmm8,xmm10 ; "a0" movdqa xmm11,xmm6 - punpcklqdq xmm6,xmm7 - punpckhqdq xmm9,xmm10 - punpckhqdq xmm11,xmm7 + punpcklqdq xmm6,xmm7 ; "a2" + punpckhqdq xmm9,xmm10 ; "a1" + punpckhqdq xmm11,xmm7 ; "a3" paddd xmm12,XMMWORD[((128-256))+rcx] paddd xmm13,XMMWORD[((144-256))+rcx] paddd xmm14,XMMWORD[((160-256))+rcx] paddd xmm15,XMMWORD[((176-256))+rcx] - movdqa XMMWORD[rsp],xmm8 + movdqa XMMWORD[rsp],xmm8 ; offload movdqa XMMWORD[16+rsp],xmm9 - movdqa xmm8,XMMWORD[32+rsp] - movdqa xmm9,XMMWORD[48+rsp] + movdqa xmm8,XMMWORD[32+rsp] ; "xc2" + movdqa xmm9,XMMWORD[48+rsp] ; "xc3" movdqa xmm10,xmm12 punpckldq xmm12,xmm13 @@ -796,17 +798,17 @@ $L$oop4x: punpckhdq xmm10,xmm13 punpckhdq xmm7,xmm15 movdqa xmm13,xmm12 - punpcklqdq xmm12,xmm14 + punpcklqdq xmm12,xmm14 ; "b0" movdqa xmm15,xmm10 - punpcklqdq xmm10,xmm7 - punpckhqdq xmm13,xmm14 - punpckhqdq xmm15,xmm7 + punpcklqdq xmm10,xmm7 ; "b2" + punpckhqdq xmm13,xmm14 ; "b1" + punpckhqdq xmm15,xmm7 ; "b3" paddd xmm4,XMMWORD[((192-256))+rcx] paddd xmm5,XMMWORD[((208-256))+rcx] paddd xmm8,XMMWORD[((224-256))+rcx] paddd xmm9,XMMWORD[((240-256))+rcx] - movdqa XMMWORD[32+rsp],xmm6 + movdqa XMMWORD[32+rsp],xmm6 ; keep offloading movdqa XMMWORD[48+rsp],xmm11 movdqa xmm14,xmm4 @@ -816,11 +818,11 @@ $L$oop4x: punpckhdq xmm14,xmm5 punpckhdq xmm7,xmm9 movdqa xmm5,xmm4 - punpcklqdq xmm4,xmm8 + punpcklqdq xmm4,xmm8 ; "c0" movdqa xmm9,xmm14 - punpcklqdq xmm14,xmm7 - punpckhqdq xmm5,xmm8 - punpckhqdq xmm9,xmm7 + punpcklqdq xmm14,xmm7 ; "c2" + punpckhqdq xmm5,xmm8 ; "c1" + punpckhqdq xmm9,xmm7 ; "c3" paddd xmm0,XMMWORD[((256-256))+rcx] paddd xmm1,XMMWORD[((272-256))+rcx] paddd xmm2,XMMWORD[((288-256))+rcx] @@ -833,19 +835,19 @@ $L$oop4x: punpckhdq xmm8,xmm1 punpckhdq xmm7,xmm3 movdqa xmm1,xmm0 - punpcklqdq xmm0,xmm2 + punpcklqdq xmm0,xmm2 ; "d0" movdqa xmm3,xmm8 - punpcklqdq xmm8,xmm7 - punpckhqdq xmm1,xmm2 - punpckhqdq xmm3,xmm7 + punpcklqdq xmm8,xmm7 ; "d2" + punpckhqdq xmm1,xmm2 ; "d1" + punpckhqdq xmm3,xmm7 ; "d3" cmp rdx,64*4 jb NEAR $L$tail4x - movdqu xmm6,XMMWORD[rsi] + movdqu xmm6,XMMWORD[rsi] ; xor with input movdqu xmm11,XMMWORD[16+rsi] movdqu xmm2,XMMWORD[32+rsi] movdqu xmm7,XMMWORD[48+rsi] - pxor xmm6,XMMWORD[rsp] + pxor xmm6,XMMWORD[rsp] ; is offloaded, remember? pxor xmm11,xmm12 pxor xmm2,xmm4 pxor xmm7,xmm0 @@ -858,7 +860,7 @@ $L$oop4x: movdqu xmm2,XMMWORD[96+rsi] movdqu XMMWORD[48+rdi],xmm7 movdqu xmm7,XMMWORD[112+rsi] - lea rsi,[128+rsi] + lea rsi,[128+rsi] ; size optimization pxor xmm6,XMMWORD[16+rsp] pxor xmm11,xmm13 pxor xmm2,xmm5 @@ -871,7 +873,7 @@ $L$oop4x: movdqu XMMWORD[96+rdi],xmm2 movdqu xmm2,XMMWORD[32+rsi] movdqu XMMWORD[112+rdi],xmm7 - lea rdi,[128+rdi] + lea rdi,[128+rdi] ; size optimization movdqu xmm7,XMMWORD[48+rsi] pxor xmm6,XMMWORD[32+rsp] pxor xmm11,xmm10 @@ -886,7 +888,7 @@ $L$oop4x: movdqu xmm2,XMMWORD[96+rsi] movdqu XMMWORD[48+rdi],xmm7 movdqu xmm7,XMMWORD[112+rsi] - lea rsi,[128+rsi] + lea rsi,[128+rsi] ; inp+=64*4 pxor xmm6,XMMWORD[48+rsp] pxor xmm11,xmm15 pxor xmm2,xmm9 @@ -895,7 +897,7 @@ $L$oop4x: movdqu XMMWORD[80+rdi],xmm11 movdqu XMMWORD[96+rdi],xmm2 movdqu XMMWORD[112+rdi],xmm7 - lea rdi,[128+rdi] + lea rdi,[128+rdi] ; out+=64*4 sub rdx,64*4 jnz NEAR $L$oop_outer4x @@ -910,9 +912,9 @@ $L$tail4x: cmp rdx,64 jae NEAR $L$64_or_more4x - +; movdqa 0x00(%rsp),%xmm6 # is offloaded, remember? xor r10,r10 - +; movdqa %xmm6,0x00(%rsp) movdqa XMMWORD[16+rsp],xmm12 movdqa XMMWORD[32+rsp],xmm4 movdqa XMMWORD[48+rsp],xmm0 @@ -920,11 +922,11 @@ $L$tail4x: ALIGN 32 $L$64_or_more4x: - movdqu xmm6,XMMWORD[rsi] + movdqu xmm6,XMMWORD[rsi] ; xor with input movdqu xmm11,XMMWORD[16+rsi] movdqu xmm2,XMMWORD[32+rsi] movdqu xmm7,XMMWORD[48+rsi] - pxor xmm6,XMMWORD[rsp] + pxor xmm6,XMMWORD[rsp] ; is offloaded, remember? pxor xmm11,xmm12 pxor xmm2,xmm4 pxor xmm7,xmm0 @@ -934,24 +936,24 @@ $L$64_or_more4x: movdqu XMMWORD[48+rdi],xmm7 je NEAR $L$done4x - movdqa xmm6,XMMWORD[16+rsp] - lea rsi,[64+rsi] + movdqa xmm6,XMMWORD[16+rsp] ; is offloaded, remember? + lea rsi,[64+rsi] ; inp+=64*1 xor r10,r10 movdqa XMMWORD[rsp],xmm6 movdqa XMMWORD[16+rsp],xmm13 - lea rdi,[64+rdi] + lea rdi,[64+rdi] ; out+=64*1 movdqa XMMWORD[32+rsp],xmm5 - sub rdx,64 + sub rdx,64 ; len-=64*1 movdqa XMMWORD[48+rsp],xmm1 jmp NEAR $L$oop_tail4x ALIGN 32 $L$128_or_more4x: - movdqu xmm6,XMMWORD[rsi] + movdqu xmm6,XMMWORD[rsi] ; xor with input movdqu xmm11,XMMWORD[16+rsi] movdqu xmm2,XMMWORD[32+rsi] movdqu xmm7,XMMWORD[48+rsi] - pxor xmm6,XMMWORD[rsp] + pxor xmm6,XMMWORD[rsp] ; is offloaded, remember? pxor xmm11,xmm12 pxor xmm2,xmm4 pxor xmm7,xmm0 @@ -974,24 +976,24 @@ $L$128_or_more4x: movdqu XMMWORD[112+rdi],xmm7 je NEAR $L$done4x - movdqa xmm6,XMMWORD[32+rsp] - lea rsi,[128+rsi] + movdqa xmm6,XMMWORD[32+rsp] ; is offloaded, remember? + lea rsi,[128+rsi] ; inp+=64*2 xor r10,r10 movdqa XMMWORD[rsp],xmm6 movdqa XMMWORD[16+rsp],xmm10 - lea rdi,[128+rdi] + lea rdi,[128+rdi] ; out+=64*2 movdqa XMMWORD[32+rsp],xmm14 - sub rdx,128 + sub rdx,128 ; len-=64*2 movdqa XMMWORD[48+rsp],xmm8 jmp NEAR $L$oop_tail4x ALIGN 32 $L$192_or_more4x: - movdqu xmm6,XMMWORD[rsi] + movdqu xmm6,XMMWORD[rsi] ; xor with input movdqu xmm11,XMMWORD[16+rsi] movdqu xmm2,XMMWORD[32+rsi] movdqu xmm7,XMMWORD[48+rsi] - pxor xmm6,XMMWORD[rsp] + pxor xmm6,XMMWORD[rsp] ; is offloaded, remember? pxor xmm11,xmm12 pxor xmm2,xmm4 pxor xmm7,xmm0 @@ -1004,7 +1006,7 @@ $L$192_or_more4x: movdqu xmm2,XMMWORD[96+rsi] movdqu XMMWORD[48+rdi],xmm7 movdqu xmm7,XMMWORD[112+rsi] - lea rsi,[128+rsi] + lea rsi,[128+rsi] ; size optimization pxor xmm6,XMMWORD[16+rsp] pxor xmm11,xmm13 pxor xmm2,xmm5 @@ -1017,7 +1019,7 @@ $L$192_or_more4x: movdqu XMMWORD[96+rdi],xmm2 movdqu xmm2,XMMWORD[32+rsi] movdqu XMMWORD[112+rdi],xmm7 - lea rdi,[128+rdi] + lea rdi,[128+rdi] ; size optimization movdqu xmm7,XMMWORD[48+rsi] pxor xmm6,XMMWORD[32+rsp] pxor xmm11,xmm10 @@ -1029,14 +1031,14 @@ $L$192_or_more4x: movdqu XMMWORD[48+rdi],xmm7 je NEAR $L$done4x - movdqa xmm6,XMMWORD[48+rsp] - lea rsi,[64+rsi] + movdqa xmm6,XMMWORD[48+rsp] ; is offloaded, remember? + lea rsi,[64+rsi] ; inp+=64*3 xor r10,r10 movdqa XMMWORD[rsp],xmm6 movdqa XMMWORD[16+rsp],xmm15 - lea rdi,[64+rdi] + lea rdi,[64+rdi] ; out+=64*3 movdqa XMMWORD[32+rsp],xmm9 - sub rdx,192 + sub rdx,192 ; len-=64*3 movdqa XMMWORD[48+rsp],xmm3 $L$oop_tail4x: @@ -1084,7 +1086,7 @@ $L$SEH_begin_ChaCha20_ctr32_avx2: _CET_ENDBR - mov r9,rsp + mov r9,rsp ; frame register sub rsp,0x280+168 and rsp,-32 @@ -1101,27 +1103,27 @@ _CET_ENDBR $L$8x_body: vzeroupper - - - - - - - - - - vbroadcasti128 ymm11,XMMWORD[$L$sigma] - vbroadcasti128 ymm3,XMMWORD[rcx] - vbroadcasti128 ymm15,XMMWORD[16+rcx] - vbroadcasti128 ymm7,XMMWORD[r8] - lea rcx,[256+rsp] - lea rax,[512+rsp] +; ############### stack layout +; +0x00 SIMD equivalent of %r12d +; ... +; +0x80 constant copy of key[0-2] smashed by lanes +; ... +; +0x200 SIMD counters (with nonce smashed by lanes) +; ... +; +0x280 + + vbroadcasti128 ymm11,XMMWORD[$L$sigma] ; key[0] + vbroadcasti128 ymm3,XMMWORD[rcx] ; key[1] + vbroadcasti128 ymm15,XMMWORD[16+rcx] ; key[2] + vbroadcasti128 ymm7,XMMWORD[r8] ; key[3] + lea rcx,[256+rsp] ; size optimization + lea rax,[512+rsp] ; size optimization lea r10,[$L$rot16] lea r11,[$L$rot24] - vpshufd ymm8,ymm11,0x00 + vpshufd ymm8,ymm11,0x00 ; smash key by lanes... vpshufd ymm9,ymm11,0x55 - vmovdqa YMMWORD[(128-256)+rcx],ymm8 + vmovdqa YMMWORD[(128-256)+rcx],ymm8 ; ... and offload vpshufd ymm10,ymm11,0xaa vmovdqa YMMWORD[(160-256)+rcx],ymm9 vpshufd ymm11,ymm11,0xff @@ -1137,18 +1139,18 @@ $L$8x_body: vmovdqa YMMWORD[(320-256)+rcx],ymm2 vmovdqa YMMWORD[(352-256)+rcx],ymm3 - vpshufd ymm12,ymm15,0x00 - vpshufd ymm13,ymm15,0x55 + vpshufd ymm12,ymm15,0x00 ; "xc0" + vpshufd ymm13,ymm15,0x55 ; "xc1" vmovdqa YMMWORD[(384-512)+rax],ymm12 - vpshufd ymm14,ymm15,0xaa + vpshufd ymm14,ymm15,0xaa ; "xc2" vmovdqa YMMWORD[(416-512)+rax],ymm13 - vpshufd ymm15,ymm15,0xff + vpshufd ymm15,ymm15,0xff ; "xc3" vmovdqa YMMWORD[(448-512)+rax],ymm14 vmovdqa YMMWORD[(480-512)+rax],ymm15 vpshufd ymm4,ymm7,0x00 vpshufd ymm5,ymm7,0x55 - vpaddd ymm4,ymm4,YMMWORD[$L$incy] + vpaddd ymm4,ymm4,YMMWORD[$L$incy] ; don't save counters yet vpshufd ymm6,ymm7,0xaa vmovdqa YMMWORD[(544-512)+rax],ymm5 vpshufd ymm7,ymm7,0xff @@ -1159,7 +1161,7 @@ $L$8x_body: ALIGN 32 $L$oop_outer8x: - vmovdqa ymm8,YMMWORD[((128-256))+rcx] + vmovdqa ymm8,YMMWORD[((128-256))+rcx] ; re-load smashed key vmovdqa ymm9,YMMWORD[((160-256))+rcx] vmovdqa ymm10,YMMWORD[((192-256))+rcx] vmovdqa ymm11,YMMWORD[((224-256))+rcx] @@ -1167,21 +1169,21 @@ $L$oop_outer8x: vmovdqa ymm1,YMMWORD[((288-256))+rcx] vmovdqa ymm2,YMMWORD[((320-256))+rcx] vmovdqa ymm3,YMMWORD[((352-256))+rcx] - vmovdqa ymm12,YMMWORD[((384-512))+rax] - vmovdqa ymm13,YMMWORD[((416-512))+rax] - vmovdqa ymm14,YMMWORD[((448-512))+rax] - vmovdqa ymm15,YMMWORD[((480-512))+rax] + vmovdqa ymm12,YMMWORD[((384-512))+rax] ; "xc0" + vmovdqa ymm13,YMMWORD[((416-512))+rax] ; "xc1" + vmovdqa ymm14,YMMWORD[((448-512))+rax] ; "xc2" + vmovdqa ymm15,YMMWORD[((480-512))+rax] ; "xc3" vmovdqa ymm4,YMMWORD[((512-512))+rax] vmovdqa ymm5,YMMWORD[((544-512))+rax] vmovdqa ymm6,YMMWORD[((576-512))+rax] vmovdqa ymm7,YMMWORD[((608-512))+rax] - vpaddd ymm4,ymm4,YMMWORD[$L$eight] + vpaddd ymm4,ymm4,YMMWORD[$L$eight] ; next SIMD counters $L$oop_enter8x: - vmovdqa YMMWORD[64+rsp],ymm14 - vmovdqa YMMWORD[96+rsp],ymm15 + vmovdqa YMMWORD[64+rsp],ymm14 ; SIMD equivalent of "%nox" + vmovdqa YMMWORD[96+rsp],ymm15 ; SIMD equivalent of "%nox" vbroadcasti128 ymm15,XMMWORD[r10] - vmovdqa YMMWORD[(512-512)+rax],ymm4 + vmovdqa YMMWORD[(512-512)+rax],ymm4 ; save SIMD counters mov eax,10 jmp NEAR $L$oop8x @@ -1334,20 +1336,20 @@ $L$oop8x: dec eax jnz NEAR $L$oop8x - lea rax,[512+rsp] - vpaddd ymm8,ymm8,YMMWORD[((128-256))+rcx] + lea rax,[512+rsp] ; size optimization + vpaddd ymm8,ymm8,YMMWORD[((128-256))+rcx] ; accumulate key vpaddd ymm9,ymm9,YMMWORD[((160-256))+rcx] vpaddd ymm10,ymm10,YMMWORD[((192-256))+rcx] vpaddd ymm11,ymm11,YMMWORD[((224-256))+rcx] - vpunpckldq ymm14,ymm8,ymm9 + vpunpckldq ymm14,ymm8,ymm9 ; "de-interlace" data vpunpckldq ymm15,ymm10,ymm11 vpunpckhdq ymm8,ymm8,ymm9 vpunpckhdq ymm10,ymm10,ymm11 - vpunpcklqdq ymm9,ymm14,ymm15 - vpunpckhqdq ymm14,ymm14,ymm15 - vpunpcklqdq ymm11,ymm8,ymm10 - vpunpckhqdq ymm8,ymm8,ymm10 + vpunpcklqdq ymm9,ymm14,ymm15 ; "a0" + vpunpckhqdq ymm14,ymm14,ymm15 ; "a1" + vpunpcklqdq ymm11,ymm8,ymm10 ; "a2" + vpunpckhqdq ymm8,ymm8,ymm10 ; "a3" vpaddd ymm0,ymm0,YMMWORD[((256-256))+rcx] vpaddd ymm1,ymm1,YMMWORD[((288-256))+rcx] vpaddd ymm2,ymm2,YMMWORD[((320-256))+rcx] @@ -1357,11 +1359,11 @@ $L$oop8x: vpunpckldq ymm15,ymm2,ymm3 vpunpckhdq ymm0,ymm0,ymm1 vpunpckhdq ymm2,ymm2,ymm3 - vpunpcklqdq ymm1,ymm10,ymm15 - vpunpckhqdq ymm10,ymm10,ymm15 - vpunpcklqdq ymm3,ymm0,ymm2 - vpunpckhqdq ymm0,ymm0,ymm2 - vperm2i128 ymm15,ymm9,ymm1,0x20 + vpunpcklqdq ymm1,ymm10,ymm15 ; "b0" + vpunpckhqdq ymm10,ymm10,ymm15 ; "b1" + vpunpcklqdq ymm3,ymm0,ymm2 ; "b2" + vpunpckhqdq ymm0,ymm0,ymm2 ; "b3" + vperm2i128 ymm15,ymm9,ymm1,0x20 ; "de-interlace" further vperm2i128 ymm1,ymm9,ymm1,0x31 vperm2i128 ymm9,ymm14,ymm10,0x20 vperm2i128 ymm10,ymm14,ymm10,0x31 @@ -1369,10 +1371,10 @@ $L$oop8x: vperm2i128 ymm3,ymm11,ymm3,0x31 vperm2i128 ymm11,ymm8,ymm0,0x20 vperm2i128 ymm0,ymm8,ymm0,0x31 - vmovdqa YMMWORD[rsp],ymm15 + vmovdqa YMMWORD[rsp],ymm15 ; offload vmovdqa YMMWORD[32+rsp],ymm9 - vmovdqa ymm15,YMMWORD[64+rsp] - vmovdqa ymm9,YMMWORD[96+rsp] + vmovdqa ymm15,YMMWORD[64+rsp] ; %ymm15 + vmovdqa ymm9,YMMWORD[96+rsp] ; %ymm9 vpaddd ymm12,ymm12,YMMWORD[((384-512))+rax] vpaddd ymm13,ymm13,YMMWORD[((416-512))+rax] @@ -1383,10 +1385,10 @@ $L$oop8x: vpunpckldq ymm8,ymm15,ymm9 vpunpckhdq ymm12,ymm12,ymm13 vpunpckhdq ymm15,ymm15,ymm9 - vpunpcklqdq ymm13,ymm2,ymm8 - vpunpckhqdq ymm2,ymm2,ymm8 - vpunpcklqdq ymm9,ymm12,ymm15 - vpunpckhqdq ymm12,ymm12,ymm15 + vpunpcklqdq ymm13,ymm2,ymm8 ; "c0" + vpunpckhqdq ymm2,ymm2,ymm8 ; "c1" + vpunpcklqdq ymm9,ymm12,ymm15 ; "c2" + vpunpckhqdq ymm12,ymm12,ymm15 ; "c3" vpaddd ymm4,ymm4,YMMWORD[((512-512))+rax] vpaddd ymm5,ymm5,YMMWORD[((544-512))+rax] vpaddd ymm6,ymm6,YMMWORD[((576-512))+rax] @@ -1396,11 +1398,11 @@ $L$oop8x: vpunpckldq ymm8,ymm6,ymm7 vpunpckhdq ymm4,ymm4,ymm5 vpunpckhdq ymm6,ymm6,ymm7 - vpunpcklqdq ymm5,ymm15,ymm8 - vpunpckhqdq ymm15,ymm15,ymm8 - vpunpcklqdq ymm7,ymm4,ymm6 - vpunpckhqdq ymm4,ymm4,ymm6 - vperm2i128 ymm8,ymm13,ymm5,0x20 + vpunpcklqdq ymm5,ymm15,ymm8 ; "d0" + vpunpckhqdq ymm15,ymm15,ymm8 ; "d1" + vpunpcklqdq ymm7,ymm4,ymm6 ; "d2" + vpunpckhqdq ymm4,ymm4,ymm6 ; "d3" + vperm2i128 ymm8,ymm13,ymm5,0x20 ; "de-interlace" further vperm2i128 ymm5,ymm13,ymm5,0x31 vperm2i128 ymm13,ymm2,ymm15,0x20 vperm2i128 ymm15,ymm2,ymm15,0x31 @@ -1408,55 +1410,55 @@ $L$oop8x: vperm2i128 ymm7,ymm9,ymm7,0x31 vperm2i128 ymm9,ymm12,ymm4,0x20 vperm2i128 ymm4,ymm12,ymm4,0x31 - vmovdqa ymm6,YMMWORD[rsp] + vmovdqa ymm6,YMMWORD[rsp] ; was offloaded, remember? vmovdqa ymm12,YMMWORD[32+rsp] cmp rdx,64*8 jb NEAR $L$tail8x - vpxor ymm6,ymm6,YMMWORD[rsi] + vpxor ymm6,ymm6,YMMWORD[rsi] ; xor with input vpxor ymm8,ymm8,YMMWORD[32+rsi] vpxor ymm1,ymm1,YMMWORD[64+rsi] vpxor ymm5,ymm5,YMMWORD[96+rsi] - lea rsi,[128+rsi] + lea rsi,[128+rsi] ; size optimization vmovdqu YMMWORD[rdi],ymm6 vmovdqu YMMWORD[32+rdi],ymm8 vmovdqu YMMWORD[64+rdi],ymm1 vmovdqu YMMWORD[96+rdi],ymm5 - lea rdi,[128+rdi] + lea rdi,[128+rdi] ; size optimization vpxor ymm12,ymm12,YMMWORD[rsi] vpxor ymm13,ymm13,YMMWORD[32+rsi] vpxor ymm10,ymm10,YMMWORD[64+rsi] vpxor ymm15,ymm15,YMMWORD[96+rsi] - lea rsi,[128+rsi] + lea rsi,[128+rsi] ; size optimization vmovdqu YMMWORD[rdi],ymm12 vmovdqu YMMWORD[32+rdi],ymm13 vmovdqu YMMWORD[64+rdi],ymm10 vmovdqu YMMWORD[96+rdi],ymm15 - lea rdi,[128+rdi] + lea rdi,[128+rdi] ; size optimization vpxor ymm14,ymm14,YMMWORD[rsi] vpxor ymm2,ymm2,YMMWORD[32+rsi] vpxor ymm3,ymm3,YMMWORD[64+rsi] vpxor ymm7,ymm7,YMMWORD[96+rsi] - lea rsi,[128+rsi] + lea rsi,[128+rsi] ; size optimization vmovdqu YMMWORD[rdi],ymm14 vmovdqu YMMWORD[32+rdi],ymm2 vmovdqu YMMWORD[64+rdi],ymm3 vmovdqu YMMWORD[96+rdi],ymm7 - lea rdi,[128+rdi] + lea rdi,[128+rdi] ; size optimization vpxor ymm11,ymm11,YMMWORD[rsi] vpxor ymm9,ymm9,YMMWORD[32+rsi] vpxor ymm0,ymm0,YMMWORD[64+rsi] vpxor ymm4,ymm4,YMMWORD[96+rsi] - lea rsi,[128+rsi] + lea rsi,[128+rsi] ; size optimization vmovdqu YMMWORD[rdi],ymm11 vmovdqu YMMWORD[32+rdi],ymm9 vmovdqu YMMWORD[64+rdi],ymm0 vmovdqu YMMWORD[96+rdi],ymm4 - lea rdi,[128+rdi] + lea rdi,[128+rdi] ; size optimization sub rdx,64*8 jnz NEAR $L$oop_outer8x @@ -1486,23 +1488,23 @@ $L$tail8x: ALIGN 32 $L$64_or_more8x: - vpxor ymm6,ymm6,YMMWORD[rsi] + vpxor ymm6,ymm6,YMMWORD[rsi] ; xor with input vpxor ymm8,ymm8,YMMWORD[32+rsi] vmovdqu YMMWORD[rdi],ymm6 vmovdqu YMMWORD[32+rdi],ymm8 je NEAR $L$done8x - lea rsi,[64+rsi] + lea rsi,[64+rsi] ; inp+=64*1 xor r10,r10 vmovdqa YMMWORD[rsp],ymm1 - lea rdi,[64+rdi] - sub rdx,64 + lea rdi,[64+rdi] ; out+=64*1 + sub rdx,64 ; len-=64*1 vmovdqa YMMWORD[32+rsp],ymm5 jmp NEAR $L$oop_tail8x ALIGN 32 $L$128_or_more8x: - vpxor ymm6,ymm6,YMMWORD[rsi] + vpxor ymm6,ymm6,YMMWORD[rsi] ; xor with input vpxor ymm8,ymm8,YMMWORD[32+rsi] vpxor ymm1,ymm1,YMMWORD[64+rsi] vpxor ymm5,ymm5,YMMWORD[96+rsi] @@ -1512,17 +1514,17 @@ $L$128_or_more8x: vmovdqu YMMWORD[96+rdi],ymm5 je NEAR $L$done8x - lea rsi,[128+rsi] + lea rsi,[128+rsi] ; inp+=64*2 xor r10,r10 vmovdqa YMMWORD[rsp],ymm12 - lea rdi,[128+rdi] - sub rdx,128 + lea rdi,[128+rdi] ; out+=64*2 + sub rdx,128 ; len-=64*2 vmovdqa YMMWORD[32+rsp],ymm13 jmp NEAR $L$oop_tail8x ALIGN 32 $L$192_or_more8x: - vpxor ymm6,ymm6,YMMWORD[rsi] + vpxor ymm6,ymm6,YMMWORD[rsi] ; xor with input vpxor ymm8,ymm8,YMMWORD[32+rsi] vpxor ymm1,ymm1,YMMWORD[64+rsi] vpxor ymm5,ymm5,YMMWORD[96+rsi] @@ -1536,17 +1538,17 @@ $L$192_or_more8x: vmovdqu YMMWORD[160+rdi],ymm13 je NEAR $L$done8x - lea rsi,[192+rsi] + lea rsi,[192+rsi] ; inp+=64*3 xor r10,r10 vmovdqa YMMWORD[rsp],ymm10 - lea rdi,[192+rdi] - sub rdx,192 + lea rdi,[192+rdi] ; out+=64*3 + sub rdx,192 ; len-=64*3 vmovdqa YMMWORD[32+rsp],ymm15 jmp NEAR $L$oop_tail8x ALIGN 32 $L$256_or_more8x: - vpxor ymm6,ymm6,YMMWORD[rsi] + vpxor ymm6,ymm6,YMMWORD[rsi] ; xor with input vpxor ymm8,ymm8,YMMWORD[32+rsi] vpxor ymm1,ymm1,YMMWORD[64+rsi] vpxor ymm5,ymm5,YMMWORD[96+rsi] @@ -1564,17 +1566,17 @@ $L$256_or_more8x: vmovdqu YMMWORD[224+rdi],ymm15 je NEAR $L$done8x - lea rsi,[256+rsi] + lea rsi,[256+rsi] ; inp+=64*4 xor r10,r10 vmovdqa YMMWORD[rsp],ymm14 - lea rdi,[256+rdi] - sub rdx,256 + lea rdi,[256+rdi] ; out+=64*4 + sub rdx,256 ; len-=64*4 vmovdqa YMMWORD[32+rsp],ymm2 jmp NEAR $L$oop_tail8x ALIGN 32 $L$320_or_more8x: - vpxor ymm6,ymm6,YMMWORD[rsi] + vpxor ymm6,ymm6,YMMWORD[rsi] ; xor with input vpxor ymm8,ymm8,YMMWORD[32+rsi] vpxor ymm1,ymm1,YMMWORD[64+rsi] vpxor ymm5,ymm5,YMMWORD[96+rsi] @@ -1596,17 +1598,17 @@ $L$320_or_more8x: vmovdqu YMMWORD[288+rdi],ymm2 je NEAR $L$done8x - lea rsi,[320+rsi] + lea rsi,[320+rsi] ; inp+=64*5 xor r10,r10 vmovdqa YMMWORD[rsp],ymm3 - lea rdi,[320+rdi] - sub rdx,320 + lea rdi,[320+rdi] ; out+=64*5 + sub rdx,320 ; len-=64*5 vmovdqa YMMWORD[32+rsp],ymm7 jmp NEAR $L$oop_tail8x ALIGN 32 $L$384_or_more8x: - vpxor ymm6,ymm6,YMMWORD[rsi] + vpxor ymm6,ymm6,YMMWORD[rsi] ; xor with input vpxor ymm8,ymm8,YMMWORD[32+rsi] vpxor ymm1,ymm1,YMMWORD[64+rsi] vpxor ymm5,ymm5,YMMWORD[96+rsi] @@ -1632,17 +1634,17 @@ $L$384_or_more8x: vmovdqu YMMWORD[352+rdi],ymm7 je NEAR $L$done8x - lea rsi,[384+rsi] + lea rsi,[384+rsi] ; inp+=64*6 xor r10,r10 vmovdqa YMMWORD[rsp],ymm11 - lea rdi,[384+rdi] - sub rdx,384 + lea rdi,[384+rdi] ; out+=64*6 + sub rdx,384 ; len-=64*6 vmovdqa YMMWORD[32+rsp],ymm9 jmp NEAR $L$oop_tail8x ALIGN 32 $L$448_or_more8x: - vpxor ymm6,ymm6,YMMWORD[rsi] + vpxor ymm6,ymm6,YMMWORD[rsi] ; xor with input vpxor ymm8,ymm8,YMMWORD[32+rsi] vpxor ymm1,ymm1,YMMWORD[64+rsi] vpxor ymm5,ymm5,YMMWORD[96+rsi] @@ -1672,11 +1674,11 @@ $L$448_or_more8x: vmovdqu YMMWORD[416+rdi],ymm9 je NEAR $L$done8x - lea rsi,[448+rsi] + lea rsi,[448+rsi] ; inp+=64*7 xor r10,r10 vmovdqa YMMWORD[rsp],ymm0 - lea rdi,[448+rdi] - sub rdx,448 + lea rdi,[448+rdi] ; out+=64*7 + sub rdx,448 ; len-=64*7 vmovdqa YMMWORD[32+rsp],ymm4 $L$oop_tail8x: @@ -1723,20 +1725,20 @@ se_handler: pushfq sub rsp,64 - mov rax,QWORD[120+r8] - mov rbx,QWORD[248+r8] + mov rax,QWORD[120+r8] ; pull context->Rax + mov rbx,QWORD[248+r8] ; pull context->Rip - mov rsi,QWORD[8+r9] - mov r11,QWORD[56+r9] + mov rsi,QWORD[8+r9] ; disp->ImageBase + mov r11,QWORD[56+r9] ; disp->HandlerData lea r10,[$L$ctr32_body] - cmp rbx,r10 + cmp rbx,r10 ; context->Rip<.Lprologue jb NEAR $L$common_seh_tail - mov rax,QWORD[152+r8] + mov rax,QWORD[152+r8] ; pull context->Rsp - lea r10,[$L$no_data] - cmp rbx,r10 + lea r10,[$L$no_data] ; epilogue label + cmp rbx,r10 ; context->Rip>=.Lepilogue jae NEAR $L$common_seh_tail lea rax,[((64+24+48))+rax] @@ -1747,40 +1749,40 @@ se_handler: mov r13,QWORD[((-32))+rax] mov r14,QWORD[((-40))+rax] mov r15,QWORD[((-48))+rax] - mov QWORD[144+r8],rbx - mov QWORD[160+r8],rbp - mov QWORD[216+r8],r12 - mov QWORD[224+r8],r13 - mov QWORD[232+r8],r14 - mov QWORD[240+r8],r15 + mov QWORD[144+r8],rbx ; restore context->Rbx + mov QWORD[160+r8],rbp ; restore context->Rbp + mov QWORD[216+r8],r12 ; restore context->R12 + mov QWORD[224+r8],r13 ; restore context->R13 + mov QWORD[232+r8],r14 ; restore context->R14 + mov QWORD[240+r8],r15 ; restore context->R14 $L$common_seh_tail: mov rdi,QWORD[8+rax] mov rsi,QWORD[16+rax] - mov QWORD[152+r8],rax - mov QWORD[168+r8],rsi - mov QWORD[176+r8],rdi + mov QWORD[152+r8],rax ; restore context->Rsp + mov QWORD[168+r8],rsi ; restore context->Rsi + mov QWORD[176+r8],rdi ; restore context->Rdi - mov rdi,QWORD[40+r9] - mov rsi,r8 - mov ecx,154 - DD 0xa548f3fc + mov rdi,QWORD[40+r9] ; disp->ContextRecord + mov rsi,r8 ; context + mov ecx,154 ; sizeof(CONTEXT) + DD 0xa548f3fc ; cld; rep movsq mov rsi,r9 - xor rcx,rcx - mov rdx,QWORD[8+rsi] - mov r8,QWORD[rsi] - mov r9,QWORD[16+rsi] - mov r10,QWORD[40+rsi] - lea r11,[56+rsi] - lea r12,[24+rsi] - mov QWORD[32+rsp],r10 - mov QWORD[40+rsp],r11 - mov QWORD[48+rsp],r12 - mov QWORD[56+rsp],rcx + xor rcx,rcx ; arg1, UNW_FLAG_NHANDLER + mov rdx,QWORD[8+rsi] ; arg2, disp->ImageBase + mov r8,QWORD[rsi] ; arg3, disp->ControlPc + mov r9,QWORD[16+rsi] ; arg4, disp->FunctionEntry + mov r10,QWORD[40+rsi] ; disp->ContextRecord + lea r11,[56+rsi] ; &disp->HandlerData + lea r12,[24+rsi] ; &disp->EstablisherFrame + mov QWORD[32+rsp],r10 ; arg5 + mov QWORD[40+rsp],r11 ; arg6 + mov QWORD[48+rsp],r12 ; arg7 + mov QWORD[56+rsp],rcx ; arg8, (NULL) call QWORD[__imp_RtlVirtualUnwind] - mov eax,1 + mov eax,1 ; ExceptionContinueSearch add rsp,64 popfq pop r15 @@ -1808,28 +1810,28 @@ ssse3_handler: pushfq sub rsp,64 - mov rax,QWORD[120+r8] - mov rbx,QWORD[248+r8] + mov rax,QWORD[120+r8] ; pull context->Rax + mov rbx,QWORD[248+r8] ; pull context->Rip - mov rsi,QWORD[8+r9] - mov r11,QWORD[56+r9] + mov rsi,QWORD[8+r9] ; disp->ImageBase + mov r11,QWORD[56+r9] ; disp->HandlerData - mov r10d,DWORD[r11] - lea r10,[r10*1+rsi] - cmp rbx,r10 + mov r10d,DWORD[r11] ; HandlerData[0] + lea r10,[r10*1+rsi] ; prologue label + cmp rbx,r10 ; context->RipR9 - mov r10d,DWORD[4+r11] - lea r10,[r10*1+rsi] - cmp rbx,r10 + mov r10d,DWORD[4+r11] ; HandlerData[1] + lea r10,[r10*1+rsi] ; epilogue label + cmp rbx,r10 ; context->Rip>=epilogue label jae NEAR $L$common_seh_tail lea rsi,[((-40))+rax] - lea rdi,[512+r8] + lea rdi,[512+r8] ; &context.Xmm6 mov ecx,4 - DD 0xa548f3fc + DD 0xa548f3fc ; cld; rep movsq jmp NEAR $L$common_seh_tail @@ -1848,28 +1850,28 @@ full_handler: pushfq sub rsp,64 - mov rax,QWORD[120+r8] - mov rbx,QWORD[248+r8] + mov rax,QWORD[120+r8] ; pull context->Rax + mov rbx,QWORD[248+r8] ; pull context->Rip - mov rsi,QWORD[8+r9] - mov r11,QWORD[56+r9] + mov rsi,QWORD[8+r9] ; disp->ImageBase + mov r11,QWORD[56+r9] ; disp->HandlerData - mov r10d,DWORD[r11] - lea r10,[r10*1+rsi] - cmp rbx,r10 + mov r10d,DWORD[r11] ; HandlerData[0] + lea r10,[r10*1+rsi] ; prologue label + cmp rbx,r10 ; context->RipR9 - mov r10d,DWORD[4+r11] - lea r10,[r10*1+rsi] - cmp rbx,r10 + mov r10d,DWORD[4+r11] ; HandlerData[1] + lea r10,[r10*1+rsi] ; epilogue label + cmp rbx,r10 ; context->Rip>=epilogue label jae NEAR $L$common_seh_tail lea rsi,[((-168))+rax] - lea rdi,[512+r8] + lea rdi,[512+r8] ; &context.Xmm6 mov ecx,20 - DD 0xa548f3fc + DD 0xa548f3fc ; cld; rep movsq jmp NEAR $L$common_seh_tail @@ -1908,7 +1910,7 @@ $L$SEH_info_ChaCha20_ctr32_ssse3_4x: $L$SEH_info_ChaCha20_ctr32_avx2: DB 9,0,0,0 DD full_handler wrt ..imagebase - DD $L$8x_body wrt ..imagebase,$L$8x_epilogue wrt ..imagebase + DD $L$8x_body wrt ..imagebase,$L$8x_epilogue wrt ..imagebase ; HandlerData[] %else ; Work around https://bugzilla.nasm.us/show_bug.cgi?id=3392738 ret diff --git a/third_party/boringssl/gen/crypto/chacha20_poly1305_armv8-apple.S b/third_party/boringssl/gen/crypto/chacha20_poly1305_armv8-apple.S index d3a8c95b6..a01db47bb 100644 --- a/third_party/boringssl/gen/crypto/chacha20_poly1305_armv8-apple.S +++ b/third_party/boringssl/gen/crypto/chacha20_poly1305_armv8-apple.S @@ -7,6 +7,7 @@ .section __TEXT,__const .align 7 +chacha20_poly1305_constants: Lchacha20_consts: .byte 'e','x','p','a','n','d',' ','3','2','-','b','y','t','e',' ','k' Linc: @@ -20,6 +21,7 @@ Lclamp: .align 6 +chacha20_poly1305_helpers: Lpoly_hash_ad_internal: .cfi_startproc cbnz x4, Lpoly_hash_intro @@ -118,6 +120,7 @@ Lpoly_hash_ad_ret: .private_extern _chacha20_poly1305_seal .align 6 +.alt_entry _chacha20_poly1305_seal _chacha20_poly1305_seal: AARCH64_SIGN_LINK_REGISTER .cfi_startproc @@ -1474,6 +1477,7 @@ Lseal_128_rounds: .private_extern _chacha20_poly1305_open .align 6 +.alt_entry _chacha20_poly1305_open _chacha20_poly1305_open: AARCH64_SIGN_LINK_REGISTER .cfi_startproc diff --git a/third_party/boringssl/gen/crypto/chacha20_poly1305_armv8-linux.S b/third_party/boringssl/gen/crypto/chacha20_poly1305_armv8-linux.S index 0ab662fb5..bf8df2d2a 100644 --- a/third_party/boringssl/gen/crypto/chacha20_poly1305_armv8-linux.S +++ b/third_party/boringssl/gen/crypto/chacha20_poly1305_armv8-linux.S @@ -7,6 +7,7 @@ .section .rodata .align 7 +chacha20_poly1305_constants: .Lchacha20_consts: .byte 'e','x','p','a','n','d',' ','3','2','-','b','y','t','e',' ','k' .Linc: @@ -20,6 +21,7 @@ .type .Lpoly_hash_ad_internal,%function .align 6 +chacha20_poly1305_helpers: .Lpoly_hash_ad_internal: .cfi_startproc cbnz x4, .Lpoly_hash_intro diff --git a/third_party/boringssl/gen/crypto/chacha20_poly1305_armv8-win.S b/third_party/boringssl/gen/crypto/chacha20_poly1305_armv8-win.S index fa86675f3..00af636af 100644 --- a/third_party/boringssl/gen/crypto/chacha20_poly1305_armv8-win.S +++ b/third_party/boringssl/gen/crypto/chacha20_poly1305_armv8-win.S @@ -7,6 +7,7 @@ .section .rodata .align 7 +chacha20_poly1305_constants: Lchacha20_consts: .byte 'e','x','p','a','n','d',' ','3','2','-','b','y','t','e',' ','k' Linc: @@ -22,6 +23,7 @@ Lclamp: .type 32 .endef .align 6 +chacha20_poly1305_helpers: Lpoly_hash_ad_internal: .cfi_startproc cbnz x4, Lpoly_hash_intro diff --git a/third_party/boringssl/gen/crypto/chacha20_poly1305_x86_64-apple.S b/third_party/boringssl/gen/crypto/chacha20_poly1305_x86_64-apple.S index 4044212e3..dbfa00e76 100644 --- a/third_party/boringssl/gen/crypto/chacha20_poly1305_x86_64-apple.S +++ b/third_party/boringssl/gen/crypto/chacha20_poly1305_x86_64-apple.S @@ -56,7 +56,7 @@ poly_hash_ad_internal: cmpq $13,%r8 jne L$hash_ad_loop L$poly_fast_tls_ad: - +// Special treatment for the TLS case of 13 bytes movq (%rcx),%r10 movq 5(%rcx),%r11 shrq $24,%r11 @@ -87,21 +87,21 @@ L$poly_fast_tls_ad: movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 ret L$hash_ad_loop: - +// Hash in 16 byte chunk cmpq $16,%r8 jb L$hash_ad_tail addq 0+0(%rcx),%r10 @@ -133,14 +133,14 @@ L$hash_ad_loop: movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -151,7 +151,7 @@ L$hash_ad_loop: L$hash_ad_tail: cmpq $0,%r8 je L$hash_ad_done - +// Hash last < 16 byte tail xorq %r13,%r13 xorq %r14,%r14 xorq %r15,%r15 @@ -194,19 +194,19 @@ L$hash_ad_tail_loop: movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 - +// Finished AD L$hash_ad_done: ret @@ -216,6 +216,7 @@ L$hash_ad_done: .private_extern _chacha20_poly1305_open_sse41 .p2align 6 +.alt_entry _chacha20_poly1305_open_sse41 _chacha20_poly1305_open_sse41: _CET_ENDBR @@ -231,8 +232,8 @@ _CET_ENDBR pushq %r15 - - +// We write the calculated authenticator back to keyp at the end, so save +// the pointer on the stack too. pushq %r9 subq $288 + 0 + 32,%rsp @@ -247,14 +248,14 @@ _CET_ENDBR cmpq $128,%rbx jbe L$open_sse_128 - +// For long buffers, prepare the poly key first movdqa L$chacha20_consts(%rip),%xmm0 movdqu 0(%r9),%xmm4 movdqu 16(%r9),%xmm8 movdqu 32(%r9),%xmm12 movdqa %xmm12,%xmm7 - +// Store on stack, to free keyp movdqa %xmm4,0+48(%rbp) movdqa %xmm8,0+64(%rbp) movdqa %xmm12,0+96(%rbp) @@ -305,20 +306,20 @@ L$open_sse_init_rounds: decq %r10 jne L$open_sse_init_rounds - +// A0|B0 hold the Poly1305 32-byte key, C0,D0 can be discarded paddd L$chacha20_consts(%rip),%xmm0 paddd 0+48(%rbp),%xmm4 - +// Clamp and store the key pand L$clamp(%rip),%xmm0 movdqa %xmm0,0+0(%rbp) movdqa %xmm4,0+16(%rbp) - +// Hash movq %r8,%r8 call poly_hash_ad_internal L$open_sse_main_loop: cmpq $256,%rbx jb L$open_sse_tail - +// Load state, increment counter blocks movdqa L$chacha20_consts(%rip),%xmm0 movdqa 0+48(%rbp),%xmm4 movdqa 0+64(%rbp),%xmm8 @@ -344,8 +345,8 @@ L$open_sse_main_loop: movdqa %xmm14,0+128(%rbp) movdqa %xmm15,0+144(%rbp) - - +// There are 10 ChaCha20 iterations of 2QR each, so for 6 iterations we +// hash 2 blocks, and for the remaining 4 only 1 block - for a total of 16 movq $4,%rcx movq %rsi,%r8 L$open_sse_main_loop_rounds: @@ -480,14 +481,14 @@ L$open_sse_main_loop_rounds: movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -607,14 +608,14 @@ L$open_sse_main_loop_rounds: movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -693,7 +694,7 @@ L$open_sse_main_loop_rounds: subq $256,%rbx jmp L$open_sse_main_loop L$open_sse_tail: - +// Handle the various tail sizes efficiently testq %rbx,%rbx jz L$open_sse_finalize cmpq $192,%rbx @@ -743,14 +744,14 @@ L$open_sse_tail_64_rounds_and_x1hash: movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -811,7 +812,7 @@ L$open_sse_tail_64_rounds: paddd 0+96(%rbp),%xmm12 jmp L$open_sse_tail_64_dec_loop - +// ############################################################################# L$open_sse_tail_128: movdqa L$chacha20_consts(%rip),%xmm0 movdqa 0+48(%rbp),%xmm4 @@ -859,14 +860,14 @@ L$open_sse_tail_128_rounds_and_x1hash: movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -987,7 +988,7 @@ L$open_sse_tail_128_rounds: leaq 64(%rsi),%rsi leaq 64(%rdi),%rdi jmp L$open_sse_tail_64_dec_loop - +// ############################################################################# L$open_sse_tail_192: movdqa L$chacha20_consts(%rip),%xmm0 movdqa 0+48(%rbp),%xmm4 @@ -1044,14 +1045,14 @@ L$open_sse_tail_192_rounds_and_x1hash: movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -1220,14 +1221,14 @@ L$open_sse_tail_192_rounds: movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -1263,14 +1264,14 @@ L$open_sse_tail_192_rounds: movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -1317,7 +1318,7 @@ L$open_sse_tail_192_finish: leaq 128(%rsi),%rsi leaq 128(%rdi),%rdi jmp L$open_sse_tail_64_dec_loop - +// ############################################################################# L$open_sse_tail_256: movdqa L$chacha20_consts(%rip),%xmm0 movdqa 0+48(%rbp),%xmm4 @@ -1528,14 +1529,14 @@ L$open_sse_tail_256_rounds_and_x1hash: movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -1599,14 +1600,14 @@ L$open_sse_tail_256_hash: movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -1672,8 +1673,8 @@ L$open_sse_tail_256_hash: subq $192,%rbx leaq 192(%rsi),%rsi leaq 192(%rdi),%rdi - - +// ############################################################################# +// Decrypt the remaining data, 16B at a time, using existing stream L$open_sse_tail_64_dec_loop: cmpq $16,%rbx jb L$open_sse_tail_16_init @@ -1690,13 +1691,13 @@ L$open_sse_tail_64_dec_loop: L$open_sse_tail_16_init: movdqa %xmm0,%xmm1 - +// Decrypt up to 16 bytes at the end. L$open_sse_tail_16: testq %rbx,%rbx jz L$open_sse_finalize - - +// Read the final bytes into %xmm3. They need to be read in reverse order so +// that they end up in the correct order in %xmm3. pxor %xmm3,%xmm3 leaq -1(%rsi,%rbx,1),%rsi movq %rbx,%r8 @@ -1709,10 +1710,10 @@ L$open_sse_tail_16_compose: movq %xmm3,%r13 pextrq $1,%xmm3,%r14 - +// The final bytes of keystream are in %xmm1. pxor %xmm1,%xmm3 - +// Copy the plaintext bytes out. L$open_sse_tail_16_extract: pextrb $0,%xmm3,(%rdi) psrldq $1,%xmm3 @@ -1749,14 +1750,14 @@ L$open_sse_tail_16_extract: movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -1792,19 +1793,19 @@ L$open_sse_finalize: movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 - +// Final reduce movq %r10,%r13 movq %r11,%r14 movq %r12,%r15 @@ -1814,14 +1815,14 @@ L$open_sse_finalize: cmovcq %r13,%r10 cmovcq %r14,%r11 cmovcq %r15,%r12 - +// Add in s part of the key addq 0+0+16(%rbp),%r10 adcq 8+0+16(%rbp),%r11 addq $288 + 0 + 32,%rsp - +// The tag replaces the key on return popq %r9 movq %r10,(%r9) @@ -1839,7 +1840,7 @@ L$open_sse_finalize: popq %rbp ret - +// ############################################################################# L$open_sse_128: movdqu L$chacha20_consts(%rip),%xmm0 @@ -2002,11 +2003,11 @@ L$open_sse_128_rounds: paddd %xmm15,%xmm13 paddd L$sse_inc(%rip),%xmm15 paddd %xmm15,%xmm14 - +// Clamp and store the key pand L$clamp(%rip),%xmm0 movdqa %xmm0,0+0(%rbp) movdqa %xmm4,0+16(%rbp) - +// Hash movq %r8,%r8 call poly_hash_ad_internal L$open_sse_128_xor_hash: @@ -2017,7 +2018,7 @@ L$open_sse_128_xor_hash: adcq 8+0(%rsi),%r11 adcq $1,%r12 - +// Load for decryption movdqu 0(%rsi),%xmm3 pxor %xmm3,%xmm1 movdqu %xmm1,0(%rdi) @@ -2049,19 +2050,19 @@ L$open_sse_128_xor_hash: movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 - +// Shift the stream left movdqa %xmm5,%xmm1 movdqa %xmm9,%xmm5 movdqa %xmm13,%xmm9 @@ -2073,16 +2074,17 @@ L$open_sse_128_xor_hash: - - - - - - +// ############################################################################## +// ############################################################################## +// void chacha20_poly1305_seal(uint8_t *out_ciphertext, const uint8_t *plaintext, +// size_t plaintext_len, const uint8_t *ad, +// size_t ad_len, +// union chacha20_poly1305_seal_data *data); .globl _chacha20_poly1305_seal_sse41 .private_extern _chacha20_poly1305_seal_sse41 .p2align 6 +.alt_entry _chacha20_poly1305_seal_sse41 _chacha20_poly1305_seal_sse41: _CET_ENDBR @@ -2098,8 +2100,8 @@ _CET_ENDBR pushq %r15 - - +// We write the calculated authenticator back to keyp at the end, so save +// the pointer on the stack too. pushq %r9 subq $288 + 0 + 32,%rsp @@ -2107,7 +2109,7 @@ _CET_ENDBR leaq 32(%rsp),%rbp andq $-32,%rbp - movq 56(%r9),%rbx + movq 56(%r9),%rbx // extra_in_len addq %rdx,%rbx movq %r8,0+0+32(%rbp) movq %rbx,8+0+32(%rbp) @@ -2115,7 +2117,7 @@ _CET_ENDBR cmpq $128,%rbx jbe L$seal_sse_128 - +// For longer buffers, prepare the poly key + some stream movdqa L$chacha20_consts(%rip),%xmm0 movdqu 0(%r9),%xmm4 movdqu 16(%r9),%xmm8 @@ -2136,7 +2138,7 @@ _CET_ENDBR paddd L$sse_inc(%rip),%xmm12 movdqa %xmm12,%xmm13 paddd L$sse_inc(%rip),%xmm12 - +// Store on stack movdqa %xmm4,0+48(%rbp) movdqa %xmm8,0+64(%rbp) movdqa %xmm12,0+96(%rbp) @@ -2349,11 +2351,11 @@ L$seal_sse_init_rounds: paddd 0+64(%rbp),%xmm8 paddd 0+96(%rbp),%xmm12 - +// Clamp and store the key pand L$clamp(%rip),%xmm3 movdqa %xmm3,0+0(%rbp) movdqa %xmm7,0+16(%rbp) - +// Hash movq %r8,%r8 call poly_hash_ad_internal movdqu 0 + 0(%rsi),%xmm3 @@ -2570,14 +2572,14 @@ L$seal_sse_main_rounds: movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -2698,14 +2700,14 @@ L$seal_sse_main_rounds: movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -2806,7 +2808,7 @@ L$seal_sse_main_loop_xor: ja L$seal_sse_tail_192 cmpq $64,%rbx ja L$seal_sse_tail_128 - +// ############################################################################# L$seal_sse_tail_64: movdqa L$chacha20_consts(%rip),%xmm0 movdqa 0+48(%rbp),%xmm4 @@ -2845,14 +2847,14 @@ L$seal_sse_tail_64_rounds_and_x2hash: movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -2930,14 +2932,14 @@ L$seal_sse_tail_64_rounds_and_x1hash: movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -2953,7 +2955,7 @@ L$seal_sse_tail_64_rounds_and_x1hash: paddd 0+96(%rbp),%xmm12 jmp L$seal_sse_128_tail_xor - +// ############################################################################# L$seal_sse_tail_128: movdqa L$chacha20_consts(%rip),%xmm0 movdqa 0+48(%rbp),%xmm4 @@ -2998,14 +3000,14 @@ L$seal_sse_tail_128_rounds_and_x2hash: movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -3083,14 +3085,14 @@ L$seal_sse_tail_128_rounds_and_x1hash: movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -3167,7 +3169,7 @@ L$seal_sse_tail_128_rounds_and_x1hash: subq $64,%rbx leaq 64(%rsi),%rsi jmp L$seal_sse_128_tail_hash - +// ############################################################################# L$seal_sse_tail_192: movdqa L$chacha20_consts(%rip),%xmm0 movdqa 0+48(%rbp),%xmm4 @@ -3218,14 +3220,14 @@ L$seal_sse_tail_192_rounds_and_x2hash: movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -3324,14 +3326,14 @@ L$seal_sse_tail_192_rounds_and_x1hash: movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -3444,7 +3446,7 @@ L$seal_sse_tail_192_rounds_and_x1hash: movq $128,%rcx subq $128,%rbx leaq 128(%rsi),%rsi - +// ############################################################################# L$seal_sse_128_tail_hash: cmpq $16,%rcx jb L$seal_sse_128_tail_xor @@ -3477,14 +3479,14 @@ L$seal_sse_128_tail_hash: movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -3497,11 +3499,11 @@ L$seal_sse_128_tail_xor: cmpq $16,%rbx jb L$seal_sse_tail_16 subq $16,%rbx - +// Load for decryption movdqu 0(%rsi),%xmm3 pxor %xmm3,%xmm0 movdqu %xmm0,0(%rdi) - +// Then hash addq 0(%rdi),%r10 adcq 8(%rdi),%r11 adcq $1,%r12 @@ -3533,19 +3535,19 @@ L$seal_sse_128_tail_xor: movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 - +// Shift the stream left movdqa %xmm4,%xmm0 movdqa %xmm8,%xmm4 movdqa %xmm12,%xmm8 @@ -3558,7 +3560,7 @@ L$seal_sse_128_tail_xor: L$seal_sse_tail_16: testq %rbx,%rbx jz L$process_blocks_of_extra_in - +// We can only load the PT one byte at a time to avoid buffer overread movq %rbx,%r8 movq %rbx,%rcx leaq -1(%rsi,%rbx,1),%rsi @@ -3570,10 +3572,10 @@ L$seal_sse_tail_16_compose: decq %rcx jne L$seal_sse_tail_16_compose - +// XOR the keystream with the plaintext. pxor %xmm0,%xmm15 - +// Write ciphertext out, byte-by-byte. movq %rbx,%rcx movdqu %xmm15,%xmm0 L$seal_sse_tail_16_extract: @@ -3583,42 +3585,42 @@ L$seal_sse_tail_16_extract: subq $1,%rcx jnz L$seal_sse_tail_16_extract - - - - - - - +// %xmm15 contains the final (partial, non-empty) block of ciphertext which +// needs to be fed into the Poly1305 state. The right-most %rbx bytes of it +// are valid. We need to fill it with extra_in bytes until full, or until we +// run out of bytes. +// +// %r9 points to the tag output, which is actually a struct with the +// extra_in pointer and length at offset 48. movq 288 + 0 + 32(%rsp),%r9 - movq 56(%r9),%r14 - movq 48(%r9),%r13 + movq 56(%r9),%r14 // extra_in_len + movq 48(%r9),%r13 // extra_in testq %r14,%r14 - jz L$process_partial_block + jz L$process_partial_block // Common case: no bytes of extra_in movq $16,%r15 - subq %rbx,%r15 - cmpq %r15,%r14 - + subq %rbx,%r15 // 16-%rbx is the number of bytes that fit into %xmm15. + cmpq %r15,%r14 // if extra_in_len < 16-%rbx, only copy extra_in_len +// (note that AT&T syntax reverses the arguments) jge L$load_extra_in movq %r14,%r15 L$load_extra_in: - - +// %r15 contains the number of bytes of extra_in (pointed to by %r13) to load +// into %xmm15. They are loaded in reverse order. leaq -1(%r13,%r15,1),%rsi - - +// Update extra_in and extra_in_len to reflect the bytes that are about to +// be read. addq %r15,%r13 subq %r15,%r14 movq %r13,48(%r9) movq %r14,56(%r9) - - +// Update %r8, which is used to select the mask later on, to reflect the +// extra bytes about to be added. addq %r15,%r8 - +// Load %r15 bytes of extra_in into %xmm11. pxor %xmm11,%xmm11 L$load_extra_load_loop: pslldq $1,%xmm11 @@ -3627,9 +3629,9 @@ L$load_extra_load_loop: subq $1,%r15 jnz L$load_extra_load_loop - - - +// Shift %xmm11 up the length of the remainder from the main encryption. Sadly, +// the shift for an XMM register has to be a constant, thus we loop to do +// this. movq %rbx,%r15 L$load_extra_shift_loop: @@ -3637,18 +3639,18 @@ L$load_extra_shift_loop: subq $1,%r15 jnz L$load_extra_shift_loop - - - +// Mask %xmm15 (the remainder from the main encryption) so that superfluous +// bytes are zero. This means that the non-zero bytes in %xmm11 and %xmm15 are +// disjoint and so we can merge them with an OR. leaq L$and_masks(%rip),%r15 shlq $4,%rbx pand -16(%r15,%rbx,1),%xmm15 - +// Merge %xmm11 into %xmm15, forming the remainder block. por %xmm11,%xmm15 - - +// The block of ciphertext + extra_in is ready to be included in the +// Poly1305 state. movq %xmm15,%r13 pextrq $1,%xmm15,%r14 addq %r13,%r10 @@ -3680,26 +3682,26 @@ L$load_extra_shift_loop: movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 L$process_blocks_of_extra_in: - +// There may be additional bytes of extra_in to process. movq 288+32+0 (%rsp),%r9 - movq 48(%r9),%rsi - movq 56(%r9),%r8 + movq 48(%r9),%rsi // extra_in + movq 56(%r9),%r8 // extra_in_len movq %r8,%rcx - shrq $4,%r8 + shrq $4,%r8 // number of blocks L$process_extra_hash_loop: jz process_extra_in_trailer @@ -3732,14 +3734,14 @@ L$process_extra_hash_loop: movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -3747,8 +3749,9 @@ L$process_extra_hash_loop: leaq 16(%rsi),%rsi subq $1,%r8 jmp L$process_extra_hash_loop +.alt_entry process_extra_in_trailer process_extra_in_trailer: - andq $15,%rcx + andq $15,%rcx // remaining num bytes (<16) of extra_in movq %rcx,%rbx jz L$do_length_block leaq -1(%rsi,%rcx,1),%rsi @@ -3761,7 +3764,7 @@ L$process_extra_in_trailer_load: jnz L$process_extra_in_trailer_load L$process_partial_block: - +// %xmm15 contains %rbx bytes of data to be fed into Poly1305. %rbx leaq L$and_masks(%rip),%r15 shlq $4,%rbx pand -16(%r15,%rbx,1),%xmm15 @@ -3796,14 +3799,14 @@ L$process_partial_block: movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -3839,19 +3842,19 @@ L$do_length_block: movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 - +// Final reduce movq %r10,%r13 movq %r11,%r14 movq %r12,%r15 @@ -3861,14 +3864,14 @@ L$do_length_block: cmovcq %r13,%r10 cmovcq %r14,%r11 cmovcq %r15,%r12 - +// Add in s part of the key addq 0+0+16(%rbp),%r10 adcq 8+0+16(%rbp),%r11 addq $288 + 0 + 32,%rsp - +// The tag replaces the key on return popq %r9 movq %r10,(%r9) @@ -3886,7 +3889,7 @@ L$do_length_block: popq %rbp ret - +// ############################################################################## L$seal_sse_128: movdqu L$chacha20_consts(%rip),%xmm0 @@ -4049,22 +4052,23 @@ L$seal_sse_128_rounds: paddd %xmm15,%xmm12 paddd L$sse_inc(%rip),%xmm15 paddd %xmm15,%xmm13 - +// Clamp and store the key pand L$clamp(%rip),%xmm2 movdqa %xmm2,0+0(%rbp) movdqa %xmm6,0+16(%rbp) - +// Hash movq %r8,%r8 call poly_hash_ad_internal jmp L$seal_sse_128_tail_xor - +// ############################################################################# .globl _chacha20_poly1305_open_avx2 .private_extern _chacha20_poly1305_open_avx2 .p2align 6 +.alt_entry _chacha20_poly1305_open_avx2 _chacha20_poly1305_open_avx2: _CET_ENDBR @@ -4080,8 +4084,8 @@ _CET_ENDBR pushq %r15 - - +// We write the calculated authenticator back to keyp at the end, so save +// the pointer on the stack too. pushq %r9 subq $288 + 0 + 32,%rsp @@ -4157,16 +4161,16 @@ L$open_avx2_init_rounds: vpaddd 0+160(%rbp),%ymm12,%ymm12 vperm2i128 $0x02,%ymm0,%ymm4,%ymm3 - +// Clamp and store key vpand L$clamp(%rip),%ymm3,%ymm3 vmovdqa %ymm3,0+0(%rbp) - +// Stream for the first 64 bytes vperm2i128 $0x13,%ymm0,%ymm4,%ymm0 vperm2i128 $0x13,%ymm8,%ymm12,%ymm4 - +// Hash AD + first 64 bytes movq %r8,%r8 call poly_hash_ad_internal - +// Hash first 64 bytes xorq %rcx,%rcx L$open_avx2_init_hash: addq 0+0(%rsi,%rcx,1),%r10 @@ -4198,14 +4202,14 @@ L$open_avx2_init_hash: movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -4213,17 +4217,17 @@ L$open_avx2_init_hash: addq $16,%rcx cmpq $64,%rcx jne L$open_avx2_init_hash - +// Decrypt first 64 bytes vpxor 0(%rsi),%ymm0,%ymm0 vpxor 32(%rsi),%ymm4,%ymm4 - +// Store first 64 bytes of decrypted data vmovdqu %ymm0,0(%rdi) vmovdqu %ymm4,32(%rdi) leaq 64(%rsi),%rsi leaq 64(%rdi),%rdi subq $64,%rbx L$open_avx2_main_loop: - +// Hash and decrypt 512 bytes each iteration cmpq $512,%rbx jb L$open_avx2_main_loop_done vmovdqa L$chacha20_consts(%rip),%ymm0 @@ -4313,14 +4317,14 @@ L$open_avx2_main_loop_rounds: movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -4408,14 +4412,14 @@ L$open_avx2_main_loop_rounds: movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -4503,14 +4507,14 @@ L$open_avx2_main_loop_rounds: movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -4579,14 +4583,14 @@ L$open_avx2_main_loop_rounds: movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -4643,14 +4647,14 @@ L$open_avx2_main_loop_rounds: movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -4693,7 +4697,7 @@ L$open_avx2_main_loop_done: movq %rbx,%rcx andq $-16,%rcx testq %rcx,%rcx - je L$open_avx2_tail_128_rounds + je L$open_avx2_tail_128_rounds // Have nothing to hash L$open_avx2_tail_128_rounds_and_x1hash: addq 0+0(%rsi,%r8,1),%r10 adcq 8+0(%rsi,%r8,1),%r11 @@ -4724,14 +4728,14 @@ L$open_avx2_tail_128_rounds_and_x1hash: movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -4792,7 +4796,7 @@ L$open_avx2_tail_128_rounds: vmovdqa %ymm3,%ymm8 jmp L$open_avx2_tail_128_xor - +// ############################################################################# L$open_avx2_tail_256: vmovdqa L$chacha20_consts(%rip),%ymm0 vmovdqa 0+64(%rbp),%ymm4 @@ -4838,14 +4842,14 @@ L$open_avx2_tail_256_rounds_and_x1hash: movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -4984,14 +4988,14 @@ L$open_avx2_tail_256_hash: movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -5029,7 +5033,7 @@ L$open_avx2_tail_256_done: leaq 128(%rdi),%rdi subq $128,%rbx jmp L$open_avx2_tail_128_xor - +// ############################################################################# L$open_avx2_tail_384: vmovdqa L$chacha20_consts(%rip),%ymm0 vmovdqa 0+64(%rbp),%ymm4 @@ -5081,14 +5085,14 @@ L$open_avx2_tail_384_rounds_and_x2hash: movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -5181,14 +5185,14 @@ L$open_avx2_tail_384_rounds_and_x1hash: movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -5287,14 +5291,14 @@ L$open_avx2_384_tail_hash: movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -5348,7 +5352,7 @@ L$open_avx2_384_tail_done: leaq 256(%rdi),%rdi subq $256,%rbx jmp L$open_avx2_tail_128_xor - +// ############################################################################# L$open_avx2_tail_512: vmovdqa L$chacha20_consts(%rip),%ymm0 vmovdqa 0+64(%rbp),%ymm4 @@ -5404,14 +5408,14 @@ L$open_avx2_tail_512_rounds_and_x2hash: movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -5477,14 +5481,14 @@ L$open_avx2_tail_512_rounds_and_x1hash: movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -5558,14 +5562,14 @@ L$open_avx2_tail_512_rounds_and_x1hash: movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -5684,14 +5688,14 @@ L$open_avx2_tail_512_hash: movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -5782,7 +5786,7 @@ L$open_avx2_tail_32_xor: vmovdqa %xmm0,%xmm1 jb L$open_avx2_exit subq $16,%rbx - +// load for decryption vpxor (%rsi),%xmm0,%xmm1 vmovdqu %xmm1,(%rdi) leaq 16(%rsi),%rsi @@ -5792,7 +5796,7 @@ L$open_avx2_tail_32_xor: L$open_avx2_exit: vzeroupper jmp L$open_sse_tail_16 - +// ############################################################################# L$open_avx2_192: vmovdqa %ymm0,%ymm1 vmovdqa %ymm0,%ymm2 @@ -5893,10 +5897,10 @@ L$open_avx2_192_rounds: vpaddd %ymm11,%ymm12,%ymm12 vpaddd %ymm15,%ymm13,%ymm13 vperm2i128 $0x02,%ymm0,%ymm4,%ymm3 - +// Clamp and store the key vpand L$clamp(%rip),%ymm3,%ymm3 vmovdqa %ymm3,0+0(%rbp) - +// Stream for up to 192 bytes vperm2i128 $0x13,%ymm0,%ymm4,%ymm0 vperm2i128 $0x13,%ymm8,%ymm12,%ymm4 vperm2i128 $0x02,%ymm1,%ymm5,%ymm8 @@ -5939,14 +5943,14 @@ L$open_avx2_short_hash_and_xor_loop: movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -5979,24 +5983,24 @@ L$open_avx2_short_hash_and_xor_loop: movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 - +// Load + decrypt vpxor (%rsi),%ymm0,%ymm0 vmovdqu %ymm0,(%rdi) leaq 32(%rsi),%rsi leaq 32(%rdi),%rdi - +// Shift stream vmovdqa %ymm4,%ymm0 vmovdqa %ymm8,%ymm4 vmovdqa %ymm12,%ymm8 @@ -6041,14 +6045,14 @@ L$open_avx2_short_tail_32: movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -6061,7 +6065,7 @@ L$open_avx2_short_tail_32: L$open_avx2_short_tail_32_exit: vzeroupper jmp L$open_sse_tail_16 - +// ############################################################################# L$open_avx2_320: vmovdqa %ymm0,%ymm1 vmovdqa %ymm0,%ymm2 @@ -6208,10 +6212,10 @@ L$open_avx2_320_rounds: vpaddd 0+192(%rbp),%ymm13,%ymm13 vpaddd 0+224(%rbp),%ymm14,%ymm14 vperm2i128 $0x02,%ymm0,%ymm4,%ymm3 - +// Clamp and store the key vpand L$clamp(%rip),%ymm3,%ymm3 vmovdqa %ymm3,0+0(%rbp) - +// Stream for up to 320 bytes vperm2i128 $0x13,%ymm0,%ymm4,%ymm0 vperm2i128 $0x13,%ymm8,%ymm12,%ymm4 vperm2i128 $0x02,%ymm1,%ymm5,%ymm8 @@ -6225,12 +6229,13 @@ L$open_avx2_320_rounds: jmp L$open_avx2_short - - +// ############################################################################# +// ############################################################################# .globl _chacha20_poly1305_seal_avx2 .private_extern _chacha20_poly1305_seal_avx2 .p2align 6 +.alt_entry _chacha20_poly1305_seal_avx2 _chacha20_poly1305_seal_avx2: _CET_ENDBR @@ -6246,8 +6251,8 @@ _CET_ENDBR pushq %r15 - - +// We write the calculated authenticator back to keyp at the end, so save +// the pointer on the stack too. pushq %r9 subq $288 + 0 + 32,%rsp @@ -6255,7 +6260,7 @@ _CET_ENDBR leaq 32(%rsp),%rbp andq $-32,%rbp - movq 56(%r9),%rbx + movq 56(%r9),%rbx // extra_in_len addq %rdx,%rbx movq %r8,0+0+32(%rbp) movq %rbx,8+0+32(%rbp) @@ -6483,7 +6488,7 @@ L$seal_avx2_init_rounds: vmovdqa %ymm15,0+0(%rbp) movq %r8,%r8 call poly_hash_ad_internal - +// Safely store 320 bytes (otherwise would handle with optimized call) vpxor 0(%rsi),%ymm3,%ymm3 vpxor 32(%rsi),%ymm11,%ymm11 vmovdqu %ymm3,0(%rdi) @@ -6863,14 +6868,14 @@ L$seal_avx2_main_loop_rounds: movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -6960,14 +6965,14 @@ L$seal_avx2_main_loop_rounds_entry: movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -7055,14 +7060,14 @@ L$seal_avx2_main_loop_rounds_entry: movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -7110,14 +7115,14 @@ L$seal_avx2_main_loop_rounds_entry: movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -7143,14 +7148,14 @@ L$seal_avx2_main_loop_rounds_entry: movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -7234,14 +7239,14 @@ L$seal_avx2_main_loop_rounds_entry: movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -7267,14 +7272,14 @@ L$seal_avx2_main_loop_rounds_entry: movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -7289,7 +7294,7 @@ L$seal_avx2_main_loop_rounds_entry: ja L$seal_avx2_tail_384 cmpq $128,%rbx ja L$seal_avx2_tail_256 - +// ############################################################################# L$seal_avx2_tail_128: vmovdqa L$chacha20_consts(%rip),%ymm0 vmovdqa 0+64(%rbp),%ymm4 @@ -7321,14 +7326,14 @@ L$seal_avx2_tail_128_rounds_and_3xhash: movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -7376,14 +7381,14 @@ L$seal_avx2_tail_128_rounds_and_2xhash: movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -7428,14 +7433,14 @@ L$seal_avx2_tail_128_rounds_and_2xhash: movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -7456,7 +7461,7 @@ L$seal_avx2_tail_128_rounds_and_2xhash: vmovdqa %ymm3,%ymm8 jmp L$seal_avx2_short_loop - +// ############################################################################# L$seal_avx2_tail_256: vmovdqa L$chacha20_consts(%rip),%ymm0 vmovdqa 0+64(%rbp),%ymm4 @@ -7500,14 +7505,14 @@ L$seal_avx2_tail_256_rounds_and_3xhash: movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -7581,14 +7586,14 @@ L$seal_avx2_tail_256_rounds_and_2xhash: movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -7659,14 +7664,14 @@ L$seal_avx2_tail_256_rounds_and_2xhash: movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -7706,7 +7711,7 @@ L$seal_avx2_tail_256_rounds_and_2xhash: leaq 128(%rsi),%rsi subq $128,%rbx jmp L$seal_avx2_short_hash_remainder - +// ############################################################################# L$seal_avx2_tail_384: vmovdqa L$chacha20_consts(%rip),%ymm0 vmovdqa 0+64(%rbp),%ymm4 @@ -7755,14 +7760,14 @@ L$seal_avx2_tail_384_rounds_and_3xhash: movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -7836,14 +7841,14 @@ L$seal_avx2_tail_384_rounds_and_2xhash: movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -7914,14 +7919,14 @@ L$seal_avx2_tail_384_rounds_and_2xhash: movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -8015,7 +8020,7 @@ L$seal_avx2_tail_384_rounds_and_2xhash: leaq 256(%rsi),%rsi subq $256,%rbx jmp L$seal_avx2_short_hash_remainder - +// ############################################################################# L$seal_avx2_tail_512: vmovdqa L$chacha20_consts(%rip),%ymm0 vmovdqa 0+64(%rbp),%ymm4 @@ -8062,14 +8067,14 @@ L$seal_avx2_tail_512_rounds_and_3xhash: movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -8198,14 +8203,14 @@ L$seal_avx2_tail_512_rounds_and_2xhash: movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -8331,14 +8336,14 @@ L$seal_avx2_tail_512_rounds_and_2xhash: movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -8414,7 +8419,7 @@ L$seal_avx2_tail_512_rounds_and_2xhash: leaq 384(%rsi),%rsi subq $384,%rbx jmp L$seal_avx2_short_hash_remainder - +// ############################################################################## L$seal_avx2_320: vmovdqa %ymm0,%ymm1 vmovdqa %ymm0,%ymm2 @@ -8561,10 +8566,10 @@ L$seal_avx2_320_rounds: vpaddd 0+192(%rbp),%ymm13,%ymm13 vpaddd 0+224(%rbp),%ymm14,%ymm14 vperm2i128 $0x02,%ymm0,%ymm4,%ymm3 - +// Clamp and store the key vpand L$clamp(%rip),%ymm3,%ymm3 vmovdqa %ymm3,0+0(%rbp) - +// Stream for up to 320 bytes vperm2i128 $0x13,%ymm0,%ymm4,%ymm0 vperm2i128 $0x13,%ymm8,%ymm12,%ymm4 vperm2i128 $0x02,%ymm1,%ymm5,%ymm8 @@ -8576,7 +8581,7 @@ L$seal_avx2_320_rounds: vperm2i128 $0x13,%ymm2,%ymm6,%ymm2 vperm2i128 $0x13,%ymm10,%ymm14,%ymm6 jmp L$seal_avx2_short - +// ############################################################################## L$seal_avx2_192: vmovdqa %ymm0,%ymm1 vmovdqa %ymm0,%ymm2 @@ -8677,10 +8682,10 @@ L$seal_avx2_192_rounds: vpaddd %ymm11,%ymm12,%ymm12 vpaddd %ymm15,%ymm13,%ymm13 vperm2i128 $0x02,%ymm0,%ymm4,%ymm3 - +// Clamp and store the key vpand L$clamp(%rip),%ymm3,%ymm3 vmovdqa %ymm3,0+0(%rbp) - +// Stream for up to 192 bytes vperm2i128 $0x13,%ymm0,%ymm4,%ymm0 vperm2i128 $0x13,%ymm8,%ymm12,%ymm4 vperm2i128 $0x02,%ymm1,%ymm5,%ymm8 @@ -8723,14 +8728,14 @@ L$seal_avx2_short_hash_remainder: movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -8742,11 +8747,11 @@ L$seal_avx2_short_loop: cmpq $32,%rbx jb L$seal_avx2_short_tail subq $32,%rbx - +// Encrypt vpxor (%rsi),%ymm0,%ymm0 vmovdqu %ymm0,(%rdi) leaq 32(%rsi),%rsi - +// Load + hash addq 0+0(%rdi),%r10 adcq 8+0(%rdi),%r11 adcq $1,%r12 @@ -8776,14 +8781,14 @@ L$seal_avx2_short_loop: movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -8816,20 +8821,20 @@ L$seal_avx2_short_loop: movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 leaq 32(%rdi),%rdi - +// Shift stream vmovdqa %ymm4,%ymm0 vmovdqa %ymm8,%ymm4 vmovdqa %ymm12,%ymm8 @@ -8876,14 +8881,14 @@ L$seal_avx2_short_tail: movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 diff --git a/third_party/boringssl/gen/crypto/chacha20_poly1305_x86_64-linux.S b/third_party/boringssl/gen/crypto/chacha20_poly1305_x86_64-linux.S index 6fd94c840..9f45a2170 100644 --- a/third_party/boringssl/gen/crypto/chacha20_poly1305_x86_64-linux.S +++ b/third_party/boringssl/gen/crypto/chacha20_poly1305_x86_64-linux.S @@ -56,7 +56,7 @@ poly_hash_ad_internal: cmpq $13,%r8 jne .Lhash_ad_loop .Lpoly_fast_tls_ad: - +// Special treatment for the TLS case of 13 bytes movq (%rcx),%r10 movq 5(%rcx),%r11 shrq $24,%r11 @@ -87,21 +87,21 @@ poly_hash_ad_internal: movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 ret .Lhash_ad_loop: - +// Hash in 16 byte chunk cmpq $16,%r8 jb .Lhash_ad_tail addq 0+0(%rcx),%r10 @@ -133,14 +133,14 @@ poly_hash_ad_internal: movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -151,7 +151,7 @@ poly_hash_ad_internal: .Lhash_ad_tail: cmpq $0,%r8 je .Lhash_ad_done - +// Hash last < 16 byte tail xorq %r13,%r13 xorq %r14,%r14 xorq %r15,%r15 @@ -194,19 +194,19 @@ poly_hash_ad_internal: movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 - +// Finished AD .Lhash_ad_done: ret .cfi_endproc @@ -237,8 +237,8 @@ _CET_ENDBR pushq %r15 .cfi_adjust_cfa_offset 8 .cfi_offset %r15,-56 - - +// We write the calculated authenticator back to keyp at the end, so save +// the pointer on the stack too. pushq %r9 .cfi_adjust_cfa_offset 8 .cfi_offset %r9,-64 @@ -254,14 +254,14 @@ _CET_ENDBR cmpq $128,%rbx jbe .Lopen_sse_128 - +// For long buffers, prepare the poly key first movdqa .Lchacha20_consts(%rip),%xmm0 movdqu 0(%r9),%xmm4 movdqu 16(%r9),%xmm8 movdqu 32(%r9),%xmm12 movdqa %xmm12,%xmm7 - +// Store on stack, to free keyp movdqa %xmm4,0+48(%rbp) movdqa %xmm8,0+64(%rbp) movdqa %xmm12,0+96(%rbp) @@ -312,20 +312,20 @@ _CET_ENDBR decq %r10 jne .Lopen_sse_init_rounds - +// A0|B0 hold the Poly1305 32-byte key, C0,D0 can be discarded paddd .Lchacha20_consts(%rip),%xmm0 paddd 0+48(%rbp),%xmm4 - +// Clamp and store the key pand .Lclamp(%rip),%xmm0 movdqa %xmm0,0+0(%rbp) movdqa %xmm4,0+16(%rbp) - +// Hash movq %r8,%r8 call poly_hash_ad_internal .Lopen_sse_main_loop: cmpq $256,%rbx jb .Lopen_sse_tail - +// Load state, increment counter blocks movdqa .Lchacha20_consts(%rip),%xmm0 movdqa 0+48(%rbp),%xmm4 movdqa 0+64(%rbp),%xmm8 @@ -351,8 +351,8 @@ _CET_ENDBR movdqa %xmm14,0+128(%rbp) movdqa %xmm15,0+144(%rbp) - - +// There are 10 ChaCha20 iterations of 2QR each, so for 6 iterations we +// hash 2 blocks, and for the remaining 4 only 1 block - for a total of 16 movq $4,%rcx movq %rsi,%r8 .Lopen_sse_main_loop_rounds: @@ -487,14 +487,14 @@ _CET_ENDBR movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -614,14 +614,14 @@ _CET_ENDBR movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -700,7 +700,7 @@ _CET_ENDBR subq $256,%rbx jmp .Lopen_sse_main_loop .Lopen_sse_tail: - +// Handle the various tail sizes efficiently testq %rbx,%rbx jz .Lopen_sse_finalize cmpq $192,%rbx @@ -750,14 +750,14 @@ _CET_ENDBR movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -818,7 +818,7 @@ _CET_ENDBR paddd 0+96(%rbp),%xmm12 jmp .Lopen_sse_tail_64_dec_loop - +// ############################################################################# .Lopen_sse_tail_128: movdqa .Lchacha20_consts(%rip),%xmm0 movdqa 0+48(%rbp),%xmm4 @@ -866,14 +866,14 @@ _CET_ENDBR movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -994,7 +994,7 @@ _CET_ENDBR leaq 64(%rsi),%rsi leaq 64(%rdi),%rdi jmp .Lopen_sse_tail_64_dec_loop - +// ############################################################################# .Lopen_sse_tail_192: movdqa .Lchacha20_consts(%rip),%xmm0 movdqa 0+48(%rbp),%xmm4 @@ -1051,14 +1051,14 @@ _CET_ENDBR movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -1227,14 +1227,14 @@ _CET_ENDBR movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -1270,14 +1270,14 @@ _CET_ENDBR movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -1324,7 +1324,7 @@ _CET_ENDBR leaq 128(%rsi),%rsi leaq 128(%rdi),%rdi jmp .Lopen_sse_tail_64_dec_loop - +// ############################################################################# .Lopen_sse_tail_256: movdqa .Lchacha20_consts(%rip),%xmm0 movdqa 0+48(%rbp),%xmm4 @@ -1535,14 +1535,14 @@ _CET_ENDBR movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -1606,14 +1606,14 @@ _CET_ENDBR movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -1679,8 +1679,8 @@ _CET_ENDBR subq $192,%rbx leaq 192(%rsi),%rsi leaq 192(%rdi),%rdi - - +// ############################################################################# +// Decrypt the remaining data, 16B at a time, using existing stream .Lopen_sse_tail_64_dec_loop: cmpq $16,%rbx jb .Lopen_sse_tail_16_init @@ -1697,13 +1697,13 @@ _CET_ENDBR .Lopen_sse_tail_16_init: movdqa %xmm0,%xmm1 - +// Decrypt up to 16 bytes at the end. .Lopen_sse_tail_16: testq %rbx,%rbx jz .Lopen_sse_finalize - - +// Read the final bytes into %xmm3. They need to be read in reverse order so +// that they end up in the correct order in %xmm3. pxor %xmm3,%xmm3 leaq -1(%rsi,%rbx,1),%rsi movq %rbx,%r8 @@ -1716,10 +1716,10 @@ _CET_ENDBR movq %xmm3,%r13 pextrq $1,%xmm3,%r14 - +// The final bytes of keystream are in %xmm1. pxor %xmm1,%xmm3 - +// Copy the plaintext bytes out. .Lopen_sse_tail_16_extract: pextrb $0,%xmm3,(%rdi) psrldq $1,%xmm3 @@ -1756,14 +1756,14 @@ _CET_ENDBR movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -1799,19 +1799,19 @@ _CET_ENDBR movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 - +// Final reduce movq %r10,%r13 movq %r11,%r14 movq %r12,%r15 @@ -1821,14 +1821,14 @@ _CET_ENDBR cmovcq %r13,%r10 cmovcq %r14,%r11 cmovcq %r15,%r12 - +// Add in s part of the key addq 0+0+16(%rbp),%r10 adcq 8+0+16(%rbp),%r11 .cfi_remember_state addq $288 + 0 + 32,%rsp .cfi_adjust_cfa_offset -(288 + 32) - +// The tag replaces the key on return popq %r9 .cfi_adjust_cfa_offset -8 .cfi_restore %r9 @@ -1853,7 +1853,7 @@ _CET_ENDBR .cfi_adjust_cfa_offset -8 .cfi_restore %rbp ret - +// ############################################################################# .Lopen_sse_128: .cfi_restore_state movdqu .Lchacha20_consts(%rip),%xmm0 @@ -2016,11 +2016,11 @@ _CET_ENDBR paddd %xmm15,%xmm13 paddd .Lsse_inc(%rip),%xmm15 paddd %xmm15,%xmm14 - +// Clamp and store the key pand .Lclamp(%rip),%xmm0 movdqa %xmm0,0+0(%rbp) movdqa %xmm4,0+16(%rbp) - +// Hash movq %r8,%r8 call poly_hash_ad_internal .Lopen_sse_128_xor_hash: @@ -2031,7 +2031,7 @@ _CET_ENDBR adcq 8+0(%rsi),%r11 adcq $1,%r12 - +// Load for decryption movdqu 0(%rsi),%xmm3 pxor %xmm3,%xmm1 movdqu %xmm1,0(%rdi) @@ -2063,19 +2063,19 @@ _CET_ENDBR movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 - +// Shift the stream left movdqa %xmm5,%xmm1 movdqa %xmm9,%xmm5 movdqa %xmm13,%xmm9 @@ -2087,12 +2087,12 @@ _CET_ENDBR .size chacha20_poly1305_open_sse41, .-chacha20_poly1305_open_sse41 .cfi_endproc - - - - - - +// ############################################################################## +// ############################################################################## +// void chacha20_poly1305_seal(uint8_t *out_ciphertext, const uint8_t *plaintext, +// size_t plaintext_len, const uint8_t *ad, +// size_t ad_len, +// union chacha20_poly1305_seal_data *data); .globl chacha20_poly1305_seal_sse41 .hidden chacha20_poly1305_seal_sse41 .type chacha20_poly1305_seal_sse41,@function @@ -2118,8 +2118,8 @@ _CET_ENDBR pushq %r15 .cfi_adjust_cfa_offset 8 .cfi_offset %r15,-56 - - +// We write the calculated authenticator back to keyp at the end, so save +// the pointer on the stack too. pushq %r9 .cfi_adjust_cfa_offset 8 .cfi_offset %r9,-64 @@ -2128,7 +2128,7 @@ _CET_ENDBR leaq 32(%rsp),%rbp andq $-32,%rbp - movq 56(%r9),%rbx + movq 56(%r9),%rbx // extra_in_len addq %rdx,%rbx movq %r8,0+0+32(%rbp) movq %rbx,8+0+32(%rbp) @@ -2136,7 +2136,7 @@ _CET_ENDBR cmpq $128,%rbx jbe .Lseal_sse_128 - +// For longer buffers, prepare the poly key + some stream movdqa .Lchacha20_consts(%rip),%xmm0 movdqu 0(%r9),%xmm4 movdqu 16(%r9),%xmm8 @@ -2157,7 +2157,7 @@ _CET_ENDBR paddd .Lsse_inc(%rip),%xmm12 movdqa %xmm12,%xmm13 paddd .Lsse_inc(%rip),%xmm12 - +// Store on stack movdqa %xmm4,0+48(%rbp) movdqa %xmm8,0+64(%rbp) movdqa %xmm12,0+96(%rbp) @@ -2370,11 +2370,11 @@ _CET_ENDBR paddd 0+64(%rbp),%xmm8 paddd 0+96(%rbp),%xmm12 - +// Clamp and store the key pand .Lclamp(%rip),%xmm3 movdqa %xmm3,0+0(%rbp) movdqa %xmm7,0+16(%rbp) - +// Hash movq %r8,%r8 call poly_hash_ad_internal movdqu 0 + 0(%rsi),%xmm3 @@ -2591,14 +2591,14 @@ _CET_ENDBR movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -2719,14 +2719,14 @@ _CET_ENDBR movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -2827,7 +2827,7 @@ _CET_ENDBR ja .Lseal_sse_tail_192 cmpq $64,%rbx ja .Lseal_sse_tail_128 - +// ############################################################################# .Lseal_sse_tail_64: movdqa .Lchacha20_consts(%rip),%xmm0 movdqa 0+48(%rbp),%xmm4 @@ -2866,14 +2866,14 @@ _CET_ENDBR movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -2951,14 +2951,14 @@ _CET_ENDBR movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -2974,7 +2974,7 @@ _CET_ENDBR paddd 0+96(%rbp),%xmm12 jmp .Lseal_sse_128_tail_xor - +// ############################################################################# .Lseal_sse_tail_128: movdqa .Lchacha20_consts(%rip),%xmm0 movdqa 0+48(%rbp),%xmm4 @@ -3019,14 +3019,14 @@ _CET_ENDBR movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -3104,14 +3104,14 @@ _CET_ENDBR movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -3188,7 +3188,7 @@ _CET_ENDBR subq $64,%rbx leaq 64(%rsi),%rsi jmp .Lseal_sse_128_tail_hash - +// ############################################################################# .Lseal_sse_tail_192: movdqa .Lchacha20_consts(%rip),%xmm0 movdqa 0+48(%rbp),%xmm4 @@ -3239,14 +3239,14 @@ _CET_ENDBR movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -3345,14 +3345,14 @@ _CET_ENDBR movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -3465,7 +3465,7 @@ _CET_ENDBR movq $128,%rcx subq $128,%rbx leaq 128(%rsi),%rsi - +// ############################################################################# .Lseal_sse_128_tail_hash: cmpq $16,%rcx jb .Lseal_sse_128_tail_xor @@ -3498,14 +3498,14 @@ _CET_ENDBR movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -3518,11 +3518,11 @@ _CET_ENDBR cmpq $16,%rbx jb .Lseal_sse_tail_16 subq $16,%rbx - +// Load for decryption movdqu 0(%rsi),%xmm3 pxor %xmm3,%xmm0 movdqu %xmm0,0(%rdi) - +// Then hash addq 0(%rdi),%r10 adcq 8(%rdi),%r11 adcq $1,%r12 @@ -3554,19 +3554,19 @@ _CET_ENDBR movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 - +// Shift the stream left movdqa %xmm4,%xmm0 movdqa %xmm8,%xmm4 movdqa %xmm12,%xmm8 @@ -3579,7 +3579,7 @@ _CET_ENDBR .Lseal_sse_tail_16: testq %rbx,%rbx jz .Lprocess_blocks_of_extra_in - +// We can only load the PT one byte at a time to avoid buffer overread movq %rbx,%r8 movq %rbx,%rcx leaq -1(%rsi,%rbx,1),%rsi @@ -3591,10 +3591,10 @@ _CET_ENDBR decq %rcx jne .Lseal_sse_tail_16_compose - +// XOR the keystream with the plaintext. pxor %xmm0,%xmm15 - +// Write ciphertext out, byte-by-byte. movq %rbx,%rcx movdqu %xmm15,%xmm0 .Lseal_sse_tail_16_extract: @@ -3604,42 +3604,42 @@ _CET_ENDBR subq $1,%rcx jnz .Lseal_sse_tail_16_extract - - - - - - - +// %xmm15 contains the final (partial, non-empty) block of ciphertext which +// needs to be fed into the Poly1305 state. The right-most %rbx bytes of it +// are valid. We need to fill it with extra_in bytes until full, or until we +// run out of bytes. +// +// %r9 points to the tag output, which is actually a struct with the +// extra_in pointer and length at offset 48. movq 288 + 0 + 32(%rsp),%r9 - movq 56(%r9),%r14 - movq 48(%r9),%r13 + movq 56(%r9),%r14 // extra_in_len + movq 48(%r9),%r13 // extra_in testq %r14,%r14 - jz .Lprocess_partial_block + jz .Lprocess_partial_block // Common case: no bytes of extra_in movq $16,%r15 - subq %rbx,%r15 - cmpq %r15,%r14 - + subq %rbx,%r15 // 16-%rbx is the number of bytes that fit into %xmm15. + cmpq %r15,%r14 // if extra_in_len < 16-%rbx, only copy extra_in_len +// (note that AT&T syntax reverses the arguments) jge .Lload_extra_in movq %r14,%r15 .Lload_extra_in: - - +// %r15 contains the number of bytes of extra_in (pointed to by %r13) to load +// into %xmm15. They are loaded in reverse order. leaq -1(%r13,%r15,1),%rsi - - +// Update extra_in and extra_in_len to reflect the bytes that are about to +// be read. addq %r15,%r13 subq %r15,%r14 movq %r13,48(%r9) movq %r14,56(%r9) - - +// Update %r8, which is used to select the mask later on, to reflect the +// extra bytes about to be added. addq %r15,%r8 - +// Load %r15 bytes of extra_in into %xmm11. pxor %xmm11,%xmm11 .Lload_extra_load_loop: pslldq $1,%xmm11 @@ -3648,9 +3648,9 @@ _CET_ENDBR subq $1,%r15 jnz .Lload_extra_load_loop - - - +// Shift %xmm11 up the length of the remainder from the main encryption. Sadly, +// the shift for an XMM register has to be a constant, thus we loop to do +// this. movq %rbx,%r15 .Lload_extra_shift_loop: @@ -3658,18 +3658,18 @@ _CET_ENDBR subq $1,%r15 jnz .Lload_extra_shift_loop - - - +// Mask %xmm15 (the remainder from the main encryption) so that superfluous +// bytes are zero. This means that the non-zero bytes in %xmm11 and %xmm15 are +// disjoint and so we can merge them with an OR. leaq .Land_masks(%rip),%r15 shlq $4,%rbx pand -16(%r15,%rbx,1),%xmm15 - +// Merge %xmm11 into %xmm15, forming the remainder block. por %xmm11,%xmm15 - - +// The block of ciphertext + extra_in is ready to be included in the +// Poly1305 state. movq %xmm15,%r13 pextrq $1,%xmm15,%r14 addq %r13,%r10 @@ -3701,26 +3701,26 @@ _CET_ENDBR movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 .Lprocess_blocks_of_extra_in: - +// There may be additional bytes of extra_in to process. movq 288+32+0 (%rsp),%r9 - movq 48(%r9),%rsi - movq 56(%r9),%r8 + movq 48(%r9),%rsi // extra_in + movq 56(%r9),%r8 // extra_in_len movq %r8,%rcx - shrq $4,%r8 + shrq $4,%r8 // number of blocks .Lprocess_extra_hash_loop: jz process_extra_in_trailer @@ -3753,14 +3753,14 @@ _CET_ENDBR movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -3769,7 +3769,7 @@ _CET_ENDBR subq $1,%r8 jmp .Lprocess_extra_hash_loop process_extra_in_trailer: - andq $15,%rcx + andq $15,%rcx // remaining num bytes (<16) of extra_in movq %rcx,%rbx jz .Ldo_length_block leaq -1(%rsi,%rcx,1),%rsi @@ -3782,7 +3782,7 @@ process_extra_in_trailer: jnz .Lprocess_extra_in_trailer_load .Lprocess_partial_block: - +// %xmm15 contains %rbx bytes of data to be fed into Poly1305. %rbx leaq .Land_masks(%rip),%r15 shlq $4,%rbx pand -16(%r15,%rbx,1),%xmm15 @@ -3817,14 +3817,14 @@ process_extra_in_trailer: movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -3860,19 +3860,19 @@ process_extra_in_trailer: movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 - +// Final reduce movq %r10,%r13 movq %r11,%r14 movq %r12,%r15 @@ -3882,14 +3882,14 @@ process_extra_in_trailer: cmovcq %r13,%r10 cmovcq %r14,%r11 cmovcq %r15,%r12 - +// Add in s part of the key addq 0+0+16(%rbp),%r10 adcq 8+0+16(%rbp),%r11 .cfi_remember_state addq $288 + 0 + 32,%rsp .cfi_adjust_cfa_offset -(288 + 32) - +// The tag replaces the key on return popq %r9 .cfi_adjust_cfa_offset -8 .cfi_restore %r9 @@ -3914,7 +3914,7 @@ process_extra_in_trailer: .cfi_adjust_cfa_offset -8 .cfi_restore %rbp ret - +// ############################################################################## .Lseal_sse_128: .cfi_restore_state movdqu .Lchacha20_consts(%rip),%xmm0 @@ -4077,18 +4077,18 @@ process_extra_in_trailer: paddd %xmm15,%xmm12 paddd .Lsse_inc(%rip),%xmm15 paddd %xmm15,%xmm13 - +// Clamp and store the key pand .Lclamp(%rip),%xmm2 movdqa %xmm2,0+0(%rbp) movdqa %xmm6,0+16(%rbp) - +// Hash movq %r8,%r8 call poly_hash_ad_internal jmp .Lseal_sse_128_tail_xor .size chacha20_poly1305_seal_sse41, .-chacha20_poly1305_seal_sse41 .cfi_endproc - +// ############################################################################# .globl chacha20_poly1305_open_avx2 .hidden chacha20_poly1305_open_avx2 .type chacha20_poly1305_open_avx2,@function @@ -4114,8 +4114,8 @@ _CET_ENDBR pushq %r15 .cfi_adjust_cfa_offset 8 .cfi_offset %r15,-56 - - +// We write the calculated authenticator back to keyp at the end, so save +// the pointer on the stack too. pushq %r9 .cfi_adjust_cfa_offset 8 .cfi_offset %r9,-64 @@ -4192,16 +4192,16 @@ _CET_ENDBR vpaddd 0+160(%rbp),%ymm12,%ymm12 vperm2i128 $0x02,%ymm0,%ymm4,%ymm3 - +// Clamp and store key vpand .Lclamp(%rip),%ymm3,%ymm3 vmovdqa %ymm3,0+0(%rbp) - +// Stream for the first 64 bytes vperm2i128 $0x13,%ymm0,%ymm4,%ymm0 vperm2i128 $0x13,%ymm8,%ymm12,%ymm4 - +// Hash AD + first 64 bytes movq %r8,%r8 call poly_hash_ad_internal - +// Hash first 64 bytes xorq %rcx,%rcx .Lopen_avx2_init_hash: addq 0+0(%rsi,%rcx,1),%r10 @@ -4233,14 +4233,14 @@ _CET_ENDBR movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -4248,17 +4248,17 @@ _CET_ENDBR addq $16,%rcx cmpq $64,%rcx jne .Lopen_avx2_init_hash - +// Decrypt first 64 bytes vpxor 0(%rsi),%ymm0,%ymm0 vpxor 32(%rsi),%ymm4,%ymm4 - +// Store first 64 bytes of decrypted data vmovdqu %ymm0,0(%rdi) vmovdqu %ymm4,32(%rdi) leaq 64(%rsi),%rsi leaq 64(%rdi),%rdi subq $64,%rbx .Lopen_avx2_main_loop: - +// Hash and decrypt 512 bytes each iteration cmpq $512,%rbx jb .Lopen_avx2_main_loop_done vmovdqa .Lchacha20_consts(%rip),%ymm0 @@ -4348,14 +4348,14 @@ _CET_ENDBR movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -4443,14 +4443,14 @@ _CET_ENDBR movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -4538,14 +4538,14 @@ _CET_ENDBR movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -4614,14 +4614,14 @@ _CET_ENDBR movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -4678,14 +4678,14 @@ _CET_ENDBR movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -4728,7 +4728,7 @@ _CET_ENDBR movq %rbx,%rcx andq $-16,%rcx testq %rcx,%rcx - je .Lopen_avx2_tail_128_rounds + je .Lopen_avx2_tail_128_rounds // Have nothing to hash .Lopen_avx2_tail_128_rounds_and_x1hash: addq 0+0(%rsi,%r8,1),%r10 adcq 8+0(%rsi,%r8,1),%r11 @@ -4759,14 +4759,14 @@ _CET_ENDBR movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -4827,7 +4827,7 @@ _CET_ENDBR vmovdqa %ymm3,%ymm8 jmp .Lopen_avx2_tail_128_xor - +// ############################################################################# .Lopen_avx2_tail_256: vmovdqa .Lchacha20_consts(%rip),%ymm0 vmovdqa 0+64(%rbp),%ymm4 @@ -4873,14 +4873,14 @@ _CET_ENDBR movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -5019,14 +5019,14 @@ _CET_ENDBR movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -5064,7 +5064,7 @@ _CET_ENDBR leaq 128(%rdi),%rdi subq $128,%rbx jmp .Lopen_avx2_tail_128_xor - +// ############################################################################# .Lopen_avx2_tail_384: vmovdqa .Lchacha20_consts(%rip),%ymm0 vmovdqa 0+64(%rbp),%ymm4 @@ -5116,14 +5116,14 @@ _CET_ENDBR movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -5216,14 +5216,14 @@ _CET_ENDBR movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -5322,14 +5322,14 @@ _CET_ENDBR movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -5383,7 +5383,7 @@ _CET_ENDBR leaq 256(%rdi),%rdi subq $256,%rbx jmp .Lopen_avx2_tail_128_xor - +// ############################################################################# .Lopen_avx2_tail_512: vmovdqa .Lchacha20_consts(%rip),%ymm0 vmovdqa 0+64(%rbp),%ymm4 @@ -5439,14 +5439,14 @@ _CET_ENDBR movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -5512,14 +5512,14 @@ _CET_ENDBR movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -5593,14 +5593,14 @@ _CET_ENDBR movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -5719,14 +5719,14 @@ _CET_ENDBR movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -5817,7 +5817,7 @@ _CET_ENDBR vmovdqa %xmm0,%xmm1 jb .Lopen_avx2_exit subq $16,%rbx - +// load for decryption vpxor (%rsi),%xmm0,%xmm1 vmovdqu %xmm1,(%rdi) leaq 16(%rsi),%rsi @@ -5827,7 +5827,7 @@ _CET_ENDBR .Lopen_avx2_exit: vzeroupper jmp .Lopen_sse_tail_16 - +// ############################################################################# .Lopen_avx2_192: vmovdqa %ymm0,%ymm1 vmovdqa %ymm0,%ymm2 @@ -5928,10 +5928,10 @@ _CET_ENDBR vpaddd %ymm11,%ymm12,%ymm12 vpaddd %ymm15,%ymm13,%ymm13 vperm2i128 $0x02,%ymm0,%ymm4,%ymm3 - +// Clamp and store the key vpand .Lclamp(%rip),%ymm3,%ymm3 vmovdqa %ymm3,0+0(%rbp) - +// Stream for up to 192 bytes vperm2i128 $0x13,%ymm0,%ymm4,%ymm0 vperm2i128 $0x13,%ymm8,%ymm12,%ymm4 vperm2i128 $0x02,%ymm1,%ymm5,%ymm8 @@ -5974,14 +5974,14 @@ _CET_ENDBR movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -6014,24 +6014,24 @@ _CET_ENDBR movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 - +// Load + decrypt vpxor (%rsi),%ymm0,%ymm0 vmovdqu %ymm0,(%rdi) leaq 32(%rsi),%rsi leaq 32(%rdi),%rdi - +// Shift stream vmovdqa %ymm4,%ymm0 vmovdqa %ymm8,%ymm4 vmovdqa %ymm12,%ymm8 @@ -6076,14 +6076,14 @@ _CET_ENDBR movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -6096,7 +6096,7 @@ _CET_ENDBR .Lopen_avx2_short_tail_32_exit: vzeroupper jmp .Lopen_sse_tail_16 - +// ############################################################################# .Lopen_avx2_320: vmovdqa %ymm0,%ymm1 vmovdqa %ymm0,%ymm2 @@ -6243,10 +6243,10 @@ _CET_ENDBR vpaddd 0+192(%rbp),%ymm13,%ymm13 vpaddd 0+224(%rbp),%ymm14,%ymm14 vperm2i128 $0x02,%ymm0,%ymm4,%ymm3 - +// Clamp and store the key vpand .Lclamp(%rip),%ymm3,%ymm3 vmovdqa %ymm3,0+0(%rbp) - +// Stream for up to 320 bytes vperm2i128 $0x13,%ymm0,%ymm4,%ymm0 vperm2i128 $0x13,%ymm8,%ymm12,%ymm4 vperm2i128 $0x02,%ymm1,%ymm5,%ymm8 @@ -6260,8 +6260,8 @@ _CET_ENDBR jmp .Lopen_avx2_short .size chacha20_poly1305_open_avx2, .-chacha20_poly1305_open_avx2 .cfi_endproc - - +// ############################################################################# +// ############################################################################# .globl chacha20_poly1305_seal_avx2 .hidden chacha20_poly1305_seal_avx2 .type chacha20_poly1305_seal_avx2,@function @@ -6287,8 +6287,8 @@ _CET_ENDBR pushq %r15 .cfi_adjust_cfa_offset 8 .cfi_offset %r15,-56 - - +// We write the calculated authenticator back to keyp at the end, so save +// the pointer on the stack too. pushq %r9 .cfi_adjust_cfa_offset 8 .cfi_offset %r9,-64 @@ -6297,7 +6297,7 @@ _CET_ENDBR leaq 32(%rsp),%rbp andq $-32,%rbp - movq 56(%r9),%rbx + movq 56(%r9),%rbx // extra_in_len addq %rdx,%rbx movq %r8,0+0+32(%rbp) movq %rbx,8+0+32(%rbp) @@ -6525,7 +6525,7 @@ _CET_ENDBR vmovdqa %ymm15,0+0(%rbp) movq %r8,%r8 call poly_hash_ad_internal - +// Safely store 320 bytes (otherwise would handle with optimized call) vpxor 0(%rsi),%ymm3,%ymm3 vpxor 32(%rsi),%ymm11,%ymm11 vmovdqu %ymm3,0(%rdi) @@ -6905,14 +6905,14 @@ _CET_ENDBR movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -7002,14 +7002,14 @@ _CET_ENDBR movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -7097,14 +7097,14 @@ _CET_ENDBR movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -7152,14 +7152,14 @@ _CET_ENDBR movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -7185,14 +7185,14 @@ _CET_ENDBR movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -7276,14 +7276,14 @@ _CET_ENDBR movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -7309,14 +7309,14 @@ _CET_ENDBR movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -7331,7 +7331,7 @@ _CET_ENDBR ja .Lseal_avx2_tail_384 cmpq $128,%rbx ja .Lseal_avx2_tail_256 - +// ############################################################################# .Lseal_avx2_tail_128: vmovdqa .Lchacha20_consts(%rip),%ymm0 vmovdqa 0+64(%rbp),%ymm4 @@ -7363,14 +7363,14 @@ _CET_ENDBR movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -7418,14 +7418,14 @@ _CET_ENDBR movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -7470,14 +7470,14 @@ _CET_ENDBR movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -7498,7 +7498,7 @@ _CET_ENDBR vmovdqa %ymm3,%ymm8 jmp .Lseal_avx2_short_loop - +// ############################################################################# .Lseal_avx2_tail_256: vmovdqa .Lchacha20_consts(%rip),%ymm0 vmovdqa 0+64(%rbp),%ymm4 @@ -7542,14 +7542,14 @@ _CET_ENDBR movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -7623,14 +7623,14 @@ _CET_ENDBR movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -7701,14 +7701,14 @@ _CET_ENDBR movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -7748,7 +7748,7 @@ _CET_ENDBR leaq 128(%rsi),%rsi subq $128,%rbx jmp .Lseal_avx2_short_hash_remainder - +// ############################################################################# .Lseal_avx2_tail_384: vmovdqa .Lchacha20_consts(%rip),%ymm0 vmovdqa 0+64(%rbp),%ymm4 @@ -7797,14 +7797,14 @@ _CET_ENDBR movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -7878,14 +7878,14 @@ _CET_ENDBR movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -7956,14 +7956,14 @@ _CET_ENDBR movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -8057,7 +8057,7 @@ _CET_ENDBR leaq 256(%rsi),%rsi subq $256,%rbx jmp .Lseal_avx2_short_hash_remainder - +// ############################################################################# .Lseal_avx2_tail_512: vmovdqa .Lchacha20_consts(%rip),%ymm0 vmovdqa 0+64(%rbp),%ymm4 @@ -8104,14 +8104,14 @@ _CET_ENDBR movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -8240,14 +8240,14 @@ _CET_ENDBR movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -8373,14 +8373,14 @@ _CET_ENDBR movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -8456,7 +8456,7 @@ _CET_ENDBR leaq 384(%rsi),%rsi subq $384,%rbx jmp .Lseal_avx2_short_hash_remainder - +// ############################################################################## .Lseal_avx2_320: vmovdqa %ymm0,%ymm1 vmovdqa %ymm0,%ymm2 @@ -8603,10 +8603,10 @@ _CET_ENDBR vpaddd 0+192(%rbp),%ymm13,%ymm13 vpaddd 0+224(%rbp),%ymm14,%ymm14 vperm2i128 $0x02,%ymm0,%ymm4,%ymm3 - +// Clamp and store the key vpand .Lclamp(%rip),%ymm3,%ymm3 vmovdqa %ymm3,0+0(%rbp) - +// Stream for up to 320 bytes vperm2i128 $0x13,%ymm0,%ymm4,%ymm0 vperm2i128 $0x13,%ymm8,%ymm12,%ymm4 vperm2i128 $0x02,%ymm1,%ymm5,%ymm8 @@ -8618,7 +8618,7 @@ _CET_ENDBR vperm2i128 $0x13,%ymm2,%ymm6,%ymm2 vperm2i128 $0x13,%ymm10,%ymm14,%ymm6 jmp .Lseal_avx2_short - +// ############################################################################## .Lseal_avx2_192: vmovdqa %ymm0,%ymm1 vmovdqa %ymm0,%ymm2 @@ -8719,10 +8719,10 @@ _CET_ENDBR vpaddd %ymm11,%ymm12,%ymm12 vpaddd %ymm15,%ymm13,%ymm13 vperm2i128 $0x02,%ymm0,%ymm4,%ymm3 - +// Clamp and store the key vpand .Lclamp(%rip),%ymm3,%ymm3 vmovdqa %ymm3,0+0(%rbp) - +// Stream for up to 192 bytes vperm2i128 $0x13,%ymm0,%ymm4,%ymm0 vperm2i128 $0x13,%ymm8,%ymm12,%ymm4 vperm2i128 $0x02,%ymm1,%ymm5,%ymm8 @@ -8765,14 +8765,14 @@ _CET_ENDBR movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -8784,11 +8784,11 @@ _CET_ENDBR cmpq $32,%rbx jb .Lseal_avx2_short_tail subq $32,%rbx - +// Encrypt vpxor (%rsi),%ymm0,%ymm0 vmovdqu %ymm0,(%rdi) leaq 32(%rsi),%rsi - +// Load + hash addq 0+0(%rdi),%r10 adcq 8+0(%rdi),%r11 adcq $1,%r12 @@ -8818,14 +8818,14 @@ _CET_ENDBR movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 @@ -8858,20 +8858,20 @@ _CET_ENDBR movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 leaq 32(%rdi),%rdi - +// Shift stream vmovdqa %ymm4,%ymm0 vmovdqa %ymm8,%ymm4 vmovdqa %ymm12,%ymm8 @@ -8918,14 +8918,14 @@ _CET_ENDBR movq %r13,%r10 movq %r14,%r11 movq %r15,%r12 - andq $3,%r12 + andq $3,%r12 // At this point acc2 is 2 bits at most (value of 3) movq %r15,%r13 andq $-4,%r13 movq %r9,%r14 shrdq $2,%r9,%r15 shrq $2,%r9 addq %r13,%r15 - adcq %r14,%r9 + adcq %r14,%r9 // No carry out since t3 is 61 bits and t1 is 63 bits addq %r15,%r10 adcq %r9,%r11 adcq $0,%r12 diff --git a/third_party/boringssl/gen/crypto/chacha20_poly1305_x86_64-win.asm b/third_party/boringssl/gen/crypto/chacha20_poly1305_x86_64-win.asm index 489167792..3a3e3019c 100644 --- a/third_party/boringssl/gen/crypto/chacha20_poly1305_x86_64-win.asm +++ b/third_party/boringssl/gen/crypto/chacha20_poly1305_x86_64-win.asm @@ -64,7 +64,7 @@ poly_hash_ad_internal: cmp r8,13 jne NEAR $L$hash_ad_loop $L$poly_fast_tls_ad: - +; Special treatment for the TLS case of 13 bytes mov r10,QWORD[rcx] mov r11,QWORD[5+rcx] shr r11,24 @@ -95,21 +95,21 @@ $L$poly_fast_tls_ad: mov r10,r13 mov r11,r14 mov r12,r15 - and r12,3 + and r12,3 ; At this point acc2 is 2 bits at most (value of 3) mov r13,r15 and r13,-4 mov r14,r9 shrd r15,r9,2 shr r9,2 add r15,r13 - adc r9,r14 + adc r9,r14 ; No carry out since t3 is 61 bits and t1 is 63 bits add r10,r15 adc r11,r9 adc r12,0 ret $L$hash_ad_loop: - +; Hash in 16 byte chunk cmp r8,16 jb NEAR $L$hash_ad_tail add r10,QWORD[((0+0))+rcx] @@ -141,14 +141,14 @@ $L$hash_ad_loop: mov r10,r13 mov r11,r14 mov r12,r15 - and r12,3 + and r12,3 ; At this point acc2 is 2 bits at most (value of 3) mov r13,r15 and r13,-4 mov r14,r9 shrd r15,r9,2 shr r9,2 add r15,r13 - adc r9,r14 + adc r9,r14 ; No carry out since t3 is 61 bits and t1 is 63 bits add r10,r15 adc r11,r9 adc r12,0 @@ -159,7 +159,7 @@ $L$hash_ad_loop: $L$hash_ad_tail: cmp r8,0 je NEAR $L$hash_ad_done - +; Hash last < 16 byte tail xor r13,r13 xor r14,r14 xor r15,r15 @@ -202,19 +202,19 @@ $L$hash_ad_tail_loop: mov r10,r13 mov r11,r14 mov r12,r15 - and r12,3 + and r12,3 ; At this point acc2 is 2 bits at most (value of 3) mov r13,r15 and r13,-4 mov r14,r9 shrd r15,r9,2 shr r9,2 add r15,r13 - adc r9,r14 + adc r9,r14 ; No carry out since t3 is 61 bits and t1 is 63 bits add r10,r15 adc r11,r9 adc r12,0 - +; Finished AD $L$hash_ad_done: ret @@ -250,8 +250,8 @@ _CET_ENDBR push r15 - - +; We write the calculated authenticator back to keyp at the end, so save +; the pointer on the stack too. push r9 sub rsp,288 + 160 + 32 @@ -277,14 +277,14 @@ _CET_ENDBR cmp rbx,128 jbe NEAR $L$open_sse_128 - +; For long buffers, prepare the poly key first movdqa xmm0,XMMWORD[$L$chacha20_consts] movdqu xmm4,XMMWORD[r9] movdqu xmm8,XMMWORD[16+r9] movdqu xmm12,XMMWORD[32+r9] movdqa xmm7,xmm12 - +; Store on stack, to free keyp movdqa XMMWORD[(160+48)+rbp],xmm4 movdqa XMMWORD[(160+64)+rbp],xmm8 movdqa XMMWORD[(160+96)+rbp],xmm12 @@ -335,20 +335,20 @@ $L$open_sse_init_rounds: dec r10 jne NEAR $L$open_sse_init_rounds - +; A0|B0 hold the Poly1305 32-byte key, C0,D0 can be discarded paddd xmm0,XMMWORD[$L$chacha20_consts] paddd xmm4,XMMWORD[((160+48))+rbp] - +; Clamp and store the key pand xmm0,XMMWORD[$L$clamp] movdqa XMMWORD[(160+0)+rbp],xmm0 movdqa XMMWORD[(160+16)+rbp],xmm4 - +; Hash mov r8,r8 call poly_hash_ad_internal $L$open_sse_main_loop: cmp rbx,16*16 jb NEAR $L$open_sse_tail - +; Load state, increment counter blocks movdqa xmm0,XMMWORD[$L$chacha20_consts] movdqa xmm4,XMMWORD[((160+48))+rbp] movdqa xmm8,XMMWORD[((160+64))+rbp] @@ -374,8 +374,8 @@ $L$open_sse_main_loop: movdqa XMMWORD[(160+128)+rbp],xmm14 movdqa XMMWORD[(160+144)+rbp],xmm15 - - +; There are 10 ChaCha20 iterations of 2QR each, so for 6 iterations we +; hash 2 blocks, and for the remaining 4 only 1 block - for a total of 16 mov rcx,4 mov r8,rsi $L$open_sse_main_loop_rounds: @@ -510,14 +510,14 @@ $L$open_sse_main_loop_rounds: mov r10,r13 mov r11,r14 mov r12,r15 - and r12,3 + and r12,3 ; At this point acc2 is 2 bits at most (value of 3) mov r13,r15 and r13,-4 mov r14,r9 shrd r15,r9,2 shr r9,2 add r15,r13 - adc r9,r14 + adc r9,r14 ; No carry out since t3 is 61 bits and t1 is 63 bits add r10,r15 adc r11,r9 adc r12,0 @@ -637,14 +637,14 @@ $L$open_sse_main_loop_rounds: mov r10,r13 mov r11,r14 mov r12,r15 - and r12,3 + and r12,3 ; At this point acc2 is 2 bits at most (value of 3) mov r13,r15 and r13,-4 mov r14,r9 shrd r15,r9,2 shr r9,2 add r15,r13 - adc r9,r14 + adc r9,r14 ; No carry out since t3 is 61 bits and t1 is 63 bits add r10,r15 adc r11,r9 adc r12,0 @@ -723,7 +723,7 @@ $L$open_sse_main_loop_rounds: sub rbx,16*16 jmp NEAR $L$open_sse_main_loop $L$open_sse_tail: - +; Handle the various tail sizes efficiently test rbx,rbx jz NEAR $L$open_sse_finalize cmp rbx,12*16 @@ -773,14 +773,14 @@ $L$open_sse_tail_64_rounds_and_x1hash: mov r10,r13 mov r11,r14 mov r12,r15 - and r12,3 + and r12,3 ; At this point acc2 is 2 bits at most (value of 3) mov r13,r15 and r13,-4 mov r14,r9 shrd r15,r9,2 shr r9,2 add r15,r13 - adc r9,r14 + adc r9,r14 ; No carry out since t3 is 61 bits and t1 is 63 bits add r10,r15 adc r11,r9 adc r12,0 @@ -841,7 +841,7 @@ $L$open_sse_tail_64_rounds: paddd xmm12,XMMWORD[((160+96))+rbp] jmp NEAR $L$open_sse_tail_64_dec_loop - +; ############################################################################## $L$open_sse_tail_128: movdqa xmm0,XMMWORD[$L$chacha20_consts] movdqa xmm4,XMMWORD[((160+48))+rbp] @@ -889,14 +889,14 @@ $L$open_sse_tail_128_rounds_and_x1hash: mov r10,r13 mov r11,r14 mov r12,r15 - and r12,3 + and r12,3 ; At this point acc2 is 2 bits at most (value of 3) mov r13,r15 and r13,-4 mov r14,r9 shrd r15,r9,2 shr r9,2 add r15,r13 - adc r9,r14 + adc r9,r14 ; No carry out since t3 is 61 bits and t1 is 63 bits add r10,r15 adc r11,r9 adc r12,0 @@ -1017,7 +1017,7 @@ $L$open_sse_tail_128_rounds: lea rsi,[64+rsi] lea rdi,[64+rdi] jmp NEAR $L$open_sse_tail_64_dec_loop - +; ############################################################################## $L$open_sse_tail_192: movdqa xmm0,XMMWORD[$L$chacha20_consts] movdqa xmm4,XMMWORD[((160+48))+rbp] @@ -1074,14 +1074,14 @@ $L$open_sse_tail_192_rounds_and_x1hash: mov r10,r13 mov r11,r14 mov r12,r15 - and r12,3 + and r12,3 ; At this point acc2 is 2 bits at most (value of 3) mov r13,r15 and r13,-4 mov r14,r9 shrd r15,r9,2 shr r9,2 add r15,r13 - adc r9,r14 + adc r9,r14 ; No carry out since t3 is 61 bits and t1 is 63 bits add r10,r15 adc r11,r9 adc r12,0 @@ -1250,14 +1250,14 @@ $L$open_sse_tail_192_rounds: mov r10,r13 mov r11,r14 mov r12,r15 - and r12,3 + and r12,3 ; At this point acc2 is 2 bits at most (value of 3) mov r13,r15 and r13,-4 mov r14,r9 shrd r15,r9,2 shr r9,2 add r15,r13 - adc r9,r14 + adc r9,r14 ; No carry out since t3 is 61 bits and t1 is 63 bits add r10,r15 adc r11,r9 adc r12,0 @@ -1293,14 +1293,14 @@ $L$open_sse_tail_192_rounds: mov r10,r13 mov r11,r14 mov r12,r15 - and r12,3 + and r12,3 ; At this point acc2 is 2 bits at most (value of 3) mov r13,r15 and r13,-4 mov r14,r9 shrd r15,r9,2 shr r9,2 add r15,r13 - adc r9,r14 + adc r9,r14 ; No carry out since t3 is 61 bits and t1 is 63 bits add r10,r15 adc r11,r9 adc r12,0 @@ -1347,7 +1347,7 @@ $L$open_sse_tail_192_finish: lea rsi,[128+rsi] lea rdi,[128+rdi] jmp NEAR $L$open_sse_tail_64_dec_loop - +; ############################################################################## $L$open_sse_tail_256: movdqa xmm0,XMMWORD[$L$chacha20_consts] movdqa xmm4,XMMWORD[((160+48))+rbp] @@ -1558,14 +1558,14 @@ $L$open_sse_tail_256_rounds_and_x1hash: mov r10,r13 mov r11,r14 mov r12,r15 - and r12,3 + and r12,3 ; At this point acc2 is 2 bits at most (value of 3) mov r13,r15 and r13,-4 mov r14,r9 shrd r15,r9,2 shr r9,2 add r15,r13 - adc r9,r14 + adc r9,r14 ; No carry out since t3 is 61 bits and t1 is 63 bits add r10,r15 adc r11,r9 adc r12,0 @@ -1629,14 +1629,14 @@ $L$open_sse_tail_256_hash: mov r10,r13 mov r11,r14 mov r12,r15 - and r12,3 + and r12,3 ; At this point acc2 is 2 bits at most (value of 3) mov r13,r15 and r13,-4 mov r14,r9 shrd r15,r9,2 shr r9,2 add r15,r13 - adc r9,r14 + adc r9,r14 ; No carry out since t3 is 61 bits and t1 is 63 bits add r10,r15 adc r11,r9 adc r12,0 @@ -1702,8 +1702,8 @@ $L$open_sse_tail_256_hash: sub rbx,12*16 lea rsi,[192+rsi] lea rdi,[192+rdi] - - +; ############################################################################## +; Decrypt the remaining data, 16B at a time, using existing stream $L$open_sse_tail_64_dec_loop: cmp rbx,16 jb NEAR $L$open_sse_tail_16_init @@ -1720,13 +1720,13 @@ $L$open_sse_tail_64_dec_loop: $L$open_sse_tail_16_init: movdqa xmm1,xmm0 - +; Decrypt up to 16 bytes at the end. $L$open_sse_tail_16: test rbx,rbx jz NEAR $L$open_sse_finalize - - +; Read the final bytes into %xmm3. They need to be read in reverse order so +; that they end up in the correct order in %xmm3. pxor xmm3,xmm3 lea rsi,[((-1))+rbx*1+rsi] mov r8,rbx @@ -1739,10 +1739,10 @@ $L$open_sse_tail_16_compose: movq r13,xmm3 pextrq r14,xmm3,1 - +; The final bytes of keystream are in %xmm1. pxor xmm3,xmm1 - +; Copy the plaintext bytes out. $L$open_sse_tail_16_extract: pextrb XMMWORD[rdi],xmm3,0 psrldq xmm3,1 @@ -1779,14 +1779,14 @@ $L$open_sse_tail_16_extract: mov r10,r13 mov r11,r14 mov r12,r15 - and r12,3 + and r12,3 ; At this point acc2 is 2 bits at most (value of 3) mov r13,r15 and r13,-4 mov r14,r9 shrd r15,r9,2 shr r9,2 add r15,r13 - adc r9,r14 + adc r9,r14 ; No carry out since t3 is 61 bits and t1 is 63 bits add r10,r15 adc r11,r9 adc r12,0 @@ -1822,19 +1822,19 @@ $L$open_sse_finalize: mov r10,r13 mov r11,r14 mov r12,r15 - and r12,3 + and r12,3 ; At this point acc2 is 2 bits at most (value of 3) mov r13,r15 and r13,-4 mov r14,r9 shrd r15,r9,2 shr r9,2 add r15,r13 - adc r9,r14 + adc r9,r14 ; No carry out since t3 is 61 bits and t1 is 63 bits add r10,r15 adc r11,r9 adc r12,0 - +; Final reduce mov r13,r10 mov r14,r11 mov r15,r12 @@ -1844,7 +1844,7 @@ $L$open_sse_finalize: cmovc r10,r13 cmovc r11,r14 cmovc r12,r15 - +; Add in s part of the key add r10,QWORD[((0+160+16))+rbp] adc r11,QWORD[((8+160+16))+rbp] @@ -1862,7 +1862,7 @@ $L$open_sse_finalize: add rsp,288 + 160 + 32 - +; The tag replaces the key on return pop r9 mov QWORD[r9],r10 @@ -1882,7 +1882,7 @@ $L$open_sse_finalize: mov rdi,QWORD[8+rsp] ;WIN64 epilogue mov rsi,QWORD[16+rsp] ret - +; ############################################################################## $L$open_sse_128: movdqu xmm0,XMMWORD[$L$chacha20_consts] @@ -2045,11 +2045,11 @@ $L$open_sse_128_rounds: paddd xmm13,xmm15 paddd xmm15,XMMWORD[$L$sse_inc] paddd xmm14,xmm15 - +; Clamp and store the key pand xmm0,XMMWORD[$L$clamp] movdqa XMMWORD[(160+0)+rbp],xmm0 movdqa XMMWORD[(160+16)+rbp],xmm4 - +; Hash mov r8,r8 call poly_hash_ad_internal $L$open_sse_128_xor_hash: @@ -2060,7 +2060,7 @@ $L$open_sse_128_xor_hash: adc r11,QWORD[((8+0))+rsi] adc r12,1 - +; Load for decryption movdqu xmm3,XMMWORD[rsi] pxor xmm1,xmm3 movdqu XMMWORD[rdi],xmm1 @@ -2092,19 +2092,19 @@ $L$open_sse_128_xor_hash: mov r10,r13 mov r11,r14 mov r12,r15 - and r12,3 + and r12,3 ; At this point acc2 is 2 bits at most (value of 3) mov r13,r15 and r13,-4 mov r14,r9 shrd r15,r9,2 shr r9,2 add r15,r13 - adc r9,r14 + adc r9,r14 ; No carry out since t3 is 61 bits and t1 is 63 bits add r10,r15 adc r11,r9 adc r12,0 - +; Shift the stream left movdqa xmm1,xmm5 movdqa xmm5,xmm9 movdqa xmm9,xmm13 @@ -2116,12 +2116,12 @@ $L$open_sse_128_xor_hash: $L$SEH_end_chacha20_poly1305_open_sse41: - - - - - - +; ############################################################################### +; ############################################################################### +; void chacha20_poly1305_seal(uint8_t *out_ciphertext, const uint8_t *plaintext, +; size_t plaintext_len, const uint8_t *ad, +; size_t ad_len, +; union chacha20_poly1305_seal_data *data); global chacha20_poly1305_seal_sse41 ALIGN 64 @@ -2152,8 +2152,8 @@ _CET_ENDBR push r15 - - +; We write the calculated authenticator back to keyp at the end, so save +; the pointer on the stack too. push r9 sub rsp,288 + 160 + 32 @@ -2172,7 +2172,7 @@ _CET_ENDBR movaps XMMWORD[(128+0)+rbp],xmm14 movaps XMMWORD[(144+0)+rbp],xmm15 - mov rbx,QWORD[56+r9] + mov rbx,QWORD[56+r9] ; extra_in_len add rbx,rdx mov QWORD[((0+160+32))+rbp],r8 mov QWORD[((8+160+32))+rbp],rbx @@ -2180,7 +2180,7 @@ _CET_ENDBR cmp rbx,128 jbe NEAR $L$seal_sse_128 - +; For longer buffers, prepare the poly key + some stream movdqa xmm0,XMMWORD[$L$chacha20_consts] movdqu xmm4,XMMWORD[r9] movdqu xmm8,XMMWORD[16+r9] @@ -2201,7 +2201,7 @@ _CET_ENDBR paddd xmm12,XMMWORD[$L$sse_inc] movdqa xmm13,xmm12 paddd xmm12,XMMWORD[$L$sse_inc] - +; Store on stack movdqa XMMWORD[(160+48)+rbp],xmm4 movdqa XMMWORD[(160+64)+rbp],xmm8 movdqa XMMWORD[(160+96)+rbp],xmm12 @@ -2414,11 +2414,11 @@ $L$seal_sse_init_rounds: paddd xmm8,XMMWORD[((160+64))+rbp] paddd xmm12,XMMWORD[((160+96))+rbp] - +; Clamp and store the key pand xmm3,XMMWORD[$L$clamp] movdqa XMMWORD[(160+0)+rbp],xmm3 movdqa XMMWORD[(160+16)+rbp],xmm7 - +; Hash mov r8,r8 call poly_hash_ad_internal movdqu xmm3,XMMWORD[((0 + 0))+rsi] @@ -2635,14 +2635,14 @@ $L$seal_sse_main_rounds: mov r10,r13 mov r11,r14 mov r12,r15 - and r12,3 + and r12,3 ; At this point acc2 is 2 bits at most (value of 3) mov r13,r15 and r13,-4 mov r14,r9 shrd r15,r9,2 shr r9,2 add r15,r13 - adc r9,r14 + adc r9,r14 ; No carry out since t3 is 61 bits and t1 is 63 bits add r10,r15 adc r11,r9 adc r12,0 @@ -2763,14 +2763,14 @@ $L$seal_sse_main_rounds: mov r10,r13 mov r11,r14 mov r12,r15 - and r12,3 + and r12,3 ; At this point acc2 is 2 bits at most (value of 3) mov r13,r15 and r13,-4 mov r14,r9 shrd r15,r9,2 shr r9,2 add r15,r13 - adc r9,r14 + adc r9,r14 ; No carry out since t3 is 61 bits and t1 is 63 bits add r10,r15 adc r11,r9 adc r12,0 @@ -2871,7 +2871,7 @@ $L$seal_sse_main_loop_xor: ja NEAR $L$seal_sse_tail_192 cmp rbx,4*16 ja NEAR $L$seal_sse_tail_128 - +; ############################################################################## $L$seal_sse_tail_64: movdqa xmm0,XMMWORD[$L$chacha20_consts] movdqa xmm4,XMMWORD[((160+48))+rbp] @@ -2910,14 +2910,14 @@ $L$seal_sse_tail_64_rounds_and_x2hash: mov r10,r13 mov r11,r14 mov r12,r15 - and r12,3 + and r12,3 ; At this point acc2 is 2 bits at most (value of 3) mov r13,r15 and r13,-4 mov r14,r9 shrd r15,r9,2 shr r9,2 add r15,r13 - adc r9,r14 + adc r9,r14 ; No carry out since t3 is 61 bits and t1 is 63 bits add r10,r15 adc r11,r9 adc r12,0 @@ -2995,14 +2995,14 @@ $L$seal_sse_tail_64_rounds_and_x1hash: mov r10,r13 mov r11,r14 mov r12,r15 - and r12,3 + and r12,3 ; At this point acc2 is 2 bits at most (value of 3) mov r13,r15 and r13,-4 mov r14,r9 shrd r15,r9,2 shr r9,2 add r15,r13 - adc r9,r14 + adc r9,r14 ; No carry out since t3 is 61 bits and t1 is 63 bits add r10,r15 adc r11,r9 adc r12,0 @@ -3018,7 +3018,7 @@ $L$seal_sse_tail_64_rounds_and_x1hash: paddd xmm12,XMMWORD[((160+96))+rbp] jmp NEAR $L$seal_sse_128_tail_xor - +; ############################################################################## $L$seal_sse_tail_128: movdqa xmm0,XMMWORD[$L$chacha20_consts] movdqa xmm4,XMMWORD[((160+48))+rbp] @@ -3063,14 +3063,14 @@ $L$seal_sse_tail_128_rounds_and_x2hash: mov r10,r13 mov r11,r14 mov r12,r15 - and r12,3 + and r12,3 ; At this point acc2 is 2 bits at most (value of 3) mov r13,r15 and r13,-4 mov r14,r9 shrd r15,r9,2 shr r9,2 add r15,r13 - adc r9,r14 + adc r9,r14 ; No carry out since t3 is 61 bits and t1 is 63 bits add r10,r15 adc r11,r9 adc r12,0 @@ -3148,14 +3148,14 @@ $L$seal_sse_tail_128_rounds_and_x1hash: mov r10,r13 mov r11,r14 mov r12,r15 - and r12,3 + and r12,3 ; At this point acc2 is 2 bits at most (value of 3) mov r13,r15 and r13,-4 mov r14,r9 shrd r15,r9,2 shr r9,2 add r15,r13 - adc r9,r14 + adc r9,r14 ; No carry out since t3 is 61 bits and t1 is 63 bits add r10,r15 adc r11,r9 adc r12,0 @@ -3232,7 +3232,7 @@ $L$seal_sse_tail_128_rounds_and_x1hash: sub rbx,4*16 lea rsi,[64+rsi] jmp NEAR $L$seal_sse_128_tail_hash - +; ############################################################################## $L$seal_sse_tail_192: movdqa xmm0,XMMWORD[$L$chacha20_consts] movdqa xmm4,XMMWORD[((160+48))+rbp] @@ -3283,14 +3283,14 @@ $L$seal_sse_tail_192_rounds_and_x2hash: mov r10,r13 mov r11,r14 mov r12,r15 - and r12,3 + and r12,3 ; At this point acc2 is 2 bits at most (value of 3) mov r13,r15 and r13,-4 mov r14,r9 shrd r15,r9,2 shr r9,2 add r15,r13 - adc r9,r14 + adc r9,r14 ; No carry out since t3 is 61 bits and t1 is 63 bits add r10,r15 adc r11,r9 adc r12,0 @@ -3389,14 +3389,14 @@ $L$seal_sse_tail_192_rounds_and_x1hash: mov r10,r13 mov r11,r14 mov r12,r15 - and r12,3 + and r12,3 ; At this point acc2 is 2 bits at most (value of 3) mov r13,r15 and r13,-4 mov r14,r9 shrd r15,r9,2 shr r9,2 add r15,r13 - adc r9,r14 + adc r9,r14 ; No carry out since t3 is 61 bits and t1 is 63 bits add r10,r15 adc r11,r9 adc r12,0 @@ -3509,7 +3509,7 @@ $L$seal_sse_tail_192_rounds_and_x1hash: mov rcx,8*16 sub rbx,8*16 lea rsi,[128+rsi] - +; ############################################################################## $L$seal_sse_128_tail_hash: cmp rcx,16 jb NEAR $L$seal_sse_128_tail_xor @@ -3542,14 +3542,14 @@ $L$seal_sse_128_tail_hash: mov r10,r13 mov r11,r14 mov r12,r15 - and r12,3 + and r12,3 ; At this point acc2 is 2 bits at most (value of 3) mov r13,r15 and r13,-4 mov r14,r9 shrd r15,r9,2 shr r9,2 add r15,r13 - adc r9,r14 + adc r9,r14 ; No carry out since t3 is 61 bits and t1 is 63 bits add r10,r15 adc r11,r9 adc r12,0 @@ -3562,11 +3562,11 @@ $L$seal_sse_128_tail_xor: cmp rbx,16 jb NEAR $L$seal_sse_tail_16 sub rbx,16 - +; Load for decryption movdqu xmm3,XMMWORD[rsi] pxor xmm0,xmm3 movdqu XMMWORD[rdi],xmm0 - +; Then hash add r10,QWORD[rdi] adc r11,QWORD[8+rdi] adc r12,1 @@ -3598,19 +3598,19 @@ $L$seal_sse_128_tail_xor: mov r10,r13 mov r11,r14 mov r12,r15 - and r12,3 + and r12,3 ; At this point acc2 is 2 bits at most (value of 3) mov r13,r15 and r13,-4 mov r14,r9 shrd r15,r9,2 shr r9,2 add r15,r13 - adc r9,r14 + adc r9,r14 ; No carry out since t3 is 61 bits and t1 is 63 bits add r10,r15 adc r11,r9 adc r12,0 - +; Shift the stream left movdqa xmm0,xmm4 movdqa xmm4,xmm8 movdqa xmm8,xmm12 @@ -3623,7 +3623,7 @@ $L$seal_sse_128_tail_xor: $L$seal_sse_tail_16: test rbx,rbx jz NEAR $L$process_blocks_of_extra_in - +; We can only load the PT one byte at a time to avoid buffer overread mov r8,rbx mov rcx,rbx lea rsi,[((-1))+rbx*1+rsi] @@ -3635,10 +3635,10 @@ $L$seal_sse_tail_16_compose: dec rcx jne NEAR $L$seal_sse_tail_16_compose - +; XOR the keystream with the plaintext. pxor xmm15,xmm0 - +; Write ciphertext out, byte-by-byte. mov rcx,rbx movdqu xmm0,xmm15 $L$seal_sse_tail_16_extract: @@ -3648,42 +3648,42 @@ $L$seal_sse_tail_16_extract: sub rcx,1 jnz NEAR $L$seal_sse_tail_16_extract - - - - - - - +; %xmm15 contains the final (partial, non-empty) block of ciphertext which +; needs to be fed into the Poly1305 state. The right-most %rbx bytes of it +; are valid. We need to fill it with extra_in bytes until full, or until we +; run out of bytes. +; +; %r9 points to the tag output, which is actually a struct with the +; extra_in pointer and length at offset 48. mov r9,QWORD[((288 + 160 + 32))+rsp] - mov r14,QWORD[56+r9] - mov r13,QWORD[48+r9] + mov r14,QWORD[56+r9] ; extra_in_len + mov r13,QWORD[48+r9] ; extra_in test r14,r14 - jz NEAR $L$process_partial_block + jz NEAR $L$process_partial_block ; Common case: no bytes of extra_in mov r15,16 - sub r15,rbx - cmp r14,r15 - + sub r15,rbx ; 16-%rbx is the number of bytes that fit into %xmm15. + cmp r14,r15 ; if extra_in_len < 16-%rbx, only copy extra_in_len +; (note that AT&T syntax reverses the arguments) jge NEAR $L$load_extra_in mov r15,r14 $L$load_extra_in: - - +; %r15 contains the number of bytes of extra_in (pointed to by %r13) to load +; into %xmm15. They are loaded in reverse order. lea rsi,[((-1))+r15*1+r13] - - +; Update extra_in and extra_in_len to reflect the bytes that are about to +; be read. add r13,r15 sub r14,r15 mov QWORD[48+r9],r13 mov QWORD[56+r9],r14 - - +; Update %r8, which is used to select the mask later on, to reflect the +; extra bytes about to be added. add r8,r15 - +; Load %r15 bytes of extra_in into %xmm11. pxor xmm11,xmm11 $L$load_extra_load_loop: pslldq xmm11,1 @@ -3692,9 +3692,9 @@ $L$load_extra_load_loop: sub r15,1 jnz NEAR $L$load_extra_load_loop - - - +; Shift %xmm11 up the length of the remainder from the main encryption. Sadly, +; the shift for an XMM register has to be a constant, thus we loop to do +; this. mov r15,rbx $L$load_extra_shift_loop: @@ -3702,18 +3702,18 @@ $L$load_extra_shift_loop: sub r15,1 jnz NEAR $L$load_extra_shift_loop - - - +; Mask %xmm15 (the remainder from the main encryption) so that superfluous +; bytes are zero. This means that the non-zero bytes in %xmm11 and %xmm15 are +; disjoint and so we can merge them with an OR. lea r15,[$L$and_masks] shl rbx,4 pand xmm15,XMMWORD[((-16))+rbx*1+r15] - +; Merge %xmm11 into %xmm15, forming the remainder block. por xmm15,xmm11 - - +; The block of ciphertext + extra_in is ready to be included in the +; Poly1305 state. movq r13,xmm15 pextrq r14,xmm15,1 add r10,r13 @@ -3745,26 +3745,26 @@ $L$load_extra_shift_loop: mov r10,r13 mov r11,r14 mov r12,r15 - and r12,3 + and r12,3 ; At this point acc2 is 2 bits at most (value of 3) mov r13,r15 and r13,-4 mov r14,r9 shrd r15,r9,2 shr r9,2 add r15,r13 - adc r9,r14 + adc r9,r14 ; No carry out since t3 is 61 bits and t1 is 63 bits add r10,r15 adc r11,r9 adc r12,0 $L$process_blocks_of_extra_in: - +; There may be additional bytes of extra_in to process. mov r9,QWORD[((288+32+160 ))+rsp] - mov rsi,QWORD[48+r9] - mov r8,QWORD[56+r9] + mov rsi,QWORD[48+r9] ; extra_in + mov r8,QWORD[56+r9] ; extra_in_len mov rcx,r8 - shr r8,4 + shr r8,4 ; number of blocks $L$process_extra_hash_loop: jz NEAR process_extra_in_trailer @@ -3797,14 +3797,14 @@ $L$process_extra_hash_loop: mov r10,r13 mov r11,r14 mov r12,r15 - and r12,3 + and r12,3 ; At this point acc2 is 2 bits at most (value of 3) mov r13,r15 and r13,-4 mov r14,r9 shrd r15,r9,2 shr r9,2 add r15,r13 - adc r9,r14 + adc r9,r14 ; No carry out since t3 is 61 bits and t1 is 63 bits add r10,r15 adc r11,r9 adc r12,0 @@ -3813,7 +3813,7 @@ $L$process_extra_hash_loop: sub r8,1 jmp NEAR $L$process_extra_hash_loop process_extra_in_trailer: - and rcx,15 + and rcx,15 ; remaining num bytes (<16) of extra_in mov rbx,rcx jz NEAR $L$do_length_block lea rsi,[((-1))+rcx*1+rsi] @@ -3826,7 +3826,7 @@ $L$process_extra_in_trailer_load: jnz NEAR $L$process_extra_in_trailer_load $L$process_partial_block: - +; %xmm15 contains %rbx bytes of data to be fed into Poly1305. %rbx != 0 lea r15,[$L$and_masks] shl rbx,4 pand xmm15,XMMWORD[((-16))+rbx*1+r15] @@ -3861,14 +3861,14 @@ $L$process_partial_block: mov r10,r13 mov r11,r14 mov r12,r15 - and r12,3 + and r12,3 ; At this point acc2 is 2 bits at most (value of 3) mov r13,r15 and r13,-4 mov r14,r9 shrd r15,r9,2 shr r9,2 add r15,r13 - adc r9,r14 + adc r9,r14 ; No carry out since t3 is 61 bits and t1 is 63 bits add r10,r15 adc r11,r9 adc r12,0 @@ -3904,19 +3904,19 @@ $L$do_length_block: mov r10,r13 mov r11,r14 mov r12,r15 - and r12,3 + and r12,3 ; At this point acc2 is 2 bits at most (value of 3) mov r13,r15 and r13,-4 mov r14,r9 shrd r15,r9,2 shr r9,2 add r15,r13 - adc r9,r14 + adc r9,r14 ; No carry out since t3 is 61 bits and t1 is 63 bits add r10,r15 adc r11,r9 adc r12,0 - +; Final reduce mov r13,r10 mov r14,r11 mov r15,r12 @@ -3926,7 +3926,7 @@ $L$do_length_block: cmovc r10,r13 cmovc r11,r14 cmovc r12,r15 - +; Add in s part of the key add r10,QWORD[((0+160+16))+rbp] adc r11,QWORD[((8+160+16))+rbp] @@ -3944,7 +3944,7 @@ $L$do_length_block: add rsp,288 + 160 + 32 - +; The tag replaces the key on return pop r9 mov QWORD[r9],r10 @@ -3964,7 +3964,7 @@ $L$do_length_block: mov rdi,QWORD[8+rsp] ;WIN64 epilogue mov rsi,QWORD[16+rsp] ret - +; ############################################################################### $L$seal_sse_128: movdqu xmm0,XMMWORD[$L$chacha20_consts] @@ -4127,18 +4127,18 @@ $L$seal_sse_128_rounds: paddd xmm12,xmm15 paddd xmm15,XMMWORD[$L$sse_inc] paddd xmm13,xmm15 - +; Clamp and store the key pand xmm2,XMMWORD[$L$clamp] movdqa XMMWORD[(160+0)+rbp],xmm2 movdqa XMMWORD[(160+16)+rbp],xmm6 - +; Hash mov r8,r8 call poly_hash_ad_internal jmp NEAR $L$seal_sse_128_tail_xor $L$SEH_end_chacha20_poly1305_seal_sse41: - +; ############################################################################## global chacha20_poly1305_open_avx2 ALIGN 64 @@ -4169,8 +4169,8 @@ _CET_ENDBR push r15 - - +; We write the calculated authenticator back to keyp at the end, so save +; the pointer on the stack too. push r9 sub rsp,288 + 160 + 32 @@ -4257,16 +4257,16 @@ $L$open_avx2_init_rounds: vpaddd ymm12,ymm12,YMMWORD[((160+160))+rbp] vperm2i128 ymm3,ymm4,ymm0,0x02 - +; Clamp and store key vpand ymm3,ymm3,YMMWORD[$L$clamp] vmovdqa YMMWORD[(160+0)+rbp],ymm3 - +; Stream for the first 64 bytes vperm2i128 ymm0,ymm4,ymm0,0x13 vperm2i128 ymm4,ymm12,ymm8,0x13 - +; Hash AD + first 64 bytes mov r8,r8 call poly_hash_ad_internal - +; Hash first 64 bytes xor rcx,rcx $L$open_avx2_init_hash: add r10,QWORD[((0+0))+rcx*1+rsi] @@ -4298,14 +4298,14 @@ $L$open_avx2_init_hash: mov r10,r13 mov r11,r14 mov r12,r15 - and r12,3 + and r12,3 ; At this point acc2 is 2 bits at most (value of 3) mov r13,r15 and r13,-4 mov r14,r9 shrd r15,r9,2 shr r9,2 add r15,r13 - adc r9,r14 + adc r9,r14 ; No carry out since t3 is 61 bits and t1 is 63 bits add r10,r15 adc r11,r9 adc r12,0 @@ -4313,17 +4313,17 @@ $L$open_avx2_init_hash: add rcx,16 cmp rcx,2*32 jne NEAR $L$open_avx2_init_hash - +; Decrypt first 64 bytes vpxor ymm0,ymm0,YMMWORD[rsi] vpxor ymm4,ymm4,YMMWORD[32+rsi] - +; Store first 64 bytes of decrypted data vmovdqu YMMWORD[rdi],ymm0 vmovdqu YMMWORD[32+rdi],ymm4 lea rsi,[64+rsi] lea rdi,[64+rdi] sub rbx,2*32 $L$open_avx2_main_loop: - +; Hash and decrypt 512 bytes each iteration cmp rbx,16*32 jb NEAR $L$open_avx2_main_loop_done vmovdqa ymm0,YMMWORD[$L$chacha20_consts] @@ -4413,14 +4413,14 @@ $L$open_avx2_main_loop_rounds: mov r10,r13 mov r11,r14 mov r12,r15 - and r12,3 + and r12,3 ; At this point acc2 is 2 bits at most (value of 3) mov r13,r15 and r13,-4 mov r14,r9 shrd r15,r9,2 shr r9,2 add r15,r13 - adc r9,r14 + adc r9,r14 ; No carry out since t3 is 61 bits and t1 is 63 bits add r10,r15 adc r11,r9 adc r12,0 @@ -4508,14 +4508,14 @@ $L$open_avx2_main_loop_rounds: mov r10,r13 mov r11,r14 mov r12,r15 - and r12,3 + and r12,3 ; At this point acc2 is 2 bits at most (value of 3) mov r13,r15 and r13,-4 mov r14,r9 shrd r15,r9,2 shr r9,2 add r15,r13 - adc r9,r14 + adc r9,r14 ; No carry out since t3 is 61 bits and t1 is 63 bits add r10,r15 adc r11,r9 adc r12,0 @@ -4603,14 +4603,14 @@ $L$open_avx2_main_loop_rounds: mov r10,r13 mov r11,r14 mov r12,r15 - and r12,3 + and r12,3 ; At this point acc2 is 2 bits at most (value of 3) mov r13,r15 and r13,-4 mov r14,r9 shrd r15,r9,2 shr r9,2 add r15,r13 - adc r9,r14 + adc r9,r14 ; No carry out since t3 is 61 bits and t1 is 63 bits add r10,r15 adc r11,r9 adc r12,0 @@ -4679,14 +4679,14 @@ $L$open_avx2_main_loop_rounds: mov r10,r13 mov r11,r14 mov r12,r15 - and r12,3 + and r12,3 ; At this point acc2 is 2 bits at most (value of 3) mov r13,r15 and r13,-4 mov r14,r9 shrd r15,r9,2 shr r9,2 add r15,r13 - adc r9,r14 + adc r9,r14 ; No carry out since t3 is 61 bits and t1 is 63 bits add r10,r15 adc r11,r9 adc r12,0 @@ -4743,14 +4743,14 @@ $L$open_avx2_main_loop_rounds: mov r10,r13 mov r11,r14 mov r12,r15 - and r12,3 + and r12,3 ; At this point acc2 is 2 bits at most (value of 3) mov r13,r15 and r13,-4 mov r14,r9 shrd r15,r9,2 shr r9,2 add r15,r13 - adc r9,r14 + adc r9,r14 ; No carry out since t3 is 61 bits and t1 is 63 bits add r10,r15 adc r11,r9 adc r12,0 @@ -4793,7 +4793,7 @@ $L$open_avx2_main_loop_done: mov rcx,rbx and rcx,-16 test rcx,rcx - je NEAR $L$open_avx2_tail_128_rounds + je NEAR $L$open_avx2_tail_128_rounds ; Have nothing to hash $L$open_avx2_tail_128_rounds_and_x1hash: add r10,QWORD[((0+0))+r8*1+rsi] adc r11,QWORD[((8+0))+r8*1+rsi] @@ -4824,14 +4824,14 @@ $L$open_avx2_tail_128_rounds_and_x1hash: mov r10,r13 mov r11,r14 mov r12,r15 - and r12,3 + and r12,3 ; At this point acc2 is 2 bits at most (value of 3) mov r13,r15 and r13,-4 mov r14,r9 shrd r15,r9,2 shr r9,2 add r15,r13 - adc r9,r14 + adc r9,r14 ; No carry out since t3 is 61 bits and t1 is 63 bits add r10,r15 adc r11,r9 adc r12,0 @@ -4892,7 +4892,7 @@ $L$open_avx2_tail_128_rounds: vmovdqa ymm8,ymm3 jmp NEAR $L$open_avx2_tail_128_xor - +; ############################################################################## $L$open_avx2_tail_256: vmovdqa ymm0,YMMWORD[$L$chacha20_consts] vmovdqa ymm4,YMMWORD[((160+64))+rbp] @@ -4938,14 +4938,14 @@ $L$open_avx2_tail_256_rounds_and_x1hash: mov r10,r13 mov r11,r14 mov r12,r15 - and r12,3 + and r12,3 ; At this point acc2 is 2 bits at most (value of 3) mov r13,r15 and r13,-4 mov r14,r9 shrd r15,r9,2 shr r9,2 add r15,r13 - adc r9,r14 + adc r9,r14 ; No carry out since t3 is 61 bits and t1 is 63 bits add r10,r15 adc r11,r9 adc r12,0 @@ -5084,14 +5084,14 @@ $L$open_avx2_tail_256_hash: mov r10,r13 mov r11,r14 mov r12,r15 - and r12,3 + and r12,3 ; At this point acc2 is 2 bits at most (value of 3) mov r13,r15 and r13,-4 mov r14,r9 shrd r15,r9,2 shr r9,2 add r15,r13 - adc r9,r14 + adc r9,r14 ; No carry out since t3 is 61 bits and t1 is 63 bits add r10,r15 adc r11,r9 adc r12,0 @@ -5129,7 +5129,7 @@ $L$open_avx2_tail_256_done: lea rdi,[128+rdi] sub rbx,4*32 jmp NEAR $L$open_avx2_tail_128_xor - +; ############################################################################## $L$open_avx2_tail_384: vmovdqa ymm0,YMMWORD[$L$chacha20_consts] vmovdqa ymm4,YMMWORD[((160+64))+rbp] @@ -5181,14 +5181,14 @@ $L$open_avx2_tail_384_rounds_and_x2hash: mov r10,r13 mov r11,r14 mov r12,r15 - and r12,3 + and r12,3 ; At this point acc2 is 2 bits at most (value of 3) mov r13,r15 and r13,-4 mov r14,r9 shrd r15,r9,2 shr r9,2 add r15,r13 - adc r9,r14 + adc r9,r14 ; No carry out since t3 is 61 bits and t1 is 63 bits add r10,r15 adc r11,r9 adc r12,0 @@ -5281,14 +5281,14 @@ $L$open_avx2_tail_384_rounds_and_x1hash: mov r10,r13 mov r11,r14 mov r12,r15 - and r12,3 + and r12,3 ; At this point acc2 is 2 bits at most (value of 3) mov r13,r15 and r13,-4 mov r14,r9 shrd r15,r9,2 shr r9,2 add r15,r13 - adc r9,r14 + adc r9,r14 ; No carry out since t3 is 61 bits and t1 is 63 bits add r10,r15 adc r11,r9 adc r12,0 @@ -5387,14 +5387,14 @@ $L$open_avx2_384_tail_hash: mov r10,r13 mov r11,r14 mov r12,r15 - and r12,3 + and r12,3 ; At this point acc2 is 2 bits at most (value of 3) mov r13,r15 and r13,-4 mov r14,r9 shrd r15,r9,2 shr r9,2 add r15,r13 - adc r9,r14 + adc r9,r14 ; No carry out since t3 is 61 bits and t1 is 63 bits add r10,r15 adc r11,r9 adc r12,0 @@ -5448,7 +5448,7 @@ $L$open_avx2_384_tail_done: lea rdi,[256+rdi] sub rbx,8*32 jmp NEAR $L$open_avx2_tail_128_xor - +; ############################################################################## $L$open_avx2_tail_512: vmovdqa ymm0,YMMWORD[$L$chacha20_consts] vmovdqa ymm4,YMMWORD[((160+64))+rbp] @@ -5504,14 +5504,14 @@ $L$open_avx2_tail_512_rounds_and_x2hash: mov r10,r13 mov r11,r14 mov r12,r15 - and r12,3 + and r12,3 ; At this point acc2 is 2 bits at most (value of 3) mov r13,r15 and r13,-4 mov r14,r9 shrd r15,r9,2 shr r9,2 add r15,r13 - adc r9,r14 + adc r9,r14 ; No carry out since t3 is 61 bits and t1 is 63 bits add r10,r15 adc r11,r9 adc r12,0 @@ -5577,14 +5577,14 @@ $L$open_avx2_tail_512_rounds_and_x1hash: mov r10,r13 mov r11,r14 mov r12,r15 - and r12,3 + and r12,3 ; At this point acc2 is 2 bits at most (value of 3) mov r13,r15 and r13,-4 mov r14,r9 shrd r15,r9,2 shr r9,2 add r15,r13 - adc r9,r14 + adc r9,r14 ; No carry out since t3 is 61 bits and t1 is 63 bits add r10,r15 adc r11,r9 adc r12,0 @@ -5658,14 +5658,14 @@ $L$open_avx2_tail_512_rounds_and_x1hash: mov r10,r13 mov r11,r14 mov r12,r15 - and r12,3 + and r12,3 ; At this point acc2 is 2 bits at most (value of 3) mov r13,r15 and r13,-4 mov r14,r9 shrd r15,r9,2 shr r9,2 add r15,r13 - adc r9,r14 + adc r9,r14 ; No carry out since t3 is 61 bits and t1 is 63 bits add r10,r15 adc r11,r9 adc r12,0 @@ -5784,14 +5784,14 @@ $L$open_avx2_tail_512_hash: mov r10,r13 mov r11,r14 mov r12,r15 - and r12,3 + and r12,3 ; At this point acc2 is 2 bits at most (value of 3) mov r13,r15 and r13,-4 mov r14,r9 shrd r15,r9,2 shr r9,2 add r15,r13 - adc r9,r14 + adc r9,r14 ; No carry out since t3 is 61 bits and t1 is 63 bits add r10,r15 adc r11,r9 adc r12,0 @@ -5882,7 +5882,7 @@ $L$open_avx2_tail_32_xor: vmovdqa xmm1,xmm0 jb NEAR $L$open_avx2_exit sub rbx,16 - +; load for decryption vpxor xmm1,xmm0,XMMWORD[rsi] vmovdqu XMMWORD[rdi],xmm1 lea rsi,[16+rsi] @@ -5892,7 +5892,7 @@ $L$open_avx2_tail_32_xor: $L$open_avx2_exit: vzeroupper jmp NEAR $L$open_sse_tail_16 - +; ############################################################################## $L$open_avx2_192: vmovdqa ymm1,ymm0 vmovdqa ymm2,ymm0 @@ -5993,10 +5993,10 @@ $L$open_avx2_192_rounds: vpaddd ymm12,ymm12,ymm11 vpaddd ymm13,ymm13,ymm15 vperm2i128 ymm3,ymm4,ymm0,0x02 - +; Clamp and store the key vpand ymm3,ymm3,YMMWORD[$L$clamp] vmovdqa YMMWORD[(160+0)+rbp],ymm3 - +; Stream for up to 192 bytes vperm2i128 ymm0,ymm4,ymm0,0x13 vperm2i128 ymm4,ymm12,ymm8,0x13 vperm2i128 ymm8,ymm5,ymm1,0x02 @@ -6039,14 +6039,14 @@ $L$open_avx2_short_hash_and_xor_loop: mov r10,r13 mov r11,r14 mov r12,r15 - and r12,3 + and r12,3 ; At this point acc2 is 2 bits at most (value of 3) mov r13,r15 and r13,-4 mov r14,r9 shrd r15,r9,2 shr r9,2 add r15,r13 - adc r9,r14 + adc r9,r14 ; No carry out since t3 is 61 bits and t1 is 63 bits add r10,r15 adc r11,r9 adc r12,0 @@ -6079,24 +6079,24 @@ $L$open_avx2_short_hash_and_xor_loop: mov r10,r13 mov r11,r14 mov r12,r15 - and r12,3 + and r12,3 ; At this point acc2 is 2 bits at most (value of 3) mov r13,r15 and r13,-4 mov r14,r9 shrd r15,r9,2 shr r9,2 add r15,r13 - adc r9,r14 + adc r9,r14 ; No carry out since t3 is 61 bits and t1 is 63 bits add r10,r15 adc r11,r9 adc r12,0 - +; Load + decrypt vpxor ymm0,ymm0,YMMWORD[rsi] vmovdqu YMMWORD[rdi],ymm0 lea rsi,[32+rsi] lea rdi,[32+rdi] - +; Shift stream vmovdqa ymm0,ymm4 vmovdqa ymm4,ymm8 vmovdqa ymm8,ymm12 @@ -6141,14 +6141,14 @@ $L$open_avx2_short_tail_32: mov r10,r13 mov r11,r14 mov r12,r15 - and r12,3 + and r12,3 ; At this point acc2 is 2 bits at most (value of 3) mov r13,r15 and r13,-4 mov r14,r9 shrd r15,r9,2 shr r9,2 add r15,r13 - adc r9,r14 + adc r9,r14 ; No carry out since t3 is 61 bits and t1 is 63 bits add r10,r15 adc r11,r9 adc r12,0 @@ -6161,7 +6161,7 @@ $L$open_avx2_short_tail_32: $L$open_avx2_short_tail_32_exit: vzeroupper jmp NEAR $L$open_sse_tail_16 - +; ############################################################################## $L$open_avx2_320: vmovdqa ymm1,ymm0 vmovdqa ymm2,ymm0 @@ -6308,10 +6308,10 @@ $L$open_avx2_320_rounds: vpaddd ymm13,ymm13,YMMWORD[((160+192))+rbp] vpaddd ymm14,ymm14,YMMWORD[((160+224))+rbp] vperm2i128 ymm3,ymm4,ymm0,0x02 - +; Clamp and store the key vpand ymm3,ymm3,YMMWORD[$L$clamp] vmovdqa YMMWORD[(160+0)+rbp],ymm3 - +; Stream for up to 320 bytes vperm2i128 ymm0,ymm4,ymm0,0x13 vperm2i128 ymm4,ymm12,ymm8,0x13 vperm2i128 ymm8,ymm5,ymm1,0x02 @@ -6325,8 +6325,8 @@ $L$open_avx2_320_rounds: jmp NEAR $L$open_avx2_short $L$SEH_end_chacha20_poly1305_open_avx2: - - +; ############################################################################## +; ############################################################################## global chacha20_poly1305_seal_avx2 ALIGN 64 @@ -6357,8 +6357,8 @@ _CET_ENDBR push r15 - - +; We write the calculated authenticator back to keyp at the end, so save +; the pointer on the stack too. push r9 sub rsp,288 + 160 + 32 @@ -6377,7 +6377,7 @@ _CET_ENDBR movaps XMMWORD[(128+0)+rbp],xmm14 movaps XMMWORD[(144+0)+rbp],xmm15 - mov rbx,QWORD[56+r9] + mov rbx,QWORD[56+r9] ; extra_in_len add rbx,rdx mov QWORD[((0+160+32))+rbp],r8 mov QWORD[((8+160+32))+rbp],rbx @@ -6605,7 +6605,7 @@ $L$seal_avx2_init_rounds: vmovdqa YMMWORD[(160+0)+rbp],ymm15 mov r8,r8 call poly_hash_ad_internal - +; Safely store 320 bytes (otherwise would handle with optimized call) vpxor ymm3,ymm3,YMMWORD[rsi] vpxor ymm11,ymm11,YMMWORD[32+rsi] vmovdqu YMMWORD[rdi],ymm3 @@ -6985,14 +6985,14 @@ $L$seal_avx2_main_loop_rounds: mov r10,r13 mov r11,r14 mov r12,r15 - and r12,3 + and r12,3 ; At this point acc2 is 2 bits at most (value of 3) mov r13,r15 and r13,-4 mov r14,r9 shrd r15,r9,2 shr r9,2 add r15,r13 - adc r9,r14 + adc r9,r14 ; No carry out since t3 is 61 bits and t1 is 63 bits add r10,r15 adc r11,r9 adc r12,0 @@ -7082,14 +7082,14 @@ $L$seal_avx2_main_loop_rounds_entry: mov r10,r13 mov r11,r14 mov r12,r15 - and r12,3 + and r12,3 ; At this point acc2 is 2 bits at most (value of 3) mov r13,r15 and r13,-4 mov r14,r9 shrd r15,r9,2 shr r9,2 add r15,r13 - adc r9,r14 + adc r9,r14 ; No carry out since t3 is 61 bits and t1 is 63 bits add r10,r15 adc r11,r9 adc r12,0 @@ -7177,14 +7177,14 @@ $L$seal_avx2_main_loop_rounds_entry: mov r10,r13 mov r11,r14 mov r12,r15 - and r12,3 + and r12,3 ; At this point acc2 is 2 bits at most (value of 3) mov r13,r15 and r13,-4 mov r14,r9 shrd r15,r9,2 shr r9,2 add r15,r13 - adc r9,r14 + adc r9,r14 ; No carry out since t3 is 61 bits and t1 is 63 bits add r10,r15 adc r11,r9 adc r12,0 @@ -7232,14 +7232,14 @@ $L$seal_avx2_main_loop_rounds_entry: mov r10,r13 mov r11,r14 mov r12,r15 - and r12,3 + and r12,3 ; At this point acc2 is 2 bits at most (value of 3) mov r13,r15 and r13,-4 mov r14,r9 shrd r15,r9,2 shr r9,2 add r15,r13 - adc r9,r14 + adc r9,r14 ; No carry out since t3 is 61 bits and t1 is 63 bits add r10,r15 adc r11,r9 adc r12,0 @@ -7265,14 +7265,14 @@ $L$seal_avx2_main_loop_rounds_entry: mov r10,r13 mov r11,r14 mov r12,r15 - and r12,3 + and r12,3 ; At this point acc2 is 2 bits at most (value of 3) mov r13,r15 and r13,-4 mov r14,r9 shrd r15,r9,2 shr r9,2 add r15,r13 - adc r9,r14 + adc r9,r14 ; No carry out since t3 is 61 bits and t1 is 63 bits add r10,r15 adc r11,r9 adc r12,0 @@ -7356,14 +7356,14 @@ $L$seal_avx2_main_loop_rounds_entry: mov r10,r13 mov r11,r14 mov r12,r15 - and r12,3 + and r12,3 ; At this point acc2 is 2 bits at most (value of 3) mov r13,r15 and r13,-4 mov r14,r9 shrd r15,r9,2 shr r9,2 add r15,r13 - adc r9,r14 + adc r9,r14 ; No carry out since t3 is 61 bits and t1 is 63 bits add r10,r15 adc r11,r9 adc r12,0 @@ -7389,14 +7389,14 @@ $L$seal_avx2_main_loop_rounds_entry: mov r10,r13 mov r11,r14 mov r12,r15 - and r12,3 + and r12,3 ; At this point acc2 is 2 bits at most (value of 3) mov r13,r15 and r13,-4 mov r14,r9 shrd r15,r9,2 shr r9,2 add r15,r13 - adc r9,r14 + adc r9,r14 ; No carry out since t3 is 61 bits and t1 is 63 bits add r10,r15 adc r11,r9 adc r12,0 @@ -7411,7 +7411,7 @@ $L$seal_avx2_main_loop_rounds_entry: ja NEAR $L$seal_avx2_tail_384 cmp rbx,4*32 ja NEAR $L$seal_avx2_tail_256 - +; ############################################################################## $L$seal_avx2_tail_128: vmovdqa ymm0,YMMWORD[$L$chacha20_consts] vmovdqa ymm4,YMMWORD[((160+64))+rbp] @@ -7443,14 +7443,14 @@ $L$seal_avx2_tail_128_rounds_and_3xhash: mov r10,r13 mov r11,r14 mov r12,r15 - and r12,3 + and r12,3 ; At this point acc2 is 2 bits at most (value of 3) mov r13,r15 and r13,-4 mov r14,r9 shrd r15,r9,2 shr r9,2 add r15,r13 - adc r9,r14 + adc r9,r14 ; No carry out since t3 is 61 bits and t1 is 63 bits add r10,r15 adc r11,r9 adc r12,0 @@ -7498,14 +7498,14 @@ $L$seal_avx2_tail_128_rounds_and_2xhash: mov r10,r13 mov r11,r14 mov r12,r15 - and r12,3 + and r12,3 ; At this point acc2 is 2 bits at most (value of 3) mov r13,r15 and r13,-4 mov r14,r9 shrd r15,r9,2 shr r9,2 add r15,r13 - adc r9,r14 + adc r9,r14 ; No carry out since t3 is 61 bits and t1 is 63 bits add r10,r15 adc r11,r9 adc r12,0 @@ -7550,14 +7550,14 @@ $L$seal_avx2_tail_128_rounds_and_2xhash: mov r10,r13 mov r11,r14 mov r12,r15 - and r12,3 + and r12,3 ; At this point acc2 is 2 bits at most (value of 3) mov r13,r15 and r13,-4 mov r14,r9 shrd r15,r9,2 shr r9,2 add r15,r13 - adc r9,r14 + adc r9,r14 ; No carry out since t3 is 61 bits and t1 is 63 bits add r10,r15 adc r11,r9 adc r12,0 @@ -7578,7 +7578,7 @@ $L$seal_avx2_tail_128_rounds_and_2xhash: vmovdqa ymm8,ymm3 jmp NEAR $L$seal_avx2_short_loop - +; ############################################################################## $L$seal_avx2_tail_256: vmovdqa ymm0,YMMWORD[$L$chacha20_consts] vmovdqa ymm4,YMMWORD[((160+64))+rbp] @@ -7622,14 +7622,14 @@ $L$seal_avx2_tail_256_rounds_and_3xhash: mov r10,r13 mov r11,r14 mov r12,r15 - and r12,3 + and r12,3 ; At this point acc2 is 2 bits at most (value of 3) mov r13,r15 and r13,-4 mov r14,r9 shrd r15,r9,2 shr r9,2 add r15,r13 - adc r9,r14 + adc r9,r14 ; No carry out since t3 is 61 bits and t1 is 63 bits add r10,r15 adc r11,r9 adc r12,0 @@ -7703,14 +7703,14 @@ $L$seal_avx2_tail_256_rounds_and_2xhash: mov r10,r13 mov r11,r14 mov r12,r15 - and r12,3 + and r12,3 ; At this point acc2 is 2 bits at most (value of 3) mov r13,r15 and r13,-4 mov r14,r9 shrd r15,r9,2 shr r9,2 add r15,r13 - adc r9,r14 + adc r9,r14 ; No carry out since t3 is 61 bits and t1 is 63 bits add r10,r15 adc r11,r9 adc r12,0 @@ -7781,14 +7781,14 @@ $L$seal_avx2_tail_256_rounds_and_2xhash: mov r10,r13 mov r11,r14 mov r12,r15 - and r12,3 + and r12,3 ; At this point acc2 is 2 bits at most (value of 3) mov r13,r15 and r13,-4 mov r14,r9 shrd r15,r9,2 shr r9,2 add r15,r13 - adc r9,r14 + adc r9,r14 ; No carry out since t3 is 61 bits and t1 is 63 bits add r10,r15 adc r11,r9 adc r12,0 @@ -7828,7 +7828,7 @@ $L$seal_avx2_tail_256_rounds_and_2xhash: lea rsi,[128+rsi] sub rbx,4*32 jmp NEAR $L$seal_avx2_short_hash_remainder - +; ############################################################################## $L$seal_avx2_tail_384: vmovdqa ymm0,YMMWORD[$L$chacha20_consts] vmovdqa ymm4,YMMWORD[((160+64))+rbp] @@ -7877,14 +7877,14 @@ $L$seal_avx2_tail_384_rounds_and_3xhash: mov r10,r13 mov r11,r14 mov r12,r15 - and r12,3 + and r12,3 ; At this point acc2 is 2 bits at most (value of 3) mov r13,r15 and r13,-4 mov r14,r9 shrd r15,r9,2 shr r9,2 add r15,r13 - adc r9,r14 + adc r9,r14 ; No carry out since t3 is 61 bits and t1 is 63 bits add r10,r15 adc r11,r9 adc r12,0 @@ -7958,14 +7958,14 @@ $L$seal_avx2_tail_384_rounds_and_2xhash: mov r10,r13 mov r11,r14 mov r12,r15 - and r12,3 + and r12,3 ; At this point acc2 is 2 bits at most (value of 3) mov r13,r15 and r13,-4 mov r14,r9 shrd r15,r9,2 shr r9,2 add r15,r13 - adc r9,r14 + adc r9,r14 ; No carry out since t3 is 61 bits and t1 is 63 bits add r10,r15 adc r11,r9 adc r12,0 @@ -8036,14 +8036,14 @@ $L$seal_avx2_tail_384_rounds_and_2xhash: mov r10,r13 mov r11,r14 mov r12,r15 - and r12,3 + and r12,3 ; At this point acc2 is 2 bits at most (value of 3) mov r13,r15 and r13,-4 mov r14,r9 shrd r15,r9,2 shr r9,2 add r15,r13 - adc r9,r14 + adc r9,r14 ; No carry out since t3 is 61 bits and t1 is 63 bits add r10,r15 adc r11,r9 adc r12,0 @@ -8137,7 +8137,7 @@ $L$seal_avx2_tail_384_rounds_and_2xhash: lea rsi,[256+rsi] sub rbx,8*32 jmp NEAR $L$seal_avx2_short_hash_remainder - +; ############################################################################## $L$seal_avx2_tail_512: vmovdqa ymm0,YMMWORD[$L$chacha20_consts] vmovdqa ymm4,YMMWORD[((160+64))+rbp] @@ -8184,14 +8184,14 @@ $L$seal_avx2_tail_512_rounds_and_3xhash: mov r10,r13 mov r11,r14 mov r12,r15 - and r12,3 + and r12,3 ; At this point acc2 is 2 bits at most (value of 3) mov r13,r15 and r13,-4 mov r14,r9 shrd r15,r9,2 shr r9,2 add r15,r13 - adc r9,r14 + adc r9,r14 ; No carry out since t3 is 61 bits and t1 is 63 bits add r10,r15 adc r11,r9 adc r12,0 @@ -8320,14 +8320,14 @@ $L$seal_avx2_tail_512_rounds_and_2xhash: mov r10,r13 mov r11,r14 mov r12,r15 - and r12,3 + and r12,3 ; At this point acc2 is 2 bits at most (value of 3) mov r13,r15 and r13,-4 mov r14,r9 shrd r15,r9,2 shr r9,2 add r15,r13 - adc r9,r14 + adc r9,r14 ; No carry out since t3 is 61 bits and t1 is 63 bits add r10,r15 adc r11,r9 adc r12,0 @@ -8453,14 +8453,14 @@ $L$seal_avx2_tail_512_rounds_and_2xhash: mov r10,r13 mov r11,r14 mov r12,r15 - and r12,3 + and r12,3 ; At this point acc2 is 2 bits at most (value of 3) mov r13,r15 and r13,-4 mov r14,r9 shrd r15,r9,2 shr r9,2 add r15,r13 - adc r9,r14 + adc r9,r14 ; No carry out since t3 is 61 bits and t1 is 63 bits add r10,r15 adc r11,r9 adc r12,0 @@ -8536,7 +8536,7 @@ $L$seal_avx2_tail_512_rounds_and_2xhash: lea rsi,[384+rsi] sub rbx,12*32 jmp NEAR $L$seal_avx2_short_hash_remainder - +; ############################################################################### $L$seal_avx2_320: vmovdqa ymm1,ymm0 vmovdqa ymm2,ymm0 @@ -8683,10 +8683,10 @@ $L$seal_avx2_320_rounds: vpaddd ymm13,ymm13,YMMWORD[((160+192))+rbp] vpaddd ymm14,ymm14,YMMWORD[((160+224))+rbp] vperm2i128 ymm3,ymm4,ymm0,0x02 - +; Clamp and store the key vpand ymm3,ymm3,YMMWORD[$L$clamp] vmovdqa YMMWORD[(160+0)+rbp],ymm3 - +; Stream for up to 320 bytes vperm2i128 ymm0,ymm4,ymm0,0x13 vperm2i128 ymm4,ymm12,ymm8,0x13 vperm2i128 ymm8,ymm5,ymm1,0x02 @@ -8698,7 +8698,7 @@ $L$seal_avx2_320_rounds: vperm2i128 ymm2,ymm6,ymm2,0x13 vperm2i128 ymm6,ymm14,ymm10,0x13 jmp NEAR $L$seal_avx2_short - +; ############################################################################### $L$seal_avx2_192: vmovdqa ymm1,ymm0 vmovdqa ymm2,ymm0 @@ -8799,10 +8799,10 @@ $L$seal_avx2_192_rounds: vpaddd ymm12,ymm12,ymm11 vpaddd ymm13,ymm13,ymm15 vperm2i128 ymm3,ymm4,ymm0,0x02 - +; Clamp and store the key vpand ymm3,ymm3,YMMWORD[$L$clamp] vmovdqa YMMWORD[(160+0)+rbp],ymm3 - +; Stream for up to 192 bytes vperm2i128 ymm0,ymm4,ymm0,0x13 vperm2i128 ymm4,ymm12,ymm8,0x13 vperm2i128 ymm8,ymm5,ymm1,0x02 @@ -8845,14 +8845,14 @@ $L$seal_avx2_short_hash_remainder: mov r10,r13 mov r11,r14 mov r12,r15 - and r12,3 + and r12,3 ; At this point acc2 is 2 bits at most (value of 3) mov r13,r15 and r13,-4 mov r14,r9 shrd r15,r9,2 shr r9,2 add r15,r13 - adc r9,r14 + adc r9,r14 ; No carry out since t3 is 61 bits and t1 is 63 bits add r10,r15 adc r11,r9 adc r12,0 @@ -8864,11 +8864,11 @@ $L$seal_avx2_short_loop: cmp rbx,32 jb NEAR $L$seal_avx2_short_tail sub rbx,32 - +; Encrypt vpxor ymm0,ymm0,YMMWORD[rsi] vmovdqu YMMWORD[rdi],ymm0 lea rsi,[32+rsi] - +; Load + hash add r10,QWORD[((0+0))+rdi] adc r11,QWORD[((8+0))+rdi] adc r12,1 @@ -8898,14 +8898,14 @@ $L$seal_avx2_short_loop: mov r10,r13 mov r11,r14 mov r12,r15 - and r12,3 + and r12,3 ; At this point acc2 is 2 bits at most (value of 3) mov r13,r15 and r13,-4 mov r14,r9 shrd r15,r9,2 shr r9,2 add r15,r13 - adc r9,r14 + adc r9,r14 ; No carry out since t3 is 61 bits and t1 is 63 bits add r10,r15 adc r11,r9 adc r12,0 @@ -8938,20 +8938,20 @@ $L$seal_avx2_short_loop: mov r10,r13 mov r11,r14 mov r12,r15 - and r12,3 + and r12,3 ; At this point acc2 is 2 bits at most (value of 3) mov r13,r15 and r13,-4 mov r14,r9 shrd r15,r9,2 shr r9,2 add r15,r13 - adc r9,r14 + adc r9,r14 ; No carry out since t3 is 61 bits and t1 is 63 bits add r10,r15 adc r11,r9 adc r12,0 lea rdi,[32+rdi] - +; Shift stream vmovdqa ymm0,ymm4 vmovdqa ymm4,ymm8 vmovdqa ymm8,ymm12 @@ -8998,14 +8998,14 @@ $L$seal_avx2_short_tail: mov r10,r13 mov r11,r14 mov r12,r15 - and r12,3 + and r12,3 ; At this point acc2 is 2 bits at most (value of 3) mov r13,r15 and r13,-4 mov r14,r9 shrd r15,r9,2 shr r9,2 add r15,r13 - adc r9,r14 + adc r9,r14 ; No carry out since t3 is 61 bits and t1 is 63 bits add r10,r15 adc r11,r9 adc r12,0 diff --git a/third_party/boringssl/gen/crypto/md5-x86_64-apple.S b/third_party/boringssl/gen/crypto/md5-x86_64-apple.S index e4c02415e..cebbe92e2 100644 --- a/third_party/boringssl/gen/crypto/md5-x86_64-apple.S +++ b/third_party/boringssl/gen/crypto/md5-x86_64-apple.S @@ -25,651 +25,651 @@ _CET_ENDBR L$prologue: - - - - movq %rdi,%rbp - shlq $6,%rdx - leaq (%rsi,%rdx,1),%rdi - movl 0(%rbp),%eax - movl 4(%rbp),%ebx - movl 8(%rbp),%ecx - movl 12(%rbp),%edx - - - - - - - - cmpq %rdi,%rsi - je L$end - - -L$loop: +// rdi = arg #1 (ctx, MD5_CTX pointer) +// rsi = arg #2 (ptr, data pointer) +// rdx = arg #3 (nbr, number of 16-word blocks to process) + movq %rdi,%rbp // rbp = ctx + shlq $6,%rdx // rdx = nbr in bytes + leaq (%rsi,%rdx,1),%rdi // rdi = end + movl 0(%rbp),%eax // eax = ctx->A + movl 4(%rbp),%ebx // ebx = ctx->B + movl 8(%rbp),%ecx // ecx = ctx->C + movl 12(%rbp),%edx // edx = ctx->D +// end is 'rdi' +// ptr is 'rsi' +// A is 'eax' +// B is 'ebx' +// C is 'ecx' +// D is 'edx' + + cmpq %rdi,%rsi // cmp end with ptr + je L$end // jmp if ptr == end + +// BEGIN of loop over 16-word blocks +L$loop: // save old values of A, B, C, D movl %eax,%r8d movl %ebx,%r9d movl %ecx,%r14d movl %edx,%r15d - movl 0(%rsi),%r10d - movl %edx,%r11d - xorl %ecx,%r11d - leal -680876936(%rax,%r10,1),%eax - andl %ebx,%r11d - xorl %edx,%r11d - movl 4(%rsi),%r10d - addl %r11d,%eax - roll $7,%eax - movl %ecx,%r11d - addl %ebx,%eax - xorl %ebx,%r11d - leal -389564586(%rdx,%r10,1),%edx - andl %eax,%r11d - xorl %ecx,%r11d - movl 8(%rsi),%r10d - addl %r11d,%edx - roll $12,%edx - movl %ebx,%r11d - addl %eax,%edx - xorl %eax,%r11d - leal 606105819(%rcx,%r10,1),%ecx - andl %edx,%r11d - xorl %ebx,%r11d - movl 12(%rsi),%r10d - addl %r11d,%ecx - roll $17,%ecx - movl %eax,%r11d - addl %edx,%ecx - xorl %edx,%r11d - leal -1044525330(%rbx,%r10,1),%ebx - andl %ecx,%r11d - xorl %eax,%r11d - movl 16(%rsi),%r10d - addl %r11d,%ebx - roll $22,%ebx - movl %edx,%r11d - addl %ecx,%ebx - xorl %ecx,%r11d - leal -176418897(%rax,%r10,1),%eax - andl %ebx,%r11d - xorl %edx,%r11d - movl 20(%rsi),%r10d - addl %r11d,%eax - roll $7,%eax - movl %ecx,%r11d - addl %ebx,%eax - xorl %ebx,%r11d - leal 1200080426(%rdx,%r10,1),%edx - andl %eax,%r11d - xorl %ecx,%r11d - movl 24(%rsi),%r10d - addl %r11d,%edx - roll $12,%edx - movl %ebx,%r11d - addl %eax,%edx - xorl %eax,%r11d - leal -1473231341(%rcx,%r10,1),%ecx - andl %edx,%r11d - xorl %ebx,%r11d - movl 28(%rsi),%r10d - addl %r11d,%ecx - roll $17,%ecx - movl %eax,%r11d - addl %edx,%ecx - xorl %edx,%r11d - leal -45705983(%rbx,%r10,1),%ebx - andl %ecx,%r11d - xorl %eax,%r11d - movl 32(%rsi),%r10d - addl %r11d,%ebx - roll $22,%ebx - movl %edx,%r11d - addl %ecx,%ebx - xorl %ecx,%r11d - leal 1770035416(%rax,%r10,1),%eax - andl %ebx,%r11d - xorl %edx,%r11d - movl 36(%rsi),%r10d - addl %r11d,%eax - roll $7,%eax - movl %ecx,%r11d - addl %ebx,%eax - xorl %ebx,%r11d - leal -1958414417(%rdx,%r10,1),%edx - andl %eax,%r11d - xorl %ecx,%r11d - movl 40(%rsi),%r10d - addl %r11d,%edx - roll $12,%edx - movl %ebx,%r11d - addl %eax,%edx - xorl %eax,%r11d - leal -42063(%rcx,%r10,1),%ecx - andl %edx,%r11d - xorl %ebx,%r11d - movl 44(%rsi),%r10d - addl %r11d,%ecx - roll $17,%ecx - movl %eax,%r11d - addl %edx,%ecx - xorl %edx,%r11d - leal -1990404162(%rbx,%r10,1),%ebx - andl %ecx,%r11d - xorl %eax,%r11d - movl 48(%rsi),%r10d - addl %r11d,%ebx - roll $22,%ebx - movl %edx,%r11d - addl %ecx,%ebx - xorl %ecx,%r11d - leal 1804603682(%rax,%r10,1),%eax - andl %ebx,%r11d - xorl %edx,%r11d - movl 52(%rsi),%r10d - addl %r11d,%eax - roll $7,%eax - movl %ecx,%r11d - addl %ebx,%eax - xorl %ebx,%r11d - leal -40341101(%rdx,%r10,1),%edx - andl %eax,%r11d - xorl %ecx,%r11d - movl 56(%rsi),%r10d - addl %r11d,%edx - roll $12,%edx - movl %ebx,%r11d - addl %eax,%edx - xorl %eax,%r11d - leal -1502002290(%rcx,%r10,1),%ecx - andl %edx,%r11d - xorl %ebx,%r11d - movl 60(%rsi),%r10d - addl %r11d,%ecx - roll $17,%ecx - movl %eax,%r11d - addl %edx,%ecx - xorl %edx,%r11d - leal 1236535329(%rbx,%r10,1),%ebx - andl %ecx,%r11d - xorl %eax,%r11d - movl 0(%rsi),%r10d - addl %r11d,%ebx - roll $22,%ebx - movl %edx,%r11d - addl %ecx,%ebx - movl 4(%rsi),%r10d - movl %edx,%r11d - movl %edx,%r12d - notl %r11d - leal -165796510(%rax,%r10,1),%eax - andl %ebx,%r12d - andl %ecx,%r11d - movl 24(%rsi),%r10d - orl %r11d,%r12d - movl %ecx,%r11d - addl %r12d,%eax - movl %ecx,%r12d - roll $5,%eax - addl %ebx,%eax - notl %r11d - leal -1069501632(%rdx,%r10,1),%edx - andl %eax,%r12d - andl %ebx,%r11d - movl 44(%rsi),%r10d - orl %r11d,%r12d - movl %ebx,%r11d - addl %r12d,%edx - movl %ebx,%r12d - roll $9,%edx - addl %eax,%edx - notl %r11d - leal 643717713(%rcx,%r10,1),%ecx - andl %edx,%r12d - andl %eax,%r11d - movl 0(%rsi),%r10d - orl %r11d,%r12d - movl %eax,%r11d - addl %r12d,%ecx - movl %eax,%r12d - roll $14,%ecx - addl %edx,%ecx - notl %r11d - leal -373897302(%rbx,%r10,1),%ebx - andl %ecx,%r12d - andl %edx,%r11d - movl 20(%rsi),%r10d - orl %r11d,%r12d - movl %edx,%r11d - addl %r12d,%ebx - movl %edx,%r12d - roll $20,%ebx - addl %ecx,%ebx - notl %r11d - leal -701558691(%rax,%r10,1),%eax - andl %ebx,%r12d - andl %ecx,%r11d - movl 40(%rsi),%r10d - orl %r11d,%r12d - movl %ecx,%r11d - addl %r12d,%eax - movl %ecx,%r12d - roll $5,%eax - addl %ebx,%eax - notl %r11d - leal 38016083(%rdx,%r10,1),%edx - andl %eax,%r12d - andl %ebx,%r11d - movl 60(%rsi),%r10d - orl %r11d,%r12d - movl %ebx,%r11d - addl %r12d,%edx - movl %ebx,%r12d - roll $9,%edx - addl %eax,%edx - notl %r11d - leal -660478335(%rcx,%r10,1),%ecx - andl %edx,%r12d - andl %eax,%r11d - movl 16(%rsi),%r10d - orl %r11d,%r12d - movl %eax,%r11d - addl %r12d,%ecx - movl %eax,%r12d - roll $14,%ecx - addl %edx,%ecx - notl %r11d - leal -405537848(%rbx,%r10,1),%ebx - andl %ecx,%r12d - andl %edx,%r11d - movl 36(%rsi),%r10d - orl %r11d,%r12d - movl %edx,%r11d - addl %r12d,%ebx - movl %edx,%r12d - roll $20,%ebx - addl %ecx,%ebx - notl %r11d - leal 568446438(%rax,%r10,1),%eax - andl %ebx,%r12d - andl %ecx,%r11d - movl 56(%rsi),%r10d - orl %r11d,%r12d - movl %ecx,%r11d - addl %r12d,%eax - movl %ecx,%r12d - roll $5,%eax - addl %ebx,%eax - notl %r11d - leal -1019803690(%rdx,%r10,1),%edx - andl %eax,%r12d - andl %ebx,%r11d - movl 12(%rsi),%r10d - orl %r11d,%r12d - movl %ebx,%r11d - addl %r12d,%edx - movl %ebx,%r12d - roll $9,%edx - addl %eax,%edx - notl %r11d - leal -187363961(%rcx,%r10,1),%ecx - andl %edx,%r12d - andl %eax,%r11d - movl 32(%rsi),%r10d - orl %r11d,%r12d - movl %eax,%r11d - addl %r12d,%ecx - movl %eax,%r12d - roll $14,%ecx - addl %edx,%ecx - notl %r11d - leal 1163531501(%rbx,%r10,1),%ebx - andl %ecx,%r12d - andl %edx,%r11d - movl 52(%rsi),%r10d - orl %r11d,%r12d - movl %edx,%r11d - addl %r12d,%ebx - movl %edx,%r12d - roll $20,%ebx - addl %ecx,%ebx - notl %r11d - leal -1444681467(%rax,%r10,1),%eax - andl %ebx,%r12d - andl %ecx,%r11d - movl 8(%rsi),%r10d - orl %r11d,%r12d - movl %ecx,%r11d - addl %r12d,%eax - movl %ecx,%r12d - roll $5,%eax - addl %ebx,%eax - notl %r11d - leal -51403784(%rdx,%r10,1),%edx - andl %eax,%r12d - andl %ebx,%r11d - movl 28(%rsi),%r10d - orl %r11d,%r12d - movl %ebx,%r11d - addl %r12d,%edx - movl %ebx,%r12d - roll $9,%edx - addl %eax,%edx - notl %r11d - leal 1735328473(%rcx,%r10,1),%ecx - andl %edx,%r12d - andl %eax,%r11d - movl 48(%rsi),%r10d - orl %r11d,%r12d - movl %eax,%r11d - addl %r12d,%ecx - movl %eax,%r12d - roll $14,%ecx - addl %edx,%ecx - notl %r11d - leal -1926607734(%rbx,%r10,1),%ebx - andl %ecx,%r12d - andl %edx,%r11d - movl 0(%rsi),%r10d - orl %r11d,%r12d - movl %edx,%r11d - addl %r12d,%ebx - movl %edx,%r12d - roll $20,%ebx - addl %ecx,%ebx - movl 20(%rsi),%r10d - movl %ecx,%r11d - leal -378558(%rax,%r10,1),%eax - movl 32(%rsi),%r10d - xorl %edx,%r11d - xorl %ebx,%r11d - addl %r11d,%eax - roll $4,%eax - movl %ebx,%r11d - addl %ebx,%eax - leal -2022574463(%rdx,%r10,1),%edx - movl 44(%rsi),%r10d - xorl %ecx,%r11d - xorl %eax,%r11d - addl %r11d,%edx - roll $11,%edx - movl %eax,%r11d - addl %eax,%edx - leal 1839030562(%rcx,%r10,1),%ecx - movl 56(%rsi),%r10d - xorl %ebx,%r11d - xorl %edx,%r11d - addl %r11d,%ecx - roll $16,%ecx - movl %edx,%r11d - addl %edx,%ecx - leal -35309556(%rbx,%r10,1),%ebx - movl 4(%rsi),%r10d - xorl %eax,%r11d - xorl %ecx,%r11d - addl %r11d,%ebx - roll $23,%ebx - movl %ecx,%r11d - addl %ecx,%ebx - leal -1530992060(%rax,%r10,1),%eax - movl 16(%rsi),%r10d - xorl %edx,%r11d - xorl %ebx,%r11d - addl %r11d,%eax - roll $4,%eax - movl %ebx,%r11d - addl %ebx,%eax - leal 1272893353(%rdx,%r10,1),%edx - movl 28(%rsi),%r10d - xorl %ecx,%r11d - xorl %eax,%r11d - addl %r11d,%edx - roll $11,%edx - movl %eax,%r11d - addl %eax,%edx - leal -155497632(%rcx,%r10,1),%ecx - movl 40(%rsi),%r10d - xorl %ebx,%r11d - xorl %edx,%r11d - addl %r11d,%ecx - roll $16,%ecx - movl %edx,%r11d - addl %edx,%ecx - leal -1094730640(%rbx,%r10,1),%ebx - movl 52(%rsi),%r10d - xorl %eax,%r11d - xorl %ecx,%r11d - addl %r11d,%ebx - roll $23,%ebx - movl %ecx,%r11d - addl %ecx,%ebx - leal 681279174(%rax,%r10,1),%eax - movl 0(%rsi),%r10d - xorl %edx,%r11d - xorl %ebx,%r11d - addl %r11d,%eax - roll $4,%eax - movl %ebx,%r11d - addl %ebx,%eax - leal -358537222(%rdx,%r10,1),%edx - movl 12(%rsi),%r10d - xorl %ecx,%r11d - xorl %eax,%r11d - addl %r11d,%edx - roll $11,%edx - movl %eax,%r11d - addl %eax,%edx - leal -722521979(%rcx,%r10,1),%ecx - movl 24(%rsi),%r10d - xorl %ebx,%r11d - xorl %edx,%r11d - addl %r11d,%ecx - roll $16,%ecx - movl %edx,%r11d - addl %edx,%ecx - leal 76029189(%rbx,%r10,1),%ebx - movl 36(%rsi),%r10d - xorl %eax,%r11d - xorl %ecx,%r11d - addl %r11d,%ebx - roll $23,%ebx - movl %ecx,%r11d - addl %ecx,%ebx - leal -640364487(%rax,%r10,1),%eax - movl 48(%rsi),%r10d - xorl %edx,%r11d - xorl %ebx,%r11d - addl %r11d,%eax - roll $4,%eax - movl %ebx,%r11d - addl %ebx,%eax - leal -421815835(%rdx,%r10,1),%edx - movl 60(%rsi),%r10d - xorl %ecx,%r11d - xorl %eax,%r11d - addl %r11d,%edx - roll $11,%edx - movl %eax,%r11d - addl %eax,%edx - leal 530742520(%rcx,%r10,1),%ecx - movl 8(%rsi),%r10d - xorl %ebx,%r11d - xorl %edx,%r11d - addl %r11d,%ecx - roll $16,%ecx - movl %edx,%r11d - addl %edx,%ecx - leal -995338651(%rbx,%r10,1),%ebx - movl 0(%rsi),%r10d - xorl %eax,%r11d - xorl %ecx,%r11d - addl %r11d,%ebx - roll $23,%ebx - movl %ecx,%r11d - addl %ecx,%ebx - movl 0(%rsi),%r10d + movl 0(%rsi),%r10d // (NEXT STEP) X[0] + movl %edx,%r11d // (NEXT STEP) z' = %edx + xorl %ecx,%r11d // y ^ ... + leal -680876936(%rax,%r10,1),%eax // Const + dst + ... + andl %ebx,%r11d // x & ... + xorl %edx,%r11d // z ^ ... + movl 4(%rsi),%r10d // (NEXT STEP) X[1] + addl %r11d,%eax // dst += ... + roll $7,%eax // dst <<< s + movl %ecx,%r11d // (NEXT STEP) z' = %ecx + addl %ebx,%eax // dst += x + xorl %ebx,%r11d // y ^ ... + leal -389564586(%rdx,%r10,1),%edx // Const + dst + ... + andl %eax,%r11d // x & ... + xorl %ecx,%r11d // z ^ ... + movl 8(%rsi),%r10d // (NEXT STEP) X[2] + addl %r11d,%edx // dst += ... + roll $12,%edx // dst <<< s + movl %ebx,%r11d // (NEXT STEP) z' = %ebx + addl %eax,%edx // dst += x + xorl %eax,%r11d // y ^ ... + leal 606105819(%rcx,%r10,1),%ecx // Const + dst + ... + andl %edx,%r11d // x & ... + xorl %ebx,%r11d // z ^ ... + movl 12(%rsi),%r10d // (NEXT STEP) X[3] + addl %r11d,%ecx // dst += ... + roll $17,%ecx // dst <<< s + movl %eax,%r11d // (NEXT STEP) z' = %eax + addl %edx,%ecx // dst += x + xorl %edx,%r11d // y ^ ... + leal -1044525330(%rbx,%r10,1),%ebx // Const + dst + ... + andl %ecx,%r11d // x & ... + xorl %eax,%r11d // z ^ ... + movl 16(%rsi),%r10d // (NEXT STEP) X[4] + addl %r11d,%ebx // dst += ... + roll $22,%ebx // dst <<< s + movl %edx,%r11d // (NEXT STEP) z' = %edx + addl %ecx,%ebx // dst += x + xorl %ecx,%r11d // y ^ ... + leal -176418897(%rax,%r10,1),%eax // Const + dst + ... + andl %ebx,%r11d // x & ... + xorl %edx,%r11d // z ^ ... + movl 20(%rsi),%r10d // (NEXT STEP) X[5] + addl %r11d,%eax // dst += ... + roll $7,%eax // dst <<< s + movl %ecx,%r11d // (NEXT STEP) z' = %ecx + addl %ebx,%eax // dst += x + xorl %ebx,%r11d // y ^ ... + leal 1200080426(%rdx,%r10,1),%edx // Const + dst + ... + andl %eax,%r11d // x & ... + xorl %ecx,%r11d // z ^ ... + movl 24(%rsi),%r10d // (NEXT STEP) X[6] + addl %r11d,%edx // dst += ... + roll $12,%edx // dst <<< s + movl %ebx,%r11d // (NEXT STEP) z' = %ebx + addl %eax,%edx // dst += x + xorl %eax,%r11d // y ^ ... + leal -1473231341(%rcx,%r10,1),%ecx // Const + dst + ... + andl %edx,%r11d // x & ... + xorl %ebx,%r11d // z ^ ... + movl 28(%rsi),%r10d // (NEXT STEP) X[7] + addl %r11d,%ecx // dst += ... + roll $17,%ecx // dst <<< s + movl %eax,%r11d // (NEXT STEP) z' = %eax + addl %edx,%ecx // dst += x + xorl %edx,%r11d // y ^ ... + leal -45705983(%rbx,%r10,1),%ebx // Const + dst + ... + andl %ecx,%r11d // x & ... + xorl %eax,%r11d // z ^ ... + movl 32(%rsi),%r10d // (NEXT STEP) X[8] + addl %r11d,%ebx // dst += ... + roll $22,%ebx // dst <<< s + movl %edx,%r11d // (NEXT STEP) z' = %edx + addl %ecx,%ebx // dst += x + xorl %ecx,%r11d // y ^ ... + leal 1770035416(%rax,%r10,1),%eax // Const + dst + ... + andl %ebx,%r11d // x & ... + xorl %edx,%r11d // z ^ ... + movl 36(%rsi),%r10d // (NEXT STEP) X[9] + addl %r11d,%eax // dst += ... + roll $7,%eax // dst <<< s + movl %ecx,%r11d // (NEXT STEP) z' = %ecx + addl %ebx,%eax // dst += x + xorl %ebx,%r11d // y ^ ... + leal -1958414417(%rdx,%r10,1),%edx // Const + dst + ... + andl %eax,%r11d // x & ... + xorl %ecx,%r11d // z ^ ... + movl 40(%rsi),%r10d // (NEXT STEP) X[10] + addl %r11d,%edx // dst += ... + roll $12,%edx // dst <<< s + movl %ebx,%r11d // (NEXT STEP) z' = %ebx + addl %eax,%edx // dst += x + xorl %eax,%r11d // y ^ ... + leal -42063(%rcx,%r10,1),%ecx // Const + dst + ... + andl %edx,%r11d // x & ... + xorl %ebx,%r11d // z ^ ... + movl 44(%rsi),%r10d // (NEXT STEP) X[11] + addl %r11d,%ecx // dst += ... + roll $17,%ecx // dst <<< s + movl %eax,%r11d // (NEXT STEP) z' = %eax + addl %edx,%ecx // dst += x + xorl %edx,%r11d // y ^ ... + leal -1990404162(%rbx,%r10,1),%ebx // Const + dst + ... + andl %ecx,%r11d // x & ... + xorl %eax,%r11d // z ^ ... + movl 48(%rsi),%r10d // (NEXT STEP) X[12] + addl %r11d,%ebx // dst += ... + roll $22,%ebx // dst <<< s + movl %edx,%r11d // (NEXT STEP) z' = %edx + addl %ecx,%ebx // dst += x + xorl %ecx,%r11d // y ^ ... + leal 1804603682(%rax,%r10,1),%eax // Const + dst + ... + andl %ebx,%r11d // x & ... + xorl %edx,%r11d // z ^ ... + movl 52(%rsi),%r10d // (NEXT STEP) X[13] + addl %r11d,%eax // dst += ... + roll $7,%eax // dst <<< s + movl %ecx,%r11d // (NEXT STEP) z' = %ecx + addl %ebx,%eax // dst += x + xorl %ebx,%r11d // y ^ ... + leal -40341101(%rdx,%r10,1),%edx // Const + dst + ... + andl %eax,%r11d // x & ... + xorl %ecx,%r11d // z ^ ... + movl 56(%rsi),%r10d // (NEXT STEP) X[14] + addl %r11d,%edx // dst += ... + roll $12,%edx // dst <<< s + movl %ebx,%r11d // (NEXT STEP) z' = %ebx + addl %eax,%edx // dst += x + xorl %eax,%r11d // y ^ ... + leal -1502002290(%rcx,%r10,1),%ecx // Const + dst + ... + andl %edx,%r11d // x & ... + xorl %ebx,%r11d // z ^ ... + movl 60(%rsi),%r10d // (NEXT STEP) X[15] + addl %r11d,%ecx // dst += ... + roll $17,%ecx // dst <<< s + movl %eax,%r11d // (NEXT STEP) z' = %eax + addl %edx,%ecx // dst += x + xorl %edx,%r11d // y ^ ... + leal 1236535329(%rbx,%r10,1),%ebx // Const + dst + ... + andl %ecx,%r11d // x & ... + xorl %eax,%r11d // z ^ ... + movl 0(%rsi),%r10d // (NEXT STEP) X[0] + addl %r11d,%ebx // dst += ... + roll $22,%ebx // dst <<< s + movl %edx,%r11d // (NEXT STEP) z' = %edx + addl %ecx,%ebx // dst += x + movl 4(%rsi),%r10d // (NEXT STEP) X[1] + movl %edx,%r11d // (NEXT STEP) z' = %edx + movl %edx,%r12d // (NEXT STEP) z' = %edx + notl %r11d // not z + leal -165796510(%rax,%r10,1),%eax // Const + dst + ... + andl %ebx,%r12d // x & z + andl %ecx,%r11d // y & (not z) + movl 24(%rsi),%r10d // (NEXT STEP) X[6] + orl %r11d,%r12d // (y & (not z)) | (x & z) + movl %ecx,%r11d // (NEXT STEP) z' = %ecx + addl %r12d,%eax // dst += ... + movl %ecx,%r12d // (NEXT STEP) z' = %ecx + roll $5,%eax // dst <<< s + addl %ebx,%eax // dst += x + notl %r11d // not z + leal -1069501632(%rdx,%r10,1),%edx // Const + dst + ... + andl %eax,%r12d // x & z + andl %ebx,%r11d // y & (not z) + movl 44(%rsi),%r10d // (NEXT STEP) X[11] + orl %r11d,%r12d // (y & (not z)) | (x & z) + movl %ebx,%r11d // (NEXT STEP) z' = %ebx + addl %r12d,%edx // dst += ... + movl %ebx,%r12d // (NEXT STEP) z' = %ebx + roll $9,%edx // dst <<< s + addl %eax,%edx // dst += x + notl %r11d // not z + leal 643717713(%rcx,%r10,1),%ecx // Const + dst + ... + andl %edx,%r12d // x & z + andl %eax,%r11d // y & (not z) + movl 0(%rsi),%r10d // (NEXT STEP) X[0] + orl %r11d,%r12d // (y & (not z)) | (x & z) + movl %eax,%r11d // (NEXT STEP) z' = %eax + addl %r12d,%ecx // dst += ... + movl %eax,%r12d // (NEXT STEP) z' = %eax + roll $14,%ecx // dst <<< s + addl %edx,%ecx // dst += x + notl %r11d // not z + leal -373897302(%rbx,%r10,1),%ebx // Const + dst + ... + andl %ecx,%r12d // x & z + andl %edx,%r11d // y & (not z) + movl 20(%rsi),%r10d // (NEXT STEP) X[5] + orl %r11d,%r12d // (y & (not z)) | (x & z) + movl %edx,%r11d // (NEXT STEP) z' = %edx + addl %r12d,%ebx // dst += ... + movl %edx,%r12d // (NEXT STEP) z' = %edx + roll $20,%ebx // dst <<< s + addl %ecx,%ebx // dst += x + notl %r11d // not z + leal -701558691(%rax,%r10,1),%eax // Const + dst + ... + andl %ebx,%r12d // x & z + andl %ecx,%r11d // y & (not z) + movl 40(%rsi),%r10d // (NEXT STEP) X[10] + orl %r11d,%r12d // (y & (not z)) | (x & z) + movl %ecx,%r11d // (NEXT STEP) z' = %ecx + addl %r12d,%eax // dst += ... + movl %ecx,%r12d // (NEXT STEP) z' = %ecx + roll $5,%eax // dst <<< s + addl %ebx,%eax // dst += x + notl %r11d // not z + leal 38016083(%rdx,%r10,1),%edx // Const + dst + ... + andl %eax,%r12d // x & z + andl %ebx,%r11d // y & (not z) + movl 60(%rsi),%r10d // (NEXT STEP) X[15] + orl %r11d,%r12d // (y & (not z)) | (x & z) + movl %ebx,%r11d // (NEXT STEP) z' = %ebx + addl %r12d,%edx // dst += ... + movl %ebx,%r12d // (NEXT STEP) z' = %ebx + roll $9,%edx // dst <<< s + addl %eax,%edx // dst += x + notl %r11d // not z + leal -660478335(%rcx,%r10,1),%ecx // Const + dst + ... + andl %edx,%r12d // x & z + andl %eax,%r11d // y & (not z) + movl 16(%rsi),%r10d // (NEXT STEP) X[4] + orl %r11d,%r12d // (y & (not z)) | (x & z) + movl %eax,%r11d // (NEXT STEP) z' = %eax + addl %r12d,%ecx // dst += ... + movl %eax,%r12d // (NEXT STEP) z' = %eax + roll $14,%ecx // dst <<< s + addl %edx,%ecx // dst += x + notl %r11d // not z + leal -405537848(%rbx,%r10,1),%ebx // Const + dst + ... + andl %ecx,%r12d // x & z + andl %edx,%r11d // y & (not z) + movl 36(%rsi),%r10d // (NEXT STEP) X[9] + orl %r11d,%r12d // (y & (not z)) | (x & z) + movl %edx,%r11d // (NEXT STEP) z' = %edx + addl %r12d,%ebx // dst += ... + movl %edx,%r12d // (NEXT STEP) z' = %edx + roll $20,%ebx // dst <<< s + addl %ecx,%ebx // dst += x + notl %r11d // not z + leal 568446438(%rax,%r10,1),%eax // Const + dst + ... + andl %ebx,%r12d // x & z + andl %ecx,%r11d // y & (not z) + movl 56(%rsi),%r10d // (NEXT STEP) X[14] + orl %r11d,%r12d // (y & (not z)) | (x & z) + movl %ecx,%r11d // (NEXT STEP) z' = %ecx + addl %r12d,%eax // dst += ... + movl %ecx,%r12d // (NEXT STEP) z' = %ecx + roll $5,%eax // dst <<< s + addl %ebx,%eax // dst += x + notl %r11d // not z + leal -1019803690(%rdx,%r10,1),%edx // Const + dst + ... + andl %eax,%r12d // x & z + andl %ebx,%r11d // y & (not z) + movl 12(%rsi),%r10d // (NEXT STEP) X[3] + orl %r11d,%r12d // (y & (not z)) | (x & z) + movl %ebx,%r11d // (NEXT STEP) z' = %ebx + addl %r12d,%edx // dst += ... + movl %ebx,%r12d // (NEXT STEP) z' = %ebx + roll $9,%edx // dst <<< s + addl %eax,%edx // dst += x + notl %r11d // not z + leal -187363961(%rcx,%r10,1),%ecx // Const + dst + ... + andl %edx,%r12d // x & z + andl %eax,%r11d // y & (not z) + movl 32(%rsi),%r10d // (NEXT STEP) X[8] + orl %r11d,%r12d // (y & (not z)) | (x & z) + movl %eax,%r11d // (NEXT STEP) z' = %eax + addl %r12d,%ecx // dst += ... + movl %eax,%r12d // (NEXT STEP) z' = %eax + roll $14,%ecx // dst <<< s + addl %edx,%ecx // dst += x + notl %r11d // not z + leal 1163531501(%rbx,%r10,1),%ebx // Const + dst + ... + andl %ecx,%r12d // x & z + andl %edx,%r11d // y & (not z) + movl 52(%rsi),%r10d // (NEXT STEP) X[13] + orl %r11d,%r12d // (y & (not z)) | (x & z) + movl %edx,%r11d // (NEXT STEP) z' = %edx + addl %r12d,%ebx // dst += ... + movl %edx,%r12d // (NEXT STEP) z' = %edx + roll $20,%ebx // dst <<< s + addl %ecx,%ebx // dst += x + notl %r11d // not z + leal -1444681467(%rax,%r10,1),%eax // Const + dst + ... + andl %ebx,%r12d // x & z + andl %ecx,%r11d // y & (not z) + movl 8(%rsi),%r10d // (NEXT STEP) X[2] + orl %r11d,%r12d // (y & (not z)) | (x & z) + movl %ecx,%r11d // (NEXT STEP) z' = %ecx + addl %r12d,%eax // dst += ... + movl %ecx,%r12d // (NEXT STEP) z' = %ecx + roll $5,%eax // dst <<< s + addl %ebx,%eax // dst += x + notl %r11d // not z + leal -51403784(%rdx,%r10,1),%edx // Const + dst + ... + andl %eax,%r12d // x & z + andl %ebx,%r11d // y & (not z) + movl 28(%rsi),%r10d // (NEXT STEP) X[7] + orl %r11d,%r12d // (y & (not z)) | (x & z) + movl %ebx,%r11d // (NEXT STEP) z' = %ebx + addl %r12d,%edx // dst += ... + movl %ebx,%r12d // (NEXT STEP) z' = %ebx + roll $9,%edx // dst <<< s + addl %eax,%edx // dst += x + notl %r11d // not z + leal 1735328473(%rcx,%r10,1),%ecx // Const + dst + ... + andl %edx,%r12d // x & z + andl %eax,%r11d // y & (not z) + movl 48(%rsi),%r10d // (NEXT STEP) X[12] + orl %r11d,%r12d // (y & (not z)) | (x & z) + movl %eax,%r11d // (NEXT STEP) z' = %eax + addl %r12d,%ecx // dst += ... + movl %eax,%r12d // (NEXT STEP) z' = %eax + roll $14,%ecx // dst <<< s + addl %edx,%ecx // dst += x + notl %r11d // not z + leal -1926607734(%rbx,%r10,1),%ebx // Const + dst + ... + andl %ecx,%r12d // x & z + andl %edx,%r11d // y & (not z) + movl 0(%rsi),%r10d // (NEXT STEP) X[0] + orl %r11d,%r12d // (y & (not z)) | (x & z) + movl %edx,%r11d // (NEXT STEP) z' = %edx + addl %r12d,%ebx // dst += ... + movl %edx,%r12d // (NEXT STEP) z' = %edx + roll $20,%ebx // dst <<< s + addl %ecx,%ebx // dst += x + movl 20(%rsi),%r10d // (NEXT STEP) X[5] + movl %ecx,%r11d // (NEXT STEP) y' = %ecx + leal -378558(%rax,%r10,1),%eax // Const + dst + ... + movl 32(%rsi),%r10d // (NEXT STEP) X[8] + xorl %edx,%r11d // z ^ ... + xorl %ebx,%r11d // x ^ ... + addl %r11d,%eax // dst += ... + roll $4,%eax // dst <<< s + movl %ebx,%r11d // (NEXT STEP) y' = %ebx + addl %ebx,%eax // dst += x + leal -2022574463(%rdx,%r10,1),%edx // Const + dst + ... + movl 44(%rsi),%r10d // (NEXT STEP) X[11] + xorl %ecx,%r11d // z ^ ... + xorl %eax,%r11d // x ^ ... + addl %r11d,%edx // dst += ... + roll $11,%edx // dst <<< s + movl %eax,%r11d // (NEXT STEP) y' = %eax + addl %eax,%edx // dst += x + leal 1839030562(%rcx,%r10,1),%ecx // Const + dst + ... + movl 56(%rsi),%r10d // (NEXT STEP) X[14] + xorl %ebx,%r11d // z ^ ... + xorl %edx,%r11d // x ^ ... + addl %r11d,%ecx // dst += ... + roll $16,%ecx // dst <<< s + movl %edx,%r11d // (NEXT STEP) y' = %edx + addl %edx,%ecx // dst += x + leal -35309556(%rbx,%r10,1),%ebx // Const + dst + ... + movl 4(%rsi),%r10d // (NEXT STEP) X[1] + xorl %eax,%r11d // z ^ ... + xorl %ecx,%r11d // x ^ ... + addl %r11d,%ebx // dst += ... + roll $23,%ebx // dst <<< s + movl %ecx,%r11d // (NEXT STEP) y' = %ecx + addl %ecx,%ebx // dst += x + leal -1530992060(%rax,%r10,1),%eax // Const + dst + ... + movl 16(%rsi),%r10d // (NEXT STEP) X[4] + xorl %edx,%r11d // z ^ ... + xorl %ebx,%r11d // x ^ ... + addl %r11d,%eax // dst += ... + roll $4,%eax // dst <<< s + movl %ebx,%r11d // (NEXT STEP) y' = %ebx + addl %ebx,%eax // dst += x + leal 1272893353(%rdx,%r10,1),%edx // Const + dst + ... + movl 28(%rsi),%r10d // (NEXT STEP) X[7] + xorl %ecx,%r11d // z ^ ... + xorl %eax,%r11d // x ^ ... + addl %r11d,%edx // dst += ... + roll $11,%edx // dst <<< s + movl %eax,%r11d // (NEXT STEP) y' = %eax + addl %eax,%edx // dst += x + leal -155497632(%rcx,%r10,1),%ecx // Const + dst + ... + movl 40(%rsi),%r10d // (NEXT STEP) X[10] + xorl %ebx,%r11d // z ^ ... + xorl %edx,%r11d // x ^ ... + addl %r11d,%ecx // dst += ... + roll $16,%ecx // dst <<< s + movl %edx,%r11d // (NEXT STEP) y' = %edx + addl %edx,%ecx // dst += x + leal -1094730640(%rbx,%r10,1),%ebx // Const + dst + ... + movl 52(%rsi),%r10d // (NEXT STEP) X[13] + xorl %eax,%r11d // z ^ ... + xorl %ecx,%r11d // x ^ ... + addl %r11d,%ebx // dst += ... + roll $23,%ebx // dst <<< s + movl %ecx,%r11d // (NEXT STEP) y' = %ecx + addl %ecx,%ebx // dst += x + leal 681279174(%rax,%r10,1),%eax // Const + dst + ... + movl 0(%rsi),%r10d // (NEXT STEP) X[0] + xorl %edx,%r11d // z ^ ... + xorl %ebx,%r11d // x ^ ... + addl %r11d,%eax // dst += ... + roll $4,%eax // dst <<< s + movl %ebx,%r11d // (NEXT STEP) y' = %ebx + addl %ebx,%eax // dst += x + leal -358537222(%rdx,%r10,1),%edx // Const + dst + ... + movl 12(%rsi),%r10d // (NEXT STEP) X[3] + xorl %ecx,%r11d // z ^ ... + xorl %eax,%r11d // x ^ ... + addl %r11d,%edx // dst += ... + roll $11,%edx // dst <<< s + movl %eax,%r11d // (NEXT STEP) y' = %eax + addl %eax,%edx // dst += x + leal -722521979(%rcx,%r10,1),%ecx // Const + dst + ... + movl 24(%rsi),%r10d // (NEXT STEP) X[6] + xorl %ebx,%r11d // z ^ ... + xorl %edx,%r11d // x ^ ... + addl %r11d,%ecx // dst += ... + roll $16,%ecx // dst <<< s + movl %edx,%r11d // (NEXT STEP) y' = %edx + addl %edx,%ecx // dst += x + leal 76029189(%rbx,%r10,1),%ebx // Const + dst + ... + movl 36(%rsi),%r10d // (NEXT STEP) X[9] + xorl %eax,%r11d // z ^ ... + xorl %ecx,%r11d // x ^ ... + addl %r11d,%ebx // dst += ... + roll $23,%ebx // dst <<< s + movl %ecx,%r11d // (NEXT STEP) y' = %ecx + addl %ecx,%ebx // dst += x + leal -640364487(%rax,%r10,1),%eax // Const + dst + ... + movl 48(%rsi),%r10d // (NEXT STEP) X[12] + xorl %edx,%r11d // z ^ ... + xorl %ebx,%r11d // x ^ ... + addl %r11d,%eax // dst += ... + roll $4,%eax // dst <<< s + movl %ebx,%r11d // (NEXT STEP) y' = %ebx + addl %ebx,%eax // dst += x + leal -421815835(%rdx,%r10,1),%edx // Const + dst + ... + movl 60(%rsi),%r10d // (NEXT STEP) X[15] + xorl %ecx,%r11d // z ^ ... + xorl %eax,%r11d // x ^ ... + addl %r11d,%edx // dst += ... + roll $11,%edx // dst <<< s + movl %eax,%r11d // (NEXT STEP) y' = %eax + addl %eax,%edx // dst += x + leal 530742520(%rcx,%r10,1),%ecx // Const + dst + ... + movl 8(%rsi),%r10d // (NEXT STEP) X[2] + xorl %ebx,%r11d // z ^ ... + xorl %edx,%r11d // x ^ ... + addl %r11d,%ecx // dst += ... + roll $16,%ecx // dst <<< s + movl %edx,%r11d // (NEXT STEP) y' = %edx + addl %edx,%ecx // dst += x + leal -995338651(%rbx,%r10,1),%ebx // Const + dst + ... + movl 0(%rsi),%r10d // (NEXT STEP) X[0] + xorl %eax,%r11d // z ^ ... + xorl %ecx,%r11d // x ^ ... + addl %r11d,%ebx // dst += ... + roll $23,%ebx // dst <<< s + movl %ecx,%r11d // (NEXT STEP) y' = %ecx + addl %ecx,%ebx // dst += x + movl 0(%rsi),%r10d // (NEXT STEP) X[0] movl $0xffffffff,%r11d - xorl %edx,%r11d - leal -198630844(%rax,%r10,1),%eax - orl %ebx,%r11d - xorl %ecx,%r11d - addl %r11d,%eax - movl 28(%rsi),%r10d + xorl %edx,%r11d // (NEXT STEP) not z' = not %edx + leal -198630844(%rax,%r10,1),%eax // Const + dst + ... + orl %ebx,%r11d // x | ... + xorl %ecx,%r11d // y ^ ... + addl %r11d,%eax // dst += ... + movl 28(%rsi),%r10d // (NEXT STEP) X[7] movl $0xffffffff,%r11d - roll $6,%eax - xorl %ecx,%r11d - addl %ebx,%eax - leal 1126891415(%rdx,%r10,1),%edx - orl %eax,%r11d - xorl %ebx,%r11d - addl %r11d,%edx - movl 56(%rsi),%r10d + roll $6,%eax // dst <<< s + xorl %ecx,%r11d // (NEXT STEP) not z' = not %ecx + addl %ebx,%eax // dst += x + leal 1126891415(%rdx,%r10,1),%edx // Const + dst + ... + orl %eax,%r11d // x | ... + xorl %ebx,%r11d // y ^ ... + addl %r11d,%edx // dst += ... + movl 56(%rsi),%r10d // (NEXT STEP) X[14] movl $0xffffffff,%r11d - roll $10,%edx - xorl %ebx,%r11d - addl %eax,%edx - leal -1416354905(%rcx,%r10,1),%ecx - orl %edx,%r11d - xorl %eax,%r11d - addl %r11d,%ecx - movl 20(%rsi),%r10d + roll $10,%edx // dst <<< s + xorl %ebx,%r11d // (NEXT STEP) not z' = not %ebx + addl %eax,%edx // dst += x + leal -1416354905(%rcx,%r10,1),%ecx // Const + dst + ... + orl %edx,%r11d // x | ... + xorl %eax,%r11d // y ^ ... + addl %r11d,%ecx // dst += ... + movl 20(%rsi),%r10d // (NEXT STEP) X[5] movl $0xffffffff,%r11d - roll $15,%ecx - xorl %eax,%r11d - addl %edx,%ecx - leal -57434055(%rbx,%r10,1),%ebx - orl %ecx,%r11d - xorl %edx,%r11d - addl %r11d,%ebx - movl 48(%rsi),%r10d + roll $15,%ecx // dst <<< s + xorl %eax,%r11d // (NEXT STEP) not z' = not %eax + addl %edx,%ecx // dst += x + leal -57434055(%rbx,%r10,1),%ebx // Const + dst + ... + orl %ecx,%r11d // x | ... + xorl %edx,%r11d // y ^ ... + addl %r11d,%ebx // dst += ... + movl 48(%rsi),%r10d // (NEXT STEP) X[12] movl $0xffffffff,%r11d - roll $21,%ebx - xorl %edx,%r11d - addl %ecx,%ebx - leal 1700485571(%rax,%r10,1),%eax - orl %ebx,%r11d - xorl %ecx,%r11d - addl %r11d,%eax - movl 12(%rsi),%r10d + roll $21,%ebx // dst <<< s + xorl %edx,%r11d // (NEXT STEP) not z' = not %edx + addl %ecx,%ebx // dst += x + leal 1700485571(%rax,%r10,1),%eax // Const + dst + ... + orl %ebx,%r11d // x | ... + xorl %ecx,%r11d // y ^ ... + addl %r11d,%eax // dst += ... + movl 12(%rsi),%r10d // (NEXT STEP) X[3] movl $0xffffffff,%r11d - roll $6,%eax - xorl %ecx,%r11d - addl %ebx,%eax - leal -1894986606(%rdx,%r10,1),%edx - orl %eax,%r11d - xorl %ebx,%r11d - addl %r11d,%edx - movl 40(%rsi),%r10d + roll $6,%eax // dst <<< s + xorl %ecx,%r11d // (NEXT STEP) not z' = not %ecx + addl %ebx,%eax // dst += x + leal -1894986606(%rdx,%r10,1),%edx // Const + dst + ... + orl %eax,%r11d // x | ... + xorl %ebx,%r11d // y ^ ... + addl %r11d,%edx // dst += ... + movl 40(%rsi),%r10d // (NEXT STEP) X[10] movl $0xffffffff,%r11d - roll $10,%edx - xorl %ebx,%r11d - addl %eax,%edx - leal -1051523(%rcx,%r10,1),%ecx - orl %edx,%r11d - xorl %eax,%r11d - addl %r11d,%ecx - movl 4(%rsi),%r10d + roll $10,%edx // dst <<< s + xorl %ebx,%r11d // (NEXT STEP) not z' = not %ebx + addl %eax,%edx // dst += x + leal -1051523(%rcx,%r10,1),%ecx // Const + dst + ... + orl %edx,%r11d // x | ... + xorl %eax,%r11d // y ^ ... + addl %r11d,%ecx // dst += ... + movl 4(%rsi),%r10d // (NEXT STEP) X[1] movl $0xffffffff,%r11d - roll $15,%ecx - xorl %eax,%r11d - addl %edx,%ecx - leal -2054922799(%rbx,%r10,1),%ebx - orl %ecx,%r11d - xorl %edx,%r11d - addl %r11d,%ebx - movl 32(%rsi),%r10d + roll $15,%ecx // dst <<< s + xorl %eax,%r11d // (NEXT STEP) not z' = not %eax + addl %edx,%ecx // dst += x + leal -2054922799(%rbx,%r10,1),%ebx // Const + dst + ... + orl %ecx,%r11d // x | ... + xorl %edx,%r11d // y ^ ... + addl %r11d,%ebx // dst += ... + movl 32(%rsi),%r10d // (NEXT STEP) X[8] movl $0xffffffff,%r11d - roll $21,%ebx - xorl %edx,%r11d - addl %ecx,%ebx - leal 1873313359(%rax,%r10,1),%eax - orl %ebx,%r11d - xorl %ecx,%r11d - addl %r11d,%eax - movl 60(%rsi),%r10d + roll $21,%ebx // dst <<< s + xorl %edx,%r11d // (NEXT STEP) not z' = not %edx + addl %ecx,%ebx // dst += x + leal 1873313359(%rax,%r10,1),%eax // Const + dst + ... + orl %ebx,%r11d // x | ... + xorl %ecx,%r11d // y ^ ... + addl %r11d,%eax // dst += ... + movl 60(%rsi),%r10d // (NEXT STEP) X[15] movl $0xffffffff,%r11d - roll $6,%eax - xorl %ecx,%r11d - addl %ebx,%eax - leal -30611744(%rdx,%r10,1),%edx - orl %eax,%r11d - xorl %ebx,%r11d - addl %r11d,%edx - movl 24(%rsi),%r10d + roll $6,%eax // dst <<< s + xorl %ecx,%r11d // (NEXT STEP) not z' = not %ecx + addl %ebx,%eax // dst += x + leal -30611744(%rdx,%r10,1),%edx // Const + dst + ... + orl %eax,%r11d // x | ... + xorl %ebx,%r11d // y ^ ... + addl %r11d,%edx // dst += ... + movl 24(%rsi),%r10d // (NEXT STEP) X[6] movl $0xffffffff,%r11d - roll $10,%edx - xorl %ebx,%r11d - addl %eax,%edx - leal -1560198380(%rcx,%r10,1),%ecx - orl %edx,%r11d - xorl %eax,%r11d - addl %r11d,%ecx - movl 52(%rsi),%r10d + roll $10,%edx // dst <<< s + xorl %ebx,%r11d // (NEXT STEP) not z' = not %ebx + addl %eax,%edx // dst += x + leal -1560198380(%rcx,%r10,1),%ecx // Const + dst + ... + orl %edx,%r11d // x | ... + xorl %eax,%r11d // y ^ ... + addl %r11d,%ecx // dst += ... + movl 52(%rsi),%r10d // (NEXT STEP) X[13] movl $0xffffffff,%r11d - roll $15,%ecx - xorl %eax,%r11d - addl %edx,%ecx - leal 1309151649(%rbx,%r10,1),%ebx - orl %ecx,%r11d - xorl %edx,%r11d - addl %r11d,%ebx - movl 16(%rsi),%r10d + roll $15,%ecx // dst <<< s + xorl %eax,%r11d // (NEXT STEP) not z' = not %eax + addl %edx,%ecx // dst += x + leal 1309151649(%rbx,%r10,1),%ebx // Const + dst + ... + orl %ecx,%r11d // x | ... + xorl %edx,%r11d // y ^ ... + addl %r11d,%ebx // dst += ... + movl 16(%rsi),%r10d // (NEXT STEP) X[4] movl $0xffffffff,%r11d - roll $21,%ebx - xorl %edx,%r11d - addl %ecx,%ebx - leal -145523070(%rax,%r10,1),%eax - orl %ebx,%r11d - xorl %ecx,%r11d - addl %r11d,%eax - movl 44(%rsi),%r10d + roll $21,%ebx // dst <<< s + xorl %edx,%r11d // (NEXT STEP) not z' = not %edx + addl %ecx,%ebx // dst += x + leal -145523070(%rax,%r10,1),%eax // Const + dst + ... + orl %ebx,%r11d // x | ... + xorl %ecx,%r11d // y ^ ... + addl %r11d,%eax // dst += ... + movl 44(%rsi),%r10d // (NEXT STEP) X[11] movl $0xffffffff,%r11d - roll $6,%eax - xorl %ecx,%r11d - addl %ebx,%eax - leal -1120210379(%rdx,%r10,1),%edx - orl %eax,%r11d - xorl %ebx,%r11d - addl %r11d,%edx - movl 8(%rsi),%r10d + roll $6,%eax // dst <<< s + xorl %ecx,%r11d // (NEXT STEP) not z' = not %ecx + addl %ebx,%eax // dst += x + leal -1120210379(%rdx,%r10,1),%edx // Const + dst + ... + orl %eax,%r11d // x | ... + xorl %ebx,%r11d // y ^ ... + addl %r11d,%edx // dst += ... + movl 8(%rsi),%r10d // (NEXT STEP) X[2] movl $0xffffffff,%r11d - roll $10,%edx - xorl %ebx,%r11d - addl %eax,%edx - leal 718787259(%rcx,%r10,1),%ecx - orl %edx,%r11d - xorl %eax,%r11d - addl %r11d,%ecx - movl 36(%rsi),%r10d + roll $10,%edx // dst <<< s + xorl %ebx,%r11d // (NEXT STEP) not z' = not %ebx + addl %eax,%edx // dst += x + leal 718787259(%rcx,%r10,1),%ecx // Const + dst + ... + orl %edx,%r11d // x | ... + xorl %eax,%r11d // y ^ ... + addl %r11d,%ecx // dst += ... + movl 36(%rsi),%r10d // (NEXT STEP) X[9] movl $0xffffffff,%r11d - roll $15,%ecx - xorl %eax,%r11d - addl %edx,%ecx - leal -343485551(%rbx,%r10,1),%ebx - orl %ecx,%r11d - xorl %edx,%r11d - addl %r11d,%ebx - movl 0(%rsi),%r10d + roll $15,%ecx // dst <<< s + xorl %eax,%r11d // (NEXT STEP) not z' = not %eax + addl %edx,%ecx // dst += x + leal -343485551(%rbx,%r10,1),%ebx // Const + dst + ... + orl %ecx,%r11d // x | ... + xorl %edx,%r11d // y ^ ... + addl %r11d,%ebx // dst += ... + movl 0(%rsi),%r10d // (NEXT STEP) X[0] movl $0xffffffff,%r11d - roll $21,%ebx - xorl %edx,%r11d - addl %ecx,%ebx - + roll $21,%ebx // dst <<< s + xorl %edx,%r11d // (NEXT STEP) not z' = not %edx + addl %ecx,%ebx // dst += x +// add old values of A, B, C, D addl %r8d,%eax addl %r9d,%ebx addl %r14d,%ecx addl %r15d,%edx - - addq $64,%rsi - cmpq %rdi,%rsi - jb L$loop - +// loop control + addq $64,%rsi // ptr += 64 + cmpq %rdi,%rsi // cmp end with ptr + jb L$loop // jmp if ptr < end +// END of loop over 16-word blocks L$end: - movl %eax,0(%rbp) - movl %ebx,4(%rbp) - movl %ecx,8(%rbp) - movl %edx,12(%rbp) + movl %eax,0(%rbp) // ctx->A = A + movl %ebx,4(%rbp) // ctx->B = B + movl %ecx,8(%rbp) // ctx->C = C + movl %edx,12(%rbp) // ctx->D = D movq (%rsp),%r15 diff --git a/third_party/boringssl/gen/crypto/md5-x86_64-linux.S b/third_party/boringssl/gen/crypto/md5-x86_64-linux.S index 7b93662a4..e8852c461 100644 --- a/third_party/boringssl/gen/crypto/md5-x86_64-linux.S +++ b/third_party/boringssl/gen/crypto/md5-x86_64-linux.S @@ -30,651 +30,651 @@ _CET_ENDBR .cfi_offset r15,-48 .Lprologue: +// rdi = arg #1 (ctx, MD5_CTX pointer) +// rsi = arg #2 (ptr, data pointer) +// rdx = arg #3 (nbr, number of 16-word blocks to process) + movq %rdi,%rbp // rbp = ctx + shlq $6,%rdx // rdx = nbr in bytes + leaq (%rsi,%rdx,1),%rdi // rdi = end + movl 0(%rbp),%eax // eax = ctx->A + movl 4(%rbp),%ebx // ebx = ctx->B + movl 8(%rbp),%ecx // ecx = ctx->C + movl 12(%rbp),%edx // edx = ctx->D +// end is 'rdi' +// ptr is 'rsi' +// A is 'eax' +// B is 'ebx' +// C is 'ecx' +// D is 'edx' + cmpq %rdi,%rsi // cmp end with ptr + je .Lend // jmp if ptr == end - - movq %rdi,%rbp - shlq $6,%rdx - leaq (%rsi,%rdx,1),%rdi - movl 0(%rbp),%eax - movl 4(%rbp),%ebx - movl 8(%rbp),%ecx - movl 12(%rbp),%edx - - - - - - - - cmpq %rdi,%rsi - je .Lend - - -.Lloop: +// BEGIN of loop over 16-word blocks +.Lloop: // save old values of A, B, C, D movl %eax,%r8d movl %ebx,%r9d movl %ecx,%r14d movl %edx,%r15d - movl 0(%rsi),%r10d - movl %edx,%r11d - xorl %ecx,%r11d - leal -680876936(%rax,%r10,1),%eax - andl %ebx,%r11d - xorl %edx,%r11d - movl 4(%rsi),%r10d - addl %r11d,%eax - roll $7,%eax - movl %ecx,%r11d - addl %ebx,%eax - xorl %ebx,%r11d - leal -389564586(%rdx,%r10,1),%edx - andl %eax,%r11d - xorl %ecx,%r11d - movl 8(%rsi),%r10d - addl %r11d,%edx - roll $12,%edx - movl %ebx,%r11d - addl %eax,%edx - xorl %eax,%r11d - leal 606105819(%rcx,%r10,1),%ecx - andl %edx,%r11d - xorl %ebx,%r11d - movl 12(%rsi),%r10d - addl %r11d,%ecx - roll $17,%ecx - movl %eax,%r11d - addl %edx,%ecx - xorl %edx,%r11d - leal -1044525330(%rbx,%r10,1),%ebx - andl %ecx,%r11d - xorl %eax,%r11d - movl 16(%rsi),%r10d - addl %r11d,%ebx - roll $22,%ebx - movl %edx,%r11d - addl %ecx,%ebx - xorl %ecx,%r11d - leal -176418897(%rax,%r10,1),%eax - andl %ebx,%r11d - xorl %edx,%r11d - movl 20(%rsi),%r10d - addl %r11d,%eax - roll $7,%eax - movl %ecx,%r11d - addl %ebx,%eax - xorl %ebx,%r11d - leal 1200080426(%rdx,%r10,1),%edx - andl %eax,%r11d - xorl %ecx,%r11d - movl 24(%rsi),%r10d - addl %r11d,%edx - roll $12,%edx - movl %ebx,%r11d - addl %eax,%edx - xorl %eax,%r11d - leal -1473231341(%rcx,%r10,1),%ecx - andl %edx,%r11d - xorl %ebx,%r11d - movl 28(%rsi),%r10d - addl %r11d,%ecx - roll $17,%ecx - movl %eax,%r11d - addl %edx,%ecx - xorl %edx,%r11d - leal -45705983(%rbx,%r10,1),%ebx - andl %ecx,%r11d - xorl %eax,%r11d - movl 32(%rsi),%r10d - addl %r11d,%ebx - roll $22,%ebx - movl %edx,%r11d - addl %ecx,%ebx - xorl %ecx,%r11d - leal 1770035416(%rax,%r10,1),%eax - andl %ebx,%r11d - xorl %edx,%r11d - movl 36(%rsi),%r10d - addl %r11d,%eax - roll $7,%eax - movl %ecx,%r11d - addl %ebx,%eax - xorl %ebx,%r11d - leal -1958414417(%rdx,%r10,1),%edx - andl %eax,%r11d - xorl %ecx,%r11d - movl 40(%rsi),%r10d - addl %r11d,%edx - roll $12,%edx - movl %ebx,%r11d - addl %eax,%edx - xorl %eax,%r11d - leal -42063(%rcx,%r10,1),%ecx - andl %edx,%r11d - xorl %ebx,%r11d - movl 44(%rsi),%r10d - addl %r11d,%ecx - roll $17,%ecx - movl %eax,%r11d - addl %edx,%ecx - xorl %edx,%r11d - leal -1990404162(%rbx,%r10,1),%ebx - andl %ecx,%r11d - xorl %eax,%r11d - movl 48(%rsi),%r10d - addl %r11d,%ebx - roll $22,%ebx - movl %edx,%r11d - addl %ecx,%ebx - xorl %ecx,%r11d - leal 1804603682(%rax,%r10,1),%eax - andl %ebx,%r11d - xorl %edx,%r11d - movl 52(%rsi),%r10d - addl %r11d,%eax - roll $7,%eax - movl %ecx,%r11d - addl %ebx,%eax - xorl %ebx,%r11d - leal -40341101(%rdx,%r10,1),%edx - andl %eax,%r11d - xorl %ecx,%r11d - movl 56(%rsi),%r10d - addl %r11d,%edx - roll $12,%edx - movl %ebx,%r11d - addl %eax,%edx - xorl %eax,%r11d - leal -1502002290(%rcx,%r10,1),%ecx - andl %edx,%r11d - xorl %ebx,%r11d - movl 60(%rsi),%r10d - addl %r11d,%ecx - roll $17,%ecx - movl %eax,%r11d - addl %edx,%ecx - xorl %edx,%r11d - leal 1236535329(%rbx,%r10,1),%ebx - andl %ecx,%r11d - xorl %eax,%r11d - movl 0(%rsi),%r10d - addl %r11d,%ebx - roll $22,%ebx - movl %edx,%r11d - addl %ecx,%ebx - movl 4(%rsi),%r10d - movl %edx,%r11d - movl %edx,%r12d - notl %r11d - leal -165796510(%rax,%r10,1),%eax - andl %ebx,%r12d - andl %ecx,%r11d - movl 24(%rsi),%r10d - orl %r11d,%r12d - movl %ecx,%r11d - addl %r12d,%eax - movl %ecx,%r12d - roll $5,%eax - addl %ebx,%eax - notl %r11d - leal -1069501632(%rdx,%r10,1),%edx - andl %eax,%r12d - andl %ebx,%r11d - movl 44(%rsi),%r10d - orl %r11d,%r12d - movl %ebx,%r11d - addl %r12d,%edx - movl %ebx,%r12d - roll $9,%edx - addl %eax,%edx - notl %r11d - leal 643717713(%rcx,%r10,1),%ecx - andl %edx,%r12d - andl %eax,%r11d - movl 0(%rsi),%r10d - orl %r11d,%r12d - movl %eax,%r11d - addl %r12d,%ecx - movl %eax,%r12d - roll $14,%ecx - addl %edx,%ecx - notl %r11d - leal -373897302(%rbx,%r10,1),%ebx - andl %ecx,%r12d - andl %edx,%r11d - movl 20(%rsi),%r10d - orl %r11d,%r12d - movl %edx,%r11d - addl %r12d,%ebx - movl %edx,%r12d - roll $20,%ebx - addl %ecx,%ebx - notl %r11d - leal -701558691(%rax,%r10,1),%eax - andl %ebx,%r12d - andl %ecx,%r11d - movl 40(%rsi),%r10d - orl %r11d,%r12d - movl %ecx,%r11d - addl %r12d,%eax - movl %ecx,%r12d - roll $5,%eax - addl %ebx,%eax - notl %r11d - leal 38016083(%rdx,%r10,1),%edx - andl %eax,%r12d - andl %ebx,%r11d - movl 60(%rsi),%r10d - orl %r11d,%r12d - movl %ebx,%r11d - addl %r12d,%edx - movl %ebx,%r12d - roll $9,%edx - addl %eax,%edx - notl %r11d - leal -660478335(%rcx,%r10,1),%ecx - andl %edx,%r12d - andl %eax,%r11d - movl 16(%rsi),%r10d - orl %r11d,%r12d - movl %eax,%r11d - addl %r12d,%ecx - movl %eax,%r12d - roll $14,%ecx - addl %edx,%ecx - notl %r11d - leal -405537848(%rbx,%r10,1),%ebx - andl %ecx,%r12d - andl %edx,%r11d - movl 36(%rsi),%r10d - orl %r11d,%r12d - movl %edx,%r11d - addl %r12d,%ebx - movl %edx,%r12d - roll $20,%ebx - addl %ecx,%ebx - notl %r11d - leal 568446438(%rax,%r10,1),%eax - andl %ebx,%r12d - andl %ecx,%r11d - movl 56(%rsi),%r10d - orl %r11d,%r12d - movl %ecx,%r11d - addl %r12d,%eax - movl %ecx,%r12d - roll $5,%eax - addl %ebx,%eax - notl %r11d - leal -1019803690(%rdx,%r10,1),%edx - andl %eax,%r12d - andl %ebx,%r11d - movl 12(%rsi),%r10d - orl %r11d,%r12d - movl %ebx,%r11d - addl %r12d,%edx - movl %ebx,%r12d - roll $9,%edx - addl %eax,%edx - notl %r11d - leal -187363961(%rcx,%r10,1),%ecx - andl %edx,%r12d - andl %eax,%r11d - movl 32(%rsi),%r10d - orl %r11d,%r12d - movl %eax,%r11d - addl %r12d,%ecx - movl %eax,%r12d - roll $14,%ecx - addl %edx,%ecx - notl %r11d - leal 1163531501(%rbx,%r10,1),%ebx - andl %ecx,%r12d - andl %edx,%r11d - movl 52(%rsi),%r10d - orl %r11d,%r12d - movl %edx,%r11d - addl %r12d,%ebx - movl %edx,%r12d - roll $20,%ebx - addl %ecx,%ebx - notl %r11d - leal -1444681467(%rax,%r10,1),%eax - andl %ebx,%r12d - andl %ecx,%r11d - movl 8(%rsi),%r10d - orl %r11d,%r12d - movl %ecx,%r11d - addl %r12d,%eax - movl %ecx,%r12d - roll $5,%eax - addl %ebx,%eax - notl %r11d - leal -51403784(%rdx,%r10,1),%edx - andl %eax,%r12d - andl %ebx,%r11d - movl 28(%rsi),%r10d - orl %r11d,%r12d - movl %ebx,%r11d - addl %r12d,%edx - movl %ebx,%r12d - roll $9,%edx - addl %eax,%edx - notl %r11d - leal 1735328473(%rcx,%r10,1),%ecx - andl %edx,%r12d - andl %eax,%r11d - movl 48(%rsi),%r10d - orl %r11d,%r12d - movl %eax,%r11d - addl %r12d,%ecx - movl %eax,%r12d - roll $14,%ecx - addl %edx,%ecx - notl %r11d - leal -1926607734(%rbx,%r10,1),%ebx - andl %ecx,%r12d - andl %edx,%r11d - movl 0(%rsi),%r10d - orl %r11d,%r12d - movl %edx,%r11d - addl %r12d,%ebx - movl %edx,%r12d - roll $20,%ebx - addl %ecx,%ebx - movl 20(%rsi),%r10d - movl %ecx,%r11d - leal -378558(%rax,%r10,1),%eax - movl 32(%rsi),%r10d - xorl %edx,%r11d - xorl %ebx,%r11d - addl %r11d,%eax - roll $4,%eax - movl %ebx,%r11d - addl %ebx,%eax - leal -2022574463(%rdx,%r10,1),%edx - movl 44(%rsi),%r10d - xorl %ecx,%r11d - xorl %eax,%r11d - addl %r11d,%edx - roll $11,%edx - movl %eax,%r11d - addl %eax,%edx - leal 1839030562(%rcx,%r10,1),%ecx - movl 56(%rsi),%r10d - xorl %ebx,%r11d - xorl %edx,%r11d - addl %r11d,%ecx - roll $16,%ecx - movl %edx,%r11d - addl %edx,%ecx - leal -35309556(%rbx,%r10,1),%ebx - movl 4(%rsi),%r10d - xorl %eax,%r11d - xorl %ecx,%r11d - addl %r11d,%ebx - roll $23,%ebx - movl %ecx,%r11d - addl %ecx,%ebx - leal -1530992060(%rax,%r10,1),%eax - movl 16(%rsi),%r10d - xorl %edx,%r11d - xorl %ebx,%r11d - addl %r11d,%eax - roll $4,%eax - movl %ebx,%r11d - addl %ebx,%eax - leal 1272893353(%rdx,%r10,1),%edx - movl 28(%rsi),%r10d - xorl %ecx,%r11d - xorl %eax,%r11d - addl %r11d,%edx - roll $11,%edx - movl %eax,%r11d - addl %eax,%edx - leal -155497632(%rcx,%r10,1),%ecx - movl 40(%rsi),%r10d - xorl %ebx,%r11d - xorl %edx,%r11d - addl %r11d,%ecx - roll $16,%ecx - movl %edx,%r11d - addl %edx,%ecx - leal -1094730640(%rbx,%r10,1),%ebx - movl 52(%rsi),%r10d - xorl %eax,%r11d - xorl %ecx,%r11d - addl %r11d,%ebx - roll $23,%ebx - movl %ecx,%r11d - addl %ecx,%ebx - leal 681279174(%rax,%r10,1),%eax - movl 0(%rsi),%r10d - xorl %edx,%r11d - xorl %ebx,%r11d - addl %r11d,%eax - roll $4,%eax - movl %ebx,%r11d - addl %ebx,%eax - leal -358537222(%rdx,%r10,1),%edx - movl 12(%rsi),%r10d - xorl %ecx,%r11d - xorl %eax,%r11d - addl %r11d,%edx - roll $11,%edx - movl %eax,%r11d - addl %eax,%edx - leal -722521979(%rcx,%r10,1),%ecx - movl 24(%rsi),%r10d - xorl %ebx,%r11d - xorl %edx,%r11d - addl %r11d,%ecx - roll $16,%ecx - movl %edx,%r11d - addl %edx,%ecx - leal 76029189(%rbx,%r10,1),%ebx - movl 36(%rsi),%r10d - xorl %eax,%r11d - xorl %ecx,%r11d - addl %r11d,%ebx - roll $23,%ebx - movl %ecx,%r11d - addl %ecx,%ebx - leal -640364487(%rax,%r10,1),%eax - movl 48(%rsi),%r10d - xorl %edx,%r11d - xorl %ebx,%r11d - addl %r11d,%eax - roll $4,%eax - movl %ebx,%r11d - addl %ebx,%eax - leal -421815835(%rdx,%r10,1),%edx - movl 60(%rsi),%r10d - xorl %ecx,%r11d - xorl %eax,%r11d - addl %r11d,%edx - roll $11,%edx - movl %eax,%r11d - addl %eax,%edx - leal 530742520(%rcx,%r10,1),%ecx - movl 8(%rsi),%r10d - xorl %ebx,%r11d - xorl %edx,%r11d - addl %r11d,%ecx - roll $16,%ecx - movl %edx,%r11d - addl %edx,%ecx - leal -995338651(%rbx,%r10,1),%ebx - movl 0(%rsi),%r10d - xorl %eax,%r11d - xorl %ecx,%r11d - addl %r11d,%ebx - roll $23,%ebx - movl %ecx,%r11d - addl %ecx,%ebx - movl 0(%rsi),%r10d + movl 0(%rsi),%r10d // (NEXT STEP) X[0] + movl %edx,%r11d // (NEXT STEP) z' = %edx + xorl %ecx,%r11d // y ^ ... + leal -680876936(%rax,%r10,1),%eax // Const + dst + ... + andl %ebx,%r11d // x & ... + xorl %edx,%r11d // z ^ ... + movl 4(%rsi),%r10d // (NEXT STEP) X[1] + addl %r11d,%eax // dst += ... + roll $7,%eax // dst <<< s + movl %ecx,%r11d // (NEXT STEP) z' = %ecx + addl %ebx,%eax // dst += x + xorl %ebx,%r11d // y ^ ... + leal -389564586(%rdx,%r10,1),%edx // Const + dst + ... + andl %eax,%r11d // x & ... + xorl %ecx,%r11d // z ^ ... + movl 8(%rsi),%r10d // (NEXT STEP) X[2] + addl %r11d,%edx // dst += ... + roll $12,%edx // dst <<< s + movl %ebx,%r11d // (NEXT STEP) z' = %ebx + addl %eax,%edx // dst += x + xorl %eax,%r11d // y ^ ... + leal 606105819(%rcx,%r10,1),%ecx // Const + dst + ... + andl %edx,%r11d // x & ... + xorl %ebx,%r11d // z ^ ... + movl 12(%rsi),%r10d // (NEXT STEP) X[3] + addl %r11d,%ecx // dst += ... + roll $17,%ecx // dst <<< s + movl %eax,%r11d // (NEXT STEP) z' = %eax + addl %edx,%ecx // dst += x + xorl %edx,%r11d // y ^ ... + leal -1044525330(%rbx,%r10,1),%ebx // Const + dst + ... + andl %ecx,%r11d // x & ... + xorl %eax,%r11d // z ^ ... + movl 16(%rsi),%r10d // (NEXT STEP) X[4] + addl %r11d,%ebx // dst += ... + roll $22,%ebx // dst <<< s + movl %edx,%r11d // (NEXT STEP) z' = %edx + addl %ecx,%ebx // dst += x + xorl %ecx,%r11d // y ^ ... + leal -176418897(%rax,%r10,1),%eax // Const + dst + ... + andl %ebx,%r11d // x & ... + xorl %edx,%r11d // z ^ ... + movl 20(%rsi),%r10d // (NEXT STEP) X[5] + addl %r11d,%eax // dst += ... + roll $7,%eax // dst <<< s + movl %ecx,%r11d // (NEXT STEP) z' = %ecx + addl %ebx,%eax // dst += x + xorl %ebx,%r11d // y ^ ... + leal 1200080426(%rdx,%r10,1),%edx // Const + dst + ... + andl %eax,%r11d // x & ... + xorl %ecx,%r11d // z ^ ... + movl 24(%rsi),%r10d // (NEXT STEP) X[6] + addl %r11d,%edx // dst += ... + roll $12,%edx // dst <<< s + movl %ebx,%r11d // (NEXT STEP) z' = %ebx + addl %eax,%edx // dst += x + xorl %eax,%r11d // y ^ ... + leal -1473231341(%rcx,%r10,1),%ecx // Const + dst + ... + andl %edx,%r11d // x & ... + xorl %ebx,%r11d // z ^ ... + movl 28(%rsi),%r10d // (NEXT STEP) X[7] + addl %r11d,%ecx // dst += ... + roll $17,%ecx // dst <<< s + movl %eax,%r11d // (NEXT STEP) z' = %eax + addl %edx,%ecx // dst += x + xorl %edx,%r11d // y ^ ... + leal -45705983(%rbx,%r10,1),%ebx // Const + dst + ... + andl %ecx,%r11d // x & ... + xorl %eax,%r11d // z ^ ... + movl 32(%rsi),%r10d // (NEXT STEP) X[8] + addl %r11d,%ebx // dst += ... + roll $22,%ebx // dst <<< s + movl %edx,%r11d // (NEXT STEP) z' = %edx + addl %ecx,%ebx // dst += x + xorl %ecx,%r11d // y ^ ... + leal 1770035416(%rax,%r10,1),%eax // Const + dst + ... + andl %ebx,%r11d // x & ... + xorl %edx,%r11d // z ^ ... + movl 36(%rsi),%r10d // (NEXT STEP) X[9] + addl %r11d,%eax // dst += ... + roll $7,%eax // dst <<< s + movl %ecx,%r11d // (NEXT STEP) z' = %ecx + addl %ebx,%eax // dst += x + xorl %ebx,%r11d // y ^ ... + leal -1958414417(%rdx,%r10,1),%edx // Const + dst + ... + andl %eax,%r11d // x & ... + xorl %ecx,%r11d // z ^ ... + movl 40(%rsi),%r10d // (NEXT STEP) X[10] + addl %r11d,%edx // dst += ... + roll $12,%edx // dst <<< s + movl %ebx,%r11d // (NEXT STEP) z' = %ebx + addl %eax,%edx // dst += x + xorl %eax,%r11d // y ^ ... + leal -42063(%rcx,%r10,1),%ecx // Const + dst + ... + andl %edx,%r11d // x & ... + xorl %ebx,%r11d // z ^ ... + movl 44(%rsi),%r10d // (NEXT STEP) X[11] + addl %r11d,%ecx // dst += ... + roll $17,%ecx // dst <<< s + movl %eax,%r11d // (NEXT STEP) z' = %eax + addl %edx,%ecx // dst += x + xorl %edx,%r11d // y ^ ... + leal -1990404162(%rbx,%r10,1),%ebx // Const + dst + ... + andl %ecx,%r11d // x & ... + xorl %eax,%r11d // z ^ ... + movl 48(%rsi),%r10d // (NEXT STEP) X[12] + addl %r11d,%ebx // dst += ... + roll $22,%ebx // dst <<< s + movl %edx,%r11d // (NEXT STEP) z' = %edx + addl %ecx,%ebx // dst += x + xorl %ecx,%r11d // y ^ ... + leal 1804603682(%rax,%r10,1),%eax // Const + dst + ... + andl %ebx,%r11d // x & ... + xorl %edx,%r11d // z ^ ... + movl 52(%rsi),%r10d // (NEXT STEP) X[13] + addl %r11d,%eax // dst += ... + roll $7,%eax // dst <<< s + movl %ecx,%r11d // (NEXT STEP) z' = %ecx + addl %ebx,%eax // dst += x + xorl %ebx,%r11d // y ^ ... + leal -40341101(%rdx,%r10,1),%edx // Const + dst + ... + andl %eax,%r11d // x & ... + xorl %ecx,%r11d // z ^ ... + movl 56(%rsi),%r10d // (NEXT STEP) X[14] + addl %r11d,%edx // dst += ... + roll $12,%edx // dst <<< s + movl %ebx,%r11d // (NEXT STEP) z' = %ebx + addl %eax,%edx // dst += x + xorl %eax,%r11d // y ^ ... + leal -1502002290(%rcx,%r10,1),%ecx // Const + dst + ... + andl %edx,%r11d // x & ... + xorl %ebx,%r11d // z ^ ... + movl 60(%rsi),%r10d // (NEXT STEP) X[15] + addl %r11d,%ecx // dst += ... + roll $17,%ecx // dst <<< s + movl %eax,%r11d // (NEXT STEP) z' = %eax + addl %edx,%ecx // dst += x + xorl %edx,%r11d // y ^ ... + leal 1236535329(%rbx,%r10,1),%ebx // Const + dst + ... + andl %ecx,%r11d // x & ... + xorl %eax,%r11d // z ^ ... + movl 0(%rsi),%r10d // (NEXT STEP) X[0] + addl %r11d,%ebx // dst += ... + roll $22,%ebx // dst <<< s + movl %edx,%r11d // (NEXT STEP) z' = %edx + addl %ecx,%ebx // dst += x + movl 4(%rsi),%r10d // (NEXT STEP) X[1] + movl %edx,%r11d // (NEXT STEP) z' = %edx + movl %edx,%r12d // (NEXT STEP) z' = %edx + notl %r11d // not z + leal -165796510(%rax,%r10,1),%eax // Const + dst + ... + andl %ebx,%r12d // x & z + andl %ecx,%r11d // y & (not z) + movl 24(%rsi),%r10d // (NEXT STEP) X[6] + orl %r11d,%r12d // (y & (not z)) | (x & z) + movl %ecx,%r11d // (NEXT STEP) z' = %ecx + addl %r12d,%eax // dst += ... + movl %ecx,%r12d // (NEXT STEP) z' = %ecx + roll $5,%eax // dst <<< s + addl %ebx,%eax // dst += x + notl %r11d // not z + leal -1069501632(%rdx,%r10,1),%edx // Const + dst + ... + andl %eax,%r12d // x & z + andl %ebx,%r11d // y & (not z) + movl 44(%rsi),%r10d // (NEXT STEP) X[11] + orl %r11d,%r12d // (y & (not z)) | (x & z) + movl %ebx,%r11d // (NEXT STEP) z' = %ebx + addl %r12d,%edx // dst += ... + movl %ebx,%r12d // (NEXT STEP) z' = %ebx + roll $9,%edx // dst <<< s + addl %eax,%edx // dst += x + notl %r11d // not z + leal 643717713(%rcx,%r10,1),%ecx // Const + dst + ... + andl %edx,%r12d // x & z + andl %eax,%r11d // y & (not z) + movl 0(%rsi),%r10d // (NEXT STEP) X[0] + orl %r11d,%r12d // (y & (not z)) | (x & z) + movl %eax,%r11d // (NEXT STEP) z' = %eax + addl %r12d,%ecx // dst += ... + movl %eax,%r12d // (NEXT STEP) z' = %eax + roll $14,%ecx // dst <<< s + addl %edx,%ecx // dst += x + notl %r11d // not z + leal -373897302(%rbx,%r10,1),%ebx // Const + dst + ... + andl %ecx,%r12d // x & z + andl %edx,%r11d // y & (not z) + movl 20(%rsi),%r10d // (NEXT STEP) X[5] + orl %r11d,%r12d // (y & (not z)) | (x & z) + movl %edx,%r11d // (NEXT STEP) z' = %edx + addl %r12d,%ebx // dst += ... + movl %edx,%r12d // (NEXT STEP) z' = %edx + roll $20,%ebx // dst <<< s + addl %ecx,%ebx // dst += x + notl %r11d // not z + leal -701558691(%rax,%r10,1),%eax // Const + dst + ... + andl %ebx,%r12d // x & z + andl %ecx,%r11d // y & (not z) + movl 40(%rsi),%r10d // (NEXT STEP) X[10] + orl %r11d,%r12d // (y & (not z)) | (x & z) + movl %ecx,%r11d // (NEXT STEP) z' = %ecx + addl %r12d,%eax // dst += ... + movl %ecx,%r12d // (NEXT STEP) z' = %ecx + roll $5,%eax // dst <<< s + addl %ebx,%eax // dst += x + notl %r11d // not z + leal 38016083(%rdx,%r10,1),%edx // Const + dst + ... + andl %eax,%r12d // x & z + andl %ebx,%r11d // y & (not z) + movl 60(%rsi),%r10d // (NEXT STEP) X[15] + orl %r11d,%r12d // (y & (not z)) | (x & z) + movl %ebx,%r11d // (NEXT STEP) z' = %ebx + addl %r12d,%edx // dst += ... + movl %ebx,%r12d // (NEXT STEP) z' = %ebx + roll $9,%edx // dst <<< s + addl %eax,%edx // dst += x + notl %r11d // not z + leal -660478335(%rcx,%r10,1),%ecx // Const + dst + ... + andl %edx,%r12d // x & z + andl %eax,%r11d // y & (not z) + movl 16(%rsi),%r10d // (NEXT STEP) X[4] + orl %r11d,%r12d // (y & (not z)) | (x & z) + movl %eax,%r11d // (NEXT STEP) z' = %eax + addl %r12d,%ecx // dst += ... + movl %eax,%r12d // (NEXT STEP) z' = %eax + roll $14,%ecx // dst <<< s + addl %edx,%ecx // dst += x + notl %r11d // not z + leal -405537848(%rbx,%r10,1),%ebx // Const + dst + ... + andl %ecx,%r12d // x & z + andl %edx,%r11d // y & (not z) + movl 36(%rsi),%r10d // (NEXT STEP) X[9] + orl %r11d,%r12d // (y & (not z)) | (x & z) + movl %edx,%r11d // (NEXT STEP) z' = %edx + addl %r12d,%ebx // dst += ... + movl %edx,%r12d // (NEXT STEP) z' = %edx + roll $20,%ebx // dst <<< s + addl %ecx,%ebx // dst += x + notl %r11d // not z + leal 568446438(%rax,%r10,1),%eax // Const + dst + ... + andl %ebx,%r12d // x & z + andl %ecx,%r11d // y & (not z) + movl 56(%rsi),%r10d // (NEXT STEP) X[14] + orl %r11d,%r12d // (y & (not z)) | (x & z) + movl %ecx,%r11d // (NEXT STEP) z' = %ecx + addl %r12d,%eax // dst += ... + movl %ecx,%r12d // (NEXT STEP) z' = %ecx + roll $5,%eax // dst <<< s + addl %ebx,%eax // dst += x + notl %r11d // not z + leal -1019803690(%rdx,%r10,1),%edx // Const + dst + ... + andl %eax,%r12d // x & z + andl %ebx,%r11d // y & (not z) + movl 12(%rsi),%r10d // (NEXT STEP) X[3] + orl %r11d,%r12d // (y & (not z)) | (x & z) + movl %ebx,%r11d // (NEXT STEP) z' = %ebx + addl %r12d,%edx // dst += ... + movl %ebx,%r12d // (NEXT STEP) z' = %ebx + roll $9,%edx // dst <<< s + addl %eax,%edx // dst += x + notl %r11d // not z + leal -187363961(%rcx,%r10,1),%ecx // Const + dst + ... + andl %edx,%r12d // x & z + andl %eax,%r11d // y & (not z) + movl 32(%rsi),%r10d // (NEXT STEP) X[8] + orl %r11d,%r12d // (y & (not z)) | (x & z) + movl %eax,%r11d // (NEXT STEP) z' = %eax + addl %r12d,%ecx // dst += ... + movl %eax,%r12d // (NEXT STEP) z' = %eax + roll $14,%ecx // dst <<< s + addl %edx,%ecx // dst += x + notl %r11d // not z + leal 1163531501(%rbx,%r10,1),%ebx // Const + dst + ... + andl %ecx,%r12d // x & z + andl %edx,%r11d // y & (not z) + movl 52(%rsi),%r10d // (NEXT STEP) X[13] + orl %r11d,%r12d // (y & (not z)) | (x & z) + movl %edx,%r11d // (NEXT STEP) z' = %edx + addl %r12d,%ebx // dst += ... + movl %edx,%r12d // (NEXT STEP) z' = %edx + roll $20,%ebx // dst <<< s + addl %ecx,%ebx // dst += x + notl %r11d // not z + leal -1444681467(%rax,%r10,1),%eax // Const + dst + ... + andl %ebx,%r12d // x & z + andl %ecx,%r11d // y & (not z) + movl 8(%rsi),%r10d // (NEXT STEP) X[2] + orl %r11d,%r12d // (y & (not z)) | (x & z) + movl %ecx,%r11d // (NEXT STEP) z' = %ecx + addl %r12d,%eax // dst += ... + movl %ecx,%r12d // (NEXT STEP) z' = %ecx + roll $5,%eax // dst <<< s + addl %ebx,%eax // dst += x + notl %r11d // not z + leal -51403784(%rdx,%r10,1),%edx // Const + dst + ... + andl %eax,%r12d // x & z + andl %ebx,%r11d // y & (not z) + movl 28(%rsi),%r10d // (NEXT STEP) X[7] + orl %r11d,%r12d // (y & (not z)) | (x & z) + movl %ebx,%r11d // (NEXT STEP) z' = %ebx + addl %r12d,%edx // dst += ... + movl %ebx,%r12d // (NEXT STEP) z' = %ebx + roll $9,%edx // dst <<< s + addl %eax,%edx // dst += x + notl %r11d // not z + leal 1735328473(%rcx,%r10,1),%ecx // Const + dst + ... + andl %edx,%r12d // x & z + andl %eax,%r11d // y & (not z) + movl 48(%rsi),%r10d // (NEXT STEP) X[12] + orl %r11d,%r12d // (y & (not z)) | (x & z) + movl %eax,%r11d // (NEXT STEP) z' = %eax + addl %r12d,%ecx // dst += ... + movl %eax,%r12d // (NEXT STEP) z' = %eax + roll $14,%ecx // dst <<< s + addl %edx,%ecx // dst += x + notl %r11d // not z + leal -1926607734(%rbx,%r10,1),%ebx // Const + dst + ... + andl %ecx,%r12d // x & z + andl %edx,%r11d // y & (not z) + movl 0(%rsi),%r10d // (NEXT STEP) X[0] + orl %r11d,%r12d // (y & (not z)) | (x & z) + movl %edx,%r11d // (NEXT STEP) z' = %edx + addl %r12d,%ebx // dst += ... + movl %edx,%r12d // (NEXT STEP) z' = %edx + roll $20,%ebx // dst <<< s + addl %ecx,%ebx // dst += x + movl 20(%rsi),%r10d // (NEXT STEP) X[5] + movl %ecx,%r11d // (NEXT STEP) y' = %ecx + leal -378558(%rax,%r10,1),%eax // Const + dst + ... + movl 32(%rsi),%r10d // (NEXT STEP) X[8] + xorl %edx,%r11d // z ^ ... + xorl %ebx,%r11d // x ^ ... + addl %r11d,%eax // dst += ... + roll $4,%eax // dst <<< s + movl %ebx,%r11d // (NEXT STEP) y' = %ebx + addl %ebx,%eax // dst += x + leal -2022574463(%rdx,%r10,1),%edx // Const + dst + ... + movl 44(%rsi),%r10d // (NEXT STEP) X[11] + xorl %ecx,%r11d // z ^ ... + xorl %eax,%r11d // x ^ ... + addl %r11d,%edx // dst += ... + roll $11,%edx // dst <<< s + movl %eax,%r11d // (NEXT STEP) y' = %eax + addl %eax,%edx // dst += x + leal 1839030562(%rcx,%r10,1),%ecx // Const + dst + ... + movl 56(%rsi),%r10d // (NEXT STEP) X[14] + xorl %ebx,%r11d // z ^ ... + xorl %edx,%r11d // x ^ ... + addl %r11d,%ecx // dst += ... + roll $16,%ecx // dst <<< s + movl %edx,%r11d // (NEXT STEP) y' = %edx + addl %edx,%ecx // dst += x + leal -35309556(%rbx,%r10,1),%ebx // Const + dst + ... + movl 4(%rsi),%r10d // (NEXT STEP) X[1] + xorl %eax,%r11d // z ^ ... + xorl %ecx,%r11d // x ^ ... + addl %r11d,%ebx // dst += ... + roll $23,%ebx // dst <<< s + movl %ecx,%r11d // (NEXT STEP) y' = %ecx + addl %ecx,%ebx // dst += x + leal -1530992060(%rax,%r10,1),%eax // Const + dst + ... + movl 16(%rsi),%r10d // (NEXT STEP) X[4] + xorl %edx,%r11d // z ^ ... + xorl %ebx,%r11d // x ^ ... + addl %r11d,%eax // dst += ... + roll $4,%eax // dst <<< s + movl %ebx,%r11d // (NEXT STEP) y' = %ebx + addl %ebx,%eax // dst += x + leal 1272893353(%rdx,%r10,1),%edx // Const + dst + ... + movl 28(%rsi),%r10d // (NEXT STEP) X[7] + xorl %ecx,%r11d // z ^ ... + xorl %eax,%r11d // x ^ ... + addl %r11d,%edx // dst += ... + roll $11,%edx // dst <<< s + movl %eax,%r11d // (NEXT STEP) y' = %eax + addl %eax,%edx // dst += x + leal -155497632(%rcx,%r10,1),%ecx // Const + dst + ... + movl 40(%rsi),%r10d // (NEXT STEP) X[10] + xorl %ebx,%r11d // z ^ ... + xorl %edx,%r11d // x ^ ... + addl %r11d,%ecx // dst += ... + roll $16,%ecx // dst <<< s + movl %edx,%r11d // (NEXT STEP) y' = %edx + addl %edx,%ecx // dst += x + leal -1094730640(%rbx,%r10,1),%ebx // Const + dst + ... + movl 52(%rsi),%r10d // (NEXT STEP) X[13] + xorl %eax,%r11d // z ^ ... + xorl %ecx,%r11d // x ^ ... + addl %r11d,%ebx // dst += ... + roll $23,%ebx // dst <<< s + movl %ecx,%r11d // (NEXT STEP) y' = %ecx + addl %ecx,%ebx // dst += x + leal 681279174(%rax,%r10,1),%eax // Const + dst + ... + movl 0(%rsi),%r10d // (NEXT STEP) X[0] + xorl %edx,%r11d // z ^ ... + xorl %ebx,%r11d // x ^ ... + addl %r11d,%eax // dst += ... + roll $4,%eax // dst <<< s + movl %ebx,%r11d // (NEXT STEP) y' = %ebx + addl %ebx,%eax // dst += x + leal -358537222(%rdx,%r10,1),%edx // Const + dst + ... + movl 12(%rsi),%r10d // (NEXT STEP) X[3] + xorl %ecx,%r11d // z ^ ... + xorl %eax,%r11d // x ^ ... + addl %r11d,%edx // dst += ... + roll $11,%edx // dst <<< s + movl %eax,%r11d // (NEXT STEP) y' = %eax + addl %eax,%edx // dst += x + leal -722521979(%rcx,%r10,1),%ecx // Const + dst + ... + movl 24(%rsi),%r10d // (NEXT STEP) X[6] + xorl %ebx,%r11d // z ^ ... + xorl %edx,%r11d // x ^ ... + addl %r11d,%ecx // dst += ... + roll $16,%ecx // dst <<< s + movl %edx,%r11d // (NEXT STEP) y' = %edx + addl %edx,%ecx // dst += x + leal 76029189(%rbx,%r10,1),%ebx // Const + dst + ... + movl 36(%rsi),%r10d // (NEXT STEP) X[9] + xorl %eax,%r11d // z ^ ... + xorl %ecx,%r11d // x ^ ... + addl %r11d,%ebx // dst += ... + roll $23,%ebx // dst <<< s + movl %ecx,%r11d // (NEXT STEP) y' = %ecx + addl %ecx,%ebx // dst += x + leal -640364487(%rax,%r10,1),%eax // Const + dst + ... + movl 48(%rsi),%r10d // (NEXT STEP) X[12] + xorl %edx,%r11d // z ^ ... + xorl %ebx,%r11d // x ^ ... + addl %r11d,%eax // dst += ... + roll $4,%eax // dst <<< s + movl %ebx,%r11d // (NEXT STEP) y' = %ebx + addl %ebx,%eax // dst += x + leal -421815835(%rdx,%r10,1),%edx // Const + dst + ... + movl 60(%rsi),%r10d // (NEXT STEP) X[15] + xorl %ecx,%r11d // z ^ ... + xorl %eax,%r11d // x ^ ... + addl %r11d,%edx // dst += ... + roll $11,%edx // dst <<< s + movl %eax,%r11d // (NEXT STEP) y' = %eax + addl %eax,%edx // dst += x + leal 530742520(%rcx,%r10,1),%ecx // Const + dst + ... + movl 8(%rsi),%r10d // (NEXT STEP) X[2] + xorl %ebx,%r11d // z ^ ... + xorl %edx,%r11d // x ^ ... + addl %r11d,%ecx // dst += ... + roll $16,%ecx // dst <<< s + movl %edx,%r11d // (NEXT STEP) y' = %edx + addl %edx,%ecx // dst += x + leal -995338651(%rbx,%r10,1),%ebx // Const + dst + ... + movl 0(%rsi),%r10d // (NEXT STEP) X[0] + xorl %eax,%r11d // z ^ ... + xorl %ecx,%r11d // x ^ ... + addl %r11d,%ebx // dst += ... + roll $23,%ebx // dst <<< s + movl %ecx,%r11d // (NEXT STEP) y' = %ecx + addl %ecx,%ebx // dst += x + movl 0(%rsi),%r10d // (NEXT STEP) X[0] movl $0xffffffff,%r11d - xorl %edx,%r11d - leal -198630844(%rax,%r10,1),%eax - orl %ebx,%r11d - xorl %ecx,%r11d - addl %r11d,%eax - movl 28(%rsi),%r10d + xorl %edx,%r11d // (NEXT STEP) not z' = not %edx + leal -198630844(%rax,%r10,1),%eax // Const + dst + ... + orl %ebx,%r11d // x | ... + xorl %ecx,%r11d // y ^ ... + addl %r11d,%eax // dst += ... + movl 28(%rsi),%r10d // (NEXT STEP) X[7] movl $0xffffffff,%r11d - roll $6,%eax - xorl %ecx,%r11d - addl %ebx,%eax - leal 1126891415(%rdx,%r10,1),%edx - orl %eax,%r11d - xorl %ebx,%r11d - addl %r11d,%edx - movl 56(%rsi),%r10d + roll $6,%eax // dst <<< s + xorl %ecx,%r11d // (NEXT STEP) not z' = not %ecx + addl %ebx,%eax // dst += x + leal 1126891415(%rdx,%r10,1),%edx // Const + dst + ... + orl %eax,%r11d // x | ... + xorl %ebx,%r11d // y ^ ... + addl %r11d,%edx // dst += ... + movl 56(%rsi),%r10d // (NEXT STEP) X[14] movl $0xffffffff,%r11d - roll $10,%edx - xorl %ebx,%r11d - addl %eax,%edx - leal -1416354905(%rcx,%r10,1),%ecx - orl %edx,%r11d - xorl %eax,%r11d - addl %r11d,%ecx - movl 20(%rsi),%r10d + roll $10,%edx // dst <<< s + xorl %ebx,%r11d // (NEXT STEP) not z' = not %ebx + addl %eax,%edx // dst += x + leal -1416354905(%rcx,%r10,1),%ecx // Const + dst + ... + orl %edx,%r11d // x | ... + xorl %eax,%r11d // y ^ ... + addl %r11d,%ecx // dst += ... + movl 20(%rsi),%r10d // (NEXT STEP) X[5] movl $0xffffffff,%r11d - roll $15,%ecx - xorl %eax,%r11d - addl %edx,%ecx - leal -57434055(%rbx,%r10,1),%ebx - orl %ecx,%r11d - xorl %edx,%r11d - addl %r11d,%ebx - movl 48(%rsi),%r10d + roll $15,%ecx // dst <<< s + xorl %eax,%r11d // (NEXT STEP) not z' = not %eax + addl %edx,%ecx // dst += x + leal -57434055(%rbx,%r10,1),%ebx // Const + dst + ... + orl %ecx,%r11d // x | ... + xorl %edx,%r11d // y ^ ... + addl %r11d,%ebx // dst += ... + movl 48(%rsi),%r10d // (NEXT STEP) X[12] movl $0xffffffff,%r11d - roll $21,%ebx - xorl %edx,%r11d - addl %ecx,%ebx - leal 1700485571(%rax,%r10,1),%eax - orl %ebx,%r11d - xorl %ecx,%r11d - addl %r11d,%eax - movl 12(%rsi),%r10d + roll $21,%ebx // dst <<< s + xorl %edx,%r11d // (NEXT STEP) not z' = not %edx + addl %ecx,%ebx // dst += x + leal 1700485571(%rax,%r10,1),%eax // Const + dst + ... + orl %ebx,%r11d // x | ... + xorl %ecx,%r11d // y ^ ... + addl %r11d,%eax // dst += ... + movl 12(%rsi),%r10d // (NEXT STEP) X[3] movl $0xffffffff,%r11d - roll $6,%eax - xorl %ecx,%r11d - addl %ebx,%eax - leal -1894986606(%rdx,%r10,1),%edx - orl %eax,%r11d - xorl %ebx,%r11d - addl %r11d,%edx - movl 40(%rsi),%r10d + roll $6,%eax // dst <<< s + xorl %ecx,%r11d // (NEXT STEP) not z' = not %ecx + addl %ebx,%eax // dst += x + leal -1894986606(%rdx,%r10,1),%edx // Const + dst + ... + orl %eax,%r11d // x | ... + xorl %ebx,%r11d // y ^ ... + addl %r11d,%edx // dst += ... + movl 40(%rsi),%r10d // (NEXT STEP) X[10] movl $0xffffffff,%r11d - roll $10,%edx - xorl %ebx,%r11d - addl %eax,%edx - leal -1051523(%rcx,%r10,1),%ecx - orl %edx,%r11d - xorl %eax,%r11d - addl %r11d,%ecx - movl 4(%rsi),%r10d + roll $10,%edx // dst <<< s + xorl %ebx,%r11d // (NEXT STEP) not z' = not %ebx + addl %eax,%edx // dst += x + leal -1051523(%rcx,%r10,1),%ecx // Const + dst + ... + orl %edx,%r11d // x | ... + xorl %eax,%r11d // y ^ ... + addl %r11d,%ecx // dst += ... + movl 4(%rsi),%r10d // (NEXT STEP) X[1] movl $0xffffffff,%r11d - roll $15,%ecx - xorl %eax,%r11d - addl %edx,%ecx - leal -2054922799(%rbx,%r10,1),%ebx - orl %ecx,%r11d - xorl %edx,%r11d - addl %r11d,%ebx - movl 32(%rsi),%r10d + roll $15,%ecx // dst <<< s + xorl %eax,%r11d // (NEXT STEP) not z' = not %eax + addl %edx,%ecx // dst += x + leal -2054922799(%rbx,%r10,1),%ebx // Const + dst + ... + orl %ecx,%r11d // x | ... + xorl %edx,%r11d // y ^ ... + addl %r11d,%ebx // dst += ... + movl 32(%rsi),%r10d // (NEXT STEP) X[8] movl $0xffffffff,%r11d - roll $21,%ebx - xorl %edx,%r11d - addl %ecx,%ebx - leal 1873313359(%rax,%r10,1),%eax - orl %ebx,%r11d - xorl %ecx,%r11d - addl %r11d,%eax - movl 60(%rsi),%r10d + roll $21,%ebx // dst <<< s + xorl %edx,%r11d // (NEXT STEP) not z' = not %edx + addl %ecx,%ebx // dst += x + leal 1873313359(%rax,%r10,1),%eax // Const + dst + ... + orl %ebx,%r11d // x | ... + xorl %ecx,%r11d // y ^ ... + addl %r11d,%eax // dst += ... + movl 60(%rsi),%r10d // (NEXT STEP) X[15] movl $0xffffffff,%r11d - roll $6,%eax - xorl %ecx,%r11d - addl %ebx,%eax - leal -30611744(%rdx,%r10,1),%edx - orl %eax,%r11d - xorl %ebx,%r11d - addl %r11d,%edx - movl 24(%rsi),%r10d + roll $6,%eax // dst <<< s + xorl %ecx,%r11d // (NEXT STEP) not z' = not %ecx + addl %ebx,%eax // dst += x + leal -30611744(%rdx,%r10,1),%edx // Const + dst + ... + orl %eax,%r11d // x | ... + xorl %ebx,%r11d // y ^ ... + addl %r11d,%edx // dst += ... + movl 24(%rsi),%r10d // (NEXT STEP) X[6] movl $0xffffffff,%r11d - roll $10,%edx - xorl %ebx,%r11d - addl %eax,%edx - leal -1560198380(%rcx,%r10,1),%ecx - orl %edx,%r11d - xorl %eax,%r11d - addl %r11d,%ecx - movl 52(%rsi),%r10d + roll $10,%edx // dst <<< s + xorl %ebx,%r11d // (NEXT STEP) not z' = not %ebx + addl %eax,%edx // dst += x + leal -1560198380(%rcx,%r10,1),%ecx // Const + dst + ... + orl %edx,%r11d // x | ... + xorl %eax,%r11d // y ^ ... + addl %r11d,%ecx // dst += ... + movl 52(%rsi),%r10d // (NEXT STEP) X[13] movl $0xffffffff,%r11d - roll $15,%ecx - xorl %eax,%r11d - addl %edx,%ecx - leal 1309151649(%rbx,%r10,1),%ebx - orl %ecx,%r11d - xorl %edx,%r11d - addl %r11d,%ebx - movl 16(%rsi),%r10d + roll $15,%ecx // dst <<< s + xorl %eax,%r11d // (NEXT STEP) not z' = not %eax + addl %edx,%ecx // dst += x + leal 1309151649(%rbx,%r10,1),%ebx // Const + dst + ... + orl %ecx,%r11d // x | ... + xorl %edx,%r11d // y ^ ... + addl %r11d,%ebx // dst += ... + movl 16(%rsi),%r10d // (NEXT STEP) X[4] movl $0xffffffff,%r11d - roll $21,%ebx - xorl %edx,%r11d - addl %ecx,%ebx - leal -145523070(%rax,%r10,1),%eax - orl %ebx,%r11d - xorl %ecx,%r11d - addl %r11d,%eax - movl 44(%rsi),%r10d + roll $21,%ebx // dst <<< s + xorl %edx,%r11d // (NEXT STEP) not z' = not %edx + addl %ecx,%ebx // dst += x + leal -145523070(%rax,%r10,1),%eax // Const + dst + ... + orl %ebx,%r11d // x | ... + xorl %ecx,%r11d // y ^ ... + addl %r11d,%eax // dst += ... + movl 44(%rsi),%r10d // (NEXT STEP) X[11] movl $0xffffffff,%r11d - roll $6,%eax - xorl %ecx,%r11d - addl %ebx,%eax - leal -1120210379(%rdx,%r10,1),%edx - orl %eax,%r11d - xorl %ebx,%r11d - addl %r11d,%edx - movl 8(%rsi),%r10d + roll $6,%eax // dst <<< s + xorl %ecx,%r11d // (NEXT STEP) not z' = not %ecx + addl %ebx,%eax // dst += x + leal -1120210379(%rdx,%r10,1),%edx // Const + dst + ... + orl %eax,%r11d // x | ... + xorl %ebx,%r11d // y ^ ... + addl %r11d,%edx // dst += ... + movl 8(%rsi),%r10d // (NEXT STEP) X[2] movl $0xffffffff,%r11d - roll $10,%edx - xorl %ebx,%r11d - addl %eax,%edx - leal 718787259(%rcx,%r10,1),%ecx - orl %edx,%r11d - xorl %eax,%r11d - addl %r11d,%ecx - movl 36(%rsi),%r10d + roll $10,%edx // dst <<< s + xorl %ebx,%r11d // (NEXT STEP) not z' = not %ebx + addl %eax,%edx // dst += x + leal 718787259(%rcx,%r10,1),%ecx // Const + dst + ... + orl %edx,%r11d // x | ... + xorl %eax,%r11d // y ^ ... + addl %r11d,%ecx // dst += ... + movl 36(%rsi),%r10d // (NEXT STEP) X[9] movl $0xffffffff,%r11d - roll $15,%ecx - xorl %eax,%r11d - addl %edx,%ecx - leal -343485551(%rbx,%r10,1),%ebx - orl %ecx,%r11d - xorl %edx,%r11d - addl %r11d,%ebx - movl 0(%rsi),%r10d + roll $15,%ecx // dst <<< s + xorl %eax,%r11d // (NEXT STEP) not z' = not %eax + addl %edx,%ecx // dst += x + leal -343485551(%rbx,%r10,1),%ebx // Const + dst + ... + orl %ecx,%r11d // x | ... + xorl %edx,%r11d // y ^ ... + addl %r11d,%ebx // dst += ... + movl 0(%rsi),%r10d // (NEXT STEP) X[0] movl $0xffffffff,%r11d - roll $21,%ebx - xorl %edx,%r11d - addl %ecx,%ebx - + roll $21,%ebx // dst <<< s + xorl %edx,%r11d // (NEXT STEP) not z' = not %edx + addl %ecx,%ebx // dst += x +// add old values of A, B, C, D addl %r8d,%eax addl %r9d,%ebx addl %r14d,%ecx addl %r15d,%edx - - addq $64,%rsi - cmpq %rdi,%rsi - jb .Lloop - +// loop control + addq $64,%rsi // ptr += 64 + cmpq %rdi,%rsi // cmp end with ptr + jb .Lloop // jmp if ptr < end +// END of loop over 16-word blocks .Lend: - movl %eax,0(%rbp) - movl %ebx,4(%rbp) - movl %ecx,8(%rbp) - movl %edx,12(%rbp) + movl %eax,0(%rbp) // ctx->A = A + movl %ebx,4(%rbp) // ctx->B = B + movl %ecx,8(%rbp) // ctx->C = C + movl %edx,12(%rbp) // ctx->D = D movq (%rsp),%r15 .cfi_restore r15 diff --git a/third_party/boringssl/gen/crypto/md5-x86_64-win.asm b/third_party/boringssl/gen/crypto/md5-x86_64-win.asm index 5cfac974f..85148720d 100644 --- a/third_party/boringssl/gen/crypto/md5-x86_64-win.asm +++ b/third_party/boringssl/gen/crypto/md5-x86_64-win.asm @@ -41,651 +41,651 @@ _CET_ENDBR $L$prologue: - - - - mov rbp,rdi - shl rdx,6 - lea rdi,[rdx*1+rsi] - mov eax,DWORD[rbp] - mov ebx,DWORD[4+rbp] - mov ecx,DWORD[8+rbp] - mov edx,DWORD[12+rbp] - - - - - - - - cmp rsi,rdi - je NEAR $L$end - - -$L$loop: +; rdi = arg #1 (ctx, MD5_CTX pointer) +; rsi = arg #2 (ptr, data pointer) +; rdx = arg #3 (nbr, number of 16-word blocks to process) + mov rbp,rdi ; rbp = ctx + shl rdx,6 ; rdx = nbr in bytes + lea rdi,[rdx*1+rsi] ; rdi = end + mov eax,DWORD[rbp] ; eax = ctx->A + mov ebx,DWORD[4+rbp] ; ebx = ctx->B + mov ecx,DWORD[8+rbp] ; ecx = ctx->C + mov edx,DWORD[12+rbp] ; edx = ctx->D +; end is 'rdi' +; ptr is 'rsi' +; A is 'eax' +; B is 'ebx' +; C is 'ecx' +; D is 'edx' + + cmp rsi,rdi ; cmp end with ptr + je NEAR $L$end ; jmp if ptr == end + +; BEGIN of loop over 16-word blocks +$L$loop: ; save old values of A, B, C, D mov r8d,eax mov r9d,ebx mov r14d,ecx mov r15d,edx - mov r10d,DWORD[rsi] - mov r11d,edx - xor r11d,ecx - lea eax,[((-680876936))+r10*1+rax] - and r11d,ebx - xor r11d,edx - mov r10d,DWORD[4+rsi] - add eax,r11d - rol eax,7 - mov r11d,ecx - add eax,ebx - xor r11d,ebx - lea edx,[((-389564586))+r10*1+rdx] - and r11d,eax - xor r11d,ecx - mov r10d,DWORD[8+rsi] - add edx,r11d - rol edx,12 - mov r11d,ebx - add edx,eax - xor r11d,eax - lea ecx,[606105819+r10*1+rcx] - and r11d,edx - xor r11d,ebx - mov r10d,DWORD[12+rsi] - add ecx,r11d - rol ecx,17 - mov r11d,eax - add ecx,edx - xor r11d,edx - lea ebx,[((-1044525330))+r10*1+rbx] - and r11d,ecx - xor r11d,eax - mov r10d,DWORD[16+rsi] - add ebx,r11d - rol ebx,22 - mov r11d,edx - add ebx,ecx - xor r11d,ecx - lea eax,[((-176418897))+r10*1+rax] - and r11d,ebx - xor r11d,edx - mov r10d,DWORD[20+rsi] - add eax,r11d - rol eax,7 - mov r11d,ecx - add eax,ebx - xor r11d,ebx - lea edx,[1200080426+r10*1+rdx] - and r11d,eax - xor r11d,ecx - mov r10d,DWORD[24+rsi] - add edx,r11d - rol edx,12 - mov r11d,ebx - add edx,eax - xor r11d,eax - lea ecx,[((-1473231341))+r10*1+rcx] - and r11d,edx - xor r11d,ebx - mov r10d,DWORD[28+rsi] - add ecx,r11d - rol ecx,17 - mov r11d,eax - add ecx,edx - xor r11d,edx - lea ebx,[((-45705983))+r10*1+rbx] - and r11d,ecx - xor r11d,eax - mov r10d,DWORD[32+rsi] - add ebx,r11d - rol ebx,22 - mov r11d,edx - add ebx,ecx - xor r11d,ecx - lea eax,[1770035416+r10*1+rax] - and r11d,ebx - xor r11d,edx - mov r10d,DWORD[36+rsi] - add eax,r11d - rol eax,7 - mov r11d,ecx - add eax,ebx - xor r11d,ebx - lea edx,[((-1958414417))+r10*1+rdx] - and r11d,eax - xor r11d,ecx - mov r10d,DWORD[40+rsi] - add edx,r11d - rol edx,12 - mov r11d,ebx - add edx,eax - xor r11d,eax - lea ecx,[((-42063))+r10*1+rcx] - and r11d,edx - xor r11d,ebx - mov r10d,DWORD[44+rsi] - add ecx,r11d - rol ecx,17 - mov r11d,eax - add ecx,edx - xor r11d,edx - lea ebx,[((-1990404162))+r10*1+rbx] - and r11d,ecx - xor r11d,eax - mov r10d,DWORD[48+rsi] - add ebx,r11d - rol ebx,22 - mov r11d,edx - add ebx,ecx - xor r11d,ecx - lea eax,[1804603682+r10*1+rax] - and r11d,ebx - xor r11d,edx - mov r10d,DWORD[52+rsi] - add eax,r11d - rol eax,7 - mov r11d,ecx - add eax,ebx - xor r11d,ebx - lea edx,[((-40341101))+r10*1+rdx] - and r11d,eax - xor r11d,ecx - mov r10d,DWORD[56+rsi] - add edx,r11d - rol edx,12 - mov r11d,ebx - add edx,eax - xor r11d,eax - lea ecx,[((-1502002290))+r10*1+rcx] - and r11d,edx - xor r11d,ebx - mov r10d,DWORD[60+rsi] - add ecx,r11d - rol ecx,17 - mov r11d,eax - add ecx,edx - xor r11d,edx - lea ebx,[1236535329+r10*1+rbx] - and r11d,ecx - xor r11d,eax - mov r10d,DWORD[rsi] - add ebx,r11d - rol ebx,22 - mov r11d,edx - add ebx,ecx - mov r10d,DWORD[4+rsi] - mov r11d,edx - mov r12d,edx - not r11d - lea eax,[((-165796510))+r10*1+rax] - and r12d,ebx - and r11d,ecx - mov r10d,DWORD[24+rsi] - or r12d,r11d - mov r11d,ecx - add eax,r12d - mov r12d,ecx - rol eax,5 - add eax,ebx - not r11d - lea edx,[((-1069501632))+r10*1+rdx] - and r12d,eax - and r11d,ebx - mov r10d,DWORD[44+rsi] - or r12d,r11d - mov r11d,ebx - add edx,r12d - mov r12d,ebx - rol edx,9 - add edx,eax - not r11d - lea ecx,[643717713+r10*1+rcx] - and r12d,edx - and r11d,eax - mov r10d,DWORD[rsi] - or r12d,r11d - mov r11d,eax - add ecx,r12d - mov r12d,eax - rol ecx,14 - add ecx,edx - not r11d - lea ebx,[((-373897302))+r10*1+rbx] - and r12d,ecx - and r11d,edx - mov r10d,DWORD[20+rsi] - or r12d,r11d - mov r11d,edx - add ebx,r12d - mov r12d,edx - rol ebx,20 - add ebx,ecx - not r11d - lea eax,[((-701558691))+r10*1+rax] - and r12d,ebx - and r11d,ecx - mov r10d,DWORD[40+rsi] - or r12d,r11d - mov r11d,ecx - add eax,r12d - mov r12d,ecx - rol eax,5 - add eax,ebx - not r11d - lea edx,[38016083+r10*1+rdx] - and r12d,eax - and r11d,ebx - mov r10d,DWORD[60+rsi] - or r12d,r11d - mov r11d,ebx - add edx,r12d - mov r12d,ebx - rol edx,9 - add edx,eax - not r11d - lea ecx,[((-660478335))+r10*1+rcx] - and r12d,edx - and r11d,eax - mov r10d,DWORD[16+rsi] - or r12d,r11d - mov r11d,eax - add ecx,r12d - mov r12d,eax - rol ecx,14 - add ecx,edx - not r11d - lea ebx,[((-405537848))+r10*1+rbx] - and r12d,ecx - and r11d,edx - mov r10d,DWORD[36+rsi] - or r12d,r11d - mov r11d,edx - add ebx,r12d - mov r12d,edx - rol ebx,20 - add ebx,ecx - not r11d - lea eax,[568446438+r10*1+rax] - and r12d,ebx - and r11d,ecx - mov r10d,DWORD[56+rsi] - or r12d,r11d - mov r11d,ecx - add eax,r12d - mov r12d,ecx - rol eax,5 - add eax,ebx - not r11d - lea edx,[((-1019803690))+r10*1+rdx] - and r12d,eax - and r11d,ebx - mov r10d,DWORD[12+rsi] - or r12d,r11d - mov r11d,ebx - add edx,r12d - mov r12d,ebx - rol edx,9 - add edx,eax - not r11d - lea ecx,[((-187363961))+r10*1+rcx] - and r12d,edx - and r11d,eax - mov r10d,DWORD[32+rsi] - or r12d,r11d - mov r11d,eax - add ecx,r12d - mov r12d,eax - rol ecx,14 - add ecx,edx - not r11d - lea ebx,[1163531501+r10*1+rbx] - and r12d,ecx - and r11d,edx - mov r10d,DWORD[52+rsi] - or r12d,r11d - mov r11d,edx - add ebx,r12d - mov r12d,edx - rol ebx,20 - add ebx,ecx - not r11d - lea eax,[((-1444681467))+r10*1+rax] - and r12d,ebx - and r11d,ecx - mov r10d,DWORD[8+rsi] - or r12d,r11d - mov r11d,ecx - add eax,r12d - mov r12d,ecx - rol eax,5 - add eax,ebx - not r11d - lea edx,[((-51403784))+r10*1+rdx] - and r12d,eax - and r11d,ebx - mov r10d,DWORD[28+rsi] - or r12d,r11d - mov r11d,ebx - add edx,r12d - mov r12d,ebx - rol edx,9 - add edx,eax - not r11d - lea ecx,[1735328473+r10*1+rcx] - and r12d,edx - and r11d,eax - mov r10d,DWORD[48+rsi] - or r12d,r11d - mov r11d,eax - add ecx,r12d - mov r12d,eax - rol ecx,14 - add ecx,edx - not r11d - lea ebx,[((-1926607734))+r10*1+rbx] - and r12d,ecx - and r11d,edx - mov r10d,DWORD[rsi] - or r12d,r11d - mov r11d,edx - add ebx,r12d - mov r12d,edx - rol ebx,20 - add ebx,ecx - mov r10d,DWORD[20+rsi] - mov r11d,ecx - lea eax,[((-378558))+r10*1+rax] - mov r10d,DWORD[32+rsi] - xor r11d,edx - xor r11d,ebx - add eax,r11d - rol eax,4 - mov r11d,ebx - add eax,ebx - lea edx,[((-2022574463))+r10*1+rdx] - mov r10d,DWORD[44+rsi] - xor r11d,ecx - xor r11d,eax - add edx,r11d - rol edx,11 - mov r11d,eax - add edx,eax - lea ecx,[1839030562+r10*1+rcx] - mov r10d,DWORD[56+rsi] - xor r11d,ebx - xor r11d,edx - add ecx,r11d - rol ecx,16 - mov r11d,edx - add ecx,edx - lea ebx,[((-35309556))+r10*1+rbx] - mov r10d,DWORD[4+rsi] - xor r11d,eax - xor r11d,ecx - add ebx,r11d - rol ebx,23 - mov r11d,ecx - add ebx,ecx - lea eax,[((-1530992060))+r10*1+rax] - mov r10d,DWORD[16+rsi] - xor r11d,edx - xor r11d,ebx - add eax,r11d - rol eax,4 - mov r11d,ebx - add eax,ebx - lea edx,[1272893353+r10*1+rdx] - mov r10d,DWORD[28+rsi] - xor r11d,ecx - xor r11d,eax - add edx,r11d - rol edx,11 - mov r11d,eax - add edx,eax - lea ecx,[((-155497632))+r10*1+rcx] - mov r10d,DWORD[40+rsi] - xor r11d,ebx - xor r11d,edx - add ecx,r11d - rol ecx,16 - mov r11d,edx - add ecx,edx - lea ebx,[((-1094730640))+r10*1+rbx] - mov r10d,DWORD[52+rsi] - xor r11d,eax - xor r11d,ecx - add ebx,r11d - rol ebx,23 - mov r11d,ecx - add ebx,ecx - lea eax,[681279174+r10*1+rax] - mov r10d,DWORD[rsi] - xor r11d,edx - xor r11d,ebx - add eax,r11d - rol eax,4 - mov r11d,ebx - add eax,ebx - lea edx,[((-358537222))+r10*1+rdx] - mov r10d,DWORD[12+rsi] - xor r11d,ecx - xor r11d,eax - add edx,r11d - rol edx,11 - mov r11d,eax - add edx,eax - lea ecx,[((-722521979))+r10*1+rcx] - mov r10d,DWORD[24+rsi] - xor r11d,ebx - xor r11d,edx - add ecx,r11d - rol ecx,16 - mov r11d,edx - add ecx,edx - lea ebx,[76029189+r10*1+rbx] - mov r10d,DWORD[36+rsi] - xor r11d,eax - xor r11d,ecx - add ebx,r11d - rol ebx,23 - mov r11d,ecx - add ebx,ecx - lea eax,[((-640364487))+r10*1+rax] - mov r10d,DWORD[48+rsi] - xor r11d,edx - xor r11d,ebx - add eax,r11d - rol eax,4 - mov r11d,ebx - add eax,ebx - lea edx,[((-421815835))+r10*1+rdx] - mov r10d,DWORD[60+rsi] - xor r11d,ecx - xor r11d,eax - add edx,r11d - rol edx,11 - mov r11d,eax - add edx,eax - lea ecx,[530742520+r10*1+rcx] - mov r10d,DWORD[8+rsi] - xor r11d,ebx - xor r11d,edx - add ecx,r11d - rol ecx,16 - mov r11d,edx - add ecx,edx - lea ebx,[((-995338651))+r10*1+rbx] - mov r10d,DWORD[rsi] - xor r11d,eax - xor r11d,ecx - add ebx,r11d - rol ebx,23 - mov r11d,ecx - add ebx,ecx - mov r10d,DWORD[rsi] + mov r10d,DWORD[rsi] ; (NEXT STEP) X[0] + mov r11d,edx ; (NEXT STEP) z' = %edx + xor r11d,ecx ; y ^ ... + lea eax,[((-680876936))+r10*1+rax] ; Const + dst + ... + and r11d,ebx ; x & ... + xor r11d,edx ; z ^ ... + mov r10d,DWORD[4+rsi] ; (NEXT STEP) X[1] + add eax,r11d ; dst += ... + rol eax,7 ; dst <<< s + mov r11d,ecx ; (NEXT STEP) z' = %ecx + add eax,ebx ; dst += x + xor r11d,ebx ; y ^ ... + lea edx,[((-389564586))+r10*1+rdx] ; Const + dst + ... + and r11d,eax ; x & ... + xor r11d,ecx ; z ^ ... + mov r10d,DWORD[8+rsi] ; (NEXT STEP) X[2] + add edx,r11d ; dst += ... + rol edx,12 ; dst <<< s + mov r11d,ebx ; (NEXT STEP) z' = %ebx + add edx,eax ; dst += x + xor r11d,eax ; y ^ ... + lea ecx,[606105819+r10*1+rcx] ; Const + dst + ... + and r11d,edx ; x & ... + xor r11d,ebx ; z ^ ... + mov r10d,DWORD[12+rsi] ; (NEXT STEP) X[3] + add ecx,r11d ; dst += ... + rol ecx,17 ; dst <<< s + mov r11d,eax ; (NEXT STEP) z' = %eax + add ecx,edx ; dst += x + xor r11d,edx ; y ^ ... + lea ebx,[((-1044525330))+r10*1+rbx] ; Const + dst + ... + and r11d,ecx ; x & ... + xor r11d,eax ; z ^ ... + mov r10d,DWORD[16+rsi] ; (NEXT STEP) X[4] + add ebx,r11d ; dst += ... + rol ebx,22 ; dst <<< s + mov r11d,edx ; (NEXT STEP) z' = %edx + add ebx,ecx ; dst += x + xor r11d,ecx ; y ^ ... + lea eax,[((-176418897))+r10*1+rax] ; Const + dst + ... + and r11d,ebx ; x & ... + xor r11d,edx ; z ^ ... + mov r10d,DWORD[20+rsi] ; (NEXT STEP) X[5] + add eax,r11d ; dst += ... + rol eax,7 ; dst <<< s + mov r11d,ecx ; (NEXT STEP) z' = %ecx + add eax,ebx ; dst += x + xor r11d,ebx ; y ^ ... + lea edx,[1200080426+r10*1+rdx] ; Const + dst + ... + and r11d,eax ; x & ... + xor r11d,ecx ; z ^ ... + mov r10d,DWORD[24+rsi] ; (NEXT STEP) X[6] + add edx,r11d ; dst += ... + rol edx,12 ; dst <<< s + mov r11d,ebx ; (NEXT STEP) z' = %ebx + add edx,eax ; dst += x + xor r11d,eax ; y ^ ... + lea ecx,[((-1473231341))+r10*1+rcx] ; Const + dst + ... + and r11d,edx ; x & ... + xor r11d,ebx ; z ^ ... + mov r10d,DWORD[28+rsi] ; (NEXT STEP) X[7] + add ecx,r11d ; dst += ... + rol ecx,17 ; dst <<< s + mov r11d,eax ; (NEXT STEP) z' = %eax + add ecx,edx ; dst += x + xor r11d,edx ; y ^ ... + lea ebx,[((-45705983))+r10*1+rbx] ; Const + dst + ... + and r11d,ecx ; x & ... + xor r11d,eax ; z ^ ... + mov r10d,DWORD[32+rsi] ; (NEXT STEP) X[8] + add ebx,r11d ; dst += ... + rol ebx,22 ; dst <<< s + mov r11d,edx ; (NEXT STEP) z' = %edx + add ebx,ecx ; dst += x + xor r11d,ecx ; y ^ ... + lea eax,[1770035416+r10*1+rax] ; Const + dst + ... + and r11d,ebx ; x & ... + xor r11d,edx ; z ^ ... + mov r10d,DWORD[36+rsi] ; (NEXT STEP) X[9] + add eax,r11d ; dst += ... + rol eax,7 ; dst <<< s + mov r11d,ecx ; (NEXT STEP) z' = %ecx + add eax,ebx ; dst += x + xor r11d,ebx ; y ^ ... + lea edx,[((-1958414417))+r10*1+rdx] ; Const + dst + ... + and r11d,eax ; x & ... + xor r11d,ecx ; z ^ ... + mov r10d,DWORD[40+rsi] ; (NEXT STEP) X[10] + add edx,r11d ; dst += ... + rol edx,12 ; dst <<< s + mov r11d,ebx ; (NEXT STEP) z' = %ebx + add edx,eax ; dst += x + xor r11d,eax ; y ^ ... + lea ecx,[((-42063))+r10*1+rcx] ; Const + dst + ... + and r11d,edx ; x & ... + xor r11d,ebx ; z ^ ... + mov r10d,DWORD[44+rsi] ; (NEXT STEP) X[11] + add ecx,r11d ; dst += ... + rol ecx,17 ; dst <<< s + mov r11d,eax ; (NEXT STEP) z' = %eax + add ecx,edx ; dst += x + xor r11d,edx ; y ^ ... + lea ebx,[((-1990404162))+r10*1+rbx] ; Const + dst + ... + and r11d,ecx ; x & ... + xor r11d,eax ; z ^ ... + mov r10d,DWORD[48+rsi] ; (NEXT STEP) X[12] + add ebx,r11d ; dst += ... + rol ebx,22 ; dst <<< s + mov r11d,edx ; (NEXT STEP) z' = %edx + add ebx,ecx ; dst += x + xor r11d,ecx ; y ^ ... + lea eax,[1804603682+r10*1+rax] ; Const + dst + ... + and r11d,ebx ; x & ... + xor r11d,edx ; z ^ ... + mov r10d,DWORD[52+rsi] ; (NEXT STEP) X[13] + add eax,r11d ; dst += ... + rol eax,7 ; dst <<< s + mov r11d,ecx ; (NEXT STEP) z' = %ecx + add eax,ebx ; dst += x + xor r11d,ebx ; y ^ ... + lea edx,[((-40341101))+r10*1+rdx] ; Const + dst + ... + and r11d,eax ; x & ... + xor r11d,ecx ; z ^ ... + mov r10d,DWORD[56+rsi] ; (NEXT STEP) X[14] + add edx,r11d ; dst += ... + rol edx,12 ; dst <<< s + mov r11d,ebx ; (NEXT STEP) z' = %ebx + add edx,eax ; dst += x + xor r11d,eax ; y ^ ... + lea ecx,[((-1502002290))+r10*1+rcx] ; Const + dst + ... + and r11d,edx ; x & ... + xor r11d,ebx ; z ^ ... + mov r10d,DWORD[60+rsi] ; (NEXT STEP) X[15] + add ecx,r11d ; dst += ... + rol ecx,17 ; dst <<< s + mov r11d,eax ; (NEXT STEP) z' = %eax + add ecx,edx ; dst += x + xor r11d,edx ; y ^ ... + lea ebx,[1236535329+r10*1+rbx] ; Const + dst + ... + and r11d,ecx ; x & ... + xor r11d,eax ; z ^ ... + mov r10d,DWORD[rsi] ; (NEXT STEP) X[0] + add ebx,r11d ; dst += ... + rol ebx,22 ; dst <<< s + mov r11d,edx ; (NEXT STEP) z' = %edx + add ebx,ecx ; dst += x + mov r10d,DWORD[4+rsi] ; (NEXT STEP) X[1] + mov r11d,edx ; (NEXT STEP) z' = %edx + mov r12d,edx ; (NEXT STEP) z' = %edx + not r11d ; not z + lea eax,[((-165796510))+r10*1+rax] ; Const + dst + ... + and r12d,ebx ; x & z + and r11d,ecx ; y & (not z) + mov r10d,DWORD[24+rsi] ; (NEXT STEP) X[6] + or r12d,r11d ; (y & (not z)) | (x & z) + mov r11d,ecx ; (NEXT STEP) z' = %ecx + add eax,r12d ; dst += ... + mov r12d,ecx ; (NEXT STEP) z' = %ecx + rol eax,5 ; dst <<< s + add eax,ebx ; dst += x + not r11d ; not z + lea edx,[((-1069501632))+r10*1+rdx] ; Const + dst + ... + and r12d,eax ; x & z + and r11d,ebx ; y & (not z) + mov r10d,DWORD[44+rsi] ; (NEXT STEP) X[11] + or r12d,r11d ; (y & (not z)) | (x & z) + mov r11d,ebx ; (NEXT STEP) z' = %ebx + add edx,r12d ; dst += ... + mov r12d,ebx ; (NEXT STEP) z' = %ebx + rol edx,9 ; dst <<< s + add edx,eax ; dst += x + not r11d ; not z + lea ecx,[643717713+r10*1+rcx] ; Const + dst + ... + and r12d,edx ; x & z + and r11d,eax ; y & (not z) + mov r10d,DWORD[rsi] ; (NEXT STEP) X[0] + or r12d,r11d ; (y & (not z)) | (x & z) + mov r11d,eax ; (NEXT STEP) z' = %eax + add ecx,r12d ; dst += ... + mov r12d,eax ; (NEXT STEP) z' = %eax + rol ecx,14 ; dst <<< s + add ecx,edx ; dst += x + not r11d ; not z + lea ebx,[((-373897302))+r10*1+rbx] ; Const + dst + ... + and r12d,ecx ; x & z + and r11d,edx ; y & (not z) + mov r10d,DWORD[20+rsi] ; (NEXT STEP) X[5] + or r12d,r11d ; (y & (not z)) | (x & z) + mov r11d,edx ; (NEXT STEP) z' = %edx + add ebx,r12d ; dst += ... + mov r12d,edx ; (NEXT STEP) z' = %edx + rol ebx,20 ; dst <<< s + add ebx,ecx ; dst += x + not r11d ; not z + lea eax,[((-701558691))+r10*1+rax] ; Const + dst + ... + and r12d,ebx ; x & z + and r11d,ecx ; y & (not z) + mov r10d,DWORD[40+rsi] ; (NEXT STEP) X[10] + or r12d,r11d ; (y & (not z)) | (x & z) + mov r11d,ecx ; (NEXT STEP) z' = %ecx + add eax,r12d ; dst += ... + mov r12d,ecx ; (NEXT STEP) z' = %ecx + rol eax,5 ; dst <<< s + add eax,ebx ; dst += x + not r11d ; not z + lea edx,[38016083+r10*1+rdx] ; Const + dst + ... + and r12d,eax ; x & z + and r11d,ebx ; y & (not z) + mov r10d,DWORD[60+rsi] ; (NEXT STEP) X[15] + or r12d,r11d ; (y & (not z)) | (x & z) + mov r11d,ebx ; (NEXT STEP) z' = %ebx + add edx,r12d ; dst += ... + mov r12d,ebx ; (NEXT STEP) z' = %ebx + rol edx,9 ; dst <<< s + add edx,eax ; dst += x + not r11d ; not z + lea ecx,[((-660478335))+r10*1+rcx] ; Const + dst + ... + and r12d,edx ; x & z + and r11d,eax ; y & (not z) + mov r10d,DWORD[16+rsi] ; (NEXT STEP) X[4] + or r12d,r11d ; (y & (not z)) | (x & z) + mov r11d,eax ; (NEXT STEP) z' = %eax + add ecx,r12d ; dst += ... + mov r12d,eax ; (NEXT STEP) z' = %eax + rol ecx,14 ; dst <<< s + add ecx,edx ; dst += x + not r11d ; not z + lea ebx,[((-405537848))+r10*1+rbx] ; Const + dst + ... + and r12d,ecx ; x & z + and r11d,edx ; y & (not z) + mov r10d,DWORD[36+rsi] ; (NEXT STEP) X[9] + or r12d,r11d ; (y & (not z)) | (x & z) + mov r11d,edx ; (NEXT STEP) z' = %edx + add ebx,r12d ; dst += ... + mov r12d,edx ; (NEXT STEP) z' = %edx + rol ebx,20 ; dst <<< s + add ebx,ecx ; dst += x + not r11d ; not z + lea eax,[568446438+r10*1+rax] ; Const + dst + ... + and r12d,ebx ; x & z + and r11d,ecx ; y & (not z) + mov r10d,DWORD[56+rsi] ; (NEXT STEP) X[14] + or r12d,r11d ; (y & (not z)) | (x & z) + mov r11d,ecx ; (NEXT STEP) z' = %ecx + add eax,r12d ; dst += ... + mov r12d,ecx ; (NEXT STEP) z' = %ecx + rol eax,5 ; dst <<< s + add eax,ebx ; dst += x + not r11d ; not z + lea edx,[((-1019803690))+r10*1+rdx] ; Const + dst + ... + and r12d,eax ; x & z + and r11d,ebx ; y & (not z) + mov r10d,DWORD[12+rsi] ; (NEXT STEP) X[3] + or r12d,r11d ; (y & (not z)) | (x & z) + mov r11d,ebx ; (NEXT STEP) z' = %ebx + add edx,r12d ; dst += ... + mov r12d,ebx ; (NEXT STEP) z' = %ebx + rol edx,9 ; dst <<< s + add edx,eax ; dst += x + not r11d ; not z + lea ecx,[((-187363961))+r10*1+rcx] ; Const + dst + ... + and r12d,edx ; x & z + and r11d,eax ; y & (not z) + mov r10d,DWORD[32+rsi] ; (NEXT STEP) X[8] + or r12d,r11d ; (y & (not z)) | (x & z) + mov r11d,eax ; (NEXT STEP) z' = %eax + add ecx,r12d ; dst += ... + mov r12d,eax ; (NEXT STEP) z' = %eax + rol ecx,14 ; dst <<< s + add ecx,edx ; dst += x + not r11d ; not z + lea ebx,[1163531501+r10*1+rbx] ; Const + dst + ... + and r12d,ecx ; x & z + and r11d,edx ; y & (not z) + mov r10d,DWORD[52+rsi] ; (NEXT STEP) X[13] + or r12d,r11d ; (y & (not z)) | (x & z) + mov r11d,edx ; (NEXT STEP) z' = %edx + add ebx,r12d ; dst += ... + mov r12d,edx ; (NEXT STEP) z' = %edx + rol ebx,20 ; dst <<< s + add ebx,ecx ; dst += x + not r11d ; not z + lea eax,[((-1444681467))+r10*1+rax] ; Const + dst + ... + and r12d,ebx ; x & z + and r11d,ecx ; y & (not z) + mov r10d,DWORD[8+rsi] ; (NEXT STEP) X[2] + or r12d,r11d ; (y & (not z)) | (x & z) + mov r11d,ecx ; (NEXT STEP) z' = %ecx + add eax,r12d ; dst += ... + mov r12d,ecx ; (NEXT STEP) z' = %ecx + rol eax,5 ; dst <<< s + add eax,ebx ; dst += x + not r11d ; not z + lea edx,[((-51403784))+r10*1+rdx] ; Const + dst + ... + and r12d,eax ; x & z + and r11d,ebx ; y & (not z) + mov r10d,DWORD[28+rsi] ; (NEXT STEP) X[7] + or r12d,r11d ; (y & (not z)) | (x & z) + mov r11d,ebx ; (NEXT STEP) z' = %ebx + add edx,r12d ; dst += ... + mov r12d,ebx ; (NEXT STEP) z' = %ebx + rol edx,9 ; dst <<< s + add edx,eax ; dst += x + not r11d ; not z + lea ecx,[1735328473+r10*1+rcx] ; Const + dst + ... + and r12d,edx ; x & z + and r11d,eax ; y & (not z) + mov r10d,DWORD[48+rsi] ; (NEXT STEP) X[12] + or r12d,r11d ; (y & (not z)) | (x & z) + mov r11d,eax ; (NEXT STEP) z' = %eax + add ecx,r12d ; dst += ... + mov r12d,eax ; (NEXT STEP) z' = %eax + rol ecx,14 ; dst <<< s + add ecx,edx ; dst += x + not r11d ; not z + lea ebx,[((-1926607734))+r10*1+rbx] ; Const + dst + ... + and r12d,ecx ; x & z + and r11d,edx ; y & (not z) + mov r10d,DWORD[rsi] ; (NEXT STEP) X[0] + or r12d,r11d ; (y & (not z)) | (x & z) + mov r11d,edx ; (NEXT STEP) z' = %edx + add ebx,r12d ; dst += ... + mov r12d,edx ; (NEXT STEP) z' = %edx + rol ebx,20 ; dst <<< s + add ebx,ecx ; dst += x + mov r10d,DWORD[20+rsi] ; (NEXT STEP) X[5] + mov r11d,ecx ; (NEXT STEP) y' = %ecx + lea eax,[((-378558))+r10*1+rax] ; Const + dst + ... + mov r10d,DWORD[32+rsi] ; (NEXT STEP) X[8] + xor r11d,edx ; z ^ ... + xor r11d,ebx ; x ^ ... + add eax,r11d ; dst += ... + rol eax,4 ; dst <<< s + mov r11d,ebx ; (NEXT STEP) y' = %ebx + add eax,ebx ; dst += x + lea edx,[((-2022574463))+r10*1+rdx] ; Const + dst + ... + mov r10d,DWORD[44+rsi] ; (NEXT STEP) X[11] + xor r11d,ecx ; z ^ ... + xor r11d,eax ; x ^ ... + add edx,r11d ; dst += ... + rol edx,11 ; dst <<< s + mov r11d,eax ; (NEXT STEP) y' = %eax + add edx,eax ; dst += x + lea ecx,[1839030562+r10*1+rcx] ; Const + dst + ... + mov r10d,DWORD[56+rsi] ; (NEXT STEP) X[14] + xor r11d,ebx ; z ^ ... + xor r11d,edx ; x ^ ... + add ecx,r11d ; dst += ... + rol ecx,16 ; dst <<< s + mov r11d,edx ; (NEXT STEP) y' = %edx + add ecx,edx ; dst += x + lea ebx,[((-35309556))+r10*1+rbx] ; Const + dst + ... + mov r10d,DWORD[4+rsi] ; (NEXT STEP) X[1] + xor r11d,eax ; z ^ ... + xor r11d,ecx ; x ^ ... + add ebx,r11d ; dst += ... + rol ebx,23 ; dst <<< s + mov r11d,ecx ; (NEXT STEP) y' = %ecx + add ebx,ecx ; dst += x + lea eax,[((-1530992060))+r10*1+rax] ; Const + dst + ... + mov r10d,DWORD[16+rsi] ; (NEXT STEP) X[4] + xor r11d,edx ; z ^ ... + xor r11d,ebx ; x ^ ... + add eax,r11d ; dst += ... + rol eax,4 ; dst <<< s + mov r11d,ebx ; (NEXT STEP) y' = %ebx + add eax,ebx ; dst += x + lea edx,[1272893353+r10*1+rdx] ; Const + dst + ... + mov r10d,DWORD[28+rsi] ; (NEXT STEP) X[7] + xor r11d,ecx ; z ^ ... + xor r11d,eax ; x ^ ... + add edx,r11d ; dst += ... + rol edx,11 ; dst <<< s + mov r11d,eax ; (NEXT STEP) y' = %eax + add edx,eax ; dst += x + lea ecx,[((-155497632))+r10*1+rcx] ; Const + dst + ... + mov r10d,DWORD[40+rsi] ; (NEXT STEP) X[10] + xor r11d,ebx ; z ^ ... + xor r11d,edx ; x ^ ... + add ecx,r11d ; dst += ... + rol ecx,16 ; dst <<< s + mov r11d,edx ; (NEXT STEP) y' = %edx + add ecx,edx ; dst += x + lea ebx,[((-1094730640))+r10*1+rbx] ; Const + dst + ... + mov r10d,DWORD[52+rsi] ; (NEXT STEP) X[13] + xor r11d,eax ; z ^ ... + xor r11d,ecx ; x ^ ... + add ebx,r11d ; dst += ... + rol ebx,23 ; dst <<< s + mov r11d,ecx ; (NEXT STEP) y' = %ecx + add ebx,ecx ; dst += x + lea eax,[681279174+r10*1+rax] ; Const + dst + ... + mov r10d,DWORD[rsi] ; (NEXT STEP) X[0] + xor r11d,edx ; z ^ ... + xor r11d,ebx ; x ^ ... + add eax,r11d ; dst += ... + rol eax,4 ; dst <<< s + mov r11d,ebx ; (NEXT STEP) y' = %ebx + add eax,ebx ; dst += x + lea edx,[((-358537222))+r10*1+rdx] ; Const + dst + ... + mov r10d,DWORD[12+rsi] ; (NEXT STEP) X[3] + xor r11d,ecx ; z ^ ... + xor r11d,eax ; x ^ ... + add edx,r11d ; dst += ... + rol edx,11 ; dst <<< s + mov r11d,eax ; (NEXT STEP) y' = %eax + add edx,eax ; dst += x + lea ecx,[((-722521979))+r10*1+rcx] ; Const + dst + ... + mov r10d,DWORD[24+rsi] ; (NEXT STEP) X[6] + xor r11d,ebx ; z ^ ... + xor r11d,edx ; x ^ ... + add ecx,r11d ; dst += ... + rol ecx,16 ; dst <<< s + mov r11d,edx ; (NEXT STEP) y' = %edx + add ecx,edx ; dst += x + lea ebx,[76029189+r10*1+rbx] ; Const + dst + ... + mov r10d,DWORD[36+rsi] ; (NEXT STEP) X[9] + xor r11d,eax ; z ^ ... + xor r11d,ecx ; x ^ ... + add ebx,r11d ; dst += ... + rol ebx,23 ; dst <<< s + mov r11d,ecx ; (NEXT STEP) y' = %ecx + add ebx,ecx ; dst += x + lea eax,[((-640364487))+r10*1+rax] ; Const + dst + ... + mov r10d,DWORD[48+rsi] ; (NEXT STEP) X[12] + xor r11d,edx ; z ^ ... + xor r11d,ebx ; x ^ ... + add eax,r11d ; dst += ... + rol eax,4 ; dst <<< s + mov r11d,ebx ; (NEXT STEP) y' = %ebx + add eax,ebx ; dst += x + lea edx,[((-421815835))+r10*1+rdx] ; Const + dst + ... + mov r10d,DWORD[60+rsi] ; (NEXT STEP) X[15] + xor r11d,ecx ; z ^ ... + xor r11d,eax ; x ^ ... + add edx,r11d ; dst += ... + rol edx,11 ; dst <<< s + mov r11d,eax ; (NEXT STEP) y' = %eax + add edx,eax ; dst += x + lea ecx,[530742520+r10*1+rcx] ; Const + dst + ... + mov r10d,DWORD[8+rsi] ; (NEXT STEP) X[2] + xor r11d,ebx ; z ^ ... + xor r11d,edx ; x ^ ... + add ecx,r11d ; dst += ... + rol ecx,16 ; dst <<< s + mov r11d,edx ; (NEXT STEP) y' = %edx + add ecx,edx ; dst += x + lea ebx,[((-995338651))+r10*1+rbx] ; Const + dst + ... + mov r10d,DWORD[rsi] ; (NEXT STEP) X[0] + xor r11d,eax ; z ^ ... + xor r11d,ecx ; x ^ ... + add ebx,r11d ; dst += ... + rol ebx,23 ; dst <<< s + mov r11d,ecx ; (NEXT STEP) y' = %ecx + add ebx,ecx ; dst += x + mov r10d,DWORD[rsi] ; (NEXT STEP) X[0] mov r11d,0xffffffff - xor r11d,edx - lea eax,[((-198630844))+r10*1+rax] - or r11d,ebx - xor r11d,ecx - add eax,r11d - mov r10d,DWORD[28+rsi] + xor r11d,edx ; (NEXT STEP) not z' = not %edx + lea eax,[((-198630844))+r10*1+rax] ; Const + dst + ... + or r11d,ebx ; x | ... + xor r11d,ecx ; y ^ ... + add eax,r11d ; dst += ... + mov r10d,DWORD[28+rsi] ; (NEXT STEP) X[7] mov r11d,0xffffffff - rol eax,6 - xor r11d,ecx - add eax,ebx - lea edx,[1126891415+r10*1+rdx] - or r11d,eax - xor r11d,ebx - add edx,r11d - mov r10d,DWORD[56+rsi] + rol eax,6 ; dst <<< s + xor r11d,ecx ; (NEXT STEP) not z' = not %ecx + add eax,ebx ; dst += x + lea edx,[1126891415+r10*1+rdx] ; Const + dst + ... + or r11d,eax ; x | ... + xor r11d,ebx ; y ^ ... + add edx,r11d ; dst += ... + mov r10d,DWORD[56+rsi] ; (NEXT STEP) X[14] mov r11d,0xffffffff - rol edx,10 - xor r11d,ebx - add edx,eax - lea ecx,[((-1416354905))+r10*1+rcx] - or r11d,edx - xor r11d,eax - add ecx,r11d - mov r10d,DWORD[20+rsi] + rol edx,10 ; dst <<< s + xor r11d,ebx ; (NEXT STEP) not z' = not %ebx + add edx,eax ; dst += x + lea ecx,[((-1416354905))+r10*1+rcx] ; Const + dst + ... + or r11d,edx ; x | ... + xor r11d,eax ; y ^ ... + add ecx,r11d ; dst += ... + mov r10d,DWORD[20+rsi] ; (NEXT STEP) X[5] mov r11d,0xffffffff - rol ecx,15 - xor r11d,eax - add ecx,edx - lea ebx,[((-57434055))+r10*1+rbx] - or r11d,ecx - xor r11d,edx - add ebx,r11d - mov r10d,DWORD[48+rsi] + rol ecx,15 ; dst <<< s + xor r11d,eax ; (NEXT STEP) not z' = not %eax + add ecx,edx ; dst += x + lea ebx,[((-57434055))+r10*1+rbx] ; Const + dst + ... + or r11d,ecx ; x | ... + xor r11d,edx ; y ^ ... + add ebx,r11d ; dst += ... + mov r10d,DWORD[48+rsi] ; (NEXT STEP) X[12] mov r11d,0xffffffff - rol ebx,21 - xor r11d,edx - add ebx,ecx - lea eax,[1700485571+r10*1+rax] - or r11d,ebx - xor r11d,ecx - add eax,r11d - mov r10d,DWORD[12+rsi] + rol ebx,21 ; dst <<< s + xor r11d,edx ; (NEXT STEP) not z' = not %edx + add ebx,ecx ; dst += x + lea eax,[1700485571+r10*1+rax] ; Const + dst + ... + or r11d,ebx ; x | ... + xor r11d,ecx ; y ^ ... + add eax,r11d ; dst += ... + mov r10d,DWORD[12+rsi] ; (NEXT STEP) X[3] mov r11d,0xffffffff - rol eax,6 - xor r11d,ecx - add eax,ebx - lea edx,[((-1894986606))+r10*1+rdx] - or r11d,eax - xor r11d,ebx - add edx,r11d - mov r10d,DWORD[40+rsi] + rol eax,6 ; dst <<< s + xor r11d,ecx ; (NEXT STEP) not z' = not %ecx + add eax,ebx ; dst += x + lea edx,[((-1894986606))+r10*1+rdx] ; Const + dst + ... + or r11d,eax ; x | ... + xor r11d,ebx ; y ^ ... + add edx,r11d ; dst += ... + mov r10d,DWORD[40+rsi] ; (NEXT STEP) X[10] mov r11d,0xffffffff - rol edx,10 - xor r11d,ebx - add edx,eax - lea ecx,[((-1051523))+r10*1+rcx] - or r11d,edx - xor r11d,eax - add ecx,r11d - mov r10d,DWORD[4+rsi] + rol edx,10 ; dst <<< s + xor r11d,ebx ; (NEXT STEP) not z' = not %ebx + add edx,eax ; dst += x + lea ecx,[((-1051523))+r10*1+rcx] ; Const + dst + ... + or r11d,edx ; x | ... + xor r11d,eax ; y ^ ... + add ecx,r11d ; dst += ... + mov r10d,DWORD[4+rsi] ; (NEXT STEP) X[1] mov r11d,0xffffffff - rol ecx,15 - xor r11d,eax - add ecx,edx - lea ebx,[((-2054922799))+r10*1+rbx] - or r11d,ecx - xor r11d,edx - add ebx,r11d - mov r10d,DWORD[32+rsi] + rol ecx,15 ; dst <<< s + xor r11d,eax ; (NEXT STEP) not z' = not %eax + add ecx,edx ; dst += x + lea ebx,[((-2054922799))+r10*1+rbx] ; Const + dst + ... + or r11d,ecx ; x | ... + xor r11d,edx ; y ^ ... + add ebx,r11d ; dst += ... + mov r10d,DWORD[32+rsi] ; (NEXT STEP) X[8] mov r11d,0xffffffff - rol ebx,21 - xor r11d,edx - add ebx,ecx - lea eax,[1873313359+r10*1+rax] - or r11d,ebx - xor r11d,ecx - add eax,r11d - mov r10d,DWORD[60+rsi] + rol ebx,21 ; dst <<< s + xor r11d,edx ; (NEXT STEP) not z' = not %edx + add ebx,ecx ; dst += x + lea eax,[1873313359+r10*1+rax] ; Const + dst + ... + or r11d,ebx ; x | ... + xor r11d,ecx ; y ^ ... + add eax,r11d ; dst += ... + mov r10d,DWORD[60+rsi] ; (NEXT STEP) X[15] mov r11d,0xffffffff - rol eax,6 - xor r11d,ecx - add eax,ebx - lea edx,[((-30611744))+r10*1+rdx] - or r11d,eax - xor r11d,ebx - add edx,r11d - mov r10d,DWORD[24+rsi] + rol eax,6 ; dst <<< s + xor r11d,ecx ; (NEXT STEP) not z' = not %ecx + add eax,ebx ; dst += x + lea edx,[((-30611744))+r10*1+rdx] ; Const + dst + ... + or r11d,eax ; x | ... + xor r11d,ebx ; y ^ ... + add edx,r11d ; dst += ... + mov r10d,DWORD[24+rsi] ; (NEXT STEP) X[6] mov r11d,0xffffffff - rol edx,10 - xor r11d,ebx - add edx,eax - lea ecx,[((-1560198380))+r10*1+rcx] - or r11d,edx - xor r11d,eax - add ecx,r11d - mov r10d,DWORD[52+rsi] + rol edx,10 ; dst <<< s + xor r11d,ebx ; (NEXT STEP) not z' = not %ebx + add edx,eax ; dst += x + lea ecx,[((-1560198380))+r10*1+rcx] ; Const + dst + ... + or r11d,edx ; x | ... + xor r11d,eax ; y ^ ... + add ecx,r11d ; dst += ... + mov r10d,DWORD[52+rsi] ; (NEXT STEP) X[13] mov r11d,0xffffffff - rol ecx,15 - xor r11d,eax - add ecx,edx - lea ebx,[1309151649+r10*1+rbx] - or r11d,ecx - xor r11d,edx - add ebx,r11d - mov r10d,DWORD[16+rsi] + rol ecx,15 ; dst <<< s + xor r11d,eax ; (NEXT STEP) not z' = not %eax + add ecx,edx ; dst += x + lea ebx,[1309151649+r10*1+rbx] ; Const + dst + ... + or r11d,ecx ; x | ... + xor r11d,edx ; y ^ ... + add ebx,r11d ; dst += ... + mov r10d,DWORD[16+rsi] ; (NEXT STEP) X[4] mov r11d,0xffffffff - rol ebx,21 - xor r11d,edx - add ebx,ecx - lea eax,[((-145523070))+r10*1+rax] - or r11d,ebx - xor r11d,ecx - add eax,r11d - mov r10d,DWORD[44+rsi] + rol ebx,21 ; dst <<< s + xor r11d,edx ; (NEXT STEP) not z' = not %edx + add ebx,ecx ; dst += x + lea eax,[((-145523070))+r10*1+rax] ; Const + dst + ... + or r11d,ebx ; x | ... + xor r11d,ecx ; y ^ ... + add eax,r11d ; dst += ... + mov r10d,DWORD[44+rsi] ; (NEXT STEP) X[11] mov r11d,0xffffffff - rol eax,6 - xor r11d,ecx - add eax,ebx - lea edx,[((-1120210379))+r10*1+rdx] - or r11d,eax - xor r11d,ebx - add edx,r11d - mov r10d,DWORD[8+rsi] + rol eax,6 ; dst <<< s + xor r11d,ecx ; (NEXT STEP) not z' = not %ecx + add eax,ebx ; dst += x + lea edx,[((-1120210379))+r10*1+rdx] ; Const + dst + ... + or r11d,eax ; x | ... + xor r11d,ebx ; y ^ ... + add edx,r11d ; dst += ... + mov r10d,DWORD[8+rsi] ; (NEXT STEP) X[2] mov r11d,0xffffffff - rol edx,10 - xor r11d,ebx - add edx,eax - lea ecx,[718787259+r10*1+rcx] - or r11d,edx - xor r11d,eax - add ecx,r11d - mov r10d,DWORD[36+rsi] + rol edx,10 ; dst <<< s + xor r11d,ebx ; (NEXT STEP) not z' = not %ebx + add edx,eax ; dst += x + lea ecx,[718787259+r10*1+rcx] ; Const + dst + ... + or r11d,edx ; x | ... + xor r11d,eax ; y ^ ... + add ecx,r11d ; dst += ... + mov r10d,DWORD[36+rsi] ; (NEXT STEP) X[9] mov r11d,0xffffffff - rol ecx,15 - xor r11d,eax - add ecx,edx - lea ebx,[((-343485551))+r10*1+rbx] - or r11d,ecx - xor r11d,edx - add ebx,r11d - mov r10d,DWORD[rsi] + rol ecx,15 ; dst <<< s + xor r11d,eax ; (NEXT STEP) not z' = not %eax + add ecx,edx ; dst += x + lea ebx,[((-343485551))+r10*1+rbx] ; Const + dst + ... + or r11d,ecx ; x | ... + xor r11d,edx ; y ^ ... + add ebx,r11d ; dst += ... + mov r10d,DWORD[rsi] ; (NEXT STEP) X[0] mov r11d,0xffffffff - rol ebx,21 - xor r11d,edx - add ebx,ecx - + rol ebx,21 ; dst <<< s + xor r11d,edx ; (NEXT STEP) not z' = not %edx + add ebx,ecx ; dst += x +; add old values of A, B, C, D add eax,r8d add ebx,r9d add ecx,r14d add edx,r15d - - add rsi,64 - cmp rsi,rdi - jb NEAR $L$loop - +; loop control + add rsi,64 ; ptr += 64 + cmp rsi,rdi ; cmp end with ptr + jb NEAR $L$loop ; jmp if ptr < end +; END of loop over 16-word blocks $L$end: - mov DWORD[rbp],eax - mov DWORD[4+rbp],ebx - mov DWORD[8+rbp],ecx - mov DWORD[12+rbp],edx + mov DWORD[rbp],eax ; ctx->A = A + mov DWORD[4+rbp],ebx ; ctx->B = B + mov DWORD[8+rbp],ecx ; ctx->C = C + mov DWORD[12+rbp],edx ; ctx->D = D mov r15,QWORD[rsp] @@ -720,17 +720,17 @@ se_handler: pushfq sub rsp,64 - mov rax,QWORD[120+r8] - mov rbx,QWORD[248+r8] + mov rax,QWORD[120+r8] ; pull context->Rax + mov rbx,QWORD[248+r8] ; pull context->Rip lea r10,[$L$prologue] - cmp rbx,r10 + cmp rbx,r10 ; context->Rip<.Lprologue jb NEAR $L$in_prologue - mov rax,QWORD[152+r8] + mov rax,QWORD[152+r8] ; pull context->Rsp lea r10,[$L$epilogue] - cmp rbx,r10 + cmp rbx,r10 ; context->Rip>=.Lepilogue jae NEAR $L$in_prologue lea rax,[40+rax] @@ -740,39 +740,39 @@ se_handler: mov r12,QWORD[((-24))+rax] mov r14,QWORD[((-32))+rax] mov r15,QWORD[((-40))+rax] - mov QWORD[144+r8],rbx - mov QWORD[160+r8],rbp - mov QWORD[216+r8],r12 - mov QWORD[232+r8],r14 - mov QWORD[240+r8],r15 + mov QWORD[144+r8],rbx ; restore context->Rbx + mov QWORD[160+r8],rbp ; restore context->Rbp + mov QWORD[216+r8],r12 ; restore context->R12 + mov QWORD[232+r8],r14 ; restore context->R14 + mov QWORD[240+r8],r15 ; restore context->R15 $L$in_prologue: mov rdi,QWORD[8+rax] mov rsi,QWORD[16+rax] - mov QWORD[152+r8],rax - mov QWORD[168+r8],rsi - mov QWORD[176+r8],rdi + mov QWORD[152+r8],rax ; restore context->Rsp + mov QWORD[168+r8],rsi ; restore context->Rsi + mov QWORD[176+r8],rdi ; restore context->Rdi - mov rdi,QWORD[40+r9] - mov rsi,r8 - mov ecx,154 - DD 0xa548f3fc + mov rdi,QWORD[40+r9] ; disp->ContextRecord + mov rsi,r8 ; context + mov ecx,154 ; sizeof(CONTEXT) + DD 0xa548f3fc ; cld; rep movsq mov rsi,r9 - xor rcx,rcx - mov rdx,QWORD[8+rsi] - mov r8,QWORD[rsi] - mov r9,QWORD[16+rsi] - mov r10,QWORD[40+rsi] - lea r11,[56+rsi] - lea r12,[24+rsi] - mov QWORD[32+rsp],r10 - mov QWORD[40+rsp],r11 - mov QWORD[48+rsp],r12 - mov QWORD[56+rsp],rcx + xor rcx,rcx ; arg1, UNW_FLAG_NHANDLER + mov rdx,QWORD[8+rsi] ; arg2, disp->ImageBase + mov r8,QWORD[rsi] ; arg3, disp->ControlPc + mov r9,QWORD[16+rsi] ; arg4, disp->FunctionEntry + mov r10,QWORD[40+rsi] ; disp->ContextRecord + lea r11,[56+rsi] ; &disp->HandlerData + lea r12,[24+rsi] ; &disp->EstablisherFrame + mov QWORD[32+rsp],r10 ; arg5 + mov QWORD[40+rsp],r11 ; arg6 + mov QWORD[48+rsp],r12 ; arg7 + mov QWORD[56+rsp],rcx ; arg8, (NULL) call QWORD[__imp_RtlVirtualUnwind] - mov eax,1 + mov eax,1 ; ExceptionContinueSearch add rsp,64 popfq pop r15 diff --git a/third_party/boringssl/gen/test_support/trampoline-armv8-apple.S b/third_party/boringssl/gen/test_support/trampoline-armv8-apple.S index b32c707ef..e00564813 100644 --- a/third_party/boringssl/gen/test_support/trampoline-armv8-apple.S +++ b/third_party/boringssl/gen/test_support/trampoline-armv8-apple.S @@ -128,6 +128,7 @@ Lx29_ok: .globl _abi_test_clobber_x0 .private_extern _abi_test_clobber_x0 .align 4 +.alt_entry _abi_test_clobber_x0 _abi_test_clobber_x0: AARCH64_VALID_CALL_TARGET mov x0, xzr @@ -137,6 +138,7 @@ _abi_test_clobber_x0: .globl _abi_test_clobber_x1 .private_extern _abi_test_clobber_x1 .align 4 +.alt_entry _abi_test_clobber_x1 _abi_test_clobber_x1: AARCH64_VALID_CALL_TARGET mov x1, xzr @@ -146,6 +148,7 @@ _abi_test_clobber_x1: .globl _abi_test_clobber_x2 .private_extern _abi_test_clobber_x2 .align 4 +.alt_entry _abi_test_clobber_x2 _abi_test_clobber_x2: AARCH64_VALID_CALL_TARGET mov x2, xzr @@ -155,6 +158,7 @@ _abi_test_clobber_x2: .globl _abi_test_clobber_x3 .private_extern _abi_test_clobber_x3 .align 4 +.alt_entry _abi_test_clobber_x3 _abi_test_clobber_x3: AARCH64_VALID_CALL_TARGET mov x3, xzr @@ -164,6 +168,7 @@ _abi_test_clobber_x3: .globl _abi_test_clobber_x4 .private_extern _abi_test_clobber_x4 .align 4 +.alt_entry _abi_test_clobber_x4 _abi_test_clobber_x4: AARCH64_VALID_CALL_TARGET mov x4, xzr @@ -173,6 +178,7 @@ _abi_test_clobber_x4: .globl _abi_test_clobber_x5 .private_extern _abi_test_clobber_x5 .align 4 +.alt_entry _abi_test_clobber_x5 _abi_test_clobber_x5: AARCH64_VALID_CALL_TARGET mov x5, xzr @@ -182,6 +188,7 @@ _abi_test_clobber_x5: .globl _abi_test_clobber_x6 .private_extern _abi_test_clobber_x6 .align 4 +.alt_entry _abi_test_clobber_x6 _abi_test_clobber_x6: AARCH64_VALID_CALL_TARGET mov x6, xzr @@ -191,6 +198,7 @@ _abi_test_clobber_x6: .globl _abi_test_clobber_x7 .private_extern _abi_test_clobber_x7 .align 4 +.alt_entry _abi_test_clobber_x7 _abi_test_clobber_x7: AARCH64_VALID_CALL_TARGET mov x7, xzr @@ -200,6 +208,7 @@ _abi_test_clobber_x7: .globl _abi_test_clobber_x8 .private_extern _abi_test_clobber_x8 .align 4 +.alt_entry _abi_test_clobber_x8 _abi_test_clobber_x8: AARCH64_VALID_CALL_TARGET mov x8, xzr @@ -209,6 +218,7 @@ _abi_test_clobber_x8: .globl _abi_test_clobber_x9 .private_extern _abi_test_clobber_x9 .align 4 +.alt_entry _abi_test_clobber_x9 _abi_test_clobber_x9: AARCH64_VALID_CALL_TARGET mov x9, xzr @@ -218,6 +228,7 @@ _abi_test_clobber_x9: .globl _abi_test_clobber_x10 .private_extern _abi_test_clobber_x10 .align 4 +.alt_entry _abi_test_clobber_x10 _abi_test_clobber_x10: AARCH64_VALID_CALL_TARGET mov x10, xzr @@ -227,6 +238,7 @@ _abi_test_clobber_x10: .globl _abi_test_clobber_x11 .private_extern _abi_test_clobber_x11 .align 4 +.alt_entry _abi_test_clobber_x11 _abi_test_clobber_x11: AARCH64_VALID_CALL_TARGET mov x11, xzr @@ -236,6 +248,7 @@ _abi_test_clobber_x11: .globl _abi_test_clobber_x12 .private_extern _abi_test_clobber_x12 .align 4 +.alt_entry _abi_test_clobber_x12 _abi_test_clobber_x12: AARCH64_VALID_CALL_TARGET mov x12, xzr @@ -245,6 +258,7 @@ _abi_test_clobber_x12: .globl _abi_test_clobber_x13 .private_extern _abi_test_clobber_x13 .align 4 +.alt_entry _abi_test_clobber_x13 _abi_test_clobber_x13: AARCH64_VALID_CALL_TARGET mov x13, xzr @@ -254,6 +268,7 @@ _abi_test_clobber_x13: .globl _abi_test_clobber_x14 .private_extern _abi_test_clobber_x14 .align 4 +.alt_entry _abi_test_clobber_x14 _abi_test_clobber_x14: AARCH64_VALID_CALL_TARGET mov x14, xzr @@ -263,6 +278,7 @@ _abi_test_clobber_x14: .globl _abi_test_clobber_x15 .private_extern _abi_test_clobber_x15 .align 4 +.alt_entry _abi_test_clobber_x15 _abi_test_clobber_x15: AARCH64_VALID_CALL_TARGET mov x15, xzr @@ -272,6 +288,7 @@ _abi_test_clobber_x15: .globl _abi_test_clobber_x16 .private_extern _abi_test_clobber_x16 .align 4 +.alt_entry _abi_test_clobber_x16 _abi_test_clobber_x16: AARCH64_VALID_CALL_TARGET mov x16, xzr @@ -281,6 +298,7 @@ _abi_test_clobber_x16: .globl _abi_test_clobber_x17 .private_extern _abi_test_clobber_x17 .align 4 +.alt_entry _abi_test_clobber_x17 _abi_test_clobber_x17: AARCH64_VALID_CALL_TARGET mov x17, xzr @@ -290,6 +308,7 @@ _abi_test_clobber_x17: .globl _abi_test_clobber_x19 .private_extern _abi_test_clobber_x19 .align 4 +.alt_entry _abi_test_clobber_x19 _abi_test_clobber_x19: AARCH64_VALID_CALL_TARGET mov x19, xzr @@ -299,6 +318,7 @@ _abi_test_clobber_x19: .globl _abi_test_clobber_x20 .private_extern _abi_test_clobber_x20 .align 4 +.alt_entry _abi_test_clobber_x20 _abi_test_clobber_x20: AARCH64_VALID_CALL_TARGET mov x20, xzr @@ -308,6 +328,7 @@ _abi_test_clobber_x20: .globl _abi_test_clobber_x21 .private_extern _abi_test_clobber_x21 .align 4 +.alt_entry _abi_test_clobber_x21 _abi_test_clobber_x21: AARCH64_VALID_CALL_TARGET mov x21, xzr @@ -317,6 +338,7 @@ _abi_test_clobber_x21: .globl _abi_test_clobber_x22 .private_extern _abi_test_clobber_x22 .align 4 +.alt_entry _abi_test_clobber_x22 _abi_test_clobber_x22: AARCH64_VALID_CALL_TARGET mov x22, xzr @@ -326,6 +348,7 @@ _abi_test_clobber_x22: .globl _abi_test_clobber_x23 .private_extern _abi_test_clobber_x23 .align 4 +.alt_entry _abi_test_clobber_x23 _abi_test_clobber_x23: AARCH64_VALID_CALL_TARGET mov x23, xzr @@ -335,6 +358,7 @@ _abi_test_clobber_x23: .globl _abi_test_clobber_x24 .private_extern _abi_test_clobber_x24 .align 4 +.alt_entry _abi_test_clobber_x24 _abi_test_clobber_x24: AARCH64_VALID_CALL_TARGET mov x24, xzr @@ -344,6 +368,7 @@ _abi_test_clobber_x24: .globl _abi_test_clobber_x25 .private_extern _abi_test_clobber_x25 .align 4 +.alt_entry _abi_test_clobber_x25 _abi_test_clobber_x25: AARCH64_VALID_CALL_TARGET mov x25, xzr @@ -353,6 +378,7 @@ _abi_test_clobber_x25: .globl _abi_test_clobber_x26 .private_extern _abi_test_clobber_x26 .align 4 +.alt_entry _abi_test_clobber_x26 _abi_test_clobber_x26: AARCH64_VALID_CALL_TARGET mov x26, xzr @@ -362,6 +388,7 @@ _abi_test_clobber_x26: .globl _abi_test_clobber_x27 .private_extern _abi_test_clobber_x27 .align 4 +.alt_entry _abi_test_clobber_x27 _abi_test_clobber_x27: AARCH64_VALID_CALL_TARGET mov x27, xzr @@ -371,6 +398,7 @@ _abi_test_clobber_x27: .globl _abi_test_clobber_x28 .private_extern _abi_test_clobber_x28 .align 4 +.alt_entry _abi_test_clobber_x28 _abi_test_clobber_x28: AARCH64_VALID_CALL_TARGET mov x28, xzr @@ -380,6 +408,7 @@ _abi_test_clobber_x28: .globl _abi_test_clobber_x29 .private_extern _abi_test_clobber_x29 .align 4 +.alt_entry _abi_test_clobber_x29 _abi_test_clobber_x29: AARCH64_VALID_CALL_TARGET mov x29, xzr @@ -389,6 +418,7 @@ _abi_test_clobber_x29: .globl _abi_test_clobber_d0 .private_extern _abi_test_clobber_d0 .align 4 +.alt_entry _abi_test_clobber_d0 _abi_test_clobber_d0: AARCH64_VALID_CALL_TARGET fmov d0, xzr @@ -398,6 +428,7 @@ _abi_test_clobber_d0: .globl _abi_test_clobber_d1 .private_extern _abi_test_clobber_d1 .align 4 +.alt_entry _abi_test_clobber_d1 _abi_test_clobber_d1: AARCH64_VALID_CALL_TARGET fmov d1, xzr @@ -407,6 +438,7 @@ _abi_test_clobber_d1: .globl _abi_test_clobber_d2 .private_extern _abi_test_clobber_d2 .align 4 +.alt_entry _abi_test_clobber_d2 _abi_test_clobber_d2: AARCH64_VALID_CALL_TARGET fmov d2, xzr @@ -416,6 +448,7 @@ _abi_test_clobber_d2: .globl _abi_test_clobber_d3 .private_extern _abi_test_clobber_d3 .align 4 +.alt_entry _abi_test_clobber_d3 _abi_test_clobber_d3: AARCH64_VALID_CALL_TARGET fmov d3, xzr @@ -425,6 +458,7 @@ _abi_test_clobber_d3: .globl _abi_test_clobber_d4 .private_extern _abi_test_clobber_d4 .align 4 +.alt_entry _abi_test_clobber_d4 _abi_test_clobber_d4: AARCH64_VALID_CALL_TARGET fmov d4, xzr @@ -434,6 +468,7 @@ _abi_test_clobber_d4: .globl _abi_test_clobber_d5 .private_extern _abi_test_clobber_d5 .align 4 +.alt_entry _abi_test_clobber_d5 _abi_test_clobber_d5: AARCH64_VALID_CALL_TARGET fmov d5, xzr @@ -443,6 +478,7 @@ _abi_test_clobber_d5: .globl _abi_test_clobber_d6 .private_extern _abi_test_clobber_d6 .align 4 +.alt_entry _abi_test_clobber_d6 _abi_test_clobber_d6: AARCH64_VALID_CALL_TARGET fmov d6, xzr @@ -452,6 +488,7 @@ _abi_test_clobber_d6: .globl _abi_test_clobber_d7 .private_extern _abi_test_clobber_d7 .align 4 +.alt_entry _abi_test_clobber_d7 _abi_test_clobber_d7: AARCH64_VALID_CALL_TARGET fmov d7, xzr @@ -461,6 +498,7 @@ _abi_test_clobber_d7: .globl _abi_test_clobber_d8 .private_extern _abi_test_clobber_d8 .align 4 +.alt_entry _abi_test_clobber_d8 _abi_test_clobber_d8: AARCH64_VALID_CALL_TARGET fmov d8, xzr @@ -470,6 +508,7 @@ _abi_test_clobber_d8: .globl _abi_test_clobber_d9 .private_extern _abi_test_clobber_d9 .align 4 +.alt_entry _abi_test_clobber_d9 _abi_test_clobber_d9: AARCH64_VALID_CALL_TARGET fmov d9, xzr @@ -479,6 +518,7 @@ _abi_test_clobber_d9: .globl _abi_test_clobber_d10 .private_extern _abi_test_clobber_d10 .align 4 +.alt_entry _abi_test_clobber_d10 _abi_test_clobber_d10: AARCH64_VALID_CALL_TARGET fmov d10, xzr @@ -488,6 +528,7 @@ _abi_test_clobber_d10: .globl _abi_test_clobber_d11 .private_extern _abi_test_clobber_d11 .align 4 +.alt_entry _abi_test_clobber_d11 _abi_test_clobber_d11: AARCH64_VALID_CALL_TARGET fmov d11, xzr @@ -497,6 +538,7 @@ _abi_test_clobber_d11: .globl _abi_test_clobber_d12 .private_extern _abi_test_clobber_d12 .align 4 +.alt_entry _abi_test_clobber_d12 _abi_test_clobber_d12: AARCH64_VALID_CALL_TARGET fmov d12, xzr @@ -506,6 +548,7 @@ _abi_test_clobber_d12: .globl _abi_test_clobber_d13 .private_extern _abi_test_clobber_d13 .align 4 +.alt_entry _abi_test_clobber_d13 _abi_test_clobber_d13: AARCH64_VALID_CALL_TARGET fmov d13, xzr @@ -515,6 +558,7 @@ _abi_test_clobber_d13: .globl _abi_test_clobber_d14 .private_extern _abi_test_clobber_d14 .align 4 +.alt_entry _abi_test_clobber_d14 _abi_test_clobber_d14: AARCH64_VALID_CALL_TARGET fmov d14, xzr @@ -524,6 +568,7 @@ _abi_test_clobber_d14: .globl _abi_test_clobber_d15 .private_extern _abi_test_clobber_d15 .align 4 +.alt_entry _abi_test_clobber_d15 _abi_test_clobber_d15: AARCH64_VALID_CALL_TARGET fmov d15, xzr @@ -533,6 +578,7 @@ _abi_test_clobber_d15: .globl _abi_test_clobber_d16 .private_extern _abi_test_clobber_d16 .align 4 +.alt_entry _abi_test_clobber_d16 _abi_test_clobber_d16: AARCH64_VALID_CALL_TARGET fmov d16, xzr @@ -542,6 +588,7 @@ _abi_test_clobber_d16: .globl _abi_test_clobber_d17 .private_extern _abi_test_clobber_d17 .align 4 +.alt_entry _abi_test_clobber_d17 _abi_test_clobber_d17: AARCH64_VALID_CALL_TARGET fmov d17, xzr @@ -551,6 +598,7 @@ _abi_test_clobber_d17: .globl _abi_test_clobber_d18 .private_extern _abi_test_clobber_d18 .align 4 +.alt_entry _abi_test_clobber_d18 _abi_test_clobber_d18: AARCH64_VALID_CALL_TARGET fmov d18, xzr @@ -560,6 +608,7 @@ _abi_test_clobber_d18: .globl _abi_test_clobber_d19 .private_extern _abi_test_clobber_d19 .align 4 +.alt_entry _abi_test_clobber_d19 _abi_test_clobber_d19: AARCH64_VALID_CALL_TARGET fmov d19, xzr @@ -569,6 +618,7 @@ _abi_test_clobber_d19: .globl _abi_test_clobber_d20 .private_extern _abi_test_clobber_d20 .align 4 +.alt_entry _abi_test_clobber_d20 _abi_test_clobber_d20: AARCH64_VALID_CALL_TARGET fmov d20, xzr @@ -578,6 +628,7 @@ _abi_test_clobber_d20: .globl _abi_test_clobber_d21 .private_extern _abi_test_clobber_d21 .align 4 +.alt_entry _abi_test_clobber_d21 _abi_test_clobber_d21: AARCH64_VALID_CALL_TARGET fmov d21, xzr @@ -587,6 +638,7 @@ _abi_test_clobber_d21: .globl _abi_test_clobber_d22 .private_extern _abi_test_clobber_d22 .align 4 +.alt_entry _abi_test_clobber_d22 _abi_test_clobber_d22: AARCH64_VALID_CALL_TARGET fmov d22, xzr @@ -596,6 +648,7 @@ _abi_test_clobber_d22: .globl _abi_test_clobber_d23 .private_extern _abi_test_clobber_d23 .align 4 +.alt_entry _abi_test_clobber_d23 _abi_test_clobber_d23: AARCH64_VALID_CALL_TARGET fmov d23, xzr @@ -605,6 +658,7 @@ _abi_test_clobber_d23: .globl _abi_test_clobber_d24 .private_extern _abi_test_clobber_d24 .align 4 +.alt_entry _abi_test_clobber_d24 _abi_test_clobber_d24: AARCH64_VALID_CALL_TARGET fmov d24, xzr @@ -614,6 +668,7 @@ _abi_test_clobber_d24: .globl _abi_test_clobber_d25 .private_extern _abi_test_clobber_d25 .align 4 +.alt_entry _abi_test_clobber_d25 _abi_test_clobber_d25: AARCH64_VALID_CALL_TARGET fmov d25, xzr @@ -623,6 +678,7 @@ _abi_test_clobber_d25: .globl _abi_test_clobber_d26 .private_extern _abi_test_clobber_d26 .align 4 +.alt_entry _abi_test_clobber_d26 _abi_test_clobber_d26: AARCH64_VALID_CALL_TARGET fmov d26, xzr @@ -632,6 +688,7 @@ _abi_test_clobber_d26: .globl _abi_test_clobber_d27 .private_extern _abi_test_clobber_d27 .align 4 +.alt_entry _abi_test_clobber_d27 _abi_test_clobber_d27: AARCH64_VALID_CALL_TARGET fmov d27, xzr @@ -641,6 +698,7 @@ _abi_test_clobber_d27: .globl _abi_test_clobber_d28 .private_extern _abi_test_clobber_d28 .align 4 +.alt_entry _abi_test_clobber_d28 _abi_test_clobber_d28: AARCH64_VALID_CALL_TARGET fmov d28, xzr @@ -650,6 +708,7 @@ _abi_test_clobber_d28: .globl _abi_test_clobber_d29 .private_extern _abi_test_clobber_d29 .align 4 +.alt_entry _abi_test_clobber_d29 _abi_test_clobber_d29: AARCH64_VALID_CALL_TARGET fmov d29, xzr @@ -659,6 +718,7 @@ _abi_test_clobber_d29: .globl _abi_test_clobber_d30 .private_extern _abi_test_clobber_d30 .align 4 +.alt_entry _abi_test_clobber_d30 _abi_test_clobber_d30: AARCH64_VALID_CALL_TARGET fmov d30, xzr @@ -668,6 +728,7 @@ _abi_test_clobber_d30: .globl _abi_test_clobber_d31 .private_extern _abi_test_clobber_d31 .align 4 +.alt_entry _abi_test_clobber_d31 _abi_test_clobber_d31: AARCH64_VALID_CALL_TARGET fmov d31, xzr @@ -677,6 +738,7 @@ _abi_test_clobber_d31: .globl _abi_test_clobber_v8_upper .private_extern _abi_test_clobber_v8_upper .align 4 +.alt_entry _abi_test_clobber_v8_upper _abi_test_clobber_v8_upper: AARCH64_VALID_CALL_TARGET fmov v8.d[1], xzr @@ -686,6 +748,7 @@ _abi_test_clobber_v8_upper: .globl _abi_test_clobber_v9_upper .private_extern _abi_test_clobber_v9_upper .align 4 +.alt_entry _abi_test_clobber_v9_upper _abi_test_clobber_v9_upper: AARCH64_VALID_CALL_TARGET fmov v9.d[1], xzr @@ -695,6 +758,7 @@ _abi_test_clobber_v9_upper: .globl _abi_test_clobber_v10_upper .private_extern _abi_test_clobber_v10_upper .align 4 +.alt_entry _abi_test_clobber_v10_upper _abi_test_clobber_v10_upper: AARCH64_VALID_CALL_TARGET fmov v10.d[1], xzr @@ -704,6 +768,7 @@ _abi_test_clobber_v10_upper: .globl _abi_test_clobber_v11_upper .private_extern _abi_test_clobber_v11_upper .align 4 +.alt_entry _abi_test_clobber_v11_upper _abi_test_clobber_v11_upper: AARCH64_VALID_CALL_TARGET fmov v11.d[1], xzr @@ -713,6 +778,7 @@ _abi_test_clobber_v11_upper: .globl _abi_test_clobber_v12_upper .private_extern _abi_test_clobber_v12_upper .align 4 +.alt_entry _abi_test_clobber_v12_upper _abi_test_clobber_v12_upper: AARCH64_VALID_CALL_TARGET fmov v12.d[1], xzr @@ -722,6 +788,7 @@ _abi_test_clobber_v12_upper: .globl _abi_test_clobber_v13_upper .private_extern _abi_test_clobber_v13_upper .align 4 +.alt_entry _abi_test_clobber_v13_upper _abi_test_clobber_v13_upper: AARCH64_VALID_CALL_TARGET fmov v13.d[1], xzr @@ -731,6 +798,7 @@ _abi_test_clobber_v13_upper: .globl _abi_test_clobber_v14_upper .private_extern _abi_test_clobber_v14_upper .align 4 +.alt_entry _abi_test_clobber_v14_upper _abi_test_clobber_v14_upper: AARCH64_VALID_CALL_TARGET fmov v14.d[1], xzr @@ -740,6 +808,7 @@ _abi_test_clobber_v14_upper: .globl _abi_test_clobber_v15_upper .private_extern _abi_test_clobber_v15_upper .align 4 +.alt_entry _abi_test_clobber_v15_upper _abi_test_clobber_v15_upper: AARCH64_VALID_CALL_TARGET fmov v15.d[1], xzr diff --git a/third_party/boringssl/gen/test_support/trampoline-x86_64-apple.S b/third_party/boringssl/gen/test_support/trampoline-x86_64-apple.S index 5d60e67ad..d98e7219b 100644 --- a/third_party/boringssl/gen/test_support/trampoline-x86_64-apple.S +++ b/third_party/boringssl/gen/test_support/trampoline-x86_64-apple.S @@ -6,13 +6,13 @@ #if !defined(OPENSSL_NO_ASM) && defined(OPENSSL_X86_64) && defined(__APPLE__) .text - - - - - - - +// abi_test_trampoline loads callee-saved registers from |state|, calls |func| +// with |argv|, then saves the callee-saved registers into |state|. It returns +// the result of |func|. If |unwind| is non-zero, this function triggers unwind +// instrumentation. +// uint64_t abi_test_trampoline(void (*func)(...), CallerState *state, +// const uint64_t *argv, size_t argc, +// int unwind); .globl _abi_test_trampoline .private_extern _abi_test_trampoline @@ -21,15 +21,15 @@ _abi_test_trampoline: _CET_ENDBR - - - - - - - - - +// Stack layout: +// 8 bytes - align +// 48 bytes - saved caller registers +// 8 bytes - scratch space +// 8 bytes - saved copy of $unwind (SysV-only) +// 8 bytes - saved copy of $state +// 8 bytes - saved copy of $func +// 8 bytes - if needed for stack alignment +// 8*4 bytes - parameters for $func subq $120,%rsp @@ -59,13 +59,13 @@ _CET_ENDBR movq 24(%rsi),%r13 movq 32(%rsi),%r14 movq 40(%rsi),%r15 - +// Stash $func and $state, so they are available after the call returns. movq %rdi,32(%rsp) movq %rsi,40(%rsp) - - - +// Load parameters. Note this will clobber $argv and $argc, so we can +// only use non-parameter volatile registers. There are three, and they +// are the same between SysV and Win64: %rax, %r10, and %r11. movq %rdx,%r10 movq %rcx,%r11 decq %r11 @@ -97,11 +97,11 @@ L$args_loop: decq %r11 js L$args_done - - - - - +// This block should be: +// movq (%r10), %rtmp +// movq %rtmp, (%rax) +// There are no spare registers available, so we spill into the scratch +// space. movq %r11,56(%rsp) movq (%r10),%r11 movq %r11,(%rax) @@ -117,31 +117,34 @@ L$args_done: testq %r10,%r10 jz L$no_unwind - +// Set the trap flag. pushfq orq $0x100,0(%rsp) popfq - - +// Run an instruction to trigger a breakpoint immediately before the +// call. nop .globl _abi_test_unwind_start .private_extern _abi_test_unwind_start +.alt_entry _abi_test_unwind_start _abi_test_unwind_start: call *%rax .globl _abi_test_unwind_return .private_extern _abi_test_unwind_return +.alt_entry _abi_test_unwind_return _abi_test_unwind_return: - - - +// Clear the trap flag. Note this assumes the trap flag was clear on +// entry. We do not support instrumenting an unwind-instrumented +// |abi_test_trampoline|. pushfq - andq $-0x101,0(%rsp) + andq $-0x101,0(%rsp) // -0x101 is ~0x100 popfq .globl _abi_test_unwind_stop .private_extern _abi_test_unwind_stop +.alt_entry _abi_test_unwind_stop _abi_test_unwind_stop: jmp L$call_done @@ -150,7 +153,7 @@ L$no_unwind: call *%rax L$call_done: - +// Store what $func did our state, so our caller can check. movq 40(%rsp),%rsi movq %rbx,0(%rsi) movq %rbp,8(%rsi) @@ -173,7 +176,7 @@ L$call_done: addq $120,%rsp - +// %rax already contains $func's return value, unmodified. ret @@ -182,6 +185,7 @@ L$call_done: .private_extern _abi_test_clobber_rax .p2align 4 +.alt_entry _abi_test_clobber_rax _abi_test_clobber_rax: _CET_ENDBR xorq %rax,%rax @@ -191,6 +195,7 @@ _CET_ENDBR .private_extern _abi_test_clobber_rbx .p2align 4 +.alt_entry _abi_test_clobber_rbx _abi_test_clobber_rbx: _CET_ENDBR xorq %rbx,%rbx @@ -200,6 +205,7 @@ _CET_ENDBR .private_extern _abi_test_clobber_rcx .p2align 4 +.alt_entry _abi_test_clobber_rcx _abi_test_clobber_rcx: _CET_ENDBR xorq %rcx,%rcx @@ -209,6 +215,7 @@ _CET_ENDBR .private_extern _abi_test_clobber_rdx .p2align 4 +.alt_entry _abi_test_clobber_rdx _abi_test_clobber_rdx: _CET_ENDBR xorq %rdx,%rdx @@ -218,6 +225,7 @@ _CET_ENDBR .private_extern _abi_test_clobber_rdi .p2align 4 +.alt_entry _abi_test_clobber_rdi _abi_test_clobber_rdi: _CET_ENDBR xorq %rdi,%rdi @@ -227,6 +235,7 @@ _CET_ENDBR .private_extern _abi_test_clobber_rsi .p2align 4 +.alt_entry _abi_test_clobber_rsi _abi_test_clobber_rsi: _CET_ENDBR xorq %rsi,%rsi @@ -236,6 +245,7 @@ _CET_ENDBR .private_extern _abi_test_clobber_rbp .p2align 4 +.alt_entry _abi_test_clobber_rbp _abi_test_clobber_rbp: _CET_ENDBR xorq %rbp,%rbp @@ -245,6 +255,7 @@ _CET_ENDBR .private_extern _abi_test_clobber_r8 .p2align 4 +.alt_entry _abi_test_clobber_r8 _abi_test_clobber_r8: _CET_ENDBR xorq %r8,%r8 @@ -254,6 +265,7 @@ _CET_ENDBR .private_extern _abi_test_clobber_r9 .p2align 4 +.alt_entry _abi_test_clobber_r9 _abi_test_clobber_r9: _CET_ENDBR xorq %r9,%r9 @@ -263,6 +275,7 @@ _CET_ENDBR .private_extern _abi_test_clobber_r10 .p2align 4 +.alt_entry _abi_test_clobber_r10 _abi_test_clobber_r10: _CET_ENDBR xorq %r10,%r10 @@ -272,6 +285,7 @@ _CET_ENDBR .private_extern _abi_test_clobber_r11 .p2align 4 +.alt_entry _abi_test_clobber_r11 _abi_test_clobber_r11: _CET_ENDBR xorq %r11,%r11 @@ -281,6 +295,7 @@ _CET_ENDBR .private_extern _abi_test_clobber_r12 .p2align 4 +.alt_entry _abi_test_clobber_r12 _abi_test_clobber_r12: _CET_ENDBR xorq %r12,%r12 @@ -290,6 +305,7 @@ _CET_ENDBR .private_extern _abi_test_clobber_r13 .p2align 4 +.alt_entry _abi_test_clobber_r13 _abi_test_clobber_r13: _CET_ENDBR xorq %r13,%r13 @@ -299,6 +315,7 @@ _CET_ENDBR .private_extern _abi_test_clobber_r14 .p2align 4 +.alt_entry _abi_test_clobber_r14 _abi_test_clobber_r14: _CET_ENDBR xorq %r14,%r14 @@ -308,6 +325,7 @@ _CET_ENDBR .private_extern _abi_test_clobber_r15 .p2align 4 +.alt_entry _abi_test_clobber_r15 _abi_test_clobber_r15: _CET_ENDBR xorq %r15,%r15 @@ -317,6 +335,7 @@ _CET_ENDBR .private_extern _abi_test_clobber_xmm0 .p2align 4 +.alt_entry _abi_test_clobber_xmm0 _abi_test_clobber_xmm0: _CET_ENDBR pxor %xmm0,%xmm0 @@ -326,6 +345,7 @@ _CET_ENDBR .private_extern _abi_test_clobber_xmm1 .p2align 4 +.alt_entry _abi_test_clobber_xmm1 _abi_test_clobber_xmm1: _CET_ENDBR pxor %xmm1,%xmm1 @@ -335,6 +355,7 @@ _CET_ENDBR .private_extern _abi_test_clobber_xmm2 .p2align 4 +.alt_entry _abi_test_clobber_xmm2 _abi_test_clobber_xmm2: _CET_ENDBR pxor %xmm2,%xmm2 @@ -344,6 +365,7 @@ _CET_ENDBR .private_extern _abi_test_clobber_xmm3 .p2align 4 +.alt_entry _abi_test_clobber_xmm3 _abi_test_clobber_xmm3: _CET_ENDBR pxor %xmm3,%xmm3 @@ -353,6 +375,7 @@ _CET_ENDBR .private_extern _abi_test_clobber_xmm4 .p2align 4 +.alt_entry _abi_test_clobber_xmm4 _abi_test_clobber_xmm4: _CET_ENDBR pxor %xmm4,%xmm4 @@ -362,6 +385,7 @@ _CET_ENDBR .private_extern _abi_test_clobber_xmm5 .p2align 4 +.alt_entry _abi_test_clobber_xmm5 _abi_test_clobber_xmm5: _CET_ENDBR pxor %xmm5,%xmm5 @@ -371,6 +395,7 @@ _CET_ENDBR .private_extern _abi_test_clobber_xmm6 .p2align 4 +.alt_entry _abi_test_clobber_xmm6 _abi_test_clobber_xmm6: _CET_ENDBR pxor %xmm6,%xmm6 @@ -380,6 +405,7 @@ _CET_ENDBR .private_extern _abi_test_clobber_xmm7 .p2align 4 +.alt_entry _abi_test_clobber_xmm7 _abi_test_clobber_xmm7: _CET_ENDBR pxor %xmm7,%xmm7 @@ -389,6 +415,7 @@ _CET_ENDBR .private_extern _abi_test_clobber_xmm8 .p2align 4 +.alt_entry _abi_test_clobber_xmm8 _abi_test_clobber_xmm8: _CET_ENDBR pxor %xmm8,%xmm8 @@ -398,6 +425,7 @@ _CET_ENDBR .private_extern _abi_test_clobber_xmm9 .p2align 4 +.alt_entry _abi_test_clobber_xmm9 _abi_test_clobber_xmm9: _CET_ENDBR pxor %xmm9,%xmm9 @@ -407,6 +435,7 @@ _CET_ENDBR .private_extern _abi_test_clobber_xmm10 .p2align 4 +.alt_entry _abi_test_clobber_xmm10 _abi_test_clobber_xmm10: _CET_ENDBR pxor %xmm10,%xmm10 @@ -416,6 +445,7 @@ _CET_ENDBR .private_extern _abi_test_clobber_xmm11 .p2align 4 +.alt_entry _abi_test_clobber_xmm11 _abi_test_clobber_xmm11: _CET_ENDBR pxor %xmm11,%xmm11 @@ -425,6 +455,7 @@ _CET_ENDBR .private_extern _abi_test_clobber_xmm12 .p2align 4 +.alt_entry _abi_test_clobber_xmm12 _abi_test_clobber_xmm12: _CET_ENDBR pxor %xmm12,%xmm12 @@ -434,6 +465,7 @@ _CET_ENDBR .private_extern _abi_test_clobber_xmm13 .p2align 4 +.alt_entry _abi_test_clobber_xmm13 _abi_test_clobber_xmm13: _CET_ENDBR pxor %xmm13,%xmm13 @@ -443,6 +475,7 @@ _CET_ENDBR .private_extern _abi_test_clobber_xmm14 .p2align 4 +.alt_entry _abi_test_clobber_xmm14 _abi_test_clobber_xmm14: _CET_ENDBR pxor %xmm14,%xmm14 @@ -452,29 +485,31 @@ _CET_ENDBR .private_extern _abi_test_clobber_xmm15 .p2align 4 +.alt_entry _abi_test_clobber_xmm15 _abi_test_clobber_xmm15: _CET_ENDBR pxor %xmm15,%xmm15 ret - - - +// abi_test_bad_unwind_wrong_register preserves the ABI, but annotates the wrong +// register in unwind metadata. +// void abi_test_bad_unwind_wrong_register(void); .globl _abi_test_bad_unwind_wrong_register .private_extern _abi_test_bad_unwind_wrong_register .p2align 4 +.alt_entry _abi_test_bad_unwind_wrong_register _abi_test_bad_unwind_wrong_register: _CET_ENDBR pushq %r12 +// This should be %r13 +// This should be %r13 - - - - - +// Windows evaluates epilogs directly in the unwinder, rather than using +// unwind codes. Add a nop so there is one non-epilog point (immediately +// before the nop) where the unwinder can observe the mistake. nop popq %r12 @@ -483,13 +518,14 @@ _CET_ENDBR - - - +// abi_test_bad_unwind_temporary preserves the ABI, but temporarily corrupts the +// storage space for a saved register, breaking unwind. +// void abi_test_bad_unwind_temporary(void); .globl _abi_test_bad_unwind_temporary .private_extern _abi_test_bad_unwind_temporary .p2align 4 +.alt_entry _abi_test_bad_unwind_temporary _abi_test_bad_unwind_temporary: @@ -502,11 +538,11 @@ _CET_ENDBR movq %r12,%rax incq %rax movq %rax,(%rsp) - - +// Unwinding from here is incorrect. Although %r12 itself has not been +// changed, the unwind codes say to look in (%rsp) instead. movq %r12,(%rsp) - +// Unwinding is now fixed. popq %r12 @@ -515,12 +551,13 @@ _CET_ENDBR - - - +// abi_test_get_and_clear_direction_flag clears the direction flag. If the flag +// was previously set, it returns one. Otherwise, it returns zero. +// int abi_test_get_and_clear_direction_flag(void); .globl _abi_test_get_and_clear_direction_flag .private_extern _abi_test_get_and_clear_direction_flag +.alt_entry _abi_test_get_and_clear_direction_flag _abi_test_get_and_clear_direction_flag: _CET_ENDBR pushfq @@ -531,11 +568,12 @@ _CET_ENDBR ret - - +// abi_test_set_direction_flag sets the direction flag. +// void abi_test_set_direction_flag(void); .globl _abi_test_set_direction_flag .private_extern _abi_test_set_direction_flag +.alt_entry _abi_test_set_direction_flag _abi_test_set_direction_flag: _CET_ENDBR std diff --git a/third_party/boringssl/gen/test_support/trampoline-x86_64-linux.S b/third_party/boringssl/gen/test_support/trampoline-x86_64-linux.S index 13d08a301..56ea8b19f 100644 --- a/third_party/boringssl/gen/test_support/trampoline-x86_64-linux.S +++ b/third_party/boringssl/gen/test_support/trampoline-x86_64-linux.S @@ -6,13 +6,13 @@ #if !defined(OPENSSL_NO_ASM) && defined(OPENSSL_X86_64) && defined(__ELF__) .text - - - - - - - +// abi_test_trampoline loads callee-saved registers from |state|, calls |func| +// with |argv|, then saves the callee-saved registers into |state|. It returns +// the result of |func|. If |unwind| is non-zero, this function triggers unwind +// instrumentation. +// uint64_t abi_test_trampoline(void (*func)(...), CallerState *state, +// const uint64_t *argv, size_t argc, +// int unwind); .globl abi_test_trampoline .hidden abi_test_trampoline .type abi_test_trampoline, @function @@ -21,15 +21,15 @@ abi_test_trampoline: .cfi_startproc _CET_ENDBR - - - - - - - - - +// Stack layout: +// 8 bytes - align +// 48 bytes - saved caller registers +// 8 bytes - scratch space +// 8 bytes - saved copy of $unwind (SysV-only) +// 8 bytes - saved copy of $state +// 8 bytes - saved copy of $func +// 8 bytes - if needed for stack alignment +// 8*4 bytes - parameters for $func subq $120,%rsp .cfi_adjust_cfa_offset 120 @@ -59,13 +59,13 @@ _CET_ENDBR movq 24(%rsi),%r13 movq 32(%rsi),%r14 movq 40(%rsi),%r15 - +// Stash $func and $state, so they are available after the call returns. movq %rdi,32(%rsp) movq %rsi,40(%rsp) - - - +// Load parameters. Note this will clobber $argv and $argc, so we can +// only use non-parameter volatile registers. There are three, and they +// are the same between SysV and Win64: %rax, %r10, and %r11. movq %rdx,%r10 movq %rcx,%r11 decq %r11 @@ -97,11 +97,11 @@ _CET_ENDBR decq %r11 js .Largs_done - - - - - +// This block should be: +// movq (%r10), %rtmp +// movq %rtmp, (%rax) +// There are no spare registers available, so we spill into the scratch +// space. movq %r11,56(%rsp) movq (%r10),%r11 movq %r11,(%rax) @@ -117,13 +117,13 @@ _CET_ENDBR testq %r10,%r10 jz .Lno_unwind - +// Set the trap flag. pushfq orq $0x100,0(%rsp) popfq - - +// Run an instruction to trigger a breakpoint immediately before the +// call. nop .globl abi_test_unwind_start .hidden abi_test_unwind_start @@ -134,11 +134,11 @@ abi_test_unwind_start: .hidden abi_test_unwind_return abi_test_unwind_return: - - - +// Clear the trap flag. Note this assumes the trap flag was clear on +// entry. We do not support instrumenting an unwind-instrumented +// |abi_test_trampoline|. pushfq - andq $-0x101,0(%rsp) + andq $-0x101,0(%rsp) // -0x101 is ~0x100 popfq .globl abi_test_unwind_stop .hidden abi_test_unwind_stop @@ -150,7 +150,7 @@ abi_test_unwind_stop: call *%rax .Lcall_done: - +// Store what $func did our state, so our caller can check. movq 40(%rsp),%rsi movq %rbx,0(%rsi) movq %rbp,8(%rsi) @@ -173,7 +173,7 @@ abi_test_unwind_stop: addq $120,%rsp .cfi_adjust_cfa_offset -120 - +// %rax already contains $func's return value, unmodified. ret .cfi_endproc @@ -457,9 +457,9 @@ _CET_ENDBR pxor %xmm15,%xmm15 ret .size abi_test_clobber_xmm15,.-abi_test_clobber_xmm15 - - - +// abi_test_bad_unwind_wrong_register preserves the ABI, but annotates the wrong +// register in unwind metadata. +// void abi_test_bad_unwind_wrong_register(void); .globl abi_test_bad_unwind_wrong_register .hidden abi_test_bad_unwind_wrong_register .type abi_test_bad_unwind_wrong_register, @function @@ -470,12 +470,12 @@ abi_test_bad_unwind_wrong_register: _CET_ENDBR pushq %r12 .cfi_adjust_cfa_offset 8 -.cfi_offset %r13,-16 - - - - +.cfi_offset %r13,-16 // This should be %r13 +// This should be %r13 +// Windows evaluates epilogs directly in the unwinder, rather than using +// unwind codes. Add a nop so there is one non-epilog point (immediately +// before the nop) where the unwinder can observe the mistake. nop popq %r12 .cfi_adjust_cfa_offset -8 @@ -485,9 +485,9 @@ _CET_ENDBR .cfi_endproc .size abi_test_bad_unwind_wrong_register,.-abi_test_bad_unwind_wrong_register - - - +// abi_test_bad_unwind_temporary preserves the ABI, but temporarily corrupts the +// storage space for a saved register, breaking unwind. +// void abi_test_bad_unwind_temporary(void); .globl abi_test_bad_unwind_temporary .hidden abi_test_bad_unwind_temporary .type abi_test_bad_unwind_temporary, @function @@ -505,11 +505,11 @@ _CET_ENDBR movq %r12,%rax incq %rax movq %rax,(%rsp) - - +// Unwinding from here is incorrect. Although %r12 itself has not been +// changed, the unwind codes say to look in (%rsp) instead. movq %r12,(%rsp) - +// Unwinding is now fixed. popq %r12 .cfi_adjust_cfa_offset -8 @@ -519,9 +519,9 @@ _CET_ENDBR .size abi_test_bad_unwind_temporary,.-abi_test_bad_unwind_temporary - - - +// abi_test_get_and_clear_direction_flag clears the direction flag. If the flag +// was previously set, it returns one. Otherwise, it returns zero. +// int abi_test_get_and_clear_direction_flag(void); .globl abi_test_get_and_clear_direction_flag .hidden abi_test_get_and_clear_direction_flag .type abi_test_get_and_clear_direction_flag, @function @@ -535,8 +535,8 @@ _CET_ENDBR ret .size abi_test_get_and_clear_direction_flag,.-abi_test_get_and_clear_direction_flag - - +// abi_test_set_direction_flag sets the direction flag. +// void abi_test_set_direction_flag(void); .globl abi_test_set_direction_flag .hidden abi_test_set_direction_flag .type abi_test_set_direction_flag, @function diff --git a/third_party/boringssl/gen/test_support/trampoline-x86_64-win.asm b/third_party/boringssl/gen/test_support/trampoline-x86_64-win.asm index ca2fb87bf..9ffbd6a52 100644 --- a/third_party/boringssl/gen/test_support/trampoline-x86_64-win.asm +++ b/third_party/boringssl/gen/test_support/trampoline-x86_64-win.asm @@ -14,13 +14,13 @@ default rel section .text code align=64 - - - - - - - +; abi_test_trampoline loads callee-saved registers from |state|, calls |func| +; with |argv|, then saves the callee-saved registers into |state|. It returns +; the result of |func|. If |unwind| is non-zero, this function triggers unwind +; instrumentation. +; uint64_t abi_test_trampoline(void (*func)(...), CallerState *state, +; const uint64_t *argv, size_t argc, +; int unwind); global abi_test_trampoline ALIGN 16 @@ -28,15 +28,15 @@ abi_test_trampoline: $L$SEH_begin_abi_test_trampoline_1: _CET_ENDBR - - - - - - - - - +; Stack layout: +; 8 bytes - align +; 224 bytes - saved caller registers +; 8 bytes - scratch space +; 8 bytes - saved copy of $unwind (SysV-only) +; 8 bytes - saved copy of $state +; 8 bytes - saved copy of $func +; 8 bytes - if needed for stack alignment +; 8*10 bytes - parameters for $func sub rsp,344 $L$SEH_prologue_abi_test_trampoline_2: @@ -113,13 +113,13 @@ $L$SEH_endprologue_abi_test_trampoline_21: movdqa xmm13,XMMWORD[176+rdx] movdqa xmm14,XMMWORD[192+rdx] movdqa xmm15,XMMWORD[208+rdx] - +; Stash $func and $state, so they are available after the call returns. mov QWORD[88+rsp],rcx mov QWORD[96+rsp],rdx - - - +; Load parameters. Note this will clobber $argv and $argc, so we can +; only use non-parameter volatile registers. There are three, and they +; are the same between SysV and Win64: %rax, %r10, and %r11. mov r10,r8 mov r11,r9 dec r11 @@ -143,11 +143,11 @@ $L$args_loop: dec r11 js NEAR $L$args_done - - - - - +; This block should be: +; movq (%r10), %rtmp +; movq %rtmp, (%rax) +; There are no spare registers available, so we spill into the scratch +; space. mov QWORD[104+rsp],r11 mov r11,QWORD[r10] mov QWORD[rax],r11 @@ -163,13 +163,13 @@ $L$args_done: test r10,r10 jz NEAR $L$no_unwind - +; Set the trap flag. pushfq or QWORD[rsp],0x100 popfq - - +; Run an instruction to trigger a breakpoint immediately before the +; call. nop global abi_test_unwind_start abi_test_unwind_start: @@ -178,11 +178,11 @@ abi_test_unwind_start: global abi_test_unwind_return abi_test_unwind_return: - - - +; Clear the trap flag. Note this assumes the trap flag was clear on +; entry. We do not support instrumenting an unwind-instrumented +; |abi_test_trampoline|. pushfq - and QWORD[rsp],-0x101 + and QWORD[rsp],-0x101 ; -0x101 is ~0x100 popfq global abi_test_unwind_stop abi_test_unwind_stop: @@ -193,7 +193,7 @@ $L$no_unwind: call rax $L$call_done: - +; Store what $func did our state, so our caller can check. mov rdx,QWORD[96+rsp] mov QWORD[rdx],rbx mov QWORD[8+rdx],rbp @@ -252,7 +252,7 @@ $L$call_done: add rsp,344 - +; %rax already contains $func's return value, unmodified. ret $L$SEH_end_abi_test_trampoline_22: @@ -505,9 +505,9 @@ _CET_ENDBR pxor xmm15,xmm15 ret - - - +; abi_test_bad_unwind_wrong_register preserves the ABI, but annotates the wrong +; register in unwind metadata. +; void abi_test_bad_unwind_wrong_register(void); global abi_test_bad_unwind_wrong_register ALIGN 16 @@ -516,12 +516,12 @@ abi_test_bad_unwind_wrong_register: $L$SEH_begin_abi_test_bad_unwind_wrong_register_1: _CET_ENDBR push r12 - -$L$SEH_prologue_abi_test_bad_unwind_wrong_register_2: +; This should be %r13 +$L$SEH_prologue_abi_test_bad_unwind_wrong_register_2: ; This should be %r13 $L$SEH_endprologue_abi_test_bad_unwind_wrong_register_3: - - - +; Windows evaluates epilogs directly in the unwinder, rather than using +; unwind codes. Add a nop so there is one non-epilog point (immediately +; before the nop) where the unwinder can observe the mistake. nop pop r12 @@ -530,9 +530,9 @@ $L$SEH_end_abi_test_bad_unwind_wrong_register_4: - - - +; abi_test_bad_unwind_temporary preserves the ABI, but temporarily corrupts the +; storage space for a saved register, breaking unwind. +; void abi_test_bad_unwind_temporary(void); global abi_test_bad_unwind_temporary ALIGN 16 @@ -548,11 +548,11 @@ $L$SEH_endprologue_abi_test_bad_unwind_temporary_3: mov rax,r12 inc rax mov QWORD[rsp],rax - - +; Unwinding from here is incorrect. Although %r12 itself has not been +; changed, the unwind codes say to look in (%rsp) instead. mov QWORD[rsp],r12 - +; Unwinding is now fixed. pop r12 @@ -561,9 +561,9 @@ $L$SEH_endprologue_abi_test_bad_unwind_temporary_3: $L$SEH_end_abi_test_bad_unwind_temporary_4: - - - +; abi_test_get_and_clear_direction_flag clears the direction flag. If the flag +; was previously set, it returns one. Otherwise, it returns zero. +; int abi_test_get_and_clear_direction_flag(void); global abi_test_get_and_clear_direction_flag abi_test_get_and_clear_direction_flag: @@ -576,8 +576,8 @@ _CET_ENDBR ret - - +; abi_test_set_direction_flag sets the direction flag. +; void abi_test_set_direction_flag(void); global abi_test_set_direction_flag abi_test_set_direction_flag: @@ -585,10 +585,10 @@ _CET_ENDBR std ret - - - - +; abi_test_bad_unwind_epilog preserves the ABI, and correctly annotates the +; prolog, but the epilog does not match Win64's rules, breaking unwind during +; the epilog. +; void abi_test_bad_unwind_epilog(void); global abi_test_bad_unwind_epilog ALIGN 16 @@ -600,7 +600,7 @@ $L$SEH_endprologue_abi_test_bad_unwind_epilog_3: nop - +; The epilog should begin here, but the nop makes it invalid. pop r12 nop ret @@ -628,7 +628,7 @@ ALIGN 4 section .xdata rdata align=8 ALIGN 4 $L$SEH_info_abi_test_trampoline_0: - DB 1 + DB 1 ; version 1, no flags DB $L$SEH_endprologue_abi_test_trampoline_21-$L$SEH_begin_abi_test_trampoline_1 DB 38 DB 0 @@ -691,7 +691,7 @@ $L$SEH_info_abi_test_trampoline_0: DW 43 $L$SEH_info_abi_test_bad_unwind_wrong_register_0: - DB 1 + DB 1 ; version 1, no flags DB $L$SEH_endprologue_abi_test_bad_unwind_wrong_register_3-$L$SEH_begin_abi_test_bad_unwind_wrong_register_1 DB 1 DB 0 @@ -700,7 +700,7 @@ $L$SEH_info_abi_test_bad_unwind_wrong_register_0: DW 0 $L$SEH_info_abi_test_bad_unwind_temporary_0: - DB 1 + DB 1 ; version 1, no flags DB $L$SEH_endprologue_abi_test_bad_unwind_temporary_3-$L$SEH_begin_abi_test_bad_unwind_temporary_1 DB 1 DB 0 @@ -709,7 +709,7 @@ $L$SEH_info_abi_test_bad_unwind_temporary_0: DW 0 $L$SEH_info_abi_test_bad_unwind_epilog_0: - DB 1 + DB 1 ; version 1, no flags DB $L$SEH_endprologue_abi_test_bad_unwind_epilog_3-$L$SEH_begin_abi_test_bad_unwind_epilog_1 DB 1 DB 0 diff --git a/third_party/boringssl/sources.cmake b/third_party/boringssl/sources.cmake index 751cfa3da..4e68eb56d 100644 --- a/third_party/boringssl/sources.cmake +++ b/third_party/boringssl/sources.cmake @@ -95,6 +95,7 @@ set(crypto_sources ${BORINGSSL_ROOT}src/crypto/cpu_arm_linux.cc ${BORINGSSL_ROOT}src/crypto/cpu_intel.cc ${BORINGSSL_ROOT}src/crypto/crypto.cc + ${BORINGSSL_ROOT}src/crypto/curve25519/cpace.cc ${BORINGSSL_ROOT}src/crypto/curve25519/curve25519.cc ${BORINGSSL_ROOT}src/crypto/curve25519/curve25519_64_adx.cc ${BORINGSSL_ROOT}src/crypto/curve25519/spake25519.cc @@ -121,9 +122,11 @@ set(crypto_sources ${BORINGSSL_ROOT}src/crypto/evp/p_ec.cc ${BORINGSSL_ROOT}src/crypto/evp/p_ed25519.cc ${BORINGSSL_ROOT}src/crypto/evp/p_hkdf.cc + ${BORINGSSL_ROOT}src/crypto/evp/p_keccak.cc ${BORINGSSL_ROOT}src/crypto/evp/p_mldsa.cc ${BORINGSSL_ROOT}src/crypto/evp/p_mlkem.cc ${BORINGSSL_ROOT}src/crypto/evp/p_rsa.cc + ${BORINGSSL_ROOT}src/crypto/evp/p_slhdsa.cc ${BORINGSSL_ROOT}src/crypto/evp/p_x25519.cc ${BORINGSSL_ROOT}src/crypto/evp/p_xwing.cc ${BORINGSSL_ROOT}src/crypto/evp/pbkdf.cc @@ -136,7 +139,6 @@ set(crypto_sources ${BORINGSSL_ROOT}src/crypto/fuzzer_mode.cc ${BORINGSSL_ROOT}src/crypto/hpke/hpke.cc ${BORINGSSL_ROOT}src/crypto/hrss/hrss.cc - ${BORINGSSL_ROOT}src/crypto/kyber/kyber.cc ${BORINGSSL_ROOT}src/crypto/lhash/lhash.cc ${BORINGSSL_ROOT}src/crypto/md4/md4.cc ${BORINGSSL_ROOT}src/crypto/md5/md5.cc @@ -167,7 +169,6 @@ set(crypto_sources ${BORINGSSL_ROOT}src/crypto/rand/forkunsafe.cc ${BORINGSSL_ROOT}src/crypto/rand/getentropy.cc ${BORINGSSL_ROOT}src/crypto/rand/ios.cc - ${BORINGSSL_ROOT}src/crypto/rand/passive.cc ${BORINGSSL_ROOT}src/crypto/rand/rand.cc ${BORINGSSL_ROOT}src/crypto/rand/trusty.cc ${BORINGSSL_ROOT}src/crypto/rand/urandom.cc @@ -237,6 +238,7 @@ set(crypto_sources ${BORINGSSL_ROOT}src/crypto/x509/x509_def.cc ${BORINGSSL_ROOT}src/crypto/x509/x509_ext.cc ${BORINGSSL_ROOT}src/crypto/x509/x509_lu.cc + ${BORINGSSL_ROOT}src/crypto/x509/x509_mtc.cc ${BORINGSSL_ROOT}src/crypto/x509/x509_obj.cc ${BORINGSSL_ROOT}src/crypto/x509/x509_req.cc ${BORINGSSL_ROOT}src/crypto/x509/x509_set.cc diff --git a/third_party/boringssl/src/crypto/asn1/a_bitstr.cc b/third_party/boringssl/src/crypto/asn1/a_bitstr.cc index a071589e8..60e76eb0e 100644 --- a/third_party/boringssl/src/crypto/asn1/a_bitstr.cc +++ b/third_party/boringssl/src/crypto/asn1/a_bitstr.cc @@ -32,7 +32,7 @@ using namespace bssl; static void set_unused_bits(ASN1_BIT_STRING *str, uint8_t unused_bits) { assert(unused_bits < 8); assert(unused_bits == 0 || str->length > 0); - // |ASN1_STRING_FLAG_BITS_LEFT| and the bottom 3 bits encode |padding|. + // `ASN1_STRING_FLAG_BITS_LEFT` and the bottom 3 bits encode `padding`. str->flags &= ~0x07; str->flags |= ASN1_STRING_FLAG_BITS_LEFT | unused_bits; } diff --git a/third_party/boringssl/src/crypto/asn1/a_gentm.cc b/third_party/boringssl/src/crypto/asn1/a_gentm.cc index 1dd22be0e..72615723c 100644 --- a/third_party/boringssl/src/crypto/asn1/a_gentm.cc +++ b/third_party/boringssl/src/crypto/asn1/a_gentm.cc @@ -107,7 +107,7 @@ ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_adj(ASN1_GENERALIZEDTIME *s, int ret = snprintf(buf, sizeof(buf), "%04d%02d%02d%02d%02d%02dZ", data.tm_year + 1900, data.tm_mon + 1, data.tm_mday, data.tm_hour, data.tm_min, data.tm_sec); - // |snprintf| must write exactly 15 bytes (plus the NUL) to the buffer. + // `snprintf` must write exactly 15 bytes (plus the NUL) to the buffer. BSSL_CHECK(ret == static_cast(sizeof(buf) - 1)); int free_s = 0; diff --git a/third_party/boringssl/src/crypto/asn1/a_int.cc b/third_party/boringssl/src/crypto/asn1/a_int.cc index 216c9daa8..9edbde203 100644 --- a/third_party/boringssl/src/crypto/asn1/a_int.cc +++ b/third_party/boringssl/src/crypto/asn1/a_int.cc @@ -42,8 +42,8 @@ int ASN1_INTEGER_cmp(const ASN1_INTEGER *x, const ASN1_INTEGER *y) { int ret = ASN1_STRING_cmp(x, y); if (neg) { - // This could be |-ret|, but |ASN1_STRING_cmp| is not forbidden from - // returning |INT_MIN|. + // This could be `-ret`, but `ASN1_STRING_cmp` is not forbidden from + // returning `INT_MIN`. if (ret < 0) { return 1; } else if (ret > 0) { @@ -56,7 +56,7 @@ int ASN1_INTEGER_cmp(const ASN1_INTEGER *x, const ASN1_INTEGER *y) { return ret; } -// negate_twos_complement negates |len| bytes from |buf| in-place, interpreted +// negate_twos_complement negates `len` bytes from `buf` in-place, interpreted // as a signed, big-endian two's complement value. static void negate_twos_complement(uint8_t *buf, size_t len) { uint8_t borrow = 0; @@ -96,7 +96,7 @@ int i2c_ASN1_INTEGER(const ASN1_INTEGER *in, unsigned char **outp) { return 0; } - // |ASN1_INTEGER|s should be represented minimally, but it is possible to + // `ASN1_INTEGER`s should be represented minimally, but it is possible to // construct invalid ones. Skip leading zeros so this does not produce an // invalid encoding or break invariants. CBS cbs; @@ -159,7 +159,7 @@ static int asn1_parse_integer_contents(Span in, return 0; } - // Convert to |ASN1_INTEGER|'s sign-and-magnitude representation. First, + // Convert to `ASN1_INTEGER`'s sign-and-magnitude representation. First, // determine the size needed for a minimal result. if (is_negative) { // 0xff00...01 through 0xff7f..ff have a two's complement of 0x00ff...ff @@ -353,7 +353,7 @@ static int asn1_string_get_int64(int64_t *out, const ASN1_STRING *a, int type) { } int64_t i64; int fits_in_i64; - // Check |v != 0| to handle manually-constructed negative zeros. + // Check `v != 0` to handle manually-constructed negative zeros. if ((a->type & V_ASN1_NEG) && v != 0) { i64 = (int64_t)(0u - v); fits_in_i64 = i64 < 0; diff --git a/third_party/boringssl/src/crypto/asn1/a_mbstr.cc b/third_party/boringssl/src/crypto/asn1/a_mbstr.cc index c70bb3b53..946ab52a6 100644 --- a/third_party/boringssl/src/crypto/asn1/a_mbstr.cc +++ b/third_party/boringssl/src/crypto/asn1/a_mbstr.cc @@ -82,7 +82,7 @@ int ASN1_mbstring_ncopy(ASN1_STRING **out, const unsigned char *in, return -1; } - // Check |minsize| and |maxsize| and work out the minimal type, if any. + // Check `minsize` and `maxsize` and work out the minimal type, if any. CBS cbs; CBS_init(&cbs, in, len); size_t utf8_len = 0, nchar = 0; @@ -209,7 +209,7 @@ int ASN1_mbstring_ncopy(ASN1_STRING **out, const unsigned char *in, } } if (/* OpenSSL historically NUL-terminated this value with a single byte, - * even for |MBSTRING_BMP| and |MBSTRING_UNIV|. */ + * even for `MBSTRING_BMP` and `MBSTRING_UNIV`. */ !CBB_add_u8(&cbb, 0) || // !CBB_finish(&cbb, &data, &data_len) || // data_len < 1 || // diff --git a/third_party/boringssl/src/crypto/asn1/a_object.cc b/third_party/boringssl/src/crypto/asn1/a_object.cc index 125058ab3..c88ca4f14 100644 --- a/third_party/boringssl/src/crypto/asn1/a_object.cc +++ b/third_party/boringssl/src/crypto/asn1/a_object.cc @@ -117,7 +117,7 @@ ASN1_OBJECT *c2i_ASN1_OBJECT(ASN1_OBJECT **out, const unsigned char **inp, ASN1_OBJECT_create(NID_undef, CBS_data(cbs), CBS_len(cbs), /*sn=*/nullptr, /*ln=*/nullptr); if (ret != nullptr) { - // |c2i_ASN1_OBJECT| consumes its whole input on success. + // `c2i_ASN1_OBJECT` consumes its whole input on success. BSSL_CHECK(CBS_skip(cbs, CBS_len(cbs))); } return ret; @@ -158,12 +158,12 @@ void ASN1_OBJECT_free(ASN1_OBJECT *a) { return; } if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_STRINGS) { - OPENSSL_free((void *)a->sn); - OPENSSL_free((void *)a->ln); + OPENSSL_free(const_cast(a->sn)); + OPENSSL_free(const_cast(a->ln)); a->sn = a->ln = nullptr; } if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_DATA) { - OPENSSL_free((void *)a->data); + OPENSSL_free(const_cast(a->data)); a->data = nullptr; a->length = 0; } diff --git a/third_party/boringssl/src/crypto/asn1/a_strex.cc b/third_party/boringssl/src/crypto/asn1/a_strex.cc index 663865ce9..933818eb4 100644 --- a/third_party/boringssl/src/crypto/asn1/a_strex.cc +++ b/third_party/boringssl/src/crypto/asn1/a_strex.cc @@ -15,7 +15,6 @@ #include #include -#include #include #include #include @@ -27,7 +26,6 @@ #include #include "../bytestring/internal.h" -#include "../internal.h" #include "internal.h" @@ -37,16 +35,18 @@ using namespace bssl; (ASN1_STRFLGS_ESC_2253 | ASN1_STRFLGS_ESC_QUOTE | ASN1_STRFLGS_ESC_CTRL | \ ASN1_STRFLGS_ESC_MSB) -static int maybe_write(BIO *out, const void *buf, int len) { - // If |out| is NULL, ignore the output but report the length. +namespace { + +int maybe_write(BIO *out, const void *buf, int len) { + // If `out` is NULL, ignore the output but report the length. return out == nullptr || BIO_write(out, buf, len) == len; } -static int is_control_character(unsigned char c) { return c < 32 || c == 127; } +int is_control_character(unsigned char c) { return c < 32 || c == 127; } -static int do_esc_char(uint32_t c, unsigned long flags, char *do_quotes, - BIO *out, int is_first, int is_last) { - // |c| is a |uint32_t| because, depending on |ASN1_STRFLGS_UTF8_CONVERT|, +int do_esc_char(uint32_t c, unsigned long flags, char *do_quotes, BIO *out, + int is_first, int is_last) { + // `c` is a `uint32_t` because, depending on `ASN1_STRFLGS_UTF8_CONVERT`, // we may be escaping bytes or Unicode codepoints. char buf[16]; // Large enough for "\\W01234567". unsigned char u8 = (unsigned char)c; @@ -93,8 +93,8 @@ static int do_esc_char(uint32_t c, unsigned long flags, char *do_quotes, // interprets the content formats and converts to or from UTF8 as // appropriate. -static int do_buf(const unsigned char *buf, int buflen, int encoding, - unsigned long flags, char *quotes, BIO *out) { +int do_buf(const unsigned char *buf, int buflen, int encoding, + unsigned long flags, char *quotes, BIO *out) { int (*get_char)(CBS *cbs, uint32_t *out); int get_char_error; switch (encoding) { @@ -158,7 +158,7 @@ static int do_buf(const unsigned char *buf, int buflen, int encoding, return outlen; } -static int do_hex_dump(BIO *out, Span in) { +int do_hex_dump(BIO *out, Span in) { if (in.size() > INT_MAX / 2) { return -1; } @@ -180,7 +180,7 @@ static int do_hex_dump(BIO *out, Span in) { // request it. We can either dump the content octets or the entire DER // encoding. This uses the RFC 2253 #01234 format. -static int do_dump(unsigned long flags, BIO *out, const ASN1_STRING *str) { +int do_dump(unsigned long flags, BIO *out, const ASN1_STRING *str) { if (!maybe_write(out, "#", 1)) { return -1; } @@ -195,7 +195,7 @@ static int do_dump(unsigned long flags, BIO *out, const ASN1_STRING *str) { } ScopedCBB cbb; - // Roughly estimate the encoded size with |str->length| to reduce unnecessary + // Roughly estimate the encoded size with `str->length` to reduce unnecessary // reallocations. (Tag, length, miscellaneous type-dependent overhead.) if (!CBB_init(cbb.get(), 4 + str->length) || !asn1_marshal_any_string(cbb.get(), str)) { @@ -208,12 +208,12 @@ static int do_dump(unsigned long flags, BIO *out, const ASN1_STRING *str) { return outlen + 1; } -// string_type_to_encoding returns the |MBSTRING_*| constant for the encoding -// used by the |ASN1_STRING| type |type|, or -1 if |tag| is not a string +// string_type_to_encoding returns the `MBSTRING_*` constant for the encoding +// used by the `ASN1_STRING` type `type`, or -1 if `tag` is not a string // type. -static int string_type_to_encoding(int type) { +int string_type_to_encoding(int type) { // This function is sometimes passed ASN.1 universal types and sometimes - // passed |ASN1_STRING| type values + // passed `ASN1_STRING` type values switch (type) { case V_ASN1_UTF8STRING: return MBSTRING_UTF8; @@ -224,7 +224,7 @@ static int string_type_to_encoding(int type) { case V_ASN1_UTCTIME: case V_ASN1_GENERALIZEDTIME: case V_ASN1_ISO64STRING: - // |MBSTRING_ASC| refers to Latin-1, not ASCII. + // `MBSTRING_ASC` refers to Latin-1, not ASCII. return MBSTRING_ASC; case V_ASN1_UNIVERSALSTRING: return MBSTRING_UNIV; @@ -234,6 +234,11 @@ static int string_type_to_encoding(int type) { return -1; } +const char *const kMon[12] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", + "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; + +} // namespace + // This is the main function, print out an ASN1_STRING taking note of various // escape and display options. Returns number of characters written or -1 if // an error occurred. @@ -251,7 +256,7 @@ int ASN1_STRING_print_ex(BIO *out, const ASN1_STRING *str, outlen++; } - // Decide what to do with |str|, either dump the contents or display it. + // Decide what to do with `str`, either dump the contents or display it. int encoding; if (flags & ASN1_STRFLGS_DUMP_ALL) { // Dump everything. @@ -302,7 +307,7 @@ int ASN1_STRING_print_ex_fp(FILE *fp, const ASN1_STRING *str, unsigned long flags) { BIO *bio = nullptr; if (fp != nullptr) { - // If |fp| is NULL, this function returns the number of bytes without + // If `fp` is NULL, this function returns the number of bytes without // writing. bio = BIO_new_fp(fp, BIO_NOCLOSE); if (bio == nullptr) { @@ -379,9 +384,6 @@ int ASN1_TIME_print(BIO *bp, const ASN1_TIME *tm) { return 0; } -static const char *const mon[12] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", - "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; - int ASN1_GENERALIZEDTIME_print(BIO *bp, const ASN1_GENERALIZEDTIME *tm) { CBS cbs; CBS_init(&cbs, tm->data, tm->length); @@ -391,7 +393,7 @@ int ASN1_GENERALIZEDTIME_print(BIO *bp, const ASN1_GENERALIZEDTIME *tm) { return 0; } - return BIO_printf(bp, "%s %2d %02d:%02d:%02d %d GMT", mon[utc.tm_mon], + return BIO_printf(bp, "%s %2d %02d:%02d:%02d %d GMT", kMon[utc.tm_mon], utc.tm_mday, utc.tm_hour, utc.tm_min, utc.tm_sec, utc.tm_year + 1900) > 0; } @@ -405,7 +407,7 @@ int ASN1_UTCTIME_print(BIO *bp, const ASN1_UTCTIME *tm) { return 0; } - return BIO_printf(bp, "%s %2d %02d:%02d:%02d %d GMT", mon[utc.tm_mon], + return BIO_printf(bp, "%s %2d %02d:%02d:%02d %d GMT", kMon[utc.tm_mon], utc.tm_mday, utc.tm_hour, utc.tm_min, utc.tm_sec, utc.tm_year + 1900) > 0; } diff --git a/third_party/boringssl/src/crypto/asn1/a_strnid.cc b/third_party/boringssl/src/crypto/asn1/a_strnid.cc index 7ebf30a75..db50d518e 100644 --- a/third_party/boringssl/src/crypto/asn1/a_strnid.cc +++ b/third_party/boringssl/src/crypto/asn1/a_strnid.cc @@ -38,45 +38,12 @@ DEFINE_LHASH_OF(ASN1_STRING_TABLE) BSSL_NAMESPACE_END -static LHASH_OF(ASN1_STRING_TABLE) *string_tables = nullptr; -static StaticMutex string_tables_lock; - void ASN1_STRING_set_default_mask(unsigned long mask) {} unsigned long ASN1_STRING_get_default_mask() { return B_ASN1_UTF8STRING; } int ASN1_STRING_set_default_mask_asc(const char *p) { return 1; } -static const ASN1_STRING_TABLE *asn1_string_table_get(int nid); - -// The following function generates an ASN1_STRING based on limits in a -// table. Frequently the types and length of an ASN1_STRING are restricted by -// a corresponding OID. For example certificates and certificate requests. - -ASN1_STRING *ASN1_STRING_set_by_NID(ASN1_STRING **out, const unsigned char *in, - ossl_ssize_t len, int inform, int nid) { - ASN1_STRING *str = nullptr; - int ret; - if (!out) { - out = &str; - } - const ASN1_STRING_TABLE *tbl = asn1_string_table_get(nid); - if (tbl != nullptr) { - unsigned long mask = tbl->mask; - if (!(tbl->flags & STABLE_NO_MASK)) { - mask &= B_ASN1_UTF8STRING; - } - ret = ASN1_mbstring_ncopy(out, in, len, inform, mask, tbl->minsize, - tbl->maxsize); - } else { - ret = ASN1_mbstring_copy(out, in, len, inform, B_ASN1_UTF8STRING); - } - if (ret <= 0) { - return nullptr; - } - return *out; -} - // Now the tables and helper functions for the string table: // See RFC 5280. @@ -89,9 +56,14 @@ ASN1_STRING *ASN1_STRING_set_by_NID(ASN1_STRING **out, const unsigned char *in, #define ub_email_address 128 #define ub_serial_number 64 +namespace { + +LHASH_OF(ASN1_STRING_TABLE) *string_tables = nullptr; +StaticMutex string_tables_lock; + // This table must be kept in NID order -static const ASN1_STRING_TABLE tbl_standard[] = { +const ASN1_STRING_TABLE tbl_standard[] = { {NID_commonName, 1, ub_common_name, DIRSTRING_TYPE, 0}, {NID_countryName, 2, 2, B_ASN1_PRINTABLESTRING, STABLE_NO_MASK}, {NID_localityName, 1, ub_locality_name, DIRSTRING_TYPE, 0}, @@ -115,7 +87,7 @@ static const ASN1_STRING_TABLE tbl_standard[] = { {NID_domainComponent, 1, -1, B_ASN1_IA5STRING, STABLE_NO_MASK}, {NID_ms_csp_name, -1, -1, B_ASN1_BMPSTRING, STABLE_NO_MASK}}; -static int table_cmp(const ASN1_STRING_TABLE *a, const ASN1_STRING_TABLE *b) { +int table_cmp(const ASN1_STRING_TABLE *a, const ASN1_STRING_TABLE *b) { if (a->nid < b->nid) { return -1; } @@ -125,16 +97,16 @@ static int table_cmp(const ASN1_STRING_TABLE *a, const ASN1_STRING_TABLE *b) { return 0; } -static int table_cmp_void(const void *a, const void *b) { +int table_cmp_void(const void *a, const void *b) { return table_cmp(reinterpret_cast(a), reinterpret_cast(b)); } -static uint32_t table_hash(const ASN1_STRING_TABLE *tbl) { +uint32_t table_hash(const ASN1_STRING_TABLE *tbl) { return OPENSSL_hash32(&tbl->nid, sizeof(tbl->nid)); } -static const ASN1_STRING_TABLE *asn1_string_table_get(int nid) { +const ASN1_STRING_TABLE *asn1_string_table_get(int nid) { ASN1_STRING_TABLE key; key.nid = nid; const ASN1_STRING_TABLE *tbl = reinterpret_cast( @@ -149,12 +121,42 @@ static const ASN1_STRING_TABLE *asn1_string_table_get(int nid) { tbl = lh_ASN1_STRING_TABLE_retrieve(string_tables, &key); } string_tables_lock.UnlockRead(); - // Note returning |tbl| without the lock is only safe because - // |ASN1_STRING_TABLE_add| cannot modify or delete existing entries. If we + // Note returning `tbl` without the lock is only safe because + // `ASN1_STRING_TABLE_add` cannot modify or delete existing entries. If we // wish to support that, this function must copy the result under a lock. return tbl; } +} // namespace + +// The following function generates an ASN1_STRING based on limits in a +// table. Frequently the types and length of an ASN1_STRING are restricted by +// a corresponding OID. For example certificates and certificate requests. + +ASN1_STRING *ASN1_STRING_set_by_NID(ASN1_STRING **out, const unsigned char *in, + ossl_ssize_t len, int inform, int nid) { + ASN1_STRING *str = nullptr; + int ret; + if (!out) { + out = &str; + } + const ASN1_STRING_TABLE *tbl = asn1_string_table_get(nid); + if (tbl != nullptr) { + unsigned long mask = tbl->mask; + if (!(tbl->flags & STABLE_NO_MASK)) { + mask &= B_ASN1_UTF8STRING; + } + ret = ASN1_mbstring_ncopy(out, in, len, inform, mask, tbl->minsize, + tbl->maxsize); + } else { + ret = ASN1_mbstring_copy(out, in, len, inform, B_ASN1_UTF8STRING); + } + if (ret <= 0) { + return nullptr; + } + return *out; +} + int ASN1_STRING_TABLE_add(int nid, long minsize, long maxsize, unsigned long mask, unsigned long flags) { // Existing entries cannot be overwritten. diff --git a/third_party/boringssl/src/crypto/asn1/a_time.cc b/third_party/boringssl/src/crypto/asn1/a_time.cc index 9d642d4ab..ccddadbf7 100644 --- a/third_party/boringssl/src/crypto/asn1/a_time.cc +++ b/third_party/boringssl/src/crypto/asn1/a_time.cc @@ -108,7 +108,7 @@ ASN1_GENERALIZEDTIME *ASN1_TIME_to_generalizedtime(const ASN1_TIME *in, } } else { assert(in->type == V_ASN1_UTCTIME); - // |ASN1_TIME_check| implies a bound on the string's lengths. In particular, + // `ASN1_TIME_check` implies a bound on the string's lengths. In particular, // the longest possible UTCTime is "YYMMDDHHMMSS+HHMM", with the (invalid) // timezone offsets. static constexpr size_t kMaxUTCTimeLength = 17; diff --git a/third_party/boringssl/src/crypto/asn1/a_type.cc b/third_party/boringssl/src/crypto/asn1/a_type.cc index 03ac9bf48..eb5a91409 100644 --- a/third_party/boringssl/src/crypto/asn1/a_type.cc +++ b/third_party/boringssl/src/crypto/asn1/a_type.cc @@ -21,11 +21,49 @@ #include #include +#include "../bytestring/internal.h" +#include "../internal.h" #include "internal.h" using namespace bssl; +ASN1_TYPE *ASN1_TYPE_new() { + ASN1_TYPE *ret = New(); + if (ret == nullptr) { + return nullptr; + } + OPENSSL_memset(ret, 0, sizeof(ASN1_TYPE)); + ret->type = -1; + return ret; +} + +static void asn1_type_cleanup(ASN1_TYPE *a) { + switch (a->type) { + case V_ASN1_NULL: + a->value.ptr = nullptr; + break; + case V_ASN1_BOOLEAN: + a->value.boolean = ASN1_BOOLEAN_NONE; + break; + case V_ASN1_OBJECT: + ASN1_OBJECT_free(a->value.object); + a->value.object = nullptr; + break; + default: + ASN1_STRING_free(a->value.asn1_string); + a->value.asn1_string = nullptr; + break; + } +} + +void ASN1_TYPE_free(ASN1_TYPE *a) { + if (a != nullptr) { + asn1_type_cleanup(a); + Delete(a); + } +} + int ASN1_TYPE_get(const ASN1_TYPE *a) { switch (a->type) { case V_ASN1_NULL: @@ -52,8 +90,8 @@ const void *bssl::asn1_type_value_as_pointer(const ASN1_TYPE *a) { } void bssl::asn1_type_set0_string(ASN1_TYPE *a, ASN1_STRING *str) { - // |ASN1_STRING| types are almost the same as |ASN1_TYPE| types, except that - // the negative flag is not reflected into |ASN1_TYPE|. + // `ASN1_STRING` types are almost the same as `ASN1_TYPE` types, except that + // the negative flag is not reflected into `ASN1_TYPE`. int type = str->type; if (type == V_ASN1_NEG_INTEGER) { type = V_ASN1_INTEGER; @@ -61,32 +99,13 @@ void bssl::asn1_type_set0_string(ASN1_TYPE *a, ASN1_STRING *str) { type = V_ASN1_ENUMERATED; } - // These types are not |ASN1_STRING| types and use a different - // representation when stored in |ASN1_TYPE|. + // These types are not `ASN1_STRING` types and use a different + // representation when stored in `ASN1_TYPE`. assert(type != V_ASN1_NULL && type != V_ASN1_OBJECT && type != V_ASN1_BOOLEAN); ASN1_TYPE_set(a, type, str); } -void bssl::asn1_type_cleanup(ASN1_TYPE *a) { - switch (a->type) { - case V_ASN1_NULL: - a->value.ptr = nullptr; - break; - case V_ASN1_BOOLEAN: - a->value.boolean = ASN1_BOOLEAN_NONE; - break; - case V_ASN1_OBJECT: - ASN1_OBJECT_free(a->value.object); - a->value.object = nullptr; - break; - default: - ASN1_STRING_free(a->value.asn1_string); - a->value.asn1_string = nullptr; - break; - } -} - void ASN1_TYPE_set(ASN1_TYPE *a, int type, void *value) { asn1_type_cleanup(a); a->type = type; @@ -128,24 +147,23 @@ int ASN1_TYPE_set1(ASN1_TYPE *a, int type, const void *value) { return 1; } -// Returns 0 if they are equal, != 0 otherwise. int ASN1_TYPE_cmp(const ASN1_TYPE *a, const ASN1_TYPE *b) { - int result = -1; + if (a == nullptr || b == nullptr) { + // NULL sorts before any non-NULL value. Two NULLs are equal. + return (a != nullptr) - (b != nullptr); + } - if (!a || !b || a->type != b->type) { - return -1; + if (a->type != b->type) { + return a->type < b->type ? -1 : 1; } switch (a->type) { case V_ASN1_OBJECT: - result = OBJ_cmp(a->value.object, b->value.object); - break; + return OBJ_cmp(a->value.object, b->value.object); case V_ASN1_NULL: - result = 0; // They do not have content. - break; + return 0; // They do not have content. case V_ASN1_BOOLEAN: - result = a->value.boolean - b->value.boolean; - break; + return a->value.boolean - b->value.boolean; case V_ASN1_INTEGER: case V_ASN1_ENUMERATED: case V_ASN1_BIT_STRING: @@ -167,11 +185,8 @@ int ASN1_TYPE_cmp(const ASN1_TYPE *a, const ASN1_TYPE *b) { case V_ASN1_UTF8STRING: case V_ASN1_OTHER: default: - result = ASN1_STRING_cmp(a->value.asn1_string, b->value.asn1_string); - break; + return ASN1_STRING_cmp(a->value.asn1_string, b->value.asn1_string); } - - return result; } int bssl::asn1_parse_any(CBS *cbs, ASN1_TYPE *out) { @@ -231,7 +246,7 @@ int bssl::asn1_parse_any_as_string(CBS *cbs, ASN1_STRING *out) { } // Reject unexpectedly constructed or primitive universal types, rather than - // encoding them as an opaque |V_ASN1_OTHER|. As of X.680 (02/2021), tag + // encoding them as an opaque `V_ASN1_OTHER`. As of X.680 (02/2021), tag // numbers 0-36 have been allocated, except 15. Of these, only 8 (EXTERNAL), // 11 (EMBEDDED PDV), 16 (SEQUENCE), 17 (SET), and 29 (CHARACTER STRING) are // constructed. @@ -255,15 +270,15 @@ int bssl::asn1_parse_any_as_string(CBS *cbs, ASN1_STRING *out) { } // Historically, parsing high universal tag numbers made OpenSSL's - // |ASN1_STRING| representation ambiguous. We've since fixed this with - // |V_ASN1_OTHER| but, for now, continue to enforce the limit. + // `ASN1_STRING` representation ambiguous. We've since fixed this with + // `V_ASN1_OTHER` but, for now, continue to enforce the limit. if (tag_class == CBS_ASN1_UNIVERSAL && number > V_ASN1_MAX_UNIVERSAL) { OPENSSL_PUT_ERROR(ASN1, ASN1_R_DECODE_ERROR); return 0; } - // These types are just parsed as |V_ASN1_OTHER| here. Check the contents - // before the generic |V_ASN1_OTHER| path. + // These types are just parsed as `V_ASN1_OTHER` here. Check the contents + // before the generic `V_ASN1_OTHER` path. CBS body = elem; BSSL_CHECK(CBS_skip(&body, header_len)); switch (tag) { @@ -273,6 +288,12 @@ int bssl::asn1_parse_any_as_string(CBS *cbs, ASN1_STRING *out) { return 0; } break; + case CBS_ASN1_RELATIVE_OID: + if (!CBS_is_valid_asn1_relative_oid(&body)) { + OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_OBJECT_ENCODING); + return 0; + } + break; case CBS_ASN1_NULL: if (CBS_len(&body) != 0) { OPENSSL_PUT_ERROR(ASN1, ASN1_R_NULL_IS_WRONG_LENGTH); @@ -307,13 +328,13 @@ int bssl::asn1_parse_any_as_string(CBS *cbs, ASN1_STRING *out) { case CBS_ASN1_UTF8STRING: return asn1_parse_utf8_string(&elem, out, tag); case CBS_ASN1_UTCTIME: - // TODO(crbug.com/42290221): Reject timezone offsets here. We have no - // known cases where UTCTime inside ANY needs accept invalid timezones. - return asn1_parse_utc_time(&elem, out, tag, /*allow_timezone_offset=*/1); + return asn1_parse_utc_time(&elem, out, tag, /*allow_timezone_offset=*/0); case CBS_ASN1_GENERALIZEDTIME: return asn1_parse_generalized_time(&elem, out, tag); case CBS_ASN1_OCTETSTRING: + return asn1_parse_octet_string(&elem, out, tag); case CBS_ASN1_T61STRING: + return asn1_parse_t61_string(&elem, out, tag); case CBS_ASN1_IA5STRING: case CBS_ASN1_NUMERICSTRING: case CBS_ASN1_PRINTABLESTRING: @@ -321,19 +342,14 @@ int bssl::asn1_parse_any_as_string(CBS *cbs, ASN1_STRING *out) { case CBS_ASN1_GRAPHICSTRING: case CBS_ASN1_VISIBLESTRING: case CBS_ASN1_GENERALSTRING: - // T61String is parsed as Latin-1, so all byte strings are valid. The - // others we currently do not enforce. - // - // TODO(crbug.com/42290290): Enforce the encoding of the other string - // types. - if (!asn1_parse_octet_string(&elem, out, tag)) { - return 0; - } - out->type = static_cast(tag); - return 1; + // |CBS_ASN1_*| and |V_ASN1_*| constants match for universal types. + static_assert(CBS_ASN1_IA5STRING == V_ASN1_IA5STRING); + // TODO(crbug.com/42290290): Enforce the encoding of these string types. + return asn1_parse_string_unchecked(&elem, out, static_cast(tag), + tag); default: // All unrecognized types, or types that cannot be represented as - // |ASN1_STRING|, are represented as the whole element. + // `ASN1_STRING`, are represented as the whole element. if (!ASN1_STRING_set(out, CBS_data(&elem), CBS_len(&elem))) { return 0; } @@ -379,12 +395,12 @@ int bssl::asn1_marshal_any(CBB *out, const ASN1_TYPE *in) { case V_ASN1_SEQUENCE: case V_ASN1_SET: case V_ASN1_OTHER: - // If |in->type| and the underlying |ASN1_STRING| type don't match, use - // |in->type|. See b/446993031. + // If `in->type` and the underlying `ASN1_STRING` type don't match, use + // `in->type`. See b/446993031. return asn1_marshal_string_with_type(out, in->value.asn1_string, in->type); default: - // |ASN1_TYPE|s can have type -1 when default-constructed. + // `ASN1_TYPE`s can have type -1 when default-constructed. OPENSSL_PUT_ERROR(ASN1, ASN1_R_WRONG_TYPE); return 0; } @@ -424,7 +440,7 @@ static int asn1_marshal_string_with_type(CBB *out, const ASN1_STRING *in, return CBB_add_bytes(out, ASN1_STRING_get0_data(in), ASN1_STRING_length(in)); default: - // |ASN1_TYPE|s can have type -1 when default-constructed. + // `ASN1_TYPE`s can have type -1 when default-constructed. OPENSSL_PUT_ERROR(ASN1, ASN1_R_WRONG_TYPE); return 0; } @@ -433,3 +449,19 @@ static int asn1_marshal_string_with_type(CBB *out, const ASN1_STRING *in, int bssl::asn1_marshal_any_string(CBB *out, const ASN1_STRING *in) { return asn1_marshal_string_with_type(out, in, in->type); } + +ASN1_TYPE *d2i_ASN1_TYPE(ASN1_TYPE **out, const uint8_t **inp, long len) { + return D2IFromCBS(out, inp, len, [](CBS *cbs) -> UniquePtr { + UniquePtr ret(ASN1_TYPE_new()); + if (ret == nullptr || !asn1_parse_any(cbs, ret.get())) { + return nullptr; + } + return ret; + }); +} + +int i2d_ASN1_TYPE(const ASN1_TYPE *in, uint8_t **outp) { + return I2DFromCBB(/*initial_capacity=*/16, outp, [&](CBB *cbb) -> bool { + return asn1_marshal_any(cbb, in); + }); +} diff --git a/third_party/boringssl/src/crypto/asn1/a_utctm.cc b/third_party/boringssl/src/crypto/asn1/a_utctm.cc index 16a140670..1f4d7d0bf 100644 --- a/third_party/boringssl/src/crypto/asn1/a_utctm.cc +++ b/third_party/boringssl/src/crypto/asn1/a_utctm.cc @@ -105,7 +105,7 @@ ASN1_UTCTIME *ASN1_UTCTIME_adj(ASN1_UTCTIME *s, int64_t posix_time, int ret = snprintf(buf, sizeof(buf), "%02d%02d%02d%02d%02d%02dZ", data.tm_year % 100, data.tm_mon + 1, data.tm_mday, data.tm_hour, data.tm_min, data.tm_sec); - // |snprintf| must write exactly 15 bytes (plus the NUL) to the buffer. + // `snprintf` must write exactly 15 bytes (plus the NUL) to the buffer. BSSL_CHECK(ret == static_cast(sizeof(buf) - 1)); int free_s = 0; diff --git a/third_party/boringssl/src/crypto/asn1/asn1_lib.cc b/third_party/boringssl/src/crypto/asn1/asn1_lib.cc index c129c6b46..960cabb31 100644 --- a/third_party/boringssl/src/crypto/asn1/asn1_lib.cc +++ b/third_party/boringssl/src/crypto/asn1/asn1_lib.cc @@ -14,6 +14,7 @@ #include +#include #include #include @@ -63,13 +64,13 @@ OPENSSL_DECLARE_ERROR_REASON(ASN1, UNKNOWN_FORMAT) OPENSSL_DECLARE_ERROR_REASON(ASN1, UNKNOWN_TAG) OPENSSL_DECLARE_ERROR_REASON(ASN1, UNSUPPORTED_TYPE) -// Limit |ASN1_STRING|s to 64 MiB of data. Most of this module, as well as +// Limit `ASN1_STRING`s to 64 MiB of data. Most of this module, as well as // downstream code, does not correctly handle overflow. We cap string fields -// more tightly than strictly necessary to fit in |int|. This is not expected to +// more tightly than strictly necessary to fit in `int`. This is not expected to // impact real world uses of this field. // // In particular, this limit is small enough that the bit count of a BIT STRING -// comfortably fits in an |int|, with room for arithmetic. +// comfortably fits in an `int`, with room for arithmetic. #define ASN1_STRING_MAX (64 * 1024 * 1024) static void asn1_put_length(unsigned char **pp, int length); @@ -110,7 +111,7 @@ int ASN1_get_object(const unsigned char **inp, long *out_len, int *out_tag, return constructed; } -// class 0 is constructed constructed == 2 for indefinite length constructed +// class 0 is constructed, constructed == 2 for indefinite length constructed void ASN1_put_object(unsigned char **pp, int constructed, int length, int tag, int xclass) { unsigned char *p = *pp; @@ -270,7 +271,7 @@ int ASN1_STRING_set(ASN1_STRING *str, const void *_data, ossl_ssize_t len_s) { if (data != nullptr) { OPENSSL_memcpy(str->data, data, len); // Historically, OpenSSL would NUL-terminate most (but not all) - // |ASN1_STRING|s, in case anyone accidentally passed |str->data| into a + // `ASN1_STRING`s, in case anyone accidentally passed `str->data` into a // function expecting a C string. We retain this behavior for compatibility, // but code must not rely on this. See CVE-2021-3712. str->data[len] = '\0'; @@ -279,6 +280,7 @@ int ASN1_STRING_set(ASN1_STRING *str, const void *_data, ossl_ssize_t len_s) { } void ASN1_STRING_set0(ASN1_STRING *str, void *data, int len) { + BSSL_CHECK(len >= 0); OPENSSL_free(str->data); str->data = reinterpret_cast(data); str->length = len; @@ -360,6 +362,8 @@ int ASN1_STRING_cmp(const ASN1_STRING *a, const ASN1_STRING *b) { int ASN1_STRING_length(const ASN1_STRING *str) { return str->length; } +size_t ASN1_STRING_get_length(const ASN1_STRING *str) { return str->length; } + int ASN1_STRING_type(const ASN1_STRING *str) { return str->type; } unsigned char *ASN1_STRING_data(ASN1_STRING *str) { return str->data; } @@ -368,9 +372,9 @@ const unsigned char *ASN1_STRING_get0_data(const ASN1_STRING *str) { return str->data; } -int bssl::asn1_parse_octet_string(CBS *cbs, ASN1_STRING *out, - CBS_ASN1_TAG tag) { - tag = tag == 0 ? CBS_ASN1_OCTETSTRING : tag; +int bssl::asn1_parse_string_unchecked(CBS *cbs, ASN1_STRING *out, int str_type, + CBS_ASN1_TAG tag) { + assert(tag != 0); CBS child; if (!CBS_get_asn1(cbs, &child, tag)) { OPENSSL_PUT_ERROR(ASN1, ASN1_R_DECODE_ERROR); @@ -379,10 +383,24 @@ int bssl::asn1_parse_octet_string(CBS *cbs, ASN1_STRING *out, if (!ASN1_STRING_set(out, CBS_data(&child), CBS_len(&child))) { return 0; } - out->type = V_ASN1_OCTET_STRING; + out->type = str_type; return 1; } +int bssl::asn1_parse_octet_string(CBS *cbs, ASN1_STRING *out, + CBS_ASN1_TAG tag) { + // Nothing to check for OCTET STRING. + tag = tag == 0 ? CBS_ASN1_OCTETSTRING : tag; + return asn1_parse_string_unchecked(cbs, out, V_ASN1_OCTET_STRING, tag); +} + +int bssl::asn1_parse_t61_string(CBS *cbs, ASN1_STRING *out, CBS_ASN1_TAG tag) { + // Nothing to check for T61String. We parse it as Latin-1 and all byte strings + // are valid Latin-1. + tag = tag == 0 ? CBS_ASN1_T61STRING : tag; + return asn1_parse_string_unchecked(cbs, out, V_ASN1_T61STRING, tag); +} + int bssl::asn1_marshal_octet_string(CBB *out, const ASN1_STRING *in, CBS_ASN1_TAG tag) { tag = tag == 0 ? CBS_ASN1_OCTETSTRING : tag; diff --git a/third_party/boringssl/src/crypto/asn1/asn1_par.cc b/third_party/boringssl/src/crypto/asn1/asn1_par.cc index 7767ccad9..54921d56c 100644 --- a/third_party/boringssl/src/crypto/asn1/asn1_par.cc +++ b/third_party/boringssl/src/crypto/asn1/asn1_par.cc @@ -30,7 +30,7 @@ const char *ASN1_tag2str(int tag) { "ENUMERATED", "", "UTF8STRING", - "", + "RELATIVE-OID", "", "", "SEQUENCE", diff --git a/third_party/boringssl/src/crypto/asn1/internal.h b/third_party/boringssl/src/crypto/asn1/internal.h index 970de1075..382a1ee8e 100644 --- a/third_party/boringssl/src/crypto/asn1/internal.h +++ b/third_party/boringssl/src/crypto/asn1/internal.h @@ -19,28 +19,31 @@ #include #include +#include + +#include "../internal.h" BSSL_NAMESPACE_BEGIN // Wrapper functions for time functions. -// OPENSSL_gmtime converts a time_t value in |time| which must be in the range -// of year 0000 to 9999 to a broken out time value in |tm|. On success |tm| is +// OPENSSL_gmtime converts a time_t value in `time` which must be in the range +// of year 0000 to 9999 to a broken out time value in `tm`. On success `tm` is // returned. On failure NULL is returned. OPENSSL_EXPORT struct tm *OPENSSL_gmtime(const time_t *time, struct tm *result); -// OPENSSL_gmtime_adj returns one on success, and updates |tm| by adding -// |offset_day| days and |offset_sec| seconds. It returns zero on failure. |tm| +// OPENSSL_gmtime_adj returns one on success, and updates `tm` by adding +// `offset_day` days and `offset_sec` seconds. It returns zero on failure. `tm` // must be in the range of year 0000 to 9999 both before and after the update or // a failure will be returned. OPENSSL_EXPORT int OPENSSL_gmtime_adj(struct tm *tm, int offset_day, int64_t offset_sec); -// OPENSSL_gmtime_diff calculates the difference between |from| and |to|. It +// OPENSSL_gmtime_diff calculates the difference between `from` and `to`. It // returns one, and outputs the difference as a number of days and seconds in -// |*out_days| and |*out_secs| on success. It returns zero on failure. Both -// |from| and |to| must be in the range of year 0000 to 9999 or a failure will +// `*out_days` and `*out_secs` on success. It returns zero on failure. Both +// `from` and `to` must be in the range of year 0000 to 9999 or a failure will // be returned. OPENSSL_EXPORT int OPENSSL_gmtime_diff(int *out_days, int *out_secs, const struct tm *from, @@ -57,10 +60,10 @@ OPENSSL_EXPORT int OPENSSL_gmtime_diff(int *out_days, int *out_secs, BSSL_NAMESPACE_END -// An asn1_object_st (aka |ASN1_OBJECT|) represents an ASN.1 OBJECT IDENTIFIER. -// Note: Mutating an |ASN1_OBJECT| is only permitted when initializing it. The -// library maintains a table of static |ASN1_OBJECT|s, which may be referenced -// by non-const |ASN1_OBJECT| pointers. Code which receives an |ASN1_OBJECT| +// An asn1_object_st (aka `ASN1_OBJECT`) represents an ASN.1 OBJECT IDENTIFIER. +// Note: Mutating an `ASN1_OBJECT` is only permitted when initializing it. The +// library maintains a table of static `ASN1_OBJECT`s, which may be referenced +// by non-const `ASN1_OBJECT` pointers. Code which receives an `ASN1_OBJECT` // pointer externally must assume it is immutable, even if the pointer is not // const. struct asn1_object_st { @@ -75,44 +78,70 @@ BSSL_NAMESPACE_BEGIN ASN1_OBJECT *ASN1_OBJECT_new(); -// asn1_parse_object parses a DER-encoded ASN.1 OBJECT IDENTIFIER from |cbs| and -// write the result to |out|. If |tag| is non-zero, the value is implicitly -// tagged with |tag|. On success, it returns a newly-allocated |ASN1_OBJECT| -// with the result and advances |cbs| past the parsed element. +// asn1_parse_object parses a DER-encoded ASN.1 OBJECT IDENTIFIER from `cbs` and +// write the result to `out`. If `tag` is non-zero, the value is implicitly +// tagged with `tag`. On success, it returns a newly-allocated `ASN1_OBJECT` +// with the result and advances `cbs` past the parsed element. // // TODO(crbug.com/boringssl/414361735): This should return a bssl::UniquePtr, // but cannot until it is made C++ linkage. ASN1_OBJECT *asn1_parse_object(CBS *cbs, CBS_ASN1_TAG tag); -// asn1_marshal_object marshals |in| as a DER-encoded, ASN.1 OBJECT IDENTIFIER -// and writes the result to |out|. It returns one on success and zero on error. -// If |tag| is non-zero, the tag is replaced with |tag|. +// asn1_marshal_object marshals `in` as a DER-encoded, ASN.1 OBJECT IDENTIFIER +// and writes the result to `out`. It returns one on success and zero on error. +// If `tag` is non-zero, the tag is replaced with `tag`. int asn1_marshal_object(CBB *out, const ASN1_OBJECT *in, CBS_ASN1_TAG tag); // Strings. -// asn1_is_printable returns one if |value| is a valid Unicode codepoint for an +// asn1_is_printable returns one if `value` is a valid Unicode codepoint for an // ASN.1 PrintableString, and zero otherwise. int asn1_is_printable(uint32_t value); -// asn1_string_init initializes |str|, which may be uninitialized, with type -// |type|. +// asn1_string_init initializes `str`, which may be uninitialized, with type +// `type`. void asn1_string_init(ASN1_STRING *str, int type); -// asn1_string_cleanup releases memory associated with |str|'s value, without -// freeing |str| itself. +// asn1_string_cleanup releases memory associated with `str`'s value, without +// freeing `str` itself. void asn1_string_cleanup(ASN1_STRING *str); +// A ScopedASN1String is a stack-allocatable `ASN1_STRING` with managed +// lifetime. +// TODO(crbug.com/443769299): Once `ASN1_STRING` is no longer public, this can +// instead use `DECLARE_OPAQUE_STRUCT`. +class ScopedASN1String { + public: + explicit ScopedASN1String(int type) { asn1_string_init(&str_, type); } + ScopedASN1String(const ScopedASN1String &) = delete; + ScopedASN1String &operator=(const ScopedASN1String &) = delete; + ~ScopedASN1String() { asn1_string_cleanup(&str_); } + + ASN1_STRING *get() { return &str_; } + const ASN1_STRING *get() const { return &str_; } + ASN1_STRING *operator->() { return &str_; } + const ASN1_STRING *operator->() const { return &str_; } + + private: + ASN1_STRING str_; +}; + +// asn1_parse_string_unchecked parses a DER-encoded string of type `str_type`, +// tagged with `tag`. It does not check the contents. +int asn1_parse_string_unchecked(CBS *cbs, ASN1_STRING *out, int str_type, + CBS_ASN1_TAG tag); + // The following functions parse a DER-encoded ASN.1 value of the specified -// type from |cbs| and write the result to |*out|. If |tag| is non-zero, the -// value is implicitly tagged with |tag|. On success, they return one and -// advance |cbs| past the parsed element. On entry, |*out| must contain an -// |ASN1_STRING| in some valid state. +// type from `cbs` and write the result to `*out`. If `tag` is non-zero, the +// value is implicitly tagged with `tag`. On success, they return one and +// advance `cbs` past the parsed element. On entry, `*out` must contain an +// `ASN1_STRING` in some valid state. int asn1_parse_bit_string(CBS *cbs, ASN1_BIT_STRING *out, CBS_ASN1_TAG tag); int asn1_parse_integer(CBS *cbs, ASN1_INTEGER *out, CBS_ASN1_TAG tag); int asn1_parse_enumerated(CBS *cbs, ASN1_ENUMERATED *out, CBS_ASN1_TAG tag); int asn1_parse_octet_string(CBS *cbs, ASN1_STRING *out, CBS_ASN1_TAG tag); +int asn1_parse_t61_string(CBS *cbs, ASN1_STRING *out, CBS_ASN1_TAG tag); int asn1_parse_bmp_string(CBS *cbs, ASN1_BMPSTRING *out, CBS_ASN1_TAG tag); int asn1_parse_universal_string(CBS *cbs, ASN1_UNIVERSALSTRING *out, CBS_ASN1_TAG tag); @@ -123,25 +152,25 @@ int asn1_parse_generalized_time(CBS *cbs, ASN1_GENERALIZEDTIME *out, int asn1_parse_utc_time(CBS *cbs, ASN1_UTCTIME *out, CBS_ASN1_TAG tag, int allow_timezone_offset); -// asn1_parse_bit_string_with_bad_length behaves like |asn1_parse_bit_string| +// asn1_parse_bit_string_with_bad_length behaves like `asn1_parse_bit_string` // but tolerates BER non-minimal, definite lengths. int asn1_parse_bit_string_with_bad_length(CBS *cbs, ASN1_BIT_STRING *out); -// asn1_marshal_bit_string marshals |in| as a DER-encoded, ASN.1 BIT STRING and -// writes the result to |out|. It returns one on success and zero on error. If -// |tag| is non-zero, the tag is replaced with |tag|. +// asn1_marshal_bit_string marshals `in` as a DER-encoded, ASN.1 BIT STRING and +// writes the result to `out`. It returns one on success and zero on error. If +// `tag` is non-zero, the tag is replaced with `tag`. int asn1_marshal_bit_string(CBB *out, const ASN1_BIT_STRING *in, CBS_ASN1_TAG tag); -// asn1_marshal_integer marshals |in| as a DER-encoded, ASN.1 INTEGER and writes -// the result to |out|. It returns one on success and zero on error. If |tag| is -// non-zero, the tag is replaced with |tag|. This can also be used to marshal an +// asn1_marshal_integer marshals `in` as a DER-encoded, ASN.1 INTEGER and writes +// the result to `out`. It returns one on success and zero on error. If `tag` is +// non-zero, the tag is replaced with `tag`. This can also be used to marshal an // ASN.1 ENUMERATED value by overriding the tag. int asn1_marshal_integer(CBB *out, const ASN1_INTEGER *in, CBS_ASN1_TAG tag); -// asn1_marshal_octet_string marshals |in| as a DER-encoded, ASN.1 OCTET STRING -// and writes the result to |out|. It returns one on success and zero on error. -// If |tag| is non-zero, the tag is replaced with |tag|. +// asn1_marshal_octet_string marshals `in` as a DER-encoded, ASN.1 OCTET STRING +// and writes the result to `out`. It returns one on success and zero on error. +// If `tag` is non-zero, the tag is replaced with `tag`. // // This function may be used to marshal other string-based universal types whose // encoding is that of an implicitly-tagged OCTET STRING, e.g. UTF8String. @@ -159,37 +188,33 @@ int asn1_marshal_time(CBB *cbb, const ASN1_TIME *in); // The ASN.1 ANY type. -// asn1_type_value_as_pointer returns |a|'s value in pointer form. This is +// asn1_type_value_as_pointer returns `a`'s value in pointer form. This is // usually the value object but, for BOOLEAN values, is 0 or 0xff cast to // a pointer. const void *asn1_type_value_as_pointer(const ASN1_TYPE *a); -// asn1_type_set0_string sets |a|'s value to the object represented by |str| and -// takes ownership of |str|. +// asn1_type_set0_string sets `a`'s value to the object represented by `str` and +// takes ownership of `str`. void asn1_type_set0_string(ASN1_TYPE *a, ASN1_STRING *str); -// asn1_type_cleanup releases memory associated with |a|'s value, without -// freeing |a| itself. -void asn1_type_cleanup(ASN1_TYPE *a); - -// asn1_parse_any parses a DER-encoded ASN.1 value of any type from |cbs| and -// writes the result to |*out|. On success, it advances |cbs| past the parsed -// element and returns one. On entry, |*out| must contain an |ASN1_TYPE| in some +// asn1_parse_any parses a DER-encoded ASN.1 value of any type from `cbs` and +// writes the result to `*out`. On success, it advances `cbs` past the parsed +// element and returns one. On entry, `*out` must contain an `ASN1_TYPE` in some // valid state. int asn1_parse_any(CBS *cbs, ASN1_TYPE *out); -// asn1_parse_any_as_string behaves like |asn1_parse_any| but represents the -// value as an |ASN1_STRING|. Types which are not represented with -// |ASN1_STRING|, such as |ASN1_OBJECT|, are represented with type -// |V_ASN1_OTHER|. +// asn1_parse_any_as_string behaves like `asn1_parse_any` but represents the +// value as an `ASN1_STRING`. Types which are not represented with +// `ASN1_STRING`, such as `ASN1_OBJECT`, are represented with type +// `V_ASN1_OTHER`. int asn1_parse_any_as_string(CBS *cbs, ASN1_STRING *out); -// asn1_marshal_any marshals |in| as a DER-encoded ASN.1 value and writes the -// result to |out|. It returns one on success and zeron on error. +// asn1_marshal_any marshals `in` as a DER-encoded ASN.1 value and writes the +// result to `out`. It returns one on success and zero on error. int asn1_marshal_any(CBB *out, const ASN1_TYPE *in); -// asn1_marshal_any_string marshals |in| as a DER-encoded ASN.1 value and writes -// the result to |out|. It returns one on success and zeron on error. +// asn1_marshal_any_string marshals `in` as a DER-encoded ASN.1 value and writes +// the result to `out`. It returns one on success and zero on error. int asn1_marshal_any_string(CBB *out, const ASN1_STRING *in); @@ -198,9 +223,9 @@ int asn1_marshal_any_string(CBB *out, const ASN1_STRING *in); // ASN1_ENCODING is used to save the received encoding of an ASN.1 type. This // avoids problems with invalid encodings that break signatures. typedef struct ASN1_ENCODING_st { - // enc is the saved DER encoding. Its ownership is determined by |buf|. + // enc is the saved DER encoding. Its ownership is determined by `buf`. uint8_t *enc; - // len is the length of |enc|. If zero, there is no saved encoding. + // len is the length of `enc`. If zero, there is no saved encoding. size_t len; } ASN1_ENCODING; @@ -209,40 +234,46 @@ void ASN1_item_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it); void ASN1_template_free(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt); -// ASN1_item_ex_d2i parses |len| bytes from |*in| as a structure of type |it| -// and writes the result to |*pval|. If |tag| is non-negative, |it| is -// implicitly tagged with the tag specified by |tag| and |aclass|. If |opt| is -// non-zero, the value is optional. -// -// This function returns one and advances |*in| if an object was successfully -// parsed, -1 if an optional value was successfully skipped, and zero on error. -int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len, - const ASN1_ITEM *it, int tag, int aclass, char opt); - -// ASN1_item_ex_i2d encodes |*pval| as a value of type |it| to |out| under the -// i2d output convention. It returns a non-zero length on success and -1 on -// error. If |tag| is -1. the tag and class come from |it|. Otherwise, the tag -// number is |tag| and the class is |aclass|. This is used for implicit tagging. -// This function treats a missing value as an error, not an optional field. -int ASN1_item_ex_i2d(ASN1_VALUE **pval, unsigned char **out, - const ASN1_ITEM *it, int tag, int aclass); - void ASN1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it); -// asn1_get_choice_selector returns the CHOICE selector value for |*pval|, which -// must of type |it|. +// asn1_load_ptr returns `*pval`, but without asserting `*pval`'s underlying +// type for strict aliasing. This helper must be used when the field's +// underlying type is not known statically. It must not be used for +// `ASN1_BOOLEAN` fields, which are not a pointers. +inline ASN1_VALUE *asn1_load_ptr(ASN1_VALUE **pval) { + ASN1_VALUE *ret; + OPENSSL_memcpy(&ret, pval, sizeof(ASN1_VALUE *)); + return ret; +} + +// asn1_load_ptr_as behaves like `asn1_load_ptr`, but casts the result to `T*`. +template +inline T *asn1_load_ptr_as(ASN1_VALUE **pval) { + return reinterpret_cast(asn1_load_ptr(pval)); +} + +// asn1_store_ptr sets `*pval` to `v`, but without asserting `*pval`'s +// underlying type for strict aliasing. This helper must be used when the +// field's underlying type is not known statically. It must not be used for +// `ASN1_BOOLEAN` fields, which are not a pointers. +inline void asn1_store_ptr(ASN1_VALUE **pval, const void *v) { + OPENSSL_memcpy(pval, &v, sizeof(v)); +} + +// asn1_get_choice_selector returns the CHOICE selector value for `*pval`, which +// must of type `it`. int asn1_get_choice_selector(ASN1_VALUE **pval, const ASN1_ITEM *it); int asn1_set_choice_selector(ASN1_VALUE **pval, int value, const ASN1_ITEM *it); -// asn1_get_field_ptr returns a pointer to the field in |*pval| corresponding to -// |tt|. +// asn1_get_field_ptr returns a pointer to the field in `*pval` corresponding to +// `tt`. ASN1_VALUE **asn1_get_field_ptr(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt); -// asn1_do_adb returns the |ASN1_TEMPLATE| for the ANY DEFINED BY field |tt|, -// based on the selector INTEGER or OID in |*pval|. If |tt| is not an ADB field, -// it returns |tt|. If the selector does not match any value, it returns NULL. -// If |nullerr| is non-zero, it will additionally push an error to the error +// asn1_do_adb returns the `ASN1_TEMPLATE` for the ANY DEFINED BY field `tt`, +// based on the selector INTEGER or OID in `*pval`. If `tt` is not an ADB field, +// it returns `tt`. If the selector does not match any value, it returns NULL. +// If `nullerr` is non-zero, it will additionally push an error to the error // queue when there is no match. const ASN1_TEMPLATE *asn1_do_adb(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt, int nullerr); @@ -253,21 +284,30 @@ int asn1_refcount_dec_and_test_zero(ASN1_VALUE **pval, const ASN1_ITEM *it); void asn1_enc_init(ASN1_VALUE **pval, const ASN1_ITEM *it); void asn1_enc_free(ASN1_VALUE **pval, const ASN1_ITEM *it); -// asn1_enc_restore, if |*pval| has a saved encoding, writes it to |out| under -// the i2d output convention, sets |*len| to the length, and returns one. If it -// has no saved encoding, it returns zero. -int asn1_enc_restore(int *len, unsigned char **out, ASN1_VALUE **pval, +// asn1_enc_restore, if `*pval` has a saved encoding, sets `*out` to it, and +// returns one. If it has no saved encoding, it returns zero. +int asn1_enc_restore(Span *out, ASN1_VALUE **pval, const ASN1_ITEM *it); -// asn1_enc_save saves |inlen| bytes from |in| as |*pval|'s saved encoding. It -// returns one on success and zero on error. If |buf| is non-NULL, |in| must -// point into |buf|. +// asn1_enc_save saves `inlen` bytes from `in` as `*pval`'s saved encoding. It +// returns one on success and zero on error. If `buf` is non-NULL, `in` must +// point into `buf`. int asn1_enc_save(ASN1_VALUE **pval, const uint8_t *in, size_t inlen, const ASN1_ITEM *it); -// asn1_encoding_clear clears the cached encoding in |enc|. +// asn1_encoding_clear clears the cached encoding in `enc`. void asn1_encoding_clear(ASN1_ENCODING *enc); +// asn1_tag_to_cbs converts a tag from this library's representation (the +// `V_ASN1_*` constants) to that of `CBS` and `CBB`. It returns the tag on +// success and zero if the input could not be represented. (Zero would be +// [UNIVERSAL 0], which is a reserved tag value.) +// +// The resulting tag will not have the constructed bit set. The caller must OR +// the result with `CBS_ASN1_CONSTRUCTED` if necessary, before passing to +// `CBS` and `CBB` functions. +CBS_ASN1_TAG asn1_tag_to_cbs(int aclass, int tag); + typedef struct { int nid; long minsize; @@ -276,8 +316,8 @@ typedef struct { unsigned long flags; } ASN1_STRING_TABLE; -// asn1_get_string_table_for_testing sets |*out_ptr| and |*out_len| to the table -// of built-in |ASN1_STRING_TABLE| values. It is exported for testing. +// asn1_get_string_table_for_testing sets `*out_ptr` and `*out_len` to the table +// of built-in `ASN1_STRING_TABLE` values. It is exported for testing. OPENSSL_EXPORT void asn1_get_string_table_for_testing( const ASN1_STRING_TABLE **out_ptr, size_t *out_len); @@ -287,21 +327,23 @@ typedef ASN1_VALUE *ASN1_d2i_func(ASN1_VALUE **a, const unsigned char **in, long length); typedef int ASN1_i2d_func(ASN1_VALUE *a, unsigned char **in); -// An ASN1_ex_parse function should parse a value from |cbs| and set |*pval| to -// the result. It should return one on success and zero on failure. If |opt| is +// An ASN1_ex_parse function should parse a value from `cbs` and set `*pval` to +// the result. It should return one on success and zero on failure. If `opt` is // non-zero, the field may be optional. If an optional element is missing, the -// function should return one and consume zero bytes from |cbs|. +// function should return one and consume zero bytes from `cbs`. // -// If |opt| is non-zero, the function can assume that |*pval| is nullptr on -// entry. Otherwise, |*pval| may either be nullptr, or the result of -// |ASN1_ex_new_func|. The function may either write into the existing object, +// If `opt` is non-zero, the function can assume that `*pval` is nullptr on +// entry. Otherwise, `*pval` may either be nullptr, or the result of +// `ASN1_ex_new_func`. The function may either write into the existing object, // if any, or unconditionally make a new one. (The existing object comes from // tasn_new.cc recursively filling in objects before parsing into them.) typedef int ASN1_ex_parse(ASN1_VALUE **pval, CBS *cbs, const ASN1_ITEM *it, int opt); -typedef int ASN1_ex_i2d(ASN1_VALUE **pval, unsigned char **out, - const ASN1_ITEM *it); +// An ASN1_ex_marshal function should marshal `*pval`, a value of type `it`, to +// `cbb` and return one on success or zero on error. +typedef int ASN1_ex_marshal(CBB *cbb, ASN1_VALUE **pval, const ASN1_ITEM *it); + typedef int ASN1_ex_new_func(ASN1_VALUE **pval, const ASN1_ITEM *it); typedef void ASN1_ex_free_func(ASN1_VALUE **pval, const ASN1_ITEM *it); @@ -309,54 +351,79 @@ typedef struct ASN1_EXTERN_FUNCS_st { ASN1_ex_new_func *asn1_ex_new; ASN1_ex_free_func *asn1_ex_free; ASN1_ex_parse *asn1_ex_parse; - ASN1_ex_i2d *asn1_ex_i2d; + ASN1_ex_marshal *asn1_ex_marshal; } ASN1_EXTERN_FUNCS; -#define IMPLEMENT_EXTERN_ASN1_SIMPLE(name, new_func, free_func, tag, \ - parse_func, i2d_func) \ - static int name##_new_cb(ASN1_VALUE **pval, const ASN1_ITEM *it) { \ - *pval = (ASN1_VALUE *)new_func(); \ - return *pval != nullptr; \ - } \ - \ - static void name##_free_cb(ASN1_VALUE **pval, const ASN1_ITEM *it) { \ - free_func((name *)*pval); \ - *pval = nullptr; \ - } \ - \ - static int name##_parse_cb(ASN1_VALUE **pval, CBS *cbs, const ASN1_ITEM *it, \ - int opt) { \ - if (opt && !CBS_peek_asn1_tag(cbs, (tag))) { \ - return 1; \ - } \ - \ - if ((*pval == nullptr && !name##_new_cb(pval, it)) || \ - !parse_func(cbs, (name *)*pval)) { \ - return 0; \ - } \ - return 1; \ - } \ - \ - static int name##_i2d_cb(ASN1_VALUE **pval, unsigned char **out, \ - const ASN1_ITEM *it) { \ - return i2d_func((name *)*pval, out); \ - } \ - \ - static const ASN1_EXTERN_FUNCS name##_extern_funcs = { \ - name##_new_cb, name##_free_cb, name##_parse_cb, name##_i2d_cb}; \ - \ +// IMPLEMENT_EXTERN_ASN1_PARSE_NEW implements an `ASN1_ITEM` for a type whose +// parse function returns a new object. +#define IMPLEMENT_EXTERN_ASN1_PARSE_NEW(name, new_func, free_func, tag, \ + parse_func, marshal_func) \ + namespace { \ + int name##_new_cb(ASN1_VALUE **pval, const ASN1_ITEM *it) { \ + name *obj = new_func(); \ + if (obj == nullptr) { \ + return 0; \ + } \ + asn1_store_ptr(pval, obj); \ + return 1; \ + } \ + \ + void name##_free_cb(ASN1_VALUE **pval, const ASN1_ITEM *it) { \ + free_func(asn1_load_ptr_as(pval)); \ + asn1_store_ptr(pval, nullptr); \ + } \ + \ + int name##_parse_cb(ASN1_VALUE **pval, CBS *cbs, const ASN1_ITEM *it, \ + int opt) { \ + if (opt && !CBS_peek_asn1_tag(cbs, (tag))) { \ + return 1; \ + } \ + \ + bssl::UniquePtr ret = parse_func(cbs); \ + if (ret == nullptr) { \ + return 0; \ + } \ + name##_free_cb(pval, it); \ + asn1_store_ptr(pval, ret.release()); \ + return 1; \ + } \ + \ + int name##_marshal_cb(CBB *cbb, ASN1_VALUE **pval, const ASN1_ITEM *it) { \ + return marshal_func(cbb, asn1_load_ptr_as(pval)); \ + } \ + \ + const ASN1_EXTERN_FUNCS name##_extern_funcs = { \ + name##_new_cb, name##_free_cb, name##_parse_cb, name##_marshal_cb}; \ + } \ IMPLEMENT_EXTERN_ASN1(name, name##_extern_funcs) -// ASN1_TIME is an |ASN1_ITEM| whose ASN.1 type is X.509 Time (RFC 5280) and C -// type is |ASN1_TIME*|. +// IMPLEMENT_EXTERN_ASN1_PARSE_INTO implements an `ASN1_ITEM` for a type whose +// parse function writes into an existing object. +#define IMPLEMENT_EXTERN_ASN1_PARSE_INTO(name, new_func, free_func, tag, \ + parse_func, marshal_func) \ + \ + namespace { \ + bssl::UniquePtr name##_parse_new(CBS *cbs) { \ + bssl::UniquePtr ret(new_func()); \ + if (ret == nullptr || !parse_func(cbs, ret.get())) { \ + return 0; \ + } \ + return ret; \ + } \ + } \ + IMPLEMENT_EXTERN_ASN1_PARSE_NEW(name, new_func, free_func, tag, \ + name##_parse_new, marshal_func) + +// ASN1_TIME is an `ASN1_ITEM` whose ASN.1 type is X.509 Time (RFC 5280) and C +// type is `ASN1_TIME*`. DECLARE_ASN1_ITEM(ASN1_TIME) -// DIRECTORYSTRING is an |ASN1_ITEM| whose ASN.1 type is X.509 DirectoryString -// (RFC 5280) and C type is |ASN1_STRING*|. +// DIRECTORYSTRING is an `ASN1_ITEM` whose ASN.1 type is X.509 DirectoryString +// (RFC 5280) and C type is `ASN1_STRING*`. DECLARE_ASN1_ITEM(DIRECTORYSTRING) -// DISPLAYTEXT is an |ASN1_ITEM| whose ASN.1 type is X.509 DisplayText (RFC -// 5280) and C type is |ASN1_STRING*|. +// DISPLAYTEXT is an `ASN1_ITEM` whose ASN.1 type is X.509 DisplayText (RFC +// 5280) and C type is `ASN1_STRING*`. DECLARE_ASN1_ITEM(DISPLAYTEXT) BSSL_NAMESPACE_END diff --git a/third_party/boringssl/src/crypto/asn1/tasn_dec.cc b/third_party/boringssl/src/crypto/asn1/tasn_dec.cc index 20701dffc..85d44e7a9 100644 --- a/third_party/boringssl/src/crypto/asn1/tasn_dec.cc +++ b/third_party/boringssl/src/crypto/asn1/tasn_dec.cc @@ -23,35 +23,57 @@ #include #include -#include "../bytestring/internal.h" +#include + #include "../internal.h" #include "internal.h" using namespace bssl; -// Constructed types with a recursive definition (such as can be found in PKCS7) -// could eventually exceed the stack given malicious input with excessive -// recursion. Therefore we limit the stack depth. This is the maximum number of -// recursive invocations of asn1_item_embed_d2i(). +// Constructed types with a recursive definition could eventually exceed the +// stack given malicious input with excessive recursion. We don't currently +// support such types (our PKCS#7 parser does not use this framework and does +// not implement the full recursive PKCS#7), but limit recursion nonetheless. #define ASN1_MAX_CONSTRUCTED_NEST 30 -static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass, - char *cst, const unsigned char **in, long len, - int exptag, int expclass, char opt); - -static int asn1_template_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, - long len, const ASN1_TEMPLATE *tt, char opt, - int depth); -static int asn1_template_noexp_d2i(ASN1_VALUE **val, const unsigned char **in, - long len, const ASN1_TEMPLATE *tt, char opt, - int depth); -static int asn1_d2i_ex_primitive(ASN1_VALUE **pval, const unsigned char **in, - long len, const ASN1_ITEM *it, int tag, - int aclass, char opt); -static int asn1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, - long len, const ASN1_ITEM *it, int tag, int aclass, - char opt, int depth); +// The following functions parse a structure of type `it` (or `tt`) from `cbs` +// and write the result to `*pval`. They return one on success and zero on +// error. +// +// If `opt` is true, the field is optional. If an optional element is missing, +// the function returns one and consumes zero bytes from `cbs`. +// +// If `tag` is non-zero, the type is implicitly-tagged with the specified tag. +// `tag` should not have the constructed bit set. If the ASN.1 type is +// constructed, it will be set as appropriate. +// +// On entry, `*pval` may either be null or a default-initialized object, +// according to tasn_new.cc. It, however, may not be an arbitrary object. +// Historically, this code supported arbitrary objects, though not very well, so +// some remnants of more general handling remain. +// +// With the exception of `asn1_parse_item`, there may be a partial object left +// in `*pval` on error. `asn1_parse_item` is the main entrypoint and is +// responsible for recursively freeing objects on error. +static int asn1_parse_item(ASN1_VALUE **pval, CBS *cbs, const ASN1_ITEM *it, + CBS_ASN1_TAG tag, bool opt, int depth); +static int asn1_parse_template(ASN1_VALUE **pval, CBS *cbs, + const ASN1_TEMPLATE *tt, CBS_ASN1_TAG tag, + bool opt, int depth); + +// asn1_parse_template_no_explicit behaves like `asn1_parse_template` but +// ignores any explicit tag specified in `tt`. +static int asn1_parse_template_no_explicit(ASN1_VALUE **pval, CBS *cbs, + const ASN1_TEMPLATE *tt, + CBS_ASN1_TAG tag, bool opt, + int depth); + +// asn1_parse_item_primitive behaves like `asn1_parse_item` but only supports +// primitive types. +static int asn1_parse_item_primitive(ASN1_VALUE **pval, CBS *cbs, + const ASN1_ITEM *it, CBS_ASN1_TAG tag, + bool opt, int depth); unsigned long ASN1_tag2bit(int tag) { switch (tag) { @@ -92,625 +114,418 @@ unsigned long ASN1_tag2bit(int tag) { } } -// Decode an ASN1 item, this currently behaves just like a standard 'd2i' -// function. 'in' points to a buffer to read the data from, in future we -// will have more advanced versions that can input data a piece at a time and -// this will simply be a special case. - -ASN1_VALUE *ASN1_item_d2i(ASN1_VALUE **pval, const unsigned char **in, long len, +ASN1_VALUE *ASN1_item_d2i(ASN1_VALUE **pval, const uint8_t **in, long len, const ASN1_ITEM *it) { + if (len < 0) { + OPENSSL_PUT_ERROR(ASN1, ASN1_R_BUFFER_TOO_SMALL); + return nullptr; + } + + // Bound `len` to comfortably fit in an `int`. Historically, this parser + // routinely mismatched integer types and did not correctly handle overflow. + // This is now largely fixed, but continue to cap the length to be safe. + len = std::min(len, long{INT_MAX / 2}); + + CBS cbs; + CBS_init(&cbs, *in, len); ASN1_VALUE *ret = nullptr; - if (asn1_item_ex_d2i(&ret, in, len, it, /*tag=*/-1, /*aclass=*/0, /*opt=*/0, - /*depth=*/0) <= 0) { - // Clean up, in case the caller left a partial object. - // - // TODO(davidben): I don't think it can leave one, but the codepaths below - // are a bit inconsistent. Revisit this when rewriting this function. - ASN1_item_ex_free(&ret, it); + if (!asn1_parse_item(&ret, &cbs, it, /*tag=*/0, /*opt=*/false, /*depth=*/0)) { + return nullptr; } // If the caller supplied an output pointer, free the old one and replace it - // with |ret|. This differs from OpenSSL slightly in that we don't support + // with `ret`. This differs from OpenSSL slightly in that we don't support // object reuse. We run this on both success and failure. On failure, even // with object reuse, OpenSSL destroys the previous object. if (pval != nullptr) { ASN1_item_ex_free(pval, it); - *pval = ret; + asn1_store_ptr(pval, ret); } + *in = CBS_data(&cbs); return ret; } -// Decode an item, taking care of IMPLICIT tagging, if any. If 'opt' set and -// tag mismatch return -1 to handle OPTIONAL -// -// TODO(davidben): Historically, all functions in this file had to account for -// |*pval| containing an arbitrary existing value. This is no longer the case -// because |ASN1_item_d2i| now always starts from NULL. As part of rewriting -// this function, take the simplified assumptions into account. Though we must -// still account for the internal calls to |ASN1_item_ex_new|. - -static int asn1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, - long len, const ASN1_ITEM *it, int tag, int aclass, - char opt, int depth) { - const ASN1_TEMPLATE *tt, *errtt = nullptr; - const unsigned char *p = nullptr, *q; - unsigned char oclass; - char cst, isopt; - int i; - int otag; - int ret = 0; - ASN1_VALUE **pchptr; - if (!pval) { - return 0; - } - if (len < 0) { - OPENSSL_PUT_ERROR(ASN1, ASN1_R_BUFFER_TOO_SMALL); - goto err; +template +static T *ensure_value(ASN1_VALUE **pval) { + T *obj = asn1_load_ptr_as(pval); + if (obj != nullptr) { + return obj; } - - // Bound |len| to comfortably fit in an int. Lengths in this module often - // switch between int and long without overflow checks. - if (len > INT_MAX / 2) { - len = INT_MAX / 2; + obj = new_func(); + if (obj == nullptr) { + return nullptr; } + asn1_store_ptr(pval, obj); + return obj; +} +static int asn1_parse_item_no_error_cleanup(ASN1_VALUE **pval, CBS *cbs, + const ASN1_ITEM *it, + CBS_ASN1_TAG tag, bool opt, + int depth, + const ASN1_TEMPLATE **errtt) { if (++depth > ASN1_MAX_CONSTRUCTED_NEST) { OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_TOO_DEEP); - goto err; + return 0; } switch (it->itype) { case ASN1_ITYPE_PRIMITIVE: if (it->templates) { - // tagging or OPTIONAL is currently illegal on an item template - // because the flags can't get passed down. In practice this - // isn't a problem: we include the relevant flags from the item - // template in the template itself. - if ((tag != -1) || opt) { - OPENSSL_PUT_ERROR(ASN1, ASN1_R_ILLEGAL_OPTIONS_ON_ITEM_TEMPLATE); - goto err; + // This is an `ASN1_ITEM_TEMPLATE`, so the underlying template cannot be + // optional. Optionality should be expressed at the layer above. + if (it->templates->flags & ASN1_TFLG_OPTIONAL) { + OPENSSL_PUT_ERROR(ASN1, ASN1_R_BAD_TEMPLATE); + return 0; } - return asn1_template_ex_d2i(pval, in, len, it->templates, opt, depth); + return asn1_parse_template(pval, cbs, it->templates, tag, opt, depth); } - return asn1_d2i_ex_primitive(pval, in, len, it, tag, aclass, opt); + return asn1_parse_item_primitive(pval, cbs, it, tag, opt, depth); - case ASN1_ITYPE_MSTRING: - // It never makes sense for multi-strings to have implicit tagging, so - // if tag != -1, then this looks like an error in the template. - if (tag != -1) { + case ASN1_ITYPE_MSTRING: { + // Multi-strings are CHOICE types and cannot be implicitly tagged. + if (tag != 0) { OPENSSL_PUT_ERROR(ASN1, ASN1_R_BAD_TEMPLATE); - goto err; - } - - p = *in; - // Just read in tag and class - ret = - asn1_check_tlen(nullptr, &otag, &oclass, nullptr, &p, len, -1, 0, 1); - if (!ret) { - OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR); - goto err; + return 0; } - // Must be UNIVERSAL class - if (oclass != V_ASN1_UNIVERSAL) { - // If OPTIONAL, assume this is OK - if (opt) { - return -1; - } - OPENSSL_PUT_ERROR(ASN1, ASN1_R_MSTRING_NOT_UNIVERSAL); - goto err; - } - // Check tag matches bit map - if (!(ASN1_tag2bit(otag) & it->utype)) { - // If OPTIONAL, assume this is OK + tag = CBS_peek_any_asn1_tag(cbs); + if ((tag & CBS_ASN1_CLASS_MASK) != CBS_ASN1_UNIVERSAL || + (tag & CBS_ASN1_CONSTRUCTED) || + (ASN1_tag2bit(tag & CBS_ASN1_TAG_NUMBER_MASK) & it->utype) == 0) { if (opt) { - return -1; + ASN1_item_ex_free(pval, it); + return 1; // Omitted optional field. } OPENSSL_PUT_ERROR(ASN1, ASN1_R_MSTRING_WRONG_TAG); - goto err; + return 0; } - return asn1_d2i_ex_primitive(pval, in, len, it, otag, 0, 0); + ASN1_STRING *out = ensure_value(pval); + return out != nullptr && asn1_parse_any_as_string(cbs, out); + } case ASN1_ITYPE_EXTERN: { // We don't support implicit tagging with external types. - if (tag != -1) { + if (tag != 0) { OPENSSL_PUT_ERROR(ASN1, ASN1_R_BAD_TEMPLATE); - goto err; - } - const ASN1_EXTERN_FUNCS *ef = - reinterpret_cast(it->funcs); - CBS cbs; - CBS_init(&cbs, *in, len); - CBS copy = cbs; - if (!ef->asn1_ex_parse(pval, &cbs, it, opt)) { - goto err; + return 0; } - *in = CBS_data(&cbs); - // Check whether the function skipped an optional element. - // - // TODO(crbug.com/42290418): Switch the rest of this function to - // |asn1_ex_parse|'s calling convention. - return CBS_len(&cbs) == CBS_len(©) ? -1 : 1; + auto *ef = reinterpret_cast(it->funcs); + return ef->asn1_ex_parse(pval, cbs, it, opt); } case ASN1_ITYPE_CHOICE: { - // It never makes sense for CHOICE types to have implicit tagging, so if - // tag != -1, then this looks like an error in the template. - if (tag != -1) { + // CHOICE types cannot be implicitly tagged. + if (tag != 0) { OPENSSL_PUT_ERROR(ASN1, ASN1_R_BAD_TEMPLATE); - goto err; + return 0; } const ASN1_AUX *aux = reinterpret_cast(it->funcs); ASN1_aux_cb *asn1_cb = aux != nullptr ? aux->asn1_cb : nullptr; if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, nullptr)) { - goto auxerr; + OPENSSL_PUT_ERROR(ASN1, ASN1_R_AUX_ERROR); + return 0; } - if (*pval) { - // Free up and zero CHOICE value if initialised - i = asn1_get_choice_selector(pval, it); - if ((i >= 0) && (i < it->tcount)) { - tt = it->templates + i; - pchptr = asn1_get_field_ptr(pval, tt); + if (asn1_load_ptr(pval)) { + // Free up and zero the CHOICE value if initialised. + int idx = asn1_get_choice_selector(pval, it); + if (idx >= 0 && idx < it->tcount) { + const ASN1_TEMPLATE *tt = &it->templates[idx]; + ASN1_VALUE **pchptr = asn1_get_field_ptr(pval, tt); ASN1_template_free(pchptr, tt); asn1_set_choice_selector(pval, -1, it); } } else if (!ASN1_item_ex_new(pval, it)) { OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR); - goto err; + return 0; } - // CHOICE type, try each possibility in turn - p = *in; - for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) { - pchptr = asn1_get_field_ptr(pval, tt); - // We mark field as OPTIONAL so its absence can be recognised. - ret = asn1_template_ex_d2i(pchptr, &p, len, tt, 1, depth); - // If field not present, try the next one - if (ret == -1) { - continue; + + // Parse each possibility as an optional field and find the first one. + size_t orig_len = CBS_len(cbs); + int idx = 0; + for (idx = 0; idx < it->tcount; idx++) { + const ASN1_TEMPLATE *tt = &it->templates[idx]; + ASN1_VALUE **pchptr = asn1_get_field_ptr(pval, tt); + if (!asn1_parse_template(pchptr, cbs, tt, /*tag=*/0, /*opt=*/true, + depth)) { + // Free any partial object left in `pchptr`. `asn1_item_parse` will + // clean up `pval`, but that will not clean up `pchptr` because the + // choice selector has not yet been set below. + ASN1_template_free(pchptr, tt); + *errtt = tt; + OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR); + return 0; } - // If positive return, read OK, break loop - if (ret > 0) { - break; + if (CBS_len(cbs) != orig_len) { + break; // Found a match. } - // Otherwise must be an ASN1 parsing error - errtt = tt; - OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR); - goto err; } - - // Did we fall off the end without reading anything? - if (i == it->tcount) { - // If OPTIONAL, this is OK + if (idx == it->tcount) { + // Nothing matched. If the CHOICE is OPTIONAL, this is OK. if (opt) { - // Free and zero it ASN1_item_ex_free(pval, it); - return -1; + return 1; } OPENSSL_PUT_ERROR(ASN1, ASN1_R_NO_MATCHING_CHOICE_TYPE); - goto err; + return 0; } - - asn1_set_choice_selector(pval, i, it); + asn1_set_choice_selector(pval, idx, it); if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, nullptr)) { - goto auxerr; + OPENSSL_PUT_ERROR(ASN1, ASN1_R_AUX_ERROR); + return 0; } - *in = p; return 1; } case ASN1_ITYPE_SEQUENCE: { - p = *in; - - // If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL - if (tag == -1) { - tag = V_ASN1_SEQUENCE; - aclass = V_ASN1_UNIVERSAL; + tag = tag == 0 ? CBS_ASN1_SEQUENCE : (tag | CBS_ASN1_CONSTRUCTED); + if (opt && !CBS_peek_asn1_tag(cbs, tag)) { + ASN1_item_ex_free(pval, it); + return 1; } - // Get SEQUENCE length and update len, p - ret = asn1_check_tlen(&len, nullptr, nullptr, &cst, &p, len, tag, aclass, - opt); - if (!ret) { - OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR); - goto err; - } else if (ret == -1) { - return -1; - } - if (!cst) { - OPENSSL_PUT_ERROR(ASN1, ASN1_R_SEQUENCE_NOT_CONSTRUCTED); - goto err; + + CBS copy = *cbs, seq; + if (!CBS_get_asn1(cbs, &seq, tag)) { + OPENSSL_PUT_ERROR(ASN1, ASN1_R_DECODE_ERROR); + return 0; } - if (!*pval && !ASN1_item_ex_new(pval, it)) { + if (!asn1_load_ptr(pval) && !ASN1_item_ex_new(pval, it)) { OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR); - goto err; + return 0; } const ASN1_AUX *aux = reinterpret_cast(it->funcs); ASN1_aux_cb *asn1_cb = aux != nullptr ? aux->asn1_cb : nullptr; if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, nullptr)) { - goto auxerr; + OPENSSL_PUT_ERROR(ASN1, ASN1_R_AUX_ERROR); + return 0; } - // Free up and zero any ADB found - for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) { + // Free up and zero any ADB found. + for (int i = 0; i < it->tcount; i++) { + const ASN1_TEMPLATE *tt = &it->templates[i]; if (tt->flags & ASN1_TFLG_ADB_MASK) { - const ASN1_TEMPLATE *seqtt; - ASN1_VALUE **pseqval; - seqtt = asn1_do_adb(pval, tt, 0); + const ASN1_TEMPLATE *seqtt = asn1_do_adb(pval, tt, 0); if (seqtt == nullptr) { continue; } - pseqval = asn1_get_field_ptr(pval, seqtt); + ASN1_VALUE **pseqval = asn1_get_field_ptr(pval, seqtt); ASN1_template_free(pseqval, seqtt); } } - // Get each field entry - for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) { - const ASN1_TEMPLATE *seqtt; - ASN1_VALUE **pseqval; - seqtt = asn1_do_adb(pval, tt, 1); + // Get each field entry. + for (int i = 0; i < it->tcount; i++) { + const ASN1_TEMPLATE *tt = &it->templates[i]; + const ASN1_TEMPLATE *seqtt = asn1_do_adb(pval, tt, /*nullerr=*/1); if (seqtt == nullptr) { - goto err; - } - pseqval = asn1_get_field_ptr(pval, seqtt); - // Have we ran out of data? - if (!len) { - break; + return 0; } - q = p; - // This determines the OPTIONAL flag value. The field cannot be - // omitted if it is the last of a SEQUENCE and there is still - // data to be read. This isn't strictly necessary but it - // increases efficiency in some cases. - if (i == (it->tcount - 1)) { - isopt = 0; - } else { - isopt = (seqtt->flags & ASN1_TFLG_OPTIONAL) != 0; + ASN1_VALUE **pseqval = asn1_get_field_ptr(pval, seqtt); + const bool field_opt = (tt->flags & ASN1_TFLG_OPTIONAL) != 0; + if (!asn1_parse_template(pseqval, &seq, seqtt, /*tag=*/0, field_opt, + depth)) { + *errtt = tt; + return 0; } - // attempt to read in field, allowing each to be OPTIONAL - - ret = asn1_template_ex_d2i(pseqval, &p, len, seqtt, isopt, depth); - if (!ret) { - errtt = seqtt; - goto err; - } else if (ret == -1) { - // OPTIONAL component absent. Free and zero the field. - ASN1_template_free(pseqval, seqtt); - continue; - } - // Update length - len -= p - q; } - - // Check all data read - if (len) { + if (CBS_len(&seq) != 0) { OPENSSL_PUT_ERROR(ASN1, ASN1_R_SEQUENCE_LENGTH_MISMATCH); - goto err; - } - - // If we get here we've got no more data in the SEQUENCE, however we - // may not have read all fields so check all remaining are OPTIONAL - // and clear any that are. - for (; i < it->tcount; tt++, i++) { - const ASN1_TEMPLATE *seqtt; - seqtt = asn1_do_adb(pval, tt, 1); - if (seqtt == nullptr) { - goto err; - } - if (seqtt->flags & ASN1_TFLG_OPTIONAL) { - ASN1_VALUE **pseqval; - pseqval = asn1_get_field_ptr(pval, seqtt); - ASN1_template_free(pseqval, seqtt); - } else { - errtt = seqtt; - OPENSSL_PUT_ERROR(ASN1, ASN1_R_FIELD_MISSING); - goto err; - } + return 0; } - // Save encoding - if (!asn1_enc_save(pval, *in, p - *in, it)) { - goto auxerr; + // Save a copy of the bytes consumed. + if (!asn1_enc_save(pval, CBS_data(©), CBS_len(©) - CBS_len(cbs), + it)) { + OPENSSL_PUT_ERROR(ASN1, ASN1_R_AUX_ERROR); + return 0; } if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, nullptr)) { - goto auxerr; + OPENSSL_PUT_ERROR(ASN1, ASN1_R_AUX_ERROR); + return 0; } - *in = p; return 1; } default: + OPENSSL_PUT_ERROR(ASN1, ASN1_R_BAD_TEMPLATE); return 0; } -auxerr: - OPENSSL_PUT_ERROR(ASN1, ASN1_R_AUX_ERROR); -err: - ASN1_item_ex_free(pval, it); - if (errtt) { - ERR_add_error_data(4, "Field=", errtt->field_name, ", Type=", it->sname); - } else { - ERR_add_error_data(2, "Type=", it->sname); - } - return 0; -} - -int bssl::ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, - long len, const ASN1_ITEM *it, int tag, int aclass, - char opt) { - return asn1_item_ex_d2i(pval, in, len, it, tag, aclass, opt, /*depth=*/0); } -// Templates are handled with two separate functions. One handles any -// EXPLICIT tag and the other handles the rest. - -static int asn1_template_ex_d2i(ASN1_VALUE **val, const unsigned char **in, - long inlen, const ASN1_TEMPLATE *tt, char opt, - int depth) { - int aclass; - int ret; - long len; - const unsigned char *p, *q; - if (!val) { +static int asn1_parse_item(ASN1_VALUE **pval, CBS *cbs, const ASN1_ITEM *it, + CBS_ASN1_TAG tag, bool opt, int depth) { + const ASN1_TEMPLATE *errtt = nullptr; + if (!asn1_parse_item_no_error_cleanup(pval, cbs, it, tag, opt, depth, + &errtt)) { + ASN1_item_ex_free(pval, it); + if (errtt != nullptr) { + ERR_add_error_data(4, "Field=", errtt->field_name, ", Type=", it->sname); + } else { + ERR_add_error_data(2, "Type=", it->sname); + } return 0; } + return 1; +} + +// Templates are handled with two separate functions. One handles any EXPLICIT +// tag and the other handles the rest. + +static int asn1_parse_template(ASN1_VALUE **pval, CBS *cbs, + const ASN1_TEMPLATE *tt, CBS_ASN1_TAG tag, + bool opt, int depth) { uint32_t flags = tt->flags; - aclass = flags & ASN1_TFLG_TAG_CLASS; - - p = *in; - - // Check if EXPLICIT tag expected - if (flags & ASN1_TFLG_EXPTAG) { - char cst; - // Need to work out amount of data available to the inner content and - // where it starts: so read in EXPLICIT header to get the info. - ret = asn1_check_tlen(&len, nullptr, nullptr, &cst, &p, inlen, tt->tag, - aclass, opt); - q = p; - if (!ret) { - OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR); - return 0; - } else if (ret == -1) { - return -1; - } - if (!cst) { - OPENSSL_PUT_ERROR(ASN1, ASN1_R_EXPLICIT_TAG_NOT_CONSTRUCTED); - return 0; - } - // We've found the field so it can't be OPTIONAL now - ret = asn1_template_noexp_d2i(val, &p, len, tt, /*opt=*/0, depth); - if (!ret) { - OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR); + if ((flags & ASN1_TFLG_EXPTAG) == 0) { + return asn1_parse_template_no_explicit(pval, cbs, tt, tag, opt, depth); + } + + // Determine the tag. + if (tag == 0) { + tag = asn1_tag_to_cbs(flags & ASN1_TFLG_TAG_CLASS, tt->tag); + if (tag == 0) { return 0; } - // We read the field in OK so update length - len -= p - q; - // Check for trailing data. - if (len) { - OPENSSL_PUT_ERROR(ASN1, ASN1_R_EXPLICIT_LENGTH_MISMATCH); - goto err; - } - } else { - return asn1_template_noexp_d2i(val, in, inlen, tt, opt, depth); } + tag |= CBS_ASN1_CONSTRUCTED; - *in = p; - return 1; - -err: - ASN1_template_free(val, tt); - return 0; -} + if (opt && !CBS_peek_asn1_tag(cbs, tag)) { + ASN1_template_free(pval, tt); + return 1; + } -static int asn1_template_noexp_d2i(ASN1_VALUE **val, const unsigned char **in, - long len, const ASN1_TEMPLATE *tt, char opt, - int depth) { - int aclass; - int ret; - const unsigned char *p; - if (!val) { + CBS child; + if (!CBS_get_asn1(cbs, &child, tag)) { + OPENSSL_PUT_ERROR(ASN1, ASN1_R_DECODE_ERROR); return 0; } - uint32_t flags = tt->flags; - aclass = flags & ASN1_TFLG_TAG_CLASS; + if (!asn1_parse_template_no_explicit(pval, &child, tt, /*tag=*/0, + /*opt=*/false, depth)) { + OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR); + return 0; + } + if (CBS_len(&child) != 0) { + OPENSSL_PUT_ERROR(ASN1, ASN1_R_EXPLICIT_LENGTH_MISMATCH); + return 0; + } + return 1; +} - p = *in; +static int asn1_parse_template_no_explicit(ASN1_VALUE **pval, CBS *cbs, + const ASN1_TEMPLATE *tt, + CBS_ASN1_TAG tag, bool opt, + int depth) { + // If `tt` provides implicit tagging and the caller did not already override + // the tag, pick up the tag from the template. + uint32_t flags = tt->flags; + if ((flags & ASN1_TFLG_IMPTAG) && tag == 0) { + tag = asn1_tag_to_cbs(flags & ASN1_TFLG_TAG_CLASS, tt->tag); + if (tag == 0) { + return 0; + } + } if (flags & ASN1_TFLG_SK_MASK) { - // SET OF, SEQUENCE OF - int sktag, skaclass; - // First work out expected inner tag value - if (flags & ASN1_TFLG_IMPTAG) { - sktag = tt->tag; - skaclass = aclass; - } else { - skaclass = V_ASN1_UNIVERSAL; - if (flags & ASN1_TFLG_SET_OF) { - sktag = V_ASN1_SET; - } else { - sktag = V_ASN1_SEQUENCE; - } + if (tag == 0) { + tag = (flags & ASN1_TFLG_SET_OF) ? CBS_ASN1_SET : CBS_ASN1_SEQUENCE; } - // Get the tag - ret = asn1_check_tlen(&len, nullptr, nullptr, nullptr, &p, len, sktag, - skaclass, opt); - if (!ret) { - OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR); - return 0; - } else if (ret == -1) { - return -1; + tag |= CBS_ASN1_CONSTRUCTED; + + if (opt && !CBS_peek_asn1_tag(cbs, tag)) { + ASN1_template_free(pval, tt); + return 1; } - if (!*val) { - *val = (ASN1_VALUE *)sk_ASN1_VALUE_new_null(); - } else { - // We've got a valid STACK: free up any items present - STACK_OF(ASN1_VALUE) *sktmp = (STACK_OF(ASN1_VALUE) *)*val; - ASN1_VALUE *vtmp; - while (sk_ASN1_VALUE_num(sktmp) > 0) { - vtmp = sk_ASN1_VALUE_pop(sktmp); - ASN1_item_ex_free(&vtmp, ASN1_ITEM_ptr(tt->item)); - } + CBS seq; + if (!CBS_get_asn1(cbs, &seq, tag)) { + OPENSSL_PUT_ERROR(ASN1, ASN1_R_DECODE_ERROR); + return 0; } - if (!*val) { - goto err; + STACK_OF(ASN1_VALUE) *sk = + ensure_value(pval); + if (sk == nullptr) { + return 0; } + assert(sk_ASN1_VALUE_num(sk) == 0); - // Read as many items as we can - while (len > 0) { - ASN1_VALUE *skfield; - const unsigned char *q = p; - skfield = nullptr; - if (!asn1_item_ex_d2i(&skfield, &p, len, ASN1_ITEM_ptr(tt->item), - /*tag=*/-1, /*aclass=*/0, /*opt=*/0, depth)) { - ASN1_item_ex_free(&skfield, ASN1_ITEM_ptr(tt->item)); + while (CBS_len(&seq) != 0) { + ASN1_VALUE *skfield = nullptr; + if (!asn1_parse_item(&skfield, &seq, ASN1_ITEM_ptr(tt->item), /*tag=*/0, + /*opt=*/false, depth)) { OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR); - goto err; + ASN1_item_ex_free(&skfield, ASN1_ITEM_ptr(tt->item)); + return 0; } - len -= p - q; - if (!sk_ASN1_VALUE_push((STACK_OF(ASN1_VALUE) *)*val, skfield)) { + if (!sk_ASN1_VALUE_push(sk, skfield)) { ASN1_item_ex_free(&skfield, ASN1_ITEM_ptr(tt->item)); - goto err; + return 0; } } - } else if (flags & ASN1_TFLG_IMPTAG) { - // IMPLICIT tagging - ret = asn1_item_ex_d2i(val, &p, len, ASN1_ITEM_ptr(tt->item), tt->tag, - aclass, opt, depth); - if (!ret) { - OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR); - goto err; - } else if (ret == -1) { - return -1; - } - } else { - // Nothing special - ret = asn1_item_ex_d2i(val, &p, len, ASN1_ITEM_ptr(tt->item), /*tag=*/-1, - /*aclass=*/0, opt, depth); - if (!ret) { - OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR); - goto err; - } else if (ret == -1) { - return -1; - } - } - - *in = p; - return 1; - -err: - ASN1_template_free(val, tt); - return 0; -} - -// TODO(crbug.com/42290418): Switch the whole file to use a CBS-based calling -// convention. -static int asn1_d2i_ex_primitive_cbs(ASN1_VALUE **pval, CBS *cbs, - const ASN1_ITEM *it, int tag, int aclass, - char opt); - -// asn1_d2i_ex_primitive returns one on success, zero on error, and -1 if an -// optional value was skipped. -static int asn1_d2i_ex_primitive(ASN1_VALUE **pval, const unsigned char **in, - long inlen, const ASN1_ITEM *it, int tag, - int aclass, char opt) { - CBS cbs; - CBS_init(&cbs, *in, inlen); - int ret = asn1_d2i_ex_primitive_cbs(pval, &cbs, it, tag, aclass, opt); - if (ret <= 0) { - return ret; + return 1; } - *in = CBS_data(&cbs); - return 1; -} -static ASN1_STRING *ensure_string(ASN1_VALUE **pval) { - if (*pval) { - return (ASN1_STRING *)*pval; - } - ASN1_STRING *str = ASN1_STRING_new(); - if (str == nullptr) { - return nullptr; - } - *pval = (ASN1_VALUE *)str; - return str; + return asn1_parse_item(pval, cbs, ASN1_ITEM_ptr(tt->item), tag, opt, depth); } -static int asn1_d2i_ex_primitive_cbs(ASN1_VALUE **pval, CBS *cbs, - const ASN1_ITEM *it, int tag, int aclass, - char opt) { - // Historically, |it->funcs| for primitive types contained an - // |ASN1_PRIMITIVE_FUNCS| table of callbacks. +static int asn1_parse_item_primitive(ASN1_VALUE **pval, CBS *cbs, + const ASN1_ITEM *it, CBS_ASN1_TAG tag, + bool opt, int depth) { + assert(it->itype == ASN1_ITYPE_PRIMITIVE); + // `ASN1_ITEM_TEMPLATE` should have been handled by the caller. + assert(it->templates == nullptr); + // Historically, `it->funcs` for primitive types contained an + // `ASN1_PRIMITIVE_FUNCS` table of callbacks. assert(it->funcs == nullptr); - - int utype; - assert(it->itype == ASN1_ITYPE_PRIMITIVE || it->itype == ASN1_ITYPE_MSTRING); - if (it->itype == ASN1_ITYPE_MSTRING) { - // MSTRING passes utype in |tag|, normally used for implicit tagging. - utype = tag; - tag = -1; - } else { - utype = it->utype; - } + int utype = it->utype; // Handle ANY types. if (utype == V_ASN1_ANY) { - if (tag >= 0) { + if (tag != 0) { OPENSSL_PUT_ERROR(ASN1, ASN1_R_ILLEGAL_TAGGED_ANY); return 0; } if (opt && CBS_len(cbs) == 0) { - return -1; // Omitted OPTIONAL value. + ASN1_item_ex_free(pval, it); + return 1; // Omitted OPTIONAL value. } - ASN1_TYPE *typ; - if (!*pval) { - typ = ASN1_TYPE_new(); - if (typ == nullptr) { - return 0; - } - *pval = (ASN1_VALUE *)typ; - } else { - typ = (ASN1_TYPE *)*pval; + ASN1_TYPE *typ = ensure_value(pval); + if (typ == nullptr) { + return 0; } return asn1_parse_any(cbs, typ); } - // Convert the crypto/asn1 tag into a CBS one. - if (tag == -1) { - tag = utype; - aclass = V_ASN1_UNIVERSAL; + // Determine the ASN.1 tag. `utype` must be a primitive `ASN1_ITEM`, handled + // by `DECLARE_ASN1_ITEM`. + assert(0 <= utype && utype <= int{CBS_ASN1_TAG_NUMBER_MASK}); + if (tag == 0) { + tag = static_cast(utype); } - - // All edge cases of |utype| should have been handled already. |utype| is now - // either a primitive |ASN1_ITEM|, handled by |DECLARE_ASN1_ITEM|, or a - // multistring option with a corresponding |B_ASN1_*| constant. - assert(utype >= 0 && utype <= V_ASN1_MAX_UNIVERSAL); - CBS_ASN1_TAG cbs_tag = - (static_cast(aclass) << CBS_ASN1_TAG_SHIFT) | - static_cast(tag); if (utype == V_ASN1_SEQUENCE || utype == V_ASN1_SET) { - cbs_tag |= CBS_ASN1_CONSTRUCTED; + tag |= CBS_ASN1_CONSTRUCTED; } - if (opt && !CBS_peek_asn1_tag(cbs, cbs_tag)) { - return -1; // Omitted OPTIONAL value. + if (opt && !CBS_peek_asn1_tag(cbs, tag)) { + ASN1_item_ex_free(pval, it); + return 1; // Omitted OPTIONAL value. } - // Handle non-|ASN1_STRING| types. + // Handle non-`ASN1_STRING` types. switch (utype) { case V_ASN1_OBJECT: { - UniquePtr obj(asn1_parse_object(cbs, cbs_tag)); + UniquePtr obj(asn1_parse_object(cbs, tag)); if (obj == nullptr) { return 0; } - ASN1_OBJECT_free((ASN1_OBJECT *)*pval); - *pval = (ASN1_VALUE *)obj.release(); + ASN1_OBJECT_free(asn1_load_ptr_as(pval)); + asn1_store_ptr(pval, obj.release()); return 1; } case V_ASN1_NULL: { CBS null; - if (!CBS_get_asn1(cbs, &null, cbs_tag)) { + if (!CBS_get_asn1(cbs, &null, tag)) { OPENSSL_PUT_ERROR(ASN1, ASN1_R_DECODE_ERROR); return 0; } @@ -718,78 +533,68 @@ static int asn1_d2i_ex_primitive_cbs(ASN1_VALUE **pval, CBS *cbs, OPENSSL_PUT_ERROR(ASN1, ASN1_R_NULL_IS_WRONG_LENGTH); return 0; } - *pval = (ASN1_VALUE *)1; + asn1_store_ptr(pval, reinterpret_cast(1)); return 1; } case V_ASN1_BOOLEAN: { CBS child; - if (!CBS_get_asn1(cbs, &child, cbs_tag)) { + if (!CBS_get_asn1(cbs, &child, tag)) { OPENSSL_PUT_ERROR(ASN1, ASN1_R_DECODE_ERROR); return 0; } // TODO(crbug.com/42290221): Reject invalid BOOLEAN encodings and just - // call |CBS_get_asn1_bool|. + // call `CBS_get_asn1_bool`. if (CBS_len(&child) != 1) { OPENSSL_PUT_ERROR(ASN1, ASN1_R_BOOLEAN_IS_WRONG_LENGTH); return 0; } - ASN1_BOOLEAN *tbool; - tbool = (ASN1_BOOLEAN *)pval; - *tbool = CBS_data(&child)[0]; + *reinterpret_cast(pval) = + CBS_data(&child)[0] ? ASN1_BOOLEAN_TRUE : ASN1_BOOLEAN_FALSE; return 1; } } - // All other types as an |ASN1_STRING| representation. - ASN1_STRING *str = ensure_string(pval); + // All other types as an `ASN1_STRING` representation. + ASN1_STRING *str = ensure_value(pval); if (str == nullptr) { return 0; } switch (utype) { case V_ASN1_BIT_STRING: - return asn1_parse_bit_string(cbs, str, cbs_tag); + return asn1_parse_bit_string(cbs, str, tag); case V_ASN1_INTEGER: - return asn1_parse_integer(cbs, str, cbs_tag); + return asn1_parse_integer(cbs, str, tag); case V_ASN1_ENUMERATED: - return asn1_parse_enumerated(cbs, str, cbs_tag); + return asn1_parse_enumerated(cbs, str, tag); case V_ASN1_UNIVERSALSTRING: - return asn1_parse_universal_string(cbs, str, cbs_tag); + return asn1_parse_universal_string(cbs, str, tag); case V_ASN1_BMPSTRING: - return asn1_parse_bmp_string(cbs, str, cbs_tag); + return asn1_parse_bmp_string(cbs, str, tag); case V_ASN1_UTF8STRING: - return asn1_parse_utf8_string(cbs, str, cbs_tag); + return asn1_parse_utf8_string(cbs, str, tag); case V_ASN1_UTCTIME: - // TODO(crbug.com/42290221): Reject timezone offsets. We need to parse - // invalid timestamps in |X509| objects, but that parser no longer uses - // this code. - return asn1_parse_utc_time(cbs, str, cbs_tag, - /*allow_timezone_offset=*/1); + return asn1_parse_utc_time(cbs, str, tag, /*allow_timezone_offset=*/0); case V_ASN1_GENERALIZEDTIME: - return asn1_parse_generalized_time(cbs, str, cbs_tag); + return asn1_parse_generalized_time(cbs, str, tag); case V_ASN1_OCTET_STRING: + return asn1_parse_octet_string(cbs, str, tag); + case V_ASN1_T61STRING: + return asn1_parse_t61_string(cbs, str, tag); case V_ASN1_NUMERICSTRING: case V_ASN1_PRINTABLESTRING: - case V_ASN1_T61STRING: case V_ASN1_VIDEOTEXSTRING: case V_ASN1_IA5STRING: case V_ASN1_GRAPHICSTRING: case V_ASN1_VISIBLESTRING: case V_ASN1_GENERALSTRING: - // T61String is parsed as Latin-1, so all byte strings are valid. The - // others we currently do not enforce. - // // TODO(crbug.com/42290290): Enforce the encoding of the other string // types. - if (!asn1_parse_octet_string(cbs, str, cbs_tag)) { - return 0; - } - str->type = utype; - return 1; + return asn1_parse_string_unchecked(cbs, str, utype, tag); case V_ASN1_SEQUENCE: { // Save the entire element in the string. CBS elem; - if (!CBS_get_asn1_element(cbs, &elem, cbs_tag)) { + if (!CBS_get_asn1_element(cbs, &elem, tag)) { OPENSSL_PUT_ERROR(ASN1, ASN1_R_DECODE_ERROR); return 0; } @@ -799,53 +604,5 @@ static int asn1_d2i_ex_primitive_cbs(ASN1_VALUE **pval, CBS *cbs, default: OPENSSL_PUT_ERROR(ASN1, ASN1_R_BAD_TEMPLATE); return 0; - } -} - -// Check an ASN1 tag and length: a bit like ASN1_get_object but it -// checks the expected tag. - -static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass, - char *cst, const unsigned char **in, long len, - int exptag, int expclass, char opt) { - int i; - int ptag, pclass; - long plen; - const unsigned char *p; - p = *in; - - i = ASN1_get_object(&p, &plen, &ptag, &pclass, len); - if (i & 0x80) { - OPENSSL_PUT_ERROR(ASN1, ASN1_R_BAD_OBJECT_HEADER); - return 0; - } - if (exptag >= 0) { - if ((exptag != ptag) || (expclass != pclass)) { - // If type is OPTIONAL, not an error: indicate missing type. - if (opt) { - return -1; - } - OPENSSL_PUT_ERROR(ASN1, ASN1_R_WRONG_TAG); - return 0; - } } - - if (cst) { - *cst = i & V_ASN1_CONSTRUCTED; - } - - if (olen) { - *olen = plen; - } - - if (oclass) { - *oclass = pclass; - } - - if (otag) { - *otag = ptag; - } - - *in = p; - return 1; } diff --git a/third_party/boringssl/src/crypto/asn1/tasn_enc.cc b/third_party/boringssl/src/crypto/asn1/tasn_enc.cc index 1d1ad97f3..ae83044aa 100644 --- a/third_party/boringssl/src/crypto/asn1/tasn_enc.cc +++ b/third_party/boringssl/src/crypto/asn1/tasn_enc.cc @@ -21,614 +21,306 @@ #include #include +#include "../bytestring/internal.h" #include "../internal.h" #include "internal.h" using namespace bssl; -static int asn1_item_ex_i2d_opt(ASN1_VALUE **pval, unsigned char **out, - const ASN1_ITEM *it, int tag, int aclass, - int optional); -static int asn1_i2d_ex_primitive(ASN1_VALUE **pval, unsigned char **out, - const ASN1_ITEM *it, int tag, int aclass, - int optional); -static int asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cont, int *out_omit, - int *putype, const ASN1_ITEM *it); -static int asn1_set_seq_out(STACK_OF(ASN1_VALUE) *sk, unsigned char **out, - int skcontlen, const ASN1_ITEM *item, int do_sort); -static int asn1_template_ex_i2d(ASN1_VALUE **pval, unsigned char **out, - const ASN1_TEMPLATE *tt, int tag, int aclass, - int optional); - -// Top level i2d equivalents +static int asn1_marshal_item(CBB *cbb, ASN1_VALUE **pval, const ASN1_ITEM *it, + CBS_ASN1_TAG tag); +static int asn1_marshal_template(CBB *cbb, ASN1_VALUE **pval, + const ASN1_TEMPLATE *tt, CBS_ASN1_TAG tag); +static int asn1_marshal_template_no_explicit(CBB *cbb, ASN1_VALUE **pval, + const ASN1_TEMPLATE *tt, + CBS_ASN1_TAG tag); +static int asn1_marshal_primitive(CBB *cbb, ASN1_VALUE **pval, + const ASN1_ITEM *it, CBS_ASN1_TAG tag); + +static bool asn1_item_has_value(ASN1_VALUE **pval, const ASN1_ITEM *it); +static bool asn1_template_has_value(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt); + +int ASN1_item_i2d(ASN1_VALUE *val, uint8_t **out, const ASN1_ITEM *it) { + return I2DFromCBB(/*initial_capacity=*/64, out, [&](CBB *cbb) -> bool { + return asn1_marshal_item(cbb, &val, it, /*tag=*/0); + }); +} -int ASN1_item_i2d(ASN1_VALUE *val, unsigned char **out, const ASN1_ITEM *it) { - if (out && !*out) { - unsigned char *p, *buf; - int len = ASN1_item_ex_i2d(&val, nullptr, it, /*tag=*/-1, /*aclass=*/0); - if (len <= 0) { - return len; - } - buf = reinterpret_cast(OPENSSL_malloc(len)); - if (!buf) { - return -1; - } - p = buf; - int len2 = ASN1_item_ex_i2d(&val, &p, it, /*tag=*/-1, /*aclass=*/0); - if (len2 <= 0) { - OPENSSL_free(buf); - return len2; - } - assert(len == len2); - *out = buf; - return len; +// asn1_item_has_value returns whether `pval`, which must have type `it`, +// contains a value. +bool asn1_item_has_value(ASN1_VALUE **pval, const ASN1_ITEM *it) { + // Almost every `ASN1_VALUE` is a pointer, except for types represented as + // `ASN1_BOOLEAN`. + if (it->itype != ASN1_ITYPE_PRIMITIVE) { + // All non-primitive types are pointers. + return asn1_load_ptr(pval) != nullptr; } - - return ASN1_item_ex_i2d(&val, out, it, /*tag=*/-1, /*aclass=*/0); + if (it->templates != nullptr) { + // This is an `ASN1_ITEM_TEMPLATE`. Recurse into the template. + return asn1_template_has_value(pval, it->templates); + } + if (it->utype != V_ASN1_BOOLEAN) { + return asn1_load_ptr(pval) != nullptr; + } + // `ASN1_BOOLEAN` may be omitted in two ways. -1 indicates an omitted + // OPTIONAL BOOLEAN. Additionally, if `it` is `ASN1_FBOOLEAN` or + // `ASN1_TBOOLEAN`, this is DEFAULT FALSE and DEFAULT TRUE, respectively. This + // is stored in `it->size`. + ASN1_BOOLEAN b = *reinterpret_cast(pval); + if (b == ASN1_BOOLEAN_NONE) { + return false; + } + if (it->size > 0) { + // This is DEFAULT TRUE, so only FALSE is present. + return b == ASN1_BOOLEAN_FALSE; + } + if (it->size == 0) { + // This is DEFAULT FALSE, so only TRUE is present. + return b != ASN1_BOOLEAN_FALSE; + } + return true; } -// Encode an item, taking care of IMPLICIT tagging (if any). This function -// performs the normal item handling: it can be used in external types. - -int bssl::ASN1_item_ex_i2d(ASN1_VALUE **pval, unsigned char **out, - const ASN1_ITEM *it, int tag, int aclass) { - int ret = asn1_item_ex_i2d_opt(pval, out, it, tag, aclass, /*optional=*/0); - assert(ret != 0); - return ret; +// asn1_template_has_value returns whether `pval`, which must have type `tt`, +// contains a value. +bool asn1_template_has_value(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt) { + if (tt->flags & ASN1_TFLG_SK_MASK) { + // If `tt` is a SEQUENCE OF or SET OF type, the C type is `STACK_OF(T)` and + // is a pointer. + return asn1_load_ptr(pval) != nullptr; + } + // Otherwise, `tt`'s representation is the underlying item. + return asn1_item_has_value(pval, ASN1_ITEM_ptr(tt->item)); } -// asn1_item_ex_i2d_opt behaves like |ASN1_item_ex_i2d| but, if |optional| is -// non-zero and |*pval| is omitted, it returns zero and writes no bytes. -int asn1_item_ex_i2d_opt(ASN1_VALUE **pval, unsigned char **out, - const ASN1_ITEM *it, int tag, int aclass, - int optional) { - const ASN1_TEMPLATE *tt = nullptr; - int i, seqcontlen, seqlen; - - // Historically, |aclass| was repurposed to pass additional flags into the - // encoding process. - assert((aclass & ASN1_TFLG_TAG_CLASS) == aclass); - // If not overriding the tag, |aclass| is ignored and should be zero. - assert(tag != -1 || aclass == 0); - - // All fields are pointers, except for boolean |ASN1_ITYPE_PRIMITIVE|s. - // Optional primitives are handled later. - if ((it->itype != ASN1_ITYPE_PRIMITIVE) && !*pval) { - if (optional) { - return 0; - } +// asn1_marshal_item marshals `pval`, of type `it`, and writes the result to +// `cbb`. It returns one on success and zero on error. If `tag` is non-zero, the +// value is implicitly tagged with `tag`. +static int asn1_marshal_item(CBB *cbb, ASN1_VALUE **pval, const ASN1_ITEM *it, + CBS_ASN1_TAG tag) { + // All fields are pointers, except for boolean `ASN1_ITYPE_PRIMITIVE`s. Those + // cases will be checked later. + if ((it->itype != ASN1_ITYPE_PRIMITIVE) && !asn1_load_ptr(pval)) { OPENSSL_PUT_ERROR(ASN1, ASN1_R_MISSING_VALUE); - return -1; + return 0; } switch (it->itype) { case ASN1_ITYPE_PRIMITIVE: if (it->templates) { - // This is an |ASN1_ITEM_TEMPLATE|. + // This is an `ASN1_ITEM_TEMPLATE`, so the underlying template cannot be + // optional. Optionality should be expressed at the layer above. if (it->templates->flags & ASN1_TFLG_OPTIONAL) { OPENSSL_PUT_ERROR(ASN1, ASN1_R_BAD_TEMPLATE); - return -1; + return 0; } - return asn1_template_ex_i2d(pval, out, it->templates, tag, aclass, - optional); + return asn1_marshal_template(cbb, pval, it->templates, tag); } - return asn1_i2d_ex_primitive(pval, out, it, tag, aclass, optional); + return asn1_marshal_primitive(cbb, pval, it, tag); case ASN1_ITYPE_MSTRING: - // It never makes sense for multi-strings to have implicit tagging, so - // if tag != -1, then this looks like an error in the template. - if (tag != -1) { + // CHOICE types cannot be implicitly tagged. + if (tag != 0) { OPENSSL_PUT_ERROR(ASN1, ASN1_R_BAD_TEMPLATE); - return -1; + return 0; } - return asn1_i2d_ex_primitive(pval, out, it, -1, 0, optional); + return asn1_marshal_any_string(cbb, asn1_load_ptr_as(pval)); case ASN1_ITYPE_CHOICE: { - // It never makes sense for CHOICE types to have implicit tagging, so if - // tag != -1, then this looks like an error in the template. - if (tag != -1) { + // CHOICE types cannot be implicitly tagged. + if (tag != 0) { OPENSSL_PUT_ERROR(ASN1, ASN1_R_BAD_TEMPLATE); - return -1; + return 0; } - i = asn1_get_choice_selector(pval, it); + int i = asn1_get_choice_selector(pval, it); if (i < 0 || i >= it->tcount) { OPENSSL_PUT_ERROR(ASN1, ASN1_R_NO_MATCHING_CHOICE_TYPE); - return -1; + return 0; } - const ASN1_TEMPLATE *chtt = it->templates + i; + const ASN1_TEMPLATE *chtt = &it->templates[i]; if (chtt->flags & ASN1_TFLG_OPTIONAL) { OPENSSL_PUT_ERROR(ASN1, ASN1_R_BAD_TEMPLATE); - return -1; + return 0; } ASN1_VALUE **pchval = asn1_get_field_ptr(pval, chtt); - return asn1_template_ex_i2d(pchval, out, chtt, -1, 0, /*optional=*/0); + return asn1_marshal_template(cbb, pchval, chtt, /*tag=*/0); } case ASN1_ITYPE_EXTERN: { // We don't support implicit tagging with external types. - if (tag != -1) { + if (tag != 0) { OPENSSL_PUT_ERROR(ASN1, ASN1_R_BAD_TEMPLATE); - return -1; - } - const ASN1_EXTERN_FUNCS *ef = - reinterpret_cast(it->funcs); - int ret = ef->asn1_ex_i2d(pval, out, it); - if (ret == 0) { - // |asn1_ex_i2d| should never return zero. We have already checked - // for optional values generically, and |ASN1_ITYPE_EXTERN| fields - // must be pointers. - OPENSSL_PUT_ERROR(ASN1, ERR_R_INTERNAL_ERROR); - return -1; + return 0; } - return ret; + auto *ef = static_cast(it->funcs); + return ef->asn1_ex_marshal(cbb, pval, it); } case ASN1_ITYPE_SEQUENCE: { - i = asn1_enc_restore(&seqcontlen, out, pval, it); - // An error occurred - if (i < 0) { - return -1; + Span enc; + if (asn1_enc_restore(&enc, pval, it)) { + return CBB_add_bytes(cbb, enc.data(), enc.size()); } - // We have a valid cached encoding... - if (i > 0) { - return seqcontlen; - } - // Otherwise carry on - seqcontlen = 0; - // If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL - if (tag == -1) { - tag = V_ASN1_SEQUENCE; - aclass = V_ASN1_UNIVERSAL; + tag = tag == 0 ? CBS_ASN1_SEQUENCE : (tag | CBS_ASN1_CONSTRUCTED); + CBB seq; + if (!CBB_add_asn1(cbb, &seq, tag)) { + return 0; } - // First work out sequence content length - for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) { - const ASN1_TEMPLATE *seqtt; - ASN1_VALUE **pseqval; - int tmplen; - seqtt = asn1_do_adb(pval, tt, 1); + for (int i = 0; i < it->tcount; i++) { + const ASN1_TEMPLATE *tt = &it->templates[i]; + const ASN1_TEMPLATE *seqtt = asn1_do_adb(pval, tt, /*nullerr=*/1); if (!seqtt) { - return -1; - } - pseqval = asn1_get_field_ptr(pval, seqtt); - tmplen = asn1_template_ex_i2d(pseqval, nullptr, seqtt, -1, 0, - /*optional=*/0); - if (tmplen == -1 || (tmplen > INT_MAX - seqcontlen)) { - return -1; + return 0; } - seqcontlen += tmplen; - } - - seqlen = ASN1_object_size(/*constructed=*/1, seqcontlen, tag); - if (!out || seqlen == -1) { - return seqlen; - } - // Output SEQUENCE header - ASN1_put_object(out, /*constructed=*/1, seqcontlen, tag, aclass); - for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) { - const ASN1_TEMPLATE *seqtt; - ASN1_VALUE **pseqval; - seqtt = asn1_do_adb(pval, tt, 1); - if (!seqtt) { - return -1; + ASN1_VALUE **pseqval = asn1_get_field_ptr(pval, seqtt); + if (!asn1_template_has_value(pseqval, seqtt)) { + if ((seqtt->flags & ASN1_TFLG_OPTIONAL) != 0) { + continue; + } + OPENSSL_PUT_ERROR(ASN1, ASN1_R_MISSING_VALUE); + return 0; } - pseqval = asn1_get_field_ptr(pval, seqtt); - if (asn1_template_ex_i2d(pseqval, out, seqtt, -1, 0, /*optional=*/0) < - 0) { - return -1; + if (!asn1_marshal_template(&seq, pseqval, seqtt, /*tag=*/0)) { + return 0; } } - return seqlen; + return CBB_flush(cbb); } default: OPENSSL_PUT_ERROR(ASN1, ASN1_R_BAD_TEMPLATE); - return -1; + return 0; } } -// asn1_template_ex_i2d behaves like |asn1_item_ex_i2d_opt| but uses an -// |ASN1_TEMPLATE| instead of an |ASN1_ITEM|. An |ASN1_TEMPLATE| wraps an -// |ASN1_ITEM| with modifiers such as tagging, SEQUENCE or SET, etc. -static int asn1_template_ex_i2d(ASN1_VALUE **pval, unsigned char **out, - const ASN1_TEMPLATE *tt, int tag, int iclass, - int optional) { - int i, ret, ttag, tclass; - size_t j; +// asn1_marshal_template marshals `pval`, of type `tt`, and writes the result to +// `cbb`. It returns one on success and zero on error. If `tag` is non-zero, the +// value is implicitly tagged with `tag`. +// +// This function does not look at `ASN1_TFLG_OPTIONAL`. The caller is expected +// to have evaluated it already and, if applicable, skip this function. +static int asn1_marshal_template(CBB *cbb, ASN1_VALUE **pval, + const ASN1_TEMPLATE *tt, CBS_ASN1_TAG tag) { uint32_t flags = tt->flags; + if (!(flags & ASN1_TFLG_EXPTAG)) { + return asn1_marshal_template_no_explicit(cbb, pval, tt, tag); + } - // Historically, |iclass| was repurposed to pass additional flags into the - // encoding process. - assert((iclass & ASN1_TFLG_TAG_CLASS) == iclass); - // If not overriding the tag, |iclass| is ignored and should be zero. - assert(tag != -1 || iclass == 0); - - // Work out tag and class to use: tagging may come either from the - // template or the arguments, not both because this would create - // ambiguity. - if (flags & ASN1_TFLG_TAG_MASK) { - // Error if argument and template tagging - if (tag != -1) { - OPENSSL_PUT_ERROR(ASN1, ASN1_R_BAD_TEMPLATE); - return -1; + // Determine the explicit tag to use. If the caller supplied an implicit tag, + // it overrides this one. Otherwise, use the tag in the template. + if (tag == 0) { + tag = asn1_tag_to_cbs(flags & ASN1_TFLG_TAG_CLASS, tt->tag); + if (tag == 0) { + return 0; } - // Get tagging from template - ttag = tt->tag; - tclass = flags & ASN1_TFLG_TAG_CLASS; - } else if (tag != -1) { - // No template tagging, get from arguments - ttag = tag; - tclass = iclass & ASN1_TFLG_TAG_CLASS; - } else { - ttag = -1; - tclass = 0; } - // The template may itself by marked as optional, or this may be the template - // of an |ASN1_ITEM_TEMPLATE| type which was contained inside an outer - // optional template. (They cannot both be true because the - // |ASN1_ITEM_TEMPLATE| codepath rejects optional templates.) - assert(!optional || (flags & ASN1_TFLG_OPTIONAL) == 0); - optional = optional || (flags & ASN1_TFLG_OPTIONAL) != 0; + CBB child; + return CBB_add_asn1(cbb, &child, tag | CBS_ASN1_CONSTRUCTED) && + asn1_marshal_template_no_explicit(&child, pval, tt, /*tag=*/0) && + CBB_flush(cbb); +} - // At this point 'ttag' contains the outer tag to use, and 'tclass' is the - // class. +// asn1_marshal_template_no_explicit behaves like `asn1_marshal_template` except +// it ignores any explicit tagging specified in `tt`. +static int asn1_marshal_template_no_explicit(CBB *cbb, ASN1_VALUE **pval, + const ASN1_TEMPLATE *tt, + CBS_ASN1_TAG tag) { + uint32_t flags = tt->flags; - if (flags & ASN1_TFLG_SK_MASK) { - // SET OF, SEQUENCE OF - STACK_OF(ASN1_VALUE) *sk = (STACK_OF(ASN1_VALUE) *)*pval; - int isset, sktag, skaclass; - int skcontlen, sklen; - ASN1_VALUE *skitem; + // If `tt` provides implicit tagging and the caller did not already override + // the tag, pick up the tag from the template. + if ((flags & ASN1_TFLG_IMPTAG) && tag == 0) { + tag = asn1_tag_to_cbs(flags & ASN1_TFLG_TAG_CLASS, tt->tag); + if (tag == 0) { + return 0; + } + } - if (!*pval) { - if (optional) { - return 0; - } + if (flags & ASN1_TFLG_SK_MASK) { + // This is a SET OF or SEQUENCE OF type. + STACK_OF(ASN1_VALUE) *sk = asn1_load_ptr_as(pval); + if (sk == nullptr) { OPENSSL_PUT_ERROR(ASN1, ASN1_R_MISSING_VALUE); - return -1; + return 0; } - - if (flags & ASN1_TFLG_SET_OF) { - isset = 1; - // Historically, types with both bits set were mutated when - // serialized to apply the sort. We no longer support this. + bool is_set = (flags & ASN1_TFLG_SET_OF) != 0; + if (is_set) { + // Historically, types with both bits set were mutated when serialized to + // apply the sort. We no longer support this. assert((flags & ASN1_TFLG_SEQUENCE_OF) == 0); - } else { - isset = 0; } - - // Work out inner tag value: if EXPLICIT or no tagging use underlying - // type. - if ((ttag != -1) && !(flags & ASN1_TFLG_EXPTAG)) { - sktag = ttag; - skaclass = tclass; - } else { - skaclass = V_ASN1_UNIVERSAL; - if (isset) { - sktag = V_ASN1_SET; - } else { - sktag = V_ASN1_SEQUENCE; - } - } - - // Determine total length of items - skcontlen = 0; - for (j = 0; j < sk_ASN1_VALUE_num(sk); j++) { - int tmplen; - skitem = sk_ASN1_VALUE_value(sk, j); - tmplen = - ASN1_item_ex_i2d(&skitem, nullptr, ASN1_ITEM_ptr(tt->item), -1, 0); - if (tmplen == -1 || (skcontlen > INT_MAX - tmplen)) { - return -1; - } - skcontlen += tmplen; - } - sklen = ASN1_object_size(/*constructed=*/1, skcontlen, sktag); - if (sklen == -1) { - return -1; - } - // If EXPLICIT need length of surrounding tag - if (flags & ASN1_TFLG_EXPTAG) { - ret = ASN1_object_size(/*constructed=*/1, sklen, ttag); - } else { - ret = sklen; - } - - if (!out || ret == -1) { - return ret; - } - - // Now encode this lot... - // EXPLICIT tag - if (flags & ASN1_TFLG_EXPTAG) { - ASN1_put_object(out, /*constructed=*/1, sklen, ttag, tclass); - } - // SET or SEQUENCE and IMPLICIT tag - ASN1_put_object(out, /*constructed=*/1, skcontlen, sktag, skaclass); - // And the stuff itself - if (!asn1_set_seq_out(sk, out, skcontlen, ASN1_ITEM_ptr(tt->item), isset)) { - return -1; + if (tag == 0) { + tag = is_set ? CBS_ASN1_SET : CBS_ASN1_SEQUENCE; } - return ret; - } - - if (flags & ASN1_TFLG_EXPTAG) { - // EXPLICIT tagging - // Find length of tagged item - i = asn1_item_ex_i2d_opt(pval, nullptr, ASN1_ITEM_ptr(tt->item), -1, 0, - optional); - if (i <= 0) { - return i; - } - // Find length of EXPLICIT tag - ret = ASN1_object_size(/*constructed=*/1, i, ttag); - if (out && ret != -1) { - // Output tag and item - ASN1_put_object(out, /*constructed=*/1, i, ttag, tclass); - if (ASN1_item_ex_i2d(pval, out, ASN1_ITEM_ptr(tt->item), -1, 0) < 0) { - return -1; - } + CBB child; + if (!CBB_add_asn1(cbb, &child, tag | CBS_ASN1_CONSTRUCTED)) { + return 0; } - return ret; - } - - // Either normal or IMPLICIT tagging - return asn1_item_ex_i2d_opt(pval, out, ASN1_ITEM_ptr(tt->item), ttag, tclass, - optional); -} - -// Temporary structure used to hold DER encoding of items for SET OF - -typedef struct { - unsigned char *data; - int length; -} DER_ENC; - -static int der_cmp(const void *a, const void *b) { - const DER_ENC *d1 = reinterpret_cast(a), - *d2 = reinterpret_cast(b); - int cmplen, i; - cmplen = (d1->length < d2->length) ? d1->length : d2->length; - i = OPENSSL_memcmp(d1->data, d2->data, cmplen); - if (i) { - return i; - } - return d1->length - d2->length; -} - -// asn1_set_seq_out writes |sk| to |out| under the i2d output convention, -// excluding the tag and length. It returns one on success and zero on error. -// |skcontlen| must be the total encoded size. If |do_sort| is non-zero, the -// elements are sorted for a SET OF type. Each element of |sk| has type -// |item|. -static int asn1_set_seq_out(STACK_OF(ASN1_VALUE) *sk, unsigned char **out, - int skcontlen, const ASN1_ITEM *item, int do_sort) { - // No need to sort if there are fewer than two items. - if (!do_sort || sk_ASN1_VALUE_num(sk) < 2) { for (size_t i = 0; i < sk_ASN1_VALUE_num(sk); i++) { - ASN1_VALUE *skitem = sk_ASN1_VALUE_value(sk, i); - if (ASN1_item_ex_i2d(&skitem, out, item, -1, 0) < 0) { + ASN1_VALUE *elem = sk_ASN1_VALUE_value(sk, i); + if (!asn1_marshal_item(&child, &elem, ASN1_ITEM_ptr(tt->item), + /*tag=*/0)) { return 0; } } - return 1; - } - - int ret = 0; - uint8_t *const buf = reinterpret_cast(OPENSSL_malloc(skcontlen)); - DER_ENC *encoded = reinterpret_cast( - OPENSSL_calloc(sk_ASN1_VALUE_num(sk), sizeof(*encoded))); - uint8_t *p = buf; - if (encoded == nullptr || buf == nullptr) { - goto err; - } - - // Encode all the elements into |buf| and populate |encoded|. - for (size_t i = 0; i < sk_ASN1_VALUE_num(sk); i++) { - ASN1_VALUE *skitem = sk_ASN1_VALUE_value(sk, i); - encoded[i].data = p; - encoded[i].length = ASN1_item_ex_i2d(&skitem, &p, item, -1, 0); - if (encoded[i].length < 0) { - goto err; - } - assert(p - buf <= skcontlen); - } - - qsort(encoded, sk_ASN1_VALUE_num(sk), sizeof(*encoded), der_cmp); - - // Output the elements in sorted order. - p = *out; - for (size_t i = 0; i < sk_ASN1_VALUE_num(sk); i++) { - OPENSSL_memcpy(p, encoded[i].data, encoded[i].length); - p += encoded[i].length; - } - *out = p; - - ret = 1; - -err: - OPENSSL_free(encoded); - OPENSSL_free(buf); - return ret; -} - -// asn1_i2d_ex_primitive behaves like |ASN1_item_ex_i2d| but |item| must be a -// a PRIMITIVE or MSTRING type that is not an |ASN1_ITEM_TEMPLATE|. -static int asn1_i2d_ex_primitive(ASN1_VALUE **pval, unsigned char **out, - const ASN1_ITEM *it, int tag, int aclass, - int optional) { - // Get length of content octets and maybe find out the underlying type. - int omit; - int utype = it->utype; - int len = asn1_ex_i2c(pval, nullptr, &omit, &utype, it); - if (len < 0) { - return -1; - } - if (omit) { - if (optional) { - return 0; - } - OPENSSL_PUT_ERROR(ASN1, ASN1_R_MISSING_VALUE); - return -1; - } - - // If SEQUENCE, SET or OTHER then header is included in pseudo content - // octets so don't include tag+length. We need to check here because the - // call to asn1_ex_i2c() could change utype. - int usetag = - utype != V_ASN1_SEQUENCE && utype != V_ASN1_SET && utype != V_ASN1_OTHER; - - // If not implicitly tagged get tag from underlying type - if (tag == -1) { - tag = utype; - } - - // Output tag+length followed by content octets - if (out) { - if (usetag) { - ASN1_put_object(out, /*constructed=*/0, len, tag, aclass); - } - int len2 = asn1_ex_i2c(pval, *out, &omit, &utype, it); - if (len2 < 0) { - return -1; + // If this is a SET OF type, sort the encodings when flushing. + if (is_set) { + return CBB_flush_asn1_set_of(&child) && CBB_flush(cbb); } - assert(len == len2); - assert(!omit); - *out += len; + return CBB_flush(cbb); } - if (usetag) { - return ASN1_object_size(/*constructed=*/0, len, tag); - } - return len; + return asn1_marshal_item(cbb, pval, ASN1_ITEM_ptr(tt->item), tag); } -// asn1_ex_i2c writes the |*pval| to |cout| under the i2d output convention, -// excluding the tag and length. It returns the number of bytes written, -// possibly zero, on success or -1 on error. If |*pval| should be omitted, it -// returns zero and sets |*out_omit| to true. -// -// If |it| is an MSTRING or ANY type, it gets the underlying type from |*pval|, -// which must be an |ASN1_STRING| or |ASN1_TYPE|, respectively. It then updates -// |*putype| with the tag number of type used, or |V_ASN1_OTHER| if it was not a -// universal type. If |*putype| is set to |V_ASN1_SEQUENCE|, |V_ASN1_SET|, or -// |V_ASN1_OTHER|, it additionally outputs the tag and length, so the caller -// must not do so. -// -// Otherwise, |*putype| must contain |it->utype|. -// -// WARNING: Unlike most functions in this file, |asn1_ex_i2c| can return zero -// without omitting the element. ASN.1 values may have empty contents. -static int asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cout, int *out_omit, - int *putype, const ASN1_ITEM *it) { - ASN1_BOOLEAN *tbool = nullptr; - ASN1_STRING *strtmp; - ASN1_OBJECT *otmp; - int utype; - const unsigned char *cont; - unsigned char c; - int len; - - assert(it->itype == ASN1_ITYPE_PRIMITIVE || it->itype == ASN1_ITYPE_MSTRING); - // Historically, |it->funcs| for primitive types contained an - // |ASN1_PRIMITIVE_FUNCS| table of callbacks. +static int asn1_marshal_primitive(CBB *cbb, ASN1_VALUE **pval, + const ASN1_ITEM *it, CBS_ASN1_TAG tag) { + assert(it->itype == ASN1_ITYPE_PRIMITIVE); + // Historically, `it->funcs` for primitive types contained an + // `ASN1_PRIMITIVE_FUNCS` table of callbacks. assert(it->funcs == nullptr); - - *out_omit = 0; - - // Handle omitted optional values for all but BOOLEAN, which uses a - // non-pointer representation. - if (it->itype != ASN1_ITYPE_PRIMITIVE || it->utype != V_ASN1_BOOLEAN) { - if (!*pval) { - *out_omit = 1; - return 0; - } - } - - if (it->itype == ASN1_ITYPE_MSTRING) { - // If MSTRING type set the underlying type - strtmp = (ASN1_STRING *)*pval; - utype = strtmp->type; - if (utype < 0 && utype != V_ASN1_OTHER) { - // MSTRINGs can have type -1 when default-constructed. - OPENSSL_PUT_ERROR(ASN1, ASN1_R_WRONG_TYPE); - return -1; - } - // Negative INTEGER and ENUMERATED values use |ASN1_STRING| type values that - // do not match their corresponding utype values. - if (utype == V_ASN1_NEG_INTEGER) { - utype = V_ASN1_INTEGER; - } else if (utype == V_ASN1_NEG_ENUMERATED) { - utype = V_ASN1_ENUMERATED; - } - *putype = utype; - } else if (it->utype == V_ASN1_ANY) { - // If ANY set type and pointer to value - ASN1_TYPE *typ; - typ = (ASN1_TYPE *)*pval; - utype = typ->type; - if (utype < 0 && utype != V_ASN1_OTHER) { - // |ASN1_TYPE|s can have type -1 when default-constructed. - OPENSSL_PUT_ERROR(ASN1, ASN1_R_WRONG_TYPE); - return -1; - } - *putype = utype; - pval = &typ->value.asn1_value; - } else { - utype = *putype; + if (!asn1_item_has_value(pval, it)) { + OPENSSL_PUT_ERROR(ASN1, ASN1_R_MISSING_VALUE); + return 0; } - switch (utype) { - case V_ASN1_OBJECT: - otmp = (ASN1_OBJECT *)*pval; - cont = otmp->data; - len = otmp->length; - if (len == 0) { - // Some |ASN1_OBJECT|s do not have OIDs and cannot be serialized. - OPENSSL_PUT_ERROR(ASN1, ASN1_R_ILLEGAL_OBJECT); - return -1; - } - break; - - case V_ASN1_NULL: - cont = nullptr; - len = 0; - break; - - case V_ASN1_BOOLEAN: - tbool = (ASN1_BOOLEAN *)pval; - if (*tbool == ASN1_BOOLEAN_NONE) { - *out_omit = 1; + // Handle non-string types. + switch (it->utype) { + case V_ASN1_ANY: + // ANY types cannot be implicitly tagged. + if (tag != 0) { + OPENSSL_PUT_ERROR(ASN1, ASN1_R_BAD_TEMPLATE); return 0; } - if (it->utype != V_ASN1_ANY) { - // Default handling if value == size field then omit - if ((*tbool && (it->size > 0)) || (!*tbool && !it->size)) { - *out_omit = 1; - return 0; - } - } - c = *tbool ? 0xff : 0x00; - cont = &c; - len = 1; - break; - - case V_ASN1_BIT_STRING: { - int ret = - i2c_ASN1_BIT_STRING((ASN1_BIT_STRING *)*pval, cout ? &cout : nullptr); - // |i2c_ASN1_BIT_STRING| returns zero on error instead of -1. - return ret <= 0 ? -1 : ret; + return asn1_marshal_any(cbb, asn1_load_ptr_as(pval)); + case V_ASN1_OBJECT: + return asn1_marshal_object(cbb, asn1_load_ptr_as(pval), tag); + case V_ASN1_NULL: + tag = tag == 0 ? CBS_ASN1_NULL : tag; + return CBB_add_asn1_element(cbb, tag, nullptr, 0); + case V_ASN1_BOOLEAN: { + // `ASN1_BOOLEAN` is not a pointer type. + ASN1_BOOLEAN b = *reinterpret_cast(pval); + tag = tag == 0 ? CBS_ASN1_BOOLEAN : tag; + CBB child; + return CBB_add_asn1(cbb, &child, tag) && + CBB_add_u8(&child, b ? 0xff : 0x00) && // + CBB_flush(cbb); } + } + // All other supported types are represented as strings. + const ASN1_STRING *str = asn1_load_ptr_as(pval); + switch (it->utype) { + case V_ASN1_BIT_STRING: + return asn1_marshal_bit_string(cbb, str, tag); case V_ASN1_INTEGER: - case V_ASN1_ENUMERATED: { - // |i2c_ASN1_INTEGER| also handles ENUMERATED. - int ret = i2c_ASN1_INTEGER((ASN1_INTEGER *)*pval, cout ? &cout : nullptr); - // |i2c_ASN1_INTEGER| returns zero on error instead of -1. - return ret <= 0 ? -1 : ret; - } - + return asn1_marshal_integer(cbb, str, tag); + case V_ASN1_ENUMERATED: + tag = tag == 0 ? CBS_ASN1_ENUMERATED : tag; + return asn1_marshal_integer(cbb, str, tag); case V_ASN1_OCTET_STRING: case V_ASN1_NUMERICSTRING: case V_ASN1_PRINTABLESTRING: @@ -643,22 +335,14 @@ static int asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cout, int *out_omit, case V_ASN1_UNIVERSALSTRING: case V_ASN1_BMPSTRING: case V_ASN1_UTF8STRING: + tag = tag == 0 ? static_cast(it->utype) : tag; + return asn1_marshal_octet_string(cbb, str, tag); case V_ASN1_SEQUENCE: - case V_ASN1_SET: - // This is not a valid |ASN1_ITEM| type, but it appears in |ASN1_TYPE|. - case V_ASN1_OTHER: - // All based on ASN1_STRING and handled the same - strtmp = (ASN1_STRING *)*pval; - cont = strtmp->data; - len = strtmp->length; - break; - + // The `ASN1_SEQUENCE` item stores the entire element. + return CBB_add_bytes(cbb, ASN1_STRING_get0_data(str), + ASN1_STRING_length(str)); default: OPENSSL_PUT_ERROR(ASN1, ASN1_R_BAD_TEMPLATE); - return -1; - } - if (cout && len) { - OPENSSL_memcpy(cout, cont, len); + return 0; } - return len; } diff --git a/third_party/boringssl/src/crypto/asn1/tasn_fre.cc b/third_party/boringssl/src/crypto/asn1/tasn_fre.cc index 7b5164bda..ba5fe2beb 100644 --- a/third_party/boringssl/src/crypto/asn1/tasn_fre.cc +++ b/third_party/boringssl/src/crypto/asn1/tasn_fre.cc @@ -34,7 +34,7 @@ void bssl::ASN1_item_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it) { if (!pval) { return; } - if ((it->itype != ASN1_ITYPE_PRIMITIVE) && !*pval) { + if ((it->itype != ASN1_ITYPE_PRIMITIVE) && !asn1_load_ptr(pval)) { return; } @@ -68,8 +68,8 @@ void bssl::ASN1_item_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it) { if (asn1_cb) { asn1_cb(ASN1_OP_FREE_POST, pval, it, nullptr); } - OPENSSL_free(*pval); - *pval = nullptr; + OPENSSL_free(asn1_load_ptr(pval)); + asn1_store_ptr(pval, nullptr); break; } @@ -108,8 +108,8 @@ void bssl::ASN1_item_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it) { if (asn1_cb) { asn1_cb(ASN1_OP_FREE_POST, pval, it, nullptr); } - OPENSSL_free(*pval); - *pval = nullptr; + OPENSSL_free(asn1_load_ptr(pval)); + asn1_store_ptr(pval, nullptr); break; } } @@ -117,34 +117,35 @@ void bssl::ASN1_item_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it) { void bssl::ASN1_template_free(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt) { if (tt->flags & ASN1_TFLG_SK_MASK) { - STACK_OF(ASN1_VALUE) *sk = (STACK_OF(ASN1_VALUE) *)*pval; + STACK_OF(ASN1_VALUE) *sk = asn1_load_ptr_as(pval); for (size_t i = 0; i < sk_ASN1_VALUE_num(sk); i++) { ASN1_VALUE *vtmp = sk_ASN1_VALUE_value(sk, i); ASN1_item_ex_free(&vtmp, ASN1_ITEM_ptr(tt->item)); } sk_ASN1_VALUE_free(sk); - *pval = nullptr; + asn1_store_ptr(pval, nullptr); } else { ASN1_item_ex_free(pval, ASN1_ITEM_ptr(tt->item)); } } void bssl::ASN1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it) { - // Historically, |it->funcs| for primitive types contained an - // |ASN1_PRIMITIVE_FUNCS| table of callbacks. + // Historically, `it->funcs` for primitive types contained an + // `ASN1_PRIMITIVE_FUNCS` table of callbacks. assert(it->funcs == nullptr); int utype = it->itype == ASN1_ITYPE_MSTRING ? -1 : it->utype; switch (utype) { case V_ASN1_OBJECT: - ASN1_OBJECT_free((ASN1_OBJECT *)*pval); + ASN1_OBJECT_free(asn1_load_ptr_as(pval)); break; case V_ASN1_BOOLEAN: if (it) { - *(ASN1_BOOLEAN *)pval = (ASN1_BOOLEAN)it->size; + *reinterpret_cast(pval) = + static_cast(it->size); } else { - *(ASN1_BOOLEAN *)pval = ASN1_BOOLEAN_NONE; + *reinterpret_cast(pval) = ASN1_BOOLEAN_NONE; } return; @@ -152,16 +153,11 @@ void bssl::ASN1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it) { break; case V_ASN1_ANY: - if (*pval != nullptr) { - asn1_type_cleanup((ASN1_TYPE *)*pval); - OPENSSL_free(*pval); - } + ASN1_TYPE_free(asn1_load_ptr_as(pval)); break; default: - ASN1_STRING_free((ASN1_STRING *)*pval); - *pval = nullptr; - break; + ASN1_STRING_free(asn1_load_ptr_as(pval)); } - *pval = nullptr; + asn1_store_ptr(pval, nullptr); } diff --git a/third_party/boringssl/src/crypto/asn1/tasn_new.cc b/third_party/boringssl/src/crypto/asn1/tasn_new.cc index f73484bbd..0a380eb88 100644 --- a/third_party/boringssl/src/crypto/asn1/tasn_new.cc +++ b/third_party/boringssl/src/crypto/asn1/tasn_new.cc @@ -88,10 +88,11 @@ int bssl::ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it) { return 1; } } - *pval = reinterpret_cast(OPENSSL_zalloc(it->size)); - if (!*pval) { + void *obj = OPENSSL_zalloc(it->size); + if (obj == nullptr) { goto memerr; } + asn1_store_ptr(pval, obj); asn1_set_choice_selector(pval, -1, it); if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, nullptr)) { goto auxerr2; @@ -111,10 +112,11 @@ int bssl::ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it) { return 1; } } - *pval = reinterpret_cast(OPENSSL_zalloc(it->size)); - if (!*pval) { + void *obj = OPENSSL_zalloc(it->size); + if (obj == nullptr) { goto memerr; } + asn1_store_ptr(pval, obj); asn1_refcount_set_one(pval, it); asn1_enc_init(pval, it); for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) { @@ -146,7 +148,7 @@ int bssl::ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it) { static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it) { switch (it->itype) { case ASN1_ITYPE_EXTERN: - *pval = nullptr; + asn1_store_ptr(pval, nullptr); break; case ASN1_ITYPE_PRIMITIVE: @@ -163,46 +165,39 @@ static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it) { case ASN1_ITYPE_CHOICE: case ASN1_ITYPE_SEQUENCE: - *pval = nullptr; + asn1_store_ptr(pval, nullptr); break; } } static int ASN1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt) { const ASN1_ITEM *it = ASN1_ITEM_ptr(tt->item); - int ret; if (tt->flags & ASN1_TFLG_OPTIONAL) { asn1_template_clear(pval, tt); return 1; } - // If ANY DEFINED BY nothing to do - + // If ANY DEFINED BY, there is nothing to do. if (tt->flags & ASN1_TFLG_ADB_MASK) { - *pval = nullptr; + asn1_store_ptr(pval, nullptr); return 1; } - // If SET OF or SEQUENCE OF, its a STACK + // If SET OF or SEQUENCE OF, it's a STACK. if (tt->flags & ASN1_TFLG_SK_MASK) { - STACK_OF(ASN1_VALUE) *skval; - skval = sk_ASN1_VALUE_new_null(); + STACK_OF(ASN1_VALUE) *skval = sk_ASN1_VALUE_new_null(); if (!skval) { - ret = 0; - goto done; + return 0; } - *pval = (ASN1_VALUE *)skval; - ret = 1; - goto done; + asn1_store_ptr(pval, skval); + return 1; } - // Otherwise pass it back to the item routine - ret = ASN1_item_ex_new(pval, it); -done: - return ret; + // Otherwise, pass it back to the item routine. + return ASN1_item_ex_new(pval, it); } static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt) { - // If ADB or STACK just NULL the field + // If ADB or STACK, just NULL the field. if (tt->flags & (ASN1_TFLG_ADB_MASK | ASN1_TFLG_SK_MASK)) { - *pval = nullptr; + asn1_store_ptr(pval, nullptr); } else { asn1_item_clear(pval, ASN1_ITEM_ptr(tt->item)); } @@ -216,8 +211,8 @@ static int ASN1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it) { return 0; } - // Historically, |it->funcs| for primitive types contained an - // |ASN1_PRIMITIVE_FUNCS| table of callbacks. + // Historically, `it->funcs` for primitive types contained an + // `ASN1_PRIMITIVE_FUNCS` table of callbacks. assert(it->funcs == nullptr); int utype; @@ -228,33 +223,32 @@ static int ASN1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it) { } switch (utype) { case V_ASN1_OBJECT: - *pval = (ASN1_VALUE *)OBJ_get_undef(); + asn1_store_ptr(pval, OBJ_get_undef()); return 1; case V_ASN1_BOOLEAN: - *(ASN1_BOOLEAN *)pval = (ASN1_BOOLEAN)it->size; + *reinterpret_cast(pval) = + static_cast(it->size); return 1; case V_ASN1_NULL: - *pval = (ASN1_VALUE *)1; + asn1_store_ptr(pval, reinterpret_cast(1)); return 1; case V_ASN1_ANY: { - ASN1_TYPE *typ = New(); + ASN1_TYPE *typ = ASN1_TYPE_new(); if (!typ) { return 0; } - typ->value.ptr = nullptr; - typ->type = -1; - *pval = (ASN1_VALUE *)typ; + asn1_store_ptr(pval, typ); break; } default: - *pval = (ASN1_VALUE *)ASN1_STRING_type_new(utype); + asn1_store_ptr(pval, ASN1_STRING_type_new(utype)); break; } - if (*pval) { + if (asn1_load_ptr(pval)) { return 1; } return 0; @@ -262,8 +256,8 @@ static int ASN1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it) { static void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it) { int utype; - // Historically, |it->funcs| for primitive types contained an - // |ASN1_PRIMITIVE_FUNCS| table of callbacks. + // Historically, `it->funcs` for primitive types contained an + // `ASN1_PRIMITIVE_FUNCS` table of callbacks. assert(it == nullptr || it->funcs == nullptr); if (!it || (it->itype == ASN1_ITYPE_MSTRING)) { utype = -1; @@ -271,8 +265,10 @@ static void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it) { utype = it->utype; } if (utype == V_ASN1_BOOLEAN) { - *(ASN1_BOOLEAN *)pval = (ASN1_BOOLEAN)it->size; + // `ASN1_BOOLEAN` is not a pointer type. + *reinterpret_cast(pval) = + static_cast(it->size); } else { - *pval = nullptr; + asn1_store_ptr(pval, nullptr); } } diff --git a/third_party/boringssl/src/crypto/asn1/tasn_typ.cc b/third_party/boringssl/src/crypto/asn1/tasn_typ.cc index 0c3f6e2fd..e0dcfcff3 100644 --- a/third_party/boringssl/src/crypto/asn1/tasn_typ.cc +++ b/third_party/boringssl/src/crypto/asn1/tasn_typ.cc @@ -21,7 +21,7 @@ using namespace bssl; -// TODO(crbug.com/42290417): While we need |ASN1_ITEM|s, the exposed new, free, +// TODO(crbug.com/42290417): While we need `ASN1_ITEM`s, the exposed new, free, // i2d, and d2i functions should call the underlying implementations directly. #define IMPLEMENT_ASN1_STRING_FUNCTIONS(sname) \ @@ -55,8 +55,6 @@ IMPLEMENT_ASN1_TYPE(ASN1_ANY) // Just swallow an ASN1_SEQUENCE in an ASN1_STRING IMPLEMENT_ASN1_TYPE(ASN1_SEQUENCE) -IMPLEMENT_ASN1_FUNCTIONS_const_fname(ASN1_TYPE, ASN1_ANY, ASN1_TYPE) - BSSL_NAMESPACE_BEGIN IMPLEMENT_ASN1_MSTRING(DISPLAYTEXT, B_ASN1_DISPLAYTEXT) diff --git a/third_party/boringssl/src/crypto/asn1/tasn_utl.cc b/third_party/boringssl/src/crypto/asn1/tasn_utl.cc index 7af994197..6d3320f49 100644 --- a/third_party/boringssl/src/crypto/asn1/tasn_utl.cc +++ b/third_party/boringssl/src/crypto/asn1/tasn_utl.cc @@ -36,7 +36,8 @@ using namespace bssl; // Given an ASN1_ITEM CHOICE type return the selector value int bssl::asn1_get_choice_selector(ASN1_VALUE **pval, const ASN1_ITEM *it) { - int *sel = reinterpret_cast(offset2ptr(*pval, it->utype)); + int *sel = + reinterpret_cast(offset2ptr(asn1_load_ptr(pval), it->utype)); return *sel; } @@ -44,7 +45,7 @@ int bssl::asn1_get_choice_selector(ASN1_VALUE **pval, const ASN1_ITEM *it) { int bssl::asn1_set_choice_selector(ASN1_VALUE **pval, int value, const ASN1_ITEM *it) { int *sel, ret; - sel = reinterpret_cast(offset2ptr(*pval, it->utype)); + sel = reinterpret_cast(offset2ptr(asn1_load_ptr(pval), it->utype)); ret = *sel; *sel = value; return ret; @@ -60,7 +61,7 @@ static CRYPTO_refcount_t *asn1_get_references(ASN1_VALUE **pval, return nullptr; } return reinterpret_cast( - offset2ptr(*pval, aux->ref_offset)); + offset2ptr(asn1_load_ptr(pval), aux->ref_offset)); } void bssl::asn1_refcount_set_one(ASN1_VALUE **pval, const ASN1_ITEM *it) { @@ -82,14 +83,15 @@ int bssl::asn1_refcount_dec_and_test_zero(ASN1_VALUE **pval, static ASN1_ENCODING *asn1_get_enc_ptr(ASN1_VALUE **pval, const ASN1_ITEM *it) { assert(it->itype == ASN1_ITYPE_SEQUENCE); const ASN1_AUX *aux; - if (!pval || !*pval) { + if (!pval || !asn1_load_ptr(pval)) { return nullptr; } aux = reinterpret_cast(it->funcs); if (!aux || !(aux->flags & ASN1_AFLG_ENCODING)) { return nullptr; } - return reinterpret_cast(offset2ptr(*pval, aux->enc_offset)); + return reinterpret_cast( + offset2ptr(asn1_load_ptr(pval), aux->enc_offset)); } void bssl::asn1_enc_init(ASN1_VALUE **pval, const ASN1_ITEM *it) { @@ -131,53 +133,40 @@ void bssl::asn1_encoding_clear(ASN1_ENCODING *enc) { enc->len = 0; } -int bssl::asn1_enc_restore(int *len, unsigned char **out, ASN1_VALUE **pval, +int bssl::asn1_enc_restore(Span *out, ASN1_VALUE **pval, const ASN1_ITEM *it) { ASN1_ENCODING *enc = asn1_get_enc_ptr(pval, it); if (!enc || enc->len == 0) { return 0; } - if (out) { - OPENSSL_memcpy(*out, enc->enc, enc->len); - *out += enc->len; - } - if (len) { - *len = enc->len; - } + *out = Span(enc->enc, enc->len); return 1; } // Given an ASN1_TEMPLATE get a pointer to a field ASN1_VALUE **bssl::asn1_get_field_ptr(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt) { - ASN1_VALUE **pvaltmp = - reinterpret_cast(offset2ptr(*pval, tt->offset)); - // NOTE for BOOLEAN types the field is just a plain int so we can't return - // int **, so settle for (int *). - return pvaltmp; + return reinterpret_cast( + offset2ptr(asn1_load_ptr(pval), tt->offset)); } // Handle ANY DEFINED BY template, find the selector, look up the relevant // ASN1_TEMPLATE in the table and return it. const ASN1_TEMPLATE *bssl::asn1_do_adb(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt, int nullerr) { - const ASN1_ADB *adb; - const ASN1_ADB_TABLE *atbl; - ASN1_VALUE **sfld; - int i; if (!(tt->flags & ASN1_TFLG_ADB_MASK)) { return tt; } // Else ANY DEFINED BY ... get the table - adb = ASN1_ADB_ptr(tt->item); + const ASN1_ADB *adb = ASN1_ADB_ptr(tt->item); // Get the selector field - sfld = reinterpret_cast(offset2ptr(*pval, adb->offset)); - - // Check if NULL + ASN1_VALUE **sfld = reinterpret_cast( + offset2ptr(asn1_load_ptr(pval), adb->offset)); int selector; - if (*sfld == nullptr) { + const ASN1_OBJECT *selector_obj = asn1_load_ptr_as(sfld); + if (selector_obj == nullptr) { if (!adb->null_tt) { goto err; } @@ -188,21 +177,19 @@ const ASN1_TEMPLATE *bssl::asn1_do_adb(ASN1_VALUE **pval, // NB: don't check for NID_undef here because it // might be a legitimate value in the table assert(tt->flags & ASN1_TFLG_ADB_OID); - selector = OBJ_obj2nid((ASN1_OBJECT *)*sfld); + selector = OBJ_obj2nid(asn1_load_ptr_as(sfld)); // Try to find matching entry in table Maybe should check application types // first to allow application override? Might also be useful to have a flag // which indicates table is sorted and we can do a binary search. For now // stick to a linear search. - - for (atbl = adb->tbl, i = 0; i < adb->tblcount; i++, atbl++) { + for (int i = 0; i < adb->tblcount; i++) { + const ASN1_ADB_TABLE *atbl = &adb->tbl[i]; if (atbl->value == selector) { return &atbl->tt; } } - // FIXME: need to search application table too - // No match, return default type if (!adb->default_tt) { goto err; @@ -216,3 +203,16 @@ const ASN1_TEMPLATE *bssl::asn1_do_adb(ASN1_VALUE **pval, } return nullptr; } + +CBS_ASN1_TAG bssl::asn1_tag_to_cbs(int aclass, int tag) { + if ((aclass & ASN1_TFLG_TAG_CLASS) != aclass || // + tag < 0 || static_cast(tag) > CBS_ASN1_TAG_NUMBER_MASK || + // Universal tag zero is reserved and should never appear in a template. + // This file treats the zero tag as a sentinel value. + (aclass == 0 && tag == 0)) { + OPENSSL_PUT_ERROR(ASN1, ASN1_R_BAD_TEMPLATE); + return 0; + } + return ((static_cast(aclass)) << CBS_ASN1_TAG_SHIFT) | + static_cast(tag); +} diff --git a/third_party/boringssl/src/crypto/base64/base64.cc b/third_party/boringssl/src/crypto/base64/base64.cc index 4b202e17e..77e8e95b5 100644 --- a/third_party/boringssl/src/crypto/base64/base64.cc +++ b/third_party/boringssl/src/crypto/base64/base64.cc @@ -24,18 +24,18 @@ using namespace bssl; -// constant_time_lt_args_8 behaves like |constant_time_lt_8| but takes |uint8_t| +// constant_time_lt_args_8 behaves like `constant_time_lt_8` but takes `uint8_t` // arguments for a slightly simpler implementation. static uint8_t constant_time_lt_args_8(uint8_t a, uint8_t b) { crypto_word_t aw = a; crypto_word_t bw = b; - // |crypto_word_t| is larger than |uint8_t|, so |aw| and |bw| have the same - // MSB. |aw| < |bw| iff MSB(|aw| - |bw|) is 1. + // `crypto_word_t` is larger than `uint8_t`, so `aw` and `bw` have the same + // MSB. `aw` < `bw` iff MSB(`aw` - `bw`) is 1. return constant_time_msb_w(aw - bw); } -// constant_time_in_range_8 returns |CONSTTIME_TRUE_8| if |min| <= |a| <= |max| -// and |CONSTTIME_FALSE_8| otherwise. +// constant_time_in_range_8 returns `CONSTTIME_TRUE_8` if `min` <= `a` <= `max` +// and `CONSTTIME_FALSE_8` otherwise. static uint8_t constant_time_in_range_8(uint8_t a, uint8_t min, uint8_t max) { a -= min; return constant_time_lt_args_8(a, max - min + 1); @@ -242,7 +242,7 @@ static uint8_t base64_ascii_to_bin(uint8_t a) { } // base64_decode_quad decodes a single “quad” (i.e. four characters) of base64 -// data and writes up to three bytes to |out|. It sets |*out_num_bytes| to the +// data and writes up to three bytes to `out`. It sets `*out_num_bytes` to the // number of bytes written, which will be less than three if the quad ended // with padding. It returns one on success or zero on error. static int base64_decode_quad(uint8_t *out, size_t *out_num_bytes, diff --git a/third_party/boringssl/src/crypto/bcm_support.h b/third_party/boringssl/src/crypto/bcm_support.h index 20e6ceff9..451e56a1c 100644 --- a/third_party/boringssl/src/crypto/bcm_support.h +++ b/third_party/boringssl/src/crypto/bcm_support.h @@ -28,14 +28,10 @@ BSSL_NAMESPACE_BEGIN // from the operating system, if the operating system requires initialization. void CRYPTO_init_sysrand(); -// CRYPTO_sysrand fills |len| bytes at |buf| with entropy from the operating +// CRYPTO_sysrand fills `len` bytes at `buf` with entropy from the operating // system. void CRYPTO_sysrand(uint8_t *buf, size_t len); -// RAND_need_entropy is called whenever the BCM module has stopped because it -// has run out of entropy. -void RAND_need_entropy(size_t bytes_needed); - // crypto_get_fork_generation returns the fork generation number for the current // process, or zero if not supported on the platform. The fork generation number // is a non-zero, strictly-monotonic counter with the property that, if queried @@ -48,7 +44,7 @@ void RAND_need_entropy(size_t bytes_needed); // the save fork generation. Note this logic transparently handles platforms // which always return zero. // -// This is not reliably supported on all platforms which implement |fork|, so it +// This is not reliably supported on all platforms which implement `fork`, so it // should only be used as a hardening measure. OPENSSL_EXPORT uint64_t CRYPTO_get_fork_generation(); diff --git a/third_party/boringssl/src/crypto/bio/bio.cc b/third_party/boringssl/src/crypto/bio/bio.cc index 9250be0fb..7db379337 100644 --- a/third_party/boringssl/src/crypto/bio/bio.cc +++ b/third_party/boringssl/src/crypto/bio/bio.cc @@ -15,15 +15,16 @@ #include #include -#include #include #include +#include #include #include #include #include +#include #include "../internal.h" #include "../mem_internal.h" @@ -45,6 +46,8 @@ BIO *BIO_new(const BIO_METHOD *method) { } if (method->create != nullptr && !method->create(ret.get())) { + // Matching OpenSSL, don't call `destroy` if `create` fails. + ret->method = nullptr; return nullptr; } @@ -53,7 +56,8 @@ BIO *BIO_new(const BIO_METHOD *method) { Bio::~Bio() { BIO *next = BIO_pop(this); - if (method->destroy != nullptr) { + // `method` may be null if `create` failed. + if (method != nullptr && method->destroy != nullptr) { method->destroy(this); } CRYPTO_free_ex_data(&g_ex_data_class, &ex_data); @@ -72,6 +76,13 @@ int BIO_up_ref(BIO *bio) { return 1; } +BIO *BIO_dup_ref(const BIO *bio) { + if (bio == nullptr) { + return nullptr; + } + return FromOpaque(bio)->DupRefInternal(); +} + void BIO_vfree(BIO *bio) { BIO_free(bio); } void BIO_free_all(BIO *bio) { BIO_free(bio); } @@ -80,11 +91,11 @@ int BIO_read(BIO *bio, void *buf, int len) { auto *impl = FromOpaque(bio); if (impl == nullptr || impl->method->bread == nullptr) { OPENSSL_PUT_ERROR(BIO, BIO_R_UNSUPPORTED_METHOD); - return -2; + return -1; } if (!impl->init) { OPENSSL_PUT_ERROR(BIO, BIO_R_UNINITIALIZED); - return -2; + return -1; } if (len <= 0) { return 0; @@ -92,6 +103,9 @@ int BIO_read(BIO *bio, void *buf, int len) { int ret = impl->method->bread(impl, reinterpret_cast(buf), len); if (ret > 0) { impl->num_read += ret; + } else if (ret < 0) { + // In preparation for |BIO_read_ex|, canonicalize error returns to -1. + ret = -1; } return ret; } @@ -100,11 +114,11 @@ int BIO_gets(BIO *bio, char *buf, int len) { auto *impl = FromOpaque(bio); if (impl == nullptr || impl->method->bgets == nullptr) { OPENSSL_PUT_ERROR(BIO, BIO_R_UNSUPPORTED_METHOD); - return -2; + return -1; } if (!impl->init) { OPENSSL_PUT_ERROR(BIO, BIO_R_UNINITIALIZED); - return -2; + return -1; } if (len <= 0) { return 0; @@ -116,35 +130,66 @@ int BIO_gets(BIO *bio, char *buf, int len) { return ret; } -int BIO_write(BIO *bio, const void *in, int inl) { +int BIO_write_ex(BIO *bio, const void *data, size_t len, size_t *out_written) { + if (out_written != nullptr) { + *out_written = 0; + } auto *impl = FromOpaque(bio); - if (impl == nullptr || impl->method->bwrite == nullptr) { + if (impl == nullptr || + (impl->method->bwrite == nullptr && impl->method->bwrite_ex == nullptr)) { OPENSSL_PUT_ERROR(BIO, BIO_R_UNSUPPORTED_METHOD); - return -2; + return 0; } if (!impl->init) { OPENSSL_PUT_ERROR(BIO, BIO_R_UNINITIALIZED); - return -2; - } - if (inl <= 0) { return 0; } - int ret = impl->method->bwrite(impl, reinterpret_cast(in), inl); - if (ret > 0) { - impl->num_write += ret; + // Matching OpenSSL, writing zero bytes should "successfully" write no bytes. + if (len == 0) { + return 1; } - return ret; + // Support `BIO_METHOD`s using either the old or new API. + size_t written; + if (impl->method->bwrite_ex != nullptr) { + if (!impl->method->bwrite_ex(bio, static_cast(data), len, + &written)) { + return 0; + } + } else { + int ret = + impl->method->bwrite(impl, static_cast(data), + static_cast(std::min(len, size_t{INT_MAX}))); + if (ret <= 0) { + return 0; + } + written = static_cast(ret); + } + impl->num_write += written; + if (out_written != nullptr) { + *out_written = written; + } + return 1; +} + +int BIO_write(BIO *bio, const void *in, int inl) { + // Matching OpenSSL, `inl <= 0` returns zero, i.e. a "successful" write of + // zero bytes. + inl = std::max(inl, 0); + size_t written; + if (!BIO_write_ex(bio, in, static_cast(inl), &written)) { + return -1; + } + return static_cast(written); } int BIO_write_all(BIO *bio, const void *data, size_t len) { - const uint8_t *data_u8 = reinterpret_cast(data); - while (len > 0) { - int ret = BIO_write(bio, data_u8, len > INT_MAX ? INT_MAX : (int)len); - if (ret <= 0) { + auto span = Span(reinterpret_cast(data), len); + while (!span.empty()) { + size_t written; + if (!BIO_write_ex(bio, span.data(), span.size(), &written)) { return 0; } - data_u8 += ret; - len -= ret; + span = span.subspan(written); } return 1; } @@ -152,7 +197,7 @@ int BIO_write_all(BIO *bio, const void *data, size_t len) { int BIO_puts(BIO *bio, const char *in) { size_t len = strlen(in); if (len > INT_MAX) { - // |BIO_write| and the return value both assume the string fits in |int|. + // `BIO_write` and the return value both assume the string fits in `int`. OPENSSL_PUT_ERROR(BIO, ERR_R_OVERFLOW); return -1; } @@ -171,7 +216,7 @@ long BIO_ctrl(BIO *bio, int cmd, long larg, void *parg) { if (impl->method->ctrl == nullptr) { OPENSSL_PUT_ERROR(BIO, BIO_R_UNSUPPORTED_METHOD); - return -2; + return -1; } return impl->method->ctrl(impl, cmd, larg, parg); @@ -255,9 +300,7 @@ void BIO_clear_retry_flags(BIO *bio) { impl->retry_reason = 0; } -int BIO_method_type(const BIO *bio) { - return FromOpaque(bio)->method->type; -} +int BIO_method_type(const BIO *bio) { return FromOpaque(bio)->method->type; } void BIO_copy_next_retry(BIO *bio) { auto *impl = FromOpaque(bio); @@ -381,14 +424,14 @@ static int print_bio(const char *str, size_t len, void *bio) { void ERR_print_errors(BIO *bio) { ERR_print_errors_cb(print_bio, bio); } -// bio_read_all reads everything from |bio| and prepends |prefix| to it. On -// success, |*out| is set to an allocated buffer (which should be freed with -// |OPENSSL_free|), |*out_len| is set to its length and one is returned. The -// buffer will contain |prefix| followed by the contents of |bio|. On failure, +// bio_read_all reads everything from `bio` and prepends `prefix` to it. On +// success, `*out` is set to an allocated buffer (which should be freed with +// `OPENSSL_free`), `*out_len` is set to its length and one is returned. The +// buffer will contain `prefix` followed by the contents of `bio`. On failure, // zero is returned. // // The function will fail if the size of the output would equal or exceed -// |max_len|. +// `max_len`. static int bio_read_all(Bio *bio, uint8_t **out, size_t *out_len, const uint8_t *prefix, size_t prefix_len, size_t max_len) { @@ -443,11 +486,11 @@ static int bio_read_all(Bio *bio, uint8_t **out, size_t *out_len, } } -// bio_read_full reads |len| bytes |bio| and writes them into |out|. It -// tolerates partial reads from |bio| and returns one on success or zero if a -// read fails before |len| bytes are read. On failure, it additionally sets -// |*out_eof_on_first_read| to whether the error was due to |bio| returning zero -// on the first read. |out_eof_on_first_read| may be NULL to discard the value. +// bio_read_full reads `len` bytes `bio` and writes them into `out`. It +// tolerates partial reads from `bio` and returns one on success or zero if a +// read fails before `len` bytes are read. On failure, it additionally sets +// `*out_eof_on_first_read` to whether the error was due to `bio` returning zero +// on the first read. `out_eof_on_first_read` may be NULL to discard the value. static int bio_read_full(Bio *bio, uint8_t *out, int *out_eof_on_first_read, size_t len) { int first_read = 1; @@ -468,8 +511,8 @@ static int bio_read_full(Bio *bio, uint8_t *out, int *out_eof_on_first_read, return 1; } -// For compatibility with existing |d2i_*_bio| callers, |BIO_read_asn1| uses -// |ERR_LIB_ASN1| errors. +// For compatibility with existing `d2i_*_bio` callers, `BIO_read_asn1` uses +// `ERR_LIB_ASN1` errors. OPENSSL_DECLARE_ERROR_REASON(ASN1, ASN1_R_DECODE_ERROR) OPENSSL_DECLARE_ERROR_REASON(ASN1, ASN1_R_HEADER_TOO_LONG) OPENSSL_DECLARE_ERROR_REASON(ASN1, ASN1_R_NOT_ENOUGH_DATA) @@ -483,9 +526,9 @@ int BIO_read_asn1(BIO *bio, uint8_t **out, size_t *out_len, size_t max_len) { auto *impl = FromOpaque(bio); if (!bio_read_full(impl, header, &eof_on_first_read, kInitialHeaderLen)) { if (eof_on_first_read) { - // Historically, OpenSSL returned |ASN1_R_HEADER_TOO_LONG| when - // |d2i_*_bio| could not read anything. CPython conditions on this to - // determine if |bio| was empty. + // Historically, OpenSSL returned `ASN1_R_HEADER_TOO_LONG` when + // `d2i_*_bio` could not read anything. CPython conditions on this to + // determine if `bio` was empty. OPENSSL_PUT_ERROR(ASN1, ASN1_R_HEADER_TOO_LONG); } else { OPENSSL_PUT_ERROR(ASN1, ASN1_R_NOT_ENOUGH_DATA); @@ -580,7 +623,7 @@ static int g_index = BIO_TYPE_START; int BIO_get_new_index() { MutexWriteLock lock(&g_index_lock); - // If |g_index| exceeds 255, it will collide with the flags bits. + // If `g_index` exceeds 255, it will collide with the flags bits. int ret = g_index > 255 ? -1 : g_index++; return ret; } @@ -591,7 +634,6 @@ BIO_METHOD *BIO_meth_new(int type, const char *name) { return nullptr; } method->type = type; - method->name = name; return method; } @@ -607,6 +649,13 @@ int BIO_meth_set_destroy(BIO_METHOD *method, int (*destroy_func)(BIO *)) { return 1; } +int BIO_meth_set_write_ex(BIO_METHOD *method, + int (*write_ex_func)(BIO *, const char *, size_t, + size_t *)) { + method->bwrite_ex = write_ex_func; + return 1; +} + int BIO_meth_set_write(BIO_METHOD *method, int (*write_func)(BIO *, const char *, int)) { method->bwrite = write_func; @@ -653,7 +702,7 @@ void BIO_set_shutdown(BIO *bio, int shutdown) { int BIO_get_shutdown(BIO *bio) { return FromOpaque(bio)->shutdown; } int BIO_meth_set_puts(BIO_METHOD *method, int (*puts)(BIO *, const char *)) { - // Ignore the parameter. We implement |BIO_puts| using |BIO_write|. + // Ignore the parameter. We implement `BIO_puts` using `BIO_write`. return 1; } diff --git a/third_party/boringssl/src/crypto/bio/bio_mem.cc b/third_party/boringssl/src/crypto/bio/bio_mem.cc index 479b9ce20..af79c03b9 100644 --- a/third_party/boringssl/src/crypto/bio/bio_mem.cc +++ b/third_party/boringssl/src/crypto/bio/bio_mem.cc @@ -43,14 +43,14 @@ BIO *BIO_new_mem_buf(const void *buf, ossl_ssize_t len) { } b = (BUF_MEM *)BIO_get_data(ret); - // BIO_FLAGS_MEM_RDONLY ensures |b->data| is not written to. + // BIO_FLAGS_MEM_RDONLY ensures `b->data` is not written to. b->data = reinterpret_cast(const_cast(buf)); b->length = size; b->max = size; BIO_set_flags(ret, BIO_FLAGS_MEM_RDONLY); - // |num| is used to store the value that this BIO will return when it runs + // `num` is used to store the value that this BIO will return when it runs // out of data. If it's negative then the retry flags will also be set. Since // this is static data, retrying won't help FromOpaque(ret)->num = 0; @@ -66,7 +66,7 @@ static int mem_new(BIO *bio) { return 0; } - // |shutdown| is used to store the close flag: whether the BIO has ownership + // `shutdown` is used to store the close flag: whether the BIO has ownership // of the BUF_MEM. BIO_set_shutdown(bio, 1); BIO_set_init(bio, 1); @@ -93,10 +93,6 @@ static int mem_free(BIO *bio) { static int mem_read(BIO *bio, char *out, int outl) { BIO_clear_retry_flags(bio); - if (outl <= 0) { - return 0; - } - BUF_MEM *b = reinterpret_cast(BIO_get_data(bio)); int ret = outl; if ((size_t)ret > b->length) { @@ -120,23 +116,20 @@ static int mem_read(BIO *bio, char *out, int outl) { return ret; } -static int mem_write(BIO *bio, const char *in, int inl) { +static int mem_write_ex(BIO *bio, const char *in, size_t inl, + size_t *out_written) { BIO_clear_retry_flags(bio); - if (inl <= 0) { - return 0; // Successfully write zero bytes. - } - if (BIO_test_flags(bio, BIO_FLAGS_MEM_RDONLY)) { OPENSSL_PUT_ERROR(BIO, BIO_R_WRITE_TO_READ_ONLY_BIO); - return -1; + return 0; } BUF_MEM *b = reinterpret_cast(BIO_get_data(bio)); if (!BUF_MEM_append(b, in, inl)) { - return -1; + return 0; } - - return inl; + *out_written = inl; + return 1; } static int mem_gets(BIO *bio, char *buf, int size) { @@ -192,8 +185,8 @@ static long mem_ctrl(BIO *bio, int cmd, long num, void *ptr) { char **out = reinterpret_cast(ptr); *out = b->data; } - // This API can overflow on 64-bit Windows, where |long| is smaller than - // |ptrdiff_t|. |BIO_mem_contents| is the overflow-safe API. + // This API can overflow on 64-bit Windows, where `long` is smaller than + // `ptrdiff_t`. `BIO_mem_contents` is the overflow-safe API. return static_cast(b->length); case BIO_C_SET_BUF_MEM: mem_free(bio); @@ -224,9 +217,15 @@ static long mem_ctrl(BIO *bio, int cmd, long num, void *ptr) { } static const BIO_METHOD mem_method = { - BIO_TYPE_MEM, "memory buffer", mem_write, - mem_read, mem_gets, mem_ctrl, - mem_new, mem_free, /*callback_ctrl=*/nullptr, + BIO_TYPE_MEM, + /*bwrite=*/nullptr, + mem_write_ex, + mem_read, + mem_gets, + mem_ctrl, + mem_new, + mem_free, + /*callback_ctrl=*/nullptr, }; const BIO_METHOD *BIO_s_mem() { return &mem_method; } diff --git a/third_party/boringssl/src/crypto/bio/connect.cc b/third_party/boringssl/src/crypto/bio/connect.cc index 7f4a17be1..6e0e7a556 100644 --- a/third_party/boringssl/src/crypto/bio/connect.cc +++ b/third_party/boringssl/src/crypto/bio/connect.cc @@ -18,6 +18,7 @@ #include #include +#include #include #if !defined(OPENSSL_WINDOWS) @@ -30,6 +31,7 @@ #include #endif +#include #include #include @@ -74,9 +76,9 @@ struct BIO_CONNECT { static int closesocket(int sock) { return close(sock); } #endif -// split_host_and_port sets |*out_host| and |*out_port| to the host and port -// parsed from |name|. It returns one on success or zero on error. Even when -// successful, |*out_port| may be NULL on return if no port was specified. +// split_host_and_port sets `*out_host` and `*out_port` to the host and port +// parsed from `name`. It returns one on success or zero on error. Even when +// successful, `*out_port` may be NULL on return if no port was specified. static int split_host_and_port(UniquePtr *out_host, UniquePtr *out_port, const char *name) { const char *host, *port = nullptr; @@ -209,21 +211,19 @@ static int conn_state(BIO *bio, BIO_CONNECT *c) { break; case BIO_CONN_S_BLOCKED_CONNECT: - i = bio_sock_error(FromOpaque(bio)->num); - if (i) { - if (bio_socket_should_retry(ret)) { + if (!bio_socket_finish_connect(FromOpaque(bio)->num)) { + if (bio_socket_should_retry(-1)) { BIO_set_retry_special(bio); c->state = BIO_CONN_S_BLOCKED_CONNECT; BIO_set_retry_reason(bio, BIO_RR_CONNECT); - ret = -1; } else { BIO_clear_retry_flags(bio); OPENSSL_PUT_SYSTEM_ERROR(); OPENSSL_PUT_ERROR(BIO, BIO_R_NBIO_CONNECT_ERROR); ERR_add_error_data(4, "host=", c->param_hostname.get(), ":", c->param_port.get()); - ret = 0; } + ret = -1; goto exit_loop; } else { c->state = BIO_CONN_S_OK; @@ -311,28 +311,32 @@ static int conn_read(BIO *bio, char *out, int out_len) { return ret; } -static int conn_write(BIO *bio, const char *in, int in_len) { - int ret; - BIO_CONNECT *data; - - data = (BIO_CONNECT *)BIO_get_data(bio); +static int conn_write_ex(BIO *bio, const char *in, size_t in_len, + size_t *out_written) { + BIO_CONNECT *data = (BIO_CONNECT *)BIO_get_data(bio); if (data->state != BIO_CONN_S_OK) { - ret = conn_state(bio, data); - if (ret <= 0) { - return ret; + if (conn_state(bio, data) <= 0) { + return 0; } } bio_clear_socket_error(); - ret = (int)send(FromOpaque(bio)->num, in, in_len, 0); +#if defined(OPENSSL_WINDOWS) + in_len = std::min(in_len, size_t{INT_MAX}); + int ret = send(FromOpaque(bio)->num, in, static_cast(in_len), 0); +#else + ssize_t ret = send(FromOpaque(bio)->num, in, in_len, 0); +#endif BIO_clear_retry_flags(bio); if (ret <= 0) { if (bio_socket_should_retry(ret)) { BIO_set_retry_write(bio); } + return 0; } - return ret; + *out_written = ret; + return 1; } static long conn_ctrl(BIO *bio, int cmd, long num, void *ptr) { @@ -427,9 +431,9 @@ BIO *BIO_new_connect(const char *hostname) { } static const BIO_METHOD methods_connectp = { - BIO_TYPE_CONNECT, "socket connect", conn_write, - conn_read, /*gets=*/nullptr, conn_ctrl, - conn_new, conn_free, conn_callback_ctrl, + BIO_TYPE_CONNECT, /*bwrite=*/nullptr, conn_write_ex, + conn_read, /*gets=*/nullptr, conn_ctrl, + conn_new, conn_free, conn_callback_ctrl, }; const BIO_METHOD *BIO_s_connect() { return &methods_connectp; } diff --git a/third_party/boringssl/src/crypto/bio/fd.cc b/third_party/boringssl/src/crypto/bio/fd.cc index b49acb653..1ad9b09ab 100644 --- a/third_party/boringssl/src/crypto/bio/fd.cc +++ b/third_party/boringssl/src/crypto/bio/fd.cc @@ -23,6 +23,8 @@ #include #else #include +#include +#include #endif #include @@ -36,12 +38,10 @@ #define BORINGSSL_CLOSE _close #define BORINGSSL_LSEEK _lseek #define BORINGSSL_READ _read - #define BORINGSSL_WRITE _write #else #define BORINGSSL_CLOSE close #define BORINGSSL_LSEEK lseek #define BORINGSSL_READ read - #define BORINGSSL_WRITE write #endif using namespace bssl; @@ -72,9 +72,7 @@ static int fd_free(BIO *bio) { } static int fd_read(BIO *b, char *out, int outl) { - int ret = 0; - - ret = (int)BORINGSSL_READ(FromOpaque(b)->num, out, outl); + int ret = (int)BORINGSSL_READ(FromOpaque(b)->num, out, outl); BIO_clear_retry_flags(b); if (ret <= 0) { if (bio_errno_should_retry(ret)) { @@ -85,16 +83,24 @@ static int fd_read(BIO *b, char *out, int outl) { return ret; } -static int fd_write(BIO *b, const char *in, int inl) { - int ret = (int)BORINGSSL_WRITE(FromOpaque(b)->num, in, inl); +static int fd_write_ex(BIO *b, const char *in, size_t inl, + size_t *out_written) { +#if defined(OPENSSL_WINDOWS) + inl = std::min(inl, size_t{UINT_MAX}); + int ret = _write(FromOpaque(b)->num, in, static_cast(inl)); +#else + ssize_t ret = write(FromOpaque(b)->num, in, inl); +#endif BIO_clear_retry_flags(b); if (ret <= 0) { if (bio_errno_should_retry(ret)) { BIO_set_retry_write(b); } + return 0; } - return ret; + *out_written = ret; + return 1; } static long fd_ctrl(BIO *b, int cmd, long num, void *ptr) { @@ -158,14 +164,20 @@ static int fd_gets(BIO *bp, char *buf, int size) { ptr[0] = '\0'; - // The output length is bounded by |size|. + // The output length is bounded by `size`. return (int)(ptr - buf); } static const BIO_METHOD methods_fdp = { - BIO_TYPE_FD, "file descriptor", fd_write, - fd_read, fd_gets, fd_ctrl, - fd_new, fd_free, /*callback_ctrl=*/nullptr, + BIO_TYPE_FD, + /*bwrite=*/nullptr, + fd_write_ex, + fd_read, + fd_gets, + fd_ctrl, + fd_new, + fd_free, + /*callback_ctrl=*/nullptr, }; const BIO_METHOD *BIO_s_fd() { return &methods_fdp; } diff --git a/third_party/boringssl/src/crypto/bio/file.cc b/third_party/boringssl/src/crypto/bio/file.cc index ca80b69af..cdee5b1dd 100644 --- a/third_party/boringssl/src/crypto/bio/file.cc +++ b/third_party/boringssl/src/crypto/bio/file.cc @@ -111,10 +111,6 @@ static int file_free(BIO *bio) { } static int file_read(BIO *b, char *out, int outl) { - if (!BIO_get_init(b)) { - return 0; - } - size_t ret = fread(out, 1, outl, (FILE *)BIO_get_data(b)); if (ret == 0 && ferror((FILE *)BIO_get_data(b))) { OPENSSL_PUT_SYSTEM_ERROR(); @@ -122,20 +118,23 @@ static int file_read(BIO *b, char *out, int outl) { return -1; } - // fread reads at most |outl| bytes, so |ret| fits in an int. + // fread reads at most `outl` bytes, so `ret` fits in an int. return (int)ret; } -static int file_write(BIO *b, const char *in, int inl) { - if (!BIO_get_init(b)) { +static int file_write_ex(BIO *b, const char *in, size_t inl, + size_t *out_written) { + if (fwrite(in, inl, 1, (FILE *)BIO_get_data(b)) == 0) { return 0; } - - int ret = (int)fwrite(in, inl, 1, (FILE *)BIO_get_data(b)); - if (ret > 0) { - ret = inl; - } - return ret; + // We currently ask |fwrite| to write one "object" of size |inl|, rather than + // |inl| objects of size 1. |fwrite| will only succeed if it wrote the whole + // input. + // + // TODO(crbug.com/518854940): This also means we discard information about + // partial writes. + *out_written = inl; + return 1; } static long file_ctrl(BIO *b, int cmd, long num, void *ptr) { @@ -157,10 +156,10 @@ static long file_ctrl(BIO *b, int cmd, long num, void *ptr) { static_assert((BIO_CLOSE & BIO_FP_TEXT) == 0, "BIO_CLOSE and BIO_FP_TEXT must not collide"); #if defined(OPENSSL_WINDOWS) - // If |BIO_FP_TEXT| is not set, OpenSSL will switch the file to binary + // If `BIO_FP_TEXT` is not set, OpenSSL will switch the file to binary // mode. BoringSSL intentionally diverges here because it means code - // tested under POSIX will inadvertently change the state of |FILE| - // objects when wrapping them in a |BIO|. + // tested under POSIX will inadvertently change the state of `FILE` + // objects when wrapping them in a `BIO`. if (num & BIO_FP_TEXT) { _setmode(_fileno(reinterpret_cast(ptr)), _O_TEXT); } @@ -226,7 +225,7 @@ static int file_gets(BIO *bp, char *buf, int size) { if (!fgets(buf, size, (FILE *)BIO_get_data(bp))) { buf[0] = 0; // TODO(davidben): This doesn't distinguish error and EOF. This should check - // |ferror| as in |file_read|. + // `ferror` as in `file_read`. return 0; } @@ -234,9 +233,15 @@ static int file_gets(BIO *bp, char *buf, int size) { } static const BIO_METHOD methods_filep = { - BIO_TYPE_FILE, "FILE pointer", file_write, - file_read, file_gets, file_ctrl, - /*create=*/nullptr, file_free, /*callback_ctrl=*/nullptr, + BIO_TYPE_FILE, + /*bwrite=*/nullptr, + file_write_ex, + file_read, + file_gets, + file_ctrl, + /*create=*/nullptr, + file_free, + /*callback_ctrl=*/nullptr, }; const BIO_METHOD *BIO_s_file() { return &methods_filep; } diff --git a/third_party/boringssl/src/crypto/bio/hexdump.cc b/third_party/boringssl/src/crypto/bio/hexdump.cc index a3f611a3a..244070c5c 100644 --- a/third_party/boringssl/src/crypto/bio/hexdump.cc +++ b/third_party/boringssl/src/crypto/bio/hexdump.cc @@ -46,8 +46,8 @@ static char to_char(uint8_t b) { return b; } -// hexdump_write adds |len| bytes of |data| to the current hex dump described by -// |ctx|. +// hexdump_write adds `len` bytes of `data` to the current hex dump described by +// `ctx`. static int hexdump_write(struct hexdump_ctx *ctx, const uint8_t *data, size_t len) { char buf[10]; @@ -106,9 +106,9 @@ static int hexdump_write(struct hexdump_ctx *ctx, const uint8_t *data, return 1; } -// finish flushes any buffered data in |ctx|. +// finish flushes any buffered data in `ctx`. static int finish(struct hexdump_ctx *ctx) { - // See the comments in |hexdump| for the details of this format. + // See the comments in `hexdump` for the details of this format. const unsigned n_bytes = ctx->used; unsigned l; char buf[5]; diff --git a/third_party/boringssl/src/crypto/bio/internal.h b/third_party/boringssl/src/crypto/bio/internal.h index 77cd8a88c..5b4425ddd 100644 --- a/third_party/boringssl/src/crypto/bio/internal.h +++ b/third_party/boringssl/src/crypto/bio/internal.h @@ -28,8 +28,8 @@ // newlib uses u_short in socket.h without defining it. typedef unsigned short u_short; #endif -#include #include +#include #else #include typedef int socklen_t; @@ -41,8 +41,8 @@ DECLARE_OPAQUE_STRUCT(bio_st, Bio) struct bio_method_st { int type; - const char *name; int (*bwrite)(BIO *, const char *, int); + int (*bwrite_ex)(BIO *, const char *, size_t, size_t *); int (*bread)(BIO *, char *, int); int (*bgets)(BIO *, char *, int); long (*ctrl)(BIO *, int, long, void *); @@ -60,14 +60,14 @@ class Bio : public bio_st, public RefCounted { const BIO_METHOD *method; CRYPTO_EX_DATA ex_data; - // TODO(crbug.com/412269080): |init| and |shutdown| could be bitfields, or - // integrated into |flags|, to save memory. + // TODO(crbug.com/412269080): `init` and `shutdown` could be bitfields, or + // integrated into `flags`, to save memory. - // init is non-zero if this |BIO| has been initialised. + // init is non-zero if this `BIO` has been initialised. int init = 0; - // shutdown is often used by specific |BIO_METHOD|s to determine whether + // shutdown is often used by specific `BIO_METHOD`s to determine whether // they own some underlying resource. This flag can often be controlled by - // |BIO_set_close|. For example, whether an fd BIO closes the underlying fd + // `BIO_set_close`. For example, whether an fd BIO closes the underlying fd // when it, itself, is closed. int shutdown = 1; int flags = 0; @@ -76,8 +76,8 @@ class Bio : public bio_st, public RefCounted { // file descriptor. int num = 0; void *ptr = nullptr; - // next_bio points to the next |BIO| in a chain. This |BIO| owns a reference - // to |next_bio|. + // next_bio points to the next `BIO` in a chain. This `BIO` owns a reference + // to `next_bio`. Bio *next_bio = nullptr; // used by filter BIOs uint64_t num_read = 0, num_write = 0; @@ -88,16 +88,16 @@ class Bio : public bio_st, public RefCounted { #if !defined(OPENSSL_NO_SOCK) -// bio_ip_and_port_to_socket_and_addr creates a socket and fills in |*out_addr| -// and |*out_addr_length| with the correct values for connecting to |hostname| -// on |port_str|. It returns one on success or zero on error. +// bio_ip_and_port_to_socket_and_addr creates a socket and fills in `*out_addr` +// and `*out_addr_length` with the correct values for connecting to `hostname` +// on `port_str`. It returns one on success or zero on error. int bio_ip_and_port_to_socket_and_addr(int *out_sock, struct sockaddr_storage *out_addr, socklen_t *out_addr_length, const char *hostname, const char *port_str); -// bio_socket_nbio sets whether |sock| is non-blocking. It returns one on +// bio_socket_nbio sets whether `sock` is non-blocking. It returns one on // success and zero otherwise. int bio_socket_nbio(int sock, int on); @@ -106,17 +106,20 @@ int bio_socket_nbio(int sock, int on); // TODO(fork): remove all callers of this. void bio_clear_socket_error(); -// bio_sock_error returns the last socket error on |sock|. -int bio_sock_error(int sock); +// bio_socket_finish_connect attempts to complete an in-progress, non-blocking +// connect operation on `sock`. It returns one if the connect operation +// succeeded. Otherwise, it returns zero and sets the last socket error to the +// reason it failed. +int bio_socket_finish_connect(int sock); -// bio_socket_should_retry returns non-zero if |return_value| indicates an error +// bio_socket_should_retry returns non-zero if `return_value` indicates an error // and the last socket error indicates that it's non-fatal. int bio_socket_should_retry(int return_value); #endif // !OPENSSL_NO_SOCK -// bio_errno_should_retry returns non-zero if |return_value| indicates an error -// and |errno| indicates that it's non-fatal. +// bio_errno_should_retry returns non-zero if `return_value` indicates an error +// and `errno` indicates that it's non-fatal. int bio_errno_should_retry(int return_value); BSSL_NAMESPACE_END diff --git a/third_party/boringssl/src/crypto/bio/pair.cc b/third_party/boringssl/src/crypto/bio/pair.cc index 11b5c2efa..f80ec36c2 100644 --- a/third_party/boringssl/src/crypto/bio/pair.cc +++ b/third_party/boringssl/src/crypto/bio/pair.cc @@ -108,28 +108,18 @@ static int bio_free(BIO *bio) { static int bio_read(BIO *bio, char *buf, int size_) { size_t size = size_; - size_t rest; - struct bio_bio_st *b, *peer_b; BIO_clear_retry_flags(bio); - if (!BIO_get_init(bio)) { - return 0; - } - - b = reinterpret_cast(BIO_get_data(bio)); + bio_bio_st *b = reinterpret_cast(BIO_get_data(bio)); assert(b != nullptr); assert(b->peer != nullptr); - peer_b = reinterpret_cast(BIO_get_data(b->peer)); + bio_bio_st *peer_b = reinterpret_cast(BIO_get_data(b->peer)); assert(peer_b != nullptr); assert(peer_b->buf != nullptr); peer_b->request = 0; // will be set in "retry_read" situation - if (buf == nullptr || size == 0) { - return 0; - } - if (peer_b->len == 0) { if (peer_b->closed) { return 0; // writer has closed, and no data is left @@ -152,7 +142,7 @@ static int bio_read(BIO *bio, char *buf, int size_) { } // now read "size" bytes - rest = size; + size_t rest = size; assert(rest > 0); // one or two iterations @@ -186,22 +176,15 @@ static int bio_read(BIO *bio, char *buf, int size_) { rest -= chunk; } while (rest); - // |size| is bounded by the buffer size, which fits in |int|. + // `size` is bounded by the buffer size, which fits in `int`. return (int)size; } -static int bio_write(BIO *bio, const char *buf, int num_) { - size_t num = num_; - size_t rest; - struct bio_bio_st *b; - +static int bio_write_ex(BIO *bio, const char *buf, size_t num, + size_t *out_written) { BIO_clear_retry_flags(bio); - if (!BIO_get_init(bio) || buf == nullptr || num == 0) { - return 0; - } - - b = reinterpret_cast(BIO_get_data(bio)); + struct bio_bio_st *b = reinterpret_cast(BIO_get_data(bio)); assert(b != nullptr); assert(b->peer != nullptr); assert(b->buf != nullptr); @@ -210,14 +193,14 @@ static int bio_write(BIO *bio, const char *buf, int num_) { if (b->closed) { // we already closed OPENSSL_PUT_ERROR(BIO, BIO_R_BROKEN_PIPE); - return -1; + return 0; } assert(b->len <= b->size); if (b->len == b->size) { BIO_set_retry_write(bio); // buffer is full - return -1; + return 0; } // we can write @@ -226,7 +209,7 @@ static int bio_write(BIO *bio, const char *buf, int num_) { } // now write "num" bytes - rest = num; + size_t rest = num; assert(rest > 0); // one or two iterations @@ -259,8 +242,8 @@ static int bio_write(BIO *bio, const char *buf, int num_) { buf += chunk; } while (rest); - // |num| is bounded by the buffer size, which fits in |int|. - return (int)num; + *out_written = num; + return 1; } static int bio_make_pair(BIO *bio1, BIO *bio2, size_t writebuf1_len, @@ -322,7 +305,7 @@ static long bio_ctrl(BIO *bio, int cmd, long num, void *ptr) { // Specific control codes first: case BIO_C_GET_WRITE_BUF_SIZE: // TODO(crbug.com/412584975): This can overflow on 64-bit Windows. Do we - // need it? It implements |BIO_get_write_buf_size|, but we don't have the + // need it? It implements `BIO_get_write_buf_size`, but we don't have the // wrapper. return static_cast(b->size); @@ -400,8 +383,8 @@ static long bio_ctrl(BIO *bio, int cmd, long num, void *ptr) { static const BIO_METHOD methods_biop = { BIO_TYPE_BIO, - "BIO pair", - bio_write, + /*bwrite=*/nullptr, + bio_write_ex, bio_read, /*gets=*/nullptr, bio_ctrl, diff --git a/third_party/boringssl/src/crypto/bio/socket.cc b/third_party/boringssl/src/crypto/bio/socket.cc index c61daeca4..19266bd4d 100644 --- a/third_party/boringssl/src/crypto/bio/socket.cc +++ b/third_party/boringssl/src/crypto/bio/socket.cc @@ -18,6 +18,9 @@ #include #include +#include + +#include #if !defined(OPENSSL_WINDOWS) #include @@ -47,10 +50,6 @@ static int sock_free(BIO *bio) { } static int sock_read(BIO *b, char *out, int outl) { - if (out == nullptr) { - return 0; - } - bio_clear_socket_error(); #if defined(OPENSSL_WINDOWS) int ret = recv(FromOpaque(b)->num, out, outl, 0); @@ -66,20 +65,25 @@ static int sock_read(BIO *b, char *out, int outl) { return ret; } -static int sock_write(BIO *b, const char *in, int inl) { +static int sock_write_ex(BIO *b, const char *in, size_t inl, + size_t *out_written) { bio_clear_socket_error(); #if defined(OPENSSL_WINDOWS) - int ret = send(FromOpaque(b)->num, in, inl, 0); + inl = std::min(inl, size_t{INT_MAX}); + int ret = send(FromOpaque(b)->num, in, static_cast(inl), 0); #else - int ret = (int)write(FromOpaque(b)->num, in, inl); + ssize_t ret = write(FromOpaque(b)->num, in, inl); #endif BIO_clear_retry_flags(b); if (ret <= 0) { if (bio_socket_should_retry(ret)) { BIO_set_retry_write(b); } + return 0; } - return ret; + + *out_written = ret; + return 1; } static long sock_ctrl(BIO *b, int cmd, long num, void *ptr) { @@ -113,14 +117,10 @@ static long sock_ctrl(BIO *b, int cmd, long num, void *ptr) { static const BIO_METHOD methods_sockp = { BIO_TYPE_SOCKET, - "socket", - sock_write, - sock_read, - nullptr /* gets, */, - sock_ctrl, - nullptr /* create */, - sock_free, - nullptr /* callback_ctrl */, + /*bwrite=*/nullptr, sock_write_ex, + sock_read, nullptr /* gets, */, + sock_ctrl, nullptr /* create */, + sock_free, nullptr /* callback_ctrl */, }; const BIO_METHOD *BIO_s_socket() { return &methods_sockp; } @@ -137,28 +137,28 @@ BIO *BIO_new_socket(int fd, int close_flag) { } // These functions are provided solely for compatibility with software that -// tries to copy and then modify |BIO_s_socket|. See bio.h for details. -// PostgreSQL's use makes several fragile assumptions on |BIO_s_socket|: +// tries to copy and then modify `BIO_s_socket`. See bio.h for details. +// PostgreSQL's use makes several fragile assumptions on `BIO_s_socket`: // -// - We do not store anything in |BIO_set_data|. (Broken in upstream OpenSSL, +// - We do not store anything in `BIO_set_data`. (Broken in upstream OpenSSL, // which broke PostgreSQL.) -// - We do not store anything in |BIO_set_app_data|. -// - |BIO_s_socket| is implemented internally using the non-|size_t|-clean -// I/O functions rather than the |size_t|-clean ones. -// - |BIO_METHOD| never gains another function pointer that is used in concert +// - We do not store anything in `BIO_set_app_data`. +// - `BIO_s_socket` is implemented internally using the non-`size_t`-clean +// I/O functions rather than the `size_t`-clean ones. +// - `BIO_METHOD` never gains another function pointer that is used in concert // with any of the functions here. // -// Some other projects doing similar things use |BIO_meth_get_read| and -// |BIO_meth_get_write| and in turn assume that |BIO_s_socket| has not been -// ported to the |size_t|-clean |BIO_read_ex| and |BIO_write_ex|. (Not yet +// Some other projects doing similar things use `BIO_meth_get_read` and +// `BIO_meth_get_write` and in turn assume that `BIO_s_socket` has not been +// ported to the `size_t`-clean `BIO_read_ex` and `BIO_write_ex`. (Not yet // implemented in BoringSSL.) // // This is hopelessly fragile. PostgreSQL 18 will include a fix to stop using // these APIs, but older versions and other software remain impacted, so we -// implement these functions, but only support |BIO_s_socket|. For now they just +// implement these functions, but only support `BIO_s_socket`. For now they just // return the underlying functions, but if we ever need to break the above -// assumptions, we can return an older, frozen version of |BIO_s_socket|. -// Limiting to exactly one allowed |BIO_METHOD| lets us do this. +// assumptions, we can return an older, frozen version of `BIO_s_socket`. +// Limiting to exactly one allowed `BIO_METHOD` lets us do this. // // These functions are also deprecated in upstream OpenSSL. See // https://github.com/openssl/openssl/issues/26047 diff --git a/third_party/boringssl/src/crypto/bio/socket_helper.cc b/third_party/boringssl/src/crypto/bio/socket_helper.cc index e251b3417..6c975b382 100644 --- a/third_party/boringssl/src/crypto/bio/socket_helper.cc +++ b/third_party/boringssl/src/crypto/bio/socket_helper.cc @@ -27,7 +27,9 @@ #include #if !defined(OPENSSL_WINDOWS) +#include #include +#include #include #else #include @@ -38,13 +40,13 @@ #include "../internal.h" -using namespace bssl; +BSSL_NAMESPACE_BEGIN -int bssl::bio_ip_and_port_to_socket_and_addr(int *out_sock, - struct sockaddr_storage *out_addr, - socklen_t *out_addr_length, - const char *hostname, - const char *port_str) { +int bio_ip_and_port_to_socket_and_addr(int *out_sock, + struct sockaddr_storage *out_addr, + socklen_t *out_addr_length, + const char *hostname, + const char *port_str) { struct addrinfo hint, *result, *cur; int ret; @@ -90,7 +92,7 @@ int bssl::bio_ip_and_port_to_socket_and_addr(int *out_sock, return ret; } -int bssl::bio_socket_nbio(int sock, int on) { +int bio_socket_nbio(int sock, int on) { #if defined(OPENSSL_WINDOWS) u_long arg = on; @@ -109,19 +111,61 @@ int bssl::bio_socket_nbio(int sock, int on) { #endif } -void bssl::bio_clear_socket_error() {} +void bio_clear_socket_error() {} -int bssl::bio_sock_error(int sock) { +int bio_socket_finish_connect(int sock) { + // A blocked connect signals whether it is ready based on whether it is + // writable. (SO_ERROR is not filled in before it is writable.) +#if defined(OPENSSL_WINDOWS) + fd_set write_set, except_set; + FD_ZERO(&write_set); + FD_SET(static_cast(sock), &write_set); + FD_ZERO(&except_set); + FD_SET(static_cast(sock), &except_set); + timeval timeout = {0, 0}; + if (select(0 /* unused on Windows */, /*readfds=*/nullptr, &write_set, + &except_set, &timeout) == SOCKET_ERROR) { + return 0; + } + if (!FD_ISSET(sock, &write_set) && !FD_ISSET(sock, &except_set)) { + // The connect has not completed. Set the error that |connect| would return. + WSASetLastError(WSAEWOULDBLOCK); + return 0; + } +#else + pollfd pfd; + pfd.fd = sock; + // poll implicitly listens for POLLERR and POLLHUP. + pfd.events = POLLOUT; + pfd.revents = 0; + if (poll(&pfd, 1, /*timeout=*/0) < 0) { + return 0; + } + if (pfd.revents == 0) { + // The connect has not completed. Set the error that |connect| would return. + errno = EINPROGRESS; + return 0; + } +#endif + + // Check if the connection succeeded. int error; socklen_t error_size = sizeof(error); - if (getsockopt(sock, SOL_SOCKET, SO_ERROR, (char *)&error, &error_size) < 0) { - return 1; + return 0; + } + if (error != 0) { +#if defined(OPENSSL_WINDOWS) + WSASetLastError(error); +#else + errno = error; +#endif + return 0; } - return error; + return 1; } -int bssl::bio_socket_should_retry(int return_value) { +int bio_socket_should_retry(int return_value) { #if defined(OPENSSL_WINDOWS) return return_value == -1 && WSAGetLastError() == WSAEWOULDBLOCK; #else @@ -130,4 +174,6 @@ int bssl::bio_socket_should_retry(int return_value) { #endif } +BSSL_NAMESPACE_END + #endif // OPENSSL_NO_SOCK diff --git a/third_party/boringssl/src/crypto/blake2/blake2.cc b/third_party/boringssl/src/crypto/blake2/blake2.cc index e149164d8..684b35e0d 100644 --- a/third_party/boringssl/src/crypto/blake2/blake2.cc +++ b/third_party/boringssl/src/crypto/blake2/blake2.cc @@ -22,6 +22,8 @@ using namespace bssl; +namespace { + // https://tools.ietf.org/html/rfc7693#section-2.6 static const uint64_t kIV[8] = { UINT64_C(0x6a09e667f3bcc908), UINT64_C(0xbb67ae8584caa73b), @@ -47,8 +49,8 @@ static const uint8_t kSigma[10 * 16] = { }; // https://tools.ietf.org/html/rfc7693#section-3.1 -static void blake2b_mix(uint64_t v[16], int a, int b, int c, int d, uint64_t x, - uint64_t y) { +void blake2b_mix(uint64_t v[16], int a, int b, int c, int d, uint64_t x, + uint64_t y) { v[a] = v[a] + v[b] + x; v[d] = CRYPTO_rotr_u64(v[d] ^ v[a], 32); v[c] = v[c] + v[d]; @@ -59,13 +61,12 @@ static void blake2b_mix(uint64_t v[16], int a, int b, int c, int d, uint64_t x, v[b] = CRYPTO_rotr_u64(v[b] ^ v[c], 63); } -static uint64_t blake2b_load(const uint8_t block[BLAKE2B_CBLOCK], size_t i) { +uint64_t blake2b_load(const uint8_t block[BLAKE2B_CBLOCK], size_t i) { return CRYPTO_load_u64_le(block + 8 * i); } -static void blake2b_transform(BLAKE2B_CTX *b2b, - const uint8_t block[BLAKE2B_CBLOCK], - size_t num_bytes, int is_final_block) { +void blake2b_transform(BLAKE2B_CTX *b2b, const uint8_t block[BLAKE2B_CBLOCK], + size_t num_bytes, int is_final_block) { // https://tools.ietf.org/html/rfc7693#section-3.2 uint64_t v[16]; static_assert(sizeof(v) == sizeof(b2b->h) + sizeof(kIV)); @@ -109,6 +110,8 @@ static void blake2b_transform(BLAKE2B_CTX *b2b, } } +} // namespace + void BLAKE2B256_Init(BLAKE2B_CTX *b2b) { OPENSSL_memset(b2b, 0, sizeof(BLAKE2B_CTX)); @@ -139,7 +142,7 @@ void BLAKE2B256_Update(BLAKE2B_CTX *b2b, const void *in_data, size_t len) { return; } - // More input remains therefore we must have filled |b2b->block|. + // More input remains therefore we must have filled `b2b->block`. assert(b2b->block_used == BLAKE2B_CBLOCK); blake2b_transform(b2b, b2b->block, BLAKE2B_CBLOCK, /*is_final_block=*/0); diff --git a/third_party/boringssl/src/crypto/bn/bn_asn1.cc b/third_party/boringssl/src/crypto/bn/bn_asn1.cc index 71b7ae1b7..80f5b6aca 100644 --- a/third_party/boringssl/src/crypto/bn/bn_asn1.cc +++ b/third_party/boringssl/src/crypto/bn/bn_asn1.cc @@ -45,7 +45,7 @@ int BN_marshal_asn1(CBB *cbb, const BIGNUM *bn) { CBB child; if (!CBB_add_asn1(cbb, &child, CBS_ASN1_INTEGER) || // The number must be padded with a leading zero if the high bit would - // otherwise be set or if |bn| is zero. + // otherwise be set or if `bn` is zero. (BN_num_bits(bn) % 8 == 0 && !CBB_add_u8(&child, 0x00)) || !BN_bn2cbb_padded(&child, BN_num_bytes(bn), bn) || !CBB_flush(cbb)) { diff --git a/third_party/boringssl/src/crypto/bn/convert.cc b/third_party/boringssl/src/crypto/bn/convert.cc index d41453ad6..a31e97fb1 100644 --- a/third_party/boringssl/src/crypto/bn/convert.cc +++ b/third_party/boringssl/src/crypto/bn/convert.cc @@ -73,20 +73,20 @@ char *BN_bn2hex(const BIGNUM *bn) { return buf; } -// decode_hex decodes |in_len| bytes of hex data from |in| and updates |bn|. +// decode_hex decodes `in_len` bytes of hex data from `in` and updates `bn`. static int decode_hex(BIGNUM *bn, const char *in, int in_len) { if (in_len > INT_MAX / 4) { OPENSSL_PUT_ERROR(BN, BN_R_BIGNUM_TOO_LONG); return 0; } - // |in_len| is the number of hex digits. + // `in_len` is the number of hex digits. if (!bn_expand(bn, in_len * 4)) { return 0; } int i = 0; while (in_len > 0) { - // Decode one |BN_ULONG| at a time. + // Decode one `BN_ULONG` at a time. int todo = BN_BYTES * 2; if (todo > in_len) { todo = in_len; @@ -97,7 +97,7 @@ static int decode_hex(BIGNUM *bn, const char *in, int in_len) { for (j = todo; j > 0; j--) { uint8_t hex = 0; if (!OPENSSL_fromxdigit(&hex, in[in_len - j])) { - // This shouldn't happen. The caller checks |OPENSSL_isxdigit|. + // This shouldn't happen. The caller checks `OPENSSL_isxdigit`. assert(0); } word = (word << 4) | hex; @@ -111,12 +111,12 @@ static int decode_hex(BIGNUM *bn, const char *in, int in_len) { return 1; } -// decode_dec decodes |in_len| bytes of decimal data from |in| and updates |bn|. +// decode_dec decodes `in_len` bytes of decimal data from `in` and updates `bn`. static int decode_dec(BIGNUM *bn, const char *in, int in_len) { int i, j; BN_ULONG l = 0; - // Decode |BN_DEC_NUM| digits at a time. + // Decode `BN_DEC_NUM` digits at a time. j = BN_DEC_NUM - (in_len % BN_DEC_NUM); if (j == BN_DEC_NUM) { j = 0; diff --git a/third_party/boringssl/src/crypto/bn/div.cc b/third_party/boringssl/src/crypto/bn/div.cc index 824ab75dd..fae57b824 100644 --- a/third_party/boringssl/src/crypto/bn/div.cc +++ b/third_party/boringssl/src/crypto/bn/div.cc @@ -30,27 +30,27 @@ int BN_mod_pow2(BIGNUM *r, const BIGNUM *a, size_t e) { size_t num_words = 1 + ((e - 1) / BN_BITS2); - // If |a| definitely has less than |e| bits, just BN_copy. + // If `a` definitely has less than `e` bits, just BN_copy. if ((size_t)a->width < num_words) { return BN_copy(r, a) != nullptr; } - // Otherwise, first make sure we have enough space in |r|. + // Otherwise, first make sure we have enough space in `r`. // Note that this will fail if num_words > INT_MAX. if (!bn_wexpand(r, num_words)) { return 0; } - // Copy the content of |a| into |r|. + // Copy the content of `a` into `r`. OPENSSL_memcpy(r->d, a->d, num_words * sizeof(BN_ULONG)); - // If |e| isn't word-aligned, we have to mask off some of our bits. + // If `e` isn't word-aligned, we have to mask off some of our bits. size_t top_word_exponent = e % (sizeof(BN_ULONG) * 8); if (top_word_exponent != 0) { r->d[num_words - 1] &= (((BN_ULONG)1) << top_word_exponent) - 1; } - // Fill in the remaining fields of |r|. + // Fill in the remaining fields of `r`. r->neg = a->neg; r->width = (int)num_words; bn_set_minimal_width(r); @@ -69,21 +69,21 @@ int BN_nnmod_pow2(BIGNUM *r, const BIGNUM *a, size_t e) { size_t num_words = 1 + (e - 1) / BN_BITS2; - // Expand |r| to the size of our modulus. + // Expand `r` to the size of our modulus. if (!bn_wexpand(r, num_words)) { return 0; } - // Clear the upper words of |r|. + // Clear the upper words of `r`. OPENSSL_memset(&r->d[r->width], 0, (num_words - r->width) * BN_BYTES); - // Set parameters of |r|. + // Set parameters of `r`. r->neg = 0; r->width = (int)num_words; - // Now, invert every word. The idea here is that we want to compute 2^e-|x|, - // which is actually equivalent to the twos-complement representation of |x| - // in |e| bits, which is -x = ~x + 1. + // Now, invert every word. The idea here is that we want to compute 2^e-`x`, + // which is actually equivalent to the twos-complement representation of `x` + // in `e` bits, which is -x = ~x + 1. for (int i = 0; i < r->width; i++) { r->d[i] = ~r->d[i]; } @@ -94,7 +94,7 @@ int BN_nnmod_pow2(BIGNUM *r, const BIGNUM *a, size_t e) { r->d[r->width - 1] &= (((BN_ULONG)1) << top_word_exponent) - 1; } - // Keep the minimal-width invariant for |BIGNUM|. + // Keep the minimal-width invariant for `BIGNUM`. bn_set_minimal_width(r); // Finally, add one, for the reason described above. diff --git a/third_party/boringssl/src/crypto/bn/exponentiation.cc b/third_party/boringssl/src/crypto/bn/exponentiation.cc index f4a4b4067..e0981ac0e 100644 --- a/third_party/boringssl/src/crypto/bn/exponentiation.cc +++ b/third_party/boringssl/src/crypto/bn/exponentiation.cc @@ -80,7 +80,7 @@ static int mod_exp_even(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, return BN_one(r); } - // Make a copy of |a|, in case it aliases |r|. + // Make a copy of `a`, in case it aliases `r`. BN_CTXScope scope(ctx); BIGNUM *tmp = BN_CTX_get(ctx); if (tmp == nullptr || !BN_copy(tmp, a)) { diff --git a/third_party/boringssl/src/crypto/bn/sqrt.cc b/third_party/boringssl/src/crypto/bn/sqrt.cc index 56a0f003b..c5e4d0523 100644 --- a/third_party/boringssl/src/crypto/bn/sqrt.cc +++ b/third_party/boringssl/src/crypto/bn/sqrt.cc @@ -49,22 +49,22 @@ int BN_sqrt(BIGNUM *out_sqrt, const BIGNUM *in, BN_CTX *ctx) { goto err; } - // This is Newton's method for finding a root of the equation |estimate|^2 - - // |in| = 0. + // This is Newton's method for finding a root of the equation `estimate`^2 - + // `in` = 0. for (;;) { - // |estimate| = 1/2 * (|estimate| + |in|/|estimate|) + // `estimate` = 1/2 * (`estimate` + `in`/`estimate`) if (!BN_div(tmp, nullptr, in, estimate, ctx) || !BN_add(tmp, tmp, estimate) || !BN_rshift1(estimate, tmp) || - // |tmp| = |estimate|^2 + // `tmp` = `estimate`^2 !BN_sqr(tmp, estimate, ctx) || - // |delta| = |in| - |tmp| + // `delta` = `in` - `tmp` !BN_sub(delta, in, tmp)) { OPENSSL_PUT_ERROR(BN, ERR_R_BN_LIB); goto err; } delta->neg = 0; - // The difference between |in| and |estimate| squared is required to always + // The difference between `in` and `estimate` squared is required to always // decrease. This ensures that the loop always terminates, but I don't have // a proof that it always finds the square root for a given square. if (last_delta_valid && BN_cmp(delta, last_delta) >= 0) { diff --git a/third_party/boringssl/src/crypto/bytestring/asn1_compat.cc b/third_party/boringssl/src/crypto/bytestring/asn1_compat.cc index 0ce6055de..21ebde9e1 100644 --- a/third_party/boringssl/src/crypto/bytestring/asn1_compat.cc +++ b/third_party/boringssl/src/crypto/bytestring/asn1_compat.cc @@ -34,7 +34,6 @@ int bssl::CBB_finish_i2d(CBB *cbb, uint8_t **outp) { uint8_t *der; size_t der_len; if (!CBB_finish(cbb, &der, &der_len)) { - CBB_cleanup(cbb); return -1; } if (der_len > INT_MAX) { diff --git a/third_party/boringssl/src/crypto/bytestring/ber.cc b/third_party/boringssl/src/crypto/bytestring/ber.cc index f7ec5d71f..ac8148b8a 100644 --- a/third_party/boringssl/src/crypto/bytestring/ber.cc +++ b/third_party/boringssl/src/crypto/bytestring/ber.cc @@ -25,7 +25,7 @@ using namespace bssl; // kMaxDepth limits the recursion depth to avoid overflowing the stack. static const uint32_t kMaxDepth = 128; -// is_string_type returns one if |tag| is a string type and zero otherwise. It +// is_string_type returns one if `tag` is a string type and zero otherwise. It // ignores the constructed bit. static int is_string_type(CBS_ASN1_TAG tag) { // While BER supports constructed BIT STRINGS, OpenSSL misparses them. To @@ -50,10 +50,10 @@ static int is_string_type(CBS_ASN1_TAG tag) { } } -// cbs_find_ber walks an ASN.1 structure in |orig_in| and sets |*ber_found| +// cbs_find_ber walks an ASN.1 structure in `orig_in` and sets `*ber_found` // depending on whether an indefinite length element or constructed string was -// found. The value of |orig_in| is not changed. It returns one on success (i.e. -// |*ber_found| was set) and zero on error. +// found. The value of `orig_in` is not changed. It returns one on success (i.e. +// `*ber_found` was set) and zero on error. static int cbs_find_ber(const CBS *orig_in, int *ber_found, uint32_t depth) { if (depth > kMaxDepth) { return 0; @@ -94,8 +94,8 @@ static int cbs_find_ber(const CBS *orig_in, int *ber_found, uint32_t depth) { return 1; } -// cbs_get_eoc returns one if |cbs| begins with an "end of contents" (EOC) value -// and zero otherwise. If an EOC was found, it advances |cbs| past it. +// cbs_get_eoc returns one if `cbs` begins with an "end of contents" (EOC) value +// and zero otherwise. If an EOC was found, it advances `cbs` past it. static int cbs_get_eoc(CBS *cbs) { if (CBS_len(cbs) >= 2 && CBS_data(cbs)[0] == 0 && CBS_data(cbs)[1] == 0) { @@ -104,11 +104,11 @@ static int cbs_get_eoc(CBS *cbs) { return 0; } -// cbs_convert_ber reads BER data from |in| and writes DER data to |out|. If -// |string_tag| is non-zero, then all elements must match |string_tag| up to the -// constructed bit and primitive element bodies are written to |out| without +// cbs_convert_ber reads BER data from `in` and writes DER data to `out`. If +// `string_tag` is non-zero, then all elements must match `string_tag` up to the +// constructed bit and primitive element bodies are written to `out` without // element headers. This is used when concatenating the fragments of a -// constructed string. If |looking_for_eoc| is set then any EOC elements found +// constructed string. If `looking_for_eoc` is set then any EOC elements found // will cause the function to return after consuming it. It returns one on // success and zero on error. static int cbs_convert_ber(CBS *in, CBB *out, CBS_ASN1_TAG string_tag, @@ -136,7 +136,7 @@ static int cbs_convert_ber(CBS *in, CBB *out, CBS_ASN1_TAG string_tag, if (string_tag != 0) { // This is part of a constructed string. All elements must match - // |string_tag| up to the constructed bit and get appended to |out| + // `string_tag` up to the constructed bit and get appended to `out` // without a child element. if ((tag & ~CBS_ASN1_CONSTRUCTED) != string_tag) { return 0; @@ -235,7 +235,7 @@ int bssl::CBS_get_asn1_implicit_string(CBS *in, CBS *out, uint8_t **out_storage, } // Otherwise, try to parse an implicitly-tagged constructed string. - // |CBS_asn1_ber_to_der| is assumed to have run, so only allow one level deep + // `CBS_asn1_ber_to_der` is assumed to have run, so only allow one level deep // of nesting. CBB result; CBS child; diff --git a/third_party/boringssl/src/crypto/bytestring/cbb.cc b/third_party/boringssl/src/crypto/bytestring/cbb.cc index bbe59fa85..8e46486b7 100644 --- a/third_party/boringssl/src/crypto/bytestring/cbb.cc +++ b/third_party/boringssl/src/crypto/bytestring/cbb.cc @@ -15,13 +15,18 @@ #include #include +#include #include +#include #include +#include + #include #include #include "../internal.h" +#include "../mem_internal.h" #include "internal.h" @@ -58,8 +63,8 @@ int CBB_init_fixed(CBB *cbb, uint8_t *buf, size_t len) { } void CBB_cleanup(CBB *cbb) { - // Child |CBB|s are non-owning. They are implicitly discarded and should not - // be used with |CBB_cleanup| or |ScopedCBB|. + // Child `CBB`s are non-owning. They are implicitly discarded and should not + // be used with `CBB_cleanup` or `ScopedCBB`. assert(!cbb->is_child); if (cbb->is_child) { return; @@ -67,12 +72,14 @@ void CBB_cleanup(CBB *cbb) { if (cbb->u.base.can_resize) { OPENSSL_free(cbb->u.base.buf); + cbb->u.base.buf = nullptr; } } static int cbb_buffer_reserve(struct cbb_buffer_st *base, uint8_t **out, size_t len) { if (base == nullptr) { + OPENSSL_PUT_ERROR(CRYPTO, ERR_R_PASSED_NULL_PARAMETER); return 0; } @@ -119,7 +126,7 @@ static int cbb_buffer_add(struct cbb_buffer_st *base, uint8_t **out, if (!cbb_buffer_reserve(base, out, len)) { return 0; } - // This will not overflow or |cbb_buffer_reserve| would have failed. + // This will not overflow or `cbb_buffer_reserve` would have failed. base->len += len; return 1; } @@ -135,7 +142,8 @@ int CBB_finish(CBB *cbb, uint8_t **out_data, size_t *out_len) { } if (cbb->u.base.can_resize && (out_data == nullptr || out_len == nullptr)) { - // |out_data| and |out_len| can only be NULL if the CBB is fixed. + // `out_data` and `out_len` can only be NULL if the CBB is fixed. + OPENSSL_PUT_ERROR(CRYPTO, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); return 0; } @@ -158,24 +166,24 @@ static struct cbb_buffer_st *cbb_get_base(CBB *cbb) { } static void cbb_on_error(CBB *cbb) { - // Due to C's lack of destructors and |CBB|'s auto-flushing API, a failing - // |CBB|-taking function may leave a dangling pointer to a child |CBB|. As a - // result, the convention is callers may not write to |CBB|s that have failed. - // But, as a safety measure, we lock the |CBB| into an error state. Once the - // error bit is set, |cbb->child| will not be read. + // Due to C's lack of destructors and `CBB`'s auto-flushing API, a failing + // `CBB`-taking function may leave a dangling pointer to a child `CBB`. As a + // result, the convention is callers may not write to `CBB`s that have failed. + // But, as a safety measure, we lock the `CBB` into an error state. Once the + // error bit is set, `cbb->child` will not be read. // - // TODO(davidben): This still isn't quite ideal. A |CBB| function *outside* - // this file may originate an error while the |CBB| points to a local child. + // TODO(davidben): This still isn't quite ideal. A `CBB` function *outside* + // this file may originate an error while the `CBB` points to a local child. // In that case we don't set the error bit and are reliant on the error - // convention. Perhaps we allow |CBB_cleanup| on child |CBB|s and make every - // child's |CBB_cleanup| set the error bit if unflushed. That will be + // convention. Perhaps we allow `CBB_cleanup` on child `CBB`s and make every + // child's `CBB_cleanup` set the error bit if unflushed. That will be // convenient for C++ callers, but very tedious for C callers. So C callers - // perhaps should get a |CBB_on_error| function that can be, less tediously, - // stuck in a |goto err| block. + // perhaps should get a `CBB_on_error` function that can be, less tediously, + // stuck in a `goto err` block. cbb_get_base(cbb)->error = 1; // Clearing the pointer is not strictly necessary, but GCC's dangling pointer - // warning does not know |cbb->child| will not be read once |error| is set + // warning does not know `cbb->child` will not be read once `error` is set // above. cbb->child = nullptr; } @@ -184,11 +192,12 @@ static void cbb_on_error(CBB *cbb) { // current length of the underlying base is taken to be the length of the // length-prefixed data. int CBB_flush(CBB *cbb) { - // If |base| has hit an error, the buffer is in an undefined state, so - // fail all following calls. In particular, |cbb->child| may point to invalid + // If `base` has hit an error, the buffer is in an undefined state, so + // fail all following calls. In particular, `cbb->child` may point to invalid // memory. struct cbb_buffer_st *base = cbb_get_base(cbb); if (base == nullptr || base->error) { + OPENSSL_PUT_ERROR(CRYPTO, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); return 0; } @@ -203,8 +212,11 @@ int CBB_flush(CBB *cbb) { size_t child_start = child->offset + child->pending_len_len; size_t len; - if (!CBB_flush(cbb->child) || child_start < child->offset || - base->len < child_start) { + if (!CBB_flush(cbb->child)) { + goto err; + } + if (child_start < child->offset || base->len < child_start) { + OPENSSL_PUT_ERROR(CRYPTO, ERR_R_INTERNAL_ERROR); goto err; } @@ -338,7 +350,7 @@ int CBB_add_u24_length_prefixed(CBB *cbb, CBB *out_contents) { return cbb_add_length_prefixed(cbb, out_contents, 3); } -// add_base128_integer encodes |v| as a big-endian base-128 integer where the +// add_base128_integer encodes `v` as a big-endian base-128 integer where the // high bit of each byte indicates where there is more data. This is the // encoding used in DER for both high tag number form and OID components. static int add_base128_integer(CBB *cbb, uint64_t v) { @@ -364,6 +376,13 @@ static int add_base128_integer(CBB *cbb, uint64_t v) { return 1; } +int bssl::cbb_add_decimal_ascii(CBB *out, uint64_t v) { + char buf[DECIMAL_SIZE(uint64_t) + 1]; + snprintf(buf, sizeof(buf), "%" PRIu64, v); + return CBB_add_bytes(out, reinterpret_cast(buf), + strlen(buf)); +} + int CBB_add_asn1(CBB *cbb, CBB *out_contents, CBS_ASN1_TAG tag) { if (!CBB_flush(cbb)) { return 0; @@ -382,7 +401,7 @@ int CBB_add_asn1(CBB *cbb, CBB *out_contents, CBS_ASN1_TAG tag) { return 0; } - // Reserve one byte of length prefix. |CBB_flush| will finish it later. + // Reserve one byte of length prefix. `CBB_flush` will finish it later. return cbb_add_child(cbb, out_contents, /*len_len=*/1, /*is_asn1=*/1); } @@ -423,6 +442,7 @@ int CBB_did_write(CBB *cbb, size_t len) { struct cbb_buffer_st *base = cbb_get_base(cbb); size_t newlen = base->len + len; if (cbb->child != nullptr || newlen < base->len || newlen > base->cap) { + OPENSSL_PUT_ERROR(CRYPTO, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); return 0; } base->len = newlen; @@ -440,8 +460,9 @@ static int cbb_add_u(CBB *cbb, uint64_t v, size_t len_len) { v >>= 8; } - // |v| must fit in |len_len| bytes. + // `v` must fit in `len_len` bytes. if (v != 0) { + OPENSSL_PUT_ERROR(CRYPTO, ERR_R_OVERFLOW); cbb_on_error(cbb); return 0; } @@ -465,6 +486,8 @@ int CBB_add_u32le(CBB *cbb, uint32_t value) { return CBB_add_u32(cbb, CRYPTO_bswap4(value)); } +int CBB_add_u48(CBB *cbb, uint64_t value) { return cbb_add_u(cbb, value, 6); } + int CBB_add_u64(CBB *cbb, uint64_t value) { return cbb_add_u(cbb, value, 8); } int CBB_add_u64le(CBB *cbb, uint64_t value) { @@ -594,9 +617,9 @@ int CBB_add_asn1_bool(CBB *cbb, int value) { return 1; } -// parse_dotted_decimal parses one decimal component from |cbs|, where |cbs| is +// parse_dotted_decimal parses one decimal component from `cbs`, where `cbs` is // an OID literal, e.g., "1.2.840.113554.4.1.72585". It consumes both the -// component and the dot, so |cbs| may be passed into the function again for the +// component and the dot, so `cbs` may be passed into the function again for the // next value. static int parse_dotted_decimal(CBS *cbs, uint64_t *out) { if (!CBS_get_u64_decimal(cbs, out)) { @@ -624,8 +647,8 @@ int CBB_add_asn1_oid_from_text(CBB *cbb, const char *text, size_t len) { return 0; } - // The first component is encoded as 40 * |a| + |b|. This assumes that |a| is - // 0, 1, or 2 and that, when it is 0 or 1, |b| is at most 39. + // The first component is encoded as 40 * `a` + `b`. This assumes that `a` is + // 0, 1, or 2 and that, when it is 0 or 1, `b` is at most 39. if (a > 2 || (a < 2 && b > 39) || b > UINT64_MAX - 80 || !add_base128_integer(cbb, 40u * a + b)) { return 0; @@ -665,31 +688,32 @@ int CBB_add_asn1_relative_oid_from_text(CBB *cbb, const char *text, return 1; } -int CBB_add_asn1_oid_component(CBB *cbb, uint64_t value) { - if (!CBB_flush(cbb)) { +int CBB_add_asn1_relative_oid_from_der_to_text(CBB *cbb, const uint8_t *data, + size_t data_len) { + CBS der; + CBS_init(&der, data, data_len); + // Relative OIDs must have at least one component. + uint64_t v; + if (!CBS_get_asn1_oid_component(&der, &v) || !cbb_add_decimal_ascii(cbb, v)) { return 0; } - return add_base128_integer(cbb, value); + while (CBS_len(&der) != 0) { + if (!CBS_get_asn1_oid_component(&der, &v) || !CBB_add_u8(cbb, '.') || + !cbb_add_decimal_ascii(cbb, v)) { + return 0; + } + } + + return 1; } -static int compare_set_of_element(const void *a_ptr, const void *b_ptr) { - // See X.690, section 11.6 for the ordering. They are sorted in ascending - // order by their DER encoding. - const CBS *a = reinterpret_cast(a_ptr), - *b = reinterpret_cast(b_ptr); - size_t a_len = CBS_len(a), b_len = CBS_len(b); - size_t min_len = a_len < b_len ? a_len : b_len; - int ret = OPENSSL_memcmp(CBS_data(a), CBS_data(b), min_len); - if (ret != 0) { - return ret; - } - if (a_len == b_len) { +int CBB_add_asn1_oid_component(CBB *cbb, uint64_t value) { + if (!CBB_flush(cbb)) { return 0; } - // If one is a prefix of the other, the shorter one sorts first. (This is not - // actually reachable. No DER encoding is a prefix of another DER encoding.) - return a_len < b_len ? -1 : 1; + + return add_base128_integer(cbb, value); } int CBB_flush_asn1_set_of(CBB *cbb) { @@ -697,9 +721,8 @@ int CBB_flush_asn1_set_of(CBB *cbb) { return 0; } - CBS cbs; size_t num_children = 0; - CBS_init(&cbs, CBB_data(cbb), CBB_len(cbb)); + CBS cbs(CBBAsSpan(cbb)); while (CBS_len(&cbs) != 0) { if (!CBS_get_any_asn1_element(&cbs, nullptr, nullptr, nullptr)) { OPENSSL_PUT_ERROR(CRYPTO, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); @@ -713,47 +736,38 @@ int CBB_flush_asn1_set_of(CBB *cbb) { } // Parse out the children and sort. We alias them into a copy of so they - // remain valid as we rewrite |cbb|. - int ret = 0; - size_t buf_len = CBB_len(cbb); - uint8_t *buf = - reinterpret_cast(OPENSSL_memdup(CBB_data(cbb), buf_len)); - CBS *children = - reinterpret_cast(OPENSSL_calloc(num_children, sizeof(CBS))); - uint8_t *out; - size_t offset = 0; - if (buf == nullptr || children == nullptr) { - goto err; + // remain valid as we rewrite `cbb`. + Array copy; + Array children; + if (!copy.CopyFrom(CBBAsSpan(cbb)) || // + !children.Init(num_children)) { + return 0; } - CBS_init(&cbs, buf, buf_len); + cbs = CBS(copy); for (size_t i = 0; i < num_children; i++) { if (!CBS_get_any_asn1_element(&cbs, &children[i], nullptr, nullptr)) { - goto err; + OPENSSL_PUT_ERROR(CRYPTO, ERR_R_INTERNAL_ERROR); + return 0; } } - qsort(children, num_children, sizeof(CBS), compare_set_of_element); + std::sort(children.begin(), children.end(), bssl::cbs_asn1_set_of_less); // Write the contents back in the new order. - out = (uint8_t *)CBB_data(cbb); + uint8_t *out = const_cast(CBB_data(cbb)); + size_t offset = 0; for (size_t i = 0; i < num_children; i++) { OPENSSL_memcpy(out + offset, CBS_data(&children[i]), CBS_len(&children[i])); offset += CBS_len(&children[i]); } - assert(offset == buf_len); - - ret = 1; - -err: - OPENSSL_free(buf); - OPENSSL_free(children); - return ret; + assert(offset == copy.size()); + return 1; } bool bssl::CBBFinishArray(CBB *cbb, Array *out) { uint8_t *ptr; size_t len; if (!CBB_finish(cbb, &ptr, &len)) { - OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); + OPENSSL_PUT_ERROR(CRYPTO, ERR_R_INTERNAL_ERROR); return false; } out->Reset(ptr, len); diff --git a/third_party/boringssl/src/crypto/bytestring/cbs.cc b/third_party/boringssl/src/crypto/bytestring/cbs.cc index dbaa3e8af..e0f734609 100644 --- a/third_party/boringssl/src/crypto/bytestring/cbs.cc +++ b/third_party/boringssl/src/crypto/bytestring/cbs.cc @@ -18,9 +18,10 @@ #include #include -#include #include +#include + #include "../asn1/internal.h" #include "../internal.h" #include "internal.h" @@ -146,6 +147,8 @@ int CBS_get_u32le(CBS *cbs, uint32_t *out) { return 1; } +int CBS_get_u48(CBS *cbs, uint64_t *out) { return cbs_get_u(cbs, out, 6); } + int CBS_get_u64(CBS *cbs, uint64_t *out) { return cbs_get_u(cbs, out, 8); } int CBS_get_u64le(CBS *cbs, uint64_t *out) { @@ -188,7 +191,7 @@ static int cbs_get_length_prefixed(CBS *cbs, CBS *out, size_t len_len) { if (!cbs_get_u(cbs, &len, len_len)) { return 0; } - // If |len_len| <= 3 then we know that |len| will fit into a |size_t|, even on + // If `len_len` <= 3 then we know that `len` will fit into a `size_t`, even on // 32-bit systems. assert(len_len <= 3); return CBS_get_bytes(cbs, out, len); @@ -275,10 +278,7 @@ int CBS_get_u64_decimal(CBS *cbs, uint64_t *out) { return seen_digit; } -// parse_base128_integer reads a big-endian base-128 integer from |cbs| and sets -// |*out| to the result. This is the encoding used in DER for both high tag -// number form and OID components. -static int parse_base128_integer(CBS *cbs, uint64_t *out) { +int CBS_get_asn1_oid_component(CBS *cbs, uint64_t *out) { uint64_t v = 0; uint8_t b; do { @@ -317,8 +317,9 @@ static int parse_asn1_tag(CBS *cbs, CBS_ASN1_TAG *out) { CBS_ASN1_TAG tag = ((CBS_ASN1_TAG)tag_byte & 0xe0) << CBS_ASN1_TAG_SHIFT; CBS_ASN1_TAG tag_number = tag_byte & 0x1f; if (tag_number == 0x1f) { + // High tag numbers are encoded in the same format as OID components. uint64_t v; - if (!parse_base128_integer(cbs, &v) || + if (!CBS_get_asn1_oid_component(cbs, &v) || // Check the tag number is within our supported bounds. v > CBS_ASN1_TAG_NUMBER_MASK || // Small tag numbers should have used low tag number form, even in BER. @@ -411,7 +412,7 @@ static int cbs_get_any_asn1_element(CBS *cbs, CBS *out, CBS_ASN1_TAG *out_tag, // ITU-T X.690 section 10.1 (DER length forms) requires encoding the // length with the minimum number of octets. BER could, technically, have // 125 superfluous zero bytes. We do not attempt to handle that and still - // require that the length fit in a |uint32_t| for BER. + // require that the length fit in a `uint32_t` for BER. if (len64 < 128) { // Length should have used short-form encoding. if (ber_ok) { @@ -504,10 +505,20 @@ int CBS_get_asn1_element(CBS *cbs, CBS *out, CBS_ASN1_TAG tag_value) { return cbs_get_asn1(cbs, out, tag_value, 0 /* include header */); } -int CBS_peek_asn1_tag(const CBS *cbs, CBS_ASN1_TAG tag_value) { +CBS_ASN1_TAG CBS_peek_any_asn1_tag(const CBS *cbs) { CBS copy = *cbs; - CBS_ASN1_TAG actual_tag; - return parse_asn1_tag(©, &actual_tag) && tag_value == actual_tag; + CBS_ASN1_TAG tag; + if (!parse_asn1_tag(©, &tag)) { + return 0; + } + return tag; +} + +int CBS_peek_asn1_tag(const CBS *cbs, CBS_ASN1_TAG tag_value) { + CBS_ASN1_TAG actual_tag = CBS_peek_any_asn1_tag(cbs); + // The caller should never pass zero as |tag_value|, but return zero if they + // did. + return actual_tag != 0 && actual_tag == tag_value; } int CBS_get_asn1_uint64(CBS *cbs, uint64_t *out) { @@ -720,10 +731,38 @@ int CBS_is_unsigned_asn1_integer(const CBS *cbs) { return CBS_is_valid_asn1_integer(cbs, &is_negative) && !is_negative; } -static int add_decimal(CBB *out, uint64_t v) { - char buf[DECIMAL_SIZE(uint64_t) + 1]; - snprintf(buf, sizeof(buf), "%" PRIu64, v); - return CBB_add_bytes(out, (const uint8_t *)buf, strlen(buf)); +bool bssl::cbs_asn1_set_of_less(CBS a, CBS b) { + // See X.690, section 11.6 for the ordering. They are sorted in ascending + // order by their DER encoding. First compare the common prefix. + size_t a_len = CBS_len(&a), b_len = CBS_len(&b); + int cmp = OPENSSL_memcmp(CBS_data(&a), CBS_data(&b), std::min(a_len, b_len)); + if (cmp != 0) { + return cmp < 0; + } + // If one is a prefix of the other, the shorter one sorts first. (They're + // actually always equal length. DER encodings are prefix-free.) + return a_len < b_len; +} + +int CBS_is_valid_asn1_set_of(const CBS *cbs) { + CBS copy = *cbs; + if (CBS_len(©) == 0) { + return 1; + } + CBS prev, cur; + if (!CBS_get_any_asn1_element(©, &prev, nullptr, nullptr)) { + return 0; + } + while (CBS_len(©) != 0) { + if (!CBS_get_any_asn1_element(©, &cur, nullptr, nullptr) || + // Allow duplicate entries; only reject if the current member should go + // strictly before the previous. + cbs_asn1_set_of_less(cur, prev)) { + return 0; + } + prev = cur; + } + return 1; } int CBS_is_valid_asn1_oid(const CBS *cbs) { @@ -735,9 +774,9 @@ int CBS_is_valid_asn1_oid(const CBS *cbs) { uint8_t v, prev = 0; while (CBS_get_u8(©, &v)) { // OID encodings are a sequence of minimally-encoded base-128 integers (see - // |parse_base128_integer|). If |prev|'s MSB was clear, it was the last byte - // of an integer (or |v| is the first byte). |v| is then the first byte of - // the next integer. If first byte of an integer is 0x80, it is not + // `CBS_get_asn1_oid_component`). If `prev`'s MSB was clear, it was the last + // byte of an integer (or `v` is the first byte). `v` is then the first byte + // of the next integer. If first byte of an integer is 0x80, it is not // minimally-encoded. if ((prev & 0x80) == 0 && v == 0x80) { return 0; @@ -758,23 +797,23 @@ char *CBS_asn1_oid_to_text(const CBS *cbs) { // The first component is 40 * value1 + value2, where value1 is 0, 1, or 2. uint64_t v; - if (!parse_base128_integer(©, &v)) { + if (!CBS_get_asn1_oid_component(©, &v)) { goto err; } if (v >= 80) { if (!CBB_add_bytes(&cbb, (const uint8_t *)"2.", 2) || - !add_decimal(&cbb, v - 80)) { + !cbb_add_decimal_ascii(&cbb, v - 80)) { goto err; } - } else if (!add_decimal(&cbb, v / 40) || !CBB_add_u8(&cbb, '.') || - !add_decimal(&cbb, v % 40)) { + } else if (!cbb_add_decimal_ascii(&cbb, v / 40) || !CBB_add_u8(&cbb, '.') || + !cbb_add_decimal_ascii(&cbb, v % 40)) { goto err; } while (CBS_len(©) != 0) { - if (!parse_base128_integer(©, &v) || !CBB_add_u8(&cbb, '.') || - !add_decimal(&cbb, v)) { + if (!CBS_get_asn1_oid_component(©, &v) || !CBB_add_u8(&cbb, '.') || + !cbb_add_decimal_ascii(&cbb, v)) { goto err; } } @@ -797,25 +836,15 @@ int CBS_is_valid_asn1_relative_oid(const CBS *cbs) { } char *CBS_asn1_relative_oid_to_text(const CBS *cbs) { - CBS copy = *cbs; ScopedCBB cbb; if (!CBB_init(cbb.get(), 32)) { return nullptr; } - - // Relative OIDs must have at least one component. - uint64_t v; - if (!parse_base128_integer(©, &v) || !add_decimal(cbb.get(), v)) { + if (!CBB_add_asn1_relative_oid_from_der_to_text(cbb.get(), CBS_data(cbs), + CBS_len(cbs))) { return nullptr; } - while (CBS_len(©) != 0) { - if (!parse_base128_integer(©, &v) || !CBB_add_u8(cbb.get(), '.') || - !add_decimal(cbb.get(), v)) { - return nullptr; - } - } - uint8_t *txt; size_t txt_len; if (!CBB_add_u8(cbb.get(), '\0') || !CBB_finish(cbb.get(), &txt, &txt_len)) { @@ -918,10 +947,10 @@ static int CBS_parse_rfc5280_time_internal(const CBS *cbs, int is_gentime, case 'Z': break; // We correctly have 'Z' on the end as per spec. case '+': - offset_sign = 1; + offset_sign = -1; break; // Should not be allowed per RFC 5280. case '-': - offset_sign = -1; + offset_sign = 1; break; // Should not be allowed per RFC 5280. default: return 0; // Reject anything else after the time. @@ -930,7 +959,7 @@ static int CBS_parse_rfc5280_time_internal(const CBS *cbs, int is_gentime, // If allow_timezone_offset is non-zero, allow for a four digit timezone // offset to be specified even though this is not allowed by RFC 5280. We are // permissive of this for UTCTimes due to the unfortunate existence of - // artisinally rolled long lived certificates that were baked into places that + // artisanally rolled long lived certificates that were baked into places that // are now difficult to change. These certificates were generated with the // 'openssl' command that permissively allowed the creation of certificates // with notBefore and notAfter times specified as strings for direct diff --git a/third_party/boringssl/src/crypto/bytestring/internal.h b/third_party/boringssl/src/crypto/bytestring/internal.h index 35bd4c9c9..de2fcd2e8 100644 --- a/third_party/boringssl/src/crypto/bytestring/internal.h +++ b/third_party/boringssl/src/crypto/bytestring/internal.h @@ -26,18 +26,18 @@ BSSL_NAMESPACE_BEGIN -// CBS_asn1_ber_to_der reads a BER element from |in|. If it finds +// CBS_asn1_ber_to_der reads a BER element from `in`. If it finds // indefinite-length elements or constructed strings then it converts the BER -// data to DER, sets |out| to the converted contents and |*out_storage| to a -// buffer which the caller must release with |OPENSSL_free|. Otherwise, it sets -// |out| to the original BER element in |in| and |*out_storage| to NULL. -// Additionally, |*in| will be advanced over the BER element. +// data to DER, sets `out` to the converted contents and `*out_storage` to a +// buffer which the caller must release with `OPENSSL_free`. Otherwise, it sets +// `out` to the original BER element in `in` and `*out_storage` to NULL. +// Additionally, `*in` will be advanced over the BER element. // // This function should successfully process any valid BER input, however it // will not convert all of BER's deviations from DER. BER is ambiguous between // implicitly-tagged SEQUENCEs of strings and implicitly-tagged constructed // strings. Implicitly-tagged strings must be parsed with -// |CBS_get_ber_implicitly_tagged_string| instead of |CBS_get_asn1|. The caller +// `CBS_get_ber_implicitly_tagged_string` instead of `CBS_get_asn1`. The caller // must also account for BER variations in the contents of a primitive. // // It returns one on success and zero otherwise. @@ -45,15 +45,15 @@ OPENSSL_EXPORT int CBS_asn1_ber_to_der(CBS *in, CBS *out, uint8_t **out_storage); // CBS_get_asn1_implicit_string parses a BER string of primitive type -// |inner_tag| implicitly-tagged with |outer_tag|. It sets |out| to the -// contents. If concatenation was needed, it sets |*out_storage| to a buffer -// which the caller must release with |OPENSSL_free|. Otherwise, it sets -// |*out_storage| to NULL. +// `inner_tag` implicitly-tagged with `outer_tag`. It sets `out` to the +// contents. If concatenation was needed, it sets `*out_storage` to a buffer +// which the caller must release with `OPENSSL_free`. Otherwise, it sets +// `*out_storage` to NULL. // // This function does not parse all of BER. It requires the string be // definite-length. Constructed strings are allowed, but all children of the // outermost element must be primitive. The caller should use -// |CBS_asn1_ber_to_der| before running this function. +// `CBS_asn1_ber_to_der` before running this function. // // It returns one on success and zero otherwise. OPENSSL_EXPORT int CBS_get_asn1_implicit_string(CBS *in, CBS *out, @@ -61,17 +61,16 @@ OPENSSL_EXPORT int CBS_get_asn1_implicit_string(CBS *in, CBS *out, CBS_ASN1_TAG outer_tag, CBS_ASN1_TAG inner_tag); -// CBB_finish_i2d calls |CBB_finish| on |cbb| which must have been initialized -// with |CBB_init|. If |outp| is not NULL then the result is written to |*outp| -// and |*outp| is advanced just past the output. It returns the number of bytes -// in the result, whether written or not, or a negative value on error. On -// error, it calls |CBB_cleanup| on |cbb|. +// CBB_finish_i2d calls `CBB_finish` on `cbb` which must have been initialized +// with `CBB_init`. If `outp` is not NULL then the result is written to `*outp` +// and `*outp` is advanced just past the output. It returns the number of bytes +// in the result, whether written or not, or a negative value on error. // // This function may be used to help implement legacy i2d ASN.1 functions. OPENSSL_EXPORT int CBB_finish_i2d(CBB *cbb, uint8_t **outp); -// CBBAsSpan returns a span containing |cbb|'s contents. It does not flush -// |cbb|. The span is valid until the next operation to |cbb|. +// CBBAsSpan returns a span containing `cbb`'s contents. It does not flush +// `cbb`. The span is valid until the next operation to `cbb`. // // To avoid unfinalized length prefixes, it is a fatal error to call this on a // CBB with any active children. @@ -79,14 +78,14 @@ inline Span CBBAsSpan(const CBB *cbb) { return Span(CBB_data(cbb), CBB_len(cbb)); } -// CBBFinishArray behaves like |CBB_finish| but stores the result in an Array. +// CBBFinishArray behaves like `CBB_finish` but stores the result in an Array. OPENSSL_EXPORT bool CBBFinishArray(CBB *cbb, Array *out); -// D2IFromCBS takes a functor of type |Unique(CBS*)| and implements the d2i +// D2IFromCBS takes a functor of type `Unique(CBS*)` and implements the d2i // calling convention. For compatibility with functions that don't tag their -// return value (e.g. public APIs), |T*(CBS)| is also accepted. The callback can -// assume that the |CBS|'s length fits in |long|. The callback should not access -// |out|, |inp|, or |len| directly. +// return value (e.g. public APIs), `T*(CBS)` is also accepted. The callback can +// assume that the `CBS`'s length fits in `long`. The callback should not access +// `out`, `inp`, or `len` directly. template inline T *D2IFromCBS(T **out, const uint8_t **inp, long len, CBSFunc func) { static_assert(std::is_invocable_v); @@ -111,9 +110,9 @@ inline T *D2IFromCBS(T **out, const uint8_t **inp, long len, CBSFunc func) { return ret.release(); } -// I2DFromCBB takes a functor of type |bool(CBB*)| and implements the i2d -// calling convention. It internally makes a |CBB| with the specified initial -// capacity. The callback should not access |outp| directly. +// I2DFromCBB takes a functor of type `bool(CBB*)` and implements the i2d +// calling convention. It internally makes a `CBB` with the specified initial +// capacity. The callback should not access `outp` directly. template inline int I2DFromCBB(size_t initial_capacity, uint8_t **outp, CBBFunc func) { static_assert(std::is_invocable_v); @@ -125,6 +124,16 @@ inline int I2DFromCBB(size_t initial_capacity, uint8_t **outp, CBBFunc func) { return CBB_finish_i2d(cbb.get(), outp); } +// cbb_add_decimal_ascii adds the characters of an ASCII string representation +// of `v` as a decimal value to `out` (without a terminating NUL byte). It +// returns one on success and zero on error. +int cbb_add_decimal_ascii(CBB *out, uint64_t v); + +// cbs_asn1_set_of_less returns true iff `a` should be sorted before `b` +// according to the DER rules for encoding an ASN.1 SET OF, of which `a` and `b` +// are members. +bool cbs_asn1_set_of_less(CBS a, CBS b); + BSSL_NAMESPACE_END #endif // OPENSSL_HEADER_CRYPTO_BYTESTRING_INTERNAL_H diff --git a/third_party/boringssl/src/crypto/bytestring/unicode.cc b/third_party/boringssl/src/crypto/bytestring/unicode.cc index 74f558eb6..6293febab 100644 --- a/third_party/boringssl/src/crypto/bytestring/unicode.cc +++ b/third_party/boringssl/src/crypto/bytestring/unicode.cc @@ -33,10 +33,10 @@ static int is_valid_code_point(uint32_t v) { return 1; } -// BOTTOM_BITS returns a byte with the bottom |n| bits set. +// BOTTOM_BITS returns a byte with the bottom `n` bits set. #define BOTTOM_BITS(n) (uint8_t)((1u << (n)) - 1) -// TOP_BITS returns a byte with the top |n| bits set. +// TOP_BITS returns a byte with the top `n` bits set. #define TOP_BITS(n) ((uint8_t)~BOTTOM_BITS(8 - (n))) int CBS_get_utf8(CBS *cbs, uint32_t *out) { diff --git a/third_party/boringssl/src/crypto/chacha/chacha.cc b/third_party/boringssl/src/crypto/chacha/chacha.cc index 6e444411b..808a975de 100644 --- a/third_party/boringssl/src/crypto/chacha/chacha.cc +++ b/third_party/boringssl/src/crypto/chacha/chacha.cc @@ -150,7 +150,7 @@ void CRYPTO_chacha_20(uint8_t *out, const uint8_t *in, size_t in_len, #else // chacha_core performs 20 rounds of ChaCha on the input words in -// |input| and writes the 64 output bytes to |output|. +// `input` and writes the 64 output bytes to `output`. static void chacha_core(uint8_t output[64], const uint32_t input[16]) { uint32_t x[16]; int i; diff --git a/third_party/boringssl/src/crypto/chacha/internal.h b/third_party/boringssl/src/crypto/chacha/internal.h index a758055d1..04c20a46f 100644 --- a/third_party/boringssl/src/crypto/chacha/internal.h +++ b/third_party/boringssl/src/crypto/chacha/internal.h @@ -84,11 +84,11 @@ extern "C" void ChaCha20_ctr32_ssse3(uint8_t *out, const uint8_t *in, #endif #if defined(CHACHA20_ASM_NOHW) -// ChaCha20_ctr32_nohw encrypts |in_len| bytes from |in| and writes the result -// to |out|. If |in| and |out| alias, they must be equal. |in_len| may not be +// ChaCha20_ctr32_nohw encrypts `in_len` bytes from `in` and writes the result +// to `out`. If `in` and `out` alias, they must be equal. `in_len` may not be // zero. // -// |counter[0]| is the initial 32-bit block counter, and the remainder is the +// `counter[0]` is the initial 32-bit block counter, and the remainder is the // 96-bit nonce. If the counter overflows, the output is undefined. The function // will produce output, but the output may vary by machine and may not be // self-consistent. (On some architectures, the assembly implements a mix of diff --git a/third_party/boringssl/src/crypto/cipher/e_aesctrhmac.cc b/third_party/boringssl/src/crypto/cipher/e_aesctrhmac.cc index 019d9e607..2bb1db6ec 100644 --- a/third_party/boringssl/src/crypto/cipher/e_aesctrhmac.cc +++ b/third_party/boringssl/src/crypto/cipher/e_aesctrhmac.cc @@ -221,6 +221,13 @@ static int aead_aes_ctr_hmac_sha256_openv_detached( Span aadvecs) { const struct aead_aes_ctr_hmac_sha256_ctx *aes_ctx = (struct aead_aes_ctr_hmac_sha256_ctx *)&ctx->state; + const uint64_t in_len_64 = bssl::iovec::TotalLength(iovecs); + + if (in_len_64 >= (UINT64_C(1) << 32) * AES_BLOCK_SIZE) { + // This input is so large it would overflow the 32-bit block counter. + OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT); + return 0; + } if (in_tag.size() != ctx->tag_len) { OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT); diff --git a/third_party/boringssl/src/crypto/cipher/e_aeseax.cc b/third_party/boringssl/src/crypto/cipher/e_aeseax.cc index f85da3d68..0a5cfc08f 100644 --- a/third_party/boringssl/src/crypto/cipher/e_aeseax.cc +++ b/third_party/boringssl/src/crypto/cipher/e_aeseax.cc @@ -201,7 +201,7 @@ static void omac_with_tag_iovec_out(const struct aead_aes_eax_ctx *aes_ctx, }); } -// Encrypts/decrypts |in_len| bytes from |in| to |out| using AES-CTR with |n| as +// Encrypts/decrypts `in_len` bytes from `in` to `out` using AES-CTR with `n` as // the IV. static void aes_ctr(const struct aead_aes_eax_ctx *aes_ctx, Span iovecs, @@ -273,9 +273,7 @@ static int aead_aes_eax_openv_detached(const EVP_AEAD_CTX *ctx, return 0; } - const uint64_t in_len_64 = bssl::iovec::TotalLength(iovecs); - if (in_tag.size() != EVP_AEAD_AES_EAX_TAG_LEN || - in_len_64 > (UINT64_C(1) << 36) + AES_BLOCK_SIZE) { + if (in_tag.size() != EVP_AEAD_AES_EAX_TAG_LEN) { OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT); return 0; } diff --git a/third_party/boringssl/src/crypto/cipher/e_aesgcmsiv.cc b/third_party/boringssl/src/crypto/cipher/e_aesgcmsiv.cc index cc80306b2..cd4165294 100644 --- a/third_party/boringssl/src/crypto/cipher/e_aesgcmsiv.cc +++ b/third_party/boringssl/src/crypto/cipher/e_aesgcmsiv.cc @@ -24,6 +24,7 @@ #include "../fipsmodule/aes/internal.h" #include "../fipsmodule/cipher/internal.h" #include "../internal.h" +#include "internal.h" using namespace bssl; @@ -36,155 +37,17 @@ void inc_counter(uint8_t tag[16], uint32_t by) { CRYPTO_store_u32_le(tag, CRYPTO_load_u32_le(tag) + by); } -// TODO(davidben): AES-GCM-SIV assembly is not correct for Windows. It must save -// and restore xmm6 through xmm15. -#if defined(OPENSSL_X86_64) && !defined(OPENSSL_NO_ASM) && \ - !defined(OPENSSL_WINDOWS) -#define AES_GCM_SIV_ASM - -// Optimised AES-GCM-SIV - -struct aead_aes_gcm_siv_asm_ctx { - alignas(16) uint8_t key[16 * 15]; - int is_128_bit; -}; +#if defined(AES_GCM_SIV_ASM) // The assembly code assumes 8-byte alignment of the EVP_AEAD_CTX's state, and // aligns to 16 bytes itself. -static_assert(sizeof(((EVP_AEAD_CTX *)nullptr)->state) + 8 >= - sizeof(struct aead_aes_gcm_siv_asm_ctx), +static_assert(sizeof(((EVP_AEAD_CTX *)nullptr)->state) >= + sizeof(struct aead_aes_gcm_siv_asm_ctx) + 8, "AEAD state is too small"); static_assert(alignof(union evp_aead_ctx_st_state) >= 8, "AEAD state has insufficient alignment"); -extern "C" { -// aes128gcmsiv_aes_ks writes an AES-128 key schedule for |key| to -// |out_expanded_key|. -extern void aes128gcmsiv_aes_ks(const uint8_t key[16], - uint8_t out_expanded_key[16 * 15]); - -// aes256gcmsiv_aes_ks writes an AES-256 key schedule for |key| to -// |out_expanded_key|. -extern void aes256gcmsiv_aes_ks(const uint8_t key[32], - uint8_t out_expanded_key[16 * 15]); - -// aesgcmsiv_polyval_horner updates the POLYVAL value in |in_out_poly| to -// include a number (|in_blocks|) of 16-byte blocks of data from |in|, given -// the POLYVAL key in |key|. -extern void aesgcmsiv_polyval_horner(const uint8_t in_out_poly[16], - const uint8_t key[16], const uint8_t *in, - size_t in_blocks); - -// aesgcmsiv_htable_init writes powers 1..8 of |auth_key| to |out_htable|. -extern void aesgcmsiv_htable_init(uint8_t out_htable[16 * 8], - const uint8_t auth_key[16]); - -// aesgcmsiv_htable6_init writes powers 1..6 of |auth_key| to |out_htable|. -extern void aesgcmsiv_htable6_init(uint8_t out_htable[16 * 6], - const uint8_t auth_key[16]); - -// aesgcmsiv_htable_polyval updates the POLYVAL value in |in_out_poly| to -// include |in_len| bytes of data from |in|. (Where |in_len| must be a multiple -// of 16.) It uses the precomputed powers of the key given in |htable|. -extern void aesgcmsiv_htable_polyval(const uint8_t htable[16 * 8], - const uint8_t *in, size_t in_len, - uint8_t in_out_poly[16]); - -// aes128gcmsiv_dec decrypts |in_len| & ~15 bytes from |out| and writes them to -// |in|. |in| and |out| may be equal, but must not otherwise alias. -// -// |in_out_calculated_tag_and_scratch|, on entry, must contain: -// 1. The current value of the calculated tag, which will be updated during -// decryption and written back to the beginning of this buffer on exit. -// 2. The claimed tag, which is needed to derive counter values. -// -// While decrypting, the whole of |in_out_calculated_tag_and_scratch| may be -// used for other purposes. In order to decrypt and update the POLYVAL value, it -// uses the expanded key from |key| and the table of powers in |htable|. -extern void aes128gcmsiv_dec(const uint8_t *in, uint8_t *out, - uint8_t in_out_calculated_tag_and_scratch[16 * 8], - const uint8_t htable[16 * 6], - const struct aead_aes_gcm_siv_asm_ctx *key, - size_t in_len); - -// aes256gcmsiv_dec acts like |aes128gcmsiv_dec|, but for AES-256. -extern void aes256gcmsiv_dec(const uint8_t *in, uint8_t *out, - uint8_t in_out_calculated_tag_and_scratch[16 * 8], - const uint8_t htable[16 * 6], - const struct aead_aes_gcm_siv_asm_ctx *key, - size_t in_len); - -// aes128gcmsiv_kdf performs the AES-GCM-SIV KDF given the expanded key from -// |key_schedule| and the nonce in |nonce|. Note that, while only 12 bytes of -// the nonce are used, 16 bytes are read and so the value must be -// right-padded. -extern void aes128gcmsiv_kdf(const uint8_t nonce[16], - uint64_t out_key_material[8], - const uint8_t *key_schedule); - -// aes256gcmsiv_kdf acts like |aes128gcmsiv_kdf|, but for AES-256. -extern void aes256gcmsiv_kdf(const uint8_t nonce[16], - uint64_t out_key_material[12], - const uint8_t *key_schedule); - -// aes128gcmsiv_aes_ks_enc_x1 performs a key expansion of the AES-128 key in -// |key|, writes the expanded key to |out_expanded_key| and encrypts a single -// block from |in| to |out|. -extern void aes128gcmsiv_aes_ks_enc_x1(const uint8_t in[16], uint8_t out[16], - uint8_t out_expanded_key[16 * 15], - const uint64_t key[2]); - -// aes256gcmsiv_aes_ks_enc_x1 acts like |aes128gcmsiv_aes_ks_enc_x1|, but for -// AES-256. -extern void aes256gcmsiv_aes_ks_enc_x1(const uint8_t in[16], uint8_t out[16], - uint8_t out_expanded_key[16 * 15], - const uint64_t key[4]); - -// aes128gcmsiv_ecb_enc_block encrypts a single block from |in| to |out| using -// the expanded key in |expanded_key|. -extern void aes128gcmsiv_ecb_enc_block( - const uint8_t in[16], uint8_t out[16], - const struct aead_aes_gcm_siv_asm_ctx *expanded_key); - -// aes256gcmsiv_ecb_enc_block acts like |aes128gcmsiv_ecb_enc_block|, but for -// AES-256. -extern void aes256gcmsiv_ecb_enc_block( - const uint8_t in[16], uint8_t out[16], - const struct aead_aes_gcm_siv_asm_ctx *expanded_key); - -// aes128gcmsiv_enc_msg_x4 encrypts |in_len| bytes from |in| to |out| using the -// expanded key from |key|. (The value of |in_len| must be a multiple of 16.) -// The |in| and |out| buffers may be equal but must not otherwise overlap. The -// initial counter is constructed from the given |tag| as required by -// AES-GCM-SIV. -extern void aes128gcmsiv_enc_msg_x4(const uint8_t *in, uint8_t *out, - const uint8_t *tag, - const struct aead_aes_gcm_siv_asm_ctx *key, - size_t in_len); - -// aes256gcmsiv_enc_msg_x4 acts like |aes128gcmsiv_enc_msg_x4|, but for -// AES-256. -extern void aes256gcmsiv_enc_msg_x4(const uint8_t *in, uint8_t *out, - const uint8_t *tag, - const struct aead_aes_gcm_siv_asm_ctx *key, - size_t in_len); - -// aes128gcmsiv_enc_msg_x8 acts like |aes128gcmsiv_enc_msg_x4|, but is -// optimised for longer messages. -extern void aes128gcmsiv_enc_msg_x8(const uint8_t *in, uint8_t *out, - const uint8_t *tag, - const struct aead_aes_gcm_siv_asm_ctx *key, - size_t in_len); - -// aes256gcmsiv_enc_msg_x8 acts like |aes256gcmsiv_enc_msg_x4|, but is -// optimised for longer messages. -extern void aes256gcmsiv_enc_msg_x8(const uint8_t *in, uint8_t *out, - const uint8_t *tag, - const struct aead_aes_gcm_siv_asm_ctx *key, - size_t in_len); -} - -// asm_ctx_from_ctx returns a 16-byte aligned context pointer from |ctx|. +// asm_ctx_from_ctx returns a 16-byte aligned context pointer from `ctx`. struct aead_aes_gcm_siv_asm_ctx *asm_ctx_from_ctx(const EVP_AEAD_CTX *ctx) { // ctx->state must already be 8-byte aligned. Thus, at most, we may need to // add eight to align it to 16 bytes. @@ -228,8 +91,8 @@ int aead_aes_gcm_siv_asm_init(EVP_AEAD_CTX *ctx, const uint8_t *key, void aead_aes_gcm_siv_asm_cleanup(EVP_AEAD_CTX *ctx) {} -// gcm_siv_asm_polyval evaluates POLYVAL at |auth_key| on the given plaintext -// and AD. The result is written to |out_tag|. +// gcm_siv_asm_polyval evaluates POLYVAL at `auth_key` on the given plaintext +// and AD. The result is written to `out_tag`, which must be 16-byte aligned. void gcm_siv_asm_polyval(uint8_t out_tag[16], Span iovecs, Span aadvecs, const uint8_t auth_key[16], const uint8_t nonce[12]) { @@ -271,11 +134,11 @@ void gcm_siv_asm_polyval(uint8_t out_tag[16], Span iovecs, return true; }; - bssl::iovec::ForEachBlockRange(aadvecs, f_whole, - f_final); + bssl::iovec::ForEachBlockRange( + aadvecs, f_whole, f_final); - bssl::iovec::ForEachBlockRange(iovecs, f_whole, - f_final); + bssl::iovec::ForEachBlockRange( + iovecs, f_whole, f_final); uint8_t length_block[16]; CRYPTO_store_u64_le(length_block, ad_len * 8); @@ -291,8 +154,8 @@ void gcm_siv_asm_polyval(uint8_t out_tag[16], Span iovecs, // aead_aes_gcm_siv_asm_crypt_last_block handles the encryption/decryption // (same thing in CTR mode) of the final block of a plaintext/ciphertext. It -// writes |total_in_len| & 15 bytes to |out_last_block|, based on an initial -// counter derived from |tag|. +// writes `total_in_len` & 15 bytes to `out_last_block`, based on an initial +// counter derived from `tag`. void aead_aes_gcm_siv_asm_crypt_last_block( int is_128_bit, uint8_t *out_last_block, const uint8_t *in_last_block, size_t total_in_len, const uint8_t tag[16], @@ -315,13 +178,13 @@ void aead_aes_gcm_siv_asm_crypt_last_block( } // aead_aes_gcm_siv_kdf calculates the record encryption and authentication -// keys given the |nonce|. +// keys given the `nonce`. void aead_aes_gcm_siv_kdf(int is_128_bit, const struct aead_aes_gcm_siv_asm_ctx *gcm_siv_ctx, uint64_t out_record_auth_key[2], uint64_t out_record_enc_key[4], const uint8_t nonce[12]) { - alignas(16) uint8_t padded_nonce[16]; + alignas(16) uint8_t padded_nonce[16] = {0}; OPENSSL_memcpy(padded_nonce, nonce, 12); alignas(16) uint64_t key_material[12]; @@ -648,7 +511,7 @@ const EVP_AEAD aead_aes_256_gcm_siv_asm = { nullptr /* tag_len */, }; -#endif // X86_64 && !NO_ASM && !WINDOWS +#endif // AES_GCM_SIV_ASM struct aead_aes_gcm_siv_ctx { @@ -698,12 +561,12 @@ int aead_aes_gcm_siv_init(EVP_AEAD_CTX *ctx, const uint8_t *key, size_t key_len, void aead_aes_gcm_siv_cleanup(EVP_AEAD_CTX *ctx) {} -// gcm_siv_crypt encrypts (or decrypts—it's the same thing) bytes from |in| to -// |out| in the |iovec|, using the block function |enc_block| with |key| in -// counter mode, starting at |initial_counter|. This differs from the +// gcm_siv_crypt encrypts (or decrypts—it's the same thing) bytes from `in` to +// `out` in the `iovec`, using the block function `enc_block` with `key` in +// counter mode, starting at `initial_counter`. This differs from the // traditional counter mode code in that the counter is handled little-endian, // only the first four bytes are used and the GCM-SIV tweak to the final byte -// is applied. The |in| and |out| pointers may be equal but otherwise must not +// is applied. The `in` and `out` pointers may be equal but otherwise must not // alias. void gcm_siv_crypt(Span iovecs, const uint8_t initial_counter[AES_BLOCK_SIZE], @@ -744,9 +607,9 @@ void gcm_siv_crypt(Span iovecs, // similar to the one that GHASH uses. See // https://www.rfc-editor.org/rfc/rfc8452.html#section-3. -// POLYVAL(H, X_1, ..., X_n) = -// ByteReverse(GHASH(mulX_GHASH(ByteReverse(H)), ByteReverse(X_1), ..., -// ByteReverse(X_n))). +// POLYVAL(H, X₁, ..., Xₙ) = +// ByteReverse(GHASH(mulX_GHASH(ByteReverse(H)), ByteReverse(X₁), ..., +// ByteReverse(Xₙ))). // // See https://www.rfc-editor.org/rfc/rfc8452.html#appendix-A. @@ -757,7 +620,7 @@ struct polyval_ctx { ghash_func ghash; }; -// byte_reverse reverses the order of the bytes in |b->c|. +// byte_reverse reverses the order of the bytes in `b->c`. void byte_reverse(uint8_t b[16]) { uint64_t hi = CRYPTO_load_u64_le(b); uint64_t lo = CRYPTO_load_u64_le(b + 8); @@ -765,8 +628,8 @@ void byte_reverse(uint8_t b[16]) { CRYPTO_store_u64_le(b + 8, CRYPTO_bswap8(hi)); } -// reverse_and_mulX_ghash interprets |b| as a reversed element of the GHASH -// field, multiplies that by 'x' and serialises the result back into |b|, but +// reverse_and_mulX_ghash interprets `b` as a reversed element of the GHASH +// field, multiplies that by 'x' and serialises the result back into `b`, but // with GHASH's backwards bit ordering. void reverse_and_mulX_ghash(uint8_t b[16]) { uint64_t hi = CRYPTO_load_u64_le(b); @@ -818,8 +681,8 @@ void crypto_polyval_finish(const struct polyval_ctx *ctx, uint8_t out[16]) { byte_reverse(out); } -// gcm_siv_polyval evaluates POLYVAL at |auth_key| on the given plaintext and -// AD. The result is written to |out_tag|. +// gcm_siv_polyval evaluates POLYVAL at `auth_key` on the given plaintext and +// AD. The result is written to `out_tag`. void gcm_siv_polyval(uint8_t out_tag[16], Span iovecs, bool encrypt, Span aadvecs, const uint8_t auth_key[16], @@ -847,8 +710,8 @@ void gcm_siv_polyval(uint8_t out_tag[16], Span iovecs, return true; }; - bssl::iovec::ForEachBlockRange(aadvecs, f_whole, - f_final); + bssl::iovec::ForEachBlockRange( + aadvecs, f_whole, f_final); if (encrypt) { bssl::iovec::ForEachBlockRange( @@ -883,7 +746,7 @@ struct gcm_siv_record_keys { }; // gcm_siv_keys calculates the keys for a specific GCM-SIV record with the -// given nonce and writes them to |*out_keys|. +// given nonce and writes them to `*out_keys`. void gcm_siv_keys(const struct aead_aes_gcm_siv_ctx *gcm_siv_ctx, struct gcm_siv_record_keys *out_keys, const uint8_t nonce[EVP_AEAD_AES_GCM_SIV_NONCE_LEN]) { @@ -904,13 +767,13 @@ void gcm_siv_keys(const struct aead_aes_gcm_siv_ctx *gcm_siv_ctx, } OPENSSL_memcpy(out_keys->auth_key, key_material, 16); - // Note the |ctr128_f| function uses a big-endian couner, while AES-GCM-SIV + // Note the `ctr128_f` function uses a big-endian counter, while AES-GCM-SIV // uses a little-endian counter. We ignore the return value and only use - // |block128_f|. This has a significant performance cost for the fallback + // `block128_f`. This has a significant performance cost for the fallback // bitsliced AES implementations (bsaes and aes_nohw). // // We currently do not consider AES-GCM-SIV to be performance-sensitive on - // client hardware. If this changes, we can write little-endian |ctr128_f| + // client hardware. If this changes, we can write little-endian `ctr128_f` // functions. aes_ctr_set_key(&out_keys->enc_key.ks, nullptr, &out_keys->enc_block, key_material + 16, gcm_siv_ctx->is_256 ? 32 : 16); @@ -972,7 +835,7 @@ int aead_aes_gcm_siv_openv_detached(const EVP_AEAD_CTX *ctx, const uint64_t in_len_64 = bssl::iovec::TotalLength(iovecs); if (in_tag.size() != EVP_AEAD_AES_GCM_SIV_TAG_LEN || - in_len_64 > (UINT64_C(1) << 36) + AES_BLOCK_SIZE) { + in_len_64 > (UINT64_C(1) << 36)) { OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT); return 0; } @@ -1036,26 +899,20 @@ const EVP_AEAD aead_aes_256_gcm_siv = { }; } // namespace -#if defined(AES_GCM_SIV_ASM) - const EVP_AEAD *EVP_aead_aes_128_gcm_siv() { - if (CRYPTO_is_AVX_capable() && CRYPTO_is_AESNI_capable()) { +#if defined(AES_GCM_SIV_ASM) + if (aes_gcm_siv_asm_capable()) { return &aead_aes_128_gcm_siv_asm; } +#endif return &aead_aes_128_gcm_siv; } const EVP_AEAD *EVP_aead_aes_256_gcm_siv() { - if (CRYPTO_is_AVX_capable() && CRYPTO_is_AESNI_capable()) { +#if defined(AES_GCM_SIV_ASM) + if (aes_gcm_siv_asm_capable()) { return &aead_aes_256_gcm_siv_asm; } +#endif return &aead_aes_256_gcm_siv; } - -#else - -const EVP_AEAD *EVP_aead_aes_128_gcm_siv() { return &aead_aes_128_gcm_siv; } - -const EVP_AEAD *EVP_aead_aes_256_gcm_siv() { return &aead_aes_256_gcm_siv; } - -#endif // AES_GCM_SIV_ASM diff --git a/third_party/boringssl/src/crypto/cipher/e_chacha20poly1305.cc b/third_party/boringssl/src/crypto/cipher/e_chacha20poly1305.cc index 4f99f6eca..69b707141 100644 --- a/third_party/boringssl/src/crypto/cipher/e_chacha20poly1305.cc +++ b/third_party/boringssl/src/crypto/cipher/e_chacha20poly1305.cc @@ -79,7 +79,7 @@ static void poly1305_update_length(poly1305_state *poly1305, size_t data_len) { CRYPTO_poly1305_update(poly1305, length_bytes, sizeof(length_bytes)); } -// calc_tag_pre prepares filling |tag| with the authentication tag for the given +// calc_tag_pre prepares filling `tag` with the authentication tag for the given // inputs. static size_t calc_tag_pre(poly1305_state *ctx, const uint8_t key[32], const uint8_t nonce[12], @@ -129,9 +129,9 @@ static int chacha20_poly1305_sealv(const uint8_t *key, return 0; } - // |CRYPTO_chacha_20| uses a 32-bit block counter. Therefore we disallow + // `CRYPTO_chacha_20` uses a 32-bit block counter. Therefore we disallow // individual operations that work on more than 256GB at a time. - // |in_len_64| is needed because, on 32-bit platforms, size_t is only + // `in_len_64` is needed because, on 32-bit platforms, size_t is only // 32-bits and this produces a warning because it's always false. // Casting to uint64_t inside the conditional is not sufficient to stop // the warning. @@ -148,7 +148,7 @@ static int chacha20_poly1305_sealv(const uint8_t *key, data.in.counter = 0; CopySpan(nonce, data.in.nonce); if (iovecs.size() >= 2) { - // |chacha20_poly1305_seal| only supports one extra input and expects it + // `chacha20_poly1305_seal` only supports one extra input and expects it // to have been encrypted ahead of time. (Historically it was only used // for very short inputs.) constexpr size_t kChaChaBlockSize = 64; @@ -271,9 +271,9 @@ static int chacha20_poly1305_openv_detached(const uint8_t *key, return 0; } - // |CRYPTO_chacha_20| uses a 32-bit block counter. Therefore we disallow + // `CRYPTO_chacha_20` uses a 32-bit block counter. Therefore we disallow // individual operations that work on more than 256GB at a time. - // |in_len_64| is needed because, on 32-bit platforms, size_t is only + // `in_len_64` is needed because, on 32-bit platforms, size_t is only // 32-bits and this produces a warning because it's always false. // Casting to uint64_t inside the conditional is not sufficient to stop // the warning. diff --git a/third_party/boringssl/src/crypto/cipher/e_rc2.cc b/third_party/boringssl/src/crypto/cipher/e_rc2.cc index 91c5f52d6..2fb90ada2 100644 --- a/third_party/boringssl/src/crypto/cipher/e_rc2.cc +++ b/third_party/boringssl/src/crypto/cipher/e_rc2.cc @@ -100,7 +100,7 @@ typedef struct rc2_key_st { uint16_t data[64]; } RC2_KEY; -static void RC2_encrypt(uint32_t *d, RC2_KEY *key) { +static void rc2_encrypt(uint32_t *d, RC2_KEY *key) { int i, n; uint16_t *p0, *p1; uint16_t x0, x1, x2, x3, t; @@ -144,7 +144,7 @@ static void RC2_encrypt(uint32_t *d, RC2_KEY *key) { d[1] = (uint32_t)(x2 & 0xffff) | ((uint32_t)(x3 & 0xffff) << 16L); } -static void RC2_decrypt(uint32_t *d, RC2_KEY *key) { +static void rc2_decrypt(uint32_t *d, RC2_KEY *key) { int i, n; uint16_t *p0, *p1; uint16_t x0, x1, x2, x3, t; @@ -189,7 +189,7 @@ static void RC2_decrypt(uint32_t *d, RC2_KEY *key) { d[1] = (uint32_t)(x2 & 0xffff) | ((uint32_t)(x3 & 0xffff) << 16L); } -static void RC2_cbc_encrypt(const uint8_t *in, uint8_t *out, size_t length, +static void rc2_cbc_encrypt(const uint8_t *in, uint8_t *out, size_t length, RC2_KEY *ks, uint8_t *iv, int encrypt) { uint32_t tin0, tin1; uint32_t tout0, tout1, xor0, xor1; @@ -207,7 +207,7 @@ static void RC2_cbc_encrypt(const uint8_t *in, uint8_t *out, size_t length, tin1 ^= tout1; tin[0] = tin0; tin[1] = tin1; - RC2_encrypt(tin, ks); + rc2_encrypt(tin, ks); tout0 = tin[0]; l2c(tout0, out); tout1 = tin[1]; @@ -219,7 +219,7 @@ static void RC2_cbc_encrypt(const uint8_t *in, uint8_t *out, size_t length, tin1 ^= tout1; tin[0] = tin0; tin[1] = tin1; - RC2_encrypt(tin, ks); + rc2_encrypt(tin, ks); tout0 = tin[0]; l2c(tout0, out); tout1 = tin[1]; @@ -236,7 +236,7 @@ static void RC2_cbc_encrypt(const uint8_t *in, uint8_t *out, size_t length, tin[0] = tin0; c2l(in, tin1); tin[1] = tin1; - RC2_decrypt(tin, ks); + rc2_decrypt(tin, ks); tout0 = tin[0] ^ xor0; tout1 = tin[1] ^ xor1; l2c(tout0, out); @@ -249,7 +249,7 @@ static void RC2_cbc_encrypt(const uint8_t *in, uint8_t *out, size_t length, tin[0] = tin0; c2l(in, tin1); tin[1] = tin1; - RC2_decrypt(tin, ks); + rc2_decrypt(tin, ks); tout0 = tin[0] ^ xor0; tout1 = tin[1] ^ xor1; l2cn(tout0, tout1, out, l + 8); @@ -287,18 +287,23 @@ static const uint8_t key_table[256] = { 0xfe, 0x7f, 0xc1, 0xad, }; -static void RC2_set_key(RC2_KEY *key, int len, const uint8_t *data, int bits) { - int i, j; +static int rc2_set_key(RC2_KEY *key, size_t len, const uint8_t *data, + int bits) { + size_t i, j; uint8_t *k; - uint16_t *ki; unsigned int c, d; k = (uint8_t *)&key->data[0]; *k = 0; // for if there is a zero length key + if (len == 0) { + OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_KEY_LENGTH); + return 0; + } if (len > 128) { len = 128; } + if (bits <= 0) { bits = 1024; } @@ -326,16 +331,20 @@ static void RC2_set_key(RC2_KEY *key, int len, const uint8_t *data, int bits) { d = key_table[k[i] & c]; k[i] = d; - while (i--) { + + while (i > 0) { + --i; d = key_table[k[i + j] ^ d]; k[i] = d; } // copy from bytes into uint16_t's - ki = &(key->data[63]); - for (i = 127; i >= 0; i -= 2) { - *(ki--) = ((k[i] << 8) | k[i - 1]) & 0xffff; + for (i = 0; i < 64; ++i) { + j = 63 - i; + key->data[j] = ((k[2 * j + 1] << 8) | k[2 * j]) & 0xffff; } + + return 1; } typedef struct { @@ -346,8 +355,10 @@ typedef struct { static int rc2_init_key(EVP_CIPHER_CTX *ctx, const uint8_t *key, const uint8_t *iv, int enc) { EVP_RC2_KEY *rc2_key = (EVP_RC2_KEY *)ctx->cipher_data; - RC2_set_key(&rc2_key->ks, EVP_CIPHER_CTX_key_length(ctx), key, - rc2_key->key_bits); + if (!rc2_set_key(&rc2_key->ks, EVP_CIPHER_CTX_key_length(ctx), key, + rc2_key->key_bits)) { + return 0; + } return 1; } @@ -357,13 +368,13 @@ static int rc2_cbc_cipher_update(EVP_CIPHER_CTX *ctx, uint8_t *out, static const size_t kChunkSize = 0x10000; while (len >= kChunkSize) { - RC2_cbc_encrypt(in, out, kChunkSize, &key->ks, ctx->iv, ctx->encrypt); + rc2_cbc_encrypt(in, out, kChunkSize, &key->ks, ctx->iv, ctx->encrypt); len -= kChunkSize; in += kChunkSize; out += kChunkSize; } if (len) { - RC2_cbc_encrypt(in, out, len, &key->ks, ctx->iv, ctx->encrypt); + rc2_cbc_encrypt(in, out, len, &key->ks, ctx->iv, ctx->encrypt); } return 1; } @@ -376,7 +387,7 @@ static int rc2_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr) { key->key_bits = EVP_CIPHER_CTX_key_length(ctx) * 8; return 1; case EVP_CTRL_SET_RC2_KEY_BITS: - // Should be overridden by later call to |EVP_CTRL_INIT|, but + // Should be overridden by later call to `EVP_CTRL_INIT`, but // people call it, so it may as well work. key->key_bits = arg; return 1; diff --git a/third_party/boringssl/src/crypto/cipher/e_tls.cc b/third_party/boringssl/src/crypto/cipher/e_tls.cc index f38a41e1c..8f93522c6 100644 --- a/third_party/boringssl/src/crypto/cipher/e_tls.cc +++ b/third_party/boringssl/src/crypto/cipher/e_tls.cc @@ -108,8 +108,8 @@ static size_t aead_tls_tag_len(const EVP_AEAD_CTX *ctx, const size_t in_len) { const size_t hmac_len = HMAC_size(tls_ctx->hmac_ctx); const size_t block_size = EVP_CIPHER_CTX_block_size(&tls_ctx->cipher_ctx); - // An overflow of |in_len + hmac_len| doesn't affect the result mod - // |block_size|, provided that |block_size| is a smaller power of two. + // An overflow of `in_len + hmac_len` doesn't affect the result mod + // `block_size`, provided that `block_size` is a smaller power of two. assert(block_size == 8 /*3DES*/ || block_size == 16 /*AES*/); const size_t pad_len = block_size - ((in_len + hmac_len) & (block_size - 1)); return hmac_len + pad_len; @@ -145,7 +145,7 @@ static int aead_tls_sealv(const EVP_AEAD_CTX *ctx, return 0; } - // To allow for CBC mode which changes cipher length, |ad| doesn't include the + // To allow for CBC mode which changes cipher length, `ad` doesn't include the // length for legacy ciphers. uint8_t ad_extra[2]; CRYPTO_store_u16_be(ad_extra, static_cast(in_len)); @@ -217,7 +217,7 @@ static int aead_tls_sealv(const EVP_AEAD_CTX *ctx, // Feed the MAC into the cipher in two steps. First complete the // final partial block from encrypting the input and split the - // result between |out| and |out_tag|. Then feed the rest. + // result between `out` and `out_tag`. Then feed the rest. const size_t early_mac_len = block_size - remaining; assert(early_mac_len < block_size); assert(len + block_size - early_mac_len == in_len); @@ -329,19 +329,22 @@ static int aead_tls_openv(const EVP_AEAD_CTX *ctx, const size_t mac_len = HMAC_size(tls_ctx->hmac_ctx); - // Split the decrypted record into |iovecs_without_trailer| and |trailer|, + // Split the decrypted record into `iovecs_without_trailer` and `trailer`, // based on the public lower bound of where the plaintext ends. The plaintext - // is followed by |mac_len| and then at most 256 bytes of padding. - InplaceVector iovecs_without_trailer; - iovecs_without_trailer.CopyFrom(iovecs); + // is followed by `mac_len` and then at most 256 bytes of padding. + bssl::iovec::MaybeInplaceArray iovecs_without_trailer; + if (!iovecs_without_trailer.CopyFrom(iovecs)) { + return 0; + } uint8_t trailer_buf[EVP_MAX_MD_SIZE + 256]; const size_t trailer_len = std::min(in_len, mac_len + 256); - std::optional> trailer = bssl::iovec::GetAndRemoveOutSuffix( - Span(trailer_buf).first(trailer_len), Span(iovecs_without_trailer)); + std::optional> trailer = + bssl::iovec::GetAndRemoveOutSuffix(Span(trailer_buf).first(trailer_len), + Span(iovecs_without_trailer)); BSSL_CHECK(trailer.has_value()); // Remove CBC padding. Code from here on is timing-sensitive with respect to - // |padding_ok|, |trailer_minus_padding|, and derived values. + // `padding_ok`, `trailer_minus_padding`, and derived values. crypto_word_t padding_ok; size_t trailer_minus_padding; if (!EVP_tls_cbc_remove_padding(&padding_ok, &trailer_minus_padding, @@ -352,16 +355,16 @@ static int aead_tls_openv(const EVP_AEAD_CTX *ctx, return 0; } - // If the padding is valid, |trailer->first(trailer_minus_padding)| is the + // If the padding is valid, `trailer->first(trailer_minus_padding)` is the // last bytes of plaintext and the MAC. Otherwise, it is still large enough to - // extract a MAC, but it will be irrelevant. Note that |trailer_minus_padding| + // extract a MAC, but it will be irrelevant. Note that `trailer_minus_padding` // is secret. declassify_assert(trailer_minus_padding >= mac_len); size_t data_in_trailer_len = trailer_minus_padding - mac_len; size_t max_data_in_trailer_len = trailer->size() - mac_len; size_t data_len = total - trailer->size() + data_in_trailer_len; - // To allow for CBC mode which changes cipher length, |ad_len| doesn't + // To allow for CBC mode which changes cipher length, `ad_len` doesn't // include the length for legacy ciphers. uint8_t ad_extra[2]; CRYPTO_store_u16_be(ad_extra, static_cast(data_len)); diff --git a/third_party/boringssl/src/crypto/cipher/get_cipher.cc b/third_party/boringssl/src/crypto/cipher/get_cipher.cc index dabc54aa1..576a94f5f 100644 --- a/third_party/boringssl/src/crypto/cipher/get_cipher.cc +++ b/third_party/boringssl/src/crypto/cipher/get_cipher.cc @@ -69,7 +69,7 @@ const EVP_CIPHER *EVP_get_cipherbyname(const char *name) { } // This is not a name used by OpenSSL, but tcpdump registers it with - // |EVP_add_cipher_alias|. Our |EVP_add_cipher_alias| is a no-op, so we + // `EVP_add_cipher_alias`. Our `EVP_add_cipher_alias` is a no-op, so we // support the name here. if (OPENSSL_strcasecmp(name, "3des") == 0) { name = "des-ede3-cbc"; diff --git a/third_party/boringssl/src/crypto/cipher/internal.h b/third_party/boringssl/src/crypto/cipher/internal.h index e6ce207e0..8ee1c590b 100644 --- a/third_party/boringssl/src/crypto/cipher/internal.h +++ b/third_party/boringssl/src/crypto/cipher/internal.h @@ -18,6 +18,7 @@ #include #include +#include #include #include #include @@ -28,23 +29,23 @@ BSSL_NAMESPACE_BEGIN // EVP_tls_cbc_get_padding determines the padding from the decrypted, TLS, CBC -// record in |in|. This decrypted record should not include any "decrypted" +// record in `in`. This decrypted record should not include any "decrypted" // explicit IV. If the record is publicly invalid, it returns zero. Otherwise, -// it returns one and sets |*out_padding_ok| to all ones (0xfff..f) if the -// padding is valid and zero otherwise. It then sets |*out_len| to the length -// with the padding removed or |in_len| if invalid. +// it returns one and sets `*out_padding_ok` to all ones (0xfff..f) if the +// padding is valid and zero otherwise. It then sets `*out_len` to the length +// with the padding removed or `in_len` if invalid. // // If the function returns one, it runs in time independent of the contents of -// |in|. It is also guaranteed that, independent of |*out_padding_ok|, |mac_len| -// <= |*out_len| <= |in_len|, satisfying |EVP_tls_cbc_copy_mac|'s precondition. +// `in`. It is also guaranteed that, independent of `*out_padding_ok`, `mac_len` +// <= `*out_len` <= `in_len`, satisfying `EVP_tls_cbc_copy_mac`'s precondition. int EVP_tls_cbc_remove_padding(crypto_word_t *out_padding_ok, size_t *out_len, const uint8_t *in, size_t in_len, size_t block_size, size_t mac_size); -// EVP_tls_cbc_copy_mac copies |md_size| bytes from the end of the first -// |in_len| bytes of |in| to |out| in constant time (independent of the concrete -// value of |in_len|, which may vary within a 256-byte window). |in| must point -// to a buffer of |orig_len| bytes. +// EVP_tls_cbc_copy_mac copies `md_size` bytes from the end of the first +// `in_len` bytes of `in` to `out` in constant time (independent of the concrete +// value of `in_len`, which may vary within a 256-byte window). `in` must point +// to a buffer of `orig_len` bytes. // // On entry: // orig_len >= in_len >= md_size @@ -52,14 +53,14 @@ int EVP_tls_cbc_remove_padding(crypto_word_t *out_padding_ok, size_t *out_len, void EVP_tls_cbc_copy_mac(uint8_t *out, size_t md_size, const uint8_t *in, size_t in_len, size_t orig_len); -// EVP_tls_cbc_record_digest_supported returns 1 iff |md| is a hash function +// EVP_tls_cbc_record_digest_supported returns 1 iff `md` is a hash function // which EVP_tls_cbc_digest_record supports. int EVP_tls_cbc_record_digest_supported(const EVP_MD *md); -// EVP_sha1_final_with_secret_suffix computes the result of hashing |len| bytes -// from |in| to |ctx| and writes the resulting hash to |out|. |len| is treated -// as secret and must be at most |max_len|, which is treated as public. |in| -// must point to a buffer of at least |max_len| bytes. It returns one on success +// EVP_sha1_final_with_secret_suffix computes the result of hashing `len` bytes +// from `in` to `ctx` and writes the resulting hash to `out`. `len` is treated +// as secret and must be at most `max_len`, which is treated as public. `in` +// must point to a buffer of at least `max_len` bytes. It returns one on success // and zero if inputs are too long. // // This function is exported for unit tests. @@ -68,7 +69,7 @@ OPENSSL_EXPORT int EVP_sha1_final_with_secret_suffix( size_t max_len); // EVP_sha256_final_with_secret_suffix acts like -// |EVP_sha1_final_with_secret_suffix|, but for SHA-256. +// `EVP_sha1_final_with_secret_suffix`, but for SHA-256. // // This function is exported for unit tests. OPENSSL_EXPORT int EVP_sha256_final_with_secret_suffix( @@ -90,7 +91,7 @@ OPENSSL_EXPORT int EVP_sha256_final_with_secret_suffix( // trailer: a buffer, of public length, containing the remainder of the // plaintext as a prefix. // data_in_trailer_size: the secret, reported length of the data portion in -// |trailer| once the padding and MAC have been removed. +// `trailer` once the padding and MAC have been removed. // // On entry: by virtue of having been through one of the remove_padding // functions, above, we know that data_plus_mac_size is large enough to contain @@ -103,6 +104,9 @@ int EVP_tls_cbc_digest_record( bssl::Span trailer, size_t data_in_trailer_size, const uint8_t *mac_secret, unsigned mac_secret_length); + +// ChaCha20-Poly1305 Assembly. + #define POLY1305_TAG_LEN 16 // For convenience (the x86_64 calling convention allows only six parameters in @@ -149,9 +153,9 @@ inline int chacha20_poly1305_asm_capable() { } // chacha20_poly1305_open is defined in chacha20_poly1305_*.pl. It decrypts -// |plaintext_len| bytes from |ciphertext| and writes them to |out_plaintext|. -// Additional input parameters are passed in |aead_data->in|. On exit, it will -// write calculated tag value to |aead_data->out.tag|, which the caller must +// `plaintext_len` bytes from `ciphertext` and writes them to `out_plaintext`. +// Additional input parameters are passed in `aead_data->in`. On exit, it will +// write calculated tag value to `aead_data->out.tag`, which the caller must // check. #if defined(OPENSSL_X86_64) extern "C" void chacha20_poly1305_open_sse41( @@ -182,10 +186,10 @@ extern "C" void chacha20_poly1305_open(uint8_t *out_plaintext, #endif // chacha20_poly1305_open is defined in chacha20_poly1305_*.pl. It encrypts -// |plaintext_len| bytes from |plaintext| and writes them to |out_ciphertext|. -// Additional input parameters are passed in |aead_data->in|. The calculated tag -// value is over the computed ciphertext concatenated with |extra_ciphertext| -// and written to |aead_data->out.tag|. +// `plaintext_len` bytes from `plaintext` and writes them to `out_ciphertext`. +// Additional input parameters are passed in `aead_data->in`. The calculated tag +// value is over the computed ciphertext concatenated with `extra_ciphertext` +// and written to `aead_data->out.tag`. #if defined(OPENSSL_X86_64) extern "C" void chacha20_poly1305_seal_sse41( uint8_t *out_ciphertext, const uint8_t *plaintext, size_t plaintext_len, @@ -235,6 +239,161 @@ inline void chacha20_poly1305_seal(uint8_t *out_ciphertext, } #endif + +// AES-GCM-SIV Assembly. + +// TODO(davidben): AES-GCM-SIV assembly is not correct for Windows. It must save +// and restore xmm6 through xmm15. +#if defined(OPENSSL_X86_64) && !defined(OPENSSL_NO_ASM) && \ + !defined(OPENSSL_WINDOWS) +#define AES_GCM_SIV_ASM + +struct aead_aes_gcm_siv_asm_ctx { + alignas(16) uint8_t key[16 * 15]; + int is_128_bit; +}; + +inline int aes_gcm_siv_asm_capable() { + return CRYPTO_is_AVX_capable() && CRYPTO_is_AESNI_capable() && + CRYPTO_is_PCLMUL_capable(); +} + +extern "C" { +// aes128gcmsiv_aes_ks writes an AES-128 key schedule for `key` to +// `out_expanded_key`. `out_expanded_key` must be 16-byte aligned. +extern void aes128gcmsiv_aes_ks(const uint8_t key[16], + uint8_t out_expanded_key[16 * 15]); + +// aes256gcmsiv_aes_ks writes an AES-256 key schedule for `key` to +// `out_expanded_key`. `out_expanded_key` must be 16-byte aligned. +extern void aes256gcmsiv_aes_ks(const uint8_t key[32], + uint8_t out_expanded_key[16 * 15]); + +// aesgcmsiv_polyval_horner updates the POLYVAL value in `in_out_poly` to +// include a number (`in_blocks`) of 16-byte blocks of data from `in`, given +// the POLYVAL key in `key`. `in_out_poly` and `key` must be 16-byte aligned. +extern void aesgcmsiv_polyval_horner(const uint8_t in_out_poly[16], + const uint8_t key[16], const uint8_t *in, + size_t in_blocks); + +// aesgcmsiv_htable_init writes powers 1..8 of `auth_key` to `out_htable`. +// `out_htable` and `auth_key` must be 16-byte aligned. +extern void aesgcmsiv_htable_init(uint8_t out_htable[16 * 8], + const uint8_t auth_key[16]); + +// aesgcmsiv_htable6_init writes powers 1..6 of `auth_key` to `out_htable`. +// `out_htable` and `auth_key` must be 16-byte aligned. +extern void aesgcmsiv_htable6_init(uint8_t out_htable[16 * 6], + const uint8_t auth_key[16]); + +// aesgcmsiv_htable_polyval updates the POLYVAL value in `in_out_poly` to +// include `in_len` bytes of data from `in`. (Where `in_len` must be a multiple +// of 16.) It uses the precomputed powers of the key given in `htable`. +// `in_out_poly` and `htable` must be 16-byte aligned. +extern void aesgcmsiv_htable_polyval(const uint8_t htable[16 * 8], + const uint8_t *in, size_t in_len, + uint8_t in_out_poly[16]); + +// aes128gcmsiv_dec decrypts `in_len` & ~15 bytes from `out` and writes them to +// `in`. `in` and `out` may be equal, but must not otherwise alias. +// +// `in_out_calculated_tag_and_scratch`, on entry, must contain: +// 1. The current value of the calculated tag, which will be updated during +// decryption and written back to the beginning of this buffer on exit. +// 2. The claimed tag, which is needed to derive counter values. +// +// While decrypting, the whole of `in_out_calculated_tag_and_scratch` may be +// used for other purposes. `in_out_calculated_tag_and_scratch` and `htable` +// must be 16-byte aligned. In order to decrypt and update the POLYVAL value, it +// uses the expanded key from `key` and the table of powers in `htable`. +extern void aes128gcmsiv_dec(const uint8_t *in, uint8_t *out, + uint8_t in_out_calculated_tag_and_scratch[16 * 8], + const uint8_t htable[16 * 6], + const struct aead_aes_gcm_siv_asm_ctx *key, + size_t in_len); + +// aes256gcmsiv_dec acts like `aes128gcmsiv_dec`, but for AES-256. +// `in_out_calculated_tag_and_scratch` and `htable` must be 16-byte aligned. +extern void aes256gcmsiv_dec(const uint8_t *in, uint8_t *out, + uint8_t in_out_calculated_tag_and_scratch[16 * 8], + const uint8_t htable[16 * 6], + const struct aead_aes_gcm_siv_asm_ctx *key, + size_t in_len); + +// aes128gcmsiv_kdf performs the AES-GCM-SIV KDF given the expanded key from +// `key_schedule` and the nonce in `nonce`. Note that, while only 12 bytes of +// the nonce are used, 16 bytes are read and so the value must be +// right-padded. `nonce`, `out_key_material`, and `key_schedule` must be +// 16-byte aligned. +extern void aes128gcmsiv_kdf(const uint8_t nonce[16], + uint64_t out_key_material[8], + const uint8_t *key_schedule); + +// aes256gcmsiv_kdf acts like `aes128gcmsiv_kdf`, but for AES-256. `nonce`, +// `out_key_material`, and `key_schedule` must be 16-byte aligned. +extern void aes256gcmsiv_kdf(const uint8_t nonce[16], + uint64_t out_key_material[12], + const uint8_t *key_schedule); + +// aes128gcmsiv_aes_ks_enc_x1 performs a key expansion of the AES-128 key in +// `key`, writes the expanded key to `out_expanded_key` and encrypts a single +// block from `in` to `out`. `in`, `out`, `out_expanded_key`, and `key` must be +// 16-byte aligned. +extern void aes128gcmsiv_aes_ks_enc_x1(const uint8_t in[16], uint8_t out[16], + uint8_t out_expanded_key[16 * 15], + const uint64_t key[2]); + +// aes256gcmsiv_aes_ks_enc_x1 acts like `aes128gcmsiv_aes_ks_enc_x1`, but for +// AES-256. `in`, `out`, `out_expanded_key`, and `key` must be 16-byte aligned. +extern void aes256gcmsiv_aes_ks_enc_x1(const uint8_t in[16], uint8_t out[16], + uint8_t out_expanded_key[16 * 15], + const uint64_t key[4]); + +// aes128gcmsiv_ecb_enc_block encrypts a single block from `in` to `out` using +// the expanded key in `expanded_key`. `in` and `out` must be 16-byte aligned. +extern void aes128gcmsiv_ecb_enc_block( + const uint8_t in[16], uint8_t out[16], + const struct aead_aes_gcm_siv_asm_ctx *expanded_key); + +// aes256gcmsiv_ecb_enc_block acts like `aes128gcmsiv_ecb_enc_block`, but for +// AES-256. `in` and `out` must be 16-byte aligned. +extern void aes256gcmsiv_ecb_enc_block( + const uint8_t in[16], uint8_t out[16], + const struct aead_aes_gcm_siv_asm_ctx *expanded_key); + +// aes128gcmsiv_enc_msg_x4 encrypts `in_len` bytes from `in` to `out` using the +// expanded key from `key`. (The value of `in_len` must be a multiple of 16.) +// The `in` and `out` buffers may be equal but must not otherwise overlap. The +// initial counter is constructed from the given `tag` as required by +// AES-GCM-SIV. `tag` must be 16-byte aligned. +extern void aes128gcmsiv_enc_msg_x4(const uint8_t *in, uint8_t *out, + const uint8_t *tag, + const struct aead_aes_gcm_siv_asm_ctx *key, + size_t in_len); + +// aes256gcmsiv_enc_msg_x4 acts like `aes128gcmsiv_enc_msg_x4`, but for +// AES-256. `tag` must be 16-byte aligned. +extern void aes256gcmsiv_enc_msg_x4(const uint8_t *in, uint8_t *out, + const uint8_t *tag, + const struct aead_aes_gcm_siv_asm_ctx *key, + size_t in_len); + +// aes128gcmsiv_enc_msg_x8 acts like `aes128gcmsiv_enc_msg_x4`, but is +// optimised for longer messages. +extern void aes128gcmsiv_enc_msg_x8(const uint8_t *in, uint8_t *out, + const uint8_t *tag, + const struct aead_aes_gcm_siv_asm_ctx *key, + size_t in_len); + +// aes256gcmsiv_enc_msg_x8 acts like `aes256gcmsiv_enc_msg_x4`, but is +// optimised for longer messages. +extern void aes256gcmsiv_enc_msg_x8(const uint8_t *in, uint8_t *out, + const uint8_t *tag, + const struct aead_aes_gcm_siv_asm_ctx *key, + size_t in_len); +} +#endif // OPENSSL_X86_64 && !OPENSSL_NO_ASM && !OPENSSL_WINDOWS + BSSL_NAMESPACE_END #endif // OPENSSL_HEADER_CRYPTO_CIPHER_INTERNAL_H diff --git a/third_party/boringssl/src/crypto/cipher/tls_cbc.cc b/third_party/boringssl/src/crypto/cipher/tls_cbc.cc index f0c57647b..dc7058e5b 100644 --- a/third_party/boringssl/src/crypto/cipher/tls_cbc.cc +++ b/third_party/boringssl/src/crypto/cipher/tls_cbc.cc @@ -46,7 +46,7 @@ int bssl::EVP_tls_cbc_remove_padding(crypto_word_t *out_padding_ok, // length byte. Thus, with the length byte included, there are i+1 // bytes of padding. // - // We can't check just |padding_length+1| bytes because that leaks + // We can't check just `padding_length+1` bytes because that leaks // decrypted information. Therefore we always have to check the maximum // amount of padding possible. (Again, the length of the record is // public information so we can use it.) @@ -57,21 +57,21 @@ int bssl::EVP_tls_cbc_remove_padding(crypto_word_t *out_padding_ok, for (size_t i = 0; i < to_check; i++) { uint8_t mask = constant_time_ge_8(padding_length, i); - // The value barrier on |(in_len - 1 - i)| isn't needed to enforce + // The value barrier on `(in_len - 1 - i)` isn't needed to enforce // constant-time. It is just there to prevent a false positive in // constant-time checks by valgrind. uint8_t b = in[value_barrier_w(in_len - 1 - i)]; - // The final |padding_length+1| bytes should all have the value - // |padding_length|. Therefore the XOR should be zero. + // The final `padding_length+1` bytes should all have the value + // `padding_length`. Therefore the XOR should be zero. good &= ~(mask & (padding_length ^ b)); } - // If any of the final |padding_length+1| bytes had the wrong value, - // one or more of the lower eight bits of |good| will be cleared. + // If any of the final `padding_length+1` bytes had the wrong value, + // one or more of the lower eight bits of `good` will be cleared. good = constant_time_eq_w(0xff, good & 0xff); - // Always treat |padding_length| as zero on error. If, assuming block size of - // 16, a padding of [<15 arbitrary bytes> 15] treated |padding_length| as 16 + // Always treat `padding_length` as zero on error. If, assuming block size of + // 16, a padding of [<15 arbitrary bytes> 15] treated `padding_length` as 16 // and returned -1, distinguishing good MAC and bad padding from bad MAC and // bad padding would give POODLE's padding oracle. padding_length = good & (padding_length + 1); @@ -86,7 +86,7 @@ void bssl::EVP_tls_cbc_copy_mac(uint8_t *out, size_t md_size, const uint8_t *in, uint8_t *rotated_mac = rotated_mac1; uint8_t *rotated_mac_tmp = rotated_mac2; - // mac_end is the index of |in| just after the end of the MAC. + // mac_end is the index of `in` just after the end of the MAC. size_t mac_end = in_len; size_t mac_start = mac_end - md_size; @@ -114,15 +114,15 @@ void bssl::EVP_tls_cbc_copy_mac(uint8_t *out, size_t md_size, const uint8_t *in, mac_started |= is_mac_start; uint8_t mac_ended = constant_time_ge_8(i, mac_end); rotated_mac[j] |= in[i] & mac_started & ~mac_ended; - // Save the offset that |mac_start| is mapped to. + // Save the offset that `mac_start` is mapped to. rotate_offset |= j & is_mac_start; } // Now rotate the MAC. We rotate in log(md_size) steps, one for each bit // position. for (size_t offset = 1; offset < md_size; offset <<= 1, rotate_offset >>= 1) { - // Rotate by |offset| iff the corresponding bit is set in - // |rotate_offset|, placing the result in |rotated_mac_tmp|. + // Rotate by `offset` iff the corresponding bit is set in + // `rotate_offset`, placing the result in `rotated_mac_tmp`. const uint8_t skip_rotate = (rotate_offset & 1) - 1; for (size_t i = 0, j = offset; i < md_size; i++, j++) { if (j >= md_size) { @@ -132,7 +132,7 @@ void bssl::EVP_tls_cbc_copy_mac(uint8_t *out, size_t md_size, const uint8_t *in, constant_time_select_8(skip_rotate, rotated_mac[i], rotated_mac[j]); } - // Swap pointers so |rotated_mac| contains the (possibly) rotated value. + // Swap pointers so `rotated_mac` contains the (possibly) rotated value. // Note the number of iterations and thus the identity of these pointers is // public information. uint8_t *tmp = rotated_mac; @@ -147,8 +147,8 @@ int bssl::EVP_sha1_final_with_secret_suffix(SHA_CTX *ctx, uint8_t out[SHA_DIGEST_LENGTH], const uint8_t *in, size_t len, size_t max_len) { - // Bound the input length so |total_bits| below fits in four bytes. This is - // redundant with TLS record size limits. This also ensures |input_idx| below + // Bound the input length so `total_bits` below fits in four bytes. This is + // redundant with TLS record size limits. This also ensures `input_idx` below // does not overflow. size_t max_len_bits = max_len << 3; if (ctx->Nh != 0 || @@ -158,7 +158,7 @@ int bssl::EVP_sha1_final_with_secret_suffix(SHA_CTX *ctx, return 0; } - // We need to hash the following into |ctx|: + // We need to hash the following into `ctx`: // // - ctx->data[:ctx->num] // - in[:len] @@ -169,7 +169,7 @@ int bssl::EVP_sha1_final_with_secret_suffix(SHA_CTX *ctx, size_t last_block = num_blocks - 1; size_t max_blocks = (ctx->num + max_len + 1 + 8 + SHA_CBLOCK - 1) >> 6; - // The bounds above imply |total_bits| fits in four bytes. + // The bounds above imply `total_bits` fits in four bytes. size_t total_bits = ctx->Nl + (len << 3); uint8_t length_bytes[4]; CRYPTO_store_u32_be(length_bytes, total_bits); @@ -177,13 +177,13 @@ int bssl::EVP_sha1_final_with_secret_suffix(SHA_CTX *ctx, // We now construct and process each expected block in constant-time. uint8_t block[SHA_CBLOCK] = {0}; uint32_t result[5] = {0}; - // input_idx is the index into |in| corresponding to the current block. - // However, we allow this index to overflow beyond |max_len|, to simplify the + // input_idx is the index into `in` corresponding to the current block. + // However, we allow this index to overflow beyond `max_len`, to simplify the // 0x80 byte. size_t input_idx = 0; for (size_t i = 0; i < max_blocks; i++) { - // Fill |block| with data from the partial block in |ctx| and |in|. We copy - // as if we were hashing up to |max_len| and then zero the excess later. + // Fill `block` with data from the partial block in `ctx` and `in`. We copy + // as if we were hashing up to `max_len` and then zero the excess later. size_t block_start = 0; if (i == 0) { OPENSSL_memcpy(block, ctx->data, ctx->num); @@ -197,12 +197,12 @@ int bssl::EVP_sha1_final_with_secret_suffix(SHA_CTX *ctx, OPENSSL_memcpy(block + block_start, in + input_idx, to_copy); } - // Zero any bytes beyond |len| and add the 0x80 byte. + // Zero any bytes beyond `len` and add the 0x80 byte. for (size_t j = block_start; j < SHA_CBLOCK; j++) { // input[idx] corresponds to block[j]. size_t idx = input_idx + j - block_start; - // The barriers on |len| are not strictly necessary. However, without - // them, GCC compiles this code by incorporating |len| into the loop + // The barriers on `len` are not strictly necessary. However, without + // them, GCC compiles this code by incorporating `len` into the loop // counter and subtracting it out later. This is still constant-time, but // it frustrates attempts to validate this. uint8_t is_in_bounds = constant_time_lt_8(idx, value_barrier_w(len)); @@ -239,8 +239,8 @@ int bssl::EVP_sha256_final_with_secret_suffix(SHA256_CTX *ctx, uint8_t out[SHA256_DIGEST_LENGTH], const uint8_t *in, size_t len, size_t max_len) { - // Bound the input length so |total_bits| below fits in four bytes. This is - // redundant with TLS record size limits. This also ensures |input_idx| below + // Bound the input length so `total_bits` below fits in four bytes. This is + // redundant with TLS record size limits. This also ensures `input_idx` below // does not overflow. size_t max_len_bits = max_len << 3; if (ctx->Nh != 0 || @@ -250,7 +250,7 @@ int bssl::EVP_sha256_final_with_secret_suffix(SHA256_CTX *ctx, return 0; } - // We need to hash the following into |ctx|: + // We need to hash the following into `ctx`: // // - ctx->data[:ctx->num] // - in[:len] @@ -261,7 +261,7 @@ int bssl::EVP_sha256_final_with_secret_suffix(SHA256_CTX *ctx, size_t last_block = num_blocks - 1; size_t max_blocks = (ctx->num + max_len + 1 + 8 + SHA256_CBLOCK - 1) >> 6; - // The bounds above imply |total_bits| fits in four bytes. + // The bounds above imply `total_bits` fits in four bytes. size_t total_bits = ctx->Nl + (len << 3); uint8_t length_bytes[4]; CRYPTO_store_u32_be(length_bytes, total_bits); @@ -269,13 +269,13 @@ int bssl::EVP_sha256_final_with_secret_suffix(SHA256_CTX *ctx, // We now construct and process each expected block in constant-time. uint8_t block[SHA256_CBLOCK] = {0}; uint32_t result[8] = {0}; - // input_idx is the index into |in| corresponding to the current block. - // However, we allow this index to overflow beyond |max_len|, to simplify the + // input_idx is the index into `in` corresponding to the current block. + // However, we allow this index to overflow beyond `max_len`, to simplify the // 0x80 byte. size_t input_idx = 0; for (size_t i = 0; i < max_blocks; i++) { - // Fill |block| with data from the partial block in |ctx| and |in|. We copy - // as if we were hashing up to |max_len| and then zero the excess later. + // Fill `block` with data from the partial block in `ctx` and `in`. We copy + // as if we were hashing up to `max_len` and then zero the excess later. size_t block_start = 0; if (i == 0) { OPENSSL_memcpy(block, ctx->data, ctx->num); @@ -289,12 +289,12 @@ int bssl::EVP_sha256_final_with_secret_suffix(SHA256_CTX *ctx, OPENSSL_memcpy(block + block_start, in + input_idx, to_copy); } - // Zero any bytes beyond |len| and add the 0x80 byte. + // Zero any bytes beyond `len` and add the 0x80 byte. for (size_t j = block_start; j < SHA256_CBLOCK; j++) { // input[idx] corresponds to block[j]. size_t idx = input_idx + j - block_start; - // The barriers on |len| are not strictly necessary. However, without - // them, GCC compiles this code by incorporating |len| into the loop + // The barriers on `len` are not strictly necessary. However, without + // them, GCC compiles this code by incorporating `len` into the loop // counter and subtracting it out later. This is still constant-time, but // it frustrates attempts to validate this. uint8_t is_in_bounds = constant_time_lt_8(idx, value_barrier_w(len)); @@ -372,7 +372,7 @@ static int tls_cbc_digest_record_sha1( SHA1_Update(&ctx, iovec.out, iovec.len); } - // Hash the remaining data without leaking |data_in_trailer_size|. + // Hash the remaining data without leaking `data_in_trailer_size`. uint8_t mac_out[SHA_DIGEST_LENGTH]; if (!EVP_sha1_final_with_secret_suffix(&ctx, mac_out, trailer.data(), data_in_trailer_size, @@ -428,7 +428,7 @@ static int tls_cbc_digest_record_sha256( SHA256_Update(&ctx, iovec.out, iovec.len); } - // Hash the remaining data without leaking |data_in_trailer_size|. + // Hash the remaining data without leaking `data_in_trailer_size`. uint8_t mac_out[SHA256_DIGEST_LENGTH]; if (!EVP_sha256_final_with_secret_suffix(&ctx, mac_out, trailer.data(), data_in_trailer_size, diff --git a/third_party/boringssl/src/crypto/cms/cms.cc b/third_party/boringssl/src/crypto/cms/cms.cc index e7b18036f..ddd9d60c8 100644 --- a/third_party/boringssl/src/crypto/cms/cms.cc +++ b/third_party/boringssl/src/crypto/cms/cms.cc @@ -56,8 +56,9 @@ class CMSContentInfo : public CMS_ContentInfo_st { BSSL_NAMESPACE_END -CMS_ContentInfo *CMS_sign(X509 *signcert, EVP_PKEY *pkey, STACK_OF(X509) *certs, - BIO *data, uint32_t flags) { +CMS_ContentInfo *CMS_sign(const X509 *signcert, const EVP_PKEY *pkey, + const STACK_OF(X509) *certs, BIO *data, + uint32_t flags) { // We only support external signatures and do not support embedding // certificates in SignedData. if ((flags & CMS_DETACHED) == 0 || sk_X509_num(certs) != 0) { @@ -75,8 +76,8 @@ CMS_ContentInfo *CMS_sign(X509 *signcert, EVP_PKEY *pkey, STACK_OF(X509) *certs, return nullptr; } - // We don't actually use streaming mode, but Linux passes |CMS_STREAM| to - // |CMS_sign| and OpenSSL interprets it as an alias for |CMS_PARTIAL| in this + // We don't actually use streaming mode, but Linux passes `CMS_STREAM` to + // `CMS_sign` and OpenSSL interprets it as an alias for `CMS_PARTIAL` in this // context. if ((flags & (CMS_PARTIAL | CMS_STREAM)) == 0 && !CMS_final(cms.get(), data, nullptr, flags)) { @@ -88,8 +89,8 @@ CMS_ContentInfo *CMS_sign(X509 *signcert, EVP_PKEY *pkey, STACK_OF(X509) *certs, void CMS_ContentInfo_free(CMS_ContentInfo *cms) { Delete(FromOpaque(cms)); } -CMS_SignerInfo *CMS_add1_signer(CMS_ContentInfo *cms, X509 *signcert, - EVP_PKEY *pkey, const EVP_MD *md, +CMS_SignerInfo *CMS_add1_signer(CMS_ContentInfo *cms, const X509 *signcert, + const EVP_PKEY *pkey, const EVP_MD *md, uint32_t flags) { auto *impl = FromOpaque(cms); if ( // Already finalized. @@ -137,7 +138,7 @@ int CMS_final(CMS_ContentInfo *cms, BIO *data, BIO *dcont, uint32_t flags) { // We only support the straightforward passthrough mode, without S/MIME // translations. (flags & CMS_BINARY) == 0 || - // We do not support |dcont|. It is unclear what it does. + // We do not support `dcont`. It is unclear what it does. dcont != nullptr) { OPENSSL_PUT_ERROR(CMS, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); return 0; diff --git a/third_party/boringssl/src/crypto/conf/conf.cc b/third_party/boringssl/src/crypto/conf/conf.cc index 0c7ea9db7..e4d01c1a6 100644 --- a/third_party/boringssl/src/crypto/conf/conf.cc +++ b/third_party/boringssl/src/crypto/conf/conf.cc @@ -154,7 +154,7 @@ static int is_quote(char c) { return c == '"' || c == '\'' || c == '`'; } static int is_esc(char c) { return c == '\\'; } static int is_conf_ws(char c) { - // This differs from |OPENSSL_isspace| in that CONF does not accept '\v' and + // This differs from `OPENSSL_isspace` in that CONF does not accept '\v' and // '\f' as whitespace. return c == ' ' || c == '\t' || c == '\r' || c == '\n'; } @@ -285,7 +285,7 @@ static int add_string(const CONF *conf, CONF_SECTION *section, CONF_VALUE *old_value; if (!lh_CONF_VALUE_insert(conf->values, &old_value, value)) { - // Remove |value| from |section->values|, so we do not leave a dangling + // Remove `value` from `section->values`, so we do not leave a dangling // pointer. sk_CONF_VALUE_pop(section->values); return 0; diff --git a/third_party/boringssl/src/crypto/conf/internal.h b/third_party/boringssl/src/crypto/conf/internal.h index 7fe35aa2a..f924390f7 100644 --- a/third_party/boringssl/src/crypto/conf/internal.h +++ b/third_party/boringssl/src/crypto/conf/internal.h @@ -36,14 +36,14 @@ struct conf_st { BSSL_NAMESPACE_BEGIN -// CONF_VALUE_new returns a freshly allocated and zeroed |CONF_VALUE|. +// CONF_VALUE_new returns a freshly allocated and zeroed `CONF_VALUE`. CONF_VALUE *CONF_VALUE_new(); -// CONF_parse_list takes a list separated by 'sep' and calls |list_cb| giving +// CONF_parse_list takes a list separated by 'sep' and calls `list_cb` giving // the start and length of each member, optionally stripping leading and // trailing whitespace. This can be used to parse comma separated lists for -// example. If |list_cb| returns <= 0, then the iteration is halted and that -// value is returned immediately. Otherwise it returns one. Note that |list_cb| +// example. If `list_cb` returns <= 0, then the iteration is halted and that +// value is returned immediately. Otherwise it returns one. Note that `list_cb` // may be called on an empty member. OPENSSL_EXPORT int CONF_parse_list( const char *list, char sep, int remove_whitespace, diff --git a/third_party/boringssl/src/crypto/cpu_arm_freebsd.cc b/third_party/boringssl/src/crypto/cpu_arm_freebsd.cc index 7a8d05b69..f67b94996 100644 --- a/third_party/boringssl/src/crypto/cpu_arm_freebsd.cc +++ b/third_party/boringssl/src/crypto/cpu_arm_freebsd.cc @@ -27,7 +27,7 @@ using namespace bssl; void bssl::OPENSSL_cpuid_setup() { unsigned long hwcap = 0, hwcap2 = 0; - // |elf_aux_info| may fail, in which case |hwcap| and |hwcap2| will be + // `elf_aux_info` may fail, in which case `hwcap` and `hwcap2` will be // left at zero. The rest of this function will then gracefully report // the features are absent. elf_aux_info(AT_HWCAP, &hwcap, sizeof(hwcap)); diff --git a/third_party/boringssl/src/crypto/cpu_arm_linux.cc b/third_party/boringssl/src/crypto/cpu_arm_linux.cc index d8ea98a6d..29325f764 100644 --- a/third_party/boringssl/src/crypto/cpu_arm_linux.cc +++ b/third_party/boringssl/src/crypto/cpu_arm_linux.cc @@ -46,8 +46,8 @@ static ssize_t read_eintr(int fd, void *out, size_t len) { return ret; } -// read_file opens |path| and reads until end-of-file. On success, it returns -// one and sets |*out_ptr| and |*out_len| to a newly-allocated buffer with the +// read_file opens `path` and reads until end-of-file. On success, it returns +// one and sets `*out_ptr` and `*out_len` to a newly-allocated buffer with the // contents. Otherwise, it returns zero. static int read_file(char **out_ptr, size_t *out_len, const char *path) { int fd = open_eintr(path, O_RDONLY); diff --git a/third_party/boringssl/src/crypto/cpu_arm_linux.h b/third_party/boringssl/src/crypto/cpu_arm_linux.h index b01ee5414..bea577ab9 100644 --- a/third_party/boringssl/src/crypto/cpu_arm_linux.h +++ b/third_party/boringssl/src/crypto/cpu_arm_linux.h @@ -41,8 +41,8 @@ namespace armcap { #define CRYPTO_HWCAP2_SHA1 (1 << 2) #define CRYPTO_HWCAP2_SHA2 (1 << 3) -// SplitStringView finds the first occurrence of |sep| in |in| and, if found, -// sets |*out_left| and |*out_right| to |in| split before and after |sep|, and +// SplitStringView finds the first occurrence of `sep` in `in` and, if found, +// sets `*out_left` and `*out_right` to `in` split before and after `sep`, and // returns true. If not found, it returns false. inline bool SplitStringView(std::string_view *out_left, std::string_view *out_right, std::string_view in, @@ -56,23 +56,23 @@ inline bool SplitStringView(std::string_view *out_left, return true; } -// GetDelimited reads a |sep|-delimited entry from |s|, writing it to |out| and -// updating |s| to point beyond it. It returns true on success and false if |s| -// is empty. If |s| has no copies of |sep| and is non-empty, it reads the entire -// string to |out|. +// GetDelimited reads a `sep`-delimited entry from `s`, writing it to `out` and +// updating `s` to point beyond it. It returns true on success and false if `s` +// is empty. If `s` has no copies of `sep` and is non-empty, it reads the entire +// string to `out`. inline bool GetDelimited(std::string_view *s, std::string_view *out, char sep) { if (s->empty()) { return false; } if (!SplitStringView(out, s, *s, sep)) { - // |s| had no instances of |sep|. Return the entire string. + // `s` had no instances of `sep`. Return the entire string. *out = *s; *s = std::string_view(); } return true; } -// TrimStringView removes leading and trailing whitespace from |s|. +// TrimStringView removes leading and trailing whitespace from `s`. inline std::string_view TrimStringView(std::string_view s) { size_t pos = s.find_first_not_of(" \t"); if (pos == std::string_view::npos) { @@ -84,11 +84,11 @@ inline std::string_view TrimStringView(std::string_view s) { return s.substr(0, pos + 1); } -// ExtractCpuinfoField extracts a /proc/cpuinfo field named |field| from |in|. +// ExtractCpuinfoField extracts a /proc/cpuinfo field named `field` from `in`. // If found, it returns the value. Otherwise, it returns the empty string. inline std::string_view ExtractCpuinfoField(std::string_view in, std::string_view field) { - // Process |in| one line at a time. + // Process `in` one line at a time. std::string_view line; while (GetDelimited(&in, &line, '\n')) { std::string_view key, value; @@ -103,8 +103,8 @@ inline std::string_view ExtractCpuinfoField(std::string_view in, return {}; } -// HasListItem treats |list| as a space-separated list of items and returns -// whether |item| is contained in |list|. +// HasListItem treats `list` as a space-separated list of items and returns +// whether `item` is contained in `list`. inline bool HasListItem(std::string_view list, std::string_view item) { std::string_view feature; while (GetDelimited(&list, &feature, ' ')) { @@ -115,8 +115,8 @@ inline bool HasListItem(std::string_view list, std::string_view item) { return false; } -// GetHWCAP2FromCpuinfo returns an equivalent ARM |AT_HWCAP2| value from -// |cpuinfo|. +// GetHWCAP2FromCpuinfo returns an equivalent ARM `AT_HWCAP2` value from +// `cpuinfo`. inline unsigned long GetHWCAP2FromCpuinfo(std::string_view cpuinfo) { std::string_view features = ExtractCpuinfoField(cpuinfo, "Features"); unsigned long ret = 0; diff --git a/third_party/boringssl/src/crypto/cpu_intel.cc b/third_party/boringssl/src/crypto/cpu_intel.cc index 6a406a797..3d1180abb 100644 --- a/third_party/boringssl/src/crypto/cpu_intel.cc +++ b/third_party/boringssl/src/crypto/cpu_intel.cc @@ -33,9 +33,9 @@ using namespace bssl; -// OPENSSL_cpuid runs the cpuid instruction. |leaf| is passed in as EAX and ECX -// is set to zero. It writes EAX, EBX, ECX, and EDX to |*out_eax| through -// |*out_edx|. +// OPENSSL_cpuid runs the cpuid instruction. `leaf` is passed in as EAX and ECX +// is set to zero. It writes EAX, EBX, ECX, and EDX to `*out_eax` through +// `*out_edx`. static void OPENSSL_cpuid(uint32_t *out_eax, uint32_t *out_ebx, uint32_t *out_ecx, uint32_t *out_edx, uint32_t leaf) { #if defined(_MSC_VER) @@ -65,7 +65,7 @@ static void OPENSSL_cpuid(uint32_t *out_eax, uint32_t *out_ebx, } // OPENSSL_xgetbv returns the value of an Intel Extended Control Register (XCR). -// Currently only XCR0 is defined by Intel so |xcr| should always be zero. +// Currently only XCR0 is defined by Intel so `xcr` should always be zero. static uint64_t OPENSSL_xgetbv(uint32_t xcr) { #if defined(_MSC_VER) return (uint64_t)_xgetbv(xcr); @@ -92,9 +92,9 @@ static bool os_supports_avx512(uint64_t xcr0) { #endif } -// handle_cpu_env applies the value from |in| to the CPUID values in |out[0]| -// and |out[1]|. See the comment in |OPENSSL_cpuid_setup| about this. The -// |is_last| argument specifies whether the value is at the end of the string. +// handle_cpu_env applies the value from `in` to the CPUID values in `out[0]` +// and `out[1]`. See the comment in `OPENSSL_cpuid_setup` about this. The +// `is_last` argument specifies whether the value is at the end of the string. // Otherwise it may be followed by a colon. static void handle_cpu_env(uint32_t out[2], const char *in, bool is_last) { const int invert_op = in[0] == '~'; diff --git a/third_party/boringssl/src/crypto/curve25519/cpace.cc b/third_party/boringssl/src/crypto/curve25519/cpace.cc new file mode 100644 index 000000000..99529a7db --- /dev/null +++ b/third_party/boringssl/src/crypto/curve25519/cpace.cc @@ -0,0 +1,227 @@ +// Copyright 2026 The BoringSSL Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + + +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "../internal.h" +#include "../mem_internal.h" +#include "./internal.h" + + +CPACE_CTX *CPACE_CTX_new(enum cpace_role_t role, const uint8_t *password, + size_t password_len, const uint8_t *channel_id, + size_t channel_id_len, const uint8_t *assoc_data, + size_t assoc_data_len, const uint8_t *session_id, + size_t session_id_len) { + bssl::UniquePtr ctx = bssl::MakeUnique(); + if (ctx == nullptr) { + return nullptr; + } + + ctx->our_role = role; + if (!ctx->our_password.CopyFrom(bssl::Span(password, password_len)) || + !ctx->channel_id.CopyFrom(bssl::Span(channel_id, channel_id_len)) || + !ctx->our_aad.CopyFrom(bssl::Span(assoc_data, assoc_data_len)) || + !ctx->session_id.CopyFrom(bssl::Span(session_id, session_id_len))) { + return nullptr; + } + + return ctx.release(); +} + +void CPACE_CTX_free(CPACE_CTX *ctx) { Delete(bssl::FromOpaque(ctx)); } + +namespace { +constexpr uint64_t leb128_len(uint64_t len) { + if (len < 128) { + return 1; + } + return (bssl::CRYPTO_bit_width(len) + 6) / 7; +} + +static constexpr size_t kMaxLenTagSize = leb128_len(UINT64_MAX); +static constexpr std::string_view kDSI = "CPace255"; + +constexpr size_t leb128_tag(uint64_t len, uint8_t tag[kMaxLenTagSize]) { + size_t tag_len = leb128_len(len); + for (size_t i = 0; i < tag_len; ++i) { + if (len < 128) { + tag[i] = len; + } else { + tag[i] = (len & 0x7f) | 0x80; + len >>= 7; + } + } + return tag_len; +} + +void push_lv_to_hasher(SHA512_CTX &hasher, bssl::Span data) { + uint8_t lv_tag[kMaxLenTagSize]; + size_t lv_tag_len = leb128_tag(data.size(), lv_tag); + SHA512_Update(&hasher, lv_tag, lv_tag_len); + SHA512_Update(&hasher, data.data(), data.size()); +} +} // namespace + + +// This implements the first two steps of G.calculate_generator in Section 8.2 +// of draft-irtf-cfrg-cpace-21. +void bssl::CpaceCtx::ComputeGeneratorStrHash( + uint8_t out_gen_str_hash[32]) const { + // len_zpad = max(0,s_in_bytes - 1 - len(prepend_len(PRS)) - + // len(prepend_len(DSI))) + size_t digest_len = leb128_len(our_password.size()) + our_password.size() // + + leb128_len(kDSI.length()) + kDSI.length(); + size_t len_zpad = 0; + if (digest_len < SHA512_CBLOCK - 1) { + len_zpad = SHA512_CBLOCK - 1 - digest_len; + } + + constexpr uint8_t kZeros[SHA512_CBLOCK] = {}; + // gen_str = generator_string(G.DSI, PRS, CI, sid, H.s_in_bytes) + SHA512_CTX hasher; + SHA512_Init(&hasher); + push_lv_to_hasher(hasher, StringAsBytes(kDSI)); + push_lv_to_hasher(hasher, our_password); + push_lv_to_hasher(hasher, Span(kZeros, len_zpad)); + push_lv_to_hasher(hasher, channel_id); + push_lv_to_hasher(hasher, session_id); + uint8_t out[SHA512_DIGEST_LENGTH]; + // gen_str_hash = H.hash(gen_str, G.field_size_bytes). + SHA512_Final(out, &hasher); + OPENSSL_memcpy(out_gen_str_hash, out, 32); +} + +bool bssl::CpaceCtx::ComputeMessage(uint8_t msg[CPACE_MSG_LEN]) { + if (state != cpace_state_init) { + OPENSSL_PUT_ERROR(CRYPTO, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); + return false; + } + + uint8_t gen_str_hash[32]; + // We now compute the first two steps of G.calculate_generator(H, PRS, sid, + // CI). + ComputeGeneratorStrHash(gen_str_hash); + // This result is then considered as a field coordinate using the u = + // decodeUCoordinate(gen_str_hash, G.field_size_bits) function from RFC7748. + uint8_t g[32], v[32]; + map_to_curve_curve25519_elligator2(g, v, gen_str_hash); + // sample_scalar() = sample_random_bytes(G.field_size_bytes) + RAND_bytes(our_private_key, 32); + auto rc = X25519(our_public_key, our_private_key, g); + OPENSSL_memcpy(msg, our_public_key, 32); + if (!rc) { + // This will happen with negligible probability. + OPENSSL_PUT_ERROR(CRYPTO, ERR_R_INTERNAL_ERROR); + return false; + } + state = cpace_state_msg_generated; + return true; +} + +int CPACE_generate_msg(CPACE_CTX *ctx, uint8_t out_msg[CPACE_MSG_LEN]) { + auto *impl = bssl::FromOpaque(ctx); + return impl->ComputeMessage(out_msg); +} + +bool bssl::CpaceCtx::ComputeISK( + const uint8_t peer_msg[CPACE_MSG_LEN], Span peer_assoc_data, + uint8_t out_shared_secret[CPACE_SHARED_SECRET_LEN], + uint8_t out_sid[SHA512_DIGEST_LENGTH]) { + if (state != cpace_state_msg_generated) { + OPENSSL_PUT_ERROR(CRYPTO, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); + return false; + } + + state = cpace_state_key_generated; + + uint8_t k[32]; + if (X25519(k, our_private_key, peer_msg) != 1) { + // TODO(crbug.com/42290066): This is another instance where unifying the + // reason code and library code into one error code makes most sense. + OPENSSL_PUT_ERROR(EC, EC_R_DECODE_ERROR); + return false; + } + // ISK = H.hash(lv_cat(G.DSI || b"_ISK", sid, K)||transcript(Ya,ADa,Yb,ADb)) + static constexpr std::string_view kDsiIsk = "CPace255_ISK"; + static constexpr std::string_view kDsiSidOut = "CPaceSidOutput"; + SHA512_CTX hasher, sid_hasher; + SHA512_Init(&hasher); + const bool compute_sid = out_sid != nullptr; + if (compute_sid) { + SHA512_Init(&sid_hasher); + SHA512_Update(&sid_hasher, kDsiSidOut.data(), kDsiSidOut.size()); + } + push_lv_to_hasher(hasher, StringAsBytes(kDsiIsk)); + push_lv_to_hasher(hasher, session_id); + push_lv_to_hasher(hasher, Span(k, 32)); + Span peer_public_key(peer_msg, 32); + auto push_our_transcript = [&] { + push_lv_to_hasher(hasher, our_public_key); + push_lv_to_hasher(hasher, our_aad); + if (compute_sid) { + push_lv_to_hasher(sid_hasher, our_public_key); + push_lv_to_hasher(sid_hasher, our_aad); + } + }; + auto push_peer_transcript = [&] { + push_lv_to_hasher(hasher, peer_public_key); + push_lv_to_hasher(hasher, peer_assoc_data); + if (compute_sid) { + push_lv_to_hasher(sid_hasher, peer_public_key); + push_lv_to_hasher(sid_hasher, peer_assoc_data); + } + }; + + switch (our_role) { + case cpace_role_initiator: { + push_our_transcript(); + push_peer_transcript(); + break; + } + case cpace_role_responder: { + push_peer_transcript(); + push_our_transcript(); + break; + } + } + if (compute_sid) { + SHA512_Final(out_sid, &sid_hasher); + } + return SHA512_Final(out_shared_secret, &hasher); +} + +// CPACE_process_msg processes a received message and finalize +int CPACE_process_msg(CPACE_CTX *ctx, const uint8_t msg[CPACE_MSG_LEN], + const uint8_t *peer_assoc_data, + size_t peer_assoc_data_len, + uint8_t out_shared_secret[CPACE_SHARED_SECRET_LEN], + uint8_t out_sid[SHA512_DIGEST_LENGTH]) { + auto *impl = bssl::FromOpaque(ctx); + return impl->ComputeISK(msg, bssl::Span(peer_assoc_data, peer_assoc_data_len), + out_shared_secret, out_sid); +} diff --git a/third_party/boringssl/src/crypto/curve25519/curve25519.cc b/third_party/boringssl/src/crypto/curve25519/curve25519.cc index 66d5d84b9..2ddfca600 100644 --- a/third_party/boringssl/src/crypto/curve25519/curve25519.cc +++ b/third_party/boringssl/src/crypto/curve25519/curve25519.cc @@ -22,6 +22,7 @@ #include #include +#include #include #include #include @@ -43,9 +44,11 @@ using namespace bssl; +namespace { + // Low-level intrinsic operations -static uint64_t load_3(const uint8_t *in) { +uint64_t load_3(const uint8_t *in) { uint64_t result; result = (uint64_t)in[0]; result |= ((uint64_t)in[1]) << 8; @@ -53,7 +56,7 @@ static uint64_t load_3(const uint8_t *in) { return result; } -static uint64_t load_4(const uint8_t *in) { +uint64_t load_4(const uint8_t *in) { uint64_t result; result = (uint64_t)in[0]; result |= ((uint64_t)in[1]) << 8; @@ -70,7 +73,7 @@ static uint64_t load_4(const uint8_t *in) { typedef uint64_t fe_limb_t; #define FE_NUM_LIMBS 5 -// assert_fe asserts that |f| satisfies bounds: +// assert_fe asserts that `f` satisfies bounds: // // [[0x0 ~> 0x8cccccccccccc], // [0x0 ~> 0x8cccccccccccc], @@ -87,7 +90,7 @@ typedef uint64_t fe_limb_t; } \ } while (0) -// assert_fe_loose asserts that |f| satisfies bounds: +// assert_fe_loose asserts that `f` satisfies bounds: // // [[0x0 ~> 0x1a666666666664], // [0x0 ~> 0x1a666666666664], @@ -109,7 +112,7 @@ typedef uint64_t fe_limb_t; typedef uint32_t fe_limb_t; #define FE_NUM_LIMBS 10 -// assert_fe asserts that |f| satisfies bounds: +// assert_fe asserts that `f` satisfies bounds: // // [[0x0 ~> 0x4666666], [0x0 ~> 0x2333333], // [0x0 ~> 0x4666666], [0x0 ~> 0x2333333], @@ -127,7 +130,7 @@ typedef uint32_t fe_limb_t; } \ } while (0) -// assert_fe_loose asserts that |f| satisfies bounds: +// assert_fe_loose asserts that `f` satisfies bounds: // // [[0x0 ~> 0xd333332], [0x0 ~> 0x6999999], // [0x0 ~> 0xd333332], [0x0 ~> 0x6999999], @@ -150,44 +153,46 @@ typedef uint32_t fe_limb_t; static_assert(sizeof(fe) == sizeof(fe_limb_t) * FE_NUM_LIMBS, "fe_limb_t[FE_NUM_LIMBS] is inconsistent with fe"); -static void fe_frombytes_strict(fe *h, const uint8_t s[32]) { - // |fiat_25519_from_bytes| requires the top-most bit be clear. +void fe_frombytes_strict(fe *h, const uint8_t s[32]) { + // `fiat_25519_from_bytes` requires the top-most bit be clear. declassify_assert((s[31] & 0x80) == 0); fiat_25519_from_bytes(h->v, s); assert_fe(h->v); } -static void fe_frombytes(fe *h, const uint8_t s[32]) { +// fe_frombytes takes in a little-endian 255-bit number and decodes it into an +// `fe`. +void fe_frombytes(fe *h, const uint8_t s[32]) { uint8_t s_copy[32]; OPENSSL_memcpy(s_copy, s, 32); s_copy[31] &= 0x7f; fe_frombytes_strict(h, s_copy); } -static void fe_tobytes(uint8_t s[32], const fe *f) { +void fe_tobytes(uint8_t s[32], const fe *f) { assert_fe(f->v); fiat_25519_to_bytes(s, f->v); } // h = 0 -static void fe_0(fe *h) { OPENSSL_memset(h, 0, sizeof(fe)); } +void fe_0(fe *h) { OPENSSL_memset(h, 0, sizeof(fe)); } -static void fe_loose_0(fe_loose *h) { OPENSSL_memset(h, 0, sizeof(fe_loose)); } +void fe_loose_0(fe_loose *h) { OPENSSL_memset(h, 0, sizeof(fe_loose)); } // h = 1 -static void fe_1(fe *h) { +void fe_1(fe *h) { OPENSSL_memset(h, 0, sizeof(fe)); h->v[0] = 1; } -static void fe_loose_1(fe_loose *h) { +void fe_loose_1(fe_loose *h) { OPENSSL_memset(h, 0, sizeof(fe_loose)); h->v[0] = 1; } // h = f + g // Can overlap h with f or g. -static void fe_add(fe_loose *h, const fe *f, const fe *g) { +void fe_add(fe_loose *h, const fe *f, const fe *g) { assert_fe(f->v); assert_fe(g->v); fiat_25519_add(h->v, f->v, g->v); @@ -196,59 +201,58 @@ static void fe_add(fe_loose *h, const fe *f, const fe *g) { // h = f - g // Can overlap h with f or g. -static void fe_sub(fe_loose *h, const fe *f, const fe *g) { +void fe_sub(fe_loose *h, const fe *f, const fe *g) { assert_fe(f->v); assert_fe(g->v); fiat_25519_sub(h->v, f->v, g->v); assert_fe_loose(h->v); } -static void fe_carry(fe *h, const fe_loose *f) { +void fe_carry(fe *h, const fe_loose *f) { assert_fe_loose(f->v); fiat_25519_carry(h->v, f->v); assert_fe(h->v); } -static void fe_mul_impl(fe_limb_t out[FE_NUM_LIMBS], - const fe_limb_t in1[FE_NUM_LIMBS], - const fe_limb_t in2[FE_NUM_LIMBS]) { +void fe_mul_impl(fe_limb_t out[FE_NUM_LIMBS], const fe_limb_t in1[FE_NUM_LIMBS], + const fe_limb_t in2[FE_NUM_LIMBS]) { assert_fe_loose(in1); assert_fe_loose(in2); fiat_25519_carry_mul(out, in1, in2); assert_fe(out); } -static void fe_mul_ltt(fe_loose *h, const fe *f, const fe *g) { +void fe_mul_ltt(fe_loose *h, const fe *f, const fe *g) { fe_mul_impl(h->v, f->v, g->v); } -static void fe_mul_llt(fe_loose *h, const fe_loose *f, const fe *g) { +void fe_mul_llt(fe_loose *h, const fe_loose *f, const fe *g) { fe_mul_impl(h->v, f->v, g->v); } -static void fe_mul_ttt(fe *h, const fe *f, const fe *g) { +void fe_mul_ttt(fe *h, const fe *f, const fe *g) { fe_mul_impl(h->v, f->v, g->v); } -static void fe_mul_tlt(fe *h, const fe_loose *f, const fe *g) { +void fe_mul_tlt(fe *h, const fe_loose *f, const fe *g) { fe_mul_impl(h->v, f->v, g->v); } -static void fe_mul_ttl(fe *h, const fe *f, const fe_loose *g) { +void fe_mul_ttl(fe *h, const fe *f, const fe_loose *g) { fe_mul_impl(h->v, f->v, g->v); } -static void fe_mul_tll(fe *h, const fe_loose *f, const fe_loose *g) { +void fe_mul_tll(fe *h, const fe_loose *f, const fe_loose *g) { fe_mul_impl(h->v, f->v, g->v); } -static void fe_sq_tl(fe *h, const fe_loose *f) { +void fe_sq_tl(fe *h, const fe_loose *f) { assert_fe_loose(f->v); fiat_25519_carry_square(h->v, f->v); assert_fe(h->v); } -static void fe_sq_tt(fe *h, const fe *f) { +void fe_sq_tt(fe *h, const fe *f) { assert_fe_loose(f->v); fiat_25519_carry_square(h->v, f->v); assert_fe(h->v); @@ -258,7 +262,7 @@ static void fe_sq_tt(fe *h, const fe *f) { // replace (f,g) with (f,g) if b == 0. // // Preconditions: b in {0,1}. -static void fe_cswap(fe *f, fe *g, fe_limb_t b) { +void fe_cswap(fe *f, fe *g, fe_limb_t b) { b = 0 - b; for (unsigned i = 0; i < FE_NUM_LIMBS; i++) { fe_limb_t x = f->v[i] ^ g->v[i]; @@ -268,14 +272,14 @@ static void fe_cswap(fe *f, fe *g, fe_limb_t b) { } } -static void fe_mul121666(fe *h, const fe_loose *f) { +void fe_mul121666(fe *h, const fe_loose *f) { assert_fe_loose(f->v); fiat_25519_carry_scmul_121666(h->v, f->v); assert_fe(h->v); } // h = -f -static void fe_neg(fe_loose *h, const fe *f) { +void fe_neg(fe_loose *h, const fe *f) { assert_fe(f->v); fiat_25519_opp(h->v, f->v); assert_fe_loose(h->v); @@ -285,8 +289,8 @@ static void fe_neg(fe_loose *h, const fe *f) { // replace (f,g) with (f,g) if b == 0. // // Preconditions: b in {0,1}. -static void fe_cmov(fe_loose *f, const fe_loose *g, fe_limb_t b) { - // Silence an unused function warning. |fiat_25519_selectznz| isn't quite the +void fe_cmov(fe_loose *f, const fe_loose *g, fe_limb_t b) { + // Silence an unused function warning. `fiat_25519_selectznz` isn't quite the // calling convention the rest of this code wants, so implement it by hand. // // TODO(davidben): Switch to fiat's calling convention, or ask fiat to emit a @@ -300,15 +304,28 @@ static void fe_cmov(fe_loose *f, const fe_loose *g, fe_limb_t b) { } } +// Replace (f,g) with (g,g) if b == 1; +// replace (f,g) with (f,g) if b == 0. +// +// Preconditions: b in {0,1}. +void fe_cmov(fe *f, const fe *g, fe_limb_t b) { + b = 0 - b; + for (unsigned i = 0; i < FE_NUM_LIMBS; i++) { + fe_limb_t x = f->v[i] ^ g->v[i]; + x &= b; + f->v[i] ^= x; + } +} + // h = f -static void fe_copy(fe *h, const fe *f) { OPENSSL_memmove(h, f, sizeof(fe)); } +void fe_copy(fe *h, const fe *f) { OPENSSL_memmove(h, f, sizeof(fe)); } -static void fe_copy_lt(fe_loose *h, const fe *f) { +void fe_copy_lt(fe_loose *h, const fe *f) { static_assert(sizeof(fe_loose) == sizeof(fe), "fe and fe_loose mismatch"); OPENSSL_memmove(h, f, sizeof(fe)); } -static void fe_loose_invert(fe *out, const fe_loose *z) { +void fe_loose_invert(fe *out, const fe_loose *z) { fe t0; fe t1; fe t2; @@ -366,7 +383,7 @@ static void fe_loose_invert(fe *out, const fe_loose *z) { fe_mul_ttt(out, &t1, &t0); } -static void fe_invert(fe *out, const fe *z) { +void fe_invert(fe *out, const fe *z) { fe_loose l; fe_copy_lt(&l, z); fe_loose_invert(out, &l); @@ -374,26 +391,32 @@ static void fe_invert(fe *out, const fe *z) { // return 0 if f == 0 // return 1 if f != 0 -static int fe_isnonzero(const fe_loose *f) { - fe tight; - fe_carry(&tight, f); +int fe_isnonzero(const fe *f) { uint8_t s[32]; - fe_tobytes(s, &tight); + fe_tobytes(s, f); static const uint8_t zero[32] = {0}; return CRYPTO_memcmp(s, zero, sizeof(zero)) != 0; } +// return 0 if f == 0 +// return 1 if f != 0 +int fe_isnonzero(const fe_loose *f) { + fe tight; + fe_carry(&tight, f); + return fe_isnonzero(&tight); +} + // return 1 if f is in {1,3,5,...,q-2} // return 0 if f is in {0,2,4,...,q-1} -static int fe_isnegative(const fe *f) { +int fe_isnegative(const fe *f) { uint8_t s[32]; fe_tobytes(s, f); return s[0] & 1; } -static void fe_sq2_tt(fe *h, const fe *f) { - // h = f^2 +void fe_sq2_tt(fe *h, const fe *f) { + // h = f² fe_sq_tt(h, f); // h = h + h @@ -402,7 +425,7 @@ static void fe_sq2_tt(fe *h, const fe *f) { fe_carry(h, &tmp); } -static void fe_pow22523(fe *out, const fe *z) { +void fe_pow22523(fe *out, const fe *z) { fe t0; fe t1; fe t2; @@ -459,6 +482,7 @@ static void fe_pow22523(fe *out, const fe *z) { fe_mul_ttt(out, &t0, z); } +} // namespace // Group operations. @@ -474,7 +498,9 @@ void bssl::x25519_ge_tobytes(uint8_t s[32], const ge_p2 *h) { s[31] ^= fe_isnegative(&x) << 7; } -static void ge_p3_tobytes(uint8_t s[32], const ge_p3 *h) { +namespace { + +void ge_p3_tobytes(uint8_t s[32], const ge_p3 *h) { fe recip; fe x; fe y; @@ -486,6 +512,8 @@ static void ge_p3_tobytes(uint8_t s[32], const ge_p3 *h) { s[31] ^= fe_isnegative(&x) << 7; } +} // namespace + int bssl::x25519_ge_frombytes_vartime(ge_p3 *h, const uint8_t s[32]) { fe u; fe_loose v; @@ -497,9 +525,9 @@ int bssl::x25519_ge_frombytes_vartime(ge_p3 *h, const uint8_t s[32]) { fe_1(&h->Z); fe_sq_tt(&w, &h->Y); fe_mul_ttt(&vxx, &w, &d); - fe_sub(&v, &w, &h->Z); // u = y^2-1 + fe_sub(&v, &w, &h->Z); // u = y² - 1 fe_carry(&u, &v); - fe_add(&v, &vxx, &h->Z); // v = dy^2+1 + fe_add(&v, &vxx, &h->Z); // v = dy² + 1 fe_mul_ttl(&w, &u, &v); // w = u*v fe_pow22523(&h->X, &w); // x = w^((q-5)/8) @@ -526,39 +554,43 @@ int bssl::x25519_ge_frombytes_vartime(ge_p3 *h, const uint8_t s[32]) { return 1; } -static void ge_p2_0(ge_p2 *h) { +namespace { + +void ge_p2_0(ge_p2 *h) { fe_0(&h->X); fe_1(&h->Y); fe_1(&h->Z); } -static void ge_p3_0(ge_p3 *h) { +void ge_p3_0(ge_p3 *h) { fe_0(&h->X); fe_1(&h->Y); fe_1(&h->Z); fe_0(&h->T); } -static void ge_cached_0(ge_cached *h) { +void ge_cached_0(ge_cached *h) { fe_loose_1(&h->YplusX); fe_loose_1(&h->YminusX); fe_loose_1(&h->Z); fe_loose_0(&h->T2d); } -static void ge_precomp_0(ge_precomp *h) { +void ge_precomp_0(ge_precomp *h) { fe_loose_1(&h->yplusx); fe_loose_1(&h->yminusx); fe_loose_0(&h->xy2d); } // r = p -static void ge_p3_to_p2(ge_p2 *r, const ge_p3 *p) { +void ge_p3_to_p2(ge_p2 *r, const ge_p3 *p) { fe_copy(&r->X, &p->X); fe_copy(&r->Y, &p->Y); fe_copy(&r->Z, &p->Z); } +} // namespace + // r = p void bssl::x25519_ge_p3_to_cached(ge_cached *r, const ge_p3 *p) { fe_add(&r->YplusX, &p->Y, &p->X); @@ -582,15 +614,17 @@ void bssl::x25519_ge_p1p1_to_p3(ge_p3 *r, const ge_p1p1 *p) { fe_mul_tll(&r->T, &p->X, &p->Y); } +namespace { + // r = p -static void ge_p1p1_to_cached(ge_cached *r, const ge_p1p1 *p) { +void ge_p1p1_to_cached(ge_cached *r, const ge_p1p1 *p) { ge_p3 t; x25519_ge_p1p1_to_p3(&t, p); x25519_ge_p3_to_cached(r, &t); } // r = 2 * p -static void ge_p2_dbl(ge_p1p1 *r, const ge_p2 *p) { +void ge_p2_dbl(ge_p1p1 *r, const ge_p2 *p) { fe trX, trZ, trT; fe t0; @@ -609,14 +643,14 @@ static void ge_p2_dbl(ge_p1p1 *r, const ge_p2 *p) { } // r = 2 * p -static void ge_p3_dbl(ge_p1p1 *r, const ge_p3 *p) { +void ge_p3_dbl(ge_p1p1 *r, const ge_p3 *p) { ge_p2 q; ge_p3_to_p2(&q, p); ge_p2_dbl(r, &q); } // r = p + q -static void ge_madd(ge_p1p1 *r, const ge_p3 *p, const ge_precomp *q) { +void ge_madd(ge_p1p1 *r, const ge_p3 *p, const ge_precomp *q) { fe trY, trZ, trT; fe_add(&r->X, &p->Y, &p->X); @@ -633,7 +667,7 @@ static void ge_madd(ge_p1p1 *r, const ge_p3 *p, const ge_precomp *q) { } // r = p - q -static void ge_msub(ge_p1p1 *r, const ge_p3 *p, const ge_precomp *q) { +void ge_msub(ge_p1p1 *r, const ge_p3 *p, const ge_precomp *q) { fe trY, trZ, trT; fe_add(&r->X, &p->Y, &p->X); @@ -649,6 +683,8 @@ static void ge_msub(ge_p1p1 *r, const ge_p3 *p, const ge_precomp *q) { fe_add(&r->T, &trZ, &trT); } +} // namespace + // r = p + q void bssl::x25519_ge_add(ge_p1p1 *r, const ge_p3 *p, const ge_cached *q) { fe trX, trY, trZ, trT; @@ -685,22 +721,26 @@ void bssl::x25519_ge_sub(ge_p1p1 *r, const ge_p3 *p, const ge_cached *q) { fe_add(&r->T, &trZ, &trT); } -static void cmov(ge_precomp *t, const ge_precomp *u, uint8_t b) { +namespace { + +void cmov(ge_precomp *t, const ge_precomp *u, uint8_t b) { fe_cmov(&t->yplusx, &u->yplusx, b); fe_cmov(&t->yminusx, &u->yminusx, b); fe_cmov(&t->xy2d, &u->xy2d, b); } +} // namespace + void bssl::x25519_ge_scalarmult_small_precomp( ge_p3 *h, const uint8_t a[32], const uint8_t precomp_table[15 * 2 * 32]) { - // precomp_table is first expanded into matching |ge_precomp| + // precomp_table is first expanded into matching `ge_precomp` // elements. ge_precomp multiples[15]; unsigned i; for (i = 0; i < 15; i++) { // The precomputed table is assumed to already clear the top bit, so - // |fe_frombytes_strict| may be used directly. + // `fe_frombytes_strict` may be used directly. const uint8_t *bytes = &precomp_table[i * (2 * 32)]; fe x, y; fe_frombytes_strict(&x, bytes); @@ -713,7 +753,7 @@ void bssl::x25519_ge_scalarmult_small_precomp( fe_mul_llt(&out->xy2d, &out->xy2d, &d2); } - // See the comment above |k25519SmallPrecomp| about the structure of the + // See the comment above `k25519SmallPrecomp` about the structure of the // precomputed elements. This loop does 64 additions and 64 doublings to // calculate the result. ge_p3_0(h); @@ -753,7 +793,9 @@ void bssl::x25519_ge_scalarmult_base(ge_p3 *h, const uint8_t a[32]) { #else -static void table_select(ge_precomp *t, const int pos, const signed char b) { +namespace { + +void table_select(ge_precomp *t, const int pos, const signed char b) { uint8_t bnegative = constant_time_msb_w(b); uint8_t babs = b - ((bnegative & b) << 1); @@ -787,6 +829,8 @@ static void table_select(ge_precomp *t, const int pos, const signed char b) { cmov(t, &minust, bnegative >> 7); } +} // namespace + // h = a * B // where a = a[0]+256*a[1]+...+256^31 a[31] // B is the Ed25519 base point (x,4/5) with x positive. @@ -855,13 +899,17 @@ void bssl::x25519_ge_scalarmult_base(ge_p3 *h, const uint8_t a[32]) { #endif -static void cmov_cached(ge_cached *t, ge_cached *u, uint8_t b) { +namespace { + +void cmov_cached(ge_cached *t, ge_cached *u, uint8_t b) { fe_cmov(&t->YplusX, &u->YplusX, b); fe_cmov(&t->YminusX, &u->YminusX, b); fe_cmov(&t->Z, &u->Z, b); fe_cmov(&t->T2d, &u->T2d, b); } +} // namespace + // r = scalar * A. // where a = a[0]+256*a[1]+...+256^31 a[31]. void bssl::x25519_ge_scalarmult(ge_p2 *r, const uint8_t *scalar, @@ -917,7 +965,9 @@ void bssl::x25519_ge_scalarmult(ge_p2 *r, const uint8_t *scalar, } } -static void slide(signed char *r, const uint8_t *a) { +namespace { + +void slide(signed char *r, const uint8_t *a) { int i; int b; int k; @@ -955,8 +1005,8 @@ static void slide(signed char *r, const uint8_t *a) { // where a = a[0]+256*a[1]+...+256^31 a[31]. // and b = b[0]+256*b[1]+...+256^31 b[31]. // B is the Ed25519 base point (x,4/5) with x positive. -static void ge_double_scalarmult_vartime(ge_p2 *r, const uint8_t *a, - const ge_p3 *A, const uint8_t *b) { +void ge_double_scalarmult_vartime(ge_p2 *r, const uint8_t *a, const ge_p3 *A, + const uint8_t *b) { signed char aslide[256]; signed char bslide[256]; ge_cached Ai[8]; // A,3A,5A,7A,9A,11A,13A,15A @@ -1024,11 +1074,11 @@ static void ge_double_scalarmult_vartime(ge_p2 *r, const uint8_t *a, } } -// int64_lshift21 returns |a << 21| but is defined when shifting bits into the +// int64_lshift21 returns `a << 21` but is defined when shifting bits into the // sign bit. This works around a language flaw in C. -static int64_t int64_lshift21(int64_t a) { - return (int64_t)((uint64_t)a << 21); -} +int64_t int64_lshift21(int64_t a) { return (int64_t)((uint64_t)a << 21); } + +} // namespace // The set of scalars is \Z/l // where l = 2^252 + 27742317777372353535851937790883648493. @@ -1381,8 +1431,10 @@ void bssl::x25519_sc_reduce(uint8_t s[64]) { // Output: // s[0]+256*s[1]+...+256^31*s[31] = (ab+c) mod l // where l = 2^252 + 27742317777372353535851937790883648493. -static void sc_muladd(uint8_t *s, const uint8_t *a, const uint8_t *b, - const uint8_t *c) { +namespace { + +void sc_muladd(uint8_t *s, const uint8_t *a, const uint8_t *b, + const uint8_t *c) { int64_t a0 = 2097151 & load_3(a); int64_t a1 = 2097151 & (load_4(a + 2) >> 5); int64_t a2 = 2097151 & (load_3(a + 5) >> 2); @@ -1862,19 +1914,19 @@ static void sc_muladd(uint8_t *s, const uint8_t *a, const uint8_t *b, s[31] = s11 >> 17; } +} // namespace + void ED25519_keypair(uint8_t out_public_key[32], uint8_t out_private_key[64]) { uint8_t seed[32]; RAND_bytes(seed, 32); ED25519_keypair_from_seed(out_public_key, out_private_key, seed); } -int ED25519_sign(uint8_t out_sig[64], const uint8_t *message, - size_t message_len, const uint8_t private_key[64]) { - // NOTE: The documentation on this function says that it returns zero on - // allocation failure. While that can't happen with the current - // implementation, we want to reserve the ability to allocate in this - // implementation in the future. +namespace { +int ed25519_sign(uint8_t out_sig[64], const uint8_t *hash_prefix, + size_t hash_prefix_len, const uint8_t *message, + size_t message_len, const uint8_t private_key[64]) { uint8_t az[SHA512_DIGEST_LENGTH]; SHA512(private_key, 32, az); @@ -1884,6 +1936,7 @@ int ED25519_sign(uint8_t out_sig[64], const uint8_t *message, SHA512_CTX hash_ctx; SHA512_Init(&hash_ctx); + SHA512_Update(&hash_ctx, hash_prefix, hash_prefix_len); SHA512_Update(&hash_ctx, az + 32, 32); SHA512_Update(&hash_ctx, message, message_len); uint8_t nonce[SHA512_DIGEST_LENGTH]; @@ -1895,6 +1948,7 @@ int ED25519_sign(uint8_t out_sig[64], const uint8_t *message, ge_p3_tobytes(out_sig, &R); SHA512_Init(&hash_ctx); + SHA512_Update(&hash_ctx, hash_prefix, hash_prefix_len); SHA512_Update(&hash_ctx, out_sig, 32); SHA512_Update(&hash_ctx, private_key + 32, 32); SHA512_Update(&hash_ctx, message, message_len); @@ -1909,7 +1963,8 @@ int ED25519_sign(uint8_t out_sig[64], const uint8_t *message, return 1; } -int ED25519_verify(const uint8_t *message, size_t message_len, +int ed25519_verify(const uint8_t *hash_prefix, size_t hash_prefix_len, + const uint8_t *message, size_t message_len, const uint8_t signature[64], const uint8_t public_key[32]) { ge_p3 A; if ((signature[63] & 224) != 0 || @@ -1953,6 +2008,7 @@ int ED25519_verify(const uint8_t *message, size_t message_len, SHA512_CTX hash_ctx; SHA512_Init(&hash_ctx); + SHA512_Update(&hash_ctx, hash_prefix, hash_prefix_len); SHA512_Update(&hash_ctx, signature, 32); SHA512_Update(&hash_ctx, public_key, 32); SHA512_Update(&hash_ctx, message, message_len); @@ -1970,6 +2026,84 @@ int ED25519_verify(const uint8_t *message, size_t message_len, return CRYPTO_memcmp(rcheck, rcopy, sizeof(rcheck)) == 0; } +} // namespace + +int ED25519_sign(uint8_t out_sig[64], const uint8_t *message, + size_t message_len, const uint8_t private_key[64]) { + // NOTE: The documentation on this function says that it returns zero on + // allocation failure. While that can't happen with the current + // implementation, we want to reserve the ability to allocate in this + // implementation in the future. + + return ed25519_sign(out_sig, nullptr, 0, message, message_len, private_key); +} + +int ED25519_verify(const uint8_t *message, size_t message_len, + const uint8_t signature[64], const uint8_t public_key[32]) { + return ed25519_verify(nullptr, 0, message, message_len, signature, + public_key); +} + +// ED25519_MAX_CONTEXT is the longest context permitted by RFC 8032. +#define ED25519_MAX_CONTEXT 255 +#define ED25519_MAX_HASH_PREFIX (32 + 1 + 1 + ED25519_MAX_CONTEXT) + +namespace { + +// ed25519_build_hash_prefix implements the `dom2` function from +// https://datatracker.ietf.org/doc/html/rfc8032#section-2 +int ed25519_build_hash_prefix(uint8_t out_hash_prefix[ED25519_MAX_HASH_PREFIX], + size_t *out_hash_prefix_len, + const uint8_t *context, size_t context_len) { + if (context_len > ED25519_MAX_CONTEXT) { + return 0; + } + + OPENSSL_memcpy(out_hash_prefix, "SigEd25519 no Ed25519 collisions", 32); + out_hash_prefix[32] = 1; + out_hash_prefix[33] = static_cast(context_len); + OPENSSL_memcpy(&out_hash_prefix[34], context, context_len); + *out_hash_prefix_len = 32 + 1 + 1 + context_len; + return 1; +} + +} // namespace + +int ED25519_sign_prehashed(uint8_t out_sig[64], const uint8_t *context, + size_t context_len, + const uint8_t sha512_digest[SHA512_DIGEST_LENGTH], + const uint8_t private_key[64]) { + // NOTE: The documentation on this function says that it returns zero on + // allocation failure. While that can't happen with the current + // implementation, we want to reserve the ability to allocate in this + // implementation in the future. + + uint8_t hash_prefix[ED25519_MAX_HASH_PREFIX]; + size_t hash_prefix_len; + if (!ed25519_build_hash_prefix(hash_prefix, &hash_prefix_len, context, + context_len)) { + return 0; + } + + return ed25519_sign(out_sig, hash_prefix, hash_prefix_len, sha512_digest, + SHA512_DIGEST_LENGTH, private_key); +} + +int ED25519_verify_prehashed(const uint8_t *context, size_t context_len, + const uint8_t sha512_digest[SHA512_DIGEST_LENGTH], + const uint8_t signature[64], + const uint8_t public_key[32]) { + uint8_t hash_prefix[ED25519_MAX_HASH_PREFIX]; + size_t hash_prefix_len; + if (!ed25519_build_hash_prefix(hash_prefix, &hash_prefix_len, context, + context_len)) { + return 0; + } + + return ed25519_verify(hash_prefix, hash_prefix_len, sha512_digest, + SHA512_DIGEST_LENGTH, signature, public_key); +} + void ED25519_keypair_from_seed(uint8_t out_public_key[32], uint8_t out_private_key[64], const uint8_t seed[32]) { @@ -1991,9 +2125,10 @@ void ED25519_keypair_from_seed(uint8_t out_public_key[32], } -static void x25519_scalar_mult_generic(uint8_t out[32], - const uint8_t scalar[32], - const uint8_t point[32]) { +namespace { + +void x25519_scalar_mult_generic(uint8_t out[32], const uint8_t scalar[32], + const uint8_t point[32]) { fe x1, x2, z2, x3, z3, tmp0, tmp1; fe_loose x2l, z2l, x3l, tmp0l, tmp1l; @@ -2075,8 +2210,8 @@ static void x25519_scalar_mult_generic(uint8_t out[32], fe_tobytes(out, &x2); } -static void x25519_scalar_mult(uint8_t out[32], const uint8_t scalar[32], - const uint8_t point[32]) { +void x25519_scalar_mult(uint8_t out[32], const uint8_t scalar[32], + const uint8_t point[32]) { #if defined(BORINGSSL_X25519_NEON) if (CRYPTO_is_NEON_capable()) { x25519_NEON(out, scalar, point); @@ -2093,6 +2228,8 @@ static void x25519_scalar_mult(uint8_t out[32], const uint8_t scalar[32], x25519_scalar_mult_generic(out, scalar, point); } +} // namespace + void X25519_keypair(uint8_t out_public_value[32], uint8_t out_private_key[32]) { RAND_bytes(out_private_key, 32); @@ -2155,3 +2292,191 @@ void X25519_public_from_private(uint8_t out_public_value[32], fe_tobytes(out_public_value, &zminusy_inv); CONSTTIME_DECLASSIFY(out_public_value, 32); } + +namespace { + +void fe_from_limb(fe *h, fe_limb_t val) { + fe_0(h); + h->v[0] = val; + assert_fe(h->v); +} + +// map_to_curve_elligator2 implements the straight-line Elligator 2 mapping +// for Curve25519 from RFC 9380, Appendix G.2.1. +// +// It maps a field element u to a Montgomery point (xn / xd, y / 1) on the curve +// t^2 = s^3 + 486662*s^2 + s. +void map_to_curve_elligator2(fe *out_xMn, fe *out_xMd, fe *out_yMn, + const fe *u) { + fe one, J_fe, c2_fe; + fe_1(&one); + fe_from_limb(&J_fe, 486662); + // c2 = 2^((p + 3) / 8) mod (2^255 - 19) in little-endian. + constexpr static uint8_t kC2[32] = { + 0xb1, 0xa0, 0x0e, 0x4a, 0x27, 0x1b, 0xee, 0xc4, 0x78, 0xe4, 0x2f, + 0xad, 0x06, 0x18, 0x43, 0x2f, 0xa7, 0xd7, 0xfb, 0x3d, 0x99, 0x00, + 0x4d, 0x2b, 0x0b, 0xdf, 0xc1, 0x4f, 0x80, 0x24, 0x83, 0x2b}; + fe_frombytes(&c2_fe, kC2); + + // 1-2. tv1 = 2 * u^2 + fe tv1; + fe_sq2_tt(&tv1, u); + + // 3. xd = tv1 + 1 + fe_loose xd_loose; + fe_add(&xd_loose, &tv1, &one); + + // 4. x1n = -J + fe_loose x1n_loose; + fe_neg(&x1n_loose, &J_fe); + + // 5. tv2 = xd^2 + fe tv2; + fe_sq_tl(&tv2, &xd_loose); + + // 6. gxd = xd^3 + fe gxd; + fe_mul_ttl(&gxd, &tv2, &xd_loose); + + fe gx1; + { + fe_loose l_tmp; + // 7. gx1 = J * tv1 + fe_mul_ltt(&l_tmp, &J_fe, &tv1); + // 8. gx1 = gx1 * x1n + fe_mul_tll(&gx1, &l_tmp, &x1n_loose); + // 9. gx1 = gx1 + tv2 + fe_add(&l_tmp, &gx1, &tv2); + // 10. gx1 = gx1 * x1n + fe_mul_tll(&gx1, &l_tmp, &x1n_loose); + } + + fe tv3; + // 11. tv3 = gxd^2 + fe_sq_tt(&tv3, &gxd); + // 12. tv2 = tv3^2 + fe_sq_tt(&tv2, &tv3); + // 13. tv3 = tv3 * gxd + fe_mul_ttt(&tv3, &tv3, &gxd); + // 14. tv3 = tv3 * gx1 + fe_mul_ttt(&tv3, &tv3, &gx1); + // 15. tv2 = tv2 * tv3 + fe_mul_ttt(&tv2, &tv2, &tv3); + + // 16. y11 = tv2^c4 = (gx1 * gxd^7)^((p - 5) / 8) + fe y11; + fe_pow22523(&y11, &tv2); + // 17. y11 = y11 * tv3 + fe_mul_ttt(&y11, &y11, &tv3); + + // 18. y12 = y11 * c3 + fe y12; + fe_mul_ttt(&y12, &y11, &sqrtm1); + + // 19. tv2 = y11 ^ 2 + fe_sq_tt(&tv2, &y11); + // 20. tv2 = tv2 * gxd + fe_mul_ttt(&tv2, &tv2, &gxd); + // 21. e1 = tv2 == gx1 + fe_limb_t e1; + { + fe_loose l_tmp; + fe_sub(&l_tmp, &tv2, &gx1); + e1 = 1 - fe_isnonzero(&l_tmp); + } + + // 22. y1 = CMOV(y12, y11, e1) # If g(x1) is square, this is its sqrt + fe y1; + fe_copy(&y1, &y12); + fe_cmov(&y1, &y11, e1); + + // 23. x2n = x1n * tv1 + fe x2n; + fe_mul_ttl(&x2n, &tv1, &x1n_loose); + + // 24. y21 = y11 * u + fe y21; + fe_mul_ttt(&y21, &y11, u); + // 25. y21 = y21 * c2 + fe_mul_ttt(&y21, &y21, &c2_fe); + + // 26. y22 = y21 * c3 + fe y22; + fe_mul_ttt(&y22, &y21, &sqrtm1); + + // 27. gx2 = gx1 * tv1 # g(x2) = gx2 / gxd = 2 * u^2 * g(x1) + fe gx2; + fe_mul_ttt(&gx2, &gx1, &tv1); + + // 28. tv2 = y21^2 + fe_sq_tt(&tv2, &y21); + // 29. tv2 = tv2 * gxd + fe_mul_ttt(&tv2, &tv2, &gxd); + // 30. e2 = tv2 == gx2 + fe_limb_t e2; + { + fe_loose l_tmp; + fe_sub(&l_tmp, &tv2, &gx2); + e2 = 1 - fe_isnonzero(&l_tmp); + } + + // 31. y2 = CMOV(y22, y21, e2) # If g(x2) is square, this is its sqrt + fe y2; + fe_copy(&y2, &y22); + fe_cmov(&y2, &y21, e2); + + // 32. tv2 = y1^2 + fe_sq_tt(&tv2, &y1); + // 33. tv2 = tv2 * gxd + fe_mul_ttt(&tv2, &tv2, &gxd); + // 34. e3 = tv2 == gx1 + fe_limb_t e3; + { + fe_loose l_tmp; + fe_sub(&l_tmp, &tv2, &gx1); + e3 = 1 - fe_isnonzero(&l_tmp); + } + + fe xn, y; + // 35. xn = CMOV(x2n, x1n, e3) # If e3, x = x1, else x = x2 + // or xn = CMOV(x1n, x2n, 1 - e3) + fe_carry(&xn, &x1n_loose); + fe_cmov(&xn, &x2n, 1 - e3); + // 36. y = CMOV(y2, y1, e3) # If e3, y = y1, else y = y2 + fe_copy(&y, &y2); + fe_cmov(&y, &y1, e3); + + // 37. e4 = sgn0(y) == 1 # Fix sign of y + fe_limb_t e4 = fe_isnegative(&y); + fe negy; + { + fe_loose l_tmp; + fe_neg(&l_tmp, &y); + fe_carry(&negy, &l_tmp); + } + // 38. y = CMOV(y, -y, e3 XOR e4) + fe_cmov(&y, &negy, e3 ^ e4); + + // 39. return (xn, xd, y, /* 1 */) + fe_copy(out_xMn, &xn); + fe_carry(out_xMd, &xd_loose); + fe_copy(out_yMn, &y); +} + +} // namespace + +void bssl::map_to_curve_curve25519_elligator2(uint8_t out_qx[32], + uint8_t out_qy[32], + const uint8_t u[32]) { + fe u_fe; + fe_frombytes(&u_fe, u); + + fe xMn, xMd, yMn; + map_to_curve_elligator2(&xMn, &xMd, &yMn, &u_fe); + + fe xMd_inv, qx; + fe_invert(&xMd_inv, &xMd); + fe_mul_ttt(&qx, &xMn, &xMd_inv); + fe_tobytes(out_qx, &qx); + fe_tobytes(out_qy, &yMn); +} diff --git a/third_party/boringssl/src/crypto/curve25519/curve25519_tables.h b/third_party/boringssl/src/crypto/curve25519/curve25519_tables.h index 942336d08..723a417a0 100644 --- a/third_party/boringssl/src/crypto/curve25519/curve25519_tables.h +++ b/third_party/boringssl/src/crypto/curve25519/curve25519_tables.h @@ -16,7 +16,9 @@ // ./make_curve25519_tables.py > curve25519_tables.h -static const bssl::fe d = {{ +namespace { + +const bssl::fe d = {{ #if defined(OPENSSL_64_BIT) 929955233495203, 466365720129213, 1662059464998953, 2033849074728123, 1442794654840575 @@ -26,7 +28,7 @@ static const bssl::fe d = {{ #endif }}; -static const bssl::fe sqrtm1 = {{ +const bssl::fe sqrtm1 = {{ #if defined(OPENSSL_64_BIT) 1718705420411056, 234908883556509, 2233514472574048, 2117202627021982, 765476049583133 @@ -36,7 +38,7 @@ static const bssl::fe sqrtm1 = {{ #endif }}; -static const bssl::fe d2 = {{ +const bssl::fe d2 = {{ #if defined(OPENSSL_64_BIT) 1859910466990425, 932731440258426, 1072319116312658, 1815898335770999, 633789495995903 @@ -46,17 +48,257 @@ static const bssl::fe d2 = {{ #endif }}; +// Bi[i] = (2*i+1)*B +const bssl::ge_precomp Bi[8] = { + { + {{ +#if defined(OPENSSL_64_BIT) + 1288382639258501, 245678601348599, 269427782077623, + 1462984067271730, 137412439391563 +#else + 25967493, 19198397, 29566455, 3660896, 54414519, 4014786, 27544626, + 21800161, 61029707, 2047604 +#endif + }}, + {{ +#if defined(OPENSSL_64_BIT) + 62697248952638, 204681361388450, 631292143396476, 338455783676468, + 1213667448819585 +#else + 54563134, 934261, 64385954, 3049989, 66381436, 9406985, 12720692, + 5043384, 19500929, 18085054 +#endif + }}, + {{ +#if defined(OPENSSL_64_BIT) + 301289933810280, 1259582250014073, 1422107436869536, + 796239922652654, 1953934009299142 +#else + 58370664, 4489569, 9688441, 18769238, 10184608, 21191052, 29287918, + 11864899, 42594502, 29115885 +#endif + }}, + }, + { + {{ +#if defined(OPENSSL_64_BIT) + 1601611775252272, 1720807796594148, 1132070835939856, + 1260455018889551, 2147779492816911 +#else + 15636272, 23865875, 24204772, 25642034, 616976, 16869170, 27787599, + 18782243, 28944399, 32004408 +#endif + }}, + {{ +#if defined(OPENSSL_64_BIT) + 316559037616741, 2177824224946892, 1459442586438991, + 1461528397712656, 751590696113597 +#else + 16568933, 4717097, 55552716, 32452109, 15682895, 21747389, 16354576, + 21778470, 7689661, 11199574 +#endif + }}, + {{ +#if defined(OPENSSL_64_BIT) + 1850748884277385, 1200145853858453, 1068094770532492, + 672251375690438, 1586055907191707 +#else + 30464137, 27578307, 55329429, 17883566, 23220364, 15915852, 7512774, + 10017326, 49359771, 23634074 +#endif + }}, + }, + { + {{ +#if defined(OPENSSL_64_BIT) + 769950342298419, 132954430919746, 844085933195555, 974092374476333, + 726076285546016 +#else + 10861363, 11473154, 27284546, 1981175, 37044515, 12577860, 32867885, + 14515107, 51670560, 10819379 +#endif + }}, + {{ +#if defined(OPENSSL_64_BIT) + 425251763115706, 608463272472562, 442562545713235, 837766094556764, + 374555092627893 +#else + 4708026, 6336745, 20377586, 9066809, 55836755, 6594695, 41455196, + 12483687, 54440373, 5581305 +#endif + }}, + {{ +#if defined(OPENSSL_64_BIT) + 1086255230780037, 274979815921559, 1960002765731872, + 929474102396301, 1190409889297339 +#else + 19563141, 16186464, 37722007, 4097518, 10237984, 29206317, 28542349, + 13850243, 43430843, 17738489 +#endif + }}, + }, + { + {{ +#if defined(OPENSSL_64_BIT) + 665000864555967, 2065379846933859, 370231110385876, 350988370788628, + 1233371373142985 +#else + 5153727, 9909285, 1723747, 30776558, 30523604, 5516873, 19480852, + 5230134, 43156425, 18378665 +#endif + }}, + {{ +#if defined(OPENSSL_64_BIT) + 2019367628972465, 676711900706637, 110710997811333, + 1108646842542025, 517791959672113 +#else + 36839857, 30090922, 7665485, 10083793, 28475525, 1649722, 20654025, + 16520125, 30598449, 7715701 +#endif + }}, + {{ +#if defined(OPENSSL_64_BIT) + 965130719900578, 247011430587952, 526356006571389, 91986625355052, + 2157223321444601 +#else + 28881826, 14381568, 9657904, 3680757, 46927229, 7843315, 35708204, + 1370707, 29794553, 32145132 +#endif + }}, + }, + { + {{ +#if defined(OPENSSL_64_BIT) + 1802695059465007, 1664899123557221, 593559490740857, + 2160434469266659, 927570450755031 +#else + 44589871, 26862249, 14201701, 24808930, 43598457, 8844725, 18474211, + 32192982, 54046167, 13821876 +#endif + }}, + {{ +#if defined(OPENSSL_64_BIT) + 1725674970513508, 1933645953859181, 1542344539275782, + 1767788773573747, 1297447965928905 +#else + 60653668, 25714560, 3374701, 28813570, 40010246, 22982724, 31655027, + 26342105, 18853321, 19333481 +#endif + }}, + {{ +#if defined(OPENSSL_64_BIT) + 1381809363726107, 1430341051343062, 2061843536018959, + 1551778050872521, 2036394857967624 +#else + 4566811, 20590564, 38133974, 21313742, 59506191, 30723862, 58594505, + 23123294, 2207752, 30344648 +#endif + }}, + }, + { + {{ +#if defined(OPENSSL_64_BIT) + 1970894096313054, 528066325833207, 1619374932191227, + 2207306624415883, 1169170329061080 +#else + 41954014, 29368610, 29681143, 7868801, 60254203, 24130566, 54671499, + 32891431, 35997400, 17421995 +#endif + }}, + {{ +#if defined(OPENSSL_64_BIT) + 2070390218572616, 1458919061857835, 624171843017421, + 1055332792707765, 433987520732508 +#else + 25576264, 30851218, 7349803, 21739588, 16472781, 9300885, 3844789, + 15725684, 171356, 6466918 +#endif + }}, + {{ +#if defined(OPENSSL_64_BIT) + 893653801273833, 1168026499324677, 1242553501121234, + 1306366254304474, 1086752658510815 +#else + 23103977, 13316479, 9739013, 17404951, 817874, 18515490, 8965338, + 19466374, 36393951, 16193876 +#endif + }}, + }, + { + {{ +#if defined(OPENSSL_64_BIT) + 213454002618221, 939771523987438, 1159882208056014, 317388369627517, + 621213314200687 +#else + 33587053, 3180712, 64714734, 14003686, 50205390, 17283591, 17238397, + 4729455, 49034351, 9256799 +#endif + }}, + {{ +#if defined(OPENSSL_64_BIT) + 1971678598905747, 338026507889165, 762398079972271, 655096486107477, + 42299032696322 +#else + 41926547, 29380300, 32336397, 5036987, 45872047, 11360616, 22616405, + 9761698, 47281666, 630304 +#endif + }}, + {{ +#if defined(OPENSSL_64_BIT) + 177130678690680, 1754759263300204, 1864311296286618, + 1180675631479880, 1292726903152791 +#else + 53388152, 2639452, 42871404, 26147950, 9494426, 27780403, 60554312, + 17593437, 64659607, 19263131 +#endif + }}, + }, + { + {{ +#if defined(OPENSSL_64_BIT) + 1913163449625248, 460779200291993, 2193883288642314, + 1008900146920800, 1721983679009502 +#else + 63957664, 28508356, 9282713, 6866145, 35201802, 32691408, 48168288, + 15033783, 25105118, 25659556 +#endif + }}, + {{ +#if defined(OPENSSL_64_BIT) + 1070401523076875, 1272492007800961, 1910153608563310, + 2075579521696771, 1191169788841221 +#else + 42782475, 15950225, 35307649, 18961608, 55446126, 28463506, 1573891, + 30928545, 2198789, 17749813 +#endif + }}, + {{ +#if defined(OPENSSL_64_BIT) + 692896803108118, 500174642072499, 2068223309439677, + 1162190621851337, 1426986007309901 +#else + 64009494, 10324966, 64867251, 7453182, 61661885, 30818928, 53296841, + 17317989, 34647629, 21263748 +#endif + }}, + }, +}; + +} // namespace + #if defined(OPENSSL_SMALL) +namespace { + // This block of code replaces the standard base-point table with a much smaller // one. The standard table is 30,720 bytes while this one is just 960. // // This table contains 15 pairs of group elements, (x, y), where each field -// element is serialised with |fe_tobytes|. If |i| is the index of the group -// element then consider i+1 as a four-bit number: (i₀, i₁, i₂, i₃) (where i₀ -// is the most significant bit). The value of the group element is then: +// element is serialised with `fe_tobytes`. If `i` is the index of the group +// element then consider i+1 as a four-bit number: (i₀, i₁, i₂, i₃) where i₀ +// is the most significant bit. The value of the group element is then: // (i₀×2^192 + i₁×2^128 + i₂×2^64 + i₃)G, where G is the generator. -static const uint8_t k25519SmallPrecomp[15 * 2 * 32] = { +const uint8_t k25519SmallPrecomp[15 * 2 * 32] = { 0x1a, 0xd5, 0x25, 0x8f, 0x60, 0x2d, 0x56, 0xc9, 0xb2, 0xa7, 0x25, 0x95, 0x60, 0xc7, 0x2c, 0x69, 0x5c, 0xdc, 0xd6, 0xfd, 0x31, 0xe2, 0xa4, 0xc0, 0xfe, 0x53, 0x6e, 0xcd, 0xd3, 0x36, 0x69, 0x21, 0x58, 0x66, 0x66, 0x66, @@ -139,7 +381,9 @@ static const uint8_t k25519SmallPrecomp[15 * 2 * 32] = { 0x45, 0xc9, 0x8b, 0x17, 0x79, 0xe7, 0xc7, 0x90, 0x99, 0x3a, 0x18, 0x25, }; -#else +} // namespace + +#else // !OPENSSL_SMALL // k25519Precomp[i][j] = (j+1)*256^i*B const uint8_t bssl::k25519Precomp[32][8][3][32] = { @@ -3025,240 +3269,4 @@ const uint8_t bssl::k25519Precomp[32][8][3][32] = { }, }; -#endif // OPENSSL_SMALL - -// Bi[i] = (2*i+1)*B -static const bssl::ge_precomp Bi[8] = { - { - {{ -#if defined(OPENSSL_64_BIT) - 1288382639258501, 245678601348599, 269427782077623, - 1462984067271730, 137412439391563 -#else - 25967493, 19198397, 29566455, 3660896, 54414519, 4014786, 27544626, - 21800161, 61029707, 2047604 -#endif - }}, - {{ -#if defined(OPENSSL_64_BIT) - 62697248952638, 204681361388450, 631292143396476, 338455783676468, - 1213667448819585 -#else - 54563134, 934261, 64385954, 3049989, 66381436, 9406985, 12720692, - 5043384, 19500929, 18085054 -#endif - }}, - {{ -#if defined(OPENSSL_64_BIT) - 301289933810280, 1259582250014073, 1422107436869536, - 796239922652654, 1953934009299142 -#else - 58370664, 4489569, 9688441, 18769238, 10184608, 21191052, 29287918, - 11864899, 42594502, 29115885 -#endif - }}, - }, - { - {{ -#if defined(OPENSSL_64_BIT) - 1601611775252272, 1720807796594148, 1132070835939856, - 1260455018889551, 2147779492816911 -#else - 15636272, 23865875, 24204772, 25642034, 616976, 16869170, 27787599, - 18782243, 28944399, 32004408 -#endif - }}, - {{ -#if defined(OPENSSL_64_BIT) - 316559037616741, 2177824224946892, 1459442586438991, - 1461528397712656, 751590696113597 -#else - 16568933, 4717097, 55552716, 32452109, 15682895, 21747389, 16354576, - 21778470, 7689661, 11199574 -#endif - }}, - {{ -#if defined(OPENSSL_64_BIT) - 1850748884277385, 1200145853858453, 1068094770532492, - 672251375690438, 1586055907191707 -#else - 30464137, 27578307, 55329429, 17883566, 23220364, 15915852, 7512774, - 10017326, 49359771, 23634074 -#endif - }}, - }, - { - {{ -#if defined(OPENSSL_64_BIT) - 769950342298419, 132954430919746, 844085933195555, 974092374476333, - 726076285546016 -#else - 10861363, 11473154, 27284546, 1981175, 37044515, 12577860, 32867885, - 14515107, 51670560, 10819379 -#endif - }}, - {{ -#if defined(OPENSSL_64_BIT) - 425251763115706, 608463272472562, 442562545713235, 837766094556764, - 374555092627893 -#else - 4708026, 6336745, 20377586, 9066809, 55836755, 6594695, 41455196, - 12483687, 54440373, 5581305 -#endif - }}, - {{ -#if defined(OPENSSL_64_BIT) - 1086255230780037, 274979815921559, 1960002765731872, - 929474102396301, 1190409889297339 -#else - 19563141, 16186464, 37722007, 4097518, 10237984, 29206317, 28542349, - 13850243, 43430843, 17738489 -#endif - }}, - }, - { - {{ -#if defined(OPENSSL_64_BIT) - 665000864555967, 2065379846933859, 370231110385876, 350988370788628, - 1233371373142985 -#else - 5153727, 9909285, 1723747, 30776558, 30523604, 5516873, 19480852, - 5230134, 43156425, 18378665 -#endif - }}, - {{ -#if defined(OPENSSL_64_BIT) - 2019367628972465, 676711900706637, 110710997811333, - 1108646842542025, 517791959672113 -#else - 36839857, 30090922, 7665485, 10083793, 28475525, 1649722, 20654025, - 16520125, 30598449, 7715701 -#endif - }}, - {{ -#if defined(OPENSSL_64_BIT) - 965130719900578, 247011430587952, 526356006571389, 91986625355052, - 2157223321444601 -#else - 28881826, 14381568, 9657904, 3680757, 46927229, 7843315, 35708204, - 1370707, 29794553, 32145132 -#endif - }}, - }, - { - {{ -#if defined(OPENSSL_64_BIT) - 1802695059465007, 1664899123557221, 593559490740857, - 2160434469266659, 927570450755031 -#else - 44589871, 26862249, 14201701, 24808930, 43598457, 8844725, 18474211, - 32192982, 54046167, 13821876 -#endif - }}, - {{ -#if defined(OPENSSL_64_BIT) - 1725674970513508, 1933645953859181, 1542344539275782, - 1767788773573747, 1297447965928905 -#else - 60653668, 25714560, 3374701, 28813570, 40010246, 22982724, 31655027, - 26342105, 18853321, 19333481 -#endif - }}, - {{ -#if defined(OPENSSL_64_BIT) - 1381809363726107, 1430341051343062, 2061843536018959, - 1551778050872521, 2036394857967624 -#else - 4566811, 20590564, 38133974, 21313742, 59506191, 30723862, 58594505, - 23123294, 2207752, 30344648 -#endif - }}, - }, - { - {{ -#if defined(OPENSSL_64_BIT) - 1970894096313054, 528066325833207, 1619374932191227, - 2207306624415883, 1169170329061080 -#else - 41954014, 29368610, 29681143, 7868801, 60254203, 24130566, 54671499, - 32891431, 35997400, 17421995 -#endif - }}, - {{ -#if defined(OPENSSL_64_BIT) - 2070390218572616, 1458919061857835, 624171843017421, - 1055332792707765, 433987520732508 -#else - 25576264, 30851218, 7349803, 21739588, 16472781, 9300885, 3844789, - 15725684, 171356, 6466918 -#endif - }}, - {{ -#if defined(OPENSSL_64_BIT) - 893653801273833, 1168026499324677, 1242553501121234, - 1306366254304474, 1086752658510815 -#else - 23103977, 13316479, 9739013, 17404951, 817874, 18515490, 8965338, - 19466374, 36393951, 16193876 -#endif - }}, - }, - { - {{ -#if defined(OPENSSL_64_BIT) - 213454002618221, 939771523987438, 1159882208056014, 317388369627517, - 621213314200687 -#else - 33587053, 3180712, 64714734, 14003686, 50205390, 17283591, 17238397, - 4729455, 49034351, 9256799 -#endif - }}, - {{ -#if defined(OPENSSL_64_BIT) - 1971678598905747, 338026507889165, 762398079972271, 655096486107477, - 42299032696322 -#else - 41926547, 29380300, 32336397, 5036987, 45872047, 11360616, 22616405, - 9761698, 47281666, 630304 -#endif - }}, - {{ -#if defined(OPENSSL_64_BIT) - 177130678690680, 1754759263300204, 1864311296286618, - 1180675631479880, 1292726903152791 -#else - 53388152, 2639452, 42871404, 26147950, 9494426, 27780403, 60554312, - 17593437, 64659607, 19263131 -#endif - }}, - }, - { - {{ -#if defined(OPENSSL_64_BIT) - 1913163449625248, 460779200291993, 2193883288642314, - 1008900146920800, 1721983679009502 -#else - 63957664, 28508356, 9282713, 6866145, 35201802, 32691408, 48168288, - 15033783, 25105118, 25659556 -#endif - }}, - {{ -#if defined(OPENSSL_64_BIT) - 1070401523076875, 1272492007800961, 1910153608563310, - 2075579521696771, 1191169788841221 -#else - 42782475, 15950225, 35307649, 18961608, 55446126, 28463506, 1573891, - 30928545, 2198789, 17749813 -#endif - }}, - {{ -#if defined(OPENSSL_64_BIT) - 692896803108118, 500174642072499, 2068223309439677, - 1162190621851337, 1426986007309901 -#else - 64009494, 10324966, 64867251, 7453182, 61661885, 30818928, 53296841, - 17317989, 34647629, 21263748 -#endif - }}, - }, -}; +#endif // !OPENSSL_SMALL diff --git a/third_party/boringssl/src/crypto/curve25519/internal.h b/third_party/boringssl/src/crypto/curve25519/internal.h index c3bc6fe09..d5f9033b5 100644 --- a/third_party/boringssl/src/crypto/curve25519/internal.h +++ b/third_party/boringssl/src/crypto/curve25519/internal.h @@ -15,9 +15,11 @@ #ifndef OPENSSL_HEADER_CRYPTO_CURVE25519_INTERNAL_H #define OPENSSL_HEADER_CRYPTO_CURVE25519_INTERNAL_H +#include #include -#include "../internal.h" +#include "../mem_internal.h" + BSSL_NAMESPACE_BEGIN @@ -30,7 +32,7 @@ extern "C" void x25519_NEON(uint8_t out[32], const uint8_t scalar[32], #endif #if !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_SMALL) && \ - defined(__GNUC__) && defined(__x86_64__) && !defined(OPENSSL_WINDOWS) + (defined(__APPLE__) || defined(__ELF__)) && defined(OPENSSL_X86_64) #define BORINGSSL_FE25519_ADX // fiat_curve25519_adx_mul is defined in @@ -51,33 +53,41 @@ void x25519_ge_scalarmult_base_adx(uint8_t h[4][32], const uint8_t a[32]); #endif #if defined(OPENSSL_64_BIT) -// fe means field element. Here the field is \Z/(2^255-19). An element t, -// entries t[0]...t[4], represents the integer t[0]+2^51 t[1]+2^102 t[2]+2^153 -// t[3]+2^204 t[4]. +// fe means field element. Here the field is ℤ/(2^255-19). An element t, +// entries t[0]...t[4], represents the integer +// t[0] + 2^51 t[1] + 2^102 t[2] + 2^153 t[3] + 2^204 t[4]. // fe limbs are bounded by 1.125*2^51. // Multiplication and carrying produce fe from fe_loose. -typedef struct fe { uint64_t v[5]; } fe; +typedef struct fe { + uint64_t v[5]; +} fe; // fe_loose limbs are bounded by 3.375*2^51. // Addition and subtraction produce fe_loose from (fe, fe). -typedef struct fe_loose { uint64_t v[5]; } fe_loose; +typedef struct fe_loose { + uint64_t v[5]; +} fe_loose; #else -// fe means field element. Here the field is \Z/(2^255-19). An element t, -// entries t[0]...t[9], represents the integer t[0]+2^26 t[1]+2^51 t[2]+2^77 -// t[3]+2^102 t[4]+...+2^230 t[9]. +// fe means field element. Here the field is ℤ/(2^255-19). +// An element t, entries t[0]...t[9], represents the integer +// t[0] + 2^26 t[1] + 2^51 t[2] + 2^77 t[3] + 2^102 t[4] + ... + 2^230 t[9]. // fe limbs are bounded by 1.125*2^26,1.125*2^25,1.125*2^26,1.125*2^25,etc. // Multiplication and carrying produce fe from fe_loose. -typedef struct fe { uint32_t v[10]; } fe; - -// fe_loose limbs are bounded by 3.375*2^26,3.375*2^25,3.375*2^26,3.375*2^25,etc. -// Addition and subtraction produce fe_loose from (fe, fe). -typedef struct fe_loose { uint32_t v[10]; } fe_loose; +typedef struct fe { + uint32_t v[10]; +} fe; + +// fe_loose limbs are bounded by 3.375*2^26, 3.375*2^25, 3.375*2^26, 3.375*2^25, +// etc. Addition and subtraction produce fe_loose from (fe, fe). +typedef struct fe_loose { + uint32_t v[10]; +} fe_loose; #endif // ge means group element. // // Here the group is the set of pairs (x,y) of field elements (see fe.h) -// satisfying -x^2 + y^2 = 1 + d x^2y^2 +// satisfying -x² + y² = 1 + d x²y² // where d = -121665/121666. // // Representations: @@ -138,6 +148,9 @@ enum spake2_state_t { spake2_state_key_generated, }; +void map_to_curve_curve25519_elligator2(uint8_t out_qx[32], uint8_t out_qy[32], + const uint8_t u[32]); + BSSL_NAMESPACE_END struct spake2_ctx_st { @@ -154,8 +167,34 @@ struct spake2_ctx_st { char disable_password_scalar_hack; }; +DECLARE_OPAQUE_STRUCT(cpace_ctx_st, CpaceCtx) + BSSL_NAMESPACE_BEGIN +class CpaceCtx : public cpace_ctx_st { + public: + enum cpace_role_t our_role; + enum cpace_state_t { + cpace_state_init = 0, + cpace_state_msg_generated, + cpace_state_key_generated, + } state = cpace_state_init; + Array our_aad; + Array our_password; + Array channel_id; + Array session_id; + uint8_t our_public_key[32]; + uint8_t our_private_key[32]; + + static constexpr bool kAllowUniquePtr = true; + + void ComputeGeneratorStrHash(uint8_t out_gen_str_hash[32]) const; + bool ComputeMessage(uint8_t msg[32]); + bool ComputeISK(const uint8_t msg[32], Span peer_assoc_data, + uint8_t shared_secret[64], + uint8_t out_sid[SHA512_DIGEST_LENGTH]); +}; + extern const uint8_t k25519Precomp[32][8][3][32]; BSSL_NAMESPACE_END diff --git a/third_party/boringssl/src/crypto/curve25519/spake25519.cc b/third_party/boringssl/src/crypto/curve25519/spake25519.cc index 0f6cf04e3..fc565616c 100644 --- a/third_party/boringssl/src/crypto/curve25519/spake25519.cc +++ b/third_party/boringssl/src/crypto/curve25519/spake25519.cc @@ -112,7 +112,9 @@ if __name__ == "__main__": printtable(Mtable, "kSpakeMSmallPrecomp") */ -static const uint8_t kSpakeNSmallPrecomp[15 * 2 * 32] = { +namespace { + +const uint8_t kSpakeNSmallPrecomp[15 * 2 * 32] = { 0x20, 0x1b, 0xc5, 0xb3, 0x43, 0x17, 0x71, 0x10, 0x44, 0x1e, 0x73, 0xb3, 0xae, 0x3f, 0xbf, 0x9f, 0xf5, 0x44, 0xc8, 0x13, 0x8f, 0xd1, 0x01, 0xc2, 0x8a, 0x1a, 0x6d, 0xea, 0x4d, 0x00, 0x5d, 0x6e, 0x10, 0xe3, 0xdf, 0x0a, @@ -195,7 +197,7 @@ static const uint8_t kSpakeNSmallPrecomp[15 * 2 * 32] = { 0x72, 0x46, 0x04, 0x07, 0xf2, 0x5b, 0xf4, 0x07, 0xd1, 0xf4, 0x59, 0x71, }; -static const uint8_t kSpakeMSmallPrecomp[15 * 2 * 32] = { +const uint8_t kSpakeMSmallPrecomp[15 * 2 * 32] = { 0xc8, 0xa6, 0x63, 0xc5, 0x97, 0xf1, 0xee, 0x40, 0xab, 0x62, 0x42, 0xee, 0x25, 0x6f, 0x32, 0x6c, 0x75, 0x2c, 0xa7, 0xd3, 0xbd, 0x32, 0x3b, 0x1e, 0x11, 0x9c, 0xbd, 0x04, 0xa9, 0x78, 0x6f, 0x45, 0x5a, 0xda, 0x7e, 0x4b, @@ -278,6 +280,8 @@ static const uint8_t kSpakeMSmallPrecomp[15 * 2 * 32] = { 0xa6, 0x76, 0x81, 0x28, 0xb2, 0x65, 0xe8, 0x47, 0x14, 0xc6, 0x39, 0x06, }; +} // namespace + SPAKE2_CTX *SPAKE2_CTX_new(enum spake2_role_t my_role, const uint8_t *my_name, size_t my_name_len, const uint8_t *their_name, size_t their_name_len) { @@ -310,9 +314,11 @@ void SPAKE2_CTX_free(SPAKE2_CTX *ctx) { Delete(ctx); } -// left_shift_3 sets |n| to |n|*8, where |n| is represented in little-endian +namespace { + +// left_shift_3 sets `n` to `n`*8, where `n` is represented in little-endian // order. -static void left_shift_3(uint8_t n[32]) { +void left_shift_3(uint8_t n[32]) { uint8_t carry = 0; unsigned i; @@ -323,33 +329,33 @@ static void left_shift_3(uint8_t n[32]) { } } -namespace { typedef struct { BN_ULONG words[32 / sizeof(BN_ULONG)]; } scalar; -} // namespace // kOrder is the order of the prime-order subgroup of curve25519. -static const scalar kOrder = { +const scalar kOrder = { {TOBN(0x5812631a, 0x5cf5d3ed), TOBN(0x14def9de, 0xa2f79cd6), TOBN(0x00000000, 0x00000000), TOBN(0x10000000, 0x00000000)}}; -// scalar_cmov copies |src| to |dest| if |mask| is all ones. -static void scalar_cmov(scalar *dest, const scalar *src, crypto_word_t mask) { +// scalar_cmov copies `src` to `dest` if `mask` is all ones. +void scalar_cmov(scalar *dest, const scalar *src, crypto_word_t mask) { bn_select_words(dest->words, mask, src->words, dest->words, std::size(dest->words)); } -// scalar_double sets |s| to |2×s|. -static void scalar_double(scalar *s) { +// scalar_double sets `s` to `2×s`. +void scalar_double(scalar *s) { bn_add_words(s->words, s->words, s->words, std::size(s->words)); } -// scalar_add sets |dest| to |dest| plus |src|. -static void scalar_add(scalar *dest, const scalar *src) { +// scalar_add sets `dest` to `dest` plus `src`. +void scalar_add(scalar *dest, const scalar *src) { bn_add_words(dest->words, dest->words, src->words, std::size(dest->words)); } +} // namespace + int SPAKE2_generate_msg(SPAKE2_CTX *ctx, uint8_t *out, size_t *out_len, size_t max_out_len, const uint8_t *password, size_t password_len) { @@ -378,8 +384,8 @@ int SPAKE2_generate_msg(SPAKE2_CTX *ctx, uint8_t *out, size_t *out_len, OPENSSL_memcpy(ctx->password_hash, password_tmp, sizeof(ctx->password_hash)); x25519_sc_reduce(password_tmp); - // Due to a copy-paste error, the call to |left_shift_3| was omitted after - // the |x25519_sc_reduce|, just above. This meant that |ctx->password_scalar| + // Due to a copy-paste error, the call to `left_shift_3` was omitted after + // the `x25519_sc_reduce`, just above. This meant that `ctx->password_scalar` // was not a multiple of eight to clear the cofactor and thus three bits of // the password hash would leak. In order to fix this in a unilateral way, // points of small order are added to the mask point such that it is in the @@ -395,8 +401,8 @@ int SPAKE2_generate_msg(SPAKE2_CTX *ctx, uint8_t *out, size_t *out_len, scalar password_scalar; OPENSSL_memcpy(&password_scalar, password_tmp, sizeof(password_scalar)); - // |password_scalar| is the result of |x25519_sc_reduce| and thus is, at - // most, $l-1$ (where $l$ is |kOrder|, the order of the prime-order subgroup + // `password_scalar` is the result of `x25519_sc_reduce` and thus is, at + // most, $l-1$ (where $l$ is `kOrder`, the order of the prime-order subgroup // of Ed25519). In the following, we may add $l + 2×l + 4×l$ for a max value // of $8×l-1$. That is < 2**256, as required. @@ -451,8 +457,10 @@ int SPAKE2_generate_msg(SPAKE2_CTX *ctx, uint8_t *out, size_t *out_len, return 1; } -static void update_with_length_prefix(SHA512_CTX *sha, const uint8_t *data, - const size_t len) { +namespace { + +void update_with_length_prefix(SHA512_CTX *sha, const uint8_t *data, + const size_t len) { uint8_t len_le[8]; size_t l = len; unsigned i; @@ -466,6 +474,8 @@ static void update_with_length_prefix(SHA512_CTX *sha, const uint8_t *data, SHA512_Update(sha, data, len); } +} // namespace + int SPAKE2_process_msg(SPAKE2_CTX *ctx, uint8_t *out_key, size_t *out_key_len, size_t max_out_key_len, const uint8_t *their_msg, size_t their_msg_len) { diff --git a/third_party/boringssl/src/crypto/des/internal.h b/third_party/boringssl/src/crypto/des/internal.h index 6bb46ff5a..221f3091b 100644 --- a/third_party/boringssl/src/crypto/des/internal.h +++ b/third_party/boringssl/src/crypto/des/internal.h @@ -24,7 +24,7 @@ BSSL_NAMESPACE_BEGIN // TODO(davidben): Ideally these macros would be replaced with -// |CRYPTO_load_u32_le| and |CRYPTO_store_u32_le|. +// `CRYPTO_load_u32_le` and `CRYPTO_store_u32_le`. #define c2l(c, l) \ do { \ @@ -129,7 +129,7 @@ void DES_ede3_cbc_encrypt_ex(const uint8_t *in, uint8_t *out, size_t len, // Private functions. // -// These functions are only exported for use in |decrepit|. +// These functions are only exported for use in `decrepit`. OPENSSL_EXPORT void DES_decrypt3(uint32_t data[2], const DES_key_schedule *ks1, const DES_key_schedule *ks2, diff --git a/third_party/boringssl/src/crypto/dh/params.cc b/third_party/boringssl/src/crypto/dh/params.cc index 406a069d8..b981157bf 100644 --- a/third_party/boringssl/src/crypto/dh/params.cc +++ b/third_party/boringssl/src/crypto/dh/params.cc @@ -296,7 +296,7 @@ int DH_generate_parameters_ex(DH *dh, int prime_bits, int generator, // // I've implemented the second simple method :-). // Since DH should be using a safe prime (both p and q are prime), - // this generator function can take a very very long time to run. + // this generator function can take a very, very long time to run. // Actually there is no reason to insist that 'generator' be a generator. // It's just as OK (and in some sense better) to use a generator of the @@ -307,7 +307,7 @@ int DH_generate_parameters_ex(DH *dh, int prime_bits, int generator, return 0; } - // Make sure |dh| has the necessary elements + // Make sure `dh` has the necessary elements auto *impl = FromOpaque(dh); if (impl->p == nullptr) { impl->p.reset(BN_new()); diff --git a/third_party/boringssl/src/crypto/digest/digest_extra.cc b/third_party/boringssl/src/crypto/digest/digest_extra.cc index d38e0c113..4080076fe 100644 --- a/third_party/boringssl/src/crypto/digest/digest_extra.cc +++ b/third_party/boringssl/src/crypto/digest/digest_extra.cc @@ -26,11 +26,16 @@ #include #include +// IWYU pragma: begin_keep #include "../asn1/internal.h" #include "../fipsmodule/digest/internal.h" -#include "../internal.h" +// IWYU pragma: end_keep +using namespace bssl; + +namespace { + struct nid_to_digest { int nid; const EVP_MD *(*md_func)(); @@ -38,7 +43,7 @@ struct nid_to_digest { const char *long_name; }; -static const struct nid_to_digest nid_to_digest_mapping[] = { +const nid_to_digest nid_to_digest_mapping[] = { {NID_md4, EVP_md4, SN_md4, LN_md4}, {NID_md5, EVP_md5, SN_md5, LN_md5}, {NID_sha1, EVP_sha1, SN_sha1, LN_sha1}, @@ -48,10 +53,10 @@ static const struct nid_to_digest nid_to_digest_mapping[] = { {NID_sha512, EVP_sha512, SN_sha512, LN_sha512}, {NID_sha512_256, EVP_sha512_256, SN_sha512_256, LN_sha512_256}, {NID_md5_sha1, EVP_md5_sha1, SN_md5_sha1, LN_md5_sha1}, - // As a remnant of signing |EVP_MD|s, OpenSSL returned the corresponding + // As a remnant of signing `EVP_MD`s, OpenSSL returned the corresponding // hash function when given a signature OID. To avoid unintended lax parsing // of hash OIDs, this is no longer supported for lookup by OID or NID. - // Node.js, however, exposes |EVP_get_digestbyname|'s full behavior to + // Node.js, however, exposes `EVP_get_digestbyname`'s full behavior to // consumers so we retain it there. {NID_undef, EVP_sha1, SN_dsaWithSHA, LN_dsaWithSHA}, {NID_undef, EVP_sha1, SN_dsaWithSHA1, LN_dsaWithSHA1}, @@ -68,22 +73,7 @@ static const struct nid_to_digest nid_to_digest_mapping[] = { LN_sha512WithRSAEncryption}, }; -const EVP_MD *EVP_get_digestbynid(int nid) { - if (nid == NID_undef) { - // Skip the |NID_undef| entries in |nid_to_digest_mapping|. - return nullptr; - } - - for (const auto &mapping : nid_to_digest_mapping) { - if (mapping.nid == nid) { - return mapping.md_func(); - } - } - - return nullptr; -} - -static const struct { +const struct { uint8_t oid[9]; uint8_t oid_len; int nid; @@ -104,20 +94,36 @@ static const struct { {{0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x04}, 9, NID_sha224}, }; -static int cbs_to_digest_nid(const CBS *cbs) { +int cbs_to_digest_nid(const CBS *cbs) { for (const auto &md : kMDOIDs) { - if (bssl::Span(*cbs) == - bssl::Span(md.oid).first(md.oid_len)) { + if (Span(*cbs) == Span(md.oid).first(md.oid_len)) { return md.nid; } } return NID_undef; } +} // namespace + +const EVP_MD *EVP_get_digestbynid(int nid) { + if (nid == NID_undef) { + // Skip the `NID_undef` entries in `nid_to_digest_mapping`. + return nullptr; + } + + for (const auto &mapping : nid_to_digest_mapping) { + if (mapping.nid == nid) { + return mapping.md_func(); + } + } + + return nullptr; +} + const EVP_MD *EVP_get_digestbyobj(const ASN1_OBJECT *obj) { int nid = obj->nid; if (nid == NID_undef) { - // Handle objects with no saved NID. Note we don't use |OBJ_obj2nid| here to + // Handle objects with no saved NID. Note we don't use `OBJ_obj2nid` here to // avoid pulling in the OID table. CBS cbs; CBS_init(&cbs, OBJ_get0_data(obj), OBJ_length(obj)); @@ -166,8 +172,9 @@ const EVP_MD *EVP_parse_digest_algorithm(CBS *cbs) { return EVP_get_digestbynid(nid); } -static int marshal_digest_algorithm(CBB *cbb, const EVP_MD *md, - bool with_null) { +namespace { + +int marshal_digest_algorithm(CBB *cbb, const EVP_MD *md, bool with_null) { CBB algorithm, oid, null; if (!CBB_add_asn1(cbb, &algorithm, CBS_ASN1_SEQUENCE) || !CBB_add_asn1(&algorithm, &oid, CBS_ASN1_OBJECT)) { @@ -199,6 +206,8 @@ static int marshal_digest_algorithm(CBB *cbb, const EVP_MD *md, return 1; } +} // namespace + int EVP_marshal_digest_algorithm(CBB *cbb, const EVP_MD *md) { return marshal_digest_algorithm(cbb, md, /*with_null=*/true); } @@ -247,115 +256,132 @@ int EVP_Q_digest(OSSL_LIB_CTX *libctx, const char *name, const char *propq, return 1; } -static void blake2b256_init(EVP_MD_CTX *ctx) { +namespace { + +void blake2b256_init(EVP_MD_CTX *ctx) { BLAKE2B256_Init(reinterpret_cast(ctx->md_data)); } -static void blake2b256_update(EVP_MD_CTX *ctx, const void *data, size_t len) { +void blake2b256_update(EVP_MD_CTX *ctx, const void *data, size_t len) { BLAKE2B256_Update(reinterpret_cast(ctx->md_data), data, len); } -static void blake2b256_final(EVP_MD_CTX *ctx, uint8_t *md) { +void blake2b256_final(EVP_MD_CTX *ctx, uint8_t *md) { BLAKE2B256_Final(md, reinterpret_cast(ctx->md_data)); } -static const EVP_MD evp_md_blake2b256 = { - NID_undef, BLAKE2B256_DIGEST_LENGTH, 0, - blake2b256_init, blake2b256_update, blake2b256_final, - BLAKE2B_CBLOCK, sizeof(BLAKE2B_CTX), +const EVP_MD evp_md_blake2b256 = { + /*type=*/NID_undef, + /*md_size=*/BLAKE2B256_DIGEST_LENGTH, + /*flags=*/0, + /*init=*/blake2b256_init, + /*update=*/blake2b256_update, + /*final=*/blake2b256_final, + /*squeeze=*/nullptr, + /*block_size=*/BLAKE2B_CBLOCK, + /*ctx_size=*/sizeof(BLAKE2B_CTX), }; -const EVP_MD *EVP_blake2b256() { return &evp_md_blake2b256; } - -static_assert(sizeof(BLAKE2B_CTX) <= EVP_MAX_MD_DATA_SIZE); - - -static void md4_init(EVP_MD_CTX *ctx) { +void md4_init(EVP_MD_CTX *ctx) { BSSL_CHECK(MD4_Init(reinterpret_cast(ctx->md_data))); } -static void md4_update(EVP_MD_CTX *ctx, const void *data, size_t count) { +void md4_update(EVP_MD_CTX *ctx, const void *data, size_t count) { BSSL_CHECK( MD4_Update(reinterpret_cast(ctx->md_data), data, count)); } -static void md4_final(EVP_MD_CTX *ctx, uint8_t *out) { +void md4_final(EVP_MD_CTX *ctx, uint8_t *out) { BSSL_CHECK(MD4_Final(out, reinterpret_cast(ctx->md_data))); } -static const EVP_MD evp_md_md4 = { - NID_md4, // - MD4_DIGEST_LENGTH, // - 0, - md4_init, - md4_update, - md4_final, - 64, - sizeof(MD4_CTX), +const EVP_MD evp_md_md4 = { + /*type=*/NID_md4, + /*md_size=*/MD4_DIGEST_LENGTH, + /*flags=*/0, + /*init=*/md4_init, + /*update=*/md4_update, + /*final=*/md4_final, + /*squeeze=*/nullptr, + /*block_size=*/64, + /*ctx_size=*/sizeof(MD4_CTX), }; -const EVP_MD *EVP_md4() { return &evp_md_md4; } - -static_assert(sizeof(MD4_CTX) <= EVP_MAX_MD_DATA_SIZE); - - -static void md5_init(EVP_MD_CTX *ctx) { +void md5_init(EVP_MD_CTX *ctx) { BSSL_CHECK(MD5_Init(reinterpret_cast(ctx->md_data))); } -static void md5_update(EVP_MD_CTX *ctx, const void *data, size_t count) { +void md5_update(EVP_MD_CTX *ctx, const void *data, size_t count) { BSSL_CHECK( MD5_Update(reinterpret_cast(ctx->md_data), data, count)); } -static void md5_final(EVP_MD_CTX *ctx, uint8_t *out) { +void md5_final(EVP_MD_CTX *ctx, uint8_t *out) { BSSL_CHECK(MD5_Final(out, reinterpret_cast(ctx->md_data))); } -static const EVP_MD evp_md_md5 = { - NID_md5, MD5_DIGEST_LENGTH, 0, md5_init, - md5_update, md5_final, 64, sizeof(MD5_CTX), +const EVP_MD evp_md_md5 = { + /*type=*/NID_md5, + /*md_size=*/MD5_DIGEST_LENGTH, + /*flags=*/0, + /*init=*/md5_init, + /*update=*/md5_update, + /*final=*/md5_final, + /*squeeze=*/nullptr, + /*block_size=*/64, + /*ctx_size=*/sizeof(MD5_CTX), }; -const EVP_MD *EVP_md5() { return &evp_md_md5; } - -static_assert(sizeof(MD5_CTX) <= EVP_MAX_MD_DATA_SIZE); - - -typedef struct { +struct MD5_SHA1_CTX { MD5_CTX md5; SHA_CTX sha1; -} MD5_SHA1_CTX; +}; -static void md5_sha1_init(EVP_MD_CTX *md_ctx) { +void md5_sha1_init(EVP_MD_CTX *md_ctx) { MD5_SHA1_CTX *ctx = reinterpret_cast(md_ctx->md_data); BSSL_CHECK(MD5_Init(&ctx->md5) && SHA1_Init(&ctx->sha1)); } -static void md5_sha1_update(EVP_MD_CTX *md_ctx, const void *data, - size_t count) { +void md5_sha1_update(EVP_MD_CTX *md_ctx, const void *data, size_t count) { MD5_SHA1_CTX *ctx = reinterpret_cast(md_ctx->md_data); BSSL_CHECK(MD5_Update(&ctx->md5, data, count) && SHA1_Update(&ctx->sha1, data, count)); } -static void md5_sha1_final(EVP_MD_CTX *md_ctx, uint8_t *out) { +void md5_sha1_final(EVP_MD_CTX *md_ctx, uint8_t *out) { MD5_SHA1_CTX *ctx = reinterpret_cast(md_ctx->md_data); BSSL_CHECK(MD5_Final(out, &ctx->md5) && SHA1_Final(out + MD5_DIGEST_LENGTH, &ctx->sha1)); } const EVP_MD evp_md_md5_sha1 = { - NID_md5_sha1, - MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH, - 0, - md5_sha1_init, - md5_sha1_update, - md5_sha1_final, - 64, - sizeof(MD5_SHA1_CTX), + /*type=*/NID_md5_sha1, + /*md_size=*/MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH, + /*flags=*/0, + /*init=*/md5_sha1_init, + /*update=*/md5_sha1_update, + /*final=*/md5_sha1_final, + /*squeeze=*/nullptr, + /*block_size=*/64, + /*ctx_size=*/sizeof(MD5_SHA1_CTX), }; +} // namespace + +const EVP_MD *EVP_blake2b256() { return &evp_md_blake2b256; } + +static_assert(sizeof(BLAKE2B_CTX) <= EVP_MAX_MD_DATA_SIZE); + +const EVP_MD *EVP_md4() { return &evp_md_md4; } + +static_assert(sizeof(MD4_CTX) <= EVP_MAX_MD_DATA_SIZE); + + +const EVP_MD *EVP_md5() { return &evp_md_md5; } + +static_assert(sizeof(MD5_CTX) <= EVP_MAX_MD_DATA_SIZE); + + const EVP_MD *EVP_md5_sha1() { return &evp_md_md5_sha1; } static_assert(sizeof(MD5_SHA1_CTX) <= EVP_MAX_MD_DATA_SIZE); diff --git a/third_party/boringssl/src/crypto/dsa/dsa.cc b/third_party/boringssl/src/crypto/dsa/dsa.cc index e346758bb..9bb2df179 100644 --- a/third_party/boringssl/src/crypto/dsa/dsa.cc +++ b/third_party/boringssl/src/crypto/dsa/dsa.cc @@ -72,6 +72,13 @@ int DSA_up_ref(DSA *dsa) { return 1; } +DSA *DSA_dup_ref(const DSA *dsa) { + if (dsa == nullptr) { + return nullptr; + } + return FromOpaque(dsa)->DupRefInternal(); +} + unsigned DSA_bits(const DSA *dsa) { return BN_num_bits(FromOpaque(dsa)->p.get()); } @@ -476,15 +483,15 @@ int DSA_SIG_set0(DSA_SIG *sig, BIGNUM *r, BIGNUM *s) { return 1; } -// mod_mul_consttime sets |r| to |a| * |b| modulo |mont->N|, treating |a| and -// |b| as secret. This function internally uses Montgomery reduction, but +// mod_mul_consttime sets `r` to `a` * `b` modulo `mont->N`, treating `a` and +// `b` as secret. This function internally uses Montgomery reduction, but // neither inputs nor outputs are in Montgomery form. static int mod_mul_consttime(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BN_MONT_CTX *mont, BN_CTX *ctx) { BN_CTXScope scope(ctx); BIGNUM *tmp = BN_CTX_get(ctx); - // |BN_mod_mul_montgomery| removes a factor of R, so we cancel it with a - // single |BN_to_montgomery| which adds one factor of R. + // `BN_mod_mul_montgomery` removes a factor of R, so we cancel it with a + // single `BN_to_montgomery` which adds one factor of R. return tmp != nullptr && // BN_to_montgomery(tmp, a, mont, ctx) && BN_mod_mul_montgomery(r, tmp, b, mont, ctx); @@ -524,7 +531,7 @@ DSA_SIG *DSA_do_sign(const uint8_t *digest, size_t digest_len, const DSA *dsa) { // not impact valid parameters because the probability of requiring even one // retry is negligible, let alone 32. Unfortunately, DSA was mis-specified, // so invalid parameters are reachable from most callers handling untrusted - // private keys. (The |dsa_check_key| call above is not sufficient. Checking + // private keys. (The `dsa_check_key` call above is not sufficient. Checking // whether arbitrary parameters form a valid DSA group is expensive.) static const int kMaxIterations = 32; int iters = 0; @@ -534,9 +541,9 @@ DSA_SIG *DSA_do_sign(const uint8_t *digest, size_t digest_len, const DSA *dsa) { } if (digest_len > BN_num_bytes(impl->q.get())) { - // If the digest length is greater than the size of |impl->q| use the + // If the digest length is greater than the size of `impl->q` use the // BN_num_bits(impl->q) leftmost bits of the digest, see FIPS 186-3, 4.2. - // Note the above check that |impl->q| is a multiple of 8 bits. + // Note the above check that `impl->q` is a multiple of 8 bits. digest_len = BN_num_bytes(impl->q.get()); } @@ -544,8 +551,8 @@ DSA_SIG *DSA_do_sign(const uint8_t *digest, size_t digest_len, const DSA *dsa) { goto err; } - // |m| is bounded by 2^(num_bits(q)), which is slightly looser than q. This - // violates |bn_mod_add_consttime| and |mod_mul_consttime|'s preconditions. + // `m` is bounded by 2^(num_bits(q)), which is slightly looser than q. This + // violates `bn_mod_add_consttime` and `mod_mul_consttime`'s preconditions. // (The underlying algorithms could accept looser bounds, but we reduce for // simplicity.) size_t q_width = bn_minimal_width(impl->q.get()); @@ -555,8 +562,8 @@ DSA_SIG *DSA_do_sign(const uint8_t *digest, size_t digest_len, const DSA *dsa) { bn_reduce_once_in_place(m.d, 0 /* no carry word */, impl->q->d, xr.d /* scratch space */, q_width); - // Compute s = inv(k) (m + xr) mod q. Note |impl->method_mont_q| is - // initialized by |dsa_sign_setup|. + // Compute s = inv(k) (m + xr) mod q. Note `impl->method_mont_q` is + // initialized by `dsa_sign_setup`. if (!mod_mul_consttime(&xr, impl->priv_key.get(), r, impl->method_mont_q.get(), ctx) || !bn_mod_add_consttime(s, &xr, &m, impl->q.get(), ctx) || @@ -741,6 +748,7 @@ int DSA_verify(int type, const uint8_t *digest, size_t digest_len, int DSA_check_signature(int *out_valid, const uint8_t *digest, size_t digest_len, const uint8_t *sig, size_t sig_len, const DSA *dsa) { + *out_valid = 0; DSA_SIG *s = nullptr; int ret = 0; uint8_t *der = nullptr; @@ -772,7 +780,7 @@ int DSA_check_signature(int *out_valid, const uint8_t *digest, return ret; } -// der_len_len returns the number of bytes needed to represent a length of |len| +// der_len_len returns the number of bytes needed to represent a length of `len` // in DER. static size_t der_len_len(size_t len) { if (len < 0x80) { @@ -794,7 +802,7 @@ int DSA_size(const DSA *dsa) { } size_t order_len = BN_num_bytes(impl->q.get()); - // Compute the maximum length of an |order_len| byte integer. Defensively + // Compute the maximum length of an `order_len` byte integer. Defensively // assume that the leading 0x00 is included. size_t integer_len = 1 /* tag */ + der_len_len(order_len + 1) + 1 + order_len; if (integer_len < order_len) { @@ -833,18 +841,19 @@ static int dsa_sign_setup(const DSAImpl *dsa, BN_CTX *ctx, BIGNUM **out_kinv, OPENSSL_PUT_ERROR(DSA, ERR_R_BN_LIB); goto err; } - // Note |BN_mod| below is not constant-time and may leak information about - // |r|. |dsa->p| may be significantly larger than |dsa->q|, so this is not + // Note `BN_mod` below is not constant-time and may leak information about + // `r`. `dsa->p` may be significantly larger than `dsa->q`, so this is not // easily performed in constant-time with Montgomery reduction. // - // However, |r| at this point is g^k (mod p). It is almost the value of |r| + // However, `r` at this point is g^k (mod p). It is almost the value of `r` // revealed in the signature anyway (g^k (mod p) (mod q)), going from it to - // |k| would require computing a discrete log. + // `k` would require computing a discrete log. bn_declassify(r); if (!BN_mod(r, r, dsa->q.get(), ctx) || // Compute part of 's = inv(k) (m + xr) mod q' using Fermat's Little // Theorem. - !bn_mod_inverse_prime(kinv, &k, dsa->q.get(), ctx, dsa->method_mont_q.get())) { + !bn_mod_inverse_prime(kinv, &k, dsa->q.get(), ctx, + dsa->method_mont_q.get())) { OPENSSL_PUT_ERROR(DSA, ERR_R_BN_LIB); goto err; } diff --git a/third_party/boringssl/src/crypto/dsa/dsa_asn1.cc b/third_party/boringssl/src/crypto/dsa/dsa_asn1.cc index a13386817..d2ac83cf4 100644 --- a/third_party/boringssl/src/crypto/dsa/dsa_asn1.cc +++ b/third_party/boringssl/src/crypto/dsa/dsa_asn1.cc @@ -29,7 +29,7 @@ using namespace bssl; // This function is in dsa_asn1.c rather than dsa.c because it is reachable from -// |EVP_PKEY| parsers. This makes it easier for the static linker to drop most +// `EVP_PKEY` parsers. This makes it easier for the static linker to drop most // of the DSA implementation. int bssl::dsa_check_key(const DSAImpl *dsa) { if (!dsa->p || !dsa->q || !dsa->g) { @@ -38,17 +38,17 @@ int bssl::dsa_check_key(const DSAImpl *dsa) { } // Fully checking for invalid DSA groups is expensive, so security and - // correctness of the signature scheme depend on how |dsa| was computed. I.e. + // correctness of the signature scheme depend on how `dsa` was computed. I.e. // we leave "assurance of domain parameter validity" from FIPS 186-4 to the // caller. However, we check bounds on all values to avoid DoS vectors even // when domain parameters are invalid. In particular, signing will infinite - // loop if |g| is zero. + // loop if `g` is zero. if (BN_is_negative(dsa->p.get()) || BN_is_negative(dsa->q.get()) || BN_is_zero(dsa->p.get()) || BN_is_zero(dsa->q.get()) || !BN_is_odd(dsa->p.get()) || !BN_is_odd(dsa->q.get()) || - // |q| must be a prime divisor of |p - 1|, which implies |q < p|. + // `q` must be a prime divisor of `p - 1`, which implies `q < p`. BN_cmp(dsa->q.get(), dsa->p.get()) >= 0 || - // |g| is in the multiplicative group of |p|. + // `g` is in the multiplicative group of `p`. BN_is_negative(dsa->g.get()) || BN_is_zero(dsa->g.get()) || BN_cmp(dsa->g.get(), dsa->p.get()) >= 0) { OPENSSL_PUT_ERROR(DSA, DSA_R_INVALID_PARAMETERS); @@ -62,7 +62,7 @@ int bssl::dsa_check_key(const DSAImpl *dsa) { return 0; } - // Bound |dsa->p| to avoid a DoS vector. Note this limit is much larger than + // Bound `dsa->p` to avoid a DoS vector. Note this limit is much larger than // the one in FIPS 186-4, which only allows L = 1024, 2048, and 3072. if (BN_num_bits(dsa->p.get()) > OPENSSL_DSA_MAX_MODULUS_BITS) { OPENSSL_PUT_ERROR(DSA, DSA_R_MODULUS_TOO_LARGE); @@ -70,7 +70,7 @@ int bssl::dsa_check_key(const DSAImpl *dsa) { } if (dsa->pub_key != nullptr) { - // The public key is also in the multiplicative group of |p|. + // The public key is also in the multiplicative group of `p`. if (BN_is_negative(dsa->pub_key.get()) || BN_is_zero(dsa->pub_key.get()) || BN_cmp(dsa->pub_key.get(), dsa->p.get()) >= 0) { OPENSSL_PUT_ERROR(DSA, DSA_R_INVALID_PARAMETERS); @@ -80,7 +80,7 @@ int bssl::dsa_check_key(const DSAImpl *dsa) { if (dsa->priv_key != nullptr) { // The private key is a non-zero element of the scalar field, determined by - // |q|. + // `q`. if (BN_is_negative(dsa->priv_key.get()) || constant_time_declassify_int(BN_is_zero(dsa->priv_key.get())) || constant_time_declassify_int( diff --git a/third_party/boringssl/src/crypto/dsa/internal.h b/third_party/boringssl/src/crypto/dsa/internal.h index 70f77e76f..99387321d 100644 --- a/third_party/boringssl/src/crypto/dsa/internal.h +++ b/third_party/boringssl/src/crypto/dsa/internal.h @@ -47,7 +47,7 @@ class DSAImpl : public dsa_st, public RefCounted { ~DSAImpl(); }; -// dsa_check_key performs cheap self-checks on |dsa|, and ensures it is within +// dsa_check_key performs cheap self-checks on `dsa`, and ensures it is within // DoS bounds. It returns one on success and zero on error. int dsa_check_key(const DSAImpl *dsa); diff --git a/third_party/boringssl/src/crypto/ec/ec_asn1.cc b/third_party/boringssl/src/crypto/ec/ec_asn1.cc index a34b41074..8cf7166ae 100644 --- a/third_party/boringssl/src/crypto/ec/ec_asn1.cc +++ b/third_party/boringssl/src/crypto/ec/ec_asn1.cc @@ -83,7 +83,7 @@ EC_KEY *bssl::ec_key_parse_private_key( ec_key_parse_parameters(&child, allowed_groups); if (inner_group == nullptr) { // If the caller already supplied a group, any explicit group is required - // to match. On mismatch, |ec_key_parse_parameters| will fail to recognize + // to match. On mismatch, `ec_key_parse_parameters` will fail to recognize // any other groups, so remap the error. if (group != nullptr && ERR_equals(ERR_peek_last_error(), ERR_LIB_EC, EC_R_UNKNOWN_GROUP)) { @@ -92,7 +92,7 @@ EC_KEY *bssl::ec_key_parse_private_key( } return nullptr; } - // Overriding |allowed_groups| above ensures the only returned group will be + // Overriding `allowed_groups` above ensures the only returned group will be // the matching one. assert(group == nullptr || inner_group == group); group = inner_group; @@ -134,7 +134,7 @@ EC_KEY *bssl::ec_key_parse_private_key( // encoded as a BIT STRING with bits ordered as in the DER encoding. !CBS_get_u8(&public_key, &padding) || // padding != 0 || - // Explicitly check |public_key| is non-empty to save the conversion + // Explicitly check `public_key` is non-empty to save the conversion // form later. CBS_len(&public_key) == 0 || !EC_POINT_oct2point(group, ret->pub_key, CBS_data(&public_key), @@ -260,7 +260,7 @@ static int parse_explicit_prime_curve(CBS *in, !CBS_get_asn1(¶ms, &curve, CBS_ASN1_SEQUENCE) || !CBS_get_asn1(&curve, &out->a, CBS_ASN1_OCTETSTRING) || !CBS_get_asn1(&curve, &out->b, CBS_ASN1_OCTETSTRING) || - // |curve| has an optional BIT STRING seed which we ignore. + // `curve` has an optional BIT STRING seed which we ignore. !CBS_get_optional_asn1(&curve, nullptr, nullptr, CBS_ASN1_BITSTRING) || CBS_len(&curve) != 0 || !CBS_get_asn1(¶ms, &base, CBS_ASN1_OCTETSTRING) || @@ -300,12 +300,12 @@ static int parse_explicit_prime_curve(CBS *in, return 1; } -// integers_equal returns one if |bytes| is a big-endian encoding of |bn|, and +// integers_equal returns one if `bytes` is a big-endian encoding of `bn`, and // zero otherwise. static int integers_equal(const CBS *bytes, const BIGNUM *bn) { // Although, in SEC 1, Field-Element-to-Octet-String has a fixed width, - // OpenSSL mis-encodes the |a| and |b|, so we tolerate any number of leading - // zeros. (This matters for P-521 whose |b| has a leading 0.) + // OpenSSL mis-encodes the `a` and `b`, so we tolerate any number of leading + // zeros. (This matters for P-521 whose `b` has a leading 0.) CBS copy = *bytes; while (CBS_len(©) > 0 && CBS_data(©)[0] == 0) { CBS_skip(©, 1); @@ -343,7 +343,7 @@ const EC_GROUP *bssl::ec_key_parse_curve_name( } EC_GROUP *EC_KEY_parse_curve_name(CBS *cbs) { - // This function only ever returns a static |EC_GROUP|, but currently returns + // This function only ever returns a static `EC_GROUP`, but currently returns // a non-const pointer for historical reasons. return const_cast(ec_key_parse_curve_name(cbs, get_all_groups())); } @@ -413,7 +413,7 @@ const EC_GROUP *bssl::ec_key_parse_parameters( } EC_GROUP *EC_KEY_parse_parameters(CBS *cbs) { - // This function only ever returns a static |EC_GROUP|, but currently returns + // This function only ever returns a static `EC_GROUP`, but currently returns // a non-const pointer for historical reasons. return const_cast(ec_key_parse_parameters(cbs, get_all_groups())); } @@ -430,8 +430,8 @@ int EC_POINT_point2cbb(CBB *out, const EC_GROUP *group, const EC_POINT *point, } EC_KEY *d2i_ECPrivateKey(EC_KEY **out, const uint8_t **inp, long len) { - // This function treats its |out| parameter differently from other |d2i| - // functions. If supplied, take the group from |*out|. + // This function treats its `out` parameter differently from other `d2i` + // functions. If supplied, take the group from `*out`. const EC_GROUP *group = nullptr; if (out != nullptr && *out != nullptr) { group = EC_KEY_get0_group(*out); @@ -518,7 +518,7 @@ int i2o_ECPublicKey(const EC_KEY *key, uint8_t **outp) { return 0; } const ECKey *key_impl = FromOpaque(key); - // No initial capacity because |EC_POINT_point2cbb| will internally reserve + // No initial capacity because `EC_POINT_point2cbb` will internally reserve // the right size in one shot, so it's best to leave this at zero. int ret = I2DFromCBB( /*initial_capacity=*/0, outp, [&](CBB *cbb) -> bool { diff --git a/third_party/boringssl/src/crypto/ec/ec_derive.cc b/third_party/boringssl/src/crypto/ec/ec_derive.cc index 6f6f7865b..b500336f9 100644 --- a/third_party/boringssl/src/crypto/ec/ec_derive.cc +++ b/third_party/boringssl/src/crypto/ec/ec_derive.cc @@ -35,8 +35,8 @@ EC_KEY *EC_KEY_derive_from_secret(const EC_GROUP *group, const uint8_t *secret, return nullptr; } - // Assemble a label string to provide some key separation in case |secret| is - // misused, but ultimately it's on the caller to ensure |secret| is suitably + // Assemble a label string to provide some key separation in case `secret` is + // misused, but ultimately it's on the caller to ensure `secret` is suitably // separated. static const char kLabel[] = "derive EC key "; char info[sizeof(kLabel) + EC_KEY_DERIVE_MAX_NAME_LEN]; @@ -70,9 +70,9 @@ EC_KEY *EC_KEY_derive_from_secret(const EC_GROUP *group, const uint8_t *secret, bssl::UniquePtr priv(BN_bin2bn(derived, derived_len, nullptr)); bssl::UniquePtr pub(EC_POINT_new(group)); if (key == nullptr || ctx == nullptr || priv == nullptr || pub == nullptr || - // Reduce |priv| with Montgomery reduction. First, convert "from" - // Montgomery form to compute |priv| * R^-1 mod |order|. This requires - // |priv| be under order * R, which is true if the group order is large + // Reduce `priv` with Montgomery reduction. First, convert "from" + // Montgomery form to compute `priv` * R^-1 mod `order`. This requires + // `priv` be under order * R, which is true if the group order is large // enough. 2^(num_bytes(order)) < 2^8 * order, so: // // priv < 2^8 * order * 2^128 < order * order < order * R diff --git a/third_party/boringssl/src/crypto/ec/hash_to_curve.cc b/third_party/boringssl/src/crypto/ec/hash_to_curve.cc index 6ce88ee14..ec6aa06da 100644 --- a/third_party/boringssl/src/crypto/ec/hash_to_curve.cc +++ b/third_party/boringssl/src/crypto/ec/hash_to_curve.cc @@ -14,9 +14,13 @@ #include +#include #include #include +#include +#include #include +#include #include @@ -37,11 +41,11 @@ namespace { // becomes a performance bottleneck, some possible optimizations by // specializing it to the curve: // -// - Rather than using a generic |ec_felem_exp|, specialize the exponentiation +// - Rather than using a generic `ec_felem_exp`, specialize the exponentiation // to c2 with a faster addition chain. // -// - |ec_felem_mul| and |ec_felem_sqr| are generic Montgomery code. Given the -// few curves, we could specialize |map_to_curve_simple_swu|. But doing this +// - `ec_felem_mul` and `ec_felem_sqr` are generic Montgomery code. Given the +// few curves, we could specialize `map_to_curve_simple_swu`. But doing this // reasonably without duplicating code in C is difficult. (C++ templates // would be useful here.) // @@ -83,7 +87,7 @@ int expand_message_xmd(const EVP_MD *md, uint8_t *out, size_t out_len, // Compute b_0. static const uint8_t kZeros[EVP_MAX_MD_BLOCK_SIZE] = {0}; - // If |out_len| exceeds 16 bits then |i| will wrap below causing an error to + // If `out_len` exceeds 16 bits then `i` will wrap below causing an error to // be returned. This depends on the static assert above. uint8_t l_i_b_str_zero[3] = {static_cast(out_len >> 8), static_cast(out_len), 0}; @@ -134,15 +138,15 @@ int expand_message_xmd(const EVP_MD *md, uint8_t *out, size_t out_len, } // num_bytes_to_derive determines the number of bytes to derive when hashing to -// a number modulo |modulus|. See the hash_to_field operation defined in +// a number modulo `modulus`. See the hash_to_field operation defined in // section 5.2 of RFC 9380. int num_bytes_to_derive(size_t *out, const BIGNUM *modulus, unsigned k) { size_t bits = BN_num_bits(modulus); size_t L = (bits + k + 7) / 8; // We require 2^(8*L) < 2^(2*bits - 2) <= n^2 so to fit in bounds for - // |felem_reduce| and |ec_scalar_reduce|. All defined hash-to-curve suites - // define |k| to be well under this bound. (|k| is usually around half of - // |p_bits|.) + // `felem_reduce` and `ec_scalar_reduce`. All defined hash-to-curve suites + // define `k` to be well under this bound. (`k` is usually around half of + // `p_bits`.) if (L * 8 >= 2 * bits - 2 || L > 2 * EC_MAX_BYTES) { assert(0); OPENSSL_PUT_ERROR(EC, ERR_R_INTERNAL_ERROR); @@ -153,21 +157,28 @@ int num_bytes_to_derive(size_t *out, const BIGNUM *modulus, unsigned k) { return 1; } -// big_endian_to_words decodes |in| as a big-endian integer and writes the -// result to |out|. |num_words| must be large enough to contain the output. -void big_endian_to_words(BN_ULONG *out, size_t num_words, const uint8_t *in, - size_t len) { - assert(len <= num_words * sizeof(BN_ULONG)); +// big_endian_to_words decodes `in` as a big-endian integer and writes the +// result to `out`. `out` must be large enough to contain the output. +void big_endian_to_words(Span out, Span in) { + BSSL_CHECK(in.size() <= out.size() * sizeof(BN_ULONG)); // Ensure any excess bytes are zeroed. - OPENSSL_memset(out, 0, num_words * sizeof(BN_ULONG)); - uint8_t *out_u8 = (uint8_t *)out; - for (size_t i = 0; i < len; i++) { - out_u8[len - 1 - i] = in[i]; + OPENSSL_memset(out.data(), 0, out.size() * sizeof(BN_ULONG)); + uint8_t *out_u8 = reinterpret_cast(out.data()); + for (size_t i = 0; i < in.size(); i++) { + out_u8[in.size() - 1 - i] = in[i]; } } +void big_endian_reduce_to_felem(const EC_GROUP *group, EC_FELEM *out, + Span in) { + BN_ULONG words_buf[2 * EC_MAX_WORDS]; + auto words = Span(words_buf).first(2 * group->field.N.width); + big_endian_to_words(words, in); + ec_felem_reduce(group, out, words.data(), words.size()); +} + // hash_to_field implements the operation described in section 5.2 -// of RFC 9380, with count = 2. |k| is the security factor. +// of RFC 9380, with count = 2. `k` is the security factor. int hash_to_field2(const EC_GROUP *group, const EVP_MD *md, EC_FELEM *out1, EC_FELEM *out2, const uint8_t *dst, size_t dst_len, unsigned k, const uint8_t *msg, size_t msg_len) { @@ -177,17 +188,13 @@ int hash_to_field2(const EC_GROUP *group, const EVP_MD *md, EC_FELEM *out1, !expand_message_xmd(md, buf, 2 * L, msg, msg_len, dst, dst_len)) { return 0; } - BN_ULONG words[2 * EC_MAX_WORDS]; - size_t num_words = 2 * group->field.N.width; - big_endian_to_words(words, num_words, buf, L); - ec_felem_reduce(group, out1, words, num_words); - big_endian_to_words(words, num_words, buf + L, L); - ec_felem_reduce(group, out2, words, num_words); + big_endian_reduce_to_felem(group, out1, Span(buf).first(L)); + big_endian_reduce_to_felem(group, out2, Span(buf).subspan(L, L)); return 1; } // hash_to_field1 implements the operation described in section 5.2 -// of RFC 9380, with count = 1. |k| is the security factor. +// of RFC 9380, with count = 1. `k` is the security factor. int hash_to_field1(const EC_GROUP *group, const EVP_MD *md, EC_FELEM *out, const uint8_t *dst, size_t dst_len, unsigned k, const uint8_t *msg, size_t msg_len) { @@ -197,15 +204,12 @@ int hash_to_field1(const EC_GROUP *group, const EVP_MD *md, EC_FELEM *out, !expand_message_xmd(md, buf, L, msg, msg_len, dst, dst_len)) { return 0; } - BN_ULONG words[2 * EC_MAX_WORDS]; - size_t num_words = 2 * group->field.N.width; - big_endian_to_words(words, num_words, buf, L); - ec_felem_reduce(group, out, words, num_words); + big_endian_reduce_to_felem(group, out, Span(buf).first(L)); return 1; } -// hash_to_scalar behaves like |hash_to_field2| but returns a value modulo the -// group order rather than a field element. |k| is the security factor. +// hash_to_scalar behaves like `hash_to_field2` but returns a value modulo the +// group order rather than a field element. `k` is the security factor. int hash_to_scalar(const EC_GROUP *group, const EVP_MD *md, EC_SCALAR *out, const uint8_t *dst, size_t dst_len, unsigned k, const uint8_t *msg, size_t msg_len) { @@ -219,7 +223,7 @@ int hash_to_scalar(const EC_GROUP *group, const EVP_MD *md, EC_SCALAR *out, BN_ULONG words[2 * EC_MAX_WORDS]; size_t num_words = 2 * order->width; - big_endian_to_words(words, num_words, buf, L); + big_endian_to_words(Span(words).first(num_words), Span(buf).first(L)); ec_scalar_reduce(group, out, words, num_words); return 1; } @@ -240,17 +244,13 @@ BN_ULONG sgn0(const EC_GROUP *group, const EC_FELEM *a) { return buf[len - 1] & 1; } -[[maybe_unused]] static int is_3mod4(const EC_GROUP *group) { - return group->field.N.width > 0 && (group->field.N.d[0] & 3) == 3; -} - // sqrt_ratio_3mod4 implements the operation described in appendix F.2.1.2 // of RFC 9380. BN_ULONG sqrt_ratio_3mod4(const EC_GROUP *group, const EC_FELEM *Z, const BN_ULONG *c1, size_t num_c1, const EC_FELEM *c2, EC_FELEM *out_y, const EC_FELEM *u, const EC_FELEM *v) { - assert(is_3mod4(group)); + assert(group->field_is_3_mod_4); EC_FELEM tv1, tv2, tv3, y1, y2; ec_felem_sqr(group, &tv1, v); // 1. tv1 = v^2 @@ -266,7 +266,7 @@ BN_ULONG sqrt_ratio_3mod4(const EC_GROUP *group, const EC_FELEM *Z, // 10. y = CMOV(y2, y1, isQR) // 11. return (isQR, y) // - // Note the specification's CMOV function and our |ec_felem_select| have the + // Note the specification's CMOV function and our `ec_felem_select` have the // opposite argument order. ec_felem_sub(group, &tv1, &tv3, u); const BN_ULONG isQR = ~ec_felem_non_zero_mask(group, &tv1); @@ -281,7 +281,7 @@ void map_to_curve_simple_swu(const EC_GROUP *group, const EC_FELEM *Z, const EC_FELEM *c2, EC_JACOBIAN *out, const EC_FELEM *u) { // This function requires the prime be 3 mod 4, and that A = -3. - assert(is_3mod4(group)); + assert(group->field_is_3_mod_4); assert(group->a_is_minus3); EC_FELEM tv1, tv2, tv3, tv4, tv5, tv6, x, y, y1; @@ -332,7 +332,7 @@ void map_to_curve_simple_swu(const EC_GROUP *group, const EC_FELEM *Z, // 25. x = x / tv4 // - // Our output is in projective coordinates, so rather than inverting |tv4| + // Our output is in projective coordinates, so rather than inverting `tv4` // now, represent (x / tv4, y) as (x * tv4, y * tv4^3, tv4). This is much more // efficient if the caller will do further computation on the output. (If the // caller will immediately convert to affine coordinates, it is slightly less @@ -342,16 +342,9 @@ void map_to_curve_simple_swu(const EC_GROUP *group, const EC_FELEM *Z, out->Z = tv4; } -int hash_to_curve(const EC_GROUP *group, const EVP_MD *md, const EC_FELEM *Z, - const EC_FELEM *c2, unsigned k, EC_JACOBIAN *out, - const uint8_t *dst, size_t dst_len, const uint8_t *msg, - size_t msg_len) { - EC_FELEM u0, u1; - if (!hash_to_field2(group, md, &u0, &u1, dst, dst_len, k, msg, msg_len)) { - return 0; - } - - // Compute |c1| = (p - 3) / 4. +int sswu_and_add(const EC_GROUP *group, const EC_FELEM *Z, const EC_FELEM *c2, + EC_JACOBIAN *out, const EC_FELEM *u0, const EC_FELEM *u1) { + // Compute `c1` = (p - 3) / 4. BN_ULONG c1[EC_MAX_WORDS]; size_t num_c1 = group->field.N.width; if (!bn_copy_words(c1, num_c1, &group->field.N)) { @@ -360,14 +353,23 @@ int hash_to_curve(const EC_GROUP *group, const EVP_MD *md, const EC_FELEM *Z, bn_rshift_words(c1, c1, /*shift=*/2, /*num=*/num_c1); EC_JACOBIAN Q0, Q1; - map_to_curve_simple_swu(group, Z, c1, num_c1, c2, &Q0, &u0); - map_to_curve_simple_swu(group, Z, c1, num_c1, c2, &Q1, &u1); + map_to_curve_simple_swu(group, Z, c1, num_c1, c2, &Q0, u0); + map_to_curve_simple_swu(group, Z, c1, num_c1, c2, &Q1, u1); group->meth->add(group, out, &Q0, &Q1); // R = Q0 + Q1 - // All our curves have cofactor one, so |clear_cofactor| is a no-op. + // All our curves have cofactor one, so `clear_cofactor` is a no-op. return 1; } +int hash_to_curve(const EC_GROUP *group, const EVP_MD *md, const EC_FELEM *Z, + const EC_FELEM *c2, unsigned k, EC_JACOBIAN *out, + const uint8_t *dst, size_t dst_len, const uint8_t *msg, + size_t msg_len) { + EC_FELEM u0, u1; + return hash_to_field2(group, md, &u0, &u1, dst, dst_len, k, msg, msg_len) && + sswu_and_add(group, Z, c2, out, &u0, &u1); +} + int encode_to_curve(const EC_GROUP *group, const EVP_MD *md, const EC_FELEM *Z, const EC_FELEM *c2, unsigned k, EC_JACOBIAN *out, const uint8_t *dst, size_t dst_len, const uint8_t *msg, @@ -377,7 +379,7 @@ int encode_to_curve(const EC_GROUP *group, const EVP_MD *md, const EC_FELEM *Z, return 0; } - // Compute |c1| = (p - 3) / 4. + // Compute `c1` = (p - 3) / 4. BN_ULONG c1[EC_MAX_WORDS]; size_t num_c1 = group->field.N.width; if (!bn_copy_words(c1, num_c1, &group->field.N)) { @@ -386,7 +388,7 @@ int encode_to_curve(const EC_GROUP *group, const EVP_MD *md, const EC_FELEM *Z, bn_rshift_words(c1, c1, /*shift=*/2, /*num=*/num_c1); map_to_curve_simple_swu(group, Z, c1, num_c1, c2, out, &u); - // All our curves have cofactor one, so |clear_cofactor| is a no-op. + // All our curves have cofactor one, so `clear_cofactor` is a no-op. return 1; } @@ -554,6 +556,176 @@ int EC_encode_to_curve_p384_xmd_sha384_sswu(const EC_GROUP *group, msg, msg_len); } +int EC_wpa3_sae_hash_to_curve_p256(const EC_GROUP *group, EC_POINT *out, + const uint8_t *salt, size_t salt_len, + const uint8_t *ikm, size_t ikm_len) { + if (EC_GROUP_cmp(group, out->group, nullptr) != 0) { + OPENSSL_PUT_ERROR(EC, EC_R_INCOMPATIBLE_OBJECTS); + return 0; + } + if (EC_GROUP_get_curve_name(group) != NID_X9_62_prime256v1) { + OPENSSL_PUT_ERROR(EC, EC_R_GROUP_MISMATCH); + return 0; + } + + // WPA3 uses a different process for computing `u0` and `u1` (called `u1` and + // `u2` in 802.11) than RFC 9380. + uint8_t pwd_seed[EVP_MAX_MD_SIZE]; + size_t pwd_seed_len; + if (!HKDF_extract(pwd_seed, &pwd_seed_len, EVP_sha256(), ikm, ikm_len, salt, + salt_len)) { + return 0; + } + + uint8_t pwd_value[48]; // `len` for P-256 is 32 + 32 / 2 + auto label = StringAsBytes("SAE Hash to Element u1 P1"); + if (!HKDF_expand(pwd_value, sizeof(pwd_value), EVP_sha256(), pwd_seed, + pwd_seed_len, label.data(), label.size())) { + return 0; + } + EC_FELEM u1; + big_endian_reduce_to_felem(group, &u1, pwd_value); + + label = StringAsBytes("SAE Hash to Element u2 P2"); + if (!HKDF_expand(pwd_value, sizeof(pwd_value), EVP_sha256(), pwd_seed, + pwd_seed_len, label.data(), label.size())) { + return 0; + } + EC_FELEM u2; + big_endian_reduce_to_felem(group, &u2, pwd_value); + + // Z = -10, c2 = sqrt(10) + EC_FELEM Z, c2; + if (!felem_from_u8(group, &Z, 10) || + !ec_felem_from_bytes(group, &c2, kP256Sqrt10, sizeof(kP256Sqrt10))) { + return 0; + } + ec_felem_neg(group, &Z, &Z); + + return sswu_and_add(group, &Z, &c2, &out->raw, &u1, &u2); +} + +// See IEEE Std 802.11-2020, Section 12.7.1.6.2. +static int kdf_hash_length(const EVP_MD *md, Span out, + Span key, Span label, + Span context) { + ScopedHMAC_CTX hmac; + if (!HMAC_Init_ex(hmac.get(), key.data(), key.size(), md, nullptr)) { + return 0; + } + + assert(out.size() <= 0xffff); + uint8_t len_bytes[2]; + CRYPTO_store_u16_le(len_bytes, static_cast(out.size() * 8)); + const size_t md_len = HMAC_size(hmac.get()); + for (uint16_t i = 1; !out.empty(); i++) { + uint8_t i_bytes[2]; + CRYPTO_store_u16_le(i_bytes, i); + if (!HMAC_Init_ex(hmac.get(), nullptr, 0, nullptr, nullptr) || // Reset + !HMAC_Update(hmac.get(), i_bytes, sizeof(i_bytes)) || + !HMAC_Update(hmac.get(), label.data(), label.size()) || + !HMAC_Update(hmac.get(), context.data(), context.size()) || + !HMAC_Update(hmac.get(), len_bytes, sizeof(len_bytes))) { + return 0; + } + + if (out.size() < md_len) { + uint8_t tmp[EVP_MAX_MD_SIZE]; + if (!HMAC_Final(hmac.get(), tmp, nullptr)) { + return 0; + } + OPENSSL_memcpy(out.data(), tmp, out.size()); + break; + } + + if (!HMAC_Final(hmac.get(), out.data(), nullptr)) { + return 0; + } + out = out.subspan(md_len); + } + + return 1; +} + +int EC_wpa3_sae_hunt_and_peck_p256(const EC_GROUP *group, EC_POINT *out, + const uint8_t *salt, size_t salt_len, + const uint8_t *password, size_t password_len, + uint8_t min_iterations) { + if (EC_GROUP_cmp(group, out->group, nullptr) != 0) { + OPENSSL_PUT_ERROR(EC, EC_R_INCOMPATIBLE_OBJECTS); + return 0; + } + if (EC_GROUP_get_curve_name(group) != NID_X9_62_prime256v1) { + OPENSSL_PUT_ERROR(EC, EC_R_GROUP_MISMATCH); + return 0; + } + + static constexpr size_t kFieldBytes = 32; + static constexpr size_t kFieldWords = 32 / sizeof(BN_ULONG); + assert(static_cast(kFieldBytes) == BN_num_bytes(&group->field.N)); + assert(static_cast(kFieldWords) == group->field.N.width); + const EVP_MD *md = EVP_sha256(); + static const char kPWDValueLabel[] = "SAE Hunting and Pecking"; + + // The KDF uses the field as the context value. + uint8_t p_bytes[kFieldBytes]; + BSSL_CHECK(BN_bn2bin_padded(p_bytes, kFieldBytes, &group->field.N)); + + crypto_word_t ok = CONSTTIME_FALSE_W; + EC_AFFINE affine = {}; + for (uint8_t counter = 1; counter != 0; counter++) { + // Compute `pwd_seed`. This is an HKDF-Extract, or HMAC. + ScopedHMAC_CTX hmac; + uint8_t pwd_seed[EVP_MAX_MD_SIZE]; + unsigned pwd_seed_len; + if (!HMAC_Init_ex(hmac.get(), salt, salt_len, md, nullptr) || + !HMAC_Update(hmac.get(), password, password_len) || + !HMAC_Update(hmac.get(), &counter, 1) || + !HMAC_Final(hmac.get(), pwd_seed, &pwd_seed_len)) { + return 0; + } + + // Compute `pwd_value`. This uses something custom instead of HKDF-Expand. + uint8_t pwd_value[kFieldBytes]; + if (!kdf_hash_length(md, Span(pwd_value), Span(pwd_seed, pwd_seed_len), + StringAsBytes(kPWDValueLabel), p_bytes)) { + return 0; + } + + // Try to import `pwd_value` as a compressed point. + // The parity of y is determined by the least significant bit of `pwd_seed`. + uint8_t y_bit = pwd_seed[pwd_seed_len - 1] & 1; + EC_FELEM x, y2, y; + crypto_word_t iter_ok = + ec_felem_from_bytes_or_placeholder(group, &x, pwd_value, kFieldBytes); + ec_y_sqr_from_x(group, &y2, &x); + iter_ok &= ec_felem_sqrt_secret(group, &y, &y2, y_bit); + + // Save the first valid point. + crypto_word_t save = iter_ok & ~ok; + constant_time_conditional_memcpy(affine.X.words, x.words, + sizeof(BN_ULONG) * kFieldWords, save); + constant_time_conditional_memcpy(affine.Y.words, y.words, + sizeof(BN_ULONG) * kFieldWords, save); + ok |= save; + + // Stop if we've found something and iterated at least `min_iterations`. + if (constant_time_declassify_int( + ok & constant_time_ge_8(counter, min_iterations))) { + break; + } + } + + if (constant_time_declassify_w(ok) == 0) { + // This should be impossible. It can only fail with probabilty 2^-256. + OPENSSL_PUT_ERROR(EC, ERR_R_INTERNAL_ERROR); + return 0; + } + + ec_affine_to_jacobian(group, &out->raw, &affine); + return 1; +} + int bssl::ec_hash_to_scalar_p384_xmd_sha384(const EC_GROUP *group, EC_SCALAR *out, const uint8_t *dst, size_t dst_len, const uint8_t *msg, diff --git a/third_party/boringssl/src/crypto/ec/internal.h b/third_party/boringssl/src/crypto/ec/internal.h index 6e4648544..902eb81d2 100644 --- a/third_party/boringssl/src/crypto/ec/internal.h +++ b/third_party/boringssl/src/crypto/ec/internal.h @@ -26,21 +26,21 @@ BSSL_NAMESPACE_BEGIN // Parsing functions. -// ec_key_parse_curve_name behaves like |EC_KEY_parse_curve_name| but only -// supports the groups in |allowed_groups|. If no syntax errors were found but -// the group is unknown, it will fail with an error of |EC_R_UNKNOWN_GROUP|. +// ec_key_parse_curve_name behaves like `EC_KEY_parse_curve_name` but only +// supports the groups in `allowed_groups`. If no syntax errors were found but +// the group is unknown, it will fail with an error of `EC_R_UNKNOWN_GROUP`. const EC_GROUP *ec_key_parse_curve_name( CBS *cbs, bssl::Span allowed_groups); -// ec_key_parse_parameters behaves like |EC_KEY_parse_parameters| but only -// supports the groups in |allowed_groups|. If no syntax errors were found but -// the group is unknown, it will fail with an error of |EC_R_UNKNOWN_GROUP|. +// ec_key_parse_parameters behaves like `EC_KEY_parse_parameters` but only +// supports the groups in `allowed_groups`. If no syntax errors were found but +// the group is unknown, it will fail with an error of `EC_R_UNKNOWN_GROUP`. const EC_GROUP *ec_key_parse_parameters( CBS *cbs, bssl::Span allowed_groups); -// ec_key_parse_private_key behaves like |EC_KEY_parse_private_key| but only -// supports the groups in |allowed_groups|. If |group| is non-NULL, -// |allowed_groups| is ignored and instead only |group| is supported. +// ec_key_parse_private_key behaves like `EC_KEY_parse_private_key` but only +// supports the groups in `allowed_groups`. If `group` is non-NULL, +// `allowed_groups` is ignored and instead only `group` is supported. // // TODO(crbug.com/boringssl/414361735): This should return a bssl::UniquePtr, // but cannot until it is made C++ linkage. @@ -51,46 +51,46 @@ EC_KEY *ec_key_parse_private_key( // Hash-to-curve. // -// Internal |EC_JACOBIAN| versions of the corresponding public APIs. +// Internal `EC_JACOBIAN` versions of the corresponding public APIs. -// ec_hash_to_curve_p256_xmd_sha256_sswu hashes |msg| to a point on |group| and -// writes the result to |out|, implementing the P256_XMD:SHA-256_SSWU_RO_ suite +// ec_hash_to_curve_p256_xmd_sha256_sswu hashes `msg` to a point on `group` and +// writes the result to `out`, implementing the P256_XMD:SHA-256_SSWU_RO_ suite // from RFC 9380. It returns one on success and zero on error. OPENSSL_EXPORT int ec_hash_to_curve_p256_xmd_sha256_sswu( const EC_GROUP *group, EC_JACOBIAN *out, const uint8_t *dst, size_t dst_len, const uint8_t *msg, size_t msg_len); -// ec_hash_to_curve_p384_xmd_sha384_sswu hashes |msg| to a point on |group| and -// writes the result to |out|, implementing the P384_XMD:SHA-384_SSWU_RO_ suite +// ec_hash_to_curve_p384_xmd_sha384_sswu hashes `msg` to a point on `group` and +// writes the result to `out`, implementing the P384_XMD:SHA-384_SSWU_RO_ suite // from RFC 9380. It returns one on success and zero on error. OPENSSL_EXPORT int ec_hash_to_curve_p384_xmd_sha384_sswu( const EC_GROUP *group, EC_JACOBIAN *out, const uint8_t *dst, size_t dst_len, const uint8_t *msg, size_t msg_len); -// ec_encode_to_curve_p256_xmd_sha256_sswu hashes |msg| to a point on |group| -// and writes the result to |out|, implementing the P256_XMD:SHA-256_SSWU_NU_ +// ec_encode_to_curve_p256_xmd_sha256_sswu hashes `msg` to a point on `group` +// and writes the result to `out`, implementing the P256_XMD:SHA-256_SSWU_NU_ // suite from RFC 9380. It returns one on success and zero on error. OPENSSL_EXPORT int ec_encode_to_curve_p256_xmd_sha256_sswu( const EC_GROUP *group, EC_JACOBIAN *out, const uint8_t *dst, size_t dst_len, const uint8_t *msg, size_t msg_len); -// ec_encode_to_curve_p384_xmd_sha384_sswu hashes |msg| to a point on |group| -// and writes the result to |out|, implementing the P384_XMD:SHA-384_SSWU_NU_ +// ec_encode_to_curve_p384_xmd_sha384_sswu hashes `msg` to a point on `group` +// and writes the result to `out`, implementing the P384_XMD:SHA-384_SSWU_NU_ // suite from RFC 9380. It returns one on success and zero on error. OPENSSL_EXPORT int ec_encode_to_curve_p384_xmd_sha384_sswu( const EC_GROUP *group, EC_JACOBIAN *out, const uint8_t *dst, size_t dst_len, const uint8_t *msg, size_t msg_len); -// ec_hash_to_scalar_p384_xmd_sha384 hashes |msg| to a scalar on |group| -// and writes the result to |out|, using the hash_to_field operation from the +// ec_hash_to_scalar_p384_xmd_sha384 hashes `msg` to a scalar on `group` +// and writes the result to `out`, using the hash_to_field operation from the // P384_XMD:SHA-384_SSWU_RO_ suite from RFC 9380, but generating a value modulo // the group order rather than a field element. OPENSSL_EXPORT int ec_hash_to_scalar_p384_xmd_sha384( const EC_GROUP *group, EC_SCALAR *out, const uint8_t *dst, size_t dst_len, const uint8_t *msg, size_t msg_len); -// ec_hash_to_curve_p384_xmd_sha512_sswu_draft07 hashes |msg| to a point on -// |group| and writes the result to |out|, implementing the +// ec_hash_to_curve_p384_xmd_sha512_sswu_draft07 hashes `msg` to a point on +// `group` and writes the result to `out`, implementing the // P384_XMD:SHA-512_SSWU_RO_ suite from draft-irtf-cfrg-hash-to-curve-07. It // returns one on success and zero on error. // @@ -99,8 +99,8 @@ OPENSSL_EXPORT int ec_hash_to_curve_p384_xmd_sha512_sswu_draft07( const EC_GROUP *group, EC_JACOBIAN *out, const uint8_t *dst, size_t dst_len, const uint8_t *msg, size_t msg_len); -// ec_hash_to_scalar_p384_xmd_sha512_draft07 hashes |msg| to a scalar on |group| -// and writes the result to |out|, using the hash_to_field operation from the +// ec_hash_to_scalar_p384_xmd_sha512_draft07 hashes `msg` to a scalar on `group` +// and writes the result to `out`, using the hash_to_field operation from the // P384_XMD:SHA-512_SSWU_RO_ suite from draft-irtf-cfrg-hash-to-curve-07, but // generating a value modulo the group order rather than a field element. // diff --git a/third_party/boringssl/src/crypto/ecdsa/ecdsa_asn1.cc b/third_party/boringssl/src/crypto/ecdsa/ecdsa_asn1.cc index d4d0b4b6f..ad29cbbdf 100644 --- a/third_party/boringssl/src/crypto/ecdsa/ecdsa_asn1.cc +++ b/third_party/boringssl/src/crypto/ecdsa/ecdsa_asn1.cc @@ -292,7 +292,7 @@ int ECDSA_SIG_to_bytes(uint8_t **out_bytes, size_t *out_len, return 1; } -// der_len_len returns the number of bytes needed to represent a length of |len| +// der_len_len returns the number of bytes needed to represent a length of `len` // in DER. static size_t der_len_len(size_t len) { if (len < 0x80) { @@ -307,7 +307,7 @@ static size_t der_len_len(size_t len) { } size_t ECDSA_SIG_max_len(size_t order_len) { - // Compute the maximum length of an |order_len| byte integer. Defensively + // Compute the maximum length of an `order_len` byte integer. Defensively // assume that the leading 0x00 is included. size_t integer_len = 1 /* tag */ + der_len_len(order_len + 1) + 1 + order_len; if (integer_len < order_len) { diff --git a/third_party/boringssl/src/crypto/engine/engine.cc b/third_party/boringssl/src/crypto/engine/engine.cc index 4d39cd83f..c39d34039 100644 --- a/third_party/boringssl/src/crypto/engine/engine.cc +++ b/third_party/boringssl/src/crypto/engine/engine.cc @@ -42,7 +42,7 @@ int ENGINE_free(ENGINE *engine) { } // set_method takes a pointer to a method and its given size and sets -// |*out_member| to point to it. This function might want to be extended in the +// `*out_member` to point to it. This function might want to be extended in the // future to support making a copy of the method so that a stable ABI for // ENGINEs can be supported. But, for the moment, all *_METHODS must be // static. diff --git a/third_party/boringssl/src/crypto/err/err.cc b/third_party/boringssl/src/crypto/err/err.cc index b3fe31649..fc30f0193 100644 --- a/third_party/boringssl/src/crypto/err/err.cc +++ b/third_party/boringssl/src/crypto/err/err.cc @@ -40,13 +40,13 @@ struct err_error_st { // file contains the filename where the error occurred. const char *file; // data contains a NUL-terminated string with optional data. It is allocated - // with system |malloc| and must be freed with |free| (not |OPENSSL_free|) + // with system `malloc` and must be freed with `free` (not `OPENSSL_free`) char *data; // packed contains the error library and reason, as packed by ERR_PACK. uint32_t packed; // line contains the line number where the error occurred. uint16_t line; - // mark indicates a reversion point in the queue. See |ERR_pop_to_mark|. + // mark indicates a reversion point in the queue. See `ERR_pop_to_mark`. unsigned mark : 1; }; @@ -55,14 +55,14 @@ typedef struct err_state_st { // errors contains up to ERR_NUM_ERRORS - 1 most recent errors, organised as a // ring buffer. struct err_error_st errors[ERR_NUM_ERRORS]; - // top contains the index of the most recent error. If |top| equals |bottom| + // top contains the index of the most recent error. If `top` equals `bottom` // then the queue is empty. unsigned top; // bottom contains the index before the least recent error in the queue. unsigned bottom; // to_free, if not NULL, contains a pointer owned by this structure that was - // previously a |data| pointer of one of the elements of |errors|. + // previously a `data` pointer of one of the elements of `errors`. void *to_free; } ERR_STATE; } // namespace @@ -76,7 +76,7 @@ extern const char kOpenSSLReasonStringData[]; BSSL_NAMESPACE_END static char *strdup_libc_malloc(const char *str) { - // |strdup| is not in C until C23, so MSVC triggers deprecation warnings, and + // `strdup` is not in C until C23, so MSVC triggers deprecation warnings, and // glibc and musl gate it on a feature macro. Reimplementing it is easier. size_t len = strlen(str); char *ret = reinterpret_cast(malloc(len + 1)); @@ -108,7 +108,7 @@ static void err_copy(struct err_error_st *dst, const struct err_error_st *src) { // global_next_library contains the next custom library value to return. static int global_next_library = ERR_NUM_LIBS; -// global_next_library_mutex protects |global_next_library| from concurrent +// global_next_library_mutex protects `global_next_library` from concurrent // updates. static StaticMutex global_next_library_mutex; @@ -187,7 +187,7 @@ static uint32_t get_error_values(int inc, int top, const char **file, int *line, } else { *data = error->data; if (flags != nullptr) { - // Without |ERR_FLAG_MALLOCED|, rust-openssl assumes the string has a + // Without `ERR_FLAG_MALLOCED`, rust-openssl assumes the string has a // static lifetime. In both cases, we retain ownership of the string, // and the caller is not expected to free it. *flags = ERR_FLAG_STRING | ERR_FLAG_MALLOCED; @@ -298,7 +298,7 @@ void ERR_remove_state(unsigned long pid) { ERR_clear_error(); } void ERR_clear_system_error() { errno = 0; } // err_string_cmp is a compare function for searching error values with -// |bsearch| in |err_string_lookup|. +// `bsearch` in `err_string_lookup`. static int err_string_cmp(const void *a, const void *b) { const uint32_t a_key = *((const uint32_t *)a) >> 15; const uint32_t b_key = *((const uint32_t *)b) >> 15; @@ -312,23 +312,23 @@ static int err_string_cmp(const void *a, const void *b) { } } -// err_string_lookup looks up the string associated with |lib| and |key| in -// |values| and |string_data|. It returns the string or NULL if not found. +// err_string_lookup looks up the string associated with `lib` and `key` in +// `values` and `string_data`. It returns the string or NULL if not found. static const char *err_string_lookup(uint32_t lib, uint32_t key, const uint32_t *values, size_t num_values, const char *string_data) { - // |values| points to data in err_data.h, which is generated by + // `values` points to data in err_data.h, which is generated by // err_data_generate.go. It's an array of uint32_t values. Each value has the // following structure: // | lib | key | offset | // |6 bits| 11 bits | 15 bits | // - // The |lib| value is a library identifier: one of the |ERR_LIB_*| values. - // The |key| is a reason code, depending on the context. - // The |offset| is the number of bytes from the start of |string_data| where + // The `lib` value is a library identifier: one of the `ERR_LIB_*` values. + // The `key` is a reason code, depending on the context. + // The `offset` is the number of bytes from the start of `string_data` where // the (NUL terminated) string for this value can be found. // - // Values are sorted based on treating the |lib| and |key| part as an + // Values are sorted based on treating the `lib` and `key` part as an // unsigned integer. if (lib >= (1 << 6) || key >= (1 << 11)) { return nullptr; @@ -385,6 +385,7 @@ static const LIBRARY_NAME kLibraryNames[ERR_NUM_LIBS] = { {"Cipher functions", "CIPHER", "CIPHER_LIB"}, {"HKDF functions", "HKDF", "HKDF_LIB"}, {"Trust Token functions", "TRUST_TOKEN", "TRUST_TOKEN_LIB"}, + {"CMS routines", "CMS", "CMS_LIB"}, {"User defined functions", "USER", "USER_LIB"}, }; @@ -445,7 +446,7 @@ static const char *err_reason_error_string(uint32_t packed_error, int symbol) { } // Unlike OpenSSL, BoringSSL's reason strings already match symbol name, so we - // do not need to check |symbol|. + // do not need to check `symbol`. return err_string_lookup(lib, reason, kOpenSSLReasonValues, kOpenSSLReasonValuesLen, kOpenSSLReasonStringData); } @@ -469,7 +470,7 @@ char *ERR_error_string(uint32_t packed_error, char *ret) { #if !defined(NDEBUG) // This is aimed to help catch callers who don't provide - // |ERR_ERROR_STRING_BUF_LEN| bytes of space. + // `ERR_ERROR_STRING_BUF_LEN` bytes of space. OPENSSL_memset(ret, 0, ERR_ERROR_STRING_BUF_LEN); #endif @@ -518,7 +519,7 @@ char *ERR_error_string_n(uint32_t packed_error, char *buf, size_t len) { char *last_pos = &buf[len - 1] - num_colons + i; if (colon == nullptr || colon > last_pos) { - // set colon |i| at last possible position (buf[len-1] is the + // set colon `i` at last possible position (`buf[len-1]` is the // terminating 0). If we're setting this colon, then all whole of the // rest of the string must be colons in order to have the correct // number. @@ -540,7 +541,7 @@ void ERR_print_errors_cb(ERR_print_errors_callback_t callback, void *ctx) { int line, flags; uint32_t packed_error; - // thread_hash is the least-significant bits of the |ERR_STATE| pointer value + // thread_hash is the least-significant bits of the `ERR_STATE` pointer value // for this thread. const unsigned long thread_hash = (uintptr_t)err_get_state(); @@ -669,10 +670,11 @@ void ERR_add_error_dataf(const char *format, ...) { va_list ap; va_start(ap, format); - if (OPENSSL_vasprintf_internal(&buf, format, ap, /*system_malloc=*/1) == -1) { + int len = OPENSSL_vasprintf_internal(&buf, format, ap, /*system_malloc=*/1); + va_end(ap); + if (len == -1) { return; } - va_end(ap); err_set_error_data(buf); } @@ -690,8 +692,8 @@ void ERR_set_error_data(char *data, int flags) { err_set_error_data(copy); } if (flags & ERR_FLAG_MALLOCED) { - // We can not take ownership of |data| directly because it is allocated with - // |OPENSSL_malloc| and we will free it with system |free| later. + // We can not take ownership of `data` directly because it is allocated with + // `OPENSSL_malloc` and we will free it with system `free` later. OPENSSL_free(data); } } diff --git a/third_party/boringssl/src/crypto/err/internal.h b/third_party/boringssl/src/crypto/err/internal.h index e81768c75..d0f722d94 100644 --- a/third_party/boringssl/src/crypto/err/internal.h +++ b/third_party/boringssl/src/crypto/err/internal.h @@ -23,19 +23,19 @@ BSSL_NAMESPACE_BEGIN // Private error queue functions. // ERR_SAVE_STATE contains a saved representation of the error queue. It is -// slightly more compact than |ERR_STATE| as the error queue will typically not -// contain |ERR_NUM_ERRORS| entries. +// slightly more compact than `ERR_STATE` as the error queue will typically not +// contain `ERR_NUM_ERRORS` entries. typedef struct err_save_state_st ERR_SAVE_STATE; -// ERR_SAVE_STATE_free releases all memory associated with |state|. +// ERR_SAVE_STATE_free releases all memory associated with `state`. OPENSSL_EXPORT void ERR_SAVE_STATE_free(ERR_SAVE_STATE *state); -// ERR_save_state returns a newly-allocated |ERR_SAVE_STATE| structure +// ERR_save_state returns a newly-allocated `ERR_SAVE_STATE` structure // containing the current state of the error queue or NULL on allocation -// error. It should be released with |ERR_SAVE_STATE_free|. +// error. It should be released with `ERR_SAVE_STATE_free`. OPENSSL_EXPORT ERR_SAVE_STATE *ERR_save_state(); -// ERR_restore_state clears the error queue and replaces it with |state|. +// ERR_restore_state clears the error queue and replaces it with `state`. OPENSSL_EXPORT void ERR_restore_state(const ERR_SAVE_STATE *state); BORINGSSL_MAKE_DELETER(ERR_SAVE_STATE, ERR_SAVE_STATE_free) diff --git a/third_party/boringssl/src/crypto/evp/evp.cc b/third_party/boringssl/src/crypto/evp/evp.cc index 1b182592d..f52a96758 100644 --- a/third_party/boringssl/src/crypto/evp/evp.cc +++ b/third_party/boringssl/src/crypto/evp/evp.cc @@ -29,7 +29,7 @@ using namespace bssl; -// Node depends on |EVP_R_NOT_XOF_OR_INVALID_LENGTH|. +// Node depends on `EVP_R_NOT_XOF_OR_INVALID_LENGTH`. // // TODO(davidben): Fix Node to not touch the error queue itself and remove this. OPENSSL_DECLARE_ERROR_REASON(EVP, NOT_XOF_OR_INVALID_LENGTH) @@ -60,10 +60,10 @@ int EVP_PKEY_up_ref(EVP_PKEY *pkey) { } EVP_PKEY *EVP_PKEY_dup_ref(const EVP_PKEY *pkey) { - auto pkey_ref = const_cast(pkey); - // We know that this call always returns one. - EVP_PKEY_up_ref(pkey_ref); - return pkey_ref; + if (pkey == nullptr) { + return nullptr; + } + return FromOpaque(pkey)->DupRefInternal(); } int EVP_PKEY_is_opaque(const EVP_PKEY *pkey) { @@ -75,7 +75,7 @@ int EVP_PKEY_is_opaque(const EVP_PKEY *pkey) { } int EVP_PKEY_eq(const EVP_PKEY *a, const EVP_PKEY *b) { - // This also checks that |EVP_PKEY_id| matches. + // This also checks that `EVP_PKEY_id` matches. if (!EVP_PKEY_parameters_eq(a, b)) { return 0; } @@ -96,7 +96,7 @@ int EVP_PKEY_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from) { auto *from_impl = FromOpaque(from); if (EVP_PKEY_id(to_impl) == EVP_PKEY_NONE) { - // TODO(crbug.com/42290409): This shouldn't leave |to| in a half-empty state + // TODO(crbug.com/42290409): This shouldn't leave `to` in a half-empty state // on error. The complexity here largely comes from parameterless DSA keys, // which we no longer support, so this function can probably be trimmed // down. @@ -205,15 +205,15 @@ int EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key) { int EVP_PKEY_set_type(EVP_PKEY *pkey, int type) { auto *impl = FromOpaque(pkey); if (impl && impl->pkey) { - // Some callers rely on |pkey| getting cleared even if |type| is - // unsupported, usually setting |type| to |EVP_PKEY_NONE|. + // Some callers rely on `pkey` getting cleared even if `type` is + // unsupported, usually setting `type` to `EVP_PKEY_NONE`. evp_pkey_set0(impl, nullptr, nullptr); } - // This function broadly isn't useful. It initializes |EVP_PKEY| for a type, - // but forgets to put anything in the |pkey|. The one pattern where it does - // anything is |EVP_PKEY_X25519|, where it's needed to make - // |EVP_PKEY_set1_tls_encodedpoint| work, so we support only that. + // This function broadly isn't useful. It initializes `EVP_PKEY` for a type, + // but forgets to put anything in the `pkey`. The one pattern where it does + // anything is `EVP_PKEY_X25519`, where it's needed to make + // `EVP_PKEY_set1_tls_encodedpoint` work, so we support only that. const EVP_PKEY_ALG *alg; if (type == EVP_PKEY_X25519) { alg = EVP_pkey_x25519(); @@ -367,8 +367,8 @@ int EVP_PKEY_CTX_set1_signature_context_string(EVP_PKEY_CTX *ctx, void *EVP_PKEY_get0(const EVP_PKEY *pkey) { // Node references, but never calls this function, so for now we return NULL. - // If other projects require complete support, call |EVP_PKEY_get0_RSA|, etc., - // rather than reading |pkey->pkey| directly. This avoids problems if our + // If other projects require complete support, call `EVP_PKEY_get0_RSA`, etc., + // rather than reading `pkey->pkey` directly. This avoids problems if our // internal representation does not match the type the caller expects from // OpenSSL. return nullptr; diff --git a/third_party/boringssl/src/crypto/evp/evp_asn1.cc b/third_party/boringssl/src/crypto/evp/evp_asn1.cc index e32a6f71b..31a877500 100644 --- a/third_party/boringssl/src/crypto/evp/evp_asn1.cc +++ b/third_party/boringssl/src/crypto/evp/evp_asn1.cc @@ -238,7 +238,7 @@ EVP_PKEY *d2i_PrivateKey(int type, EVP_PKEY **out, const uint8_t **inp, }); } -// num_elements parses one SEQUENCE from |in| and returns the number of elements +// num_elements parses one SEQUENCE from `in` and returns the number of elements // in it. On parse error, it returns zero. static size_t num_elements(const uint8_t *in, size_t in_len) { CBS cbs, sequence; @@ -326,9 +326,9 @@ EVP_PKEY *d2i_PublicKey(int type, EVP_PKEY **out, const uint8_t **inp, // Unlike OpenSSL, we do not support EC keys with this API. The raw EC // public key serialization requires knowing the group. In OpenSSL, - // calling this function with |EVP_PKEY_EC| and setting |out| to - // nullptr does not work. It requires |*out| to include a - // partially-initialized |EVP_PKEY| to extract the group. + // calling this function with `EVP_PKEY_EC` and setting `out` to + // nullptr does not work. It requires `*out` to include a + // partially-initialized `EVP_PKEY` to extract the group. default: OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_PUBLIC_KEY_TYPE); return nullptr; @@ -380,8 +380,7 @@ int i2d_RSA_PUBKEY(const RSA *rsa, uint8_t **outp) { } UniquePtr pkey(EVP_PKEY_new()); - if (pkey == nullptr || - !EVP_PKEY_set1_RSA(pkey.get(), const_cast(rsa))) { + if (pkey == nullptr || !EVP_PKEY_set1_RSA(pkey.get(), rsa)) { return -1; } @@ -404,8 +403,7 @@ int i2d_DSA_PUBKEY(const DSA *dsa, uint8_t **outp) { } UniquePtr pkey(EVP_PKEY_new()); - if (pkey == nullptr || - !EVP_PKEY_set1_DSA(pkey.get(), const_cast(dsa))) { + if (pkey == nullptr || !EVP_PKEY_set1_DSA(pkey.get(), dsa)) { return -1; } @@ -430,8 +428,7 @@ int i2d_EC_PUBKEY(const EC_KEY *ec_key, uint8_t **outp) { } UniquePtr pkey(EVP_PKEY_new()); - if (pkey == nullptr || - !EVP_PKEY_set1_EC_KEY(pkey.get(), const_cast(ec_key))) { + if (pkey == nullptr || !EVP_PKEY_set1_EC_KEY(pkey.get(), ec_key)) { return -1; } diff --git a/third_party/boringssl/src/crypto/evp/evp_ctx.cc b/third_party/boringssl/src/crypto/evp/evp_ctx.cc index ffb9b6f2e..9fb491292 100644 --- a/third_party/boringssl/src/crypto/evp/evp_ctx.cc +++ b/third_party/boringssl/src/crypto/evp/evp_ctx.cc @@ -30,7 +30,8 @@ using namespace bssl; static UniquePtr evp_pkey_ctx_new( - EvpPkey *pkey, const EVP_PKEY_ALG *alg, const EVP_PKEY_CTX_METHOD *pmeth) { + const EvpPkey *pkey, const EVP_PKEY_ALG *alg, + const EVP_PKEY_CTX_METHOD *pmeth) { assert(pkey != nullptr || alg != nullptr); UniquePtr ret = MakeUnique(); if (!ret) { @@ -42,14 +43,14 @@ static UniquePtr evp_pkey_ctx_new( ret->pkey = UpRef(pkey); if (pmeth->init && pmeth->init(ret.get(), alg) <= 0) { - ret->pmeth = nullptr; // Don't call |pmeth->cleanup|. + ret->pmeth = nullptr; // Don't call `pmeth->cleanup`. return nullptr; } return ret; } -EVP_PKEY_CTX *EVP_PKEY_CTX_new(EVP_PKEY *pkey, ENGINE *e) { +EVP_PKEY_CTX *EVP_PKEY_CTX_new(const EVP_PKEY *pkey, ENGINE *e) { auto *pkey_impl = FromOpaque(pkey); if (pkey_impl == nullptr || pkey_impl->ameth == nullptr) { OPENSSL_PUT_ERROR(EVP, ERR_R_PASSED_NULL_PARAMETER); @@ -71,9 +72,9 @@ EVP_PKEY_CTX *EVP_PKEY_CTX_new(EVP_PKEY *pkey, ENGINE *e) { } EVP_PKEY_CTX *EVP_PKEY_CTX_new_id(int id, ENGINE *e) { - // |EVP_PKEY_RSA_PSS| is intentionally omitted from this list. These are types - // that can be created without an |EVP_PKEY|, and we do not support - // |EVP_PKEY_RSA_PSS| keygen. + // `EVP_PKEY_RSA_PSS` is intentionally omitted from this list. These are types + // that can be created without an `EVP_PKEY`, and we do not support + // `EVP_PKEY_RSA_PSS` keygen. const EVP_PKEY_ALG *alg = nullptr; switch (id) { case EVP_PKEY_RSA: @@ -106,6 +107,15 @@ EVP_PKEY_CTX *EVP_PKEY_CTX_new_id(int id, ENGINE *e) { case EVP_PKEY_ML_KEM_1024: alg = EVP_pkey_ml_kem_1024(); break; + case EVP_PKEY_SLH_DSA_SHA2_128S: + alg = EVP_pkey_slh_dsa_sha2_128s(); + break; + case EVP_PKEY_SLH_DSA_SHA2_128S_WITH_SHA256: + alg = EVP_pkey_slh_dsa_sha2_128s_with_sha256(); + break; + case EVP_PKEY_SLH_DSA_SHAKE_256F: + alg = EVP_pkey_slh_dsa_shake_256f(); + break; } if (alg == nullptr || alg->pkey_method == nullptr) { OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_ALGORITHM); @@ -151,7 +161,7 @@ EVP_PKEY_CTX *EVP_PKEY_CTX_dup(EVP_PKEY_CTX *ctx) { return ret.release(); } -EVP_PKEY *EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx) { +EVP_PKEY *EVP_PKEY_CTX_get0_pkey(const EVP_PKEY_CTX *ctx) { auto *impl = FromOpaque(ctx); return impl->pkey.get(); } @@ -315,7 +325,7 @@ int EVP_PKEY_derive_init(EVP_PKEY_CTX *ctx) { return 1; } -int EVP_PKEY_derive_set_peer(EVP_PKEY_CTX *ctx, EVP_PKEY *peer) { +int EVP_PKEY_derive_set_peer(EVP_PKEY_CTX *ctx, const EVP_PKEY *peer) { auto *impl = FromOpaque(ctx); if (!impl || !impl->pmeth || !(impl->pmeth->derive || impl->pmeth->encrypt || impl->pmeth->decrypt) || @@ -330,7 +340,8 @@ int EVP_PKEY_derive_set_peer(EVP_PKEY_CTX *ctx, EVP_PKEY *peer) { return 0; } - int ret = impl->pmeth->ctrl(impl, EVP_PKEY_CTRL_PEER_KEY, 0, peer); + int ret = impl->pmeth->ctrl(impl, EVP_PKEY_CTRL_PEER_KEY, 0, + const_cast(peer)); if (ret <= 0) { return 0; @@ -357,7 +368,7 @@ int EVP_PKEY_derive_set_peer(EVP_PKEY_CTX *ctx, EVP_PKEY *peer) { } impl->peerkey = UpRef(FromOpaque(peer)); - ret = impl->pmeth->ctrl(impl, EVP_PKEY_CTRL_PEER_KEY, 1, peer); + ret = impl->pmeth->ctrl(impl, EVP_PKEY_CTRL_PEER_KEY, 1, impl->peerkey.get()); if (ret <= 0) { impl->peerkey = nullptr; return 0; diff --git a/third_party/boringssl/src/crypto/evp/evp_kem.cc b/third_party/boringssl/src/crypto/evp/evp_kem.cc index e35043308..e718caf97 100644 --- a/third_party/boringssl/src/crypto/evp/evp_kem.cc +++ b/third_party/boringssl/src/crypto/evp/evp_kem.cc @@ -36,7 +36,8 @@ static bool check_kem_invocation(const EVP_KEM *kem, OPENSSL_PUT_ERROR(EVP, ERR_R_PASSED_NULL_PARAMETER); return false; } - if (kem->pkey_id != EVP_PKEY_id(pkey_impl)) { + if (kem->pkey_id != EVP_PKEY_id(pkey_impl) || + (kem->check_key != nullptr && !kem->check_key(kem, pkey_impl))) { OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_ALGORITHM); return false; } @@ -55,8 +56,10 @@ size_t EVP_KEM_ciphertext_len(const EVP_KEM *kem) { return kem->ciphertext_len; } -size_t EVP_KEM_secret_len(const EVP_KEM *kem) { - return kem->secret_len; +size_t EVP_KEM_secret_len(const EVP_KEM *kem) { return kem->secret_len; } + +size_t EVP_KEM_entropy_len_for_testing(const EVP_KEM *kem) { + return kem->entropy_len; } int EVP_KEM_encap(const EVP_KEM *kem, uint8_t *out_ciphertext, @@ -66,8 +69,25 @@ int EVP_KEM_encap(const EVP_KEM *kem, uint8_t *out_ciphertext, if (!check_kem_invocation(kem, &ciphertext_len, secret_len, pkey_impl)) { return 0; } - return kem->encap(out_ciphertext, ciphertext_len, out_secret, secret_len, - pkey_impl); + return kem->encap(kem, Span(out_ciphertext, ciphertext_len), + Span(out_secret, secret_len), pkey_impl); +} + +int EVP_KEM_encap_external_entropy_for_testing( + const EVP_KEM *kem, uint8_t *out_ciphertext, size_t ciphertext_len, + uint8_t *out_secret, size_t secret_len, const EVP_PKEY *peer_key, + const uint8_t *entropy, size_t entropy_len) { + auto *pkey_impl = FromOpaque(peer_key); + if (!check_kem_invocation(kem, &ciphertext_len, secret_len, pkey_impl)) { + return 0; + } + if (entropy_len != kem->entropy_len) { + OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_ENTROPY_LENGTH); + return 0; + } + return kem->encap_external_entropy(kem, Span(out_ciphertext, ciphertext_len), + Span(out_secret, secret_len), pkey_impl, + Span(entropy, entropy_len)); } int EVP_KEM_decap(const EVP_KEM *kem, uint8_t *out_secret, size_t secret_len, @@ -77,6 +97,6 @@ int EVP_KEM_decap(const EVP_KEM *kem, uint8_t *out_secret, size_t secret_len, if (!check_kem_invocation(kem, nullptr, secret_len, pkey_impl)) { return 0; } - return kem->decap(out_secret, secret_len, ciphertext, ciphertext_len, - pkey_impl); + return kem->decap(kem, Span(out_secret, secret_len), + Span(ciphertext, ciphertext_len), pkey_impl); } diff --git a/third_party/boringssl/src/crypto/evp/internal.h b/third_party/boringssl/src/crypto/evp/internal.h index 10485b64a..d4e57dc08 100644 --- a/third_party/boringssl/src/crypto/evp/internal.h +++ b/third_party/boringssl/src/crypto/evp/internal.h @@ -36,7 +36,7 @@ typedef struct evp_pkey_ctx_method_st EVP_PKEY_CTX_METHOD; BSSL_NAMESPACE_END struct evp_pkey_alg_st { - // method and pkey_method implement operations for this |EVP_PKEY_ALG|. + // method and pkey_method implement operations for this `EVP_PKEY_ALG`. const bssl::EVP_PKEY_ASN1_METHOD *method; const bssl::EVP_PKEY_CTX_METHOD *pkey_method; }; @@ -50,7 +50,7 @@ enum evp_decode_result_t { }; struct evp_pkey_asn1_method_st { - // pkey_id contains one of the |EVP_PKEY_*| values and corresponds to the OID + // pkey_id contains one of the `EVP_PKEY_*` values and corresponds to the OID // in the key type's AlgorithmIdentifier. int pkey_id; uint8_t oid[9]; @@ -58,53 +58,53 @@ struct evp_pkey_asn1_method_st { const EVP_PKEY_CTX_METHOD *pkey_method; - // pub_decode decodes |params| and |key| as a SubjectPublicKeyInfo - // and writes the result into |out|. It returns |evp_decode_ok| on success, - // and |evp_decode_error| on error, and |evp_decode_unsupported| if the input - // was not supported by this |EVP_PKEY_ALG|. In case of - // |evp_decode_unsupported|, it does not add an error to the error queue. May - // modify |params| and |key|. Callers must make a copy if calling in a loop. + // pub_decode decodes `params` and `key` as a SubjectPublicKeyInfo + // and writes the result into `out`. It returns `evp_decode_ok` on success, + // and `evp_decode_error` on error, and `evp_decode_unsupported` if the input + // was not supported by this `EVP_PKEY_ALG`. In case of + // `evp_decode_unsupported`, it does not add an error to the error queue. May + // modify `params` and `key`. Callers must make a copy if calling in a loop. // - // |params| is the AlgorithmIdentifier after the OBJECT IDENTIFIER type field, - // and |key| is the contents of the subjectPublicKey with the leading padding + // `params` is the AlgorithmIdentifier after the OBJECT IDENTIFIER type field, + // and `key` is the contents of the subjectPublicKey with the leading padding // byte checked and removed. Although X.509 uses BIT STRINGs to represent // SubjectPublicKeyInfo, every key type defined encodes the key as a byte // string with the same conversion to BIT STRING. evp_decode_result_t (*pub_decode)(const EVP_PKEY_ALG *alg, EvpPkey *out, CBS *params, CBS *key); - // pub_encode encodes |key| as a SubjectPublicKeyInfo and appends the result - // to |out|. It returns one on success and zero on error. + // pub_encode encodes `key` as a SubjectPublicKeyInfo and appends the result + // to `out`. It returns one on success and zero on error. int (*pub_encode)(CBB *out, const EvpPkey *key); bool (*pub_equal)(const EvpPkey *a, const EvpPkey *b); - // pub_present returns true iff the |pk| has a public key. (If so, validity + // pub_present returns true iff the `pk` has a public key. (If so, validity // is not guaranteed and should be checked separately.) bool (*pub_present)(const EvpPkey *pk); - // pub_copy sets the key data of |out| to a newly allocated key data structure - // which contains a copy of only the public key of |pk|, freeing any key - // previously in |out|. Returns true on success or false on failure. + // pub_copy sets the key data of `out` to a newly allocated key data structure + // which contains a copy of only the public key of `pk`, freeing any key + // previously in `out`. Returns true on success or false on failure. bool (*pub_copy)(EvpPkey *out, const EvpPkey *pk); - // priv_decode decodes |params| and |key| as a PrivateKeyInfo and writes the - // result into |out|. It returns |evp_decode_ok| on success, and - // |evp_decode_error| on error, and |evp_decode_unsupported| if the key type - // was not supported by this |EVP_PKEY_ALG|. In case of - // |evp_decode_unsupported|, it does not add an error to the error queue. May - // modify |params| and |key|. Callers must make a copy if calling in a loop. + // priv_decode decodes `params` and `key` as a PrivateKeyInfo and writes the + // result into `out`. It returns `evp_decode_ok` on success, and + // `evp_decode_error` on error, and `evp_decode_unsupported` if the key type + // was not supported by this `EVP_PKEY_ALG`. In case of + // `evp_decode_unsupported`, it does not add an error to the error queue. May + // modify `params` and `key`. Callers must make a copy if calling in a loop. // - // |params| is the AlgorithmIdentifier after the OBJECT IDENTIFIER type field, - // and |key| is the contents of the OCTET STRING privateKey field. + // `params` is the AlgorithmIdentifier after the OBJECT IDENTIFIER type field, + // and `key` is the contents of the OCTET STRING privateKey field. evp_decode_result_t (*priv_decode)(const EVP_PKEY_ALG *alg, EvpPkey *out, CBS *params, CBS *key); - // priv_encode encodes |key| as a PrivateKeyInfo and appends the result to - // |out|. It returns one on success and zero on error. + // priv_encode encodes `key` as a PrivateKeyInfo and appends the result to + // `out`. It returns one on success and zero on error. int (*priv_encode)(CBB *out, const EvpPkey *key); - // priv_present returns true iff the |pk| has a private key. (If so, validity + // priv_present returns true iff the `pk` has a private key. (If so, validity // is not guaranteed and should be checked separately.) bool (*priv_present)(const EvpPkey *pk); @@ -116,9 +116,9 @@ struct evp_pkey_asn1_method_st { int (*get_pub_raw)(const EvpPkey *pkey, uint8_t *out, size_t *out_len); // TODO(davidben): Can these be merged with the functions above? OpenSSL does - // not implement |EVP_PKEY_get_raw_public_key|, etc., for |EVP_PKEY_EC|, but + // not implement `EVP_PKEY_get_raw_public_key`, etc., for `EVP_PKEY_EC`, but // the distinction seems unimportant. OpenSSL 3.0 has since renamed - // |EVP_PKEY_get1_tls_encodedpoint| to |EVP_PKEY_get1_encoded_public_key|, and + // `EVP_PKEY_get1_tls_encodedpoint` to `EVP_PKEY_get1_encoded_public_key`, and // what is the difference between "raw" and an "encoded" public key. // // One nuisance is the notion of "raw" is slightly ambiguous for EC keys. Is @@ -126,7 +126,7 @@ struct evp_pkey_asn1_method_st { int (*set1_tls_encodedpoint)(EvpPkey *pkey, const uint8_t *in, size_t len); size_t (*get1_tls_encodedpoint)(const EvpPkey *pkey, uint8_t **out_ptr); - // pkey_opaque returns 1 if the |pk| is opaque. Opaque keys are backed by + // pkey_opaque returns 1 if the `pk` is opaque. Opaque keys are backed by // custom implementations which do not expose key material and parameters. int (*pkey_opaque)(const EvpPkey *pk); @@ -144,7 +144,7 @@ class EvpPkey : public evp_pkey_st, public RefCounted { public: EvpPkey(); - // pkey contains a pointer to a structure dependent on |ameth|. + // pkey contains a pointer to a structure dependent on `ameth`. void *pkey = nullptr; // ameth contains a pointer to a method table that determines the key type, or @@ -175,12 +175,12 @@ class EvpPkey : public evp_pkey_st, public RefCounted { #define EVP_PKEY_OP_TYPE_GEN (EVP_PKEY_OP_KEYGEN | EVP_PKEY_OP_PARAMGEN) -// EVP_PKEY_CTX_ctrl performs |cmd| on |ctx|. The |keytype| and |optype| +// EVP_PKEY_CTX_ctrl performs `cmd` on `ctx`. The `keytype` and `optype` // arguments can be -1 to specify that any type and operation are acceptable, -// otherwise |keytype| must match the type of |ctx| and the bits of |optype| -// must intersect the operation flags set on |ctx|. +// otherwise `keytype` must match the type of `ctx` and the bits of `optype` +// must intersect the operation flags set on `ctx`. // -// The |p1| and |p2| arguments depend on the value of |cmd|. +// The `p1` and `p2` arguments depend on the value of `cmd`. // // It returns one on success and zero on error. OPENSSL_EXPORT int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype, @@ -189,14 +189,14 @@ OPENSSL_EXPORT int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype, #define EVP_PKEY_CTRL_MD 1 #define EVP_PKEY_CTRL_GET_MD 2 -// EVP_PKEY_CTRL_PEER_KEY is called with different values of |p1|: -// 0: Is called from |EVP_PKEY_derive_set_peer| and |p2| contains a peer key. +// EVP_PKEY_CTRL_PEER_KEY is called with different values of `p1`: +// 0: Is called from `EVP_PKEY_derive_set_peer` and `p2` contains a peer key. // If the return value is <= 0, the key is rejected. -// 1: Is called at the end of |EVP_PKEY_derive_set_peer| and |p2| contains a +// 1: Is called at the end of `EVP_PKEY_derive_set_peer` and `p2` contains a // peer key. If the return value is <= 0, the key is rejected. -// 2: Is called with |p2| == NULL to test whether the peer's key was used. +// 2: Is called with `p2` == NULL to test whether the peer's key was used. // (EC)DH always return one in this case. -// 3: Is called with |p2| == NULL to set whether the peer's key was used. +// 3: Is called with `p2` == NULL to set whether the peer's key was used. // (EC)DH always return one in this case. This was only used for GOST. #define EVP_PKEY_CTRL_PEER_KEY 3 @@ -229,10 +229,20 @@ class EvpPkeyCtx : public evp_pkey_ctx_st { public: static constexpr bool kAllowUniquePtr = true; - // TODO(crbug.com/487376811): Ideally this destructor should be virtual so - // that we can emit vtables in libcrypto. In that case we would be able to - // replace |pmeth| with virtual methods and subclassing. + // Use an optional virtual destructor. This class does not (yet) have a need + // for a vtable, but we intend to write code with vtables in the future. + // Virtual destructors add a reference to symbols in the C++ runtime, so trial + // the dependency here. This is, temporarily, gated on a build define. If this + // breaks your build, build with `BORINGSSL_TEMPORARY_NO_CXX_RUNTIME` and then + // contact the BoringSSL team, so we can help fix your build. + // + // TODO(crbug.com/486922845): Remove the `BORINGSSL_TEMPORARY_NO_CXX_RUNTIME` + // case. +#if defined(BORINGSSL_TEMPORARY_NO_CXX_RUNTIME) ~EvpPkeyCtx(); +#else + virtual ~EvpPkeyCtx(); +#endif // Method associated with this operation const bssl::EVP_PKEY_CTX_METHOD *pmeth = nullptr; @@ -240,10 +250,10 @@ class EvpPkeyCtx : public evp_pkey_ctx_st { bssl::UniquePtr pkey; // Peer key for key agreement, may be nullptr bssl::UniquePtr peerkey; - // operation contains one of the |EVP_PKEY_OP_*| values. + // operation contains one of the `EVP_PKEY_OP_*` values. int operation = EVP_PKEY_OP_UNDEFINED; // Algorithm specific data. - // TODO(crbug.com/487376811): Since a |EVP_PKEY_CTX| never has its type change + // TODO(crbug.com/487376811): Since a `EVP_PKEY_CTX` never has its type change // after creation, this should instead be a base class, with the // algorithm-specific data on the subclass, coming from the same allocation. void *data = nullptr; @@ -252,7 +262,7 @@ class EvpPkeyCtx : public evp_pkey_ctx_st { struct evp_pkey_ctx_method_st { int pkey_id; - // |alg| may be nullptr. If non-null, |ctx| will have a key set. + // `alg` may be nullptr. If non-null, `ctx` will have a key set. int (*init)(EvpPkeyCtx *ctx, const EVP_PKEY_ALG *alg); int (*copy)(EvpPkeyCtx *dst, EvpPkeyCtx *src); void (*cleanup)(EvpPkeyCtx *ctx); @@ -304,13 +314,22 @@ struct evp_kem_st { // Constant lengths of ciphertexts and secrets produced/consumed by this KEM. size_t ciphertext_len; size_t secret_len; - - int (*encap)(uint8_t *out_ciphertext, size_t ciphertext_len, - uint8_t *out_secret, size_t secret_len, - const EVP_PKEY *peer_key); - int (*decap)(uint8_t *out_secret, size_t secret_len, - const uint8_t *ciphertext, size_t ciphertext_len, - const EVP_PKEY *key); + // Fixed length of external entropy for testing. + size_t entropy_len; + + // check_key, if non-null, returns whether `key`, whose type matches + // `pkey_id`, is usable with this KEM. + bool (*check_key)(const EVP_KEM *kem, const EVP_PKEY *key); + + int (*encap)(const EVP_KEM *kem, bssl::Span out_ciphertext, + bssl::Span out_secret, const EVP_PKEY *peer_key); + int (*encap_external_entropy)(const EVP_KEM *kem, + bssl::Span out_ciphertext, + bssl::Span out_secret, + const EVP_PKEY *peer_key, + bssl::Span entropy); + int (*decap)(const EVP_KEM *kem, bssl::Span out_secret, + bssl::Span ciphertext, const EVP_PKEY *key); } /* EVP_KEM */; BSSL_NAMESPACE_BEGIN @@ -339,8 +358,8 @@ struct KemAdapter { OPENSSL_PUT_ERROR(EVP, EVP_R_BUFFER_TOO_SMALL); return 0; } - if (KEM.encap(out_ciphertext, KEM.ciphertext_len, out_secret, - KEM.secret_len, ctx->pkey.get())) { + if (KEM.encap(&KEM, Span(out_ciphertext, KEM.ciphertext_len), + Span(out_secret, KEM.secret_len), ctx->pkey.get())) { *out_ciphertext_len = KEM.ciphertext_len; *out_secret_len = KEM.secret_len; return 1; @@ -359,8 +378,8 @@ struct KemAdapter { OPENSSL_PUT_ERROR(EVP, EVP_R_BUFFER_TOO_SMALL); return 0; } - if (KEM.decap(out_secret, KEM.secret_len, ciphertext, ciphertext_len, - ctx->pkey.get())) { + if (KEM.decap(&KEM, Span(out_secret, KEM.secret_len), + Span(ciphertext, ciphertext_len), ctx->pkey.get())) { *out_secret_len = KEM.secret_len; return 1; } @@ -368,27 +387,27 @@ struct KemAdapter { } }; -// evp_pkey_ec_no_curve returns an internal curveless EC |EVP_PKEY_ALG|. This +// evp_pkey_ec_no_curve returns an internal curveless EC `EVP_PKEY_ALG`. This // cannot be used to parse anything and is only useful for key generation. const EVP_PKEY_ALG *evp_pkey_ec_no_curve(); -// evp_pkey_hkdf returns an internal |EVP_PKEY_ALG| used to implement -// |EVP_PKEY_HKDF|. It has no associated key type. +// evp_pkey_hkdf returns an internal `EVP_PKEY_ALG` used to implement +// `EVP_PKEY_HKDF`. It has no associated key type. const EVP_PKEY_ALG *evp_pkey_hkdf(); -// evp_pkey_ctx_new_alg behaves like |EVP_PKEY_CTX_new_id| but takes an -// |EVP_PKEY_ALG|. +// evp_pkey_ctx_new_alg behaves like `EVP_PKEY_CTX_new_id` but takes an +// `EVP_PKEY_ALG`. UniquePtr evp_pkey_ctx_new_alg(const EVP_PKEY_ALG *alg); -// evp_pkey_set0 sets |pkey|'s method to |method| and data to |pkey_data|, +// evp_pkey_set0 sets `pkey`'s method to `method` and data to `pkey_data`, // freeing any key that may previously have been configured. This function takes -// ownership of |pkey_data|, which must be of the type expected by |method|. +// ownership of `pkey_data`, which must be of the type expected by `method`. void evp_pkey_set0(EvpPkey *pkey, const EVP_PKEY_ASN1_METHOD *method, void *pkey_data); inline auto GetDefaultEVPAlgorithms() { - // A set of algorithms to use by default in |EVP_parse_public_key| and - // |EVP_parse_private_key|. + // A set of algorithms to use by default in `EVP_parse_public_key` and + // `EVP_parse_private_key`. return std::array{ EVP_pkey_ec_p224(), EVP_pkey_ec_p256(), @@ -403,7 +422,7 @@ inline auto GetDefaultEVPAlgorithms() { EVP_pkey_ml_kem_768(), EVP_pkey_ml_kem_1024(), // TODO(crbug.com/438761503): Remove DSA from this set, after callers that - // need DSA pass in |EVP_pkey_dsa| explicitly. + // need DSA pass in `EVP_pkey_dsa` explicitly. EVP_pkey_dsa(), }; } diff --git a/third_party/boringssl/src/crypto/evp/p_dh.cc b/third_party/boringssl/src/crypto/evp/p_dh.cc index eaf7fadc8..aa1db9f9c 100644 --- a/third_party/boringssl/src/crypto/evp/p_dh.cc +++ b/third_party/boringssl/src/crypto/evp/p_dh.cc @@ -34,25 +34,25 @@ namespace { extern const EVP_PKEY_CTX_METHOD dh_pkey_meth; -static void dh_free(EvpPkey *pkey) { +void dh_free(EvpPkey *pkey) { DH_free(reinterpret_cast(pkey->pkey)); pkey->pkey = nullptr; } -static int dh_size(const EvpPkey *pkey) { +int dh_size(const EvpPkey *pkey) { return DH_size(reinterpret_cast(pkey->pkey)); } -static int dh_bits(const EvpPkey *pkey) { +int dh_bits(const EvpPkey *pkey) { return DH_bits(reinterpret_cast(pkey->pkey)); } -static int dh_param_missing(const EvpPkey *pkey) { +int dh_param_missing(const EvpPkey *pkey) { const DH *dh = reinterpret_cast(pkey->pkey); return dh == nullptr || DH_get0_p(dh) == nullptr || DH_get0_g(dh) == nullptr; } -static int dh_param_copy(EvpPkey *to, const EvpPkey *from) { +int dh_param_copy(EvpPkey *to, const EvpPkey *from) { if (dh_param_missing(from)) { OPENSSL_PUT_ERROR(EVP, EVP_R_MISSING_PARAMETERS); return 0; @@ -77,11 +77,11 @@ static int dh_param_copy(EvpPkey *to, const EvpPkey *from) { return 0; } - // |DH_set0_pqg| took ownership of |p|, |q|, and |g|. + // `DH_set0_pqg` took ownership of `p`, `q`, and `g`. return 1; } -static bool dh_param_equal(const EvpPkey *a, const EvpPkey *b) { +bool dh_param_equal(const EvpPkey *a, const EvpPkey *b) { if (dh_param_missing(a) || dh_param_missing(b)) { return false; } @@ -94,7 +94,7 @@ static bool dh_param_equal(const EvpPkey *a, const EvpPkey *b) { BN_cmp(DH_get0_g(a_dh), DH_get0_g(b_dh)) == 0; } -static bool dh_pub_equal(const EvpPkey *a, const EvpPkey *b) { +bool dh_pub_equal(const EvpPkey *a, const EvpPkey *b) { if (!dh_param_equal(a, b)) { return false; } @@ -104,12 +104,12 @@ static bool dh_pub_equal(const EvpPkey *a, const EvpPkey *b) { return BN_cmp(DH_get0_pub_key(a_dh), DH_get0_pub_key(b_dh)) == 0; } -static bool dh_has_pub(const EvpPkey *pk) { +bool dh_has_pub(const EvpPkey *pk) { const DH *pk_dh = reinterpret_cast(pk->pkey); return DH_get0_pub_key(pk_dh) != nullptr; } -static bool dh_pub_copy(EvpPkey *out, const EvpPkey *pk) { +bool dh_pub_copy(EvpPkey *out, const EvpPkey *pk) { const DH *pk_dh = reinterpret_cast(pk->pkey); const BIGNUM *public_key = DH_get0_pub_key(pk_dh); if (public_key == nullptr) { @@ -128,12 +128,12 @@ static bool dh_pub_copy(EvpPkey *out, const EvpPkey *pk) { return true; } -static bool dh_has_priv(const EvpPkey *pk) { +bool dh_has_priv(const EvpPkey *pk) { const DH *pk_dh = reinterpret_cast(pk->pkey); return DH_get0_priv_key(pk_dh) != nullptr; } -static const EVP_PKEY_ASN1_METHOD dh_asn1_meth = { +const EVP_PKEY_ASN1_METHOD dh_asn1_meth = { /*pkey_id=*/EVP_PKEY_DH, /*oid=*/{0}, /*oid_len=*/0, @@ -167,7 +167,7 @@ struct DH_PKEY_CTX { bool pad = false; }; -static int pkey_dh_init(EvpPkeyCtx *ctx, const EVP_PKEY_ALG *) { +int pkey_dh_init(EvpPkeyCtx *ctx, const EVP_PKEY_ALG *) { DH_PKEY_CTX *dctx = New(); if (dctx == nullptr) { return 0; @@ -177,7 +177,7 @@ static int pkey_dh_init(EvpPkeyCtx *ctx, const EVP_PKEY_ALG *) { return 1; } -static int pkey_dh_copy(EvpPkeyCtx *dst, EvpPkeyCtx *src) { +int pkey_dh_copy(EvpPkeyCtx *dst, EvpPkeyCtx *src) { if (!pkey_dh_init(dst, nullptr)) { return 0; } @@ -188,11 +188,11 @@ static int pkey_dh_copy(EvpPkeyCtx *dst, EvpPkeyCtx *src) { return 1; } -static void pkey_dh_cleanup(EvpPkeyCtx *ctx) { +void pkey_dh_cleanup(EvpPkeyCtx *ctx) { Delete(reinterpret_cast(ctx->data)); } -static int pkey_dh_keygen(EvpPkeyCtx *ctx, EvpPkey *pkey) { +int pkey_dh_keygen(EvpPkeyCtx *ctx, EvpPkey *pkey) { DH *dh = DH_new(); if (dh == nullptr || !EVP_PKEY_assign_DH(pkey, dh)) { DH_free(dh); @@ -207,7 +207,7 @@ static int pkey_dh_keygen(EvpPkeyCtx *ctx, EvpPkey *pkey) { return DH_generate_key(dh); } -static int pkey_dh_derive(EvpPkeyCtx *ctx, uint8_t *out, size_t *out_len) { +int pkey_dh_derive(EvpPkeyCtx *ctx, uint8_t *out, size_t *out_len) { DH_PKEY_CTX *dctx = reinterpret_cast(ctx->data); if (ctx->pkey == nullptr || ctx->peerkey == nullptr) { OPENSSL_PUT_ERROR(EVP, EVP_R_KEYS_NOT_SET); @@ -248,11 +248,11 @@ static int pkey_dh_derive(EvpPkeyCtx *ctx, uint8_t *out, size_t *out_len) { return 1; } -static int pkey_dh_ctrl(EvpPkeyCtx *ctx, int type, int p1, void *p2) { +int pkey_dh_ctrl(EvpPkeyCtx *ctx, int type, int p1, void *p2) { DH_PKEY_CTX *dctx = reinterpret_cast(ctx->data); switch (type) { case EVP_PKEY_CTRL_PEER_KEY: - // |EVP_PKEY_derive_set_peer| requires the key implement this command, + // `EVP_PKEY_derive_set_peer` requires the key implement this command, // even if it is a no-op. return 1; @@ -288,12 +288,8 @@ const EVP_PKEY_CTX_METHOD dh_pkey_meth = { } // namespace -int EVP_PKEY_set1_DH(EVP_PKEY *pkey, DH *key) { - if (EVP_PKEY_assign_DH(pkey, key)) { - DH_up_ref(key); - return 1; - } - return 0; +int EVP_PKEY_set1_DH(EVP_PKEY *pkey, const DH *key) { + return EVP_PKEY_assign_DH(pkey, DH_dup_ref(key)); } int EVP_PKEY_assign_DH(EVP_PKEY *pkey, DH *key) { diff --git a/third_party/boringssl/src/crypto/evp/p_dsa.cc b/third_party/boringssl/src/crypto/evp/p_dsa.cc index 6b6201c4b..f8ffde4bb 100644 --- a/third_party/boringssl/src/crypto/evp/p_dsa.cc +++ b/third_party/boringssl/src/crypto/evp/p_dsa.cc @@ -31,9 +31,9 @@ namespace { extern const EVP_PKEY_ASN1_METHOD dsa_asn1_meth; -static bssl::evp_decode_result_t dsa_pub_decode(const EVP_PKEY_ALG *alg, - EvpPkey *out, CBS *params, - CBS *key) { +bssl::evp_decode_result_t dsa_pub_decode(const EVP_PKEY_ALG *alg, + EvpPkey *out, CBS *params, + CBS *key) { // See RFC 3279, section 2.3.2. // Decode parameters. RFC 3279 permits DSA parameters to be omitted, in which @@ -59,7 +59,7 @@ static bssl::evp_decode_result_t dsa_pub_decode(const EVP_PKEY_ALG *alg, return evp_decode_ok; } -static int dsa_pub_encode(CBB *out, const EvpPkey *key) { +int dsa_pub_encode(CBB *out, const EvpPkey *key) { const DSAImpl *dsa = reinterpret_cast(key->pkey); const int has_params = dsa->p != nullptr && dsa->q != nullptr && dsa->g != nullptr; @@ -81,9 +81,9 @@ static int dsa_pub_encode(CBB *out, const EvpPkey *key) { return 1; } -static bssl::evp_decode_result_t dsa_priv_decode(const EVP_PKEY_ALG *alg, - EvpPkey *out, CBS *params, - CBS *key) { +bssl::evp_decode_result_t dsa_priv_decode(const EVP_PKEY_ALG *alg, + EvpPkey *out, CBS *params, + CBS *key) { // See PKCS#11, v2.40, section 2.5. // Decode parameters. @@ -102,8 +102,8 @@ static bssl::evp_decode_result_t dsa_priv_decode(const EVP_PKEY_ALG *alg, return evp_decode_error; } - // To avoid DoS attacks when importing private keys, check bounds on |dsa|. - // This bounds |dsa->priv_key| against |dsa->q| and bounds |dsa->q|'s bit + // To avoid DoS attacks when importing private keys, check bounds on `dsa`. + // This bounds `dsa->priv_key` against `dsa->q` and bounds `dsa->q`'s bit // width. if (!dsa_check_key(dsa.get())) { OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR); @@ -124,7 +124,7 @@ static bssl::evp_decode_result_t dsa_priv_decode(const EVP_PKEY_ALG *alg, return evp_decode_ok; } -static int dsa_priv_encode(CBB *out, const EvpPkey *key) { +int dsa_priv_encode(CBB *out, const EvpPkey *key) { const DSAImpl *dsa = reinterpret_cast(key->pkey); if (dsa == nullptr || dsa->priv_key == nullptr) { OPENSSL_PUT_ERROR(EVP, EVP_R_MISSING_PARAMETERS); @@ -148,17 +148,17 @@ static int dsa_priv_encode(CBB *out, const EvpPkey *key) { return 1; } -static int int_dsa_size(const EvpPkey *pkey) { +int int_dsa_size(const EvpPkey *pkey) { const DSAImpl *dsa = reinterpret_cast(pkey->pkey); return DSA_size(dsa); } -static int dsa_bits(const EvpPkey *pkey) { +int dsa_bits(const EvpPkey *pkey) { const DSAImpl *dsa = reinterpret_cast(pkey->pkey); return BN_num_bits(DSA_get0_p(dsa)); } -static int dsa_missing_parameters(const EvpPkey *pkey) { +int dsa_missing_parameters(const EvpPkey *pkey) { const DSAImpl *dsa = reinterpret_cast(pkey->pkey); if (DSA_get0_p(dsa) == nullptr || DSA_get0_q(dsa) == nullptr || DSA_get0_g(dsa) == nullptr) { @@ -167,7 +167,7 @@ static int dsa_missing_parameters(const EvpPkey *pkey) { return 0; } -static int dup_bn_into(UniquePtr *out, const BIGNUM *src) { +int dup_bn_into(UniquePtr *out, const BIGNUM *src) { UniquePtr copy(BN_dup(src)); if (copy == nullptr) { return 0; @@ -176,7 +176,7 @@ static int dup_bn_into(UniquePtr *out, const BIGNUM *src) { return 1; } -static int dsa_copy_parameters(EvpPkey *to, const EvpPkey *from) { +int dsa_copy_parameters(EvpPkey *to, const EvpPkey *from) { if (to->pkey == nullptr) { to->pkey = DSA_new(); if (to->pkey == nullptr) { @@ -194,7 +194,7 @@ static int dsa_copy_parameters(EvpPkey *to, const EvpPkey *from) { return 1; } -static bool dsa_equal_parameters(const EvpPkey *a, const EvpPkey *b) { +bool dsa_equal_parameters(const EvpPkey *a, const EvpPkey *b) { const DSAImpl *a_dsa = reinterpret_cast(a->pkey); const DSAImpl *b_dsa = reinterpret_cast(b->pkey); return BN_cmp(DSA_get0_p(a_dsa), DSA_get0_p(b_dsa)) == 0 && @@ -202,18 +202,18 @@ static bool dsa_equal_parameters(const EvpPkey *a, const EvpPkey *b) { BN_cmp(DSA_get0_g(a_dsa), DSA_get0_g(b_dsa)) == 0; } -static bool dsa_pub_equal(const EvpPkey *a, const EvpPkey *b) { +bool dsa_pub_equal(const EvpPkey *a, const EvpPkey *b) { const DSAImpl *a_dsa = reinterpret_cast(a->pkey); const DSAImpl *b_dsa = reinterpret_cast(b->pkey); return BN_cmp(DSA_get0_pub_key(b_dsa), DSA_get0_pub_key(a_dsa)) == 0; } -static bool dsa_pub_present(const EvpPkey *pk) { +bool dsa_pub_present(const EvpPkey *pk) { const DSA *pk_dsa = reinterpret_cast(pk->pkey); return DSA_get0_pub_key(pk_dsa) != nullptr; } -static bool dsa_pub_copy(EvpPkey *out, const EvpPkey *pk) { +bool dsa_pub_copy(EvpPkey *out, const EvpPkey *pk) { const DSA *pk_dsa = reinterpret_cast(pk->pkey); const BIGNUM *public_key = DSA_get0_pub_key(pk_dsa); if (public_key == nullptr) { @@ -232,12 +232,12 @@ static bool dsa_pub_copy(EvpPkey *out, const EvpPkey *pk) { return true; } -static bool dsa_priv_present(const EvpPkey *pk) { +bool dsa_priv_present(const EvpPkey *pk) { const DSA *pk_dsa = reinterpret_cast(pk->pkey); return DSA_get0_priv_key(pk_dsa) != nullptr; } -static void int_dsa_free(EvpPkey *pkey) { +void int_dsa_free(EvpPkey *pkey) { DSA_free(reinterpret_cast(pkey->pkey)); pkey->pkey = nullptr; } @@ -289,23 +289,19 @@ const EVP_PKEY_ALG *EVP_pkey_dsa() { } int EVP_PKEY_CTX_set_dsa_paramgen_bits(EVP_PKEY_CTX *ctx, int nbits) { - // BoringSSL does not support DSA in |EVP_PKEY_CTX|. + // BoringSSL does not support DSA in `EVP_PKEY_CTX`. OPENSSL_PUT_ERROR(EVP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); return 0; } int EVP_PKEY_CTX_set_dsa_paramgen_q_bits(EVP_PKEY_CTX *ctx, int qbits) { - // BoringSSL does not support DSA in |EVP_PKEY_CTX|. + // BoringSSL does not support DSA in `EVP_PKEY_CTX`. OPENSSL_PUT_ERROR(EVP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); return 0; } -int EVP_PKEY_set1_DSA(EVP_PKEY *pkey, DSA *key) { - if (EVP_PKEY_assign_DSA(pkey, key)) { - DSA_up_ref(key); - return 1; - } - return 0; +int EVP_PKEY_set1_DSA(EVP_PKEY *pkey, const DSA *key) { + return EVP_PKEY_assign_DSA(pkey, DSA_dup_ref(key)); } int EVP_PKEY_assign_DSA(EVP_PKEY *pkey, DSA *key) { diff --git a/third_party/boringssl/src/crypto/evp/p_ec.cc b/third_party/boringssl/src/crypto/evp/p_ec.cc index ac9a489d2..51e7145d2 100644 --- a/third_party/boringssl/src/crypto/evp/p_ec.cc +++ b/third_party/boringssl/src/crypto/evp/p_ec.cc @@ -40,14 +40,14 @@ using namespace bssl; namespace { struct EVP_PKEY_ALG_EC : public EVP_PKEY_ALG { - // ec_group returns the |EC_GROUP| for this algorithm. + // ec_group returns the `EC_GROUP` for this algorithm. const EC_GROUP *(*ec_group)(); }; extern const EVP_PKEY_ASN1_METHOD ec_asn1_meth; extern const EVP_PKEY_CTX_METHOD ec_pkey_meth; -static int eckey_pub_encode(CBB *out, const EvpPkey *key) { +int eckey_pub_encode(CBB *out, const EvpPkey *key) { const EC_KEY *ec_key = reinterpret_cast(key->pkey); const EC_GROUP *group = EC_KEY_get0_group(ec_key); const EC_POINT *public_key = EC_KEY_get0_public_key(ec_key); @@ -71,9 +71,9 @@ static int eckey_pub_encode(CBB *out, const EvpPkey *key) { return 1; } -static bssl::evp_decode_result_t eckey_pub_decode(const EVP_PKEY_ALG *alg, - EvpPkey *out, CBS *params, - CBS *key) { +bssl::evp_decode_result_t eckey_pub_decode(const EVP_PKEY_ALG *alg, + EvpPkey *out, CBS *params, + CBS *key) { const auto *ec_alg = static_cast(alg); if (ec_alg->ec_group == nullptr) { return evp_decode_unsupported; @@ -81,7 +81,7 @@ static bssl::evp_decode_result_t eckey_pub_decode(const EVP_PKEY_ALG *alg, // See RFC 5480, section 2. - // Check that |params| matches |alg|. Only the namedCurve form is allowed. + // Check that `params` matches `alg`. Only the namedCurve form is allowed. const EC_GROUP *group = ec_alg->ec_group(); if (ec_key_parse_curve_name(params, Span(&group, 1)) == nullptr) { if (ERR_equals(ERR_peek_last_error(), ERR_LIB_EC, EC_R_UNKNOWN_GROUP)) { @@ -107,7 +107,7 @@ static bssl::evp_decode_result_t eckey_pub_decode(const EVP_PKEY_ALG *alg, return evp_decode_ok; } -static bool eckey_pub_equal(const EvpPkey *a, const EvpPkey *b) { +bool eckey_pub_equal(const EvpPkey *a, const EvpPkey *b) { const EC_KEY *a_ec = reinterpret_cast(a->pkey); const EC_KEY *b_ec = reinterpret_cast(b->pkey); const EC_GROUP *group = EC_KEY_get0_group(b_ec); @@ -116,9 +116,9 @@ static bool eckey_pub_equal(const EvpPkey *a, const EvpPkey *b) { return EC_POINT_cmp(group, pa, pb, nullptr) == 0; } -static bssl::evp_decode_result_t eckey_priv_decode(const EVP_PKEY_ALG *alg, - EvpPkey *out, CBS *params, - CBS *key) { +bssl::evp_decode_result_t eckey_priv_decode(const EVP_PKEY_ALG *alg, + EvpPkey *out, CBS *params, + CBS *key) { const auto *ec_alg = static_cast(alg); if (ec_alg->ec_group == nullptr) { return evp_decode_unsupported; @@ -149,7 +149,7 @@ static bssl::evp_decode_result_t eckey_priv_decode(const EVP_PKEY_ALG *alg, return evp_decode_ok; } -static int eckey_priv_encode(CBB *out, const EvpPkey *key) { +int eckey_priv_encode(CBB *out, const EvpPkey *key) { const EC_KEY *ec_key = reinterpret_cast(key->pkey); // Omit the redundant copy of the curve name. This contradicts RFC 5915 but @@ -176,8 +176,8 @@ static int eckey_priv_encode(CBB *out, const EvpPkey *key) { return 1; } -static int eckey_set1_tls_encodedpoint(EvpPkey *pkey, const uint8_t *in, - size_t len) { +int eckey_set1_tls_encodedpoint(EvpPkey *pkey, const uint8_t *in, + size_t len) { EC_KEY *ec_key = reinterpret_cast(pkey->pkey); if (ec_key == nullptr) { OPENSSL_PUT_ERROR(EVP, EVP_R_NO_KEY_SET); @@ -187,8 +187,8 @@ static int eckey_set1_tls_encodedpoint(EvpPkey *pkey, const uint8_t *in, return EC_KEY_oct2key(ec_key, in, len, nullptr); } -static size_t eckey_get1_tls_encodedpoint(const EvpPkey *pkey, - uint8_t **out_ptr) { +size_t eckey_get1_tls_encodedpoint(const EvpPkey *pkey, + uint8_t **out_ptr) { const EC_KEY *ec_key = reinterpret_cast(pkey->pkey); if (ec_key == nullptr) { OPENSSL_PUT_ERROR(EVP, EVP_R_NO_KEY_SET); @@ -199,12 +199,12 @@ static size_t eckey_get1_tls_encodedpoint(const EvpPkey *pkey, nullptr); } -static int int_ec_size(const EvpPkey *pkey) { +int int_ec_size(const EvpPkey *pkey) { const EC_KEY *ec_key = reinterpret_cast(pkey->pkey); return ECDSA_size(ec_key); } -static int ec_bits(const EvpPkey *pkey) { +int ec_bits(const EvpPkey *pkey) { const EC_KEY *ec_key = reinterpret_cast(pkey->pkey); const EC_GROUP *group = EC_KEY_get0_group(ec_key); if (group == nullptr) { @@ -214,12 +214,12 @@ static int ec_bits(const EvpPkey *pkey) { return EC_GROUP_order_bits(group); } -static int ec_missing_parameters(const EvpPkey *pkey) { +int ec_missing_parameters(const EvpPkey *pkey) { const EC_KEY *ec_key = reinterpret_cast(pkey->pkey); return ec_key == nullptr || EC_KEY_get0_group(ec_key) == nullptr; } -static int ec_copy_parameters(EvpPkey *to, const EvpPkey *from) { +int ec_copy_parameters(EvpPkey *to, const EvpPkey *from) { const EC_KEY *from_key = reinterpret_cast(from->pkey); if (from_key == nullptr) { OPENSSL_PUT_ERROR(EVP, EVP_R_NO_KEY_SET); @@ -239,7 +239,7 @@ static int ec_copy_parameters(EvpPkey *to, const EvpPkey *from) { return EC_KEY_set_group(reinterpret_cast(to->pkey), group); } -static bool ec_equal_parameters(const EvpPkey *a, const EvpPkey *b) { +bool ec_equal_parameters(const EvpPkey *a, const EvpPkey *b) { const EC_KEY *a_ec = reinterpret_cast(a->pkey); const EC_KEY *b_ec = reinterpret_cast(b->pkey); if (a_ec == nullptr || b_ec == nullptr) { @@ -254,22 +254,22 @@ static bool ec_equal_parameters(const EvpPkey *a, const EvpPkey *b) { return EC_GROUP_cmp(group_a, group_b, nullptr) == 0; } -static void int_ec_free(EvpPkey *pkey) { +void int_ec_free(EvpPkey *pkey) { EC_KEY_free(reinterpret_cast(pkey->pkey)); pkey->pkey = nullptr; } -static int eckey_opaque(const EvpPkey *pkey) { +int eckey_opaque(const EvpPkey *pkey) { const EC_KEY *ec_key = reinterpret_cast(pkey->pkey); return EC_KEY_is_opaque(ec_key); } -static bool eckey_pub_present(const EvpPkey *pkey) { +bool eckey_pub_present(const EvpPkey *pkey) { const EC_KEY *ec_key = reinterpret_cast(pkey->pkey); return EC_KEY_get0_public_key(ec_key) != nullptr; } -static bool eckey_pub_copy(EvpPkey *out, const EvpPkey *pkey) { +bool eckey_pub_copy(EvpPkey *out, const EvpPkey *pkey) { const EC_KEY *ec_key = reinterpret_cast(pkey->pkey); const EC_POINT *public_key = EC_KEY_get0_public_key(ec_key); if (public_key == nullptr) { @@ -287,7 +287,7 @@ static bool eckey_pub_copy(EvpPkey *out, const EvpPkey *pkey) { return true; } -static bool eckey_priv_present(const EvpPkey *pkey) { +bool eckey_priv_present(const EvpPkey *pkey) { const EC_KEY *ec_key = reinterpret_cast(pkey->pkey); return EC_KEY_get0_private_key(ec_key) != nullptr; } @@ -337,7 +337,7 @@ struct EC_PKEY_CTX { const EC_GROUP *gen_group = nullptr; }; -static int pkey_ec_init(EvpPkeyCtx *ctx, const EVP_PKEY_ALG *alg) { +int pkey_ec_init(EvpPkeyCtx *ctx, const EVP_PKEY_ALG *alg) { EC_PKEY_CTX *dctx = New(); if (!dctx) { return 0; @@ -352,7 +352,7 @@ static int pkey_ec_init(EvpPkeyCtx *ctx, const EVP_PKEY_ALG *alg) { return 1; } -static int pkey_ec_copy(EvpPkeyCtx *dst, EvpPkeyCtx *src) { +int pkey_ec_copy(EvpPkeyCtx *dst, EvpPkeyCtx *src) { if (!pkey_ec_init(dst, nullptr)) { return 0; } @@ -364,12 +364,12 @@ static int pkey_ec_copy(EvpPkeyCtx *dst, EvpPkeyCtx *src) { return 1; } -static void pkey_ec_cleanup(EvpPkeyCtx *ctx) { +void pkey_ec_cleanup(EvpPkeyCtx *ctx) { Delete(reinterpret_cast(ctx->data)); } -static int pkey_ec_sign(EvpPkeyCtx *ctx, uint8_t *sig, size_t *siglen, - const uint8_t *tbs, size_t tbslen) { +int pkey_ec_sign(EvpPkeyCtx *ctx, uint8_t *sig, size_t *siglen, + const uint8_t *tbs, size_t tbslen) { const EC_KEY *ec = reinterpret_cast(ctx->pkey->pkey); if (!sig) { *siglen = ECDSA_size(ec); @@ -387,13 +387,13 @@ static int pkey_ec_sign(EvpPkeyCtx *ctx, uint8_t *sig, size_t *siglen, return 1; } -static int pkey_ec_verify(EvpPkeyCtx *ctx, const uint8_t *sig, size_t siglen, - const uint8_t *tbs, size_t tbslen) { +int pkey_ec_verify(EvpPkeyCtx *ctx, const uint8_t *sig, size_t siglen, + const uint8_t *tbs, size_t tbslen) { const EC_KEY *ec_key = reinterpret_cast(ctx->pkey->pkey); return ECDSA_verify(0, tbs, tbslen, sig, siglen, ec_key); } -static int pkey_ec_derive(EvpPkeyCtx *ctx, uint8_t *key, size_t *keylen) { +int pkey_ec_derive(EvpPkeyCtx *ctx, uint8_t *key, size_t *keylen) { if (!ctx->pkey || !ctx->peerkey) { OPENSSL_PUT_ERROR(EVP, EVP_R_KEYS_NOT_SET); return 0; @@ -421,7 +421,7 @@ static int pkey_ec_derive(EvpPkeyCtx *ctx, uint8_t *key, size_t *keylen) { return 1; } -static int pkey_ec_ctrl(EvpPkeyCtx *ctx, int type, int p1, void *p2) { +int pkey_ec_ctrl(EvpPkeyCtx *ctx, int type, int p1, void *p2) { EC_PKEY_CTX *dctx = reinterpret_cast(ctx->data); switch (type) { @@ -457,7 +457,7 @@ static int pkey_ec_ctrl(EvpPkeyCtx *ctx, int type, int p1, void *p2) { } } -static int pkey_ec_keygen(EvpPkeyCtx *ctx, EvpPkey *pkey) { +int pkey_ec_keygen(EvpPkeyCtx *ctx, EvpPkey *pkey) { EC_PKEY_CTX *dctx = reinterpret_cast(ctx->data); const EC_GROUP *group = dctx->gen_group; if (group == nullptr) { @@ -477,7 +477,7 @@ static int pkey_ec_keygen(EvpPkeyCtx *ctx, EvpPkey *pkey) { return 1; } -static int pkey_ec_paramgen(EvpPkeyCtx *ctx, EvpPkey *pkey) { +int pkey_ec_paramgen(EvpPkeyCtx *ctx, EvpPkey *pkey) { EC_PKEY_CTX *dctx = reinterpret_cast(ctx->data); if (dctx->gen_group == nullptr) { OPENSSL_PUT_ERROR(EVP, EVP_R_NO_PARAMETERS_SET); @@ -543,12 +543,8 @@ const EVP_PKEY_ALG *bssl::evp_pkey_ec_no_curve() { return &kAlg; } -int EVP_PKEY_set1_EC_KEY(EVP_PKEY *pkey, EC_KEY *key) { - if (EVP_PKEY_assign_EC_KEY(pkey, key)) { - EC_KEY_up_ref(key); - return 1; - } - return 0; +int EVP_PKEY_set1_EC_KEY(EVP_PKEY *pkey, const EC_KEY *key) { + return EVP_PKEY_assign_EC_KEY(pkey, EC_KEY_dup_ref(key)); } int EVP_PKEY_assign_EC_KEY(EVP_PKEY *pkey, EC_KEY *key) { @@ -575,6 +571,127 @@ EC_KEY *EVP_PKEY_get1_EC_KEY(const EVP_PKEY *pkey) { return ec_key; } +static const EC_GROUP *alg_to_group(const EVP_PKEY_ALG *alg) { + if (alg->method->pkey_id != EVP_PKEY_EC) { + OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_ALGORITHM); + return nullptr; + } + auto *alg_ec = static_cast(alg); + if (alg_ec->ec_group == nullptr) { + OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_ALGORITHM); + return nullptr; + } + return alg_ec->ec_group(); +} + +static EVP_PKEY *pkey_from_ec_point(const EC_GROUP *group, const uint8_t *in, + size_t len) { + UniquePtr pkey(EVP_PKEY_new()); + UniquePtr ec_key(EC_KEY_new()); + if (pkey == nullptr || ec_key == nullptr || + !EC_KEY_set_group(ec_key.get(), group) || + !EC_KEY_oct2key(ec_key.get(), in, len, nullptr)) { + return nullptr; + } + EVP_PKEY_assign_EC_KEY(pkey.get(), ec_key.release()); + return pkey.release(); +} + +EVP_PKEY *EVP_PKEY_from_ec_uncompressed_point( + const EVP_PKEY_ALG *alg, const uint8_t *in, size_t len) { + const EC_GROUP *group = alg_to_group(alg); + if (group == nullptr) { + return nullptr; + } + // The `EC_KEY` APIs are not separated by uncompressed and compressed. + if (len == 0 || in[0] != 0x04) { + OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR); + return nullptr; + } + return pkey_from_ec_point(group, in, len); +} + +EVP_PKEY *EVP_PKEY_from_ec_compressed_point(const EVP_PKEY_ALG *alg, + const uint8_t *in, size_t len) { + const EC_GROUP *group = alg_to_group(alg); + if (group == nullptr) { + return nullptr; + } + // The `EC_KEY` APIs are not separated by uncompressed and compressed. + if (len == 0 || in[0] == 0x04) { + OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR); + return nullptr; + } + return pkey_from_ec_point(group, in, len); +} + +static int marshal_ec_point(CBB *cbb, const EVP_PKEY *key, + point_conversion_form_t form) { + const EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(key); + if (ec_key == nullptr) { + return 0; + } + const EC_GROUP *group = EC_KEY_get0_group(ec_key); + const EC_POINT *pub = EC_KEY_get0_public_key(ec_key); + if (group == nullptr || pub == nullptr) { + OPENSSL_PUT_ERROR(EVP, EVP_R_MISSING_PUBLIC_KEY); + return 0; + } + return EC_POINT_point2cbb(cbb, group, pub, form, nullptr); +} + +int EVP_PKEY_marshal_ec_uncompressed_point(CBB *cbb, const EVP_PKEY *key) { + return marshal_ec_point(cbb, key, POINT_CONVERSION_UNCOMPRESSED); +} + +int EVP_PKEY_marshal_ec_compressed_point(CBB *cbb, const EVP_PKEY *key) { + return marshal_ec_point(cbb, key, POINT_CONVERSION_COMPRESSED); +} + +EVP_PKEY *EVP_PKEY_from_ec_private_scalar(const EVP_PKEY_ALG *alg, + const uint8_t *in, size_t len) { + const EC_GROUP *group = alg_to_group(alg); + if (group == nullptr) { + return nullptr; + } + UniquePtr pkey(EVP_PKEY_new()); + UniquePtr ec_key(EC_KEY_new()); + if (pkey == nullptr || ec_key == nullptr || + !EC_KEY_set_group(ec_key.get(), group) || + !EC_KEY_oct2priv(ec_key.get(), in, len)) { + return nullptr; + } + // TODO(crbug.com/42290404): `EC_KEY_oct2priv` should fill in the public key + // internally. + UniquePtr pub(EC_POINT_new(group)); + if (pub == nullptr || + !EC_POINT_mul(group, pub.get(), EC_KEY_get0_private_key(ec_key.get()), + nullptr, nullptr, nullptr) || + !EC_KEY_set_public_key(ec_key.get(), pub.get())) { + return 0; + } + + EVP_PKEY_assign_EC_KEY(pkey.get(), ec_key.release()); + return pkey.release(); +} + +int EVP_PKEY_marshal_ec_private_scalar(CBB *cbb, const EVP_PKEY *key) { + const EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(key); + if (ec_key == nullptr) { + return 0; + } + size_t len = EC_KEY_priv2oct(ec_key, nullptr, 0); + if (len == 0) { + return 0; + } + uint8_t *out; + if (!CBB_reserve(cbb, &out, len)) { + return 0; + } + len = EC_KEY_priv2oct(ec_key, out, len); + return len != 0 && CBB_did_write(cbb, len); +} + int EVP_PKEY_get_ec_curve_nid(const EVP_PKEY *pkey) { const EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(pkey); if (ec_key == nullptr) { diff --git a/third_party/boringssl/src/crypto/evp/p_ed25519.cc b/third_party/boringssl/src/crypto/evp/p_ed25519.cc index 3ca66b7cb..3cdbc1d8d 100644 --- a/third_party/boringssl/src/crypto/evp/p_ed25519.cc +++ b/third_party/boringssl/src/crypto/evp/p_ed25519.cc @@ -29,8 +29,8 @@ namespace { struct ED25519_KEY { // key is the concatenation of the private seed and public key. It is stored - // as a single 64-bit array to allow passing to |ED25519_sign|. If - // |has_private| is false, the first 32 bytes are uninitialized and the public + // as a single 64-bit array to allow passing to `ED25519_sign`. If + // `has_private` is false, the first 32 bytes are uninitialized and the public // key is in the last 32 bytes. uint8_t key[64]; bool has_private; @@ -41,13 +41,13 @@ extern const EVP_PKEY_CTX_METHOD ed25519_pkey_meth; #define ED25519_PUBLIC_KEY_OFFSET 32 -static void ed25519_free(EvpPkey *pkey) { +void ed25519_free(EvpPkey *pkey) { ED25519_KEY *key = reinterpret_cast(pkey->pkey); Delete(key); pkey->pkey = nullptr; } -static int ed25519_set_priv_raw(EvpPkey *pkey, const uint8_t *in, size_t len) { +int ed25519_set_priv_raw(EvpPkey *pkey, const uint8_t *in, size_t len) { if (len != 32) { OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR); return 0; @@ -67,7 +67,7 @@ static int ed25519_set_priv_raw(EvpPkey *pkey, const uint8_t *in, size_t len) { return 1; } -static int ed25519_set_pub_raw(EvpPkey *pkey, const uint8_t *in, size_t len) { +int ed25519_set_pub_raw(EvpPkey *pkey, const uint8_t *in, size_t len) { if (len != 32) { OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR); return 0; @@ -84,8 +84,8 @@ static int ed25519_set_pub_raw(EvpPkey *pkey, const uint8_t *in, size_t len) { return 1; } -static int ed25519_get_priv_raw(const EvpPkey *pkey, uint8_t *out, - size_t *out_len) { +int ed25519_get_priv_raw(const EvpPkey *pkey, uint8_t *out, + size_t *out_len) { const ED25519_KEY *key = reinterpret_cast(pkey->pkey); if (!key->has_private) { OPENSSL_PUT_ERROR(EVP, EVP_R_NOT_A_PRIVATE_KEY); @@ -108,8 +108,8 @@ static int ed25519_get_priv_raw(const EvpPkey *pkey, uint8_t *out, return 1; } -static int ed25519_get_pub_raw(const EvpPkey *pkey, uint8_t *out, - size_t *out_len) { +int ed25519_get_pub_raw(const EvpPkey *pkey, uint8_t *out, + size_t *out_len) { const ED25519_KEY *key = reinterpret_cast(pkey->pkey); if (out == nullptr) { *out_len = 32; @@ -126,9 +126,9 @@ static int ed25519_get_pub_raw(const EvpPkey *pkey, uint8_t *out, return 1; } -static bssl::evp_decode_result_t ed25519_pub_decode(const EVP_PKEY_ALG *alg, - EvpPkey *out, CBS *params, - CBS *key) { +bssl::evp_decode_result_t ed25519_pub_decode(const EVP_PKEY_ALG *alg, + EvpPkey *out, CBS *params, + CBS *key) { // See RFC 8410, section 4. // The parameters must be omitted. Public keys have length 32. @@ -142,7 +142,7 @@ static bssl::evp_decode_result_t ed25519_pub_decode(const EVP_PKEY_ALG *alg, : evp_decode_error; } -static int ed25519_pub_encode(CBB *out, const EvpPkey *pkey) { +int ed25519_pub_encode(CBB *out, const EvpPkey *pkey) { const ED25519_KEY *key = reinterpret_cast(pkey->pkey); // See RFC 8410, section 4. @@ -163,16 +163,16 @@ static int ed25519_pub_encode(CBB *out, const EvpPkey *pkey) { return 1; } -static bool ed25519_pub_equal(const EvpPkey *a, const EvpPkey *b) { +bool ed25519_pub_equal(const EvpPkey *a, const EvpPkey *b) { const ED25519_KEY *a_key = reinterpret_cast(a->pkey); const ED25519_KEY *b_key = reinterpret_cast(b->pkey); return OPENSSL_memcmp(a_key->key + ED25519_PUBLIC_KEY_OFFSET, b_key->key + ED25519_PUBLIC_KEY_OFFSET, 32) == 0; } -static bssl::evp_decode_result_t ed25519_priv_decode(const EVP_PKEY_ALG *alg, - EvpPkey *out, CBS *params, - CBS *key) { +bssl::evp_decode_result_t ed25519_priv_decode(const EVP_PKEY_ALG *alg, + EvpPkey *out, CBS *params, + CBS *key) { // See RFC 8410, section 7. // Parameters must be empty. The key is a 32-byte value wrapped in an extra @@ -189,7 +189,7 @@ static bssl::evp_decode_result_t ed25519_priv_decode(const EVP_PKEY_ALG *alg, : evp_decode_error; } -static int ed25519_priv_encode(CBB *out, const EvpPkey *pkey) { +int ed25519_priv_encode(CBB *out, const EvpPkey *pkey) { const ED25519_KEY *key = reinterpret_cast(pkey->pkey); if (!key->has_private) { OPENSSL_PUT_ERROR(EVP, EVP_R_NOT_A_PRIVATE_KEY); @@ -216,9 +216,9 @@ static int ed25519_priv_encode(CBB *out, const EvpPkey *pkey) { return 1; } -static bool ed25519_pub_present(const EvpPkey *) { return true; } +bool ed25519_pub_present(const EvpPkey *) { return true; } -static bool ed25519_pub_copy(EvpPkey *out, const EvpPkey *pkey) { +bool ed25519_pub_copy(EvpPkey *out, const EvpPkey *pkey) { const ED25519_KEY *pkey_ed25519 = reinterpret_cast(pkey->pkey); ED25519_KEY *public_copy = New(); @@ -233,14 +233,14 @@ static bool ed25519_pub_copy(EvpPkey *out, const EvpPkey *pkey) { return true; } -static bool ed25519_priv_present(const EvpPkey *pkey) { +bool ed25519_priv_present(const EvpPkey *pkey) { const ED25519_KEY *key = reinterpret_cast(pkey->pkey); return key->has_private; } -static int ed25519_size(const EvpPkey *pkey) { return 64; } +int ed25519_size(const EvpPkey *pkey) { return 64; } -static int ed25519_bits(const EvpPkey *pkey) { return 253; } +int ed25519_bits(const EvpPkey *pkey) { return 253; } const EVP_PKEY_ASN1_METHOD ed25519_asn1_meth = { EVP_PKEY_ED25519, @@ -273,9 +273,9 @@ const EVP_PKEY_ASN1_METHOD ed25519_asn1_meth = { }; // Ed25519 has no parameters to copy. -static int pkey_ed25519_copy(EvpPkeyCtx *dst, EvpPkeyCtx *src) { return 1; } +int pkey_ed25519_copy(EvpPkeyCtx *dst, EvpPkeyCtx *src) { return 1; } -static int pkey_ed25519_keygen(EvpPkeyCtx *ctx, EvpPkey *pkey) { +int pkey_ed25519_keygen(EvpPkeyCtx *ctx, EvpPkey *pkey) { ED25519_KEY *key = New(); if (key == nullptr) { return 0; @@ -289,9 +289,9 @@ static int pkey_ed25519_keygen(EvpPkeyCtx *ctx, EvpPkey *pkey) { return 1; } -static int pkey_ed25519_sign_message(EvpPkeyCtx *ctx, uint8_t *sig, - size_t *siglen, const uint8_t *tbs, - size_t tbslen) { +int pkey_ed25519_sign_message(EvpPkeyCtx *ctx, uint8_t *sig, + size_t *siglen, const uint8_t *tbs, + size_t tbslen) { const ED25519_KEY *key = reinterpret_cast(ctx->pkey->pkey); if (!key->has_private) { @@ -317,9 +317,9 @@ static int pkey_ed25519_sign_message(EvpPkeyCtx *ctx, uint8_t *sig, return 1; } -static int pkey_ed25519_verify_message(EvpPkeyCtx *ctx, const uint8_t *sig, - size_t siglen, const uint8_t *tbs, - size_t tbslen) { +int pkey_ed25519_verify_message(EvpPkeyCtx *ctx, const uint8_t *sig, + size_t siglen, const uint8_t *tbs, + size_t tbslen) { const ED25519_KEY *key = reinterpret_cast(ctx->pkey->pkey); if (siglen != 64 || diff --git a/third_party/boringssl/src/crypto/evp/p_hkdf.cc b/third_party/boringssl/src/crypto/evp/p_hkdf.cc index 14cb76cbf..3494d5e58 100644 --- a/third_party/boringssl/src/crypto/evp/p_hkdf.cc +++ b/third_party/boringssl/src/crypto/evp/p_hkdf.cc @@ -37,12 +37,12 @@ struct HKDF_PKEY_CTX { Vector info; }; -static int pkey_hkdf_init(EvpPkeyCtx *ctx, const EVP_PKEY_ALG *) { +int pkey_hkdf_init(EvpPkeyCtx *ctx, const EVP_PKEY_ALG *) { ctx->data = New(); return 1; } -static int pkey_hkdf_copy(EvpPkeyCtx *dst, EvpPkeyCtx *src) { +int pkey_hkdf_copy(EvpPkeyCtx *dst, EvpPkeyCtx *src) { if (!pkey_hkdf_init(dst, nullptr)) { return 0; } @@ -62,11 +62,11 @@ static int pkey_hkdf_copy(EvpPkeyCtx *dst, EvpPkeyCtx *src) { return 1; } -static void pkey_hkdf_cleanup(EvpPkeyCtx *ctx) { +void pkey_hkdf_cleanup(EvpPkeyCtx *ctx) { Delete(reinterpret_cast(ctx->data)); } -static int pkey_hkdf_derive(EvpPkeyCtx *ctx, uint8_t *out, size_t *out_len) { +int pkey_hkdf_derive(EvpPkeyCtx *ctx, uint8_t *out, size_t *out_len) { HKDF_PKEY_CTX *hctx = reinterpret_cast(ctx->data); if (hctx->md == nullptr) { OPENSSL_PUT_ERROR(EVP, EVP_R_MISSING_PARAMETERS); @@ -81,7 +81,7 @@ static int pkey_hkdf_derive(EvpPkeyCtx *ctx, uint8_t *out, size_t *out_len) { if (hctx->mode == EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY) { *out_len = EVP_MD_size(hctx->md); } - // HKDF-Expand is variable-length and returns |*out_len| bytes. "Output" the + // HKDF-Expand is variable-length and returns `*out_len` bytes. "Output" the // input length by leaving it alone. return 1; } @@ -110,7 +110,7 @@ static int pkey_hkdf_derive(EvpPkeyCtx *ctx, uint8_t *out, size_t *out_len) { return 0; } -static int pkey_hkdf_ctrl(EvpPkeyCtx *ctx, int type, int p1, void *p2) { +int pkey_hkdf_ctrl(EvpPkeyCtx *ctx, int type, int p1, void *p2) { HKDF_PKEY_CTX *hctx = reinterpret_cast(ctx->data); switch (type) { case EVP_PKEY_CTRL_HKDF_MODE: @@ -135,7 +135,7 @@ static int pkey_hkdf_ctrl(EvpPkeyCtx *ctx, int type, int p1, void *p2) { } case EVP_PKEY_CTRL_HKDF_INFO: { const auto *info = reinterpret_cast *>(p2); - // |EVP_PKEY_CTX_add1_hkdf_info| appends to the info string, rather than + // `EVP_PKEY_CTX_add1_hkdf_info` appends to the info string, rather than // replacing it. return hctx->info.Append(*info); } diff --git a/third_party/boringssl/src/crypto/evp/p_keccak.cc b/third_party/boringssl/src/crypto/evp/p_keccak.cc new file mode 100644 index 000000000..e9e7b03c4 --- /dev/null +++ b/third_party/boringssl/src/crypto/evp/p_keccak.cc @@ -0,0 +1,118 @@ +// Copyright 2026 The BoringSSL Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + + +#include + +#include "../fipsmodule/digest/internal.h" // IWYU pragma: keep +#include "../fipsmodule/keccak/internal.h" + + +BSSL_NAMESPACE_BEGIN +namespace { +template +void keccak_init(EVP_MD_CTX *ctx) { + BORINGSSL_keccak_init(reinterpret_cast(ctx->md_data), + Config); +} + +void keccak_update(EVP_MD_CTX *ctx, const void *data, size_t count) { + BORINGSSL_keccak_absorb(reinterpret_cast(ctx->md_data), + static_cast(data), count); +} + +template +void keccak_final(EVP_MD_CTX *ctx, uint8_t *out) { + BORINGSSL_keccak_squeeze( + reinterpret_cast(ctx->md_data), + static_cast(out), Size); +} + +void keccak_squeeze(EVP_MD_CTX *ctx, uint8_t *out, size_t len) { + BORINGSSL_keccak_squeeze( + reinterpret_cast(ctx->md_data), + static_cast(out), len); +} + +constexpr EVP_MD evp_md_sha3_256 = { + /*type=*/NID_undef, + /*md_size=*/SHA3_256_DIGEST_LENGTH, + /*flags=*/0, + /*init=*/keccak_init, + /*update=*/keccak_update, + /*final=*/keccak_final, + /*squeeze=*/nullptr, + /*block_size=*/SHA3_256_CBLOCK, + /*ctx_size=*/sizeof(BORINGSSL_keccak_st), +}; + +constexpr EVP_MD evp_md_sha3_384 = { + /*type=*/NID_undef, + /*md_size=*/SHA3_384_DIGEST_LENGTH, + /*flags=*/0, + /*init=*/keccak_init, + /*update=*/keccak_update, + /*final=*/keccak_final, + /*squeeze=*/nullptr, + /*block_size=*/SHA3_384_CBLOCK, + /*ctx_size=*/sizeof(BORINGSSL_keccak_st), +}; + +constexpr EVP_MD evp_md_sha3_512 = { + /*type=*/NID_undef, + /*md_size=*/SHA3_512_DIGEST_LENGTH, + /*flags=*/0, + /*init=*/keccak_init, + /*update=*/keccak_update, + /*final=*/keccak_final, + /*squeeze=*/nullptr, + /*block_size=*/SHA3_512_CBLOCK, + /*ctx_size=*/sizeof(BORINGSSL_keccak_st), +}; + +constexpr EVP_MD evp_md_shake256 = { + /*type=*/NID_undef, + /*md_size=*/32, + /*flags=*/0, + /*init=*/keccak_init, + /*update=*/keccak_update, + /*final=*/keccak_final<32>, + /*squeeze=*/keccak_squeeze, + /*block_size=*/SHAKE256_CBLOCK, + /*ctx_size=*/sizeof(BORINGSSL_keccak_st), +}; + +constexpr EVP_MD evp_md_turboshake256 = { + /*type=*/NID_undef, + /*md_size=*/32, + /*flags=*/0, + /*init=*/keccak_init, + /*update=*/keccak_update, + /*final=*/keccak_final<32>, + /*squeeze=*/keccak_squeeze, + /*block_size=*/SHAKE256_CBLOCK, + /*ctx_size=*/sizeof(BORINGSSL_keccak_st), +}; + +static_assert(sizeof(BORINGSSL_keccak_st) <= EVP_MAX_MD_DATA_SIZE); +static_assert(SHA3_256_CBLOCK <= EVP_MAX_MD_BLOCK_SIZE); +static_assert(SHAKE256_CBLOCK <= EVP_MAX_MD_BLOCK_SIZE); +} // namespace +BSSL_NAMESPACE_END + +const EVP_MD *EVP_sha3_256() { return &bssl::evp_md_sha3_256; } +const EVP_MD *EVP_sha3_384() { return &bssl::evp_md_sha3_384; } +const EVP_MD *EVP_sha3_512() { return &bssl::evp_md_sha3_512; } +const EVP_MD *EVP_shake256() { return &bssl::evp_md_shake256; } +const EVP_MD *EVP_turboshake256() { return &bssl::evp_md_turboshake256; } diff --git a/third_party/boringssl/src/crypto/evp/p_mldsa.cc b/third_party/boringssl/src/crypto/evp/p_mldsa.cc index 3e4c66271..6fe356444 100644 --- a/third_party/boringssl/src/crypto/evp/p_mldsa.cc +++ b/third_party/boringssl/src/crypto/evp/p_mldsa.cc @@ -69,13 +69,13 @@ MAKE_MLDSA_TRAITS(44) MAKE_MLDSA_TRAITS(65) MAKE_MLDSA_TRAITS(87) -// For each ML-DSA variant, the |EvpPkey| must hold a public or private key. +// For each ML-DSA variant, the `EvpPkey` must hold a public or private key. // EVP uses the same type for public and private keys, so the representation // must support both. The private key type contains the public key struct in it, // so we use a pointer to either a PrivateKeyData or // PublicKeyData, with a common base class to dispatch between them. // -// TODO(crbug.com/404286922): In C++20, we need fewer |typename|s in front of +// TODO(crbug.com/404286922): In C++20, we need fewer `typename`s in front of // dependent type names. template @@ -252,8 +252,7 @@ struct MLDSAImplementation { static evp_decode_result_t DecodePublic(const EVP_PKEY_ALG *alg, EvpPkey *out, CBS *params, CBS *key) { - // The parameters must be omitted. See - // draft-ietf-lamps-dilithium-certificates-13, Section 2. + // The parameters must be omitted. See RFC 9881, Section 2. if (CBS_len(params) != 0) { OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR); return evp_decode_error; @@ -264,7 +263,7 @@ struct MLDSAImplementation { static int EncodePublic(CBB *out, const EvpPkey *pkey) { const auto *pub = GetKeyData(pkey)->GetPublicKey(); - // See draft-ietf-lamps-dilithium-certificates-13, Sections 2 and 4. + // See RFC 9881, Sections 2 and 4. CBB spki, algorithm, key_bitstring; if (!CBB_add_asn1(out, &spki, CBS_ASN1_SEQUENCE) || !CBB_add_asn1(&spki, &algorithm, CBS_ASN1_SEQUENCE) || @@ -302,17 +301,16 @@ struct MLDSAImplementation { static evp_decode_result_t DecodePrivate(const EVP_PKEY_ALG *alg, EvpPkey *out, CBS *params, CBS *key) { - // The parameters must be omitted. See - // draft-ietf-lamps-dilithium-certificates-13, Section 2. + // The parameters must be omitted. See RFC 9881, Section 2. if (CBS_len(params) != 0) { OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR); return evp_decode_error; } - // See draft-ietf-lamps-dilithium-certificates-13, Section 6. Three - // different encodings were specified, adding complexity to the question of - // whether a private key is valid. We only implement the "seed" - // representation. Give this case a different error for easier diagnostics. + // See RFC 9881, Section 6. Three different encodings were specified, adding + // complexity to the question of whether a private key is valid. We only + // implement the "seed" representation. Give this case a different error for + // easier diagnostics. // // The "expandedKey" representation was a last-minute accommodation for // legacy hardware, which should be updated to use seeds. Supporting it @@ -345,8 +343,7 @@ struct MLDSAImplementation { OPENSSL_PUT_ERROR(EVP, EVP_R_NOT_A_PRIVATE_KEY); return 0; } - // See draft-ietf-lamps-dilithium-certificates-13, Sections 2 and 6. We - // encode only the seed representation. + // See RFC 9881, Sections 2 and 6. We encode only the seed representation. CBB pkcs8, algorithm, private_key; if (!CBB_add_asn1(out, &pkcs8, CBS_ASN1_SEQUENCE) || !CBB_add_asn1_uint64(&pkcs8, 0 /* version */) || diff --git a/third_party/boringssl/src/crypto/evp/p_mlkem.cc b/third_party/boringssl/src/crypto/evp/p_mlkem.cc index 2687ba84e..dd8710c16 100644 --- a/third_party/boringssl/src/crypto/evp/p_mlkem.cc +++ b/third_party/boringssl/src/crypto/evp/p_mlkem.cc @@ -56,6 +56,8 @@ constexpr uint8_t kMLKEM1024OID[] = {OBJ_ENC_ML_KEM_1024}; static constexpr auto PublicOfPrivate = &BCM_mlkem##x##_public_of_private; \ static constexpr auto PublicKeysEqual = &BCM_mlkem##x##_public_keys_equal; \ static constexpr auto Encap = &MLKEM##x##_encap; \ + static constexpr auto EncapExternalEntropy = \ + &BCM_mlkem##x##_encap_external_entropy; \ static constexpr auto Decap = &MLKEM##x##_decap; \ static constexpr auto MarshalPublicKey = &MLKEM##x##_marshal_public_key; \ static constexpr auto ParsePublicKey = &MLKEM##x##_parse_public_key; \ @@ -354,42 +356,67 @@ struct MLKEMImplementation { return 1; } - static int KemEncap(uint8_t *out_ciphertext, size_t ciphertext_len, - uint8_t *out_secret, size_t secret_len, - const EVP_PKEY *peer_key) { + static int KemEncap(const EVP_KEM *kem, Span out_ciphertext, + Span out_secret, const EVP_PKEY *peer_key) { const auto *peer_pubkey = GetKeyData(FromOpaque(peer_key))->GetPublicKey(); - if (ciphertext_len != Traits::kCiphertextBytes) { + if (out_ciphertext.size() != Traits::kCiphertextBytes) { OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_CIPHERTEXT_LENGTH); return 0; } - if (secret_len != MLKEM_SHARED_SECRET_BYTES) { + if (out_secret.size() != MLKEM_SHARED_SECRET_BYTES) { OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_SECRET_LENGTH); return 0; } - Traits::Encap(out_ciphertext, out_secret, peer_pubkey); + Traits::Encap(out_ciphertext.data(), out_secret.data(), peer_pubkey); return 1; } - static int KemDecap(uint8_t *out_secret, size_t secret_len, - const uint8_t *ciphertext, size_t ciphertext_len, - const EVP_PKEY *key) { + static int KemEncapExternalEntropy(const EVP_KEM *kem, + Span out_ciphertext, + Span out_secret, + const EVP_PKEY *peer_key, + Span entropy) { + const auto *peer_pubkey = GetKeyData(FromOpaque(peer_key))->GetPublicKey(); + if (out_ciphertext.size() != Traits::kCiphertextBytes) { + OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_CIPHERTEXT_LENGTH); + return 0; + } + if (out_secret.size() != MLKEM_SHARED_SECRET_BYTES) { + OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_SECRET_LENGTH); + return 0; + } + if (entropy.size() != BCM_MLKEM_ENCAP_ENTROPY) { + OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_ENTROPY_LENGTH); + return 0; + } + Traits::EncapExternalEntropy(out_ciphertext.data(), out_secret.data(), + peer_pubkey, entropy.data()); + return 1; + } + + static int KemDecap(const EVP_KEM *kem, Span out_secret, + Span ciphertext, const EVP_PKEY *key) { const auto *priv = GetKeyData(FromOpaque(key))->AsPrivateKeyData(); if (priv == nullptr) { OPENSSL_PUT_ERROR(EVP, EVP_R_NOT_A_PRIVATE_KEY); return 0; } - if (secret_len != MLKEM_SHARED_SECRET_BYTES) { + if (out_secret.size() != MLKEM_SHARED_SECRET_BYTES) { OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_SECRET_LENGTH); return 0; } - return Traits::Decap(out_secret, ciphertext, ciphertext_len, &priv->priv); + return Traits::Decap(out_secret.data(), ciphertext.data(), + ciphertext.size(), &priv->priv); } static constexpr EVP_KEM evp_kem = { /*pkey_id=*/Traits::kType, /*ciphertext_len=*/Traits::kCiphertextBytes, /*secret_len=*/MLKEM_SHARED_SECRET_BYTES, + /*entropy_len=*/BCM_MLKEM_ENCAP_ENTROPY, + /*check_key=*/nullptr, &KemEncap, + &KemEncapExternalEntropy, &KemDecap, }; diff --git a/third_party/boringssl/src/crypto/evp/p_rsa.cc b/third_party/boringssl/src/crypto/evp/p_rsa.cc index 900385388..9097b7a7f 100644 --- a/third_party/boringssl/src/crypto/evp/p_rsa.cc +++ b/third_party/boringssl/src/crypto/evp/p_rsa.cc @@ -46,7 +46,7 @@ extern const EVP_PKEY_ASN1_METHOD rsa_pss_asn1_meth; extern const EVP_PKEY_CTX_METHOD rsa_pkey_meth; extern const EVP_PKEY_CTX_METHOD rsa_pss_pkey_meth; -static int rsa_pub_encode(CBB *out, const EvpPkey *key) { +int rsa_pub_encode(CBB *out, const EvpPkey *key) { // See RFC 3279, section 2.3.1. const RSA *rsa = reinterpret_cast(key->pkey); CBB spki, algorithm, null, key_bitstring; @@ -66,9 +66,9 @@ static int rsa_pub_encode(CBB *out, const EvpPkey *key) { return 1; } -static bssl::evp_decode_result_t rsa_pub_decode(const EVP_PKEY_ALG *alg, - EvpPkey *out, CBS *params, - CBS *key) { +bssl::evp_decode_result_t rsa_pub_decode(const EVP_PKEY_ALG *alg, + EvpPkey *out, CBS *params, + CBS *key) { // See RFC 3279, section 2.3.1. // The parameters must be NULL. @@ -89,8 +89,8 @@ static bssl::evp_decode_result_t rsa_pub_decode(const EVP_PKEY_ALG *alg, return evp_decode_ok; } -static bool rsa_pub_equal(const EvpPkey *a, const EvpPkey *b) { - // We currently assume that all |EVP_PKEY_RSA_PSS| keys have the same +bool rsa_pub_equal(const EvpPkey *a, const EvpPkey *b) { + // We currently assume that all `EVP_PKEY_RSA_PSS` keys have the same // parameters, so this vacuously compares parameters. If we ever support // multiple PSS parameter sets, we probably should compare them too. Note, // however, that OpenSSL does not compare parameters here. @@ -100,7 +100,7 @@ static bool rsa_pub_equal(const EvpPkey *a, const EvpPkey *b) { BN_cmp(RSA_get0_e(b_rsa), RSA_get0_e(a_rsa)) == 0; } -static bool rsa_pub_present(const EvpPkey *pk) { +bool rsa_pub_present(const EvpPkey *pk) { const RSA *pk_rsa = reinterpret_cast(pk->pkey); // An RSA public key should always have n and e. It's possible for a (private) // key to have n and d, but not e, so we must explicitly check for the @@ -108,7 +108,7 @@ static bool rsa_pub_present(const EvpPkey *pk) { return RSA_get0_n(pk_rsa) != nullptr && RSA_get0_e(pk_rsa) != nullptr; } -static bool rsa_pub_copy(EvpPkey *out, const EvpPkey *pkey) { +bool rsa_pub_copy(EvpPkey *out, const EvpPkey *pkey) { const RSAImpl *pk_rsa = reinterpret_cast(pkey->pkey); const BIGNUM *pk_n = RSA_get0_n(pk_rsa); const BIGNUM *pk_e = RSA_get0_e(pk_rsa); @@ -126,7 +126,7 @@ static bool rsa_pub_copy(EvpPkey *out, const EvpPkey *pkey) { return true; } -static int rsa_priv_encode(CBB *out, const EvpPkey *key) { +int rsa_priv_encode(CBB *out, const EvpPkey *key) { const RSA *rsa = reinterpret_cast(key->pkey); CBB pkcs8, algorithm, null, private_key; if (!CBB_add_asn1(out, &pkcs8, CBS_ASN1_SEQUENCE) || @@ -145,9 +145,9 @@ static int rsa_priv_encode(CBB *out, const EvpPkey *key) { return 1; } -static bssl::evp_decode_result_t rsa_priv_decode(const EVP_PKEY_ALG *alg, - EvpPkey *out, CBS *params, - CBS *key) { +bssl::evp_decode_result_t rsa_priv_decode(const EVP_PKEY_ALG *alg, + EvpPkey *out, CBS *params, + CBS *key) { // Per RFC 8017, A.1, the parameters have type NULL. CBS null; if (!CBS_get_asn1(params, &null, CBS_ASN1_NULL) || CBS_len(&null) != 0 || @@ -166,12 +166,12 @@ static bssl::evp_decode_result_t rsa_priv_decode(const EVP_PKEY_ALG *alg, return evp_decode_ok; } -static bool rsa_priv_present(const EvpPkey *pk) { +bool rsa_priv_present(const EvpPkey *pk) { const RSA *pk_rsa = reinterpret_cast(pk->pkey); return RSA_get0_n(pk_rsa) != nullptr && RSA_get0_d(pk_rsa) != nullptr; } -static bssl::evp_decode_result_t rsa_decode_pss_params( +bssl::evp_decode_result_t rsa_decode_pss_params( rsa_pss_params_t expected, CBS *params) { if (CBS_len(params) == 0) { return evp_decode_unsupported; @@ -186,7 +186,7 @@ static bssl::evp_decode_result_t rsa_decode_pss_params( return pss_params == expected ? evp_decode_ok : evp_decode_unsupported; } -static int rsa_pub_encode_pss(CBB *out, const EvpPkey *key) { +int rsa_pub_encode_pss(CBB *out, const EvpPkey *key) { const RSAImpl *rsa = reinterpret_cast(key->pkey); CBB spki, algorithm, key_bitstring; if (!CBB_add_asn1(out, &spki, CBS_ASN1_SEQUENCE) || @@ -205,28 +205,34 @@ static int rsa_pub_encode_pss(CBB *out, const EvpPkey *key) { return 1; } -static bssl::evp_decode_result_t rsa_pub_decode_pss(const EVP_PKEY_ALG *alg, - EvpPkey *out, CBS *params, - CBS *key) { +void evp_pkey_set0_pss(EvpPkey *out, const EVP_PKEY_ALG *alg, + UniquePtr rsa) { + BSSL_CHECK(alg->pkey_method->pkey_id == EVP_PKEY_RSA_PSS); + const auto *alg_pss = static_cast(alg); + FromOpaque(rsa.get())->pss_params = alg_pss->pss_params; + evp_pkey_set0(out, alg->method, rsa.release()); +} + +bssl::evp_decode_result_t rsa_pub_decode_pss(const EVP_PKEY_ALG *alg, + EvpPkey *out, CBS *params, + CBS *key) { const auto *alg_pss = static_cast(alg); evp_decode_result_t ret = rsa_decode_pss_params(alg_pss->pss_params, params); if (ret != evp_decode_ok) { return ret; } - UniquePtr rsa( - FromOpaque(RSA_public_key_from_bytes(CBS_data(key), CBS_len(key)))); + UniquePtr rsa(RSA_public_key_from_bytes(CBS_data(key), CBS_len(key))); if (rsa == nullptr) { OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR); return evp_decode_error; } - rsa->pss_params = alg_pss->pss_params; - evp_pkey_set0(out, &rsa_pss_asn1_meth, rsa.release()); + evp_pkey_set0_pss(out, alg, std::move(rsa)); return evp_decode_ok; } -static int rsa_priv_encode_pss(CBB *out, const EvpPkey *key) { +int rsa_priv_encode_pss(CBB *out, const EvpPkey *key) { const RSAImpl *rsa = reinterpret_cast(key->pkey); CBB pkcs8, algorithm, private_key; if (!CBB_add_asn1(out, &pkcs8, CBS_ASN1_SEQUENCE) || @@ -245,53 +251,51 @@ static int rsa_priv_encode_pss(CBB *out, const EvpPkey *key) { return 1; } -static bssl::evp_decode_result_t rsa_priv_decode_pss(const EVP_PKEY_ALG *alg, - EvpPkey *out, CBS *params, - CBS *key) { +bssl::evp_decode_result_t rsa_priv_decode_pss(const EVP_PKEY_ALG *alg, + EvpPkey *out, CBS *params, + CBS *key) { const auto *alg_pss = static_cast(alg); evp_decode_result_t ret = rsa_decode_pss_params(alg_pss->pss_params, params); if (ret != evp_decode_ok) { return ret; } - UniquePtr rsa( - FromOpaque(RSA_private_key_from_bytes(CBS_data(key), CBS_len(key)))); + UniquePtr rsa(RSA_private_key_from_bytes(CBS_data(key), CBS_len(key))); if (rsa == nullptr) { OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR); return evp_decode_error; } - rsa->pss_params = alg_pss->pss_params; - evp_pkey_set0(out, &rsa_pss_asn1_meth, rsa.release()); + evp_pkey_set0_pss(out, alg, std::move(rsa)); return evp_decode_ok; } -static int rsa_opaque(const EvpPkey *pkey) { +int rsa_opaque(const EvpPkey *pkey) { const RSA *rsa = reinterpret_cast(pkey->pkey); return RSA_is_opaque(rsa); } -static int int_rsa_size(const EvpPkey *pkey) { +int int_rsa_size(const EvpPkey *pkey) { const RSA *rsa = reinterpret_cast(pkey->pkey); return RSA_size(rsa); } -static int rsa_bits(const EvpPkey *pkey) { +int rsa_bits(const EvpPkey *pkey) { const RSA *rsa = reinterpret_cast(pkey->pkey); return RSA_bits(rsa); } -static void int_rsa_free(EvpPkey *pkey) { +void int_rsa_free(EvpPkey *pkey) { RSA_free(reinterpret_cast(pkey->pkey)); pkey->pkey = nullptr; } -static int rsa_pss_params_missing(const EvpPkey *pkey) { +int rsa_pss_params_missing(const EvpPkey *pkey) { const RSA *rsa = reinterpret_cast(pkey->pkey); return rsa == nullptr || FromOpaque(rsa)->pss_params == rsa_pss_none; } -static int rsa_pss_params_copy(EvpPkey *to, const EvpPkey *from) { +int rsa_pss_params_copy(EvpPkey *to, const EvpPkey *from) { const RSA *from_key = reinterpret_cast(from->pkey); if (from_key == nullptr) { OPENSSL_PUT_ERROR(EVP, EVP_R_NO_KEY_SET); @@ -312,7 +316,7 @@ static int rsa_pss_params_copy(EvpPkey *to, const EvpPkey *from) { return 1; } -static bool rsa_pss_params_equal(const EvpPkey *a, const EvpPkey *b) { +bool rsa_pss_params_equal(const EvpPkey *a, const EvpPkey *b) { const RSA *a_rsa = reinterpret_cast(a->pkey); const RSA *b_rsa = reinterpret_cast(b->pkey); if (a_rsa == nullptr || b_rsa == nullptr) { @@ -418,17 +422,17 @@ struct RSA_PKEY_CTX { // PSS salt length int saltlen = RSA_PSS_SALTLEN_DIGEST; // restrict_pss_params, if true, indicates that the PSS signing/verifying - // parameters are restricted by the key's parameters. |md| and |mgf1md| may - // not change, and |saltlen| must be at least |md|'s hash length. + // parameters are restricted by the key's parameters. `md` and `mgf1md` may + // not change, and `saltlen` must be at least `md`'s hash length. bool restrict_pss_params = false; Array oaep_label; }; -static bool is_pss_only(const EvpPkeyCtx *ctx) { +bool is_pss_only(const EvpPkeyCtx *ctx) { return ctx->pmeth->pkey_id == EVP_PKEY_RSA_PSS; } -static int pkey_rsa_init(EvpPkeyCtx *ctx, const EVP_PKEY_ALG *alg) { +int pkey_rsa_init(EvpPkeyCtx *ctx, const EVP_PKEY_ALG *alg) { RSA_PKEY_CTX *rctx = New(); if (!rctx) { return 0; @@ -458,7 +462,7 @@ static int pkey_rsa_init(EvpPkeyCtx *ctx, const EVP_PKEY_ALG *alg) { return 1; } -static int pkey_rsa_copy(EvpPkeyCtx *dst, EvpPkeyCtx *src) { +int pkey_rsa_copy(EvpPkeyCtx *dst, EvpPkeyCtx *src) { RSA_PKEY_CTX *dctx, *sctx; if (!pkey_rsa_init(dst, nullptr)) { return 0; @@ -485,12 +489,12 @@ static int pkey_rsa_copy(EvpPkeyCtx *dst, EvpPkeyCtx *src) { return 1; } -static void pkey_rsa_cleanup(EvpPkeyCtx *ctx) { +void pkey_rsa_cleanup(EvpPkeyCtx *ctx) { Delete(reinterpret_cast(ctx->data)); } -static int pkey_rsa_sign(EvpPkeyCtx *ctx, uint8_t *sig, size_t *siglen, - const uint8_t *tbs, size_t tbslen) { +int pkey_rsa_sign(EvpPkeyCtx *ctx, uint8_t *sig, size_t *siglen, + const uint8_t *tbs, size_t tbslen) { RSA_PKEY_CTX *rctx = reinterpret_cast(ctx->data); RSA *rsa = reinterpret_cast(ctx->pkey->pkey); const size_t key_len = EVP_PKEY_size(ctx->pkey.get()); @@ -527,8 +531,8 @@ static int pkey_rsa_sign(EvpPkeyCtx *ctx, uint8_t *sig, size_t *siglen, return RSA_sign_raw(rsa, siglen, sig, *siglen, tbs, tbslen, rctx->pad_mode); } -static int pkey_rsa_verify(EvpPkeyCtx *ctx, const uint8_t *sig, size_t siglen, - const uint8_t *tbs, size_t tbslen) { +int pkey_rsa_verify(EvpPkeyCtx *ctx, const uint8_t *sig, size_t siglen, + const uint8_t *tbs, size_t tbslen) { RSA_PKEY_CTX *rctx = reinterpret_cast(ctx->data); RSA *rsa = reinterpret_cast(ctx->pkey->pkey); @@ -562,9 +566,9 @@ static int pkey_rsa_verify(EvpPkeyCtx *ctx, const uint8_t *sig, size_t siglen, return 1; } -static int pkey_rsa_verify_recover(EvpPkeyCtx *ctx, uint8_t *out, - size_t *out_len, const uint8_t *sig, - size_t sig_len) { +int pkey_rsa_verify_recover(EvpPkeyCtx *ctx, uint8_t *out, + size_t *out_len, const uint8_t *sig, + size_t sig_len) { RSA_PKEY_CTX *rctx = reinterpret_cast(ctx->data); RSA *rsa = reinterpret_cast(ctx->pkey->pkey); const size_t key_len = EVP_PKEY_size(ctx->pkey.get()); @@ -622,8 +626,8 @@ static int pkey_rsa_verify_recover(EvpPkeyCtx *ctx, uint8_t *out, return 1; } -static int pkey_rsa_encrypt(EvpPkeyCtx *ctx, uint8_t *out, size_t *outlen, - const uint8_t *in, size_t inlen) { +int pkey_rsa_encrypt(EvpPkeyCtx *ctx, uint8_t *out, size_t *outlen, + const uint8_t *in, size_t inlen) { RSA_PKEY_CTX *rctx = reinterpret_cast(ctx->data); RSA *rsa = reinterpret_cast(ctx->pkey->pkey); const size_t key_len = EVP_PKEY_size(ctx->pkey.get()); @@ -654,8 +658,8 @@ static int pkey_rsa_encrypt(EvpPkeyCtx *ctx, uint8_t *out, size_t *outlen, return RSA_encrypt(rsa, outlen, out, *outlen, in, inlen, rctx->pad_mode); } -static int pkey_rsa_decrypt(EvpPkeyCtx *ctx, uint8_t *out, size_t *outlen, - const uint8_t *in, size_t inlen) { +int pkey_rsa_decrypt(EvpPkeyCtx *ctx, uint8_t *out, size_t *outlen, + const uint8_t *in, size_t inlen) { RSA_PKEY_CTX *rctx = reinterpret_cast(ctx->data); RSA *rsa = reinterpret_cast(ctx->pkey->pkey); const size_t key_len = EVP_PKEY_size(ctx->pkey.get()); @@ -688,7 +692,7 @@ static int pkey_rsa_decrypt(EvpPkeyCtx *ctx, uint8_t *out, size_t *outlen, return RSA_decrypt(rsa, outlen, out, key_len, in, inlen, rctx->pad_mode); } -static int check_padding_md(const EVP_MD *md, int padding) { +int check_padding_md(const EVP_MD *md, int padding) { if (!md) { return 1; } @@ -701,7 +705,7 @@ static int check_padding_md(const EVP_MD *md, int padding) { return 1; } -static int is_known_padding(int padding_mode) { +int is_known_padding(int padding_mode) { switch (padding_mode) { case RSA_PKCS1_PADDING: case RSA_NO_PADDING: @@ -713,7 +717,7 @@ static int is_known_padding(int padding_mode) { } } -static int pkey_rsa_ctrl(EvpPkeyCtx *ctx, int type, int p1, void *p2) { +int pkey_rsa_ctrl(EvpPkeyCtx *ctx, int type, int p1, void *p2) { RSA_PKEY_CTX *rctx = reinterpret_cast(ctx->data); switch (type) { case EVP_PKEY_CTRL_RSA_PADDING: @@ -755,7 +759,7 @@ static int pkey_rsa_ctrl(EvpPkeyCtx *ctx, int type, int p1, void *p2) { return 0; } // All our PSS restrictions accept saltlen == hashlen, so allow - // |RSA_PSS_SALTLEN_DIGEST|. Reject |RSA_PSS_SALTLEN_AUTO| for + // `RSA_PSS_SALTLEN_DIGEST`. Reject `RSA_PSS_SALTLEN_AUTO` for // simplicity. if (rctx->restrict_pss_params && p1 != RSA_PSS_SALTLEN_DIGEST) { OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_PSS_SALTLEN); @@ -845,8 +849,8 @@ static int pkey_rsa_ctrl(EvpPkeyCtx *ctx, int type, int p1, void *p2) { OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_PADDING_MODE); return 0; } - // |EVP_PKEY_CTRL_RSA_OAEP_LABEL| takes ownership of |label|'s underlying - // buffer (via |Reset|), but only on success. + // `EVP_PKEY_CTRL_RSA_OAEP_LABEL` takes ownership of `label`'s underlying + // buffer (via `Reset`), but only on success. auto *label = reinterpret_cast *>(p2); rctx->oaep_label.Reset(label->data(), label->size()); return 1; @@ -866,7 +870,7 @@ static int pkey_rsa_ctrl(EvpPkeyCtx *ctx, int type, int p1, void *p2) { } } -static int pkey_rsa_keygen(EvpPkeyCtx *ctx, EvpPkey *pkey) { +int pkey_rsa_keygen(EvpPkeyCtx *ctx, EvpPkey *pkey) { RSA_PKEY_CTX *rctx = reinterpret_cast(ctx->data); if (!rctx->pub_exp) { rctx->pub_exp.reset(BN_new()); @@ -913,11 +917,11 @@ const EVP_PKEY_CTX_METHOD rsa_pss_pkey_meth = { pkey_rsa_init, pkey_rsa_copy, pkey_rsa_cleanup, - // In OpenSSL, |EVP_PKEY_RSA_PSS| supports key generation and fills in PSS + // In OpenSSL, `EVP_PKEY_RSA_PSS` supports key generation and fills in PSS // parameters based on a separate set of keygen-targetted setters: - // |EVP_PKEY_CTX_set_rsa_pss_keygen_saltlen|, - // |EVP_PKEY_CTX_set_rsa_pss_keygen_mgf1_md|, and - // |EVP_PKEY_CTX_rsa_pss_key_digest|. We do not currently implement this + // `EVP_PKEY_CTX_set_rsa_pss_keygen_saltlen`, + // `EVP_PKEY_CTX_set_rsa_pss_keygen_mgf1_md`, and + // `EVP_PKEY_CTX_rsa_pss_key_digest`. We do not currently implement this // because we only support one parameter set. /*keygen=*/nullptr, pkey_rsa_sign, @@ -974,12 +978,72 @@ EVP_PKEY *EVP_RSA_gen(unsigned bits) { return pkey; } -int EVP_PKEY_set1_RSA(EVP_PKEY *pkey, RSA *key) { - if (EVP_PKEY_assign_RSA(pkey, key)) { - RSA_up_ref(key); - return 1; +EVP_PKEY *EVP_PKEY_from_rsa_public_key(const EVP_PKEY_ALG *alg, + const uint8_t *in, size_t len) { + if (alg->pkey_method->pkey_id != EVP_PKEY_RSA && + alg->pkey_method->pkey_id != EVP_PKEY_RSA_PSS) { + OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_ALGORITHM); + return nullptr; } - return 0; + UniquePtr rsa(RSA_public_key_from_bytes(in, len)); + if (rsa == nullptr) { + return nullptr; + } + UniquePtr ret(EVP_PKEY_new()); + if (ret == nullptr) { + return nullptr; + } + // Use the PSS-specific setter if needed, to fill in `rsa->pss_params`. + if (alg->pkey_method->pkey_id == EVP_PKEY_RSA) { + evp_pkey_set0(FromOpaque(ret.get()), alg->method, rsa.release()); + } else { + evp_pkey_set0_pss(FromOpaque(ret.get()), alg, std::move(rsa)); + } + return ret.release(); +} + +EVP_PKEY *EVP_PKEY_from_rsa_private_key(const EVP_PKEY_ALG *alg, + const uint8_t *in, size_t len) { + if (alg->pkey_method->pkey_id != EVP_PKEY_RSA && + alg->pkey_method->pkey_id != EVP_PKEY_RSA_PSS) { + OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_ALGORITHM); + return nullptr; + } + UniquePtr rsa(RSA_private_key_from_bytes(in, len)); + if (rsa == nullptr) { + return nullptr; + } + UniquePtr ret(EVP_PKEY_new()); + if (ret == nullptr) { + return nullptr; + } + // Use the PSS-specific setter if needed, to fill in `rsa->pss_params`. + if (alg->pkey_method->pkey_id == EVP_PKEY_RSA) { + evp_pkey_set0(FromOpaque(ret.get()), alg->method, rsa.release()); + } else { + evp_pkey_set0_pss(FromOpaque(ret.get()), alg, std::move(rsa)); + } + return ret.release(); +} + +int EVP_PKEY_marshal_rsa_public_key(CBB *cbb, const EVP_PKEY *key) { + const RSA *rsa = EVP_PKEY_get0_RSA(key); + if (rsa == nullptr) { + return 0; + } + return RSA_marshal_public_key(cbb, rsa); +} + +int EVP_PKEY_marshal_rsa_private_key(CBB *cbb, const EVP_PKEY *key) { + const RSA *rsa = EVP_PKEY_get0_RSA(key); + if (rsa == nullptr) { + return 0; + } + return RSA_marshal_private_key(cbb, rsa); +} + +int EVP_PKEY_set1_RSA(EVP_PKEY *pkey, const RSA *key) { + return EVP_PKEY_assign_RSA(pkey, RSA_dup_ref(key)); } int EVP_PKEY_assign_RSA(EVP_PKEY *pkey, RSA *key) { @@ -1032,18 +1096,18 @@ int EVP_PKEY_CTX_get_rsa_padding(EVP_PKEY_CTX *ctx, int *out_padding) { } int EVP_PKEY_CTX_set_rsa_pss_keygen_md(EVP_PKEY_CTX *ctx, const EVP_MD *md) { - // We currently do not support keygen with |EVP_PKEY_RSA_PSS|. + // We currently do not support keygen with `EVP_PKEY_RSA_PSS`. return 0; } int EVP_PKEY_CTX_set_rsa_pss_keygen_saltlen(EVP_PKEY_CTX *ctx, int salt_len) { - // We currently do not support keygen with |EVP_PKEY_RSA_PSS|. + // We currently do not support keygen with `EVP_PKEY_RSA_PSS`. return 0; } int EVP_PKEY_CTX_set_rsa_pss_keygen_mgf1_md(EVP_PKEY_CTX *ctx, const EVP_MD *md) { - // We currently do not support keygen with |EVP_PKEY_RSA_PSS|. + // We currently do not support keygen with `EVP_PKEY_RSA_PSS`. return 0; } diff --git a/third_party/boringssl/src/crypto/evp/p_slhdsa.cc b/third_party/boringssl/src/crypto/evp/p_slhdsa.cc new file mode 100644 index 000000000..c6bb4fde7 --- /dev/null +++ b/third_party/boringssl/src/crypto/evp/p_slhdsa.cc @@ -0,0 +1,537 @@ +// Copyright 2026 The BoringSSL Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include + +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "../mem_internal.h" +#include "internal.h" + +using namespace bssl; + +namespace { + +constexpr uint8_t kSLHDSASHA2_128SOID[] = {OBJ_ENC_SLH_DSA_SHA2_128s}; +constexpr uint8_t kSLHDSASHAKE_256FOID[] = {OBJ_ENC_SLH_DSA_SHAKE_256f}; +constexpr uint8_t kSLHDSASHA2_128SWithSHA256OID[] = { + OBJ_ENC_SLH_DSA_SHA2_128s_WITH_SHA256}; + +constexpr size_t kMaxContextLength = 255; + +struct SLHDSASHA2_128STraits { + static constexpr size_t kPublicKeyBytes = SLHDSA_SHA2_128S_PUBLIC_KEY_BYTES; + static constexpr size_t kPrivateKeyBytes = SLHDSA_SHA2_128S_PRIVATE_KEY_BYTES; + static constexpr size_t kSignatureBytes = SLHDSA_SHA2_128S_SIGNATURE_BYTES; + static constexpr int kType = EVP_PKEY_SLH_DSA_SHA2_128S; + static constexpr Span kOID = kSLHDSASHA2_128SOID; + static constexpr bool kPrehash = false; + + static constexpr auto GenerateKey = &SLHDSA_SHA2_128S_generate_key; + static constexpr auto Sign = &SLHDSA_SHA2_128S_sign; + static constexpr auto Verify = &SLHDSA_SHA2_128S_verify; +}; + +struct SLHDSASHAKE_256FTraits { + static constexpr size_t kPublicKeyBytes = SLHDSA_SHAKE_256F_PUBLIC_KEY_BYTES; + static constexpr size_t kPrivateKeyBytes = + SLHDSA_SHAKE_256F_PRIVATE_KEY_BYTES; + static constexpr size_t kSignatureBytes = SLHDSA_SHAKE_256F_SIGNATURE_BYTES; + static constexpr int kType = EVP_PKEY_SLH_DSA_SHAKE_256F; + static constexpr Span kOID = kSLHDSASHAKE_256FOID; + static constexpr bool kPrehash = false; + + static constexpr auto GenerateKey = &SLHDSA_SHAKE_256F_generate_key; + static constexpr auto Sign = &SLHDSA_SHAKE_256F_sign; + static constexpr auto Verify = &SLHDSA_SHAKE_256F_verify; +}; + +struct SLHDSASHA2_128SWithSHA256Traits { + static constexpr size_t kPublicKeyBytes = SLHDSA_SHA2_128S_PUBLIC_KEY_BYTES; + static constexpr size_t kPrivateKeyBytes = SLHDSA_SHA2_128S_PRIVATE_KEY_BYTES; + static constexpr size_t kSignatureBytes = SLHDSA_SHA2_128S_SIGNATURE_BYTES; + static constexpr int kType = EVP_PKEY_SLH_DSA_SHA2_128S_WITH_SHA256; + static constexpr Span kOID = kSLHDSASHA2_128SWithSHA256OID; + static constexpr bool kPrehash = true; + static constexpr size_t kDigestLength = SHA256_DIGEST_LENGTH; + static constexpr int kDigestNID = NID_sha256; + + static constexpr auto GenerateKey = &SLHDSA_SHA2_128S_generate_key; + static constexpr auto PrehashSign = &SLHDSA_SHA2_128S_prehash_sign; + static constexpr auto PrehashVerify = &SLHDSA_SHA2_128S_prehash_verify; +}; + +template +struct SLHDSA_KEY { + enum { kAllowUniquePtr = true }; + + // key is the 4n-byte SLH-DSA private key (SK.seed || SK.prf || PK.seed || + // PK.root). If `has_private` is false, the first 2n bytes are uninitialized + // and the 2n-byte public key (PK.seed || PK.root) is in the last 2n bytes. + uint8_t key[Traits::kPrivateKeyBytes]; + bool has_private; + + Span public_key() { + return MakeSpan(key).template last(); + } + Span public_key() const { + return MakeConstSpan(key).template last(); + } +}; + +struct SlhdsaPkeyCtx { + bssl::Array context; +}; + +template +struct SLHDSAImplementation { + static SLHDSA_KEY *GetKeyData(EvpPkey *pkey) { + assert(pkey->ameth == &asn1_method); + return static_cast *>(pkey->pkey); + } + + static const SLHDSA_KEY *GetKeyData(const EvpPkey *pkey) { + return GetKeyData(const_cast(pkey)); + } + + static void PkeyFree(EvpPkey *pkey) { + Delete(GetKeyData(pkey)); + pkey->pkey = nullptr; + } + + static int SetRawPrivate(EvpPkey *pkey, const uint8_t *in, size_t len) { + if (len != Traits::kPrivateKeyBytes) { + OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR); + return 0; + } + UniquePtr> key = MakeUnique>(); + if (key == nullptr) { + return 0; + } + OPENSSL_memcpy(key->key, in, Traits::kPrivateKeyBytes); + key->has_private = true; + evp_pkey_set0(pkey, &asn1_method, key.release()); + return 1; + } + + static int SetRawPublic(EvpPkey *pkey, const uint8_t *in, size_t len) { + if (len != Traits::kPublicKeyBytes) { + OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR); + return 0; + } + UniquePtr> key = MakeUnique>(); + if (key == nullptr) { + return 0; + } + OPENSSL_memcpy(key->public_key().data(), in, Traits::kPublicKeyBytes); + key->has_private = false; + evp_pkey_set0(pkey, &asn1_method, key.release()); + return 1; + } + + static int GetRawPrivate(const EvpPkey *pkey, uint8_t *out, size_t *out_len) { + const SLHDSA_KEY *key = GetKeyData(pkey); + if (!key->has_private) { + OPENSSL_PUT_ERROR(EVP, EVP_R_NOT_A_PRIVATE_KEY); + return 0; + } + if (out == nullptr) { + *out_len = Traits::kPrivateKeyBytes; + return 1; + } + if (*out_len < Traits::kPrivateKeyBytes) { + OPENSSL_PUT_ERROR(EVP, EVP_R_BUFFER_TOO_SMALL); + return 0; + } + OPENSSL_memcpy(out, key->key, Traits::kPrivateKeyBytes); + *out_len = Traits::kPrivateKeyBytes; + return 1; + } + + static int GetRawPublic(const EvpPkey *pkey, uint8_t *out, size_t *out_len) { + const SLHDSA_KEY *key = GetKeyData(pkey); + if (out == nullptr) { + *out_len = Traits::kPublicKeyBytes; + return 1; + } + if (*out_len < Traits::kPublicKeyBytes) { + OPENSSL_PUT_ERROR(EVP, EVP_R_BUFFER_TOO_SMALL); + return 0; + } + OPENSSL_memcpy(out, key->public_key().data(), Traits::kPublicKeyBytes); + *out_len = Traits::kPublicKeyBytes; + return 1; + } + + static evp_decode_result_t DecodePublic(const EVP_PKEY_ALG *alg, EvpPkey *out, + CBS *params, CBS *key) { + // The parameters must be omitted. See RFC 9909, Sections 3 and 4. + if (CBS_len(params) != 0) { + OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR); + return evp_decode_error; + } + return SetRawPublic(out, CBS_data(key), CBS_len(key)) ? evp_decode_ok + : evp_decode_error; + } + + static int EncodePublic(CBB *out, const EvpPkey *pkey) { + const SLHDSA_KEY *key = GetKeyData(pkey); + // See RFC 9909, Sections 3 and 4. + CBB spki, algorithm, key_bitstring; + if (!CBB_add_asn1(out, &spki, CBS_ASN1_SEQUENCE) || + !CBB_add_asn1(&spki, &algorithm, CBS_ASN1_SEQUENCE) || + !CBB_add_asn1_element(&algorithm, CBS_ASN1_OBJECT, Traits::kOID.data(), + Traits::kOID.size()) || + !CBB_add_asn1(&spki, &key_bitstring, CBS_ASN1_BITSTRING) || + !CBB_add_u8(&key_bitstring, 0 /* no unused bits */) || + !CBB_add_bytes(&key_bitstring, key->public_key().data(), + key->public_key().size()) || + !CBB_flush(out)) { + OPENSSL_PUT_ERROR(EVP, EVP_R_ENCODE_ERROR); + return 0; + } + return 1; + } + + static bool EqualPublic(const EvpPkey *a, const EvpPkey *b) { + const SLHDSA_KEY *a_key = GetKeyData(a); + const SLHDSA_KEY *b_key = GetKeyData(b); + return OPENSSL_memcmp(a_key->public_key().data(), + b_key->public_key().data(), + Traits::kPublicKeyBytes) == 0; + } + + static bool HasPublic(const EvpPkey *pk) { return true; } + + static bool CopyPublic(EvpPkey *out, const EvpPkey *pk) { + const SLHDSA_KEY *key = GetKeyData(pk); + UniquePtr> public_copy = MakeUnique>(); + if (public_copy == nullptr) { + return false; + } + OPENSSL_memcpy(public_copy->public_key().data(), key->public_key().data(), + Traits::kPublicKeyBytes); + public_copy->has_private = false; + evp_pkey_set0(out, &asn1_method, public_copy.release()); + return true; + } + + static evp_decode_result_t DecodePrivate(const EVP_PKEY_ALG *alg, + EvpPkey *out, CBS *params, + CBS *key) { + // The parameters must be omitted. See RFC 9909, Sections 3 and 5. + if (CBS_len(params) != 0) { + OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR); + return evp_decode_error; + } + return SetRawPrivate(out, CBS_data(key), CBS_len(key)) ? evp_decode_ok + : evp_decode_error; + } + + static int EncodePrivate(CBB *out, const EvpPkey *pkey) { + const SLHDSA_KEY *key = GetKeyData(pkey); + if (!key->has_private) { + OPENSSL_PUT_ERROR(EVP, EVP_R_NOT_A_PRIVATE_KEY); + return 0; + } + // See RFC 9909, Sections 3 and 5. + CBB pkcs8, algorithm, private_key; + if (!CBB_add_asn1(out, &pkcs8, CBS_ASN1_SEQUENCE) || + !CBB_add_asn1_uint64(&pkcs8, 0 /* version */) || + !CBB_add_asn1(&pkcs8, &algorithm, CBS_ASN1_SEQUENCE) || + !CBB_add_asn1_element(&algorithm, CBS_ASN1_OBJECT, Traits::kOID.data(), + Traits::kOID.size()) || + !CBB_add_asn1(&pkcs8, &private_key, CBS_ASN1_OCTETSTRING) || + !CBB_add_bytes(&private_key, key->key, Traits::kPrivateKeyBytes) || + !CBB_flush(out)) { + OPENSSL_PUT_ERROR(EVP, EVP_R_ENCODE_ERROR); + return 0; + } + return 1; + } + + static bool HasPrivate(const EvpPkey *pk) { + return GetKeyData(pk)->has_private; + } + + static int PkeySize(const EvpPkey *pkey) { return Traits::kSignatureBytes; } + static int PkeyBits(const EvpPkey *pkey) { + // OpenSSL counts the bits in the public key serialization. + return Traits::kPublicKeyBytes * 8; + } + + static int Init(EvpPkeyCtx *ctx, const EVP_PKEY_ALG *) { + SlhdsaPkeyCtx *sctx = New(); + if (sctx == nullptr) { + return 0; + } + ctx->data = sctx; + return 1; + } + + static void Cleanup(EvpPkeyCtx *ctx) { + Delete(static_cast(ctx->data)); + } + + static int CopyContext(EvpPkeyCtx *dst, EvpPkeyCtx *src) { + if (!Init(dst, nullptr)) { + return 0; + } + SlhdsaPkeyCtx *sctx = static_cast(src->data); + SlhdsaPkeyCtx *dctx = static_cast(dst->data); + if (!dctx->context.CopyFrom(sctx->context)) { + return 0; + } + return 1; + } + + static int KeyGen(EvpPkeyCtx *ctx, EvpPkey *pkey) { + UniquePtr> key = MakeUnique>(); + if (key == nullptr) { + return 0; + } + uint8_t unused_public[Traits::kPublicKeyBytes]; + Traits::GenerateKey(unused_public, key->key); + key->has_private = true; + evp_pkey_set0(pkey, &asn1_method, key.release()); + return 1; + } + + static int SignMessage(EvpPkeyCtx *ctx, uint8_t *sig, size_t *siglen, + const uint8_t *tbs, size_t tbslen) { + const SLHDSA_KEY *key = GetKeyData(ctx->pkey.get()); + if (!key->has_private) { + OPENSSL_PUT_ERROR(EVP, EVP_R_NOT_A_PRIVATE_KEY); + return 0; + } + if (sig == nullptr) { + *siglen = Traits::kSignatureBytes; + return 1; + } + if (*siglen < Traits::kSignatureBytes) { + OPENSSL_PUT_ERROR(EVP, EVP_R_BUFFER_TOO_SMALL); + return 0; + } + SlhdsaPkeyCtx *sctx = static_cast(ctx->data); + if (!Traits::Sign(sig, key->key, tbs, tbslen, sctx->context.data(), + sctx->context.size())) { + return 0; + } + *siglen = Traits::kSignatureBytes; + return 1; + } + + static int VerifyMessage(EvpPkeyCtx *ctx, const uint8_t *sig, size_t siglen, + const uint8_t *tbs, size_t tbslen) { + const SLHDSA_KEY *key = GetKeyData(ctx->pkey.get()); + SlhdsaPkeyCtx *sctx = static_cast(ctx->data); + if (!Traits::Verify(sig, siglen, key->public_key().data(), tbs, tbslen, + sctx->context.data(), sctx->context.size())) { + OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_SIGNATURE); + return 0; + } + return 1; + } + + static int SignDigest(EvpPkeyCtx *ctx, uint8_t *sig, size_t *siglen, + const uint8_t *tbs, size_t tbslen) { + const SLHDSA_KEY *key = GetKeyData(ctx->pkey.get()); + if (!key->has_private) { + OPENSSL_PUT_ERROR(EVP, EVP_R_NOT_A_PRIVATE_KEY); + return 0; + } + if (sig == nullptr) { + *siglen = Traits::kSignatureBytes; + return 1; + } + if (*siglen < Traits::kSignatureBytes) { + OPENSSL_PUT_ERROR(EVP, EVP_R_BUFFER_TOO_SMALL); + return 0; + } + if (tbslen != Traits::kDigestLength) { + OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_DIGEST_LENGTH); + return 0; + } + SlhdsaPkeyCtx *sctx = static_cast(ctx->data); + if (!Traits::PrehashSign(sig, key->key, tbs, tbslen, Traits::kDigestNID, + sctx->context.data(), sctx->context.size())) { + return 0; + } + *siglen = Traits::kSignatureBytes; + return 1; + } + + static int VerifyDigest(EvpPkeyCtx *ctx, const uint8_t *sig, size_t siglen, + const uint8_t *tbs, size_t tbslen) { + const SLHDSA_KEY *key = GetKeyData(ctx->pkey.get()); + if (tbslen != Traits::kDigestLength) { + OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_DIGEST_LENGTH); + return 0; + } + SlhdsaPkeyCtx *sctx = static_cast(ctx->data); + if (!Traits::PrehashVerify(sig, siglen, key->public_key().data(), tbs, + tbslen, Traits::kDigestNID, + sctx->context.data(), sctx->context.size())) { + OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_SIGNATURE); + return 0; + } + return 1; + } + + static int Ctrl(EvpPkeyCtx *ctx, int type, int p1, void *p2) { + SlhdsaPkeyCtx *sctx = static_cast(ctx->data); + switch (type) { + case EVP_PKEY_CTRL_SIGNATURE_CONTEXT_STRING: { + const auto *context_string = + reinterpret_cast *>(p2); + if (context_string == nullptr || + context_string->size() > kMaxContextLength) { + OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_PARAMETERS); + return 0; + } + return sctx->context.CopyFrom(*context_string); + } + + case EVP_PKEY_CTRL_MD: { + if constexpr (!Traits::kPrehash) { + OPENSSL_PUT_ERROR(EVP, EVP_R_COMMAND_NOT_SUPPORTED); + return 0; + } else { + const auto *md = reinterpret_cast(p2); + if (md == nullptr || EVP_MD_type(md) != Traits::kDigestNID) { + OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_DIGEST_TYPE); + return 0; + } + return 1; + } + } + + case EVP_PKEY_CTRL_GET_MD: { + if constexpr (!Traits::kPrehash) { + OPENSSL_PUT_ERROR(EVP, EVP_R_COMMAND_NOT_SUPPORTED); + return 0; + } else { + // Add a `Digest()` hook to the traits if other algorithms supported. + static_assert(Traits::kDigestNID == NID_sha256); + *reinterpret_cast(p2) = EVP_sha256(); + return 1; + } + } + + default: + OPENSSL_PUT_ERROR(EVP, EVP_R_COMMAND_NOT_SUPPORTED); + return 0; + } + } + + static constexpr EVP_PKEY_CTX_METHOD BuildPkeyMethod() { + EVP_PKEY_CTX_METHOD ret = { + Traits::kType, + &Init, + &CopyContext, + &Cleanup, + &KeyGen, + /*sign=*/nullptr, + /*sign_message=*/nullptr, + /*verify=*/nullptr, + /*verify_message=*/nullptr, + /*verify_recover=*/nullptr, + /*encrypt=*/nullptr, + /*decrypt=*/nullptr, + /*derive=*/nullptr, + /*paramgen=*/nullptr, + /*encap=*/nullptr, + /*decap=*/nullptr, + &Ctrl, + }; + if constexpr (Traits::kPrehash) { + ret.sign = &SignDigest; + ret.verify = &VerifyDigest; + } else { + ret.sign_message = &SignMessage; + ret.verify_message = &VerifyMessage; + } + return ret; + } + + static constexpr EVP_PKEY_CTX_METHOD pkey_method = BuildPkeyMethod(); + + static constexpr EVP_PKEY_ASN1_METHOD BuildASN1Method() { + EVP_PKEY_ASN1_METHOD ret = { + Traits::kType, + // The OID is filled in below. + /*oid=*/{}, + /*oid_len=*/0, + &pkey_method, + &DecodePublic, + &EncodePublic, + &EqualPublic, + &HasPublic, + &CopyPublic, + &DecodePrivate, + &EncodePrivate, + &HasPrivate, + &SetRawPrivate, + /*set_priv_seed=*/nullptr, + &SetRawPublic, + &GetRawPrivate, + /*get_priv_seed=*/nullptr, + &GetRawPublic, + /*set1_tls_encodedpoint=*/nullptr, + /*get1_tls_encodedpoint=*/nullptr, + /*pkey_opaque=*/nullptr, + &PkeySize, + &PkeyBits, + /*param_missing=*/nullptr, + /*param_copy=*/nullptr, + /*param_equal=*/nullptr, + &PkeyFree, + }; + constexpr auto oid = Traits::kOID; + static_assert(oid.size() <= sizeof(ret.oid)); + for (size_t i = 0; i < oid.size(); i++) { + ret.oid[i] = oid[i]; + } + ret.oid_len = oid.size(); + return ret; + } + + static constexpr EVP_PKEY_ASN1_METHOD asn1_method = BuildASN1Method(); + static constexpr EVP_PKEY_ALG pkey_alg = {&asn1_method, &pkey_method}; +}; + +} // namespace + +const EVP_PKEY_ALG *EVP_pkey_slh_dsa_sha2_128s() { + return &SLHDSAImplementation::pkey_alg; +} + +const EVP_PKEY_ALG *EVP_pkey_slh_dsa_shake_256f() { + return &SLHDSAImplementation::pkey_alg; +} + +const EVP_PKEY_ALG *EVP_pkey_slh_dsa_sha2_128s_with_sha256() { + return &SLHDSAImplementation::pkey_alg; +} diff --git a/third_party/boringssl/src/crypto/evp/p_x25519.cc b/third_party/boringssl/src/crypto/evp/p_x25519.cc index db38d26f7..2c536088a 100644 --- a/third_party/boringssl/src/crypto/evp/p_x25519.cc +++ b/third_party/boringssl/src/crypto/evp/p_x25519.cc @@ -37,13 +37,13 @@ struct X25519_KEY { extern const EVP_PKEY_ASN1_METHOD x25519_asn1_meth; extern const EVP_PKEY_CTX_METHOD x25519_pkey_meth; -static void x25519_free(EvpPkey *pkey) { +void x25519_free(EvpPkey *pkey) { X25519_KEY *key = reinterpret_cast(pkey->pkey); OPENSSL_free(key); pkey->pkey = nullptr; } -static int x25519_set_priv_raw(EvpPkey *pkey, const uint8_t *in, size_t len) { +int x25519_set_priv_raw(EvpPkey *pkey, const uint8_t *in, size_t len) { if (len != 32) { OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR); return 0; @@ -62,7 +62,7 @@ static int x25519_set_priv_raw(EvpPkey *pkey, const uint8_t *in, size_t len) { return 1; } -static int x25519_set_pub_raw(EvpPkey *pkey, const uint8_t *in, size_t len) { +int x25519_set_pub_raw(EvpPkey *pkey, const uint8_t *in, size_t len) { if (len != 32) { OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR); return 0; @@ -80,8 +80,7 @@ static int x25519_set_pub_raw(EvpPkey *pkey, const uint8_t *in, size_t len) { return 1; } -static int x25519_get_priv_raw(const EvpPkey *pkey, uint8_t *out, - size_t *out_len) { +int x25519_get_priv_raw(const EvpPkey *pkey, uint8_t *out, size_t *out_len) { const X25519_KEY *key = reinterpret_cast(pkey->pkey); if (!key->has_private) { OPENSSL_PUT_ERROR(EVP, EVP_R_NOT_A_PRIVATE_KEY); @@ -103,8 +102,7 @@ static int x25519_get_priv_raw(const EvpPkey *pkey, uint8_t *out, return 1; } -static int x25519_get_pub_raw(const EvpPkey *pkey, uint8_t *out, - size_t *out_len) { +int x25519_get_pub_raw(const EvpPkey *pkey, uint8_t *out, size_t *out_len) { const X25519_KEY *key = reinterpret_cast(pkey->pkey); if (out == nullptr) { *out_len = 32; @@ -121,13 +119,11 @@ static int x25519_get_pub_raw(const EvpPkey *pkey, uint8_t *out, return 1; } -static int x25519_set1_tls_encodedpoint(EvpPkey *pkey, const uint8_t *in, - size_t len) { +int x25519_set1_tls_encodedpoint(EvpPkey *pkey, const uint8_t *in, size_t len) { return x25519_set_pub_raw(pkey, in, len); } -static size_t x25519_get1_tls_encodedpoint(const EvpPkey *pkey, - uint8_t **out_ptr) { +size_t x25519_get1_tls_encodedpoint(const EvpPkey *pkey, uint8_t **out_ptr) { const X25519_KEY *key = reinterpret_cast(pkey->pkey); if (key == nullptr) { OPENSSL_PUT_ERROR(EVP, EVP_R_NO_KEY_SET); @@ -138,9 +134,9 @@ static size_t x25519_get1_tls_encodedpoint(const EvpPkey *pkey, return *out_ptr == nullptr ? 0 : 32; } -static bssl::evp_decode_result_t x25519_pub_decode(const EVP_PKEY_ALG *alg, - EvpPkey *out, CBS *params, - CBS *key) { +bssl::evp_decode_result_t x25519_pub_decode(const EVP_PKEY_ALG *alg, + EvpPkey *out, CBS *params, + CBS *key) { // See RFC 8410, section 4. // The parameters must be omitted. Public keys have length 32. @@ -154,7 +150,7 @@ static bssl::evp_decode_result_t x25519_pub_decode(const EVP_PKEY_ALG *alg, : evp_decode_error; } -static int x25519_pub_encode(CBB *out, const EvpPkey *pkey) { +int x25519_pub_encode(CBB *out, const EvpPkey *pkey) { const X25519_KEY *key = reinterpret_cast(pkey->pkey); // See RFC 8410, section 4. @@ -174,15 +170,15 @@ static int x25519_pub_encode(CBB *out, const EvpPkey *pkey) { return 1; } -static bool x25519_pub_equal(const EvpPkey *a, const EvpPkey *b) { +bool x25519_pub_equal(const EvpPkey *a, const EvpPkey *b) { const X25519_KEY *a_key = reinterpret_cast(a->pkey); const X25519_KEY *b_key = reinterpret_cast(b->pkey); return OPENSSL_memcmp(a_key->pub, b_key->pub, 32) == 0; } -static bssl::evp_decode_result_t x25519_priv_decode(const EVP_PKEY_ALG *alg, - EvpPkey *out, CBS *params, - CBS *key) { +bssl::evp_decode_result_t x25519_priv_decode(const EVP_PKEY_ALG *alg, + EvpPkey *out, CBS *params, + CBS *key) { // See RFC 8410, section 7. // Parameters must be empty. The key is a 32-byte value wrapped in an extra @@ -199,7 +195,7 @@ static bssl::evp_decode_result_t x25519_priv_decode(const EVP_PKEY_ALG *alg, : evp_decode_error; } -static int x25519_priv_encode(CBB *out, const EvpPkey *pkey) { +int x25519_priv_encode(CBB *out, const EvpPkey *pkey) { const X25519_KEY *key = reinterpret_cast(pkey->pkey); if (!key->has_private) { OPENSSL_PUT_ERROR(EVP, EVP_R_NOT_A_PRIVATE_KEY); @@ -226,9 +222,9 @@ static int x25519_priv_encode(CBB *out, const EvpPkey *pkey) { return 1; } -static bool x25519_pub_present(const EvpPkey *) { return true; } +bool x25519_pub_present(const EvpPkey *) { return true; } -static bool x25519_pub_copy(EvpPkey *out, const EvpPkey *pkey) { +bool x25519_pub_copy(EvpPkey *out, const EvpPkey *pkey) { const X25519_KEY *pkey_x25519 = reinterpret_cast(pkey->pkey); X25519_KEY *public_copy = New(); @@ -241,14 +237,14 @@ static bool x25519_pub_copy(EvpPkey *out, const EvpPkey *pkey) { return true; } -static bool x25519_priv_present(const EvpPkey *pk) { +bool x25519_priv_present(const EvpPkey *pk) { const X25519_KEY *key = reinterpret_cast(pk->pkey); return key->has_private; } -static int x25519_size(const EvpPkey *pkey) { return 32; } +int x25519_size(const EvpPkey *pkey) { return 32; } -static int x25519_bits(const EvpPkey *pkey) { return 253; } +int x25519_bits(const EvpPkey *pkey) { return 253; } const EVP_PKEY_ASN1_METHOD x25519_asn1_meth = { EVP_PKEY_X25519, @@ -281,9 +277,9 @@ const EVP_PKEY_ASN1_METHOD x25519_asn1_meth = { }; // X25519 has no parameters to copy. -static int pkey_x25519_copy(EvpPkeyCtx *dst, EvpPkeyCtx *src) { return 1; } +int pkey_x25519_copy(EvpPkeyCtx *dst, EvpPkeyCtx *src) { return 1; } -static int pkey_x25519_keygen(EvpPkeyCtx *ctx, EvpPkey *pkey) { +int pkey_x25519_keygen(EvpPkeyCtx *ctx, EvpPkey *pkey) { X25519_KEY *key = New(); if (key == nullptr) { return 0; @@ -295,7 +291,7 @@ static int pkey_x25519_keygen(EvpPkeyCtx *ctx, EvpPkey *pkey) { return 1; } -static int pkey_x25519_derive(EvpPkeyCtx *ctx, uint8_t *out, size_t *out_len) { +int pkey_x25519_derive(EvpPkeyCtx *ctx, uint8_t *out, size_t *out_len) { if (ctx->pkey == nullptr || ctx->peerkey == nullptr) { OPENSSL_PUT_ERROR(EVP, EVP_R_KEYS_NOT_SET); return 0; @@ -330,10 +326,10 @@ static int pkey_x25519_derive(EvpPkeyCtx *ctx, uint8_t *out, size_t *out_len) { return 1; } -static int pkey_x25519_ctrl(EvpPkeyCtx *ctx, int type, int p1, void *p2) { +int pkey_x25519_ctrl(EvpPkeyCtx *ctx, int type, int p1, void *p2) { switch (type) { case EVP_PKEY_CTRL_PEER_KEY: - // |EVP_PKEY_derive_set_peer| requires the key implement this command, + // `EVP_PKEY_derive_set_peer` requires the key implement this command, // even if it is a no-op. return 1; diff --git a/third_party/boringssl/src/crypto/evp/p_xwing.cc b/third_party/boringssl/src/crypto/evp/p_xwing.cc index 2e0096822..7d9362ce1 100644 --- a/third_party/boringssl/src/crypto/evp/p_xwing.cc +++ b/third_party/boringssl/src/crypto/evp/p_xwing.cc @@ -39,19 +39,22 @@ struct XWING_KEY { extern const EVP_PKEY_ASN1_METHOD xwing_asn1_meth; extern const EVP_PKEY_CTX_METHOD xwing_pkey_meth; -static void xwing_free(EvpPkey *pkey) { +// This is the length of `eseed` in draft-connolly-cfrg-xwing-kem-06. +constexpr size_t kXwingEncapEntropyBytes = 64; + +void xwing_free(EvpPkey *pkey) { Delete(reinterpret_cast(pkey->pkey)); } -static bool xwing_pub_equal(const EvpPkey *a, const EvpPkey *b) { +bool xwing_pub_equal(const EvpPkey *a, const EvpPkey *b) { const XWING_KEY *a_key = reinterpret_cast(a->pkey); const XWING_KEY *b_key = reinterpret_cast(b->pkey); return OPENSSL_memcmp(a_key->pub, b_key->pub, XWING_PUBLIC_KEY_BYTES) == 0; } -static bool xwing_pub_present(const EvpPkey *) { return true; } +bool xwing_pub_present(const EvpPkey *) { return true; } -static bool xwing_pub_copy(EvpPkey *out, const EvpPkey *pkey) { +bool xwing_pub_copy(EvpPkey *out, const EvpPkey *pkey) { const XWING_KEY *pkey_xwing = reinterpret_cast(pkey->pkey); auto public_copy = MakeUnique(); if (public_copy == nullptr) { @@ -63,12 +66,12 @@ static bool xwing_pub_copy(EvpPkey *out, const EvpPkey *pkey) { return true; } -static bool xwing_priv_present(const EvpPkey *pk) { +bool xwing_priv_present(const EvpPkey *pk) { const XWING_KEY *key = reinterpret_cast(pk->pkey); return key->has_private; } -static int xwing_set_priv_seed(EvpPkey *pkey, const uint8_t *in, size_t len) { +int xwing_set_priv_seed(EvpPkey *pkey, const uint8_t *in, size_t len) { auto key = MakeUnique(); if (key == nullptr) { return 0; @@ -85,17 +88,16 @@ static int xwing_set_priv_seed(EvpPkey *pkey, const uint8_t *in, size_t len) { return 1; } -static int xwing_get_priv_seed(const EvpPkey *pkey, uint8_t *out, - size_t *out_len) { - if (out == nullptr) { - *out_len = XWING_PRIVATE_KEY_BYTES; - return 1; - } +int xwing_get_priv_seed(const EvpPkey *pkey, uint8_t *out, size_t *out_len) { const XWING_KEY *key = reinterpret_cast(pkey->pkey); if (key == nullptr || !key->has_private) { OPENSSL_PUT_ERROR(EVP, EVP_R_NOT_A_PRIVATE_KEY); return 0; } + if (out == nullptr) { + *out_len = XWING_PRIVATE_KEY_BYTES; + return 1; + } if (*out_len < XWING_PRIVATE_KEY_BYTES) { OPENSSL_PUT_ERROR(EVP, EVP_R_BUFFER_TOO_SMALL); return 0; @@ -110,7 +112,7 @@ static int xwing_get_priv_seed(const EvpPkey *pkey, uint8_t *out, return 1; } -static int xwing_set_pub_raw(EvpPkey *pkey, const uint8_t *in, size_t len) { +int xwing_set_pub_raw(EvpPkey *pkey, const uint8_t *in, size_t len) { if (len != XWING_PUBLIC_KEY_BYTES) { OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR); return 0; @@ -125,8 +127,7 @@ static int xwing_set_pub_raw(EvpPkey *pkey, const uint8_t *in, size_t len) { return 1; } -static int xwing_get_pub_raw(const EvpPkey *pkey, uint8_t *out, - size_t *out_len) { +int xwing_get_pub_raw(const EvpPkey *pkey, uint8_t *out, size_t *out_len) { if (out == nullptr) { *out_len = XWING_PUBLIC_KEY_BYTES; return 1; @@ -141,16 +142,14 @@ static int xwing_get_pub_raw(const EvpPkey *pkey, uint8_t *out, return 1; } -static int xwing_size(const EvpPkey *pkey) { return XWING_CIPHERTEXT_BYTES; } +int xwing_size(const EvpPkey *pkey) { return XWING_CIPHERTEXT_BYTES; } -static int xwing_bits(const EvpPkey *pkey) { - return XWING_PUBLIC_KEY_BYTES * 8; -} +int xwing_bits(const EvpPkey *pkey) { return XWING_PUBLIC_KEY_BYTES * 8; } // X-Wing has no parameters to copy. -static int pkey_xwing_copy_ctx(EvpPkeyCtx *dst, EvpPkeyCtx *src) { return 1; } +int pkey_xwing_copy_ctx(EvpPkeyCtx *dst, EvpPkeyCtx *src) { return 1; } -static int pkey_xwing_keygen(EvpPkeyCtx *ctx, EvpPkey *pkey) { +int pkey_xwing_keygen(EvpPkeyCtx *ctx, EvpPkey *pkey) { auto key = MakeUnique(); if (key == nullptr || !XWING_generate_key(key->pub, &key->priv)) { OPENSSL_PUT_ERROR(EVP, ERR_R_INTERNAL_ERROR); @@ -161,49 +160,74 @@ static int pkey_xwing_keygen(EvpPkeyCtx *ctx, EvpPkey *pkey) { return 1; } -static int xwing_kem_encap(uint8_t *out_ciphertext, size_t ciphertext_len, - uint8_t *out_secret, size_t secret_len, - const EVP_PKEY *peer_key) { - if (ciphertext_len != XWING_CIPHERTEXT_BYTES) { +int xwing_kem_encap(const EVP_KEM *kem, Span out_ciphertext, + Span out_secret, const EVP_PKEY *peer_key) { + if (out_ciphertext.size() != XWING_CIPHERTEXT_BYTES) { + OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_CIPHERTEXT_LENGTH); + return 0; + } + if (out_secret.size() != XWING_SHARED_SECRET_BYTES) { + OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_SECRET_LENGTH); + return 0; + } + const XWING_KEY *peer_pubkey = + reinterpret_cast(FromOpaque(peer_key)->pkey); + return XWING_encap(out_ciphertext.data(), out_secret.data(), + peer_pubkey->pub); +} + +int xwing_kem_encap_external_entropy(const EVP_KEM *kem, + Span out_ciphertext, + Span out_secret, + const EVP_PKEY *peer_key, + Span entropy) { + if (out_ciphertext.size() != XWING_CIPHERTEXT_BYTES) { OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_CIPHERTEXT_LENGTH); return 0; } - if (secret_len != XWING_SHARED_SECRET_BYTES) { + if (out_secret.size() != XWING_SHARED_SECRET_BYTES) { OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_SECRET_LENGTH); return 0; } + if (entropy.size() != kXwingEncapEntropyBytes) { + OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_ENTROPY_LENGTH); + return 0; + } const XWING_KEY *peer_pubkey = reinterpret_cast(FromOpaque(peer_key)->pkey); - return XWING_encap(out_ciphertext, out_secret, peer_pubkey->pub); + return XWING_encap_external_entropy(out_ciphertext.data(), out_secret.data(), + peer_pubkey->pub, entropy.data()); } -static int xwing_kem_decap(uint8_t *out_secret, size_t secret_len, - const uint8_t *ciphertext, size_t ciphertext_len, - const EVP_PKEY *key) { +int xwing_kem_decap(const EVP_KEM *kem, Span out_secret, + Span ciphertext, const EVP_PKEY *key) { const XWING_KEY *priv = reinterpret_cast(FromOpaque(key)->pkey); if (priv == nullptr || !priv->has_private) { OPENSSL_PUT_ERROR(EVP, EVP_R_NOT_A_PRIVATE_KEY); return 0; } - if (secret_len != XWING_SHARED_SECRET_BYTES) { + if (out_secret.size() != XWING_SHARED_SECRET_BYTES) { OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_SECRET_LENGTH); return 0; } // XWING_decap does not accept wrong ciphertext lengths, so we must check for // the proper length here. For consistency, we don't add an error to the error // queue when a KEM decap fails due to incorrect ciphertext length. - if (ciphertext_len != XWING_CIPHERTEXT_BYTES) { + if (ciphertext.size() != XWING_CIPHERTEXT_BYTES) { return 0; } - return XWING_decap(out_secret, ciphertext, &priv->priv); + return XWING_decap(out_secret.data(), ciphertext.data(), &priv->priv); } -static const EVP_KEM xwing_evp_kem = { - EVP_PKEY_XWING, // - XWING_CIPHERTEXT_BYTES, // - XWING_SHARED_SECRET_BYTES, // - &xwing_kem_encap, // - &xwing_kem_decap, // +const EVP_KEM xwing_evp_kem = { + EVP_PKEY_XWING, // + XWING_CIPHERTEXT_BYTES, // + XWING_SHARED_SECRET_BYTES, // + kXwingEncapEntropyBytes, // + /*check_key=*/nullptr, // + &xwing_kem_encap, // + &xwing_kem_encap_external_entropy, // + &xwing_kem_decap, // }; const EVP_PKEY_CTX_METHOD xwing_pkey_meth = { diff --git a/third_party/boringssl/src/crypto/evp/pbkdf.cc b/third_party/boringssl/src/crypto/evp/pbkdf.cc index 6388876ef..8dddbd083 100644 --- a/third_party/boringssl/src/crypto/evp/pbkdf.cc +++ b/third_party/boringssl/src/crypto/evp/pbkdf.cc @@ -16,6 +16,7 @@ #include +#include #include #include "../internal.h" @@ -34,6 +35,10 @@ int PKCS5_PBKDF2_HMAC(const char *password, size_t password_len, uint32_t i = 1; size_t md_len = EVP_MD_size(digest); + if (key_len > ((uint64_t{1} << 32) - 1) * md_len) { + OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_SECRET_LENGTH); + return 0; + } while (key_len > 0) { size_t todo = md_len; if (todo > key_len) { @@ -77,7 +82,7 @@ int PKCS5_PBKDF2_HMAC(const char *password, size_t password_len, // value of 0 is an error. // // Unfortunately not all consumers of PKCS5_PBKDF2_HMAC() check their return - // value, expecting it to succeed and unconditionally using |out_key|. As a + // value, expecting it to succeed and unconditionally using `out_key`. As a // precaution for such callsites in external code, the old behavior of // iterations < 1 being treated as iterations == 1 is preserved, but // additionally an error result is returned. diff --git a/third_party/boringssl/src/crypto/evp/print.cc b/third_party/boringssl/src/crypto/evp/print.cc index 2dce12fe1..b50f5a372 100644 --- a/third_party/boringssl/src/crypto/evp/print.cc +++ b/third_party/boringssl/src/crypto/evp/print.cc @@ -72,7 +72,7 @@ static int bn_print(BIO *bp, const char *name, const BIGNUM *num, int off) { return 0; } - // Print |num| in hex, adding a leading zero, as in ASN.1, if the high bit + // Print `num` in hex, adding a leading zero, as in ASN.1, if the high bit // is set. // // TODO(davidben): Do we need to do this? We already print "(Negative)" above diff --git a/third_party/boringssl/src/crypto/evp/scrypt.cc b/third_party/boringssl/src/crypto/evp/scrypt.cc index 8c229b2ea..d1e89a773 100644 --- a/third_party/boringssl/src/crypto/evp/scrypt.cc +++ b/third_party/boringssl/src/crypto/evp/scrypt.cc @@ -26,8 +26,8 @@ // // Note scrypt refers to both "blocks" and a "block size" parameter, r. These // are two different notions of blocks. A Salsa20 block is 64 bytes long, -// represented in this implementation by 16 |uint32_t|s. |r| determines the -// number of 64-byte Salsa20 blocks in a scryptBlockMix block, which is 2 * |r| +// represented in this implementation by 16 `uint32_t`s. `r` determines the +// number of 64-byte Salsa20 blocks in a scryptBlockMix block, which is 2 * `r` // Salsa20 blocks. This implementation refers to them as Salsa20 blocks and // scrypt blocks, respectively. @@ -41,7 +41,7 @@ typedef struct { static_assert(sizeof(block_t) == 64, "block_t has padding"); // salsa208_word_specification implements the Salsa20/8 core function, also -// described in RFC 7914, section 3. It modifies the block at |inout| +// described in RFC 7914, section 3. It modifies the block at `inout` // in-place. static void salsa208_word_specification(block_t *inout) { block_t x; @@ -87,7 +87,7 @@ static void salsa208_word_specification(block_t *inout) { } } -// xor_block sets |*out| to be |*a| XOR |*b|. +// xor_block sets `*out` to be `*a` XOR `*b`. static void xor_block(block_t *out, const block_t *a, const block_t *b) { for (size_t i = 0; i < 16; i++) { out->words[i] = a->words[i] ^ b->words[i]; @@ -95,8 +95,8 @@ static void xor_block(block_t *out, const block_t *a, const block_t *b) { } // scryptBlockMix implements the function described in RFC 7914, section 4. B' -// is written to |out|. |out| and |B| may not alias and must be each one scrypt -// block (2 * |r| Salsa20 blocks) long. +// is written to `out`. `out` and `B` may not alias and must be each one scrypt +// block (2 * `r` Salsa20 blocks) long. static void scryptBlockMix(block_t *out, const block_t *B, uint64_t r) { assert(out != B); @@ -111,11 +111,11 @@ static void scryptBlockMix(block_t *out, const block_t *B, uint64_t r) { } } -// scryptROMix implements the function described in RFC 7914, section 5. |B| is -// an scrypt block (2 * |r| Salsa20 blocks) and is modified in-place. |T| and -// |V| are scratch space allocated by the caller. |T| must have space for one -// scrypt block (2 * |r| Salsa20 blocks). |V| must have space for |N| scrypt -// blocks (2 * |r| * |N| Salsa20 blocks). +// scryptROMix implements the function described in RFC 7914, section 5. `B` is +// an scrypt block (2 * `r` Salsa20 blocks) and is modified in-place. `T` and +// `V` are scratch space allocated by the caller. `T` must have space for one +// scrypt block (2 * `r` Salsa20 blocks). `V` must have space for `N` scrypt +// blocks (2 * `r` * `N` Salsa20 blocks). static void scryptROMix(block_t *B, uint64_t r, uint64_t N, block_t *T, block_t *V) { // Steps 1 and 2. @@ -128,7 +128,7 @@ static void scryptROMix(block_t *B, uint64_t r, uint64_t N, block_t *T, // Step 3. for (uint64_t i = 0; i < N; i++) { - // Note this assumes |N| <= 2^32 and is a power of 2. + // Note this assumes `N` <= 2^32 and is a power of 2. uint32_t j = B[2 * r - 1].words[0] & (N - 1); for (size_t k = 0; k < 2 * r; k++) { xor_block(&T[k], &B[k], &V[2 * r * j + k]); @@ -146,7 +146,7 @@ static void scryptROMix(block_t *B, uint64_t r, uint64_t N, block_t *T, #define SCRYPT_PR_MAX ((1 << 30) - 1) // SCRYPT_MAX_MEM is the default maximum memory that may be allocated by -// |EVP_PBE_scrypt|. +// `EVP_PBE_scrypt`. #define SCRYPT_MAX_MEM (1024 * 1024 * 65) int EVP_PBE_scrypt(const char *password, size_t password_len, @@ -154,18 +154,18 @@ int EVP_PBE_scrypt(const char *password, size_t password_len, uint64_t p, size_t max_mem, uint8_t *out_key, size_t key_len) { if (r == 0 || p == 0 || p > SCRYPT_PR_MAX / r || - // |N| must be a power of two. + // `N` must be a power of two. N < 2 || (N & (N - 1)) || - // We only support |N| <= 2^32 in |scryptROMix|. + // We only support `N` <= 2^32 in `scryptROMix`. N > UINT64_C(1) << 32 || - // Check that |N| < 2^(128×r / 8). + // Check that `N` < 2^(128×r / 8). (16 * r <= 63 && N >= UINT64_C(1) << (16 * r))) { OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_PARAMETERS); return 0; } - // Determine the amount of memory needed. B, T, and V are |p|, 1, and |N| - // scrypt blocks, respectively. Each scrypt block is 2*|r| |block_t|s. + // Determine the amount of memory needed. B, T, and V are `p`, 1, and `N` + // scrypt blocks, respectively. Each scrypt block is 2*`r` `block_t`s. if (max_mem == 0) { max_mem = SCRYPT_MAX_MEM; } @@ -176,7 +176,7 @@ int EVP_PBE_scrypt(const char *password, size_t password_len, return 0; } - // Allocate and divide up the scratch space. |max_mem| fits in a size_t, which + // Allocate and divide up the scratch space. `max_mem` fits in a size_t, which // is no bigger than uint64_t, so none of these operations may overflow. static_assert(UINT64_MAX >= SIZE_MAX, "size_t exceeds uint64_t"); size_t B_blocks = p * 2 * r; @@ -194,7 +194,7 @@ int EVP_PBE_scrypt(const char *password, size_t password_len, block_t *V = T + T_blocks; // NOTE: PKCS5_PBKDF2_HMAC can only fail due to allocation failure - // or |iterations| of 0 (we pass 1 here). This is consistent with + // or `iterations` of 0 (we pass 1 here). This is consistent with // the documented failure conditions of EVP_PBE_scrypt. if (!PKCS5_PBKDF2_HMAC(password, password_len, salt, salt_len, 1, EVP_sha256(), B_bytes, (uint8_t *)B)) { diff --git a/third_party/boringssl/src/crypto/evp/sign.cc b/third_party/boringssl/src/crypto/evp/sign.cc index e14acc4e5..3503c4081 100644 --- a/third_party/boringssl/src/crypto/evp/sign.cc +++ b/third_party/boringssl/src/crypto/evp/sign.cc @@ -19,8 +19,6 @@ #include #include -#include "internal.h" - int EVP_SignInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl) { return EVP_DigestInit_ex(ctx, type, impl); @@ -35,8 +33,8 @@ int EVP_SignUpdate(EVP_MD_CTX *ctx, const void *data, size_t len) { } int EVP_SignFinal(const EVP_MD_CTX *ctx, uint8_t *sig, unsigned *out_sig_len, - EVP_PKEY *pkey) { - // Ensure the final result will fit in |unsigned|. + const EVP_PKEY *pkey) { + // Ensure the final result will fit in `unsigned`. size_t sig_len = EVP_PKEY_size(pkey); if (sig_len > UINT_MAX) { sig_len = UINT_MAX; @@ -75,7 +73,7 @@ int EVP_VerifyUpdate(EVP_MD_CTX *ctx, const void *data, size_t len) { } int EVP_VerifyFinal(EVP_MD_CTX *ctx, const uint8_t *sig, size_t sig_len, - EVP_PKEY *pkey) { + const EVP_PKEY *pkey) { uint8_t m[EVP_MAX_MD_SIZE]; unsigned m_len; bssl::ScopedEVP_MD_CTX tmp_ctx; @@ -85,11 +83,9 @@ int EVP_VerifyFinal(EVP_MD_CTX *ctx, const uint8_t *sig, size_t sig_len, } bssl::UniquePtr pkctx(EVP_PKEY_CTX_new(pkey, nullptr)); - if (!pkctx || - !EVP_PKEY_verify_init(pkctx.get()) || + if (!pkctx || !EVP_PKEY_verify_init(pkctx.get()) || !EVP_PKEY_CTX_set_signature_md(pkctx.get(), ctx->digest)) { return 0; } return EVP_PKEY_verify(pkctx.get(), sig, sig_len, m, m_len); } - diff --git a/third_party/boringssl/src/crypto/ex_data.cc b/third_party/boringssl/src/crypto/ex_data.cc index ea66bc6da..4614065fb 100644 --- a/third_party/boringssl/src/crypto/ex_data.cc +++ b/third_party/boringssl/src/crypto/ex_data.cc @@ -33,8 +33,8 @@ struct ExDataFuncs { long argl; // Arbitrary long void *argp; // Arbitrary void pointer CRYPTO_EX_free *free_func; - // next points to the next |ExDataFuncs| or NULL if this is the last - // one. It may only be read if synchronized with a read from |num_funcs|. + // next points to the next `ExDataFuncs` or NULL if this is the last + // one. It may only be read if synchronized with a read from `num_funcs`. ExDataFuncs *next; }; @@ -53,13 +53,13 @@ int CRYPTO_get_ex_new_index_ex(ExDataClass *ex_data_class, long argl, MutexWriteLock lock(&ex_data_class->lock); uint32_t num_funcs = ex_data_class->num_funcs.load(); - // The index must fit in |int|. + // The index must fit in `int`. if (num_funcs > (size_t)(INT_MAX - ex_data_class->num_reserved)) { OPENSSL_PUT_ERROR(CRYPTO, ERR_R_OVERFLOW); return -1; } - // Append |funcs| to the linked list. + // Append `funcs` to the linked list. if (ex_data_class->last == nullptr) { assert(num_funcs == 0); ex_data_class->funcs = funcs; @@ -76,8 +76,8 @@ int CRYPTO_get_ex_new_index_ex(ExDataClass *ex_data_class, long argl, int CRYPTO_set_ex_data(CRYPTO_EX_DATA *ad, int index, void *val) { if (index < 0) { // A caller that can accidentally pass in an invalid index into this - // function will hit an memory error if |index| happened to be valid, and - // expected |val| to be of a different type. + // function will hit a memory error if `index` happened to be valid, and + // expected `val` to be of a different type. abort(); } @@ -115,11 +115,11 @@ void CRYPTO_free_ex_data(ExDataClass *ex_data_class, CRYPTO_EX_DATA *ad) { } uint32_t num_funcs = ex_data_class->num_funcs.load(); - // |CRYPTO_get_ex_new_index_ex| will not allocate indices beyond |INT_MAX|. + // `CRYPTO_get_ex_new_index_ex` will not allocate indices beyond `INT_MAX`. assert(num_funcs <= (size_t)(INT_MAX - ex_data_class->num_reserved)); - // Defer dereferencing |ex_data_class->funcs| and |funcs->next|. It must come - // after the |num_funcs| comparison to be correctly synchronized. + // Defer dereferencing `ex_data_class->funcs` and `funcs->next`. It must come + // after the `num_funcs` comparison to be correctly synchronized. ExDataFuncs *const *funcs = &ex_data_class->funcs; for (uint32_t i = 0; i < num_funcs; i++) { if ((*funcs)->free_func != nullptr) { diff --git a/third_party/boringssl/src/crypto/fipsmodule/aes/aes.cc.inc b/third_party/boringssl/src/crypto/fipsmodule/aes/aes.cc.inc index 56a110f33..f18906b05 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/aes/aes.cc.inc +++ b/third_party/boringssl/src/crypto/fipsmodule/aes/aes.cc.inc @@ -21,9 +21,9 @@ using namespace bssl; // Be aware that different sets of AES functions use incompatible key -// representations, varying in format of the key schedule, the |AES_KEY.rounds| +// representations, varying in format of the key schedule, the `AES_KEY.rounds` // value, or both. Therefore they cannot mix. Also, on AArch64, the plain-C -// code, above, is incompatible with the |aes_hw_*| functions. +// code, above, is incompatible with the `aes_hw_*` functions. bcm_infallible bssl::BCM_aes_encrypt(const uint8_t *in, uint8_t *out, const AES_KEY *key) { @@ -84,8 +84,8 @@ bcm_status bssl::BCM_aes_set_decrypt_key(const uint8_t *key, unsigned bits, } #if defined(HWAES) && (defined(OPENSSL_X86) || defined(OPENSSL_X86_64)) -// On x86 and x86_64, |aes_hw_set_decrypt_key|, we implement -// |aes_hw_encrypt_key_to_decrypt_key| in assembly and rely on C code to combine +// On x86 and x86_64, `aes_hw_set_decrypt_key`, we implement +// `aes_hw_encrypt_key_to_decrypt_key` in assembly and rely on C code to combine // the operations. int bssl::aes_hw_set_decrypt_key(const uint8_t *user_key, int bits, AES_KEY *key) { @@ -107,7 +107,7 @@ int bssl::aes_hw_set_encrypt_key(const uint8_t *user_key, int bits, #endif #if defined(VPAES) && defined(OPENSSL_X86) -// On x86, there is no |vpaes_ctr32_encrypt_blocks|, so we implement it +// On x86, there is no `vpaes_ctr32_encrypt_blocks`, so we implement it // ourselves. This avoids all callers needing to account for a missing function. void bssl::vpaes_ctr32_encrypt_blocks(const uint8_t *in, uint8_t *out, size_t blocks, const AES_KEY *key, @@ -131,7 +131,7 @@ void bssl::vpaes_ctr32_encrypt_blocks_with_bsaes(const uint8_t *in, uint8_t *out, size_t blocks, const AES_KEY *key, const uint8_t ivec[16]) { - // |bsaes_ctr32_encrypt_blocks| is faster than |vpaes_ctr32_encrypt_blocks|, + // `bsaes_ctr32_encrypt_blocks` is faster than `vpaes_ctr32_encrypt_blocks`, // but it takes at least one full 8-block batch to amortize the conversion. if (blocks < 8) { vpaes_ctr32_encrypt_blocks(in, out, blocks, key, ivec); @@ -140,9 +140,9 @@ void bssl::vpaes_ctr32_encrypt_blocks_with_bsaes(const uint8_t *in, size_t bsaes_blocks = blocks; if (bsaes_blocks % 8 < 6) { - // |bsaes_ctr32_encrypt_blocks| internally works in 8-block batches. If the + // `bsaes_ctr32_encrypt_blocks` internally works in 8-block batches. If the // final batch is too small (under six blocks), it is faster to loop over - // |vpaes_encrypt|. Round |bsaes_blocks| down to a multiple of 8. + // `vpaes_encrypt`. Round `bsaes_blocks` down to a multiple of 8. bsaes_blocks -= bsaes_blocks % 8; } @@ -160,7 +160,7 @@ void bssl::vpaes_ctr32_encrypt_blocks_with_bsaes(const uint8_t *in, uint32_t ctr = CRYPTO_load_u32_be(ivec + 12) + bsaes_blocks; CRYPTO_store_u32_be(new_ivec + 12, ctr); - // Finish any remaining blocks with |vpaes_ctr32_encrypt_blocks|. + // Finish any remaining blocks with `vpaes_ctr32_encrypt_blocks`. vpaes_ctr32_encrypt_blocks(in, out, blocks, key, new_ivec); } #endif // BSAES diff --git a/third_party/boringssl/src/crypto/fipsmodule/aes/aes_nohw.cc.inc b/third_party/boringssl/src/crypto/fipsmodule/aes/aes_nohw.cc.inc index 20f6a6ba3..18e4a4cdf 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/aes/aes_nohw.cc.inc +++ b/third_party/boringssl/src/crypto/fipsmodule/aes/aes_nohw.cc.inc @@ -45,18 +45,18 @@ using namespace bssl; // shifts match the operations themselves, which makes them reversed in a // little-endian, left-to-right reading. // -// Eight |aes_word_t|s contain |AES_NOHW_BATCH_SIZE| blocks. The bits in an -// |aes_word_t| are divided into 16 consecutive groups of |AES_NOHW_BATCH_SIZE| +// Eight `aes_word_t`s contain `AES_NOHW_BATCH_SIZE` blocks. The bits in an +// `aes_word_t` are divided into 16 consecutive groups of `AES_NOHW_BATCH_SIZE` // bits each, each corresponding to a byte in an AES block in column-major // order (AES's byte order). We refer to these as "logical bytes". Note, in the // 32-bit and 64-bit implementations, they are smaller than a byte. (The // contents of a logical byte will be described later.) // -// MSVC does not support C bit operators on |__m128i|, so the wrapper functions -// |aes_nohw_and|, etc., should be used instead. Note |aes_nohw_shift_left| and -// |aes_nohw_shift_right| measure the shift in logical bytes. That is, the shift -// value ranges from 0 to 15 independent of |aes_word_t| and -// |AES_NOHW_BATCH_SIZE|. +// MSVC does not support C bit operators on `__m128i`, so the wrapper functions +// `aes_nohw_and`, etc., should be used instead. Note `aes_nohw_shift_left` and +// `aes_nohw_shift_right` measure the shift in logical bytes. That is, the shift +// value ranges from 0 to 15 independent of `aes_word_t` and +// `AES_NOHW_BATCH_SIZE`. // // This ordering is different from https://eprint.iacr.org/2009/129.pdf, which // uses row-major order. Matching the AES order was easier to reason about, and @@ -100,7 +100,7 @@ static aes_word_t aes_nohw_not(aes_word_t a) { a, _mm_set_epi32(0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff)); } -// These are macros because parameters to |_mm_slli_si128| and |_mm_srli_si128| +// These are macros because parameters to `_mm_slli_si128` and `_mm_srli_si128` // must be constants. #define aes_nohw_shift_left(/* aes_word_t */ a, /* const */ i) \ _mm_slli_si128((a), (i)) @@ -158,8 +158,8 @@ static_assert(AES_NOHW_WORD_SIZE == sizeof(aes_word_t), // // This implementation uses three representations for AES blocks. First, the // public API represents blocks as uint8_t[16] in the usual way. Second, most -// AES steps are evaluated in bitsliced form, stored in an |AES_NOHW_BATCH|. -// This stores |AES_NOHW_BATCH_SIZE| blocks in bitsliced order. For 64-bit words +// AES steps are evaluated in bitsliced form, stored in an `AES_NOHW_BATCH`. +// This stores `AES_NOHW_BATCH_SIZE` blocks in bitsliced order. For 64-bit words // containing bitsliced blocks a, b, c, d, this would be as follows (vertical // bars divide logical bytes): // @@ -172,10 +172,10 @@ static_assert(AES_NOHW_WORD_SIZE == sizeof(aes_word_t), // Finally, an individual block may be stored as an intermediate form in an // aes_word_t[AES_NOHW_BLOCK_WORDS]. In this form, we permute the bits in each // block, so that block[0]'s ith logical byte contains least-significant -// |AES_NOHW_BATCH_SIZE| bits of byte i, block[1] contains the next group of -// |AES_NOHW_BATCH_SIZE| bits, and so on. We refer to this transformation as +// `AES_NOHW_BATCH_SIZE` bits of byte i, block[1] contains the next group of +// `AES_NOHW_BATCH_SIZE` bits, and so on. We refer to this transformation as // "compacting" the block. Note this is no-op with 128-bit words because then -// |AES_NOHW_BLOCK_WORDS| is one and |AES_NOHW_BATCH_SIZE| is eight. For 64-bit +// `AES_NOHW_BLOCK_WORDS` is one and `AES_NOHW_BATCH_SIZE` is eight. For 64-bit // words, one block would be stored in two words: // // block[0] = a0 a1 a2 a3 | a8 a9 a10 a11 | a16 a17 a18 a19 ... @@ -197,40 +197,40 @@ static_assert(AES_NOHW_WORD_SIZE == sizeof(aes_word_t), // batch.w[3] = a3 b3 c3 d3 | a11 b11 c11 d11 | a19 b19 c19 d19 ... // // Note also that bitwise operations and (logical) byte permutations on an -// |aes_word_t| work equally for the bitsliced and compact words. +// `aes_word_t` work equally for the bitsliced and compact words. // -// We use the compact form in the |AES_KEY| representation to save work -// inflating round keys into |AES_NOHW_BATCH|. The compact form also exists -// temporarily while moving blocks in or out of an |AES_NOHW_BATCH|, immediately -// before or after |aes_nohw_transpose|. +// We use the compact form in the `AES_KEY` representation to save work +// inflating round keys into `AES_NOHW_BATCH`. The compact form also exists +// temporarily while moving blocks in or out of an `AES_NOHW_BATCH`, immediately +// before or after `aes_nohw_transpose`. #define AES_NOHW_BLOCK_WORDS (16 / sizeof(aes_word_t)) -// An AES_NOHW_BATCH stores |AES_NOHW_BATCH_SIZE| blocks. Unless otherwise +// An AES_NOHW_BATCH stores `AES_NOHW_BATCH_SIZE` blocks. Unless otherwise // specified, it is in bitsliced form. typedef struct { aes_word_t w[8]; } AES_NOHW_BATCH; // An AES_NOHW_SCHEDULE is an expanded bitsliced AES key schedule. It is -// suitable for encryption or decryption. It is as large as |AES_NOHW_BATCH| -// |AES_KEY|s so it should not be used as a long-term key representation. +// suitable for encryption or decryption. It is as large as `AES_NOHW_BATCH` +// `AES_KEY`s so it should not be used as a long-term key representation. typedef struct { // keys is an array of batches, one for each round key. Each batch stores - // |AES_NOHW_BATCH_SIZE| copies of the round key in bitsliced form. + // `AES_NOHW_BATCH_SIZE` copies of the round key in bitsliced form. AES_NOHW_BATCH keys[AES_MAXNR + 1]; } AES_NOHW_SCHEDULE; -// aes_nohw_batch_set sets the |i|th block of |batch| to |in|. |batch| is in +// aes_nohw_batch_set sets the `i`th block of `batch` to `in`. `batch` is in // compact form. static void aes_nohw_batch_set(AES_NOHW_BATCH *batch, const aes_word_t in[AES_NOHW_BLOCK_WORDS], size_t i) { - // Note the words are interleaved. The order comes from |aes_nohw_transpose|. - // If |i| is zero and this is the 64-bit implementation, in[0] contains bits + // Note the words are interleaved. The order comes from `aes_nohw_transpose`. + // If `i` is zero and this is the 64-bit implementation, in[0] contains bits // 0-3 and in[1] contains bits 4-7. We place in[0] at w[0] and in[1] at // w[4] so that bits 0 and 4 are in the correct position. (In general, bits - // along diagonals of |AES_NOHW_BATCH_SIZE| by |AES_NOHW_BATCH_SIZE| squares + // along diagonals of `AES_NOHW_BATCH_SIZE` by `AES_NOHW_BATCH_SIZE` squares // will be correctly placed.) assert(i < AES_NOHW_BATCH_SIZE); #if defined(OPENSSL_SSE2) @@ -246,7 +246,7 @@ static void aes_nohw_batch_set(AES_NOHW_BATCH *batch, #endif } -// aes_nohw_batch_get writes the |i|th block of |batch| to |out|. |batch| is in +// aes_nohw_batch_get writes the `i`th block of `batch` to `out`. `batch` is in // compact form. static void aes_nohw_batch_get(const AES_NOHW_BATCH *batch, aes_word_t out[AES_NOHW_BLOCK_WORDS], size_t i) { @@ -265,8 +265,8 @@ static void aes_nohw_batch_get(const AES_NOHW_BATCH *batch, } #if !defined(OPENSSL_SSE2) -// aes_nohw_delta_swap returns |a| with bits |a & mask| and -// |a & (mask << shift)| swapped. |mask| and |mask << shift| may not overlap. +// aes_nohw_delta_swap returns `a` with bits |a & mask| and +// |a & (mask << shift)| swapped. `mask` and |mask << shift| may not overlap. static aes_word_t aes_nohw_delta_swap(aes_word_t a, aes_word_t mask, aes_word_t shift) { // See @@ -276,8 +276,8 @@ static aes_word_t aes_nohw_delta_swap(aes_word_t a, aes_word_t mask, } // In the 32-bit and 64-bit implementations, a block spans multiple words. -// |aes_nohw_compact_block| must permute bits across different words. First we -// implement |aes_nohw_compact_word| which performs a smaller version of the +// `aes_nohw_compact_block` must permute bits across different words. First we +// implement `aes_nohw_compact_word` which performs a smaller version of the // transformation which stays within a single word. // // These transformations are generalizations of the output of @@ -301,7 +301,7 @@ static uint64_t aes_nohw_compact_word(uint64_t a) { } static uint64_t aes_nohw_uncompact_word(uint64_t a) { - // Reverse the steps of |aes_nohw_uncompact_word|. + // Reverse the steps of `aes_nohw_uncompact_word`. a = aes_nohw_delta_swap(a, UINT64_C(0x00000000ffff0000), 16); a = aes_nohw_delta_swap(a, UINT64_C(0x0000ff000000ff00), 8); a = aes_nohw_delta_swap(a, UINT64_C(0x00f000f000f000f0), 4); @@ -324,7 +324,7 @@ static uint32_t aes_nohw_compact_word(uint32_t a) { } static uint32_t aes_nohw_uncompact_word(uint32_t a) { - // Reverse the steps of |aes_nohw_uncompact_word|. + // Reverse the steps of `aes_nohw_uncompact_word`. a = aes_nohw_delta_swap(a, 0x0000f0f0, 12); a = aes_nohw_delta_swap(a, 0x00cc00cc, 6); return a; @@ -406,11 +406,11 @@ static void aes_nohw_uncompact_block( } // aes_nohw_swap_bits is a variation on a delta swap. It swaps the bits in -// |*a & (mask << shift)| with the bits in |*b & mask|. |mask| and -// |mask << shift| must not overlap. |mask| is specified as a |uint32_t|, but it -// is repeated to the full width of |aes_word_t|. +// `*a & (mask << shift)` with the bits in `*b & mask`. `mask` and +// `mask << shift` must not overlap. `mask` is specified as a `uint32_t`, but it +// is repeated to the full width of `aes_word_t`. #if defined(OPENSSL_SSE2) -// This must be a macro because |_mm_srli_epi32| and |_mm_slli_epi32| require +// This must be a macro because `_mm_srli_epi32` and `_mm_slli_epi32` require // constant shift values. #define aes_nohw_swap_bits(/*__m128i* */ a, /*__m128i* */ b, \ /* uint32_t */ mask, /* const */ shift) \ @@ -437,7 +437,7 @@ static void aes_nohw_swap_bits(aes_word_t *a, aes_word_t *b, uint32_t mask, } #endif // OPENSSL_SSE2 -// aes_nohw_transpose converts |batch| to and from bitsliced form. It divides +// aes_nohw_transpose converts `batch` to and from bitsliced form. It divides // the 8 × word_size bits into AES_NOHW_BATCH_SIZE × AES_NOHW_BATCH_SIZE squares // and transposes each square. static void aes_nohw_transpose(AES_NOHW_BATCH *batch) { @@ -464,8 +464,8 @@ static void aes_nohw_transpose(AES_NOHW_BATCH *batch) { #endif } -// aes_nohw_to_batch initializes |out| with the |num_blocks| blocks from |in|. -// |num_blocks| must be at most |AES_NOHW_BATCH|. +// aes_nohw_to_batch initializes `out` with the `num_blocks` blocks from `in`. +// `num_blocks` must be at most `AES_NOHW_BATCH`. static void aes_nohw_to_batch(AES_NOHW_BATCH *out, const uint8_t *in, size_t num_blocks) { // Don't leave unused blocks uninitialized. @@ -480,8 +480,8 @@ static void aes_nohw_to_batch(AES_NOHW_BATCH *out, const uint8_t *in, aes_nohw_transpose(out); } -// aes_nohw_to_batch writes the first |num_blocks| blocks in |batch| to |out|. -// |num_blocks| must be at most |AES_NOHW_BATCH|. +// aes_nohw_to_batch writes the first `num_blocks` blocks in `batch` to `out`. +// `num_blocks` must be at most `AES_NOHW_BATCH`. static void aes_nohw_from_batch(uint8_t *out, size_t num_blocks, const AES_NOHW_BATCH *batch) { AES_NOHW_BATCH copy = *batch; @@ -696,8 +696,8 @@ static void aes_nohw_inv_sub_bytes(AES_NOHW_BATCH *batch) { aes_nohw_sub_bytes_inv_affine(batch); } -// aes_nohw_rotate_cols_right returns |v| with the columns in each row rotated -// to the right by |n|. This is a macro because |aes_nohw_shift_*| require +// aes_nohw_rotate_cols_right returns `v` with the columns in each row rotated +// to the right by `n`. This is a macro because `aes_nohw_shift_*` require // constant shift counts in the SSE2 implementation. #define aes_nohw_rotate_cols_right(/* aes_word_t */ v, /* const */ n) \ (aes_nohw_or(aes_nohw_shift_right((v), (n)*4), \ @@ -729,7 +729,7 @@ static void aes_nohw_inv_shift_rows(AES_NOHW_BATCH *batch) { } } -// aes_nohw_rotate_rows_down returns |v| with the rows in each column rotated +// aes_nohw_rotate_rows_down returns `v` with the rows in each column rotated // down by one. static aes_word_t aes_nohw_rotate_rows_down(aes_word_t v) { #if defined(OPENSSL_SSE2) @@ -742,7 +742,7 @@ static aes_word_t aes_nohw_rotate_rows_down(aes_word_t v) { #endif } -// aes_nohw_rotate_rows_twice returns |v| with the rows in each column rotated +// aes_nohw_rotate_rows_twice returns `v` with the rows in each column rotated // by two. static aes_word_t aes_nohw_rotate_rows_twice(aes_word_t v) { #if defined(OPENSSL_SSE2) @@ -823,7 +823,7 @@ static void aes_nohw_inv_mix_columns(AES_NOHW_BATCH *batch) { // | 0d 09 0e 0b | | 01 01 02 03 | | 04 00 05 00 | // | 0b 0d 09 0e | | 03 01 01 02 | | 00 04 00 05 | // - // First, apply the [5 0 4 0] matrix. Multiplying by 4 in F_(2^8) is described + // First, apply the [5 0 4 0] matrix. Multiplying by 4 in F_(2⁸) is described // by the following bit equations: // // b0 = a6 @@ -910,8 +910,8 @@ static void aes_nohw_expand_round_keys(AES_NOHW_SCHEDULE *out, static const uint8_t aes_nohw_rcon[10] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36}; -// aes_nohw_rcon_slice returns the |i|th group of |AES_NOHW_BATCH_SIZE| bits in -// |rcon|, stored in a |aes_word_t|. +// aes_nohw_rcon_slice returns the `i`th group of `AES_NOHW_BATCH_SIZE` bits in +// `rcon`, stored in a `aes_word_t`. static aes_word_t aes_nohw_rcon_slice(uint8_t rcon, size_t i) { rcon = (rcon >> (i * AES_NOHW_BATCH_SIZE)) & ((1 << AES_NOHW_BATCH_SIZE) - 1); #if defined(OPENSSL_SSE2) @@ -944,7 +944,7 @@ static void aes_nohw_setup_key_128(AES_KEY *key, const uint8_t in[16]) { aes_nohw_sub_block(sub, block); uint8_t rcon = aes_nohw_rcon[i - 1]; for (size_t j = 0; j < AES_NOHW_BLOCK_WORDS; j++) { - // Incorporate |rcon| and the transformed word into the first word. + // Incorporate `rcon` and the transformed word into the first word. block[j] = aes_nohw_xor(block[j], aes_nohw_rcon_slice(rcon, j)); block[j] = aes_nohw_xor( block[j], @@ -971,9 +971,9 @@ static void aes_nohw_setup_key_192(AES_KEY *key, const uint8_t in[24]) { // We maintain a sliding window of two blocks, filled to 1.5 blocks at a time. // We loop below every three blocks or two key schedule iterations. // - // On entry to the loop, |block1| and the first half of |block2| contain the - // previous key schedule iteration. |block1| has been written to |key|, but - // |block2| has not as it is incomplete. + // On entry to the loop, `block1` and the first half of `block2` contain the + // previous key schedule iteration. `block1` has been written to `key`, but + // `block2` has not as it is incomplete. aes_nohw_compact_block(block1, in); memcpy(key->rd_key, block1, 16); @@ -987,17 +987,17 @@ static void aes_nohw_setup_key_192(AES_KEY *key, const uint8_t in[24]) { uint8_t rcon = aes_nohw_rcon[2 * i]; for (size_t j = 0; j < AES_NOHW_BLOCK_WORDS; j++) { // Compute the first two words of the next key schedule iteration, which - // go in the second half of |block2|. The first two words of the previous - // iteration are in the first half of |block1|. Apply |rcon| here too + // go in the second half of `block2`. The first two words of the previous + // iteration are in the first half of `block1`. Apply `rcon` here too // because the shifts match. block2[j] = aes_nohw_or( block2[j], aes_nohw_shift_left( aes_nohw_xor(block1[j], aes_nohw_rcon_slice(rcon, j)), 8)); // Incorporate the transformed word and propagate. Note the last word of - // the previous iteration corresponds to the second word of |copy|. This + // the previous iteration corresponds to the second word of `copy`. This // is incorporated into the first word of the next iteration, or the third - // word of |block2|. + // word of `block2`. block2[j] = aes_nohw_xor( block2[j], aes_nohw_and(aes_nohw_shift_left( aes_nohw_rotate_rows_down(sub[j]), 4), @@ -1006,12 +1006,12 @@ static void aes_nohw_setup_key_192(AES_KEY *key, const uint8_t in[24]) { block2[j], aes_nohw_and(aes_nohw_shift_left(block2[j], 4), AES_NOHW_COL3_MASK)); - // Compute the remaining four words, which fill |block1|. Begin by moving + // Compute the remaining four words, which fill `block1`. Begin by moving // the corresponding words of the previous iteration: the second half of - // |block1| and the first half of |block2|. + // `block1` and the first half of `block2`. block1[j] = aes_nohw_shift_right(block1[j], 8); block1[j] = aes_nohw_or(block1[j], aes_nohw_shift_left(block2[j], 8)); - // Incorporate the second word, computed previously in |block2|, and + // Incorporate the second word, computed previously in `block2`, and // propagate. block1[j] = aes_nohw_xor(block1[j], aes_nohw_shift_right(block2[j], 12)); aes_word_t v = block1[j]; @@ -1020,7 +1020,7 @@ static void aes_nohw_setup_key_192(AES_KEY *key, const uint8_t in[24]) { block1[j] = aes_nohw_xor(block1[j], aes_nohw_shift_left(v, 12)); } - // This completes two round keys. Note half of |block2| was computed in the + // This completes two round keys. Note half of `block2` was computed in the // previous loop iteration but was not yet output. memcpy(key->rd_key + 4 * (3 * i + 1), block2, 16); memcpy(key->rd_key + 4 * (3 * i + 2), block1, 16); @@ -1029,12 +1029,12 @@ static void aes_nohw_setup_key_192(AES_KEY *key, const uint8_t in[24]) { rcon = aes_nohw_rcon[2 * i + 1]; for (size_t j = 0; j < AES_NOHW_BLOCK_WORDS; j++) { // Compute the first four words of the next key schedule iteration in - // |block2|. Begin by moving the corresponding words of the previous - // iteration: the second half of |block2| and the first half of |block1|. + // `block2`. Begin by moving the corresponding words of the previous + // iteration: the second half of `block2` and the first half of `block1`. block2[j] = aes_nohw_shift_right(block2[j], 8); block2[j] = aes_nohw_or(block2[j], aes_nohw_shift_left(block1[j], 8)); // Incorporate rcon and the transformed word. Note the last word of the - // previous iteration corresponds to the last word of |copy|. + // previous iteration corresponds to the last word of `copy`. block2[j] = aes_nohw_xor(block2[j], aes_nohw_rcon_slice(rcon, j)); block2[j] = aes_nohw_xor( block2[j], @@ -1045,9 +1045,9 @@ static void aes_nohw_setup_key_192(AES_KEY *key, const uint8_t in[24]) { block2[j] = aes_nohw_xor(block2[j], aes_nohw_shift_left(v, 8)); block2[j] = aes_nohw_xor(block2[j], aes_nohw_shift_left(v, 12)); - // Compute the last two words, which go in the first half of |block1|. The + // Compute the last two words, which go in the first half of `block1`. The // last two words of the previous iteration are in the second half of - // |block1|. + // `block1`. block1[j] = aes_nohw_shift_right(block1[j], 8); // Propagate blocks and mask off the excess. block1[j] = aes_nohw_xor(block1[j], aes_nohw_shift_right(block2[j], 12)); @@ -1055,7 +1055,7 @@ static void aes_nohw_setup_key_192(AES_KEY *key, const uint8_t in[24]) { block1[j] = aes_nohw_and(block1[j], AES_NOHW_COL01_MASK); } - // |block2| has a complete round key. |block1| will be completed in the next + // `block2` has a complete round key. `block1` will be completed in the next // iteration. memcpy(key->rd_key + 4 * (3 * i + 3), block2, 16); @@ -1082,7 +1082,7 @@ static void aes_nohw_setup_key_256(AES_KEY *key, const uint8_t in[32]) { aes_nohw_sub_block(sub, block2); uint8_t rcon = aes_nohw_rcon[i / 2 - 1]; for (size_t j = 0; j < AES_NOHW_BLOCK_WORDS; j++) { - // Incorporate |rcon| and the transformed word into the first word. + // Incorporate `rcon` and the transformed word into the first word. block1[j] = aes_nohw_xor(block1[j], aes_nohw_rcon_slice(rcon, j)); block1[j] = aes_nohw_xor( block1[j], @@ -1178,7 +1178,7 @@ void bssl::aes_nohw_ctr32_encrypt_blocks(const uint8_t *in, uint8_t *out, AES_NOHW_SCHEDULE sched; aes_nohw_expand_round_keys(&sched, key); - // Make |AES_NOHW_BATCH_SIZE| copies of |ivec|. + // Make `AES_NOHW_BATCH_SIZE` copies of `ivec`. alignas(AES_NOHW_WORD_SIZE) uint8_t ivs[AES_NOHW_BATCH_SIZE * 16]; alignas(AES_NOHW_WORD_SIZE) uint8_t enc_ivs[AES_NOHW_BATCH_SIZE * 16]; for (size_t i = 0; i < AES_NOHW_BATCH_SIZE; i++) { diff --git a/third_party/boringssl/src/crypto/fipsmodule/aes/cbc.cc.inc b/third_party/boringssl/src/crypto/fipsmodule/aes/cbc.cc.inc index aa9b759ab..5ed6373ea 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/aes/cbc.cc.inc +++ b/third_party/boringssl/src/crypto/fipsmodule/aes/cbc.cc.inc @@ -26,7 +26,7 @@ void bssl::CRYPTO_cbc128_encrypt(const uint8_t *in, uint8_t *out, size_t len, block128_f block) { assert(key != nullptr && ivec != nullptr); if (len == 0) { - // Avoid |ivec| == |iv| in the |memcpy| below, which is not legal in C. + // Avoid `ivec` == `iv` in the `memcpy` below, which is not legal in C. return; } @@ -67,7 +67,7 @@ void bssl::CRYPTO_cbc128_decrypt(const uint8_t *in, uint8_t *out, size_t len, block128_f block) { assert(key != nullptr && ivec != nullptr); if (len == 0) { - // Avoid |ivec| == |iv| in the |memcpy| below, which is not legal in C. + // Avoid `ivec` == `iv` in the `memcpy` below, which is not legal in C. return; } @@ -75,13 +75,13 @@ void bssl::CRYPTO_cbc128_decrypt(const uint8_t *in, uint8_t *out, size_t len, const uintptr_t inptr = (uintptr_t) in; const uintptr_t outptr = (uintptr_t) out; - // If |in| and |out| alias, |in| must be ahead. + // If `in` and `out` alias, `in` must be ahead. assert(inptr >= outptr || inptr + len <= outptr); size_t n; alignas(16) uint8_t tmp[16]; if ((inptr >= 32 && outptr <= inptr - 32) || inptr < outptr) { - // If |out| is at least two blocks behind |in| or completely disjoint, there + // If `out` is at least two blocks behind `in` or completely disjoint, there // is no need to decrypt to a temporary block. const uint8_t *iv = ivec; while (len >= 16) { diff --git a/third_party/boringssl/src/crypto/fipsmodule/aes/gcm.cc.inc b/third_party/boringssl/src/crypto/fipsmodule/aes/gcm.cc.inc index 73beb8b36..732a76cea 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/aes/gcm.cc.inc +++ b/third_party/boringssl/src/crypto/fipsmodule/aes/gcm.cc.inc @@ -26,7 +26,7 @@ using namespace bssl; // kSizeTWithoutLower4Bits is a mask that can be used to zero the lower four -// bits of a |size_t|. +// bits of a `size_t`. static const size_t kSizeTWithoutLower4Bits = (size_t) -16; @@ -79,7 +79,7 @@ void bssl::gcm_init_ssse3(u128 Htable[16], const uint64_t H[2]) { Htable[14].hi = V.hi ^ Htable[6].hi, Htable[14].lo = V.lo ^ Htable[6].lo; Htable[15].hi = V.hi ^ Htable[7].hi, Htable[15].lo = V.lo ^ Htable[7].lo; - // Treat |Htable| as a 16x16 byte table and transpose it. Thus, Htable[i] + // Treat `Htable` as a 16x16 byte table and transpose it. Thus, Htable[i] // contains the i'th byte of j*H for all j. uint8_t *Hbytes = (uint8_t *)Htable; for (int i = 0; i < 16; i++) { @@ -180,7 +180,7 @@ static size_t hw_gcm_decrypt(const uint8_t *in, uint8_t *out, size_t len, void bssl::CRYPTO_ghash_init(gmult_func *out_mult, ghash_func *out_hash, u128 out_table[16], const uint8_t gcm_key[16]) { - // H is passed to |gcm_init_*| as a pair of byte-swapped, 64-bit values. + // H is passed to `gcm_init_*` as a pair of byte-swapped, 64-bit values. uint64_t H[2] = {CRYPTO_load_u64_be(gcm_key), CRYPTO_load_u64_be(gcm_key + 8)}; @@ -276,10 +276,10 @@ void bssl::CRYPTO_gcm128_init_aes_key(GCM128_KEY *gcm_key, const uint8_t *key, #if !defined(OPENSSL_NO_ASM) #if defined(OPENSSL_X86_64) - if (gcm_key->ghash == gcm_ghash_vpclmulqdq_avx512 && + if (gcm_key->ghash == gcm_ghash_vpclmulqdq_avx512 && is_hwaes && CRYPTO_is_VAES_capable()) { gcm_key->impl = gcm_x86_vaes_avx512; - } else if (gcm_key->ghash == gcm_ghash_vpclmulqdq_avx2 && + } else if (gcm_key->ghash == gcm_ghash_vpclmulqdq_avx2 && is_hwaes && CRYPTO_is_VAES_capable()) { gcm_key->impl = gcm_x86_vaes_avx2; } else if (gcm_key->ghash == gcm_ghash_avx && is_hwaes) { @@ -435,7 +435,7 @@ int bssl::CRYPTO_gcm128_encrypt(const GCM128_KEY *key, GCM128_CONTEXT *ctx, #if defined(HW_GCM) if (key->impl != gcm_separate && len > 0) { - // |hw_gcm_encrypt| may not process all the input given to it. It may + // `hw_gcm_encrypt` may not process all the input given to it. It may // not process *any* of its input if it is deemed too small. size_t bulk = hw_gcm_encrypt(in, out, len, &key->aes, ctx->Yi, ctx->Xi, key->Htable, key->impl); @@ -523,7 +523,7 @@ int bssl::CRYPTO_gcm128_decrypt(const GCM128_KEY *key, GCM128_CONTEXT *ctx, #if defined(HW_GCM) if (key->impl != gcm_separate && len > 0) { - // |hw_gcm_decrypt| may not process all the input given to it. It may + // `hw_gcm_decrypt` may not process all the input given to it. It may // not process *any* of its input if it is deemed too small. size_t bulk = hw_gcm_decrypt(in, out, len, &key->aes, ctx->Yi, ctx->Xi, key->Htable, key->impl); diff --git a/third_party/boringssl/src/crypto/fipsmodule/aes/gcm_nohw.cc.inc b/third_party/boringssl/src/crypto/fipsmodule/aes/gcm_nohw.cc.inc index a4c6d1c05..4af795e63 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/aes/gcm_nohw.cc.inc +++ b/third_party/boringssl/src/crypto/fipsmodule/aes/gcm_nohw.cc.inc @@ -40,7 +40,7 @@ static void gcm_mul64_nohw(uint64_t *out_lo, uint64_t *out_hi, uint64_t a, // One term every four bits means the largest term is 64/4 = 16, which barely // overflows into the next term. Using one term every five bits would cost 25 // multiplications instead of 16. It is faster to mask off the bottom four - // bits of |a|, giving a largest term of 60/4 = 15, and apply the bottom bits + // bits of `a`, giving a largest term of 60/4 = 15, and apply the bottom bits // separately. uint64_t a0 = a & UINT64_C(0x1111111111111110); uint64_t a1 = a & UINT64_C(0x2222222222222220); @@ -61,7 +61,7 @@ static void gcm_mul64_nohw(uint64_t *out_lo, uint64_t *out_hi, uint64_t a, uint128_t c3 = (a0 * (uint128_t)b3) ^ (a1 * (uint128_t)b2) ^ (a2 * (uint128_t)b1) ^ (a3 * (uint128_t)b0); - // Multiply the bottom four bits of |a| with |b|. + // Multiply the bottom four bits of `a` with `b`. uint64_t a0_mask = UINT64_C(0) - (a & 1); uint64_t a1_mask = UINT64_C(0) - ((a >> 1) & 1); uint64_t a2_mask = UINT64_C(0) - ((a >> 2) & 1); @@ -201,7 +201,7 @@ void bssl::gcm_init_nohw(u128 Htable[16], const uint64_t Xi[2]) { // rev128(X) * rev128(Y) = rev255(X*Y). // // Per Appendix A, we run mulX_POLYVAL. Note this is the same transformation - // applied by |gcm_init_clmul|, etc. Note |Xi| has already been byteswapped. + // applied by `gcm_init_clmul`, etc. Note `Xi` has already been byteswapped. // // See also slide 16 of // https://crypto.stanford.edu/RealWorldCrypto/slides/gueron.pdf @@ -220,12 +220,12 @@ void bssl::gcm_init_nohw(u128 Htable[16], const uint64_t Xi[2]) { Htable[0].lo ^= carry & 1; Htable[0].hi ^= carry & UINT64_C(0xc200000000000000); - // This implementation does not use the rest of |Htable|. + // This implementation does not use the rest of `Htable`. } static void gcm_polyval_nohw(uint64_t Xi[2], const u128 *H) { - // Karatsuba multiplication. The product of |Xi| and |H| is stored in |r0| - // through |r3|. Note there is no byte or bit reversal because we are + // Karatsuba multiplication. The product of `Xi` and `H` is stored in `r0` + // through `r3`. Note there is no byte or bit reversal because we are // evaluating POLYVAL. uint64_t r0, r1; gcm_mul64_nohw(&r0, &r1, Xi[0], H->lo); @@ -238,8 +238,8 @@ static void gcm_polyval_nohw(uint64_t Xi[2], const u128 *H) { r2 ^= mid1; r1 ^= mid0; - // Now we multiply our 256-bit result by x^-128 and reduce. |r2| and - // |r3| shifts into position and we must multiply |r0| and |r1| by x^-128. We + // Now we multiply our 256-bit result by x^-128 and reduce. `r2` and + // `r3` shifts into position and we must multiply `r0` and `r1` by x^-128. We // have: // // 1 = x^121 + x^126 + x^127 + x^128 @@ -249,7 +249,7 @@ static void gcm_polyval_nohw(uint64_t Xi[2], const u128 *H) { // The x^-7, x^-2, and x^-1 terms shift bits past x^0, which would require // another reduction steps. Instead, we gather the excess bits, incorporate - // them into |r0| and |r1| and reduce once. See slides 17-19 + // them into `r0` and `r1` and reduce once. See slides 17-19 // of https://crypto.stanford.edu/RealWorldCrypto/slides/gueron.pdf. r1 ^= (r0 << 63) ^ (r0 << 62) ^ (r0 << 57); diff --git a/third_party/boringssl/src/crypto/fipsmodule/aes/internal.h b/third_party/boringssl/src/crypto/fipsmodule/aes/internal.h index db627037f..3ee356c5a 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/aes/internal.h +++ b/third_party/boringssl/src/crypto/fipsmodule/aes/internal.h @@ -26,9 +26,9 @@ BSSL_NAMESPACE_BEGIN // block128_f is the type of an AES block cipher implementation. // // Unlike upstream OpenSSL, it and the other functions in this file hard-code -// |AES_KEY|. It is undefined in C to call a function pointer with anything -// other than the original type. Thus we either must match |block128_f| to the -// type signature of |BCM_aes_encrypt| and friends or pass in |void*| wrapper +// `AES_KEY`. It is undefined in C to call a function pointer with anything +// other than the original type. Thus we either must match `block128_f` to the +// type signature of `BCM_aes_encrypt` and friends or pass in `void*` wrapper // functions. // // These functions are called exclusively with AES, so we use the former. @@ -39,9 +39,9 @@ typedef void (*block128_f)(const uint8_t in[16], uint8_t out[16], typedef void (*ctr128_f)(const uint8_t *in, uint8_t *out, size_t blocks, const AES_KEY *key, const uint8_t ivec[16]); -// aes_ctr_set_key initialises |*aes_key| using |key_bytes| bytes from |key|, -// where |key_bytes| must either be 16, 24 or 32. If not NULL, |*out_block| is -// set to a function that encrypts single blocks. If not NULL, |*out_is_hwaes| +// aes_ctr_set_key initialises `*aes_key` using `key_bytes` bytes from `key`, +// where `key_bytes` must either be 16, 24 or 32. If not NULL, `*out_block` is +// set to a function that encrypts single blocks. If not NULL, `*out_is_hwaes` // is set to whether the hardware AES implementation was used. It returns a // function for optimised CTR-mode. ctr128_f aes_ctr_set_key(AES_KEY *aes_key, int *out_is_hwaes, @@ -104,8 +104,8 @@ extern "C" void aes_hw_ctr32_encrypt_blocks(const uint8_t *in, uint8_t *out, const uint8_t ivec[16]); #if defined(OPENSSL_X86) || defined(OPENSSL_X86_64) -// On x86 and x86_64, |aes_hw_set_decrypt_key| is implemented in terms of -// |aes_hw_set_encrypt_key| and a conversion function. +// On x86 and x86_64, `aes_hw_set_decrypt_key` is implemented in terms of +// `aes_hw_set_encrypt_key` and a conversion function. extern "C" void aes_hw_encrypt_key_to_decrypt_key(AES_KEY *key); // There are two variants of this function, one which uses aeskeygenassist @@ -177,7 +177,7 @@ extern "C" void aes_hw_ecb_encrypt(const uint8_t *in, uint8_t *out, #if defined(BSAES) -// Note |bsaes_cbc_encrypt| requires |enc| to be zero. +// Note `bsaes_cbc_encrypt` requires `enc` to be zero. extern "C" void bsaes_cbc_encrypt(const uint8_t *in, uint8_t *out, size_t length, const AES_KEY *key, uint8_t ivec[16], int enc); @@ -264,8 +264,8 @@ void aes_nohw_cbc_encrypt(const uint8_t *in, uint8_t *out, size_t len, inline void CRYPTO_xor16(uint8_t out[16], const uint8_t a[16], const uint8_t b[16]) { // TODO(davidben): Ideally we'd leave this to the compiler, which could use - // vector registers, etc. But the compiler doesn't know that |in| and |out| - // cannot partially alias. |restrict| is slightly two strict (we allow exact + // vector registers, etc. But the compiler doesn't know that `in` and `out` + // cannot partially alias. `restrict` is slightly two strict (we allow exact // aliasing), but perhaps in-place could be a separate function? static_assert(16 % sizeof(crypto_word_t) == 0, "block cannot be evenly divided into words"); @@ -279,12 +279,12 @@ inline void CRYPTO_xor16(uint8_t out[16], const uint8_t a[16], // CTR. // CRYPTO_ctr128_encrypt_ctr32 encrypts (or decrypts, it's the same in CTR mode) -// |len| bytes from |in| to |out| using |block| in counter mode. There's no -// requirement that |len| be a multiple of any value and any partial blocks are -// stored in |ecount_buf| and |*num|, which must be zeroed before the initial -// call. The counter is a 128-bit, big-endian value in |ivec| and is +// `len` bytes from `in` to `out` using `block` in counter mode. There's no +// requirement that `len` be a multiple of any value and any partial blocks are +// stored in `ecount_buf` and `*num`, which must be zeroed before the initial +// call. The counter is a 128-bit, big-endian value in `ivec` and is // incremented by this function. If the counter overflows, it wraps around. -// |ctr| must be a function that performs CTR mode but only deals with the lower +// `ctr` must be a function that performs CTR mode but only deals with the lower // 32 bits of the counter. void CRYPTO_ctr128_encrypt_ctr32(const uint8_t *in, uint8_t *out, size_t len, const AES_KEY *key, uint8_t ivec[16], @@ -294,10 +294,10 @@ void CRYPTO_ctr128_encrypt_ctr32(const uint8_t *in, uint8_t *out, size_t len, // GCM. // -// This API differs from the upstream API slightly. The |GCM128_CONTEXT| does -// not have a |key| pointer that points to the key as upstream's version does. -// Instead, every function takes a |key| parameter. This way |GCM128_CONTEXT| -// can be safely copied. Additionally, |gcm_key| is split into a separate +// This API differs from the upstream API slightly. The `GCM128_CONTEXT` does +// not have a `key` pointer that points to the key as upstream's version does. +// Instead, every function takes a `key` parameter. This way `GCM128_CONTEXT` +// can be safely copied. Additionally, `gcm_key` is split into a separate // struct. // gcm_impl_t specifies an assembly implementation of AES-GCM. @@ -312,12 +312,12 @@ enum gcm_impl_t { typedef struct { uint64_t hi,lo; } u128; -// gmult_func multiplies |Xi| by the GCM key and writes the result back to -// |Xi|. +// gmult_func multiplies `Xi` by the GCM key and writes the result back to +// `Xi`. typedef void (*gmult_func)(uint8_t Xi[16], const u128 Htable[16]); -// ghash_func repeatedly multiplies |Xi| by the GCM key and adds in blocks from -// |inp|. The result is written back to |Xi| and the |len| argument must be a +// ghash_func repeatedly multiplies `Xi` by the GCM key and adds in blocks from +// `inp`. The result is written back to `Xi` and the `len` argument must be a // multiple of 16. typedef void (*ghash_func)(uint8_t Xi[16], const u128 Htable[16], const uint8_t *inp, size_t len); @@ -354,48 +354,48 @@ typedef struct { int crypto_gcm_clmul_enabled(); #endif -// CRYPTO_ghash_init writes a precomputed table of powers of |gcm_key| to -// |out_table| and sets |*out_mult| and |*out_hash| to (potentially hardware +// CRYPTO_ghash_init writes a precomputed table of powers of `gcm_key` to +// `out_table` and sets `*out_mult` and `*out_hash` to (potentially hardware // accelerated) functions for performing operations in the GHASH field. void CRYPTO_ghash_init(gmult_func *out_mult, ghash_func *out_hash, u128 out_table[16], const uint8_t gcm_key[16]); -// CRYPTO_gcm128_init_aes_key initialises |gcm_key| to with AES key |key|. +// CRYPTO_gcm128_init_aes_key initialises `gcm_key` to with AES key `key`. void CRYPTO_gcm128_init_aes_key(GCM128_KEY *gcm_key, const uint8_t *key, size_t key_bytes); -// CRYPTO_gcm128_init_ctx initializes |ctx| to encrypt with |key| and |iv|. +// CRYPTO_gcm128_init_ctx initializes `ctx` to encrypt with `key` and `iv`. void CRYPTO_gcm128_init_ctx(const GCM128_KEY *key, GCM128_CONTEXT *ctx, const uint8_t *iv, size_t iv_len); // CRYPTO_gcm128_aad adds to the authenticated data for an instance of GCM. -// This must be called before and data is encrypted. |key| must be the same -// value that was passed to |CRYPTO_gcm128_init_ctx|. It returns one on success +// This must be called before and data is encrypted. `key` must be the same +// value that was passed to `CRYPTO_gcm128_init_ctx`. It returns one on success // and zero otherwise. int CRYPTO_gcm128_aad(const GCM128_KEY *key, GCM128_CONTEXT *ctx, const uint8_t *aad, size_t aad_len); -// CRYPTO_gcm128_encrypt encrypts |len| bytes from |in| to |out|. |key| must be -// the same value that was passed to |CRYPTO_gcm128_init_ctx|. It returns one on +// CRYPTO_gcm128_encrypt encrypts `len` bytes from `in` to `out`. `key` must be +// the same value that was passed to `CRYPTO_gcm128_init_ctx`. It returns one on // success and zero otherwise. int CRYPTO_gcm128_encrypt(const GCM128_KEY *key, GCM128_CONTEXT *ctx, const uint8_t *in, uint8_t *out, size_t len); -// CRYPTO_gcm128_decrypt decrypts |len| bytes from |in| to |out|. |key| must be -// the same value that was passed to |CRYPTO_gcm128_init_ctx|. It returns one on +// CRYPTO_gcm128_decrypt decrypts `len` bytes from `in` to `out`. `key` must be +// the same value that was passed to `CRYPTO_gcm128_init_ctx`. It returns one on // success and zero otherwise. int CRYPTO_gcm128_decrypt(const GCM128_KEY *key, GCM128_CONTEXT *ctx, const uint8_t *in, uint8_t *out, size_t len); // CRYPTO_gcm128_finish calculates the authenticator and compares it against -// |len| bytes of |tag|. |key| must be the same value that was passed to -// |CRYPTO_gcm128_init_ctx|. It returns one on success and zero otherwise. +// `len` bytes of `tag`. `key` must be the same value that was passed to +// `CRYPTO_gcm128_init_ctx`. It returns one on success and zero otherwise. int CRYPTO_gcm128_finish(const GCM128_KEY *key, GCM128_CONTEXT *ctx, const uint8_t *tag, size_t len); -// CRYPTO_gcm128_tag calculates the authenticator and copies it into |tag|. -// The minimum of |len| and 16 bytes are copied into |tag|. |key| must be the -// same value that was passed to |CRYPTO_gcm128_init_ctx|. +// CRYPTO_gcm128_tag calculates the authenticator and copies it into `tag`. +// The minimum of `len` and 16 bytes are copied into `tag`. `key` must be the +// same value that was passed to `CRYPTO_gcm128_init_ctx`. void CRYPTO_gcm128_tag(const GCM128_KEY *key, GCM128_CONTEXT *ctx, uint8_t *tag, size_t len); @@ -528,7 +528,7 @@ extern "C" void aes_gcm_dec_kernel_eor3(const uint8_t *in, uint64_t in_bits, typedef void (*cbc128_f)(const uint8_t *in, uint8_t *out, size_t len, const AES_KEY *key, uint8_t ivec[16], int enc); -// CRYPTO_cbc128_encrypt encrypts |len| bytes from |in| to |out| using the +// CRYPTO_cbc128_encrypt encrypts `len` bytes from `in` to `out` using the // given IV and block cipher in CBC mode. The input need not be a multiple of // 128 bits long, but the output will round up to the nearest 128 bit multiple, // zero padding the input if needed. The IV will be updated on return. @@ -536,10 +536,10 @@ void CRYPTO_cbc128_encrypt(const uint8_t *in, uint8_t *out, size_t len, const AES_KEY *key, uint8_t ivec[16], block128_f block); -// CRYPTO_cbc128_decrypt decrypts |len| bytes from |in| to |out| using the -// given IV and block cipher in CBC mode. If |len| is not a multiple of 128 +// CRYPTO_cbc128_decrypt decrypts `len` bytes from `in` to `out` using the +// given IV and block cipher in CBC mode. If `len` is not a multiple of 128 // bits then only that many bytes will be written, but a multiple of 128 bits -// is always read from |in|. The IV will be updated on return. +// is always read from `in`. The IV will be updated on return. void CRYPTO_cbc128_decrypt(const uint8_t *in, uint8_t *out, size_t len, const AES_KEY *key, uint8_t ivec[16], block128_f block); @@ -548,9 +548,9 @@ void CRYPTO_cbc128_decrypt(const uint8_t *in, uint8_t *out, size_t len, // OFB. // CRYPTO_ofb128_encrypt encrypts (or decrypts, it's the same with OFB mode) -// |len| bytes from |in| to |out| using |block| in OFB mode. There's no -// requirement that |len| be a multiple of any value and any partial blocks are -// stored in |ivec| and |*num|, the latter must be zero before the initial +// `len` bytes from `in` to `out` using `block` in OFB mode. There's no +// requirement that `len` be a multiple of any value and any partial blocks are +// stored in `ivec` and `*num`, the latter must be zero before the initial // call. void CRYPTO_ofb128_encrypt(const uint8_t *in, uint8_t *out, size_t len, const AES_KEY *key, uint8_t ivec[16], unsigned *num, @@ -559,24 +559,24 @@ void CRYPTO_ofb128_encrypt(const uint8_t *in, uint8_t *out, size_t len, // CFB. -// CRYPTO_cfb128_encrypt encrypts (or decrypts, if |enc| is zero) |len| bytes -// from |in| to |out| using |block| in CFB mode. There's no requirement that -// |len| be a multiple of any value and any partial blocks are stored in |ivec| -// and |*num|, the latter must be zero before the initial call. +// CRYPTO_cfb128_encrypt encrypts (or decrypts, if `enc` is zero) `len` bytes +// from `in` to `out` using `block` in CFB mode. There's no requirement that +// `len` be a multiple of any value and any partial blocks are stored in `ivec` +// and `*num`, the latter must be zero before the initial call. void CRYPTO_cfb128_encrypt(const uint8_t *in, uint8_t *out, size_t len, const AES_KEY *key, uint8_t ivec[16], unsigned *num, int enc, block128_f block); -// CRYPTO_cfb128_8_encrypt encrypts (or decrypts, if |enc| is zero) |len| bytes -// from |in| to |out| using |block| in CFB-8 mode. Prior to the first call -// |num| should be set to zero. +// CRYPTO_cfb128_8_encrypt encrypts (or decrypts, if `enc` is zero) `len` bytes +// from `in` to `out` using `block` in CFB-8 mode. Prior to the first call +// `num` should be set to zero. void CRYPTO_cfb128_8_encrypt(const uint8_t *in, uint8_t *out, size_t len, const AES_KEY *key, uint8_t ivec[16], unsigned *num, int enc, block128_f block); -// CRYPTO_cfb128_1_encrypt encrypts (or decrypts, if |enc| is zero) |len| bytes -// from |in| to |out| using |block| in CFB-1 mode. Prior to the first call -// |num| should be set to zero. +// CRYPTO_cfb128_1_encrypt encrypts (or decrypts, if `enc` is zero) `len` bytes +// from `in` to `out` using `block` in CFB-1 mode. Prior to the first call +// `num` should be set to zero. void CRYPTO_cfb128_1_encrypt(const uint8_t *in, uint8_t *out, size_t bits, const AES_KEY *key, uint8_t ivec[16], unsigned *num, int enc, block128_f block); diff --git a/third_party/boringssl/src/crypto/fipsmodule/aes/mode_wrappers.cc.inc b/third_party/boringssl/src/crypto/fipsmodule/aes/mode_wrappers.cc.inc index 34f14b001..5507961e9 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/aes/mode_wrappers.cc.inc +++ b/third_party/boringssl/src/crypto/fipsmodule/aes/mode_wrappers.cc.inc @@ -39,8 +39,8 @@ void AES_ctr128_encrypt(const uint8_t *in, uint8_t *out, size_t len, CRYPTO_ctr128_encrypt_ctr32(in, out, len, key, ivec, ecount_buf, num, aes_hw_ctr32_encrypt_blocks); } else if (vpaes_capable()) { - // TODO(davidben): On ARM, where |BSAES| is additionally defined, this could - // use |vpaes_ctr32_encrypt_blocks_with_bsaes|. + // TODO(davidben): On ARM, where `BSAES` is additionally defined, this could + // use `vpaes_ctr32_encrypt_blocks_with_bsaes`. CRYPTO_ctr128_encrypt_ctr32(in, out, len, key, ivec, ecount_buf, num, vpaes_ctr32_encrypt_blocks); } else { diff --git a/third_party/boringssl/src/crypto/fipsmodule/bcm.cc b/third_party/boringssl/src/crypto/fipsmodule/bcm.cc index 1a5a6fd6c..b0eae679e 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/bcm.cc +++ b/third_party/boringssl/src/crypto/fipsmodule/bcm.cc @@ -102,6 +102,7 @@ OPENSSL_CLANG_PRAGMA("clang diagnostic ignored \"-Wheader-hygiene\"") #include "keccak/keccak.cc.inc" #include "mldsa/mldsa.cc.inc" #include "mlkem/mlkem.cc.inc" +#include "rand/android_entropy_client.cc.inc" #include "rand/ctrdrbg.cc.inc" #include "rand/rand.cc.inc" #include "rsa/padding.cc.inc" @@ -162,7 +163,7 @@ static void assert_within(const void *start, const void *symbol, static void BORINGSSL_maybe_set_module_text_permissions(int permission) { // Android may be compiled in execute-only-memory mode, in which case the // .text segment cannot be read. That conflicts with the need for a FIPS - // module to hash its own contents, therefore |mprotect| is used to make + // module to hash its own contents, therefore `mprotect` is used to make // the module's .text readable for the duration of the hashing process. In // other build configurations this is a no-op. const uintptr_t page_size = getpagesize(); @@ -181,8 +182,8 @@ static void BORINGSSL_maybe_set_module_text_permissions(int permission) {} #endif // !ASAN -static void - __attribute__((constructor)) BORINGSSL_bcm_power_on_self_test(void) { +static void __attribute__((constructor)) BORINGSSL_bcm_power_on_self_test( + void) { #if !defined(OPENSSL_ASAN) // Integrity tests cannot run under ASAN because it involves reading the full // .text section, which triggers the global-buffer overflow detection. diff --git a/third_party/boringssl/src/crypto/fipsmodule/bcm_interface.h b/third_party/boringssl/src/crypto/fipsmodule/bcm_interface.h index d4ea95d5c..66f32aa92 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/bcm_interface.h +++ b/third_party/boringssl/src/crypto/fipsmodule/bcm_interface.h @@ -73,54 +73,48 @@ inline bcm_status_t bcm_as_not_approved_status(int result) { #endif // BORINGSSL_FIPS -// BCM_rand_load_entropy supplies |entropy_len| bytes of entropy to the BCM -// module. The |want_additional_input| parameter is true iff the entropy was -// obtained from a source other than the system, e.g. directly from the CPU. -bcm_infallible BCM_rand_load_entropy(const uint8_t *entropy, size_t entropy_len, - int want_additional_input); - -// BCM_rand_bytes is the same as the public |RAND_bytes| function, other +// BCM_rand_bytes is the same as the public `RAND_bytes` function, other // than returning a bcm_infallible status indicator. bcm_infallible BCM_rand_bytes(uint8_t *out, size_t out_len); -// BCM_rand_bytes_hwrng attempts to fill |out| with |len| bytes of entropy from +// BCM_rand_bytes_hwrng attempts to fill `out` with `len` bytes of entropy from // the CPU hardware random number generator if one is present. // bcm_status_approved is returned on success, and a failure status is // returned otherwise. bcm_status BCM_rand_bytes_hwrng(uint8_t *out, size_t len); // BCM_rand_bytes_with_additional_data samples from the RNG after mixing 32 -// bytes from |user_additional_data| in. +// bytes from `user_additional_data` in. bcm_infallible BCM_rand_bytes_with_additional_data( uint8_t *out, size_t out_len, const uint8_t user_additional_data[32]); // SHA-1 -// BCM_sha1_init initialises |sha|. +// BCM_sha1_init initialises `sha`. bcm_infallible BCM_sha1_init(SHA_CTX *sha); // SHA1_transform is a low-level function that performs a single, SHA-1 -// block transformation using the state from |sha| and |SHA_CBLOCK| bytes from -// |block|. +// block transformation using the state from `sha` and `SHA_CBLOCK` bytes from +// `block`. bcm_infallible BCM_sha1_transform(SHA_CTX *c, const uint8_t data[SHA_CBLOCK]); -// BCM_sha1_update adds |len| bytes from |data| to |sha|. +// BCM_sha1_update adds `len` bytes from `data` to `sha`. bcm_infallible BCM_sha1_update(SHA_CTX *c, const void *data, size_t len); -// BCM_sha1_final adds the final padding to |sha| and writes the resulting -// digest to |out|, which must have at least |SHA_DIGEST_LENGTH| bytes of space. +// BCM_sha1_final adds the final padding to `sha` and writes the resulting +// digest to `out`, which must have at least `SHA_DIGEST_LENGTH` bytes of space. bcm_infallible BCM_sha1_final(uint8_t out[SHA_DIGEST_LENGTH], SHA_CTX *c); -// BCM_fips_186_2_prf derives |out_len| bytes from |xkey| using the PRF +// BCM_fips_186_2_prf derives `out_len` bytes from `xkey` using the PRF // defined in FIPS 186-2, Appendix 3.1, with change notice 1 applied. The b // parameter is 160 and seed, XKEY, is also 160 bits. The optional XSEED user // input is all zeros. // // The PRF generates a sequence of 320-bit numbers. Each number is encoded as a -// 40-byte string in big-endian and then concatenated to form |out|. If -// |out_len| is not a multiple of 40, the result is truncated. This matches the +// 40-byte string in big-endian and then concatenated to form `out`. If +// `out_len` is not a multiple of 40, the result is truncated. This matches the // construction used in Section 7 of RFC 4186 and Section 7 of RFC 4187. // // This PRF is based on SHA-1, a weak hash function, and should not be used @@ -132,14 +126,14 @@ bcm_infallible BCM_fips_186_2_prf(uint8_t *out, size_t out_len, // SHA-224 -// BCM_sha224_unit initialises |sha|. +// BCM_sha224_unit initialises `sha`. bcm_infallible BCM_sha224_init(SHA256_CTX *sha); -// BCM_sha224_update adds |len| bytes from |data| to |sha|. +// BCM_sha224_update adds `len` bytes from `data` to `sha`. bcm_infallible BCM_sha224_update(SHA256_CTX *sha, const void *data, size_t len); -// BCM_sha224_final adds the final padding to |sha| and writes the resulting -// digest to |out|, which must have at least |SHA224_DIGEST_LENGTH| bytes of +// BCM_sha224_final adds the final padding to `sha` and writes the resulting +// digest to `out`, which must have at least `SHA224_DIGEST_LENGTH` bytes of // space. It aborts on programmer error. bcm_infallible BCM_sha224_final(uint8_t out[SHA224_DIGEST_LENGTH], SHA256_CTX *sha); @@ -147,27 +141,27 @@ bcm_infallible BCM_sha224_final(uint8_t out[SHA224_DIGEST_LENGTH], // SHA-256 -// BCM_sha256_init initialises |sha|. +// BCM_sha256_init initialises `sha`. bcm_infallible BCM_sha256_init(SHA256_CTX *sha); -// BCM_sha256_update adds |len| bytes from |data| to |sha|. +// BCM_sha256_update adds `len` bytes from `data` to `sha`. bcm_infallible BCM_sha256_update(SHA256_CTX *sha, const void *data, size_t len); -// BCM_sha256_final adds the final padding to |sha| and writes the resulting -// digest to |out|, which must have at least |SHA256_DIGEST_LENGTH| bytes of +// BCM_sha256_final adds the final padding to `sha` and writes the resulting +// digest to `out`, which must have at least `SHA256_DIGEST_LENGTH` bytes of // space. It aborts on programmer error. bcm_infallible BCM_sha256_final(uint8_t out[SHA256_DIGEST_LENGTH], SHA256_CTX *sha); // BCM_sha256_transform is a low-level function that performs a single, SHA-256 -// block transformation using the state from |sha| and |SHA256_CBLOCK| bytes -// from |block|. +// block transformation using the state from `sha` and `SHA256_CBLOCK` bytes +// from `block`. bcm_infallible BCM_sha256_transform(SHA256_CTX *sha, const uint8_t block[SHA256_CBLOCK]); -// BCM_sha256_transform_blocks is a low-level function that takes |num_blocks| * -// |SHA256_CBLOCK| bytes of data and performs SHA-256 transforms on it to update -// |state|. +// BCM_sha256_transform_blocks is a low-level function that takes `num_blocks` * +// `SHA256_CBLOCK` bytes of data and performs SHA-256 transforms on it to update +// `state`. bcm_infallible BCM_sha256_transform_blocks(uint32_t state[8], const uint8_t *data, size_t num_blocks); @@ -175,14 +169,14 @@ bcm_infallible BCM_sha256_transform_blocks(uint32_t state[8], // SHA-384. -// BCM_sha384_init initialises |sha|. +// BCM_sha384_init initialises `sha`. bcm_infallible BCM_sha384_init(SHA512_CTX *sha); -// BCM_sha384_update adds |len| bytes from |data| to |sha|. +// BCM_sha384_update adds `len` bytes from `data` to `sha`. bcm_infallible BCM_sha384_update(SHA512_CTX *sha, const void *data, size_t len); -// BCM_sha384_final adds the final padding to |sha| and writes the resulting -// digest to |out|, which must have at least |SHA384_DIGEST_LENGTH| bytes of +// BCM_sha384_final adds the final padding to `sha` and writes the resulting +// digest to `out`, which must have at least `SHA384_DIGEST_LENGTH` bytes of // space. It may abort on programmer error. bcm_infallible BCM_sha384_final(uint8_t out[SHA384_DIGEST_LENGTH], SHA512_CTX *sha); @@ -190,21 +184,21 @@ bcm_infallible BCM_sha384_final(uint8_t out[SHA384_DIGEST_LENGTH], // SHA-512. -// BCM_sha512_init initialises |sha|. +// BCM_sha512_init initialises `sha`. bcm_infallible BCM_sha512_init(SHA512_CTX *sha); -// BCM_sha512_update adds |len| bytes from |data| to |sha|. +// BCM_sha512_update adds `len` bytes from `data` to `sha`. bcm_infallible BCM_sha512_update(SHA512_CTX *sha, const void *data, size_t len); -// BCM_sha512_final adds the final padding to |sha| and writes the resulting -// digest to |out|, which must have at least |SHA512_DIGEST_LENGTH| bytes of +// BCM_sha512_final adds the final padding to `sha` and writes the resulting +// digest to `out`, which must have at least `SHA512_DIGEST_LENGTH` bytes of // space. bcm_infallible BCM_sha512_final(uint8_t out[SHA512_DIGEST_LENGTH], SHA512_CTX *sha); // BCM_sha512_transform is a low-level function that performs a single, SHA-512 -// block transformation using the state from |sha| and |SHA512_CBLOCK| bytes -// from |block|. +// block transformation using the state from `sha` and `SHA512_CBLOCK` bytes +// from `block`. bcm_infallible BCM_sha512_transform(SHA512_CTX *sha, const uint8_t block[SHA512_CBLOCK]); @@ -213,15 +207,15 @@ bcm_infallible BCM_sha512_transform(SHA512_CTX *sha, // // See https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf section 5.3.6 -// BCM_sha512_256_init initialises |sha|. +// BCM_sha512_256_init initialises `sha`. bcm_infallible BCM_sha512_256_init(SHA512_CTX *sha); -// BCM_sha512_256_update adds |len| bytes from |data| to |sha|. +// BCM_sha512_256_update adds `len` bytes from `data` to `sha`. bcm_infallible BCM_sha512_256_update(SHA512_CTX *sha, const void *data, size_t len); -// BCM_sha512_256_final adds the final padding to |sha| and writes the resulting -// digest to |out|, which must have at least |SHA512_256_DIGEST_LENGTH| bytes of +// BCM_sha512_256_final adds the final padding to `sha` and writes the resulting +// digest to `out`, which must have at least `SHA512_256_DIGEST_LENGTH` bytes of // space. It may abort on programmer error. bcm_infallible BCM_sha512_256_final(uint8_t out[SHA512_256_DIGEST_LENGTH], SHA512_CTX *sha); @@ -250,7 +244,7 @@ OPENSSL_EXPORT bcm_status BCM_mldsa65_private_key_from_seed( OPENSSL_EXPORT bcm_status BCM_mldsa65_public_from_private( MLDSA65_public_key *out_public_key, const MLDSA65_private_key *private_key); -// BCM_mldsa65_public_of_private returns the public half of |private_key|. +// BCM_mldsa65_public_of_private returns the public half of `private_key`. const MLDSA65_public_key *BCM_mldsa65_public_of_private( const MLDSA65_private_key *private_key); @@ -305,7 +299,7 @@ BCM_mldsa65_parse_private_key(MLDSA65_private_key *private_key, CBS *in); // BCM_mldsa65_generate_key_external_entropy generates a public/private key pair // using the given seed, writes the encoded public key to -// |out_encoded_public_key| and sets |out_private_key| to the private key. +// `out_encoded_public_key` and sets `out_private_key` to the private key. OPENSSL_EXPORT bcm_status BCM_mldsa65_generate_key_external_entropy( uint8_t out_encoded_public_key[MLDSA65_PUBLIC_KEY_BYTES], MLDSA65_private_key *out_private_key, @@ -316,11 +310,11 @@ OPENSSL_EXPORT bcm_status BCM_mldsa65_generate_key_external_entropy_fips( MLDSA65_private_key *out_private_key, const uint8_t entropy[MLDSA_SEED_BYTES]); -// BCM_mldsa5_sign_internal signs |msg| using |private_key| and writes the -// signature to |out_encoded_signature|. The |context_prefix| and |context| are -// prefixed to the message, in that order, before signing. The |randomizer| +// BCM_mldsa5_sign_internal signs `msg` using `private_key` and writes the +// signature to `out_encoded_signature`. The `context_prefix` and `context` are +// prefixed to the message, in that order, before signing. The `randomizer` // value can be set to zero bytes in order to make a deterministic signature, or -// else filled with entropy for the usual |MLDSA_sign| behavior. +// else filled with entropy for the usual `MLDSA_sign` behavior. OPENSSL_EXPORT bcm_status BCM_mldsa65_sign_internal( uint8_t out_encoded_signature[MLDSA65_SIGNATURE_BYTES], const MLDSA65_private_key *private_key, const uint8_t *msg, size_t msg_len, @@ -334,8 +328,8 @@ OPENSSL_EXPORT bcm_status BCM_mldsa65_sign_mu_internal( const uint8_t msg_rep[MLDSA_MU_BYTES], const uint8_t randomizer[BCM_MLDSA_SIGNATURE_RANDOMIZER_BYTES]); -// BCM_mldsa5_verify_internal verifies that |encoded_signature| is a valid -// signature of |msg| by |public_key|. The |context_prefix| and |context| are +// BCM_mldsa5_verify_internal verifies that `encoded_signature` is a valid +// signature of `msg` by `public_key`. The `context_prefix` and `context` are // prefixed to the message before verification, in that order. OPENSSL_EXPORT bcm_status BCM_mldsa65_verify_internal( const MLDSA65_public_key *public_key, @@ -343,12 +337,12 @@ OPENSSL_EXPORT bcm_status BCM_mldsa65_verify_internal( const uint8_t *msg, size_t msg_len, const uint8_t *context_prefix, size_t context_prefix_len, const uint8_t *context, size_t context_len); -// BCM_mldsa65_marshal_private_key serializes |private_key| to |out| in the +// BCM_mldsa65_marshal_private_key serializes `private_key` to `out` in the // NIST format for ML-DSA-65 private keys. OPENSSL_EXPORT bcm_status BCM_mldsa65_marshal_private_key( CBB *out, const MLDSA65_private_key *private_key); -// BCM_mldsa65_public_keys_equal returns one if |a| and |b| are equal and zero +// BCM_mldsa65_public_keys_equal returns one if `a` and `b` are equal and zero // otherwise. int BCM_mldsa65_public_keys_equal(const MLDSA65_public_key *a, const MLDSA65_public_key *b); @@ -368,7 +362,7 @@ OPENSSL_EXPORT bcm_status BCM_mldsa87_private_key_from_seed( OPENSSL_EXPORT bcm_status BCM_mldsa87_public_from_private( MLDSA87_public_key *out_public_key, const MLDSA87_private_key *private_key); -// BCM_mldsa87_public_of_private returns the public half of |private_key|. +// BCM_mldsa87_public_of_private returns the public half of `private_key`. const MLDSA87_public_key *BCM_mldsa87_public_of_private( const MLDSA87_private_key *private_key); @@ -423,7 +417,7 @@ BCM_mldsa87_parse_private_key(MLDSA87_private_key *private_key, CBS *in); // BCM_mldsa87_generate_key_external_entropy generates a public/private key pair // using the given seed, writes the encoded public key to -// |out_encoded_public_key| and sets |out_private_key| to the private key. +// `out_encoded_public_key` and sets `out_private_key` to the private key. OPENSSL_EXPORT bcm_status BCM_mldsa87_generate_key_external_entropy( uint8_t out_encoded_public_key[MLDSA87_PUBLIC_KEY_BYTES], MLDSA87_private_key *out_private_key, @@ -434,11 +428,11 @@ OPENSSL_EXPORT bcm_status BCM_mldsa87_generate_key_external_entropy_fips( MLDSA87_private_key *out_private_key, const uint8_t entropy[MLDSA_SEED_BYTES]); -// BCM_mldsa87_sign_internal signs |msg| using |private_key| and writes the -// signature to |out_encoded_signature|. The |context_prefix| and |context| are -// prefixed to the message, in that order, before signing. The |randomizer| +// BCM_mldsa87_sign_internal signs `msg` using `private_key` and writes the +// signature to `out_encoded_signature`. The `context_prefix` and `context` are +// prefixed to the message, in that order, before signing. The `randomizer` // value can be set to zero bytes in order to make a deterministic signature, or -// else filled with entropy for the usual |MLDSA_sign| behavior. +// else filled with entropy for the usual `MLDSA_sign` behavior. OPENSSL_EXPORT bcm_status BCM_mldsa87_sign_internal( uint8_t out_encoded_signature[MLDSA87_SIGNATURE_BYTES], const MLDSA87_private_key *private_key, const uint8_t *msg, size_t msg_len, @@ -452,8 +446,8 @@ OPENSSL_EXPORT bcm_status BCM_mldsa87_sign_mu_internal( const uint8_t msg_rep[MLDSA_MU_BYTES], const uint8_t randomizer[BCM_MLDSA_SIGNATURE_RANDOMIZER_BYTES]); -// BCM_mldsa87_verify_internal verifies that |encoded_signature| is a valid -// signature of |msg| by |public_key|. The |context_prefix| and |context| are +// BCM_mldsa87_verify_internal verifies that `encoded_signature` is a valid +// signature of `msg` by `public_key`. The `context_prefix` and `context` are // prefixed to the message before verification, in that order. OPENSSL_EXPORT bcm_status BCM_mldsa87_verify_internal( const MLDSA87_public_key *public_key, @@ -461,12 +455,12 @@ OPENSSL_EXPORT bcm_status BCM_mldsa87_verify_internal( const uint8_t *msg, size_t msg_len, const uint8_t *context_prefix, size_t context_prefix_len, const uint8_t *context, size_t context_len); -// BCM_mldsa87_marshal_private_key serializes |private_key| to |out| in the +// BCM_mldsa87_marshal_private_key serializes `private_key` to `out` in the // NIST format for ML-DSA-87 private keys. OPENSSL_EXPORT bcm_status BCM_mldsa87_marshal_private_key( CBB *out, const MLDSA87_private_key *private_key); -// BCM_mldsa87_public_keys_equal returns one if |a| and |b| are equal and zero +// BCM_mldsa87_public_keys_equal returns one if `a` and `b` are equal and zero // otherwise. int BCM_mldsa87_public_keys_equal(const MLDSA87_public_key *a, const MLDSA87_public_key *b); @@ -485,7 +479,7 @@ OPENSSL_EXPORT bcm_status BCM_mldsa44_private_key_from_seed( OPENSSL_EXPORT bcm_status BCM_mldsa44_public_from_private( MLDSA44_public_key *out_public_key, const MLDSA44_private_key *private_key); -// BCM_mldsa44_public_of_private returns the public half of |private_key|. +// BCM_mldsa44_public_of_private returns the public half of `private_key`. const MLDSA44_public_key *BCM_mldsa44_public_of_private( const MLDSA44_private_key *private_key); @@ -540,7 +534,7 @@ BCM_mldsa44_parse_private_key(MLDSA44_private_key *private_key, CBS *in); // BCM_mldsa44_generate_key_external_entropy generates a public/private key pair // using the given seed, writes the encoded public key to -// |out_encoded_public_key| and sets |out_private_key| to the private key. +// `out_encoded_public_key` and sets `out_private_key` to the private key. OPENSSL_EXPORT bcm_status BCM_mldsa44_generate_key_external_entropy( uint8_t out_encoded_public_key[MLDSA44_PUBLIC_KEY_BYTES], MLDSA44_private_key *out_private_key, @@ -551,11 +545,11 @@ OPENSSL_EXPORT bcm_status BCM_mldsa44_generate_key_external_entropy_fips( MLDSA44_private_key *out_private_key, const uint8_t entropy[MLDSA_SEED_BYTES]); -// BCM_mldsa44_sign_internal signs |msg| using |private_key| and writes the -// signature to |out_encoded_signature|. The |context_prefix| and |context| are -// prefixed to the message, in that order, before signing. The |randomizer| +// BCM_mldsa44_sign_internal signs `msg` using `private_key` and writes the +// signature to `out_encoded_signature`. The `context_prefix` and `context` are +// prefixed to the message, in that order, before signing. The `randomizer` // value can be set to zero bytes in order to make a deterministic signature, or -// else filled with entropy for the usual |MLDSA_sign| behavior. +// else filled with entropy for the usual `MLDSA_sign` behavior. OPENSSL_EXPORT bcm_status BCM_mldsa44_sign_internal( uint8_t out_encoded_signature[MLDSA44_SIGNATURE_BYTES], const MLDSA44_private_key *private_key, const uint8_t *msg, size_t msg_len, @@ -569,8 +563,8 @@ OPENSSL_EXPORT bcm_status BCM_mldsa44_sign_mu_internal( const uint8_t msg_rep[MLDSA_MU_BYTES], const uint8_t randomizer[BCM_MLDSA_SIGNATURE_RANDOMIZER_BYTES]); -// BCM_mldsa44_verify_internal verifies that |encoded_signature| is a valid -// signature of |msg| by |public_key|. The |context_prefix| and |context| are +// BCM_mldsa44_verify_internal verifies that `encoded_signature` is a valid +// signature of `msg` by `public_key`. The `context_prefix` and `context` are // prefixed to the message before verification, in that order. OPENSSL_EXPORT bcm_status BCM_mldsa44_verify_internal( const MLDSA44_public_key *public_key, @@ -578,12 +572,12 @@ OPENSSL_EXPORT bcm_status BCM_mldsa44_verify_internal( const uint8_t *msg, size_t msg_len, const uint8_t *context_prefix, size_t context_prefix_len, const uint8_t *context, size_t context_len); -// BCM_mldsa44_marshal_private_key serializes |private_key| to |out| in the +// BCM_mldsa44_marshal_private_key serializes `private_key` to `out` in the // NIST format for ML-DSA-44 private keys. OPENSSL_EXPORT bcm_status BCM_mldsa44_marshal_private_key( CBB *out, const MLDSA44_private_key *private_key); -// BCM_mldsa44_public_keys_equal returns one if |a| and |b| are equal and zero +// BCM_mldsa44_public_keys_equal returns one if `a` and `b` are equal and zero // otherwise. int BCM_mldsa44_public_keys_equal(const MLDSA44_public_key *a, const MLDSA44_public_key *b); @@ -600,11 +594,11 @@ int BCM_mldsa44_public_keys_equal(const MLDSA44_public_key *a, #define BCM_MLKEM_ENCAP_ENTROPY 32 // BCM_MLKEM768_PRIVATE_KEY_BYTES is the length of the data produced by -// |BCM_mlkem768_marshal_private_key|. +// `BCM_mlkem768_marshal_private_key`. #define BCM_MLKEM768_PRIVATE_KEY_BYTES 2400 // BCM_MLKEM1024_PRIVATE_KEY_BYTES is the length of the data produced by -// |BCM_mlkem1024_marshal_private_key|. +// `BCM_mlkem1024_marshal_private_key`. #define BCM_MLKEM1024_PRIVATE_KEY_BYTES 3168 OPENSSL_EXPORT bcm_infallible BCM_mlkem768_generate_key( @@ -644,7 +638,7 @@ BCM_mlkem768_decap(uint8_t out_shared_secret[MLKEM_SHARED_SECRET_BYTES], OPENSSL_EXPORT bcm_status BCM_mlkem768_marshal_public_key( CBB *out, const MLKEM768_public_key *public_key); -// BCM_mlkem768_public_keys_equal returns one if |a| and |b| are equal and zero +// BCM_mlkem768_public_keys_equal returns one if `a` and `b` are equal and zero // otherwise. int BCM_mlkem768_public_keys_equal(const MLKEM768_public_key *a, const MLKEM768_public_key *b); @@ -653,37 +647,37 @@ OPENSSL_EXPORT bcm_status BCM_mlkem768_parse_public_key(MLKEM768_public_key *out_public_key, CBS *in); // BCM_mlkem768_parse_private_key parses a private key, in NIST's format for -// private keys, from |in| and writes the result to |out_private_key|. It +// private keys, from `in` and writes the result to `out_private_key`. It // returns one on success or zero on parse error or if there are trailing bytes -// in |in|. This format is verbose and should be avoided. Private keys should be -// stored as seeds and parsed using |BCM_mlkem768_private_key_from_seed|. +// in `in`. This format is verbose and should be avoided. Private keys should be +// stored as seeds and parsed using `BCM_mlkem768_private_key_from_seed`. OPENSSL_EXPORT bcm_status BCM_mlkem768_parse_private_key(MLKEM768_private_key *out_private_key, CBS *in); // BCM_mlkem768_generate_key_external_seed is a deterministic function to create // a pair of ML-KEM-768 keys, using the supplied seed. The seed needs to be // uniformly random. This function should only be used for tests; regular -// callers should use the non-deterministic |BCM_mlkem768_generate_key| +// callers should use the non-deterministic `BCM_mlkem768_generate_key` // directly. OPENSSL_EXPORT bcm_infallible BCM_mlkem768_generate_key_external_seed( uint8_t out_encoded_public_key[MLKEM768_PUBLIC_KEY_BYTES], MLKEM768_private_key *out_private_key, const uint8_t seed[MLKEM_SEED_BYTES]); -// BCM_mlkem768_encap_external_entropy behaves like |MLKEM768_encap|, but uses -// |MLKEM_ENCAP_ENTROPY| bytes of |entropy| for randomization. The decapsulating -// side will be able to recover |entropy| in full. This function should only be +// BCM_mlkem768_encap_external_entropy behaves like `MLKEM768_encap`, but uses +// `MLKEM_ENCAP_ENTROPY` bytes of `entropy` for randomization. The decapsulating +// side will be able to recover `entropy` in full. This function should only be // used for tests, regular callers should use the non-deterministic -// |BCM_mlkem768_encap| directly. +// `BCM_mlkem768_encap` directly. OPENSSL_EXPORT bcm_infallible BCM_mlkem768_encap_external_entropy( uint8_t out_ciphertext[MLKEM768_CIPHERTEXT_BYTES], uint8_t out_shared_secret[MLKEM_SHARED_SECRET_BYTES], const MLKEM768_public_key *public_key, const uint8_t entropy[BCM_MLKEM_ENCAP_ENTROPY]); -// BCM_mlkem768_marshal_private_key serializes |private_key| to |out| in the +// BCM_mlkem768_marshal_private_key serializes `private_key` to `out` in the // NIST format for ML-KEM-768 private keys. (Note that one can also save just -// the seed value produced by |BCM_mlkem768_generate_key|, which is +// the seed value produced by `BCM_mlkem768_generate_key`, which is // significantly smaller.) OPENSSL_EXPORT bcm_status BCM_mlkem768_marshal_private_key( CBB *out, const MLKEM768_private_key *private_key); @@ -725,7 +719,7 @@ BCM_mlkem1024_decap(uint8_t out_shared_secret[MLKEM_SHARED_SECRET_BYTES], OPENSSL_EXPORT bcm_status BCM_mlkem1024_marshal_public_key( CBB *out, const MLKEM1024_public_key *public_key); -// BCM_mlkem1024_public_keys_equal returns one if |a| and |b| are equal and zero +// BCM_mlkem1024_public_keys_equal returns one if `a` and `b` are equal and zero // otherwise. int BCM_mlkem1024_public_keys_equal(const MLKEM1024_public_key *a, const MLKEM1024_public_key *b); @@ -734,37 +728,37 @@ OPENSSL_EXPORT bcm_status BCM_mlkem1024_parse_public_key(MLKEM1024_public_key *out_public_key, CBS *in); // BCM_mlkem1024_parse_private_key parses a private key, in NIST's format for -// private keys, from |in| and writes the result to |out_private_key|. It +// private keys, from `in` and writes the result to `out_private_key`. It // returns one on success or zero on parse error or if there are trailing bytes -// in |in|. This format is verbose and should be avoided. Private keys should be -// stored as seeds and parsed using |BCM_mlkem1024_private_key_from_seed|. +// in `in`. This format is verbose and should be avoided. Private keys should be +// stored as seeds and parsed using `BCM_mlkem1024_private_key_from_seed`. OPENSSL_EXPORT bcm_status BCM_mlkem1024_parse_private_key( MLKEM1024_private_key *out_private_key, CBS *in); // BCM_mlkem1024_generate_key_external_seed is a deterministic function to // create a pair of ML-KEM-1024 keys, using the supplied seed. The seed needs to // be uniformly random. This function should only be used for tests, regular -// callers should use the non-deterministic |BCM_mlkem1024_generate_key| +// callers should use the non-deterministic `BCM_mlkem1024_generate_key` // directly. OPENSSL_EXPORT bcm_infallible BCM_mlkem1024_generate_key_external_seed( uint8_t out_encoded_public_key[MLKEM1024_PUBLIC_KEY_BYTES], MLKEM1024_private_key *out_private_key, const uint8_t seed[MLKEM_SEED_BYTES]); -// BCM_mlkem1024_encap_external_entropy behaves like |MLKEM1024_encap|, but uses -// |MLKEM_ENCAP_ENTROPY| bytes of |entropy| for randomization. The -// decapsulating side will be able to recover |entropy| in full. This function +// BCM_mlkem1024_encap_external_entropy behaves like `MLKEM1024_encap`, but uses +// `MLKEM_ENCAP_ENTROPY` bytes of `entropy` for randomization. The +// decapsulating side will be able to recover `entropy` in full. This function // should only be used for tests, regular callers should use the -// non-deterministic |BCM_mlkem1024_encap| directly. +// non-deterministic `BCM_mlkem1024_encap` directly. OPENSSL_EXPORT bcm_infallible BCM_mlkem1024_encap_external_entropy( uint8_t out_ciphertext[MLKEM1024_CIPHERTEXT_BYTES], uint8_t out_shared_secret[MLKEM_SHARED_SECRET_BYTES], const MLKEM1024_public_key *public_key, const uint8_t entropy[BCM_MLKEM_ENCAP_ENTROPY]); -// BCM_mlkem1024_marshal_private_key serializes |private_key| to |out| in the +// BCM_mlkem1024_marshal_private_key serializes `private_key` to `out` in the // NIST format for ML-KEM-1024 private keys. (Note that one can also save just -// the seed value produced by |BCM_mlkem1024_generate_key|, which is +// the seed value produced by `BCM_mlkem1024_generate_key`, which is // significantly smaller.) OPENSSL_EXPORT bcm_status BCM_mlkem1024_marshal_private_key( CBB *out, const MLKEM1024_private_key *private_key); @@ -796,8 +790,8 @@ OPENSSL_EXPORT bcm_status BCM_mlkem1024_marshal_private_key( #define BCM_SLHDSA_SHAKE_256F_SIGNATURE_BYTES 49856 // BCM_slhdsa_sha2_128s_generate_key_from_seed generates an SLH-DSA-SHA2-128s -// key pair from a 48-byte seed and writes the result to |out_public_key| and -// |out_secret_key|. +// key pair from a 48-byte seed and writes the result to `out_public_key` and +// `out_secret_key`. OPENSSL_EXPORT bcm_infallible BCM_slhdsa_sha2_128s_generate_key_from_seed( uint8_t out_public_key[BCM_SLHDSA_SHA2_128S_PUBLIC_KEY_BYTES], uint8_t out_secret_key[BCM_SLHDSA_SHA2_128S_PRIVATE_KEY_BYTES], @@ -821,11 +815,11 @@ OPENSSL_EXPORT bcm_status BCM_slhdsa_shake_256f_generate_key_from_seed_fips( uint8_t out_secret_key[BCM_SLHDSA_SHAKE_256F_PRIVATE_KEY_BYTES], const uint8_t seed[3 * BCM_SLHDSA_SHAKE_256F_N]); -// BCM_slhdsa_sha2_128s_sign_internal acts like |SLHDSA_SHA2_128S_sign| but +// BCM_slhdsa_sha2_128s_sign_internal acts like `SLHDSA_SHA2_128S_sign` but // accepts an explicit entropy input, which can be PK.seed (bytes 32..48 of // the private key) to generate deterministic signatures. It also takes the // input message in three parts so that the "internal" version of the signing -// function, from section 9.2, can be implemented. The |header| argument may be +// function, from section 9.2, can be implemented. The `header` argument may be // NULL to omit it. OPENSSL_EXPORT bcm_infallible BCM_slhdsa_sha2_128s_sign_internal( uint8_t out_signature[BCM_SLHDSA_SHA2_128S_SIGNATURE_BYTES], @@ -841,9 +835,9 @@ OPENSSL_EXPORT bcm_infallible BCM_slhdsa_shake_256f_sign_internal( size_t context_len, const uint8_t *msg, size_t msg_len, const uint8_t entropy[BCM_SLHDSA_SHAKE_256F_N]); -// BCM_slhdsa_sha2_128s_verify_internal acts like |SLHDSA_SHA2_128S_verify| but +// BCM_slhdsa_sha2_128s_verify_internal acts like `SLHDSA_SHA2_128S_verify` but // takes the input message in three parts so that the "internal" version of the -// verification function, from section 9.3, can be implemented. The |header| +// verification function, from section 9.3, can be implemented. The `header` // argument may be NULL to omit it. OPENSSL_EXPORT bcm_status BCM_slhdsa_sha2_128s_verify_internal( const uint8_t *signature, size_t signature_len, @@ -932,24 +926,24 @@ OPENSSL_EXPORT bcm_status BCM_slhdsa_shake_256f_prehash_verify( // AES -// BCM_aes_encrypt encrypts a single block from |in| to |out| with |key|. The -// |in| and |out| pointers may overlap. +// BCM_aes_encrypt encrypts a single block from `in` to `out` with `key`. The +// `in` and `out` pointers may overlap. bcm_infallible BCM_aes_encrypt(const uint8_t *in, uint8_t *out, const AES_KEY *key); -// BCM_aes_decrypt decrypts a single block from |in| to |out| with |key|. The -// |in| and |out| pointers may overlap. +// BCM_aes_decrypt decrypts a single block from `in` to `out` with `key`. The +// `in` and `out` pointers may overlap. bcm_infallible BCM_aes_decrypt(const uint8_t *in, uint8_t *out, const AES_KEY *key); -// BCM_aes_set_encrypt_key configures |aeskey| to encrypt with the |bits|-bit -// key, |key|. |key| must point to |bits|/8 bytes. It will return failure if -// |bits| is an invalid AES key size. +// BCM_aes_set_encrypt_key configures `aeskey` to encrypt with the `bits`-bit +// key, `key`. `key` must point to `bits`/8 bytes. It will return failure if +// `bits` is an invalid AES key size. bcm_status BCM_aes_set_encrypt_key(const uint8_t *key, unsigned bits, AES_KEY *aeskey); -// BCM_aes_set_decrypt_key configures |aeskey| to decrypt with the |bits|-bit -// key, |key|. |key| must point to |bits|/8 bytes. It will return failure if -// |bits| is an invalid AES key size. +// BCM_aes_set_decrypt_key configures `aeskey` to decrypt with the `bits`-bit +// key, `key`. `key` must point to `bits`/8 bytes. It will return failure if +// `bits` is an invalid AES key size. bcm_status BCM_aes_set_decrypt_key(const uint8_t *key, unsigned bits, AES_KEY *aeskey); diff --git a/third_party/boringssl/src/crypto/fipsmodule/bn/add.cc.inc b/third_party/boringssl/src/crypto/fipsmodule/bn/add.cc.inc index 38c7d5b3d..6910089c0 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/bn/add.cc.inc +++ b/third_party/boringssl/src/crypto/fipsmodule/bn/add.cc.inc @@ -25,6 +25,14 @@ using namespace bssl; +BN_ULONG bssl::bn_add_carry_words(BN_ULONG *rp, const BN_ULONG *ap, + BN_ULONG carry, size_t num) { + for (size_t i = 0; i < num; i++) { + rp[i] = CRYPTO_addc_w(ap[i], 0, carry, &carry); + } + return carry; +} + int BN_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b) { const BIGNUM *tmp; int a_neg = a->neg, ret; @@ -62,7 +70,7 @@ int BN_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b) { } int bssl::bn_uadd_consttime(BIGNUM *r, const BIGNUM *a, const BIGNUM *b) { - // Widths are public, so we normalize to make |a| the larger one. + // Widths are public, so we normalize to make `a` the larger one. if (a->width < b->width) { const BIGNUM *tmp = a; a = b; @@ -77,10 +85,7 @@ int bssl::bn_uadd_consttime(BIGNUM *r, const BIGNUM *a, const BIGNUM *b) { r->width = max + 1; BN_ULONG carry = bn_add_words(r->d, a->d, b->d, min); - for (int i = min; i < max; i++) { - r->d[i] = CRYPTO_addc_w(a->d[i], 0, carry, &carry); - } - + carry = bn_add_carry_words(r->d + min, a->d + min, carry, max - min); r->d[max] = carry; return 1; } @@ -182,8 +187,8 @@ int BN_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b) { } int bssl::bn_usub_consttime(BIGNUM *r, const BIGNUM *a, const BIGNUM *b) { - // |b| may have more words than |a| given non-minimal inputs, but all words - // beyond |a->width| must then be zero. + // `b` may have more words than `a` given non-minimal inputs, but all words + // beyond `a->width` must then be zero. int b_width = b->width; if (b_width > a->width) { if (!bn_fits_in_words(b, a->width)) { diff --git a/third_party/boringssl/src/crypto/fipsmodule/bn/asm/x86_64-gcc.cc.inc b/third_party/boringssl/src/crypto/fipsmodule/bn/asm/x86_64-gcc.cc.inc index 920cba2d7..aae589e0d 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/bn/asm/x86_64-gcc.cc.inc +++ b/third_party/boringssl/src/crypto/fipsmodule/bn/asm/x86_64-gcc.cc.inc @@ -105,13 +105,13 @@ using namespace bssl; (carry) = high; \ } while (0) -// r0:r1:carry = r0:r1 + a^2 + carry:0 +// r0:r1:carry = r0:r1 + a² + carry:0 #define sqr_add(r0, r1, a, carry) \ do { \ BN_ULONG high, low; \ - /* lo:hi = a^2 */ \ + /* lo:hi = a² */ \ __asm__("mulq %2" : "=a"(low), "=d"(high) : "a"(a) : "cc"); \ - /* carry:hi = lo:hi + carry:0 = a^2 + carry */ \ + /* carry:hi = lo:hi + carry:0 = a² + carry */ \ __asm__("addq %2,%0; adcq $0,%1" \ : "+r"(carry), "+d"(high) \ : "a"(low) \ @@ -273,7 +273,7 @@ BN_ULONG bssl::bn_sub_words(BN_ULONG *rp, const BN_ULONG *ap, // mul_add_c(a,b,c0,c1,c2) -- c+=a*b for three word number c=(c2,c1,c0) // mul_add_c2(a,b,c0,c1,c2) -- c+=2*a*b for three word number c=(c2,c1,c0) -// sqr_add_c(a,i,c0,c1,c2) -- c+=a[i]^2 for three word number c=(c2,c1,c0) +// sqr_add_c(a,i,c0,c1,c2) -- c+=a[i]² for three word number c=(c2,c1,c0) // sqr_add_c2(a,i,c0,c1,c2) -- c+=2*a[i]*a[j] for three word number c=(c2,c1,c0) // Keep in mind that carrying into high part of multiplication result can not diff --git a/third_party/boringssl/src/crypto/fipsmodule/bn/bn.cc.inc b/third_party/boringssl/src/crypto/fipsmodule/bn/bn.cc.inc index 38dea2504..c18efa955 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/bn/bn.cc.inc +++ b/third_party/boringssl/src/crypto/fipsmodule/bn/bn.cc.inc @@ -28,8 +28,8 @@ using namespace bssl; -// BN_MAX_WORDS is the maximum number of words allowed in a |BIGNUM|. It is -// sized so byte and bit counts of a |BIGNUM| always fit in |int|, with room to +// BN_MAX_WORDS is the maximum number of words allowed in a `BIGNUM`. It is +// sized so byte and bit counts of a `BIGNUM` always fit in `int`, with room to // spare. #define BN_MAX_WORDS (INT_MAX / (4 * BN_BITS2)) @@ -123,25 +123,25 @@ DEFINE_METHOD_FUNCTION(BIGNUM, BN_value_one) { } // BN_num_bits_word returns the minimum number of bits needed to represent the -// value in |l|. +// value in `l`. unsigned BN_num_bits_word(BN_ULONG l) { - // |BN_num_bits| is often called on RSA prime factors. These have public bit + // `BN_num_bits` is often called on RSA prime factors. These have public bit // lengths, but all bits beyond the high bit are secret, so count bits in // constant time. BN_ULONG x, mask; int bits = (l != 0); #if BN_BITS2 > 32 - // Look at the upper half of |x|. |x| is at most 64 bits long. + // Look at the upper half of `x`. `x` is at most 64 bits long. x = l >> 32; - // Set |mask| to all ones if |x| (the top 32 bits of |l|) is non-zero and all + // Set `mask` to all ones if `x` (the top 32 bits of `l`) is non-zero and all // all zeros otherwise. mask = 0u - x; mask = (0u - (mask >> (BN_BITS2 - 1))); - // If |x| is non-zero, the lower half is included in the bit count in full, + // If `x` is non-zero, the lower half is included in the bit count in full, // and we count the upper half. Otherwise, we count the lower half. bits += 32 & mask; - l ^= (x ^ l) & mask; // |l| is |x| if |mask| and remains |l| otherwise. + l ^= (x ^ l) & mask; // `l` is `x` if `mask` and remains `l` otherwise. #endif // The remaining blocks are analogous iterations at lower powers of two. @@ -235,7 +235,7 @@ int bssl::bn_set_words(BIGNUM *bn, const BN_ULONG *words, size_t num) { return 0; } OPENSSL_memmove(bn->d, words, num * sizeof(BN_ULONG)); - // |bn_wexpand| verified that |num| isn't too large. + // `bn_wexpand` verified that `num` isn't too large. bn->width = (int)num; bn->neg = 0; return 1; @@ -255,7 +255,7 @@ void bssl::bn_set_static_words(BIGNUM *bn, const BN_ULONG *words, size_t num) { } int bssl::bn_fits_in_words(const BIGNUM *bn, size_t num) { - // All words beyond |num| must be zero. + // All words beyond `num` must be zero. BN_ULONG mask = 0; for (size_t i = num; i < (size_t)bn->width; i++) { mask |= bn->d[i]; diff --git a/third_party/boringssl/src/crypto/fipsmodule/bn/bytes.cc.inc b/third_party/boringssl/src/crypto/fipsmodule/bn/bytes.cc.inc index 4d3fadaff..ef2c87b37 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/bn/bytes.cc.inc +++ b/third_party/boringssl/src/crypto/fipsmodule/bn/bytes.cc.inc @@ -24,8 +24,8 @@ using namespace bssl; void bssl::bn_big_endian_to_words(BN_ULONG *out, size_t out_len, const uint8_t *in, size_t in_len) { - // The caller should have sized |out| to fit |in| without truncating. This - // condition ensures we do not overflow |out|, so use a runtime check. + // The caller should have sized `out` to fit `in` without truncating. This + // condition ensures we do not overflow `out`, so use a runtime check. BSSL_CHECK(in_len <= out_len * sizeof(BN_ULONG)); // Load whole words. @@ -73,8 +73,8 @@ BIGNUM *BN_bin2bn(const uint8_t *in, size_t len, BIGNUM *ret) { return nullptr; } - // |bn_wexpand| must check bounds on |num_words| to write it into - // |ret->dmax|. + // `bn_wexpand` must check bounds on `num_words` to write it into + // `ret->dmax`. assert(num_words <= INT_MAX); ret->width = (int)num_words; ret->neg = 0; @@ -99,7 +99,7 @@ BIGNUM *BN_lebin2bn(const uint8_t *in, size_t len, BIGNUM *ret) { return ret; } - // Reserve enough space in |ret|. + // Reserve enough space in `ret`. size_t num_words = ((len - 1) / BN_BYTES) + 1; if (!bn_wexpand(ret, num_words)) { BN_free(bn); @@ -121,8 +121,8 @@ BIGNUM *BN_le2bn(const uint8_t *in, size_t len, BIGNUM *ret) { return BN_lebin2bn(in, len, ret); } -// fits_in_bytes returns one if the |num_words| words in |words| can be -// represented in |num_bytes| bytes. +// fits_in_bytes returns one if the `num_words` words in `words` can be +// represented in `num_bytes` bytes. static int fits_in_bytes(const BN_ULONG *words, size_t num_words, size_t num_bytes) { const uint8_t *bytes = (const uint8_t *)words; diff --git a/third_party/boringssl/src/crypto/fipsmodule/bn/cmp.cc.inc b/third_party/boringssl/src/crypto/fipsmodule/bn/cmp.cc.inc index aed2de170..c0a29ab8c 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/bn/cmp.cc.inc +++ b/third_party/boringssl/src/crypto/fipsmodule/bn/cmp.cc.inc @@ -38,7 +38,7 @@ static int bn_cmp_words_consttime(const BN_ULONG *a, size_t a_len, constant_time_select_int(eq, ret, constant_time_select_int(lt, -1, 1)); } - // If |a| or |b| has non-zero words beyond |min|, they take precedence. + // If `a` or `b` has non-zero words beyond `min`, they take precedence. if (a_len < b_len) { crypto_word_t mask = 0; for (size_t i = a_len; i < b_len; i++) { @@ -72,7 +72,7 @@ int BN_cmp(const BIGNUM *a, const BIGNUM *b) { } // We do not attempt to process the sign bit in constant time. Negative - // |BIGNUM|s should never occur in crypto, only calculators. + // `BIGNUM`s should never occur in crypto, only calculators. if (a->neg != b->neg) { if (a->neg) { return -1; @@ -143,7 +143,7 @@ int BN_is_pow2(const BIGNUM *bn) { int BN_equal_consttime(const BIGNUM *a, const BIGNUM *b) { BN_ULONG mask = 0; - // If |a| or |b| has more words than the other, all those words must be zero. + // If `a` or `b` has more words than the other, all those words must be zero. for (int i = a->width; i < b->width; i++) { mask |= b->d[i]; } diff --git a/third_party/boringssl/src/crypto/fipsmodule/bn/ctx.cc.inc b/third_party/boringssl/src/crypto/fipsmodule/bn/ctx.cc.inc index 1d67281c8..a7381b055 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/bn/ctx.cc.inc +++ b/third_party/boringssl/src/crypto/fipsmodule/bn/ctx.cc.inc @@ -35,25 +35,25 @@ BSSL_NAMESPACE_BEGIN class BignumCtx : public bignum_ctx { public: ~BignumCtx() { - // All |BN_CTX_start| calls must be matched with |BN_CTX_end|, otherwise the + // All `BN_CTX_start` calls must be matched with `BN_CTX_end`, otherwise the // function may use more memory than expected, potentially without bound if - // done in a loop. Assert that all |BIGNUM|s have been released. + // done in a loop. Assert that all `BIGNUM`s have been released. assert(used_ == 0 || error_); } - // bignums_ is the stack of |BIGNUM|s managed by this |BN_CTX|. + // bignums_ is the stack of `BIGNUM`s managed by this `BN_CTX`. Vector> bignums_; - // stack_ is the stack of |BN_CTX_start| frames. It is the value of |used_| at - // the time |BN_CTX_start| was called. + // stack_ is the stack of `BN_CTX_start` frames. It is the value of `used_` at + // the time `BN_CTX_start` was called. Vector stack_; - // used_ is the number of |BIGNUM|s from |bignums_| that have been used. + // used_ is the number of `BIGNUM`s from `bignums_` that have been used. size_t used_ = 0; - // error_ is whether any operation on this |BN_CTX| failed. All subsequent + // error_ is whether any operation on this `BN_CTX` failed. All subsequent // operations will fail. bool error_ = false; - // defer_error_ is whether an operation on this |BN_CTX| has failed, but no + // defer_error_ is whether an operation on this `BN_CTX` has failed, but no // error has been pushed to the queue yet. This is used to defer errors from - // |BN_CTX_start| to |BN_CTX_get|. + // `BN_CTX_start` to `BN_CTX_get`. bool defer_error_ = false; }; @@ -71,14 +71,14 @@ void BN_CTX_start(BN_CTX *ctx) { auto *impl = FromOpaque(ctx); if (impl->error_) { - // Once an operation has failed, |impl->stack| no longer matches the number - // of |BN_CTX_end| calls to come. Do nothing. + // Once an operation has failed, `impl->stack` no longer matches the number + // of `BN_CTX_end` calls to come. Do nothing. return; } if (!impl->stack_.Push(impl->used_)) { impl->error_ = true; - // |BN_CTX_start| cannot fail, so defer the error to |BN_CTX_get|. + // `BN_CTX_start` cannot fail, so defer the error to `BN_CTX_get`. impl->defer_error_ = true; ERR_clear_error(); } @@ -107,7 +107,7 @@ BIGNUM *BN_CTX_get(BN_CTX *ctx) { BIGNUM *ret = impl->bignums_[impl->used_].get(); BN_zero(ret); - // This is bounded by |impl->bignums_.size()|, so it cannot overflow. + // This is bounded by `impl->bignums_.size()`, so it cannot overflow. impl->used_++; return ret; } @@ -116,8 +116,8 @@ void BN_CTX_end(BN_CTX *ctx) { auto *impl = FromOpaque(ctx); if (impl->error_) { - // Once an operation has failed, |impl->stack_| no longer matches the number - // of |BN_CTX_end| calls to come. Do nothing. + // Once an operation has failed, `impl->stack_` no longer matches the number + // of `BN_CTX_end` calls to come. Do nothing. return; } diff --git a/third_party/boringssl/src/crypto/fipsmodule/bn/div.cc.inc b/third_party/boringssl/src/crypto/fipsmodule/bn/div.cc.inc index af522dfc6..8147dcce3 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/bn/div.cc.inc +++ b/third_party/boringssl/src/crypto/fipsmodule/bn/div.cc.inc @@ -35,8 +35,8 @@ using namespace bssl; -// bn_div_words divides a double-width |h|,|l| by |d| and returns the result, -// which must fit in a |BN_ULONG|, i.e. |h < d|. +// bn_div_words divides a double-width `h`,`l` by `d` and returns the result, +// which must fit in a `BN_ULONG`, i.e. `h < d`. [[maybe_unused]] static BN_ULONG bn_div_words(BN_ULONG h, BN_ULONG l, BN_ULONG d) { assert(h < d); @@ -108,18 +108,18 @@ static BN_ULONG bn_div_words(BN_ULONG h, BN_ULONG l, BN_ULONG d) { return ret; } -// bn_div_rem_words divides a double-width numerator (high half |nh| and low -// half |nl|) with a single-width divisor. It sets |*quotient_out| and -// |*rem_out| to be the quotient and numerator, respectively. The quotient must -// fit in a |BN_ULONG|, i.e. |nh < d|. +// bn_div_rem_words divides a double-width numerator (high half `nh` and low +// half `nl`) with a single-width divisor. It sets `*quotient_out` and +// `*rem_out` to be the quotient and numerator, respectively. The quotient must +// fit in a `BN_ULONG`, i.e. `nh < d`. static void bn_div_rem_words(BN_ULONG *quotient_out, BN_ULONG *rem_out, BN_ULONG nh, BN_ULONG nl, BN_ULONG d) { assert(nh < d); // This operation is the x86 and x86_64 DIV instruction, but it is difficult - // for the compiler to emit it. Dividing a |BN_ULLONG| by a |BN_ULONG| does - // not work because, a priori, the quotient may not fit in |BN_ULONG| and DIV + // for the compiler to emit it. Dividing a `BN_ULLONG` by a `BN_ULONG` does + // not work because, a priori, the quotient may not fit in `BN_ULONG` and DIV // will trap on overflow, not truncate. The compiler will instead emit a call - // to a more expensive support function (e.g. |__udivdi3|). Thus we use inline + // to a more expensive support function (e.g. `__udivdi3`). Thus we use inline // assembly or intrinsics to get the instruction. // // These is specific to x86 and x86_64; Arm and RISC-V do not have double-wide @@ -189,8 +189,8 @@ int BN_div(BIGNUM *quotient, BIGNUM *rem, const BIGNUM *numerator, return 0; } - // This algorithm relies on |sdiv| being minimal width. We do not use this - // function on secret inputs, so leaking this is fine. Also minimize |snum| to + // This algorithm relies on `sdiv` being minimal width. We do not use this + // function on secret inputs, so leaking this is fine. Also minimize `snum` to // avoid looping on leading zeros, as we're not trying to be leak-free. bn_set_minimal_width(sdiv); bn_set_minimal_width(snum); @@ -199,10 +199,10 @@ int BN_div(BIGNUM *quotient, BIGNUM *rem, const BIGNUM *numerator, d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2]; assert(d0 & (((BN_ULONG)1) << (BN_BITS2 - 1))); - // Extend |snum| with zeros to satisfy the long division invariants: - // - |snum| must have at least |div_n| + 1 words. - // - |snum|'s most significant word must be zero to guarantee the first loop - // iteration works with a prefix greater than |sdiv|. (This is the extra u0 + // Extend `snum` with zeros to satisfy the long division invariants: + // - `snum` must have at least `div_n` + 1 words. + // - `snum`'s most significant word must be zero to guarantee the first loop + // iteration works with a prefix greater than `sdiv`. (This is the extra u0 // digit in Knuth step D1.) num_n = snum->width <= div_n ? div_n + 1 : snum->width + 1; if (!bn_resize_words(snum, num_n)) { @@ -356,18 +356,18 @@ int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx) { BN_ULONG bssl::bn_reduce_once(BN_ULONG *r, const BN_ULONG *a, BN_ULONG carry, const BN_ULONG *m, size_t num) { assert(r != a); - // |r| = |a| - |m|. |bn_sub_words| performs the bulk of the subtraction, and - // then we apply the borrow to |carry|. + // `r` = `a` - `m`. `bn_sub_words` performs the bulk of the subtraction, and + // then we apply the borrow to `carry`. carry -= bn_sub_words(r, a, m, num); - // We know 0 <= |a| < 2*|m|, so -|m| <= |r| < |m|. + // We know 0 <= `a` < 2*`m`, so -`m` <= `r` < `m`. // - // If 0 <= |r| < |m|, |r| fits in |num| words and |carry| is zero. We then - // wish to select |r| as the answer. Otherwise -m <= r < 0 and we wish to - // return |r| + |m|, or |a|. |carry| must then be -1 or all ones. In both - // cases, |carry| is a suitable input to |bn_select_words|. + // If 0 <= `r` < `m`, `r` fits in `num` words and `carry` is zero. We then + // wish to select `r` as the answer. Otherwise -m <= r < 0 and we wish to + // return `r` + `m`, or `a`. `carry` must then be -1 or all ones. In both + // cases, `carry` is a suitable input to `bn_select_words`. // - // Although |carry| may be one if it was one on input and |bn_sub_words| - // returns zero, this would give |r| > |m|, violating our input assumptions. + // Although `carry` may be one if it was one on input and `bn_sub_words` + // returns zero, this would give `r` > `m`, violating our input assumptions. declassify_assert(carry + 1 <= 1); bn_select_words(r, carry, a /* r < 0 */, r /* r >= 0 */, num); return carry; @@ -376,7 +376,7 @@ BN_ULONG bssl::bn_reduce_once(BN_ULONG *r, const BN_ULONG *a, BN_ULONG carry, BN_ULONG bssl::bn_reduce_once_in_place(BN_ULONG *r, BN_ULONG carry, const BN_ULONG *m, BN_ULONG *tmp, size_t num) { - // See |bn_reduce_once| for why this logic works. + // See `bn_reduce_once` for why this logic works. carry -= bn_sub_words(tmp, r, m, num); declassify_assert(carry + 1 <= 1); bn_select_words(r, carry, r /* tmp < 0 */, tmp /* tmp >= 0 */, num); @@ -438,14 +438,14 @@ int bssl::bn_div_consttime(BIGNUM *quotient, BIGNUM *remainder, r->width = divisor->width; r->neg = 0; - // Incorporate |numerator| into |r|, one bit at a time, reducing after each - // step. We maintain the invariant that |0 <= r < divisor| and - // |q * divisor + r = n| where |n| is the portion of |numerator| incorporated + // Incorporate `numerator` into `r`, one bit at a time, reducing after each + // step. We maintain the invariant that `0 <= r < divisor` and + // `q * divisor + r = n` where `n` is the portion of `numerator` incorporated // so far. // - // First, we short-circuit the loop: if we know |divisor| has at least - // |divisor_min_bits| bits, the top |divisor_min_bits - 1| can be incorporated - // without reductions. This significantly speeds up |RSA_check_key|. For + // First, we short-circuit the loop: if we know `divisor` has at least + // `divisor_min_bits` bits, the top `divisor_min_bits - 1` can be incorporated + // without reductions. This significantly speeds up `RSA_check_key`. For // simplicity, we round down to a whole number of words. declassify_assert(divisor_min_bits <= BN_num_bits(divisor)); initial_words = 0; @@ -462,13 +462,13 @@ int bssl::bn_div_consttime(BIGNUM *quotient, BIGNUM *remainder, for (int bit = BN_BITS2 - 1; bit >= 0; bit--) { // Incorporate the next bit of the numerator, by computing // r = 2*r or 2*r + 1. Note the result fits in one more word. We store the - // extra word in |carry|. + // extra word in `carry`. BN_ULONG carry = bn_add_words(r->d, r->d, r->d, divisor->width); r->d[0] |= (numerator->d[i] >> bit) & 1; - // |r| was previously fully-reduced, so we know: + // `r` was previously fully-reduced, so we know: // 2*0 <= r <= 2*(divisor-1) + 1 // 0 <= r <= 2*divisor - 1 < 2*divisor. - // Thus |r| satisfies the preconditions for |bn_reduce_once_in_place|. + // Thus `r` satisfies the preconditions for `bn_reduce_once_in_place`. BN_ULONG subtracted = bn_reduce_once_in_place(r->d, carry, divisor->d, tmp->d, divisor->width); // The corresponding bit of the quotient is set iff we needed to subtract. @@ -494,11 +494,11 @@ static BIGNUM *bn_scratch_space_from_ctx(size_t width, BN_CTX *ctx) { return ret; } -// bn_resized_from_ctx returns |bn| with width at least |width| or NULL on +// bn_resized_from_ctx returns `bn` with width at least `width` or NULL on // error. This is so it may be used with low-level "words" functions. If -// necessary, it allocates a new |BIGNUM| with a lifetime of the current scope -// in |ctx|, so the caller does not need to explicitly free it. |bn| must fit in -// |width| words. +// necessary, it allocates a new `BIGNUM` with a lifetime of the current scope +// in `ctx`, so the caller does not need to explicitly free it. `bn` must fit in +// `width` words. static const BIGNUM *bn_resized_from_ctx(const BIGNUM *bn, size_t width, BN_CTX *ctx) { if ((size_t)bn->width >= width) { @@ -679,7 +679,7 @@ BN_ULONG BN_div_word(BIGNUM *a, BN_ULONG w) { return 0; } - // normalize input for |bn_div_rem_words|. + // normalize input for `bn_div_rem_words`. j = BN_BITS2 - BN_num_bits_word(w); w <<= j; if (!BN_lshift(a, a, j)) { @@ -713,8 +713,8 @@ BN_ULONG BN_mod_word(const BIGNUM *a, BN_ULONG w) { } #ifndef BN_CAN_DIVIDE_ULLONG - // If |w| is too long and we don't have |BN_ULLONG| division then we need to - // fall back to using |BN_div_word|. + // If `w` is too long and we don't have `BN_ULLONG` division then we need to + // fall back to using `BN_div_word`. if (w > ((BN_ULONG)1 << BN_BITS4)) { BIGNUM *tmp = BN_dup(a); if (tmp == nullptr) { diff --git a/third_party/boringssl/src/crypto/fipsmodule/bn/div_extra.cc.inc b/third_party/boringssl/src/crypto/fipsmodule/bn/div_extra.cc.inc index 52e1be63b..b518b4e1a 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/bn/div_extra.cc.inc +++ b/third_party/boringssl/src/crypto/fipsmodule/bn/div_extra.cc.inc @@ -26,13 +26,13 @@ using namespace bssl; // http://ridiculousfish.com/blog/posts/labor-of-division-episode-i.html // // We use 32-bit numerator and 16-bit divisor for simplicity. This allows -// computing |m| and |q| without architecture-specific code. +// computing `m` and `q` without architecture-specific code. -// mod_u16 returns |n| mod |d|. |p| and |m| are the "magic numbers" for |d| (see +// mod_u16 returns `n` mod `d`. `p` and `m` are the "magic numbers" for `d` (see // reference). For proof of correctness in Coq, see // https://github.com/davidben/fiat-crypto/blob/barrett/src/Arithmetic/BarrettReduction/RidiculousFish.v -// Note the Coq version of |mod_u16| additionally includes the computation of -// |p| and |m| from |bn_mod_u16_consttime| below. +// Note the Coq version of `mod_u16` additionally includes the computation of +// `p` and `m` from `bn_mod_u16_consttime` below. static uint16_t mod_u16(uint32_t n, uint16_t d, uint32_t p, uint32_t m) { // Compute floor(n/d) per steps 3 through 5. uint32_t q = ((uint64_t)m * n) >> 32; @@ -46,11 +46,11 @@ static uint16_t mod_u16(uint32_t n, uint16_t d, uint32_t p, uint32_t m) { return n; } -// shift_and_add_mod_u16 returns |r| * 2^32 + |a| mod |d|. |p| and |m| are the -// "magic numbers" for |d| (see reference). +// shift_and_add_mod_u16 returns `r` * 2^32 + `a` mod `d`. `p` and `m` are the +// "magic numbers" for `d` (see reference). static uint16_t shift_and_add_mod_u16(uint16_t r, uint32_t a, uint16_t d, uint32_t p, uint32_t m) { - // Incorporate |a| in two 16-bit chunks. + // Incorporate `a` in two 16-bit chunks. uint32_t t = r; t <<= 16; t |= a >> 16; @@ -67,11 +67,11 @@ uint16_t bssl::bn_mod_u16_consttime(const BIGNUM *bn, uint16_t d) { return 0; } - // Compute the "magic numbers" for |d|. See steps 1 and 2. + // Compute the "magic numbers" for `d`. See steps 1 and 2. // This computes p = ceil(log_2(d)). uint32_t p = BN_num_bits_word(d - 1); - // This operation is not constant-time, but |p| and |d| are public values. - // Note that |p| is at most 16, so the computation fits in |uint64_t|. + // This operation is not constant-time, but `p` and `d` are public values. + // Note that `p` is at most 16, so the computation fits in `uint64_t`. assert(p <= 16); uint32_t m = (uint32_t)(((UINT64_C(1) << (32 + p)) + d - 1) / d); diff --git a/third_party/boringssl/src/crypto/fipsmodule/bn/exponentiation.cc.inc b/third_party/boringssl/src/crypto/fipsmodule/bn/exponentiation.cc.inc index 869c78eb5..81b638593 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/bn/exponentiation.cc.inc +++ b/third_party/boringssl/src/crypto/fipsmodule/bn/exponentiation.cc.inc @@ -30,11 +30,11 @@ using namespace bssl; #if defined(OPENSSL_BN_ASM_MONT5) -// bn_mul_mont_gather5 multiples loads index |power| of |table|, multiplies it -// by |ap| modulo |np|, and stores the result in |rp|. The values are |num| -// words long and represented in Montgomery form. |n0| is a pointer to the -// corresponding field in |BN_MONT_CTX|. |table| must be aligned to at least -// 16 bytes. |power| must be less than 32 and is treated as secret. +// bn_mul_mont_gather5 multiples loads index `power` of `table`, multiplies it +// by `ap` modulo `np`, and stores the result in `rp`. The values are `num` +// words long and represented in Montgomery form. `n0` is a pointer to the +// corresponding field in `BN_MONT_CTX`. `table` must be aligned to at least +// 16 bytes. `power` must be less than 32 and is treated as secret. // // WARNING: This function implements Almost Montgomery Multiplication from // https://eprint.iacr.org/2011/239. The inputs do not need to be fully reduced. @@ -51,11 +51,11 @@ static void bn_mul_mont_gather5(BN_ULONG *rp, const BN_ULONG *ap, } } -// bn_power5 squares |ap| five times and multiplies it by the value stored at -// index |power| of |table|, modulo |np|. It stores the result in |rp|. The -// values are |num| words long and represented in Montgomery form. |n0| is a -// pointer to the corresponding field in |BN_MONT_CTX|. |num| must be divisible -// by 8. |power| must be less than 32 and is treated as secret. +// bn_power5 squares `ap` five times and multiplies it by the value stored at +// index `power` of `table`, modulo `np`. It stores the result in `rp`. The +// values are `num` words long and represented in Montgomery form. `n0` is a +// pointer to the corresponding field in `BN_MONT_CTX`. `num` must be divisible +// by 8. `power` must be less than 32 and is treated as secret. // // WARNING: This function implements Almost Montgomery Multiplication from // https://eprint.iacr.org/2011/239. The inputs do not need to be fully reduced. @@ -74,7 +74,7 @@ static void bn_power5(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *table, #endif // defined(OPENSSL_BN_ASM_MONT5) // BN_window_bits_for_exponent_size returns sliding window size for mod_exp with -// a |b| bit exponent. +// a `b` bit exponent. // // For window size 'w' (w >= 2) and a random 'b' bits exponent, the number of // multiplications is a constant plus on average @@ -114,16 +114,16 @@ static int BN_window_bits_for_exponent_size(size_t b) { // TABLE_SIZE is the maximum precomputation table size for *variable* sliding // windows. This must be 2^(max_window - 1), where max_window is the largest -// value returned from |BN_window_bits_for_exponent_size|. +// value returned from `BN_window_bits_for_exponent_size`. #define TABLE_SIZE 32 // TABLE_BITS_SMALL is the smallest value returned from -// |BN_window_bits_for_exponent_size| when |b| is at most |BN_BITS2| * -// |BN_SMALL_MAX_WORDS| words. +// `BN_window_bits_for_exponent_size` when `b` is at most `BN_BITS2` * +// `BN_SMALL_MAX_WORDS` words. #define TABLE_BITS_SMALL 5 -// TABLE_SIZE_SMALL is the same as |TABLE_SIZE|, but when |b| is at most -// |BN_BITS2| * |BN_SMALL_MAX_WORDS|. +// TABLE_SIZE_SMALL is the same as `TABLE_SIZE`, but when `b` is at most +// `BN_BITS2` * `BN_SMALL_MAX_WORDS`. #define TABLE_SIZE_SMALL (1 << (TABLE_BITS_SMALL - 1)) int BN_mod_exp_mont(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p, @@ -136,7 +136,7 @@ int BN_mod_exp_mont(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p, OPENSSL_PUT_ERROR(BN, BN_R_NEGATIVE_NUMBER); return 0; } - // |a| is secret, but |a < m| is not. + // `a` is secret, but `a < m` is not. if (a->neg || constant_time_declassify_int(BN_ucmp(a, m)) >= 0) { OPENSSL_PUT_ERROR(BN, BN_R_INPUT_NOT_REDUCED); return 0; @@ -172,7 +172,7 @@ int BN_mod_exp_mont(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p, } // We exponentiate by looking at sliding windows of the exponent and - // precomputing powers of |a|. Windows may be shifted so they always end on a + // precomputing powers of `a`. Windows may be shifted so they always end on a // set bit, so only precompute odd powers. We compute val[i] = a^(2*i + 1) // for i = 0 to 2^(window-1), all in Montgomery form. int window = BN_window_bits_for_exponent_size(bits); @@ -193,8 +193,8 @@ int BN_mod_exp_mont(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p, } } - // |p| is non-zero, so at least one window is non-zero. To save some - // multiplications, defer initializing |r| until then. + // `p` is non-zero, so at least one window is non-zero. To save some + // multiplications, defer initializing `r` until then. int r_is_one = 1; int wstart = bits - 1; // The top bit of the window. for (;;) { @@ -220,7 +220,7 @@ int BN_mod_exp_mont(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p, } } - // Shift |r| to the end of the window. + // Shift `r` to the end of the window. if (!r_is_one) { for (int i = 0; i < wsize + 1; i++) { if (!BN_mod_mul_montgomery(r, r, r, mont, ctx)) { @@ -246,7 +246,7 @@ int BN_mod_exp_mont(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p, wstart -= wsize + 1; } - // |p| is non-zero, so |r_is_one| must be cleared at some point. + // `p` is non-zero, so `r_is_one` must be cleared at some point. assert(!r_is_one); return BN_from_montgomery(rr, r, mont, ctx); @@ -261,8 +261,8 @@ void bssl::bn_mod_exp_mont_small(BN_ULONG *r, const BN_ULONG *a, size_t num, } assert(BN_is_odd(&mont->N)); - // Count the number of bits in |p|, skipping leading zeros. Note this function - // treats |p| as public. + // Count the number of bits in `p`, skipping leading zeros. Note this function + // treats `p` as public. while (num_p != 0 && p[num_p - 1] == 0) { num_p--; } @@ -274,12 +274,12 @@ void bssl::bn_mod_exp_mont_small(BN_ULONG *r, const BN_ULONG *a, size_t num, assert(bits != 0); // We exponentiate by looking at sliding windows of the exponent and - // precomputing powers of |a|. Windows may be shifted so they always end on a + // precomputing powers of `a`. Windows may be shifted so they always end on a // set bit, so only precompute odd powers. We compute val[i] = a^(2*i + 1) for // i = 0 to 2^(window-1), all in Montgomery form. unsigned window = BN_window_bits_for_exponent_size(bits); if (window > TABLE_BITS_SMALL) { - window = TABLE_BITS_SMALL; // Tolerate excessively large |p|. + window = TABLE_BITS_SMALL; // Tolerate excessively large `p`. } BN_ULONG val[TABLE_SIZE_SMALL][BN_SMALL_MAX_WORDS]; OPENSSL_memcpy(val[0], a, num * sizeof(BN_ULONG)); @@ -291,8 +291,8 @@ void bssl::bn_mod_exp_mont_small(BN_ULONG *r, const BN_ULONG *a, size_t num, } } - // |p| is non-zero, so at least one window is non-zero. To save some - // multiplications, defer initializing |r| until then. + // `p` is non-zero, so at least one window is non-zero. To save some + // multiplications, defer initializing `r` until then. int r_is_one = 1; size_t wstart = bits - 1; // The top bit of the window. for (;;) { @@ -318,7 +318,7 @@ void bssl::bn_mod_exp_mont_small(BN_ULONG *r, const BN_ULONG *a, size_t num, } } - // Shift |r| to the end of the window. + // Shift `r` to the end of the window. if (!r_is_one) { for (unsigned i = 0; i < wsize + 1; i++) { bn_mod_mul_montgomery_small(r, r, r, num, mont); @@ -339,7 +339,7 @@ void bssl::bn_mod_exp_mont_small(BN_ULONG *r, const BN_ULONG *a, size_t num, wstart -= wsize + 1; } - // |p| is non-zero, so |r_is_one| must be cleared at some point. + // `p` is non-zero, so `r_is_one` must be cleared at some point. assert(!r_is_one); OPENSSL_cleanse(val, sizeof(val)); } @@ -372,44 +372,72 @@ void bssl::bn_mod_inverse0_prime_mont_small(BN_ULONG *r, const BN_ULONG *a, static void copy_to_prebuf(const BIGNUM *b, int top, BN_ULONG *table, int idx, int window) { int ret = bn_copy_words(table + idx * top, top, b); - assert(ret); // |b| is guaranteed to fit. + assert(ret); // `b` is guaranteed to fit. (void)ret; } +// Window sizes optimized for fixed window size modular exponentiation +// algorithm (BN_mod_exp_mont_consttime). +// +// TODO(davidben): These window sizes were originally set for 64-byte cache +// lines with a cache-line-dependent constant-time mitigation. They can probably +// be revised now that our implementation is no longer cache-time-dependent. +#define BN_window_bits_for_ctime_exponent_size(b) \ + ((b) > 937 ? 6 : (b) > 306 ? 5 : (b) > 89 ? 4 : (b) > 22 ? 3 : 1) +#define BN_MAX_MOD_EXP_CTIME_WINDOW (6) + static int copy_from_prebuf(BIGNUM *b, int top, const BN_ULONG *table, int idx, int window) { if (!bn_wexpand(b, top)) { return 0; } - OPENSSL_memset(b->d, 0, sizeof(BN_ULONG) * top); + assert(window > 0 && window <= BN_MAX_MOD_EXP_CTIME_WINDOW); + + // Compute the selection masks up front. The value barrier prevents Clang + // from adding a branch when `i != idx` and making this copy not constant + // time. const int width = 1 << window; - for (int i = 0; i < width; i++, table += top) { - // Use a value barrier to prevent Clang from adding a branch when |i != idx| - // and making this copy not constant time. Clang is still allowed to learn - // that |mask| is constant across the inner loop, so this won't inhibit any - // vectorization it might do. - BN_ULONG mask = value_barrier_w(constant_time_eq_int(i, idx)); - for (int j = 0; j < top; j++) { - b->d[j] |= table[j] & mask; + BN_ULONG masks[1 << BN_MAX_MOD_EXP_CTIME_WINDOW]; + for (int i = 0; i < width; i++) { + masks[i] = value_barrier_w(constant_time_eq_int(i, idx)); + } + + // Every table entry is read for every limb, independent of `idx`. The output + // is accumulated a few limbs at a time in registers. Updating `b->d` once per + // table entry instead adds a store-to-load dependency to every iteration, + // which the compiler cannot remove because `b->d` may alias `table`. + BN_ULONG *const d = b->d; + int j = 0; + for (; j + 4 <= top; j += 4) { + BN_ULONG a0 = 0, a1 = 0, a2 = 0, a3 = 0; + const BN_ULONG *t = table + j; + for (int i = 0; i < width; i++, t += top) { + BN_ULONG m = masks[i]; + a0 |= t[0] & m; + a1 |= t[1] & m; + a2 |= t[2] & m; + a3 |= t[3] & m; } + d[j] = a0; + d[j + 1] = a1; + d[j + 2] = a2; + d[j + 3] = a3; + } + for (; j < top; j++) { + BN_ULONG a = 0; + const BN_ULONG *t = table + j; + for (int i = 0; i < width; i++, t += top) { + a |= *t & masks[i]; + } + d[j] = a; } b->width = top; return 1; } -// Window sizes optimized for fixed window size modular exponentiation -// algorithm (BN_mod_exp_mont_consttime). -// -// TODO(davidben): These window sizes were originally set for 64-byte cache -// lines with a cache-line-dependent constant-time mitigation. They can probably -// be revised now that our implementation is no longer cache-time-dependent. -#define BN_window_bits_for_ctime_exponent_size(b) \ - ((b) > 937 ? 6 : (b) > 306 ? 5 : (b) > 89 ? 4 : (b) > 22 ? 3 : 1) -#define BN_MAX_MOD_EXP_CTIME_WINDOW (6) - -// This variant of |BN_mod_exp_mont| uses fixed windows and fixed memory access +// This variant of `BN_mod_exp_mont` uses fixed windows and fixed memory access // patterns to protect secret exponents (cf. the hyper-threading timing attacks // pointed out by Colin Percival, // http://www.daemonology.net/hyperthreading-considered-harmful/) @@ -430,14 +458,14 @@ int BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p, OPENSSL_PUT_ERROR(BN, BN_R_NEGATIVE_NUMBER); return 0; } - // |a| is secret, but it is required to be in range, so these comparisons may + // `a` is secret, but it is required to be in range, so these comparisons may // be leaked. if (a->neg || constant_time_declassify_int(BN_ucmp(a, m) >= 0)) { OPENSSL_PUT_ERROR(BN, BN_R_INPUT_NOT_REDUCED); return 0; } - // Use all bits stored in |p|, rather than |BN_num_bits|, so we do not leak + // Use all bits stored in `p`, rather than `BN_num_bits`, so we do not leak // whether the top bits are zero. int max_bits = p->width * BN_BITS2; int bits = max_bits; @@ -461,8 +489,8 @@ int BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p, mont = new_mont.get(); } - // Use the width in |mont->N|, rather than the copy in |m|. The assembly - // implementation assumes it can use |top| to size R. + // Use the width in `mont->N`, rather than the copy in `m`. The assembly + // implementation assumes it can use `top` to size R. top = mont->N.width; #if defined(OPENSSL_BN_ASM_MONT5) || defined(RSAZ_ENABLED) @@ -494,7 +522,7 @@ int BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p, window = BN_window_bits_for_ctime_exponent_size(bits); assert(window <= BN_MAX_MOD_EXP_CTIME_WINDOW); - // Calculating |powerbuf_len| below cannot overflow because of the bound on + // Calculating `powerbuf_len` below cannot overflow because of the bound on // Montgomery reduction. assert((size_t)top <= BN_MONTGOMERY_MAX_WORDS); static_assert( @@ -505,13 +533,13 @@ int BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p, #if defined(OPENSSL_BN_ASM_MONT5) if (window >= 5) { window = 5; // ~5% improvement for RSA2048 sign, and even for RSA4096 - // Reserve space for the |mont->N| copy. + // Reserve space for the `mont->N` copy. powerbuf_len += top * sizeof(mont->N.d[0]); } #endif // Allocate a buffer large enough to hold all of the pre-computed - // powers of |am|, |am| itself, and |tmp|. + // powers of `am`, `am` itself, and `tmp`. num_powers = 1 << window; powerbuf_len += sizeof(m->d[0]) * top * (num_powers + 2); @@ -519,7 +547,7 @@ int BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p, if (powerbuf_len <= sizeof(storage)) { powerbuf = storage; } - // |storage| is more than large enough to handle 1024-bit inputs. + // `storage` is more than large enough to handle 1024-bit inputs. assert(powerbuf != nullptr || top * BN_BITS2 > 1024); #endif if (powerbuf == nullptr) { @@ -532,7 +560,7 @@ int BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p, } OPENSSL_memset(powerbuf, 0, powerbuf_len); - // Place |tmp| and |am| right after powers table. + // Place `tmp` and `am` right after powers table. BIGNUM tmp, am; tmp.d = powerbuf + top * num_powers; am.d = tmp.d + top; @@ -545,7 +573,7 @@ int BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p, goto err; } - // Prepare a^1 in the Montgomery domain. + // Prepare a¹ in the Montgomery domain. assert(!a->neg); declassify_assert(BN_ucmp(a, m) < 0); if (!BN_to_montgomery(&am, a, mont, ctx) || !bn_resize_words(&am, top)) { @@ -561,24 +589,24 @@ int BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p, // exponentiation, used in RSA-1024 with CRT, but RSA-1024 is no longer // important. // - // |bn_mul_mont_gather5| and |bn_power5| implement the "almost" reduction + // `bn_mul_mont_gather5` and `bn_power5` implement the "almost" reduction // variant, so the values here may not be fully reduced. They are bounded by R - // (i.e. they fit in |top| words), not |m|. Additionally, we pass these - // "almost" reduced inputs into |bn_mul_mont_words|, which implements the - // normal reduction variant. Given those inputs, |bn_mul_mont_words| may not + // (i.e. they fit in `top` words), not `m`. Additionally, we pass these + // "almost" reduced inputs into `bn_mul_mont_words`, which implements the + // normal reduction variant. Given those inputs, `bn_mul_mont_words` may not // give reduced output, but it will still produce "almost" reduced output. // // TODO(davidben): Using "almost" reduction complicates analysis of this code, // and its interaction with other parts of the project. Determine whether this // is actually necessary for performance. if (window == 5 && top > 1) { - // Copy |mont->N| to improve cache locality. + // Copy `mont->N` to improve cache locality. BN_ULONG *np = am.d + top; for (i = 0; i < top; i++) { np[i] = mont->N.d[i]; } - // Fill |powerbuf| with the first 32 powers of |am|. + // Fill `powerbuf` with the first 32 powers of `am`. const BN_ULONG *n0 = mont->n0; bn_scatter5(tmp.d, top, powerbuf, 0); bn_scatter5(am.d, am.width, powerbuf, 1); @@ -590,7 +618,7 @@ int BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p, bn_mul_mont_words(tmp.d, tmp.d, tmp.d, np, n0, top); bn_scatter5(tmp.d, top, powerbuf, i); } - // Compute odd powers |i| based on |i - 1|, then all powers |i * 2^j|. + // Compute odd powers `i` based on `i - 1`, then all powers `i * 2^j`. for (i = 3; i < 32; i += 2) { bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1); bn_scatter5(tmp.d, top, powerbuf, i); @@ -606,7 +634,7 @@ int BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p, } bn_gather5(tmp.d, top, powerbuf, wvalue); - // At this point |bits| is 4 mod 5 and at least -1. (|bits| is the first bit + // At this point `bits` is 4 mod 5 and at least -1. (`bits` is the first bit // that has not been read yet.) assert(bits >= -1 && (bits == -1 || bits % 5 == 4)); @@ -628,16 +656,16 @@ int BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p, } else { const uint8_t *p_bytes = (const uint8_t *)p->d; assert(bits < max_bits); - // |p = 0| has been handled as a special case, so |max_bits| is at least + // `p = 0` has been handled as a special case, so `max_bits` is at least // one word. assert(max_bits >= 64); // If the first bit to be read lands in the last byte, unroll the first - // iteration to avoid reading past the bounds of |p->d|. (After the first - // iteration, we are guaranteed to be past the last byte.) Note |bits| + // iteration to avoid reading past the bounds of `p->d`. (After the first + // iteration, we are guaranteed to be past the last byte.) Note `bits` // here is the top bit, inclusive. if (bits - 4 >= max_bits - 8) { - // Read five bits from |bits-4| through |bits|, inclusive. + // Read five bits from `bits-4` through `bits`, inclusive. wvalue = p_bytes[p->width * BN_BYTES - 1]; wvalue >>= (bits - 4) & 7; wvalue &= 0x1f; @@ -645,7 +673,7 @@ int BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p, bn_power5(tmp.d, tmp.d, powerbuf, np, n0, top, wvalue); } while (bits >= 0) { - // Read five bits from |bits-4| through |bits|, inclusive. + // Read five bits from `bits-4` through `bits`, inclusive. int first_bit = bits - 4; uint16_t val; OPENSSL_memcpy(&val, p_bytes + (first_bit >> 3), sizeof(val)); @@ -655,8 +683,8 @@ int BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p, bn_power5(tmp.d, tmp.d, powerbuf, np, n0, top, val); } } - // The result is now in |tmp| in Montgomery form, but it may not be fully - // reduced. This is within bounds for |BN_from_montgomery| (tmp < R <= m*R) + // The result is now in `tmp` in Montgomery form, but it may not be fully + // reduced. This is within bounds for `BN_from_montgomery` (tmp < R <= m*R) // so it will, when converting from Montgomery form, produce a fully reduced // result. // @@ -672,7 +700,7 @@ int BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p, // If the window size is greater than 1, then calculate // val[i=2..2^winsize-1]. Powers are computed as a*a^(i-1) - // (even powers could instead be computed as (a^(i/2))^2 + // (even powers could instead be computed as (a^(i/2))² // to use the slight performance advantage of sqr over mul). if (window > 1) { if (!BN_mod_mul_montgomery(&tmp, &am, &am, mont, ctx)) { @@ -725,9 +753,9 @@ int BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p, } // Convert the final result from Montgomery to standard format. If we used the - // |OPENSSL_BN_ASM_MONT5| codepath, |tmp| may not be fully reduced. It is only - // bounded by R rather than |m|. However, that is still within bounds for - // |BN_from_montgomery|, which implements full Montgomery reduction, not + // `OPENSSL_BN_ASM_MONT5` codepath, `tmp` may not be fully reduced. It is only + // bounded by R rather than `m`. However, that is still within bounds for + // `BN_from_montgomery`, which implements full Montgomery reduction, not // "almost" Montgomery reduction. if (!BN_from_montgomery(rr, &tmp, mont, ctx)) { goto err; diff --git a/third_party/boringssl/src/crypto/fipsmodule/bn/gcd.cc.inc b/third_party/boringssl/src/crypto/fipsmodule/bn/gcd.cc.inc index 4fdb2e964..d78185ce1 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/bn/gcd.cc.inc +++ b/third_party/boringssl/src/crypto/fipsmodule/bn/gcd.cc.inc @@ -211,7 +211,7 @@ BIGNUM *BN_mod_inverse(BIGNUM *out, const BIGNUM *a, const BIGNUM *n, return nullptr; } - new_out.release(); // Passed to the caller via |out|. + new_out.release(); // Passed to the caller via `out`. return out; } @@ -219,7 +219,7 @@ int BN_mod_inverse_blinded(BIGNUM *out, int *out_no_inverse, const BIGNUM *a, const BN_MONT_CTX *mont, BN_CTX *ctx) { *out_no_inverse = 0; - // |a| is secret, but it is required to be in range, so these comparisons may + // `a` is secret, but it is required to be in range, so these comparisons may // be leaked. if (BN_is_negative(a) || constant_time_declassify_int(BN_cmp(a, &mont->N) >= 0)) { @@ -232,15 +232,15 @@ int BN_mod_inverse_blinded(BIGNUM *out, int *out_no_inverse, const BIGNUM *a, return 0; } - // |BN_mod_inverse_odd| is leaky, so generate a secret blinding factor and - // blind |a|. This works because (ar)^-1 * r = a^-1, supposing r is + // `BN_mod_inverse_odd` is leaky, so generate a secret blinding factor and + // blind `a`. This works because (ar)^-1 * r = a^-1, supposing r is // invertible. If r is not invertible, this function will fail. However, we // only use this in RSA, where stumbling on an uninvertible element means // stumbling on the key's factorization. That is, if this function fails, the // RSA key was not actually a product of two large primes. // // TODO(crbug.com/boringssl/677): When the PRNG output is marked secret by - // default, the explicit |bn_secret| call can be removed. + // default, the explicit `bn_secret` call can be removed. if (!BN_rand_range_ex(blinding_factor.get(), 1, &mont->N)) { return 0; } @@ -249,8 +249,8 @@ int BN_mod_inverse_blinded(BIGNUM *out, int *out_no_inverse, const BIGNUM *a, return 0; } - // Once blinded, |out| is no longer secret, so it may be passed to a leaky - // mod inverse function. Note |blinding_factor| is secret, so |out| will be + // Once blinded, `out` is no longer secret, so it may be passed to a leaky + // mod inverse function. Note `blinding_factor` is secret, so `out` will be // secret again after multiplying. bn_declassify(out); if (!BN_mod_inverse_odd(out, out_no_inverse, out, &mont->N, ctx) || diff --git a/third_party/boringssl/src/crypto/fipsmodule/bn/gcd_extra.cc.inc b/third_party/boringssl/src/crypto/fipsmodule/bn/gcd_extra.cc.inc index a48762fe9..867147da4 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/bn/gcd_extra.cc.inc +++ b/third_party/boringssl/src/crypto/fipsmodule/bn/gcd_extra.cc.inc @@ -72,7 +72,7 @@ static int bn_gcd_consttime(BIGNUM *r, unsigned *out_shift, const BIGNUM *x, return 0; } - // Each loop iteration halves at least one of |u| and |v|. Thus we need at + // Each loop iteration halves at least one of `u` and `v`. Thus we need at // most the combined bit width of inputs for at least one value to be zero. x_bits = x->width * BN_BITS2; y_bits = y->width * BN_BITS2; @@ -86,14 +86,14 @@ static int bn_gcd_consttime(BIGNUM *r, unsigned *out_shift, const BIGNUM *x, for (unsigned i = 0; i < num_iters; i++) { BN_ULONG both_odd = word_is_odd_mask(u->d[0]) & word_is_odd_mask(v->d[0]); - // If both |u| and |v| are odd, subtract the smaller from the larger. + // If both `u` and `v` are odd, subtract the smaller from the larger. BN_ULONG u_less_than_v = (BN_ULONG)0 - bn_sub_words(tmp->d, u->d, v->d, width); bn_select_words(u->d, both_odd & ~u_less_than_v, tmp->d, u->d, width); bn_sub_words(tmp->d, v->d, u->d, width); bn_select_words(v->d, both_odd & u_less_than_v, tmp->d, v->d, width); - // At least one of |u| and |v| is now even. + // At least one of `u` and `v` is now even. BN_ULONG u_is_odd = word_is_odd_mask(u->d[0]); BN_ULONG v_is_odd = word_is_odd_mask(v->d[0]); declassify_assert(!(u_is_odd & v_is_odd)); @@ -106,8 +106,8 @@ static int bn_gcd_consttime(BIGNUM *r, unsigned *out_shift, const BIGNUM *x, maybe_rshift1_words(v->d, ~v_is_odd, tmp->d, width); } - // One of |u| or |v| is zero at this point. The algorithm usually makes |u| - // zero, unless |y| was already zero on input. Fix this by combining the + // One of `u` or `v` is zero at this point. The algorithm usually makes `u` + // zero, unless `y` was already zero on input. Fix this by combining the // values. declassify_assert(BN_is_zero(u) | BN_is_zero(v)); for (size_t i = 0; i < width; i++) { @@ -132,7 +132,7 @@ int bssl::bn_is_relatively_prime(int *out_relatively_prime, const BIGNUM *x, return 0; } - // Check that 2^|shift| * |gcd| is one. + // Check that 2^`shift` * `gcd` is one. if (gcd->width == 0) { *out_relatively_prime = 0; } else { @@ -154,7 +154,7 @@ int bssl::bn_lcm_consttime(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, return gcd != nullptr && // bn_mul_consttime(r, a, b, ctx) && bn_gcd_consttime(gcd, &shift, a, b, ctx) && - // |gcd| has a secret bit width. + // `gcd` has a secret bit width. bn_div_consttime(r, nullptr, r, gcd, /*divisor_min_bits=*/0, ctx) && bn_rshift_secret_shift(r, r, shift, ctx); } @@ -183,9 +183,9 @@ int bssl::bn_mod_inverse_consttime(BIGNUM *r, int *out_no_inverse, // negative numbers. // // For more details and proof of correctness, see - // https://github.com/mit-plv/fiat-crypto/pull/333. In particular, see |step| - // and |mod_inverse_consttime| for the algorithm in Gallina and see - // |mod_inverse_consttime_spec| for the correctness result. + // https://github.com/mit-plv/fiat-crypto/pull/333. In particular, see `step` + // and `mod_inverse_consttime` for the algorithm in Gallina and see + // `mod_inverse_consttime_spec` for the correctness result. if (!BN_is_odd(a) && !BN_is_odd(n)) { *out_no_inverse = 1; @@ -193,8 +193,8 @@ int bssl::bn_mod_inverse_consttime(BIGNUM *r, int *out_no_inverse, return 0; } - // This function exists to compute the RSA private exponent, where |a| is one - // word. We'll thus use |a_width| when available. + // This function exists to compute the RSA private exponent, where `a` is one + // word. We'll thus use `a_width` when available. size_t n_width = n->width, a_width = a->width; if (a_width > n_width) { a_width = n_width; @@ -222,25 +222,25 @@ int bssl::bn_mod_inverse_consttime(BIGNUM *r, int *out_no_inverse, !BN_copy(v, n) || // !BN_one(A) || // !BN_one(D) || - // For convenience, size |u| and |v| equivalently. + // For convenience, size `u` and `v` equivalently. !bn_resize_words(u, n_width) || // !bn_resize_words(v, n_width) || - // |A| and |C| are bounded by |m|. + // `A` and `C` are bounded by `m`. !bn_resize_words(A, n_width) || // !bn_resize_words(C, n_width) || - // |B| and |D| are bounded by |a|. + // `B` and `D` are bounded by `a`. !bn_resize_words(B, a_width) || // !bn_resize_words(D, a_width) || - // |tmp| and |tmp2| may be used at either size. + // `tmp` and `tmp2` may be used at either size. !bn_resize_words(tmp, n_width) || // !bn_resize_words(tmp2, n_width)) { return 0; } - // Each loop iteration halves at least one of |u| and |v|. Thus we need at + // Each loop iteration halves at least one of `u` and `v`. Thus we need at // most the combined bit width of inputs for at least one value to be zero. - // |a_bits| and |n_bits| cannot overflow because |bn_wexpand| ensures bit - // counts fit in even |int|. + // `a_bits` and `n_bits` cannot overflow because `bn_wexpand` ensures bit + // counts fit in even `int`. a_bits = a_width * BN_BITS2; n_bits = n_width * BN_BITS2; num_iters = a_bits + n_bits; @@ -265,7 +265,7 @@ int bssl::bn_mod_inverse_consttime(BIGNUM *r, int *out_no_inverse, for (size_t i = 0; i < num_iters; i++) { BN_ULONG both_odd = word_is_odd_mask(u->d[0]) & word_is_odd_mask(v->d[0]); - // If both |u| and |v| are odd, subtract the smaller from the larger. + // If both `u` and `v` are odd, subtract the smaller from the larger. BN_ULONG v_less_than_u = (BN_ULONG)0 - bn_sub_words(tmp->d, v->d, u->d, n_width); bn_select_words(v->d, both_odd & ~v_less_than_u, tmp->d, v->d, n_width); @@ -285,8 +285,8 @@ int bssl::bn_mod_inverse_consttime(BIGNUM *r, int *out_no_inverse, bn_select_words(B->d, both_odd & v_less_than_u, tmp->d, B->d, a_width); bn_select_words(D->d, both_odd & ~v_less_than_u, tmp->d, D->d, a_width); - // Our loop invariants hold at this point. Additionally, exactly one of |u| - // and |v| is now even. + // Our loop invariants hold at this point. Additionally, exactly one of `u` + // and `v` is now even. BN_ULONG u_is_even = ~word_is_odd_mask(u->d[0]); BN_ULONG v_is_even = ~word_is_odd_mask(v->d[0]); declassify_assert(u_is_even != v_is_even); diff --git a/third_party/boringssl/src/crypto/fipsmodule/bn/internal.h b/third_party/boringssl/src/crypto/fipsmodule/bn/internal.h index 486fae10b..73dc27b51 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/bn/internal.h +++ b/third_party/boringssl/src/crypto/fipsmodule/bn/internal.h @@ -64,7 +64,7 @@ BSSL_NAMESPACE_BEGIN #define BN_MASK2h1 (0xffff8000UL) #define BN_MASK2h (0xffff0000UL) // On some 32-bit platforms, Montgomery multiplication is done using 64-bit -// arithmetic with SIMD instructions. On such platforms, |BN_MONT_CTX::n0| +// arithmetic with SIMD instructions. On such platforms, `BN_MONT_CTX::n0` // needs to be two words long. Only certain 32-bit platforms actually make use // of n0[1] and shorter R value would suffice for the others. However, // currently only the assembly files know which is which. @@ -81,7 +81,7 @@ BSSL_NAMESPACE_BEGIN #define BN_CAN_USE_INLINE_ASM #endif -// MOD_EXP_CTIME_ALIGN is the alignment needed for |BN_mod_exp_mont_consttime|'s +// MOD_EXP_CTIME_ALIGN is the alignment needed for `BN_mod_exp_mont_consttime`'s // tables. // // TODO(davidben): Historically, this alignment came from cache line @@ -91,8 +91,8 @@ BSSL_NAMESPACE_BEGIN // requirements. #define MOD_EXP_CTIME_ALIGN 64 -// MOD_EXP_CTIME_STORAGE_LEN is the number of |BN_ULONG|s needed for the -// |BN_mod_exp_mont_consttime| stack-allocated storage buffer. The buffer is +// MOD_EXP_CTIME_STORAGE_LEN is the number of `BN_ULONG`s needed for the +// `BN_mod_exp_mont_consttime` stack-allocated storage buffer. The buffer is // just the right size for the RSAZ and is about ~1KB larger than what's // necessary (4480 bytes) for 1024-bit inputs. #define MOD_EXP_CTIME_STORAGE_LEN \ @@ -105,64 +105,64 @@ BSSL_NAMESPACE_BEGIN } // bn_minimal_width returns the minimal number of words needed to represent -// |bn|. +// `bn`. int bn_minimal_width(const BIGNUM *bn); -// bn_set_minimal_width sets |bn->width| to |bn_minimal_width(bn)|. If |bn| is -// zero, |bn->neg| is set to zero. +// bn_set_minimal_width sets `bn->width` to `bn_minimal_width(bn)`. If `bn` is +// zero, `bn->neg` is set to zero. void bn_set_minimal_width(BIGNUM *bn); -// bn_wexpand ensures that |bn| has at least |words| works of space without +// bn_wexpand ensures that `bn` has at least `words` works of space without // altering its value. It returns one on success or zero on allocation // failure. int bn_wexpand(BIGNUM *bn, size_t words); -// bn_expand acts the same as |bn_wexpand|, but takes a number of bits rather +// bn_expand acts the same as `bn_wexpand`, but takes a number of bits rather // than a number of words. int bn_expand(BIGNUM *bn, size_t bits); -// bn_resize_words adjusts |bn->width| to be |words|. It returns one on success -// and zero on allocation error or if |bn|'s value is too large. +// bn_resize_words adjusts `bn->width` to be `words`. It returns one on success +// and zero on allocation error or if `bn`'s value is too large. OPENSSL_EXPORT int bn_resize_words(BIGNUM *bn, size_t words); -// bn_select_words sets |r| to |a| if |mask| is all ones or |b| if |mask| is +// bn_select_words sets `r` to `a` if `mask` is all ones or `b` if `mask` is // all zeros. void bn_select_words(BN_ULONG *r, BN_ULONG mask, const BN_ULONG *a, const BN_ULONG *b, size_t num); -// bn_set_words sets |bn| to the value encoded in the |num| words in |words|, +// bn_set_words sets `bn` to the value encoded in the `num` words in `words`, // least significant word first. int bn_set_words(BIGNUM *bn, const BN_ULONG *words, size_t num); -// bn_set_static_words acts like |bn_set_words|, but doesn't copy the data. A -// flag is set on |bn| so that |BN_free| won't attempt to free the data. +// bn_set_static_words acts like `bn_set_words`, but doesn't copy the data. A +// flag is set on `bn` so that `BN_free` won't attempt to free the data. // -// The |STATIC_BIGNUM| macro is probably a better solution for this outside of +// The `STATIC_BIGNUM` macro is probably a better solution for this outside of // the FIPS module. Inside of the FIPS module that macro generates rel.ro data, // which doesn't work with FIPS requirements. void bn_set_static_words(BIGNUM *bn, const BN_ULONG *words, size_t num); -// bn_fits_in_words returns one if |bn| may be represented in |num| words, plus +// bn_fits_in_words returns one if `bn` may be represented in `num` words, plus // a sign bit, and zero otherwise. int bn_fits_in_words(const BIGNUM *bn, size_t num); -// bn_copy_words copies the value of |bn| to |out| and returns one if the value -// is representable in |num| words. Otherwise, it returns zero. +// bn_copy_words copies the value of `bn` to `out` and returns one if the value +// is representable in `num` words. Otherwise, it returns zero. int bn_copy_words(BN_ULONG *out, size_t num, const BIGNUM *bn); -// bn_assert_fits_in_bytes asserts that |bn| fits in |num| bytes. This is a +// bn_assert_fits_in_bytes asserts that `bn` fits in `num` bytes. This is a // no-op in release builds, but triggers an assert in debug builds, and // declassifies all bytes which are therefore known to be zero in constant-time // validation. void bn_assert_fits_in_bytes(const BIGNUM *bn, size_t num); -// bn_secret marks |bn|'s contents, but not its width or sign, as secret. See -// |CONSTTIME_SECRET| for details. +// bn_secret marks `bn`'s contents, but not its width or sign, as secret. See +// `CONSTTIME_SECRET` for details. inline void bn_secret(BIGNUM *bn) { CONSTTIME_SECRET(bn->d, bn->width * sizeof(BN_ULONG)); } -// bn_declassify marks |bn|'s value as public. See |CONSTTIME_DECLASSIFY| for +// bn_declassify marks `bn`'s value as public. See `CONSTTIME_DECLASSIFY` for // details. inline void bn_declassify(BIGNUM *bn) { CONSTTIME_DECLASSIFY(bn->d, bn->width * sizeof(BN_ULONG)); @@ -186,9 +186,9 @@ inline void bn_declassify(BIGNUM *bn) { #define BN_ADD_ASM #endif -// bn_mul_add_words multiples |ap| by |w|, adds the result to |rp|, and places -// the result in |rp|. |ap| and |rp| must both be |num| words long. It returns -// the carry word of the operation. |ap| and |rp| may be equal but otherwise may +// bn_mul_add_words multiples `ap` by `w`, adds the result to `rp`, and places +// the result in `rp`. `ap` and `rp` must both be `num` words long. It returns +// the carry word of the operation. `ap` and `rp` may be equal but otherwise may // not alias. #if defined(BN_MUL_ASM) extern "C" @@ -196,41 +196,40 @@ extern "C" BN_ULONG bn_mul_add_words(BN_ULONG *rp, const BN_ULONG *ap, size_t num, BN_ULONG w); -// bn_mul_words multiples |ap| by |w| and places the result in |rp|. |ap| and -// |rp| must both be |num| words long. It returns the carry word of the -// operation. |ap| and |rp| may be equal but otherwise may not alias. +// bn_mul_words multiples `ap` by `w` and places the result in `rp`. `ap` and +// `rp` must both be `num` words long. It returns the carry word of the +// operation. `ap` and `rp` may be equal but otherwise may not alias. #if defined(BN_MUL_ASM) extern "C" #endif BN_ULONG bn_mul_words(BN_ULONG *rp, const BN_ULONG *ap, size_t num, BN_ULONG w); -// bn_sqr_add_words computes |tmp| where |tmp[2*i]| and |tmp[2*i+1]| are -// |ap[i]|'s square, for all |i| up to |num|, and adds the result to |rp|. If -// the result does not fit in |2*num| words, the final carry bit is truncated. -// |ap| is an array of |num| words and |rp| an array of |2*num| words. |ap| and -// |rp| may not alias. +// bn_sqr_add_words computes `tmp` where `tmp[2*i]` and `tmp[2*i+1]` are +// `ap[i]`'s square, for all `i` up to `num`, and adds the result to `rp`. If +// the result does not fit in `2*num` words, the final carry bit is truncated. +// `ap` is an array of `num` words and `rp` an array of `2*num` words. `ap` and +// `rp` may not alias. // -// This gives the contribution of the |ap[i]*ap[i]| terms when squaring |ap|. +// This gives the contribution of the `ap[i]*ap[i]` terms when squaring `ap`. #if defined(BN_MUL_ASM) extern "C" #endif void bn_sqr_add_words(BN_ULONG *rp, const BN_ULONG *ap, size_t num); -// bn_add_words adds |ap| to |bp| and places the result in |rp|, each of which -// are |num| words long. It returns the carry bit, which is one if the operation -// overflowed and zero otherwise. Any pair of |ap|, |bp|, and |rp| may be equal +// bn_add_words adds `ap` to `bp` and places the result in `rp`, each of which +// are `num` words long. It returns the carry bit, which is one if the operation +// overflowed and zero otherwise. Any pair of `ap`, `bp`, and `rp` may be equal // to each other but otherwise may not alias. - #if defined(BN_ADD_ASM) extern "C" #endif BN_ULONG bn_add_words(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, size_t num); -// bn_sub_words subtracts |bp| from |ap| and places the result in |rp|. It +// bn_sub_words subtracts `bp` from `ap` and places the result in `rp`. It // returns the borrow bit, which is one if the computation underflowed and zero -// otherwise. Any pair of |ap|, |bp|, and |rp| may be equal to each other but +// otherwise. Any pair of `ap`, `bp`, and `rp` may be equal to each other but // otherwise may not alias. #if defined(BN_ADD_ASM) extern "C" @@ -238,76 +237,83 @@ extern "C" BN_ULONG bn_sub_words(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, size_t num); -// bn_mul_comba4 sets |r| to the product of |a| and |b|. +// bn_mul_comba4 sets `r` to the product of `a` and `b`. #if defined(BN_MUL_ASM) extern "C" #endif void bn_mul_comba4(BN_ULONG r[8], const BN_ULONG a[4], const BN_ULONG b[4]); -// bn_mul_comba8 sets |r| to the product of |a| and |b|. +// bn_mul_comba8 sets `r` to the product of `a` and `b`. #if defined(BN_MUL_ASM) extern "C" #endif void bn_mul_comba8(BN_ULONG r[16], const BN_ULONG a[8], const BN_ULONG b[8]); -// bn_sqr_comba8 sets |r| to |a|^2. +// bn_sqr_comba8 sets `r` to `a`². #if defined(BN_MUL_ASM) extern "C" #endif void bn_sqr_comba8(BN_ULONG r[16], const BN_ULONG a[8]); -// bn_sqr_comba4 sets |r| to |a|^2. +// bn_sqr_comba4 sets `r` to `a`². #if defined(BN_MUL_ASM) extern "C" #endif void bn_sqr_comba4(BN_ULONG r[8], const BN_ULONG a[4]); -// bn_less_than_words returns one if |a| < |b| and zero otherwise, where |a| -// and |b| both are |len| words long. It runs in constant time. +// bn_add_carry_words adds `carry` to `ap` and places the result in `rp`. `ap` +// and `rp` are `num` words long and may alias. `carry` must be 0 or 1. It +// returns the carry bit, which is one if the operation overflowed and zero +// otherwise. +BN_ULONG bn_add_carry_words(BN_ULONG *rp, const BN_ULONG *ap, BN_ULONG carry, + size_t num); + +// bn_less_than_words returns one if `a` < `b` and zero otherwise, where `a` +// and `b` both are `len` words long. It runs in constant time. int bn_less_than_words(const BN_ULONG *a, const BN_ULONG *b, size_t len); -// bn_in_range_words returns one if |min_inclusive| <= |a| < |max_exclusive|, -// where |a| and |max_exclusive| both are |len| words long. |a| and -// |max_exclusive| are treated as secret. +// bn_in_range_words returns one if `min_inclusive` <= `a` < `max_exclusive`, +// where `a` and `max_exclusive` both are `len` words long. `a` and +// `max_exclusive` are treated as secret. int bn_in_range_words(const BN_ULONG *a, BN_ULONG min_inclusive, const BN_ULONG *max_exclusive, size_t len); -// bn_rand_range_words sets |out| to a uniformly distributed random number from -// |min_inclusive| to |max_exclusive|. Both |out| and |max_exclusive| are |len| +// bn_rand_range_words sets `out` to a uniformly distributed random number from +// `min_inclusive` to `max_exclusive`. Both `out` and `max_exclusive` are `len` // words long. // -// This function runs in time independent of the result, but |min_inclusive| and -// |max_exclusive| are public data. (Information about the range is unavoidably +// This function runs in time independent of the result, but `min_inclusive` and +// `max_exclusive` are public data. (Information about the range is unavoidably // leaked by how many iterations it took to select a number.) int bn_rand_range_words(BN_ULONG *out, BN_ULONG min_inclusive, const BN_ULONG *max_exclusive, size_t len, const uint8_t additional_data[32]); -// bn_range_secret_range behaves like |BN_rand_range_ex|, but treats -// |max_exclusive| as secret. Because of this constraint, the distribution of +// bn_range_secret_range behaves like `BN_rand_range_ex`, but treats +// `max_exclusive` as secret. Because of this constraint, the distribution of // values returned is more complex. // // Rather than repeatedly generating values until one is in range, which would // leak information, it generates one value. If the value is in range, it sets -// |*out_is_uniform| to one. Otherwise, it sets |*out_is_uniform| to zero, +// `*out_is_uniform` to one. Otherwise, it sets `*out_is_uniform` to zero, // fixing up the value to force it in range. // -// The subset of calls to |bn_rand_secret_range| which set |*out_is_uniform| to +// The subset of calls to `bn_rand_secret_range` which set `*out_is_uniform` to // one are uniformly distributed in the target range. Calls overall are not. // This function is intended for use in situations where the extra values are // still usable and where the number of iterations needed to reach the target // number of uniform outputs may be blinded for negligible probabilities of // timing leaks. // -// Although this function treats |max_exclusive| as secret, it treats the number -// of bits in |max_exclusive| as public. +// Although this function treats `max_exclusive` as secret, it treats the number +// of bits in `max_exclusive` as public. int bn_rand_secret_range(BIGNUM *r, int *out_is_uniform, BN_ULONG min_inclusive, const BIGNUM *max_exclusive); -// BN_MONTGOMERY_MAX_WORDS is the maximum number of words allowed in a |BIGNUM| +// BN_MONTGOMERY_MAX_WORDS is the maximum number of words allowed in a `BIGNUM` // used with Montgomery reduction. Ideally this limit would be applied to all -// |BIGNUM|s, in |bn_wexpand|, but the exactfloat library needs to create 8 MiB +// `BIGNUM`s, in `bn_wexpand`, but the exactfloat library needs to create 8 MiB // values for other operations. // // This limit is set so that one number fits within 2 KiB, giving room to @@ -318,10 +324,10 @@ int bn_rand_secret_range(BIGNUM *r, int *out_is_uniform, BN_ULONG min_inclusive, BSSL_NAMESPACE_END struct bn_mont_ctx_st { - // RR is R^2, reduced modulo |N|. It is used to convert to Montgomery form. It - // is guaranteed to have the same width as |N|. + // RR is R², reduced modulo `N`. It is used to convert to Montgomery form. It + // is guaranteed to have the same width as `N`. BIGNUM RR; - // N is the modulus. It is always stored in minimal form, so |N.width| + // N is the modulus. It is always stored in minimal form, so `N.width` // determines R. BIGNUM N; BN_ULONG n0[BN_MONT_CTX_N0_LIMBS]; // least significant words of (R*Ri-1)/N @@ -333,23 +339,23 @@ BSSL_NAMESPACE_BEGIN (defined(OPENSSL_X86) || defined(OPENSSL_X86_64) || \ defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64)) #define OPENSSL_BN_ASM_MONT -// bn_mul_mont_words writes |ap| * |bp| mod |np| to |rp|, each |num| words -// long. Inputs and outputs are in Montgomery form. |n0| is a pointer to the -// corresponding field in |BN_MONT_CTX|. +// bn_mul_mont_words writes `ap` * `bp` mod `np` to `rp`, each `num` words +// long. Inputs and outputs are in Montgomery form. `n0` is a pointer to the +// corresponding field in `BN_MONT_CTX`. // -// If at least one of |ap| or |bp| is fully reduced, |rp| will be fully reduced. +// If at least one of `ap` or `bp` is fully reduced, `rp` will be fully reduced. // If neither is fully-reduced, the output may not be either. // -// This function allocates up to 2 * |num| words (plus a constant allocation) on -// the stack, so |num| should be at most |BN_MONTGOMERY_MAX_WORDS|. -// Additionally, |num| must be at least 128 / |BN_BITS2|. +// This function allocates up to 2 * `num` words (plus a constant allocation) on +// the stack, so `num` should be at most `BN_MONTGOMERY_MAX_WORDS`. +// Additionally, `num` must be at least 128 / `BN_BITS2`. // // TODO(davidben): The x86_64 implementation expects a 32-bit input and masks // off upper bits. The aarch64 implementation expects a 64-bit input and does -// not. |size_t| is the safer option but not strictly correct for x86_64. But -// the |BN_MONTGOMERY_MAX_WORDS| bound makes this moot. +// not. `size_t` is the safer option but not strictly correct for x86_64. But +// the `BN_MONTGOMERY_MAX_WORDS` bound makes this moot. // -// See also discussion in |ToWord| in abi_test.h for notes on smaller-than-word +// See also discussion in `ToWord` in abi_test.h for notes on smaller-than-word // inputs. extern "C" void bn_mul_mont_words(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, const BN_ULONG *np, @@ -405,8 +411,8 @@ extern "C" void bn_mul_mont_nohw(BN_ULONG *rp, const BN_ULONG *ap, #if !defined(OPENSSL_NO_ASM) && defined(OPENSSL_X86_64) #define OPENSSL_BN_ASM_MONT5 -// The following functions implement |bn_mul_mont_gather5|. See -// |bn_mul_mont_gather5| for details. +// The following functions implement `bn_mul_mont_gather5`. See +// `bn_mul_mont_gather5` for details. inline int bn_mul4x_mont_gather5_capable(int num) { return (num & 7) == 0; } extern "C" void bn_mul4x_mont_gather5(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *table, const BN_ULONG *np, @@ -427,20 +433,20 @@ extern "C" void bn_mul_mont_gather5_nohw( BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *table, const BN_ULONG *np, const BN_ULONG n0[BN_MONT_CTX_N0_LIMBS], int num, int power); -// bn_scatter5 stores |inp| to index |power| of |table|. |inp| and each entry of -// |table| are |num| words long. |power| must be less than 32 and is treated as -// public. |table| must be 32*|num| words long. |table| must be aligned to at +// bn_scatter5 stores `inp` to index `power` of `table`. `inp` and each entry of +// `table` are `num` words long. `power` must be less than 32 and is treated as +// public. `table` must be 32*`num` words long. `table` must be aligned to at // least 16 bytes. extern "C" void bn_scatter5(const BN_ULONG *inp, size_t num, BN_ULONG *table, size_t power); -// bn_gather5 loads index |power| of |table| and stores it in |out|. |out| and -// each entry of |table| are |num| words long. |power| must be less than 32 and -// is treated as secret. |table| must be aligned to at least 16 bytes. +// bn_gather5 loads index `power` of `table` and stores it in `out`. `out` and +// each entry of `table` are `num` words long. `power` must be less than 32 and +// is treated as secret. `table` must be aligned to at least 16 bytes. extern "C" void bn_gather5(BN_ULONG *out, size_t num, const BN_ULONG *table, size_t power); -// The following functions implement |bn_power5|. See |bn_power5| for details. +// The following functions implement `bn_power5`. See `bn_power5` for details. extern "C" void bn_power5_nohw(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *table, const BN_ULONG *np, const BN_ULONG n0[BN_MONT_CTX_N0_LIMBS], int num, @@ -461,10 +467,10 @@ extern "C" void bn_powerx5(BN_ULONG *rp, const BN_ULONG *ap, uint64_t bn_mont_n0(const BIGNUM *n); -// bn_mont_ctx_set_RR_consttime initializes |mont->RR|. It returns one on -// success and zero on error. |mont->N| and |mont->n0| must have been -// initialized already. The bit width of |mont->N| is assumed public, but -// |mont->N| is otherwise treated as secret. +// bn_mont_ctx_set_RR_consttime initializes `mont->RR`. It returns one on +// success and zero on error. `mont->N` and `mont->n0` must have been +// initialized already. The bit width of `mont->N` is assumed public, but +// `mont->N` is otherwise treated as secret. int bn_mont_ctx_set_RR_consttime(BN_MONT_CTX *mont, BN_CTX *ctx); #if defined(_MSC_VER) @@ -485,34 +491,34 @@ int bn_mont_ctx_set_RR_consttime(BN_MONT_CTX *mont, BN_CTX *ctx); #error "Either BN_ULLONG or BN_UMULT_LOHI must be defined on every platform." #endif -// bn_jacobi returns the Jacobi symbol of |a| and |b| (which is -1, 0 or 1), or +// bn_jacobi returns the Jacobi symbol of `a` and `b` (which is -1, 0 or 1), or // -2 on error. int bn_jacobi(const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx); -// bn_is_bit_set_words returns one if bit |bit| is set in |a| and zero +// bn_is_bit_set_words returns one if bit `bit` is set in `a` and zero // otherwise. int bn_is_bit_set_words(const BN_ULONG *a, size_t num, size_t bit); -// bn_one_to_montgomery sets |r| to one in Montgomery form. It returns one on +// bn_one_to_montgomery sets `r` to one in Montgomery form. It returns one on // success and zero on error. This function treats the bit width of the modulus // as public. int bn_one_to_montgomery(BIGNUM *r, const BN_MONT_CTX *mont, BN_CTX *ctx); -// bn_less_than_montgomery_R returns one if |bn| is less than the Montgomery R -// value for |mont| and zero otherwise. +// bn_less_than_montgomery_R returns one if `bn` is less than the Montgomery R +// value for `mont` and zero otherwise. int bn_less_than_montgomery_R(const BIGNUM *bn, const BN_MONT_CTX *mont); -// bn_mod_u16_consttime returns |bn| mod |d|, ignoring |bn|'s sign bit. It runs -// in time independent of the value of |bn|, but it treats |d| as public. +// bn_mod_u16_consttime returns `bn` mod `d`, ignoring `bn`'s sign bit. It runs +// in time independent of the value of `bn`, but it treats `d` as public. OPENSSL_EXPORT uint16_t bn_mod_u16_consttime(const BIGNUM *bn, uint16_t d); -// bn_odd_number_is_obviously_composite returns one if |bn| is divisible by one +// bn_odd_number_is_obviously_composite returns one if `bn` is divisible by one // of the first several odd primes and zero otherwise. int bn_odd_number_is_obviously_composite(const BIGNUM *bn); // A BN_MILLER_RABIN stores state common to each Miller-Rabin iteration. It is -// initialized within an existing |BN_CTX| scope and may not be used after -// that scope is released with |BN_CTX_end|. Field names match those in FIPS +// initialized within an existing `BN_CTX` scope and may not be used after +// that scope is released with `BN_CTX_end`. Field names match those in FIPS // 186-5, section B.3.1. typedef struct { // w1 is w-1. @@ -529,46 +535,46 @@ typedef struct { int a; } BN_MILLER_RABIN; -// bn_miller_rabin_init initializes |miller_rabin| for testing if |mont->N| is +// bn_miller_rabin_init initializes `miller_rabin` for testing if `mont->N` is // prime. It returns one on success and zero on error. OPENSSL_EXPORT int bn_miller_rabin_init(BN_MILLER_RABIN *miller_rabin, const BN_MONT_CTX *mont, BN_CTX *ctx); // bn_miller_rabin_iteration performs one Miller-Rabin iteration, checking if -// |b| is a composite witness for |mont->N|. |miller_rabin| must have been -// initialized with |bn_miller_rabin_setup|. On success, it returns one and sets -// |*out_is_possibly_prime| to one if |mont->N| may still be prime or zero if -// |b| shows it is composite. On allocation or internal failure, it returns +// `b` is a composite witness for `mont->N`. `miller_rabin` must have been +// initialized with `bn_miller_rabin_setup`. On success, it returns one and sets +// `*out_is_possibly_prime` to one if `mont->N` may still be prime or zero if +// `b` shows it is composite. On allocation or internal failure, it returns // zero. OPENSSL_EXPORT int bn_miller_rabin_iteration( const BN_MILLER_RABIN *miller_rabin, int *out_is_possibly_prime, const BIGNUM *b, const BN_MONT_CTX *mont, BN_CTX *ctx); -// bn_rshift1_words sets |r| to |a| >> 1, where both arrays are |num| bits wide. +// bn_rshift1_words sets `r` to `a` >> 1, where both arrays are `num` bits wide. void bn_rshift1_words(BN_ULONG *r, const BN_ULONG *a, size_t num); -// bn_rshift_words sets |r| to |a| >> |shift|, where both arrays are |num| bits +// bn_rshift_words sets `r` to `a` >> `shift`, where both arrays are `num` bits // wide. void bn_rshift_words(BN_ULONG *r, const BN_ULONG *a, unsigned shift, size_t num); -// bn_rshift_secret_shift behaves like |BN_rshift| but runs in time independent -// of both |a| and |n|. +// bn_rshift_secret_shift behaves like `BN_rshift` but runs in time independent +// of both `a` and `n`. OPENSSL_EXPORT int bn_rshift_secret_shift(BIGNUM *r, const BIGNUM *a, unsigned n, BN_CTX *ctx); -// bn_reduce_once sets |r| to |a| mod |m| where 0 <= |a| < 2*|m|. It returns -// zero if |a| < |m| and a mask of all ones if |a| >= |m|. Each array is |num| -// words long, but |a| has an additional word specified by |carry|. |carry| must -// be zero or one, as implied by the bounds on |a|. +// bn_reduce_once sets `r` to `a` mod `m` where 0 <= `a` < 2*`m`. It returns +// zero if `a` < `m` and a mask of all ones if `a` >= `m`. Each array is `num` +// words long, but `a` has an additional word specified by `carry`. `carry` must +// be zero or one, as implied by the bounds on `a`. // -// |r|, |a|, and |m| may not alias. Use |bn_reduce_once_in_place| if |r| and |a| +// `r`, `a`, and `m` may not alias. Use `bn_reduce_once_in_place` if `r` and `a` // must alias. BN_ULONG bn_reduce_once(BN_ULONG *r, const BN_ULONG *a, BN_ULONG carry, const BN_ULONG *m, size_t num); -// bn_reduce_once_in_place behaves like |bn_reduce_once| but acts in-place on -// |r|, using |tmp| as scratch space. |r|, |tmp|, and |m| may not alias. +// bn_reduce_once_in_place behaves like `bn_reduce_once` but acts in-place on +// `r`, using `tmp` as scratch space. `r`, `tmp`, and `m` may not alias. BN_ULONG bn_reduce_once_in_place(BN_ULONG *r, BN_ULONG carry, const BN_ULONG *m, BN_ULONG *tmp, size_t num); @@ -576,64 +582,64 @@ BN_ULONG bn_reduce_once_in_place(BN_ULONG *r, BN_ULONG carry, const BN_ULONG *m, // Constant-time non-modular arithmetic. // // The following functions implement non-modular arithmetic in constant-time -// and pessimally set |r->width| to the largest possible word size. +// and pessimally set `r->width` to the largest possible word size. // // Note this means that, e.g., repeatedly multiplying by one will cause widths // to increase without bound. The corresponding public API functions minimize // their outputs to avoid regressing calculator consumers. -// bn_uadd_consttime behaves like |BN_uadd|, but it pessimally sets -// |r->width| = |a->width| + |b->width| + 1. +// bn_uadd_consttime behaves like `BN_uadd`, but it pessimally sets +// `r->width` = `a->width` + `b->width` + 1. int bn_uadd_consttime(BIGNUM *r, const BIGNUM *a, const BIGNUM *b); -// bn_usub_consttime behaves like |BN_usub|, but it pessimally sets -// |r->width| = |a->width|. +// bn_usub_consttime behaves like `BN_usub`, but it pessimally sets +// `r->width` = `a->width`. int bn_usub_consttime(BIGNUM *r, const BIGNUM *a, const BIGNUM *b); -// bn_abs_sub_consttime sets |r| to the absolute value of |a| - |b|, treating +// bn_abs_sub_consttime sets `r` to the absolute value of `a` - `b`, treating // both inputs as secret. It returns one on success and zero on error. OPENSSL_EXPORT int bn_abs_sub_consttime(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx); -// bn_mul_consttime behaves like |BN_mul|, but it rejects negative inputs and -// pessimally sets |r->width| to |a->width| + |b->width|, to avoid leaking -// information about |a| and |b|. +// bn_mul_consttime behaves like `BN_mul`, but it rejects negative inputs and +// pessimally sets `r->width` to `a->width` + `b->width`, to avoid leaking +// information about `a` and `b`. int bn_mul_consttime(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx); -// bn_sqrt_consttime behaves like |BN_sqrt|, but it pessimally sets |r->width| -// to 2*|a->width|, to avoid leaking information about |a| and |b|. +// bn_sqrt_consttime behaves like `BN_sqrt`, but it pessimally sets `r->width` +// to 2*`a->width`, to avoid leaking information about `a` and `b`. int bn_sqr_consttime(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx); -// bn_div_consttime behaves like |BN_div|, but it rejects negative inputs and +// bn_div_consttime behaves like `BN_div`, but it rejects negative inputs and // treats both inputs, including their magnitudes, as secret. It is, as a -// result, much slower than |BN_div| and should only be used for rare operations -// where Montgomery reduction is not available. |divisor_min_bits| is a -// public lower bound for |BN_num_bits(divisor)|. When |divisor|'s bit width is +// result, much slower than `BN_div` and should only be used for rare operations +// where Montgomery reduction is not available. `divisor_min_bits` is a +// public lower bound for `BN_num_bits(divisor)`. When `divisor`'s bit width is // public, this can speed up the operation. // -// Note that |quotient->width| will be set pessimally to |numerator->width|. +// Note that `quotient->width` will be set pessimally to `numerator->width`. OPENSSL_EXPORT int bn_div_consttime(BIGNUM *quotient, BIGNUM *remainder, const BIGNUM *numerator, const BIGNUM *divisor, unsigned divisor_min_bits, BN_CTX *ctx); -// bn_is_relatively_prime checks whether GCD(|x|, |y|) is one. On success, it -// returns one and sets |*out_relatively_prime| to one if the GCD was one and +// bn_is_relatively_prime checks whether GCD(`x`, `y`) is one. On success, it +// returns one and sets `*out_relatively_prime` to one if the GCD was one and // zero otherwise. On error, it returns zero. OPENSSL_EXPORT int bn_is_relatively_prime(int *out_relatively_prime, const BIGNUM *x, const BIGNUM *y, BN_CTX *ctx); -// bn_lcm_consttime sets |r| to LCM(|a|, |b|). It returns one and success and -// zero on error. |a| and |b| are both treated as secret. +// bn_lcm_consttime sets `r` to LCM(`a`, `b`). It returns one and success and +// zero on error. `a` and `b` are both treated as secret. OPENSSL_EXPORT int bn_lcm_consttime(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx); -// bn_mont_ctx_init zero-initialies |mont|. +// bn_mont_ctx_init zero-initialies `mont`. void bn_mont_ctx_init(BN_MONT_CTX *mont); -// bn_mont_ctx_cleanup releases memory associated with |mont|, without freeing -// |mont| itself. +// bn_mont_ctx_cleanup releases memory associated with `mont`, without freeing +// `mont` itself. void bn_mont_ctx_cleanup(BN_MONT_CTX *mont); @@ -641,65 +647,65 @@ void bn_mont_ctx_cleanup(BN_MONT_CTX *mont); // // The following functions implement basic constant-time modular arithmetic. -// bn_mod_add_words sets |r| to |a| + |b| (mod |m|), using |tmp| as scratch -// space. Each array is |num| words long. |a| and |b| must be < |m|. Any pair of -// |r|, |a|, and |b| may alias. +// bn_mod_add_words sets `r` to `a` + `b` (mod `m`), using `tmp` as scratch +// space. Each array is `num` words long. `a` and `b` must be < `m`. Any pair of +// `r`, `a`, and `b` may alias. void bn_mod_add_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, const BN_ULONG *m, BN_ULONG *tmp, size_t num); -// bn_mod_add_consttime acts like |BN_mod_add_quick| but takes a |BN_CTX|. +// bn_mod_add_consttime acts like `BN_mod_add_quick` but takes a `BN_CTX`. int bn_mod_add_consttime(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m, BN_CTX *ctx); -// bn_mod_sub_words sets |r| to |a| - |b| (mod |m|), using |tmp| as scratch -// space. Each array is |num| words long. |a| and |b| must be < |m|. Any pair of -// |r|, |a|, and |b| may alias. +// bn_mod_sub_words sets `r` to `a` - `b` (mod `m`), using `tmp` as scratch +// space. Each array is `num` words long. `a` and `b` must be < `m`. Any pair of +// `r`, `a`, and `b` may alias. void bn_mod_sub_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, const BN_ULONG *m, BN_ULONG *tmp, size_t num); -// bn_mod_sub_consttime acts like |BN_mod_sub_quick| but takes a |BN_CTX|. +// bn_mod_sub_consttime acts like `BN_mod_sub_quick` but takes a `BN_CTX`. int bn_mod_sub_consttime(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m, BN_CTX *ctx); -// bn_mod_lshift1_consttime acts like |BN_mod_lshift1_quick| but takes a -// |BN_CTX|. +// bn_mod_lshift1_consttime acts like `BN_mod_lshift1_quick` but takes a +// `BN_CTX`. int bn_mod_lshift1_consttime(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx); -// bn_mod_lshift_consttime acts like |BN_mod_lshift_quick| but takes a |BN_CTX|. +// bn_mod_lshift_consttime acts like `BN_mod_lshift_quick` but takes a `BN_CTX`. int bn_mod_lshift_consttime(BIGNUM *r, const BIGNUM *a, int n, const BIGNUM *m, BN_CTX *ctx); -// bn_mod_inverse_consttime sets |r| to |a|^-1, mod |n|. |a| must be non- -// negative and less than |n|. It returns one on success and zero on error. On -// failure, if the failure was caused by |a| having no inverse mod |n| then -// |*out_no_inverse| will be set to one; otherwise it will be set to zero. +// bn_mod_inverse_consttime sets `r` to `a`^-1, mod `n`. `a` must be non- +// negative and less than `n`. It returns one on success and zero on error. On +// failure, if the failure was caused by `a` having no inverse mod `n` then +// `*out_no_inverse` will be set to one; otherwise it will be set to zero. // -// This function treats both |a| and |n| as secret, provided they are both non- +// This function treats both `a` and `n` as secret, provided they are both non- // zero and the inverse exists. It should only be used for even moduli where // none of the less general implementations are applicable. OPENSSL_EXPORT int bn_mod_inverse_consttime(BIGNUM *r, int *out_no_inverse, const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx); -// bn_mod_inverse_prime sets |out| to the modular inverse of |a| modulo |p|, +// bn_mod_inverse_prime sets `out` to the modular inverse of `a` modulo `p`, // computed with Fermat's Little Theorem. It returns one on success and zero on -// error. If |mont_p| is NULL, one will be computed temporarily. +// error. If `mont_p` is NULL, one will be computed temporarily. int bn_mod_inverse_prime(BIGNUM *out, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx, const BN_MONT_CTX *mont_p); -// bn_mod_inverse_secret_prime behaves like |bn_mod_inverse_prime| but uses -// |BN_mod_exp_mont_consttime| instead of |BN_mod_exp_mont| in hopes of +// bn_mod_inverse_secret_prime behaves like `bn_mod_inverse_prime` but uses +// `BN_mod_exp_mont_consttime` instead of `BN_mod_exp_mont` in hopes of // protecting the exponent. int bn_mod_inverse_secret_prime(BIGNUM *out, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx, const BN_MONT_CTX *mont_p); -// BN_MONT_CTX_set_locked takes |lock| and checks whether |*pmont| is NULL. If -// so, it creates a new |BN_MONT_CTX| and sets the modulus for it to |mod|. It -// then stores it as |*pmont|. It returns one on success and zero on error. Note -// this function assumes |mod| is public. +// BN_MONT_CTX_set_locked takes `lock` and checks whether `*pmont` is NULL. If +// so, it creates a new `BN_MONT_CTX` and sets the modulus for it to `mod`. It +// then stores it as `*pmont`. It returns one on success and zero on error. Note +// this function assumes `mod` is public. // -// If |*pmont| is already non-NULL then it does nothing and returns one. +// If `*pmont` is already non-NULL then it does nothing and returns one. int BN_MONT_CTX_set_locked(UniquePtr *pmont, Mutex *lock, const BIGNUM *mod, BN_CTX *bn_ctx); @@ -720,80 +726,80 @@ int BN_MONT_CTX_set_locked(UniquePtr *pmont, Mutex *lock, #define BN_SMALL_MAX_WORDS 9 #endif -// bn_mul_small sets |r| to |a|*|b|. |num_r| must be |num_a| + |num_b|. |r| may -// not alias with |a| or |b|. +// bn_mul_small sets `r` to `a`*`b`. `num_r` must be `num_a` + `num_b`. `r` may +// not alias with `a` or `b`. void bn_mul_small(BN_ULONG *r, size_t num_r, const BN_ULONG *a, size_t num_a, const BN_ULONG *b, size_t num_b); -// bn_sqr_small sets |r| to |a|^2. |num_r| must be |num_a|*2. |r| and |a| may +// bn_sqr_small sets `r` to `a`². `num_r` must be `num_a`*2. `r` and `a` may // not alias. void bn_sqr_small(BN_ULONG *r, size_t num_r, const BN_ULONG *a, size_t num_a); -// In the following functions, the modulus must be at most |BN_SMALL_MAX_WORDS| +// In the following functions, the modulus must be at most `BN_SMALL_MAX_WORDS` // words long. -// bn_to_montgomery_small sets |r| to |a| translated to the Montgomery domain. -// |r| and |a| are |num| words long, which must be |mont->N.width|. |a| must be -// fully reduced and may alias |r|. +// bn_to_montgomery_small sets `r` to `a` translated to the Montgomery domain. +// `r` and `a` are `num` words long, which must be `mont->N.width`. `a` must be +// fully reduced and may alias `r`. void bn_to_montgomery_small(BN_ULONG *r, const BN_ULONG *a, size_t num, const BN_MONT_CTX *mont); -// bn_from_montgomery_small sets |r| to |a| translated out of the Montgomery -// domain. |r| and |a| are |num_r| and |num_a| words long, respectively. |num_r| -// must be |mont->N.width|. |a| must be at most |mont->N|^2 and may alias |r|. +// bn_from_montgomery_small sets `r` to `a` translated out of the Montgomery +// domain. `r` and `a` are `num_r` and `num_a` words long, respectively. `num_r` +// must be `mont->N.width`. `a` must be at most `mont->N`² and may alias `r`. // -// Unlike most of these functions, only |num_r| is bounded by -// |BN_SMALL_MAX_WORDS|. |num_a| may exceed it, but must be at most 2 * |num_r|. +// Unlike most of these functions, only `num_r` is bounded by +// `BN_SMALL_MAX_WORDS`. `num_a` may exceed it, but must be at most 2 * `num_r`. void bn_from_montgomery_small(BN_ULONG *r, size_t num_r, const BN_ULONG *a, size_t num_a, const BN_MONT_CTX *mont); -// bn_mod_mul_montgomery_small sets |r| to |a| * |b| mod |mont->N|. Both inputs -// and outputs are in the Montgomery domain. Each array is |num| words long, -// which must be |mont->N.width|. Any two of |r|, |a|, and |b| may alias. |a| -// and |b| must be reduced on input. +// bn_mod_mul_montgomery_small sets `r` to `a` * `b` mod `mont->N`. Both inputs +// and outputs are in the Montgomery domain. Each array is `num` words long, +// which must be `mont->N.width`. Any two of `r`, `a`, and `b` may alias. `a` +// and `b` must be reduced on input. void bn_mod_mul_montgomery_small(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, size_t num, const BN_MONT_CTX *mont); -// bn_mod_exp_mont_small sets |r| to |a|^|p| mod |mont->N|. It returns one on +// bn_mod_exp_mont_small sets `r` to `a`^`p` mod `mont->N`. It returns one on // success and zero on programmer or internal error. Both inputs and outputs are -// in the Montgomery domain. |r| and |a| are |num| words long, which must be -// |mont->N.width| and at most |BN_SMALL_MAX_WORDS|. |num_p|, measured in bits, -// must fit in |size_t|. |a| must be fully-reduced. This function runs in time -// independent of |a|, but |p| and |mont->N| are public values. |a| must be -// fully-reduced and may alias with |r|. +// in the Montgomery domain. `r` and `a` are `num` words long, which must be +// `mont->N.width` and at most `BN_SMALL_MAX_WORDS`. `num_p`, measured in bits, +// must fit in `size_t`. `a` must be fully-reduced. This function runs in time +// independent of `a`, but `p` and `mont->N` are public values. `a` must be +// fully-reduced and may alias with `r`. // -// Note this function differs from |BN_mod_exp_mont| which uses Montgomery +// Note this function differs from `BN_mod_exp_mont` which uses Montgomery // reduction but takes input and output outside the Montgomery domain. Combine -// this function with |bn_from_montgomery_small| and |bn_to_montgomery_small| +// this function with `bn_from_montgomery_small` and `bn_to_montgomery_small` // if necessary. void bn_mod_exp_mont_small(BN_ULONG *r, const BN_ULONG *a, size_t num, const BN_ULONG *p, size_t num_p, const BN_MONT_CTX *mont); -// bn_mod_inverse0_prime_mont_small sets |r| to |a|^-1 mod |mont->N|. If |a| is -// zero, |r| is set to zero. |mont->N| must be a prime. |r| and |a| are |num| -// words long, which must be |mont->N.width| and at most |BN_SMALL_MAX_WORDS|. -// |a| must be fully-reduced and may alias |r|. This function runs in time -// independent of |a|, but |mont->N| is a public value. +// bn_mod_inverse0_prime_mont_small sets `r` to `a`^-1 mod `mont->N`. If `a` is +// zero, `r` is set to zero. `mont->N` must be a prime. `r` and `a` are `num` +// words long, which must be `mont->N.width` and at most `BN_SMALL_MAX_WORDS`. +// `a` must be fully-reduced and may alias `r`. This function runs in time +// independent of `a`, but `mont->N` is a public value. void bn_mod_inverse0_prime_mont_small(BN_ULONG *r, const BN_ULONG *a, size_t num, const BN_MONT_CTX *mont); // Word-based byte conversion functions. -// bn_big_endian_to_words interprets |in_len| bytes from |in| as a big-endian, -// unsigned integer and writes the result to |out_len| words in |out|. |out_len| -// must be large enough to represent any |in_len|-byte value. That is, |in_len| -// must be at most |BN_BYTES * out_len|. +// bn_big_endian_to_words interprets `in_len` bytes from `in` as a big-endian, +// unsigned integer and writes the result to `out_len` words in `out`. `out_len` +// must be large enough to represent any `in_len`-byte value. That is, `in_len` +// must be at most `BN_BYTES * out_len`. void bn_big_endian_to_words(BN_ULONG *out, size_t out_len, const uint8_t *in, size_t in_len); -// bn_words_to_big_endian represents |in_len| words from |in| as a big-endian, -// unsigned integer in |out_len| bytes. It writes the result to |out|. |out_len| -// must be large enough to represent |in| without truncation. +// bn_words_to_big_endian represents `in_len` words from `in` as a big-endian, +// unsigned integer in `out_len` bytes. It writes the result to `out`. `out_len` +// must be large enough to represent `in` without truncation. // -// Note |out_len| may be less than |BN_BYTES * in_len| if |in| is known to have +// Note `out_len` may be less than `BN_BYTES * in_len` if `in` is known to have // leading zeros. void bn_words_to_big_endian(uint8_t *out, size_t out_len, const BN_ULONG *in, size_t in_len); diff --git a/third_party/boringssl/src/crypto/fipsmodule/bn/jacobi.cc.inc b/third_party/boringssl/src/crypto/fipsmodule/bn/jacobi.cc.inc index f04bcc981..a3126dd01 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/bn/jacobi.cc.inc +++ b/third_party/boringssl/src/crypto/fipsmodule/bn/jacobi.cc.inc @@ -79,7 +79,7 @@ int bssl::bn_jacobi(const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx) { } if (i & 1) { // i is odd - // multiply 'ret' by $(-1)^{(B^2-1)/8}$ + // multiply 'ret' by $(-1)^{(B²-1)/8}$ ret = ret * tab[BN_lsw(B) & 7]; } diff --git a/third_party/boringssl/src/crypto/fipsmodule/bn/montgomery.cc.inc b/third_party/boringssl/src/crypto/fipsmodule/bn/montgomery.cc.inc index 4bc6247c1..4be4ecf70 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/bn/montgomery.cc.inc +++ b/third_party/boringssl/src/crypto/fipsmodule/bn/montgomery.cc.inc @@ -95,7 +95,7 @@ static int bn_mont_ctx_set_N_and_n0(BN_MONT_CTX *mont, const BIGNUM *mod) { OPENSSL_PUT_ERROR(BN, ERR_R_INTERNAL_ERROR); return 0; } - // |mont->N| is always stored minimally. Computing RR efficiently leaks the + // `mont->N` is always stored minimally. Computing RR efficiently leaks the // size of the modulus. While the modulus may be private in RSA (one of the // primes), their sizes are public, so this is fine. bn_set_minimal_width(&mont->N); @@ -103,8 +103,8 @@ static int bn_mont_ctx_set_N_and_n0(BN_MONT_CTX *mont, const BIGNUM *mod) { // Find n0 such that n0 * N == -1 (mod r). // // Only certain BN_BITS2<=32 platforms actually make use of n0[1]. For the - // others, we could use a shorter R value and use faster |BN_ULONG|-based - // math instead of |uint64_t|-based math, which would be double-precision. + // others, we could use a shorter R value and use faster `BN_ULONG`-based + // math instead of `uint64_t`-based math, which would be double-precision. // However, currently only the assembler files know which is which. static_assert(BN_MONT_CTX_N0_LIMBS == 1 || BN_MONT_CTX_N0_LIMBS == 2, "BN_MONT_CTX_N0_LIMBS value is invalid"); @@ -134,9 +134,9 @@ int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, BN_CTX *ctx) { // Save RR = R**2 (mod N). R is the smallest power of 2**BN_BITS2 such that R // > mod. Even though the assembly on some 32-bit platforms works with 64-bit - // values, using |BN_BITS2| here, rather than |BN_MONT_CTX_N0_LIMBS * - // BN_BITS2|, is correct because R**2 will still be a multiple of the latter - // as |BN_MONT_CTX_N0_LIMBS| is either one or two. + // values, using `BN_BITS2` here, rather than + // `BN_MONT_CTX_N0_LIMBS * BN_BITS2`, is correct because R**2 will still be a + // multiple of the latter as `BN_MONT_CTX_N0_LIMBS` is either one or two. unsigned lgBigR = mont->N.width * BN_BITS2; BN_zero(&mont->RR); int ok = BN_set_bit(&mont->RR, lgBigR * 2) && @@ -196,9 +196,9 @@ static int bn_from_montgomery_in_place(BN_ULONG *r, size_t num_r, BN_ULONG *a, return 0; } - // Add multiples of |n| to |r| until R = 2^(nl * BN_BITS2) divides it. On - // input, we had |r| < |n| * R, so now |r| < 2 * |n| * R. Note that |r| - // includes |carry| which is stored separately. + // Add multiples of `n` to `r` until R = 2^(nl * BN_BITS2) divides it. On + // input, we had `r` < `n` * R, so now `r` < 2 * `n` * R. Note that `r` + // includes `carry` which is stored separately. BN_ULONG n0 = mont->n0[0]; BN_ULONG carry = 0; for (size_t i = 0; i < num_n; i++) { @@ -206,11 +206,11 @@ static int bn_from_montgomery_in_place(BN_ULONG *r, size_t num_r, BN_ULONG *a, a[i + num_n] = CRYPTO_addc_w(a[i + num_n], v, carry, &carry); } - // Shift |num_n| words to divide by R. We have |a| < 2 * |n|. Note that |a| - // includes |carry| which is stored separately. + // Shift `num_n` words to divide by R. We have `a` < 2 * `n`. Note that `a` + // includes `carry` which is stored separately. a += num_n; - // |a| thus requires at most one additional subtraction |n| to be reduced. + // `a` thus requires at most one additional subtraction `n` to be reduced. bn_reduce_once(r, a, carry, n, num_n); return 1; } @@ -251,8 +251,8 @@ int BN_from_montgomery(BIGNUM *r, const BIGNUM *a, const BN_MONT_CTX *mont, int bssl::bn_one_to_montgomery(BIGNUM *r, const BN_MONT_CTX *mont, BN_CTX *ctx) { - // If the high bit of |n| is set, R = 2^(width*BN_BITS2) < 2 * |n|, so we - // compute R - |n| rather than perform Montgomery reduction. + // If the high bit of `n` is set, R = 2^(width*BN_BITS2) < 2 * `n`, so we + // compute R - `n` rather than perform Montgomery reduction. const BIGNUM *n = &mont->N; if (n->width > 0 && (n->d[n->width - 1] >> (BN_BITS2 - 1)) != 0) { if (!bn_wexpand(r, n->width)) { @@ -306,14 +306,14 @@ int BN_mod_mul_montgomery(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, } #if defined(OPENSSL_BN_ASM_MONT) - // |bn_mul_mont_words| requires at least 128 bits of limbs. + // `bn_mul_mont_words` requires at least 128 bits of limbs. int num = mont->N.width; if (num >= (128 / BN_BITS2) && a->width == num && b->width == num) { if (!bn_wexpand(r, num)) { return 0; } - // This bound is implied by |bn_mont_ctx_set_N_and_n0|. |bn_mul_mont_words| - // allocates |num| words on the stack, so |num| cannot be too large. + // This bound is implied by `bn_mont_ctx_set_N_and_n0`. `bn_mul_mont_words` + // allocates `num` words on the stack, so `num` cannot be too large. assert((size_t)num <= BN_MONTGOMERY_MAX_WORDS); bn_mul_mont_words(r->d, a->d, b->d, mont->N.d, mont->n0, num); r->neg = 0; @@ -357,7 +357,7 @@ void bssl::bn_mod_mul_montgomery_small(BN_ULONG *r, const BN_ULONG *a, } #if defined(OPENSSL_BN_ASM_MONT) - // |bn_mul_mont_words| requires at least 128 bits of limbs. + // `bn_mul_mont_words` requires at least 128 bits of limbs. if (num >= (128 / BN_BITS2)) { bn_mul_mont_words(r, a, b, mont->N.d, mont->n0, num); return; diff --git a/third_party/boringssl/src/crypto/fipsmodule/bn/montgomery_inv.cc.inc b/third_party/boringssl/src/crypto/fipsmodule/bn/montgomery_inv.cc.inc index e4ee08961..cde73732d 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/bn/montgomery_inv.cc.inc +++ b/third_party/boringssl/src/crypto/fipsmodule/bn/montgomery_inv.cc.inc @@ -30,30 +30,30 @@ static_assert(sizeof(BN_ULONG) * BN_MONT_CTX_N0_LIMBS == sizeof(uint64_t), "uint64_t is insufficient precision for n0"); uint64_t bssl::bn_mont_n0(const BIGNUM *n) { - // These conditions are checked by the caller, |BN_MONT_CTX_set| or - // |BN_MONT_CTX_new_consttime|. + // These conditions are checked by the caller, `BN_MONT_CTX_set` or + // `BN_MONT_CTX_new_consttime`. assert(!BN_is_zero(n)); assert(!BN_is_negative(n)); assert(BN_is_odd(n)); // r == 2**(BN_MONT_CTX_N0_LIMBS * BN_BITS2) ensures that we can do integer - // division by |r| by simply ignoring |BN_MONT_CTX_N0_LIMBS| limbs. Similarly, - // we can calculate values modulo |r| by just looking at the lowest - // |BN_MONT_CTX_N0_LIMBS| limbs. This is what makes Montgomery multiplication + // division by `r` by simply ignoring `BN_MONT_CTX_N0_LIMBS` limbs. Similarly, + // we can calculate values modulo `r` by just looking at the lowest + // `BN_MONT_CTX_N0_LIMBS` limbs. This is what makes Montgomery multiplication // efficient. // // As shown in Algorithm 1 of "Fast Prime Field Elliptic Curve Cryptography // with 256 Bit Primes" by Shay Gueron and Vlad Krasnov, in the loop of a - // multi-limb Montgomery multiplication of |a * b (mod n)|, given the - // unreduced product |t == a * b|, we repeatedly calculate: + // multi-limb Montgomery multiplication of `a * b (mod n)`, given the + // unreduced product `t == a * b`, we repeatedly calculate: // - // t1 := t % r |t1| is |t|'s lowest limb (see previous paragraph). + // t1 := t % r `t1` is `t`'s lowest limb (see previous paragraph). // t2 := t1*n0*n // t3 := t + t2 - // t := t3 / r copy all limbs of |t3| except the lowest to |t|. + // t := t3 / r copy all limbs of `t3` except the lowest to `t`. // // In the last step, it would only make sense to ignore the lowest limb of - // |t3| if it were zero. The middle steps ensure that this is the case: + // `t3` if it were zero. The middle steps ensure that this is the case: // // t3 == 0 (mod r) // t + t2 == 0 (mod r) @@ -64,10 +64,10 @@ uint64_t bssl::bn_mont_n0(const BIGNUM *n) { // n0 == -1/n (mod r) // // Thus, in each iteration of the loop, we multiply by the constant factor - // |n0|, the negative inverse of n (mod r). + // `n0`, the negative inverse of n (mod r). // n_mod_r = n % r. As explained above, this is done by taking the lowest - // |BN_MONT_CTX_N0_LIMBS| limbs of |n|. + // `BN_MONT_CTX_N0_LIMBS` limbs of `n`. uint64_t n_mod_r = n->d[0]; #if BN_MONT_CTX_N0_LIMBS == 2 if (n->width > 1) { @@ -80,7 +80,7 @@ uint64_t bssl::bn_mont_n0(const BIGNUM *n) { return bn_neg_inv_mod_u64(n_mod_r); } -// bn_neg_inv_mod_u64 calculates -1/n mod 2^64. |n| must be odd. +// bn_neg_inv_mod_u64 calculates -1/n mod 2^64. `n` must be odd. static uint64_t bn_neg_inv_mod_u64(uint64_t n) { // This is a modified version of the technique described in // https://crypto.stackexchange.com/a/47496 and @@ -89,10 +89,10 @@ static uint64_t bn_neg_inv_mod_u64(uint64_t n) { // negation happens before we go to double-word precision, instead of at the // end. // - // If r = -n^-1 (mod m), then r * (r*n + 2) is -n^(-1) (mod m^2). This is + // If r = -n^-1 (mod m), then r * (r*n + 2) is -n^-1 (mod m²). This is // because, for some k, r*n = k*m - 1. Then: // - // r*n * (r*n + 2) = (k*m - 1) * (k*m + 1) = k^2*m^2 - 1 = -1 (mod m^2) + // r*n * (r*n + 2) = (k*m - 1) * (k*m + 1) = k²*m² - 1 = -1 (mod m²) // // We start with the negative inverse mod some small power of 2 and square the // modulus up to 2^64. n = n^-1 (mod 8) for all odd n, so r = -n (mod 8). From @@ -136,8 +136,8 @@ int bssl::bn_mont_ctx_set_RR_consttime(BN_MONT_CTX *mont, BN_CTX *ctx) { // Montgomery domain, 2R or 2^(lgBigR+1), and then use Montgomery // square-and-multiply to exponentiate. // - // The square steps take 2^n R to (2^n)*(2^n) R = 2^2n R. This is the same as - // doubling 2^n R, n times (doubling any x, n times, computes 2^n * x). When n + // The square steps take 2ⁿ R to (2ⁿ)*(2ⁿ) R = 2^2n R. This is the same as + // doubling 2ⁿ R, n times (doubling any x, n times, computes 2ⁿ * x). When n // is below some threshold, doubling is faster; when above, squaring is // faster. From benchmarking various 32-bit and 64-bit architectures, the word // count seems to work well as a threshold. (Doubling scales linearly and diff --git a/third_party/boringssl/src/crypto/fipsmodule/bn/mul.cc.inc b/third_party/boringssl/src/crypto/fipsmodule/bn/mul.cc.inc index 82746ad10..3b3951f5b 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/bn/mul.cc.inc +++ b/third_party/boringssl/src/crypto/fipsmodule/bn/mul.cc.inc @@ -68,12 +68,12 @@ static void bn_mul_normal(BN_ULONG *r, const BN_ULONG *a, size_t na, } // bn_sub_part_words sets |r| to |a| - |b|. It returns the borrow bit, which is -// one if the operation underflowed and zero otherwise. |cl| is the common -// length, that is, the shorter of len(a) or len(b). |dl| is the delta length, +// one if the operation underflowed and zero otherwise. `cl` is the common +// length, that is, the shorter of len(a) or len(b). `dl` is the delta length, // that is, len(a) - len(b). |r|'s length matches the larger of |a| and |b|, or // cl + abs(dl). // -// TODO(davidben): Make this take |size_t|. The |cl| + |dl| calling convention +// TODO(davidben): Make this take `size_t`. The `cl` + `dl` calling convention // is confusing. static BN_ULONG bn_sub_part_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, int cl, int dl) { @@ -88,15 +88,15 @@ static BN_ULONG bn_sub_part_words(BN_ULONG *r, const BN_ULONG *a, b += cl; if (dl < 0) { - // |a| is shorter than |b|. Complete the subtraction as if the excess words - // in |a| were zeros. + // `a` is shorter than `b`. Complete the subtraction as if the excess words + // in `a` were zeros. dl = -dl; for (int i = 0; i < dl; i++) { r[i] = CRYPTO_subc_w(0, b[i], borrow, &borrow); } } else { - // |b| is shorter than |a|. Complete the subtraction as if the excess words - // in |b| were zeros. + // `b` is shorter than `a`. Complete the subtraction as if the excess words + // in `b` were zeros. for (int i = 0; i < dl; i++) { r[i] = CRYPTO_subc_w(a[i], 0, borrow, &borrow); } @@ -107,10 +107,10 @@ static BN_ULONG bn_sub_part_words(BN_ULONG *r, const BN_ULONG *a, // bn_abs_sub_part_words computes |r| = |a| - |b|, storing the absolute value // and returning a mask of all ones if the result was negative and all zeros if -// the result was positive. |cl| and |dl| follow the |bn_sub_part_words| calling +// the result was positive. `cl` and `dl` follow the `bn_sub_part_words` calling // convention. // -// TODO(davidben): Make this take |size_t|. The |cl| + |dl| calling convention +// TODO(davidben): Make this take `size_t`. The `cl` + `dl` calling convention // is confusing. // // TODO(davidben): This function used to be used as part of a general Karatsuba @@ -144,8 +144,8 @@ int bssl::bn_abs_sub_consttime(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, return 1; } -// bn_mul_impl implements |BN_mul| and |bn_mul_consttime|. Note this function -// breaks |BIGNUM| invariants and may return a negative zero. This is handled by +// bn_mul_impl implements `BN_mul` and `bn_mul_consttime`. Note this function +// breaks `BIGNUM` invariants and may return a negative zero. This is handled by // the callers. static int bn_mul_impl(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx) { @@ -200,7 +200,7 @@ int BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx) { return 0; } - // This additionally fixes any negative zeros created by |bn_mul_impl|. + // This additionally fixes any negative zeros created by `bn_mul_impl`. bn_set_minimal_width(r); return 1; } @@ -221,7 +221,7 @@ void bssl::bn_mul_small(BN_ULONG *r, size_t num_r, const BN_ULONG *a, if (num_r != num_a + num_b) { abort(); } - // TODO(davidben): Should this call |bn_mul_comba4| too? |BN_mul| does not + // TODO(davidben): Should this call `bn_mul_comba4` too? `BN_mul` does not // hit that code. if (num_a == 8 && num_b == 8) { bn_mul_comba8(r, a, b); @@ -255,10 +255,10 @@ static void bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, size_t n) { } } - // The final result fits in |max| words, so none of the following operations + // The final result fits in `max` words, so none of the following operations // will overflow. - // Double |r|, giving the contribution of a[i] * a[j] for all i != j. + // Double `r`, giving the contribution of a[i] * a[j] for all i != j. bn_add_words(r, r, r, max); // Add in the contribution of a[i] * a[i] for all i. diff --git a/third_party/boringssl/src/crypto/fipsmodule/bn/prime.cc.inc b/third_party/boringssl/src/crypto/fipsmodule/bn/prime.cc.inc index 14eac4759..7f4722963 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/bn/prime.cc.inc +++ b/third_party/boringssl/src/crypto/fipsmodule/bn/prime.cc.inc @@ -139,7 +139,7 @@ static const uint16_t kPrimes[] = { // S+:=2^(m-(m-1)*t)*s; // end for; // A:=2^(k-2-M*t); -// B:=8*(Pi(RealField(32))^2-6)/3*2^(k-2)*S; +// B:=8*(Pi(RealField(32))²-6)/3*2^(k-2)*S; // pkt:=2.00743*Log(2)*k*2^-k*(A+B); // seclevel:=Floor(-Log(2,pkt)); // if seclevel ge securitybits then @@ -204,9 +204,9 @@ static size_t num_trial_division_primes(const BIGNUM *n) { } // BN_PRIME_CHECKS_BLINDED is the iteration count for blinding the constant-time -// primality test. See |BN_primality_test| for details. This number is selected -// so that, for a candidate N-bit RSA prime, picking |BN_PRIME_CHECKS_BLINDED| -// random N-bit numbers will have at least |BN_prime_checks_for_size(N)| values +// primality test. See `BN_primality_test` for details. This number is selected +// so that, for a candidate N-bit RSA prime, picking `BN_PRIME_CHECKS_BLINDED` +// random N-bit numbers will have at least `BN_prime_checks_for_size(N)` values // in range with high probability. // // The following Python script computes the blinding factor needed for the @@ -214,7 +214,7 @@ static size_t num_trial_division_primes(const BIGNUM *n) { /* import math -# We choose candidate RSA primes between sqrt(2)/2 * 2^N and 2^N and select +# We choose candidate RSA primes between sqrt(2)/2 * 2ᴺ and 2ᴺ and select # witnesses by generating random N-bit numbers. Thus the probability of # selecting one in range is at least sqrt(2)/2. p = math.sqrt(2) / 2 @@ -231,8 +231,8 @@ def choose(a, b): return r def failure_rate(min_uniform, iterations): - """ Returns the probability that, for |iterations| candidate witnesses, fewer - than |min_uniform| of them will be uniform. """ + """ Returns the probability that, for `iterations` candidate witnesses, fewer + than `min_uniform` of them will be uniform. """ prob = 0.0 for i in xrange(min_uniform): prob += (choose(iterations, i) * @@ -353,8 +353,8 @@ loop: return 0; } - // Interleave |ret| and |t|'s primality tests to avoid paying the full - // iteration count on |ret| only to quickly discover |t| is composite. + // Interleave `ret` and `t`'s primality tests to avoid paying the full + // iteration count on `ret` only to quickly discover `t` is composite. // // TODO(davidben): This doesn't quite work because an iteration count of 1 // still runs the blinding mechanism. @@ -387,7 +387,7 @@ loop: static int bn_trial_division(uint16_t *out, const BIGNUM *bn) { const size_t num_primes = num_trial_division_primes(bn); for (size_t i = 1; i < num_primes; i++) { - // During RSA key generation, |bn| may be secret, but only if |bn| was + // During RSA key generation, `bn` may be secret, but only if `bn` was // prime, so it is safe to leak failed trial divisions. if (constant_time_declassify_int(bn_mod_u16_consttime(bn, kPrimes[i]) == 0)) { @@ -407,7 +407,7 @@ int bssl::bn_miller_rabin_init(BN_MILLER_RABIN *miller_rabin, const BN_MONT_CTX *mont, BN_CTX *ctx) { // This function corresponds to steps 1 through 3 of FIPS 186-5, B.3.1. const BIGNUM *w = &mont->N; - // Note we do not call |BN_CTX_start| in this function. We intentionally + // Note we do not call `BN_CTX_start` in this function. We intentionally // allocate values in the containing scope so they outlive this function. miller_rabin->w1 = BN_CTX_get(ctx); miller_rabin->m = BN_CTX_get(ctx); @@ -434,7 +434,7 @@ int bssl::bn_miller_rabin_init(BN_MILLER_RABIN *miller_rabin, // Precompute some values in Montgomery form. if (!bn_one_to_montgomery(miller_rabin->one_mont, mont, ctx) || // w - 1 is -1 mod w, so we can compute it in the Montgomery domain, -R, - // with a subtraction. (|one_mont| cannot be zero.) + // with a subtraction. (`one_mont` cannot be zero.) !bn_usub_consttime(miller_rabin->w1_mont, w, miller_rabin->one_mont)) { return 0; } @@ -459,8 +459,8 @@ int bssl::bn_miller_rabin_iteration(const BN_MILLER_RABIN *miller_rabin, return 0; } - // is_possibly_prime is all ones if we have determined |b| is not a composite - // witness for |w|. This is equivalent to going to step 4.7 in the original + // is_possibly_prime is all ones if we have determined `b` is not a composite + // witness for `w`. This is equivalent to going to step 4.7 in the original // algorithm. To avoid timing leaks, we run the algorithm to the end for prime // inputs. is_possibly_prime = 0; @@ -473,8 +473,8 @@ int bssl::bn_miller_rabin_iteration(const BN_MILLER_RABIN *miller_rabin, // Step 4.5. // - // To avoid leaking |a|, we run the loop to |w_bits| and mask off all - // iterations once |j| = |a|. + // To avoid leaking `a`, we run the loop to `w_bits` and mask off all + // iterations once `j` = `a`. for (int j = 1; j < miller_rabin->w_bits; j++) { if (constant_time_declassify_w(constant_time_eq_int(j, miller_rabin->a) & ~is_possibly_prime)) { @@ -492,7 +492,7 @@ int bssl::bn_miller_rabin_iteration(const BN_MILLER_RABIN *miller_rabin, // witness. crypto_word_t z_is_w1_mont = BN_equal_consttime(z, miller_rabin->w1_mont); z_is_w1_mont = 0 - z_is_w1_mont; // Make it all zeros or all ones. - is_possibly_prime |= z_is_w1_mont; // Go to step 4.7 if |z_is_w1_mont|. + is_possibly_prime |= z_is_w1_mont; // Go to step 4.7 if `z_is_w1_mont`. // Step 4.5.3. If z = 1 and the loop is not done, the previous value of z // was not -1. There are no non-trivial square roots of 1 modulo a prime, so @@ -514,20 +514,20 @@ int BN_primality_test(int *out_is_probably_prime, const BIGNUM *w, int checks, // generation. We generate RSA keys by selecting two large, secret primes with // rejection sampling. // - // We thus treat |w| as secret if turns out to be a large prime. However, if - // |w| is composite, we treat this and |w| itself as public. (Conversely, if - // |w| is prime, that it is prime is public. Only the value is secret.) This + // We thus treat `w` as secret if turns out to be a large prime. However, if + // `w` is composite, we treat this and `w` itself as public. (Conversely, if + // `w` is prime, that it is prime is public. Only the value is secret.) This // is fine for RSA key generation, but note it is important that we use // rejection sampling, with each candidate prime chosen independently. This // would not work for, e.g., an algorithm which looked for primes in // consecutive integers. These assumptions allow us to discard composites - // quickly. We additionally treat |w| as public when it is a small prime to + // quickly. We additionally treat `w` as public when it is a small prime to // simplify trial decryption and some edge cases. // // One RSA key generation will call this function on exactly two primes and // many more composites. The overall cost is a combination of several factors: // - // 1. Checking if |w| is divisible by a small prime is much faster than + // 1. Checking if `w` is divisible by a small prime is much faster than // learning it is composite by Miller-Rabin (see below for details on that // cost). Trial division by p saves 1/p of Miller-Rabin calls, so this is // worthwhile until p exceeds the ratio of the two costs. @@ -538,11 +538,11 @@ int BN_primality_test(int *out_is_probably_prime, const BIGNUM *w, int checks, // trial decryption, in practice, cost one Miller-Rabin iteration. Only the // two actual primes cost the full iteration count. // - // 3. A Miller-Rabin iteration is a modular exponentiation plus |a| additional - // modular squares, where |a| is the number of factors of two in |w-1|. |a| + // 3. A Miller-Rabin iteration is a modular exponentiation plus `a` additional + // modular squares, where `a` is the number of factors of two in `w-1`. `a` // is likely small (the distribution falls exponentially), but it is also - // potentially secret, so we loop up to its log(w) upper bound when |w| is - // prime. When |w| is composite, we break early, so only two calls pay this + // potentially secret, so we loop up to its log(w) upper bound when `w` is + // prime. When `w` is composite, we break early, so only two calls pay this // cost. (Note that all calls pay the modular exponentiation which is, // itself, log(w) modular multiplications and squares.) // @@ -610,14 +610,14 @@ int BN_primality_test(int *out_is_probably_prime, const BIGNUM *w, int checks, // The following loop performs in inner iteration of the Miller-Rabin // Primality test (Step 4). // - // The algorithm as specified in FIPS 186-5 leaks information on |w|, the RSA + // The algorithm as specified in FIPS 186-5 leaks information on `w`, the RSA // private key. Instead, we run through each iteration unconditionally, // performing modular multiplications, masking off any effects to behave // equivalently to the specified algorithm. // - // We also blind the number of values of |b| we try. Steps 4.1–4.2 say to - // discard out-of-range values. To avoid leaking information on |w|, we use - // |bn_rand_secret_range| which, rather than discarding bad values, adjusts + // We also blind the number of values of `b` we try. Steps 4.1–4.2 say to + // discard out-of-range values. To avoid leaking information on `w`, we use + // `bn_rand_secret_range` which, rather than discarding bad values, adjusts // them to be in range. Though not uniformly selected, these adjusted values // are still usable as Miller-Rabin checks. // @@ -628,14 +628,14 @@ int BN_primality_test(int *out_is_probably_prime, const BIGNUM *w, int checks, // function is more complex and has more timing risk than necessary. // // We count both total iterations and uniform ones and iterate until we've - // reached at least |BN_PRIME_CHECKS_BLINDED| and |iterations|, respectively. + // reached at least `BN_PRIME_CHECKS_BLINDED` and `iterations`, respectively. // If the latter is large enough, it will be the limiting factor with high // probability and we won't leak information. // // Note this blinding does not impact most calls when picking primes because // composites are rejected early. Only the two secret primes see extra work. - // Using |constant_time_lt_w| seems to prevent the compiler from optimizing + // Using `constant_time_lt_w` seems to prevent the compiler from optimizing // this into two jumps. for (int i = 1; constant_time_declassify_w( (i <= BN_PRIME_CHECKS_BLINDED) | @@ -706,7 +706,7 @@ int BN_enhanced_miller_rabin_primality_test( return 0; } - // Write w1 as m*2^a (Steps 1 and 2). + // Write w1 as m*2ᵃ (Steps 1 and 2). int a = 0; while (!BN_is_bit_set(w1, a)) { a++; diff --git a/third_party/boringssl/src/crypto/fipsmodule/bn/random.cc.inc b/third_party/boringssl/src/crypto/fipsmodule/bn/random.cc.inc index 591cb8fab..1dbe27bdb 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/bn/random.cc.inc +++ b/third_party/boringssl/src/crypto/fipsmodule/bn/random.cc.inc @@ -94,7 +94,7 @@ int BN_pseudo_rand(BIGNUM *rnd, int bits, int top, int bottom) { } // bn_less_than_word_mask returns a mask of all ones if the number represented -// by |len| words at |a| is less than |b| and zero otherwise. It performs this +// by `len` words at |a| is less than |b| and zero otherwise. It performs this // computation in time independent of the value of |a|. |b| is assumed public. static crypto_word_t bn_less_than_word_mask(const BN_ULONG *a, size_t len, BN_ULONG b) { @@ -112,7 +112,7 @@ static crypto_word_t bn_less_than_word_mask(const BN_ULONG *a, size_t len, for (size_t i = 1; i < len; i++) { mask |= a[i]; } - // |mask| is now zero iff a[1..len-1] are all zero. + // `mask` is now zero iff a[1..len-1] are all zero. mask = constant_time_is_zero_w(mask); mask &= constant_time_lt_w(a[0], b); return mask; @@ -127,7 +127,7 @@ int bssl::bn_in_range_words(const BN_ULONG *a, BN_ULONG min_inclusive, static int bn_range_to_mask(size_t *out_words, BN_ULONG *out_mask, size_t min_inclusive, const BN_ULONG *max_exclusive, size_t len) { - // The magnitude of |max_exclusive| is assumed public. + // The magnitude of `max_exclusive` is assumed public. size_t words = len; while (words > 0 && max_exclusive[words - 1] == 0) { words--; @@ -137,7 +137,7 @@ static int bn_range_to_mask(size_t *out_words, BN_ULONG *out_mask, return 0; } BN_ULONG mask = max_exclusive[words - 1]; - // This sets all bits in |mask| below the most significant bit. + // This sets all bits in `mask` below the most significant bit. mask |= mask >> 1; mask |= mask >> 2; mask |= mask >> 4; @@ -157,10 +157,10 @@ int bssl::bn_rand_range_words(BN_ULONG *out, BN_ULONG min_inclusive, const uint8_t additional_data[32]) { // This function implements the equivalent of steps 1 through 4 of FIPS 186-5 // appendices A.2.2 and A.3.2, repeating the process on failure. When called - // in those contexts, |max_exclusive| is n and |min_inclusive| is one. + // in those contexts, `max_exclusive` is n and `min_inclusive` is one. - // Compute the bit length of |max_exclusive| (step 1), in terms of a number of - // |words| worth of entropy to fill and a mask of bits to clear in the top + // Compute the bit length of `max_exclusive` (step 1), in terms of a number of + // `words` worth of entropy to fill and a mask of bits to clear in the top // word. size_t words; BN_ULONG mask; @@ -178,8 +178,8 @@ int bssl::bn_rand_range_words(BN_ULONG *out, BN_ULONG min_inclusive, return 0; } - // Use |words| and |mask| together to obtain a string of N bits, where N is - // the bit length of |max_exclusive|. + // Use `words` and `mask` together to obtain a string of N bits, where N is + // the bit length of `max_exclusive`. FIPS_service_indicator_lock_state(); BCM_rand_bytes_with_additional_data( (uint8_t *)out, words * sizeof(BN_ULONG), additional_data); @@ -190,7 +190,7 @@ int bssl::bn_rand_range_words(BN_ULONG *out, BN_ULONG min_inclusive, // comparison may be treated as public. It only reveals how many attempts // were needed before we found a value in range. This is independent of the // final secret output, and has a distribution that depends only on - // |min_inclusive| and |max_exclusive|, both of which are public. + // `min_inclusive` and `max_exclusive`, both of which are public. } while (!constant_time_declassify_int( bn_in_range_words(out, min_inclusive, max_exclusive, words))); return 1; diff --git a/third_party/boringssl/src/crypto/fipsmodule/bn/rsaz_exp.cc.inc b/third_party/boringssl/src/crypto/fipsmodule/bn/rsaz_exp.cc.inc index 105daae31..6a04ac9c9 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/bn/rsaz_exp.cc.inc +++ b/third_party/boringssl/src/crypto/fipsmodule/bn/rsaz_exp.cc.inc @@ -52,7 +52,7 @@ void bssl::RSAZ_1024_mod_exp_avx2(BN_ULONG result_norm[16], assert((uintptr_t)storage % 64 == 0); BN_ULONG *a_inv, *m, *result, *table_s = storage + 40 * 3, *R2 = table_s; - // Note |R2| aliases |table_s|. + // Note `R2` aliases `table_s`. if (((((uintptr_t)storage & 4095) + 320) >> 12) != 0) { result = storage; a_inv = storage + 40; @@ -67,33 +67,33 @@ void bssl::RSAZ_1024_mod_exp_avx2(BN_ULONG result_norm[16], rsaz_1024_norm2red_avx2(a_inv, base_norm); rsaz_1024_norm2red_avx2(R2, RR); - // Convert |R2| from the usual radix, giving R = 2^1024, to RSAZ's radix, + // Convert `R2` from the usual radix, giving R = 2^1024, to RSAZ's radix, // giving R = 2^(36*29) = 2^1044. rsaz_1024_mul_avx2(R2, R2, R2, m, k0); // R2 = 2^2048 * 2^2048 / 2^1044 = 2^3052 rsaz_1024_mul_avx2(R2, R2, rsaz_two80, m, k0); - // R2 = 2^3052 * 2^80 / 2^1044 = 2^2088 = (2^1044)^2 + // R2 = 2^3052 * 2^80 / 2^1044 = 2^2088 = (2^1044)² // table[0] = 1 - // table[1] = a_inv^1 + // table[1] = a_inv¹ rsaz_1024_mul_avx2(result, R2, rsaz_one, m, k0); rsaz_1024_mul_avx2(a_inv, a_inv, R2, m, k0); rsaz_1024_scatter5_avx2(table_s, result, 0); rsaz_1024_scatter5_avx2(table_s, a_inv, 1); - // table[2] = a_inv^2 + // table[2] = a_inv² rsaz_1024_sqr_avx2(result, a_inv, m, k0, 1); rsaz_1024_scatter5_avx2(table_s, result, 2); - // table[4] = a_inv^4 + // table[4] = a_inv⁴ rsaz_1024_sqr_avx2(result, result, m, k0, 1); rsaz_1024_scatter5_avx2(table_s, result, 4); - // table[8] = a_inv^8 + // table[8] = a_inv⁸ rsaz_1024_sqr_avx2(result, result, m, k0, 1); rsaz_1024_scatter5_avx2(table_s, result, 8); // table[16] = a_inv^16 rsaz_1024_sqr_avx2(result, result, m, k0, 1); rsaz_1024_scatter5_avx2(table_s, result, 16); for (int i = 3; i < 32; i += 2) { - // table[i] = table[i-1] * a_inv = a_inv^i + // table[i] = table[i-1] * a_inv = a_invⁱ rsaz_1024_gather5_avx2(result, table_s, i - 1); rsaz_1024_mul_avx2(result, result, a_inv, m, k0); rsaz_1024_scatter5_avx2(table_s, result, i); @@ -119,7 +119,7 @@ void bssl::RSAZ_1024_mod_exp_avx2(BN_ULONG result_norm[16], wvalue = (wvalue >> (index % 8)) & 31; index -= 5; - rsaz_1024_gather5_avx2(a_inv, table_s, wvalue); // Borrow |a_inv|. + rsaz_1024_gather5_avx2(a_inv, table_s, wvalue); // Borrow `a_inv`. rsaz_1024_mul_avx2(result, result, a_inv, m, k0); } @@ -128,7 +128,7 @@ void bssl::RSAZ_1024_mod_exp_avx2(BN_ULONG result_norm[16], wvalue = p_str[0] & 15; - rsaz_1024_gather5_avx2(a_inv, table_s, wvalue); // Borrow |a_inv|. + rsaz_1024_gather5_avx2(a_inv, table_s, wvalue); // Borrow `a_inv`. rsaz_1024_mul_avx2(result, result, a_inv, m, k0); // Convert from Montgomery. diff --git a/third_party/boringssl/src/crypto/fipsmodule/bn/rsaz_exp.h b/third_party/boringssl/src/crypto/fipsmodule/bn/rsaz_exp.h index 9bd3cb9a6..26df52a31 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/bn/rsaz_exp.h +++ b/third_party/boringssl/src/crypto/fipsmodule/bn/rsaz_exp.h @@ -31,11 +31,11 @@ BSSL_NAMESPACE_BEGIN #if !defined(OPENSSL_NO_ASM) && defined(OPENSSL_X86_64) #define RSAZ_ENABLED -// RSAZ_1024_mod_exp_avx2 sets |result| to |base_norm| raised to |exponent| -// modulo |m_norm|. |base_norm| must be fully-reduced and |exponent| must have -// the high bit set (it is 1024 bits wide). |RR| and |k0| must be |RR| and |n0|, -// respectively, extracted from |m_norm|'s |BN_MONT_CTX|. |storage_words| is a -// temporary buffer that must be aligned to |MOD_EXP_CTIME_ALIGN| bytes. +// RSAZ_1024_mod_exp_avx2 sets `result` to `base_norm` raised to `exponent` +// modulo `m_norm`. `base_norm` must be fully-reduced and `exponent` must have +// the high bit set (it is 1024 bits wide). `RR` and `k0` must be `RR` and `n0`, +// respectively, extracted from `m_norm`'s `BN_MONT_CTX`. `storage_words` is a +// temporary buffer that must be aligned to `MOD_EXP_CTIME_ALIGN` bytes. void RSAZ_1024_mod_exp_avx2(BN_ULONG result[16], const BN_ULONG base_norm[16], const BN_ULONG exponent[16], const BN_ULONG m_norm[16], const BN_ULONG RR[16], @@ -62,43 +62,43 @@ inline int rsaz_avx2_preferred() { // // See crypto/bn/asm/rsaz-avx2.pl for further details. -// rsaz_1024_norm2red_avx2 converts |norm| from |BIGNUM| to RSAZ representation -// and writes the result to |red|. +// rsaz_1024_norm2red_avx2 converts `norm` from `BIGNUM` to RSAZ representation +// and writes the result to `red`. extern "C" void rsaz_1024_norm2red_avx2(BN_ULONG red[40], const BN_ULONG norm[16]); -// rsaz_1024_mul_avx2 computes |a| * |b| mod |n| and writes the result to |ret|. -// Inputs and outputs are in Montgomery form, using RSAZ's representation. |k| -// is -|n|^-1 mod 2^64 or |n0| from |BN_MONT_CTX|. +// rsaz_1024_mul_avx2 computes `a` * `b` mod `n` and writes the result to `ret`. +// Inputs and outputs are in Montgomery form, using RSAZ's representation. `k` +// is -`n`^-1 mod 2^64 or `n0` from `BN_MONT_CTX`. extern "C" void rsaz_1024_mul_avx2(BN_ULONG ret[40], const BN_ULONG a[40], const BN_ULONG b[40], const BN_ULONG n[40], BN_ULONG k); -// rsaz_1024_mul_avx2 computes |a|^(2*|count|) mod |n| and writes the result to -// |ret|. Inputs and outputs are in Montgomery form, using RSAZ's -// representation. |k| is -|n|^-1 mod 2^64 or |n0| from |BN_MONT_CTX|. +// rsaz_1024_mul_avx2 computes `a`^(2*`count`) mod `n` and writes the result to +// `ret`. Inputs and outputs are in Montgomery form, using RSAZ's +// representation. `k` is -`n`^-1 mod 2^64 or `n0` from `BN_MONT_CTX`. extern "C" void rsaz_1024_sqr_avx2(BN_ULONG ret[40], const BN_ULONG a[40], const BN_ULONG n[40], BN_ULONG k, int count); -// rsaz_1024_scatter5_avx2 stores |val| at index |i| of |tbl|. |i| must be +// rsaz_1024_scatter5_avx2 stores `val` at index `i` of `tbl`. `i` must be // positive and at most 31. It is treated as public. Note the table only uses 18 -// |BN_ULONG|s per entry instead of 40. It packs two 29-bit limbs into each -// |BN_ULONG| and only stores 36 limbs rather than the padded 40. +// `BN_ULONG`s per entry instead of 40. It packs two 29-bit limbs into each +// `BN_ULONG` and only stores 36 limbs rather than the padded 40. extern "C" void rsaz_1024_scatter5_avx2(BN_ULONG tbl[32 * 18], const BN_ULONG val[40], int i); -// rsaz_1024_gather5_avx2 loads index |i| of |tbl| and writes it to |val|. |i| -// must be positive and at most 31. It is treated as secret. |tbl| must be +// rsaz_1024_gather5_avx2 loads index `i` of `tbl` and writes it to `val`. `i` +// must be positive and at most 31. It is treated as secret. `tbl` must be // aligned to 32 bytes. extern "C" void rsaz_1024_gather5_avx2(BN_ULONG val[40], const BN_ULONG tbl[32 * 18], int i); -// rsaz_1024_red2norm_avx2 converts |red| from RSAZ to |BIGNUM| representation -// and writes the result to |norm|. The result will be <= the modulus. +// rsaz_1024_red2norm_avx2 converts `red` from RSAZ to `BIGNUM` representation +// and writes the result to `norm`. The result will be <= the modulus. // -// WARNING: The result of this operation may not be fully reduced. |norm| may be +// WARNING: The result of this operation may not be fully reduced. `norm` may be // the modulus instead of zero. This function should be followed by a call to -// |bn_reduce_once|. +// `bn_reduce_once`. extern "C" void rsaz_1024_red2norm_avx2(BN_ULONG norm[16], const BN_ULONG red[40]); diff --git a/third_party/boringssl/src/crypto/fipsmodule/bn/shift.cc.inc b/third_party/boringssl/src/crypto/fipsmodule/bn/shift.cc.inc index 4799bac9f..bf96726d4 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/bn/shift.cc.inc +++ b/third_party/boringssl/src/crypto/fipsmodule/bn/shift.cc.inc @@ -263,7 +263,7 @@ static int bn_count_low_zero_bits_word(BN_ULONG l) { int bits = 0; #if BN_BITS2 > 32 - // Check if the lower half of |x| are all zero. + // Check if the lower half of `x` are all zero. mask = constant_time_is_zero_w(l << (BN_BITS2 - 32)); // If the lower half is all zeros, it is included in the bit count and we // count the upper half. Otherwise, we count the lower half. @@ -311,7 +311,7 @@ int BN_count_low_zero_bits(const BIGNUM *bn) { ret |= first_nonzero & (i * BN_BITS2 + bits); } - // If got to the end of |bn| and saw no non-zero words, |bn| is zero. |ret| + // If got to the end of `bn` and saw no non-zero words, `bn` is zero. `ret` // will then remain zero. return ret; } diff --git a/third_party/boringssl/src/crypto/fipsmodule/bn/sqrt.cc.inc b/third_party/boringssl/src/crypto/fipsmodule/bn/sqrt.cc.inc index 308044033..550e14f5c 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/bn/sqrt.cc.inc +++ b/third_party/boringssl/src/crypto/fipsmodule/bn/sqrt.cc.inc @@ -86,7 +86,7 @@ BIGNUM *BN_mod_sqrt(BIGNUM *in, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx) { goto end; } - // now write |p| - 1 as 2^e*q where q is odd + // now write |p| - 1 as 2ᵉ⋅q where q is odd e = 1; while (!BN_is_bit_set(p, e)) { e++; @@ -115,23 +115,23 @@ BIGNUM *BN_mod_sqrt(BIGNUM *in, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx) { // |p| == 5 (mod 8) // // In this case 2 is always a non-square since - // Legendre(2,p) = (-1)^((p^2-1)/8) for any odd prime. - // So if a really is a square, then 2*a is a non-square. + // Legendre(2,p) = (-1)^((p²-1)/8) for any odd prime. + // So if a really is a square, then 2⋅a is a non-square. // Thus for - // b := (2*a)^((|p|-5)/8), - // i := (2*a)*b^2 + // b := (2⋅a)^((|p|-5)/8), + // i := (2⋅a)⋅b² // we have - // i^2 = (2*a)^((1 + (|p|-5)/4)*2) - // = (2*a)^((p-1)/2) - // = -1; + // i² = (2⋅a)^((1 + (|p|-5)/4)⋅2) + // = (2⋅a)^((p-1)/2) + // = -1; // so if we set - // x := a*b*(i-1), + // x := a⋅b⋅(i-1), // then - // x^2 = a^2 * b^2 * (i^2 - 2*i + 1) - // = a^2 * b^2 * (-2*i) - // = a*(-i)*(2*a*b^2) - // = a*(-i)*i - // = a. + // x² = a² ⋅ b² ⋅ (i² - 2⋅i + 1) + // = a² ⋅ b² ⋅ (-2⋅i) + // = a⋅(-i)⋅(2⋅a⋅b²) + // = a⋅(-i)⋅i + // = a. // // (This is due to A.O.L. Atkin, // aead = nullptr; } -// check_alias returns 1 if |out| is compatible with |in| and 0 otherwise. If -// |in| and |out| alias, we require that |in| == |out|. +// check_alias returns 1 if `out` is compatible with `in` and 0 otherwise. If +// `in` and `out` alias, we require that `in` == `out`. static int check_alias(const uint8_t *in, size_t in_len, const uint8_t *out, size_t out_len) { if (!buffers_alias(in, in_len, out, out_len)) { @@ -178,7 +178,7 @@ int EVP_AEAD_CTX_seal_scatter(const EVP_AEAD_CTX *ctx, uint8_t *out, } }); - // |out_tag| contains both the encryption of |extra_in| and the tag. + // `out_tag` contains both the encryption of `extra_in` and the tag. Span out_tag_span(out_tag, max_out_tag_len); if (out_tag_span.size() < extra_in_len) { OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL); @@ -338,7 +338,7 @@ int EVP_AEAD_CTX_sealv(const EVP_AEAD_CTX *ctx, const CRYPTO_IOVEC *iovec, } // Enforce aliasing rules: no output may alias any input, with the one - // exception that an iovec member's |in| and |out| pointers may be identical + // exception that an iovec member's `in` and `out` pointers may be identical // for in-place operation. if (!check_iovec_alias(iovecs, aadvecs, out_tag, max_out_tag_len, nonce, nonce_len, nullptr, 0)) { @@ -481,7 +481,7 @@ int EVP_AEAD_CTX_openv(const EVP_AEAD_CTX *ctx, const CRYPTO_IOVEC *iovec, } // Enforce aliasing rules: no output may alias any input, with the one - // exception that an iovec member's |in| and |out| pointers may be identical + // exception that an iovec member's `in` and `out` pointers may be identical // for in-place operation. if (!check_iovec_alias(iovecs, aadvecs, nullptr, 0, nonce, nonce_len, nullptr, 0)) { @@ -492,14 +492,16 @@ int EVP_AEAD_CTX_openv(const EVP_AEAD_CTX *ctx, const CRYPTO_IOVEC *iovec, if (!ctx->aead->openv) { if (ctx->tag_len && ctx->aead->openv_detached) { // Try with a detached tag. - InplaceVector detached_iovecs; - detached_iovecs.CopyFrom(iovecs); + bssl::iovec::MaybeInplaceArray detached_iovecs; + if (!detached_iovecs.CopyFrom(iovecs)) { + return 0; + } uint8_t tagbuf[EVP_AEAD_MAX_OVERHEAD]; std::optional> tag = bssl::iovec::GetAndRemoveSuffix( Span(tagbuf).first(ctx->tag_len), Span(detached_iovecs)); - if (!tag.has_value()) { // I.e. no |ctx->tag_len| bytes available. + if (!tag.has_value()) { // I.e. no `ctx->tag_len` bytes available. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT); return 0; } @@ -554,7 +556,7 @@ int EVP_AEAD_CTX_openv_detached(const EVP_AEAD_CTX *ctx, } // Enforce aliasing rules: no output may alias any input, with the one - // exception that an iovec member's |in| and |out| pointers may be identical + // exception that an iovec member's `in` and `out` pointers may be identical // for in-place operation. if (!check_iovec_alias(iovecs, aadvecs, nullptr, 0, nonce, nonce_len, in_tag, in_tag_len)) { diff --git a/third_party/boringssl/src/crypto/fipsmodule/cipher/cipher.cc.inc b/third_party/boringssl/src/crypto/fipsmodule/cipher/cipher.cc.inc index eed5d0ad6..56d950156 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/cipher/cipher.cc.inc +++ b/third_party/boringssl/src/crypto/fipsmodule/cipher/cipher.cc.inc @@ -66,11 +66,6 @@ int EVP_CIPHER_CTX_copy(EVP_CIPHER_CTX *out, const EVP_CIPHER_CTX *in) { return 0; } - if (in->poisoned) { - OPENSSL_PUT_ERROR(CIPHER, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); - return 0; - } - EVP_CIPHER_CTX_cleanup(out); OPENSSL_memcpy(out, in, sizeof(EVP_CIPHER_CTX)); @@ -190,9 +185,6 @@ int EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, ctx->buf_len = 0; ctx->final_used = 0; - // Clear the poisoned flag to permit reuse of a CTX that previously had a - // failed operation. - ctx->poisoned = 0; return 1; } @@ -206,15 +198,56 @@ int EVP_DecryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, return EVP_CipherInit_ex(ctx, cipher, impl, key, iv, 0); } -// block_remainder returns the number of bytes to remove from |len| to get a -// multiple of |ctx|'s block size. +// block_remainder returns the number of bytes to remove from `len` to get a +// multiple of `ctx`'s block size. static size_t block_remainder(const EVP_CIPHER_CTX *ctx, size_t len) { - // |block_size| must be a power of two. + // `block_size` must be a power of two. assert(ctx->cipher->block_size != 0); assert((ctx->cipher->block_size & (ctx->cipher->block_size - 1)) == 0); return len & (ctx->cipher->block_size - 1); } +static size_t evp_cipher_ctx_max_encrypt_update(const EVP_CIPHER_CTX *ctx, + size_t in_len) { + if (in_len == 0) { + return 0; + } + + size_t block_size = ctx->cipher->block_size; + // `block_size` must be a power of 2. + assert(block_size != 0 && (block_size & (block_size - 1)) == 0); + size_t buf_len = ctx->buf_len; + + // Any buffered input is combined with `in_len`, then we round down to a + // multiple of the block size. + return (in_len + buf_len) & ~(block_size - 1); +} + +size_t EVP_CIPHER_CTX_max_next_update(const EVP_CIPHER_CTX *ctx, + size_t in_len) { + if (in_len == 0) { + return 0; + } + + size_t ret = evp_cipher_ctx_max_encrypt_update(ctx, in_len); + size_t block_size = ctx->cipher->block_size; + size_t buf_len = ctx->buf_len; + if (!ctx->encrypt && block_size > 1 && !(ctx->flags & EVP_CIPH_NO_PADDING)) { + if (ctx->final_used) { + // There was a buffered decrypted block. Now that it is known to not have + // padding, DecryptUpdate will output it. + ret += block_size; + } + if (block_remainder(ctx, in_len + buf_len) == 0) { + // This call ends on a block boundary. The last block will be buffered in + // `ctx->final` until it is known to have padding. + assert(ret >= block_size); + ret -= block_size; + } + } + return ret; +} + int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len, const uint8_t *in, int in_len) { *out_len = 0; @@ -242,30 +275,22 @@ int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len, return 1; } -template -static int WrapWithPoison(EVP_CIPHER_CTX *ctx, F f) { - if (ctx->poisoned) { - // |ctx| has been left in an indeterminate state by a previous failed - // operation. Do not allow proceeding. - OPENSSL_PUT_ERROR(CIPHER, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); - return 0; - } - if (!f()) { - // Functions using |WrapWithPoison| may leave |ctx| in an indeterminate - // state. Mark the object as poisoned. - ctx->poisoned = 1; +int EVP_EncryptUpdate_ex(EVP_CIPHER_CTX *ctx, uint8_t *out, size_t *out_len, + size_t max_out_len, const uint8_t *in, size_t in_len) { + *out_len = 0; + + // `evp_cipher_ctx_max_encrypt_update` is currently exact. Check bounds before + // performing any operations so that, if the output is too small, retrying the + // operation with a larger buffer is well-defined. This only matters when it + // takes two steps to process `in`. + size_t expected = evp_cipher_ctx_max_encrypt_update(ctx, in_len); + if (max_out_len < expected) { + OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL); return 0; } - return 1; -} - -static int EVP_EncryptUpdate_ex_internal(EVP_CIPHER_CTX *ctx, uint8_t *out, - size_t *out_len, size_t max_out_len, - const uint8_t *in, size_t in_len) { - *out_len = 0; - // Ciphers that use blocks may write up to |block_size| extra bytes. Ensure - // the output does not overflow |*out_len|. + // Ciphers that use blocks may write up to `block_size` extra bytes. Ensure + // the output does not overflow `*out_len`. Span in_span(in, in_len); size_t block_size = ctx->cipher->block_size; @@ -316,18 +341,10 @@ static int EVP_EncryptUpdate_ex_internal(EVP_CIPHER_CTX *ctx, uint8_t *out, ctx->buf_len = in_span.size(); *out_len = max_out_len - out_span.size(); + assert(*out_len == expected); // See `EVP_CIPHER_CTX_max_next_update` check. return 1; } -int EVP_EncryptUpdate_ex(EVP_CIPHER_CTX *ctx, uint8_t *out, size_t *out_len, - size_t max_out_len, const uint8_t *in, size_t in_len) { - *out_len = 0; - return WrapWithPoison(ctx, [&] { - return EVP_EncryptUpdate_ex_internal(ctx, out, out_len, max_out_len, in, - in_len); - }); -} - int EVP_EncryptFinal_ex(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len) { size_t out_len_sz; int ret = @@ -337,8 +354,8 @@ int EVP_EncryptFinal_ex(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len) { return ret; } -static int EVP_EncryptFinal_ex2_internal(EVP_CIPHER_CTX *ctx, uint8_t *out, - size_t *out_len, size_t max_out_len) { +int EVP_EncryptFinal_ex2(EVP_CIPHER_CTX *ctx, uint8_t *out, size_t *out_len, + size_t max_out_len) { *out_len = 0; size_t block_size = ctx->cipher->block_size; @@ -347,6 +364,7 @@ static int EVP_EncryptFinal_ex2_internal(EVP_CIPHER_CTX *ctx, uint8_t *out, if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) { return ctx->cipher->cipher_final(ctx); } + EVP_Cipher_verify_service_indicator(ctx); return 1; } @@ -356,40 +374,30 @@ static int EVP_EncryptFinal_ex2_internal(EVP_CIPHER_CTX *ctx, uint8_t *out, OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH); return 0; } + EVP_Cipher_verify_service_indicator(ctx); return 1; } - size_t padding = block_size - buf_len; - for (size_t i = buf_len; i < block_size; i++) { - ctx->buf[i] = padding; - } - Span out_span(out, max_out_len); if (out_span.size() < block_size) { OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL); return 0; } + + size_t padding = block_size - buf_len; + for (size_t i = buf_len; i < block_size; i++) { + ctx->buf[i] = padding; + } if (!ctx->cipher->cipher_update(ctx, out_span.data(), ctx->buf, block_size)) { return 0; } out_span = out_span.subspan(block_size); *out_len = max_out_len - out_span.size(); + EVP_Cipher_verify_service_indicator(ctx); return 1; } -int EVP_EncryptFinal_ex2(EVP_CIPHER_CTX *ctx, uint8_t *out, size_t *out_len, - size_t max_out_len) { - *out_len = 0; - return WrapWithPoison(ctx, [&] { - if (!EVP_EncryptFinal_ex2_internal(ctx, out, out_len, max_out_len)) { - return 0; - } - EVP_Cipher_verify_service_indicator(ctx); - return 1; - }); -} - int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len, const uint8_t *in, int in_len) { *out_len = 0; @@ -417,16 +425,14 @@ int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len, return 1; } -static int EVP_DecryptUpdate_ex_internal(EVP_CIPHER_CTX *ctx, uint8_t *out, - size_t *out_len, size_t max_out_len, - const uint8_t *in, size_t in_len) { +int EVP_DecryptUpdate_ex(EVP_CIPHER_CTX *ctx, uint8_t *out, size_t *out_len, + size_t max_out_len, const uint8_t *in, size_t in_len) { *out_len = 0; - // Ciphers that use blocks may write up to |block_size| extra bytes. Ensure - // the output does not overflow |*out_len|. + // Ciphers that use blocks may write up to `block_size` extra bytes. Ensure + // the output does not overflow `*out_len`. Span in_span(in, in_len); size_t block_size = ctx->cipher->block_size; - if (in_span.empty()) { return 1; } @@ -434,9 +440,18 @@ static int EVP_DecryptUpdate_ex_internal(EVP_CIPHER_CTX *ctx, uint8_t *out, Span out_span(out, max_out_len); if (ctx->flags & EVP_CIPH_NO_PADDING) { // Use the shared block handling logic from encryption. - return EVP_EncryptUpdate_ex_internal(ctx, out_span.data(), out_len, - out_span.size(), in_span.data(), - in_span.size()); + return EVP_EncryptUpdate_ex(ctx, out_span.data(), out_len, out_span.size(), + in_span.data(), in_span.size()); + } + + // `EVP_CIPHER_CTX_max_next_update` is currently exact. Check bounds before + // performing any operations so that, if the output is too small, retrying the + // operation with a larger buffer is well-defined. This only matters when it + // takes two steps to process `in`. + size_t expected = EVP_CIPHER_CTX_max_next_update(ctx, in_len); + if (max_out_len < expected) { + OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL); + return 0; } assert(block_size <= sizeof(ctx->final)); @@ -466,16 +481,15 @@ static int EVP_DecryptUpdate_ex_internal(EVP_CIPHER_CTX *ctx, uint8_t *out, // less. size_t head = in_span.size() > block_size ? in_span.size() - block_size : 0; size_t head_out_len; - if (!EVP_EncryptUpdate_ex_internal(ctx, out_span.data(), &head_out_len, - out_span.size(), in_span.data(), head)) { + if (!EVP_EncryptUpdate_ex(ctx, out_span.data(), &head_out_len, + out_span.size(), in_span.data(), head)) { return 0; } in_span = in_span.subspan(head); out_span = out_span.subspan(head_out_len); size_t final_size; - if (!EVP_EncryptUpdate_ex_internal(ctx, ctx->final, &final_size, - sizeof(ctx->final), in_span.data(), - in_span.size())) { + if (!EVP_EncryptUpdate_ex(ctx, ctx->final, &final_size, sizeof(ctx->final), + in_span.data(), in_span.size())) { return 0; } ctx->final_used = 1; @@ -484,9 +498,9 @@ static int EVP_DecryptUpdate_ex_internal(EVP_CIPHER_CTX *ctx, uint8_t *out, } else { // Buffer will be non-empty. size_t written_out_len; - if (!EVP_EncryptUpdate_ex_internal(ctx, out_span.data(), &written_out_len, - out_span.size(), in_span.data(), - in_span.size())) { + if (!EVP_EncryptUpdate_ex(ctx, out_span.data(), &written_out_len, + out_span.size(), in_span.data(), + in_span.size())) { return 0; } assert(block_size == 1 || ctx->buf_len != 0); @@ -494,17 +508,10 @@ static int EVP_DecryptUpdate_ex_internal(EVP_CIPHER_CTX *ctx, uint8_t *out, } *out_len = max_out_len - out_span.size(); + assert(*out_len == expected); // See `EVP_CIPHER_CTX_max_next_update` check. return 1; } -int EVP_DecryptUpdate_ex(EVP_CIPHER_CTX *ctx, uint8_t *out, size_t *out_len, - size_t max_out_len, const uint8_t *in, size_t in_len) { - return WrapWithPoison(ctx, [&] { - return EVP_DecryptUpdate_ex_internal(ctx, out, out_len, max_out_len, in, - in_len); - }); -} - int EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len) { size_t out_len_sz; int ret = @@ -514,9 +521,8 @@ int EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len) { return ret; } -static int EVP_DecryptFinal_ex2_internal(EVP_CIPHER_CTX *ctx, - unsigned char *out, size_t *out_len, - size_t max_out_len) { +int EVP_DecryptFinal_ex2(EVP_CIPHER_CTX *ctx, uint8_t *out, size_t *out_len, + size_t max_out_len) { *out_len = 0; size_t block_size = ctx->cipher->block_size; @@ -525,6 +531,7 @@ static int EVP_DecryptFinal_ex2_internal(EVP_CIPHER_CTX *ctx, if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) { return ctx->cipher->cipher_final(ctx); } + EVP_Cipher_verify_service_indicator(ctx); return 1; } @@ -534,6 +541,7 @@ static int EVP_DecryptFinal_ex2_internal(EVP_CIPHER_CTX *ctx, OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH); return 0; } + EVP_Cipher_verify_service_indicator(ctx); return 1; } @@ -543,8 +551,8 @@ static int EVP_DecryptFinal_ex2_internal(EVP_CIPHER_CTX *ctx, } assert(block_size <= sizeof(ctx->final)); - // The following assumes that the ciphertext has been authenticated. - // Otherwise it provides a padding oracle. + // The following assumes that the ciphertext has been authenticated. Otherwise + // it provides a padding oracle. size_t padding = ctx->final[block_size - 1]; if (padding == 0 || padding > block_size) { OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT); @@ -568,21 +576,10 @@ static int EVP_DecryptFinal_ex2_internal(EVP_CIPHER_CTX *ctx, out_span = out_span.subspan(payload); *out_len = max_out_len - out_span.size(); + EVP_Cipher_verify_service_indicator(ctx); return 1; } -int EVP_DecryptFinal_ex2(EVP_CIPHER_CTX *ctx, unsigned char *out, - size_t *out_len, size_t max_out_len) { - *out_len = 0; - return WrapWithPoison(ctx, [&] { - if (!EVP_DecryptFinal_ex2_internal(ctx, out, out_len, max_out_len)) { - return 0; - } - EVP_Cipher_verify_service_indicator(ctx); - return 1; - }); -} - int EVP_Cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, const uint8_t *in, size_t in_len) { const int kError = @@ -614,15 +611,15 @@ int EVP_Cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, const uint8_t *in, out_len = in_len; } - // |EVP_CIPH_FLAG_CUSTOM_CIPHER| never sets the FIPS indicator via - // |EVP_Cipher| because it's complicated whether the operation has completed - // or not. E.g. AES-GCM with a non-NULL |in| argument hasn't completed an - // operation. Callers should use the |EVP_AEAD| API or, at least, - // |EVP_CipherUpdate| etc. + // `EVP_CIPH_FLAG_CUSTOM_CIPHER` never sets the FIPS indicator via + // `EVP_Cipher` because it's complicated whether the operation has completed + // or not. E.g. AES-GCM with a non-NULL `in` argument hasn't completed an + // operation. Callers should use the `EVP_AEAD` API or, at least, + // `EVP_CipherUpdate` etc. // - // This call can't be pushed into |EVP_Cipher_verify_service_indicator| - // because whether |ret| indicates success or not depends on whether - // |EVP_CIPH_FLAG_CUSTOM_CIPHER| is set. (This unreasonable, but matches + // This call can't be pushed into `EVP_Cipher_verify_service_indicator` + // because whether `ret` indicates success or not depends on whether + // `EVP_CIPH_FLAG_CUSTOM_CIPHER` is set. (This unreasonable, but matches // OpenSSL.) if (!(ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER)) { EVP_Cipher_verify_service_indicator(ctx); @@ -654,13 +651,11 @@ int EVP_CipherUpdate_ex(EVP_CIPHER_CTX *ctx, uint8_t *out, size_t *out_len, } int EVP_CipherUpdateAAD(EVP_CIPHER_CTX *ctx, const uint8_t *in, size_t in_len) { - return WrapWithPoison(ctx, [&] { - if (!(ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER)) { - OPENSSL_PUT_ERROR(CIPHER, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); - return 0; - } - return ctx->cipher->update_aad(ctx, in, in_len); - }); + if (!(ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER)) { + OPENSSL_PUT_ERROR(CIPHER, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); + return 0; + } + return ctx->cipher->update_aad(ctx, in, in_len); } int EVP_CipherFinal_ex(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len) { @@ -680,6 +675,19 @@ int EVP_CipherFinal_ex2(EVP_CIPHER_CTX *ctx, uint8_t *out, size_t *out_len, } } +size_t EVP_CIPHER_CTX_max_final(const EVP_CIPHER_CTX *ctx) { + size_t block_size = ctx->cipher->block_size; + if (block_size == 1 || (ctx->flags & EVP_CIPH_NO_PADDING)) { + return 0; + } + if (ctx->encrypt) { + return block_size; + } else { + // At least one byte will be removed when processing padding. + return block_size - 1; + } +} + const EVP_CIPHER *EVP_CIPHER_CTX_cipher(const EVP_CIPHER_CTX *ctx) { return ctx->cipher; } diff --git a/third_party/boringssl/src/crypto/fipsmodule/cipher/e_aes.cc.inc b/third_party/boringssl/src/crypto/fipsmodule/cipher/e_aes.cc.inc index 4ddf4b95f..83fa9fffc 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/cipher/e_aes.cc.inc +++ b/third_party/boringssl/src/crypto/fipsmodule/cipher/e_aes.cc.inc @@ -93,7 +93,7 @@ static int aes_init_key(EVP_CIPHER_CTX *ctx, const uint8_t *key, if (ret == 0) { vpaes_decrypt_key_to_bsaes(&dat->ks.ks, &dat->ks.ks); } - // If |dat->stream.cbc| is provided, |dat->block| is never used. + // If `dat->stream.cbc` is provided, `dat->block` is never used. dat->block = nullptr; dat->stream.cbc = bsaes_cbc_encrypt; #endif @@ -323,14 +323,14 @@ static int aes_gcm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) { gctx->iv_gen = 1; return 1; } - // Fixed field must be at least 4 bytes and invocation field - // at least 8. + // The fixed field must be at least 4 bytes and the invocation field at + // least 8. if (arg < 4 || (gctx->ivlen - arg) < 8) { return 0; } OPENSSL_memcpy(gctx->iv, ptr, arg); if (c->encrypt) { - // |BCM_rand_bytes| calls within the fipsmodule should be wrapped with + // `BCM_rand_bytes` calls within the fipsmodule should be wrapped with // state lock functions to avoid updating the service indicator with the // DRBG functions. FIPS_service_indicator_lock_state(); @@ -341,7 +341,7 @@ static int aes_gcm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) { return 1; case EVP_CTRL_GCM_IV_GEN: { - if (gctx->iv_gen == 0 || gctx->key_set == 0) { + if (gctx->iv_gen == 0 || gctx->key_set == 0 || gctx->ivlen < 8) { return 0; } CRYPTO_gcm128_init_ctx(&gctx->key, &gctx->gcm, gctx->iv, gctx->ivlen); @@ -349,8 +349,6 @@ static int aes_gcm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) { arg = gctx->ivlen; } OPENSSL_memcpy(ptr, gctx->iv + gctx->ivlen - arg, arg); - // Invocation field will be at least 8 bytes in size, so no need to check - // wrap around or increment more than last 8 bytes. uint8_t *ctr = gctx->iv + gctx->ivlen - 8; CRYPTO_store_u64_be(ctr, CRYPTO_load_u64_be(ctr) + 1); gctx->iv_set = 1; @@ -358,7 +356,8 @@ static int aes_gcm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) { } case EVP_CTRL_GCM_SET_IV_INV: - if (gctx->iv_gen == 0 || gctx->key_set == 0 || c->encrypt) { + if (gctx->iv_gen == 0 || gctx->key_set == 0 || c->encrypt || arg < 0 || + arg > gctx->ivlen) { return 0; } OPENSSL_memcpy(gctx->iv + gctx->ivlen - arg, ptr, arg); @@ -960,7 +959,7 @@ static int aead_aes_gcm_sealv_randnonce(const EVP_AEAD_CTX *ctx, return 0; } - // |BCM_rand_bytes| calls within the fipsmodule should be wrapped with state + // `BCM_rand_bytes` calls within the fipsmodule should be wrapped with state // lock functions to avoid updating the service indicator with the DRBG // functions. FIPS_service_indicator_lock_state(); diff --git a/third_party/boringssl/src/crypto/fipsmodule/cipher/e_aesccm.cc.inc b/third_party/boringssl/src/crypto/fipsmodule/cipher/e_aesccm.cc.inc index f100cf932..de6bfd4ff 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/cipher/e_aesccm.cc.inc +++ b/third_party/boringssl/src/crypto/fipsmodule/cipher/e_aesccm.cc.inc @@ -67,7 +67,7 @@ static int ccm128_init_state(const struct ccm128_context *ctx, const unsigned M = ctx->M; const unsigned L = ctx->L; - // |L| determines the expected |nonce_len| and the limit for |plaintext_len|. + // `L` determines the expected `nonce_len` and the limit for `plaintext_len`. if (plaintext_len > CRYPTO_ccm128_max_input(ctx)) { OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE); return 0; @@ -155,8 +155,8 @@ static int ccm128_init_state(const struct ccm128_context *ctx, return 0; } - // Assemble the first block for encrypting and decrypting. The bottom |L| - // bytes are replaced with a counter and all bit the encoding of |L| is + // Assemble the first block for encrypting and decrypting. The bottom `L` + // bytes are replaced with a counter and all bit the encoding of `L` is // cleared in the first byte. state->nonce[0] &= 7; return 1; diff --git a/third_party/boringssl/src/crypto/fipsmodule/cipher/internal.h b/third_party/boringssl/src/crypto/fipsmodule/cipher/internal.h index 0983b1577..70ecf5ec9 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/cipher/internal.h +++ b/third_party/boringssl/src/crypto/fipsmodule/cipher/internal.h @@ -33,7 +33,7 @@ extern "C" { -// EVP_CIPH_MODE_MASK contains the bits of |flags| that represent the mode. +// EVP_CIPH_MODE_MASK contains the bits of `flags` that represent the mode. #define EVP_CIPH_MODE_MASK 0x3f // EVP_AEAD represents a specific AEAD algorithm. @@ -43,8 +43,8 @@ struct evp_aead_st { uint8_t overhead; uint8_t max_tag_len; - // init initialises an |EVP_AEAD_CTX|. If this call returns zero then - // |cleanup| will not be called for that context. + // init initialises an `EVP_AEAD_CTX`. If this call returns zero then + // `cleanup` will not be called for that context. int (*init)(EVP_AEAD_CTX *, const uint8_t *key, size_t key_len, size_t tag_len); int (*init_with_direction)(EVP_AEAD_CTX *, const uint8_t *key, size_t key_len, @@ -53,7 +53,7 @@ struct evp_aead_st { // AEADs need to provide one of the following sets of methods: // - // - openv + sealv: variable tag lenght AEAD. + // - openv + sealv: variable tag length AEAD. // - openv_detached + sealv: fixed tag length AEAD. int (*openv)(const EVP_AEAD_CTX *ctx, bssl::Span iovecs, @@ -96,16 +96,16 @@ struct evp_cipher_st { // cipher. unsigned ctx_size; - // flags contains the OR of a number of flags. See |EVP_CIPH_*|. + // flags contains the OR of a number of flags. See `EVP_CIPH_*`. uint32_t flags; int (*init)(EVP_CIPHER_CTX *ctx, const uint8_t *key, const uint8_t *iv, int enc); - // cipher encrypts/decrypts |in|, write output to |out|. Writes exactly |len| - // bytes, which must be a multiple of the |block_size|. + // cipher encrypts/decrypts `in`, write output to `out`. Writes exactly `len` + // bytes, which must be a multiple of the `block_size`. // - // For ciphers where encryption and decryption operations differ, |init| + // For ciphers where encryption and decryption operations differ, `init` // shall set an internal state for this. // // Returns 1 on success, or 0 on error. @@ -115,22 +115,22 @@ struct evp_cipher_st { // cipher_final finalizes the cipher, performing possible final // authentication checks. // - // Only used for |EVP_CIPH_FLAG_CUSTOM_CIPHER| ciphers. + // Only used for `EVP_CIPH_FLAG_CUSTOM_CIPHER` ciphers. // // Returns 1 on success, or 0 on error. When decrypting, if an error is // returned, the decrypted data must not be used. int (*cipher_final)(EVP_CIPHER_CTX *ctx); - // update_aad adds |in| (of length |inl|) to the authenticated data for the + // update_aad adds `in` (of length `inl`) to the authenticated data for the // encryption operation. // - // Only used for |EVP_CIPH_FLAG_CUSTOM_CIPHER| ciphers. + // Only used for `EVP_CIPH_FLAG_CUSTOM_CIPHER` ciphers. // // Returns 1 on success, or 0 on error. int (*update_aad)(EVP_CIPHER_CTX *ctx, const uint8_t *in, size_t inl); // cleanup, if non-NULL, releases memory associated with the context. It is - // called if |EVP_CTRL_INIT| succeeds. Note that |init| may not have been + // called if `EVP_CTRL_INIT` succeeds. Note that `init` may not have been // called at this point. void (*cleanup)(EVP_CIPHER_CTX *); @@ -141,7 +141,7 @@ struct evp_cipher_st { BSSL_NAMESPACE_BEGIN -// CopySpan copies an entire span of bytes from |from| to |to|. +// CopySpan copies an entire span of bytes from `from` to `to`. // // The spans need to have the same length. inline void CopySpan(Span from, Span to) { @@ -149,7 +149,7 @@ inline void CopySpan(Span from, Span to) { std::copy(from.begin(), from.end(), to.begin()); } -// CopyToPrefix copies a span of bytes from |from| into |to|. It aborts +// CopyToPrefix copies a span of bytes from `from` into `to`. It aborts // if there is not enough space. // // TODO(crbug.com/404286922): Can we simplify this in a C++20 world (e.g. @@ -161,15 +161,12 @@ inline void CopyToPrefix(Span from, Span to) { // Generic CRYPTO_IOVEC/CRYPTO_IVEC helpers. namespace iovec { -// IsValid returns whether the given |CRYPTO_IVEC| or |CRYPTO_IOVEC| is -// valid for use with public APIs, i.e. does not contain more than |SIZE_MAX| -// bytes and not more than |CRYPTO_IOVEC_MAX| chunks. Note that the `EVP_AEAD` -// methods need to accept an arbitrary number of chunks. +// IsValid returns whether the given `CRYPTO_IVEC` or `CRYPTO_IOVEC` is +// valid for use with public APIs, i.e. does not contain more than `SIZE_MAX` +// bytes. Note that the `EVP_AEAD` methods need to accept an arbitrary number +// of chunks. template inline bool IsValid(Span ivecs) { - if (ivecs.size() > CRYPTO_IOVEC_MAX) { - return false; - } size_t allowed = SIZE_MAX; for (const IVec &ivec : ivecs) { size_t len = ivec.len; @@ -181,8 +178,8 @@ inline bool IsValid(Span ivecs) { return true; } -// Length returns the total length in bytes of a given |CRYPTO_IVEC| or -// |CRYPTO_IOVEC|. +// Length returns the total length in bytes of a given `CRYPTO_IVEC` or +// `CRYPTO_IOVEC`. template inline size_t TotalLength(Span ivecs) { size_t total = 0; @@ -192,15 +189,15 @@ inline size_t TotalLength(Span ivecs) { return total; } -// GetAndRemoveSuffix takes |suffix_buf.size()| final bytes from the given -// |CRYPTO_IVEC| or |CRYPTO_IOVEC| (mutating said iovec to no longer contain +// GetAndRemoveSuffix takes `suffix_buf.size()` final bytes from the given +// `CRYPTO_IVEC` or `CRYPTO_IOVEC` (mutating said iovec to no longer contain // those bytes) and returns them. // -// If the byte range is contained in a single chunk of |ivecs|, it will just -// return that span pointing into |ivecs|; otherwise, it will copy the bytes -// into |out| and return that. +// If the byte range is contained in a single chunk of `ivecs`, it will just +// return that span pointing into `ivecs`; otherwise, it will copy the bytes +// into `out` and return that. // -// If |ivecs| is too short, returns |nullopt|. +// If `ivecs` is too short, returns `nullopt`. template inline std::optional> GetAndRemoveSuffix( @@ -221,7 +218,7 @@ inline std::optional> GetAndRemoveSuffix( ivecs.back().len -= suffix_buf.size(); return Span(ivecs.back().*ReadFrom + ivecs.back().len, suffix_buf.size()); } - // Otherwise, collect it into the buffer while trimming |ivecs|. + // Otherwise, collect it into the buffer while trimming `ivecs`. Span remaining = suffix_buf; while (!ivecs.empty()) { Span src(ivecs.back().*ReadFrom, ivecs.back().len); @@ -238,8 +235,8 @@ inline std::optional> GetAndRemoveSuffix( return std::nullopt; } -// GetAndRemoveOutSuffix is like |GetAndRemoveSuffix| but takes from a -// |CRYPTO_IOVEC|'s |out| member instead. +// GetAndRemoveOutSuffix is like `GetAndRemoveSuffix` but takes from a +// `CRYPTO_IOVEC`'s `out` member instead. inline std::optional> GetAndRemoveOutSuffix( Span out, Span iovecs) { return GetAndRemoveSuffix out, CRYPTO_IOVEC head, } } // namespace internal -// ForEachBlockRange iterates over the |ivecs| as follows: +// ForEachBlockRange iterates over the `ivecs` as follows: // -// - |f_whole| gets called on whole blocks crossing |ivecs| chunk boundaries, or +// - `f_whole` gets called on whole blocks crossing `ivecs` chunk boundaries, or // ranges of whole blocks that are entirely in chunks. -// - |f| gets called exactly once, on the last block range which may +// - `f` gets called exactly once, on the last block range which may // end up with a partial block. -// - Both functions receive an |in| pointer that either points into |ivecs| or -// into a chunk assembly buffer and a |len| which indicates the number of -// bytes from |in| that can be accessed. If the function returns 0, +// - Both functions receive an `in` pointer that either points into `ivecs` or +// into a chunk assembly buffer and a `len` which indicates the number of +// bytes from `in` that can be accessed. If the function returns 0, // iteration stops. -// - If |WriteOut| is set, |f_whole| and |f| receive an extra |out| +// - If `WriteOut` is set, `f_whole` and `f` receive an extra `out` // argument to which they can write output. This output will be placed into -// the |ivecs|'s |out| members either during or after the call. If iteration -// was stopped, the contents of |out| are indeterminate. +// the `ivecs`'s `out` members either during or after the call. If iteration +// was stopped, the contents of `out` are indeterminate. // - The return value is true if iteration was not stopped by the callbacks. template < size_t BlockSize, bool WriteOut = false, typename IVec, @@ -310,8 +307,8 @@ inline bool ForEachBlockRange(Span ivecs, const FWhole &f_whole, } }; - // Ensure the last item in |ivecs| is nonempty. This is necessary for - // detecting being at the end and calling |f_final| at the appropriate time. + // Ensure the last item in `ivecs` is nonempty. This is necessary for + // detecting being at the end and calling `f_final` at the appropriate time. Span ivecs_trimmed = ivecs; while (!ivecs_trimmed.empty() && ivecs_trimmed.back().len == 0) { ivecs_trimmed = ivecs_trimmed.first(ivecs_trimmed.size() - 1); @@ -320,7 +317,7 @@ inline bool ForEachBlockRange(Span ivecs, const FWhole &f_whole, return call_func(f_final, IVec{}); } - // Now there are at least two non-empty |ivecs|, and neither the first nor the + // Now there are at least two non-empty `ivecs`, and neither the first nor the // last can be empty. MutableIVec current_range_head = ivecs_trimmed.front(); @@ -356,7 +353,7 @@ inline bool ForEachBlockRange(Span ivecs, const FWhole &f_whole, remove_prefix(collect_from_head, remaining); break; } - // Consume all of |ivec| and advance. + // Consume all of `ivec` and advance. in.Append(Span(collect_from_head.*ReadFrom, collect_from_head.len)); if (collect_from_rest.empty()) { // Nothing left - so this is the final block. @@ -378,12 +375,12 @@ inline bool ForEachBlockRange(Span ivecs, const FWhole &f_whole, } assert(in.size() == BlockSize); - // The above loop ensures this condition by the |break| only happening if - // |collect_from_head| has at least one byte remaining, and the loop + // The above loop ensures this condition by the `break` only happening if + // `collect_from_head` has at least one byte remaining, and the loop // otherwise ensuring as an invariant that the final chunk - which is - // nonempty - is among |collect_from_head| and |collect_from_rest|. + // nonempty - is among `collect_from_head` and `collect_from_rest`. // - // As such, at least one byte is remaining, and thus calling |f_whole| is + // As such, at least one byte is remaining, and thus calling `f_whole` is // appropriate. assert(collect_from_head.len != 0 || !collect_from_rest.empty()); @@ -406,15 +403,19 @@ inline bool ForEachBlockRange(Span ivecs, const FWhole &f_whole, } // If current_range_head.len is zero, then the last item of ivecs is empty. - // That however was excluded at the start of the function to ensure |f_final| + // That however was excluded at the start of the function to ensure `f_final` // is always used for the last call. assert(current_range_head.len != 0); return call_func(f_final, current_range_head); } -// ForEachOutBlockRange is like |ForEachBlockRange| but reads from a -// |CRYPTO_IOVEC|'s |out| member instead. +// MaybeInplaceArray can hold a copy of a CRYPTO_IOVEC. If it is a low +// amount of entries, it will be stored on the stack, otherwise on the heap. +using MaybeInplaceArray = bssl::MaybeInplaceArray; + +// ForEachOutBlockRange is like `ForEachBlockRange` but reads from a +// `CRYPTO_IOVEC`'s `out` member instead. template < size_t BlockSize, typename /* int(const uint8_t *in, [uint8_t *out,] size_t len) */ FWhole, @@ -428,7 +429,7 @@ inline int ForEachOutBlockRange(Span iovecs, f_final); } -// ForEachBlockRange_Dynamic is simply |ForEachBlockRange| with a +// ForEachBlockRange_Dynamic is simply `ForEachBlockRange` with a // runtime dispatch on the block size. template < bool WriteOut = false, typename IVec, typename ReadFromT = const uint8_t *, diff --git a/third_party/boringssl/src/crypto/fipsmodule/cmac/cmac.cc.inc b/third_party/boringssl/src/crypto/fipsmodule/cmac/cmac.cc.inc index 18d09afd8..93e48f874 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/cmac/cmac.cc.inc +++ b/third_party/boringssl/src/crypto/fipsmodule/cmac/cmac.cc.inc @@ -37,7 +37,7 @@ struct cmac_ctx_st { uint8_t k2[AES_BLOCK_SIZE]; // Last (possibly partial) scratch uint8_t block[AES_BLOCK_SIZE]; - // block_used contains the number of valid bytes in |block|. + // block_used contains the number of valid bytes in `block`. unsigned block_used; }; @@ -115,14 +115,14 @@ int CMAC_CTX_copy(CMAC_CTX *out, const CMAC_CTX *in) { return 1; } -// binary_field_mul_x_128 treats the 128 bits at |in| as an element of GF(2¹²⁸) -// with a hard-coded reduction polynomial and sets |out| as x times the input. +// binary_field_mul_x_128 treats the 128 bits at `in` as an element of GF(2¹²⁸) +// with a hard-coded reduction polynomial and sets `out` as x times the input. // // See https://tools.ietf.org/html/rfc4493#section-2.3 static void binary_field_mul_x_128(uint8_t out[16], const uint8_t in[16]) { unsigned i; - // Shift |in| to left, including carry. + // Shift `in` to left, including carry. for (i = 0; i < 15; i++) { out[i] = (in[i] << 1) | (in[i + 1] >> 7); } @@ -132,14 +132,14 @@ static void binary_field_mul_x_128(uint8_t out[16], const uint8_t in[16]) { out[i] = (in[i] << 1) ^ ((0 - carry) & 0x87); } -// binary_field_mul_x_64 behaves like |binary_field_mul_x_128| but acts on an +// binary_field_mul_x_64 behaves like `binary_field_mul_x_128` but acts on an // element of GF(2⁶⁴). // // See https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-38b.pdf static void binary_field_mul_x_64(uint8_t out[8], const uint8_t in[8]) { unsigned i; - // Shift |in| to left, including carry. + // Shift `in` to left, including carry. for (i = 0; i < 7; i++) { out[i] = (in[i] << 1) | (in[i + 1] >> 7); } @@ -156,7 +156,7 @@ int CMAC_Init(CMAC_CTX *ctx, const void *key, size_t key_len, int ret = 0; uint8_t scratch[AES_BLOCK_SIZE]; - // We have to avoid the underlying AES-CBC |EVP_CIPHER| services updating the + // We have to avoid the underlying AES-CBC `EVP_CIPHER` services updating the // indicator state, so we lock the state here. FIPS_service_indicator_lock_state(); @@ -196,7 +196,7 @@ int CMAC_Reset(CMAC_CTX *ctx) { int CMAC_Update(CMAC_CTX *ctx, const uint8_t *in, size_t in_len) { int ret = 0; - // We have to avoid the underlying AES-CBC |EVP_Cipher| services updating the + // We have to avoid the underlying AES-CBC `EVP_Cipher` services updating the // indicator state, so we lock the state here. FIPS_service_indicator_lock_state(); @@ -215,9 +215,9 @@ int CMAC_Update(CMAC_CTX *ctx, const uint8_t *in, size_t in_len) { in_len -= todo; ctx->block_used += todo; - // If |in_len| is zero then either |ctx->block_used| is less than - // |block_size|, in which case we can stop here, or |ctx->block_used| is - // exactly |block_size| but there's no more data to process. In the latter + // If `in_len` is zero then either `ctx->block_used` is less than + // `block_size`, in which case we can stop here, or `ctx->block_used` is + // exactly `block_size` but there's no more data to process. In the latter // case we don't want to process this block now because it might be the last // block and that block is treated specially. if (in_len == 0) { @@ -242,7 +242,7 @@ int CMAC_Update(CMAC_CTX *ctx, const uint8_t *in, size_t in_len) { } OPENSSL_memcpy(ctx->block, in, in_len); - // |in_len| is bounded by |block_size|, which fits in |unsigned|. + // `in_len` is bounded by `block_size`, which fits in `unsigned`. static_assert(EVP_MAX_BLOCK_LENGTH < UINT_MAX, "EVP_MAX_BLOCK_LENGTH is too large"); ctx->block_used = (unsigned)in_len; @@ -258,7 +258,7 @@ int CMAC_Final(CMAC_CTX *ctx, uint8_t *out, size_t *out_len) { size_t block_size = EVP_CIPHER_CTX_block_size(&ctx->cipher_ctx); assert(block_size <= AES_BLOCK_SIZE); - // We have to avoid the underlying AES-CBC |EVP_Cipher| services updating the + // We have to avoid the underlying AES-CBC `EVP_Cipher` services updating the // indicator state, so we lock the state here. FIPS_service_indicator_lock_state(); diff --git a/third_party/boringssl/src/crypto/fipsmodule/delocate.h b/third_party/boringssl/src/crypto/fipsmodule/delocate.h index 750154805..b97108510 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/delocate.h +++ b/third_party/boringssl/src/crypto/fipsmodule/delocate.h @@ -29,7 +29,7 @@ #if !defined(BORINGSSL_SHARED_LIBRARY) && defined(BORINGSSL_FIPS) && \ !defined(OPENSSL_ASAN) && !defined(OPENSSL_MSAN) #define DEFINE_BSS_GET(type, name, init_expr) \ - /* delocate needs C linkage and for |name| to be unique across BCM. */ \ + /* delocate needs C linkage and for `name` to be unique across BCM. */ \ extern "C" { \ extern type BCM_ADD_PREFIX(name); \ type BCM_ADD_PREFIX(name) init_expr; \ @@ -67,12 +67,12 @@ } \ static void name##_do_init(type *out) -// DEFINE_METHOD_FUNCTION defines a function named |name| which returns a -// method table of type const |type|*. In FIPS mode, to avoid rel.ro data, it +// DEFINE_METHOD_FUNCTION defines a function named `name` which returns a +// method table of type const `type`*. In FIPS mode, to avoid rel.ro data, it // is split into a CRYPTO_once_t-guarded initializer in the module and // unhashed, non-module accessor functions to space reserved in the BSS. The // method table is initialized by a caller-supplied function which takes a -// parameter named |out| of type |type|*. The caller should follow the macro +// parameter named `out` of type `type`*. The caller should follow the macro // invocation with the body of this function: // // DEFINE_METHOD_FUNCTION(EVP_MD, EVP_md4) { diff --git a/third_party/boringssl/src/crypto/fipsmodule/dh/check.cc.inc b/third_party/boringssl/src/crypto/fipsmodule/dh/check.cc.inc index ec8e35841..084881903 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/dh/check.cc.inc +++ b/third_party/boringssl/src/crypto/fipsmodule/dh/check.cc.inc @@ -67,12 +67,12 @@ int DH_check_pub_key(const DH *dh, const BIGNUM *pub_key, int *out_flags) { } BN_CTXScope scope(ctx.get()); - // Check |pub_key| is greater than 1. + // Check `pub_key` is greater than 1. if (BN_cmp(pub_key, BN_value_one()) <= 0) { *out_flags |= DH_CHECK_PUBKEY_TOO_SMALL; } - // Check |pub_key| is less than |impl->p| - 1. + // Check `pub_key` is less than `impl->p` - 1. BIGNUM *tmp = BN_CTX_get(ctx.get()); if (tmp == nullptr || !BN_copy(tmp, impl->p.get()) || !BN_sub_word(tmp, 1)) { return 0; @@ -82,9 +82,9 @@ int DH_check_pub_key(const DH *dh, const BIGNUM *pub_key, int *out_flags) { } if (impl->q != nullptr) { - // Check |pub_key|^|impl->q| is 1 mod |impl->p|. This is necessary for RFC + // Check `pub_key`^`impl->q` is 1 mod `impl->p`. This is necessary for RFC // 5114 groups which are not safe primes but pick a generator on a - // prime-order subgroup of size |impl->q|. + // prime-order subgroup of size `impl->q`. if (!BN_mod_exp_mont(tmp, pub_key, impl->q.get(), impl->p.get(), ctx.get(), nullptr)) { return 0; diff --git a/third_party/boringssl/src/crypto/fipsmodule/dh/dh.cc.inc b/third_party/boringssl/src/crypto/fipsmodule/dh/dh.cc.inc index 9837c6c6b..8dfcdee98 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/dh/dh.cc.inc +++ b/third_party/boringssl/src/crypto/fipsmodule/dh/dh.cc.inc @@ -145,7 +145,7 @@ int DH_generate_key(DH *dh) { } // Only generate a private key if there's already one. Otherwise, - // |DH_generate_key| recomputes the public key. + // `DH_generate_key` recomputes the public key. const BIGNUM *priv_key = impl->priv_key.get(); UniquePtr new_priv_key; if (priv_key == nullptr) { @@ -156,10 +156,10 @@ int DH_generate_key(DH *dh) { } if (impl->q) { // Section 5.6.1.1.4 of SP 800-56A Rev3 generates a private key uniformly - // from [1, min(2^N-1, q-1)]. + // from [1, min(2ᴺ-1, q-1)]. // // Although SP 800-56A Rev3 now permits a private key length N, - // |impl->priv_length| historically was ignored when q is available. We + // `impl->priv_length` historically was ignored when q is available. We // continue to ignore it and interpret such a configuration as N = len(q). if (!BN_rand_range_ex(new_priv_key.get(), 1, impl->q.get())) { OPENSSL_PUT_ERROR(DH, ERR_R_BN_LIB); @@ -170,12 +170,12 @@ int DH_generate_key(DH *dh) { // the (p-1)/2 subgroup. So, we use q = (p-1)/2. (If g generates a smaller // prime-order subgroup, q will still divide (p-1)/2.) // - // We set N from |impl->priv_length|. Section 5.6.1.1.4 of SP 800-56A Rev3 + // We set N from `impl->priv_length`. Section 5.6.1.1.4 of SP 800-56A Rev3 // says to reject N > len(q), or N > num_bits(p) - 1. However, this logic // originally aligned with PKCS#3, which allows num_bits(p). Instead, we - // clamp |impl->priv_length| before invoking the algorithm. + // clamp `impl->priv_length` before invoking the algorithm. - // Compute M = min(2^N, q). + // Compute M = min(2ᴺ, q). UniquePtr priv_key_limit(BN_new()); if (priv_key_limit == nullptr) { OPENSSL_PUT_ERROR(DH, ERR_R_BN_LIB); @@ -189,7 +189,7 @@ int DH_generate_key(DH *dh) { return 0; } } else { - // M = 2^N. + // M = 2ᴺ. if (!BN_set_bit(priv_key_limit.get(), impl->priv_length)) { OPENSSL_PUT_ERROR(DH, ERR_R_BN_LIB); return 0; @@ -302,7 +302,7 @@ int DH_compute_key(unsigned char *out, const BIGNUM *peers_key, DH *dh) { !dh_compute_key(dh, shared_key, peers_key, ctx.get())) { return -1; } - // A |BIGNUM|'s byte count fits in |int|. + // A `BIGNUM`'s byte count fits in `int`. return static_cast(BN_bn2bin(shared_key, out)); } @@ -353,6 +353,13 @@ int DH_up_ref(DH *dh) { return 1; } +DH *DH_dup_ref(const DH *dh) { + if (dh == nullptr) { + return nullptr; + } + return FromOpaque(dh)->DupRefInternal(); +} + DH *DH_get_rfc7919_2048() { // This is the prime from https://tools.ietf.org/html/rfc7919#appendix-A.1, // which is specifically approved for FIPS in appendix D of SP 800-56Ar3. @@ -392,7 +399,7 @@ DH *DH_get_rfc7919_2048() { ffdhe2048_g.get())) { return nullptr; } - // |DH_set0_pqg| takes ownership on success. + // `DH_set0_pqg` takes ownership on success. ffdhe2048_p.release(); ffdhe2048_q.release(); ffdhe2048_g.release(); diff --git a/third_party/boringssl/src/crypto/fipsmodule/dh/internal.h b/third_party/boringssl/src/crypto/fipsmodule/dh/internal.h index 337afa619..bacde0f1b 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/dh/internal.h +++ b/third_party/boringssl/src/crypto/fipsmodule/dh/internal.h @@ -32,11 +32,11 @@ class DHImpl : public dh_st, public RefCounted { UniquePtr p; UniquePtr g; UniquePtr q; - UniquePtr pub_key; // g^x mod p + UniquePtr pub_key; // gˣ mod p UniquePtr priv_key; // x // priv_length contains the length, in bits, of the private value. If zero, - // the private value will be the same length as |p|. + // the private value will be the same length as `p`. unsigned priv_length = 0; mutable Mutex method_mont_p_lock; @@ -47,12 +47,12 @@ class DHImpl : public dh_st, public RefCounted { ~DHImpl() = default; }; -// dh_check_params_fast checks basic invariants on |dh|'s domain parameters. It -// does not check that |dh| forms a valid group, only that the sizes are within +// dh_check_params_fast checks basic invariants on `dh`'s domain parameters. It +// does not check that `dh` forms a valid group, only that the sizes are within // DoS bounds. int dh_check_params_fast(const DH *dh); -// dh_compute_key_padded_no_self_test does the same as |DH_compute_key_padded|, +// dh_compute_key_padded_no_self_test does the same as `DH_compute_key_padded`, // but doesn't try to run the self-test first. This is for use in the self tests // themselves, to prevent an infinite loop. int dh_compute_key_padded_no_self_test(unsigned char *out, diff --git a/third_party/boringssl/src/crypto/fipsmodule/digest/digest.cc.inc b/third_party/boringssl/src/crypto/fipsmodule/digest/digest.cc.inc index 49b067b78..8d4b2aa26 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/digest/digest.cc.inc +++ b/third_party/boringssl/src/crypto/fipsmodule/digest/digest.cc.inc @@ -84,8 +84,20 @@ void EVP_MD_CTX_free(EVP_MD_CTX *ctx) { void EVP_MD_CTX_destroy(EVP_MD_CTX *ctx) { EVP_MD_CTX_free(ctx); } int EVP_DigestFinalXOF(EVP_MD_CTX *ctx, uint8_t *out, size_t len) { - OPENSSL_PUT_ERROR(DIGEST, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); - return 0; + if (!EVP_DigestSqueeze(ctx, out, len)) { + return 0; + } + EVP_MD_CTX_cleanse(ctx); + return 1; +} + +int EVP_DigestSqueeze(EVP_MD_CTX *ctx, unsigned char *out, size_t len) { + if (ctx->digest->squeeze == nullptr) { + OPENSSL_PUT_ERROR(DIGEST, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); + return 0; + } + ctx->digest->squeeze(ctx, out, len); + return 1; } uint32_t EVP_MD_meth_get_flags(const EVP_MD *md) { return EVP_MD_flags(md); } @@ -93,8 +105,8 @@ uint32_t EVP_MD_meth_get_flags(const EVP_MD *md) { return EVP_MD_flags(md); } void EVP_MD_CTX_set_flags(EVP_MD_CTX *ctx, int flags) {} int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in) { - // |in->digest| may be NULL if this is a signing |EVP_MD_CTX| for, e.g., - // Ed25519 which does not hash with |EVP_MD_CTX|. + // `in->digest` may be NULL if this is a signing `EVP_MD_CTX` for, e.g., + // Ed25519 which does not hash with `EVP_MD_CTX`. if (in == nullptr || (in->pctx == nullptr && in->digest == nullptr)) { OPENSSL_PUT_ERROR(DIGEST, DIGEST_R_INPUT_NOT_INITIALIZED); return 0; @@ -128,9 +140,9 @@ int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in) { void EVP_MD_CTX_move(EVP_MD_CTX *out, EVP_MD_CTX *in) { EVP_MD_CTX_cleanup(out); - // While not guaranteed, |EVP_MD_CTX| is currently safe to move with |memcpy|. + // While not guaranteed, `EVP_MD_CTX` is currently safe to move with `memcpy`. // bssl-crypto currently relies on this, however, so if we change this, we - // need to box the |HMAC_CTX|. (Relying on this is only fine because we assume + // need to box the `HMAC_CTX`. (Relying on this is only fine because we assume // BoringSSL and bssl-crypto will always be updated atomically. We do not // allow any version skew between the two.) OPENSSL_memcpy(out, in, sizeof(EVP_MD_CTX)); @@ -149,14 +161,12 @@ int EVP_MD_CTX_reset(EVP_MD_CTX *ctx) { } int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *engine) { - if (ctx->digest != type) { - assert(type->ctx_size != 0); - assert(type->ctx_size <= sizeof(ctx->md_data)); - ctx->digest = type; - } - - assert(ctx->pctx == nullptr || ctx->pctx_ops != nullptr); + EVP_MD_CTX_cleanup(ctx); + assert(ctx->pctx == nullptr); + assert(type->ctx_size != 0); + assert(type->ctx_size <= sizeof(ctx->md_data)); + ctx->digest = type; ctx->digest->init(ctx); return 1; } diff --git a/third_party/boringssl/src/crypto/fipsmodule/digest/digests.cc.inc b/third_party/boringssl/src/crypto/fipsmodule/digest/digests.cc.inc index a246a5110..23b7409f0 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/digest/digests.cc.inc +++ b/third_party/boringssl/src/crypto/fipsmodule/digest/digests.cc.inc @@ -19,7 +19,6 @@ #include -#include "../../internal.h" #include "../bcm_interface.h" #include "../delocate.h" #include "internal.h" @@ -78,6 +77,7 @@ DEFINE_METHOD_FUNCTION(EVP_MD, EVP_sha224) { out->init = sha224_init; out->update = sha224_update; out->final = sha224_final; + out->squeeze = nullptr; out->block_size = 64; out->ctx_size = sizeof(SHA256_CTX); } @@ -103,6 +103,7 @@ DEFINE_METHOD_FUNCTION(EVP_MD, EVP_sha256) { out->init = sha256_init; out->update = sha256_update; out->final = sha256_final; + out->squeeze = nullptr; out->block_size = 64; out->ctx_size = sizeof(SHA256_CTX); } @@ -127,6 +128,7 @@ DEFINE_METHOD_FUNCTION(EVP_MD, EVP_sha384) { out->init = sha384_init; out->update = sha384_update; out->final = sha384_final; + out->squeeze = nullptr; out->block_size = 128; out->ctx_size = sizeof(SHA512_CTX); } @@ -152,6 +154,7 @@ DEFINE_METHOD_FUNCTION(EVP_MD, EVP_sha512) { out->init = sha512_init; out->update = sha512_update; out->final = sha512_final; + out->squeeze = nullptr; out->block_size = 128; out->ctx_size = sizeof(SHA512_CTX); } @@ -177,6 +180,7 @@ DEFINE_METHOD_FUNCTION(EVP_MD, EVP_sha512_256) { out->init = sha512_256_init; out->update = sha512_256_update; out->final = sha512_256_final; + out->squeeze = nullptr; out->block_size = 128; out->ctx_size = sizeof(SHA512_CTX); } diff --git a/third_party/boringssl/src/crypto/fipsmodule/digest/internal.h b/third_party/boringssl/src/crypto/fipsmodule/digest/internal.h index 1174fcc5f..6b7ca78d0 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/digest/internal.h +++ b/third_party/boringssl/src/crypto/fipsmodule/digest/internal.h @@ -32,18 +32,22 @@ struct env_md_st { // md_size contains the size, in bytes, of the resulting digest. unsigned md_size; - // flags contains the OR of |EVP_MD_FLAG_*| values. + // flags contains the OR of `EVP_MD_FLAG_*` values. uint32_t flags; - // init initialises the state in |ctx->md_data|. + // init initialises the state in `ctx->md_data`. void (*init)(EVP_MD_CTX *ctx); - // update hashes |len| bytes of |data| into the state in |ctx->md_data|. + // update hashes `len` bytes of `data` into the state in `ctx->md_data`. void (*update)(EVP_MD_CTX *ctx, const void *data, size_t count); - // final completes the hash and writes |md_size| bytes of digest to |out|. + // final completes the hash and writes `md_size` bytes of digest to `out`. void (*final)(EVP_MD_CTX *ctx, uint8_t *out); + // squeeze writes the `out_len` bytes of digest to `out`. + // It is only applicable to extendable output functions. + void (*squeeze)(EVP_MD_CTX *ctx, uint8_t *out, size_t out_len); + // block_size contains the hash's native block size. unsigned block_size; @@ -51,15 +55,15 @@ struct env_md_st { unsigned ctx_size; }; -// evp_md_pctx_ops contains function pointers to allow the |pctx| member of -// |EVP_MD_CTX| to be manipulated without breaking layering by calling EVP +// evp_md_pctx_ops contains function pointers to allow the `pctx` member of +// `EVP_MD_CTX` to be manipulated without breaking layering by calling EVP // functions. struct evp_md_pctx_ops { - // free is called when an |EVP_MD_CTX| is being freed and the |pctx| also + // free is called when an `EVP_MD_CTX` is being freed and the `pctx` also // needs to be freed. void (*free)(EVP_PKEY_CTX *pctx); - // dup is called when an |EVP_MD_CTX| is copied and so the |pctx| also needs + // dup is called when an `EVP_MD_CTX` is copied and so the `pctx` also needs // to be copied. EVP_PKEY_CTX *(*dup)(EVP_PKEY_CTX *pctx); }; diff --git a/third_party/boringssl/src/crypto/fipsmodule/digest/md32_common.h b/third_party/boringssl/src/crypto/fipsmodule/digest/md32_common.h index 9e237b2ba..86f21dcde 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/digest/md32_common.h +++ b/third_party/boringssl/src/crypto/fipsmodule/digest/md32_common.h @@ -44,9 +44,9 @@ BSSL_NAMESPACE_BEGIN // any truncation (e.g. 64 for SHA-224 and SHA-256, 128 for SHA-384 and // SHA-512). // -// |h| is the hash state and is updated by a function of type -// |crypto_md32_block_func|. |data| is the partial unprocessed block and has -// |num| bytes. |Nl| and |Nh| maintain the number of bits processed so far. +// `h` is the hash state and is updated by a function of type +// `crypto_md32_block_func`. `data` is the partial unprocessed block and has +// `num` bytes. `Nl` and `Nh` maintain the number of bits processed so far. // // The template parameter is then a traits struct defined as follows: // @@ -61,8 +61,8 @@ BSSL_NAMESPACE_BEGIN // // big or little endian. // static constexpr bool kLengthIsBigEndian = ...; // -// // HashBlocks incorporates |num_blocks| blocks of input from |data| -// // into |state|. It is assumed the caller has sized |state| and |data| +// // HashBlocks incorporates `num_blocks` blocks of input from `data` +// // into `state`. It is assumed the caller has sized `state` and `data` // // for the hash function. // static void HashBlocks(uint32_t *state, const uint8_t *data, // size_t num_blocks) { @@ -73,7 +73,7 @@ BSSL_NAMESPACE_BEGIN // The reason for this formulation is to encourage the compiler to specialize // all the code for the block size and block function. -// crypto_md32_update hashes |in| to |ctx|. +// crypto_md32_update hashes `in` to `ctx`. template inline void crypto_md32_update(typename Traits::HashContext *ctx, Span in) { @@ -98,7 +98,7 @@ inline void crypto_md32_update(typename Traits::HashContext *ctx, Traits::HashBlocks(ctx->h, ctx->data, 1); in = in.subspan(Traits::kBlockSize - n); ctx->num = 0; - // Keep |data| zeroed when unused. + // Keep `data` zeroed when unused. OPENSSL_memset(ctx->data, 0, Traits::kBlockSize); } else { OPENSSL_memcpy(ctx->data + n, in.data(), in.size()); @@ -120,19 +120,19 @@ inline void crypto_md32_update(typename Traits::HashContext *ctx, } // crypto_md32_final incorporates the partial block and trailing length into the -// digest state in |ctx|. The trailing length is encoded in little-endian if -// |is_big_endian| is zero and big-endian otherwise. |data| must be a buffer of -// length |block_size| with the first |*num| bytes containing a partial block. -// |Nh| and |Nl| contain the total number of bits processed. On return, this -// function clears the partial block in |data| and -// |*num|. +// digest state in `ctx`. The trailing length is encoded in little-endian if +// `is_big_endian` is zero and big-endian otherwise. `data` must be a buffer of +// length `block_size` with the first `*num` bytes containing a partial block. +// `Nh` and `Nl` contain the total number of bits processed. On return, this +// function clears the partial block in `data` and +// `*num`. // -// This function does not serialize |h| into a final digest. This is the +// This function does not serialize `h` into a final digest. This is the // responsibility of the caller. template inline void crypto_md32_final(typename Traits::HashContext *ctx) { static_assert(Traits::kBlockSize == sizeof(ctx->data), "block size is wrong"); - // |data| always has room for at least one byte. A full block would have + // `data` always has room for at least one byte. A full block would have // been consumed. size_t n = ctx->num; assert(n < Traits::kBlockSize); diff --git a/third_party/boringssl/src/crypto/fipsmodule/digestsign/digestsign.cc.inc b/third_party/boringssl/src/crypto/fipsmodule/digestsign/digestsign.cc.inc index 5324107b4..a886d46c4 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/digestsign/digestsign.cc.inc +++ b/third_party/boringssl/src/crypto/fipsmodule/digestsign/digestsign.cc.inc @@ -12,9 +12,10 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include +#include #include +#include #include "../../evp/internal.h" #include "../delocate.h" @@ -34,37 +35,34 @@ DEFINE_LOCAL_DATA(struct evp_md_pctx_ops, md_pctx_ops) { out->dup = EVP_PKEY_CTX_dup; } -static int uses_prehash(EVP_MD_CTX *ctx, enum evp_sign_verify_t op) { - return (op == evp_sign) ? (FromOpaque(ctx->pctx)->pmeth->sign != nullptr) - : (FromOpaque(ctx->pctx)->pmeth->verify != nullptr); +static int uses_prehash(EVP_PKEY_CTX *pctx, enum evp_sign_verify_t op) { + return (op == evp_sign) ? (FromOpaque(pctx)->pmeth->sign != nullptr) + : (FromOpaque(pctx)->pmeth->verify != nullptr); } -static int do_sigver_init(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx, - const EVP_MD *type, ENGINE *e, EVP_PKEY *pkey, +static int do_sigver_init(EVP_MD_CTX *ctx, EVP_PKEY_CTX **out_pctx, + const EVP_MD *type, ENGINE *e, const EVP_PKEY *pkey, enum evp_sign_verify_t op) { - if (ctx->pctx == nullptr) { - ctx->pctx = EVP_PKEY_CTX_new(pkey, e); - } - if (ctx->pctx == nullptr) { + UniquePtr pctx(EVP_PKEY_CTX_new(pkey, e)); + if (pctx == nullptr) { return 0; } - ctx->pctx_ops = md_pctx_ops(); if (op == evp_verify) { - if (!EVP_PKEY_verify_init(ctx->pctx)) { + if (!EVP_PKEY_verify_init(pctx.get())) { return 0; } } else { - if (!EVP_PKEY_sign_init(ctx->pctx)) { + if (!EVP_PKEY_sign_init(pctx.get())) { return 0; } } - if (type != nullptr && !EVP_PKEY_CTX_set_signature_md(ctx->pctx, type)) { + if (type != nullptr && !EVP_PKEY_CTX_set_signature_md(pctx.get(), type)) { return 0; } - if (uses_prehash(ctx, op)) { + if (uses_prehash(pctx.get(), op)) { if (type == nullptr) { OPENSSL_PUT_ERROR(EVP, EVP_R_NO_DEFAULT_DIGEST); return 0; @@ -72,26 +70,31 @@ static int do_sigver_init(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx, if (!EVP_DigestInit_ex(ctx, type, e)) { return 0; } + } else { + EVP_MD_CTX_reset(ctx); } - if (pctx) { - *pctx = ctx->pctx; + assert(ctx->pctx == nullptr); + ctx->pctx_ops = md_pctx_ops(); + ctx->pctx = pctx.release(); + if (out_pctx) { + *out_pctx = ctx->pctx; } return 1; } int EVP_DigestSignInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx, const EVP_MD *type, - ENGINE *e, EVP_PKEY *pkey) { + ENGINE *e, const EVP_PKEY *pkey) { return do_sigver_init(ctx, pctx, type, e, pkey, evp_sign); } int EVP_DigestVerifyInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx, - const EVP_MD *type, ENGINE *e, EVP_PKEY *pkey) { + const EVP_MD *type, ENGINE *e, const EVP_PKEY *pkey) { return do_sigver_init(ctx, pctx, type, e, pkey, evp_verify); } int EVP_DigestSignUpdate(EVP_MD_CTX *ctx, const void *data, size_t len) { - if (!uses_prehash(ctx, evp_sign)) { + if (!uses_prehash(ctx->pctx, evp_sign)) { OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); return 0; } @@ -100,7 +103,7 @@ int EVP_DigestSignUpdate(EVP_MD_CTX *ctx, const void *data, size_t len) { } int EVP_DigestVerifyUpdate(EVP_MD_CTX *ctx, const void *data, size_t len) { - if (!uses_prehash(ctx, evp_verify)) { + if (!uses_prehash(ctx->pctx, evp_verify)) { OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); return 0; } @@ -110,7 +113,7 @@ int EVP_DigestVerifyUpdate(EVP_MD_CTX *ctx, const void *data, size_t len) { int EVP_DigestSignFinal(EVP_MD_CTX *ctx, uint8_t *out_sig, size_t *out_sig_len) { - if (!uses_prehash(ctx, evp_sign)) { + if (!uses_prehash(ctx->pctx, evp_sign)) { OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); return 0; } @@ -140,9 +143,8 @@ int EVP_DigestSignFinal(EVP_MD_CTX *ctx, uint8_t *out_sig, } } -int EVP_DigestVerifyFinal(EVP_MD_CTX *ctx, const uint8_t *sig, - size_t sig_len) { - if (!uses_prehash(ctx, evp_verify)) { +int EVP_DigestVerifyFinal(EVP_MD_CTX *ctx, const uint8_t *sig, size_t sig_len) { + if (!uses_prehash(ctx->pctx, evp_verify)) { OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); return 0; } @@ -172,9 +174,9 @@ int EVP_DigestSign(EVP_MD_CTX *ctx, uint8_t *out_sig, size_t *out_sig_len, FIPS_service_indicator_lock_state(); int ret = 0; - if (uses_prehash(ctx, evp_sign)) { - // If |out_sig| is NULL, the caller is only querying the maximum output - // length. |data| should only be incorporated in the final call. + if (uses_prehash(ctx->pctx, evp_sign)) { + // If `out_sig` is NULL, the caller is only querying the maximum output + // length. `data` should only be incorporated in the final call. if (out_sig != nullptr && !EVP_DigestSignUpdate(ctx, data, data_len)) { goto end; } @@ -204,7 +206,7 @@ int EVP_DigestVerify(EVP_MD_CTX *ctx, const uint8_t *sig, size_t sig_len, FIPS_service_indicator_lock_state(); int ret = 0; - if (uses_prehash(ctx, evp_verify)) { + if (uses_prehash(ctx->pctx, evp_verify)) { ret = EVP_DigestVerifyUpdate(ctx, data, len) && EVP_DigestVerifyFinal(ctx, sig, sig_len); goto end; diff --git a/third_party/boringssl/src/crypto/fipsmodule/ec/ec.cc.inc b/third_party/boringssl/src/crypto/fipsmodule/ec/ec.cc.inc index 6d7507fbf..31658ce51 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/ec/ec.cc.inc +++ b/third_party/boringssl/src/crypto/fipsmodule/ec/ec.cc.inc @@ -55,7 +55,7 @@ static void ec_group_init_static_mont(BN_MONT_CTX *mont, size_t num_words, static void ec_group_set_a_minus3(EC_GROUP *group) { const EC_FELEM *one = ec_felem_one(group); - group->a_is_minus3 = 1; + group->a_is_minus3 = true; ec_felem_neg(group, &group->a, one); ec_felem_sub(group, &group->a, &group->a, one); ec_felem_sub(group, &group->a, &group->a, one); @@ -82,8 +82,9 @@ DEFINE_METHOD_FUNCTION(EC_GROUP, EC_group_p224) { out->generator.group = out; ec_group_set_a_minus3(out); - out->has_order = 1; - out->field_greater_than_order = 1; + out->field_is_3_mod_4 = false; + out->has_order = true; + out->field_greater_than_order = true; } DEFINE_METHOD_FUNCTION(EC_GROUP, EC_group_p256) { @@ -113,8 +114,9 @@ DEFINE_METHOD_FUNCTION(EC_GROUP, EC_group_p256) { OPENSSL_memcpy(out->b.words, kP256MontB, sizeof(kP256MontB)); ec_group_set_a_minus3(out); - out->has_order = 1; - out->field_greater_than_order = 1; + out->field_is_3_mod_4 = true; + out->has_order = true; + out->field_greater_than_order = true; } DEFINE_METHOD_FUNCTION(EC_GROUP, EC_group_p384) { @@ -138,8 +140,9 @@ DEFINE_METHOD_FUNCTION(EC_GROUP, EC_group_p384) { OPENSSL_memcpy(out->b.words, kP384MontB, sizeof(kP384MontB)); ec_group_set_a_minus3(out); - out->has_order = 1; - out->field_greater_than_order = 1; + out->field_is_3_mod_4 = true; + out->has_order = true; + out->field_greater_than_order = true; } DEFINE_METHOD_FUNCTION(EC_GROUP, EC_group_p521) { @@ -163,8 +166,9 @@ DEFINE_METHOD_FUNCTION(EC_GROUP, EC_group_p521) { OPENSSL_memcpy(out->b.words, kP521MontB, sizeof(kP521MontB)); ec_group_set_a_minus3(out); - out->has_order = 1; - out->field_greater_than_order = 1; + out->field_is_3_mod_4 = true; + out->has_order = true; + out->field_greater_than_order = true; } EC_GROUP *EC_GROUP_new_curve_GFp(const BIGNUM *p, const BIGNUM *a, @@ -183,7 +187,7 @@ EC_GROUP *EC_GROUP_new_curve_GFp(const BIGNUM *p, const BIGNUM *a, ctx = new_ctx.get(); } - // Historically, |a| and |b| were not required to be fully reduced. + // Historically, `a` and `b` were not required to be fully reduced. // TODO(davidben): Can this be removed? BN_CTXScope scope(ctx); BIGNUM *a_reduced = BN_CTX_get(ctx); @@ -209,10 +213,10 @@ int EC_GROUP_set_generator(EC_GROUP *group, const EC_POINT *generator, const BIGNUM *order, const BIGNUM *cofactor) { if (group->curve_name != NID_undef || group->has_order || generator->group != group) { - // |EC_GROUP_set_generator| may only be used with |EC_GROUP|s returned by - // |EC_GROUP_new_curve_GFp| and may only used once on each group. - // |generator| must have been created from |EC_GROUP_new_curve_GFp|, not a - // copy, so that |generator->group->generator| is set correctly. + // `EC_GROUP_set_generator` may only be used with `EC_GROUP`s returned by + // `EC_GROUP_new_curve_GFp` and may only used once on each group. + // `generator` must have been created from `EC_GROUP_new_curve_GFp`, not a + // copy, so that `generator->group->generator` is set correctly. OPENSSL_PUT_ERROR(EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); return 0; } @@ -231,7 +235,7 @@ int EC_GROUP_set_generator(EC_GROUP *group, const EC_POINT *generator, // Require that p < 2×order. This simplifies some ECDSA operations. // // Note any curve which did not satisfy this must have been invalid or use a - // tiny prime (less than 17). See the proof in |field_element_to_scalar| in + // tiny prime (less than 17). See the proof in `field_element_to_scalar` in // the ECDSA implementation. UniquePtr tmp(BN_new()); if (tmp == nullptr || !BN_lshift1(tmp.get(), order)) { @@ -251,8 +255,8 @@ int EC_GROUP_set_generator(EC_GROUP *group, const EC_POINT *generator, group->field_greater_than_order = BN_cmp(&group->field.N, order) > 0; group->generator.raw.X = affine.X; group->generator.raw.Y = affine.Y; - // |raw.Z| was set to 1 by |EC_GROUP_new_curve_GFp|. - group->has_order = 1; + // `raw.Z` was set to 1 by `EC_GROUP_new_curve_GFp`. + group->has_order = true; return 1; } @@ -303,7 +307,7 @@ EC_GROUP *EC_GROUP_dup(const EC_GROUP *a) { } auto *custom = static_cast(a); - // Groups are logically immutable (but for |EC_GROUP_set_generator| which must + // Groups are logically immutable (but for `EC_GROUP_set_generator` which must // be called early on), so we simply take a reference. ECCustomGroup *group = const_cast(custom); group->UpRefInternal(); @@ -323,8 +327,8 @@ int EC_GROUP_cmp(const EC_GROUP *a, const EC_GROUP *b, BN_CTX *ignored) { return 0; } - // |a| and |b| are both custom curves. We compare the entire curve - // structure. If |a| or |b| is incomplete (due to legacy OpenSSL mistakes, + // `a` and `b` are both custom curves. We compare the entire curve + // structure. If `a` or `b` is incomplete (due to legacy OpenSSL mistakes, // custom curve construction is sadly done in two parts) but otherwise not the // same object, we consider them always unequal. return a->meth != b->meth || // @@ -358,7 +362,7 @@ int EC_GROUP_order_bits(const EC_GROUP *group) { int EC_GROUP_get_cofactor(const EC_GROUP *group, BIGNUM *cofactor, BN_CTX *ctx) { - // All |EC_GROUP|s have cofactor 1. + // All `EC_GROUP`s have cofactor 1. return BN_set_word(cofactor, 1); } @@ -495,7 +499,7 @@ int EC_POINT_cmp(const EC_GROUP *group, const EC_POINT *a, const EC_POINT *b, return -1; } - // Note |EC_POINT_cmp| returns zero for equality and non-zero for inequality. + // Note `EC_POINT_cmp` returns zero for equality and non-zero for inequality. return ec_GFp_simple_points_equal(group, &a->raw, &b->raw) ? 0 : 1; } @@ -548,16 +552,21 @@ int bssl::ec_jacobian_to_affine_batch(const EC_GROUP *group, EC_AFFINE *out, return group->meth->jacobian_to_affine_batch(group, out, in, num); } +void bssl::ec_y_sqr_from_x(const EC_GROUP *group, EC_FELEM *out, + const EC_FELEM *x) { + ec_felem_sqr(group, out, x); // out = x² + ec_felem_add(group, out, out, &group->a); // out = x² + a + ec_felem_mul(group, out, out, x); // out = x³ + ax + ec_felem_add(group, out, out, &group->b); // out = x³ + ax + b +} + int bssl::ec_point_set_affine_coordinates(const EC_GROUP *group, EC_AFFINE *out, const EC_FELEM *x, const EC_FELEM *y) { // Check if the point is on the curve. EC_FELEM lhs, rhs; - ec_felem_sqr(group, &lhs, y); // lhs = y^2 - ec_felem_sqr(group, &rhs, x); // rhs = x^2 - ec_felem_add(group, &rhs, &rhs, &group->a); // rhs = x^2 + a - ec_felem_mul(group, &rhs, &rhs, x); // rhs = x^3 + ax - ec_felem_add(group, &rhs, &rhs, &group->b); // rhs = x^3 + ax + b + ec_felem_sqr(group, &lhs, y); // lhs = y² + ec_y_sqr_from_x(group, &rhs, x); // rhs = x³ + ax + b if (!ec_felem_equal(group, &lhs, &rhs)) { OPENSSL_PUT_ERROR(EC, EC_R_POINT_IS_NOT_ON_CURVE); // In the event of an error, defend against the caller not checking the @@ -576,6 +585,29 @@ int bssl::ec_point_set_affine_coordinates(const EC_GROUP *group, EC_AFFINE *out, return 1; } +int bssl::ec_point_set_compressed_coordinates(const EC_GROUP *group, + EC_AFFINE *out, const EC_FELEM *x, + crypto_word_t y_bit) { + EC_FELEM y2, y; + ec_y_sqr_from_x(group, &y2, x); + if (!ec_felem_sqrt(group, &y, &y2, y_bit)) { + OPENSSL_PUT_ERROR(EC, EC_R_POINT_IS_NOT_ON_CURVE); + // In the event of an error, defend against the caller not checking the + // return value by setting a known safe value. Note this may not be possible + // if the caller is in the process of constructing an arbitrary group and + // the generator is missing. + if (group->has_order) { + out->X = group->generator.raw.X; + out->Y = group->generator.raw.Y; + } + return 0; + } + + out->X = *x; + out->Y = y; + return 1; +} + int EC_POINT_set_affine_coordinates_GFp(const EC_GROUP *group, EC_POINT *point, const BIGNUM *x, const BIGNUM *y, BN_CTX *ctx) { @@ -610,6 +642,28 @@ int EC_POINT_set_affine_coordinates(const EC_GROUP *group, EC_POINT *point, return EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx); } +int EC_POINT_set_compressed_coordinates_GFp(const EC_GROUP *group, + EC_POINT *point, const BIGNUM *x, + int y_bit, BN_CTX *ctx) { + if (EC_GROUP_cmp(group, point->group, nullptr) != 0) { + OPENSSL_PUT_ERROR(EC, EC_R_INCOMPATIBLE_OBJECTS); + return 0; + } + + EC_FELEM x_felem; + EC_AFFINE affine; + if (!ec_bignum_to_felem(group, &x_felem, x) || + !ec_point_set_compressed_coordinates(group, &affine, &x_felem, y_bit)) { + // In the event of an error, defend against the caller not checking the + // return value by setting a known safe value. + ec_set_to_safe_point(group, &point->raw); + return 0; + } + + ec_affine_to_jacobian(group, &point->raw, &affine); + return 1; +} + int EC_POINT_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a, const EC_POINT *b, BN_CTX *ctx) { if (EC_GROUP_cmp(group, r->group, nullptr) != 0 || @@ -661,7 +715,7 @@ static int arbitrary_bignum_to_scalar(const EC_GROUP *group, EC_SCALAR *out, int bssl::ec_point_mul_no_self_test(const EC_GROUP *group, EC_POINT *r, const BIGNUM *g_scalar, const EC_POINT *p, const BIGNUM *p_scalar, BN_CTX *ctx) { - // Previously, this function set |r| to the point at infinity if there was + // Previously, this function set `r` to the point at infinity if there was // nothing to multiply. But, nobody should be calling this function with // nothing to multiply in the first place. if ((g_scalar == nullptr && p_scalar == nullptr) || @@ -685,8 +739,8 @@ int bssl::ec_point_mul_no_self_test(const EC_GROUP *group, EC_POINT *r, ctx = new_ctx.get(); } - // If both |g_scalar| and |p_scalar| are non-NULL, - // |ec_point_mul_scalar_public| would share the doublings between the two + // If both `g_scalar` and `p_scalar` are non-NULL, + // `ec_point_mul_scalar_public` would share the doublings between the two // products, which would be more efficient. However, we conservatively assume // the caller needs a constant-time operation. (ECDSA verification does not // use this function.) @@ -768,7 +822,7 @@ int bssl::ec_point_mul_scalar(const EC_GROUP *group, EC_JACOBIAN *r, // Check the result is on the curve to defend against fault attacks or bugs. // This has negligible cost compared to the multiplication. - if (!ec_GFp_simple_is_on_curve(group, r)) { + if (!constant_time_declassify_int(ec_GFp_simple_is_on_curve(group, r))) { OPENSSL_PUT_ERROR(EC, ERR_R_INTERNAL_ERROR); return 0; } @@ -810,7 +864,7 @@ int bssl::ec_point_mul_scalar_batch( // Check the result is on the curve to defend against fault attacks or bugs. // This has negligible cost compared to the multiplication. - if (!ec_GFp_simple_is_on_curve(group, r)) { + if (!constant_time_declassify_int(ec_GFp_simple_is_on_curve(group, r))) { OPENSSL_PUT_ERROR(EC, ERR_R_INTERNAL_ERROR); return 0; } @@ -841,7 +895,7 @@ int bssl::ec_point_mul_scalar_precomp( // Check the result is on the curve to defend against fault attacks or bugs. // This has negligible cost compared to the multiplication. - if (!ec_GFp_simple_is_on_curve(group, r)) { + if (!constant_time_declassify_int(ec_GFp_simple_is_on_curve(group, r))) { OPENSSL_PUT_ERROR(EC, ERR_R_INTERNAL_ERROR); return 0; } @@ -891,7 +945,7 @@ int bssl::ec_get_x_coordinate_as_scalar(const EC_GROUP *group, EC_SCALAR *out, // order. These may not have the same size. However, we must have p < 2×order, // assuming p is not tiny (p >= 17). // - // Thus |bytes| will fit in |order.width + 1| words, and we can reduce by + // Thus `bytes` will fit in `order.width + 1` words, and we can reduce by // performing at most one subtraction. // // Proof: We only work with prime order curves, so the number of points on @@ -905,7 +959,7 @@ int bssl::ec_get_x_coordinate_as_scalar(const EC_GROUP *group, EC_SCALAR *out, // p < 2×order // // Additionally, one can manually check this property for built-in curves. It - // is enforced for legacy custom curves in |EC_GROUP_set_generator|. + // is enforced for legacy custom curves in `EC_GROUP_set_generator`. const BIGNUM *order = EC_GROUP_get0_order(group); BN_ULONG words[EC_MAX_WORDS + 1] = {0}; bn_big_endian_to_words(words, order->width + 1, bytes, len); @@ -953,7 +1007,7 @@ int EC_GROUP_get_asn1_flag(const EC_GROUP *group) { const EC_METHOD *EC_GROUP_method_of(const EC_GROUP *group) { // This function exists purely to give callers a way to call - // |EC_METHOD_get_field_type|. cryptography.io crashes if |EC_GROUP_method_of| + // `EC_METHOD_get_field_type`. cryptography.io crashes if `EC_GROUP_method_of` // returns NULL, so return some other garbage pointer. return (const EC_METHOD *)0x12340000; } diff --git a/third_party/boringssl/src/crypto/fipsmodule/ec/ec_key.cc.inc b/third_party/boringssl/src/crypto/fipsmodule/ec/ec_key.cc.inc index e666baf1c..078e541ab 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/ec/ec_key.cc.inc +++ b/third_party/boringssl/src/crypto/fipsmodule/ec/ec_key.cc.inc @@ -26,7 +26,6 @@ #include "../../internal.h" #include "../../mem_internal.h" -#include "../bcm_interface.h" #include "../delocate.h" #include "../ecdsa/internal.h" #include "../service_indicator/internal.h" @@ -147,6 +146,13 @@ int EC_KEY_up_ref(EC_KEY *r) { return 1; } +EC_KEY *EC_KEY_dup_ref(const EC_KEY *key) { + if (key == nullptr) { + return nullptr; + } + return FromOpaque(key)->DupRefInternal(); +} + int EC_KEY_is_opaque(const EC_KEY *key) { auto *impl = FromOpaque(key); return impl->ecdsa_meth && (impl->ecdsa_meth->flags & ECDSA_FLAG_OPAQUE); @@ -160,7 +166,7 @@ const EC_GROUP *EC_KEY_get0_group(const EC_KEY *key) { int EC_KEY_set_group(EC_KEY *key, const EC_GROUP *group) { auto *impl = FromOpaque(key); - // If |impl| already has a group, it is an error to switch to another one. + // If `impl` already has a group, it is an error to switch to another one. if (impl->group != nullptr) { if (EC_GROUP_cmp(impl->group, group, nullptr) != 0) { OPENSSL_PUT_ERROR(EC, EC_R_GROUP_MISMATCH); @@ -285,7 +291,7 @@ int EC_KEY_check_key(const EC_KEY *eckey) { OPENSSL_PUT_ERROR(EC, ERR_R_EC_LIB); return 0; } - // Leaking this comparison only leaks whether |eckey|'s public key was + // Leaking this comparison only leaks whether `eckey`'s public key was // correct. if (!constant_time_declassify_int(ec_GFp_simple_points_equal( impl->group, &point, &impl->pub_key->raw))) { @@ -480,7 +486,7 @@ int EC_KEY_generate_key(EC_KEY *key) { // The public key is derived from the private key, but it is public. // - // TODO(crbug.com/boringssl/677): This isn't quite right. While |pub_key| + // TODO(crbug.com/boringssl/677): This isn't quite right. While `pub_key` // represents a public point, it is still in Jacobian form and the exact // Jacobian representation is secret. We need to make it affine first. See // discussion in the bug. diff --git a/third_party/boringssl/src/crypto/fipsmodule/ec/ec_montgomery.cc.inc b/third_party/boringssl/src/crypto/fipsmodule/ec/ec_montgomery.cc.inc index 4f1f1d38d..bb7696e8d 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/ec/ec_montgomery.cc.inc +++ b/third_party/boringssl/src/crypto/fipsmodule/ec/ec_montgomery.cc.inc @@ -41,8 +41,8 @@ static int ec_GFp_mont_point_get_affine_coordinates(const EC_GROUP *group, return 0; } - // Transform (X, Y, Z) into (x, y) := (X/Z^2, Y/Z^3). Note the check above - // ensures |point->Z| is non-zero, so the inverse always exists. + // Transform (X, Y, Z) into (x, y) := (X/Z², Y/Z³). Note the check above + // ensures `point->Z` is non-zero, so the inverse always exists. EC_FELEM z1, z2; ec_GFp_mont_felem_inv0(group, &z2, &point->Z); ec_felem_sqr(group, &z1, &z2); @@ -67,7 +67,7 @@ static int ec_GFp_mont_jacobian_to_affine_batch(const EC_GROUP *group, return 1; } - // Compute prefix products of all Zs. Use |out[i].X| as scratch space + // Compute prefix products of all Zs. Use `out[i].X` as scratch space // to store these values. out[0].X = in[0].Z; for (size_t i = 1; i < num; i++) { @@ -84,7 +84,7 @@ static int ec_GFp_mont_jacobian_to_affine_batch(const EC_GROUP *group, EC_FELEM zinvprod; ec_GFp_mont_felem_inv0(group, &zinvprod, &out[num - 1].X); for (size_t i = num - 1; i < num; i--) { - // Our loop invariant is that |zinvprod| is Z0^-1 * Z1^-1 * ... * Zi^-1. + // Our loop invariant is that `zinvprod` is Z0^-1 * Z1^-1 * ... * Zi^-1. // Recover Zi^-1 by multiplying by the previous product. EC_FELEM zinv, zinv2; if (i == 0) { @@ -174,7 +174,7 @@ void bssl::ec_GFp_mont_add(const EC_GROUP *group, EC_JACOBIAN *out, BN_ULONG yneq = ec_felem_non_zero_mask(group, &r); - // This case will never occur in the constant-time |ec_GFp_mont_mul|. + // This case will never occur in the constant-time `ec_GFp_mont_mul`. BN_ULONG is_nontrivial_double = ~xneq & ~yneq & z1nz & z2nz; if (constant_time_declassify_w(is_nontrivial_double)) { ec_GFp_mont_dbl(group, out, a); @@ -226,9 +226,9 @@ void bssl::ec_GFp_mont_dbl(const EC_GROUP *group, EC_JACOBIAN *r, // // EC_FELEM delta, gamma, beta, ftmp, ftmp2, tmptmp, alpha, fourbeta; - // delta = z^2 + // delta = z² ec_felem_sqr(group, &delta, &a->Z); - // gamma = y^2 + // gamma = y² ec_felem_sqr(group, &gamma, &a->Y); // beta = x*gamma ec_felem_mul(group, &beta, &a->X, &gamma); @@ -241,20 +241,20 @@ void bssl::ec_GFp_mont_dbl(const EC_GROUP *group, EC_JACOBIAN *r, ec_felem_add(group, &ftmp2, &ftmp2, &tmptmp); ec_felem_mul(group, &alpha, &ftmp, &ftmp2); - // x' = alpha^2 - 8*beta + // x' = alpha² - 8*beta ec_felem_sqr(group, &r->X, &alpha); ec_felem_add(group, &fourbeta, &beta, &beta); ec_felem_add(group, &fourbeta, &fourbeta, &fourbeta); ec_felem_add(group, &tmptmp, &fourbeta, &fourbeta); ec_felem_sub(group, &r->X, &r->X, &tmptmp); - // z' = (y + z)^2 - gamma - delta + // z' = (y + z)² - gamma - delta ec_felem_add(group, &delta, &gamma, &delta); ec_felem_add(group, &ftmp, &a->Y, &a->Z); ec_felem_sqr(group, &r->Z, &ftmp); ec_felem_sub(group, &r->Z, &r->Z, &delta); - // y' = alpha*(4*beta - x') - 8*gamma^2 + // y' = alpha*(4*beta - x') - 8*gamma² ec_felem_sub(group, &r->Y, &fourbeta, &r->X); ec_felem_add(group, &gamma, &gamma, &gamma); ec_felem_sqr(group, &gamma, &gamma); @@ -274,7 +274,7 @@ void bssl::ec_GFp_mont_dbl(const EC_GROUP *group, EC_JACOBIAN *r, ec_felem_sqr(group, &yyyy, &yy); ec_felem_sqr(group, &zz, &a->Z); - // s = 2*((x_in + yy)^2 - xx - yyyy) + // s = 2*((x_in + yy)² - xx - yyyy) EC_FELEM s; ec_felem_add(group, &s, &a->X, &yy); ec_felem_sqr(group, &s, &s); @@ -282,7 +282,7 @@ void bssl::ec_GFp_mont_dbl(const EC_GROUP *group, EC_JACOBIAN *r, ec_felem_sub(group, &s, &s, &yyyy); ec_felem_add(group, &s, &s, &s); - // m = 3*xx + a*zz^2 + // m = 3*xx + a*zz² EC_FELEM m; ec_felem_sqr(group, &m, &zz); ec_felem_mul(group, &m, &group->a, &m); @@ -290,12 +290,12 @@ void bssl::ec_GFp_mont_dbl(const EC_GROUP *group, EC_JACOBIAN *r, ec_felem_add(group, &m, &m, &xx); ec_felem_add(group, &m, &m, &xx); - // x_out = m^2 - 2*s + // x_out = m² - 2*s ec_felem_sqr(group, &r->X, &m); ec_felem_sub(group, &r->X, &r->X, &s); ec_felem_sub(group, &r->X, &r->X, &s); - // z_out = (y_in + z_in)^2 - yy - zz + // z_out = (y_in + z_in)² - yy - zz ec_felem_add(group, &r->Z, &a->Y, &a->Z); ec_felem_sqr(group, &r->Z, &r->Z); ec_felem_sub(group, &r->Z, &r->Z, &yy); @@ -325,8 +325,8 @@ static int ec_GFp_mont_cmp_x_coordinate(const EC_GROUP *group, return 0; } - // We wish to compare X/Z^2 with r. This is equivalent to comparing X with - // r*Z^2. Note that X and Z are represented in Montgomery form, while r is + // We wish to compare X/Z² with r. This is equivalent to comparing X with + // r⋅Z². Note that X and Z are represented in Montgomery form, while r is // not. EC_FELEM r_Z2, Z2_mont, X; ec_felem_sqr(group, &Z2_mont, &p->Z); @@ -341,13 +341,13 @@ static int ec_GFp_mont_cmp_x_coordinate(const EC_GROUP *group, // During signing the x coefficient is reduced modulo the group order. // Therefore there is a small possibility, less than 1/2^128, that group_order - // < p.x < P. in that case we need not only to compare against |r| but also to + // < p.x < P. in that case we need not only to compare against `r` but also to // compare against r+group_order. BN_ULONG carry = bn_add_words(r_Z2.words, r->words, group->order.N.d, group->field.N.width); if (carry == 0 && bn_less_than_words(r_Z2.words, group->field.N.d, group->field.N.width)) { - // r + group_order < p, so compare (r + group_order) * Z^2 against X. + // r + group_order < p, so compare (r + group_order) ⋅ Z² against X. ec_felem_mul(group, &r_Z2, &r_Z2, &Z2_mont); if (ec_felem_equal(group, &r_Z2, &X)) { return 1; diff --git a/third_party/boringssl/src/crypto/fipsmodule/ec/felem.cc.inc b/third_party/boringssl/src/crypto/fipsmodule/ec/felem.cc.inc index c634cc1dd..388f7fe2d 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/ec/felem.cc.inc +++ b/third_party/boringssl/src/crypto/fipsmodule/ec/felem.cc.inc @@ -12,6 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +#include #include #include #include @@ -23,15 +24,14 @@ #include "internal.h" -using namespace bssl; +BSSL_NAMESPACE_BEGIN -const EC_FELEM *bssl::ec_felem_one(const EC_GROUP *group) { +const EC_FELEM *ec_felem_one(const EC_GROUP *group) { // We reuse generator.Z as a cache for 1 in the field. return &group->generator.raw.Z; } -int bssl::ec_bignum_to_felem(const EC_GROUP *group, EC_FELEM *out, - const BIGNUM *in) { +int ec_bignum_to_felem(const EC_GROUP *group, EC_FELEM *out, const BIGNUM *in) { uint8_t bytes[EC_MAX_BYTES]; size_t len = BN_num_bytes(&group->field.N); assert(sizeof(bytes) >= len); @@ -44,16 +44,15 @@ int bssl::ec_bignum_to_felem(const EC_GROUP *group, EC_FELEM *out, return ec_felem_from_bytes(group, out, bytes, len); } -int bssl::ec_felem_to_bignum(const EC_GROUP *group, BIGNUM *out, - const EC_FELEM *in) { +int ec_felem_to_bignum(const EC_GROUP *group, BIGNUM *out, const EC_FELEM *in) { uint8_t bytes[EC_MAX_BYTES]; size_t len; ec_felem_to_bytes(group, bytes, &len, in); return BN_bin2bn(bytes, len, out) != nullptr; } -void bssl::ec_felem_to_bytes(const EC_GROUP *group, uint8_t *out, - size_t *out_len, const EC_FELEM *in) { +void ec_felem_to_bytes(const EC_GROUP *group, uint8_t *out, size_t *out_len, + const EC_FELEM *in) { EC_FELEM tmp; ec_felem_from_montgomery(group, &tmp, in); size_t len = BN_num_bytes(&group->field.N); @@ -61,8 +60,8 @@ void bssl::ec_felem_to_bytes(const EC_GROUP *group, uint8_t *out, *out_len = len; } -int bssl::ec_felem_from_bytes(const EC_GROUP *group, EC_FELEM *out, - const uint8_t *in, size_t len) { +int ec_felem_from_bytes(const EC_GROUP *group, EC_FELEM *out, const uint8_t *in, + size_t len) { if (len != BN_num_bytes(&group->field.N)) { OPENSSL_PUT_ERROR(EC, EC_R_DECODE_ERROR); return 0; @@ -78,8 +77,25 @@ int bssl::ec_felem_from_bytes(const EC_GROUP *group, EC_FELEM *out, return 1; } -void bssl::ec_felem_neg(const EC_GROUP *group, EC_FELEM *out, - const EC_FELEM *a) { +crypto_word_t ec_felem_from_bytes_or_placeholder(const EC_GROUP *group, + EC_FELEM *out, + const uint8_t *in, + size_t len) { + const BIGNUM *field = &group->field.N; + BSSL_CHECK(len == BN_num_bytes(field)); + bn_big_endian_to_words(out->words, field->width, in, len); + BN_ULONG ok = + BN_ULONG{0} - bn_less_than_words(out->words, field->d, field->width); + // Replace `out->words` with an arbitrary valid field element if out of range. + // This avoids breaking preconditions of later operations. + for (int i = 0; i < field->width; i++) { + out->words[i] &= ok; + } + ec_felem_to_montgomery(group, out, out); + return ok; +} + +void ec_felem_neg(const EC_GROUP *group, EC_FELEM *out, const EC_FELEM *a) { // -a is zero if a is zero and p-a otherwise. BN_ULONG mask = ec_felem_non_zero_mask(group, a); BN_ULONG borrow = bn_sub_words(out->words, group->field.N.d, a->words, @@ -91,22 +107,21 @@ void bssl::ec_felem_neg(const EC_GROUP *group, EC_FELEM *out, } } -void bssl::ec_felem_add(const EC_GROUP *group, EC_FELEM *out, const EC_FELEM *a, - const EC_FELEM *b) { +void ec_felem_add(const EC_GROUP *group, EC_FELEM *out, const EC_FELEM *a, + const EC_FELEM *b) { EC_FELEM tmp; bn_mod_add_words(out->words, a->words, b->words, group->field.N.d, tmp.words, group->field.N.width); } -void bssl::ec_felem_sub(const EC_GROUP *group, EC_FELEM *out, const EC_FELEM *a, - const EC_FELEM *b) { +void ec_felem_sub(const EC_GROUP *group, EC_FELEM *out, const EC_FELEM *a, + const EC_FELEM *b) { EC_FELEM tmp; bn_mod_sub_words(out->words, a->words, b->words, group->field.N.d, tmp.words, group->field.N.width); } -BN_ULONG bssl::ec_felem_non_zero_mask(const EC_GROUP *group, - const EC_FELEM *a) { +BN_ULONG ec_felem_non_zero_mask(const EC_GROUP *group, const EC_FELEM *a) { BN_ULONG mask = 0; for (int i = 0; i < group->field.N.width; i++) { mask |= a->words[i]; @@ -114,43 +129,42 @@ BN_ULONG bssl::ec_felem_non_zero_mask(const EC_GROUP *group, return ~constant_time_is_zero_w(mask); } -void bssl::ec_felem_select(const EC_GROUP *group, EC_FELEM *out, BN_ULONG mask, - const EC_FELEM *a, const EC_FELEM *b) { +void ec_felem_select(const EC_GROUP *group, EC_FELEM *out, BN_ULONG mask, + const EC_FELEM *a, const EC_FELEM *b) { bn_select_words(out->words, mask, a->words, b->words, group->field.N.width); } -int bssl::ec_felem_equal(const EC_GROUP *group, const EC_FELEM *a, - const EC_FELEM *b) { +int ec_felem_equal(const EC_GROUP *group, const EC_FELEM *a, + const EC_FELEM *b) { return CRYPTO_memcmp(a->words, b->words, group->field.N.width * sizeof(BN_ULONG)) == 0; } -void bssl::ec_felem_mul(const EC_GROUP *group, EC_FELEM *out, const EC_FELEM *a, - const EC_FELEM *b) { +void ec_felem_mul(const EC_GROUP *group, EC_FELEM *out, const EC_FELEM *a, + const EC_FELEM *b) { bn_mod_mul_montgomery_small(out->words, a->words, b->words, group->field.N.width, &group->field); } -void bssl::ec_felem_sqr(const EC_GROUP *group, EC_FELEM *out, - const EC_FELEM *a) { +void ec_felem_sqr(const EC_GROUP *group, EC_FELEM *out, const EC_FELEM *a) { bn_mod_mul_montgomery_small(out->words, a->words, a->words, group->field.N.width, &group->field); } -void bssl::ec_felem_to_montgomery(const EC_GROUP *group, EC_FELEM *out, - const EC_FELEM *a) { +void ec_felem_to_montgomery(const EC_GROUP *group, EC_FELEM *out, + const EC_FELEM *a) { bn_to_montgomery_small(out->words, a->words, group->field.N.width, &group->field); } -void bssl::ec_felem_from_montgomery(const EC_GROUP *group, EC_FELEM *out, - const EC_FELEM *a) { +void ec_felem_from_montgomery(const EC_GROUP *group, EC_FELEM *out, + const EC_FELEM *a) { bn_from_montgomery_small(out->words, group->field.N.width, a->words, group->field.N.width, &group->field); } -void bssl::ec_felem_reduce(const EC_GROUP *group, EC_FELEM *out, - const BN_ULONG *words, size_t num) { +void ec_felem_reduce(const EC_GROUP *group, EC_FELEM *out, + const BN_ULONG *words, size_t num) { // Convert "from" Montgomery form so the value is reduced mod p. bn_from_montgomery_small(out->words, group->field.N.width, words, num, &group->field); @@ -160,8 +174,77 @@ void bssl::ec_felem_reduce(const EC_GROUP *group, EC_FELEM *out, ec_felem_to_montgomery(group, out, out); } -void bssl::ec_felem_exp(const EC_GROUP *group, EC_FELEM *out, const EC_FELEM *a, - const BN_ULONG *exp, size_t num_exp) { +void ec_felem_exp(const EC_GROUP *group, EC_FELEM *out, const EC_FELEM *a, + const BN_ULONG *exp, size_t num_exp) { bn_mod_exp_mont_small(out->words, a->words, group->field.N.width, exp, num_exp, &group->field); } + +// adjust_y negates `y` if its LSB (when converted out of Montgomery form) was +// not `y_bit`. +static void adjust_y(const EC_GROUP *group, EC_FELEM *y, crypto_word_t y_bit) { + EC_FELEM y_from_mont, neg_y; + // `y` is stored in the Montgomery domain. Convert back out to sample the LSB. + ec_felem_from_montgomery(group, &y_from_mont, y); + BN_ULONG lsb_mismatch = (y_bit ^ y_from_mont.words[0]) & 1; + lsb_mismatch = 0u - lsb_mismatch; // All ones or all zeros. + ec_felem_neg(group, &neg_y, y); + ec_felem_select(group, y, lsb_mismatch, &neg_y, y); +} + +int ec_felem_sqrt(const EC_GROUP *group, EC_FELEM *out, const EC_FELEM *a, + crypto_word_t y_bit) { + if (group->field_is_3_mod_4) { + if (!ec_felem_sqrt_secret(group, out, a, y_bit)) { + OPENSSL_PUT_ERROR(EC, EC_R_INVALID_COMPRESSED_POINT); + return 0; + } + return 1; + } + + // Curves that are not 3 mod 4 are messy to implement modular square root. For + // now, fall back to BIGNUM. This case comes up for P-224 and custom curves. + // P-224's prime is particularly inconvenient for compressed coordinates. See + // https://cr.yp.to/papers/sqroot.pdf + UniquePtr ctx(BN_CTX_new()); + UniquePtr bn(BN_new()); + if (ctx == nullptr || bn == nullptr || + !ec_felem_to_bignum(group, bn.get(), a)) { + return 0; + } + + UniquePtr sqrt( + BN_mod_sqrt(nullptr, bn.get(), &group->field.N, ctx.get())); + if (sqrt == nullptr || !ec_bignum_to_felem(group, out, sqrt.get())) { + return 0; + } + + adjust_y(group, out, y_bit); + return 1; +} + +crypto_word_t ec_felem_sqrt_secret(const EC_GROUP *group, EC_FELEM *out, + const EC_FELEM *a, crypto_word_t y_bit) { + BSSL_CHECK(group->field_is_3_mod_4); + + // If `a` is a square, then a^(p+1)/4 is a square root. By Euler's criterion, + // a^(p-1)/2 = 1 for squares, so a^(p+1)/2 = a. p is 3 mod 4, so that exponent + // is even, so we can halve it to find a square root. + const size_t num_exp = group->field.N.width; + BN_ULONG exp[EC_MAX_WORDS]; + bn_rshift_words(exp, group->field.N.d, 2, num_exp); // exp = (p-3)/4 + bn_add_carry_words(exp, exp, /*carry=*/1, num_exp); // exp = (p+1)/4 + ec_felem_exp(group, out, a, exp, num_exp); // out = sqrt(a) + + // Pick the correct (possible) square root. + adjust_y(group, out, y_bit); + + // Check `out^2 = a`. If `a` had a square root, `out` must be one and the + // check will pass. If not, the check cannot pass. + EC_FELEM check; + ec_felem_sqr(group, &check, out); // check = out^2 + ec_felem_sub(group, &check, &check, a); // check = out^2 - a + return ~ec_felem_non_zero_mask(group, &check); +} + +BSSL_NAMESPACE_END diff --git a/third_party/boringssl/src/crypto/fipsmodule/ec/internal.h b/third_party/boringssl/src/crypto/fipsmodule/ec/internal.h index abcb77e75..1c92f304e 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/ec/internal.h +++ b/third_party/boringssl/src/crypto/fipsmodule/ec/internal.h @@ -50,189 +50,217 @@ static_assert(EC_MAX_WORDS <= BN_SMALL_MAX_WORDS, // Scalars. // An EC_SCALAR is an integer fully reduced modulo the order. Only the first -// |order->width| words are used. An |EC_SCALAR| is specific to an |EC_GROUP| +// `order->width` words are used. An `EC_SCALAR` is specific to an `EC_GROUP` // and must not be mixed between groups. typedef struct { BN_ULONG words[EC_MAX_WORDS]; } EC_SCALAR; -// ec_bignum_to_scalar converts |in| to an |EC_SCALAR| and writes it to -// |*out|. It returns one on success and zero if |in| is out of range. +// ec_bignum_to_scalar converts `in` to an `EC_SCALAR` and writes it to +// `*out`. It returns one on success and zero if `in` is out of range. int ec_bignum_to_scalar(const EC_GROUP *group, EC_SCALAR *out, const BIGNUM *in); -// ec_scalar_to_bytes serializes |in| as a big-endian bytestring to |out| and -// sets |*out_len| to the number of bytes written. The number of bytes written -// is |BN_num_bytes(&group->order)|, which is at most |EC_MAX_BYTES|. +// ec_scalar_to_bytes serializes `in` as a big-endian bytestring to `out` and +// sets `*out_len` to the number of bytes written. The number of bytes written +// is `BN_num_bytes(&group->order)`, which is at most `EC_MAX_BYTES`. void ec_scalar_to_bytes(const EC_GROUP *group, uint8_t *out, size_t *out_len, const EC_SCALAR *in); -// ec_scalar_from_bytes deserializes |in| and stores the resulting scalar over -// group |group| to |out|. It returns one on success and zero if |in| is +// ec_scalar_from_bytes deserializes `in` and stores the resulting scalar over +// group `group` to `out`. It returns one on success and zero if `in` is // invalid. int ec_scalar_from_bytes(const EC_GROUP *group, EC_SCALAR *out, const uint8_t *in, size_t len); -// ec_scalar_reduce sets |out| to |words|, reduced modulo the group order. -// |words| must be less than order^2. |num| must be at most twice the width of -// group order. This function treats |words| as secret. +// ec_scalar_reduce sets `out` to `words`, reduced modulo the group order. +// `words` must be less than order². `num` must be at most twice the width of +// group order. This function treats `words` as secret. void ec_scalar_reduce(const EC_GROUP *group, EC_SCALAR *out, const BN_ULONG *words, size_t num); -// ec_random_nonzero_scalar sets |out| to a uniformly selected random value from -// zero to |group->order| - 1. It returns one on success and zero on error. +// ec_random_nonzero_scalar sets `out` to a uniformly selected random value from +// zero to `group->order` - 1. It returns one on success and zero on error. int ec_random_scalar(const EC_GROUP *group, EC_SCALAR *out, const uint8_t additional_data[32]); -// ec_random_nonzero_scalar sets |out| to a uniformly selected random value from -// 1 to |group->order| - 1. It returns one on success and zero on error. +// ec_random_nonzero_scalar sets `out` to a uniformly selected random value from +// 1 to `group->order` - 1. It returns one on success and zero on error. int ec_random_nonzero_scalar(const EC_GROUP *group, EC_SCALAR *out, const uint8_t additional_data[32]); -// ec_scalar_equal_vartime returns one if |a| and |b| are equal and zero +// ec_scalar_equal_vartime returns one if `a` and `b` are equal and zero // otherwise. Both values are treated as public. int ec_scalar_equal_vartime(const EC_GROUP *group, const EC_SCALAR *a, const EC_SCALAR *b); -// ec_scalar_is_zero returns one if |a| is zero and zero otherwise. +// ec_scalar_is_zero returns one if `a` is zero and zero otherwise. int ec_scalar_is_zero(const EC_GROUP *group, const EC_SCALAR *a); -// ec_scalar_add sets |r| to |a| + |b|. +// ec_scalar_add sets `r` to `a` + `b`. void ec_scalar_add(const EC_GROUP *group, EC_SCALAR *r, const EC_SCALAR *a, const EC_SCALAR *b); -// ec_scalar_sub sets |r| to |a| - |b|. +// ec_scalar_sub sets `r` to `a` - `b`. void ec_scalar_sub(const EC_GROUP *group, EC_SCALAR *r, const EC_SCALAR *a, const EC_SCALAR *b); -// ec_scalar_neg sets |r| to -|a|. +// ec_scalar_neg sets `r` to -`a`. void ec_scalar_neg(const EC_GROUP *group, EC_SCALAR *r, const EC_SCALAR *a); -// ec_scalar_to_montgomery sets |r| to |a| in Montgomery form. +// ec_scalar_to_montgomery sets `r` to `a` in Montgomery form. void ec_scalar_to_montgomery(const EC_GROUP *group, EC_SCALAR *r, const EC_SCALAR *a); -// ec_scalar_to_montgomery sets |r| to |a| converted from Montgomery form. +// ec_scalar_to_montgomery sets `r` to `a` converted from Montgomery form. void ec_scalar_from_montgomery(const EC_GROUP *group, EC_SCALAR *r, const EC_SCALAR *a); -// ec_scalar_mul_montgomery sets |r| to |a| * |b| where inputs and outputs are +// ec_scalar_mul_montgomery sets `r` to `a` * `b` where inputs and outputs are // in Montgomery form. void ec_scalar_mul_montgomery(const EC_GROUP *group, EC_SCALAR *r, const EC_SCALAR *a, const EC_SCALAR *b); -// ec_scalar_inv0_montgomery sets |r| to |a|^-1 where inputs and outputs are in -// Montgomery form. If |a| is zero, |r| is set to zero. +// ec_scalar_inv0_montgomery sets `r` to `a`^-1 where inputs and outputs are in +// Montgomery form. If `a` is zero, `r` is set to zero. void ec_scalar_inv0_montgomery(const EC_GROUP *group, EC_SCALAR *r, const EC_SCALAR *a); -// ec_scalar_to_montgomery_inv_vartime sets |r| to |a|^-1 R. That is, it takes -// in |a| not in Montgomery form and computes the inverse in Montgomery form. It -// returns one on success and zero if |a| has no inverse. This function assumes -// |a| is public and may leak information about it via timing. +// ec_scalar_to_montgomery_inv_vartime sets `r` to `a`^-1 R. That is, it takes +// in `a` not in Montgomery form and computes the inverse in Montgomery form. It +// returns one on success and zero if `a` has no inverse. This function assumes +// `a` is public and may leak information about it via timing. // -// Note this is not the same operation as |ec_scalar_inv0_montgomery|. +// Note this is not the same operation as `ec_scalar_inv0_montgomery`. int ec_scalar_to_montgomery_inv_vartime(const EC_GROUP *group, EC_SCALAR *r, const EC_SCALAR *a); -// ec_scalar_select, in constant time, sets |out| to |a| if |mask| is all ones -// and |b| if |mask| is all zeros. +// ec_scalar_select, in constant time, sets `out` to `a` if `mask` is all ones +// and `b` if `mask` is all zeros. void ec_scalar_select(const EC_GROUP *group, EC_SCALAR *out, BN_ULONG mask, const EC_SCALAR *a, const EC_SCALAR *b); // Field elements. -// An EC_FELEM represents a field element. Only the first |field->width| words -// are used. An |EC_FELEM| is specific to an |EC_GROUP| and must not be mixed +// An EC_FELEM represents a field element. Only the first `field->width` words +// are used. An `EC_FELEM` is specific to an `EC_GROUP` and must not be mixed // between groups. Unless otherwise stated, all inputs and outputs are in // Montgomery form. typedef struct { BN_ULONG words[EC_MAX_WORDS]; } EC_FELEM; -// ec_felem_one returns one in |group|'s field. +// ec_felem_one returns one in `group`'s field. const EC_FELEM *ec_felem_one(const EC_GROUP *group); -// ec_bignum_to_felem converts |in| to an |EC_FELEM|. It returns one on success -// and zero if |in| is out of range. +// ec_bignum_to_felem converts `in` to an `EC_FELEM`. It returns one on success +// and zero if `in` is out of range. int ec_bignum_to_felem(const EC_GROUP *group, EC_FELEM *out, const BIGNUM *in); -// ec_felem_to_bignum converts |in| to a |BIGNUM|. It returns one on success and +// ec_felem_to_bignum converts `in` to a `BIGNUM`. It returns one on success and // zero on allocation failure. int ec_felem_to_bignum(const EC_GROUP *group, BIGNUM *out, const EC_FELEM *in); -// ec_felem_to_bytes serializes |in| as a big-endian bytestring to |out| and -// sets |*out_len| to the number of bytes written. The number of bytes written -// is |BN_num_bytes(&group->order)|, which is at most |EC_MAX_BYTES|. +// ec_felem_to_bytes serializes `in` as a big-endian bytestring to `out` and +// sets `*out_len` to the number of bytes written. The number of bytes written +// is `BN_num_bytes(&group->order)`, which is at most `EC_MAX_BYTES`. void ec_felem_to_bytes(const EC_GROUP *group, uint8_t *out, size_t *out_len, const EC_FELEM *in); -// ec_felem_from_bytes deserializes |in| and stores the resulting field element -// to |out|. It returns one on success and zero if |in| is invalid. +// ec_felem_from_bytes deserializes `in` as a big-endian field element and +// stores the result to `out`. It returns one on success and zero if `in` is +// invalid. int ec_felem_from_bytes(const EC_GROUP *group, EC_FELEM *out, const uint8_t *in, size_t len); -// ec_felem_neg sets |out| to -|a|. +// ec_felem_from_bytes_or_placeholder deserializes `in` as a big-endian field +// element. On success, it stores the resulting field element to `out` and +// returns `CONSTTIME_TRUE_W`. If `in` is out of range, it sets `out` to an +// arbitrary field element and returns `CONSTTIME_FALSE_W`. Both `in`, and +// whether it was in range, are treated as secret. `len` must be equal to +// `BN_num_words(field)` or the function will abort. +crypto_word_t ec_felem_from_bytes_or_placeholder(const EC_GROUP *group, + EC_FELEM *out, + const uint8_t *in, size_t len); + +// ec_felem_neg sets `out` to -`a`. void ec_felem_neg(const EC_GROUP *group, EC_FELEM *out, const EC_FELEM *a); -// ec_felem_add sets |out| to |a| + |b|. +// ec_felem_add sets `out` to `a` + `b`. void ec_felem_add(const EC_GROUP *group, EC_FELEM *out, const EC_FELEM *a, const EC_FELEM *b); -// ec_felem_add sets |out| to |a| - |b|. +// ec_felem_add sets `out` to `a` - `b`. void ec_felem_sub(const EC_GROUP *group, EC_FELEM *out, const EC_FELEM *a, const EC_FELEM *b); -// ec_felem_non_zero_mask returns all ones if |a| is non-zero and all zeros +// ec_felem_non_zero_mask returns all ones if `a` is non-zero and all zeros // otherwise. BN_ULONG ec_felem_non_zero_mask(const EC_GROUP *group, const EC_FELEM *a); -// ec_felem_select, in constant time, sets |out| to |a| if |mask| is all ones -// and |b| if |mask| is all zeros. +// ec_felem_select, in constant time, sets `out` to `a` if `mask` is all ones +// and `b` if `mask` is all zeros. void ec_felem_select(const EC_GROUP *group, EC_FELEM *out, BN_ULONG mask, const EC_FELEM *a, const EC_FELEM *b); -// ec_felem_equal returns one if |a| and |b| are equal and zero otherwise. +// ec_felem_equal returns one if `a` and `b` are equal and zero otherwise. int ec_felem_equal(const EC_GROUP *group, const EC_FELEM *a, const EC_FELEM *b); -// ec_felem_mul sets |out| to |a| * |b|. +// ec_felem_mul sets `out` to `a` * `b`. void ec_felem_mul(const EC_GROUP *group, EC_FELEM *out, const EC_FELEM *a, const EC_FELEM *b); -// ec_felem_sqr sets |out| to |a|^2. +// ec_felem_sqr sets `out` to `a`². void ec_felem_sqr(const EC_GROUP *group, EC_FELEM *out, const EC_FELEM *a); -// ec_felem_to_montgomery sets |out| to |a| converted to Montgomery form. +// ec_felem_to_montgomery sets `out` to `a` converted to Montgomery form. void ec_felem_to_montgomery(const EC_GROUP *group, EC_FELEM *out, const EC_FELEM *a); -// ec_felem_from_montgomery sets |out| to |a| converted from Montgomery form. +// ec_felem_from_montgomery sets `out` to `a` converted from Montgomery form. void ec_felem_from_montgomery(const EC_GROUP *group, EC_FELEM *out, const EC_FELEM *a); -// ec_felem_reduce sets |out| to |words|, reduced modulo the field size, p. -// |words| must be less than p^2. |num| must be at most twice the width of p. -// This function treats |words| as secret. +// ec_felem_reduce sets `out` to `words`, reduced modulo the field size, p. +// `words` must be less than p². `num` must be at most twice the width of p. +// This function treats `words` as secret. void ec_felem_reduce(const EC_GROUP *group, EC_FELEM *out, const BN_ULONG *words, size_t num); -// ec_felem_exp sets |out| to |a|^|exp|. It treats |a| is secret but |exp| as +// ec_felem_exp sets `out` to `a`^`exp`. It treats `a` is secret but `exp` as // public. // // TODO(crbug.com/42290435): hash-to-curve uses this as part of computing a // square root, which is what compressed coordinates ultimately needs to avoid -// |BIGNUM|. Can we unify this a bit? By generalizing to arbitrary +// `BIGNUM`. Can we unify this a bit? By generalizing to arbitrary // exponentiation, we also miss an opportunity to use a specialized addition // chain. We also miss our specialized field arithmetic for P-256. void ec_felem_exp(const EC_GROUP *group, EC_FELEM *out, const EC_FELEM *a, const BN_ULONG *exp, size_t num_exp); +// ec_felem_sqrt sets `out` to the square root of `a` whose least significant +// bit is `y_bit`. It returns one on success and zero on error or if `a` is not +// a square. +int ec_felem_sqrt(const EC_GROUP *group, EC_FELEM *out, const EC_FELEM *a, + crypto_word_t y_bit); + +// ec_felem_sqrt_secret sets `out` to the square root of `a` whose least +// significant bit is `y_bit`. It returns `CONSTTIME_TRUE_W` on success and +// `CONSTTIME_FALSE_W` if `a` is not a square. +// +// This function treats `a`, `y_bit`, and whether `a` is a square, as secret. If +// `a` is not a square, `out` will be set to some arbitrary field element. This +// function is only implemented for curves whose field is 3 mod 4. (In +// particular, it is *not* implemented for P-224.) It aborts on invalid group. +crypto_word_t ec_felem_sqrt_secret(const EC_GROUP *group, EC_FELEM *out, + const EC_FELEM *a, crypto_word_t y_bit); + // Points. // -// Points may represented in affine coordinates as |EC_AFFINE| or Jacobian -// coordinates as |EC_JACOBIAN|. Affine coordinates directly represent a +// Points may represented in affine coordinates as `EC_AFFINE` or Jacobian +// coordinates as `EC_JACOBIAN`. Affine coordinates directly represent a // point on the curve, but point addition over affine coordinates requires // costly field inversions, so arithmetic is done in Jacobian coordinates. // Converting from affine to Jacobian is cheap, while converting from Jacobian @@ -240,8 +268,8 @@ void ec_felem_exp(const EC_GROUP *group, EC_FELEM *out, const EC_FELEM *a, // inversions needed in a sequence of point operations.) // An EC_JACOBIAN represents an elliptic curve point in Jacobian coordinates. -// Unlike |EC_POINT|, it is a plain struct which can be stack-allocated and -// needs no cleanup. It is specific to an |EC_GROUP| and must not be mixed +// Unlike `EC_POINT`, it is a plain struct which can be stack-allocated and +// needs no cleanup. It is specific to an `EC_GROUP` and must not be mixed // between groups. typedef struct { // X, Y, and Z are Jacobian projective coordinates. They represent @@ -256,23 +284,23 @@ typedef struct { EC_FELEM X, Y; } EC_AFFINE; -// ec_affine_to_jacobian converts |p| to Jacobian form and writes the result to -// |*out|. This operation is very cheap and only costs a few copies. +// ec_affine_to_jacobian converts `p` to Jacobian form and writes the result to +// `*out`. This operation is very cheap and only costs a few copies. void ec_affine_to_jacobian(const EC_GROUP *group, EC_JACOBIAN *out, const EC_AFFINE *p); -// ec_jacobian_to_affine converts |p| to affine form and writes the result to -// |*out|. It returns one on success and zero if |p| was the point at infinity. +// ec_jacobian_to_affine converts `p` to affine form and writes the result to +// `*out`. It returns one on success and zero if `p` was the point at infinity. // This operation performs a field inversion and should only be done once per // point. // -// If only extracting the x-coordinate, use |ec_get_x_coordinate_*| which is +// If only extracting the x-coordinate, use `ec_get_x_coordinate_*` which is // slightly faster. int ec_jacobian_to_affine(const EC_GROUP *group, EC_AFFINE *out, const EC_JACOBIAN *p); -// ec_jacobian_to_affine_batch converts |num| points in |in| from Jacobian -// coordinates to affine coordinates and writes the results to |out|. It returns +// ec_jacobian_to_affine_batch converts `num` points in `in` from Jacobian +// coordinates to affine coordinates and writes the results to `out`. It returns // one on success and zero if any of the input points were infinity. // // This function is not implemented for all curves. Add implementations as @@ -280,36 +308,45 @@ int ec_jacobian_to_affine(const EC_GROUP *group, EC_AFFINE *out, int ec_jacobian_to_affine_batch(const EC_GROUP *group, EC_AFFINE *out, const EC_JACOBIAN *in, size_t num); -// ec_point_set_affine_coordinates sets |out|'s to a point with affine -// coordinates |x| and |y|. It returns one if the point is on the curve and -// zero otherwise. If the point is not on the curve, the value of |out| is -// undefined. +// ec_y_sqr_from_x sets `out` to `x^3 + ax + b`, the expected value of y^2. +void ec_y_sqr_from_x(const EC_GROUP *group, EC_FELEM *out, const EC_FELEM *x); + +// ec_point_set_affine_coordinates sets `out` to a point with affine coordinates +// `x` and `y`. It returns one if the point is on the curve and zero otherwise. +// If the point is not on the curve, the value of `out` is undefined. int ec_point_set_affine_coordinates(const EC_GROUP *group, EC_AFFINE *out, const EC_FELEM *x, const EC_FELEM *y); -// ec_point_mul_no_self_test does the same as |EC_POINT_mul|, but doesn't try to +// ec_point_set_compressed_coordinates sets `out` to a point with compressed +// coordinates `x` and `y_bit`. It returns one if the point is on the curve and +// zero otherwise. If the point is not on the curve, the value of `out` is +// undefined. +int ec_point_set_compressed_coordinates(const EC_GROUP *group, EC_AFFINE *out, + const EC_FELEM *x, crypto_word_t y_bit); + +// ec_point_mul_no_self_test does the same as `EC_POINT_mul`, but doesn't try to // run the self-test first. This is for use in the self tests themselves, to // prevent an infinite loop. int ec_point_mul_no_self_test(const EC_GROUP *group, EC_POINT *r, const BIGNUM *g_scalar, const EC_POINT *p, const BIGNUM *p_scalar, BN_CTX *ctx); -// ec_point_mul_scalar sets |r| to |p| * |scalar|. Both inputs are considered +// ec_point_mul_scalar sets `r` to `p` * `scalar`. Both inputs are considered // secret. int ec_point_mul_scalar(const EC_GROUP *group, EC_JACOBIAN *r, const EC_JACOBIAN *p, const EC_SCALAR *scalar); -// ec_point_mul_scalar_base sets |r| to generator * |scalar|. |scalar| is +// ec_point_mul_scalar_base sets `r` to generator * `scalar`. `scalar` is // treated as secret. int ec_point_mul_scalar_base(const EC_GROUP *group, EC_JACOBIAN *r, const EC_SCALAR *scalar); -// ec_point_mul_scalar_batch sets |r| to |p0| * |scalar0| + |p1| * |scalar1| + -// |p2| * |scalar2|. |p2| may be NULL to skip that term. +// ec_point_mul_scalar_batch sets `r` to `p0` * `scalar0` + `p1` * `scalar1` + +// `p2` * `scalar2`. `p2` may be NULL to skip that term. // // The inputs are treated as secret, however, this function leaks information // about whether intermediate computations add a point to itself. Callers must -// ensure that discrete logs between |p0|, |p1|, and |p2| are uniformly +// ensure that discrete logs between `p0`, `p1`, and `p2` are uniformly // distributed and independent of the scalars, which should be uniformly // selected and not under the attackers control. This ensures the doubling case // will occur with negligible probability. @@ -318,7 +355,7 @@ int ec_point_mul_scalar_base(const EC_GROUP *group, EC_JACOBIAN *r, // needed. // // TODO(davidben): This function does not use base point tables. For now, it is -// only used with the generic |EC_GFp_mont_method| implementation which has +// only used with the generic `EC_GFp_mont_method` implementation which has // none. If generalizing to tuned curves, this may be useful. However, we still // must double up to the least efficient input, so precomputed tables can only // save table setup and allow a wider window size. @@ -329,16 +366,16 @@ int ec_point_mul_scalar_batch(const EC_GROUP *group, EC_JACOBIAN *r, #define EC_MONT_PRECOMP_COMB_SIZE 5 -// An |EC_PRECOMP| stores precomputed information about a point, to optimize +// An `EC_PRECOMP` stores precomputed information about a point, to optimize // repeated multiplications involving it. It is a union so different -// |EC_METHOD|s can store different information in it. +// `EC_METHOD`s can store different information in it. typedef union { EC_AFFINE comb[(1 << EC_MONT_PRECOMP_COMB_SIZE) - 1]; } EC_PRECOMP; -// ec_init_precomp precomputes multiples of |p| and writes the result to |out|. +// ec_init_precomp precomputes multiples of `p` and writes the result to `out`. // It returns one on success and zero on error. The resulting table may be used -// with |ec_point_mul_scalar_precomp|. This function will fail if |p| is the +// with `ec_point_mul_scalar_precomp`. This function will fail if `p` is the // point at infinity. // // This function is not implemented for all curves. Add implementations as @@ -346,16 +383,16 @@ typedef union { int ec_init_precomp(const EC_GROUP *group, EC_PRECOMP *out, const EC_JACOBIAN *p); -// ec_point_mul_scalar_precomp sets |r| to |p0| * |scalar0| + |p1| * |scalar1| + -// |p2| * |scalar2|. |p1| or |p2| may be NULL to skip the corresponding term. -// The points are represented as |EC_PRECOMP| and must be initialized with -// |ec_init_precomp|. This function runs faster than |ec_point_mul_scalar_batch| +// ec_point_mul_scalar_precomp sets `r` to `p0` * `scalar0` + `p1` * `scalar1` + +// `p2` * `scalar2`. `p1` or `p2` may be NULL to skip the corresponding term. +// The points are represented as `EC_PRECOMP` and must be initialized with +// `ec_init_precomp`. This function runs faster than `ec_point_mul_scalar_batch` // but requires setup work per input point, so it is only appropriate for points // which are used frequently. // // The inputs are treated as secret, however, this function leaks information // about whether intermediate computations add a point to itself. Callers must -// ensure that discrete logs between |p0|, |p1|, and |p2| are uniformly +// ensure that discrete logs between `p0`, `p1`, and `p2` are uniformly // distributed and independent of the scalars, which should be uniformly // selected and not under the attackers control. This ensures the doubling case // will occur with negligible probability. @@ -364,7 +401,7 @@ int ec_init_precomp(const EC_GROUP *group, EC_PRECOMP *out, // needed. // // TODO(davidben): This function does not use base point tables. For now, it is -// only used with the generic |EC_GFp_mont_method| implementation which has +// only used with the generic `EC_GFp_mont_method` implementation which has // none. If generalizing to tuned curves, we should add a parameter for the base // point and arrange for the generic implementation to have base point tables // available. @@ -373,17 +410,17 @@ int ec_point_mul_scalar_precomp(const EC_GROUP *group, EC_JACOBIAN *r, const EC_PRECOMP *p1, const EC_SCALAR *scalar1, const EC_PRECOMP *p2, const EC_SCALAR *scalar2); -// ec_point_mul_scalar_public sets |r| to -// generator * |g_scalar| + |p| * |p_scalar|. It assumes that the inputs are +// ec_point_mul_scalar_public sets `r` to +// generator * `g_scalar` + `p` * `p_scalar`. It assumes that the inputs are // public so there is no concern about leaking their values through timing. int ec_point_mul_scalar_public(const EC_GROUP *group, EC_JACOBIAN *r, const EC_SCALAR *g_scalar, const EC_JACOBIAN *p, const EC_SCALAR *p_scalar); -// ec_point_mul_scalar_public_batch sets |r| to the sum of generator * -// |g_scalar| and |points[i]| * |scalars[i]| where |points| and |scalars| have -// |num| elements. It assumes that the inputs are public so there is no concern -// about leaking their values through timing. |g_scalar| may be NULL to skip +// ec_point_mul_scalar_public_batch sets `r` to the sum of generator * +// `g_scalar` and `points[i]` * `scalars[i]` where `points` and `scalars` have +// `num` elements. It assumes that the inputs are public so there is no concern +// about leaking their values through timing. `g_scalar` may be NULL to skip // that term. // // This function is not implemented for all curves. Add implementations as @@ -393,33 +430,33 @@ int ec_point_mul_scalar_public_batch(const EC_GROUP *group, EC_JACOBIAN *r, const EC_JACOBIAN *points, const EC_SCALAR *scalars, size_t num); -// ec_point_select, in constant time, sets |out| to |a| if |mask| is all ones -// and |b| if |mask| is all zeros. +// ec_point_select, in constant time, sets `out` to `a` if `mask` is all ones +// and `b` if `mask` is all zeros. void ec_point_select(const EC_GROUP *group, EC_JACOBIAN *out, BN_ULONG mask, const EC_JACOBIAN *a, const EC_JACOBIAN *b); -// ec_affine_select behaves like |ec_point_select| but acts on affine points. +// ec_affine_select behaves like `ec_point_select` but acts on affine points. void ec_affine_select(const EC_GROUP *group, EC_AFFINE *out, BN_ULONG mask, const EC_AFFINE *a, const EC_AFFINE *b); -// ec_precomp_select behaves like |ec_point_select| but acts on |EC_PRECOMP|. +// ec_precomp_select behaves like `ec_point_select` but acts on `EC_PRECOMP`. void ec_precomp_select(const EC_GROUP *group, EC_PRECOMP *out, BN_ULONG mask, const EC_PRECOMP *a, const EC_PRECOMP *b); -// ec_cmp_x_coordinate compares the x (affine) coordinate of |p|, mod the group -// order, with |r|. It returns one if the values match and zero if |p| is the -// point at infinity of the values do not match. |p| is treated as public. +// ec_cmp_x_coordinate compares the x (affine) coordinate of `p`, mod the group +// order, with `r`. It returns one if the values match and zero if `p` is the +// point at infinity of the values do not match. `p` is treated as public. int ec_cmp_x_coordinate(const EC_GROUP *group, const EC_JACOBIAN *p, const EC_SCALAR *r); -// ec_get_x_coordinate_as_scalar sets |*out| to |p|'s x-coordinate, modulo -// |group->order|. It returns one on success and zero if |p| is the point at +// ec_get_x_coordinate_as_scalar sets `*out` to `p`'s x-coordinate, modulo +// `group->order`. It returns one on success and zero if `p` is the point at // infinity. int ec_get_x_coordinate_as_scalar(const EC_GROUP *group, EC_SCALAR *out, const EC_JACOBIAN *p); -// ec_get_x_coordinate_as_bytes writes |p|'s affine x-coordinate to |out|, which -// must have at must |max_out| bytes. It sets |*out_len| to the number of bytes +// ec_get_x_coordinate_as_bytes writes `p`'s affine x-coordinate to `out`, which +// must have at must `max_out` bytes. It sets `*out_len` to the number of bytes // written. The value is written big-endian and zero-padded to the size of the // field. This function returns one on success and zero on failure. int ec_get_x_coordinate_as_bytes(const EC_GROUP *group, uint8_t *out, @@ -427,30 +464,36 @@ int ec_get_x_coordinate_as_bytes(const EC_GROUP *group, uint8_t *out, const EC_JACOBIAN *p); // ec_point_byte_len returns the number of bytes in the byte representation of -// a non-infinity point in |group|, encoded according to |form|, or zero if -// |form| is invalid. +// a non-infinity point in `group`, encoded according to `form`, or zero if +// `form` is invalid. size_t ec_point_byte_len(const EC_GROUP *group, point_conversion_form_t form); -// ec_point_to_bytes encodes |point| according to |form| and writes the result -// |buf|. It returns the size of the output on success or zero on error. At most -// |max_out| bytes will be written. The buffer should be at least -// |ec_point_byte_len| long to guarantee success. +// ec_point_to_bytes encodes `point` according to `form` and writes the result +// `buf`. It returns the size of the output on success or zero on error. At most +// `max_out` bytes will be written. The buffer should be at least +// `ec_point_byte_len` long to guarantee success. size_t ec_point_to_bytes(const EC_GROUP *group, const EC_AFFINE *point, point_conversion_form_t form, uint8_t *buf, size_t max_out); -// ec_point_from_uncompressed parses |in| as a point in uncompressed form and -// sets the result to |out|. It returns one on success and zero if the input was +// ec_point_from_uncompressed parses `in` as a point in uncompressed form and +// sets `out` to the result. It returns one on success and zero if the input was // invalid. int ec_point_from_uncompressed(const EC_GROUP *group, EC_AFFINE *out, const uint8_t *in, size_t len); -// ec_set_to_safe_point sets |out| to an arbitrary point on |group|, either the +// ec_point_from_compressed parses `in` as a point in compressed form and sets +// `out` to the result`. It returns one on success and zero if the input was +// invalid. +int ec_point_from_compressed(const EC_GROUP *group, EC_AFFINE *out, + const uint8_t *in, size_t len); + +// ec_set_to_safe_point sets `out` to an arbitrary point on `group`, either the // generator or the point at infinity. This is used to guard against callers of // external APIs not checking the return value. void ec_set_to_safe_point(const EC_GROUP *group, EC_JACOBIAN *out); -// ec_affine_jacobian_equal returns one if |a| and |b| represent the same point +// ec_affine_jacobian_equal returns one if `a` and `b` represent the same point // and zero otherwise. It treats both inputs as secret. int ec_affine_jacobian_equal(const EC_GROUP *group, const EC_AFFINE *a, const EC_JACOBIAN *b); @@ -460,56 +503,56 @@ BSSL_NAMESPACE_END // Implementation details. struct ec_method_st { - // point_get_affine_coordinates sets |*x| and |*y| to the affine coordinates - // of |p|. Either |x| or |y| may be NULL to omit it. It returns one on success - // and zero if |p| is the point at infinity. It leaks whether |p| was the - // point at infinity, but otherwise treats |p| as secret. + // point_get_affine_coordinates sets `*x` and `*y` to the affine coordinates + // of `p`. Either `x` or `y` may be NULL to omit it. It returns one on success + // and zero if `p` is the point at infinity. It leaks whether `p` was the + // point at infinity, but otherwise treats `p` as secret. int (*point_get_affine_coordinates)(const EC_GROUP *, const bssl::EC_JACOBIAN *p, bssl::EC_FELEM *x, bssl::EC_FELEM *y); - // jacobian_to_affine_batch implements |ec_jacobian_to_affine_batch|. + // jacobian_to_affine_batch implements `ec_jacobian_to_affine_batch`. int (*jacobian_to_affine_batch)(const EC_GROUP *group, bssl::EC_AFFINE *out, const bssl::EC_JACOBIAN *in, size_t num); - // add sets |r| to |a| + |b|. + // add sets `r` to `a` + `b`. void (*add)(const EC_GROUP *group, bssl::EC_JACOBIAN *r, const bssl::EC_JACOBIAN *a, const bssl::EC_JACOBIAN *b); - // dbl sets |r| to |a| + |a|. + // dbl sets `r` to `a` + `a`. void (*dbl)(const EC_GROUP *group, bssl::EC_JACOBIAN *r, const bssl::EC_JACOBIAN *a); - // mul sets |r| to |scalar|*|p|. + // mul sets `r` to `scalar`*`p`. void (*mul)(const EC_GROUP *group, bssl::EC_JACOBIAN *r, const bssl::EC_JACOBIAN *p, const bssl::EC_SCALAR *scalar); - // mul_base sets |r| to |scalar|*generator. + // mul_base sets `r` to `scalar`*generator. void (*mul_base)(const EC_GROUP *group, bssl::EC_JACOBIAN *r, const bssl::EC_SCALAR *scalar); - // mul_batch implements |ec_mul_scalar_batch|. + // mul_batch implements `ec_mul_scalar_batch`. void (*mul_batch)(const EC_GROUP *group, bssl::EC_JACOBIAN *r, const bssl::EC_JACOBIAN *p0, const bssl::EC_SCALAR *scalar0, const bssl::EC_JACOBIAN *p1, const bssl::EC_SCALAR *scalar1, const bssl::EC_JACOBIAN *p2, const bssl::EC_SCALAR *scalar2); - // mul_public sets |r| to |g_scalar|*generator + |p_scalar|*|p|. It assumes + // mul_public sets `r` to `g_scalar`*generator + `p_scalar`*`p`. It assumes // that the inputs are public so there is no concern about leaking their // values through timing. // - // This function may be omitted if |mul_public_batch| is provided. + // This function may be omitted if `mul_public_batch` is provided. void (*mul_public)(const EC_GROUP *group, bssl::EC_JACOBIAN *r, const bssl::EC_SCALAR *g_scalar, const bssl::EC_JACOBIAN *p, const bssl::EC_SCALAR *p_scalar); - // mul_public_batch implements |ec_point_mul_scalar_public_batch|. + // mul_public_batch implements `ec_point_mul_scalar_public_batch`. int (*mul_public_batch)(const EC_GROUP *group, bssl::EC_JACOBIAN *r, const bssl::EC_SCALAR *g_scalar, const bssl::EC_JACOBIAN *points, const bssl::EC_SCALAR *scalars, size_t num); - // init_precomp implements |ec_init_precomp|. + // init_precomp implements `ec_init_precomp`. int (*init_precomp)(const EC_GROUP *group, bssl::EC_PRECOMP *out, const bssl::EC_JACOBIAN *p); - // mul_precomp implements |ec_point_mul_scalar_precomp|. + // mul_precomp implements `ec_point_mul_scalar_precomp`. void (*mul_precomp)(const EC_GROUP *group, bssl::EC_JACOBIAN *r, const bssl::EC_PRECOMP *p0, const bssl::EC_SCALAR *scalar0, @@ -518,18 +561,18 @@ struct ec_method_st { const bssl::EC_PRECOMP *p2, const bssl::EC_SCALAR *scalar2); - // scalar_inv0_montgomery implements |ec_scalar_inv0_montgomery|. + // scalar_inv0_montgomery implements `ec_scalar_inv0_montgomery`. void (*scalar_inv0_montgomery)(const EC_GROUP *group, bssl::EC_SCALAR *out, const bssl::EC_SCALAR *in); // scalar_to_montgomery_inv_vartime implements - // |ec_scalar_to_montgomery_inv_vartime|. + // `ec_scalar_to_montgomery_inv_vartime`. int (*scalar_to_montgomery_inv_vartime)(const EC_GROUP *group, bssl::EC_SCALAR *out, const bssl::EC_SCALAR *in); - // cmp_x_coordinate compares the x (affine) coordinate of |p|, mod the group - // order, with |r|. It returns one if the values match and zero if |p| is the + // cmp_x_coordinate compares the x (affine) coordinate of `p`, mod the group + // order, with `r`. It returns one if the values match and zero if `p` is the // point at infinity of the values do not match. int (*cmp_x_coordinate)(const EC_GROUP *group, const bssl::EC_JACOBIAN *p, const bssl::EC_SCALAR *r); @@ -542,12 +585,12 @@ const EC_METHOD *EC_GFp_mont_method(); BSSL_NAMESPACE_END struct ec_point_st { - // group is an owning reference to |group|, unless this is - // |group->generator|. + // group is an owning reference to `group`, unless this is + // `group->generator`. EC_GROUP *group; - // raw is the group-specific point data. Functions that take |EC_POINT| - // typically check consistency with |EC_GROUP| while functions that take - // |EC_JACOBIAN| do not. Thus accesses to this field should be externally + // raw is the group-specific point data. Functions that take `EC_POINT` + // typically check consistency with `EC_GROUP` while functions that take + // `EC_JACOBIAN` do not. Thus accesses to this field should be externally // checked for consistency. bssl::EC_JACOBIAN raw; } /* EC_POINT */; @@ -555,9 +598,9 @@ struct ec_point_st { struct ec_group_st { const EC_METHOD *meth; - // Unlike all other |EC_POINT|s, |generator| does not own |generator->group| + // Unlike all other `EC_POINT`s, `generator` does not own `generator->group` // to avoid a reference cycle. Additionally, Z is guaranteed to be one, so X - // and Y are suitable for use as an |EC_AFFINE|. Before |has_order| is set, Z + // and Y are suitable for use as an `EC_AFFINE`. Before `has_order` is set, Z // is one, but X and Y are uninitialized. EC_POINT generator; @@ -578,16 +621,18 @@ struct ec_group_st { uint8_t oid[9]; uint8_t oid_len; - // a_is_minus3 is one if |a| is -3 mod |field| and zero otherwise. Point - // arithmetic is optimized for -3. - int a_is_minus3; + // a_is_minus3 is whether `a` is -3 mod `field`. Point arithmetic is optimized + // for -3. + bool a_is_minus3; + + // field_is_3_mod_4 is whether `field` is 3 mod 4. + bool field_is_3_mod_4; - // has_order is one if |generator| and |order| have been initialized. - int has_order; + // has_order is whether `generator` and `order` have been initialized. + bool has_order; - // field_greater_than_order is one if |field| is greater than |order| and zero - // otherwise. - int field_greater_than_order; + // field_greater_than_order is whether `field` is greater than `order`. + bool field_greater_than_order; } /* EC_GROUP */; BSSL_NAMESPACE_BEGIN @@ -620,7 +665,7 @@ void ec_GFp_mont_mul_precomp(const EC_GROUP *group, EC_JACOBIAN *r, const EC_PRECOMP *p2, const EC_SCALAR *scalar2); // ec_compute_wNAF writes the modified width-(w+1) Non-Adjacent Form (wNAF) of -// |scalar| to |out|. |out| must have room for |bits| + 1 elements, each of +// `scalar` to `out`. `out` must have room for `bits` + 1 elements, each of // which will be either zero or odd with an absolute value less than 2^w // satisfying // scalar = \sum_j out[j]*2^j @@ -670,8 +715,8 @@ const EC_METHOD *EC_GFp_nistp256_method(); // x86-64 optimized P256. See http://eprint.iacr.org/2013/816. const EC_METHOD *EC_GFp_nistz256_method(); -// An EC_WRAPPED_SCALAR is an |EC_SCALAR| with a parallel |BIGNUM| -// representation. It exists to support the |EC_KEY_get0_private_key| API. +// An EC_WRAPPED_SCALAR is an `EC_SCALAR` with a parallel `BIGNUM` +// representation. It exists to support the `EC_KEY_get0_private_key` API. typedef struct { BIGNUM bignum; EC_SCALAR scalar; @@ -683,9 +728,9 @@ class ECKey : public ec_key_st, public RefCounted { EC_GROUP *group = nullptr; - // Ideally |pub_key| would be an |EC_AFFINE| so serializing it does not pay an - // inversion each time, but the |EC_KEY_get0_public_key| API implies public - // keys are stored in an |EC_POINT|-compatible form. + // Ideally `pub_key` would be an `EC_AFFINE` so serializing it does not pay an + // inversion each time, but the `EC_KEY_get0_public_key` API implies public + // keys are stored in an `EC_POINT`-compatible form. EC_POINT *pub_key = nullptr; bssl::EC_WRAPPED_SCALAR *priv_key = nullptr; diff --git a/third_party/boringssl/src/crypto/fipsmodule/ec/oct.cc.inc b/third_party/boringssl/src/crypto/fipsmodule/ec/oct.cc.inc index 1ed7cbd26..012070820 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/ec/oct.cc.inc +++ b/third_party/boringssl/src/crypto/fipsmodule/ec/oct.cc.inc @@ -69,19 +69,31 @@ size_t bssl::ec_point_to_bytes(const EC_GROUP *group, const EC_AFFINE *point, int bssl::ec_point_from_uncompressed(const EC_GROUP *group, EC_AFFINE *out, const uint8_t *in, size_t len) { const size_t field_len = BN_num_bytes(&group->field.N); - if (len != 1 + 2 * field_len || in[0] != POINT_CONVERSION_UNCOMPRESSED) { + if (len != 1 /* form */ + 2 * field_len || + in[0] != POINT_CONVERSION_UNCOMPRESSED) { OPENSSL_PUT_ERROR(EC, EC_R_INVALID_ENCODING); return 0; } EC_FELEM x, y; - if (!ec_felem_from_bytes(group, &x, in + 1, field_len) || - !ec_felem_from_bytes(group, &y, in + 1 + field_len, field_len) || - !ec_point_set_affine_coordinates(group, out, &x, &y)) { + return ec_felem_from_bytes(group, &x, in + 1, field_len) && + ec_felem_from_bytes(group, &y, in + 1 + field_len, field_len) && + ec_point_set_affine_coordinates(group, out, &x, &y); +} + +int bssl::ec_point_from_compressed(const EC_GROUP *group, EC_AFFINE *out, + const uint8_t *in, size_t len) { + const size_t field_len = BN_num_bytes(&group->field.N); + if (len != 1 /* form */ + field_len || + (in[0] & ~1u) != POINT_CONVERSION_COMPRESSED) { + OPENSSL_PUT_ERROR(EC, EC_R_INVALID_ENCODING); return 0; } - return 1; + crypto_word_t y_bit = in[0] & 1; + EC_FELEM x; + return ec_felem_from_bytes(group, &x, in + 1, field_len) && + ec_point_set_compressed_coordinates(group, out, &x, y_bit); } static int ec_GFp_simple_oct2point(const EC_GROUP *group, EC_POINT *point, @@ -92,56 +104,18 @@ static int ec_GFp_simple_oct2point(const EC_GROUP *group, EC_POINT *point, return 0; } - uint8_t form = buf[0]; - if (form == static_cast(POINT_CONVERSION_UNCOMPRESSED)) { - EC_AFFINE affine; - if (!ec_point_from_uncompressed(group, &affine, buf, len)) { - // In the event of an error, defend against the caller not checking the - // return value by setting a known safe value. - ec_set_to_safe_point(group, &point->raw); - return 0; - } - ec_affine_to_jacobian(group, &point->raw, &affine); - return 1; - } - - const int y_bit = form & 1; - const size_t field_len = BN_num_bytes(&group->field.N); - form = form & ~1u; - if (form != static_cast(POINT_CONVERSION_COMPRESSED) || - len != 1 /* type byte */ + field_len) { - OPENSSL_PUT_ERROR(EC, EC_R_INVALID_ENCODING); - return 0; - } - - // TODO(davidben): Integrate compressed coordinates with the lower-level EC - // abstractions. This requires a way to compute square roots, which is tricky - // for primes which are not 3 (mod 4), namely P-224 and custom curves. P-224's - // prime is particularly inconvenient for compressed coordinates. See - // https://cr.yp.to/papers/sqroot.pdf - UniquePtr new_ctx; - if (ctx == nullptr) { - new_ctx.reset(BN_CTX_new()); - if (new_ctx == nullptr) { - return 0; - } - ctx = new_ctx.get(); - } - - BN_CTXScope scope(ctx); - BIGNUM *x = BN_CTX_get(ctx); - if (x == nullptr || !BN_bin2bn(buf + 1, field_len, x)) { - return 0; - } - if (BN_ucmp(x, &group->field.N) >= 0) { - OPENSSL_PUT_ERROR(EC, EC_R_INVALID_ENCODING); - return 0; - } - - if (!EC_POINT_set_compressed_coordinates_GFp(group, point, x, y_bit, ctx)) { + const uint8_t form = buf[0]; + EC_AFFINE affine; + bool ok = form == static_cast(POINT_CONVERSION_UNCOMPRESSED) + ? ec_point_from_uncompressed(group, &affine, buf, len) + : ec_point_from_compressed(group, &affine, buf, len); + if (!ok) { + // In the event of an error, defend against the caller not checking the + // return value by setting a known safe value. + ec_set_to_safe_point(group, &point->raw); return 0; } - + ec_affine_to_jacobian(group, &point->raw, &affine); return 1; } @@ -162,9 +136,10 @@ size_t EC_POINT_point2oct(const EC_GROUP *group, const EC_POINT *point, return 0; } if (buf == nullptr) { - // When |buf| is NULL, just return the number of bytes that would be + // When `buf` is NULL, just return the number of bytes that would be // written, without doing an expensive Jacobian-to-affine conversion. - if (ec_GFp_simple_is_at_infinity(group, &point->raw)) { + if (constant_time_declassify_int( + ec_GFp_simple_is_at_infinity(group, &point->raw))) { OPENSSL_PUT_ERROR(EC, EC_R_POINT_AT_INFINITY); return 0; } @@ -197,100 +172,3 @@ size_t EC_POINT_point2buf(const EC_GROUP *group, const EC_POINT *point, *out_buf = buf; return len; } - -int EC_POINT_set_compressed_coordinates_GFp(const EC_GROUP *group, - EC_POINT *point, const BIGNUM *x, - int y_bit, BN_CTX *ctx) { - if (EC_GROUP_cmp(group, point->group, nullptr) != 0) { - OPENSSL_PUT_ERROR(EC, EC_R_INCOMPATIBLE_OBJECTS); - return 0; - } - - const BIGNUM *field = &group->field.N; - if (BN_is_negative(x) || BN_cmp(x, field) >= 0) { - OPENSSL_PUT_ERROR(EC, EC_R_INVALID_COMPRESSED_POINT); - return 0; - } - - ERR_clear_error(); - - UniquePtr new_ctx; - if (ctx == nullptr) { - new_ctx.reset(BN_CTX_new()); - if (new_ctx == nullptr) { - return 0; - } - ctx = new_ctx.get(); - } - - y_bit = (y_bit != 0); - - BN_CTXScope scope(ctx); - BIGNUM *tmp1 = BN_CTX_get(ctx); - BIGNUM *tmp2 = BN_CTX_get(ctx); - BIGNUM *a = BN_CTX_get(ctx); - BIGNUM *b = BN_CTX_get(ctx); - BIGNUM *y = BN_CTX_get(ctx); - if (y == nullptr || !EC_GROUP_get_curve_GFp(group, nullptr, a, b, ctx)) { - return 0; - } - - // Recover y. We have a Weierstrass equation - // y^2 = x^3 + a*x + b, - // so y is one of the square roots of x^3 + a*x + b. - - // tmp1 := x^3 - if (!BN_mod_sqr(tmp2, x, field, ctx) || - !BN_mod_mul(tmp1, tmp2, x, field, ctx)) { - return 0; - } - - // tmp1 := tmp1 + a*x - if (group->a_is_minus3) { - if (!bn_mod_lshift1_consttime(tmp2, x, field, ctx) || - !bn_mod_add_consttime(tmp2, tmp2, x, field, ctx) || - !bn_mod_sub_consttime(tmp1, tmp1, tmp2, field, ctx)) { - return 0; - } - } else { - if (!BN_mod_mul(tmp2, a, x, field, ctx) || - !bn_mod_add_consttime(tmp1, tmp1, tmp2, field, ctx)) { - return 0; - } - } - - // tmp1 := tmp1 + b - if (!bn_mod_add_consttime(tmp1, tmp1, b, field, ctx)) { - return 0; - } - - if (!BN_mod_sqrt(y, tmp1, field, ctx)) { - if (ERR_equals(ERR_peek_last_error(), ERR_LIB_BN, BN_R_NOT_A_SQUARE)) { - ERR_clear_error(); - OPENSSL_PUT_ERROR(EC, EC_R_INVALID_COMPRESSED_POINT); - } else { - OPENSSL_PUT_ERROR(EC, ERR_R_BN_LIB); - } - return 0; - } - - if (y_bit != BN_is_odd(y)) { - if (BN_is_zero(y)) { - OPENSSL_PUT_ERROR(EC, EC_R_INVALID_COMPRESSION_BIT); - return 0; - } - if (!BN_usub(y, field, y)) { - return 0; - } - } - if (y_bit != BN_is_odd(y)) { - OPENSSL_PUT_ERROR(EC, ERR_R_INTERNAL_ERROR); - return 0; - } - - if (!EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx)) { - return 0; - } - - return 1; -} diff --git a/third_party/boringssl/src/crypto/fipsmodule/ec/p256-nistz.cc.inc b/third_party/boringssl/src/crypto/fipsmodule/ec/p256-nistz.cc.inc index 6200d0732..32dce88e3 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/ec/p256-nistz.cc.inc +++ b/third_party/boringssl/src/crypto/fipsmodule/ec/p256-nistz.cc.inc @@ -57,7 +57,7 @@ static const BN_ULONG ONE_MONT[P256_LIMBS] = { // Precomputed tables for the default generator #include "p256-nistz-table.h" -// Recode window to a signed digit, see |ec_GFp_nistp_recode_scalar_bits| in +// Recode window to a signed digit, see `ec_GFp_nistp_recode_scalar_bits` in // util.c for details static crypto_word_t booth_recode_w5(crypto_word_t in) { crypto_word_t s, d; @@ -81,8 +81,8 @@ static crypto_word_t booth_recode_w7(crypto_word_t in) { return (d << 1) + (s & 1); } -// copy_conditional copies |src| to |dst| if |move| is one and leaves it as-is -// if |move| is zero. +// copy_conditional copies `src` to `dst` if `move` is one and leaves it as-is +// if `move` is zero. // // WARNING: this breaks the usual convention of constant-time functions // returning masks. @@ -215,7 +215,7 @@ static void ecp_nistz256_point_add_affine(P256_POINT *r, const P256_POINT *a, } #endif // OPENSSL_X86_64 -// ecp_nistz256_from_mont sets |res| to |in|, converted from Montgomery domain +// ecp_nistz256_from_mont sets `res` to `in`, converted from Montgomery domain // by multiplying with 1. static void ecp_nistz256_from_mont(BN_ULONG res[P256_LIMBS], const BN_ULONG in[P256_LIMBS]) { @@ -223,8 +223,8 @@ static void ecp_nistz256_from_mont(BN_ULONG res[P256_LIMBS], ecp_nistz256_mul_mont(res, in, ONE); } -// ecp_nistz256_mod_inverse_sqr_mont sets |r| to (|in| * 2^-256)^-2 * 2^256 mod -// p. That is, |r| is the modular inverse square of |in| for input and output in +// ecp_nistz256_mod_inverse_sqr_mont sets `r` to (`in` * 2^-256)^-2 * 2^256 mod +// p. That is, `r` is the modular inverse square of `in` for input and output in // the Montgomery domain. static void ecp_nistz256_mod_inverse_sqr_mont(BN_ULONG r[P256_LIMBS], const BN_ULONG in[P256_LIMBS]) { @@ -232,17 +232,17 @@ static void ecp_nistz256_mod_inverse_sqr_mont(BN_ULONG r[P256_LIMBS], // https://briansmith.org/ecc-inversion-addition-chains-01#p256_field_inversion BN_ULONG x2[P256_LIMBS], x3[P256_LIMBS], x6[P256_LIMBS], x12[P256_LIMBS], x15[P256_LIMBS], x30[P256_LIMBS], x32[P256_LIMBS]; - ecp_nistz256_sqr_mont(x2, in); // 2^2 - 2^1 - ecp_nistz256_mul_mont(x2, x2, in); // 2^2 - 2^0 + ecp_nistz256_sqr_mont(x2, in); // 2² - 2¹ + ecp_nistz256_mul_mont(x2, x2, in); // 2² - 2⁰ - ecp_nistz256_sqr_mont(x3, x2); // 2^3 - 2^1 - ecp_nistz256_mul_mont(x3, x3, in); // 2^3 - 2^0 + ecp_nistz256_sqr_mont(x3, x2); // 2³ - 2¹ + ecp_nistz256_mul_mont(x3, x3, in); // 2³ - 2⁰ ecp_nistz256_sqr_mont(x6, x3); for (int i = 1; i < 3; i++) { ecp_nistz256_sqr_mont(x6, x6); - } // 2^6 - 2^3 - ecp_nistz256_mul_mont(x6, x6, x3); // 2^6 - 2^0 + } // 2⁶ - 2³ + ecp_nistz256_mul_mont(x6, x6, x3); // 2⁶ - 2⁰ ecp_nistz256_sqr_mont(x12, x6); for (int i = 1; i < 6; i++) { @@ -303,8 +303,8 @@ static void ecp_nistz256_windowed_mul(const EC_GROUP *group, P256_POINT *r, static const size_t kWindowSize = 5; static const crypto_word_t kMask = (1 << (5 /* kWindowSize */ + 1)) - 1; - // A |P256_POINT| is (3 * 32) = 96 bytes, and the 64-byte alignment should - // add no more than 63 bytes of overhead. Thus, |table| should require + // A `P256_POINT` is (3 * 32) = 96 bytes, and the 64-byte alignment should + // add no more than 63 bytes of overhead. Thus, `table` should require // ~1599 ((96 * 16) + 63) bytes of stack space. alignas(64) P256_POINT table[16]; uint8_t p_str[33]; @@ -434,9 +434,9 @@ static void ecp_nistz256_point_mul_base(const EC_GROUP *group, EC_JACOBIAN *r, ecp_nistz256_neg(p.Z, t.Y); copy_conditional(t.Y, p.Z, wvalue & 1); - // Convert |t| from affine to Jacobian coordinates. We set Z to zero if |t| - // is infinity and |ONE_MONT| otherwise. |t| was computed from the table, so - // it is infinity iff |wvalue >> 1| is zero. + // Convert `t` from affine to Jacobian coordinates. We set Z to zero if `t` + // is infinity and `ONE_MONT` otherwise. `t` was computed from the table, so + // it is infinity iff `wvalue >> 1` is zero. OPENSSL_memcpy(p.X, t.X, sizeof(p.X)); OPENSSL_memcpy(p.Y, t.Y, sizeof(p.Y)); OPENSSL_memset(p.Z, 0, sizeof(p.Z)); @@ -451,7 +451,7 @@ static void ecp_nistz256_point_mul_base(const EC_GROUP *group, EC_JACOBIAN *r, ecp_nistz256_neg(neg_Y, t.Y); copy_conditional(t.Y, neg_Y, wvalue & 1); - // Note |ecp_nistz256_point_add_affine| does not work if |p| and |t| are the + // Note `ecp_nistz256_point_add_affine` does not work if `p` and `t` are the // same non-infinity point. ecp_nistz256_point_add_affine(&p, &p, &t); } @@ -478,9 +478,9 @@ static void ecp_nistz256_points_mul_public(const EC_GROUP *group, size_t index = 0; size_t wvalue = calc_first_wvalue(&index, p_str); - // Convert |p| from affine to Jacobian coordinates. We set Z to zero if |p| - // is infinity and |ONE_MONT| otherwise. |p| was computed from the table, so - // it is infinity iff |wvalue >> 1| is zero. + // Convert `p` from affine to Jacobian coordinates. We set Z to zero if `p` + // is infinity and `ONE_MONT` otherwise. `p` was computed from the table, so + // it is infinity iff `wvalue >> 1` is zero. if ((wvalue >> 1) != 0) { OPENSSL_memcpy(p.X, &ecp_nistz256_precomputed[0][(wvalue >> 1) - 1].X, sizeof(p.X)); @@ -510,9 +510,9 @@ static void ecp_nistz256_points_mul_public(const EC_GROUP *group, ecp_nistz256_neg(t.Y, t.Y); } - // Note |ecp_nistz256_point_add_affine| does not work if |p| and |t| are + // Note `ecp_nistz256_point_add_affine` does not work if `p` and `t` are // the same non-infinity point, so it is important that we compute the - // |g_scalar| term before the |p_scalar| term. + // `g_scalar` term before the `p_scalar` term. ecp_nistz256_point_add_affine(&p, &p, &t); } @@ -581,7 +581,7 @@ static void ecp_nistz256_dbl(const EC_GROUP *group, EC_JACOBIAN *r, static void ecp_nistz256_inv0_mod_ord(const EC_GROUP *group, EC_SCALAR *out, const EC_SCALAR *in) { - // table[i] stores a power of |in| corresponding to the matching enum value. + // table[i] stores a power of `in` corresponding to the matching enum value. enum { // The following indices specify the power in binary. i_1 = 0, @@ -642,7 +642,7 @@ static void ecp_nistz256_inv0_mod_ord(const EC_GROUP *group, EC_SCALAR *out, ecp_nistz256_ord_sqr_mont(table[i_x32], table[i_x16], 16); ecp_nistz256_ord_mul_mont(table[i_x32], table[i_x32], table[i_x16]); - // Compute |in| raised to the order-2. + // Compute `in` raised to the order-2. ecp_nistz256_ord_sqr_mont(out->words, table[i_x32], 64); ecp_nistz256_ord_mul_mont(out->words, out->words, table[i_x32]); static const struct { @@ -690,8 +690,8 @@ static int ecp_nistz256_cmp_x_coordinate(const EC_GROUP *group, assert(group->order.N.width == P256_LIMBS); assert(group->field.N.width == P256_LIMBS); - // We wish to compare X/Z^2 with r. This is equivalent to comparing X with - // r*Z^2. Note that X and Z are represented in Montgomery form, while r is + // We wish to compare X/Z² with r. This is equivalent to comparing X with + // r⋅Z². Note that X and Z are represented in Montgomery form, while r is // not. BN_ULONG r_Z2[P256_LIMBS], Z2_mont[P256_LIMBS], X[P256_LIMBS]; ecp_nistz256_mul_mont(Z2_mont, p->Z.words, p->Z.words); @@ -704,11 +704,11 @@ static int ecp_nistz256_cmp_x_coordinate(const EC_GROUP *group, // During signing the x coefficient is reduced modulo the group order. // Therefore there is a small possibility, less than 1/2^128, that group_order - // < p.x < P. in that case we need not only to compare against |r| but also to + // < p.x < P. in that case we need not only to compare against `r` but also to // compare against r+group_order. BN_ULONG carry = bn_add_words(r_Z2, r->words, group->order.N.d, P256_LIMBS); if (carry == 0 && bn_less_than_words(r_Z2, group->field.N.d, P256_LIMBS)) { - // r + group_order < p, so compare (r + group_order) * Z^2 against X. + // r + group_order < p, so compare (r + group_order) ⋅ Z² against X. ecp_nistz256_mul_mont(r_Z2, r_Z2, Z2_mont); if (OPENSSL_memcmp(r_Z2, X, sizeof(r_Z2)) == 0) { return 1; diff --git a/third_party/boringssl/src/crypto/fipsmodule/ec/p256-nistz.h b/third_party/boringssl/src/crypto/fipsmodule/ec/p256-nistz.h index c69638872..43da01cf1 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/ec/p256-nistz.h +++ b/third_party/boringssl/src/crypto/fipsmodule/ec/p256-nistz.h @@ -42,17 +42,17 @@ extern "C" { // P-256 field operations. // // An element mod P in P-256 is represented as a little-endian array of -// |P256_LIMBS| |BN_ULONG|s, spanning the full range of values. +// `P256_LIMBS` `BN_ULONG`s, spanning the full range of values. // // The following functions take fully-reduced inputs mod P and give // fully-reduced outputs. They may be used in-place. #define P256_LIMBS (256 / BN_BITS2) -// ecp_nistz256_neg sets |res| to -|a| mod P. +// ecp_nistz256_neg sets `res` to -`a` mod P. void ecp_nistz256_neg(BN_ULONG res[P256_LIMBS], const BN_ULONG a[P256_LIMBS]); -// ecp_nistz256_mul_mont sets |res| to |a| * |b| * 2^-256 mod P. +// ecp_nistz256_mul_mont sets `res` to `a` * `b` * 2^-256 mod P. #if defined(OPENSSL_X86_64) void ecp_nistz256_mul_mont_nohw(BN_ULONG res[P256_LIMBS], const BN_ULONG a[P256_LIMBS], @@ -66,7 +66,7 @@ void ecp_nistz256_mul_mont(BN_ULONG res[P256_LIMBS], const BN_ULONG b[P256_LIMBS]); #endif -// ecp_nistz256_sqr_mont sets |res| to |a| * |a| * 2^-256 mod P. +// ecp_nistz256_sqr_mont sets `res` to `a` * `a` * 2^-256 mod P. #if defined(OPENSSL_X86_64) void ecp_nistz256_sqr_mont_nohw(BN_ULONG res[P256_LIMBS], const BN_ULONG a[P256_LIMBS]); @@ -83,8 +83,8 @@ void ecp_nistz256_sqr_mont(BN_ULONG res[P256_LIMBS], // The following functions compute modulo N, where N is the order of P-256. They // take fully-reduced inputs and give fully-reduced outputs. -// ecp_nistz256_ord_mul_mont sets |res| to |a| * |b| where inputs and outputs -// are in Montgomery form. That is, |res| is |a| * |b| * 2^-256 mod N. +// ecp_nistz256_ord_mul_mont sets `res` to `a` * `b` where inputs and outputs +// are in Montgomery form. That is, `res` is `a` * `b` * 2^-256 mod N. #if defined(OPENSSL_X86_64) void ecp_nistz256_ord_mul_mont_nohw(BN_ULONG res[P256_LIMBS], const BN_ULONG a[P256_LIMBS], @@ -98,9 +98,9 @@ void ecp_nistz256_ord_mul_mont(BN_ULONG res[P256_LIMBS], const BN_ULONG b[P256_LIMBS]); #endif -// ecp_nistz256_ord_sqr_mont sets |res| to |a|^(2*|rep|) where inputs and -// outputs are in Montgomery form. That is, |res| is -// (|a| * 2^-256)^(2*|rep|) * 2^256 mod N. +// ecp_nistz256_ord_sqr_mont sets `res` to `a`^(2*`rep`) where inputs and +// outputs are in Montgomery form. That is, `res` is +// (`a` * 2^-256)^(2*`rep`) * 2^256 mod N. #if defined(OPENSSL_X86_64) void ecp_nistz256_ord_sqr_mont_nohw(BN_ULONG res[P256_LIMBS], const BN_ULONG a[P256_LIMBS], BN_ULONG rep); @@ -137,8 +137,8 @@ typedef struct { BN_ULONG Y[P256_LIMBS]; } P256_POINT_AFFINE; -// ecp_nistz256_select_w5 sets |*val| to |in_t[index-1]| if 1 <= |index| <= 16 -// and all zeros (the point at infinity) if |index| is 0. This is done in +// ecp_nistz256_select_w5 sets `*val` to `in_t[index-1]` if 1 <= `index` <= 16 +// and all zeros (the point at infinity) if `index` is 0. This is done in // constant time. #if defined(OPENSSL_X86_64) void ecp_nistz256_select_w5_nohw(P256_POINT *val, const P256_POINT in_t[16], @@ -150,8 +150,8 @@ void ecp_nistz256_select_w5(P256_POINT *val, const P256_POINT in_t[16], int index); #endif -// ecp_nistz256_select_w7 sets |*val| to |in_t[index-1]| if 1 <= |index| <= 64 -// and all zeros (the point at infinity) if |index| is 0. This is done in +// ecp_nistz256_select_w7 sets `*val` to `in_t[index-1]` if 1 <= `index` <= 64 +// and all zeros (the point at infinity) if `index` is 0. This is done in // constant time. #if defined(OPENSSL_X86_64) void ecp_nistz256_select_w7_nohw(P256_POINT_AFFINE *val, @@ -163,7 +163,7 @@ void ecp_nistz256_select_w7(P256_POINT_AFFINE *val, const P256_POINT_AFFINE in_t[64], int index); #endif -// ecp_nistz256_point_double sets |r| to |a| doubled. +// ecp_nistz256_point_double sets `r` to `a` doubled. #if defined(OPENSSL_X86_64) void ecp_nistz256_point_double_nohw(P256_POINT *r, const P256_POINT *a); void ecp_nistz256_point_double_adx(P256_POINT *r, const P256_POINT *a); @@ -171,7 +171,7 @@ void ecp_nistz256_point_double_adx(P256_POINT *r, const P256_POINT *a); void ecp_nistz256_point_double(P256_POINT *r, const P256_POINT *a); #endif -// ecp_nistz256_point_add adds |a| to |b| and places the result in |r|. +// ecp_nistz256_point_add adds `a` to `b` and places the result in `r`. #if defined(OPENSSL_X86_64) void ecp_nistz256_point_add_nohw(P256_POINT *r, const P256_POINT *a, const P256_POINT *b); @@ -182,8 +182,8 @@ void ecp_nistz256_point_add(P256_POINT *r, const P256_POINT *a, const P256_POINT *b); #endif -// ecp_nistz256_point_add_affine adds |a| to |b| and places the result in -// |r|. |a| and |b| must not represent the same point unless they are both +// ecp_nistz256_point_add_affine adds `a` to `b` and places the result in +// `r`. `a` and `b` must not represent the same point unless they are both // infinity. #if defined(OPENSSL_X86_64) void ecp_nistz256_point_add_affine_adx(P256_POINT *r, const P256_POINT *a, diff --git a/third_party/boringssl/src/crypto/fipsmodule/ec/p256.cc.inc b/third_party/boringssl/src/crypto/fipsmodule/ec/p256.cc.inc index 49235c6b5..1bd5c13f9 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/ec/p256.cc.inc +++ b/third_party/boringssl/src/crypto/fipsmodule/ec/p256.cc.inc @@ -71,9 +71,9 @@ static void fiat_p256_cmovznz(fiat_p256_limb_t out[FIAT_P256_NLIMBS], static void fiat_p256_from_words(fiat_p256_felem out, const BN_ULONG in[32 / sizeof(BN_ULONG)]) { - // Typically, |BN_ULONG| and |fiat_p256_limb_t| will be the same type, but on - // 64-bit platforms without |uint128_t|, they are different. However, on - // little-endian systems, |uint64_t[4]| and |uint32_t[8]| have the same + // Typically, `BN_ULONG` and `fiat_p256_limb_t` will be the same type, but on + // 64-bit platforms without `uint128_t`, they are different. However, on + // little-endian systems, `uint64_t[4]` and `uint32_t[8]` have the same // layout. OPENSSL_memcpy(out, in, 32); } @@ -83,11 +83,11 @@ static void fiat_p256_from_generic(fiat_p256_felem out, const EC_FELEM *in) { } static void fiat_p256_to_generic(EC_FELEM *out, const fiat_p256_felem in) { - // See |fiat_p256_from_words|. + // See `fiat_p256_from_words`. OPENSSL_memcpy(out->words, in, 32); } -// fiat_p256_inv_square calculates |out| = |in|^{-2} +// fiat_p256_inv_square calculates `out` = `in`^{-2} // // Based on Fermat's Little Theorem: // a^p = a (mod p) @@ -98,17 +98,17 @@ static void fiat_p256_inv_square(fiat_p256_felem out, // This implements the addition chain described in // https://briansmith.org/ecc-inversion-addition-chains-01#p256_field_inversion fiat_p256_felem x2, x3, x6, x12, x15, x30, x32; - fiat_p256_square(x2, in); // 2^2 - 2^1 - fiat_p256_mul(x2, x2, in); // 2^2 - 2^0 + fiat_p256_square(x2, in); // 2² - 2¹ + fiat_p256_mul(x2, x2, in); // 2² - 2⁰ - fiat_p256_square(x3, x2); // 2^3 - 2^1 - fiat_p256_mul(x3, x3, in); // 2^3 - 2^0 + fiat_p256_square(x3, x2); // 2³ - 2¹ + fiat_p256_mul(x3, x3, in); // 2³ - 2⁰ fiat_p256_square(x6, x3); for (int i = 1; i < 3; i++) { fiat_p256_square(x6, x6); - } // 2^6 - 2^3 - fiat_p256_mul(x6, x6, x3); // 2^6 - 2^0 + } // 2⁶ - 2³ + fiat_p256_mul(x6, x6, x3); // 2⁶ - 2⁰ fiat_p256_square(x12, x6); for (int i = 1; i < 6; i++) { @@ -204,8 +204,8 @@ static void fiat_p256_point_add(fiat_p256_felem x3, fiat_p256_felem y3, } #include "./p256_table.h" -// fiat_p256_select_point_affine selects the |idx-1|th point from a -// precomputation table and copies it to out. If |idx| is zero, the output is +// fiat_p256_select_point_affine selects the `idx-1`th point from a +// precomputation table and copies it to out. If `idx` is zero, the output is // the point at infinity. static void fiat_p256_select_point_affine( const fiat_p256_limb_t idx, size_t size, @@ -219,7 +219,7 @@ static void fiat_p256_select_point_affine( fiat_p256_cmovznz(out[2], idx, out[2], fiat_p256_one); } -// fiat_p256_select_point selects the |idx|th point from a precomputation table +// fiat_p256_select_point selects the `idx`th point from a precomputation table // and copies it to out. static void fiat_p256_select_point(const fiat_p256_limb_t idx, size_t size, const fiat_p256_felem pre_comp[/*size*/][3], @@ -233,7 +233,7 @@ static void fiat_p256_select_point(const fiat_p256_limb_t idx, size_t size, } } -// fiat_p256_get_bit returns the |i|th bit in |in|. +// fiat_p256_get_bit returns the `i`th bit in `in`. static crypto_word_t fiat_p256_get_bit(const EC_SCALAR *in, int i) { if (i < 0 || i >= 256) { return 0; @@ -250,7 +250,7 @@ static crypto_word_t fiat_p256_get_bit(const EC_SCALAR *in, int i) { // OPENSSL EC_METHOD FUNCTIONS // Takes the Jacobian coordinates (X, Y, Z) of a point and returns (X', Y') = -// (X/Z^2, Y/Z^3). +// (X/Z², Y/Z³). static int ec_GFp_nistp256_point_get_affine_coordinates( const EC_GROUP *group, const EC_JACOBIAN *point, EC_FELEM *x_out, EC_FELEM *y_out) { @@ -335,7 +335,7 @@ static void ec_GFp_nistp256_point_mul(const EC_GROUP *group, EC_JACOBIAN *r, // Set nq to the point at infinity. fiat_p256_felem nq[3] = {{0}, {0}, {0}}, ftmp, tmp[3]; - // Loop over |scalar| msb-to-lsb, incorporating |p_pre_comp| every 5th round. + // Loop over `scalar` msb-to-lsb, incorporating `p_pre_comp` every 5th round. int skip = 1; // Save two point operations in the first round. for (size_t i = 255; i < 256; i--) { // double @@ -431,7 +431,7 @@ static void ec_GFp_nistp256_point_mul_public(const EC_GROUP *group, const EC_JACOBIAN *p, const EC_SCALAR *p_scalar) { #define P256_WSIZE_PUBLIC 4 - // Precompute multiples of |p|. p_pre_comp[i] is (2*i+1) * |p|. + // Precompute multiples of `p`. p_pre_comp[i] is (2*i+1) * `p`. fiat_p256_felem p_pre_comp[1 << (P256_WSIZE_PUBLIC - 1)][3]; fiat_p256_from_generic(p_pre_comp[0][0], &p->X); fiat_p256_from_generic(p_pre_comp[0][1], &p->Y); @@ -445,11 +445,11 @@ static void ec_GFp_nistp256_point_mul_public(const EC_GROUP *group, p_pre_comp[i - 1][2], p2[0], p2[1], p2[2]); } - // Set up the coefficients for |p_scalar|. + // Set up the coefficients for `p_scalar`. int8_t p_wNAF[257]; ec_compute_wNAF(group, p_wNAF, p_scalar, 256, P256_WSIZE_PUBLIC); - // Set |ret| to the point at infinity. + // Set `ret` to the point at infinity. int skip = 1; // Save some point operations. fiat_p256_felem ret[3] = {{0}, {0}, {0}}; for (int i = 256; i >= 0; i--) { @@ -457,7 +457,7 @@ static void ec_GFp_nistp256_point_mul_public(const EC_GROUP *group, fiat_p256_point_double(ret[0], ret[1], ret[2], ret[0], ret[1], ret[2]); } - // For the |g_scalar|, we use the precomputed table without the + // For the `g_scalar`, we use the precomputed table without the // constant-time lookup. if (i <= 31) { // First, look 32 bits upwards. @@ -520,8 +520,8 @@ static int ec_GFp_nistp256_cmp_x_coordinate(const EC_GROUP *group, return 0; } - // We wish to compare X/Z^2 with r. This is equivalent to comparing X with - // r*Z^2. Note that X and Z are represented in Montgomery form, while r is + // We wish to compare X/Z² with r. This is equivalent to comparing X with + // r⋅Z². Note that X and Z are represented in Montgomery form, while r is // not. fiat_p256_felem Z2_mont; fiat_p256_from_generic(Z2_mont, &p->Z); @@ -541,7 +541,7 @@ static int ec_GFp_nistp256_cmp_x_coordinate(const EC_GROUP *group, // During signing the x coefficient is reduced modulo the group order. // Therefore there is a small possibility, less than 1/2^128, that group_order - // < p.x < P. in that case we need not only to compare against |r| but also to + // < p.x < P. in that case we need not only to compare against `r` but also to // compare against r+group_order. assert(group->field.N.width == group->order.N.width); EC_FELEM tmp; diff --git a/third_party/boringssl/src/crypto/fipsmodule/ec/scalar.cc.inc b/third_party/boringssl/src/crypto/fipsmodule/ec/scalar.cc.inc index 958132584..f8b064294 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/ec/scalar.cc.inc +++ b/third_party/boringssl/src/crypto/fipsmodule/ec/scalar.cc.inc @@ -164,8 +164,8 @@ int bssl::ec_simple_scalar_to_montgomery_inv_vartime(const EC_GROUP *group, // even though for this interface it is not mandatory. // r = a^-1 in the Montgomery domain. This is - // |ec_scalar_to_montgomery| followed by |ec_scalar_inv0_montgomery|, but - // |ec_scalar_inv0_montgomery| followed by |ec_scalar_from_montgomery| is + // `ec_scalar_to_montgomery` followed by `ec_scalar_inv0_montgomery`, but + // `ec_scalar_inv0_montgomery` followed by `ec_scalar_from_montgomery` is // equivalent and slightly more efficient. ec_scalar_inv0_montgomery(group, r, a); ec_scalar_from_montgomery(group, r, r); diff --git a/third_party/boringssl/src/crypto/fipsmodule/ec/simple.cc.inc b/third_party/boringssl/src/crypto/fipsmodule/ec/simple.cc.inc index bd3dd1e4e..7fdb5e4dd 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/ec/simple.cc.inc +++ b/third_party/boringssl/src/crypto/fipsmodule/ec/simple.cc.inc @@ -64,12 +64,11 @@ int bssl::ec_GFp_simple_group_set_curve(EC_GROUP *group, const BIGNUM *p, return 0; } - // group->a_is_minus3 - if (!BN_copy(tmp, a) || - !BN_add_word(tmp, 3)) { + if (!BN_copy(tmp, a) || !BN_add_word(tmp, 3)) { return 0; } group->a_is_minus3 = (0 == BN_cmp(tmp, &group->field.N)); + group->field_is_3_mod_4 = (group->field.N.d[0] & 3) == 3; return 1; } @@ -99,7 +98,7 @@ void bssl::ec_GFp_simple_point_copy(EC_JACOBIAN *dest, const EC_JACOBIAN *src) { void bssl::ec_GFp_simple_point_set_to_infinity(const EC_GROUP *group, EC_JACOBIAN *point) { // Although it is strictly only necessary to zero Z, we zero the entire point - // in case |point| was stack-allocated and yet to be initialized. + // in case `point` was stack-allocated and yet to be initialized. ec_GFp_simple_point_init(point); } @@ -115,18 +114,18 @@ int bssl::ec_GFp_simple_is_at_infinity(const EC_GROUP *group, int bssl::ec_GFp_simple_is_on_curve(const EC_GROUP *group, const EC_JACOBIAN *point) { // We have a curve defined by a Weierstrass equation - // y^2 = x^3 + a*x + b. + // y² = x³ + a⋅x + b. // The point to consider is given in Jacobian projective coordinates - // where (X, Y, Z) represents (x, y) = (X/Z^2, Y/Z^3). - // Substituting this and multiplying by Z^6 transforms the above equation + // where (X, Y, Z) represents (x, y) = (X/Z², Y/Z³). + // Substituting this and multiplying by Z⁶ transforms the above equation // into - // Y^2 = X^3 + a*X*Z^4 + b*Z^6. + // Y² = X³ + a⋅X⋅Z⁴ + b⋅Z⁶. // To test this, we add up the right-hand side in 'rh'. // // This function may be used when double-checking the secret result of a point // multiplication, so we proceed in constant-time. - // rh := X^2 + // rh := X² EC_FELEM rh; ec_felem_sqr(group, &rh, &point->X); @@ -135,7 +134,7 @@ int bssl::ec_GFp_simple_is_on_curve(const EC_GROUP *group, ec_felem_sqr(group, &Z4, &tmp); ec_felem_mul(group, &Z6, &Z4, &tmp); - // rh := rh + a*Z^4 + // rh := rh + a⋅Z⁴ if (group->a_is_minus3) { ec_felem_add(group, &tmp, &Z4, &Z4); ec_felem_add(group, &tmp, &tmp, &Z4); @@ -145,14 +144,14 @@ int bssl::ec_GFp_simple_is_on_curve(const EC_GROUP *group, ec_felem_add(group, &rh, &rh, &tmp); } - // rh := (rh + a*Z^4)*X + // rh := (rh + a⋅Z⁴)⋅X ec_felem_mul(group, &rh, &rh, &point->X); - // rh := rh + b*Z^6 + // rh := rh + b⋅Z⁶ ec_felem_mul(group, &tmp, &group->b, &Z6); ec_felem_add(group, &rh, &rh, &tmp); - // 'lh' := Y^2 + // 'lh' := Y² ec_felem_sqr(group, &tmp, &point->Y); ec_felem_sub(group, &tmp, &tmp, &rh); @@ -177,23 +176,23 @@ int bssl::ec_GFp_simple_points_equal(const EC_GROUP *group, // restore this optimization by keeping better track of affine vs. Jacobian // forms. See https://crbug.com/boringssl/326. - // If neither |a| or |b| is infinity, we have to decide whether - // (X_a/Z_a^2, Y_a/Z_a^3) = (X_b/Z_b^2, Y_b/Z_b^3), + // If neither `a` or `b` is infinity, we have to decide whether + // (X_a/Z_a², Y_a/Z_a³) = (X_b/Z_b², Y_b/Z_b³), // or equivalently, whether - // (X_a*Z_b^2, Y_a*Z_b^3) = (X_b*Z_a^2, Y_b*Z_a^3). + // (X_a⋅Z_b², Y_a⋅Z_b³) = (X_b⋅Z_a², Y_b⋅Z_a³). EC_FELEM tmp1, tmp2, Za23, Zb23; - ec_felem_sqr(group, &Zb23, &b->Z); // Zb23 = Z_b^2 - ec_felem_mul(group, &tmp1, &a->X, &Zb23); // tmp1 = X_a * Z_b^2 - ec_felem_sqr(group, &Za23, &a->Z); // Za23 = Z_a^2 - ec_felem_mul(group, &tmp2, &b->X, &Za23); // tmp2 = X_b * Z_a^2 + ec_felem_sqr(group, &Zb23, &b->Z); // Zb23 = Z_b² + ec_felem_mul(group, &tmp1, &a->X, &Zb23); // tmp1 = X_a ⋅ Z_b² + ec_felem_sqr(group, &Za23, &a->Z); // Za23 = Z_a² + ec_felem_mul(group, &tmp2, &b->X, &Za23); // tmp2 = X_b ⋅ Z_a² ec_felem_sub(group, &tmp1, &tmp1, &tmp2); const BN_ULONG x_not_equal = ec_felem_non_zero_mask(group, &tmp1); - ec_felem_mul(group, &Zb23, &Zb23, &b->Z); // Zb23 = Z_b^3 - ec_felem_mul(group, &tmp1, &a->Y, &Zb23); // tmp1 = Y_a * Z_b^3 - ec_felem_mul(group, &Za23, &Za23, &a->Z); // Za23 = Z_a^3 - ec_felem_mul(group, &tmp2, &b->Y, &Za23); // tmp2 = Y_b * Z_a^3 + ec_felem_mul(group, &Zb23, &Zb23, &b->Z); // Zb23 = Z_b³ + ec_felem_mul(group, &tmp1, &a->Y, &Zb23); // tmp1 = Y_a ⋅ Z_b³ + ec_felem_mul(group, &Za23, &Za23, &a->Z); // Za23 = Z_a³ + ec_felem_mul(group, &tmp2, &b->Y, &Za23); // tmp2 = Y_b ⋅ Z_a³ ec_felem_sub(group, &tmp1, &tmp1, &tmp2); const BN_ULONG y_not_equal = ec_felem_non_zero_mask(group, &tmp1); const BN_ULONG x_and_y_equal = ~(x_not_equal | y_not_equal); @@ -209,19 +208,19 @@ int bssl::ec_GFp_simple_points_equal(const EC_GROUP *group, int bssl::ec_affine_jacobian_equal(const EC_GROUP *group, const EC_AFFINE *a, const EC_JACOBIAN *b) { - // If |b| is not infinity, we have to decide whether - // (X_a, Y_a) = (X_b/Z_b^2, Y_b/Z_b^3), + // If `b` is not infinity, we have to decide whether + // (X_a, Y_a) = (X_b/Z_b², Y_b/Z_b³), // or equivalently, whether - // (X_a*Z_b^2, Y_a*Z_b^3) = (X_b, Y_b). + // (X_a⋅Z_b², Y_a⋅Z_b³) = (X_b, Y_b). EC_FELEM tmp, Zb2; - ec_felem_sqr(group, &Zb2, &b->Z); // Zb2 = Z_b^2 - ec_felem_mul(group, &tmp, &a->X, &Zb2); // tmp = X_a * Z_b^2 + ec_felem_sqr(group, &Zb2, &b->Z); // Zb2 = Z_b² + ec_felem_mul(group, &tmp, &a->X, &Zb2); // tmp = X_a ⋅ Z_b² ec_felem_sub(group, &tmp, &tmp, &b->X); const BN_ULONG x_not_equal = ec_felem_non_zero_mask(group, &tmp); - ec_felem_mul(group, &tmp, &a->Y, &Zb2); // tmp = Y_a * Z_b^2 - ec_felem_mul(group, &tmp, &tmp, &b->Z); // tmp = Y_a * Z_b^3 + ec_felem_mul(group, &tmp, &a->Y, &Zb2); // tmp = Y_a ⋅ Z_b² + ec_felem_mul(group, &tmp, &tmp, &b->Z); // tmp = Y_a ⋅ Z_b³ ec_felem_sub(group, &tmp, &tmp, &b->Y); const BN_ULONG y_not_equal = ec_felem_non_zero_mask(group, &tmp); const BN_ULONG x_and_y_equal = ~(x_not_equal | y_not_equal); @@ -236,7 +235,7 @@ int bssl::ec_GFp_simple_cmp_x_coordinate(const EC_GROUP *group, const EC_JACOBIAN *p, const EC_SCALAR *r) { if (ec_GFp_simple_is_at_infinity(group, p)) { - // |ec_get_x_coordinate_as_scalar| will check this internally, but this way + // `ec_get_x_coordinate_as_scalar` will check this internally, but this way // we do not push to the error queue. return 0; } diff --git a/third_party/boringssl/src/crypto/fipsmodule/ec/simple_mul.cc.inc b/third_party/boringssl/src/crypto/fipsmodule/ec/simple_mul.cc.inc index e4a665f9d..baff127af 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/ec/simple_mul.cc.inc +++ b/third_party/boringssl/src/crypto/fipsmodule/ec/simple_mul.cc.inc @@ -30,9 +30,9 @@ void bssl::ec_GFp_mont_mul(const EC_GROUP *group, EC_JACOBIAN *r, const EC_JACOBIAN *p, const EC_SCALAR *scalar) { // This is a generic implementation for uncommon curves that not do not // warrant a tuned one. It uses unsigned digits so that the doubling case in - // |ec_GFp_mont_add| is always unreachable, erring on safety and simplicity. + // `ec_GFp_mont_add` is always unreachable, erring on safety and simplicity. - // Compute a table of the first 32 multiples of |p| (including infinity). + // Compute a table of the first 32 multiples of `p` (including infinity). EC_JACOBIAN precomp[32]; ec_GFp_simple_point_set_to_infinity(group, &precomp[0]); ec_GFp_simple_point_copy(&precomp[1], p); @@ -44,7 +44,7 @@ void bssl::ec_GFp_mont_mul(const EC_GROUP *group, EC_JACOBIAN *r, } } - // Divide bits in |scalar| into windows. + // Divide bits in `scalar` into windows. unsigned bits = EC_GROUP_order_bits(group); int r_is_at_infinity = 1; for (unsigned i = bits - 1; i < bits; i--) { @@ -142,7 +142,7 @@ void bssl::ec_GFp_mont_mul_batch( ec_GFp_mont_batch_precomp(group, precomp[2], 17, p2); } - // Divide bits in |scalar| into windows. + // Divide bits in `scalar` into windows. unsigned bits = EC_GROUP_order_bits(group); int r_is_at_infinity = 1; for (unsigned i = bits; i <= bits; i--) { @@ -183,7 +183,7 @@ int bssl::ec_GFp_mont_init_precomp(const EC_GROUP *group, EC_PRECOMP *out, // comb[i - 1] stores the ith element of the comb. That is, if i is // b4 * 2^4 + b3 * 2^3 + ... + b0 * 2^0, it stores k * |p|, where k is // b4 * 2^(4*stride) + b3 * 2^(3*stride) + ... + b0 * 2^(0*stride). stride - // here is |ec_GFp_mont_comb_stride|. We store at index i - 1 because the 0th + // here is `ec_GFp_mont_comb_stride`. We store at index i - 1 because the 0th // comb entry is always infinity. EC_JACOBIAN comb[(1 << EC_MONT_PRECOMP_COMB_SIZE) - 1]; unsigned stride = ec_GFp_mont_comb_stride(group); @@ -192,7 +192,7 @@ int bssl::ec_GFp_mont_init_precomp(const EC_GROUP *group, EC_PRECOMP *out, // entries up to 2^0 are filled. comb[(1 << 0) - 1] = *p; for (unsigned i = 1; i < EC_MONT_PRECOMP_COMB_SIZE; i++) { - // Compute entry 2^i by doubling the entry for 2^(i-1) |stride| times. + // Compute entry 2^i by doubling the entry for 2^(i-1) `stride` times. unsigned bit = 1 << i; ec_GFp_mont_dbl(group, &comb[bit - 1], &comb[bit / 2 - 1]); for (unsigned j = 1; j < stride; j++) { @@ -219,15 +219,15 @@ static void ec_GFp_mont_get_comb_window(const EC_GROUP *group, const EC_SCALAR *scalar, unsigned i) { const size_t width = group->order.N.width; unsigned stride = ec_GFp_mont_comb_stride(group); - // Select the bits corresponding to the comb shifted up by |i|. + // Select the bits corresponding to the comb shifted up by `i`. unsigned window = 0; for (unsigned j = 0; j < EC_MONT_PRECOMP_COMB_SIZE; j++) { window |= bn_is_bit_set_words(scalar->words, width, j * stride + i) << j; } - // Select precomp->comb[window - 1]. If |window| is zero, |match| will always - // be zero, which will leave |out| at infinity. + // Select precomp->comb[window - 1]. If `window` is zero, `match` will always + // be zero, which will leave `out` at infinity. OPENSSL_memset(out, 0, sizeof(EC_JACOBIAN)); for (unsigned j = 0; j < std::size(precomp->comb); j++) { BN_ULONG match = constant_time_eq_w(window, j + 1); diff --git a/third_party/boringssl/src/crypto/fipsmodule/ec/util.cc.inc b/third_party/boringssl/src/crypto/fipsmodule/ec/util.cc.inc index 5b98ae09b..8ca2406f5 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/ec/util.cc.inc +++ b/third_party/boringssl/src/crypto/fipsmodule/ec/util.cc.inc @@ -110,7 +110,7 @@ using namespace bssl; // is a prime that is much larger than 2^w. It also only holds when windows // are applied from most significant to least significant, doubling between each // window. It does not apply to more complex table strategies such as -// |EC_GFp_nistz256_method|. +// `EC_GFp_nistz256_method`. // // PROOF: // diff --git a/third_party/boringssl/src/crypto/fipsmodule/ec/wnaf.cc.inc b/third_party/boringssl/src/crypto/fipsmodule/ec/wnaf.cc.inc index 74def20d3..2592e7985 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/ec/wnaf.cc.inc +++ b/third_party/boringssl/src/crypto/fipsmodule/ec/wnaf.cc.inc @@ -85,8 +85,8 @@ void bssl::ec_compute_wNAF(const EC_GROUP *group, int8_t *out, out[j] = digit; - // Incorporate the next bit. Previously, |window_val| <= |next_bit|, so if - // we shift and add at most one copy of |bit|, this will continue to hold + // Incorporate the next bit. Previously, `window_val` <= `next_bit`, so if + // we shift and add at most one copy of `bit`, this will continue to hold // afterwards. window_val >>= 1; window_val += bit * bn_is_bit_set_words(scalar->words, group->order.N.width, @@ -98,7 +98,7 @@ void bssl::ec_compute_wNAF(const EC_GROUP *group, int8_t *out, assert(window_val == 0); } -// compute_precomp sets |out[i]| to (2*i+1)*p, for i from 0 to |len|. +// compute_precomp sets `out[i]` to (2*i+1)*p, for i from 0 to `len`. static void compute_precomp(const EC_GROUP *group, EC_JACOBIAN *out, const EC_JACOBIAN *p, size_t len) { ec_GFp_simple_point_copy(&out[0], p); @@ -120,10 +120,10 @@ static void lookup_precomp(const EC_GROUP *group, EC_JACOBIAN *out, } } -// EC_WNAF_WINDOW_BITS is the window size to use for |ec_GFp_mont_mul_public|. +// EC_WNAF_WINDOW_BITS is the window size to use for `ec_GFp_mont_mul_public`. #define EC_WNAF_WINDOW_BITS 4 -// EC_WNAF_TABLE_SIZE is the table size to use for |ec_GFp_mont_mul_public|. +// EC_WNAF_TABLE_SIZE is the table size to use for `ec_GFp_mont_mul_public`. #define EC_WNAF_TABLE_SIZE (1 << (EC_WNAF_WINDOW_BITS - 1)) // EC_WNAF_STACK is the number of points worth of data to stack-allocate and diff --git a/third_party/boringssl/src/crypto/fipsmodule/ecdsa/ecdsa.cc.inc b/third_party/boringssl/src/crypto/fipsmodule/ecdsa/ecdsa.cc.inc index 463fc669c..ce0dae2b0 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/ecdsa/ecdsa.cc.inc +++ b/third_party/boringssl/src/crypto/fipsmodule/ecdsa/ecdsa.cc.inc @@ -31,7 +31,7 @@ using namespace bssl; -// digest_to_scalar interprets |digest_len| bytes from |digest| as a scalar for +// digest_to_scalar interprets `digest_len` bytes from `digest` as a scalar for // ECDSA. static void digest_to_scalar(const EC_GROUP *group, EC_SCALAR *out, const uint8_t *digest, size_t digest_len) { @@ -49,8 +49,8 @@ static void digest_to_scalar(const EC_GROUP *group, EC_SCALAR *out, bn_rshift_words(out->words, out->words, 8 - (num_bits & 0x7), order->width); } - // |out| now has the same bit width as |order|, but this only bounds by - // 2*|order|. Subtract the order if out of range. + // `out` now has the same bit width as `order`, but this only bounds by + // 2*`order`. Subtract the order if out of range. // // Montgomery multiplication accepts the looser bounds, so this isn't strictly // necessary, but it is a cleaner abstraction and has no performance impact. @@ -89,8 +89,8 @@ int bssl::ecdsa_verify_fixed_no_self_test(const uint8_t *digest, // u1 = m * s^-1 mod order // u2 = r * s^-1 mod order // - // |s_inv_mont| is in Montgomery form while |m| and |r| are not, so |u1| and - // |u2| will be taken out of Montgomery form, as desired. + // `s_inv_mont` is in Montgomery form while `m` and `r` are not, so `u1` and + // `u2` will be taken out of Montgomery form, as desired. digest_to_scalar(group, &m, digest, digest_len); ec_scalar_mul_montgomery(group, &u1, &m, &s_inv_mont); ec_scalar_mul_montgomery(group, &u2, &r, &s_inv_mont); @@ -144,7 +144,7 @@ static int ecdsa_sign_impl(const EC_GROUP *group, int *out_retry, uint8_t *sig, } // s = priv_key * r. Note if only one parameter is in the Montgomery domain, - // |ec_scalar_mod_mul_montgomery| will compute the answer in the normal + // `ec_scalar_mod_mul_montgomery` will compute the answer in the normal // domain. EC_SCALAR s; ec_scalar_to_montgomery(group, &s, &r); @@ -156,13 +156,13 @@ static int ecdsa_sign_impl(const EC_GROUP *group, int *out_retry, uint8_t *sig, ec_scalar_add(group, &s, &s, &tmp); // s = k^-1 * (m + priv_key * r). First, we compute k^-1 in the Montgomery - // domain. This is |ec_scalar_to_montgomery| followed by - // |ec_scalar_inv0_montgomery|, but |ec_scalar_inv0_montgomery| followed by - // |ec_scalar_from_montgomery| is equivalent and slightly more efficient. + // domain. This is `ec_scalar_to_montgomery` followed by + // `ec_scalar_inv0_montgomery`, but `ec_scalar_inv0_montgomery` followed by + // `ec_scalar_from_montgomery` is equivalent and slightly more efficient. // Then, as above, only one parameter is in the Montgomery domain, so the // result is in the normal domain. Finally, note k is non-zero (or computing r // would fail), so the inverse must exist. - ec_scalar_inv0_montgomery(group, &tmp, k); // tmp = k^-1 R^2 + ec_scalar_inv0_montgomery(group, &tmp, k); // tmp = k^-1 R² ec_scalar_from_montgomery(group, &tmp, &tmp); // tmp = k^-1 R ec_scalar_mul_montgomery(group, &s, &s, &tmp); if (constant_time_declassify_int(ec_scalar_is_zero(group, &s))) { @@ -264,7 +264,7 @@ int bssl::ecdsa_sign_fixed(const uint8_t *digest, size_t digest_len, goto out; } - // TODO(davidben): Move this inside |ec_random_nonzero_scalar| or lower, so + // TODO(davidben): Move this inside `ec_random_nonzero_scalar` or lower, so // that all scalars we generate are, by default, secret. CONSTTIME_SECRET(k.words, sizeof(k.words)); diff --git a/third_party/boringssl/src/crypto/fipsmodule/ecdsa/internal.h b/third_party/boringssl/src/crypto/fipsmodule/ecdsa/internal.h index 622b688c6..90cf9c504 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/ecdsa/internal.h +++ b/third_party/boringssl/src/crypto/fipsmodule/ecdsa/internal.h @@ -26,21 +26,21 @@ BSSL_NAMESPACE_BEGIN // fixed-width, big-endian format from IEEE P1363. #define ECDSA_MAX_FIXED_LEN (2 * EC_MAX_BYTES) -// ecdsa_sign_fixed behaves like |ECDSA_sign| but uses the fixed-width, +// ecdsa_sign_fixed behaves like `ECDSA_sign` but uses the fixed-width, // big-endian format from IEEE P1363. int ecdsa_sign_fixed(const uint8_t *digest, size_t digest_len, uint8_t *sig, size_t *out_sig_len, size_t max_sig_len, const EC_KEY *key); // ecdsa_sign_fixed_with_nonce_for_known_answer_test behaves like -// |ecdsa_sign_fixed| but takes a caller-supplied nonce. This function is used +// `ecdsa_sign_fixed` but takes a caller-supplied nonce. This function is used // as part of known-answer tests in the FIPS module. int ecdsa_sign_fixed_with_nonce_for_known_answer_test( const uint8_t *digest, size_t digest_len, uint8_t *sig, size_t *out_sig_len, size_t max_sig_len, const EC_KEY *key, const uint8_t *nonce, size_t nonce_len); -// ecdsa_verify_fixed behaves like |ECDSA_verify| but uses the fixed-width, +// ecdsa_verify_fixed behaves like `ECDSA_verify` but uses the fixed-width, // big-endian format from IEEE P1363. int ecdsa_verify_fixed(const uint8_t *digest, size_t digest_len, const uint8_t *sig, size_t sig_len, const EC_KEY *key); diff --git a/third_party/boringssl/src/crypto/fipsmodule/entropy/jitter.cc.inc b/third_party/boringssl/src/crypto/fipsmodule/entropy/jitter.cc.inc index 77d278bb1..8c751665f 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/entropy/jitter.cc.inc +++ b/third_party/boringssl/src/crypto/fipsmodule/entropy/jitter.cc.inc @@ -32,6 +32,7 @@ #include +#include "../../internal.h" #include "internal.h" #include "sha512.cc.inc" @@ -88,6 +89,11 @@ class MemoryAccessSampler { array_size_ & (array_size_ - 1)) { abort(); } + + if (boringssl_fips_break_test("JITTER_APT")) { + // In this break test, array_[0] is used to keep state. See below. + array_[0] = 0; + } } ~MemoryAccessSampler() { OPENSSL_free(const_cast(array_)); } @@ -104,16 +110,28 @@ class MemoryAccessSampler { } bool Next(uint64_t *out) { - // Perform some memory accesses and measure how long it took. The LCG is - // intended to defeat any CPU predictors and thus expose this code to as - // much system entropy as possible. - for (unsigned i = 0; i < num_samples_; i++) { - // The lower bits of an LCG tend to fall into short cycles and so are - // discarded here. - array_[(lcg_.Next() >> 6) & (array_size_ - 1)] += 1; - } + if (boringssl_fips_break_test("JITTER_RCT")) { + // To break the repetition test a constant value is returned. + *out = 1; + } else if (boringssl_fips_break_test("JITTER_APT")) { + // To break the adaptive test, but not the repetition test, a counter + // that advances every fourth call is returned. Since samples are + // processed by `DeltaSampler`, the tests see 0,0,0,1,... which exceeds + // the APT cutoff (348/512) without tripping the RCT (26). + *out = array_[0] >> 2; + array_[0] += 1; + } else { + // Perform some memory accesses and measure how long it took. The LCG is + // intended to defeat any CPU predictors and thus expose this code to as + // much system entropy as possible. + for (unsigned i = 0; i < num_samples_; i++) { + // The lower bits of an LCG tend to fall into short cycles and so are + // discarded here. + array_[(lcg_.Next() >> 6) & (array_size_ - 1)] += 1; + } - *out = GetTimestamp(); + *out = GetTimestamp(); + } return true; } @@ -226,6 +244,9 @@ class RepetitionCountTest { last_sample_ = sample; } if (count_ >= kThreshold) { +#if defined(BORINGSSL_FIPS_BREAK_TESTS) + fprintf(stderr, "Jitter RCT failure.\n"); +#endif return false; } *out = sample; @@ -281,7 +302,7 @@ constexpr unsigned CritBinom(unsigned trials, double probability_s, return trials; } -// ExpTaylor calculates e^x using the Taylor series: e^x = 1 + x + x²/2! + +// ExpTaylor calculates eˣ using the Taylor series: eˣ = 1 + x + x²/2! + // x³/3! + ... constexpr double ExpTaylor(double x) { double sum = 1.0; @@ -351,6 +372,9 @@ class AdaptiveProportionTest { const uint16_t new_count = ++counts_[sample]; if (new_count > kThreshold) { +#if defined(BORINGSSL_FIPS_BREAK_TESTS) + fprintf(stderr, "Jitter APT failure.\n"); +#endif return false; } diff --git a/third_party/boringssl/src/crypto/fipsmodule/entropy/sha512.cc.inc b/third_party/boringssl/src/crypto/fipsmodule/entropy/sha512.cc.inc index 53512a86e..1d954d0a3 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/entropy/sha512.cc.inc +++ b/third_party/boringssl/src/crypto/fipsmodule/entropy/sha512.cc.inc @@ -74,8 +74,8 @@ void SHA384_Init(SHA512_CTX *sha) { } void SHA384_Final(uint8_t out[kSHA384DigestLength], SHA512_CTX *sha) { - // This function must be paired with |SHA384_Init|, which sets - // |sha->md_len| to |kSHA384DigestLength|. + // This function must be paired with `SHA384_Init`, which sets + // `sha->md_len` to `kSHA384DigestLength`. sha512_final_impl(out, kSHA384DigestLength, sha); return; } diff --git a/third_party/boringssl/src/crypto/fipsmodule/fips_shared_support.cc b/third_party/boringssl/src/crypto/fipsmodule/fips_shared_support.cc index f1f9d99f2..ebc64ef31 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/fips_shared_support.cc +++ b/third_party/boringssl/src/crypto/fipsmodule/fips_shared_support.cc @@ -16,10 +16,10 @@ #if defined(BORINGSSL_FIPS) && defined(BORINGSSL_SHARED_LIBRARY) -// BORINGSSL_bcm_text_hash is is default hash value for the FIPS integrity check -// that must be replaced with the real value during the build process. This -// value need only be distinct, i.e. so that we can safely search-and-replace it -// in an object file. +// BORINGSSL_bcm_text_hash is the default hash value for the FIPS integrity +// check that must be replaced with the real value during the build process. +// This value need only be distinct, i.e. so that we can safely +// search-and-replace it in an object file. extern const uint8_t BORINGSSL_bcm_text_hash[32] = { 0xae, 0x2c, 0xea, 0x2a, 0xbd, 0xa6, 0xf3, 0xec, 0x97, 0x7f, 0x9b, 0xf6, 0x94, 0x9a, 0xfc, 0x83, 0x68, 0x27, 0xcb, 0xa0, 0xa0, 0x9f, diff --git a/third_party/boringssl/src/crypto/fipsmodule/hmac/hmac.cc.inc b/third_party/boringssl/src/crypto/fipsmodule/hmac/hmac.cc.inc index 621176021..c753fae3c 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/hmac/hmac.cc.inc +++ b/third_party/boringssl/src/crypto/fipsmodule/hmac/hmac.cc.inc @@ -98,10 +98,10 @@ int HMAC_Init_ex(HMAC_CTX *ctx, const void *key, size_t key_len, md = ctx->md; } - // If either |key| is non-NULL or |md| has changed, initialize with a new key + // If either `key` is non-NULL or `md` has changed, initialize with a new key // rather than rewinding the previous one. // - // TODO(davidben,eroman): Passing the previous |md| with a NULL |key| is + // TODO(davidben,eroman): Passing the previous `md` with a NULL `key` is // ambiguous between using the empty key and reusing the previous key. There // exist callers which intend the latter, but the former is an awkward edge // case. Fix to API to avoid this. @@ -165,7 +165,7 @@ int HMAC_Final(HMAC_CTX *ctx, uint8_t *out, unsigned int *out_len) { FIPS_service_indicator_lock_state(); // TODO(davidben): The only thing that can officially fail here is - // |EVP_MD_CTX_copy_ex|, but even that should be impossible in this case. + // `EVP_MD_CTX_copy_ex`, but even that should be impossible in this case. if (!EVP_DigestFinal_ex(&ctx->md_ctx, buf, &i) || !EVP_MD_CTX_copy_ex(&ctx->md_ctx, &ctx->o_ctx) || !EVP_DigestUpdate(&ctx->md_ctx, buf, i) || diff --git a/third_party/boringssl/src/crypto/fipsmodule/keccak/internal.h b/third_party/boringssl/src/crypto/fipsmodule/keccak/internal.h index ff10c4444..59d45fba6 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/keccak/internal.h +++ b/third_party/boringssl/src/crypto/fipsmodule/keccak/internal.h @@ -17,14 +17,31 @@ #include +#include "../../internal.h" + BSSL_NAMESPACE_BEGIN +#define BITS_TO_BYTES(BITS) ((BITS) / 8) +#define SHA3_256_DIGEST_LENGTH BITS_TO_BYTES(256) +#define SHA3_384_DIGEST_LENGTH BITS_TO_BYTES(384) +#define SHA3_512_DIGEST_LENGTH BITS_TO_BYTES(512) +#define SHA3_BLOCK_SIZE(BITS) BITS_TO_BYTES(1600 - 2 * (BITS)) +#define SHA3_256_CBLOCK SHA3_BLOCK_SIZE(256) +#define SHA3_384_CBLOCK SHA3_BLOCK_SIZE(384) +#define SHA3_512_CBLOCK SHA3_BLOCK_SIZE(512) +#define SHAKE256_CBLOCK SHA3_BLOCK_SIZE(256) + enum boringssl_keccak_config_t : int32_t { boringssl_sha3_256, + boringssl_sha3_384, boringssl_sha3_512, boringssl_shake128, boringssl_shake256, + boringssl_turboshake128, + boringssl_turboshake256, + boringssl_cshake128, + boringssl_cshake256, }; enum boringssl_keccak_phase_t : int32_t { @@ -45,34 +62,67 @@ struct BORINGSSL_keccak_st { size_t squeeze_offset; }; -// BORINGSSL_keccak hashes |in_len| bytes from |in| and writes |out_len| bytes -// of output to |out|. If the |config| specifies a fixed-output function, like -// SHA3-256, then |out_len| must be the correct length for that function. +// BORINGSSL_keccak hashes `in_len` bytes from `in` and writes `out_len` bytes +// of output to `out`. If the `config` specifies a fixed-output function, like +// SHA3-256, then `out_len` must be the correct length for that function. The +// cSHAKE configurations are only valid with the BORINGSSL_cshake functions. OPENSSL_EXPORT void BORINGSSL_keccak(uint8_t *out, size_t out_len, const uint8_t *in, size_t in_len, enum boringssl_keccak_config_t config); -// BORINGSSL_keccak_init prepares |ctx| for absorbing. If the |config| specifies +// BORINGSSL_keccak_init prepares `ctx` for absorbing. If the `config` specifies // a fixed-output function, like SHA3-256, then the output must be squeezed in a -// single call to |BORINGSSL_keccak_squeeze|. In that case, it is recommended to -// use |BORINGSSL_keccak| if the input can be absorbed in a single call. +// single call to `BORINGSSL_keccak_squeeze`. In that case, it is recommended to +// use `BORINGSSL_keccak` if the input can be absorbed in a single call. OPENSSL_EXPORT void BORINGSSL_keccak_init( struct BORINGSSL_keccak_st *ctx, enum boringssl_keccak_config_t config); -// BORINGSSL_keccak_absorb absorbs |in_len| bytes from |in|. +// BORINGSSL_cshake_init prepares `ctx` for cSHAKE, including absorbing the +// function-name and customization strings. `config` must be +// `boringssl_cshake128` or `boringssl_cshake256`. The message may then be +// absorbed and output squeezed with the usual Keccak functions. +OPENSSL_EXPORT void BORINGSSL_cshake_init(struct BORINGSSL_keccak_st *ctx, + enum boringssl_keccak_config_t config, + const uint8_t *function_name, + size_t function_name_len, + const uint8_t *customization, + size_t customization_len); + +// BORINGSSL_cshake is the one-shot form of BORINGSSL_cshake_init. +OPENSSL_EXPORT void BORINGSSL_cshake(uint8_t *out, size_t out_len, + const uint8_t *in, size_t in_len, + enum boringssl_keccak_config_t config, + const uint8_t *function_name, + size_t function_name_len, + const uint8_t *customization, + size_t customization_len); + +// BORINGSSL_keccak_absorb absorbs `in_len` bytes from `in`. OPENSSL_EXPORT void BORINGSSL_keccak_absorb(struct BORINGSSL_keccak_st *ctx, const uint8_t *in, size_t in_len); -// BORINGSSL_keccak_squeeze writes |out_len| bytes to |out| from |ctx|. If the -// configuration previously passed in |BORINGSSL_keccak_init| specifies a -// fixed-output function, then a single call to |BORINGSSL_keccak_squeeze| is -// allowed, where |out_len| must be the correct length for that function. +// BORINGSSL_keccak_squeeze writes `out_len` bytes to `out` from `ctx`. If the +// configuration previously passed in `BORINGSSL_keccak_init` specifies a +// fixed-output function, then a single call to `BORINGSSL_keccak_squeeze` is +// allowed, where `out_len` must be the correct length for that function. OPENSSL_EXPORT void BORINGSSL_keccak_squeeze(struct BORINGSSL_keccak_st *ctx, uint8_t *out, size_t out_len); #if defined(__has_attribute) #if __has_attribute(vector_size) + #define HAVE_KECCAK_X2 + +#if defined(OPENSSL_AARCH64) +#define HAVE_KECCAK_X4 +#define KECCAK_X4_TARGET +#endif + +#if defined(OPENSSL_X86_64) +#define HAVE_KECCAK_X4 +#define KECCAK_X4_TARGET __attribute__((target("avx2"))) +#endif + #endif // vector_size #endif // __has_attribute @@ -80,17 +130,47 @@ OPENSSL_EXPORT void BORINGSSL_keccak_squeeze(struct BORINGSSL_keccak_st *ctx, // BORINGSSL_keccak_squeeze_x2 performs BORINGSSL_keccak_squeeze in parallel // with two same-length outputs. The contexts must be in equivalent state (i.e. // same config, same amount of bytes absorbed and squeezed). +// This function is not compatible with cSHAKE nor TurboSHAKE. OPENSSL_EXPORT void BORINGSSL_keccak_squeeze_x2( struct BORINGSSL_keccak_st ctx[2], uint8_t *outs[2], size_t out_len); // BORINGSSL_keccak_short_x2 performs BORINGSSL_keccak in parallel on two -// same-length strings with same-length outputs. |in_len| must be less than 72 -// (or actually |rate_bytes|). +// same-length strings with same-length outputs. `in_len` must be less than 72 +// (or actually `rate_bytes`). +// This function is not compatible with cSHAKE nor TurboSHAKE. OPENSSL_EXPORT void BORINGSSL_keccak_short_x2( uint8_t *outs[2], size_t out_len, const uint8_t *ins[2], size_t in_len, enum boringssl_keccak_config_t config); #endif +#if defined(HAVE_KECCAK_X4) +// BORINGSSL_have_keccak_x4 needs to return true before using any of the _x4 +// variants here, as they may require additional CPU features. +// This function is not compatible with cSHAKE nor TurboSHAKE. +inline bool BORINGSSL_have_keccak_x4() { +#if defined(OPENSSL_X86_64) + return CRYPTO_is_AVX2_capable(); +#else + return true; +#endif +} + +// BORINGSSL_keccak_squeeze_x4 performs BORINGSSL_keccak_squeeze in parallel +// with four same-length outputs. The contexts must be in equivalent state (i.e. +// same config, same amount of bytes absorbed and squeezed). +// This function is not compatible with cSHAKE nor TurboSHAKE. +OPENSSL_EXPORT void BORINGSSL_keccak_squeeze_x4( + struct BORINGSSL_keccak_st ctx[4], uint8_t *outs[4], size_t out_len); + +// BORINGSSL_keccak_short_x4 performs BORINGSSL_keccak in parallel on four +// same-length strings with same-length outputs. in_len must be less than 72 +// (or actually `rate_bytes`). +// This function is not compatible with cSHAKE nor TurboSHAKE. +OPENSSL_EXPORT void BORINGSSL_keccak_short_x4( + uint8_t *outs[4], size_t out_len, const uint8_t *ins[4], size_t in_len, + enum boringssl_keccak_config_t config); +#endif + BSSL_NAMESPACE_END #endif // OPENSSL_HEADER_CRYPTO_FIPSMODULE_KECCAK_INTERNAL_H diff --git a/third_party/boringssl/src/crypto/fipsmodule/keccak/keccak.cc.inc b/third_party/boringssl/src/crypto/fipsmodule/keccak/keccak.cc.inc index 5008cd465..e808dd583 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/keccak/keccak.cc.inc +++ b/third_party/boringssl/src/crypto/fipsmodule/keccak/keccak.cc.inc @@ -23,16 +23,33 @@ using namespace bssl; -// keccak_f implements the Keccak-1600 permutation as described at +// A rotate-left function is needed for every type to use Keccak with. +// +// As argument passing may differ for AVX2 and non-AVX2 functions taking vector +// arguments by value, this function takes a pointer instead; however, thanks +// to `OPENSSL_ATTR_ALWAYS_INLINE`, this should not actually change generated +// code. +static inline OPENSSL_ATTR_ALWAYS_INLINE void rotl_u64(uint64_t *value, + int shift) { + *value = CRYPTO_rotl_u64(*value, shift); +} + +// keccak_f implements the last `num_rounds` of the Keccak-1600 permutation, +// i.e. Keccak-p[1600, num_rounds], as described at // https://keccak.team/keccak_specs_summary.html. Each lane is represented as a // 64-bit value and the 5×5 lanes are stored as an array in row-major order. // // To support vectorization, U64 shall either be uint64_t, or an uint64_t-based // vector type. -template -static void keccak_f(U64 state[25]) { +// +// `OPENSSL_ATTR_ALWAYS_INLINE` is used here to ensure that AVX2 optimizations +// are used whenever the calling function is `KECCAK_X4_TARGET`, as target +// attributes are inherited into callees when inlining. +template +static inline OPENSSL_ATTR_ALWAYS_INLINE void keccak_f(U64 state[25], + int num_rounds = 24) { static const int kNumRounds = 24; - for (int round = 0; round < kNumRounds; round++) { + for (int round = kNumRounds - num_rounds; round < kNumRounds; round++) { // θ step U64 c[5]; for (int x = 0; x < 5; x++) { @@ -41,7 +58,9 @@ static void keccak_f(U64 state[25]) { } for (int x = 0; x < 5; x++) { - const U64 d = c[(x + 4) % 5] ^ rotl(c[(x + 1) % 5], 1); + U64 next_value = c[(x + 1) % 5]; + rotl(&next_value, 1); + const U64 d = c[(x + 4) % 5] ^ next_value; for (int y = 0; y < 5; y++) { state[y * 5 + x] ^= d; } @@ -63,11 +82,12 @@ static void keccak_f(U64 state[25]) { // at (0, 0), but the rotation for that element is zero, and it goes to (0, // 0), so we can ignore it. U64 prev_value = state[1]; -#define PI_RHO_STEP(index, rotation) \ - do { \ - const U64 value = rotl(prev_value, rotation); \ - prev_value = state[index]; \ - state[index] = value; \ +#define PI_RHO_STEP(index, rotation) \ + do { \ + U64 value = prev_value; \ + rotl(&value, rotation); \ + prev_value = state[index]; \ + state[index] = value; \ } while (0) PI_RHO_STEP(10, 1); @@ -113,7 +133,7 @@ static void keccak_f(U64 state[25]) { // // From https://keccak.team/files/Keccak-reference-3.0.pdf, section // 1.2, the round constants are based on the output of a LFSR. Thus, as - // suggested in the appendix of of + // suggested in the appendix of // https://keccak.team/keccak_specs_summary.html, the values are // simply encoded here. static const uint64_t kRoundConstants[24] = { @@ -134,8 +154,8 @@ static void keccak_f(U64 state[25]) { #if defined(HAVE_KECCAK_X2) typedef uint64_t v2u64 __attribute__((vector_size(16))); -static inline v2u64 v_rotl(v2u64 x, int shift) { - return (x << shift) | (x >> ((-shift) & 63)); +static inline void v2_rotl(v2u64 *x, int shift) { + *x = (*x << shift) | (*x >> ((-shift) & 63)); } static void keccak_f_x2(uint64_t state0[25], uint64_t state1[25]) { @@ -143,10 +163,35 @@ static void keccak_f_x2(uint64_t state0[25], uint64_t state1[25]) { for (int i = 0; i < 25; i++) { s[i] = (v2u64){state0[i], state1[i]}; } - keccak_f(s); + keccak_f(s); + for (int i = 0; i < 25; i++) { + state0[i] = s[i][0]; + state1[i] = s[i][1]; + } +} +#endif + +#if defined(HAVE_KECCAK_X4) +typedef uint64_t v4u64 __attribute__((vector_size(32))); + +KECCAK_X4_TARGET static inline void v4_rotl(v4u64 *x, int shift) { + *x = (*x << shift) | (*x >> ((-shift) & 63)); +} + +KECCAK_X4_TARGET static void keccak_f_x4(uint64_t state0[25], + uint64_t state1[25], + uint64_t state2[25], + uint64_t state3[25]) { + v4u64 s[25]; + for (int i = 0; i < 25; i++) { + s[i] = (v4u64){state0[i], state1[i], state2[i], state3[i]}; + } + keccak_f(s); for (int i = 0; i < 25; i++) { state0[i] = s[i][0]; state1[i] = s[i][1]; + state2[i] = s[i][2]; + state3[i] = s[i][3]; } } #endif @@ -160,15 +205,23 @@ static void keccak_init(struct BORINGSSL_keccak_st *ctx, capacity_bytes = 512 / 8; required_out_len = 32; break; + case boringssl_sha3_384: + capacity_bytes = 768 / 8; + required_out_len = 48; + break; case boringssl_sha3_512: capacity_bytes = 1024 / 8; required_out_len = 64; break; case boringssl_shake128: + case boringssl_turboshake128: + case boringssl_cshake128: capacity_bytes = 256 / 8; required_out_len = 0; break; case boringssl_shake256: + case boringssl_turboshake256: + case boringssl_cshake256: capacity_bytes = 512 / 8; required_out_len = 0; break; @@ -184,14 +237,30 @@ static void keccak_init(struct BORINGSSL_keccak_st *ctx, assert(ctx->rate_bytes % 8 == 0); } +[[maybe_unused]] static bool keccak_can_parallel( + enum boringssl_keccak_config_t config) { + return config == boringssl_sha3_256 || config == boringssl_sha3_384 || + config == boringssl_sha3_512 || config == boringssl_shake128 || + config == boringssl_shake256; +} + +static bool keccak_is_cshake(enum boringssl_keccak_config_t config) { + return config == boringssl_cshake128 || config == boringssl_cshake256; +} + +static int keccak_num_rounds(enum boringssl_keccak_config_t config) { + if (config == boringssl_turboshake128 || config == boringssl_turboshake256) { + return 12; + } + return 24; +} + void bssl::BORINGSSL_keccak(uint8_t *out, size_t out_len, const uint8_t *in, size_t in_len, enum boringssl_keccak_config_t config) { struct BORINGSSL_keccak_st ctx; BORINGSSL_keccak_init(&ctx, config); - if (ctx.required_out_len != 0 && out_len != ctx.required_out_len) { - abort(); - } + BSSL_CHECK(ctx.required_out_len == 0 || out_len == ctx.required_out_len); BORINGSSL_keccak_absorb(&ctx, in, in_len); BORINGSSL_keccak_squeeze(&ctx, out, out_len); } @@ -200,17 +269,18 @@ void bssl::BORINGSSL_keccak(uint8_t *out, size_t out_len, const uint8_t *in, void bssl::BORINGSSL_keccak_short_x2(uint8_t *outs[2], size_t out_len, const uint8_t *ins[2], size_t in_len, enum boringssl_keccak_config_t config) { + assert(keccak_can_parallel(config)); + struct BORINGSSL_keccak_st ctx[2]; for (size_t i = 0; i < 2; ++i) { BORINGSSL_keccak_init(&ctx[i], config); - if (ctx[i].required_out_len != 0 && out_len != ctx[i].required_out_len) { - abort(); - } + BSSL_CHECK(ctx[i].required_out_len == 0 || + out_len == ctx[i].required_out_len); - // NOTE: this implementation is only efficient if in_len < ctx->rate_bytes, - // as right now only keccak_f calls in BORINGSSL_keccak_squeeze and - // keccak_finalize are vectorized. So just fail in every other case for - // now. + // NOTE: this implementation is only efficient if in_len < + // ctx->rate_bytes, as right now only keccak_f calls in + // BORINGSSL_keccak_squeeze and keccak_finalize are vectorized. So just + // fail in every other case for now. BSSL_CHECK(in_len < ctx[i].rate_bytes); BORINGSSL_keccak_absorb(&ctx[i], ins[i], in_len); @@ -219,21 +289,44 @@ void bssl::BORINGSSL_keccak_short_x2(uint8_t *outs[2], size_t out_len, } #endif +#if defined(HAVE_KECCAK_X4) +KECCAK_X4_TARGET void bssl::BORINGSSL_keccak_short_x4( + uint8_t *outs[4], size_t out_len, const uint8_t *ins[4], size_t in_len, + enum boringssl_keccak_config_t config) { + assert(keccak_can_parallel(config)); + + struct BORINGSSL_keccak_st ctx[4]; + for (size_t i = 0; i < 4; ++i) { + BORINGSSL_keccak_init(&ctx[i], config); + BSSL_CHECK(ctx[i].required_out_len == 0 || + out_len == ctx[i].required_out_len); + + // NOTE: this implementation is only efficient if in_len < + // ctx->rate_bytes, as right now only keccak_f calls in + // BORINGSSL_keccak_squeeze and keccak_finalize are vectorized. So just + // fail in every other case for now. + BSSL_CHECK(in_len < ctx[i].rate_bytes); + + BORINGSSL_keccak_absorb(&ctx[i], ins[i], in_len); + } + BORINGSSL_keccak_squeeze_x4(ctx, outs, out_len); +} +#endif + void bssl::BORINGSSL_keccak_init(struct BORINGSSL_keccak_st *ctx, enum boringssl_keccak_config_t config) { + BSSL_CHECK(!keccak_is_cshake(config)); keccak_init(ctx, config); } void bssl::BORINGSSL_keccak_absorb(struct BORINGSSL_keccak_st *ctx, const uint8_t *in, size_t in_len) { - if (ctx->phase == boringssl_keccak_phase_squeeze) { - // It's illegal to call absorb() again after calling squeeze(). - abort(); - } + // It's illegal to call absorb() again after calling squeeze(). + BSSL_CHECK(ctx->phase != boringssl_keccak_phase_squeeze); const size_t rate_words = ctx->rate_bytes / 8; - // XOR the input. Accessing |ctx->state| as a |uint8_t*| is allowed by strict - // aliasing because we require |uint8_t| to be a character type. + // XOR the input. Accessing `ctx->state` as a `uint8_t*` is allowed by + // strict aliasing because we require `uint8_t` to be a character type. uint8_t *state_bytes = (uint8_t *)ctx->state; // Absorb partial block. @@ -250,7 +343,7 @@ void bssl::BORINGSSL_keccak_absorb(struct BORINGSSL_keccak_st *ctx, return; } - keccak_f(ctx->state); + keccak_f(ctx->state, keccak_num_rounds(ctx->config)); in += first_block_len; in_len -= first_block_len; } @@ -260,7 +353,7 @@ void bssl::BORINGSSL_keccak_absorb(struct BORINGSSL_keccak_st *ctx, for (size_t i = 0; i < rate_words; i++) { ctx->state[i] ^= CRYPTO_load_u64_le(in + 8 * i); } - keccak_f(ctx->state); + keccak_f(ctx->state, keccak_num_rounds(ctx->config)); in += ctx->rate_bytes; in_len -= ctx->rate_bytes; } @@ -273,33 +366,105 @@ void bssl::BORINGSSL_keccak_absorb(struct BORINGSSL_keccak_st *ctx, ctx->absorb_offset = in_len; } +// left_encode implements the integer encoding from NIST SP 800-185, section +// 2.3.1. +static size_t left_encode(uint8_t out[sizeof(size_t) + 1], size_t value) { + size_t num_bytes = 1; + for (size_t v = value; v > 0xff; v >>= 8) { + num_bytes++; + } + out[0] = (uint8_t)num_bytes; + for (size_t i = 0; i < num_bytes; i++) { + out[num_bytes - i] = (uint8_t)(value >> (8 * i)); + } + return num_bytes + 1; +} + +static void cshake_absorb_encode_string(struct BORINGSSL_keccak_st *ctx, + const uint8_t *in, size_t in_len) { + BSSL_CHECK(in_len <= SIZE_MAX / 8); + uint8_t encoded[sizeof(size_t) + 1]; + const size_t encoded_len = left_encode(encoded, in_len * 8); + BORINGSSL_keccak_absorb(ctx, encoded, encoded_len); + BORINGSSL_keccak_absorb(ctx, in, in_len); +} + +void bssl::BORINGSSL_cshake_init(struct BORINGSSL_keccak_st *ctx, + enum boringssl_keccak_config_t config, + const uint8_t *function_name, + size_t function_name_len, + const uint8_t *customization, + size_t customization_len) { + BSSL_CHECK(keccak_is_cshake(config)); + + // SP 800-185 defines cSHAKE with two empty strings to be SHAKE. + if (function_name_len == 0 && customization_len == 0) { + BORINGSSL_keccak_init(ctx, config == boringssl_cshake128 + ? boringssl_shake128 + : boringssl_shake256); + return; + } + + keccak_init(ctx, config); + uint8_t encoded[sizeof(size_t) + 1]; + const size_t encoded_len = left_encode(encoded, ctx->rate_bytes); + BORINGSSL_keccak_absorb(ctx, encoded, encoded_len); + cshake_absorb_encode_string(ctx, function_name, function_name_len); + cshake_absorb_encode_string(ctx, customization, customization_len); + + static const uint8_t kZeros[168] = {0}; + if (ctx->absorb_offset != 0) { + BORINGSSL_keccak_absorb(ctx, kZeros, ctx->rate_bytes - ctx->absorb_offset); + } +} + +void bssl::BORINGSSL_cshake(uint8_t *out, size_t out_len, const uint8_t *in, + size_t in_len, + enum boringssl_keccak_config_t config, + const uint8_t *function_name, + size_t function_name_len, + const uint8_t *customization, + size_t customization_len) { + struct BORINGSSL_keccak_st ctx; + BORINGSSL_cshake_init(&ctx, config, function_name, function_name_len, + customization, customization_len); + BORINGSSL_keccak_absorb(&ctx, in, in_len); + BORINGSSL_keccak_squeeze(&ctx, out, out_len); +} + static uint8_t keccak_terminator(struct BORINGSSL_keccak_st *ctx) { switch (ctx->config) { case boringssl_sha3_256: + case boringssl_sha3_384: case boringssl_sha3_512: return 0x06; case boringssl_shake128: case boringssl_shake256: + case boringssl_turboshake128: + case boringssl_turboshake256: return 0x1f; + case boringssl_cshake128: + case boringssl_cshake256: + return 0x04; default: abort(); } } static void keccak_finalize(struct BORINGSSL_keccak_st *ctx) { - // XOR the terminator. Accessing |ctx->state| as a |uint8_t*| is allowed by - // strict aliasing because we require |uint8_t| to be a character type. + // XOR the terminator. Accessing `ctx->state` as a `uint8_t*` is allowed by + // strict aliasing because we require `uint8_t` to be a character type. uint8_t *state_bytes = (uint8_t *)ctx->state; state_bytes[ctx->absorb_offset] ^= keccak_terminator(ctx); state_bytes[ctx->rate_bytes - 1] ^= 0x80; - keccak_f(ctx->state); + keccak_f(ctx->state, keccak_num_rounds(ctx->config)); } #if defined(HAVE_KECCAK_X2) static void keccak_finalize_x2(struct BORINGSSL_keccak_st ctx[2]) { for (size_t i = 0; i < 2; ++i) { - // XOR the terminator. Accessing |ctx->state| as a |uint8_t*| is allowed by - // strict aliasing because we require |uint8_t| to be a character type. + // XOR the terminator. Accessing `ctx->state` as a `uint8_t*` is allowed + // by strict aliasing because we require `uint8_t` to be a character type. uint8_t *state_bytes = (uint8_t *)ctx[i].state; state_bytes[ctx[i].absorb_offset] ^= keccak_terminator(&ctx[i]); state_bytes[ctx[i].rate_bytes - 1] ^= 0x80; @@ -308,27 +473,40 @@ static void keccak_finalize_x2(struct BORINGSSL_keccak_st ctx[2]) { } #endif +#if defined(HAVE_KECCAK_X4) +static void keccak_finalize_x4(struct BORINGSSL_keccak_st ctx[4]) { + assert(keccak_can_parallel(ctx[0].config)); + + for (size_t i = 0; i < 4; ++i) { + // XOR the terminator. Accessing `ctx->state` as a `uint8_t*` is allowed + // by strict aliasing because we require `uint8_t` to be a character type. + uint8_t *state_bytes = (uint8_t *)ctx[i].state; + state_bytes[ctx[i].absorb_offset] ^= keccak_terminator(&ctx[i]); + state_bytes[ctx[i].rate_bytes - 1] ^= 0x80; + } + keccak_f_x4(ctx[0].state, ctx[1].state, ctx[2].state, ctx[3].state); +} +#endif + void bssl::BORINGSSL_keccak_squeeze(struct BORINGSSL_keccak_st *ctx, uint8_t *out, size_t out_len) { - if (ctx->required_out_len != 0 && - (ctx->phase == boringssl_keccak_phase_squeeze || - out_len != ctx->required_out_len)) { - // The SHA-3 variants must be squeezed in a single call, to confirm that the - // output length is correct. - abort(); - } + // The SHA-3 variants must be squeezed in a single call, to confirm that the + // output length is correct. + BSSL_CHECK(ctx->required_out_len == 0 || + (ctx->phase != boringssl_keccak_phase_squeeze && + out_len == ctx->required_out_len)); if (ctx->phase == boringssl_keccak_phase_absorb) { keccak_finalize(ctx); ctx->phase = boringssl_keccak_phase_squeeze; } - // Accessing |ctx->state| as a |uint8_t*| is allowed by strict aliasing - // because we require |uint8_t| to be a character type. + // Accessing `ctx->state` as a `uint8_t*` is allowed by strict aliasing + // because we require `uint8_t` to be a character type. const uint8_t *state_bytes = (const uint8_t *)ctx->state; while (out_len) { if (ctx->squeeze_offset == ctx->rate_bytes) { - keccak_f(ctx->state); + keccak_f(ctx->state, keccak_num_rounds(ctx->config)); ctx->squeeze_offset = 0; } @@ -347,18 +525,18 @@ void bssl::BORINGSSL_keccak_squeeze(struct BORINGSSL_keccak_st *ctx, #if defined(HAVE_KECCAK_X2) void bssl::BORINGSSL_keccak_squeeze_x2(struct BORINGSSL_keccak_st ctx[2], uint8_t *outs[2], size_t out_len) { + assert(keccak_can_parallel(ctx[0].config)); + for (size_t i = 0; i < 2; ++i) { - if (ctx[i].required_out_len != 0 && - (ctx[i].phase == boringssl_keccak_phase_squeeze || - out_len != ctx[i].required_out_len)) { - // The SHA-3 variants must be squeezed in a single call, to confirm that - // the output length is correct. - abort(); - } + // The SHA-3 variants must be squeezed in a single call, to confirm that + // the output length is correct. + BSSL_CHECK(ctx[i].required_out_len == 0 || + (ctx[i].phase != boringssl_keccak_phase_squeeze && + out_len == ctx[i].required_out_len)); } - // These fields are processed in parallel. Everything here uses ctx[0]; at the - // end changes are mirrored back to ctx[1] just in case. + // These fields are processed in parallel. Everything here uses ctx[0]; at + // the end changes are mirrored back to ctx[1] just in case. #define FOR_COMMON_FIELDS(MACRO) \ MACRO(phase) \ MACRO(config) \ @@ -375,8 +553,8 @@ void bssl::BORINGSSL_keccak_squeeze_x2(struct BORINGSSL_keccak_st ctx[2], ctx->phase = boringssl_keccak_phase_squeeze; } - // Accessing |ctx->state| as a |uint8_t*| is allowed by strict aliasing - // because we require |uint8_t| to be a character type. + // Accessing `ctx->state` as a `uint8_t*` is allowed by strict aliasing + // because we require `uint8_t` to be a character type. uint8_t *optr[2] = {outs[0], outs[1]}; while (out_len) { if (ctx->squeeze_offset == ctx->rate_bytes) { @@ -405,3 +583,71 @@ void bssl::BORINGSSL_keccak_squeeze_x2(struct BORINGSSL_keccak_st ctx[2], #undef FOR_COMMON_FIELDS } #endif // HAVE_KECCAK_X2 + +#if defined(HAVE_KECCAK_X4) +KECCAK_X4_TARGET void bssl::BORINGSSL_keccak_squeeze_x4( + struct BORINGSSL_keccak_st ctx[4], uint8_t *outs[4], size_t out_len) { + assert(keccak_can_parallel(ctx[0].config)); + + for (size_t i = 0; i < 4; ++i) { + // The SHA-3 variants must be squeezed in a single call, to confirm that + // the output length is correct. + BSSL_CHECK(ctx[i].required_out_len == 0 || + (ctx[i].phase != boringssl_keccak_phase_squeeze && + out_len == ctx[i].required_out_len)); + } + + // These fields are processed in parallel. Everything here uses ctx[0]; at + // the end changes are mirrored back to ctx[1..3] just in case. +#define FOR_COMMON_FIELDS(MACRO) \ + MACRO(phase) \ + MACRO(config) \ + MACRO(absorb_offset) \ + MACRO(squeeze_offset) \ + MACRO(rate_bytes) + +#define MUST_BE_EQUAL(field) \ + BSSL_CHECK(ctx[0].field == ctx[1].field); \ + BSSL_CHECK(ctx[0].field == ctx[2].field); \ + BSSL_CHECK(ctx[0].field == ctx[3].field); + FOR_COMMON_FIELDS(MUST_BE_EQUAL) +#undef MUST_BE_EQUAL + + if (ctx->phase == boringssl_keccak_phase_absorb) { + keccak_finalize_x4(ctx); + ctx->phase = boringssl_keccak_phase_squeeze; + } + + // Accessing `ctx->state` as a `uint8_t*` is allowed by strict aliasing + // because we require `uint8_t` to be a character type. + uint8_t *optr[4] = {outs[0], outs[1], outs[2], outs[3]}; + while (out_len) { + if (ctx->squeeze_offset == ctx->rate_bytes) { + keccak_f_x4(ctx[0].state, ctx[1].state, ctx[2].state, ctx[3].state); + ctx->squeeze_offset = 0; + } + + size_t remaining = ctx->rate_bytes - ctx->squeeze_offset; + size_t todo = out_len; + if (todo > remaining) { + todo = remaining; + } + for (size_t i = 0; i < 4; ++i) { + const uint8_t *state_bytes = (const uint8_t *)ctx[i].state; + OPENSSL_memcpy(optr[i], &state_bytes[ctx->squeeze_offset], todo); + optr[i] += todo; + } + out_len -= todo; + ctx->squeeze_offset += todo; + } + +#define COPY_FIELD_VALUE(field) \ + ctx[1].field = ctx[0].field; \ + ctx[2].field = ctx[0].field; \ + ctx[3].field = ctx[0].field; + FOR_COMMON_FIELDS(COPY_FIELD_VALUE) +#undef COPY_FIELD_VALUE + +#undef FOR_COMMON_FIELDS +} +#endif // HAVE_KECCAK_X4 diff --git a/third_party/boringssl/src/crypto/fipsmodule/mldsa/mldsa.cc.inc b/third_party/boringssl/src/crypto/fipsmodule/mldsa/mldsa.cc.inc index 4bd388548..ca9374141 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/mldsa/mldsa.cc.inc +++ b/third_party/boringssl/src/crypto/fipsmodule/mldsa/mldsa.cc.inc @@ -62,8 +62,9 @@ constexpr uint32_t kPrime = 8380417; constexpr uint32_t kPrimeNegInverse = 4236238847; constexpr int kDroppedBits = 13; constexpr uint32_t kHalfPrime = (kPrime - 1) / 2; -// 256^-1 mod kPrime, in Montgomery form. -constexpr uint32_t kInverseDegreeMontgomery = 41978; +// 256^-1 ⋅ R² mod kPrime. This is not 256^-1 in Montgomery form, but 256^-1 +// doubly-converted to Montgomery form. +constexpr uint32_t kInverseDegreeDoubleMontgomery = 41978; // Constants that vary depending on ML-DSA size. // @@ -303,10 +304,26 @@ inline uint32_t reduce_once(uint32_t x) { // We usually add value barriers to selects because Clang turns consecutive // selects with the same condition into a branch instead of CMOV/CSEL. This // condition does not occur in ML-DSA, so omitting it seems to be generally - // safe. However, see |coefficient_from_nibble|. + // safe. However, see `coefficient_from_nibble`. return (mask & x) | (~mask & subtracted); } +// reduce_from_u32 returns `x mod kPrime`. `x` can be any `uint32_t`. +inline uint32_t reduce_from_u32(uint32_t x) { + // kPrime is 2^23 - 2^13 + 1. We first split `x` into the upper 9 bits and the + // lower 23 bits: + uint32_t hi = x >> 23, lo = x & ((uint32_t{1} << 23) - 1); + // 2^23 = 2^13 - 1 mod kPrime + // x = hi * 2^23 + lo + // = hi * 2^13 - hi + lo mod kPrime + // + // This value does not overflow and is below 2 * kPrime. We can `reduce_once`. + static constexpr uint32_t kMaxHi = UINT32_MAX >> 23; + static constexpr uint32_t kMaxLo = (uint32_t{1} << 23) - 1; + static_assert((kMaxHi << 13) - kMaxHi + kMaxLo < 2 * kPrime); + return reduce_once((hi << 13) - hi + lo); +} + // Returns the absolute value in constant time, interpreting the high bit as a // sign bit. inline uint32_t abs_signed(uint32_t x) { @@ -341,7 +358,7 @@ inline uint32_t mod_sub(uint32_t a, uint32_t b) { uint32_t r = a - b; // return r < 0 ? r + kPrime : r; uint32_t mask = 0u - (r >> 31); - // See |reduce_once| for why this does not have a value barrier. + // See `reduce_once` for why this does not have a value barrier. return (mask & (r + kPrime)) | (~mask & r); } @@ -357,17 +374,27 @@ inline void scalar_sub(scalar *out, const scalar *lhs, const scalar *rhs) { } } -inline uint32_t reduce_montgomery(uint64_t x) { +// reduce_montgomery_partial returns a number equivalent to x * R^-1 mod kPrime. +// The output is less than 2 * kPrime. `x` must be at most kPrime * 2^32. +inline uint32_t reduce_montgomery_partial(uint64_t x) { declassify_assert(x <= ((uint64_t)kPrime << 32)); uint64_t a = (uint32_t)x * kPrimeNegInverse; uint64_t b = x + a * kPrime; declassify_assert((b & 0xffffffff) == 0); - uint32_t c = b >> 32; - return reduce_once(c); + return b >> 32; +} + +// reduce_montgomery returns x * R^-1 mod kPrime, fully reduced. `x` must be at +// most kPrime * 2^32. +inline uint32_t reduce_montgomery(uint64_t x) { + return reduce_once(reduce_montgomery_partial(x)); } -// Multiply two scalars in the number theoretically transformed state. -inline void scalar_mult(scalar *out, const scalar *lhs, const scalar *rhs) { +// Montgomery-multiply two scalars in the number theoretically transformed +// state. Each term gains an extra R^-1 term, which is expected to be canceled +// by `scalar_inverse_ntt_montgomery`. +inline void scalar_mult_montgomery(scalar *out, const scalar *lhs, + const scalar *rhs) { for (int i = 0; i < kDegree; i++) { out->c[i] = reduce_montgomery((uint64_t)lhs->c[i] * (uint64_t)rhs->c[i]); } @@ -377,63 +404,100 @@ inline void scalar_mult(scalar *out, const scalar *lhs, const scalar *rhs) { // // FIPS 204, Algorithm 41 (`NTT`). inline void scalar_ntt(scalar *s) { - // Step: 1, 2, 4, 8, ..., 128 - // Offset: 128, 64, 32, 16, ..., 1 - int offset = kDegree; - for (int step = 1; step < kDegree; step <<= 1) { - offset >>= 1; - int k = 0; - for (int i = 0; i < step; i++) { - assert(k == 2 * offset * i); - const uint32_t step_root = kNTTRootsMontgomery[step + i]; - for (int j = k; j < k + offset; j++) { - uint32_t even = s->c[j]; - // |reduce_montgomery| works on values up to kPrime*R and R > 2*kPrime. - // |step_root| < kPrime because it's static data. |s->c[...]| is < - // kPrime by the invariants of that struct. - uint32_t odd = - reduce_montgomery((uint64_t)step_root * (uint64_t)s->c[j + offset]); - s->c[j] = reduce_once(odd + even); - s->c[j + offset] = mod_sub(even, odd); - } - k += 2 * offset; - } + // Manually unrolled loop to maximize vectorization. + // + // Intermediate values of `s` are not fully reduced. `ITER` writes to every + // element of `s` exactly once. Each write adds less than 2*kPrime to some + // value from the previous iteration. After eight iterations, each element + // will be less than 17*kPrime. This fits in `uint32_t`, so we never overflow. +#define ITER(step, offset) \ + { \ + int k = 0; \ + for (int i = 0; i < step; i++) { \ + const uint32_t step_root = kNTTRootsMontgomery[step + i]; \ + for (int j = k; j < k + offset; j++) { \ + uint32_t even = s->c[j]; \ + /* `reduce_montgomery_partial` works on values up to kPrime*R. \ + * `step_root < kPrime` because it's static data. `s->c[...] < R` \ + * because it's 32-bit. */ \ + uint32_t odd = reduce_montgomery_partial(uint64_t{step_root} * \ + uint64_t{s->c[j + offset]}); \ + s->c[j] = even + odd; \ + s->c[j + offset] = even + 2 * kPrime - odd; \ + } \ + k += 2 * offset; \ + } \ + } + + ITER(1, 128) + ITER(2, 64) + ITER(4, 32) + ITER(8, 16) + ITER(16, 8) + ITER(32, 4) + ITER(64, 2) + ITER(128, 1) + static_assert(kDegree == 256); +#undef ITER + + // Now `s` is correct but the elements are not fully reduced. Reduce them. + for (int i = 0; i < kDegree; i++) { + s->c[i] = reduce_from_u32(s->c[i]); } } -// In place inverse number theoretic transform of a given scalar. +// In place inverse number theoretic transform of a given scalar, but with each +// term multiplied by an extra factor of R. This is function is intended to be +// used with products that have gone through `reduce_montgomery` one extra time, +// so the extra R^-1 term is canceled out. // // FIPS 204, Algorithm 42 (`NTT^-1`). -inline void scalar_inverse_ntt(scalar *s) { - // Step: 128, 64, 32, 16, ..., 1 - // Offset: 1, 2, 4, 8, ..., 128 - int step = kDegree; - for (int offset = 1; offset < kDegree; offset <<= 1) { - step >>= 1; - int k = 0; - for (int i = 0; i < step; i++) { - assert(k == 2 * offset * i); - const uint32_t step_root = - kPrime - kNTTRootsMontgomery[step + (step - 1 - i)]; - for (int j = k; j < k + offset; j++) { - uint32_t even = s->c[j]; - uint32_t odd = s->c[j + offset]; - s->c[j] = reduce_once(odd + even); - - // |reduce_montgomery| works on values up to kPrime*R and R > 2*kPrime. - // kPrime + even < 2*kPrime because |even| < kPrime, by the invariants - // of that structure. Thus kPrime + even - odd < 2*kPrime because odd >= - // 0, because it's unsigned and less than kPrime. Lastly step_root < - // kPrime, because |kNTTRootsMontgomery| is static data. - s->c[j + offset] = reduce_montgomery((uint64_t)step_root * - (uint64_t)(kPrime + even - odd)); - } - k += 2 * offset; - } - } +inline void scalar_inverse_ntt_montgomery(scalar *s) { + // Manually unrolled loop to maximize vectorization. + // + // Intermediate values of `s` are not fully reduced. `ITER` writes + // to every element of `s` exactly once. Each write either adds two values + // from the previous iteration, or is at most 2 * kPrime. After eight + // iterations, each element will be less than 2⁸ * kPrime = 256 * kPrime. + // This fits in `uint32_t`, so we never overflow. +#define ITER(step, offset) \ + { \ + int k = 0; \ + for (int i = 0; i < step; i++) { \ + const uint32_t step_root = \ + kPrime - kNTTRootsMontgomery[step + (step - 1 - i)]; \ + for (int j = k; j < k + offset; j++) { \ + uint32_t even = s->c[j]; \ + uint32_t odd = s->c[j + offset]; \ + s->c[j] = odd + even; \ + /* `reduce_montgomery_partial` works on values up to kPrime*R. \ + * `step_root < kPrime` because it's static data. The other term is \ + * less than R because it's 32-bit, and does not overflow because \ + * `odd` is at most 128 * kPrime per the above. */ \ + s->c[j + offset] = reduce_montgomery_partial( \ + uint64_t{step_root} * uint64_t{128 * kPrime + even - odd}); \ + } \ + k += 2 * offset; \ + } \ + } + + ITER(128, 1) + ITER(64, 2) + ITER(32, 4) + ITER(16, 8) + ITER(8, 16) + ITER(4, 32) + ITER(2, 64) + ITER(1, 128) + static_assert(kDegree == 256); +#undef ITER + + // `kInverseDegreeDoubleMontgomery` has been double-converted to Montgomery + // form, so the result is an extra R term in the final result. This also fully + // reduces the partially-reduced values above. for (int i = 0; i < kDegree; i++) { - s->c[i] = reduce_montgomery((uint64_t)s->c[i] * - (uint64_t)kInverseDegreeMontgomery); + s->c[i] = reduce_montgomery(uint64_t{s->c[i]} * + uint64_t{kInverseDegreeDoubleMontgomery}); } } @@ -459,10 +523,10 @@ inline void vector_sub(vector *out, const vector *lhs, } template -inline void vector_mult_scalar(vector *out, const vector *lhs, - const scalar *rhs) { +inline void vector_mult_scalar_montgomery(vector *out, const vector *lhs, + const scalar *rhs) { for (int i = 0; i < X; i++) { - scalar_mult(&out->v[i], &lhs->v[i], rhs); + scalar_mult_montgomery(&out->v[i], &lhs->v[i], rhs); } } @@ -474,21 +538,30 @@ inline void vector_ntt(vector *a) { } template -inline void vector_inverse_ntt(vector *a) { +inline void vector_inverse_ntt_montgomery(vector *a) { for (int i = 0; i < X; i++) { - scalar_inverse_ntt(&a->v[i]); + scalar_inverse_ntt_montgomery(&a->v[i]); } } +// matrix_mult_montgomery sets `out` to `m` times `a`, both in the number +// theoretically transformed state and fully reduced. Like +// `scalar_mult_montgomery`, each term gains an extra R^-1 factor. template -inline void matrix_mult(vector *out, const matrix *m, - const vector *a) { - vector_zero(out); +inline void matrix_mult_montgomery(vector *out, const matrix *m, + const vector *a) { + // Rather than reducing every product, sum the L products for each + // coefficient and reduce once. The sum is at most L * (kPrime - 1)^2, which + // is within the range `reduce_montgomery` accepts. + static_assert(uint64_t{L} * (kPrime - 1) * (kPrime - 1) <= + (uint64_t{kPrime} << 32)); for (int i = 0; i < K; i++) { - for (int j = 0; j < L; j++) { - scalar product; - scalar_mult(&product, &m->v[i][j], &a->v[j]); - scalar_add(&out->v[i], &out->v[i], &product); + for (int n = 0; n < kDegree; n++) { + uint64_t sum = 0; + for (int j = 0; j < L; j++) { + sum += uint64_t{m->v[i][j].c[n]} * a->v[j].c[n]; + } + out->v[i].c[n] = reduce_montgomery(sum); } } } @@ -514,12 +587,12 @@ inline void power2_round(uint32_t *r1, uint32_t *r0, uint32_t r) { // Scale back previously rounded value. inline void scale_power2_round(uint32_t *out, uint32_t r1) { - // Pre-condition: 0 <= r1 <= 2^10 - 1 + // Pre-condition: 0 ≤ r1 ≤ 2^10 - 1 assert(r1 < (1u << 10)); *out = r1 << kDroppedBits; - // Post-condition: 0 <= out <= 2^23 - 2^13 = kPrime - 1 + // Post-condition: 0 ≤ out ≤ 2^23 - 2^13 = kPrime - 1 assert(*out < kPrime); } @@ -542,7 +615,7 @@ inline uint32_t high_bits(uint32_t x) { // Below is the formula taken from the reference implementation. // // Here, Gamma2 == 2^18 - 2^8 - // This returns ((ceil(x / 2^7) * (2^10 + 1) + 2^21) / 2^22) mod 2^4 + // This returns ((ceil(x / 2^7) ⋅ (2^10 + 1) + 2^21) / 2^22) mod 2^4 r1 = (r1 * 1025 + (1 << 21)) >> 22; r1 &= 15; } else if constexpr (prime_minus_one_over_gamma2() == 88) { @@ -600,8 +673,8 @@ inline uint32_t use_hint_vartime(uint32_t h, uint32_t r) { if (h) { if constexpr (prime_minus_one_over_gamma2() == 32) { if (r0 > 0) { - // (Q-1)/(2 gamma2) = m = 16, thus |mod m| in the spec turns into |& - // 15|. + // (Q-1)/(2 gamma2) = m = 16, thus `mod m` in the spec turns into + // `& 15`. return (r1 + 1) & 15; } else { return (r1 - 1) & 15; @@ -760,8 +833,12 @@ inline void vector_use_hint_vartime(vector *out, const vector *h, /* Bit packing */ +template +inline void scalar_encode(uint8_t *out, const scalar *s); + // FIPS 204, Algorithm 16 (`SimpleBitPack`). Specialized to bitlen(b) = 4. -inline void scalar_encode_4(uint8_t out[128], const scalar *s) { +template <> +inline void scalar_encode<4>(uint8_t out[128], const scalar *s) { // Every two elements lands on a byte boundary. static_assert(kDegree % 2 == 0, "kDegree must be a multiple of 2"); for (int i = 0; i < kDegree / 2; i++) { @@ -774,7 +851,8 @@ inline void scalar_encode_4(uint8_t out[128], const scalar *s) { } // FIPS 204, Algorithm 16 (`SimpleBitPack`). Specialized to bitlen(b) = 6. -inline void scalar_encode_6(uint8_t out[192], const scalar *s) { +template <> +inline void scalar_encode<6>(uint8_t out[192], const scalar *s) { // Every four elements lands on a byte boundary. static_assert(kDegree % 4 == 0, "kDegree must be a multiple of 4"); for (int i = 0; i < kDegree / 4; i++) { @@ -793,7 +871,8 @@ inline void scalar_encode_6(uint8_t out[192], const scalar *s) { } // FIPS 204, Algorithm 16 (`SimpleBitPack`). Specialized to bitlen(b) = 10. -inline void scalar_encode_10(uint8_t out[320], const scalar *s) { +template <> +inline void scalar_encode<10>(uint8_t out[320], const scalar *s) { // Every four elements lands on a byte boundary. static_assert(kDegree % 4 == 0, "kDegree must be a multiple of 4"); for (int i = 0; i < kDegree / 4; i++) { @@ -813,8 +892,12 @@ inline void scalar_encode_10(uint8_t out[320], const scalar *s) { } } +template +inline void scalar_encode_signed(uint8_t *out, const scalar *s); + // FIPS 204, Algorithm 17 (`BitPack`). Specialized to bitlen(a+b) = 4 and b = 4. -inline void scalar_encode_signed_4_4(uint8_t out[128], const scalar *s) { +template <> +inline void scalar_encode_signed<4, 4>(uint8_t out[128], const scalar *s) { // Every two elements lands on a byte boundary. static_assert(kDegree % 2 == 0, "kDegree must be a multiple of 2"); for (int i = 0; i < kDegree / 2; i++) { @@ -827,7 +910,8 @@ inline void scalar_encode_signed_4_4(uint8_t out[128], const scalar *s) { } // FIPS 204, Algorithm 17 (`BitPack`). Specialized to bitlen(a+b) = 3 and b = 2. -inline void scalar_encode_signed_3_2(uint8_t out[96], const scalar *s) { +template <> +inline void scalar_encode_signed<3, 2>(uint8_t out[96], const scalar *s) { static_assert(kDegree % 8 == 0, "kDegree must be a multiple of 8"); for (int i = 0; i < kDegree / 8; i++) { uint32_t a = mod_sub(2, s->c[8 * i]); @@ -848,7 +932,9 @@ inline void scalar_encode_signed_3_2(uint8_t out[96], const scalar *s) { // FIPS 204, Algorithm 17 (`BitPack`). Specialized to bitlen(a+b) = 13 and b = // 2^12. -inline void scalar_encode_signed_13_12(uint8_t out[416], const scalar *s) { +template <> +inline void scalar_encode_signed<13, (1u << 12)>(uint8_t out[416], + const scalar *s) { static const uint32_t kMax = 1u << 12; // Every two elements lands on a byte boundary. static_assert(kDegree % 8 == 0, "kDegree must be a multiple of 8"); @@ -888,7 +974,9 @@ inline void scalar_encode_signed_13_12(uint8_t out[416], const scalar *s) { // FIPS 204, Algorithm 17 (`BitPack`). Specialized to bitlen(a+b) = 20 and b = // 2^19. -inline void scalar_encode_signed_20_19(uint8_t out[640], const scalar *s) { +template <> +inline void scalar_encode_signed<20, (1u << 19)>(uint8_t out[640], + const scalar *s) { static const uint32_t kMax = 1u << 19; // Every two elements lands on a byte boundary. static_assert(kDegree % 4 == 0, "kDegree must be a multiple of 4"); @@ -914,7 +1002,9 @@ inline void scalar_encode_signed_20_19(uint8_t out[640], const scalar *s) { // FIPS 204, Algorithm 17 (`BitPack`). Specialized to bitlen(a+b) = 18 and b = // 2^17. -inline void scalar_encode_signed_18_17(uint8_t out[576], const scalar *s) { +template <> +inline void scalar_encode_signed<18, (1u << 17)>(uint8_t out[576], + const scalar *s) { static const uint32_t kMax = 1u << 17; static_assert(kDegree % 4 == 0, "kDegree must be a multiple of 4"); for (int i = 0; i < kDegree / 4; i++) { @@ -938,30 +1028,12 @@ inline void scalar_encode_signed_18_17(uint8_t out[576], const scalar *s) { } } -// FIPS 204, Algorithm 17 (`BitPack`). -inline void scalar_encode_signed(uint8_t *out, const scalar *s, int bits, - uint32_t max) { - if (bits == 3) { - assert(max == 2); - scalar_encode_signed_3_2(out, s); - } else if (bits == 4) { - assert(max == 4); - scalar_encode_signed_4_4(out, s); - } else if (bits == 20) { - assert(max == 1u << 19); - scalar_encode_signed_20_19(out, s); - } else if (bits == 18) { - assert(max == 1u << 17); - scalar_encode_signed_18_17(out, s); - } else { - assert(bits == 13); - assert(max == 1u << 12); - scalar_encode_signed_13_12(out, s); - } -} +template +inline void scalar_decode(scalar *out, const uint8_t *in); // FIPS 204, Algorithm 18 (`SimpleBitUnpack`). Specialized for bitlen(b) == 10. -inline void scalar_decode_10(scalar *out, const uint8_t in[320]) { +template <> +inline void scalar_decode<10>(scalar *out, const uint8_t in[320]) { static_assert(kDegree % 4 == 0, "kDegree must be a multiple of 4"); for (int i = 0; i < kDegree / 4; i++) { uint32_t v = CRYPTO_load_u32_le(&in[5 * i]); @@ -972,9 +1044,13 @@ inline void scalar_decode_10(scalar *out, const uint8_t in[320]) { } } +template +inline int scalar_decode_signed(scalar *out, const uint8_t *in); + // FIPS 204, Algorithm 19 (`BitUnpack`). Specialized to bitlen(a+b) = 4 and b = // 4. -inline int scalar_decode_signed_4_4(scalar *out, const uint8_t in[128]) { +template <> +inline int scalar_decode_signed<4, 4>(scalar *out, const uint8_t in[128]) { static_assert(kDegree % 8 == 0, "kDegree must be a multiple of 8"); for (int i = 0; i < kDegree / 8; i++) { uint32_t v = CRYPTO_load_u32_le(&in[4 * i]); @@ -1003,7 +1079,8 @@ inline int scalar_decode_signed_4_4(scalar *out, const uint8_t in[128]) { // FIPS 204, Algorithm 19 (`BitUnpack`). Specialized to bitlen(a+b) = 3 and b = // 2. -inline int scalar_decode_signed_3_2(scalar *out, const uint8_t in[96]) { +template <> +inline int scalar_decode_signed<3, 2>(scalar *out, const uint8_t in[96]) { uint32_t v; uint8_t v_bytes[sizeof(v)] = {0}; static_assert(kDegree % 8 == 0, "kDegree must be a multiple of 8"); @@ -1036,7 +1113,9 @@ inline int scalar_decode_signed_3_2(scalar *out, const uint8_t in[96]) { // FIPS 204, Algorithm 19 (`BitUnpack`). Specialized to bitlen(a+b) = 13 and b = // 2^12. -inline void scalar_decode_signed_13_12(scalar *out, const uint8_t in[416]) { +template <> +inline int scalar_decode_signed<13, (1u << 12)>(scalar *out, + const uint8_t in[416]) { static const uint32_t kMax = 1u << 12; static const uint32_t k13Bits = (1u << 13) - 1; static const uint32_t k7Bits = (1u << 7) - 1; @@ -1059,11 +1138,14 @@ inline void scalar_decode_signed_13_12(scalar *out, const uint8_t in[416]) { out->c[i * 8 + 6] = mod_sub(kMax, (c >> 14) & k13Bits); out->c[i * 8 + 7] = mod_sub(kMax, (c >> 27) | ((uint32_t)d) << 5); } + return 1; } // FIPS 204, Algorithm 19 (`BitUnpack`). Specialized to bitlen(a+b) = 18 and b = // 2^17. -inline void scalar_decode_signed_18_17(scalar *out, const uint8_t in[576]) { +template <> +inline int scalar_decode_signed<18, (1u << 17)>(scalar *out, + const uint8_t in[576]) { static const uint32_t kMax = 1u << 17; static_assert(kDegree % 4 == 0, "kDegree must be a multiple of 4"); @@ -1085,11 +1167,14 @@ inline void scalar_decode_signed_18_17(scalar *out, const uint8_t in[576]) { out->c[i * 4 + 2] = mod_sub(kMax, c); out->c[i * 4 + 3] = mod_sub(kMax, d); } + return 1; } // FIPS 204, Algorithm 19 (`BitUnpack`). Specialized to bitlen(a+b) = 20 and b = // 2^19. -inline void scalar_decode_signed_20_19(scalar *out, const uint8_t in[640]) { +template <> +inline int scalar_decode_signed<20, (1u << 19)>(scalar *out, + const uint8_t in[640]) { static const uint32_t kMax = 1u << 19; static const uint32_t k20Bits = (1u << 20) - 1; @@ -1106,33 +1191,10 @@ inline void scalar_decode_signed_20_19(scalar *out, const uint8_t in[640]) { out->c[i * 4 + 2] = mod_sub(kMax, (b >> 8) & k20Bits); out->c[i * 4 + 3] = mod_sub(kMax, (b >> 28) | ((uint32_t)c) << 4); } + return 1; } -// FIPS 204, Algorithm 19 (`BitUnpack`). -inline int scalar_decode_signed(scalar *out, const uint8_t *in, int bits, - uint32_t max) { - if (bits == 3) { - assert(max == 2); - return scalar_decode_signed_3_2(out, in); - } else if (bits == 4) { - assert(max == 4); - return scalar_decode_signed_4_4(out, in); - } else if (bits == 13) { - assert(max == (1u << 12)); - scalar_decode_signed_13_12(out, in); - return 1; - } else if (bits == 18) { - assert(max == (1u << 17)); - scalar_decode_signed_18_17(out, in); - return 1; - } else if (bits == 20) { - assert(max == (1u << 19)); - scalar_decode_signed_20_19(out, in); - return 1; - } else { - abort(); - } -} + /* Expansion functions */ @@ -1140,100 +1202,260 @@ inline int scalar_decode_signed(scalar *out, const uint8_t *in, int bits, // // Rejection samples a Keccak stream to get uniformly distributed elements. This // is used for matrix expansion and only operates on public inputs. -inline void scalar_from_keccak_vartime( - scalar *out, const uint8_t derived_seed[kRhoBytes + 2]) { - BORINGSSL_keccak_st keccak_ctx; - BORINGSSL_keccak_init(&keccak_ctx, boringssl_shake128); - BORINGSSL_keccak_absorb(&keccak_ctx, derived_seed, kRhoBytes + 2); - assert(keccak_ctx.squeeze_offset == 0); - assert(keccak_ctx.rate_bytes == 168); +inline void scalar_from_keccak_block_vartime(scalar *out, int *done, + const uint8_t block[168]) { + // kStep is the number of loop iterations to do at once. May need to become + // platform dependent at some point. Must be a compile-time constant - if we + // ever need to runtime dispatch on it, it has to be made a template argument. + const size_t kStep = 2; + + // Branchless sampling as long as there is enough space in the output buffer + // even if all sampled numbers are < `kPrime`. + size_t i = 0; + for (; i <= size_t{168 - 3 * kStep} && *done <= int{kDegree - kStep}; + i += 3 * kStep) { + uint32_t d[kStep]; + for (size_t k = 0; k < kStep; ++k) { + size_t bi = i + 3 * k; + d[k] = (uint32_t)block[bi] | ((uint32_t)block[bi + 1] << 8) | + (((uint32_t)block[bi + 2] & 0x7f) << 16); + } + for (size_t k = 0; k < kStep; ++k) { + out->c[*done] = d[k]; + *done += d[k] < kPrime; + } + } + + // Branchful "tail". + for (; i < 168 && *done < kDegree; i += 3) { + // FIPS 204, Algorithm 14 (`CoeffFromThreeBytes`). + uint32_t value = (uint32_t)block[i] | ((uint32_t)block[i + 1] << 8) | + (((uint32_t)block[i + 2] & 0x7f) << 16); + if (value < kPrime) { + out->c[(*done)++] = value; + } + } +} + +// FIPS 204, Algorithm 30 (`RejNTTPoly`). +// +// Rejection samples a Keccak stream to get uniformly distributed elements. This +// is used for matrix expansion and only operates on public inputs. +inline void scalar_from_keccak_vartime(scalar *out, + BORINGSSL_keccak_st *keccak_ctx) { + assert(keccak_ctx->squeeze_offset == 0); + assert(keccak_ctx->rate_bytes == 168); static_assert(168 % 3 == 0, "block and coefficient boundaries do not align"); int done = 0; while (done < kDegree) { uint8_t block[168]; - BORINGSSL_keccak_squeeze(&keccak_ctx, block, sizeof(block)); - for (size_t i = 0; i < sizeof(block) && done < kDegree; i += 3) { - // FIPS 204, Algorithm 14 (`CoeffFromThreeBytes`). - uint32_t value = (uint32_t)block[i] | ((uint32_t)block[i + 1] << 8) | - (((uint32_t)block[i + 2] & 0x7f) << 16); - if (value < kPrime) { - out->c[done++] = value; - } - } + BORINGSSL_keccak_squeeze(keccak_ctx, block, sizeof(block)); + scalar_from_keccak_block_vartime(out, &done, block); + } +} + +#if defined(HAVE_KECCAK_X2) +inline void scalar_from_keccak_vartime_x2(scalar *out[2], + BORINGSSL_keccak_st keccak_ctx[2]) { + assert(keccak_ctx[0].squeeze_offset == 0); + assert(keccak_ctx[0].rate_bytes == 168); + assert(keccak_ctx[1].squeeze_offset == 0); + assert(keccak_ctx[1].rate_bytes == 168); + static_assert(168 % 3 == 0, "block and coefficient boundaries do not align"); + + int done[2] = {0, 0}; + while (done[0] < kDegree || done[1] < kDegree) { + uint8_t block[2][168]; + uint8_t *blocks[] = {block[0], block[1]}; + BORINGSSL_keccak_squeeze_x2(keccak_ctx, blocks, sizeof(block[0])); + scalar_from_keccak_block_vartime(out[0], &done[0], block[0]); + scalar_from_keccak_block_vartime(out[1], &done[1], block[1]); + } +} +#endif + +#if defined(HAVE_KECCAK_X4) +inline void scalar_from_keccak_vartime_x4(scalar *out[4], + BORINGSSL_keccak_st keccak_ctx[]) { + for (int i = 0; i < 4; i++) { + assert(keccak_ctx[i].squeeze_offset == 0); + assert(keccak_ctx[i].rate_bytes == 168); + } + static_assert(168 % 3 == 0, "block and coefficient boundaries do not align"); + + int done[4] = {0, 0, 0, 0}; + while (done[0] < kDegree || done[1] < kDegree || done[2] < kDegree || + done[3] < kDegree) { + uint8_t block[4][168]; + uint8_t *blocks[] = {block[0], block[1], block[2], block[3]}; + BORINGSSL_keccak_squeeze_x4(keccak_ctx, blocks, sizeof(block[0])); + scalar_from_keccak_block_vartime(out[0], &done[0], block[0]); + scalar_from_keccak_block_vartime(out[1], &done[1], block[1]); + scalar_from_keccak_block_vartime(out[2], &done[2], block[2]); + scalar_from_keccak_block_vartime(out[3], &done[3], block[3]); } } +#endif template inline bool coefficient_from_nibble(uint32_t nibble, uint32_t *result); template <> inline bool coefficient_from_nibble<4>(uint32_t nibble, uint32_t *result) { - if (constant_time_declassify_int(nibble < 9)) { - // Knowing bounds on |nibble| seems to tempt some versions of Clang to emit - // a branch, if we don't have a barrier in |mod_sub|. - *result = mod_sub(4, value_barrier_u32(nibble)); - return true; - } - return false; + // Knowing bounds on `nibble` seems to tempt some versions of Clang to emit + // a branch, if we don't have a barrier in `mod_sub`. + *result = mod_sub(4, value_barrier_u32(nibble)); + return constant_time_declassify_int(nibble < 9); } template <> inline bool coefficient_from_nibble<2>(uint32_t nibble, uint32_t *result) { - if (constant_time_declassify_int(nibble < 15)) { - // Knowing bounds on |nibble| seems to tempt some versions of Clang to emit - // a branch, if we don't have a barrier in |mod_sub|. - // Constant time "nibble % 5". - nibble = nibble - 5 * ((205 * nibble) >> 10); - *result = mod_sub(2, value_barrier_u32(nibble)); - return true; + // Knowing bounds on `nibble` seems to tempt some versions of Clang to emit + // a branch, if we don't have a barrier in `mod_sub`. + // Constant time "nibble % 5". + uint32_t rem = nibble - 5 * ((205 * nibble) >> 10); + *result = mod_sub(2, value_barrier_u32(rem)); + return constant_time_declassify_int(nibble < 15); +} + +template +inline void scalar_uniform_block(scalar *out, int *done, + const uint8_t block[136]) { + for (size_t i = 0; i < 136 && *done < kDegree; ++i) { + uint32_t t0 = block[i] & 0x0F; + uint32_t t1 = block[i] >> 4; + // FIPS 204, Algorithm 15 (`CoefFromHalfByte`). Although both the input + // and output here are secret, it is OK to leak when we rejected a byte. + // Individual bytes of the SHAKE-256 stream are (indistinguishable from) + // independent of each other and the original seed, so leaking information + // about the rejected bytes does not reveal the input or output. + *done += coefficient_from_nibble(t0, &out->c[*done]); + if (*done < kDegree) { + *done += coefficient_from_nibble(t1, &out->c[*done]); + } } - return false; } // FIPS 204, Algorithm 31 (`RejBoundedPoly`). template -inline void scalar_uniform(scalar *out, - const uint8_t derived_seed[kSigmaBytes + 2]) { - BORINGSSL_keccak_st keccak_ctx; - BORINGSSL_keccak_init(&keccak_ctx, boringssl_shake256); - BORINGSSL_keccak_absorb(&keccak_ctx, derived_seed, kSigmaBytes + 2); - assert(keccak_ctx.squeeze_offset == 0); - assert(keccak_ctx.rate_bytes == 136); +inline void scalar_uniform(scalar *out, BORINGSSL_keccak_st *keccak_ctx) { + assert(keccak_ctx->squeeze_offset == 0); + assert(keccak_ctx->rate_bytes == 136); int done = 0; while (done < kDegree) { uint8_t block[136]; - BORINGSSL_keccak_squeeze(&keccak_ctx, block, sizeof(block)); - for (size_t i = 0; i < sizeof(block) && done < kDegree; ++i) { - uint32_t t0 = block[i] & 0x0F; - uint32_t t1 = block[i] >> 4; - // FIPS 204, Algorithm 15 (`CoefFromHalfByte`). Although both the input - // and output here are secret, it is OK to leak when we rejected a byte. - // Individual bytes of the SHAKE-256 stream are (indistinguishable from) - // independent of each other and the original seed, so leaking information - // about the rejected bytes does not reveal the input or output. - uint32_t v; - if (coefficient_from_nibble(t0, &v)) { - out->c[done++] = v; - } - if (done < kDegree && coefficient_from_nibble(t1, &v)) { - out->c[done++] = v; - } - } + BORINGSSL_keccak_squeeze(keccak_ctx, block, sizeof(block)); + scalar_uniform_block(out, &done, block); } } +#if defined(HAVE_KECCAK_X2) +template +inline void scalar_uniform_x2(scalar *out[2], + BORINGSSL_keccak_st keccak_ctx[2]) { + assert(keccak_ctx[0].squeeze_offset == 0); + assert(keccak_ctx[0].rate_bytes == 136); + assert(keccak_ctx[1].squeeze_offset == 0); + assert(keccak_ctx[1].rate_bytes == 136); + + int done[2] = {0, 0}; + while (done[0] < kDegree || done[1] < kDegree) { + uint8_t block[2][136]; + uint8_t *blocks[] = {block[0], block[1]}; + BORINGSSL_keccak_squeeze_x2(keccak_ctx, blocks, sizeof(block[0])); + scalar_uniform_block(out[0], &done[0], block[0]); + scalar_uniform_block(out[1], &done[1], block[1]); + } +} +#endif + +#if defined(HAVE_KECCAK_X4) +template +inline void scalar_uniform_x4(scalar *out[4], + BORINGSSL_keccak_st keccak_ctx[]) { + for (int i = 0; i < 4; i++) { + assert(keccak_ctx[i].squeeze_offset == 0); + assert(keccak_ctx[i].rate_bytes == 136); + } + + int done[4] = {0, 0, 0, 0}; + while (done[0] < kDegree || done[1] < kDegree || done[2] < kDegree || + done[3] < kDegree) { + uint8_t block[4][136]; + uint8_t *blocks[] = {block[0], block[1], block[2], block[3]}; + BORINGSSL_keccak_squeeze_x4(keccak_ctx, blocks, sizeof(block[0])); + scalar_uniform_block(out[0], &done[0], block[0]); + scalar_uniform_block(out[1], &done[1], block[1]); + scalar_uniform_block(out[2], &done[2], block[2]); + scalar_uniform_block(out[3], &done[3], block[3]); + } +} +#endif + +// Initializes `ctx` to output the bytes that `ExpandMask` decodes into the +// scalar at position `index`. +inline void sample_mask_keccak_init(struct BORINGSSL_keccak_st *ctx, + const uint8_t rho_prime[kRhoPrimeBytes], + size_t index) { + BORINGSSL_keccak_init(ctx, boringssl_shake256); + BORINGSSL_keccak_absorb(ctx, rho_prime, kRhoPrimeBytes); + uint8_t index_buf[2]; + index_buf[0] = static_cast(index); + index_buf[1] = static_cast(index >> 8); + BORINGSSL_keccak_absorb(ctx, index_buf, 2); +} + // FIPS 204, Algorithm 34 (`ExpandMask`), but just a single step. template inline void scalar_sample_mask(scalar *out, - const uint8_t derived_seed[kRhoPrimeBytes + 2]) { + const uint8_t rho_prime[kRhoPrimeBytes], + size_t index) { uint8_t buf[scalar_le_gamma1_bytes()]; - BORINGSSL_keccak(buf, sizeof(buf), derived_seed, kRhoPrimeBytes + 2, - boringssl_shake256); + struct BORINGSSL_keccak_st ctx; + sample_mask_keccak_init(&ctx, rho_prime, index); + BORINGSSL_keccak_squeeze(&ctx, buf, sizeof(buf)); + scalar_decode_signed() + 1, gamma1()>(out, buf); +} - scalar_decode_signed(out, buf, gamma1_bits() + 1, gamma1()); +#if defined(HAVE_KECCAK_X2) +// Performs `scalar_sample_mask` for `index` and `index + 1` in parallel. +template +inline void scalar_sample_mask_x2(scalar *out[2], + const uint8_t rho_prime[kRhoPrimeBytes], + size_t index) { + BORINGSSL_keccak_st keccak_ctx[2]; + for (int k = 0; k < 2; k++) { + sample_mask_keccak_init(&keccak_ctx[k], rho_prime, index + k); + } + uint8_t buf[2][scalar_le_gamma1_bytes()]; + uint8_t *bufs[] = {buf[0], buf[1]}; + BORINGSSL_keccak_squeeze_x2(keccak_ctx, bufs, sizeof(buf[0])); + for (int k = 0; k < 2; k++) { + scalar_decode_signed() + 1, gamma1()>(out[k], buf[k]); + } } +#endif + +#if defined(HAVE_KECCAK_X4) +// Performs `scalar_sample_mask` for `index` through `index + 3` in parallel. +template +inline void scalar_sample_mask_x4(scalar *out[4], + const uint8_t rho_prime[kRhoPrimeBytes], + size_t index) { + BORINGSSL_keccak_st keccak_ctx[4]; + for (int k = 0; k < 4; k++) { + sample_mask_keccak_init(&keccak_ctx[k], rho_prime, index + k); + } + uint8_t buf[4][scalar_le_gamma1_bytes()]; + uint8_t *bufs[] = {buf[0], buf[1], buf[2], buf[3]}; + BORINGSSL_keccak_squeeze_x4(keccak_ctx, bufs, sizeof(buf[0])); + for (int k = 0; k < 4; k++) { + scalar_decode_signed() + 1, gamma1()>(out[k], buf[k]); + } +} +#endif // FIPS 204, Algorithm 29 (`SampleInBall`). inline void scalar_sample_in_ball_vartime(scalar *out, const uint8_t *seed, @@ -1278,23 +1500,146 @@ inline void scalar_sample_in_ball_vartime(scalar *out, const uint8_t *seed, } } -// FIPS 204, Algorithm 32 (`ExpandA`). +template +inline void matrix_expand_step(scalar *out, const uint8_t rho[kRhoBytes], + int index) { + BORINGSSL_keccak_st keccak_ctx; + BORINGSSL_keccak_init(&keccak_ctx, boringssl_shake128); + BORINGSSL_keccak_absorb(&keccak_ctx, rho, kRhoBytes); + uint8_t index_buf[2]; + index_buf[0] = static_cast(index % L); + index_buf[1] = static_cast(index / L); + BORINGSSL_keccak_absorb(&keccak_ctx, index_buf, 2); + scalar_from_keccak_vartime(out, &keccak_ctx); +} + +#if defined(HAVE_KECCAK_X2) +template +inline void matrix_expand_step_x2(matrix *out, + const uint8_t rho[kRhoBytes], int index) { + BORINGSSL_keccak_st keccak_ctx[2]; + BORINGSSL_keccak_init(&keccak_ctx[0], boringssl_shake128); + BORINGSSL_keccak_init(&keccak_ctx[1], boringssl_shake128); + BORINGSSL_keccak_absorb(&keccak_ctx[0], rho, kRhoBytes); + BORINGSSL_keccak_absorb(&keccak_ctx[1], rho, kRhoBytes); + uint8_t index_buf[2]; + index_buf[0] = static_cast(index % L); + index_buf[1] = static_cast(index / L); + BORINGSSL_keccak_absorb(&keccak_ctx[0], index_buf, 2); + index_buf[0] = static_cast((index + 1) % L); + index_buf[1] = static_cast((index + 1) / L); + BORINGSSL_keccak_absorb(&keccak_ctx[1], index_buf, 2); + + scalar *outs[] = {&out->v[index / L][index % L], + &out->v[(index + 1) / L][(index + 1) % L]}; + scalar_from_keccak_vartime_x2(outs, keccak_ctx); +} +#endif + +#if defined(HAVE_KECCAK_X4) +template +inline void matrix_expand_step_x4(matrix *out, + const uint8_t rho[kRhoBytes], int index) { + BORINGSSL_keccak_st keccak_ctx[4]; + for (int k = 0; k < 4; k++) { + BORINGSSL_keccak_init(&keccak_ctx[k], boringssl_shake128); + BORINGSSL_keccak_absorb(&keccak_ctx[k], rho, kRhoBytes); + uint8_t index_buf[2]; + index_buf[0] = static_cast((index + k) % L); + index_buf[1] = static_cast((index + k) / L); + BORINGSSL_keccak_absorb(&keccak_ctx[k], index_buf, 2); + } + + scalar *outs[] = {&out->v[index / L][index % L], + &out->v[(index + 1) / L][(index + 1) % L], + &out->v[(index + 2) / L][(index + 2) % L], + &out->v[(index + 3) / L][(index + 3) % L]}; + scalar_from_keccak_vartime_x4(outs, keccak_ctx); +} +#endif + template inline void matrix_expand(matrix *out, const uint8_t rho[kRhoBytes]) { static_assert(K <= 0x100, "K must fit in 8 bits"); static_assert(L <= 0x100, "L must fit in 8 bits"); - uint8_t derived_seed[kRhoBytes + 2]; - OPENSSL_memcpy(derived_seed, rho, kRhoBytes); - for (int i = 0; i < K; i++) { - for (int j = 0; j < L; j++) { - derived_seed[kRhoBytes + 1] = (uint8_t)i; - derived_seed[kRhoBytes] = (uint8_t)j; - scalar_from_keccak_vartime(&out->v[i][j], derived_seed); + int index = 0; +#if defined(HAVE_KECCAK_X4) + if (BORINGSSL_have_keccak_x4()) { + for (; index + 4 <= K * L; index += 4) { + matrix_expand_step_x4(out, rho, index); } } +#endif +#if defined(HAVE_KECCAK_X2) + for (; index + 2 <= K * L; index += 2) { + matrix_expand_step_x2(out, rho, index); + } +#endif + + for (; index < K * L; index++) { + matrix_expand_step(&out->v[index / L][index % L], rho, index); + } +} + +template +inline void vector_expand_short_step(scalar *out, + const uint8_t sigma[kSigmaBytes], + int index) { + BORINGSSL_keccak_st keccak_ctx; + BORINGSSL_keccak_init(&keccak_ctx, boringssl_shake256); + BORINGSSL_keccak_absorb(&keccak_ctx, sigma, kSigmaBytes); + uint8_t index_buf[2]; + index_buf[0] = static_cast(index); + index_buf[1] = 0; + BORINGSSL_keccak_absorb(&keccak_ctx, index_buf, 2); + scalar_uniform()>(out, &keccak_ctx); } +#if defined(HAVE_KECCAK_X2) +template +inline void vector_expand_short_step_x2(scalar *out0, scalar *out1, + const uint8_t sigma[kSigmaBytes], + int index) { + BORINGSSL_keccak_st keccak_ctx[2]; + BORINGSSL_keccak_init(&keccak_ctx[0], boringssl_shake256); + BORINGSSL_keccak_init(&keccak_ctx[1], boringssl_shake256); + BORINGSSL_keccak_absorb(&keccak_ctx[0], sigma, kSigmaBytes); + BORINGSSL_keccak_absorb(&keccak_ctx[1], sigma, kSigmaBytes); + uint8_t index_buf[2]; + index_buf[0] = static_cast(index); + index_buf[1] = 0; + BORINGSSL_keccak_absorb(&keccak_ctx[0], index_buf, 2); + index_buf[0] = static_cast(index + 1); + index_buf[1] = 0; + BORINGSSL_keccak_absorb(&keccak_ctx[1], index_buf, 2); + + scalar *outs[] = {out0, out1}; + scalar_uniform_x2()>(outs, keccak_ctx); +} +#endif + +#if defined(HAVE_KECCAK_X4) +template +inline void vector_expand_short_step_x4(scalar *out0, scalar *out1, + scalar *out2, scalar *out3, + const uint8_t sigma[kSigmaBytes], + int index) { + BORINGSSL_keccak_st keccak_ctx[4]; + for (int k = 0; k < 4; k++) { + BORINGSSL_keccak_init(&keccak_ctx[k], boringssl_shake256); + BORINGSSL_keccak_absorb(&keccak_ctx[k], sigma, kSigmaBytes); + uint8_t index_buf[2]; + index_buf[0] = static_cast(index + k); + index_buf[1] = 0; + BORINGSSL_keccak_absorb(&keccak_ctx[k], index_buf, 2); + } + + scalar *outs[] = {out0, out1, out2, out3}; + scalar_uniform_x4()>(outs, keccak_ctx); +} +#endif + // FIPS 204, Algorithm 33 (`ExpandS`). template inline void vector_expand_short(vector *s1, vector *s2, @@ -1303,34 +1648,64 @@ inline void vector_expand_short(vector *s1, vector *s2, static_assert(L <= 0x100, "L must fit in 8 bits"); static_assert(K + L <= 0x100, "K+L must fit in 8 bits"); - uint8_t derived_seed[kSigmaBytes + 2]; - OPENSSL_memcpy(derived_seed, sigma, kSigmaBytes); - derived_seed[kSigmaBytes] = 0; - derived_seed[kSigmaBytes + 1] = 0; + scalar *all_scalars[K + L]; for (int i = 0; i < L; i++) { - scalar_uniform()>(&s1->v[i], derived_seed); - ++derived_seed[kSigmaBytes]; + all_scalars[i] = &s1->v[i]; } for (int i = 0; i < K; i++) { - scalar_uniform()>(&s2->v[i], derived_seed); - ++derived_seed[kSigmaBytes]; + all_scalars[L + i] = &s2->v[i]; + } + + int index = 0; +#if defined(HAVE_KECCAK_X4) + if (BORINGSSL_have_keccak_x4()) { + for (; index + 4 <= K + L; index += 4) { + vector_expand_short_step_x4(all_scalars[index], all_scalars[index + 1], + all_scalars[index + 2], + all_scalars[index + 3], sigma, index); + } + } +#endif +#if defined(HAVE_KECCAK_X2) + for (; index + 2 <= K + L; index += 2) { + vector_expand_short_step_x2(all_scalars[index], all_scalars[index + 1], + sigma, index); + } +#endif + + for (; index < K + L; index++) { + vector_expand_short_step(all_scalars[index], sigma, index); } } // FIPS 204, Algorithm 34 (`ExpandMask`). template inline void vector_expand_mask(vector *out, - const uint8_t seed[kRhoPrimeBytes], + const uint8_t rho_prime[kRhoPrimeBytes], size_t kappa) { assert(kappa + L <= 0x10000); - uint8_t derived_seed[kRhoPrimeBytes + 2]; - OPENSSL_memcpy(derived_seed, seed, kRhoPrimeBytes); - for (int i = 0; i < L; i++) { - size_t index = kappa + i; - derived_seed[kRhoPrimeBytes] = index & 0xFF; - derived_seed[kRhoPrimeBytes + 1] = (index >> 8) & 0xFF; - scalar_sample_mask(&out->v[i], derived_seed); + // Each scalar is sampled independently, so use the parallel Keccak + // implementations where possible. + int i = 0; +#if defined(HAVE_KECCAK_X4) + if (BORINGSSL_have_keccak_x4()) { + for (; i + 4 <= L; i += 4) { + scalar *outs[] = {&out->v[i], &out->v[i + 1], &out->v[i + 2], + &out->v[i + 3]}; + scalar_sample_mask_x4(outs, rho_prime, kappa + i); + } + } +#endif +#if defined(HAVE_KECCAK_X2) + for (; i + 2 <= L; i += 2) { + scalar *outs[] = {&out->v[i], &out->v[i + 1]}; + scalar_sample_mask_x2(outs, rho_prime, kappa + i); + } +#endif + + for (; i < L; i++) { + scalar_sample_mask(&out->v[i], rho_prime, kappa + i); } } @@ -1338,54 +1713,60 @@ inline void vector_expand_mask(vector *out, // FIPS 204, Algorithm 16 (`SimpleBitPack`). // -// Encodes an entire vector into 32*K*|bits| bytes. Note that since 256 +// Encodes an entire vector into 32*K*`bits` bytes. Note that since 256 // (kDegree) is divisible by 8, the individual vector entries will always fill a // whole number of bytes, so we do not need to worry about bit packing here. -template -inline void vector_encode(uint8_t *out, const vector *a, int bits) { - if (bits == 4) { - for (int i = 0; i < K; i++) { - scalar_encode_4(out + i * bits * kDegree / 8, &a->v[i]); - } - } else if (bits == 6) { - for (int i = 0; i < K; i++) { - scalar_encode_6(out + i * bits * kDegree / 8, &a->v[i]); - } - } else { - assert(bits == 10); - for (int i = 0; i < K; i++) { - scalar_encode_10(out + i * bits * kDegree / 8, &a->v[i]); - } +template +inline void vector_encode(uint8_t *out, const vector *a) { + for (int i = 0; i < K; i++) { + scalar_encode(out + i * bits * kDegree / 8, &a->v[i]); } } // FIPS 204, Algorithm 18 (`SimpleBitUnpack`). -template -inline void vector_decode_10(vector *out, const uint8_t *in) { +template +inline void vector_decode(vector *out, const uint8_t *in) { for (int i = 0; i < K; i++) { - scalar_decode_10(&out->v[i], in + i * 10 * kDegree / 8); + scalar_decode(&out->v[i], in + i * bits * kDegree / 8); } } // FIPS 204, Algorithm 17 (`BitPack`). // -// Encodes an entire vector into 32*L*|bits| bytes. Note that since 256 +// Encodes an entire vector into 32*L*`bits` bytes. Note that since 256 // (kDegree) is divisible by 8, the individual vector entries will always fill a // whole number of bytes, so we do not need to worry about bit packing here. -template -inline void vector_encode_signed(uint8_t *out, const vector *a, int bits, - uint32_t max) { +template +inline void vector_encode_signed(uint8_t *out, const vector *a) { for (int i = 0; i < X; i++) { - scalar_encode_signed(out + i * bits * kDegree / 8, &a->v[i], bits, max); + scalar_encode_signed(out + i * bits * kDegree / 8, &a->v[i]); } } -template -inline int vector_decode_signed(vector *out, const uint8_t *in, int bits, - uint32_t max) { +// `vector_encode_signed_ntt` behaves like `vector_encode_signed` but takes its +// input in NTT form. This is only used by the testing-only `skEncode` and +// `skDecode` functions and is not performance-sensitive. +template +inline void vector_encode_signed_ntt(uint8_t *out, const vector *a_ntt) { + vector a = *a_ntt; + vector_inverse_ntt_montgomery(&a); + // `vector_inverse_ntt_montgomery` adds an extra factor of R, which we must + // cancel with a Montgomery reduction. for (int i = 0; i < X; i++) { - if (!scalar_decode_signed(&out->v[i], in + i * bits * kDegree / 8, bits, - max)) { + for (int j = 0; j < kDegree; j++) { + a.v[i].c[j] = reduce_montgomery(a.v[i].c[j]); + } + } + + vector_encode_signed(out, &a); +} + +// FIPS 204, Algorithm 19 (`BitUnpack`). +template +inline int vector_decode_signed(vector *out, const uint8_t *in) { + for (int i = 0; i < X; i++) { + if (!scalar_decode_signed(&out->v[i], + in + i * bits * kDegree / 8)) { return 0; } } @@ -1395,7 +1776,7 @@ inline int vector_decode_signed(vector *out, const uint8_t *in, int bits, // FIPS 204, Algorithm 28 (`w1Encode`). template inline void w1_encode(uint8_t out[w1_bytes()], const vector *w1) { - vector_encode(out, w1, w1_coeffs_bits()); + vector_encode()>(out, w1); } // FIPS 204, Algorithm 20 (`HintBitPack`). @@ -1458,9 +1839,9 @@ template struct private_key { public_key pub; uint8_t k[kKBytes]; - vector s1; - vector s2; - vector t0; + vector s1_ntt; + vector s2_ntt; + vector t0_ntt; }; template @@ -1481,7 +1862,7 @@ inline int mldsa_marshal_public_key(CBB *out, const public_key *pub) { if (!CBB_add_space(out, &vectork_output, 320 * K)) { return 0; } - vector_encode(vectork_output, &pub->t1, 10); + vector_encode<10>(vectork_output, &pub->t1); return 1; } @@ -1499,7 +1880,7 @@ inline int mldsa_parse_public_key(public_key *pub, CBS *in) { if (!CBS_get_bytes(in, &t1_bytes, 320 * K) || CBS_len(in) != 0) { return 0; } - vector_decode_10(&pub->t1, CBS_data(&t1_bytes)); + vector_decode<10>(&pub->t1, CBS_data(&t1_bytes)); // Compute pre-cached values. BORINGSSL_keccak(pub->public_key_hash, sizeof(pub->public_key_hash), @@ -1524,21 +1905,21 @@ inline int mldsa_marshal_private_key(CBB *out, const private_key *priv) { if (!CBB_add_space(out, &vectorl_output, scalar_bytes * L)) { return 0; } - vector_encode_signed(vectorl_output, &priv->s1, plus_minus_eta_bitlen(), - eta()); + vector_encode_signed_ntt(), eta()>(vectorl_output, + &priv->s1_ntt); uint8_t *s2_output; if (!CBB_add_space(out, &s2_output, scalar_bytes * K)) { return 0; } - vector_encode_signed(s2_output, &priv->s2, plus_minus_eta_bitlen(), - eta()); + vector_encode_signed_ntt(), eta()>(s2_output, + &priv->s2_ntt); uint8_t *t0_output; if (!CBB_add_space(out, &t0_output, 416 * K)) { return 0; } - vector_encode_signed(t0_output, &priv->t0, 13, 1 << 12); + vector_encode_signed_ntt<13, (1 << 12)>(t0_output, &priv->t0_ntt); return 1; } @@ -1554,18 +1935,17 @@ inline int mldsa_parse_private_key(private_key *priv, CBS *in) { !CBS_copy_bytes(in, priv->k, sizeof(priv->k)) || !CBS_get_bytes(in, &public_key_hash, kTrBytes) || !CBS_get_bytes(in, &s1_bytes, scalar_bytes * L) || - !vector_decode_signed(&priv->s1, CBS_data(&s1_bytes), - plus_minus_eta_bitlen(), eta()) || + !vector_decode_signed(), eta()>( + &priv->s1_ntt, CBS_data(&s1_bytes)) || !CBS_get_bytes(in, &s2_bytes, scalar_bytes * K) || - !vector_decode_signed(&priv->s2, CBS_data(&s2_bytes), - plus_minus_eta_bitlen(), eta()) || - !CBS_get_bytes(in, &t0_bytes, 416 * K) || - // Note: Decoding 13 bits into (-2^12, 2^12] cannot fail. - !vector_decode_signed(&priv->t0, CBS_data(&t0_bytes), 13, 1 << 12)) { + !vector_decode_signed(), eta()>( + &priv->s2_ntt, CBS_data(&s2_bytes)) || + !CBS_get_bytes(in, &t0_bytes, 416 * K)) { return 0; } - // Compute `t1`, which is not in the `skDecode` input. + // Compute `t1`, which is not in the `skDecode` input. This also computes + // `t0_ntt` and converts `s1_ntt` and `s2_ntt` to NTT form. uint8_t unused[public_key_bytes()]; if (!mldsa_finish_keygen(unused, priv)) { return 0; @@ -1574,7 +1954,7 @@ inline int mldsa_parse_private_key(private_key *priv, CBS *in) { // As a side effect of computing `t1`, we also compute `t0` and // `public_key_hash`. Check they match the received bytes. uint8_t t0_computed[416 * K]; - vector_encode_signed(t0_computed, &priv->t0, 13, 1 << 12); + vector_encode_signed_ntt<13, (1 << 12)>(t0_computed, &priv->t0_ntt); if (!CBS_mem_equal(&public_key_hash, priv->pub.public_key_hash, sizeof(priv->pub.public_key_hash)) || !CBS_mem_equal(&t0_bytes, t0_computed, sizeof(t0_computed))) { @@ -1595,8 +1975,8 @@ inline int mldsa_marshal_signature(CBB *out, const signature *sign) { if (!CBB_add_space(out, &vectorl_output, scalar_le_gamma1_bytes() * L)) { return 0; } - vector_encode_signed(vectorl_output, &sign->z, gamma1_bits() + 1, - gamma1()); + vector_encode_signed() + 1, gamma1()>(vectorl_output, + &sign->z); uint8_t *hint_output; if (!CBB_add_space(out, &hint_output, omega() + K)) { @@ -1615,8 +1995,8 @@ inline int mldsa_parse_signature(signature *sign, CBS *in) { if (!CBS_copy_bytes(in, sign->c_tilde, sizeof(sign->c_tilde)) || !CBS_get_bytes(in, &z_bytes, scalar_le_gamma1_bytes() * L) || // Note: Decoding b+1 bits into (-2^b, 2^b] cannot fail. - !vector_decode_signed(&sign->z, CBS_data(&z_bytes), gamma1_bits() + 1, - gamma1()) || + !vector_decode_signed() + 1, gamma1()>( + &sign->z, CBS_data(&z_bytes)) || !CBS_get_bytes(in, &hint_bytes, omega() + K) || !hint_bit_unpack(&sign->h, CBS_data(&hint_bytes))) { return 0; @@ -1627,6 +2007,9 @@ inline int mldsa_parse_signature(signature *sign, CBS *in) { // FIPS 204, Algorithm 6 (`ML-DSA.KeyGen_internal`), steps 3 and 5–11. // Returns 1 on success and 0 on failure. +// +// On input, `priv->s1_ntt` and `priv->s2_ntt` are not yet in NTT form. They are +// converted to NTT form by this function. template inline int mldsa_finish_keygen( uint8_t out_encoded_public_key[public_key_bytes()], @@ -1636,7 +2019,6 @@ inline int mldsa_finish_keygen( struct Values { enum { kAllowUniquePtr = true }; matrix a_ntt; - vector s1_ntt; vector t; }; auto values = MakeUnique(); @@ -1647,16 +2029,17 @@ inline int mldsa_finish_keygen( // Step 3. matrix_expand(&values->a_ntt, priv->pub.rho); - // Step 5. - OPENSSL_memcpy(&values->s1_ntt, &priv->s1, sizeof(values->s1_ntt)); - vector_ntt(&values->s1_ntt); - - matrix_mult(&values->t, &values->a_ntt, &values->s1_ntt); - vector_inverse_ntt(&values->t); - vector_add(&values->t, &values->t, &priv->s2); - - // Step 6-7. - vector_power2_round(&priv->pub.t1, &priv->t0, &values->t); + // Step 5. Note that, on input, `s1_ntt` and `s2_ntt` are not yet in NTT form, + // but this function is responsible for converting them to NTT. + vector_ntt(&priv->s1_ntt); + matrix_mult_montgomery(&values->t, &values->a_ntt, &priv->s1_ntt); + vector_inverse_ntt_montgomery(&values->t); + vector_add(&values->t, &values->t, &priv->s2_ntt); + vector_ntt(&priv->s2_ntt); + + // Step 6-7. We store t0 in NTT form. + vector_power2_round(&priv->pub.t1, &priv->t0_ntt, &values->t); + vector_ntt(&priv->t0_ntt); // t1 is public. CONSTTIME_DECLASSIFY(&priv->pub.t1, sizeof(priv->pub.t1)); @@ -1698,8 +2081,9 @@ inline int mldsa_generate_key_external_entropy_no_self_test( CONSTTIME_DECLASSIFY(rho, kRhoBytes); OPENSSL_memcpy(priv->pub.rho, rho, sizeof(priv->pub.rho)); OPENSSL_memcpy(priv->k, k, sizeof(priv->k)); - // Step 4. This is independent of A (step 3) and can be done first. - vector_expand_short(&priv->s1, &priv->s2, sigma); + // Steps 4. This is independent of A (step 3) and can be done first. + // `mldsa_finish_keygen` will convert `s1_ntt` and `s2_ntt` into NTT from. + vector_expand_short(&priv->s1_ntt, &priv->s2_ntt, sigma); // Steps 3 and 5-11. return mldsa_finish_keygen(out_encoded_public_key, priv); } @@ -1734,9 +2118,6 @@ inline int mldsa_sign_mu_no_self_test( struct Values { enum { kAllowUniquePtr = true }; signature sign; - vector s1_ntt; - vector s2_ntt; - vector t0_ntt; matrix a_ntt; vector y; vector w; @@ -1748,15 +2129,6 @@ inline int mldsa_sign_mu_no_self_test( if (values == nullptr) { return 0; } - OPENSSL_memcpy(&values->s1_ntt, &priv->s1, sizeof(values->s1_ntt)); - vector_ntt(&values->s1_ntt); - - OPENSSL_memcpy(&values->s2_ntt, &priv->s2, sizeof(values->s2_ntt)); - vector_ntt(&values->s2_ntt); - - OPENSSL_memcpy(&values->t0_ntt, &priv->t0, sizeof(values->t0_ntt)); - vector_ntt(&values->t0_ntt); - matrix_expand(&values->a_ntt, priv->pub.rho); // kappa must not exceed 2**16/L = 13107. But the probability of it @@ -1768,8 +2140,8 @@ inline int mldsa_sign_mu_no_self_test( OPENSSL_memcpy(y_ntt, &values->y, sizeof(*y_ntt)); vector_ntt(y_ntt); - matrix_mult(&values->w, &values->a_ntt, y_ntt); - vector_inverse_ntt(&values->w); + matrix_mult_montgomery(&values->w, &values->a_ntt, y_ntt); + vector_inverse_ntt_montgomery(&values->w); vector_high_bits(&values->w1, &values->w); uint8_t w1_encoded[w1_bytes()]; @@ -1786,10 +2158,10 @@ inline int mldsa_sign_mu_no_self_test( sizeof(values->sign.c_tilde), tau()); scalar_ntt(&c_ntt); - vector_mult_scalar(&values->cs1, &values->s1_ntt, &c_ntt); - vector_inverse_ntt(&values->cs1); - vector_mult_scalar(&values->cs2, &values->s2_ntt, &c_ntt); - vector_inverse_ntt(&values->cs2); + vector_mult_scalar_montgomery(&values->cs1, &priv->s1_ntt, &c_ntt); + vector_inverse_ntt_montgomery(&values->cs1); + vector_mult_scalar_montgomery(&values->cs2, &priv->s2_ntt, &c_ntt); + vector_inverse_ntt_montgomery(&values->cs2); vector_add(&values->sign.z, &values->y, &values->cs1); @@ -1804,7 +2176,7 @@ inline int mldsa_sign_mu_no_self_test( // https://pq-crystals.org/dilithium/data/dilithium-specification-round3.pdf // describes this leak as OK. Note we leak less than what is described by // the paper; we do not reveal which coefficient violated the bound, and - // we hide which of the |z_max| or |r0_max| bound failed. See also + // we hide which of the `z_max` or `r0_max` bound failed. See also // https://boringssl-review.googlesource.com/c/boringssl/+/67747/comment/2bbab0fa_d241d35a/ uint32_t z_max = vector_max(&values->sign.z); uint32_t r0_max = vector_max_signed(r0); @@ -1820,8 +2192,8 @@ inline int mldsa_sign_mu_no_self_test( } vector *ct0 = &values->w1; - vector_mult_scalar(ct0, &values->t0_ntt, &c_ntt); - vector_inverse_ntt(ct0); + vector_mult_scalar_montgomery(ct0, &priv->t0_ntt, &c_ntt); + vector_inverse_ntt_montgomery(ct0); vector_make_hint(&values->sign.h, ct0, &values->cs2, &values->w); // See above. @@ -1970,16 +2342,16 @@ inline int mldsa_verify_mu_no_self_test( OPENSSL_memcpy(&values->z_ntt, &values->sign.z, sizeof(values->z_ntt)); vector_ntt(&values->z_ntt); - matrix_mult(&values->az_ntt, &values->a_ntt, &values->z_ntt); + matrix_mult_montgomery(&values->az_ntt, &values->a_ntt, &values->z_ntt); vector_scale_power2_round(&values->ct1_ntt, &pub->t1); vector_ntt(&values->ct1_ntt); - vector_mult_scalar(&values->ct1_ntt, &values->ct1_ntt, &c_ntt); + vector_mult_scalar_montgomery(&values->ct1_ntt, &values->ct1_ntt, &c_ntt); vector *const w1 = &values->az_ntt; vector_sub(w1, &values->az_ntt, &values->ct1_ntt); - vector_inverse_ntt(w1); + vector_inverse_ntt_montgomery(w1); vector_use_hint_vartime(w1, &values->sign.h, w1); uint8_t w1_encoded[w1_bytes()]; @@ -2324,8 +2696,8 @@ bcm_status bssl::BCM_mldsa65_check_key_fips(MLDSA65_private_key *private_key) { mldsa::fips::check_key(mldsa::private_key_from_external_65(private_key))); } -// Calls |MLDSA_generate_key_external_entropy| with random bytes from -// |BCM_rand_bytes|. +// Calls `MLDSA_generate_key_external_entropy` with random bytes from +// `BCM_rand_bytes`. bcm_status bssl::BCM_mldsa65_generate_key( uint8_t out_encoded_public_key[MLDSA65_PUBLIC_KEY_BYTES], uint8_t out_seed[MLDSA_SEED_BYTES], MLDSA65_private_key *out_private_key) { @@ -2429,7 +2801,7 @@ bcm_status bssl::BCM_mldsa65_sign_mu_internal( } // ML-DSA signature in randomized mode, filling the random bytes with -// |BCM_rand_bytes|. +// `BCM_rand_bytes`. bcm_status bssl::BCM_mldsa65_sign( uint8_t out_encoded_signature[MLDSA65_SIGNATURE_BYTES], const MLDSA65_private_key *private_key, const uint8_t *msg, size_t msg_len, @@ -2528,7 +2900,7 @@ int bssl::BCM_mldsa65_public_keys_equal(const MLDSA65_public_key *a, const MLDSA65_public_key *b) { auto *a_pub = mldsa::public_key_from_external_65(a); auto *b_pub = mldsa::public_key_from_external_65(b); - // It is sufficient to compare |public_key_hash|. When importing a public key, + // It is sufficient to compare `public_key_hash`. When importing a public key, // the hash must be computed. When importing a private key in expanded form // (an internal testing-only API), the hash is provided, but we recompute it // and check for correctness. @@ -2564,8 +2936,8 @@ bcm_status bssl::BCM_mldsa87_check_key_fips(MLDSA87_private_key *private_key) { mldsa::fips::check_key(mldsa::private_key_from_external_87(private_key))); } -// Calls |MLDSA_generate_key_external_entropy| with random bytes from -// |BCM_rand_bytes|. +// Calls `MLDSA_generate_key_external_entropy` with random bytes from +// `BCM_rand_bytes`. bcm_status bssl::BCM_mldsa87_generate_key( uint8_t out_encoded_public_key[MLDSA87_PUBLIC_KEY_BYTES], uint8_t out_seed[MLDSA_SEED_BYTES], MLDSA87_private_key *out_private_key) { @@ -2669,7 +3041,7 @@ bcm_status bssl::BCM_mldsa87_sign_mu_internal( } // ML-DSA signature in randomized mode, filling the random bytes with -// |BCM_rand_bytes|. +// `BCM_rand_bytes`. bcm_status bssl::BCM_mldsa87_sign( uint8_t out_encoded_signature[MLDSA87_SIGNATURE_BYTES], const MLDSA87_private_key *private_key, const uint8_t *msg, size_t msg_len, @@ -2769,7 +3141,7 @@ int bssl::BCM_mldsa87_public_keys_equal(const MLDSA87_public_key *a, const MLDSA87_public_key *b) { auto *a_pub = mldsa::public_key_from_external_87(a); auto *b_pub = mldsa::public_key_from_external_87(b); - // It is sufficient to compare |public_key_hash|. When importing a public key, + // It is sufficient to compare `public_key_hash`. When importing a public key, // the hash must be computed. When importing a private key in expanded form // (an internal testing-only API), the hash is provided, but we recompute it // and check for correctness. @@ -2805,8 +3177,8 @@ bcm_status bssl::BCM_mldsa44_check_key_fips(MLDSA44_private_key *private_key) { mldsa::fips::check_key(mldsa::private_key_from_external_44(private_key))); } -// Calls |MLDSA_generate_key_external_entropy| with random bytes from -// |BCM_rand_bytes|. +// Calls `MLDSA_generate_key_external_entropy` with random bytes from +// `BCM_rand_bytes`. bcm_status bssl::BCM_mldsa44_generate_key( uint8_t out_encoded_public_key[MLDSA44_PUBLIC_KEY_BYTES], uint8_t out_seed[MLDSA_SEED_BYTES], MLDSA44_private_key *out_private_key) { @@ -2910,7 +3282,7 @@ bcm_status bssl::BCM_mldsa44_sign_mu_internal( } // ML-DSA signature in randomized mode, filling the random bytes with -// |BCM_rand_bytes|. +// `BCM_rand_bytes`. bcm_status bssl::BCM_mldsa44_sign( uint8_t out_encoded_signature[MLDSA44_SIGNATURE_BYTES], const MLDSA44_private_key *private_key, const uint8_t *msg, size_t msg_len, @@ -3010,7 +3382,7 @@ int bssl::BCM_mldsa44_public_keys_equal(const MLDSA44_public_key *a, const MLDSA44_public_key *b) { auto *a_pub = mldsa::public_key_from_external_44(a); auto *b_pub = mldsa::public_key_from_external_44(b); - // It is sufficient to compare |public_key_hash|. When importing a public key, + // It is sufficient to compare `public_key_hash`. When importing a public key, // the hash must be computed. When importing a private key in expanded form // (an internal testing-only API), the hash is provided, but we recompute it // and check for correctness. diff --git a/third_party/boringssl/src/crypto/fipsmodule/mlkem/mlkem.cc.inc b/third_party/boringssl/src/crypto/fipsmodule/mlkem/mlkem.cc.inc index 540b5b390..ab640efbc 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/mlkem/mlkem.cc.inc +++ b/third_party/boringssl/src/crypto/fipsmodule/mlkem/mlkem.cc.inc @@ -62,6 +62,13 @@ inline void prf_x2(uint8_t *outs[2], size_t out_len, } #endif +#if defined(HAVE_KECCAK_X4) +inline void prf_x4(uint8_t *outs[4], size_t out_len, + const uint8_t *ins[4] /* 33 each */) { + BORINGSSL_keccak_short_x4(outs, out_len, ins, 33, boringssl_shake256); +} +#endif + // Section 4.1 void hash_h(uint8_t out[32], const uint8_t *in, size_t len) { BORINGSSL_keccak(out, 32, in, len, boringssl_sha3_256); @@ -84,8 +91,8 @@ void kdf(uint8_t out[MLKEM_SHARED_SECRET_BYTES], // Constants that are common across all sizes. #define DEGREE 256 -const size_t kBarrettMultiplier = 5039; -const unsigned kBarrettShift = 24; +const size_t kBarrettMultiplier = 1290167; +const unsigned kBarrettShift = 32; const uint16_t kPrime = 3329; const int kLog2Prime = 12; const uint16_t kHalfPrime = (/*kPrime=*/3329 - 1) / 2; @@ -212,49 +219,55 @@ inline uint16_t reduce_once(uint16_t x) { // We usually add value barriers to selects because Clang turns consecutive // selects with the same condition into a branch instead of CMOV/CSEL. This // condition does not occur in ML-KEM, so omitting it seems to be safe so far, - // but see |scalar_centered_binomial_distribution_eta_2_with_prf|. + // but see `scalar_centered_binomial_distribution_eta_2_with_prf`. return (mask & x) | (~mask & subtracted); } -// constant time reduce x mod kPrime using Barrett reduction. x must be less -// than kPrime + 2×kPrime². -inline uint16_t reduce(uint32_t x) { - declassify_assert(x < kPrime + 2u * kPrime * kPrime); +// reduce_partial returns a number equivalent to x mod kPrime. The output is +// less than 2 * kPrime. `x` can be any `uint32_t`. +inline uint32_t reduce_partial(uint32_t x) { uint64_t product = (uint64_t)x * kBarrettMultiplier; uint32_t quotient = (uint32_t)(product >> kBarrettShift); uint32_t remainder = x - quotient * kPrime; - return reduce_once(remainder); + return remainder; } -inline void scalar_zero(scalar *out) { OPENSSL_memset(out, 0, sizeof(*out)); } - -template -inline void vector_zero(vector *out) { - OPENSSL_memset(out->v, 0, sizeof(scalar) * RANK); -} +// constant time reduce x mod kPrime using Barrett reduction. `x` can be any +// `uint32_t`. +inline uint16_t reduce(uint32_t x) { return reduce_once(reduce_partial(x)); } // In place number theoretic transform of a given scalar. // Note that MLKEM's kPrime 3329 does not have a 512th root of unity, so this // transform leaves off the last iteration of the usual FFT code, with the 128 -// relevant roots of unity being stored in |kNTTRoots|. This means the output +// relevant roots of unity being stored in `kNTTRoots`. This means the output // should be seen as 128 elements in GF(3329^2), with the coefficients of the -// elements being consecutive entries in |s->c|. +// elements being consecutive entries in `s->c`. inline void scalar_ntt(scalar *s) { // Manually unrolled loop to maximize vectorization. -#define ITER(step, offset) \ - { \ - int k = 0; \ - for (int i = 0; i < step; i++) { \ - const uint32_t step_root = kNTTRoots[i + step]; \ - for (int j = k; j < k + offset; j++) { \ - uint16_t odd = reduce(step_root * s->c[j + offset]); \ - uint16_t even = s->c[j]; \ - s->c[j] = reduce_once(odd + even); \ - s->c[j + offset] = reduce_once(even - odd + kPrime); \ - } \ - k += 2 * offset; \ - } \ + // + // Intermediate values of `s` are not fully reduced. `ITER` writes to every + // element of `s` exactly once. Each write adds less than 2*kPrime to some + // value from the previous iteration. After seven iterations, each element + // will be less than 15*kPrime. This fits in `uint16_t`, so we never overflow. +#define ITER(step, offset) \ + { \ + int k = 0; \ + for (int i = 0; i < step; i++) { \ + const uint32_t step_root = kNTTRoots[step + i]; \ + for (int j = k; j < k + offset; j++) { \ + uint32_t even = s->c[j]; \ + /* `reduce_partial` works on values up to 2^32. \ + * `step_root < kPrime` because it's static data. `s->c[...] < 2^16` \ + * because it's 16-bit. */ \ + uint32_t odd = \ + reduce_partial(uint32_t{step_root} * uint32_t{s->c[j + offset]}); \ + s->c[j] = even + odd; \ + s->c[j + offset] = even + 2 * kPrime - odd; \ + } \ + k += 2 * offset; \ + } \ } + // for (int step = 1; step < DEGREE / 2; step <<= 1) ITER(1, 128) ITER(2, 64) @@ -265,6 +278,11 @@ inline void scalar_ntt(scalar *s) { ITER(64, 2) static_assert(DEGREE == 256); #undef ITER + + // Now `s` is correct but the elements are not fully reduced. Reduce them. + for (int i = 0; i < DEGREE; i++) { + s->c[i] = reduce(s->c[i]); + } } template @@ -278,36 +296,53 @@ inline void vector_ntt(vector *a) { // entries of s->v being interpreted as elements of GF(3329^2). Just as with the // number theoretic transform, this leaves off the first step of the normal iFFT // to account for the fact that 3329 does not have a 512th root of unity, using -// the precomputed 128 roots of unity stored in |kInverseNTTRoots|. +// the precomputed 128 roots of unity stored in `kInverseNTTRoots`. +// +// FIPS 203, Algorithm 10 (`NTT^-1`). void scalar_inverse_ntt(scalar *s) { // Manually unrolled loop to maximize vectorization. -#define ITER(step, offset) \ - { \ - int k = 0; \ - for (int i = 0; i < step; i++) { \ - uint32_t step_root = kInverseNTTRoots[i + step]; \ - for (int j = k; j < k + offset; j++) { \ - uint16_t odd = s->c[j + offset]; \ - uint16_t even = s->c[j]; \ - s->c[j] = reduce_once(odd + even); \ - s->c[j + offset] = reduce(step_root * (even - odd + kPrime)); \ - } \ - k += 2 * offset; \ - } \ + // + // Intermediate values are not fully reduced. `ITER` writes to every element + // exactly once. Each write either adds two values from the previous + // iteration, or is at most 2 * kPrime. After seven iterations, each element + // will be less than 2^7 * kPrime = 128 * kPrime. This fits in `uint32_t`, so + // we never overflow. + uint32_t c[DEGREE]; +#define ITER(step, offset, src) \ + { \ + int k = 0; \ + for (int i = 0; i < step; i++) { \ + const uint32_t step_root = kInverseNTTRoots[step + i]; \ + for (int j = k; j < k + offset; j++) { \ + uint32_t even = (src)[j]; \ + uint32_t odd = (src)[j + offset]; \ + c[j] = odd + even; \ + /* `reduce_partial` works on values up to 2^32. \ + * `step_root < kPrime` because it's static data. The other term is \ + * less than 2^32, and does not overflow because `odd` is at most \ + * 128 * kPrime per the above. */ \ + c[j + offset] = reduce_partial(uint32_t{step_root} * \ + uint32_t{128 * kPrime + even - odd}); \ + } \ + k += 2 * offset; \ + } \ } + // for (int offset = 2; offset < DEGREE; offset <<= 1) - ITER(64, 2) - ITER(32, 4) - ITER(16, 8) - ITER(8, 16) - ITER(4, 32) - ITER(2, 64) - ITER(1, 128) + ITER(64, 2, s->c) + ITER(32, 4, c) + ITER(16, 8, c) + ITER(8, 16, c) + ITER(4, 32, c) + ITER(2, 64, c) + ITER(1, 128, c) static_assert(DEGREE == 256); #undef ITER + // The final multiplication by `kInverseDegree` also fully reduces the + // partially-reduced values above. for (int i = 0; i < DEGREE; i++) { - s->c[i] = reduce(s->c[i] * kInverseDegree); + s->c[i] = reduce(c[i] * kInverseDegree); } } @@ -334,19 +369,75 @@ inline void scalar_sub(scalar *lhs, const scalar *rhs) { // 3329 does not have a 512th root of unity, this means we have to interpret // the 2*ith and (2*i+1)th entries of the scalar as elements of GF(3329)[X]/(X^2 // - 17^(2*bitreverse(i)+1)) The value of 17^(2*bitreverse(i)+1) mod 3329 is -// stored in the precomputed |kModRoots| table. Note that our Barrett transform -// only allows us to multiply two reduced numbers together, so we need some -// intermediate reduction steps, even if an uint64_t could hold 3 multiplied -// numbers. -inline void scalar_mult(scalar *out, const scalar *lhs, const scalar *rhs) { +// stored in the precomputed `kModRoots` table. +// +// Writing the two elements as a0 + a1*X and b0 + b1*X, and the root as zeta, +// their product is (a0*b0 + a1*(b1*zeta)) + (a0*b1 + a1*b0)*X. Products are +// only ever needed as terms of a sum, in matrix-vector and inner products, so +// they are computed lazily: +// +// - b1*zeta mod kPrime only depends on the right-hand side, which is shared by +// every row of a matrix-vector product. It is computed once per scalar and +// stored in a `scalar_mulcache`. This is the "asymmetric multiplication" of +// https://eprint.iacr.org/2021/986. +// - The terms are summed in a `scalar_accumulator` without any modular +// reduction, and the sum is reduced once, at the end. + +// scalar_mulcache holds, for a scalar b, b[2*i+1] * kModRoots[i] mod kPrime. +struct scalar_mulcache { + // On every function entry and exit, 0 <= c < kPrime. + uint16_t c[DEGREE / 2]; +}; + +template +struct vector_mulcache { + scalar_mulcache v[RANK]; +}; + +inline void scalar_mulcache_compute(scalar_mulcache *out, const scalar *b) { + for (int i = 0; i < DEGREE / 2; i++) { + out->c[i] = reduce(uint32_t{b->c[2 * i + 1]} * kModRoots[i]); + } +} + +template +inline void vector_mulcache_compute(vector_mulcache *out, + const vector *b) { + for (int i = 0; i < RANK; i++) { + scalar_mulcache_compute(&out->v[i], &b->v[i]); + } +} + +// scalar_accumulator holds a sum of products of scalars, not reduced mod +// kPrime. +struct scalar_accumulator { + uint32_t c[DEGREE]; +}; + +// Scalars are fully reduced, so each product adds at most 2 * (kPrime - 1)^2 to +// each element of a `scalar_accumulator`. Sums have at most RANK1024 terms, so +// the elements never overflow. +static_assert(uint64_t{RANK1024} * 2 * (kPrime - 1) * (kPrime - 1) <= + UINT32_MAX); + +// scalar_mult_accumulate adds `lhs * rhs` to `acc`. `rhs_cache` must have been +// computed from `rhs`. +inline void scalar_mult_accumulate(scalar_accumulator *acc, const scalar *lhs, + const scalar *rhs, + const scalar_mulcache *rhs_cache) { for (int i = 0; i < DEGREE / 2; i++) { - uint32_t real_real = (uint32_t)lhs->c[2 * i] * rhs->c[2 * i]; - uint32_t img_img = (uint32_t)lhs->c[2 * i + 1] * rhs->c[2 * i + 1]; - uint32_t real_img = (uint32_t)lhs->c[2 * i] * rhs->c[2 * i + 1]; - uint32_t img_real = (uint32_t)lhs->c[2 * i + 1] * rhs->c[2 * i]; - out->c[2 * i] = - reduce(real_real + (uint32_t)reduce(img_img) * kModRoots[i]); - out->c[2 * i + 1] = reduce(img_real + real_img); + const uint32_t lhs_real = lhs->c[2 * i]; + const uint32_t lhs_img = lhs->c[2 * i + 1]; + acc->c[2 * i] += lhs_real * rhs->c[2 * i] + lhs_img * rhs_cache->c[i]; + acc->c[2 * i + 1] += lhs_real * rhs->c[2 * i + 1] + lhs_img * rhs->c[2 * i]; + } +} + +// scalar_from_accumulator sets `out` to `acc` mod kPrime. +inline void scalar_from_accumulator(scalar *out, + const scalar_accumulator *acc) { + for (int i = 0; i < DEGREE; i++) { + out->c[i] = reduce(acc->c[i]); } } @@ -359,44 +450,68 @@ inline void vector_add(vector *lhs, const vector *rhs) { template inline void matrix_mult(vector *out, const matrix *m, - const vector *a) { - vector_zero(out); + const vector *a, + const vector_mulcache *a_cache) { for (int i = 0; i < RANK; i++) { + scalar_accumulator acc = {}; for (int j = 0; j < RANK; j++) { - scalar product; - scalar_mult(&product, &m->v[i][j], &a->v[j]); - scalar_add(&out->v[i], &product); + scalar_mult_accumulate(&acc, &m->v[i][j], &a->v[j], &a_cache->v[j]); } + scalar_from_accumulator(&out->v[i], &acc); } } template inline void matrix_mult_transpose(vector *out, const matrix *m, - const vector *a) { - vector_zero(out); + const vector *a, + const vector_mulcache *a_cache) { for (int i = 0; i < RANK; i++) { + scalar_accumulator acc = {}; for (int j = 0; j < RANK; j++) { - scalar product; - scalar_mult(&product, &m->v[j][i], &a->v[j]); - scalar_add(&out->v[i], &product); + scalar_mult_accumulate(&acc, &m->v[j][i], &a->v[j], &a_cache->v[j]); } + scalar_from_accumulator(&out->v[i], &acc); } } template inline void scalar_inner_product(scalar *out, const vector *lhs, - const vector *rhs) { - scalar_zero(out); + const vector *rhs, + const vector_mulcache *rhs_cache) { + scalar_accumulator acc = {}; for (int i = 0; i < RANK; i++) { - scalar product; - scalar_mult(&product, &lhs->v[i], &rhs->v[i]); - scalar_add(out, &product); + scalar_mult_accumulate(&acc, &lhs->v[i], &rhs->v[i], &rhs_cache->v[i]); } + scalar_from_accumulator(out, &acc); } inline void scalar_from_keccak_block_vartime(scalar *out, int *done, const uint8_t block[168]) { - for (size_t i = 0; i < 168 && *done < DEGREE; i += 3) { + // kStep is the number of loop iterations to do at once. May need to become + // platform dependent at some point. Must be a compile-time constant - if we + // ever need to runtime dispatch on it, it has to be made a template argument. + const size_t kStep = 2; + + // Branchless sampling as long as there is enough space in the output buffer + // even if all sampled numbers are < `kPrime`. + size_t i = 0; + for (; i <= size_t{168 - 3 * kStep} && *done <= int{DEGREE - 2 * kStep}; + i += 3 * kStep) { + uint16_t d[2 * kStep]; + for (size_t k = 0; k < kStep; ++k) { + size_t di = 2 * k; + size_t bi = i + 3 * k; + d[di] = block[bi] + 256 * (block[bi + 1] % 16); + d[di + 1] = block[bi + 1] / 16 + 16 * block[bi + 2]; + } + for (size_t k = 0; k < 2 * kStep; ++k) { + out->c[*done] = d[k]; + *done += d[k] < kPrime; + } + } + + // Branchful "tail". + for (; i < 168 && *done < DEGREE; i += 3) { uint16_t d1 = block[i] + 256 * (block[i + 1] % 16); uint16_t d2 = block[i + 1] / 16 + 16 * block[i + 2]; if (d1 < kPrime) { @@ -445,6 +560,29 @@ inline void scalar_from_keccak_vartime_x2(scalar *out[2], } #endif +#if defined(HAVE_KECCAK_X4) +inline void scalar_from_keccak_vartime_x4(scalar *out[4], + BORINGSSL_keccak_st keccak_ctx[]) { + for (int i = 0; i < 4; i++) { + assert(keccak_ctx[i].squeeze_offset == 0); + assert(keccak_ctx[i].rate_bytes == 168); + } + static_assert(168 % 3 == 0, "block and coefficient boundaries do not align"); + + int done[4] = {0, 0, 0, 0}; + while (done[0] < DEGREE || done[1] < DEGREE || done[2] < DEGREE || + done[3] < DEGREE) { + uint8_t block[4][168]; + uint8_t *blocks[] = {block[0], block[1], block[2], block[3]}; + BORINGSSL_keccak_squeeze_x4(keccak_ctx, blocks, sizeof(block[0])); + scalar_from_keccak_block_vartime(out[0], &done[0], block[0]); + scalar_from_keccak_block_vartime(out[1], &done[1], block[1]); + scalar_from_keccak_block_vartime(out[2], &done[2], block[2]); + scalar_from_keccak_block_vartime(out[3], &done[3], block[3]); + } +} +#endif + static void scalar_centered_binomial_distribution_eta_2( scalar *out, const uint8_t entropy[128]) { for (int i = 0; i < DEGREE; i += 2) { @@ -452,10 +590,10 @@ static void scalar_centered_binomial_distribution_eta_2( uint16_t value = (byte & 1) + ((byte >> 1) & 1); value -= ((byte >> 2) & 1) + ((byte >> 3) & 1); - // Add |kPrime| if |value| underflowed. See |reduce_once| for a discussion + // Add `kPrime` if `value` underflowed. See `reduce_once` for a discussion // on why the value barrier is omitted. While this could have been written // reduce_once(value + kPrime), this is one extra addition and small range - // of |value| tempts some versions of Clang to emit a branch. + // of `value` tempts some versions of Clang to emit a branch. uint16_t mask = 0u - (value >> 15); out->c[i] = ((value + kPrime) & mask) | (value & ~mask); @@ -469,7 +607,7 @@ static void scalar_centered_binomial_distribution_eta_2( } // Algorithm 8 from the spec, with eta fixed to two and the PRF call -// included. Creates binominally distributed elements by sampling 2*|eta| bits, +// included. Creates binominally distributed elements by sampling 2*`eta` bits, // and setting the coefficient to the count of the first bits minus the count of // the second bits, resulting in a centered binomial distribution. Since eta is // two this gives -2/2 with a probability of 1/16, -1/1 with probability 1/4, @@ -504,9 +642,32 @@ static void scalar_centered_binomial_distribution_eta_2_with_prf_x2( } #endif +#if defined(HAVE_KECCAK_X4) +// scalar_centered_binomial_distribution_eta_2_with_prf_x4 computes four blocks +// of scalar_centered_binomial_distribution_eta_2_with_prf. +static void scalar_centered_binomial_distribution_eta_2_with_prf_x4( + scalar out[4], uint8_t input[33]) { + uint8_t entropy[4][128]; + static_assert(sizeof(entropy[0]) == 2 * /*kEta=*/2 * DEGREE / 8); + + uint8_t inputs[4][33]; + for (int i = 0; i < 4; i++) { + OPENSSL_memcpy(inputs[i], input, 33); + inputs[i][32] += i; + } + uint8_t *entropies[] = {entropy[0], entropy[1], entropy[2], entropy[3]}; + const uint8_t *input_ptrs[4] = {inputs[0], inputs[1], inputs[2], inputs[3]}; + prf_x4(entropies, sizeof(entropy[0]), input_ptrs); + + for (int k = 0; k < 4; k++) { + scalar_centered_binomial_distribution_eta_2(&out[k], entropy[k]); + } +} +#endif + // Generates a secret vector by using -// |scalar_centered_binomial_distribution_eta_2_with_prf|, using the given seed -// appending and incrementing |counter| for entry of the vector. +// `scalar_centered_binomial_distribution_eta_2_with_prf`, using the given seed +// appending and incrementing `counter` for entry of the vector. template void vector_generate_secret_eta_2(vector *out, uint8_t *counter, const uint8_t seed[32]) { @@ -514,6 +675,16 @@ void vector_generate_secret_eta_2(vector *out, uint8_t *counter, OPENSSL_memcpy(input, seed, 32); int i = 0; +#if defined(HAVE_KECCAK_X4) + if (BORINGSSL_have_keccak_x4()) { + for (; i + 4 <= RANK; i += 4) { + input[32] = *counter; + *counter += 4; + scalar_centered_binomial_distribution_eta_2_with_prf_x4(&out->v[i], + input); + } + } +#endif #if defined(HAVE_KECCAK_X2) for (; i + 2 <= RANK; i += 2) { input[32] = *counter; @@ -540,7 +711,7 @@ void matrix_expand_step(scalar *out, uint8_t input[34], int i) { #if defined(HAVE_KECCAK_X2) template -void matrix_expand_step_x2(matrix out[2], uint8_t input[34], int i) { +void matrix_expand_step_x2(matrix *out, uint8_t input[34], int i) { input[32] = i / RANK; input[33] = i % RANK; uint8_t input1[34]; @@ -558,6 +729,26 @@ void matrix_expand_step_x2(matrix out[2], uint8_t input[34], int i) { } #endif +#if defined(HAVE_KECCAK_X4) +template +void matrix_expand_step_x4(matrix *out, uint8_t input[34], int i) { + uint8_t inputs[4][34]; + BORINGSSL_keccak_st keccak_ctx[4]; + for (int k = 0; k < 4; k++) { + OPENSSL_memcpy(inputs[k], input, 32); + inputs[k][32] = (i + k) / RANK; + inputs[k][33] = (i + k) % RANK; + BORINGSSL_keccak_init(&keccak_ctx[k], boringssl_shake128); + BORINGSSL_keccak_absorb(&keccak_ctx[k], inputs[k], 34); + } + scalar *outs[] = {&out->v[i / RANK][i % RANK], + &out->v[(i + 1) / RANK][(i + 1) % RANK], + &out->v[(i + 2) / RANK][(i + 2) % RANK], + &out->v[(i + 3) / RANK][(i + 3) % RANK]}; + scalar_from_keccak_vartime_x4(outs, keccak_ctx); +} +#endif + // Expands the matrix of a seed for key generation and for encaps-CPA. template void matrix_expand(matrix *out, const uint8_t rho[32]) { @@ -565,9 +756,16 @@ void matrix_expand(matrix *out, const uint8_t rho[32]) { OPENSSL_memcpy(input, rho, 32); int i = 0; +#if defined(HAVE_KECCAK_X4) + if (BORINGSSL_have_keccak_x4()) { + for (; i + 4 <= RANK * RANK; i += 4) { + matrix_expand_step_x4(out, input, i); + } + } +#endif #if defined(HAVE_KECCAK_X2) for (; i + 2 <= RANK * RANK; i += 2) { - matrix_expand_step_x2(out, input, i); + matrix_expand_step_x2(out, input, i); } #endif @@ -576,9 +774,9 @@ void matrix_expand(matrix *out, const uint8_t rho[32]) { } } -// Encodes a scalar of 256 |BITS|-bit words into 32*|BITS| bytes by splitting +// Encodes a scalar of 256 `BITS`-bit words into 32*`BITS` bytes by splitting // and joining into bytes using LSB-first bit order (i.e. opposite to standard -// reading order). See below for examples. If an input is >= 1 << |BITS|, the +// reading order). See below for examples. If an input is >= 1 << `BITS`, the // result is undefined. template void scalar_encode(uint8_t *out, const scalar *s); @@ -704,7 +902,7 @@ void scalar_encode<1>(uint8_t out[32], const scalar *s) { } } -// Encodes an entire vector into 32*|RANK|*|bits| bytes. Note that since 256 +// Encodes an entire vector into 32*`RANK`*`bits` bytes. Note that since 256 // (DEGREE) is divisible by 8, the individual vector entries will always fill a // whole number of bytes, so we do not need to worry about bit packing here. template @@ -714,14 +912,15 @@ void vector_encode(uint8_t *out, const vector *a) { } } -// The inverse of |scalar_encode|. Returns 1 iff the encoded scalar is valid, -// i.e. all components are < |kPrime|. Otherwise, returns 0 and the value of -// |out| is undefined. +// scalar_decode parses `DEGREE * BITS` bits from `in` into `DEGREE` values in +// `out`. This is the inverse of `scalar_encode`. This is only defined for `BITS +// < 12`. Otherwise, the decoding is fallible and must use `scalar_decode_12` +// instead. template -int scalar_decode(scalar *out, const uint8_t *in); +void scalar_decode(scalar *out, const uint8_t *in); template <> -int scalar_decode<10>(scalar *out, const uint8_t in[320]) { +void scalar_decode<10>(scalar *out, const uint8_t in[320]) { for (int i = 0; i < DEGREE; i += 4) { uint16_t s0 = (uint16_t)(in[0] | ((in[1] & 0x03) << 8)); uint16_t s1 = (uint16_t)((in[1] >> 2) | ((in[2] & 0x0f) << 6)); @@ -733,28 +932,10 @@ int scalar_decode<10>(scalar *out, const uint8_t in[320]) { out->c[i + 3] = s3; in += 5; } - return 1; } template <> -int scalar_decode<12>(scalar *out, const uint8_t in[384]) { - for (int i = 0; i < DEGREE; i += 2) { - uint16_t s0 = (uint16_t)(in[0] | ((in[1] & 0x0f) << 8)); - uint16_t s1 = (uint16_t)((in[1] >> 4) | (in[2] << 4)); - if (constant_time_declassify_int((s0 | s1) >= kPrime)) { - if (s0 >= kPrime || s1 >= kPrime) { - return 0; - } - } - out->c[i] = s0; - out->c[i + 1] = s1; - in += 3; - } - return 1; -} - -template <> -int scalar_decode<4>(scalar *out, const uint8_t in[128]) { +void scalar_decode<4>(scalar *out, const uint8_t in[128]) { for (int i = 0; i < DEGREE; i += 2) { uint16_t s0 = (uint16_t)(in[0] & 0x0f); uint16_t s1 = (uint16_t)(in[0] >> 4); @@ -763,14 +944,10 @@ int scalar_decode<4>(scalar *out, const uint8_t in[128]) { out->c[i + 1] = s1; in += 1; } - return 1; } -// scalar_decode parses |DEGREE * bits| bits from |in| into |DEGREE| values in -// |out|. It returns one on success and zero if any parsed value is >= -// |kPrime|. template <> -int scalar_decode<11>(scalar *out, const uint8_t in[352]) { +void scalar_decode<11>(scalar *out, const uint8_t in[352]) { for (int i = 0; i < DEGREE; i += 8) { uint16_t s0 = (uint16_t)(in[0] | ((in[1] & 0x07) << 8)); uint16_t s1 = (uint16_t)((in[1] >> 3) | ((in[2] & 0x3f) << 5)); @@ -792,11 +969,10 @@ int scalar_decode<11>(scalar *out, const uint8_t in[352]) { out->c[i + 7] = s7; in += 11; } - return 1; } template <> -int scalar_decode<5>(scalar *out, const uint8_t in[160]) { +void scalar_decode<5>(scalar *out, const uint8_t in[160]) { for (int i = 0; i < DEGREE; i += 8) { uint16_t s0 = (uint16_t)(in[0] & 0x1f); uint16_t s1 = (uint16_t)((in[0] >> 5) | ((in[1] & 0x03) << 3)); @@ -817,38 +993,66 @@ int scalar_decode<5>(scalar *out, const uint8_t in[160]) { out->c[i + 7] = s7; in += 5; } - return 1; } template <> -int scalar_decode<1>(scalar *out, const uint8_t in[32]) { +void scalar_decode<1>(scalar *out, const uint8_t in[32]) { for (int i = 0; i < DEGREE; i += 8) { uint8_t in_byte = in[i / 8]; for (int j = 0; j < 8; j++) { out->c[i + j] = (in_byte >> j) & 1; } } +} + +// The inverse of `scalar_encode<12>`. Returns 1 iff the encoded scalar is +// valid, i.e. all components are < `kPrime`. Otherwise, returns 0 and the value +// of `out` is undefined. +int scalar_decode_12(scalar *out, const uint8_t in[384]) { + for (int i = 0; i < DEGREE; i += 2) { + uint16_t s0 = (uint16_t)(in[0] | ((in[1] & 0x0f) << 8)); + uint16_t s1 = (uint16_t)((in[1] >> 4) | (in[2] << 4)); + // This is only used with secret input when parsing the test-only + // semi-expanded format. Leaking information about invalid inputs is OK. + if (constant_time_declassify_int(s0 >= kPrime) || + constant_time_declassify_int(s1 >= kPrime)) { + return 0; + } + out->c[i] = s0; + out->c[i + 1] = s1; + in += 3; + } return 1; } -// Decodes 32*|RANK|*|bits| bytes from |in| into |out|. It returns one on -// success or zero if any parsed value is >= |kPrime|. +// Decodes 32*`RANK`*`bits` bytes from `in` into `out`. This only supports `bits +// < 12`, necessary for decoding to be infallible. template -inline int vector_decode(vector *out, const uint8_t *in) { +inline void vector_decode(vector *out, const uint8_t *in) { + static_assert(bits < 12); + for (int i = 0; i < RANK; i++) { + scalar_decode(&out->v[i], in + i * bits * DEGREE / 8); + } +} + +// Decodes 32*`RANK`*12 bytes from `in` into `out` It returns one on success or +// zero if any parsed value is >= `kPrime`. +template +inline int vector_decode_12(vector *out, const uint8_t *in) { for (int i = 0; i < RANK; i++) { - if (!scalar_decode(&out->v[i], in + i * bits * DEGREE / 8)) { + if (!scalar_decode_12(&out->v[i], in + i * 12 * DEGREE / 8)) { return 0; } } return 1; } -// Compresses (lossily) an input |x| mod 3329 into |bits| many bits by grouping +// Compresses (lossily) an input `x` mod 3329 into `bits` many bits by grouping // numbers close to each other together. The formula used is -// round(2^|bits|/kPrime*x) mod 2^|bits|. +// round(2^`bits`/kPrime*x) mod 2^`bits`. // Uses Barrett reduction to achieve constant time. Since we need both the // remainder (for rounding) and the quotient (as the result), we cannot use -// |reduce| here, but need to do the Barrett reduction directly. +// `reduce` here, but need to do the Barrett reduction directly. inline uint16_t compress(uint16_t x, int bits) { uint32_t shifted = (uint32_t)x << bits; uint64_t product = (uint64_t)shifted * kBarrettMultiplier; @@ -865,19 +1069,19 @@ inline uint16_t compress(uint16_t x, int bits) { return quotient & ((1 << bits) - 1); } -// Decompresses |x| by using an equi-distant representative. The formula is -// round(kPrime/2^|bits|*x). Note that 2^|bits| being the divisor allows us to +// Decompresses `x` by using an equi-distant representative. The formula is +// round(kPrime/2^`bits`*x). Note that 2^`bits` being the divisor allows us to // implement this logic using only bit operations. inline uint16_t decompress(uint16_t x, int bits) { uint32_t product = (uint32_t)x * kPrime; uint32_t power = 1 << bits; - // This is |product| % power, since |power| is a power of 2. + // This is `product` % power, since `power` is a power of 2. uint32_t remainder = product & (power - 1); - // This is |product| / power, since |power| is a power of 2. + // This is `product` / power, since `power` is a power of 2. uint32_t lower = product >> bits; - // The rounding logic works since the first half of numbers mod |power| have a - // 0 as first bit, and the second half has a 1 as first bit, since |power| is - // a power of 2. As a 12 bit number, |remainder| is always positive, so we + // The rounding logic works since the first half of numbers mod `power` have a + // 0 as first bit, and the second half has a 1 as first bit, since `power` is + // a power of 2. As a 12 bit number, `remainder` is always positive, so we // will shift in 0s for a right shift. return lower + (remainder >> (bits - 1)); } @@ -933,11 +1137,13 @@ inline void decrypt_cpa(uint8_t out[32], const private_key *priv, vector_decode(&u, ciphertext); vector_decompress(&u, du); vector_ntt(&u); + vector_mulcache u_cache; + vector_mulcache_compute(&u_cache, &u); scalar v; scalar_decode(&v, ciphertext + compressed_vector_size(RANK)); scalar_decompress(&v, dv); scalar mask; - scalar_inner_product(&mask, &priv->s, &u); + scalar_inner_product(&mask, &priv->s, &u, &u_cache); scalar_inverse_ntt(&mask); scalar_sub(&v, &mask); scalar_compress(&v, 1); @@ -986,7 +1192,9 @@ void mlkem_generate_key_external_seed_no_self_test( vector error; vector_generate_secret_eta_2(&error, &counter, sigma); vector_ntt(&error); - matrix_mult_transpose(&priv->pub.t, &priv->pub.m, &priv->s); + vector_mulcache s_cache; + vector_mulcache_compute(&s_cache, &priv->s); + matrix_mult_transpose(&priv->pub.t, &priv->pub.m, &priv->s, &s_cache); vector_add(&priv->pub.t, &error); // t is part of the public key and thus is public. CONSTTIME_DECLASSIFY(&priv->pub.t, sizeof(priv->pub.t)); @@ -1012,7 +1220,7 @@ void mlkem_generate_key_external_seed(uint8_t *out_encoded_public_key, } // Encrypts a message with given randomness to -// the ciphertext in |out|. Without applying the Fujisaki-Okamoto transform this +// the ciphertext in `out`. Without applying the Fujisaki-Okamoto transform this // would not result in a CCA secure scheme, since lattice schemes are vulnerable // to decryption failure oracles. template @@ -1025,6 +1233,8 @@ void encrypt_cpa(uint8_t *out, const mlkem::public_key *pub, mlkem::vector secret; vector_generate_secret_eta_2(&secret, &counter, randomness); vector_ntt(&secret); + mlkem::vector_mulcache secret_cache; + vector_mulcache_compute(&secret_cache, &secret); mlkem::vector error; vector_generate_secret_eta_2(&error, &counter, randomness); uint8_t input[33]; @@ -1033,11 +1243,11 @@ void encrypt_cpa(uint8_t *out, const mlkem::public_key *pub, mlkem::scalar scalar_error; scalar_centered_binomial_distribution_eta_2_with_prf(&scalar_error, input); mlkem::vector u; - matrix_mult(&u, &pub->m, &secret); + matrix_mult(&u, &pub->m, &secret, &secret_cache); vector_inverse_ntt(&u); vector_add(&u, &error); mlkem::scalar v; - scalar_inner_product(&v, &pub->t, &secret); + scalar_inner_product(&v, &pub->t, &secret, &secret_cache); scalar_inverse_ntt(&v); scalar_add(&v, &scalar_error); mlkem::scalar expanded_message; @@ -1085,14 +1295,14 @@ void mlkem_decap(uint8_t out_shared_secret[MLKEM_SHARED_SECRET_BYTES], mlkem_decap_no_self_test(out_shared_secret, ciphertext, priv); } -// mlkem_parse_public_key_with_trailing_data parses |in| into |pub| but leaves -// trailing data in |in| for the caller. +// mlkem_parse_public_key_with_trailing_data parses `in` into `pub` but leaves +// trailing data in `in` for the caller. template int mlkem_parse_public_key_with_trailing_data(public_key *pub, CBS *in) { CBS orig_in = *in; CBS t_bytes; if (!CBS_get_bytes(in, &t_bytes, encoded_vector_size(RANK)) || - !vector_decode(&pub->t, CBS_data(&t_bytes)) || + !vector_decode_12(&pub->t, CBS_data(&t_bytes)) || !CBS_copy_bytes(in, pub->rho, sizeof(pub->rho))) { return 0; } @@ -1116,7 +1326,7 @@ template int mlkem_parse_private_key(private_key *priv, CBS *in) { CBS s_bytes, public_key_hash; if (!CBS_get_bytes(in, &s_bytes, encoded_vector_size(RANK)) || - !vector_decode(&priv->s, CBS_data(&s_bytes)) || + !vector_decode_12(&priv->s, CBS_data(&s_bytes)) || !mlkem_parse_public_key_with_trailing_data(&priv->pub, in) || // We compute the public key hash ourselves, but check it matched. !CBS_get_bytes(in, &public_key_hash, sizeof(priv->pub.public_key_hash)) || @@ -1527,8 +1737,8 @@ const MLKEM1024_public_key *bssl::BCM_mlkem1024_public_of_private( return mlkem::public_key_1024_to_external(&priv->pub); } -// Calls |MLKEM768_encap_external_entropy| with random bytes from -// |BCM_rand_bytes| +// Calls `MLKEM768_encap_external_entropy` with random bytes from +// `BCM_rand_bytes` bcm_infallible bssl::BCM_mlkem768_encap( uint8_t out_ciphertext[MLKEM768_CIPHERTEXT_BYTES], uint8_t out_shared_secret[MLKEM_SHARED_SECRET_BYTES], diff --git a/third_party/boringssl/src/crypto/fipsmodule/rand/android_entropy_client.cc.inc b/third_party/boringssl/src/crypto/fipsmodule/rand/android_entropy_client.cc.inc new file mode 100644 index 000000000..1611052f2 --- /dev/null +++ b/third_party/boringssl/src/crypto/fipsmodule/rand/android_entropy_client.cc.inc @@ -0,0 +1,82 @@ +// Copyright 2020 The BoringSSL Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include + +#if defined(BORINGSSL_FIPS) && defined(OPENSSL_ANDROID) + +#include +#include +#include +#include +#include + +#include "internal.h" + +BSSL_NAMESPACE_BEGIN + +int bssl_get_seed_from_daemon(uint8_t *out_entropy, size_t *inout_entropy_len) { + const int sock = socket(AF_UNIX, SOCK_STREAM, 0); + if (sock < 0) { + return 0; + } + + struct sockaddr_un sun; + memset(&sun, 0, sizeof(sun)); + sun.sun_family = AF_UNIX; + static const char kSocketPath[] = "/dev/socket/prng_seeder"; + static_assert(sizeof(kSocketPath) <= UNIX_PATH_MAX, "kSocketPath too long"); + OPENSSL_memcpy(sun.sun_path, kSocketPath, sizeof(kSocketPath)); + + int ret = 0; + size_t done = 0; + if (connect(sock, (struct sockaddr *)&sun, sizeof(sun))) { + goto out; + } + + uint8_t buffer[BSSL_ENTROPY_DAEMON_RESPONSE_LEN]; + while (done < sizeof(buffer)) { + ssize_t n; + do { + n = read(sock, buffer + done, sizeof(buffer) - done); + } while (n == -1 && errno == EINTR); + + if (n < 1) { + goto out; + } + done += n; + } + + if (done != BSSL_ENTROPY_DAEMON_RESPONSE_LEN) { + // The daemon should always write `BSSL_ENTROPY_DAEMON_RESPONSE_LEN` bytes + // on every connection. + goto out; + } + + if (*inout_entropy_len > sizeof(buffer)) { + *inout_entropy_len = sizeof(buffer); + } + OPENSSL_memcpy(out_entropy, buffer, *inout_entropy_len); + ret = 1; + +out: + if (sock >= 0) { + close(sock); + } + return ret; +} + +BSSL_NAMESPACE_END + +#endif // FIPS && ANDROID diff --git a/third_party/boringssl/src/crypto/fipsmodule/rand/ctrdrbg.cc.inc b/third_party/boringssl/src/crypto/fipsmodule/rand/ctrdrbg.cc.inc index 04f519599..8aa034c6c 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/rand/ctrdrbg.cc.inc +++ b/third_party/boringssl/src/crypto/fipsmodule/rand/ctrdrbg.cc.inc @@ -241,7 +241,7 @@ int bssl::CTR_DRBG_init(CTR_DRBG_STATE *drbg, int df, const uint8_t *entropy, static_assert(CTR_DRBG_SEED_LEN % AES_BLOCK_SIZE == 0, "not a multiple of AES block size"); -// ctr_inc adds |n| to the last four bytes of |drbg->counter|, treated as a +// ctr_inc adds `n` to the last four bytes of `drbg->counter`, treated as a // big-endian number. static void ctr32_add(CTR_DRBG_STATE *drbg, uint32_t n) { uint32_t ctr = CRYPTO_load_u32_be(drbg->counter + 12); diff --git a/third_party/boringssl/src/crypto/fipsmodule/rand/internal.h b/third_party/boringssl/src/crypto/fipsmodule/rand/internal.h index 0913f008b..8895b961d 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/rand/internal.h +++ b/third_party/boringssl/src/crypto/fipsmodule/rand/internal.h @@ -25,7 +25,7 @@ BSSL_NAMESPACE_BEGIN // rand_fork_unsafe_buffering_enabled returns whether fork-unsafe buffering has -// been enabled via |RAND_enable_fork_unsafe_buffering|. +// been enabled via `RAND_enable_fork_unsafe_buffering`. int rand_fork_unsafe_buffering_enabled(); BSSL_NAMESPACE_END @@ -43,19 +43,29 @@ struct ctr_drbg_state_st { BSSL_NAMESPACE_BEGIN -// CTR_DRBG_init initialises |*drbg| given |entropy_len| bytes of entropy in -// |entropy| and, optionally, a personalization string up to -// |CTR_DRBG_SEED_LEN| bytes in length. It returns one on success and zero on +// CTR_DRBG_init initialises `*drbg` given `entropy_len` bytes of entropy in +// `entropy` and, optionally, a personalization string up to +// `CTR_DRBG_SEED_LEN` bytes in length. It returns one on success and zero on // error. // -// If `df` is false then `entropy_len` must be |CTR_DRBG_ENTROPY_LEN| and -// |nonce| must be nullptr. +// If `df` is false then `entropy_len` must be `CTR_DRBG_ENTROPY_LEN` and +// `nonce` must be nullptr. OPENSSL_EXPORT int CTR_DRBG_init(CTR_DRBG_STATE *drbg, int df, const uint8_t *entropy, size_t entropy_len, const uint8_t nonce[CTR_DRBG_NONCE_LEN], const uint8_t *personalization, size_t personalization_len); +// BSSL_ENTROPY_DAEMON_RESPONSE_LEN is the number of bytes that the Android +// entropy daemon replies with. +#define BSSL_ENTROPY_DAEMON_RESPONSE_LEN (48 * 10 + 16) + +// bssl_get_seed_from_daemon fetches up to `*inout_entropy_len` bytes of +// entropy from the Android entropy daemon, writing them to `*out_entropy`. +// On successful exit, `*inout_entropy_len` contains the number of bytes +// written. Returns one on success and zero on error. +int bssl_get_seed_from_daemon(uint8_t *out_entropy, size_t *inout_entropy_len); + #if defined(OPENSSL_X86_64) && !defined(OPENSSL_NO_ASM) inline int have_rdrand() { return CRYPTO_is_RDRAND_capable(); } @@ -68,11 +78,11 @@ inline int have_fast_rdrand() { } // CRYPTO_rdrand writes eight bytes of random data from the hardware RNG to -// |out|. It returns one on success or zero on hardware failure. +// `out`. It returns one on success or zero on hardware failure. extern "C" int CRYPTO_rdrand(uint8_t out[8]); -// CRYPTO_rdrand_multiple8_buf fills |len| bytes at |buf| with random data from -// the hardware RNG. The |len| argument must be a multiple of eight. It returns +// CRYPTO_rdrand_multiple8_buf fills `len` bytes at `buf` with random data from +// the hardware RNG. The `len` argument must be a multiple of eight. It returns // one on success and zero on hardware failure. extern "C" int CRYPTO_rdrand_multiple8_buf(uint8_t *buf, size_t len); diff --git a/third_party/boringssl/src/crypto/fipsmodule/rand/rand.cc.inc b/third_party/boringssl/src/crypto/fipsmodule/rand/rand.cc.inc index 9b2944169..839a03fcb 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/rand/rand.cc.inc +++ b/third_party/boringssl/src/crypto/fipsmodule/rand/rand.cc.inc @@ -24,6 +24,7 @@ #include #include #include +#include #include "../../bcm_support.h" #include "../../mem_internal.h" @@ -31,66 +32,186 @@ #include "../delocate.h" #include "internal.h" +#if defined(BORINGSSL_FIPS) +#include "../entropy/internal.h" +#endif + using namespace bssl; -// It's assumed that the operating system always has an unfailing source of -// entropy which is accessed via |CRYPTO_sysrand|. (If the operating system -// entropy source fails, it's up to |CRYPTO_sysrand| to abort the process—we -// don't try to handle it.) +// # Entropy design +// +// Each thread gets its own, thread-local DRBG. These are `rand_thread_state` +// objects. In non-FIPS mode these are seeded from the operating system. It's +// assumed that the operating system always has an unfailing source of entropy +// which is accessed via `CRYPTO_sysrand`. (If the operating system entropy +// source fails, it's up to `CRYPTO_sysrand` to abort the process—we don't try +// to handle it.) +// +// If running in FIPS mode, a compliant entropy source must be used to seed the +// thread-local DRBGs instead. That is either the BoringCrypto jitter source or, +// on Android, a source provided by the OS which is accessed via a UNIX domain +// socket. When seeding from these sources, OS entropy is still used via the +// "additional data" input to the DRBG. But, from a compliance perspective, +// this input is irrelevent. +// +// Since the jitter source takes some time to run, there is a single jitter +// source per address space. It's used to seed a DRBG (`core_drbg`) which, in +// turn, seeds the thread-local DRBGs. Android also uses this core DRBG design +// in order to keep things more uniform across platforms. // -// In addition, the hardware may provide a low-latency RNG. Intel's rdrand -// instruction is the canonical example of this. When a hardware RNG is -// available we don't need to worry about an RNG failure arising from fork()ing -// the process or moving a VM, so we can keep thread-local RNG state and use it -// as an additional-data input to CTR-DRBG. +// The core DRBG will be reseeded in accordance with NIST's reseeding +// requirements. However, since our DRBG configuration allows for 2**48 +// generate calls before reseeding, it's unlikely that any process would +// observe a reseed of that DRBG. +// +// The thread-local DRBGs mean that we have to worry about the process fork()ing +// or the underlying VM getting cloned. Thus we try to mix entropy into each +// generation from the thread-local DRBGs. The hardware may provide a +// low-latency RNG. (Intel's rdrand instruction is the canonical example of +// this.) If available, that'll be used for this additional data. Otherwise +// we'll draw from the OS if it doesn't provide other fork-detection APIs. // // (We assume that the OS entropy is safe from fork()ing and VM duplication. // This might be a bit of a leap of faith, esp on Windows, but there's nothing // that we can do about it.) -// kReseedInterval is the number of generate calls made to CTR-DRBG before -// reseeding. -static const unsigned kReseedInterval = 4096; +#if defined(BORINGSSL_FIPS) +// This #if-block needs to be outside of the anonymous namespace: + +struct core_drbg { + CTR_DRBG_STATE drbg; + // calls is the number of generate calls made on `drbg` since it was last + // (re)seeded. This is bound by `kCoreReseedInterval`. + uint64_t calls = 0; +}; -// CRNGT_BLOCK_SIZE is the number of bytes in a “block” for the purposes of the -// continuous random number generator test in FIPS 140-2, section 4.9.2. -#define CRNGT_BLOCK_SIZE 16 +DEFINE_BSS_GET(struct core_drbg *, g_core_drbg, = nullptr) +DEFINE_STATIC_MUTEX(g_core_drbg_lock) +#endif // FIPS namespace { + +#if defined(BORINGSSL_FIPS) + +// The number of times we'll generate from the core DRBG before reseeding. +// This is within the permitted bounds but so high that this effectively means +// that we'll never reseed it. See SP 800-90Ar1 table 3. +constexpr uint64_t kCoreReseedInterval = (UINT64_C(1) << 48) - 1; +constexpr size_t kCoreDRBGEntropySize = 48; + +#if defined(OPENSSL_ANDROID) +// Android seeds from a system daemon which is responsible for getting +// entropy from a validated source. +void core_drbg_get_entropy(uint8_t entropy[kCoreDRBGEntropySize]) { + uint8_t daemon_buffer[BSSL_ENTROPY_DAEMON_RESPONSE_LEN]; + size_t daemon_buffer_len = sizeof(daemon_buffer); + if (!bssl_get_seed_from_daemon(daemon_buffer, &daemon_buffer_len)) { + // If the daemon isn't running then fallback to system entropy. + CRYPTO_sysrand(entropy, kCoreDRBGEntropySize); + return; + } + SHA384(daemon_buffer, daemon_buffer_len, entropy); +} +#else +// Non-Android platforms use the jitter source. +void core_drbg_get_entropy(uint8_t entropy[kCoreDRBGEntropySize]) { + // This runs the jitter source and takes some milliseconds. + for (unsigned i = 0; i < 100; i++) { + // The internal health checks of the jitter source may cause spurious + // failures, therefore we retry a number of times because failure + // to get entropy is fatal. + if (entropy::GetSeed(entropy)) { + return; + } + } + fprintf(stderr, "Persistent jitter source failure.\n"); + BORINGSSL_FIPS_abort(); +} +#endif + +struct core_drbg *core_drbg_get_locked() { + struct core_drbg **core = g_core_drbg_bss_get(); + if (*core != nullptr) { + return *core; + } + + uint8_t entropy[kCoreDRBGEntropySize]; + core_drbg_get_entropy(entropy); + + *core = New(); + if (!*core) { + BORINGSSL_FIPS_abort(); + } + + uint8_t nonce[CTR_DRBG_NONCE_LEN] = {0}; + static constexpr char kPersonalization[] = "BoringSSL"; + if (!CTR_DRBG_init(&(*core)->drbg, /*df=*/1, entropy, sizeof(entropy), nonce, + reinterpret_cast(kPersonalization), + sizeof(kPersonalization))) { + BORINGSSL_FIPS_abort(); + } + + return *core; +} + +// Draw a seed for a thread-local DRBG from the core DRBG. +void core_drbg_draw_seed(uint8_t *out_seed, size_t len) { + MutexWriteLock lock(g_core_drbg_lock_bss_get()); + + struct core_drbg *const core = core_drbg_get_locked(); + if (core->calls > kCoreReseedInterval) { + uint8_t reseed_entropy[kCoreDRBGEntropySize]; + core_drbg_get_entropy(reseed_entropy); + if (!CTR_DRBG_reseed(&core->drbg, reseed_entropy, + /*additional_data=*/nullptr, 0)) { + BORINGSSL_FIPS_abort(); + } + core->calls = 0; + } + + if (!CTR_DRBG_generate(&core->drbg, out_seed, len, + /*additional_data=*/nullptr, 0)) { + BORINGSSL_FIPS_abort(); + } + core->calls++; +} + +#endif // BORINGSSL_FIPS + +// kReseedInterval is the number of generate calls made to a thread-local +// CTR-DRBG before reseeding. +static const unsigned kReseedInterval = 4096; + // rand_thread_state contains the per-thread state for the RNG. struct rand_thread_state { CTR_DRBG_STATE drbg; uint64_t fork_generation; - // calls is the number of generate calls made on |drbg| since it was last - // (re)seeded. This is bound by |kReseedInterval|. + // calls is the number of generate calls made on `drbg` since it was last + // (re)seeded. This is bound by `kReseedInterval`. unsigned calls; - // last_block_valid is non-zero iff |last_block| contains data from - // |get_seed_entropy|. - int last_block_valid; - // fork_unsafe_buffering is non-zero iff, when |drbg| was last (re)seeded, + // fork_unsafe_buffering is non-zero iff, when `drbg` was last (re)seeded, // fork-unsafe buffering was enabled. int fork_unsafe_buffering; #if defined(BORINGSSL_FIPS) - // last_block contains the previous block from |get_seed_entropy|. - uint8_t last_block[CRNGT_BLOCK_SIZE]; // next and prev form a nullptr-terminated, double-linked list of all states // in a process. struct rand_thread_state *next, *prev; - // clear_drbg_lock synchronizes between uses of |drbg| and - // |rand_thread_state_clear_all| clearing it. This lock should be uncontended + // clear_drbg_lock synchronizes between uses of `drbg` and + // `rand_thread_state_clear_all` clearing it. This lock should be uncontended // in the common case, except on shutdown. Mutex clear_drbg_lock; #endif }; + } // namespace #if defined(BORINGSSL_FIPS) -// thread_states_list is the head of a linked-list of all |rand_thread_state| +// thread_states_list is the head of a linked-list of all `rand_thread_state` // objects in the process, one per thread. This is needed because FIPS requires -// that they be zeroed on process exit, but thread-local destructors aren't -// called when the whole process is exiting. +// the ability to zero them on demand (AS09.28). BoringSSL triggers this with a +// destructor function. DEFINE_BSS_GET(struct rand_thread_state *, thread_states_list, = nullptr) DEFINE_STATIC_MUTEX(thread_states_list_lock) @@ -102,14 +223,21 @@ static void rand_thread_state_clear_all() { cur->clear_drbg_lock.LockWrite(); CTR_DRBG_clear(&cur->drbg); } + + g_core_drbg_lock_bss_get()->LockWrite(); + struct core_drbg **core = g_core_drbg_bss_get(); + if (*core) { + CTR_DRBG_clear(&(*core)->drbg); + } + // The locks are deliberately left locked so that any threads that are still - // running will hang if they try to call |BCM_rand_bytes|. It also ensures - // |rand_thread_state_free| cannot free any thread state while we've taken the + // running will hang if they try to call `BCM_rand_bytes`. It also ensures + // `rand_thread_state_free` cannot free any thread state while we've taken the // lock. } #endif -// rand_thread_state_free frees a |rand_thread_state|. This is called when a +// rand_thread_state_free frees a `rand_thread_state`. This is called when a // thread exits. static void rand_thread_state_free(void *state_in) { struct rand_thread_state *state = @@ -125,8 +253,8 @@ static void rand_thread_state_free(void *state_in) { if (state->prev != nullptr) { state->prev->next = state->next; } else if (*thread_states_list_bss_get() == state) { - // |state->prev| may be nullptr either if it is the head of the list, - // or if |state| is freed before it was added to the list at all. + // `state->prev` may be nullptr either if it is the head of the list, + // or if `state` is freed before it was added to the list at all. // Compare against the head of the list to distinguish these cases. *thread_states_list_bss_get() = state->next; } @@ -145,7 +273,7 @@ static void rand_thread_state_free(void *state_in) { #if defined(OPENSSL_X86_64) && !defined(OPENSSL_NO_ASM) && \ !defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION) -// rdrand should only be called if either |have_rdrand| or |have_fast_rdrand| +// rdrand should only be called if either `have_rdrand` or `have_fast_rdrand` // returned true. static int rdrand(uint8_t *buf, const size_t len) { const size_t len_multiple8 = len & ~7; @@ -185,146 +313,26 @@ bcm_status bssl::BCM_rand_bytes_hwrng(uint8_t *buf, const size_t len) { #if defined(BORINGSSL_FIPS) -// In passive entropy mode, entropy is supplied from outside of the module via -// |BCM_rand_load_entropy| and is stored in global instance of the following -// structure. - -struct entropy_buffer { - // bytes contains entropy suitable for seeding a DRBG. - uint8_t bytes[CRNGT_BLOCK_SIZE + CTR_DRBG_SEED_LEN * BORINGSSL_FIPS_OVERREAD]; - // bytes_valid indicates the number of bytes of |bytes| that contain valid - // data. - size_t bytes_valid; - // want_additional_input is true if any of the contents of |bytes| were - // obtained via a method other than from the kernel. In these cases entropy - // from the kernel is also provided via an additional input to the DRBG. - int want_additional_input; -}; - -DEFINE_BSS_GET(struct entropy_buffer, entropy_buffer, = {}) -DEFINE_STATIC_MUTEX(entropy_buffer_lock) - -bcm_infallible bssl::BCM_rand_load_entropy(const uint8_t *entropy, - size_t entropy_len, - int want_additional_input) { - struct entropy_buffer *const buffer = entropy_buffer_bss_get(); - - MutexWriteLock lock(entropy_buffer_lock_bss_get()); - const size_t space = sizeof(buffer->bytes) - buffer->bytes_valid; - if (entropy_len > space) { - entropy_len = space; - } - - OPENSSL_memcpy(&buffer->bytes[buffer->bytes_valid], entropy, entropy_len); - buffer->bytes_valid += entropy_len; - buffer->want_additional_input |= want_additional_input && (entropy_len != 0); - return bcm_infallible::not_approved; -} - -// get_seed_entropy fills |out_entropy_len| bytes of |out_entropy| from the -// global |entropy_buffer|. -static void get_seed_entropy(uint8_t *out_entropy, size_t out_entropy_len, - int *out_want_additional_input) { - struct entropy_buffer *const buffer = entropy_buffer_bss_get(); - if (out_entropy_len > sizeof(buffer->bytes)) { - abort(); - } - - MutexWriteLock lock(entropy_buffer_lock_bss_get()); - while (buffer->bytes_valid < out_entropy_len) { - MutexWriteUnlock unlock(entropy_buffer_lock_bss_get()); - RAND_need_entropy(out_entropy_len - buffer->bytes_valid); - } - - *out_want_additional_input = buffer->want_additional_input; - OPENSSL_memcpy(out_entropy, buffer->bytes, out_entropy_len); - OPENSSL_memmove(buffer->bytes, &buffer->bytes[out_entropy_len], - buffer->bytes_valid - out_entropy_len); - buffer->bytes_valid -= out_entropy_len; - if (buffer->bytes_valid == 0) { - buffer->want_additional_input = 0; - } -} - -// rand_get_seed fills |seed| with entropy. In some cases, it will additionally -// fill |additional_input| with entropy to supplement |seed|. It sets -// |*out_additional_input_len| to the number of extra bytes. +// rand_get_seed fills `seed` with entropy. Since, in FIPS mode, this entropy +// comes from the jitter source / system daemon, `additional_input` will also be +// filled with system entropy. static void rand_get_seed(struct rand_thread_state *state, - uint8_t seed[CTR_DRBG_SEED_LEN], + uint8_t seed[CTR_DRBG_ENTROPY_LEN], uint8_t additional_input[CTR_DRBG_SEED_LEN], size_t *out_additional_input_len) { - uint8_t entropy_bytes[sizeof(state->last_block) + - CTR_DRBG_SEED_LEN * BORINGSSL_FIPS_OVERREAD]; - uint8_t *entropy = entropy_bytes; - size_t entropy_len = sizeof(entropy_bytes); - - if (state->last_block_valid) { - // No need to fill |state->last_block| with entropy from the read. - entropy += sizeof(state->last_block); - entropy_len -= sizeof(state->last_block); - } - - int want_additional_input; - get_seed_entropy(entropy, entropy_len, &want_additional_input); - - if (!state->last_block_valid) { - OPENSSL_memcpy(state->last_block, entropy, sizeof(state->last_block)); - entropy += sizeof(state->last_block); - entropy_len -= sizeof(state->last_block); - } - - // See FIPS 140-2, section 4.9.2. This is the “continuous random number - // generator test” which causes the program to randomly abort. Hopefully the - // rate of failure is small enough not to be a problem in practice. - if (CRYPTO_memcmp(state->last_block, entropy, sizeof(state->last_block)) == - 0) { - fprintf(CRYPTO_get_stderr(), "CRNGT failed.\n"); - BORINGSSL_FIPS_abort(); - } - - assert(entropy_len % CRNGT_BLOCK_SIZE == 0); - for (size_t i = CRNGT_BLOCK_SIZE; i < entropy_len; i += CRNGT_BLOCK_SIZE) { - if (CRYPTO_memcmp(entropy + i - CRNGT_BLOCK_SIZE, entropy + i, - CRNGT_BLOCK_SIZE) == 0) { - fprintf(CRYPTO_get_stderr(), "CRNGT failed.\n"); - BORINGSSL_FIPS_abort(); - } - } - OPENSSL_memcpy(state->last_block, entropy + entropy_len - CRNGT_BLOCK_SIZE, - CRNGT_BLOCK_SIZE); - - assert(entropy_len == BORINGSSL_FIPS_OVERREAD * CTR_DRBG_SEED_LEN); - OPENSSL_memcpy(seed, entropy, CTR_DRBG_SEED_LEN); - - for (size_t i = 1; i < BORINGSSL_FIPS_OVERREAD; i++) { - for (size_t j = 0; j < CTR_DRBG_SEED_LEN; j++) { - seed[j] ^= entropy[CTR_DRBG_SEED_LEN * i + j]; - } - } - - // If we used something other than system entropy then also read from the - // system. This avoids solely relying on the hardware. - // TODO(crbug.com/446280903): Once this change sticks, switch - // |get_seed_entropy| to draw from the OS instead of RDRAND. - *out_additional_input_len = 0; - if (want_additional_input) { - CRYPTO_sysrand(additional_input, CTR_DRBG_SEED_LEN); - *out_additional_input_len = CTR_DRBG_SEED_LEN; - } + core_drbg_draw_seed(seed, CTR_DRBG_ENTROPY_LEN); + CRYPTO_sysrand(additional_input, CTR_DRBG_SEED_LEN); + *out_additional_input_len = CTR_DRBG_SEED_LEN; } #else -// rand_get_seed fills |seed| with entropy. In some cases, it will additionally -// fill |additional_input| with entropy to supplement |seed|. It sets -// |*out_additional_input_len| to the number of extra bytes. +// rand_get_seed fills `seed` with system entropy in a non-FIPS build. static void rand_get_seed(struct rand_thread_state *state, - uint8_t seed[CTR_DRBG_SEED_LEN], + uint8_t seed[CTR_DRBG_ENTROPY_LEN], uint8_t additional_input[CTR_DRBG_SEED_LEN], size_t *out_additional_input_len) { - // If not in FIPS mode, we don't overread from the system entropy source and - // we don't depend only on the hardware RDRAND. - CRYPTO_sysrand(seed, CTR_DRBG_SEED_LEN); + CRYPTO_sysrand(seed, CTR_DRBG_ENTROPY_LEN); *out_additional_input_len = 0; } @@ -349,7 +357,7 @@ bcm_infallible bssl::BCM_rand_bytes_with_additional_data( if (!have_fast_rdrand() || !rdrand(additional_data, sizeof(additional_data))) { // Without a hardware RNG to save us from address-space duplication, the OS - // entropy is used. This can be expensive (one read per |RAND_bytes| call) + // entropy is used. This can be expensive (one read per `RAND_bytes` call) // and so is disabled when we have fork detection, or if the application has // promised not to fork. if (fork_generation != 0 || fork_unsafe_buffering) { @@ -373,17 +381,19 @@ bcm_infallible bssl::BCM_rand_bytes_with_additional_data( !CRYPTO_set_thread_local(OPENSSL_THREAD_LOCAL_RAND, state, rand_thread_state_free)) { // If the system is out of memory, use an ephemeral state on the - // stack. + // stack. `rand_thread_state_free` has already been called by + // `CRYPTO_set_thread_local` on failure. state = &stack_state; } - state->last_block_valid = 0; - uint8_t seed[CTR_DRBG_SEED_LEN]; + uint8_t seed[CTR_DRBG_ENTROPY_LEN]; uint8_t personalization[CTR_DRBG_SEED_LEN] = {0}; size_t personalization_len = 0; rand_get_seed(state, seed, personalization, &personalization_len); - if (!CTR_DRBG_init(&state->drbg, /*df=*/true, seed, 32u, seed + 32, + if (!CTR_DRBG_init(&state->drbg, /*df=*/true, seed, + sizeof(seed) - CTR_DRBG_NONCE_LEN, + seed + sizeof(seed) - CTR_DRBG_NONCE_LEN, personalization, personalization_len)) { abort(); } @@ -406,23 +416,23 @@ bcm_infallible bssl::BCM_rand_bytes_with_additional_data( } if (state->calls >= kReseedInterval || - // If we've forked since |state| was last seeded, reseed. + // If we've forked since `state` was last seeded, reseed. state->fork_generation != fork_generation || - // If |state| was seeded from a state with different fork-safety - // preferences, reseed. Suppose |state| was fork-safe, then forked into + // If `state` was seeded from a state with different fork-safety + // preferences, reseed. Suppose `state` was fork-safe, then forked into // two children, but each of the children never fork and disable fork // safety. The children must reseed to avoid working from the same PRNG // state. state->fork_unsafe_buffering != fork_unsafe_buffering) { - uint8_t seed[CTR_DRBG_SEED_LEN]; + uint8_t seed[CTR_DRBG_ENTROPY_LEN]; uint8_t reseed_additional_data[CTR_DRBG_SEED_LEN] = {0}; size_t reseed_additional_data_len = 0; rand_get_seed(state, seed, reseed_additional_data, &reseed_additional_data_len); #if defined(BORINGSSL_FIPS) - // Take a read lock around accesses to |state->drbg|. This is needed to + // Take a read lock around accesses to `state->drbg`. This is needed to // avoid returning bad entropy if we race with - // |rand_thread_state_clear_all|. + // `rand_thread_state_clear_all`. state->clear_drbg_lock.LockRead(); #endif if (!CTR_DRBG_reseed_ex(&state->drbg, seed, sizeof(seed), @@ -454,7 +464,7 @@ bcm_infallible bssl::BCM_rand_bytes_with_additional_data( out += todo; out_len -= todo; // Though we only check before entering the loop, this cannot add enough to - // overflow a |size_t|. + // overflow a `size_t`. state->calls++; first_call = 0; } @@ -474,9 +484,3 @@ bcm_infallible bssl::BCM_rand_bytes(uint8_t *out, size_t out_len) { BCM_rand_bytes_with_additional_data(out, out_len, kZeroAdditionalData); return bcm_infallible::approved; } - -int RAND_maybe_reseed() { - // Currently does nothing since we don't use jitter entropy yet and so the - // reseeding is quick. - return 0; -} diff --git a/third_party/boringssl/src/crypto/fipsmodule/rsa/internal.h b/third_party/boringssl/src/crypto/fipsmodule/rsa/internal.h index 3a0daf563..ebb70c322 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/rsa/internal.h +++ b/third_party/boringssl/src/crypto/fipsmodule/rsa/internal.h @@ -31,7 +31,7 @@ BSSL_NAMESPACE_BEGIN // TODO(crbug.com/42290480): Raise this limit. 512-bit RSA was factored in 1999. #define OPENSSL_RSA_MIN_MODULUS_BITS 512 -// TODO(davidben): This is inside BCM because |RSA| is inside BCM, but BCM never +// TODO(davidben): This is inside BCM because `RSA` is inside BCM, but BCM never // uses this. Split the RSA type in two. enum rsa_pss_params_t { // No parameters. @@ -67,19 +67,19 @@ class RSAImpl : public rsa_st, public RefCounted { Mutex lock; // Used to cache montgomery values. The creation of these values is protected - // by |lock|. + // by `lock`. UniquePtr mont_n; UniquePtr mont_p; UniquePtr mont_q; - // The following fields are copies of |d|, |dmp1|, and |dmq1|, respectively, + // The following fields are copies of `d`, `dmp1`, and `dmq1`, respectively, // but with the correct widths to prevent side channels. These must use // separate copies due to threading concerns caused by OpenSSL's API // mistakes. See https://github.com/openssl/openssl/issues/5158 and - // the |freeze_private_key| implementation. + // the `freeze_private_key` implementation. UniquePtr d_fixed, dmp1_fixed, dmq1_fixed; - // iqmp_mont is q^-1 mod p in Montgomery form, using |mont_p|. + // iqmp_mont is q^-1 mod p in Montgomery form, using `mont_p`. UniquePtr iqmp_mont; // pss_params is the RSA-PSS parameters associated with the key. This is not @@ -118,26 +118,26 @@ int RSA_padding_check_PKCS1_type_1(uint8_t *out, size_t *out_len, int RSA_padding_add_none(uint8_t *to, size_t to_len, const uint8_t *from, size_t from_len); -// rsa_check_public_key checks that |rsa|'s public modulus and exponent are +// rsa_check_public_key checks that `rsa`'s public modulus and exponent are // within DoS bounds. int rsa_check_public_key(const RSA *rsa); // rsa_private_transform_no_self_test calls either the method-specific -// |private_transform| function (if given) or the generic one. See the comment -// for |private_transform| in |rsa_meth_st|. +// `private_transform` function (if given) or the generic one. See the comment +// for `private_transform` in `rsa_meth_st`. int rsa_private_transform_no_self_test(RSA *rsa, uint8_t *out, const uint8_t *in, size_t len); -// rsa_private_transform acts the same as |rsa_private_transform_no_self_test| +// rsa_private_transform acts the same as `rsa_private_transform_no_self_test` // but, in FIPS mode, performs an RSA self test before calling the default RSA // implementation. int rsa_private_transform(RSA *rsa, uint8_t *out, const uint8_t *in, size_t len); -// rsa_invalidate_key is called after |rsa| has been mutated, to invalidate +// rsa_invalidate_key is called after `rsa` has been mutated, to invalidate // fields derived from the original structure. This function assumes exclusive -// access to |rsa|. In particular, no other thread may be concurrently signing, -// etc., with |rsa|. +// access to `rsa`. In particular, no other thread may be concurrently signing, +// etc., with `rsa`. void rsa_invalidate_key(RSA *rsa); diff --git a/third_party/boringssl/src/crypto/fipsmodule/rsa/padding.cc.inc b/third_party/boringssl/src/crypto/fipsmodule/rsa/padding.cc.inc index 44c8ce453..5d8824b8f 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/rsa/padding.cc.inc +++ b/third_party/boringssl/src/crypto/fipsmodule/rsa/padding.cc.inc @@ -184,7 +184,7 @@ int RSA_verify_PKCS1_PSS_mgf1(const RSA *rsa, const uint8_t *mHash, if (sLen == RSA_PSS_SALTLEN_DIGEST) { sLen = (int)hLen; } else if (sLen == RSA_PSS_SALTLEN_AUTO) { - // Leave |sLen| negative, which will trigger the logic below to recover and + // Leave `sLen` negative, which will trigger the logic below to recover and // allow any salt length. } else if (sLen < 0) { // Other negative values are reserved. @@ -202,7 +202,7 @@ int RSA_verify_PKCS1_PSS_mgf1(const RSA *rsa, const uint8_t *mHash, EM++; emLen--; } - // |sLen| may be negative for the non-standard salt length recovery mode. + // `sLen` may be negative for the non-standard salt length recovery mode. if (emLen < hLen + 2 || (sLen >= 0 && emLen < hLen + (size_t)sLen + 2)) { OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_TOO_LARGE); goto err; diff --git a/third_party/boringssl/src/crypto/fipsmodule/rsa/rsa.cc.inc b/third_party/boringssl/src/crypto/fipsmodule/rsa/rsa.cc.inc index 65109588e..cf406b27d 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/rsa/rsa.cc.inc +++ b/third_party/boringssl/src/crypto/fipsmodule/rsa/rsa.cc.inc @@ -31,7 +31,6 @@ #include "../../internal.h" #include "../../mem_internal.h" -#include "../bcm_interface.h" #include "../bn/internal.h" #include "../delocate.h" #include "internal.h" @@ -222,6 +221,13 @@ int RSA_up_ref(RSA *rsa) { return 1; } +RSA *RSA_dup_ref(const RSA *rsa) { + if (rsa == nullptr) { + return nullptr; + } + return FromOpaque(rsa)->DupRefInternal(); +} + unsigned RSA_bits(const RSA *rsa) { return BN_num_bits(FromOpaque(rsa)->n.get()); } @@ -407,7 +413,7 @@ struct pkcs1_sig_prefix { int nid; // hash_len is the expected length of the hash function. uint8_t hash_len; - // len is the number of bytes of |bytes| which are valid. + // len is the number of bytes of `bytes` which are valid. uint8_t len; // bytes contains the DER bytes. uint8_t bytes[19]; @@ -551,7 +557,7 @@ int bssl::rsa_sign_no_self_test(int hash_nid, const uint8_t *digest, if (!rsa_check_digest_size(hash_nid, digest_len)) { return 0; } - // All supported digest lengths fit in |unsigned|. + // All supported digest lengths fit in `unsigned`. assert(digest_len <= EVP_MAX_MD_SIZE); static_assert(EVP_MAX_MD_SIZE <= UINT_MAX, "digest too long"); return impl->meth->sign(hash_nid, digest, (unsigned)digest_len, out, @@ -715,8 +721,8 @@ static int check_mod_inverse(int *out_ok, const BIGNUM *a, const BIGNUM *ainv, return 1; } - // Note |bn_mul_consttime| and |bn_div_consttime| do not scale linearly, but - // checking |ainv| is in range bounds the running time, assuming |m|'s bounds + // Note `bn_mul_consttime` and `bn_div_consttime` do not scale linearly, but + // checking `ainv` is in range bounds the running time, assuming `m`'s bounds // were checked by the caller. BN_CTXScope scope(ctx); BIGNUM *tmp = BN_CTX_get(ctx); @@ -731,8 +737,8 @@ static int check_mod_inverse(int *out_ok, const BIGNUM *a, const BIGNUM *ainv, int RSA_check_key(const RSA *key) { // TODO(davidben): RSA key initialization is spread across - // |rsa_check_public_key|, |RSA_check_key|, |freeze_private_key|, and - // |BN_MONT_CTX_set_locked| as a result of API issues. See + // `rsa_check_public_key`, `RSA_check_key`, `freeze_private_key`, and + // `BN_MONT_CTX_set_locked` as a result of API issues. See // https://crbug.com/boringssl/316. As a result, we inconsistently check RSA // invariants. We should fix this and integrate that logic. @@ -746,7 +752,7 @@ int RSA_check_key(const RSA *key) { return 0; } - // |impl->d| must be bounded by |impl->n|. This ensures bounds on |RSA_bits| + // `impl->d` must be bounded by `impl->n`. This ensures bounds on `RSA_bits` // translate to bounds on the running time of private key operations. if (impl->d != nullptr && (BN_is_negative(impl->d.get()) || BN_cmp(impl->d.get(), impl->n.get()) >= 0)) { @@ -777,8 +783,8 @@ int RSA_check_key(const RSA *key) { BN_init(&dmq1); // Check that p * q == n. Before we multiply, we check that p and q are in - // bounds, to avoid a DoS vector in |bn_mul_consttime| below. Note that - // n was bound by |rsa_check_public_key|. This also implicitly checks p and q + // bounds, to avoid a DoS vector in `bn_mul_consttime` below. Note that + // n was bound by `rsa_check_public_key`. This also implicitly checks p and q // are odd, which is a necessary condition for Montgomery reduction. if (BN_is_negative(impl->p.get()) || constant_time_declassify_int(BN_cmp(impl->p.get(), impl->n.get()) >= 0) || @@ -833,7 +839,7 @@ int RSA_check_key(const RSA *key) { pm1_bits, ctx) || !check_mod_inverse(&dmq1_ok, impl->e.get(), impl->dmq1.get(), &qm1, qm1_bits, ctx) || - // |p| is odd, so |pm1| and |p| have the same bit width. If they didn't, + // `p` is odd, so `pm1` and `p` have the same bit width. If they didn't, // we only need a lower bound anyway. !check_mod_inverse(&iqmp_ok, impl->q.get(), impl->iqmp.get(), impl->p.get(), pm1_bits, ctx)) { @@ -906,12 +912,12 @@ int RSA_check_fips(RSA *key) { // Perform partial public key validation of RSA keys (SP 800-89 5.3.3). // Although this is not for primality testing, SP 800-89 cites an RSA - // primality testing algorithm, so we use |BN_prime_checks_for_generation| to + // primality testing algorithm, so we use `BN_prime_checks_for_generation` to // match. This is only a plausibility test and we expect the value to be // composite, so too few iterations will cause us to reject the key, not use // an implausible one. // - // |key->e| may be nullptr if created with |RSA_new_private_key_no_e|. + // `key->e` may be nullptr if created with `RSA_new_private_key_no_e`. enum bn_primality_result_t primality_result; auto *impl = FromOpaque(key); if (impl->e.get() == nullptr || // @@ -939,7 +945,7 @@ int RSA_check_fips(RSA *key) { } // FIPS pairwise consistency test (FIPS 140-2 4.9.2). Per FIPS 140-2 IG, - // section 9.9, it is not known whether |rsa| will be used for signing or + // section 9.9, it is not known whether `rsa` will be used for signing or // encryption, so either pair-wise consistency self-test is acceptable. We // perform a signing test. uint8_t data[32] = {0}; diff --git a/third_party/boringssl/src/crypto/fipsmodule/rsa/rsa_impl.cc.inc b/third_party/boringssl/src/crypto/fipsmodule/rsa/rsa_impl.cc.inc index ba08ae772..f4054062b 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/rsa/rsa_impl.cc.inc +++ b/third_party/boringssl/src/crypto/fipsmodule/rsa/rsa_impl.cc.inc @@ -97,7 +97,7 @@ int bssl::rsa_check_public_key(const RSA *rsa) { return 0; } - // The upper bound on |e_bits| and lower bound on |n_bits| imply e is + // The upper bound on `e_bits` and lower bound on `n_bits` imply e is // bounded by n. assert(BN_ucmp(impl->n.get(), impl->e.get()) > 0); } @@ -123,9 +123,9 @@ static int ensure_fixed_copy(UniquePtr *out, const BIGNUM *in, return 1; } -// freeze_private_key finishes initializing |rsa|'s private key components. -// After this function has returned, |rsa| may not be changed. This is needed -// because |RSA| is a public struct and, additionally, OpenSSL 1.1.0 opaquified +// freeze_private_key finishes initializing `rsa`'s private key components. +// After this function has returned, `rsa` may not be changed. This is needed +// because `RSA` is a public struct and, additionally, OpenSSL 1.1.0 opaquified // it wrong (see https://github.com/openssl/openssl/issues/5158). static int freeze_private_key(RSAImpl *rsa, BN_CTX *ctx) { rsa->lock.LockRead(); @@ -148,9 +148,9 @@ static int freeze_private_key(RSAImpl *rsa, BN_CTX *ctx) { // Pre-compute various intermediate values, as well as copies of private // exponents with correct widths. Note that other threads may concurrently - // read from |rsa->n|, |rsa->e|, etc., so any fixes must be in separate - // copies. We use |mont_n->N|, |mont_p->N|, and |mont_q->N| as copies of |n|, - // |p|, and |q| with the correct minimal widths. + // read from `rsa->n`, `rsa->e`, etc., so any fixes must be in separate + // copies. We use `mont_n->N`, `mont_p->N`, and `mont_q->N` as copies of `n`, + // `p`, and `q` with the correct minimal widths. if (rsa->mont_n == nullptr) { rsa->mont_n.reset(BN_MONT_CTX_new_for_modulus(rsa->n.get(), ctx)); @@ -160,9 +160,9 @@ static int freeze_private_key(RSAImpl *rsa, BN_CTX *ctx) { } n_fixed = &rsa->mont_n->N; - // The only public upper-bound of |rsa->d| is the bit length of |rsa->n|. The + // The only public upper-bound of `rsa->d` is the bit length of `rsa->n`. The // ASN.1 serialization of RSA private keys unfortunately leaks the byte length - // of |rsa->d|, but normalize it so we only leak it once, rather than per + // of `rsa->d`, but normalize it so we only leak it once, rather than per // operation. if (rsa->d != nullptr && !ensure_fixed_copy(&rsa->d_fixed, rsa->d.get(), n_fixed->width)) { @@ -201,7 +201,7 @@ static int freeze_private_key(RSAImpl *rsa, BN_CTX *ctx) { return 0; } - // Compute |iqmp_mont|, which is |iqmp| in Montgomery form and with the + // Compute `iqmp_mont`, which is `iqmp` in Montgomery form and with the // correct bit width. if (rsa->iqmp_mont == nullptr) { UniquePtr iqmp_mont(BN_new()); @@ -427,18 +427,18 @@ int bssl::rsa_default_private_transform(RSA *rsa, uint8_t *out, if (impl->e == nullptr && (impl->flags & RSA_FLAG_NO_PUBLIC_EXPONENT) == 0) { // Unless the private key was specifically created with an API like - // |RSA_new_private_key_no_e|, don't allow RSA keys to be missing the public + // `RSA_new_private_key_no_e`, don't allow RSA keys to be missing the public // exponent, which disables some fault attack mitigations. (It should not be - // possible to construct such an |RSA| object in the public API.) + // possible to construct such an `RSA` object in the public API.) OPENSSL_PUT_ERROR(RSA, RSA_R_NO_PUBLIC_EXPONENT); return 0; } if (impl->p != nullptr && impl->q != nullptr && impl->e != nullptr && impl->dmp1 != nullptr && impl->dmq1 != nullptr && impl->iqmp != nullptr && - // Require that we can reduce |f| by |impl->p| and |impl->q| in constant + // Require that we can reduce `f` by `impl->p` and `impl->q` in constant // time, which requires primes be the same size, rounded to the Montgomery - // coefficient. (See |mod_montgomery|.) This is not required by RFC 8017, + // coefficient. (See `mod_montgomery`.) This is not required by RFC 8017, // but it is true for keys generated by us and all common implementations. bn_less_than_montgomery_R(impl->q.get(), impl->mont_p.get()) && bn_less_than_montgomery_R(impl->p.get(), impl->mont_q.get())) { @@ -459,8 +459,8 @@ int bssl::rsa_default_private_transform(RSA *rsa, uint8_t *out, // works when the CRT isn't used. That attack is much less likely to succeed // than the CRT attack, but there have likely been improvements since 1997. // - // This check is cheap assuming |e| is small, which we require in - // |rsa_check_public_key|. + // This check is cheap assuming `e` is small, which we require in + // `rsa_check_public_key`. if (impl->e != nullptr) { BIGNUM *vrfy = BN_CTX_get(ctx.get()); if (vrfy == nullptr || @@ -472,7 +472,7 @@ int bssl::rsa_default_private_transform(RSA *rsa, uint8_t *out, } } - // The computation should have left |result| as a maximally-wide number, so + // The computation should have left `result` as a maximally-wide number, so // that it and serializing does not leak information about the magnitude of // the result. // @@ -487,8 +487,8 @@ int bssl::rsa_default_private_transform(RSA *rsa, uint8_t *out, return 1; } -// mod_montgomery sets |r| to |I| mod |p|. |I| must already be fully reduced -// modulo |p| times |q|. It returns one on success and zero on error. +// mod_montgomery sets `r` to `I` mod `p`. `I` must already be fully reduced +// modulo `p` times `q`. It returns one on success and zero on error. static int mod_montgomery(BIGNUM *r, const BIGNUM *I, const BIGNUM *p, const BN_MONT_CTX *mont_p, const BIGNUM *q, BN_CTX *ctx) { @@ -502,19 +502,19 @@ static int mod_montgomery(BIGNUM *r, const BIGNUM *I, const BIGNUM *p, if ( // Reduce mod p with Montgomery reduction. This computes I * R^-1 mod p. !BN_from_montgomery(r, I, mont_p, ctx) || - // Multiply by R^2 and do another Montgomery reduction to compute - // I * R^-1 * R^2 * R^-1 = I mod p. + // Multiply by R² and do another Montgomery reduction to compute + // I * R^-1 * R² * R^-1 = I mod p. !BN_to_montgomery(r, r, mont_p, ctx)) { return 0; } - // By precomputing R^3 mod p (normally |BN_MONT_CTX| only uses R^2 mod p) and - // adjusting the API for |BN_mod_exp_mont_consttime|, we could instead compute + // By precomputing R³ mod p (normally `BN_MONT_CTX` only uses R² mod p) and + // adjusting the API for `BN_mod_exp_mont_consttime`, we could instead compute // I * R mod p here and save a reduction per prime. But this would require // changing the RSAZ code and may not be worth it. Note that the RSAZ code // uses a different radix, so it uses R' = 2^1044. There we'd actually want - // R^2 * R', and would further benefit from a precomputed R'^2. It currently - // converts |mont_p->RR| to R'^2. + // R² * R', and would further benefit from a precomputed R'². It currently + // converts `mont_p->RR` to R'². return 1; } @@ -538,49 +538,49 @@ static int rsa_mod_exp_crt(BIGNUM *r0, const BIGNUM *I, RSAImpl *rsa, return 0; } - // Use the minimal-width versions of |n|, |p|, and |q|. Either works, but if + // Use the minimal-width versions of `n`, `p`, and `q`. Either works, but if // someone gives us non-minimal values, these will be slightly more efficient // on the non-Montgomery operations. BIGNUM *n = &rsa->mont_n->N; BIGNUM *p = &rsa->mont_p->N; BIGNUM *q = &rsa->mont_q->N; - // This is a pre-condition for |mod_montgomery|. It was already checked by the + // This is a pre-condition for `mod_montgomery`. It was already checked by the // caller. declassify_assert(BN_ucmp(I, n) < 0); - if ( // |m1| is the result modulo |q|. + if ( // `m1` is the result modulo `q`. !mod_montgomery(r1, I, q, rsa->mont_q.get(), p, ctx) || !BN_mod_exp_mont_consttime(m1, r1, rsa->dmq1_fixed.get(), q, ctx, rsa->mont_q.get()) || - // |r0| is the result modulo |p|. + // `r0` is the result modulo `p`. !mod_montgomery(r1, I, p, rsa->mont_p.get(), q, ctx) || !BN_mod_exp_mont_consttime(r0, r1, rsa->dmp1_fixed.get(), p, ctx, rsa->mont_p.get()) || - // Compute r0 = r0 - m1 mod p. |m1| is reduced mod |q|, not |p|, so we - // just run |mod_montgomery| again for srsaicity. This could be more - // efficient with more cases: if |p > q|, |m1| is already reduced. If - // |p < q| but they have the same bit width, |bn_reduce_once| suffices. + // Compute r0 = r0 - m1 mod p. `m1` is reduced mod `q`, not `p`, so we + // just run `mod_montgomery` again for srsaicity. This could be more + // efficient with more cases: if `p > q`, `m1` is already reduced. If + // `p < q` but they have the same bit width, `bn_reduce_once` suffices. // However, compared to over 2048 Montgomery multiplications above, this // difference is not measurable. !mod_montgomery(r1, m1, p, rsa->mont_p.get(), q, ctx) || !bn_mod_sub_consttime(r0, r0, r1, p, ctx) || // r0 = r0 * iqmp mod p. We use Montgomery multiplication to compute this - // in constant time. |iqmp_mont| is in Montgomery form and r0 is not, so + // in constant time. `iqmp_mont` is in Montgomery form and r0 is not, so // the result is taken out of Montgomery form. !BN_mod_mul_montgomery(r0, r0, rsa->iqmp_mont.get(), rsa->mont_p.get(), ctx) || // r0 = r0 * q + m1 gives the final result. Reducing modulo q gives m1, so // it is correct mod p. Reducing modulo p gives (r0-m1)*iqmp*q + m1 = r0, // so it is correct mod q. Finally, the result is bounded by [m1, n + m1), - // and the result is at least |m1|, so this must be the unique answer in + // and the result is at least `m1`, so this must be the unique answer in // [0, n). !bn_mul_consttime(r0, r0, q, ctx) || // !bn_uadd_consttime(r0, r0, m1)) { return 0; } - // The result should be bounded by |n|, but fixed-width operations may + // The result should be bounded by `n`, but fixed-width operations may // bound the width slightly higher, so fix it. This trips constant-time checks // because a naive data flow analysis does not realize the excess words are // publicly zero. @@ -600,9 +600,9 @@ static int ensure_bignum(UniquePtr *out) { return *out != nullptr; } -// generate_prime sets |out| to a prime with length |bits| such that |out|-1 is -// relatively prime to |e|. If |p| is non-NULL, |out| will also not be close to -// |p|. |pow2_bits_100| must be 2^(bits-100). +// generate_prime sets `out` to a prime with length `bits` such that `out`-1 is +// relatively prime to `e`. If `p` is non-NULL, `out` will also not be close to +// `p`. `pow2_bits_100` must be 2^(bits-100). // // This function fails with probability around 2^-21. static int generate_prime(BIGNUM *out, int bits, const BIGNUM *e, @@ -615,14 +615,14 @@ static int generate_prime(BIGNUM *out, int bits, const BIGNUM *e, assert(BN_is_pow2(pow2_bits_100)); assert(BN_is_bit_set(pow2_bits_100, bits - 100)); - // See FIPS 186-5 appendix A.1.3, steps 4 and 5. Note |bits| here is nlen/2. + // See FIPS 186-5 appendix A.1.3, steps 4 and 5. Note `bits` here is nlen/2. - // Use the limit from steps 4.7 and 5.8 for most values of |e|. When |e| is 3, + // Use the limit from steps 4.7 and 5.8 for most values of `e`. When `e` is 3, // the 186-5 limit is too low, so we use a higher one. Note this case is not - // reachable from |RSA_generate_key_fips|. + // reachable from `RSA_generate_key_fips`. // - // |limit| determines the failure probability. We must find a prime that is - // not 1 mod |e|. By the prime number theorem, we'll find one with probability + // `limit` determines the failure probability. We must find a prime that is + // not 1 mod `e`. By the prime number theorem, we'll find one with probability // p = (e-1)/e * 2/(ln(2)*bits). Note the second term is doubled because we // discard even numbers. // @@ -659,7 +659,7 @@ static int generate_prime(BIGNUM *out, int bits, const BIGNUM *e, } for (;;) { - // Generate a random number of length |bits| where the bottom bit is set and + // Generate a random number of length `bits` where the bottom bit is set and // top two bits are set (steps 4.2–4.4 and 5.2–5.4): // // - Setting the top two bits is permitted by steps 4.2.1 and 5.2.1. Doing @@ -673,7 +673,7 @@ static int generate_prime(BIGNUM *out, int bits, const BIGNUM *e, } if (p != nullptr) { - // If |p| and |out| are too close, try again (step 5.5). + // If `p` and `out` are too close, try again (step 5.5). if (!bn_abs_sub_consttime(tmp, out, p, ctx)) { return 0; } @@ -694,7 +694,7 @@ static int generate_prime(BIGNUM *out, int bits, const BIGNUM *e, return 0; } if (constant_time_declassify_int(relatively_prime)) { - // Test |out| for primality (steps 4.5.1 and 5.6.1). + // Test `out` for primality (steps 4.5.1 and 5.6.1). int is_probable_prime; if (!BN_primality_test(&is_probable_prime, out, BN_prime_checks_for_generation, ctx, 0, cb)) { @@ -719,7 +719,7 @@ static int generate_prime(BIGNUM *out, int bits, const BIGNUM *e, } // rsa_generate_key_impl generates an RSA key using a generalized version of -// FIPS 186-5 appendix A.1.3. |RSA_generate_key_fips| performs additional checks +// FIPS 186-5 appendix A.1.3. `RSA_generate_key_fips` performs additional checks // for FIPS-compliant key generation. // // This function returns one on success and zero on failure. It has a failure @@ -731,7 +731,7 @@ static int rsa_generate_key_impl(RSAImpl *rsa, int bits, const BIGNUM *e_value, return 0; } - // Always generate RSA keys which are a multiple of 128 bits. Round |bits| + // Always generate RSA keys which are a multiple of 128 bits. Round `bits` // down as needed. bits &= ~127; @@ -743,7 +743,7 @@ static int rsa_generate_key_impl(RSAImpl *rsa, int bits, const BIGNUM *e_value, // Reject excessively large public exponents. Windows CryptoAPI and Go don't // support values larger than 32 bits, so match their limits for generating - // keys. (|rsa_check_public_key| uses a slightly more conservative value, but + // keys. (`rsa_check_public_key` uses a slightly more conservative value, but // we don't need to support generating such keys.) // https://github.com/golang/go/issues/3161 // https://msdn.microsoft.com/en-us/library/aa387685(VS.85).aspx @@ -756,13 +756,13 @@ static int rsa_generate_key_impl(RSAImpl *rsa, int bits, const BIGNUM *e_value, // Catching these here prevents endless loops or slow computation when trying // to generate keys later, and results in a better error code. if ( - // Would fail in |bn_lcm_consttime| as it only allows positive integers. + // Would fail in `bn_lcm_consttime` as it only allows positive integers. BN_is_negative(e_value) || - // Would fail in |generate_prime| as only one |rsa->p|-1 is coprime with - // an even |e_value| and that one is a little bit short. (The R in RSA + // Would fail in `generate_prime` as only one `rsa->p`-1 is coprime with + // an even `e_value` and that one is a little bit short. (The R in RSA // doesn't stand for Rabin.) !BN_is_odd(e_value) || - // Would loop endlessly because it'll always compute an |rsa->d| exponent + // Would loop endlessly because it'll always compute an `rsa->d` exponent // of 1, which is too small. BN_is_one(e_value)) { OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_E_VALUE); @@ -809,11 +809,11 @@ static int rsa_generate_key_impl(RSAImpl *rsa, int bits, const BIGNUM *e_value, } do { - // Generate p and q, each of size |prime_bits|, using the steps outlined in + // Generate p and q, each of size `prime_bits`, using the steps outlined in // appendix FIPS 186-5 appendix C.3.3. // - // Each call to |generate_prime| fails with probability p = 2^-21. The - // probability that either call fails is 1 - (1-p)^2, which is around 2^-20. + // Each call to `generate_prime` fails with probability p = 2^-21. The + // probability that either call fails is 1 - (1-p)², which is around 2^-20. if (!generate_prime(rsa->p.get(), prime_bits, rsa->e.get(), nullptr, pow2_prime_bits_100, ctx.get(), cb) || !BN_GENCB_call(cb, 3, 0) || @@ -845,7 +845,7 @@ static int rsa_generate_key_impl(RSAImpl *rsa, int bits, const BIGNUM *e_value, return 0; } - // Retry if |rsa->d| <= 2^|prime_bits|. See appendix A.3.1's guidance on + // Retry if `rsa->d` <= 2^`prime_bits`. See appendix A.3.1's guidance on // values for d. When we retry, p and q are discarded, so it is safe to leak // this comparison. } while ( @@ -866,7 +866,7 @@ static int rsa_generate_key_impl(RSAImpl *rsa, int bits, const BIGNUM *e_value, } bn_set_minimal_width(rsa->n.get()); - // |rsa->n| is computed from the private key, but is public. + // `rsa->n` is computed from the private key, but is public. bn_declassify(rsa->n.get()); // Calculate q^-1 mod p. @@ -878,8 +878,8 @@ static int rsa_generate_key_impl(RSAImpl *rsa, int bits, const BIGNUM *e_value, return 0; } - // Sanity-check that |rsa->n| has the specified size. This is rsaied by - // |generate_prime|'s bounds. + // Sanity-check that `rsa->n` has the specified size. This is rsaied by + // `generate_prime`'s bounds. if (BN_num_bits(rsa->n.get()) != (unsigned)bits) { OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR); return 0; @@ -887,7 +887,7 @@ static int rsa_generate_key_impl(RSAImpl *rsa, int bits, const BIGNUM *e_value, // The key generation process is complex and thus error-prone. It could be // disastrous to generate and then use a bad key so double-check that the key - // makes sense. Also, while |rsa| is mutable, fill in the cached components. + // makes sense. Also, while `rsa` is mutable, fill in the cached components. if (!RSA_check_key(rsa) || !freeze_private_key(rsa, ctx.get())) { OPENSSL_PUT_ERROR(RSA, RSA_R_INTERNAL_ERROR); return 0; @@ -909,7 +909,7 @@ static int RSA_generate_key_ex_maybe_fips(RSAImpl *rsa, int bits, UniquePtr tmp; - // |rsa_generate_key_impl|'s 2^-20 failure probability is too high at scale, + // `rsa_generate_key_impl`'s 2^-20 failure probability is too high at scale, // so we run the FIPS algorithm four times, bringing it down to 2^-80. We // should just adjust the retry limit, but FIPS 186-5 prescribes that value // and thus results in unnecessary complexity. @@ -929,8 +929,8 @@ static int RSA_generate_key_ex_maybe_fips(RSAImpl *rsa, int bits, tmp = nullptr; failures++; - // Only retry on |RSA_R_TOO_MANY_ITERATIONS|. This is so a caller-induced - // failure in |BN_GENCB_call| is still fatal. + // Only retry on `RSA_R_TOO_MANY_ITERATIONS`. This is so a caller-induced + // failure in `BN_GENCB_call` is still fatal. } while (failures < 4 && ERR_equals(ERR_peek_error(), ERR_LIB_RSA, RSA_R_TOO_MANY_ITERATIONS)); @@ -992,7 +992,7 @@ BSSL_NAMESPACE_BEGIN DEFINE_METHOD_FUNCTION(RSA_METHOD, RSA_default_method) { // All of the methods are NULL to make it easier for the compiler/linker to // drop unused functions. The wrapper functions will select the appropriate - // |rsa_default_*| implementation. + // `rsa_default_*` implementation. OPENSSL_memset(out, 0, sizeof(RSA_METHOD)); out->common.is_static = 1; } diff --git a/third_party/boringssl/src/crypto/fipsmodule/self_check/fips.cc.inc b/third_party/boringssl/src/crypto/fipsmodule/self_check/fips.cc.inc index 5ed05cbb7..e2eaf70b6 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/self_check/fips.cc.inc +++ b/third_party/boringssl/src/crypto/fipsmodule/self_check/fips.cc.inc @@ -111,7 +111,7 @@ void bssl::boringssl_fips_inc_counter(enum fips_counter_t counter) { if (!CRYPTO_set_thread_local(OPENSSL_THREAD_LOCAL_FIPS_COUNTERS, array, OPENSSL_free)) { - // |OPENSSL_free| has already been called by |CRYPTO_set_thread_local|. + // `OPENSSL_free` has already been called by `CRYPTO_set_thread_local`. return; } } diff --git a/third_party/boringssl/src/crypto/fipsmodule/self_check/self_check.cc.inc b/third_party/boringssl/src/crypto/fipsmodule/self_check/self_check.cc.inc index 8ea40a183..d54e45f8f 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/self_check/self_check.cc.inc +++ b/third_party/boringssl/src/crypto/fipsmodule/self_check/self_check.cc.inc @@ -297,7 +297,7 @@ err: // // Self tests that are slow are deferred until the corresponding algorithm is // actually exercised, in FIPS mode. (In non-FIPS mode these tests are only run -// when requested by |BORINGSSL_self_test|.) +// when requested by `BORINGSSL_self_test`.) static int boringssl_self_test_rsa_sign() { UniquePtr rsa_key(self_test_rsa_private_key()); @@ -484,7 +484,7 @@ static int boringssl_self_test_ecc() { 0xa5, 0x2c, 0xb5, 0x9f, 0xeb, 0x70, 0xae, 0xde, 0x6c, 0xe3, 0xbf, 0xb3, 0xe0, 0x10, 0x54, 0x85, 0xab, 0xd8, 0x61, 0xd7, 0x7b, }; - // kP256PointResult is |kP256Scalar|×|kP256Point|. + // kP256PointResult is `kP256Scalar`×`kP256Point`. static const uint8_t kP256PointResult[65] = { 0x04, 0xf1, 0x63, 0x00, 0x88, 0xc5, 0xd5, 0xe9, 0x05, 0x52, 0xac, 0xb6, 0xec, 0x68, 0x76, 0xb8, 0x73, 0x7f, 0x0f, 0x72, 0x34, 0xe6, diff --git a/third_party/boringssl/src/crypto/fipsmodule/service_indicator/internal.h b/third_party/boringssl/src/crypto/fipsmodule/service_indicator/internal.h index 064618e6a..a46b1e0f4 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/service_indicator/internal.h +++ b/third_party/boringssl/src/crypto/fipsmodule/service_indicator/internal.h @@ -20,21 +20,21 @@ BSSL_NAMESPACE_BEGIN -// FIPS_service_indicator_before_call and |FIPS_service_indicator_after_call| +// FIPS_service_indicator_before_call and `FIPS_service_indicator_after_call` // both currently return the same local thread counter which is slowly // incremented whenever approved services are called. The -// |CALL_SERVICE_AND_CHECK_APPROVED| macro is strongly recommended over calling +// `CALL_SERVICE_AND_CHECK_APPROVED` macro is strongly recommended over calling // these functions directly. // -// |FIPS_service_indicator_before_call| is intended to be called immediately -// before an approved service, while |FIPS_service_indicator_after_call| should +// `FIPS_service_indicator_before_call` is intended to be called immediately +// before an approved service, while `FIPS_service_indicator_after_call` should // be called immediately after. If the values returned from these two functions // are not equal, this means that the service called in between is deemed to be // approved. If the values are still the same, this means the counter has not // been incremented, and the service called is not approved for FIPS. // -// In non-FIPS builds, |FIPS_service_indicator_before_call| always returns zero -// and |FIPS_service_indicator_after_call| always returns one. Thus calls always +// In non-FIPS builds, `FIPS_service_indicator_before_call` always returns zero +// and `FIPS_service_indicator_after_call` always returns one. Thus calls always // appear to be approved. This is intended to simplify testing. OPENSSL_EXPORT uint64_t FIPS_service_indicator_before_call(); OPENSSL_EXPORT uint64_t FIPS_service_indicator_after_call(); @@ -45,11 +45,11 @@ OPENSSL_EXPORT uint64_t FIPS_service_indicator_after_call(); // invoked. void FIPS_service_indicator_update_state(); -// FIPS_service_indicator_lock_state and |FIPS_service_indicator_unlock_state| -// stop |FIPS_service_indicator_update_state| from actually updating the service +// FIPS_service_indicator_lock_state and `FIPS_service_indicator_unlock_state` +// stop `FIPS_service_indicator_update_state` from actually updating the service // indicator. This is used when a primitive calls a potentially approved // primitive to avoid false positives. For example, just because a key -// generation calls |BCM_rand_bytes| (and thus the approved DRBG) doesn't mean +// generation calls `BCM_rand_bytes` (and thus the approved DRBG) doesn't mean // that the key generation operation itself is approved. // // This lock nests: i.e. locking twice is fine so long as each lock is paired @@ -57,7 +57,7 @@ void FIPS_service_indicator_update_state(); void FIPS_service_indicator_lock_state(); void FIPS_service_indicator_unlock_state(); -// The following functions may call |FIPS_service_indicator_update_state| if +// The following functions may call `FIPS_service_indicator_update_state` if // their parameter specifies an approved operation. void AEAD_GCM_verify_service_indicator(const EVP_AEAD_CTX *ctx); diff --git a/third_party/boringssl/src/crypto/fipsmodule/service_indicator/service_indicator.cc.inc b/third_party/boringssl/src/crypto/fipsmodule/service_indicator/service_indicator.cc.inc index ef8fb350f..20b2b0c09 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/service_indicator/service_indicator.cc.inc +++ b/third_party/boringssl/src/crypto/fipsmodule/service_indicator/service_indicator.cc.inc @@ -34,14 +34,14 @@ using namespace bssl; // state of the FIPS service indicator. struct fips_service_indicator_state { // lock_state records the number of times the indicator has been locked. - // When it is zero (i.e. |STATE_UNLOCKED|) then the indicator can be updated. + // When it is zero (i.e. `STATE_UNLOCKED`) then the indicator can be updated. uint64_t lock_state; // counter is the indicator state. It is incremented when an approved service // completes. uint64_t counter; }; -// service_indicator_get returns a pointer to the |fips_service_indicator_state| +// service_indicator_get returns a pointer to the `fips_service_indicator_state` // for the current thread. It returns nullptr on error. // // FIPS 140-3 requires that the module should provide the service indicator @@ -101,15 +101,15 @@ void bssl::FIPS_service_indicator_lock_state() { return; } - // |FIPS_service_indicator_lock_state| and - // |FIPS_service_indicator_unlock_state| should not under/overflow in normal + // `FIPS_service_indicator_lock_state` and + // `FIPS_service_indicator_unlock_state` should not under/overflow in normal // operation. They are still checked and errors added to facilitate testing in // service_indicator_test.cc. This should only happen if lock/unlock are // called in an incorrect order or multiple times in the same function. const uint64_t new_state = indicator->lock_state + 1; if (new_state < indicator->lock_state) { // Overflow. This would imply that our call stack length has exceeded a - // |uint64_t| which impossible on a 64-bit system. + // `uint64_t` which impossible on a 64-bit system. abort(); } @@ -201,8 +201,8 @@ static void evp_md_ctx_verify_service_indicator(const EVP_MD_CTX *ctx, // EVP_PKEY_RSA_PSS SPKIs aren't supported. if (pkey_type == EVP_PKEY_RSA) { // Message digest used in the private key should be of the same type - // as the given one, so we extract the MD type from the |EVP_PKEY| - // and compare it with the type in |ctx|. + // as the given one, so we extract the MD type from the `EVP_PKEY` + // and compare it with the type in `ctx`. const EVP_MD *pctx_md; if (!EVP_PKEY_CTX_get_signature_md(pctx, &pctx_md)) { goto err; @@ -230,7 +230,7 @@ static void evp_md_ctx_verify_service_indicator(const EVP_MD_CTX *ctx, } // The approved RSA key sizes for signing are 2048, 3072 and 4096 bits. - // Note: |EVP_PKEY_size| returns the size in bytes. + // Note: `EVP_PKEY_size` returns the size in bytes. size_t pkey_size = EVP_PKEY_size(FromOpaque(ctx->pctx)->pkey.get()); // Check if the MD type and the RSA key size are approved. @@ -322,7 +322,7 @@ uint64_t bssl::FIPS_service_indicator_before_call() { return 0; } uint64_t bssl::FIPS_service_indicator_after_call() { // One is returned so that the return value is always greater than zero, the - // return value of |FIPS_service_indicator_before_call|. This makes everything + // return value of `FIPS_service_indicator_before_call`. This makes everything // report as "approved" in non-FIPS builds. return 1; } diff --git a/third_party/boringssl/src/crypto/fipsmodule/sha/sha1.cc.inc b/third_party/boringssl/src/crypto/fipsmodule/sha/sha1.cc.inc index ce962d35d..8f8ac181f 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/sha/sha1.cc.inc +++ b/third_party/boringssl/src/crypto/fipsmodule/sha/sha1.cc.inc @@ -97,7 +97,7 @@ bcm_infallible bssl::BCM_fips_186_2_prf(uint8_t *out, size_t out_len, BCM_sha1_init(&ctx); BCM_sha1_transform(&ctx, block); - // XKEY = (1 + XKEY + w_i) mod 2^b + // XKEY = (1 + XKEY + wᵢ) mod 2ᵇ uint32_t carry = 1; for (int i = 4; i >= 0; i--) { uint32_t tmp = CRYPTO_load_u32_be(block + i * 4); @@ -105,7 +105,7 @@ bcm_infallible bssl::BCM_fips_186_2_prf(uint8_t *out, size_t out_len, CRYPTO_store_u32_be(block + i * 4, tmp); } - // Output w_i. + // Output wᵢ. if (out_len < SHA_DIGEST_LENGTH) { uint8_t buf[SHA_DIGEST_LENGTH]; sha1_output_state(buf, &ctx); diff --git a/third_party/boringssl/src/crypto/fipsmodule/sha/sha256.cc.inc b/third_party/boringssl/src/crypto/fipsmodule/sha/sha256.cc.inc index 20cfda9a4..8a179fd6e 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/sha/sha256.cc.inc +++ b/third_party/boringssl/src/crypto/fipsmodule/sha/sha256.cc.inc @@ -106,9 +106,9 @@ static void sha256_final_impl(uint8_t *out, size_t md_len, SHA256_CTX *c) { bcm_infallible bssl::BCM_sha256_final(uint8_t out[SHA256_DIGEST_LENGTH], SHA256_CTX *c) { - // Ideally we would assert |sha->md_len| is |SHA256_DIGEST_LENGTH| tomatch the - // size hint, but calling code often pairs |SHA224_Init| with |SHA256_Final| - // and expects |sha->md_len| to carry the size over. + // Ideally we would assert `sha->md_len` is `SHA256_DIGEST_LENGTH` tomatch the + // size hint, but calling code often pairs `SHA224_Init` with `SHA256_Final` + // and expects `sha->md_len` to carry the size over. // // TODO(davidben): Add an assert and fix code to match them up. sha256_final_impl(out, c->md_len, c); @@ -117,8 +117,8 @@ bcm_infallible bssl::BCM_sha256_final(uint8_t out[SHA256_DIGEST_LENGTH], bcm_infallible bssl::BCM_sha224_final(uint8_t out[SHA224_DIGEST_LENGTH], SHA256_CTX *ctx) { - // This function must be paired with |SHA224_Init|, which sets |ctx->md_len| - // to |SHA224_DIGEST_LENGTH|. + // This function must be paired with `SHA224_Init`, which sets `ctx->md_len` + // to `SHA224_DIGEST_LENGTH`. assert(ctx->md_len == SHA224_DIGEST_LENGTH); sha256_final_impl(out, SHA224_DIGEST_LENGTH, ctx); return bcm_infallible::approved; diff --git a/third_party/boringssl/src/crypto/fipsmodule/sha/sha512.cc.inc b/third_party/boringssl/src/crypto/fipsmodule/sha/sha512.cc.inc index f2282f4d0..164a80e8f 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/sha/sha512.cc.inc +++ b/third_party/boringssl/src/crypto/fipsmodule/sha/sha512.cc.inc @@ -92,8 +92,8 @@ static void sha512_block_data_order(uint64_t state[8], const uint8_t *in, bcm_infallible bssl::BCM_sha384_final(uint8_t out[SHA384_DIGEST_LENGTH], SHA512_CTX *sha) { - // This function must be paired with |BCM_sha384_init|, which sets - // |sha->md_len| to |SHA384_DIGEST_LENGTH|. + // This function must be paired with `BCM_sha384_init`, which sets + // `sha->md_len` to `SHA384_DIGEST_LENGTH`. assert(sha->md_len == SHA384_DIGEST_LENGTH); sha512_final_impl(out, SHA384_DIGEST_LENGTH, sha); return bcm_infallible::approved; @@ -111,8 +111,8 @@ bcm_infallible bssl::BCM_sha512_256_update(SHA512_CTX *sha, const void *data, bcm_infallible bssl::BCM_sha512_256_final(uint8_t out[SHA512_256_DIGEST_LENGTH], SHA512_CTX *sha) { - // This function must be paired with |BCM_sha512_256_init|, which sets - // |sha->md_len| to |SHA512_256_DIGEST_LENGTH|. + // This function must be paired with `BCM_sha512_256_init`, which sets + // `sha->md_len` to `SHA512_256_DIGEST_LENGTH`. assert(sha->md_len == SHA512_256_DIGEST_LENGTH); sha512_final_impl(out, SHA512_256_DIGEST_LENGTH, sha); return bcm_infallible::approved; @@ -170,9 +170,9 @@ bcm_infallible bssl::BCM_sha512_update(SHA512_CTX *c, const void *in_data, bcm_infallible bssl::BCM_sha512_final(uint8_t out[SHA512_DIGEST_LENGTH], SHA512_CTX *sha) { - // Ideally we would assert |sha->md_len| is |SHA512_DIGEST_LENGTH| to match - // the size hint, but calling code often pairs |BCM_sha384_init| with - // |BCM_sha512_final| and expects |sha->md_len| to carry the size over. + // Ideally we would assert `sha->md_len` is `SHA512_DIGEST_LENGTH` to match + // the size hint, but calling code often pairs `BCM_sha384_init` with + // `BCM_sha512_final` and expects `sha->md_len` to carry the size over. // // TODO(davidben): Add an assert and fix code to match them up. sha512_final_impl(out, sha->md_len, sha); diff --git a/third_party/boringssl/src/crypto/fipsmodule/slhdsa/fors.cc.inc b/third_party/boringssl/src/crypto/fipsmodule/slhdsa/fors.cc.inc index 8ef61c5f5..2f1678820 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/slhdsa/fors.cc.inc +++ b/third_party/boringssl/src/crypto/fipsmodule/slhdsa/fors.cc.inc @@ -26,7 +26,7 @@ using namespace bssl; -// Compute the base 2^a representation of `message` (algorithm 4, page 16). +// Compute the base 2ᵃ representation of `message` (algorithm 4, page 16). static void fors_base_b(const slh_dsa_config *config, uint32_t *indices, const uint8_t *message) { const size_t bits_per_index = config->fors_height; @@ -53,14 +53,15 @@ static void fors_base_b(const slh_dsa_config *config, uint32_t *indices, // Implements Algorithm 14: fors_skGen function (page 29) void bssl::slhdsa_fors_sk_gen(const slh_dsa_config *config, uint8_t *fors_sk, uint32_t idx, const uint8_t *sk_seed, - const uint8_t *pk_seed, uint8_t addr[32]) { + const slhdsa_hash_ctx *hash_ctx, + uint8_t addr[32]) { uint8_t sk_addr[32]; OPENSSL_memcpy(sk_addr, addr, sizeof(sk_addr)); slhdsa_set_type(config, sk_addr, SLHDSA_ADDR_TYPE_FORSPRF); slhdsa_copy_keypair_addr(config, sk_addr, addr); slhdsa_set_tree_index(config, sk_addr, idx); - slhdsa_thash_prf(config, fors_sk, pk_seed, sk_seed, sk_addr); + slhdsa_thash_prf(config, fors_sk, hash_ctx, sk_seed, sk_addr); } // Implements Algorithm 15: fors_node function (page 30) @@ -68,7 +69,8 @@ void bssl::slhdsa_fors_treehash(const slh_dsa_config *config, uint8_t *root_node, const uint8_t *sk_seed, uint32_t i /*target node index*/, uint32_t z /*target node height*/, - const uint8_t *pk_seed, uint8_t addr[32]) { + const slhdsa_hash_ctx *hash_ctx, + uint8_t addr[32]) { BSSL_CHECK(z <= config->fors_height); const uint32_t nodes_in_tree = 1u << (config->fors_height - z); BSSL_CHECK(i < (uint32_t)(config->fors_trees * nodes_in_tree)); @@ -77,24 +79,24 @@ void bssl::slhdsa_fors_treehash(const slh_dsa_config *config, uint8_t sk[SLHDSA_MAX_N]; slhdsa_set_tree_height(config, addr, 0); slhdsa_set_tree_index(config, addr, i); - slhdsa_fors_sk_gen(config, sk, i, sk_seed, pk_seed, addr); - slhdsa_thash_f(config, root_node, sk, pk_seed, addr); + slhdsa_fors_sk_gen(config, sk, i, sk_seed, hash_ctx, addr); + slhdsa_thash_f(config, root_node, sk, hash_ctx, addr); } else { // Stores left node and right node. uint8_t nodes[2 * SLHDSA_MAX_N]; - slhdsa_fors_treehash(config, nodes, sk_seed, 2 * i, z - 1, pk_seed, addr); + slhdsa_fors_treehash(config, nodes, sk_seed, 2 * i, z - 1, hash_ctx, addr); slhdsa_fors_treehash(config, nodes + config->n, sk_seed, 2 * i + 1, z - 1, - pk_seed, addr); + hash_ctx, addr); slhdsa_set_tree_height(config, addr, z); slhdsa_set_tree_index(config, addr, i); - slhdsa_thash_h(config, root_node, nodes, pk_seed, addr); + slhdsa_thash_h(config, root_node, nodes, hash_ctx, addr); } } // Implements Algorithm 16: fors_sign function (page 31) void bssl::slhdsa_fors_sign(const slh_dsa_config *config, uint8_t *fors_sig, const uint8_t *message, const uint8_t *sk_seed, - const uint8_t *pk_seed, uint8_t addr[32]) { + const slhdsa_hash_ctx *hash_ctx, uint8_t addr[32]) { uint32_t indices[SLHDSA_MAX_FORS_TREES]; BSSL_CHECK(config->fors_trees <= SLHDSA_MAX_FORS_TREES); BSSL_CHECK(config->fors_height <= SLHDSA_MAX_FORS_HEIGHT); @@ -111,13 +113,13 @@ void bssl::slhdsa_fors_sign(const slh_dsa_config *config, uint8_t *fors_sig, uint8_t *tree_sig = fors_sig + i * node_stride; slhdsa_fors_sk_gen(config, tree_sig, i * (1u << config->fors_height) + indices[i], sk_seed, - pk_seed, addr); + hash_ctx, addr); for (uint32_t j = 0; j < config->fors_height; ++j) { const size_t sibling = (indices[i] >> j) ^ 1u; // Write the FORS auth path element to the correct position. slhdsa_fors_treehash(config, tree_sig + (j + 1) * n, sk_seed, i * (1u << (config->fors_height - j)) + sibling, j, - pk_seed, addr); + hash_ctx, addr); } } } @@ -126,7 +128,8 @@ void bssl::slhdsa_fors_sign(const slh_dsa_config *config, uint8_t *fors_sig, void bssl::slhdsa_fors_pk_from_sig(const slh_dsa_config *config, uint8_t *fors_pk, const uint8_t *fors_sig, const uint8_t *message, - const uint8_t *pk_seed, uint8_t addr[32]) { + const slhdsa_hash_ctx *hash_ctx, + uint8_t addr[32]) { uint32_t indices[SLHDSA_MAX_FORS_TREES]; uint8_t tmp[2 * SLHDSA_MAX_N]; uint8_t roots[SLHDSA_MAX_FORS_TREES * SLHDSA_MAX_N]; @@ -149,7 +152,7 @@ void bssl::slhdsa_fors_pk_from_sig(const slh_dsa_config *config, uint32_t index = i * (1ULL << config->fors_height) + indices[i]; slhdsa_set_tree_index(config, addr, index); - slhdsa_thash_f(config, nodes, tree_sig, pk_seed, addr); + slhdsa_thash_f(config, nodes, tree_sig, hash_ctx, addr); for (uint32_t j = 0; j < config->fors_height; ++j) { slhdsa_set_tree_height(config, addr, j + 1); @@ -160,11 +163,11 @@ void bssl::slhdsa_fors_pk_from_sig(const slh_dsa_config *config, if (((indices[i] >> j) & 1) == 0) { OPENSSL_memcpy(tmp, nodes, n); OPENSSL_memcpy(tmp + n, auth + j * n, n); - slhdsa_thash_h(config, nodes + n, tmp, pk_seed, addr); + slhdsa_thash_h(config, nodes + n, tmp, hash_ctx, addr); } else { OPENSSL_memcpy(tmp, auth + j * n, n); OPENSSL_memcpy(tmp + n, nodes, n); - slhdsa_thash_h(config, nodes + n, tmp, pk_seed, addr); + slhdsa_thash_h(config, nodes + n, tmp, hash_ctx, addr); } OPENSSL_memcpy(nodes, nodes + n, n); } @@ -175,5 +178,5 @@ void bssl::slhdsa_fors_pk_from_sig(const slh_dsa_config *config, OPENSSL_memcpy(forspk_addr, addr, sizeof(forspk_addr)); slhdsa_set_type(config, forspk_addr, SLHDSA_ADDR_TYPE_FORSPK); slhdsa_copy_keypair_addr(config, forspk_addr, addr); - slhdsa_thash_tk(config, fors_pk, roots, pk_seed, forspk_addr); + slhdsa_thash_tk(config, fors_pk, roots, hash_ctx, forspk_addr); } diff --git a/third_party/boringssl/src/crypto/fipsmodule/slhdsa/fors.h b/third_party/boringssl/src/crypto/fipsmodule/slhdsa/fors.h index f123a22fe..9963778db 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/slhdsa/fors.h +++ b/third_party/boringssl/src/crypto/fipsmodule/slhdsa/fors.h @@ -16,6 +16,7 @@ #define OPENSSL_HEADER_CRYPTO_FIPSMODULE_SLHDSA_FORS_H #include "./params.h" +#include "./thash.h" BSSL_NAMESPACE_BEGIN @@ -23,25 +24,25 @@ BSSL_NAMESPACE_BEGIN // Implements Algorithm 14: fors_skGen function (page 29) void slhdsa_fors_sk_gen(const slh_dsa_config *config, uint8_t *fors_sk, uint32_t idx, const uint8_t *sk_seed, - const uint8_t *pk_seed, uint8_t addr[32]); + const slhdsa_hash_ctx *hash_ctx, uint8_t addr[32]); // Implements Algorithm 15: fors_node function (page 30) void slhdsa_fors_treehash(const slh_dsa_config *config, uint8_t *root_node, const uint8_t *sk_seed, uint32_t i /*target node index*/, uint32_t z /*target node height*/, - const uint8_t *pk_seed, + const slhdsa_hash_ctx *hash_ctx, uint8_t addr[32]); // Implements Algorithm 16: fors_sign function (page 31) void slhdsa_fors_sign(const slh_dsa_config *config, uint8_t *fors_sig, const uint8_t *message, const uint8_t *sk_seed, - const uint8_t *pk_seed, uint8_t addr[32]); + const slhdsa_hash_ctx *hash_ctx, uint8_t addr[32]); // Implements Algorithm 17: fors_pkFromSig function (page 32) void slhdsa_fors_pk_from_sig(const slh_dsa_config *config, uint8_t *fors_pk, const uint8_t *fors_sig, const uint8_t *message, - const uint8_t *pk_seed, uint8_t addr[32]); + const slhdsa_hash_ctx *hash_ctx, uint8_t addr[32]); BSSL_NAMESPACE_END diff --git a/third_party/boringssl/src/crypto/fipsmodule/slhdsa/merkle.cc.inc b/third_party/boringssl/src/crypto/fipsmodule/slhdsa/merkle.cc.inc index 84602c88b..243982efb 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/slhdsa/merkle.cc.inc +++ b/third_party/boringssl/src/crypto/fipsmodule/slhdsa/merkle.cc.inc @@ -29,58 +29,60 @@ using namespace bssl; // Implements Algorithm 9: xmss_node function (page 23) void bssl::slhdsa_treehash(const slh_dsa_config *config, uint8_t *out_pk, const uint8_t *sk_seed, uint32_t i, uint32_t z, - const uint8_t *pk_seed, uint8_t addr[32]) { + const slhdsa_hash_ctx *hash_ctx, uint8_t addr[32]) { BSSL_CHECK(z <= config->tree_height); BSSL_CHECK(i < (uint32_t)(1u << (config->tree_height - z))); if (z == 0) { slhdsa_set_type(config, addr, SLHDSA_ADDR_TYPE_WOTS); slhdsa_set_keypair_addr(config, addr, i); - slhdsa_wots_pk_gen(config, out_pk, sk_seed, pk_seed, addr); + slhdsa_wots_pk_gen(config, out_pk, sk_seed, hash_ctx, addr); } else { // Stores left node and right node. uint8_t nodes[2 * SLHDSA_MAX_N]; - slhdsa_treehash(config, nodes, sk_seed, 2 * i, z - 1, pk_seed, addr); + slhdsa_treehash(config, nodes, sk_seed, 2 * i, z - 1, hash_ctx, addr); slhdsa_treehash(config, nodes + config->n, sk_seed, 2 * i + 1, z - 1, - pk_seed, addr); + hash_ctx, addr); slhdsa_set_type(config, addr, SLHDSA_ADDR_TYPE_HASHTREE); slhdsa_set_tree_height(config, addr, z); slhdsa_set_tree_index(config, addr, i); - slhdsa_thash_h(config, out_pk, nodes, pk_seed, addr); + slhdsa_thash_h(config, out_pk, nodes, hash_ctx, addr); } } // Implements Algorithm 10: xmss_sign function (page 24) void bssl::slhdsa_xmss_sign(const slh_dsa_config *config, uint8_t *sig, const uint8_t *msg, unsigned int idx, - const uint8_t *sk_seed, const uint8_t *pk_seed, + const uint8_t *sk_seed, + const slhdsa_hash_ctx *hash_ctx, uint8_t addr[32]) { // Build authentication path const size_t wots_bytes = slhdsa_wots_bytes(config); const size_t n = config->n; for (size_t j = 0; j < config->tree_height; ++j) { unsigned int k = (idx >> j) ^ 1; - slhdsa_treehash(config, sig + wots_bytes + j * n, sk_seed, k, j, pk_seed, + slhdsa_treehash(config, sig + wots_bytes + j * n, sk_seed, k, j, hash_ctx, addr); } // Compute WOTS+ signature slhdsa_set_type(config, addr, SLHDSA_ADDR_TYPE_WOTS); slhdsa_set_keypair_addr(config, addr, idx); - slhdsa_wots_sign(config, sig, msg, sk_seed, pk_seed, addr); + slhdsa_wots_sign(config, sig, msg, sk_seed, hash_ctx, addr); } // Implements Algorithm 11: xmss_pkFromSig function (page 25) void bssl::slhdsa_xmss_pk_from_sig(const slh_dsa_config *config, uint8_t *root, const uint8_t *xmss_sig, unsigned int idx, - const uint8_t *msg, const uint8_t *pk_seed, + const uint8_t *msg, + const slhdsa_hash_ctx *hash_ctx, uint8_t addr[32]) { // Stores node[0] and node[1] from Algorithm 11 slhdsa_set_type(config, addr, SLHDSA_ADDR_TYPE_WOTS); slhdsa_set_keypair_addr(config, addr, idx); uint8_t node[2 * SLHDSA_MAX_N]; const size_t n = config->n; - slhdsa_wots_pk_from_sig(config, node, xmss_sig, msg, pk_seed, addr); + slhdsa_wots_pk_from_sig(config, node, xmss_sig, msg, hash_ctx, addr); slhdsa_set_type(config, addr, SLHDSA_ADDR_TYPE_HASHTREE); slhdsa_set_tree_index(config, addr, idx); @@ -94,13 +96,13 @@ void bssl::slhdsa_xmss_pk_from_sig(const slh_dsa_config *config, uint8_t *root, slhdsa_get_tree_index(config, addr) >> 1); OPENSSL_memcpy(tmp, node, n); OPENSSL_memcpy(tmp + n, auth + k * n, n); - slhdsa_thash_h(config, node + n, tmp, pk_seed, addr); + slhdsa_thash_h(config, node + n, tmp, hash_ctx, addr); } else { slhdsa_set_tree_index(config, addr, (slhdsa_get_tree_index(config, addr) - 1) >> 1); OPENSSL_memcpy(tmp, auth + k * n, n); OPENSSL_memcpy(tmp + n, node, n); - slhdsa_thash_h(config, node + n, tmp, pk_seed, addr); + slhdsa_thash_h(config, node + n, tmp, hash_ctx, addr); } OPENSSL_memcpy(node, node + n, n); } @@ -111,14 +113,14 @@ void bssl::slhdsa_xmss_pk_from_sig(const slh_dsa_config *config, uint8_t *root, void bssl::slhdsa_ht_sign(const slh_dsa_config *config, uint8_t *sig, const uint8_t *message, uint64_t idx_tree, uint32_t idx_leaf, const uint8_t *sk_seed, - const uint8_t *pk_seed) { + const slhdsa_hash_ctx *hash_ctx) { uint8_t addr[32] = {0}; slhdsa_set_tree_addr(config, addr, idx_tree); // Layer 0 - slhdsa_xmss_sign(config, sig, message, idx_leaf, sk_seed, pk_seed, addr); + slhdsa_xmss_sign(config, sig, message, idx_leaf, sk_seed, hash_ctx, addr); uint8_t root[SLHDSA_MAX_N]; - slhdsa_xmss_pk_from_sig(config, root, sig, idx_leaf, message, pk_seed, addr); + slhdsa_xmss_pk_from_sig(config, root, sig, idx_leaf, message, hash_ctx, addr); sig += slhdsa_xmss_bytes(config); // All other layers @@ -129,9 +131,10 @@ void bssl::slhdsa_ht_sign(const slh_dsa_config *config, uint8_t *sig, idx_tree = idx_tree >> config->tree_height; slhdsa_set_layer_addr(config, addr, j); slhdsa_set_tree_addr(config, addr, idx_tree); - slhdsa_xmss_sign(config, sig, root, idx_leaf, sk_seed, pk_seed, addr); + slhdsa_xmss_sign(config, sig, root, idx_leaf, sk_seed, hash_ctx, addr); if (j < (config->d - 1)) { - slhdsa_xmss_pk_from_sig(config, root, sig, idx_leaf, root, pk_seed, addr); + slhdsa_xmss_pk_from_sig(config, root, sig, idx_leaf, root, hash_ctx, + addr); } sig += slhdsa_xmss_bytes(config); @@ -142,12 +145,12 @@ void bssl::slhdsa_ht_sign(const slh_dsa_config *config, uint8_t *sig, int bssl::slhdsa_ht_verify(const slh_dsa_config *config, const uint8_t *sig, const uint8_t *message, uint64_t idx_tree, uint32_t idx_leaf, const uint8_t *pk_root, - const uint8_t *pk_seed) { + const slhdsa_hash_ctx *hash_ctx) { uint8_t addr[32] = {0}; slhdsa_set_tree_addr(config, addr, idx_tree); uint8_t node[SLHDSA_MAX_N]; - slhdsa_xmss_pk_from_sig(config, node, sig, idx_leaf, message, pk_seed, addr); + slhdsa_xmss_pk_from_sig(config, node, sig, idx_leaf, message, hash_ctx, addr); BSSL_CHECK(config->tree_height <= SLHDSA_MAX_TREE_HEIGHT); const uint32_t leaf_mask = (1u << config->tree_height) - 1; @@ -158,7 +161,7 @@ int bssl::slhdsa_ht_verify(const slh_dsa_config *config, const uint8_t *sig, slhdsa_set_tree_addr(config, addr, idx_tree); slhdsa_xmss_pk_from_sig(config, node, sig + j * slhdsa_xmss_bytes(config), - idx_leaf, node, pk_seed, addr); + idx_leaf, node, hash_ctx, addr); } return memcmp(node, pk_root, config->n) == 0; } diff --git a/third_party/boringssl/src/crypto/fipsmodule/slhdsa/merkle.h b/third_party/boringssl/src/crypto/fipsmodule/slhdsa/merkle.h index c13b6ff37..5d2a140cd 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/slhdsa/merkle.h +++ b/third_party/boringssl/src/crypto/fipsmodule/slhdsa/merkle.h @@ -20,6 +20,7 @@ #include #include "./params.h" +#include "./thash.h" BSSL_NAMESPACE_BEGIN @@ -27,31 +28,33 @@ BSSL_NAMESPACE_BEGIN // Implements Algorithm 9: xmss_node function (page 23) void slhdsa_treehash(const slh_dsa_config *config, uint8_t *out_pk, const uint8_t *sk_seed, uint32_t i /*target node index*/, - uint32_t z /*target node height*/, const uint8_t *pk_seed, - uint8_t addr[32]); + uint32_t z /*target node height*/, + const slhdsa_hash_ctx *hash_ctx, uint8_t addr[32]); // Implements Algorithm 10: xmss_sign function (page 24) void slhdsa_xmss_sign(const slh_dsa_config *config, uint8_t *sig, const uint8_t *msg, unsigned int idx, - const uint8_t *sk_seed, const uint8_t *pk_seed, + const uint8_t *sk_seed, const slhdsa_hash_ctx *hash_ctx, uint8_t addr[32]); // Implements Algorithm 11: xmss_pkFromSig function (page 25) void slhdsa_xmss_pk_from_sig(const slh_dsa_config *config, uint8_t *root, const uint8_t *xmss_sig, unsigned int idx, - const uint8_t *msg, const uint8_t *pk_seed, + const uint8_t *msg, + const slhdsa_hash_ctx *hash_ctx, uint8_t addr[32]); // Implements Algorithm 12: ht_sign function (page 27) void slhdsa_ht_sign(const slh_dsa_config *config, uint8_t *sig, - const uint8_t *message, uint64_t idx_tree, uint32_t idx_leaf, - const uint8_t *sk_seed, const uint8_t *pk_seed); + const uint8_t *message, uint64_t idx_tree, + uint32_t idx_leaf, const uint8_t *sk_seed, + const slhdsa_hash_ctx *hash_ctx); // Implements Algorithm 13: ht_verify function (page 28) int slhdsa_ht_verify(const slh_dsa_config *config, const uint8_t *sig, const uint8_t *message, uint64_t idx_tree, uint32_t idx_leaf, const uint8_t *pk_root, - const uint8_t *pk_seed); + const slhdsa_hash_ctx *hash_ctx); BSSL_NAMESPACE_END diff --git a/third_party/boringssl/src/crypto/fipsmodule/slhdsa/slhdsa.cc.inc b/third_party/boringssl/src/crypto/fipsmodule/slhdsa/slhdsa.cc.inc index 7d56ebe65..91c256476 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/slhdsa/slhdsa.cc.inc +++ b/third_party/boringssl/src/crypto/fipsmodule/slhdsa/slhdsa.cc.inc @@ -65,12 +65,15 @@ bcm_infallible generate_key_from_seed_no_self_test(const slh_dsa_config *config, // Initialize PK.seed from seed. OPENSSL_memcpy(out_public_key, seed + 2 * config->n, config->n); + slhdsa_hash_ctx hash_ctx; + slhdsa_hash_ctx_init(config, &hash_ctx, out_public_key); + uint8_t addr[32] = {0}; slhdsa_set_layer_addr(config, addr, config->d - 1); // Set PK.root slhdsa_treehash(config, out_public_key + config->n, out_secret_key, 0, - config->tree_height, out_public_key, addr); + config->tree_height, &hash_ctx, addr); OPENSSL_memcpy(out_secret_key + 3 * config->n, out_public_key + config->n, config->n); @@ -157,15 +160,17 @@ bcm_infallible sign_internal_no_self_test( slhdsa_set_type(config, addr, SLHDSA_ADDR_TYPE_FORSTREE); slhdsa_set_keypair_addr(config, addr, idx_leaf); - slhdsa_fors_sign(config, out_signature + n, fors_digest, sk_seed, pk_seed, + slhdsa_hash_ctx hash_ctx; + slhdsa_hash_ctx_init(config, &hash_ctx, pk_seed); + slhdsa_fors_sign(config, out_signature + n, fors_digest, sk_seed, &hash_ctx, addr); uint8_t pk_fors[SLHDSA_MAX_N]; slhdsa_fors_pk_from_sig(config, pk_fors, out_signature + n, fors_digest, - pk_seed, addr); + &hash_ctx, addr); slhdsa_ht_sign(config, out_signature + n + slhdsa_fors_bytes(config), pk_fors, - idx_tree, idx_leaf, sk_seed, pk_seed); + idx_tree, idx_leaf, sk_seed, &hash_ctx); return bcm_infallible::approved; } @@ -213,11 +218,13 @@ bcm_status verify_internal(const slh_dsa_config *config, slhdsa_set_keypair_addr(config, addr, idx_leaf); uint8_t pk_fors[SLHDSA_MAX_N]; - slhdsa_fors_pk_from_sig(config, pk_fors, sig_fors, fors_digest, pk_seed, + slhdsa_hash_ctx hash_ctx; + slhdsa_hash_ctx_init(config, &hash_ctx, pk_seed); + slhdsa_fors_pk_from_sig(config, pk_fors, sig_fors, fors_digest, &hash_ctx, addr); if (!slhdsa_ht_verify(config, sig_ht, pk_fors, idx_tree, idx_leaf, pk_root, - pk_seed)) { + &hash_ctx)) { return bcm_status::failure; } @@ -313,11 +320,14 @@ bcm_infallible bssl::BCM_slhdsa_sha2_128s_generate_key_from_seed( &kSLHDSAConfigSHA2_128s, out_public_key, out_secret_key, seed); } +// The `BCM_slhdsa_shake_256f_*` functions in this file are not FIPS approved +// because the module's CASTs only cover SLH-DSA-SHA2-128s. Thus they report +// `not_approved` and do not run the SHA2-128s self-tests. + bcm_infallible bssl::BCM_slhdsa_shake_256f_generate_key_from_seed( uint8_t out_public_key[BCM_SLHDSA_SHAKE_256F_PUBLIC_KEY_BYTES], uint8_t out_secret_key[BCM_SLHDSA_SHAKE_256F_PRIVATE_KEY_BYTES], const uint8_t seed[3 * BCM_SLHDSA_SHAKE_256F_N]) { - fips::ensure_keygen_self_test(); return generate_key_from_seed_no_self_test( &kSLHDSAConfigSHAKE_256f, out_public_key, out_secret_key, seed); } @@ -343,7 +353,7 @@ bcm_status bssl::BCM_slhdsa_shake_256f_generate_key_from_seed_fips( } BCM_slhdsa_shake_256f_generate_key_from_seed(out_public_key, out_secret_key, seed); - return bcm_status::approved; + return bcm_status::not_approved; } bcm_infallible bssl::BCM_slhdsa_sha2_128s_generate_key( @@ -381,7 +391,7 @@ bcm_status bssl::BCM_slhdsa_shake_256f_generate_key_fips( return bcm_status::failure; } BCM_slhdsa_shake_256f_generate_key(out_public_key, out_private_key); - return bcm_status::approved; + return bcm_status::not_approved; } bcm_infallible bssl::BCM_slhdsa_sha2_128s_public_from_private( @@ -397,7 +407,7 @@ bcm_infallible bssl::BCM_slhdsa_shake_256f_public_from_private( const uint8_t private_key[BCM_SLHDSA_SHAKE_256F_PRIVATE_KEY_BYTES]) { OPENSSL_memcpy(out_public_key, private_key + 2 * BCM_SLHDSA_SHAKE_256F_N, BCM_SLHDSA_SHAKE_256F_N * 2); - return bcm_infallible::approved; + return bcm_infallible::not_approved; } bcm_status bssl::BCM_slhdsa_sha2_128s_sign( @@ -440,7 +450,7 @@ bcm_status bssl::BCM_slhdsa_shake_256f_sign( BCM_slhdsa_shake_256f_sign_internal(out_signature, private_key, M_prime_header, context, context_len, msg, msg_len, entropy); - return bcm_status::approved; + return bcm_status::not_approved; } static int slhdsa_get_context_and_oid(uint8_t *out_context_and_oid, @@ -508,10 +518,10 @@ bcm_infallible bssl::BCM_slhdsa_shake_256f_sign_internal( const uint8_t header[BCM_SLHDSA_M_PRIME_HEADER_LEN], const uint8_t *context, size_t context_len, const uint8_t *msg, size_t msg_len, const uint8_t entropy[BCM_SLHDSA_SHAKE_256F_N]) { - fips::ensure_sign_self_test(); - return sign_internal_no_self_test(&kSLHDSAConfigSHAKE_256f, out_signature, - secret_key, header, context, context_len, - msg, msg_len, entropy); + sign_internal_no_self_test(&kSLHDSAConfigSHAKE_256f, out_signature, + secret_key, header, context, context_len, msg, + msg_len, entropy); + return bcm_infallible::not_approved; } bcm_status bssl::BCM_slhdsa_sha2_128s_prehash_sign( @@ -569,7 +579,7 @@ bcm_status bssl::BCM_slhdsa_shake_256f_prehash_sign( BCM_slhdsa_shake_256f_sign_internal( out_signature, private_key, M_prime_header, context_and_oid, context_and_oid_len, hashed_msg, hashed_msg_len, entropy); - return bcm_status::approved; + return bcm_status::not_approved; } // Implements Algorithm 24: slh_verify function (Section 10.3, page 41) @@ -678,10 +688,9 @@ bcm_status bssl::BCM_slhdsa_shake_256f_verify_internal( const uint8_t public_key[BCM_SLHDSA_SHAKE_256F_PUBLIC_KEY_BYTES], const uint8_t header[BCM_SLHDSA_M_PRIME_HEADER_LEN], const uint8_t *context, size_t context_len, const uint8_t *msg, size_t msg_len) { - fips::ensure_verify_self_test(); - return verify_internal(&kSLHDSAConfigSHAKE_256f, signature, signature_len, - public_key, header, context, context_len, msg, - msg_len); + return bcm_as_not_approved_status(bcm_success( + verify_internal(&kSLHDSAConfigSHAKE_256f, signature, signature_len, + public_key, header, context, context_len, msg, msg_len))); } int bssl::boringssl_self_test_slhdsa() { diff --git a/third_party/boringssl/src/crypto/fipsmodule/slhdsa/thash.cc.inc b/third_party/boringssl/src/crypto/fipsmodule/slhdsa/thash.cc.inc index 2cb608fb1..6e76b5640 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/slhdsa/thash.cc.inc +++ b/third_party/boringssl/src/crypto/fipsmodule/slhdsa/thash.cc.inc @@ -20,28 +20,71 @@ #include #include "../../internal.h" +#include "../bcm_interface.h" #include "../keccak/internal.h" #include "./thash.h" using namespace bssl; -// Internal thash function used by F, H, and T_l for SHA-256 based parameter -// sets (Section 11.2, pages 44-46) +void bssl::slhdsa_hash_ctx_init(const slh_dsa_config *config, + slhdsa_hash_ctx *hash_ctx, + const uint8_t *pk_seed) { + hash_ctx->pk_seed = pk_seed; + if (config->hash_type != SLH_DSA_HASH_SHA2_256) { + return; + } + // Absorb PK.seed || toByte(0, 64 - n), which is exactly one SHA-256 block. + BSSL_CHECK(config->hash_block_bytes == SHA256_CBLOCK); + BSSL_CHECK(config->n <= SHA256_CBLOCK); + uint8_t block[SHA256_CBLOCK] = {0}; + OPENSSL_memcpy(block, pk_seed, config->n); + BCM_sha256_init(&hash_ctx->sha256_seeded); + BCM_sha256_update(&hash_ctx->sha256_seeded, block, sizeof(block)); +} + +// Internal thash function used by F, H, T_l, and PRF for SHA-256 based +// parameter sets (Section 11.2, pages 44-46). Every input starts with the same +// block, PK.seed || toByte(0, 64 - n), so this resumes from the state that +// `slhdsa_hash_ctx_init` precomputed after that block. static void slhdsa_thash_sha256(const slh_dsa_config *config, uint8_t *output, const uint8_t *input, size_t input_blocks, - const uint8_t *pk_seed, uint8_t addr[32]) { + const slhdsa_hash_ctx *hash_ctx, + uint8_t addr[32]) { BSSL_CHECK(config->hash_type == SLH_DSA_HASH_SHA2_256); - SHA256_CTX sha256; - SHA256_Init(&sha256); - - uint8_t zeros[SLHDSA_MAX_HASH_BLOCK_BYTES] = {0}; - BSSL_CHECK(config->hash_block_bytes <= sizeof(zeros)); - BSSL_CHECK(config->hash_block_bytes >= config->n); - SHA256_Update(&sha256, pk_seed, config->n); - SHA256_Update(&sha256, zeros, config->hash_block_bytes - config->n); + const size_t input_len = input_blocks * config->n; + const size_t msg_len = SLHDSA_ADDR_COMPRESSED_BYTES + input_len; + + // For F, H, and PRF, ADRSc || input fits in a single block together with the + // SHA-256 padding: a 0x80 byte and the 64-bit message length. Finish those + // with one compression function call, skipping the overhead of the generic + // SHA256_Update and SHA256_Final logic. This branch only depends on public + // parameters. + if (msg_len + 1 + 8 <= SHA256_CBLOCK) { + uint8_t block[SHA256_CBLOCK] = {0}; + OPENSSL_memcpy(block, addr, SLHDSA_ADDR_COMPRESSED_BYTES); + OPENSSL_memcpy(block + SLHDSA_ADDR_COMPRESSED_BYTES, input, input_len); + block[msg_len] = 0x80; + // The message length, in bits, includes the precomputed first block. + CRYPTO_store_u64_be(block + SHA256_CBLOCK - 8, + (uint64_t{SHA256_CBLOCK} + msg_len) * 8); + + uint32_t state[8]; + OPENSSL_memcpy(state, hash_ctx->sha256_seeded.h, sizeof(state)); + BCM_sha256_transform_blocks(state, block, 1); + + static_assert(SLHDSA_MAX_N <= SHA256_DIGEST_LENGTH, "n is too large"); + uint8_t hash[SHA256_DIGEST_LENGTH]; + for (size_t i = 0; i < 8; i++) { + CRYPTO_store_u32_be(hash + 4 * i, state[i]); + } + OPENSSL_memcpy(output, hash, config->n); + return; + } + + SHA256_CTX sha256 = hash_ctx->sha256_seeded; SHA256_Update(&sha256, addr, SLHDSA_ADDR_COMPRESSED_BYTES); - SHA256_Update(&sha256, input, input_blocks * config->n); + SHA256_Update(&sha256, input, input_len); uint8_t hash[SLHDSA_MAX_DIGEST_SIZE]; SHA256_Final(hash, &sha256); @@ -66,13 +109,15 @@ static void slhdsa_thash_shake(const slh_dsa_config *config, uint8_t *output, static void slhdsa_thash_dispatch(const slh_dsa_config *config, uint8_t *output, const uint8_t *input, size_t input_blocks, - const uint8_t *pk_seed, uint8_t addr[32]) { + const slhdsa_hash_ctx *hash_ctx, + uint8_t addr[32]) { switch (config->hash_type) { case SLH_DSA_HASH_SHA2_256: - slhdsa_thash_sha256(config, output, input, input_blocks, pk_seed, addr); + slhdsa_thash_sha256(config, output, input, input_blocks, hash_ctx, addr); return; case SLH_DSA_HASH_SHAKE_256: - slhdsa_thash_shake(config, output, input, input_blocks, pk_seed, addr); + slhdsa_thash_shake(config, output, input, input_blocks, hash_ctx->pk_seed, + addr); return; } BSSL_CHECK(false); @@ -260,9 +305,10 @@ void bssl::slhdsa_thash_hmsg(const slh_dsa_config *config, uint8_t *output, // Implements PRF function (Section 4.1, page 11 and Section 11.2, pages 44-46) static void slhdsa_thash_prf_sha256(const slh_dsa_config *config, - uint8_t *output, const uint8_t *pk_seed, + uint8_t *output, + const slhdsa_hash_ctx *hash_ctx, const uint8_t *sk_seed, uint8_t addr[32]) { - slhdsa_thash_sha256(config, output, sk_seed, 1, pk_seed, addr); + slhdsa_thash_sha256(config, output, sk_seed, 1, hash_ctx, addr); } static void slhdsa_thash_prf_shake(const slh_dsa_config *config, @@ -280,14 +326,14 @@ static void slhdsa_thash_prf_shake(const slh_dsa_config *config, } void bssl::slhdsa_thash_prf(const slh_dsa_config *config, uint8_t *output, - const uint8_t *pk_seed, const uint8_t *sk_seed, - uint8_t addr[32]) { + const slhdsa_hash_ctx *hash_ctx, + const uint8_t *sk_seed, uint8_t addr[32]) { switch (config->hash_type) { case SLH_DSA_HASH_SHA2_256: - slhdsa_thash_prf_sha256(config, output, pk_seed, sk_seed, addr); + slhdsa_thash_prf_sha256(config, output, hash_ctx, sk_seed, addr); return; case SLH_DSA_HASH_SHAKE_256: - slhdsa_thash_prf_shake(config, output, pk_seed, sk_seed, addr); + slhdsa_thash_prf_shake(config, output, hash_ctx->pk_seed, sk_seed, addr); return; } BSSL_CHECK(false); @@ -296,31 +342,31 @@ void bssl::slhdsa_thash_prf(const slh_dsa_config *config, uint8_t *output, // Implements T_l function for WOTS+ public key compression (Section 4.1, page // 11 and Section 11.2, pages 44-46) void bssl::slhdsa_thash_tl(const slh_dsa_config *config, uint8_t *output, - const uint8_t *input, const uint8_t *pk_seed, - uint8_t addr[32]) { - slhdsa_thash_dispatch(config, output, input, slhdsa_wots_len(config), pk_seed, - addr); + const uint8_t *input, + const slhdsa_hash_ctx *hash_ctx, uint8_t addr[32]) { + slhdsa_thash_dispatch(config, output, input, slhdsa_wots_len(config), + hash_ctx, addr); } // Implements H function (Section 4.1, page 11 and Section 11.2, pages 44-46) void bssl::slhdsa_thash_h(const slh_dsa_config *config, uint8_t *output, - const uint8_t *input, const uint8_t *pk_seed, - uint8_t addr[32]) { - slhdsa_thash_dispatch(config, output, input, 2, pk_seed, addr); + const uint8_t *input, + const slhdsa_hash_ctx *hash_ctx, uint8_t addr[32]) { + slhdsa_thash_dispatch(config, output, input, 2, hash_ctx, addr); } // Implements F function (Section 4.1, page 11 and Section 11.2, pages 44-46) void bssl::slhdsa_thash_f(const slh_dsa_config *config, uint8_t *output, - const uint8_t *input, const uint8_t *pk_seed, - uint8_t addr[32]) { - slhdsa_thash_dispatch(config, output, input, 1, pk_seed, addr); + const uint8_t *input, + const slhdsa_hash_ctx *hash_ctx, uint8_t addr[32]) { + slhdsa_thash_dispatch(config, output, input, 1, hash_ctx, addr); } // Implements T_k function for FORS public key compression (Section 4.1, page 11 // and Section 11.2, pages 44-46) void bssl::slhdsa_thash_tk(const slh_dsa_config *config, uint8_t *output, - const uint8_t *input, const uint8_t *pk_seed, - uint8_t addr[32]) { - slhdsa_thash_dispatch(config, output, input, config->fors_trees, pk_seed, + const uint8_t *input, + const slhdsa_hash_ctx *hash_ctx, uint8_t addr[32]) { + slhdsa_thash_dispatch(config, output, input, config->fors_trees, hash_ctx, addr); } diff --git a/third_party/boringssl/src/crypto/fipsmodule/slhdsa/thash.h b/third_party/boringssl/src/crypto/fipsmodule/slhdsa/thash.h index 23a42c6ba..b72e0faed 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/slhdsa/thash.h +++ b/third_party/boringssl/src/crypto/fipsmodule/slhdsa/thash.h @@ -15,11 +15,31 @@ #ifndef OPENSSL_HEADER_CRYPTO_FIPSMODULE_SLHDSA_THASH_H #define OPENSSL_HEADER_CRYPTO_FIPSMODULE_SLHDSA_THASH_H +#include + #include "./params.h" BSSL_NAMESPACE_BEGIN +// slhdsa_hash_ctx holds the public seed, plus precomputed hash state, that is +// shared by every F, H, T_l, and PRF call made under a single key. +struct slhdsa_hash_ctx { + // pk_seed is PK.seed. It must outlive the context. + const uint8_t *pk_seed; + // sha256_seeded is only used for SHA-256 parameter sets. It holds the + // SHA-256 state after absorbing PK.seed || toByte(0, 64 - n), the first + // block of every F, H, T_l, and PRF input (Section 11.2, pages 44-46). + // Computing it once per key saves one compression function call per + // tweakable hash call. + SHA256_CTX sha256_seeded; +}; + +// slhdsa_hash_ctx_init initializes `hash_ctx` for computing tweakable hashes +// with public seed `pk_seed`, which must outlive `hash_ctx`. +void slhdsa_hash_ctx_init(const slh_dsa_config *config, + slhdsa_hash_ctx *hash_ctx, const uint8_t *pk_seed); + // Implements PRF_msg: a pseudo-random function that is used to generate the // randomizer r for the randomized hashing of the message to be signed. // (Section 4.1, page 11) @@ -39,31 +59,31 @@ void slhdsa_thash_hmsg(const slh_dsa_config *config, uint8_t *output, // Implements PRF: a pseudo-random function that is used to generate the secret // values in WOTS+ and FORS private keys. (Section 4.1, page 11) void slhdsa_thash_prf(const slh_dsa_config *config, uint8_t *output, - const uint8_t *pk_seed, const uint8_t *sk_seed, + const slhdsa_hash_ctx *hash_ctx, const uint8_t *sk_seed, uint8_t addr[32]); // Implements T_l: a hash function that maps an l*n-byte message to an n-byte // message. Used for WOTS+ public key compression. (Section 4.1, page 11) void slhdsa_thash_tl(const slh_dsa_config *config, uint8_t *output, - const uint8_t *input, const uint8_t *pk_seed, + const uint8_t *input, const slhdsa_hash_ctx *hash_ctx, uint8_t addr[32]); // Implements H: a hash function that takes a 2*n-byte message as input and // produces an n-byte output. (Section 4.1, page 11) void slhdsa_thash_h(const slh_dsa_config *config, uint8_t *output, - const uint8_t *input, const uint8_t *pk_seed, + const uint8_t *input, const slhdsa_hash_ctx *hash_ctx, uint8_t addr[32]); // Implements F: a hash function that takes an n-byte message as input and // produces an n-byte output. (Section 4.1, page 11) void slhdsa_thash_f(const slh_dsa_config *config, uint8_t *output, - const uint8_t *input, const uint8_t *pk_seed, + const uint8_t *input, const slhdsa_hash_ctx *hash_ctx, uint8_t addr[32]); // Implements T_k: a hash function that maps a k*n-byte message to an n-byte // message. Used for FORS public key compression. (Section 4.1, page 11) void slhdsa_thash_tk(const slh_dsa_config *config, uint8_t *output, - const uint8_t *input, const uint8_t *pk_seed, + const uint8_t *input, const slhdsa_hash_ctx *hash_ctx, uint8_t addr[32]); BSSL_NAMESPACE_END diff --git a/third_party/boringssl/src/crypto/fipsmodule/slhdsa/wots.cc.inc b/third_party/boringssl/src/crypto/fipsmodule/slhdsa/wots.cc.inc index e8bc99009..7a073b9d3 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/slhdsa/wots.cc.inc +++ b/third_party/boringssl/src/crypto/fipsmodule/slhdsa/wots.cc.inc @@ -30,7 +30,7 @@ using namespace bssl; // Implements Algorithm 5: chain function, page 18 static void chain(const slh_dsa_config *config, uint8_t *output, const uint8_t *input, uint32_t start, uint32_t steps, - const uint8_t *pub_seed, uint8_t addr[32]) { + const slhdsa_hash_ctx *hash_ctx, uint8_t addr[32]) { assert(start < config->wots_w); assert(steps < config->wots_w); @@ -38,25 +38,26 @@ static void chain(const slh_dsa_config *config, uint8_t *output, for (uint32_t i = start; i < (start + steps) && i < config->wots_w; ++i) { slhdsa_set_hash_addr(config, addr, i); - slhdsa_thash_f(config, output, output, pub_seed, addr); + slhdsa_thash_f(config, output, output, hash_ctx, addr); } } static void slhdsa_wots_do_chain(const slh_dsa_config *config, uint8_t *out, uint8_t sk_addr[32], uint8_t addr[32], uint8_t value, const uint8_t *sk_seed, - const uint8_t *pub_seed, + const slhdsa_hash_ctx *hash_ctx, uint32_t chain_index) { uint8_t tmp_sk[SLHDSA_MAX_N]; slhdsa_set_chain_addr(config, sk_addr, chain_index); - slhdsa_thash_prf(config, tmp_sk, pub_seed, sk_seed, sk_addr); + slhdsa_thash_prf(config, tmp_sk, hash_ctx, sk_seed, sk_addr); slhdsa_set_chain_addr(config, addr, chain_index); - chain(config, out, tmp_sk, 0, value, pub_seed, addr); + chain(config, out, tmp_sk, 0, value, hash_ctx, addr); } // Implements Algorithm 6: wots_pkGen function, page 18 void bssl::slhdsa_wots_pk_gen(const slh_dsa_config *config, uint8_t *pk, - const uint8_t *sk_seed, const uint8_t *pub_seed, + const uint8_t *sk_seed, + const slhdsa_hash_ctx *hash_ctx, uint8_t addr[32]) { uint8_t wots_pk_addr[32], sk_addr[32]; OPENSSL_memcpy(wots_pk_addr, addr, sizeof(wots_pk_addr)); @@ -70,19 +71,19 @@ void bssl::slhdsa_wots_pk_gen(const slh_dsa_config *config, uint8_t *pk, const uint32_t max_chain = config->wots_w - 1; for (uint32_t i = 0; i < wots_len; ++i) { slhdsa_wots_do_chain(config, tmp + i * n, sk_addr, addr, max_chain, sk_seed, - pub_seed, i); + hash_ctx, i); } // Compress pk slhdsa_set_type(config, wots_pk_addr, SLHDSA_ADDR_TYPE_WOTSPK); slhdsa_copy_keypair_addr(config, wots_pk_addr, addr); - slhdsa_thash_tl(config, pk, tmp, pub_seed, wots_pk_addr); + slhdsa_thash_tl(config, pk, tmp, hash_ctx, wots_pk_addr); } // Implements Algorithm 7: wots_sign function, page 20 void bssl::slhdsa_wots_sign(const slh_dsa_config *config, uint8_t *sig, const uint8_t *msg, const uint8_t *sk_seed, - const uint8_t *pub_seed, uint8_t addr[32]) { + const slhdsa_hash_ctx *hash_ctx, uint8_t addr[32]) { BSSL_CHECK(config->wots_w == 16); const size_t n = config->n; const uint32_t max_chain = config->wots_w - 1; @@ -102,40 +103,41 @@ void bssl::slhdsa_wots_sign(const slh_dsa_config *config, uint8_t *sig, uint32_t chain_index = 0; for (size_t i = 0; i < n; ++i) { slhdsa_wots_do_chain(config, sig, sk_addr, addr, msg[i] >> 4, sk_seed, - pub_seed, chain_index++); + hash_ctx, chain_index++); sig += n; slhdsa_wots_do_chain(config, sig, sk_addr, addr, msg[i] & 15, sk_seed, - pub_seed, chain_index++); + hash_ctx, chain_index++); sig += n; } // Include the WOTS checksum values (len2 = 3 for the parameter sets we // support). slhdsa_wots_do_chain(config, sig, sk_addr, addr, (csum >> 8) & 15, sk_seed, - pub_seed, chain_index++); + hash_ctx, chain_index++); sig += n; slhdsa_wots_do_chain(config, sig, sk_addr, addr, (csum >> 4) & 15, sk_seed, - pub_seed, chain_index++); + hash_ctx, chain_index++); sig += n; - slhdsa_wots_do_chain(config, sig, sk_addr, addr, csum & 15, sk_seed, pub_seed, + slhdsa_wots_do_chain(config, sig, sk_addr, addr, csum & 15, sk_seed, hash_ctx, chain_index++); } static void slhdsa_wots_pk_from_sig_do_chain(const slh_dsa_config *config, uint8_t *out, uint8_t addr[32], const uint8_t *in, uint8_t value, - const uint8_t *pub_seed, + const slhdsa_hash_ctx *hash_ctx, uint32_t chain_index) { slhdsa_set_chain_addr(config, addr, chain_index); chain(config, out + chain_index * config->n, in + chain_index * config->n, - value, (config->wots_w - 1) - value, pub_seed, addr); + value, (config->wots_w - 1) - value, hash_ctx, addr); } // Implements Algorithm 8: wots_pkFromSig function, page 21 void bssl::slhdsa_wots_pk_from_sig(const slh_dsa_config *config, uint8_t *pk, const uint8_t *sig, const uint8_t *msg, - const uint8_t *pub_seed, uint8_t addr[32]) { + const slhdsa_hash_ctx *hash_ctx, + uint8_t addr[32]) { BSSL_CHECK(config->wots_w == 16); const size_t n = config->n; const uint32_t max_chain = config->wots_w - 1; @@ -153,20 +155,20 @@ void bssl::slhdsa_wots_pk_from_sig(const slh_dsa_config *config, uint8_t *pk, uint32_t chain_index = 0; for (size_t i = 0; i < n; ++i) { slhdsa_wots_pk_from_sig_do_chain(config, tmp, addr, sig, msg[i] >> 4, - pub_seed, chain_index++); + hash_ctx, chain_index++); slhdsa_wots_pk_from_sig_do_chain(config, tmp, addr, sig, msg[i] & 15, - pub_seed, chain_index++); + hash_ctx, chain_index++); } - slhdsa_wots_pk_from_sig_do_chain(config, tmp, addr, sig, csum >> 8, pub_seed, + slhdsa_wots_pk_from_sig_do_chain(config, tmp, addr, sig, csum >> 8, hash_ctx, chain_index++); slhdsa_wots_pk_from_sig_do_chain(config, tmp, addr, sig, (csum >> 4) & 15, - pub_seed, chain_index++); - slhdsa_wots_pk_from_sig_do_chain(config, tmp, addr, sig, csum & 15, pub_seed, + hash_ctx, chain_index++); + slhdsa_wots_pk_from_sig_do_chain(config, tmp, addr, sig, csum & 15, hash_ctx, chain_index++); // Compress pk slhdsa_set_type(config, wots_pk_addr, SLHDSA_ADDR_TYPE_WOTSPK); slhdsa_copy_keypair_addr(config, wots_pk_addr, addr); - slhdsa_thash_tl(config, pk, tmp, pub_seed, wots_pk_addr); + slhdsa_thash_tl(config, pk, tmp, hash_ctx, wots_pk_addr); } diff --git a/third_party/boringssl/src/crypto/fipsmodule/slhdsa/wots.h b/third_party/boringssl/src/crypto/fipsmodule/slhdsa/wots.h index 28afeb057..a41f26c92 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/slhdsa/wots.h +++ b/third_party/boringssl/src/crypto/fipsmodule/slhdsa/wots.h @@ -16,24 +16,25 @@ #define OPENSSL_HEADER_CRYPTO_FIPSMODULE_SLHDSA_WOTS_H #include "./params.h" +#include "./thash.h" BSSL_NAMESPACE_BEGIN // Implements Algorithm 6: wots_pkGen function, page 18 void slhdsa_wots_pk_gen(const slh_dsa_config *config, uint8_t *pk, - const uint8_t *sk_seed, const uint8_t *pub_seed, + const uint8_t *sk_seed, const slhdsa_hash_ctx *hash_ctx, uint8_t addr[32]); // Implements Algorithm 7: wots_sign function, page 20 void slhdsa_wots_sign(const slh_dsa_config *config, uint8_t *sig, const uint8_t *msg, const uint8_t *sk_seed, - const uint8_t *pub_seed, uint8_t addr[32]); + const slhdsa_hash_ctx *hash_ctx, uint8_t addr[32]); // Implements Algorithm 8: wots_pkFromSig function, page 21 void slhdsa_wots_pk_from_sig(const slh_dsa_config *config, uint8_t *pk, const uint8_t *sig, const uint8_t *msg, - const uint8_t *pub_seed, uint8_t addr[32]); + const slhdsa_hash_ctx *hash_ctx, uint8_t addr[32]); BSSL_NAMESPACE_END diff --git a/third_party/boringssl/src/crypto/fipsmodule/tls/kdf.cc.inc b/third_party/boringssl/src/crypto/fipsmodule/tls/kdf.cc.inc index 2446a36bb..350cd0608 100644 --- a/third_party/boringssl/src/crypto/fipsmodule/tls/kdf.cc.inc +++ b/third_party/boringssl/src/crypto/fipsmodule/tls/kdf.cc.inc @@ -29,8 +29,8 @@ using namespace bssl; // tls1_P_hash computes the TLS P_ function as described in RFC 5246, -// section 5. It XORs |out_len| bytes to |out|, using |md| as the hash and -// |secret| as the secret. |label|, |seed1|, and |seed2| are concatenated to +// section 5. It XORs `out_len` bytes to `out`, using `md` as the hash and +// `secret` as the secret. `label`, `seed1`, and `seed2` are concatenated to // form the seed parameter. It returns true on success and false on failure. static int tls1_P_hash(uint8_t *out, size_t out_len, const EVP_MD *md, const uint8_t *secret, size_t secret_len, @@ -60,7 +60,7 @@ static int tls1_P_hash(uint8_t *out, size_t out_len, const EVP_MD *md, unsigned len_u; uint8_t hmac[EVP_MAX_MD_SIZE]; if (!HMAC_CTX_copy_ex(&ctx, &ctx_init) || !HMAC_Update(&ctx, A1, A1_len) || - // Save a copy of |ctx| to compute the next A1 value below. + // Save a copy of `ctx` to compute the next A1 value below. (out_len > chunk && !HMAC_CTX_copy_ex(&ctx_tmp, &ctx)) || !HMAC_Update(&ctx, (const uint8_t *)label, label_len) || !HMAC_Update(&ctx, seed1, seed1_len) || @@ -71,7 +71,7 @@ static int tls1_P_hash(uint8_t *out, size_t out_len, const EVP_MD *md, size_t len = len_u; assert(len == chunk); - // XOR the result into |out|. + // XOR the result into `out`. if (len > out_len) { len = out_len; } @@ -117,14 +117,14 @@ int CRYPTO_tls1_prf(const EVP_MD *digest, uint8_t *out, size_t out_len, int ret = 0; if (digest == EVP_md5_sha1()) { - // If using the MD5/SHA1 PRF, |secret| is partitioned between MD5 and SHA-1. + // If using the MD5/SHA1 PRF, `secret` is partitioned between MD5 and SHA-1. size_t secret_half = secret_len - (secret_len / 2); if (!tls1_P_hash(out, out_len, EVP_md5(), secret, secret_half, label, label_len, seed1, seed1_len, seed2, seed2_len)) { goto end; } - // Note that, if |secret_len| is odd, the two halves share a byte. + // Note that, if `secret_len` is odd, the two halves share a byte. secret += secret_len - secret_half; secret_len = secret_half; digest = EVP_sha1(); diff --git a/third_party/boringssl/src/crypto/hpke/hpke.cc b/third_party/boringssl/src/crypto/hpke/hpke.cc index 2365a53b1..70487fa65 100644 --- a/third_party/boringssl/src/crypto/hpke/hpke.cc +++ b/third_party/boringssl/src/crypto/hpke/hpke.cc @@ -18,6 +18,7 @@ #include #include +#include #include #include @@ -25,17 +26,18 @@ #include #include #include +#include #include +#include #include #include #include #include -#include #include #include #include -#include "../fipsmodule/bcm_interface.h" +#include "../evp/internal.h" #include "../fipsmodule/ec/internal.h" #include "../fipsmodule/keccak/internal.h" #include "../internal.h" @@ -46,40 +48,86 @@ using namespace bssl; // This file implements RFC 9180. -#define MAX_SEED_LEN XWING_SEED_LEN #define MAX_SHARED_SECRET_LEN SHA256_DIGEST_LENGTH +#define MAX_DHKEM_DH_SHARED_KEY_LEN X25519_SHARED_KEY_LEN +#define MAX_DHKEM_PUBLIC_KEY_LEN 65 // kP256PublicValueLen -// TODO(chlily): Fold this into `EVP_KEM`. -struct evp_hpke_kem_st { - uint16_t id; +namespace { + +// HpkeKeyMethod implements the key-type-specific operations that HPKE performs +// on keys of a particular `EVP_PKEY_ALG` type. +struct HpkeKeyMethod { + // hpke_kem_id is the ID of the KEM in RFC 9180. + uint16_t hpke_kem_id; + + // public_key_len corresponds to Npk in RFC 9180. size_t public_key_len; + // private_key_len corresponds to Nsk in RFC 9180. size_t private_key_len; - size_t seed_len; - size_t enc_len; - int (*init_key)(EVP_HPKE_KEY *key, const uint8_t *priv_key, - size_t priv_key_len); - int (*generate_key)(EVP_HPKE_KEY *key); - int (*derive_key)(EVP_HPKE_KEY *key, Span ikm); - int (*encap_with_seed)(const EVP_HPKE_KEM *kem, uint8_t *out_shared_secret, - size_t *out_shared_secret_len, uint8_t *out_enc, - size_t *out_enc_len, size_t max_enc, - const uint8_t *peer_public_key, - size_t peer_public_key_len, const uint8_t *seed, - size_t seed_len); - int (*decap)(const EVP_HPKE_KEY *key, uint8_t *out_shared_secret, - size_t *out_shared_secret_len, const uint8_t *enc, - size_t enc_len); - int (*auth_encap_with_seed)(const EVP_HPKE_KEY *key, - uint8_t *out_shared_secret, - size_t *out_shared_secret_len, uint8_t *out_enc, - size_t *out_enc_len, size_t max_enc, - const uint8_t *peer_public_key, - size_t peer_public_key_len, const uint8_t *seed, - size_t seed_len); - int (*auth_decap)(const EVP_HPKE_KEY *key, uint8_t *out_shared_secret, - size_t *out_shared_secret_len, const uint8_t *enc, - size_t enc_len, const uint8_t *peer_public_key, - size_t peer_public_key_len); + + const EVP_PKEY_ALG *(*alg_func)(); + + // deserialize_private_key and serialize_private_key implement the + // DeserializePrivateKey and SerializePrivateKey operations for the key type + // from section 4 of RFC 9180, using the same format accepted by + // `EVP_HPKE_KEY_init`. + EVP_PKEY *(*deserialize_private_key)(const EVP_PKEY_ALG *alg, + const uint8_t *in, size_t len); + int (*serialize_private_key)(const EVP_PKEY *pkey, uint8_t *out, + size_t *out_len); + + // deserialize_public_key and serialize_public_key implement the + // DeserializePublicKey and SerializePublicKey operations from section 4 of + // RFC 9180. + EVP_PKEY *(*deserialize_public_key)(const EVP_PKEY_ALG *alg, + const uint8_t *in, size_t len); + int (*serialize_public_key)(const EVP_PKEY *pkey, uint8_t *out, + size_t *out_len); + + // derive_key_pair implements the DeriveKeyPair operation from section 4 of + // RFC 9180. + EVP_PKEY *(*derive_key_pair)(const EVP_PKEY_ALG *alg, uint16_t kem_suite_id, + Span ikm); +}; + +// EVP_KEM_DHKEM implements the DHKEM construction from RFC 9180 based on a +// DH group and an HKDF. +// +// In DHKEM, the encapsulated key is the serialized ephemeral public key, so +// Nenc (`ciphertext_len`) equals Npk (`public_key_len`). +struct EVP_KEM_DHKEM : public EVP_KEM { + // dh_shared_key_len corresponds to Ndh in RFC 9180. + size_t dh_shared_key_len; + + const HpkeKeyMethod *key_method; + const EVP_MD *(*hkdf_digest_func)(); + + // Generates the ephemeral encapsulation key from external entropy bytes + // provided as a "seed" for testing. The seed's format is variant-specific. + EVP_PKEY *(*key_from_encap_entropy)(const EVP_PKEY_ALG *alg, + const uint8_t *in, size_t len); +}; + +} // namespace + +struct evp_hpke_kem_st { + uint16_t id; + + const EVP_KEM *(*evp_kem_func)(); + const HpkeKeyMethod *key_method; + + int (*auth_encap)(const EVP_KEM *kem, Span out_ciphertext, + Span out_secret, const EVP_PKEY *peer_key, + const EVP_PKEY *auth_key); + int (*auth_encap_external_entropy)(const EVP_KEM *kem, + Span out_ciphertext, + Span out_secret, + const EVP_PKEY *peer_key, + const EVP_PKEY *auth_key, + Span entropy); + int (*auth_decap)(const EVP_KEM *kem, Span out_secret, + Span ciphertext, const EVP_PKEY *key, + const EVP_PKEY *auth_key); }; struct evp_hpke_kdf_st { @@ -93,20 +141,21 @@ struct evp_hpke_aead_st { const EVP_AEAD *(*aead_func)(); }; +namespace { // Low-level labeled KDF functions. -static const char kHpkeVersionId[] = "HPKE-v1"; +const char kHpkeVersionId[] = "HPKE-v1"; -static int add_label_string(CBB *cbb, const char *label) { +int add_label_string(CBB *cbb, const char *label) { return CBB_add_bytes(cbb, (const uint8_t *)label, strlen(label)); } -static int hpke_labeled_extract(const EVP_MD *hkdf_md, uint8_t *out_key, - size_t *out_len, const uint8_t *salt, - size_t salt_len, const uint8_t *suite_id, - size_t suite_id_len, const char *label, - const uint8_t *ikm, size_t ikm_len) { +int hpke_labeled_extract(const EVP_MD *hkdf_md, uint8_t *out_key, + size_t *out_len, const uint8_t *salt, size_t salt_len, + const uint8_t *suite_id, size_t suite_id_len, + const char *label, const uint8_t *ikm, + size_t ikm_len) { // labeledIKM = concat("HPKE-v1", suite_id, label, IKM) CBB labeled_ikm; int ok = CBB_init(&labeled_ikm, 0) && @@ -120,11 +169,11 @@ static int hpke_labeled_extract(const EVP_MD *hkdf_md, uint8_t *out_key, return ok; } -static int hpke_labeled_expand(const EVP_MD *hkdf_md, uint8_t *out_key, - size_t out_len, const uint8_t *prk, - size_t prk_len, const uint8_t *suite_id, - size_t suite_id_len, const char *label, - const uint8_t *info, size_t info_len) { +int hpke_labeled_expand(const EVP_MD *hkdf_md, uint8_t *out_key, size_t out_len, + const uint8_t *prk, size_t prk_len, + const uint8_t *suite_id, size_t suite_id_len, + const char *label, const uint8_t *info, + size_t info_len) { // labeledInfo = concat(I2OSP(L, 2), "HPKE-v1", suite_id, label, info) CBB labeled_info; int ok = CBB_init(&labeled_info, 0) && // @@ -139,17 +188,16 @@ static int hpke_labeled_expand(const EVP_MD *hkdf_md, uint8_t *out_key, return ok; } -static void absorb_u16(BORINGSSL_keccak_st *ctx, uint16_t n) { +void absorb_u16(BORINGSSL_keccak_st *ctx, uint16_t n) { uint8_t bytes[2]; CRYPTO_store_u16_be(bytes, n); BORINGSSL_keccak_absorb(ctx, bytes, sizeof(bytes)); } -static void hpke_shake256_labeled_derive(Span out, - Span ikm, - Span suite_id, - std::string_view label, - Span context) { +void hpke_shake256_labeled_derive(Span out, Span ikm, + Span suite_id, + std::string_view label, + Span context) { // https://www.ietf.org/archive/id/draft-ietf-hpke-hpke-03.html#name-labeled-derivation-function // https://www.ietf.org/archive/id/draft-ietf-hpke-pq-04.html#name-single-stage-kdfs BORINGSSL_keccak_st ctx; @@ -171,7 +219,7 @@ static void hpke_shake256_labeled_derive(Span out, // KEM implementations. -static std::array hpke_kem_suite_id(uint16_t kem_id) { +std::array hpke_kem_suite_id(uint16_t kem_id) { // concat("KEM", I2OSP(kem_id, 2)) return {'K', 'E', 'M', static_cast(kem_id >> 8), static_cast(kem_id)}; @@ -179,11 +227,11 @@ static std::array hpke_kem_suite_id(uint16_t kem_id) { // dhkem_extract_and_expand implements the ExtractAndExpand operation in the // DHKEM construction. See section 4.1 of RFC 9180. -static int dhkem_extract_and_expand(uint16_t kem_id, const EVP_MD *hkdf_md, - uint8_t *out_key, size_t out_len, - const uint8_t *dh, size_t dh_len, - const uint8_t *kem_context, - size_t kem_context_len) { +int dhkem_extract_and_expand(uint16_t kem_id, const EVP_MD *hkdf_md, + uint8_t *out_key, size_t out_len, + const uint8_t *dh, size_t dh_len, + const uint8_t *kem_context, + size_t kem_context_len) { auto suite_id = hpke_kem_suite_id(kem_id); uint8_t prk[EVP_MAX_MD_SIZE]; size_t prk_len; @@ -195,786 +243,600 @@ static int dhkem_extract_and_expand(uint16_t kem_id, const EVP_MD *hkdf_md, kem_context, kem_context_len); } -static int x25519_init_key(EVP_HPKE_KEY *key, const uint8_t *priv_key, - size_t priv_key_len) { - if (priv_key_len != X25519_PRIVATE_KEY_LEN) { - OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR); - return 0; +bool dhkem_dh(const EVP_KEM_DHKEM *dhkem, Span out, + const EVP_PKEY *key, const EVP_PKEY *peer) { + assert(out.size() == dhkem->dh_shared_key_len); + UniquePtr pkctx(EVP_PKEY_CTX_new(key, nullptr)); + if (pkctx == nullptr) { + return false; } - - OPENSSL_memcpy(key->private_key, priv_key, priv_key_len); - X25519_public_from_private(key->public_key, priv_key); - return 1; + size_t out_len = out.size(); + return EVP_PKEY_derive_init(pkctx.get()) && + EVP_PKEY_derive_set_peer(pkctx.get(), peer) && + EVP_PKEY_derive(pkctx.get(), out.data(), &out_len) && + out_len == dhkem->dh_shared_key_len; } -static int x25519_generate_key(EVP_HPKE_KEY *key) { - X25519_keypair(key->public_key, key->private_key); - return 1; -} - -static int x25519_derive_key(EVP_HPKE_KEY *key, Span ikm) { - // https://www.rfc-editor.org/rfc/rfc9180.html#name-derivekeypair - auto suite_id = hpke_kem_suite_id(EVP_HPKE_DHKEM_X25519_HKDF_SHA256); - uint8_t dkp_prk[SHA256_DIGEST_LENGTH]; - size_t dkp_prk_len; - uint8_t sk[32]; - if (!hpke_labeled_extract(EVP_sha256(), dkp_prk, &dkp_prk_len, nullptr, 0, - suite_id.data(), suite_id.size(), "dkp_prk", - ikm.data(), ikm.size()) || - !hpke_labeled_expand(EVP_sha256(), sk, sizeof(sk), dkp_prk, dkp_prk_len, - suite_id.data(), suite_id.size(), "sk", - /*info=*/nullptr, /*info_len=*/0)) { - return 0; - } - return x25519_init_key(key, sk, sizeof(sk)); -} - -static int x25519_encap_with_seed( - const EVP_HPKE_KEM *kem, uint8_t *out_shared_secret, - size_t *out_shared_secret_len, uint8_t *out_enc, size_t *out_enc_len, - size_t max_enc, const uint8_t *peer_public_key, size_t peer_public_key_len, - const uint8_t *seed, size_t seed_len) { - if (max_enc < X25519_PUBLIC_VALUE_LEN) { +bool dhkem_serialize_public_key(const EVP_KEM_DHKEM *dhkem, Span out, + const EVP_PKEY *key) { + if (out.size() != dhkem->key_method->public_key_len) { OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_BUFFER_SIZE); - return 0; + return false; } - if (seed_len != X25519_PRIVATE_KEY_LEN) { - OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR); - return 0; + size_t serialized_len = out.size(); + if (!dhkem->key_method->serialize_public_key(key, out.data(), + &serialized_len)) { + return false; } - X25519_public_from_private(out_enc, seed); - - uint8_t dh[X25519_SHARED_KEY_LEN]; - if (peer_public_key_len != X25519_PUBLIC_VALUE_LEN || - !X25519(dh, seed, peer_public_key)) { - OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_PEER_KEY); - return 0; - } - - uint8_t kem_context[2 * X25519_PUBLIC_VALUE_LEN]; - OPENSSL_memcpy(kem_context, out_enc, X25519_PUBLIC_VALUE_LEN); - OPENSSL_memcpy(kem_context + X25519_PUBLIC_VALUE_LEN, peer_public_key, - X25519_PUBLIC_VALUE_LEN); - if (!dhkem_extract_and_expand(kem->id, EVP_sha256(), out_shared_secret, - SHA256_DIGEST_LENGTH, dh, sizeof(dh), - kem_context, sizeof(kem_context))) { - return 0; - } - - *out_enc_len = X25519_PUBLIC_VALUE_LEN; - *out_shared_secret_len = SHA256_DIGEST_LENGTH; - return 1; + BSSL_CHECK(serialized_len == out.size()); + return true; } -static int x25519_decap(const EVP_HPKE_KEY *key, uint8_t *out_shared_secret, - size_t *out_shared_secret_len, const uint8_t *enc, - size_t enc_len) { - uint8_t dh[X25519_SHARED_KEY_LEN]; - if (enc_len != X25519_PUBLIC_VALUE_LEN || - !X25519(dh, key->private_key, enc)) { - OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_PEER_KEY); - return 0; - } - - uint8_t kem_context[2 * X25519_PUBLIC_VALUE_LEN]; - OPENSSL_memcpy(kem_context, enc, X25519_PUBLIC_VALUE_LEN); - OPENSSL_memcpy(kem_context + X25519_PUBLIC_VALUE_LEN, key->public_key, - X25519_PUBLIC_VALUE_LEN); - if (!dhkem_extract_and_expand(key->kem->id, EVP_sha256(), out_shared_secret, - SHA256_DIGEST_LENGTH, dh, sizeof(dh), - kem_context, sizeof(kem_context))) { - return 0; +bool dhkem_serialize_public_keys(const EVP_KEM_DHKEM *dhkem, Span out, + const EVP_PKEY *key1, const EVP_PKEY *key2) { + const size_t public_len = dhkem->key_method->public_key_len; + if (out.size() != 2 * dhkem->key_method->public_key_len) { + OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_BUFFER_SIZE); + return false; } - - *out_shared_secret_len = SHA256_DIGEST_LENGTH; - return 1; + return dhkem_serialize_public_key(dhkem, out.subspan(0, public_len), key1) && + dhkem_serialize_public_key(dhkem, out.subspan(public_len), key2); } -static int x25519_auth_encap_with_seed( - const EVP_HPKE_KEY *key, uint8_t *out_shared_secret, - size_t *out_shared_secret_len, uint8_t *out_enc, size_t *out_enc_len, - size_t max_enc, const uint8_t *peer_public_key, size_t peer_public_key_len, - const uint8_t *seed, size_t seed_len) { - if (max_enc < X25519_PUBLIC_VALUE_LEN) { +bool dhkem_serialize_public_keys(const EVP_KEM_DHKEM *dhkem, Span out, + const EVP_PKEY *key1, const EVP_PKEY *key2, + const EVP_PKEY *key3) { + const size_t public_len = dhkem->key_method->public_key_len; + if (out.size() != 3 * public_len) { OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_BUFFER_SIZE); - return 0; - } - if (seed_len != X25519_PRIVATE_KEY_LEN) { - OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR); - return 0; - } - X25519_public_from_private(out_enc, seed); - - uint8_t dh[2 * X25519_SHARED_KEY_LEN]; - if (peer_public_key_len != X25519_PUBLIC_VALUE_LEN || - !X25519(dh, seed, peer_public_key) || - !X25519(dh + X25519_SHARED_KEY_LEN, key->private_key, peer_public_key)) { - OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_PEER_KEY); - return 0; - } - - uint8_t kem_context[3 * X25519_PUBLIC_VALUE_LEN]; - OPENSSL_memcpy(kem_context, out_enc, X25519_PUBLIC_VALUE_LEN); - OPENSSL_memcpy(kem_context + X25519_PUBLIC_VALUE_LEN, peer_public_key, - X25519_PUBLIC_VALUE_LEN); - OPENSSL_memcpy(kem_context + 2 * X25519_PUBLIC_VALUE_LEN, key->public_key, - X25519_PUBLIC_VALUE_LEN); - if (!dhkem_extract_and_expand(key->kem->id, EVP_sha256(), out_shared_secret, - SHA256_DIGEST_LENGTH, dh, sizeof(dh), - kem_context, sizeof(kem_context))) { - return 0; + return false; } - - *out_enc_len = X25519_PUBLIC_VALUE_LEN; - *out_shared_secret_len = SHA256_DIGEST_LENGTH; - return 1; + return dhkem_serialize_public_key(dhkem, out.subspan(0, public_len), key1) && + dhkem_serialize_public_keys(dhkem, out.subspan(public_len), key2, + key3); } -static int x25519_auth_decap(const EVP_HPKE_KEY *key, - uint8_t *out_shared_secret, - size_t *out_shared_secret_len, const uint8_t *enc, - size_t enc_len, const uint8_t *peer_public_key, - size_t peer_public_key_len) { - uint8_t dh[2 * X25519_SHARED_KEY_LEN]; - if (enc_len != X25519_PUBLIC_VALUE_LEN || - peer_public_key_len != X25519_PUBLIC_VALUE_LEN || - !X25519(dh, key->private_key, enc) || - !X25519(dh + X25519_SHARED_KEY_LEN, key->private_key, peer_public_key)) { - OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_PEER_KEY); +int dhkem_encap_with_ephemeral_key(const EVP_KEM_DHKEM *dhkem, + Span out_ciphertext, + Span out_secret, + const EVP_PKEY *peer_key, + const EVP_PKEY *ephemeral_key) { + const size_t public_len = dhkem->key_method->public_key_len; + if (out_ciphertext.size() != public_len) { + OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_CIPHERTEXT_LENGTH); return 0; } - - uint8_t kem_context[3 * X25519_PUBLIC_VALUE_LEN]; - OPENSSL_memcpy(kem_context, enc, X25519_PUBLIC_VALUE_LEN); - OPENSSL_memcpy(kem_context + X25519_PUBLIC_VALUE_LEN, key->public_key, - X25519_PUBLIC_VALUE_LEN); - OPENSSL_memcpy(kem_context + 2 * X25519_PUBLIC_VALUE_LEN, peer_public_key, - X25519_PUBLIC_VALUE_LEN); - if (!dhkem_extract_and_expand(key->kem->id, EVP_sha256(), out_shared_secret, - SHA256_DIGEST_LENGTH, dh, sizeof(dh), - kem_context, sizeof(kem_context))) { + if (out_secret.size() != dhkem->secret_len) { + OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_SECRET_LENGTH); return 0; } - - *out_shared_secret_len = SHA256_DIGEST_LENGTH; - return 1; -} - -const EVP_HPKE_KEM *EVP_hpke_x25519_hkdf_sha256() { - static const EVP_HPKE_KEM kKEM = { - /*id=*/EVP_HPKE_DHKEM_X25519_HKDF_SHA256, - /*public_key_len=*/X25519_PUBLIC_VALUE_LEN, - /*private_key_len=*/X25519_PRIVATE_KEY_LEN, - /*seed_len=*/X25519_PRIVATE_KEY_LEN, - /*enc_len=*/X25519_PUBLIC_VALUE_LEN, - x25519_init_key, - x25519_generate_key, - x25519_derive_key, - x25519_encap_with_seed, - x25519_decap, - x25519_auth_encap_with_seed, - x25519_auth_decap, - }; - return &kKEM; -} - -#define P256_PRIVATE_KEY_LEN 32 -#define P256_PUBLIC_KEY_LEN 65 -#define P256_PUBLIC_VALUE_LEN 65 -#define P256_SEED_LEN 32 -#define P256_SHARED_KEY_LEN 32 - -static int p256_public_from_private(uint8_t out_pub[P256_PUBLIC_VALUE_LEN], - const uint8_t priv[P256_PRIVATE_KEY_LEN]) { - const EC_GROUP *const group = EC_group_p256(); - const uint8_t kAllZeros[P256_PRIVATE_KEY_LEN] = {0}; - EC_SCALAR private_scalar; - EC_JACOBIAN public_point; - EC_AFFINE public_point_affine; - - if (CRYPTO_memcmp(kAllZeros, priv, sizeof(kAllZeros)) == 0) { - OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR); + // dh = DH(skE, pkR) + InplaceVector dh; + if (!dh.TryResizeForOverwrite(dhkem->dh_shared_key_len) || + !dhkem_dh(dhkem, Span(dh), ephemeral_key, peer_key)) { return 0; } - - if (!ec_scalar_from_bytes(group, &private_scalar, priv, - P256_PRIVATE_KEY_LEN) || - !ec_point_mul_scalar_base(group, &public_point, &private_scalar) || - !ec_jacobian_to_affine(group, &public_point_affine, &public_point)) { + // enc = SerializePublicKey(pkE) + // pkRm = SerializePublicKey(pkR) + // kem_context = concat(enc, pkRm) + InplaceVector kem_context; + if (!kem_context.TryResizeForOverwrite(2 * public_len) || + !dhkem_serialize_public_keys(dhkem, Span(kem_context), ephemeral_key, + peer_key)) { return 0; } - - size_t out_len_x, out_len_y; - out_pub[0] = POINT_CONVERSION_UNCOMPRESSED; - ec_felem_to_bytes(group, &out_pub[1], &out_len_x, &public_point_affine.X); - ec_felem_to_bytes(group, &out_pub[33], &out_len_y, &public_point_affine.Y); - return 1; + OPENSSL_memcpy(out_ciphertext.data(), kem_context.data(), public_len); + // shared_secret = ExtractAndExpand(dh, kem_context) + return dhkem_extract_and_expand(dhkem->key_method->hpke_kem_id, + dhkem->hkdf_digest_func(), out_secret.data(), + out_secret.size(), dh.data(), dh.size(), + kem_context.data(), kem_context.size()); } -static int p256_init_key(EVP_HPKE_KEY *key, const uint8_t *priv_key, - size_t priv_key_len) { - if (priv_key_len != P256_PRIVATE_KEY_LEN) { - OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR); +int dhkem_encap(const EVP_KEM *kem, Span out_ciphertext, + Span out_secret, const EVP_PKEY *peer_key) { + const auto *dhkem = static_cast(kem); + UniquePtr ephemeral_key( + EVP_PKEY_generate_from_alg(dhkem->key_method->alg_func())); + if (ephemeral_key == nullptr) { return 0; } + return dhkem_encap_with_ephemeral_key(dhkem, out_ciphertext, out_secret, + peer_key, ephemeral_key.get()); +} - if (!p256_public_from_private(key->public_key, priv_key)) { +int dhkem_encap_external_entropy(const EVP_KEM *kem, + Span out_ciphertext, + Span out_secret, + const EVP_PKEY *peer_key, + Span entropy) { + const auto *dhkem = static_cast(kem); + UniquePtr ephemeral_key(dhkem->key_from_encap_entropy( + dhkem->key_method->alg_func(), entropy.data(), entropy.size())); + if (ephemeral_key == nullptr) { return 0; } - - OPENSSL_memcpy(key->private_key, priv_key, priv_key_len); - return 1; + return dhkem_encap_with_ephemeral_key(dhkem, out_ciphertext, out_secret, + peer_key, ephemeral_key.get()); } -static int p256_private_key_from_seed(uint8_t out_priv[P256_PRIVATE_KEY_LEN], - const uint8_t *seed, size_t seed_len) { - // https://www.rfc-editor.org/rfc/rfc9180.html#name-derivekeypair - auto suite_id = hpke_kem_suite_id(EVP_HPKE_DHKEM_P256_HKDF_SHA256); - uint8_t dkp_prk[32]; - size_t dkp_prk_len; - if (!hpke_labeled_extract(EVP_sha256(), dkp_prk, &dkp_prk_len, nullptr, 0, - suite_id.data(), suite_id.size(), "dkp_prk", seed, - seed_len)) { +int dhkem_decap(const EVP_KEM *kem, Span out_secret, + Span ciphertext, const EVP_PKEY *key) { + const auto *dhkem = static_cast(kem); + const size_t public_len = dhkem->key_method->public_key_len; + if (ciphertext.size() != public_len) { + OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_CIPHERTEXT_LENGTH); return 0; } - assert(dkp_prk_len == sizeof(dkp_prk)); - - const EC_GROUP *const group = EC_group_p256(); - EC_SCALAR private_scalar; - - for (unsigned counter = 0; counter < 256; counter++) { - const uint8_t counter_byte = counter & 0xff; - if (!hpke_labeled_expand(EVP_sha256(), out_priv, P256_PRIVATE_KEY_LEN, - dkp_prk, sizeof(dkp_prk), suite_id.data(), - suite_id.size(), "candidate", &counter_byte, - sizeof(counter_byte))) { - return 0; - } - - // |ec_scalar_from_bytes| checks that the scalar is less than the order. - if (ec_scalar_from_bytes(group, &private_scalar, out_priv, - P256_PRIVATE_KEY_LEN) && - !ec_scalar_is_zero(group, &private_scalar)) { - return 1; - } + if (out_secret.size() != dhkem->secret_len) { + OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_SECRET_LENGTH); + return 0; } - - // This happens with probability of 2^-(32*256). - OPENSSL_PUT_ERROR(EVP, ERR_R_INTERNAL_ERROR); - return 0; -} - -static int p256_derive_key(EVP_HPKE_KEY *key, Span ikm) { - if (!p256_private_key_from_seed(key->private_key, ikm.data(), ikm.size()) || - !p256_public_from_private(key->public_key, key->private_key)) { + // pkE = DeserializePublicKey(enc) + UniquePtr ephemeral_key(dhkem->key_method->deserialize_public_key( + dhkem->key_method->alg_func(), ciphertext.data(), ciphertext.size())); + if (ephemeral_key == nullptr) { return 0; } - return 1; -} - -static int p256_generate_key(EVP_HPKE_KEY *key) { - uint8_t seed[P256_SEED_LEN]; - RAND_bytes(seed, sizeof(seed)); - return p256_derive_key(key, seed); -} - -static int p256(uint8_t out_dh[P256_SHARED_KEY_LEN], - const uint8_t my_private[P256_PRIVATE_KEY_LEN], - const uint8_t their_public[P256_PUBLIC_VALUE_LEN]) { - const EC_GROUP *const group = EC_group_p256(); - EC_SCALAR private_scalar; - EC_FELEM x, y; - EC_JACOBIAN shared_point, their_point; - EC_AFFINE their_point_affine, shared_point_affine; - - if (their_public[0] != POINT_CONVERSION_UNCOMPRESSED || - !ec_felem_from_bytes(group, &x, &their_public[1], 32) || - !ec_felem_from_bytes(group, &y, &their_public[33], 32) || - !ec_point_set_affine_coordinates(group, &their_point_affine, &x, &y) || - !ec_scalar_from_bytes(group, &private_scalar, my_private, - P256_PRIVATE_KEY_LEN)) { - OPENSSL_PUT_ERROR(EVP, ERR_R_INTERNAL_ERROR); + // dh = DH(skR, pkE) + InplaceVector dh; + if (!dh.TryResizeForOverwrite(dhkem->dh_shared_key_len) || + !dhkem_dh(dhkem, Span(dh), key, ephemeral_key.get())) { return 0; } - - ec_affine_to_jacobian(group, &their_point, &their_point_affine); - if (!ec_point_mul_scalar(group, &shared_point, &their_point, - &private_scalar) || - !ec_jacobian_to_affine(group, &shared_point_affine, &shared_point)) { - OPENSSL_PUT_ERROR(EVP, ERR_R_INTERNAL_ERROR); + // pkRm = SerializePublicKey(pk(skR)) + // kem_context = concat(enc, pkRm) + InplaceVector kem_context; + if (!kem_context.TryResizeForOverwrite(2 * public_len) || + !dhkem_serialize_public_key(dhkem, Span(kem_context).subspan(public_len), + key)) { return 0; } - - size_t out_len; - ec_felem_to_bytes(group, out_dh, &out_len, &shared_point_affine.X); - assert(out_len == P256_SHARED_KEY_LEN); - return 1; + OPENSSL_memcpy(kem_context.data(), ciphertext.data(), ciphertext.size()); + // shared_secret = ExtractAndExpand(dh, kem_context) + return dhkem_extract_and_expand(dhkem->key_method->hpke_kem_id, + dhkem->hkdf_digest_func(), out_secret.data(), + out_secret.size(), dh.data(), dh.size(), + kem_context.data(), kem_context.size()); } -static int p256_encap_with_seed(const EVP_HPKE_KEM *kem, - uint8_t *out_shared_secret, - size_t *out_shared_secret_len, uint8_t *out_enc, - size_t *out_enc_len, size_t max_enc, - const uint8_t *peer_public_key, - size_t peer_public_key_len, const uint8_t *seed, - size_t seed_len) { - if (max_enc < P256_PUBLIC_VALUE_LEN) { - OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_BUFFER_SIZE); +int dhkem_auth_encap_with_ephemeral_key(const EVP_KEM_DHKEM *dhkem, + Span out_ciphertext, + Span out_secret, + const EVP_PKEY *peer_key, + const EVP_PKEY *auth_key, + const EVP_PKEY *ephemeral_key) { + const size_t public_len = dhkem->key_method->public_key_len; + if (out_ciphertext.size() != public_len) { + OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_CIPHERTEXT_LENGTH); return 0; } - if (seed_len != P256_SEED_LEN) { - OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR); + if (out_secret.size() != dhkem->secret_len) { + OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_SECRET_LENGTH); return 0; } - uint8_t private_key[P256_PRIVATE_KEY_LEN]; - if (!p256_private_key_from_seed(private_key, seed, seed_len) || - !p256_public_from_private(out_enc, private_key)) { + // dh = concat(DH(skE, pkR), DH(skS, pkR)) + InplaceVector dh; + if (!dh.TryResizeForOverwrite(2 * dhkem->dh_shared_key_len) || + !dhkem_dh(dhkem, Span(dh).subspan(0, dhkem->dh_shared_key_len), + ephemeral_key, peer_key) || + !dhkem_dh(dhkem, Span(dh).subspan(dhkem->dh_shared_key_len), auth_key, + peer_key)) { return 0; } - - uint8_t dh[P256_SHARED_KEY_LEN]; - if (peer_public_key_len != P256_PUBLIC_VALUE_LEN || - !p256(dh, private_key, peer_public_key)) { - OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_PEER_KEY); + // enc = SerializePublicKey(pkE) + // pkRm = SerializePublicKey(pkR) + // pkSm = SerializePublicKey(pk(skS)) + // kem_context = concat(enc, pkRm, pkSm) + InplaceVector kem_context; + if (!kem_context.TryResizeForOverwrite(3 * public_len) || + !dhkem_serialize_public_keys(dhkem, Span(kem_context), ephemeral_key, + peer_key, auth_key)) { return 0; } - - uint8_t kem_context[2 * P256_PUBLIC_VALUE_LEN]; - OPENSSL_memcpy(kem_context, out_enc, P256_PUBLIC_VALUE_LEN); - OPENSSL_memcpy(kem_context + P256_PUBLIC_VALUE_LEN, peer_public_key, - P256_PUBLIC_VALUE_LEN); - if (!dhkem_extract_and_expand(kem->id, EVP_sha256(), out_shared_secret, - SHA256_DIGEST_LENGTH, dh, sizeof(dh), - kem_context, sizeof(kem_context))) { - return 0; - } - - *out_enc_len = P256_PUBLIC_VALUE_LEN; - *out_shared_secret_len = SHA256_DIGEST_LENGTH; - return 1; + OPENSSL_memcpy(out_ciphertext.data(), kem_context.data(), public_len); + // shared_secret = ExtractAndExpand(dh, kem_context) + return dhkem_extract_and_expand(dhkem->key_method->hpke_kem_id, + dhkem->hkdf_digest_func(), out_secret.data(), + out_secret.size(), dh.data(), dh.size(), + kem_context.data(), kem_context.size()); } -static int p256_decap(const EVP_HPKE_KEY *key, uint8_t *out_shared_secret, - size_t *out_shared_secret_len, const uint8_t *enc, - size_t enc_len) { - uint8_t dh[P256_SHARED_KEY_LEN]; - if (enc_len != P256_PUBLIC_VALUE_LEN || // - !p256(dh, key->private_key, enc)) { - OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_PEER_KEY); +int dhkem_auth_encap(const EVP_KEM *kem, Span out_ciphertext, + Span out_secret, const EVP_PKEY *peer_key, + const EVP_PKEY *auth_key) { + const auto *dhkem = static_cast(kem); + UniquePtr ephemeral_key( + EVP_PKEY_generate_from_alg(dhkem->key_method->alg_func())); + if (ephemeral_key == nullptr) { return 0; } + return dhkem_auth_encap_with_ephemeral_key(dhkem, out_ciphertext, out_secret, + peer_key, auth_key, + ephemeral_key.get()); +} - uint8_t kem_context[2 * P256_PUBLIC_VALUE_LEN]; - OPENSSL_memcpy(kem_context, enc, P256_PUBLIC_VALUE_LEN); - OPENSSL_memcpy(kem_context + P256_PUBLIC_VALUE_LEN, key->public_key, - P256_PUBLIC_VALUE_LEN); - if (!dhkem_extract_and_expand(key->kem->id, EVP_sha256(), out_shared_secret, - SHA256_DIGEST_LENGTH, dh, sizeof(dh), - kem_context, sizeof(kem_context))) { +int dhkem_auth_encap_external_entropy(const EVP_KEM *kem, + Span out_ciphertext, + Span out_secret, + const EVP_PKEY *peer_key, + const EVP_PKEY *auth_key, + Span entropy) { + const auto *dhkem = static_cast(kem); + UniquePtr ephemeral_key(dhkem->key_from_encap_entropy( + dhkem->key_method->alg_func(), entropy.data(), entropy.size())); + if (ephemeral_key == nullptr) { return 0; } - - *out_shared_secret_len = SHA256_DIGEST_LENGTH; - return 1; + return dhkem_auth_encap_with_ephemeral_key(dhkem, out_ciphertext, out_secret, + peer_key, auth_key, + ephemeral_key.get()); } -static int p256_auth_encap_with_seed( - const EVP_HPKE_KEY *key, uint8_t *out_shared_secret, - size_t *out_shared_secret_len, uint8_t *out_enc, size_t *out_enc_len, - size_t max_enc, const uint8_t *peer_public_key, size_t peer_public_key_len, - const uint8_t *seed, size_t seed_len) { - if (max_enc < P256_PUBLIC_VALUE_LEN) { - OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_BUFFER_SIZE); +int dhkem_auth_decap(const EVP_KEM *kem, Span out_secret, + Span ciphertext, const EVP_PKEY *key, + const EVP_PKEY *auth_key) { + const auto *dhkem = static_cast(kem); + const size_t public_len = dhkem->key_method->public_key_len; + if (ciphertext.size() != public_len) { + OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_CIPHERTEXT_LENGTH); return 0; } - if (seed_len != P256_SEED_LEN) { - OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR); + if (out_secret.size() != dhkem->secret_len) { + OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_SECRET_LENGTH); return 0; } - uint8_t private_key[P256_PRIVATE_KEY_LEN]; - if (!p256_private_key_from_seed(private_key, seed, seed_len) || - !p256_public_from_private(out_enc, private_key)) { + // pkE = DeserializePublicKey(enc) + UniquePtr ephemeral_key(dhkem->key_method->deserialize_public_key( + dhkem->key_method->alg_func(), ciphertext.data(), ciphertext.size())); + if (ephemeral_key == nullptr) { return 0; } - - uint8_t dh[2 * P256_SHARED_KEY_LEN]; - if (peer_public_key_len != P256_PUBLIC_VALUE_LEN || - !p256(dh, private_key, peer_public_key) || - !p256(dh + P256_SHARED_KEY_LEN, key->private_key, peer_public_key)) { - OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_PEER_KEY); + // dh = concat(DH(skR, pkE), DH(skR, pkS)) + InplaceVector dh; + if (!dh.TryResizeForOverwrite(2 * dhkem->dh_shared_key_len) || + !dhkem_dh(dhkem, Span(dh).subspan(0, dhkem->dh_shared_key_len), key, + ephemeral_key.get()) || + !dhkem_dh(dhkem, Span(dh).subspan(dhkem->dh_shared_key_len), key, + auth_key)) { return 0; } - - uint8_t kem_context[3 * P256_PUBLIC_VALUE_LEN]; - OPENSSL_memcpy(kem_context, out_enc, P256_PUBLIC_VALUE_LEN); - OPENSSL_memcpy(kem_context + P256_PUBLIC_VALUE_LEN, peer_public_key, - P256_PUBLIC_VALUE_LEN); - OPENSSL_memcpy(kem_context + 2 * P256_PUBLIC_VALUE_LEN, key->public_key, - P256_PUBLIC_VALUE_LEN); - if (!dhkem_extract_and_expand(key->kem->id, EVP_sha256(), out_shared_secret, - SHA256_DIGEST_LENGTH, dh, sizeof(dh), - kem_context, sizeof(kem_context))) { + // pkRm = SerializePublicKey(pk(skR)) + // pkSm = SerializePublicKey(pkS) + // kem_context = concat(enc, pkRm, pkSm) + InplaceVector kem_context; + if (!kem_context.TryResizeForOverwrite(3 * public_len) || + !dhkem_serialize_public_keys(dhkem, Span(kem_context).subspan(public_len), + key, auth_key)) { return 0; } - - *out_enc_len = P256_PUBLIC_VALUE_LEN; - *out_shared_secret_len = SHA256_DIGEST_LENGTH; - return 1; + OPENSSL_memcpy(kem_context.data(), ciphertext.data(), ciphertext.size()); + // shared_secret = ExtractAndExpand(dh, kem_context) + return dhkem_extract_and_expand(dhkem->key_method->hpke_kem_id, + dhkem->hkdf_digest_func(), out_secret.data(), + out_secret.size(), dh.data(), dh.size(), + kem_context.data(), kem_context.size()); } -static int p256_auth_decap(const EVP_HPKE_KEY *key, uint8_t *out_shared_secret, - size_t *out_shared_secret_len, const uint8_t *enc, - size_t enc_len, const uint8_t *peer_public_key, - size_t peer_public_key_len) { - uint8_t dh[2 * P256_SHARED_KEY_LEN]; - if (enc_len != P256_PUBLIC_VALUE_LEN || - peer_public_key_len != P256_PUBLIC_VALUE_LEN || - !p256(dh, key->private_key, enc) || - !p256(dh + P256_SHARED_KEY_LEN, key->private_key, peer_public_key)) { - OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_PEER_KEY); - return 0; +// RFC 9180: "For X25519 ..., the DeriveKeyPair() function applies a KDF to the +// input." Since HPKE uses X25519 only in the DHKEM(X25519, HKDF-SHA256) +// construction, this uses HKDF-SHA256 as the KDF. +EVP_PKEY *x25519_derive_key_pair(const EVP_PKEY_ALG *alg, uint16_t kem_suite_id, + Span ikm) { + // https://www.rfc-editor.org/rfc/rfc9180.html#name-derivekeypair + auto suite_id = hpke_kem_suite_id(kem_suite_id); + uint8_t dkp_prk[SHA256_DIGEST_LENGTH]; + size_t dkp_prk_len; + uint8_t sk[X25519_PRIVATE_KEY_LEN]; + if (!hpke_labeled_extract(EVP_sha256(), dkp_prk, &dkp_prk_len, nullptr, 0, + suite_id.data(), suite_id.size(), "dkp_prk", + ikm.data(), ikm.size()) || + !hpke_labeled_expand(EVP_sha256(), sk, sizeof(sk), dkp_prk, dkp_prk_len, + suite_id.data(), suite_id.size(), "sk", + /*info=*/nullptr, /*info_len=*/0)) { + return nullptr; } + return EVP_PKEY_from_raw_private_key(alg, sk, sizeof(sk)); +} - uint8_t kem_context[3 * P256_PUBLIC_VALUE_LEN]; - OPENSSL_memcpy(kem_context, enc, P256_PUBLIC_VALUE_LEN); - OPENSSL_memcpy(kem_context + P256_PUBLIC_VALUE_LEN, key->public_key, - P256_PUBLIC_VALUE_LEN); - OPENSSL_memcpy(kem_context + 2 * P256_PUBLIC_VALUE_LEN, peer_public_key, - P256_PUBLIC_VALUE_LEN); - if (!dhkem_extract_and_expand(key->kem->id, EVP_sha256(), out_shared_secret, - SHA256_DIGEST_LENGTH, dh, sizeof(dh), - kem_context, sizeof(kem_context))) { - return 0; - } +constexpr HpkeKeyMethod kX25519KeyMethod = { + /*hpke_kem_id=*/EVP_HPKE_DHKEM_X25519_HKDF_SHA256, + /*public_key_len=*/X25519_PUBLIC_VALUE_LEN, + /*private_key_len=*/X25519_PRIVATE_KEY_LEN, + /*alg_func=*/&EVP_pkey_x25519, + /*deserialize_private_key=*/&EVP_PKEY_from_raw_private_key, + /*serialize_private_key=*/&EVP_PKEY_get_raw_private_key, + /*deserialize_public_key=*/&EVP_PKEY_from_raw_public_key, + /*serialize_public_key=*/&EVP_PKEY_get_raw_public_key, + /*derive_key_pair=*/x25519_derive_key_pair, +}; - *out_shared_secret_len = SHA256_DIGEST_LENGTH; - return 1; +} // namespace + +const EVP_KEM *EVP_kem_dhkem_x25519_hkdf_sha256() { + static constexpr EVP_KEM_DHKEM kDhkemX25519 = { + /*EVP_KEM=*/{ + /*pkey_id=*/EVP_PKEY_X25519, + /*ciphertext_len=*/X25519_PUBLIC_VALUE_LEN, + /*secret_len=*/SHA256_DIGEST_LENGTH, + /*entropy_len=*/X25519_PRIVATE_KEY_LEN, + /*check_key=*/nullptr, + /*encap=*/&dhkem_encap, + /*encap_external_entropy=*/&dhkem_encap_external_entropy, + /*decap=*/&dhkem_decap, + }, + /*dh_shared_key_len=*/X25519_SHARED_KEY_LEN, + /*key_method=*/&kX25519KeyMethod, + /*hkdf_digest_func=*/&EVP_sha256, + // The X25519 encap entropy is the ephemeral private key itself. + /*key_from_encap_entropy=*/&EVP_PKEY_from_raw_private_key, + }; + return &kDhkemX25519; } -const EVP_HPKE_KEM *EVP_hpke_p256_hkdf_sha256() { - static const EVP_HPKE_KEM kKEM = { - /*id=*/EVP_HPKE_DHKEM_P256_HKDF_SHA256, - /*public_key_len=*/P256_PUBLIC_KEY_LEN, - /*private_key_len=*/P256_PRIVATE_KEY_LEN, - /*seed_len=*/P256_SEED_LEN, - /*enc_len=*/P256_PUBLIC_VALUE_LEN, - p256_init_key, - p256_generate_key, - p256_derive_key, - p256_encap_with_seed, - p256_decap, - p256_auth_encap_with_seed, - p256_auth_decap, +const EVP_HPKE_KEM *EVP_hpke_x25519_hkdf_sha256() { + static constexpr EVP_HPKE_KEM kKEM = { + /*id=*/EVP_HPKE_DHKEM_X25519_HKDF_SHA256, + /*evp_kem_func=*/&EVP_kem_dhkem_x25519_hkdf_sha256, + /*key_method=*/&kX25519KeyMethod, + /*auth_encap=*/&dhkem_auth_encap, + /*auth_encap_external_entropy=*/&dhkem_auth_encap_external_entropy, + /*auth_decap=*/&dhkem_auth_decap, }; return &kKEM; } -#define XWING_PRIVATE_KEY_LEN XWING_PRIVATE_KEY_BYTES -#define XWING_PUBLIC_KEY_LEN XWING_PUBLIC_KEY_BYTES -#define XWING_PUBLIC_VALUE_LEN XWING_CIPHERTEXT_BYTES -#define XWING_SEED_LEN 64 -#define XWING_SHARED_KEY_LEN XWING_SHARED_SECRET_BYTES +namespace { -static int xwing_init_key(EVP_HPKE_KEY *key, const uint8_t *priv_key, - size_t priv_key_len) { - CBS cbs; - CBS_init(&cbs, priv_key, priv_key_len); - XWING_private_key private_key; - if (!XWING_parse_private_key(&private_key, &cbs) || CBS_len(&cbs) != 0) { - OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR); - return 0; - } +constexpr size_t kP256PrivateKeyLen = 32; +constexpr size_t kP256PublicValueLen = 65; +constexpr size_t kP256SeedLen = 32; +constexpr size_t kP256SharedKeyLen = 32; - if (!XWING_public_from_private(key->public_key, &private_key)) { +// RFC 9180's SerializePublicKey format for DHKEM(P-256, HKDF-SHA256) is the +// uncompressed X9.62 point, and its private keys are the big-endian scalar. +int p256_serialize_public_key(const EVP_PKEY *pkey, uint8_t *out, + size_t *out_len) { + CBB cbb; + CBB_init_fixed(&cbb, out, *out_len); + if (!EVP_PKEY_marshal_ec_uncompressed_point(&cbb, pkey)) { return 0; } + *out_len = CBB_len(&cbb); + return 1; +} - if (priv_key_len > sizeof(key->private_key)) { - OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR); +int p256_serialize_private_key(const EVP_PKEY *pkey, uint8_t *out, + size_t *out_len) { + CBB cbb; + CBB_init_fixed(&cbb, out, *out_len); + if (!EVP_PKEY_marshal_ec_private_scalar(&cbb, pkey)) { return 0; } - OPENSSL_memcpy(key->private_key, priv_key, priv_key_len); + *out_len = CBB_len(&cbb); return 1; } -static int xwing_generate_key(EVP_HPKE_KEY *key) { - XWING_private_key private_key; - if (!XWING_generate_key(key->public_key, &private_key)) { - return 0; +EVP_PKEY *p256_derive_key_pair(const EVP_PKEY_ALG *alg, uint16_t kem_suite_id, + Span ikm) { + // https://www.rfc-editor.org/rfc/rfc9180.html#name-derivekeypair + auto suite_id = hpke_kem_suite_id(kem_suite_id); + uint8_t dkp_prk[SHA256_DIGEST_LENGTH]; + size_t dkp_prk_len; + if (!hpke_labeled_extract(EVP_sha256(), dkp_prk, &dkp_prk_len, nullptr, 0, + suite_id.data(), suite_id.size(), "dkp_prk", + ikm.data(), ikm.size())) { + return nullptr; } + assert(dkp_prk_len == sizeof(dkp_prk)); + const EC_GROUP *const group = EC_group_p256(); - CBB cbb; - CBB_init_fixed(&cbb, key->private_key, XWING_PRIVATE_KEY_LEN); - if (!XWING_marshal_private_key(&cbb, &private_key) || - CBB_len(&cbb) != XWING_PRIVATE_KEY_LEN) { - return 0; + // Rejection sampling. + uint8_t priv[kP256PrivateKeyLen]; + for (size_t counter = 0; counter < 256; counter++) { + const uint8_t counter_byte = counter & 0xff; + if (!hpke_labeled_expand(EVP_sha256(), priv, sizeof(priv), dkp_prk, + sizeof(dkp_prk), suite_id.data(), suite_id.size(), + "candidate", &counter_byte, + sizeof(counter_byte))) { + return nullptr; + } + EC_SCALAR private_scalar; + // `ec_scalar_from_bytes` checks that the scalar is less than the order. + if (ec_scalar_from_bytes(group, &private_scalar, priv, sizeof(priv)) && + !ec_scalar_is_zero(group, &private_scalar)) { + return EVP_PKEY_from_ec_private_scalar(alg, priv, sizeof(priv)); + } } + // This happens with probability of 2^-(32*256). + OPENSSL_PUT_ERROR(EVP, ERR_R_INTERNAL_ERROR); + return nullptr; +} - return 1; +// The P-256 encap entropy is ikmE, the input to DeriveKeyPair. +EVP_PKEY *p256_key_from_encap_entropy(const EVP_PKEY_ALG *alg, + const uint8_t *in, size_t len) { + return p256_derive_key_pair(alg, EVP_HPKE_DHKEM_P256_HKDF_SHA256, + Span(in, len)); } -static int xwing_derive_key(EVP_HPKE_KEY *key, Span ikm) { - uint8_t seed[32]; - hpke_shake256_labeled_derive(seed, ikm, hpke_kem_suite_id(EVP_HPKE_XWING), - "DeriveKeyPair", /*context=*/{}); - return xwing_init_key(key, seed, sizeof(seed)); +bool p256_check_key(const EVP_KEM *kem, const EVP_PKEY *key) { + const EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(key); + return ec_key != nullptr && + EC_GROUP_cmp(EC_KEY_get0_group(ec_key), EC_group_p256(), + /*ignored=*/nullptr) == 0; } -static int xwing_encap_with_seed(const EVP_HPKE_KEM *kem, - uint8_t *out_shared_secret, - size_t *out_shared_secret_len, - uint8_t *out_enc, size_t *out_enc_len, - size_t max_enc, const uint8_t *peer_public_key, - size_t peer_public_key_len, - const uint8_t *seed, size_t seed_len) { - if (max_enc < XWING_PUBLIC_VALUE_LEN) { - OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_BUFFER_SIZE); - return 0; - } - if (peer_public_key_len != XWING_PUBLIC_KEY_LEN || - seed_len != XWING_SEED_LEN) { - OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR); - return 0; - } +constexpr HpkeKeyMethod kP256KeyMethod = { + /*hpke_kem_id=*/EVP_HPKE_DHKEM_P256_HKDF_SHA256, + /*public_key_len=*/kP256PublicValueLen, + /*private_key_len=*/kP256PrivateKeyLen, + /*alg_func=*/&EVP_pkey_ec_p256, + /*deserialize_private_key=*/&EVP_PKEY_from_ec_private_scalar, + /*serialize_private_key=*/&p256_serialize_private_key, + /*deserialize_public_key=*/&EVP_PKEY_from_ec_uncompressed_point, + /*serialize_public_key=*/&p256_serialize_public_key, + /*derive_key_pair=*/&p256_derive_key_pair, +}; - if (!XWING_encap_external_entropy(out_enc, out_shared_secret, peer_public_key, - seed)) { - OPENSSL_PUT_ERROR(EVP, ERR_R_INTERNAL_ERROR); - return 0; - } +} // namespace - *out_enc_len = XWING_PUBLIC_VALUE_LEN; - *out_shared_secret_len = XWING_SHARED_KEY_LEN; - return 1; +const EVP_KEM *EVP_kem_dhkem_p256_hkdf_sha256() { + static constexpr EVP_KEM_DHKEM kDhkemP256 = { + /*EVP_KEM=*/{ + /*pkey_id=*/EVP_PKEY_EC, + /*ciphertext_len=*/kP256PublicValueLen, + /*secret_len=*/SHA256_DIGEST_LENGTH, + /*entropy_len=*/kP256SeedLen, + /*check_key=*/&p256_check_key, + /*encap=*/&dhkem_encap, + /*encap_external_entropy=*/&dhkem_encap_external_entropy, + /*decap=*/&dhkem_decap, + }, + /*dh_shared_key_len=*/kP256SharedKeyLen, + /*key_method=*/&kP256KeyMethod, + /*hkdf_digest_func=*/&EVP_sha256, + /*key_from_encap_entropy=*/&p256_key_from_encap_entropy, + }; + return &kDhkemP256; } -static int xwing_decap(const EVP_HPKE_KEY *key, uint8_t *out_shared_secret, - size_t *out_shared_secret_len, const uint8_t *enc, - size_t enc_len) { - if (enc_len != XWING_PUBLIC_VALUE_LEN) { - OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR); - return 0; - } +const EVP_HPKE_KEM *EVP_hpke_p256_hkdf_sha256() { + static constexpr EVP_HPKE_KEM kKEM = { + /*id=*/EVP_HPKE_DHKEM_P256_HKDF_SHA256, + /*evp_kem_func=*/&EVP_kem_dhkem_p256_hkdf_sha256, + /*key_method=*/&kP256KeyMethod, + /*auth_encap=*/&dhkem_auth_encap, + /*auth_encap_external_entropy=*/&dhkem_auth_encap_external_entropy, + /*auth_decap=*/&dhkem_auth_decap, + }; + return &kKEM; +} - CBS cbs; - CBS_init(&cbs, key->private_key, XWING_PRIVATE_KEY_LEN); - XWING_private_key private_key; - if (!XWING_parse_private_key(&private_key, &cbs)) { - OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR); - return 0; - } +namespace { - if (!XWING_decap(out_shared_secret, enc, &private_key)) { - OPENSSL_PUT_ERROR(EVP, ERR_R_INTERNAL_ERROR); - return 0; - } +template +EVP_PKEY *shake256_derive_key_pair_from_private_seed(const EVP_PKEY_ALG *alg, + uint16_t kem_suite_id, + Span ikm) { + uint8_t seed[SEED_BYTES]; + hpke_shake256_labeled_derive(seed, ikm, hpke_kem_suite_id(kem_suite_id), + "DeriveKeyPair", /*context=*/{}); + return EVP_PKEY_from_private_seed(alg, seed, sizeof(seed)); +} + +constexpr HpkeKeyMethod kXwingKeyMethod = { + /*hpke_kem_id=*/EVP_HPKE_XWING, + /*public_key_len=*/XWING_PUBLIC_KEY_BYTES, + /*private_key_len=*/XWING_PRIVATE_KEY_BYTES, + /*alg_func=*/&EVP_pkey_xwing, + /*deserialize_private_key=*/&EVP_PKEY_from_private_seed, + /*serialize_private_key=*/&EVP_PKEY_get_private_seed, + /*deserialize_public_key=*/&EVP_PKEY_from_raw_public_key, + /*serialize_public_key=*/&EVP_PKEY_get_raw_public_key, + /*derive_key_pair=*/ + shake256_derive_key_pair_from_private_seed, +}; - *out_shared_secret_len = XWING_SHARED_KEY_LEN; - return 1; -} +} // namespace const EVP_HPKE_KEM *EVP_hpke_xwing() { - static const EVP_HPKE_KEM kKEM = { + static constexpr EVP_HPKE_KEM kKEM = { /*id=*/EVP_HPKE_XWING, - /*public_key_len=*/XWING_PUBLIC_KEY_LEN, - /*private_key_len=*/XWING_PRIVATE_KEY_LEN, - /*seed_len=*/XWING_SEED_LEN, - /*enc_len=*/XWING_PUBLIC_VALUE_LEN, - xwing_init_key, - xwing_generate_key, - xwing_derive_key, - xwing_encap_with_seed, - xwing_decap, + /*evp_kem_func=*/&EVP_kem_xwing, + /*key_method=*/&kXwingKeyMethod, // X-Wing doesn't support authenticated encapsulation/decapsulation: // https://datatracker.ietf.org/doc/html/draft-connolly-cfrg-xwing-kem-08#name-use-in-hpke - /* auth_encap_with_seed= */ nullptr, - /* auth_decap= */ nullptr, + /*auth_encap=*/nullptr, + /*auth_encap_external_entropy=*/nullptr, + /*auth_decap=*/nullptr, }; return &kKEM; } namespace { -template -struct MLKEMHPKE { - // These sizes are common across both ML-KEM-768 and ML-KEM-1024. - static constexpr size_t PRIVATE_KEY_LEN = MLKEM_SEED_BYTES; - static constexpr size_t SHARED_KEY_LEN = MLKEM_SHARED_SECRET_BYTES; - - static constexpr uint16_t ID = KEM_ID; - static constexpr size_t PUBLIC_KEY_LEN = PUBLIC_KEY_BYTES; - static constexpr size_t SEED_LEN = ENCAP_ENTROPY_BYTES; - static constexpr size_t ENC_LEN = CIPHERTEXT_BYTES; - - static int InitKey(EVP_HPKE_KEY *key, const uint8_t *priv_key, - size_t priv_key_len) { - PrivateKey expanded_private_key; - if (!PrivateKeyFromSeed(&expanded_private_key, priv_key, priv_key_len)) { - OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR); - return 0; - } - PublicKey public_key; - PublicFromPrivate(&public_key, &expanded_private_key); - CBB cbb; - static_assert(sizeof(key->public_key) >= PUBLIC_KEY_BYTES, - "EVP_HPKE_KEY public_key is too small for ML-KEM."); - if (!CBB_init_fixed(&cbb, key->public_key, PUBLIC_KEY_BYTES) || - !MarshalPublicKey(&cbb, &public_key)) { - return 0; - } - - static_assert(sizeof(key->private_key) >= PRIVATE_KEY_LEN, - "EVP_HPKE_KEY private_key is too small for ML-KEM"); - OPENSSL_memcpy(key->private_key, priv_key, priv_key_len); - return 1; - } - - static int HpkeDeriveKey(EVP_HPKE_KEY *key, Span ikm) { - uint8_t seed[64]; - hpke_shake256_labeled_derive(seed, ikm, hpke_kem_suite_id(ID), - "DeriveKeyPair", - /*context=*/{}); - return InitKey(key, seed, sizeof(seed)); - } - - static int HpkeGenerateKey(EVP_HPKE_KEY *key) { - static_assert(sizeof(key->public_key) >= PUBLIC_KEY_BYTES, - "EVP_HPKE_KEY public_key is too small for ML-KEM."); - static_assert(sizeof(key->private_key) >= PRIVATE_KEY_LEN, - "EVP_HPKE_KEY private_key is too small for ML-KEM"); - PrivateKey expanded_private_key; - GenerateKey(key->public_key, key->private_key, &expanded_private_key); - - return 1; - } - - static int EncapWithSeed(const EVP_HPKE_KEM *kem, uint8_t *out_shared_secret, - size_t *out_shared_secret_len, uint8_t *out_enc, - size_t *out_enc_len, size_t max_enc, - const uint8_t *peer_public_key, - size_t peer_public_key_len, const uint8_t *seed, - size_t seed_len) { - if (max_enc < CIPHERTEXT_BYTES) { - OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_BUFFER_SIZE); - return 0; - } - if (peer_public_key_len != PUBLIC_KEY_BYTES || - seed_len != ENCAP_ENTROPY_BYTES) { - OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR); - return 0; - } - - CBS cbs; - CBS_init(&cbs, peer_public_key, peer_public_key_len); - PublicKey public_key; - if (!ParsePublicKey(&public_key, &cbs)) { - OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR); - return 0; - } - // The public ML-KEM interface doesn't support providing the encap entropy - // so the BCM function is used here. - BCMEncapExternalEntropy(out_enc, out_shared_secret, &public_key, seed); - - *out_enc_len = CIPHERTEXT_BYTES; - *out_shared_secret_len = SHARED_KEY_LEN; - return 1; - } - - static int HpkeDecap(const EVP_HPKE_KEY *key, uint8_t *out_shared_secret, - size_t *out_shared_secret_len, const uint8_t *enc, - size_t enc_len) { - PrivateKey private_key; - if (!PrivateKeyFromSeed(&private_key, key->private_key, PRIVATE_KEY_LEN)) { - OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR); - return 0; - } - - if (!Decap(out_shared_secret, enc, enc_len, &private_key)) { - OPENSSL_PUT_ERROR(EVP, ERR_R_INTERNAL_ERROR); - return 0; - } - - *out_shared_secret_len = SHARED_KEY_LEN; - return 1; - } +constexpr HpkeKeyMethod kMlkem768KeyMethod = { + /*hpke_kem_id=*/EVP_HPKE_MLKEM768, + /*public_key_len=*/MLKEM768_PUBLIC_KEY_BYTES, + /*private_key_len=*/MLKEM_SEED_BYTES, + /*alg_func=*/&EVP_pkey_ml_kem_768, + /*deserialize_private_key=*/&EVP_PKEY_from_private_seed, + /*serialize_private_key=*/&EVP_PKEY_get_private_seed, + /*deserialize_public_key=*/&EVP_PKEY_from_raw_public_key, + /*serialize_public_key=*/&EVP_PKEY_get_raw_public_key, + /*derive_key_pair=*/ + shake256_derive_key_pair_from_private_seed, }; -using MLKEM768HPKE = - MLKEMHPKE; - -using MLKEM1024HPKE = - MLKEMHPKE; - -template -static const EVP_HPKE_KEM kMLKEM = { - /*id=*/MLKEM::ID, - /*public_key_len=*/MLKEM::PUBLIC_KEY_LEN, - /*private_key_len=*/MLKEM::PRIVATE_KEY_LEN, - /*seed_len=*/MLKEM::SEED_LEN, - /*enc_len=*/MLKEM::ENC_LEN, - MLKEM::InitKey, - MLKEM::HpkeGenerateKey, - MLKEM::HpkeDeriveKey, - MLKEM::EncapWithSeed, - MLKEM::HpkeDecap, - // ML-KEM doesn't support authenticated encapsulation/decapsulation: - // https://datatracker.ietf.org/doc/draft-ietf-hpke-pq/01/ - /*auth_encap_with_seed=*/nullptr, - /*auth_decap=*/nullptr, +constexpr HpkeKeyMethod kMlkem1024KeyMethod = { + /*hpke_kem_id=*/EVP_HPKE_MLKEM1024, + /*public_key_len=*/MLKEM1024_PUBLIC_KEY_BYTES, + /*private_key_len=*/MLKEM_SEED_BYTES, + /*alg_func=*/&EVP_pkey_ml_kem_1024, + /*deserialize_private_key=*/&EVP_PKEY_from_private_seed, + /*serialize_private_key=*/&EVP_PKEY_get_private_seed, + /*deserialize_public_key=*/&EVP_PKEY_from_raw_public_key, + /*serialize_public_key=*/&EVP_PKEY_get_raw_public_key, + /*derive_key_pair=*/ + shake256_derive_key_pair_from_private_seed, }; } // namespace -const EVP_HPKE_KEM *EVP_hpke_mlkem768() { return &kMLKEM; } -const EVP_HPKE_KEM *EVP_hpke_mlkem1024() { return &kMLKEM; } +const EVP_HPKE_KEM *EVP_hpke_mlkem768() { + static constexpr EVP_HPKE_KEM kKEM = { + /*id=*/EVP_HPKE_MLKEM768, + /*evp_kem_func=*/&EVP_kem_ml_kem_768, + /*key_method=*/&kMlkem768KeyMethod, + // ML-KEM doesn't support authenticated encapsulation/decapsulation: + // https://datatracker.ietf.org/doc/draft-ietf-hpke-pq/01/ + /*auth_encap=*/nullptr, + /*auth_encap_external_entropy=*/nullptr, + /*auth_decap=*/nullptr, + }; + return &kKEM; +} + +const EVP_HPKE_KEM *EVP_hpke_mlkem1024() { + static constexpr EVP_HPKE_KEM kKEM = { + /*id=*/EVP_HPKE_MLKEM1024, + /*evp_kem_func=*/&EVP_kem_ml_kem_1024, + /*key_method=*/&kMlkem1024KeyMethod, + // ML-KEM doesn't support authenticated encapsulation/decapsulation: + // https://datatracker.ietf.org/doc/draft-ietf-hpke-pq/01/ + /*auth_encap=*/nullptr, + /*auth_encap_external_entropy=*/nullptr, + /*auth_decap=*/nullptr, + }; + return &kKEM; +} uint16_t EVP_HPKE_KEM_id(const EVP_HPKE_KEM *kem) { return kem->id; } size_t EVP_HPKE_KEM_public_key_len(const EVP_HPKE_KEM *kem) { - return kem->public_key_len; + return kem->key_method->public_key_len; } size_t EVP_HPKE_KEM_private_key_len(const EVP_HPKE_KEM *kem) { - return kem->private_key_len; + return kem->key_method->private_key_len; +} + +size_t EVP_HPKE_KEM_enc_len(const EVP_HPKE_KEM *kem) { + return EVP_KEM_ciphertext_len(kem->evp_kem_func()); } -size_t EVP_HPKE_KEM_enc_len(const EVP_HPKE_KEM *kem) { return kem->enc_len; } +size_t EVP_HPKE_KEM_shared_secret_len(const EVP_HPKE_KEM *kem) { + return EVP_KEM_secret_len(kem->evp_kem_func()); +} void EVP_HPKE_KEY_zero(EVP_HPKE_KEY *key) { OPENSSL_memset(key, 0, sizeof(EVP_HPKE_KEY)); } void EVP_HPKE_KEY_cleanup(EVP_HPKE_KEY *key) { - // Nothing to clean up for now, but we may introduce a cleanup process in the - // future. + EVP_PKEY_free(key->pkey); + key->pkey = nullptr; } EVP_HPKE_KEY *EVP_HPKE_KEY_new() { @@ -994,17 +856,17 @@ void EVP_HPKE_KEY_free(EVP_HPKE_KEY *key) { } int EVP_HPKE_KEY_copy(EVP_HPKE_KEY *dst, const EVP_HPKE_KEY *src) { - // For now, |EVP_HPKE_KEY| is trivially copyable. - OPENSSL_memcpy(dst, src, sizeof(EVP_HPKE_KEY)); + dst->kem = src->kem; + dst->pkey = EVP_PKEY_dup_ref(src->pkey); return 1; } void EVP_HPKE_KEY_move(EVP_HPKE_KEY *out, EVP_HPKE_KEY *in) { EVP_HPKE_KEY_cleanup(out); - // For now, |EVP_HPKE_KEY| is trivially movable. + // For now, `EVP_HPKE_KEY` is trivially movable. // Note that Rust may move this structure. See // bssl-crypto/src/scoped.rs:EvpHpkeKey. - OPENSSL_memcpy(out, in, sizeof(EVP_HPKE_KEY)); + *out = *in; EVP_HPKE_KEY_zero(in); } @@ -1012,20 +874,26 @@ int EVP_HPKE_KEY_init(EVP_HPKE_KEY *key, const EVP_HPKE_KEM *kem, const uint8_t *priv_key, size_t priv_key_len) { EVP_HPKE_KEY_zero(key); key->kem = kem; - if (!kem->init_key(key, priv_key, priv_key_len)) { + UniquePtr pkey(kem->key_method->deserialize_private_key( + kem->key_method->alg_func(), priv_key, priv_key_len)); + if (pkey == nullptr) { key->kem = nullptr; return 0; } + key->pkey = pkey.release(); return 1; } int EVP_HPKE_KEY_generate(EVP_HPKE_KEY *key, const EVP_HPKE_KEM *kem) { EVP_HPKE_KEY_zero(key); key->kem = kem; - if (!kem->generate_key(key)) { + UniquePtr pkey( + EVP_PKEY_generate_from_alg(kem->key_method->alg_func())); + if (pkey == nullptr) { key->kem = nullptr; return 0; } + key->pkey = pkey.release(); return 1; } @@ -1033,10 +901,13 @@ int EVP_HPKE_KEY_derive(EVP_HPKE_KEY *key, const EVP_HPKE_KEM *kem, const uint8_t *ikm, size_t ikm_len) { EVP_HPKE_KEY_zero(key); key->kem = kem; - if (!kem->derive_key(key, Span(ikm, ikm_len))) { + UniquePtr pkey(kem->key_method->derive_key_pair( + kem->key_method->alg_func(), kem->id, Span(ikm, ikm_len))); + if (pkey == nullptr) { key->kem = nullptr; return 0; } + key->pkey = pkey.release(); return 1; } @@ -1046,24 +917,22 @@ const EVP_HPKE_KEM *EVP_HPKE_KEY_kem(const EVP_HPKE_KEY *key) { int EVP_HPKE_KEY_public_key(const EVP_HPKE_KEY *key, uint8_t *out, size_t *out_len, size_t max_out) { - if (max_out < key->kem->public_key_len) { + if (max_out < EVP_HPKE_KEM_public_key_len(key->kem)) { OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_BUFFER_SIZE); return 0; } - OPENSSL_memcpy(out, key->public_key, key->kem->public_key_len); - *out_len = key->kem->public_key_len; - return 1; + *out_len = max_out; + return key->kem->key_method->serialize_public_key(key->pkey, out, out_len); } int EVP_HPKE_KEY_private_key(const EVP_HPKE_KEY *key, uint8_t *out, size_t *out_len, size_t max_out) { - if (max_out < key->kem->private_key_len) { + if (max_out < EVP_HPKE_KEM_private_key_len(key->kem)) { OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_BUFFER_SIZE); return 0; } - OPENSSL_memcpy(out, key->private_key, key->kem->private_key_len); - *out_len = key->kem->private_key_len; - return 1; + *out_len = max_out; + return key->kem->key_method->serialize_private_key(key->pkey, out, out_len); } @@ -1112,7 +981,12 @@ const EVP_AEAD *EVP_HPKE_AEAD_aead(const EVP_HPKE_AEAD *aead) { // HPKE implementation. -static std::array hpke_full_suite_id(const EVP_HPKE_CTX *ctx) { +#define HPKE_MODE_BASE 0 +#define HPKE_MODE_AUTH 2 + +namespace { + +std::array hpke_full_suite_id(const EVP_HPKE_CTX *ctx) { return { 'H', 'P', @@ -1126,14 +1000,9 @@ static std::array hpke_full_suite_id(const EVP_HPKE_CTX *ctx) { static_cast(ctx->aead->id), }; } - -#define HPKE_MODE_BASE 0 -#define HPKE_MODE_AUTH 2 - -static int hpke_key_schedule(EVP_HPKE_CTX *ctx, uint8_t mode, - const uint8_t *shared_secret, - size_t shared_secret_len, const uint8_t *info, - size_t info_len) { +int hpke_key_schedule(EVP_HPKE_CTX *ctx, uint8_t mode, + const uint8_t *shared_secret, size_t shared_secret_len, + const uint8_t *info, size_t info_len) { auto suite_id = hpke_full_suite_id(ctx); // psk_id_hash = LabeledExtract("", "psk_id_hash", psk_id) @@ -1207,6 +1076,125 @@ static int hpke_key_schedule(EVP_HPKE_CTX *ctx, uint8_t mode, return 1; } +int hpke_setup_sender_impl(EVP_HPKE_CTX *ctx, uint8_t *out_enc, + size_t *out_enc_len, size_t max_enc, + const EVP_HPKE_KEM *kem, const EVP_HPKE_KDF *kdf, + const EVP_HPKE_AEAD *aead, + Span peer_public_key, + Span info, + std::optional> entropy) { + EVP_HPKE_CTX_zero(ctx); + ctx->is_sender = 1; + ctx->kem = kem; + ctx->kdf = kdf; + ctx->aead = aead; + + const EVP_KEM *evp_kem = kem->evp_kem_func(); + if (max_enc < EVP_KEM_ciphertext_len(evp_kem)) { + OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_BUFFER_SIZE); + return 0; + } + *out_enc_len = EVP_KEM_ciphertext_len(evp_kem); + + UniquePtr peer_pubkey(kem->key_method->deserialize_public_key( + kem->key_method->alg_func(), peer_public_key.data(), + peer_public_key.size())); + if (peer_pubkey == nullptr) { + return 0; + } + InplaceVector shared_secret; + if (!shared_secret.TryResizeForOverwrite(evp_kem->secret_len)) { + OPENSSL_PUT_ERROR(EVP, ERR_R_INTERNAL_ERROR); + return 0; + } + int encap_ok = + entropy.has_value() + ? EVP_KEM_encap_external_entropy_for_testing( + evp_kem, out_enc, *out_enc_len, shared_secret.data(), + shared_secret.size(), peer_pubkey.get(), entropy->data(), + entropy->size()) + : EVP_KEM_encap(evp_kem, out_enc, *out_enc_len, shared_secret.data(), + shared_secret.size(), peer_pubkey.get()); + if (!encap_ok || + !hpke_key_schedule(ctx, HPKE_MODE_BASE, shared_secret.data(), + shared_secret.size(), info.data(), info.size())) { + EVP_HPKE_CTX_cleanup(ctx); + return 0; + } + return 1; +} + +int hpke_setup_auth_sender_impl( + EVP_HPKE_CTX *ctx, uint8_t *out_enc, size_t *out_enc_len, size_t max_enc, + const EVP_HPKE_KEY *key, const EVP_HPKE_KDF *kdf, const EVP_HPKE_AEAD *aead, + Span peer_public_key, Span info, + std::optional> entropy) { + if (entropy.has_value() ? key->kem->auth_encap_external_entropy == nullptr + : key->kem->auth_encap == nullptr) { + // Not all HPKE KEMs support AuthEncap. + OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); + return 0; + } + + const EVP_KEM *kem = key->kem->evp_kem_func(); + EVP_HPKE_CTX_zero(ctx); + ctx->is_sender = 1; + ctx->kem = key->kem; + ctx->kdf = kdf; + ctx->aead = aead; + if (max_enc < kem->ciphertext_len) { + OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_BUFFER_SIZE); + return 0; + } + UniquePtr peer_key(key->kem->key_method->deserialize_public_key( + key->kem->key_method->alg_func(), peer_public_key.data(), + peer_public_key.size())); + if (peer_key == nullptr) { + return 0; + } + + InplaceVector shared_secret; + if (!shared_secret.TryResizeForOverwrite(kem->secret_len)) { + OPENSSL_PUT_ERROR(EVP, ERR_R_INTERNAL_ERROR); + return 0; + } + Span enc_span(out_enc, kem->ciphertext_len); + Span secret_span(shared_secret); + int auth_encap_ok = + entropy.has_value() + ? key->kem->auth_encap_external_entropy( + kem, enc_span, secret_span, peer_key.get(), key->pkey, *entropy) + : key->kem->auth_encap(kem, enc_span, secret_span, peer_key.get(), + key->pkey); + if (!auth_encap_ok || + !hpke_key_schedule(ctx, HPKE_MODE_AUTH, secret_span.data(), + secret_span.size(), info.data(), info.size())) { + EVP_HPKE_CTX_cleanup(ctx); + return 0; + } + *out_enc_len = kem->ciphertext_len; + return 1; +} + +void hpke_nonce(const EVP_HPKE_CTX *ctx, uint8_t *out_nonce, size_t nonce_len) { + assert(nonce_len >= 8); + + // Write padded big-endian bytes of `ctx->seq` to `out_nonce`. + OPENSSL_memset(out_nonce, 0, nonce_len); + uint64_t seq_copy = ctx->seq; + for (size_t i = 0; i < 8; i++) { + out_nonce[nonce_len - i - 1] = seq_copy & 0xff; + seq_copy >>= 8; + } + + // XOR the encoded sequence with the `ctx->base_nonce`. + for (size_t i = 0; i < nonce_len; i++) { + out_nonce[i] ^= ctx->base_nonce[i]; + } +} + +} // namespace + void EVP_HPKE_CTX_zero(EVP_HPKE_CTX *ctx) { OPENSSL_memset(ctx, 0, sizeof(EVP_HPKE_CTX)); EVP_AEAD_CTX_zero(&ctx->aead_ctx); @@ -1232,6 +1220,7 @@ void EVP_HPKE_CTX_free(EVP_HPKE_CTX *ctx) { } } + int EVP_HPKE_CTX_setup_sender(EVP_HPKE_CTX *ctx, uint8_t *out_enc, size_t *out_enc_len, size_t max_enc, const EVP_HPKE_KEM *kem, const EVP_HPKE_KDF *kdf, @@ -1239,11 +1228,10 @@ int EVP_HPKE_CTX_setup_sender(EVP_HPKE_CTX *ctx, uint8_t *out_enc, const uint8_t *peer_public_key, size_t peer_public_key_len, const uint8_t *info, size_t info_len) { - uint8_t seed[MAX_SEED_LEN]; - RAND_bytes(seed, kem->seed_len); - return EVP_HPKE_CTX_setup_sender_with_seed_for_testing( - ctx, out_enc, out_enc_len, max_enc, kem, kdf, aead, peer_public_key, - peer_public_key_len, info, info_len, seed, kem->seed_len); + return hpke_setup_sender_impl(ctx, out_enc, out_enc_len, max_enc, kem, kdf, + aead, + Span(peer_public_key, peer_public_key_len), + Span(info, info_len), /*entropy=*/std::nullopt); } int EVP_HPKE_CTX_setup_sender_with_seed_for_testing( @@ -1252,17 +1240,28 @@ int EVP_HPKE_CTX_setup_sender_with_seed_for_testing( const uint8_t *peer_public_key, size_t peer_public_key_len, const uint8_t *info, size_t info_len, const uint8_t *seed, size_t seed_len) { + return hpke_setup_sender_impl(ctx, out_enc, out_enc_len, max_enc, kem, kdf, + aead, + Span(peer_public_key, peer_public_key_len), + Span(info, info_len), Span(seed, seed_len)); +} + +int EVP_HPKE_CTX_setup_recipient_with_shared_secret( + EVP_HPKE_CTX *ctx, const EVP_HPKE_KEM *kem, const EVP_HPKE_KDF *kdf, + const EVP_HPKE_AEAD *aead, const uint8_t *shared_secret, + size_t shared_secret_len, const uint8_t *info, size_t info_len) { EVP_HPKE_CTX_zero(ctx); - ctx->is_sender = 1; + + if (shared_secret_len != EVP_HPKE_KEM_shared_secret_len(kem)) { + OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_BUFFER_SIZE); + return 0; + } + + ctx->is_sender = 0; ctx->kem = kem; ctx->kdf = kdf; ctx->aead = aead; - uint8_t shared_secret[MAX_SHARED_SECRET_LEN]; - size_t shared_secret_len; - if (!kem->encap_with_seed(kem, shared_secret, &shared_secret_len, out_enc, - out_enc_len, max_enc, peer_public_key, - peer_public_key_len, seed, seed_len) || - !hpke_key_schedule(ctx, HPKE_MODE_BASE, shared_secret, shared_secret_len, + if (!hpke_key_schedule(ctx, HPKE_MODE_BASE, shared_secret, shared_secret_len, info, info_len)) { EVP_HPKE_CTX_cleanup(ctx); return 0; @@ -1276,32 +1275,31 @@ int EVP_HPKE_CTX_setup_recipient(EVP_HPKE_CTX *ctx, const EVP_HPKE_KEY *key, size_t enc_len, const uint8_t *info, size_t info_len) { EVP_HPKE_CTX_zero(ctx); - ctx->is_sender = 0; - ctx->kem = key->kem; - ctx->kdf = kdf; - ctx->aead = aead; - uint8_t shared_secret[MAX_SHARED_SECRET_LEN]; - size_t shared_secret_len; - if (!key->kem->decap(key, shared_secret, &shared_secret_len, enc, enc_len) || - !hpke_key_schedule(ctx, HPKE_MODE_BASE, shared_secret, shared_secret_len, - info, info_len)) { - EVP_HPKE_CTX_cleanup(ctx); + + const EVP_KEM *evp_kem = key->kem->evp_kem_func(); + const EVP_PKEY *decap_key = key->pkey; + + InplaceVector shared_secret; + if (!shared_secret.TryResizeForOverwrite(evp_kem->secret_len)) { + OPENSSL_PUT_ERROR(EVP, ERR_R_INTERNAL_ERROR); return 0; } - return 1; + return EVP_KEM_decap(evp_kem, shared_secret.data(), shared_secret.size(), enc, + enc_len, decap_key) && + EVP_HPKE_CTX_setup_recipient_with_shared_secret( + ctx, key->kem, kdf, aead, shared_secret.data(), + shared_secret.size(), info, info_len); } - int EVP_HPKE_CTX_setup_auth_sender( EVP_HPKE_CTX *ctx, uint8_t *out_enc, size_t *out_enc_len, size_t max_enc, const EVP_HPKE_KEY *key, const EVP_HPKE_KDF *kdf, const EVP_HPKE_AEAD *aead, const uint8_t *peer_public_key, size_t peer_public_key_len, const uint8_t *info, size_t info_len) { - uint8_t seed[MAX_SEED_LEN]; - RAND_bytes(seed, key->kem->seed_len); - return EVP_HPKE_CTX_setup_auth_sender_with_seed_for_testing( - ctx, out_enc, out_enc_len, max_enc, key, kdf, aead, peer_public_key, - peer_public_key_len, info, info_len, seed, key->kem->seed_len); + return hpke_setup_auth_sender_impl( + ctx, out_enc, out_enc_len, max_enc, key, kdf, aead, + Span(peer_public_key, peer_public_key_len), Span(info, info_len), + /*entropy=*/std::nullopt); } int EVP_HPKE_CTX_setup_auth_sender_with_seed_for_testing( @@ -1310,28 +1308,10 @@ int EVP_HPKE_CTX_setup_auth_sender_with_seed_for_testing( const uint8_t *peer_public_key, size_t peer_public_key_len, const uint8_t *info, size_t info_len, const uint8_t *seed, size_t seed_len) { - if (key->kem->auth_encap_with_seed == nullptr) { - // Not all HPKE KEMs support AuthEncap. - OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); - return 0; - } - - EVP_HPKE_CTX_zero(ctx); - ctx->is_sender = 1; - ctx->kem = key->kem; - ctx->kdf = kdf; - ctx->aead = aead; - uint8_t shared_secret[MAX_SHARED_SECRET_LEN]; - size_t shared_secret_len; - if (!key->kem->auth_encap_with_seed( - key, shared_secret, &shared_secret_len, out_enc, out_enc_len, max_enc, - peer_public_key, peer_public_key_len, seed, seed_len) || - !hpke_key_schedule(ctx, HPKE_MODE_AUTH, shared_secret, shared_secret_len, - info, info_len)) { - EVP_HPKE_CTX_cleanup(ctx); - return 0; - } - return 1; + return hpke_setup_auth_sender_impl( + ctx, out_enc, out_enc_len, max_enc, key, kdf, aead, + Span(peer_public_key, peer_public_key_len), Span(info, info_len), + Span(seed, seed_len)); } int EVP_HPKE_CTX_setup_auth_recipient( @@ -1350,34 +1330,25 @@ int EVP_HPKE_CTX_setup_auth_recipient( ctx->kem = key->kem; ctx->kdf = kdf; ctx->aead = aead; - uint8_t shared_secret[MAX_SHARED_SECRET_LEN]; - size_t shared_secret_len; - if (!key->kem->auth_decap(key, shared_secret, &shared_secret_len, enc, - enc_len, peer_public_key, peer_public_key_len) || - !hpke_key_schedule(ctx, HPKE_MODE_AUTH, shared_secret, shared_secret_len, - info, info_len)) { - EVP_HPKE_CTX_cleanup(ctx); + const EVP_KEM *kem = key->kem->evp_kem_func(); + InplaceVector shared_secret; + if (!shared_secret.TryResizeForOverwrite(kem->secret_len)) { + OPENSSL_PUT_ERROR(EVP, ERR_R_INTERNAL_ERROR); return 0; } - return 1; -} - -static void hpke_nonce(const EVP_HPKE_CTX *ctx, uint8_t *out_nonce, - size_t nonce_len) { - assert(nonce_len >= 8); - - // Write padded big-endian bytes of |ctx->seq| to |out_nonce|. - OPENSSL_memset(out_nonce, 0, nonce_len); - uint64_t seq_copy = ctx->seq; - for (size_t i = 0; i < 8; i++) { - out_nonce[nonce_len - i - 1] = seq_copy & 0xff; - seq_copy >>= 8; + UniquePtr peer_key(key->kem->key_method->deserialize_public_key( + key->kem->key_method->alg_func(), peer_public_key, peer_public_key_len)); + if (peer_key == nullptr) { + return 0; } - - // XOR the encoded sequence with the |ctx->base_nonce|. - for (size_t i = 0; i < nonce_len; i++) { - out_nonce[i] ^= ctx->base_nonce[i]; + if (!key->kem->auth_decap(kem, Span(shared_secret), Span(enc, enc_len), + key->pkey, peer_key.get()) || + !hpke_key_schedule(ctx, HPKE_MODE_AUTH, shared_secret.data(), + shared_secret.size(), info, info_len)) { + EVP_HPKE_CTX_cleanup(ctx); + return 0; } + return 1; } int EVP_HPKE_CTX_open(EVP_HPKE_CTX *ctx, uint8_t *out, size_t *out_len, diff --git a/third_party/boringssl/src/crypto/hrss/hrss.cc b/third_party/boringssl/src/crypto/hrss/hrss.cc index 2915c70cd..1b49358b5 100644 --- a/third_party/boringssl/src/crypto/hrss/hrss.cc +++ b/third_party/boringssl/src/crypto/hrss/hrss.cc @@ -57,7 +57,7 @@ using namespace bssl; // // A couple of functions in this file can use vector operations to meaningful // effect. If we're building for a target that has a supported vector unit, -// |HRSS_HAVE_VECTOR_UNIT| will be defined and |vec_t| will be typedefed to a +// `HRSS_HAVE_VECTOR_UNIT` will be defined and `vec_t` will be typedefed to a // 128-bit vector. The following functions abstract over the differences between // NEON and SSE2 for implementing some vector operations. @@ -72,25 +72,25 @@ typedef __m128i vec_t; // vec_capable returns one iff the current platform supports SSE2. static int vec_capable() { return 1; } -// vec_add performs a pair-wise addition of four uint16s from |a| and |b|. +// vec_add performs a pair-wise addition of four uint16s from `a` and `b`. static vec_t vec_add(vec_t a, vec_t b) { return _mm_add_epi16(a, b); } -// vec_sub performs a pair-wise subtraction of four uint16s from |a| and |b|. +// vec_sub performs a pair-wise subtraction of four uint16s from `a` and `b`. static vec_t vec_sub(vec_t a, vec_t b) { return _mm_sub_epi16(a, b); } -// vec_mul multiplies each uint16_t in |a| by |b| and returns the resulting +// vec_mul multiplies each uint16_t in `a` by `b` and returns the resulting // vector. static vec_t vec_mul(vec_t a, uint16_t b) { return _mm_mullo_epi16(a, _mm_set1_epi16(b)); } -// vec_fma multiplies each uint16_t in |b| by |c|, adds the result to |a|, and +// vec_fma multiplies each uint16_t in `b` by `c`, adds the result to `a`, and // returns the resulting vector. static vec_t vec_fma(vec_t a, vec_t b, uint16_t c) { return _mm_add_epi16(a, _mm_mullo_epi16(b, _mm_set1_epi16(c))); } -// vec3_rshift_word right-shifts the 24 uint16_t's in |v| by one uint16. +// vec3_rshift_word right-shifts the 24 uint16_t's in `v` by one uint16. static void vec3_rshift_word(vec_t v[3]) { // Intel's left and right shifting is backwards compared to the order in // memory because they're based on little-endian order of words (and not just @@ -107,7 +107,7 @@ static void vec3_rshift_word(vec_t v[3]) { v[2] |= carry1; } -// vec4_rshift_word right-shifts the 32 uint16_t's in |v| by one uint16. +// vec4_rshift_word right-shifts the 32 uint16_t's in `v` by one uint16. static void vec4_rshift_word(vec_t v[4]) { // Intel's left and right shifting is backwards compared to the order in // memory because they're based on little-endian order of words (and not just @@ -128,13 +128,13 @@ static void vec4_rshift_word(vec_t v[4]) { v[3] |= carry2; } -// vec_merge_3_5 takes the final three uint16_t's from |left|, appends the first -// five from |right|, and returns the resulting vector. +// vec_merge_3_5 takes the final three uint16_t's from `left`, appends the first +// five from `right`, and returns the resulting vector. static vec_t vec_merge_3_5(vec_t left, vec_t right) { return _mm_srli_si128(left, 10) | _mm_slli_si128(right, 6); } -// poly3_vec_lshift1 left-shifts the 768 bits in |a_s|, and in |a_a|, by one +// poly3_vec_lshift1 left-shifts the 768 bits in `a_s`, and in `a_a`, by one // bit. static void poly3_vec_lshift1(vec_t a_s[6], vec_t a_a[6]) { vec_t carry_s = {0}; @@ -155,7 +155,7 @@ static void poly3_vec_lshift1(vec_t a_s[6], vec_t a_a[6]) { } } -// poly3_vec_rshift1 right-shifts the 768 bits in |a_s|, and in |a_a|, by one +// poly3_vec_rshift1 right-shifts the 768 bits in `a_s`, and in `a_a`, by one // bit. static void poly3_vec_rshift1(vec_t a_s[6], vec_t a_a[6]) { vec_t carry_s = {0}; @@ -176,15 +176,15 @@ static void poly3_vec_rshift1(vec_t a_s[6], vec_t a_a[6]) { } } -// vec_broadcast_bit duplicates the least-significant bit in |a| to all bits in +// vec_broadcast_bit duplicates the least-significant bit in `a` to all bits in // a vector and returns the result. static vec_t vec_broadcast_bit(vec_t a) { return _mm_shuffle_epi32(_mm_srai_epi32(_mm_slli_epi64(a, 63), 31), 0b01010101); } -// vec_get_word returns the |i|th uint16_t in |v|. (This is a macro because the -// compiler requires that |i| be a compile-time constant.) +// vec_get_word returns the `i`th uint16_t in `v`. (This is a macro because the +// compiler requires that `i` be a compile-time constant.) #define vec_get_word(v, i) _mm_extract_epi16(v, i) #elif (defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64)) && defined(__ARM_NEON) @@ -300,7 +300,7 @@ static void poly2_zero(struct poly2 *p) { OPENSSL_memset(&p->v[0], 0, sizeof(crypto_word_t) * WORDS_PER_POLY); } -// word_reverse returns |in| with the bits in reverse order. +// word_reverse returns `in` with the bits in reverse order. static crypto_word_t word_reverse(crypto_word_t in) { #if defined(OPENSSL_64_BIT) static const crypto_word_t kMasks[6] = { @@ -321,12 +321,12 @@ static crypto_word_t word_reverse(crypto_word_t in) { return in; } -// lsb_to_all replicates the least-significant bit of |v| to all bits of the +// lsb_to_all replicates the least-significant bit of `v` to all bits of the // word. This is used in bit-slicing operations to make a vector from a fixed // value. static crypto_word_t lsb_to_all(crypto_word_t v) { return 0u - (v & 1); } -// poly2_mod_phiN reduces |p| by Φ(N). +// poly2_mod_phiN reduces `p` by Φ(N). static void poly2_mod_phiN(struct poly2 *p) { // m is the term at x^700, replicated to every bit. const crypto_word_t m = @@ -337,8 +337,8 @@ static void poly2_mod_phiN(struct poly2 *p) { p->v[WORDS_PER_POLY - 1] &= (UINT64_C(1) << (BITS_IN_LAST_WORD - 1)) - 1; } -// poly2_reverse_700 reverses the order of the first 700 bits of |in| and writes -// the result to |out|. +// poly2_reverse_700 reverses the order of the first 700 bits of `in` and writes +// the result to `out`. static void poly2_reverse_700(struct poly2 *out, const struct poly2 *in) { struct poly2 t; for (size_t i = 0; i < WORDS_PER_POLY; i++) { @@ -353,7 +353,7 @@ static void poly2_reverse_700(struct poly2 *out, const struct poly2 *in) { out->v[WORDS_PER_POLY - 1] = t.v[0] >> shift; } -// poly2_cswap exchanges the values of |a| and |b| if |swap| is all ones. +// poly2_cswap exchanges the values of `a` and `b` if `swap` is all ones. static void poly2_cswap(struct poly2 *a, struct poly2 *b, crypto_word_t swap) { for (size_t i = 0; i < WORDS_PER_POLY; i++) { const crypto_word_t sum = swap & (a->v[i] ^ b->v[i]); @@ -362,8 +362,8 @@ static void poly2_cswap(struct poly2 *a, struct poly2 *b, crypto_word_t swap) { } } -// poly2_fmadd sets |out| to |out| + |in| * m, where m is either -// |CONSTTIME_TRUE_W| or |CONSTTIME_FALSE_W|. +// poly2_fmadd sets `out` to `out` + `in` * m, where m is either +// `CONSTTIME_TRUE_W` or `CONSTTIME_FALSE_W`. static void poly2_fmadd(struct poly2 *out, const struct poly2 *in, crypto_word_t m) { for (size_t i = 0; i < WORDS_PER_POLY; i++) { @@ -371,7 +371,7 @@ static void poly2_fmadd(struct poly2 *out, const struct poly2 *in, } } -// poly2_lshift1 left-shifts |p| by one bit. +// poly2_lshift1 left-shifts `p` by one bit. static void poly2_lshift1(struct poly2 *p) { crypto_word_t carry = 0; for (size_t i = 0; i < WORDS_PER_POLY; i++) { @@ -382,7 +382,7 @@ static void poly2_lshift1(struct poly2 *p) { } } -// poly2_rshift1 right-shifts |p| by one bit. +// poly2_rshift1 right-shifts `p` by one bit. static void poly2_rshift1(struct poly2 *p) { crypto_word_t carry = 0; for (size_t i = WORDS_PER_POLY - 1; i < WORDS_PER_POLY; i--) { @@ -402,7 +402,7 @@ static void poly2_clear_top_bits(struct poly2 *p) { // Ternary polynomials. // poly3 represents a degree-N polynomial over GF(3). Each coefficient is -// bitsliced across the |s| and |a| arrays, like this: +// bitsliced across the `s` and `a` arrays, like this: // // s | a | value // ----------------- @@ -441,14 +441,14 @@ static void poly3_zero(struct poly3 *p) { poly2_zero(&p->a); } -// poly3_reverse_700 reverses the order of the first 700 terms of |in| and -// writes them to |out|. +// poly3_reverse_700 reverses the order of the first 700 terms of `in` and +// writes them to `out`. static void poly3_reverse_700(struct poly3 *out, const struct poly3 *in) { poly2_reverse_700(&out->a, &in->a); poly2_reverse_700(&out->s, &in->s); } -// poly3_word_mul sets (|out_s|, |out_a|) to (|s1|, |a1|) × (|s2|, |a2|). +// poly3_word_mul sets (`out_s`, `out_a`) to (`s1`, `a1`) × (`s2`, `a2`). static void poly3_word_mul(crypto_word_t *out_s, crypto_word_t *out_a, const crypto_word_t s1, const crypto_word_t a1, const crypto_word_t s2, const crypto_word_t a2) { @@ -456,7 +456,7 @@ static void poly3_word_mul(crypto_word_t *out_s, crypto_word_t *out_a, *out_s = (s1 ^ s2) & *out_a; } -// poly3_word_add sets (|out_s|, |out_a|) to (|s1|, |a1|) + (|s2|, |a2|). +// poly3_word_add sets (`out_s`, `out_a`) to (`s1`, `a1`) + (`s2`, `a2`). static void poly3_word_add(crypto_word_t *out_s, crypto_word_t *out_a, const crypto_word_t s1, const crypto_word_t a1, const crypto_word_t s2, const crypto_word_t a2) { @@ -465,7 +465,7 @@ static void poly3_word_add(crypto_word_t *out_s, crypto_word_t *out_a, *out_a = (a1 ^ a2) | (t ^ s2); } -// poly3_word_sub sets (|out_s|, |out_a|) to (|s1|, |a1|) - (|s2|, |a2|). +// poly3_word_sub sets (`out_s`, `out_a`) to (`s1`, `a1`) - (`s2`, `a2`). static void poly3_word_sub(crypto_word_t *out_s, crypto_word_t *out_a, const crypto_word_t s1, const crypto_word_t a1, const crypto_word_t s2, const crypto_word_t a2) { @@ -474,7 +474,7 @@ static void poly3_word_sub(crypto_word_t *out_s, crypto_word_t *out_a, *out_a = t | (s1 ^ s2); } -// poly3_mul_const sets |p| to |p|×m, where m = (ms, ma). +// poly3_mul_const sets `p` to `p`×m, where m = (ms, ma). static void poly3_mul_const(struct poly3 *p, crypto_word_t ms, crypto_word_t ma) { ms = lsb_to_all(ms); @@ -485,7 +485,7 @@ static void poly3_mul_const(struct poly3 *p, crypto_word_t ms, } } -// poly3_fmadd sets |out| to |out| - |in|×m, where m is (ms, ma). +// poly3_fmadd sets `out` to `out` - `in`×m, where m is (ms, ma). static void poly3_fmsub(struct poly3 *out, const struct poly3 *in, crypto_word_t ms, crypto_word_t ma) { crypto_word_t product_s, product_a; @@ -502,7 +502,7 @@ static crypto_word_t final_bit_to_all(crypto_word_t v) { return lsb_to_all(v >> (BITS_IN_LAST_WORD - 1)); } -// poly3_mod_phiN reduces |p| by Φ(N). +// poly3_mod_phiN reduces `p` by Φ(N). static void poly3_mod_phiN(struct poly3 *p) { // In order to reduce by Φ(N) we subtract by the value of the greatest // coefficient. @@ -539,8 +539,8 @@ struct poly3_span { crypto_word_t *a; }; -// poly3_span_add adds |n| words of values from |a| and |b| and writes the -// result to |out|. +// poly3_span_add adds `n` words of values from `a` and `b` and writes the +// result to `out`. static void poly3_span_add(const struct poly3_span *out, const struct poly3_span *a, const struct poly3_span *b, size_t n) { @@ -549,7 +549,7 @@ static void poly3_span_add(const struct poly3_span *out, } } -// poly3_span_sub subtracts |n| words of |b| from |n| words of |a|. +// poly3_span_sub subtracts `n` words of `b` from `n` words of `a`. static void poly3_span_sub(const struct poly3_span *a, const struct poly3_span *b, size_t n) { for (size_t i = 0; i < n; i++) { @@ -557,11 +557,11 @@ static void poly3_span_sub(const struct poly3_span *a, } } -// poly3_mul_aux is a recursive function that multiplies |n| words from |a| and -// |b| and writes 2×|n| words to |out|. Each call uses 2*ceil(n/2) elements of -// |scratch| and the function recurses, except if |n| == 1, when |scratch| isn't -// used and the recursion stops. For |n| in {11, 22}, the transitive total -// amount of |scratch| needed happens to be 2n+2. +// poly3_mul_aux is a recursive function that multiplies `n` words from `a` and +// `b` and writes 2×`n` words to `out`. Each call uses 2*ceil(n/2) elements of +// `scratch` and the function recurses, except if `n` == 1, when `scratch` isn't +// used and the recursion stops. For `n` in {11, 22}, the transitive total +// amount of `scratch` needed happens to be 2n+2. static void poly3_mul_aux(const struct poly3_span *out, const struct poly3_span *scratch, const struct poly3_span *a, @@ -608,14 +608,14 @@ static void poly3_mul_aux(const struct poly3_span *out, // Karatsuba multiplication. // https://en.wikipedia.org/wiki/Karatsuba_algorithm - // When |n| is odd, the two "halves" will have different lengths. The first + // When `n` is odd, the two "halves" will have different lengths. The first // is always the smaller. const size_t low_len = n / 2; const size_t high_len = n - low_len; const struct poly3_span a_high = {&a->s[low_len], &a->a[low_len]}; const struct poly3_span b_high = {&b->s[low_len], &b->a[low_len]}; - // Store a_1 + a_0 in the first half of |out| and b_1 + b_0 in the second + // Store a1 + a0 in the first half of `out` and b1 + b0 in the second // half. const struct poly3_span a_cross_sum = *out; const struct poly3_span b_cross_sum = {&out->s[high_len], &out->a[high_len]}; @@ -634,11 +634,11 @@ static void poly3_mul_aux(const struct poly3_span *out, const struct poly3_span out_high = {&out->s[2 * low_len], &out->a[2 * low_len]}; - // Calculate (a_1 + a_0) × (b_1 + b_0) and write to scratch buffer. + // Calculate (a1 + a0) × (b1 + b0) and write to scratch buffer. poly3_mul_aux(scratch, &child_scratch, &a_cross_sum, &b_cross_sum, high_len); - // Calculate a_1 × b_1. + // Calculate a1 × b1. poly3_mul_aux(&out_high, &child_scratch, &a_high, &b_high, high_len); - // Calculate a_0 × b_0. + // Calculate a0 × b0. poly3_mul_aux(out, &child_scratch, a, b, low_len); // Subtract those last two products from the first. @@ -649,7 +649,7 @@ static void poly3_mul_aux(const struct poly3_span *out, poly3_span_add(&out_mid, &out_mid, scratch, high_len * 2); } -// HRSS_poly3_mul sets |*out| to |x|×|y| mod Φ(N). +// HRSS_poly3_mul sets `*out` to `x`×`y` mod Φ(N). void bssl::HRSS_poly3_mul(struct poly3 *out, const struct poly3 *x, const struct poly3 *y) { crypto_word_t prod_s[WORDS_PER_POLY * 2]; @@ -665,7 +665,7 @@ void bssl::HRSS_poly3_mul(struct poly3 *out, const struct poly3 *x, poly3_mul_aux(&prod_span, &scratch_span, &x_span, &y_span, WORDS_PER_POLY); - // |prod| needs to be reduced mod (𝑥^n - 1), which just involves adding the + // `prod` needs to be reduced mod (𝑥^n - 1), which just involves adding the // upper-half to the lower-half. However, N is 701, which isn't a multiple of // BITS_PER_WORD, so the upper-half vectors all have to be shifted before // being added to the lower-half. @@ -683,8 +683,8 @@ void bssl::HRSS_poly3_mul(struct poly3 *out, const struct poly3 *x, #if defined(HRSS_HAVE_VECTOR_UNIT) && !defined(OPENSSL_AARCH64) -// poly3_vec_cswap swaps (|a_s|, |a_a|) and (|b_s|, |b_a|) if |swap| is -// |0xff..ff|. Otherwise, |swap| must be zero. +// poly3_vec_cswap swaps (`a_s`, `a_a`) and (`b_s`, `b_a`) if `swap` is +// `0xff..ff`. Otherwise, `swap` must be zero. static void poly3_vec_cswap(vec_t a_s[6], vec_t a_a[6], vec_t b_s[6], vec_t b_a[6], const vec_t swap) { for (int i = 0; i < 6; i++) { @@ -698,7 +698,7 @@ static void poly3_vec_cswap(vec_t a_s[6], vec_t a_a[6], vec_t b_s[6], } } -// poly3_vec_fmsub subtracts (|ms|, |ma|) × (|b_s|, |b_a|) from (|a_s|, |a_a|). +// poly3_vec_fmsub subtracts (`ms`, `ma`) × (`b_s`, `b_a`) from (`a_s`, `a_a`). static void poly3_vec_fmsub(vec_t a_s[6], vec_t a_a[6], vec_t b_s[6], vec_t b_a[6], const vec_t ms, const vec_t ma) { for (int i = 0; i < 6; i++) { @@ -716,7 +716,7 @@ static void poly3_vec_fmsub(vec_t a_s[6], vec_t a_a[6], vec_t b_s[6], } } -// poly3_invert_vec sets |*out| to |in|^-1, i.e. such that |out|×|in| == 1 mod +// poly3_invert_vec sets `*out` to `in`^-1, i.e. such that `out`×`in` == 1 mod // Φ(N). static void poly3_invert_vec(struct poly3 *out, const struct poly3 *in) { // This algorithm is taken from section 7.1 of [SAFEGCD]. @@ -737,7 +737,7 @@ static void poly3_invert_vec(struct poly3 *out, const struct poly3 *in) { memset(f_s, 0, sizeof(f_s)); memset(f_a, 0xff, 5 * sizeof(vec_t)); memcpy(&f_a[5], kBottomSixtyOne, sizeof(kBottomSixtyOne)); - // g is the reversal of |in|. + // g is the reversal of `in`. struct poly3 in_reversed; poly3_reverse_700(&in_reversed, in); g_s[5] = kZero; @@ -782,7 +782,7 @@ static void poly3_invert_vec(struct poly3 *out, const struct poly3 *in) { #endif // HRSS_HAVE_VECTOR_UNIT -// HRSS_poly3_invert sets |*out| to |in|^-1, i.e. such that |out|×|in| == 1 mod +// HRSS_poly3_invert sets `*out` to `in`^-1, i.e. such that `out`×`in` == 1 mod // Φ(N). void bssl::HRSS_poly3_invert(struct poly3 *out, const struct poly3 *in) { // The vector version of this function seems slightly slower on AArch64, but @@ -805,7 +805,7 @@ void bssl::HRSS_poly3_invert(struct poly3 *out, const struct poly3 *in) { OPENSSL_memset(&f.s, 0, sizeof(struct poly2)); OPENSSL_memset(&f.a, 0xff, sizeof(struct poly2)); f.a.v[WORDS_PER_POLY - 1] >>= BITS_PER_WORD - BITS_IN_LAST_WORD; - // g is the reversal of |in|. + // g is the reversal of `in`. poly3_reverse_700(&g, in); int delta = 1; @@ -874,14 +874,14 @@ struct poly { }; } // namespace -// poly_normalize zeros out the excess elements of |x| which are included only +// poly_normalize zeros out the excess elements of `x` which are included only // for alignment. static void poly_normalize(struct poly *x) { OPENSSL_memset(&x->v[N], 0, 3 * sizeof(uint16_t)); } -// poly_assert_normalized asserts that the excess elements of |x| are zeroed out -// for the cases that case. (E.g. |poly_mul_vec|.) +// poly_assert_normalized asserts that the excess elements of `x` are zeroed out +// for the cases that case. (E.g. `poly_mul_vec`.) static void poly_assert_normalized(const struct poly *x) { assert(x->v[N] == 0); assert(x->v[N + 1] == 0); @@ -890,20 +890,20 @@ static void poly_assert_normalized(const struct poly *x) { namespace { // POLY_MUL_SCRATCH contains space for the working variables needed by -// |poly_mul|. The contents afterwards may be discarded, but the object may also -// be reused with future |poly_mul| calls to save heap allocations. +// `poly_mul`. The contents afterwards may be discarded, but the object may also +// be reused with future `poly_mul` calls to save heap allocations. // // This object must have 32-byte alignment. struct POLY_MUL_SCRATCH { union { - // This is used by |poly_mul_novec|. + // This is used by `poly_mul_novec`. struct { uint16_t prod[2 * N]; uint16_t scratch[1318]; } novec; #if defined(HRSS_HAVE_VECTOR_UNIT) - // This is used by |poly_mul_vec|. + // This is used by `poly_mul_vec`. struct { vec_t prod[VECS_PER_POLY * 2]; vec_t scratch[172]; @@ -911,7 +911,7 @@ struct POLY_MUL_SCRATCH { #endif #if defined(POLY_RQ_MUL_ASM) - // This is the space used by |poly_Rq_mul|. + // This is the space used by `poly_Rq_mul`. uint8_t rq[POLY_MUL_RQ_SCRATCH_SPACE]; #endif } u; @@ -920,10 +920,10 @@ struct POLY_MUL_SCRATCH { #if defined(HRSS_HAVE_VECTOR_UNIT) -// poly_mul_vec_aux is a recursive function that multiplies |n| words from |a| -// and |b| and writes 2×|n| words to |out|. Each call uses 2*ceil(n/2) elements -// of |scratch| and the function recurses, except if |n| < 3, when |scratch| -// isn't used and the recursion stops. If |n| == |VECS_PER_POLY| then |scratch| +// poly_mul_vec_aux is a recursive function that multiplies `n` words from `a` +// and `b` and writes 2×`n` words to `out`. Each call uses 2*ceil(n/2) elements +// of `scratch` and the function recurses, except if `n` < 3, when `scratch` +// isn't used and the recursion stops. If `n` == `VECS_PER_POLY` then `scratch` // needs 172 elements. static void poly_mul_vec_aux(vec_t *out, vec_t *scratch, const vec_t *a, const vec_t *b, const size_t n) { @@ -1119,14 +1119,14 @@ static void poly_mul_vec_aux(vec_t *out, vec_t *scratch, const vec_t *a, // Karatsuba multiplication. // https://en.wikipedia.org/wiki/Karatsuba_algorithm - // When |n| is odd, the two "halves" will have different lengths. The first is + // When `n` is odd, the two "halves" will have different lengths. The first is // always the smaller. const size_t low_len = n / 2; const size_t high_len = n - low_len; const vec_t *a_high = &a[low_len]; const vec_t *b_high = &b[low_len]; - // Store a_1 + a_0 in the first half of |out| and b_1 + b_0 in the second + // Store a1 + a0 in the first half of `out` and b1 + b0 in the second // half. for (size_t i = 0; i < low_len; i++) { out[i] = vec_add(a_high[i], a[i]); @@ -1138,11 +1138,11 @@ static void poly_mul_vec_aux(vec_t *out, vec_t *scratch, const vec_t *a, } vec_t *const child_scratch = &scratch[2 * high_len]; - // Calculate (a_1 + a_0) × (b_1 + b_0) and write to scratch buffer. + // Calculate (a1 + a0) × (b1 + b0) and write to scratch buffer. poly_mul_vec_aux(scratch, child_scratch, out, &out[high_len], high_len); - // Calculate a_1 × b_1. + // Calculate a1 × b1. poly_mul_vec_aux(&out[low_len * 2], child_scratch, a_high, b_high, high_len); - // Calculate a_0 × b_0. + // Calculate a0 × b0. poly_mul_vec_aux(out, child_scratch, a, b, low_len); // Subtract those last two products from the first. @@ -1161,7 +1161,7 @@ static void poly_mul_vec_aux(vec_t *out, vec_t *scratch, const vec_t *a, } } -// poly_mul_vec sets |*out| to |x|×|y| mod (𝑥^n - 1). +// poly_mul_vec sets `*out` to `x`×`y` mod (𝑥^n - 1). static void poly_mul_vec(struct POLY_MUL_SCRATCH *scratch, struct poly *out, const struct poly *x, const struct poly *y) { static_assert(sizeof(out->v) == sizeof(vec_t) * VECS_PER_POLY, @@ -1175,7 +1175,7 @@ static void poly_mul_vec(struct POLY_MUL_SCRATCH *scratch, struct poly *out, vec_t *const aux_scratch = scratch->u.vec.scratch; poly_mul_vec_aux(prod, aux_scratch, x->vectors, y->vectors, VECS_PER_POLY); - // |prod| needs to be reduced mod (𝑥^n - 1), which just involves adding the + // `prod` needs to be reduced mod (𝑥^n - 1), which just involves adding the // upper-half to the lower-half. However, N is 701, which isn't a multiple of // the vector size, so the upper-half vectors all have to be shifted before // being added to the lower-half. @@ -1192,11 +1192,11 @@ static void poly_mul_vec(struct POLY_MUL_SCRATCH *scratch, struct poly *out, #endif // HRSS_HAVE_VECTOR_UNIT -// poly_mul_novec_aux writes the product of |a| and |b| to |out|, using -// |scratch| as scratch space. It'll use Karatsuba if the inputs are large -// enough to warrant it. Each call uses 2*ceil(n/2) elements of |scratch| and -// the function recurses, except if |n| < 64, when |scratch| isn't used and the -// recursion stops. If |n| == |N| then |scratch| needs 1318 elements. +// poly_mul_novec_aux writes the product of `a` and `b` to `out`, using +// `scratch` as scratch space. It'll use Karatsuba if the inputs are large +// enough to warrant it. Each call uses 2*ceil(n/2) elements of `scratch` and +// the function recurses, except if `n` < 64, when `scratch` isn't used and the +// recursion stops. If `n` == `N` then `scratch` needs 1318 elements. static void poly_mul_novec_aux(uint16_t *out, uint16_t *scratch, const uint16_t *a, const uint16_t *b, size_t n) { static const size_t kSchoolbookLimit = 64; @@ -1214,7 +1214,7 @@ static void poly_mul_novec_aux(uint16_t *out, uint16_t *scratch, // Karatsuba multiplication. // https://en.wikipedia.org/wiki/Karatsuba_algorithm - // When |n| is odd, the two "halves" will have different lengths. The + // When `n` is odd, the two "halves" will have different lengths. The // first is always the smaller. const size_t low_len = n / 2; const size_t high_len = n - low_len; @@ -1249,7 +1249,7 @@ static void poly_mul_novec_aux(uint16_t *out, uint16_t *scratch, } } -// poly_mul_novec sets |*out| to |x|×|y| mod (𝑥^n - 1). +// poly_mul_novec sets `*out` to `x`×`y` mod (𝑥^n - 1). static void poly_mul_novec(struct POLY_MUL_SCRATCH *scratch, struct poly *out, const struct poly *x, const struct poly *y) { uint16_t *const prod = scratch->u.novec.prod; @@ -1285,7 +1285,7 @@ static void poly_mul(struct POLY_MUL_SCRATCH *scratch, struct poly *r, poly_assert_normalized(r); } -// poly_mul_x_minus_1 sets |p| to |p|×(𝑥 - 1) mod (𝑥^n - 1). +// poly_mul_x_minus_1 sets `p` to `p`×(𝑥 - 1) mod (𝑥^n - 1). static void poly_mul_x_minus_1(struct poly *p) { // Multiplying by (𝑥 - 1) means negating each coefficient and adding in // the value of the previous one. @@ -1297,7 +1297,7 @@ static void poly_mul_x_minus_1(struct poly *p) { p->v[0] = orig_final_coefficient - p->v[0]; } -// poly_mod_phiN sets |p| to |p| mod Φ(N). +// poly_mod_phiN sets `p` to `p` mod Φ(N). static void poly_mod_phiN(struct poly *p) { const uint16_t coeff700 = p->v[N - 1]; @@ -1317,7 +1317,7 @@ static void poly_clamp(struct poly *p) { // Conversion functions // -------------------- -// poly2_from_poly sets |*out| to |in| mod 2. +// poly2_from_poly sets `*out` to `in` mod 2. static void poly2_from_poly(struct poly2 *out, const struct poly *in) { crypto_word_t *words = out->v; unsigned shift = 0; @@ -1340,16 +1340,16 @@ static void poly2_from_poly(struct poly2 *out, const struct poly *in) { *words = word; } -// mod3 treats |a| as a signed number and returns |a| mod 3. +// mod3 treats `a` as a signed number and returns `a` mod 3. static uint16_t mod3(int16_t a) { const int16_t q = ((int32_t)a * 21845) >> 16; int16_t ret = a - 3 * q; - // At this point, |ret| is in {0, 1, 2, 3} and that needs to be mapped to {0, + // At this point, `ret` is in {0, 1, 2, 3} and that needs to be mapped to {0, // 1, 2, 0}. return ret & ((ret & (ret >> 1)) - 1); } -// poly3_from_poly sets |*out| to |in|. +// poly3_from_poly sets `*out` to `in`. static void poly3_from_poly(struct poly3 *out, const struct poly *in) { crypto_word_t *words_s = out->s.v; crypto_word_t *words_a = out->a.v; @@ -1385,7 +1385,7 @@ static void poly3_from_poly(struct poly3 *out, const struct poly *in) { *words_a = a; } -// poly3_from_poly_checked sets |*out| to |in|, which has coefficients in {0, 1, +// poly3_from_poly_checked sets `*out` to `in`, which has coefficients in {0, 1, // Q-1}. It returns a mask indicating whether all coefficients were found to be // in that set. static crypto_word_t poly3_from_poly_checked(struct poly3 *out, @@ -1480,7 +1480,7 @@ static void poly_from_poly3(struct poly *out, const struct poly3 *in) { // Polynomial inversion // -------------------- -// poly_invert_mod2 sets |*out| to |in^-1| (i.e. such that |*out|×|in| = 1 mod +// poly_invert_mod2 sets `*out` to `in^-1` (i.e. such that `*out`×`in` = 1 mod // Φ(N)), all mod 2. This isn't useful in itself, but is part of doing inversion // mod Q. static void poly_invert_mod2(struct poly *out, const struct poly *in) { @@ -1495,7 +1495,7 @@ static void poly_invert_mod2(struct poly *out, const struct poly *in) { // f = all ones. OPENSSL_memset(&f, 0xff, sizeof(struct poly2)); f.v[WORDS_PER_POLY - 1] >>= BITS_PER_WORD - BITS_IN_LAST_WORD; - // g is the reversal of |in|. + // g is the reversal of `in`. poly2_from_poly(&g, in); poly2_mod_phiN(&g); poly2_reverse_700(&g, &g); @@ -1531,7 +1531,7 @@ static void poly_invert_mod2(struct poly *out, const struct poly *in) { poly_assert_normalized(out); } -// poly_invert sets |*out| to |in^-1| (i.e. such that |*out|×|in| = 1 mod Φ(N)). +// poly_invert sets `*out` to `in^-1` (i.e. such that `*out`×`in` = 1 mod Φ(N)). static void poly_invert(struct POLY_MUL_SCRATCH *scratch, struct poly *out, const struct poly *in) { // Inversion mod Q, which is done based on the result of inverting mod @@ -1564,7 +1564,7 @@ static void poly_invert(struct POLY_MUL_SCRATCH *scratch, struct poly *out, #define POLY_BYTES 1138 -// poly_marshal serialises all but the final coefficient of |in| to |out|. +// poly_marshal serialises all but the final coefficient of `in` to `out`. static void poly_marshal(uint8_t out[POLY_BYTES], const struct poly *in) { const uint16_t *p = in->v; @@ -1597,10 +1597,10 @@ static void poly_marshal(uint8_t out[POLY_BYTES], const struct poly *in) { out[6] = 0xf & (p[3] >> 9); } -// poly_unmarshal parses the output of |poly_marshal| and sets |out| such that +// poly_unmarshal parses the output of `poly_marshal` and sets `out` such that // all but the final coefficients match, and the final coefficient is calculated -// such that evaluating |out| at one results in zero. It returns one on success -// or zero if |in| is an invalid encoding. +// such that evaluating `out` at one results in zero. It returns one on success +// or zero if `in` is an invalid encoding. static int poly_unmarshal(struct poly *out, const uint8_t in[POLY_BYTES]) { uint16_t *p = out->v; @@ -1651,15 +1651,15 @@ static int poly_unmarshal(struct poly *out, const uint8_t in[POLY_BYTES]) { return 1; } -// mod3_from_modQ maps {0, 1, Q-1, 65535} -> {0, 1, 2, 2}. Note that |v| may +// mod3_from_modQ maps {0, 1, Q-1, 65535} -> {0, 1, 2, 2}. Note that `v` may // have an invalid value when processing attacker-controlled inputs. static uint16_t mod3_from_modQ(uint16_t v) { v &= 3; return v ^ (v >> 1); } -// poly_marshal_mod3 marshals |in| to |out| where the coefficients of |in| are -// all in {0, 1, Q-1, 65535} and |in| is mod Φ(N). (Note that coefficients may +// poly_marshal_mod3 marshals `in` to `out` where the coefficients of `in` are +// all in {0, 1, Q-1, 65535} and `in` is mod Φ(N). (Note that coefficients may // have invalid values when processing attacker-controlled inputs.) static void poly_marshal_mod3(uint8_t out[HRSS_POLY3_BYTES], const struct poly *in) { @@ -1707,7 +1707,7 @@ static void poly_short_sample_plus(struct poly *out, poly_short_sample(out, in); // sum (and the product in the for loop) will overflow. But that's fine - // because |sum| is bound by +/- (N-2), and N < 2^15 so it works out. + // because `sum` is bound by +/- (N-2), and N < 2^15 so it works out. uint16_t sum = 0; for (unsigned i = 0; i < N - 2; i++) { sum += (unsigned)out->v[i] * out->v[i + 1]; @@ -1749,13 +1749,13 @@ static void poly_lift(struct poly *out, const struct poly *a) { // The use of z̅ is that, when working mod (𝑥^701 - 1), vz[0] = , vz[1] = , …. (Where is the inner product: the sum // of the point-wise products.) Although we calculated the inverse mod - // Φ(N), we can work mod (𝑥^N - 1) and reduce mod Φ(N) at the end. - // (That's because (𝑥^N - 1) is a multiple of Φ(N).) + // Φ(N), we can work mod (𝑥ᴺ - 1) and reduce mod Φ(N) at the end. + // (That's because (𝑥ᴺ - 1) is a multiple of Φ(N).) // - // When working mod (𝑥^N - 1), multiplication by 𝑥 is a right-rotation + // When working mod (𝑥ᴺ - 1), multiplication by 𝑥 is a right-rotation // of the list of coefficients. // - // Thus we can consider what the pattern of z̅, 𝑥z̅, 𝑥^2z̅, … looks like: + // Thus we can consider what the pattern of z̅, 𝑥z̅, 𝑥²z̅, … looks like: // // def reverse(xs): // suffix = list(xs[1:]) @@ -1779,11 +1779,11 @@ static void poly_lift(struct poly *out, const struct poly *a) { // (For a formula for z̅, see lemma two of appendix B.) // // After the first three elements have been taken care of, all then have - // a repeating three-element cycle. The next value (𝑥^3z̅) involves + // a repeating three-element cycle. The next value (𝑥³z̅) involves // three rotations of the first pattern, thus the three-element cycle // lines up. However, the discontinuity in the first three elements // obviously moves to a different position. Consider the difference - // between 𝑥^3z̅ and z̅: + // between 𝑥³z̅ and z̅: // // [x-y for (x,y) in zip(zoverbar, x3zoverbar)][:15] // [0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] @@ -1858,7 +1858,7 @@ struct private_key { // but we need 16-byte alignment. We could annotate the external struct with // that alignment but we can only assume that malloced pointers are 8-byte // aligned in any case. (Even if the underlying malloc returns values with -// 16-byte alignment, |OPENSSL_malloc| will store an 8-byte size prefix and mess +// 16-byte alignment, `OPENSSL_malloc` will store an 8-byte size prefix and mess // that up.) static struct public_key *public_key_from_external( struct HRSS_public_key *ext) { @@ -1869,7 +1869,7 @@ static struct public_key *public_key_from_external( return reinterpret_cast(align_pointer(ext->opaque, 16)); } -// private_key_from_external does the same thing as |public_key_from_external|, +// private_key_from_external does the same thing as `public_key_from_external`, // but for private keys. See the comment on that function about alignment // issues. static struct private_key *private_key_from_external( @@ -1881,8 +1881,8 @@ static struct private_key *private_key_from_external( return reinterpret_cast(align_pointer(ext->opaque, 16)); } -// malloc_align32 returns a pointer to |size| bytes of 32-byte-aligned heap and -// sets |*out_ptr| to a value that can be passed to |OPENSSL_free| to release +// malloc_align32 returns a pointer to `size` bytes of 32-byte-aligned heap and +// sets `*out_ptr` to a value that can be passed to `OPENSSL_free` to release // it. It returns NULL if out of memory. static void *malloc_align32(void **out_ptr, size_t size) { void *ptr = OPENSSL_malloc(size + 31); @@ -1915,7 +1915,7 @@ int HRSS_generate_key( if (!vars) { // If the caller ignores the return value the output will still be safe. // The private key output is randomised in case it's later passed to - // |HRSS_encap|. + // `HRSS_encap`. memset(out_pub, 0, sizeof(struct HRSS_public_key)); RAND_bytes((uint8_t *)out_priv, sizeof(struct HRSS_private_key)); return 0; @@ -2060,7 +2060,7 @@ int HRSS_decap(uint8_t out_shared_key[HRSS_KEY_BYTES], OPENSSL_memset(vars, 0xff, sizeof(struct vars)); #endif - // This is HMAC, expanded inline rather than using the |HMAC| function so that + // This is HMAC, expanded inline rather than using the `HMAC` function so that // we can avoid dealing with possible allocation failures and so keep this // function infallible. static_assert(sizeof(priv->hmac_key) <= sizeof(vars->masked_key), @@ -2119,26 +2119,26 @@ int HRSS_decap(uint8_t out_shared_key[HRSS_KEY_BYTES], ok = poly3_from_poly_checked(&vars->r3, &vars->r); // [NTRUCOMP] section 5.1 includes ReEnc2 and a proof that it's valid. Rather - // than do an expensive |poly_mul|, it rebuilds |c'| from |c - lift(m)| - // (called |b|) with: + // than do an expensive `poly_mul`, it rebuilds `c'` from `c - lift(m)` + // (called `b`) with: // t = (−b(1)/N) mod Q // c' = b + tΦ(N) + lift(m) mod Q // // When polynomials are transmitted, the final coefficient is omitted and - // |poly_unmarshal| sets it such that f(1) == 0. Thus c(1) == 0. Also, - // |poly_lift| multiplies the result by (x-1) and therefore evaluating a + // `poly_unmarshal` sets it such that f(1) == 0. Thus c(1) == 0. Also, + // `poly_lift` multiplies the result by (x-1) and therefore evaluating a // lifted polynomial at 1 is also zero. Thus lift(m)(1) == 0 and so // (c - lift(m))(1) == 0. // - // Although we defer the reduction above, |b| is conceptually reduced mod - // Φ(N). In order to do that reduction one subtracts |c[N-1]| from every - // coefficient. Therefore b(1) = -c[N-1]×N. The value of |t|, above, then is - // just recovering |c[N-1]|, and adding tΦ(N) is simply undoing the reduction. + // Although we defer the reduction above, `b` is conceptually reduced mod + // Φ(N). In order to do that reduction one subtracts `c[N-1]` from every + // coefficient. Therefore b(1) = -c[N-1]×N. The value of `t`, above, then is + // just recovering `c[N-1]`, and adding tΦ(N) is simply undoing the reduction. // Therefore b + tΦ(N) + lift(m) = c by construction and we don't need to - // recover |c| at all so long as we do the checks in - // |poly3_from_poly_checked|. + // recover `c` at all so long as we do the checks in + // `poly3_from_poly_checked`. // - // The |poly_marshal| here then is just confirming that |poly_unmarshal| is + // The `poly_marshal` here then is just confirming that `poly_unmarshal` is // strict and could be omitted. static_assert(HRSS_CIPHERTEXT_BYTES == POLY_BYTES, diff --git a/third_party/boringssl/src/crypto/hrss/internal.h b/third_party/boringssl/src/crypto/hrss/internal.h index 78d385b01..185d79ad2 100644 --- a/third_party/boringssl/src/crypto/hrss/internal.h +++ b/third_party/boringssl/src/crypto/hrss/internal.h @@ -40,7 +40,7 @@ OPENSSL_EXPORT void HRSS_poly3_invert(struct poly3 *out, // On x86-64, we can use the AVX2 code from [HRSS]. (The authors have given // explicit permission for this and signed a CLA.) However it's 57KB of object -// code, so it's not used if |OPENSSL_SMALL| is defined. +// code, so it's not used if `OPENSSL_SMALL` is defined. #if !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_SMALL) && \ defined(OPENSSL_X86_64) && defined(OPENSSL_LINUX) #define POLY_RQ_MUL_ASM diff --git a/third_party/boringssl/src/crypto/internal.h b/third_party/boringssl/src/crypto/internal.h index 7c4b509a8..f49d0a5ed 100644 --- a/third_party/boringssl/src/crypto/internal.h +++ b/third_party/boringssl/src/crypto/internal.h @@ -76,7 +76,7 @@ BSSL_NAMESPACE_BEGIN #define NEED_CPUID // OPENSSL_cpuid_setup initializes the platform-specific feature cache. This -// function should not be called directly. Call |OPENSSL_init_cpuid| instead. +// function should not be called directly. Call `OPENSSL_init_cpuid` instead. void OPENSSL_cpuid_setup(); // OPENSSL_init_cpuid initializes the platform-specific feature cache, if @@ -89,7 +89,7 @@ inline void OPENSSL_init_cpuid() {} #if (defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64)) && \ !defined(OPENSSL_STATIC_ARMCAP) // OPENSSL_get_armcap_pointer_for_test returns a pointer to -// |OPENSSL_armcap_P| for unit tests. Any modifications to the value must be +// `OPENSSL_armcap_P` for unit tests. Any modifications to the value must be // made before any other function call in BoringSSL. OPENSSL_EXPORT uint32_t *OPENSSL_get_armcap_pointer_for_test(); #endif @@ -113,7 +113,7 @@ typedef __uint128_t uint128_t; #endif #endif -// GCC-like compilers indicate SSE2 with |__SSE2__|. MSVC leaves the caller to +// GCC-like compilers indicate SSE2 with `__SSE2__`. MSVC leaves the caller to // know that x86_64 has SSE2, and uses _M_IX86_FP to indicate SSE2 on x86. // https://learn.microsoft.com/en-us/cpp/preprocessor/predefined-macros?view=msvc-170 #if defined(__SSE2__) || defined(_M_AMD64) || defined(_M_X64) || \ @@ -127,7 +127,7 @@ typedef __uint128_t uint128_t; #endif // For convenience in testing the fallback code, we allow disabling SSE2 -// intrinsics via |OPENSSL_NO_SSE2_FOR_TESTING|. We require SSE2 on x86 and +// intrinsics via `OPENSSL_NO_SSE2_FOR_TESTING`. We require SSE2 on x86 and // x86_64, so we would otherwise need to test such code on a non-x86 platform. // // This does not remove the above requirement for SSE2 support with assembly @@ -143,6 +143,15 @@ typedef __uint128_t uint128_t; #define OPENSSL_ATTR_CONST #endif +// OPENSSL_ATTR_ALWAYS_INLINE tells the compiler to always attempt inlining the +// function. This has the beneficial side effects that it inherits attributes +// for code generation (e.g. the target attribute) from its callee. +#if defined(__GNUC__) || defined(__clang__) +#define OPENSSL_ATTR_ALWAYS_INLINE __attribute__((always_inline)) +#else +#define OPENSSL_ATTR_ALWAYS_INLINE +#endif + #if defined(BORINGSSL_MALLOC_FAILURE_TESTING) // OPENSSL_reset_malloc_counter_for_testing, when malloc testing is enabled, // resets the internal malloc counter, to simulate further malloc failures. This @@ -151,7 +160,7 @@ typedef __uint128_t uint128_t; OPENSSL_EXPORT void OPENSSL_reset_malloc_counter_for_testing(); // OPENSSL_disable_malloc_failures_for_testing, when malloc testing is enabled, -// disables simulated malloc failures. Calls to |OPENSSL_malloc| will not +// disables simulated malloc failures. Calls to `OPENSSL_malloc` will not // increment the malloc counter or synthesize failures. This may be used to skip // simulating malloc failures in some region of code. OPENSSL_EXPORT void OPENSSL_disable_malloc_failures_for_testing(); @@ -174,10 +183,10 @@ inline void OPENSSL_enable_malloc_failures_for_testing() {} // Pointer utility functions. -// buffers_alias returns one if |a| and |b| alias and zero otherwise. +// buffers_alias returns one if `a` and `b` alias and zero otherwise. inline int buffers_alias(const void *a, size_t a_bytes, const void *b, size_t b_bytes) { - // Cast |a| and |b| to integers. In C, pointer comparisons between unrelated + // Cast `a` and `b` to integers. In C, pointer comparisons between unrelated // objects are undefined whereas pointer to integer conversions are merely // implementation-defined. We assume the implementation defined it in a sane // way. @@ -186,22 +195,22 @@ inline int buffers_alias(const void *a, size_t a_bytes, const void *b, return a_u + a_bytes > b_u && b_u + b_bytes > a_u; } -// spans_alias returns one if |a| and |b| alias, and zero otherwise. +// spans_alias returns one if `a` and `b` alias, and zero otherwise. template inline int spans_alias(Span a, Span b) { return buffers_alias(a.data(), a.size() * sizeof(T), b.data(), b.size() * sizeof(T)); } -// align_pointer returns |ptr|, advanced to |alignment|. |alignment| must be a -// power of two, and |ptr| must have at least |alignment - 1| bytes of scratch +// align_pointer returns `ptr`, advanced to `alignment`. `alignment` must be a +// power of two, and `ptr` must have at least `alignment - 1` bytes of scratch // space. inline void *align_pointer(void *ptr, size_t alignment) { - // |alignment| must be a power of two. + // `alignment` must be a power of two. assert(alignment != 0 && (alignment & (alignment - 1)) == 0); - // Instead of aligning |ptr| as a |uintptr_t| and casting back, compute the + // Instead of aligning `ptr` as a `uintptr_t` and casting back, compute the // offset and advance in pointer space. C guarantees that casting from pointer - // to |uintptr_t| and back gives the same pointer, but general + // to `uintptr_t` and back gives the same pointer, but general // integer-to-pointer conversions are implementation-defined. GCC does define // it in the useful way, but this makes fewer assumptions. uintptr_t offset = (0u - (uintptr_t)ptr) & (alignment - 1); @@ -229,10 +238,10 @@ inline void *align_pointer(void *ptr, size_t alignment) { // c = constant_time_select_w(lt, a, b); // crypto_word_t is the type that most constant-time functions use. Ideally we -// would like it to be |size_t|, but NaCl builds in 64-bit mode with 32-bit -// pointers, which means that |size_t| can be 32 bits when |BN_ULONG| is 64 +// would like it to be `size_t`, but NaCl builds in 64-bit mode with 32-bit +// pointers, which means that `size_t` can be 32 bits when `BN_ULONG` is 64 // bits. Since we want to be able to do constant-time operations on a -// |BN_ULONG|, |crypto_word_t| is defined as an unsigned value with the native +// `BN_ULONG`, `crypto_word_t` is defined as an unsigned value with the native // word length. #if defined(OPENSSL_64_BIT) typedef uint64_t crypto_word_t; @@ -247,11 +256,11 @@ typedef uint32_t crypto_word_t; #define CONSTTIME_TRUE_8 ((uint8_t)0xff) #define CONSTTIME_FALSE_8 ((uint8_t)0) -// value_barrier_w returns |a|, but prevents GCC and Clang from reasoning about +// value_barrier_w returns `a`, but prevents GCC and Clang from reasoning about // the returned value. This is used to mitigate compilers undoing constant-time // code, until we can express our requirements directly in the language. // -// Note the compiler is aware that |value_barrier_w| has no side effects and +// Note the compiler is aware that `value_barrier_w` has no side effects and // always has the same output for a given input. This allows it to eliminate // dead code, move computations across loops, and vectorize. inline crypto_word_t value_barrier_w(crypto_word_t a) { @@ -261,7 +270,7 @@ inline crypto_word_t value_barrier_w(crypto_word_t a) { return a; } -// value_barrier_u32 behaves like |value_barrier_w| but takes a |uint32_t|. +// value_barrier_u32 behaves like `value_barrier_w` but takes a `uint32_t`. inline uint32_t value_barrier_u32(uint32_t a) { #if defined(__GNUC__) || defined(__clang__) __asm__("" : "+r"(a) : /* no inputs */); @@ -269,7 +278,7 @@ inline uint32_t value_barrier_u32(uint32_t a) { return a; } -// value_barrier_u64 behaves like |value_barrier_w| but takes a |uint64_t|. +// value_barrier_u64 behaves like `value_barrier_w` but takes a `uint64_t`. inline uint64_t value_barrier_u64(uint64_t a) { #if defined(__GNUC__) || defined(__clang__) __asm__("" : "+r"(a) : /* no inputs */); @@ -277,7 +286,7 @@ inline uint64_t value_barrier_u64(uint64_t a) { return a; } -// |value_barrier_u8| could be defined as above, but compilers other than +// `value_barrier_u8` could be defined as above, but compilers other than // clang seem to still materialize 0x00..00MM instead of reusing 0x??..??MM. // constant_time_msb_w returns the given value with the MSB copied to all the @@ -321,7 +330,7 @@ inline crypto_word_t constant_time_lt_w(crypto_word_t a, crypto_word_t b) { return constant_time_msb_w(a ^ ((a ^ b) | ((a - b) ^ a))); } -// constant_time_lt_8 acts like |constant_time_lt_w| but returns an 8-bit +// constant_time_lt_8 acts like `constant_time_lt_w` but returns an 8-bit // mask. inline uint8_t constant_time_lt_8(crypto_word_t a, crypto_word_t b) { return (uint8_t)(constant_time_lt_w(a, b)); @@ -332,7 +341,7 @@ inline crypto_word_t constant_time_ge_w(crypto_word_t a, crypto_word_t b) { return ~constant_time_lt_w(a, b); } -// constant_time_ge_8 acts like |constant_time_ge_w| but returns an 8-bit +// constant_time_ge_8 acts like `constant_time_ge_w` but returns an 8-bit // mask. inline uint8_t constant_time_ge_8(crypto_word_t a, crypto_word_t b) { return (uint8_t)(constant_time_ge_w(a, b)); @@ -353,7 +362,7 @@ inline crypto_word_t constant_time_is_zero_w(crypto_word_t a) { return constant_time_msb_w(~a & (a - 1)); } -// constant_time_is_zero_8 acts like |constant_time_is_zero_w| but returns an +// constant_time_is_zero_8 acts like `constant_time_is_zero_w` but returns an // 8-bit mask. inline uint8_t constant_time_is_zero_8(crypto_word_t a) { return (uint8_t)(constant_time_is_zero_w(a)); @@ -364,27 +373,27 @@ inline crypto_word_t constant_time_eq_w(crypto_word_t a, crypto_word_t b) { return constant_time_is_zero_w(a ^ b); } -// constant_time_eq_8 acts like |constant_time_eq_w| but returns an 8-bit +// constant_time_eq_8 acts like `constant_time_eq_w` but returns an 8-bit // mask. inline uint8_t constant_time_eq_8(crypto_word_t a, crypto_word_t b) { return (uint8_t)(constant_time_eq_w(a, b)); } -// constant_time_eq_int acts like |constant_time_eq_w| but works on int +// constant_time_eq_int acts like `constant_time_eq_w` but works on int // values. inline crypto_word_t constant_time_eq_int(int a, int b) { return constant_time_eq_w((crypto_word_t)(a), (crypto_word_t)(b)); } -// constant_time_eq_int_8 acts like |constant_time_eq_int| but returns an 8-bit +// constant_time_eq_int_8 acts like `constant_time_eq_int` but returns an 8-bit // mask. inline uint8_t constant_time_eq_int_8(int a, int b) { return constant_time_eq_8((crypto_word_t)(a), (crypto_word_t)(b)); } -// constant_time_select_w returns (mask & a) | (~mask & b). When |mask| is all +// constant_time_select_w returns (mask & a) | (~mask & b). When `mask` is all // 1s or all 0s (as returned by the methods above), the select methods return -// either |a| (if |mask| is nonzero) or |b| (if |mask| is zero). +// either `a` (if `mask` is nonzero) or `b` (if `mask` is zero). inline crypto_word_t constant_time_select_w(crypto_word_t mask, crypto_word_t a, crypto_word_t b) { // Clang recognizes this pattern as a select. While it usually transforms it @@ -396,28 +405,28 @@ inline crypto_word_t constant_time_select_w(crypto_word_t mask, crypto_word_t a, return (mask & a) | (~mask & b); } -// constant_time_select_8 acts like |constant_time_select| but operates on +// constant_time_select_8 acts like `constant_time_select` but operates on // 8-bit values. inline uint8_t constant_time_select_8(crypto_word_t mask, uint8_t a, uint8_t b) { - // |mask| is a word instead of |uint8_t| to avoid materializing 0x000..0MM - // Making both |mask| and its value barrier |uint8_t| would allow the compiler + // `mask` is a word instead of `uint8_t` to avoid materializing 0x000..0MM + // Making both `mask` and its value barrier `uint8_t` would allow the compiler // to materialize 0x????..?MM instead, but only clang is that clever. // However, vectorization of bitwise operations seems to work better on - // |uint8_t| than a mix of |uint64_t| and |uint8_t|, so |m| is cast to - // |uint8_t| after the value barrier but before the bitwise operations. + // `uint8_t` than a mix of `uint64_t` and `uint8_t`, so `m` is cast to + // `uint8_t` after the value barrier but before the bitwise operations. uint8_t m = value_barrier_w(mask); return (m & a) | (~m & b); } -// constant_time_select_int acts like |constant_time_select| but operates on +// constant_time_select_int acts like `constant_time_select` but operates on // ints. inline int constant_time_select_int(crypto_word_t mask, int a, int b) { return static_cast(constant_time_select_w( mask, static_cast(a), static_cast(b))); } -// constant_time_select_32 acts like |constant_time_select| but operates on +// constant_time_select_32 acts like `constant_time_select` but operates on // 32-bit values. inline uint32_t constant_time_select_32(crypto_word_t mask, uint32_t a, uint32_t b) { @@ -425,9 +434,9 @@ inline uint32_t constant_time_select_32(crypto_word_t mask, uint32_t a, constant_time_select_w(mask, crypto_word_t{a}, crypto_word_t{b})); } -// constant_time_conditional_memcpy copies |n| bytes from |src| to |dst| if -// |mask| is 0xff..ff and does nothing if |mask| is 0. The |n|-byte memory -// ranges at |dst| and |src| must not overlap, as when calling |memcpy|. +// constant_time_conditional_memcpy copies `n` bytes from `src` to `dst` if +// `mask` is 0xff..ff and does nothing if `mask` is 0. The `n`-byte memory +// ranges at `dst` and `src` must not overlap, as when calling `memcpy`. inline void constant_time_conditional_memcpy(void *dst, const void *src, const size_t n, const crypto_word_t mask) { @@ -439,17 +448,19 @@ inline void constant_time_conditional_memcpy(void *dst, const void *src, } } -// constant_time_conditional_memxor xors |n| bytes from |src| to |dst| if -// |mask| is 0xff..ff and does nothing if |mask| is 0. The |n|-byte memory -// ranges at |dst| and |src| must not overlap, as when calling |memcpy|. +// constant_time_conditional_memxor xors `n` bytes from `src` to `dst` if +// `mask` is 0xff..ff and does nothing if `mask` is 0. The `n`-byte memory +// ranges at `dst` and `src` must not overlap, as when calling `memcpy`. inline void constant_time_conditional_memxor(void *dst, const void *src, size_t n, const crypto_word_t mask) { assert(!buffers_alias(dst, n, src, n)); uint8_t *out = (uint8_t *)dst; const uint8_t *in = (const uint8_t *)src; -#if defined(__GNUC__) && !defined(__clang__) +#if defined(__GNUC__) || defined(__clang__) // gcc 13.2.0 doesn't automatically vectorize this loop regardless of barrier + // clang 16.0.6 vectorizes if alias analysis finds src and dst are disjoint, + // but does not conclude that from the !buffers_alias assert. typedef uint8_t v32u8 __attribute__((vector_size(32), aligned(1), may_alias)); size_t n_vec = n & ~(size_t)31; v32u8 masks = ((uint8_t)mask - (v32u8){}); // broadcast @@ -495,9 +506,9 @@ inline void constant_time_conditional_memxor(void *dst, const void *src, #endif // BORINGSSL_CONSTANT_TIME_VALIDATION inline crypto_word_t constant_time_declassify_w(crypto_word_t v) { - // Return |v| through a value barrier to be safe. Valgrind-based constant-time + // Return `v` through a value barrier to be safe. Valgrind-based constant-time // validation is partly to check the compiler has not undone any constant-time - // work. Any place |BORINGSSL_CONSTANT_TIME_VALIDATION| influences + // work. Any place `BORINGSSL_CONSTANT_TIME_VALIDATION` influences // optimizations, this validation is inaccurate. // // However, by sending pointers through valgrind, we likely inhibit escape @@ -518,8 +529,8 @@ inline int constant_time_declassify_int(int v) { return value_barrier_u32(v); } -// declassify_assert behaves like |assert| but declassifies the result of -// evaluating |expr|. This allows the assertion to branch on the (presumably +// declassify_assert behaves like `assert` but declassifies the result of +// evaluating `expr`. This allows the assertion to branch on the (presumably // public) result, but still ensures that values leading up to the computation // were secret. #define declassify_assert(expr) assert(constant_time_declassify_int(expr)) @@ -540,13 +551,13 @@ typedef pthread_once_t CRYPTO_once_t; #error "Unknown threading library" #endif -// CRYPTO_once calls |init| exactly once per process. This is thread-safe: if -// concurrent threads call |CRYPTO_once| with the same |CRYPTO_once_t| argument -// then they will block until |init| completes, but |init| will have only been +// CRYPTO_once calls `init` exactly once per process. This is thread-safe: if +// concurrent threads call `CRYPTO_once` with the same `CRYPTO_once_t` argument +// then they will block until `init` completes, but `init` will have only been // called once. // -// The |once| argument must be a |CRYPTO_once_t| that has been initialised with -// the value |CRYPTO_ONCE_INIT|. +// The `once` argument must be a `CRYPTO_once_t` that has been initialised with +// the value `CRYPTO_ONCE_INIT`. OPENSSL_EXPORT void CRYPTO_once(CRYPTO_once_t *once, void (*init)()); @@ -606,20 +617,20 @@ class Atomic { using CRYPTO_refcount_t = Atomic; -// CRYPTO_refcount_inc atomically increments the value at |*count| unless the +// CRYPTO_refcount_inc atomically increments the value at `*count` unless the // value would overflow. It's safe for multiple threads to concurrently call -// this or |CRYPTO_refcount_dec_and_test_zero| on the same -// |CRYPTO_refcount_t|. +// this or `CRYPTO_refcount_dec_and_test_zero` on the same +// `CRYPTO_refcount_t`. OPENSSL_EXPORT void CRYPTO_refcount_inc(CRYPTO_refcount_t *count); -// CRYPTO_refcount_dec_and_test_zero tests the value at |*count|: +// CRYPTO_refcount_dec_and_test_zero tests the value at `*count`: // if it's zero, it crashes the address space. // if it's the maximum value, it returns zero. // otherwise, it atomically decrements it and returns one iff the resulting // value is zero. // // It's safe for multiple threads to concurrently call this or -// |CRYPTO_refcount_inc| on the same |CRYPTO_refcount_t|. +// `CRYPTO_refcount_inc` on the same `CRYPTO_refcount_t`. OPENSSL_EXPORT int CRYPTO_refcount_dec_and_test_zero(CRYPTO_refcount_t *count); @@ -646,7 +657,7 @@ class OPENSSL_EXPORT StaticMutex { // UnlockWrite releases a write lock. void UnlockWrite(); -protected: + protected: #if !defined(OPENSSL_THREADS) // Nothing. #elif defined(OPENSSL_WINDOWS_THREADS) @@ -658,7 +669,7 @@ class OPENSSL_EXPORT StaticMutex { #endif }; -class OPENSSL_EXPORT Mutex : public StaticMutex { +class OPENSSL_EXPORT Mutex : public StaticMutex { public: constexpr Mutex() = default; ~Mutex(); @@ -718,18 +729,18 @@ OPENSSL_EXPORT void *CRYPTO_get_thread_local(thread_local_data_t value); // CRYPTO_set_thread_local sets a pointer value for the current thread at the // given index. This function should only be called once per thread for a given -// |index|: rather than update the pointer value itself, update the data that +// `index`: rather than update the pointer value itself, update the data that // is pointed to. // // The destructor function will be called when a thread exits to free this -// thread-local data. All calls to |CRYPTO_set_thread_local| with the same -// |index| should have the same |destructor| argument. The destructor may be +// thread-local data. All calls to `CRYPTO_set_thread_local` with the same +// `index` should have the same `destructor` argument. The destructor may be // called with a NULL argument if a thread that never set a thread-local -// pointer for |index|, exits. The destructor may be called concurrently with +// pointer for `index`, exits. The destructor may be called concurrently with // different arguments. // // This function returns one on success or zero on error. If it returns zero -// then |destructor| has been called with |value| already. +// then `destructor` has been called with `value` already. OPENSSL_EXPORT int CRYPTO_set_thread_local( thread_local_data_t index, void *value, thread_local_destructor_t destructor); @@ -755,23 +766,23 @@ struct ExDataClass { : num_reserved(with_app_data ? 1 : 0) {} StaticMutex lock; - // funcs is a linked list of |ExDataFuncs| structures. It may be traversed - // without serialization only up to |num_funcs|. last points to the final - // entry of |funcs|, or nullptr if empty. + // funcs is a linked list of `ExDataFuncs` structures. It may be traversed + // without serialization only up to `num_funcs`. last points to the final + // entry of `funcs`, or nullptr if empty. ExDataFuncs *funcs = nullptr, *last = nullptr; - // num_funcs is the number of entries in |funcs|. + // num_funcs is the number of entries in `funcs`. Atomic num_funcs = 0; // num_reserved is one if the ex_data index zero is reserved for legacy - // |TYPE_get_app_data| functions. + // `TYPE_get_app_data` functions. uint8_t num_reserved = 0; }; -// CRYPTO_get_ex_new_index_ex allocates a new index for |ex_data_class|. Each +// CRYPTO_get_ex_new_index_ex allocates a new index for `ex_data_class`. Each // class of object should provide a wrapper function that uses the correct -// |ExDataClass|. It returns the new index on success and -1 on error. -OPENSSL_EXPORT int CRYPTO_get_ex_new_index_ex( - ExDataClass *ex_data_class, long argl, void *argp, - CRYPTO_EX_free *free_func); +// `ExDataClass`. It returns the new index on success and -1 on error. +OPENSSL_EXPORT int CRYPTO_get_ex_new_index_ex(ExDataClass *ex_data_class, + long argl, void *argp, + CRYPTO_EX_free *free_func); // CRYPTO_set_ex_data sets an extra data pointer on a given object. Each class // of object should provide a wrapper function. @@ -782,10 +793,10 @@ OPENSSL_EXPORT int CRYPTO_set_ex_data(CRYPTO_EX_DATA *ad, int index, void *val); // function. OPENSSL_EXPORT void *CRYPTO_get_ex_data(const CRYPTO_EX_DATA *ad, int index); -// CRYPTO_new_ex_data initialises a newly allocated |CRYPTO_EX_DATA|. +// CRYPTO_new_ex_data initialises a newly allocated `CRYPTO_EX_DATA`. OPENSSL_EXPORT void CRYPTO_new_ex_data(CRYPTO_EX_DATA *ad); -// CRYPTO_free_ex_data frees |ad|, which is an object of the given class. +// CRYPTO_free_ex_data frees `ad`, which is an object of the given class. OPENSSL_EXPORT void CRYPTO_free_ex_data(ExDataClass *ex_data_class, CRYPTO_EX_DATA *ad); @@ -831,9 +842,9 @@ inline uint64_t CRYPTO_bswap8(uint64_t x) { // These wrapper functions behave the same as the corresponding C standard // functions, but behave as expected when passed NULL if the length is zero. // -// Note |OPENSSL_memcmp| is a different function from |CRYPTO_memcmp|. +// Note `OPENSSL_memcmp` is a different function from `CRYPTO_memcmp`. -// C++ defines |memchr| as a const-correct overload. +// C++ defines `memchr` as a const-correct overload. inline const void *OPENSSL_memchr(const void *s, int c, size_t n) { if (n == 0) { return nullptr; @@ -886,7 +897,7 @@ inline void *OPENSSL_memset(void *dst, int c, size_t n) { // Loads and stores. // // The following functions load and store sized integers with the specified -// endianness. They use |memcpy|, and so avoid alignment or strict aliasing +// endianness. They use `memcpy`, and so avoid alignment or strict aliasing // requirements on the input and output pointers. inline uint16_t CRYPTO_load_u16_le(const void *in) { @@ -977,7 +988,7 @@ inline crypto_word_t CRYPTO_load_word_be(const void *in) { // Bit rotation functions. // -// Note these functions use |(-shift) & 31|, etc., because shifting by the bit +// Note these functions use `(-shift) & 31`, etc., because shifting by the bit // width is undefined. Both Clang and GCC recognize this pattern as a rotation, // but MSVC does not. Instead, we call MSVC's built-in functions. @@ -1014,6 +1025,60 @@ inline uint64_t CRYPTO_rotr_u64(uint64_t value, int shift) { } +// Bit math functions. +// +// Polyfills for C++20. + +// CRYPTO_bit_width returns the smallest number of bits needed to represent `n`. +// It returns zero if `n` is zero. +inline constexpr int CRYPTO_bit_width(uint64_t n) { +#if OPENSSL_HAS_BUILTIN(__builtin_clzll) + static_assert(sizeof(unsigned long long) >= sizeof(uint64_t)); + return n ? (sizeof(unsigned long long) * 8 - __builtin_clzll(n)) : 0; +#else + int width = 0; + while (n > 0) { + ++width; + n >>= 1; + } + return width; +#endif +} + +// CRYPTO_popcount returns the number of set bits in `n`'s binary +// representation. +inline int CRYPTO_popcount(uint64_t n) { +#if OPENSSL_HAS_BUILTIN(__builtin_popcountll) + static_assert(sizeof(unsigned long long) >= sizeof(uint64_t)); + return __builtin_popcountll(n); +#else + int count = 0; + while (n > 0) { + n &= (n - 1); // Clears the least significant bit that is set. + ++count; + } + return count; +#endif +} + +// CRYPTO_bit_ceil returns the smallest power of 2 that is greater than or equal +// to `n`. +inline uint64_t CRYPTO_bit_ceil(uint64_t n) { + return n ? uint64_t{1} << CRYPTO_bit_width(n - 1) : uint64_t{1}; +} + +// CRYPTO_bit_floor returns the largest power of 2 that is less than or equal to +// `n`. It returns zero if `n` is zero. +inline uint64_t CRYPTO_bit_floor(uint64_t n) { + return n ? (uint64_t{1} << (CRYPTO_bit_width(n) - 1)) : uint64_t{0}; +} + +// CRYPTO_has_single_bit returns whether `n` is an integral power of 2. +inline bool CRYPTO_has_single_bit(uint64_t n) { + return CRYPTO_popcount(n) == 1; +} + + // FIPS functions. #if defined(BORINGSSL_FIPS) @@ -1025,7 +1090,7 @@ void BORINGSSL_FIPS_abort() __attribute__((noreturn)); // boringssl_self_test_startup runs all startup self tests and returns one on // success or zero on error. Startup self tests do not include lazy tests. -// Call |BORINGSSL_self_test| to run every self test. +// Call `BORINGSSL_self_test` to run every self test. int boringssl_self_test_startup(); // boringssl_ensure_rsa_sign_self_test checks whether the RSA signing self-test @@ -1059,7 +1124,7 @@ inline void boringssl_ensure_ffdh_self_test() {} #endif // FIPS -// BORINGSSL_check_test checks that |expected| and |actual| are equal. It +// BORINGSSL_check_test checks that `expected` and `actual` are equal. It // returns 1 on success and, on failure, it prints an error message that // includes the hexdumps the two values and returns 0. int BORINGSSL_check_test(Span expected, @@ -1121,16 +1186,16 @@ inline int boringssl_fips_break_test(const char *test) { return 0; } // and AVX512 bits in XCR0, so it is not necessary to check those. (WARNING: See // caveats in cpu_intel.c.) // -// This symbol should only be accessed with |OPENSSL_get_ia32cap|. +// This symbol should only be accessed with `OPENSSL_get_ia32cap`. extern uint32_t OPENSSL_ia32cap_P[4]; -// OPENSSL_get_ia32cap initializes the library if needed and returns the |idx|th -// entry of |OPENSSL_ia32cap_P|. It is marked as a const function so duplicate +// OPENSSL_get_ia32cap initializes the library if needed and returns the `idx`th +// entry of `OPENSSL_ia32cap_P`. It is marked as a const function so duplicate // calls can be merged by the compiler, at least when indices match. OPENSSL_ATTR_CONST uint32_t OPENSSL_get_ia32cap(int idx); -// OPENSSL_adjust_ia32cap adjusts |cap|, which should contain -// |OPENSSL_ia32cap_P|, based on the environment variable value in |env|. This +// OPENSSL_adjust_ia32cap adjusts `cap`, which should contain +// `OPENSSL_ia32cap_P`, based on the environment variable value in `env`. This // function is exposed for unit tests. void OPENSSL_adjust_ia32cap(uint32_t cap[4], const char *env); @@ -1183,8 +1248,8 @@ inline int CRYPTO_is_AESNI_capable() { #endif } -// We intentionally avoid defining a |CRYPTO_is_XSAVE_capable| function. See -// |CRYPTO_cpu_perf_is_like_silvermont|. +// We intentionally avoid defining a `CRYPTO_is_XSAVE_capable` function. See +// `CRYPTO_cpu_perf_is_like_silvermont`. inline int CRYPTO_is_AVX_capable() { #if defined(__AVX__) @@ -1195,7 +1260,7 @@ inline int CRYPTO_is_AVX_capable() { } inline int CRYPTO_is_RDRAND_capable() { - // We intentionally do not check |__RDRND__| here. On some AMD processors, we + // We intentionally do not check `__RDRND__` here. On some AMD processors, we // will act as if the hardware is RDRAND-incapable, even it actually supports // it. See cpu_intel.c. return (OPENSSL_get_ia32cap(1) & (1u << 30)) != 0; @@ -1334,11 +1399,11 @@ inline int CRYPTO_is_VPCLMULQDQ_capable() { #define ARMV8_SHA3 (1 << 7) #if defined(OPENSSL_STATIC_ARMCAP) -// We assume |CRYPTO_is_*_capable| already checked static capabilities. +// We assume `CRYPTO_is_*_capable` already checked static capabilities. inline uint32_t OPENSSL_get_armcap() { return 0; } #else // OPENSSL_armcap_P contains ARM CPU capabilities as a bitmask of the above -// constants. This should only be accessed with |OPENSSL_get_armcap|. +// constants. This should only be accessed with `OPENSSL_get_armcap`. extern uint32_t OPENSSL_armcap_P; // OPENSSL_get_armcap initializes the library if needed and returns ARM CPU @@ -1389,7 +1454,7 @@ inline int CRYPTO_is_ARMv8_PMULL_capable() { } inline int CRYPTO_is_ARMv8_SHA1_capable() { - // SHA-1 and SHA-2 (only) share |__ARM_FEATURE_SHA2| but otherwise + // SHA-1 and SHA-2 (only) share `__ARM_FEATURE_SHA2` but otherwise // are dealt with independently. #if defined(OPENSSL_STATIC_ARMCAP_SHA1) || defined(__ARM_FEATURE_SHA2) return 1; @@ -1399,7 +1464,7 @@ inline int CRYPTO_is_ARMv8_SHA1_capable() { } inline int CRYPTO_is_ARMv8_SHA256_capable() { - // SHA-1 and SHA-2 (only) share |__ARM_FEATURE_SHA2| but otherwise + // SHA-1 and SHA-2 (only) share `__ARM_FEATURE_SHA2` but otherwise // are dealt with independently. #if defined(OPENSSL_STATIC_ARMCAP_SHA256) || defined(__ARM_FEATURE_SHA2) return 1; @@ -1409,7 +1474,7 @@ inline int CRYPTO_is_ARMv8_SHA256_capable() { } inline int CRYPTO_is_ARMv8_SHA512_capable() { - // There is no |OPENSSL_STATIC_ARMCAP_SHA512|. + // There is no `OPENSSL_STATIC_ARMCAP_SHA512`. #if defined(__ARM_FEATURE_SHA512) return 1; #else @@ -1418,7 +1483,7 @@ inline int CRYPTO_is_ARMv8_SHA512_capable() { } inline int CRYPTO_is_ARMv8_SHA3_capable() { - // There is no |OPENSSL_STATIC_ARMCAP_SHA3|. + // There is no `OPENSSL_STATIC_ARMCAP_SHA3`. #if defined(__ARM_FEATURE_SHA3) return 1; #else @@ -1446,10 +1511,10 @@ extern "C" uint8_t BORINGSSL_function_hit[8]; #endif // BORINGSSL_DISPATCH_TEST -// OPENSSL_vasprintf_internal is just like |vasprintf(3)|. If |system_malloc| is -// 0, memory will be allocated with |OPENSSL_malloc| and must be freed with -// |OPENSSL_free|. Otherwise the system |malloc| function is used and the memory -// must be freed with the system |free| function. +// OPENSSL_vasprintf_internal is just like `vasprintf(3)`. If `system_malloc` is +// 0, memory will be allocated with `OPENSSL_malloc` and must be freed with +// `OPENSSL_free`. Otherwise the system `malloc` function is used and the memory +// must be freed with the system `free` function. OPENSSL_EXPORT int OPENSSL_vasprintf_internal(char **str, const char *format, va_list args, int system_malloc) OPENSSL_PRINTF_FORMAT_FUNC(2, 0); @@ -1459,7 +1524,7 @@ OPENSSL_EXPORT int OPENSSL_vasprintf_internal(char **str, const char *format, #if defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION) // CRYPTO_fuzzer_mode_enabled returns whether fuzzer mode is enabled. See -// |CRYPTO_set_fuzzer_mode|. In non-fuzzer builds, this function statically +// `CRYPTO_set_fuzzer_mode`. In non-fuzzer builds, this function statically // returns zero so the codepaths will be deleted by the optimizer. int CRYPTO_fuzzer_mode_enabled(); #else @@ -1469,8 +1534,8 @@ inline int CRYPTO_fuzzer_mode_enabled() { return 0; } // Arithmetic functions. -// CRYPTO_addc_* returns |x + y + carry|, and sets |*out_carry| to the carry -// bit. |carry| must be zero or one. +// CRYPTO_addc_* returns `x + y + carry`, and sets `*out_carry` to the carry +// bit. `carry` must be zero or one. // NOTE: Unoptimized GCC builds may compile these builtins to non-constant-time // code. For correct constant-time behavior, ensure builds are optimized. @@ -1546,8 +1611,8 @@ inline uint64_t CRYPTO_addc_u64(uint64_t x, uint64_t y, uint64_t carry, #endif -// CRYPTO_subc_* returns |x - y - borrow|, and sets |*out_borrow| to the borrow -// bit. |borrow| must be zero or one. +// CRYPTO_subc_* returns `x - y - borrow`, and sets `*out_borrow` to the borrow +// bit. `borrow` must be zero or one. #if OPENSSL_HAS_BUILTIN(__builtin_subc) inline unsigned int CRYPTO_subc_impl(unsigned int x, unsigned int y, @@ -1640,13 +1705,13 @@ class Cleanup { template Cleanup(F func) -> Cleanup; -// DECLARE_OPAQUE_STRUCT defines a public struct |public_name| with an -// implementation struct |impl_name|. +// DECLARE_OPAQUE_STRUCT defines a public struct `public_name` with an +// implementation struct `impl_name`. // -// To prevent accidents, the |public_name| struct will be neither constructable, +// To prevent accidents, the `public_name` struct will be neither constructable, // nor copyable/movable, nor deletable. // -// It must be used from inside the |bssl| namespace; however, |public_name| will +// It must be used from inside the `bssl` namespace; however, `public_name` will // be defined outside. // // Usage: @@ -1666,8 +1731,8 @@ Cleanup(F func) -> Cleanup; // // The implementation struct can be converted to the public struct implicitly; // to convert the public struct to the implementation struct, call -// |FromOpaque| on it. It is explicitly allowed to call |FromOpaque| on a -// |nullptr|. +// `FromOpaque` on it. It is explicitly allowed to call `FromOpaque` on a +// `nullptr`. #define DECLARE_OPAQUE_STRUCT(public_name, impl_name) \ BSSL_NAMESPACE_BEGIN \ class impl_name; \ diff --git a/third_party/boringssl/src/crypto/kyber/internal.h b/third_party/boringssl/src/crypto/kyber/internal.h deleted file mode 100644 index 21ec5afdd..000000000 --- a/third_party/boringssl/src/crypto/kyber/internal.h +++ /dev/null @@ -1,165 +0,0 @@ -// Copyright 2023 The BoringSSL Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// https://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#ifndef OPENSSL_HEADER_CRYPTO_KYBER_INTERNAL_H -#define OPENSSL_HEADER_CRYPTO_KYBER_INTERNAL_H - -#include - - -BSSL_NAMESPACE_BEGIN - -// Kyber is the pre-standard version of ML-KEM. This was once exported as public -// API, but is now internal and only used by libssl. It will be removed entirely -// in the future. -// -// This implements the round-3 specification of Kyber, defined at -// https://pq-crystals.org/kyber/data/kyber-specification-round3-20210804.pdf - -// KYBER_public_key contains a Kyber768 public key. The contents of this -// object should never leave the address space since the format is unstable. -struct KYBER_public_key { - union { - uint8_t bytes[512 * (3 + 9) + 32 + 32]; - uint16_t alignment; - } opaque; -}; - -// KYBER_private_key contains a Kyber768 private key. The contents of this -// object should never leave the address space since the format is unstable. -struct KYBER_private_key { - union { - uint8_t bytes[512 * (3 + 3 + 9) + 32 + 32 + 32]; - uint16_t alignment; - } opaque; -}; - -// KYBER_PUBLIC_KEY_BYTES is the number of bytes in an encoded Kyber768 public -// key. -#define KYBER_PUBLIC_KEY_BYTES 1184 - -// KYBER_SHARED_SECRET_BYTES is the number of bytes in the Kyber768 shared -// secret. Although the round-3 specification has a variable-length output, the -// final ML-KEM construction is expected to use a fixed 32-byte output. To -// simplify the future transition, we apply the same restriction. -#define KYBER_SHARED_SECRET_BYTES 32 - -// KYBER_generate_key generates a random public/private key pair, writes the -// encoded public key to |out_encoded_public_key| and sets |out_private_key| to -// the private key. -OPENSSL_EXPORT void KYBER_generate_key( - uint8_t out_encoded_public_key[KYBER_PUBLIC_KEY_BYTES], - struct KYBER_private_key *out_private_key); - -// KYBER_public_from_private sets |*out_public_key| to the public key that -// corresponds to |private_key|. (This is faster than parsing the output of -// |KYBER_generate_key| if, for some reason, you need to encapsulate to a key -// that was just generated.) -OPENSSL_EXPORT void KYBER_public_from_private( - struct KYBER_public_key *out_public_key, - const struct KYBER_private_key *private_key); - -// KYBER_CIPHERTEXT_BYTES is number of bytes in the Kyber768 ciphertext. -#define KYBER_CIPHERTEXT_BYTES 1088 - -// KYBER_encap encrypts a random shared secret for |public_key|, writes the -// ciphertext to |out_ciphertext|, and writes the random shared secret to -// |out_shared_secret|. -OPENSSL_EXPORT void KYBER_encap( - uint8_t out_ciphertext[KYBER_CIPHERTEXT_BYTES], - uint8_t out_shared_secret[KYBER_SHARED_SECRET_BYTES], - const struct KYBER_public_key *public_key); - -// KYBER_decap decrypts a shared secret from |ciphertext| using |private_key| -// and writes it to |out_shared_secret|. If |ciphertext| is invalid, -// |out_shared_secret| is filled with a key that will always be the same for the -// same |ciphertext| and |private_key|, but which appears to be random unless -// one has access to |private_key|. These alternatives occur in constant time. -// Any subsequent symmetric encryption using |out_shared_secret| must use an -// authenticated encryption scheme in order to discover the decapsulation -// failure. -OPENSSL_EXPORT void KYBER_decap( - uint8_t out_shared_secret[KYBER_SHARED_SECRET_BYTES], - const uint8_t ciphertext[KYBER_CIPHERTEXT_BYTES], - const struct KYBER_private_key *private_key); - - -// Serialisation of keys. - -// KYBER_marshal_public_key serializes |public_key| to |out| in the standard -// format for Kyber public keys. It returns one on success or zero on allocation -// error. -OPENSSL_EXPORT int KYBER_marshal_public_key( - CBB *out, const struct KYBER_public_key *public_key); - -// KYBER_parse_public_key parses a public key, in the format generated by -// |KYBER_marshal_public_key|, from |in| and writes the result to -// |out_public_key|. It returns one on success or zero on parse error or if -// there are trailing bytes in |in|. -OPENSSL_EXPORT int KYBER_parse_public_key( - struct KYBER_public_key *out_public_key, CBS *in); - -// KYBER_marshal_private_key serializes |private_key| to |out| in the standard -// format for Kyber private keys. It returns one on success or zero on -// allocation error. -OPENSSL_EXPORT int KYBER_marshal_private_key( - CBB *out, const struct KYBER_private_key *private_key); - -// KYBER_PRIVATE_KEY_BYTES is the length of the data produced by -// |KYBER_marshal_private_key|. -#define KYBER_PRIVATE_KEY_BYTES 2400 - -// KYBER_parse_private_key parses a private key, in the format generated by -// |KYBER_marshal_private_key|, from |in| and writes the result to -// |out_private_key|. It returns one on success or zero on parse error or if -// there are trailing bytes in |in|. -OPENSSL_EXPORT int KYBER_parse_private_key( - struct KYBER_private_key *out_private_key, CBS *in); - - -// Internal symbols. - -// KYBER_ENCAP_ENTROPY is the number of bytes of uniformly random entropy -// necessary to encapsulate a secret. The entropy will be leaked to the -// decapsulating party. -#define KYBER_ENCAP_ENTROPY 32 - -// KYBER_GENERATE_KEY_ENTROPY is the number of bytes of uniformly random entropy -// necessary to generate a key. -#define KYBER_GENERATE_KEY_ENTROPY 64 - -// KYBER_generate_key_external_entropy is a deterministic function to create a -// pair of Kyber768 keys, using the supplied entropy. The entropy needs to be -// uniformly random generated. This function is should only be used for tests, -// regular callers should use the non-deterministic |KYBER_generate_key| -// directly. -OPENSSL_EXPORT void KYBER_generate_key_external_entropy( - uint8_t out_encoded_public_key[KYBER_PUBLIC_KEY_BYTES], - struct KYBER_private_key *out_private_key, - const uint8_t entropy[KYBER_GENERATE_KEY_ENTROPY]); - -// KYBER_encap_external_entropy behaves like |KYBER_encap|, but uses -// |KYBER_ENCAP_ENTROPY| bytes of |entropy| for randomization. The decapsulating -// side will be able to recover |entropy| in full. This function should only be -// used for tests, regular callers should use the non-deterministic -// |KYBER_encap| directly. -OPENSSL_EXPORT void KYBER_encap_external_entropy( - uint8_t out_ciphertext[KYBER_CIPHERTEXT_BYTES], - uint8_t out_shared_secret[KYBER_SHARED_SECRET_BYTES], - const struct KYBER_public_key *public_key, - const uint8_t entropy[KYBER_ENCAP_ENTROPY]); - -BSSL_NAMESPACE_END - -#endif // OPENSSL_HEADER_CRYPTO_KYBER_INTERNAL_H diff --git a/third_party/boringssl/src/crypto/kyber/kyber.cc b/third_party/boringssl/src/crypto/kyber/kyber.cc deleted file mode 100644 index f35ca558b..000000000 --- a/third_party/boringssl/src/crypto/kyber/kyber.cc +++ /dev/null @@ -1,870 +0,0 @@ -// Copyright 2023 The BoringSSL Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// https://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include -#include - -#include -#include - -#include "../fipsmodule/keccak/internal.h" -#include "../internal.h" -#include "./internal.h" - - -// See -// https://pq-crystals.org/kyber/data/kyber-specification-round3-20210804.pdf - -using namespace bssl; - -static void prf(uint8_t *out, size_t out_len, const uint8_t in[33]) { - BORINGSSL_keccak(out, out_len, in, 33, boringssl_shake256); -} - -static void hash_h(uint8_t out[32], const uint8_t *in, size_t len) { - BORINGSSL_keccak(out, 32, in, len, boringssl_sha3_256); -} - -static void hash_g(uint8_t out[64], const uint8_t *in, size_t len) { - BORINGSSL_keccak(out, 64, in, len, boringssl_sha3_512); -} - -static void kdf(uint8_t *out, size_t out_len, const uint8_t *in, size_t len) { - BORINGSSL_keccak(out, out_len, in, len, boringssl_shake256); -} - -#define DEGREE 256 -#define RANK 3 - -static const size_t kBarrettMultiplier = 5039; -static const unsigned kBarrettShift = 24; -static const uint16_t kPrime = 3329; -static const int kLog2Prime = 12; -static const uint16_t kHalfPrime = (/*kPrime=*/3329 - 1) / 2; -static const int kDU = 10; -static const int kDV = 4; -// kInverseDegree is 128^-1 mod 3329; 128 because kPrime does not have a 512th -// root of unity. -static const uint16_t kInverseDegree = 3303; -static const size_t kEncodedVectorSize = - (/*kLog2Prime=*/12 * DEGREE / 8) * RANK; -static const size_t kCompressedVectorSize = /*kDU=*/10 * RANK * DEGREE / 8; - -typedef struct scalar { - // On every function entry and exit, 0 <= c < kPrime. - uint16_t c[DEGREE]; -} scalar; - -typedef struct vector { - scalar v[RANK]; -} vector; - -typedef struct matrix { - scalar v[RANK][RANK]; -} matrix; - -// This bit of Python will be referenced in some of the following comments: -// -// p = 3329 -// -// def bitreverse(i): -// ret = 0 -// for n in range(7): -// bit = i & 1 -// ret <<= 1 -// ret |= bit -// i >>= 1 -// return ret - -// kNTTRoots = [pow(17, bitreverse(i), p) for i in range(128)] -static const uint16_t kNTTRoots[128] = { - 1, 1729, 2580, 3289, 2642, 630, 1897, 848, 1062, 1919, 193, 797, - 2786, 3260, 569, 1746, 296, 2447, 1339, 1476, 3046, 56, 2240, 1333, - 1426, 2094, 535, 2882, 2393, 2879, 1974, 821, 289, 331, 3253, 1756, - 1197, 2304, 2277, 2055, 650, 1977, 2513, 632, 2865, 33, 1320, 1915, - 2319, 1435, 807, 452, 1438, 2868, 1534, 2402, 2647, 2617, 1481, 648, - 2474, 3110, 1227, 910, 17, 2761, 583, 2649, 1637, 723, 2288, 1100, - 1409, 2662, 3281, 233, 756, 2156, 3015, 3050, 1703, 1651, 2789, 1789, - 1847, 952, 1461, 2687, 939, 2308, 2437, 2388, 733, 2337, 268, 641, - 1584, 2298, 2037, 3220, 375, 2549, 2090, 1645, 1063, 319, 2773, 757, - 2099, 561, 2466, 2594, 2804, 1092, 403, 1026, 1143, 2150, 2775, 886, - 1722, 1212, 1874, 1029, 2110, 2935, 885, 2154, -}; - -// kInverseNTTRoots = [pow(17, -bitreverse(i), p) for i in range(128)] -static const uint16_t kInverseNTTRoots[128] = { - 1, 1600, 40, 749, 2481, 1432, 2699, 687, 1583, 2760, 69, 543, - 2532, 3136, 1410, 2267, 2508, 1355, 450, 936, 447, 2794, 1235, 1903, - 1996, 1089, 3273, 283, 1853, 1990, 882, 3033, 2419, 2102, 219, 855, - 2681, 1848, 712, 682, 927, 1795, 461, 1891, 2877, 2522, 1894, 1010, - 1414, 2009, 3296, 464, 2697, 816, 1352, 2679, 1274, 1052, 1025, 2132, - 1573, 76, 2998, 3040, 1175, 2444, 394, 1219, 2300, 1455, 2117, 1607, - 2443, 554, 1179, 2186, 2303, 2926, 2237, 525, 735, 863, 2768, 1230, - 2572, 556, 3010, 2266, 1684, 1239, 780, 2954, 109, 1292, 1031, 1745, - 2688, 3061, 992, 2596, 941, 892, 1021, 2390, 642, 1868, 2377, 1482, - 1540, 540, 1678, 1626, 279, 314, 1173, 2573, 3096, 48, 667, 1920, - 2229, 1041, 2606, 1692, 680, 2746, 568, 3312, -}; - -// kModRoots = [pow(17, 2*bitreverse(i) + 1, p) for i in range(128)] -static const uint16_t kModRoots[128] = { - 17, 3312, 2761, 568, 583, 2746, 2649, 680, 1637, 1692, 723, 2606, - 2288, 1041, 1100, 2229, 1409, 1920, 2662, 667, 3281, 48, 233, 3096, - 756, 2573, 2156, 1173, 3015, 314, 3050, 279, 1703, 1626, 1651, 1678, - 2789, 540, 1789, 1540, 1847, 1482, 952, 2377, 1461, 1868, 2687, 642, - 939, 2390, 2308, 1021, 2437, 892, 2388, 941, 733, 2596, 2337, 992, - 268, 3061, 641, 2688, 1584, 1745, 2298, 1031, 2037, 1292, 3220, 109, - 375, 2954, 2549, 780, 2090, 1239, 1645, 1684, 1063, 2266, 319, 3010, - 2773, 556, 757, 2572, 2099, 1230, 561, 2768, 2466, 863, 2594, 735, - 2804, 525, 1092, 2237, 403, 2926, 1026, 2303, 1143, 2186, 2150, 1179, - 2775, 554, 886, 2443, 1722, 1607, 1212, 2117, 1874, 1455, 1029, 2300, - 2110, 1219, 2935, 394, 885, 2444, 2154, 1175, -}; - -// reduce_once reduces 0 <= x < 2*kPrime, mod kPrime. -static uint16_t reduce_once(uint16_t x) { - declassify_assert(x < 2 * kPrime); - const uint16_t subtracted = x - kPrime; - uint16_t mask = 0u - (subtracted >> 15); - // Although this is a constant-time select, we omit a value barrier here. - // Value barriers impede auto-vectorization (likely because it forces the - // value to transit through a general-purpose register). On AArch64, this is a - // difference of 2x. - // - // We usually add value barriers to selects because Clang turns consecutive - // selects with the same condition into a branch instead of CMOV/CSEL. This - // condition does not occur in Kyber, so omitting it seems to be safe so far, - // but see |scalar_centered_binomial_distribution_eta_2_with_prf|. - return (mask & x) | (~mask & subtracted); -} - -// constant time reduce x mod kPrime using Barrett reduction. x must be less -// than kPrime + 2×kPrime². -static uint16_t reduce(uint32_t x) { - declassify_assert(x < kPrime + 2u * kPrime * kPrime); - uint64_t product = (uint64_t)x * kBarrettMultiplier; - uint32_t quotient = (uint32_t)(product >> kBarrettShift); - uint32_t remainder = x - quotient * kPrime; - return reduce_once(remainder); -} - -static void scalar_zero(scalar *out) { OPENSSL_memset(out, 0, sizeof(*out)); } - -static void vector_zero(vector *out) { OPENSSL_memset(out, 0, sizeof(*out)); } - -// In place number theoretic transform of a given scalar. -// Note that Kyber's kPrime 3329 does not have a 512th root of unity, so this -// transform leaves off the last iteration of the usual FFT code, with the 128 -// relevant roots of unity being stored in |kNTTRoots|. This means the output -// should be seen as 128 elements in GF(3329^2), with the coefficients of the -// elements being consecutive entries in |s->c|. -static void scalar_ntt(scalar *s) { - int offset = DEGREE; - // `int` is used here because using `size_t` throughout caused a ~5% slowdown - // with Clang 14 on Aarch64. - for (int step = 1; step < DEGREE / 2; step <<= 1) { - offset >>= 1; - int k = 0; - for (int i = 0; i < step; i++) { - const uint32_t step_root = kNTTRoots[i + step]; - for (int j = k; j < k + offset; j++) { - uint16_t odd = reduce(step_root * s->c[j + offset]); - uint16_t even = s->c[j]; - s->c[j] = reduce_once(odd + even); - s->c[j + offset] = reduce_once(even - odd + kPrime); - } - k += 2 * offset; - } - } -} - -static void vector_ntt(vector *a) { - for (int i = 0; i < RANK; i++) { - scalar_ntt(&a->v[i]); - } -} - -// In place inverse number theoretic transform of a given scalar, with pairs of -// entries of s->v being interpreted as elements of GF(3329^2). Just as with the -// number theoretic transform, this leaves off the first step of the normal iFFT -// to account for the fact that 3329 does not have a 512th root of unity, using -// the precomputed 128 roots of unity stored in |kInverseNTTRoots|. -static void scalar_inverse_ntt(scalar *s) { - int step = DEGREE / 2; - // `int` is used here because using `size_t` throughout caused a ~5% slowdown - // with Clang 14 on Aarch64. - for (int offset = 2; offset < DEGREE; offset <<= 1) { - step >>= 1; - int k = 0; - for (int i = 0; i < step; i++) { - uint32_t step_root = kInverseNTTRoots[i + step]; - for (int j = k; j < k + offset; j++) { - uint16_t odd = s->c[j + offset]; - uint16_t even = s->c[j]; - s->c[j] = reduce_once(odd + even); - s->c[j + offset] = reduce(step_root * (even - odd + kPrime)); - } - k += 2 * offset; - } - } - for (int i = 0; i < DEGREE; i++) { - s->c[i] = reduce(s->c[i] * kInverseDegree); - } -} - -static void vector_inverse_ntt(vector *a) { - for (int i = 0; i < RANK; i++) { - scalar_inverse_ntt(&a->v[i]); - } -} - -static void scalar_add(scalar *lhs, const scalar *rhs) { - for (int i = 0; i < DEGREE; i++) { - lhs->c[i] = reduce_once(lhs->c[i] + rhs->c[i]); - } -} - -static void scalar_sub(scalar *lhs, const scalar *rhs) { - for (int i = 0; i < DEGREE; i++) { - lhs->c[i] = reduce_once(lhs->c[i] - rhs->c[i] + kPrime); - } -} - -// Multiplying two scalars in the number theoretically transformed state. Since -// 3329 does not have a 512th root of unity, this means we have to interpret -// the 2*ith and (2*i+1)th entries of the scalar as elements of GF(3329)[X]/(X^2 -// - 17^(2*bitreverse(i)+1)) The value of 17^(2*bitreverse(i)+1) mod 3329 is -// stored in the precomputed |kModRoots| table. Note that our Barrett transform -// only allows us to multiply two reduced numbers together, so we need some -// intermediate reduction steps, even if an uint64_t could hold 3 multiplied -// numbers. -static void scalar_mult(scalar *out, const scalar *lhs, const scalar *rhs) { - for (int i = 0; i < DEGREE / 2; i++) { - uint32_t real_real = (uint32_t)lhs->c[2 * i] * rhs->c[2 * i]; - uint32_t img_img = (uint32_t)lhs->c[2 * i + 1] * rhs->c[2 * i + 1]; - uint32_t real_img = (uint32_t)lhs->c[2 * i] * rhs->c[2 * i + 1]; - uint32_t img_real = (uint32_t)lhs->c[2 * i + 1] * rhs->c[2 * i]; - out->c[2 * i] = - reduce(real_real + (uint32_t)reduce(img_img) * kModRoots[i]); - out->c[2 * i + 1] = reduce(img_real + real_img); - } -} - -static void vector_add(vector *lhs, const vector *rhs) { - for (int i = 0; i < RANK; i++) { - scalar_add(&lhs->v[i], &rhs->v[i]); - } -} - -static void matrix_mult(vector *out, const matrix *m, const vector *a) { - vector_zero(out); - for (int i = 0; i < RANK; i++) { - for (int j = 0; j < RANK; j++) { - scalar product; - scalar_mult(&product, &m->v[i][j], &a->v[j]); - scalar_add(&out->v[i], &product); - } - } -} - -static void matrix_mult_transpose(vector *out, const matrix *m, - const vector *a) { - vector_zero(out); - for (int i = 0; i < RANK; i++) { - for (int j = 0; j < RANK; j++) { - scalar product; - scalar_mult(&product, &m->v[j][i], &a->v[j]); - scalar_add(&out->v[i], &product); - } - } -} - -static void scalar_inner_product(scalar *out, const vector *lhs, - const vector *rhs) { - scalar_zero(out); - for (int i = 0; i < RANK; i++) { - scalar product; - scalar_mult(&product, &lhs->v[i], &rhs->v[i]); - scalar_add(out, &product); - } -} - -// Algorithm 1 of the Kyber spec. Rejection samples a Keccak stream to get -// uniformly distributed elements. This is used for matrix expansion and only -// operates on public inputs. -static void scalar_from_keccak_vartime(scalar *out, - struct BORINGSSL_keccak_st *keccak_ctx) { - assert(keccak_ctx->squeeze_offset == 0); - assert(keccak_ctx->rate_bytes == 168); - static_assert(168 % 3 == 0, "block and coefficient boundaries do not align"); - - int done = 0; - while (done < DEGREE) { - uint8_t block[168]; - BORINGSSL_keccak_squeeze(keccak_ctx, block, sizeof(block)); - for (size_t i = 0; i < sizeof(block) && done < DEGREE; i += 3) { - uint16_t d1 = block[i] + 256 * (block[i + 1] % 16); - uint16_t d2 = block[i + 1] / 16 + 16 * block[i + 2]; - if (d1 < kPrime) { - out->c[done++] = d1; - } - if (d2 < kPrime && done < DEGREE) { - out->c[done++] = d2; - } - } - } -} - -// Algorithm 2 of the Kyber spec, with eta fixed to two and the PRF call -// included. Creates binominally distributed elements by sampling 2*|eta| bits, -// and setting the coefficient to the count of the first bits minus the count of -// the second bits, resulting in a centered binomial distribution. Since eta is -// two this gives -2/2 with a probability of 1/16, -1/1 with probability 1/4, -// and 0 with probability 3/8. -static void scalar_centered_binomial_distribution_eta_2_with_prf( - scalar *out, const uint8_t input[33]) { - uint8_t entropy[128]; - static_assert(sizeof(entropy) == 2 * /*kEta=*/2 * DEGREE / 8); - prf(entropy, sizeof(entropy), input); - - for (int i = 0; i < DEGREE; i += 2) { - uint8_t byte = entropy[i / 2]; - - uint16_t value = (byte & 1) + ((byte >> 1) & 1); - value -= ((byte >> 2) & 1) + ((byte >> 3) & 1); - // Add |kPrime| if |value| underflowed. See |reduce_once| for a discussion - // on why the value barrier is omitted. While this could have been written - // reduce_once(value + kPrime), this is one extra addition and small range - // of |value| tempts some versions of Clang to emit a branch. - uint16_t mask = 0u - (value >> 15); - out->c[i] = value + (kPrime & mask); - - byte >>= 4; - value = (byte & 1) + ((byte >> 1) & 1); - value -= ((byte >> 2) & 1) + ((byte >> 3) & 1); - // See above. - mask = 0u - (value >> 15); - out->c[i + 1] = value + (kPrime & mask); - } -} - -// Generates a secret vector by using -// |scalar_centered_binomial_distribution_eta_2_with_prf|, using the given seed -// appending and incrementing |counter| for entry of the vector. -static void vector_generate_secret_eta_2(vector *out, uint8_t *counter, - const uint8_t seed[32]) { - uint8_t input[33]; - OPENSSL_memcpy(input, seed, 32); - for (int i = 0; i < RANK; i++) { - input[32] = (*counter)++; - scalar_centered_binomial_distribution_eta_2_with_prf(&out->v[i], input); - } -} - -// Expands the matrix of a seed for key generation and for encaps-CPA. -static void matrix_expand(matrix *out, const uint8_t rho[32]) { - uint8_t input[34]; - OPENSSL_memcpy(input, rho, 32); - for (int i = 0; i < RANK; i++) { - for (int j = 0; j < RANK; j++) { - input[32] = i; - input[33] = j; - struct BORINGSSL_keccak_st keccak_ctx; - BORINGSSL_keccak_init(&keccak_ctx, boringssl_shake128); - BORINGSSL_keccak_absorb(&keccak_ctx, input, sizeof(input)); - scalar_from_keccak_vartime(&out->v[i][j], &keccak_ctx); - } - } -} - -static const uint8_t kMasks[8] = {0x01, 0x03, 0x07, 0x0f, - 0x1f, 0x3f, 0x7f, 0xff}; - -static void scalar_encode(uint8_t *out, const scalar *s, int bits) { - assert(bits <= (int)sizeof(*s->c) * 8 && bits != 1); - - uint8_t out_byte = 0; - int out_byte_bits = 0; - - for (int i = 0; i < DEGREE; i++) { - uint16_t element = s->c[i]; - int element_bits_done = 0; - - while (element_bits_done < bits) { - int chunk_bits = bits - element_bits_done; - int out_bits_remaining = 8 - out_byte_bits; - if (chunk_bits >= out_bits_remaining) { - chunk_bits = out_bits_remaining; - out_byte |= (element & kMasks[chunk_bits - 1]) << out_byte_bits; - *out = out_byte; - out++; - out_byte_bits = 0; - out_byte = 0; - } else { - out_byte |= (element & kMasks[chunk_bits - 1]) << out_byte_bits; - out_byte_bits += chunk_bits; - } - - element_bits_done += chunk_bits; - element >>= chunk_bits; - } - } - - if (out_byte_bits > 0) { - *out = out_byte; - } -} - -// scalar_encode_1 is |scalar_encode| specialised for |bits| == 1. -static void scalar_encode_1(uint8_t out[32], const scalar *s) { - for (int i = 0; i < DEGREE; i += 8) { - uint8_t out_byte = 0; - for (int j = 0; j < 8; j++) { - out_byte |= (s->c[i + j] & 1) << j; - } - *out = out_byte; - out++; - } -} - -// Encodes an entire vector into 32*|RANK|*|bits| bytes. Note that since 256 -// (DEGREE) is divisible by 8, the individual vector entries will always fill a -// whole number of bytes, so we do not need to worry about bit packing here. -static void vector_encode(uint8_t *out, const vector *a, int bits) { - for (int i = 0; i < RANK; i++) { - scalar_encode(out + i * bits * DEGREE / 8, &a->v[i], bits); - } -} - -// scalar_decode parses |DEGREE * bits| bits from |in| into |DEGREE| values in -// |out|. It returns one on success and zero if any parsed value is >= -// |kPrime|. -static int scalar_decode(scalar *out, const uint8_t *in, int bits) { - assert(bits <= (int)sizeof(*out->c) * 8 && bits != 1); - - uint8_t in_byte = 0; - int in_byte_bits_left = 0; - - for (int i = 0; i < DEGREE; i++) { - uint16_t element = 0; - int element_bits_done = 0; - - while (element_bits_done < bits) { - if (in_byte_bits_left == 0) { - in_byte = *in; - in++; - in_byte_bits_left = 8; - } - - int chunk_bits = bits - element_bits_done; - if (chunk_bits > in_byte_bits_left) { - chunk_bits = in_byte_bits_left; - } - - element |= (in_byte & kMasks[chunk_bits - 1]) << element_bits_done; - in_byte_bits_left -= chunk_bits; - in_byte >>= chunk_bits; - - element_bits_done += chunk_bits; - } - - // An element is only out of range in the case of invalid input, in which - // case it is okay to leak the comparison. - if (constant_time_declassify_int(element >= kPrime)) { - return 0; - } - out->c[i] = element; - } - - return 1; -} - -// scalar_decode_1 is |scalar_decode| specialised for |bits| == 1. -static void scalar_decode_1(scalar *out, const uint8_t in[32]) { - for (int i = 0; i < DEGREE; i += 8) { - uint8_t in_byte = *in; - in++; - for (int j = 0; j < 8; j++) { - out->c[i + j] = in_byte & 1; - in_byte >>= 1; - } - } -} - -// Decodes 32*|RANK|*|bits| bytes from |in| into |out|. It returns one on -// success or zero if any parsed value is >= |kPrime|. -static int vector_decode(vector *out, const uint8_t *in, int bits) { - for (int i = 0; i < RANK; i++) { - if (!scalar_decode(&out->v[i], in + i * bits * DEGREE / 8, bits)) { - return 0; - } - } - return 1; -} - -// Compresses (lossily) an input |x| mod 3329 into |bits| many bits by grouping -// numbers close to each other together. The formula used is -// round(2^|bits|/kPrime*x) mod 2^|bits|. -// Uses Barrett reduction to achieve constant time. Since we need both the -// remainder (for rounding) and the quotient (as the result), we cannot use -// |reduce| here, but need to do the Barrett reduction directly. -static uint16_t compress(uint16_t x, int bits) { - uint32_t shifted = (uint32_t)x << bits; - uint64_t product = (uint64_t)shifted * kBarrettMultiplier; - uint32_t quotient = (uint32_t)(product >> kBarrettShift); - uint32_t remainder = shifted - quotient * kPrime; - - // Adjust the quotient to round correctly: - // 0 <= remainder <= kHalfPrime round to 0 - // kHalfPrime < remainder <= kPrime + kHalfPrime round to 1 - // kPrime + kHalfPrime < remainder < 2 * kPrime round to 2 - declassify_assert(remainder < 2u * kPrime); - quotient += 1 & constant_time_lt_w(kHalfPrime, remainder); - quotient += 1 & constant_time_lt_w(kPrime + kHalfPrime, remainder); - return quotient & ((1 << bits) - 1); -} - -// Decompresses |x| by using an equi-distant representative. The formula is -// round(kPrime/2^|bits|*x). Note that 2^|bits| being the divisor allows us to -// implement this logic using only bit operations. -static uint16_t decompress(uint16_t x, int bits) { - uint32_t product = (uint32_t)x * kPrime; - uint32_t power = 1 << bits; - // This is |product| % power, since |power| is a power of 2. - uint32_t remainder = product & (power - 1); - // This is |product| / power, since |power| is a power of 2. - uint32_t lower = product >> bits; - // The rounding logic works since the first half of numbers mod |power| have a - // 0 as first bit, and the second half has a 1 as first bit, since |power| is - // a power of 2. As a 12 bit number, |remainder| is always positive, so we - // will shift in 0s for a right shift. - return lower + (remainder >> (bits - 1)); -} - -static void scalar_compress(scalar *s, int bits) { - for (int i = 0; i < DEGREE; i++) { - s->c[i] = compress(s->c[i], bits); - } -} - -static void scalar_decompress(scalar *s, int bits) { - for (int i = 0; i < DEGREE; i++) { - s->c[i] = decompress(s->c[i], bits); - } -} - -static void vector_compress(vector *a, int bits) { - for (int i = 0; i < RANK; i++) { - scalar_compress(&a->v[i], bits); - } -} - -static void vector_decompress(vector *a, int bits) { - for (int i = 0; i < RANK; i++) { - scalar_decompress(&a->v[i], bits); - } -} - -namespace { - -struct public_key { - vector t; - uint8_t rho[32]; - uint8_t public_key_hash[32]; - matrix m; -}; - -static struct public_key *public_key_from_external( - const struct KYBER_public_key *external) { - static_assert(sizeof(struct KYBER_public_key) >= sizeof(struct public_key), - "Kyber public key is too small"); - static_assert(alignof(struct KYBER_public_key) >= alignof(struct public_key), - "Kyber public key align incorrect"); - return (struct public_key *)external; -} - -struct private_key { - struct public_key pub; - vector s; - uint8_t fo_failure_secret[32]; -}; - -static struct private_key *private_key_from_external( - const struct KYBER_private_key *external) { - static_assert(sizeof(struct KYBER_private_key) >= sizeof(struct private_key), - "Kyber private key too small"); - static_assert( - alignof(struct KYBER_private_key) >= alignof(struct private_key), - "Kyber private key align incorrect"); - return (struct private_key *)external; -} - -} // namespace - -// Calls |KYBER_generate_key_external_entropy| with random bytes from -// |RAND_bytes|. -void bssl::KYBER_generate_key( - uint8_t out_encoded_public_key[KYBER_PUBLIC_KEY_BYTES], - struct KYBER_private_key *out_private_key) { - uint8_t entropy[KYBER_GENERATE_KEY_ENTROPY]; - RAND_bytes(entropy, sizeof(entropy)); - CONSTTIME_SECRET(entropy, sizeof(entropy)); - KYBER_generate_key_external_entropy(out_encoded_public_key, out_private_key, - entropy); -} - -static int kyber_marshal_public_key(CBB *out, const struct public_key *pub) { - uint8_t *vector_output; - if (!CBB_add_space(out, &vector_output, kEncodedVectorSize)) { - return 0; - } - vector_encode(vector_output, &pub->t, kLog2Prime); - if (!CBB_add_bytes(out, pub->rho, sizeof(pub->rho))) { - return 0; - } - return 1; -} - -// Algorithms 4 and 7 of the Kyber spec. Algorithms are combined since key -// generation is not part of the FO transform, and the spec uses Algorithm 7 to -// specify the actual key format. -void bssl::KYBER_generate_key_external_entropy( - uint8_t out_encoded_public_key[KYBER_PUBLIC_KEY_BYTES], - struct KYBER_private_key *out_private_key, - const uint8_t entropy[KYBER_GENERATE_KEY_ENTROPY]) { - struct private_key *priv = private_key_from_external(out_private_key); - uint8_t hashed[64]; - hash_g(hashed, entropy, 32); - const uint8_t *const rho = hashed; - const uint8_t *const sigma = hashed + 32; - // rho is public. - CONSTTIME_DECLASSIFY(rho, 32); - OPENSSL_memcpy(priv->pub.rho, hashed, sizeof(priv->pub.rho)); - matrix_expand(&priv->pub.m, rho); - uint8_t counter = 0; - vector_generate_secret_eta_2(&priv->s, &counter, sigma); - vector_ntt(&priv->s); - vector error; - vector_generate_secret_eta_2(&error, &counter, sigma); - vector_ntt(&error); - matrix_mult_transpose(&priv->pub.t, &priv->pub.m, &priv->s); - vector_add(&priv->pub.t, &error); - // t is part of the public key and thus is public. - CONSTTIME_DECLASSIFY(&priv->pub.t, sizeof(priv->pub.t)); - - CBB cbb; - CBB_init_fixed(&cbb, out_encoded_public_key, KYBER_PUBLIC_KEY_BYTES); - if (!kyber_marshal_public_key(&cbb, &priv->pub)) { - abort(); - } - - hash_h(priv->pub.public_key_hash, out_encoded_public_key, - KYBER_PUBLIC_KEY_BYTES); - OPENSSL_memcpy(priv->fo_failure_secret, entropy + 32, 32); -} - -void bssl::KYBER_public_from_private( - struct KYBER_public_key *out_public_key, - const struct KYBER_private_key *private_key) { - struct public_key *const pub = public_key_from_external(out_public_key); - const struct private_key *const priv = private_key_from_external(private_key); - *pub = priv->pub; -} - -// Algorithm 5 of the Kyber spec. Encrypts a message with given randomness to -// the ciphertext in |out|. Without applying the Fujisaki-Okamoto transform this -// would not result in a CCA secure scheme, since lattice schemes are vulnerable -// to decryption failure oracles. -static void encrypt_cpa(uint8_t out[KYBER_CIPHERTEXT_BYTES], - const struct public_key *pub, const uint8_t message[32], - const uint8_t randomness[32]) { - uint8_t counter = 0; - vector secret; - vector_generate_secret_eta_2(&secret, &counter, randomness); - vector_ntt(&secret); - vector error; - vector_generate_secret_eta_2(&error, &counter, randomness); - uint8_t input[33]; - OPENSSL_memcpy(input, randomness, 32); - input[32] = counter; - scalar scalar_error; - scalar_centered_binomial_distribution_eta_2_with_prf(&scalar_error, input); - vector u; - matrix_mult(&u, &pub->m, &secret); - vector_inverse_ntt(&u); - vector_add(&u, &error); - scalar v; - scalar_inner_product(&v, &pub->t, &secret); - scalar_inverse_ntt(&v); - scalar_add(&v, &scalar_error); - scalar expanded_message; - scalar_decode_1(&expanded_message, message); - scalar_decompress(&expanded_message, 1); - scalar_add(&v, &expanded_message); - vector_compress(&u, kDU); - vector_encode(out, &u, kDU); - scalar_compress(&v, kDV); - scalar_encode(out + kCompressedVectorSize, &v, kDV); -} - -// Calls KYBER_encap_external_entropy| with random bytes from |RAND_bytes| -void bssl::KYBER_encap(uint8_t out_ciphertext[KYBER_CIPHERTEXT_BYTES], - uint8_t out_shared_secret[KYBER_SHARED_SECRET_BYTES], - const struct KYBER_public_key *public_key) { - uint8_t entropy[KYBER_ENCAP_ENTROPY]; - RAND_bytes(entropy, KYBER_ENCAP_ENTROPY); - CONSTTIME_SECRET(entropy, KYBER_ENCAP_ENTROPY); - KYBER_encap_external_entropy(out_ciphertext, out_shared_secret, public_key, - entropy); -} - -// Algorithm 8 of the Kyber spec, safe for line 2 of the spec. The spec there -// hashes the output of the system's random number generator, since the FO -// transform will reveal it to the decrypting party. There is no reason to do -// this when a secure random number generator is used. When an insecure random -// number generator is used, the caller should switch to a secure one before -// calling this method. -void bssl::KYBER_encap_external_entropy( - uint8_t out_ciphertext[KYBER_CIPHERTEXT_BYTES], - uint8_t out_shared_secret[KYBER_SHARED_SECRET_BYTES], - const struct KYBER_public_key *public_key, - const uint8_t entropy[KYBER_ENCAP_ENTROPY]) { - const struct public_key *pub = public_key_from_external(public_key); - uint8_t input[64]; - OPENSSL_memcpy(input, entropy, KYBER_ENCAP_ENTROPY); - OPENSSL_memcpy(input + KYBER_ENCAP_ENTROPY, pub->public_key_hash, - sizeof(input) - KYBER_ENCAP_ENTROPY); - uint8_t prekey_and_randomness[64]; - hash_g(prekey_and_randomness, input, sizeof(input)); - encrypt_cpa(out_ciphertext, pub, entropy, prekey_and_randomness + 32); - // The ciphertext is public. - CONSTTIME_DECLASSIFY(out_ciphertext, KYBER_CIPHERTEXT_BYTES); - hash_h(prekey_and_randomness + 32, out_ciphertext, KYBER_CIPHERTEXT_BYTES); - kdf(out_shared_secret, KYBER_SHARED_SECRET_BYTES, prekey_and_randomness, - sizeof(prekey_and_randomness)); -} - -// Algorithm 6 of the Kyber spec. -static void decrypt_cpa(uint8_t out[32], const struct private_key *priv, - const uint8_t ciphertext[KYBER_CIPHERTEXT_BYTES]) { - vector u; - vector_decode(&u, ciphertext, kDU); - vector_decompress(&u, kDU); - vector_ntt(&u); - scalar v; - scalar_decode(&v, ciphertext + kCompressedVectorSize, kDV); - scalar_decompress(&v, kDV); - scalar mask; - scalar_inner_product(&mask, &priv->s, &u); - scalar_inverse_ntt(&mask); - scalar_sub(&v, &mask); - scalar_compress(&v, 1); - scalar_encode_1(out, &v); -} - -// Algorithm 9 of the Kyber spec, performing the FO transform by running -// encrypt_cpa on the decrypted message. The spec does not allow the decryption -// failure to be passed on to the caller, and instead returns a result that is -// deterministic but unpredictable to anyone without knowledge of the private -// key. -void bssl::KYBER_decap(uint8_t out_shared_secret[KYBER_SHARED_SECRET_BYTES], - const uint8_t ciphertext[KYBER_CIPHERTEXT_BYTES], - const struct KYBER_private_key *private_key) { - const struct private_key *priv = private_key_from_external(private_key); - uint8_t decrypted[64]; - decrypt_cpa(decrypted, priv, ciphertext); - OPENSSL_memcpy(decrypted + 32, priv->pub.public_key_hash, - sizeof(decrypted) - 32); - uint8_t prekey_and_randomness[64]; - hash_g(prekey_and_randomness, decrypted, sizeof(decrypted)); - uint8_t expected_ciphertext[KYBER_CIPHERTEXT_BYTES]; - encrypt_cpa(expected_ciphertext, &priv->pub, decrypted, - prekey_and_randomness + 32); - uint8_t mask = - constant_time_eq_int_8(CRYPTO_memcmp(ciphertext, expected_ciphertext, - sizeof(expected_ciphertext)), - 0); - uint8_t input[64]; - for (int i = 0; i < 32; i++) { - input[i] = constant_time_select_8(mask, prekey_and_randomness[i], - priv->fo_failure_secret[i]); - } - hash_h(input + 32, ciphertext, KYBER_CIPHERTEXT_BYTES); - kdf(out_shared_secret, KYBER_SHARED_SECRET_BYTES, input, sizeof(input)); -} - -int bssl::KYBER_marshal_public_key(CBB *out, - const struct KYBER_public_key *public_key) { - return kyber_marshal_public_key(out, public_key_from_external(public_key)); -} - -// kyber_parse_public_key_no_hash parses |in| into |pub| but doesn't calculate -// the value of |pub->public_key_hash|. -static int kyber_parse_public_key_no_hash(struct public_key *pub, CBS *in) { - CBS t_bytes; - if (!CBS_get_bytes(in, &t_bytes, kEncodedVectorSize) || - !vector_decode(&pub->t, CBS_data(&t_bytes), kLog2Prime) || - !CBS_copy_bytes(in, pub->rho, sizeof(pub->rho))) { - return 0; - } - matrix_expand(&pub->m, pub->rho); - return 1; -} - -int bssl::KYBER_parse_public_key(struct KYBER_public_key *public_key, CBS *in) { - struct public_key *pub = public_key_from_external(public_key); - CBS orig_in = *in; - if (!kyber_parse_public_key_no_hash(pub, in) || // - CBS_len(in) != 0) { - return 0; - } - hash_h(pub->public_key_hash, CBS_data(&orig_in), CBS_len(&orig_in)); - return 1; -} - -int bssl::KYBER_marshal_private_key( - CBB *out, const struct KYBER_private_key *private_key) { - const struct private_key *const priv = private_key_from_external(private_key); - uint8_t *s_output; - if (!CBB_add_space(out, &s_output, kEncodedVectorSize)) { - return 0; - } - vector_encode(s_output, &priv->s, kLog2Prime); - if (!kyber_marshal_public_key(out, &priv->pub) || - !CBB_add_bytes(out, priv->pub.public_key_hash, - sizeof(priv->pub.public_key_hash)) || - !CBB_add_bytes(out, priv->fo_failure_secret, - sizeof(priv->fo_failure_secret))) { - return 0; - } - return 1; -} - -int bssl::KYBER_parse_private_key(struct KYBER_private_key *out_private_key, - CBS *in) { - struct private_key *const priv = private_key_from_external(out_private_key); - - CBS s_bytes; - if (!CBS_get_bytes(in, &s_bytes, kEncodedVectorSize) || - !vector_decode(&priv->s, CBS_data(&s_bytes), kLog2Prime) || - !kyber_parse_public_key_no_hash(&priv->pub, in) || - !CBS_copy_bytes(in, priv->pub.public_key_hash, - sizeof(priv->pub.public_key_hash)) || - !CBS_copy_bytes(in, priv->fo_failure_secret, - sizeof(priv->fo_failure_secret)) || - CBS_len(in) != 0) { - return 0; - } - return 1; -} diff --git a/third_party/boringssl/src/crypto/lhash/internal.h b/third_party/boringssl/src/crypto/lhash/internal.h index 27c778062..6b5e0f842 100644 --- a/third_party/boringssl/src/crypto/lhash/internal.h +++ b/third_party/boringssl/src/crypto/lhash/internal.h @@ -24,8 +24,8 @@ BSSL_NAMESPACE_BEGIN // contracts as needed. One should not use the lh_* functions directly, rather // use the type-safe macro wrappers: // -// A hash table of a specific type of object has type |LHASH_OF(type)|. This -// can be defined (once) with |DEFINE_LHASH_OF(type)|. +// A hash table of a specific type of object has type `LHASH_OF(type)`. This +// can be defined (once) with `DEFINE_LHASH_OF(type)`. // // struct foo { // int bar; @@ -33,10 +33,10 @@ BSSL_NAMESPACE_BEGIN // // DEFINE_LHASH_OF(struct foo) // -// Although note that the hash table will contain /pointers/ to |foo|. +// Although note that the hash table will contain /pointers/ to `foo`. // -// A macro will be defined for each of the |OPENSSL_lh_*| functions below. For -// |LHASH_OF(foo)|, the macros would be |lh_foo_new|, |lh_foo_num_items| etc. +// A macro will be defined for each of the `OPENSSL_lh_*` functions below. For +// `LHASH_OF(foo)`, the macros would be `lh_foo_new`, `lh_foo_num_items` etc. // // TODO(davidben): Now that this type is completely internal, this can just be a // C++ template without any macros. @@ -45,12 +45,12 @@ BSSL_NAMESPACE_BEGIN #define LHASH_OF(type) struct bssl::type##_lhash_st // lhash_cmp_func is a comparison function that returns a value equal, or not -// equal, to zero depending on whether |*a| is equal, or not equal to |*b|, -// respectively. Note the difference between this and |stack_cmp_func| in that +// equal, to zero depending on whether `*a` is equal, or not equal to `*b`, +// respectively. Note the difference between this and `stack_cmp_func` in that // this takes pointers to the objects directly. // // This function's actual type signature is int (*)(const T*, const T*). The -// low-level |lh_*| functions will be passed a type-specific wrapper to call it +// low-level `lh_*` functions will be passed a type-specific wrapper to call it // correctly. typedef int (*lhash_cmp_func)(const void *a, const void *b); typedef int (*lhash_cmp_func_helper)(lhash_cmp_func func, const void *a, @@ -60,7 +60,7 @@ typedef int (*lhash_cmp_func_helper)(lhash_cmp_func func, const void *a, // uint32_t. // // This function's actual type signature is uint32_t (*)(const T*). The -// low-level |lh_*| functions will be passed a type-specific wrapper to call it +// low-level `lh_*` functions will be passed a type-specific wrapper to call it // correctly. typedef uint32_t (*lhash_hash_func)(const void *a); typedef uint32_t (*lhash_hash_func_helper)(lhash_hash_func func, const void *a); @@ -72,45 +72,45 @@ OPENSSL_EXPORT _LHASH *OPENSSL_lh_new(lhash_hash_func hash, lhash_cmp_func comp); // OPENSSL_lh_free frees the hash table itself but none of the elements. See -// |OPENSSL_lh_doall|. +// `OPENSSL_lh_doall`. OPENSSL_EXPORT void OPENSSL_lh_free(_LHASH *lh); -// OPENSSL_lh_num_items returns the number of items in |lh|. +// OPENSSL_lh_num_items returns the number of items in `lh`. OPENSSL_EXPORT size_t OPENSSL_lh_num_items(const _LHASH *lh); -// OPENSSL_lh_retrieve finds an element equal to |data| in the hash table and +// OPENSSL_lh_retrieve finds an element equal to `data` in the hash table and // returns it. If no such element exists, it returns NULL. OPENSSL_EXPORT void *OPENSSL_lh_retrieve(const _LHASH *lh, const void *data, lhash_hash_func_helper call_hash_func, lhash_cmp_func_helper call_cmp_func); -// OPENSSL_lh_retrieve_key finds an element matching |key|, given the specified -// hash and comparison function. This differs from |OPENSSL_lh_retrieve| in that -// the key may be a different type than the values stored in |lh|. |key_hash| -// and |cmp_key| must be compatible with the functions passed into -// |OPENSSL_lh_new|. +// OPENSSL_lh_retrieve_key finds an element matching `key`, given the specified +// hash and comparison function. This differs from `OPENSSL_lh_retrieve` in that +// the key may be a different type than the values stored in `lh`. `key_hash` +// and `cmp_key` must be compatible with the functions passed into +// `OPENSSL_lh_new`. OPENSSL_EXPORT void *OPENSSL_lh_retrieve_key(const _LHASH *lh, const void *key, uint32_t key_hash, int (*cmp_key)(const void *key, const void *value)); -// OPENSSL_lh_insert inserts |data| into the hash table. If an existing element -// is equal to |data| (with respect to the comparison function) then |*old_data| +// OPENSSL_lh_insert inserts `data` into the hash table. If an existing element +// is equal to `data` (with respect to the comparison function) then `*old_data` // will be set to that value and it will be replaced. Otherwise, or in the -// event of an error, |*old_data| will be set to NULL. It returns one on +// event of an error, `*old_data` will be set to NULL. It returns one on // success or zero in the case of an allocation error. OPENSSL_EXPORT int OPENSSL_lh_insert(_LHASH *lh, void **old_data, void *data, lhash_hash_func_helper call_hash_func, lhash_cmp_func_helper call_cmp_func); -// OPENSSL_lh_delete removes an element equal to |data| from the hash table and +// OPENSSL_lh_delete removes an element equal to `data` from the hash table and // returns it. If no such element is found, it returns NULL. OPENSSL_EXPORT void *OPENSSL_lh_delete(_LHASH *lh, const void *data, lhash_hash_func_helper call_hash_func, lhash_cmp_func_helper call_cmp_func); -// OPENSSL_lh_doall_arg calls |func| on each element of the hash table and also -// passes |arg| as the second argument. +// OPENSSL_lh_doall_arg calls `func` on each element of the hash table and also +// passes `arg` as the second argument. // TODO(fork): rename this OPENSSL_EXPORT void OPENSSL_lh_doall_arg(_LHASH *lh, void (*func)(void *, void *), @@ -122,9 +122,9 @@ OPENSSL_EXPORT void OPENSSL_lh_doall_arg(_LHASH *lh, /* We disable MSVC C4191 in this macro, which warns when pointers are cast \ * to the wrong type. While the cast itself is valid, it is often a bug \ * because calling it through the cast is UB. However, we never actually \ - * call functions as |lhash_cmp_func|. The type is just a type-erased \ + * call functions as `lhash_cmp_func`. The type is just a type-erased \ * function pointer. (C does not guarantee function pointers fit in \ - * |void*|, and GCC will warn on this.) Thus we just disable the false \ + * `void*`, and GCC will warn on this.) Thus we just disable the false \ * positive warning. */ \ OPENSSL_MSVC_PRAGMA(warning(push)) \ OPENSSL_MSVC_PRAGMA(warning(disable : 4191)) \ diff --git a/third_party/boringssl/src/crypto/lhash/lhash.cc b/third_party/boringssl/src/crypto/lhash/lhash.cc index b074bb86a..77a0c87e7 100644 --- a/third_party/boringssl/src/crypto/lhash/lhash.cc +++ b/third_party/boringssl/src/crypto/lhash/lhash.cc @@ -25,7 +25,7 @@ BSSL_NAMESPACE_BEGIN -// kMinNumBuckets is the minimum size of the buckets array in an |_LHASH|. +// kMinNumBuckets is the minimum size of the buckets array in an `_LHASH`. static const size_t kMinNumBuckets = 16; // kMaxAverageChainLength contains the maximum, average chain length. When the @@ -39,22 +39,22 @@ static const size_t kMinAverageChainLength = 1; struct LHASH_ITEM { void *data = nullptr; LHASH_ITEM *next = nullptr; - // hash contains the cached, hash value of |data|. + // hash contains the cached, hash value of `data`. uint32_t hash = 0; }; struct _LHASH { // num_items contains the total number of items in the hash table. size_t num_items = 0; - // buckets is an array of |num_buckets| pointers. Each points to the head of + // buckets is an array of `num_buckets` pointers. Each points to the head of // a chain of LHASH_ITEM objects that have the same hash value, mod - // |num_buckets|. + // `num_buckets`. LHASH_ITEM **buckets = nullptr; - // num_buckets contains the length of |buckets|. This value is always >= + // num_buckets contains the length of `buckets`. This value is always >= // kMinNumBuckets. size_t num_buckets = 0; - // callback_depth contains the current depth of |lh_doall| or |lh_doall_arg| - // calls. If non-zero then this suppresses resizing of the |buckets| array, + // callback_depth contains the current depth of `lh_doall` or `lh_doall_arg` + // calls. If non-zero then this suppresses resizing of the `buckets` array, // which would otherwise disrupt the iteration. unsigned callback_depth = 0; @@ -101,12 +101,12 @@ void OPENSSL_lh_free(_LHASH *lh) { size_t OPENSSL_lh_num_items(const _LHASH *lh) { return lh->num_items; } // get_next_ptr_and_hash returns a pointer to the pointer that points to the -// item equal to |data|. In other words, it searches for an item equal to |data| +// item equal to `data`. In other words, it searches for an item equal to `data` // and, if it's at the start of a chain, then it returns a pointer to an -// element of |lh->buckets|, otherwise it returns a pointer to the |next| -// element of the previous item in the chain. If an element equal to |data| is -// not found, it returns a pointer that points to a NULL pointer. If |out_hash| -// is not NULL, then it also puts the hash value of |data| in |*out_hash|. +// element of `lh->buckets`, otherwise it returns a pointer to the `next` +// element of the previous item in the chain. If an element equal to `data` is +// not found, it returns a pointer that points to a NULL pointer. If `out_hash` +// is not NULL, then it also puts the hash value of `data` in `*out_hash`. static LHASH_ITEM **get_next_ptr_and_hash(const _LHASH *lh, uint32_t *out_hash, const void *data, lhash_hash_func_helper call_hash_func, @@ -127,8 +127,8 @@ static LHASH_ITEM **get_next_ptr_and_hash(const _LHASH *lh, uint32_t *out_hash, return ret; } -// get_next_ptr_by_key behaves like |get_next_ptr_and_hash| but takes a key -// which may be a different type from the values stored in |lh|. +// get_next_ptr_by_key behaves like `get_next_ptr_and_hash` but takes a key +// which may be a different type from the values stored in `lh`. static LHASH_ITEM **get_next_ptr_by_key(const _LHASH *lh, const void *key, uint32_t key_hash, int (*cmp_key)(const void *key, @@ -160,8 +160,8 @@ void *OPENSSL_lh_retrieve_key(const _LHASH *lh, const void *key, return *next_ptr == nullptr ? nullptr : (*next_ptr)->data; } -// lh_rebucket allocates a new array of |new_num_buckets| pointers and -// redistributes the existing items into it before making it |lh->buckets| and +// lh_rebucket allocates a new array of `new_num_buckets` pointers and +// redistributes the existing items into it before making it `lh->buckets` and // freeing the old array. static void lh_rebucket(_LHASH *lh, const size_t new_num_buckets) { LHASH_ITEM **new_buckets, *cur, *next; @@ -192,7 +192,7 @@ static void lh_rebucket(_LHASH *lh, const size_t new_num_buckets) { lh->buckets = new_buckets; } -// lh_maybe_resize resizes the |buckets| array if needed. +// lh_maybe_resize resizes the `buckets` array if needed. static void lh_maybe_resize(_LHASH *lh) { size_t avg_chain_length; @@ -234,14 +234,14 @@ int OPENSSL_lh_insert(_LHASH *lh, void **old_data, void *data, if (*next_ptr != nullptr) { - // An element equal to |data| already exists in the hash table. It will be + // An element equal to `data` already exists in the hash table. It will be // replaced. *old_data = (*next_ptr)->data; (*next_ptr)->data = data; return 1; } - // An element equal to |data| doesn't exist in the hash table yet. + // An element equal to `data` doesn't exist in the hash table yet. item = New(); if (item == nullptr) { return 0; @@ -287,7 +287,7 @@ void OPENSSL_lh_doall_arg(_LHASH *lh, void (*func)(void *, void *), void *arg) { } if (lh->callback_depth < UINT_MAX) { - // |callback_depth| is a saturating counter. + // `callback_depth` is a saturating counter. lh->callback_depth++; } @@ -304,7 +304,7 @@ void OPENSSL_lh_doall_arg(_LHASH *lh, void (*func)(void *, void *), void *arg) { } // The callback may have added or removed elements and the non-zero value of - // |callback_depth| will have suppressed any resizing. Thus any needed + // `callback_depth` will have suppressed any resizing. Thus any needed // resizing is done here. lh_maybe_resize(lh); } diff --git a/third_party/boringssl/src/crypto/md5/md5.cc b/third_party/boringssl/src/crypto/md5/md5.cc index 0604ddc07..ba9147ea4 100644 --- a/third_party/boringssl/src/crypto/md5/md5.cc +++ b/third_party/boringssl/src/crypto/md5/md5.cc @@ -82,6 +82,48 @@ int MD5_Final(uint8_t out[MD5_DIGEST_LENGTH], MD5_CTX *c) { return 1; } +int MD5_marshal_state(const MD5_CTX *md5, uint8_t out[MD5_STATE_MAX_BYTES], + size_t *out_len) { + BSSL_CHECK(md5->num < MD5_CBLOCK); + CRYPTO_store_u32_le(out, md5->h[0]); + CRYPTO_store_u32_le(out + 4, md5->h[1]); + CRYPTO_store_u32_le(out + 8, md5->h[2]); + CRYPTO_store_u32_le(out + 12, md5->h[3]); + CRYPTO_store_u32_le(out + 16, md5->Nl); + CRYPTO_store_u32_le(out + 20, md5->Nh); + OPENSSL_memcpy(out + 24, md5->data, md5->num); + *out_len = 24 + md5->num; + return 1; +} + +int MD5_parse_state(MD5_CTX *md5, const uint8_t *in, size_t in_len) { + if (in_len < 24 || in_len > MD5_STATE_MAX_BYTES) { + return 0; + } + uint32_t nl = CRYPTO_load_u32_le(in + 16); + if ((nl & 7) != 0) { + return 0; + } + // The number of bytes left in `data` is the number of bits divided by 8, + // modulo the block size. + static_assert((MD5_CBLOCK & (MD5_CBLOCK - 1)) == 0, + "MD5_CBLOCK must be a power of 2"); + unsigned num = (nl >> 3) & (MD5_CBLOCK - 1); + if (in_len != 24 + num) { + return 0; + } + OPENSSL_memset(md5, 0, sizeof(MD5_CTX)); + md5->h[0] = CRYPTO_load_u32_le(in); + md5->h[1] = CRYPTO_load_u32_le(in + 4); + md5->h[2] = CRYPTO_load_u32_le(in + 8); + md5->h[3] = CRYPTO_load_u32_le(in + 12); + md5->Nl = nl; + md5->Nh = CRYPTO_load_u32_le(in + 20); + md5->num = num; + OPENSSL_memcpy(md5->data, in + 24, num); + return 1; +} + // As pointed out by Wei Dai , the above can be // simplified to the code below. Wei attributes these optimizations // to Peter Gutmann's SHS code, and he attributes it to Rich Schroeppel. diff --git a/third_party/boringssl/src/crypto/mem.cc b/third_party/boringssl/src/crypto/mem.cc index 28ab3debe..faedd5332 100644 --- a/third_party/boringssl/src/crypto/mem.cc +++ b/third_party/boringssl/src/crypto/mem.cc @@ -69,14 +69,14 @@ static void __asan_unpoison_memory_region(const void *addr, size_t size) {} #endif #if defined(BORINGSSL_DETECT_SDALLOCX) -// sdallocx is a sized |free| function. By passing the size (which we happen to +// sdallocx is a sized `free` function. By passing the size (which we happen to // always know in BoringSSL), the malloc implementation can save work. We cannot -// depend on |sdallocx| being available, however, so it's a weak symbol. +// depend on `sdallocx` being available, however, so it's a weak symbol. // -// This mechanism is kept opt-in because it assumes that, when |sdallocx| is -// defined, it is part of the same allocator as |malloc|. This is usually true -// but may break if |malloc| does not implement |sdallocx|, but some other -// allocator with |sdallocx| is imported which does. +// This mechanism is kept opt-in because it assumes that, when `sdallocx` is +// defined, it is part of the same allocator as `malloc`. This is usually true +// but may break if `malloc` does not implement `sdallocx`, but some other +// allocator with `sdallocx` is imported which does. WEAK_SYMBOL_FUNC(void, sdallocx, (void *ptr, size_t size, int flags)) #else static void (*const sdallocx)(void *ptr, size_t size, int flags) = nullptr; @@ -84,8 +84,8 @@ static void (*const sdallocx)(void *ptr, size_t size, int flags) = nullptr; // The following three functions can be defined to override default heap // allocation and freeing. If defined, it is the responsibility of -// |OPENSSL_memory_free| to zero out the memory before returning it to the -// system. |OPENSSL_memory_free| will not be passed NULL pointers. +// `OPENSSL_memory_free` to zero out the memory before returning it to the +// system. `OPENSSL_memory_free` will not be passed NULL pointers. // // WARNING: These functions are called on every allocation and free in // BoringSSL across the entire process. They may be called by any code in the diff --git a/third_party/boringssl/src/crypto/mem_internal.h b/third_party/boringssl/src/crypto/mem_internal.h index 78c3348d6..488052fac 100644 --- a/third_party/boringssl/src/crypto/mem_internal.h +++ b/third_party/boringssl/src/crypto/mem_internal.h @@ -33,24 +33,24 @@ BSSL_NAMESPACE_BEGIN // Internal allocation-dependent functions. // // This header is separate from crypto/internal.h because there are some files -// which must avoid |OPENSSL_malloc|, to avoid a circular dependency, but +// which must avoid `OPENSSL_malloc`, to avoid a circular dependency, but // need other support routines in crypto/internal.h. (See -// |_BORINGSSL_PROHIBIT_OPENSSL_MALLOC|.) +// `_BORINGSSL_PROHIBIT_OPENSSL_MALLOC`.) // Memory allocation. -// New behaves like |new| but uses |OPENSSL_malloc| for memory allocation. It +// New behaves like `new` but uses `OPENSSL_malloc` for memory allocation. It // returns nullptr on allocation error. It only implements single-object // allocation and not new T[n]. // // When called with no arguments, it performs value-initialization, not -// default-initialization. This means that, if selects a non-user-provided +// default-initialization. This means that, if it selects a non-user-provided // constructor, the object will be zero-initialized. (As in any C++ type, once -// |T| gains a user-provided constructors, it is responsible for initializing +// `T` gains a user-provided constructor, it is responsible for initializing // all fields explicitly.) // -// Note: unlike |new|, this does not support non-public constructors. +// Note: unlike `new`, this does not support non-public constructors. template T *New(Args &&...args) { void *t = OPENSSL_malloc(sizeof(T)); @@ -60,9 +60,9 @@ T *New(Args &&...args) { return new (t) T(std::forward(args)...); } -// Delete behaves like |delete| but uses |OPENSSL_free| to release memory. +// Delete behaves like `delete` but uses `OPENSSL_free` to release memory. // -// Note: unlike |delete| this does not support non-public destructors. +// Note: unlike `delete` this does not support non-public destructors. template void Delete(T *t) { if (t != nullptr) { @@ -74,9 +74,9 @@ void Delete(T *t) { namespace internal { // All types with kAllowUniquePtr set may be used with UniquePtr. Other types -// may be C structs which require a |BORINGSSL_MAKE_DELETER| registration. Where +// may be C structs which require a `BORINGSSL_MAKE_DELETER` registration. Where // an internal type cannot be annotated (e.g. an alias of std::variant), use -// |BORINGSSL_MAKE_DELETER(T, Delete)|. +// `BORINGSSL_MAKE_DELETER(T, Delete)`. template struct DeleterImpl> { static void Free(T *t) { Delete(t); } @@ -93,7 +93,7 @@ struct DeleterImpl> { // All types with kAllowRefCountedUniquePtr types also automatically get an // UpRef function. Other types may be C structs which require a -// |BORINGSSL_MAKE_UP_REF| registration. +// `BORINGSSL_MAKE_UP_REF` registration. template > inline UniquePtr UpRef(const T *v) { if (v != nullptr) { @@ -106,7 +106,7 @@ inline UniquePtr UpRef(const UniquePtr &ptr) { return UpRef(ptr.get()); } -// MakeUnique behaves like |std::make_unique| but returns nullptr on allocation +// MakeUnique behaves like `std::make_unique` but returns nullptr on allocation // error. template UniquePtr MakeUnique(Args &&...args) { @@ -128,13 +128,20 @@ class RefCounted { // These methods are intentionally named differently from `bssl::UpRef` to // avoid a collision. Only the implementations of `FOO_up_ref` and `FOO_free` - // should call these. |DecRefInternal| returns true if the object was freed + // should call these. `DecRefInternal` returns true if the object was freed // and false if there are still references. void UpRefInternal() const { - // Safety: the folowing call does not mutate anything other than the atomic + // Safety: the following call does not mutate anything other than the atomic // ref-count variable. CRYPTO_refcount_inc(&references_); } + + // Only the implementations of `FOO_dup_ref` should call these. + Derived *DupRefInternal() const { + UpRefInternal(); + return const_cast(static_cast(this)); + } + bool DecRefInternal() { if (CRYPTO_refcount_dec_and_test_zero(&references_)) { Derived *d = static_cast(this); @@ -168,7 +175,7 @@ class RefCounted { // Containers. -// Array is an owning array of elements of |T|. +// Array is an owning array of elements of `T`. template class Array { public: @@ -244,9 +251,9 @@ class Array { size_ = 0; } - // Init replaces the array with a newly-allocated array of |new_size| - // value-constructed copies of |T|. It returns true on success and false on - // error. If |T| is a primitive type like |uint8_t|, value-construction means + // Init replaces the array with a newly-allocated array of `new_size` + // value-constructed copies of `T`. It returns true on success and false on + // error. If `T` is a primitive type like `uint8_t`, value-construction means // it will be zero-initialized. [[nodiscard]] bool Init(size_t new_size) { if (!InitUninitialized(new_size)) { @@ -256,8 +263,8 @@ class Array { return true; } - // InitForOverwrite behaves like |Init| but it default-constructs each element - // instead. This means that, if |T| is a primitive type, the array will be + // InitForOverwrite behaves like `Init` but it default-constructs each element + // instead. This means that, if `T` is a primitive type, the array will be // uninitialized and thus must be filled in by the caller. [[nodiscard]] bool InitForOverwrite(size_t new_size) { if (!InitUninitialized(new_size)) { @@ -267,10 +274,10 @@ class Array { return true; } - // CopyFrom replaces the array with a newly-allocated copy of |in|. It returns + // CopyFrom replaces the array with a newly-allocated copy of `in`. It returns // true on success and false on error. // - // |in| may not alias |this|. + // `in` may not alias `this`. [[nodiscard]] bool CopyFrom(Span in) { BSSL_CHECK(!spans_alias(MakeConstSpan(*this), in)); if (!InitUninitialized(in.size())) { @@ -280,7 +287,7 @@ class Array { return true; } - // Shrink shrinks the stored size of the array to |new_size|. It crashes if + // Shrink shrinks the stored size of the array to `new_size`. It crashes if // the new size is larger. Note this does not shrink the allocation itself. void Shrink(size_t new_size) { if (new_size > size_) { @@ -292,7 +299,7 @@ class Array { private: // InitUninitialized replaces the array with a newly-allocated array of - // |new_size| elements, but whose constructor has not yet run. On success, the + // `new_size` elements, but whose constructor has not yet run. On success, the // elements must be constructed before returning control to the caller. bool InitUninitialized(size_t new_size) { Reset(); @@ -316,7 +323,7 @@ class Array { size_t size_ = 0; }; -// Vector is a resizable array of elements of |T|. +// Vector is a resizable array of elements of `T`. template class Vector { public: @@ -384,7 +391,7 @@ class Vector { size_--; } - // Push adds |elem| at the end of the internal array, growing if necessary. It + // Push adds `elem` at the end of the internal array, growing if necessary. It // returns false when allocation fails. [[nodiscard]] bool Push(T elem) { if (!MaybeGrow(1)) { @@ -395,7 +402,7 @@ class Vector { return true; } - // CopyFrom replaces the contents of the array with a copy of |in|. It returns + // CopyFrom replaces the contents of the array with a copy of `in`. It returns // true on success and false on allocation error. [[nodiscard]] bool CopyFrom(Span in) { Array copy; @@ -409,7 +416,7 @@ class Vector { return true; } - // Append appends the contents of |in| to the array. It returns true on + // Append appends the contents of `in` to the array. It returns true on // success and false on allocation error. [[nodiscard]] bool Append(Span in) { if (!MaybeGrow(in.size())) { @@ -420,7 +427,7 @@ class Vector { return true; } - // AppendMove moves the contents of |in| and appends them to the array. It + // AppendMove moves the contents of `in` and appends them to the array. It // returns true on success and false on allocation error. [[nodiscard]] bool AppendMove(Span in) { if (!MaybeGrow(in.size())) { @@ -431,7 +438,7 @@ class Vector { return true; } - // EraseIf removes all elements that satisfy the predicate |pred|. + // EraseIf removes all elements that satisfy the predicate `pred`. template void EraseIf(Pred pred) { auto it = std::remove_if(begin(), end(), pred); @@ -440,7 +447,7 @@ class Vector { } private: - // If there is no room for |num| elements, creates a new backing array with + // If there is no room for `num` elements, creates a new backing array with // double the size of the old one and copies elements over. [[nodiscard]] bool MaybeGrow(size_t num) { constexpr size_t kDefaultSize = 16; @@ -475,12 +482,12 @@ class Vector { return true; } - // data_ is a pointer to |capacity_| objects of size |T|, the first |size_| of + // data_ is a pointer to `capacity_` objects of size `T`, the first `size_` of // which are constructed. T *data_ = nullptr; - // |size_| is the number of elements stored in this Vector. + // `size_` is the number of elements stored in this Vector. size_t size_ = 0; - // |capacity_| is the number of elements allocated in this Vector. + // `capacity_` is the number of elements allocated in this Vector. size_t capacity_ = 0; }; @@ -560,8 +567,8 @@ class InplaceVector { Shrink(size_ - 1); } - // Shrink resizes the vector to |new_size|, which must not be larger than the - // current size. Unlike |Resize|, this can be called when |T| is not + // Shrink resizes the vector to `new_size`, which must not be larger than the + // current size. Unlike `Resize`, this can be called when `T` is not // default-constructible. void Shrink(size_t new_size) { BSSL_CHECK(new_size <= size_); @@ -569,8 +576,8 @@ class InplaceVector { size_ = static_cast>(new_size); } - // TryResize resizes the vector to |new_size| and returns true, or returns - // false if |new_size| is too large. Any newly-added elements are + // TryResize resizes the vector to `new_size` and returns true, or returns + // false if `new_size` is too large. Any newly-added elements are // value-initialized. [[nodiscard]] bool TryResize(size_t new_size) { if (new_size <= size_) { @@ -585,7 +592,7 @@ class InplaceVector { return true; } - // TryResizeForOverwrite behaves like |TryResize|, but newly-added elements + // TryResizeForOverwrite behaves like `TryResize`, but newly-added elements // are default-initialized, so POD types may contain uninitialized values that // the caller is responsible for filling in. [[nodiscard]] bool TryResizeForOverwrite(size_t new_size) { @@ -601,10 +608,10 @@ class InplaceVector { return true; } - // TryCopyFrom sets the vector to a copy of |in| and returns true, or returns - // false if |in| is too large. + // TryCopyFrom sets the vector to a copy of `in` and returns true, or returns + // false if `in` is too large. // - // |in| may not alias |this|. + // `in` may not alias `this`. [[nodiscard]] bool TryCopyFrom(Span in) { BSSL_CHECK(!spans_alias(MakeConstSpan(*this), in)); if (in.size() > capacity()) { @@ -616,8 +623,8 @@ class InplaceVector { return true; } - // TryAppend appends the vector by a copy of |in| and returns true, or - // returns false if |in| is too large. + // TryAppend appends the vector by a copy of `in` and returns true, or + // returns false if `in` is too large. [[nodiscard]] bool TryAppend(Span in) { if (in.size() > capacity() - size()) { return false; @@ -627,7 +634,7 @@ class InplaceVector { return true; } - // TryPushBack appends |val| to the vector and returns a pointer to the + // TryPushBack appends `val` to the vector and returns a pointer to the // newly-inserted value, or nullptr if the vector is at capacity. [[nodiscard]] T *TryPushBack(T val) { if (size() >= capacity()) { @@ -639,7 +646,7 @@ class InplaceVector { return ret; } - // The following methods behave like their |Try*| counterparts, but abort the + // The following methods behave like their `Try*` counterparts, but abort the // program on failure. void Resize(size_t size) { BSSL_CHECK(TryResize(size)); } void ResizeForOverwrite(size_t size) { @@ -653,7 +660,7 @@ class InplaceVector { return *ret; } - // EraseIf removes all elements that satisfy the predicate |pred|. + // EraseIf removes all elements that satisfy the predicate `pred`. template void EraseIf(Pred pred) { auto it = std::remove_if(begin(), end(), pred); @@ -665,6 +672,41 @@ class InplaceVector { PackedSize size_ = 0; }; +// A MaybeInplaceArray is like an `Array`, but backed by an `InplaceVector` if +// `size() <= N`, and by an `Array` if `size() > N`. +template +class MaybeInplaceArray { + public: + // CopyFrom replaces the array with a newly-allocated copy of `in`. It returns + // true on success and false on error. + // + // `in` may not alias `this`. + [[nodiscard]] bool CopyFrom(Span in) { + if (in.size() <= N) { + small_.CopyFrom(in); + large_.Reset(); + } else { + if (!large_.CopyFrom(in)) { + return false; + } + small_.clear(); + } + return true; + } + + // Minimal methods to allow conversion to a `Span`. + const T *data() const { return IsSmall() ? small_.data() : large_.data(); } + T *data() { return IsSmall() ? small_.data() : large_.data(); } + size_t size() const { return IsSmall() ? small_.size() : large_.size(); } + + private: + bool IsSmall() const { return large_.empty(); } + + // TODO(crbug.com/548222332): Optimize storage by putting this on the stack. + // Invariant: at least one of these two is empty. + Array large_; + InplaceVector small_; +}; BSSL_NAMESPACE_END diff --git a/third_party/boringssl/src/crypto/obj/obj.cc b/third_party/boringssl/src/crypto/obj/obj.cc index a6e6e5a86..98b4745c7 100644 --- a/third_party/boringssl/src/crypto/obj/obj.cc +++ b/third_party/boringssl/src/crypto/obj/obj.cc @@ -29,7 +29,7 @@ #include "../internal.h" #include "../lhash/internal.h" -// obj_data.h must be included after the definition of |ASN1_OBJECT|. +// obj_data.h must be included after the definition of `ASN1_OBJECT`. #include "obj_dat.h" @@ -42,7 +42,7 @@ DEFINE_LHASH_OF(ASN1_OBJECT) BSSL_NAMESPACE_END static StaticMutex global_added_lock; -// These globals are protected by |global_added_lock|. +// These globals are protected by `global_added_lock`. static LHASH_OF(ASN1_OBJECT) *global_added_by_data = nullptr; static LHASH_OF(ASN1_OBJECT) *global_added_by_nid = nullptr; static LHASH_OF(ASN1_OBJECT) *global_added_by_short_name = nullptr; @@ -57,63 +57,48 @@ static int obj_next_nid() { } ASN1_OBJECT *OBJ_dup(const ASN1_OBJECT *o) { - ASN1_OBJECT *r; - unsigned char *data = nullptr; - char *sn = nullptr, *ln = nullptr; - if (o == nullptr) { return nullptr; } if (!(o->flags & ASN1_OBJECT_FLAG_DYNAMIC)) { // TODO(fork): this is a little dangerous. - return (ASN1_OBJECT *)o; + return const_cast(o); } - r = ASN1_OBJECT_new(); + UniquePtr r(ASN1_OBJECT_new()); if (r == nullptr) { OPENSSL_PUT_ERROR(OBJ, ERR_R_ASN1_LIB); return nullptr; } - r->ln = r->sn = nullptr; - // once data is attached to an object, it remains const + // All fields of the object will be allocated. + r->flags = o->flags | ASN1_OBJECT_FLAG_DYNAMIC | + ASN1_OBJECT_FLAG_DYNAMIC_STRINGS | ASN1_OBJECT_FLAG_DYNAMIC_DATA; + r->data = reinterpret_cast(OPENSSL_memdup(o->data, o->length)); if (o->length != 0 && r->data == nullptr) { - goto err; + return nullptr; } r->length = o->length; r->nid = o->nid; if (o->ln != nullptr) { - ln = OPENSSL_strdup(o->ln); - if (ln == nullptr) { - goto err; + r->ln = OPENSSL_strdup(o->ln); + if (r->ln == nullptr) { + return nullptr; } } if (o->sn != nullptr) { - sn = OPENSSL_strdup(o->sn); - if (sn == nullptr) { - goto err; + r->sn = OPENSSL_strdup(o->sn); + if (r->sn == nullptr) { + return nullptr; } } - r->sn = sn; - r->ln = ln; - - r->flags = - o->flags | (ASN1_OBJECT_FLAG_DYNAMIC | ASN1_OBJECT_FLAG_DYNAMIC_STRINGS | - ASN1_OBJECT_FLAG_DYNAMIC_DATA); - return r; - -err: - OPENSSL_free(ln); - OPENSSL_free(sn); - OPENSSL_free(data); - OPENSSL_free(r); - return nullptr; + return r.release(); } int OBJ_cmp(const ASN1_OBJECT *a, const ASN1_OBJECT *b) { @@ -142,14 +127,14 @@ size_t OBJ_length(const ASN1_OBJECT *obj) { } static const ASN1_OBJECT *get_builtin_object(int nid) { - // |NID_undef| is stored separately, so all the indices are off by one. The + // `NID_undef` is stored separately, so all the indices are off by one. The // caller of this function must have a valid built-in, non-undef NID. BSSL_CHECK(nid > 0 && nid < NUM_NID); return &kObjects[nid - 1]; } -// obj_cmp is called to search the kNIDsInOIDOrder array. The |key| argument is -// an |ASN1_OBJECT|* that we're looking for and |element| is a pointer to an +// obj_cmp is called to search the kNIDsInOIDOrder array. The `key` argument is +// an `ASN1_OBJECT`* that we're looking for and `element` is a pointer to an // unsigned int in the array. static int obj_cmp(const void *key, const void *element) { uint16_t nid = *((const uint16_t *)element); @@ -200,7 +185,7 @@ int OBJ_cbs2nid(const CBS *cbs) { } // short_name_cmp is called to search the kNIDsInShortNameOrder array. The -// |key| argument is name that we're looking for and |element| is a pointer to +// `key` argument is name that we're looking for and `element` is a pointer to // an unsigned int in the array. static int short_name_cmp(const void *key, const void *element) { const char *name = (const char *)key; @@ -234,7 +219,7 @@ int OBJ_sn2nid(const char *short_name) { } // long_name_cmp is called to search the kNIDsInLongNameOrder array. The -// |key| argument is name that we're looking for and |element| is a pointer to +// `key` argument is name that we're looking for and `element` is a pointer to // an unsigned int in the array. static int long_name_cmp(const void *key, const void *element) { const char *name = (const char *)key; @@ -450,7 +435,7 @@ static int cmp_long_name(const ASN1_OBJECT *a, const ASN1_OBJECT *b) { return strcmp(a->ln, b->ln); } -// obj_add_object inserts |obj| into the various global hashes for run-time +// obj_add_object inserts `obj` into the various global hashes for run-time // added objects. It returns one on success or zero otherwise. static int obj_add_object(ASN1_OBJECT *obj) { obj->flags &= ~(ASN1_OBJECT_FLAG_DYNAMIC | ASN1_OBJECT_FLAG_DYNAMIC_STRINGS | @@ -479,10 +464,10 @@ static int obj_add_object(ASN1_OBJECT *obj) { return 0; } - // We don't pay attention to |old_object| (which contains any previous object + // We don't pay attention to `old_object` (which contains any previous object // that was evicted from the hashes) because we don't have a reference count // on ASN1_OBJECT values. Also, we should never have duplicates nids and so - // should always have objects in |global_added_by_nid|. + // should always have objects in `global_added_by_nid`. ASN1_OBJECT *old_object; int ok = lh_ASN1_OBJECT_insert(global_added_by_nid, &old_object, obj); if (obj->length != 0 && obj->data != nullptr) { diff --git a/third_party/boringssl/src/crypto/obj/obj_dat.h b/third_party/boringssl/src/crypto/obj/obj_dat.h index feb8f2d1e..0e7d4c2d0 100644 --- a/third_party/boringssl/src/crypto/obj/obj_dat.h +++ b/third_party/boringssl/src/crypto/obj/obj_dat.h @@ -16,7 +16,7 @@ BSSL_NAMESPACE_BEGIN -#define NUM_NID 973 +#define NUM_NID 979 static const uint8_t kObjectData[] = { /* NID_rsadsi */ @@ -7146,6 +7146,63 @@ static const uint8_t kObjectData[] = { 0x04, 0x04, 0x02, + /* NID_SLH_DSA_SHA2_128s */ + 0x60, + 0x86, + 0x48, + 0x01, + 0x65, + 0x03, + 0x04, + 0x03, + 0x14, + /* NID_SLH_DSA_SHAKE_256f */ + 0x60, + 0x86, + 0x48, + 0x01, + 0x65, + 0x03, + 0x04, + 0x03, + 0x1f, + /* NID_SLH_DSA_SHA2_128s_WITH_SHA256 */ + 0x60, + 0x86, + 0x48, + 0x01, + 0x65, + 0x03, + 0x04, + 0x03, + 0x23, + /* NID_alg_mtcProof */ + 0x2b, + 0x06, + 0x01, + 0x05, + 0x05, + 0x07, + 0x06, + 0x43, + /* NID_rdna_trustAnchorID */ + 0x2b, + 0x06, + 0x01, + 0x05, + 0x05, + 0x07, + 0x19, + 0x03, + /* NID_pe_mtcCertificationAuthority_SHA256 */ + 0x2b, + 0x06, + 0x01, + 0x05, + 0x05, + 0x07, + 0x01, + 0x26, }; static const ASN1_OBJECT kObjects[NUM_NID] = { @@ -8802,6 +8859,19 @@ static const ASN1_OBJECT kObjects[NUM_NID] = { 0}, {NULL, NULL, NID_undef, 0, NULL, 0}, {"X-Wing", "X-Wing", NID_X_Wing, 0, NULL, 0}, + {"id-slh-dsa-sha2-128s", "SLH-DSA-SHA2-128s", NID_SLH_DSA_SHA2_128s, 9, + &kObjectData[6241], 0}, + {"id-slh-dsa-shake-256f", "SLH-DSA-SHAKE-256f", NID_SLH_DSA_SHAKE_256f, 9, + &kObjectData[6250], 0}, + {"id-hash-slh-dsa-sha2-128s-with-sha256", "SLH-DSA-SHA2-128s-WITH-SHA256", + NID_SLH_DSA_SHA2_128s_WITH_SHA256, 9, &kObjectData[6259], 0}, + {"alg-mtcProof", "alg-mtcProof", NID_alg_mtcProof, 8, &kObjectData[6268], + 0}, + {"rdna-trustAnchorID", "rdna-trustAnchorID", NID_rdna_trustAnchorID, 8, + &kObjectData[6276], 0}, + {"pe-mtcCertificationAuthority-SHA256", + "pe-mtcCertificationAuthority-SHA256", + NID_pe_mtcCertificationAuthority_SHA256, 8, &kObjectData[6284], 0}, }; static const uint16_t kNIDsInShortNameOrder[] = { @@ -9017,6 +9087,7 @@ static const uint16_t kNIDsInShortNameOrder[] = { 368 /* acceptableResponses */, 446 /* account */, 363 /* ad_timestamping */, + 976 /* alg-mtcProof */, 376 /* algorithm */, 405 /* ansi-X9-62 */, 910 /* anyExtendedKeyUsage */, @@ -9275,6 +9346,7 @@ static const uint16_t kNIDsInShortNameOrder[] = { 331 /* id-cmc-transactionId */, 787 /* id-ct-asciiTextWithCRLF */, 408 /* id-ecPublicKey */, + 975 /* id-hash-slh-dsa-sha2-128s-with-sha256 */, 508 /* id-hex-multipart-message */, 507 /* id-hex-partial-message */, 260 /* id-it */, @@ -9346,6 +9418,8 @@ static const uint16_t kNIDsInShortNameOrder[] = { 322 /* id-regInfo-certReq */, 321 /* id-regInfo-utf8Pairs */, 512 /* id-set */, + 973 /* id-slh-dsa-sha2-128s */, + 974 /* id-slh-dsa-shake-256f */, 191 /* id-smime-aa */, 215 /* id-smime-aa-contentHint */, 218 /* id-smime-aa-contentIdentifier */, @@ -9478,6 +9552,7 @@ static const uint16_t kNIDsInShortNameOrder[] = { 489 /* pagerTelephoneNumber */, 374 /* path */, 112 /* pbeWithMD5AndCast5CBC */, + 978 /* pe-mtcCertificationAuthority-SHA256 */, 499 /* personalSignature */, 487 /* personalTitle */, 464 /* photo */, @@ -9531,6 +9606,7 @@ static const uint16_t kNIDsInShortNameOrder[] = { 286 /* qcStatements */, 457 /* qualityLabelledData */, 450 /* rFC822localPart */, + 977 /* rdna-trustAnchorID */, 870 /* registeredAddress */, 400 /* role */, 877 /* roleOccupant */, @@ -9871,6 +9947,9 @@ static const uint16_t kNIDsInLongNameOrder[] = { 2 /* RSA Data Security, Inc. PKCS */, 188 /* S/MIME */, 167 /* S/MIME Capabilities */, + 973 /* SLH-DSA-SHA2-128s */, + 975 /* SLH-DSA-SHA2-128s-WITH-SHA256 */, + 974 /* SLH-DSA-SHAKE-256f */, 387 /* SNMPv2 */, 512 /* Secure Electronic Transactions */, 386 /* Security */, @@ -9954,6 +10033,7 @@ static const uint16_t kNIDsInLongNameOrder[] = { 901 /* aes-256-gcm */, 428 /* aes-256-ofb */, 914 /* aes-256-xts */, + 976 /* alg-mtcProof */, 376 /* algorithm */, 484 /* associatedDomain */, 485 /* associatedName */, @@ -10432,6 +10512,7 @@ static const uint16_t kNIDsInLongNameOrder[] = { 145 /* pbeWithSHA1And40BitRC4 */, 170 /* pbeWithSHA1AndDES-CBC */, 68 /* pbeWithSHA1AndRC2-CBC */, + 978 /* pe-mtcCertificationAuthority-SHA256 */, 499 /* personalSignature */, 487 /* personalTitle */, 464 /* photo */, @@ -10491,6 +10572,7 @@ static const uint16_t kNIDsInLongNameOrder[] = { 122 /* rc5-cfb */, 121 /* rc5-ecb */, 123 /* rc5-ofb */, + 977 /* rdna-trustAnchorID */, 870 /* registeredAddress */, 460 /* rfc822Mailbox */, 117 /* ripemd160 */, @@ -11232,6 +11314,7 @@ static const uint16_t kNIDsInOIDOrder[] = { 397 /* 1.3.6.1.5.5.7.1.10 (OBJ_ac_proxying) */, 398 /* 1.3.6.1.5.5.7.1.11 (OBJ_sinfo_access) */, 663 /* 1.3.6.1.5.5.7.1.14 (OBJ_proxyCertInfo) */, + 978 /* 1.3.6.1.5.5.7.1.38 (OBJ_pe_mtcCertificationAuthority_SHA256) */, 164 /* 1.3.6.1.5.5.7.2.1 (OBJ_id_qt_cps) */, 165 /* 1.3.6.1.5.5.7.2.2 (OBJ_id_qt_unotice) */, 293 /* 1.3.6.1.5.5.7.2.3 (OBJ_textNotice) */, @@ -11267,6 +11350,7 @@ static const uint16_t kNIDsInOIDOrder[] = { 324 /* 1.3.6.1.5.5.7.6.2 (OBJ_id_alg_noSignature) */, 325 /* 1.3.6.1.5.5.7.6.3 (OBJ_id_alg_dh_sig_hmac_sha1) */, 326 /* 1.3.6.1.5.5.7.6.4 (OBJ_id_alg_dh_pop) */, + 976 /* 1.3.6.1.5.5.7.6.67 (OBJ_alg_mtcProof) */, 327 /* 1.3.6.1.5.5.7.7.1 (OBJ_id_cmc_statusInfo) */, 328 /* 1.3.6.1.5.5.7.7.2 (OBJ_id_cmc_identification) */, 329 /* 1.3.6.1.5.5.7.7.3 (OBJ_id_cmc_identityProof) */, @@ -11307,6 +11391,7 @@ static const uint16_t kNIDsInOIDOrder[] = { 664 /* 1.3.6.1.5.5.7.21.0 (OBJ_id_ppl_anyLanguage) */, 665 /* 1.3.6.1.5.5.7.21.1 (OBJ_id_ppl_inheritAll) */, 667 /* 1.3.6.1.5.5.7.21.2 (OBJ_Independent) */, + 977 /* 1.3.6.1.5.5.7.25.3 (OBJ_rdna_trustAnchorID) */, 178 /* 1.3.6.1.5.5.7.48.1 (OBJ_ad_OCSP) */, 179 /* 1.3.6.1.5.5.7.48.2 (OBJ_ad_ca_issuers) */, 363 /* 1.3.6.1.5.5.7.48.3 (OBJ_ad_timeStamping) */, @@ -11445,6 +11530,9 @@ static const uint16_t kNIDsInOIDOrder[] = { 967 /* 2.16.840.1.101.3.4.3.17 (OBJ_ML_DSA_44) */, 968 /* 2.16.840.1.101.3.4.3.18 (OBJ_ML_DSA_65) */, 969 /* 2.16.840.1.101.3.4.3.19 (OBJ_ML_DSA_87) */, + 973 /* 2.16.840.1.101.3.4.3.20 (OBJ_SLH_DSA_SHA2_128s) */, + 974 /* 2.16.840.1.101.3.4.3.31 (OBJ_SLH_DSA_SHAKE_256f) */, + 975 /* 2.16.840.1.101.3.4.3.35 (OBJ_SLH_DSA_SHA2_128s_WITH_SHA256) */, 970 /* 2.16.840.1.101.3.4.4.2 (OBJ_ML_KEM_768) */, 966 /* 2.16.840.1.101.3.4.4.3 (OBJ_ML_KEM_1024) */, 71 /* 2.16.840.1.113730.1.1 (OBJ_netscape_cert_type) */, diff --git a/third_party/boringssl/src/crypto/pem/internal.h b/third_party/boringssl/src/crypto/pem/internal.h index 656dffbab..117cfa0bf 100644 --- a/third_party/boringssl/src/crypto/pem/internal.h +++ b/third_party/boringssl/src/crypto/pem/internal.h @@ -23,21 +23,107 @@ BSSL_NAMESPACE_BEGIN -// PEM_get_EVP_CIPHER_INFO decodes |header| as a PEM header block and writes the -// specified cipher and IV to |cipher|. It returns one on success and zero on -// error. |header| must be a NUL-terminated string. If |header| does not +// These macros make the PEM_read/PEM_write functions easier to maintain and +// write. Now they are all implemented with either: +// IMPLEMENT_PEM_rw(...) or IMPLEMENT_PEM_rw_cb(...) + +#define IMPLEMENT_PEM_read_fp(name, type, str, asn1) \ + static void *pem_read_##name##_d2i(void **x, const unsigned char **inp, \ + long len) { \ + return d2i_##asn1((type **)x, inp, len); \ + } \ + OPENSSL_EXPORT type *PEM_read_##name(FILE *fp, type **x, \ + pem_password_cb *cb, void *u) { \ + return (type *)PEM_ASN1_read(pem_read_##name##_d2i, str, fp, (void **)x, \ + cb, u); \ + } + +#define IMPLEMENT_PEM_write_fp(name, type, str, asn1) \ + static int pem_write_##name##_i2d(const void *x, unsigned char **outp) { \ + return i2d_##asn1((const type *)x, outp); \ + } \ + OPENSSL_EXPORT int PEM_write_##name(FILE *fp, const type *x) { \ + return PEM_ASN1_write(pem_write_##name##_i2d, str, fp, x, NULL, NULL, 0, \ + NULL, NULL); \ + } + +#define IMPLEMENT_PEM_write_cb_fp(name, type, str, asn1) \ + static int pem_write_##name##_i2d(const void *x, unsigned char **outp) { \ + return i2d_##asn1((const type *)x, outp); \ + } \ + OPENSSL_EXPORT int PEM_write_##name( \ + FILE *fp, const type *x, const EVP_CIPHER *enc, \ + const unsigned char *pass, int pass_len, pem_password_cb *cb, void *u) { \ + return PEM_ASN1_write(pem_write_##name##_i2d, str, fp, x, enc, pass, \ + pass_len, cb, u); \ + } + +#define IMPLEMENT_PEM_read_bio(name, type, str, asn1) \ + static void *pem_read_bio_##name##_d2i(void **x, const unsigned char **inp, \ + long len) { \ + return d2i_##asn1((type **)x, inp, len); \ + } \ + OPENSSL_EXPORT type *PEM_read_bio_##name(BIO *bp, type **x, \ + pem_password_cb *cb, void *u) { \ + return (type *)PEM_ASN1_read_bio(pem_read_bio_##name##_d2i, str, bp, \ + (void **)x, cb, u); \ + } + +#define IMPLEMENT_PEM_write_bio(name, type, str, asn1) \ + static int pem_write_bio_##name##_i2d(const void *x, unsigned char **outp) { \ + return i2d_##asn1((const type *)x, outp); \ + } \ + OPENSSL_EXPORT int PEM_write_bio_##name(BIO *bp, const type *x) { \ + return PEM_ASN1_write_bio(pem_write_bio_##name##_i2d, str, bp, x, NULL, \ + NULL, 0, NULL, NULL); \ + } + +#define IMPLEMENT_PEM_write_cb_bio(name, type, str, asn1) \ + static int pem_write_bio_##name##_i2d(const void *x, unsigned char **outp) { \ + return i2d_##asn1((const type *)x, outp); \ + } \ + OPENSSL_EXPORT int PEM_write_bio_##name( \ + BIO *bp, const type *x, const EVP_CIPHER *enc, \ + const unsigned char *pass, int pass_len, pem_password_cb *cb, void *u) { \ + return PEM_ASN1_write_bio(pem_write_bio_##name##_i2d, str, bp, x, enc, \ + pass, pass_len, cb, u); \ + } + +#define IMPLEMENT_PEM_write(name, type, str, asn1) \ + IMPLEMENT_PEM_write_bio(name, type, str, asn1) \ + IMPLEMENT_PEM_write_fp(name, type, str, asn1) + +#define IMPLEMENT_PEM_write_cb(name, type, str, asn1) \ + IMPLEMENT_PEM_write_cb_bio(name, type, str, asn1) \ + IMPLEMENT_PEM_write_cb_fp(name, type, str, asn1) + +#define IMPLEMENT_PEM_read(name, type, str, asn1) \ + IMPLEMENT_PEM_read_bio(name, type, str, asn1) \ + IMPLEMENT_PEM_read_fp(name, type, str, asn1) + +#define IMPLEMENT_PEM_rw(name, type, str, asn1) \ + IMPLEMENT_PEM_read(name, type, str, asn1) \ + IMPLEMENT_PEM_write(name, type, str, asn1) + +#define IMPLEMENT_PEM_rw_cb(name, type, str, asn1) \ + IMPLEMENT_PEM_read(name, type, str, asn1) \ + IMPLEMENT_PEM_write_cb(name, type, str, asn1) + +// PEM_get_EVP_CIPHER_INFO decodes `header` as a PEM header block and writes the +// specified cipher and IV to `cipher`. It returns one on success and zero on +// error. `header` must be a NUL-terminated string. If `header` does not // specify encryption, this function will return success and set -// |cipher->cipher| to NULL. +// `cipher->cipher` to NULL. int PEM_get_EVP_CIPHER_INFO(const char *header, EVP_CIPHER_INFO *cipher); -// PEM_do_header decrypts |*len| bytes from |data| in-place according to the -// information in |cipher|. On success, it returns one and sets |*len| to the -// length of the plaintext. Otherwise, it returns zero. If |cipher| specifies -// encryption, the key is derived from a password returned from |callback|. +// PEM_do_header decrypts `*len` bytes from `data` in-place according to the +// information in `cipher`. On success, it returns one and sets `*len` to the +// length of the plaintext. Otherwise, it returns zero. If `cipher` specifies +// encryption, the key is derived from a password returned from `callback`. int PEM_do_header(const EVP_CIPHER_INFO *cipher, uint8_t *data, size_t *len, pem_password_cb *callback, void *u); -// PEM_read_bio_inner differs from |PEM_read_bio| on the out pointer |len| +// PEM_read_bio_inner differs from `PEM_read_bio` on the out pointer `len` // so that it guarantee non-negativeness on this output and it takes in // owned types. int PEM_read_bio_inner(BIO *bp, bssl::UniquePtr *name, diff --git a/third_party/boringssl/src/crypto/pem/pem_all.cc b/third_party/boringssl/src/crypto/pem/pem_all.cc index f03b6080c..7d0f1e3dd 100644 --- a/third_party/boringssl/src/crypto/pem/pem_all.cc +++ b/third_party/boringssl/src/crypto/pem/pem_all.cc @@ -23,9 +23,8 @@ #include #include -static RSA *pkey_get_rsa(EVP_PKEY *key, RSA **rsa); -static DSA *pkey_get_dsa(EVP_PKEY *key, DSA **dsa); -static EC_KEY *pkey_get_eckey(EVP_PKEY *key, EC_KEY **eckey); +#include "internal.h" + IMPLEMENT_PEM_rw(X509_REQ, X509_REQ, PEM_STRING_X509_REQ, X509_REQ) @@ -33,11 +32,13 @@ IMPLEMENT_PEM_write(X509_REQ_NEW, X509_REQ, PEM_STRING_X509_REQ_OLD, X509_REQ) IMPLEMENT_PEM_rw(X509_CRL, X509_CRL, PEM_STRING_X509_CRL, X509_CRL) IMPLEMENT_PEM_rw(PKCS7, PKCS7, PEM_STRING_PKCS7, PKCS7) +namespace { + // We treat RSA or DSA private keys as a special case. For private keys we // read in an EVP_PKEY structure with PEM_read_bio_PrivateKey() and extract // the relevant private key: this means can handle "traditional" and PKCS#8 // formats transparently. -static RSA *pkey_get_rsa(EVP_PKEY *key, RSA **rsa) { +RSA *pkey_get_rsa(EVP_PKEY *key, RSA **rsa) { if (!key) { return nullptr; } @@ -57,6 +58,8 @@ static RSA *pkey_get_rsa(EVP_PKEY *key, RSA **rsa) { return rtmp; } +} // namespace + RSA *PEM_read_bio_RSAPrivateKey(BIO *bp, RSA **rsa, pem_password_cb *cb, void *u) { bssl::UniquePtr pkey(PEM_read_bio_PrivateKey(bp, nullptr, cb, u)); @@ -68,13 +71,16 @@ RSA *PEM_read_RSAPrivateKey(FILE *fp, RSA **rsa, pem_password_cb *cb, void *u) { return pkey_get_rsa(pkey.get(), rsa); } -IMPLEMENT_PEM_write_cb_const(RSAPrivateKey, RSA, PEM_STRING_RSA, RSAPrivateKey) +IMPLEMENT_PEM_write_cb(RSAPrivateKey, RSA, PEM_STRING_RSA, RSAPrivateKey) -IMPLEMENT_PEM_rw_const(RSAPublicKey, RSA, PEM_STRING_RSA_PUBLIC, RSAPublicKey) +IMPLEMENT_PEM_rw(RSAPublicKey, RSA, PEM_STRING_RSA_PUBLIC, RSAPublicKey) IMPLEMENT_PEM_rw(RSA_PUBKEY, RSA, PEM_STRING_PUBLIC, RSA_PUBKEY) + #ifndef OPENSSL_NO_DSA -static DSA *pkey_get_dsa(EVP_PKEY *key, DSA **dsa) { +namespace { + +DSA *pkey_get_dsa(EVP_PKEY *key, DSA **dsa) { DSA *dtmp; if (!key) { return nullptr; @@ -91,6 +97,8 @@ static DSA *pkey_get_dsa(EVP_PKEY *key, DSA **dsa) { return dtmp; } +} // namespace + DSA *PEM_read_bio_DSAPrivateKey(BIO *bp, DSA **dsa, pem_password_cb *cb, void *u) { EVP_PKEY *pktmp; @@ -98,7 +106,7 @@ DSA *PEM_read_bio_DSAPrivateKey(BIO *bp, DSA **dsa, pem_password_cb *cb, return pkey_get_dsa(pktmp, dsa); // will free pktmp } -IMPLEMENT_PEM_write_cb_const(DSAPrivateKey, DSA, PEM_STRING_DSA, DSAPrivateKey) +IMPLEMENT_PEM_write_cb(DSAPrivateKey, DSA, PEM_STRING_DSA, DSAPrivateKey) IMPLEMENT_PEM_rw(DSA_PUBKEY, DSA, PEM_STRING_PUBLIC, DSA_PUBKEY) DSA *PEM_read_DSAPrivateKey(FILE *fp, DSA **dsa, pem_password_cb *cb, void *u) { @@ -107,9 +115,12 @@ DSA *PEM_read_DSAPrivateKey(FILE *fp, DSA **dsa, pem_password_cb *cb, void *u) { return pkey_get_dsa(pktmp, dsa); // will free pktmp } -IMPLEMENT_PEM_rw_const(DSAparams, DSA, PEM_STRING_DSAPARAMS, DSAparams) +IMPLEMENT_PEM_rw(DSAparams, DSA, PEM_STRING_DSAPARAMS, DSAparams) #endif -static EC_KEY *pkey_get_eckey(EVP_PKEY *key, EC_KEY **eckey) { + +namespace { + +EC_KEY *pkey_get_eckey(EVP_PKEY *key, EC_KEY **eckey) { EC_KEY *dtmp; if (!key) { return nullptr; @@ -126,6 +137,8 @@ static EC_KEY *pkey_get_eckey(EVP_PKEY *key, EC_KEY **eckey) { return dtmp; } +} // namespace + EC_KEY *PEM_read_bio_ECPrivateKey(BIO *bp, EC_KEY **key, pem_password_cb *cb, void *u) { EVP_PKEY *pktmp; @@ -145,6 +158,6 @@ EC_KEY *PEM_read_ECPrivateKey(FILE *fp, EC_KEY **eckey, pem_password_cb *cb, } -IMPLEMENT_PEM_rw_const(DHparams, DH, PEM_STRING_DHPARAMS, DHparams) +IMPLEMENT_PEM_rw(DHparams, DH, PEM_STRING_DHPARAMS, DHparams) IMPLEMENT_PEM_rw(PUBKEY, EVP_PKEY, PEM_STRING_PUBLIC, PUBKEY) diff --git a/third_party/boringssl/src/crypto/pem/pem_info.cc b/third_party/boringssl/src/crypto/pem/pem_info.cc index a75e7e41e..271e7955b 100644 --- a/third_party/boringssl/src/crypto/pem/pem_info.cc +++ b/third_party/boringssl/src/crypto/pem/pem_info.cc @@ -33,9 +33,11 @@ using namespace bssl; -static X509_PKEY *X509_PKEY_new() { return New(); } +namespace { -static void X509_PKEY_free(X509_PKEY *x) { +X509_PKEY *X509_PKEY_new() { return New(); } + +void X509_PKEY_free(X509_PKEY *x) { if (x == nullptr) { return; } @@ -44,7 +46,9 @@ static void X509_PKEY_free(X509_PKEY *x) { Delete(x); } -static X509_INFO *X509_INFO_new() { return New(); } +X509_INFO *X509_INFO_new() { return New(); } + +} // namespace void X509_INFO_free(X509_INFO *x) { if (x == nullptr) { @@ -71,14 +75,16 @@ STACK_OF(X509_INFO) *PEM_X509_INFO_read(FILE *fp, STACK_OF(X509_INFO) *sk, return ret; } +namespace { + enum parse_result_t { parse_ok, parse_error, parse_new_entry, }; -static enum parse_result_t parse_x509(X509_INFO *info, const uint8_t *data, - size_t len, int key_type) { +enum parse_result_t parse_x509(X509_INFO *info, const uint8_t *data, size_t len, + int key_type) { if (info->x509 != nullptr) { return parse_new_entry; } @@ -86,8 +92,8 @@ static enum parse_result_t parse_x509(X509_INFO *info, const uint8_t *data, return info->x509 != nullptr ? parse_ok : parse_error; } -static enum parse_result_t parse_x509_aux(X509_INFO *info, const uint8_t *data, - size_t len, int key_type) { +enum parse_result_t parse_x509_aux(X509_INFO *info, const uint8_t *data, + size_t len, int key_type) { if (info->x509 != nullptr) { return parse_new_entry; } @@ -95,8 +101,8 @@ static enum parse_result_t parse_x509_aux(X509_INFO *info, const uint8_t *data, return info->x509 != nullptr ? parse_ok : parse_error; } -static enum parse_result_t parse_crl(X509_INFO *info, const uint8_t *data, - size_t len, int key_type) { +enum parse_result_t parse_crl(X509_INFO *info, const uint8_t *data, size_t len, + int key_type) { if (info->crl != nullptr) { return parse_new_entry; } @@ -104,8 +110,8 @@ static enum parse_result_t parse_crl(X509_INFO *info, const uint8_t *data, return info->crl != nullptr ? parse_ok : parse_error; } -static enum parse_result_t parse_key(X509_INFO *info, const uint8_t *data, - size_t len, int key_type) { +enum parse_result_t parse_key(X509_INFO *info, const uint8_t *data, size_t len, + int key_type) { if (info->x_pkey != nullptr) { return parse_new_entry; } @@ -117,6 +123,8 @@ static enum parse_result_t parse_key(X509_INFO *info, const uint8_t *data, return info->x_pkey->dec_pkey != nullptr ? parse_ok : parse_error; } +} // namespace + STACK_OF(X509_INFO) *PEM_X509_INFO_read_bio(BIO *bp, STACK_OF(X509_INFO) *sk, pem_password_cb *cb, void *u) { X509_INFO *info = nullptr; @@ -175,7 +183,7 @@ STACK_OF(X509_INFO) *PEM_X509_INFO_read_bio(BIO *bp, STACK_OF(X509_INFO) *sk, // not decrypt private keys. if (key_type != EVP_PKEY_NONE && strlen(header.get()) > 10) { if (data.size() > INT_MAX) { - // We need the data to fit in |info| which forces the size to + // We need the data to fit in `info` which forces the size to // fit in one int type. goto err; } @@ -196,7 +204,7 @@ STACK_OF(X509_INFO) *PEM_X509_INFO_read_bio(BIO *bp, STACK_OF(X509_INFO) *sk, } size_t size; data.Release(reinterpret_cast(&info->enc_data), &size); - // Safety: we checked that |size| <= |INT_MAX|. + // Safety: we checked that `size` <= `INT_MAX`. info->enc_len = static_cast(size); } else if (parse_function != nullptr) { EVP_CIPHER_INFO cipher; diff --git a/third_party/boringssl/src/crypto/pem/pem_lib.cc b/third_party/boringssl/src/crypto/pem/pem_lib.cc index 3fb851596..de3b287c6 100644 --- a/third_party/boringssl/src/crypto/pem/pem_lib.cc +++ b/third_party/boringssl/src/crypto/pem/pem_lib.cc @@ -42,32 +42,12 @@ using namespace bssl; -static int load_iv(const char **fromp, unsigned char *to, size_t num); -static int check_pem(const std::string_view nm, const std::string_view name); - -// PEM_proc_type appends a Proc-Type header to |buf|, determined by |type|. -static void PEM_proc_type(char buf[PEM_BUFSIZE], int type) { - const char *str; - - if (type == PEM_TYPE_ENCRYPTED) { - str = "ENCRYPTED"; - } else if (type == PEM_TYPE_MIC_CLEAR) { - str = "MIC-CLEAR"; - } else if (type == PEM_TYPE_MIC_ONLY) { - str = "MIC-ONLY"; - } else { - str = "BAD-TYPE"; - } - - OPENSSL_strlcat(buf, "Proc-Type: 4,", PEM_BUFSIZE); - OPENSSL_strlcat(buf, str, PEM_BUFSIZE); - OPENSSL_strlcat(buf, "\n", PEM_BUFSIZE); -} +namespace { -// PEM_dek_info appends a DEK-Info header to |buf|, with an algorithm of |type| -// and a single parameter, specified by hex-encoding |len| bytes from |str|. -static void PEM_dek_info(char buf[PEM_BUFSIZE], const char *type, size_t len, - char *str) { +// PEM_dek_info appends a DEK-Info header to `buf`, with an algorithm of `type` +// and a single parameter, specified by hex-encoding `len` bytes from `str`. +void PEM_dek_info(char buf[PEM_BUFSIZE], const char *type, size_t len, + char *str) { static const unsigned char map[17] = "0123456789ABCDEF"; OPENSSL_strlcat(buf, "DEK-Info: ", PEM_BUFSIZE); @@ -88,74 +68,49 @@ static void PEM_dek_info(char buf[PEM_BUFSIZE], const char *type, size_t len, buf[used + len * 2 + 1] = '\0'; } -void *PEM_ASN1_read(d2i_of_void *d2i, const char *name, FILE *fp, void **x, - pem_password_cb *cb, void *u) { - BIO *b = BIO_new_fp(fp, BIO_NOCLOSE); - if (b == nullptr) { - OPENSSL_PUT_ERROR(PEM, ERR_R_BUF_LIB); - return nullptr; +bool check_pem(std::string_view name, std::string_view expected) { + // `PEM_STRING_EVP_PKEY` is not a real PEM type, but a placeholder that + // matches one of several private key types. + if (expected == PEM_STRING_EVP_PKEY) { + return name == PEM_STRING_PKCS8 || name == PEM_STRING_PKCS8INF || + name == PEM_STRING_RSA || name == PEM_STRING_EC || + name == PEM_STRING_DSA; } - void *ret = PEM_ASN1_read_bio(d2i, name, b, x, cb, u); - BIO_free(b); - return ret; -} -static int check_pem(const std::string_view nm, const std::string_view name) { - // Normal matching nm and name - if (nm == name) { - return 1; + // Normal name matching. + if (name == expected) { + return true; } - // Make PEM_STRING_EVP_PKEY match any private key - - if (name == PEM_STRING_EVP_PKEY) { - return nm == PEM_STRING_PKCS8 || nm == PEM_STRING_PKCS8INF || - nm == PEM_STRING_RSA || nm == PEM_STRING_EC || nm == PEM_STRING_DSA; + // Permit older strings. + if (name == PEM_STRING_X509_OLD && expected == PEM_STRING_X509) { + return true; } - - // Permit older strings - - if (nm == PEM_STRING_X509_OLD && name == PEM_STRING_X509) { - return 1; + if (name == PEM_STRING_X509_REQ_OLD && expected == PEM_STRING_X509_REQ) { + return true; } - if (nm == PEM_STRING_X509_REQ_OLD && name == PEM_STRING_X509_REQ) { - return 1; + // Allow normal certs to be read as trusted certs. + if (name == PEM_STRING_X509 && expected == PEM_STRING_X509_TRUSTED) { + return true; } - - // Allow normal certs to be read as trusted certs - if (nm == PEM_STRING_X509 && name == PEM_STRING_X509_TRUSTED) { - return 1; + if (name == PEM_STRING_X509_OLD && expected == PEM_STRING_X509_TRUSTED) { + return true; } - if (nm == PEM_STRING_X509_OLD && name == PEM_STRING_X509_TRUSTED) { - return 1; + // Some CAs use PKCS#7 with CERTIFICATE headers. + if (name == PEM_STRING_X509 && expected == PEM_STRING_PKCS7) { + return true; } - - // Some CAs use PKCS#7 with CERTIFICATE headers - if (nm == PEM_STRING_X509 && name == PEM_STRING_PKCS7) { - return 1; - } - - if (nm == PEM_STRING_PKCS7_SIGNED && name == PEM_STRING_PKCS7) { - return 1; + if (name == PEM_STRING_PKCS7_SIGNED && expected == PEM_STRING_PKCS7) { + return true; } -#ifndef OPENSSL_NO_CMS - if (nm == PEM_STRING_X509 && name == PEM_STRING_CMS) { - return 1; - } - // Allow CMS to be read from PKCS#7 headers - if (nm == PEM_STRING_PKCS7 && name == PEM_STRING_CMS) { - return 1; - } -#endif - - return 0; + return false; } -static const EVP_CIPHER *cipher_by_name(const std::string_view name) { - // This is similar to the (deprecated) function |EVP_get_cipherbyname|. Note +const EVP_CIPHER *cipher_by_name(const std::string_view name) { + // This is similar to the (deprecated) function `EVP_get_cipherbyname`. Note // the PEM code assumes that ciphers have at least 8 bytes of IV, at most 20 // bytes of overhead and generally behave like CBC mode. if (name == SN_des_cbc) { @@ -173,8 +128,44 @@ static const EVP_CIPHER *cipher_by_name(const std::string_view name) { } } +int load_iv(const char **fromp, unsigned char *to, size_t num) { + uint8_t v; + const char *from; + + from = *fromp; + for (size_t i = 0; i < num; i++) { + to[i] = 0; + } + num *= 2; + for (size_t i = 0; i < num; i++) { + if (!OPENSSL_fromxdigit(&v, *from)) { + OPENSSL_PUT_ERROR(PEM, PEM_R_BAD_IV_CHARS); + return 0; + } + from++; + to[i / 2] |= v << (!(i & 1)) * 4; + } + + *fromp = from; + return 1; +} + +} // namespace + +void *PEM_ASN1_read(d2i_of_void *d2i, const char *name, FILE *fp, void **x, + pem_password_cb *cb, void *u) { + BIO *b = BIO_new_fp(fp, BIO_NOCLOSE); + if (b == nullptr) { + OPENSSL_PUT_ERROR(PEM, ERR_R_BUF_LIB); + return nullptr; + } + void *ret = PEM_ASN1_read_bio(d2i, name, b, x, cb, u); + BIO_free(b); + return ret; +} + int PEM_bytes_read_bio(unsigned char **pdata, long *plen, char **pnm, - const char *name, BIO *bp, pem_password_cb *cb, + const char *expected_name, BIO *bp, pem_password_cb *cb, void *u) { EVP_CIPHER_INFO cipher; UniquePtr nm; @@ -186,7 +177,7 @@ int PEM_bytes_read_bio(unsigned char **pdata, long *plen, char **pnm, for (;;) { if (!PEM_read_bio_inner(bp, &nm, &header, &data)) { if (ERR_equals(ERR_peek_error(), ERR_LIB_PEM, PEM_R_NO_START_LINE)) { - ERR_add_error_data(2, "Expecting: ", name); + ERR_add_error_data(2, "Expecting: ", expected_name); } return 0; } @@ -194,7 +185,7 @@ int PEM_bytes_read_bio(unsigned char **pdata, long *plen, char **pnm, OPENSSL_PUT_ERROR(PEM, ERR_R_OVERFLOW); return 0; } - if (check_pem(nm.get(), name)) { + if (check_pem(nm.get(), expected_name)) { break; } } @@ -207,9 +198,9 @@ int PEM_bytes_read_bio(unsigned char **pdata, long *plen, char **pnm, } // Release the buffer to the caller. - // Note that |PEM_do_header| may have reduced the length after decrypting + // Note that `PEM_do_header` may have reduced the length after decrypting // in-place. - // This will not overflow because |data.size()| was checked to fit in |long| + // This will not overflow because `data.size()` was checked to fit in `long` // above. data.Release(pdata, &unused); *plen = static_cast(ulen); @@ -221,7 +212,7 @@ int PEM_bytes_read_bio(unsigned char **pdata, long *plen, char **pnm, return 1; } -int PEM_ASN1_write(i2d_of_void *i2d, const char *name, FILE *fp, void *x, +int PEM_ASN1_write(i2d_of_void *i2d, const char *name, FILE *fp, const void *x, const EVP_CIPHER *enc, const unsigned char *pass, int pass_len, pem_password_cb *callback, void *u) { BIO *b = BIO_new_fp(fp, BIO_NOCLOSE); @@ -235,9 +226,10 @@ int PEM_ASN1_write(i2d_of_void *i2d, const char *name, FILE *fp, void *x, return ret; } -int PEM_ASN1_write_bio(i2d_of_void *i2d, const char *name, BIO *bp, void *x, - const EVP_CIPHER *enc, const unsigned char *pass, - int pass_len, pem_password_cb *callback, void *u) { +int PEM_ASN1_write_bio(i2d_of_void *i2d, const char *name, BIO *bp, + const void *x, const EVP_CIPHER *enc, + const unsigned char *pass, int pass_len, + pem_password_cb *callback, void *u) { ScopedEVP_CIPHER_CTX ctx; int dsize = 0, ret = 0; size_t i, j, data_size; @@ -278,7 +270,7 @@ int PEM_ASN1_write_bio(i2d_of_void *i2d, const char *name, BIO *bp, void *x, if (!callback) { callback = PEM_def_callback; } - pass_len = (*callback)(buf, PEM_BUFSIZE, 1, u); + pass_len = (*callback)(buf, PEM_BUFSIZE, /*enc=*/1, u); if (pass_len < 0) { OPENSSL_PUT_ERROR(PEM, PEM_R_READ_KEY); goto err; @@ -301,10 +293,8 @@ int PEM_ASN1_write_bio(i2d_of_void *i2d, const char *name, BIO *bp, void *x, assert(strlen(objstr) + 23 + 2 * iv_len + 13 <= sizeof(buf)); - buf[0] = '\0'; - PEM_proc_type(buf, PEM_TYPE_ENCRYPTED); + OPENSSL_strlcpy(buf, "Proc-Type: 4,ENCRYPTED\n", sizeof(buf)); PEM_dek_info(buf, objstr, iv_len, (char *)iv); - // k=strlen(buf); ret = 1; if (!EVP_EncryptInit_ex(ctx.get(), enc, nullptr, key, iv) || @@ -349,7 +339,7 @@ int bssl::PEM_do_header(const EVP_CIPHER_INFO *cipher, unsigned char *data, if (!callback) { callback = PEM_def_callback; } - pass_len = callback(buf, PEM_BUFSIZE, 0, u); + pass_len = callback(buf, PEM_BUFSIZE, /*enc=*/0, u); if (pass_len < 0) { OPENSSL_PUT_ERROR(PEM, PEM_R_BAD_PASSWORD_READ); return 0; @@ -360,7 +350,7 @@ int bssl::PEM_do_header(const EVP_CIPHER_INFO *cipher, unsigned char *data, return 0; } - // Safety: we have checked |*len| before narrowing so that |EVP_DecryptUpdate| + // Safety: we have checked `*len` before narrowing so that `EVP_DecryptUpdate` // can safely work with it. size_t out_len1 = 0; size_t out_len2 = 0; @@ -425,7 +415,7 @@ int bssl::PEM_get_EVP_CIPHER_INFO(const char *header, EVP_CIPHER_INFO *cipher) { return 0; } // The IV parameter must be at least 8 bytes long to be used as the salt in - // the KDF. (This should not happen given |cipher_by_name|.) + // the KDF. (This should not happen given `cipher_by_name`.) if (EVP_CIPHER_iv_length(cipher->cipher) < 8) { assert(0); OPENSSL_PUT_ERROR(PEM, PEM_R_UNSUPPORTED_ENCRYPTION); @@ -439,28 +429,6 @@ int bssl::PEM_get_EVP_CIPHER_INFO(const char *header, EVP_CIPHER_INFO *cipher) { return 1; } -static int load_iv(const char **fromp, unsigned char *to, size_t num) { - uint8_t v; - const char *from; - - from = *fromp; - for (size_t i = 0; i < num; i++) { - to[i] = 0; - } - num *= 2; - for (size_t i = 0; i < num; i++) { - if (!OPENSSL_fromxdigit(&v, *from)) { - OPENSSL_PUT_ERROR(PEM, PEM_R_BAD_IV_CHARS); - return 0; - } - from++; - to[i / 2] |= v << (!(i & 1)) * 4; - } - - *fromp = from; - return 1; -} - int PEM_write(FILE *fp, const char *name, const char *header, const unsigned char *data, long len) { BIO *b = BIO_new_fp(fp, BIO_NOCLOSE); @@ -678,7 +646,8 @@ int bssl::PEM_read_bio_inner(BIO *bp, UniquePtr *name, return 0; } int k; - status = EVP_DecodeFinal(&ctx, (unsigned char *)&(dataB->data[bl]), &k); + status = EVP_DecodeFinal(&ctx, + (unsigned char *)&(dataB->data[decoded_length]), &k); if (status < 0) { OPENSSL_PUT_ERROR(PEM, PEM_R_BAD_BASE64_DECODE); return 0; @@ -721,12 +690,12 @@ int PEM_read_bio(BIO *bp, char **name, char **header, unsigned char **data, *name = owned_name.release(); *header = owned_header.release(); owned_data.Release(data, &ulen); - // Safety: we checked that |ulen| <= |LONG_MAX|. + // Safety: we checked that `ulen` <= `LONG_MAX`. *len = static_cast(ulen); return 1; } -int PEM_def_callback(char *buf, int size, int rwflag, void *userdata) { +int PEM_def_callback(char *buf, int size, int enc, void *userdata) { if (!buf || !userdata || size < 0) { return -1; } diff --git a/third_party/boringssl/src/crypto/pem/pem_pk8.cc b/third_party/boringssl/src/crypto/pem/pem_pk8.cc index 9f477e9f4..69232f29f 100644 --- a/third_party/boringssl/src/crypto/pem/pem_pk8.cc +++ b/third_party/boringssl/src/crypto/pem/pem_pk8.cc @@ -22,45 +22,14 @@ #include #include -static int do_pk8pkey(BIO *bp, const EVP_PKEY *x, int isder, int nid, - const EVP_CIPHER *enc, const char *pass, int pass_len, - pem_password_cb *cb, void *u); -static int do_pk8pkey_fp(FILE *bp, const EVP_PKEY *x, int isder, int nid, - const EVP_CIPHER *enc, const char *pass, int pass_len, - pem_password_cb *cb, void *u); +#include "internal.h" -// These functions write a private key in PKCS#8 format: it is a "drop in" -// replacement for PEM_write_bio_PrivateKey() and friends. As usual if 'enc' -// is NULL then it uses the unencrypted private key form. The 'nid' versions -// uses PKCS#5 v1.5 PBE algorithms whereas the others use PKCS#5 v2.0. - -int PEM_write_bio_PKCS8PrivateKey_nid(BIO *bp, const EVP_PKEY *x, int nid, - const char *pass, int pass_len, - pem_password_cb *cb, void *u) { - return do_pk8pkey(bp, x, 0, nid, nullptr, pass, pass_len, cb, u); -} - -int PEM_write_bio_PKCS8PrivateKey(BIO *bp, const EVP_PKEY *x, - const EVP_CIPHER *enc, const char *pass, - int pass_len, pem_password_cb *cb, void *u) { - return do_pk8pkey(bp, x, 0, -1, enc, pass, pass_len, cb, u); -} - -int i2d_PKCS8PrivateKey_bio(BIO *bp, const EVP_PKEY *x, const EVP_CIPHER *enc, - const char *pass, int pass_len, pem_password_cb *cb, - void *u) { - return do_pk8pkey(bp, x, 1, -1, enc, pass, pass_len, cb, u); -} -int i2d_PKCS8PrivateKey_nid_bio(BIO *bp, const EVP_PKEY *x, int nid, - const char *pass, int pass_len, - pem_password_cb *cb, void *u) { - return do_pk8pkey(bp, x, 1, nid, nullptr, pass, pass_len, cb, u); -} +namespace { -static int do_pk8pkey(BIO *bp, const EVP_PKEY *x, int isder, int nid, - const EVP_CIPHER *enc, const char *pass, int pass_len, - pem_password_cb *cb, void *u) { +int do_pk8pkey(BIO *bp, const EVP_PKEY *x, int isder, int nid, + const EVP_CIPHER *enc, const char *pass, int pass_len, + pem_password_cb *cb, void *u) { X509_SIG *p8; PKCS8_PRIV_KEY_INFO *p8inf; char buf[PEM_BUFSIZE]; @@ -106,6 +75,51 @@ static int do_pk8pkey(BIO *bp, const EVP_PKEY *x, int isder, int nid, } } +int do_pk8pkey_fp(FILE *fp, const EVP_PKEY *x, int isder, int nid, + const EVP_CIPHER *enc, const char *pass, int pass_len, + pem_password_cb *cb, void *u) { + BIO *bp; + int ret; + if (!(bp = BIO_new_fp(fp, BIO_NOCLOSE))) { + OPENSSL_PUT_ERROR(PEM, ERR_R_BUF_LIB); + return 0; + } + ret = do_pk8pkey(bp, x, isder, nid, enc, pass, pass_len, cb, u); + BIO_free(bp); + return ret; +} + +} // namespace + +// These functions write a private key in PKCS#8 format: it is a "drop in" +// replacement for PEM_write_bio_PrivateKey() and friends. As usual if 'enc' +// is NULL then it uses the unencrypted private key form. The 'nid' versions +// uses PKCS#5 v1.5 PBE algorithms whereas the others use PKCS#5 v2.0. + +int PEM_write_bio_PKCS8PrivateKey_nid(BIO *bp, const EVP_PKEY *x, int nid, + const char *pass, int pass_len, + pem_password_cb *cb, void *u) { + return do_pk8pkey(bp, x, 0, nid, nullptr, pass, pass_len, cb, u); +} + +int PEM_write_bio_PKCS8PrivateKey(BIO *bp, const EVP_PKEY *x, + const EVP_CIPHER *enc, const char *pass, + int pass_len, pem_password_cb *cb, void *u) { + return do_pk8pkey(bp, x, 0, -1, enc, pass, pass_len, cb, u); +} + +int i2d_PKCS8PrivateKey_bio(BIO *bp, const EVP_PKEY *x, const EVP_CIPHER *enc, + const char *pass, int pass_len, pem_password_cb *cb, + void *u) { + return do_pk8pkey(bp, x, 1, -1, enc, pass, pass_len, cb, u); +} + +int i2d_PKCS8PrivateKey_nid_bio(BIO *bp, const EVP_PKEY *x, int nid, + const char *pass, int pass_len, + pem_password_cb *cb, void *u) { + return do_pk8pkey(bp, x, 1, nid, nullptr, pass, pass_len, cb, u); +} + EVP_PKEY *d2i_PKCS8PrivateKey_bio(BIO *bp, EVP_PKEY **x, pem_password_cb *cb, void *u) { PKCS8_PRIV_KEY_INFO *p8inf = nullptr; @@ -173,20 +187,6 @@ int PEM_write_PKCS8PrivateKey(FILE *fp, const EVP_PKEY *x, return do_pk8pkey_fp(fp, x, 0, -1, enc, pass, pass_len, cb, u); } -static int do_pk8pkey_fp(FILE *fp, const EVP_PKEY *x, int isder, int nid, - const EVP_CIPHER *enc, const char *pass, int pass_len, - pem_password_cb *cb, void *u) { - BIO *bp; - int ret; - if (!(bp = BIO_new_fp(fp, BIO_NOCLOSE))) { - OPENSSL_PUT_ERROR(PEM, ERR_R_BUF_LIB); - return 0; - } - ret = do_pk8pkey(bp, x, isder, nid, enc, pass, pass_len, cb, u); - BIO_free(bp); - return ret; -} - EVP_PKEY *d2i_PKCS8PrivateKey_fp(FILE *fp, EVP_PKEY **x, pem_password_cb *cb, void *u) { BIO *bp; diff --git a/third_party/boringssl/src/crypto/pem/pem_pkey.cc b/third_party/boringssl/src/crypto/pem/pem_pkey.cc index 433215e0f..836c4a9a3 100644 --- a/third_party/boringssl/src/crypto/pem/pem_pkey.cc +++ b/third_party/boringssl/src/crypto/pem/pem_pkey.cc @@ -67,7 +67,7 @@ EVP_PKEY *PEM_read_bio_PrivateKey(BIO *bp, EVP_PKEY **x, pem_password_cb *cb, if (!cb) { cb = PEM_def_callback; } - pass_len = cb(psbuf, PEM_BUFSIZE, 0, u); + pass_len = cb(psbuf, PEM_BUFSIZE, /*enc=*/0, u); if (pass_len < 0) { OPENSSL_PUT_ERROR(PEM, PEM_R_BAD_PASSWORD_READ); X509_SIG_free(p8); @@ -82,7 +82,7 @@ EVP_PKEY *PEM_read_bio_PrivateKey(BIO *bp, EVP_PKEY **x, pem_password_cb *cb, ret = EVP_PKCS82PKEY(p8inf); if (x) { if (*x) { - EVP_PKEY_free((EVP_PKEY *)*x); + EVP_PKEY_free(*x); } *x = ret; } @@ -96,7 +96,13 @@ EVP_PKEY *PEM_read_bio_PrivateKey(BIO *bp, EVP_PKEY **x, pem_password_cb *cb, ret = d2i_PrivateKey(EVP_PKEY_EC, x, &p, len); } else if (strcmp(nm, PEM_STRING_DSA) == 0) { ret = d2i_PrivateKey(EVP_PKEY_DSA, x, &p, len); + } else { + // `PEM_bytes_read_bio` should not have returned a PEM type this function + // does not recognized. + OPENSSL_PUT_ERROR(PEM, ERR_R_INTERNAL_ERROR); + goto err; } + p8err: if (ret == nullptr) { OPENSSL_PUT_ERROR(PEM, ERR_R_ASN1_LIB); @@ -108,7 +114,7 @@ EVP_PKEY *PEM_read_bio_PrivateKey(BIO *bp, EVP_PKEY **x, pem_password_cb *cb, return ret; } -int PEM_write_bio_PrivateKey(BIO *bp, EVP_PKEY *x, const EVP_CIPHER *enc, +int PEM_write_bio_PrivateKey(BIO *bp, const EVP_PKEY *x, const EVP_CIPHER *enc, const unsigned char *pass, int pass_len, pem_password_cb *cb, void *u) { return PEM_write_bio_PKCS8PrivateKey(bp, x, enc, (const char *)pass, pass_len, @@ -127,7 +133,7 @@ EVP_PKEY *PEM_read_PrivateKey(FILE *fp, EVP_PKEY **x, pem_password_cb *cb, return ret; } -int PEM_write_PrivateKey(FILE *fp, EVP_PKEY *x, const EVP_CIPHER *enc, +int PEM_write_PrivateKey(FILE *fp, const EVP_PKEY *x, const EVP_CIPHER *enc, const unsigned char *pass, int pass_len, pem_password_cb *cb, void *u) { BIO *b = BIO_new_fp(fp, BIO_NOCLOSE); diff --git a/third_party/boringssl/src/crypto/pem/pem_x509.cc b/third_party/boringssl/src/crypto/pem/pem_x509.cc index e33991446..9b38e98fa 100644 --- a/third_party/boringssl/src/crypto/pem/pem_x509.cc +++ b/third_party/boringssl/src/crypto/pem/pem_x509.cc @@ -19,4 +19,7 @@ #include #include +#include "internal.h" + + IMPLEMENT_PEM_rw(X509, X509, PEM_STRING_X509, X509) diff --git a/third_party/boringssl/src/crypto/pem/pem_xaux.cc b/third_party/boringssl/src/crypto/pem/pem_xaux.cc index 56a2b1e54..30f171623 100644 --- a/third_party/boringssl/src/crypto/pem/pem_xaux.cc +++ b/third_party/boringssl/src/crypto/pem/pem_xaux.cc @@ -19,4 +19,7 @@ #include #include +#include "internal.h" + + IMPLEMENT_PEM_rw(X509_AUX, X509, PEM_STRING_X509_TRUSTED, X509_AUX) diff --git a/third_party/boringssl/src/crypto/pkcs7/internal.h b/third_party/boringssl/src/crypto/pkcs7/internal.h index b85f2be7c..32c74c22a 100644 --- a/third_party/boringssl/src/crypto/pkcs7/internal.h +++ b/third_party/boringssl/src/crypto/pkcs7/internal.h @@ -21,16 +21,16 @@ BSSL_NAMESPACE_BEGIN // pkcs7_parse_header reads the non-certificate/non-CRL prefix of a PKCS#7 -// SignedData blob from |cbs| and sets |*out| to point to the rest of the -// input. If the input is in BER format, then |*der_bytes| will be set to a +// SignedData blob from `cbs` and sets `*out` to point to the rest of the +// input. If the input is in BER format, then `*der_bytes` will be set to a // pointer that needs to be freed by the caller once they have finished -// processing |*out| (which will be pointing into |*der_bytes|). +// processing `*out` (which will be pointing into `*der_bytes`). // -// It returns one on success or zero on error. On error, |*der_bytes| is +// It returns one on success or zero on error. On error, `*der_bytes` is // NULL. int pkcs7_parse_header(uint8_t **der_bytes, CBS *out, CBS *cbs); -// pkcs7_add_signed_data writes a PKCS#7, SignedData structure to |out|. While +// pkcs7_add_signed_data writes a PKCS#7, SignedData structure to `out`. While // doing so it makes callbacks to let the caller fill in parts of the structure. // All callbacks are ignored if NULL and return one on success or zero on error. // @@ -38,9 +38,9 @@ int pkcs7_parse_header(uint8_t **der_bytes, CBS *out, CBS *cbs); // it is always 1. In CMS, it depends on the features used. // digest_algos_cb: may write AlgorithmIdentifiers into the given CBB, which // is a SET of digest algorithms. -// cert_crl_cb: may write the |certificates| or |crls| fields. +// cert_crl_cb: may write the `certificates` or `crls` fields. // (See https://datatracker.ietf.org/doc/html/rfc2315#section-9.1) -// signer_infos_cb: may write the contents of the |signerInfos| field. +// signer_infos_cb: may write the contents of the `signerInfos` field. // (See https://datatracker.ietf.org/doc/html/rfc2315#section-9.1) // // pkcs7_add_signed_data returns one on success or zero on error. @@ -51,16 +51,17 @@ int pkcs7_add_signed_data(CBB *out, uint64_t signed_data_version, void *arg); // pkcs7_add_external_signature writes a PKCS#7 or CMS SignedData structure to -// |out|, containing an external (i.e. the contents are not included) signature, -// using |sign_cert| and |key| to sign the contents of |data| with |md|. If -// |use_key_id| is true (CMS-only), the SignerInfo specifies the signer with key +// `out`, containing an external (i.e. the contents are not included) signature, +// using `sign_cert` and `key` to sign the contents of `data` with `md`. If +// `use_key_id` is true (CMS-only), the SignerInfo specifies the signer with key // identifier. Otherwise, it uses issuer and serial number (PKCS#7 or CMS v1). // The SignedData will have no embedded certificates and no attributes. // // Note: CMS v1 and PKCS#7 v1.5 are not completely compatible, but they overlap // in all cases implemented by this function. -int pkcs7_add_external_signature(CBB *out, X509 *sign_cert, EVP_PKEY *key, - const EVP_MD *md, BIO *data, bool use_key_id); +int pkcs7_add_external_signature(CBB *out, const X509 *sign_cert, + const EVP_PKEY *key, const EVP_MD *md, + BIO *data, bool use_key_id); BSSL_NAMESPACE_END diff --git a/third_party/boringssl/src/crypto/pkcs7/pkcs7.cc b/third_party/boringssl/src/crypto/pkcs7/pkcs7.cc index 71e9ab840..9ca6a4711 100644 --- a/third_party/boringssl/src/crypto/pkcs7/pkcs7.cc +++ b/third_party/boringssl/src/crypto/pkcs7/pkcs7.cc @@ -26,6 +26,8 @@ using namespace bssl; +namespace { + // 1.2.840.113549.1.7.1 static const uint8_t kPKCS7Data[] = {0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x01}; @@ -34,13 +36,15 @@ static const uint8_t kPKCS7Data[] = {0x2a, 0x86, 0x48, 0x86, 0xf7, static const uint8_t kPKCS7SignedData[] = {0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x02}; +} // namespace + // pkcs7_parse_header reads the non-certificate/non-CRL prefix of a PKCS#7 -// SignedData blob from |cbs| and sets |*out| to point to the rest of the -// input. If the input is in BER format, then |*der_bytes| will be set to a +// SignedData blob from `cbs` and sets `*out` to point to the rest of the +// input. If the input is in BER format, then `*der_bytes` will be set to a // pointer that needs to be freed by the caller once they have finished -// processing |*out| (which will be pointing into |*der_bytes|). +// processing `*out` (which will be pointing into `*der_bytes`). // -// It returns one on success or zero on error. On error, |*der_bytes| is +// It returns one on success or zero on error. On error, `*der_bytes` is // NULL. int bssl::pkcs7_parse_header(uint8_t **der_bytes, CBS *out, CBS *cbs) { CBS in, content_info, content_type, wrapped_signed_data, signed_data; @@ -132,7 +136,9 @@ int PKCS7_get_raw_certificates(STACK_OF(CRYPTO_BUFFER) *out_certs, CBS *cbs, return ret; } -static int pkcs7_bundle_raw_certificates_cb(CBB *out, void *arg) { +namespace { + +int pkcs7_bundle_raw_certificates_cb(CBB *out, void *arg) { const STACK_OF(CRYPTO_BUFFER) *certs = reinterpret_cast(arg); CBB certificates; @@ -151,10 +157,12 @@ static int pkcs7_bundle_raw_certificates_cb(CBB *out, void *arg) { } } - // |certificates| is a implicitly-tagged SET OF. + // `certificates` is an implicitly-tagged SET OF. return CBB_flush_asn1_set_of(&certificates) && CBB_flush(out); } +} // namespace + int PKCS7_bundle_raw_certificates(CBB *out, const STACK_OF(CRYPTO_BUFFER) *certs) { return pkcs7_add_signed_data(out, /*signed_data_version=*/1, diff --git a/third_party/boringssl/src/crypto/pkcs7/pkcs7_x509.cc b/third_party/boringssl/src/crypto/pkcs7/pkcs7_x509.cc index 07975b2cf..4ca06ef10 100644 --- a/third_party/boringssl/src/crypto/pkcs7/pkcs7_x509.cc +++ b/third_party/boringssl/src/crypto/pkcs7/pkcs7_x509.cc @@ -177,7 +177,9 @@ int PKCS7_get_PEM_CRLs(STACK_OF(X509_CRL) *out_crls, BIO *pem_bio) { return ret; } -static int pkcs7_bundle_certificates_cb(CBB *out, void *arg) { +namespace { + +int pkcs7_bundle_certificates_cb(CBB *out, void *arg) { auto *certs = static_cast(arg); size_t i; CBB certificates; @@ -199,10 +201,12 @@ static int pkcs7_bundle_certificates_cb(CBB *out, void *arg) { } } - // |certificates| is a implicitly-tagged SET OF. + // `certificates` is an implicitly-tagged SET OF. return CBB_flush_asn1_set_of(&certificates) && CBB_flush(out); } +} // namespace + int PKCS7_bundle_certificates(CBB *out, const STACK_OF(X509) *certs) { return pkcs7_add_signed_data( out, /*signed_data_version=*/1, @@ -210,7 +214,9 @@ int PKCS7_bundle_certificates(CBB *out, const STACK_OF(X509) *certs) { /*signer_infos_cb=*/nullptr, const_cast(certs)); } -static int pkcs7_bundle_crls_cb(CBB *out, void *arg) { +namespace { + +int pkcs7_bundle_crls_cb(CBB *out, void *arg) { auto *crls = static_cast(arg); size_t i; CBB crl_data; @@ -232,10 +238,12 @@ static int pkcs7_bundle_crls_cb(CBB *out, void *arg) { } } - // |crl_data| is a implicitly-tagged SET OF. + // `crl_data` is an implicitly-tagged SET OF. return CBB_flush_asn1_set_of(&crl_data) && CBB_flush(out); } +} // namespace + int PKCS7_bundle_CRLs(CBB *out, const STACK_OF(X509_CRL) *crls) { return pkcs7_add_signed_data( out, /*signed_data_version=*/1, @@ -243,7 +251,9 @@ int PKCS7_bundle_CRLs(CBB *out, const STACK_OF(X509_CRL) *crls) { /*signer_infos_cb=*/nullptr, const_cast(crls)); } -static PKCS7 *pkcs7_new(CBS *cbs) { +namespace { + +PKCS7 *pkcs7_new(CBS *cbs) { CBS copy = *cbs, copy2 = *cbs; PKCS7 *ret = New(); if (ret == nullptr) { @@ -286,6 +296,8 @@ static PKCS7 *pkcs7_new(CBS *cbs) { return nullptr; } +} // namespace + PKCS7 *d2i_PKCS7(PKCS7 **out, const uint8_t **inp, size_t len) { CBS cbs; CBS_init(&cbs, *inp, len); @@ -372,7 +384,9 @@ int PKCS7_type_is_enveloped(const PKCS7 *p7) { return 0; } int PKCS7_type_is_signed(const PKCS7 *p7) { return 1; } int PKCS7_type_is_signedAndEnveloped(const PKCS7 *p7) { return 0; } -static bool digest_sign_update(EVP_MD_CTX *ctx, BIO *data) { +namespace { + +bool digest_sign_update(EVP_MD_CTX *ctx, BIO *data) { for (;;) { uint8_t buf[4096]; const int n = BIO_read(data, buf, sizeof(buf)); @@ -384,15 +398,13 @@ static bool digest_sign_update(EVP_MD_CTX *ctx, BIO *data) { } } -namespace { struct signer_info_data { - X509 *sign_cert = nullptr; + const X509 *sign_cert = nullptr; ScopedEVP_MD_CTX sign_ctx; bool use_key_id = false; }; -} // namespace -static int write_signer_digest_algos(CBB *digest_algos_set, void *arg) { +int write_signer_digest_algos(CBB *digest_algos_set, void *arg) { auto *si_data = static_cast(arg); // https://www.rfc-editor.org/rfc/rfc5754.html#section-2 // "Implementations MUST generate SHA2 AlgorithmIdentifiers with absent @@ -403,9 +415,9 @@ static int write_signer_digest_algos(CBB *digest_algos_set, void *arg) { // write_signer_info writes the SignerInfo structure from // https://www.rfc-editor.org/rfc/rfc2315.html#section-9.2 and -// https://www.rfc-editor.org/rfc/rfc5652.html#section-5.3 to |out|. It returns +// https://www.rfc-editor.org/rfc/rfc5652.html#section-5.3 to `out`. It returns // one on success or zero on error. -static int write_signer_info(CBB *out, void *arg) { +int write_signer_info(CBB *out, void *arg) { auto *si_data = static_cast(arg); uint64_t version = si_data->use_key_id ? 3u : 1u; @@ -472,9 +484,11 @@ static int write_signer_info(CBB *out, void *arg) { return 1; } -int bssl::pkcs7_add_external_signature(CBB *out, X509 *sign_cert, EVP_PKEY *key, - const EVP_MD *md, BIO *data, - bool use_key_id) { +} // namespace + +int bssl::pkcs7_add_external_signature(CBB *out, const X509 *sign_cert, + const EVP_PKEY *key, const EVP_MD *md, + BIO *data, bool use_key_id) { signer_info_data si_data; si_data.use_key_id = use_key_id; si_data.sign_cert = sign_cert; @@ -494,8 +508,8 @@ int bssl::pkcs7_add_external_signature(CBB *out, X509 *sign_cert, EVP_PKEY *key, /*cert_crl_cb=*/nullptr, write_signer_info, &si_data); } -PKCS7 *PKCS7_sign(X509 *sign_cert, EVP_PKEY *pkey, STACK_OF(X509) *certs, - BIO *data, int flags) { +PKCS7 *PKCS7_sign(const X509 *sign_cert, const EVP_PKEY *pkey, + const STACK_OF(X509) *certs, BIO *data, int flags) { ScopedCBB cbb; if (!CBB_init(cbb.get(), 2048)) { return nullptr; diff --git a/third_party/boringssl/src/crypto/pkcs8/internal.h b/third_party/boringssl/src/crypto/pkcs8/internal.h index 89cdf9cdd..f45c6b93c 100644 --- a/third_party/boringssl/src/crypto/pkcs8/internal.h +++ b/third_party/boringssl/src/crypto/pkcs8/internal.h @@ -28,9 +28,9 @@ struct pkcs8_priv_key_info_st { BSSL_NAMESPACE_BEGIN -// pkcs8_pbe_decrypt decrypts |in| using the PBE scheme described by -// |algorithm|, which should be a serialized AlgorithmIdentifier structure. On -// success, it sets |*out| to a newly-allocated buffer containing the decrypted +// pkcs8_pbe_decrypt decrypts `in` using the PBE scheme described by +// `algorithm`, which should be a serialized AlgorithmIdentifier structure. On +// success, it sets `*out` to a newly-allocated buffer containing the decrypted // result and returns one. Otherwise, it returns zero. int pkcs8_pbe_decrypt(uint8_t **out, size_t *out_len, CBS *algorithm, const char *pass, size_t pass_len, const uint8_t *in, @@ -41,17 +41,17 @@ int pkcs8_pbe_decrypt(uint8_t **out, size_t *out_len, CBS *algorithm, #define PKCS12_MAC_ID 3 // pkcs12_key_gen runs the PKCS#12 key derivation function as specified in -// RFC 7292, appendix B. On success, it writes the resulting |out_len| bytes of -// key material to |out| and returns one. Otherwise, it returns zero. |id| -// should be one of the |PKCS12_*_ID| values. +// RFC 7292, appendix B. On success, it writes the resulting `out_len` bytes of +// key material to `out` and returns one. Otherwise, it returns zero. `id` +// should be one of the `PKCS12_*_ID` values. int pkcs12_key_gen(const char *pass, size_t pass_len, const uint8_t *salt, size_t salt_len, uint8_t id, uint32_t iterations, size_t out_len, uint8_t *out, const EVP_MD *md); -// pkcs12_pbe_encrypt_init configures |ctx| for encrypting with a PBES1 scheme +// pkcs12_pbe_encrypt_init configures `ctx` for encrypting with a PBES1 scheme // defined in PKCS#12, or a PBES2 scheme defined in PKCS#5. The algorithm is -// determined as in |PKCS8_encrypt|. It writes the corresponding -// AlgorithmIdentifier to |out|. +// determined as in `PKCS8_encrypt`. It writes the corresponding +// AlgorithmIdentifier to `out`. int pkcs12_pbe_encrypt_init(CBB *out, EVP_CIPHER_CTX *ctx, int alg_nid, const EVP_CIPHER *alg_cipher, uint32_t iterations, const char *pass, size_t pass_len, @@ -63,8 +63,8 @@ struct pbe_suite { uint8_t oid_len; const EVP_CIPHER *(*cipher_func)(); const EVP_MD *(*md_func)(); - // decrypt_init initialize |ctx| for decrypting. The password is specified by - // |pass| and |pass_len|. |param| contains the serialized parameters field of + // decrypt_init initialize `ctx` for decrypting. The password is specified by + // `pass` and `pass_len`. `param` contains the serialized parameters field of // the AlgorithmIdentifier. // // It returns one on success and zero on error. @@ -74,22 +74,22 @@ struct pbe_suite { #define PKCS5_SALT_LEN 16 -// pkcs5_pbe2_nid_to_cipher returns the |EVP_CIPHER| for |nid| if |nid| is +// pkcs5_pbe2_nid_to_cipher returns the `EVP_CIPHER` for `nid` if `nid` is // supported with PKCS#5 PBES2, and nullptr otherwise. const EVP_CIPHER *pkcs5_pbe2_nid_to_cipher(int nid); int PKCS5_pbe2_decrypt_init(const struct pbe_suite *suite, EVP_CIPHER_CTX *ctx, const char *pass, size_t pass_len, CBS *param); -// PKCS5_pbe2_encrypt_init configures |ctx| for encrypting with PKCS #5 PBES2, +// PKCS5_pbe2_encrypt_init configures `ctx` for encrypting with PKCS #5 PBES2, // as defined in RFC 2998, with the specified parameters. It writes the -// corresponding AlgorithmIdentifier to |out|. +// corresponding AlgorithmIdentifier to `out`. int PKCS5_pbe2_encrypt_init(CBB *out, EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, uint32_t iterations, const char *pass, size_t pass_len, const uint8_t *salt, size_t salt_len); -// pkcs12_iterations_acceptable returns one if |iterations| is a reasonable +// pkcs12_iterations_acceptable returns one if `iterations` is a reasonable // number of PBKDF2 iterations and zero otherwise. int pkcs12_iterations_acceptable(uint64_t iterations); diff --git a/third_party/boringssl/src/crypto/pkcs8/p5_pbev2.cc b/third_party/boringssl/src/crypto/pkcs8/p5_pbev2.cc index e1307bd90..6ecdd7983 100644 --- a/third_party/boringssl/src/crypto/pkcs8/p5_pbev2.cc +++ b/third_party/boringssl/src/crypto/pkcs8/p5_pbev2.cc @@ -25,12 +25,13 @@ #include #include -#include "../internal.h" #include "internal.h" using namespace bssl; +namespace { + // 1.2.840.113549.1.5.12 static const uint8_t kPBKDF2[] = {0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x05, 0x0c}; @@ -80,7 +81,7 @@ static const struct { &EVP_aes_256_cbc}, }; -static const EVP_CIPHER *cbs_to_cipher(const CBS *cbs) { +const EVP_CIPHER *cbs_to_cipher(const CBS *cbs) { for (const auto &cipher : kCipherOIDs) { if (CBS_mem_equal(cbs, cipher.oid, cipher.oid_len)) { return cipher.cipher_func(); @@ -90,7 +91,7 @@ static const EVP_CIPHER *cbs_to_cipher(const CBS *cbs) { return nullptr; } -static int add_cipher_oid(CBB *out, int nid) { +int add_cipher_oid(CBB *out, int nid) { for (const auto &cipher : kCipherOIDs) { if (cipher.nid == nid) { return CBB_add_asn1_element(out, CBS_ASN1_OBJECT, cipher.oid, @@ -102,20 +103,11 @@ static int add_cipher_oid(CBB *out, int nid) { return 0; } -const EVP_CIPHER *bssl::pkcs5_pbe2_nid_to_cipher(int nid) { - for (const auto &cipher : kCipherOIDs) { - if (cipher.nid == nid) { - return cipher.cipher_func(); - } - } - return nullptr; -} - -static int pkcs5_pbe2_cipher_init(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, - const EVP_MD *pbkdf2_md, uint32_t iterations, - const char *pass, size_t pass_len, - const uint8_t *salt, size_t salt_len, - const uint8_t *iv, size_t iv_len, int enc) { +int pkcs5_pbe2_cipher_init(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, + const EVP_MD *pbkdf2_md, uint32_t iterations, + const char *pass, size_t pass_len, + const uint8_t *salt, size_t salt_len, + const uint8_t *iv, size_t iv_len, int enc) { if (iv_len != EVP_CIPHER_iv_length(cipher)) { OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_ERROR_SETTING_CIPHER_PARAMS); return 0; @@ -129,6 +121,17 @@ static int pkcs5_pbe2_cipher_init(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, return ret; } +} // namespace + +const EVP_CIPHER *bssl::pkcs5_pbe2_nid_to_cipher(int nid) { + for (const auto &cipher : kCipherOIDs) { + if (cipher.nid == nid) { + return cipher.cipher_func(); + } + } + return nullptr; +} + int bssl::PKCS5_pbe2_encrypt_init(CBB *out, EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, uint32_t iterations, const char *pass, size_t pass_len, @@ -261,8 +264,8 @@ int bssl::PKCS5_pbe2_decrypt_init(const struct pbe_suite *suite, // All supported PRFs use a NULL parameter. CBS null; - if (!CBS_get_asn1(&alg_id, &null, CBS_ASN1_NULL) || - CBS_len(&null) != 0 || + if (!CBS_get_asn1(&alg_id, &null, CBS_ASN1_NULL) || // + CBS_len(&null) != 0 || // CBS_len(&alg_id) != 0) { OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_DECODE_ERROR); return 0; diff --git a/third_party/boringssl/src/crypto/pkcs8/pkcs8.cc b/third_party/boringssl/src/crypto/pkcs8/pkcs8.cc index e7f388aa5..21cf4563b 100644 --- a/third_party/boringssl/src/crypto/pkcs8/pkcs8.cc +++ b/third_party/boringssl/src/crypto/pkcs8/pkcs8.cc @@ -26,15 +26,16 @@ #include #include -#include "../bytestring/internal.h" #include "../internal.h" #include "internal.h" using namespace bssl; -static int pkcs12_encode_password(const char *in, size_t in_len, uint8_t **out, - size_t *out_len) { +namespace { + +int pkcs12_encode_password(const char *in, size_t in_len, uint8_t **out, + size_t *out_len) { ScopedCBB cbb; if (!CBB_init(cbb.get(), in_len * 2)) { return 0; @@ -60,6 +61,8 @@ static int pkcs12_encode_password(const char *in, size_t in_len, uint8_t **out, return 1; } +} // namespace + int bssl::pkcs12_key_gen(const char *pass, size_t pass_len, const uint8_t *salt, size_t salt_len, uint8_t id, uint32_t iterations, size_t out_len, uint8_t *out, const EVP_MD *md) { @@ -78,14 +81,14 @@ int bssl::pkcs12_key_gen(const char *pass, size_t pass_len, const uint8_t *salt, size_t pass_raw_len = 0, I_len = 0; { - // If |pass| is NULL, we use the empty string rather than {0, 0} as the raw + // If `pass` is NULL, we use the empty string rather than {0, 0} as the raw // password. if (pass != nullptr && !pkcs12_encode_password(pass, pass_len, &pass_raw, &pass_raw_len)) { goto err; } - // In the spec, |block_size| is called "v", but measured in bits. + // In the spec, `block_size` is called "v", but measured in bits. size_t block_size = EVP_MD_block_size(md); // 1. Construct a string, D (the "diversifier"), by concatenating v/8 copies @@ -186,11 +189,12 @@ int bssl::pkcs12_key_gen(const char *pass, size_t pass_len, const uint8_t *salt, return ret; } -static int pkcs12_pbe_cipher_init(const struct pbe_suite *suite, - EVP_CIPHER_CTX *ctx, uint32_t iterations, - const char *pass, size_t pass_len, - const uint8_t *salt, size_t salt_len, - int is_encrypt) { +namespace { + +int pkcs12_pbe_cipher_init(const struct pbe_suite *suite, EVP_CIPHER_CTX *ctx, + uint32_t iterations, const char *pass, + size_t pass_len, const uint8_t *salt, + size_t salt_len, int is_encrypt) { const EVP_CIPHER *cipher = suite->cipher_func(); const EVP_MD *md = suite->md_func(); @@ -210,9 +214,8 @@ static int pkcs12_pbe_cipher_init(const struct pbe_suite *suite, return ret; } -static int pkcs12_pbe_decrypt_init(const struct pbe_suite *suite, - EVP_CIPHER_CTX *ctx, const char *pass, - size_t pass_len, CBS *param) { +int pkcs12_pbe_decrypt_init(const struct pbe_suite *suite, EVP_CIPHER_CTX *ctx, + const char *pass, size_t pass_len, CBS *param) { CBS pbe_param, salt; uint64_t iterations; if (!CBS_get_asn1(param, &pbe_param, CBS_ASN1_SEQUENCE) || @@ -233,7 +236,7 @@ static int pkcs12_pbe_decrypt_init(const struct pbe_suite *suite, 0 /* decrypt */); } -static const struct bssl::pbe_suite kBuiltinPBE[] = { +static const struct pbe_suite kBuiltinPBE[] = { { NID_pbe_WithSHA1And40BitRC2_CBC, // 1.2.840.113549.1.12.1.6 @@ -272,10 +275,10 @@ static const struct bssl::pbe_suite kBuiltinPBE[] = { }, }; -static const struct bssl::pbe_suite *get_pkcs12_pbe_suite(int pbe_nid) { +const struct pbe_suite *get_pkcs12_pbe_suite(int pbe_nid) { for (const auto &pbe : kBuiltinPBE) { if (pbe.pbe_nid == pbe_nid && - // If |cipher_func| or |md_func| are missing, this is a PBES2 scheme. + // If `cipher_func` or `md_func` are missing, this is a PBES2 scheme. pbe.cipher_func != nullptr && pbe.md_func != nullptr) { return &pbe; } @@ -284,13 +287,15 @@ static const struct bssl::pbe_suite *get_pkcs12_pbe_suite(int pbe_nid) { return nullptr; } +} // namespace + int bssl::pkcs12_pbe_encrypt_init(CBB *out, EVP_CIPHER_CTX *ctx, int alg_nid, const EVP_CIPHER *alg_cipher, uint32_t iterations, const char *pass, size_t pass_len, const uint8_t *salt, size_t salt_len) { - // TODO(davidben): OpenSSL has since extended |pbe_nid| to control either - // the PBES1 scheme or the PBES2 PRF. E.g. passing |NID_hmacWithSHA256| will + // TODO(davidben): OpenSSL has since extended `pbe_nid` to control either + // the PBES1 scheme or the PBES2 PRF. E.g. passing `NID_hmacWithSHA256` will // select PBES2 with HMAC-SHA256 as the PRF. Implement this if anything uses // it. See 5693a30813a031d3921a016a870420e7eb93ec90 in OpenSSL. if (alg_nid == -1) { diff --git a/third_party/boringssl/src/crypto/pkcs8/pkcs8_x509.cc b/third_party/boringssl/src/crypto/pkcs8/pkcs8_x509.cc index 3f18013d9..81941e64a 100644 --- a/third_party/boringssl/src/crypto/pkcs8/pkcs8_x509.cc +++ b/third_party/boringssl/src/crypto/pkcs8/pkcs8_x509.cc @@ -194,6 +194,8 @@ X509_SIG *PKCS8_encrypt(int pbe_nid, const EVP_CIPHER *cipher, const char *pass, return ret; } +namespace { + struct pkcs12_context { EVP_PKEY **out_key; STACK_OF(X509) *out_certs; @@ -203,14 +205,14 @@ struct pkcs12_context { // PKCS12_handle_sequence parses a BER-encoded SEQUENCE of elements in a PKCS#12 // structure. -static int PKCS12_handle_sequence( - CBS *sequence, struct pkcs12_context *ctx, - int (*handle_element)(CBS *cbs, struct pkcs12_context *ctx)) { +int PKCS12_handle_sequence(CBS *sequence, struct pkcs12_context *ctx, + int (*handle_element)(CBS *cbs, + struct pkcs12_context *ctx)) { uint8_t *storage = nullptr; CBS in; int ret = 0; - // Although a BER->DER conversion is done at the beginning of |PKCS12_parse|, + // Although a BER->DER conversion is done at the beginning of `PKCS12_parse`, // the ASN.1 data gets wrapped in OCTETSTRINGs and/or encrypted and the // conversion cannot see through those wrappings. So each time we step // through one we need to convert to DER again. @@ -269,11 +271,11 @@ static const uint8_t kX509Certificate[] = {0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x16, 0x01}; // parse_bag_attributes parses the bagAttributes field of a SafeBag structure. -// It sets |*out_friendly_name| to a newly-allocated copy of the friendly name, +// It sets `*out_friendly_name` to a newly-allocated copy of the friendly name, // encoded as a UTF-8 string, or NULL if there is none. It returns one on // success and zero on error. -static int parse_bag_attributes(CBS *attrs, uint8_t **out_friendly_name, - size_t *out_friendly_name_len) { +int parse_bag_attributes(CBS *attrs, uint8_t **out_friendly_name, + size_t *out_friendly_name_len) { *out_friendly_name = nullptr; *out_friendly_name_len = 0; @@ -326,7 +328,7 @@ static int parse_bag_attributes(CBS *attrs, uint8_t **out_friendly_name, // PKCS12_handle_safe_bag parses a single SafeBag element in a PKCS#12 // structure. -static int PKCS12_handle_safe_bag(CBS *safe_bag, struct pkcs12_context *ctx) { +int PKCS12_handle_safe_bag(CBS *safe_bag, struct pkcs12_context *ctx) { CBS bag_id, wrapped_value, bag_attrs; if (!CBS_get_asn1(safe_bag, &bag_id, CBS_ASN1_OBJECT) || !CBS_get_asn1(safe_bag, &wrapped_value, @@ -437,8 +439,7 @@ static const uint8_t kPKCS7EncryptedData[] = {0x2a, 0x86, 0x48, 0x86, 0xf7, // PKCS12_handle_content_info parses a single PKCS#7 ContentInfo element in a // PKCS#12 structure. -static int PKCS12_handle_content_info(CBS *content_info, - struct pkcs12_context *ctx) { +int PKCS12_handle_content_info(CBS *content_info, struct pkcs12_context *ctx) { CBS content_type, wrapped_contents, contents; int ret = 0; uint8_t *storage = nullptr; @@ -514,10 +515,9 @@ static int PKCS12_handle_content_info(CBS *content_info, return ret; } -static int pkcs12_check_mac(int *out_mac_ok, const char *password, - size_t password_len, const CBS *salt, - uint32_t iterations, const EVP_MD *md, - const CBS *authsafes, const CBS *expected_mac) { +int pkcs12_check_mac(int *out_mac_ok, const char *password, size_t password_len, + const CBS *salt, uint32_t iterations, const EVP_MD *md, + const CBS *authsafes, const CBS *expected_mac) { int ret = 0; uint8_t hmac_key[EVP_MAX_MD_SIZE]; if (!pkcs12_key_gen(password, password_len, CBS_data(salt), CBS_len(salt), @@ -544,6 +544,8 @@ static int pkcs12_check_mac(int *out_mac_ok, const char *password, return ret; } +} // namespace + int PKCS12_get_key_and_certs(EVP_PKEY **out_key, STACK_OF(X509) *out_certs, CBS *ber_in, const char *password) { @@ -853,7 +855,7 @@ int PKCS12_parse(const PKCS12 *p12, const char *password, EVP_PKEY **out_pkey, } // OpenSSL selects the last certificate which matches the private key as - // |out_cert|. + // `out_cert`. *out_cert = nullptr; size_t num_certs = sk_X509_num(ca_certs); if (*out_pkey != nullptr && num_certs > 0) { @@ -902,10 +904,12 @@ int PKCS12_verify_mac(const PKCS12 *p12, const char *password, return 1; } +namespace { + // add_bag_attributes adds the bagAttributes field of a SafeBag structure, // containing the specified friendlyName and localKeyId attributes. -static int add_bag_attributes(CBB *bag, const char *name, size_t name_len, - const uint8_t *key_id, size_t key_id_len) { +int add_bag_attributes(CBB *bag, const char *name, size_t name_len, + const uint8_t *key_id, size_t key_id_len) { if (name == nullptr && key_id_len == 0) { return 1; // Omit the OPTIONAL SET. } @@ -947,8 +951,8 @@ static int add_bag_attributes(CBB *bag, const char *name, size_t name_len, return CBB_flush_asn1_set_of(&attrs) && CBB_flush(bag); } -static int add_cert_bag(CBB *cbb, X509 *cert, const char *name, - const uint8_t *key_id, size_t key_id_len) { +int add_cert_bag(CBB *cbb, const X509 *cert, const char *name, + const uint8_t *key_id, size_t key_id_len) { CBB bag, bag_contents, cert_bag, wrapped_cert, cert_value; if ( // See https://tools.ietf.org/html/rfc7292#section-4.2. !CBB_add_asn1(cbb, &bag, CBS_ASN1_SEQUENCE) || @@ -990,9 +994,9 @@ static int add_cert_bag(CBB *cbb, X509 *cert, const char *name, return 1; } -static int add_cert_safe_contents(CBB *cbb, X509 *cert, - const STACK_OF(X509) *chain, const char *name, - const uint8_t *key_id, size_t key_id_len) { +int add_cert_safe_contents(CBB *cbb, const X509 *cert, + const STACK_OF(X509) *chain, const char *name, + const uint8_t *key_id, size_t key_id_len) { CBB safe_contents; if (!CBB_add_asn1(cbb, &safe_contents, CBS_ASN1_SEQUENCE) || (cert != nullptr && @@ -1011,14 +1015,12 @@ static int add_cert_safe_contents(CBB *cbb, X509 *cert, return CBB_flush(cbb); } -// add_encrypted_data encrypts |in| with |pbe_nid| and |pbe_cipher|, writing the -// result to |out|. It returns one on success and zero on error. |pbe_nid| and -// |pbe_cipher| are interpreted as in |PKCS8_encrypt|. -static int add_encrypted_data(CBB *out, int pbe_nid, - const EVP_CIPHER *pbe_cipher, - const char *password, size_t password_len, - uint32_t iterations, const uint8_t *in, - size_t in_len) { +// add_encrypted_data encrypts `in` with `pbe_nid` and `pbe_cipher`, writing the +// result to `out`. It returns one on success and zero on error. `pbe_nid` and +// `pbe_cipher` are interpreted as in `PKCS8_encrypt`. +int add_encrypted_data(CBB *out, int pbe_nid, const EVP_CIPHER *pbe_cipher, + const char *password, size_t password_len, + uint32_t iterations, const uint8_t *in, size_t in_len) { uint8_t salt[PKCS5_SALT_LEN]; if (!RAND_bytes(salt, sizeof(salt))) { return 0; @@ -1070,8 +1072,10 @@ static int add_encrypted_data(CBB *out, int pbe_nid, return 1; } +} // namespace + PKCS12 *PKCS12_create(const char *password, const char *name, - const EVP_PKEY *pkey, X509 *cert, + const EVP_PKEY *pkey, const X509 *cert, const STACK_OF(X509) *chain, int key_nid, int cert_nid, int iterations, int mac_iterations, int key_type) { if (key_nid == 0) { @@ -1103,7 +1107,7 @@ PKCS12 *PKCS12_create(const char *password, const char *name, // recursive data format. Section 5.1 of RFC 7292 describes the encoding // algorithm, but there is no clear overview. A quick summary: // - // PKCS#7 defines a ContentInfo structure, which is a overgeneralized typed + // PKCS#7 defines a ContentInfo structure, which is an overgeneralized typed // combinator structure for applying cryptography. We care about two types. A // data ContentInfo contains an OCTET STRING and is a leaf node of the // combinator tree. An encrypted-data ContentInfo contains encryption @@ -1129,7 +1133,7 @@ PKCS12 *PKCS12_create(const char *password, const char *name, // key-specific encryption container, PKCS8ShroudedKeyBag, which is used // instead. - // Note that |password| may be NULL to specify no password, rather than the + // Note that `password` may be NULL to specify no password, rather than the // empty string. They are encoded differently in PKCS#12. (One is the empty // byte array and the other is NUL-terminated UCS-2.) size_t password_len = password != nullptr ? strlen(password) : 0; @@ -1160,7 +1164,7 @@ PKCS12 *PKCS12_create(const char *password, const char *name, CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 0) || !CBB_add_asn1(&auth_safe_wrapper, &auth_safe_data, CBS_ASN1_OCTETSTRING) || - // See https://tools.ietf.org/html/rfc7292#section-4.1. |auth_safe|'s + // See https://tools.ietf.org/html/rfc7292#section-4.1. `auth_safe`'s // contains a SEQUENCE of ContentInfos. !CBB_add_asn1(&auth_safe_data, &content_infos, CBS_ASN1_SEQUENCE)) { goto err; @@ -1267,7 +1271,7 @@ PKCS12 *PKCS12_create(const char *password, const char *name, { // Compute the MAC. Match OpenSSL in using SHA-1 as the hash function. The - // MAC covers |auth_safe_data|. + // MAC covers `auth_safe_data`. const EVP_MD *mac_md = EVP_sha1(); uint8_t mac_salt[PKCS5_SALT_LEN]; uint8_t mac[EVP_MAX_MD_SIZE]; diff --git a/third_party/boringssl/src/crypto/poly1305/internal.h b/third_party/boringssl/src/crypto/poly1305/internal.h index 39094906b..c920e5c6a 100644 --- a/third_party/boringssl/src/crypto/poly1305/internal.h +++ b/third_party/boringssl/src/crypto/poly1305/internal.h @@ -24,6 +24,23 @@ BSSL_NAMESPACE_BEGIN #if defined(OPENSSL_ARM) && !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_APPLE) #define OPENSSL_POLY1305_NEON +struct alignas(16) fe1305x2 { + uint32_t v[12]; // for alignment; only using 10 +}; + +extern "C" { +// openssl_poly1305_neon2_addmulmod computes (x * y) + c and writes the result +// to `r`. +void openssl_poly1305_neon2_addmulmod(fe1305x2 *r, const fe1305x2 *x, + const fe1305x2 *y, const fe1305x2 *c); + +// openssl_poly1305_neon2_blocks processes `inlen` bytes from `in` and updates +// the hash state in `h` using the precomputed powers in `precomp`. It returns +// the number of bytes that were not processed (i.e. the remainder < 32 bytes). +int openssl_poly1305_neon2_blocks(fe1305x2 *h, const fe1305x2 precomp[2], + const uint8_t *in, size_t inlen); +} + void CRYPTO_poly1305_init_neon(poly1305_state *state, const uint8_t key[32]); void CRYPTO_poly1305_update_neon(poly1305_state *state, const uint8_t *in, diff --git a/third_party/boringssl/src/crypto/poly1305/poly1305.cc b/third_party/boringssl/src/crypto/poly1305/poly1305.cc index 2d71558fe..869722d0c 100644 --- a/third_party/boringssl/src/crypto/poly1305/poly1305.cc +++ b/third_party/boringssl/src/crypto/poly1305/poly1305.cc @@ -19,17 +19,19 @@ #include #include -#include +#include // IWYU pragma: keep #include "../internal.h" -#include "internal.h" +#include "internal.h" // IWYU pragma: keep using namespace bssl; #if !defined(BORINGSSL_HAS_UINT128) || !defined(OPENSSL_X86_64) -static uint64_t mul32x32_64(uint32_t a, uint32_t b) { return (uint64_t)a * b; } +namespace { + +uint64_t mul32x32_64(uint32_t a, uint32_t b) { return (uint64_t)a * b; } struct poly1305_state_st { uint32_t r0, r1, r2, r3, r4; @@ -44,15 +46,15 @@ static_assert( sizeof(struct poly1305_state_st) + 63 <= sizeof(poly1305_state), "poly1305_state isn't large enough to hold aligned poly1305_state_st"); -static struct poly1305_state_st *poly1305_aligned_state(poly1305_state *state) { +struct poly1305_state_st *poly1305_aligned_state(poly1305_state *state) { return reinterpret_cast(align_pointer(state, 64)); } -// poly1305_blocks updates |state| given some amount of input data. This -// function may only be called with a |len| that is not a multiple of 16 at the +// poly1305_blocks updates `state` given some amount of input data. This +// function may only be called with a `len` that is not a multiple of 16 at the // end of the data. Otherwise the input must be buffered into 16 byte blocks. -static void poly1305_update(struct poly1305_state_st *state, const uint8_t *in, - size_t len) { +void poly1305_update(struct poly1305_state_st *state, const uint8_t *in, + size_t len) { uint32_t t0, t1, t2, t3; uint64_t t[5]; uint32_t b; @@ -145,6 +147,8 @@ static void poly1305_update(struct poly1305_state_st *state, const uint8_t *in, goto poly1305_donna_mul; } +} // namespace + void CRYPTO_poly1305_init(poly1305_state *statep, const uint8_t key[32]) { struct poly1305_state_st *state = poly1305_aligned_state(statep); uint32_t t0, t1, t2, t3; diff --git a/third_party/boringssl/src/crypto/poly1305/poly1305_arm.cc b/third_party/boringssl/src/crypto/poly1305/poly1305_arm.cc index 6620d70d3..1221eb215 100644 --- a/third_party/boringssl/src/crypto/poly1305/poly1305_arm.cc +++ b/third_party/boringssl/src/crypto/poly1305/poly1305_arm.cc @@ -18,32 +18,24 @@ #include #include -#include +#include // IWYU pragma: keep +// IWYU pragma: begin_keep #include "../internal.h" #include "internal.h" +// IWYU pragma: end_keep BSSL_NAMESPACE_BEGIN #if defined(OPENSSL_POLY1305_NEON) -typedef struct { - uint32_t v[12]; // for alignment; only using 10 -} fe1305x2; - #define addmulmod openssl_poly1305_neon2_addmulmod #define blocks openssl_poly1305_neon2_blocks -extern "C" { -extern void addmulmod(fe1305x2 *r, const fe1305x2 *x, const fe1305x2 *y, - const fe1305x2 *c); - -extern int blocks(fe1305x2 *h, const fe1305x2 precomp[2], const uint8_t *in, - size_t inlen); -} +namespace { -static void freeze(fe1305x2 *r) { +void freeze(fe1305x2 *r) { int i; uint32_t x0 = r->v[0]; @@ -108,7 +100,7 @@ static void freeze(fe1305x2 *r) { r->v[8] = y4; } -static void fe1305x2_tobytearray(uint8_t r[16], fe1305x2 *x) { +void fe1305x2_tobytearray(uint8_t r[16], fe1305x2 *x) { uint32_t x0 = x->v[0]; uint32_t x1 = x->v[2]; uint32_t x2 = x->v[4]; @@ -130,7 +122,7 @@ static void fe1305x2_tobytearray(uint8_t r[16], fe1305x2 *x) { CRYPTO_store_u32_le(r + 12, (x3 >> 18) + (x4 << 8)); } -static void fe1305x2_frombytearray(fe1305x2 *r, const uint8_t *x, size_t xlen) { +void fe1305x2_frombytearray(fe1305x2 *r, const uint8_t *x, size_t xlen) { size_t i; uint8_t t[17]; @@ -169,7 +161,7 @@ static void fe1305x2_frombytearray(fe1305x2 *r, const uint8_t *x, size_t xlen) { } } -static const fe1305x2 zero alignas(16) = {0}; +static const fe1305x2 zero = {0}; struct poly1305_state_st { fe1305x2 r, h, c, precomp[2]; @@ -182,10 +174,12 @@ static_assert( sizeof(struct poly1305_state_st) + 63 <= sizeof(poly1305_state), "poly1305_state isn't large enough to hold aligned poly1305_state_st."); -static poly1305_state_st *poly1305_aligned_state(poly1305_state *state) { +poly1305_state_st *poly1305_aligned_state(poly1305_state *state) { return reinterpret_cast(align_pointer(state, 64)); } +} // namespace + void CRYPTO_poly1305_init_neon(poly1305_state *state, const uint8_t key[32]) { poly1305_state_st *st = poly1305_aligned_state(state); fe1305x2 *const r = &st->r; diff --git a/third_party/boringssl/src/crypto/poly1305/poly1305_vec.cc b/third_party/boringssl/src/crypto/poly1305/poly1305_vec.cc index 20f77f2d6..c0a585246 100644 --- a/third_party/boringssl/src/crypto/poly1305/poly1305_vec.cc +++ b/third_party/boringssl/src/crypto/poly1305/poly1305_vec.cc @@ -36,11 +36,11 @@ namespace { typedef __m128i xmmi; -alignas(16) const uint32_t poly1305_x64_sse2_message_mask[4] = { +alignas(16) static const uint32_t poly1305_x64_sse2_message_mask[4] = { (1 << 26) - 1, 0, (1 << 26) - 1, 0}; -alignas(16) const uint32_t poly1305_x64_sse2_5[4] = {5, 0, 5, 0}; -alignas(16) const uint32_t poly1305_x64_sse2_1shl128[4] = {(1 << 24), 0, - (1 << 24), 0}; +alignas(16) static const uint32_t poly1305_x64_sse2_5[4] = {5, 0, 5, 0}; +alignas(16) static const uint32_t poly1305_x64_sse2_1shl128[4] = {(1 << 24), 0, + (1 << 24), 0}; uint128_t add128(uint128_t a, uint128_t b) { return a + b; } @@ -159,7 +159,7 @@ void poly1305_first_block(poly1305_state_internal *st, const uint8_t *m) { pad0 = ((uint64_t)p->R23.d[3] << 32) | (uint64_t)p->R23.d[1]; pad1 = ((uint64_t)p->R24.d[3] << 32) | (uint64_t)p->R24.d[1]; - // compute powers r^2,r^4 + // compute powers r², r⁴ r20 = r0; r21 = r1; r22 = r2; @@ -249,7 +249,7 @@ void poly1305_blocks(poly1305_state_internal *st, const uint8_t *m, H4 = st->H[4]; while (bytes >= 64) { - // H *= [r^4,r^4] + // H *= [r⁴, r⁴] p = &st->P[0]; T0 = _mm_mul_epu32(H0, p->R20.v); T1 = _mm_mul_epu32(H0, p->R21.v); @@ -297,7 +297,7 @@ void poly1305_blocks(poly1305_state_internal *st, const uint8_t *m, T5 = _mm_mul_epu32(H4, p->R20.v); T4 = _mm_add_epi64(T4, T5); - // H += [Mx,My]*[r^2,r^2] + // H += [Mx,My]⋅[r², r²] T5 = _mm_unpacklo_epi64(_mm_loadl_epi64((const xmmi *)(m + 0)), _mm_loadl_epi64((const xmmi *)(m + 16))); T6 = _mm_unpacklo_epi64(_mm_loadl_epi64((const xmmi *)(m + 8)), diff --git a/third_party/boringssl/src/crypto/pool/internal.h b/third_party/boringssl/src/crypto/pool/internal.h index 6f8165ff1..49d9b8fe5 100644 --- a/third_party/boringssl/src/crypto/pool/internal.h +++ b/third_party/boringssl/src/crypto/pool/internal.h @@ -73,11 +73,11 @@ class CryptoBufferPool : public crypto_buffer_pool_st, public: CryptoBufferPool(); - // Hash returns the hash of |data|. + // Hash returns the hash of `data`. uint32_t Hash(Span data) const; - // FindBufferLocked looks for a buffer with hash |hash| and contents |data|. - // It returns it if found and nullptr otherwise. |handle_->lock_| must be + // FindBufferLocked looks for a buffer with hash `hash` and contents `data`. + // It returns it if found and nullptr otherwise. `handle_->lock_` must be // locked for reading or writing before calling this. CryptoBuffer *FindBufferLocked(uint32_t hash, Span data); diff --git a/third_party/boringssl/src/crypto/pool/pool.cc b/third_party/boringssl/src/crypto/pool/pool.cc index c9f7bc807..21e220397 100644 --- a/third_party/boringssl/src/crypto/pool/pool.cc +++ b/third_party/boringssl/src/crypto/pool/pool.cc @@ -31,12 +31,12 @@ using namespace bssl; static uint32_t CRYPTO_BUFFER_hash(const CryptoBuffer *buf) { // This function must be called while there is a read or write lock on the - // pool, so it is safe to read |pool_|. + // pool, so it is safe to read `pool_`. return buf->pool_handle_->pool_->Hash(buf->span()); } static int CRYPTO_BUFFER_cmp(const CryptoBuffer *a, const CryptoBuffer *b) { - // Only |CRYPTO_BUFFER|s from the same pool have compatible hashes. + // Only `CRYPTO_BUFFER`s from the same pool have compatible hashes. assert(a->pool_handle_ != nullptr); assert(a->pool_handle_ == b->pool_handle_); return a->span() == b->span() ? 0 : 1; @@ -95,12 +95,19 @@ int CRYPTO_BUFFER_POOL_up_ref(CRYPTO_BUFFER_POOL *pool) { return 1; } +CRYPTO_BUFFER_POOL *CRYPTO_BUFFER_POOL_dup_ref(const CRYPTO_BUFFER_POOL *pool) { + if (pool == nullptr) { + return nullptr; + } + return FromOpaque(pool)->DupRefInternal(); +} + void CryptoBuffer::UpRefInternal() const { - // This is safe in the case that |buf->pool| is NULL because it's just + // This is safe in the case that `buf->pool` is NULL because it's just // standard reference counting in that case. // - // This is also safe if |buf->pool| is non-NULL because, if it were racing - // with |CRYPTO_BUFFER_free| then the two callers must have independent + // This is also safe if `buf->pool` is non-NULL because, if it were racing + // with `CRYPTO_BUFFER_free` then the two callers must have independent // references already and so the reference count will never hit zero. CRYPTO_refcount_inc(&references_); } @@ -123,9 +130,9 @@ void CryptoBuffer::DecRefInternal() { // we can free this buffer. It is possible the pool was already destroyed, // but it cannot be destroyed concurrently. // - // Note it is possible |buf| is no longer in the pool, if it was replaced by + // Note it is possible `buf` is no longer in the pool, if it was replaced by // a static version. If that static version was since removed, it is even - // possible for |found| to be NULL. + // possible for `found` to be NULL. if (CryptoBufferPool *pool = pool_handle_->pool_; pool != nullptr) { CryptoBuffer *found = lh_CryptoBuffer_retrieve(pool->bufs_, this); if (found == this) { @@ -180,7 +187,7 @@ static UniquePtr crypto_buffer_new_with_pool( MutexReadLock lock(&pool->handle_->lock_); CryptoBuffer *duplicate = pool->FindBufferLocked(hash, data); if (data_is_static && duplicate != nullptr && !duplicate->data_is_static_) { - // If the new |CRYPTO_BUFFER| would have static data, but the duplicate + // If the new `CRYPTO_BUFFER` would have static data, but the duplicate // does not, we replace the old one with the new static version. duplicate = nullptr; } @@ -197,7 +204,7 @@ static UniquePtr crypto_buffer_new_with_pool( MutexWriteLock lock(&pool->handle_->lock_); CryptoBuffer *duplicate = pool->FindBufferLocked(hash, data); if (data_is_static && duplicate != nullptr && !duplicate->data_is_static_) { - // If the new |CRYPTO_BUFFER| would have static data, but the duplicate does + // If the new `CRYPTO_BUFFER` would have static data, but the duplicate does // not, we replace the old one with the new static version. duplicate = nullptr; } @@ -205,8 +212,8 @@ static UniquePtr crypto_buffer_new_with_pool( return UpRef(duplicate); } - // Insert |buf| into the pool. Note |old| may be non-NULL if a match was found - // but ignored. |pool->bufs_| does not increment refcounts, so there is no + // Insert `buf` into the pool. Note `old` may be non-NULL if a match was found + // but ignored. `pool->bufs_` does not increment refcounts, so there is no // need to clean up after the replacement. buf->pool_handle_ = UpRef(pool->handle_); CryptoBuffer *old = nullptr; @@ -264,6 +271,9 @@ int CRYPTO_BUFFER_up_ref(CRYPTO_BUFFER *buf) { } CRYPTO_BUFFER *CRYPTO_BUFFER_dup_ref(const CRYPTO_BUFFER *buf) { + if (buf == nullptr) { + return nullptr; + } auto *buf_ = const_cast(buf); FromOpaque(buf_)->UpRefInternal(); return buf_; diff --git a/third_party/boringssl/src/crypto/rand/deterministic.cc b/third_party/boringssl/src/crypto/rand/deterministic.cc index d2fcceee4..ce16d0fb1 100644 --- a/third_party/boringssl/src/crypto/rand/deterministic.cc +++ b/third_party/boringssl/src/crypto/rand/deterministic.cc @@ -14,8 +14,10 @@ #include +// IWYU pragma: begin_keep #include "../bcm_support.h" #include "internal.h" +// IWYU pragma: end_keep #if defined(OPENSSL_RAND_DETERMINISTIC) @@ -28,13 +30,17 @@ using namespace bssl; -// g_num_calls is the number of calls to |CRYPTO_sysrand| that have occurred. +namespace { + +// g_num_calls is the number of calls to `CRYPTO_sysrand` that have occurred. // // This is intentionally not thread-safe. If the fuzzer mode is ever used in a // multi-threaded program, replace this with a thread-local. (A mutex would not // be deterministic.) -static uint64_t g_num_calls = 0; -static StaticMutex g_num_calls_lock; +uint64_t g_num_calls = 0; +StaticMutex g_num_calls_lock; + +} // namespace void RAND_reset_for_fuzzing() { g_num_calls = 0; } diff --git a/third_party/boringssl/src/crypto/rand/fork_detect.cc b/third_party/boringssl/src/crypto/rand/fork_detect.cc index ecee43611..d6683e9f8 100644 --- a/third_party/boringssl/src/crypto/rand/fork_detect.cc +++ b/third_party/boringssl/src/crypto/rand/fork_detect.cc @@ -56,14 +56,16 @@ using namespace bssl; #if defined(OPENSSL_FORK_DETECTION_WIPEONFORK) -static bool wipeonfork(void *addr, size_t page_size) { +namespace { + +bool wipeonfork(void *addr, size_t page_size) { #if defined(OPENSSL_LINUX) // Linux flavor, >=4.14. // Some versions of qemu (up to at least 5.0.0-rc4, see linux-user/syscall.c) - // ignore |madvise| calls and just return zero (i.e. success). But we need to + // ignore `madvise` calls and just return zero (i.e. success). But we need to // know whether MADV_WIPEONFORK actually took effect. Therefore try an invalid - // call to check that the implementation of |madvise| is actually rejecting - // unknown |advice| values. + // call to check that the implementation of `madvise` is actually rejecting + // unknown `advice` values. return madvise(addr, page_size, -1) != 0 && madvise(addr, page_size, MADV_WIPEONFORK) == 0; #elif defined(MAP_INHERIT_ZERO) @@ -75,14 +77,14 @@ static bool wipeonfork(void *addr, size_t page_size) { #endif } -static int g_force_madv_wipeonfork; -static int g_force_madv_wipeonfork_enabled; -static CRYPTO_once_t g_fork_detect_once = CRYPTO_ONCE_INIT; -static StaticMutex g_fork_detect_lock; -static Atomic *g_fork_detect_addr; -static uint64_t g_fork_generation; +int g_force_madv_wipeonfork; +int g_force_madv_wipeonfork_enabled; +CRYPTO_once_t g_fork_detect_once = CRYPTO_ONCE_INIT; +StaticMutex g_fork_detect_lock; +Atomic *g_fork_detect_addr; +uint64_t g_fork_generation; -static void init_fork_detect() { +void init_fork_detect() { if (g_force_madv_wipeonfork) { return; } @@ -107,13 +109,15 @@ static void init_fork_detect() { g_fork_generation = 1; } +} // namespace + uint64_t bssl::CRYPTO_get_fork_generation() { CRYPTO_once(&g_fork_detect_once, init_fork_detect); // In a single-threaded process, there are obviously no races because there's // only a single mutator in the address space. // - // In a multi-threaded environment, |CRYPTO_once| ensures that the flag byte + // In a multi-threaded environment, `CRYPTO_once` ensures that the flag byte // is initialised atomically, even if multiple threads enter this function // concurrently. // @@ -123,8 +127,8 @@ uint64_t bssl::CRYPTO_get_fork_generation() { Atomic *const flag_ptr = g_fork_detect_addr; if (flag_ptr == nullptr) { - // Our kernel is too old to support |MADV_WIPEONFORK| or - // |g_force_madv_wipeonfork| is set. + // Our kernel is too old to support `MADV_WIPEONFORK` or + // `g_force_madv_wipeonfork` is set. if (g_force_madv_wipeonfork && g_force_madv_wipeonfork_enabled) { // A constant generation number to simulate support, even if the kernel // doesn't support it. @@ -143,7 +147,7 @@ uint64_t bssl::CRYPTO_get_fork_generation() { // avoids cacheline contention in the PRNG. uint64_t *const generation_ptr = &g_fork_generation; if (flag_ptr->load() != 0) { - // If we observe a non-zero flag, it is safe to read |generation_ptr| + // If we observe a non-zero flag, it is safe to read `generation_ptr` // without a lock. The flag and generation number are fixed for this copy of // the address space. return *generation_ptr; @@ -162,8 +166,8 @@ uint64_t bssl::CRYPTO_get_fork_generation() { current_generation = 1; } - // We must update |generation_ptr| before |flag_ptr|. Other threads may - // observe |flag_ptr| without taking a lock. + // We must update `generation_ptr` before `flag_ptr`. Other threads may + // observe `flag_ptr` without taking a lock. *generation_ptr = current_generation; flag_ptr->store(1); } @@ -178,10 +182,12 @@ void bssl::CRYPTO_fork_detect_force_madv_wipeonfork_for_testing(int on) { #elif defined(OPENSSL_FORK_DETECTION_PTHREAD_ATFORK) -static CRYPTO_once_t g_pthread_fork_detection_once = CRYPTO_ONCE_INIT; -static uint64_t g_atfork_fork_generation; +namespace { -static void we_are_forked() { +CRYPTO_once_t g_pthread_fork_detection_once = CRYPTO_ONCE_INIT; +uint64_t g_atfork_fork_generation; + +void we_are_forked() { // Immediately after a fork, the process must be single-threaded. uint64_t value = g_atfork_fork_generation + 1; if (value == 0) { @@ -190,13 +196,15 @@ static void we_are_forked() { g_atfork_fork_generation = value; } -static void init_pthread_fork_detection() { +void init_pthread_fork_detection() { if (pthread_atfork(nullptr, nullptr, we_are_forked) != 0) { abort(); } g_atfork_fork_generation = 1; } +} // namespace + uint64_t bssl::CRYPTO_get_fork_generation() { CRYPTO_once(&g_pthread_fork_detection_once, init_pthread_fork_detection); diff --git a/third_party/boringssl/src/crypto/rand/forkunsafe.cc b/third_party/boringssl/src/crypto/rand/forkunsafe.cc index 32760b271..77858d71d 100644 --- a/third_party/boringssl/src/crypto/rand/forkunsafe.cc +++ b/third_party/boringssl/src/crypto/rand/forkunsafe.cc @@ -22,9 +22,13 @@ using namespace bssl; +namespace { + // g_buffering_enabled is one if fork-unsafe buffering has been enabled and zero // otherwise. -static bssl::Atomic g_buffering_enabled; +bssl::Atomic g_buffering_enabled; + +} // namespace #if !defined(OPENSSL_WINDOWS) void RAND_enable_fork_unsafe_buffering(int fd) { diff --git a/third_party/boringssl/src/crypto/rand/getentropy.cc b/third_party/boringssl/src/crypto/rand/getentropy.cc index 7e85d8aba..c39296b18 100644 --- a/third_party/boringssl/src/crypto/rand/getentropy.cc +++ b/third_party/boringssl/src/crypto/rand/getentropy.cc @@ -35,10 +35,10 @@ using namespace bssl; void bssl::CRYPTO_init_sysrand() {} -// CRYPTO_sysrand puts |requested| random bytes into |out|. +// CRYPTO_sysrand puts `requested` random bytes into `out`. void bssl::CRYPTO_sysrand(uint8_t *out, size_t requested) { while (requested > 0) { - // |getentropy| can only request 256 bytes at a time. + // `getentropy` can only request 256 bytes at a time. size_t todo = requested <= 256 ? requested : 256; if (getentropy(out, todo) != 0) { perror("getentropy() failed"); diff --git a/third_party/boringssl/src/crypto/rand/passive.cc b/third_party/boringssl/src/crypto/rand/passive.cc deleted file mode 100644 index a791ec0cb..000000000 --- a/third_party/boringssl/src/crypto/rand/passive.cc +++ /dev/null @@ -1,180 +0,0 @@ -// Copyright 2020 The BoringSSL Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// https://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include - -#include "../bcm_support.h" -#include "../fipsmodule/bcm_interface.h" -#include "../internal.h" - -#if defined(BORINGSSL_FIPS) - -#include - -using namespace bssl; - -// passive_get_seed_entropy writes |out_entropy_len| bytes of entropy, suitable -// for seeding a DRBG, to |out_entropy|. It sets |*out_used_cpu| to one if the -// entropy came directly from the CPU and zero if it came from the OS. It -// actively obtains entropy from the CPU/OS -static void passive_get_seed_entropy(uint8_t *out_entropy, - size_t out_entropy_len, - int *out_want_additional_input) { - *out_want_additional_input = 0; - if (bcm_success(BCM_rand_bytes_hwrng(out_entropy, out_entropy_len))) { - *out_want_additional_input = 1; - } else { - CRYPTO_sysrand(out_entropy, out_entropy_len); - } -} - -#define ENTROPY_READ_LEN \ - (/* last_block size */ 16 + CTR_DRBG_SEED_LEN * BORINGSSL_FIPS_OVERREAD) - -#if defined(OPENSSL_ANDROID) - -#include -#include -#include -#include -#include - -// socket_history_t enumerates whether the entropy daemon should be contacted -// for a given entropy request. Values other than socket_not_yet_attempted are -// sticky so if the first attempt to read from the daemon fails it's assumed -// that the daemon is not present and no more attempts will be made. If the -// first attempt is successful then attempts will be made forever more. -enum class socket_history_t { - // initial value, no connections to the entropy daemon have been made yet. - socket_not_yet_attempted = 0, - // reading from the entropy daemon was successful - socket_success, - // reading from the entropy daemon failed. - socket_failed, -}; - -static std::atomic g_socket_history{ - socket_history_t::socket_not_yet_attempted}; - -// DAEMON_RESPONSE_LEN is the number of bytes that the entropy daemon replies -// with. -#define DAEMON_RESPONSE_LEN 496 - -static_assert(ENTROPY_READ_LEN == DAEMON_RESPONSE_LEN, - "entropy daemon response length mismatch"); - -static int get_seed_from_daemon(uint8_t *out_entropy, size_t out_entropy_len) { - // |RAND_need_entropy| should never call this function for more than - // |DAEMON_RESPONSE_LEN| bytes. - if (out_entropy_len > DAEMON_RESPONSE_LEN) { - abort(); - } - - const socket_history_t socket_history = - g_socket_history.load(std::memory_order_acquire); - if (socket_history == socket_history_t::socket_failed) { - return 0; - } - - int ret = 0; - static const char kSocketPath[] = "/dev/socket/prng_seeder"; - struct sockaddr_un sun; - uint8_t buffer[DAEMON_RESPONSE_LEN]; - size_t done = 0; - const int sock = socket(AF_UNIX, SOCK_STREAM, 0); - if (sock < 0) { - goto out; - } - - memset(&sun, 0, sizeof(sun)); - sun.sun_family = AF_UNIX; - static_assert(sizeof(kSocketPath) <= UNIX_PATH_MAX, "kSocketPath too long"); - OPENSSL_memcpy(sun.sun_path, kSocketPath, sizeof(kSocketPath)); - - if (connect(sock, (struct sockaddr *)&sun, sizeof(sun))) { - goto out; - } - - while (done < sizeof(buffer)) { - ssize_t n; - do { - n = read(sock, buffer + done, sizeof(buffer) - done); - } while (n == -1 && errno == EINTR); - - if (n < 1) { - goto out; - } - done += n; - } - - if (done != DAEMON_RESPONSE_LEN) { - // The daemon should always write |DAEMON_RESPONSE_LEN| bytes on every - // connection. - goto out; - } - - assert(out_entropy_len <= DAEMON_RESPONSE_LEN); - OPENSSL_memcpy(out_entropy, buffer, out_entropy_len); - ret = 1; - -out: - if (socket_history == socket_history_t::socket_not_yet_attempted) { - socket_history_t expected = socket_history_t::socket_not_yet_attempted; - // If another thread has already updated |g_socket_history| then we defer - // to their value. - g_socket_history.compare_exchange_strong( - expected, - (ret == 0) ? socket_history_t::socket_failed - : socket_history_t::socket_success, - std::memory_order_release, std::memory_order_relaxed); - } - - close(sock); - return ret; -} - -#else - -static int get_seed_from_daemon(uint8_t *out_entropy, size_t out_entropy_len) { - return 0; -} - -#endif // OPENSSL_ANDROID - -// RAND_need_entropy is called by the FIPS module when it has blocked because of -// a lack of entropy. This signal is used as an indication to feed it more. -void bssl::RAND_need_entropy(size_t bytes_needed) { - uint8_t buf[ENTROPY_READ_LEN]; - size_t todo = sizeof(buf); - if (todo > bytes_needed) { - todo = bytes_needed; - } - - int want_additional_input; - if (get_seed_from_daemon(buf, todo)) { - want_additional_input = 1; - } else { - passive_get_seed_entropy(buf, todo, &want_additional_input); - } - - if (boringssl_fips_break_test("CRNG")) { - // This breaks the "continuous random number generator test" defined in FIPS - // 140-2, section 4.9.2, and implemented in |rand_get_seed|. - OPENSSL_memset(buf, 0, todo); - } - - BCM_rand_load_entropy(buf, todo, want_additional_input); -} - -#endif // FIPS diff --git a/third_party/boringssl/src/crypto/rand/rand.cc b/third_party/boringssl/src/crypto/rand/rand.cc index 2aa30270a..8d18ac88e 100644 --- a/third_party/boringssl/src/crypto/rand/rand.cc +++ b/third_party/boringssl/src/crypto/rand/rand.cc @@ -30,7 +30,7 @@ int RAND_bytes(uint8_t *buf, size_t len) { int RAND_pseudo_bytes(uint8_t *buf, size_t len) { return RAND_bytes(buf, len); } void RAND_seed(const void *buf, int num) { - // OpenSSH calls |RAND_seed| before jailing on the assumption that any needed + // OpenSSH calls `RAND_seed` before jailing on the assumption that any needed // file descriptors etc will be opened. uint8_t unused; RAND_bytes(&unused, sizeof(unused)); @@ -56,11 +56,15 @@ int RAND_poll() { return 1; } int RAND_status() { return 1; } +namespace { + static const struct rand_meth_st kSSLeayMethod = { RAND_seed, RAND_bytes, RAND_cleanup, RAND_add, RAND_pseudo_bytes, RAND_status, }; +} // namespace + RAND_METHOD *RAND_SSLeay() { return (RAND_METHOD *)&kSSLeayMethod; } RAND_METHOD *RAND_OpenSSL() { return RAND_SSLeay(); } diff --git a/third_party/boringssl/src/crypto/rand/urandom.cc b/third_party/boringssl/src/crypto/rand/urandom.cc index d06ade712..9349c7f20 100644 --- a/third_party/boringssl/src/crypto/rand/urandom.cc +++ b/third_party/boringssl/src/crypto/rand/urandom.cc @@ -41,7 +41,9 @@ void __msan_unpoison(void *, size_t); } #endif -static ssize_t boringssl_getrandom(void *buf, size_t buf_len, unsigned flags) { +namespace { + +ssize_t boringssl_getrandom(void *buf, size_t buf_len, unsigned flags) { ssize_t ret; do { ret = syscall(__NR_getrandom, buf, buf_len, flags); @@ -49,7 +51,7 @@ static ssize_t boringssl_getrandom(void *buf, size_t buf_len, unsigned flags) { #if defined(OPENSSL_MSAN) if (ret > 0) { - // MSAN doesn't recognise |syscall| and thus doesn't notice that we have + // MSAN doesn't recognise `syscall` and thus doesn't notice that we have // initialised the output buffer. __msan_unpoison(buf, ret); } @@ -58,19 +60,19 @@ static ssize_t boringssl_getrandom(void *buf, size_t buf_len, unsigned flags) { return ret; } -// kHaveGetrandom in |urandom_fd| signals that |getrandom| or |getentropy| is +// kHaveGetrandom in `urandom_fd` signals that `getrandom` or `getentropy` is // available and should be used instead. static const int kHaveGetrandom = -3; -// urandom_fd is a file descriptor to /dev/urandom. It's protected by |once|. -static int urandom_fd; +// urandom_fd is a file descriptor to /dev/urandom. It's protected by `once`. +int urandom_fd; -static CRYPTO_once_t rand_once = CRYPTO_ONCE_INIT; +CRYPTO_once_t g_rand_once = CRYPTO_ONCE_INIT; // init_once initializes the state of this module to values previously -// requested. This is the only function that modifies |urandom_fd|, which may be +// requested. This is the only function that modifies `urandom_fd`, which may be // read safely after calling the once. -static void init_once() { +void init_once() { int have_getrandom; uint8_t dummy; ssize_t getrandom_ret = @@ -113,9 +115,11 @@ static void init_once() { urandom_fd = fd; } -void bssl::CRYPTO_init_sysrand() { CRYPTO_once(&rand_once, init_once); } +} // namespace + +void bssl::CRYPTO_init_sysrand() { CRYPTO_once(&g_rand_once, init_once); } -// CRYPTO_sysrand writes |len| bytes of entropy into |out|. +// CRYPTO_sysrand writes `len` bytes of entropy into `out`. void bssl::CRYPTO_sysrand(uint8_t *out, size_t len) { if (len == 0) { return; @@ -123,7 +127,7 @@ void bssl::CRYPTO_sysrand(uint8_t *out, size_t len) { CRYPTO_init_sysrand(); - // Clear |errno| so it has defined value if |read| or |getrandom| + // Clear `errno` so it has defined value if `read` or `getrandom` // "successfully" returns zero. errno = 0; while (len > 0) { diff --git a/third_party/boringssl/src/crypto/rand/windows.cc b/third_party/boringssl/src/crypto/rand/windows.cc index ebd0d4afd..30aac3fc3 100644 --- a/third_party/boringssl/src/crypto/rand/windows.cc +++ b/third_party/boringssl/src/crypto/rand/windows.cc @@ -14,9 +14,11 @@ #include +// IWYU pragma: begin_keep #include "../bcm_support.h" #include "../internal.h" #include "internal.h" +// IWYU pragma: end_keep #if defined(OPENSSL_RAND_WINDOWS) @@ -56,11 +58,13 @@ void bssl::CRYPTO_sysrand(uint8_t *out, size_t requested) { #else +namespace { + // See: https://learn.microsoft.com/en-us/windows/win32/seccng/processprng typedef BOOL(WINAPI *ProcessPrngFunction)(PBYTE pbData, SIZE_T cbData); -static ProcessPrngFunction g_processprng_fn = nullptr; +ProcessPrngFunction g_processprng_fn = nullptr; -static void init_processprng() { +void init_processprng() { HMODULE hmod = LoadLibraryW(L"bcryptprimitives"); if (hmod == nullptr) { abort(); @@ -71,6 +75,8 @@ static void init_processprng() { } } +} // namespace + void bssl::CRYPTO_init_sysrand() { static CRYPTO_once_t once = CRYPTO_ONCE_INIT; CRYPTO_once(&once, init_processprng); diff --git a/third_party/boringssl/src/crypto/rsa/internal.h b/third_party/boringssl/src/crypto/rsa/internal.h index fa56db7b6..ce057a70d 100644 --- a/third_party/boringssl/src/crypto/rsa/internal.h +++ b/third_party/boringssl/src/crypto/rsa/internal.h @@ -28,21 +28,21 @@ int RSA_padding_check_PKCS1_OAEP_mgf1(uint8_t *out, size_t *out_len, size_t param_len, const EVP_MD *md, const EVP_MD *mgf1md); -// rsa_pss_params_get_md returns the hash function used with |params|. This also +// rsa_pss_params_get_md returns the hash function used with `params`. This also // specifies the MGF-1 hash and the salt length because we do not support other // configurations. const EVP_MD *rsa_pss_params_get_md(rsa_pss_params_t params); -// rsa_marshal_pss_params marshals |params| as a DER-encoded RSASSA-PSS-params -// (RFC 4055). It returns one on success and zero on error. If |params| is -// |rsa_pss_params_none|, this function gives an error. +// rsa_marshal_pss_params marshals `params` as a DER-encoded RSASSA-PSS-params +// (RFC 4055). It returns one on success and zero on error. If `params` is +// `rsa_pss_params_none`, this function gives an error. int rsa_marshal_pss_params(CBB *cbb, rsa_pss_params_t params); // rsa_marshal_pss_params decodes a DER-encoded RSASSA-PSS-params // (RFC 4055). It returns one on success and zero on error. On success, it sets -// |*out| to the result. If |allow_explicit_trailer| is non-zero, an explicit +// `*out` to the result. If `allow_explicit_trailer` is non-zero, an explicit // encoding of the trailerField is allowed, although it is not valid DER. This -// function never outputs |rsa_pss_params_none|. +// function never outputs `rsa_pss_params_none`. int rsa_parse_pss_params(CBS *cbs, rsa_pss_params_t *out, int allow_explicit_trailer); diff --git a/third_party/boringssl/src/crypto/rsa/rsa_asn1.cc b/third_party/boringssl/src/crypto/rsa/rsa_asn1.cc index 3869de62b..18d977c68 100644 --- a/third_party/boringssl/src/crypto/rsa/rsa_asn1.cc +++ b/third_party/boringssl/src/crypto/rsa/rsa_asn1.cc @@ -35,7 +35,9 @@ using namespace bssl; -static int parse_integer(CBS *cbs, UniquePtr *out) { +namespace { + +int parse_integer(CBS *cbs, UniquePtr *out) { assert(*out == nullptr); out->reset(BN_new()); if (*out == nullptr) { @@ -44,7 +46,7 @@ static int parse_integer(CBS *cbs, UniquePtr *out) { return BN_parse_asn1_unsigned(cbs, out->get()); } -static int marshal_integer(CBB *cbb, const BIGNUM *bn) { +int marshal_integer(CBB *cbb, const BIGNUM *bn) { if (bn == nullptr) { // An RSA object may be missing some components. OPENSSL_PUT_ERROR(RSA, RSA_R_VALUE_MISSING); @@ -53,15 +55,17 @@ static int marshal_integer(CBB *cbb, const BIGNUM *bn) { return BN_marshal_asn1(cbb, bn); } +} // namespace + RSA *RSA_parse_public_key(CBS *cbs) { RSAImpl *ret = FromOpaque(RSA_new()); if (ret == nullptr) { return nullptr; } CBS child; - if (!CBS_get_asn1(cbs, &child, CBS_ASN1_SEQUENCE) || - !parse_integer(&child, &ret->n) || - !parse_integer(&child, &ret->e) || + if (!CBS_get_asn1(cbs, &child, CBS_ASN1_SEQUENCE) || // + !parse_integer(&child, &ret->n) || // + !parse_integer(&child, &ret->e) || // CBS_len(&child) != 0) { OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_ENCODING); RSA_free(ret); @@ -92,9 +96,9 @@ RSA *RSA_public_key_from_bytes(const uint8_t *in, size_t in_len) { int RSA_marshal_public_key(CBB *cbb, const RSA *rsa) { CBB child; const RSAImpl *impl = FromOpaque(rsa); - if (!CBB_add_asn1(cbb, &child, CBS_ASN1_SEQUENCE) || - !marshal_integer(&child, impl->n.get()) || - !marshal_integer(&child, impl->e.get()) || // + if (!CBB_add_asn1(cbb, &child, CBS_ASN1_SEQUENCE) || // + !marshal_integer(&child, impl->n.get()) || // + !marshal_integer(&child, impl->e.get()) || // !CBB_flush(cbb)) { OPENSSL_PUT_ERROR(RSA, RSA_R_ENCODE_ERROR); return 0; @@ -106,8 +110,8 @@ int RSA_public_key_to_bytes(uint8_t **out_bytes, size_t *out_len, const RSA *rsa) { CBB cbb; CBB_zero(&cbb); - if (!CBB_init(&cbb, 0) || - !RSA_marshal_public_key(&cbb, rsa) || + if (!CBB_init(&cbb, 0) || // + !RSA_marshal_public_key(&cbb, rsa) || // !CBB_finish(&cbb, out_bytes, out_len)) { OPENSSL_PUT_ERROR(RSA, RSA_R_ENCODE_ERROR); CBB_cleanup(&cbb); @@ -116,10 +120,14 @@ int RSA_public_key_to_bytes(uint8_t **out_bytes, size_t *out_len, return 1; } +namespace { + // kVersionTwoPrime is the value of the version field for a two-prime // RSAPrivateKey structure (RFC 8017). static const uint64_t kVersionTwoPrime = 0; +} // namespace + RSA *RSA_parse_private_key(CBS *cbs) { RSAImpl *ret = FromOpaque(RSA_new()); if (ret == nullptr) { @@ -139,13 +147,13 @@ RSA *RSA_parse_private_key(CBS *cbs) { goto err; } - if (!parse_integer(&child, &ret->n) || - !parse_integer(&child, &ret->e) || - !parse_integer(&child, &ret->d) || - !parse_integer(&child, &ret->p) || - !parse_integer(&child, &ret->q) || - !parse_integer(&child, &ret->dmp1) || - !parse_integer(&child, &ret->dmq1) || + if (!parse_integer(&child, &ret->n) || // + !parse_integer(&child, &ret->e) || // + !parse_integer(&child, &ret->d) || // + !parse_integer(&child, &ret->p) || // + !parse_integer(&child, &ret->q) || // + !parse_integer(&child, &ret->dmp1) || // + !parse_integer(&child, &ret->dmq1) || // !parse_integer(&child, &ret->iqmp)) { goto err; } @@ -182,16 +190,16 @@ RSA *RSA_private_key_from_bytes(const uint8_t *in, size_t in_len) { int RSA_marshal_private_key(CBB *cbb, const RSA *rsa) { const RSAImpl *impl = FromOpaque(rsa); CBB child; - if (!CBB_add_asn1(cbb, &child, CBS_ASN1_SEQUENCE) || - !CBB_add_asn1_uint64(&child, kVersionTwoPrime) || - !marshal_integer(&child, impl->n.get()) || - !marshal_integer(&child, impl->e.get()) || - !marshal_integer(&child, impl->d.get()) || - !marshal_integer(&child, impl->p.get()) || - !marshal_integer(&child, impl->q.get()) || - !marshal_integer(&child, impl->dmp1.get()) || - !marshal_integer(&child, impl->dmq1.get()) || - !marshal_integer(&child, impl->iqmp.get()) || // + if (!CBB_add_asn1(cbb, &child, CBS_ASN1_SEQUENCE) || // + !CBB_add_asn1_uint64(&child, kVersionTwoPrime) || // + !marshal_integer(&child, impl->n.get()) || // + !marshal_integer(&child, impl->e.get()) || // + !marshal_integer(&child, impl->d.get()) || // + !marshal_integer(&child, impl->p.get()) || // + !marshal_integer(&child, impl->q.get()) || // + !marshal_integer(&child, impl->dmp1.get()) || // + !marshal_integer(&child, impl->dmq1.get()) || // + !marshal_integer(&child, impl->iqmp.get()) || // !CBB_flush(cbb)) { OPENSSL_PUT_ERROR(RSA, RSA_R_ENCODE_ERROR); return 0; @@ -203,8 +211,8 @@ int RSA_private_key_to_bytes(uint8_t **out_bytes, size_t *out_len, const RSA *rsa) { CBB cbb; CBB_zero(&cbb); - if (!CBB_init(&cbb, 0) || - !RSA_marshal_private_key(&cbb, rsa) || + if (!CBB_init(&cbb, 0) || // + !RSA_marshal_private_key(&cbb, rsa) || // !CBB_finish(&cbb, out_bytes, out_len)) { OPENSSL_PUT_ERROR(RSA, RSA_R_ENCODE_ERROR); CBB_cleanup(&cbb); @@ -255,6 +263,8 @@ RSA *RSAPrivateKey_dup(const RSA *rsa) { return ret; } +namespace { + static const uint8_t kPSSParamsSHA256[] = { 0x30, 0x34, 0xa0, 0x0f, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05, 0x00, 0xa1, 0x1c, 0x30, @@ -276,6 +286,8 @@ static const uint8_t kPSSParamsSHA512[] = { 0x08, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03, 0x05, 0x00, 0xa2, 0x03, 0x02, 0x01, 0x40}; +} // namespace + const EVP_MD *bssl::rsa_pss_params_get_md(rsa_pss_params_t params) { switch (params) { case rsa_pss_none: @@ -310,10 +322,14 @@ int bssl::rsa_marshal_pss_params(CBB *cbb, rsa_pss_params_t params) { return CBB_add_bytes(cbb, bytes.data(), bytes.size()); } +namespace { + // 1.2.840.113549.1.1.8 static const uint8_t kMGF1OID[] = {0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x08}; +} // namespace + int bssl::rsa_parse_pss_params(CBS *cbs, rsa_pss_params_t *out, int allow_explicit_trailer) { // See RFC 4055, section 3.1. @@ -326,14 +342,14 @@ int bssl::rsa_parse_pss_params(CBS *cbs, rsa_pss_params_t *out, if (!CBS_get_asn1(cbs, ¶ms, CBS_ASN1_SEQUENCE) || !CBS_get_asn1(¶ms, &hash_wrapper, CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 0) || - // |hash_wrapper| will be parsed below. + // `hash_wrapper` will be parsed below. !CBS_get_asn1(¶ms, &mask_wrapper, CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 1) || !CBS_get_asn1(&mask_wrapper, &mask_alg, CBS_ASN1_SEQUENCE) || !CBS_get_asn1(&mask_alg, &mask_oid, CBS_ASN1_OBJECT) || // We only support MGF-1. Span(mask_oid) != kMGF1OID || - // The remainder of |mask_alg| will be parsed below. + // The remainder of `mask_alg` will be parsed below. CBS_len(&mask_wrapper) != 0 || !CBS_get_asn1(¶ms, &salt_wrapper, CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 2) || diff --git a/third_party/boringssl/src/crypto/rsa/rsa_crypt.cc b/third_party/boringssl/src/crypto/rsa/rsa_crypt.cc index 16085c53c..47254d9aa 100644 --- a/third_party/boringssl/src/crypto/rsa/rsa_crypt.cc +++ b/third_party/boringssl/src/crypto/rsa/rsa_crypt.cc @@ -31,7 +31,9 @@ using namespace bssl; -static void rand_nonzero(uint8_t *out, size_t len) { +namespace { + +void rand_nonzero(uint8_t *out, size_t len) { RAND_bytes(out, len); for (size_t i = 0; i < len; i++) { @@ -43,6 +45,8 @@ static void rand_nonzero(uint8_t *out, size_t len) { } } +} // namespace + int RSA_padding_add_PKCS1_OAEP_mgf1(uint8_t *to, size_t to_len, const uint8_t *from, size_t from_len, const uint8_t *param, size_t param_len, @@ -220,8 +224,10 @@ int bssl::RSA_padding_check_PKCS1_OAEP_mgf1(uint8_t *out, size_t *out_len, return 0; } -static int rsa_padding_add_PKCS1_type_2(uint8_t *to, size_t to_len, - const uint8_t *from, size_t from_len) { +namespace { + +int rsa_padding_add_PKCS1_type_2(uint8_t *to, size_t to_len, + const uint8_t *from, size_t from_len) { // See RFC 8017, section 7.2.1. if (to_len < RSA_PKCS1_PADDING_SIZE) { OPENSSL_PUT_ERROR(RSA, RSA_R_KEY_SIZE_TOO_SMALL); @@ -243,9 +249,9 @@ static int rsa_padding_add_PKCS1_type_2(uint8_t *to, size_t to_len, return 1; } -static int rsa_padding_check_PKCS1_type_2(uint8_t *out, size_t *out_len, - size_t max_out, const uint8_t *from, - size_t from_len) { +int rsa_padding_check_PKCS1_type_2(uint8_t *out, size_t *out_len, + size_t max_out, const uint8_t *from, + size_t from_len) { if (from_len == 0) { OPENSSL_PUT_ERROR(RSA, RSA_R_EMPTY_PUBLIC_KEY); return 0; @@ -254,7 +260,7 @@ static int rsa_padding_check_PKCS1_type_2(uint8_t *out, size_t *out_len, // PKCS#1 v1.5 decryption. See "PKCS #1 v2.2: RSA Cryptography // Standard", section 7.2.2. if (from_len < RSA_PKCS1_PADDING_SIZE) { - // |from| is zero-padded to the size of the RSA modulus, a public value, so + // `from` is zero-padded to the size of the RSA modulus, a public value, so // this can be rejected in non-constant time. OPENSSL_PUT_ERROR(RSA, RSA_R_KEY_SIZE_TOO_SMALL); return 0; @@ -278,16 +284,16 @@ static int rsa_padding_check_PKCS1_type_2(uint8_t *out, size_t *out_len, // We must have found the end of PS. valid_index &= ~looking_for_index; - // PS must be at least 8 bytes long, and it starts two bytes into |from|. + // PS must be at least 8 bytes long, and it starts two bytes into `from`. valid_index &= constant_time_ge_w(zero_index, 2 + 8); // Skip the zero byte. zero_index++; // NOTE: Although this logic attempts to be constant time, the API contracts - // of this function and |RSA_decrypt| with |RSA_PKCS1_PADDING| make it + // of this function and `RSA_decrypt` with `RSA_PKCS1_PADDING` make it // impossible to completely avoid Bleichenbacher's attack. Consumers should - // use |RSA_PADDING_NONE| and perform the padding check in constant-time + // use `RSA_PADDING_NONE` and perform the padding check in constant-time // combined with a swap to a random session key or other mitigation. CONSTTIME_DECLASSIFY(&valid_index, sizeof(valid_index)); CONSTTIME_DECLASSIFY(&zero_index, sizeof(zero_index)); @@ -300,7 +306,7 @@ static int rsa_padding_check_PKCS1_type_2(uint8_t *out, size_t *out_len, const size_t msg_len = from_len - zero_index; if (msg_len > max_out) { // This shouldn't happen because this function is always called with - // |max_out| as the key size and |from_len| is bounded by the key size. + // `max_out` as the key size and `from_len` is bounded by the key size. OPENSSL_PUT_ERROR(RSA, RSA_R_PKCS_DECODING_ERROR); return 0; } @@ -310,6 +316,8 @@ static int rsa_padding_check_PKCS1_type_2(uint8_t *out, size_t *out_len, return 1; } +} // namespace + int RSA_public_encrypt(size_t flen, const uint8_t *from, uint8_t *to, RSA *rsa, int padding) { size_t out_len; @@ -426,9 +434,10 @@ int RSA_encrypt(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out, return ret; } -static int rsa_default_decrypt(RSA *rsa, size_t *out_len, uint8_t *out, - size_t max_out, const uint8_t *in, size_t in_len, - int padding) { +namespace { + +int rsa_default_decrypt(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out, + const uint8_t *in, size_t in_len, int padding) { const unsigned rsa_size = RSA_size(rsa); uint8_t *buf = nullptr; int ret = 0; @@ -491,6 +500,8 @@ static int rsa_default_decrypt(RSA *rsa, size_t *out_len, uint8_t *out, return ret; } +} // namespace + int RSA_decrypt(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out, const uint8_t *in, size_t in_len, int padding) { auto *impl = FromOpaque(rsa); diff --git a/third_party/boringssl/src/crypto/rsa/rsa_extra.cc b/third_party/boringssl/src/crypto/rsa/rsa_extra.cc index 58015bd4b..96fe4e02c 100644 --- a/third_party/boringssl/src/crypto/rsa/rsa_extra.cc +++ b/third_party/boringssl/src/crypto/rsa/rsa_extra.cc @@ -20,11 +20,11 @@ void RSA_blinding_off(RSA *rsa) {} const RSA_PSS_PARAMS *RSA_get0_pss_params(const RSA *rsa) { // We do not currently implement this function. By default, we will not parse - // |EVP_PKEY_RSA_PSS|. Callers that opt in with a BoringSSL-specific API are + // `EVP_PKEY_RSA_PSS`. Callers that opt in with a BoringSSL-specific API are // currently assumed to not need this function. Callers that need that opt-in // and this functionality should contact the BoringSSL team. // - // If we do add support later, the |maskHash| field should be filled in for + // If we do add support later, the `maskHash` field should be filled in for // OpenSSL compatibility. return nullptr; } diff --git a/third_party/boringssl/src/crypto/rsa/rsa_print.cc b/third_party/boringssl/src/crypto/rsa/rsa_print.cc index 52d773214..274df3f69 100644 --- a/third_party/boringssl/src/crypto/rsa/rsa_print.cc +++ b/third_party/boringssl/src/crypto/rsa/rsa_print.cc @@ -19,7 +19,6 @@ int RSA_print(BIO *bio, const RSA *rsa, int indent) { bssl::UniquePtr pkey(EVP_PKEY_new()); - return pkey != nullptr && - EVP_PKEY_set1_RSA(pkey.get(), const_cast(rsa)) && + return pkey != nullptr && EVP_PKEY_set1_RSA(pkey.get(), rsa) && EVP_PKEY_print_private(bio, pkey.get(), indent, nullptr); } diff --git a/third_party/boringssl/src/crypto/sha/sha1.cc b/third_party/boringssl/src/crypto/sha/sha1.cc index 6b06961b7..5cb31c769 100644 --- a/third_party/boringssl/src/crypto/sha/sha1.cc +++ b/third_party/boringssl/src/crypto/sha/sha1.cc @@ -36,15 +36,6 @@ int SHA1_Final(uint8_t out[SHA_DIGEST_LENGTH], SHA_CTX *sha) { return 1; } -uint8_t *SHA1(const uint8_t *data, size_t len, uint8_t out[SHA_DIGEST_LENGTH]) { - SHA_CTX ctx; - BCM_sha1_init(&ctx); - BCM_sha1_update(&ctx, data, len); - BCM_sha1_final(out, &ctx); - OPENSSL_cleanse(&ctx, sizeof(ctx)); - return out; -} - void SHA1_Transform(SHA_CTX *sha, const uint8_t block[SHA_CBLOCK]) { BCM_sha1_transform(sha, block); } diff --git a/third_party/boringssl/src/crypto/sha/sha256.cc b/third_party/boringssl/src/crypto/sha/sha256.cc index 3ef66bce5..452817724 100644 --- a/third_party/boringssl/src/crypto/sha/sha256.cc +++ b/third_party/boringssl/src/crypto/sha/sha256.cc @@ -36,16 +36,6 @@ int SHA224_Final(uint8_t out[SHA224_DIGEST_LENGTH], SHA256_CTX *sha) { return 1; } -uint8_t *SHA224(const uint8_t *data, size_t len, - uint8_t out[SHA224_DIGEST_LENGTH]) { - SHA256_CTX ctx; - BCM_sha224_init(&ctx); - BCM_sha224_update(&ctx, data, len); - BCM_sha224_final(out, &ctx); - OPENSSL_cleanse(&ctx, sizeof(ctx)); - return out; -} - int SHA256_Init(SHA256_CTX *sha) { BCM_sha256_init(sha); return 1; @@ -69,16 +59,6 @@ int SHA256_Final(uint8_t out[SHA256_DIGEST_LENGTH], SHA256_CTX *sha) { return 1; } -uint8_t *SHA256(const uint8_t *data, size_t len, - uint8_t out[SHA256_DIGEST_LENGTH]) { - SHA256_CTX ctx; - BCM_sha256_init(&ctx); - BCM_sha256_update(&ctx, data, len); - BCM_sha256_final(out, &ctx); - OPENSSL_cleanse(&ctx, sizeof(ctx)); - return out; -} - void SHA256_Transform(SHA256_CTX *sha, const uint8_t block[SHA256_CBLOCK]) { BCM_sha256_transform(sha, block); } diff --git a/third_party/boringssl/src/crypto/sha/sha512.cc b/third_party/boringssl/src/crypto/sha/sha512.cc index afb5516ca..87c94a22b 100644 --- a/third_party/boringssl/src/crypto/sha/sha512.cc +++ b/third_party/boringssl/src/crypto/sha/sha512.cc @@ -36,16 +36,6 @@ int SHA384_Final(uint8_t out[SHA384_DIGEST_LENGTH], SHA512_CTX *sha) { return 1; } -uint8_t *SHA384(const uint8_t *data, size_t len, - uint8_t out[SHA384_DIGEST_LENGTH]) { - SHA512_CTX ctx; - BCM_sha384_init(&ctx); - BCM_sha384_update(&ctx, data, len); - BCM_sha384_final(out, &ctx); - OPENSSL_cleanse(&ctx, sizeof(ctx)); - return out; -} - int SHA512_256_Init(SHA512_CTX *sha) { BCM_sha512_256_init(sha); return 1; @@ -61,16 +51,6 @@ int SHA512_256_Final(uint8_t out[SHA512_256_DIGEST_LENGTH], SHA512_CTX *sha) { return 1; } -uint8_t *SHA512_256(const uint8_t *data, size_t len, - uint8_t out[SHA512_256_DIGEST_LENGTH]) { - SHA512_CTX ctx; - BCM_sha512_256_init(&ctx); - BCM_sha512_256_update(&ctx, data, len); - BCM_sha512_256_final(out, &ctx); - OPENSSL_cleanse(&ctx, sizeof(ctx)); - return out; -} - int SHA512_Init(SHA512_CTX *sha) { BCM_sha512_init(sha); return 1; @@ -91,16 +71,6 @@ int SHA512_Final(uint8_t out[SHA512_DIGEST_LENGTH], SHA512_CTX *sha) { return 1; } -uint8_t *SHA512(const uint8_t *data, size_t len, - uint8_t out[SHA512_DIGEST_LENGTH]) { - SHA512_CTX ctx; - BCM_sha512_init(&ctx); - BCM_sha512_update(&ctx, data, len); - BCM_sha512_final(out, &ctx); - OPENSSL_cleanse(&ctx, sizeof(ctx)); - return out; -} - void SHA512_Transform(SHA512_CTX *sha, const uint8_t block[SHA512_CBLOCK]) { BCM_sha512_transform(sha, block); } diff --git a/third_party/boringssl/src/crypto/slhdsa/slhdsa.cc b/third_party/boringssl/src/crypto/slhdsa/slhdsa.cc index ff4707357..ece60ff02 100644 --- a/third_party/boringssl/src/crypto/slhdsa/slhdsa.cc +++ b/third_party/boringssl/src/crypto/slhdsa/slhdsa.cc @@ -21,12 +21,14 @@ using namespace bssl; +static_assert(SLHDSA_SHA2_128S_SEED_BYTES == 3 * BCM_SLHDSA_SHA2_128S_N); static_assert(SLHDSA_SHA2_128S_PUBLIC_KEY_BYTES == BCM_SLHDSA_SHA2_128S_PUBLIC_KEY_BYTES); static_assert(SLHDSA_SHA2_128S_PRIVATE_KEY_BYTES == BCM_SLHDSA_SHA2_128S_PRIVATE_KEY_BYTES); static_assert(SLHDSA_SHA2_128S_SIGNATURE_BYTES == BCM_SLHDSA_SHA2_128S_SIGNATURE_BYTES); +static_assert(SLHDSA_SHAKE_256F_SEED_BYTES == 3 * BCM_SLHDSA_SHAKE_256F_N); static_assert(SLHDSA_SHAKE_256F_PUBLIC_KEY_BYTES == BCM_SLHDSA_SHAKE_256F_PUBLIC_KEY_BYTES); static_assert(SLHDSA_SHAKE_256F_PRIVATE_KEY_BYTES == @@ -34,6 +36,22 @@ static_assert(SLHDSA_SHAKE_256F_PRIVATE_KEY_BYTES == static_assert(SLHDSA_SHAKE_256F_SIGNATURE_BYTES == BCM_SLHDSA_SHAKE_256F_SIGNATURE_BYTES); +void SLHDSA_SHA2_128S_generate_key_from_seed( + uint8_t out_public_key[SLHDSA_SHA2_128S_PUBLIC_KEY_BYTES], + uint8_t out_private_key[SLHDSA_SHA2_128S_PRIVATE_KEY_BYTES], + const uint8_t seed[SLHDSA_SHA2_128S_SEED_BYTES]) { + BCM_slhdsa_sha2_128s_generate_key_from_seed(out_public_key, out_private_key, + seed); +} + +void SLHDSA_SHAKE_256F_generate_key_from_seed( + uint8_t out_public_key[SLHDSA_SHAKE_256F_PUBLIC_KEY_BYTES], + uint8_t out_private_key[SLHDSA_SHAKE_256F_PRIVATE_KEY_BYTES], + const uint8_t seed[SLHDSA_SHAKE_256F_SEED_BYTES]) { + BCM_slhdsa_shake_256f_generate_key_from_seed(out_public_key, out_private_key, + seed); +} + void SLHDSA_SHA2_128S_generate_key( uint8_t out_public_key[SLHDSA_SHA2_128S_PUBLIC_KEY_BYTES], uint8_t out_private_key[SLHDSA_SHA2_128S_PRIVATE_KEY_BYTES]) { @@ -73,8 +91,7 @@ int SLHDSA_SHAKE_256F_sign( const uint8_t *msg, size_t msg_len, const uint8_t *context, size_t context_len) { return bcm_success(BCM_slhdsa_shake_256f_sign(out_signature, private_key, msg, - msg_len, context, - context_len)); + msg_len, context, context_len)); } int SLHDSA_SHA2_128S_verify( @@ -83,8 +100,8 @@ int SLHDSA_SHA2_128S_verify( const uint8_t *msg, size_t msg_len, const uint8_t *context, size_t context_len) { return bcm_success(BCM_slhdsa_sha2_128s_verify(signature, signature_len, - public_key, msg, msg_len, - context, context_len)); + public_key, msg, msg_len, + context, context_len)); } int SLHDSA_SHAKE_256F_verify( @@ -93,8 +110,8 @@ int SLHDSA_SHAKE_256F_verify( const uint8_t *msg, size_t msg_len, const uint8_t *context, size_t context_len) { return bcm_success(BCM_slhdsa_shake_256f_verify(signature, signature_len, - public_key, msg, msg_len, - context, context_len)); + public_key, msg, msg_len, + context, context_len)); } int SLHDSA_SHA2_128S_prehash_sign( diff --git a/third_party/boringssl/src/crypto/spake2plus/internal.h b/third_party/boringssl/src/crypto/spake2plus/internal.h index 7344c57ee..2ca85ce48 100644 --- a/third_party/boringssl/src/crypto/spake2plus/internal.h +++ b/third_party/boringssl/src/crypto/spake2plus/internal.h @@ -64,14 +64,14 @@ constexpr size_t kSecretSize = 32; // step of the SPAKE2+ protocol. See the following for more details: // https://www.rfc-editor.org/rfc/rfc9383.html#section-3.2 // -// The |password| argument is the mandatory prover password. The |out_w0|, -// |out_w1|, and |out_registration_record| arguments are where the password +// The `password` argument is the mandatory prover password. The `out_w0`, +// `out_w1`, and `out_registration_record` arguments are where the password // verifiers (w0 and w1) and registration record (L) are stored, respectively. -// The prover is given |out_w0| and |out_w1| while the verifier is given -// |out_w0| and |out_registration_record|. +// The prover is given `out_w0` and `out_w1` while the verifier is given +// `out_w0` and `out_registration_record`. // -// To ensure success, |out_w0| and |out_w1| must be of length |kVerifierSize|, -// and |out_registration_record| of size |kRegistrationRecordSize|. +// To ensure success, `out_w0` and `out_w1` must be of length `kVerifierSize`, +// and `out_registration_record` of size `kRegistrationRecordSize`. [[nodiscard]] OPENSSL_EXPORT bool Register( Span out_w0, Span out_w1, Span out_registration_record, Span password, @@ -87,10 +87,10 @@ class OPENSSL_EXPORT Prover { // Init creates a new prover, which can only be used for a single execution of // the protocol. // - // The |context| argument is an application-specific value meant to constrain - // the protocol execution. The |w0| and |w1| arguments are password verifier + // The `context` argument is an application-specific value meant to constrain + // the protocol execution. The `w0` and `w1` arguments are password verifier // values computed during the offline registration phase of the protocol. The - // |id_prover| and |id_verifier| arguments allow optional, opaque names to be + // `id_prover` and `id_verifier` arguments allow optional, opaque names to be // bound into the protocol. See the following for more information about how // these identities may be chosen: // https://www.rfc-editor.org/rfc/rfc9383.html#name-definition-of-spake2 @@ -100,20 +100,20 @@ class OPENSSL_EXPORT Prover { Span w0, Span w1, Span x = Span()); - // GenerateShare computes a SPAKE2+ share and writes it to |out_share|. + // GenerateShare computes a SPAKE2+ share and writes it to `out_share`. // - // This function can only be called once for a given |Prover|. To ensure - // success, |out_share| must be |kShareSize| bytes. + // This function can only be called once for a given `Prover`. To ensure + // success, `out_share` must be `kShareSize` bytes. [[nodiscard]] bool GenerateShare(Span out_share); // ComputeConfirmation computes a SPAKE2+ key confirmation - // message and writes it to |out_confirm|. It also computes the shared secret - // and writes it to |out_secret|. + // message and writes it to `out_confirm`. It also computes the shared secret + // and writes it to `out_secret`. // - // This function can only be called once for a given |Prover|. + // This function can only be called once for a given `Prover`. // - // To ensure success, |out_confirm| must be |kConfirmSize| bytes - // and |out_secret| must be |kSecretSize| bytes. + // To ensure success, `out_confirm` must be `kConfirmSize` bytes + // and `out_secret` must be `kSecretSize` bytes. [[nodiscard]] bool ComputeConfirmation(Span out_confirm, Span out_secret, Span peer_share, @@ -146,12 +146,12 @@ class OPENSSL_EXPORT Verifier { // Init creates a new verifier, which can only be used for a single execution // of the protocol. // - // The |context| argument is an application-specific value meant to constrain - // the protocol execution. The |w0| and |registration_record| arguments are - // required, and are computed by the prover via |Register|. Only the prover - // can produce |w0| and |registration_record|, as they require + // The `context` argument is an application-specific value meant to constrain + // the protocol execution. The `w0` and `registration_record` arguments are + // required, and are computed by the prover via `Register`. Only the prover + // can produce `w0` and `registration_record`, as they require // knowledge of the password. The prover must securely transmit this to the - // verifier out-of-band. The |id_prover| and |id_verifier| arguments allow + // verifier out-of-band. The `id_prover` and `id_verifier` arguments allow // optional, opaque names to be bound into the protocol. See the following for // more information about how these identities may be chosen: // https://www.rfc-editor.org/rfc/rfc9383.html#name-definition-of-spake2 @@ -163,23 +163,23 @@ class OPENSSL_EXPORT Verifier { Span y = Span()); // ProcessProverShare computes a SPAKE2+ share from an input share, - // |prover_share|, and writes it to |out_share|. It also computes the key - // confirmation message and writes it to |out_confirm|. Finally, it computes - // the shared secret and writes it to |out_secret|. + // `prover_share`, and writes it to `out_share`. It also computes the key + // confirmation message and writes it to `out_confirm`. Finally, it computes + // the shared secret and writes it to `out_secret`. // - // This function can only be called once for a given |Verifier|. + // This function can only be called once for a given `Verifier`. // - // To ensure success, |out_share| must be |kShareSize| bytes, |out_confirm| - // must be |kConfirmSize| bytes, and |out_secret| must be |kSecretSize| bytes. + // To ensure success, `out_share` must be `kShareSize` bytes, `out_confirm` + // must be `kConfirmSize` bytes, and `out_secret` must be `kSecretSize` bytes. [[nodiscard]] bool ProcessProverShare(Span out_share, Span out_confirm, Span out_secret, Span prover_share); // VerifyProverConfirmation verifies a SPAKE2+ key confirmation message, - // |prover_confirm|. + // `prover_confirm`. // - // This function can only be called once for a given |Verifier|. + // This function can only be called once for a given `Verifier`. [[nodiscard]] bool VerifyProverConfirmation(Span peer_confirm); private: diff --git a/third_party/boringssl/src/crypto/stack/stack.cc b/third_party/boringssl/src/crypto/stack/stack.cc index 89b4f9543..55ae25d7f 100644 --- a/third_party/boringssl/src/crypto/stack/stack.cc +++ b/third_party/boringssl/src/crypto/stack/stack.cc @@ -29,14 +29,14 @@ using namespace bssl; struct stack_st { - // num contains the number of valid pointers in |data|. + // num contains the number of valid pointers in `data`. size_t num; void **data; - // sorted is non-zero if the values pointed to by |data| are in ascending - // order, based on |comp|. + // sorted is non-zero if the values pointed to by `data` are in ascending + // order, based on `comp`. int sorted; // num_alloc contains the number of pointers allocated in the buffer pointed - // to by |data|, which may be larger than |num|. + // to by `data`, which may be larger than `num`. size_t num_alloc; // comp is an optional comparison function. OPENSSL_sk_cmp_func comp; @@ -93,6 +93,13 @@ void *OPENSSL_sk_value(const OPENSSL_STACK *sk, size_t i) { return sk->data[i]; } +void *OPENSSL_sk_last(const OPENSSL_STACK *sk) { + if (!sk || sk->num == 0) { + return nullptr; + } + return sk->data[sk->num - 1]; +} + void *OPENSSL_sk_set(OPENSSL_STACK *sk, size_t i, void *value) { if (!sk || i >= sk->num) { return nullptr; @@ -124,8 +131,8 @@ void OPENSSL_sk_pop_free_ex(OPENSSL_STACK *sk, OPENSSL_sk_free(sk); } -// Historically, |sk_pop_free| called the function as |OPENSSL_sk_free_func| -// directly. This is undefined in C. Some callers called |sk_pop_free| directly, +// Historically, `sk_pop_free` called the function as `OPENSSL_sk_free_func` +// directly. This is undefined in C. Some callers called `sk_pop_free` directly, // so we must maintain a compatibility version for now. static void call_free_func_legacy(OPENSSL_sk_free_func func, void *ptr) { func(ptr); @@ -271,20 +278,20 @@ int OPENSSL_sk_find(const OPENSSL_STACK *sk, size_t *out_index, const void *p, // The stack is sorted, so binary search to find the element. // - // |lo| and |hi| maintain a half-open interval of where the answer may be. All - // indices such that |lo <= idx < hi| are candidates. + // `lo` and `hi` maintain a half-open interval of where the answer may be. All + // indices such that `lo <= idx < hi` are candidates. size_t lo = 0, hi = sk->num; while (lo < hi) { - // Bias |mid| towards |lo|. See the |r == 0| case below. + // Bias `mid` towards `lo`. See the `r == 0` case below. size_t mid = lo + (hi - lo - 1) / 2; assert(lo <= mid && mid < hi); int r = call_cmp_func(sk->comp, p, sk->data[mid]); if (r > 0) { - lo = mid + 1; // |mid| is too low. + lo = mid + 1; // `mid` is too low. } else if (r < 0) { - hi = mid; // |mid| is too high. + hi = mid; // `mid` is too high. } else { - // |mid| matches. However, this function returns the earliest match, so we + // `mid` matches. However, this function returns the earliest match, so we // can only return if the range has size one. if (hi - lo == 1) { if (out_index != nullptr) { @@ -292,8 +299,8 @@ int OPENSSL_sk_find(const OPENSSL_STACK *sk, size_t *out_index, const void *p, } return 1; } - // The sample is biased towards |lo|. |mid| can only be |hi - 1| if - // |hi - lo| was one, so this makes forward progress. + // The sample is biased towards `lo`. `mid` can only be `hi - 1` if + // `hi - lo` was one, so this makes forward progress. assert(mid + 1 < hi); hi = mid + 1; } diff --git a/third_party/boringssl/src/crypto/thread_win.cc b/third_party/boringssl/src/crypto/thread_win.cc index 5fce1bcc8..d87c29632 100644 --- a/third_party/boringssl/src/crypto/thread_win.cc +++ b/third_party/boringssl/src/crypto/thread_win.cc @@ -57,10 +57,10 @@ static void thread_local_init() { static void NTAPI thread_local_destructor(PVOID module, DWORD reason, PVOID reserved) { - // Only free memory on |DLL_THREAD_DETACH|, not |DLL_PROCESS_DETACH|. In + // Only free memory on `DLL_THREAD_DETACH`, not `DLL_PROCESS_DETACH`. In // VS2015's debug runtime, the C runtime has been unloaded by the time - // |DLL_PROCESS_DETACH| runs. See https://crbug.com/575795. This is consistent - // with |pthread_key_create| which does not call destructors on process exit, + // `DLL_PROCESS_DETACH` runs. See https://crbug.com/575795. This is consistent + // with `pthread_key_create` which does not call destructors on process exit, // only thread exit. if (reason != DLL_THREAD_DETACH) { return; @@ -103,7 +103,7 @@ static void NTAPI thread_local_destructor(PVOID module, DWORD reason, // a reference to p_thread_callback_boringssl to prevent whole program // optimization from discarding the variable. // -// Note, in the prefixed build, |p_thread_callback_boringssl| may be a macro. +// Note, in the prefixed build, `p_thread_callback_boringssl` may be a macro. #define STRINGIFY(x) #x #define EXPAND_AND_STRINGIFY(x) STRINGIFY(x) #ifdef _WIN64 @@ -160,13 +160,13 @@ PIMAGE_TLS_CALLBACK p_thread_callback_boringssl = thread_local_destructor; #endif // _WIN64 static void **get_thread_locals() { - // |TlsGetValue| clears the last error even on success, so that callers may + // `TlsGetValue` clears the last error even on success, so that callers may // distinguish it successfully returning NULL or failing. It is documented to - // never fail if the argument is a valid index from |TlsAlloc|, so we do not + // never fail if the argument is a valid index from `TlsAlloc`, so we do not // need to handle this. // // However, this error-mangling behavior interferes with the caller's use of - // |GetLastError|. In particular |SSL_get_error| queries the error queue to + // `GetLastError`. In particular `SSL_get_error` queries the error queue to // determine whether the caller should look at the OS's errors. To avoid // destroying state, save and restore the Windows error. // diff --git a/third_party/boringssl/src/crypto/trust_token/internal.h b/third_party/boringssl/src/crypto/trust_token/internal.h index d3afc172f..600681b57 100644 --- a/third_party/boringssl/src/crypto/trust_token/internal.h +++ b/third_party/boringssl/src/crypto/trust_token/internal.h @@ -44,8 +44,8 @@ BSSL_NAMESPACE_BEGIN typedef struct { // TODO(https://crbug.com/boringssl/334): These should store - // |bssl::EC_PRECOMP| so that |TRUST_TOKEN_finish_issuance| can use - // |ec_point_mul_scalar_precomp|. + // `bssl::EC_PRECOMP` so that `TRUST_TOKEN_finish_issuance` can use + // `ec_point_mul_scalar_precomp`. bssl::EC_AFFINE pub0; bssl::EC_AFFINE pub1; bssl::EC_AFFINE pubs; @@ -75,7 +75,7 @@ typedef struct pmb_pretoken_st { bssl::EC_AFFINE Tp; } TRUST_TOKEN_PRETOKEN; -// TRUST_TOKEN_PRETOKEN_free releases the memory associated with |token|. +// TRUST_TOKEN_PRETOKEN_free releases the memory associated with `token`. OPENSSL_EXPORT void TRUST_TOKEN_PRETOKEN_free(TRUST_TOKEN_PRETOKEN *token); DEFINE_NAMESPACED_STACK_OF(TRUST_TOKEN_PRETOKEN) @@ -88,8 +88,8 @@ DEFINE_NAMESPACED_STACK_OF(TRUST_TOKEN_PRETOKEN) // construction with validity verification, described in appendix H, // construction 6. -// The following functions implement the corresponding |TRUST_TOKENS_METHOD| -// functions for |TRUST_TOKENS_experiment_v1|'s PMBTokens construction which +// The following functions implement the corresponding `TRUST_TOKENS_METHOD` +// functions for `TRUST_TOKENS_experiment_v1`'s PMBTokens construction which // uses P-384. int pmbtoken_exp1_generate_key(CBB *out_private, CBB *out_public); int pmbtoken_exp1_derive_key_from_secret(CBB *out_private, CBB *out_public, @@ -120,8 +120,8 @@ int pmbtoken_exp1_read(const TRUST_TOKEN_ISSUER_KEY *key, // function is used to confirm H was computed as expected. OPENSSL_EXPORT int pmbtoken_exp1_get_h_for_testing(uint8_t out[97]); -// The following functions implement the corresponding |TRUST_TOKENS_METHOD| -// functions for |TRUST_TOKENS_experiment_v2|'s PMBTokens construction which +// The following functions implement the corresponding `TRUST_TOKENS_METHOD` +// functions for `TRUST_TOKENS_experiment_v2`'s PMBTokens construction which // uses P-384. int pmbtoken_exp2_generate_key(CBB *out_private, CBB *out_public); int pmbtoken_exp2_derive_key_from_secret(CBB *out_private, CBB *out_public, @@ -152,8 +152,8 @@ int pmbtoken_exp2_read(const TRUST_TOKEN_ISSUER_KEY *key, // function is used to confirm H was computed as expected. OPENSSL_EXPORT int pmbtoken_exp2_get_h_for_testing(uint8_t out[97]); -// The following functions implement the corresponding |TRUST_TOKENS_METHOD| -// functions for |TRUST_TOKENS_pst_v1|'s PMBTokens construction which uses +// The following functions implement the corresponding `TRUST_TOKENS_METHOD` +// functions for `TRUST_TOKENS_pst_v1`'s PMBTokens construction which uses // P-384. int pmbtoken_pst1_generate_key(CBB *out_private, CBB *out_public); int pmbtoken_pst1_derive_key_from_secret(CBB *out_private, CBB *out_public, @@ -191,11 +191,11 @@ OPENSSL_EXPORT int pmbtoken_pst1_get_h_for_testing(uint8_t out[97]); // and provide anonymous tokens. This implementation uses TrustToken DSTs and // the DLEQ batching primitive from // https://eprint.iacr.org/2020/072/20200324:214215. -// VOPRF only uses the |pub|' field of the TRUST_TOKEN_CLIENT_KEY and -// |xs|/|pubs| fields of the TRUST_TOKEN_ISSUER_KEY. +// VOPRF only uses the `pub`' field of the TRUST_TOKEN_CLIENT_KEY and +// `xs`/`pubs` fields of the TRUST_TOKEN_ISSUER_KEY. -// The following functions implement the corresponding |TRUST_TOKENS_METHOD| -// functions for |TRUST_TOKENS_experiment_v2|'s VOPRF construction which uses +// The following functions implement the corresponding `TRUST_TOKENS_METHOD` +// functions for `TRUST_TOKENS_experiment_v2`'s VOPRF construction which uses // P-384. int voprf_exp2_generate_key(CBB *out_private, CBB *out_public); int voprf_exp2_derive_key_from_secret(CBB *out_private, CBB *out_public, @@ -221,8 +221,8 @@ int voprf_exp2_read(const TRUST_TOKEN_ISSUER_KEY *key, size_t token_len, int include_message, const uint8_t *msg, size_t msg_len); -// The following functions implement the corresponding |TRUST_TOKENS_METHOD| -// functions for |TRUST_TOKENS_pst_v1|'s VOPRF construction which uses P-384. +// The following functions implement the corresponding `TRUST_TOKENS_METHOD` +// functions for `TRUST_TOKENS_pst_v1`'s VOPRF construction which uses P-384. int voprf_pst1_generate_key(CBB *out_private, CBB *out_public); int voprf_pst1_derive_key_from_secret(CBB *out_private, CBB *out_public, const uint8_t *secret, size_t secret_len); @@ -259,33 +259,33 @@ BSSL_NAMESPACE_END struct trust_token_method_st { // generate_key generates a fresh keypair and writes their serialized - // forms into |out_private| and |out_public|. It returns one on success and + // forms into `out_private` and `out_public`. It returns one on success and // zero on failure. int (*generate_key)(CBB *out_private, CBB *out_public); // derive_key_from_secret deterministically derives a keypair based on - // |secret| and writes their serialized forms into |out_private| and - // |out_public|. It returns one on success and zero on failure. + // `secret` and writes their serialized forms into `out_private` and + // `out_public`. It returns one on success and zero on failure. int (*derive_key_from_secret)(CBB *out_private, CBB *out_public, const uint8_t *secret, size_t secret_len); - // client_key_from_bytes decodes a client key from |in| and sets |key| + // client_key_from_bytes decodes a client key from `in` and sets `key` // to the resulting key. It returns one on success and zero // on failure. int (*client_key_from_bytes)(bssl::TRUST_TOKEN_CLIENT_KEY *key, const uint8_t *in, size_t len); - // issuer_key_from_bytes decodes a issuer key from |in| and sets |key| + // issuer_key_from_bytes decodes an issuer key from `in` and sets `key` // to the resulting key. It returns one on success and zero // on failure. int (*issuer_key_from_bytes)(bssl::TRUST_TOKEN_ISSUER_KEY *key, const uint8_t *in, size_t len); - // blind generates a new issuance request for |count| tokens. If - // |include_message| is set, then |msg| is used to derive the token nonces. On - // success, it returns a newly-allocated |STACK_OF(TRUST_TOKEN_PRETOKEN)| and - // writes a request to the issuer to |cbb|. On failure, it returns NULL. The - // |STACK_OF(TRUST_TOKEN_PRETOKEN)|s should be passed to |pmbtoken_unblind| + // blind generates a new issuance request for `count` tokens. If + // `include_message` is set, then `msg` is used to derive the token nonces. On + // success, it returns a newly-allocated `STACK_OF(TRUST_TOKEN_PRETOKEN)` and + // writes a request to the issuer to `cbb`. On failure, it returns NULL. The + // `STACK_OF(TRUST_TOKEN_PRETOKEN)`s should be passed to `pmbtoken_unblind` // when the server responds. // // This function implements the AT.Usr0 operation. @@ -293,9 +293,9 @@ struct trust_token_method_st { int include_message, const uint8_t *msg, size_t msg_len); - // sign parses a request for |num_requested| tokens from |cbs| and - // issues |num_to_issue| tokens with |key| and a private metadata value of - // |private_metadata|. It then writes the response to |cbb|. It returns one on + // sign parses a request for `num_requested` tokens from `cbs` and + // issues `num_to_issue` tokens with `key` and a private metadata value of + // `private_metadata`. It then writes the response to `cbb`. It returns one on // success and zero on failure. // // This function implements the AT.Sig operation. @@ -303,11 +303,11 @@ struct trust_token_method_st { size_t num_requested, size_t num_to_issue, uint8_t private_metadata); - // unblind processes an issuance response for |count| tokens from |cbs| - // and unblinds the signed tokens. |pretokens| are the pre-tokens returned - // from the corresponding |blind| call. On success, the function returns a - // newly-allocated |STACK_OF(TRUST_TOKEN)| containing the resulting tokens. - // Each token's serialization will have |key_id| prepended. Otherwise, it + // unblind processes an issuance response for `count` tokens from `cbs` + // and unblinds the signed tokens. `pretokens` are the pre-tokens returned + // from the corresponding `blind` call. On success, the function returns a + // newly-allocated `STACK_OF(TRUST_TOKEN)` containing the resulting tokens. + // Each token's serialization will have `key_id` prepended. Otherwise, it // returns NULL. // // This function implements the AT.Usr1 operation. @@ -316,11 +316,11 @@ struct trust_token_method_st { const bssl::StackOfTrustTokenPretoken *pretokens, CBS *cbs, size_t count, uint32_t key_id); - // read parses a token from |token| and verifies it using |key|. If - // |include_message| is set, then the nonce is derived from |msg| and the salt + // read parses a token from `token` and verifies it using `key`. If + // `include_message` is set, then the nonce is derived from `msg` and the salt // in the token. On success, it returns one and stores the nonce and private - // metadata bit in |out_nonce| and |*out_private_metadata|. Otherwise, it - // returns zero. Note that, unlike the output of |unblind|, |token| does not + // metadata bit in `out_nonce` and `*out_private_metadata`. Otherwise, it + // returns zero. Note that, unlike the output of `unblind`, `token` does not // have a four-byte key ID prepended. int (*read)(const bssl::TRUST_TOKEN_ISSUER_KEY *key, uint8_t out_nonce[TRUST_TOKEN_NONCE_SIZE], @@ -363,7 +363,7 @@ struct trust_token_client_st { // keys is the set of public keys that are supported by the client for // issuance/redemptions. - // TODO(crbug.com/42290036): Replace this and |num_keys| with an + // TODO(crbug.com/42290036): Replace this and `num_keys` with an // InplaceVector. struct bssl::trust_token_client_key_st keys[6]; diff --git a/third_party/boringssl/src/crypto/trust_token/pmbtoken.cc b/third_party/boringssl/src/crypto/trust_token/pmbtoken.cc index 95f1fd3d5..1f446dffa 100644 --- a/third_party/boringssl/src/crypto/trust_token/pmbtoken.cc +++ b/third_party/boringssl/src/crypto/trust_token/pmbtoken.cc @@ -337,7 +337,7 @@ static STACK_OF(TRUST_TOKEN_PRETOKEN) *pmbtoken_blind( } for (size_t i = 0; i < count; i++) { - // Insert |pretoken| into |pretokens| early to simplify error-handling. + // Insert `pretoken` into `pretokens` early to simplify error-handling. TRUST_TOKEN_PRETOKEN *pretoken = New(); if (pretoken == nullptr || !sk_TRUST_TOKEN_PRETOKEN_push(pretokens, pretoken)) { @@ -356,7 +356,7 @@ static STACK_OF(TRUST_TOKEN_PRETOKEN) *pmbtoken_blind( OPENSSL_memcpy(pretoken->t, pretoken->salt, TRUST_TOKEN_NONCE_SIZE); } - // We sample |pretoken->r| in Montgomery form to simplify inverting. + // We sample `pretoken->r` in Montgomery form to simplify inverting. if (!ec_random_nonzero_scalar(group, &pretoken->r, kDefaultAdditionalData)) { goto err; @@ -833,7 +833,7 @@ static int pmbtoken_sign(const PMBTOKEN_METHOD *method, uint8_t s[TRUST_TOKEN_NONCE_SIZE]; RAND_bytes(s, TRUST_TOKEN_NONCE_SIZE); - // The |jacobians| and |affines| contain Sp, Wp, and Wsp. + // The `jacobians` and `affines` contain Sp, Wp, and Wsp. EC_JACOBIAN jacobians[3]; EC_AFFINE affines[3]; if (!method->hash_s(group, &jacobians[0], &Tp_affine, s) || @@ -996,7 +996,7 @@ static STACK_OF(TRUST_TOKEN) *pmbtoken_unblind( goto err; } - // Serialize the token. Include |key_id| to avoid an extra copy in the layer + // Serialize the token. Include `key_id` to avoid an extra copy in the layer // above. CBB token_cbb; size_t point_len = ec_point_byte_len(group, POINT_CONVERSION_UNCOMPRESSED); @@ -1106,7 +1106,7 @@ static int pmbtoken_read(const PMBTOKEN_METHOD *method, } // We perform three multiplications with S and T. This is enough that it is - // worth using |ec_point_mul_scalar_precomp|. + // worth using `ec_point_mul_scalar_precomp`. EC_JACOBIAN S_jacobian; EC_PRECOMP S_precomp, T_precomp; ec_affine_to_jacobian(group, &S_jacobian, &S); @@ -1198,7 +1198,7 @@ static PMBTOKEN_METHOD pmbtoken_exp1_method; static CRYPTO_once_t pmbtoken_exp1_method_once = CRYPTO_ONCE_INIT; static void pmbtoken_exp1_init_method_impl() { - // This is the output of |ec_hash_to_scalar_p384_xmd_sha512_draft07| with DST + // This is the output of `ec_hash_to_scalar_p384_xmd_sha512_draft07` with DST // "PMBTokens Experiment V1 HashH" and message "generator". static const uint8_t kH[] = { 0x04, 0x82, 0xd5, 0x68, 0xf5, 0x39, 0xf6, 0x08, 0x19, 0xa1, 0x75, @@ -1373,7 +1373,7 @@ static PMBTOKEN_METHOD pmbtoken_exp2_method; static CRYPTO_once_t pmbtoken_exp2_method_once = CRYPTO_ONCE_INIT; static void pmbtoken_exp2_init_method_impl() { - // This is the output of |ec_hash_to_scalar_p384_xmd_sha512_draft07| with DST + // This is the output of `ec_hash_to_scalar_p384_xmd_sha512_draft07` with DST // "PMBTokens Experiment V2 HashH" and message "generator". static const uint8_t kH[] = { 0x04, 0xbc, 0x27, 0x24, 0x99, 0xfa, 0xc9, 0xa4, 0x74, 0x6f, 0xf9, @@ -1548,7 +1548,7 @@ static PMBTOKEN_METHOD pmbtoken_pst1_method; static CRYPTO_once_t pmbtoken_pst1_method_once = CRYPTO_ONCE_INIT; static void pmbtoken_pst1_init_method_impl() { - // This is the output of |ec_hash_to_scalar_p384_xmd_sha384| with DST + // This is the output of `ec_hash_to_scalar_p384_xmd_sha384` with DST // "PMBTokens PST V1 HashH" and message "generator". static const uint8_t kH[] = { 0x04, 0x4c, 0xfa, 0xd4, 0x33, 0x6d, 0x8c, 0x4e, 0x18, 0xce, 0x1a, diff --git a/third_party/boringssl/src/crypto/trust_token/trust_token.cc b/third_party/boringssl/src/crypto/trust_token/trust_token.cc index d0e5c2430..b295a6b43 100644 --- a/third_party/boringssl/src/crypto/trust_token/trust_token.cc +++ b/third_party/boringssl/src/crypto/trust_token/trust_token.cc @@ -279,13 +279,13 @@ int TRUST_TOKEN_CLIENT_add_key(TRUST_TOKEN_CLIENT *ctx, size_t *out_key_index, return 1; } -int TRUST_TOKEN_CLIENT_set_srr_key(TRUST_TOKEN_CLIENT *ctx, EVP_PKEY *key) { +int TRUST_TOKEN_CLIENT_set_srr_key(TRUST_TOKEN_CLIENT *ctx, + const EVP_PKEY *key) { if (!ctx->method->has_srr) { return 1; } EVP_PKEY_free(ctx->srr_key); - EVP_PKEY_up_ref(key); - ctx->srr_key = key; + ctx->srr_key = EVP_PKEY_dup_ref(key); return 1; } @@ -515,10 +515,10 @@ int TRUST_TOKEN_ISSUER_add_key(TRUST_TOKEN_ISSUER *ctx, const uint8_t *key, return 1; } -int TRUST_TOKEN_ISSUER_set_srr_key(TRUST_TOKEN_ISSUER *ctx, EVP_PKEY *key) { +int TRUST_TOKEN_ISSUER_set_srr_key(TRUST_TOKEN_ISSUER *ctx, + const EVP_PKEY *key) { EVP_PKEY_free(ctx->srr_key); - EVP_PKEY_up_ref(key); - ctx->srr_key = key; + ctx->srr_key = EVP_PKEY_dup_ref(key); return 1; } diff --git a/third_party/boringssl/src/crypto/trust_token/voprf.cc b/third_party/boringssl/src/crypto/trust_token/voprf.cc index 1a302b23f..4dcb98960 100644 --- a/third_party/boringssl/src/crypto/trust_token/voprf.cc +++ b/third_party/boringssl/src/crypto/trust_token/voprf.cc @@ -211,7 +211,7 @@ static STACK_OF(TRUST_TOKEN_PRETOKEN) *voprf_blind(const VOPRF_METHOD *method, } for (size_t i = 0; i < count; i++) { - // Insert |pretoken| into |pretokens| early to simplify error-handling. + // Insert `pretoken` into `pretokens` early to simplify error-handling. TRUST_TOKEN_PRETOKEN *pretoken = New(); if (pretoken == nullptr || !sk_TRUST_TOKEN_PRETOKEN_push(pretokens, pretoken)) { @@ -617,7 +617,7 @@ static STACK_OF(TRUST_TOKEN) *voprf_unblind_tt( goto err; } - // Serialize the token. Include |key_id| to avoid an extra copy in the layer + // Serialize the token. Include `key_id` to avoid an extra copy in the layer // above. CBB token_cbb; size_t point_len = ec_point_byte_len(group, POINT_CONVERSION_UNCOMPRESSED); @@ -1009,7 +1009,7 @@ static STACK_OF(TRUST_TOKEN) *voprf_unblind( goto err; } - // Serialize the token. Include |key_id| to avoid an extra copy in the layer + // Serialize the token. Include `key_id` to avoid an extra copy in the layer // above. CBB token_cbb; size_t point_len = ec_point_byte_len(group, POINT_CONVERSION_UNCOMPRESSED); diff --git a/third_party/boringssl/src/crypto/x509/a_sign.cc b/third_party/boringssl/src/crypto/x509/a_sign.cc index a430bff34..f09e80a8f 100644 --- a/third_party/boringssl/src/crypto/x509/a_sign.cc +++ b/third_party/boringssl/src/crypto/x509/a_sign.cc @@ -31,7 +31,7 @@ using namespace bssl; int ASN1_item_sign(const ASN1_ITEM *it, X509_ALGOR *algor1, X509_ALGOR *algor2, - ASN1_BIT_STRING *signature, void *asn, EVP_PKEY *pkey, + ASN1_BIT_STRING *signature, void *asn, const EVP_PKEY *pkey, const EVP_MD *type) { if (signature->type != V_ASN1_BIT_STRING) { OPENSSL_PUT_ERROR(ASN1, ASN1_R_WRONG_TYPE); @@ -47,12 +47,12 @@ int ASN1_item_sign(const ASN1_ITEM *it, X509_ALGOR *algor1, X509_ALGOR *algor2, int ASN1_item_sign_ctx(const ASN1_ITEM *it, X509_ALGOR *algor1, X509_ALGOR *algor2, ASN1_BIT_STRING *signature, void *asn, EVP_MD_CTX *ctx) { - // Historically, this function called |EVP_MD_CTX_cleanup| on return. Some + // Historically, this function called `EVP_MD_CTX_cleanup` on return. Some // callers rely on this to avoid memory leaks. Cleanup cleanup = [&] { EVP_MD_CTX_cleanup(ctx); }; // Write out the requested copies of the AlgorithmIdentifier. This may modify - // |asn|, so we must do it first. + // `asn`, so we must do it first. if ((algor1 != nullptr && !x509_digest_sign_algorithm(ctx, algor1)) || (algor2 != nullptr && !x509_digest_sign_algorithm(ctx, algor2))) { return 0; @@ -78,7 +78,7 @@ int bssl::x509_sign_to_bit_string(EVP_MD_CTX *ctx, ASN1_BIT_STRING *out, EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx->pctx); size_t sig_len = EVP_PKEY_size(pkey); if (sig_len > INT_MAX) { - // Ensure the signature will fit in |out|. + // Ensure the signature will fit in `out`. OPENSSL_PUT_ERROR(X509, ERR_R_OVERFLOW); return 0; } diff --git a/third_party/boringssl/src/crypto/x509/a_verify.cc b/third_party/boringssl/src/crypto/x509/a_verify.cc index 052c52136..e3ae122ae 100644 --- a/third_party/boringssl/src/crypto/x509/a_verify.cc +++ b/third_party/boringssl/src/crypto/x509/a_verify.cc @@ -33,24 +33,32 @@ using namespace bssl; int bssl::x509_verify_signature(const X509_ALGOR *sigalg, const ASN1_BIT_STRING *signature, - Span in, EVP_PKEY *pkey) { - if (!pkey) { - OPENSSL_PUT_ERROR(X509, ERR_R_PASSED_NULL_PARAMETER); - return 0; - } - + Span in, const EVP_PKEY *pkey) { if (signature->type == V_ASN1_BIT_STRING && ASN1_BIT_STRING_unused_bits(signature) != 0) { OPENSSL_PUT_ERROR(X509, X509_R_INVALID_BIT_STRING_BITS_LEFT); return 0; } + Span signature_bytes(ASN1_STRING_get0_data(signature), + ASN1_STRING_length(signature)); + return x509_verify_signature_bytes(sigalg, signature_bytes, in, pkey); +} + +int bssl::x509_verify_signature_bytes(const X509_ALGOR *sigalg, + Span signature, + Span in, + const EVP_PKEY *pkey) { + if (!pkey) { + OPENSSL_PUT_ERROR(X509, ERR_R_PASSED_NULL_PARAMETER); + return 0; + } ScopedEVP_MD_CTX ctx; if (!x509_digest_verify_init(ctx.get(), sigalg, pkey)) { return 0; } - if (!EVP_DigestVerify(ctx.get(), ASN1_STRING_get0_data(signature), - ASN1_STRING_length(signature), in.data(), in.size())) { + if (!EVP_DigestVerify(ctx.get(), signature.data(), signature.size(), + in.data(), in.size())) { OPENSSL_PUT_ERROR(X509, ERR_R_EVP_LIB); return 0; } @@ -59,7 +67,7 @@ int bssl::x509_verify_signature(const X509_ALGOR *sigalg, int ASN1_item_verify(const ASN1_ITEM *it, const X509_ALGOR *sigalg, const ASN1_BIT_STRING *signature, void *asn, - EVP_PKEY *pkey) { + const EVP_PKEY *pkey) { uint8_t *in = nullptr; int in_len = ASN1_item_i2d(reinterpret_cast(asn), &in, it); if (in_len < 0) { diff --git a/third_party/boringssl/src/crypto/x509/algorithm.cc b/third_party/boringssl/src/crypto/x509/algorithm.cc index 4330fb4d1..1e5afbcbd 100644 --- a/third_party/boringssl/src/crypto/x509/algorithm.cc +++ b/third_party/boringssl/src/crypto/x509/algorithm.cc @@ -27,11 +27,13 @@ using namespace bssl; // TODO(crbug.com/42290422): Rewrite this logic to recognize signature // algorithms without pulling in the OID table. We can enumerate every supported -// signature algorithm into a small enum and convert them to/from |EVP_PKEY_CTX| -// and |X509_ALGOR|. +// signature algorithm into a small enum and convert them to/from `EVP_PKEY_CTX` +// and `X509_ALGOR`. + +namespace { // Restrict the digests that are allowed in X509 certificates -static int x509_digest_nid_ok(const int digest_nid) { +int x509_digest_nid_ok(const int digest_nid) { switch (digest_nid) { case NID_md4: case NID_md5: @@ -40,6 +42,8 @@ static int x509_digest_nid_ok(const int digest_nid) { return 1; } +} // namespace + int bssl::x509_digest_sign_algorithm(EVP_MD_CTX *ctx, X509_ALGOR *algor) { EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx->pctx); if (pkey == nullptr) { @@ -103,7 +107,7 @@ int bssl::x509_digest_sign_algorithm(EVP_MD_CTX *ctx, X509_ALGOR *algor) { } int bssl::x509_digest_verify_init(EVP_MD_CTX *ctx, const X509_ALGOR *sigalg, - EVP_PKEY *pkey) { + const EVP_PKEY *pkey) { // Convert the signature OID into digest and public key OIDs. int sigalg_nid = OBJ_obj2nid(sigalg->algorithm); int digest_nid, pkey_nid; diff --git a/third_party/boringssl/src/crypto/x509/asn1_gen.cc b/third_party/boringssl/src/crypto/x509/asn1_gen.cc index 27b844658..a8adc5059 100644 --- a/third_party/boringssl/src/crypto/x509/asn1_gen.cc +++ b/third_party/boringssl/src/crypto/x509/asn1_gen.cc @@ -48,45 +48,25 @@ using namespace bssl; #define ASN1_GEN_FORMAT_HEX 3 #define ASN1_GEN_FORMAT_BITLIST 4 -// generate_v3 converts |str| into an ASN.1 structure and writes the result to -// |cbb|. It returns one on success and zero on error. |depth| bounds recursion, -// and |format| specifies the current format modifier. +namespace { + +// generate_v3 converts `str` into an ASN.1 structure and writes the result to +// `cbb`. It returns one on success and zero on error. `depth` bounds recursion, +// and `format` specifies the current format modifier. // -// If |tag| is non-zero, the structure is implicitly tagged with |tag|. |tag| +// If `tag` is non-zero, the structure is implicitly tagged with `tag`. `tag` // must not have the constructed bit set. -static int generate_v3(CBB *cbb, const char *str, const X509V3_CTX *cnf, - CBS_ASN1_TAG tag, int format, int depth); - -static int bitstr_cb(const char *elem, size_t len, void *bitstr); - -ASN1_TYPE *bssl::ASN1_generate_v3(const char *str, const X509V3_CTX *cnf) { - ScopedCBB cbb; - if (!CBB_init(cbb.get(), 0) || // - !generate_v3(cbb.get(), str, cnf, /*tag=*/0, ASN1_GEN_FORMAT_ASCII, - /*depth=*/0)) { - return nullptr; - } - - // While not strictly necessary to avoid a DoS (we rely on any super-linear - // checks being performed internally), cap the overall output to - // |ASN1_GEN_MAX_OUTPUT| so the externally-visible behavior is consistent. - if (CBB_len(cbb.get()) > ASN1_GEN_MAX_OUTPUT) { - OPENSSL_PUT_ERROR(ASN1, ASN1_R_TOO_LONG); - return nullptr; - } - - const uint8_t *der = CBB_data(cbb.get()); - return d2i_ASN1_TYPE(nullptr, &der, CBB_len(cbb.get())); -} +int generate_v3(CBB *cbb, const char *str, const X509V3_CTX *cnf, + CBS_ASN1_TAG tag, int format, int depth); -static int cbs_str_equal(const CBS *cbs, const char *str) { +int cbs_str_equal(const CBS *cbs, const char *str) { return CBS_len(cbs) == strlen(str) && OPENSSL_memcmp(CBS_data(cbs), str, strlen(str)) == 0; } -// parse_tag decodes a tag specifier in |cbs|. It returns the tag on success or +// parse_tag decodes a tag specifier in `cbs`. It returns the tag on success or // zero on error. -static CBS_ASN1_TAG parse_tag(const CBS *cbs) { +CBS_ASN1_TAG parse_tag(const CBS *cbs) { CBS copy = *cbs; uint64_t num; if (!CBS_get_u64_decimal(©, &num) || num > CBS_ASN1_TAG_NUMBER_MASK) { @@ -132,9 +112,8 @@ static CBS_ASN1_TAG parse_tag(const CBS *cbs) { return tag_class | (CBS_ASN1_TAG)num; } -static int generate_wrapped(CBB *cbb, const char *str, const X509V3_CTX *cnf, - CBS_ASN1_TAG tag, int padding, int format, - int depth) { +int generate_wrapped(CBB *cbb, const char *str, const X509V3_CTX *cnf, + CBS_ASN1_TAG tag, int padding, int format, int depth) { CBB child; return CBB_add_asn1(cbb, &child, tag) && (!padding || CBB_add_u8(&child, 0)) && @@ -142,8 +121,33 @@ static int generate_wrapped(CBB *cbb, const char *str, const X509V3_CTX *cnf, CBB_flush(cbb); } -static int generate_v3(CBB *cbb, const char *str, const X509V3_CTX *cnf, - CBS_ASN1_TAG tag, int format, int depth) { +int bitstr_cb(const char *elem, size_t len, void *bitstr) { + CBS cbs; + CBS_init(&cbs, (const uint8_t *)elem, len); + uint64_t bitnum; + if (!CBS_get_u64_decimal(&cbs, &bitnum) || CBS_len(&cbs) != 0 || + // Cap the highest allowed bit so this mechanism cannot be used to create + // extremely large allocations with short inputs. The highest named bit in + // RFC 5280 is 8, so 256 should give comfortable margin but still only + // allow a 32-byte allocation. + // + // We do not consider this function to be safe with untrusted inputs (even + // without bugs, it is prone to string injection vulnerabilities), so DoS + // is not truly a concern, but the limit is necessary to keep fuzzing + // effective. + bitnum > 256) { + OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_NUMBER); + return 0; + } + if (!ASN1_BIT_STRING_set_bit(reinterpret_cast(bitstr), + (int)bitnum, 1)) { + return 0; + } + return 1; +} + +int generate_v3(CBB *cbb, const char *str, const X509V3_CTX *cnf, + CBS_ASN1_TAG tag, int format, int depth) { assert((tag & CBS_ASN1_CONSTRUCTED) == 0); if (depth > ASN1_GEN_MAX_DEPTH) { OPENSSL_PUT_ERROR(ASN1, ASN1_R_ILLEGAL_NESTED_TAGGING); @@ -151,7 +155,7 @@ static int generate_v3(CBB *cbb, const char *str, const X509V3_CTX *cnf, } // Process modifiers. This function uses a mix of NUL-terminated strings and - // |CBS|. Several functions only work with NUL-terminated strings, so we need + // `CBS`. Several functions only work with NUL-terminated strings, so we need // to keep track of when a slice spans the whole buffer. for (;;) { // Skip whitespace. @@ -241,7 +245,7 @@ static int generate_v3(CBB *cbb, const char *str, const X509V3_CTX *cnf, tag = tag == 0 ? CBS_ASN1_SET : (tag | CBS_ASN1_CONSTRUCTED); return generate_wrapped(cbb, str, cnf, tag, /*padding=*/0, format, depth); } else { - // If this was not a recognized modifier, rewind |str| to before splitting + // If this was not a recognized modifier, rewind `str` to before splitting // on the comma. The type itself consumes all remaining input. str = str_old; break; @@ -412,9 +416,9 @@ static int generate_v3(CBB *cbb, const char *str, const X509V3_CTX *cnf, return 0; } - // |maxsize| is measured in code points, rather than bytes, but pass it in + // `maxsize` is measured in code points, rather than bytes, but pass it in // as a loose cap so fuzzers can exit from excessively long inputs - // earlier. This limit is not load-bearing because |ASN1_mbstring_ncopy|'s + // earlier. This limit is not load-bearing because `ASN1_mbstring_ncopy`'s // output is already linear in the input. ASN1_STRING *obj = nullptr; if (ASN1_mbstring_ncopy(&obj, (const uint8_t *)value, -1, encoding, @@ -493,8 +497,8 @@ static int generate_v3(CBB *cbb, const char *str, const X509V3_CTX *cnf, ASN1_GEN_FORMAT_ASCII, depth + 1)) { return 0; } - // This recursive call, by referencing |section|, is the one place - // where |generate_v3|'s output can be super-linear in the input. + // This recursive call, by referencing `section`, is the one place + // where `generate_v3`'s output can be super-linear in the input. // Check bounds here. if (CBB_len(&child) > ASN1_GEN_MAX_OUTPUT) { OPENSSL_PUT_ERROR(ASN1, ASN1_R_TOO_LONG); @@ -514,27 +518,24 @@ static int generate_v3(CBB *cbb, const char *str, const X509V3_CTX *cnf, } } -static int bitstr_cb(const char *elem, size_t len, void *bitstr) { - CBS cbs; - CBS_init(&cbs, (const uint8_t *)elem, len); - uint64_t bitnum; - if (!CBS_get_u64_decimal(&cbs, &bitnum) || CBS_len(&cbs) != 0 || - // Cap the highest allowed bit so this mechanism cannot be used to create - // extremely large allocations with short inputs. The highest named bit in - // RFC 5280 is 8, so 256 should give comfortable margin but still only - // allow a 32-byte allocation. - // - // We do not consider this function to be safe with untrusted inputs (even - // without bugs, it is prone to string injection vulnerabilities), so DoS - // is not truly a concern, but the limit is necessary to keep fuzzing - // effective. - bitnum > 256) { - OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_NUMBER); - return 0; +} // namespace + +ASN1_TYPE *bssl::ASN1_generate_v3(const char *str, const X509V3_CTX *cnf) { + ScopedCBB cbb; + if (!CBB_init(cbb.get(), 0) || // + !generate_v3(cbb.get(), str, cnf, /*tag=*/0, ASN1_GEN_FORMAT_ASCII, + /*depth=*/0)) { + return nullptr; } - if (!ASN1_BIT_STRING_set_bit(reinterpret_cast(bitstr), - (int)bitnum, 1)) { - return 0; + + // While not strictly necessary to avoid a DoS (we rely on any super-linear + // checks being performed internally), cap the overall output to + // `ASN1_GEN_MAX_OUTPUT` so the externally-visible behavior is consistent. + if (CBB_len(cbb.get()) > ASN1_GEN_MAX_OUTPUT) { + OPENSSL_PUT_ERROR(ASN1, ASN1_R_TOO_LONG); + return nullptr; } - return 1; + + const uint8_t *der = CBB_data(cbb.get()); + return d2i_ASN1_TYPE(nullptr, &der, CBB_len(cbb.get())); } diff --git a/third_party/boringssl/src/crypto/x509/by_dir.cc b/third_party/boringssl/src/crypto/x509/by_dir.cc index 545ffd95e..eeb7aa7ac 100644 --- a/third_party/boringssl/src/crypto/x509/by_dir.cc +++ b/third_party/boringssl/src/crypto/x509/by_dir.cc @@ -15,6 +15,9 @@ #include #include +#include +#include + #include #include #include @@ -25,113 +28,87 @@ #include "internal.h" -using namespace bssl; - BSSL_NAMESPACE_BEGIN -typedef struct lookup_dir_hashes_st { - uint32_t hash; - int suffix; -} BY_DIR_HASH; +namespace { -typedef struct lookup_dir_entry_st { - Mutex lock; - char *dir; - int dir_type; - STACK_OF(BY_DIR_HASH) *hashes; -} BY_DIR_ENTRY; +// A ByDirEntry tracks state for a single directory, notably the starting suffix +// for CRL lookups. +class ByDirEntry { + public: + static constexpr bool kAllowUniquePtr = true; -typedef struct lookup_dir_st { - STACK_OF(BY_DIR_ENTRY) *dirs; -} BY_DIR; + ByDirEntry() = default; -DEFINE_NAMESPACED_STACK_OF(BY_DIR_HASH) -DEFINE_NAMESPACED_STACK_OF(BY_DIR_ENTRY) - -BSSL_NAMESPACE_END - -static int dir_ctrl(X509_LOOKUP *ctx, int cmd, const char *argp, long argl, - char **ret); -static int new_dir(X509_LOOKUP *lu); -static void free_dir(X509_LOOKUP *lu); -static int add_cert_dir(BY_DIR *ctx, const char *dir, int type); -static int get_cert_by_subject(X509_LOOKUP *xl, int type, const X509_NAME *name, - X509_OBJECT *ret); -static const X509_LOOKUP_METHOD x509_dir_lookup = { - new_dir, // new - free_dir, // free - dir_ctrl, // ctrl - get_cert_by_subject, // get_by_subject -}; - -const X509_LOOKUP_METHOD *X509_LOOKUP_hash_dir() { return &x509_dir_lookup; } + static UniquePtr Create(int dir_type, std::string_view dir) { + auto ret = MakeUnique(); + ret->dir_type_ = dir_type; + ret->dir_.reset(OPENSSL_strndup(dir.data(), dir.size())); + if (ret->dir_ == nullptr) { + return nullptr; + } + return ret; + } -static int dir_ctrl(X509_LOOKUP *ctx, int cmd, const char *argp, long argl, - char **retp) { - int ret = 0; - char *dir = nullptr; + int dir_type() const { return dir_type_; } + const char *dir() const { return dir_.get(); } - BY_DIR *ld = reinterpret_cast(ctx->method_data); + int GetCRLSuffix(uint32_t hash) const { + MutexReadLock lock(&lock_); + auto it = + std::lower_bound(crl_suffixes_.begin(), crl_suffixes_.end(), hash); + if (it == crl_suffixes_.end() || it->hash != hash) { + return 0; + } + return it->suffix; + } - switch (cmd) { - case X509_L_ADD_DIR: - if (argl == X509_FILETYPE_DEFAULT) { - dir = (char *)getenv(X509_get_default_cert_dir_env()); - if (dir) { - ret = add_cert_dir(ld, dir, X509_FILETYPE_PEM); - } else { - ret = - add_cert_dir(ld, X509_get_default_cert_dir(), X509_FILETYPE_PEM); - } - if (!ret) { - OPENSSL_PUT_ERROR(X509, X509_R_LOADING_CERT_DIR); - } - } else { - ret = add_cert_dir(ld, argp, (int)argl); - } - break; + bool UpdateCRLSuffix(uint32_t hash, int suffix) { + MutexWriteLock lock(&lock_); + auto it = + std::lower_bound(crl_suffixes_.begin(), crl_suffixes_.end(), hash); + if (it != crl_suffixes_.end() && it->hash == hash) { + it->suffix = std::max(suffix, it->suffix); + return true; + } + if (!crl_suffixes_.Push(CRLSuffix{hash, suffix})) { + return false; + } + std::sort(crl_suffixes_.begin(), crl_suffixes_.end()); + return true; } - return ret; -} -static int new_dir(X509_LOOKUP *lu) { - BY_DIR *a; + private: + struct CRLSuffix { + uint32_t hash; + int suffix; + bool operator<(uint32_t h) const { return hash < h; } + bool operator<(const CRLSuffix &other) const { return hash < other.hash; } + }; + + UniquePtr dir_; + int dir_type_ = 0; + mutable Mutex lock_; + // crl_suffixes_ is kept sorted. + // TODO(davidben): This should be a hash table. Insertions are O(N log N). + Vector crl_suffixes_; +}; + +struct ByDir { + Vector> dirs; +}; - if ((a = New()) == nullptr) { +int new_dir(X509_LOOKUP *lu) { + ByDir *a = New(); + if (a == nullptr) { return 0; } - a->dirs = nullptr; lu->method_data = a; return 1; } -static void by_dir_hash_free(BY_DIR_HASH *hash) { Delete(hash); } - -static int by_dir_hash_cmp(const BY_DIR_HASH *const *a, - const BY_DIR_HASH *const *b) { - if ((*a)->hash > (*b)->hash) { - return 1; - } - if ((*a)->hash < (*b)->hash) { - return -1; - } - return 0; -} - -static void by_dir_entry_free(BY_DIR_ENTRY *ent) { - if (ent != nullptr) { - Delete(ent->dir); - sk_BY_DIR_HASH_pop_free(ent->hashes, by_dir_hash_free); - Delete(ent); - } -} - -static void free_dir(X509_LOOKUP *lu) { - BY_DIR *a = reinterpret_cast(lu->method_data); - if (a != nullptr) { - sk_BY_DIR_ENTRY_pop_free(a->dirs, by_dir_entry_free); - Delete(a); - } +void free_dir(X509_LOOKUP *lu) { + Delete(reinterpret_cast(lu->method_data)); } #if defined(OPENSSL_WINDOWS) @@ -140,78 +117,72 @@ static void free_dir(X509_LOOKUP *lu) { #define DIR_HASH_SEPARATOR ':' #endif -static int add_cert_dir(BY_DIR *ctx, const char *dir, int type) { - size_t j, len; - const char *s, *ss, *p; - - if (dir == nullptr || !*dir) { +int add_cert_dir(ByDir *ctx, const char *inp, int type) { + if (inp == nullptr || !*inp) { OPENSSL_PUT_ERROR(X509, X509_R_INVALID_DIRECTORY); return 0; } - s = dir; - p = s; + std::string_view rest = inp; do { - if (*p == DIR_HASH_SEPARATOR || *p == '\0') { - BY_DIR_ENTRY *ent; - ss = s; - s = p + 1; - len = p - ss; - if (len == 0) { - continue; - } - for (j = 0; j < sk_BY_DIR_ENTRY_num(ctx->dirs); j++) { - ent = sk_BY_DIR_ENTRY_value(ctx->dirs, j); - if (strlen(ent->dir) == len && strncmp(ent->dir, ss, len) == 0) { - break; - } - } - if (j < sk_BY_DIR_ENTRY_num(ctx->dirs)) { - continue; - } - if (ctx->dirs == nullptr) { - ctx->dirs = sk_BY_DIR_ENTRY_new_null(); - if (!ctx->dirs) { - return 0; - } - } - ent = New(); - if (!ent) { - return 0; - } - ent->dir_type = type; - ent->hashes = sk_BY_DIR_HASH_new(by_dir_hash_cmp); - ent->dir = OPENSSL_strndup(ss, len); - if (ent->dir == nullptr || ent->hashes == nullptr || - !sk_BY_DIR_ENTRY_push(ctx->dirs, ent)) { - by_dir_entry_free(ent); - return 0; - } + // Split by `DIR_HASH_SEPARATOR`. + size_t sep = rest.find(DIR_HASH_SEPARATOR); + std::string_view dir; + if (sep == std::string_view::npos) { + dir = rest; + rest = std::string_view(); + } else { + dir = rest.substr(0, sep); + rest = rest.substr(sep + 1); } - } while (*p++ != '\0'); + if (dir.empty()) { + continue; + } + // Ignore duplicates. + if (std::any_of(ctx->dirs.begin(), ctx->dirs.end(), + [&](const auto &ent) { return ent->dir() == dir; })) { + continue; + } + auto ent = ByDirEntry::Create(type, dir); + if (ent == nullptr || !ctx->dirs.Push(std::move(ent))) { + return 0; + } + } while (!rest.empty()); return 1; } -static int get_cert_by_subject(X509_LOOKUP *xl, int type, const X509_NAME *name, - X509_OBJECT *ret) { - UniquePtr lookup_cert; - UniquePtr lookup_crl; - int ok = 0; - size_t i; - int k; - uint32_t h; - uint32_t hash_array[2]; - int hash_index; - char *b = nullptr; - X509_OBJECT stmp, *tmp; - const char *postfix = ""; +int dir_ctrl(X509_LOOKUP *ctx, int cmd, const char *argp, long argl, + char **retp) { + ByDir *ld = reinterpret_cast(ctx->method_data); + switch (cmd) { + case X509_L_ADD_DIR: + if (argl == X509_FILETYPE_DEFAULT) { + const char *dir = getenv(X509_get_default_cert_dir_env()); + if (!add_cert_dir(ld, dir ? dir : X509_get_default_cert_dir(), + X509_FILETYPE_PEM)) { + OPENSSL_PUT_ERROR(X509, X509_R_LOADING_CERT_DIR); + return 0; + } + return 1; + } + return add_cert_dir(ld, argp, (int)argl); + } + return 0; +} +int get_cert_by_subject(X509_LOOKUP *xl, int type, const X509_NAME *name, + X509_OBJECT *ret) { if (name == nullptr) { return 0; } + // Set up an `X509_OBJECT` to compare against. + UniquePtr lookup_cert; + UniquePtr lookup_crl; + X509_OBJECT stmp; + const char *postfix = ""; stmp.type = type; - BY_DIR *ctx = reinterpret_cast(xl->method_data); + ByDir *ctx = reinterpret_cast(xl->method_data); if (type == X509_LU_X509) { lookup_cert.reset(X509_new()); if (lookup_cert == nullptr || @@ -230,123 +201,98 @@ static int get_cert_by_subject(X509_LOOKUP *xl, int type, const X509_NAME *name, postfix = "r"; } else { OPENSSL_PUT_ERROR(X509, X509_R_WRONG_LOOKUP_TYPE); - goto finish; + return 0; } - hash_array[0] = X509_NAME_hash(name); - hash_array[1] = X509_NAME_hash_old(name); - for (hash_index = 0; hash_index < 2; ++hash_index) { - h = hash_array[hash_index]; - for (i = 0; i < sk_BY_DIR_ENTRY_num(ctx->dirs); i++) { - BY_DIR_ENTRY *ent; - size_t idx; - BY_DIR_HASH htmp, *hent; - ent = sk_BY_DIR_ENTRY_value(ctx->dirs, i); - if (type == X509_LU_CRL && ent->hashes) { - htmp.hash = h; - MutexReadLock lock(&ent->lock); - if (sk_BY_DIR_HASH_find(ent->hashes, &idx, &htmp)) { - hent = sk_BY_DIR_HASH_value(ent->hashes, idx); - k = hent->suffix; - } else { - hent = nullptr; - k = 0; - } - } else { - k = 0; - hent = nullptr; + // Try both new and old hashes. + const uint32_t hashes[] = {X509_NAME_hash(name), X509_NAME_hash_old(name)}; + for (uint32_t hash : hashes) { + for (UniquePtr &ent : ctx->dirs) { + // If a CRL, start from the previously saved suffix. Updated CRLs are + // expected to be added until new filenames. + // TODO(crbug.com/42290566): Is this what we want? + int suffix = 0; + if (type == X509_LU_CRL) { + suffix = ent->GetCRLSuffix(hash); } + + // The directory format handles hash collections by incrementing a suffix + // on the file name. Load every suffix into the cache. for (;;) { - OPENSSL_free(b); - if (OPENSSL_asprintf(&b, "%s/%08" PRIx32 ".%s%d", ent->dir, h, postfix, - k) == -1) { + char *path = nullptr; + if (OPENSSL_asprintf(&path, "%s/%08" PRIx32 ".%s%d", ent->dir(), hash, + postfix, suffix) == -1) { OPENSSL_PUT_ERROR(X509, ERR_R_BUF_LIB); - b = nullptr; - goto finish; + return 0; } + UniquePtr free_path(path); if (type == X509_LU_X509) { - if ((X509_load_cert_file(xl, b, ent->dir_type)) == 0) { - // Don't expose the lower level error, All of these boil - // down to "we could not find a CA". + if ((X509_load_cert_file(xl, path, ent->dir_type())) == 0) { + // Don't expose the lower level error, All of these boil down to "we + // could not find a CA". ERR_clear_error(); break; } } else if (type == X509_LU_CRL) { - if ((X509_load_crl_file(xl, b, ent->dir_type)) == 0) { - // Don't expose the lower level error, All of these boil - // down to "we could not find a CRL". + if ((X509_load_crl_file(xl, path, ent->dir_type())) == 0) { + // Don't expose the lower level error, All of these boil down to "we + // could not find a CRL". ERR_clear_error(); break; } } - // The lack of a CA or CRL will be caught higher up - k++; + // The lack of a CA or CRL will be caught higher up. + suffix++; } - // we have added it to the cache so now pull it out again + // We have added it to the cache so now pull it out again. auto *store_impl = FromOpaque(xl->store_ctx); store_impl->objs_lock.LockWrite(); - tmp = nullptr; + const X509_OBJECT *found = nullptr; sk_X509_OBJECT_sort(store_impl->objs.get()); + size_t idx; if (sk_X509_OBJECT_find(store_impl->objs.get(), &idx, &stmp)) { - tmp = sk_X509_OBJECT_value(store_impl->objs.get(), idx); + found = sk_X509_OBJECT_value(store_impl->objs.get(), idx); } store_impl->objs_lock.UnlockWrite(); - // If a CRL, update the last file suffix added for this - - if (type == X509_LU_CRL) { - ent->lock.LockWrite(); - // Look for entry again in case another thread added an entry - // first. - if (!hent) { - htmp.hash = h; - sk_BY_DIR_HASH_sort(ent->hashes); - if (sk_BY_DIR_HASH_find(ent->hashes, &idx, &htmp)) { - hent = sk_BY_DIR_HASH_value(ent->hashes, idx); - } - } - if (!hent) { - hent = New(); - if (hent == nullptr) { - ent->lock.UnlockWrite(); - ok = 0; - goto finish; - } - hent->hash = h; - hent->suffix = k; - if (!sk_BY_DIR_HASH_push(ent->hashes, hent)) { - ent->lock.UnlockWrite(); - Delete(hent); - ok = 0; - goto finish; - } - sk_BY_DIR_HASH_sort(ent->hashes); - } else if (hent->suffix < k) { - hent->suffix = k; - } - - ent->lock.UnlockWrite(); + // If a CRL, store the last suffix we saw, to skip already loaded files + // next time. + // TODO(crbug.com/42290566): Is this what we want? + if (type == X509_LU_CRL && !ent->UpdateCRLSuffix(hash, suffix)) { + return 0; } - if (tmp != nullptr) { - ok = 1; - ret->type = tmp->type; - OPENSSL_memcpy(&ret->data, &tmp->data, sizeof(ret->data)); - - // Clear any errors that might have been raised processing empty - // or malformed files. + if (found != nullptr) { + // Clear any errors that might have been raised processing empty or + // malformed files. ERR_clear_error(); - // If we were going to up the reference count, we would need - // to do it on a perl 'type' basis - goto finish; + // TODO(crbug.com/42290561): This should manage the reference counts + // correctly but does not. + ret->type = found->type; + OPENSSL_memcpy(&ret->data, &found->data, sizeof(ret->data)); + return 1; } } } -finish: - OPENSSL_free(b); - return ok; + + return 0; +} + +const X509_LOOKUP_METHOD x509_dir_lookup = { + new_dir, // new + free_dir, // free + dir_ctrl, // ctrl + get_cert_by_subject, // get_by_subject +}; + +} // namespace + +BSSL_NAMESPACE_END + +const X509_LOOKUP_METHOD *X509_LOOKUP_hash_dir() { + return &bssl::x509_dir_lookup; } int X509_LOOKUP_add_dir(X509_LOOKUP *lookup, const char *name, int type) { diff --git a/third_party/boringssl/src/crypto/x509/by_file.cc b/third_party/boringssl/src/crypto/x509/by_file.cc index f7d124a79..39de09682 100644 --- a/third_party/boringssl/src/crypto/x509/by_file.cc +++ b/third_party/boringssl/src/crypto/x509/by_file.cc @@ -20,19 +20,10 @@ #include "internal.h" -static int by_file_ctrl(X509_LOOKUP *ctx, int cmd, const char *argc, long argl, - char **ret); -static const X509_LOOKUP_METHOD x509_file_lookup = { - nullptr, // new - nullptr, // free - by_file_ctrl, // ctrl - nullptr, // get_by_subject -}; - -const X509_LOOKUP_METHOD *X509_LOOKUP_file() { return &x509_file_lookup; } +namespace { -static int by_file_ctrl(X509_LOOKUP *ctx, int cmd, const char *argp, long argl, - char **ret) { +int by_file_ctrl(X509_LOOKUP *ctx, int cmd, const char *argp, long argl, + char **ret) { if (cmd != X509_L_FILE_LOAD) { return 0; } @@ -53,6 +44,17 @@ static int by_file_ctrl(X509_LOOKUP *ctx, int cmd, const char *argp, long argl, return 0; } +const X509_LOOKUP_METHOD x509_file_lookup = { + nullptr, // new + nullptr, // free + by_file_ctrl, // ctrl + nullptr, // get_by_subject +}; + +} // namespace + +const X509_LOOKUP_METHOD *X509_LOOKUP_file() { return &x509_file_lookup; } + int X509_load_cert_file(X509_LOOKUP *ctx, const char *file, int type) { int ret = 0; BIO *in = nullptr; diff --git a/third_party/boringssl/src/crypto/x509/internal.h b/third_party/boringssl/src/crypto/x509/internal.h index 7257fcf4d..eb35b9acd 100644 --- a/third_party/boringssl/src/crypto/x509/internal.h +++ b/third_party/boringssl/src/crypto/x509/internal.h @@ -21,52 +21,69 @@ #include #include "../asn1/internal.h" -#include "../mem_internal.h" #include "../internal.h" +#include "../mem_internal.h" // Internal structures. DECLARE_OPAQUE_STRUCT(x509_st, X509Impl) DECLARE_OPAQUE_STRUCT(x509_store_st, X509Store) DECLARE_OPAQUE_STRUCT(X509_name_st, X509Name) - -struct X509_pubkey_st { - X509_ALGOR algor; - ASN1_BIT_STRING public_key; - EVP_PKEY *pkey; -} /* X509_PUBKEY */; +DECLARE_OPAQUE_STRUCT(X509_name_entry_st, X509NameEntry) +DECLARE_OPAQUE_STRUCT(X509_pubkey_st, X509Pubkey) BSSL_NAMESPACE_BEGIN -void x509_pubkey_init(X509_PUBKEY *key); -void x509_pubkey_cleanup(X509_PUBKEY *key); +void x509_algor_init(X509_ALGOR *alg); +void x509_algor_cleanup(X509_ALGOR *alg); + +// A ScopedX509Algor is a stack-allocatable `X509_ALGOR` with managed lifetime. +// This cannot use `DECLARE_OPAQUE_STRUCT` because `X509_ALGOR` is a public +// struct. +BORINGSSL_MAKE_STACK_TRAITS(X509_ALGOR, x509_algor_init, x509_algor_cleanup) +using ScopedX509Algor = internal::StackAllocated; + +// x509_parse_algorithm parses a DER-encoded, AlgorithmIdentifier from `cbs` and +// writes the result to `*out`. It returns one on success and zero on error. +int x509_parse_algorithm(CBS *cbs, X509_ALGOR *out); + +// x509_marshal_algorithm marshals `in` as a DER-encoded, AlgorithmIdentifier +// and writes the result to `out`. It returns one on success and zero on error. +int x509_marshal_algorithm(CBB *out, const X509_ALGOR *in); + +class X509Pubkey : public X509_pubkey_st { + public: + ScopedX509Algor algor; + ScopedASN1String public_key{V_ASN1_BIT_STRING}; + UniquePtr pkey; +}; int x509_parse_public_key(CBS *cbs, X509_PUBKEY *out, Span algs); int x509_marshal_public_key(CBB *cbb, const X509_PUBKEY *in); -int x509_pubkey_set1(X509_PUBKEY *key, EVP_PKEY *pkey); +int x509_pubkey_set1(X509_PUBKEY *key, const EVP_PKEY *pkey); -// X509_PUBKEY is an |ASN1_ITEM| whose ASN.1 type is SubjectPublicKeyInfo and C -// type is |X509_PUBKEY*|. -// TODO(crbug.com/42290417): Remove this when |X509| and |X509_REQ| no longer +// X509_PUBKEY is an `ASN1_ITEM` whose ASN.1 type is SubjectPublicKeyInfo and C +// type is `X509_PUBKEY*`. +// TODO(crbug.com/42290417): Remove this when `X509` and `X509_REQ` no longer // depend on the tables. DECLARE_ASN1_ITEM(X509_PUBKEY) -BSSL_NAMESPACE_END - -struct X509_name_entry_st { - ASN1_OBJECT *object; - ASN1_STRING value; - int set; -} /* X509_NAME_ENTRY */; +class X509NameEntry : public X509_name_entry_st { + public: + static constexpr bool kAllowUniquePtr = true; + X509NameEntry(); -BSSL_NAMESPACE_BEGIN + UniquePtr object; + ScopedASN1String value{-1}; + int set = 0; +}; -// X509_NAME_ENTRY is an |ASN1_ITEM| whose ASN.1 type is AttributeTypeAndValue -// (RFC 5280) and C type is |X509_NAME_ENTRY*|. +// X509_NAME_ENTRY is an `ASN1_ITEM` whose ASN.1 type is AttributeTypeAndValue +// (RFC 5280) and C type is `X509_NAME_ENTRY*`. DECLARE_ASN1_ITEM(X509_NAME_ENTRY) -struct X509_NAME_CACHE { +struct X509NameCache { static constexpr bool kAllowUniquePtr = true; // canon contains the DER-encoded canonicalized X.509 Name, not including the // outermost TLV. @@ -77,8 +94,14 @@ struct X509_NAME_CACHE { class X509Name : public X509_name_st { public: - STACK_OF(X509_NAME_ENTRY) *entries = nullptr; - mutable bssl::Atomic cache; + ~X509Name(); + + // TODO(crbug.com/42290036): Switch to `Vector>`, + // which would save an allocation. Potentially `Vector` if we + // are willing to break pointer stability of entries after + // `X509_NAME_add_entry` or `X509_NAME_delete_entry`. + UniquePtr entries; + mutable Atomic cache = nullptr; } /* X509_NAME */; BSSL_NAMESPACE_END @@ -90,8 +113,8 @@ struct x509_attributes_st { BSSL_NAMESPACE_BEGIN -// X509_ATTRIBUTE is an |ASN1_ITEM| whose ASN.1 type is Attribute (RFC 2986) and -// C type is |X509_ATTRIBUTE*|. +// X509_ATTRIBUTE is an `ASN1_ITEM` whose ASN.1 type is Attribute (RFC 2986) and +// C type is `X509_ATTRIBUTE*`. DECLARE_ASN1_ITEM(X509_ATTRIBUTE) typedef struct x509_cert_aux_st { @@ -111,14 +134,14 @@ struct X509_extension_st { ASN1_OCTET_STRING *value; } /* X509_EXTENSION */; -// X509_EXTENSION is an |ASN1_ITEM| whose ASN.1 type is X.509 Extension (RFC -// 5280) and C type is |X509_EXTENSION*|. +// X509_EXTENSION is an `ASN1_ITEM` whose ASN.1 type is X.509 Extension (RFC +// 5280) and C type is `X509_EXTENSION*`. DECLARE_ASN1_ITEM(X509_EXTENSION) BSSL_NAMESPACE_BEGIN -// X509_EXTENSIONS is an |ASN1_ITEM| whose ASN.1 type is SEQUENCE of Extension -// (RFC 5280) and C type is |STACK_OF(X509_EXTENSION)*|. +// X509_EXTENSIONS is an `ASN1_ITEM` whose ASN.1 type is SEQUENCE of Extension +// (RFC 5280) and C type is `STACK_OF(X509_EXTENSION)*`. DECLARE_ASN1_ITEM(X509_EXTENSIONS) class X509Impl : public x509_st, public RefCounted { @@ -126,49 +149,61 @@ class X509Impl : public x509_st, public RefCounted { X509Impl(); // TBSCertificate fields: - uint8_t version = X509_VERSION_1; // One of the |X509_VERSION_*| constants. - ASN1_INTEGER serialNumber; - X509_ALGOR tbs_sig_alg; + uint8_t version = X509_VERSION_1; // One of the `X509_VERSION_*` constants. + ScopedASN1String serialNumber{V_ASN1_INTEGER}; + ScopedX509Algor tbs_sig_alg; X509Name issuer; - ASN1_TIME notBefore; - ASN1_TIME notAfter; + ScopedASN1String notBefore{-1}; + ScopedASN1String notAfter{-1}; X509Name subject; - X509_PUBKEY key; - ASN1_BIT_STRING *issuerUID = nullptr; // [ 1 ] optional in v2 - ASN1_BIT_STRING *subjectUID = nullptr; // [ 2 ] optional in v2 + X509Pubkey key; + UniquePtr issuerUID; // [ 1 ] optional in v2 + UniquePtr subjectUID; // [ 2 ] optional in v2 STACK_OF(X509_EXTENSION) *extensions = nullptr; // [ 3 ] optional in v3 // Certificate fields: - X509_ALGOR sig_alg; - ASN1_BIT_STRING signature; + ScopedX509Algor sig_alg; + ScopedASN1String signature{V_ASN1_BIT_STRING}; // Other state: // buf, if not nullptr, contains a copy of the serialized Certificate. - // TODO(davidben): Now every parsed |X509| has an underlying |CRYPTO_BUFFER|, - // but |X509|s created peacemeal do not. Can we make this more uniform? - CRYPTO_BUFFER *buf = nullptr; + // TODO(davidben): Now every parsed `X509` has an underlying `CRYPTO_BUFFER`, + // but `X509`s created peacemeal do not. Can we make this more uniform? + UniquePtr buf; CRYPTO_EX_DATA ex_data; // These contain copies of various extension values - long ex_pathlen = -1; - uint32_t ex_flags = 0; - uint32_t ex_kusage = 0; - uint32_t ex_xkusage = 0; - ASN1_OCTET_STRING *skid = nullptr; - AUTHORITY_KEYID *akid = nullptr; - STACK_OF(DIST_POINT) *crldp = nullptr; - STACK_OF(GENERAL_NAME) *altname = nullptr; - NAME_CONSTRAINTS *nc = nullptr; - unsigned char cert_hash[SHA256_DIGEST_LENGTH] = {}; + mutable long ex_pathlen = -1; + mutable uint32_t ex_flags = 0; + mutable uint32_t ex_kusage = 0; + mutable uint32_t ex_xkusage = 0; + mutable UniquePtr skid; + mutable UniquePtr akid; + mutable UniquePtr crldp; + mutable UniquePtr altname; + mutable UniquePtr nc; + mutable unsigned char cert_hash[SHA256_DIGEST_LENGTH] = {}; bssl::X509_CERT_AUX *aux = nullptr; - Mutex lock; + mutable Mutex lock; private: friend RefCounted; ~X509Impl(); } /* X509 */; +// x509_marshal_tbs_cert sets `cbb` to the serialized TBSCertificate of `x509`. +// It either replays the saved TBSCertificate encoding from the `CRYPTO_BUFFER`, +// or marshals the TBSCertificate from fields set on `x509`. It returns one on +// success or zero on error. int x509_marshal_tbs_cert(CBB *cbb, const X509 *x509); -// X509 is an |ASN1_ITEM| whose ASN.1 type is X.509 Certificate (RFC 5280) and C -// type is |X509*|. +// x509_get_or_marshal_tbs_cert sets `out` to the serialized TBSCertificate of +// `x509`. If possible, it gets the saved TBSCertificate encoding from the +// `CRYPTO_BUFFER` of `x509`, otherwise it marshals the TBSCertificate from +// fields set on `x509` into `scratch`. It returns one on success or zero on +// error. +int x509_get_or_marshal_tbs_cert(CBS *out, Array *scratch, + const X509 *x509); + +// X509 is an `ASN1_ITEM` whose ASN.1 type is X.509 Certificate (RFC 5280) and C +// type is `X509*`. DECLARE_ASN1_ITEM(X509) typedef struct { @@ -192,8 +227,8 @@ struct X509_req_st { BSSL_NAMESPACE_BEGIN -// X509_REQ is an |ASN1_ITEM| whose ASN.1 type is CertificateRequest (RFC 2986) -// and C type is |X509_REQ*|. +// X509_REQ is an `ASN1_ITEM` whose ASN.1 type is CertificateRequest (RFC 2986) +// and C type is `X509_REQ*`. DECLARE_ASN1_ITEM(X509_REQ) BSSL_NAMESPACE_END @@ -208,9 +243,9 @@ struct x509_revoked_st { BSSL_NAMESPACE_BEGIN -// X509_REVOKED is an |ASN1_ITEM| whose ASN.1 type is an element of the +// X509_REVOKED is an `ASN1_ITEM` whose ASN.1 type is an element of the // revokedCertificates field of TBSCertList (RFC 5280) and C type is -// |X509_REVOKED*|. +// `X509_REVOKED*`. DECLARE_ASN1_ITEM(X509_REVOKED) typedef struct { @@ -228,22 +263,6 @@ DECLARE_ASN1_FUNCTIONS_const(X509_CRL_INFO) BSSL_NAMESPACE_END -// Values in idp_flags field -// IDP present -#define IDP_PRESENT 0x1 -// IDP values inconsistent -#define IDP_INVALID 0x2 -// onlyuser true -#define IDP_ONLYUSER 0x4 -// onlyCA true -#define IDP_ONLYCA 0x8 -// onlyattr true -#define IDP_ONLYATTR 0x10 -// indirectCRL true -#define IDP_INDIRECT 0x20 -// onlysomereasons present -#define IDP_REASONS 0x40 - struct X509_crl_st { // actual signature bssl::X509_CRL_INFO *crl; @@ -254,23 +273,21 @@ struct X509_crl_st { // Copies of various extensions AUTHORITY_KEYID *akid; ISSUING_DIST_POINT *idp; - // Convenient breakdown of IDP - int idp_flags; unsigned char crl_hash[SHA256_DIGEST_LENGTH]; } /* X509_CRL */; BSSL_NAMESPACE_BEGIN -// X509_CRL is an |ASN1_ITEM| whose ASN.1 type is X.509 CertificateList (RFC -// 5280) and C type is |X509_CRL*|. +// X509_CRL is an `ASN1_ITEM` whose ASN.1 type is X.509 CertificateList (RFC +// 5280) and C type is `X509_CRL*`. DECLARE_ASN1_ITEM(X509_CRL) -// GENERAL_NAME is an |ASN1_ITEM| whose ASN.1 type is GeneralName and C type is -// |GENERAL_NAME*|. +// GENERAL_NAME is an `ASN1_ITEM` whose ASN.1 type is GeneralName and C type is +// `GENERAL_NAME*`. DECLARE_ASN1_ITEM(GENERAL_NAME) -// GENERAL_NAMES is an |ASN1_ITEM| whose ASN.1 type is SEQUENCE OF GeneralName -// and C type is |GENERAL_NAMES*|, aka |STACK_OF(GENERAL_NAME)*|. +// GENERAL_NAMES is an `ASN1_ITEM` whose ASN.1 type is SEQUENCE OF GeneralName +// and C type is `GENERAL_NAMES*`, aka `STACK_OF(GENERAL_NAME)*`. DECLARE_ASN1_ITEM(GENERAL_NAMES) BSSL_NAMESPACE_END @@ -305,12 +322,12 @@ struct x509_object_st { BSSL_NAMESPACE_BEGIN -// NETSCAPE_SPKI is an |ASN1_ITEM| whose ASN.1 type is -// SignedPublicKeyAndChallenge and C type is |NETSCAPE_SPKI*|. +// NETSCAPE_SPKI is an `ASN1_ITEM` whose ASN.1 type is +// SignedPublicKeyAndChallenge and C type is `NETSCAPE_SPKI*`. DECLARE_ASN1_ITEM(NETSCAPE_SPKI) -// NETSCAPE_SPKAC is an |ASN1_ITEM| whose ASN.1 type is PublicKeyAndChallenge -// and C type is |NETSCAPE_SPKAC*|. +// NETSCAPE_SPKAC is an `ASN1_ITEM` whose ASN.1 type is PublicKeyAndChallenge +// and C type is `NETSCAPE_SPKAC*`. DECLARE_ASN1_ITEM(NETSCAPE_SPKAC) BSSL_NAMESPACE_END @@ -356,12 +373,12 @@ BSSL_NAMESPACE_END // This is the functions plus an instance of the local variables. struct x509_lookup_st { const X509_LOOKUP_METHOD *method; // the functions - void *method_data; // method data + void *method_data; // method data X509_STORE *store_ctx; // who owns us } /* X509_LOOKUP */; -// This is a used when verifying cert chains. Since the +// This is used when verifying cert chains. Since the // gathering of the cert chain can take some time (and have to be // 'retried', this needs to be kept and passed around. struct x509_store_ctx_st { @@ -375,24 +392,18 @@ struct x509_store_ctx_st { X509_VERIFY_PARAM *param; // trusted_stack, if non-NULL, is a set of trusted certificates to consider - // instead of those from |X509_STORE|. + // instead of those from `X509_STORE`. STACK_OF(X509) *trusted_stack; // Callbacks for various operations - X509_STORE_CTX_verify_cb verify_cb; // error callback + X509_STORE_CTX_verify_cb verify_cb; // error callback - // The following is built up - int last_untrusted; // index of last untrusted cert STACK_OF(X509) *chain; // chain of X509s - built up and trusted // When something goes wrong, this is why int error_depth; int error; X509 *current_cert; - X509_CRL *current_crl; // current CRL - - X509 *current_crl_issuer; // issuer of current CRL - int current_crl_score; // score of current CRL CRYPTO_EX_DATA ex_data; } /* X509_STORE_CTX */; @@ -406,20 +417,20 @@ int X509_CERT_AUX_print(BIO *bp, X509_CERT_AUX *x, int indent); // RSA-PSS functions. -// x509_rsa_pss_to_ctx configures |ctx| for an RSA-PSS operation based on -// signature algorithm parameters in |sigalg| (which must have type -// |NID_rsassaPss|) and key |pkey|. It returns one on success and zero on +// x509_rsa_pss_to_ctx configures `ctx` for an RSA-PSS operation based on +// signature algorithm parameters in `sigalg` (which must have type +// `NID_rsassaPss`) and key `pkey`. It returns one on success and zero on // error. int x509_rsa_pss_to_ctx(EVP_MD_CTX *ctx, const X509_ALGOR *sigalg, - EVP_PKEY *pkey); + const EVP_PKEY *pkey); -// x509_rsa_pss_to_ctx sets |algor| to the signature algorithm parameters for -// |ctx|, which must have been configured for an RSA-PSS signing operation. It +// x509_rsa_pss_to_ctx sets `algor` to the signature algorithm parameters for +// `ctx`, which must have been configured for an RSA-PSS signing operation. It // returns one on success and zero on error. int x509_rsa_ctx_to_pss(EVP_MD_CTX *ctx, X509_ALGOR *algor); // x509_print_rsa_pss_params prints a human-readable representation of RSA-PSS -// parameters in |sigalg| to |bp|. It returns one on success and zero on +// parameters in `sigalg` to `bp`. It returns one on success and zero on // error. int x509_print_rsa_pss_params(BIO *bp, const X509_ALGOR *sigalg, int indent, ASN1_PCTX *pctx); @@ -427,25 +438,32 @@ int x509_print_rsa_pss_params(BIO *bp, const X509_ALGOR *sigalg, int indent, // Signature algorithm functions. -// x509_digest_sign_algorithm encodes the signing parameters of |ctx| as an -// AlgorithmIdentifier and saves the result in |algor|. It returns one on +// x509_digest_sign_algorithm encodes the signing parameters of `ctx` as an +// AlgorithmIdentifier and saves the result in `algor`. It returns one on // success, or zero on error. int x509_digest_sign_algorithm(EVP_MD_CTX *ctx, X509_ALGOR *algor); -// x509_digest_verify_init sets up |ctx| for a signature verification operation -// with public key |pkey| and parameters from |algor|. The |ctx| argument must -// have been initialised with |EVP_MD_CTX_init|. It returns one on success, or +// x509_digest_verify_init sets up `ctx` for a signature verification operation +// with public key `pkey` and parameters from `algor`. The `ctx` argument must +// have been initialised with `EVP_MD_CTX_init`. It returns one on success, or // zero on error. int x509_digest_verify_init(EVP_MD_CTX *ctx, const X509_ALGOR *sigalg, - EVP_PKEY *pkey); + const EVP_PKEY *pkey); -// x509_verify_signature verifies a |signature| using |sigalg| and |pkey| over -// |in|. It returns one if the signature is valid and zero on error. +// x509_verify_signature verifies a `signature` using `sigalg` and `pkey` over +// `in`. It returns one if the signature is valid and zero on error. int x509_verify_signature(const X509_ALGOR *sigalg, const ASN1_BIT_STRING *signature, - Span in, EVP_PKEY *pkey); + Span in, const EVP_PKEY *pkey); + +// x509_verify_signature_bytes behaves like `x509_verify_signature` but takes in +// a span containing the signature bytes (without ASN.1 headers). It returns one +// if the signature is valid and zero on error. +int x509_verify_signature_bytes(const X509_ALGOR *sigalg, + Span signature, + Span in, const EVP_PKEY *pkey); -// x509_sign_to_bit_string signs |in| using |ctx| and saves the result in |out|. +// x509_sign_to_bit_string signs `in` using `ctx` and saves the result in `out`. // It returns the length of the signature on success and zero on error. int x509_sign_to_bit_string(EVP_MD_CTX *ctx, ASN1_BIT_STRING *out, Span in); @@ -453,124 +471,123 @@ int x509_sign_to_bit_string(EVP_MD_CTX *ctx, ASN1_BIT_STRING *out, // Path-building functions. -// X509_policy_check checks certificate policies in |certs|. |user_policies| is -// the user-initial-policy-set. If |user_policies| is NULL or empty, it is -// interpreted as anyPolicy. |flags| is a set of |X509_V_FLAG_*| values to -// apply. It returns |X509_V_OK| on success and |X509_V_ERR_*| on error. It -// additionally sets |*out_current_cert| to the certificate where the error -// occurred. If the function succeeded, or the error applies to the entire -// chain, it sets |*out_current_cert| to NULL. +// X509_policy_check checks certificate policies in `certs`. `user_policies` is +// the user-initial-policy-set. If `user_policies` is NULL or empty, it is +// interpreted as anyPolicy. `flags` is a set of `X509_V_FLAG_*` values to +// apply. It returns `X509_V_OK` on success and `X509_V_ERR_*` on error. It +// additionally sets `*out_error_depth` to the index of certificate where the +// error occurred. int X509_policy_check(const STACK_OF(X509) *certs, const STACK_OF(ASN1_OBJECT) *user_policies, - unsigned long flags, X509 **out_current_cert); + unsigned long flags, int *out_error_depth); -// x509_check_issued_with_callback calls |X509_check_issued|, but allows the +// x509_check_issued_with_callback calls `X509_check_issued`, but allows the // verify callback to override the result. It returns one on success and zero on // error. // // TODO(davidben): Reduce the scope of the verify callback and remove this. The -// callback only runs with |X509_V_FLAG_CB_ISSUER_CHECK|, which is only used by +// callback only runs with `X509_V_FLAG_CB_ISSUER_CHECK`, which is only used by // one internal project and rust-openssl, who use it by mistake. int x509_check_issued_with_callback(X509_STORE_CTX *ctx, const X509 *x, const X509 *issuer); -// x509v3_bytes_to_hex encodes |len| bytes from |in| to hex and returns a +// x509v3_bytes_to_hex encodes `len` bytes from `in` to hex and returns a // newly-allocated NUL-terminated string containing the result, or NULL on // allocation error. // -// This function was historically named |hex_to_string| in OpenSSL. Despite the -// name, |hex_to_string| converted to hex. +// This function was historically named `hex_to_string` in OpenSSL. Despite the +// name, `hex_to_string` converted to hex. OPENSSL_EXPORT char *x509v3_bytes_to_hex(const uint8_t *in, size_t len); -// x509v3_hex_string_to_bytes decodes |str| in hex and returns a newly-allocated -// array containing the result, or NULL on error. On success, it sets |*len| to +// x509v3_hex_string_to_bytes decodes `str` in hex and returns a newly-allocated +// array containing the result, or NULL on error. On success, it sets `*len` to // the length of the result. Colon separators between bytes in the input are // allowed and ignored. // -// This function was historically named |string_to_hex| in OpenSSL. Despite the -// name, |string_to_hex| converted from hex. +// This function was historically named `string_to_hex` in OpenSSL. Despite the +// name, `string_to_hex` converted from hex. unsigned char *x509v3_hex_to_bytes(const char *str, size_t *len); -// x509v3_conf_name_matches returns one if |name| is equal to |cmp| or begins -// with |cmp| followed by '.', and zero otherwise. +// x509v3_conf_name_matches returns one if `name` is equal to `cmp` or begins +// with `cmp` followed by '.', and zero otherwise. int x509v3_conf_name_matches(const char *name, const char *cmp); -// x509v3_looks_like_dns_name returns one if |in| looks like a DNS name and zero +// x509v3_looks_like_dns_name returns one if `in` looks like a DNS name and zero // otherwise. OPENSSL_EXPORT int x509v3_looks_like_dns_name(const unsigned char *in, size_t len); // x509v3_cache_extensions fills in a number of fields relating to X.509 -// extensions in |x|. It returns one on success and zero if some extensions were +// extensions in `x`. It returns one on success and zero if some extensions were // invalid. -OPENSSL_EXPORT int x509v3_cache_extensions(X509 *x); +OPENSSL_EXPORT int x509v3_cache_extensions(const X509 *x); -// x509v3_a2i_ipadd decodes |ipasc| as an IPv4 or IPv6 address. IPv6 addresses +// x509v3_a2i_ipadd decodes `ipasc` as an IPv4 or IPv6 address. IPv6 addresses // use colon-separated syntax while IPv4 addresses use dotted decimal syntax. If // it decodes an IPv4 address, it writes the result to the first four bytes of -// |ipout| and returns four. If it decodes an IPv6 address, it writes the result -// to all 16 bytes of |ipout| and returns 16. Otherwise, it returns zero. +// `ipout` and returns four. If it decodes an IPv6 address, it writes the result +// to all 16 bytes of `ipout` and returns 16. Otherwise, it returns zero. int x509v3_a2i_ipadd(unsigned char ipout[16], const char *ipasc); -// A |BIT_STRING_BITNAME| is used to contain a list of bit names. +// A `BIT_STRING_BITNAME` is used to contain a list of bit names. typedef struct { int bitnum; const char *lname; const char *sname; } BIT_STRING_BITNAME; -// x509V3_add_value_asn1_string appends a |CONF_VALUE| with the specified name -// and value to |*extlist|. if |*extlist| is NULL, it sets |*extlist| to a -// newly-allocated |STACK_OF(CONF_VALUE)| first. It returns one on success and +// x509V3_add_value_asn1_string appends a `CONF_VALUE` with the specified name +// and value to `*extlist`. if `*extlist` is NULL, it sets `*extlist` to a +// newly-allocated `STACK_OF(CONF_VALUE)` first. It returns one on success and // zero on error. int x509V3_add_value_asn1_string(const char *name, const ASN1_STRING *value, STACK_OF(CONF_VALUE) **extlist); -// X509V3_NAME_from_section adds attributes to |nm| by interpreting the -// key/value pairs in |dn_sk|. It returns one on success and zero on error. -// |chtype|, which should be one of |MBSTRING_*| constants, determines the +// X509V3_NAME_from_section adds attributes to `nm` by interpreting the +// key/value pairs in `dn_sk`. It returns one on success and zero on error. +// `chtype`, which should be one of `MBSTRING_*` constants, determines the // character encoding used to interpret values. int X509V3_NAME_from_section(X509_NAME *nm, const STACK_OF(CONF_VALUE) *dn_sk, int chtype); -// X509V3_bool_from_string decodes |str| as a boolean. On success, it returns -// one and sets |*out_bool| to resulting value. Otherwise, it returns zero. +// X509V3_bool_from_string decodes `str` as a boolean. On success, it returns +// one and sets `*out_bool` to resulting value. Otherwise, it returns zero. int X509V3_bool_from_string(const char *str, ASN1_BOOLEAN *out_bool); -// X509V3_get_value_bool decodes |value| as a boolean. On success, it returns -// one and sets |*out_bool| to the resulting value. Otherwise, it returns zero. +// X509V3_get_value_bool decodes `value` as a boolean. On success, it returns +// one and sets `*out_bool` to the resulting value. Otherwise, it returns zero. int X509V3_get_value_bool(const CONF_VALUE *value, ASN1_BOOLEAN *out_bool); -// X509V3_get_value_int decodes |value| as an integer. On success, it returns -// one and sets |*aint| to the resulting value. Otherwise, it returns zero. If -// |*aint| was non-NULL at the start of the function, it frees the previous +// X509V3_get_value_int decodes `value` as an integer. On success, it returns +// one and sets `*aint` to the resulting value. Otherwise, it returns zero. If +// `*aint` was non-NULL at the start of the function, it frees the previous // value before writing a new one. int X509V3_get_value_int(const CONF_VALUE *value, ASN1_INTEGER **aint); -// X509V3_get_section behaves like |NCONF_get_section| but queries |ctx|'s +// X509V3_get_section behaves like `NCONF_get_section` but queries `ctx`'s // config database. const STACK_OF(CONF_VALUE) *X509V3_get_section(const X509V3_CTX *ctx, const char *section); -// X509V3_add_value appends a |CONF_VALUE| containing |name| and |value| to -// |*extlist|. It returns one on success and zero on error. If |*extlist| is -// NULL, it sets |*extlist| to a newly-allocated |STACK_OF(CONF_VALUE)| -// containing the result. Either |name| or |value| may be NULL to omit the +// X509V3_add_value appends a `CONF_VALUE` containing `name` and `value` to +// `*extlist`. It returns one on success and zero on error. If `*extlist` is +// NULL, it sets `*extlist` to a newly-allocated `STACK_OF(CONF_VALUE)` +// containing the result. Either `name` or `value` may be NULL to omit the // field. // -// On failure, if |*extlist| was NULL, |*extlist| will remain NULL when the +// On failure, if `*extlist` was NULL, `*extlist` will remain NULL when the // function returns. int X509V3_add_value(const char *name, const char *value, STACK_OF(CONF_VALUE) **extlist); -// X509V3_add_value_bool behaves like |X509V3_add_value| but stores the value -// "TRUE" if |asn1_bool| is non-zero and "FALSE" otherwise. +// X509V3_add_value_bool behaves like `X509V3_add_value` but stores the value +// "TRUE" if `asn1_bool` is non-zero and "FALSE" otherwise. int X509V3_add_value_bool(const char *name, int asn1_bool, STACK_OF(CONF_VALUE) **extlist); -// X509V3_add_value_bool behaves like |X509V3_add_value| but stores a string -// representation of |aint|. Note this string representation may be decimal or -// hexadecimal, depending on the size of |aint|. +// X509V3_add_value_bool behaves like `X509V3_add_value` but stores a string +// representation of `aint`. Note this string representation may be decimal or +// hexadecimal, depending on the size of `aint`. int X509V3_add_value_int(const char *name, const ASN1_INTEGER *aint, STACK_OF(CONF_VALUE) **extlist); @@ -580,16 +597,16 @@ STACK_OF(CONF_VALUE) *X509V3_parse_list(const char *line); ERR_add_error_data(6, "section:", (val)->section, ",name:", (val)->name, \ ",value:", (val)->value); -// GENERAL_NAME_cmp returns zero if |a| and |b| are equal and a non-zero -// value otherwise. Note this function does not provide a comparison suitable -// for sorting. -// -// This function is exported for testing. -OPENSSL_EXPORT int GENERAL_NAME_cmp(const GENERAL_NAME *a, - const GENERAL_NAME *b); +// GENERAL_NAME_cmp compares `a` and `b`. It returns zero if they are equal, and +// a negative or positive value if `a` sorts before or after `b`, respectively. +// Names are ordered first by type, then by type-specific contents. The order +// is arbitrary but suitable for sorting, with one exception: if a directoryName +// cannot be serialized, `X509_NAME_cmp`, and thus this function, returns -2. +// This cannot happen for parsed names that have not since been modified. +int GENERAL_NAME_cmp(const GENERAL_NAME *a, const GENERAL_NAME *b); -// X509_VERIFY_PARAM_lookup returns a pre-defined |X509_VERIFY_PARAM| named by -// |name|, or NULL if no such name is defined. +// X509_VERIFY_PARAM_lookup returns a pre-defined `X509_VERIFY_PARAM` named by +// `name`, or NULL if no such name is defined. const X509_VERIFY_PARAM *X509_VERIFY_PARAM_lookup(const char *name); GENERAL_NAME *v2i_GENERAL_NAME(const X509V3_EXT_METHOD *method, @@ -608,35 +625,48 @@ int X509_is_valid_trust_id(int trust); int X509_PURPOSE_get_trust(const X509_PURPOSE *xp); -// TODO(https://crbug.com/boringssl/695): Remove this. -int DIST_POINT_set_dpname(DIST_POINT_NAME *dpn, X509_NAME *iname); - -void x509_name_init(X509_NAME *name); -void x509_name_cleanup(X509_NAME *name); - -// x509_parse_name parses a DER-encoded, X.509 Name from |cbs| and writes the -// result to |*out|. It returns one on success and zero on error. +// x509_parse_name parses a DER-encoded, X.509 Name from `cbs` and writes the +// result to `*out`. It returns one on success and zero on error. int x509_parse_name(CBS *cbs, X509_NAME *out); -// x509_marshal_name marshals |in| as a DER-encoded, X.509 Name and writes the -// result to |out|. It returns one on success and zero on error. +// x509_marshal_name marshals `in` as a DER-encoded, X.509 Name and writes the +// result to `out`. It returns one on success and zero on error. int x509_marshal_name(CBB *out, const X509_NAME *in); -const X509_NAME_CACHE *x509_name_get_cache(const X509_NAME *name); +const X509NameCache *x509_name_get_cache(const X509_NAME *name); void x509_name_invalidate_cache(X509_NAME *name); int x509_name_copy(X509_NAME *dst, const X509_NAME *src); -void x509_algor_init(X509_ALGOR *alg); -void x509_algor_cleanup(X509_ALGOR *alg); - -// x509_parse_algorithm parses a DER-encoded, AlgorithmIdentifier from |cbs| and -// writes the result to |*out|. It returns one on success and zero on error. -int x509_parse_algorithm(CBS *cbs, X509_ALGOR *out); -// x509_marshal_algorithm marshals |in| as a DER-encoded, AlgorithmIdentifier -// and writes the result to |out|. It returns one on success and zero on error. -int x509_marshal_algorithm(CBB *out, const X509_ALGOR *in); +// Merkle Tree Certificate (MTC) verification functions. + +// x509_is_merkle_tree_ca returns whether `x509` contains an extension of type +// id-pe-mtcCertificationAuthority-SHA256. +bool x509_is_merkle_tree_ca(const X509 *x509); + +// x509_evaluate_mtc_subtree_inclusion_proof carries out the procedure in +// section 4.3.2 of draft-ietf-plants-merkle-tree-certs to evaluate a subtree +// inclusion proof for an entry at index `index` with hash `entry_hash` of a +// subtree defined by [`subtree_start`, `subtree_end`). The `inclusion_proof` to +// be evaluated is passed as a byte array consisting of concatenated hashes +// produced from the `log_hash` algorithm. This function returns true if +// inclusion proof evaluation succeeded, and if so, writes the expected subtree +// hash for the specified subtree containing the entry to `out`, which must be +// the right size for `log_hash`. It returns false on error, including if the +// inclusion proof fails to evaluate. +bool x509_evaluate_mtc_subtree_inclusion_proof( + Span out, const EVP_MD *log_hash, + Span inclusion_proof, uint64_t index, + Span entry_hash, uint64_t subtree_start, + uint64_t subtree_end); + +// x509_verify_mtc verifies `x509` as a Merkle Tree Certificate issued by +// `issuer`, which must be an MTC CA represented in X.509 format. `pkey` is the +// `issuer`'s public key. It returns one if the MTC is valid, or zero on error. +// This function only checks the MTC proof itself and does not perform a full +// certificate validation. +int x509_verify_mtc(const X509 *x509, const EVP_PKEY *pkey, const X509 *issuer); // Standard extensions. diff --git a/third_party/boringssl/src/crypto/x509/name_print.cc b/third_party/boringssl/src/crypto/x509/name_print.cc index e4db871e4..ffe7dc1ec 100644 --- a/third_party/boringssl/src/crypto/x509/name_print.cc +++ b/third_party/boringssl/src/crypto/x509/name_print.cc @@ -23,13 +23,15 @@ #include -static int maybe_write(BIO *out, const void *buf, int len) { - // If |out| is NULL, ignore the output but report the length. +namespace { + +int maybe_write(BIO *out, const void *buf, int len) { + // If `out` is NULL, ignore the output but report the length. return out == nullptr || BIO_write(out, buf, len) == len; } -// do_indent prints |indent| spaces to |out|. -static int do_indent(BIO *out, int indent) { +// do_indent prints `indent` spaces to `out`. +int do_indent(BIO *out, int indent) { for (int i = 0; i < indent; i++) { if (!maybe_write(out, " ", 1)) { return 0; @@ -41,8 +43,7 @@ static int do_indent(BIO *out, int indent) { #define FN_WIDTH_LN 25 #define FN_WIDTH_SN 10 -static int do_name_ex(BIO *out, const X509_NAME *n, int indent, - unsigned long flags) { +int do_name_ex(BIO *out, const X509_NAME *n, int indent, unsigned long flags) { int prev = -1, orflags; int outlen, len; const char *sep_dn, *sep_mv, *sep_eq; @@ -127,7 +128,7 @@ static int do_name_ex(BIO *out, const X509_NAME *n, int indent, const ASN1_OBJECT *fn = X509_NAME_ENTRY_get_object(ent); const ASN1_STRING *val = X509_NAME_ENTRY_get_data(ent); assert((flags & XN_FLAG_FN_MASK) == XN_FLAG_FN_SN); - // Print the short name if available, othewise serialize the OID. + // Print the short name if available, otherwise serialize the OID. char objtmp[80]; const char *objbuf = nullptr; int fn_nid = OBJ_obj2nid(fn); @@ -162,6 +163,8 @@ static int do_name_ex(BIO *out, const X509_NAME *n, int indent, return outlen; } +} // namespace + int X509_NAME_print_ex(BIO *out, const X509_NAME *nm, int indent, unsigned long flags) { if (flags == XN_FLAG_COMPAT) { @@ -174,7 +177,7 @@ int X509_NAME_print_ex_fp(FILE *fp, const X509_NAME *nm, int indent, unsigned long flags) { BIO *bio = nullptr; if (fp != nullptr) { - // If |fp| is NULL, this function returns the number of bytes without + // If `fp` is NULL, this function returns the number of bytes without // writing. bio = BIO_new_fp(fp, BIO_NOCLOSE); if (bio == nullptr) { diff --git a/third_party/boringssl/src/crypto/x509/policy.cc b/third_party/boringssl/src/crypto/x509/policy.cc index 0ab37547d..534ac0076 100644 --- a/third_party/boringssl/src/crypto/x509/policy.cc +++ b/third_party/boringssl/src/crypto/x509/policy.cc @@ -93,13 +93,13 @@ struct X509PolicyNode { bool mapped = false; // reachable is whether this node is reachable from some valid policy in the - // end-entity certificate. It is computed during |has_explicit_policy|. + // end-entity certificate. It is computed during `has_explicit_policy`. bool reachable = false; }; // An X509PolicyLevel is the collection of nodes at the same depth in the // policy graph. This structure can also be used to represent a level's -// "expected_policy_set" values. See |process_policy_mappings|. +// "expected_policy_set" values. See `process_policy_mappings`. class X509PolicyLevel { public: bool has_any_policy() const { return has_any_policy_; } @@ -116,7 +116,7 @@ class X509PolicyLevel { nodes_.clear(); } - // Find returns the node corresponding to |policy|, or nullptr if none exists. + // Find returns the node corresponding to `policy`, or nullptr if none exists. X509PolicyNode *Find(const ASN1_OBJECT *policy) { // The list is sorted, so we can binary search. auto it = std::lower_bound( @@ -130,9 +130,9 @@ class X509PolicyLevel { return &*it; } - // AddNodes adds the nodes in |nodes|. It returns true on success and false on - // error. No policy in |nodes| may already be present. This method leaves - // the objects in |nodes| in a moved-from state. + // AddNodes adds the nodes in `nodes`. It returns true on success and false on + // error. No policy in `nodes` may already be present. This method leaves + // the objects in `nodes` in a moved-from state. // // This method re-sorts the nodes, so it runs in time proportional to the // total size of the level. However, each level is only added to three times @@ -151,7 +151,7 @@ class X509PolicyLevel { return true; } - // EraseNodesIf removes all nodes that satisfy the predicate |pred|. + // EraseNodesIf removes all nodes that satisfy the predicate `pred`. template void EraseNodesIf(Pred pred) { nodes_.EraseIf(pred); @@ -170,11 +170,11 @@ int policyinfo_cmp(const POLICYINFO *const *a, const POLICYINFO *const *b) { return OBJ_cmp((*a)->policyid, (*b)->policyid); } -// process_certificate_policies updates |level| to incorporate |x509|'s +// process_certificate_policies updates `level` to incorporate `x509`'s // certificate policies extension. This implements steps (d) and (e) of RFC -// 5280, section 6.1.3. |level| must contain the previous level's +// 5280, section 6.1.3. `level` must contain the previous level's // "expected_policy_set" information. For all but the top-most level, this is -// the output of |process_policy_mappings|. |any_policy_allowed| specifies +// the output of `process_policy_mappings`. `any_policy_allowed` specifies // whether anyPolicy is allowed or inhibited, taking into account the exception // for self-issued certificates. bool process_certificate_policies(const X509 *x509, X509PolicyLevel *level, @@ -210,20 +210,20 @@ bool process_certificate_policies(const X509 *x509, X509PolicyLevel *level, } if (i > 0 && OBJ_cmp(sk_POLICYINFO_value(policies.get(), i - 1)->policyid, policy->policyid) == 0) { - // Per RFC 5280, section 4.2.1.4, |policies| may not have duplicates. + // Per RFC 5280, section 4.2.1.4, `policies` may not have duplicates. OPENSSL_PUT_ERROR(X509, X509_R_INVALID_POLICY_EXTENSION); return false; } } // This does the same thing as RFC 5280, section 6.1.3, step (d), though in - // a slightly different order. |level| currently contains + // a slightly different order. `level` currently contains // "expected_policy_set" values of the previous level. See - // |process_policy_mappings| for details. + // `process_policy_mappings` for details. const bool previous_level_has_any_policy = level->has_any_policy(); // First, we handle steps (d.1.i) and (d.2). The net effect of these two - // steps is to intersect |level| with |policies|, ignoring anyPolicy if it + // steps is to intersect `level` with `policies`, ignoring anyPolicy if it // is inhibited. if (!cert_has_any_policy || !any_policy_allowed) { level->EraseNodesIf([&](const X509PolicyNode &node) { @@ -241,7 +241,7 @@ bool process_certificate_policies(const X509 *x509, X509PolicyLevel *level, if (previous_level_has_any_policy) { Vector new_nodes; for (const POLICYINFO *policy : policies.get()) { - // Though we've reordered the steps slightly, |policy| is in |level| if + // Though we've reordered the steps slightly, `policy` is in `level` if // and only if it would have been a match in step (d.1.ii). if (!is_any_policy(policy->policyid) && level->Find(policy->policyid) == nullptr) { @@ -269,21 +269,21 @@ int compare_subject_policy(const POLICY_MAPPING *const *a, return OBJ_cmp((*a)->subjectDomainPolicy, (*b)->subjectDomainPolicy); } -// process_policy_mappings processes the policy mappings extension of |cert|, -// whose corresponding graph level is |level|. |mapping_allowed| specifies +// process_policy_mappings processes the policy mappings extension of `cert`, +// whose corresponding graph level is `level`. `mapping_allowed` specifies // whether policy mapping is inhibited at this point. On success, it returns an -// |X509PolicyLevel| containing the "expected_policy_set" for |level|. On error, +// `X509PolicyLevel` containing the "expected_policy_set" for `level`. On error, // it returns std::nullopt. This implements steps (a) and (b) of RFC 5280, // section 6.1.4. // -// We represent the "expected_policy_set" as an |X509PolicyLevel|. -// |has_any_policy| indicates whether there is an anyPolicy node with +// We represent the "expected_policy_set" as an `X509PolicyLevel`. +// `has_any_policy` indicates whether there is an anyPolicy node with // "expected_policy_set" of {anyPolicy}. If a node with policy oid P1 contains // P2 in its "expected_policy_set", the level will contain a node of policy P2 -// with P1 in |parent_policies|. +// with P1 in `parent_policies`. // -// This is equivalent to the |X509PolicyLevel| that would result if the next -// certificates contained anyPolicy. |process_certificate_policies| will filter +// This is equivalent to the `X509PolicyLevel` that would result if the next +// certificates contained anyPolicy. `process_certificate_policies` will filter // this result down to compute the actual level. std::optional process_policy_mappings(const X509 *cert, X509PolicyLevel *level, @@ -317,12 +317,12 @@ std::optional process_policy_mappings(const X509 *cert, sk_POLICY_MAPPING_sort(mappings.get()); if (mapping_allowed) { - // Mark nodes as mapped, and add any nodes to |level| which may be + // Mark nodes as mapped, and add any nodes to `level` which may be // needed as part of RFC 5280, section 6.1.4, step (b.1). Vector new_nodes; const ASN1_OBJECT *last_policy = nullptr; for (const POLICY_MAPPING *mapping : mappings.get()) { - // There may be multiple mappings with the same |issuerDomainPolicy|. + // There may be multiple mappings with the same `issuerDomainPolicy`. if (last_policy != nullptr && OBJ_cmp(mapping->issuerDomainPolicy, last_policy) == 0) { continue; @@ -353,7 +353,7 @@ std::optional process_policy_mappings(const X509 *cert, // RFC 5280, section 6.1.4, step (b.2). If mapping is inhibited, delete // all mapped nodes. level->EraseNodesIf([&](const X509PolicyNode &node) { - // |mappings| must have been sorted by |compare_issuer_policy|. + // `mappings` must have been sorted by `compare_issuer_policy`. assert(sk_POLICY_MAPPING_is_sorted(mappings.get())); // Check if the node was mapped. POLICY_MAPPING mapping; @@ -367,7 +367,7 @@ std::optional process_policy_mappings(const X509 *cert, } // If a node was not mapped, it retains the original "explicit_policy_set" - // value, itself. Add those to |mappings|. + // value, itself. Add those to `mappings`. if (mappings == nullptr) { mappings.reset(sk_POLICY_MAPPING_new_null()); if (mappings == nullptr) { @@ -394,10 +394,10 @@ std::optional process_policy_mappings(const X509 *cert, sk_POLICY_MAPPING_set_cmp_func(mappings.get(), compare_subject_policy); sk_POLICY_MAPPING_sort(mappings.get()); - // Convert |mappings| to our "expected_policy_set" representation. + // Convert `mappings` to our "expected_policy_set" representation. Vector next_nodes; for (POLICY_MAPPING *mapping : mappings.get()) { - // Skip mappings where |issuerDomainPolicy| does not appear in the graph. + // Skip mappings where `issuerDomainPolicy` does not appear in the graph. if (!level->has_any_policy() && level->Find(mapping->issuerDomainPolicy) == nullptr) { continue; @@ -411,7 +411,7 @@ std::optional process_policy_mappings(const X509 *cert, } } - // |mapping| is going to be destroyed, so steal its policy object. + // `mapping` is going to be destroyed, so steal its policy object. UniquePtr policy( std::exchange(mapping->issuerDomainPolicy, nullptr)); if (!next_nodes.back().parent_policies.Push(std::move(policy))) { @@ -427,9 +427,9 @@ std::optional process_policy_mappings(const X509 *cert, return next; } -// apply_skip_certs, if |skip_certs| is non-NULL, sets |*value| to the minimum -// of its current value and |skip_certs|. It returns true on success and false -// if |skip_certs| is negative. +// apply_skip_certs, if `skip_certs` is non-NULL, sets `*value` to the minimum +// of its current value and `skip_certs`. It returns true on success and false +// if `skip_certs` is negative. bool apply_skip_certs(const ASN1_INTEGER *skip_certs, size_t *value) { if (skip_certs == nullptr) { return true; @@ -441,7 +441,7 @@ bool apply_skip_certs(const ASN1_INTEGER *skip_certs, size_t *value) { return false; } - // If |skip_certs| does not fit in |uint64_t|, it must exceed |*value|. + // If `skip_certs` does not fit in `uint64_t`, it must exceed `*value`. uint64_t u64; if (ASN1_INTEGER_get_uint64(&u64, skip_certs) && u64 < *value) { *value = (size_t)u64; @@ -450,8 +450,8 @@ bool apply_skip_certs(const ASN1_INTEGER *skip_certs, size_t *value) { return true; } -// process_policy_constraints updates |*explicit_policy|, |*policy_mapping|, and -// |*inhibit_any_policy| according to |x509|'s policy constraints and inhibit +// process_policy_constraints updates `*explicit_policy`, `*policy_mapping`, and +// `*inhibit_any_policy` according to `x509`'s policy constraints and inhibit // anyPolicy extensions. It returns one on success and zero on error. This // implements steps (i) and (j) of RFC 5280, section 6.1.4. bool process_policy_constraints(const X509 *x509, size_t *explicit_policy, @@ -489,9 +489,9 @@ bool process_policy_constraints(const X509 *x509, size_t *explicit_policy, } // has_explicit_policy returns true if the set of authority-space policy OIDs -// |levels| has some non-empty intersection with |user_policies|, and false +// `levels` has some non-empty intersection with `user_policies`, and false // otherwise. This mirrors the logic in RFC 5280, section 6.1.5, step (g). This -// function modifies |levels| and should only be called at the end of policy +// function modifies `levels` and should only be called at the end of policy // evaluation. bool has_explicit_policy(Span levels, const STACK_OF(ASN1_OBJECT) *user_policies) { @@ -505,7 +505,7 @@ bool has_explicit_policy(Span levels, // Step (g.ii). If the policy graph is not empty and the user set contains // anyPolicy, the intersection is the entire (non-empty) graph. // - // If |user_policies| is empty, we interpret it as having a single anyPolicy + // If `user_policies` is empty, we interpret it as having a single anyPolicy // value. The caller may also have supplied anyPolicy explicitly. if (sk_ASN1_OBJECT_num(user_policies) == 0) { return true; @@ -539,15 +539,15 @@ bool has_explicit_policy(Span levels, continue; } if (node.parent_is_any_policy()) { - // |node|'s parent is anyPolicy and is part of "valid_policy_node_set". - // If it exists in |user_policies|, the intersection is non-empty and we + // `node`'s parent is anyPolicy and is part of "valid_policy_node_set". + // If it exists in `user_policies`, the intersection is non-empty and we // can return immediately. if (sk_ASN1_OBJECT_find(user_policies, /*out_index=*/nullptr, node.policy.get())) { return true; } } else if (i > 0) { - // |node|'s parents are concrete policies. Mark the parents reachable, + // `node`'s parents are concrete policies. Mark the parents reachable, // to be inspected by the next loop iteration. X509PolicyLevel &prev = levels[i - 1]; for (const auto &parent_policy : node.parent_policies) { @@ -571,10 +571,13 @@ int asn1_object_cmp(const ASN1_OBJECT *const *a, const ASN1_OBJECT *const *b) { int X509_policy_check(const STACK_OF(X509) *certs, const STACK_OF(ASN1_OBJECT) *user_policies, - unsigned long flags, X509 **out_current_cert) { - *out_current_cert = nullptr; + unsigned long flags, int *out_error_depth) { + *out_error_depth = 0; // Skip policy checking if the chain is just the trust anchor. + // + // TODO(crbug.com/525688038): Apply trust anchor constraints. See RFC 5937 and + // ApplyTrustAnchorConstraints from verify_certificate_chain.cc. const size_t num_certs = sk_X509_num(certs); if (num_certs <= 1) { return X509_V_OK; @@ -593,6 +596,7 @@ int X509_policy_check(const STACK_OF(X509) *certs, X509 *cert = sk_X509_value(certs, i); uint32_t ex_flags = X509_get_extension_flags(cert); if (ex_flags & EXFLAG_INVALID) { + *out_error_depth = static_cast(i); return X509_V_ERR_OUT_OF_MEM; } const bool is_self_issued = (ex_flags & EXFLAG_SI) != 0; @@ -605,22 +609,24 @@ int X509_policy_check(const STACK_OF(X509) *certs, level->set_has_any_policy(true); } - // RFC 5280, section 6.1.3, steps (d) and (e). |any_policy_allowed| is + // RFC 5280, section 6.1.3, steps (d) and (e). `any_policy_allowed` is // computed as in step (d.2). const int any_policy_allowed = inhibit_any_policy > 0 || (i > 0 && is_self_issued); if (!process_certificate_policies(cert, &*level, any_policy_allowed)) { - *out_current_cert = cert; + *out_error_depth = static_cast(i); return X509_V_ERR_INVALID_POLICY_EXTENSION; } // RFC 5280, section 6.1.3, step (f). if (explicit_policy == 0 && level->is_empty()) { + *out_error_depth = static_cast(i); return X509_V_ERR_NO_EXPLICIT_POLICY; } // Insert the completed level into the list. if (!levels.Push(*std::exchange(level, std::nullopt))) { + *out_error_depth = static_cast(i); return X509_V_ERR_OUT_OF_MEM; } level = std::nullopt; @@ -631,14 +637,14 @@ int X509_policy_check(const STACK_OF(X509) *certs, // RFC 5280, section 6.1.4, steps (a) and (b). level = process_policy_mappings(cert, &levels.back(), policy_mapping > 0); if (!level.has_value()) { - *out_current_cert = cert; + *out_error_depth = static_cast(i); return X509_V_ERR_INVALID_POLICY_EXTENSION; } } // RFC 5280, section 6.1.4, step (h-j) for non-leaves, and section 6.1.5, // step (a-b) for leaves. In the leaf case, RFC 5280 says only to update - // |explicit_policy|, but |policy_mapping| and |inhibit_any_policy| are no + // `explicit_policy`, but `policy_mapping` and `inhibit_any_policy` are no // longer read at this point, so we use the same process. if (i == 0 || !is_self_issued) { if (explicit_policy > 0) { @@ -653,7 +659,7 @@ int X509_policy_check(const STACK_OF(X509) *certs, } if (!process_policy_constraints(cert, &explicit_policy, &policy_mapping, &inhibit_any_policy)) { - *out_current_cert = cert; + *out_error_depth = static_cast(i); return X509_V_ERR_INVALID_POLICY_EXTENSION; } } @@ -661,10 +667,10 @@ int X509_policy_check(const STACK_OF(X509) *certs, // RFC 5280, section 6.1.5, step (g). We do not output the policy set, so it // is only necessary to check if the user-constrained-policy-set is not empty. if (explicit_policy == 0) { - // Build a sorted copy of |user_policies| for more efficient lookup. + // Build a sorted copy of `user_policies` for more efficient lookup. STACK_OF(ASN1_OBJECT) *user_policies_sorted = nullptr; - // |user_policies_sorted|'s contents are owned by |user_policies|, so we do - // not use |sk_ASN1_OBJECT_pop_free|. + // `user_policies_sorted`'s contents are owned by `user_policies`, so we do + // not use `sk_ASN1_OBJECT_pop_free`. Cleanup cleanup = [&] { sk_ASN1_OBJECT_free(user_policies_sorted); }; if (user_policies != nullptr) { user_policies_sorted = sk_ASN1_OBJECT_dup(user_policies); diff --git a/third_party/boringssl/src/crypto/x509/rsa_pss.cc b/third_party/boringssl/src/crypto/x509/rsa_pss.cc index 4aa212358..3a0ebebf8 100644 --- a/third_party/boringssl/src/crypto/x509/rsa_pss.cc +++ b/third_party/boringssl/src/crypto/x509/rsa_pss.cc @@ -32,26 +32,18 @@ using namespace bssl; -static int rsa_pss_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, - void *exarg) { +namespace { + +int rsa_pss_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, + void *exarg) { if (operation == ASN1_OP_FREE_PRE) { - RSA_PSS_PARAMS *pss = (RSA_PSS_PARAMS *)*pval; + RSA_PSS_PARAMS *pss = asn1_load_ptr_as(pval); X509_ALGOR_free(pss->maskHash); } return 1; } -ASN1_SEQUENCE_cb(RSA_PSS_PARAMS, rsa_pss_cb) = { - ASN1_EXP_OPT(RSA_PSS_PARAMS, hashAlgorithm, X509_ALGOR, 0), - ASN1_EXP_OPT(RSA_PSS_PARAMS, maskGenAlgorithm, X509_ALGOR, 1), - ASN1_EXP_OPT(RSA_PSS_PARAMS, saltLength, ASN1_INTEGER, 2), - ASN1_EXP_OPT(RSA_PSS_PARAMS, trailerField, ASN1_INTEGER, 3), -} ASN1_SEQUENCE_END_cb(RSA_PSS_PARAMS, RSA_PSS_PARAMS) - -IMPLEMENT_ASN1_FUNCTIONS_const(RSA_PSS_PARAMS) - - -static int rsa_pss_decode(const X509_ALGOR *alg, rsa_pss_params_t *out) { +int rsa_pss_decode(const X509_ALGOR *alg, rsa_pss_params_t *out) { if (alg->parameter == nullptr || alg->parameter->type != V_ASN1_SEQUENCE) { return 0; } @@ -65,6 +57,17 @@ static int rsa_pss_decode(const X509_ALGOR *alg, rsa_pss_params_t *out) { CBS_len(&cbs) == 0; } +} // namespace + +ASN1_SEQUENCE_cb(RSA_PSS_PARAMS, rsa_pss_cb) = { + ASN1_EXP_OPT(RSA_PSS_PARAMS, hashAlgorithm, X509_ALGOR, 0), + ASN1_EXP_OPT(RSA_PSS_PARAMS, maskGenAlgorithm, X509_ALGOR, 1), + ASN1_EXP_OPT(RSA_PSS_PARAMS, saltLength, ASN1_INTEGER, 2), + ASN1_EXP_OPT(RSA_PSS_PARAMS, trailerField, ASN1_INTEGER, 3), +} ASN1_SEQUENCE_END_cb(RSA_PSS_PARAMS, RSA_PSS_PARAMS) + +IMPLEMENT_ASN1_FUNCTIONS_const(RSA_PSS_PARAMS) + int bssl::x509_rsa_ctx_to_pss(EVP_MD_CTX *ctx, X509_ALGOR *algor) { const EVP_MD *sigmd, *mgf1md; int saltlen; @@ -100,7 +103,7 @@ int bssl::x509_rsa_ctx_to_pss(EVP_MD_CTX *ctx, X509_ALGOR *algor) { return 0; } - // Encode |params| to an |ASN1_STRING|. + // Encode `params` to an `ASN1_STRING`. uint8_t buf[128]; // The largest param fits comfortably in 128 bytes. CBB cbb; CBB_init_fixed(&cbb, buf, sizeof(buf)); @@ -117,12 +120,12 @@ int bssl::x509_rsa_ctx_to_pss(EVP_MD_CTX *ctx, X509_ALGOR *algor) { params_str.get())) { return 0; } - params_str.release(); // |X509_ALGOR_set0| took ownership. + params_str.release(); // `X509_ALGOR_set0` took ownership. return 1; } int bssl::x509_rsa_pss_to_ctx(EVP_MD_CTX *ctx, const X509_ALGOR *sigalg, - EVP_PKEY *pkey) { + const EVP_PKEY *pkey) { assert(OBJ_obj2nid(sigalg->algorithm) == NID_rsassaPss); rsa_pss_params_t params; if (!rsa_pss_decode(sigalg, ¶ms)) { @@ -154,7 +157,7 @@ int bssl::x509_print_rsa_pss_params(BIO *bp, const X509_ALGOR *sigalg, uint32_t salt_len = 0; switch (params) { case rsa_pss_none: - // |rsa_pss_decode| will never return this. + // `rsa_pss_decode` will never return this. OPENSSL_PUT_ERROR(X509, ERR_R_INTERNAL_ERROR); return 0; case rsa_pss_sha256: diff --git a/third_party/boringssl/src/crypto/x509/t_crl.cc b/third_party/boringssl/src/crypto/x509/t_crl.cc index 5ef99ab38..54f08bc06 100644 --- a/third_party/boringssl/src/crypto/x509/t_crl.cc +++ b/third_party/boringssl/src/crypto/x509/t_crl.cc @@ -41,7 +41,7 @@ int X509_CRL_print(BIO *out, const X509_CRL *x) { if (BIO_printf(out, "Certificate Revocation List (CRL):\n") <= 0 || BIO_printf(out, "%8sVersion %ld (0x%lx)\n", "", version + 1, (unsigned long)version) <= 0 || - // Note this and the other |X509_signature_print| call both print the + // Note this and the other `X509_signature_print` call both print the // outer signature algorithm, rather than printing the inner and outer // ones separately. This matches OpenSSL, though it was probably a bug. !X509_signature_print(out, sig_alg, nullptr)) { @@ -77,7 +77,7 @@ int X509_CRL_print(BIO *out, const X509_CRL *x) { return 0; } - // TODO(crbug.com/442860745): |X509_CRL_get_REVOKED| is not const-correct. + // TODO(crbug.com/442860745): `X509_CRL_get_REVOKED` is not const-correct. const STACK_OF(X509_REVOKED) *rev = X509_CRL_get_REVOKED(const_cast(x)); if (sk_X509_REVOKED_num(rev) > 0) { diff --git a/third_party/boringssl/src/crypto/x509/t_req.cc b/third_party/boringssl/src/crypto/x509/t_req.cc index 8eeac0786..6096f4781 100644 --- a/third_party/boringssl/src/crypto/x509/t_req.cc +++ b/third_party/boringssl/src/crypto/x509/t_req.cc @@ -64,7 +64,7 @@ int X509_REQ_print_ex(BIO *bio, const X509_REQ *x, unsigned long nmflags, } if (!(cflag & X509_FLAG_NO_VERSION)) { l = X509_REQ_get_version(x); - // Only zero, |X509_REQ_VERSION_1|, is valid but our parser accepts some + // Only zero, `X509_REQ_VERSION_1`, is valid but our parser accepts some // invalid values for compatibility. assert(0 <= l && l <= 2); if (BIO_printf(bio, "%8sVersion: %ld (0x%lx)\n", "", l + 1, @@ -82,7 +82,7 @@ int X509_REQ_print_ex(BIO *bio, const X509_REQ *x, unsigned long nmflags, if (!(cflag & X509_FLAG_NO_PUBKEY)) { if (BIO_write(bio, " Subject Public Key Info:\n", 33) <= 0 || BIO_printf(bio, "%12sPublic Key Algorithm: ", "") <= 0 || - i2a_ASN1_OBJECT(bio, ri->pubkey->algor.algorithm) <= 0 || + i2a_ASN1_OBJECT(bio, FromOpaque(ri->pubkey)->algor->algorithm) <= 0 || BIO_puts(bio, "\n") <= 0) { goto err; } @@ -108,7 +108,7 @@ int X509_REQ_print_ex(BIO *bio, const X509_REQ *x, unsigned long nmflags, } } else { for (size_t i = 0; i < sk_X509_ATTRIBUTE_num(sk); i++) { - // TODO(crbug.com/442860745): |X509_ATTRIBUTE| accessors are not + // TODO(crbug.com/442860745): `X509_ATTRIBUTE` accessors are not // const-correct. X509_ATTRIBUTE *a = sk_X509_ATTRIBUTE_value(sk, i); const ASN1_OBJECT *aobj = X509_ATTRIBUTE_get0_object(a); diff --git a/third_party/boringssl/src/crypto/x509/t_x509.cc b/third_party/boringssl/src/crypto/x509/t_x509.cc index 69c8d859b..a14ec59eb 100644 --- a/third_party/boringssl/src/crypto/x509/t_x509.cc +++ b/third_party/boringssl/src/crypto/x509/t_x509.cc @@ -93,7 +93,7 @@ int X509_print_ex(BIO *bp, const X509 *x, unsigned long nmflags, return 0; } } else { - ERR_clear_error(); // Clear |ASN1_INTEGER_get_uint64|'s error. + ERR_clear_error(); // Clear `ASN1_INTEGER_get_uint64`'s error. const char *neg = (serial->type == V_ASN1_NEG_INTEGER) ? " (Negative)" : ""; if (BIO_printf(bp, "\n%12s%s", "", neg) <= 0) { @@ -110,7 +110,7 @@ int X509_print_ex(BIO *bp, const X509 *x, unsigned long nmflags, } if (!(cflag & X509_FLAG_NO_SIGNAME)) { - if (X509_signature_print(bp, &impl->tbs_sig_alg, nullptr) <= 0) { + if (X509_signature_print(bp, impl->tbs_sig_alg.get(), nullptr) <= 0) { return 0; } } @@ -166,7 +166,7 @@ int X509_print_ex(BIO *bp, const X509 *x, unsigned long nmflags, if (BIO_printf(bp, "%12sPublic Key Algorithm: ", "") <= 0) { return 0; } - if (i2a_ASN1_OBJECT(bp, impl->key.algor.algorithm) <= 0) { + if (i2a_ASN1_OBJECT(bp, impl->key.algor->algorithm) <= 0) { return 0; } if (BIO_puts(bp, "\n") <= 0) { @@ -187,7 +187,7 @@ int X509_print_ex(BIO *bp, const X509 *x, unsigned long nmflags, if (BIO_printf(bp, "%8sIssuer Unique ID: ", "") <= 0) { return 0; } - if (!X509_signature_dump(bp, impl->issuerUID, 12)) { + if (!X509_signature_dump(bp, impl->issuerUID.get(), 12)) { return 0; } } @@ -195,7 +195,7 @@ int X509_print_ex(BIO *bp, const X509 *x, unsigned long nmflags, if (BIO_printf(bp, "%8sSubject Unique ID: ", "") <= 0) { return 0; } - if (!X509_signature_dump(bp, impl->subjectUID, 12)) { + if (!X509_signature_dump(bp, impl->subjectUID.get(), 12)) { return 0; } } @@ -207,7 +207,8 @@ int X509_print_ex(BIO *bp, const X509 *x, unsigned long nmflags, } if (!(cflag & X509_FLAG_NO_SIGDUMP)) { - if (X509_signature_print(bp, &impl->sig_alg, &impl->signature) <= 0) { + if (X509_signature_print(bp, impl->sig_alg.get(), impl->signature.get()) <= + 0) { return 0; } } diff --git a/third_party/boringssl/src/crypto/x509/v3_akey.cc b/third_party/boringssl/src/crypto/x509/v3_akey.cc index 9f5df55f9..2c82870ca 100644 --- a/third_party/boringssl/src/crypto/x509/v3_akey.cc +++ b/third_party/boringssl/src/crypto/x509/v3_akey.cc @@ -28,31 +28,11 @@ using namespace bssl; -static STACK_OF(CONF_VALUE) *i2v_AUTHORITY_KEYID( - const X509V3_EXT_METHOD *method, void *ext, STACK_OF(CONF_VALUE) *extlist); -static void *v2i_AUTHORITY_KEYID(const X509V3_EXT_METHOD *method, - const X509V3_CTX *ctx, - const STACK_OF(CONF_VALUE) *values); +namespace { -const X509V3_EXT_METHOD bssl::v3_akey_id = { - NID_authority_key_identifier, - X509V3_EXT_MULTILINE, - ASN1_ITEM_ref(AUTHORITY_KEYID), - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - i2v_AUTHORITY_KEYID, - v2i_AUTHORITY_KEYID, - nullptr, - nullptr, - nullptr, -}; - -static STACK_OF(CONF_VALUE) *i2v_AUTHORITY_KEYID( - const X509V3_EXT_METHOD *method, void *ext, STACK_OF(CONF_VALUE) *extlist) { +STACK_OF(CONF_VALUE) *i2v_AUTHORITY_KEYID(const X509V3_EXT_METHOD *method, + void *ext, + STACK_OF(CONF_VALUE) *extlist) { const AUTHORITY_KEYID *akeyid = reinterpret_cast(ext); int extlist_was_null = extlist == nullptr; @@ -86,36 +66,29 @@ static STACK_OF(CONF_VALUE) *i2v_AUTHORITY_KEYID( return nullptr; } -// Currently two options: keyid: use the issuers subject keyid, the value -// 'always' means its is an error if the issuer certificate doesn't have a -// key id. issuer: use the issuers cert issuer and serial number. The default -// is to only use this if keyid is not present. With the option 'always' this -// is always included. - -static void *v2i_AUTHORITY_KEYID(const X509V3_EXT_METHOD *method, - const X509V3_CTX *ctx, - const STACK_OF(CONF_VALUE) *values) { - char keyid = 0, issuer = 0; - int j; - ASN1_OCTET_STRING *ikeyid = nullptr; - X509_NAME *isname = nullptr; - GENERAL_NAMES *gens = nullptr; - GENERAL_NAME *gen = nullptr; - ASN1_INTEGER *serial = nullptr; - const X509 *cert; - AUTHORITY_KEYID *akeyid; - - for (size_t i = 0; i < sk_CONF_VALUE_num(values); i++) { - const CONF_VALUE *cnf = sk_CONF_VALUE_value(values, i); +// Currently two options: +// +// - keyid: Use the issuer's subject key ID. The value 'always' means it's an +// error if the issuer certificate doesn't have one. +// +// - issuer: Use the issuer's issuer and serial number. The default is to only +// use this if the key ID is not present. The value 'always' means it's always +// included. +void *v2i_AUTHORITY_KEYID(const X509V3_EXT_METHOD *method, + const X509V3_CTX *ctx, + const STACK_OF(CONF_VALUE) *values) { + enum Option { kOff = 0, kOn = 1, kAlways = 2 }; + Option use_key_id = kOff, use_issuer = kOff; + for (const CONF_VALUE *cnf : values) { if (!strcmp(cnf->name, "keyid")) { - keyid = 1; + use_key_id = kOn; if (cnf->value && !strcmp(cnf->value, "always")) { - keyid = 2; + use_key_id = kAlways; } } else if (!strcmp(cnf->name, "issuer")) { - issuer = 1; + use_issuer = kOn; if (cnf->value && !strcmp(cnf->value, "always")) { - issuer = 2; + use_issuer = kAlways; } } else { OPENSSL_PUT_ERROR(X509V3, X509V3_R_UNKNOWN_OPTION); @@ -132,51 +105,69 @@ static void *v2i_AUTHORITY_KEYID(const X509V3_EXT_METHOD *method, return nullptr; } - cert = ctx->issuer_cert; - - if (keyid) { - j = X509_get_ext_by_NID(cert, NID_subject_key_identifier, -1); - const X509_EXTENSION *ext; - if ((j >= 0) && (ext = X509_get_ext(cert, j))) { - ikeyid = reinterpret_cast(X509V3_EXT_d2i(ext)); + UniquePtr key_id; + if (use_key_id != kOff) { + int critical; + key_id.reset(static_cast(X509_get_ext_d2i( + ctx->issuer_cert, NID_subject_key_identifier, &critical, nullptr))); + if (key_id == nullptr && critical != -1) { + return nullptr; // Syntax error in the extension. } - if (keyid == 2 && !ikeyid) { + if (use_key_id == kAlways && key_id == nullptr) { OPENSSL_PUT_ERROR(X509V3, X509V3_R_UNABLE_TO_GET_ISSUER_KEYID); return nullptr; } } - if ((issuer && !ikeyid) || (issuer == 2)) { - isname = X509_NAME_dup(X509_get_issuer_name(cert)); - serial = ASN1_INTEGER_dup(X509_get0_serialNumber(cert)); - if (!isname || !serial) { + UniquePtr serial; + UniquePtr issuer_gens; + if ((use_issuer == kOn && key_id == nullptr) || use_issuer == kAlways) { + UniquePtr issuer_name( + X509_NAME_dup(X509_get_issuer_name(ctx->issuer_cert))); + serial.reset(ASN1_INTEGER_dup(X509_get0_serialNumber(ctx->issuer_cert))); + if (issuer_name == nullptr || serial == nullptr) { OPENSSL_PUT_ERROR(X509V3, X509V3_R_UNABLE_TO_GET_ISSUER_DETAILS); - goto err; + return nullptr; } - } - - if (!(akeyid = AUTHORITY_KEYID_new())) { - goto err; - } - - if (isname) { - if (!(gens = sk_GENERAL_NAME_new_null()) || !(gen = GENERAL_NAME_new()) || - !sk_GENERAL_NAME_push(gens, gen)) { - goto err; + // AKID wraps the issuer name in a GeneralNames structure. + UniquePtr gen(GENERAL_NAME_new()); + if (gen == nullptr) { + return nullptr; } gen->type = GEN_DIRNAME; - gen->d.dirn = isname; + gen->d.directoryName = issuer_name.release(); + issuer_gens.reset(sk_GENERAL_NAME_new_null()); + if (issuer_gens == nullptr || + !PushToStack(issuer_gens.get(), std::move(gen))) { + return nullptr; + } } - akeyid->issuer = gens; - akeyid->serial = serial; - akeyid->keyid = ikeyid; + UniquePtr akid(AUTHORITY_KEYID_new()); + if (akid == nullptr) { + return nullptr; + } + akid->issuer = issuer_gens.release(); + akid->serial = serial.release(); + akid->keyid = key_id.release(); + return akid.release(); +} - return akeyid; +} // namespace -err: - X509_NAME_free(isname); - ASN1_INTEGER_free(serial); - ASN1_OCTET_STRING_free(ikeyid); - return nullptr; -} +const X509V3_EXT_METHOD bssl::v3_akey_id = { + NID_authority_key_identifier, + X509V3_EXT_MULTILINE, + ASN1_ITEM_ref(AUTHORITY_KEYID), + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + i2v_AUTHORITY_KEYID, + v2i_AUTHORITY_KEYID, + nullptr, + nullptr, + nullptr, +}; diff --git a/third_party/boringssl/src/crypto/x509/v3_alt.cc b/third_party/boringssl/src/crypto/x509/v3_alt.cc index 8aa2da8ff..ffc21778d 100644 --- a/third_party/boringssl/src/crypto/x509/v3_alt.cc +++ b/third_party/boringssl/src/crypto/x509/v3_alt.cc @@ -27,24 +27,315 @@ using namespace bssl; -static void *v2i_subject_alt(const X509V3_EXT_METHOD *method, - const X509V3_CTX *ctx, - const STACK_OF(CONF_VALUE) *nval); -static void *v2i_issuer_alt(const X509V3_EXT_METHOD *method, - const X509V3_CTX *ctx, - const STACK_OF(CONF_VALUE) *nval); -static int copy_email(const X509V3_CTX *ctx, GENERAL_NAMES *gens, int move_p); -static int copy_issuer(const X509V3_CTX *ctx, GENERAL_NAMES *gens); -static int do_othername(GENERAL_NAME *gen, const char *value, - const X509V3_CTX *ctx); -static int do_dirname(GENERAL_NAME *gen, const char *value, - const X509V3_CTX *ctx); - -static STACK_OF(CONF_VALUE) *i2v_GENERAL_NAMES_cb( - const X509V3_EXT_METHOD *method, void *ext, STACK_OF(CONF_VALUE) *ret) { +namespace { + +STACK_OF(CONF_VALUE) *i2v_GENERAL_NAMES_cb(const X509V3_EXT_METHOD *method, + void *ext, + STACK_OF(CONF_VALUE) *ret) { return i2v_GENERAL_NAMES(method, reinterpret_cast(ext), ret); } +// Append subject altname of issuer to issuer alt name of subject +int copy_issuer(const X509V3_CTX *ctx, GENERAL_NAMES *gens) { + if (ctx && (ctx->flags == X509V3_CTX_TEST)) { + return 1; + } + if (!ctx || !ctx->issuer_cert) { + OPENSSL_PUT_ERROR(X509V3, X509V3_R_NO_ISSUER_DETAILS); + return 0; + } + int i = X509_get_ext_by_NID(ctx->issuer_cert, NID_subject_alt_name, -1); + if (i < 0) { + return 1; + } + + int ret = 0; + GENERAL_NAMES *ialt = nullptr; + X509_EXTENSION *ext; + if (!(ext = X509_get_ext(ctx->issuer_cert, i)) || + !(ialt = reinterpret_cast(X509V3_EXT_d2i(ext)))) { + OPENSSL_PUT_ERROR(X509V3, X509V3_R_ISSUER_DECODE_ERROR); + goto err; + } + + for (size_t j = 0; j < sk_GENERAL_NAME_num(ialt); j++) { + GENERAL_NAME *gen = sk_GENERAL_NAME_value(ialt, j); + if (!sk_GENERAL_NAME_push(gens, gen)) { + goto err; + } + // Ownership of `gen` has moved from `ialt` to `gens`. + sk_GENERAL_NAME_set(ialt, j, nullptr); + } + + ret = 1; + +err: + GENERAL_NAMES_free(ialt); + return ret; +} + +void *v2i_issuer_alt(const X509V3_EXT_METHOD *method, const X509V3_CTX *ctx, + const STACK_OF(CONF_VALUE) *nval) { + GENERAL_NAMES *gens = sk_GENERAL_NAME_new_null(); + if (gens == nullptr) { + return nullptr; + } + for (size_t i = 0; i < sk_CONF_VALUE_num(nval); i++) { + const CONF_VALUE *cnf = sk_CONF_VALUE_value(nval, i); + if (x509v3_conf_name_matches(cnf->name, "issuer") && cnf->value && + !strcmp(cnf->value, "copy")) { + if (!copy_issuer(ctx, gens)) { + goto err; + } + } else { + GENERAL_NAME *gen = v2i_GENERAL_NAME(method, ctx, cnf); + if (gen == nullptr || !sk_GENERAL_NAME_push(gens, gen)) { + GENERAL_NAME_free(gen); + goto err; + } + } + } + return gens; +err: + sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free); + return nullptr; +} + +// Copy any email addresses in a certificate or request to GENERAL_NAMES +int copy_email(const X509V3_CTX *ctx, GENERAL_NAMES *gens, int move_p) { + X509_NAME *nm; + ASN1_IA5STRING *email = nullptr; + X509_NAME_ENTRY *ne; + GENERAL_NAME *gen = nullptr; + int i; + if (ctx != nullptr && ctx->flags == X509V3_CTX_TEST) { + return 1; + } + if (!ctx || (!ctx->subject_cert && !ctx->subject_req)) { + OPENSSL_PUT_ERROR(X509V3, X509V3_R_NO_SUBJECT_DETAILS); + goto err; + } + // Find the subject name + if (ctx->subject_cert) { + nm = X509_get_subject_name(ctx->subject_cert); + } else { + nm = X509_REQ_get_subject_name(ctx->subject_req); + } + + // Now add any email address(es) to STACK + i = -1; + while ((i = X509_NAME_get_index_by_NID(nm, NID_pkcs9_emailAddress, i)) >= 0) { + ne = X509_NAME_get_entry(nm, i); + email = ASN1_STRING_dup(X509_NAME_ENTRY_get_data(ne)); + if (move_p) { + X509_NAME_delete_entry(nm, i); + X509_NAME_ENTRY_free(ne); + i--; + } + if (!email || !(gen = GENERAL_NAME_new())) { + goto err; + } + gen->d.ia5 = email; + email = nullptr; + gen->type = GEN_EMAIL; + if (!sk_GENERAL_NAME_push(gens, gen)) { + goto err; + } + gen = nullptr; + } + + return 1; + +err: + GENERAL_NAME_free(gen); + ASN1_IA5STRING_free(email); + return 0; +} + +void *v2i_subject_alt(const X509V3_EXT_METHOD *method, const X509V3_CTX *ctx, + const STACK_OF(CONF_VALUE) *nval) { + GENERAL_NAMES *gens = sk_GENERAL_NAME_new_null(); + if (gens == nullptr) { + return nullptr; + } + for (size_t i = 0; i < sk_CONF_VALUE_num(nval); i++) { + const CONF_VALUE *cnf = sk_CONF_VALUE_value(nval, i); + if (x509v3_conf_name_matches(cnf->name, "email") && cnf->value && + !strcmp(cnf->value, "copy")) { + if (!copy_email(ctx, gens, 0)) { + goto err; + } + } else if (x509v3_conf_name_matches(cnf->name, "email") && cnf->value && + !strcmp(cnf->value, "move")) { + if (!copy_email(ctx, gens, 1)) { + goto err; + } + } else { + GENERAL_NAME *gen = v2i_GENERAL_NAME(method, ctx, cnf); + if (gen == nullptr || !sk_GENERAL_NAME_push(gens, gen)) { + GENERAL_NAME_free(gen); + goto err; + } + } + } + return gens; +err: + sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free); + return nullptr; +} + +int do_othername(GENERAL_NAME *gen, const char *value, const X509V3_CTX *ctx) { + const char *semicolon = strchr(value, ';'); + if (semicolon == nullptr) { + return 0; + } + + OTHERNAME *name = OTHERNAME_new(); + if (name == nullptr) { + return 0; + } + + char *objtmp = OPENSSL_strndup(value, semicolon - value); + if (objtmp == nullptr) { + goto err; + } + ASN1_OBJECT_free(name->type_id); + name->type_id = OBJ_txt2obj(objtmp, /*dont_search_names=*/0); + OPENSSL_free(objtmp); + if (name->type_id == nullptr) { + goto err; + } + + ASN1_TYPE_free(name->value); + name->value = ASN1_generate_v3(semicolon + 1, ctx); + if (name->value == nullptr) { + goto err; + } + + gen->type = GEN_OTHERNAME; + gen->d.otherName = name; + return 1; + +err: + OTHERNAME_free(name); + return 0; +} + +int do_dirname(GENERAL_NAME *gen, const char *value, const X509V3_CTX *ctx) { + int ret = 0; + const STACK_OF(CONF_VALUE) *sk = X509V3_get_section(ctx, value); + X509_NAME *nm = X509_NAME_new(); + if (nm == nullptr) { + goto err; + } + if (sk == nullptr) { + OPENSSL_PUT_ERROR(X509V3, X509V3_R_SECTION_NOT_FOUND); + ERR_add_error_data(2, "section=", value); + goto err; + } + // FIXME: should allow other character types... + if (!X509V3_NAME_from_section(nm, sk, MBSTRING_ASC)) { + goto err; + } + gen->type = GEN_DIRNAME; + gen->d.dirn = nm; + ret = 1; + +err: + if (!ret) { + X509_NAME_free(nm); + } + return ret; +} + +GENERAL_NAME *a2i_GENERAL_NAME(GENERAL_NAME *out, + const X509V3_EXT_METHOD *method, + const X509V3_CTX *ctx, int gen_type, + const char *value, int is_nc) { + if (!value) { + OPENSSL_PUT_ERROR(X509V3, X509V3_R_MISSING_VALUE); + return nullptr; + } + + GENERAL_NAME *gen = nullptr; + if (out) { + gen = out; + } else { + gen = GENERAL_NAME_new(); + if (gen == nullptr) { + return nullptr; + } + } + + switch (gen_type) { + case GEN_URI: + case GEN_EMAIL: + case GEN_DNS: { + ASN1_IA5STRING *str = ASN1_IA5STRING_new(); + if (str == nullptr || !ASN1_STRING_set(str, value, strlen(value))) { + ASN1_STRING_free(str); + goto err; + } + gen->type = gen_type; + gen->d.ia5 = str; + break; + } + + case GEN_RID: { + ASN1_OBJECT *obj; + if (!(obj = OBJ_txt2obj(value, 0))) { + OPENSSL_PUT_ERROR(X509V3, X509V3_R_BAD_OBJECT); + ERR_add_error_data(2, "value=", value); + goto err; + } + gen->type = GEN_RID; + gen->d.rid = obj; + break; + } + + case GEN_IPADD: + gen->type = GEN_IPADD; + if (is_nc) { + gen->d.ip = a2i_IPADDRESS_NC(value); + } else { + gen->d.ip = a2i_IPADDRESS(value); + } + if (gen->d.ip == nullptr) { + OPENSSL_PUT_ERROR(X509V3, X509V3_R_BAD_IP_ADDRESS); + ERR_add_error_data(2, "value=", value); + goto err; + } + break; + + case GEN_DIRNAME: + if (!do_dirname(gen, value, ctx)) { + OPENSSL_PUT_ERROR(X509V3, X509V3_R_DIRNAME_ERROR); + goto err; + } + break; + + case GEN_OTHERNAME: + if (!do_othername(gen, value, ctx)) { + OPENSSL_PUT_ERROR(X509V3, X509V3_R_OTHERNAME_ERROR); + goto err; + } + break; + default: + OPENSSL_PUT_ERROR(X509V3, X509V3_R_UNSUPPORTED_TYPE); + goto err; + } + + return gen; + +err: + if (!out) { + GENERAL_NAME_free(gen); + } + return nullptr; +} + +} // namespace + const X509V3_EXT_METHOD bssl::v3_subject_alt_name = { NID_subject_alt_name, 0, @@ -121,7 +412,7 @@ STACK_OF(CONF_VALUE) *i2v_GENERAL_NAME(const X509V3_EXT_METHOD *method, const GENERAL_NAME *gen, STACK_OF(CONF_VALUE) *ret) { // Note the error-handling for this function relies on there being at most - // one |X509V3_add_value| call. If there were two and the second failed, we + // one `X509V3_add_value` call. If there were two and the second failed, we // would need to sometimes free the first call's result. unsigned char *p; char oline[256], htmp[5]; @@ -194,231 +485,78 @@ STACK_OF(CONF_VALUE) *i2v_GENERAL_NAME(const X509V3_EXT_METHOD *method, if (!X509V3_add_value("IP Address", oline, &ret)) { return nullptr; } - break; - - case GEN_RID: - i2t_ASN1_OBJECT(oline, 256, gen->d.rid); - if (!X509V3_add_value("Registered ID", oline, &ret)) { - return nullptr; - } - break; - } - return ret; -} - -int GENERAL_NAME_print(BIO *out, const GENERAL_NAME *gen) { - switch (gen->type) { - case GEN_OTHERNAME: - BIO_printf(out, "othername:"); - break; - - case GEN_X400: - BIO_printf(out, "X400Name:"); - break; - - case GEN_EDIPARTY: - // Maybe fix this: it is supported now - BIO_printf(out, "EdiPartyName:"); - break; - - case GEN_EMAIL: - BIO_printf(out, "email:"); - ASN1_STRING_print(out, gen->d.ia5); - break; - - case GEN_DNS: - BIO_printf(out, "DNS:"); - ASN1_STRING_print(out, gen->d.ia5); - break; - - case GEN_URI: - BIO_printf(out, "URI:"); - ASN1_STRING_print(out, gen->d.ia5); - break; - - case GEN_DIRNAME: - BIO_printf(out, "DirName: "); - X509_NAME_print_ex(out, gen->d.dirn, 0, XN_FLAG_ONELINE); - break; - - case GEN_IPADD: { - const unsigned char *p = gen->d.ip->data; - if (gen->d.ip->length == 4) { - BIO_printf(out, "IP Address:%d.%d.%d.%d", p[0], p[1], p[2], p[3]); - } else if (gen->d.ip->length == 16) { - BIO_printf(out, "IP Address"); - for (int i = 0; i < 8; i++) { - uint16_t v = ((uint16_t)p[0] << 8) | p[1]; - BIO_printf(out, ":%X", v); - p += 2; - } - BIO_puts(out, "\n"); - } else { - BIO_printf(out, "IP Address:"); - break; - } - break; - } - - case GEN_RID: - BIO_printf(out, "Registered ID"); - i2a_ASN1_OBJECT(out, gen->d.rid); - break; - } - return 1; -} - -static void *v2i_issuer_alt(const X509V3_EXT_METHOD *method, - const X509V3_CTX *ctx, - const STACK_OF(CONF_VALUE) *nval) { - GENERAL_NAMES *gens = sk_GENERAL_NAME_new_null(); - if (gens == nullptr) { - return nullptr; - } - for (size_t i = 0; i < sk_CONF_VALUE_num(nval); i++) { - const CONF_VALUE *cnf = sk_CONF_VALUE_value(nval, i); - if (x509v3_conf_name_matches(cnf->name, "issuer") && cnf->value && - !strcmp(cnf->value, "copy")) { - if (!copy_issuer(ctx, gens)) { - goto err; - } - } else { - GENERAL_NAME *gen = v2i_GENERAL_NAME(method, ctx, cnf); - if (gen == nullptr || !sk_GENERAL_NAME_push(gens, gen)) { - GENERAL_NAME_free(gen); - goto err; - } - } - } - return gens; -err: - sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free); - return nullptr; -} - -// Append subject altname of issuer to issuer alt name of subject - -static int copy_issuer(const X509V3_CTX *ctx, GENERAL_NAMES *gens) { - if (ctx && (ctx->flags == X509V3_CTX_TEST)) { - return 1; - } - if (!ctx || !ctx->issuer_cert) { - OPENSSL_PUT_ERROR(X509V3, X509V3_R_NO_ISSUER_DETAILS); - return 0; - } - int i = X509_get_ext_by_NID(ctx->issuer_cert, NID_subject_alt_name, -1); - if (i < 0) { - return 1; - } - - int ret = 0; - GENERAL_NAMES *ialt = nullptr; - X509_EXTENSION *ext; - if (!(ext = X509_get_ext(ctx->issuer_cert, i)) || - !(ialt = reinterpret_cast(X509V3_EXT_d2i(ext)))) { - OPENSSL_PUT_ERROR(X509V3, X509V3_R_ISSUER_DECODE_ERROR); - goto err; - } - - for (size_t j = 0; j < sk_GENERAL_NAME_num(ialt); j++) { - GENERAL_NAME *gen = sk_GENERAL_NAME_value(ialt, j); - if (!sk_GENERAL_NAME_push(gens, gen)) { - goto err; - } - // Ownership of |gen| has moved from |ialt| to |gens|. - sk_GENERAL_NAME_set(ialt, j, nullptr); - } - - ret = 1; - -err: - GENERAL_NAMES_free(ialt); - return ret; -} - -static void *v2i_subject_alt(const X509V3_EXT_METHOD *method, - const X509V3_CTX *ctx, - const STACK_OF(CONF_VALUE) *nval) { - GENERAL_NAMES *gens = sk_GENERAL_NAME_new_null(); - if (gens == nullptr) { - return nullptr; - } - for (size_t i = 0; i < sk_CONF_VALUE_num(nval); i++) { - const CONF_VALUE *cnf = sk_CONF_VALUE_value(nval, i); - if (x509v3_conf_name_matches(cnf->name, "email") && cnf->value && - !strcmp(cnf->value, "copy")) { - if (!copy_email(ctx, gens, 0)) { - goto err; - } - } else if (x509v3_conf_name_matches(cnf->name, "email") && cnf->value && - !strcmp(cnf->value, "move")) { - if (!copy_email(ctx, gens, 1)) { - goto err; - } - } else { - GENERAL_NAME *gen = v2i_GENERAL_NAME(method, ctx, cnf); - if (gen == nullptr || !sk_GENERAL_NAME_push(gens, gen)) { - GENERAL_NAME_free(gen); - goto err; + break; + + case GEN_RID: + i2t_ASN1_OBJECT(oline, 256, gen->d.rid); + if (!X509V3_add_value("Registered ID", oline, &ret)) { + return nullptr; } - } + break; } - return gens; -err: - sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free); - return nullptr; + return ret; } -// Copy any email addresses in a certificate or request to GENERAL_NAMES +int GENERAL_NAME_print(BIO *out, const GENERAL_NAME *gen) { + switch (gen->type) { + case GEN_OTHERNAME: + BIO_printf(out, "othername:"); + break; -static int copy_email(const X509V3_CTX *ctx, GENERAL_NAMES *gens, int move_p) { - X509_NAME *nm; - ASN1_IA5STRING *email = nullptr; - X509_NAME_ENTRY *ne; - GENERAL_NAME *gen = nullptr; - int i; - if (ctx != nullptr && ctx->flags == X509V3_CTX_TEST) { - return 1; - } - if (!ctx || (!ctx->subject_cert && !ctx->subject_req)) { - OPENSSL_PUT_ERROR(X509V3, X509V3_R_NO_SUBJECT_DETAILS); - goto err; - } - // Find the subject name - if (ctx->subject_cert) { - nm = X509_get_subject_name(ctx->subject_cert); - } else { - nm = X509_REQ_get_subject_name(ctx->subject_req); - } + case GEN_X400: + BIO_printf(out, "X400Name:"); + break; - // Now add any email address(es) to STACK - i = -1; - while ((i = X509_NAME_get_index_by_NID(nm, NID_pkcs9_emailAddress, i)) >= 0) { - ne = X509_NAME_get_entry(nm, i); - email = ASN1_STRING_dup(X509_NAME_ENTRY_get_data(ne)); - if (move_p) { - X509_NAME_delete_entry(nm, i); - X509_NAME_ENTRY_free(ne); - i--; - } - if (!email || !(gen = GENERAL_NAME_new())) { - goto err; - } - gen->d.ia5 = email; - email = nullptr; - gen->type = GEN_EMAIL; - if (!sk_GENERAL_NAME_push(gens, gen)) { - goto err; + case GEN_EDIPARTY: + // Maybe fix this: it is supported now + BIO_printf(out, "EdiPartyName:"); + break; + + case GEN_EMAIL: + BIO_printf(out, "email:"); + ASN1_STRING_print(out, gen->d.ia5); + break; + + case GEN_DNS: + BIO_printf(out, "DNS:"); + ASN1_STRING_print(out, gen->d.ia5); + break; + + case GEN_URI: + BIO_printf(out, "URI:"); + ASN1_STRING_print(out, gen->d.ia5); + break; + + case GEN_DIRNAME: + BIO_printf(out, "DirName: "); + X509_NAME_print_ex(out, gen->d.dirn, 0, XN_FLAG_ONELINE); + break; + + case GEN_IPADD: { + const unsigned char *p = gen->d.ip->data; + if (gen->d.ip->length == 4) { + BIO_printf(out, "IP Address:%d.%d.%d.%d", p[0], p[1], p[2], p[3]); + } else if (gen->d.ip->length == 16) { + BIO_printf(out, "IP Address"); + for (int i = 0; i < 8; i++) { + uint16_t v = ((uint16_t)p[0] << 8) | p[1]; + BIO_printf(out, ":%X", v); + p += 2; + } + BIO_puts(out, "\n"); + } else { + BIO_printf(out, "IP Address:"); + break; + } + break; } - gen = nullptr; - } + case GEN_RID: + BIO_printf(out, "Registered ID"); + i2a_ASN1_OBJECT(out, gen->d.rid); + break; + } return 1; - -err: - GENERAL_NAME_free(gen); - ASN1_IA5STRING_free(email); - return 0; } GENERAL_NAMES *bssl::v2i_GENERAL_NAMES(const X509V3_EXT_METHOD *method, @@ -448,92 +586,6 @@ GENERAL_NAME *bssl::v2i_GENERAL_NAME(const X509V3_EXT_METHOD *method, return v2i_GENERAL_NAME_ex(nullptr, method, ctx, cnf, 0); } -static GENERAL_NAME *a2i_GENERAL_NAME(GENERAL_NAME *out, - const X509V3_EXT_METHOD *method, - const X509V3_CTX *ctx, int gen_type, - const char *value, int is_nc) { - if (!value) { - OPENSSL_PUT_ERROR(X509V3, X509V3_R_MISSING_VALUE); - return nullptr; - } - - GENERAL_NAME *gen = nullptr; - if (out) { - gen = out; - } else { - gen = GENERAL_NAME_new(); - if (gen == nullptr) { - return nullptr; - } - } - - switch (gen_type) { - case GEN_URI: - case GEN_EMAIL: - case GEN_DNS: { - ASN1_IA5STRING *str = ASN1_IA5STRING_new(); - if (str == nullptr || !ASN1_STRING_set(str, value, strlen(value))) { - ASN1_STRING_free(str); - goto err; - } - gen->type = gen_type; - gen->d.ia5 = str; - break; - } - - case GEN_RID: { - ASN1_OBJECT *obj; - if (!(obj = OBJ_txt2obj(value, 0))) { - OPENSSL_PUT_ERROR(X509V3, X509V3_R_BAD_OBJECT); - ERR_add_error_data(2, "value=", value); - goto err; - } - gen->type = GEN_RID; - gen->d.rid = obj; - break; - } - - case GEN_IPADD: - gen->type = GEN_IPADD; - if (is_nc) { - gen->d.ip = a2i_IPADDRESS_NC(value); - } else { - gen->d.ip = a2i_IPADDRESS(value); - } - if (gen->d.ip == nullptr) { - OPENSSL_PUT_ERROR(X509V3, X509V3_R_BAD_IP_ADDRESS); - ERR_add_error_data(2, "value=", value); - goto err; - } - break; - - case GEN_DIRNAME: - if (!do_dirname(gen, value, ctx)) { - OPENSSL_PUT_ERROR(X509V3, X509V3_R_DIRNAME_ERROR); - goto err; - } - break; - - case GEN_OTHERNAME: - if (!do_othername(gen, value, ctx)) { - OPENSSL_PUT_ERROR(X509V3, X509V3_R_OTHERNAME_ERROR); - goto err; - } - break; - default: - OPENSSL_PUT_ERROR(X509V3, X509V3_R_UNSUPPORTED_TYPE); - goto err; - } - - return gen; - -err: - if (!out) { - GENERAL_NAME_free(gen); - } - return nullptr; -} - GENERAL_NAME *bssl::v2i_GENERAL_NAME_ex(GENERAL_NAME *out, const X509V3_EXT_METHOD *method, const X509V3_CTX *ctx, @@ -568,69 +620,3 @@ GENERAL_NAME *bssl::v2i_GENERAL_NAME_ex(GENERAL_NAME *out, return a2i_GENERAL_NAME(out, method, ctx, type, value, is_nc); } - -static int do_othername(GENERAL_NAME *gen, const char *value, - const X509V3_CTX *ctx) { - const char *semicolon = strchr(value, ';'); - if (semicolon == nullptr) { - return 0; - } - - OTHERNAME *name = OTHERNAME_new(); - if (name == nullptr) { - return 0; - } - - char *objtmp = OPENSSL_strndup(value, semicolon - value); - if (objtmp == nullptr) { - goto err; - } - ASN1_OBJECT_free(name->type_id); - name->type_id = OBJ_txt2obj(objtmp, /*dont_search_names=*/0); - OPENSSL_free(objtmp); - if (name->type_id == nullptr) { - goto err; - } - - ASN1_TYPE_free(name->value); - name->value = ASN1_generate_v3(semicolon + 1, ctx); - if (name->value == nullptr) { - goto err; - } - - gen->type = GEN_OTHERNAME; - gen->d.otherName = name; - return 1; - -err: - OTHERNAME_free(name); - return 0; -} - -static int do_dirname(GENERAL_NAME *gen, const char *value, - const X509V3_CTX *ctx) { - int ret = 0; - const STACK_OF(CONF_VALUE) *sk = X509V3_get_section(ctx, value); - X509_NAME *nm = X509_NAME_new(); - if (nm == nullptr) { - goto err; - } - if (sk == nullptr) { - OPENSSL_PUT_ERROR(X509V3, X509V3_R_SECTION_NOT_FOUND); - ERR_add_error_data(2, "section=", value); - goto err; - } - // FIXME: should allow other character types... - if (!X509V3_NAME_from_section(nm, sk, MBSTRING_ASC)) { - goto err; - } - gen->type = GEN_DIRNAME; - gen->d.dirn = nm; - ret = 1; - -err: - if (!ret) { - X509_NAME_free(nm); - } - return ret; -} diff --git a/third_party/boringssl/src/crypto/x509/v3_bcons.cc b/third_party/boringssl/src/crypto/x509/v3_bcons.cc index 61194001b..d70c8d0a5 100644 --- a/third_party/boringssl/src/crypto/x509/v3_bcons.cc +++ b/third_party/boringssl/src/crypto/x509/v3_bcons.cc @@ -27,29 +27,6 @@ using namespace bssl; -static STACK_OF(CONF_VALUE) *i2v_BASIC_CONSTRAINTS( - const X509V3_EXT_METHOD *method, void *ext, STACK_OF(CONF_VALUE) *extlist); -static void *v2i_BASIC_CONSTRAINTS(const X509V3_EXT_METHOD *method, - const X509V3_CTX *ctx, - const STACK_OF(CONF_VALUE) *values); - -const X509V3_EXT_METHOD bssl::v3_bcons = { - NID_basic_constraints, - 0, - ASN1_ITEM_ref(BASIC_CONSTRAINTS), - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - i2v_BASIC_CONSTRAINTS, - v2i_BASIC_CONSTRAINTS, - nullptr, - nullptr, - nullptr, -}; - ASN1_SEQUENCE(BASIC_CONSTRAINTS) = { ASN1_OPT(BASIC_CONSTRAINTS, ca, ASN1_FBOOLEAN), ASN1_OPT(BASIC_CONSTRAINTS, pathlen, ASN1_INTEGER), @@ -57,8 +34,11 @@ ASN1_SEQUENCE(BASIC_CONSTRAINTS) = { IMPLEMENT_ASN1_FUNCTIONS_const(BASIC_CONSTRAINTS) -static STACK_OF(CONF_VALUE) *i2v_BASIC_CONSTRAINTS( - const X509V3_EXT_METHOD *method, void *ext, STACK_OF(CONF_VALUE) *extlist) { +namespace { + +STACK_OF(CONF_VALUE) *i2v_BASIC_CONSTRAINTS(const X509V3_EXT_METHOD *method, + void *ext, + STACK_OF(CONF_VALUE) *extlist) { const BASIC_CONSTRAINTS *bcons = reinterpret_cast(ext); X509V3_add_value_bool("CA", bcons->ca, &extlist); @@ -66,9 +46,9 @@ static STACK_OF(CONF_VALUE) *i2v_BASIC_CONSTRAINTS( return extlist; } -static void *v2i_BASIC_CONSTRAINTS(const X509V3_EXT_METHOD *method, - const X509V3_CTX *ctx, - const STACK_OF(CONF_VALUE) *values) { +void *v2i_BASIC_CONSTRAINTS(const X509V3_EXT_METHOD *method, + const X509V3_CTX *ctx, + const STACK_OF(CONF_VALUE) *values) { BASIC_CONSTRAINTS *bcons = nullptr; if (!(bcons = BASIC_CONSTRAINTS_new())) { return nullptr; @@ -94,3 +74,22 @@ static void *v2i_BASIC_CONSTRAINTS(const X509V3_EXT_METHOD *method, BASIC_CONSTRAINTS_free(bcons); return nullptr; } + +} // namespace + +const X509V3_EXT_METHOD bssl::v3_bcons = { + NID_basic_constraints, + 0, + ASN1_ITEM_ref(BASIC_CONSTRAINTS), + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + i2v_BASIC_CONSTRAINTS, + v2i_BASIC_CONSTRAINTS, + nullptr, + nullptr, + nullptr, +}; diff --git a/third_party/boringssl/src/crypto/x509/v3_bitst.cc b/third_party/boringssl/src/crypto/x509/v3_bitst.cc index 9b3acc8fb..2c4abcaf4 100644 --- a/third_party/boringssl/src/crypto/x509/v3_bitst.cc +++ b/third_party/boringssl/src/crypto/x509/v3_bitst.cc @@ -25,14 +25,16 @@ using namespace bssl; -static const BIT_STRING_BITNAME ns_cert_type_table[] = { +namespace { + +const BIT_STRING_BITNAME ns_cert_type_table[] = { {0, "SSL Client", "client"}, {1, "SSL Server", "server"}, {2, "S/MIME", "email"}, {3, "Object Signing", "objsign"}, {4, "Unused", "reserved"}, {5, "SSL CA", "sslCA"}, {6, "S/MIME CA", "emailCA"}, {7, "Object Signing CA", "objCA"}, {-1, nullptr, nullptr}}; -static const BIT_STRING_BITNAME key_usage_type_table[] = { +const BIT_STRING_BITNAME key_usage_type_table[] = { {0, "Digital Signature", "digitalSignature"}, {1, "Non Repudiation", "nonRepudiation"}, {2, "Key Encipherment", "keyEncipherment"}, @@ -44,8 +46,9 @@ static const BIT_STRING_BITNAME key_usage_type_table[] = { {8, "Decipher Only", "decipherOnly"}, {-1, nullptr, nullptr}}; -static STACK_OF(CONF_VALUE) *i2v_ASN1_BIT_STRING( - const X509V3_EXT_METHOD *method, void *ext, STACK_OF(CONF_VALUE) *ret) { +STACK_OF(CONF_VALUE) *i2v_ASN1_BIT_STRING(const X509V3_EXT_METHOD *method, + void *ext, + STACK_OF(CONF_VALUE) *ret) { const ASN1_BIT_STRING *bits = reinterpret_cast(ext); const BIT_STRING_BITNAME *bnam; for (bnam = reinterpret_cast(method->usr_data); @@ -57,9 +60,9 @@ static STACK_OF(CONF_VALUE) *i2v_ASN1_BIT_STRING( return ret; } -static void *v2i_ASN1_BIT_STRING(const X509V3_EXT_METHOD *method, - const X509V3_CTX *ctx, - const STACK_OF(CONF_VALUE) *nval) { +void *v2i_ASN1_BIT_STRING(const X509V3_EXT_METHOD *method, + const X509V3_CTX *ctx, + const STACK_OF(CONF_VALUE) *nval) { ASN1_BIT_STRING *bs; if (!(bs = ASN1_BIT_STRING_new())) { return nullptr; @@ -87,12 +90,23 @@ static void *v2i_ASN1_BIT_STRING(const X509V3_EXT_METHOD *method, return bs; } -#define EXT_BITSTRING(nid, table) \ - { \ - nid, 0, ASN1_ITEM_ref(ASN1_BIT_STRING), 0, 0, 0, 0, 0, 0, \ - i2v_ASN1_BIT_STRING, v2i_ASN1_BIT_STRING, nullptr, nullptr, \ - (void *)(table) \ - } +} // namespace + +#define EXT_BITSTRING(nid, table) \ + {nid, \ + 0, \ + ASN1_ITEM_ref(ASN1_BIT_STRING), \ + 0, \ + 0, \ + 0, \ + 0, \ + 0, \ + 0, \ + i2v_ASN1_BIT_STRING, \ + v2i_ASN1_BIT_STRING, \ + nullptr, \ + nullptr, \ + (void *)(table)} const X509V3_EXT_METHOD bssl::v3_nscert = EXT_BITSTRING(NID_netscape_cert_type, ns_cert_type_table); diff --git a/third_party/boringssl/src/crypto/x509/v3_conf.cc b/third_party/boringssl/src/crypto/x509/v3_conf.cc index d3d8e5086..d80447cf8 100644 --- a/third_party/boringssl/src/crypto/x509/v3_conf.cc +++ b/third_party/boringssl/src/crypto/x509/v3_conf.cc @@ -31,64 +31,35 @@ using namespace bssl; -static int v3_check_critical(const char **value); -static int v3_check_generic(const char **value); -static X509_EXTENSION *do_ext_nconf(const CONF *conf, const X509V3_CTX *ctx, - int ext_nid, int crit, const char *value); -static X509_EXTENSION *v3_generic_extension(const char *ext, const char *value, - int crit, int type, - const X509V3_CTX *ctx); -static X509_EXTENSION *do_ext_i2d(const X509V3_EXT_METHOD *method, int ext_nid, - int crit, void *ext_struc); -static unsigned char *generic_asn1(const char *value, const X509V3_CTX *ctx, - size_t *ext_len); +namespace { -X509_EXTENSION *X509V3_EXT_nconf(const CONF *conf, const X509V3_CTX *ctx, - const char *name, const char *value) { - // If omitted, fill in an empty |X509V3_CTX|. - X509V3_CTX ctx_tmp; - if (ctx == nullptr) { - X509V3_set_ctx(&ctx_tmp, nullptr, nullptr, nullptr, nullptr, 0); - X509V3_set_nconf(&ctx_tmp, conf); - ctx = &ctx_tmp; - } - - int crit = v3_check_critical(&value); - int ext_type = v3_check_generic(&value); - if (ext_type != 0) { - return v3_generic_extension(name, value, crit, ext_type, ctx); - } - X509_EXTENSION *ret = do_ext_nconf(conf, ctx, OBJ_sn2nid(name), crit, value); - if (!ret) { - OPENSSL_PUT_ERROR(X509V3, X509V3_R_ERROR_IN_EXTENSION); - ERR_add_error_data(4, "name=", name, ", value=", value); +X509_EXTENSION *do_ext_i2d(const X509V3_EXT_METHOD *method, int ext_nid, + int crit, void *ext_struc) { + // Convert the extension's internal representation to DER. + unsigned char *ext_der = nullptr; + int ext_len = ASN1_item_i2d(reinterpret_cast(ext_struc), + &ext_der, ASN1_ITEM_ptr(method->it)); + if (ext_len < 0) { + return nullptr; } - return ret; -} -X509_EXTENSION *X509V3_EXT_nconf_nid(const CONF *conf, const X509V3_CTX *ctx, - int ext_nid, const char *value) { - // If omitted, fill in an empty |X509V3_CTX|. - X509V3_CTX ctx_tmp; - if (ctx == nullptr) { - X509V3_set_ctx(&ctx_tmp, nullptr, nullptr, nullptr, nullptr, 0); - X509V3_set_nconf(&ctx_tmp, conf); - ctx = &ctx_tmp; + ASN1_OCTET_STRING *ext_oct = ASN1_OCTET_STRING_new(); + if (ext_oct == nullptr) { + OPENSSL_free(ext_der); + return nullptr; } + ASN1_STRING_set0(ext_oct, ext_der, ext_len); - int crit = v3_check_critical(&value); - int ext_type = v3_check_generic(&value); - if (ext_type != 0) { - return v3_generic_extension(OBJ_nid2sn(ext_nid), value, crit, ext_type, - ctx); - } - return do_ext_nconf(conf, ctx, ext_nid, crit, value); + X509_EXTENSION *ext = + X509_EXTENSION_create_by_NID(nullptr, ext_nid, crit, ext_oct); + ASN1_OCTET_STRING_free(ext_oct); + return ext; } // CONF *conf: Config file // char *value: Value -static X509_EXTENSION *do_ext_nconf(const CONF *conf, const X509V3_CTX *ctx, - int ext_nid, int crit, const char *value) { +X509_EXTENSION *do_ext_nconf(const CONF *conf, const X509V3_CTX *ctx, + int ext_nid, int crit, const char *value) { const X509V3_EXT_METHOD *method; X509_EXTENSION *ext; const STACK_OF(CONF_VALUE) *nval; @@ -105,9 +76,9 @@ static X509_EXTENSION *do_ext_nconf(const CONF *conf, const X509V3_CTX *ctx, // Now get internal extension representation based on type if (method->v2i) { if (*value == '@') { - // TODO(davidben): This is the only place where |X509V3_EXT_nconf|'s - // |conf| parameter is used. All other codepaths use the copy inside - // |ctx|. Should this be switched and then the parameter ignored? + // TODO(davidben): This is the only place where `X509V3_EXT_nconf`'s + // `conf` parameter is used. All other codepaths use the copy inside + // `ctx`. Should this be switched and then the parameter ignored? if (conf == nullptr) { OPENSSL_PUT_ERROR(X509V3, X509V3_R_NO_CONFIG_DATABASE); return nullptr; @@ -136,7 +107,7 @@ static X509_EXTENSION *do_ext_nconf(const CONF *conf, const X509V3_CTX *ctx, // TODO(davidben): Should this check be removed? This matches OpenSSL, but // r2i-based extensions do not necessarily require a config database. The // two built-in extensions only use it some of the time, and already handle - // |X509V3_get_section| returning NULL. + // `X509V3_get_section` returning NULL. if (!ctx->db) { OPENSSL_PUT_ERROR(X509V3, X509V3_R_NO_CONFIG_DATABASE); return nullptr; @@ -156,42 +127,8 @@ static X509_EXTENSION *do_ext_nconf(const CONF *conf, const X509V3_CTX *ctx, return ext; } -static X509_EXTENSION *do_ext_i2d(const X509V3_EXT_METHOD *method, int ext_nid, - int crit, void *ext_struc) { - // Convert the extension's internal representation to DER. - unsigned char *ext_der = nullptr; - int ext_len = ASN1_item_i2d(reinterpret_cast(ext_struc), - &ext_der, ASN1_ITEM_ptr(method->it)); - if (ext_len < 0) { - return nullptr; - } - - ASN1_OCTET_STRING *ext_oct = ASN1_OCTET_STRING_new(); - if (ext_oct == nullptr) { - OPENSSL_free(ext_der); - return nullptr; - } - ASN1_STRING_set0(ext_oct, ext_der, ext_len); - - X509_EXTENSION *ext = - X509_EXTENSION_create_by_NID(nullptr, ext_nid, crit, ext_oct); - ASN1_OCTET_STRING_free(ext_oct); - return ext; -} - -// Given an internal structure, nid and critical flag create an extension - -X509_EXTENSION *X509V3_EXT_i2d(int ext_nid, int crit, void *ext_struc) { - const X509V3_EXT_METHOD *method; - if (!(method = X509V3_EXT_get_nid(ext_nid))) { - OPENSSL_PUT_ERROR(X509V3, X509V3_R_UNKNOWN_EXTENSION); - return nullptr; - } - return do_ext_i2d(method, ext_nid, crit, ext_struc); -} - // Check the extension string for critical flag -static int v3_check_critical(const char **value) { +int v3_check_critical(const char **value) { const char *p = *value; if ((strlen(p) < 9) || strncmp(p, "critical,", 9)) { return 0; @@ -205,7 +142,7 @@ static int v3_check_critical(const char **value) { } // Check extension string for generic extension and return the type -static int v3_check_generic(const char **value) { +int v3_check_generic(const char **value) { int gen_type = 0; const char *p = *value; if ((strlen(p) >= 4) && !strncmp(p, "DER:", 4)) { @@ -225,10 +162,26 @@ static int v3_check_generic(const char **value) { return gen_type; } +unsigned char *generic_asn1(const char *value, const X509V3_CTX *ctx, + size_t *ext_len) { + ASN1_TYPE *typ = ASN1_generate_v3(value, ctx); + if (typ == nullptr) { + return nullptr; + } + unsigned char *ext_der = nullptr; + int len = i2d_ASN1_TYPE(typ, &ext_der); + ASN1_TYPE_free(typ); + if (len < 0) { + return nullptr; + } + *ext_len = len; + return ext_der; +} + // Create a generic extension: for now just handle DER type -static X509_EXTENSION *v3_generic_extension(const char *ext, const char *value, - int crit, int gen_type, - const X509V3_CTX *ctx) { +X509_EXTENSION *v3_generic_extension(const char *ext, const char *value, + int crit, int gen_type, + const X509V3_CTX *ctx) { UniquePtr obj(OBJ_txt2obj(ext, 0)); if (obj == nullptr) { OPENSSL_PUT_ERROR(X509V3, X509V3_R_EXTENSION_NAME_ERROR); @@ -266,20 +219,59 @@ static X509_EXTENSION *v3_generic_extension(const char *ext, const char *value, return X509_EXTENSION_create_by_OBJ(nullptr, obj.get(), crit, oct.get()); } -static unsigned char *generic_asn1(const char *value, const X509V3_CTX *ctx, - size_t *ext_len) { - ASN1_TYPE *typ = ASN1_generate_v3(value, ctx); - if (typ == nullptr) { - return nullptr; +} // namespace + +X509_EXTENSION *X509V3_EXT_nconf(const CONF *conf, const X509V3_CTX *ctx, + const char *name, const char *value) { + // If omitted, fill in an empty `X509V3_CTX`. + X509V3_CTX ctx_tmp; + if (ctx == nullptr) { + X509V3_set_ctx(&ctx_tmp, nullptr, nullptr, nullptr, nullptr, 0); + X509V3_set_nconf(&ctx_tmp, conf); + ctx = &ctx_tmp; } - unsigned char *ext_der = nullptr; - int len = i2d_ASN1_TYPE(typ, &ext_der); - ASN1_TYPE_free(typ); - if (len < 0) { + + int crit = v3_check_critical(&value); + int ext_type = v3_check_generic(&value); + if (ext_type != 0) { + return v3_generic_extension(name, value, crit, ext_type, ctx); + } + X509_EXTENSION *ret = do_ext_nconf(conf, ctx, OBJ_sn2nid(name), crit, value); + if (!ret) { + OPENSSL_PUT_ERROR(X509V3, X509V3_R_ERROR_IN_EXTENSION); + ERR_add_error_data(4, "name=", name, ", value=", value); + } + return ret; +} + +X509_EXTENSION *X509V3_EXT_nconf_nid(const CONF *conf, const X509V3_CTX *ctx, + int ext_nid, const char *value) { + // If omitted, fill in an empty `X509V3_CTX`. + X509V3_CTX ctx_tmp; + if (ctx == nullptr) { + X509V3_set_ctx(&ctx_tmp, nullptr, nullptr, nullptr, nullptr, 0); + X509V3_set_nconf(&ctx_tmp, conf); + ctx = &ctx_tmp; + } + + int crit = v3_check_critical(&value); + int ext_type = v3_check_generic(&value); + if (ext_type != 0) { + return v3_generic_extension(OBJ_nid2sn(ext_nid), value, crit, ext_type, + ctx); + } + return do_ext_nconf(conf, ctx, ext_nid, crit, value); +} + +// Given an internal structure, nid and critical flag create an extension + +X509_EXTENSION *X509V3_EXT_i2d(int ext_nid, int crit, void *ext_struc) { + const X509V3_EXT_METHOD *method; + if (!(method = X509V3_EXT_get_nid(ext_nid))) { + OPENSSL_PUT_ERROR(X509V3, X509V3_R_UNKNOWN_EXTENSION); return nullptr; } - *ext_len = len; - return ext_der; + return do_ext_i2d(method, ext_nid, crit, ext_struc); } // This is the main function: add a bunch of extensions based on a config diff --git a/third_party/boringssl/src/crypto/x509/v3_cpols.cc b/third_party/boringssl/src/crypto/x509/v3_cpols.cc index f69c82d6b..a183b3377 100644 --- a/third_party/boringssl/src/crypto/x509/v3_cpols.cc +++ b/third_party/boringssl/src/crypto/x509/v3_cpols.cc @@ -15,6 +15,8 @@ #include #include +#include + #include #include #include @@ -31,39 +33,6 @@ using namespace bssl; // Certificate policies extension support: this one is a bit complex... -static int i2r_certpol(const X509V3_EXT_METHOD *method, void *ext, BIO *out, - int indent); -static void *r2i_certpol(const X509V3_EXT_METHOD *method, const X509V3_CTX *ctx, - const char *value); -static void print_qualifiers(BIO *out, const STACK_OF(POLICYQUALINFO) *quals, - int indent); -static void print_notice(BIO *out, const USERNOTICE *notice, int indent); -static POLICYINFO *policy_section(const X509V3_CTX *ctx, - const STACK_OF(CONF_VALUE) *polstrs, - int ia5org); -static POLICYQUALINFO *notice_section(const X509V3_CTX *ctx, - const STACK_OF(CONF_VALUE) *unot, - int ia5org); -static int nref_nos(STACK_OF(ASN1_INTEGER) *nnums, - const STACK_OF(CONF_VALUE) *nos); - -const X509V3_EXT_METHOD bssl::v3_cpols = { - NID_certificate_policies, - 0, - ASN1_ITEM_ref(CERTIFICATEPOLICIES), - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - i2r_certpol, - r2i_certpol, - nullptr, -}; - ASN1_SEQUENCE(NOTICEREF) = { ASN1_SIMPLE(NOTICEREF, organization, DISPLAYTEXT), ASN1_SEQUENCE_OF(NOTICEREF, noticenos, ASN1_INTEGER), @@ -108,305 +77,240 @@ ASN1_ITEM_TEMPLATE_END(CERTIFICATEPOLICIES) IMPLEMENT_ASN1_FUNCTIONS_const(CERTIFICATEPOLICIES) -static void *r2i_certpol(const X509V3_EXT_METHOD *method, const X509V3_CTX *ctx, - const char *value) { - STACK_OF(POLICYINFO) *pols = sk_POLICYINFO_new_null(); - if (pols == nullptr) { - return nullptr; - } - STACK_OF(CONF_VALUE) *vals = X509V3_parse_list(value); +namespace { - { - if (vals == nullptr) { - OPENSSL_PUT_ERROR(X509V3, ERR_R_X509V3_LIB); - goto err; +int nref_nos(STACK_OF(ASN1_INTEGER) *nnums, const STACK_OF(CONF_VALUE) *nos) { + for (size_t i = 0; i < sk_CONF_VALUE_num(nos); i++) { + const CONF_VALUE *cnf = sk_CONF_VALUE_value(nos, i); + ASN1_INTEGER *aint = s2i_ASN1_INTEGER(nullptr, cnf->name); + if (aint == nullptr) { + OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_NUMBER); + return 0; } - int ia5org = 0; - for (size_t i = 0; i < sk_CONF_VALUE_num(vals); i++) { - const CONF_VALUE *cnf = sk_CONF_VALUE_value(vals, i); - if (cnf->value || !cnf->name) { - OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_POLICY_IDENTIFIER); - X509V3_conf_err(cnf); - goto err; + if (!sk_ASN1_INTEGER_push(nnums, aint)) { + ASN1_INTEGER_free(aint); + return 0; + } + } + return 1; +} + +UniquePtr notice_section(const X509V3_CTX *ctx, + const STACK_OF(CONF_VALUE) *unot, + int ia5org) { + UniquePtr qual(POLICYQUALINFO_new()); + if (qual == nullptr) { + return nullptr; + } + qual->pqualid = OBJ_nid2obj(NID_id_qt_unotice); + if (qual->pqualid == nullptr) { + OPENSSL_PUT_ERROR(X509V3, ERR_R_INTERNAL_ERROR); + return nullptr; + } + USERNOTICE *notice = USERNOTICE_new(); + if (notice == nullptr) { + return nullptr; + } + qual->d.usernotice = notice; + for (const CONF_VALUE *cnf : unot) { + if (!strcmp(cnf->name, "explicitText")) { + notice->exptext = ASN1_VISIBLESTRING_new(); + if (notice->exptext == nullptr) { + return nullptr; } - POLICYINFO *pol; - const char *pstr = cnf->name; - if (!strcmp(pstr, "ia5org")) { - ia5org = 1; - continue; - } else if (*pstr == '@') { - const STACK_OF(CONF_VALUE) *polsect = X509V3_get_section(ctx, pstr + 1); - if (!polsect) { - OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_SECTION); - - X509V3_conf_err(cnf); - goto err; - } - pol = policy_section(ctx, polsect, ia5org); - if (!pol) { - goto err; + if (!ASN1_STRING_set(notice->exptext, cnf->value, strlen(cnf->value))) { + return nullptr; + } + } else if (!strcmp(cnf->name, "organization")) { + NOTICEREF *nref; + if (!notice->noticeref) { + if (!(nref = NOTICEREF_new())) { + return nullptr; } + notice->noticeref = nref; } else { - ASN1_OBJECT *pobj = OBJ_txt2obj(cnf->name, 0); - if (pobj == nullptr) { - OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_OBJECT_IDENTIFIER); - X509V3_conf_err(cnf); - goto err; - } - pol = POLICYINFO_new(); - if (pol == nullptr) { - ASN1_OBJECT_free(pobj); - goto err; + nref = notice->noticeref; + } + if (ia5org) { + nref->organization->type = V_ASN1_IA5STRING; + } else { + nref->organization->type = V_ASN1_VISIBLESTRING; + } + if (!ASN1_STRING_set(nref->organization, cnf->value, + strlen(cnf->value))) { + return nullptr; + } + } else if (!strcmp(cnf->name, "noticeNumbers")) { + NOTICEREF *nref; + if (!notice->noticeref) { + if (!(nref = NOTICEREF_new())) { + return nullptr; } - pol->policyid = pobj; + notice->noticeref = nref; + } else { + nref = notice->noticeref; + } + UniquePtr nos(X509V3_parse_list(cnf->value)); + if (!nos || !sk_CONF_VALUE_num(nos.get())) { + OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_NUMBERS); + X509V3_conf_err(cnf); + return nullptr; } - if (!sk_POLICYINFO_push(pols, pol)) { - POLICYINFO_free(pol); - goto err; + if (!nref_nos(nref->noticenos, nos.get())) { + return nullptr; } + } else { + OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_OPTION); + X509V3_conf_err(cnf); + return nullptr; } - sk_CONF_VALUE_pop_free(vals, X509V3_conf_free); - return pols; } -err: - sk_CONF_VALUE_pop_free(vals, X509V3_conf_free); - sk_POLICYINFO_pop_free(pols, POLICYINFO_free); - return nullptr; + if (notice->noticeref && + (!notice->noticeref->noticenos || !notice->noticeref->organization)) { + OPENSSL_PUT_ERROR(X509V3, X509V3_R_NEED_ORGANIZATION_AND_NUMBERS); + return nullptr; + } + + return qual; } -static POLICYINFO *policy_section(const X509V3_CTX *ctx, - const STACK_OF(CONF_VALUE) *polstrs, - int ia5org) { - POLICYINFO *pol; - POLICYQUALINFO *qual; - if (!(pol = POLICYINFO_new())) { - goto err; +UniquePtr policy_section(const X509V3_CTX *ctx, + const STACK_OF(CONF_VALUE) *polstrs, + int ia5org) { + UniquePtr pol(POLICYINFO_new()); + if (pol == nullptr) { + return nullptr; } - for (size_t i = 0; i < sk_CONF_VALUE_num(polstrs); i++) { - const CONF_VALUE *cnf = sk_CONF_VALUE_value(polstrs, i); + for (const CONF_VALUE *cnf : polstrs) { if (!strcmp(cnf->name, "policyIdentifier")) { ASN1_OBJECT *pobj; if (!(pobj = OBJ_txt2obj(cnf->value, 0))) { OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_OBJECT_IDENTIFIER); X509V3_conf_err(cnf); - goto err; + return nullptr; } + ASN1_OBJECT_free(pol->policyid); pol->policyid = pobj; } else if (x509v3_conf_name_matches(cnf->name, "CPS")) { - if (!pol->qualifiers) { + if (pol->qualifiers == nullptr) { pol->qualifiers = sk_POLICYQUALINFO_new_null(); } - if (!(qual = POLICYQUALINFO_new())) { - goto err; - } - if (!sk_POLICYQUALINFO_push(pol->qualifiers, qual)) { - goto err; + UniquePtr qual(POLICYQUALINFO_new()); + if (qual == nullptr || pol->qualifiers == nullptr) { + return nullptr; } qual->pqualid = OBJ_nid2obj(NID_id_qt_cps); if (qual->pqualid == nullptr) { OPENSSL_PUT_ERROR(X509V3, ERR_R_INTERNAL_ERROR); - goto err; + return nullptr; } qual->d.cpsuri = ASN1_IA5STRING_new(); if (qual->d.cpsuri == nullptr) { - goto err; + return nullptr; } if (!ASN1_STRING_set(qual->d.cpsuri, cnf->value, strlen(cnf->value))) { - goto err; + return nullptr; + } + if (!PushToStack(pol->qualifiers, std::move(qual))) { + return nullptr; } } else if (x509v3_conf_name_matches(cnf->name, "userNotice")) { if (*cnf->value != '@') { OPENSSL_PUT_ERROR(X509V3, X509V3_R_EXPECTED_A_SECTION_NAME); X509V3_conf_err(cnf); - goto err; + return nullptr; } const STACK_OF(CONF_VALUE) *unot = X509V3_get_section(ctx, cnf->value + 1); if (!unot) { OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_SECTION); X509V3_conf_err(cnf); - goto err; + return nullptr; } - qual = notice_section(ctx, unot, ia5org); - if (!qual) { - goto err; + UniquePtr qual = notice_section(ctx, unot, ia5org); + if (qual == nullptr) { + return nullptr; } - if (!pol->qualifiers) { + if (pol->qualifiers == nullptr) { pol->qualifiers = sk_POLICYQUALINFO_new_null(); } - if (!sk_POLICYQUALINFO_push(pol->qualifiers, qual)) { - goto err; + if (pol->qualifiers == nullptr || + !PushToStack(pol->qualifiers, std::move(qual))) { + return nullptr; } } else { OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_OPTION); X509V3_conf_err(cnf); - goto err; + return nullptr; } } if (!pol->policyid) { OPENSSL_PUT_ERROR(X509V3, X509V3_R_NO_POLICY_IDENTIFIER); - goto err; + return nullptr; } return pol; - -err: - POLICYINFO_free(pol); - return nullptr; } -static POLICYQUALINFO *notice_section(const X509V3_CTX *ctx, - const STACK_OF(CONF_VALUE) *unot, - int ia5org) { - USERNOTICE *notice; - POLICYQUALINFO *qual; - if (!(qual = POLICYQUALINFO_new())) { - goto err; - } - qual->pqualid = OBJ_nid2obj(NID_id_qt_unotice); - if (qual->pqualid == nullptr) { - OPENSSL_PUT_ERROR(X509V3, ERR_R_INTERNAL_ERROR); - goto err; +void *r2i_certpol(const X509V3_EXT_METHOD *method, const X509V3_CTX *ctx, + const char *value) { + UniquePtr pols(sk_POLICYINFO_new_null()); + if (pols == nullptr) { + return nullptr; } - if (!(notice = USERNOTICE_new())) { - goto err; + UniquePtr vals(X509V3_parse_list(value)); + if (vals == nullptr) { + OPENSSL_PUT_ERROR(X509V3, ERR_R_X509V3_LIB); + return nullptr; } - qual->d.usernotice = notice; - for (size_t i = 0; i < sk_CONF_VALUE_num(unot); i++) { - const CONF_VALUE *cnf = sk_CONF_VALUE_value(unot, i); - if (!strcmp(cnf->name, "explicitText")) { - notice->exptext = ASN1_VISIBLESTRING_new(); - if (notice->exptext == nullptr) { - goto err; - } - if (!ASN1_STRING_set(notice->exptext, cnf->value, strlen(cnf->value))) { - goto err; - } - } else if (!strcmp(cnf->name, "organization")) { - NOTICEREF *nref; - if (!notice->noticeref) { - if (!(nref = NOTICEREF_new())) { - goto err; - } - notice->noticeref = nref; - } else { - nref = notice->noticeref; - } - if (ia5org) { - nref->organization->type = V_ASN1_IA5STRING; - } else { - nref->organization->type = V_ASN1_VISIBLESTRING; + int ia5org = 0; + for (const CONF_VALUE *cnf : vals.get()) { + if (cnf->value || !cnf->name) { + OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_POLICY_IDENTIFIER); + X509V3_conf_err(cnf); + return nullptr; + } + UniquePtr pol; + const char *pstr = cnf->name; + if (!strcmp(pstr, "ia5org")) { + ia5org = 1; + continue; + } else if (*pstr == '@') { + const STACK_OF(CONF_VALUE) *polsect = X509V3_get_section(ctx, pstr + 1); + if (!polsect) { + OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_SECTION); + + X509V3_conf_err(cnf); + return nullptr; } - if (!ASN1_STRING_set(nref->organization, cnf->value, - strlen(cnf->value))) { - goto err; + pol = policy_section(ctx, polsect, ia5org); + if (!pol) { + return nullptr; } - } else if (!strcmp(cnf->name, "noticeNumbers")) { - NOTICEREF *nref; - STACK_OF(CONF_VALUE) *nos; - if (!notice->noticeref) { - if (!(nref = NOTICEREF_new())) { - goto err; - } - notice->noticeref = nref; - } else { - nref = notice->noticeref; - } - nos = X509V3_parse_list(cnf->value); - if (!nos || !sk_CONF_VALUE_num(nos)) { - OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_NUMBERS); + } else { + UniquePtr pobj(OBJ_txt2obj(cnf->name, 0)); + if (pobj == nullptr) { + OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_OBJECT_IDENTIFIER); X509V3_conf_err(cnf); - sk_CONF_VALUE_pop_free(nos, X509V3_conf_free); - goto err; + return nullptr; } - int ret = nref_nos(nref->noticenos, nos); - sk_CONF_VALUE_pop_free(nos, X509V3_conf_free); - if (!ret) { - goto err; + pol.reset(POLICYINFO_new()); + if (pol == nullptr) { + return nullptr; } - } else { - OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_OPTION); - X509V3_conf_err(cnf); - goto err; + pol->policyid = pobj.release(); } - } - - if (notice->noticeref && - (!notice->noticeref->noticenos || !notice->noticeref->organization)) { - OPENSSL_PUT_ERROR(X509V3, X509V3_R_NEED_ORGANIZATION_AND_NUMBERS); - goto err; - } - - return qual; - -err: - POLICYQUALINFO_free(qual); - return nullptr; -} - -static int nref_nos(STACK_OF(ASN1_INTEGER) *nnums, - const STACK_OF(CONF_VALUE) *nos) { - for (size_t i = 0; i < sk_CONF_VALUE_num(nos); i++) { - const CONF_VALUE *cnf = sk_CONF_VALUE_value(nos, i); - ASN1_INTEGER *aint = s2i_ASN1_INTEGER(nullptr, cnf->name); - if (aint == nullptr) { - OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_NUMBER); - return 0; - } - if (!sk_ASN1_INTEGER_push(nnums, aint)) { - ASN1_INTEGER_free(aint); - return 0; - } - } - return 1; -} - -static int i2r_certpol(const X509V3_EXT_METHOD *method, void *ext, BIO *out, - int indent) { - const STACK_OF(POLICYINFO) *pol = - reinterpret_cast(ext); - // First print out the policy OIDs - for (size_t i = 0; i < sk_POLICYINFO_num(pol); i++) { - const POLICYINFO *pinfo = sk_POLICYINFO_value(pol, i); - BIO_printf(out, "%*sPolicy: ", indent, ""); - i2a_ASN1_OBJECT(out, pinfo->policyid); - BIO_puts(out, "\n"); - if (pinfo->qualifiers) { - print_qualifiers(out, pinfo->qualifiers, indent + 2); + if (!PushToStack(pols.get(), std::move(pol))) { + return nullptr; } } - return 1; + return pols.release(); } -static void print_qualifiers(BIO *out, const STACK_OF(POLICYQUALINFO) *quals, - int indent) { - for (size_t i = 0; i < sk_POLICYQUALINFO_num(quals); i++) { - const POLICYQUALINFO *qualinfo = sk_POLICYQUALINFO_value(quals, i); - switch (OBJ_obj2nid(qualinfo->pqualid)) { - case NID_id_qt_cps: - BIO_printf(out, "%*sCPS: %.*s\n", indent, "", - qualinfo->d.cpsuri->length, qualinfo->d.cpsuri->data); - break; - - case NID_id_qt_unotice: - BIO_printf(out, "%*sUser Notice:\n", indent, ""); - print_notice(out, qualinfo->d.usernotice, indent + 2); - break; - - default: - BIO_printf(out, "%*sUnknown Qualifier: ", indent + 2, ""); - - i2a_ASN1_OBJECT(out, qualinfo->pqualid); - BIO_puts(out, "\n"); - break; - } - } -} - -static void print_notice(BIO *out, const USERNOTICE *notice, int indent) { +void print_notice(BIO *out, const USERNOTICE *notice, int indent) { if (notice->noticeref) { NOTICEREF *ref; ref = notice->noticeref; @@ -439,3 +343,64 @@ static void print_notice(BIO *out, const USERNOTICE *notice, int indent) { notice->exptext->length, notice->exptext->data); } } + +void print_qualifiers(BIO *out, const STACK_OF(POLICYQUALINFO) *quals, + int indent) { + for (size_t i = 0; i < sk_POLICYQUALINFO_num(quals); i++) { + const POLICYQUALINFO *qualinfo = sk_POLICYQUALINFO_value(quals, i); + switch (OBJ_obj2nid(qualinfo->pqualid)) { + case NID_id_qt_cps: + BIO_printf(out, "%*sCPS: %.*s\n", indent, "", + qualinfo->d.cpsuri->length, qualinfo->d.cpsuri->data); + break; + + case NID_id_qt_unotice: + BIO_printf(out, "%*sUser Notice:\n", indent, ""); + print_notice(out, qualinfo->d.usernotice, indent + 2); + break; + + default: + BIO_printf(out, "%*sUnknown Qualifier: ", indent + 2, ""); + + i2a_ASN1_OBJECT(out, qualinfo->pqualid); + BIO_puts(out, "\n"); + break; + } + } +} + +int i2r_certpol(const X509V3_EXT_METHOD *method, void *ext, BIO *out, + int indent) { + const STACK_OF(POLICYINFO) *pol = + reinterpret_cast(ext); + // First print out the policy OIDs + for (size_t i = 0; i < sk_POLICYINFO_num(pol); i++) { + const POLICYINFO *pinfo = sk_POLICYINFO_value(pol, i); + BIO_printf(out, "%*sPolicy: ", indent, ""); + i2a_ASN1_OBJECT(out, pinfo->policyid); + BIO_puts(out, "\n"); + if (pinfo->qualifiers) { + print_qualifiers(out, pinfo->qualifiers, indent + 2); + } + } + return 1; +} + +} // namespace + +const X509V3_EXT_METHOD bssl::v3_cpols = { + NID_certificate_policies, + 0, + ASN1_ITEM_ref(CERTIFICATEPOLICIES), + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + i2r_certpol, + r2i_certpol, + nullptr, +}; diff --git a/third_party/boringssl/src/crypto/x509/v3_crld.cc b/third_party/boringssl/src/crypto/x509/v3_crld.cc index 61225ad42..1e02784f7 100644 --- a/third_party/boringssl/src/crypto/x509/v3_crld.cc +++ b/third_party/boringssl/src/crypto/x509/v3_crld.cc @@ -15,6 +15,9 @@ #include #include +#include +#include + #include #include #include @@ -23,83 +26,86 @@ #include #include +#include "../asn1/internal.h" #include "../internal.h" #include "internal.h" using namespace bssl; -static void *v2i_crld(const X509V3_EXT_METHOD *method, const X509V3_CTX *ctx, - const STACK_OF(CONF_VALUE) *nval); -static int i2r_crldp(const X509V3_EXT_METHOD *method, void *pcrldp, BIO *out, - int indent); +ASN1_CHOICE(DIST_POINT_NAME) = { + ASN1_IMP_SEQUENCE_OF(DIST_POINT_NAME, name.fullname, GENERAL_NAME, 0), + ASN1_IMP_SET_OF(DIST_POINT_NAME, name.relativename, X509_NAME_ENTRY, 1), +} ASN1_CHOICE_END(DIST_POINT_NAME) -const X509V3_EXT_METHOD bssl::v3_crld = { - NID_crl_distribution_points, - 0, - ASN1_ITEM_ref(CRL_DIST_POINTS), - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - v2i_crld, - i2r_crldp, - nullptr, - nullptr, -}; +IMPLEMENT_ASN1_ALLOC_FUNCTIONS(DIST_POINT_NAME) -const X509V3_EXT_METHOD bssl::v3_freshest_crl = { - NID_freshest_crl, 0, ASN1_ITEM_ref(CRL_DIST_POINTS), - nullptr, nullptr, nullptr, - nullptr, nullptr, nullptr, - nullptr, v2i_crld, i2r_crldp, - nullptr, nullptr, -}; +ASN1_SEQUENCE(DIST_POINT) = { + ASN1_EXP_OPT(DIST_POINT, distpoint, DIST_POINT_NAME, 0), + ASN1_IMP_OPT(DIST_POINT, reasons, ASN1_BIT_STRING, 1), + ASN1_IMP_SEQUENCE_OF_OPT(DIST_POINT, CRLissuer, GENERAL_NAME, 2), +} ASN1_SEQUENCE_END(DIST_POINT) + +IMPLEMENT_ASN1_ALLOC_FUNCTIONS(DIST_POINT) + +ASN1_ITEM_TEMPLATE(CRL_DIST_POINTS) = ASN1_EX_TEMPLATE_TYPE( + ASN1_TFLG_SEQUENCE_OF, 0, CRLDistributionPoints, DIST_POINT) +ASN1_ITEM_TEMPLATE_END(CRL_DIST_POINTS) + +IMPLEMENT_ASN1_FUNCTIONS_const(CRL_DIST_POINTS) -static STACK_OF(GENERAL_NAME) *gnames_from_sectname(const X509V3_CTX *ctx, - char *sect) { +ASN1_SEQUENCE(ISSUING_DIST_POINT) = { + ASN1_EXP_OPT(ISSUING_DIST_POINT, distpoint, DIST_POINT_NAME, 0), + ASN1_IMP_OPT(ISSUING_DIST_POINT, onlyuser, ASN1_FBOOLEAN, 1), + ASN1_IMP_OPT(ISSUING_DIST_POINT, onlyCA, ASN1_FBOOLEAN, 2), + ASN1_IMP_OPT(ISSUING_DIST_POINT, onlysomereasons, ASN1_BIT_STRING, 3), + ASN1_IMP_OPT(ISSUING_DIST_POINT, indirectCRL, ASN1_FBOOLEAN, 4), + ASN1_IMP_OPT(ISSUING_DIST_POINT, onlyattr, ASN1_FBOOLEAN, 5), +} ASN1_SEQUENCE_END(ISSUING_DIST_POINT) + +IMPLEMENT_ASN1_FUNCTIONS_const(ISSUING_DIST_POINT) + +namespace { + +UniquePtr gnames_from_sectname(const X509V3_CTX *ctx, + const char *sect) { const STACK_OF(CONF_VALUE) *gnsect; - STACK_OF(CONF_VALUE) *gnsect_owned = nullptr; + UniquePtr gnsect_owned; if (*sect == '@') { gnsect = X509V3_get_section(ctx, sect + 1); } else { - gnsect_owned = X509V3_parse_list(sect); - gnsect = gnsect_owned; + gnsect_owned.reset(X509V3_parse_list(sect)); + gnsect = gnsect_owned.get(); } if (!gnsect) { OPENSSL_PUT_ERROR(X509V3, X509V3_R_SECTION_NOT_FOUND); return nullptr; } - STACK_OF(GENERAL_NAME) *gens = v2i_GENERAL_NAMES(nullptr, ctx, gnsect); - sk_CONF_VALUE_pop_free(gnsect_owned, X509V3_conf_free); - return gens; + return UniquePtr(v2i_GENERAL_NAMES(nullptr, ctx, gnsect)); } -// set_dist_point_name decodes a DistributionPointName from |cnf| and writes the -// result in |*pdp|. It returns 1 on success, -1 on error, and 0 if |cnf| used +// set_dist_point_name decodes a DistributionPointName from `cnf` and writes the +// result in `*pdp`. It returns 1 on success, -1 on error, and 0 if `cnf` used // an unrecognized input type. The zero return can be used by callers to support // additional syntax. -static int set_dist_point_name(DIST_POINT_NAME **pdp, const X509V3_CTX *ctx, - const CONF_VALUE *cnf) { - STACK_OF(GENERAL_NAME) *fnm = nullptr; - STACK_OF(X509_NAME_ENTRY) *rnm = nullptr; +int set_dist_point_name(DIST_POINT_NAME **pdp, const X509V3_CTX *ctx, + const CONF_VALUE *cnf) { + UniquePtr fnm; + UniquePtr rnm; if (!strcmp(cnf->name, "fullname")) { - // If |cnf| comes from |X509V3_parse_list|, which is possible for a v2i - // function, |cnf->value| may be NULL. + // If `cnf` comes from `X509V3_parse_list`, which is possible for a v2i + // function, `cnf->value` may be NULL. if (cnf->value == nullptr) { OPENSSL_PUT_ERROR(X509V3, X509V3_R_MISSING_VALUE); return -1; } fnm = gnames_from_sectname(ctx, cnf->value); if (!fnm) { - goto err; + return -1; } } else if (!strcmp(cnf->name, "relativename")) { - // If |cnf| comes from |X509V3_parse_list|, which is possible for a v2i - // function, |cnf->value| may be NULL. + // If `cnf` comes from `X509V3_parse_list`, which is possible for a v2i + // function, `cnf->value` may be NULL. if (cnf->value == nullptr) { OPENSSL_PUT_ERROR(X509V3, X509V3_R_MISSING_VALUE); return -1; @@ -109,22 +115,20 @@ static int set_dist_point_name(DIST_POINT_NAME **pdp, const X509V3_CTX *ctx, OPENSSL_PUT_ERROR(X509V3, X509V3_R_SECTION_NOT_FOUND); return -1; } - X509_NAME *nm = X509_NAME_new(); + UniquePtr nm(X509_NAME_new()); if (!nm) { return -1; } - auto *impl = FromOpaque(nm); - int ret = X509V3_NAME_from_section(impl, dnsect, MBSTRING_ASC); - rnm = impl->entries; - impl->entries = nullptr; - X509_NAME_free(nm); - if (!ret || sk_X509_NAME_ENTRY_num(rnm) <= 0) { - goto err; + int ret = X509V3_NAME_from_section(nm.get(), dnsect, MBSTRING_ASC); + rnm = std::move(FromOpaque(nm.get())->entries); + if (!ret || sk_X509_NAME_ENTRY_num(rnm.get()) <= 0) { + return -1; } // There can only be one RDN in nameRelativeToCRLIssuer. - if (sk_X509_NAME_ENTRY_value(rnm, sk_X509_NAME_ENTRY_num(rnm) - 1)->set) { + if (X509_NAME_ENTRY_set(sk_X509_NAME_ENTRY_value( + rnm.get(), sk_X509_NAME_ENTRY_num(rnm.get()) - 1)) != 0) { OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_MULTIPLE_RDNS); - goto err; + return -1; } } else { return 0; @@ -132,30 +136,25 @@ static int set_dist_point_name(DIST_POINT_NAME **pdp, const X509V3_CTX *ctx, if (*pdp) { OPENSSL_PUT_ERROR(X509V3, X509V3_R_DISTPOINT_ALREADY_SET); - goto err; + return -1; } *pdp = DIST_POINT_NAME_new(); if (!*pdp) { - goto err; + return -1; } if (fnm) { (*pdp)->type = 0; - (*pdp)->name.fullname = fnm; + (*pdp)->name.fullname = fnm.release(); } else { (*pdp)->type = 1; - (*pdp)->name.relativename = rnm; + (*pdp)->name.relativename = rnm.release(); } return 1; - -err: - sk_GENERAL_NAME_pop_free(fnm, GENERAL_NAME_free); - sk_X509_NAME_ENTRY_pop_free(rnm, X509_NAME_ENTRY_free); - return -1; } -static const BIT_STRING_BITNAME reason_flags[] = { +const BIT_STRING_BITNAME reason_flags[] = { {0, "Unused", "unused"}, {1, "Key Compromise", "keyCompromise"}, {2, "CA Compromise", "CACompromise"}, @@ -165,60 +164,49 @@ static const BIT_STRING_BITNAME reason_flags[] = { {6, "Certificate Hold", "certificateHold"}, {7, "Privilege Withdrawn", "privilegeWithdrawn"}, {8, "AA Compromise", "AACompromise"}, - {-1, nullptr, nullptr}}; +}; -static int set_reasons(ASN1_BIT_STRING **preas, const char *value) { +int set_reasons(ASN1_BIT_STRING **preas, const char *value) { if (*preas) { // Duplicate "reasons" or "onlysomereasons" key. OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_VALUE); return 0; } - int ret = 0; - STACK_OF(CONF_VALUE) *rsk = X509V3_parse_list(value); + UniquePtr rsk(X509V3_parse_list(value)); if (!rsk) { return 0; } - for (size_t i = 0; i < sk_CONF_VALUE_num(rsk); i++) { - const char *bnam = sk_CONF_VALUE_value(rsk, i)->name; + for (const CONF_VALUE *val : rsk.get()) { if (!*preas) { *preas = ASN1_BIT_STRING_new(); if (!*preas) { - goto err; - } - } - const BIT_STRING_BITNAME *pbn; - for (pbn = reason_flags; pbn->lname; pbn++) { - if (!strcmp(pbn->sname, bnam)) { - if (!ASN1_BIT_STRING_set_bit(*preas, pbn->bitnum, 1)) { - goto err; - } - break; + return 0; } } - if (!pbn->lname) { - goto err; + auto it = std::find_if(std::begin(reason_flags), std::end(reason_flags), + [&](const BIT_STRING_BITNAME &reason) { + return strcmp(reason.sname, val->name) == 0; + }); + if (it == std::end(reason_flags) || + !ASN1_BIT_STRING_set_bit(*preas, it->bitnum, 1)) { + return 0; } } - ret = 1; - -err: - sk_CONF_VALUE_pop_free(rsk, X509V3_conf_free); - return ret; + return 1; } -static int print_reasons(BIO *out, const char *rname, ASN1_BIT_STRING *rflags, - int indent) { - int first = 1; - const BIT_STRING_BITNAME *pbn; +int print_reasons(BIO *out, const char *rname, ASN1_BIT_STRING *rflags, + int indent) { + bool first = true; BIO_printf(out, "%*s%s:\n%*s", indent, "", rname, indent + 2, ""); - for (pbn = reason_flags; pbn->lname; pbn++) { - if (ASN1_BIT_STRING_get_bit(rflags, pbn->bitnum)) { + for (const BIT_STRING_BITNAME &reason : reason_flags) { + if (ASN1_BIT_STRING_get_bit(rflags, reason.bitnum)) { if (first) { - first = 0; + first = false; } else { BIO_puts(out, ", "); } - BIO_puts(out, pbn->lname); + BIO_puts(out, reason.lname); } } if (first) { @@ -229,12 +217,11 @@ static int print_reasons(BIO *out, const char *rname, ASN1_BIT_STRING *rflags, return 1; } -static DIST_POINT *crldp_from_section(const X509V3_CTX *ctx, - const STACK_OF(CONF_VALUE) *nval) { - DIST_POINT *point = nullptr; - point = DIST_POINT_new(); +UniquePtr crldp_from_section(const X509V3_CTX *ctx, + const STACK_OF(CONF_VALUE) *nval) { + UniquePtr point(DIST_POINT_new()); if (!point) { - goto err; + return nullptr; } for (size_t i = 0; i < sk_CONF_VALUE_num(nval); i++) { const CONF_VALUE *cnf = sk_CONF_VALUE_value(nval, i); @@ -243,160 +230,68 @@ static DIST_POINT *crldp_from_section(const X509V3_CTX *ctx, continue; } if (ret < 0) { - goto err; + return nullptr; } if (!strcmp(cnf->name, "reasons")) { if (!set_reasons(&point->reasons, cnf->value)) { - goto err; + return nullptr; } } else if (!strcmp(cnf->name, "CRLissuer")) { GENERAL_NAMES_free(point->CRLissuer); - point->CRLissuer = gnames_from_sectname(ctx, cnf->value); + point->CRLissuer = gnames_from_sectname(ctx, cnf->value).release(); if (!point->CRLissuer) { - goto err; + return nullptr; } } } return point; - -err: - DIST_POINT_free(point); - return nullptr; } -static void *v2i_crld(const X509V3_EXT_METHOD *method, const X509V3_CTX *ctx, - const STACK_OF(CONF_VALUE) *nval) { - STACK_OF(DIST_POINT) *crld = nullptr; - GENERAL_NAMES *gens = nullptr; - GENERAL_NAME *gen = nullptr; - if (!(crld = sk_DIST_POINT_new_null())) { - goto err; +void *v2i_crld(const X509V3_EXT_METHOD *method, const X509V3_CTX *ctx, + const STACK_OF(CONF_VALUE) *nval) { + UniquePtr crld(sk_DIST_POINT_new_null()); + if (crld == nullptr) { + return nullptr; } - for (size_t i = 0; i < sk_CONF_VALUE_num(nval); i++) { - DIST_POINT *point; - const CONF_VALUE *cnf = sk_CONF_VALUE_value(nval, i); + for (const CONF_VALUE *cnf : nval) { if (!cnf->value) { const STACK_OF(CONF_VALUE) *dpsect = X509V3_get_section(ctx, cnf->name); if (!dpsect) { - goto err; + return nullptr; } - point = crldp_from_section(ctx, dpsect); - if (!point) { - goto err; - } - if (!sk_DIST_POINT_push(crld, point)) { - DIST_POINT_free(point); - goto err; + UniquePtr point = crldp_from_section(ctx, dpsect); + if (!point || !PushToStack(crld.get(), std::move(point))) { + return nullptr; } } else { - if (!(gen = v2i_GENERAL_NAME(method, ctx, cnf))) { - goto err; - } - if (!(gens = GENERAL_NAMES_new())) { - goto err; - } - if (!sk_GENERAL_NAME_push(gens, gen)) { - goto err; + UniquePtr gen(v2i_GENERAL_NAME(method, ctx, cnf)); + if (gen == nullptr) { + return nullptr; } - gen = nullptr; - if (!(point = DIST_POINT_new())) { - goto err; + UniquePtr gens(GENERAL_NAMES_new()); + if (gens == nullptr || !PushToStack(gens.get(), std::move(gen))) { + return nullptr; } - if (!sk_DIST_POINT_push(crld, point)) { - DIST_POINT_free(point); - goto err; + UniquePtr point(DIST_POINT_new()); + if (point == nullptr) { + return nullptr; } if (!(point->distpoint = DIST_POINT_NAME_new())) { - goto err; + return nullptr; } - point->distpoint->name.fullname = gens; + point->distpoint->name.fullname = gens.release(); point->distpoint->type = 0; - gens = nullptr; + if (!PushToStack(crld.get(), std::move(point))) { + return nullptr; + } } } - return crld; - -err: - GENERAL_NAME_free(gen); - GENERAL_NAMES_free(gens); - sk_DIST_POINT_pop_free(crld, DIST_POINT_free); - return nullptr; -} - -static int dpn_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, - void *exarg) { - DIST_POINT_NAME *dpn = (DIST_POINT_NAME *)*pval; - - switch (operation) { - case ASN1_OP_NEW_POST: - dpn->dpname = nullptr; - break; - - case ASN1_OP_FREE_POST: - X509_NAME_free(dpn->dpname); - break; - } - return 1; + return crld.release(); } - -ASN1_CHOICE_cb(DIST_POINT_NAME, dpn_cb) = { - ASN1_IMP_SEQUENCE_OF(DIST_POINT_NAME, name.fullname, GENERAL_NAME, 0), - ASN1_IMP_SET_OF(DIST_POINT_NAME, name.relativename, X509_NAME_ENTRY, 1), -} ASN1_CHOICE_END_cb(DIST_POINT_NAME, DIST_POINT_NAME, type) - -IMPLEMENT_ASN1_ALLOC_FUNCTIONS(DIST_POINT_NAME) - -ASN1_SEQUENCE(DIST_POINT) = { - ASN1_EXP_OPT(DIST_POINT, distpoint, DIST_POINT_NAME, 0), - ASN1_IMP_OPT(DIST_POINT, reasons, ASN1_BIT_STRING, 1), - ASN1_IMP_SEQUENCE_OF_OPT(DIST_POINT, CRLissuer, GENERAL_NAME, 2), -} ASN1_SEQUENCE_END(DIST_POINT) - -IMPLEMENT_ASN1_ALLOC_FUNCTIONS(DIST_POINT) - -ASN1_ITEM_TEMPLATE(CRL_DIST_POINTS) = ASN1_EX_TEMPLATE_TYPE( - ASN1_TFLG_SEQUENCE_OF, 0, CRLDistributionPoints, DIST_POINT) -ASN1_ITEM_TEMPLATE_END(CRL_DIST_POINTS) - -IMPLEMENT_ASN1_FUNCTIONS_const(CRL_DIST_POINTS) - -ASN1_SEQUENCE(ISSUING_DIST_POINT) = { - ASN1_EXP_OPT(ISSUING_DIST_POINT, distpoint, DIST_POINT_NAME, 0), - ASN1_IMP_OPT(ISSUING_DIST_POINT, onlyuser, ASN1_FBOOLEAN, 1), - ASN1_IMP_OPT(ISSUING_DIST_POINT, onlyCA, ASN1_FBOOLEAN, 2), - ASN1_IMP_OPT(ISSUING_DIST_POINT, onlysomereasons, ASN1_BIT_STRING, 3), - ASN1_IMP_OPT(ISSUING_DIST_POINT, indirectCRL, ASN1_FBOOLEAN, 4), - ASN1_IMP_OPT(ISSUING_DIST_POINT, onlyattr, ASN1_FBOOLEAN, 5), -} ASN1_SEQUENCE_END(ISSUING_DIST_POINT) - -IMPLEMENT_ASN1_FUNCTIONS_const(ISSUING_DIST_POINT) - -static int i2r_idp(const X509V3_EXT_METHOD *method, void *pidp, BIO *out, - int indent); -static void *v2i_idp(const X509V3_EXT_METHOD *method, const X509V3_CTX *ctx, - const STACK_OF(CONF_VALUE) *nval); - -const X509V3_EXT_METHOD bssl::v3_idp = { - NID_issuing_distribution_point, - X509V3_EXT_MULTILINE, - ASN1_ITEM_ref(ISSUING_DIST_POINT), - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - v2i_idp, - i2r_idp, - nullptr, - nullptr, -}; - -static void *v2i_idp(const X509V3_EXT_METHOD *method, const X509V3_CTX *ctx, - const STACK_OF(CONF_VALUE) *nval) { +void *v2i_idp(const X509V3_EXT_METHOD *method, const X509V3_CTX *ctx, + const STACK_OF(CONF_VALUE) *nval) { ISSUING_DIST_POINT *idp = ISSUING_DIST_POINT_new(); if (!idp) { goto err; @@ -445,7 +340,7 @@ static void *v2i_idp(const X509V3_EXT_METHOD *method, const X509V3_CTX *ctx, return nullptr; } -static int print_gens(BIO *out, STACK_OF(GENERAL_NAME) *gens, int indent) { +int print_gens(BIO *out, STACK_OF(GENERAL_NAME) *gens, int indent) { size_t i; for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) { BIO_printf(out, "%*s", indent + 2, ""); @@ -455,13 +350,17 @@ static int print_gens(BIO *out, STACK_OF(GENERAL_NAME) *gens, int indent) { return 1; } -static int print_distpoint(BIO *out, DIST_POINT_NAME *dpn, int indent) { +int print_distpoint(BIO *out, DIST_POINT_NAME *dpn, int indent) { if (dpn->type == 0) { BIO_printf(out, "%*sFull Name:\n", indent, ""); print_gens(out, dpn->name.fullname, indent); } else { X509Name ntmp; - ntmp.entries = dpn->name.relativename; + ntmp.entries.reset(sk_X509_NAME_ENTRY_deep_copy( + dpn->name.relativename, X509_NAME_ENTRY_dup, X509_NAME_ENTRY_free)); + if (ntmp.entries == nullptr) { + return 0; + } BIO_printf(out, "%*sRelative Name:\n%*s", indent, "", indent + 2, ""); X509_NAME_print_ex(out, &ntmp, 0, XN_FLAG_ONELINE); BIO_puts(out, "\n"); @@ -469,8 +368,7 @@ static int print_distpoint(BIO *out, DIST_POINT_NAME *dpn, int indent) { return 1; } -static int i2r_idp(const X509V3_EXT_METHOD *method, void *pidp, BIO *out, - int indent) { +int i2r_idp(const X509V3_EXT_METHOD *method, void *pidp, BIO *out, int indent) { ISSUING_DIST_POINT *idp = reinterpret_cast(pidp); if (idp->distpoint) { print_distpoint(out, idp->distpoint, indent); @@ -499,8 +397,8 @@ static int i2r_idp(const X509V3_EXT_METHOD *method, void *pidp, BIO *out, return 1; } -static int i2r_crldp(const X509V3_EXT_METHOD *method, void *pcrldp, BIO *out, - int indent) { +int i2r_crldp(const X509V3_EXT_METHOD *method, void *pcrldp, BIO *out, + int indent) { STACK_OF(DIST_POINT) *crld = reinterpret_cast(pcrldp); DIST_POINT *point; size_t i; @@ -521,31 +419,46 @@ static int i2r_crldp(const X509V3_EXT_METHOD *method, void *pcrldp, BIO *out, return 1; } -int bssl::DIST_POINT_set_dpname(DIST_POINT_NAME *dpn, X509_NAME *iname) { - size_t i; - STACK_OF(X509_NAME_ENTRY) *frag; - X509_NAME_ENTRY *ne; - if (!dpn || (dpn->type != 1)) { - return 1; - } - frag = dpn->name.relativename; - dpn->dpname = X509_NAME_dup(iname); - if (!dpn->dpname) { - return 0; - } - for (i = 0; i < sk_X509_NAME_ENTRY_num(frag); i++) { - ne = sk_X509_NAME_ENTRY_value(frag, i); - if (!X509_NAME_add_entry(dpn->dpname, ne, -1, i ? 0 : 1)) { - X509_NAME_free(dpn->dpname); - dpn->dpname = nullptr; - return 0; - } - } - // generate cached encoding of name - if (i2d_X509_NAME(dpn->dpname, nullptr) < 0) { - X509_NAME_free(dpn->dpname); - dpn->dpname = nullptr; - return 0; - } - return 1; -} +} // namespace + +const X509V3_EXT_METHOD bssl::v3_crld = { + NID_crl_distribution_points, + 0, + ASN1_ITEM_ref(CRL_DIST_POINTS), + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + v2i_crld, + i2r_crldp, + nullptr, + nullptr, +}; + +const X509V3_EXT_METHOD bssl::v3_freshest_crl = { + NID_freshest_crl, 0, ASN1_ITEM_ref(CRL_DIST_POINTS), + nullptr, nullptr, nullptr, + nullptr, nullptr, nullptr, + nullptr, v2i_crld, i2r_crldp, + nullptr, nullptr, +}; + +const X509V3_EXT_METHOD bssl::v3_idp = { + NID_issuing_distribution_point, + X509V3_EXT_MULTILINE, + ASN1_ITEM_ref(ISSUING_DIST_POINT), + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + v2i_idp, + i2r_idp, + nullptr, + nullptr, +}; diff --git a/third_party/boringssl/src/crypto/x509/v3_enum.cc b/third_party/boringssl/src/crypto/x509/v3_enum.cc index 41440fe7e..524761e39 100644 --- a/third_party/boringssl/src/crypto/x509/v3_enum.cc +++ b/third_party/boringssl/src/crypto/x509/v3_enum.cc @@ -24,9 +24,11 @@ using namespace bssl; +namespace { + typedef BIT_STRING_BITNAME ENUMERATED_NAMES; -static const ENUMERATED_NAMES crl_reasons[] = { +const ENUMERATED_NAMES crl_reasons[] = { {CRL_REASON_UNSPECIFIED, "Unspecified", "unspecified"}, {CRL_REASON_KEY_COMPROMISE, "Key Compromise", "keyCompromise"}, {CRL_REASON_CA_COMPROMISE, "CA Compromise", "CACompromise"}, @@ -42,8 +44,7 @@ static const ENUMERATED_NAMES crl_reasons[] = { {CRL_REASON_AA_COMPROMISE, "AA Compromise", "AACompromise"}, {-1, nullptr, nullptr}}; -static char *i2s_ASN1_ENUMERATED_TABLE(const X509V3_EXT_METHOD *method, - void *ext) { +char *i2s_ASN1_ENUMERATED_TABLE(const X509V3_EXT_METHOD *method, void *ext) { const ASN1_ENUMERATED *e = reinterpret_cast(ext); long strval = ASN1_ENUMERATED_get(e); for (const ENUMERATED_NAMES *enam = @@ -56,6 +57,8 @@ static char *i2s_ASN1_ENUMERATED_TABLE(const X509V3_EXT_METHOD *method, return i2s_ASN1_ENUMERATED(method, e); } +} // namespace + const X509V3_EXT_METHOD bssl::v3_crl_reason = { NID_crl_reason, 0, diff --git a/third_party/boringssl/src/crypto/x509/v3_extku.cc b/third_party/boringssl/src/crypto/x509/v3_extku.cc index 60c623823..46e6f11e3 100644 --- a/third_party/boringssl/src/crypto/x509/v3_extku.cc +++ b/third_party/boringssl/src/crypto/x509/v3_extku.cc @@ -25,37 +25,17 @@ using namespace bssl; -static void *v2i_EXTENDED_KEY_USAGE(const X509V3_EXT_METHOD *method, - const X509V3_CTX *ctx, - const STACK_OF(CONF_VALUE) *nval); -static STACK_OF(CONF_VALUE) *i2v_EXTENDED_KEY_USAGE( - const X509V3_EXT_METHOD *method, void *eku, STACK_OF(CONF_VALUE) *extlist); - -const X509V3_EXT_METHOD bssl::v3_ext_ku = { - NID_ext_key_usage, - 0, - ASN1_ITEM_ref(EXTENDED_KEY_USAGE), - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - i2v_EXTENDED_KEY_USAGE, - v2i_EXTENDED_KEY_USAGE, - nullptr, - nullptr, - nullptr, -}; - ASN1_ITEM_TEMPLATE(EXTENDED_KEY_USAGE) = ASN1_EX_TEMPLATE_TYPE( ASN1_TFLG_SEQUENCE_OF, 0, EXTENDED_KEY_USAGE, ASN1_OBJECT) ASN1_ITEM_TEMPLATE_END(EXTENDED_KEY_USAGE) IMPLEMENT_ASN1_FUNCTIONS_const(EXTENDED_KEY_USAGE) -static STACK_OF(CONF_VALUE) *i2v_EXTENDED_KEY_USAGE( - const X509V3_EXT_METHOD *method, void *a, STACK_OF(CONF_VALUE) *ext_list) { +namespace { + +STACK_OF(CONF_VALUE) *i2v_EXTENDED_KEY_USAGE(const X509V3_EXT_METHOD *method, + void *a, + STACK_OF(CONF_VALUE) *ext_list) { const EXTENDED_KEY_USAGE *eku = reinterpret_cast(a); for (size_t i = 0; i < sk_ASN1_OBJECT_num(eku); i++) { @@ -67,9 +47,9 @@ static STACK_OF(CONF_VALUE) *i2v_EXTENDED_KEY_USAGE( return ext_list; } -static void *v2i_EXTENDED_KEY_USAGE(const X509V3_EXT_METHOD *method, - const X509V3_CTX *ctx, - const STACK_OF(CONF_VALUE) *nval) { +void *v2i_EXTENDED_KEY_USAGE(const X509V3_EXT_METHOD *method, + const X509V3_CTX *ctx, + const STACK_OF(CONF_VALUE) *nval) { EXTENDED_KEY_USAGE *extku = sk_ASN1_OBJECT_new_null(); if (extku == nullptr) { return nullptr; @@ -95,3 +75,22 @@ static void *v2i_EXTENDED_KEY_USAGE(const X509V3_EXT_METHOD *method, return extku; } + +} // namespace + +const X509V3_EXT_METHOD bssl::v3_ext_ku = { + NID_ext_key_usage, + 0, + ASN1_ITEM_ref(EXTENDED_KEY_USAGE), + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + i2v_EXTENDED_KEY_USAGE, + v2i_EXTENDED_KEY_USAGE, + nullptr, + nullptr, + nullptr, +}; diff --git a/third_party/boringssl/src/crypto/x509/v3_genn.cc b/third_party/boringssl/src/crypto/x509/v3_genn.cc index 67a0743b6..1f0c9de05 100644 --- a/third_party/boringssl/src/crypto/x509/v3_genn.cc +++ b/third_party/boringssl/src/crypto/x509/v3_genn.cc @@ -74,16 +74,19 @@ IMPLEMENT_ASN1_FUNCTIONS_const(GENERAL_NAMES) IMPLEMENT_ASN1_DUP_FUNCTION_const(GENERAL_NAME) -static int edipartyname_cmp(const EDIPARTYNAME *a, const EDIPARTYNAME *b) { - // nameAssigner is optional and may be NULL. - if (a->nameAssigner == nullptr) { - if (b->nameAssigner != nullptr) { - return -1; +namespace { + +int edipartyname_cmp(const EDIPARTYNAME *a, const EDIPARTYNAME *b) { + // nameAssigner is optional and may be NULL. An absent nameAssigner sorts + // before a present one. + if (a->nameAssigner == nullptr || b->nameAssigner == nullptr) { + if (a->nameAssigner != b->nameAssigner) { + return a->nameAssigner == nullptr ? -1 : 1; } } else { - if (b->nameAssigner == nullptr || - ASN1_STRING_cmp(a->nameAssigner, b->nameAssigner) != 0) { - return -1; + int ret = ASN1_STRING_cmp(a->nameAssigner, b->nameAssigner); + if (ret != 0) { + return ret; } } @@ -91,26 +94,21 @@ static int edipartyname_cmp(const EDIPARTYNAME *a, const EDIPARTYNAME *b) { return ASN1_STRING_cmp(a->partyName, b->partyName); } -// Returns 0 if they are equal, != 0 otherwise. -static int othername_cmp(const OTHERNAME *a, const OTHERNAME *b) { - int result = -1; - - if (!a || !b) { - return -1; - } +// a and b are guaranteed to be non-NULL. +int othername_cmp(const OTHERNAME *a, const OTHERNAME *b) { // Check their type first. - if ((result = OBJ_cmp(a->type_id, b->type_id)) != 0) { - return result; + int ret = OBJ_cmp(a->type_id, b->type_id); + if (ret != 0) { + return ret; } - // Check the value. - result = ASN1_TYPE_cmp(a->value, b->value); - return result; + return ASN1_TYPE_cmp(a->value, b->value); } -// Returns 0 if they are equal, != 0 otherwise. +} // namespace + int bssl::GENERAL_NAME_cmp(const GENERAL_NAME *a, const GENERAL_NAME *b) { - if (!a || !b || a->type != b->type) { - return -1; + if (a->type != b->type) { + return a->type < b->type ? -1 : 1; } switch (a->type) { @@ -138,7 +136,8 @@ int bssl::GENERAL_NAME_cmp(const GENERAL_NAME *a, const GENERAL_NAME *b) { return OBJ_cmp(a->d.rid, b->d.rid); } - return -1; + // Unreachable: `type` is always one of the CHOICE arms above. + abort(); } void GENERAL_NAME_set0_value(GENERAL_NAME *a, int type, void *value) { diff --git a/third_party/boringssl/src/crypto/x509/v3_ia5.cc b/third_party/boringssl/src/crypto/x509/v3_ia5.cc index a6b8aed9f..a51ae74c7 100644 --- a/third_party/boringssl/src/crypto/x509/v3_ia5.cc +++ b/third_party/boringssl/src/crypto/x509/v3_ia5.cc @@ -28,7 +28,9 @@ using namespace bssl; -static char *i2s_ASN1_IA5STRING(const X509V3_EXT_METHOD *method, void *ext) { +namespace { + +char *i2s_ASN1_IA5STRING(const X509V3_EXT_METHOD *method, void *ext) { const ASN1_IA5STRING *ia5 = reinterpret_cast(ext); char *tmp; if (!ia5 || !ia5->length) { @@ -42,8 +44,8 @@ static char *i2s_ASN1_IA5STRING(const X509V3_EXT_METHOD *method, void *ext) { return tmp; } -static void *s2i_ASN1_IA5STRING(const X509V3_EXT_METHOD *method, - const X509V3_CTX *ctx, const char *str) { +void *s2i_ASN1_IA5STRING(const X509V3_EXT_METHOD *method, + const X509V3_CTX *ctx, const char *str) { ASN1_IA5STRING *ia5; if (!str) { OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_NULL_ARGUMENT); @@ -61,6 +63,8 @@ static void *s2i_ASN1_IA5STRING(const X509V3_EXT_METHOD *method, return nullptr; } +} // namespace + #define EXT_IA5STRING(nid) \ { \ nid, 0, ASN1_ITEM_ref(ASN1_IA5STRING), 0, 0, 0, 0, i2s_ASN1_IA5STRING, \ diff --git a/third_party/boringssl/src/crypto/x509/v3_info.cc b/third_party/boringssl/src/crypto/x509/v3_info.cc index 21d463871..be017f57b 100644 --- a/third_party/boringssl/src/crypto/x509/v3_info.cc +++ b/third_party/boringssl/src/crypto/x509/v3_info.cc @@ -28,46 +28,6 @@ using namespace bssl; -static STACK_OF(CONF_VALUE) *i2v_AUTHORITY_INFO_ACCESS( - const X509V3_EXT_METHOD *method, void *ext, STACK_OF(CONF_VALUE) *ret); -static void *v2i_AUTHORITY_INFO_ACCESS(const X509V3_EXT_METHOD *method, - const X509V3_CTX *ctx, - const STACK_OF(CONF_VALUE) *nval); - -const X509V3_EXT_METHOD bssl::v3_info = { - NID_info_access, - X509V3_EXT_MULTILINE, - ASN1_ITEM_ref(AUTHORITY_INFO_ACCESS), - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - i2v_AUTHORITY_INFO_ACCESS, - v2i_AUTHORITY_INFO_ACCESS, - nullptr, - nullptr, - nullptr, -}; - -const X509V3_EXT_METHOD bssl::v3_sinfo = { - NID_sinfo_access, - X509V3_EXT_MULTILINE, - ASN1_ITEM_ref(AUTHORITY_INFO_ACCESS), - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - i2v_AUTHORITY_INFO_ACCESS, - v2i_AUTHORITY_INFO_ACCESS, - nullptr, - nullptr, - nullptr, -}; - ASN1_SEQUENCE(ACCESS_DESCRIPTION) = { ASN1_SIMPLE(ACCESS_DESCRIPTION, method, ASN1_OBJECT), ASN1_SIMPLE(ACCESS_DESCRIPTION, location, GENERAL_NAME), @@ -81,8 +41,11 @@ ASN1_ITEM_TEMPLATE_END(AUTHORITY_INFO_ACCESS) IMPLEMENT_ASN1_FUNCTIONS_const(AUTHORITY_INFO_ACCESS) -static STACK_OF(CONF_VALUE) *i2v_AUTHORITY_INFO_ACCESS( - const X509V3_EXT_METHOD *method, void *ext, STACK_OF(CONF_VALUE) *ret) { +namespace { + +STACK_OF(CONF_VALUE) *i2v_AUTHORITY_INFO_ACCESS(const X509V3_EXT_METHOD *method, + void *ext, + STACK_OF(CONF_VALUE) *ret) { const AUTHORITY_INFO_ACCESS *ainfo = reinterpret_cast(ext); ACCESS_DESCRIPTION *desc; @@ -120,9 +83,9 @@ static STACK_OF(CONF_VALUE) *i2v_AUTHORITY_INFO_ACCESS( return nullptr; } -static void *v2i_AUTHORITY_INFO_ACCESS(const X509V3_EXT_METHOD *method, - const X509V3_CTX *ctx, - const STACK_OF(CONF_VALUE) *nval) { +void *v2i_AUTHORITY_INFO_ACCESS(const X509V3_EXT_METHOD *method, + const X509V3_CTX *ctx, + const STACK_OF(CONF_VALUE) *nval) { UniquePtr ainfo(sk_ACCESS_DESCRIPTION_new_null()); if (ainfo == nullptr) { return nullptr; @@ -160,3 +123,39 @@ static void *v2i_AUTHORITY_INFO_ACCESS(const X509V3_EXT_METHOD *method, } return ainfo.release(); } + +} // namespace + +const X509V3_EXT_METHOD bssl::v3_info = { + NID_info_access, + X509V3_EXT_MULTILINE, + ASN1_ITEM_ref(AUTHORITY_INFO_ACCESS), + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + i2v_AUTHORITY_INFO_ACCESS, + v2i_AUTHORITY_INFO_ACCESS, + nullptr, + nullptr, + nullptr, +}; + +const X509V3_EXT_METHOD bssl::v3_sinfo = { + NID_sinfo_access, + X509V3_EXT_MULTILINE, + ASN1_ITEM_ref(AUTHORITY_INFO_ACCESS), + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + i2v_AUTHORITY_INFO_ACCESS, + v2i_AUTHORITY_INFO_ACCESS, + nullptr, + nullptr, + nullptr, +}; diff --git a/third_party/boringssl/src/crypto/x509/v3_int.cc b/third_party/boringssl/src/crypto/x509/v3_int.cc index 7d072d162..608e56584 100644 --- a/third_party/boringssl/src/crypto/x509/v3_int.cc +++ b/third_party/boringssl/src/crypto/x509/v3_int.cc @@ -22,15 +22,19 @@ using namespace bssl; -static char *i2s_ASN1_INTEGER_cb(const X509V3_EXT_METHOD *method, void *ext) { +namespace { + +char *i2s_ASN1_INTEGER_cb(const X509V3_EXT_METHOD *method, void *ext) { return i2s_ASN1_INTEGER(method, reinterpret_cast(ext)); } -static void *s2i_asn1_int(const X509V3_EXT_METHOD *meth, const X509V3_CTX *ctx, - const char *value) { +void *s2i_asn1_int(const X509V3_EXT_METHOD *meth, const X509V3_CTX *ctx, + const char *value) { return s2i_ASN1_INTEGER(meth, value); } +} // namespace + const X509V3_EXT_METHOD bssl::v3_crl_num = { NID_crl_number, 0, diff --git a/third_party/boringssl/src/crypto/x509/v3_lib.cc b/third_party/boringssl/src/crypto/x509/v3_lib.cc index 2289afd3b..a2cf9e498 100644 --- a/third_party/boringssl/src/crypto/x509/v3_lib.cc +++ b/third_party/boringssl/src/crypto/x509/v3_lib.cc @@ -29,13 +29,17 @@ using namespace bssl; +namespace { + // This is indirected though a pointer to avoid a global destructor. If we ever // add bssl::NoDestructor, we can avoid this, but this API is already // problematic and not thread-safe. -static bssl::Vector *ext_list = nullptr; +bssl::Vector *ext_list = nullptr; + +} // namespace int X509V3_EXT_add(X509V3_EXT_METHOD *ext) { - // We only support |ASN1_ITEM|-based extensions. + // We only support `ASN1_ITEM`-based extensions. assert(ext->it != nullptr); // TODO(crbug.com/42290461): This API is not locked and doesn't check for diff --git a/third_party/boringssl/src/crypto/x509/v3_ncons.cc b/third_party/boringssl/src/crypto/x509/v3_ncons.cc index 58c15960b..d65febf6c 100644 --- a/third_party/boringssl/src/crypto/x509/v3_ncons.cc +++ b/third_party/boringssl/src/crypto/x509/v3_ncons.cc @@ -56,7 +56,7 @@ int equal_case(const CBS *a, const CBS *b) { if (CBS_len(a) != CBS_len(b)) { return 0; } - // Note we cannot use |OPENSSL_strncasecmp| because that would stop + // Note we cannot use `OPENSSL_strncasecmp` because that would stop // iterating at NUL. const uint8_t *a_data = CBS_data(a), *b_data = CBS_data(b); for (size_t i = 0; i < CBS_len(a); i++) { @@ -296,11 +296,11 @@ bool nc_get_valid_uri_host(CBS *out, const ASN1_IA5STRING *uri_name) { // makes this comparison easy. It is matched if the constraint is a prefix of // the name. int nc_dn(const X509_NAME *nm, const X509_NAME *base) { - const X509_NAME_CACHE *nm_cache = x509_name_get_cache(nm); + const X509NameCache *nm_cache = x509_name_get_cache(nm); if (nm_cache == nullptr) { return X509_V_ERR_OUT_OF_MEM; } - const X509_NAME_CACHE *base_cache = x509_name_get_cache(base); + const X509NameCache *base_cache = x509_name_get_cache(base); if (base_cache == nullptr) { return X509_V_ERR_OUT_OF_MEM; } @@ -337,7 +337,7 @@ int nc_dns(const ASN1_IA5STRING *dns, const ASN1_IA5STRING *base, } // Wildcard partial-match handling ("*.bar.com" matching name constraint - // "foo.bar.com"). This only handles the case where the the dnsname and the + // "foo.bar.com"). This only handles the case where the dnsname and the // constraint match after removing the leftmost label, otherwise it is handled // by falling through to the check of whether the dnsname is fully within or // fully outside of the constraint. @@ -354,7 +354,7 @@ int nc_dns(const ASN1_IA5STRING *dns, const ASN1_IA5STRING *base, } } - // If |base_cbs| begins with a '.', do a simple suffix comparison. This is + // If `base_cbs` begins with a '.', do a simple suffix comparison. This is // not part of RFC5280, but is part of OpenSSL's original behavior. if (starts_with(&base_cbs, '.')) { if (has_suffix_case(&dns_cbs, &base_cbs)) { @@ -734,7 +734,7 @@ int NAME_CONSTRAINTS_check(const X509 *x, const NAME_CONSTRAINTS *nc) { // check. const auto *x_impl = FromOpaque(x); size_t name_count = - X509_NAME_entry_count(nm) + sk_GENERAL_NAME_num(x_impl->altname); + X509_NAME_entry_count(nm) + sk_GENERAL_NAME_num(x_impl->altname.get()); size_t constraint_count = sk_GENERAL_SUBTREE_num(nc->permittedSubtrees) + sk_GENERAL_SUBTREE_num(nc->excludedSubtrees); size_t check_count = constraint_count * name_count; @@ -787,7 +787,7 @@ int NAME_CONSTRAINTS_check(const X509 *x, const NAME_CONSTRAINTS *nc) { } } - for (const GENERAL_NAME *gen : x_impl->altname) { + for (const GENERAL_NAME *gen : x_impl->altname.get()) { int r = nc_match(gen, nc, /*case_insensitive_exclude_localpart=*/false); if (r != X509_V_OK) { return r; diff --git a/third_party/boringssl/src/crypto/x509/v3_ocsp.cc b/third_party/boringssl/src/crypto/x509/v3_ocsp.cc index f70ba2e78..f9ba15190 100644 --- a/third_party/boringssl/src/crypto/x509/v3_ocsp.cc +++ b/third_party/boringssl/src/crypto/x509/v3_ocsp.cc @@ -25,13 +25,33 @@ using namespace bssl; // OCSP extensions and a couple of CRL entry extensions -static int i2r_ocsp_acutoff(const X509V3_EXT_METHOD *method, void *nonce, - BIO *out, int indent); +namespace { -static int i2r_ocsp_nocheck(const X509V3_EXT_METHOD *method, void *nocheck, - BIO *out, int indent); -static void *s2i_ocsp_nocheck(const X509V3_EXT_METHOD *method, - const X509V3_CTX *ctx, const char *str); +int i2r_ocsp_acutoff(const X509V3_EXT_METHOD *method, void *cutoff, BIO *bp, + int ind) { + if (BIO_printf(bp, "%*s", ind, "") <= 0) { + return 0; + } + if (!ASN1_GENERALIZEDTIME_print( + bp, reinterpret_cast(cutoff))) { + return 0; + } + return 1; +} + +// Nocheck is just a single NULL. Don't print anything and always set it + +int i2r_ocsp_nocheck(const X509V3_EXT_METHOD *method, void *nocheck, BIO *out, + int indent) { + return 1; +} + +void *s2i_ocsp_nocheck(const X509V3_EXT_METHOD *method, const X509V3_CTX *ctx, + const char *str) { + return ASN1_NULL_new(); +} + +} // namespace const X509V3_EXT_METHOD bssl::v3_crl_invdate = { NID_invalidity_date, @@ -66,27 +86,3 @@ const X509V3_EXT_METHOD bssl::v3_ocsp_nocheck = { nullptr, nullptr, }; - -static int i2r_ocsp_acutoff(const X509V3_EXT_METHOD *method, void *cutoff, - BIO *bp, int ind) { - if (BIO_printf(bp, "%*s", ind, "") <= 0) { - return 0; - } - if (!ASN1_GENERALIZEDTIME_print( - bp, reinterpret_cast(cutoff))) { - return 0; - } - return 1; -} - -// Nocheck is just a single NULL. Don't print anything and always set it - -static int i2r_ocsp_nocheck(const X509V3_EXT_METHOD *method, void *nocheck, - BIO *out, int indent) { - return 1; -} - -static void *s2i_ocsp_nocheck(const X509V3_EXT_METHOD *method, - const X509V3_CTX *ctx, const char *str) { - return ASN1_NULL_new(); -} diff --git a/third_party/boringssl/src/crypto/x509/v3_pcons.cc b/third_party/boringssl/src/crypto/x509/v3_pcons.cc index f74d89cfd..eaf7ef804 100644 --- a/third_party/boringssl/src/crypto/x509/v3_pcons.cc +++ b/third_party/boringssl/src/crypto/x509/v3_pcons.cc @@ -27,29 +27,6 @@ using namespace bssl; -static STACK_OF(CONF_VALUE) *i2v_POLICY_CONSTRAINTS( - const X509V3_EXT_METHOD *method, void *bcons, - STACK_OF(CONF_VALUE) *extlist); -static void *v2i_POLICY_CONSTRAINTS(const X509V3_EXT_METHOD *method, - const X509V3_CTX *ctx, - const STACK_OF(CONF_VALUE) *values); - -const X509V3_EXT_METHOD bssl::v3_policy_constraints = { - NID_policy_constraints, - 0, - ASN1_ITEM_ref(POLICY_CONSTRAINTS), - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - i2v_POLICY_CONSTRAINTS, - v2i_POLICY_CONSTRAINTS, - nullptr, - nullptr, - nullptr}; - ASN1_SEQUENCE(POLICY_CONSTRAINTS) = { ASN1_IMP_OPT(POLICY_CONSTRAINTS, requireExplicitPolicy, ASN1_INTEGER, 0), ASN1_IMP_OPT(POLICY_CONSTRAINTS, inhibitPolicyMapping, ASN1_INTEGER, 1), @@ -57,8 +34,11 @@ ASN1_SEQUENCE(POLICY_CONSTRAINTS) = { IMPLEMENT_ASN1_ALLOC_FUNCTIONS(POLICY_CONSTRAINTS) -static STACK_OF(CONF_VALUE) *i2v_POLICY_CONSTRAINTS( - const X509V3_EXT_METHOD *method, void *a, STACK_OF(CONF_VALUE) *extlist) { +namespace { + +STACK_OF(CONF_VALUE) *i2v_POLICY_CONSTRAINTS(const X509V3_EXT_METHOD *method, + void *a, + STACK_OF(CONF_VALUE) *extlist) { const POLICY_CONSTRAINTS *pcons = reinterpret_cast(a); X509V3_add_value_int("Require Explicit Policy", pcons->requireExplicitPolicy, &extlist); @@ -67,9 +47,9 @@ static STACK_OF(CONF_VALUE) *i2v_POLICY_CONSTRAINTS( return extlist; } -static void *v2i_POLICY_CONSTRAINTS(const X509V3_EXT_METHOD *method, - const X509V3_CTX *ctx, - const STACK_OF(CONF_VALUE) *values) { +void *v2i_POLICY_CONSTRAINTS(const X509V3_EXT_METHOD *method, + const X509V3_CTX *ctx, + const STACK_OF(CONF_VALUE) *values) { POLICY_CONSTRAINTS *pcons = nullptr; if (!(pcons = POLICY_CONSTRAINTS_new())) { return nullptr; @@ -100,3 +80,21 @@ static void *v2i_POLICY_CONSTRAINTS(const X509V3_EXT_METHOD *method, POLICY_CONSTRAINTS_free(pcons); return nullptr; } + +} // namespace + +const X509V3_EXT_METHOD bssl::v3_policy_constraints = { + NID_policy_constraints, + 0, + ASN1_ITEM_ref(POLICY_CONSTRAINTS), + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + i2v_POLICY_CONSTRAINTS, + v2i_POLICY_CONSTRAINTS, + nullptr, + nullptr, + nullptr}; diff --git a/third_party/boringssl/src/crypto/x509/v3_pmaps.cc b/third_party/boringssl/src/crypto/x509/v3_pmaps.cc index bb5fa50d9..ab262d088 100644 --- a/third_party/boringssl/src/crypto/x509/v3_pmaps.cc +++ b/third_party/boringssl/src/crypto/x509/v3_pmaps.cc @@ -25,29 +25,6 @@ using namespace bssl; -static void *v2i_POLICY_MAPPINGS(const X509V3_EXT_METHOD *method, - const X509V3_CTX *ctx, - const STACK_OF(CONF_VALUE) *nval); -static STACK_OF(CONF_VALUE) *i2v_POLICY_MAPPINGS( - const X509V3_EXT_METHOD *method, void *pmps, STACK_OF(CONF_VALUE) *extlist); - -const X509V3_EXT_METHOD bssl::v3_policy_mappings = { - NID_policy_mappings, - 0, - ASN1_ITEM_ref(POLICY_MAPPINGS), - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - i2v_POLICY_MAPPINGS, - v2i_POLICY_MAPPINGS, - nullptr, - nullptr, - nullptr, -}; - ASN1_SEQUENCE(POLICY_MAPPING) = { ASN1_SIMPLE(POLICY_MAPPING, issuerDomainPolicy, ASN1_OBJECT), ASN1_SIMPLE(POLICY_MAPPING, subjectDomainPolicy, ASN1_OBJECT), @@ -59,8 +36,11 @@ ASN1_ITEM_TEMPLATE_END(POLICY_MAPPINGS) IMPLEMENT_ASN1_ALLOC_FUNCTIONS(POLICY_MAPPING) -static STACK_OF(CONF_VALUE) *i2v_POLICY_MAPPINGS( - const X509V3_EXT_METHOD *method, void *a, STACK_OF(CONF_VALUE) *ext_list) { +namespace { + +STACK_OF(CONF_VALUE) *i2v_POLICY_MAPPINGS(const X509V3_EXT_METHOD *method, + void *a, + STACK_OF(CONF_VALUE) *ext_list) { const POLICY_MAPPINGS *pmaps = reinterpret_cast(a); for (size_t i = 0; i < sk_POLICY_MAPPING_num(pmaps); i++) { const POLICY_MAPPING *pmap = sk_POLICY_MAPPING_value(pmaps, i); @@ -72,9 +52,9 @@ static STACK_OF(CONF_VALUE) *i2v_POLICY_MAPPINGS( return ext_list; } -static void *v2i_POLICY_MAPPINGS(const X509V3_EXT_METHOD *method, - const X509V3_CTX *ctx, - const STACK_OF(CONF_VALUE) *nval) { +void *v2i_POLICY_MAPPINGS(const X509V3_EXT_METHOD *method, + const X509V3_CTX *ctx, + const STACK_OF(CONF_VALUE) *nval) { POLICY_MAPPINGS *pmaps = sk_POLICY_MAPPING_new_null(); if (pmaps == nullptr) { return nullptr; @@ -108,3 +88,22 @@ static void *v2i_POLICY_MAPPINGS(const X509V3_EXT_METHOD *method, sk_POLICY_MAPPING_pop_free(pmaps, POLICY_MAPPING_free); return nullptr; } + +} // namespace + +const X509V3_EXT_METHOD bssl::v3_policy_mappings = { + NID_policy_mappings, + 0, + ASN1_ITEM_ref(POLICY_MAPPINGS), + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + i2v_POLICY_MAPPINGS, + v2i_POLICY_MAPPINGS, + nullptr, + nullptr, + nullptr, +}; diff --git a/third_party/boringssl/src/crypto/x509/v3_prn.cc b/third_party/boringssl/src/crypto/x509/v3_prn.cc index c2da548f9..ecf8c5d18 100644 --- a/third_party/boringssl/src/crypto/x509/v3_prn.cc +++ b/third_party/boringssl/src/crypto/x509/v3_prn.cc @@ -23,12 +23,11 @@ // Extension printing routines -static int unknown_ext_print(BIO *out, const X509_EXTENSION *ext, - unsigned long flag, int indent, int supported); +namespace { // Print out a name+value stack -static void X509V3_EXT_val_prn(BIO *out, const STACK_OF(CONF_VALUE) *val, - int indent, int ml) { +void X509V3_EXT_val_prn(BIO *out, const STACK_OF(CONF_VALUE) *val, int indent, + int ml) { if (!val) { return; } @@ -58,6 +57,34 @@ static void X509V3_EXT_val_prn(BIO *out, const STACK_OF(CONF_VALUE) *val, } } +int unknown_ext_print(BIO *out, const X509_EXTENSION *ext, unsigned long flag, + int indent, int supported) { + switch (flag & X509V3_EXT_UNKNOWN_MASK) { + case X509V3_EXT_DEFAULT: + return 0; + + case X509V3_EXT_ERROR_UNKNOWN: + if (supported) { + BIO_printf(out, "%*s", indent, ""); + } else { + BIO_printf(out, "%*s", indent, ""); + } + return 1; + + case X509V3_EXT_PARSE_UNKNOWN: + case X509V3_EXT_DUMP_UNKNOWN: { + const ASN1_STRING *data = X509_EXTENSION_get_data(ext); + return BIO_hexdump(out, ASN1_STRING_get0_data(data), + ASN1_STRING_length(data), indent); + } + + default: + return 1; + } +} + +} // namespace + // Main routine: print out a general extension int X509V3_EXT_print(BIO *out, const X509_EXTENSION *ext, unsigned long flag, @@ -144,32 +171,6 @@ int X509V3_extensions_print(BIO *bp, const char *title, return 1; } -static int unknown_ext_print(BIO *out, const X509_EXTENSION *ext, - unsigned long flag, int indent, int supported) { - switch (flag & X509V3_EXT_UNKNOWN_MASK) { - case X509V3_EXT_DEFAULT: - return 0; - - case X509V3_EXT_ERROR_UNKNOWN: - if (supported) { - BIO_printf(out, "%*s", indent, ""); - } else { - BIO_printf(out, "%*s", indent, ""); - } - return 1; - - case X509V3_EXT_PARSE_UNKNOWN: - case X509V3_EXT_DUMP_UNKNOWN: { - const ASN1_STRING *data = X509_EXTENSION_get_data(ext); - return BIO_hexdump(out, ASN1_STRING_get0_data(data), - ASN1_STRING_length(data), indent); - } - - default: - return 1; - } -} - int X509V3_EXT_print_fp(FILE *fp, const X509_EXTENSION *ext, int flag, int indent) { BIO *bio_tmp; diff --git a/third_party/boringssl/src/crypto/x509/v3_purp.cc b/third_party/boringssl/src/crypto/x509/v3_purp.cc index 8d7a2915c..0ffe349be 100644 --- a/third_party/boringssl/src/crypto/x509/v3_purp.cc +++ b/third_party/boringssl/src/crypto/x509/v3_purp.cc @@ -18,6 +18,7 @@ #include #include #include +#include #include #include #include @@ -43,29 +44,132 @@ struct x509_purpose_st { ((FromOpaque(x)->ex_flags & EXFLAG_XKUSAGE) && \ !(FromOpaque(x)->ex_xkusage & (usage))) -static int check_ca(const X509 *x); -static int check_purpose_ssl_client(const X509_PURPOSE *xp, const X509 *x, - int ca); -static int check_purpose_ssl_server(const X509_PURPOSE *xp, const X509 *x, - int ca); -static int check_purpose_ns_ssl_server(const X509_PURPOSE *xp, const X509 *x, - int ca); -static int check_purpose_smime_sign(const X509_PURPOSE *xp, const X509 *x, - int ca); -static int check_purpose_smime_encrypt(const X509_PURPOSE *xp, const X509 *x, - int ca); -static int check_purpose_crl_sign(const X509_PURPOSE *xp, const X509 *x, - int ca); -static int check_purpose_timestamp_sign(const X509_PURPOSE *xp, const X509 *x, - int ca); -static int no_check(const X509_PURPOSE *xp, const X509 *x, int ca); - -// X509_TRUST_NONE is not a valid |X509_TRUST_*| constant. It is used by -// |X509_PURPOSE_ANY| to indicate that it has no corresponding trust type and -// cannot be used with |X509_STORE_CTX_set_purpose|. +namespace { + +int setup_crldp(const X509 *x) { + int j; + auto *impl = FromOpaque(x); + impl->crldp.reset(reinterpret_cast( + X509_get_ext_d2i(x, NID_crl_distribution_points, &j, nullptr))); + if (impl->crldp == nullptr && j != -1) { + return 0; + } + return 1; +} + +// check_ca returns one if `x` should be considered a CA certificate and zero +// otherwise. +int check_ca(const X509 *x) { + // keyUsage if present should allow cert signing + if (ku_reject(x, X509v3_KU_KEY_CERT_SIGN)) { + return 0; + } + // Version 1 certificates are considered CAs and don't have extensions. + const auto *impl = FromOpaque(x); + if ((impl->ex_flags & V1_ROOT) == V1_ROOT) { + return 1; + } + // Otherwise, it's only a CA if basicConstraints says so. + return ((impl->ex_flags & EXFLAG_BCONS) && (impl->ex_flags & EXFLAG_CA)); +} + +// check_purpose returns one if `x` is a valid part of a certificate path for +// extended key usage `required_xku` and at least one of key usages in +// `required_kus`. `ca` indicates whether `x` is a CA or end-entity certificate. +int check_purpose(const X509 *x, int ca, int required_xku, int required_kus) { + // Check extended key usage on the entire chain. + if (required_xku != 0 && xku_reject(x, required_xku)) { + return 0; + } + + // Check key usages only on the end-entity certificate. + return ca || !ku_reject(x, required_kus); +} + +int check_purpose_ssl_client(const X509_PURPOSE *xp, const X509 *x, int ca) { + // We need to do digital signatures or key agreement. + // + // TODO(davidben): We do not implement any TLS client certificate modes based + // on key agreement. + return check_purpose(x, ca, XKU_SSL_CLIENT, + X509v3_KU_DIGITAL_SIGNATURE | X509v3_KU_KEY_AGREEMENT); +} + +// Key usage needed for TLS/SSL server: digital signature, encipherment or +// key agreement. The ssl code can check this more thoroughly for individual +// key types. +#define X509v3_KU_TLS \ + (X509v3_KU_DIGITAL_SIGNATURE | X509v3_KU_KEY_ENCIPHERMENT | \ + X509v3_KU_KEY_AGREEMENT) + +int check_purpose_ssl_server(const X509_PURPOSE *xp, const X509 *x, int ca) { + return check_purpose(x, ca, XKU_SSL_SERVER, X509v3_KU_TLS); +} + +int check_purpose_ns_ssl_server(const X509_PURPOSE *xp, const X509 *x, int ca) { + // We need to encipher or Netscape complains. + return check_purpose(x, ca, XKU_SSL_SERVER, X509v3_KU_KEY_ENCIPHERMENT); +} + +int check_purpose_smime_sign(const X509_PURPOSE *xp, const X509 *x, int ca) { + return check_purpose(x, ca, XKU_SMIME, + X509v3_KU_DIGITAL_SIGNATURE | X509v3_KU_NON_REPUDIATION); +} + +int check_purpose_smime_encrypt(const X509_PURPOSE *xp, const X509 *x, int ca) { + return check_purpose(x, ca, XKU_SMIME, X509v3_KU_KEY_ENCIPHERMENT); +} + +int check_purpose_crl_sign(const X509_PURPOSE *xp, const X509 *x, int ca) { + return check_purpose(x, ca, /*required_xku=*/0, X509v3_KU_CRL_SIGN); +} + +int check_purpose_timestamp_sign(const X509_PURPOSE *xp, const X509 *x, + int ca) { + if (ca) { + return 1; + } + + // Check the optional key usage field: + // if Key Usage is present, it must be one of digitalSignature + // and/or nonRepudiation (other values are not consistent and shall + // be rejected). + const auto *impl = FromOpaque(x); + if ((impl->ex_flags & EXFLAG_KUSAGE) && + ((impl->ex_kusage & + ~(X509v3_KU_NON_REPUDIATION | X509v3_KU_DIGITAL_SIGNATURE)) || + !(impl->ex_kusage & + (X509v3_KU_NON_REPUDIATION | X509v3_KU_DIGITAL_SIGNATURE)))) { + return 0; + } + + // Only time stamp key usage is permitted and it's required. + // + // TODO(davidben): Should we check EKUs up the chain like the other cases? + if (!(impl->ex_flags & EXFLAG_XKUSAGE) || impl->ex_xkusage != XKU_TIMESTAMP) { + return 0; + } + + // Extended Key Usage MUST be critical + int i_ext = X509_get_ext_by_NID(x, NID_ext_key_usage, -1); + if (i_ext >= 0) { + const X509_EXTENSION *ext = X509_get_ext(x, i_ext); + if (!X509_EXTENSION_get_critical(ext)) { + return 0; + } + } + + return 1; +} + +int no_check(const X509_PURPOSE *xp, const X509 *x, int ca) { return 1; } + +// X509_TRUST_NONE is not a valid `X509_TRUST_*` constant. It is used by +// `X509_PURPOSE_ANY` to indicate that it has no corresponding trust type and +// cannot be used with `X509_STORE_CTX_set_purpose`. #define X509_TRUST_NONE (-1) -static const X509_PURPOSE xstandard[] = { +const X509_PURPOSE xstandard[] = { {X509_PURPOSE_SSL_CLIENT, X509_TRUST_SSL_CLIENT, check_purpose_ssl_client, "sslclient"}, {X509_PURPOSE_SSL_SERVER, X509_TRUST_SSL_SERVER, check_purpose_ssl_server, @@ -79,14 +183,16 @@ static const X509_PURPOSE xstandard[] = { {X509_PURPOSE_CRL_SIGN, X509_TRUST_COMPAT, check_purpose_crl_sign, "crlsign"}, {X509_PURPOSE_ANY, X509_TRUST_NONE, no_check, "any"}, - // |X509_PURPOSE_OCSP_HELPER| performs no actual checks. OpenSSL's OCSP + // `X509_PURPOSE_OCSP_HELPER` performs no actual checks. OpenSSL's OCSP // implementation relied on the caller performing EKU and KU checks. {X509_PURPOSE_OCSP_HELPER, X509_TRUST_COMPAT, no_check, "ocsphelper"}, {X509_PURPOSE_TIMESTAMP_SIGN, X509_TRUST_TSA, check_purpose_timestamp_sign, "timestampsign"}, }; -int X509_check_purpose(X509 *x, int id, int ca) { +} // namespace + +int X509_check_purpose(const X509 *x, int id, int ca) { // This differs from OpenSSL, which uses -1 to indicate a fatal error and 0 to // indicate an invalid certificate. BoringSSL uses 0 for both. if (!x509v3_cache_extensions(x)) { @@ -100,9 +206,9 @@ int X509_check_purpose(X509 *x, int id, int ca) { if (pt == nullptr) { return 0; } - // Historically, |check_purpose| implementations other than |X509_PURPOSE_ANY| - // called |check_ca|. This is redundant with the |X509_V_ERR_INVALID_CA| - // logic, but |X509_check_purpose| is public API, so we preserve this + // Historically, `check_purpose` implementations other than `X509_PURPOSE_ANY` + // called `check_ca`. This is redundant with the `X509_V_ERR_INVALID_CA` + // logic, but `X509_check_purpose` is public API, so we preserve this // behavior. if (ca && id != X509_PURPOSE_ANY && !check_ca(x)) { return 0; @@ -142,45 +248,11 @@ int X509_supported_extension(const X509_EXTENSION *ex) { nid == NID_policy_constraints || // nid == NID_name_constraints || // nid == NID_policy_mappings || // - nid == NID_inhibit_any_policy; + nid == NID_inhibit_any_policy || // + nid == NID_pe_mtcCertificationAuthority_SHA256; } -static int setup_dp(X509 *x, DIST_POINT *dp) { - if (!dp->distpoint || (dp->distpoint->type != 1)) { - return 1; - } - X509_NAME *iname = nullptr; - for (size_t i = 0; i < sk_GENERAL_NAME_num(dp->CRLissuer); i++) { - GENERAL_NAME *gen = sk_GENERAL_NAME_value(dp->CRLissuer, i); - if (gen->type == GEN_DIRNAME) { - iname = gen->d.directoryName; - break; - } - } - if (!iname) { - iname = X509_get_issuer_name(x); - } - - return DIST_POINT_set_dpname(dp->distpoint, iname); -} - -static int setup_crldp(X509 *x) { - int j; - auto *impl = FromOpaque(x); - impl->crldp = reinterpret_cast( - X509_get_ext_d2i(x, NID_crl_distribution_points, &j, nullptr)); - if (impl->crldp == nullptr && j != -1) { - return 0; - } - for (size_t i = 0; i < sk_DIST_POINT_num(impl->crldp); i++) { - if (!setup_dp(x, sk_DIST_POINT_value(impl->crldp, i))) { - return 0; - } - } - return 1; -} - -int bssl::x509v3_cache_extensions(X509 *x) { +int bssl::x509v3_cache_extensions(const X509 *x) { BASIC_CONSTRAINTS *bs; ASN1_BIT_STRING *usage; EXTENDED_KEY_USAGE *extusage; @@ -219,7 +291,7 @@ int bssl::x509v3_cache_extensions(X509 *x) { impl->ex_flags |= EXFLAG_INVALID; impl->ex_pathlen = 0; } else { - // TODO(davidben): |ASN1_INTEGER_get| returns -1 on overflow, + // TODO(davidben): `ASN1_INTEGER_get` returns -1 on overflow, // which currently acts as if the constraint isn't present. This // works (an overflowing path length constraint may as well be // infinity), but Chromium's verifier simply treats values above @@ -299,13 +371,13 @@ int bssl::x509v3_cache_extensions(X509 *x) { impl->ex_flags |= EXFLAG_INVALID; } - impl->skid = reinterpret_cast( - X509_get_ext_d2i(x, NID_subject_key_identifier, &j, nullptr)); + impl->skid.reset(reinterpret_cast( + X509_get_ext_d2i(x, NID_subject_key_identifier, &j, nullptr))); if (impl->skid == nullptr && j != -1) { impl->ex_flags |= EXFLAG_INVALID; } - impl->akid = reinterpret_cast( - X509_get_ext_d2i(x, NID_authority_key_identifier, &j, nullptr)); + impl->akid.reset(reinterpret_cast( + X509_get_ext_d2i(x, NID_authority_key_identifier, &j, nullptr))); if (impl->akid == nullptr && j != -1) { impl->ex_flags |= EXFLAG_INVALID; } @@ -313,18 +385,18 @@ int bssl::x509v3_cache_extensions(X509 *x) { if (!X509_NAME_cmp(X509_get_subject_name(x), X509_get_issuer_name(x))) { impl->ex_flags |= EXFLAG_SI; // If SKID matches AKID also indicate self signed - if (X509_check_akid(x, impl->akid) == X509_V_OK && + if (X509_check_akid(x, impl->akid.get()) == X509_V_OK && !ku_reject(x, X509v3_KU_KEY_CERT_SIGN)) { impl->ex_flags |= EXFLAG_SS; } } - impl->altname = reinterpret_cast( - X509_get_ext_d2i(x, NID_subject_alt_name, &j, nullptr)); + impl->altname.reset(reinterpret_cast( + X509_get_ext_d2i(x, NID_subject_alt_name, &j, nullptr))); if (impl->altname == nullptr && j != -1) { impl->ex_flags |= EXFLAG_INVALID; } - impl->nc = reinterpret_cast( - X509_get_ext_d2i(x, NID_name_constraints, &j, nullptr)); + impl->nc.reset(reinterpret_cast( + X509_get_ext_d2i(x, NID_name_constraints, &j, nullptr))); if (impl->nc == nullptr && j != -1) { impl->ex_flags |= EXFLAG_INVALID; } @@ -347,140 +419,26 @@ int bssl::x509v3_cache_extensions(X509 *x) { return (impl->ex_flags & EXFLAG_INVALID) == 0; } -// check_ca returns one if |x| should be considered a CA certificate and zero -// otherwise. -static int check_ca(const X509 *x) { - // keyUsage if present should allow cert signing - if (ku_reject(x, X509v3_KU_KEY_CERT_SIGN)) { - return 0; - } - // Version 1 certificates are considered CAs and don't have extensions. - const auto *impl = FromOpaque(x); - if ((impl->ex_flags & V1_ROOT) == V1_ROOT) { - return 1; - } - // Otherwise, it's only a CA if basicConstraints says so. - return ((impl->ex_flags & EXFLAG_BCONS) && (impl->ex_flags & EXFLAG_CA)); -} - int X509_check_ca(const X509 *x) { - if (!x509v3_cache_extensions(const_cast(x))) { + if (!x509v3_cache_extensions(x)) { return 0; } return check_ca(x); } -// check_purpose returns one if |x| is a valid part of a certificate path for -// extended key usage |required_xku| and at least one of key usages in -// |required_kus|. |ca| indicates whether |x| is a CA or end-entity certificate. -static int check_purpose(const X509 *x, int ca, int required_xku, - int required_kus) { - // Check extended key usage on the entire chain. - if (required_xku != 0 && xku_reject(x, required_xku)) { - return 0; - } - - // Check key usages only on the end-entity certificate. - return ca || !ku_reject(x, required_kus); -} - -static int check_purpose_ssl_client(const X509_PURPOSE *xp, const X509 *x, - int ca) { - // We need to do digital signatures or key agreement. - // - // TODO(davidben): We do not implement any TLS client certificate modes based - // on key agreement. - return check_purpose(x, ca, XKU_SSL_CLIENT, - X509v3_KU_DIGITAL_SIGNATURE | X509v3_KU_KEY_AGREEMENT); -} - -// Key usage needed for TLS/SSL server: digital signature, encipherment or -// key agreement. The ssl code can check this more thoroughly for individual -// key types. -#define X509v3_KU_TLS \ - (X509v3_KU_DIGITAL_SIGNATURE | X509v3_KU_KEY_ENCIPHERMENT | \ - X509v3_KU_KEY_AGREEMENT) - -static int check_purpose_ssl_server(const X509_PURPOSE *xp, const X509 *x, - int ca) { - return check_purpose(x, ca, XKU_SSL_SERVER, X509v3_KU_TLS); -} - -static int check_purpose_ns_ssl_server(const X509_PURPOSE *xp, const X509 *x, - int ca) { - // We need to encipher or Netscape complains. - return check_purpose(x, ca, XKU_SSL_SERVER, X509v3_KU_KEY_ENCIPHERMENT); -} - -static int check_purpose_smime_sign(const X509_PURPOSE *xp, const X509 *x, - int ca) { - return check_purpose(x, ca, XKU_SMIME, - X509v3_KU_DIGITAL_SIGNATURE | X509v3_KU_NON_REPUDIATION); -} - -static int check_purpose_smime_encrypt(const X509_PURPOSE *xp, const X509 *x, - int ca) { - return check_purpose(x, ca, XKU_SMIME, X509v3_KU_KEY_ENCIPHERMENT); -} - -static int check_purpose_crl_sign(const X509_PURPOSE *xp, const X509 *x, - int ca) { - return check_purpose(x, ca, /*required_xku=*/0, X509v3_KU_CRL_SIGN); -} - -static int check_purpose_timestamp_sign(const X509_PURPOSE *xp, const X509 *x, - int ca) { - if (ca) { - return 1; - } - - // Check the optional key usage field: - // if Key Usage is present, it must be one of digitalSignature - // and/or nonRepudiation (other values are not consistent and shall - // be rejected). - const auto *impl = FromOpaque(x); - if ((impl->ex_flags & EXFLAG_KUSAGE) && - ((impl->ex_kusage & - ~(X509v3_KU_NON_REPUDIATION | X509v3_KU_DIGITAL_SIGNATURE)) || - !(impl->ex_kusage & - (X509v3_KU_NON_REPUDIATION | X509v3_KU_DIGITAL_SIGNATURE)))) { - return 0; - } - - // Only time stamp key usage is permitted and it's required. - // - // TODO(davidben): Should we check EKUs up the chain like the other cases? - if (!(impl->ex_flags & EXFLAG_XKUSAGE) || impl->ex_xkusage != XKU_TIMESTAMP) { - return 0; - } - - // Extended Key Usage MUST be critical - int i_ext = X509_get_ext_by_NID(x, NID_ext_key_usage, -1); - if (i_ext >= 0) { - const X509_EXTENSION *ext = X509_get_ext(x, i_ext); - if (!X509_EXTENSION_get_critical(ext)) { - return 0; - } - } - - return 1; -} - -static int no_check(const X509_PURPOSE *xp, const X509 *x, int ca) { return 1; } - int X509_check_issued(const X509 *issuer, const X509 *subject) { if (X509_NAME_cmp(X509_get_subject_name(issuer), X509_get_issuer_name(subject))) { return X509_V_ERR_SUBJECT_ISSUER_MISMATCH; } - if (!x509v3_cache_extensions(const_cast(issuer)) || - !x509v3_cache_extensions(const_cast(subject))) { + if (!x509v3_cache_extensions(issuer) || + !x509v3_cache_extensions(subject)) { return X509_V_ERR_UNSPECIFIED; } const auto *subject_impl = FromOpaque(subject); if (subject_impl->akid) { - int ret = X509_check_akid(issuer, subject_impl->akid); + int ret = X509_check_akid(issuer, subject_impl->akid.get()); if (ret != X509_V_OK) { return ret; } @@ -500,7 +458,7 @@ int bssl::X509_check_akid(const X509 *issuer, const AUTHORITY_KEYID *akid) { // Check key ids (if present) auto *issuer_impl = FromOpaque(issuer); if (akid->keyid && issuer_impl->skid && - ASN1_OCTET_STRING_cmp(akid->keyid, issuer_impl->skid)) { + ASN1_OCTET_STRING_cmp(akid->keyid, issuer_impl->skid.get())) { return X509_V_ERR_AKID_SKID_MISMATCH; } // Check serial number @@ -527,15 +485,15 @@ int bssl::X509_check_akid(const X509 *issuer, const AUTHORITY_KEYID *akid) { return X509_V_OK; } -uint32_t X509_get_extension_flags(X509 *x) { - // Ignore the return value. On failure, |impl->ex_flags| will include - // |EXFLAG_INVALID|. +uint32_t X509_get_extension_flags(const X509 *x) { + // Ignore the return value. On failure, `impl->ex_flags` will include + // `EXFLAG_INVALID`. x509v3_cache_extensions(x); const auto *impl = FromOpaque(x); return impl->ex_flags; } -uint32_t X509_get_key_usage(X509 *x) { +uint32_t X509_get_key_usage(const X509 *x) { if (!x509v3_cache_extensions(x)) { return 0; } @@ -544,12 +502,12 @@ uint32_t X509_get_key_usage(X509 *x) { return impl->ex_kusage; } // If there is no extension, key usage is unconstrained, so set all bits to - // one. Note that, although we use |UINT32_MAX|, |ex_kusage| only contains the + // one. Note that, although we use `UINT32_MAX`, `ex_kusage` only contains the // first 16 bits when the extension is present. return UINT32_MAX; } -uint32_t X509_get_extended_key_usage(X509 *x) { +uint32_t X509_get_extended_key_usage(const X509 *x) { if (!x509v3_cache_extensions(x)) { return 0; } @@ -562,15 +520,15 @@ uint32_t X509_get_extended_key_usage(X509 *x) { return UINT32_MAX; } -const ASN1_OCTET_STRING *X509_get0_subject_key_id(X509 *x509) { +const ASN1_OCTET_STRING *X509_get0_subject_key_id(const X509 *x509) { if (!x509v3_cache_extensions(x509)) { return nullptr; } auto *impl = FromOpaque(x509); - return impl->skid; + return impl->skid.get(); } -const ASN1_OCTET_STRING *X509_get0_authority_key_id(X509 *x509) { +const ASN1_OCTET_STRING *X509_get0_authority_key_id(const X509 *x509) { if (!x509v3_cache_extensions(x509)) { return nullptr; } @@ -578,7 +536,7 @@ const ASN1_OCTET_STRING *X509_get0_authority_key_id(X509 *x509) { return impl->akid != nullptr ? impl->akid->keyid : nullptr; } -const GENERAL_NAMES *X509_get0_authority_issuer(X509 *x509) { +const GENERAL_NAMES *X509_get0_authority_issuer(const X509 *x509) { if (!x509v3_cache_extensions(x509)) { return nullptr; } @@ -586,7 +544,7 @@ const GENERAL_NAMES *X509_get0_authority_issuer(X509 *x509) { return impl->akid != nullptr ? impl->akid->issuer : nullptr; } -const ASN1_INTEGER *X509_get0_authority_serial(X509 *x509) { +const ASN1_INTEGER *X509_get0_authority_serial(const X509 *x509) { auto *impl = FromOpaque(x509); if (!x509v3_cache_extensions(impl)) { return nullptr; @@ -594,7 +552,7 @@ const ASN1_INTEGER *X509_get0_authority_serial(X509 *x509) { return impl->akid != nullptr ? impl->akid->serial : nullptr; } -long X509_get_pathlen(X509 *x509) { +long X509_get_pathlen(const X509 *x509) { auto *impl = FromOpaque(x509); if (!x509v3_cache_extensions(x509) || (impl->ex_flags & EXFLAG_BCONS) == 0) { return -1; diff --git a/third_party/boringssl/src/crypto/x509/v3_skey.cc b/third_party/boringssl/src/crypto/x509/v3_skey.cc index e99fdaa63..841f67943 100644 --- a/third_party/boringssl/src/crypto/x509/v3_skey.cc +++ b/third_party/boringssl/src/crypto/x509/v3_skey.cc @@ -27,45 +27,15 @@ using namespace bssl; -char *i2s_ASN1_OCTET_STRING(const X509V3_EXT_METHOD *method, - const ASN1_OCTET_STRING *oct) { - return x509v3_bytes_to_hex(oct->data, oct->length); -} +namespace { -ASN1_OCTET_STRING *s2i_ASN1_OCTET_STRING(const X509V3_EXT_METHOD *method, - const X509V3_CTX *ctx, - const char *str) { - size_t len; - uint8_t *data = x509v3_hex_to_bytes(str, &len); - ASN1_OCTET_STRING *oct; - if (data == nullptr) { - return nullptr; - } - if (len > INT_MAX) { - OPENSSL_PUT_ERROR(X509V3, ERR_R_OVERFLOW); - goto err; - } - - oct = ASN1_OCTET_STRING_new(); - if (oct == nullptr) { - goto err; - } - ASN1_STRING_set0(oct, data, (int)len); - return oct; - -err: - OPENSSL_free(data); - return nullptr; -} - -static char *i2s_ASN1_OCTET_STRING_cb(const X509V3_EXT_METHOD *method, - void *ext) { +char *i2s_ASN1_OCTET_STRING_cb(const X509V3_EXT_METHOD *method, void *ext) { return i2s_ASN1_OCTET_STRING(method, reinterpret_cast(ext)); } -static void *s2i_skey_id(const X509V3_EXT_METHOD *method, const X509V3_CTX *ctx, - const char *str) { +void *s2i_skey_id(const X509V3_EXT_METHOD *method, const X509V3_CTX *ctx, + const char *str) { ASN1_OCTET_STRING *oct; const ASN1_BIT_STRING *pk; unsigned char pkey_dig[EVP_MAX_MD_SIZE]; @@ -89,7 +59,7 @@ static void *s2i_skey_id(const X509V3_EXT_METHOD *method, const X509V3_CTX *ctx, } if (ctx->subject_req) { - pk = &ctx->subject_req->req_info->pubkey->public_key; + pk = FromOpaque(ctx->subject_req->req_info->pubkey)->public_key.get(); } else { pk = X509_get0_pubkey_bitstr(ctx->subject_cert); } @@ -110,6 +80,39 @@ static void *s2i_skey_id(const X509V3_EXT_METHOD *method, const X509V3_CTX *ctx, return nullptr; } +} // namespace + +char *i2s_ASN1_OCTET_STRING(const X509V3_EXT_METHOD *method, + const ASN1_OCTET_STRING *oct) { + return x509v3_bytes_to_hex(oct->data, oct->length); +} + +ASN1_OCTET_STRING *s2i_ASN1_OCTET_STRING(const X509V3_EXT_METHOD *method, + const X509V3_CTX *ctx, + const char *str) { + size_t len; + uint8_t *data = x509v3_hex_to_bytes(str, &len); + ASN1_OCTET_STRING *oct; + if (data == nullptr) { + return nullptr; + } + if (len > INT_MAX) { + OPENSSL_PUT_ERROR(X509V3, ERR_R_OVERFLOW); + goto err; + } + + oct = ASN1_OCTET_STRING_new(); + if (oct == nullptr) { + goto err; + } + ASN1_STRING_set0(oct, data, (int)len); + return oct; + +err: + OPENSSL_free(data); + return nullptr; +} + const X509V3_EXT_METHOD bssl::v3_skey_id = { NID_subject_key_identifier, 0, diff --git a/third_party/boringssl/src/crypto/x509/v3_utl.cc b/third_party/boringssl/src/crypto/x509/v3_utl.cc index 6605c5c0d..caca589d9 100644 --- a/third_party/boringssl/src/crypto/x509/v3_utl.cc +++ b/third_party/boringssl/src/crypto/x509/v3_utl.cc @@ -36,24 +36,11 @@ using namespace bssl; -static char *strip_spaces(char *name); -static int sk_strcmp(const char *const *a, const char *const *b); -static UniquePtr get_email(const X509_NAME *name, - const GENERAL_NAMES *gens); -static void str_free(OPENSSL_STRING str); -static int append_ia5(UniquePtr *sk, - const ASN1_IA5STRING *email); - -static int ipv4_from_asc(uint8_t v4[4], const char *in); -static int ipv6_from_asc(uint8_t v6[16], const char *in); -static int ipv6_cb(const char *elem, size_t len, void *usr); -static int ipv6_hex(uint8_t *out, const char *in, size_t inlen); +namespace { // Add a CONF_VALUE name value pair to stack - -static int x509V3_add_len_value(const char *name, const char *value, - size_t value_len, int omit_value, - STACK_OF(CONF_VALUE) **extlist) { +int x509V3_add_len_value(const char *name, const char *value, size_t value_len, + int omit_value, STACK_OF(CONF_VALUE) **extlist) { CONF_VALUE *vtmp = nullptr; char *tname = nullptr, *tvalue = nullptr; int extlist_was_null = *extlist == nullptr; @@ -61,7 +48,7 @@ static int x509V3_add_len_value(const char *name, const char *value, goto err; } if (!omit_value) { - // |CONF_VALUE| cannot represent strings with NULs. + // `CONF_VALUE` cannot represent strings with NULs. if (OPENSSL_memchr(value, 0, value_len)) { OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_VALUE); goto err; @@ -95,40 +82,7 @@ static int x509V3_add_len_value(const char *name, const char *value, return 0; } -int bssl::X509V3_add_value(const char *name, const char *value, - STACK_OF(CONF_VALUE) **extlist) { - return x509V3_add_len_value(name, value, value != nullptr ? strlen(value) : 0, - /*omit_value=*/value == nullptr, extlist); -} - -int bssl::x509V3_add_value_asn1_string(const char *name, - const ASN1_STRING *value, - STACK_OF(CONF_VALUE) **extlist) { - return x509V3_add_len_value(name, (const char *)value->data, value->length, - /*omit_value=*/0, extlist); -} - -// Free function for STACK_OF(CONF_VALUE) - -void X509V3_conf_free(CONF_VALUE *conf) { - if (!conf) { - return; - } - OPENSSL_free(conf->name); - OPENSSL_free(conf->value); - OPENSSL_free(conf->section); - OPENSSL_free(conf); -} - -int bssl::X509V3_add_value_bool(const char *name, int asn1_bool, - STACK_OF(CONF_VALUE) **extlist) { - if (asn1_bool) { - return X509V3_add_value(name, "TRUE", extlist); - } - return X509V3_add_value(name, "FALSE", extlist); -} - -static char *bignum_to_string(const BIGNUM *bn) { +char *bignum_to_string(const BIGNUM *bn) { char *tmp, *ret; // Display large numbers in hex and small numbers in decimal. Converting to @@ -152,813 +106,1026 @@ static char *bignum_to_string(const BIGNUM *bn) { return ret; } -char *i2s_ASN1_ENUMERATED(const X509V3_EXT_METHOD *method, - const ASN1_ENUMERATED *a) { - BIGNUM *bntmp = nullptr; - char *strtmp = nullptr; - if (!a) { +// Delete leading and trailing spaces from a string +char *strip_spaces(char *name) { + char *p, *q; + // Skip over leading spaces + p = name; + while (*p && OPENSSL_isspace((unsigned char)*p)) { + p++; + } + if (!*p) { return nullptr; } - if (!(bntmp = ASN1_ENUMERATED_to_BN(a, nullptr)) || - !(strtmp = bignum_to_string(bntmp))) { + q = p + strlen(p) - 1; + while ((q != p) && OPENSSL_isspace((unsigned char)*q)) { + q--; } - BN_free(bntmp); - return strtmp; -} - -char *i2s_ASN1_INTEGER(const X509V3_EXT_METHOD *method, const ASN1_INTEGER *a) { - BIGNUM *bntmp = nullptr; - char *strtmp = nullptr; - if (!a) { - return nullptr; + if (p != q) { + q[1] = 0; } - if (!(bntmp = ASN1_INTEGER_to_BN(a, nullptr)) || - !(strtmp = bignum_to_string(bntmp))) { + if (!*p) { + return nullptr; } - BN_free(bntmp); - return strtmp; + return p; } -ASN1_INTEGER *s2i_ASN1_INTEGER(const X509V3_EXT_METHOD *method, - const char *value) { - BIGNUM *bn = nullptr; - ASN1_INTEGER *aint; - int isneg, ishex; - int ret; - if (!value) { - OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_NULL_VALUE); - return nullptr; - } - bn = BN_new(); - if (value[0] == '-') { - value++; - isneg = 1; - } else { - isneg = 0; - } +int sk_strcmp(const char *const *a, const char *const *b) { + return strcmp(*a, *b); +} - if (value[0] == '0' && ((value[1] == 'x') || (value[1] == 'X'))) { - value += 2; - ishex = 1; - } else { - ishex = 0; - } +void str_free(OPENSSL_STRING str) { OPENSSL_free(str); } - if (ishex) { - ret = BN_hex2bn(&bn, value); - } else { - // Decoding from decimal scales quadratically in the input length. Bound the - // largest decimal input we accept in the config parser. 8,192 decimal - // digits allows values up to 27,213 bits. This exceeds the largest RSA, - // DSA, or DH modulus we support, and those are not usefully represented in - // decimal. - if (strlen(value) > 8192) { - BN_free(bn); - OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_NUMBER); - return nullptr; - } - ret = BN_dec2bn(&bn, value); +int append_ia5(UniquePtr *sk, + const ASN1_IA5STRING *email) { + // First some sanity checks + if (email->type != V_ASN1_IA5STRING) { + return 1; } - - if (!ret || value[ret]) { - BN_free(bn); - OPENSSL_PUT_ERROR(X509V3, X509V3_R_BN_DEC2BN_ERROR); - return nullptr; + if (email->data == nullptr || email->length == 0) { + return 1; } - - if (isneg && BN_is_zero(bn)) { - isneg = 0; + // `OPENSSL_STRING` cannot represent strings with embedded NULs. Do not + // report them as outputs. + if (OPENSSL_memchr(email->data, 0, email->length) != nullptr) { + return 1; } - aint = BN_to_ASN1_INTEGER(bn, nullptr); - BN_free(bn); - if (!aint) { - OPENSSL_PUT_ERROR(X509V3, X509V3_R_BN_TO_ASN1_INTEGER_ERROR); - return nullptr; + if (!*sk) { + sk->reset(sk_OPENSSL_STRING_new(sk_strcmp)); } - if (isneg) { - aint->type |= V_ASN1_NEG; + if (!*sk) { + return 0; } - return aint; -} -int bssl::X509V3_add_value_int(const char *name, const ASN1_INTEGER *aint, - STACK_OF(CONF_VALUE) **extlist) { - char *strtmp; - int ret; - if (!aint) { - return 1; - } - if (!(strtmp = i2s_ASN1_INTEGER(nullptr, aint))) { + UniquePtr emtmp( + OPENSSL_strndup((const char *)email->data, email->length)); + if (emtmp == nullptr || !PushToStack(sk->get(), std::move(emtmp))) { return 0; } - ret = X509V3_add_value(name, strtmp, extlist); - OPENSSL_free(strtmp); - return ret; + return 1; } -int bssl::X509V3_bool_from_string(const char *str, ASN1_BOOLEAN *out_bool) { - if (!strcmp(str, "TRUE") || !strcmp(str, "true") || !strcmp(str, "Y") || - !strcmp(str, "y") || !strcmp(str, "YES") || !strcmp(str, "yes")) { - *out_bool = ASN1_BOOLEAN_TRUE; - return 1; +UniquePtr get_email(const X509_NAME *name, + const GENERAL_NAMES *gens) { + UniquePtr ret; + int i = -1; + while ((i = X509_NAME_get_index_by_NID(name, NID_pkcs9_emailAddress, i)) >= + 0) { + const X509_NAME_ENTRY *ne = X509_NAME_get_entry(name, i); + const ASN1_IA5STRING *email = X509_NAME_ENTRY_get_data(ne); + if (!append_ia5(&ret, email)) { + return nullptr; + } } - if (!strcmp(str, "FALSE") || !strcmp(str, "false") || !strcmp(str, "N") || - !strcmp(str, "n") || !strcmp(str, "NO") || !strcmp(str, "no")) { - *out_bool = ASN1_BOOLEAN_FALSE; - return 1; + for (const GENERAL_NAME *gen : gens) { + if (gen->type == GEN_EMAIL && !append_ia5(&ret, gen->d.ia5)) { + return nullptr; + } } - OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_BOOLEAN_STRING); - return 0; + sk_OPENSSL_STRING_sort_and_dedup(ret.get(), str_free); + return ret; } -int bssl::X509V3_get_value_bool(const CONF_VALUE *value, - ASN1_BOOLEAN *out_bool) { - const char *btmp = value->value; - if (btmp == nullptr) { - OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_BOOLEAN_STRING); - goto err; +typedef int (*equal_fn)(const unsigned char *pattern, size_t pattern_len, + const unsigned char *subject, size_t subject_len, + unsigned int flags); + +// Compare while ASCII ignoring case. +int equal_nocase(const unsigned char *pattern, size_t pattern_len, + const unsigned char *subject, size_t subject_len, + unsigned int flags) { + if (pattern_len != subject_len) { + return 0; } - if (!X509V3_bool_from_string(btmp, out_bool)) { - goto err; + while (pattern_len) { + unsigned char l = *pattern; + unsigned char r = *subject; + // The pattern must not contain NUL characters. + if (l == 0) { + return 0; + } + if (l != r) { + if (OPENSSL_tolower(l) != OPENSSL_tolower(r)) { + return 0; + } + } + ++pattern; + ++subject; + --pattern_len; } return 1; - -err: - X509V3_conf_err(value); - return 0; } -int bssl::X509V3_get_value_int(const CONF_VALUE *value, ASN1_INTEGER **aint) { - ASN1_INTEGER *itmp; - if (!(itmp = s2i_ASN1_INTEGER(nullptr, value->value))) { - X509V3_conf_err(value); +// Compare using OPENSSL_memcmp. +int equal_case(const unsigned char *pattern, size_t pattern_len, + const unsigned char *subject, size_t subject_len, + unsigned int flags) { + if (pattern_len != subject_len) { return 0; } - ASN1_INTEGER_free(*aint); - *aint = itmp; - return 1; + return !OPENSSL_memcmp(pattern, subject, pattern_len); } -#define HDR_NAME 1 -#define HDR_VALUE 2 +// RFC 5280, section 7.5, requires that only the domain is compared in a +// case-insensitive manner. +int equal_email(const unsigned char *a, size_t a_len, const unsigned char *b, + size_t b_len, unsigned int unused_flags) { + size_t i = a_len; + if (a_len != b_len) { + return 0; + } + // We search backwards for the '@' character, so that we do not have to + // deal with quoted local-parts. The domain part is compared in a + // case-insensitive manner. + while (i > 0) { + --i; + if (a[i] == '@' || b[i] == '@') { + if (!equal_nocase(a + i, a_len - i, b + i, a_len - i, 0)) { + return 0; + } + break; + } + } + if (i == 0) { + i = a_len; + } + return equal_case(a, i, b, i, 0); +} -// #define DEBUG +// Compare the prefix and suffix with the subject, and check that the +// characters in-between are valid. +int wildcard_match(const unsigned char *prefix, size_t prefix_len, + const unsigned char *suffix, size_t suffix_len, + const unsigned char *subject, size_t subject_len, + unsigned int flags) { + const unsigned char *wildcard_start; + const unsigned char *wildcard_end; + const unsigned char *p; + int allow_idna = 0; -STACK_OF(CONF_VALUE) *bssl::X509V3_parse_list(const char *line) { - char *p, *q, c; - char *ntmp, *vtmp; - STACK_OF(CONF_VALUE) *values = nullptr; - char *linebuf; - int state; - // We are going to modify the line so copy it first - linebuf = OPENSSL_strdup(line); - if (linebuf == nullptr) { - goto err; + if (subject_len < prefix_len + suffix_len) { + return 0; } - state = HDR_NAME; - ntmp = nullptr; - // Go through all characters - for (p = linebuf, q = linebuf; (c = *p) && (c != '\r') && (c != '\n'); p++) { - switch (state) { - case HDR_NAME: - if (c == ':') { - state = HDR_VALUE; - *p = 0; - ntmp = strip_spaces(q); - if (!ntmp) { - OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_NULL_NAME); - goto err; - } - q = p + 1; - } else if (c == ',') { - *p = 0; - ntmp = strip_spaces(q); - q = p + 1; -#if 0 - printf("%s\n", ntmp); -#endif - if (!ntmp) { - OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_NULL_NAME); - goto err; - } - X509V3_add_value(ntmp, nullptr, &values); - } - break; + if (!equal_nocase(prefix, prefix_len, subject, prefix_len, flags)) { + return 0; + } + wildcard_start = subject + prefix_len; + wildcard_end = subject + (subject_len - suffix_len); + if (!equal_nocase(wildcard_end, suffix_len, suffix, suffix_len, flags)) { + return 0; + } + // If the wildcard makes up the entire first label, it must match at + // least one character. + if (prefix_len == 0 && *suffix == '.') { + if (wildcard_start == wildcard_end) { + return 0; + } + allow_idna = 1; + } + // IDNA labels cannot match partial wildcards + if (!allow_idna && subject_len >= 4 && + OPENSSL_strncasecmp((char *)subject, "xn--", 4) == 0) { + return 0; + } + // The wildcard may match a literal '*' + if (wildcard_end == wildcard_start + 1 && *wildcard_start == '*') { + return 1; + } + // Check that the part matched by the wildcard contains only + // permitted characters and only matches a single label. + for (p = wildcard_start; p != wildcard_end; ++p) { + if (!OPENSSL_isalnum(*p) && *p != '-') { + return 0; + } + } + return 1; +} - case HDR_VALUE: - if (c == ',') { - state = HDR_NAME; - *p = 0; - vtmp = strip_spaces(q); -#if 0 - printf("%s\n", ntmp); -#endif - if (!vtmp) { - OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_NULL_VALUE); - goto err; - } - X509V3_add_value(ntmp, vtmp, &values); - ntmp = nullptr; - q = p + 1; - } +#define LABEL_START (1 << 0) +#define LABEL_END (1 << 1) +#define LABEL_HYPHEN (1 << 2) +#define LABEL_IDNA (1 << 3) + +const unsigned char *valid_star(const unsigned char *p, size_t len, + unsigned int flags) { + const unsigned char *star = nullptr; + size_t i; + int state = LABEL_START; + int dots = 0; + for (i = 0; i < len; ++i) { + // Locate first and only legal wildcard, either at the start + // or end of a non-IDNA first and not final label. + if (p[i] == '*') { + int atstart = (state & LABEL_START); + int atend = (i == len - 1 || p[i + 1] == '.'); + // At most one wildcard per pattern. + // No wildcards in IDNA labels. + // No wildcards after the first label. + if (star != nullptr || (state & LABEL_IDNA) != 0 || dots) { + return nullptr; + } + // Only full-label '*.example.com' wildcards. + if (!atstart || !atend) { + return nullptr; + } + star = &p[i]; + state &= ~LABEL_START; + } else if (OPENSSL_isalnum(p[i])) { + if ((state & LABEL_START) != 0 && len - i >= 4 && + OPENSSL_strncasecmp((char *)&p[i], "xn--", 4) == 0) { + state |= LABEL_IDNA; + } + state &= ~(LABEL_HYPHEN | LABEL_START); + } else if (p[i] == '.') { + if ((state & (LABEL_HYPHEN | LABEL_START)) != 0) { + return nullptr; + } + state = LABEL_START; + ++dots; + } else if (p[i] == '-') { + // no domain/subdomain starts with '-' + if ((state & LABEL_START) != 0) { + return nullptr; + } + state |= LABEL_HYPHEN; + } else { + return nullptr; } } - if (state == HDR_VALUE) { - vtmp = strip_spaces(q); -#if 0 - printf("%s=%s\n", ntmp, vtmp); -#endif - if (!vtmp) { - OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_NULL_VALUE); - goto err; + // The final label must not end in a hyphen or ".", and + // there must be at least two dots after the star. + if ((state & (LABEL_START | LABEL_HYPHEN)) != 0 || dots < 2) { + return nullptr; + } + return star; +} + +// Compare using wildcards. +int equal_wildcard(const unsigned char *pattern, size_t pattern_len, + const unsigned char *subject, size_t subject_len, + unsigned int flags) { + const unsigned char *star = nullptr; + + // Subject names starting with '.' can only match a wildcard pattern + // via a subject sub-domain pattern suffix match. + if (!(subject_len > 1 && subject[0] == '.')) { + star = valid_star(pattern, pattern_len, flags); + } + if (star == nullptr) { + return equal_nocase(pattern, pattern_len, subject, subject_len, flags); + } + return wildcard_match(pattern, star - pattern, star + 1, + (pattern + pattern_len) - star - 1, subject, + subject_len, flags); +} + +// Compare an ASN1_STRING to a supplied string. If they match return 1. If +// cmp_type > 0 only compare if string matches the type, otherwise convert it +// to UTF8. +int do_check_string(const ASN1_STRING *a, int cmp_type, equal_fn equal, + unsigned int flags, int check_type, const char *b, + size_t blen, char **peername) { + int rv = 0; + + if (!a->data || !a->length) { + return 0; + } + if (cmp_type > 0) { + if (cmp_type != a->type) { + return 0; + } + if (cmp_type == V_ASN1_IA5STRING) { + rv = equal(a->data, a->length, (unsigned char *)b, blen, flags); + } else if (a->length == (int)blen && !OPENSSL_memcmp(a->data, b, blen)) { + rv = 1; + } + if (rv > 0 && peername) { + *peername = OPENSSL_strndup((char *)a->data, a->length); + if (*peername == nullptr) { + return -1; + } } - X509V3_add_value(ntmp, vtmp, &values); } else { - ntmp = strip_spaces(q); -#if 0 - printf("%s\n", ntmp); -#endif - if (!ntmp) { - OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_NULL_NAME); - goto err; + int astrlen; + unsigned char *astr; + astrlen = ASN1_STRING_to_UTF8(&astr, a); + if (astrlen < 0) { + return -1; } - X509V3_add_value(ntmp, nullptr, &values); + // We check the common name against DNS name constraints if it passes + // `x509v3_looks_like_dns_name`. Thus we must not consider common names + // for DNS fallbacks if they fail this check. + if (check_type == GEN_DNS && !x509v3_looks_like_dns_name(astr, astrlen)) { + rv = 0; + } else { + rv = equal(astr, astrlen, (unsigned char *)b, blen, flags); + } + if (rv > 0 && peername) { + *peername = OPENSSL_strndup((char *)astr, astrlen); + if (*peername == nullptr) { + return -1; + } + } + OPENSSL_free(astr); } - OPENSSL_free(linebuf); - return values; + return rv; +} -err: - OPENSSL_free(linebuf); - sk_CONF_VALUE_pop_free(values, X509V3_conf_free); - return nullptr; +int do_x509_check(const X509 *x, const char *chk, size_t chklen, + unsigned int flags, int check_type, char **peername) { + int cnid = NID_undef; + int alt_type; + int rv = 0; + equal_fn equal; + if (check_type == GEN_EMAIL) { + cnid = NID_pkcs9_emailAddress; + alt_type = V_ASN1_IA5STRING; + equal = equal_email; + } else if (check_type == GEN_DNS) { + cnid = NID_commonName; + alt_type = V_ASN1_IA5STRING; + if (flags & X509_CHECK_FLAG_NO_WILDCARDS) { + equal = equal_nocase; + } else { + equal = equal_wildcard; + } + } else { + alt_type = V_ASN1_OCTET_STRING; + equal = equal_case; + } + + int critical; + UniquePtr gens(reinterpret_cast( + X509_get_ext_d2i(x, NID_subject_alt_name, &critical, nullptr))); + if (gens) { + for (const GENERAL_NAME *gen : gens.get()) { + if (gen->type != check_type) { + continue; + } + const ASN1_STRING *cstr; + if (check_type == GEN_EMAIL) { + cstr = gen->d.rfc822Name; + } else if (check_type == GEN_DNS) { + cstr = gen->d.dNSName; + } else { + cstr = gen->d.iPAddress; + } + // Positive on success, negative on error! + if ((rv = do_check_string(cstr, alt_type, equal, flags, check_type, chk, + chklen, peername)) != 0) { + break; + } + } + return rv; + } else if (critical != -1) { + // Syntax error in the subjectAltName extension. + return 0; + } + + // We're done if CN-ID is not pertinent + if (cnid == NID_undef || (flags & X509_CHECK_FLAG_NEVER_CHECK_SUBJECT)) { + return 0; + } + + int j = -1; + const X509_NAME *name = X509_get_subject_name(x); + while ((j = X509_NAME_get_index_by_NID(name, cnid, j)) >= 0) { + const X509_NAME_ENTRY *ne = X509_NAME_get_entry(name, j); + const ASN1_STRING *str = X509_NAME_ENTRY_get_data(ne); + // Positive on success, negative on error! + if ((rv = do_check_string(str, -1, equal, flags, check_type, chk, chklen, + peername)) != 0) { + return rv; + } + } + return 0; } -// Delete leading and trailing spaces from a string -static char *strip_spaces(char *name) { - char *p, *q; - // Skip over leading spaces - p = name; - while (*p && OPENSSL_isspace((unsigned char)*p)) { - p++; +// get_ipv4_component consumes one IPv4 component, terminated by either '.' or +// the end of the string, from `*str`. On success, it returns one, sets `*out` +// to the component, and advances `*str` to the first unconsumed character. On +// invalid input, it returns zero. +int get_ipv4_component(uint8_t *out_byte, const char **str) { + // Store a slightly larger intermediary so the overflow check is easier. + uint32_t out = 0; + for (;;) { + if (!OPENSSL_isdigit(**str)) { + return 0; + } + out = (out * 10) + (**str - '0'); + if (out > 255) { + // Components must be 8-bit. + return 0; + } + (*str)++; + if ((**str) == '.' || (**str) == '\0') { + *out_byte = (uint8_t)out; + return 1; + } + if (out == 0) { + // Reject extra leading zeros. Parsers sometimes treat them as octal, so + // accepting them would misinterpret input. + return 0; + } } - if (!*p) { - return nullptr; +} + +// get_ipv4_dot consumes a '.' from `*str` and advances it. It returns one on +// success and zero if `*str` does not point to a '.'. +int get_ipv4_dot(const char **str) { + if (**str != '.') { + return 0; } - q = p + strlen(p) - 1; - while ((q != p) && OPENSSL_isspace((unsigned char)*q)) { - q--; + (*str)++; + return 1; +} + +int ipv4_from_asc(uint8_t v4[4], const char *in) { + if (!get_ipv4_component(&v4[0], &in) || !get_ipv4_dot(&in) || + !get_ipv4_component(&v4[1], &in) || !get_ipv4_dot(&in) || + !get_ipv4_component(&v4[2], &in) || !get_ipv4_dot(&in) || + !get_ipv4_component(&v4[3], &in) || *in != '\0') { + return 0; } - if (p != q) { - q[1] = 0; + return 1; +} + +typedef struct { + // Temporary store for IPV6 output + uint8_t tmp[16]; + // Total number of bytes in tmp + int total; + // The position of a zero (corresponding to '::') + int zero_pos; + // Number of zeroes + int zero_cnt; +} IPV6_STAT; + +// Convert a string of up to 4 hex digits into the corresponding IPv6 form. +int ipv6_hex(uint8_t *out, const char *in, size_t inlen) { + if (inlen > 4) { + return 0; } - if (!*p) { - return nullptr; + uint16_t num = 0; + while (inlen--) { + uint8_t val; + if (!OPENSSL_fromxdigit(&val, *in++)) { + return 0; + } + num = (num << 4) | val; } - return p; + out[0] = num >> 8; + out[1] = num & 0xff; + return 1; } -// hex string utilities - -char *bssl::x509v3_bytes_to_hex(const uint8_t *in, size_t len) { - CBB cbb; - if (!CBB_init(&cbb, len * 3 + 1)) { - goto err; +int ipv6_cb(const char *elem, size_t len, void *usr) { + IPV6_STAT *s = reinterpret_cast(usr); + // Error if 16 bytes written + if (s->total == 16) { + return 0; } - for (size_t i = 0; i < len; i++) { - static const char hex[] = "0123456789ABCDEF"; - if ((i > 0 && !CBB_add_u8(&cbb, ':')) || - !CBB_add_u8(&cbb, hex[in[i] >> 4]) || - !CBB_add_u8(&cbb, hex[in[i] & 0xf])) { - goto err; + if (len == 0) { + // Zero length element, corresponds to '::' + if (s->zero_pos == -1) { + s->zero_pos = s->total; + } else if (s->zero_pos != s->total) { + // If we've already got a :: its an error + return 0; + } + if (s->zero_cnt >= 3) { + // More than three zeros is an error. + return 0; + } + s->zero_cnt++; + } else { + // If more than 4 characters could be final a.b.c.d form + if (len > 4) { + // Need at least 4 bytes left + if (s->total > 12) { + return 0; + } + // Must be end of string + if (elem[len]) { + return 0; + } + if (!ipv4_from_asc(s->tmp + s->total, elem)) { + return 0; + } + s->total += 4; + } else { + if (!ipv6_hex(s->tmp + s->total, elem, len)) { + return 0; + } + s->total += 2; } } - uint8_t *ret; - size_t unused_len; - if (!CBB_add_u8(&cbb, 0) || !CBB_finish(&cbb, &ret, &unused_len)) { - goto err; - } - - return (char *)ret; - -err: - CBB_cleanup(&cbb); - return nullptr; + return 1; } -unsigned char *bssl::x509v3_hex_to_bytes(const char *str, size_t *len) { - unsigned char *hexbuf, *q; - unsigned char ch, cl, *p; - uint8_t high, low; - if (!str) { - OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_NULL_ARGUMENT); - return nullptr; - } - if (!(hexbuf = - reinterpret_cast(OPENSSL_malloc(strlen(str) >> 1)))) { - goto err; +int ipv6_from_asc(uint8_t v6[16], const char *in) { + IPV6_STAT v6stat; + v6stat.total = 0; + v6stat.zero_pos = -1; + v6stat.zero_cnt = 0; + // Treat the IPv6 representation as a list of values separated by ':'. + // The presence of a '::' will parse as one, two or three zero length + // elements. + if (!CONF_parse_list(in, ':', 0, ipv6_cb, &v6stat)) { + return 0; } - for (p = (unsigned char *)str, q = hexbuf; *p;) { - ch = *p++; - if (ch == ':') { - continue; - } - cl = *p++; - if (!cl) { - OPENSSL_PUT_ERROR(X509V3, X509V3_R_ODD_NUMBER_OF_DIGITS); - OPENSSL_free(hexbuf); - return nullptr; + + if (v6stat.zero_pos == -1) { + // If no '::' must have exactly 16 bytes + if (v6stat.total != 16) { + return 0; } - if (!OPENSSL_fromxdigit(&high, ch)) { - goto badhex; + } else { + // If '::' must have less than 16 bytes + if (v6stat.total >= 16) { + return 0; } - if (!OPENSSL_fromxdigit(&low, cl)) { - goto badhex; + if (v6stat.zero_cnt > 3) { + // More than three zeroes is an error + return 0; + } else if (v6stat.zero_cnt == 3) { + // Can only have three zeroes if nothing else present + if (v6stat.total > 0) { + return 0; + } + } else if (v6stat.zero_cnt == 2) { + // Can only have two zeroes if at start or end + if (v6stat.zero_pos != 0 && v6stat.zero_pos != v6stat.total) { + return 0; + } + } else { + // Can only have one zero if *not* start or end + if (v6stat.zero_pos == 0 || v6stat.zero_pos == v6stat.total) { + return 0; + } } - *q++ = (high << 4) | low; } - if (len) { - *len = q - hexbuf; + // Format the result. + if (v6stat.zero_pos >= 0) { + // Copy initial part + OPENSSL_memcpy(v6, v6stat.tmp, v6stat.zero_pos); + // Zero middle + OPENSSL_memset(v6 + v6stat.zero_pos, 0, 16 - v6stat.total); + // Copy final part + if (v6stat.total != v6stat.zero_pos) { + OPENSSL_memcpy(v6 + v6stat.zero_pos + 16 - v6stat.total, + v6stat.tmp + v6stat.zero_pos, + v6stat.total - v6stat.zero_pos); + } + } else { + OPENSSL_memcpy(v6, v6stat.tmp, 16); } - return hexbuf; + return 1; +} -err: - OPENSSL_free(hexbuf); - return nullptr; +} // namespace -badhex: - OPENSSL_free(hexbuf); - OPENSSL_PUT_ERROR(X509V3, X509V3_R_ILLEGAL_HEX_DIGIT); - return nullptr; +int bssl::X509V3_add_value(const char *name, const char *value, + STACK_OF(CONF_VALUE) **extlist) { + return x509V3_add_len_value(name, value, value != nullptr ? strlen(value) : 0, + /*omit_value=*/value == nullptr, extlist); } -int bssl::x509v3_conf_name_matches(const char *name, const char *cmp) { - // |name| must begin with |cmp|. - size_t len = strlen(cmp); - if (strncmp(name, cmp, len) != 0) { - return 0; - } - // |name| must either be equal to |cmp| or begin with |cmp|, followed by '.'. - return name[len] == '\0' || name[len] == '.'; +int bssl::x509V3_add_value_asn1_string(const char *name, + const ASN1_STRING *value, + STACK_OF(CONF_VALUE) **extlist) { + return x509V3_add_len_value(name, (const char *)value->data, value->length, + /*omit_value=*/0, extlist); } -static int sk_strcmp(const char *const *a, const char *const *b) { - return strcmp(*a, *b); +// Free function for STACK_OF(CONF_VALUE) + +void X509V3_conf_free(CONF_VALUE *conf) { + if (!conf) { + return; + } + OPENSSL_free(conf->name); + OPENSSL_free(conf->value); + OPENSSL_free(conf->section); + OPENSSL_free(conf); } -STACK_OF(OPENSSL_STRING) *X509_get1_email(const X509 *x) { - UniquePtr gens(reinterpret_cast( - X509_get_ext_d2i(x, NID_subject_alt_name, nullptr, nullptr))); - return get_email(X509_get_subject_name(x), gens.get()).release(); +int bssl::X509V3_add_value_bool(const char *name, int asn1_bool, + STACK_OF(CONF_VALUE) **extlist) { + if (asn1_bool) { + return X509V3_add_value(name, "TRUE", extlist); + } + return X509V3_add_value(name, "FALSE", extlist); } -STACK_OF(OPENSSL_STRING) *X509_get1_ocsp(const X509 *x) { - UniquePtr info( - reinterpret_cast( - X509_get_ext_d2i(x, NID_info_access, nullptr, nullptr))); - if (!info) { +char *i2s_ASN1_ENUMERATED(const X509V3_EXT_METHOD *method, + const ASN1_ENUMERATED *a) { + BIGNUM *bntmp = nullptr; + char *strtmp = nullptr; + if (!a) { return nullptr; } - UniquePtr ret; - for (const ACCESS_DESCRIPTION *ad : info.get()) { - if (OBJ_obj2nid(ad->method) == NID_ad_OCSP) { - if (ad->location->type == GEN_URI) { - if (!append_ia5(&ret, ad->location->d.uniformResourceIdentifier)) { - return nullptr; - } - } - } + if (!(bntmp = ASN1_ENUMERATED_to_BN(a, nullptr)) || + !(strtmp = bignum_to_string(bntmp))) { } - sk_OPENSSL_STRING_sort_and_dedup(ret.get(), str_free); - return ret.release(); -} - -STACK_OF(OPENSSL_STRING) *X509_REQ_get1_email(const X509_REQ *x) { - UniquePtr exts(X509_REQ_get_extensions(x)); - UniquePtr gens(reinterpret_cast( - X509V3_get_d2i(exts.get(), NID_subject_alt_name, nullptr, nullptr))); - return get_email(X509_REQ_get_subject_name(x), gens.get()).release(); + BN_free(bntmp); + return strtmp; } -static UniquePtr get_email( - const X509_NAME *name, const GENERAL_NAMES *gens) { - UniquePtr ret; - int i = -1; - while ((i = X509_NAME_get_index_by_NID(name, NID_pkcs9_emailAddress, i)) >= - 0) { - const X509_NAME_ENTRY *ne = X509_NAME_get_entry(name, i); - const ASN1_IA5STRING *email = X509_NAME_ENTRY_get_data(ne); - if (!append_ia5(&ret, email)) { - return nullptr; - } +char *i2s_ASN1_INTEGER(const X509V3_EXT_METHOD *method, const ASN1_INTEGER *a) { + BIGNUM *bntmp = nullptr; + char *strtmp = nullptr; + if (!a) { + return nullptr; } - for (const GENERAL_NAME *gen : gens) { - if (gen->type == GEN_EMAIL && !append_ia5(&ret, gen->d.ia5)) { - return nullptr; - } + if (!(bntmp = ASN1_INTEGER_to_BN(a, nullptr)) || + !(strtmp = bignum_to_string(bntmp))) { } - sk_OPENSSL_STRING_sort_and_dedup(ret.get(), str_free); - return ret; + BN_free(bntmp); + return strtmp; } -static void str_free(OPENSSL_STRING str) { OPENSSL_free(str); } - -static int append_ia5(UniquePtr *sk, - const ASN1_IA5STRING *email) { - // First some sanity checks - if (email->type != V_ASN1_IA5STRING) { - return 1; - } - if (email->data == nullptr || email->length == 0) { - return 1; +ASN1_INTEGER *s2i_ASN1_INTEGER(const X509V3_EXT_METHOD *method, + const char *value) { + BIGNUM *bn = nullptr; + ASN1_INTEGER *aint; + int isneg, ishex; + int ret; + if (!value) { + OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_NULL_VALUE); + return nullptr; } - // |OPENSSL_STRING| cannot represent strings with embedded NULs. Do not - // report them as outputs. - if (OPENSSL_memchr(email->data, 0, email->length) != nullptr) { - return 1; + bn = BN_new(); + if (value[0] == '-') { + value++; + isneg = 1; + } else { + isneg = 0; } - if (!*sk) { - sk->reset(sk_OPENSSL_STRING_new(sk_strcmp)); - } - if (!*sk) { - return 0; + if (value[0] == '0' && ((value[1] == 'x') || (value[1] == 'X'))) { + value += 2; + ishex = 1; + } else { + ishex = 0; } - UniquePtr emtmp( - OPENSSL_strndup((const char *)email->data, email->length)); - if (emtmp == nullptr || !PushToStack(sk->get(), std::move(emtmp))) { - return 0; + if (ishex) { + ret = BN_hex2bn(&bn, value); + } else { + // Decoding from decimal scales quadratically in the input length. Bound the + // largest decimal input we accept in the config parser. 8,192 decimal + // digits allows values up to 27,213 bits. This exceeds the largest RSA, + // DSA, or DH modulus we support, and those are not usefully represented in + // decimal. + if (strlen(value) > 8192) { + BN_free(bn); + OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_NUMBER); + return nullptr; + } + ret = BN_dec2bn(&bn, value); } - return 1; -} -void X509_email_free(STACK_OF(OPENSSL_STRING) *sk) { - sk_OPENSSL_STRING_pop_free(sk, str_free); -} + if (!ret || value[ret]) { + BN_free(bn); + OPENSSL_PUT_ERROR(X509V3, X509V3_R_BN_DEC2BN_ERROR); + return nullptr; + } -typedef int (*equal_fn)(const unsigned char *pattern, size_t pattern_len, - const unsigned char *subject, size_t subject_len, - unsigned int flags); + if (isneg && BN_is_zero(bn)) { + isneg = 0; + } -// Compare while ASCII ignoring case. -static int equal_nocase(const unsigned char *pattern, size_t pattern_len, - const unsigned char *subject, size_t subject_len, - unsigned int flags) { - if (pattern_len != subject_len) { - return 0; + aint = BN_to_ASN1_INTEGER(bn, nullptr); + BN_free(bn); + if (!aint) { + OPENSSL_PUT_ERROR(X509V3, X509V3_R_BN_TO_ASN1_INTEGER_ERROR); + return nullptr; } - while (pattern_len) { - unsigned char l = *pattern; - unsigned char r = *subject; - // The pattern must not contain NUL characters. - if (l == 0) { - return 0; - } - if (l != r) { - if (OPENSSL_tolower(l) != OPENSSL_tolower(r)) { - return 0; - } - } - ++pattern; - ++subject; - --pattern_len; + if (isneg) { + aint->type |= V_ASN1_NEG; } - return 1; + return aint; } -// Compare using OPENSSL_memcmp. -static int equal_case(const unsigned char *pattern, size_t pattern_len, - const unsigned char *subject, size_t subject_len, - unsigned int flags) { - if (pattern_len != subject_len) { +int bssl::X509V3_add_value_int(const char *name, const ASN1_INTEGER *aint, + STACK_OF(CONF_VALUE) **extlist) { + char *strtmp; + int ret; + if (!aint) { + return 1; + } + if (!(strtmp = i2s_ASN1_INTEGER(nullptr, aint))) { return 0; } - return !OPENSSL_memcmp(pattern, subject, pattern_len); + ret = X509V3_add_value(name, strtmp, extlist); + OPENSSL_free(strtmp); + return ret; } -// RFC 5280, section 7.5, requires that only the domain is compared in a -// case-insensitive manner. -static int equal_email(const unsigned char *a, size_t a_len, - const unsigned char *b, size_t b_len, - unsigned int unused_flags) { - size_t i = a_len; - if (a_len != b_len) { - return 0; - } - // We search backwards for the '@' character, so that we do not have to - // deal with quoted local-parts. The domain part is compared in a - // case-insensitive manner. - while (i > 0) { - --i; - if (a[i] == '@' || b[i] == '@') { - if (!equal_nocase(a + i, a_len - i, b + i, a_len - i, 0)) { - return 0; - } - break; - } +int bssl::X509V3_bool_from_string(const char *str, ASN1_BOOLEAN *out_bool) { + if (!strcmp(str, "TRUE") || !strcmp(str, "true") || !strcmp(str, "Y") || + !strcmp(str, "y") || !strcmp(str, "YES") || !strcmp(str, "yes")) { + *out_bool = ASN1_BOOLEAN_TRUE; + return 1; } - if (i == 0) { - i = a_len; + if (!strcmp(str, "FALSE") || !strcmp(str, "false") || !strcmp(str, "N") || + !strcmp(str, "n") || !strcmp(str, "NO") || !strcmp(str, "no")) { + *out_bool = ASN1_BOOLEAN_FALSE; + return 1; } - return equal_case(a, i, b, i, 0); + OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_BOOLEAN_STRING); + return 0; } -// Compare the prefix and suffix with the subject, and check that the -// characters in-between are valid. -static int wildcard_match(const unsigned char *prefix, size_t prefix_len, - const unsigned char *suffix, size_t suffix_len, - const unsigned char *subject, size_t subject_len, - unsigned int flags) { - const unsigned char *wildcard_start; - const unsigned char *wildcard_end; - const unsigned char *p; - int allow_idna = 0; - - if (subject_len < prefix_len + suffix_len) { - return 0; - } - if (!equal_nocase(prefix, prefix_len, subject, prefix_len, flags)) { - return 0; - } - wildcard_start = subject + prefix_len; - wildcard_end = subject + (subject_len - suffix_len); - if (!equal_nocase(wildcard_end, suffix_len, suffix, suffix_len, flags)) { - return 0; +int bssl::X509V3_get_value_bool(const CONF_VALUE *value, + ASN1_BOOLEAN *out_bool) { + const char *btmp = value->value; + if (btmp == nullptr) { + OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_BOOLEAN_STRING); + goto err; } - // If the wildcard makes up the entire first label, it must match at - // least one character. - if (prefix_len == 0 && *suffix == '.') { - if (wildcard_start == wildcard_end) { - return 0; - } - allow_idna = 1; + if (!X509V3_bool_from_string(btmp, out_bool)) { + goto err; } - // IDNA labels cannot match partial wildcards - if (!allow_idna && subject_len >= 4 && - OPENSSL_strncasecmp((char *)subject, "xn--", 4) == 0) { + return 1; + +err: + X509V3_conf_err(value); + return 0; +} + +int bssl::X509V3_get_value_int(const CONF_VALUE *value, ASN1_INTEGER **aint) { + ASN1_INTEGER *itmp; + if (!(itmp = s2i_ASN1_INTEGER(nullptr, value->value))) { + X509V3_conf_err(value); return 0; } - // The wildcard may match a literal '*' - if (wildcard_end == wildcard_start + 1 && *wildcard_start == '*') { - return 1; - } - // Check that the part matched by the wildcard contains only - // permitted characters and only matches a single label. - for (p = wildcard_start; p != wildcard_end; ++p) { - if (!OPENSSL_isalnum(*p) && *p != '-') { - return 0; - } - } + ASN1_INTEGER_free(*aint); + *aint = itmp; return 1; } -#define LABEL_START (1 << 0) -#define LABEL_END (1 << 1) -#define LABEL_HYPHEN (1 << 2) -#define LABEL_IDNA (1 << 3) +#define HDR_NAME 1 +#define HDR_VALUE 2 -static const unsigned char *valid_star(const unsigned char *p, size_t len, - unsigned int flags) { - const unsigned char *star = nullptr; - size_t i; - int state = LABEL_START; - int dots = 0; - for (i = 0; i < len; ++i) { - // Locate first and only legal wildcard, either at the start - // or end of a non-IDNA first and not final label. - if (p[i] == '*') { - int atstart = (state & LABEL_START); - int atend = (i == len - 1 || p[i + 1] == '.'); - // At most one wildcard per pattern. - // No wildcards in IDNA labels. - // No wildcards after the first label. - if (star != nullptr || (state & LABEL_IDNA) != 0 || dots) { - return nullptr; - } - // Only full-label '*.example.com' wildcards. - if (!atstart || !atend) { - return nullptr; - } - star = &p[i]; - state &= ~LABEL_START; - } else if (OPENSSL_isalnum(p[i])) { - if ((state & LABEL_START) != 0 && len - i >= 4 && - OPENSSL_strncasecmp((char *)&p[i], "xn--", 4) == 0) { - state |= LABEL_IDNA; - } - state &= ~(LABEL_HYPHEN | LABEL_START); - } else if (p[i] == '.') { - if ((state & (LABEL_HYPHEN | LABEL_START)) != 0) { - return nullptr; - } - state = LABEL_START; - ++dots; - } else if (p[i] == '-') { - // no domain/subdomain starts with '-' - if ((state & LABEL_START) != 0) { - return nullptr; - } - state |= LABEL_HYPHEN; - } else { - return nullptr; +STACK_OF(CONF_VALUE) *bssl::X509V3_parse_list(const char *line) { + char *p, *q, c; + char *ntmp, *vtmp; + STACK_OF(CONF_VALUE) *values = nullptr; + char *linebuf; + int state; + // We are going to modify the line so copy it first + // TODO(davidben): Rewrite this parser with `std::string_view`. It doesn't + // need to copy the string. + linebuf = OPENSSL_strdup(line); + if (linebuf == nullptr) { + goto err; + } + state = HDR_NAME; + ntmp = nullptr; + // Go through all characters + for (p = linebuf, q = linebuf; (c = *p) && (c != '\r') && (c != '\n'); p++) { + switch (state) { + case HDR_NAME: + if (c == ':') { + state = HDR_VALUE; + *p = 0; + ntmp = strip_spaces(q); + if (!ntmp) { + OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_NULL_NAME); + goto err; + } + q = p + 1; + } else if (c == ',') { + *p = 0; + ntmp = strip_spaces(q); + q = p + 1; + if (!ntmp) { + OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_NULL_NAME); + goto err; + } + X509V3_add_value(ntmp, nullptr, &values); + } + break; + + case HDR_VALUE: + if (c == ',') { + state = HDR_NAME; + *p = 0; + vtmp = strip_spaces(q); + if (!vtmp) { + OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_NULL_VALUE); + goto err; + } + X509V3_add_value(ntmp, vtmp, &values); + ntmp = nullptr; + q = p + 1; + } } } - // The final label must not end in a hyphen or ".", and - // there must be at least two dots after the star. - if ((state & (LABEL_START | LABEL_HYPHEN)) != 0 || dots < 2) { - return nullptr; + if (state == HDR_VALUE) { + vtmp = strip_spaces(q); + if (!vtmp) { + OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_NULL_VALUE); + goto err; + } + X509V3_add_value(ntmp, vtmp, &values); + } else { + ntmp = strip_spaces(q); + if (!ntmp) { + OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_NULL_NAME); + goto err; + } + X509V3_add_value(ntmp, nullptr, &values); } - return star; + OPENSSL_free(linebuf); + return values; + +err: + OPENSSL_free(linebuf); + sk_CONF_VALUE_pop_free(values, X509V3_conf_free); + return nullptr; } -// Compare using wildcards. -static int equal_wildcard(const unsigned char *pattern, size_t pattern_len, - const unsigned char *subject, size_t subject_len, - unsigned int flags) { - const unsigned char *star = nullptr; +// hex string utilities - // Subject names starting with '.' can only match a wildcard pattern - // via a subject sub-domain pattern suffix match. - if (!(subject_len > 1 && subject[0] == '.')) { - star = valid_star(pattern, pattern_len, flags); +char *bssl::x509v3_bytes_to_hex(const uint8_t *in, size_t len) { + CBB cbb; + if (!CBB_init(&cbb, len * 3 + 1)) { + goto err; } - if (star == nullptr) { - return equal_nocase(pattern, pattern_len, subject, subject_len, flags); + for (size_t i = 0; i < len; i++) { + static const char hex[] = "0123456789ABCDEF"; + if ((i > 0 && !CBB_add_u8(&cbb, ':')) || + !CBB_add_u8(&cbb, hex[in[i] >> 4]) || + !CBB_add_u8(&cbb, hex[in[i] & 0xf])) { + goto err; + } + } + uint8_t *ret; + size_t unused_len; + if (!CBB_add_u8(&cbb, 0) || !CBB_finish(&cbb, &ret, &unused_len)) { + goto err; } - return wildcard_match(pattern, star - pattern, star + 1, - (pattern + pattern_len) - star - 1, subject, - subject_len, flags); -} -int bssl::x509v3_looks_like_dns_name(const unsigned char *in, size_t len) { - // This function is used as a heuristic for whether a common name is a - // hostname to be matched, or merely a decorative name to describe the - // subject. This heuristic must be applied to both name constraints and the - // common name fallback, so it must be loose enough to accept hostname - // common names, and tight enough to reject decorative common names. + return (char *)ret; - if (len > 0 && in[len - 1] == '.') { - len--; - } +err: + CBB_cleanup(&cbb); + return nullptr; +} - // Wildcards are allowed in front. - if (len >= 2 && in[0] == '*' && in[1] == '.') { - in += 2; - len -= 2; +unsigned char *bssl::x509v3_hex_to_bytes(const char *str, size_t *len) { + unsigned char *hexbuf, *q; + unsigned char ch, cl, *p; + uint8_t high, low; + if (!str) { + OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_NULL_ARGUMENT); + return nullptr; } - - if (len == 0) { - return 0; + if (!(hexbuf = + reinterpret_cast(OPENSSL_malloc(strlen(str) >> 1)))) { + goto err; } - - size_t label_start = 0; - for (size_t i = 0; i < len; i++) { - unsigned char c = in[i]; - if (OPENSSL_isalnum(c) || (c == '-' && i > label_start) || - // These are not valid characters in hostnames, but commonly found - // in deployments outside the Web PKI. - c == '_' || c == ':') { + for (p = (unsigned char *)str, q = hexbuf; *p;) { + ch = *p++; + if (ch == ':') { continue; } - - // Labels must not be empty. - if (c == '.' && i > label_start && i < len - 1) { - label_start = i + 1; - continue; + cl = *p++; + if (!cl) { + OPENSSL_PUT_ERROR(X509V3, X509V3_R_ODD_NUMBER_OF_DIGITS); + OPENSSL_free(hexbuf); + return nullptr; } + if (!OPENSSL_fromxdigit(&high, ch)) { + goto badhex; + } + if (!OPENSSL_fromxdigit(&low, cl)) { + goto badhex; + } + *q++ = (high << 4) | low; + } - return 0; + if (len) { + *len = q - hexbuf; } - return 1; -} + return hexbuf; -// Compare an ASN1_STRING to a supplied string. If they match return 1. If -// cmp_type > 0 only compare if string matches the type, otherwise convert it -// to UTF8. +err: + OPENSSL_free(hexbuf); + return nullptr; -static int do_check_string(const ASN1_STRING *a, int cmp_type, equal_fn equal, - unsigned int flags, int check_type, const char *b, - size_t blen, char **peername) { - int rv = 0; +badhex: + OPENSSL_free(hexbuf); + OPENSSL_PUT_ERROR(X509V3, X509V3_R_ILLEGAL_HEX_DIGIT); + return nullptr; +} - if (!a->data || !a->length) { +int bssl::x509v3_conf_name_matches(const char *name, const char *cmp) { + // `name` must begin with `cmp`. + size_t len = strlen(cmp); + if (strncmp(name, cmp, len) != 0) { return 0; } - if (cmp_type > 0) { - if (cmp_type != a->type) { - return 0; - } - if (cmp_type == V_ASN1_IA5STRING) { - rv = equal(a->data, a->length, (unsigned char *)b, blen, flags); - } else if (a->length == (int)blen && !OPENSSL_memcmp(a->data, b, blen)) { - rv = 1; - } - if (rv > 0 && peername) { - *peername = OPENSSL_strndup((char *)a->data, a->length); - if (*peername == nullptr) { - return -1; - } - } - } else { - int astrlen; - unsigned char *astr; - astrlen = ASN1_STRING_to_UTF8(&astr, a); - if (astrlen < 0) { - return -1; - } - // We check the common name against DNS name constraints if it passes - // |x509v3_looks_like_dns_name|. Thus we must not consider common names - // for DNS fallbacks if they fail this check. - if (check_type == GEN_DNS && !x509v3_looks_like_dns_name(astr, astrlen)) { - rv = 0; - } else { - rv = equal(astr, astrlen, (unsigned char *)b, blen, flags); - } - if (rv > 0 && peername) { - *peername = OPENSSL_strndup((char *)astr, astrlen); - if (*peername == nullptr) { - return -1; - } - } - OPENSSL_free(astr); + // `name` must either be equal to `cmp` or begin with `cmp`, followed by '.'. + return name[len] == '\0' || name[len] == '.'; +} + +STACK_OF(OPENSSL_STRING) *X509_get1_email(const X509 *x) { + int critical; + UniquePtr gens(reinterpret_cast( + X509_get_ext_d2i(x, NID_subject_alt_name, &critical, nullptr))); + if (!gens && critical != -1) { + return nullptr; } - return rv; + return get_email(X509_get_subject_name(x), gens.get()).release(); } -static int do_x509_check(const X509 *x, const char *chk, size_t chklen, - unsigned int flags, int check_type, char **peername) { - int cnid = NID_undef; - int alt_type; - int rv = 0; - equal_fn equal; - if (check_type == GEN_EMAIL) { - cnid = NID_pkcs9_emailAddress; - alt_type = V_ASN1_IA5STRING; - equal = equal_email; - } else if (check_type == GEN_DNS) { - cnid = NID_commonName; - alt_type = V_ASN1_IA5STRING; - if (flags & X509_CHECK_FLAG_NO_WILDCARDS) { - equal = equal_nocase; - } else { - equal = equal_wildcard; +STACK_OF(OPENSSL_STRING) *X509_get1_ocsp(const X509 *x) { + UniquePtr info( + reinterpret_cast( + X509_get_ext_d2i(x, NID_info_access, nullptr, nullptr))); + if (!info) { + return nullptr; + } + UniquePtr ret; + for (const ACCESS_DESCRIPTION *ad : info.get()) { + if (OBJ_obj2nid(ad->method) == NID_ad_OCSP) { + if (ad->location->type == GEN_URI) { + if (!append_ia5(&ret, ad->location->d.uniformResourceIdentifier)) { + return nullptr; + } + } } - } else { - alt_type = V_ASN1_OCTET_STRING; - equal = equal_case; } + sk_OPENSSL_STRING_sort_and_dedup(ret.get(), str_free); + return ret.release(); +} +STACK_OF(OPENSSL_STRING) *X509_REQ_get1_email(const X509_REQ *x) { + UniquePtr exts(X509_REQ_get_extensions(x)); int critical; UniquePtr gens(reinterpret_cast( - X509_get_ext_d2i(x, NID_subject_alt_name, &critical, nullptr))); - if (gens) { - for (const GENERAL_NAME *gen : gens.get()) { - if (gen->type != check_type) { - continue; - } - const ASN1_STRING *cstr; - if (check_type == GEN_EMAIL) { - cstr = gen->d.rfc822Name; - } else if (check_type == GEN_DNS) { - cstr = gen->d.dNSName; - } else { - cstr = gen->d.iPAddress; - } - // Positive on success, negative on error! - if ((rv = do_check_string(cstr, alt_type, equal, flags, check_type, chk, - chklen, peername)) != 0) { - break; - } - } - return rv; - } else if (critical != -1) { - // Syntax error in the subjectAltName extension. - return 0; + X509V3_get_d2i(exts.get(), NID_subject_alt_name, &critical, nullptr))); + if (!gens && critical != -1) { + return nullptr; } + return get_email(X509_REQ_get_subject_name(x), gens.get()).release(); +} - // We're done if CN-ID is not pertinent - if (cnid == NID_undef || (flags & X509_CHECK_FLAG_NEVER_CHECK_SUBJECT)) { +void X509_email_free(STACK_OF(OPENSSL_STRING) *sk) { + sk_OPENSSL_STRING_pop_free(sk, str_free); +} + +int bssl::x509v3_looks_like_dns_name(const unsigned char *in, size_t len) { + // This function is used as a heuristic for whether a common name is a + // hostname to be matched, or merely a decorative name to describe the + // subject. This heuristic must be applied to both name constraints and the + // common name fallback, so it must be loose enough to accept hostname + // common names, and tight enough to reject decorative common names. + + if (len > 0 && in[len - 1] == '.') { + len--; + } + + // Wildcards are allowed in front. + if (len >= 2 && in[0] == '*' && in[1] == '.') { + in += 2; + len -= 2; + } + + if (len == 0) { return 0; } - int j = -1; - const X509_NAME *name = X509_get_subject_name(x); - while ((j = X509_NAME_get_index_by_NID(name, cnid, j)) >= 0) { - const X509_NAME_ENTRY *ne = X509_NAME_get_entry(name, j); - const ASN1_STRING *str = X509_NAME_ENTRY_get_data(ne); - // Positive on success, negative on error! - if ((rv = do_check_string(str, -1, equal, flags, check_type, chk, chklen, - peername)) != 0) { - return rv; + size_t label_start = 0; + for (size_t i = 0; i < len; i++) { + unsigned char c = in[i]; + if (OPENSSL_isalnum(c) || (c == '-' && i > label_start) || + // These are not valid characters in hostnames, but commonly found + // in deployments outside the Web PKI. + c == '_' || c == ':') { + continue; + } + + // Labels must not be empty. + if (c == '.' && i > label_start && i < len - 1) { + label_start = i + 1; + continue; } + + return 0; } - return 0; + + return 1; } int X509_check_host(const X509 *x, const char *chk, size_t chklen, @@ -1094,191 +1261,6 @@ int bssl::x509v3_a2i_ipadd(uint8_t ipout[16], const char *ipasc) { } } -// get_ipv4_component consumes one IPv4 component, terminated by either '.' or -// the end of the string, from |*str|. On success, it returns one, sets |*out| -// to the component, and advances |*str| to the first unconsumed character. On -// invalid input, it returns zero. -static int get_ipv4_component(uint8_t *out_byte, const char **str) { - // Store a slightly larger intermediary so the overflow check is easier. - uint32_t out = 0; - for (;;) { - if (!OPENSSL_isdigit(**str)) { - return 0; - } - out = (out * 10) + (**str - '0'); - if (out > 255) { - // Components must be 8-bit. - return 0; - } - (*str)++; - if ((**str) == '.' || (**str) == '\0') { - *out_byte = (uint8_t)out; - return 1; - } - if (out == 0) { - // Reject extra leading zeros. Parsers sometimes treat them as octal, so - // accepting them would misinterpret input. - return 0; - } - } -} - -// get_ipv4_dot consumes a '.' from |*str| and advances it. It returns one on -// success and zero if |*str| does not point to a '.'. -static int get_ipv4_dot(const char **str) { - if (**str != '.') { - return 0; - } - (*str)++; - return 1; -} - -static int ipv4_from_asc(uint8_t v4[4], const char *in) { - if (!get_ipv4_component(&v4[0], &in) || !get_ipv4_dot(&in) || - !get_ipv4_component(&v4[1], &in) || !get_ipv4_dot(&in) || - !get_ipv4_component(&v4[2], &in) || !get_ipv4_dot(&in) || - !get_ipv4_component(&v4[3], &in) || *in != '\0') { - return 0; - } - return 1; -} - -typedef struct { - // Temporary store for IPV6 output - uint8_t tmp[16]; - // Total number of bytes in tmp - int total; - // The position of a zero (corresponding to '::') - int zero_pos; - // Number of zeroes - int zero_cnt; -} IPV6_STAT; - -static int ipv6_from_asc(uint8_t v6[16], const char *in) { - IPV6_STAT v6stat; - v6stat.total = 0; - v6stat.zero_pos = -1; - v6stat.zero_cnt = 0; - // Treat the IPv6 representation as a list of values separated by ':'. - // The presence of a '::' will parse as one, two or three zero length - // elements. - if (!CONF_parse_list(in, ':', 0, ipv6_cb, &v6stat)) { - return 0; - } - - if (v6stat.zero_pos == -1) { - // If no '::' must have exactly 16 bytes - if (v6stat.total != 16) { - return 0; - } - } else { - // If '::' must have less than 16 bytes - if (v6stat.total >= 16) { - return 0; - } - if (v6stat.zero_cnt > 3) { - // More than three zeroes is an error - return 0; - } else if (v6stat.zero_cnt == 3) { - // Can only have three zeroes if nothing else present - if (v6stat.total > 0) { - return 0; - } - } else if (v6stat.zero_cnt == 2) { - // Can only have two zeroes if at start or end - if (v6stat.zero_pos != 0 && v6stat.zero_pos != v6stat.total) { - return 0; - } - } else { - // Can only have one zero if *not* start or end - if (v6stat.zero_pos == 0 || v6stat.zero_pos == v6stat.total) { - return 0; - } - } - } - - // Format the result. - if (v6stat.zero_pos >= 0) { - // Copy initial part - OPENSSL_memcpy(v6, v6stat.tmp, v6stat.zero_pos); - // Zero middle - OPENSSL_memset(v6 + v6stat.zero_pos, 0, 16 - v6stat.total); - // Copy final part - if (v6stat.total != v6stat.zero_pos) { - OPENSSL_memcpy(v6 + v6stat.zero_pos + 16 - v6stat.total, - v6stat.tmp + v6stat.zero_pos, - v6stat.total - v6stat.zero_pos); - } - } else { - OPENSSL_memcpy(v6, v6stat.tmp, 16); - } - - return 1; -} - -static int ipv6_cb(const char *elem, size_t len, void *usr) { - IPV6_STAT *s = reinterpret_cast(usr); - // Error if 16 bytes written - if (s->total == 16) { - return 0; - } - if (len == 0) { - // Zero length element, corresponds to '::' - if (s->zero_pos == -1) { - s->zero_pos = s->total; - } else if (s->zero_pos != s->total) { - // If we've already got a :: its an error - return 0; - } - if (s->zero_cnt >= 3) { - // More than three zeros is an error. - return 0; - } - s->zero_cnt++; - } else { - // If more than 4 characters could be final a.b.c.d form - if (len > 4) { - // Need at least 4 bytes left - if (s->total > 12) { - return 0; - } - // Must be end of string - if (elem[len]) { - return 0; - } - if (!ipv4_from_asc(s->tmp + s->total, elem)) { - return 0; - } - s->total += 4; - } else { - if (!ipv6_hex(s->tmp + s->total, elem, len)) { - return 0; - } - s->total += 2; - } - } - return 1; -} - -// Convert a string of up to 4 hex digits into the corresponding IPv6 form. - -static int ipv6_hex(uint8_t *out, const char *in, size_t inlen) { - if (inlen > 4) { - return 0; - } - uint16_t num = 0; - while (inlen--) { - uint8_t val; - if (!OPENSSL_fromxdigit(&val, *in++)) { - return 0; - } - num = (num << 4) | val; - } - out[0] = num >> 8; - out[1] = num & 0xff; - return 1; -} - int bssl::X509V3_NAME_from_section(X509_NAME *nm, const STACK_OF(CONF_VALUE) *dn_sk, int chtype) { diff --git a/third_party/boringssl/src/crypto/x509/x509.cc b/third_party/boringssl/src/crypto/x509/x509.cc index 05bedc6bb..6b134369e 100644 --- a/third_party/boringssl/src/crypto/x509/x509.cc +++ b/third_party/boringssl/src/crypto/x509/x509.cc @@ -19,7 +19,7 @@ #include -// |X509_R_UNSUPPORTED_ALGORITHM| is no longer emitted, but continue to define +// `X509_R_UNSUPPORTED_ALGORITHM` is no longer emitted, but continue to define // it to avoid downstream churn. OPENSSL_DECLARE_ERROR_REASON(X509, UNSUPPORTED_ALGORITHM) diff --git a/third_party/boringssl/src/crypto/x509/x509_att.cc b/third_party/boringssl/src/crypto/x509/x509_att.cc index 654db9484..2499364f6 100644 --- a/third_party/boringssl/src/crypto/x509/x509_att.cc +++ b/third_party/boringssl/src/crypto/x509/x509_att.cc @@ -103,7 +103,7 @@ int X509_ATTRIBUTE_set1_data(X509_ATTRIBUTE *attr, int attrtype, if (attrtype == 0) { // Do nothing. This is used to create an empty value set in - // |X509_ATTRIBUTE_create_by_*|. This is invalid, but supported by OpenSSL. + // `X509_ATTRIBUTE_create_by_*`. This is invalid, but supported by OpenSSL. return 1; } @@ -114,9 +114,9 @@ int X509_ATTRIBUTE_set1_data(X509_ATTRIBUTE *attr, int attrtype, // This function is several functions in one. if (attrtype & MBSTRING_FLAG) { - // |data| is an encoded string. We must decode and re-encode it to |attr|'s - // preferred ASN.1 type. Note |len| may be -1, in which case - // |ASN1_STRING_set_by_NID| calls |strlen| automatically. + // `data` is an encoded string. We must decode and re-encode it to `attr`'s + // preferred ASN.1 type. Note `len` may be -1, in which case + // `ASN1_STRING_set_by_NID` calls `strlen` automatically. ASN1_STRING *str = ASN1_STRING_set_by_NID(nullptr, reinterpret_cast(data), len, attrtype, OBJ_obj2nid(attr->object)); @@ -126,8 +126,8 @@ int X509_ATTRIBUTE_set1_data(X509_ATTRIBUTE *attr, int attrtype, } asn1_type_set0_string(typ, str); } else if (len != -1) { - // |attrtype| must be a valid |ASN1_STRING| type. |data| and |len| is a - // value in the corresponding |ASN1_STRING| representation. + // `attrtype` must be a valid `ASN1_STRING` type. `data` and `len` is a + // value in the corresponding `ASN1_STRING` representation. ASN1_STRING *str = ASN1_STRING_type_new(attrtype); if (str == nullptr || !ASN1_STRING_set(str, data, len)) { ASN1_STRING_free(str); @@ -135,7 +135,7 @@ int X509_ATTRIBUTE_set1_data(X509_ATTRIBUTE *attr, int attrtype, } asn1_type_set0_string(typ, str); } else { - // |attrtype| must be a valid |ASN1_TYPE| type. |data| is a pointer to an + // `attrtype` must be a valid `ASN1_TYPE` type. `data` is a pointer to an // object of the corresponding type. if (!ASN1_TYPE_set1(typ, attrtype, data)) { goto err; diff --git a/third_party/boringssl/src/crypto/x509/x509_cmp.cc b/third_party/boringssl/src/crypto/x509/x509_cmp.cc index 1f66e6605..dcb88365c 100644 --- a/third_party/boringssl/src/crypto/x509/x509_cmp.cc +++ b/third_party/boringssl/src/crypto/x509/x509_cmp.cc @@ -72,14 +72,12 @@ X509_NAME *X509_get_subject_name(const X509 *a) { return const_cast(&impl->subject); } -ASN1_INTEGER *X509_get_serialNumber(X509 *a) { - auto *impl = FromOpaque(a); - return &impl->serialNumber; +ASN1_INTEGER *X509_get_serialNumber(X509 *x509) { + return FromOpaque(x509)->serialNumber.get(); } const ASN1_INTEGER *X509_get0_serialNumber(const X509 *x509) { - const auto *impl = FromOpaque(x509); - return &impl->serialNumber; + return FromOpaque(x509)->serialNumber.get(); } uint32_t X509_subject_name_hash(const X509 *x) { @@ -101,27 +99,27 @@ uint32_t X509_subject_name_hash_old(const X509 *x) { int X509_cmp(const X509 *a, const X509 *b) { const auto *a_impl = FromOpaque(a); const auto *b_impl = FromOpaque(b); - // Fill in the |cert_hash| fields. + // Fill in the `cert_hash` fields. // - // TODO(davidben): This may fail, in which case the the hash will be all + // TODO(davidben): This may fail, in which case the hash will be all // zeros. This produces a consistent comparison (failures are sticky), but // not a good one. OpenSSL now returns -2, but this is not a consistent // comparison and may cause misbehaving sorts by transitivity. For now, we // retain the old OpenSSL behavior, which was to ignore the error. See // https://crbug.com/boringssl/355. - x509v3_cache_extensions((X509Impl *)a_impl); - x509v3_cache_extensions((X509Impl *)b_impl); + x509v3_cache_extensions(a_impl); + x509v3_cache_extensions(b_impl); return OPENSSL_memcmp(a_impl->cert_hash, b_impl->cert_hash, SHA256_DIGEST_LENGTH); } int X509_NAME_cmp(const X509_NAME *a, const X509_NAME *b) { - const X509_NAME_CACHE *a_cache = x509_name_get_cache(a); + const X509NameCache *a_cache = x509_name_get_cache(a); if (a_cache == nullptr) { return -2; } - const X509_NAME_CACHE *b_cache = x509_name_get_cache(b); + const X509NameCache *b_cache = x509_name_get_cache(b); if (b_cache == nullptr) { return -2; } @@ -145,7 +143,7 @@ int X509_NAME_cmp(const X509_NAME *a, const X509_NAME *b) { } uint32_t X509_NAME_hash(const X509_NAME *x) { - const X509_NAME_CACHE *cache = x509_name_get_cache(x); + const X509NameCache *cache = x509_name_get_cache(x); if (cache == nullptr) { return 0; } @@ -158,7 +156,7 @@ uint32_t X509_NAME_hash(const X509_NAME *x) { // this is reasonably efficient. uint32_t X509_NAME_hash_old(const X509_NAME *x) { - const X509_NAME_CACHE *cache = x509_name_get_cache(x); + const X509NameCache *cache = x509_name_get_cache(x); if (cache == nullptr) { return 0; } @@ -216,7 +214,7 @@ ASN1_BIT_STRING *X509_get0_pubkey_bitstr(const X509 *x) { } // This function is not const-correct for OpenSSL compatibility. auto *impl = FromOpaque(x); - return const_cast(&impl->key.public_key); + return const_cast(impl->key.public_key.get()); } int X509_check_private_key(const X509 *x, const EVP_PKEY *k) { @@ -240,6 +238,6 @@ int X509_check_private_key(const X509 *x, const EVP_PKEY *k) { // Not strictly speaking an "up_ref" as a STACK doesn't have a reference // count but it has the same effect by duping the STACK and upping the ref of // each X509 structure. -STACK_OF(X509) *X509_chain_up_ref(STACK_OF(X509) *chain) { +STACK_OF(X509) *X509_chain_up_ref(const STACK_OF(X509) *chain) { return sk_X509_deep_copy(chain, X509_dup_ref, X509_free); } diff --git a/third_party/boringssl/src/crypto/x509/x509_ext.cc b/third_party/boringssl/src/crypto/x509/x509_ext.cc index db99212ec..b2ddb57b0 100644 --- a/third_party/boringssl/src/crypto/x509/x509_ext.cc +++ b/third_party/boringssl/src/crypto/x509/x509_ext.cc @@ -24,6 +24,20 @@ using namespace bssl; +namespace { + +X509_EXTENSION *delete_ext(STACK_OF(X509_EXTENSION) **exts, int loc) { + X509_EXTENSION *ext = X509v3_delete_ext(*exts, loc); + // Empty extension lists are omitted. + if (*exts != nullptr && sk_X509_EXTENSION_num(*exts) == 0) { + sk_X509_EXTENSION_pop_free(*exts, X509_EXTENSION_free); + *exts = nullptr; + } + return ext; +} + +} // namespace + int X509_CRL_get_ext_count(const X509_CRL *x) { return X509v3_get_ext_count(x->crl->extensions); } @@ -45,16 +59,6 @@ X509_EXTENSION *X509_CRL_get_ext(const X509_CRL *x, int loc) { return X509v3_get_ext(x->crl->extensions, loc); } -static X509_EXTENSION *delete_ext(STACK_OF(X509_EXTENSION) **exts, int loc) { - X509_EXTENSION *ext = X509v3_delete_ext(*exts, loc); - // Empty extension lists are omitted. - if (*exts != nullptr && sk_X509_EXTENSION_num(*exts) == 0) { - sk_X509_EXTENSION_pop_free(*exts, X509_EXTENSION_free); - *exts = nullptr; - } - return ext; -} - X509_EXTENSION *X509_CRL_delete_ext(X509_CRL *x, int loc) { return delete_ext(&x->crl->extensions, loc); } diff --git a/third_party/boringssl/src/crypto/x509/x509_lu.cc b/third_party/boringssl/src/crypto/x509/x509_lu.cc index 4adee7274..896337f86 100644 --- a/third_party/boringssl/src/crypto/x509/x509_lu.cc +++ b/third_party/boringssl/src/crypto/x509/x509_lu.cc @@ -30,22 +30,10 @@ using namespace bssl; -static int X509_OBJECT_idx_by_subject(STACK_OF(X509_OBJECT) *h, int type, - const X509_NAME *name); -static X509_OBJECT *X509_OBJECT_retrieve_by_subject(STACK_OF(X509_OBJECT) *h, - int type, - const X509_NAME *name); -static X509_OBJECT *X509_OBJECT_retrieve_match(STACK_OF(X509_OBJECT) *h, - X509_OBJECT *x); -static int X509_OBJECT_up_ref_count(X509_OBJECT *a); - -static X509_LOOKUP *X509_LOOKUP_new(const X509_LOOKUP_METHOD *method, - X509_STORE *store); -static int X509_LOOKUP_by_subject(X509_LOOKUP *ctx, int type, - const X509_NAME *name, X509_OBJECT *ret); - -static X509_LOOKUP *X509_LOOKUP_new(const X509_LOOKUP_METHOD *method, - X509_STORE *store) { +namespace { + +X509_LOOKUP *X509_LOOKUP_new(const X509_LOOKUP_METHOD *method, + X509_STORE *store) { X509_LOOKUP *ret = New(); if (ret == nullptr) { return nullptr; @@ -60,44 +48,22 @@ static X509_LOOKUP *X509_LOOKUP_new(const X509_LOOKUP_METHOD *method, return ret; } -void X509_LOOKUP_free(X509_LOOKUP *ctx) { - if (ctx == nullptr) { - return; - } - if (ctx->method != nullptr && ctx->method->free != nullptr) { - (*ctx->method->free)(ctx); - } - Delete(ctx); -} - -int X509_LOOKUP_ctrl(X509_LOOKUP *ctx, int cmd, const char *argc, long argl, - char **ret) { - if (ctx->method == nullptr) { - return -1; - } - if (ctx->method->ctrl != nullptr) { - return ctx->method->ctrl(ctx, cmd, argc, argl, ret); - } else { - return 1; - } -} - -static int X509_LOOKUP_by_subject(X509_LOOKUP *ctx, int type, - const X509_NAME *name, X509_OBJECT *ret) { +int X509_LOOKUP_by_subject(X509_LOOKUP *ctx, int type, const X509_NAME *name, + X509_OBJECT *ret) { if (ctx->method == nullptr || ctx->method->get_by_subject == nullptr) { return 0; } - // Note |get_by_subject| leaves |ret| in an inconsistent state. It has - // pointers to an |X509| or |X509_CRL|, but has not bumped the refcount yet. + // Note `get_by_subject` leaves `ret` in an inconsistent state. It has + // pointers to an `X509` or `X509_CRL`, but has not bumped the refcount yet. // For now, the caller is expected to fix this, but ideally we'd fix the - // |X509_LOOKUP| convention itself. + // `X509_LOOKUP` convention itself. return ctx->method->get_by_subject(ctx, type, name, ret) > 0; } -// x509_object_cmp_name compares |a| against the specified type and name. This +// x509_object_cmp_name compares `a` against the specified type and name. This // avoids needing to construct a reference certificate or CRL. -static int x509_object_cmp_name(const X509_OBJECT *a, int type, - const X509_NAME *name) { +int x509_object_cmp_name(const X509_OBJECT *a, int type, + const X509_NAME *name) { int ret = a->type - type; if (ret) { return ret; @@ -113,7 +79,7 @@ static int x509_object_cmp_name(const X509_OBJECT *a, int type, } } -static int x509_object_cmp(const X509_OBJECT *a, const X509_OBJECT *b) { +int x509_object_cmp(const X509_OBJECT *a, const X509_OBJECT *b) { switch (b->type) { case X509_LU_X509: return x509_object_cmp_name(a, b->type, @@ -126,11 +92,182 @@ static int x509_object_cmp(const X509_OBJECT *a, const X509_OBJECT *b) { } } -static int x509_object_cmp_sk(const X509_OBJECT *const *a, - const X509_OBJECT *const *b) { +int x509_object_cmp_sk(const X509_OBJECT *const *a, + const X509_OBJECT *const *b) { return x509_object_cmp(*a, *b); } +int X509_OBJECT_up_ref_count(X509_OBJECT *a) { + switch (a->type) { + case X509_LU_X509: + X509_up_ref(a->data.x509); + break; + case X509_LU_CRL: + X509_CRL_up_ref(a->data.crl); + break; + } + return 1; +} + +int x509_object_idx_cnt(STACK_OF(X509_OBJECT) *h, int type, + const X509_NAME *name, int *out_num_match) { + sk_X509_OBJECT_sort(h); + + // Find the first matching object. `sk_X509_OBJECT_find` would require + // constructing an `X509` or `X509_CRL` object, so implement our own binary + // search. + size_t start = 0, end = sk_X509_OBJECT_num(h); + while (end - start > 1) { + // Bias `mid` towards `start`. The range has more than one element, so `mid` + // is not the last element. + size_t mid = start + (end - start - 1) / 2; + assert(start <= mid && mid + 1 < end); + int r = x509_object_cmp_name(sk_X509_OBJECT_value(h, mid), type, name); + if (r < 0) { + start = mid + 1; // `mid` is too low. + } else if (r > 0) { + end = mid; // `mid` is too high. + } else { + // `mid` matches, but we need to keep searching to find the first match. + end = mid + 1; + } + } + if (start == end || + x509_object_cmp_name(sk_X509_OBJECT_value(h, start), type, name) != 0) { + return -1; + } + + if (out_num_match != nullptr) { + *out_num_match = 1; + for (size_t tidx = start + 1; tidx < sk_X509_OBJECT_num(h); tidx++) { + const X509_OBJECT *tobj = sk_X509_OBJECT_value(h, tidx); + if (x509_object_cmp_name(tobj, type, name) != 0) { + break; + } + (*out_num_match)++; + } + } + + assert(start <= INT_MAX); // `STACK_OF(T)` never stores more than `INT_MAX`. + return static_cast(start); +} + +int X509_OBJECT_idx_by_subject(STACK_OF(X509_OBJECT) *h, int type, + const X509_NAME *name) { + return x509_object_idx_cnt(h, type, name, nullptr); +} + +X509_OBJECT *X509_OBJECT_retrieve_by_subject(STACK_OF(X509_OBJECT) *h, int type, + const X509_NAME *name) { + int idx; + idx = X509_OBJECT_idx_by_subject(h, type, name); + if (idx == -1) { + return nullptr; + } + return sk_X509_OBJECT_value(h, idx); +} + +X509_OBJECT *x509_object_dup(const X509_OBJECT *obj) { + X509_OBJECT *ret = X509_OBJECT_new(); + if (ret == nullptr) { + return nullptr; + } + ret->type = obj->type; + ret->data = obj->data; + X509_OBJECT_up_ref_count(ret); + return ret; +} + +X509_OBJECT *X509_OBJECT_retrieve_match(STACK_OF(X509_OBJECT) *h, + X509_OBJECT *x) { + sk_X509_OBJECT_sort(h); + size_t idx; + if (!sk_X509_OBJECT_find(h, &idx, x)) { + return nullptr; + } + if ((x->type != X509_LU_X509) && (x->type != X509_LU_CRL)) { + return sk_X509_OBJECT_value(h, idx); + } + for (size_t i = idx; i < sk_X509_OBJECT_num(h); i++) { + X509_OBJECT *obj = sk_X509_OBJECT_value(h, i); + if (x509_object_cmp(obj, x)) { + return nullptr; + } + if (x->type == X509_LU_X509) { + if (!X509_cmp(obj->data.x509, x->data.x509)) { + return obj; + } + } else if (x->type == X509_LU_CRL) { + if (!X509_CRL_match(obj->data.crl, x->data.crl)) { + return obj; + } + } else { + return obj; + } + } + return nullptr; +} + +int x509_store_add(X509Store *ctx, const void *x, int is_crl) { + if (x == nullptr) { + return 0; + } + + X509_OBJECT *const obj = X509_OBJECT_new(); + if (obj == nullptr) { + return 0; + } + + if (is_crl) { + obj->type = X509_LU_CRL; + obj->data.crl = X509_CRL_dup_ref(static_cast(x)); + } else { + obj->type = X509_LU_X509; + obj->data.x509 = X509_dup_ref(static_cast(x)); + } + + ctx->objs_lock.LockWrite(); + + int ret = 1; + int added = 0; + // Duplicates are silently ignored + if (!X509_OBJECT_retrieve_match(ctx->objs.get(), obj)) { + ret = added = (sk_X509_OBJECT_push(ctx->objs.get(), obj) != 0); + } + + ctx->objs_lock.UnlockWrite(); + + if (!added) { + X509_OBJECT_free(obj); + } + + return ret; +} + +} // namespace + +void X509_LOOKUP_free(X509_LOOKUP *ctx) { + if (ctx == nullptr) { + return; + } + if (ctx->method != nullptr && ctx->method->free != nullptr) { + (*ctx->method->free)(ctx); + } + Delete(ctx); +} + +int X509_LOOKUP_ctrl(X509_LOOKUP *ctx, int cmd, const char *argc, long argl, + char **ret) { + if (ctx->method == nullptr) { + return -1; + } + if (ctx->method->ctrl != nullptr) { + return ctx->method->ctrl(ctx, cmd, argc, argl, ret); + } else { + return 1; + } +} + X509Store::X509Store() : RefCounted(CheckSubClass()), objs(sk_X509_OBJECT_new(x509_object_cmp_sk)), @@ -149,6 +286,13 @@ int X509_STORE_up_ref(X509_STORE *store) { return 1; } +X509_STORE *X509_STORE_dup_ref(const X509_STORE *store) { + if (store == nullptr) { + return nullptr; + } + return FromOpaque(store)->DupRefInternal(); +} + void X509_STORE_free(X509_STORE *vfy) { if (vfy == nullptr) { return; @@ -197,55 +341,18 @@ int X509_STORE_CTX_get_by_subject(X509_STORE_CTX *vs, int type, } // TODO(crbug.com/boringssl/685): This should call - // |X509_OBJECT_free_contents|. + // `X509_OBJECT_free_contents`. ret->type = tmp->type; ret->data = tmp->data; X509_OBJECT_up_ref_count(ret); return 1; } -static int x509_store_add(X509Store *ctx, void *x, int is_crl) { - if (x == nullptr) { - return 0; - } - - X509_OBJECT *const obj = X509_OBJECT_new(); - if (obj == nullptr) { - return 0; - } - - if (is_crl) { - obj->type = X509_LU_CRL; - obj->data.crl = (X509_CRL *)x; - } else { - obj->type = X509_LU_X509; - obj->data.x509 = (X509 *)x; - } - X509_OBJECT_up_ref_count(obj); - - ctx->objs_lock.LockWrite(); - - int ret = 1; - int added = 0; - // Duplicates are silently ignored - if (!X509_OBJECT_retrieve_match(ctx->objs.get(), obj)) { - ret = added = (sk_X509_OBJECT_push(ctx->objs.get(), obj) != 0); - } - - ctx->objs_lock.UnlockWrite(); - - if (!added) { - X509_OBJECT_free(obj); - } - - return ret; -} - -int X509_STORE_add_cert(X509_STORE *ctx, X509 *x) { +int X509_STORE_add_cert(X509_STORE *ctx, const X509 *x) { return x509_store_add(FromOpaque(ctx), x, /*is_crl=*/0); } -int X509_STORE_add_crl(X509_STORE *ctx, X509_CRL *x) { +int X509_STORE_add_crl(X509_STORE *ctx, const X509_CRL *x) { return x509_store_add(FromOpaque(ctx), x, /*is_crl=*/1); } @@ -259,18 +366,6 @@ void X509_OBJECT_free(X509_OBJECT *obj) { Delete(obj); } -static int X509_OBJECT_up_ref_count(X509_OBJECT *a) { - switch (a->type) { - case X509_LU_X509: - X509_up_ref(a->data.x509); - break; - case X509_LU_CRL: - X509_CRL_up_ref(a->data.crl); - break; - } - return 1; -} - void X509_OBJECT_free_contents(X509_OBJECT *a) { switch (a->type) { case X509_LU_X509: @@ -293,76 +388,6 @@ X509 *X509_OBJECT_get0_X509(const X509_OBJECT *a) { return a->data.x509; } -static int x509_object_idx_cnt(STACK_OF(X509_OBJECT) *h, int type, - const X509_NAME *name, int *out_num_match) { - sk_X509_OBJECT_sort(h); - - // Find the first matching object. |sk_X509_OBJECT_find| would require - // constructing an |X509| or |X509_CRL| object, so implement our own binary - // search. - size_t start = 0, end = sk_X509_OBJECT_num(h); - while (end - start > 1) { - // Bias |mid| towards |start|. The range has more than one element, so |mid| - // is not the last element. - size_t mid = start + (end - start - 1) / 2; - assert(start <= mid && mid + 1 < end); - int r = x509_object_cmp_name(sk_X509_OBJECT_value(h, mid), type, name); - if (r < 0) { - start = mid + 1; // |mid| is too low. - } else if (r > 0) { - end = mid; // |mid| is too high. - } else { - // |mid| matches, but we need to keep searching to find the first match. - end = mid + 1; - } - } - if (start == end || - x509_object_cmp_name(sk_X509_OBJECT_value(h, start), type, name) != 0) { - return -1; - } - - if (out_num_match != nullptr) { - *out_num_match = 1; - for (size_t tidx = start + 1; tidx < sk_X509_OBJECT_num(h); tidx++) { - const X509_OBJECT *tobj = sk_X509_OBJECT_value(h, tidx); - if (x509_object_cmp_name(tobj, type, name) != 0) { - break; - } - (*out_num_match)++; - } - } - - assert(start <= INT_MAX); // |STACK_OF(T)| never stores more than |INT_MAX|. - return static_cast(start); -} - -static int X509_OBJECT_idx_by_subject(STACK_OF(X509_OBJECT) *h, int type, - const X509_NAME *name) { - return x509_object_idx_cnt(h, type, name, nullptr); -} - -static X509_OBJECT *X509_OBJECT_retrieve_by_subject(STACK_OF(X509_OBJECT) *h, - int type, - const X509_NAME *name) { - int idx; - idx = X509_OBJECT_idx_by_subject(h, type, name); - if (idx == -1) { - return nullptr; - } - return sk_X509_OBJECT_value(h, idx); -} - -static X509_OBJECT *x509_object_dup(const X509_OBJECT *obj) { - X509_OBJECT *ret = X509_OBJECT_new(); - if (ret == nullptr) { - return nullptr; - } - ret->type = obj->type; - ret->data = obj->data; - X509_OBJECT_up_ref_count(ret); - return ret; -} - STACK_OF(X509_OBJECT) *X509_STORE_get1_objects(X509_STORE *store) { auto *impl = FromOpaque(store); MutexReadLock lock(&impl->objs_lock); @@ -456,36 +481,6 @@ STACK_OF(X509_CRL) *X509_STORE_CTX_get1_crls(X509_STORE_CTX *ctx, return sk; } -static X509_OBJECT *X509_OBJECT_retrieve_match(STACK_OF(X509_OBJECT) *h, - X509_OBJECT *x) { - sk_X509_OBJECT_sort(h); - size_t idx; - if (!sk_X509_OBJECT_find(h, &idx, x)) { - return nullptr; - } - if ((x->type != X509_LU_X509) && (x->type != X509_LU_CRL)) { - return sk_X509_OBJECT_value(h, idx); - } - for (size_t i = idx; i < sk_X509_OBJECT_num(h); i++) { - X509_OBJECT *obj = sk_X509_OBJECT_value(h, i); - if (x509_object_cmp(obj, x)) { - return nullptr; - } - if (x->type == X509_LU_X509) { - if (!X509_cmp(obj->data.x509, x->data.x509)) { - return obj; - } - } else if (x->type == X509_LU_CRL) { - if (!X509_CRL_match(obj->data.crl, x->data.crl)) { - return obj; - } - } else { - return obj; - } - } - return nullptr; -} - int X509_STORE_CTX_get1_issuer(X509 **out_issuer, X509_STORE_CTX *ctx, const X509 *x) { X509_OBJECT obj; @@ -501,7 +496,7 @@ int X509_STORE_CTX_get1_issuer(X509 **out_issuer, X509_STORE_CTX *ctx, X509_OBJECT_free_contents(&obj); // Else find index of first cert accepted by - // |x509_check_issued_with_callback|. + // `x509_check_issued_with_callback`. X509Store *store = FromOpaque(ctx->ctx); MutexWriteLock lock(&store->objs_lock); int idx = X509_OBJECT_idx_by_subject(store->objs.get(), X509_LU_X509, xn); @@ -512,7 +507,7 @@ int X509_STORE_CTX_get1_issuer(X509 **out_issuer, X509_STORE_CTX *ctx, // See if we've run past the matches. // // This works because the objects are sorted by type, then subject - // name, using |x509_object_cmp|. + // name, using `x509_object_cmp`. if (pobj->type != X509_LU_X509) { return 0; } diff --git a/third_party/boringssl/src/crypto/x509/x509_mtc.cc b/third_party/boringssl/src/crypto/x509/x509_mtc.cc new file mode 100644 index 000000000..5853f8f17 --- /dev/null +++ b/third_party/boringssl/src/crypto/x509/x509_mtc.cc @@ -0,0 +1,632 @@ +// Copyright 2026 The BoringSSL Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include +#include + +#include "../bytestring/internal.h" +#include "../internal.h" +#include "./internal.h" + + +BSSL_NAMESPACE_BEGIN +namespace { + +// Prefix for domain separation to denote a Merkle Tree leaf or interior node. +constexpr uint8_t kMTCLeafDomainSeparator[] = {0x00}; +constexpr uint8_t kMTCNodeDomainSeparator[] = {0x01}; +// DER encoding of AlgorithmIdentifier for alg-mtcProof with absent parameters. +// SEQUENCE (10 bytes) { +// OBJECT IDENTIFIER 1.3.6.1.5.5.7.6.67 (8 bytes) +// } +constexpr uint8_t kMTCAlgorithmIdentifier[] = { + 0x30, // SEQUENCE tag + 10, // tag + len + OID bytes + 0x06, // OBJECT tag + 8, // OID length + OBJ_ENC_alg_mtcProof, +}; +// MTCLogEntryType enum value for tbs_cert_entry. +constexpr uint8_t kMTCLogEntryTypeTBSCertEntry[] = {0x00, 0x01}; +// ASN.1 tag for encoded OCTET STRING. +constexpr uint8_t kEncodedOctetStringTag[] = {0x04}; +// Prefix for domain separation used in CosignedMessage. +constexpr uint8_t kCosignedMessageLabel[12] = {'s', 'u', 'b', 't', 'r', 'e', + 'e', '/', 'v', '1', '\n', '\0'}; +// String prefix used to construct ASCII representation of trust anchor IDs for +// CosignedMessage fields. +constexpr uint8_t kASCIIOIDPrefix[16] = {'o', 'i', 'd', '/', '1', '.', + '3', '.', '6', '.', '1', '.', + '4', '.', '1', '.'}; +// DER encoded OID component found in trust anchor IDs for issuance logs. +constexpr uint8_t kEncOIDLogs = 0x00; +// draft-ietf-tls-trust-anchor-ids-06 limits trust anchor IDs to 32 bytes. +constexpr size_t kMaxTrustAnchorIdBytes = 32; + +constexpr CBS_ASN1_TAG kTBSVersionTag = + CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 0; + +// HashNode executes one hashing step in the evaluation of an inclusion proof. +void HashNode(Span out, const EVP_MD *log_hash, + Span left_child, Span right_child) { + assert(out.size() == EVP_MD_size(log_hash)); + assert(left_child.size() == EVP_MD_size(log_hash)); + assert(right_child.size() == EVP_MD_size(log_hash)); + ScopedEVP_MD_CTX ctx; + EVP_DigestInit_ex(ctx.get(), log_hash, nullptr); + EVP_DigestUpdate(ctx.get(), kMTCNodeDomainSeparator, + sizeof(kMTCNodeDomainSeparator)); + EVP_DigestUpdate(ctx.get(), left_child.data(), left_child.size()); + EVP_DigestUpdate(ctx.get(), right_child.data(), right_child.size()); + EVP_DigestFinal_ex(ctx.get(), out.data(), nullptr); +} + +// lsb returns whether the least-significant bit of `n` is set. +inline bool lsb(uint64_t n) { return n & 1; } + +// is_mtc_proof returns whether `algor` is an mtcProof AlgorithmIdentifier. +bool is_mtc_proof(const X509_ALGOR *algor) { + return OBJ_obj2nid(algor->algorithm) == NID_alg_mtcProof && + algor->parameter == nullptr; +} + +// is_mtc_serial_valid returns whether `serial` is within +// (mtcMinSerial..mtcMaxSerial), which implies the log number is > 0. +bool is_mtc_serial_valid(uint64_t serial) { + // mtcMinSerial is 2^48. mtcMaxSerial is 2^64-1, the largest number + // representable as uint64_t, so is not checked explicitly. + return serial >> 48; +} + +// Returns the first hash value (of size `log_hash_size`) remaining in +// `inclusion_proof` and advances `inclusion_proof` past the returned value. +// Returns an empty span if there are no more hash values of the appropriate +// size. +Span GetNextValueFromInclusionProof( + size_t log_hash_size, Span &inclusion_proof) { + if (inclusion_proof.size() < log_hash_size) { + return Span(); + } + Span value = inclusion_proof.first(log_hash_size); + inclusion_proof = inclusion_proof.subspan(log_hash_size); + return value; +} + +// Writes `data` to the digest in `ctx`, prepending an 8-bit length prefix. +bool DigestUpdateWithU8LengthPrefix(EVP_MD_CTX *ctx, Span data) { + BSSL_CHECK(data.size() < (1u << 8)); + const uint8_t length_prefix = static_cast(data.size()); + return EVP_DigestUpdate(ctx, &length_prefix, sizeof(length_prefix)) && + EVP_DigestUpdate(ctx, data.data(), data.size()); +} + +// Writes `data` to the digest in `ctx`, prepending a 16-bit length prefix. +bool DigestUpdateWithU16LengthPrefix(EVP_MD_CTX *ctx, + Span data) { + BSSL_CHECK(data.size() < (1u << 16)); + const uint8_t length_prefix[2] = {static_cast(data.size() >> 8), + static_cast(data.size())}; + return EVP_DigestUpdate(ctx, length_prefix, sizeof(length_prefix)) && + EVP_DigestUpdate(ctx, data.data(), data.size()); +} + +// Reads the next ASN.1 element from `cbs` and writes it as-is to the digest in +// `ctx`, including ASN.1 header bytes. +bool DigestUpdateNextASN1Element(EVP_MD_CTX *ctx, CBS *cbs) { + CBS element; + if (!CBS_get_any_asn1_element(cbs, &element, /*out_tag=*/nullptr, + /*out_header_len=*/nullptr)) { + return false; + } + return EVP_DigestUpdate(ctx, CBS_data(&element), CBS_len(&element)); +} + +// CheckMTCLogEntryExtensions returns whether `extensions` is a well-formed list +// of MTCLogEntryExtension structures, sorted in strictly ascending order by +// extension type (see section 5.2.1). No extension types are currently +// recognized, so the contents are otherwise ignored (see section 12.5). +bool CheckMTCLogEntryExtensions(CBS extensions) { + std::optional prev_type; + while (CBS_len(&extensions) > 0) { + uint16_t type; + CBS data; + if (!CBS_get_u16(&extensions, &type) || + !CBS_get_u16_length_prefixed(&extensions, &data)) { + OPENSSL_PUT_ERROR(X509, X509_R_INVALID_MTC_PROOF); + return false; + } + if (prev_type.has_value() && *prev_type >= type) { + OPENSSL_PUT_ERROR(X509, X509_R_INVALID_MTC_PROOF); + return false; + } + prev_type = type; + } + return true; +} + +// MTCCACosigner represents a Merkle Tree CA and its corresponding CA cosigner +// (see draft-ietf-plants-merkle-tree-certs, section 5.5). +// Only id-pe-mtcCertificationAuthority-SHA256 is supported. +class MTCCACosigner { + public: + MTCCACosigner() = default; + ~MTCCACosigner() = default; + + // Init() must be called before this object can be used. + // Init parses parameters defining an MTC CA cosigner from `x509`, including + // its MTCCertificationAuthority extension. `pkey` is the public key + // previously parsed from `x509`. Returns true if successfully initialized an + // MTCCACosigner representing a valid MTC CA, or false on error. + bool Init(const X509 *x509, const EVP_PKEY *pkey) { + // Get and parse the MTCCertificationAuthority extension (see section 5.5). + int ext_index = + X509_get_ext_by_NID(x509, NID_pe_mtcCertificationAuthority_SHA256, -1); + const X509_EXTENSION *ext = X509_get_ext(x509, ext_index); + if (ext == nullptr || !X509_EXTENSION_get_critical(ext)) { + OPENSSL_PUT_ERROR(X509, X509_R_INVALID_MTC_CA); + return false; + } + const ASN1_STRING *value = X509_EXTENSION_get_data(ext); + CBS ext_value, seq; + CBS_init(&ext_value, ASN1_STRING_get0_data(value), + ASN1_STRING_length(value)); + if (!CBS_get_asn1(&ext_value, &seq, CBS_ASN1_SEQUENCE) || // + CBS_len(&ext_value) != 0 || // + !x509_parse_algorithm(&seq, cosign_sigalg_.get()) || // + !CBS_get_asn1_uint64(&seq, &min_serial_) || // + !CBS_get_asn1_uint64(&seq, &max_serial_) || // + CBS_len(&seq) != 0) { + OPENSSL_PUT_ERROR(ASN1, ASN1_R_DECODE_ERROR); + return false; + } + if (!is_mtc_serial_valid(min_serial_) || + !is_mtc_serial_valid(max_serial_)) { + OPENSSL_PUT_ERROR(X509, X509_R_INVALID_MTC_CA); + return false; + } + // Extract the CA ID from the subject (see section 5.1). + const X509_NAME *subject = X509_get_subject_name(x509); + int tai_index = + X509_NAME_get_index_by_NID(subject, NID_rdna_trustAnchorID, -1); + if (tai_index < 0 || X509_NAME_entry_count(subject) != 1) { + OPENSSL_PUT_ERROR(X509, X509_R_INVALID_MTC_CA); + return false; + } + const ASN1_STRING *tai = + X509_NAME_ENTRY_get_data(X509_NAME_get_entry(subject, tai_index)); + // RELATIVE-OID has no dedicated ASN1_STRING representation, so the value + // is stored as V_ASN1_OTHER containing the entire element. + if (ASN1_STRING_type(tai) != V_ASN1_OTHER) { + OPENSSL_PUT_ERROR(X509, X509_R_WRONG_TYPE); + return false; + } + CBS tai_elem, tai_relative_oid; + CBS_init(&tai_elem, ASN1_STRING_get0_data(tai), ASN1_STRING_length(tai)); + if (!CBS_get_asn1(&tai_elem, &tai_relative_oid, CBS_ASN1_RELATIVE_OID) || + CBS_len(&tai_elem) != 0 || + !CBS_is_valid_asn1_relative_oid(&tai_relative_oid)) { + OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_OBJECT_ENCODING); + return false; + } + if (CBS_len(&tai_relative_oid) > kMaxTrustAnchorIdBytes) { + OPENSSL_PUT_ERROR(X509, X509_R_INVALID_MTC_CA); + return false; + } + if (!ca_id_enc_oid_.CopyFrom(tai_relative_oid)) { + OPENSSL_PUT_ERROR(X509, ERR_R_INTERNAL_ERROR); + return false; + } + + cosign_pubkey_.reset(EVP_PKEY_dup_ref(pkey)); + return true; + } + + bool IsSerialInRange(uint64_t serial) const { + bool ok = min_serial_ <= serial && serial <= max_serial_; + assert(!ok || is_mtc_serial_valid(serial)); + return ok; + } + + // Constructs CosignedMessage and computes signature. + bool VerifyCACosignature(Span ca_cosignature, + uint64_t log_number, uint64_t subtree_start, + uint64_t subtree_end, + Span expected_subtree_hash) const { + ScopedCBB cosigned_message; + if (!CBB_init(cosigned_message.get(), 256) || + !CBB_add_bytes(cosigned_message.get(), kCosignedMessageLabel, + sizeof(kCosignedMessageLabel)) || + !WriteCosignerName(cosigned_message.get()) || + !CBB_add_u64(cosigned_message.get(), 0u) || // timestamp + !WriteLogOrigin(cosigned_message.get(), log_number) || + !CBB_add_u64(cosigned_message.get(), subtree_start) || + !CBB_add_u64(cosigned_message.get(), subtree_end) || + !CBB_add_bytes(cosigned_message.get(), expected_subtree_hash.data(), + expected_subtree_hash.size())) { + OPENSSL_PUT_ERROR(X509, ERR_R_INTERNAL_ERROR); + return false; + } + return x509_verify_signature_bytes(cosign_sigalg_.get(), ca_cosignature, + CBBAsSpan(cosigned_message.get()), + cosign_pubkey_.get()); + } + + // IsCACosigner returns whether `trust_anchor_id` equals the cosigner ID + // of this CA's CA cosigner. + bool IsCACosigner(Span trust_anchor_id) const { + return trust_anchor_id == ca_id_enc_oid_; + } + + const EVP_MD *log_hash() const { return EVP_sha256(); } + + private: + // WriteTaiAscii concatenates the trust anchor ID prefix with the CA ID as a + // relative OID to get the CA ID as a full OID, then appends any additional + // OID components from `oid_suffix`, and writes the ASCII string + // representation of the full trust anchor ID to `out`. + bool WriteTaiAscii(CBB *out, Span oid_suffix = {}) const { + // Trust anchor IDs such as the CA ID are limited to 32 bytes by spec, but + // `oid_suffix` may add additional bytes. + uint8_t id[2 * kMaxTrustAnchorIdBytes]; + CBB id_cbb; + if (!CBB_init_fixed(&id_cbb, id, sizeof(id)) || + !CBB_add_bytes(&id_cbb, ca_id_enc_oid_.data(), ca_id_enc_oid_.size())) { + return false; + } + for (const uint64_t component : oid_suffix) { + if (!CBB_add_asn1_oid_component(&id_cbb, component)) { + return false; + } + } + return CBB_add_asn1_relative_oid_from_der_to_text(out, CBB_data(&id_cbb), + CBB_len(&id_cbb)); + } + + // WriteCosignerName writes the `cosigner_name` field for a CosignedMessage (a + // prefixed ASCII representation of the CA ID) to `out`. + bool WriteCosignerName(CBB *out) const { + CBB cosigner_name; + return CBB_add_u8_length_prefixed(out, &cosigner_name) && + CBB_add_bytes(&cosigner_name, kASCIIOIDPrefix, + sizeof(kASCIIOIDPrefix)) && + WriteTaiAscii(&cosigner_name) && // + CBB_flush(out); + } + + // WriteLogOrigin writes the `log_origin` field for a CosignedMessage (a + // prefixed ASCII representation of the CA's log ID for a given `log_number`) + // to `out`. + bool WriteLogOrigin(CBB *out, uint64_t log_number) const { + const uint64_t log_number_relative_oid[2] = {kEncOIDLogs, log_number}; + CBB log_origin; + return CBB_add_u8_length_prefixed(out, &log_origin) && + CBB_add_bytes(&log_origin, kASCIIOIDPrefix, + sizeof(kASCIIOIDPrefix)) && + WriteTaiAscii(&log_origin, Span(log_number_relative_oid)) && + CBB_flush(out); + } + + // The CA ID, which is equal to the CA cosigner's cosigner ID (see sections + // 5.1 and 5.4), in the form of a DER-encoded relative OID. This does not + // include any framing, only the element's contents. + Array ca_id_enc_oid_; + // Public key of the CA cosigner. + UniquePtr cosign_pubkey_; + // Signature algorithm for CA cosigner signatures. + ScopedX509Algor cosign_sigalg_; + // Min and max allowed serial number, both (2^48 .. 2^64-1). + uint64_t min_serial_ = 0u; + uint64_t max_serial_ = 0u; +}; + +} // namespace + +// Only id-pe-mtcCertificationAuthority-SHA256 is supported. +bool x509_is_merkle_tree_ca(const X509 *x509) { + return X509_get_ext_by_NID(x509, NID_pe_mtcCertificationAuthority_SHA256, + -1) >= 0; +} + +bool x509_evaluate_mtc_subtree_inclusion_proof( + Span out, const EVP_MD *log_hash, + Span inclusion_proof, uint64_t index, + Span entry_hash, uint64_t subtree_start, + uint64_t subtree_end) { + const size_t log_hash_size = EVP_MD_size(log_hash); + if (out.size() != log_hash_size || entry_hash.size() != log_hash_size) { + return false; + } + + // Check that `subtree_start` and `subtree_end` define a valid subtree. + if (subtree_start > subtree_end) { + return false; + } + // The subtree must be aligned and not have a ragged left edge, i.e. the size + // must not exceed the largest power of 2 that divides the start index. + const uint64_t subtree_size = subtree_end - subtree_start; + if (subtree_start != 0 && + // The expression `subtree_start & (~subtree_start + 1)` isolates the + // lowest set bit of `subtree_start`. + subtree_size > (subtree_start & (~subtree_start + 1))) { + return false; + } + // Check that `index` is in range for the subtree. + if (index < subtree_start || subtree_end <= index) { + return false; + } + + OPENSSL_memcpy(out.data(), entry_hash.data(), log_hash_size); + + // `fn` is the index of the entry if the subtree were re-numbered to start at + // 0, and `sn` is what the last entry of such a re-numbered subtree would be. + uint64_t fn = index - subtree_start; + uint64_t sn = subtree_size - 1; + while (!inclusion_proof.empty()) { + Span p = + GetNextValueFromInclusionProof(log_hash_size, inclusion_proof); + if (p.empty()) { + // Truncated hash in inclusion proof, or trailing data after last full + // hash. + return false; + } + assert(p.size() == log_hash_size); + if (sn == 0) { + // More hashes in the inclusion proof than expected. + return false; + } + if (lsb(fn) || fn == sn) { + HashNode(out, log_hash, /*left_child=*/p, /*right_child=*/out); + while (!lsb(fn)) { + fn >>= 1; + sn >>= 1; + } + } else { + HashNode(out, log_hash, /*left_child=*/out, /*right_child=*/p); + } + fn >>= 1; + sn >>= 1; + } + + if (sn != 0) { + // Not enough hashes in inclusion proof. + return false; + } + + return true; +} + +int x509_verify_mtc(const X509 *x509, const EVP_PKEY *pkey, + const X509 *issuer) { + MTCCACosigner issuer_mtc_ca; + if (!issuer_mtc_ca.Init(issuer, pkey)) { + return 0; + } + + auto *impl = FromOpaque(x509); + if (!is_mtc_proof(impl->sig_alg.get())) { + OPENSSL_PUT_ERROR(X509, X509_R_UNSUPPORTED_ALGORITHM); + return 0; + } + if (X509_ALGOR_cmp(impl->sig_alg.get(), impl->tbs_sig_alg.get())) { + OPENSSL_PUT_ERROR(X509, X509_R_SIGNATURE_ALGORITHM_MISMATCH); + return 0; + } + + if (impl->signature->type == V_ASN1_BIT_STRING && + ASN1_BIT_STRING_unused_bits(impl->signature.get()) != 0) { + OPENSSL_PUT_ERROR(X509, X509_R_INVALID_BIT_STRING_BITS_LEFT); + return 0; + } + + CBS mtc_proof; + CBS_init(&mtc_proof, ASN1_STRING_get0_data(impl->signature.get()), + ASN1_STRING_length(impl->signature.get())); + + CBS extensions, inclusion_proof, signatures; + uint64_t subtree_start, subtree_end; + if (!CBS_get_u16_length_prefixed(&mtc_proof, &extensions) || + !CBS_get_u48(&mtc_proof, &subtree_start) || + !CBS_get_u48(&mtc_proof, &subtree_end) || + !CBS_get_u16_length_prefixed(&mtc_proof, &inclusion_proof) || + !CBS_get_u24_length_prefixed(&mtc_proof, &signatures) || + CBS_len(&mtc_proof) != 0) { + OPENSSL_PUT_ERROR(ASN1, ASN1_R_DECODE_ERROR); + return 0; + } + if (!CheckMTCLogEntryExtensions(extensions)) { + return 0; + } + + // Iterate through the subtree signatures array to check that they conform to + // spec, and identify the subtree signature from the CA cosigner. + CBS cosigner_id, signature; + std::optional> prev_cosigner_id; + std::optional> ca_cosignature; + while (CBS_len(&signatures) > 0) { + if (!CBS_get_u8_length_prefixed(&signatures, &cosigner_id) || + CBS_len(&cosigner_id) == 0 || + !CBS_get_u16_length_prefixed(&signatures, &signature)) { + OPENSSL_PUT_ERROR(ASN1, ASN1_R_DECODE_ERROR); + return 0; + } + Span cosigner_id_span = cosigner_id; + // Cosignatures must be ordered by increasing cosigner ID length + // (lexicographical order if same length), and must not contain duplicate + // cosigner IDs. + if (prev_cosigner_id.has_value()) { + if (prev_cosigner_id->size() > cosigner_id_span.size()) { + OPENSSL_PUT_ERROR(X509, X509_R_INVALID_MTC_PROOF); + return 0; + } + if (prev_cosigner_id->size() == cosigner_id_span.size() && + !std::lexicographical_compare( + prev_cosigner_id->begin(), prev_cosigner_id->end(), + cosigner_id_span.begin(), cosigner_id_span.end())) { + OPENSSL_PUT_ERROR(X509, X509_R_INVALID_MTC_PROOF); + return 0; + } + } + prev_cosigner_id = cosigner_id_span; + if (issuer_mtc_ca.IsCACosigner(cosigner_id_span)) { + ca_cosignature = signature; + } + } + if (!ca_cosignature.has_value()) { + OPENSSL_PUT_ERROR(X509, X509_R_INVALID_MTC_PROOF); + return 0; + } + + CBS tbs_seq, tbs; + Array tbs_scratch; + if (!x509_get_or_marshal_tbs_cert(&tbs_seq, &tbs_scratch, x509) || + !CBS_get_asn1(&tbs_seq, &tbs, CBS_ASN1_SEQUENCE)) { + OPENSSL_PUT_ERROR(ASN1, ASN1_R_DECODE_ERROR); + return 0; + } + + CBS version_field; + if (CBS_peek_asn1_tag(&tbs, kTBSVersionTag)) { + if (!CBS_get_asn1_element(&tbs, &version_field, kTBSVersionTag)) { + OPENSSL_PUT_ERROR(ASN1, ASN1_R_DECODE_ERROR); + return 0; + } + } else { + CBS_init(&version_field, nullptr, 0); + } + + // Check serial number is well-formed and within CA's range. (Caller is + // responsible for checking serial number against other revoked ranges.) + uint64_t serial; + if (!CBS_get_asn1_uint64(&tbs, &serial)) { + OPENSSL_PUT_ERROR(ASN1, ASN1_R_DECODE_ERROR); + return 0; + } + if (!issuer_mtc_ca.IsSerialInRange(serial)) { + OPENSSL_PUT_ERROR(X509, X509_R_INVALID_PARAMETER); + return 0; + } + // `index` is the least significant 48 bits of `serial`. + const uint64_t index = serial & ((uint64_t{1} << 48) - 1); + const uint64_t log_number = serial >> 48; + assert(is_mtc_serial_valid(serial)); + + // The TBSCertificate's `signature` field must match the expected MTC + // signature algorithm identifier. + CBS tbs_sigalg; + if (!CBS_get_asn1_element(&tbs, &tbs_sigalg, CBS_ASN1_SEQUENCE) || + !CBS_mem_equal(&tbs_sigalg, kMTCAlgorithmIdentifier, + sizeof(kMTCAlgorithmIdentifier))) { + OPENSSL_PUT_ERROR(X509, X509_R_SIGNATURE_ALGORITHM_MISMATCH); + return 0; + } + + // Compute `entry_hash` in a single pass without storing the full + // TBSCertificateLogEntry or MTCLogEntry. + ScopedEVP_MD_CTX entry_hash_ctx; + if (!EVP_DigestInit_ex(entry_hash_ctx.get(), issuer_mtc_ca.log_hash(), + nullptr) || + !EVP_DigestUpdate(entry_hash_ctx.get(), kMTCLeafDomainSeparator, + sizeof(kMTCLeafDomainSeparator)) || + // MTCLogEntry `extensions` field (comes directly from the MTCProof). + !DigestUpdateWithU16LengthPrefix(entry_hash_ctx.get(), extensions) || + // MTCLogEntryType enum value for a tbs_cert_entry. + !EVP_DigestUpdate(entry_hash_ctx.get(), kMTCLogEntryTypeTBSCertEntry, + sizeof(kMTCLogEntryTypeTBSCertEntry)) || + // Contents of TBSCertificateLogEntry follow (excluding the initial + // identifier and length octets): + !EVP_DigestUpdate(entry_hash_ctx.get(), CBS_data(&version_field), + CBS_len(&version_field)) || + !DigestUpdateNextASN1Element(entry_hash_ctx.get(), &tbs) || // issuer + !DigestUpdateNextASN1Element(entry_hash_ctx.get(), &tbs) || // validity + !DigestUpdateNextASN1Element(entry_hash_ctx.get(), &tbs)) { // subject + OPENSSL_PUT_ERROR(X509, ERR_R_INTERNAL_ERROR); + return 0; + } + + // Hash the TBSCertificate's `subjectPublicKeyInfo` field. + CBS spki; + if (!CBS_get_asn1_element(&tbs, &spki, CBS_ASN1_SEQUENCE)) { + OPENSSL_PUT_ERROR(ASN1, ASN1_R_DECODE_ERROR); + return 0; + } + uint8_t spki_digest[EVP_MAX_MD_SIZE]; + unsigned int spki_digest_len; + // The short-form DER length can only encode lengths up to 127 (0x7f). The MTC + // spec permits longer hash lengths up to 255 (0xff), which would require a + // long-form length, but no `EVP_MD` currently outputs more than 127 bytes, so + // reject them here instead of supporting both short and long encoding forms. + if (!EVP_Digest(CBS_data(&spki), CBS_len(&spki), spki_digest, + &spki_digest_len, issuer_mtc_ca.log_hash(), nullptr) || + spki_digest_len == 0 || spki_digest_len > 0x7f) { + OPENSSL_PUT_ERROR(X509, ERR_R_INTERNAL_ERROR); + return 0; + } + + // Add the AlgorithmIdentifier from the subjectPublicKeyInfo. + CBS spki_contents; + if (!CBS_get_asn1(&spki, &spki_contents, CBS_ASN1_SEQUENCE)) { + OPENSSL_PUT_ERROR(ASN1, ASN1_R_DECODE_ERROR); + return 0; + } + if (!DigestUpdateNextASN1Element(entry_hash_ctx.get(), &spki_contents) || + //`subjectPublicKeyInfoHash` field. + !EVP_DigestUpdate(entry_hash_ctx.get(), &kEncodedOctetStringTag, + sizeof(kEncodedOctetStringTag)) || + !DigestUpdateWithU8LengthPrefix(entry_hash_ctx.get(), + Span(spki_digest, spki_digest_len)) || + // Write the remainder of the TBSCertificate contents octets to the hash. + // This includes the optional fields `issuerUniqueID`, `subjectUniqueID`, + // and `extensions`. + !EVP_DigestUpdate(entry_hash_ctx.get(), CBS_data(&tbs), CBS_len(&tbs))) { + OPENSSL_PUT_ERROR(X509, ERR_R_INTERNAL_ERROR); + return 0; + } + + uint8_t entry_hash[EVP_MAX_MD_SIZE]; + unsigned entry_hash_len; + if (!EVP_DigestFinal_ex(entry_hash_ctx.get(), entry_hash, &entry_hash_len)) { + OPENSSL_PUT_ERROR(X509, ERR_R_INTERNAL_ERROR); + return 0; + } + + InplaceVector expected_subtree_hash; + expected_subtree_hash.ResizeForOverwrite( + EVP_MD_size(issuer_mtc_ca.log_hash())); + + if (!x509_evaluate_mtc_subtree_inclusion_proof( + Span(expected_subtree_hash), issuer_mtc_ca.log_hash(), + inclusion_proof, index, Span(entry_hash, entry_hash_len), + subtree_start, subtree_end)) { + OPENSSL_PUT_ERROR(X509, X509_R_INVALID_MTC_PROOF); + return 0; + } + + return issuer_mtc_ca.VerifyCACosignature(ca_cosignature.value(), log_number, + subtree_start, subtree_end, + Span(expected_subtree_hash)); +} + +BSSL_NAMESPACE_END diff --git a/third_party/boringssl/src/crypto/x509/x509_obj.cc b/third_party/boringssl/src/crypto/x509/x509_obj.cc index 667456f75..4997fa71f 100644 --- a/third_party/boringssl/src/crypto/x509/x509_obj.cc +++ b/third_party/boringssl/src/crypto/x509/x509_obj.cc @@ -68,22 +68,23 @@ char *X509_NAME_oneline(const X509_NAME *a, char *buf, int len) { len--; // space for '\0' l = 0; - for (i = 0; i < sk_X509_NAME_ENTRY_num(name->entries); i++) { - ne = sk_X509_NAME_ENTRY_value(name->entries, i); - n = OBJ_obj2nid(ne->object); + for (i = 0; i < sk_X509_NAME_ENTRY_num(name->entries.get()); i++) { + ne = sk_X509_NAME_ENTRY_value(name->entries.get(), i); + n = OBJ_obj2nid(X509_NAME_ENTRY_get_object(ne)); if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == nullptr)) { - i2t_ASN1_OBJECT(tmp_buf, sizeof(tmp_buf), ne->object); + i2t_ASN1_OBJECT(tmp_buf, sizeof(tmp_buf), X509_NAME_ENTRY_get_object(ne)); s = tmp_buf; } l1 = strlen(s); - type = ne->value.type; - num = ne->value.length; + const ASN1_STRING *value = X509_NAME_ENTRY_get_data(ne); + type = value->type; + num = value->length; if (num > NAME_ONELINE_MAX) { OPENSSL_PUT_ERROR(X509, X509_R_NAME_TOO_LONG); goto err; } - q = ne->value.data; + q = value->data; if ((type == V_ASN1_GENERALSTRING) && ((num % 4) == 0)) { gs_doit[0] = gs_doit[1] = gs_doit[2] = gs_doit[3] = 0; @@ -134,7 +135,7 @@ char *X509_NAME_oneline(const X509_NAME *a, char *buf, int len) { p += l1; *(p++) = '='; - q = ne->value.data; + q = value->data; for (j = 0; j < num; j++) { if (!gs_doit[j & 3]) { diff --git a/third_party/boringssl/src/crypto/x509/x509_req.cc b/third_party/boringssl/src/crypto/x509/x509_req.cc index ee7203b37..8ed277f8e 100644 --- a/third_party/boringssl/src/crypto/x509/x509_req.cc +++ b/third_party/boringssl/src/crypto/x509/x509_req.cc @@ -28,6 +28,22 @@ using namespace bssl; +namespace { + +int X509_REQ_add0_attr(X509_REQ *req, X509_ATTRIBUTE *attr) { + if (req->req_info->attributes == nullptr) { + req->req_info->attributes = sk_X509_ATTRIBUTE_new_null(); + } + if (req->req_info->attributes == nullptr || + !sk_X509_ATTRIBUTE_push(req->req_info->attributes, attr)) { + return 0; + } + + return 1; +} + +} // namespace + long X509_REQ_get_version(const X509_REQ *req) { return ASN1_INTEGER_get(req->req_info->version); } @@ -86,7 +102,7 @@ STACK_OF(X509_EXTENSION) *X509_REQ_get_extensions(const X509_REQ *req) { } const X509_ATTRIBUTE *attr = X509_REQ_get_attr(req, idx); - // TODO(davidben): |X509_ATTRIBUTE_get0_type| is not const-correct. It should + // TODO(davidben): `X509_ATTRIBUTE_get0_type` is not const-correct. It should // take and return a const pointer. const ASN1_TYPE *ext = X509_ATTRIBUTE_get0_type((X509_ATTRIBUTE *)attr, 0); if (!ext || ext->type != V_ASN1_SEQUENCE) { @@ -169,18 +185,6 @@ X509_ATTRIBUTE *X509_REQ_delete_attr(X509_REQ *req, int loc) { return sk_X509_ATTRIBUTE_delete(req->req_info->attributes, loc); } -static int X509_REQ_add0_attr(X509_REQ *req, X509_ATTRIBUTE *attr) { - if (req->req_info->attributes == nullptr) { - req->req_info->attributes = sk_X509_ATTRIBUTE_new_null(); - } - if (req->req_info->attributes == nullptr || - !sk_X509_ATTRIBUTE_push(req->req_info->attributes, attr)) { - return 0; - } - - return 1; -} - int X509_REQ_add1_attr(X509_REQ *req, const X509_ATTRIBUTE *attr) { X509_ATTRIBUTE *new_attr = X509_ATTRIBUTE_dup(attr); if (new_attr == nullptr || !X509_REQ_add0_attr(req, new_attr)) { diff --git a/third_party/boringssl/src/crypto/x509/x509_set.cc b/third_party/boringssl/src/crypto/x509/x509_set.cc index 1833804bf..2ede541c5 100644 --- a/third_party/boringssl/src/crypto/x509/x509_set.cc +++ b/third_party/boringssl/src/crypto/x509/x509_set.cc @@ -53,7 +53,7 @@ int X509_set_serialNumber(X509 *x, const ASN1_INTEGER *serial) { } auto *impl = FromOpaque(x); - return ASN1_STRING_copy(&impl->serialNumber, serial); + return ASN1_STRING_copy(impl->serialNumber.get(), serial); } int X509_set_issuer_name(X509 *x, const X509_NAME *name) { @@ -73,9 +73,9 @@ int X509_set_subject_name(X509 *x, const X509_NAME *name) { } int X509_set1_notBefore(X509 *x, const ASN1_TIME *tm) { - // TODO(crbug.com/42290309): Check that |tm->type| is correct. + // TODO(crbug.com/42290309): Check that `tm->type` is correct. auto *impl = FromOpaque(x); - return ASN1_STRING_copy(&impl->notBefore, tm); + return ASN1_STRING_copy(impl->notBefore.get(), tm); } int X509_set_notBefore(X509 *x, const ASN1_TIME *tm) { @@ -84,29 +84,29 @@ int X509_set_notBefore(X509 *x, const ASN1_TIME *tm) { const ASN1_TIME *X509_get0_notBefore(const X509 *x) { auto *impl = FromOpaque(x); - return &impl->notBefore; + return impl->notBefore.get(); } ASN1_TIME *X509_getm_notBefore(X509 *x) { - // Note this function takes a const |X509| pointer in OpenSSL. We require - // non-const as this allows mutating |x|. If it comes up for compatibility, + // Note this function takes a const `X509` pointer in OpenSSL. We require + // non-const as this allows mutating `x`. If it comes up for compatibility, // we can relax this. auto *impl = FromOpaque(x); - return &impl->notBefore; + return impl->notBefore.get(); } ASN1_TIME *X509_get_notBefore(const X509 *x509) { - // In OpenSSL, this function is an alias for |X509_getm_notBefore|, but our - // |X509_getm_notBefore| is const-correct. |X509_get_notBefore| was + // In OpenSSL, this function is an alias for `X509_getm_notBefore`, but our + // `X509_getm_notBefore` is const-correct. `X509_get_notBefore` was // originally a macro, so it needs to capture both get0 and getm use cases. const auto *impl = FromOpaque(x509); - return const_cast(&impl->notBefore); + return const_cast(impl->notBefore.get()); } int X509_set1_notAfter(X509 *x, const ASN1_TIME *tm) { - // TODO(crbug.com/42290309): Check that |tm->type| is correct. + // TODO(crbug.com/42290309): Check that `tm->type` is correct. auto *impl = FromOpaque(x); - return ASN1_STRING_copy(&impl->notAfter, tm); + return ASN1_STRING_copy(impl->notAfter.get(), tm); } int X509_set_notAfter(X509 *x, const ASN1_TIME *tm) { @@ -114,38 +114,36 @@ int X509_set_notAfter(X509 *x, const ASN1_TIME *tm) { } const ASN1_TIME *X509_get0_notAfter(const X509 *x) { - auto *impl = FromOpaque(x); - return &impl->notAfter; + return FromOpaque(x)->notAfter.get(); } ASN1_TIME *X509_getm_notAfter(X509 *x) { - // Note this function takes a const |X509| pointer in OpenSSL. We require - // non-const as this allows mutating |x|. If it comes up for compatibility, + // Note this function takes a const `X509` pointer in OpenSSL. We require + // non-const as this allows mutating `x`. If it comes up for compatibility, // we can relax this. - auto *impl = FromOpaque(x); - return &impl->notAfter; + return FromOpaque(x)->notAfter.get(); } ASN1_TIME *X509_get_notAfter(const X509 *x509) { - // In OpenSSL, this function is an alias for |X509_getm_notAfter|, but our - // |X509_getm_notAfter| is const-correct. |X509_get_notAfter| was + // In OpenSSL, this function is an alias for `X509_getm_notAfter`, but our + // `X509_getm_notAfter` is const-correct. `X509_get_notAfter` was // originally a macro, so it needs to capture both get0 and getm use cases. const auto *impl = FromOpaque(x509); - return const_cast(&impl->notAfter); + return const_cast(impl->notAfter.get()); } void X509_get0_uids(const X509 *x509, const ASN1_BIT_STRING **out_issuer_uid, const ASN1_BIT_STRING **out_subject_uid) { const auto *impl = FromOpaque(x509); if (out_issuer_uid != nullptr) { - *out_issuer_uid = impl->issuerUID; + *out_issuer_uid = impl->issuerUID.get(); } if (out_subject_uid != nullptr) { - *out_subject_uid = impl->subjectUID; + *out_subject_uid = impl->subjectUID.get(); } } -int X509_set_pubkey(X509 *x, EVP_PKEY *pkey) { +int X509_set_pubkey(X509 *x, const EVP_PKEY *pkey) { if (x == nullptr) { return 0; } @@ -154,17 +152,15 @@ int X509_set_pubkey(X509 *x, EVP_PKEY *pkey) { } const STACK_OF(X509_EXTENSION) *X509_get0_extensions(const X509 *x) { - auto *impl = FromOpaque(x); - return impl->extensions; + return FromOpaque(x)->extensions; } const X509_ALGOR *X509_get0_tbs_sigalg(const X509 *x) { - auto *impl = FromOpaque(x); - return &impl->tbs_sig_alg; + return FromOpaque(x)->tbs_sig_alg.get(); } X509_PUBKEY *X509_get_X509_PUBKEY(const X509 *x509) { // This function is not const-correct for OpenSSL compatibility. const auto *impl = FromOpaque(x509); - return const_cast(&impl->key); + return const_cast(&impl->key); } diff --git a/third_party/boringssl/src/crypto/x509/x509_trs.cc b/third_party/boringssl/src/crypto/x509/x509_trs.cc index f5589927f..7990ecdf2 100644 --- a/third_party/boringssl/src/crypto/x509/x509_trs.cc +++ b/third_party/boringssl/src/crypto/x509/x509_trs.cc @@ -23,20 +23,60 @@ using namespace bssl; +namespace { + typedef struct x509_trust_st X509_TRUST; struct x509_trust_st { int trust; - int (*check_trust)(const X509_TRUST *, X509 *); + int (*check_trust)(const X509_TRUST *, const X509 *); int nid; } /* X509_TRUST */; -static int trust_1oidany(const X509_TRUST *trust, X509 *x); -static int trust_compat(const X509_TRUST *trust, X509 *x); +int obj_trust(int id, const X509 *x) { + auto *impl = FromOpaque(x); + X509_CERT_AUX *ax = impl->aux; + if (!ax) { + return X509_TRUST_UNTRUSTED; + } + for (size_t i = 0; i < sk_ASN1_OBJECT_num(ax->reject); i++) { + const ASN1_OBJECT *obj = sk_ASN1_OBJECT_value(ax->reject, i); + if (OBJ_obj2nid(obj) == id) { + return X509_TRUST_REJECTED; + } + } + for (size_t i = 0; i < sk_ASN1_OBJECT_num(ax->trust); i++) { + const ASN1_OBJECT *obj = sk_ASN1_OBJECT_value(ax->trust, i); + if (OBJ_obj2nid(obj) == id) { + return X509_TRUST_TRUSTED; + } + } + return X509_TRUST_UNTRUSTED; +} + +int trust_compat(const X509_TRUST *trust, const X509 *x) { + auto *impl = FromOpaque(x); + if (!x509v3_cache_extensions(x)) { + return X509_TRUST_UNTRUSTED; + } + if (impl->ex_flags & EXFLAG_SS) { + return X509_TRUST_TRUSTED; + } else { + return X509_TRUST_UNTRUSTED; + } +} -static int obj_trust(int id, X509 *x); +int trust_1oidany(const X509_TRUST *trust, const X509 *x) { + auto *impl = FromOpaque(x); + if (impl->aux && (impl->aux->trust || impl->aux->reject)) { + return obj_trust(trust->nid, impl); + } + // we don't have any trust settings: for compatibility we return trusted + // if it is self signed + return trust_compat(trust, impl); +} -static const X509_TRUST trstandard[] = { +const X509_TRUST trstandard[] = { {X509_TRUST_COMPAT, trust_compat, 0}, {X509_TRUST_SSL_CLIENT, trust_1oidany, NID_client_auth}, {X509_TRUST_SSL_SERVER, trust_1oidany, NID_server_auth}, @@ -44,7 +84,7 @@ static const X509_TRUST trstandard[] = { {X509_TRUST_OBJECT_SIGN, trust_1oidany, NID_code_sign}, {X509_TRUST_TSA, trust_1oidany, NID_time_stamp}}; -static const X509_TRUST *X509_TRUST_get0(int id) { +const X509_TRUST *X509_TRUST_get0(int id) { for (const auto &t : trstandard) { if (t.trust == id) { return &t; @@ -53,7 +93,9 @@ static const X509_TRUST *X509_TRUST_get0(int id) { return nullptr; } -int X509_check_trust(X509 *x, int id, int flags) { +} // namespace + +int X509_check_trust(const X509 *x, int id, int flags) { if (id == -1) { return X509_TRUST_TRUSTED; } @@ -78,46 +120,3 @@ int X509_check_trust(X509 *x, int id, int flags) { int bssl::X509_is_valid_trust_id(int trust) { return X509_TRUST_get0(trust) != nullptr; } - -static int trust_1oidany(const X509_TRUST *trust, X509 *x) { - auto *impl = FromOpaque(x); - if (impl->aux && (impl->aux->trust || impl->aux->reject)) { - return obj_trust(trust->nid, impl); - } - // we don't have any trust settings: for compatibility we return trusted - // if it is self signed - return trust_compat(trust, impl); -} - -static int trust_compat(const X509_TRUST *trust, X509 *x) { - auto *impl = FromOpaque(x); - if (!x509v3_cache_extensions(x)) { - return X509_TRUST_UNTRUSTED; - } - if (impl->ex_flags & EXFLAG_SS) { - return X509_TRUST_TRUSTED; - } else { - return X509_TRUST_UNTRUSTED; - } -} - -static int obj_trust(int id, X509 *x) { - auto *impl = FromOpaque(x); - X509_CERT_AUX *ax = impl->aux; - if (!ax) { - return X509_TRUST_UNTRUSTED; - } - for (size_t i = 0; i < sk_ASN1_OBJECT_num(ax->reject); i++) { - const ASN1_OBJECT *obj = sk_ASN1_OBJECT_value(ax->reject, i); - if (OBJ_obj2nid(obj) == id) { - return X509_TRUST_REJECTED; - } - } - for (size_t i = 0; i < sk_ASN1_OBJECT_num(ax->trust); i++) { - const ASN1_OBJECT *obj = sk_ASN1_OBJECT_value(ax->trust, i); - if (OBJ_obj2nid(obj) == id) { - return X509_TRUST_TRUSTED; - } - } - return X509_TRUST_UNTRUSTED; -} diff --git a/third_party/boringssl/src/crypto/x509/x509_vfy.cc b/third_party/boringssl/src/crypto/x509/x509_vfy.cc index a619d496c..bae0df672 100644 --- a/third_party/boringssl/src/crypto/x509/x509_vfy.cc +++ b/third_party/boringssl/src/crypto/x509/x509_vfy.cc @@ -12,7 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include +#include + #include #include #include @@ -22,6 +23,7 @@ #include #include #include +#include #include #include "../internal.h" @@ -31,8 +33,6 @@ using namespace bssl; -static ExDataClass g_ex_data_class(/*with_app_data=*/true); - // CRL score values // No unhandled critical extensions @@ -60,34 +60,22 @@ static ExDataClass g_ex_data_class(/*with_app_data=*/true); // CRL issuer matches CRL AKID #define CRL_SCORE_AKID 0x004 -static int null_callback(int ok, X509_STORE_CTX *e); -static X509 *find_issuer(X509_STORE_CTX *ctx, STACK_OF(X509) *sk, X509 *x); -static int check_chain_extensions(X509_STORE_CTX *ctx); -static int check_name_constraints(X509_STORE_CTX *ctx); -static int check_id(X509_STORE_CTX *ctx); -static int check_trust(X509_STORE_CTX *ctx); -static int check_revocation(X509_STORE_CTX *ctx); -static int check_cert(X509_STORE_CTX *ctx); -static int check_policy(X509_STORE_CTX *ctx); - -static X509 *get_trusted_issuer(X509_STORE_CTX *ctx, X509 *x); -static int get_crl_score(X509_STORE_CTX *ctx, X509 **pissuer, X509_CRL *crl, - X509 *x); -static int get_crl(X509_STORE_CTX *ctx, X509_CRL **pcrl, X509 *x); -static int crl_akid_check(X509_STORE_CTX *ctx, X509_CRL *crl, X509 **pissuer, - int *pcrl_score); -static int crl_crldp_check(X509 *x, X509_CRL *crl, int crl_score); -static int check_crl(X509_STORE_CTX *ctx, X509_CRL *crl); -static int cert_crl(X509_STORE_CTX *ctx, X509_CRL *crl, X509 *x); - -static int internal_verify(X509_STORE_CTX *ctx); - -static int null_callback(int ok, X509_STORE_CTX *e) { return ok; } - -// cert_self_signed checks if |x| is self-signed. If |x| is valid, it returns -// one and sets |*out_is_self_signed| to the result. If |x| is invalid, it +namespace { + +ExDataClass g_ex_data_class(/*with_app_data=*/true); + +struct CRLInfo { + UniquePtr crl; + X509 *issuer = nullptr; + int score = 0; +}; + +int null_callback(int ok, X509_STORE_CTX *e) { return ok; } + +// cert_self_signed checks if `x` is self-signed. If `x` is valid, it returns +// one and sets `*out_is_self_signed` to the result. If `x` is invalid, it // returns zero. -static int cert_self_signed(X509 *x, int *out_is_self_signed) { +int cert_self_signed(X509 *x, int *out_is_self_signed) { if (!x509v3_cache_extensions(x)) { return 0; } @@ -96,328 +84,69 @@ static int cert_self_signed(X509 *x, int *out_is_self_signed) { return 1; } -static int call_verify_cb(int ok, X509_STORE_CTX *ctx) { +int call_verify_cb(int ok, X509_STORE_CTX *ctx) { ok = ctx->verify_cb(ok, ctx); // Historically, callbacks returning values like -1 would be treated as a mix // of success or failure. Insert that callers check correctly. // // TODO(davidben): Also use this wrapper to constrain which errors may be - // suppressed, and ensure all |verify_cb| calls remember to fill in an error. + // suppressed, and ensure all `verify_cb` calls remember to fill in an error. BSSL_CHECK(ok == 0 || ok == 1); return ok; } -// Given a certificate try and find an exact match in the store -static X509 *lookup_cert_match(X509_STORE_CTX *ctx, X509 *x) { - STACK_OF(X509) *certs; - X509 *xtmp = nullptr; - size_t i; - // Lookup all certs with matching subject name - certs = X509_STORE_CTX_get1_certs(ctx, X509_get_subject_name(x)); - if (certs == nullptr) { - return nullptr; - } - // Look for exact match - for (i = 0; i < sk_X509_num(certs); i++) { - xtmp = sk_X509_value(certs, i); - if (!X509_cmp(xtmp, x)) { - break; - } - } - if (i < sk_X509_num(certs)) { - X509_up_ref(xtmp); - } else { - xtmp = nullptr; - } - sk_X509_pop_free(certs, X509_free); - return xtmp; +// TODO(crbug.com/42290549): Switch remaining `call_verify_cb` calls to this +// function. +int call_verify_cb_at_depth(int ok, X509_STORE_CTX *ctx, int depth) { + ctx->current_cert = sk_X509_value(ctx->chain, depth); + ctx->error_depth = depth; + return call_verify_cb(ok, ctx); } -int X509_verify_cert(X509_STORE_CTX *ctx) { - X509 *chain_ss = nullptr; - int bad_chain = 0; - X509_VERIFY_PARAM *param = ctx->param; - int i, ok = 0; - int j, retry, trust; - STACK_OF(X509) *sktmp = nullptr; - - { - if (ctx->cert == nullptr) { - OPENSSL_PUT_ERROR(X509, X509_R_NO_CERT_SET_FOR_US_TO_VERIFY); - ctx->error = X509_V_ERR_INVALID_CALL; - return 0; - } - - if (ctx->chain != nullptr) { - // This X509_STORE_CTX has already been used to verify a cert. We - // cannot do another one. - OPENSSL_PUT_ERROR(X509, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); - ctx->error = X509_V_ERR_INVALID_CALL; - return 0; - } - - if (ctx->param->flags & - (X509_V_FLAG_EXTENDED_CRL_SUPPORT | X509_V_FLAG_USE_DELTAS)) { - // We do not support indirect or delta CRLs. The flags still exist for - // compatibility with bindings libraries, but to ensure we do not - // inadvertently skip a CRL check that the caller expects, fail closed. - OPENSSL_PUT_ERROR(X509, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); - ctx->error = X509_V_ERR_INVALID_CALL; - return 0; - } - - // first we make sure the chain we are going to build is present and that - // the first entry is in place - ctx->chain = sk_X509_new_null(); - if (ctx->chain == nullptr || !sk_X509_push(ctx->chain, ctx->cert)) { - ctx->error = X509_V_ERR_OUT_OF_MEM; - goto end; - } - X509_up_ref(ctx->cert); - ctx->last_untrusted = 1; - - // We use a temporary STACK so we can chop and hack at it. - if (ctx->untrusted != nullptr && - (sktmp = sk_X509_dup(ctx->untrusted)) == nullptr) { - ctx->error = X509_V_ERR_OUT_OF_MEM; - goto end; - } - - int num = (int)sk_X509_num(ctx->chain); - X509 *x = sk_X509_value(ctx->chain, num - 1); - // |param->depth| does not include the leaf certificate or the trust anchor, - // so the maximum size is 2 more. - int max_chain = param->depth >= INT_MAX - 2 ? INT_MAX : param->depth + 2; - - for (;;) { - if (num >= max_chain) { - // FIXME: If this happens, we should take note of it and, if - // appropriate, use the X509_V_ERR_CERT_CHAIN_TOO_LONG error code later. - break; - } - - int is_self_signed; - if (!cert_self_signed(x, &is_self_signed)) { - ctx->error = X509_V_ERR_INVALID_EXTENSION; - goto end; - } - - // If we are self signed, we break - if (is_self_signed) { - break; - } - // If asked see if we can find issuer in trusted store first - if (ctx->param->flags & X509_V_FLAG_TRUSTED_FIRST) { - X509 *issuer = get_trusted_issuer(ctx, x); - if (issuer != nullptr) { - // Free the certificate. It will be picked up again later. - X509_free(issuer); - break; - } - } - - // If we were passed a cert chain, use it first - if (sktmp != nullptr) { - X509 *issuer = find_issuer(ctx, sktmp, x); - if (issuer != nullptr) { - if (!sk_X509_push(ctx->chain, issuer)) { - ctx->error = X509_V_ERR_OUT_OF_MEM; - goto end; - } - X509_up_ref(issuer); - (void)sk_X509_delete_ptr(sktmp, issuer); - ctx->last_untrusted++; - x = issuer; - num++; - // reparse the full chain for the next one - continue; - } - } - break; - } - - // Remember how many untrusted certs we have - j = num; - // at this point, chain should contain a list of untrusted certificates. - // We now need to add at least one trusted one, if possible, otherwise we - // complain. - - do { - // Examine last certificate in chain and see if it is self signed. - i = (int)sk_X509_num(ctx->chain); - x = sk_X509_value(ctx->chain, i - 1); - - int is_self_signed; - if (!cert_self_signed(x, &is_self_signed)) { - ctx->error = X509_V_ERR_INVALID_EXTENSION; - goto end; - } - - if (is_self_signed) { - // we have a self signed certificate - if (sk_X509_num(ctx->chain) == 1) { - // We have a single self signed certificate: see if we can - // find it in the store. We must have an exact match to avoid - // possible impersonation. - X509 *issuer = get_trusted_issuer(ctx, x); - if (issuer == nullptr || X509_cmp(x, issuer) != 0) { - X509_free(issuer); - ctx->error = X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT; - ctx->current_cert = x; - ctx->error_depth = i - 1; - bad_chain = 1; - if (!call_verify_cb(0, ctx)) { - goto end; - } - } else { - // We have a match: replace certificate with store - // version so we get any trust settings. - X509_free(x); - x = issuer; - (void)sk_X509_set(ctx->chain, i - 1, x); - ctx->last_untrusted = 0; - } - } else { - // extract and save self signed certificate for later use - chain_ss = sk_X509_pop(ctx->chain); - ctx->last_untrusted--; - num--; - j--; - x = sk_X509_value(ctx->chain, num - 1); - } - } - // We now lookup certs from the certificate store - for (;;) { - if (num >= max_chain) { - // FIXME: If this happens, we should take note of it and, if - // appropriate, use the X509_V_ERR_CERT_CHAIN_TOO_LONG error code - // later. - break; - } - if (!cert_self_signed(x, &is_self_signed)) { - ctx->error = X509_V_ERR_INVALID_EXTENSION; - goto end; - } - // If we are self signed, we break - if (is_self_signed) { - break; - } - X509 *issuer = get_trusted_issuer(ctx, x); - if (issuer == nullptr) { - break; - } - x = issuer; - if (!sk_X509_push(ctx->chain, x)) { - X509_free(issuer); - ctx->error = X509_V_ERR_OUT_OF_MEM; - goto end; - } - num++; - } - - // we now have our chain, lets check it... - trust = check_trust(ctx); - - // If explicitly rejected error - if (trust == X509_TRUST_REJECTED) { - goto end; - } - // If it's not explicitly trusted then check if there is an alternative - // chain that could be used. We only do this if we haven't already - // checked via TRUSTED_FIRST and the user hasn't switched off alternate - // chain checking - retry = 0; - if (trust != X509_TRUST_TRUSTED && - !(ctx->param->flags & X509_V_FLAG_TRUSTED_FIRST) && - !(ctx->param->flags & X509_V_FLAG_NO_ALT_CHAINS)) { - while (j-- > 1) { - X509 *issuer = - get_trusted_issuer(ctx, sk_X509_value(ctx->chain, j - 1)); - // Check if we found an alternate chain - if (issuer != nullptr) { - // Free up the found cert we'll add it again later - X509_free(issuer); - - // Dump all the certs above this point - we've found an - // alternate chain - while (num > j) { - X509_free(sk_X509_pop(ctx->chain)); - num--; - } - ctx->last_untrusted = (int)sk_X509_num(ctx->chain); - retry = 1; - break; - } - } - } - } while (retry); - - // If not explicitly trusted then indicate error unless it's a single - // self signed certificate in which case we've indicated an error already - // and set bad_chain == 1 - if (trust != X509_TRUST_TRUSTED && !bad_chain) { - if (chain_ss == nullptr || - !x509_check_issued_with_callback(ctx, x, chain_ss)) { - if (ctx->last_untrusted >= num) { - ctx->error = X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY; - } else { - ctx->error = X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT; - } - ctx->current_cert = x; - } else { - if (!sk_X509_push(ctx->chain, chain_ss)) { - ctx->error = X509_V_ERR_OUT_OF_MEM; - goto end; - } - num++; - ctx->last_untrusted = num; - ctx->current_cert = chain_ss; - ctx->error = X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN; - chain_ss = nullptr; - } +static bool sig_nid_allowed(const X509_STORE_CTX *ctx, int sig_nid) { + // TODO(crbug.com/579959247): This check duplicates some logic from + // `x509_digest_verify_init` and makes assumptions about what algorithms it + // supports. Unify this. + int digest_nid, pkey_nid; + if (OBJ_find_sigid_algs(sig_nid, &digest_nid, &pkey_nid) && + digest_nid == NID_sha1 && + (ctx->param->flags & X509_V_FLAG_ALLOW_SHA1) == 0) { + return false; + } + return true; +} - ctx->error_depth = num - 1; - bad_chain = 1; - if (!call_verify_cb(0, ctx)) { - goto end; +// Given a certificate try and find an exact match in the store +UniquePtr lookup_cert_match(X509_STORE_CTX *ctx, const X509 *x) { + if (ctx->trusted_stack != nullptr) { + // Ignore the store and use the configured stack instead. + for (X509 *found : ctx->trusted_stack) { + if (X509_cmp(found, x) == 0) { + return UpRef(found); } } - - // We have the chain complete: now we need to check its purpose - if (!check_chain_extensions(ctx) || // - !check_id(ctx) || - // We check revocation status after copying parameters because they may - // be needed for CRL signature verification. - !check_revocation(ctx) || // - !internal_verify(ctx) || // - !check_name_constraints(ctx) || - // TODO(davidben): Does |check_policy| still need to be conditioned on - // |!bad_chain|? DoS concerns have been resolved. - (!bad_chain && !check_policy(ctx))) { - goto end; - } - - ok = 1; + return nullptr; } -end: - sk_X509_free(sktmp); - X509_free(chain_ss); - - // Safety net, error returns must set ctx->error - if (!ok && ctx->error == X509_V_OK) { - ctx->error = X509_V_ERR_UNSPECIFIED; + // Lookup all certs with matching subject name. + UniquePtr certs( + X509_STORE_CTX_get1_certs(ctx, X509_get_subject_name(x))); + if (certs == nullptr) { + return nullptr; } - return ok; + // Look for exact match + for (X509 *found : certs.get()) { + if (X509_cmp(found, x) == 0) { + return UpRef(found); + } + } + return nullptr; } // Given a STACK_OF(X509) find the issuer of cert (if any) - -static X509 *find_issuer(X509_STORE_CTX *ctx, STACK_OF(X509) *sk, X509 *x) { - size_t i; - X509 *issuer; - for (i = 0; i < sk_X509_num(sk); i++) { - issuer = sk_X509_value(sk, i); +X509 *find_issuer(X509_STORE_CTX *ctx, const STACK_OF(X509) *sk, + const X509 *x) { + for (X509 *issuer : sk) { if (x509_check_issued_with_callback(ctx, x, issuer)) { return issuer; } @@ -425,58 +154,32 @@ static X509 *find_issuer(X509_STORE_CTX *ctx, STACK_OF(X509) *sk, X509 *x) { return nullptr; } -// Given a possible certificate and issuer check them - -int bssl::x509_check_issued_with_callback(X509_STORE_CTX *ctx, const X509 *x, - const X509 *issuer) { - int ret; - ret = X509_check_issued(issuer, x); - if (ret == X509_V_OK) { - return 1; - } - // If we haven't asked for issuer errors don't set ctx - if (!(ctx->param->flags & X509_V_FLAG_CB_ISSUER_CHECK)) { - return 0; - } - - ctx->error = ret; - ctx->current_cert = const_cast(x); - return call_verify_cb(0, ctx); -} - -static X509 *get_trusted_issuer(X509_STORE_CTX *ctx, X509 *x) { - X509 *issuer; +UniquePtr get_trusted_issuer(X509_STORE_CTX *ctx, const X509 *x) { if (ctx->trusted_stack != nullptr) { // Ignore the store and use the configured stack instead. - issuer = find_issuer(ctx, ctx->trusted_stack, x); - if (issuer != nullptr) { - X509_up_ref(issuer); - } - return issuer; + return UpRef(find_issuer(ctx, ctx->trusted_stack, x)); } + X509 *issuer = nullptr; if (!X509_STORE_CTX_get1_issuer(&issuer, ctx, x)) { return nullptr; } - return issuer; + // `X509_STORE_CTX_get1_issuer` passes ownership of `issuer`. + return UniquePtr(issuer); } // Check a certificate chains extensions for consistency with the supplied // purpose - -static int check_chain_extensions(X509_STORE_CTX *ctx) { +int check_chain_extensions(X509_STORE_CTX *ctx) { int plen = 0; int purpose = ctx->param->purpose; - - // Check all untrusted certificates - for (int i = 0; i < ctx->last_untrusted; i++) { + int num = static_cast(sk_X509_num(ctx->chain)); + for (int i = 0; i < num; i++) { X509Impl *x = FromOpaque(sk_X509_value(ctx->chain, i)); if (!(ctx->param->flags & X509_V_FLAG_IGNORE_CRITICAL) && (x->ex_flags & EXFLAG_CRITICAL)) { ctx->error = X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION; - ctx->error_depth = i; - ctx->current_cert = x; - if (!call_verify_cb(0, ctx)) { + if (!call_verify_cb_at_depth(0, ctx, i)) { return 0; } } @@ -484,33 +187,37 @@ static int check_chain_extensions(X509_STORE_CTX *ctx) { int must_be_ca = i > 0; if (must_be_ca && !X509_check_ca(x)) { ctx->error = X509_V_ERR_INVALID_CA; - ctx->error_depth = i; - ctx->current_cert = x; - if (!call_verify_cb(0, ctx)) { + if (!call_verify_cb_at_depth(0, ctx, i)) { return 0; } } if (ctx->param->purpose > 0 && X509_check_purpose(x, purpose, must_be_ca) != 1) { ctx->error = X509_V_ERR_INVALID_PURPOSE; - ctx->error_depth = i; - ctx->current_cert = x; - if (!call_verify_cb(0, ctx)) { + if (!call_verify_cb_at_depth(0, ctx, i)) { return 0; } } - // Check pathlen if not self issued - if (i > 1 && !(x->ex_flags & EXFLAG_SI) && x->ex_pathlen != -1 && - plen > x->ex_pathlen + 1) { + // Check path length constraints. See steps (l) and (m) of RFC 5280, + // section 6.1.4. Note the spec is structured differently from this + // logic. Section 6.1.4 runs from root to leaf and does not run on + // the leaf. `plen` counts the number of times step (l) would have + // run. The constraint is violated if some `x->ex_pathlen`, read in + // step (m), is too low to be decremented `plen` times. + // + // Note that path lengths of self-issued certificates still have to be + // considered - they are just not counted as part of the path length! + if (i > 1 && x->ex_pathlen != -1 && plen > x->ex_pathlen + 1) { ctx->error = X509_V_ERR_PATH_LENGTH_EXCEEDED; - ctx->error_depth = i; - ctx->current_cert = x; - if (!call_verify_cb(0, ctx)) { + if (!call_verify_cb_at_depth(0, ctx, i)) { return 0; } } - // Increment path length if not self issued - if (!(x->ex_flags & EXFLAG_SI)) { + // Increment path length if not self issued. As only self-issued + // _intermediates_ are skipped in (l) of RFC 5280 (simply because it + // operates on certificate chain _edges_), always increment for the first + // (the leaf) in the chain. + if (i == 0 || !(x->ex_flags & EXFLAG_SI)) { plen++; } } @@ -518,7 +225,7 @@ static int check_chain_extensions(X509_STORE_CTX *ctx) { return 1; } -static int reject_dns_name_in_common_name(X509 *x509) { +int reject_dns_name_in_common_name(X509 *x509) { const X509_NAME *name = X509_get_subject_name(x509); int i = -1; for (;;) { @@ -535,7 +242,7 @@ static int reject_dns_name_in_common_name(X509 *x509) { return X509_V_ERR_OUT_OF_MEM; } // Only process attributes that look like host names. Note it is - // important that this check be mirrored in |X509_check_host|. + // important that this check be mirrored in `X509_check_host`. int looks_like_dns = x509v3_looks_like_dns_name(idval, (size_t)idlen); OPENSSL_free(idval); if (looks_like_dns) { @@ -544,7 +251,7 @@ static int reject_dns_name_in_common_name(X509 *x509) { } } -static int check_name_constraints(X509_STORE_CTX *ctx) { +int check_name_constraints(X509_STORE_CTX *ctx) { int i, j, rv; int has_name_constraints = 0; // Check name constraints for all certificates @@ -559,7 +266,7 @@ static int check_name_constraints(X509_STORE_CTX *ctx) { // but if it includes constraints it is to be assumed it expects them // to be obeyed. for (j = (int)sk_X509_num(ctx->chain) - 1; j > i; j--) { - NAME_CONSTRAINTS *nc = FromOpaque(sk_X509_value(ctx->chain, j))->nc; + NAME_CONSTRAINTS *nc = FromOpaque(sk_X509_value(ctx->chain, j))->nc.get(); if (nc) { has_name_constraints = 1; rv = NAME_CONSTRAINTS_check(x, nc); @@ -571,9 +278,7 @@ static int check_name_constraints(X509_STORE_CTX *ctx) { return 0; default: ctx->error = rv; - ctx->error_depth = i; - ctx->current_cert = x; - if (!call_verify_cb(0, ctx)) { + if (!call_verify_cb_at_depth(0, ctx, i)) { return 0; } break; @@ -583,7 +288,7 @@ static int check_name_constraints(X509_STORE_CTX *ctx) { } // Name constraints do not match against the common name, but - // |X509_check_host| still implements the legacy behavior where, on + // `X509_check_host` still implements the legacy behavior where, on // certificates lacking a SAN list, DNS-like names in the common name are // checked instead. // @@ -603,9 +308,7 @@ static int check_name_constraints(X509_STORE_CTX *ctx) { return 0; default: ctx->error = rv; - ctx->error_depth = i; - ctx->current_cert = leaf; - if (!call_verify_cb(0, ctx)) { + if (!call_verify_cb_at_depth(0, ctx, /*depth=*/0)) { return 0; } break; @@ -615,14 +318,12 @@ static int check_name_constraints(X509_STORE_CTX *ctx) { return 1; } -static int check_id_error(X509_STORE_CTX *ctx, int errcode) { +int check_id_error(X509_STORE_CTX *ctx, int errcode) { ctx->error = errcode; - ctx->current_cert = ctx->cert; - ctx->error_depth = 0; - return call_verify_cb(0, ctx); + return call_verify_cb_at_depth(0, ctx, /*depth=*/0); } -static int check_hosts(X509 *x, X509_VERIFY_PARAM *param) { +int check_hosts(X509 *x, X509_VERIFY_PARAM *param) { size_t i; size_t n = sk_OPENSSL_STRING_num(param->hosts); char *name; @@ -636,7 +337,7 @@ static int check_hosts(X509 *x, X509_VERIFY_PARAM *param) { return n == 0; } -static int check_id(X509_STORE_CTX *ctx) { +int check_id(X509_STORE_CTX *ctx) { X509_VERIFY_PARAM *vpm = ctx->param; X509 *x = ctx->cert; if (vpm->poison) { @@ -662,112 +363,39 @@ static int check_id(X509_STORE_CTX *ctx) { return 1; } -static int check_trust(X509_STORE_CTX *ctx) { - X509 *x = nullptr; - // Check all trusted certificates in chain - for (size_t i = ctx->last_untrusted; i < sk_X509_num(ctx->chain); i++) { - x = sk_X509_value(ctx->chain, i); - int trust = X509_check_trust(x, ctx->param->trust, 0); - // If explicitly trusted return trusted - if (trust == X509_TRUST_TRUSTED) { - return X509_TRUST_TRUSTED; - } - // If explicitly rejected notify callback and reject if not - // overridden. - if (trust == X509_TRUST_REJECTED) { - ctx->error_depth = (int)i; - ctx->current_cert = x; - ctx->error = X509_V_ERR_CERT_REJECTED; - if (!call_verify_cb(0, ctx)) { - return X509_TRUST_REJECTED; - } - } - } - // If we accept partial chains and have at least one trusted certificate - // return success. - if (ctx->param->flags & X509_V_FLAG_PARTIAL_CHAIN) { - X509 *mx; - if (ctx->last_untrusted < (int)sk_X509_num(ctx->chain)) { - return X509_TRUST_TRUSTED; - } - x = sk_X509_value(ctx->chain, 0); - mx = lookup_cert_match(ctx, x); - if (mx) { - (void)sk_X509_set(ctx->chain, 0, mx); - X509_free(x); - ctx->last_untrusted = 0; - return X509_TRUST_TRUSTED; - } - } - - // If no trusted certs in chain at all return untrusted and allow - // standard (no issuer cert) etc errors to be indicated. - return X509_TRUST_UNTRUSTED; -} - -static int check_revocation(X509_STORE_CTX *ctx) { - if (!(ctx->param->flags & X509_V_FLAG_CRL_CHECK)) { - return 1; - } - int last; - if (ctx->param->flags & X509_V_FLAG_CRL_CHECK_ALL) { - last = (int)sk_X509_num(ctx->chain) - 1; - } else { - last = 0; +int check_trust(X509_STORE_CTX *ctx) { + // If this function is called, `ctx->chain` must end with some certificate + // from the store. Determine if we should treat it as a trust anchor in this + // context. + X509 *last = sk_X509_last(ctx->chain); + int trust = X509_check_trust(last, ctx->param->trust, /*flags=*/0); + if (trust == X509_TRUST_TRUSTED) { + return X509_TRUST_TRUSTED; } - for (int i = 0; i <= last; i++) { - ctx->error_depth = i; - if (!check_cert(ctx)) { - return 0; + if (trust == X509_TRUST_REJECTED) { + // Notify callback and reject if not overridden. + ctx->error = X509_V_ERR_CERT_REJECTED; + if (!call_verify_cb_at_depth(0, ctx, sk_X509_num(ctx->chain) - 1)) { + return X509_TRUST_REJECTED; } } - return 1; -} -static int check_cert(X509_STORE_CTX *ctx) { - X509_CRL *crl = nullptr; - int ok = 0, cnum = ctx->error_depth; - X509 *x = sk_X509_value(ctx->chain, cnum); - ctx->current_cert = x; - ctx->current_crl_issuer = nullptr; - ctx->current_crl_score = 0; - - // Try to retrieve the relevant CRL. Note that |get_crl| sets - // |current_crl_issuer| and |current_crl_score|, which |check_crl| then reads. - // - // TODO(davidben): The awkward internal calling convention is a historical - // artifact of when these functions were user-overridable callbacks, even - // though there was no way to set them correctly. These callbacks have since - // been removed, so we can pass input and output parameters more directly. - if (!get_crl(ctx, &crl, x)) { - ctx->error = X509_V_ERR_UNABLE_TO_GET_CRL; - ok = call_verify_cb(0, ctx); - goto err; - } - - ctx->current_crl = crl; - if (!check_crl(ctx, crl) || // - !cert_crl(ctx, crl, x)) { - goto err; + // If we accept partial chains, every certificate in the store is a trust + // anchor. + if (ctx->param->flags & X509_V_FLAG_PARTIAL_CHAIN) { + return X509_TRUST_TRUSTED; } - ok = 1; - -err: - X509_CRL_free(crl); - ctx->current_crl = nullptr; - return ok; + return X509_TRUST_UNTRUSTED; } // Check CRL times against values in X509_STORE_CTX -static int check_crl_time(X509_STORE_CTX *ctx, X509_CRL *crl, int notify) { +int check_crl_time(X509_STORE_CTX *ctx, const X509_CRL *crl, bool notify, + int depth) { if (ctx->param->flags & X509_V_FLAG_NO_CHECK_TIME) { return 1; } - if (notify) { - ctx->current_crl = crl; - } int64_t ptime; if (ctx->param->flags & X509_V_FLAG_USE_CHECK_TIME) { ptime = ctx->param->check_time; @@ -781,7 +409,7 @@ static int check_crl_time(X509_STORE_CTX *ctx, X509_CRL *crl, int notify) { return 0; } ctx->error = X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD; - if (!call_verify_cb(0, ctx)) { + if (!call_verify_cb_at_depth(0, ctx, depth)) { return 0; } } @@ -791,7 +419,7 @@ static int check_crl_time(X509_STORE_CTX *ctx, X509_CRL *crl, int notify) { return 0; } ctx->error = X509_V_ERR_CRL_NOT_YET_VALID; - if (!call_verify_cb(0, ctx)) { + if (!call_verify_cb_at_depth(0, ctx, depth)) { return 0; } } @@ -804,7 +432,7 @@ static int check_crl_time(X509_STORE_CTX *ctx, X509_CRL *crl, int notify) { return 0; } ctx->error = X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD; - if (!call_verify_cb(0, ctx)) { + if (!call_verify_cb_at_depth(0, ctx, depth)) { return 0; } } @@ -813,139 +441,87 @@ static int check_crl_time(X509_STORE_CTX *ctx, X509_CRL *crl, int notify) { return 0; } ctx->error = X509_V_ERR_CRL_HAS_EXPIRED; - if (!call_verify_cb(0, ctx)) { + if (!call_verify_cb_at_depth(0, ctx, depth)) { return 0; } } } - if (notify) { - ctx->current_crl = nullptr; - } - return 1; } -static int get_crl_sk(X509_STORE_CTX *ctx, X509_CRL **pcrl, X509 **pissuer, - int *pscore, STACK_OF(X509_CRL) *crls) { - int crl_score, best_score = *pscore; - X509 *x = ctx->current_cert; - X509_CRL *best_crl = nullptr; - X509 *crl_issuer = nullptr, *best_crl_issuer = nullptr; - - for (size_t i = 0; i < sk_X509_CRL_num(crls); i++) { - X509_CRL *crl = sk_X509_CRL_value(crls, i); - crl_score = get_crl_score(ctx, &crl_issuer, crl, x); - if (crl_score < best_score || crl_score == 0) { - continue; +int check_blocklist(X509_STORE_CTX *ctx) { + static const uint8_t kBlockList[][32] = { + {0xe0, 0x25, 0x4c, 0x7f, 0xcd, 0xc2, 0x65, 0xe2, 0xf0, 0xf8, 0xa4, + 0x28, 0xda, 0xbb, 0x75, 0xab, 0x4a, 0xe0, 0x39, 0xb4, 0x87, 0x2d, + 0x62, 0xf5, 0xee, 0x57, 0xf0, 0x87, 0x7d, 0x5a, 0x8a, 0x41}, + {0x43, 0xbf, 0xfa, 0x19, 0xd6, 0xef, 0x65, 0x14, 0x51, 0xf6, 0x84, + 0x1e, 0x01, 0x99, 0x31, 0xe9, 0x6f, 0x6f, 0x2e, 0xae, 0xff, 0x28, + 0xab, 0x4b, 0x6f, 0x1c, 0xe4, 0x57, 0x6d, 0xe2, 0x71, 0x7b}, + {0xeb, 0xed, 0x64, 0x75, 0xc2, 0xc2, 0xd8, 0x0f, 0xcb, 0x19, 0x9f, + 0x3f, 0xe8, 0xdd, 0x6e, 0x6b, 0x3c, 0xe6, 0x0b, 0x77, 0xeb, 0x15, + 0xd4, 0xe4, 0x2c, 0x3d, 0xec, 0x0c, 0x6f, 0x5b, 0x51, 0x05}, + {0x5c, 0x06, 0x60, 0xa2, 0xb1, 0x64, 0x91, 0x0d, 0x8f, 0xf5, 0x89, + 0xc1, 0x8e, 0x14, 0x55, 0x2e, 0x9e, 0x0e, 0xc2, 0x37, 0xdc, 0xef, + 0xe2, 0xcb, 0x73, 0x6d, 0x41, 0xa4, 0x81, 0x1f, 0x81, 0xa0}, + {0x63, 0xec, 0xef, 0xee, 0x43, 0x1d, 0x49, 0x52, 0x3b, 0xc1, 0x13, + 0x6c, 0x4e, 0x72, 0xd0, 0x20, 0x6a, 0x47, 0x70, 0x86, 0xca, 0xf6, + 0x06, 0xcd, 0x5c, 0xf0, 0x25, 0x68, 0x6c, 0x10, 0x9c, 0xf8}, + {0x49, 0xdd, 0x35, 0x3d, 0xc2, 0xb9, 0x19, 0xa3, 0x77, 0x06, 0x45, + 0x11, 0x7d, 0x0e, 0x60, 0xc1, 0x61, 0x15, 0xe7, 0xd1, 0xbc, 0x25, + 0x9e, 0xef, 0xe7, 0x34, 0xb0, 0x0e, 0xa4, 0x34, 0xfe, 0x5f}, + {0xec, 0x4f, 0xfa, 0x1a, 0xd0, 0xac, 0x88, 0x08, 0x13, 0x14, 0xb7, + 0xaa, 0xff, 0xef, 0x6a, 0x91, 0xf9, 0x7c, 0xd5, 0x72, 0x91, 0xbb, + 0x5b, 0xa6, 0x86, 0xc5, 0x41, 0x5f, 0x5e, 0xb4, 0xf6, 0xb6}, + {0x92, 0xc0, 0x63, 0xd6, 0x7e, 0x8f, 0x14, 0xdb, 0x71, 0xd2, 0x88, + 0xe1, 0x12, 0x66, 0xff, 0xda, 0xf6, 0x9c, 0xbf, 0x11, 0x5f, 0x7b, + 0xab, 0x14, 0xf3, 0xbc, 0x2f, 0x68, 0xd2, 0x5e, 0x0e, 0x30}, + }; + + const size_t num = sk_X509_num(ctx->chain); + for (size_t i = 0; i < num; i++) { + const X509 *x = sk_X509_value(ctx->chain, i); + ScopedCBB cbb; + if (!CBB_init(cbb.get(), 64) || + !x509_marshal_public_key(cbb.get(), X509_get_X509_PUBKEY(x))) { + ctx->error = X509_V_ERR_OUT_OF_MEM; + return 0; } - // If current CRL is equivalent use it if it is newer - if (crl_score == best_score && best_crl != nullptr) { - int day, sec; - if (ASN1_TIME_diff(&day, &sec, X509_CRL_get0_lastUpdate(best_crl), - X509_CRL_get0_lastUpdate(crl)) == 0) { - continue; - } - // ASN1_TIME_diff never returns inconsistent signs for |day| - // and |sec|. - if (day <= 0 && sec <= 0) { - continue; + uint8_t spki_hash[SHA256_DIGEST_LENGTH]; + SHA256(CBB_data(cbb.get()), CBB_len(cbb.get()), spki_hash); + for (const auto &block : kBlockList) { + if (Span(spki_hash) == Span(block)) { + ctx->error = X509_V_ERR_CERT_REVOKED; + if (!call_verify_cb_at_depth(0, ctx, static_cast(i))) { + return 0; + } } } - best_crl = crl; - best_crl_issuer = crl_issuer; - best_score = crl_score; - } - - if (best_crl) { - if (*pcrl) { - X509_CRL_free(*pcrl); - } - *pcrl = best_crl; - *pissuer = best_crl_issuer; - *pscore = best_score; - X509_CRL_up_ref(best_crl); - } - - if (best_score >= CRL_SCORE_VALID) { - return 1; - } - - return 0; -} - -// For a given CRL return how suitable it is for the supplied certificate -// 'x'. The return value is a mask of several criteria. If the issuer is not -// the certificate issuer this is returned in *pissuer. -static int get_crl_score(X509_STORE_CTX *ctx, X509 **pissuer, X509_CRL *crl, - X509 *x) { - int crl_score = 0; - - // First see if we can reject CRL straight away - - // Invalid IDP cannot be processed - if (crl->idp_flags & IDP_INVALID) { - return 0; - } - // Reason codes and indirect CRLs are not supported. - if (crl->idp_flags & (IDP_INDIRECT | IDP_REASONS)) { - return 0; - } - // We do not support indirect CRLs, so the issuer names must match. - if (X509_NAME_cmp(X509_get_issuer_name(x), X509_CRL_get_issuer(crl))) { - return 0; - } - crl_score |= CRL_SCORE_ISSUER_NAME; - - if (!(crl->flags & EXFLAG_CRITICAL)) { - crl_score |= CRL_SCORE_NOCRITICAL; - } - - // Check expiry - if (check_crl_time(ctx, crl, 0)) { - crl_score |= CRL_SCORE_TIME; } - - // Check authority key ID and locate certificate issuer - if (!crl_akid_check(ctx, crl, pissuer, &crl_score)) { - // If we can't locate certificate issuer at this point forget it - return 0; - } - - // Check cert for matching CRL distribution points - if (crl_crldp_check(x, crl, crl_score)) { - crl_score |= CRL_SCORE_SCOPE; - } - - return crl_score; + return 1; } -static int crl_akid_check(X509_STORE_CTX *ctx, X509_CRL *crl, X509 **pissuer, - int *pcrl_score) { - X509 *crl_issuer = nullptr; - X509_NAME *cnm = X509_CRL_get_issuer(crl); - int cidx = ctx->error_depth; - - if ((size_t)cidx != sk_X509_num(ctx->chain) - 1) { - cidx++; - } - - crl_issuer = sk_X509_value(ctx->chain, cidx); +int crl_akid_check(X509_STORE_CTX *ctx, const X509_CRL *crl, X509 **out_issuer, + int *out_crl_score, int depth) { + const X509_NAME *crl_name = X509_CRL_get_issuer(crl); + depth++; + assert(depth < static_cast(sk_X509_num(ctx->chain))); + X509 *crl_issuer = sk_X509_value(ctx->chain, depth); if (X509_check_akid(crl_issuer, crl->akid) == X509_V_OK) { - *pcrl_score |= CRL_SCORE_AKID | CRL_SCORE_ISSUER_CERT; - *pissuer = crl_issuer; + *out_crl_score |= CRL_SCORE_AKID | CRL_SCORE_ISSUER_CERT; + *out_issuer = crl_issuer; return 1; } - for (cidx++; cidx < (int)sk_X509_num(ctx->chain); cidx++) { - crl_issuer = sk_X509_value(ctx->chain, cidx); - if (X509_NAME_cmp(X509_get_subject_name(crl_issuer), cnm)) { + for (depth++; depth < (int)sk_X509_num(ctx->chain); depth++) { + crl_issuer = sk_X509_value(ctx->chain, depth); + if (X509_NAME_cmp(X509_get_subject_name(crl_issuer), crl_name)) { continue; } if (X509_check_akid(crl_issuer, crl->akid) == X509_V_OK) { - *pcrl_score |= CRL_SCORE_AKID | CRL_SCORE_SAME_PATH; - *pissuer = crl_issuer; + *out_crl_score |= CRL_SCORE_AKID | CRL_SCORE_SAME_PATH; + *out_issuer = crl_issuer; return 1; } } @@ -953,93 +529,76 @@ static int crl_akid_check(X509_STORE_CTX *ctx, X509_CRL *crl, X509 **pissuer, return 0; } -// Check for match between two dist point names: three separate cases. 1. -// Both are relative names and compare X509_NAME types. 2. One full, one -// relative. Compare X509_NAME to GENERAL_NAMES. 3. Both are full names and -// compare two GENERAL_NAMES. 4. One is NULL: automatic match. -static int idp_check_dp(DIST_POINT_NAME *a, DIST_POINT_NAME *b) { - X509_NAME *nm = nullptr; - GENERAL_NAMES *gens = nullptr; - GENERAL_NAME *gena, *genb; - size_t i, j; - if (!a || !b) { +int idp_check_dp(const DIST_POINT_NAME *dp_cert, + const DIST_POINT_NAME *dp_crl) { + // If the CRL IDP does not have a distributionPoint field, the CRL is scoped + // to the entire CA and matches everything. + // + // TODO(crbug.com/42290219): This covers a null `dp_crl`, but not `dp_cert`. + // If `dp_cert` is null, that means the certificate's DistributionPoint lacks + // a distributionPoint. However, DistributionPoints are required to have + // either distributionPoint or cRLIssuer (indirect CRL) and we reject the + // latter. However, we don't seem to check this in the parser. Enforce this so + // that `dp_cert` cannot be null. + if (!dp_cert || !dp_crl) { return 1; } - if (a->type == 1) { - if (!a->dpname) { - return 0; - } - // Case 1: two X509_NAME - if (b->type == 1) { - if (!b->dpname) { - return 0; - } - if (!X509_NAME_cmp(a->dpname, b->dpname)) { - return 1; - } else { - return 0; - } - } - // Case 2: set name and GENERAL_NAMES appropriately - nm = a->dpname; - gens = b->name.fullname; - } else if (b->type == 1) { - if (!b->dpname) { - return 0; - } - // Case 2: set name and GENERAL_NAMES appropriately - gens = a->name.fullname; - nm = b->dpname; + // We only support fullName, not nameRelativeToCRLIssuer. + if (dp_cert->type != 0 || dp_crl->type != 0) { + return 0; } - - // Handle case 2 with one GENERAL_NAMES and one X509_NAME - if (nm) { - for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) { - gena = sk_GENERAL_NAME_value(gens, i); - if (gena->type != GEN_DIRNAME) { - continue; - } - if (!X509_NAME_cmp(nm, gena->d.directoryName)) { - return 1; - } - } + // Check that the CRL's distributionPoint has some name in common with the + // certificate's. A CA might shard across multiple CRLs. This check ensures we + // are looking at the right shard. + const size_t crl_names_count = sk_GENERAL_NAME_num(dp_crl->name.fullname); + if (crl_names_count == 0) { + return 0; + } + const size_t cert_names_count = sk_GENERAL_NAME_num(dp_cert->name.fullname); + if (cert_names_count == 0) { return 0; } - // Else case 3: two GENERAL_NAMES + const GENERAL_NAMES *longer = crl_names_count >= cert_names_count + ? dp_crl->name.fullname + : dp_cert->name.fullname; + const GENERAL_NAMES *shorter = crl_names_count < cert_names_count + ? dp_crl->name.fullname + : dp_cert->name.fullname; + bssl::Array sorted; + if (!sorted.InitForOverwrite(sk_GENERAL_NAME_num(shorter))) { + return 0; + } + size_t i = 0; + for (const GENERAL_NAME *s : shorter) { + sorted[i++] = s; + } - for (i = 0; i < sk_GENERAL_NAME_num(a->name.fullname); i++) { - gena = sk_GENERAL_NAME_value(a->name.fullname, i); - for (j = 0; j < sk_GENERAL_NAME_num(b->name.fullname); j++) { - genb = sk_GENERAL_NAME_value(b->name.fullname, j); - if (!GENERAL_NAME_cmp(gena, genb)) { - return 1; - } + auto cmp_func = [](const GENERAL_NAME *a, const GENERAL_NAME *b) { + return GENERAL_NAME_cmp(a, b) < 0; + }; + std::sort(sorted.begin(), sorted.end(), cmp_func); + for (const GENERAL_NAME *name : longer) { + if (std::binary_search(sorted.begin(), sorted.end(), name, cmp_func)) { + return 1; } } - return 0; } // Check CRLDP and IDP -static int crl_crldp_check(X509 *x, X509_CRL *crl, int crl_score) { +int crl_crldp_check(const X509 *x, const X509_CRL *crl, int crl_score) { auto *impl = FromOpaque(x); - // TODO(bbe): crbug.com/409778435 Make tests for the corner cases we hit - // here so that we stay correct for RFC 5280 6.3.3 steps b.1 and b.2 - if (crl->idp_flags & IDP_ONLYATTR) { - return 0; - } if (impl->ex_flags & EXFLAG_CA) { - if (crl->idp_flags & IDP_ONLYUSER) { + if (crl->idp && crl->idp->onlyuser) { return 0; } } else { - if (crl->idp_flags & IDP_ONLYCA) { + if (crl->idp && crl->idp->onlyCA) { return 0; } } - for (size_t i = 0; i < sk_DIST_POINT_num(impl->crldp); i++) { - DIST_POINT *dp = sk_DIST_POINT_value(impl->crldp, i); + for (const DIST_POINT *dp : impl->crldp.get()) { // Skip distribution points with a reasons field or a CRL issuer: // // We do not support CRLs partitioned by reason code. RFC 5280 requires CAs @@ -1054,120 +613,164 @@ static int crl_crldp_check(X509 *x, X509_CRL *crl, int crl_score) { // the certificate issuer (and set CRL_SCORE_ISSUER_NAME); // RFC 5280 Section 6.3.3 step b.2 - if (!crl->idp || idp_check_dp(dp->distpoint, crl->idp->distpoint)){ + if (!crl->idp || idp_check_dp(dp->distpoint, crl->idp->distpoint)) { return 1; } } // If the CRL does not specify an issuing distribution point, allow it to - // match anything. - // - // TODO(davidben): Does this match RFC 5280? It's hard to follow because RFC - // 5280 starts from distribution points, while this starts from CRLs. + // match anything. This partially implements RFC 5280. The final paragraph of + // RFC 5280, section 6.3.3, specifies a default CRL-DP. This default would + // match IDP-less CRLs and CRLs with an IDP of the default CRL-DP. We do not + // implement the second condition. return !crl->idp || !crl->idp->distpoint; } -// Retrieve CRL corresponding to current certificate. -static int get_crl(X509_STORE_CTX *ctx, X509_CRL **pcrl, X509 *x) { - X509 *issuer = nullptr; - int crl_score = 0; - X509_CRL *crl = nullptr; - STACK_OF(X509_CRL) *skcrl = nullptr; - if (get_crl_sk(ctx, &crl, &issuer, &crl_score, ctx->crls)) { - goto done; +// For a given CRL return how suitable it is for the supplied certificate +// 'x'. The return value is a mask of several criteria, or zero if the CRL is +// unusable. The certificate issuer is returned in `*out_issuer`. +int get_crl_score(X509_STORE_CTX *ctx, X509 **out_issuer, const X509_CRL *crl, + const X509 *x, int depth) { + if (crl->idp) { + // Per RFC 5280, section 5.2.5, at most one of onlyContainsUserCerts, + // onlyContainsCACerts, and onlyContainsAttributeCerts may be true. + // TODO(crbug.com/42290307): Move this check to the `ISSUING_DIST_POINT` + // parser. + int num_only = + (!!crl->idp->onlyuser) + (!!crl->idp->onlyCA) + (!!crl->idp->onlyattr); + if (num_only > 1) { + return 0; + } + // Reason codes and indirect CRLs are not supported. + if (crl->idp->indirectCRL || crl->idp->onlysomereasons) { + return 0; + } } - // Lookup CRLs from store - skcrl = X509_STORE_CTX_get1_crls(ctx, X509_get_issuer_name(x)); + // We do not support indirect CRLs, so the issuer names must match. + if (X509_NAME_cmp(X509_get_issuer_name(x), X509_CRL_get_issuer(crl))) { + return 0; + } - // If no CRLs found and a near match from get_crl_sk use that - if (!skcrl && crl) { - goto done; + int crl_score = CRL_SCORE_ISSUER_NAME; + if (!(crl->flags & EXFLAG_CRITICAL)) { + crl_score |= CRL_SCORE_NOCRITICAL; } - get_crl_sk(ctx, &crl, &issuer, &crl_score, skcrl); + // Check expiry + if (check_crl_time(ctx, crl, /*notify=*/false, depth)) { + crl_score |= CRL_SCORE_TIME; + } - sk_X509_CRL_pop_free(skcrl, X509_CRL_free); + // Check authority key ID and locate certificate issuer + if (!crl_akid_check(ctx, crl, out_issuer, &crl_score, depth)) { + // If we can't locate certificate issuer at this point forget it + return 0; + } -done: - // If we got any kind of CRL use it and return success - if (crl) { - ctx->current_crl_issuer = issuer; - ctx->current_crl_score = crl_score; - *pcrl = crl; - return 1; + // Check cert for matching CRL distribution points + if (crl_crldp_check(x, crl, crl_score)) { + crl_score |= CRL_SCORE_SCOPE; } - return 0; + return crl_score; } -// Check CRL validity -static int check_crl(X509_STORE_CTX *ctx, X509_CRL *crl) { - X509Impl *issuer = nullptr; - int cnum = ctx->error_depth; - int chnum = (int)sk_X509_num(ctx->chain) - 1; - // If we have an alternative CRL issuer cert use that. Otherwise, it is the - // issuer of the current certificate. - if (ctx->current_crl_issuer) { - issuer = FromOpaque(ctx->current_crl_issuer); - } else if (cnum < chnum) { - issuer = FromOpaque(sk_X509_value(ctx->chain, cnum + 1)); - } else { - issuer = FromOpaque(sk_X509_value(ctx->chain, chnum)); - // If not self signed, can't check signature - if (!x509_check_issued_with_callback(ctx, issuer, issuer)) { - ctx->error = X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER; - if (!call_verify_cb(0, ctx)) { - return 0; +int get_crl_sk(X509_STORE_CTX *ctx, CRLInfo *best, STACK_OF(X509_CRL) *crls, + const X509 *x, int depth) { + for (X509_CRL *crl : crls) { + X509 *crl_issuer = nullptr; + int crl_score = get_crl_score(ctx, &crl_issuer, crl, x, depth); + if (crl_score < best->score || crl_score == 0) { + continue; + } + assert(crl_issuer != nullptr); + // If current CRL is equivalent use it if it is newer + if (crl_score == best->score && best->crl != nullptr) { + int day, sec; + if (ASN1_TIME_diff(&day, &sec, X509_CRL_get0_lastUpdate(best->crl.get()), + X509_CRL_get0_lastUpdate(crl)) == 0) { + continue; + } + // ASN1_TIME_diff never returns inconsistent signs for `day` + // and `sec`. + if (day <= 0 && sec <= 0) { + continue; } } + best->crl = UpRef(crl); + best->issuer = crl_issuer; + best->score = crl_score; } - if (issuer) { - // Check for cRLSign bit if keyUsage present - if ((issuer->ex_flags & EXFLAG_KUSAGE) && - !(issuer->ex_kusage & X509v3_KU_CRL_SIGN)) { - ctx->error = X509_V_ERR_KEYUSAGE_NO_CRL_SIGN; - if (!call_verify_cb(0, ctx)) { - return 0; - } + return best->score >= CRL_SCORE_VALID; +} + +// Retrieve CRL corresponding to current certificate. +int get_crl(X509_STORE_CTX *ctx, CRLInfo *out_crl_info, const X509 *x, + int depth) { + CRLInfo best; + if (!get_crl_sk(ctx, &best, ctx->crls, x, depth)) { + // If we haven't yet found a CRL with sufficiently high score, lookup CRLs + // from the store. + UniquePtr skcrl( + X509_STORE_CTX_get1_crls(ctx, X509_get_issuer_name(x))); + if (skcrl) { + get_crl_sk(ctx, &best, skcrl.get(), x, depth); } + } - if (!(ctx->current_crl_score & CRL_SCORE_SCOPE)) { - ctx->error = X509_V_ERR_DIFFERENT_CRL_SCOPE; - if (!call_verify_cb(0, ctx)) { - return 0; - } + // If we got any kind of CRL use it and return success + if (best.crl) { + *out_crl_info = std::move(best); + return 1; + } + + return 0; +} + +// Check CRL validity +int check_crl(X509_STORE_CTX *ctx, const CRLInfo &crl_info, int depth) { + X509_CRL *crl = crl_info.crl.get(); + X509Impl *issuer = FromOpaque(crl_info.issuer); + + // Check for cRLSign bit if keyUsage present + if ((issuer->ex_flags & EXFLAG_KUSAGE) && + !(issuer->ex_kusage & X509v3_KU_CRL_SIGN)) { + ctx->error = X509_V_ERR_KEYUSAGE_NO_CRL_SIGN; + if (!call_verify_cb_at_depth(0, ctx, depth)) { + return 0; } + } - if (crl->idp_flags & IDP_INVALID) { - ctx->error = X509_V_ERR_INVALID_EXTENSION; - if (!call_verify_cb(0, ctx)) { - return 0; - } + if (!(crl_info.score & CRL_SCORE_SCOPE)) { + ctx->error = X509_V_ERR_DIFFERENT_CRL_SCOPE; + if (!call_verify_cb_at_depth(0, ctx, depth)) { + return 0; } + } - if (!(ctx->current_crl_score & CRL_SCORE_TIME)) { - if (!check_crl_time(ctx, crl, 1)) { - return 0; - } + if (!(crl_info.score & CRL_SCORE_TIME)) { + if (!check_crl_time(ctx, crl, /*notify=*/true, depth)) { + return 0; } + } - // Attempt to get issuer certificate public key - EVP_PKEY *ikey = X509_get0_pubkey(issuer); - if (!ikey) { - ctx->error = X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY; - if (!call_verify_cb(0, ctx)) { + // Attempt to get issuer certificate public key + EVP_PKEY *ikey = X509_get0_pubkey(issuer); + if (!ikey) { + ctx->error = X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY; + if (!call_verify_cb_at_depth(0, ctx, depth)) { + return 0; + } + } else { + // Verify CRL signature + if (!sig_nid_allowed(ctx, X509_CRL_get_signature_nid(crl)) || + X509_CRL_verify(crl, ikey) <= 0) { + ctx->error = X509_V_ERR_CRL_SIGNATURE_FAILURE; + if (!call_verify_cb_at_depth(0, ctx, depth)) { return 0; } - } else { - // Verify CRL signature - if (X509_CRL_verify(crl, ikey) <= 0) { - ctx->error = X509_V_ERR_CRL_SIGNATURE_FAILURE; - if (!call_verify_cb(0, ctx)) { - return 0; - } - } } } @@ -1175,7 +778,7 @@ static int check_crl(X509_STORE_CTX *ctx, X509_CRL *crl) { } // Check certificate against CRL -static int cert_crl(X509_STORE_CTX *ctx, X509_CRL *crl, X509 *x) { +int cert_crl(X509_STORE_CTX *ctx, X509_CRL *crl, const X509 *x, int depth) { // The rules changed for this... previously if a CRL contained unhandled // critical extensions it could still be used to indicate a certificate // was revoked. This has since been changed since critical extension can @@ -1183,7 +786,7 @@ static int cert_crl(X509_STORE_CTX *ctx, X509_CRL *crl, X509 *x) { if (!(ctx->param->flags & X509_V_FLAG_IGNORE_CRITICAL) && (crl->flags & EXFLAG_CRITICAL)) { ctx->error = X509_V_ERR_UNHANDLED_CRITICAL_CRL_EXTENSION; - if (!call_verify_cb(0, ctx)) { + if (!call_verify_cb_at_depth(0, ctx, depth)) { return 0; } } @@ -1191,7 +794,7 @@ static int cert_crl(X509_STORE_CTX *ctx, X509_CRL *crl, X509 *x) { X509_REVOKED *rev; if (X509_CRL_get0_by_cert(crl, &rev, x)) { ctx->error = X509_V_ERR_CERT_REVOKED; - if (!call_verify_cb(0, ctx)) { + if (!call_verify_cb_at_depth(0, ctx, depth)) { return 0; } } @@ -1199,23 +802,57 @@ static int cert_crl(X509_STORE_CTX *ctx, X509_CRL *crl, X509 *x) { return 1; } -static int check_policy(X509_STORE_CTX *ctx) { - X509 *current_cert = nullptr; +int check_revocation_for_cert(X509_STORE_CTX *ctx, int depth) { + X509 *x = sk_X509_value(ctx->chain, depth); + CRLInfo crl_info; + if (!get_crl(ctx, &crl_info, x, depth)) { + ctx->error = X509_V_ERR_UNABLE_TO_GET_CRL; + return call_verify_cb_at_depth(0, ctx, depth); + } + + if (!check_crl(ctx, crl_info, depth) || // + !cert_crl(ctx, crl_info.crl.get(), x, depth)) { + return 0; + } + + return 1; +} + +int check_revocation(X509_STORE_CTX *ctx) { + if (!(ctx->param->flags & X509_V_FLAG_CRL_CHECK) || + sk_X509_num(ctx->chain) <= 1) { + return 1; + } + // Check either only the leaf, or everything but the trust anchor. The trust + // anchor is not formally part of RFC 5280 certification path and, without an + // issuer, we could not usefully process a CRL anyway. + const int num_to_check = (ctx->param->flags & X509_V_FLAG_CRL_CHECK_ALL) + ? static_cast(sk_X509_num(ctx->chain)) - 1 + : 1; + for (int i = 0; i < num_to_check; i++) { + if (!check_revocation_for_cert(ctx, i)) { + return 0; + } + } + return 1; +} + +int check_policy(X509_STORE_CTX *ctx) { + int error_depth; int ret = X509_policy_check(ctx->chain, ctx->param->policies, - ctx->param->flags, ¤t_cert); + ctx->param->flags, &error_depth); if (ret != X509_V_OK) { - ctx->current_cert = current_cert; ctx->error = ret; if (ret == X509_V_ERR_OUT_OF_MEM) { return 0; } - return call_verify_cb(0, ctx); + return call_verify_cb_at_depth(0, ctx, error_depth); } return 1; } -static int check_cert_time(X509_STORE_CTX *ctx, X509 *x) { +int check_cert_time(X509_STORE_CTX *ctx, X509 *x, int depth) { if (ctx->param->flags & X509_V_FLAG_NO_CHECK_TIME) { return 1; } @@ -1227,36 +864,36 @@ static int check_cert_time(X509_STORE_CTX *ctx, X509 *x) { ptime = time(nullptr); } - int i = X509_cmp_time_posix(X509_get_notBefore(x), ptime); + int i = (ctx->param->flags & X509_V_FLAG_ALLOW_TIMEZONE_OFFSET) + ? X509_cmp_time_posix_nonstandard(X509_get_notBefore(x), ptime) + : X509_cmp_time_posix(X509_get_notBefore(x), ptime); if (i == 0) { ctx->error = X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD; - ctx->current_cert = x; - if (!call_verify_cb(0, ctx)) { + if (!call_verify_cb_at_depth(0, ctx, depth)) { return 0; } } if (i > 0) { ctx->error = X509_V_ERR_CERT_NOT_YET_VALID; - ctx->current_cert = x; - if (!call_verify_cb(0, ctx)) { + if (!call_verify_cb_at_depth(0, ctx, depth)) { return 0; } } - i = X509_cmp_time_posix(X509_get_notAfter(x), ptime); + i = (ctx->param->flags & X509_V_FLAG_ALLOW_TIMEZONE_OFFSET) + ? X509_cmp_time_posix_nonstandard(X509_get_notAfter(x), ptime) + : X509_cmp_time_posix(X509_get_notAfter(x), ptime); if (i == 0) { ctx->error = X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD; - ctx->current_cert = x; - if (!call_verify_cb(0, ctx)) { + if (!call_verify_cb_at_depth(0, ctx, depth)) { return 0; } } if (i < 0) { ctx->error = X509_V_ERR_CERT_HAS_EXPIRED; - ctx->current_cert = x; - if (!call_verify_cb(0, ctx)) { + if (!call_verify_cb_at_depth(0, ctx, depth)) { return 0; } } @@ -1264,79 +901,250 @@ static int check_cert_time(X509_STORE_CTX *ctx, X509 *x) { return 1; } -static int internal_verify(X509_STORE_CTX *ctx) { - // TODO(davidben): This logic is incredibly confusing. Rewrite this: - // - // First, don't allow the verify callback to suppress - // X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY, which will simplify the - // signature check. Then replace jumping into the middle of the loop. It's - // trying to ensure that all certificates see |check_cert_time|, then checking - // the root's self signature when requested, but not breaking partial chains - // in the process. - int n = (int)sk_X509_num(ctx->chain); - ctx->error_depth = n - 1; - n--; - X509 *xi = sk_X509_value(ctx->chain, n); - X509 *xs; - if (x509_check_issued_with_callback(ctx, xi, xi)) { - xs = xi; +int verify_signature(X509_STORE_CTX *ctx, const X509 *x509, + const X509 *issuer) { + EVP_PKEY *pkey = X509_get0_pubkey(issuer); + if (pkey == nullptr) { + return X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY; + } + + int ret; + if ((ctx->param->flags & X509_V_FLAG_USE_MTC_DRAFT_PLANTS_07) && + x509_is_merkle_tree_ca(issuer)) { + ret = x509_verify_mtc(x509, pkey, issuer); } else { - if (ctx->param->flags & X509_V_FLAG_PARTIAL_CHAIN) { - xs = xi; - goto check_cert; - } - if (n <= 0) { - ctx->error = X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE; - ctx->current_cert = xi; - return call_verify_cb(0, ctx); - } - n--; - ctx->error_depth = n; - xs = sk_X509_value(ctx->chain, n); - } - - // ctx->error=0; not needed - while (n >= 0) { - ctx->error_depth = n; - - // Skip signature check for self signed certificates unless - // explicitly asked for. It doesn't add any security and just wastes - // time. - if (xs != xi || (ctx->param->flags & X509_V_FLAG_CHECK_SS_SIGNATURE)) { - EVP_PKEY *pkey = X509_get0_pubkey(xi); - if (pkey == nullptr) { - ctx->error = X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY; - ctx->current_cert = xi; - if (!call_verify_cb(0, ctx)) { - return 0; - } - } else if (X509_verify(xs, pkey) <= 0) { - ctx->error = X509_V_ERR_CERT_SIGNATURE_FAILURE; - ctx->current_cert = xs; - if (!call_verify_cb(0, ctx)) { + ret = sig_nid_allowed(ctx, X509_get_signature_nid(x509)) && + X509_verify(x509, pkey); + } + return ret ? X509_V_OK : X509_V_ERR_CERT_SIGNATURE_FAILURE; +} + +int check_chain_signatures_and_time(X509_STORE_CTX *ctx) { + size_t num = sk_X509_num(ctx->chain); + for (size_t i = num - 1; i < num; i--) { + X509 *x509 = sk_X509_value(ctx->chain, i); + if (i < num - 1) { + X509 *issuer = sk_X509_value(ctx->chain, i + 1); + if (int err = verify_signature(ctx, x509, issuer); err != X509_V_OK) { + ctx->error = err; + if (!call_verify_cb_at_depth(0, ctx, static_cast(i))) { return 0; } } } - check_cert: - if (!check_cert_time(ctx, xs)) { + if (!check_cert_time(ctx, x509, static_cast(i))) { return 0; } // The last error (if any) is still in the error value - ctx->current_cert = xs; - if (!call_verify_cb(1, ctx)) { + if (!call_verify_cb_at_depth(1, ctx, static_cast(i))) { + return 0; + } + } + + return 1; +} + +} // namespace + +// Given a possible certificate and issuer check them +int bssl::x509_check_issued_with_callback(X509_STORE_CTX *ctx, const X509 *x, + const X509 *issuer) { + int ret; + ret = X509_check_issued(issuer, x); + if (ret == X509_V_OK) { + return 1; + } + // If we haven't asked for issuer errors don't set ctx + if (!(ctx->param->flags & X509_V_FLAG_CB_ISSUER_CHECK)) { + return 0; + } + + ctx->error = ret; + ctx->current_cert = const_cast(x); + return call_verify_cb(0, ctx); +} + +int X509_verify_cert(X509_STORE_CTX *ctx) { + int ok = 0; + Cleanup set_ctx_error = [&] { + // Safety net, error returns must set ctx->error + if (!ok && ctx->error == X509_V_OK) { + ctx->error = X509_V_ERR_UNSPECIFIED; + } + }; + + const X509_VERIFY_PARAM *param = ctx->param; + if (ctx->cert == nullptr) { + OPENSSL_PUT_ERROR(X509, X509_R_NO_CERT_SET_FOR_US_TO_VERIFY); + ctx->error = X509_V_ERR_INVALID_CALL; + return 0; + } + + if (ctx->chain != nullptr) { + // This X509_STORE_CTX has already been used to verify a cert. We + // cannot do another one. + OPENSSL_PUT_ERROR(X509, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); + ctx->error = X509_V_ERR_INVALID_CALL; + return 0; + } + + if (param->flags & + (X509_V_FLAG_EXTENDED_CRL_SUPPORT | X509_V_FLAG_USE_DELTAS)) { + // We do not support indirect or delta CRLs. The flags still exist for + // compatibility with bindings libraries, but to ensure we do not + // inadvertently skip a CRL check that the caller expects, fail closed. + OPENSSL_PUT_ERROR(X509, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); + ctx->error = X509_V_ERR_INVALID_CALL; + return 0; + } + + // Copy the untrusted certificates so we can track which have been used. + // `untrusted` is not a `UniquePtr` because that would do a + // deep free and this is a shallow free. + STACK_OF(X509) *untrusted = nullptr; + Cleanup free_untrusted = [&] { sk_X509_free(untrusted); }; + if (ctx->untrusted != nullptr && + (untrusted = sk_X509_dup(ctx->untrusted)) == nullptr) { + ctx->error = X509_V_ERR_OUT_OF_MEM; + return 0; + } + + // `param->depth` does not include the leaf certificate or the trust anchor, + // so the maximum size is 2 more. + int max_chain = param->depth >= INT_MAX - 2 ? INT_MAX : param->depth + 2; + int trust = X509_TRUST_UNTRUSTED; + + // First, check if the target certificate already exists in the trust store as + // a trust "anchor". + if (UniquePtr store_cert = lookup_cert_match(ctx, ctx->cert); + store_cert != nullptr) { + ctx->chain = sk_X509_new_null(); + if (ctx->chain == nullptr || + !PushToStack(ctx->chain, std::move(store_cert))) { + ctx->error = X509_V_ERR_OUT_OF_MEM; + return 0; + } + trust = check_trust(ctx); + if (trust == X509_TRUST_REJECTED) { + return 0; + } + } + + // If it was not a directly trusted (or distrusted) leaf, build a chain the + // usual way. (This is the common case.) + if (trust == X509_TRUST_UNTRUSTED) { + // Start with the target certificate. Clear `ctx->chain` in case there is + // any remnant of a failed trusted leaf above. + sk_X509_pop_free(ctx->chain, X509_free); + ctx->chain = sk_X509_new_null(); + if (ctx->chain == nullptr || !PushToStack(ctx->chain, UpRef(ctx->cert))) { + ctx->error = X509_V_ERR_OUT_OF_MEM; return 0; } - n--; - if (n >= 0) { - xi = xs; - xs = sk_X509_value(ctx->chain, n); + // Add certificates to the path until we reach a trust anchor, or fail. + bool have_store_cert = false; + for (;;) { + if (static_cast(sk_X509_num(ctx->chain)) >= max_chain) { + break; + } + + // If the last certificate is already self-signed, we won't get anywhere + // by looking further. + X509 *last = sk_X509_last(ctx->chain); + int is_self_signed; + if (!cert_self_signed(last, &is_self_signed)) { + ctx->error = X509_V_ERR_INVALID_EXTENSION; + return 0; + } + if (is_self_signed) { + break; + } + + // Prioritize looking for issuers in the store. + if (UniquePtr issuer = get_trusted_issuer(ctx, last); + issuer != nullptr) { + if (!PushToStack(ctx->chain, std::move(issuer))) { + ctx->error = X509_V_ERR_OUT_OF_MEM; + return 0; + } + have_store_cert = true; + // Certificates from the store are candidates to be trust anchors. + trust = check_trust(ctx); + if (trust == X509_TRUST_REJECTED) { + return 0; + } + if (trust == X509_TRUST_TRUSTED) { + break; + } + continue; + } + + // Look for an issuer among peer-provided untrusted intermediates, but + // only if we included a certificate from the store. For now, we match + // OpenSSL by not interleaving them, but the logic that relied on this has + // been removed. + if (!have_store_cert) { + if (X509 *issuer = find_issuer(ctx, untrusted, last); + issuer != nullptr) { + if (!PushToStack(ctx->chain, UpRef(issuer))) { + ctx->error = X509_V_ERR_OUT_OF_MEM; + return 0; + } + (void)sk_X509_delete_ptr(untrusted, issuer); + // We must not call `check_trust` here because the last certificate in + // the chain is not from the store. Otherwise, `check_trust` might + // mistake it for a trust anchor. + continue; + } + } + + // Couldn't find anything. + break; + } + + // `X509_TRUST_REJECTED` should have returned early. + assert(trust == X509_TRUST_UNTRUSTED || X509_TRUST_TRUSTED); + if (trust == X509_TRUST_UNTRUSTED) { + if (static_cast(sk_X509_num(ctx->chain)) >= max_chain) { + ctx->error = X509_V_ERR_CERT_CHAIN_TOO_LONG; + } else if (have_store_cert) { + ctx->error = X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT; + } else { + int is_self_signed; + if (!cert_self_signed(sk_X509_last(ctx->chain), &is_self_signed)) { + ctx->error = X509_V_ERR_INVALID_EXTENSION; + } else if (!is_self_signed) { + ctx->error = X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY; + } else if (sk_X509_num(ctx->chain) == 1) { + ctx->error = X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT; + } else { + ctx->error = X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN; + } + } + if (!call_verify_cb_at_depth(0, ctx, sk_X509_num(ctx->chain) - 1)) { + return 0; + } } } + // Perform path validation. If the caller did not suppress some error with the + // verify callback, we now have either a path from the target certificate to + // some trust anchor or a directly trusted leaf. + assert(sk_X509_num(ctx->chain) >= 1); + if (!check_chain_extensions(ctx) || // + !check_id(ctx) || // + !check_revocation(ctx) || // + !check_blocklist(ctx) || // + !check_chain_signatures_and_time(ctx) || // + !check_name_constraints(ctx) || // + !check_policy(ctx)) { + return 0; + } + + ok = 1; return 1; } @@ -1358,6 +1166,15 @@ int X509_cmp_time_posix(const ASN1_TIME *ctm, int64_t cmp_time) { return (ctm_time - cmp_time <= 0) ? -1 : 1; } +int X509_cmp_time_posix_nonstandard(const ASN1_TIME *ctm, int64_t cmp_time) { + int64_t ctm_time; + if (!ASN1_TIME_to_posix_nonstandard(ctm, &ctm_time)) { + return 0; + } + // The return value 0 is reserved for errors. + return (ctm_time - cmp_time <= 0) ? -1 : 1; +} + ASN1_TIME *X509_gmtime_adj(ASN1_TIME *s, long offset_sec) { return X509_time_adj(s, offset_sec, nullptr); } @@ -1423,12 +1240,8 @@ STACK_OF(X509) *X509_STORE_CTX_get1_chain(const X509_STORE_CTX *ctx) { return X509_chain_up_ref(ctx->chain); } -X509_CRL *X509_STORE_CTX_get0_current_crl(const X509_STORE_CTX *ctx) { - return ctx->current_crl; -} - X509_STORE_CTX *X509_STORE_CTX_get0_parent_ctx(const X509_STORE_CTX *ctx) { - // In OpenSSL, an |X509_STORE_CTX| sometimes has a parent context during CRL + // In OpenSSL, an `X509_STORE_CTX` sometimes has a parent context during CRL // path validation for indirect CRLs. We require the CRL to be issued // somewhere along the certificate path, so this is always NULL. return nullptr; @@ -1447,7 +1260,7 @@ void X509_STORE_CTX_set0_crls(X509_STORE_CTX *ctx, STACK_OF(X509_CRL) *sk) { } int X509_STORE_CTX_set_purpose(X509_STORE_CTX *ctx, int purpose) { - // If |purpose| is zero, this function historically silently did nothing. + // If `purpose` is zero, this function historically silently did nothing. if (purpose == 0) { return 1; } @@ -1470,7 +1283,7 @@ int X509_STORE_CTX_set_purpose(X509_STORE_CTX *ctx, int purpose) { } int X509_STORE_CTX_set_trust(X509_STORE_CTX *ctx, int trust) { - // If |trust| is zero, this function historically silently did nothing. + // If `trust` is zero, this function historically silently did nothing. if (trust == 0) { return 1; } diff --git a/third_party/boringssl/src/crypto/x509/x509_vpm.cc b/third_party/boringssl/src/crypto/x509/x509_vpm.cc index 750798144..775a24fe9 100644 --- a/third_party/boringssl/src/crypto/x509/x509_vpm.cc +++ b/third_party/boringssl/src/crypto/x509/x509_vpm.cc @@ -30,13 +30,15 @@ using namespace bssl; // X509_VERIFY_PARAM functions +namespace { + #define SET_HOST 0 #define ADD_HOST 1 -static void str_free(char *s) { OPENSSL_free(s); } +void str_free(char *s) { OPENSSL_free(s); } -static int int_x509_param_set_hosts(X509_VERIFY_PARAM *param, int mode, - const char *name, size_t namelen) { +int int_x509_param_set_hosts(X509_VERIFY_PARAM *param, int mode, + const char *name, size_t namelen) { char *copy; if (name == nullptr || namelen == 0) { @@ -78,27 +80,7 @@ static int int_x509_param_set_hosts(X509_VERIFY_PARAM *param, int mode, return 1; } -X509_VERIFY_PARAM *X509_VERIFY_PARAM_new() { - X509_VERIFY_PARAM *param = New(); - if (!param) { - return nullptr; - } - param->depth = -1; - return param; -} - -void X509_VERIFY_PARAM_free(X509_VERIFY_PARAM *param) { - if (param == nullptr) { - return; - } - sk_ASN1_OBJECT_pop_free(param->policies, ASN1_OBJECT_free); - sk_OPENSSL_STRING_pop_free(param->hosts, str_free); - OPENSSL_free(param->email); - OPENSSL_free(param->ip); - Delete(param); -} - -static bool should_copy(bool dest_is_set, bool src_is_set, bool prefer_src) { +bool should_copy(bool dest_is_set, bool src_is_set, bool prefer_src) { if (prefer_src) { // We prefer the source, so as long as there is a value to copy, copy it. return src_is_set; @@ -109,20 +91,18 @@ static bool should_copy(bool dest_is_set, bool src_is_set, bool prefer_src) { } template -static void copy_int_param(T *dest, const T *src, T default_val, - bool prefer_src) { +void copy_int_param(T *dest, const T *src, T default_val, bool prefer_src) { static_assert(std::is_integral_v); if (should_copy(*dest != default_val, *src != default_val, prefer_src)) { *dest = *src; } } -// x509_verify_param_merge merges fields from |src| to |dest|. If both |src| and -// |dest| have some field set, |prefer_src| determines whether |src| or |dest|'s +// x509_verify_param_merge merges fields from `src` to `dest`. If both `src` and +// `dest` have some field set, `prefer_src` determines whether `src` or `dest`'s // version is used. -static int x509_verify_param_merge(X509_VERIFY_PARAM *dest, - const X509_VERIFY_PARAM *src, - bool prefer_src) { +int x509_verify_param_merge(X509_VERIFY_PARAM *dest, + const X509_VERIFY_PARAM *src, bool prefer_src) { if (src == nullptr) { return 1; } @@ -136,12 +116,12 @@ static int x509_verify_param_merge(X509_VERIFY_PARAM *dest, copy_int_param(&dest->trust, &src->trust, /*default_val=*/0, prefer_src); copy_int_param(&dest->depth, &src->depth, /*default_val=*/-1, prefer_src); - // |check_time|, unlike all other parameters, does not honor |prefer_src|. - // This means |X509_VERIFY_PARAM_set1| will not overwrite it. This behavior + // `check_time`, unlike all other parameters, does not honor `prefer_src`. + // This means `X509_VERIFY_PARAM_set1` will not overwrite it. This behavior // comes from OpenSSL but may have been a bug. if (!(dest->flags & X509_V_FLAG_USE_CHECK_TIME)) { dest->check_time = src->check_time; - // The source |X509_V_FLAG_USE_CHECK_TIME| flag, if set, is copied below. + // The source `X509_V_FLAG_USE_CHECK_TIME` flag, if set, is copied below. } dest->flags |= src->flags; @@ -183,22 +163,8 @@ static int x509_verify_param_merge(X509_VERIFY_PARAM *dest, return 1; } -int X509_VERIFY_PARAM_inherit(X509_VERIFY_PARAM *dest, - const X509_VERIFY_PARAM *src) { - // Prefer the destination. That is, this function only changes unset - // parameters in |dest|. - return x509_verify_param_merge(dest, src, /*prefer_src=*/false); -} - -int X509_VERIFY_PARAM_set1(X509_VERIFY_PARAM *to, - const X509_VERIFY_PARAM *from) { - // Prefer the source. That is, values in |to| are only preserved if they were - // unset in |from|. - return x509_verify_param_merge(to, from, /*prefer_src=*/true); -} - -static int int_x509_param_set1(char **pdest, size_t *pdestlen, const char *src, - size_t srclen) { +int int_x509_param_set1(char **pdest, size_t *pdestlen, const char *src, + size_t srclen) { void *tmp; if (src == nullptr || srclen == 0) { // Unlike OpenSSL, we do not allow an empty string to disable previously @@ -221,6 +187,107 @@ static int int_x509_param_set1(char **pdest, size_t *pdestlen, const char *src, return 1; } +const X509_VERIFY_PARAM kDefaultParam = { + /*check_time=*/0, + /*flags=*/X509_V_FLAG_ALLOW_SHA1, // TODO(crbug.com/461736992): remove once + // no longer needed. + /*purpose=*/0, + /*trust=*/0, + /*depth=*/100, + /*policies=*/nullptr, + /*hosts=*/nullptr, + /*hostflags=*/0, + /*email=*/nullptr, + /*emaillen=*/0, + /*ip=*/nullptr, + /*iplen=*/0, + /*poison=*/0, +}; + +const X509_VERIFY_PARAM kSMIMESignParam = { + /*check_time=*/0, + /*flags=*/0, + /*purpose=*/X509_PURPOSE_SMIME_SIGN, + /*trust=*/X509_TRUST_EMAIL, + /*depth=*/-1, + /*policies=*/nullptr, + /*hosts=*/nullptr, + /*hostflags=*/0, + /*email=*/nullptr, + /*emaillen=*/0, + /*ip=*/nullptr, + /*iplen=*/0, + /*poison=*/0, +}; + +const X509_VERIFY_PARAM kSSLClientParam = { + /*check_time=*/0, + /*flags=*/0, + /*purpose=*/X509_PURPOSE_SSL_CLIENT, + /*trust=*/X509_TRUST_SSL_CLIENT, + /*depth=*/-1, + /*policies=*/nullptr, + /*hosts=*/nullptr, + /*hostflags=*/0, + /*email=*/nullptr, + /*emaillen=*/0, + /*ip=*/nullptr, + /*iplen=*/0, + /*poison=*/0, +}; + +const X509_VERIFY_PARAM kSSLServerParam = { + /*check_time=*/0, + /*flags=*/0, + /*purpose=*/X509_PURPOSE_SSL_SERVER, + /*trust=*/X509_TRUST_SSL_SERVER, + /*depth=*/-1, + /*policies=*/nullptr, + /*hosts=*/nullptr, + /*hostflags=*/0, + /*email=*/nullptr, + /*emaillen=*/0, + /*ip=*/nullptr, + /*iplen=*/0, + /*poison=*/0, +}; + +} // namespace + +X509_VERIFY_PARAM *X509_VERIFY_PARAM_new() { + X509_VERIFY_PARAM *param = New(); + if (!param) { + return nullptr; + } + param->depth = -1; + return param; +} + +void X509_VERIFY_PARAM_free(X509_VERIFY_PARAM *param) { + if (param == nullptr) { + return; + } + sk_ASN1_OBJECT_pop_free(param->policies, ASN1_OBJECT_free); + sk_OPENSSL_STRING_pop_free(param->hosts, str_free); + OPENSSL_free(param->email); + OPENSSL_free(param->ip); + Delete(param); +} + +int X509_VERIFY_PARAM_inherit(X509_VERIFY_PARAM *dest, + const X509_VERIFY_PARAM *src) { + // Prefer the destination. That is, this function only changes unset + // parameters in `dest`. + return x509_verify_param_merge(dest, src, /*prefer_src=*/false); +} + +int X509_VERIFY_PARAM_set1(X509_VERIFY_PARAM *to, + const X509_VERIFY_PARAM *from) { + // Prefer the source. That is, values in `to` are only preserved if they were + // unset in `from`. + return x509_verify_param_merge(to, from, /*prefer_src=*/true); +} + int X509_VERIFY_PARAM_set_flags(X509_VERIFY_PARAM *param, unsigned long flags) { param->flags |= flags; return 1; @@ -364,70 +431,6 @@ int X509_VERIFY_PARAM_get_depth(const X509_VERIFY_PARAM *param) { return param->depth; } -static const X509_VERIFY_PARAM kDefaultParam = { - /*check_time=*/0, - /*flags=*/X509_V_FLAG_TRUSTED_FIRST, - /*purpose=*/0, - /*trust=*/0, - /*depth=*/100, - /*policies=*/nullptr, - /*hosts=*/nullptr, - /*hostflags=*/0, - /*email=*/nullptr, - /*emaillen=*/0, - /*ip=*/nullptr, - /*iplen=*/0, - /*poison=*/0, -}; - -static const X509_VERIFY_PARAM kSMIMESignParam = { - /*check_time=*/0, - /*flags=*/0, - /*purpose=*/X509_PURPOSE_SMIME_SIGN, - /*trust=*/X509_TRUST_EMAIL, - /*depth=*/-1, - /*policies=*/nullptr, - /*hosts=*/nullptr, - /*hostflags=*/0, - /*email=*/nullptr, - /*emaillen=*/0, - /*ip=*/nullptr, - /*iplen=*/0, - /*poison=*/0, -}; - -static const X509_VERIFY_PARAM kSSLClientParam = { - /*check_time=*/0, - /*flags=*/0, - /*purpose=*/X509_PURPOSE_SSL_CLIENT, - /*trust=*/X509_TRUST_SSL_CLIENT, - /*depth=*/-1, - /*policies=*/nullptr, - /*hosts=*/nullptr, - /*hostflags=*/0, - /*email=*/nullptr, - /*emaillen=*/0, - /*ip=*/nullptr, - /*iplen=*/0, - /*poison=*/0, -}; - -static const X509_VERIFY_PARAM kSSLServerParam = { - /*check_time=*/0, - /*flags=*/0, - /*purpose=*/X509_PURPOSE_SSL_SERVER, - /*trust=*/X509_TRUST_SSL_SERVER, - /*depth=*/-1, - /*policies=*/nullptr, - /*hosts=*/nullptr, - /*hostflags=*/0, - /*email=*/nullptr, - /*emaillen=*/0, - /*ip=*/nullptr, - /*iplen=*/0, - /*poison=*/0, -}; - const X509_VERIFY_PARAM *bssl::X509_VERIFY_PARAM_lookup(const char *name) { if (strcmp(name, "default") == 0) { return &kDefaultParam; diff --git a/third_party/boringssl/src/crypto/x509/x509cset.cc b/third_party/boringssl/src/crypto/x509/x509cset.cc index 19e938a80..5ece53d2c 100644 --- a/third_party/boringssl/src/crypto/x509/x509cset.cc +++ b/third_party/boringssl/src/crypto/x509/x509cset.cc @@ -103,6 +103,15 @@ int X509_CRL_up_ref(X509_CRL *crl) { return 1; } +X509_CRL *X509_CRL_dup_ref(const X509_CRL *crl) { + if (crl == nullptr) { + return nullptr; + } + auto *ptr = const_cast(crl); + CRYPTO_refcount_inc(&ptr->references); + return ptr; +} + long X509_CRL_get_version(const X509_CRL *crl) { return ASN1_INTEGER_get(crl->crl->version); } diff --git a/third_party/boringssl/src/crypto/x509/x509name.cc b/third_party/boringssl/src/crypto/x509/x509name.cc index 4225d10e1..c2b4302d7 100644 --- a/third_party/boringssl/src/crypto/x509/x509name.cc +++ b/third_party/boringssl/src/crypto/x509/x509name.cc @@ -82,7 +82,7 @@ int X509_NAME_entry_count(const X509_NAME *name) { return 0; } const auto *impl = FromOpaque(name); - return (int)sk_X509_NAME_ENTRY_num(impl->entries); + return (int)sk_X509_NAME_ENTRY_num(impl->entries.get()); } int X509_NAME_get_index_by_NID(const X509_NAME *name, int nid, int lastpos) { @@ -105,11 +105,11 @@ int X509_NAME_get_index_by_OBJ(const X509_NAME *name, const ASN1_OBJECT *obj, if (lastpos < 0) { lastpos = -1; } - const STACK_OF(X509_NAME_ENTRY) *sk = impl->entries; + const STACK_OF(X509_NAME_ENTRY) *sk = impl->entries.get(); int n = (int)sk_X509_NAME_ENTRY_num(sk); for (lastpos++; lastpos < n; lastpos++) { const X509_NAME_ENTRY *ne = sk_X509_NAME_ENTRY_value(sk, lastpos); - if (OBJ_cmp(ne->object, obj) == 0) { + if (OBJ_cmp(X509_NAME_ENTRY_get_object(ne), obj) == 0) { return lastpos; } } @@ -121,10 +121,10 @@ X509_NAME_ENTRY *X509_NAME_get_entry(const X509_NAME *name, int loc) { return nullptr; } const auto *impl = FromOpaque(name); - if (loc < 0 || sk_X509_NAME_ENTRY_num(impl->entries) <= (size_t)loc) { + if (loc < 0 || sk_X509_NAME_ENTRY_num(impl->entries.get()) <= (size_t)loc) { return nullptr; } else { - return (sk_X509_NAME_ENTRY_value(impl->entries, loc)); + return (sk_X509_NAME_ENTRY_value(impl->entries.get(), loc)); } } @@ -133,11 +133,11 @@ X509_NAME_ENTRY *X509_NAME_delete_entry(X509_NAME *name, int loc) { return nullptr; } const auto *impl = FromOpaque(name); - if (loc < 0 || sk_X509_NAME_ENTRY_num(impl->entries) <= (size_t)loc) { + if (loc < 0 || sk_X509_NAME_ENTRY_num(impl->entries.get()) <= (size_t)loc) { return nullptr; } - STACK_OF(X509_NAME_ENTRY) *sk = impl->entries; + STACK_OF(X509_NAME_ENTRY) *sk = impl->entries.get(); X509_NAME_ENTRY *ret = sk_X509_NAME_ENTRY_delete(sk, loc); size_t n = sk_X509_NAME_ENTRY_num(sk); x509_name_invalidate_cache(name); @@ -147,17 +147,17 @@ X509_NAME_ENTRY *X509_NAME_delete_entry(X509_NAME *name, int loc) { int set_prev; if (loc != 0) { - set_prev = sk_X509_NAME_ENTRY_value(sk, loc - 1)->set; + set_prev = FromOpaque(sk_X509_NAME_ENTRY_value(sk, loc - 1))->set; } else { - set_prev = ret->set - 1; + set_prev = FromOpaque(ret)->set - 1; } - int set_next = sk_X509_NAME_ENTRY_value(sk, loc)->set; + int set_next = FromOpaque(sk_X509_NAME_ENTRY_value(sk, loc))->set; // If we removed a singleton RDN, update the RDN indices so they are // consecutive again. if (set_prev + 1 < set_next) { for (size_t i = loc; i < n; i++) { - sk_X509_NAME_ENTRY_value(sk, i)->set--; + FromOpaque(sk_X509_NAME_ENTRY_value(sk, i))->set--; } } return ret; @@ -211,13 +211,13 @@ int X509_NAME_add_entry(X509_NAME *name, const X509_NAME_ENTRY *entry, int loc, } auto *impl = FromOpaque(name); if (impl->entries == nullptr) { - impl->entries = sk_X509_NAME_ENTRY_new_null(); + impl->entries.reset(sk_X509_NAME_ENTRY_new_null()); if (impl->entries == nullptr) { return 0; } } - STACK_OF(X509_NAME_ENTRY) *sk = impl->entries; + STACK_OF(X509_NAME_ENTRY) *sk = impl->entries.get(); int n = (int)sk_X509_NAME_ENTRY_num(sk); if (loc > n) { loc = n; @@ -233,17 +233,17 @@ int X509_NAME_add_entry(X509_NAME *name, const X509_NAME_ENTRY *entry, int loc, set = 0; inc = 1; } else { - set = sk_X509_NAME_ENTRY_value(sk, loc - 1)->set; + set = FromOpaque(sk_X509_NAME_ENTRY_value(sk, loc - 1))->set; } } else { // if (set >= 0) if (loc >= n) { if (loc != 0) { - set = sk_X509_NAME_ENTRY_value(sk, loc - 1)->set + 1; + set = FromOpaque(sk_X509_NAME_ENTRY_value(sk, loc - 1))->set + 1; } else { set = 0; } } else { - set = sk_X509_NAME_ENTRY_value(sk, loc)->set; + set = FromOpaque(sk_X509_NAME_ENTRY_value(sk, loc))->set; } } @@ -251,15 +251,15 @@ int X509_NAME_add_entry(X509_NAME *name, const X509_NAME_ENTRY *entry, int loc, if (new_entry == nullptr) { return 0; } - new_entry->set = set; + FromOpaque(new_entry.get())->set = set; if (!sk_X509_NAME_ENTRY_insert(sk, new_entry.get(), loc)) { return 0; } - new_entry.release(); // |sk| took ownership. + new_entry.release(); // `sk` took ownership. if (inc) { n = (int)sk_X509_NAME_ENTRY_num(sk); for (int i = loc + 1; i < n; i++) { - sk_X509_NAME_ENTRY_value(sk, i)->set += 1; + FromOpaque(sk_X509_NAME_ENTRY_value(sk, i))->set += 1; } } return 1; @@ -328,33 +328,35 @@ X509_NAME_ENTRY *X509_NAME_ENTRY_create_by_OBJ(X509_NAME_ENTRY **ne, } int X509_NAME_ENTRY_set_object(X509_NAME_ENTRY *ne, const ASN1_OBJECT *obj) { - if ((ne == nullptr) || (obj == nullptr)) { + auto *ne_impl = FromOpaque(ne); + if (ne_impl == nullptr || obj == nullptr) { OPENSSL_PUT_ERROR(X509, ERR_R_PASSED_NULL_PARAMETER); return 0; } - ASN1_OBJECT_free(ne->object); - ne->object = OBJ_dup(obj); - return ((ne->object == nullptr) ? 0 : 1); + ne_impl->object.reset(OBJ_dup(obj)); + return ne_impl->object != nullptr; } int X509_NAME_ENTRY_set_data(X509_NAME_ENTRY *ne, int type, const unsigned char *bytes, ossl_ssize_t len) { - if ((ne == nullptr) || ((bytes == nullptr) && (len != 0))) { + auto *ne_impl = FromOpaque(ne); + if (ne_impl == nullptr || (bytes == nullptr && len != 0)) { return 0; } if ((type > 0) && (type & MBSTRING_FLAG)) { - ASN1_STRING *dst = &ne->value; + ASN1_STRING *dst = ne_impl->value.get(); return ASN1_STRING_set_by_NID(&dst, bytes, len, type, - OBJ_obj2nid(ne->object)) != nullptr; + OBJ_obj2nid(ne_impl->object.get())) != + nullptr; } if (len < 0) { len = strlen((const char *)bytes); } - if (!ASN1_STRING_set(&ne->value, bytes, len)) { + if (!ASN1_STRING_set(ne_impl->value.get(), bytes, len)) { return 0; } if (type != V_ASN1_UNDEF) { - ne->value.type = type; + ne_impl->value->type = type; } return 1; } @@ -363,7 +365,7 @@ ASN1_OBJECT *X509_NAME_ENTRY_get_object(const X509_NAME_ENTRY *ne) { if (ne == nullptr) { return nullptr; } - return ne->object; + return FromOpaque(ne)->object.get(); } ASN1_STRING *X509_NAME_ENTRY_get_data(const X509_NAME_ENTRY *ne) { @@ -371,5 +373,5 @@ ASN1_STRING *X509_NAME_ENTRY_get_data(const X509_NAME_ENTRY *ne) { return nullptr; } // This function is not const-correct for OpenSSL compatibility. - return const_cast(&ne->value); + return const_cast(FromOpaque(ne)->value.get()); } diff --git a/third_party/boringssl/src/crypto/x509/x509rset.cc b/third_party/boringssl/src/crypto/x509/x509rset.cc index 076a3bfa7..010d453bd 100644 --- a/third_party/boringssl/src/crypto/x509/x509rset.cc +++ b/third_party/boringssl/src/crypto/x509/x509rset.cc @@ -31,14 +31,14 @@ int X509_REQ_set_version(X509_REQ *x, long version) { return ASN1_INTEGER_set_int64(x->req_info->version, version); } -int X509_REQ_set_subject_name(X509_REQ *x, X509_NAME *name) { +int X509_REQ_set_subject_name(X509_REQ *x, const X509_NAME *name) { if ((x == nullptr) || (x->req_info == nullptr)) { return 0; } return (X509_NAME_set(&x->req_info->subject, name)); } -int X509_REQ_set_pubkey(X509_REQ *x, EVP_PKEY *pkey) { +int X509_REQ_set_pubkey(X509_REQ *x, const EVP_PKEY *pkey) { if ((x == nullptr) || (x->req_info == nullptr)) { return 0; } diff --git a/third_party/boringssl/src/crypto/x509/x509spki.cc b/third_party/boringssl/src/crypto/x509/x509spki.cc index 67b07997f..c630c0b68 100644 --- a/third_party/boringssl/src/crypto/x509/x509spki.cc +++ b/third_party/boringssl/src/crypto/x509/x509spki.cc @@ -19,7 +19,7 @@ #include #include -int NETSCAPE_SPKI_set_pubkey(NETSCAPE_SPKI *x, EVP_PKEY *pkey) { +int NETSCAPE_SPKI_set_pubkey(NETSCAPE_SPKI *x, const EVP_PKEY *pkey) { if ((x == nullptr) || (x->spkac == nullptr)) { return 0; } @@ -64,7 +64,7 @@ NETSCAPE_SPKI *NETSCAPE_SPKI_b64_decode(const char *str, ossl_ssize_t len) { // Generate a base64 encoded string from an SPKI -char *NETSCAPE_SPKI_b64_encode(NETSCAPE_SPKI *spki) { +char *NETSCAPE_SPKI_b64_encode(const NETSCAPE_SPKI *spki) { unsigned char *der_spki, *p; char *b64_str; size_t b64_len; diff --git a/third_party/boringssl/src/crypto/x509/x_algor.cc b/third_party/boringssl/src/crypto/x509/x_algor.cc index b20c50927..93926c302 100644 --- a/third_party/boringssl/src/crypto/x509/x_algor.cc +++ b/third_party/boringssl/src/crypto/x509/x_algor.cc @@ -112,9 +112,9 @@ int i2d_X509_ALGOR(const X509_ALGOR *in, uint8_t **outp) { }); } -IMPLEMENT_EXTERN_ASN1_SIMPLE(X509_ALGOR, X509_ALGOR_new, X509_ALGOR_free, - CBS_ASN1_SEQUENCE, x509_parse_algorithm, - i2d_X509_ALGOR) +IMPLEMENT_EXTERN_ASN1_PARSE_INTO(X509_ALGOR, X509_ALGOR_new, X509_ALGOR_free, + CBS_ASN1_SEQUENCE, x509_parse_algorithm, + x509_marshal_algorithm) X509_ALGOR *X509_ALGOR_dup(const X509_ALGOR *alg) { UniquePtr copy(X509_ALGOR_new()); @@ -209,15 +209,10 @@ int X509_ALGOR_set_md(X509_ALGOR *alg, const EVP_MD *md) { nullptr); } -// X509_ALGOR_cmp returns 0 if |a| and |b| are equal and non-zero otherwise. int X509_ALGOR_cmp(const X509_ALGOR *a, const X509_ALGOR *b) { - int rv; - rv = OBJ_cmp(a->algorithm, b->algorithm); - if (rv) { - return rv; - } - if (!a->parameter && !b->parameter) { - return 0; + int ret = OBJ_cmp(a->algorithm, b->algorithm); + if (ret != 0) { + return ret; } return ASN1_TYPE_cmp(a->parameter, b->parameter); } diff --git a/third_party/boringssl/src/crypto/x509/x_all.cc b/third_party/boringssl/src/crypto/x509/x_all.cc index e9e59c432..08c32c921 100644 --- a/third_party/boringssl/src/crypto/x509/x_all.cc +++ b/third_party/boringssl/src/crypto/x509/x_all.cc @@ -35,27 +35,28 @@ using namespace bssl; -int X509_verify(const X509 *x509, EVP_PKEY *pkey) { +int X509_verify(const X509 *x509, const EVP_PKEY *pkey) { auto *impl = FromOpaque(x509); - if (X509_ALGOR_cmp(&impl->sig_alg, &impl->tbs_sig_alg)) { + if (X509_ALGOR_cmp(impl->sig_alg.get(), impl->tbs_sig_alg.get())) { OPENSSL_PUT_ERROR(X509, X509_R_SIGNATURE_ALGORITHM_MISMATCH); return 0; } // This uses the cached TBSCertificate encoding, if any. - ScopedCBB cbb; - if (!CBB_init(cbb.get(), 128) || !x509_marshal_tbs_cert(cbb.get(), x509)) { + Array scratch; + CBS tbs; + if (!x509_get_or_marshal_tbs_cert(&tbs, &scratch, x509)) { return 0; } - return x509_verify_signature(&impl->sig_alg, &impl->signature, - CBBAsSpan(cbb.get()), pkey); + return x509_verify_signature(impl->sig_alg.get(), impl->signature.get(), tbs, + pkey); } -int X509_REQ_verify(const X509_REQ *req, EVP_PKEY *pkey) { +int X509_REQ_verify(const X509_REQ *req, const EVP_PKEY *pkey) { return ASN1_item_verify(ASN1_ITEM_rptr(X509_REQ_INFO), req->sig_alg, req->signature, req->req_info, pkey); } -int X509_sign(X509 *x, EVP_PKEY *pkey, const EVP_MD *md) { +int X509_sign(X509 *x, const EVP_PKEY *pkey, const EVP_MD *md) { ScopedEVP_MD_CTX ctx; if (!EVP_DigestSignInit(ctx.get(), nullptr, md, nullptr, pkey)) { return 0; @@ -64,7 +65,7 @@ int X509_sign(X509 *x, EVP_PKEY *pkey, const EVP_MD *md) { } int X509_sign_ctx(X509 *x, EVP_MD_CTX *ctx) { - // Historically, this function called |EVP_MD_CTX_cleanup| on return. Some + // Historically, this function called `EVP_MD_CTX_cleanup` on return. Some // callers rely on this to avoid memory leaks. Cleanup cleanup = [&] { EVP_MD_CTX_cleanup(ctx); }; @@ -72,23 +73,23 @@ int X509_sign_ctx(X509 *x, EVP_MD_CTX *ctx) { // Fill in the two copies of AlgorithmIdentifier. Note one of these modifies // the TBSCertificate. - if (!x509_digest_sign_algorithm(ctx, &impl->tbs_sig_alg) || - !x509_digest_sign_algorithm(ctx, &impl->sig_alg)) { + if (!x509_digest_sign_algorithm(ctx, impl->tbs_sig_alg.get()) || + !x509_digest_sign_algorithm(ctx, impl->sig_alg.get())) { return 0; } // Discard the cached encoding. (We just modified it.) - CRYPTO_BUFFER_free(impl->buf); impl->buf = nullptr; ScopedCBB cbb; if (!CBB_init(cbb.get(), 128) || !x509_marshal_tbs_cert(cbb.get(), x)) { return 0; } - return x509_sign_to_bit_string(ctx, &impl->signature, CBBAsSpan(cbb.get())); + return x509_sign_to_bit_string(ctx, impl->signature.get(), + CBBAsSpan(cbb.get())); } -int X509_REQ_sign(X509_REQ *x, EVP_PKEY *pkey, const EVP_MD *md) { +int X509_REQ_sign(X509_REQ *x, const EVP_PKEY *pkey, const EVP_MD *md) { asn1_encoding_clear(&x->req_info->enc); return ASN1_item_sign(ASN1_ITEM_rptr(X509_REQ_INFO), x->sig_alg, nullptr, x->signature, x->req_info, pkey, md); @@ -100,7 +101,7 @@ int X509_REQ_sign_ctx(X509_REQ *x, EVP_MD_CTX *ctx) { x->signature, x->req_info, ctx); } -int X509_CRL_sign(X509_CRL *x, EVP_PKEY *pkey, const EVP_MD *md) { +int X509_CRL_sign(X509_CRL *x, const EVP_PKEY *pkey, const EVP_MD *md) { asn1_encoding_clear(&x->crl->enc); return ASN1_item_sign(ASN1_ITEM_rptr(X509_CRL_INFO), x->crl->sig_alg, x->sig_alg, x->signature, x->crl, pkey, md); @@ -112,12 +113,13 @@ int X509_CRL_sign_ctx(X509_CRL *x, EVP_MD_CTX *ctx) { x->sig_alg, x->signature, x->crl, ctx); } -int NETSCAPE_SPKI_sign(NETSCAPE_SPKI *x, EVP_PKEY *pkey, const EVP_MD *md) { +int NETSCAPE_SPKI_sign(NETSCAPE_SPKI *x, const EVP_PKEY *pkey, + const EVP_MD *md) { return ASN1_item_sign(ASN1_ITEM_rptr(NETSCAPE_SPKAC), x->sig_algor, nullptr, x->signature, x->spkac, pkey, md); } -int NETSCAPE_SPKI_verify(NETSCAPE_SPKI *spki, EVP_PKEY *pkey) { +int NETSCAPE_SPKI_verify(const NETSCAPE_SPKI *spki, const EVP_PKEY *pkey) { return ASN1_item_verify(ASN1_ITEM_rptr(NETSCAPE_SPKAC), spki->sig_algor, spki->signature, spki->spkac, pkey); } diff --git a/third_party/boringssl/src/crypto/x509/x_crl.cc b/third_party/boringssl/src/crypto/x509/x_crl.cc index f2e0f797e..05af8b44b 100644 --- a/third_party/boringssl/src/crypto/x509/x_crl.cc +++ b/third_party/boringssl/src/crypto/x509/x_crl.cc @@ -31,30 +31,19 @@ using namespace bssl; -static int X509_REVOKED_cmp(const X509_REVOKED *const *a, - const X509_REVOKED *const *b); -static int setup_idp(X509_CRL *crl, ISSUING_DIST_POINT *idp); +namespace { -BSSL_NAMESPACE_BEGIN - -ASN1_SEQUENCE(X509_REVOKED) = { - ASN1_SIMPLE(X509_REVOKED, serialNumber, ASN1_INTEGER), - ASN1_SIMPLE(X509_REVOKED, revocationDate, ASN1_TIME), - ASN1_SEQUENCE_OF_OPT(X509_REVOKED, extensions, X509_EXTENSION), -} ASN1_SEQUENCE_END(X509_REVOKED) - -BSSL_NAMESPACE_END - -static int crl_lookup(X509_CRL *crl, X509_REVOKED **ret, - const ASN1_INTEGER *serial, const X509_NAME *issuer); +int X509_REVOKED_cmp(const X509_REVOKED *const *a, + const X509_REVOKED *const *b) { + return ASN1_STRING_cmp((*a)->serialNumber, (*b)->serialNumber); +} // The X509_CRL_INFO structure needs a bit of customisation. Since we cache // the original encoding the signature won't be affected by reordering of the // revoked field. -static int crl_inf_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, - void *exarg) { - X509_CRL_INFO *a = (X509_CRL_INFO *)*pval; - +int crl_inf_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, + void *exarg) { + X509_CRL_INFO *a = asn1_load_ptr_as(pval); if (!a || !a->revoked) { return 1; } @@ -68,21 +57,7 @@ static int crl_inf_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, return 1; } -BSSL_NAMESPACE_BEGIN - -ASN1_SEQUENCE_enc(X509_CRL_INFO, enc, crl_inf_cb) = { - ASN1_OPT(X509_CRL_INFO, version, ASN1_INTEGER), - ASN1_SIMPLE(X509_CRL_INFO, sig_alg, X509_ALGOR), - ASN1_SIMPLE(X509_CRL_INFO, issuer, X509_NAME), - ASN1_SIMPLE(X509_CRL_INFO, lastUpdate, ASN1_TIME), - ASN1_OPT(X509_CRL_INFO, nextUpdate, ASN1_TIME), - ASN1_SEQUENCE_OF_OPT(X509_CRL_INFO, revoked, X509_REVOKED), - ASN1_EXP_SEQUENCE_OF_OPT(X509_CRL_INFO, extensions, X509_EXTENSION, 0), -} ASN1_SEQUENCE_END_enc(X509_CRL_INFO, X509_CRL_INFO) - -BSSL_NAMESPACE_END - -static int crl_parse_entry_extensions(X509_CRL *crl) { +int crl_parse_entry_extensions(X509_CRL *crl) { long version = ASN1_INTEGER_get(crl->crl->version); STACK_OF(X509_REVOKED) *revoked = X509_CRL_get_REVOKED(crl); for (size_t i = 0; i < sk_X509_REVOKED_num(revoked); i++) { @@ -132,9 +107,8 @@ static int crl_parse_entry_extensions(X509_CRL *crl) { // The X509_CRL structure needs a bit of customisation. Cache some extensions // and hash of the whole CRL. -static int crl_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, - void *exarg) { - X509_CRL *crl = (X509_CRL *)*pval; +int crl_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, void *exarg) { + X509_CRL *crl = asn1_load_ptr_as(pval); int i; switch (operation) { @@ -142,7 +116,6 @@ static int crl_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, crl->idp = nullptr; crl->akid = nullptr; crl->flags = 0; - crl->idp_flags = 0; break; case ASN1_OP_D2I_POST: { @@ -177,11 +150,7 @@ static int crl_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, crl->idp = reinterpret_cast(X509_CRL_get_ext_d2i( crl, NID_issuing_distribution_point, &i, nullptr)); - if (crl->idp != nullptr) { - if (!setup_idp(crl, crl->idp)) { - return 0; - } - } else if (i != -1) { + if (crl->idp == nullptr && i != -1) { return 0; } @@ -224,51 +193,74 @@ static int crl_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, return 1; } -// Convert IDP into a more convenient form -// -// TODO(davidben): Each of these flags are already booleans, so this is not -// really more convenient. We can probably remove |idp_flags|. -static int setup_idp(X509_CRL *crl, ISSUING_DIST_POINT *idp) { - int idp_only = 0; - // Set various flags according to IDP - crl->idp_flags |= IDP_PRESENT; - if (idp->onlyuser > 0) { - idp_only++; - crl->idp_flags |= IDP_ONLYUSER; - } - if (idp->onlyCA > 0) { - idp_only++; - crl->idp_flags |= IDP_ONLYCA; - } - if (idp->onlyattr > 0) { - idp_only++; - crl->idp_flags |= IDP_ONLYATTR; - } +int crl_revoked_issuer_match(const X509_CRL *crl, const X509_NAME *nm, + const X509_REVOKED *rev) { + return nm == nullptr || X509_NAME_cmp(nm, X509_CRL_get_issuer(crl)) == 0; +} - // Per RFC 5280, section 5.2.5, at most one of onlyContainsUserCerts, - // onlyContainsCACerts, and onlyContainsAttributeCerts may be true. - // - // TODO(crbug.com/boringssl/443): Move this check to the |ISSUING_DIST_POINT| - // parser. - if (idp_only > 1) { - crl->idp_flags |= IDP_INVALID; - } +StaticMutex g_crl_sort_lock; - if (idp->indirectCRL > 0) { - crl->idp_flags |= IDP_INDIRECT; - } +int crl_lookup(X509_CRL *crl, X509_REVOKED **ret, const ASN1_INTEGER *serial, + const X509_NAME *issuer) { + // Use an assert, rather than a runtime error, because returning nothing for a + // CRL is arguably failing open, rather than closed. + assert(serial->type == V_ASN1_INTEGER || serial->type == V_ASN1_NEG_INTEGER); + X509_REVOKED rtmp; + size_t idx; + rtmp.serialNumber = (ASN1_INTEGER *)serial; + // Sort revoked into serial number order if not already sorted. Do this + // under a lock to avoid race condition. + + g_crl_sort_lock.LockRead(); + const int is_sorted = sk_X509_REVOKED_is_sorted(crl->crl->revoked); + g_crl_sort_lock.UnlockRead(); - if (idp->onlysomereasons) { - crl->idp_flags |= IDP_REASONS; + if (!is_sorted) { + MutexWriteLock lock(&g_crl_sort_lock); + if (!sk_X509_REVOKED_is_sorted(crl->crl->revoked)) { + sk_X509_REVOKED_sort(crl->crl->revoked); + } } - // TODO(davidben): The new verifier does not support nameRelativeToCRLIssuer. - // Remove this? - return DIST_POINT_set_dpname(idp->distpoint, X509_CRL_get_issuer(crl)); + if (!sk_X509_REVOKED_find(crl->crl->revoked, &idx, &rtmp)) { + return 0; + } + // Need to look for matching name + for (; idx < sk_X509_REVOKED_num(crl->crl->revoked); idx++) { + X509_REVOKED *rev = sk_X509_REVOKED_value(crl->crl->revoked, idx); + if (ASN1_INTEGER_cmp(rev->serialNumber, serial)) { + return 0; + } + if (crl_revoked_issuer_match(crl, issuer, rev)) { + if (ret) { + *ret = rev; + } + return 1; + } + } + return 0; } +} // namespace + BSSL_NAMESPACE_BEGIN +ASN1_SEQUENCE(X509_REVOKED) = { + ASN1_SIMPLE(X509_REVOKED, serialNumber, ASN1_INTEGER), + ASN1_SIMPLE(X509_REVOKED, revocationDate, ASN1_TIME), + ASN1_SEQUENCE_OF_OPT(X509_REVOKED, extensions, X509_EXTENSION), +} ASN1_SEQUENCE_END(X509_REVOKED) + +ASN1_SEQUENCE_enc(X509_CRL_INFO, enc, crl_inf_cb) = { + ASN1_OPT(X509_CRL_INFO, version, ASN1_INTEGER), + ASN1_SIMPLE(X509_CRL_INFO, sig_alg, X509_ALGOR), + ASN1_SIMPLE(X509_CRL_INFO, issuer, X509_NAME), + ASN1_SIMPLE(X509_CRL_INFO, lastUpdate, ASN1_TIME), + ASN1_OPT(X509_CRL_INFO, nextUpdate, ASN1_TIME), + ASN1_SEQUENCE_OF_OPT(X509_CRL_INFO, revoked, X509_REVOKED), + ASN1_EXP_SEQUENCE_OF_OPT(X509_CRL_INFO, extensions, X509_EXTENSION, 0), +} ASN1_SEQUENCE_END_enc(X509_CRL_INFO, X509_CRL_INFO) + ASN1_SEQUENCE_ref(X509_CRL, crl_cb) = { ASN1_SIMPLE(X509_CRL, crl, X509_CRL_INFO), ASN1_SIMPLE(X509_CRL, sig_alg, X509_ALGOR), @@ -289,11 +281,6 @@ BSSL_NAMESPACE_END IMPLEMENT_ASN1_FUNCTIONS_const(X509_CRL) IMPLEMENT_ASN1_DUP_FUNCTION_const(X509_CRL) -static int X509_REVOKED_cmp(const X509_REVOKED *const *a, - const X509_REVOKED *const *b) { - return ASN1_STRING_cmp((*a)->serialNumber, (*b)->serialNumber); -} - int X509_CRL_add0_revoked(X509_CRL *crl, X509_REVOKED *rev) { X509_CRL_INFO *inf = crl->crl; if (!inf->revoked) { @@ -306,7 +293,7 @@ int X509_CRL_add0_revoked(X509_CRL *crl, X509_REVOKED *rev) { return 1; } -int X509_CRL_verify(const X509_CRL *crl, EVP_PKEY *pkey) { +int X509_CRL_verify(const X509_CRL *crl, const EVP_PKEY *pkey) { if (X509_ALGOR_cmp(crl->sig_alg, crl->crl->sig_alg) != 0) { OPENSSL_PUT_ERROR(X509, X509_R_SIGNATURE_ALGORITHM_MISMATCH); return 0; @@ -325,51 +312,3 @@ int X509_CRL_get0_by_cert(X509_CRL *crl, X509_REVOKED **ret, const X509 *x) { return crl_lookup(crl, ret, X509_get0_serialNumber(x), X509_get_issuer_name(x)); } - -static int crl_revoked_issuer_match(const X509_CRL *crl, const X509_NAME *nm, - const X509_REVOKED *rev) { - return nm == nullptr || X509_NAME_cmp(nm, X509_CRL_get_issuer(crl)) == 0; -} - -static StaticMutex g_crl_sort_lock; - -static int crl_lookup(X509_CRL *crl, X509_REVOKED **ret, - const ASN1_INTEGER *serial, const X509_NAME *issuer) { - // Use an assert, rather than a runtime error, because returning nothing for a - // CRL is arguably failing open, rather than closed. - assert(serial->type == V_ASN1_INTEGER || serial->type == V_ASN1_NEG_INTEGER); - X509_REVOKED rtmp; - size_t idx; - rtmp.serialNumber = (ASN1_INTEGER *)serial; - // Sort revoked into serial number order if not already sorted. Do this - // under a lock to avoid race condition. - - g_crl_sort_lock.LockRead(); - const int is_sorted = sk_X509_REVOKED_is_sorted(crl->crl->revoked); - g_crl_sort_lock.UnlockRead(); - - if (!is_sorted) { - MutexWriteLock lock(&g_crl_sort_lock); - if (!sk_X509_REVOKED_is_sorted(crl->crl->revoked)) { - sk_X509_REVOKED_sort(crl->crl->revoked); - } - } - - if (!sk_X509_REVOKED_find(crl->crl->revoked, &idx, &rtmp)) { - return 0; - } - // Need to look for matching name - for (; idx < sk_X509_REVOKED_num(crl->crl->revoked); idx++) { - X509_REVOKED *rev = sk_X509_REVOKED_value(crl->crl->revoked, idx); - if (ASN1_INTEGER_cmp(rev->serialNumber, serial)) { - return 0; - } - if (crl_revoked_issuer_match(crl, issuer, rev)) { - if (ret) { - *ret = rev; - } - return 1; - } - } - return 0; -} diff --git a/third_party/boringssl/src/crypto/x509/x_name.cc b/third_party/boringssl/src/crypto/x509/x_name.cc index 66ba94f08..4cdae37c0 100644 --- a/third_party/boringssl/src/crypto/x509/x_name.cc +++ b/third_party/boringssl/src/crypto/x509/x_name.cc @@ -37,40 +37,91 @@ using namespace bssl; -// X509_NAME_MAX is the length of the maximum encoded |X509_NAME| we accept. +// X509_NAME_MAX is the length of the maximum encoded `X509_NAME` we accept. #define X509_NAME_MAX (1024 * 1024) -static int asn1_marshal_string_canon(CBB *cbb, const ASN1_STRING *in); +namespace { -X509_NAME_ENTRY *X509_NAME_ENTRY_new() { - UniquePtr ret = MakeUnique(); - if (ret == nullptr) { - return nullptr; +int asn1_marshal_string_canon(CBB *cbb, const ASN1_STRING *in) { + int (*decode_func)(CBS *, uint32_t *); + int error; + switch (in->type) { + case V_ASN1_UTF8STRING: + decode_func = CBS_get_utf8; + error = ASN1_R_INVALID_UTF8STRING; + break; + case V_ASN1_BMPSTRING: + decode_func = CBS_get_ucs2_be; + error = ASN1_R_INVALID_BMPSTRING; + break; + case V_ASN1_UNIVERSALSTRING: + decode_func = CBS_get_utf32_be; + error = ASN1_R_INVALID_UNIVERSALSTRING; + break; + case V_ASN1_PRINTABLESTRING: + case V_ASN1_T61STRING: + case V_ASN1_IA5STRING: + case V_ASN1_VISIBLESTRING: + decode_func = CBS_get_latin1; + error = ERR_R_INTERNAL_ERROR; // Latin-1 inputs are never invalid. + break; + default: + // Other string types are not canonicalized. + return asn1_marshal_any_string(cbb, in); } - ret->object = const_cast(OBJ_get_undef()); - asn1_string_init(&ret->value, -1); - ret->set = 0; - return ret.release(); -} -void X509_NAME_ENTRY_free(X509_NAME_ENTRY *entry) { - if (entry != nullptr) { - ASN1_OBJECT_free(entry->object); - asn1_string_cleanup(&entry->value); - Delete(entry); + CBB child; + if (!CBB_add_asn1(cbb, &child, CBS_ASN1_UTF8STRING)) { + return 0; } + + bool empty = true; + bool in_whitespace = false; + CBS cbs; + CBS_init(&cbs, in->data, in->length); + while (CBS_len(&cbs) != 0) { + uint32_t c; + if (!decode_func(&cbs, &c)) { + OPENSSL_PUT_ERROR(ASN1, error); + return 0; + } + if (OPENSSL_isspace(c)) { + if (empty) { + continue; // Trim leading whitespace. + } + in_whitespace = true; + } else { + if (in_whitespace) { + // Collapse the previous run of whitespace into one space. + if (!CBB_add_u8(&child, ' ')) { + return 0; + } + } + in_whitespace = false; + // Lowecase ASCII codepoints. + if (c <= 0x7f) { + c = OPENSSL_tolower(c); + } + if (!CBB_add_utf8(&child, c)) { + return 0; + } + empty = false; + } + } + + return CBB_flush(cbb); } -static int x509_parse_name_entry(CBS *cbs, X509_NAME_ENTRY *out) { +int x509_parse_name_entry(CBS *cbs, X509_NAME_ENTRY *out) { + auto *out_impl = FromOpaque(out); CBS seq; if (!CBS_get_asn1(cbs, &seq, CBS_ASN1_SEQUENCE)) { OPENSSL_PUT_ERROR(ASN1, ASN1_R_DECODE_ERROR); return 0; } - ASN1_OBJECT_free(out->object); - out->object = asn1_parse_object(&seq, /*tag=*/0); - if (out->object == nullptr || // - !asn1_parse_any_as_string(&seq, &out->value) || // + out_impl->object.reset(asn1_parse_object(&seq, /*tag=*/0)); + if (out_impl->object == nullptr || // + !asn1_parse_any_as_string(&seq, out_impl->value.get()) || // CBS_len(&seq) != 0) { OPENSSL_PUT_ERROR(ASN1, ASN1_R_DECODE_ERROR); return 0; @@ -78,32 +129,74 @@ static int x509_parse_name_entry(CBS *cbs, X509_NAME_ENTRY *out) { return 1; } -static int x509_marshal_name_entry(CBB *cbb, const X509_NAME_ENTRY *entry, - int canonicalize) { +int x509_marshal_name_entry(CBB *cbb, const X509_NAME_ENTRY *entry, + int canonicalize) { + auto *entry_impl = FromOpaque(entry); CBB seq; if (!CBB_add_asn1(cbb, &seq, CBS_ASN1_SEQUENCE) || - !asn1_marshal_object(&seq, entry->object, /*tag=*/0)) { + !asn1_marshal_object(&seq, entry_impl->object.get(), /*tag=*/0)) { return 0; } - int ok = canonicalize ? asn1_marshal_string_canon(&seq, &entry->value) - : asn1_marshal_any_string(&seq, &entry->value); + int ok = canonicalize + ? asn1_marshal_string_canon(&seq, entry_impl->value.get()) + : asn1_marshal_any_string(&seq, entry_impl->value.get()); if (!ok) { return 0; } return CBB_flush(cbb); } -static int i2d_x509_name_entry(const X509_NAME_ENTRY *entry, uint8_t **out) { - return I2DFromCBB(/*initial_capacity=*/16, out, [&](CBB *cbb) -> bool { - return x509_marshal_name_entry(cbb, entry, /*canonicalize=*/0); - }); +int x509_marshal_name_entry_no_canon(CBB *cbb, const X509_NAME_ENTRY *entry) { + return x509_marshal_name_entry(cbb, entry, /*canonicalize=*/0); } +int x509_marshal_name_entries(CBB *out, const X509_NAME *name, + int canonicalize) { + auto *impl = FromOpaque(name); + if (sk_X509_NAME_ENTRY_num(impl->entries.get()) == 0) { + return 1; + } + + // Bootstrap the first RDN. + int set = FromOpaque(sk_X509_NAME_ENTRY_value(impl->entries.get(), 0))->set; + CBB rdn; + if (!CBB_add_asn1(out, &rdn, CBS_ASN1_SET)) { + return 0; + } + + for (const X509_NAME_ENTRY *entry : impl->entries.get()) { + if (FromOpaque(entry)->set != set) { + // Flush the previous RDN and start a new one. + if (!CBB_flush_asn1_set_of(&rdn) || + !CBB_add_asn1(out, &rdn, CBS_ASN1_SET)) { + return 0; + } + set = FromOpaque(entry)->set; + } + if (!x509_marshal_name_entry(&rdn, entry, canonicalize)) { + return 0; + } + } + + return CBB_flush_asn1_set_of(&rdn) && CBB_flush(out); +} + +} // namespace + +bssl::X509NameEntry::X509NameEntry() { + object.reset(const_cast(OBJ_get_undef())); +} + +X509_NAME_ENTRY *X509_NAME_ENTRY_new() { return New(); } + +void X509_NAME_ENTRY_free(X509_NAME_ENTRY *entry) { Delete(FromOpaque(entry)); } + BSSL_NAMESPACE_BEGIN -IMPLEMENT_EXTERN_ASN1_SIMPLE(X509_NAME_ENTRY, X509_NAME_ENTRY_new, - X509_NAME_ENTRY_free, CBS_ASN1_SEQUENCE, - x509_parse_name_entry, i2d_x509_name_entry) +IMPLEMENT_EXTERN_ASN1_PARSE_INTO(X509_NAME_ENTRY, X509_NAME_ENTRY_new, + X509_NAME_ENTRY_free, CBS_ASN1_SEQUENCE, + x509_parse_name_entry, + x509_marshal_name_entry_no_canon) BSSL_NAMESPACE_END @@ -122,33 +215,18 @@ X509_NAME_ENTRY *X509_NAME_ENTRY_dup(const X509_NAME_ENTRY *entry) { return copy.release(); } -void bssl::x509_name_init(X509_NAME *name) { - auto *impl = FromOpaque(name); - OPENSSL_memset(impl, 0, sizeof(*impl)); -} - -void bssl::x509_name_cleanup(X509_NAME *name) { - auto *impl = FromOpaque(name); - sk_X509_NAME_ENTRY_pop_free(impl->entries, X509_NAME_ENTRY_free); - Delete(impl->cache.exchange(nullptr)); -} +bssl::X509Name::~X509Name() { Delete(cache.exchange(nullptr)); } X509_NAME *X509_NAME_new() { return New(); } -void X509_NAME_free(X509_NAME *name) { - if (name != nullptr) { - x509_name_cleanup(name); - Delete(FromOpaque(name)); - } -} +void X509_NAME_free(X509_NAME *name) { Delete(FromOpaque(name)); } int bssl::x509_parse_name(CBS *cbs, X509_NAME *out) { auto *impl = FromOpaque(out); - // Reset the old state. - x509_name_cleanup(impl); - x509_name_init(impl); + impl->entries = nullptr; + x509_name_invalidate_cache(impl); - impl->entries = sk_X509_NAME_ENTRY_new_null(); + impl->entries.reset(sk_X509_NAME_ENTRY_new_null()); if (impl->entries == nullptr) { return 0; } @@ -167,12 +245,12 @@ int bssl::x509_parse_name(CBS *cbs, X509_NAME *out) { return 0; } while (CBS_len(&rdn) != 0) { - UniquePtr entry(X509_NAME_ENTRY_new()); + auto entry = MakeUnique(); if (entry == nullptr || !x509_parse_name_entry(&rdn, entry.get())) { return 0; } entry->set = set; - if (!PushToStack(impl->entries, std::move(entry))) { + if (!PushToStack(impl->entries.get(), std::move(entry))) { return 0; } } @@ -182,45 +260,14 @@ int bssl::x509_parse_name(CBS *cbs, X509_NAME *out) { return x509_name_get_cache(impl) != nullptr; } -static int x509_marshal_name_entries(CBB *out, const X509_NAME *name, - int canonicalize) { - auto *impl = FromOpaque(name); - if (sk_X509_NAME_ENTRY_num(impl->entries) == 0) { - return 1; - } - - // Bootstrap the first RDN. - int set = sk_X509_NAME_ENTRY_value(impl->entries, 0)->set; - CBB rdn; - if (!CBB_add_asn1(out, &rdn, CBS_ASN1_SET)) { - return 0; - } - - for (const X509_NAME_ENTRY *entry : impl->entries) { - if (entry->set != set) { - // Flush the previous RDN and start a new one. - if (!CBB_flush_asn1_set_of(&rdn) || - !CBB_add_asn1(out, &rdn, CBS_ASN1_SET)) { - return 0; - } - set = entry->set; - } - if (!x509_marshal_name_entry(&rdn, entry, canonicalize)) { - return 0; - } - } - - return CBB_flush_asn1_set_of(&rdn) && CBB_flush(out); -} - -const X509_NAME_CACHE *bssl::x509_name_get_cache(const X509_NAME *name) { +const X509NameCache *bssl::x509_name_get_cache(const X509_NAME *name) { auto *impl = FromOpaque(name); - const X509_NAME_CACHE *cache = impl->cache.load(); + const X509NameCache *cache = impl->cache.load(); if (cache != nullptr) { return cache; } - UniquePtr new_cache = MakeUnique(); + UniquePtr new_cache = MakeUnique(); // Cache the DER encoding, including the outer TLV. ScopedCBB cbb; CBB seq; @@ -237,9 +284,9 @@ const X509_NAME_CACHE *bssl::x509_name_get_cache(const X509_NAME *name) { return nullptr; } - X509_NAME_CACHE *expected = nullptr; + X509NameCache *expected = nullptr; if (impl->cache.compare_exchange_strong(expected, new_cache.get())) { - // We won the race. |impl| now owns |new_cache|. + // We won the race. `impl` now owns `new_cache`. return new_cache.release(); } @@ -255,7 +302,7 @@ void bssl::x509_name_invalidate_cache(X509_NAME *name) { } int bssl::x509_marshal_name(CBB *out, const X509_NAME *in) { - const X509_NAME_CACHE *cache = x509_name_get_cache(in); + const X509NameCache *cache = x509_name_get_cache(in); if (cache == nullptr) { return 0; } @@ -263,12 +310,12 @@ int bssl::x509_marshal_name(CBB *out, const X509_NAME *in) { } int bssl::x509_name_copy(X509_NAME *dst, const X509_NAME *src) { - const X509_NAME_CACHE *cache = x509_name_get_cache(src); + const X509NameCache *cache = x509_name_get_cache(src); if (cache == nullptr) { return 0; } // Callers sometimes try to set a name back to itself. We check this after - // |x509_name_get_cache| because, if |src| was so broken that it could not be + // `x509_name_get_cache` because, if `src` was so broken that it could not be // serialized, we used to return an error. (It's not clear if this codepath is // even possible.) if (dst == src) { @@ -305,7 +352,7 @@ int i2d_X509_NAME(const X509_NAME *in, uint8_t **outp) { OPENSSL_PUT_ERROR(X509, ERR_R_PASSED_NULL_PARAMETER); return -1; } - const X509_NAME_CACHE *cache = x509_name_get_cache(in); + const X509NameCache *cache = x509_name_get_cache(in); if (cache == nullptr) { return -1; } @@ -327,78 +374,9 @@ int i2d_X509_NAME(const X509_NAME *in, uint8_t **outp) { return len; } -IMPLEMENT_EXTERN_ASN1_SIMPLE(X509_NAME, X509_NAME_new, X509_NAME_free, - CBS_ASN1_SEQUENCE, x509_parse_name, i2d_X509_NAME) - -static int asn1_marshal_string_canon(CBB *cbb, const ASN1_STRING *in) { - int (*decode_func)(CBS *, uint32_t *); - int error; - switch (in->type) { - case V_ASN1_UTF8STRING: - decode_func = CBS_get_utf8; - error = ASN1_R_INVALID_UTF8STRING; - break; - case V_ASN1_BMPSTRING: - decode_func = CBS_get_ucs2_be; - error = ASN1_R_INVALID_BMPSTRING; - break; - case V_ASN1_UNIVERSALSTRING: - decode_func = CBS_get_utf32_be; - error = ASN1_R_INVALID_UNIVERSALSTRING; - break; - case V_ASN1_PRINTABLESTRING: - case V_ASN1_T61STRING: - case V_ASN1_IA5STRING: - case V_ASN1_VISIBLESTRING: - decode_func = CBS_get_latin1; - error = ERR_R_INTERNAL_ERROR; // Latin-1 inputs are never invalid. - break; - default: - // Other string types are not canonicalized. - return asn1_marshal_any_string(cbb, in); - } - - CBB child; - if (!CBB_add_asn1(cbb, &child, CBS_ASN1_UTF8STRING)) { - return 0; - } - - bool empty = true; - bool in_whitespace = false; - CBS cbs; - CBS_init(&cbs, in->data, in->length); - while (CBS_len(&cbs) != 0) { - uint32_t c; - if (!decode_func(&cbs, &c)) { - OPENSSL_PUT_ERROR(ASN1, error); - return 0; - } - if (OPENSSL_isspace(c)) { - if (empty) { - continue; // Trim leading whitespace. - } - in_whitespace = true; - } else { - if (in_whitespace) { - // Collapse the previous run of whitespace into one space. - if (!CBB_add_u8(&child, ' ')) { - return 0; - } - } - in_whitespace = false; - // Lowecase ASCII codepoints. - if (c <= 0x7f) { - c = OPENSSL_tolower(c); - } - if (!CBB_add_utf8(&child, c)) { - return 0; - } - empty = false; - } - } - - return CBB_flush(cbb); -} +IMPLEMENT_EXTERN_ASN1_PARSE_INTO(X509_NAME, X509_NAME_new, X509_NAME_free, + CBS_ASN1_SEQUENCE, x509_parse_name, + x509_marshal_name) int X509_NAME_set(X509_NAME **xn, const X509_NAME *name) { UniquePtr copy(X509_NAME_dup(name)); @@ -410,11 +388,13 @@ int X509_NAME_set(X509_NAME **xn, const X509_NAME *name) { return 1; } -int X509_NAME_ENTRY_set(const X509_NAME_ENTRY *ne) { return ne->set; } +int X509_NAME_ENTRY_set(const X509_NAME_ENTRY *ne) { + return FromOpaque(ne)->set; +} int X509_NAME_get0_der(const X509_NAME *nm, const unsigned char **out_der, size_t *out_der_len) { - const X509_NAME_CACHE *cache = x509_name_get_cache(nm); + const X509NameCache *cache = x509_name_get_cache(nm); if (cache == nullptr) { return 0; } diff --git a/third_party/boringssl/src/crypto/x509/x_pubkey.cc b/third_party/boringssl/src/crypto/x509/x_pubkey.cc index 828dec03c..7cce8afc5 100644 --- a/third_party/boringssl/src/crypto/x509/x_pubkey.cc +++ b/third_party/boringssl/src/crypto/x509/x_pubkey.cc @@ -16,6 +16,8 @@ #include +#include + #include #include #include @@ -35,42 +37,15 @@ using namespace bssl; -void bssl::x509_pubkey_init(X509_PUBKEY *key) { - OPENSSL_memset(key, 0, sizeof(X509_PUBKEY)); - x509_algor_init(&key->algor); - asn1_string_init(&key->public_key, V_ASN1_BIT_STRING); -} - -X509_PUBKEY *X509_PUBKEY_new() { - UniquePtr ret = MakeUnique(); - if (ret == nullptr) { - return nullptr; - } - x509_pubkey_init(ret.get()); - return ret.release(); -} - -void bssl::x509_pubkey_cleanup(X509_PUBKEY *key) { - x509_algor_cleanup(&key->algor); - asn1_string_cleanup(&key->public_key); - EVP_PKEY_free(key->pkey); -} +namespace { -void X509_PUBKEY_free(X509_PUBKEY *key) { - if (key != nullptr) { - x509_pubkey_cleanup(key); - OPENSSL_free(key); - } -} - -static void x509_pubkey_changed(X509_PUBKEY *pub, - Span algs) { - EVP_PKEY_free(pub->pkey); +void x509_pubkey_changed(X509Pubkey *pub, + Span algs) { pub->pkey = nullptr; - // Re-encode the |X509_PUBKEY| to DER and parse it with EVP's APIs. If the - // operation fails, clear errors. An |X509_PUBKEY| whose key we cannot parse - // is still a valid SPKI. It just cannot be converted to an |EVP_PKEY|. + // Re-encode the `X509_PUBKEY` to DER and parse it with EVP's APIs. If the + // operation fails, clear errors. An `X509_PUBKEY` whose key we cannot parse + // is still a valid SPKI. It just cannot be converted to an `EVP_PKEY`. ScopedCBB cbb; if (!CBB_init(cbb.get(), 64) || !x509_marshal_public_key(cbb.get(), pub)) { ERR_clear_error(); @@ -83,32 +58,40 @@ static void x509_pubkey_changed(X509_PUBKEY *pub, return; } - pub->pkey = pkey.release(); + pub->pkey = std::move(pkey); } +int x509_parse_public_key_default(CBS *cbs, X509_PUBKEY *out) { + return x509_parse_public_key(cbs, FromOpaque(out), GetDefaultEVPAlgorithms()); +} + +} // namespace + +X509_PUBKEY *X509_PUBKEY_new() { return New(); } + +void X509_PUBKEY_free(X509_PUBKEY *key) { Delete(FromOpaque(key)); } + int bssl::x509_parse_public_key(CBS *cbs, X509_PUBKEY *out, Span algs) { + auto *out_impl = FromOpaque(out); CBS seq; if (!CBS_get_asn1(cbs, &seq, CBS_ASN1_SEQUENCE) || - !x509_parse_algorithm(&seq, &out->algor) || - !asn1_parse_bit_string(&seq, &out->public_key, /*tag=*/0) || + !x509_parse_algorithm(&seq, out_impl->algor.get()) || + !asn1_parse_bit_string(&seq, out_impl->public_key.get(), /*tag=*/0) || CBS_len(&seq) != 0) { OPENSSL_PUT_ERROR(ASN1, ASN1_R_DECODE_ERROR); return 0; } - x509_pubkey_changed(out, algs); + x509_pubkey_changed(out_impl, algs); return 1; } -static int x509_parse_public_key_default(CBS *cbs, X509_PUBKEY *out) { - return x509_parse_public_key(cbs, out, GetDefaultEVPAlgorithms()); -} - int bssl::x509_marshal_public_key(CBB *cbb, const X509_PUBKEY *in) { + auto *in_impl = FromOpaque(in); CBB seq; return CBB_add_asn1(cbb, &seq, CBS_ASN1_SEQUENCE) && - x509_marshal_algorithm(&seq, &in->algor) && - asn1_marshal_bit_string(&seq, &in->public_key, /*tag=*/0) && + x509_marshal_algorithm(&seq, in_impl->algor.get()) && + asn1_marshal_bit_string(&seq, in_impl->public_key.get(), /*tag=*/0) && CBB_flush(cbb); } @@ -124,21 +107,22 @@ X509_PUBKEY *d2i_X509_PUBKEY(X509_PUBKEY **out, const uint8_t **inp, long len) { int i2d_X509_PUBKEY(const X509_PUBKEY *key, uint8_t **outp) { return I2DFromCBB(/*initial_capacity=*/32, outp, [&](CBB *cbb) -> bool { - return x509_marshal_public_key(cbb, key); + return x509_marshal_public_key(cbb, FromOpaque(key)); }); } BSSL_NAMESPACE_BEGIN -// TODO(crbug.com/42290417): Remove this when |X509| and |X509_REQ| no longer +// TODO(crbug.com/42290417): Remove this when `X509` and `X509_REQ` no longer // depend on the tables. -IMPLEMENT_EXTERN_ASN1_SIMPLE(X509_PUBKEY, X509_PUBKEY_new, X509_PUBKEY_free, - CBS_ASN1_SEQUENCE, x509_parse_public_key_default, - i2d_X509_PUBKEY) +IMPLEMENT_EXTERN_ASN1_PARSE_INTO(X509_PUBKEY, X509_PUBKEY_new, X509_PUBKEY_free, + CBS_ASN1_SEQUENCE, + x509_parse_public_key_default, + x509_marshal_public_key) BSSL_NAMESPACE_END -int bssl::x509_pubkey_set1(X509_PUBKEY *key, EVP_PKEY *pkey) { +int bssl::x509_pubkey_set1(X509_PUBKEY *key, const EVP_PKEY *pkey) { ScopedCBB cbb; if (!CBB_init(cbb.get(), 64) || !EVP_marshal_public_key(cbb.get(), pkey)) { OPENSSL_PUT_ERROR(X509, X509_R_PUBLIC_KEY_ENCODE_ERROR); @@ -147,15 +131,16 @@ int bssl::x509_pubkey_set1(X509_PUBKEY *key, EVP_PKEY *pkey) { CBS cbs; CBS_init(&cbs, CBB_data(cbb.get()), CBB_len(cbb.get())); - // TODO(crbug.com/42290364): Use an |EVP_PKEY_ALG| derived from |pkey|. - // |X509_PUBKEY_get0| does not currently work when setting, say, an - // |EVP_PKEY_RSA_PSS| key. + // TODO(crbug.com/42290364): Use an `EVP_PKEY_ALG` derived from `pkey`. + // `X509_PUBKEY_get0` does not currently work when setting, say, an + // `EVP_PKEY_RSA_PSS` key. return x509_parse_public_key(&cbs, key, GetDefaultEVPAlgorithms()); } -int X509_PUBKEY_set(X509_PUBKEY **x, EVP_PKEY *pkey) { +int X509_PUBKEY_set(X509_PUBKEY **x, const EVP_PKEY *pkey) { UniquePtr new_key(X509_PUBKEY_new()); - if (new_key == nullptr || !x509_pubkey_set1(new_key.get(), pkey)) { + if (new_key == nullptr || + !x509_pubkey_set1(FromOpaque(new_key.get()), pkey)) { return 0; } X509_PUBKEY_free(*x); @@ -168,12 +153,12 @@ EVP_PKEY *X509_PUBKEY_get0(const X509_PUBKEY *key) { return nullptr; } - if (key->pkey == nullptr) { + if (FromOpaque(key)->pkey == nullptr) { OPENSSL_PUT_ERROR(X509, X509_R_PUBLIC_KEY_DECODE_ERROR); return nullptr; } - return key->pkey; + return FromOpaque(key)->pkey.get(); } EVP_PKEY *X509_PUBKEY_get(const X509_PUBKEY *key) { @@ -186,31 +171,33 @@ EVP_PKEY *X509_PUBKEY_get(const X509_PUBKEY *key) { int X509_PUBKEY_set0_param(X509_PUBKEY *pub, ASN1_OBJECT *obj, int param_type, void *param_value, uint8_t *key, int key_len) { - if (!X509_ALGOR_set0(&pub->algor, obj, param_type, param_value)) { + auto *pub_impl = FromOpaque(pub); + if (!X509_ALGOR_set0(pub_impl->algor.get(), obj, param_type, param_value)) { return 0; } - ASN1_STRING_set0(&pub->public_key, key, key_len); - x509_pubkey_changed(pub, GetDefaultEVPAlgorithms()); + ASN1_STRING_set0(pub_impl->public_key.get(), key, key_len); + x509_pubkey_changed(pub_impl, GetDefaultEVPAlgorithms()); return 1; } int X509_PUBKEY_get0_param(ASN1_OBJECT **out_obj, const uint8_t **out_key, int *out_key_len, X509_ALGOR **out_alg, X509_PUBKEY *pub) { + auto *pub_impl = FromOpaque(pub); if (out_obj != nullptr) { - *out_obj = pub->algor.algorithm; + *out_obj = pub_impl->algor->algorithm; } if (out_key != nullptr) { - *out_key = pub->public_key.data; - *out_key_len = pub->public_key.length; + *out_key = pub_impl->public_key->data; + *out_key_len = pub_impl->public_key->length; } if (out_alg != nullptr) { - *out_alg = &pub->algor; + *out_alg = pub_impl->algor.get(); } return 1; } const ASN1_BIT_STRING *X509_PUBKEY_get0_public_key(const X509_PUBKEY *pub) { - return &pub->public_key; + return FromOpaque(pub)->public_key.get(); } diff --git a/third_party/boringssl/src/crypto/x509/x_req.cc b/third_party/boringssl/src/crypto/x509/x_req.cc index 73b1c862c..92f1e7e3f 100644 --- a/third_party/boringssl/src/crypto/x509/x_req.cc +++ b/third_party/boringssl/src/crypto/x509/x_req.cc @@ -17,21 +17,23 @@ #include #include +#include "../asn1/internal.h" #include "internal.h" using namespace bssl; +namespace { + // X509_REQ_INFO is handled in an unusual way to get round invalid encodings. // Some broken certificate requests don't encode the attributes field if it // is empty. This is in violation of PKCS#10 but we need to tolerate it. We // do this by making the attributes field OPTIONAL then using the callback to // initialise it to an empty STACK. This means that the field will be // correctly encoded unless we NULL out the field. - -static int rinf_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, - void *exarg) { - bssl::X509_REQ_INFO *rinf = (bssl::X509_REQ_INFO *)*pval; +int rinf_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, + void *exarg) { + X509_REQ_INFO *rinf = asn1_load_ptr_as(pval); if (operation == ASN1_OP_NEW_POST) { rinf->attributes = sk_X509_ATTRIBUTE_new_null(); @@ -54,6 +56,8 @@ static int rinf_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, return 1; } +} // namespace + BSSL_NAMESPACE_BEGIN ASN1_SEQUENCE_enc(X509_REQ_INFO, enc, rinf_cb) = { diff --git a/third_party/boringssl/src/crypto/x509/x_x509.cc b/third_party/boringssl/src/crypto/x509/x_x509.cc index f47df483f..f7d82ad02 100644 --- a/third_party/boringssl/src/crypto/x509/x_x509.cc +++ b/third_party/boringssl/src/crypto/x509/x_x509.cc @@ -35,25 +35,77 @@ using namespace bssl; -static ExDataClass g_ex_data_class; +namespace { -static constexpr CBS_ASN1_TAG kVersionTag = +ExDataClass g_ex_data_class; + +constexpr CBS_ASN1_TAG kVersionTag = CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 0; -static constexpr CBS_ASN1_TAG kIssuerUIDTag = CBS_ASN1_CONTEXT_SPECIFIC | 1; -static constexpr CBS_ASN1_TAG kSubjectUIDTag = CBS_ASN1_CONTEXT_SPECIFIC | 2; -static constexpr CBS_ASN1_TAG kExtensionsTag = +constexpr CBS_ASN1_TAG kIssuerUIDTag = CBS_ASN1_CONTEXT_SPECIFIC | 1; +constexpr CBS_ASN1_TAG kSubjectUIDTag = CBS_ASN1_CONTEXT_SPECIFIC | 2; +constexpr CBS_ASN1_TAG kExtensionsTag = CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 3; +UniquePtr x509_parse(CBS *cbs) { + CBS cert; + if (!CBS_get_asn1_element(cbs, &cert, CBS_ASN1_SEQUENCE)) { + OPENSSL_PUT_ERROR(ASN1, ASN1_R_DECODE_ERROR); + return nullptr; + } + + UniquePtr buf(CRYPTO_BUFFER_new_from_CBS(&cert, nullptr)); + if (buf == nullptr) { + return nullptr; + } + return UniquePtr(X509_parse_from_buffer(buf.get())); +} + +int x509_marshal(CBB *cbb, const X509 *x509) { + CBB cert; + auto *impl = FromOpaque(x509); + return CBB_add_asn1(cbb, &cert, CBS_ASN1_SEQUENCE) && + x509_marshal_tbs_cert(&cert, x509) && + x509_marshal_algorithm(&cert, impl->sig_alg.get()) && + asn1_marshal_bit_string(&cert, impl->signature.get(), /*tag=*/0) && + CBB_flush(cbb); +} + +// Serialize trusted certificate to *pp or just return the required buffer +// length if pp == NULL. We ultimately want to avoid modifying *pp in the +// error path, but that depends on similar hygiene in lower-level functions. +// Here we avoid compounding the problem. +int i2d_x509_aux_internal(const X509 *a, unsigned char **pp) { + int length, tmplen; + unsigned char *start = pp != nullptr ? *pp : nullptr; + + assert(pp == nullptr || *pp != nullptr); + + // This might perturb *pp on error, but fixing that belongs in i2d_X509() + // not here. It should be that if a == NULL length is zero, but we check + // both just in case. + length = i2d_X509(a, pp); + if (length <= 0 || a == nullptr) { + return length; + } + + auto *impl = FromOpaque(a); + if (impl->aux != nullptr) { + tmplen = i2d_X509_CERT_AUX(impl->aux, pp); + if (tmplen < 0) { + if (start != nullptr) { + *pp = start; + } + return tmplen; + } + length += tmplen; + } + + return length; +} + +} // namespace + X509Impl::X509Impl() : RefCounted(CheckSubClass()) { - asn1_string_init(&serialNumber, V_ASN1_INTEGER); - x509_algor_init(&tbs_sig_alg); - x509_name_init(&issuer); - asn1_string_init(¬Before, -1); - asn1_string_init(¬After, -1); - x509_name_init(&subject); - x509_pubkey_init(&key); - x509_algor_init(&sig_alg); - asn1_string_init(&signature, V_ASN1_BIT_STRING); CRYPTO_new_ex_data(&ex_data); } @@ -62,24 +114,7 @@ X509 *X509_new() { return New(); } X509Impl::~X509Impl() { CRYPTO_free_ex_data(&g_ex_data_class, &ex_data); - asn1_string_cleanup(&serialNumber); - x509_algor_cleanup(&tbs_sig_alg); - x509_name_cleanup(&issuer); - asn1_string_cleanup(¬Before); - asn1_string_cleanup(¬After); - x509_name_cleanup(&subject); - x509_pubkey_cleanup(&key); - ASN1_BIT_STRING_free(issuerUID); - ASN1_BIT_STRING_free(subjectUID); sk_X509_EXTENSION_pop_free(extensions, X509_EXTENSION_free); - x509_algor_cleanup(&sig_alg); - asn1_string_cleanup(&signature); - CRYPTO_BUFFER_free(buf); - ASN1_OCTET_STRING_free(skid); - AUTHORITY_KEYID_free(akid); - CRL_DIST_POINTS_free(crldp); - GENERAL_NAMES_free(altname); - NAME_CONSTRAINTS_free(nc); X509_CERT_AUX_free(aux); } @@ -91,7 +126,7 @@ void X509_free(X509 *x509) { impl->DecRefInternal(); } -X509 *X509_parse_with_algorithms(CRYPTO_BUFFER *buf, +X509 *X509_parse_with_algorithms(const CRYPTO_BUFFER *buf, const EVP_PKEY_ALG *const *algs, size_t num_algs) { UniquePtr ret(FromOpaque(X509_new())); @@ -100,7 +135,7 @@ X509 *X509_parse_with_algorithms(CRYPTO_BUFFER *buf, } // Save the buffer to cache the original encoding. - ret->buf = UpRef(buf).release(); + ret->buf = UpRef(buf); // Parse the Certificate. CBS cbs, cert, tbs; @@ -111,13 +146,13 @@ X509 *X509_parse_with_algorithms(CRYPTO_BUFFER *buf, // module often omit overflow checks. CBS_len(&cert) > INT_MAX / 2 || !CBS_get_asn1(&cert, &tbs, CBS_ASN1_SEQUENCE) || - !x509_parse_algorithm(&cert, &ret->sig_alg) || + !x509_parse_algorithm(&cert, ret->sig_alg.get()) || // For just the signature field, we accept non-minimal BER lengths, though // not indefinite-length encoding. See b/18228011. // // TODO(crbug.com/boringssl/354): Switch the affected callers to convert // the certificate before parsing and then remove this workaround. - !asn1_parse_bit_string_with_bad_length(&cert, &ret->signature) || + !asn1_parse_bit_string_with_bad_length(&cert, ret->signature.get()) || CBS_len(&cert) != 0) { OPENSSL_PUT_ERROR(ASN1, ASN1_R_DECODE_ERROR); return nullptr; @@ -134,7 +169,7 @@ X509 *X509_parse_with_algorithms(CRYPTO_BUFFER *buf, return nullptr; } // The version must be one of v1(0), v2(1), or v3(2). - // TODO(https://crbug.com/42290225): Also reject |X509_VERSION_1|. v1 is + // TODO(https://crbug.com/42290225): Also reject `X509_VERSION_1`. v1 is // DEFAULT, so DER requires it be omitted. if (version != X509_VERSION_1 && version != X509_VERSION_2 && version != X509_VERSION_3) { @@ -146,13 +181,13 @@ X509 *X509_parse_with_algorithms(CRYPTO_BUFFER *buf, ret->version = X509_VERSION_1; } CBS validity; - if (!asn1_parse_integer(&tbs, &ret->serialNumber, /*tag=*/0) || - !x509_parse_algorithm(&tbs, &ret->tbs_sig_alg) || + if (!asn1_parse_integer(&tbs, ret->serialNumber.get(), /*tag=*/0) || + !x509_parse_algorithm(&tbs, ret->tbs_sig_alg.get()) || !x509_parse_name(&tbs, &ret->issuer) || !CBS_get_asn1(&tbs, &validity, CBS_ASN1_SEQUENCE) || - !asn1_parse_time(&validity, &ret->notBefore, + !asn1_parse_time(&validity, ret->notBefore.get(), /*allow_utc_timezone_offset=*/1) || - !asn1_parse_time(&validity, &ret->notAfter, + !asn1_parse_time(&validity, ret->notAfter.get(), /*allow_utc_timezone_offset=*/1) || CBS_len(&validity) != 0 || // !x509_parse_name(&tbs, &ret->subject) || @@ -163,17 +198,17 @@ X509 *X509_parse_with_algorithms(CRYPTO_BUFFER *buf, // Per RFC 5280, section 4.1.2.8, these fields require v2 or v3: if (ret->version >= X509_VERSION_2 && CBS_peek_asn1_tag(&tbs, kIssuerUIDTag)) { - ret->issuerUID = ASN1_BIT_STRING_new(); + ret->issuerUID.reset(ASN1_BIT_STRING_new()); if (ret->issuerUID == nullptr || - !asn1_parse_bit_string(&tbs, ret->issuerUID, kIssuerUIDTag)) { + !asn1_parse_bit_string(&tbs, ret->issuerUID.get(), kIssuerUIDTag)) { return nullptr; } } if (ret->version >= X509_VERSION_2 && CBS_peek_asn1_tag(&tbs, kSubjectUIDTag)) { - ret->subjectUID = ASN1_BIT_STRING_new(); + ret->subjectUID.reset(ASN1_BIT_STRING_new()); if (ret->subjectUID == nullptr || - !asn1_parse_bit_string(&tbs, ret->subjectUID, kSubjectUIDTag)) { + !asn1_parse_bit_string(&tbs, ret->subjectUID.get(), kSubjectUIDTag)) { return nullptr; } } @@ -205,33 +240,19 @@ X509 *X509_parse_with_algorithms(CRYPTO_BUFFER *buf, return ret.release(); } -X509 *X509_parse_from_buffer(CRYPTO_BUFFER *buf) { +X509 *X509_parse_from_buffer(const CRYPTO_BUFFER *buf) { auto algs = GetDefaultEVPAlgorithms(); return X509_parse_with_algorithms(buf, algs.data(), algs.size()); } -static UniquePtr x509_parse(CBS *cbs) { - CBS cert; - if (!CBS_get_asn1_element(cbs, &cert, CBS_ASN1_SEQUENCE)) { - OPENSSL_PUT_ERROR(ASN1, ASN1_R_DECODE_ERROR); - return nullptr; - } - - UniquePtr buf(CRYPTO_BUFFER_new_from_CBS(&cert, nullptr)); - if (buf == nullptr) { - return nullptr; - } - return UniquePtr(X509_parse_from_buffer(buf.get())); -} - int bssl::x509_marshal_tbs_cert(CBB *cbb, const X509 *x509) { auto *impl = FromOpaque(x509); if (impl->buf != nullptr) { - // Replay the saved TBSCertificate from the |CRYPTO_BUFFER|, to verify - // exactly what we parsed. The |CRYPTO_BUFFER| contains the full + // Replay the saved TBSCertificate from the `CRYPTO_BUFFER`, to verify + // exactly what we parsed. The `CRYPTO_BUFFER` contains the full // Certificate, so we need to find the TBSCertificate portion. CBS cbs, cert, tbs; - CRYPTO_BUFFER_init_CBS(impl->buf, &cbs); + CRYPTO_BUFFER_init_CBS(impl->buf.get(), &cbs); if (!CBS_get_asn1(&cbs, &cert, CBS_ASN1_SEQUENCE) || !CBS_get_asn1_element(&cert, &tbs, CBS_ASN1_SEQUENCE)) { // This should be impossible. @@ -252,18 +273,19 @@ int bssl::x509_marshal_tbs_cert(CBB *cbb, const X509 *x509) { return 0; } } - if (!asn1_marshal_integer(&tbs, &impl->serialNumber, /*tag=*/0) || - !x509_marshal_algorithm(&tbs, &impl->tbs_sig_alg) || + if (!asn1_marshal_integer(&tbs, impl->serialNumber.get(), /*tag=*/0) || + !x509_marshal_algorithm(&tbs, impl->tbs_sig_alg.get()) || !x509_marshal_name(&tbs, &impl->issuer) || !CBB_add_asn1(&tbs, &validity, CBS_ASN1_SEQUENCE) || - !asn1_marshal_time(&validity, &impl->notBefore) || - !asn1_marshal_time(&validity, &impl->notAfter) || + !asn1_marshal_time(&validity, impl->notBefore.get()) || + !asn1_marshal_time(&validity, impl->notAfter.get()) || !x509_marshal_name(&tbs, &impl->subject) || !x509_marshal_public_key(&tbs, &impl->key) || (impl->issuerUID != nullptr && - !asn1_marshal_bit_string(&tbs, impl->issuerUID, kIssuerUIDTag)) || + !asn1_marshal_bit_string(&tbs, impl->issuerUID.get(), kIssuerUIDTag)) || (impl->subjectUID != nullptr && - !asn1_marshal_bit_string(&tbs, impl->subjectUID, kSubjectUIDTag))) { + !asn1_marshal_bit_string(&tbs, impl->subjectUID.get(), + kSubjectUIDTag))) { return 0; } if (impl->extensions != nullptr) { @@ -279,14 +301,30 @@ int bssl::x509_marshal_tbs_cert(CBB *cbb, const X509 *x509) { return CBB_flush(cbb); } -static int x509_marshal(CBB *cbb, const X509 *x509) { - CBB cert; +int bssl::x509_get_or_marshal_tbs_cert(CBS *out, Array *scratch, + const X509 *x509) { auto *impl = FromOpaque(x509); - return CBB_add_asn1(cbb, &cert, CBS_ASN1_SEQUENCE) && - x509_marshal_tbs_cert(&cert, x509) && - x509_marshal_algorithm(&cert, &impl->sig_alg) && - asn1_marshal_bit_string(&cert, &impl->signature, /*tag=*/0) && - CBB_flush(cbb); + if (impl->buf != nullptr) { + // If there is a cached TBSCertificate encoding, just use it and avoid + // making a copy. + CBS cbs, cert; + CRYPTO_BUFFER_init_CBS(impl->buf.get(), &cbs); + if (!CBS_get_asn1(&cbs, &cert, CBS_ASN1_SEQUENCE) || + !CBS_get_asn1_element(&cert, out, CBS_ASN1_SEQUENCE)) { + // This should be impossible. + OPENSSL_PUT_ERROR(X509, ERR_R_INTERNAL_ERROR); + return 0; + } + } else { + ScopedCBB cbb; + if (!CBB_init(cbb.get(), 128) || !x509_marshal_tbs_cert(cbb.get(), x509) || + !CBBFinishArray(cbb.get(), scratch)) { + OPENSSL_PUT_ERROR(X509, ERR_R_INTERNAL_ERROR); + return 0; + } + CBS_init(out, scratch->data(), scratch->size()); + } + return 1; } X509 *d2i_X509(X509 **out, const uint8_t **inp, long len) { @@ -304,40 +342,8 @@ int i2d_X509(const X509 *x509, uint8_t **outp) { [&](CBB *cbb) -> bool { return x509_marshal(cbb, x509); }); } -static int x509_new_cb(ASN1_VALUE **pval, const ASN1_ITEM *it) { - *pval = (ASN1_VALUE *)X509_new(); - return *pval != nullptr; -} - -static void x509_free_cb(ASN1_VALUE **pval, const ASN1_ITEM *it) { - X509_free((X509 *)*pval); - *pval = nullptr; -} - -static int x509_parse_cb(ASN1_VALUE **pval, CBS *cbs, const ASN1_ITEM *it, - int opt) { - if (opt && !CBS_peek_asn1_tag(cbs, CBS_ASN1_SEQUENCE)) { - return 1; - } - - UniquePtr ret = x509_parse(cbs); - if (ret == nullptr) { - return 0; - } - - X509_free((X509 *)*pval); - *pval = (ASN1_VALUE *)ret.release(); - return 1; -} - -static int x509_i2d_cb(ASN1_VALUE **pval, unsigned char **out, - const ASN1_ITEM *it) { - return i2d_X509((X509 *)*pval, out); -} - -static const ASN1_EXTERN_FUNCS x509_extern_funcs = {x509_new_cb, x509_free_cb, - x509_parse_cb, x509_i2d_cb}; -IMPLEMENT_EXTERN_ASN1(X509, x509_extern_funcs) +IMPLEMENT_EXTERN_ASN1_PARSE_NEW(X509, X509_new, X509_free, CBS_ASN1_SEQUENCE, + x509_parse, x509_marshal) X509 *X509_dup(const X509 *x509) { uint8_t *der = nullptr; @@ -359,10 +365,10 @@ int X509_up_ref(X509 *x) { } X509 *X509_dup_ref(const X509 *x) { - auto *x_ref = const_cast(x); - // We know that this call always returns one. - X509_up_ref(x_ref); - return x_ref; + if (x == nullptr) { + return nullptr; + } + return FromOpaque(x)->DupRefInternal(); } int X509_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused, @@ -418,39 +424,6 @@ X509 *d2i_X509_AUX(X509 **a, const unsigned char **pp, long length) { return nullptr; } -// Serialize trusted certificate to *pp or just return the required buffer -// length if pp == NULL. We ultimately want to avoid modifying *pp in the -// error path, but that depends on similar hygiene in lower-level functions. -// Here we avoid compounding the problem. -static int i2d_x509_aux_internal(const X509 *a, unsigned char **pp) { - int length, tmplen; - unsigned char *start = pp != nullptr ? *pp : nullptr; - - assert(pp == nullptr || *pp != nullptr); - - // This might perturb *pp on error, but fixing that belongs in i2d_X509() - // not here. It should be that if a == NULL length is zero, but we check - // both just in case. - length = i2d_X509(a, pp); - if (length <= 0 || a == nullptr) { - return length; - } - - auto *impl = FromOpaque(a); - if (impl->aux != nullptr) { - tmplen = i2d_X509_CERT_AUX(impl->aux, pp); - if (tmplen < 0) { - if (start != nullptr) { - *pp = start; - } - return tmplen; - } - length += tmplen; - } - - return length; -} - // Serialize trusted certificate to *pp, or just return the required buffer // length if pp == NULL. // @@ -489,7 +462,6 @@ int i2d_X509_AUX(const X509 *a, unsigned char **pp) { int i2d_re_X509_tbs(X509 *x509, uint8_t **outp) { auto *impl = FromOpaque(x509); - CRYPTO_BUFFER_free(impl->buf); impl->buf = nullptr; return i2d_X509_tbs(x509, outp); } @@ -502,26 +474,26 @@ int i2d_X509_tbs(const X509 *x509, uint8_t **outp) { int X509_set1_signature_algo(X509 *x509, const X509_ALGOR *algo) { auto *impl = FromOpaque(x509); - return X509_ALGOR_copy(&impl->sig_alg, algo) && - X509_ALGOR_copy(&impl->tbs_sig_alg, algo); + return X509_ALGOR_copy(impl->sig_alg.get(), algo) && + X509_ALGOR_copy(impl->tbs_sig_alg.get(), algo); } int X509_set1_signature_value(X509 *x509, const uint8_t *sig, size_t sig_len) { - return ASN1_STRING_set(&FromOpaque(x509)->signature, sig, sig_len); + return ASN1_STRING_set(FromOpaque(x509)->signature.get(), sig, sig_len); } void X509_get0_signature(const ASN1_BIT_STRING **psig, const X509_ALGOR **palg, const X509 *x) { const auto *impl = FromOpaque(x); if (psig) { - *psig = &impl->signature; + *psig = impl->signature.get(); } if (palg) { - *palg = &impl->sig_alg; + *palg = impl->sig_alg.get(); } } int X509_get_signature_nid(const X509 *x) { const auto *impl = FromOpaque(x); - return OBJ_obj2nid(impl->sig_alg.algorithm); + return OBJ_obj2nid(impl->sig_alg->algorithm); } diff --git a/third_party/boringssl/src/crypto/x509/x_x509a.cc b/third_party/boringssl/src/crypto/x509/x_x509a.cc index 3c5e841f2..bc32624ea 100644 --- a/third_party/boringssl/src/crypto/x509/x_x509a.cc +++ b/third_party/boringssl/src/crypto/x509/x_x509a.cc @@ -44,7 +44,9 @@ IMPLEMENT_ASN1_FUNCTIONS_const(X509_CERT_AUX) BSSL_NAMESPACE_END -static X509_CERT_AUX *aux_get(X509Impl *x) { +namespace { + +X509_CERT_AUX *aux_get(X509Impl *x) { if (!x) { return nullptr; } @@ -54,11 +56,13 @@ static X509_CERT_AUX *aux_get(X509Impl *x) { return x->aux; } +} // namespace + int X509_alias_set1(X509 *x, const uint8_t *name, ossl_ssize_t len) { auto *impl = FromOpaque(x); X509_CERT_AUX *aux; // TODO(davidben): Empty aliases are not meaningful in PKCS#12, and the - // getters cannot quite represent them. Also erase the object if |len| is + // getters cannot quite represent them. Also erase the object if `len` is // zero. if (!name) { if (!impl || !impl->aux || !impl->aux->alias) { @@ -81,7 +85,7 @@ int X509_keyid_set1(X509 *x, const uint8_t *id, ossl_ssize_t len) { auto *impl = FromOpaque(x); X509_CERT_AUX *aux; // TODO(davidben): Empty key IDs are not meaningful in PKCS#12, and the - // getters cannot quite represent them. Also erase the object if |len| is + // getters cannot quite represent them. Also erase the object if `len` is // zero. if (!id) { if (!impl || !impl->aux || !impl->aux->keyid) { diff --git a/third_party/boringssl/src/gen/crypto/err_data.cc b/third_party/boringssl/src/gen/crypto/err_data.cc index e20e7e7a2..b365a543f 100644 --- a/third_party/boringssl/src/gen/crypto/err_data.cc +++ b/third_party/boringssl/src/gen/crypto/err_data.cc @@ -80,54 +80,54 @@ const uint32_t kOpenSSLReasonValues[] = { 0xc3b00f7, 0xc3b8921, 0x10320892, - 0x10329712, - 0x1033171e, - 0x10339737, - 0x1034174a, + 0x10329729, + 0x10331735, + 0x1033974e, + 0x10341761, 0x10348fd3, 0x10350d1f, - 0x1035975d, - 0x10361787, - 0x1036979a, - 0x103717b9, - 0x103797d2, - 0x103817e7, - 0x10389805, - 0x10391814, - 0x10399830, - 0x103a184b, - 0x103a985a, - 0x103b1876, - 0x103b9891, - 0x103c18b7, + 0x10359774, + 0x1036179e, + 0x103697b1, + 0x103717d0, + 0x103797e9, + 0x103817fe, + 0x1038981c, + 0x1039182b, + 0x10399847, + 0x103a1862, + 0x103a9871, + 0x103b188d, + 0x103b98a8, + 0x103c18ce, 0x103c80f7, - 0x103d18c8, - 0x103d98dc, - 0x103e18fb, - 0x103e990a, - 0x103f1921, - 0x103f9934, + 0x103d18df, + 0x103d98f3, + 0x103e1912, + 0x103e9921, + 0x103f1938, + 0x103f994b, 0x10400ce3, - 0x10409947, - 0x10411965, - 0x10419978, - 0x10421992, - 0x104299a2, - 0x104319b6, - 0x104399cc, - 0x104419e4, - 0x104499f9, - 0x10451a0d, - 0x10459a1f, + 0x1040995e, + 0x1041197c, + 0x1041998f, + 0x104219a9, + 0x104299b9, + 0x104319cd, + 0x104399e3, + 0x104419fb, + 0x10449a10, + 0x10451a24, + 0x10459a36, 0x10460635, 0x1046899a, - 0x10471a34, - 0x10479a4b, - 0x10481a60, - 0x10489a6e, + 0x10471a4b, + 0x10479a62, + 0x10481a77, + 0x10489a85, 0x10490f1f, - 0x104998a8, - 0x104a1772, + 0x104998bf, + 0x104a1789, 0x14320cb3, 0x14328cd4, 0x14330ce3, @@ -147,56 +147,57 @@ const uint32_t kOpenSSLReasonValues[] = { 0x183690c1, 0x18371113, 0x18379129, - 0x1838113d, - 0x1838914d, + 0x18381154, + 0x18389164, 0x18390ac0, - 0x1839915d, - 0x183a1183, - 0x183a91bf, + 0x18399174, + 0x183a119a, + 0x183a91d6, 0x183b0d2b, - 0x183b9221, - 0x183c1233, - 0x183c923e, - 0x183d124e, - 0x183d925f, - 0x183e1270, - 0x183e929c, - 0x183f1282, - 0x183f92de, - 0x184012f6, + 0x183b9238, + 0x183c124a, + 0x183c9255, + 0x183d1265, + 0x183d9276, + 0x183e1287, + 0x183e92b3, + 0x183f1299, + 0x183f92f5, + 0x1840130d, 0x1840870d, - 0x184111f5, - 0x184191ad, - 0x184211cc, + 0x1841120c, + 0x184191c4, + 0x184211e3, 0x18428cc1, - 0x18431172, - 0x18439207, + 0x18431189, + 0x1843921e, 0x18441068, 0x184490e5, 0x18451087, - 0x184592c5, - 0x184611e2, + 0x184592dc, + 0x184611f9, 0x184690f9, - 0x18471197, - 0x20321330, - 0x2032931d, - 0x2432133c, + 0x184711ae, + 0x1847913d, + 0x20321347, + 0x20329334, + 0x24321353, 0x243289e0, - 0x2433134e, - 0x2433935b, - 0x24341368, - 0x2434937a, - 0x24351389, - 0x243593a6, - 0x243613b3, - 0x243693c1, - 0x243713cf, - 0x243793dd, - 0x243813e6, - 0x243893f3, - 0x24391406, - 0x2439941d, - 0x243a143b, + 0x24331365, + 0x24339372, + 0x2434137f, + 0x24349391, + 0x243513a0, + 0x243593bd, + 0x243613ca, + 0x243693d8, + 0x243713e6, + 0x243793f4, + 0x243813fd, + 0x2438940a, + 0x2439141d, + 0x24399434, + 0x243a1452, 0x28320d13, 0x28328d2b, 0x28330ce3, @@ -206,51 +207,53 @@ const uint32_t kOpenSSLReasonValues[] = { 0x283500f7, 0x28358cc1, 0x2836099a, - 0x2c3234ce, - 0x2c329443, - 0x2c3334dc, - 0x2c33b4ee, - 0x2c343502, - 0x2c34b514, - 0x2c35352f, - 0x2c35b541, - 0x2c363571, + 0x2c32353c, + 0x2c32945a, + 0x2c33354a, + 0x2c33b55c, + 0x2c343570, + 0x2c34b582, + 0x2c35359d, + 0x2c35b5af, + 0x2c3635df, 0x2c36833a, - 0x2c37357e, - 0x2c37b5aa, - 0x2c3835e8, - 0x2c38b5ff, - 0x2c39361d, - 0x2c39b62d, - 0x2c3a363f, - 0x2c3ab653, - 0x2c3b3664, - 0x2c3bb683, - 0x2c3c1455, - 0x2c3c946b, - 0x2c3d36c8, - 0x2c3d9484, - 0x2c3e36f2, - 0x2c3eb700, - 0x2c3f3718, - 0x2c3fb730, - 0x2c40375a, - 0x2c409330, - 0x2c41376b, - 0x2c41b77e, - 0x2c4212f6, - 0x2c42b78f, + 0x2c3735ec, + 0x2c37b618, + 0x2c383677, + 0x2c38b68e, + 0x2c3936ac, + 0x2c39b6bc, + 0x2c3a36ce, + 0x2c3ab6e2, + 0x2c3b36f3, + 0x2c3bb712, + 0x2c3c146c, + 0x2c3c9482, + 0x2c3d3757, + 0x2c3d949b, + 0x2c3e3781, + 0x2c3eb78f, + 0x2c3f37a7, + 0x2c3fb7bf, + 0x2c4037e9, + 0x2c409347, + 0x2c4137fa, + 0x2c41b80d, + 0x2c42130d, + 0x2c42b81e, 0x2c43076d, - 0x2c43b675, - 0x2c4435bd, - 0x2c44b73d, - 0x2c453554, - 0x2c45b590, - 0x2c46360d, - 0x2c46b697, - 0x2c4736ac, - 0x2c47b6e5, - 0x2c4835cf, + 0x2c43b704, + 0x2c44364c, + 0x2c44b7cc, + 0x2c4535c2, + 0x2c45b5fe, + 0x2c46369c, + 0x2c46b726, + 0x2c47373b, + 0x2c47b774, + 0x2c48365e, + 0x2c48b62b, + 0x2c49363a, 0x30320000, 0x30328015, 0x3033001f, @@ -390,274 +393,278 @@ const uint32_t kOpenSSLReasonValues[] = { 0x3c418e13, 0x3c420f1f, 0x3c428ea9, - 0x40321ada, - 0x40329af0, - 0x40331b1e, - 0x40339b28, - 0x40341b3f, - 0x40349b5d, - 0x40351b6d, - 0x40359b7f, - 0x40361b8c, - 0x40369b98, - 0x40371bad, - 0x40379bbf, - 0x40381bca, - 0x40389bdc, + 0x40321af1, + 0x40329b07, + 0x40331b35, + 0x40339b3f, + 0x40341b56, + 0x40349b74, + 0x40351b84, + 0x40359b96, + 0x40361ba3, + 0x40369baf, + 0x40371bc4, + 0x40379bd6, + 0x40381be1, + 0x40389bf3, 0x40390fd3, - 0x40399bec, - 0x403a1bff, - 0x403a9c20, - 0x403b1c31, - 0x403b9c41, + 0x40399c03, + 0x403a1c16, + 0x403a9c37, + 0x403b1c48, + 0x403b9c58, 0x403c0071, 0x403c8090, - 0x403d1ca2, - 0x403d9cb8, - 0x403e1cc7, - 0x403e9cff, - 0x403f1d19, - 0x403f9d41, - 0x40401d56, - 0x40409d6a, - 0x40411da5, - 0x40419dc0, - 0x40421dd9, - 0x40429dec, - 0x40431e00, - 0x40439e2e, - 0x40441e45, + 0x403d1cb9, + 0x403d9ccf, + 0x403e1cde, + 0x403e9d16, + 0x403f1d30, + 0x403f9d58, + 0x40401d6d, + 0x40409d81, + 0x40411dbc, + 0x40419dd7, + 0x40421df0, + 0x40429e03, + 0x40431e17, + 0x40439e45, + 0x40441e5c, 0x404480b9, - 0x40451e5a, - 0x40459e6c, - 0x40461e90, - 0x40469eb0, - 0x40471ebe, - 0x40479ee5, - 0x40481f66, - 0x4048a020, - 0x40492037, - 0x4049a051, - 0x404a2068, - 0x404aa086, - 0x404b209e, - 0x404ba0cb, - 0x404c20e1, - 0x404ca0f3, - 0x404d2114, - 0x404da14d, - 0x404e2161, - 0x404ea16e, - 0x404f2259, - 0x404fa2cf, - 0x4050238b, - 0x4050a39f, - 0x405123ec, - 0x405223fc, - 0x4052a42c, - 0x40532444, - 0x4053a457, - 0x4054246c, - 0x4054a48f, - 0x405524ba, - 0x4055a4f7, - 0x4056251c, - 0x4056a535, - 0x4057254d, - 0x4057a560, - 0x40582575, - 0x4058a59c, - 0x405925cb, - 0x4059a60b, - 0x405aa61f, - 0x405b2637, - 0x405ba648, - 0x405c265b, - 0x405ca6b0, - 0x405d26bd, - 0x405da6e2, - 0x405e2720, + 0x40451e71, + 0x40459e83, + 0x40461ea7, + 0x40469ec7, + 0x40471ed5, + 0x40479efc, + 0x40481f8e, + 0x4048a048, + 0x4049205f, + 0x4049a079, + 0x404a2090, + 0x404aa0ae, + 0x404b20c6, + 0x404ba0f3, + 0x404c2109, + 0x404ca11b, + 0x404d213c, + 0x404da175, + 0x404e2189, + 0x404ea196, + 0x404f2296, + 0x404fa30c, + 0x405023c8, + 0x4050a3dc, + 0x40512441, + 0x40522451, + 0x4052a481, + 0x40532499, + 0x4053a4ac, + 0x405424c1, + 0x4054a4e4, + 0x4055250f, + 0x4055a54c, + 0x40562571, + 0x4056a58a, + 0x405725a2, + 0x4057a5b5, + 0x405825ca, + 0x4058a5f1, + 0x40592620, + 0x4059a660, + 0x405aa674, + 0x405b268c, + 0x405ba69d, + 0x405c26b0, + 0x405ca705, + 0x405d2712, + 0x405da737, + 0x405e2775, 0x405e8afe, - 0x405f2750, - 0x405fa75d, - 0x4060276b, - 0x4060a78d, - 0x40612801, - 0x4061a839, - 0x40622850, - 0x4062a861, - 0x406328ae, - 0x4063a8c3, - 0x406428da, - 0x4064a906, - 0x40652921, - 0x4065a938, - 0x40662950, - 0x4066a97a, - 0x406729a5, - 0x4067a9ea, - 0x40682a32, - 0x4068aa53, - 0x40692a85, - 0x4069aab3, - 0x406a2ad4, - 0x406aaaf4, - 0x406b2c7c, - 0x406bac9f, - 0x406c2cb5, - 0x406cafbf, - 0x406d2fee, - 0x406db016, - 0x406e3044, - 0x406eb091, - 0x406f30ea, - 0x406fb122, - 0x40703135, - 0x4070b152, + 0x405f27a5, + 0x405fa7b2, + 0x406027c0, + 0x4060a7e2, + 0x40612856, + 0x4061a88e, + 0x406228a5, + 0x4062a8b6, + 0x40632903, + 0x4063a918, + 0x4064292f, + 0x4064a95b, + 0x40652976, + 0x4065a98d, + 0x406629a5, + 0x4066a9cf, + 0x406729fa, + 0x4067aa3f, + 0x40682a87, + 0x4068aaa8, + 0x40692ada, + 0x4069ab08, + 0x406a2b29, + 0x406aab49, + 0x406b2cd1, + 0x406bacf4, + 0x406c2d0a, + 0x406cb014, + 0x406d3043, + 0x406db06b, + 0x406e3099, + 0x406eb0e6, + 0x406f313f, + 0x406fb177, + 0x4070318a, + 0x4070b1a7, 0x4071084d, - 0x4071b164, - 0x40723177, - 0x4072b1ad, - 0x407331c5, - 0x4073966d, - 0x407431d9, - 0x4074b1f3, - 0x40753204, - 0x4075b218, - 0x40763226, - 0x407693f3, - 0x40773263, - 0x4077b2bf, - 0x407832da, - 0x4078b313, - 0x4079332a, - 0x4079b340, - 0x407a336c, - 0x407ab37f, - 0x407b3394, - 0x407bb3a6, - 0x407c33d7, - 0x407cb3e0, - 0x407d2a6e, - 0x407da2f7, - 0x407e32ef, - 0x407ea5ac, - 0x407f1ed2, - 0x407fa0b5, - 0x40802269, - 0x40809efa, - 0x4081241a, - 0x4081a1bc, - 0x4082302f, - 0x40829c4d, - 0x40832587, - 0x4083a8eb, - 0x40841f1e, - 0x4084a5e4, - 0x4085266c, - 0x4085a7c8, - 0x40862702, - 0x4086a343, - 0x40873075, - 0x4087a816, - 0x40881c8b, - 0x4088a9fd, - 0x40891cda, - 0x40899c67, - 0x408a2ced, - 0x408a9a85, - 0x408b33bb, - 0x408bb0ff, - 0x408c2692, - 0x408d2006, - 0x408d9f50, - 0x408e2136, - 0x408ea4d7, - 0x408f2a11, - 0x408fa7e4, - 0x409029c6, - 0x4090a6d4, - 0x40912cd5, - 0x40919abd, - 0x40921d27, - 0x4092b0b0, - 0x40933190, - 0x4093a354, - 0x40941f32, - 0x4094ad06, - 0x40952872, - 0x4095b34c, - 0x4096305c, - 0x4096a282, - 0x409723d4, - 0x4097a185, - 0x40981d87, - 0x4098a886, - 0x409930cc, - 0x4099a504, - 0x409a249d, - 0x409a9aa1, - 0x409b1f8c, - 0x409b9fb7, - 0x409c32a1, - 0x409c9fdf, - 0x409d223e, - 0x409da1d2, - 0x409e1e18, - 0x409ea2b7, - 0x409f229f, - 0x409f9f7f, - 0x40a022df, - 0x40a0a19f, - 0x40a121ed, - 0x40a1a5f8, - 0x40a22370, - 0x40a2a741, - 0x40a327b5, - 0x40a3b285, - 0x40a423ba, - 0x40a4a204, - 0x40a51f0e, - 0x40a5a311, - 0x40a6267c, - 0x40a6a226, - 0x40a7324b, - 0x40a7a40e, - 0x40a8232c, - 0x41f42ba7, - 0x41f92c39, - 0x41fe2b2c, - 0x41feade2, - 0x41ff2f10, - 0x42032bc0, - 0x42082be2, - 0x4208ac1e, - 0x42092b10, - 0x4209ac58, - 0x420a2b67, - 0x420aab47, - 0x420b2b87, - 0x420bac00, - 0x420c2f2c, - 0x420cad16, - 0x420d2dc9, - 0x420dae00, - 0x42122e33, - 0x42172ef3, - 0x4217ae75, - 0x421c2e97, - 0x421f2e52, - 0x42212fa4, - 0x42262ed6, - 0x422b2f82, - 0x422bada4, - 0x422c2f64, - 0x422cad57, - 0x422d2d30, - 0x422daf43, - 0x422e2d83, - 0x42302eb2, - 0x4230ae1a, + 0x4071b1b9, + 0x407231cc, + 0x4072b202, + 0x4073321a, + 0x40739684, + 0x4074322e, + 0x4074b248, + 0x40753259, + 0x4075b26d, + 0x4076327b, + 0x4076940a, + 0x407732b8, + 0x4077b314, + 0x4078332f, + 0x4078b381, + 0x40793398, + 0x4079b3ae, + 0x407a33da, + 0x407ab3ed, + 0x407b3402, + 0x407bb414, + 0x407c3445, + 0x407cb44e, + 0x407d2ac3, + 0x407da334, + 0x407e3344, + 0x407ea601, + 0x407f1ee9, + 0x407fa0dd, + 0x408022a6, + 0x40809f22, + 0x4081246f, + 0x4081a1e4, + 0x40823084, + 0x40829c64, + 0x408325dc, + 0x4083a940, + 0x40841f46, + 0x4084a639, + 0x408526c1, + 0x4085a81d, + 0x40862757, + 0x4086a380, + 0x408730ca, + 0x4087a86b, + 0x40881ca2, + 0x4088aa52, + 0x40891cf1, + 0x40899c7e, + 0x408a2d42, + 0x408a9a9c, + 0x408b3429, + 0x408bb154, + 0x408c26e7, + 0x408d202e, + 0x408d9f78, + 0x408e215e, + 0x408ea52c, + 0x408f2a66, + 0x408fa839, + 0x40902a1b, + 0x4090a729, + 0x40912d2a, + 0x40919ad4, + 0x40921d3e, + 0x4092b105, + 0x409331e5, + 0x4093a391, + 0x40941f5a, + 0x4094ad5b, + 0x409528c7, + 0x4095b3ba, + 0x409630b1, + 0x4096a2bf, + 0x40972429, + 0x4097a1ad, + 0x40981d9e, + 0x4098a8db, + 0x40993121, + 0x4099a559, + 0x409a24f2, + 0x409a9ab8, + 0x409b1fb4, + 0x409b9fdf, + 0x409c32f6, + 0x409ca007, + 0x409d227b, + 0x409da1fa, + 0x409e1e2f, + 0x409ea2f4, + 0x409f22dc, + 0x409f9fa7, + 0x40a0231c, + 0x40a0a1c7, + 0x40a12215, + 0x40a1a64d, + 0x40a223ad, + 0x40a2a796, + 0x40a3280a, + 0x40a3b2da, + 0x40a4240f, + 0x40a4a22c, + 0x40a51f36, + 0x40a5a34e, + 0x40a626d1, + 0x40a6a24e, + 0x40a732a0, + 0x40a7a463, + 0x40a82369, + 0x40a8b368, + 0x40a92266, + 0x40a99f11, + 0x40aa23f7, + 0x41f42bfc, + 0x41f92c8e, + 0x41fe2b81, + 0x41feae37, + 0x41ff2f65, + 0x42032c15, + 0x42082c37, + 0x4208ac73, + 0x42092b65, + 0x4209acad, + 0x420a2bbc, + 0x420aab9c, + 0x420b2bdc, + 0x420bac55, + 0x420c2f81, + 0x420cad6b, + 0x420d2e1e, + 0x420dae55, + 0x42122e88, + 0x42172f48, + 0x4217aeca, + 0x421c2eec, + 0x421f2ea7, + 0x42212ff9, + 0x42262f2b, + 0x422b2fd7, + 0x422badf9, + 0x422c2fb9, + 0x422cadac, + 0x422d2d85, + 0x422daf98, + 0x422e2dd8, + 0x42302f07, + 0x4230ae6f, 0x44320778, 0x44328787, 0x44330793, @@ -675,109 +682,109 @@ const uint32_t kOpenSSLReasonValues[] = { 0x4439084d, 0x4439885b, 0x443a086e, - 0x48321443, - 0x48329455, - 0x4833146b, - 0x48339484, - 0x4c3214c1, - 0x4c3294d1, - 0x4c3314e4, - 0x4c339504, + 0x4832145a, + 0x4832946c, + 0x48331482, + 0x4833949b, + 0x4c3214d8, + 0x4c3294e8, + 0x4c3314fb, + 0x4c33951b, 0x4c3400b9, 0x4c3480f7, - 0x4c351510, - 0x4c35951e, - 0x4c36153a, - 0x4c369560, - 0x4c37156f, - 0x4c37957d, - 0x4c381592, - 0x4c38959e, - 0x4c3915be, - 0x4c3995e8, - 0x4c3a1601, - 0x4c3a961a, + 0x4c351527, + 0x4c359535, + 0x4c361551, + 0x4c369577, + 0x4c371586, + 0x4c379594, + 0x4c3815a9, + 0x4c3895b5, + 0x4c3915d5, + 0x4c3995ff, + 0x4c3a1618, + 0x4c3a9631, 0x4c3b0635, - 0x4c3b9633, - 0x4c3c1645, - 0x4c3c9654, - 0x4c3d166d, + 0x4c3b964a, + 0x4c3c165c, + 0x4c3c966b, + 0x4c3d1684, 0x4c3d8d06, - 0x4c3e16da, - 0x4c3e967c, - 0x4c3f16fc, - 0x4c3f93f3, - 0x4c401692, - 0x4c4094ad, - 0x4c4116ca, - 0x4c41954d, - 0x4c4216b6, - 0x4c429495, - 0x503237a1, - 0x5032b7b0, - 0x503337bb, - 0x5033b7cb, - 0x503437e4, - 0x5034b7fe, - 0x5035380c, - 0x5035b822, - 0x50363834, - 0x5036b84a, - 0x50373863, - 0x5037b876, - 0x5038388e, - 0x5038b89f, - 0x503938b4, - 0x5039b8c8, - 0x503a38e8, - 0x503ab8fe, - 0x503b3916, - 0x503bb928, - 0x503c3944, - 0x503cb95b, - 0x503d3974, - 0x503db98a, - 0x503e3997, - 0x503eb9ad, - 0x503f39bf, + 0x4c3e16f1, + 0x4c3e9693, + 0x4c3f1713, + 0x4c3f940a, + 0x4c4016a9, + 0x4c4094c4, + 0x4c4116e1, + 0x4c419564, + 0x4c4216cd, + 0x4c4294ac, + 0x50323830, + 0x5032b83f, + 0x5033384a, + 0x5033b85a, + 0x50343873, + 0x5034b88d, + 0x5035389b, + 0x5035b8b1, + 0x503638c3, + 0x5036b8d9, + 0x503738f2, + 0x5037b905, + 0x5038391d, + 0x5038b92e, + 0x50393943, + 0x5039b957, + 0x503a3977, + 0x503ab98d, + 0x503b39a5, + 0x503bb9b7, + 0x503c39d3, + 0x503cb9ea, + 0x503d3a03, + 0x503dba19, + 0x503e3a26, + 0x503eba3c, + 0x503f3a4e, 0x503f83b3, - 0x504039d2, - 0x5040b9e2, - 0x504139fc, - 0x5041ba0b, - 0x50423a25, - 0x5042ba42, - 0x50433a52, - 0x5043ba62, - 0x50443a7f, + 0x50403a61, + 0x5040ba71, + 0x50413a8b, + 0x5041ba9a, + 0x50423ab4, + 0x5042bad1, + 0x50433ae1, + 0x5043baf1, + 0x50443b0e, 0x50448469, - 0x50453a93, - 0x5045bab1, - 0x50463ac4, - 0x5046bada, - 0x50473aec, - 0x5047bb01, - 0x50483b27, - 0x5048bb35, - 0x50493b48, - 0x5049bb5d, - 0x504a3b73, - 0x504abb83, - 0x504b3ba3, - 0x504bbbb6, - 0x504c3bd9, - 0x504cbc07, - 0x504d3c34, - 0x504dbc51, - 0x504e3c6c, - 0x504ebc88, - 0x504f3c9a, - 0x504fbcb1, - 0x50503cc0, + 0x50453b22, + 0x5045bb40, + 0x50463b53, + 0x5046bb69, + 0x50473b7b, + 0x5047bb90, + 0x50483bb6, + 0x5048bbc4, + 0x50493bd7, + 0x5049bbec, + 0x504a3c02, + 0x504abc12, + 0x504b3c32, + 0x504bbc45, + 0x504c3c68, + 0x504cbc96, + 0x504d3cc3, + 0x504dbce0, + 0x504e3cfb, + 0x504ebd17, + 0x504f3d29, + 0x504fbd40, + 0x50503d4f, 0x50508729, - 0x50513cd3, - 0x5051ba71, - 0x50523c19, + 0x50513d62, + 0x5051bb00, + 0x50523ca8, 0x58321011, 0x68320fd3, 0x68328d2b, @@ -819,22 +826,22 @@ const uint32_t kOpenSSLReasonValues[] = { 0x783d8b97, 0x783e0aed, 0x783e8a9f, - 0x7c32130c, - 0x80321560, + 0x7c321323, + 0x80321577, 0x80328090, - 0x8033349d, + 0x8033350b, 0x803380b9, - 0x803434ac, - 0x8034b414, - 0x80353432, - 0x8035b4c0, - 0x80363474, - 0x8036b423, - 0x80373466, - 0x8037b401, - 0x80383487, - 0x8038b443, - 0x80393458, + 0x8034351a, + 0x8034b482, + 0x803534a0, + 0x8035b52e, + 0x803634e2, + 0x8036b491, + 0x803734d4, + 0x8037b46f, + 0x803834f5, + 0x8038b4b1, + 0x803934c6, 0x84320bb0, 0x84328bc9, }; @@ -1071,6 +1078,7 @@ const char kOpenSSLReasonStringData[] = "INVALID_CIPHERTEXT_LENGTH\0" "INVALID_DIGEST_LENGTH\0" "INVALID_DIGEST_TYPE\0" + "INVALID_ENTROPY_LENGTH\0" "INVALID_KEYBITS\0" "INVALID_MGF1_MD\0" "INVALID_PADDING_MODE\0" @@ -1238,6 +1246,7 @@ const char kOpenSSLReasonStringData[] = "DIGEST_CHECK_FAILED\0" "DOWNGRADE_DETECTED\0" "DTLS_MESSAGE_TOO_BIG\0" + "DUPLICATE_CIPHER\0" "DUPLICATE_EXTENSION\0" "DUPLICATE_GROUP\0" "DUPLICATE_KEY_SHARE\0" @@ -1271,6 +1280,7 @@ const char kOpenSSLReasonStringData[] = "INVALID_ALPS_CODEPOINT\0" "INVALID_CERTIFICATE_PROPERTY_LIST\0" "INVALID_CERT_TYPES_LIST\0" + "INVALID_CIPHER_FLAGS\0" "INVALID_CLIENT_HELLO_INNER\0" "INVALID_COMMAND\0" "INVALID_COMPRESSION_LIST\0" @@ -1287,6 +1297,7 @@ const char kOpenSSLReasonStringData[] = "INVALID_SPAKE2PLUSV1_VALUE\0" "INVALID_SSL_SESSION\0" "INVALID_TICKET_KEYS_LENGTH\0" + "INVALID_TRUST_ANCHOR_ID\0" "INVALID_TRUST_ANCHOR_LIST\0" "KEY_USAGE_BIT_INCORRECT\0" "LENGTH_MISMATCH\0" @@ -1436,6 +1447,7 @@ const char kOpenSSLReasonStringData[] = "UNSUPPORTED_ELLIPTIC_CURVE\0" "UNSUPPORTED_PROTOCOL\0" "UNSUPPORTED_PROTOCOL_FOR_CUSTOM_KEY\0" + "UNUSABLE_ECH_CONFIG_LIST\0" "WRONG_CERTIFICATE_TYPE\0" "WRONG_CIPHER_RETURNED\0" "WRONG_CURVE\0" @@ -1471,6 +1483,8 @@ const char kOpenSSLReasonStringData[] = "INVALID_DIRECTORY\0" "INVALID_FIELD_FOR_VERSION\0" "INVALID_FIELD_NAME\0" + "INVALID_MTC_CA\0" + "INVALID_MTC_PROOF\0" "INVALID_PARAMETER\0" "INVALID_POLICY_EXTENSION\0" "INVALID_PSS_PARAMETERS\0" diff --git a/third_party/boringssl/src/include/openssl/aead.h b/third_party/boringssl/src/include/openssl/aead.h index d23c16dc8..339e34a97 100644 --- a/third_party/boringssl/src/include/openssl/aead.h +++ b/third_party/boringssl/src/include/openssl/aead.h @@ -15,7 +15,7 @@ #ifndef OPENSSL_HEADER_AEAD_H #define OPENSSL_HEADER_AEAD_H -#include // IWYU pragma: export +#include // IWYU pragma: export #if defined(__cplusplus) extern "C" { @@ -29,22 +29,22 @@ extern "C" { // message has a unique, per-message nonce and, optionally, additional data // which is authenticated but not included in the ciphertext. // -// The |EVP_AEAD_CTX_init| function initialises an |EVP_AEAD_CTX| structure and -// performs any precomputation needed to use |aead| with |key|. The length of -// the key, |key_len|, is given in bytes. +// The `EVP_AEAD_CTX_init` function initialises an `EVP_AEAD_CTX` structure and +// performs any precomputation needed to use `aead` with `key`. The length of +// the key, `key_len`, is given in bytes. // -// The |tag_len| argument contains the length of the tags, in bytes, and allows +// The `tag_len` argument contains the length of the tags, in bytes, and allows // for the processing of truncated authenticators. A zero value indicates that // the default tag length should be used and this is defined as -// |EVP_AEAD_DEFAULT_TAG_LENGTH| in order to make the code clear. Using +// `EVP_AEAD_DEFAULT_TAG_LENGTH` in order to make the code clear. Using // truncated tags increases an attacker's chance of creating a valid forgery. // Be aware that the attacker's chance may increase more than exponentially as // would naively be expected. // -// When no longer needed, the initialised |EVP_AEAD_CTX| structure must be -// passed to |EVP_AEAD_CTX_cleanup|, which will deallocate any memory used. +// When no longer needed, the initialised `EVP_AEAD_CTX` structure must be +// passed to `EVP_AEAD_CTX_cleanup`, which will deallocate any memory used. // -// With an |EVP_AEAD_CTX| in hand, one can seal and open messages. These +// With an `EVP_AEAD_CTX` in hand, one can seal and open messages. These // operations are intended to meet the standard notions of privacy and // authenticity for authenticated encryption. For formal definitions see // Bellare and Namprempre, "Authenticated encryption: relations among notions @@ -53,7 +53,7 @@ extern "C" { // http://www-cse.ucsd.edu/~mihir/papers/oem.html. // // When sealing messages, a nonce must be given. The length of the nonce is -// fixed by the AEAD in use and is returned by |EVP_AEAD_nonce_length|. *The +// fixed by the AEAD in use and is returned by `EVP_AEAD_nonce_length`. *The // nonce must be unique for all messages with the same key*. This is critically // important - nonce reuse may completely undermine the security of the AEAD. // Nonces may be predictable and public, so long as they are unique. Uniqueness @@ -77,12 +77,12 @@ extern "C" { // violated. // // The "seal" and "open" operations also permit additional data to be -// authenticated via the |ad| parameter. This data is not included in the +// authenticated via the `ad` parameter. This data is not included in the // ciphertext and must be identical for both the "seal" and "open" call. This // permits implicit context to be authenticated but may be empty if not needed. // -// The "seal" and "open" operations may work in-place if the |out| and |in| -// arguments are equal. Otherwise, if |out| and |in| alias, input data may be +// The "seal" and "open" operations may work in-place if the `out` and `in` +// arguments are equal. Otherwise, if `out` and `in` alias, input data may be // overwritten before it is read. This situation will cause an error. // // The "seal" and "open" operations return one on success and zero on error. @@ -135,7 +135,7 @@ OPENSSL_EXPORT const EVP_AEAD *EVP_aead_xchacha20_poly1305(void); OPENSSL_EXPORT const EVP_AEAD *EVP_aead_aes_128_ctr_hmac_sha256(void); // EVP_aead_aes_256_ctr_hmac_sha256 is AES-256 in CTR mode with HMAC-SHA256 for -// authentication. See |EVP_aead_aes_128_ctr_hmac_sha256| for details. +// authentication. See `EVP_aead_aes_128_ctr_hmac_sha256` for details. OPENSSL_EXPORT const EVP_AEAD *EVP_aead_aes_256_ctr_hmac_sha256(void); // EVP_aead_aes_128_gcm_siv is AES-128 in GCM-SIV mode. See RFC 8452. @@ -200,20 +200,20 @@ OPENSSL_EXPORT const EVP_AEAD *EVP_aead_aes_256_eax(void); // Utility functions. // EVP_AEAD_key_length returns the length, in bytes, of the keys used by -// |aead|. +// `aead`. OPENSSL_EXPORT size_t EVP_AEAD_key_length(const EVP_AEAD *aead); // EVP_AEAD_nonce_length returns the length, in bytes, of the per-message nonce -// for |aead|. +// for `aead`. OPENSSL_EXPORT size_t EVP_AEAD_nonce_length(const EVP_AEAD *aead); // EVP_AEAD_max_overhead returns the maximum number of additional bytes added -// by the act of sealing data with |aead|. +// by the act of sealing data with `aead`. OPENSSL_EXPORT size_t EVP_AEAD_max_overhead(const EVP_AEAD *aead); -// EVP_AEAD_max_tag_len returns the maximum tag length when using |aead|. This -// is the largest value that can be passed as |tag_len| to -// |EVP_AEAD_CTX_init|. +// EVP_AEAD_max_tag_len returns the maximum tag length when using `aead`. This +// is the largest value that can be passed as `tag_len` to +// `EVP_AEAD_CTX_init`. OPENSSL_EXPORT size_t EVP_AEAD_max_tag_len(const EVP_AEAD *aead); @@ -224,7 +224,7 @@ union evp_aead_ctx_st_state { uint64_t alignment; }; -// An evp_aead_ctx_st (typedefed as |EVP_AEAD_CTX| in base.h) represents an AEAD +// An evp_aead_ctx_st (typedefed as `EVP_AEAD_CTX` in base.h) represents an AEAD // algorithm configured with a specific key and message-independent IV. struct evp_aead_ctx_st { const EVP_AEAD *aead; @@ -257,119 +257,119 @@ struct evp_aead_ctx_st { // be used. #define EVP_AEAD_DEFAULT_TAG_LENGTH 0 -// EVP_AEAD_CTX_zero sets an uninitialized |ctx| to the zero state. It must be -// initialized with |EVP_AEAD_CTX_init| before use. It is safe, but not -// necessary, to call |EVP_AEAD_CTX_cleanup| in this state. This may be used for -// more uniform cleanup of |EVP_AEAD_CTX|. +// EVP_AEAD_CTX_zero sets an uninitialized `ctx` to the zero state. It must be +// initialized with `EVP_AEAD_CTX_init` before use. It is safe, but not +// necessary, to call `EVP_AEAD_CTX_cleanup` in this state. This may be used for +// more uniform cleanup of `EVP_AEAD_CTX`. OPENSSL_EXPORT void EVP_AEAD_CTX_zero(EVP_AEAD_CTX *ctx); -// EVP_AEAD_CTX_new allocates an |EVP_AEAD_CTX|, calls |EVP_AEAD_CTX_init| and -// returns the |EVP_AEAD_CTX|, or NULL on error. +// EVP_AEAD_CTX_new allocates an `EVP_AEAD_CTX`, calls `EVP_AEAD_CTX_init` and +// returns the `EVP_AEAD_CTX`, or NULL on error. OPENSSL_EXPORT EVP_AEAD_CTX *EVP_AEAD_CTX_new(const EVP_AEAD *aead, const uint8_t *key, size_t key_len, size_t tag_len); -// EVP_AEAD_CTX_free calls |EVP_AEAD_CTX_cleanup| and |OPENSSL_free| on -// |ctx|. +// EVP_AEAD_CTX_free calls `EVP_AEAD_CTX_cleanup` and `OPENSSL_free` on +// `ctx`. OPENSSL_EXPORT void EVP_AEAD_CTX_free(EVP_AEAD_CTX *ctx); -// EVP_AEAD_CTX_init initializes |ctx| for the given AEAD algorithm. The |impl| +// EVP_AEAD_CTX_init initializes `ctx` for the given AEAD algorithm. The `impl` // argument is ignored and should be NULL. Authentication tags may be truncated -// by passing a size as |tag_len|. A |tag_len| of zero indicates the default +// by passing a size as `tag_len`. A `tag_len` of zero indicates the default // tag length and this is defined as EVP_AEAD_DEFAULT_TAG_LENGTH for // readability. // // Returns 1 on success. Otherwise returns 0 and pushes to the error stack. In -// the error case, you do not need to call |EVP_AEAD_CTX_cleanup|, but it's +// the error case, you do not need to call `EVP_AEAD_CTX_cleanup`, but it's // harmless to do so. OPENSSL_EXPORT int EVP_AEAD_CTX_init(EVP_AEAD_CTX *ctx, const EVP_AEAD *aead, const uint8_t *key, size_t key_len, size_t tag_len, ENGINE *impl); -// EVP_AEAD_CTX_cleanup frees any data allocated by |ctx|. It is a no-op to -// call |EVP_AEAD_CTX_cleanup| on a |EVP_AEAD_CTX| that has been |memset| to +// EVP_AEAD_CTX_cleanup frees any data allocated by `ctx`. It is a no-op to +// call `EVP_AEAD_CTX_cleanup` on a `EVP_AEAD_CTX` that has been `memset` to // all zeros. OPENSSL_EXPORT void EVP_AEAD_CTX_cleanup(EVP_AEAD_CTX *ctx); -// EVP_AEAD_CTX_seal encrypts and authenticates |in_len| bytes from |in| and -// authenticates |ad_len| bytes from |ad| and writes the result to |out|. It +// EVP_AEAD_CTX_seal encrypts and authenticates `in_len` bytes from `in` and +// authenticates `ad_len` bytes from `ad` and writes the result to `out`. It // returns one on success and zero otherwise. // // This function may be called concurrently with itself or any other seal/open -// function on the same |EVP_AEAD_CTX|. +// function on the same `EVP_AEAD_CTX`. // -// At most |max_out_len| bytes are written to |out| and, in order to ensure -// success, |max_out_len| should be |in_len| plus the result of -// |EVP_AEAD_max_overhead|. On successful return, |*out_len| is set to the +// At most `max_out_len` bytes are written to `out` and, in order to ensure +// success, `max_out_len` should be `in_len` plus the result of +// `EVP_AEAD_max_overhead`. On successful return, `*out_len` is set to the // actual number of bytes written. // -// The length of |nonce|, |nonce_len|, must be equal to the result of -// |EVP_AEAD_nonce_length| for this AEAD. +// The length of `nonce`, `nonce_len`, must be equal to the result of +// `EVP_AEAD_nonce_length` for this AEAD. // -// |EVP_AEAD_CTX_seal| never results in a partial output. If |max_out_len| is -// insufficient, zero will be returned. If any error occurs, |out| will be -// filled with zero bytes and |*out_len| set to zero. +// `EVP_AEAD_CTX_seal` never results in a partial output. If `max_out_len` is +// insufficient, zero will be returned. If any error occurs, `out` will be +// filled with zero bytes and `*out_len` set to zero. // -// If |in| and |out| alias then |out| must be == |in|. +// If `in` and `out` alias then `out` must be == `in`. OPENSSL_EXPORT int EVP_AEAD_CTX_seal(const EVP_AEAD_CTX *ctx, uint8_t *out, size_t *out_len, size_t max_out_len, const uint8_t *nonce, size_t nonce_len, const uint8_t *in, size_t in_len, const uint8_t *ad, size_t ad_len); -// EVP_AEAD_CTX_open authenticates |in_len| bytes from |in| and |ad_len| bytes -// from |ad| and decrypts at most |in_len| bytes into |out|. It returns one on +// EVP_AEAD_CTX_open authenticates `in_len` bytes from `in` and `ad_len` bytes +// from `ad` and decrypts at most `in_len` bytes into `out`. It returns one on // success and zero otherwise. // // This function may be called concurrently with itself or any other seal/open -// function on the same |EVP_AEAD_CTX|. +// function on the same `EVP_AEAD_CTX`. // -// At most |in_len| bytes are written to |out|. In order to ensure success, -// |max_out_len| should be at least |in_len|. On successful return, |*out_len| -// is set to the the actual number of bytes written. +// At most `in_len` bytes are written to `out`. In order to ensure success, +// `max_out_len` should be at least `in_len`. On successful return, `*out_len` +// is set to the actual number of bytes written. // -// The length of |nonce|, |nonce_len|, must be equal to the result of -// |EVP_AEAD_nonce_length| for this AEAD. +// The length of `nonce`, `nonce_len`, must be equal to the result of +// `EVP_AEAD_nonce_length` for this AEAD. // -// |EVP_AEAD_CTX_open| never results in a partial output. If |max_out_len| is -// insufficient, zero will be returned. If any error occurs, |out| will be -// filled with zero bytes and |*out_len| set to zero. +// `EVP_AEAD_CTX_open` never results in a partial output. If `max_out_len` is +// insufficient, zero will be returned. If any error occurs, `out` will be +// filled with zero bytes and `*out_len` set to zero. // -// If |in| and |out| alias then |out| must be == |in|. +// If `in` and `out` alias then `out` must be == `in`. OPENSSL_EXPORT int EVP_AEAD_CTX_open(const EVP_AEAD_CTX *ctx, uint8_t *out, size_t *out_len, size_t max_out_len, const uint8_t *nonce, size_t nonce_len, const uint8_t *in, size_t in_len, const uint8_t *ad, size_t ad_len); -// EVP_AEAD_CTX_seal_scatter encrypts and authenticates |in_len| bytes from |in| -// and authenticates |ad_len| bytes from |ad|. It writes |in_len| bytes of -// ciphertext to |out| and the authentication tag to |out_tag|. It returns one +// EVP_AEAD_CTX_seal_scatter encrypts and authenticates `in_len` bytes from `in` +// and authenticates `ad_len` bytes from `ad`. It writes `in_len` bytes of +// ciphertext to `out` and the authentication tag to `out_tag`. It returns one // on success and zero otherwise. // // This function may be called concurrently with itself or any other seal/open -// function on the same |EVP_AEAD_CTX|. +// function on the same `EVP_AEAD_CTX`. // -// Exactly |in_len| bytes are written to |out|, and up to -// |EVP_AEAD_max_overhead+extra_in_len| bytes to |out_tag|. On successful -// return, |*out_tag_len| is set to the actual number of bytes written to -// |out_tag|. +// Exactly `in_len` bytes are written to `out`, and up to +// `EVP_AEAD_max_overhead+extra_in_len` bytes to `out_tag`. On successful +// return, `*out_tag_len` is set to the actual number of bytes written to +// `out_tag`. // -// |extra_in| may point to an additional plaintext input buffer if the cipher -// supports it. If present, |extra_in_len| additional bytes of plaintext are +// `extra_in` may point to an additional plaintext input buffer if the cipher +// supports it. If present, `extra_in_len` additional bytes of plaintext are // encrypted and authenticated, and the ciphertext is written (before the tag) -// to |out_tag|. |max_out_tag_len| must be sized to allow for the additional -// |extra_in_len| bytes. +// to `out_tag`. `max_out_tag_len` must be sized to allow for the additional +// `extra_in_len` bytes. // -// The length of |nonce|, |nonce_len|, must be equal to the result of -// |EVP_AEAD_nonce_length| for this AEAD. +// The length of `nonce`, `nonce_len`, must be equal to the result of +// `EVP_AEAD_nonce_length` for this AEAD. // -// |EVP_AEAD_CTX_seal_scatter| never results in a partial output. If -// |max_out_tag_len| is insufficient, zero will be returned. If any error -// occurs, |out| and |out_tag| will be filled with zero bytes and |*out_tag_len| +// `EVP_AEAD_CTX_seal_scatter` never results in a partial output. If +// `max_out_tag_len` is insufficient, zero will be returned. If any error +// occurs, `out` and `out_tag` will be filled with zero bytes and `*out_tag_len` // set to zero. // -// If |in| and |out| alias then |out| must be == |in|. |out_tag| may not alias +// If `in` and `out` alias then `out` must be == `in`. `out_tag` may not alias // any other argument. OPENSSL_EXPORT int EVP_AEAD_CTX_seal_scatter( const EVP_AEAD_CTX *ctx, uint8_t *out, uint8_t *out_tag, @@ -377,81 +377,73 @@ OPENSSL_EXPORT int EVP_AEAD_CTX_seal_scatter( size_t nonce_len, const uint8_t *in, size_t in_len, const uint8_t *extra_in, size_t extra_in_len, const uint8_t *ad, size_t ad_len); -// EVP_AEAD_CTX_open_gather decrypts and authenticates |in_len| bytes from |in| -// and authenticates |ad_len| bytes from |ad| using |in_tag_len| bytes of -// authentication tag from |in_tag|. If successful, it writes |in_len| bytes of -// plaintext to |out|. It returns one on success and zero otherwise. +// EVP_AEAD_CTX_open_gather decrypts and authenticates `in_len` bytes from `in` +// and authenticates `ad_len` bytes from `ad` using `in_tag_len` bytes of +// authentication tag from `in_tag`. If successful, it writes `in_len` bytes of +// plaintext to `out`. It returns one on success and zero otherwise. // // This function may be called concurrently with itself or any other seal/open -// function on the same |EVP_AEAD_CTX|. +// function on the same `EVP_AEAD_CTX`. // -// The length of |nonce|, |nonce_len|, must be equal to the result of -// |EVP_AEAD_nonce_length| for this AEAD. +// The length of `nonce`, `nonce_len`, must be equal to the result of +// `EVP_AEAD_nonce_length` for this AEAD. // -// |EVP_AEAD_CTX_open_gather| never results in a partial output. If any error -// occurs, |out| will be filled with zero bytes. +// `EVP_AEAD_CTX_open_gather` never results in a partial output. If any error +// occurs, `out` will be filled with zero bytes. // -// If |in| and |out| alias then |out| must be == |in|. +// If `in` and `out` alias then `out` must be == `in`. OPENSSL_EXPORT int EVP_AEAD_CTX_open_gather( const EVP_AEAD_CTX *ctx, uint8_t *out, const uint8_t *nonce, size_t nonce_len, const uint8_t *in, size_t in_len, const uint8_t *in_tag, size_t in_tag_len, const uint8_t *ad, size_t ad_len); -// crypto_ivec_st (aka |CRYPTO_IVEC|) combines a pointer to input data with its -// length. It is usually passed as an array of length of at most -// |CRYPTO_IOVEC_MAX|. +// crypto_ivec_st (aka `CRYPTO_IVEC`) combines a pointer to input data with its +// length. It is usually passed as an array. struct crypto_ivec_st { const uint8_t *in; size_t len; }; -// crypto_iovec_st (aka |CRYPTO_IOVEC| combines a pointer to input data and a -// pointer to an output buffer with their common length. It is usually passed -// as an array of length of at most |CRYPTO_IOVEC_MAX|. +// crypto_iovec_st (aka `CRYPTO_IOVEC` combines a pointer to input data and a +// pointer to an output buffer with their common length. struct crypto_iovec_st { - // |out| and |in| must be disjoint or equal + // `out` and `in` must be disjoint or equal uint8_t *out; const uint8_t *in; size_t len; }; -// CRYPTO_IOVEC_MAX is the maximum number of entries in an |CRYPTO_IOVEC| or -// |CRYPTO_IVEC| parameter. -#define CRYPTO_IOVEC_MAX 16 - -// EVP_AEAD_CTX_sealv encrypts and authenticates the |in| bytes from |iovec| -// and authenticates the |aadvec| bytes. It writes the same amount of -// ciphertext to the |out| pointers of |iovec| and the authentication tag to -// |out_tag|. It returns one on success and zero otherwise. +// EVP_AEAD_CTX_sealv encrypts and authenticates the `in` bytes from `iovec` +// and authenticates the `aadvec` bytes. It writes the same amount of +// ciphertext to the `out` pointers of `iovec` and the authentication tag to +// `out_tag`. It returns one on success and zero otherwise. // -// This function computes the same output as |EVP_AEAD_CTX_seal_scatter|, but +// This function computes the same output as `EVP_AEAD_CTX_seal_scatter`, but // without requiring the input or output to be a contiguous buffer. The // individual input and output pieces are logically concatenated for a single // operation; their boundaries are not semantically significant and will not // impact the output. // // This function may be called concurrently with itself or any other seal/open -// function on the same |EVP_AEAD_CTX|. +// function on the same `EVP_AEAD_CTX`. // -// Exactly |len| bytes are written to each |out| member of the |iovec|, and up -// to |EVP_AEAD_max_overhead+extra_in_len| bytes to |out_tag|. On successful -// return, |*out_tag_len| is set to the actual number of bytes written to -// |out_tag|. +// Exactly `len` bytes are written to each `out` member of the `iovec`, and up +// to `EVP_AEAD_max_overhead+extra_in_len` bytes to `out_tag`. On successful +// return, `*out_tag_len` is set to the actual number of bytes written to +// `out_tag`. // -// The length of |nonce|, |nonce_len|, must be equal to the result of -// |EVP_AEAD_nonce_length| for this AEAD. +// The length of `nonce`, `nonce_len`, must be equal to the result of +// `EVP_AEAD_nonce_length` for this AEAD. // -// |EVP_AEAD_CTX_sealv| never results in a partial output. If |max_out_tag_len| -// is insufficient, zero will be returned. If any error occurs, the |out| -// members of the |iovec| and |out_tag| will be filled with zero bytes and -// |*out_tag_len| set to zero. +// `EVP_AEAD_CTX_sealv` never results in a partial output. If `max_out_tag_len` +// is insufficient, zero will be returned. If any error occurs, the `out` +// members of the `iovec` and `out_tag` will be filled with zero bytes and +// `*out_tag_len` set to zero. // // No output pointer may alias any other pointer passed to this function either -// directly or via |iovec| and |aadvec|, with the one exception that it is -// permitted for the same |iovec| member's |in| and |out| members to be equal +// directly or via `iovec` and `aadvec`, with the one exception that it is +// permitted for the same `iovec` member's `in` and `out` members to be equal // (in-place operation). -// -// |num_iovec| and |num_aadvec| must be <= |CRYPTO_IOVEC_MAX|. OPENSSL_EXPORT int EVP_AEAD_CTX_sealv(const EVP_AEAD_CTX *ctx, const CRYPTO_IOVEC *iovec, size_t num_iovec, uint8_t *out_tag, size_t *out_tag_len, @@ -459,80 +451,76 @@ int EVP_AEAD_CTX_sealv(const EVP_AEAD_CTX *ctx, const CRYPTO_IOVEC *iovec, size_t nonce_len, const CRYPTO_IVEC *aadvec, size_t num_aadvec); -// EVP_AEAD_CTX_openv authenticates the |in| bytes from |iovec| and |aadvec|, -// and decrypts the |in| bytes to the |out| pointers of |iovec|. It returns one +// EVP_AEAD_CTX_openv authenticates the `in` bytes from `iovec` and `aadvec`, +// and decrypts the `in` bytes to the `out` pointers of `iovec`. It returns one // on success and zero otherwise. // -// This function computes the same output as |EVP_AEAD_CTX_open|, but without +// This function computes the same output as `EVP_AEAD_CTX_open`, but without // requiring the input or output to be a contiguous buffer. The individual // input and output pieces are logically concatenated for a single operation; // their boundaries are not semantically significant and will not impact the // output. // // This function may (and usually will) output less than the total length of -// |iovec|. In this case, it outputs to a prefix of |iovec|'s output space, +// `iovec`. In this case, it outputs to a prefix of `iovec`'s output space, // then returns the length of what was actually written. // // In AEADs with a fixed-length authentication tag, the tag is treated as if it // were appended to the ciphertext, and successful outputs will always be // exactly the tag length shorter. To open with a separate, detached tag, -// either provide it as a separate |CRYPTO_IOVEC|, or use -// |EVP_AEAD_CTX_openv_detached|. The latter may be more convenient, one does -// not need consider the possibility of output to the final |CRYPTO_IOVEC|. +// either provide it as a separate `CRYPTO_IOVEC`, or use +// `EVP_AEAD_CTX_openv_detached`. The latter may be more convenient, one does +// not need consider the possibility of output to the final `CRYPTO_IOVEC`. // // This function may be called concurrently with itself or any other seal/open -// function on the same |EVP_AEAD_CTX|. +// function on the same `EVP_AEAD_CTX`. // -// The length of |nonce|, |nonce_len|, must be equal to the result of -// |EVP_AEAD_nonce_length| for this AEAD. +// The length of `nonce`, `nonce_len`, must be equal to the result of +// `EVP_AEAD_nonce_length` for this AEAD. // -// |EVP_AEAD_CTX_openv| never results in a partial output. If any error occurs, -// |out| will be filled with zero bytes and |*out_len| set to zero. +// `EVP_AEAD_CTX_openv` never results in a partial output. If any error occurs, +// `out` will be filled with zero bytes and `*out_len` set to zero. // // No output pointer may alias any other pointer passed to this function either -// directly or via |iovec| and |aadvec|, with the one exception that it is -// permitted for the same |iovec| member's |in| and |out| members to be equal +// directly or via `iovec` and `aadvec`, with the one exception that it is +// permitted for the same `iovec` member's `in` and `out` members to be equal // (in-place operation). -// -// |num_iovec| and |num_aadvec| must be <= |CRYPTO_IOVEC_MAX|. OPENSSL_EXPORT int EVP_AEAD_CTX_openv(const EVP_AEAD_CTX *ctx, const CRYPTO_IOVEC *iovec, size_t num_iovec, size_t *out_total_bytes, const uint8_t *nonce, size_t nonce_len, const CRYPTO_IVEC *aadvec, size_t num_aadvec); -// EVP_AEAD_CTX_openv_detached authenticates the |in| bytes from |iovec| and -// |aadvec| using |in_tag_len| bytes of authentication tag from |in_tag|. If -// successful, it writes the plaintext of the |in| bytes to the |out| pointers -// of |iovec|. It returns one on success and zero otherwise. +// EVP_AEAD_CTX_openv_detached authenticates the `in` bytes from `iovec` and +// `aadvec` using `in_tag_len` bytes of authentication tag from `in_tag`. If +// successful, it writes the plaintext of the `in` bytes to the `out` pointers +// of `iovec`. It returns one on success and zero otherwise. // // This function is usable with AEADs whose output can be split into a // ciphertext portion, with the same length as the plaintext, and a // fixed-length authentication tag. If the AEAD has a variable-length overhead, // this function will return zero. Such AEADs can only be used with -// |EVP_AEAD_CTX_openv|. +// `EVP_AEAD_CTX_openv`. // -// This function computes the same output as |EVP_AEAD_CTX_open|, but with a +// This function computes the same output as `EVP_AEAD_CTX_open`, but with a // detached tag and without requiring the input or output to be a contiguous // buffer. The individual input and output pieces are logically concatenated // for a single operation; their boundaries are not semantically significant // and will not impact the output. // // This function may be called concurrently with itself or any other seal/open -// function on the same |EVP_AEAD_CTX|. +// function on the same `EVP_AEAD_CTX`. // -// The length of |nonce|, |nonce_len|, must be equal to the result of -// |EVP_AEAD_nonce_length| for this AEAD. +// The length of `nonce`, `nonce_len`, must be equal to the result of +// `EVP_AEAD_nonce_length` for this AEAD. // -// |EVP_AEAD_CTX_openv_detached| never results in a partial output. If any -// error occurs, |out| will be filled with zero bytes. +// `EVP_AEAD_CTX_openv_detached` never results in a partial output. If any +// error occurs, `out` will be filled with zero bytes. // // No output pointer may alias any other pointer passed to this function either -// directly or via |iovec| and |aadvec|, with the one exception that it is -// permitted for the same |iovec| member's |in| and |out| members to be equal +// directly or via `iovec` and `aadvec`, with the one exception that it is +// permitted for the same `iovec` member's `in` and `out` members to be equal // (in-place operation). -// -// |num_iovec| and |num_aadvec| must be <= |CRYPTO_IOVEC_MAX|. OPENSSL_EXPORT int EVP_AEAD_CTX_openv_detached(const EVP_AEAD_CTX *ctx, const CRYPTO_IOVEC *iovec, size_t num_iovec, @@ -540,45 +528,61 @@ int EVP_AEAD_CTX_openv_detached(const EVP_AEAD_CTX *ctx, const uint8_t *in_tag, size_t in_tag_len, const CRYPTO_IVEC *aadvec, size_t num_aadvec); -// EVP_AEAD_CTX_aead returns the underlying AEAD for |ctx|, or NULL if one has +// EVP_AEAD_CTX_aead returns the underlying AEAD for `ctx`, or NULL if one has // not been set. OPENSSL_EXPORT const EVP_AEAD *EVP_AEAD_CTX_aead(const EVP_AEAD_CTX *ctx); // TLS-specific AEAD algorithms. // -// These AEAD primitives do not meet the definition of generic AEADs. They are -// all specific to TLS and should not be used outside of that context. They must -// be initialized with |EVP_AEAD_CTX_init_with_direction|, are stateful, and may -// not be used concurrently. Any nonces are used as IVs, so they must be -// unpredictable. They only accept an |ad| parameter of length 11 (the standard -// TLS one with length omitted). +// WARNING: These `EVP_AEAD` objects primitives do not meet the definition of +// generic AEADs. They are all specific to TLS and should not be used outside of +// that context. They break the usual guarantees around `EVP_AEAD_CTX`, +// including statefulness, thread-safety, initialization conventions, and +// security requirements around the nonce parameter. +// The following functions implement legacy TLS CBC cipher suites. +// +// WARNING: These functions are effectively internal implementation details of +// libssl, not general-purpose constructions. They are not true AEADs and differ +// from a normal `EVP_AEAD` in many ways: +// +// * They must be initialized with `EVP_AEAD_CTX_init_with_direction`, not +// `EVP_AEAD_CTX_init`. +// +// * The resulting `EVP_AEAD_CTX`s are stateful. Neither `EVP_AEAD_CTX_open*` +// nor `EVP_AEAD_CTX_seal*` can be called concurrently. +// +// * The `ad` parameter must have length 11 (the standard TLS one with length +// omitted). +// +// * The `nonce` parameter is used as a CBC IV and must be unpredictable, not +// just unique. +// +// * The `*_implicit_iv` variants implicitly set the IV to the last block of the +// previous message. They are vulnerable the BEAST attack unless the caller +// applies record-splitting mitigations externally. OPENSSL_EXPORT const EVP_AEAD *EVP_aead_aes_128_cbc_sha1_tls(void); OPENSSL_EXPORT const EVP_AEAD *EVP_aead_aes_128_cbc_sha1_tls_implicit_iv(void); - OPENSSL_EXPORT const EVP_AEAD *EVP_aead_aes_128_cbc_sha256_tls(void); - OPENSSL_EXPORT const EVP_AEAD *EVP_aead_aes_256_cbc_sha1_tls(void); OPENSSL_EXPORT const EVP_AEAD *EVP_aead_aes_256_cbc_sha1_tls_implicit_iv(void); - OPENSSL_EXPORT const EVP_AEAD *EVP_aead_des_ede3_cbc_sha1_tls(void); OPENSSL_EXPORT const EVP_AEAD *EVP_aead_des_ede3_cbc_sha1_tls_implicit_iv(void); -// EVP_aead_aes_128_gcm_tls12 is AES-128 in Galois Counter Mode using the TLS -// 1.2 nonce construction. +// The following functions behave like `EVP_aead_aes_128_gcm` or +// `EVP_aead_aes_256_gcm`, except that seal operations fail if nonces do not +// match the TLS 1.2 or TLS 1.3 nonce construction. +// +// These functions are only applicable for callers that want an extra AEAD-level +// nonce check. `EVP_aead_aes_128_gcm` and `EVP_aead_aes_256_gcm` are otherwise +// suitable for implementing TLS and will produce the same output. +// +// WARNING: `EVP_AEAD_CTX` objects initialized with these `EVP_AEAD`s are +// stateful. `EVP_AEAD_CTX_seal*` cannot be called concurrently. OPENSSL_EXPORT const EVP_AEAD *EVP_aead_aes_128_gcm_tls12(void); - -// EVP_aead_aes_256_gcm_tls12 is AES-256 in Galois Counter Mode using the TLS -// 1.2 nonce construction. OPENSSL_EXPORT const EVP_AEAD *EVP_aead_aes_256_gcm_tls12(void); - -// EVP_aead_aes_128_gcm_tls13 is AES-128 in Galois Counter Mode using the TLS -// 1.3 nonce construction. OPENSSL_EXPORT const EVP_AEAD *EVP_aead_aes_128_gcm_tls13(void); - -// EVP_aead_aes_256_gcm_tls13 is AES-256 in Galois Counter Mode using the TLS -// 1.3 nonce construction. OPENSSL_EXPORT const EVP_AEAD *EVP_aead_aes_256_gcm_tls13(void); @@ -590,15 +594,14 @@ enum evp_aead_direction_t { evp_aead_seal, }; -// EVP_AEAD_CTX_init_with_direction calls |EVP_AEAD_CTX_init| for normal -// AEADs. For TLS-specific and SSL3-specific AEADs, it initializes |ctx| for a -// given direction. +// EVP_AEAD_CTX_init_with_direction calls `EVP_AEAD_CTX_init` for normal +// AEADs. For TLS-specific AEADs, it initializes `ctx` for a given direction. OPENSSL_EXPORT int EVP_AEAD_CTX_init_with_direction( EVP_AEAD_CTX *ctx, const EVP_AEAD *aead, const uint8_t *key, size_t key_len, size_t tag_len, enum evp_aead_direction_t dir); -// EVP_AEAD_CTX_get_iv sets |*out_len| to the length of the IV for |ctx| and -// sets |*out_iv| to point to that many bytes of the current IV. This is only +// EVP_AEAD_CTX_get_iv sets `*out_len` to the length of the IV for `ctx` and +// sets `*out_iv` to point to that many bytes of the current IV. This is only // meaningful for AEADs with implicit IVs (i.e. CBC mode in TLS 1.0). // // It returns one on success or zero on error. @@ -606,17 +609,17 @@ OPENSSL_EXPORT int EVP_AEAD_CTX_get_iv(const EVP_AEAD_CTX *ctx, const uint8_t **out_iv, size_t *out_len); // EVP_AEAD_CTX_tag_len computes the exact byte length of the tag written by -// |EVP_AEAD_CTX_seal_scatter| and writes it to |*out_tag_len|. It returns one -// on success or zero on error. |in_len| and |extra_in_len| must equal the -// arguments of the same names passed to |EVP_AEAD_CTX_seal_scatter|. +// `EVP_AEAD_CTX_seal_scatter` and writes it to `*out_tag_len`. It returns one +// on success or zero on error. `in_len` and `extra_in_len` must equal the +// arguments of the same names passed to `EVP_AEAD_CTX_seal_scatter`. // // To compute the exact byte length of the output written by -// |EVP_AEAD_CTX_seal|, set |extra_in_len| to zero and add |in_len| to the +// `EVP_AEAD_CTX_seal`, set `extra_in_len` to zero and add `in_len` to the // result. // -// To compute the exact byte length of the tag written by |EVP_AEAD_CTX_sealv|, -// set |in_len| to the sum of |len| over the entire |iovec|, and set -// |extra_in_len| to zero. +// To compute the exact byte length of the tag written by `EVP_AEAD_CTX_sealv`, +// set `in_len` to the sum of `len` over the entire `iovec`, and set +// `extra_in_len` to zero. OPENSSL_EXPORT int EVP_AEAD_CTX_tag_len(const EVP_AEAD_CTX *ctx, size_t *out_tag_len, const size_t in_len, @@ -631,9 +634,9 @@ extern "C++" { BSSL_NAMESPACE_BEGIN -using ScopedEVP_AEAD_CTX = - internal::StackAllocated; +BORINGSSL_MAKE_STACK_TRAITS(EVP_AEAD_CTX, EVP_AEAD_CTX_zero, + EVP_AEAD_CTX_cleanup) +using ScopedEVP_AEAD_CTX = internal::StackAllocated; BORINGSSL_MAKE_DELETER(EVP_AEAD_CTX, EVP_AEAD_CTX_free) diff --git a/third_party/boringssl/src/include/openssl/aes.h b/third_party/boringssl/src/include/openssl/aes.h index eee88a510..5dd0cfdcb 100644 --- a/third_party/boringssl/src/include/openssl/aes.h +++ b/third_party/boringssl/src/include/openssl/aes.h @@ -41,39 +41,39 @@ struct aes_key_st { }; typedef struct aes_key_st AES_KEY; -// AES_set_encrypt_key configures |aeskey| to encrypt with the |bits|-bit key, -// |key|. |key| must point to |bits|/8 bytes. It returns zero on success and a -// negative number if |bits| is an invalid AES key size. +// AES_set_encrypt_key configures `aeskey` to encrypt with the `bits`-bit key, +// `key`. `key` must point to `bits`/8 bytes. It returns zero on success and a +// negative number if `bits` is an invalid AES key size. // // WARNING: this function breaks the usual return value convention. OPENSSL_EXPORT int AES_set_encrypt_key(const uint8_t *key, unsigned bits, AES_KEY *aeskey); -// AES_set_decrypt_key configures |aeskey| to decrypt with the |bits|-bit key, -// |key|. |key| must point to |bits|/8 bytes. It returns zero on success and a -// negative number if |bits| is an invalid AES key size. +// AES_set_decrypt_key configures `aeskey` to decrypt with the `bits`-bit key, +// `key`. `key` must point to `bits`/8 bytes. It returns zero on success and a +// negative number if `bits` is an invalid AES key size. // // WARNING: this function breaks the usual return value convention. OPENSSL_EXPORT int AES_set_decrypt_key(const uint8_t *key, unsigned bits, AES_KEY *aeskey); -// AES_encrypt encrypts a single block from |in| to |out| with |key|. The |in| -// and |out| pointers may overlap. +// AES_encrypt encrypts a single block from `in` to `out` with `key`. The `in` +// and `out` pointers may overlap. OPENSSL_EXPORT void AES_encrypt(const uint8_t *in, uint8_t *out, const AES_KEY *key); -// AES_decrypt decrypts a single block from |in| to |out| with |key|. The |in| -// and |out| pointers may overlap. +// AES_decrypt decrypts a single block from `in` to `out` with `key`. The `in` +// and `out` pointers may overlap. OPENSSL_EXPORT void AES_decrypt(const uint8_t *in, uint8_t *out, const AES_KEY *key); // Block cipher modes. -// AES_ctr128_encrypt encrypts (or decrypts, it's the same in CTR mode) |len| -// bytes from |in| to |out|. The |num| parameter must be set to zero on the -// first call and |ivec| will be incremented. This function may be called -// in-place with |in| equal to |out|, but otherwise the buffers may not +// AES_ctr128_encrypt encrypts (or decrypts, it's the same in CTR mode) `len` +// bytes from `in` to `out`. The `num` parameter must be set to zero on the +// first call and `ivec` will be incremented. This function may be called +// in-place with `in` equal to `out`, but otherwise the buffers may not // partially overlap. A partial overlap may overwrite input data before it is // read. OPENSSL_EXPORT void AES_ctr128_encrypt(const uint8_t *in, uint8_t *out, @@ -82,34 +82,34 @@ OPENSSL_EXPORT void AES_ctr128_encrypt(const uint8_t *in, uint8_t *out, uint8_t ecount_buf[AES_BLOCK_SIZE], unsigned int *num); -// AES_ecb_encrypt encrypts (or decrypts, if |enc| == |AES_DECRYPT|) a single, -// 16 byte block from |in| to |out|. This function may be called in-place with -// |in| equal to |out|, but otherwise the buffers may not partially overlap. A +// AES_ecb_encrypt encrypts (or decrypts, if `enc` == `AES_DECRYPT`) a single, +// 16 byte block from `in` to `out`. This function may be called in-place with +// `in` equal to `out`, but otherwise the buffers may not partially overlap. A // partial overlap may overwrite input data before it is read. OPENSSL_EXPORT void AES_ecb_encrypt(const uint8_t *in, uint8_t *out, const AES_KEY *key, const int enc); -// AES_cbc_encrypt encrypts (or decrypts, if |enc| == |AES_DECRYPT|) |len| -// bytes from |in| to |out|. The length must be a multiple of the block size. -// This function may be called in-place with |in| equal to |out|, but otherwise +// AES_cbc_encrypt encrypts (or decrypts, if `enc` == `AES_DECRYPT`) `len` +// bytes from `in` to `out`. The length must be a multiple of the block size. +// This function may be called in-place with `in` equal to `out`, but otherwise // the buffers may not partially overlap. A partial overlap may overwrite input // data before it is read. OPENSSL_EXPORT void AES_cbc_encrypt(const uint8_t *in, uint8_t *out, size_t len, const AES_KEY *key, uint8_t *ivec, const int enc); -// AES_ofb128_encrypt encrypts (or decrypts, it's the same in OFB mode) |len| -// bytes from |in| to |out|. The |num| parameter must be set to zero on the -// first call. This function may be called in-place with |in| equal to |out|, +// AES_ofb128_encrypt encrypts (or decrypts, it's the same in OFB mode) `len` +// bytes from `in` to `out`. The `num` parameter must be set to zero on the +// first call. This function may be called in-place with `in` equal to `out`, // but otherwise the buffers may not partially overlap. A partial overlap may // overwrite input data before it is read. OPENSSL_EXPORT void AES_ofb128_encrypt(const uint8_t *in, uint8_t *out, size_t len, const AES_KEY *key, uint8_t *ivec, int *num); -// AES_cfb128_encrypt encrypts (or decrypts, if |enc| == |AES_DECRYPT|) |len| -// bytes from |in| to |out|. The |num| parameter must be set to zero on the -// first call. This function may be called in-place with |in| equal to |out|, +// AES_cfb128_encrypt encrypts (or decrypts, if `enc` == `AES_DECRYPT`) `len` +// bytes from `in` to `out`. The `num` parameter must be set to zero on the +// first call. This function may be called in-place with `in` equal to `out`, // but otherwise the buffers may not partially overlap. A partial overlap may // overwrite input data before it is read. OPENSSL_EXPORT void AES_cfb128_encrypt(const uint8_t *in, uint8_t *out, @@ -123,18 +123,18 @@ OPENSSL_EXPORT void AES_cfb128_encrypt(const uint8_t *in, uint8_t *out, // should never be used except to interoperate with existing systems that use // this mode. -// AES_wrap_key performs AES key wrap on |in| which must be a multiple of 8 -// bytes. |iv| must point to an 8 byte value or be NULL to use the default IV. -// |key| must have been configured for encryption. On success, it writes -// |in_len| + 8 bytes to |out| and returns |in_len| + 8. Otherwise, it returns +// AES_wrap_key performs AES key wrap on `in` which must be a multiple of 8 +// bytes. `iv` must point to an 8 byte value or be NULL to use the default IV. +// `key` must have been configured for encryption. On success, it writes +// `in_len` + 8 bytes to `out` and returns `in_len` + 8. Otherwise, it returns // -1. OPENSSL_EXPORT int AES_wrap_key(const AES_KEY *key, const uint8_t *iv, uint8_t *out, const uint8_t *in, size_t in_len); -// AES_unwrap_key performs AES key unwrap on |in| which must be a multiple of 8 -// bytes. |iv| must point to an 8 byte value or be NULL to use the default IV. -// |key| must have been configured for decryption. On success, it writes -// |in_len| - 8 bytes to |out| and returns |in_len| - 8. Otherwise, it returns +// AES_unwrap_key performs AES key unwrap on `in` which must be a multiple of 8 +// bytes. `iv` must point to an 8 byte value or be NULL to use the default IV. +// `key` must have been configured for decryption. On success, it writes +// `in_len` - 8 bytes to `out` and returns `in_len` - 8. Otherwise, it returns // -1. OPENSSL_EXPORT int AES_unwrap_key(const AES_KEY *key, const uint8_t *iv, uint8_t *out, const uint8_t *in, @@ -147,20 +147,20 @@ OPENSSL_EXPORT int AES_unwrap_key(const AES_KEY *key, const uint8_t *iv, // 5649. They should never be used except to interoperate with existing systems // that use this mode. -// AES_wrap_key_padded performs a padded AES key wrap on |in| which must be -// between 1 and 2^32-1 bytes. |key| must have been configured for encryption. -// On success it writes at most |max_out| bytes of ciphertext to |out|, sets -// |*out_len| to the number of bytes written, and returns one. On failure it -// returns zero. To ensure success, set |max_out| to at least |in_len| + 15. +// AES_wrap_key_padded performs a padded AES key wrap on `in` which must be +// between 1 and 2^32-1 bytes. `key` must have been configured for encryption. +// On success it writes at most `max_out` bytes of ciphertext to `out`, sets +// `*out_len` to the number of bytes written, and returns one. On failure it +// returns zero. To ensure success, set `max_out` to at least `in_len` + 15. OPENSSL_EXPORT int AES_wrap_key_padded(const AES_KEY *key, uint8_t *out, size_t *out_len, size_t max_out, const uint8_t *in, size_t in_len); -// AES_unwrap_key_padded performs a padded AES key unwrap on |in| which must be -// a multiple of 8 bytes. |key| must have been configured for decryption. On -// success it writes at most |max_out| bytes to |out|, sets |*out_len| to the +// AES_unwrap_key_padded performs a padded AES key unwrap on `in` which must be +// a multiple of 8 bytes. `key` must have been configured for decryption. On +// success it writes at most `max_out` bytes to `out`, sets `*out_len` to the // number of bytes written, and returns one. On failure it returns zero. Setting -// |max_out| to |in_len| is a sensible estimate. +// `max_out` to `in_len` is a sensible estimate. OPENSSL_EXPORT int AES_unwrap_key_padded(const AES_KEY *key, uint8_t *out, size_t *out_len, size_t max_out, const uint8_t *in, size_t in_len); diff --git a/third_party/boringssl/src/include/openssl/asm_base.h b/third_party/boringssl/src/include/openssl/asm_base.h index 926eb6f31..4cea0d148 100644 --- a/third_party/boringssl/src/include/openssl/asm_base.h +++ b/third_party/boringssl/src/include/openssl/asm_base.h @@ -25,9 +25,9 @@ // Every assembly file must include this header. Some linker features require // all object files to be tagged with some section metadata. This header file, // when included in assembly, adds that metadata. It also makes defines like -// |OPENSSL_X86_64| available and includes the prefixing macros. +// `OPENSSL_X86_64` available and includes the prefixing macros. // -// Including this header in an assembly file imples: +// Including this header in an assembly file implies: // // - The file does not require an executable stack. // @@ -70,7 +70,7 @@ #if defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64) -// We require the ARM assembler provide |__ARM_ARCH| from Arm C Language +// We require the ARM assembler provide `__ARM_ARCH` from Arm C Language // Extensions (ACLE). This is supported in GCC 4.8+ and Clang 3.2+. MSVC does // not implement ACLE, but we require Clang's assembler on Windows. #if !defined(__ARM_ARCH) @@ -92,12 +92,12 @@ // features which require emitting a .note.gnu.property section with the // appropriate architecture-dependent feature bits set. // -// |AARCH64_SIGN_LINK_REGISTER| and |AARCH64_VALIDATE_LINK_REGISTER| expand to -// PACIxSP and AUTIxSP, respectively. |AARCH64_SIGN_LINK_REGISTER| should be +// `AARCH64_SIGN_LINK_REGISTER` and `AARCH64_VALIDATE_LINK_REGISTER` expand to +// PACIxSP and AUTIxSP, respectively. `AARCH64_SIGN_LINK_REGISTER` should be // used immediately before saving the LR register (x30) to the stack. -// |AARCH64_VALIDATE_LINK_REGISTER| should be used immediately after restoring -// it. Note |AARCH64_SIGN_LINK_REGISTER|'s modifications to LR must be undone -// with |AARCH64_VALIDATE_LINK_REGISTER| before RET. The SP register must also +// `AARCH64_VALIDATE_LINK_REGISTER` should be used immediately after restoring +// it. Note `AARCH64_SIGN_LINK_REGISTER`'s modifications to LR must be undone +// with `AARCH64_VALIDATE_LINK_REGISTER` before RET. The SP register must also // have the same value at the two points. For example: // // .global f @@ -110,11 +110,11 @@ // AARCH64_VALIDATE_LINK_REGISTER // ret // -// |AARCH64_VALID_CALL_TARGET| expands to BTI 'c'. Either it, or -// |AARCH64_SIGN_LINK_REGISTER|, must be used at every point that may be an +// `AARCH64_VALID_CALL_TARGET` expands to BTI 'c'. Either it, or +// `AARCH64_SIGN_LINK_REGISTER`, must be used at every point that may be an // indirect call target. In particular, all symbols exported from a file must // begin with one of these macros. For example, a leaf function that does not -// save LR can instead use |AARCH64_VALID_CALL_TARGET|: +// save LR can instead use `AARCH64_VALID_CALL_TARGET`: // // .globl return_zero // return_zero: @@ -123,7 +123,7 @@ // ret // // A non-leaf function which does not immediately save LR may need both macros -// because |AARCH64_SIGN_LINK_REGISTER| appears late. For example, the function +// because `AARCH64_SIGN_LINK_REGISTER` appears late. For example, the function // may jump to an alternate implementation before setting up the stack: // // .globl with_early_jump @@ -145,7 +145,7 @@ // // These annotations are only required with indirect calls. Private symbols that // are only the target of direct calls do not require annotations. Also note -// that |AARCH64_VALID_CALL_TARGET| is only valid for indirect calls (BLR), not +// that `AARCH64_VALID_CALL_TARGET` is only valid for indirect calls (BLR), not // indirect jumps (BR). Indirect jumps in assembly are currently not supported // and would require a macro for BTI 'j'. // @@ -206,6 +206,14 @@ #endif #endif // ARM || AARCH64 +#if defined(__APPLE__) +// Require all Apple assembly to be compatible with `.subsections_via_symbols`. +// Perlasm output will automatically no-op it by marking all non-initials with +// `.alt_entry`. Other assembly must either do something similar or be +// compatible with the optimization. +.subsections_via_symbols +#endif + #endif // __ASSEMBLER__ #endif // OPENSSL_HEADER_ASM_BASE_H diff --git a/third_party/boringssl/src/include/openssl/asn1.h b/third_party/boringssl/src/include/openssl/asn1.h index e89138226..66ccf0efa 100644 --- a/third_party/boringssl/src/include/openssl/asn1.h +++ b/third_party/boringssl/src/include/openssl/asn1.h @@ -15,7 +15,7 @@ #ifndef OPENSSL_HEADER_ASN1_H #define OPENSSL_HEADER_ASN1_H -#include // IWYU pragma: export +#include // IWYU pragma: export #include @@ -36,7 +36,7 @@ extern "C" { // particularly prone to behavior changes and API removals, as BoringSSL // iterates on these issues. // -// Use the new |CBS| and |CBB| library in instead. +// Use the new `CBS` and `CBB` library in instead. // Tag constants. @@ -63,7 +63,7 @@ extern "C" { #define V_ASN1_PRIMITIVE_TAG 0x1f // V_ASN1_MAX_UNIVERSAL is the highest supported universal tag number. It is -// necessary to avoid ambiguity with |V_ASN1_NEG| and |MBSTRING_FLAG|. +// necessary to avoid ambiguity with `V_ASN1_NEG` and `MBSTRING_FLAG`. // // TODO(davidben): Make this private. #define V_ASN1_MAX_UNIVERSAL 0xff @@ -71,7 +71,7 @@ extern "C" { // V_ASN1_UNDEF is used in some APIs to indicate an ASN.1 element is omitted. #define V_ASN1_UNDEF (-1) -// V_ASN1_OTHER is used in |ASN1_TYPE| to indicate a non-universal ASN.1 type. +// V_ASN1_OTHER is used in `ASN1_TYPE` to indicate a non-universal ASN.1 type. #define V_ASN1_OTHER (-3) // V_ASN1_ANY is used by the ASN.1 templates to indicate an ANY type. @@ -90,6 +90,7 @@ extern "C" { #define V_ASN1_REAL 9 #define V_ASN1_ENUMERATED 10 #define V_ASN1_UTF8STRING 12 +#define V_ASN1_RELATIVE_OID 13 #define V_ASN1_SEQUENCE 16 #define V_ASN1_SET 17 #define V_ASN1_NUMERICSTRING 18 @@ -107,8 +108,8 @@ extern "C" { #define V_ASN1_UNIVERSALSTRING 28 #define V_ASN1_BMPSTRING 30 -// The following constants are used for |ASN1_STRING| values that represent -// negative INTEGER and ENUMERATED values. See |ASN1_STRING| for more details. +// The following constants are used for `ASN1_STRING` values that represent +// negative INTEGER and ENUMERATED values. See `ASN1_STRING` for more details. #define V_ASN1_NEG 0x100 #define V_ASN1_NEG_INTEGER (V_ASN1_INTEGER | V_ASN1_NEG) #define V_ASN1_NEG_ENUMERATED (V_ASN1_ENUMERATED | V_ASN1_NEG) @@ -133,12 +134,12 @@ extern "C" { #define B_ASN1_GENERALIZEDTIME 0x8000 #define B_ASN1_SEQUENCE 0x10000 -// ASN1_tag2bit converts |tag| from the tag number of a universal type to a -// corresponding |B_ASN1_*| constant, or zero if |tag| has no bitmask. +// ASN1_tag2bit converts `tag` from the tag number of a universal type to a +// corresponding `B_ASN1_*` constant, or zero if `tag` has no bitmask. OPENSSL_EXPORT unsigned long ASN1_tag2bit(int tag); -// ASN1_tag2str returns a string representation of |tag|, interpret as a tag -// number for a universal type, or |V_ASN1_NEG_*|. +// ASN1_tag2str returns a string representation of `tag`, interpret as a tag +// number for a universal type, or `V_ASN1_NEG_*`. OPENSSL_EXPORT const char *ASN1_tag2str(int tag); @@ -149,36 +150,36 @@ OPENSSL_EXPORT const char *ASN1_tag2str(int tag); #if 0 // Sample functions -// d2i_SAMPLE parses a structure from up to |len| bytes at |*inp|. On success, -// it advances |*inp| by the number of bytes read and returns a newly-allocated -// |SAMPLE| object containing the parsed structure. If |out| is non-NULL, it -// additionally frees the previous value at |*out| and updates |*out| to the +// d2i_SAMPLE parses a structure from up to `len` bytes at `*inp`. On success, +// it advances `*inp` by the number of bytes read and returns a newly-allocated +// `SAMPLE` object containing the parsed structure. If `out` is non-NULL, it +// additionally frees the previous value at `*out` and updates `*out` to the // result. If parsing or allocating the result fails, it returns NULL. // // This function does not reject trailing data in the input. This allows the // caller to parse a sequence of concatenated structures. Callers parsing only -// one structure should check for trailing data by comparing the updated |*inp| +// one structure should check for trailing data by comparing the updated `*inp` // with the end of the input. // -// Note: If |out| and |*out| are both non-NULL, the object at |*out| is not +// Note: If `out` and `*out` are both non-NULL, the object at `*out` is not // updated in-place. Instead, it is freed, and the pointer is updated to the -// new object. This differs from OpenSSL. Callers are recommended to set |out| +// new object. This differs from OpenSSL. Callers are recommended to set `out` // to NULL and instead use the return value. SAMPLE *d2i_SAMPLE(SAMPLE **out, const uint8_t **inp, long len); -// i2d_SAMPLE marshals |in|. On error, it returns a negative value. On success, -// it returns the length of the result and outputs it via |outp| as follows: +// i2d_SAMPLE marshals `in`. On error, it returns a negative value. On success, +// it returns the length of the result and outputs it via `outp` as follows: // -// If |outp| is NULL, the function writes nothing. This mode can be used to size +// If `outp` is NULL, the function writes nothing. This mode can be used to size // buffers. // -// If |outp| is non-NULL but |*outp| is NULL, the function sets |*outp| to a +// If `outp` is non-NULL but `*outp` is NULL, the function sets `*outp` to a // newly-allocated buffer containing the result. The caller is responsible for -// releasing |*outp| with |OPENSSL_free|. This mode is recommended for most +// releasing `*outp` with `OPENSSL_free`. This mode is recommended for most // callers. // -// If |outp| and |*outp| are non-NULL, the function writes the result to -// |*outp|, which must have enough space available, and advances |*outp| just +// If `outp` and `*outp` are non-NULL, the function writes the result to +// `*outp`, which must have enough space available, and advances `*outp` just // past the output. // // WARNING: In the third mode, the function does not internally check output @@ -189,7 +190,7 @@ int i2d_SAMPLE(const SAMPLE *in, uint8_t **outp); #endif // Sample functions // The following typedefs are sometimes used for pointers to functions like -// |d2i_SAMPLE| and |i2d_SAMPLE|. Note, however, that these act on |void*|. +// `d2i_SAMPLE` and `i2d_SAMPLE`. Note, however, that these act on `void*`. // Calling a function with a different pointer type is undefined in C, so this // is only valid with a wrapper. typedef void *d2i_of_void(void **, const unsigned char **, long); @@ -198,214 +199,209 @@ typedef int i2d_of_void(const void *, unsigned char **); // ASN.1 types. // -// An |ASN1_ITEM| represents an ASN.1 type and allows working with ASN.1 types +// An `ASN1_ITEM` represents an ASN.1 type and allows working with ASN.1 types // generically. // -// |ASN1_ITEM|s use a different namespace from C types and are accessed via -// |ASN1_ITEM_*| macros. So, for example, |ASN1_OCTET_STRING| is both a C type -// and the name of an |ASN1_ITEM|, referenced as -// |ASN1_ITEM_rptr(ASN1_OCTET_STRING)|. +// `ASN1_ITEM`s use a different namespace from C types and are accessed via +// `ASN1_ITEM_*` macros. So, for example, `ASN1_OCTET_STRING` is both a C type +// and the name of an `ASN1_ITEM`, referenced as +// `ASN1_ITEM_rptr(ASN1_OCTET_STRING)`. // -// Each |ASN1_ITEM| has a corresponding C type, typically with the same name, +// Each `ASN1_ITEM` has a corresponding C type, typically with the same name, // which represents values in the ASN.1 type. This type is either a pointer type -// or |ASN1_BOOLEAN|. When it is a pointer, NULL pointers represent omitted +// or `ASN1_BOOLEAN`. When it is a pointer, NULL pointers represent omitted // values. For example, an OCTET STRING value is declared with the C type -// |ASN1_OCTET_STRING*| and uses the |ASN1_ITEM| named |ASN1_OCTET_STRING|. An +// `ASN1_OCTET_STRING*` and uses the `ASN1_ITEM` named `ASN1_OCTET_STRING`. An // OPTIONAL OCTET STRING uses the same C type and represents an omitted value -// with a NULL pointer. |ASN1_BOOLEAN| is described in a later section. +// with a NULL pointer. `ASN1_BOOLEAN` is described in a later section. -// DECLARE_ASN1_ITEM declares an |ASN1_ITEM| with name |name|. The |ASN1_ITEM| -// may be referenced with |ASN1_ITEM_rptr|. Uses of this macro should document +// DECLARE_ASN1_ITEM declares an `ASN1_ITEM` with name `name`. The `ASN1_ITEM` +// may be referenced with `ASN1_ITEM_rptr`. Uses of this macro should document // the corresponding ASN.1 and C types. #define DECLARE_ASN1_ITEM(name) extern OPENSSL_EXPORT const ASN1_ITEM name##_it; -// ASN1_ITEM_rptr returns the |const ASN1_ITEM *| named |name|. +// ASN1_ITEM_rptr returns the `const ASN1_ITEM *` named `name`. #define ASN1_ITEM_rptr(name) (&(name##_it)) -// ASN1_ITEM_EXP is an abstraction for referencing an |ASN1_ITEM| in a +// ASN1_ITEM_EXP is an abstraction for referencing an `ASN1_ITEM` in a // constant-initialized structure, such as a method table. It exists because, on -// some OpenSSL platforms, |ASN1_ITEM| references are indirected through -// functions. Structures reference the |ASN1_ITEM| by declaring a field like -// |ASN1_ITEM_EXP *item| and initializing it with |ASN1_ITEM_ref|. +// some OpenSSL platforms, `ASN1_ITEM` references are indirected through +// functions. Structures reference the `ASN1_ITEM` by declaring a field like +// `ASN1_ITEM_EXP *item` and initializing it with `ASN1_ITEM_ref`. typedef const ASN1_ITEM ASN1_ITEM_EXP; -// ASN1_ITEM_ref returns an |ASN1_ITEM_EXP*| for the |ASN1_ITEM| named |name|. +// ASN1_ITEM_ref returns an `ASN1_ITEM_EXP*` for the `ASN1_ITEM` named `name`. #define ASN1_ITEM_ref(name) (&(name##_it)) -// ASN1_ITEM_ptr converts |iptr|, which must be an |ASN1_ITEM_EXP*| to a -// |const ASN1_ITEM*|. +// ASN1_ITEM_ptr converts `iptr`, which must be an `ASN1_ITEM_EXP*` to a +// `const ASN1_ITEM*`. #define ASN1_ITEM_ptr(iptr) (iptr) -// ASN1_VALUE_st (aka |ASN1_VALUE|) is an opaque type used as a placeholder for -// the C type corresponding to an |ASN1_ITEM|. +// ASN1_VALUE_st (aka `ASN1_VALUE`) is an opaque type used as a placeholder for +// the C type corresponding to an `ASN1_ITEM`. typedef struct ASN1_VALUE_st ASN1_VALUE; -// ASN1_item_new allocates a new value of the C type corresponding to |it|, or +// ASN1_item_new allocates a new value of the C type corresponding to `it`, or // NULL on error. On success, the caller must release the value with -// |ASN1_item_free|, or the corresponding C type's free function, when done. The +// `ASN1_item_free`, or the corresponding C type's free function, when done. The // new value will initialize fields of the value to some default state, such as // an empty string. Note, however, that this default state sometimes omits // required values, such as with CHOICE types. // -// This function may not be used with |ASN1_ITEM|s whose C type is -// |ASN1_BOOLEAN|. +// This function may not be used with `ASN1_ITEM`s whose C type is +// `ASN1_BOOLEAN`. // // WARNING: Casting the result of this function to the wrong type is a // potentially exploitable memory error. Callers must ensure the value is used -// consistently with |it|. Prefer using type-specific functions such as -// |ASN1_OCTET_STRING_new|. +// consistently with `it`. Prefer using type-specific functions such as +// `ASN1_OCTET_STRING_new`. OPENSSL_EXPORT ASN1_VALUE *ASN1_item_new(const ASN1_ITEM *it); -// ASN1_item_free releases memory associated with |val|, which must be an object -// of the C type corresponding to |it|. +// ASN1_item_free releases memory associated with `val`, which must be an object +// of the C type corresponding to `it`. // -// This function may not be used with |ASN1_ITEM|s whose C type is -// |ASN1_BOOLEAN|. +// This function may not be used with `ASN1_ITEM`s whose C type is +// `ASN1_BOOLEAN`. // // WARNING: Passing a pointer of the wrong type into this function is a -// potentially exploitable memory error. Callers must ensure |val| is consistent -// with |it|. Prefer using type-specific functions such as -// |ASN1_OCTET_STRING_free|. +// potentially exploitable memory error. Callers must ensure `val` is consistent +// with `it`. Prefer using type-specific functions such as +// `ASN1_OCTET_STRING_free`. OPENSSL_EXPORT void ASN1_item_free(ASN1_VALUE *val, const ASN1_ITEM *it); -// ASN1_item_d2i parses the ASN.1 type |it| from up to |len| bytes at |*inp|. -// It behaves like |d2i_SAMPLE|, except that |out| and the return value are cast -// to |ASN1_VALUE| pointers. +// ASN1_item_d2i parses the ASN.1 type `it` from up to `len` bytes at `*inp`. +// It behaves like `d2i_SAMPLE`, except that `out` and the return value are cast +// to `ASN1_VALUE` pointers. // -// TODO(https://crbug.com/boringssl/444): C strict aliasing forbids type-punning -// |T*| and |ASN1_VALUE*| the way this function signature does. When that bug is -// resolved, we will need to pick which type |*out| is (probably |T*|). Do not -// use a non-NULL |out| to avoid ending up on the wrong side of this question. -// -// This function may not be used with |ASN1_ITEM|s whose C type is -// |ASN1_BOOLEAN|. +// This function may not be used with `ASN1_ITEM`s whose C type is +// `ASN1_BOOLEAN`. // // WARNING: Casting the result of this function to the wrong type, or passing a // pointer of the wrong type into this function, are potentially exploitable -// memory errors. Callers must ensure |out| is consistent with |it|. Prefer -// using type-specific functions such as |d2i_ASN1_OCTET_STRING|. +// memory errors. Callers must ensure `out` is consistent with `it`. Prefer +// using type-specific functions such as `d2i_ASN1_OCTET_STRING`. OPENSSL_EXPORT ASN1_VALUE *ASN1_item_d2i(ASN1_VALUE **out, const unsigned char **inp, long len, const ASN1_ITEM *it); -// ASN1_item_i2d marshals |val| as the ASN.1 type associated with |it|, as -// described in |i2d_SAMPLE|. +// ASN1_item_i2d marshals `val` as the ASN.1 type associated with `it`, as +// described in `i2d_SAMPLE`. // -// This function may not be used with |ASN1_ITEM|s whose C type is -// |ASN1_BOOLEAN|. +// This function may not be used with `ASN1_ITEM`s whose C type is +// `ASN1_BOOLEAN`. // // WARNING: Passing a pointer of the wrong type into this function is a -// potentially exploitable memory error. Callers must ensure |val| is consistent -// with |it|. Prefer using type-specific functions such as -// |i2d_ASN1_OCTET_STRING|. +// potentially exploitable memory error. Callers must ensure `val` is consistent +// with `it`. Prefer using type-specific functions such as +// `i2d_ASN1_OCTET_STRING`. OPENSSL_EXPORT int ASN1_item_i2d(ASN1_VALUE *val, unsigned char **outp, const ASN1_ITEM *it); -// ASN1_item_dup returns a newly-allocated copy of |x|, or NULL on error. |x| -// must be an object of |it|'s C type. +// ASN1_item_dup returns a newly-allocated copy of `x`, or NULL on error. `x` +// must be an object of `it`'s C type. // -// This function may not be used with |ASN1_ITEM|s whose C type is -// |ASN1_BOOLEAN|. +// This function may not be used with `ASN1_ITEM`s whose C type is +// `ASN1_BOOLEAN`. // // WARNING: Casting the result of this function to the wrong type, or passing a // pointer of the wrong type into this function, are potentially exploitable // memory errors. Prefer using type-specific functions such as -// |ASN1_STRING_dup|. +// `ASN1_STRING_dup`. OPENSSL_EXPORT void *ASN1_item_dup(const ASN1_ITEM *it, void *x); -// The following functions behave like |ASN1_item_d2i| but read from |in| -// instead. |out| is the same parameter as in |ASN1_item_d2i|, but written with -// |void*| instead. The return values similarly match. +// The following functions behave like `ASN1_item_d2i` but read from `in` +// instead. `out` is the same parameter as in `ASN1_item_d2i`, but written with +// `void*` instead. The return values similarly match. // -// These functions may not be used with |ASN1_ITEM|s whose C type is -// |ASN1_BOOLEAN|. +// These functions may not be used with `ASN1_ITEM`s whose C type is +// `ASN1_BOOLEAN`. // -// WARNING: These functions do not bound how much data is read from |in|. +// WARNING: These functions do not bound how much data is read from `in`. // Parsing an untrusted input could consume unbounded memory. OPENSSL_EXPORT void *ASN1_item_d2i_fp(const ASN1_ITEM *it, FILE *in, void *out); OPENSSL_EXPORT void *ASN1_item_d2i_bio(const ASN1_ITEM *it, BIO *in, void *out); -// The following functions behave like |ASN1_item_i2d| but write to |out| -// instead. |in| is the same parameter as in |ASN1_item_i2d|, but written with -// |void*| instead. +// The following functions behave like `ASN1_item_i2d` but write to `out` +// instead. `in` is the same parameter as in `ASN1_item_i2d`, but written with +// `void*` instead. // -// These functions may not be used with |ASN1_ITEM|s whose C type is -// |ASN1_BOOLEAN|. +// These functions may not be used with `ASN1_ITEM`s whose C type is +// `ASN1_BOOLEAN`. OPENSSL_EXPORT int ASN1_item_i2d_fp(const ASN1_ITEM *it, FILE *out, const void *in); OPENSSL_EXPORT int ASN1_item_i2d_bio(const ASN1_ITEM *it, BIO *out, const void *in); -// ASN1_item_unpack parses |oct|'s contents as |it|'s ASN.1 type. It returns a -// newly-allocated instance of |it|'s C type on success, or NULL on error. +// ASN1_item_unpack parses `oct`'s contents as `it`'s ASN.1 type. It returns a +// newly-allocated instance of `it`'s C type on success, or NULL on error. // -// This function may not be used with |ASN1_ITEM|s whose C type is -// |ASN1_BOOLEAN|. +// This function may not be used with `ASN1_ITEM`s whose C type is +// `ASN1_BOOLEAN`. // // WARNING: Casting the result of this function to the wrong type is a // potentially exploitable memory error. Callers must ensure the value is used -// consistently with |it|. +// consistently with `it`. OPENSSL_EXPORT void *ASN1_item_unpack(const ASN1_STRING *oct, const ASN1_ITEM *it); -// ASN1_item_pack marshals |obj| as |it|'s ASN.1 type. If |out| is NULL, it -// returns a newly-allocated |ASN1_STRING| with the result, or NULL on error. -// If |out| is non-NULL, but |*out| is NULL, it does the same but additionally -// sets |*out| to the result. If both |out| and |*out| are non-NULL, it writes -// the result to |*out| and returns |*out| on success or NULL on error. +// ASN1_item_pack marshals `obj` as `it`'s ASN.1 type. If `out` is NULL, it +// returns a newly-allocated `ASN1_STRING` with the result, or NULL on error. +// If `out` is non-NULL, but `*out` is NULL, it does the same but additionally +// sets `*out` to the result. If both `out` and `*out` are non-NULL, it writes +// the result to `*out` and returns `*out` on success or NULL on error. // -// This function may not be used with |ASN1_ITEM|s whose C type is -// |ASN1_BOOLEAN|. +// This function may not be used with `ASN1_ITEM`s whose C type is +// `ASN1_BOOLEAN`. // // WARNING: Passing a pointer of the wrong type into this function is a -// potentially exploitable memory error. Callers must ensure |val| is consistent -// with |it|. +// potentially exploitable memory error. Callers must ensure `val` is consistent +// with `it`. OPENSSL_EXPORT ASN1_STRING *ASN1_item_pack(void *obj, const ASN1_ITEM *it, ASN1_STRING **out); // Booleans. // -// This library represents ASN.1 BOOLEAN values with |ASN1_BOOLEAN|, which is an +// This library represents ASN.1 BOOLEAN values with `ASN1_BOOLEAN`, which is an // integer type. FALSE is zero, TRUE is 0xff, and an omitted OPTIONAL BOOLEAN is // -1. -// ASN1_BOOLEAN_FALSE is FALSE as an |ASN1_BOOLEAN|. +// ASN1_BOOLEAN_FALSE is FALSE as an `ASN1_BOOLEAN`. #define ASN1_BOOLEAN_FALSE 0 -// ASN1_BOOLEAN_TRUE is TRUE as an |ASN1_BOOLEAN|. Some code incorrectly uses -// 1, so prefer |b != ASN1_BOOLEAN_FALSE| over |b == ASN1_BOOLEAN_TRUE|. +// ASN1_BOOLEAN_TRUE is TRUE as an `ASN1_BOOLEAN`. Some code incorrectly uses +// 1, so prefer `b != ASN1_BOOLEAN_FALSE` over `b == ASN1_BOOLEAN_TRUE`. #define ASN1_BOOLEAN_TRUE 0xff -// ASN1_BOOLEAN_NONE, in contexts where the |ASN1_BOOLEAN| represents an +// ASN1_BOOLEAN_NONE, in contexts where the `ASN1_BOOLEAN` represents an // OPTIONAL BOOLEAN, is an omitted value. Using this value in other contexts is // undefined and may be misinterpreted as TRUE. #define ASN1_BOOLEAN_NONE (-1) -// d2i_ASN1_BOOLEAN parses a DER-encoded ASN.1 BOOLEAN from up to |len| bytes at -// |*inp|. On success, it advances |*inp| by the number of bytes read and -// returns the result. If |out| is non-NULL, it additionally writes the result -// to |*out|. On error, it returns |ASN1_BOOLEAN_NONE|. +// d2i_ASN1_BOOLEAN parses a DER-encoded ASN.1 BOOLEAN from up to `len` bytes at +// `*inp`. On success, it advances `*inp` by the number of bytes read and +// returns the result. If `out` is non-NULL, it additionally writes the result +// to `*out`. On error, it returns `ASN1_BOOLEAN_NONE`. // // This function does not reject trailing data in the input. This allows the // caller to parse a sequence of concatenated structures. Callers parsing only -// one structure should check for trailing data by comparing the updated |*inp| +// one structure should check for trailing data by comparing the updated `*inp` // with the end of the input. // -// WARNING: This function's is slightly different from other |d2i_*| functions -// because |ASN1_BOOLEAN| is not a pointer type. +// WARNING: This function's is slightly different from other `d2i_*` functions +// because `ASN1_BOOLEAN` is not a pointer type. OPENSSL_EXPORT ASN1_BOOLEAN d2i_ASN1_BOOLEAN(ASN1_BOOLEAN *out, const unsigned char **inp, long len); -// i2d_ASN1_BOOLEAN marshals |a| as a DER-encoded ASN.1 BOOLEAN, as described in -// |i2d_SAMPLE|. +// i2d_ASN1_BOOLEAN marshals `a` as a DER-encoded ASN.1 BOOLEAN, as described in +// `i2d_SAMPLE`. OPENSSL_EXPORT int i2d_ASN1_BOOLEAN(ASN1_BOOLEAN a, unsigned char **outp); -// The following |ASN1_ITEM|s have ASN.1 type BOOLEAN and C type |ASN1_BOOLEAN|. -// |ASN1_TBOOLEAN| and |ASN1_FBOOLEAN| must be marked OPTIONAL. When omitted, +// The following `ASN1_ITEM`s have ASN.1 type BOOLEAN and C type `ASN1_BOOLEAN`. +// `ASN1_TBOOLEAN` and `ASN1_FBOOLEAN` must be marked OPTIONAL. When omitted, // they are parsed as TRUE and FALSE, respectively, rather than -// |ASN1_BOOLEAN_NONE|. +// `ASN1_BOOLEAN_NONE`. DECLARE_ASN1_ITEM(ASN1_BOOLEAN) DECLARE_ASN1_ITEM(ASN1_TBOOLEAN) DECLARE_ASN1_ITEM(ASN1_FBOOLEAN) @@ -415,71 +411,71 @@ DECLARE_ASN1_ITEM(ASN1_FBOOLEAN) // // ASN.1 contains a myriad of string types, as well as types that contain data // that may be encoded into a string. This library uses a single type, -// |ASN1_STRING|, to represent most values. +// `ASN1_STRING`, to represent most values. -// An asn1_string_st (aka |ASN1_STRING|) represents a value of a string-like -// ASN.1 type. It contains a |type| field, and a byte string |data| field with a +// An asn1_string_st (aka `ASN1_STRING`) represents a value of a string-like +// ASN.1 type. It contains a `type` field, and a byte string `data` field with a // type-specific representation. This type-specific representation does not // always correspond to the DER encoding of the type. // -// If |type| is one of |V_ASN1_OCTET_STRING|, |V_ASN1_UTF8STRING|, -// |V_ASN1_NUMERICSTRING|, |V_ASN1_PRINTABLESTRING|, |V_ASN1_T61STRING|, -// |V_ASN1_VIDEOTEXSTRING|, |V_ASN1_IA5STRING|, |V_ASN1_GRAPHICSTRING|, -// |V_ASN1_ISO64STRING|, |V_ASN1_VISIBLESTRING|, |V_ASN1_GENERALSTRING|, -// |V_ASN1_UNIVERSALSTRING|, or |V_ASN1_BMPSTRING|, the object represents an +// If `type` is one of `V_ASN1_OCTET_STRING`, `V_ASN1_UTF8STRING`, +// `V_ASN1_NUMERICSTRING`, `V_ASN1_PRINTABLESTRING`, `V_ASN1_T61STRING`, +// `V_ASN1_VIDEOTEXSTRING`, `V_ASN1_IA5STRING`, `V_ASN1_GRAPHICSTRING`, +// `V_ASN1_ISO64STRING`, `V_ASN1_VISIBLESTRING`, `V_ASN1_GENERALSTRING`, +// `V_ASN1_UNIVERSALSTRING`, or `V_ASN1_BMPSTRING`, the object represents an // ASN.1 string type. The data contains the byte representation of the // string. // -// If |type| is |V_ASN1_BIT_STRING|, the object represents a BIT STRING value. +// If `type` is `V_ASN1_BIT_STRING`, the object represents a BIT STRING value. // See bit string documentation below for the data and flags. // -// If |type| is one of |V_ASN1_INTEGER|, |V_ASN1_NEG_INTEGER|, -// |V_ASN1_ENUMERATED|, or |V_ASN1_NEG_ENUMERATED|, the object represents an +// If `type` is one of `V_ASN1_INTEGER`, `V_ASN1_NEG_INTEGER`, +// `V_ASN1_ENUMERATED`, or `V_ASN1_NEG_ENUMERATED`, the object represents an // INTEGER or ENUMERATED value. See integer documentation below for details. // -// If |type| is |V_ASN1_GENERALIZEDTIME| or |V_ASN1_UTCTIME|, the object +// If `type` is `V_ASN1_GENERALIZEDTIME` or `V_ASN1_UTCTIME`, the object // represents a GeneralizedTime or UTCTime value, respectively. The data // contains the DER encoding of the value. For example, the UNIX epoch would be // "19700101000000Z" for a GeneralizedTime and "700101000000Z" for a UTCTime. // -// If |type| is |V_ASN1_SEQUENCE|, |V_ASN1_SET|, or |V_ASN1_OTHER|, the object +// If `type` is `V_ASN1_SEQUENCE`, `V_ASN1_SET`, or `V_ASN1_OTHER`, the object // represents a SEQUENCE, SET, or arbitrary ASN.1 value, respectively. Unlike // the above cases, the data contains the DER encoding of the entire structure, // including the header. If the value is explicitly or implicitly tagged, this // too will be reflected in the data field. As this case handles unknown types, // the contents are not checked when parsing or serializing. // -// Other values of |type| do not represent a valid ASN.1 value, though -// default-constructed objects may set |type| to -1. Such objects cannot be +// Other values of `type` do not represent a valid ASN.1 value, though +// default-constructed objects may set `type` to -1. Such objects cannot be // serialized. // -// |ASN1_STRING| additionally has the following typedefs: |ASN1_BIT_STRING|, -// |ASN1_BMPSTRING|, |ASN1_ENUMERATED|, |ASN1_GENERALIZEDTIME|, -// |ASN1_GENERALSTRING|, |ASN1_IA5STRING|, |ASN1_INTEGER|, |ASN1_OCTET_STRING|, -// |ASN1_PRINTABLESTRING|, |ASN1_T61STRING|, |ASN1_TIME|, -// |ASN1_UNIVERSALSTRING|, |ASN1_UTCTIME|, |ASN1_UTF8STRING|, and -// |ASN1_VISIBLESTRING|. Other than |ASN1_TIME|, these correspond to universal -// ASN.1 types. |ASN1_TIME| represents a CHOICE of UTCTime and GeneralizedTime, +// `ASN1_STRING` additionally has the following typedefs: `ASN1_BIT_STRING`, +// `ASN1_BMPSTRING`, `ASN1_ENUMERATED`, `ASN1_GENERALIZEDTIME`, +// `ASN1_GENERALSTRING`, `ASN1_IA5STRING`, `ASN1_INTEGER`, `ASN1_OCTET_STRING`, +// `ASN1_PRINTABLESTRING`, `ASN1_T61STRING`, `ASN1_TIME`, +// `ASN1_UNIVERSALSTRING`, `ASN1_UTCTIME`, `ASN1_UTF8STRING`, and +// `ASN1_VISIBLESTRING`. Other than `ASN1_TIME`, these correspond to universal +// ASN.1 types. `ASN1_TIME` represents a CHOICE of UTCTime and GeneralizedTime, // with a cutoff of 2049, as used in Section 4.1.2.5 of RFC 5280. // // For clarity, callers are encouraged to use the appropriate typedef when -// available. They are the same type as |ASN1_STRING|, so a caller may freely -// pass them into functions expecting |ASN1_STRING|, such as -// |ASN1_STRING_length|. -// -// If a function returns an |ASN1_STRING| where the typedef or ASN.1 structure -// implies constraints on |type|, callers may assume that |type| is correct. -// However, if a function takes an |ASN1_STRING| as input, callers must ensure -// |type| matches. These invariants are not captured by the C type system and +// available. They are the same type as `ASN1_STRING`, so a caller may freely +// pass them into functions expecting `ASN1_STRING`, such as +// `ASN1_STRING_length`. +// +// If a function returns an `ASN1_STRING` where the typedef or ASN.1 structure +// implies constraints on `type`, callers may assume that `type` is correct. +// However, if a function takes an `ASN1_STRING` as input, callers must ensure +// `type` matches. These invariants are not captured by the C type system and // may not be checked at runtime. For example, callers may assume the output of -// |X509_get0_serialNumber| has type |V_ASN1_INTEGER| or |V_ASN1_NEG_INTEGER|. -// Callers must not pass a string of type |V_ASN1_OCTET_STRING| to -// |X509_set_serialNumber|. Doing so may break invariants on the |X509| object -// and break the |X509_get0_serialNumber| invariant. +// `X509_get0_serialNumber` has type `V_ASN1_INTEGER` or `V_ASN1_NEG_INTEGER`. +// Callers must not pass a string of type `V_ASN1_OCTET_STRING` to +// `X509_set_serialNumber`. Doing so may break invariants on the `X509` object +// and break the `X509_get0_serialNumber` invariant. // // TODO(https://crbug.com/boringssl/445): This is very unfriendly. Getting the // type field wrong should not cause memory errors, but it may do strange -// things. We should add runtime checks to anything that consumes |ASN1_STRING|s +// things. We should add runtime checks to anything that consumes `ASN1_STRING`s // from the caller. struct asn1_string_st { int length; @@ -488,91 +484,80 @@ struct asn1_string_st { long flags; }; -// ASN1_STRING_type_new returns a newly-allocated empty |ASN1_STRING| object of -// type |type|, or NULL on error. +// ASN1_STRING_type_new returns a newly-allocated empty `ASN1_STRING` object of +// type `type`, or NULL on error. OPENSSL_EXPORT ASN1_STRING *ASN1_STRING_type_new(int type); -// ASN1_STRING_new returns a newly-allocated empty |ASN1_STRING| object with an +// ASN1_STRING_new returns a newly-allocated empty `ASN1_STRING` object with an // arbitrary type. Prefer one of the type-specific constructors, such as -// |ASN1_OCTET_STRING_new|, or |ASN1_STRING_type_new|. +// `ASN1_OCTET_STRING_new`, or `ASN1_STRING_type_new`. OPENSSL_EXPORT ASN1_STRING *ASN1_STRING_new(void); -// ASN1_STRING_free releases memory associated with |str|. +// ASN1_STRING_free releases memory associated with `str`. OPENSSL_EXPORT void ASN1_STRING_free(ASN1_STRING *str); -// ASN1_STRING_copy sets |dst| to a copy of |str|. It returns one on success and +// ASN1_STRING_copy sets `dst` to a copy of `str`. It returns one on success and // zero on error. OPENSSL_EXPORT int ASN1_STRING_copy(ASN1_STRING *dst, const ASN1_STRING *str); -// ASN1_STRING_dup returns a newly-allocated copy of |str|, or NULL on error. +// ASN1_STRING_dup returns a newly-allocated copy of `str`, or NULL on error. OPENSSL_EXPORT ASN1_STRING *ASN1_STRING_dup(const ASN1_STRING *str); -// ASN1_STRING_type returns the type of |str|. This value will be one of the -// |V_ASN1_*| constants. +// ASN1_STRING_type returns the type of `str`. This value will be one of the +// `V_ASN1_*` constants. OPENSSL_EXPORT int ASN1_STRING_type(const ASN1_STRING *str); -// ASN1_STRING_get0_data returns a pointer to |str|'s contents. Callers should -// use |ASN1_STRING_length| to determine the length of the string. The string +// ASN1_STRING_get0_data returns a pointer to `str`'s contents. Callers should +// use `ASN1_STRING_length` to determine the length of the string. The string // may have embedded NUL bytes and may not be NUL-terminated. // -// The contents of an |ASN1_STRING| encode the value in some type-specific +// The contents of an `ASN1_STRING` encode the value in some type-specific // representation that does not always correspond to the DER encoding of the -// type. See the documentation for |ASN1_STRING| for details. +// type. See the documentation for `ASN1_STRING` for details. OPENSSL_EXPORT const unsigned char *ASN1_STRING_get0_data( const ASN1_STRING *str); -// ASN1_STRING_data returns a mutable pointer to |str|'s contents. Callers -// should use |ASN1_STRING_length| to determine the length of the string. The -// string may have embedded NUL bytes and may not be NUL-terminated. -// -// The contents of an |ASN1_STRING| encode the value in some type-specific -// representation that does not always correspond to the DER encoding of the -// type. See the documentation for |ASN1_STRING| for details. -// -// Prefer |ASN1_STRING_get0_data|. -OPENSSL_EXPORT unsigned char *ASN1_STRING_data(ASN1_STRING *str); - -// ASN1_STRING_length returns the length of |str|, in bytes. +// ASN1_STRING_get_length returns the length of `str`, in bytes. // -// The contents of an |ASN1_STRING| encode the value in some type-specific +// The contents of an `ASN1_STRING` encode the value in some type-specific // representation that does not always correspond to the DER encoding of the -// type. See the documentation for |ASN1_STRING| for details. -OPENSSL_EXPORT int ASN1_STRING_length(const ASN1_STRING *str); +// type. See the documentation for `ASN1_STRING` for details. +OPENSSL_EXPORT size_t ASN1_STRING_get_length(const ASN1_STRING *str); -// ASN1_STRING_cmp compares |a| and |b|'s type and contents. It returns an -// integer equal to, less than, or greater than zero if |a| is equal to, less -// than, or greater than |b|, respectively. This function compares by length, -// then data, then type. Note the data compared is the |ASN1_STRING| internal +// ASN1_STRING_cmp compares `a` and `b`'s type and contents. It returns an +// integer equal to, less than, or greater than zero if `a` is equal to, less +// than, or greater than `b`, respectively. This function compares by length, +// then data, then type. Note the data compared is the `ASN1_STRING` internal // representation and the type order is arbitrary. While this comparison is -// suitable for sorting, callers should not rely on the exact order when |a| -// and |b| are different types. +// suitable for sorting, callers should not rely on the exact order when `a` +// and `b` are different types. // -// Note that, if |a| and |b| are INTEGERs, this comparison does not order the -// values numerically. For a numerical comparison, use |ASN1_INTEGER_cmp|. +// Note that, if `a` and `b` are INTEGERs, this comparison does not order the +// values numerically. For a numerical comparison, use `ASN1_INTEGER_cmp`. OPENSSL_EXPORT int ASN1_STRING_cmp(const ASN1_STRING *a, const ASN1_STRING *b); -// ASN1_STRING_set sets the contents of |str| to a copy of |len| bytes from -// |data|. It returns one on success and zero on error. If |data| is NULL, it +// ASN1_STRING_set sets the contents of `str` to a copy of `len` bytes from +// `data`. It returns one on success and zero on error. If `data` is NULL, it // updates the length and allocates the buffer as needed, but does not // initialize the contents. // -// If |str| is a BIT STRING, this function sets the number of unused bits to -// zero. |ASN1_BIT_STRING_set1| may be used to set a BIT STRING that is not a +// If `str` is a BIT STRING, this function sets the number of unused bits to +// zero. `ASN1_BIT_STRING_set1` may be used to set a BIT STRING that is not a // whole number of bytes. OPENSSL_EXPORT int ASN1_STRING_set(ASN1_STRING *str, const void *data, ossl_ssize_t len); -// ASN1_STRING_set0 sets the contents of |str| to |len| bytes from |data|. It -// takes ownership of |data|, which must have been allocated with -// |OPENSSL_malloc|. +// ASN1_STRING_set0 sets the contents of `str` to `len` bytes from `data`. It +// takes ownership of `data`, which must have been allocated with +// `OPENSSL_malloc`. // -// If |str| is a BIT STRING, this function sets the number of unused bits to -// zero. |ASN1_BIT_STRING_set1| may be used to set a BIT STRING that is not a +// If `str` is a BIT STRING, this function sets the number of unused bits to +// zero. `ASN1_BIT_STRING_set1` may be used to set a BIT STRING that is not a // whole number of bytes. OPENSSL_EXPORT void ASN1_STRING_set0(ASN1_STRING *str, void *data, int len); -// The following functions call |ASN1_STRING_type_new| with the corresponding -// |V_ASN1_*| constant. +// The following functions call `ASN1_STRING_type_new` with the corresponding +// `V_ASN1_*` constant. OPENSSL_EXPORT ASN1_BMPSTRING *ASN1_BMPSTRING_new(void); OPENSSL_EXPORT ASN1_GENERALSTRING *ASN1_GENERALSTRING_new(void); OPENSSL_EXPORT ASN1_IA5STRING *ASN1_IA5STRING_new(void); @@ -583,7 +568,7 @@ OPENSSL_EXPORT ASN1_UNIVERSALSTRING *ASN1_UNIVERSALSTRING_new(void); OPENSSL_EXPORT ASN1_UTF8STRING *ASN1_UTF8STRING_new(void); OPENSSL_EXPORT ASN1_VISIBLESTRING *ASN1_VISIBLESTRING_new(void); -// The following functions call |ASN1_STRING_free|. +// The following functions call `ASN1_STRING_free`. OPENSSL_EXPORT void ASN1_BMPSTRING_free(ASN1_BMPSTRING *str); OPENSSL_EXPORT void ASN1_GENERALSTRING_free(ASN1_GENERALSTRING *str); OPENSSL_EXPORT void ASN1_IA5STRING_free(ASN1_IA5STRING *str); @@ -594,9 +579,9 @@ OPENSSL_EXPORT void ASN1_UNIVERSALSTRING_free(ASN1_UNIVERSALSTRING *str); OPENSSL_EXPORT void ASN1_UTF8STRING_free(ASN1_UTF8STRING *str); OPENSSL_EXPORT void ASN1_VISIBLESTRING_free(ASN1_VISIBLESTRING *str); -// The following functions parse up to |len| bytes from |*inp| as a +// The following functions parse up to `len` bytes from `*inp` as a // DER-encoded ASN.1 value of the corresponding type, as described in -// |d2i_SAMPLE|. +// `d2i_SAMPLE`. OPENSSL_EXPORT ASN1_BMPSTRING *d2i_ASN1_BMPSTRING(ASN1_BMPSTRING **out, const uint8_t **inp, long len); @@ -621,8 +606,8 @@ OPENSSL_EXPORT ASN1_UTF8STRING *d2i_ASN1_UTF8STRING(ASN1_UTF8STRING **out, OPENSSL_EXPORT ASN1_VISIBLESTRING *d2i_ASN1_VISIBLESTRING( ASN1_VISIBLESTRING **out, const uint8_t **inp, long len); -// The following functions marshal |in| as a DER-encoded ASN.1 value of the -// corresponding type, as described in |i2d_SAMPLE|. +// The following functions marshal `in` as a DER-encoded ASN.1 value of the +// corresponding type, as described in `i2d_SAMPLE`. OPENSSL_EXPORT int i2d_ASN1_BMPSTRING(const ASN1_BMPSTRING *in, uint8_t **outp); OPENSSL_EXPORT int i2d_ASN1_GENERALSTRING(const ASN1_GENERALSTRING *in, uint8_t **outp); @@ -639,8 +624,8 @@ OPENSSL_EXPORT int i2d_ASN1_UTF8STRING(const ASN1_UTF8STRING *in, OPENSSL_EXPORT int i2d_ASN1_VISIBLESTRING(const ASN1_VISIBLESTRING *in, uint8_t **outp); -// The following |ASN1_ITEM|s have the ASN.1 type referred to in their name and -// C type |ASN1_STRING*|. The C type may also be written as the corresponding +// The following `ASN1_ITEM`s have the ASN.1 type referred to in their name and +// C type `ASN1_STRING*`. The C type may also be written as the corresponding // typedef. DECLARE_ASN1_ITEM(ASN1_BMPSTRING) DECLARE_ASN1_ITEM(ASN1_GENERALSTRING) @@ -652,27 +637,27 @@ DECLARE_ASN1_ITEM(ASN1_UNIVERSALSTRING) DECLARE_ASN1_ITEM(ASN1_UTF8STRING) DECLARE_ASN1_ITEM(ASN1_VISIBLESTRING) -// ASN1_OCTET_STRING_dup calls |ASN1_STRING_dup|. +// ASN1_OCTET_STRING_dup calls `ASN1_STRING_dup`. OPENSSL_EXPORT ASN1_OCTET_STRING *ASN1_OCTET_STRING_dup( const ASN1_OCTET_STRING *a); -// ASN1_OCTET_STRING_cmp calls |ASN1_STRING_cmp|. +// ASN1_OCTET_STRING_cmp calls `ASN1_STRING_cmp`. OPENSSL_EXPORT int ASN1_OCTET_STRING_cmp(const ASN1_OCTET_STRING *a, const ASN1_OCTET_STRING *b); -// ASN1_OCTET_STRING_set calls |ASN1_STRING_set|. +// ASN1_OCTET_STRING_set calls `ASN1_STRING_set`. OPENSSL_EXPORT int ASN1_OCTET_STRING_set(ASN1_OCTET_STRING *str, const unsigned char *data, int len); -// ASN1_STRING_to_UTF8 converts |in| to UTF-8. On success, sets |*out| to a +// ASN1_STRING_to_UTF8 converts `in` to UTF-8. On success, sets `*out` to a // newly-allocated buffer containing the resulting string and returns the length -// of the string. The caller must call |OPENSSL_free| to release |*out| when +// of the string. The caller must call `OPENSSL_free` to release `*out` when // done. On error, it returns a negative number. OPENSSL_EXPORT int ASN1_STRING_to_UTF8(unsigned char **out, const ASN1_STRING *in); // The following formats define encodings for use with functions like -// |ASN1_mbstring_copy|. Note |MBSTRING_ASC| refers to Latin-1, not ASCII. +// `ASN1_mbstring_copy`. Note `MBSTRING_ASC` refers to Latin-1, not ASCII. #define MBSTRING_FLAG 0x1000 #define MBSTRING_UTF8 (MBSTRING_FLAG) #define MBSTRING_ASC (MBSTRING_FLAG | 1) @@ -687,80 +672,80 @@ OPENSSL_EXPORT int ASN1_STRING_to_UTF8(unsigned char **out, // PKCS9STRING_TYPE contains the valid string types in a PKCS9String. #define PKCS9STRING_TYPE (DIRSTRING_TYPE | B_ASN1_IA5STRING) -// ASN1_mbstring_copy converts |len| bytes from |in| to an ASN.1 string. If -// |len| is -1, |in| must be NUL-terminated and the length is determined by -// |strlen|. |in| is decoded according to |inform|, which must be one of -// |MBSTRING_*|. |mask| determines the set of valid output types and is a -// bitmask containing a subset of |B_ASN1_PRINTABLESTRING|, |B_ASN1_IA5STRING|, -// |B_ASN1_T61STRING|, |B_ASN1_BMPSTRING|, |B_ASN1_UNIVERSALSTRING|, and -// |B_ASN1_UTF8STRING|, in that preference order. This function chooses the -// first output type in |mask| which can represent |in|. It interprets T61String +// ASN1_mbstring_copy converts `len` bytes from `in` to an ASN.1 string. If +// `len` is -1, `in` must be NUL-terminated and the length is determined by +// `strlen`. `in` is decoded according to `inform`, which must be one of +// `MBSTRING_*`. `mask` determines the set of valid output types and is a +// bitmask containing a subset of `B_ASN1_PRINTABLESTRING`, `B_ASN1_IA5STRING`, +// `B_ASN1_T61STRING`, `B_ASN1_BMPSTRING`, `B_ASN1_UNIVERSALSTRING`, and +// `B_ASN1_UTF8STRING`, in that preference order. This function chooses the +// first output type in `mask` which can represent `in`. It interprets T61String // as Latin-1, rather than T.61. // -// If |mask| is zero, |DIRSTRING_TYPE| is used by default. +// If `mask` is zero, `DIRSTRING_TYPE` is used by default. // -// On success, this function returns the |V_ASN1_*| constant corresponding to -// the selected output type and, if |out| and |*out| are both non-NULL, updates -// the object at |*out| with the result. If |out| is non-NULL and |*out| is -// NULL, it instead sets |*out| to a newly-allocated |ASN1_STRING| containing -// the result. If |out| is NULL, it returns the selected output type without -// constructing an |ASN1_STRING|. On error, this function returns -1. +// On success, this function returns the `V_ASN1_*` constant corresponding to +// the selected output type and, if `out` and `*out` are both non-NULL, updates +// the object at `*out` with the result. If `out` is non-NULL and `*out` is +// NULL, it instead sets `*out` to a newly-allocated `ASN1_STRING` containing +// the result. If `out` is NULL, it returns the selected output type without +// constructing an `ASN1_STRING`. On error, this function returns -1. OPENSSL_EXPORT int ASN1_mbstring_copy(ASN1_STRING **out, const uint8_t *in, ossl_ssize_t len, int inform, unsigned long mask); -// ASN1_mbstring_ncopy behaves like |ASN1_mbstring_copy| but returns an error if -// the input is less than |minsize| or greater than |maxsize| codepoints long. A -// |maxsize| value of zero is ignored. Note the sizes are measured in +// ASN1_mbstring_ncopy behaves like `ASN1_mbstring_copy` but returns an error if +// the input is less than `minsize` or greater than `maxsize` codepoints long. A +// `maxsize` value of zero is ignored. Note the sizes are measured in // codepoints, not output bytes. OPENSSL_EXPORT int ASN1_mbstring_ncopy(ASN1_STRING **out, const uint8_t *in, ossl_ssize_t len, int inform, unsigned long mask, ossl_ssize_t minsize, ossl_ssize_t maxsize); -// ASN1_STRING_set_by_NID behaves like |ASN1_mbstring_ncopy|, but determines -// |mask|, |minsize|, and |maxsize| based on |nid|. When |nid| is a recognized +// ASN1_STRING_set_by_NID behaves like `ASN1_mbstring_ncopy`, but determines +// `mask`, `minsize`, and `maxsize` based on `nid`. When `nid` is a recognized // X.509 attribute type, it will pick a suitable ASN.1 string type and bounds. -// For most attribute types, it preferentially chooses UTF8String. If |nid| is +// For most attribute types, it preferentially chooses UTF8String. If `nid` is // unrecognized, it uses UTF8String by default. This function will also enforce // any known attribute-specific constraints on the sizes of the string and fail // if the size is invalid. In RFC 5280, these bounds are specified by // constraints like "SIZE (1..ub-common-name)" in ASN.1. // -// Slightly unlike |ASN1_mbstring_ncopy|, this function interprets |out| and -// returns its result as follows: If |out| is NULL, it returns a newly-allocated -// |ASN1_STRING| containing the result. If |out| is non-NULL and -// |*out| is NULL, it additionally sets |*out| to the result. If both |out| and -// |*out| are non-NULL, it instead updates the object at |*out| and returns -// |*out|. In all cases, it returns NULL on error. -// -// This function supports the following NIDs: |NID_countryName|, -// |NID_dnQualifier|, |NID_domainComponent|, |NID_friendlyName|, -// |NID_givenName|, |NID_initials|, |NID_localityName|, |NID_ms_csp_name|, -// |NID_name|, |NID_organizationalUnitName|, |NID_organizationName|, -// |NID_pkcs9_challengePassword|, |NID_pkcs9_emailAddress|, -// |NID_pkcs9_unstructuredAddress|, |NID_pkcs9_unstructuredName|, -// |NID_serialNumber|, |NID_stateOrProvinceName|, and |NID_surname|. Additional -// NIDs may be registered with |ASN1_STRING_set_by_NID|, but it is recommended -// to call |ASN1_mbstring_ncopy| directly instead. +// Slightly unlike `ASN1_mbstring_ncopy`, this function interprets `out` and +// returns its result as follows: If `out` is NULL, it returns a newly-allocated +// `ASN1_STRING` containing the result. If `out` is non-NULL and +// `*out` is NULL, it additionally sets `*out` to the result. If both `out` and +// `*out` are non-NULL, it instead updates the object at `*out` and returns +// `*out`. In all cases, it returns NULL on error. +// +// This function supports the following NIDs: `NID_countryName`, +// `NID_dnQualifier`, `NID_domainComponent`, `NID_friendlyName`, +// `NID_givenName`, `NID_initials`, `NID_localityName`, `NID_ms_csp_name`, +// `NID_name`, `NID_organizationalUnitName`, `NID_organizationName`, +// `NID_pkcs9_challengePassword`, `NID_pkcs9_emailAddress`, +// `NID_pkcs9_unstructuredAddress`, `NID_pkcs9_unstructuredName`, +// `NID_serialNumber`, `NID_stateOrProvinceName`, and `NID_surname`. Additional +// NIDs may be registered with `ASN1_STRING_set_by_NID`, but it is recommended +// to call `ASN1_mbstring_ncopy` directly instead. OPENSSL_EXPORT ASN1_STRING *ASN1_STRING_set_by_NID(ASN1_STRING **out, const unsigned char *in, ossl_ssize_t len, int inform, int nid); -// STABLE_NO_MASK causes |ASN1_STRING_TABLE_add| to allow types other than +// STABLE_NO_MASK causes `ASN1_STRING_TABLE_add` to allow types other than // UTF8String. #define STABLE_NO_MASK 0x02 -// ASN1_STRING_TABLE_add registers the corresponding parameters with |nid|, for -// use with |ASN1_STRING_set_by_NID|. It returns one on success and zero on -// error. It is an error to call this function if |nid| is a built-in NID, or +// ASN1_STRING_TABLE_add registers the corresponding parameters with `nid`, for +// use with `ASN1_STRING_set_by_NID`. It returns one on success and zero on +// error. It is an error to call this function if `nid` is a built-in NID, or // was already registered by a previous call. // // WARNING: This function affects global state in the library. If two libraries // in the same address space register information for the same OID, one call // will fail. Prefer directly passing the desired parameters to -// |ASN1_mbstring_copy| or |ASN1_mbstring_ncopy| instead. +// `ASN1_mbstring_copy` or `ASN1_mbstring_ncopy` instead. OPENSSL_EXPORT int ASN1_STRING_TABLE_add(int nid, long minsize, long maxsize, unsigned long mask, unsigned long flags); @@ -768,11 +753,11 @@ OPENSSL_EXPORT int ASN1_STRING_TABLE_add(int nid, long minsize, long maxsize, // Multi-strings. // -// A multi-string, or "MSTRING", is an |ASN1_STRING| that represents a CHOICE of +// A multi-string, or "MSTRING", is an `ASN1_STRING` that represents a CHOICE of // several string or string-like types, such as X.509's DirectoryString. The -// |ASN1_STRING|'s type field determines which type is used. +// `ASN1_STRING`'s type field determines which type is used. // -// Multi-string types are associated with a bitmask, using the |B_ASN1_*| +// Multi-string types are associated with a bitmask, using the `B_ASN1_*` // constants, which defines which types are valid. // B_ASN1_DIRECTORYSTRING is a bitmask of types allowed in an X.509 @@ -781,16 +766,16 @@ OPENSSL_EXPORT int ASN1_STRING_TABLE_add(int nid, long minsize, long maxsize, (B_ASN1_PRINTABLESTRING | B_ASN1_TELETEXSTRING | B_ASN1_BMPSTRING | \ B_ASN1_UNIVERSALSTRING | B_ASN1_UTF8STRING) -// DIRECTORYSTRING_new returns a newly-allocated |ASN1_STRING| with type -1, or -// NULL on error. The resulting |ASN1_STRING| is not a valid X.509 +// DIRECTORYSTRING_new returns a newly-allocated `ASN1_STRING` with type -1, or +// NULL on error. The resulting `ASN1_STRING` is not a valid X.509 // DirectoryString until initialized with a value. OPENSSL_EXPORT ASN1_STRING *DIRECTORYSTRING_new(void); -// DIRECTORYSTRING_free calls |ASN1_STRING_free|. +// DIRECTORYSTRING_free calls `ASN1_STRING_free`. OPENSSL_EXPORT void DIRECTORYSTRING_free(ASN1_STRING *str); -// d2i_DIRECTORYSTRING parses up to |len| bytes from |*inp| as a DER-encoded -// X.509 DirectoryString (RFC 5280), as described in |d2i_SAMPLE|. +// d2i_DIRECTORYSTRING parses up to `len` bytes from `*inp` as a DER-encoded +// X.509 DirectoryString (RFC 5280), as described in `d2i_SAMPLE`. // // TODO(https://crbug.com/boringssl/354): This function currently also accepts // BER, but this will be removed in the future. @@ -800,8 +785,8 @@ OPENSSL_EXPORT void DIRECTORYSTRING_free(ASN1_STRING *str); OPENSSL_EXPORT ASN1_STRING *d2i_DIRECTORYSTRING(ASN1_STRING **out, const uint8_t **inp, long len); -// i2d_DIRECTORYSTRING marshals |in| as a DER-encoded X.509 DirectoryString (RFC -// 5280), as described in |i2d_SAMPLE|. +// i2d_DIRECTORYSTRING marshals `in` as a DER-encoded X.509 DirectoryString (RFC +// 5280), as described in `i2d_SAMPLE`. OPENSSL_EXPORT int i2d_DIRECTORYSTRING(const ASN1_STRING *in, uint8_t **outp); // B_ASN1_DISPLAYTEXT is a bitmask of types allowed in an X.509 DisplayText (RFC @@ -810,16 +795,16 @@ OPENSSL_EXPORT int i2d_DIRECTORYSTRING(const ASN1_STRING *in, uint8_t **outp); (B_ASN1_IA5STRING | B_ASN1_VISIBLESTRING | B_ASN1_BMPSTRING | \ B_ASN1_UTF8STRING) -// DISPLAYTEXT_new returns a newly-allocated |ASN1_STRING| with type -1, or NULL -// on error. The resulting |ASN1_STRING| is not a valid X.509 DisplayText until +// DISPLAYTEXT_new returns a newly-allocated `ASN1_STRING` with type -1, or NULL +// on error. The resulting `ASN1_STRING` is not a valid X.509 DisplayText until // initialized with a value. OPENSSL_EXPORT ASN1_STRING *DISPLAYTEXT_new(void); -// DISPLAYTEXT_free calls |ASN1_STRING_free|. +// DISPLAYTEXT_free calls `ASN1_STRING_free`. OPENSSL_EXPORT void DISPLAYTEXT_free(ASN1_STRING *str); -// d2i_DISPLAYTEXT parses up to |len| bytes from |*inp| as a DER-encoded X.509 -// DisplayText (RFC 5280), as described in |d2i_SAMPLE|. +// d2i_DISPLAYTEXT parses up to `len` bytes from `*inp` as a DER-encoded X.509 +// DisplayText (RFC 5280), as described in `d2i_SAMPLE`. // // TODO(https://crbug.com/boringssl/354): This function currently also accepts // BER, but this will be removed in the future. @@ -829,8 +814,8 @@ OPENSSL_EXPORT void DISPLAYTEXT_free(ASN1_STRING *str); OPENSSL_EXPORT ASN1_STRING *d2i_DISPLAYTEXT(ASN1_STRING **out, const uint8_t **inp, long len); -// i2d_DISPLAYTEXT marshals |in| as a DER-encoded X.509 DisplayText (RFC 5280), -// as described in |i2d_SAMPLE|. +// i2d_DISPLAYTEXT marshals `in` as a DER-encoded X.509 DisplayText (RFC 5280), +// as described in `i2d_SAMPLE`. OPENSSL_EXPORT int i2d_DISPLAYTEXT(const ASN1_STRING *in, uint8_t **outp); @@ -843,7 +828,7 @@ OPENSSL_EXPORT int i2d_DISPLAYTEXT(const ASN1_STRING *in, uint8_t **outp); // Some BIT STRINGs represent a bitmask of named bits, such as the X.509 key // usage extension in RFC 5280, section 4.2.1.3. For such bit strings, DER // imposes an additional restriction that trailing zero bits are removed. Some -// functions like |ASN1_BIT_STRING_set_bit| help in maintaining this. +// functions like `ASN1_BIT_STRING_set_bit` help in maintaining this. // // Other BIT STRINGs are arbitrary strings of bits used as identifiers and do // not have this constraint, such as the X.509 issuerUniqueID field. @@ -854,50 +839,51 @@ OPENSSL_EXPORT int i2d_DISPLAYTEXT(const ASN1_STRING *in, uint8_t **outp); // AlgorithmIdentifier. While some unknown algorithm could choose to store // arbitrary bit strings, all supported algorithms use a byte string, with bit // order matching the DER encoding. Callers interpreting a BIT STRING as a byte -// string should use |ASN1_BIT_STRING_num_bytes| instead of |ASN1_STRING_length| -// and reject bit strings that are not a whole number of bytes. +// string should reject bit strings that are not a whole number of bytes by +// requiring that `ASN1_BIT_STRING_unused_bits` returns zero; the byte length is +// returned by `ASN1_STRING_length`. // -// This library represents BIT STRINGs as |ASN1_STRING|s with type -// |V_ASN1_BIT_STRING|. The data contains the encoded form of the BIT STRING, +// This library represents BIT STRINGs as `ASN1_STRING`s with type +// `V_ASN1_BIT_STRING`. The data contains the encoded form of the BIT STRING, // including any padding bits added to round to a whole number of bytes, but // excluding the leading byte containing the number of padding bits. Instead, -// the bottom three bits of |flags| contain the number of padding bits. For +// the bottom three bits of `flags` contain the number of padding bits. For // example, DER encodes the BIT STRING {1, 0} as {0x06, 0x80 = 0b10_000000}. The -// |ASN1_STRING| representation has data of {0x80} and flags of 6. +// `ASN1_STRING` representation has data of {0x80} and flags of 6. -// ASN1_BIT_STRING_new calls |ASN1_STRING_type_new| with |V_ASN1_BIT_STRING|. +// ASN1_BIT_STRING_new calls `ASN1_STRING_type_new` with `V_ASN1_BIT_STRING`. OPENSSL_EXPORT ASN1_BIT_STRING *ASN1_BIT_STRING_new(void); -// ASN1_BIT_STRING_free calls |ASN1_STRING_free|. +// ASN1_BIT_STRING_free calls `ASN1_STRING_free`. OPENSSL_EXPORT void ASN1_BIT_STRING_free(ASN1_BIT_STRING *str); -// d2i_ASN1_BIT_STRING parses up to |len| bytes from |*inp| as a DER-encoded -// ASN.1 BIT STRING, as described in |d2i_SAMPLE|. +// d2i_ASN1_BIT_STRING parses up to `len` bytes from `*inp` as a DER-encoded +// ASN.1 BIT STRING, as described in `d2i_SAMPLE`. OPENSSL_EXPORT ASN1_BIT_STRING *d2i_ASN1_BIT_STRING(ASN1_BIT_STRING **out, const uint8_t **inp, long len); -// i2d_ASN1_BIT_STRING marshals |in| as a DER-encoded ASN.1 BIT STRING, as -// described in |i2d_SAMPLE|. +// i2d_ASN1_BIT_STRING marshals `in` as a DER-encoded ASN.1 BIT STRING, as +// described in `i2d_SAMPLE`. OPENSSL_EXPORT int i2d_ASN1_BIT_STRING(const ASN1_BIT_STRING *in, uint8_t **outp); -// c2i_ASN1_BIT_STRING decodes |len| bytes from |*inp| as the contents of a +// c2i_ASN1_BIT_STRING decodes `len` bytes from `*inp` as the contents of a // DER-encoded BIT STRING, excluding the tag and length. It behaves like -// |d2i_SAMPLE| except, on success, it always consumes all |len| bytes. +// `d2i_SAMPLE` except, on success, it always consumes all `len` bytes. OPENSSL_EXPORT ASN1_BIT_STRING *c2i_ASN1_BIT_STRING(ASN1_BIT_STRING **out, const uint8_t **inp, long len); -// i2c_ASN1_BIT_STRING encodes |in| as the contents of a DER-encoded BIT STRING, -// excluding the tag and length. If |outp| is non-NULL, it writes the result to -// |*outp|, advances |*outp| just past the output, and returns the number of -// bytes written. |*outp| must have space available for the result. If |outp| is +// i2c_ASN1_BIT_STRING encodes `in` as the contents of a DER-encoded BIT STRING, +// excluding the tag and length. If `outp` is non-NULL, it writes the result to +// `*outp`, advances `*outp` just past the output, and returns the number of +// bytes written. `*outp` must have space available for the result. If `outp` is // NULL, it returns the number of bytes without writing anything. On error, it // returns a value <= 0. // -// Note this function differs slightly from |i2d_SAMPLE|. If |outp| is non-NULL -// and |*outp| is NULL, it does not allocate a new buffer. +// Note this function differs slightly from `i2d_SAMPLE`. If `outp` is non-NULL +// and `*outp` is NULL, it does not allocate a new buffer. // // TODO(davidben): This function currently returns zero on error instead of -1, // but it is also mostly infallible. I've currently documented <= 0 to suggest @@ -905,44 +891,45 @@ OPENSSL_EXPORT ASN1_BIT_STRING *c2i_ASN1_BIT_STRING(ASN1_BIT_STRING **out, OPENSSL_EXPORT int i2c_ASN1_BIT_STRING(const ASN1_BIT_STRING *in, uint8_t **outp); -// ASN1_BIT_STRING is an |ASN1_ITEM| with ASN.1 type BIT STRING and C type -// |ASN1_BIT_STRING*|. +// ASN1_BIT_STRING is an `ASN1_ITEM` with ASN.1 type BIT STRING and C type +// `ASN1_BIT_STRING*`. DECLARE_ASN1_ITEM(ASN1_BIT_STRING) // ASN1_BIT_STRING_unused_bits returns the number of unused bits in the last -// byte of |str|. If |str| is empty (i.e. |ASN1_STRING_length| is zero), this +// byte of `str`. If `str` is empty (i.e. `ASN1_STRING_length` is zero), this // always returns zero. Otherwise it returns a number between 0 and 7. OPENSSL_EXPORT uint8_t ASN1_BIT_STRING_unused_bits(const ASN1_BIT_STRING *str); -// ASN1_BIT_STRING_set calls |ASN1_STRING_set|. -OPENSSL_EXPORT int ASN1_BIT_STRING_set(ASN1_BIT_STRING *str, const uint8_t *data, +// ASN1_BIT_STRING_set calls `ASN1_STRING_set`. +OPENSSL_EXPORT int ASN1_BIT_STRING_set(ASN1_BIT_STRING *str, + const uint8_t *data, ossl_ssize_t length); -// ASN1_BIT_STRING_set1 sets |str| to a BIT STRING containing |length| bytes -// from |data|. It returns one on success and zero on error. The least -// significant |unused_bits| of the last byte of |data| are removed from the bit -// string. The removed bits must all be zero. |unused_bits| must be between 0 -// and 7, and must be 0 if |length| is zero. +// ASN1_BIT_STRING_set1 sets `str` to a BIT STRING containing `length` bytes +// from `data`. It returns one on success and zero on error. The least +// significant `unused_bits` of the last byte of `data` are removed from the bit +// string. The removed bits must all be zero. `unused_bits` must be between 0 +// and 7, and must be 0 if `length` is zero. OPENSSL_EXPORT int ASN1_BIT_STRING_set1(ASN1_BIT_STRING *str, const uint8_t *data, size_t length, int unused_bits); -// ASN1_BIT_STRING_set_bit sets bit |n| of |str| to one if |value| is non-zero -// and zero if |value| is zero, resizing |str| as needed. It then truncates -// trailing zeros in |str| to align with the DER representation for a bit string -// with named bits. It returns one on success and zero on error. |n| is indexed +// ASN1_BIT_STRING_set_bit sets bit `n` of `str` to one if `value` is non-zero +// and zero if `value` is zero, resizing `str` as needed. It then truncates +// trailing zeros in `str` to align with the DER representation for a bit string +// with named bits. It returns one on success and zero on error. `n` is indexed // beginning from zero. OPENSSL_EXPORT int ASN1_BIT_STRING_set_bit(ASN1_BIT_STRING *str, int n, int value); -// ASN1_BIT_STRING_get_bit returns one if bit |n| of |a| is in bounds and set, -// and zero otherwise. |n| is indexed beginning from zero. +// ASN1_BIT_STRING_get_bit returns one if bit `n` of `a` is in bounds and set, +// and zero otherwise. `n` is indexed beginning from zero. OPENSSL_EXPORT int ASN1_BIT_STRING_get_bit(const ASN1_BIT_STRING *str, int n); -// ASN1_BIT_STRING_check returns one if |str| only contains bits that are set in -// the |flags_len| bytes pointed by |flags|. Otherwise it returns zero. Bits in -// |flags| are arranged according to the DER representation, so bit 0 -// corresponds to the MSB of |flags[0]|. +// ASN1_BIT_STRING_check returns one if `str` only contains bits that are set in +// the `flags_len` bytes pointed by `flags`. Otherwise it returns zero. Bits in +// `flags` are arranged according to the DER representation, so bit 0 +// corresponds to the MSB of `flags[0]`. OPENSSL_EXPORT int ASN1_BIT_STRING_check(const ASN1_BIT_STRING *str, const unsigned char *flags, int flags_len); @@ -950,162 +937,162 @@ OPENSSL_EXPORT int ASN1_BIT_STRING_check(const ASN1_BIT_STRING *str, // Integers and enumerated values. // -// INTEGER and ENUMERATED values are represented as |ASN1_STRING|s where the +// INTEGER and ENUMERATED values are represented as `ASN1_STRING`s where the // data contains the big-endian encoding of the absolute value of the integer. // The sign bit is encoded in the type: non-negative values have a type of -// |V_ASN1_INTEGER| or |V_ASN1_ENUMERATED|, while negative values have a type of -// |V_ASN1_NEG_INTEGER| or |V_ASN1_NEG_ENUMERATED|. Note this differs from DER's +// `V_ASN1_INTEGER` or `V_ASN1_ENUMERATED`, while negative values have a type of +// `V_ASN1_NEG_INTEGER` or `V_ASN1_NEG_ENUMERATED`. Note this differs from DER's // two's complement representation. // -// The data in the |ASN1_STRING| may not have leading zeros. Note this means +// The data in the `ASN1_STRING` may not have leading zeros. Note this means // zero is represented as the empty string. Parsing functions will never return // invalid representations. If an invalid input is constructed, the marshaling // functions will skip leading zeros, however other functions, such as -// |ASN1_INTEGER_cmp| or |ASN1_INTEGER_get|, may not return the correct result. +// `ASN1_INTEGER_cmp` or `ASN1_INTEGER_get`, may not return the correct result. DEFINE_STACK_OF(ASN1_INTEGER) -// ASN1_INTEGER_new calls |ASN1_STRING_type_new| with |V_ASN1_INTEGER|. The +// ASN1_INTEGER_new calls `ASN1_STRING_type_new` with `V_ASN1_INTEGER`. The // resulting object has value zero. OPENSSL_EXPORT ASN1_INTEGER *ASN1_INTEGER_new(void); -// ASN1_INTEGER_free calls |ASN1_STRING_free|. +// ASN1_INTEGER_free calls `ASN1_STRING_free`. OPENSSL_EXPORT void ASN1_INTEGER_free(ASN1_INTEGER *str); -// ASN1_INTEGER_dup calls |ASN1_STRING_dup|. +// ASN1_INTEGER_dup calls `ASN1_STRING_dup`. OPENSSL_EXPORT ASN1_INTEGER *ASN1_INTEGER_dup(const ASN1_INTEGER *x); -// d2i_ASN1_INTEGER parses up to |len| bytes from |*inp| as a DER-encoded -// ASN.1 INTEGER, as described in |d2i_SAMPLE|. +// d2i_ASN1_INTEGER parses up to `len` bytes from `*inp` as a DER-encoded +// ASN.1 INTEGER, as described in `d2i_SAMPLE`. OPENSSL_EXPORT ASN1_INTEGER *d2i_ASN1_INTEGER(ASN1_INTEGER **out, const uint8_t **inp, long len); -// i2d_ASN1_INTEGER marshals |in| as a DER-encoded ASN.1 INTEGER, as -// described in |i2d_SAMPLE|. +// i2d_ASN1_INTEGER marshals `in` as a DER-encoded ASN.1 INTEGER, as +// described in `i2d_SAMPLE`. OPENSSL_EXPORT int i2d_ASN1_INTEGER(const ASN1_INTEGER *in, uint8_t **outp); -// c2i_ASN1_INTEGER decodes |len| bytes from |*inp| as the contents of a +// c2i_ASN1_INTEGER decodes `len` bytes from `*inp` as the contents of a // DER-encoded INTEGER, excluding the tag and length. It behaves like -// |d2i_SAMPLE| except, on success, it always consumes all |len| bytes. +// `d2i_SAMPLE` except, on success, it always consumes all `len` bytes. OPENSSL_EXPORT ASN1_INTEGER *c2i_ASN1_INTEGER(ASN1_INTEGER **in, const uint8_t **outp, long len); -// i2c_ASN1_INTEGER encodes |in| as the contents of a DER-encoded INTEGER, -// excluding the tag and length. If |outp| is non-NULL, it writes the result to -// |*outp|, advances |*outp| just past the output, and returns the number of -// bytes written. |*outp| must have space available for the result. If |outp| is +// i2c_ASN1_INTEGER encodes `in` as the contents of a DER-encoded INTEGER, +// excluding the tag and length. If `outp` is non-NULL, it writes the result to +// `*outp`, advances `*outp` just past the output, and returns the number of +// bytes written. `*outp` must have space available for the result. If `outp` is // NULL, it returns the number of bytes without writing anything. On error, it // returns a value <= 0. // -// Note this function differs slightly from |i2d_SAMPLE|. If |outp| is non-NULL -// and |*outp| is NULL, it does not allocate a new buffer. +// Note this function differs slightly from `i2d_SAMPLE`. If `outp` is non-NULL +// and `*outp` is NULL, it does not allocate a new buffer. // // TODO(davidben): This function currently returns zero on error instead of -1, // but it is also mostly infallible. I've currently documented <= 0 to suggest // callers work with both. OPENSSL_EXPORT int i2c_ASN1_INTEGER(const ASN1_INTEGER *in, uint8_t **outp); -// ASN1_INTEGER is an |ASN1_ITEM| with ASN.1 type INTEGER and C type -// |ASN1_INTEGER*|. +// ASN1_INTEGER is an `ASN1_ITEM` with ASN.1 type INTEGER and C type +// `ASN1_INTEGER*`. DECLARE_ASN1_ITEM(ASN1_INTEGER) -// ASN1_INTEGER_set_uint64 sets |a| to an INTEGER with value |v|. It returns one +// ASN1_INTEGER_set_uint64 sets `a` to an INTEGER with value `v`. It returns one // on success and zero on error. OPENSSL_EXPORT int ASN1_INTEGER_set_uint64(ASN1_INTEGER *out, uint64_t v); -// ASN1_INTEGER_set_int64 sets |a| to an INTEGER with value |v|. It returns one +// ASN1_INTEGER_set_int64 sets `a` to an INTEGER with value `v`. It returns one // on success and zero on error. OPENSSL_EXPORT int ASN1_INTEGER_set_int64(ASN1_INTEGER *out, int64_t v); -// ASN1_INTEGER_get_uint64 converts |a| to a |uint64_t|. On success, it returns -// one and sets |*out| to the result. If |a| did not fit or has the wrong type, +// ASN1_INTEGER_get_uint64 converts `a` to a `uint64_t`. On success, it returns +// one and sets `*out` to the result. If `a` did not fit or has the wrong type, // it returns zero. OPENSSL_EXPORT int ASN1_INTEGER_get_uint64(uint64_t *out, const ASN1_INTEGER *a); -// ASN1_INTEGER_get_int64 converts |a| to a |int64_t|. On success, it returns -// one and sets |*out| to the result. If |a| did not fit or has the wrong type, +// ASN1_INTEGER_get_int64 converts `a` to a `int64_t`. On success, it returns +// one and sets `*out` to the result. If `a` did not fit or has the wrong type, // it returns zero. OPENSSL_EXPORT int ASN1_INTEGER_get_int64(int64_t *out, const ASN1_INTEGER *a); -// BN_to_ASN1_INTEGER sets |ai| to an INTEGER with value |bn| and returns |ai| -// on success or NULL or error. If |ai| is NULL, it returns a newly-allocated -// |ASN1_INTEGER| on success instead, which the caller must release with -// |ASN1_INTEGER_free|. +// BN_to_ASN1_INTEGER sets `ai` to an INTEGER with value `bn` and returns `ai` +// on success or NULL or error. If `ai` is NULL, it returns a newly-allocated +// `ASN1_INTEGER` on success instead, which the caller must release with +// `ASN1_INTEGER_free`. OPENSSL_EXPORT ASN1_INTEGER *BN_to_ASN1_INTEGER(const BIGNUM *bn, ASN1_INTEGER *ai); -// ASN1_INTEGER_to_BN sets |bn| to the value of |ai| and returns |bn| on success -// or NULL or error. If |bn| is NULL, it returns a newly-allocated |BIGNUM| on -// success instead, which the caller must release with |BN_free|. +// ASN1_INTEGER_to_BN sets `bn` to the value of `ai` and returns `bn` on success +// or NULL or error. If `bn` is NULL, it returns a newly-allocated `BIGNUM` on +// success instead, which the caller must release with `BN_free`. OPENSSL_EXPORT BIGNUM *ASN1_INTEGER_to_BN(const ASN1_INTEGER *ai, BIGNUM *bn); -// ASN1_INTEGER_cmp compares the values of |x| and |y|. It returns an integer -// equal to, less than, or greater than zero if |x| is equal to, less than, or -// greater than |y|, respectively. +// ASN1_INTEGER_cmp compares the values of `x` and `y`. It returns an integer +// equal to, less than, or greater than zero if `x` is equal to, less than, or +// greater than `y`, respectively. OPENSSL_EXPORT int ASN1_INTEGER_cmp(const ASN1_INTEGER *x, const ASN1_INTEGER *y); -// ASN1_ENUMERATED_new calls |ASN1_STRING_type_new| with |V_ASN1_ENUMERATED|. +// ASN1_ENUMERATED_new calls `ASN1_STRING_type_new` with `V_ASN1_ENUMERATED`. // The resulting object has value zero. OPENSSL_EXPORT ASN1_ENUMERATED *ASN1_ENUMERATED_new(void); -// ASN1_ENUMERATED_free calls |ASN1_STRING_free|. +// ASN1_ENUMERATED_free calls `ASN1_STRING_free`. OPENSSL_EXPORT void ASN1_ENUMERATED_free(ASN1_ENUMERATED *str); -// d2i_ASN1_ENUMERATED parses up to |len| bytes from |*inp| as a DER-encoded -// ASN.1 ENUMERATED, as described in |d2i_SAMPLE|. +// d2i_ASN1_ENUMERATED parses up to `len` bytes from `*inp` as a DER-encoded +// ASN.1 ENUMERATED, as described in `d2i_SAMPLE`. OPENSSL_EXPORT ASN1_ENUMERATED *d2i_ASN1_ENUMERATED(ASN1_ENUMERATED **out, const uint8_t **inp, long len); -// i2d_ASN1_ENUMERATED marshals |in| as a DER-encoded ASN.1 ENUMERATED, as -// described in |i2d_SAMPLE|. +// i2d_ASN1_ENUMERATED marshals `in` as a DER-encoded ASN.1 ENUMERATED, as +// described in `i2d_SAMPLE`. OPENSSL_EXPORT int i2d_ASN1_ENUMERATED(const ASN1_ENUMERATED *in, uint8_t **outp); -// ASN1_ENUMERATED is an |ASN1_ITEM| with ASN.1 type ENUMERATED and C type -// |ASN1_ENUMERATED*|. +// ASN1_ENUMERATED is an `ASN1_ITEM` with ASN.1 type ENUMERATED and C type +// `ASN1_ENUMERATED*`. DECLARE_ASN1_ITEM(ASN1_ENUMERATED) -// ASN1_ENUMERATED_set_uint64 sets |a| to an ENUMERATED with value |v|. It +// ASN1_ENUMERATED_set_uint64 sets `a` to an ENUMERATED with value `v`. It // returns one on success and zero on error. OPENSSL_EXPORT int ASN1_ENUMERATED_set_uint64(ASN1_ENUMERATED *out, uint64_t v); -// ASN1_ENUMERATED_set_int64 sets |a| to an ENUMERATED with value |v|. It +// ASN1_ENUMERATED_set_int64 sets `a` to an ENUMERATED with value `v`. It // returns one on success and zero on error. OPENSSL_EXPORT int ASN1_ENUMERATED_set_int64(ASN1_ENUMERATED *out, int64_t v); -// ASN1_ENUMERATED_get_uint64 converts |a| to a |uint64_t|. On success, it -// returns one and sets |*out| to the result. If |a| did not fit or has the +// ASN1_ENUMERATED_get_uint64 converts `a` to a `uint64_t`. On success, it +// returns one and sets `*out` to the result. If `a` did not fit or has the // wrong type, it returns zero. OPENSSL_EXPORT int ASN1_ENUMERATED_get_uint64(uint64_t *out, const ASN1_ENUMERATED *a); -// ASN1_ENUMERATED_get_int64 converts |a| to a |int64_t|. On success, it -// returns one and sets |*out| to the result. If |a| did not fit or has the +// ASN1_ENUMERATED_get_int64 converts `a` to a `int64_t`. On success, it +// returns one and sets `*out` to the result. If `a` did not fit or has the // wrong type, it returns zero. OPENSSL_EXPORT int ASN1_ENUMERATED_get_int64(int64_t *out, const ASN1_ENUMERATED *a); -// BN_to_ASN1_ENUMERATED sets |ai| to an ENUMERATED with value |bn| and returns -// |ai| on success or NULL or error. If |ai| is NULL, it returns a -// newly-allocated |ASN1_ENUMERATED| on success instead, which the caller must -// release with |ASN1_ENUMERATED_free|. +// BN_to_ASN1_ENUMERATED sets `ai` to an ENUMERATED with value `bn` and returns +// `ai` on success or NULL or error. If `ai` is NULL, it returns a +// newly-allocated `ASN1_ENUMERATED` on success instead, which the caller must +// release with `ASN1_ENUMERATED_free`. OPENSSL_EXPORT ASN1_ENUMERATED *BN_to_ASN1_ENUMERATED(const BIGNUM *bn, ASN1_ENUMERATED *ai); -// ASN1_ENUMERATED_to_BN sets |bn| to the value of |ai| and returns |bn| on -// success or NULL or error. If |bn| is NULL, it returns a newly-allocated -// |BIGNUM| on success instead, which the caller must release with |BN_free|. +// ASN1_ENUMERATED_to_BN sets `bn` to the value of `ai` and returns `bn` on +// success or NULL or error. If `bn` is NULL, it returns a newly-allocated +// `BIGNUM` on success instead, which the caller must release with `BN_free`. OPENSSL_EXPORT BIGNUM *ASN1_ENUMERATED_to_BN(const ASN1_ENUMERATED *ai, BIGNUM *bn); // Time. // -// GeneralizedTime and UTCTime values are represented as |ASN1_STRING|s. The -// type field is |V_ASN1_GENERALIZEDTIME| or |V_ASN1_UTCTIME|, respectively. The +// GeneralizedTime and UTCTime values are represented as `ASN1_STRING`s. The +// type field is `V_ASN1_GENERALIZEDTIME` or `V_ASN1_UTCTIME`, respectively. The // data field contains the DER encoding of the value. For example, the UNIX // epoch would be "19700101000000Z" for a GeneralizedTime and "700101000000Z" // for a UTCTime. @@ -1116,49 +1103,46 @@ OPENSSL_EXPORT BIGNUM *ASN1_ENUMERATED_to_BN(const ASN1_ENUMERATED *ai, // BER, and the additional restrictions from RFC 5280, but future versions may. // Callers should not rely on fractional seconds and non-UTC time zones. // -// The |ASN1_TIME| typedef is a multi-string representing the X.509 Time type, +// The `ASN1_TIME` typedef is a multi-string representing the X.509 Time type, // which is a CHOICE of GeneralizedTime and UTCTime, using UTCTime when the // value is in range. -// ASN1_UTCTIME_new calls |ASN1_STRING_type_new| with |V_ASN1_UTCTIME|. The +// ASN1_UTCTIME_new calls `ASN1_STRING_type_new` with `V_ASN1_UTCTIME`. The // resulting object contains empty contents and must be initialized to be a // valid UTCTime. OPENSSL_EXPORT ASN1_UTCTIME *ASN1_UTCTIME_new(void); -// ASN1_UTCTIME_free calls |ASN1_STRING_free|. +// ASN1_UTCTIME_free calls `ASN1_STRING_free`. OPENSSL_EXPORT void ASN1_UTCTIME_free(ASN1_UTCTIME *str); -// d2i_ASN1_UTCTIME parses up to |len| bytes from |*inp| as a DER-encoded -// ASN.1 UTCTime, as described in |d2i_SAMPLE|. -// -// TODO(https://crbug.com/boringssl/354): This function currently also accepts -// BER, but this will be removed in the future. +// d2i_ASN1_UTCTIME parses up to `len` bytes from `*inp` as a DER-encoded +// ASN.1 UTCTime, as described in `d2i_SAMPLE`. OPENSSL_EXPORT ASN1_UTCTIME *d2i_ASN1_UTCTIME(ASN1_UTCTIME **out, const uint8_t **inp, long len); -// i2d_ASN1_UTCTIME marshals |in| as a DER-encoded ASN.1 UTCTime, as -// described in |i2d_SAMPLE|. +// i2d_ASN1_UTCTIME marshals `in` as a DER-encoded ASN.1 UTCTime, as +// described in `i2d_SAMPLE`. OPENSSL_EXPORT int i2d_ASN1_UTCTIME(const ASN1_UTCTIME *in, uint8_t **outp); -// ASN1_UTCTIME is an |ASN1_ITEM| with ASN.1 type UTCTime and C type -// |ASN1_UTCTIME*|. +// ASN1_UTCTIME is an `ASN1_ITEM` with ASN.1 type UTCTime and C type +// `ASN1_UTCTIME*`. DECLARE_ASN1_ITEM(ASN1_UTCTIME) -// ASN1_UTCTIME_check returns one if |a| is a valid UTCTime and zero otherwise. +// ASN1_UTCTIME_check returns one if `a` is a valid UTCTime and zero otherwise. OPENSSL_EXPORT int ASN1_UTCTIME_check(const ASN1_UTCTIME *a); -// ASN1_UTCTIME_set represents |posix_time| as a UTCTime and writes the result -// to |s|. It returns |s| on success and NULL on error. If |s| is NULL, it -// returns a newly-allocated |ASN1_UTCTIME| instead. +// ASN1_UTCTIME_set represents `posix_time` as a UTCTime and writes the result +// to `s`. It returns `s` on success and NULL on error. If `s` is NULL, it +// returns a newly-allocated `ASN1_UTCTIME` instead. // // Note this function may fail if the time is out of range for UTCTime. OPENSSL_EXPORT ASN1_UTCTIME *ASN1_UTCTIME_set(ASN1_UTCTIME *s, int64_t posix_time); -// ASN1_UTCTIME_adj adds |offset_day| days and |offset_sec| seconds to -// |posix_time| and writes the result to |s| as a UTCTime. It returns |s| on -// success and NULL on error. If |s| is NULL, it returns a newly-allocated -// |ASN1_UTCTIME| instead. +// ASN1_UTCTIME_adj adds `offset_day` days and `offset_sec` seconds to +// `posix_time` and writes the result to `s` as a UTCTime. It returns `s` on +// success and NULL on error. If `s` is NULL, it returns a newly-allocated +// `ASN1_UTCTIME` instead. // // Note this function may fail if the time overflows or is out of range for // UTCTime. @@ -1166,51 +1150,51 @@ OPENSSL_EXPORT ASN1_UTCTIME *ASN1_UTCTIME_adj(ASN1_UTCTIME *s, int64_t posix_time, int offset_day, long offset_sec); -// ASN1_UTCTIME_set_string sets |s| to a UTCTime whose contents are a copy of -// |str|. It returns one on success and zero on error or if |str| is not a valid +// ASN1_UTCTIME_set_string sets `s` to a UTCTime whose contents are a copy of +// `str`. It returns one on success and zero on error or if `str` is not a valid // UTCTime. // -// If |s| is NULL, this function validates |str| without copying it. +// If `s` is NULL, this function validates `str` without copying it. OPENSSL_EXPORT int ASN1_UTCTIME_set_string(ASN1_UTCTIME *s, const char *str); -// ASN1_GENERALIZEDTIME_new calls |ASN1_STRING_type_new| with -// |V_ASN1_GENERALIZEDTIME|. The resulting object contains empty contents and +// ASN1_GENERALIZEDTIME_new calls `ASN1_STRING_type_new` with +// `V_ASN1_GENERALIZEDTIME`. The resulting object contains empty contents and // must be initialized to be a valid GeneralizedTime. OPENSSL_EXPORT ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_new(void); -// ASN1_GENERALIZEDTIME_free calls |ASN1_STRING_free|. +// ASN1_GENERALIZEDTIME_free calls `ASN1_STRING_free`. OPENSSL_EXPORT void ASN1_GENERALIZEDTIME_free(ASN1_GENERALIZEDTIME *str); -// d2i_ASN1_GENERALIZEDTIME parses up to |len| bytes from |*inp| as a -// DER-encoded ASN.1 GeneralizedTime, as described in |d2i_SAMPLE|. +// d2i_ASN1_GENERALIZEDTIME parses up to `len` bytes from `*inp` as a +// DER-encoded ASN.1 GeneralizedTime, as described in `d2i_SAMPLE`. OPENSSL_EXPORT ASN1_GENERALIZEDTIME *d2i_ASN1_GENERALIZEDTIME( ASN1_GENERALIZEDTIME **out, const uint8_t **inp, long len); -// i2d_ASN1_GENERALIZEDTIME marshals |in| as a DER-encoded ASN.1 -// GeneralizedTime, as described in |i2d_SAMPLE|. +// i2d_ASN1_GENERALIZEDTIME marshals `in` as a DER-encoded ASN.1 +// GeneralizedTime, as described in `i2d_SAMPLE`. OPENSSL_EXPORT int i2d_ASN1_GENERALIZEDTIME(const ASN1_GENERALIZEDTIME *in, uint8_t **outp); -// ASN1_GENERALIZEDTIME is an |ASN1_ITEM| with ASN.1 type GeneralizedTime and C -// type |ASN1_GENERALIZEDTIME*|. +// ASN1_GENERALIZEDTIME is an `ASN1_ITEM` with ASN.1 type GeneralizedTime and C +// type `ASN1_GENERALIZEDTIME*`. DECLARE_ASN1_ITEM(ASN1_GENERALIZEDTIME) -// ASN1_GENERALIZEDTIME_check returns one if |a| is a valid GeneralizedTime and +// ASN1_GENERALIZEDTIME_check returns one if `a` is a valid GeneralizedTime and // zero otherwise. OPENSSL_EXPORT int ASN1_GENERALIZEDTIME_check(const ASN1_GENERALIZEDTIME *a); -// ASN1_GENERALIZEDTIME_set represents |posix_time| as a GeneralizedTime and -// writes the result to |s|. It returns |s| on success and NULL on error. If |s| -// is NULL, it returns a newly-allocated |ASN1_GENERALIZEDTIME| instead. +// ASN1_GENERALIZEDTIME_set represents `posix_time` as a GeneralizedTime and +// writes the result to `s`. It returns `s` on success and NULL on error. If `s` +// is NULL, it returns a newly-allocated `ASN1_GENERALIZEDTIME` instead. // // Note this function may fail if the time is out of range for GeneralizedTime. OPENSSL_EXPORT ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_set( ASN1_GENERALIZEDTIME *s, int64_t posix_time); -// ASN1_GENERALIZEDTIME_adj adds |offset_day| days and |offset_sec| seconds to -// |posix_time| and writes the result to |s| as a GeneralizedTime. It returns -// |s| on success and NULL on error. If |s| is NULL, it returns a -// newly-allocated |ASN1_GENERALIZEDTIME| instead. +// ASN1_GENERALIZEDTIME_adj adds `offset_day` days and `offset_sec` seconds to +// `posix_time` and writes the result to `s` as a GeneralizedTime. It returns +// `s` on success and NULL on error. If `s` is NULL, it returns a +// newly-allocated `ASN1_GENERALIZEDTIME` instead. // // Note this function may fail if the time overflows or is out of range for // GeneralizedTime. @@ -1218,113 +1202,110 @@ OPENSSL_EXPORT ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_adj( ASN1_GENERALIZEDTIME *s, int64_t posix_time, int offset_day, long offset_sec); -// ASN1_GENERALIZEDTIME_set_string sets |s| to a GeneralizedTime whose contents -// are a copy of |str|. It returns one on success and zero on error or if |str| +// ASN1_GENERALIZEDTIME_set_string sets `s` to a GeneralizedTime whose contents +// are a copy of `str`. It returns one on success and zero on error or if `str` // is not a valid GeneralizedTime. // -// If |s| is NULL, this function validates |str| without copying it. +// If `s` is NULL, this function validates `str` without copying it. OPENSSL_EXPORT int ASN1_GENERALIZEDTIME_set_string(ASN1_GENERALIZEDTIME *s, const char *str); // B_ASN1_TIME is a bitmask of types allowed in an X.509 Time. #define B_ASN1_TIME (B_ASN1_UTCTIME | B_ASN1_GENERALIZEDTIME) -// ASN1_TIME_new returns a newly-allocated |ASN1_TIME| with type -1, or NULL on -// error. The resulting |ASN1_TIME| is not a valid X.509 Time until initialized +// ASN1_TIME_new returns a newly-allocated `ASN1_TIME` with type -1, or NULL on +// error. The resulting `ASN1_TIME` is not a valid X.509 Time until initialized // with a value. OPENSSL_EXPORT ASN1_TIME *ASN1_TIME_new(void); -// ASN1_TIME_free releases memory associated with |str|. +// ASN1_TIME_free releases memory associated with `str`. OPENSSL_EXPORT void ASN1_TIME_free(ASN1_TIME *str); -// d2i_ASN1_TIME parses up to |len| bytes from |*inp| as a DER-encoded X.509 -// Time (RFC 5280), as described in |d2i_SAMPLE|. -// -// TODO(https://crbug.com/boringssl/354): This function currently also accepts -// BER, but this will be removed in the future. +// d2i_ASN1_TIME parses up to `len` bytes from `*inp` as a DER-encoded X.509 +// Time (RFC 5280), as described in `d2i_SAMPLE`. OPENSSL_EXPORT ASN1_TIME *d2i_ASN1_TIME(ASN1_TIME **out, const uint8_t **inp, long len); -// i2d_ASN1_TIME marshals |in| as a DER-encoded X.509 Time (RFC 5280), as -// described in |i2d_SAMPLE|. +// i2d_ASN1_TIME marshals `in` as a DER-encoded X.509 Time (RFC 5280), as +// described in `i2d_SAMPLE`. OPENSSL_EXPORT int i2d_ASN1_TIME(const ASN1_TIME *in, uint8_t **outp); -// ASN1_TIME_diff computes |to| - |from|. On success, it sets |*out_days| to the -// difference in days, rounded towards zero, sets |*out_seconds| to the +// ASN1_TIME_diff computes `to` - `from`. On success, it sets `*out_days` to the +// difference in days, rounded towards zero, sets `*out_seconds` to the // remainder, and returns one. On error, it returns zero. // -// If |from| is before |to|, both outputs will be <= 0, with at least one -// negative. If |from| is after |to|, both will be >= 0, with at least one +// If `from` is before `to`, both outputs will be <= 0, with at least one +// negative. If `from` is after `to`, both will be >= 0, with at least one // positive. If they are equal, ignoring fractional seconds, both will be zero. // -// Note this function may fail on overflow, or if |from| or |to| cannot be +// Note this function may fail on overflow, or if `from` or `to` cannot be // decoded. OPENSSL_EXPORT int ASN1_TIME_diff(int *out_days, int *out_seconds, const ASN1_TIME *from, const ASN1_TIME *to); -// ASN1_TIME_set_posix represents |posix_time| as a GeneralizedTime or UTCTime -// and writes the result to |s|. As in RFC 5280, section 4.1.2.5, it uses -// UTCTime when the time fits and GeneralizedTime otherwise. It returns |s| on -// success and NULL on error. If |s| is NULL, it returns a newly-allocated -// |ASN1_TIME| instead. +// ASN1_TIME_set_posix represents `posix_time` as a GeneralizedTime or UTCTime +// and writes the result to `s`. As in RFC 5280, section 4.1.2.5, it uses +// UTCTime when the time fits and GeneralizedTime otherwise. It returns `s` on +// success and NULL on error. If `s` is NULL, it returns a newly-allocated +// `ASN1_TIME` instead. // // Note this function may fail if the time is out of range for GeneralizedTime. OPENSSL_EXPORT ASN1_TIME *ASN1_TIME_set_posix(ASN1_TIME *s, int64_t posix_time); -// ASN1_TIME_set is exactly the same as |ASN1_TIME_set_posix| but with a +// ASN1_TIME_set is exactly the same as `ASN1_TIME_set_posix` but with a // time_t as input for compatibility. OPENSSL_EXPORT ASN1_TIME *ASN1_TIME_set(ASN1_TIME *s, time_t time); -// ASN1_TIME_adj adds |offset_day| days and |offset_sec| seconds to -// |posix_time| and writes the result to |s|. As in RFC 5280, section 4.1.2.5, +// ASN1_TIME_adj adds `offset_day` days and `offset_sec` seconds to +// `posix_time` and writes the result to `s`. As in RFC 5280, section 4.1.2.5, // it uses UTCTime when the time fits and GeneralizedTime otherwise. It returns -// |s| on success and NULL on error. If |s| is NULL, it returns a -// newly-allocated |ASN1_GENERALIZEDTIME| instead. +// `s` on success and NULL on error. If `s` is NULL, it returns a +// newly-allocated `ASN1_GENERALIZEDTIME` instead. // // Note this function may fail if the time overflows or is out of range for // GeneralizedTime. OPENSSL_EXPORT ASN1_TIME *ASN1_TIME_adj(ASN1_TIME *s, int64_t posix_time, int offset_day, long offset_sec); -// ASN1_TIME_check returns one if |t| is a valid UTCTime or GeneralizedTime, and -// zero otherwise. |t|'s type determines which check is performed. This +// ASN1_TIME_check returns one if `t` is a valid UTCTime or GeneralizedTime, and +// zero otherwise. `t`'s type determines which check is performed. This // function does not enforce that UTCTime was used when possible. OPENSSL_EXPORT int ASN1_TIME_check(const ASN1_TIME *t); -// ASN1_TIME_to_generalizedtime converts |t| to a GeneralizedTime. If |out| is -// NULL, it returns a newly-allocated |ASN1_GENERALIZEDTIME| on success, or NULL -// on error. If |out| is non-NULL and |*out| is NULL, it additionally sets -// |*out| to the result. If |out| and |*out| are non-NULL, it instead updates -// the object pointed by |*out| and returns |*out| on success or NULL on error. +// ASN1_TIME_to_generalizedtime converts `t` to a GeneralizedTime. If `out` is +// NULL, it returns a newly-allocated `ASN1_GENERALIZEDTIME` on success, or NULL +// on error. If `out` is non-NULL and `*out` is NULL, it additionally sets +// `*out` to the result. If `out` and `*out` are non-NULL, it instead updates +// the object pointed by `*out` and returns `*out` on success or NULL on error. OPENSSL_EXPORT ASN1_GENERALIZEDTIME *ASN1_TIME_to_generalizedtime( const ASN1_TIME *t, ASN1_GENERALIZEDTIME **out); -// ASN1_TIME_set_string behaves like |ASN1_UTCTIME_set_string| if |str| is a -// valid UTCTime, and |ASN1_GENERALIZEDTIME_set_string| if |str| is a valid -// GeneralizedTime. If |str| is neither, it returns zero. +// ASN1_TIME_set_string behaves like `ASN1_UTCTIME_set_string` if `str` is a +// valid UTCTime, and `ASN1_GENERALIZEDTIME_set_string` if `str` is a valid +// GeneralizedTime. If `str` is neither, it returns zero. OPENSSL_EXPORT int ASN1_TIME_set_string(ASN1_TIME *s, const char *str); -// ASN1_TIME_set_string_X509 behaves like |ASN1_TIME_set_string| except it +// ASN1_TIME_set_string_X509 behaves like `ASN1_TIME_set_string` except it // additionally converts GeneralizedTime to UTCTime if it is in the range where // UTCTime is used. See RFC 5280, section 4.1.2.5. OPENSSL_EXPORT int ASN1_TIME_set_string_X509(ASN1_TIME *s, const char *str); -// ASN1_TIME_to_time_t converts |t| to a time_t value in |out|. On +// ASN1_TIME_to_time_t converts `t` to a time_t value in `out`. On // success, one is returned. On failure, zero is returned. This function // will fail if the time can not be represented in a time_t. OPENSSL_EXPORT int ASN1_TIME_to_time_t(const ASN1_TIME *t, time_t *out); -// ASN1_TIME_to_posix converts |t| to a POSIX time value in |out|. On +// ASN1_TIME_to_posix converts `t` to a POSIX time value in `out`. On // success, one is returned. On failure, zero is returned. OPENSSL_EXPORT int ASN1_TIME_to_posix(const ASN1_TIME *t, int64_t *out); -// ASN1_TIME_to_posix_nonstandard converts |t| to a POSIX time value in -// |out|. It is exactly the same as |ASN1_TIME_to_posix| but allows for +// ASN1_TIME_to_posix_nonstandard converts `t` to a POSIX time value in +// `out`. It is exactly the same as `ASN1_TIME_to_posix` but allows for // non-standard four-digit timezone offsets on UTC times. On success, one is -// returned. On failure, zero is returned. |ASN1_TIME_to_posix| should normally +// returned. On failure, zero is returned. `ASN1_TIME_to_posix` should normally // be used instead of this function. -OPENSSL_EXPORT int ASN1_TIME_to_posix_nonstandard( - const ASN1_TIME *t, int64_t *out); +OPENSSL_EXPORT int ASN1_TIME_to_posix_nonstandard(const ASN1_TIME *t, + int64_t *out); // TODO(davidben): Expand and document function prototypes generated in macros. @@ -1332,120 +1313,120 @@ OPENSSL_EXPORT int ASN1_TIME_to_posix_nonstandard( // NULL values. // // This library represents the ASN.1 NULL value by a non-NULL pointer to the -// opaque type |ASN1_NULL|. An omitted OPTIONAL ASN.1 NULL value is a NULL -// pointer. Unlike other pointer types, it is not necessary to free |ASN1_NULL| +// opaque type `ASN1_NULL`. An omitted OPTIONAL ASN.1 NULL value is a NULL +// pointer. Unlike other pointer types, it is not necessary to free `ASN1_NULL` // pointers, but it is safe to do so. // ASN1_NULL_new returns an opaque, non-NULL pointer. It is safe to call -// |ASN1_NULL_free| on the result, but not necessary. +// `ASN1_NULL_free` on the result, but not necessary. OPENSSL_EXPORT ASN1_NULL *ASN1_NULL_new(void); // ASN1_NULL_free does nothing. OPENSSL_EXPORT void ASN1_NULL_free(ASN1_NULL *null); -// d2i_ASN1_NULL parses a DER-encoded ASN.1 NULL value from up to |len| bytes -// at |*inp|, as described in |d2i_SAMPLE|. +// d2i_ASN1_NULL parses a DER-encoded ASN.1 NULL value from up to `len` bytes +// at `*inp`, as described in `d2i_SAMPLE`. OPENSSL_EXPORT ASN1_NULL *d2i_ASN1_NULL(ASN1_NULL **out, const uint8_t **inp, long len); -// i2d_ASN1_NULL marshals |in| as a DER-encoded ASN.1 NULL value, as described -// in |i2d_SAMPLE|. +// i2d_ASN1_NULL marshals `in` as a DER-encoded ASN.1 NULL value, as described +// in `i2d_SAMPLE`. OPENSSL_EXPORT int i2d_ASN1_NULL(const ASN1_NULL *in, uint8_t **outp); -// ASN1_NULL is an |ASN1_ITEM| with ASN.1 type NULL and C type |ASN1_NULL*|. +// ASN1_NULL is an `ASN1_ITEM` with ASN.1 type NULL and C type `ASN1_NULL*`. DECLARE_ASN1_ITEM(ASN1_NULL) // Object identifiers. // -// An |ASN1_OBJECT| represents a ASN.1 OBJECT IDENTIFIER. See also obj.h for -// additional functions relating to |ASN1_OBJECT|. +// An `ASN1_OBJECT` represents an ASN.1 OBJECT IDENTIFIER. See also obj.h for +// additional functions relating to `ASN1_OBJECT`. // // TODO(davidben): What's the relationship between asn1.h and obj.h? Most of -// obj.h deals with the large NID table, but then functions like |OBJ_get0_data| -// or |OBJ_dup| are general |ASN1_OBJECT| functions. +// obj.h deals with the large NID table, but then functions like `OBJ_get0_data` +// or `OBJ_dup` are general `ASN1_OBJECT` functions. DEFINE_STACK_OF(ASN1_OBJECT) -// ASN1_OBJECT_create returns a newly-allocated |ASN1_OBJECT| with |len| bytes -// from |data| as the encoded OID, or NULL on error. |data| should contain the +// ASN1_OBJECT_create returns a newly-allocated `ASN1_OBJECT` with `len` bytes +// from `data` as the encoded OID, or NULL on error. `data` should contain the // DER-encoded identifier, excluding the tag and length. // -// |nid| should be |NID_undef|. Passing a NID value that does not match |data| -// will cause some functions to misbehave. |sn| and |ln| should be NULL. If +// `nid` should be `NID_undef`. Passing a NID value that does not match `data` +// will cause some functions to misbehave. `sn` and `ln` should be NULL. If // non-NULL, they are stored as short and long names, respectively, but these -// values have no effect for |ASN1_OBJECT|s created through this function. +// values have no effect for `ASN1_OBJECT`s created through this function. // // TODO(davidben): Should we just ignore all those parameters? NIDs and names -// are only relevant for |ASN1_OBJECT|s in the obj.h table. +// are only relevant for `ASN1_OBJECT`s in the obj.h table. OPENSSL_EXPORT ASN1_OBJECT *ASN1_OBJECT_create(int nid, const uint8_t *data, size_t len, const char *sn, const char *ln); -// ASN1_OBJECT_free releases memory associated with |a|. If |a| is a static -// |ASN1_OBJECT|, returned from |OBJ_nid2obj|, this function does nothing. +// ASN1_OBJECT_free releases memory associated with `a`. If `a` is a static +// `ASN1_OBJECT`, returned from `OBJ_nid2obj`, this function does nothing. OPENSSL_EXPORT void ASN1_OBJECT_free(ASN1_OBJECT *a); -// d2i_ASN1_OBJECT parses a DER-encoded ASN.1 OBJECT IDENTIFIER from up to |len| -// bytes at |*inp|, as described in |d2i_SAMPLE|. +// d2i_ASN1_OBJECT parses a DER-encoded ASN.1 OBJECT IDENTIFIER from up to `len` +// bytes at `*inp`, as described in `d2i_SAMPLE`. OPENSSL_EXPORT ASN1_OBJECT *d2i_ASN1_OBJECT(ASN1_OBJECT **out, const uint8_t **inp, long len); -// i2d_ASN1_OBJECT marshals |in| as a DER-encoded ASN.1 OBJECT IDENTIFIER, as -// described in |i2d_SAMPLE|. +// i2d_ASN1_OBJECT marshals `in` as a DER-encoded ASN.1 OBJECT IDENTIFIER, as +// described in `i2d_SAMPLE`. OPENSSL_EXPORT int i2d_ASN1_OBJECT(const ASN1_OBJECT *in, uint8_t **outp); -// c2i_ASN1_OBJECT decodes |len| bytes from |*inp| as the contents of a +// c2i_ASN1_OBJECT decodes `len` bytes from `*inp` as the contents of a // DER-encoded OBJECT IDENTIFIER, excluding the tag and length. It behaves like -// |d2i_SAMPLE| except, on success, it always consumes all |len| bytes. +// `d2i_SAMPLE` except, on success, it always consumes all `len` bytes. OPENSSL_EXPORT ASN1_OBJECT *c2i_ASN1_OBJECT(ASN1_OBJECT **out, const uint8_t **inp, long len); -// ASN1_OBJECT is an |ASN1_ITEM| with ASN.1 type OBJECT IDENTIFIER and C type -// |ASN1_OBJECT*|. +// ASN1_OBJECT is an `ASN1_ITEM` with ASN.1 type OBJECT IDENTIFIER and C type +// `ASN1_OBJECT*`. DECLARE_ASN1_ITEM(ASN1_OBJECT) // Arbitrary elements. -// An asn1_type_st (aka |ASN1_TYPE|) represents an arbitrary ASN.1 element, -// typically used for ANY types. It contains a |type| field and a |value| union -// dependent on |type|. +// An asn1_type_st (aka `ASN1_TYPE`) represents an arbitrary ASN.1 element, +// typically used for ANY types. It contains a `type` field and a `value` union +// dependent on `type`. // // WARNING: This struct has a complex representation. Callers must not construct -// |ASN1_TYPE| values manually. Use |ASN1_TYPE_set| and |ASN1_TYPE_set1| +// `ASN1_TYPE` values manually. Use `ASN1_TYPE_set` and `ASN1_TYPE_set1` // instead. Additionally, callers performing non-trivial operations on this type -// are encouraged to use |CBS| and |CBB| from , and -// convert to or from |ASN1_TYPE| with |d2i_ASN1_TYPE| or |i2d_ASN1_TYPE|. +// are encouraged to use `CBS` and `CBB` from , and +// convert to or from `ASN1_TYPE` with `d2i_ASN1_TYPE` or `i2d_ASN1_TYPE`. // -// The |type| field corresponds to the tag of the ASN.1 element being +// The `type` field corresponds to the tag of the ASN.1 element being // represented: // -// If |type| is a |V_ASN1_*| constant for an ASN.1 string-like type, as defined -// by |ASN1_STRING|, the tag matches the constant. |value| contains an -// |ASN1_STRING| pointer (equivalently, one of the more specific typedefs). See -// |ASN1_STRING| for details on the representation. Unlike |ASN1_STRING|, -// |ASN1_TYPE| does not use the |V_ASN1_NEG| flag for negative INTEGER and -// ENUMERATE values. For a negative value, the |ASN1_TYPE|'s |type| will be -// |V_ASN1_INTEGER| or |V_ASN1_ENUMERATED|, but |value| will an |ASN1_STRING| -// whose |type| is |V_ASN1_NEG_INTEGER| or |V_ASN1_NEG_ENUMERATED|. +// If `type` is a `V_ASN1_*` constant for an ASN.1 string-like type, as defined +// by `ASN1_STRING`, the tag matches the constant. `value` contains an +// `ASN1_STRING` pointer (equivalently, one of the more specific typedefs). See +// `ASN1_STRING` for details on the representation. Unlike `ASN1_STRING`, +// `ASN1_TYPE` does not use the `V_ASN1_NEG` flag for negative INTEGER and +// ENUMERATE values. For a negative value, the `ASN1_TYPE`'s `type` will be +// `V_ASN1_INTEGER` or `V_ASN1_ENUMERATED`, but `value` will an `ASN1_STRING` +// whose `type` is `V_ASN1_NEG_INTEGER` or `V_ASN1_NEG_ENUMERATED`. // -// If |type| is |V_ASN1_OBJECT|, the tag is OBJECT IDENTIFIER and |value| -// contains an |ASN1_OBJECT| pointer. +// If `type` is `V_ASN1_OBJECT`, the tag is OBJECT IDENTIFIER and `value` +// contains an `ASN1_OBJECT` pointer. // -// If |type| is |V_ASN1_NULL|, the tag is NULL. |value| contains a NULL pointer. +// If `type` is `V_ASN1_NULL`, the tag is NULL. `value` contains a NULL pointer. // -// If |type| is |V_ASN1_BOOLEAN|, the tag is BOOLEAN. |value| contains an -// |ASN1_BOOLEAN|. +// If `type` is `V_ASN1_BOOLEAN`, the tag is BOOLEAN. `value` contains an +// `ASN1_BOOLEAN`. // -// If |type| is |V_ASN1_SEQUENCE|, |V_ASN1_SET|, or |V_ASN1_OTHER|, the tag is -// SEQUENCE, SET, or some arbitrary tag, respectively. |value| uses the -// corresponding |ASN1_STRING| representation. Although any type may be -// represented in |V_ASN1_OTHER|, the parser will always return the more +// If `type` is `V_ASN1_SEQUENCE`, `V_ASN1_SET`, or `V_ASN1_OTHER`, the tag is +// SEQUENCE, SET, or some arbitrary tag, respectively. `value` uses the +// corresponding `ASN1_STRING` representation. Although any type may be +// represented in `V_ASN1_OTHER`, the parser will always return the more // specific encoding when available. // -// Other values of |type| do not represent a valid ASN.1 value, though -// default-constructed objects may set |type| to -1. Such objects cannot be +// Other values of `type` do not represent a valid ASN.1 value, though +// default-constructed objects may set `type` to -1. Such objects cannot be // serialized. struct asn1_type_st { int type; @@ -1477,18 +1458,18 @@ struct asn1_type_st { DEFINE_STACK_OF(ASN1_TYPE) -// ASN1_TYPE_new returns a newly-allocated |ASN1_TYPE|, or NULL on allocation +// ASN1_TYPE_new returns a newly-allocated `ASN1_TYPE`, or NULL on allocation // failure. The resulting object has type -1 and must be initialized to be // a valid ANY value. OPENSSL_EXPORT ASN1_TYPE *ASN1_TYPE_new(void); -// ASN1_TYPE_free releases memory associated with |a|. +// ASN1_TYPE_free releases memory associated with `a`. OPENSSL_EXPORT void ASN1_TYPE_free(ASN1_TYPE *a); -// d2i_ASN1_TYPE parses up to |len| bytes from |*inp| as an ASN.1 value of any -// type, as described in |d2i_SAMPLE|. Note this function only validates +// d2i_ASN1_TYPE parses up to `len` bytes from `*inp` as an ASN.1 value of any +// type, as described in `d2i_SAMPLE`. Note this function only validates // primitive, universal types supported by this library. Values of type -// |V_ASN1_SEQUENCE|, |V_ASN1_SET|, |V_ASN1_OTHER|, or an unsupported primitive +// `V_ASN1_SEQUENCE`, `V_ASN1_SET`, `V_ASN1_OTHER`, or an unsupported primitive // type must be validated by the caller when interpreting. // // TODO(https://crbug.com/boringssl/354): This function currently also accepts @@ -1496,46 +1477,48 @@ OPENSSL_EXPORT void ASN1_TYPE_free(ASN1_TYPE *a); OPENSSL_EXPORT ASN1_TYPE *d2i_ASN1_TYPE(ASN1_TYPE **out, const uint8_t **inp, long len); -// i2d_ASN1_TYPE marshals |in| as DER, as described in |i2d_SAMPLE|. +// i2d_ASN1_TYPE marshals `in` as DER, as described in `i2d_SAMPLE`. OPENSSL_EXPORT int i2d_ASN1_TYPE(const ASN1_TYPE *in, uint8_t **outp); -// ASN1_ANY is an |ASN1_ITEM| with ASN.1 type ANY and C type |ASN1_TYPE*|. Note -// the |ASN1_ITEM| name and C type do not match. +// ASN1_ANY is an `ASN1_ITEM` with ASN.1 type ANY and C type `ASN1_TYPE*`. Note +// the `ASN1_ITEM` name and C type do not match. DECLARE_ASN1_ITEM(ASN1_ANY) -// ASN1_TYPE_get returns the type of |a|, which will be one of the |V_ASN1_*| -// constants, or zero if |a| is not fully initialized. +// ASN1_TYPE_get returns the type of `a`, which will be one of the `V_ASN1_*` +// constants, or zero if `a` is not fully initialized. OPENSSL_EXPORT int ASN1_TYPE_get(const ASN1_TYPE *a); -// ASN1_TYPE_set sets |a| to an |ASN1_TYPE| of type |type| and value |value|, -// releasing the previous contents of |a|. +// ASN1_TYPE_set sets `a` to an `ASN1_TYPE` of type `type` and value `value`, +// releasing the previous contents of `a`. // -// If |type| is |V_ASN1_BOOLEAN|, |a| is set to FALSE if |value| is NULL and -// TRUE otherwise. If setting |a| to TRUE, |value| may be an invalid pointer, +// If `type` is `V_ASN1_BOOLEAN`, `a` is set to FALSE if `value` is NULL and +// TRUE otherwise. If setting `a` to TRUE, `value` may be an invalid pointer, // such as (void*)1. // -// If |type| is |V_ASN1_NULL|, |value| must be NULL. +// If `type` is `V_ASN1_NULL`, `value` must be NULL. // -// For other values of |type|, this function takes ownership of |value|, which -// must point to an object of the corresponding type. See |ASN1_TYPE| for +// For other values of `type`, this function takes ownership of `value`, which +// must point to an object of the corresponding type. See `ASN1_TYPE` for // details. OPENSSL_EXPORT void ASN1_TYPE_set(ASN1_TYPE *a, int type, void *value); -// ASN1_TYPE_set1 behaves like |ASN1_TYPE_set| except it does not take ownership -// of |value|. It returns one on success and zero on error. +// ASN1_TYPE_set1 behaves like `ASN1_TYPE_set` except it does not take ownership +// of `value`. It returns one on success and zero on error. OPENSSL_EXPORT int ASN1_TYPE_set1(ASN1_TYPE *a, int type, const void *value); -// ASN1_TYPE_cmp returns zero if |a| and |b| are equal and some non-zero value -// otherwise. Note this function can only be used for equality checks, not an -// ordering. +// ASN1_TYPE_cmp compares `a` and `b`. It returns zero if they are equal, and a +// negative or positive value if `a` sorts before or after `b`, respectively. +// NULL sorts before any non-NULL value. Otherwise, values are ordered first by +// type, then by contents. The order is arbitrary but suitable for sorting. +// Callers should not rely on the exact order. OPENSSL_EXPORT int ASN1_TYPE_cmp(const ASN1_TYPE *a, const ASN1_TYPE *b); typedef STACK_OF(ASN1_TYPE) ASN1_SEQUENCE_ANY; -// d2i_ASN1_SEQUENCE_ANY parses up to |len| bytes from |*inp| as a DER-encoded -// ASN.1 SEQUENCE OF ANY structure, as described in |d2i_SAMPLE|. The resulting -// |ASN1_SEQUENCE_ANY| owns its contents and thus must be released with -// |sk_ASN1_TYPE_pop_free| and |ASN1_TYPE_free|, not |sk_ASN1_TYPE_free|. +// d2i_ASN1_SEQUENCE_ANY parses up to `len` bytes from `*inp` as a DER-encoded +// ASN.1 SEQUENCE OF ANY structure, as described in `d2i_SAMPLE`. The resulting +// `ASN1_SEQUENCE_ANY` owns its contents and thus must be released with +// `sk_ASN1_TYPE_pop_free` and `ASN1_TYPE_free`, not `sk_ASN1_TYPE_free`. // // TODO(https://crbug.com/boringssl/354): This function currently also accepts // BER, but this will be removed in the future. @@ -1543,15 +1526,15 @@ OPENSSL_EXPORT ASN1_SEQUENCE_ANY *d2i_ASN1_SEQUENCE_ANY(ASN1_SEQUENCE_ANY **out, const uint8_t **inp, long len); -// i2d_ASN1_SEQUENCE_ANY marshals |in| as a DER-encoded SEQUENCE OF ANY -// structure, as described in |i2d_SAMPLE|. +// i2d_ASN1_SEQUENCE_ANY marshals `in` as a DER-encoded SEQUENCE OF ANY +// structure, as described in `i2d_SAMPLE`. OPENSSL_EXPORT int i2d_ASN1_SEQUENCE_ANY(const ASN1_SEQUENCE_ANY *in, uint8_t **outp); -// d2i_ASN1_SET_ANY parses up to |len| bytes from |*inp| as a DER-encoded ASN.1 -// SET OF ANY structure, as described in |d2i_SAMPLE|. The resulting -// |ASN1_SEQUENCE_ANY| owns its contents and thus must be released with -// |sk_ASN1_TYPE_pop_free| and |ASN1_TYPE_free|, not |sk_ASN1_TYPE_free|. +// d2i_ASN1_SET_ANY parses up to `len` bytes from `*inp` as a DER-encoded ASN.1 +// SET OF ANY structure, as described in `d2i_SAMPLE`. The resulting +// `ASN1_SEQUENCE_ANY` owns its contents and thus must be released with +// `sk_ASN1_TYPE_pop_free` and `ASN1_TYPE_free`, not `sk_ASN1_TYPE_free`. // // TODO(https://crbug.com/boringssl/354): This function currently also accepts // BER, but this will be removed in the future. @@ -1559,8 +1542,8 @@ OPENSSL_EXPORT ASN1_SEQUENCE_ANY *d2i_ASN1_SET_ANY(ASN1_SEQUENCE_ANY **out, const uint8_t **inp, long len); -// i2d_ASN1_SET_ANY marshals |in| as a DER-encoded SET OF ANY structure, as -// described in |i2d_SAMPLE|. +// i2d_ASN1_SET_ANY marshals `in` as a DER-encoded SET OF ANY structure, as +// described in `i2d_SAMPLE`. OPENSSL_EXPORT int i2d_ASN1_SET_ANY(const ASN1_SEQUENCE_ANY *in, uint8_t **outp); @@ -1571,25 +1554,25 @@ OPENSSL_EXPORT int i2d_ASN1_SET_ANY(const ASN1_SEQUENCE_ANY *in, // functions may be used for debugging and logging. However, the output should // not be consumed programmatically. They may be ambiguous or lose information. -// ASN1_UTCTIME_print writes a human-readable representation of |a| to |out|. It +// ASN1_UTCTIME_print writes a human-readable representation of `a` to `out`. It // returns one on success and zero on error. OPENSSL_EXPORT int ASN1_UTCTIME_print(BIO *out, const ASN1_UTCTIME *a); -// ASN1_GENERALIZEDTIME_print writes a human-readable representation of |a| to -// |out|. It returns one on success and zero on error. +// ASN1_GENERALIZEDTIME_print writes a human-readable representation of `a` to +// `out`. It returns one on success and zero on error. OPENSSL_EXPORT int ASN1_GENERALIZEDTIME_print(BIO *out, const ASN1_GENERALIZEDTIME *a); -// ASN1_TIME_print writes a human-readable representation of |a| to |out|. It +// ASN1_TIME_print writes a human-readable representation of `a` to `out`. It // returns one on success and zero on error. OPENSSL_EXPORT int ASN1_TIME_print(BIO *out, const ASN1_TIME *a); -// ASN1_STRING_print writes a human-readable representation of |str| to |out|. +// ASN1_STRING_print writes a human-readable representation of `str` to `out`. // It returns one on success and zero on error. Unprintable characters are // replaced with '.'. OPENSSL_EXPORT int ASN1_STRING_print(BIO *out, const ASN1_STRING *str); -// The following flags must not collide with |XN_FLAG_*|. +// The following flags must not collide with `XN_FLAG_*`. // ASN1_STRFLGS_ESC_2253 causes characters to be escaped as in RFC 2253, section // 2.4. @@ -1610,12 +1593,12 @@ OPENSSL_EXPORT int ASN1_STRING_print(BIO *out, const ASN1_STRING *str); // byte in the UTF-8 encoding treated as an individual character for purposes of // escape sequences. If not set, each Unicode codepoint in the string is treated // as a character, with wide characters escaped as "\Uxxxx" or "\Wxxxxxxxx". -// Note this can be ambiguous if |ASN1_STRFLGS_ESC_*| are all unset. In that +// Note this can be ambiguous if `ASN1_STRFLGS_ESC_*` are all unset. In that // case, backslashes are not escaped, but wide characters are. #define ASN1_STRFLGS_UTF8_CONVERT 0x10ul // ASN1_STRFLGS_IGNORE_TYPE causes the string type to be ignored. The -// |ASN1_STRING| in-memory representation will be printed directly. +// `ASN1_STRING` in-memory representation will be printed directly. #define ASN1_STRFLGS_IGNORE_TYPE 0x20ul // ASN1_STRFLGS_SHOW_TYPE causes the string type to be included in the output. @@ -1625,15 +1608,15 @@ OPENSSL_EXPORT int ASN1_STRING_print(BIO *out, const ASN1_STRING *str); // RFC 2253 hexstring notation, such as "#0123456789ABCDEF". #define ASN1_STRFLGS_DUMP_ALL 0x80ul -// ASN1_STRFLGS_DUMP_UNKNOWN behaves like |ASN1_STRFLGS_DUMP_ALL| but only +// ASN1_STRFLGS_DUMP_UNKNOWN behaves like `ASN1_STRFLGS_DUMP_ALL` but only // applies to values of unknown type. If unset, unknown values will print // their contents as single-byte characters with escape sequences. #define ASN1_STRFLGS_DUMP_UNKNOWN 0x100ul // ASN1_STRFLGS_DUMP_DER causes hexdumped strings (as determined by -// |ASN1_STRFLGS_DUMP_ALL| or |ASN1_STRFLGS_DUMP_UNKNOWN|) to print the entire +// `ASN1_STRFLGS_DUMP_ALL` or `ASN1_STRFLGS_DUMP_UNKNOWN`) to print the entire // DER element as in RFC 2253, rather than only the contents of the -// |ASN1_STRING|. +// `ASN1_STRING`. #define ASN1_STRFLGS_DUMP_DER 0x200ul // ASN1_STRFLGS_RFC2253 causes the string to be escaped as in RFC 2253, @@ -1643,103 +1626,103 @@ OPENSSL_EXPORT int ASN1_STRING_print(BIO *out, const ASN1_STRING *str); ASN1_STRFLGS_UTF8_CONVERT | ASN1_STRFLGS_DUMP_UNKNOWN | \ ASN1_STRFLGS_DUMP_DER) -// ASN1_STRING_print_ex writes a human-readable representation of |str| to -// |out|. It returns the number of bytes written on success and -1 on error. If -// |out| is NULL, it returns the number of bytes it would have written, without +// ASN1_STRING_print_ex writes a human-readable representation of `str` to +// `out`. It returns the number of bytes written on success and -1 on error. If +// `out` is NULL, it returns the number of bytes it would have written, without // writing anything. // -// The |flags| should be a combination of combination of |ASN1_STRFLGS_*| +// The `flags` should be a combination of combination of `ASN1_STRFLGS_*` // constants. See the documentation for each flag for how it controls the -// output. If unsure, use |ASN1_STRFLGS_RFC2253|. +// output. If unsure, use `ASN1_STRFLGS_RFC2253`. OPENSSL_EXPORT int ASN1_STRING_print_ex(BIO *out, const ASN1_STRING *str, unsigned long flags); -// ASN1_STRING_print_ex_fp behaves like |ASN1_STRING_print_ex| but writes to a -// |FILE| rather than a |BIO|. +// ASN1_STRING_print_ex_fp behaves like `ASN1_STRING_print_ex` but writes to a +// `FILE` rather than a `BIO`. OPENSSL_EXPORT int ASN1_STRING_print_ex_fp(FILE *fp, const ASN1_STRING *str, unsigned long flags); -// i2a_ASN1_INTEGER writes a human-readable representation of |a| to |bp|. It +// i2a_ASN1_INTEGER writes a human-readable representation of `a` to `bp`. It // returns the number of bytes written on success, or a negative number on -// error. On error, this function may have written a partial output to |bp|. +// error. On error, this function may have written a partial output to `bp`. OPENSSL_EXPORT int i2a_ASN1_INTEGER(BIO *bp, const ASN1_INTEGER *a); -// i2a_ASN1_ENUMERATED writes a human-readable representation of |a| to |bp|. It +// i2a_ASN1_ENUMERATED writes a human-readable representation of `a` to `bp`. It // returns the number of bytes written on success, or a negative number on -// error. On error, this function may have written a partial output to |bp|. +// error. On error, this function may have written a partial output to `bp`. OPENSSL_EXPORT int i2a_ASN1_ENUMERATED(BIO *bp, const ASN1_ENUMERATED *a); -// i2a_ASN1_OBJECT writes a human-readable representation of |a| to |bp|. It +// i2a_ASN1_OBJECT writes a human-readable representation of `a` to `bp`. It // returns the number of bytes written on success, or a negative number on -// error. On error, this function may have written a partial output to |bp|. +// error. On error, this function may have written a partial output to `bp`. OPENSSL_EXPORT int i2a_ASN1_OBJECT(BIO *bp, const ASN1_OBJECT *a); -// i2a_ASN1_STRING writes a text representation of |a|'s contents to |bp|. It +// i2a_ASN1_STRING writes a text representation of `a`'s contents to `bp`. It // returns the number of bytes written on success, or a negative number on -// error. On error, this function may have written a partial output to |bp|. -// |type| is ignored. +// error. On error, this function may have written a partial output to `bp`. +// `type` is ignored. // -// This function does not decode |a| into a Unicode string. It only hex-encodes -// the internal representation of |a|. This is suitable for printing an OCTET +// This function does not decode `a` into a Unicode string. It only hex-encodes +// the internal representation of `a`. This is suitable for printing an OCTET // STRING, but may not be human-readable for any other string type. OPENSSL_EXPORT int i2a_ASN1_STRING(BIO *bp, const ASN1_STRING *a, int type); -// i2t_ASN1_OBJECT calls |OBJ_obj2txt| with |always_return_oid| set to zero. +// i2t_ASN1_OBJECT calls `OBJ_obj2txt` with `always_return_oid` set to zero. OPENSSL_EXPORT int i2t_ASN1_OBJECT(char *buf, int buf_len, const ASN1_OBJECT *a); // Low-level encoding functions. -// ASN1_get_object parses a BER element from up to |max_len| bytes at |*inp|. It -// returns |V_ASN1_CONSTRUCTED| if it successfully parsed a constructed element, +// ASN1_get_object parses a BER element from up to `max_len` bytes at `*inp`. It +// returns `V_ASN1_CONSTRUCTED` if it successfully parsed a constructed element, // zero if it successfully parsed a primitive element, and 0x80 on error. On -// success, it additionally advances |*inp| to the element body, sets -// |*out_length|, |*out_tag|, and |*out_class| to the element's length, tag +// success, it additionally advances `*inp` to the element body, sets +// `*out_length`, `*out_tag`, and `*out_class` to the element's length, tag // number, and tag class, respectively, // // Unlike OpenSSL, this function only supports DER. Indefinite and non-minimal // lengths are rejected. // -// This function is difficult to use correctly. Use |CBS_get_asn1| and related +// This function is difficult to use correctly. Use `CBS_get_asn1` and related // functions from bytestring.h. OPENSSL_EXPORT int ASN1_get_object(const unsigned char **inp, long *out_length, int *out_tag, int *out_class, long max_len); -// ASN1_put_object writes the header for a DER or BER element to |*outp| and -// advances |*outp| by the number of bytes written. The caller is responsible -// for ensuring |*outp| has enough space for the output. The header describes an -// element with length |length|, tag number |tag|, and class |xclass|. |xclass| -// should be one of the |V_ASN1_*| tag class constants. The element is primitive -// if |constructed| is zero and constructed if it is one or two. If -// |constructed| is two, |length| is ignored and the element uses +// ASN1_put_object writes the header for a DER or BER element to `*outp` and +// advances `*outp` by the number of bytes written. The caller is responsible +// for ensuring `*outp` has enough space for the output. The header describes an +// element with length `length`, tag number `tag`, and class `xclass`. `xclass` +// should be one of the `V_ASN1_*` tag class constants. The element is primitive +// if `constructed` is zero and constructed if it is one or two. If +// `constructed` is two, `length` is ignored and the element uses // indefinite-length encoding. // -// Use |CBB_add_asn1| instead. +// Use `CBB_add_asn1` instead. OPENSSL_EXPORT void ASN1_put_object(unsigned char **outp, int constructed, int length, int tag, int xclass); -// ASN1_put_eoc writes two zero bytes to |*outp|, advances |*outp| to point past +// ASN1_put_eoc writes two zero bytes to `*outp`, advances `*outp` to point past // those bytes, and returns two. // // Use definite-length encoding instead. OPENSSL_EXPORT int ASN1_put_eoc(unsigned char **outp); // ASN1_object_size returns the number of bytes needed to encode a DER or BER -// value with length |length| and tag number |tag|, or -1 on error. |tag| should -// not include the constructed bit or tag class. If |constructed| is zero or +// value with length `length` and tag number `tag`, or -1 on error. `tag` should +// not include the constructed bit or tag class. If `constructed` is zero or // one, the result uses a definite-length encoding with minimally-encoded -// length, as in DER. If |constructed| is two, the result uses BER +// length, as in DER. If `constructed` is two, the result uses BER // indefinite-length encoding. // -// Use |CBB_add_asn1| instead. +// Use `CBB_add_asn1` instead. OPENSSL_EXPORT int ASN1_object_size(int constructed, int length, int tag); // Function declaration macros. // // The following macros declare functions for ASN.1 types. Prefer writing the -// prototypes directly. Particularly when |type|, |itname|, or |name| differ, +// prototypes directly. Particularly when `type`, `itname`, or `name` differ, // the macros can be difficult to understand. #define DECLARE_ASN1_FUNCTIONS(type) DECLARE_ASN1_FUNCTIONS_name(type, type) @@ -1782,21 +1765,21 @@ OPENSSL_EXPORT int ASN1_object_size(int constructed, int length, int tag); // lacked this flag, it enabled an implicit truncation behavior. This has since // been removed. // -// TODO(crbug.com/443769299): Remove this when |ASN1_STRING| is opaque. For now, -// we continue to set it in various codepaths, in case code is querying |flags| +// TODO(crbug.com/443769299): Remove this when `ASN1_STRING` is opaque. For now, +// we continue to set it in various codepaths, in case code is querying `flags` // manually, even though it does nothing. #define ASN1_STRING_FLAG_BITS_LEFT 0x08 -// ASN1_BIT_STRING_num_bytes computes the length of |str| in bytes. If |str|'s -// bit length is a multiple of 8, it sets |*out| to the byte length and returns +// ASN1_BIT_STRING_num_bytes computes the length of `str` in bytes. If `str`'s +// bit length is a multiple of 8, it sets `*out` to the byte length and returns // one. Otherwise, it returns zero. // -// This function may be used with |ASN1_STRING_get0_data| to interpret |str| as +// This function may be used with `ASN1_STRING_get0_data` to interpret `str` as // a byte string. // // This function is no longer necessary. The byte length is always equal to -// |ASN1_STRING_length| and callers can check for a whole number of bytes by -// checking if |ASN1_BIT_STRING_unused_bits| is zero. +// `ASN1_STRING_length` and callers can check for a whole number of bytes by +// checking if `ASN1_BIT_STRING_unused_bits` is zero. OPENSSL_EXPORT int ASN1_BIT_STRING_num_bytes(const ASN1_BIT_STRING *str, size_t *out); @@ -1806,13 +1789,32 @@ OPENSSL_EXPORT void ASN1_STRING_set_default_mask(unsigned long mask); // ASN1_STRING_set_default_mask_asc returns one. OPENSSL_EXPORT int ASN1_STRING_set_default_mask_asc(const char *p); -// ASN1_STRING_get_default_mask returns |B_ASN1_UTF8STRING|. +// ASN1_STRING_get_default_mask returns `B_ASN1_UTF8STRING`. OPENSSL_EXPORT unsigned long ASN1_STRING_get_default_mask(void); // ASN1_STRING_TABLE_cleanup does nothing. OPENSSL_EXPORT void ASN1_STRING_TABLE_cleanup(void); -// M_ASN1_* are legacy aliases for various |ASN1_STRING| functions. Use the +// ASN1_STRING_data returns a mutable pointer to `str`'s contents. Callers +// should use `ASN1_STRING_length` to determine the length of the string. The +// string may have embedded NUL bytes and may not be NUL-terminated. +// +// The contents of an `ASN1_STRING` encode the value in some type-specific +// representation that does not always correspond to the DER encoding of the +// type. See the documentation for `ASN1_STRING` for details. +// +// Prefer `ASN1_STRING_get0_data`. See crbug.com/574019297. +OPENSSL_EXPORT unsigned char *ASN1_STRING_data(ASN1_STRING *str); + +// ASN1_STRING_length returns the length of `str`, in bytes. Prefer +// `ASN1_STRING_get_length`. +// +// The contents of an `ASN1_STRING` encode the value in some type-specific +// representation that does not always correspond to the DER encoding of the +// type. See the documentation for `ASN1_STRING` for details. +OPENSSL_EXPORT int ASN1_STRING_length(const ASN1_STRING *str); + +// M_ASN1_* are legacy aliases for various `ASN1_STRING` functions. Use the // functions themselves. #define M_ASN1_STRING_length(x) ASN1_STRING_length(x) #define M_ASN1_STRING_type(x) ASN1_STRING_type(x) @@ -1860,30 +1862,30 @@ OPENSSL_EXPORT void ASN1_STRING_TABLE_cleanup(void); #define M_ASN1_UTF8STRING_new() ASN1_UTF8STRING_new() #define M_ASN1_UTF8STRING_free(a) ASN1_UTF8STRING_free(a) -// ASN1_INTEGER_set sets |a| to an INTEGER with value |v|. It returns one on +// ASN1_INTEGER_set sets `a` to an INTEGER with value `v`. It returns one on // success and zero on error. // -// Use |ASN1_INTEGER_set_uint64| and |ASN1_INTEGER_set_int64| instead. +// Use `ASN1_INTEGER_set_uint64` and `ASN1_INTEGER_set_int64` instead. OPENSSL_EXPORT int ASN1_INTEGER_set(ASN1_INTEGER *a, long v); -// ASN1_ENUMERATED_set sets |a| to an ENUMERATED with value |v|. It returns one +// ASN1_ENUMERATED_set sets `a` to an ENUMERATED with value `v`. It returns one // on success and zero on error. // -// Use |ASN1_ENUMERATED_set_uint64| and |ASN1_ENUMERATED_set_int64| instead. +// Use `ASN1_ENUMERATED_set_uint64` and `ASN1_ENUMERATED_set_int64` instead. OPENSSL_EXPORT int ASN1_ENUMERATED_set(ASN1_ENUMERATED *a, long v); -// ASN1_INTEGER_get returns the value of |a| as a |long|, or -1 if |a| is out of +// ASN1_INTEGER_get returns the value of `a` as a `long`, or -1 if `a` is out of // range or the wrong type. // // WARNING: This function's return value cannot distinguish errors from -1. -// Use |ASN1_INTEGER_get_uint64| and |ASN1_INTEGER_get_int64| instead. +// Use `ASN1_INTEGER_get_uint64` and `ASN1_INTEGER_get_int64` instead. OPENSSL_EXPORT long ASN1_INTEGER_get(const ASN1_INTEGER *a); -// ASN1_ENUMERATED_get returns the value of |a| as a |long|, or -1 if |a| is out +// ASN1_ENUMERATED_get returns the value of `a` as a `long`, or -1 if `a` is out // of range or the wrong type. // // WARNING: This function's return value cannot distinguish errors from -1. -// Use |ASN1_ENUMERATED_get_uint64| and |ASN1_ENUMERATED_get_int64| instead. +// Use `ASN1_ENUMERATED_get_uint64` and `ASN1_ENUMERATED_get_int64` instead. OPENSSL_EXPORT long ASN1_ENUMERATED_get(const ASN1_ENUMERATED *a); diff --git a/third_party/boringssl/src/include/openssl/asn1t.h b/third_party/boringssl/src/include/openssl/asn1t.h index 22b1688f4..b89adb8b4 100644 --- a/third_party/boringssl/src/include/openssl/asn1t.h +++ b/third_party/boringssl/src/include/openssl/asn1t.h @@ -26,8 +26,8 @@ extern "C" { /* Legacy ASN.1 library template definitions. * * This header is used to define new types in OpenSSL's ASN.1 implementation. It - * is deprecated and will be unexported from the library. Use the new |CBS| and - * |CBB| library in instead. */ + * is deprecated and will be unexported from the library. Use the new `CBS` and + * `CBB` library in instead. */ typedef struct ASN1_TEMPLATE_st ASN1_TEMPLATE; diff --git a/third_party/boringssl/src/include/openssl/base.h b/third_party/boringssl/src/include/openssl/base.h index 961e542a0..b307829c7 100644 --- a/third_party/boringssl/src/include/openssl/base.h +++ b/third_party/boringssl/src/include/openssl/base.h @@ -51,7 +51,7 @@ extern "C" { #if defined(__APPLE__) -// Note |TARGET_OS_MAC| is set for all Apple OS variants. |TARGET_OS_OSX| +// Note `TARGET_OS_MAC` is set for all Apple OS variants. `TARGET_OS_OSX` // targets macOS specifically. #if defined(TARGET_OS_OSX) && TARGET_OS_OSX #define OPENSSL_MACOS @@ -73,7 +73,7 @@ extern "C" { // A consumer may use this symbol in the preprocessor to temporarily build // against multiple revisions of BoringSSL at the same time. It is not // recommended to do so for longer than is necessary. -#define BORINGSSL_API_VERSION 40 +#define BORINGSSL_API_VERSION 44 #if defined(BORINGSSL_SHARED_LIBRARY) @@ -182,7 +182,7 @@ extern "C" { #if defined(BORINGSSL_ALWAYS_USE_STATIC_INLINE) // Add OPENSSL_UNUSED so that, should an inline function be emitted via macro -// (e.g. a |STACK_OF(T)| implementation) in a source file without tripping +// (e.g. a `STACK_OF(T)` implementation) in a source file without tripping // clang's -Wunused-function. #define OPENSSL_INLINE static inline OPENSSL_UNUSED #else @@ -208,23 +208,23 @@ enum ssl_verify_result_t BORINGSSL_ENUM_INT; #endif // ossl_ssize_t is a signed type which is large enough to fit the size of any -// valid memory allocation. We prefer using |size_t|, but sometimes we need a +// valid memory allocation. We prefer using `size_t`, but sometimes we need a // signed type for OpenSSL API compatibility. This type can be used in such // cases to avoid overflow. // -// Not all |size_t| values fit in |ossl_ssize_t|, but all |size_t| values that +// Not all `size_t` values fit in `ossl_ssize_t`, but all `size_t` values that // are sizes of or indices into C objects, can be converted without overflow. typedef ptrdiff_t ossl_ssize_t; -// CBS_ASN1_TAG is the type used by |CBS| and |CBB| for ASN.1 tags. See that +// CBS_ASN1_TAG is the type used by `CBS` and `CBB` for ASN.1 tags. See that // header for details. This type is defined in base.h as a forward declaration. typedef uint32_t CBS_ASN1_TAG; // CRYPTO_THREADID is a dummy value. typedef int CRYPTO_THREADID; -// An |ASN1_NULL| is an opaque type. asn1.h represents the ASN.1 NULL value as -// an opaque, non-NULL |ASN1_NULL*| pointer. +// An `ASN1_NULL` is an opaque type. asn1.h represents the ASN.1 NULL value as +// an opaque, non-NULL `ASN1_NULL*` pointer. typedef struct asn1_null_st ASN1_NULL; // CRYPTO_MUST_BE_NULL is an opaque type that is never returned from BoringSSL. @@ -287,6 +287,7 @@ typedef struct cbs_st CBS; typedef struct cmac_ctx_st CMAC_CTX; typedef struct conf_st CONF; typedef struct conf_value_st CONF_VALUE; +typedef struct cpace_ctx_st CPACE_CTX; typedef struct crypto_buffer_pool_st CRYPTO_BUFFER_POOL; typedef struct crypto_buffer_st CRYPTO_BUFFER; typedef struct crypto_ivec_st CRYPTO_IVEC; @@ -365,7 +366,7 @@ typedef struct x509_store_st X509_STORE; typedef void *OPENSSL_BLOCK; -// BSSL_CHECK aborts if |condition| is not true. +// BSSL_CHECK aborts if `condition` is not true. #define BSSL_CHECK(condition) \ do { \ if (!(condition)) { \ @@ -379,6 +380,7 @@ typedef void *OPENSSL_BLOCK; #define BORINGSSL_NO_CXX #endif +// clang-format off #if defined(BORINGSSL_PREFIX) #define BSSL_NAMESPACE_BEGIN \ namespace bssl { \ @@ -390,11 +392,15 @@ typedef void *OPENSSL_BLOCK; #define BSSL_NAMESPACE_BEGIN namespace bssl { #define BSSL_NAMESPACE_END } #endif +// clang-format on #if defined(BORINGSSL_NO_CXX) #define BORINGSSL_MAKE_DELETER(type, deleter) -#define BORINGSSL_MAKE_UP_REF(type, up_ref_func) +#define BORINGSSL_MAKE_UP_REF(type, dup_ref_func) +#define BORINGSSL_MAKE_STACK_TRAITS(type, init_func, cleanup_func) +#define BORINGSSL_MAKE_STACK_TRAITS_MOVABLE(type, init_func, cleanup_func, \ + move_func) #else @@ -427,12 +433,14 @@ struct Deleter { } }; -template +template +struct StackAllocatedTraits {}; + +template > class StackAllocated { public: - StackAllocated() { init(&ctx_); } - ~StackAllocated() { cleanup(&ctx_); } + StackAllocated() { Traits::Init(&ctx_); } + ~StackAllocated() { Traits::Cleanup(&ctx_); } StackAllocated(const StackAllocated &) = delete; StackAllocated &operator=(const StackAllocated &) = delete; @@ -444,27 +452,29 @@ class StackAllocated { const T *operator->() const { return &ctx_; } void Reset() { - cleanup(&ctx_); - init(&ctx_); + Traits::Cleanup(&ctx_); + Traits::Init(&ctx_); } private: T ctx_; }; -template +template +struct StackAllocatedMovableTraits {}; + +template > class StackAllocatedMovable { public: - StackAllocatedMovable() { init(&ctx_); } - ~StackAllocatedMovable() { cleanup(&ctx_); } + StackAllocatedMovable() { Traits::Init(&ctx_); } + ~StackAllocatedMovable() { Traits::Cleanup(&ctx_); } StackAllocatedMovable(StackAllocatedMovable &&other) { - init(&ctx_); - move(&ctx_, &other.ctx_); + Traits::Init(&ctx_); + Traits::Move(&ctx_, &other.ctx_); } StackAllocatedMovable &operator=(StackAllocatedMovable &&other) { - move(&ctx_, &other.ctx_); + Traits::Move(&ctx_, &other.ctx_); return *this; } @@ -475,8 +485,8 @@ class StackAllocatedMovable { const T *operator->() const { return &ctx_; } void Reset() { - cleanup(&ctx_); - init(&ctx_); + Traits::Cleanup(&ctx_); + Traits::Init(&ctx_); } private: @@ -487,10 +497,36 @@ class StackAllocatedMovable { #define BORINGSSL_MAKE_DELETER(type, deleter) \ namespace internal { \ + \ template <> \ struct DeleterImpl { \ static void Free(type *ptr) { deleter(ptr); } \ }; \ + \ + } + +#define BORINGSSL_MAKE_STACK_TRAITS(type, init_func, cleanup_func) \ + namespace internal { \ + \ + template <> \ + struct StackAllocatedTraits { \ + static void Init(type *ptr) { init_func(ptr); } \ + static void Cleanup(type *ptr) { cleanup_func(ptr); } \ + }; \ + \ + } + +#define BORINGSSL_MAKE_STACK_TRAITS_MOVABLE(type, init_func, cleanup_func, \ + move_func) \ + namespace internal { \ + \ + template <> \ + struct StackAllocatedMovableTraits { \ + static void Init(type *ptr) { init_func(ptr); } \ + static void Cleanup(type *ptr) { cleanup_func(ptr); } \ + static void Move(type *out, type *in) { move_func(out, in); } \ + }; \ + \ } // Holds ownership of heap-allocated BoringSSL structures. Sample usage: @@ -499,12 +535,9 @@ class StackAllocatedMovable { template using UniquePtr = std::unique_ptr; -#define BORINGSSL_MAKE_UP_REF(type, up_ref_func) \ - inline UniquePtr UpRef(type *v) { \ - if (v != nullptr) { \ - up_ref_func(v); \ - } \ - return UniquePtr(v); \ +#define BORINGSSL_MAKE_UP_REF(type, dup_ref_func) \ + inline UniquePtr UpRef(const type *v) { \ + return UniquePtr(dup_ref_func(v)); \ } \ \ inline UniquePtr UpRef(const UniquePtr &ptr) { \ diff --git a/third_party/boringssl/src/include/openssl/base64.h b/third_party/boringssl/src/include/openssl/base64.h index ca265cd8e..752cb93ea 100644 --- a/third_party/boringssl/src/include/openssl/base64.h +++ b/third_party/boringssl/src/include/openssl/base64.h @@ -33,29 +33,29 @@ extern "C" { // Encoding -// EVP_EncodeBlock encodes |src_len| bytes from |src| and writes the -// result to |dst| with a trailing NUL. It returns the number of bytes +// EVP_EncodeBlock encodes `src_len` bytes from `src` and writes the +// result to `dst` with a trailing NUL. It returns the number of bytes // written, not including this trailing NUL. OPENSSL_EXPORT size_t EVP_EncodeBlock(uint8_t *dst, const uint8_t *src, size_t src_len); -// EVP_EncodedLength sets |*out_len| to the number of bytes that will be needed -// to call |EVP_EncodeBlock| on an input of length |len|. This includes the -// final NUL that |EVP_EncodeBlock| writes. It returns one on success or zero +// EVP_EncodedLength sets `*out_len` to the number of bytes that will be needed +// to call `EVP_EncodeBlock` on an input of length `len`. This includes the +// final NUL that `EVP_EncodeBlock` writes. It returns one on success or zero // on error. OPENSSL_EXPORT int EVP_EncodedLength(size_t *out_len, size_t len); // Decoding -// EVP_DecodedLength sets |*out_len| to the maximum number of bytes that will -// be needed to call |EVP_DecodeBase64| on an input of length |len|. It returns -// one on success or zero if |len| is not a valid length for a base64-encoded +// EVP_DecodedLength sets `*out_len` to the maximum number of bytes that will +// be needed to call `EVP_DecodeBase64` on an input of length `len`. It returns +// one on success or zero if `len` is not a valid length for a base64-encoded // string. OPENSSL_EXPORT int EVP_DecodedLength(size_t *out_len, size_t len); -// EVP_DecodeBase64 decodes |in_len| bytes from base64 and writes -// |*out_len| bytes to |out|. |max_out| is the size of the output +// EVP_DecodeBase64 decodes `in_len` bytes from base64 and writes +// `*out_len` bytes to `out`. `max_out` is the size of the output // buffer. If it is not enough for the maximum output size, the // operation fails. It returns one on success or zero on error. OPENSSL_EXPORT int EVP_DecodeBase64(uint8_t *out, size_t *out_len, @@ -69,15 +69,15 @@ OPENSSL_EXPORT int EVP_DecodeBase64(uint8_t *out, size_t *out_len, // very specific to PEM. It is also very lenient of invalid input. Use of any of // these functions is thus deprecated. -// EVP_ENCODE_CTX_new returns a newly-allocated |EVP_ENCODE_CTX| or NULL on -// error. The caller must release the result with |EVP_ENCODE_CTX_free| when +// EVP_ENCODE_CTX_new returns a newly-allocated `EVP_ENCODE_CTX` or NULL on +// error. The caller must release the result with `EVP_ENCODE_CTX_free` when // done. OPENSSL_EXPORT EVP_ENCODE_CTX *EVP_ENCODE_CTX_new(void); -// EVP_ENCODE_CTX_free releases memory associated with |ctx|. +// EVP_ENCODE_CTX_free releases memory associated with `ctx`. OPENSSL_EXPORT void EVP_ENCODE_CTX_free(EVP_ENCODE_CTX *ctx); -// EVP_EncodeInit initialises |*ctx|, which is typically stack +// EVP_EncodeInit initialises `*ctx`, which is typically stack // allocated, for an encoding operation. // // NOTE: The encoding operation breaks its output with newlines every @@ -85,29 +85,29 @@ OPENSSL_EXPORT void EVP_ENCODE_CTX_free(EVP_ENCODE_CTX *ctx); // EVP_EncodeBlock to encode raw base64. OPENSSL_EXPORT void EVP_EncodeInit(EVP_ENCODE_CTX *ctx); -// EVP_EncodeUpdate encodes |in_len| bytes from |in| and writes an encoded -// version of them to |out| and sets |*out_len| to the number of bytes written. -// Some state may be contained in |ctx| so |EVP_EncodeFinal| must be used to +// EVP_EncodeUpdate encodes `in_len` bytes from `in` and writes an encoded +// version of them to `out` and sets `*out_len` to the number of bytes written. +// Some state may be contained in `ctx` so `EVP_EncodeFinal` must be used to // flush it before using the encoded data. OPENSSL_EXPORT void EVP_EncodeUpdate(EVP_ENCODE_CTX *ctx, uint8_t *out, int *out_len, const uint8_t *in, size_t in_len); -// EVP_EncodeFinal flushes any remaining output bytes from |ctx| to |out| and -// sets |*out_len| to the number of bytes written. +// EVP_EncodeFinal flushes any remaining output bytes from `ctx` to `out` and +// sets `*out_len` to the number of bytes written. OPENSSL_EXPORT void EVP_EncodeFinal(EVP_ENCODE_CTX *ctx, uint8_t *out, int *out_len); -// EVP_DecodeInit initialises |*ctx|, which is typically stack allocated, for +// EVP_DecodeInit initialises `*ctx`, which is typically stack allocated, for // a decoding operation. // // TODO(davidben): This isn't a straight-up base64 decode either. Document // and/or fix exactly what's going on here; maximum line length and such. OPENSSL_EXPORT void EVP_DecodeInit(EVP_ENCODE_CTX *ctx); -// EVP_DecodeUpdate decodes |in_len| bytes from |in| and writes the decoded -// data to |out| and sets |*out_len| to the number of bytes written. Some state -// may be contained in |ctx| so |EVP_DecodeFinal| must be used to flush it +// EVP_DecodeUpdate decodes `in_len` bytes from `in` and writes the decoded +// data to `out` and sets `*out_len` to the number of bytes written. Some state +// may be contained in `ctx` so `EVP_DecodeFinal` must be used to flush it // before using the encoded data. // // It returns -1 on error, one if a full line of input was processed and zero @@ -116,14 +116,14 @@ OPENSSL_EXPORT int EVP_DecodeUpdate(EVP_ENCODE_CTX *ctx, uint8_t *out, int *out_len, const uint8_t *in, size_t in_len); -// EVP_DecodeFinal flushes any remaining output bytes from |ctx| to |out| and -// sets |*out_len| to the number of bytes written. It returns one on success +// EVP_DecodeFinal flushes any remaining output bytes from `ctx` to `out` and +// sets `*out_len` to the number of bytes written. It returns one on success // and minus one on error. OPENSSL_EXPORT int EVP_DecodeFinal(EVP_ENCODE_CTX *ctx, uint8_t *out, int *out_len); -// EVP_DecodeBlock encodes |src_len| bytes from |src| and writes the result to -// |dst|. It returns the number of bytes written or -1 on error. +// EVP_DecodeBlock encodes `src_len` bytes from `src` and writes the result to +// `dst`. It returns the number of bytes written or -1 on error. // // WARNING: EVP_DecodeBlock's return value does not take padding into // account. It also strips leading whitespace and trailing @@ -133,9 +133,9 @@ OPENSSL_EXPORT int EVP_DecodeBlock(uint8_t *dst, const uint8_t *src, struct evp_encode_ctx_st { - // data_used indicates the number of bytes of |data| that are valid. When - // encoding, |data| will be filled and encoded as a lump. When decoding, only - // the first four bytes of |data| will be used. + // data_used indicates the number of bytes of `data` that are valid. When + // encoding, `data` will be filled and encoded as a lump. When decoding, only + // the first four bytes of `data` will be used. unsigned data_used; uint8_t data[48]; diff --git a/third_party/boringssl/src/include/openssl/bio.h b/third_party/boringssl/src/include/openssl/bio.h index 5c3dda0c3..48ee4aeeb 100644 --- a/third_party/boringssl/src/include/openssl/bio.h +++ b/third_party/boringssl/src/include/openssl/bio.h @@ -15,7 +15,7 @@ #ifndef OPENSSL_HEADER_BIO_H #define OPENSSL_HEADER_BIO_H -#include // IWYU pragma: export +#include // IWYU pragma: export #include // For FILE @@ -37,88 +37,105 @@ extern "C" { DEFINE_STACK_OF(BIO) // BIO_new creates a new BIO with the given method and a reference count of one. -// It returns the fresh |BIO|, or NULL on error. +// It returns the fresh `BIO`, or NULL on error. OPENSSL_EXPORT BIO *BIO_new(const BIO_METHOD *method); -// BIO_free decrements the reference count of |bio|. If the reference count +// BIO_free decrements the reference count of `bio`. If the reference count // drops to zero, it calls the destroy callback, if present, on the method and -// frees |bio| itself. If |bio| is part of a chain (see |BIO_push|), this will -// also free the next |BIO| in the chain, and so on. +// frees `bio` itself. If `bio` is part of a chain (see `BIO_push`), this will +// also free the next `BIO` in the chain, and so on. // -// It returns one if |bio| was NULL or freed. It returns zero if |bio| was +// It returns one if `bio` was NULL or freed. It returns zero if `bio` was // shared and some other owner still owns a reference count to it. // // WARNING: Do not use the return value. Returning zero is not a sign of an -// error, nor an indication to retry the operation. |BIO| is a reference-counted -// type. A given |BIO| object may be shared between multiple parts of an +// error, nor an indication to retry the operation. `BIO` is a reference-counted +// type. A given `BIO` object may be shared between multiple parts of an // application. To correctly track the reference count, without leaks or // use-after-free, each part of the application must release only the reference // counts it owns. OPENSSL_EXPORT int BIO_free(BIO *bio); -// BIO_vfree performs the same actions as |BIO_free|, but has a void return +// BIO_vfree performs the same actions as `BIO_free`, but has a void return // value. This is provided for API-compat. // // TODO(fork): remove. OPENSSL_EXPORT void BIO_vfree(BIO *bio); -// BIO_up_ref increments the reference count of |bio| and returns one. +// BIO_up_ref increments the reference count of `bio` and returns one. OPENSSL_EXPORT int BIO_up_ref(BIO *bio); +// BIO_dup_ref increments the reference count of `bio` and returns the same +// handle. +OPENSSL_EXPORT BIO *BIO_dup_ref(const BIO *bio); + // Basic I/O. -// BIO_read attempts to read |len| bytes into |data|. It returns the number of -// bytes read, zero on EOF, or a negative number on error. +// BIO_read attempts to read `len` bytes into `data`. It returns the number of +// bytes read, zero on EOF, or a negative number on error. Callers that support +// non-blocking I/O should call `BIO_should_read` on error to check if the error +// is fatal or retryable. OPENSSL_EXPORT int BIO_read(BIO *bio, void *data, int len); -// BIO_gets reads a line from |bio| and writes at most |size| bytes into |buf|. +// BIO_gets reads a line from `bio` and writes at most `size` bytes into `buf`. // It returns the number of bytes read or a negative number on error. This // function's output always includes a trailing NUL byte, so it will read at -// most |size - 1| bytes. +// most `size - 1` bytes. // // If the function read a complete line, the output will include the newline -// character, '\n'. If no newline was found before |size - 1| bytes or EOF, it +// character, '\n'. If no newline was found before `size - 1` bytes or EOF, it // outputs the bytes which were available. OPENSSL_EXPORT int BIO_gets(BIO *bio, char *buf, int size); -// BIO_write writes |len| bytes from |data| to |bio|. It returns the number of -// bytes written or a negative number on error. +// BIO_write_ex writes `len` bytes from `data` to `bio`. On success, it returns +// one and sets `*out_written` to the number of bytes written. Otherwise, it +// returns zero. `out_written` may be NULL to ignore the value. Callers that +// support non-blocking I/O should call `BIO_should_write` on error to check if +// the error is fatal or retryable. +OPENSSL_EXPORT int BIO_write_ex(BIO *bio, const void *data, size_t len, + size_t *out_written); + +// BIO_write writes `len` bytes from `data` to `bio`. It returns the number of +// bytes written or a negative number on error. Callers that support +// non-blocking I/O should call `BIO_should_write` on error to check if the +// error is fatal or retryable. OPENSSL_EXPORT int BIO_write(BIO *bio, const void *data, int len); -// BIO_write_all writes |len| bytes from |data| to |bio|, looping as necessary. +// BIO_write_all writes `len` bytes from `data` to `bio`, looping as necessary. // It returns one if all bytes were successfully written and zero on error. OPENSSL_EXPORT int BIO_write_all(BIO *bio, const void *data, size_t len); -// BIO_puts writes a NUL terminated string from |buf| to |bio|. It returns the +// BIO_puts writes a NUL terminated string from `buf` to `bio`. It returns the // number of bytes written or a negative number on error. OPENSSL_EXPORT int BIO_puts(BIO *bio, const char *buf); -// BIO_flush flushes any buffered output. It returns one on success and zero -// otherwise. +// BIO_flush flushes any buffered output. It returns one on success and <= 0 on +// error. Callers that support non-blocking I/O should call `BIO_should_write` +// on error to check if the error is fatal or retryable. OPENSSL_EXPORT int BIO_flush(BIO *bio); // Low-level control functions. // // These are generic functions for sending control requests to a BIO. In -// general one should use the wrapper functions like |BIO_get_close|. +// general one should use the wrapper functions like `BIO_get_close`. -// BIO_ctrl sends the control request |cmd| to |bio|. The |cmd| argument should -// be one of the |BIO_C_*| values. +// BIO_ctrl sends the control request `cmd` to `bio`. The `cmd` argument should +// be one of the `BIO_C_*` values. OPENSSL_EXPORT long BIO_ctrl(BIO *bio, int cmd, long larg, void *parg); -// BIO_ptr_ctrl acts like |BIO_ctrl| but passes the address of a |void*| -// pointer as |parg| and returns the value that is written to it, or NULL if +// BIO_ptr_ctrl acts like `BIO_ctrl` but passes the address of a `void*` +// pointer as `parg` and returns the value that is written to it, or NULL if // the control request returns <= 0. OPENSSL_EXPORT char *BIO_ptr_ctrl(BIO *bp, int cmd, long larg); -// BIO_int_ctrl acts like |BIO_ctrl| but passes the address of a copy of |iarg| -// as |parg|. +// BIO_int_ctrl acts like `BIO_ctrl` but passes the address of a copy of `iarg` +// as `parg`. OPENSSL_EXPORT long BIO_int_ctrl(BIO *bp, int cmd, long larg, int iarg); -// BIO_reset resets |bio| to its initial state, the precise meaning of which -// depends on the concrete type of |bio|. It normally returns one on success and +// BIO_reset resets `bio` to its initial state, the precise meaning of which +// depends on the concrete type of `bio`. It normally returns one on success and // <= 0 otherwise. However, for file and fd BIOs, it returns zero on success and // a negative number on error. // @@ -126,29 +143,29 @@ OPENSSL_EXPORT long BIO_int_ctrl(BIO *bp, int cmd, long larg, int iarg); // in this library. OPENSSL_EXPORT int BIO_reset(BIO *bio); -// BIO_eof returns non-zero when |bio| has reached end-of-file. The precise -// meaning of which depends on the concrete type of |bio|. Note that in the +// BIO_eof returns non-zero when `bio` has reached end-of-file. The precise +// meaning of which depends on the concrete type of `bio`. Note that in the // case of BIO_pair this always returns non-zero. OPENSSL_EXPORT int BIO_eof(BIO *bio); -// BIO_set_flags ORs |flags| with |bio->flags|. Unless otherwise documented, -// flags are private to either BoringSSL or the custom |BIO_METHOD|. +// BIO_set_flags ORs `flags` with `bio->flags`. Unless otherwise documented, +// flags are private to either BoringSSL or the custom `BIO_METHOD`. OPENSSL_EXPORT void BIO_set_flags(BIO *bio, int flags); -// BIO_clear_flags ANDs |bio->flags| with the bitwise-complement of |flags|. +// BIO_clear_flags ANDs `bio->flags` with the bitwise-complement of `flags`. // Unless otherwise documented, flags are private to either BoringSSL or the -// custom |BIO_METHOD|. +// custom `BIO_METHOD`. OPENSSL_EXPORT void BIO_clear_flags(BIO *bio, int flags); -// BIO_test_flags returns |bio->flags| AND |flags|. +// BIO_test_flags returns `bio->flags` AND `flags`. OPENSSL_EXPORT int BIO_test_flags(const BIO *bio, int flags); -// BIO_should_read returns non-zero if |bio| encountered a temporary error +// BIO_should_read returns non-zero if `bio` encountered a temporary error // while reading (i.e. EAGAIN), indicating that the caller should retry the // read. OPENSSL_EXPORT int BIO_should_read(const BIO *bio); -// BIO_should_write returns non-zero if |bio| encountered a temporary error +// BIO_should_write returns non-zero if `bio` encountered a temporary error // while writing (i.e. EAGAIN), indicating that the caller should retry the // write. OPENSSL_EXPORT int BIO_should_write(const BIO *bio); @@ -158,10 +175,10 @@ OPENSSL_EXPORT int BIO_should_write(const BIO *bio); // it was a permanent error and it returns zero. OPENSSL_EXPORT int BIO_should_retry(const BIO *bio); -// BIO_should_io_special returns non-zero if |bio| encountered a temporary +// BIO_should_io_special returns non-zero if `bio` encountered a temporary // error while performing a special I/O operation, indicating that the caller // should retry. The operation that caused the error is returned by -// |BIO_get_retry_reason|. +// `BIO_get_retry_reason`. OPENSSL_EXPORT int BIO_should_io_special(const BIO *bio); // BIO_RR_CONNECT indicates that a connect would have blocked @@ -171,62 +188,62 @@ OPENSSL_EXPORT int BIO_should_io_special(const BIO *bio); #define BIO_RR_ACCEPT 0x03 // BIO_get_retry_reason returns the special I/O operation that needs to be -// retried. The return value is one of the |BIO_RR_*| values. +// retried. The return value is one of the `BIO_RR_*` values. OPENSSL_EXPORT int BIO_get_retry_reason(const BIO *bio); // BIO_set_retry_reason sets the special I/O operation that needs to be retried -// to |reason|, which should be one of the |BIO_RR_*| values. +// to `reason`, which should be one of the `BIO_RR_*` values. OPENSSL_EXPORT void BIO_set_retry_reason(BIO *bio, int reason); -// BIO_set_retry_read sets the |BIO_FLAGS_READ| and |BIO_FLAGS_SHOULD_RETRY| -// flags on |bio|. +// BIO_set_retry_read sets the `BIO_FLAGS_READ` and `BIO_FLAGS_SHOULD_RETRY` +// flags on `bio`. OPENSSL_EXPORT void BIO_set_retry_read(BIO *bio); -// BIO_set_retry_write sets the |BIO_FLAGS_WRITE| and |BIO_FLAGS_SHOULD_RETRY| -// flags on |bio|. +// BIO_set_retry_write sets the `BIO_FLAGS_WRITE` and `BIO_FLAGS_SHOULD_RETRY` +// flags on `bio`. OPENSSL_EXPORT void BIO_set_retry_write(BIO *bio); -// BIO_get_retry_flags gets the |BIO_FLAGS_READ|, |BIO_FLAGS_WRITE|, -// |BIO_FLAGS_IO_SPECIAL| and |BIO_FLAGS_SHOULD_RETRY| flags from |bio|. +// BIO_get_retry_flags gets the `BIO_FLAGS_READ`, `BIO_FLAGS_WRITE`, +// `BIO_FLAGS_IO_SPECIAL` and `BIO_FLAGS_SHOULD_RETRY` flags from `bio`. OPENSSL_EXPORT int BIO_get_retry_flags(BIO *bio); -// BIO_clear_retry_flags clears the |BIO_FLAGS_READ|, |BIO_FLAGS_WRITE|, -// |BIO_FLAGS_IO_SPECIAL| and |BIO_FLAGS_SHOULD_RETRY| flags from |bio|. +// BIO_clear_retry_flags clears the `BIO_FLAGS_READ`, `BIO_FLAGS_WRITE`, +// `BIO_FLAGS_IO_SPECIAL` and `BIO_FLAGS_SHOULD_RETRY` flags from `bio`. OPENSSL_EXPORT void BIO_clear_retry_flags(BIO *bio); -// BIO_method_type returns the type of |bio|, which is one of the |BIO_TYPE_*| +// BIO_method_type returns the type of `bio`, which is one of the `BIO_TYPE_*` // values. OPENSSL_EXPORT int BIO_method_type(const BIO *bio); typedef int BIO_info_cb(BIO *, int, int); -// BIO_callback_ctrl allows the callback function to be manipulated. The |cmd| -// arg will generally be |BIO_CTRL_SET_CALLBACK| but arbitrary command values -// can be interpreted by the |BIO|. +// BIO_callback_ctrl allows the callback function to be manipulated. The `cmd` +// arg will generally be `BIO_CTRL_SET_CALLBACK` but arbitrary command values +// can be interpreted by the `BIO`. OPENSSL_EXPORT long BIO_callback_ctrl(BIO *bio, int cmd, BIO_info_cb *fp); // BIO_pending returns the number of bytes pending to be read. OPENSSL_EXPORT size_t BIO_pending(const BIO *bio); -// BIO_ctrl_pending calls |BIO_pending| and exists only for compatibility with +// BIO_ctrl_pending calls `BIO_pending` and exists only for compatibility with // OpenSSL. OPENSSL_EXPORT size_t BIO_ctrl_pending(const BIO *bio); // BIO_wpending returns the number of bytes pending to be written. OPENSSL_EXPORT size_t BIO_wpending(const BIO *bio); -// BIO_set_close sets the close flag for |bio|. The meaning of which depends on -// the type of |bio| but, for example, a memory BIO interprets the close flag +// BIO_set_close sets the close flag for `bio`. The meaning of which depends on +// the type of `bio` but, for example, a memory BIO interprets the close flag // as meaning that it owns its buffer. It returns one on success and zero // otherwise. OPENSSL_EXPORT int BIO_set_close(BIO *bio, int close_flag); // BIO_number_read returns the number of bytes that have been read from -// |bio|. +// `bio`. OPENSSL_EXPORT uint64_t BIO_number_read(const BIO *bio); // BIO_number_written returns the number of bytes that have been written to -// |bio|. +// `bio`. OPENSSL_EXPORT uint64_t BIO_number_written(const BIO *bio); @@ -236,41 +253,41 @@ OPENSSL_EXPORT uint64_t BIO_number_written(const BIO *bio); // the next etc. The most common case is a buffering BIO, which accepts and // buffers writes until flushed into the next BIO in the chain. -// BIO_push adds |appended_bio| to the end of the chain with |bio| at the head. -// It returns |bio|. Note that |appended_bio| may be the head of a chain itself +// BIO_push adds `appended_bio` to the end of the chain with `bio` at the head. +// It returns `bio`. Note that `appended_bio` may be the head of a chain itself // and thus this function can be used to join two chains. // -// BIO_push takes ownership of the caller's reference to |appended_bio|. +// BIO_push takes ownership of the caller's reference to `appended_bio`. OPENSSL_EXPORT BIO *BIO_push(BIO *bio, BIO *appended_bio); -// BIO_pop removes |bio| from the head of a chain and returns the next BIO in +// BIO_pop removes `bio` from the head of a chain and returns the next BIO in // the chain, or NULL if there is no next BIO. // -// The caller takes ownership of the chain's reference to |bio|. +// The caller takes ownership of the chain's reference to `bio`. OPENSSL_EXPORT BIO *BIO_pop(BIO *bio); -// BIO_next returns the next BIO in the chain after |bio|, or NULL if there is +// BIO_next returns the next BIO in the chain after `bio`, or NULL if there is // no such BIO. OPENSSL_EXPORT BIO *BIO_next(BIO *bio); // BIO_find_type walks a chain of BIOs and returns the first that matches -// |type|, which is one of the |BIO_TYPE_*| values. +// `type`, which is one of the `BIO_TYPE_*` values. // -// If |type & 0xff| is non-zero, i.e. |type| is a complete type and contains an -// "index" component, the function looks for an exact match. If |type & 0xff| is -// zero, i.e. |type| just specifies |BIO_TYPE_DESCRIPTOR|, |BIO_TYPE_FILTER|, -// and |BIO_TYPE_SOURCE_SINK| bits, the function looks for any BIO whose type +// If `type & 0xff` is non-zero, i.e. `type` is a complete type and contains an +// "index" component, the function looks for an exact match. If `type & 0xff` is +// zero, i.e. `type` just specifies `BIO_TYPE_DESCRIPTOR`, `BIO_TYPE_FILTER`, +// and `BIO_TYPE_SOURCE_SINK` bits, the function looks for any BIO whose type // contains at least one of those bits. OPENSSL_EXPORT BIO *BIO_find_type(BIO *bio, int type); -// BIO_copy_next_retry sets the retry flags and |retry_reason| of |bio| from +// BIO_copy_next_retry sets the retry flags and `retry_reason` of `bio` from // the next BIO in the chain. OPENSSL_EXPORT void BIO_copy_next_retry(BIO *bio); // Printf functions. -// BIO_printf behaves like |printf| but outputs to |bio| rather than a |FILE|. +// BIO_printf behaves like `printf` but outputs to `bio` rather than a `FILE`. // It returns the number of bytes written or a negative number on error. OPENSSL_EXPORT int BIO_printf(BIO *bio, const char *format, ...) OPENSSL_PRINTF_FORMAT_FUNC(2, 3); @@ -278,98 +295,98 @@ OPENSSL_EXPORT int BIO_printf(BIO *bio, const char *format, ...) // Utility functions. -// BIO_indent prints min(|indent|, |max_indent|) spaces. It returns one on +// BIO_indent prints min(`indent`, `max_indent`) spaces. It returns one on // success and zero otherwise. OPENSSL_EXPORT int BIO_indent(BIO *bio, unsigned indent, unsigned max_indent); -// BIO_hexdump writes a hex dump of |data| to |bio|. Each line will be indented -// by |indent| spaces. It returns one on success and zero otherwise. +// BIO_hexdump writes a hex dump of `data` to `bio`. Each line will be indented +// by `indent` spaces. It returns one on success and zero otherwise. OPENSSL_EXPORT int BIO_hexdump(BIO *bio, const uint8_t *data, size_t len, unsigned indent); -// ERR_print_errors prints the current contents of the error stack to |bio| +// ERR_print_errors prints the current contents of the error stack to `bio` // using human readable strings where possible. OPENSSL_EXPORT void ERR_print_errors(BIO *bio); -// BIO_read_asn1 reads a single ASN.1 object from |bio|. If successful it sets -// |*out| to be an allocated buffer (that should be freed with |OPENSSL_free|), -// |*out_size| to the length, in bytes, of that buffer and returns one. +// BIO_read_asn1 reads a single ASN.1 object from `bio`. If successful it sets +// `*out` to be an allocated buffer (that should be freed with `OPENSSL_free`), +// `*out_size` to the length, in bytes, of that buffer and returns one. // Otherwise it returns zero. // -// If the length of the object is greater than |max_len| or 2^32 then the +// If the length of the object is greater than `max_len` or 2^32 then the // function will fail. Long-form tags are not supported. If the length of the -// object is indefinite the full contents of |bio| are read, unless it would be -// greater than |max_len|, in which case the function fails. +// object is indefinite the full contents of `bio` are read, unless it would be +// greater than `max_len`, in which case the function fails. // // If the function fails then some unknown amount of data may have been read -// from |bio|. +// from `bio`. OPENSSL_EXPORT int BIO_read_asn1(BIO *bio, uint8_t **out, size_t *out_len, size_t max_len); // Memory BIOs. // -// Memory BIOs can be used as a read-only source (with |BIO_new_mem_buf|) or a -// writable sink (with |BIO_new|, |BIO_s_mem| and |BIO_mem_contents|). Data +// Memory BIOs can be used as a read-only source (with `BIO_new_mem_buf`) or a +// writable sink (with `BIO_new`, `BIO_s_mem` and `BIO_mem_contents`). Data // written to a writable, memory BIO can be recalled by reading from it. // -// Calling |BIO_reset| on a read-only BIO resets it to the original contents. +// Calling `BIO_reset` on a read-only BIO resets it to the original contents. // On a writable BIO, it clears any data. // -// If the close flag is set to |BIO_NOCLOSE| (not the default) then the -// underlying |BUF_MEM| will not be freed when the |BIO| is freed. +// If the close flag is set to `BIO_NOCLOSE` (not the default) then the +// underlying `BUF_MEM` will not be freed when the `BIO` is freed. // -// Memory BIOs support |BIO_gets| and |BIO_puts|. +// Memory BIOs support `BIO_gets` and `BIO_puts`. // -// |BIO_ctrl_pending| returns the number of bytes currently stored. +// `BIO_ctrl_pending` returns the number of bytes currently stored. -// BIO_NOCLOSE and |BIO_CLOSE| can be used as symbolic arguments when a "close +// BIO_NOCLOSE and `BIO_CLOSE` can be used as symbolic arguments when a "close // flag" is passed to a BIO function. #define BIO_NOCLOSE 0 #define BIO_CLOSE 1 -// BIO_s_mem returns a |BIO_METHOD| that uses a in-memory buffer. +// BIO_s_mem returns a `BIO_METHOD` that uses an in-memory buffer. OPENSSL_EXPORT const BIO_METHOD *BIO_s_mem(void); -// BIO_new_mem_buf creates read-only BIO that reads from |len| bytes at |buf|. +// BIO_new_mem_buf creates read-only BIO that reads from `len` bytes at `buf`. // It returns the BIO or NULL on error. This function does not copy or take -// ownership of |buf|. The caller must ensure the memory pointed to by |buf| -// outlives the |BIO|. +// ownership of `buf`. The caller must ensure the memory pointed to by `buf` +// outlives the `BIO`. // -// If |len| is negative, then |buf| is treated as a NUL-terminated string, but +// If `len` is negative, then `buf` is treated as a NUL-terminated string, but // don't depend on this in new code. OPENSSL_EXPORT BIO *BIO_new_mem_buf(const void *buf, ossl_ssize_t len); -// BIO_mem_contents sets |*out_contents| to point to the current contents of -// |bio| and |*out_len| to contain the length of that data. It returns one on +// BIO_mem_contents sets `*out_contents` to point to the current contents of +// `bio` and `*out_len` to contain the length of that data. It returns one on // success and zero otherwise. OPENSSL_EXPORT int BIO_mem_contents(const BIO *bio, const uint8_t **out_contents, size_t *out_len); -// BIO_get_mem_data sets |*contents| to point to the current contents of |bio| +// BIO_get_mem_data sets `*contents` to point to the current contents of `bio` // and returns the length of the data. // -// WARNING: don't use this, use |BIO_mem_contents|. A return value of zero from +// WARNING: don't use this, use `BIO_mem_contents`. A return value of zero from // this function can mean either that it failed or that the memory buffer is // empty. OPENSSL_EXPORT long BIO_get_mem_data(BIO *bio, char **contents); -// BIO_get_mem_ptr sets |*out| to a BUF_MEM containing the current contents of -// |bio|. It returns one on success or zero on error. +// BIO_get_mem_ptr sets `*out` to a BUF_MEM containing the current contents of +// `bio`. It returns one on success or zero on error. OPENSSL_EXPORT int BIO_get_mem_ptr(BIO *bio, BUF_MEM **out); -// BIO_set_mem_buf sets |b| as the contents of |bio|. If |take_ownership| is -// non-zero, then |b| will be freed when |bio| is closed. Returns one on +// BIO_set_mem_buf sets `b` as the contents of `bio`. If `take_ownership` is +// non-zero, then `b` will be freed when `bio` is closed. Returns one on // success or zero otherwise. OPENSSL_EXPORT int BIO_set_mem_buf(BIO *bio, BUF_MEM *b, int take_ownership); // BIO_set_mem_eof_return sets the value that will be returned from reading -// |bio| when empty. If |eof_value| is zero then an empty memory BIO will -// return EOF (that is it will return zero and |BIO_should_retry| will be -// false). If |eof_value| is non zero then it will return |eof_value| when it -// is empty and it will set the read retry flag (that is |BIO_read_retry| is -// true). To avoid ambiguity with a normal positive return value, |eof_value| +// `bio` when empty. If `eof_value` is zero then an empty memory BIO will +// return EOF (that is it will return zero and `BIO_should_retry` will be +// false). If `eof_value` is non zero then it will return `eof_value` when it +// is empty and it will set the read retry flag (that is `BIO_read_retry` is +// true). To avoid ambiguity with a normal positive return value, `eof_value` // should be set to a negative value, typically -1. // // For a read-only BIO, the default is zero (EOF). For a writable BIO, the @@ -379,149 +396,149 @@ OPENSSL_EXPORT int BIO_set_mem_eof_return(BIO *bio, int eof_value); // File descriptor BIOs. // -// File descriptor BIOs are wrappers around the system's |read| and |write| -// functions. If the close flag is set then then |close| is called on the +// File descriptor BIOs are wrappers around the system's `read` and `write` +// functions. If the close flag is set then `close` is called on the // underlying file descriptor when the BIO is freed. // -// |BIO_reset| attempts to seek the file pointer to the start of file using -// |lseek|. +// `BIO_reset` attempts to seek the file pointer to the start of file using +// `lseek`. #if !defined(OPENSSL_NO_POSIX_IO) -// BIO_s_fd returns a |BIO_METHOD| for file descriptor fds. +// BIO_s_fd returns a `BIO_METHOD` for file descriptor fds. OPENSSL_EXPORT const BIO_METHOD *BIO_s_fd(void); -// BIO_new_fd creates a new file descriptor BIO wrapping |fd|. If |close_flag| -// is non-zero, then |fd| will be closed when the BIO is. +// BIO_new_fd creates a new file descriptor BIO wrapping `fd`. If `close_flag` +// is non-zero, then `fd` will be closed when the BIO is. OPENSSL_EXPORT BIO *BIO_new_fd(int fd, int close_flag); #endif -// BIO_set_fd sets the file descriptor of |bio| to |fd|. If |close_flag| is -// non-zero then |fd| will be closed when |bio| is. It returns one on success +// BIO_set_fd sets the file descriptor of `bio` to `fd`. If `close_flag` is +// non-zero then `fd` will be closed when `bio` is. It returns one on success // or zero on error. // -// This function may also be used with socket BIOs (see |BIO_s_socket| and -// |BIO_new_socket|). +// This function may also be used with socket BIOs (see `BIO_s_socket` and +// `BIO_new_socket`). OPENSSL_EXPORT int BIO_set_fd(BIO *bio, int fd, int close_flag); -// BIO_get_fd returns the file descriptor currently in use by |bio| or -1 if -// |bio| does not wrap a file descriptor. If there is a file descriptor and -// |out_fd| is not NULL, it also sets |*out_fd| to the file descriptor. +// BIO_get_fd returns the file descriptor currently in use by `bio` or -1 if +// `bio` does not wrap a file descriptor. If there is a file descriptor and +// `out_fd` is not NULL, it also sets `*out_fd` to the file descriptor. // -// This function may also be used with socket BIOs (see |BIO_s_socket| and -// |BIO_new_socket|). +// This function may also be used with socket BIOs (see `BIO_s_socket` and +// `BIO_new_socket`). OPENSSL_EXPORT int BIO_get_fd(BIO *bio, int *out_fd); // File BIOs. // -// File BIOs are wrappers around a C |FILE| object. +// File BIOs are wrappers around a C `FILE` object. // -// |BIO_flush| on a file BIO calls |fflush| on the wrapped stream. +// `BIO_flush` on a file BIO calls `fflush` on the wrapped stream. // -// |BIO_reset| attempts to seek the file pointer to the start of file using -// |fseek|. +// `BIO_reset` attempts to seek the file pointer to the start of file using +// `fseek`. // -// Setting the close flag causes |fclose| to be called on the stream when the +// Setting the close flag causes `fclose` to be called on the stream when the // BIO is freed. -// BIO_s_file returns a BIO_METHOD that wraps a |FILE|. +// BIO_s_file returns a BIO_METHOD that wraps a `FILE`. OPENSSL_EXPORT const BIO_METHOD *BIO_s_file(void); -// BIO_new_file creates a file BIO by opening |filename| with the given mode. -// See the |fopen| manual page for details of the mode argument. On Windows, -// files may be opened in either binary or text mode so, as in |fopen|, callers -// must specify the desired option in |mode|. +// BIO_new_file creates a file BIO by opening `filename` with the given mode. +// See the `fopen` manual page for details of the mode argument. On Windows, +// files may be opened in either binary or text mode so, as in `fopen`, callers +// must specify the desired option in `mode`. OPENSSL_EXPORT BIO *BIO_new_file(const char *filename, const char *mode); -// BIO_FP_TEXT indicates the |FILE| should be switched to text mode on Windows. +// BIO_FP_TEXT indicates the `FILE` should be switched to text mode on Windows. // It has no effect on non-Windows platforms. #define BIO_FP_TEXT 0x10 -// BIO_new_fp creates a new file BIO that wraps |file|. If |flags| contains -// |BIO_CLOSE|, then |fclose| will be called on |file| when the BIO is closed. +// BIO_new_fp creates a new file BIO that wraps `file`. If `flags` contains +// `BIO_CLOSE`, then `fclose` will be called on `file` when the BIO is closed. // -// On Windows, if |flags| contains |BIO_FP_TEXT|, this function will -// additionally switch |file| to text mode. This is not recommended, but may be -// required for OpenSSL compatibility. If |file| was not already in text mode, -// mode changes can cause unflushed data in |file| to be written in unexpected -// ways. See |_setmode| in Windows documentation for details. +// On Windows, if `flags` contains `BIO_FP_TEXT`, this function will +// additionally switch `file` to text mode. This is not recommended, but may be +// required for OpenSSL compatibility. If `file` was not already in text mode, +// mode changes can cause unflushed data in `file` to be written in unexpected +// ways. See `_setmode` in Windows documentation for details. // -// Unlike OpenSSL, if |flags| does not contain |BIO_FP_TEXT|, the translation -// mode of |file| is left as-is. In OpenSSL, |file| will be set to binary, with +// Unlike OpenSSL, if `flags` does not contain `BIO_FP_TEXT`, the translation +// mode of `file` is left as-is. In OpenSSL, `file` will be set to binary, with // the same pitfalls as above. BoringSSL does not do this so that wrapping a -// |FILE| in a |BIO| will not inadvertently change its state. +// `FILE` in a `BIO` will not inadvertently change its state. // // To avoid these pitfalls, callers should set the desired translation mode when // opening the file. If targeting just BoringSSL, this is sufficient. If -// targeting both OpenSSL and BoringSSL, callers should set |BIO_FP_TEXT| to +// targeting both OpenSSL and BoringSSL, callers should set `BIO_FP_TEXT` to // match the desired state of the file. OPENSSL_EXPORT BIO *BIO_new_fp(FILE *file, int flags); -// BIO_get_fp sets |*out_file| to the current |FILE| for |bio|. It returns one +// BIO_get_fp sets `*out_file` to the current `FILE` for `bio`. It returns one // on success and zero otherwise. OPENSSL_EXPORT int BIO_get_fp(BIO *bio, FILE **out_file); -// BIO_set_fp sets the |FILE| for |bio|. If |flags| contains |BIO_CLOSE| then -// |fclose| will be called on |file| when |bio| is closed. It returns one on +// BIO_set_fp sets the `FILE` for `bio`. If `flags` contains `BIO_CLOSE` then +// `fclose` will be called on `file` when `bio` is closed. It returns one on // success and zero otherwise. // -// On Windows, if |flags| contains |BIO_FP_TEXT|, this function will -// additionally switch |file| to text mode. This is not recommended, but may be -// required for OpenSSL compatibility. If |file| was not already in text mode, -// mode changes can cause unflushed data in |file| to be written in unexpected -// ways. See |_setmode| in Windows documentation for details. +// On Windows, if `flags` contains `BIO_FP_TEXT`, this function will +// additionally switch `file` to text mode. This is not recommended, but may be +// required for OpenSSL compatibility. If `file` was not already in text mode, +// mode changes can cause unflushed data in `file` to be written in unexpected +// ways. See `_setmode` in Windows documentation for details. // -// Unlike OpenSSL, if |flags| does not contain |BIO_FP_TEXT|, the translation -// mode of |file| is left as-is. In OpenSSL, |file| will be set to binary, with +// Unlike OpenSSL, if `flags` does not contain `BIO_FP_TEXT`, the translation +// mode of `file` is left as-is. In OpenSSL, `file` will be set to binary, with // the same pitfalls as above. BoringSSL does not do this so that wrapping a -// |FILE| in a |BIO| will not inadvertently change its state. +// `FILE` in a `BIO` will not inadvertently change its state. // // To avoid these pitfalls, callers should set the desired translation mode when // opening the file. If targeting just BoringSSL, this is sufficient. If -// targeting both OpenSSL and BoringSSL, callers should set |BIO_FP_TEXT| to +// targeting both OpenSSL and BoringSSL, callers should set `BIO_FP_TEXT` to // match the desired state of the file. OPENSSL_EXPORT int BIO_set_fp(BIO *bio, FILE *file, int flags); -// BIO_read_filename opens |filename| for reading and sets the result as the -// |FILE| for |bio|. It returns one on success and zero otherwise. The |FILE| -// will be closed when |bio| is freed. On Windows, the file is opened in binary +// BIO_read_filename opens `filename` for reading and sets the result as the +// `FILE` for `bio`. It returns one on success and zero otherwise. The `FILE` +// will be closed when `bio` is freed. On Windows, the file is opened in binary // mode. OPENSSL_EXPORT int BIO_read_filename(BIO *bio, const char *filename); -// BIO_write_filename opens |filename| for writing and sets the result as the -// |FILE| for |bio|. It returns one on success and zero otherwise. The |FILE| -// will be closed when |bio| is freed. On Windows, the file is opened in binary +// BIO_write_filename opens `filename` for writing and sets the result as the +// `FILE` for `bio`. It returns one on success and zero otherwise. The `FILE` +// will be closed when `bio` is freed. On Windows, the file is opened in binary // mode. OPENSSL_EXPORT int BIO_write_filename(BIO *bio, const char *filename); -// BIO_append_filename opens |filename| for appending and sets the result as -// the |FILE| for |bio|. It returns one on success and zero otherwise. The -// |FILE| will be closed when |bio| is freed. On Windows, the file is opened in +// BIO_append_filename opens `filename` for appending and sets the result as +// the `FILE` for `bio`. It returns one on success and zero otherwise. The +// `FILE` will be closed when `bio` is freed. On Windows, the file is opened in // binary mode. OPENSSL_EXPORT int BIO_append_filename(BIO *bio, const char *filename); -// BIO_rw_filename opens |filename| for reading and writing and sets the result -// as the |FILE| for |bio|. It returns one on success and zero otherwise. The -// |FILE| will be closed when |bio| is freed. On Windows, the file is opened in +// BIO_rw_filename opens `filename` for reading and writing and sets the result +// as the `FILE` for `bio`. It returns one on success and zero otherwise. The +// `FILE` will be closed when `bio` is freed. On Windows, the file is opened in // binary mode. OPENSSL_EXPORT int BIO_rw_filename(BIO *bio, const char *filename); -// BIO_tell returns the file offset of |bio|, or a negative number on error or -// if |bio| does not support the operation. +// BIO_tell returns the file offset of `bio`, or a negative number on error or +// if `bio` does not support the operation. // -// TODO(crbug.com/42290329): On platforms where |long| is 32-bit, this function +// TODO(crbug.com/42290329): On platforms where `long` is 32-bit, this function // cannot report 64-bit offsets. OPENSSL_EXPORT long BIO_tell(BIO *bio); -// BIO_seek sets the file offset of |bio| to |offset|. It returns a non-negative -// number on success and a negative number on error. If |bio| is a file -// descriptor |BIO|, it returns the resulting file offset on success. If |bio| -// is a file |BIO|, it returns zero on success. +// BIO_seek sets the file offset of `bio` to `offset`. It returns a non-negative +// number on success and a negative number on error. If `bio` is a file +// descriptor `BIO`, it returns the resulting file offset on success. If `bio` +// is a file `BIO`, it returns zero on success. // // WARNING: This function's return value conventions differs from most functions // in this library. // -// TODO(crbug.com/42290329): On platforms where |long| is 32-bit, this function +// TODO(crbug.com/42290329): On platforms where `long` is 32-bit, this function // cannot handle 64-bit offsets. OPENSSL_EXPORT long BIO_seek(BIO *bio, long offset); @@ -529,21 +546,21 @@ OPENSSL_EXPORT long BIO_seek(BIO *bio, long offset); // Socket BIOs. // // Socket BIOs behave like file descriptor BIOs but, on Windows systems, wrap -// the system's |recv| and |send| functions instead of |read| and |write|. On +// the system's `recv` and `send` functions instead of `read` and `write`. On // Windows, file descriptors are provided by C runtime and are not // interchangeable with sockets. // -// Socket BIOs may be used with |BIO_set_fd| and |BIO_get_fd|. +// Socket BIOs may be used with `BIO_set_fd` and `BIO_get_fd`. // -// TODO(davidben): Add separate APIs and fix the internals to use |SOCKET|s +// TODO(davidben): Add separate APIs and fix the internals to use `SOCKET`s // around rather than rely on int casts. #if !defined(OPENSSL_NO_SOCK) OPENSSL_EXPORT const BIO_METHOD *BIO_s_socket(void); // BIO_new_socket allocates and initialises a fresh BIO which will read and -// write to the socket |fd|. If |close_flag| is |BIO_CLOSE| then closing the -// BIO will close |fd|. It returns the fresh |BIO| or NULL on error. +// write to the socket `fd`. If `close_flag` is `BIO_CLOSE` then closing the +// BIO will close `fd`. It returns the fresh `BIO` or NULL on error. OPENSSL_EXPORT BIO *BIO_new_socket(int fd, int close_flag); #endif // !OPENSSL_NO_SOCK @@ -557,38 +574,38 @@ OPENSSL_EXPORT BIO *BIO_new_socket(int fd, int close_flag); OPENSSL_EXPORT const BIO_METHOD *BIO_s_connect(void); // BIO_new_connect returns a BIO that connects to the given hostname and port. -// The |host_and_optional_port| argument should be of the form +// The `host_and_optional_port` argument should be of the form // "www.example.com" or "www.example.com:443". If the port is omitted, it must -// be provided with |BIO_set_conn_port|. +// be provided with `BIO_set_conn_port`. // // It returns the new BIO on success, or NULL on error. OPENSSL_EXPORT BIO *BIO_new_connect(const char *host_and_optional_port); -// BIO_set_conn_hostname sets |host_and_optional_port| as the hostname and -// optional port that |bio| will connect to. If the port is omitted, it must be -// provided with |BIO_set_conn_port|. +// BIO_set_conn_hostname sets `host_and_optional_port` as the hostname and +// optional port that `bio` will connect to. If the port is omitted, it must be +// provided with `BIO_set_conn_port`. // // It returns one on success and zero otherwise. OPENSSL_EXPORT int BIO_set_conn_hostname(BIO *bio, const char *host_and_optional_port); -// BIO_set_conn_port sets |port_str| as the port or service name that |bio| +// BIO_set_conn_port sets `port_str` as the port or service name that `bio` // will connect to. It returns one on success and zero otherwise. OPENSSL_EXPORT int BIO_set_conn_port(BIO *bio, const char *port_str); -// BIO_set_conn_int_port sets |*port| as the port that |bio| will connect to. +// BIO_set_conn_int_port sets `*port` as the port that `bio` will connect to. // It returns one on success and zero otherwise. OPENSSL_EXPORT int BIO_set_conn_int_port(BIO *bio, const int *port); -// BIO_set_nbio sets whether |bio| will use non-blocking I/O operations. It +// BIO_set_nbio sets whether `bio` will use non-blocking I/O operations. It // returns one on success and zero otherwise. This only works for connect BIOs -// and must be called before |bio| is connected to take effect. +// and must be called before `bio` is connected to take effect. // // For socket and fd BIOs, callers must configure blocking vs. non-blocking I/O // using the underlying platform APIs. OPENSSL_EXPORT int BIO_set_nbio(BIO *bio, int on); -// BIO_do_connect connects |bio| if it has not been connected yet. It returns +// BIO_do_connect connects `bio` if it has not been connected yet. It returns // one on success and <= 0 otherwise. OPENSSL_EXPORT int BIO_do_connect(BIO *bio); #endif // !OPENSSL_NO_SOCK @@ -600,18 +617,20 @@ OPENSSL_EXPORT int BIO_do_connect(BIO *bio); #define BIO_CTRL_DGRAM_QUERY_MTU 40 // as kernel for current MTU -#define BIO_CTRL_DGRAM_SET_MTU 42 /* set cached value for MTU. want to use - this if asking the kernel fails */ +#define BIO_CTRL_DGRAM_SET_MTU \ + 42 /* set cached value for MTU. want to use \ + this if asking the kernel fails */ -#define BIO_CTRL_DGRAM_MTU_EXCEEDED 43 /* check whether the MTU was exceed in - the previous write operation. */ +#define BIO_CTRL_DGRAM_MTU_EXCEEDED \ + 43 /* check whether the MTU was exceed in \ + the previous write operation. */ // BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT is unsupported as it is unused by consumers -// and depends on |timeval|, which is not 2038-clean on all platforms. +// and depends on `timeval`, which is not 2038-clean on all platforms. -#define BIO_CTRL_DGRAM_GET_PEER 46 +#define BIO_CTRL_DGRAM_GET_PEER 46 -#define BIO_CTRL_DGRAM_GET_FALLBACK_MTU 47 +#define BIO_CTRL_DGRAM_GET_FALLBACK_MTU 47 // BIO Pairs. @@ -619,154 +638,195 @@ OPENSSL_EXPORT int BIO_do_connect(BIO *bio); // BIO pairs provide a "loopback" like system: a pair of BIOs where data // written to one can be read from the other and vice versa. -// BIO_new_bio_pair sets |*out1| and |*out2| to two freshly created BIOs where +// BIO_new_bio_pair sets `*out1` and `*out2` to two freshly created BIOs where // data written to one can be read from the other and vice versa. The -// |writebuf1| argument gives the size of the buffer used in |*out1| and -// |writebuf2| for |*out2|. It returns one on success and zero on error. +// `writebuf1` argument gives the size of the buffer used in `*out1` and +// `writebuf2` for `*out2`. It returns one on success and zero on error. OPENSSL_EXPORT int BIO_new_bio_pair(BIO **out1, size_t writebuf1, BIO **out2, size_t writebuf2); // BIO_ctrl_get_read_request returns the number of bytes that the other side of -// |bio| tried (unsuccessfully) to read. +// `bio` tried (unsuccessfully) to read. OPENSSL_EXPORT size_t BIO_ctrl_get_read_request(BIO *bio); -// BIO_ctrl_get_write_guarantee returns the number of bytes that |bio| (which -// must have been returned by |BIO_new_bio_pair|) will accept on the next -// |BIO_write| call. +// BIO_ctrl_get_write_guarantee returns the number of bytes that `bio` (which +// must have been returned by `BIO_new_bio_pair`) will accept on the next +// `BIO_write` call. OPENSSL_EXPORT size_t BIO_ctrl_get_write_guarantee(BIO *bio); -// BIO_shutdown_wr marks |bio| as closed, from the point of view of the other -// side of the pair. Future |BIO_write| calls on |bio| will fail. It returns +// BIO_shutdown_wr marks `bio` as closed, from the point of view of the other +// side of the pair. Future `BIO_write` calls on `bio` will fail. It returns // one on success and zero otherwise. OPENSSL_EXPORT int BIO_shutdown_wr(BIO *bio); // Custom BIOs. // -// Consumers can create custom |BIO|s by filling in a |BIO_METHOD| and using +// Consumers can create custom `BIO`s by filling in a `BIO_METHOD` and using // low-level control functions to set state. -// BIO_get_new_index returns a new "type" value for a custom |BIO|, or -1 on +// BIO_get_new_index returns a new "type" value for a custom `BIO`, or -1 on // error. OPENSSL_EXPORT int BIO_get_new_index(void); -// BIO_meth_new returns a newly-allocated |BIO_METHOD| or NULL on allocation -// error. The |type| specifies the type that will be returned by -// |BIO_method_type|. If this is unnecessary, this value may be zero. The |name| -// parameter is vestigial and may be NULL. -// -// Use the |BIO_meth_set_*| functions below to initialize the |BIO_METHOD|. The -// function implementations may use |BIO_set_data| and |BIO_get_data| to add -// method-specific state to associated |BIO|s. Additionally, |BIO_set_init| must -// be called after an associated |BIO| is fully initialized. State set via -// |BIO_set_data| may be released by configuring a destructor with -// |BIO_meth_set_destroy|. +// BIO_meth_new returns a newly-allocated `BIO_METHOD` or NULL on allocation +// error. `type` and `name` optionally identify the `BIO_METHOD`. Most callers +// will not need to inspect the type of their own `BIO`s and can pass zero and +// NULL, respectively, to skip this mechanism. +// +// `type` specifies the type that will be returned by `BIO_method_type` and +// matched by `BIO_find_type`. If used, it should be a combination of an index, +// allocated by `BIO_get_new_index`, and optionally the `BIO_TYPE_DESCRIPTOR`, +// `BIO_TYPE_FILTER`, and `BIO_TYPE_SOURCE_SINK` flags. If `BIO_TYPE_DESCRIPTOR` +// is set, the `BIO_METHOD` must implement `BIO_C_GET_FD`. +// +// `name` is unused. BoringSSL does not currently implement `BIO_method_name`. +// +// Use the `BIO_meth_set_*` functions below to initialize the `BIO_METHOD`. The +// function implementations may use `BIO_set_data` and `BIO_get_data` to add +// method-specific state to associated `BIO`s. Additionally, `BIO_set_init` must +// be called after an associated `BIO` is fully initialized. State set via +// `BIO_set_data` may be released by configuring a destructor with +// `BIO_meth_set_destroy`. OPENSSL_EXPORT BIO_METHOD *BIO_meth_new(int type, const char *name); -// BIO_meth_free releases memory associated with |method|. +// BIO_meth_free releases memory associated with `method`. OPENSSL_EXPORT void BIO_meth_free(BIO_METHOD *method); -// BIO_meth_set_create sets a function to be called on |BIO_new| for |method| +// BIO_meth_set_create sets a function to be called on `BIO_new` for `method` // and returns one. The function should return one on success and zero on // error. OPENSSL_EXPORT int BIO_meth_set_create(BIO_METHOD *method, int (*create_func)(BIO *)); -// BIO_meth_set_destroy sets a function to release data associated with a |BIO| +// BIO_meth_set_destroy sets a function to release data associated with a `BIO` // and returns one. The function's return value is ignored. // -// As the |BIO| is about to be destroyed, it is not necessary for |destroy_func| -// to clear the BIO's state with |BIO_set_data| or |BIO_set_init|. There is no -// harm in clearing them, but the |BIO| will not be passed to |BIO| operations, -// unless |destroy_func| itself does so. +// As the `BIO` is about to be destroyed, it is not necessary for `destroy_func` +// to clear the BIO's state with `BIO_set_data` or `BIO_set_init`. There is no +// harm in clearing them, but the `BIO` will not be passed to `BIO` operations, +// unless `destroy_func` itself does so. OPENSSL_EXPORT int BIO_meth_set_destroy(BIO_METHOD *method, int (*destroy_func)(BIO *)); -// BIO_meth_set_write sets the implementation of |BIO_write| for |method| and -// returns one. |BIO_METHOD|s which implement |BIO_write| should also implement -// |BIO_CTRL_FLUSH|. (See |BIO_meth_set_ctrl|.) +// BIO_meth_set_write_ex sets the implementation of `BIO_write_ex` for `method` +// and returns one. `BIO_METHOD`s which implement `BIO_write_ex` should also +// implement `BIO_CTRL_FLUSH`. (See `BIO_meth_set_ctrl`.) +// +// `write_ex_func` can assume `BIO_get_init` returns one, the input size is +// greater than zero, and `out_written` is non-NULL. Those cases are handled +// before calling into the `BIO_METHOD`. +// +// If configured, `write_ex_func` will also be used to implement `BIO_write`, +// with the `BIO` framework converting the conventions. `BIO_meth_set_write_ex` +// and `BIO_meth_set_write` should not be configured on the same `BIO_METHOD`. +// Prefer `BIO_meth_set_write_ex` for a `size_t`-based API. +OPENSSL_EXPORT int BIO_meth_set_write_ex( + BIO_METHOD *method, + int (*write_ex_func)(BIO *, const char *, size_t, size_t *)); + +// BIO_meth_set_write sets the implementation of `BIO_write` for `method` and +// returns one. `BIO_METHOD`s which implement `BIO_write` should also implement +// `BIO_CTRL_FLUSH`. (See `BIO_meth_set_ctrl`.) +// +// `write_func` can assume `BIO_get_init` returns one and the input size is +// greater than zero. Those cases are handled before calling into the +// `BIO_METHOD`. +// +// If configured, `write_func` will also be used to implement `BIO_write_ex`, +// with the `BIO` framework converting the conventions. `BIO_meth_set_write_ex` +// and `BIO_meth_set_write` should not be configured on the same `BIO_METHOD`. +// Prefer `BIO_meth_set_write_ex` for a `size_t`-based API. OPENSSL_EXPORT int BIO_meth_set_write(BIO_METHOD *method, int (*write_func)(BIO *, const char *, int)); -// BIO_meth_set_read sets the implementation of |BIO_read| for |method| and +// BIO_meth_set_read sets the implementation of `BIO_read` for `method` and // returns one. +// +// `read_func` can assume `BIO_get_init` returns one and the output size is +// greater than zero. Those cases are handled before calling into the +// `BIO_METHOD`. OPENSSL_EXPORT int BIO_meth_set_read(BIO_METHOD *method, int (*read_func)(BIO *, char *, int)); -// BIO_meth_set_gets sets the implementation of |BIO_gets| for |method| and +// BIO_meth_set_gets sets the implementation of `BIO_gets` for `method` and // returns one. +// +// `gets_func` can assume `BIO_get_init` returns one and the output size is +// greater than zero. Those cases are handled before calling into the +// `BIO_METHOD`. OPENSSL_EXPORT int BIO_meth_set_gets(BIO_METHOD *method, int (*gets_func)(BIO *, char *, int)); -// BIO_meth_set_ctrl sets the implementation of |BIO_ctrl| for |method| and +// BIO_meth_set_ctrl sets the implementation of `BIO_ctrl` for `method` and // returns one. OPENSSL_EXPORT int BIO_meth_set_ctrl(BIO_METHOD *method, long (*ctrl_func)(BIO *, int, long, void *)); -// BIO_meth_set_callback_ctrl sets the implementation of |BIO_callback_ctrl| for -// |method| and returns one. +// BIO_meth_set_callback_ctrl sets the implementation of `BIO_callback_ctrl` for +// `method` and returns one. OPENSSL_EXPORT int BIO_meth_set_callback_ctrl( BIO_METHOD *method, long (*callback_ctrl_func)(BIO *, int, BIO_info_cb *)); -// BIO_set_data sets custom data on |bio|. It may be retried with -// |BIO_get_data|. +// BIO_set_data sets custom data on `bio`. It may be retried with +// `BIO_get_data`. // -// This function should only be called by the implementation of a custom |BIO|. -// In particular, the data pointer of a built-in |BIO| is private to the -// library. For other uses, see |BIO_set_ex_data| and |BIO_set_app_data|. +// This function should only be called by the implementation of a custom `BIO`. +// In particular, the data pointer of a built-in `BIO` is private to the +// library. For other uses, see `BIO_set_ex_data` and `BIO_set_app_data`. OPENSSL_EXPORT void BIO_set_data(BIO *bio, void *ptr); -// BIO_get_data returns custom data on |bio| set by |BIO_get_data|. +// BIO_get_data returns custom data on `bio` set by `BIO_get_data`. // -// This function should only be called by the implementation of a custom |BIO|. -// In particular, the data pointer of a built-in |BIO| is private to the -// library. For other uses, see |BIO_get_ex_data| and |BIO_get_app_data|. +// This function should only be called by the implementation of a custom `BIO`. +// In particular, the data pointer of a built-in `BIO` is private to the +// library. For other uses, see `BIO_get_ex_data` and `BIO_get_app_data`. OPENSSL_EXPORT void *BIO_get_data(BIO *bio); -// BIO_set_init sets whether |bio| has been fully initialized. Until fully -// initialized, |BIO_read| and |BIO_write| will fail. +// BIO_set_init sets whether `bio` has been fully initialized. Until fully +// initialized, `BIO_read` and `BIO_write` will fail. OPENSSL_EXPORT void BIO_set_init(BIO *bio, int init); -// BIO_get_init returns whether |bio| has been fully initialized. +// BIO_get_init returns whether `bio` has been fully initialized. OPENSSL_EXPORT int BIO_get_init(BIO *bio); -// These are values of the |cmd| argument to |BIO_ctrl|. +// These are values of the `cmd` argument to `BIO_ctrl`. -// BIO_CTRL_RESET implements |BIO_reset|. The arguments are unused. +// BIO_CTRL_RESET implements `BIO_reset`. The arguments are unused. #define BIO_CTRL_RESET 1 -// BIO_CTRL_EOF implements |BIO_eof|. The arguments are unused. +// BIO_CTRL_EOF implements `BIO_eof`. The arguments are unused. #define BIO_CTRL_EOF 2 // BIO_CTRL_INFO is a legacy command that returns information specific to the -// type of |BIO|. It is not safe to call generically and should not be -// implemented in new |BIO| types. +// type of `BIO`. It is not safe to call generically and should not be +// implemented in new `BIO` types. #define BIO_CTRL_INFO 3 -// BIO_CTRL_GET_CLOSE returns the close flag set by |BIO_CTRL_SET_CLOSE|. The +// BIO_CTRL_GET_CLOSE returns the close flag set by `BIO_CTRL_SET_CLOSE`. The // arguments are unused. #define BIO_CTRL_GET_CLOSE 8 -// BIO_CTRL_SET_CLOSE implements |BIO_set_close|. The |larg| argument is the +// BIO_CTRL_SET_CLOSE implements `BIO_set_close`. The `larg` argument is the // close flag. #define BIO_CTRL_SET_CLOSE 9 -// BIO_CTRL_PENDING implements |BIO_pending|. The arguments are unused. +// BIO_CTRL_PENDING implements `BIO_pending`. The arguments are unused. #define BIO_CTRL_PENDING 10 -// BIO_CTRL_FLUSH implements |BIO_flush|. The arguments are unused. +// BIO_CTRL_FLUSH implements `BIO_flush`. The arguments are unused. #define BIO_CTRL_FLUSH 11 -// BIO_CTRL_WPENDING implements |BIO_wpending|. The arguments are unused. +// BIO_CTRL_WPENDING implements `BIO_wpending`. The arguments are unused. #define BIO_CTRL_WPENDING 13 // BIO_CTRL_SET_CALLBACK sets an informational callback of type // int cb(BIO *bio, int state, int ret) #define BIO_CTRL_SET_CALLBACK 14 -// BIO_CTRL_GET_CALLBACK returns the callback set by |BIO_CTRL_SET_CALLBACK|. +// BIO_CTRL_GET_CALLBACK returns the callback set by `BIO_CTRL_SET_CALLBACK`. #define BIO_CTRL_GET_CALLBACK 15 // The following are never used, but are defined to aid porting existing code. @@ -780,7 +840,7 @@ OPENSSL_EXPORT int BIO_get_init(BIO *bio); // ex_data functions. // -// See |ex_data.h| for details. +// See `ex_data.h` for details. OPENSSL_EXPORT int BIO_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused, @@ -795,19 +855,19 @@ OPENSSL_EXPORT void *BIO_get_ex_data(const BIO *bio, int idx); // Deprecated functions. -// BIO_free_all calls |BIO_free|. Code that targets BoringSSL does not need to -// call a separate free function for |BIO|s that are part of a chain. +// BIO_free_all calls `BIO_free`. Code that targets BoringSSL does not need to +// call a separate free function for `BIO`s that are part of a chain. OPENSSL_EXPORT void BIO_free_all(BIO *bio); typedef BIO_info_cb bio_info_cb; -// BIO_f_base64 returns a filter |BIO| that base64-encodes data written into -// it, and decodes data read from it. |BIO_gets| is not supported. Call -// |BIO_flush| when done writing, to signal that no more data are to be -// encoded. The flag |BIO_FLAGS_BASE64_NO_NL| may be set to encode all the data +// BIO_f_base64 returns a filter `BIO` that base64-encodes data written into +// it, and decodes data read from it. `BIO_gets` is not supported. Call +// `BIO_flush` when done writing, to signal that no more data are to be +// encoded. The flag `BIO_FLAGS_BASE64_NO_NL` may be set to encode all the data // on one line. // -// Use |EVP_EncodeBlock| and |EVP_DecodeBase64| instead. +// Use `EVP_EncodeBlock` and `EVP_DecodeBase64` instead. OPENSSL_EXPORT const BIO_METHOD *BIO_f_base64(void); OPENSSL_EXPORT void BIO_set_retry_special(BIO *bio); @@ -815,41 +875,41 @@ OPENSSL_EXPORT void BIO_set_retry_special(BIO *bio); // BIO_set_write_buffer_size returns zero. OPENSSL_EXPORT int BIO_set_write_buffer_size(BIO *bio, int buffer_size); -// BIO_set_shutdown sets a method-specific "shutdown" bit on |bio|. +// BIO_set_shutdown sets a method-specific "shutdown" bit on `bio`. OPENSSL_EXPORT void BIO_set_shutdown(BIO *bio, int shutdown); // BIO_get_shutdown returns the method-specific "shutdown" bit. OPENSSL_EXPORT int BIO_get_shutdown(BIO *bio); -// BIO_meth_set_puts returns one. |BIO_puts| is implemented with |BIO_write| in +// BIO_meth_set_puts returns one. `BIO_puts` is implemented with `BIO_write` in // BoringSSL. OPENSSL_EXPORT int BIO_meth_set_puts(BIO_METHOD *method, int (*puts)(BIO *, const char *)); #if !defined(OPENSSL_NO_SOCK) // The following functions return function pointers, possibly NULL, which are -// compatible with the corresponding |BIO_meth_set_*| function. |method| must be -// |BIO_s_socket| or the program will abort. +// compatible with the corresponding `BIO_meth_set_*` function. `method` must be +// `BIO_s_socket` or the program will abort. // // Using these functions is inherently unsafe and fragile. It is not possible to // use them in a future-proof way. See // https://github.com/openssl/openssl/issues/26047 for details. BoringSSL // implements them solely for compatibility with Folly and older versions of // PostgreSQL. To work around the future-proofing problems, the return values -// may diverge from the true implementation of |BIO_s_socket|. +// may diverge from the true implementation of `BIO_s_socket`. // // Caller should not use these functions. They are not necessary to define -// custom |BIO_METHOD|s. Instead, callers should either: +// custom `BIO_METHOD`s. Instead, callers should either: // -// - Define a custom |BIO_METHOD| that owns a socket |BIO| somewhere in the -// custom data. See |BIO_set_data|. +// - Define a custom `BIO_METHOD` that owns a socket `BIO` somewhere in the +// custom data. See `BIO_set_data`. // -// - Define a custom |BIO_METHOD| that wraps a socket |BIO| as a filter. See -// |BIO_push| and |BIO_next|. +// - Define a custom `BIO_METHOD` that wraps a socket `BIO` as a filter. See +// `BIO_push` and `BIO_next`. // -// - Define a custom |BIO_METHOD| without |BIO_s_socket| at all. If not using -// the built-in read or write functions, |BIO_s_socket| only provides a no-op -// |BIO_CTRL_FLUSH| implementation. This can be implemented by the caller. +// - Define a custom `BIO_METHOD` without `BIO_s_socket` at all. If not using +// the built-in read or write functions, `BIO_s_socket` only provides a no-op +// `BIO_CTRL_FLUSH` implementation. This can be implemented by the caller. OPENSSL_EXPORT int (*BIO_meth_get_write(const BIO_METHOD *method))(BIO *, const char *, int); @@ -881,8 +941,8 @@ OPENSSL_EXPORT long (*BIO_meth_get_callback_ctrl(const BIO_METHOD *method))( // or change the data in any way. #define BIO_FLAGS_MEM_RDONLY 0x200 -// BIO_TYPE_DESCRIPTOR denotes that the |BIO| responds to the |BIO_C_SET_FD| -// (|BIO_set_fd|) and |BIO_C_GET_FD| (|BIO_get_fd|) control hooks. +// BIO_TYPE_DESCRIPTOR denotes that the `BIO` responds to the `BIO_C_SET_FD` +// (`BIO_set_fd`) and `BIO_C_GET_FD` (`BIO_get_fd`) control hooks. #define BIO_TYPE_DESCRIPTOR 0x0100 // socket, fd, connect or accept #define BIO_TYPE_FILTER 0x0200 #define BIO_TYPE_SOURCE_SINK 0x0400 @@ -912,7 +972,7 @@ OPENSSL_EXPORT long (*BIO_meth_get_callback_ctrl(const BIO_METHOD *method))( #define BIO_TYPE_ASN1 (22 | BIO_TYPE_FILTER) #define BIO_TYPE_COMP (23 | BIO_TYPE_FILTER) -// BIO_TYPE_START is the first user-allocated |BIO| type. No pre-defined type, +// BIO_TYPE_START is the first user-allocated `BIO` type. No pre-defined type, // flag bits aside, may exceed this value. #define BIO_TYPE_START 128 @@ -977,7 +1037,7 @@ extern "C++" { BSSL_NAMESPACE_BEGIN BORINGSSL_MAKE_DELETER(BIO, BIO_free) -BORINGSSL_MAKE_UP_REF(BIO, BIO_up_ref) +BORINGSSL_MAKE_UP_REF(BIO, BIO_dup_ref) BORINGSSL_MAKE_DELETER(BIO_METHOD, BIO_meth_free) BSSL_NAMESPACE_END diff --git a/third_party/boringssl/src/include/openssl/blake2.h b/third_party/boringssl/src/include/openssl/blake2.h index 2284591b1..7511e090a 100644 --- a/third_party/boringssl/src/include/openssl/blake2.h +++ b/third_party/boringssl/src/include/openssl/blake2.h @@ -36,22 +36,22 @@ struct blake2b_state_st { uint8_t block[BLAKE2B_CBLOCK]; }; -// BLAKE2B256_Init initialises |b2b| to perform a BLAKE2b-256 hash. There are no -// pointers inside |b2b| thus release of |b2b| is purely managed by the caller. +// BLAKE2B256_Init initialises `b2b` to perform a BLAKE2b-256 hash. There are no +// pointers inside `b2b` thus release of `b2b` is purely managed by the caller. OPENSSL_EXPORT void BLAKE2B256_Init(BLAKE2B_CTX *b2b); -// BLAKE2B256_Update appends |len| bytes from |data| to the digest being -// calculated by |b2b|. +// BLAKE2B256_Update appends `len` bytes from `data` to the digest being +// calculated by `b2b`. OPENSSL_EXPORT void BLAKE2B256_Update(BLAKE2B_CTX *b2b, const void *data, size_t len); -// BLAKE2B256_Final completes the digest calculated by |b2b| and writes -// |BLAKE2B256_DIGEST_LENGTH| bytes to |out|. +// BLAKE2B256_Final completes the digest calculated by `b2b` and writes +// `BLAKE2B256_DIGEST_LENGTH` bytes to `out`. OPENSSL_EXPORT void BLAKE2B256_Final(uint8_t out[BLAKE2B256_DIGEST_LENGTH], BLAKE2B_CTX *b2b); -// BLAKE2B256 writes the BLAKE2b-256 digset of |len| bytes from |data| to -// |out|. +// BLAKE2B256 writes the BLAKE2b-256 digset of `len` bytes from `data` to +// `out`. OPENSSL_EXPORT void BLAKE2B256(const uint8_t *data, size_t len, uint8_t out[BLAKE2B256_DIGEST_LENGTH]); diff --git a/third_party/boringssl/src/include/openssl/bn.h b/third_party/boringssl/src/include/openssl/bn.h index f0b53346e..5f34ab81f 100644 --- a/third_party/boringssl/src/include/openssl/bn.h +++ b/third_party/boringssl/src/include/openssl/bn.h @@ -43,10 +43,10 @@ extern "C" { // BN_ULONG is the native word size when working with big integers. // // Note: on some platforms, inttypes.h does not define print format macros in -// C++ unless |__STDC_FORMAT_MACROS| defined. This is due to text in C99 which +// C++ unless `__STDC_FORMAT_MACROS` defined. This is due to text in C99 which // was never adopted in any C++ standard and explicitly overruled in C++11. As -// this is a public header, bn.h does not define |__STDC_FORMAT_MACROS| itself. -// Projects which use |BN_*_FMT*| with outdated C headers may need to define it +// this is a public header, bn.h does not define `__STDC_FORMAT_MACROS` itself. +// Projects which use `BN_*_FMT*` with outdated C headers may need to define it // externally. #if defined(OPENSSL_64_BIT) typedef uint64_t BN_ULONG; @@ -70,26 +70,26 @@ typedef uint32_t BN_ULONG; // BN_new creates a new, allocated BIGNUM and initialises it. OPENSSL_EXPORT BIGNUM *BN_new(void); -// BN_init initialises a stack allocated |BIGNUM|. +// BN_init initialises a stack allocated `BIGNUM`. OPENSSL_EXPORT void BN_init(BIGNUM *bn); -// BN_free frees the data referenced by |bn| and, if |bn| was originally -// allocated on the heap, frees |bn| also. +// BN_free frees the data referenced by `bn` and, if `bn` was originally +// allocated on the heap, frees `bn` also. OPENSSL_EXPORT void BN_free(BIGNUM *bn); -// BN_clear_free erases and frees the data referenced by |bn| and, if |bn| was -// originally allocated on the heap, frees |bn| also. +// BN_clear_free erases and frees the data referenced by `bn` and, if `bn` was +// originally allocated on the heap, frees `bn` also. OPENSSL_EXPORT void BN_clear_free(BIGNUM *bn); -// BN_dup allocates a new BIGNUM and sets it equal to |src|. It returns the +// BN_dup allocates a new BIGNUM and sets it equal to `src`. It returns the // allocated BIGNUM on success or NULL otherwise. OPENSSL_EXPORT BIGNUM *BN_dup(const BIGNUM *src); -// BN_copy sets |dest| equal to |src| and returns |dest| or NULL on allocation +// BN_copy sets `dest` equal to `src` and returns `dest` or NULL on allocation // failure. OPENSSL_EXPORT BIGNUM *BN_copy(BIGNUM *dest, const BIGNUM *src); -// BN_clear sets |bn| to zero and erases the old data. +// BN_clear sets `bn` to zero and erases the old data. OPENSSL_EXPORT void BN_clear(BIGNUM *bn); // BN_value_one returns a static BIGNUM with value 1. @@ -99,140 +99,142 @@ OPENSSL_EXPORT const BIGNUM *BN_value_one(void); // Basic functions. // BN_num_bits returns the minimum number of bits needed to represent the -// absolute value of |bn|. +// absolute value of `bn`. OPENSSL_EXPORT unsigned BN_num_bits(const BIGNUM *bn); // BN_num_bytes returns the minimum number of bytes needed to represent the -// absolute value of |bn|. +// absolute value of `bn`. // -// While |size_t| is the preferred type for byte counts, callers can assume that -// |BIGNUM|s are bounded such that this value, and its corresponding bit count, -// will always fit in |int|. +// While `size_t` is the preferred type for byte counts, callers can assume that +// `BIGNUM`s are bounded such that this value, and its corresponding bit count, +// will always fit in `int`. OPENSSL_EXPORT unsigned BN_num_bytes(const BIGNUM *bn); -// BN_zero sets |bn| to zero. +// BN_zero sets `bn` to zero. OPENSSL_EXPORT void BN_zero(BIGNUM *bn); -// BN_one sets |bn| to one. It returns one on success or zero on allocation +// BN_one sets `bn` to one. It returns one on success or zero on allocation // failure. OPENSSL_EXPORT int BN_one(BIGNUM *bn); -// BN_set_word sets |bn| to |value|. It returns one on success or zero on +// BN_set_word sets `bn` to `value`. It returns one on success or zero on // allocation failure. OPENSSL_EXPORT int BN_set_word(BIGNUM *bn, BN_ULONG value); -// BN_set_u64 sets |bn| to |value|. It returns one on success or zero on +// BN_set_u64 sets `bn` to `value`. It returns one on success or zero on // allocation failure. OPENSSL_EXPORT int BN_set_u64(BIGNUM *bn, uint64_t value); -// BN_set_negative sets the sign of |bn|. +// BN_set_negative sets the sign of `bn`. OPENSSL_EXPORT void BN_set_negative(BIGNUM *bn, int sign); -// BN_is_negative returns one if |bn| is negative and zero otherwise. +// BN_is_negative returns one if `bn` is negative and zero otherwise. OPENSSL_EXPORT int BN_is_negative(const BIGNUM *bn); // Conversion functions. -// BN_bin2bn sets |*ret| to the value of |len| bytes from |in|, interpreted as -// a big-endian number, and returns |ret|. If |ret| is NULL then a fresh -// |BIGNUM| is allocated and returned. It returns NULL on allocation +// BN_bin2bn sets `*ret` to the value of `len` bytes from `in`, interpreted as +// a big-endian number, and returns `ret`. If `ret` is NULL then a fresh +// `BIGNUM` is allocated and returned. It returns NULL on allocation // failure. OPENSSL_EXPORT BIGNUM *BN_bin2bn(const uint8_t *in, size_t len, BIGNUM *ret); -// BN_bn2bin serialises the absolute value of |in| to |out| as a big-endian -// integer, which must have |BN_num_bytes| of space available. It returns the -// number of bytes written. Note this function leaks the magnitude of |in|. If -// |in| is secret, use |BN_bn2bin_padded| instead. +// BN_bn2bin serialises the absolute value of `in` to `out` as a big-endian +// integer, which must have `BN_num_bytes` of space available. It returns the +// number of bytes written. Note this function leaks the magnitude of `in`. If +// `in` is secret, use `BN_bn2bin_padded` instead. OPENSSL_EXPORT size_t BN_bn2bin(const BIGNUM *in, uint8_t *out); -// BN_lebin2bn sets |*ret| to the value of |len| bytes from |in|, interpreted as -// a little-endian number, and returns |ret|. If |ret| is NULL then a fresh -// |BIGNUM| is allocated and returned. It returns NULL on allocation +// BN_lebin2bn sets `*ret` to the value of `len` bytes from `in`, interpreted as +// a little-endian number, and returns `ret`. If `ret` is NULL then a fresh +// `BIGNUM` is allocated and returned. It returns NULL on allocation // failure. OPENSSL_EXPORT BIGNUM *BN_lebin2bn(const uint8_t *in, size_t len, BIGNUM *ret); -// BN_bn2le_padded serialises the absolute value of |in| to |out| as a -// little-endian integer, which must have |len| of space available, padding -// out the remainder of out with zeros. If |len| is smaller than |BN_num_bytes|, +// BN_bn2le_padded serialises the absolute value of `in` to `out` as a +// little-endian integer, which must have `len` of space available, padding +// out the remainder of out with zeros. If `len` is smaller than `BN_num_bytes`, // the function fails and returns 0. Otherwise, it returns 1. OPENSSL_EXPORT int BN_bn2le_padded(uint8_t *out, size_t len, const BIGNUM *in); -// BN_bn2bin_padded serialises the absolute value of |in| to |out| as a +// BN_bn2bin_padded serialises the absolute value of `in` to `out` as a // big-endian integer. The integer is padded with leading zeros up to size -// |len|. If |len| is smaller than |BN_num_bytes|, the function fails and +// `len`. If `len` is smaller than `BN_num_bytes`, the function fails and // returns 0. Otherwise, it returns 1. OPENSSL_EXPORT int BN_bn2bin_padded(uint8_t *out, size_t len, const BIGNUM *in); -// BN_bn2cbb_padded behaves like |BN_bn2bin_padded| but writes to a |CBB|. +// BN_bn2cbb_padded behaves like `BN_bn2bin_padded` but writes to a `CBB`. OPENSSL_EXPORT int BN_bn2cbb_padded(CBB *out, size_t len, const BIGNUM *in); // BN_bn2hex returns an allocated string that contains a NUL-terminated, hex -// representation of |bn|. If |bn| is negative, the first char in the resulting -// string will be '-'. Returns NULL on allocation failure. +// representation of `bn`. If `bn` is negative, the first char in the resulting +// string will be '-'. Returns NULL on allocation failure. The resulting string +// must be freed with `OPENSSL_free`. OPENSSL_EXPORT char *BN_bn2hex(const BIGNUM *bn); -// BN_hex2bn parses the leading hex number from |in|, which may be proceeded by +// BN_hex2bn parses the leading hex number from `in`, which may be proceeded by // a '-' to indicate a negative number and may contain trailing, non-hex data. -// If |outp| is not NULL, it constructs a BIGNUM equal to the hex number and -// stores it in |*outp|. If |*outp| is NULL then it allocates a new BIGNUM and -// updates |*outp|. It returns the number of bytes of |in| processed or zero on +// If `outp` is not NULL, it constructs a BIGNUM equal to the hex number and +// stores it in `*outp`. If `*outp` is NULL then it allocates a new BIGNUM and +// updates `*outp`. It returns the number of bytes of `in` processed or zero on // error. OPENSSL_EXPORT int BN_hex2bn(BIGNUM **outp, const char *in); // BN_bn2dec returns an allocated string that contains a NUL-terminated, -// decimal representation of |bn|. If |bn| is negative, the first char in the -// resulting string will be '-'. Returns NULL on allocation failure. +// decimal representation of `bn`. If `bn` is negative, the first char in the +// resulting string will be '-'. Returns NULL on allocation failure. The +// resulting string must be freed with `OPENSSL_free`. // // Converting an arbitrarily large integer to decimal is quadratic in the bit -// length of |a|. This function assumes the caller has capped the input within +// length of `a`. This function assumes the caller has capped the input within // performance tolerances. OPENSSL_EXPORT char *BN_bn2dec(const BIGNUM *a); -// BN_dec2bn parses the leading decimal number from |in|, which may be +// BN_dec2bn parses the leading decimal number from `in`, which may be // proceeded by a '-' to indicate a negative number and may contain trailing, -// non-decimal data. If |outp| is not NULL, it constructs a BIGNUM equal to the -// decimal number and stores it in |*outp|. If |*outp| is NULL then it -// allocates a new BIGNUM and updates |*outp|. It returns the number of bytes -// of |in| processed or zero on error. +// non-decimal data. If `outp` is not NULL, it constructs a BIGNUM equal to the +// decimal number and stores it in `*outp`. If `*outp` is NULL then it +// allocates a new BIGNUM and updates `*outp`. It returns the number of bytes +// of `in` processed or zero on error. // // Converting an arbitrarily large integer to decimal is quadratic in the bit -// length of |a|. This function assumes the caller has capped the input within +// length of `a`. This function assumes the caller has capped the input within // performance tolerances. OPENSSL_EXPORT int BN_dec2bn(BIGNUM **outp, const char *in); -// BN_asc2bn acts like |BN_dec2bn| or |BN_hex2bn| depending on whether |in| +// BN_asc2bn acts like `BN_dec2bn` or `BN_hex2bn` depending on whether `in` // begins with "0X" or "0x" (indicating hex) or not (indicating decimal). A // leading '-' is still permitted and comes before the optional 0X/0x. It // returns one on success or zero on error. OPENSSL_EXPORT int BN_asc2bn(BIGNUM **outp, const char *in); -// BN_print writes a hex encoding of |a| to |bio|. It returns one on success +// BN_print writes a hex encoding of `a` to `bio`. It returns one on success // and zero on error. OPENSSL_EXPORT int BN_print(BIO *bio, const BIGNUM *a); -// BN_print_fp acts like |BIO_print|, but wraps |fp| in a |BIO| first. +// BN_print_fp acts like `BIO_print`, but wraps `fp` in a `BIO` first. OPENSSL_EXPORT int BN_print_fp(FILE *fp, const BIGNUM *a); -// BN_get_word returns the absolute value of |bn| as a single word. If |bn| is +// BN_get_word returns the absolute value of `bn` as a single word. If `bn` is // too large to be represented as a single word, the maximum possible value // will be returned. OPENSSL_EXPORT BN_ULONG BN_get_word(const BIGNUM *bn); -// BN_get_u64 sets |*out| to the absolute value of |bn| as a |uint64_t| and -// returns one. If |bn| is too large to be represented as a |uint64_t|, it +// BN_get_u64 sets `*out` to the absolute value of `bn` as a `uint64_t` and +// returns one. If `bn` is too large to be represented as a `uint64_t`, it // returns zero. OPENSSL_EXPORT int BN_get_u64(const BIGNUM *bn, uint64_t *out); // ASN.1 functions. -// BN_parse_asn1_unsigned parses a non-negative DER INTEGER from |cbs| writes -// the result to |ret|. It returns one on success and zero on failure. +// BN_parse_asn1_unsigned parses a non-negative DER INTEGER from `cbs` writes +// the result to `ret`. It returns one on success and zero on failure. OPENSSL_EXPORT int BN_parse_asn1_unsigned(CBS *cbs, BIGNUM *ret); -// BN_marshal_asn1 marshals |bn| as a non-negative DER INTEGER and appends the -// result to |cbb|. It returns one on success and zero on failure. +// BN_marshal_asn1 marshals `bn` as a non-negative DER INTEGER and appends the +// result to `cbb`. It returns one on success and zero on failure. OPENSSL_EXPORT int BN_marshal_asn1(CBB *cbb, const BIGNUM *bn); @@ -240,100 +242,100 @@ OPENSSL_EXPORT int BN_marshal_asn1(CBB *cbb, const BIGNUM *bn); // // Certain BIGNUM operations need to use many temporary variables and // allocating and freeing them can be quite slow. Thus such operations typically -// take a |BN_CTX| parameter, which contains a pool of |BIGNUMs|. The |ctx| -// argument to a public function may be NULL, in which case a local |BN_CTX| +// take a `BN_CTX` parameter, which contains a pool of `BIGNUMs`. The `ctx` +// argument to a public function may be NULL, in which case a local `BN_CTX` // will be created just for the lifetime of that call. // -// A function must call |BN_CTX_start| first. Then, |BN_CTX_get| may be called -// repeatedly to obtain temporary |BIGNUM|s. All |BN_CTX_get| calls must be made -// before calling any other functions that use the |ctx| as an argument. +// A function must call `BN_CTX_start` first. Then, `BN_CTX_get` may be called +// repeatedly to obtain temporary `BIGNUM`s. All `BN_CTX_get` calls must be made +// before calling any other functions that use the `ctx` as an argument. // -// Finally, |BN_CTX_end| must be called before returning from the function. -// When |BN_CTX_end| is called, the |BIGNUM| pointers obtained from -// |BN_CTX_get| become invalid. +// Finally, `BN_CTX_end` must be called before returning from the function. +// When `BN_CTX_end` is called, the `BIGNUM` pointers obtained from +// `BN_CTX_get` become invalid. // BN_CTX_new returns a new, empty BN_CTX or NULL on allocation failure. OPENSSL_EXPORT BN_CTX *BN_CTX_new(void); -// BN_CTX_free frees all BIGNUMs contained in |ctx| and then frees |ctx| +// BN_CTX_free frees all BIGNUMs contained in `ctx` and then frees `ctx` // itself. OPENSSL_EXPORT void BN_CTX_free(BN_CTX *ctx); -// BN_CTX_start "pushes" a new entry onto the |ctx| stack and allows future -// calls to |BN_CTX_get|. +// BN_CTX_start "pushes" a new entry onto the `ctx` stack and allows future +// calls to `BN_CTX_get`. OPENSSL_EXPORT void BN_CTX_start(BN_CTX *ctx); -// BN_CTX_get returns a new |BIGNUM|, or NULL on allocation failure. Once -// |BN_CTX_get| has returned NULL, all future calls will also return NULL until -// |BN_CTX_end| is called. +// BN_CTX_get returns a new `BIGNUM`, or NULL on allocation failure. Once +// `BN_CTX_get` has returned NULL, all future calls will also return NULL until +// `BN_CTX_end` is called. OPENSSL_EXPORT BIGNUM *BN_CTX_get(BN_CTX *ctx); -// BN_CTX_end invalidates all |BIGNUM|s returned from |BN_CTX_get| since the -// matching |BN_CTX_start| call. +// BN_CTX_end invalidates all `BIGNUM`s returned from `BN_CTX_get` since the +// matching `BN_CTX_start` call. OPENSSL_EXPORT void BN_CTX_end(BN_CTX *ctx); // Simple arithmetic -// BN_add sets |r| = |a| + |b|, where |r| may be the same pointer as either |a| -// or |b|. It returns one on success and zero on allocation failure. +// BN_add sets `r` = `a` + `b`, where `r` may be the same pointer as either `a` +// or `b`. It returns one on success and zero on allocation failure. OPENSSL_EXPORT int BN_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b); -// BN_uadd sets |r| = |a| + |b|, considering only the absolute values of |a| and -// |b|. |r| may be the same pointer as either |a| or |b|. It returns one on +// BN_uadd sets `r` = `a` + `b`, considering only the absolute values of `a` and +// `b`. `r` may be the same pointer as either `a` or `b`. It returns one on // success and zero on allocation failure. OPENSSL_EXPORT int BN_uadd(BIGNUM *r, const BIGNUM *a, const BIGNUM *b); -// BN_add_word adds |w| to |a|. It returns one on success and zero otherwise. +// BN_add_word adds `w` to `a`. It returns one on success and zero otherwise. OPENSSL_EXPORT int BN_add_word(BIGNUM *a, BN_ULONG w); -// BN_sub sets |r| = |a| - |b|, where |r| may be the same pointer as either |a| -// or |b|. It returns one on success and zero on allocation failure. +// BN_sub sets `r` = `a` - `b`, where `r` may be the same pointer as either `a` +// or `b`. It returns one on success and zero on allocation failure. OPENSSL_EXPORT int BN_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b); -// BN_usub sets |r| = |a| - |b|, considering only the absolute values of |a| and -// |b|. The result must be non-negative, i.e. |b| <= |a|. |r| may be the same -// pointer as either |a| or |b|. It returns one on success and zero on error. +// BN_usub sets `r` = `a` - `b`, considering only the absolute values of `a` and +// `b`. The result must be non-negative, i.e. `b` <= `a`. `r` may be the same +// pointer as either `a` or `b`. It returns one on success and zero on error. OPENSSL_EXPORT int BN_usub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b); -// BN_sub_word subtracts |w| from |a|. It returns one on success and zero on +// BN_sub_word subtracts `w` from `a`. It returns one on success and zero on // allocation failure. OPENSSL_EXPORT int BN_sub_word(BIGNUM *a, BN_ULONG w); -// BN_mul sets |r| = |a| * |b|, where |r| may be the same pointer as |a| or -// |b|. Returns one on success and zero otherwise. +// BN_mul sets `r` = `a` * `b`, where `r` may be the same pointer as `a` or +// `b`. Returns one on success and zero otherwise. OPENSSL_EXPORT int BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx); -// BN_mul_word sets |bn| = |bn| * |w|. It returns one on success or zero on +// BN_mul_word sets `bn` = `bn` * `w`. It returns one on success or zero on // allocation failure. OPENSSL_EXPORT int BN_mul_word(BIGNUM *bn, BN_ULONG w); -// BN_sqr sets |r| = |a|^2 (i.e. squares), where |r| may be the same pointer as -// |a|. Returns one on success and zero otherwise. This is more efficient than +// BN_sqr sets `r` = `a`^2 (i.e. squares), where `r` may be the same pointer as +// `a`. Returns one on success and zero otherwise. This is more efficient than // BN_mul(r, a, a, ctx). OPENSSL_EXPORT int BN_sqr(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx); -// BN_div divides |numerator| by |divisor| and places the result in |quotient| -// and the remainder in |rem|. Either of |quotient| or |rem| may be NULL, in +// BN_div divides `numerator` by `divisor` and places the result in `quotient` +// and the remainder in `rem`. Either of `quotient` or `rem` may be NULL, in // which case the respective value is not returned. It returns one on success or -// zero on error. It is an error condition if |divisor| is zero. +// zero on error. It is an error condition if `divisor` is zero. // -// The outputs will be such that |quotient| * |divisor| + |rem| = |numerator|, -// with the quotient rounded towards zero. Thus, if |numerator| is negative, -// |rem| will be zero or negative. If |divisor| is negative, the sign of -// |quotient| will be flipped to compensate but otherwise rounding will be as if -// |divisor| were its absolute value. +// The outputs will be such that `quotient` * `divisor` + `rem` = `numerator`, +// with the quotient rounded towards zero. Thus, if `numerator` is negative, +// `rem` will be zero or negative. If `divisor` is negative, the sign of +// `quotient` will be flipped to compensate but otherwise rounding will be as if +// `divisor` were its absolute value. OPENSSL_EXPORT int BN_div(BIGNUM *quotient, BIGNUM *rem, const BIGNUM *numerator, const BIGNUM *divisor, BN_CTX *ctx); -// BN_div_word sets |numerator| = |numerator|/|divisor| and returns the +// BN_div_word sets `numerator` = `numerator`/`divisor` and returns the // remainder or (BN_ULONG)-1 on error. OPENSSL_EXPORT BN_ULONG BN_div_word(BIGNUM *numerator, BN_ULONG divisor); -// BN_sqrt sets |*out_sqrt| (which may be the same |BIGNUM| as |in|) to the -// square root of |in|, using |ctx|. It returns one on success or zero on +// BN_sqrt sets `*out_sqrt` (which may be the same `BIGNUM` as `in`) to the +// square root of `in`, using `ctx`. It returns one on success or zero on // error. Negative numbers and non-square numbers will result in an error with // appropriate errors on the error queue. OPENSSL_EXPORT int BN_sqrt(BIGNUM *out_sqrt, const BIGNUM *in, BN_CTX *ctx); @@ -341,213 +343,213 @@ OPENSSL_EXPORT int BN_sqrt(BIGNUM *out_sqrt, const BIGNUM *in, BN_CTX *ctx); // Comparison functions -// BN_cmp returns a value less than, equal to or greater than zero if |a| is -// less than, equal to or greater than |b|, respectively. +// BN_cmp returns a value less than, equal to or greater than zero if `a` is +// less than, equal to or greater than `b`, respectively. OPENSSL_EXPORT int BN_cmp(const BIGNUM *a, const BIGNUM *b); -// BN_cmp_word is like |BN_cmp| except it takes its second argument as a -// |BN_ULONG| instead of a |BIGNUM|. +// BN_cmp_word is like `BN_cmp` except it takes its second argument as a +// `BN_ULONG` instead of a `BIGNUM`. OPENSSL_EXPORT int BN_cmp_word(const BIGNUM *a, BN_ULONG b); // BN_ucmp returns a value less than, equal to or greater than zero if the -// absolute value of |a| is less than, equal to or greater than the absolute -// value of |b|, respectively. +// absolute value of `a` is less than, equal to or greater than the absolute +// value of `b`, respectively. OPENSSL_EXPORT int BN_ucmp(const BIGNUM *a, const BIGNUM *b); -// BN_equal_consttime returns one if |a| is equal to |b|, and zero otherwise. -// It takes an amount of time dependent on the sizes of |a| and |b|, but -// independent of the contents (including the signs) of |a| and |b|. +// BN_equal_consttime returns one if `a` is equal to `b`, and zero otherwise. +// It takes an amount of time dependent on the sizes of `a` and `b`, but +// independent of the contents (including the signs) of `a` and `b`. OPENSSL_EXPORT int BN_equal_consttime(const BIGNUM *a, const BIGNUM *b); -// BN_abs_is_word returns one if the absolute value of |bn| equals |w| and zero +// BN_abs_is_word returns one if the absolute value of `bn` equals `w` and zero // otherwise. OPENSSL_EXPORT int BN_abs_is_word(const BIGNUM *bn, BN_ULONG w); -// BN_is_zero returns one if |bn| is zero and zero otherwise. +// BN_is_zero returns one if `bn` is zero and zero otherwise. OPENSSL_EXPORT int BN_is_zero(const BIGNUM *bn); -// BN_is_one returns one if |bn| equals one and zero otherwise. +// BN_is_one returns one if `bn` equals one and zero otherwise. OPENSSL_EXPORT int BN_is_one(const BIGNUM *bn); -// BN_is_word returns one if |bn| is exactly |w| and zero otherwise. +// BN_is_word returns one if `bn` is exactly `w` and zero otherwise. OPENSSL_EXPORT int BN_is_word(const BIGNUM *bn, BN_ULONG w); -// BN_is_odd returns one if |bn| is odd and zero otherwise. +// BN_is_odd returns one if `bn` is odd and zero otherwise. OPENSSL_EXPORT int BN_is_odd(const BIGNUM *bn); -// BN_is_pow2 returns 1 if |a| is a power of two, and 0 otherwise. +// BN_is_pow2 returns 1 if `a` is a power of two, and 0 otherwise. OPENSSL_EXPORT int BN_is_pow2(const BIGNUM *a); // Bitwise operations. -// BN_lshift sets |r| equal to |a| << n. The |a| and |r| arguments may be the -// same |BIGNUM|. It returns one on success and zero on allocation failure. +// BN_lshift sets `r` equal to `a` << n. The `a` and `r` arguments may be the +// same `BIGNUM`. It returns one on success and zero on allocation failure. OPENSSL_EXPORT int BN_lshift(BIGNUM *r, const BIGNUM *a, int n); -// BN_lshift1 sets |r| equal to |a| << 1, where |r| and |a| may be the same +// BN_lshift1 sets `r` equal to `a` << 1, where `r` and `a` may be the same // pointer. It returns one on success and zero on allocation failure. OPENSSL_EXPORT int BN_lshift1(BIGNUM *r, const BIGNUM *a); -// BN_rshift sets |r| equal to |a| >> n, where |r| and |a| may be the same +// BN_rshift sets `r` equal to `a` >> n, where `r` and `a` may be the same // pointer. It returns one on success and zero on allocation failure. OPENSSL_EXPORT int BN_rshift(BIGNUM *r, const BIGNUM *a, int n); -// BN_rshift1 sets |r| equal to |a| >> 1, where |r| and |a| may be the same +// BN_rshift1 sets `r` equal to `a` >> 1, where `r` and `a` may be the same // pointer. It returns one on success and zero on allocation failure. OPENSSL_EXPORT int BN_rshift1(BIGNUM *r, const BIGNUM *a); -// BN_set_bit sets the |n|th, least-significant bit in |a|. For example, if |a| +// BN_set_bit sets the `n`th, least-significant bit in `a`. For example, if `a` // is 2 then setting bit zero will make it 3. It returns one on success or zero // on allocation failure. OPENSSL_EXPORT int BN_set_bit(BIGNUM *a, int n); -// BN_clear_bit clears the |n|th, least-significant bit in |a|. For example, if -// |a| is 3, clearing bit zero will make it two. It returns one on success or +// BN_clear_bit clears the `n`th, least-significant bit in `a`. For example, if +// `a` is 3, clearing bit zero will make it two. It returns one on success or // zero on allocation failure. OPENSSL_EXPORT int BN_clear_bit(BIGNUM *a, int n); -// BN_is_bit_set returns one if the |n|th least-significant bit in |a| exists +// BN_is_bit_set returns one if the `n`th least-significant bit in `a` exists // and is set. Otherwise, it returns zero. OPENSSL_EXPORT int BN_is_bit_set(const BIGNUM *a, int n); -// BN_mask_bits truncates |a| so that it is only |n| bits long. It returns one -// on success or zero if |n| is negative. +// BN_mask_bits truncates `a` so that it is only `n` bits long. It returns one +// on success or zero if `n` is negative. // -// This differs from OpenSSL which additionally returns zero if |a|'s word -// length is less than or equal to |n|, rounded down to a number of words. Note +// This differs from OpenSSL which additionally returns zero if `a`'s word +// length is less than or equal to `n`, rounded down to a number of words. Note // word size is platform-dependent, so this behavior is also difficult to rely // on in OpenSSL and not very useful. OPENSSL_EXPORT int BN_mask_bits(BIGNUM *a, int n); -// BN_count_low_zero_bits returns the number of low-order zero bits in |bn|, or -// the number of factors of two which divide it. It returns zero if |bn| is +// BN_count_low_zero_bits returns the number of low-order zero bits in `bn`, or +// the number of factors of two which divide it. It returns zero if `bn` is // zero. OPENSSL_EXPORT int BN_count_low_zero_bits(const BIGNUM *bn); // Modulo arithmetic. -// BN_mod_word returns |a| mod |w| or (BN_ULONG)-1 on error. +// BN_mod_word returns `a` mod `w` or (BN_ULONG)-1 on error. OPENSSL_EXPORT BN_ULONG BN_mod_word(const BIGNUM *a, BN_ULONG w); -// BN_mod_pow2 sets |r| = |a| mod 2^|e|. It returns 1 on success and +// BN_mod_pow2 sets `r` = `a` mod 2^`e`. It returns 1 on success and // 0 on error. OPENSSL_EXPORT int BN_mod_pow2(BIGNUM *r, const BIGNUM *a, size_t e); -// BN_nnmod_pow2 sets |r| = |a| mod 2^|e| where |r| is always positive. +// BN_nnmod_pow2 sets `r` = `a` mod 2^`e` where `r` is always positive. // It returns 1 on success and 0 on error. OPENSSL_EXPORT int BN_nnmod_pow2(BIGNUM *r, const BIGNUM *a, size_t e); -// BN_mod is a helper macro that calls |BN_div| and discards the quotient. +// BN_mod is a helper macro that calls `BN_div` and discards the quotient. #define BN_mod(rem, numerator, divisor, ctx) \ BN_div(NULL, (rem), (numerator), (divisor), (ctx)) -// BN_nnmod is a non-negative modulo function. It acts like |BN_mod|, but 0 <= -// |rem| < |divisor| is always true. It returns one on success and zero on +// BN_nnmod is a non-negative modulo function. It acts like `BN_mod`, but 0 <= +// `rem` < `divisor` is always true. It returns one on success and zero on // error. OPENSSL_EXPORT int BN_nnmod(BIGNUM *rem, const BIGNUM *numerator, const BIGNUM *divisor, BN_CTX *ctx); -// BN_mod_add sets |r| = |a| + |b| mod |m|. It returns one on success and zero +// BN_mod_add sets `r` = `a` + `b` mod `m`. It returns one on success and zero // on error. OPENSSL_EXPORT int BN_mod_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m, BN_CTX *ctx); -// BN_mod_add_quick acts like |BN_mod_add| but requires that |a| and |b| be -// non-negative and less than |m|. +// BN_mod_add_quick acts like `BN_mod_add` but requires that `a` and `b` be +// non-negative and less than `m`. OPENSSL_EXPORT int BN_mod_add_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m); -// BN_mod_sub sets |r| = |a| - |b| mod |m|. It returns one on success and zero +// BN_mod_sub sets `r` = `a` - `b` mod `m`. It returns one on success and zero // on error. OPENSSL_EXPORT int BN_mod_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m, BN_CTX *ctx); -// BN_mod_sub_quick acts like |BN_mod_sub| but requires that |a| and |b| be -// non-negative and less than |m|. +// BN_mod_sub_quick acts like `BN_mod_sub` but requires that `a` and `b` be +// non-negative and less than `m`. OPENSSL_EXPORT int BN_mod_sub_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m); -// BN_mod_mul sets |r| = |a|*|b| mod |m|. It returns one on success and zero +// BN_mod_mul sets `r` = `a`*`b` mod `m`. It returns one on success and zero // on error. OPENSSL_EXPORT int BN_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m, BN_CTX *ctx); -// BN_mod_sqr sets |r| = |a|^2 mod |m|. It returns one on success and zero +// BN_mod_sqr sets `r` = `a`^2 mod `m`. It returns one on success and zero // on error. OPENSSL_EXPORT int BN_mod_sqr(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx); -// BN_mod_lshift sets |r| = (|a| << n) mod |m|, where |r| and |a| may be the +// BN_mod_lshift sets `r` = (`a` << n) mod `m`, where `r` and `a` may be the // same pointer. It returns one on success and zero on error. OPENSSL_EXPORT int BN_mod_lshift(BIGNUM *r, const BIGNUM *a, int n, const BIGNUM *m, BN_CTX *ctx); -// BN_mod_lshift_quick acts like |BN_mod_lshift| but requires that |a| be -// non-negative and less than |m|. +// BN_mod_lshift_quick acts like `BN_mod_lshift` but requires that `a` be +// non-negative and less than `m`. OPENSSL_EXPORT int BN_mod_lshift_quick(BIGNUM *r, const BIGNUM *a, int n, const BIGNUM *m); -// BN_mod_lshift1 sets |r| = (|a| << 1) mod |m|, where |r| and |a| may be the +// BN_mod_lshift1 sets `r` = (`a` << 1) mod `m`, where `r` and `a` may be the // same pointer. It returns one on success and zero on error. OPENSSL_EXPORT int BN_mod_lshift1(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx); -// BN_mod_lshift1_quick acts like |BN_mod_lshift1| but requires that |a| be -// non-negative and less than |m|. +// BN_mod_lshift1_quick acts like `BN_mod_lshift1` but requires that `a` be +// non-negative and less than `m`. OPENSSL_EXPORT int BN_mod_lshift1_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *m); -// BN_mod_sqrt returns a newly-allocated |BIGNUM|, r, such that -// r^2 == a (mod p). It returns NULL on error or if |a| is not a square mod |p|. -// In the latter case, it will add |BN_R_NOT_A_SQUARE| to the error queue. -// If |a| is a square and |p| > 2, there are two possible square roots. This +// BN_mod_sqrt returns a newly-allocated `BIGNUM`, r, such that +// r^2 == a (mod p). It returns NULL on error or if `a` is not a square mod `p`. +// In the latter case, it will add `BN_R_NOT_A_SQUARE` to the error queue. +// If `a` is a square and `p` > 2, there are two possible square roots. This // function may return either and may even select one non-deterministically. // -// If |in| is non-NULL, the function, instead of allocating the result, stores -// the result in |in| and returns |in| on success or NULL on failure. +// If `in` is non-NULL, the function, instead of allocating the result, stores +// the result in `in` and returns `in` on success or NULL on failure. // -// This function only works if |p| is a prime. If |p| is composite, it may fail +// This function only works if `p` is a prime. If `p` is composite, it may fail // or return an arbitrary value. Callers should not pass attacker-controlled -// values of |p|. +// values of `p`. OPENSSL_EXPORT BIGNUM *BN_mod_sqrt(BIGNUM *in, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx); // Random and prime number generation. -// The following are values for the |top| parameter of |BN_rand|. +// The following are values for the `top` parameter of `BN_rand`. #define BN_RAND_TOP_ANY (-1) #define BN_RAND_TOP_ONE 0 #define BN_RAND_TOP_TWO 1 -// The following are values for the |bottom| parameter of |BN_rand|. +// The following are values for the `bottom` parameter of `BN_rand`. #define BN_RAND_BOTTOM_ANY 0 #define BN_RAND_BOTTOM_ODD 1 -// BN_rand sets |rnd| to a random number of length |bits|. It returns one on +// BN_rand sets `rnd` to a random number of length `bits`. It returns one on // success and zero otherwise. // -// |top| must be one of the |BN_RAND_TOP_*| values. If |BN_RAND_TOP_ONE|, the -// most-significant bit, if any, will be set. If |BN_RAND_TOP_TWO|, the two -// most significant bits, if any, will be set. If |BN_RAND_TOP_ANY|, no extra -// action will be taken and |BN_num_bits(rnd)| may not equal |bits| if the most +// `top` must be one of the `BN_RAND_TOP_*` values. If `BN_RAND_TOP_ONE`, the +// most-significant bit, if any, will be set. If `BN_RAND_TOP_TWO`, the two +// most significant bits, if any, will be set. If `BN_RAND_TOP_ANY`, no extra +// action will be taken and `BN_num_bits(rnd)` may not equal `bits` if the most // significant bits randomly ended up as zeros. // -// |bottom| must be one of the |BN_RAND_BOTTOM_*| values. If -// |BN_RAND_BOTTOM_ODD|, the least-significant bit, if any, will be set. If -// |BN_RAND_BOTTOM_ANY|, no extra action will be taken. +// `bottom` must be one of the `BN_RAND_BOTTOM_*` values. If +// `BN_RAND_BOTTOM_ODD`, the least-significant bit, if any, will be set. If +// `BN_RAND_BOTTOM_ANY`, no extra action will be taken. OPENSSL_EXPORT int BN_rand(BIGNUM *rnd, int bits, int top, int bottom); -// BN_pseudo_rand is an alias for |BN_rand|. +// BN_pseudo_rand is an alias for `BN_rand`. OPENSSL_EXPORT int BN_pseudo_rand(BIGNUM *rnd, int bits, int top, int bottom); -// BN_rand_range is equivalent to |BN_rand_range_ex| with |min_inclusive| set -// to zero and |max_exclusive| set to |range|. +// BN_rand_range is equivalent to `BN_rand_range_ex` with `min_inclusive` set +// to zero and `max_exclusive` set to `range`. OPENSSL_EXPORT int BN_rand_range(BIGNUM *rnd, const BIGNUM *range); -// BN_rand_range_ex sets |rnd| to a random value in +// BN_rand_range_ex sets `rnd` to a random value in // [min_inclusive..max_exclusive). It returns one on success and zero // otherwise. OPENSSL_EXPORT int BN_rand_range_ex(BIGNUM *r, BN_ULONG min_inclusive, @@ -559,12 +561,12 @@ OPENSSL_EXPORT int BN_pseudo_rand_range(BIGNUM *rnd, const BIGNUM *range); #define BN_GENCB_GENERATED 0 #define BN_GENCB_PRIME_TEST 1 -// bn_gencb_st, or |BN_GENCB|, holds a callback function that is used by +// bn_gencb_st, or `BN_GENCB`, holds a callback function that is used by // generation functions that can take a very long time to complete. Use -// |BN_GENCB_set| to initialise a |BN_GENCB| structure. +// `BN_GENCB_set` to initialise a `BN_GENCB` structure. // -// The callback receives the address of that |BN_GENCB| structure as its last -// argument and the user is free to put an arbitrary pointer in |arg|. The other +// The callback receives the address of that `BN_GENCB` structure as its last +// argument and the user is free to put an arbitrary pointer in `arg`. The other // arguments are set as follows: // - event=BN_GENCB_GENERATED, n=i: after generating the i'th possible prime // number. @@ -582,57 +584,57 @@ struct bn_gencb_st { int (*callback)(int event, int n, struct bn_gencb_st *); }; -// BN_GENCB_new returns a newly-allocated |BN_GENCB| object, or NULL on -// allocation failure. The result must be released with |BN_GENCB_free| when +// BN_GENCB_new returns a newly-allocated `BN_GENCB` object, or NULL on +// allocation failure. The result must be released with `BN_GENCB_free` when // done. OPENSSL_EXPORT BN_GENCB *BN_GENCB_new(void); -// BN_GENCB_free releases memory associated with |callback|. +// BN_GENCB_free releases memory associated with `callback`. OPENSSL_EXPORT void BN_GENCB_free(BN_GENCB *callback); -// BN_GENCB_set configures |callback| to call |f| and sets |callout->arg| to -// |arg|. +// BN_GENCB_set configures `callback` to call `f` and sets `callout->arg` to +// `arg`. OPENSSL_EXPORT void BN_GENCB_set(BN_GENCB *callback, int (*f)(int event, int n, BN_GENCB *), void *arg); -// BN_GENCB_call calls |callback|, if not NULL, and returns the return value of -// the callback, or 1 if |callback| is NULL. +// BN_GENCB_call calls `callback`, if not NULL, and returns the return value of +// the callback, or 1 if `callback` is NULL. OPENSSL_EXPORT int BN_GENCB_call(BN_GENCB *callback, int event, int n); -// BN_GENCB_get_arg returns |callback->arg|. +// BN_GENCB_get_arg returns `callback->arg`. OPENSSL_EXPORT void *BN_GENCB_get_arg(const BN_GENCB *callback); -// BN_generate_prime_ex sets |ret| to a prime number of |bits| length. If safe +// BN_generate_prime_ex sets `ret` to a prime number of `bits` length. If safe // is non-zero then the prime will be such that (ret-1)/2 is also a prime. // (This is needed for Diffie-Hellman groups to ensure that the only subgroups // are of size 2 and (p-1)/2.). // -// If |add| is not NULL, the prime will fulfill the condition |ret| % |add| == -// |rem| in order to suit a given generator. (If |rem| is NULL then |ret| % -// |add| == 1.) +// If `add` is not NULL, the prime will fulfill the condition `ret` % `add` == +// `rem` in order to suit a given generator. (If `rem` is NULL then `ret` % +// `add` == 1.) // -// If |cb| is not NULL, it will be called during processing to give an -// indication of progress. See the comments for |BN_GENCB|. It returns one on +// If `cb` is not NULL, it will be called during processing to give an +// indication of progress. See the comments for `BN_GENCB`. It returns one on // success and zero otherwise. OPENSSL_EXPORT int BN_generate_prime_ex(BIGNUM *ret, int bits, int safe, const BIGNUM *add, const BIGNUM *rem, BN_GENCB *cb); -// BN_prime_checks_for_validation can be used as the |checks| argument to the +// BN_prime_checks_for_validation can be used as the `checks` argument to the // primarily testing functions when validating an externally-supplied candidate // prime. It gives a false positive rate of at most 2^{-128}. (The worst case // false positive rate for a single iteration is 1/4 per // https://eprint.iacr.org/2018/749. (1/4)^64 = 2^{-128}.) #define BN_prime_checks_for_validation 64 -// BN_prime_checks_for_generation can be used as the |checks| argument to the +// BN_prime_checks_for_generation can be used as the `checks` argument to the // primality testing functions when generating random primes. It gives a false // positive rate at most the security level of the corresponding RSA key size. // // Note this value only performs enough checks if the candidate prime was // selected randomly. If validating an externally-supplied candidate, especially -// one that may be selected adversarially, use |BN_prime_checks_for_validation| +// one that may be selected adversarially, use `BN_prime_checks_for_validation` // instead. #define BN_prime_checks_for_generation 0 @@ -643,34 +645,34 @@ enum bn_primality_result_t { bn_non_prime_power_composite, }; -// BN_enhanced_miller_rabin_primality_test tests whether |w| is probably a prime +// BN_enhanced_miller_rabin_primality_test tests whether `w` is probably a prime // number using the Enhanced Miller-Rabin Test (FIPS 186-5 B.3.2) with -// |checks| iterations and returns the result in |out_result|. Enhanced +// `checks` iterations and returns the result in `out_result`. Enhanced // Miller-Rabin tests primality for odd integers greater than 3, returning -// |bn_probably_prime| if the number is probably prime, -// |bn_non_prime_power_composite| if the number is a composite that is not the -// power of a single prime, and |bn_composite| otherwise. It returns one on -// success and zero on failure. If |cb| is not NULL, then it is called during +// `bn_probably_prime` if the number is probably prime, +// `bn_non_prime_power_composite` if the number is a composite that is not the +// power of a single prime, and `bn_composite` otherwise. It returns one on +// success and zero on failure. If `cb` is not NULL, then it is called during // each iteration of the primality test. // -// See |BN_prime_checks_for_validation| and |BN_prime_checks_for_generation| for -// recommended values of |checks|. +// See `BN_prime_checks_for_validation` and `BN_prime_checks_for_generation` for +// recommended values of `checks`. OPENSSL_EXPORT int BN_enhanced_miller_rabin_primality_test( enum bn_primality_result_t *out_result, const BIGNUM *w, int checks, BN_CTX *ctx, BN_GENCB *cb); -// BN_primality_test sets |*is_probably_prime| to one if |candidate| is +// BN_primality_test sets `*is_probably_prime` to one if `candidate` is // probably a prime number by the Miller-Rabin test or zero if it's certainly // not. // -// If |do_trial_division| is non-zero then |candidate| will be tested against a +// If `do_trial_division` is non-zero then `candidate` will be tested against a // list of small primes before Miller-Rabin tests. The probability of this // function returning a false positive is at most 2^{2*checks}. See -// |BN_prime_checks_for_validation| and |BN_prime_checks_for_generation| for -// recommended values of |checks|. +// `BN_prime_checks_for_validation` and `BN_prime_checks_for_generation` for +// recommended values of `checks`. // -// If |cb| is not NULL then it is called during the checking process. See the -// comment above |BN_GENCB|. +// If `cb` is not NULL then it is called during the checking process. See the +// comment above `BN_GENCB`. // // The function returns one on success and zero on error. OPENSSL_EXPORT int BN_primality_test(int *is_probably_prime, @@ -678,72 +680,72 @@ OPENSSL_EXPORT int BN_primality_test(int *is_probably_prime, BN_CTX *ctx, int do_trial_division, BN_GENCB *cb); -// BN_is_prime_fasttest_ex returns one if |candidate| is probably a prime +// BN_is_prime_fasttest_ex returns one if `candidate` is probably a prime // number by the Miller-Rabin test, zero if it's certainly not and -1 on error. // -// If |do_trial_division| is non-zero then |candidate| will be tested against a +// If `do_trial_division` is non-zero then `candidate` will be tested against a // list of small primes before Miller-Rabin tests. The probability of this -// function returning one when |candidate| is composite is at most 2^{2*checks}. -// See |BN_prime_checks_for_validation| and |BN_prime_checks_for_generation| for -// recommended values of |checks|. +// function returning one when `candidate` is composite is at most 2^{2*checks}. +// See `BN_prime_checks_for_validation` and `BN_prime_checks_for_generation` for +// recommended values of `checks`. // -// If |cb| is not NULL then it is called during the checking process. See the -// comment above |BN_GENCB|. +// If `cb` is not NULL then it is called during the checking process. See the +// comment above `BN_GENCB`. // -// WARNING: deprecated. Use |BN_primality_test|. +// WARNING: deprecated. Use `BN_primality_test`. OPENSSL_EXPORT int BN_is_prime_fasttest_ex(const BIGNUM *candidate, int checks, BN_CTX *ctx, int do_trial_division, BN_GENCB *cb); -// BN_is_prime_ex acts the same as |BN_is_prime_fasttest_ex| with -// |do_trial_division| set to zero. +// BN_is_prime_ex acts the same as `BN_is_prime_fasttest_ex` with +// `do_trial_division` set to zero. // -// WARNING: deprecated: Use |BN_primality_test|. +// WARNING: deprecated: Use `BN_primality_test`. OPENSSL_EXPORT int BN_is_prime_ex(const BIGNUM *candidate, int checks, BN_CTX *ctx, BN_GENCB *cb); // Number theory functions -// BN_gcd sets |r| = gcd(|a|, |b|). It returns one on success and zero +// BN_gcd sets `r` = gcd(`a`, `b`). It returns one on success and zero // otherwise. OPENSSL_EXPORT int BN_gcd(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx); -// BN_mod_inverse sets |out| equal to |a|^-1, mod |n|. If |out| is NULL, a +// BN_mod_inverse sets `out` equal to `a`^-1, mod `n`. If `out` is NULL, a // fresh BIGNUM is allocated. It returns the result or NULL on error. // -// If |n| is even then the operation is performed using an algorithm that avoids +// If `n` is even then the operation is performed using an algorithm that avoids // some branches but which isn't constant-time. This function shouldn't be used -// for secret values; use |BN_mod_inverse_blinded| instead. Or, if |n| is +// for secret values; use `BN_mod_inverse_blinded` instead. Or, if `n` is // guaranteed to be prime, use -// |BN_mod_exp_mont_consttime(out, a, m_minus_2, m, ctx, m_mont)|, taking +// `BN_mod_exp_mont_consttime(out, a, m_minus_2, m, ctx, m_mont)`, taking // advantage of Fermat's Little Theorem. OPENSSL_EXPORT BIGNUM *BN_mod_inverse(BIGNUM *out, const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx); -// BN_mod_inverse_blinded sets |out| equal to |a|^-1, mod |n|, where |n| is the -// Montgomery modulus for |mont|. |a| must be non-negative and must be less -// than |n|. |n| must be greater than 1. |a| is blinded (masked by a random +// BN_mod_inverse_blinded sets `out` equal to `a`^-1, mod `n`, where `n` is the +// Montgomery modulus for `mont`. `a` must be non-negative and must be less +// than `n`. `n` must be greater than 1. `a` is blinded (masked by a random // value) to protect it against side-channel attacks. On failure, if the failure -// was caused by |a| having no inverse mod |n| then |*out_no_inverse| will be +// was caused by `a` having no inverse mod `n` then `*out_no_inverse` will be // set to one; otherwise it will be set to zero. // -// Note this function may incorrectly report |a| has no inverse if the random -// blinding value has no inverse. It should only be used when |n| has few +// Note this function may incorrectly report `a` has no inverse if the random +// blinding value has no inverse. It should only be used when `n` has few // non-invertible elements, such as an RSA modulus. OPENSSL_EXPORT int BN_mod_inverse_blinded(BIGNUM *out, int *out_no_inverse, const BIGNUM *a, const BN_MONT_CTX *mont, BN_CTX *ctx); -// BN_mod_inverse_odd sets |out| equal to |a|^-1, mod |n|. |a| must be -// non-negative and must be less than |n|. |n| must be odd. This function -// shouldn't be used for secret values; use |BN_mod_inverse_blinded| instead. -// Or, if |n| is guaranteed to be prime, use -// |BN_mod_exp_mont_consttime(out, a, m_minus_2, m, ctx, m_mont)|, taking +// BN_mod_inverse_odd sets `out` equal to `a`^-1, mod `n`. `a` must be +// non-negative and must be less than `n`. `n` must be odd. This function +// shouldn't be used for secret values; use `BN_mod_inverse_blinded` instead. +// Or, if `n` is guaranteed to be prime, use +// `BN_mod_exp_mont_consttime(out, a, m_minus_2, m, ctx, m_mont)`, taking // advantage of Fermat's Little Theorem. It returns one on success or zero on -// failure. On failure, if the failure was caused by |a| having no inverse mod -// |n| then |*out_no_inverse| will be set to one; otherwise it will be set to +// failure. On failure, if the failure was caused by `a` having no inverse mod +// `n` then `*out_no_inverse` will be set to one; otherwise it will be set to // zero. int BN_mod_inverse_odd(BIGNUM *out, int *out_no_inverse, const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx); @@ -754,41 +756,41 @@ int BN_mod_inverse_odd(BIGNUM *out, int *out_no_inverse, const BIGNUM *a, // BN_MONT_CTX contains the precomputed values needed to work in a specific // Montgomery domain. -// BN_MONT_CTX_new_for_modulus returns a fresh |BN_MONT_CTX| given the modulus, -// |mod| or NULL on error. Note this function assumes |mod| is public. +// BN_MONT_CTX_new_for_modulus returns a fresh `BN_MONT_CTX` given the modulus, +// `mod` or NULL on error. Note this function assumes `mod` is public. OPENSSL_EXPORT BN_MONT_CTX *BN_MONT_CTX_new_for_modulus(const BIGNUM *mod, BN_CTX *ctx); -// BN_MONT_CTX_new_consttime behaves like |BN_MONT_CTX_new_for_modulus| but -// treats |mod| as secret. +// BN_MONT_CTX_new_consttime behaves like `BN_MONT_CTX_new_for_modulus` but +// treats `mod` as secret. OPENSSL_EXPORT BN_MONT_CTX *BN_MONT_CTX_new_consttime(const BIGNUM *mod, BN_CTX *ctx); -// BN_MONT_CTX_free frees memory associated with |mont|. +// BN_MONT_CTX_free frees memory associated with `mont`. OPENSSL_EXPORT void BN_MONT_CTX_free(BN_MONT_CTX *mont); -// BN_MONT_CTX_copy sets |to| equal to |from|. It returns |to| on success or +// BN_MONT_CTX_copy sets `to` equal to `from`. It returns `to` on success or // NULL on error. OPENSSL_EXPORT BN_MONT_CTX *BN_MONT_CTX_copy(BN_MONT_CTX *to, const BN_MONT_CTX *from); -// BN_to_montgomery sets |ret| equal to |a| in the Montgomery domain. |a| is -// assumed to be in the range [0, n), where |n| is the Montgomery modulus. It +// BN_to_montgomery sets `ret` equal to `a` in the Montgomery domain. `a` is +// assumed to be in the range [0, n), where `n` is the Montgomery modulus. It // returns one on success or zero on error. OPENSSL_EXPORT int BN_to_montgomery(BIGNUM *ret, const BIGNUM *a, const BN_MONT_CTX *mont, BN_CTX *ctx); -// BN_from_montgomery sets |ret| equal to |a| * R^-1, i.e. translates values out -// of the Montgomery domain. |a| is assumed to be in the range [0, n*R), where -// |n| is the Montgomery modulus. Note n < R, so inputs in the range [0, n*n) +// BN_from_montgomery sets `ret` equal to `a` * R^-1, i.e. translates values out +// of the Montgomery domain. `a` is assumed to be in the range [0, n*R), where +// `n` is the Montgomery modulus. Note n < R, so inputs in the range [0, n*n) // are valid. This function returns one on success or zero on error. OPENSSL_EXPORT int BN_from_montgomery(BIGNUM *ret, const BIGNUM *a, const BN_MONT_CTX *mont, BN_CTX *ctx); -// BN_mod_mul_montgomery set |r| equal to |a| * |b|, in the Montgomery domain. -// Both |a| and |b| must already be in the Montgomery domain (by -// |BN_to_montgomery|). In particular, |a| and |b| are assumed to be in the -// range [0, n), where |n| is the Montgomery modulus. It returns one on success +// BN_mod_mul_montgomery set `r` equal to `a` * `b`, in the Montgomery domain. +// Both `a` and `b` must already be in the Montgomery domain (by +// `BN_to_montgomery`). In particular, `a` and `b` are assumed to be in the +// range [0, n), where `n` is the Montgomery modulus. It returns one on success // or zero on error. OPENSSL_EXPORT int BN_mod_mul_montgomery(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, @@ -797,27 +799,27 @@ OPENSSL_EXPORT int BN_mod_mul_montgomery(BIGNUM *r, const BIGNUM *a, // Exponentiation. -// BN_exp sets |r| equal to |a|^{|p|}. It does so with a square-and-multiply +// BN_exp sets `r` equal to `a`^{`p`}. It does so with a square-and-multiply // algorithm that leaks side-channel information. It returns one on success or // zero otherwise. OPENSSL_EXPORT int BN_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx); -// BN_mod_exp sets |r| equal to |a|^{|p|} mod |m|. It does so with the best +// BN_mod_exp sets `r` equal to `a`^{`p`} mod `m`. It does so with the best // algorithm for the values provided. It returns one on success or zero -// otherwise. The |BN_mod_exp_mont_consttime| variant must be used if the +// otherwise. The `BN_mod_exp_mont_consttime` variant must be used if the // exponent is secret. OPENSSL_EXPORT int BN_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx); -// BN_mod_exp_mont behaves like |BN_mod_exp| but treats |a| as secret and -// requires 0 <= |a| < |m|. +// BN_mod_exp_mont behaves like `BN_mod_exp` but treats `a` as secret and +// requires 0 <= `a` < `m`. OPENSSL_EXPORT int BN_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, const BN_MONT_CTX *mont); -// BN_mod_exp_mont_consttime behaves like |BN_mod_exp| but treats |a|, |p|, and -// |m| as secret and requires 0 <= |a| < |m|. +// BN_mod_exp_mont_consttime behaves like `BN_mod_exp` but treats `a`, `p`, and +// `m` as secret and requires 0 <= `a` < `m`. OPENSSL_EXPORT int BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, @@ -826,24 +828,24 @@ OPENSSL_EXPORT int BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a, // Deprecated functions -// BN_bn2mpi serialises the value of |in| to |out|, using a format that consists +// BN_bn2mpi serialises the value of `in` to `out`, using a format that consists // of the number's length in bytes represented as a 4-byte big-endian number, // and the number itself in big-endian format, where the most significant bit // signals a negative number. (The representation of numbers with the MSB set is -// prefixed with null byte). |out| must have sufficient space available; to -// find the needed amount of space, call the function with |out| set to NULL. +// prefixed with null byte). `out` must have sufficient space available; to +// find the needed amount of space, call the function with `out` set to NULL. OPENSSL_EXPORT size_t BN_bn2mpi(const BIGNUM *in, uint8_t *out); -// BN_mpi2bn parses |len| bytes from |in| and returns the resulting value. The -// bytes at |in| are expected to be in the format emitted by |BN_bn2mpi|. +// BN_mpi2bn parses `len` bytes from `in` and returns the resulting value. The +// bytes at `in` are expected to be in the format emitted by `BN_bn2mpi`. // -// If |out| is NULL then a fresh |BIGNUM| is allocated and returned, otherwise -// |out| is reused and returned. On error, NULL is returned and the error queue +// If `out` is NULL then a fresh `BIGNUM` is allocated and returned, otherwise +// `out` is reused and returned. On error, NULL is returned and the error queue // is updated. OPENSSL_EXPORT BIGNUM *BN_mpi2bn(const uint8_t *in, size_t len, BIGNUM *out); -// BN_mod_exp_mont_word is like |BN_mod_exp_mont| except that the base |a| is -// given as a |BN_ULONG| instead of a |BIGNUM *|. It returns one on success +// BN_mod_exp_mont_word is like `BN_mod_exp_mont` except that the base `a` is +// given as a `BN_ULONG` instead of a `BIGNUM *`. It returns one on success // or zero otherwise. OPENSSL_EXPORT int BN_mod_exp_mont_word(BIGNUM *r, BN_ULONG a, const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, @@ -856,67 +858,67 @@ OPENSSL_EXPORT int BN_mod_exp2_mont(BIGNUM *r, const BIGNUM *a1, const BIGNUM *p2, const BIGNUM *m, BN_CTX *ctx, const BN_MONT_CTX *mont); -// BN_MONT_CTX_new returns a fresh |BN_MONT_CTX| or NULL on allocation failure. -// Use |BN_MONT_CTX_new_for_modulus| instead. +// BN_MONT_CTX_new returns a fresh `BN_MONT_CTX` or NULL on allocation failure. +// Use `BN_MONT_CTX_new_for_modulus` instead. OPENSSL_EXPORT BN_MONT_CTX *BN_MONT_CTX_new(void); -// BN_MONT_CTX_set sets up a Montgomery context given the modulus, |mod|. It -// returns one on success and zero on error. Use |BN_MONT_CTX_new_for_modulus| +// BN_MONT_CTX_set sets up a Montgomery context given the modulus, `mod`. It +// returns one on success and zero on error. Use `BN_MONT_CTX_new_for_modulus` // instead. OPENSSL_EXPORT int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, BN_CTX *ctx); -// BN_bn2binpad behaves like |BN_bn2bin_padded|, but it returns |len| on success +// BN_bn2binpad behaves like `BN_bn2bin_padded`, but it returns `len` on success // and -1 on error. // -// Use |BN_bn2bin_padded| instead. It is |size_t|-clean. +// Use `BN_bn2bin_padded` instead. It is `size_t`-clean. OPENSSL_EXPORT int BN_bn2binpad(const BIGNUM *in, uint8_t *out, int len); -// BN_bn2lebinpad behaves like |BN_bn2le_padded|, but it returns |len| on +// BN_bn2lebinpad behaves like `BN_bn2le_padded`, but it returns `len` on // success and -1 on error. // -// Use |BN_bn2le_padded| instead. It is |size_t|-clean. +// Use `BN_bn2le_padded` instead. It is `size_t`-clean. OPENSSL_EXPORT int BN_bn2lebinpad(const BIGNUM *in, uint8_t *out, int len); -// BN_prime_checks is a deprecated alias for |BN_prime_checks_for_validation|. -// Use |BN_prime_checks_for_generation| or |BN_prime_checks_for_validation| -// instead. (This defaults to the |_for_validation| value in order to be +// BN_prime_checks is a deprecated alias for `BN_prime_checks_for_validation`. +// Use `BN_prime_checks_for_generation` or `BN_prime_checks_for_validation` +// instead. (This defaults to the `_for_validation` value in order to be // conservative.) #define BN_prime_checks BN_prime_checks_for_validation -// BN_secure_new calls |BN_new|. +// BN_secure_new calls `BN_new`. OPENSSL_EXPORT BIGNUM *BN_secure_new(void); -// BN_le2bn calls |BN_lebin2bn|. +// BN_le2bn calls `BN_lebin2bn`. OPENSSL_EXPORT BIGNUM *BN_le2bn(const uint8_t *in, size_t len, BIGNUM *ret); // Private functions struct bignum_st { - // d is a pointer to an array of |width| |BN_BITS2|-bit chunks in + // d is a pointer to an array of `width` `BN_BITS2`-bit chunks in // little-endian order. This stores the absolute value of the number. BN_ULONG *d; - // width is the number of elements of |d| which are valid. This value is not - // necessarily minimal; the most-significant words of |d| may be zero. - // |width| determines a potentially loose upper-bound on the absolute value - // of the |BIGNUM|. + // width is the number of elements of `d` which are valid. This value is not + // necessarily minimal; the most-significant words of `d` may be zero. + // `width` determines a potentially loose upper-bound on the absolute value + // of the `BIGNUM`. // - // Functions taking |BIGNUM| inputs must compute the same answer for all - // possible widths. |bn_minimal_width|, |bn_set_minimal_width|, and other + // Functions taking `BIGNUM` inputs must compute the same answer for all + // possible widths. `bn_minimal_width`, `bn_set_minimal_width`, and other // helpers may be used to recover the minimal width, provided it is not // secret. If it is secret, use a different algorithm. Functions may output - // minimal or non-minimal |BIGNUM|s depending on secrecy requirements, but + // minimal or non-minimal `BIGNUM`s depending on secrecy requirements, but // those which cause widths to unboundedly grow beyond the minimal value // should be documented such. // - // Note this is different from historical |BIGNUM| semantics. + // Note this is different from historical `BIGNUM` semantics. int width; - // dmax is number of elements of |d| which are allocated. + // dmax is number of elements of `d` which are allocated. int dmax; // neg is one if the number if negative and zero otherwise. int neg; - // flags is a bitmask of |BN_FLG_*| values + // flags is a bitmask of `BN_FLG_*` values int flags; }; @@ -924,7 +926,7 @@ OPENSSL_EXPORT unsigned BN_num_bits_word(BN_ULONG l); #define BN_FLG_MALLOCED 0x01 #define BN_FLG_STATIC_DATA 0x02 -// |BN_FLG_CONSTTIME| has been removed and intentionally omitted so code relying +// `BN_FLG_CONSTTIME` has been removed and intentionally omitted so code relying // on it will not compile. Consumers outside BoringSSL should use the // higher-level cryptographic algorithms exposed by other modules. Consumers // within the library should call the appropriate timing-sensitive algorithm diff --git a/third_party/boringssl/src/include/openssl/buf.h b/third_party/boringssl/src/include/openssl/buf.h index 18ae3a100..338beefe5 100644 --- a/third_party/boringssl/src/include/openssl/buf.h +++ b/third_party/boringssl/src/include/openssl/buf.h @@ -25,7 +25,7 @@ extern "C" { // Memory and string functions, see also mem.h. -// buf_mem_st (aka |BUF_MEM|) is a generic buffer object used by OpenSSL. +// buf_mem_st (aka `BUF_MEM`) is a generic buffer object used by OpenSSL. struct buf_mem_st { size_t length; // current number of bytes char *data; @@ -35,45 +35,45 @@ struct buf_mem_st { // BUF_MEM_new creates a new BUF_MEM which has no allocated data buffer. OPENSSL_EXPORT BUF_MEM *BUF_MEM_new(void); -// BUF_MEM_free frees |buf->data| if needed and then frees |buf| itself. +// BUF_MEM_free frees `buf->data` if needed and then frees `buf` itself. OPENSSL_EXPORT void BUF_MEM_free(BUF_MEM *buf); -// BUF_MEM_reserve ensures |buf| has capacity |cap| and allocates memory if +// BUF_MEM_reserve ensures `buf` has capacity `cap` and allocates memory if // needed. It returns one on success and zero on error. OPENSSL_EXPORT int BUF_MEM_reserve(BUF_MEM *buf, size_t cap); -// BUF_MEM_grow ensures that |buf| has length |len| and allocates memory if -// needed. If the length of |buf| increased, the new bytes are filled with -// zeros. It returns the length of |buf|, or zero if there's an error. +// BUF_MEM_grow ensures that `buf` has length `len` and allocates memory if +// needed. If the length of `buf` increased, the new bytes are filled with +// zeros. It returns the length of `buf`, or zero if there's an error. OPENSSL_EXPORT size_t BUF_MEM_grow(BUF_MEM *buf, size_t len); -// BUF_MEM_grow_clean calls |BUF_MEM_grow|. BoringSSL always zeros memory +// BUF_MEM_grow_clean calls `BUF_MEM_grow`. BoringSSL always zeros memory // allocated memory on free. OPENSSL_EXPORT size_t BUF_MEM_grow_clean(BUF_MEM *buf, size_t len); -// BUF_MEM_append appends |in| to |buf|. It returns one on success and zero on +// BUF_MEM_append appends `in` to `buf`. It returns one on success and zero on // error. OPENSSL_EXPORT int BUF_MEM_append(BUF_MEM *buf, const void *in, size_t len); // Deprecated functions. -// BUF_strdup calls |OPENSSL_strdup|. +// BUF_strdup calls `OPENSSL_strdup`. OPENSSL_EXPORT char *BUF_strdup(const char *str); -// BUF_strnlen calls |OPENSSL_strnlen|. +// BUF_strnlen calls `OPENSSL_strnlen`. OPENSSL_EXPORT size_t BUF_strnlen(const char *str, size_t max_len); -// BUF_strndup calls |OPENSSL_strndup|. +// BUF_strndup calls `OPENSSL_strndup`. OPENSSL_EXPORT char *BUF_strndup(const char *str, size_t size); -// BUF_memdup calls |OPENSSL_memdup|. +// BUF_memdup calls `OPENSSL_memdup`. OPENSSL_EXPORT void *BUF_memdup(const void *data, size_t size); -// BUF_strlcpy calls |OPENSSL_strlcpy|. +// BUF_strlcpy calls `OPENSSL_strlcpy`. OPENSSL_EXPORT size_t BUF_strlcpy(char *dst, const char *src, size_t dst_size); -// BUF_strlcat calls |OPENSSL_strlcat|. +// BUF_strlcat calls `OPENSSL_strlcat`. OPENSSL_EXPORT size_t BUF_strlcat(char *dst, const char *src, size_t dst_size); diff --git a/third_party/boringssl/src/include/openssl/bytestring.h b/third_party/boringssl/src/include/openssl/bytestring.h index 3e1cfe5d2..619a37a61 100644 --- a/third_party/boringssl/src/include/openssl/bytestring.h +++ b/third_party/boringssl/src/include/openssl/bytestring.h @@ -54,144 +54,148 @@ struct cbs_st { #endif }; -// CBS_init sets |cbs| to point to |data|. It does not take ownership of -// |data|. +// CBS_init sets `cbs` to point to `data`. It does not take ownership of +// `data`. OPENSSL_INLINE void CBS_init(CBS *cbs, const uint8_t *data, size_t len) { cbs->data = data; cbs->len = len; } -// CBS_skip advances |cbs| by |len| bytes. It returns one on success and zero +// CBS_skip advances `cbs` by `len` bytes. It returns one on success and zero // otherwise. OPENSSL_EXPORT int CBS_skip(CBS *cbs, size_t len); -// CBS_data returns a pointer to the contents of |cbs|. +// CBS_data returns a pointer to the contents of `cbs`. OPENSSL_INLINE const uint8_t *CBS_data(const CBS *cbs) { return cbs->data; } -// CBS_len returns the number of bytes remaining in |cbs|. +// CBS_len returns the number of bytes remaining in `cbs`. OPENSSL_INLINE size_t CBS_len(const CBS *cbs) { return cbs->len; } -// CBS_stow copies the current contents of |cbs| into |*out_ptr| and -// |*out_len|. If |*out_ptr| is not NULL, the contents are freed with +// CBS_stow copies the current contents of `cbs` into `*out_ptr` and +// `*out_len`. If `*out_ptr` is not NULL, the contents are freed with // OPENSSL_free. It returns one on success and zero on allocation failure. On -// success, |*out_ptr| should be freed with OPENSSL_free. If |cbs| is empty, -// |*out_ptr| will be NULL. +// success, `*out_ptr` should be freed with OPENSSL_free. If `cbs` is empty, +// `*out_ptr` will be NULL. OPENSSL_EXPORT int CBS_stow(const CBS *cbs, uint8_t **out_ptr, size_t *out_len); -// CBS_strdup copies the current contents of |cbs| into |*out_ptr| as a -// NUL-terminated C string. If |*out_ptr| is not NULL, the contents are freed +// CBS_strdup copies the current contents of `cbs` into `*out_ptr` as a +// NUL-terminated C string. If `*out_ptr` is not NULL, the contents are freed // with OPENSSL_free. It returns one on success and zero on allocation -// failure. On success, |*out_ptr| should be freed with OPENSSL_free. +// failure. On success, `*out_ptr` should be freed with OPENSSL_free. // -// NOTE: If |cbs| contains NUL bytes, the string will be truncated. Call -// |CBS_contains_zero_byte(cbs)| to check for NUL bytes. +// NOTE: If `cbs` contains NUL bytes, the string will be truncated. Call +// `CBS_contains_zero_byte(cbs)` to check for NUL bytes. OPENSSL_EXPORT int CBS_strdup(const CBS *cbs, char **out_ptr); -// CBS_contains_zero_byte returns one if the current contents of |cbs| contains +// CBS_contains_zero_byte returns one if the current contents of `cbs` contains // a NUL byte and zero otherwise. OPENSSL_EXPORT int CBS_contains_zero_byte(const CBS *cbs); -// CBS_mem_equal compares the current contents of |cbs| with the |len| bytes -// starting at |data|. If they're equal, it returns one, otherwise zero. If the +// CBS_mem_equal compares the current contents of `cbs` with the `len` bytes +// starting at `data`. If they're equal, it returns one, otherwise zero. If the // lengths match, it uses a constant-time comparison. OPENSSL_EXPORT int CBS_mem_equal(const CBS *cbs, const uint8_t *data, size_t len); -// CBS_get_u8 sets |*out| to the next uint8_t from |cbs| and advances |cbs|. It +// CBS_get_u8 sets `*out` to the next uint8_t from `cbs` and advances `cbs`. It // returns one on success and zero on error. OPENSSL_EXPORT int CBS_get_u8(CBS *cbs, uint8_t *out); -// CBS_get_u16 sets |*out| to the next, big-endian uint16_t from |cbs| and -// advances |cbs|. It returns one on success and zero on error. +// CBS_get_u16 sets `*out` to the next, big-endian uint16_t from `cbs` and +// advances `cbs`. It returns one on success and zero on error. OPENSSL_EXPORT int CBS_get_u16(CBS *cbs, uint16_t *out); -// CBS_get_u16le sets |*out| to the next, little-endian uint16_t from |cbs| and -// advances |cbs|. It returns one on success and zero on error. +// CBS_get_u16le sets `*out` to the next, little-endian uint16_t from `cbs` and +// advances `cbs`. It returns one on success and zero on error. OPENSSL_EXPORT int CBS_get_u16le(CBS *cbs, uint16_t *out); -// CBS_get_u24 sets |*out| to the next, big-endian 24-bit value from |cbs| and -// advances |cbs|. It returns one on success and zero on error. +// CBS_get_u24 sets `*out` to the next, big-endian 24-bit value from `cbs` and +// advances `cbs`. It returns one on success and zero on error. OPENSSL_EXPORT int CBS_get_u24(CBS *cbs, uint32_t *out); -// CBS_get_u32 sets |*out| to the next, big-endian uint32_t value from |cbs| -// and advances |cbs|. It returns one on success and zero on error. +// CBS_get_u32 sets `*out` to the next, big-endian uint32_t value from `cbs` +// and advances `cbs`. It returns one on success and zero on error. OPENSSL_EXPORT int CBS_get_u32(CBS *cbs, uint32_t *out); -// CBS_get_u32le sets |*out| to the next, little-endian uint32_t value from -// |cbs| and advances |cbs|. It returns one on success and zero on error. +// CBS_get_u32le sets `*out` to the next, little-endian uint32_t value from +// `cbs` and advances `cbs`. It returns one on success and zero on error. OPENSSL_EXPORT int CBS_get_u32le(CBS *cbs, uint32_t *out); -// CBS_get_u64 sets |*out| to the next, big-endian uint64_t value from |cbs| -// and advances |cbs|. It returns one on success and zero on error. +// CBS_get_u48 sets `*out` to the next, big-endian 48-bit value from `cbs` and +// advances `cbs`. It returns one on success and zero on error. +OPENSSL_EXPORT int CBS_get_u48(CBS *cbs, uint64_t *out); + +// CBS_get_u64 sets `*out` to the next, big-endian uint64_t value from `cbs` +// and advances `cbs`. It returns one on success and zero on error. OPENSSL_EXPORT int CBS_get_u64(CBS *cbs, uint64_t *out); -// CBS_get_u64le sets |*out| to the next, little-endian uint64_t value from -// |cbs| and advances |cbs|. It returns one on success and zero on error. +// CBS_get_u64le sets `*out` to the next, little-endian uint64_t value from +// `cbs` and advances `cbs`. It returns one on success and zero on error. OPENSSL_EXPORT int CBS_get_u64le(CBS *cbs, uint64_t *out); -// CBS_get_last_u8 sets |*out| to the last uint8_t from |cbs| and shortens -// |cbs|. It returns one on success and zero on error. +// CBS_get_last_u8 sets `*out` to the last uint8_t from `cbs` and shortens +// `cbs`. It returns one on success and zero on error. OPENSSL_EXPORT int CBS_get_last_u8(CBS *cbs, uint8_t *out); -// CBS_get_bytes sets |*out| to the next |len| bytes from |cbs| and advances -// |cbs|. It returns one on success and zero on error. +// CBS_get_bytes sets `*out` to the next `len` bytes from `cbs` and advances +// `cbs`. It returns one on success and zero on error. OPENSSL_EXPORT int CBS_get_bytes(CBS *cbs, CBS *out, size_t len); -// CBS_copy_bytes copies the next |len| bytes from |cbs| to |out| and advances -// |cbs|. It returns one on success and zero on error. +// CBS_copy_bytes copies the next `len` bytes from `cbs` to `out` and advances +// `cbs`. It returns one on success and zero on error. OPENSSL_EXPORT int CBS_copy_bytes(CBS *cbs, uint8_t *out, size_t len); -// CBS_get_u8_length_prefixed sets |*out| to the contents of an 8-bit, -// length-prefixed value from |cbs| and advances |cbs| over it. It returns one +// CBS_get_u8_length_prefixed sets `*out` to the contents of an 8-bit, +// length-prefixed value from `cbs` and advances `cbs` over it. It returns one // on success and zero on error. OPENSSL_EXPORT int CBS_get_u8_length_prefixed(CBS *cbs, CBS *out); -// CBS_get_u16_length_prefixed sets |*out| to the contents of a 16-bit, -// big-endian, length-prefixed value from |cbs| and advances |cbs| over it. It +// CBS_get_u16_length_prefixed sets `*out` to the contents of a 16-bit, +// big-endian, length-prefixed value from `cbs` and advances `cbs` over it. It // returns one on success and zero on error. OPENSSL_EXPORT int CBS_get_u16_length_prefixed(CBS *cbs, CBS *out); -// CBS_get_u24_length_prefixed sets |*out| to the contents of a 24-bit, -// big-endian, length-prefixed value from |cbs| and advances |cbs| over it. It +// CBS_get_u24_length_prefixed sets `*out` to the contents of a 24-bit, +// big-endian, length-prefixed value from `cbs` and advances `cbs` over it. It // returns one on success and zero on error. OPENSSL_EXPORT int CBS_get_u24_length_prefixed(CBS *cbs, CBS *out); -// CBS_get_until_first finds the first instance of |c| in |cbs|. If found, it -// sets |*out| to the text before the match, advances |cbs| over it, and returns -// one. Otherwise, it returns zero and leaves |cbs| unmodified. +// CBS_get_until_first finds the first instance of `c` in `cbs`. If found, it +// sets `*out` to the text before the match, advances `cbs` over it, and returns +// one. Otherwise, it returns zero and leaves `cbs` unmodified. OPENSSL_EXPORT int CBS_get_until_first(CBS *cbs, CBS *out, uint8_t c); -// CBS_get_until_first_of finds the first byte in |cbs| matching one of the -// characters in |chars|, which is a NUL-terminated C string. If found, it sets -// |*out| to the text before the match, advances |cbs| over it, and returns one. -// Otherwise, it returns zero and leaves |cbs| unmodified. +// CBS_get_until_first_of finds the first byte in `cbs` matching one of the +// characters in `chars`, which is a NUL-terminated C string. If found, it sets +// `*out` to the text before the match, advances `cbs` over it, and returns one. +// Otherwise, it returns zero and leaves `cbs` unmodified. OPENSSL_EXPORT int CBS_get_until_first_of(CBS *cbs, CBS *out, const char *chars); -// CBS_get_until_first_not_of finds the first byte in |cbs| that does not match -// any of the characters in |chars|, which is a NUL-terminated C string. If -// found, it sets |*out| to the text before the match, advances |cbs| over it, -// and returns one. Otherwise, it returns zero and leaves |cbs| unmodified. +// CBS_get_until_first_not_of finds the first byte in `cbs` that does not match +// any of the characters in `chars`, which is a NUL-terminated C string. If +// found, it sets `*out` to the text before the match, advances `cbs` over it, +// and returns one. Otherwise, it returns zero and leaves `cbs` unmodified. OPENSSL_EXPORT int CBS_get_until_first_not_of(CBS *cbs, CBS *out, const char *chars); -// CBS_get_u64_decimal reads a decimal integer from |cbs| and writes it to -// |*out|. It stops reading at the end of the string, or the first non-digit +// CBS_get_u64_decimal reads a decimal integer from `cbs` and writes it to +// `*out`. It stops reading at the end of the string, or the first non-digit // character. It returns one on success and zero on error. This function behaves -// analogously to |strtoul| except it does not accept empty inputs, leading +// analogously to `strtoul` except it does not accept empty inputs, leading // zeros, or negative values. OPENSSL_EXPORT int CBS_get_u64_decimal(CBS *cbs, uint64_t *out); // Parsing ASN.1 // -// |CBS| may be used to parse DER structures. Rather than using a schema +// `CBS` may be used to parse DER structures. Rather than using a schema // compiler, the following functions act on tag-length-value elements in the // serialization itself. Thus the caller is responsible for looping over a // SEQUENCE, branching on CHOICEs or OPTIONAL fields, checking for trailing // data, and handling explicit vs. implicit tagging. // -// Tags are represented as |CBS_ASN1_TAG| values in memory. The upper few bits +// Tags are represented as `CBS_ASN1_TAG` values in memory. The upper few bits // store the class and constructed bit, and the remaining bits store the tag // number. Note this differs from the DER serialization, to support tag numbers // beyond 31. Consumers must use the constants defined below to decompose or @@ -234,6 +238,7 @@ OPENSSL_EXPORT int CBS_get_u64_decimal(CBS *cbs, uint64_t *out); #define CBS_ASN1_OBJECT 0x6u #define CBS_ASN1_ENUMERATED 0xau #define CBS_ASN1_UTF8STRING 0xcu +#define CBS_ASN1_RELATIVE_OID 0xdu #define CBS_ASN1_SEQUENCE (0x10u | CBS_ASN1_CONSTRUCTED) #define CBS_ASN1_SET (0x11u | CBS_ASN1_CONSTRUCTED) #define CBS_ASN1_NUMERICSTRING 0x12u @@ -249,46 +254,51 @@ OPENSSL_EXPORT int CBS_get_u64_decimal(CBS *cbs, uint64_t *out); #define CBS_ASN1_UNIVERSALSTRING 0x1cu #define CBS_ASN1_BMPSTRING 0x1eu -// CBS_get_asn1 sets |*out| to the contents of DER-encoded, ASN.1 element (not -// including tag and length bytes) and advances |cbs| over it. The ASN.1 -// element must match |tag_value|. It returns one on success and zero +// CBS_get_asn1 sets `*out` to the contents of DER-encoded, ASN.1 element (not +// including tag and length bytes) and advances `cbs` over it. The ASN.1 +// element must match `tag_value`. It returns one on success and zero // on error. OPENSSL_EXPORT int CBS_get_asn1(CBS *cbs, CBS *out, CBS_ASN1_TAG tag_value); -// CBS_get_asn1_element acts like |CBS_get_asn1| but |out| will include the +// CBS_get_asn1_element acts like `CBS_get_asn1` but `out` will include the // ASN.1 header bytes too. OPENSSL_EXPORT int CBS_get_asn1_element(CBS *cbs, CBS *out, CBS_ASN1_TAG tag_value); -// CBS_peek_asn1_tag looks ahead at the next ASN.1 tag and returns one -// if the next ASN.1 element on |cbs| would have tag |tag_value|. If -// |cbs| is empty or the tag does not match, it returns zero. Note: if -// it returns one, CBS_get_asn1 may still fail if the rest of the +// CBS_peek_any_asn1_tag looks ahead at the next ASN.1 tag and returns it. If a +// tag could not be parsed, it returns zero, which is never a valid tag. Note: +// if it returns non-zero, `CBS_get_asn1` may still fail if the rest of the // element is malformed. +OPENSSL_EXPORT CBS_ASN1_TAG CBS_peek_any_asn1_tag(const CBS *cbs); + +// CBS_peek_asn1_tag looks ahead at the next ASN.1 tag and returns one if the +// next ASN.1 element on `cbs` would have tag `tag_value`. If `cbs` is empty or +// the tag does not match, it returns zero. Note: if it returns one, +// `CBS_get_asn1` may still fail if the rest of the element is malformed. OPENSSL_EXPORT int CBS_peek_asn1_tag(const CBS *cbs, CBS_ASN1_TAG tag_value); -// CBS_get_any_asn1 sets |*out| to contain the next ASN.1 element from |*cbs| -// (not including tag and length bytes), sets |*out_tag| to the tag number, and -// advances |*cbs|. It returns one on success and zero on error. Either of |out| -// and |out_tag| may be NULL to ignore the value. +// CBS_get_any_asn1 sets `*out` to contain the next ASN.1 element from `*cbs` +// (not including tag and length bytes), sets `*out_tag` to the tag number, and +// advances `*cbs`. It returns one on success and zero on error. Either of `out` +// and `out_tag` may be NULL to ignore the value. OPENSSL_EXPORT int CBS_get_any_asn1(CBS *cbs, CBS *out, CBS_ASN1_TAG *out_tag); -// CBS_get_any_asn1_element sets |*out| to contain the next ASN.1 element from -// |*cbs| (including header bytes) and advances |*cbs|. It sets |*out_tag| to -// the tag number and |*out_header_len| to the length of the ASN.1 header. Each -// of |out|, |out_tag|, and |out_header_len| may be NULL to ignore the value. +// CBS_get_any_asn1_element sets `*out` to contain the next ASN.1 element from +// `*cbs` (including header bytes) and advances `*cbs`. It sets `*out_tag` to +// the tag number and `*out_header_len` to the length of the ASN.1 header. Each +// of `out`, `out_tag`, and `out_header_len` may be NULL to ignore the value. OPENSSL_EXPORT int CBS_get_any_asn1_element(CBS *cbs, CBS *out, CBS_ASN1_TAG *out_tag, size_t *out_header_len); -// CBS_get_any_ber_asn1_element acts the same as |CBS_get_any_asn1_element| but +// CBS_get_any_ber_asn1_element acts the same as `CBS_get_any_asn1_element` but // also allows indefinite-length elements to be returned and does not enforce -// that lengths are minimal. It sets |*out_indefinite| to one if the length was -// indefinite and zero otherwise. If indefinite, |*out_header_len| and -// |CBS_len(out)| will be equal as only the header is returned (although this is -// also true for empty elements so |*out_indefinite| should be checked). If -// |out_ber_found| is not NULL then it is set to one if any case of invalid DER +// that lengths are minimal. It sets `*out_indefinite` to one if the length was +// indefinite and zero otherwise. If indefinite, `*out_header_len` and +// `CBS_len(out)` will be equal as only the header is returned (although this is +// also true for empty elements so `*out_indefinite` should be checked). If +// `out_ber_found` is not NULL then it is set to one if any case of invalid DER // but valid BER is found, and to zero otherwise. // // This function will not successfully parse an end-of-contents (EOC) as an @@ -300,14 +310,14 @@ OPENSSL_EXPORT int CBS_get_any_ber_asn1_element(CBS *cbs, CBS *out, int *out_ber_found, int *out_indefinite); -// CBS_get_asn1_uint64 gets an ASN.1 INTEGER from |cbs| using |CBS_get_asn1| -// and sets |*out| to its value. It returns one on success and zero on error, +// CBS_get_asn1_uint64 gets an ASN.1 INTEGER from `cbs` using `CBS_get_asn1` +// and sets `*out` to its value. It returns one on success and zero on error, // where error includes the integer being negative, or too large to represent // in 64 bits. OPENSSL_EXPORT int CBS_get_asn1_uint64(CBS *cbs, uint64_t *out); -// CBS_get_asn1_uint64_with_tag gets an ASN.1 INTEGER from |cbs| using -// |CBS_get_asn1| and sets |*out| to its value. |tag| is used to handle to +// CBS_get_asn1_uint64_with_tag gets an ASN.1 INTEGER from `cbs` using +// `CBS_get_asn1` and sets `*out` to its value. `tag` is used to handle to // handle implicitly tagged INTEGER fields. It returns one on success and zero // on error, where error includes the integer being negative, or too large to // represent in 64 bits. @@ -315,35 +325,35 @@ OPENSSL_EXPORT int CBS_get_asn1_uint64_with_tag(CBS *cbs, uint64_t *out, CBS_ASN1_TAG tag); -// CBS_get_asn1_int64 gets an ASN.1 INTEGER from |cbs| using |CBS_get_asn1| -// and sets |*out| to its value. It returns one on success and zero on error, +// CBS_get_asn1_int64 gets an ASN.1 INTEGER from `cbs` using `CBS_get_asn1` +// and sets `*out` to its value. It returns one on success and zero on error, // where error includes the integer being too large to represent in 64 bits. OPENSSL_EXPORT int CBS_get_asn1_int64(CBS *cbs, int64_t *out); -// CBS_get_asn1_int64_with_tag gets an ASN.1 INTEGER from |cbs| using -// |CBS_get_asn1| and sets |*out| to its value. |tag| is used to handle to +// CBS_get_asn1_int64_with_tag gets an ASN.1 INTEGER from `cbs` using +// `CBS_get_asn1` and sets `*out` to its value. `tag` is used to handle to // handle implicitly tagged INTEGER fields. It returns one on success and zero // on error, where error includes the integer being too large to represent in 64 // bits. OPENSSL_EXPORT int CBS_get_asn1_int64_with_tag(CBS *cbs, int64_t *out, CBS_ASN1_TAG tag); -// CBS_get_asn1_bool gets an ASN.1 BOOLEAN from |cbs| and sets |*out| to zero +// CBS_get_asn1_bool gets an ASN.1 BOOLEAN from `cbs` and sets `*out` to zero // or one based on its value. It returns one on success or zero on error. OPENSSL_EXPORT int CBS_get_asn1_bool(CBS *cbs, int *out); -// CBS_get_optional_asn1 gets an optional explicitly-tagged element from |cbs| -// tagged with |tag| and sets |*out| to its contents, or ignores it if |out| is -// NULL. If present and if |out_present| is not NULL, it sets |*out_present| to +// CBS_get_optional_asn1 gets an optional explicitly-tagged element from `cbs` +// tagged with `tag` and sets `*out` to its contents, or ignores it if `out` is +// NULL. If present and if `out_present` is not NULL, it sets `*out_present` to // one, otherwise zero. It returns one on success, whether or not the element // was present, and zero on decode failure. OPENSSL_EXPORT int CBS_get_optional_asn1(CBS *cbs, CBS *out, int *out_present, CBS_ASN1_TAG tag); // CBS_get_optional_asn1_octet_string gets an optional -// explicitly-tagged OCTET STRING from |cbs|. If present, it sets -// |*out| to the string and |*out_present| to one. Otherwise, it sets -// |*out| to empty and |*out_present| to zero. |out_present| may be +// explicitly-tagged OCTET STRING from `cbs`. If present, it sets +// `*out` to the string and `*out_present` to one. Otherwise, it sets +// `*out` to empty and `*out_present` to zero. `out_present` may be // NULL. It returns one on success, whether or not the element was // present, and zero on decode failure. OPENSSL_EXPORT int CBS_get_optional_asn1_octet_string(CBS *cbs, CBS *out, @@ -351,8 +361,8 @@ OPENSSL_EXPORT int CBS_get_optional_asn1_octet_string(CBS *cbs, CBS *out, CBS_ASN1_TAG tag); // CBS_get_optional_asn1_uint64 gets an optional explicitly-tagged -// INTEGER from |cbs|. If present, it sets |*out| to the -// value. Otherwise, it sets |*out| to |default_value|. It returns one +// INTEGER from `cbs`. If present, it sets `*out` to the +// value. Otherwise, it sets `*out` to `default_value`. It returns one // on success, whether or not the element was present, and zero on // decode failure. OPENSSL_EXPORT int CBS_get_optional_asn1_uint64(CBS *cbs, uint64_t *out, @@ -360,108 +370,125 @@ OPENSSL_EXPORT int CBS_get_optional_asn1_uint64(CBS *cbs, uint64_t *out, uint64_t default_value); // CBS_get_optional_asn1_bool gets an optional, explicitly-tagged BOOLEAN from -// |cbs|. If present, it sets |*out| to either zero or one, based on the -// boolean. Otherwise, it sets |*out| to |default_value|. It returns one on +// `cbs`. If present, it sets `*out` to either zero or one, based on the +// boolean. Otherwise, it sets `*out` to `default_value`. It returns one on // success, whether or not the element was present, and zero on decode // failure. OPENSSL_EXPORT int CBS_get_optional_asn1_bool(CBS *cbs, int *out, CBS_ASN1_TAG tag, int default_value); -// CBS_is_valid_asn1_bitstring returns one if |cbs| is a valid ASN.1 BIT STRING +// CBS_is_valid_asn1_bitstring returns one if `cbs` is a valid ASN.1 BIT STRING // body and zero otherwise. OPENSSL_EXPORT int CBS_is_valid_asn1_bitstring(const CBS *cbs); -// CBS_asn1_bitstring_has_bit returns one if |cbs| is a valid ASN.1 BIT STRING +// CBS_asn1_bitstring_has_bit returns one if `cbs` is a valid ASN.1 BIT STRING // body and the specified bit is present and set. Otherwise, it returns zero. -// |bit| is indexed starting from zero. +// `bit` is indexed starting from zero. OPENSSL_EXPORT int CBS_asn1_bitstring_has_bit(const CBS *cbs, unsigned bit); -// CBS_is_valid_asn1_integer returns one if |cbs| is a valid ASN.1 INTEGER, -// body and zero otherwise. On success, if |out_is_negative| is non-NULL, -// |*out_is_negative| will be set to one if |cbs| is negative and zero +// CBS_is_valid_asn1_integer returns one if `cbs` is a valid ASN.1 INTEGER, +// body and zero otherwise. On success, if `out_is_negative` is non-NULL, +// `*out_is_negative` will be set to one if `cbs` is negative and zero // otherwise. OPENSSL_EXPORT int CBS_is_valid_asn1_integer(const CBS *cbs, int *out_is_negative); -// CBS_is_unsigned_asn1_integer returns one if |cbs| is a valid non-negative +// CBS_is_unsigned_asn1_integer returns one if `cbs` is a valid non-negative // ASN.1 INTEGER body and zero otherwise. OPENSSL_EXPORT int CBS_is_unsigned_asn1_integer(const CBS *cbs); -// CBS_is_valid_asn1_oid returns one if |cbs| is a valid DER-encoded ASN.1 +// CBS_is_valid_asn1_set_of returns one if `cbs` is an ASN.1 SET OF contents +// (not including the element framing) with members sorted in nondescending +// order according to DER rules, and zero otherwise. This function does not +// parse the members and does not check that they are of any particular type. +OPENSSL_EXPORT int CBS_is_valid_asn1_set_of(const CBS *cbs); + +// CBS_is_valid_asn1_oid returns one if `cbs` is a valid DER-encoded ASN.1 // OBJECT IDENTIFIER contents (not including the element framing) and zero // otherwise. This function tolerates arbitrarily large OID components. OPENSSL_EXPORT int CBS_is_valid_asn1_oid(const CBS *cbs); -// CBS_asn1_oid_to_text interprets |cbs| as DER-encoded ASN.1 OBJECT IDENTIFIER +// CBS_asn1_oid_to_text interprets `cbs` as DER-encoded ASN.1 OBJECT IDENTIFIER // contents (not including the element framing) and returns the ASCII // representation (e.g., "1.2.840.113554.4.1.72585") in a newly-allocated // string, or NULL on failure. The caller must release the result with -// |OPENSSL_free|. +// `OPENSSL_free`. // -// This function may fail if |cbs| is an invalid OBJECT IDENTIFIER, or if any +// This function may fail if `cbs` is an invalid OBJECT IDENTIFIER, or if any // OID components are too large. OPENSSL_EXPORT char *CBS_asn1_oid_to_text(const CBS *cbs); -// CBS_is_valid_asn1_relative_oid returns one if |cbs| is a valid DER-encoded +// CBS_is_valid_asn1_relative_oid returns one if `cbs` is a valid DER-encoded // ASN.1 RELATIVE-OID contents (not including the element framing) and zero // otherwise. This function tolerates arbitrarily large OID components. // -// (This is actually the same as |CBS_is_valid_asn1_oid|, but is also exposed +// (This is actually the same as `CBS_is_valid_asn1_oid`, but is also exposed // under the relative_oid name for API symmetry.) OPENSSL_EXPORT int CBS_is_valid_asn1_relative_oid(const CBS *cbs); -// CBS_asn1_relative_oid_to_text interprets |cbs| as DER-encoded ASN.1 +// CBS_asn1_relative_oid_to_text interprets `cbs` as DER-encoded ASN.1 // RELATIVE-OID contents (not including the element framing) and returns the // ASCII representation (e.g., "32473.1") in a newly-allocated string, or NULL -// on failure. The caller must release the result with |OPENSSL_free|. +// on failure. The caller must release the result with `OPENSSL_free`. // -// This function may fail if |cbs| is an invalid RELATIVE-OID, or if any +// This function may fail if `cbs` is an invalid RELATIVE-OID, or if any // OID components are too large. OPENSSL_EXPORT char *CBS_asn1_relative_oid_to_text(const CBS *cbs); -// CBS_parse_generalized_time returns one if |cbs| is a valid DER-encoded, ASN.1 +// CBS_get_asn1_oid_component gets a single OID component from `cbs`. On +// success, it sets `*out` to the value, advances `cbs` past the component, and +// returns one. If the input does not contain a valid OID component or if the +// OID component is bigger than `uint64_t`, it returns zero. +// +// Specifically, this decodes a non-empty, minimal-width, base-128, big-endian +// integer. The high bit of each byte is set for continuation bytes and unset +// for the final byte. This encoding is also used for DER tag numbers 31 and up. +OPENSSL_EXPORT int CBS_get_asn1_oid_component(CBS *cbs, uint64_t *out); + +// CBS_parse_generalized_time returns one if `cbs` is a valid DER-encoded, ASN.1 // GeneralizedTime body within the limitations imposed by RFC 5280, or zero -// otherwise. If |allow_timezone_offset| is non-zero, four-digit timezone +// otherwise. If `allow_timezone_offset` is non-zero, four-digit timezone // offsets, which would not be allowed by DER, are permitted. On success, if -// |out_tm| is non-NULL, |*out_tm| will be zeroed, and then set to the -// corresponding time in UTC. This function does not compute |out_tm->tm_wday| -// or |out_tm->tm_yday|. +// `out_tm` is non-NULL, `*out_tm` will be zeroed, and then set to the +// corresponding time in UTC. This function does not compute `out_tm->tm_wday` +// or `out_tm->tm_yday`. OPENSSL_EXPORT int CBS_parse_generalized_time(const CBS *cbs, struct tm *out_tm, int allow_timezone_offset); -// CBS_parse_utc_time returns one if |cbs| is a valid DER-encoded, ASN.1 +// CBS_parse_utc_time returns one if `cbs` is a valid DER-encoded, ASN.1 // UTCTime body within the limitations imposed by RFC 5280, or zero otherwise. -// If |allow_timezone_offset| is non-zero, four-digit timezone offsets, which -// would not be allowed by DER, are permitted. On success, if |out_tm| is -// non-NULL, |*out_tm| will be zeroed, and then set to the corresponding time -// in UTC. This function does not compute |out_tm->tm_wday| or -// |out_tm->tm_yday|. +// If `allow_timezone_offset` is non-zero, four-digit timezone offsets, which +// would not be allowed by DER, are permitted. On success, if `out_tm` is +// non-NULL, `*out_tm` will be zeroed, and then set to the corresponding time +// in UTC. This function does not compute `out_tm->tm_wday` or +// `out_tm->tm_yday`. OPENSSL_EXPORT int CBS_parse_utc_time(const CBS *cbs, struct tm *out_tm, int allow_timezone_offset); + // CRYPTO ByteBuilder. // -// |CBB| objects allow one to build length-prefixed serialisations. A |CBB| +// `CBB` objects allow one to build length-prefixed serialisations. A `CBB` // object is associated with a buffer and new buffers are created with -// |CBB_init|. Several |CBB| objects can point at the same buffer when a -// length-prefix is pending, however only a single |CBB| can be 'current' at -// any one time. For example, if one calls |CBB_add_u8_length_prefixed| then -// the new |CBB| points at the same buffer as the original. But if the original -// |CBB| is used then the length prefix is written out and the new |CBB| must +// `CBB_init`. Several `CBB` objects can point at the same buffer when a +// length-prefix is pending, however only a single `CBB` can be 'current' at +// any one time. For example, if one calls `CBB_add_u8_length_prefixed` then +// the new `CBB` points at the same buffer as the original. But if the original +// `CBB` is used then the length prefix is written out and the new `CBB` must // not be used again. // -// If one needs to force a length prefix to be written out because a |CBB| is -// going out of scope, use |CBB_flush|. If an operation on a |CBB| fails, it is -// in an undefined state and must not be used except to call |CBB_cleanup|. +// If one needs to force a length prefix to be written out because a `CBB` is +// going out of scope, use `CBB_flush`. If an operation on a `CBB` fails, it is +// in an undefined state and must not be used except to call `CBB_cleanup`. struct cbb_buffer_st { uint8_t *buf; - // len is the number of valid bytes in |buf|. + // len is the number of valid bytes in `buf`. size_t len; - // cap is the size of |buf|. + // cap is the size of `buf`. size_t cap; - // can_resize is one iff |buf| is owned by this object. If not then |buf| + // can_resize is one iff `buf` is owned by this object. If not then `buf` // cannot be resized. unsigned can_resize : 1; // error is one if there was an error writing to this CBB. All future @@ -470,12 +497,12 @@ struct cbb_buffer_st { }; struct cbb_child_st { - // base is a pointer to the buffer this |CBB| writes to. + // base is a pointer to the buffer this `CBB` writes to. struct cbb_buffer_st *base; - // offset is the number of bytes from the start of |base->buf| to this |CBB|'s + // offset is the number of bytes from the start of `base->buf` to this `CBB`'s // pending length prefix. size_t offset; - // pending_len_len contains the number of bytes in this |CBB|'s pending + // pending_len_len contains the number of bytes in this `CBB`'s pending // length-prefix, or zero if no length-prefix is pending. uint8_t pending_len_len; unsigned pending_is_asn1 : 1; @@ -484,8 +511,8 @@ struct cbb_child_st { struct cbb_st { // child points to a child CBB if a length-prefix is pending. CBB *child; - // is_child is one if this is a child |CBB| and zero if it is a top-level - // |CBB|. This determines which arm of the union is valid. + // is_child is one if this is a child `CBB` and zero if it is a top-level + // `CBB`. This determines which arm of the union is valid. char is_child; union { struct cbb_buffer_st base; @@ -493,213 +520,233 @@ struct cbb_st { } u; }; -// CBB_zero sets an uninitialised |cbb| to the zero state. It must be -// initialised with |CBB_init| or |CBB_init_fixed| before use, but it is safe to -// call |CBB_cleanup| without a successful |CBB_init|. This may be used for more -// uniform cleanup of a |CBB|. +// CBB_zero sets an uninitialised `cbb` to the zero state. It must be +// initialised with `CBB_init` or `CBB_init_fixed` before use, but it is safe to +// call `CBB_cleanup` without a successful `CBB_init`. This may be used for more +// uniform cleanup of a `CBB`. OPENSSL_EXPORT void CBB_zero(CBB *cbb); -// CBB_init initialises |cbb| with |initial_capacity|. Since a |CBB| grows as -// needed, the |initial_capacity| is just a hint. It returns one on success or +// CBB_init initialises `cbb` with `initial_capacity`. Since a `CBB` grows as +// needed, the `initial_capacity` is just a hint. It returns one on success or // zero on allocation failure. OPENSSL_EXPORT int CBB_init(CBB *cbb, size_t initial_capacity); -// CBB_init_fixed initialises |cbb| to write to |len| bytes at |buf|. Since -// |buf| cannot grow, trying to write more than |len| bytes will cause CBB +// CBB_init_fixed initialises `cbb` to write to `len` bytes at `buf`. Since +// `buf` cannot grow, trying to write more than `len` bytes will cause CBB // functions to fail. This function is infallible and always returns one. It is -// safe, but not necessary, to call |CBB_cleanup| on |cbb|. +// safe, but not necessary, to call `CBB_cleanup` on `cbb`. OPENSSL_EXPORT int CBB_init_fixed(CBB *cbb, uint8_t *buf, size_t len); -// CBB_cleanup frees all resources owned by |cbb| and other |CBB| objects +// CBB_cleanup frees all resources owned by `cbb` and other `CBB` objects // writing to the same buffer. This should be used in an error case where a // serialisation is abandoned. // -// This function can only be called on a "top level" |CBB|, i.e. one initialised -// with |CBB_init| or |CBB_init_fixed|, or a |CBB| set to the zero state with -// |CBB_zero|. +// This function can only be called on a "top level" `CBB`, i.e. one initialised +// with `CBB_init` or `CBB_init_fixed`, or a `CBB` set to the zero state with +// `CBB_zero`. OPENSSL_EXPORT void CBB_cleanup(CBB *cbb); -// CBB_finish completes any pending length prefix and sets |*out_data| to a -// malloced buffer and |*out_len| to the length of that buffer. The caller +// CBB_finish completes any pending length prefix and sets `*out_data` to a +// malloced buffer and `*out_len` to the length of that buffer. The caller // takes ownership of the buffer and, unless the buffer was fixed with -// |CBB_init_fixed|, must call |OPENSSL_free| when done. +// `CBB_init_fixed`, must call `OPENSSL_free` when done. // -// It can only be called on a "top level" |CBB|, i.e. one initialised with -// |CBB_init| or |CBB_init_fixed|. It returns one on success and zero on +// It can only be called on a "top level" `CBB`, i.e. one initialised with +// `CBB_init` or `CBB_init_fixed`. It returns one on success and zero on // error. OPENSSL_EXPORT int CBB_finish(CBB *cbb, uint8_t **out_data, size_t *out_len); // CBB_flush causes any pending length prefixes to be written out and any child -// |CBB| objects of |cbb| to be invalidated. This allows |cbb| to continue to be -// used after the children go out of scope, e.g. when local |CBB| objects are -// added as children to a |CBB| that persists after a function returns. This +// `CBB` objects of `cbb` to be invalidated. This allows `cbb` to continue to be +// used after the children go out of scope, e.g. when local `CBB` objects are +// added as children to a `CBB` that persists after a function returns. This // function returns one on success or zero on error. OPENSSL_EXPORT int CBB_flush(CBB *cbb); -// CBB_data returns a pointer to the bytes written to |cbb|. It does not flush -// |cbb|. The pointer is valid until the next operation to |cbb|. +// CBB_data returns a pointer to the bytes written to `cbb`. It does not flush +// `cbb`. The pointer is valid until the next operation to `cbb`. // // To avoid unfinalized length prefixes, it is a fatal error to call this on a // CBB with any active children. OPENSSL_EXPORT uint8_t *CBB_data(const CBB *cbb); -// CBB_len returns the number of bytes written to |cbb|. It does not flush -// |cbb|. +// CBB_len returns the number of bytes written to `cbb`. It does not flush +// `cbb`. // // To avoid unfinalized length prefixes, it is a fatal error to call this on a // CBB with any active children. OPENSSL_EXPORT size_t CBB_len(const CBB *cbb); -// CBB_add_u8_length_prefixed sets |*out_contents| to a new child of |cbb|. The -// data written to |*out_contents| will be prefixed in |cbb| with an 8-bit +// CBB_add_u8_length_prefixed sets `*out_contents` to a new child of `cbb`. The +// data written to `*out_contents` will be prefixed in `cbb` with an 8-bit // length. It returns one on success or zero on error. OPENSSL_EXPORT int CBB_add_u8_length_prefixed(CBB *cbb, CBB *out_contents); -// CBB_add_u16_length_prefixed sets |*out_contents| to a new child of |cbb|. -// The data written to |*out_contents| will be prefixed in |cbb| with a 16-bit, +// CBB_add_u16_length_prefixed sets `*out_contents` to a new child of `cbb`. +// The data written to `*out_contents` will be prefixed in `cbb` with a 16-bit, // big-endian length. It returns one on success or zero on error. OPENSSL_EXPORT int CBB_add_u16_length_prefixed(CBB *cbb, CBB *out_contents); -// CBB_add_u24_length_prefixed sets |*out_contents| to a new child of |cbb|. -// The data written to |*out_contents| will be prefixed in |cbb| with a 24-bit, +// CBB_add_u24_length_prefixed sets `*out_contents` to a new child of `cbb`. +// The data written to `*out_contents` will be prefixed in `cbb` with a 24-bit, // big-endian length. It returns one on success or zero on error. OPENSSL_EXPORT int CBB_add_u24_length_prefixed(CBB *cbb, CBB *out_contents); -// CBB_add_asn1 sets |*out_contents| to a |CBB| into which the contents of an -// ASN.1 object can be written. The |tag| argument will be used as the tag for +// CBB_add_asn1 sets `*out_contents` to a `CBB` into which the contents of an +// ASN.1 object can be written. The `tag` argument will be used as the tag for // the object. It returns one on success or zero on error. OPENSSL_EXPORT int CBB_add_asn1(CBB *cbb, CBB *out_contents, CBS_ASN1_TAG tag); -// CBB_add_bytes appends |len| bytes from |data| to |cbb|. It returns one on +// CBB_add_bytes appends `len` bytes from `data` to `cbb`. It returns one on // success and zero otherwise. OPENSSL_EXPORT int CBB_add_bytes(CBB *cbb, const uint8_t *data, size_t len); -// CBB_add_zeros append |len| bytes with value zero to |cbb|. It returns one on +// CBB_add_zeros append `len` bytes with value zero to `cbb`. It returns one on // success and zero otherwise. OPENSSL_EXPORT int CBB_add_zeros(CBB *cbb, size_t len); -// CBB_add_space appends |len| bytes to |cbb| and sets |*out_data| to point to -// the beginning of that space. The caller must then write |len| bytes of -// actual contents to |*out_data|. It returns one on success and zero +// CBB_add_space appends `len` bytes to `cbb` and sets `*out_data` to point to +// the beginning of that space. The caller must then write `len` bytes of +// actual contents to `*out_data`. It returns one on success and zero // otherwise. OPENSSL_EXPORT int CBB_add_space(CBB *cbb, uint8_t **out_data, size_t len); -// CBB_reserve ensures |cbb| has room for |len| additional bytes and sets -// |*out_data| to point to the beginning of that space. It returns one on -// success and zero otherwise. The caller may write up to |len| bytes to -// |*out_data| and call |CBB_did_write| to complete the write. |*out_data| is -// valid until the next operation on |cbb| or an ancestor |CBB|. +// CBB_reserve ensures `cbb` has room for `len` additional bytes and sets +// `*out_data` to point to the beginning of that space. It returns one on +// success and zero otherwise. The caller may write up to `len` bytes to +// `*out_data` and call `CBB_did_write` to complete the write. `*out_data` is +// valid until the next operation on `cbb` or an ancestor `CBB`. OPENSSL_EXPORT int CBB_reserve(CBB *cbb, uint8_t **out_data, size_t len); -// CBB_did_write advances |cbb| by |len| bytes, assuming the space has been +// CBB_did_write advances `cbb` by `len` bytes, assuming the space has been // written to by the caller. It returns one on success and zero on error. OPENSSL_EXPORT int CBB_did_write(CBB *cbb, size_t len); -// CBB_add_u8 appends an 8-bit number from |value| to |cbb|. It returns one on +// CBB_add_u8 appends an 8-bit number from `value` to `cbb`. It returns one on // success and zero otherwise. OPENSSL_EXPORT int CBB_add_u8(CBB *cbb, uint8_t value); -// CBB_add_u16 appends a 16-bit, big-endian number from |value| to |cbb|. It +// CBB_add_u16 appends a 16-bit, big-endian number from `value` to `cbb`. It // returns one on success and zero otherwise. OPENSSL_EXPORT int CBB_add_u16(CBB *cbb, uint16_t value); -// CBB_add_u16le appends a 16-bit, little-endian number from |value| to |cbb|. +// CBB_add_u16le appends a 16-bit, little-endian number from `value` to `cbb`. // It returns one on success and zero otherwise. OPENSSL_EXPORT int CBB_add_u16le(CBB *cbb, uint16_t value); -// CBB_add_u24 appends a 24-bit, big-endian number from |value| to |cbb|. It +// CBB_add_u24 appends a 24-bit, big-endian number from `value` to `cbb`. It // returns one on success and zero otherwise. OPENSSL_EXPORT int CBB_add_u24(CBB *cbb, uint32_t value); -// CBB_add_u32 appends a 32-bit, big-endian number from |value| to |cbb|. It +// CBB_add_u32 appends a 32-bit, big-endian number from `value` to `cbb`. It // returns one on success and zero otherwise. OPENSSL_EXPORT int CBB_add_u32(CBB *cbb, uint32_t value); -// CBB_add_u32le appends a 32-bit, little-endian number from |value| to |cbb|. +// CBB_add_u32le appends a 32-bit, little-endian number from `value` to `cbb`. // It returns one on success and zero otherwise. OPENSSL_EXPORT int CBB_add_u32le(CBB *cbb, uint32_t value); -// CBB_add_u64 appends a 64-bit, big-endian number from |value| to |cbb|. It +// CBB_add_u48 appends a 48-bit, big-endian number from `value` to `cbb`. It +// returns one on success and zero otherwise. +OPENSSL_EXPORT int CBB_add_u48(CBB *cbb, uint64_t value); + +// CBB_add_u64 appends a 64-bit, big-endian number from `value` to `cbb`. It // returns one on success and zero otherwise. OPENSSL_EXPORT int CBB_add_u64(CBB *cbb, uint64_t value); -// CBB_add_u64le appends a 64-bit, little-endian number from |value| to |cbb|. +// CBB_add_u64le appends a 64-bit, little-endian number from `value` to `cbb`. // It returns one on success and zero otherwise. OPENSSL_EXPORT int CBB_add_u64le(CBB *cbb, uint64_t value); -// CBB_discard discards the last |len| bytes written to |cbb|. The process will -// abort if |cbb| has an unflushed child, or its length is smaller than |len|. +// CBB_discard discards the last `len` bytes written to `cbb`. The process will +// abort if `cbb` has an unflushed child, or its length is smaller than `len`. OPENSSL_EXPORT void CBB_discard(CBB *cbb, size_t len); -// CBB_discard_child discards the current unflushed child of |cbb|. Neither the +// CBB_discard_child discards the current unflushed child of `cbb`. Neither the // child's contents nor the length prefix will be included in the output. OPENSSL_EXPORT void CBB_discard_child(CBB *cbb); // CBB_add_asn1_element adds an ASN.1 element with the specified tag and // contents. It returns one on success and zero on error. This is a convenience -// function over |CBB_add_asn1| when the data is already available. +// function over `CBB_add_asn1` when the data is already available. OPENSSL_EXPORT int CBB_add_asn1_element(CBB *cbb, CBS_ASN1_TAG tag, const uint8_t *data, size_t data_len); -// CBB_add_asn1_uint64 writes an ASN.1 INTEGER into |cbb| using |CBB_add_asn1| -// and writes |value| in its contents. It returns one on success and zero on +// CBB_add_asn1_uint64 writes an ASN.1 INTEGER into `cbb` using `CBB_add_asn1` +// and writes `value` in its contents. It returns one on success and zero on // error. OPENSSL_EXPORT int CBB_add_asn1_uint64(CBB *cbb, uint64_t value); -// CBB_add_asn1_uint64_with_tag behaves like |CBB_add_asn1_uint64| but uses -// |tag| as the tag instead of INTEGER. This is useful if the INTEGER type uses +// CBB_add_asn1_uint64_with_tag behaves like `CBB_add_asn1_uint64` but uses +// `tag` as the tag instead of INTEGER. This is useful if the INTEGER type uses // implicit tagging. OPENSSL_EXPORT int CBB_add_asn1_uint64_with_tag(CBB *cbb, uint64_t value, CBS_ASN1_TAG tag); -// CBB_add_asn1_int64 writes an ASN.1 INTEGER into |cbb| using |CBB_add_asn1| -// and writes |value| in its contents. It returns one on success and zero on +// CBB_add_asn1_int64 writes an ASN.1 INTEGER into `cbb` using `CBB_add_asn1` +// and writes `value` in its contents. It returns one on success and zero on // error. OPENSSL_EXPORT int CBB_add_asn1_int64(CBB *cbb, int64_t value); -// CBB_add_asn1_int64_with_tag behaves like |CBB_add_asn1_int64| but uses |tag| +// CBB_add_asn1_int64_with_tag behaves like `CBB_add_asn1_int64` but uses `tag` // as the tag instead of INTEGER. This is useful if the INTEGER type uses // implicit tagging. OPENSSL_EXPORT int CBB_add_asn1_int64_with_tag(CBB *cbb, int64_t value, CBS_ASN1_TAG tag); -// CBB_add_asn1_octet_string writes an ASN.1 OCTET STRING into |cbb| with the +// CBB_add_asn1_octet_string writes an ASN.1 OCTET STRING into `cbb` with the // given contents. It returns one on success and zero on error. OPENSSL_EXPORT int CBB_add_asn1_octet_string(CBB *cbb, const uint8_t *data, size_t data_len); -// CBB_add_asn1_bool writes an ASN.1 BOOLEAN into |cbb| which is true iff -// |value| is non-zero. It returns one on success and zero on error. +// CBB_add_asn1_bool writes an ASN.1 BOOLEAN into `cbb` which is true iff +// `value` is non-zero. It returns one on success and zero on error. OPENSSL_EXPORT int CBB_add_asn1_bool(CBB *cbb, int value); -// CBB_add_asn1_oid_from_text decodes |len| bytes from |text| as an ASCII OID +// CBB_add_asn1_oid_from_text decodes `len` bytes from `text` as an ASCII OID // representation, e.g. "1.2.840.113554.4.1.72585", and writes the DER-encoded -// contents to |cbb|. It returns one on success and zero on malloc failure or if -// |text| was invalid. It does not include the OBJECT IDENTIFIER framing, only +// contents to `cbb`. It returns one on success and zero on malloc failure or if +// `text` was invalid. It does not include the OBJECT IDENTIFIER framing, only // the element's contents. // // This function considers OID strings with components which do not fit in a -// |uint64_t| to be invalid. +// `uint64_t` to be invalid. OPENSSL_EXPORT int CBB_add_asn1_oid_from_text(CBB *cbb, const char *text, size_t len); -// CBB_add_asn1_relative_oid_from_text decodes |len| bytes from |text| as an +// CBB_add_asn1_relative_oid_from_text decodes `len` bytes from `text` as an // ASCII RELATIVE-OID representation, e.g. "32473.1", and writes the -// DER-encoded contents to |cbb|. It returns one on success and zero on malloc -// failure or if |text| was invalid. It does not include any framing, only the +// DER-encoded contents to `cbb`. It returns one on success and zero on malloc +// failure or if `text` was invalid. It does not include any framing, only the // element's contents. // // This function considers OID strings with components which do not fit in a -// |uint64_t| to be invalid. +// `uint64_t` to be invalid. OPENSSL_EXPORT int CBB_add_asn1_relative_oid_from_text(CBB *cbb, const char *text, size_t len); -// CBB_add_asn1_oid_component appends a single OID component to |cbb|. +// CBB_add_asn1_relative_oid_from_der_to_text reads `data_len` bytes of +// DER-encoded ASN.1 RELATIVE-OID contents (not including the element framing) +// from `data` and writes the ASCII representation (e.g., "32473.1") to `cbb` +// (without a trailing NUL byte). It returns one on success and zero on +// failure. +// +// This function may fail if `data` is an invalid RELATIVE-OID, or if any OID +// components are too large. +OPENSSL_EXPORT int CBB_add_asn1_relative_oid_from_der_to_text( + CBB *cbb, const uint8_t *data, size_t data_len); + +// CBB_add_asn1_oid_component appends a single OID component to `cbb`. // It returns one on success and zero on error. +// +// Specifically, this encodes `value` as a non-empty, minimal-width, base-128, +// big-endian integer. The high bit of each byte is set for continuation bytes +// and unset for the final byte. This encoding is also used for DER tag numbers +// 31 and up. OPENSSL_EXPORT int CBB_add_asn1_oid_component(CBB *cbb, uint64_t value); -// CBB_flush_asn1_set_of calls |CBB_flush| on |cbb| and then reorders the +// CBB_flush_asn1_set_of calls `CBB_flush` on `cbb` and then reorders the // contents for a DER-encoded ASN.1 SET OF type. It returns one on success and // zero on failure. DER canonicalizes SET OF contents by sorting // lexicographically by encoding. Call this function when encoding a SET OF @@ -714,21 +761,21 @@ OPENSSL_EXPORT int CBB_flush_asn1_set_of(CBB *cbb); // These functions consider noncharacters (see section 23.7 from Unicode 15.0.0) // to be invalid code points and will treat them as an error condition. -// The following functions read one Unicode code point from |cbs| with the -// corresponding encoding and store it in |*out|. They return one on success and +// The following functions read one Unicode code point from `cbs` with the +// corresponding encoding and store it in `*out`. They return one on success and // zero on error. OPENSSL_EXPORT int CBS_get_utf8(CBS *cbs, uint32_t *out); OPENSSL_EXPORT int CBS_get_latin1(CBS *cbs, uint32_t *out); OPENSSL_EXPORT int CBS_get_ucs2_be(CBS *cbs, uint32_t *out); OPENSSL_EXPORT int CBS_get_utf32_be(CBS *cbs, uint32_t *out); -// CBB_get_utf8_len returns the number of bytes needed to represent |u| in +// CBB_get_utf8_len returns the number of bytes needed to represent `u` in // UTF-8. OPENSSL_EXPORT size_t CBB_get_utf8_len(uint32_t u); -// The following functions encode |u| to |cbb| with the corresponding +// The following functions encode `u` to `cbb` with the corresponding // encoding. They return one on success and zero on error. Error conditions -// include |u| being an invalid code point, or |u| being unencodable in the +// include `u` being an invalid code point, or `u` being unencodable in the // specified encoding. OPENSSL_EXPORT int CBB_add_utf8(CBB *cbb, uint32_t u); OPENSSL_EXPORT int CBB_add_latin1(CBB *cbb, uint32_t u); @@ -745,7 +792,8 @@ extern "C++" { BSSL_NAMESPACE_BEGIN -using ScopedCBB = internal::StackAllocated; +BORINGSSL_MAKE_STACK_TRAITS(CBB, CBB_zero, CBB_cleanup) +using ScopedCBB = internal::StackAllocated; BSSL_NAMESPACE_END diff --git a/third_party/boringssl/src/include/openssl/cast.h b/third_party/boringssl/src/include/openssl/cast.h index 0c19ead6a..36bad3bb5 100644 --- a/third_party/boringssl/src/include/openssl/cast.h +++ b/third_party/boringssl/src/include/openssl/cast.h @@ -43,41 +43,41 @@ typedef struct cast_key_st { int short_key; // Use reduced rounds for short key } CAST_KEY; -// CAST_set_key initializes |key| from |len| bytes of key material starting from -// |data|. CAST-128 keys are between 5 and 16 bytes long. If |len| is greater -// than 16, |data| is truncated and only the first 16 bytes are processed. If -// |len| is less than 5, it is internally zero-padded. +// CAST_set_key initializes `key` from `len` bytes of key material starting from +// `data`. CAST-128 keys are between 5 and 16 bytes long. If `len` is greater +// than 16, `data` is truncated and only the first 16 bytes are processed. If +// `len` is less than 5, it is internally zero-padded. OPENSSL_EXPORT void CAST_set_key(CAST_KEY *key, size_t len, const uint8_t *data); -// CAST_ecb_encrypt encrypts (or decrypts, if |enc| is |CAST_DECRYPT|) a single -// 8-byte block from |in| to |out|, using |key|. +// CAST_ecb_encrypt encrypts (or decrypts, if `enc` is `CAST_DECRYPT`) a single +// 8-byte block from `in` to `out`, using `key`. OPENSSL_EXPORT void CAST_ecb_encrypt(const uint8_t in[CAST_BLOCK], uint8_t out[CAST_BLOCK], const CAST_KEY *key, int enc); -// CAST_encrypt encrypts an 8-byte block from |data| in-place with |key|. An +// CAST_encrypt encrypts an 8-byte block from `data` in-place with `key`. An // 8-byte block is represented in this function as two 32-bit integers, // containing the first and second four bytes in big-endian order. OPENSSL_EXPORT void CAST_encrypt(uint32_t data[2], const CAST_KEY *key); -// CAST_decrypt decrypts an 8-byte block from |data| in-place with |key|. An +// CAST_decrypt decrypts an 8-byte block from `data` in-place with `key`. An // 8-byte block is represented in this function as two 32-bit integers, // containing the first and second four bytes in big-endian order. OPENSSL_EXPORT void CAST_decrypt(uint32_t data[2], const CAST_KEY *key); -// CAST_cbc_encrypt encrypts (or decrypts, if |enc| is |CAST_DECRYPT|) |length| -// bytes from |in| to |out| with CAST-128 in CBC mode. |length| must be a -// multiple of 8. The IV is taken from |iv|. When the function completes, the IV -// for the next block is written to |iv|. +// CAST_cbc_encrypt encrypts (or decrypts, if `enc` is `CAST_DECRYPT`) `length` +// bytes from `in` to `out` with CAST-128 in CBC mode. `length` must be a +// multiple of 8. The IV is taken from `iv`. When the function completes, the IV +// for the next block is written to `iv`. OPENSSL_EXPORT void CAST_cbc_encrypt(const uint8_t *in, uint8_t *out, size_t length, const CAST_KEY *ks, uint8_t iv[8], int enc); -// CAST_cfb64_encrypt encrypts (or decrypts, if |enc| is |CAST_DECRYPT|) -// |length| bytes from |in| to |out| with CAST-128 in CFB-64 mode. |length| must -// be a multiple of 8. On the first call, |*num| should be zero and |ivec| the -// IV. On exit, this function will write state to |ivec| and |*num| to resume an +// CAST_cfb64_encrypt encrypts (or decrypts, if `enc` is `CAST_DECRYPT`) +// `length` bytes from `in` to `out` with CAST-128 in CFB-64 mode. `length` must +// be a multiple of 8. On the first call, `*num` should be zero and `ivec` the +// IV. On exit, this function will write state to `ivec` and `*num` to resume an // encryption or decryption operation if the buffers are not contiguous. OPENSSL_EXPORT void CAST_cfb64_encrypt(const uint8_t *in, uint8_t *out, size_t length, const CAST_KEY *schedule, diff --git a/third_party/boringssl/src/include/openssl/chacha.h b/third_party/boringssl/src/include/openssl/chacha.h index ff9f55e0c..857bda612 100644 --- a/third_party/boringssl/src/include/openssl/chacha.h +++ b/third_party/boringssl/src/include/openssl/chacha.h @@ -26,9 +26,9 @@ extern "C" { // ChaCha20 is a stream cipher. See https://tools.ietf.org/html/rfc8439. -// CRYPTO_chacha_20 encrypts |in_len| bytes from |in| with the given key and -// nonce and writes the result to |out|. If |in| and |out| alias, they must be -// equal. The initial block counter is specified by |counter|. +// CRYPTO_chacha_20 encrypts `in_len` bytes from `in` with the given key and +// nonce and writes the result to `out`. If `in` and `out` alias, they must be +// equal. The initial block counter is specified by `counter`. // // This function implements a 32-bit block counter as in RFC 8439. On overflow, // the counter wraps. Reusing a key, nonce, and block counter combination is not diff --git a/third_party/boringssl/src/include/openssl/cipher.h b/third_party/boringssl/src/include/openssl/cipher.h index ed2eeab57..9ca8d2782 100644 --- a/third_party/boringssl/src/include/openssl/cipher.h +++ b/third_party/boringssl/src/include/openssl/cipher.h @@ -15,7 +15,7 @@ #ifndef OPENSSL_HEADER_CIPHER_H #define OPENSSL_HEADER_CIPHER_H -#include // IWYU pragma: export +#include // IWYU pragma: export #if defined(__cplusplus) extern "C" { @@ -27,7 +27,7 @@ extern "C" { // Cipher primitives. // -// The following functions return |EVP_CIPHER| objects that implement the named +// The following functions return `EVP_CIPHER` objects that implement the named // cipher algorithm. OPENSSL_EXPORT const EVP_CIPHER *EVP_rc4(void); @@ -48,6 +48,11 @@ OPENSSL_EXPORT const EVP_CIPHER *EVP_aes_256_ecb(void); OPENSSL_EXPORT const EVP_CIPHER *EVP_aes_256_cbc(void); OPENSSL_EXPORT const EVP_CIPHER *EVP_aes_256_ctr(void); OPENSSL_EXPORT const EVP_CIPHER *EVP_aes_256_ofb(void); + +// EVP_aes_256_xts implements AES-256-XTS. Unlike other `EVP_CIPHER`s, this +// object is single-shot. It does not support splitting a single operation (one +// set of key, IV, and input) into multiple `EVP_EncryptUpdate_ex` or +// `EVP_DecryptUpdate_ex` calls. OPENSSL_EXPORT const EVP_CIPHER *EVP_aes_256_xts(void); // EVP_enc_null returns a 'cipher' that passes plaintext through as @@ -73,54 +78,95 @@ OPENSSL_EXPORT const EVP_CIPHER *EVP_get_cipherbynid(int nid); // Cipher context allocation. // -// An |EVP_CIPHER_CTX| represents the state of an encryption or decryption in +// An `EVP_CIPHER_CTX` represents the state of an encryption or decryption in // progress. -// EVP_CIPHER_CTX_init initialises an, already allocated, |EVP_CIPHER_CTX|. +// EVP_CIPHER_CTX_init sets an uninitialized `EVP_CIPHER_CTX` to the zero state. +// This is the same as setting the structure to zero. +// +// This function is used for initializing uninitialized memory in an +// `EVP_CIPHER_CTX`, e.g. if it is declared as a local variable on the stack. +// This function should not be used on objects that have already been +// initialized. OPENSSL_EXPORT void EVP_CIPHER_CTX_init(EVP_CIPHER_CTX *ctx); -// EVP_CIPHER_CTX_new allocates a fresh |EVP_CIPHER_CTX|, calls -// |EVP_CIPHER_CTX_init| and returns it, or NULL on allocation failure. +// EVP_CIPHER_CTX_new returns a newly-allocated `EVP_CIPHER_CTX` in the zero +// state, or NULL on allocation failure. The caller must use +// `EVP_CIPHER_CTX_free` to release the resulting object. OPENSSL_EXPORT EVP_CIPHER_CTX *EVP_CIPHER_CTX_new(void); -// EVP_CIPHER_CTX_cleanup frees any memory referenced by |ctx|. It returns +// EVP_CIPHER_CTX_cleanup frees any memory referenced by `ctx`. It returns // one. OPENSSL_EXPORT int EVP_CIPHER_CTX_cleanup(EVP_CIPHER_CTX *ctx); -// EVP_CIPHER_CTX_free calls |EVP_CIPHER_CTX_cleanup| on |ctx| and then frees -// |ctx| itself. +// EVP_CIPHER_CTX_free calls `EVP_CIPHER_CTX_cleanup` on `ctx` and then frees +// `ctx` itself. OPENSSL_EXPORT void EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *ctx); -// EVP_CIPHER_CTX_copy sets |out| to be a duplicate of the current state of -// |in|. The |out| argument must have been previously initialised. +// EVP_CIPHER_CTX_copy sets `out` to be a duplicate of the current state of +// `in`. The `out` argument must have been previously initialised, e.g. with +// `EVP_CIPHER_CTX_init` or `EVP_CIPHER_CTX_new`. OPENSSL_EXPORT int EVP_CIPHER_CTX_copy(EVP_CIPHER_CTX *out, const EVP_CIPHER_CTX *in); -// EVP_CIPHER_CTX_reset calls |EVP_CIPHER_CTX_cleanup| followed by -// |EVP_CIPHER_CTX_init| and returns one. +// EVP_CIPHER_CTX_reset calls `EVP_CIPHER_CTX_cleanup` followed by +// `EVP_CIPHER_CTX_init` and returns one. OPENSSL_EXPORT int EVP_CIPHER_CTX_reset(EVP_CIPHER_CTX *ctx); // Cipher context configuration. -// EVP_CipherInit_ex configures |ctx| for a fresh encryption (or decryption, if -// |enc| is zero) operation using |cipher|. If |ctx| has been previously -// configured with a cipher then |cipher|, |key| and |iv| may be |NULL| and -// |enc| may be -1 to reuse the previous values. The operation will use |key| -// as the key and |iv| as the IV (if any). These should have the correct -// lengths given by |EVP_CIPHER_key_length| and |EVP_CIPHER_iv_length|. It -// returns one on success and zero on error. +// EVP_CipherInit_ex configures `ctx` for a fresh encryption (or decryption, if +// `enc` is zero) operation using `cipher`. The memory in `ctx` must already +// have been initialized, e.g. with `EVP_CIPHER_CTX_init` or +// `EVP_CIPHER_CTX_new`. If non-NULL, `key` and `iv` will be used as the key and +// IV, respectively. These must point to `EVP_CIPHER_key_length` and +// `EVP_CIPHER_iv_length` bytes, respectively. It returns one on success and +// zero on error. +// +// This function may be called multiple times on a single `EVP_CIPHER_CTX`, +// either to reset an existing object, or to configure a single operation in +// multiple steps. In such cases: +// +// - `cipher` may be NULL to reuse the previous cipher state. A non-NULL +// `cipher` will reset all cipher state, including the key and IV, even if +// configuring the same `EVP_CIPHER` as before. +// +// - `key` and `iv` may be NULL to either reuse the previous value, or leave +// them unconfigured. +// +// - `enc` may be -1 to reuse the previous value. In ciphers that use a +// different key schedule between encryption and decryption (e.g. CBC and ECB +// modes), callers must configure `key` and `enc` together. +// +// Some example multi-step configuration patterns: +// +// - If setting a variable-length key with `EVP_CIPHER_CTX_set_key_length`, +// first configure only `cipher`, then use `EVP_CIPHER_CTX_set_key_length`, +// and finally configure `key`, leaving `cipher` NULL. +// +// - If setting a variable-length IV with `EVP_CTRL_AEAD_SET_IVLEN`, first +// configure `cipher` and `key`, then use `EVP_CTRL_AEAD_SET_IVLEN`, and +// finally configure `iv`, leaving other parameters NULL. +// +// - If retaining a long-lived context to reuse the key schedule across +// operations, first configure only `cipher` and `key` on the long-lived +// context, then copy the key schedule to a per-operation context with +// `EVP_CIPHER_CTX_copy`, and finally configure only `iv` on the per-operation +// context. +// +// WARNING: This API is difficult to use correctly. Use `EVP_AEAD` for AEADs. OPENSSL_EXPORT int EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, ENGINE *engine, const uint8_t *key, const uint8_t *iv, int enc); -// EVP_EncryptInit_ex calls |EVP_CipherInit_ex| with |enc| equal to one. +// EVP_EncryptInit_ex calls `EVP_CipherInit_ex` with `enc` equal to one. OPENSSL_EXPORT int EVP_EncryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, ENGINE *impl, const uint8_t *key, const uint8_t *iv); -// EVP_DecryptInit_ex calls |EVP_CipherInit_ex| with |enc| equal to zero. +// EVP_DecryptInit_ex calls `EVP_CipherInit_ex` with `enc` equal to zero. OPENSSL_EXPORT int EVP_DecryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, ENGINE *impl, const uint8_t *key, const uint8_t *iv); @@ -128,71 +174,75 @@ OPENSSL_EXPORT int EVP_DecryptInit_ex(EVP_CIPHER_CTX *ctx, // Cipher operations. -// EVP_EncryptUpdate_ex encrypts |in_len| bytes from |in| and writes up to -// |max_out| bytes of ciphertext to |out|. On success, it sets |*out_len| to +// EVP_EncryptUpdate_ex encrypts `in_len` bytes from `in` and writes up to +// `max_out_len` bytes of ciphertext to `out`. On success, it sets `*out_len` to // the number of output bytes and returns one. Otherwise, it returns zero. // -// If |max_out| is not large enough for the output, the function will return +// If `max_out_len` is not large enough for the output, the function will return // zero. The size of output buffer needed depends on the cipher and the number -// of bytes encrypted by |ctx| thus far. +// of bytes encrypted by `ctx` thus far. `EVP_CIPHER_CTX_max_next_update` will +// return the maximum output for this call. // // In ciphers whose block size is not 1, such as CBC, individual calls to -// |EVP_EncryptUpdate_ex| may output more or less than |in_len| bytes: a single -// call to |EVP_EncryptUpdate_ex| may output at most |in_len + block_size - 1| -// bytes. Additionally, the total output across all |EVP_EncryptUpdate_ex| and -// |EVP_EncryptFinal_ex2| calls will be at most the total input plus one byte, +// `EVP_EncryptUpdate_ex` may output more or less than `in_len` bytes: a single +// call to `EVP_EncryptUpdate_ex` may output at most `in_len + block_size - 1` +// bytes. Additionally, the total output across all `EVP_EncryptUpdate_ex` and +// `EVP_EncryptFinal_ex2` calls will be at most the total input plus one byte, // rounded up to a multiple of the block size. OPENSSL_EXPORT int EVP_EncryptUpdate_ex(EVP_CIPHER_CTX *ctx, uint8_t *out, size_t *out_len, size_t max_out_len, const uint8_t *in, size_t in_len); // EVP_EncryptFinal_ex2 finishes an encryption operation and writes up to -// |max_out| bytes of output to out. On success, it sets |*out_len| to the +// `max_out_len` bytes of output to out. On success, it sets `*out_len` to the // number of bytes written and returns one. Otherwise, it returns zero. // -// If |max_out| is not large enough for the output, the function will return +// If `max_out_len` is not large enough for the output, the function will return // zero. The size of output buffer needed depends on the cipher and the number -// of bytes encrypted. +// of bytes encrypted. `EVP_CIPHER_CTX_max_final` will return the maximum output +// for this call. // // If the block size is 1, there will be no final output at all; otherwise, at // most one block of ciphertext will be written to the output. // // If padding is enabled (the default) and the block size is not 1, then // standard padding is applied to create the final block. If padding is -// disabled (with |EVP_CIPHER_CTX_set_padding|) then any partial block +// disabled (with `EVP_CIPHER_CTX_set_padding`) then any partial block // remaining will cause an error. The function returns one on success and zero // otherwise. OPENSSL_EXPORT int EVP_EncryptFinal_ex2(EVP_CIPHER_CTX *ctx, uint8_t *out, size_t *out_len, size_t max_out_len); -// EVP_DecryptUpdate_ex decrypts |in_len| bytes from |in| and writes up to -// |max_out| bytes of plaintext to |out|. On success, it sets |*out_len| to +// EVP_DecryptUpdate_ex decrypts `in_len` bytes from `in` and writes up to +// `max_out_len` bytes of plaintext to `out`. On success, it sets `*out_len` to // the number of output bytes and returns one. Otherwise, it returns zero. // -// If |max_out| is not large enough for the output, the function will return +// If `max_out_len` is not large enough for the output, the function will return // zero. The size of output buffer needed depends on the cipher and the number -// of bytes decrypted by |ctx| thus far. +// of bytes decrypted by `ctx` thus far. `EVP_CIPHER_CTX_max_next_update` will +// return the maximum output for this call. // // In ciphers whose block size is not 1, such as CBC, individual calls to -// |EVP_DecryptUpdate_ex| may output more or less than |in_len| bytes: a single -// call to |EVP_DecryptUpdate_ex| may output at most |in_len + block_size - 1| -// bytes. Additionally, the total output across all |EVP_DecryptUpdate_ex| and -// |EVP_DecryptFinal_ex2| calls will be at most the total input. +// `EVP_DecryptUpdate_ex` may output more or less than `in_len` bytes: a single +// call to `EVP_DecryptUpdate_ex` may output at most `in_len + block_size - 1` +// bytes. Additionally, the total output across all `EVP_DecryptUpdate_ex` and +// `EVP_DecryptFinal_ex2` calls will be at most the total input. // // WARNING: if the cipher is an AEAD cipher, decrypted data should not be // parsed or otherwise processed until success has been returned by -// |EVP_EncryptFinal_ex2|. +// `EVP_EncryptFinal_ex2`. OPENSSL_EXPORT int EVP_DecryptUpdate_ex(EVP_CIPHER_CTX *ctx, uint8_t *out, size_t *out_len, size_t max_out_len, const uint8_t *in, size_t in_len); // EVP_DecryptFinal_ex2 finishes a decryption operation and writes up to -// |max_out| bytes of output to out. On success, it sets |*out_len| to the +// `max_out_len` bytes of output to out. On success, it sets `*out_len` to the // number of bytes written and returns one. Otherwise, it returns zero. // -// If |max_out| is not large enough for the output, the function will return +// If `max_out_len` is not large enough for the output, the function will return // zero. The size of output buffer needed depends on the cipher and the number -// of bytes decrypted. +// of bytes decrypted. `EVP_CIPHER_CTX_max_final` will return the maximum output +// for this call. // // If the block size is 1, there will be no final output at all; otherwise, at // most one block of ciphertext will be written to the output. @@ -206,111 +256,129 @@ OPENSSL_EXPORT int EVP_DecryptUpdate_ex(EVP_CIPHER_CTX *ctx, uint8_t *out, OPENSSL_EXPORT int EVP_DecryptFinal_ex2(EVP_CIPHER_CTX *ctx, uint8_t *out, size_t *out_len, size_t max_out_len); -// EVP_CipherUpdate_ex calls either |EVP_EncryptUpdate_ex| or -// |EVP_DecryptUpdate_ex| depending on how |ctx| has been setup. +// EVP_CipherUpdate_ex calls either `EVP_EncryptUpdate_ex` or +// `EVP_DecryptUpdate_ex` depending on how `ctx` has been setup. OPENSSL_EXPORT int EVP_CipherUpdate_ex(EVP_CIPHER_CTX *ctx, uint8_t *out, size_t *out_len, size_t max_out_len, const uint8_t *in, size_t in_len); -// EVP_CipherUpdateAAD adds |in_len| bytes from |in| to the AAD. The AAD must +// EVP_CipherUpdateAAD adds `in_len` bytes from `in` to the AAD. The AAD must // be fully specified in this way before any plaintext or ciphertext is -// supplied to the other functions. Please consider moving to the |EVP_AEAD| +// supplied to the other functions. Please consider moving to the `EVP_AEAD` // APIs instead. OPENSSL_EXPORT int EVP_CipherUpdateAAD(EVP_CIPHER_CTX *ctx, const uint8_t *in, size_t in_len); -// EVP_CipherFinal_ex2 calls either |EVP_EncryptFinal_ex2| or -// |EVP_DecryptFinal_ex2| depending on how |ctx| has been setup. +// EVP_CipherFinal_ex2 calls either `EVP_EncryptFinal_ex2` or +// `EVP_DecryptFinal_ex2` depending on how `ctx` has been setup. OPENSSL_EXPORT int EVP_CipherFinal_ex2(EVP_CIPHER_CTX *ctx, uint8_t *out, size_t *out_len, size_t max_out_len); +// EVP_CIPHER_CTX_max_next_update returns the maximum number of bytes that may +// be output from `ctx` by a call to `EVP_CipherUpdate_ex` with `in_len` bytes +// of input. This takes the current state of `ctx` into account, so the output +// will change as bytes are passed in and out. +OPENSSL_EXPORT size_t EVP_CIPHER_CTX_max_next_update(const EVP_CIPHER_CTX *ctx, + size_t in_len); + +// EVP_CIPHER_CTX_max_final returns the maximum number of bytes that may +// be output from `ctx` by a call to `EVP_CipherFinal_ex2`. This takes the +// current state of `ctx` into account, so the output will change as bytes are +// passed in and out. +OPENSSL_EXPORT size_t EVP_CIPHER_CTX_max_final(const EVP_CIPHER_CTX *ctx); + // Cipher context accessors. -// EVP_CIPHER_CTX_cipher returns the |EVP_CIPHER| underlying |ctx|, or NULL if +// EVP_CIPHER_CTX_cipher returns the `EVP_CIPHER` underlying `ctx`, or NULL if // none has been set. OPENSSL_EXPORT const EVP_CIPHER *EVP_CIPHER_CTX_cipher( const EVP_CIPHER_CTX *ctx); -// EVP_CIPHER_CTX_nid returns a NID identifying the |EVP_CIPHER| underlying -// |ctx| (e.g. |NID_aes_128_gcm|). It will crash if no cipher has been +// EVP_CIPHER_CTX_nid returns a NID identifying the `EVP_CIPHER` underlying +// `ctx` (e.g. `NID_aes_128_gcm`). It will crash if no cipher has been // configured. OPENSSL_EXPORT int EVP_CIPHER_CTX_nid(const EVP_CIPHER_CTX *ctx); -// EVP_CIPHER_CTX_encrypting returns one if |ctx| is configured for encryption +// EVP_CIPHER_CTX_encrypting returns one if `ctx` is configured for encryption // and zero otherwise. OPENSSL_EXPORT int EVP_CIPHER_CTX_encrypting(const EVP_CIPHER_CTX *ctx); // EVP_CIPHER_CTX_block_size returns the block size, in bytes, of the cipher -// underlying |ctx|, or one if the cipher is a stream cipher. It will crash if +// underlying `ctx`, or one if the cipher is a stream cipher. It will crash if // no cipher has been configured. OPENSSL_EXPORT unsigned EVP_CIPHER_CTX_block_size(const EVP_CIPHER_CTX *ctx); // EVP_CIPHER_CTX_key_length returns the key size, in bytes, of the cipher -// underlying |ctx| or zero if no cipher has been configured. +// underlying `ctx` or zero if no cipher has been configured. OPENSSL_EXPORT unsigned EVP_CIPHER_CTX_key_length(const EVP_CIPHER_CTX *ctx); // EVP_CIPHER_CTX_iv_length returns the IV size, in bytes, of the cipher -// underlying |ctx|. It will crash if no cipher has been configured. +// underlying `ctx`. It will crash if no cipher has been configured. OPENSSL_EXPORT unsigned EVP_CIPHER_CTX_iv_length(const EVP_CIPHER_CTX *ctx); // EVP_CIPHER_CTX_get_app_data returns the opaque, application data pointer for -// |ctx|, or NULL if none has been set. +// `ctx`, or NULL if none has been set. OPENSSL_EXPORT void *EVP_CIPHER_CTX_get_app_data(const EVP_CIPHER_CTX *ctx); // EVP_CIPHER_CTX_set_app_data sets the opaque, application data pointer for -// |ctx| to |data|. +// `ctx` to `data`. OPENSSL_EXPORT void EVP_CIPHER_CTX_set_app_data(EVP_CIPHER_CTX *ctx, void *data); // EVP_CIPHER_CTX_flags returns a value which is the OR of zero or more -// |EVP_CIPH_*| flags. It will crash if no cipher has been configured. +// `EVP_CIPH_*` flags. It will crash if no cipher has been configured. OPENSSL_EXPORT uint32_t EVP_CIPHER_CTX_flags(const EVP_CIPHER_CTX *ctx); -// EVP_CIPHER_CTX_mode returns one of the |EVP_CIPH_*| cipher mode values +// EVP_CIPHER_CTX_mode returns one of the `EVP_CIPH_*` cipher mode values // enumerated below. It will crash if no cipher has been configured. OPENSSL_EXPORT uint32_t EVP_CIPHER_CTX_mode(const EVP_CIPHER_CTX *ctx); -// EVP_CIPHER_CTX_ctrl is an |ioctl| like function. The |command| argument -// should be one of the |EVP_CTRL_*| values. The |arg| and |ptr| arguments are +// EVP_CIPHER_CTX_ctrl is an `ioctl` like function. The `command` argument +// should be one of the `EVP_CTRL_*` values. The `arg` and `ptr` arguments are // specific to the command in question. OPENSSL_EXPORT int EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int command, int arg, void *ptr); -// EVP_CIPHER_CTX_set_padding sets whether padding is enabled for |ctx| and -// returns one. Pass a non-zero |pad| to enable padding (the default) or zero +// EVP_CIPHER_CTX_set_padding sets whether padding is enabled for `ctx` and +// returns one. Pass a non-zero `pad` to enable padding (the default) or zero // to disable. OPENSSL_EXPORT int EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *ctx, int pad); -// EVP_CIPHER_CTX_set_key_length sets the key length for |ctx|. This is only +// EVP_CIPHER_CTX_set_key_length sets the key length for `ctx`. This is only // valid for ciphers that can take a variable length key. It returns one on // success and zero on error. +// +// In order to use this function, |ctx| must have been configured with an +// |EVP_CIPHER| with |EVP_CipherInit_ex|. Callers should first configure only +// the cipher, leaving |key| NULL, call |EVP_CIPHER_CTX_set_key_length|, and +// finally call |EVP_CipherInit_ex| with a NULL |cipher| and non-NULL |key|. OPENSSL_EXPORT int EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *ctx, unsigned key_len); // Cipher accessors. -// EVP_CIPHER_nid returns a NID identifying |cipher|. (For example, -// |NID_aes_128_gcm|.) +// EVP_CIPHER_nid returns a NID identifying `cipher`. (For example, +// `NID_aes_128_gcm`.) OPENSSL_EXPORT int EVP_CIPHER_nid(const EVP_CIPHER *cipher); -// EVP_CIPHER_block_size returns the block size, in bytes, for |cipher|, or one -// if |cipher| is a stream cipher. +// EVP_CIPHER_block_size returns the block size, in bytes, for `cipher`, or one +// if `cipher` is a stream cipher. OPENSSL_EXPORT unsigned EVP_CIPHER_block_size(const EVP_CIPHER *cipher); -// EVP_CIPHER_key_length returns the key size, in bytes, for |cipher|. If -// |cipher| can take a variable key length then this function returns the -// default key length and |EVP_CIPHER_flags| will return a value with -// |EVP_CIPH_VARIABLE_LENGTH| set. +// EVP_CIPHER_key_length returns the key size, in bytes, for `cipher`. If +// `cipher` can take a variable key length then this function returns the +// default key length and `EVP_CIPHER_flags` will return a value with +// `EVP_CIPH_VARIABLE_LENGTH` set. OPENSSL_EXPORT unsigned EVP_CIPHER_key_length(const EVP_CIPHER *cipher); -// EVP_CIPHER_iv_length returns the IV size, in bytes, of |cipher|, or zero if -// |cipher| doesn't take an IV. +// EVP_CIPHER_iv_length returns the IV size, in bytes, of `cipher`, or zero if +// `cipher` doesn't take an IV. OPENSSL_EXPORT unsigned EVP_CIPHER_iv_length(const EVP_CIPHER *cipher); // EVP_CIPHER_flags returns a value which is the OR of zero or more -// |EVP_CIPH_*| flags. +// `EVP_CIPH_*` flags. OPENSSL_EXPORT uint32_t EVP_CIPHER_flags(const EVP_CIPHER *cipher); // EVP_CIPHER_mode returns one of the cipher mode values enumerated below. @@ -319,31 +387,31 @@ OPENSSL_EXPORT uint32_t EVP_CIPHER_mode(const EVP_CIPHER *cipher); // Key derivation. -// EVP_BytesToKey generates a key and IV for the cipher |type| by iterating -// |md| |count| times using |data| and an optional |salt|, writing the result to -// |key| and |iv|. If not NULL, the |key| and |iv| buffers must have enough -// space to hold a key and IV for |type|, as returned by |EVP_CIPHER_key_length| -// and |EVP_CIPHER_iv_length|. This function returns the length of the key +// EVP_BytesToKey generates a key and IV for the cipher `type` by iterating +// `md` `count` times using `data` and an optional `salt`, writing the result to +// `key` and `iv`. If not NULL, the `key` and `iv` buffers must have enough +// space to hold a key and IV for `type`, as returned by `EVP_CIPHER_key_length` +// and `EVP_CIPHER_iv_length`. This function returns the length of the key // (without the IV) on success or zero on error. // -// If |salt| is NULL, the empty string is used as the salt. Salt lengths other -// than 0 and 8 are not supported by this function. Either of |key| or |iv| may +// If `salt` is NULL, the empty string is used as the salt. Salt lengths other +// than 0 and 8 are not supported by this function. Either of `key` or `iv` may // be NULL to skip that output. // -// When the total data derived is less than the size of |md|, this function +// When the total data derived is less than the size of `md`, this function // implements PBKDF1 from RFC 8018. Otherwise, it generalizes PBKDF1 by -// computing prepending the previous output to |data| and re-running PBKDF1 for +// computing prepending the previous output to `data` and re-running PBKDF1 for // further output. // // This function is provided for compatibility with legacy uses of PBKDF1. New -// applications should use a more modern algorithm, such as |EVP_PBE_scrypt|. +// applications should use a more modern algorithm, such as `EVP_PBE_scrypt`. OPENSSL_EXPORT int EVP_BytesToKey(const EVP_CIPHER *type, const EVP_MD *md, const uint8_t salt[8], const uint8_t *data, size_t data_len, unsigned count, uint8_t *key, uint8_t *iv); -// Cipher modes (for |EVP_CIPHER_mode|). +// Cipher modes (for `EVP_CIPHER_mode`). #define EVP_CIPH_STREAM_CIPHER 0x0 #define EVP_CIPH_ECB_MODE 0x1 @@ -354,30 +422,30 @@ OPENSSL_EXPORT int EVP_BytesToKey(const EVP_CIPHER *type, const EVP_MD *md, #define EVP_CIPH_GCM_MODE 0x6 #define EVP_CIPH_XTS_MODE 0x7 -// The following values are never returned from |EVP_CIPHER_mode| and are +// The following values are never returned from `EVP_CIPHER_mode` and are // included only to make it easier to compile code with BoringSSL. #define EVP_CIPH_CCM_MODE 0x8 #define EVP_CIPH_OCB_MODE 0x9 #define EVP_CIPH_WRAP_MODE 0xa -// Cipher flags (for |EVP_CIPHER_flags|). +// Cipher flags (for `EVP_CIPHER_flags`). // EVP_CIPH_VARIABLE_LENGTH indicates that the cipher takes a variable length // key. #define EVP_CIPH_VARIABLE_LENGTH 0x40 -// EVP_CIPH_ALWAYS_CALL_INIT indicates that the |init| function for the cipher +// EVP_CIPH_ALWAYS_CALL_INIT indicates that the `init` function for the cipher // should always be called when initialising a new operation, even if the key // is NULL to indicate that the same key is being used. #define EVP_CIPH_ALWAYS_CALL_INIT 0x80 // EVP_CIPH_CUSTOM_IV indicates that the cipher manages the IV itself rather -// than keeping it in the |iv| member of |EVP_CIPHER_CTX|. +// than keeping it in the `iv` member of `EVP_CIPHER_CTX`. #define EVP_CIPH_CUSTOM_IV 0x100 // EVP_CIPH_CTRL_INIT indicates that EVP_CTRL_INIT should be used when -// initialising an |EVP_CIPHER_CTX|. +// initialising an `EVP_CIPHER_CTX`. #define EVP_CIPH_CTRL_INIT 0x200 // EVP_CIPH_FLAG_CUSTOM_CIPHER indicates that the cipher manages blocking @@ -389,128 +457,143 @@ OPENSSL_EXPORT int EVP_BytesToKey(const EVP_CIPHER *type, const EVP_MD *md, // one. #define EVP_CIPH_FLAG_AEAD_CIPHER 0x800 -// EVP_CIPH_CUSTOM_COPY indicates that the |ctrl| callback should be called -// with |EVP_CTRL_COPY| at the end of normal |EVP_CIPHER_CTX_copy| +// EVP_CIPH_CUSTOM_COPY indicates that the `ctrl` callback should be called +// with `EVP_CTRL_COPY` at the end of normal `EVP_CIPHER_CTX_copy` // processing. #define EVP_CIPH_CUSTOM_COPY 0x1000 // EVP_CIPH_FLAG_NON_FIPS_ALLOW is meaningless. In OpenSSL it permits non-FIPS // algorithms in FIPS mode. But BoringSSL FIPS mode doesn't prohibit algorithms -// (it's up the the caller to use the FIPS module in a fashion compliant with +// (it's up to the caller to use the FIPS module in a fashion compliant with // their needs). Thus this exists only to allow code to compile. #define EVP_CIPH_FLAG_NON_FIPS_ALLOW 0 // Deprecated functions -// EVP_CipherInit acts like EVP_CipherInit_ex except that |EVP_CIPHER_CTX_init| -// is called on |cipher| first, if |cipher| is not NULL. +// EVP_CipherInit acts like `EVP_CipherInit_ex` except that +// `EVP_CIPHER_CTX_init` is called on `cipher` first, if `cipher` is not NULL. +// +// `EVP_CIPHER_CTX_init` is used for initializing uninitialized memory in an +// `EVP_CIPHER_CTX`, e.g. if it is declared as a local variable on the stack. +// Thus this function should not be used on objects that have already been +// initialized. +// +// WARNING: This differs from OpenSSL 1.1.x, where `EVP_CipherInit` and +// `EVP_CipherInit_ex` are largely equivalent. This difference is because +// BoringSSL, like OpenSSL 1.0.x, still supports stack-allocating +// `EVP_CIPHER_CTX`. Implementing the OpenSSL 1.1.x semantics would introduce +// uninitialized reads in those callers. OPENSSL_EXPORT int EVP_CipherInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, const uint8_t *key, const uint8_t *iv, int enc); -// EVP_EncryptInit calls |EVP_CipherInit| with |enc| equal to one. +// EVP_EncryptInit calls `EVP_CipherInit` with `enc` equal to one. +// +// WARNING: See discussion in `EVP_CipherInit`. OPENSSL_EXPORT int EVP_EncryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, const uint8_t *key, const uint8_t *iv); -// EVP_DecryptInit calls |EVP_CipherInit| with |enc| equal to zero. +// EVP_DecryptInit calls `EVP_CipherInit` with `enc` equal to zero. +// +// WARNING: See discussion in `EVP_CipherInit`. OPENSSL_EXPORT int EVP_DecryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, const uint8_t *key, const uint8_t *iv); -// EVP_CipherUpdate does the same as |EVP_CipherUpdate_ex|, except that no +// EVP_CipherUpdate does the same as `EVP_CipherUpdate_ex`, except that no // output size is given and thus no bounds checking is performed. // -// Additionally, if |ctx| is an AEAD cipher, e.g. |EVP_aes_128_gcm|, and |out| -// is NULL, this function instead behaves like |EVP_CipherUpdateAAD|. +// Additionally, if `ctx` is an AEAD cipher, e.g. `EVP_aes_128_gcm`, and `out` +// is NULL, this function instead behaves like `EVP_CipherUpdateAAD`. // -// WARNING: This function does not check bounds on |out|, and correctly sizing -// the output buffer is difficult. Use |EVP_CipherUpdate_ex| or -// |EVP_CipherUpdateAAD| instead. +// WARNING: This function does not check bounds on `out`, and correctly sizing +// the output buffer is difficult. Use `EVP_CipherUpdate_ex` or +// `EVP_CipherUpdateAAD` instead. OPENSSL_EXPORT int EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len, const uint8_t *in, int in_len); -// EVP_EncryptUpdate does the same as |EVP_EncryptUpdate_ex|, except that no +// EVP_EncryptUpdate does the same as `EVP_EncryptUpdate_ex`, except that no // output size is given and thus no bounds checking is performed. // -// Additionally, if |ctx| is an AEAD cipher, e.g. |EVP_aes_128_gcm|, and |out| -// is NULL, this function instead behaves like |EVP_CipherUpdateAAD|. +// Additionally, if `ctx` is an AEAD cipher, e.g. `EVP_aes_128_gcm`, and `out` +// is NULL, this function instead behaves like `EVP_CipherUpdateAAD`. // -// WARNING: This function does not check bounds on |out|, and correctly sizing -// the output buffer is difficult. Use |EVP_EncryptUpdate_ex| or -// |EVP_CipherUpdateAAD| instead. +// WARNING: This function does not check bounds on `out`, and correctly sizing +// the output buffer is difficult. Use `EVP_EncryptUpdate_ex` or +// `EVP_CipherUpdateAAD` instead. OPENSSL_EXPORT int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len, const uint8_t *in, int in_len); -// EVP_DecryptUpdate does the same as |EVP_DecryptUpdate_ex|, except that no +// EVP_DecryptUpdate does the same as `EVP_DecryptUpdate_ex`, except that no // output size is given and thus no bounds checking is performed. // -// Additionally, if |ctx| is an AEAD cipher, e.g. |EVP_aes_128_gcm|, and |out| -// is NULL, this function instead behaves like |EVP_CipherUpdateAAD|. +// Additionally, if `ctx` is an AEAD cipher, e.g. `EVP_aes_128_gcm`, and `out` +// is NULL, this function instead behaves like `EVP_CipherUpdateAAD`. // // WARNING: This function does not check bounds on out, and correctly sizing -// the output buffer is difficult. Use |EVP_DecryptUpdate_ex| or -// |EVP_CipherUpdateAAD| instead. +// the output buffer is difficult. Use `EVP_DecryptUpdate_ex` or +// `EVP_CipherUpdateAAD` instead. OPENSSL_EXPORT int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len, const uint8_t *in, int in_len); -// EVP_CipherFinal calls |EVP_CipherFinal_ex|. +// EVP_CipherFinal calls `EVP_CipherFinal_ex`. OPENSSL_EXPORT int EVP_CipherFinal(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len); -// EVP_CipherFinal_ex does the same as |EVP_CipherFinal_ex2|, except that no +// EVP_CipherFinal_ex does the same as `EVP_CipherFinal_ex2`, except that no // output size is given and thus no bounds checking is performed. // // WARNING: This function does not check bounds on out, and correctly sizing -// the output buffer is difficult. Use |EVP_CipherFinal_ex2| instead. +// the output buffer is difficult. Use `EVP_CipherFinal_ex2` instead. OPENSSL_EXPORT int EVP_CipherFinal_ex(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len); -// EVP_EncryptFinal calls |EVP_EncryptFinal_ex|. +// EVP_EncryptFinal calls `EVP_EncryptFinal_ex`. OPENSSL_EXPORT int EVP_EncryptFinal(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len); -// EVP_EncryptFinal_ex does the same as |EVP_EncryptFinal_ex2|, except that no +// EVP_EncryptFinal_ex does the same as `EVP_EncryptFinal_ex2`, except that no // output size is given and thus no bounds checking is performed. // // WARNING: This function does not check bounds on out, and correctly sizing -// the output buffer is difficult. Use |EVP_EncryptFinal_ex2| instead. +// the output buffer is difficult. Use `EVP_EncryptFinal_ex2` instead. OPENSSL_EXPORT int EVP_EncryptFinal_ex(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len); -// EVP_DecryptFinal calls |EVP_DecryptFinal_ex|. +// EVP_DecryptFinal calls `EVP_DecryptFinal_ex`. OPENSSL_EXPORT int EVP_DecryptFinal(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len); -// EVP_DecryptFinal_ex does the same as |EVP_DecryptFinal_ex2|, except that no +// EVP_DecryptFinal_ex does the same as `EVP_DecryptFinal_ex2`, except that no // output size is given and thus no bounds checking is performed. // // WARNING: This function does not check bounds on out, and correctly sizing -// the output buffer is difficult. Use |EVP_DecryptFinal_ex2| instead. +// the output buffer is difficult. Use `EVP_DecryptFinal_ex2` instead. OPENSSL_EXPORT int EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len); -// EVP_Cipher historically exposed an internal implementation detail of |ctx| -// and should not be used. Use |EVP_CipherUpdate| and |EVP_CipherFinal_ex| +// EVP_Cipher historically exposed an internal implementation detail of `ctx` +// and should not be used. Use `EVP_CipherUpdate` and `EVP_CipherFinal_ex` // instead. // -// If |ctx|'s cipher does not have the |EVP_CIPH_FLAG_CUSTOM_CIPHER| flag, it -// encrypts or decrypts |in_len| bytes from |in| and writes the resulting -// |in_len| bytes to |out|. It returns one on success and zero on error. -// |in_len| must be a multiple of the cipher's block size, or the behavior is +// If `ctx`'s cipher does not have the `EVP_CIPH_FLAG_CUSTOM_CIPHER` flag, it +// encrypts or decrypts `in_len` bytes from `in` and writes the resulting +// `in_len` bytes to `out`. It returns one on success and zero on error. +// `in_len` must be a multiple of the cipher's block size, or the behavior is // undefined. // // TODO(davidben): Rather than being undefined (it'll often round the length up // and likely read past the buffer), just fail the operation. // -// If |ctx|'s cipher has the |EVP_CIPH_FLAG_CUSTOM_CIPHER| flag, it runs in one -// of two modes: If |in| is non-NULL, it behaves like |EVP_CipherUpdate|. If -// |in| is NULL, it behaves like |EVP_CipherFinal_ex|. In both cases, it returns -// |*out_len| on success and -1 on error. +// If `ctx`'s cipher has the `EVP_CIPH_FLAG_CUSTOM_CIPHER` flag, it runs in one +// of two modes: If `in` is non-NULL, it behaves like `EVP_CipherUpdate`. If +// `in` is NULL, it behaves like `EVP_CipherFinal_ex`. In both cases, it returns +// `*out_len` on success and -1 on error. // // WARNING: The two possible calling conventions of this function signal errors // incompatibly. In the first, zero indicates an error. In the second, zero @@ -521,20 +604,20 @@ OPENSSL_EXPORT int EVP_Cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, // EVP_add_cipher_alias does nothing and returns one. OPENSSL_EXPORT int EVP_add_cipher_alias(const char *a, const char *b); -// EVP_get_cipherbyname returns an |EVP_CIPHER| given a human readable name in -// |name|, or NULL if the name is unknown. Note using this function links almost +// EVP_get_cipherbyname returns an `EVP_CIPHER` given a human readable name in +// `name`, or NULL if the name is unknown. Note using this function links almost // every cipher implemented by BoringSSL into the binary, not just the ones the // caller requests. Size-conscious callers, such as client software, should not // use this function. OPENSSL_EXPORT const EVP_CIPHER *EVP_get_cipherbyname(const char *name); // These AEADs are deprecated AES-GCM implementations that set -// |EVP_CIPH_FLAG_CUSTOM_CIPHER|. Use |EVP_aead_aes_128_gcm| and -// |EVP_aead_aes_256_gcm| instead. +// `EVP_CIPH_FLAG_CUSTOM_CIPHER`. Use `EVP_aead_aes_128_gcm` and +// `EVP_aead_aes_256_gcm` instead. // // WARNING: Although these APIs allow streaming an individual AES-GCM operation, -// this is not secure. Until calling |EVP_DecryptFinal_ex|, the tag has not yet -// been checked and output released by |EVP_DecryptUpdate| is unauthenticated +// this is not secure. Until calling `EVP_DecryptFinal_ex`, the tag has not yet +// been checked and output released by `EVP_DecryptUpdate` is unauthenticated // and easily manipulated by attackers. Callers must buffer the output and may // not act on it until the entire operation is complete. OPENSSL_EXPORT const EVP_CIPHER *EVP_aes_128_gcm(void); @@ -547,27 +630,27 @@ OPENSSL_EXPORT const EVP_CIPHER *EVP_aes_192_ctr(void); OPENSSL_EXPORT const EVP_CIPHER *EVP_aes_192_gcm(void); OPENSSL_EXPORT const EVP_CIPHER *EVP_aes_192_ofb(void); -// EVP_des_ede3_ecb is an alias for |EVP_des_ede3|. Use the former instead. +// EVP_des_ede3_ecb is an alias for `EVP_des_ede3`. Use the former instead. OPENSSL_EXPORT const EVP_CIPHER *EVP_des_ede3_ecb(void); // EVP_aes_128_cfb128 is only available in decrepit. OPENSSL_EXPORT const EVP_CIPHER *EVP_aes_128_cfb128(void); -// EVP_aes_128_cfb is an alias for |EVP_aes_128_cfb128| and is only available in +// EVP_aes_128_cfb is an alias for `EVP_aes_128_cfb128` and is only available in // decrepit. OPENSSL_EXPORT const EVP_CIPHER *EVP_aes_128_cfb(void); // EVP_aes_192_cfb128 is only available in decrepit. OPENSSL_EXPORT const EVP_CIPHER *EVP_aes_192_cfb128(void); -// EVP_aes_192_cfb is an alias for |EVP_aes_192_cfb128| and is only available in +// EVP_aes_192_cfb is an alias for `EVP_aes_192_cfb128` and is only available in // decrepit. OPENSSL_EXPORT const EVP_CIPHER *EVP_aes_192_cfb(void); // EVP_aes_256_cfb128 is only available in decrepit. OPENSSL_EXPORT const EVP_CIPHER *EVP_aes_256_cfb128(void); -// EVP_aes_256_cfb is an alias for |EVP_aes_256_cfb128| and is only available in +// EVP_aes_256_cfb is an alias for `EVP_aes_256_cfb128` and is only available in // decrepit. OPENSSL_EXPORT const EVP_CIPHER *EVP_aes_256_cfb(void); @@ -600,7 +683,7 @@ OPENSSL_EXPORT void EVP_CIPHER_CTX_set_flags(const EVP_CIPHER_CTX *ctx, // EVP_CIPH_NO_PADDING disables padding in block ciphers. #define EVP_CIPH_NO_PADDING 0x800 -// The following are |EVP_CIPHER_CTX_ctrl| commands. +// The following are `EVP_CIPHER_CTX_ctrl` commands. #define EVP_CTRL_INIT 0x0 #define EVP_CTRL_SET_KEY_LENGTH 0x1 #define EVP_CTRL_GET_RC2_KEY_BITS 0x2 @@ -625,7 +708,7 @@ OPENSSL_EXPORT void EVP_CIPHER_CTX_set_flags(const EVP_CIPHER_CTX *ctx, #define EVP_GCM_TLS_EXPLICIT_IV_LEN 8 #define EVP_GCM_TLS_TAG_LEN 16 -// The following are legacy aliases for AEAD |EVP_CIPHER_CTX_ctrl| values. +// The following are legacy aliases for AEAD `EVP_CIPHER_CTX_ctrl` values. #define EVP_CTRL_GCM_SET_IVLEN EVP_CTRL_AEAD_SET_IVLEN #define EVP_CTRL_GCM_GET_TAG EVP_CTRL_AEAD_GET_TAG #define EVP_CTRL_GCM_SET_TAG EVP_CTRL_AEAD_SET_TAG @@ -640,19 +723,19 @@ struct evp_cipher_ctx_st { const EVP_CIPHER *cipher; // app_data is a pointer to opaque, user data. - void *app_data; // application stuff + void *app_data; // application stuff - // cipher_data points to the |cipher| specific state. + // cipher_data points to the `cipher` specific state. void *cipher_data; // key_len contains the length of the key, which may differ from - // |cipher->key_len| if the cipher can take a variable key length. + // `cipher->key_len` if the cipher can take a variable key length. unsigned key_len; // encrypt is one if encrypting and zero if decrypting. int encrypt; - // flags contains the OR of zero or more |EVP_CIPH_*| flags, above. + // flags contains the OR of zero or more `EVP_CIPH_*` flags, above. uint32_t flags; // oiv contains the original IV value. @@ -666,20 +749,17 @@ struct evp_cipher_ctx_st { uint8_t buf[EVP_MAX_BLOCK_LENGTH]; // buf_len contains the number of bytes of a partial block contained in - // |buf|. + // `buf`. int buf_len; - // num contains the number of bytes of |iv| which are valid for modes that + // num contains the number of bytes of `iv` which are valid for modes that // manage partial blocks themselves. unsigned num; - // final_used is non-zero if the |final| buffer contains plaintext. + // final_used is non-zero if the `final` buffer contains plaintext. int final_used; uint8_t final[EVP_MAX_BLOCK_LENGTH]; // possible final block - - // Has this structure been rendered unusable by a failure. - int poisoned; } /* EVP_CIPHER_CTX */; typedef struct evp_cipher_info_st { @@ -698,9 +778,9 @@ BSSL_NAMESPACE_BEGIN BORINGSSL_MAKE_DELETER(EVP_CIPHER_CTX, EVP_CIPHER_CTX_free) -using ScopedEVP_CIPHER_CTX = - internal::StackAllocated; +BORINGSSL_MAKE_STACK_TRAITS(EVP_CIPHER_CTX, EVP_CIPHER_CTX_init, + EVP_CIPHER_CTX_cleanup) +using ScopedEVP_CIPHER_CTX = internal::StackAllocated; BSSL_NAMESPACE_END diff --git a/third_party/boringssl/src/include/openssl/cmac.h b/third_party/boringssl/src/include/openssl/cmac.h index 3e5a2d654..33bbf5806 100644 --- a/third_party/boringssl/src/include/openssl/cmac.h +++ b/third_party/boringssl/src/include/openssl/cmac.h @@ -30,8 +30,8 @@ extern "C" { // One-shot functions. -// AES_CMAC calculates the 16-byte, CMAC authenticator of |in_len| bytes of -// |in| and writes it to |out|. The |key_len| may be 16 or 32 bytes to select +// AES_CMAC calculates the 16-byte, CMAC authenticator of `in_len` bytes of +// `in` and writes it to `out`. The `key_len` may be 16 or 32 bytes to select // between AES-128 and AES-256. It returns one on success or zero on error. OPENSSL_EXPORT int AES_CMAC(uint8_t out[16], const uint8_t *key, size_t key_len, const uint8_t *in, size_t in_len); @@ -39,36 +39,36 @@ OPENSSL_EXPORT int AES_CMAC(uint8_t out[16], const uint8_t *key, size_t key_len, // Incremental interface. -// CMAC_CTX_new allocates a fresh |CMAC_CTX| and returns it, or NULL on +// CMAC_CTX_new allocates a fresh `CMAC_CTX` and returns it, or NULL on // error. OPENSSL_EXPORT CMAC_CTX *CMAC_CTX_new(void); -// CMAC_CTX_free frees a |CMAC_CTX|. +// CMAC_CTX_free frees a `CMAC_CTX`. OPENSSL_EXPORT void CMAC_CTX_free(CMAC_CTX *ctx); -// CMAC_CTX_copy sets |out| to be a duplicate of the current state |in|. It +// CMAC_CTX_copy sets `out` to be a duplicate of the current state `in`. It // returns one on success and zero on error. OPENSSL_EXPORT int CMAC_CTX_copy(CMAC_CTX *out, const CMAC_CTX *in); -// CMAC_Init configures |ctx| to use the given |key| and |cipher|. The CMAC RFC -// only specifies the use of AES-128 thus |key_len| should be 16 and |cipher| -// should be |EVP_aes_128_cbc()|. However, this implementation also supports -// AES-256 by setting |key_len| to 32 and |cipher| to |EVP_aes_256_cbc()|. The -// |engine| argument is ignored. +// CMAC_Init configures `ctx` to use the given `key` and `cipher`. The CMAC RFC +// only specifies the use of AES-128 thus `key_len` should be 16 and `cipher` +// should be `EVP_aes_128_cbc()`. However, this implementation also supports +// AES-256 by setting `key_len` to 32 and `cipher` to `EVP_aes_256_cbc()`. The +// `engine` argument is ignored. // // It returns one on success or zero on error. OPENSSL_EXPORT int CMAC_Init(CMAC_CTX *ctx, const void *key, size_t key_len, const EVP_CIPHER *cipher, ENGINE *engine); -// CMAC_Reset resets |ctx| so that a fresh message can be authenticated. +// CMAC_Reset resets `ctx` so that a fresh message can be authenticated. OPENSSL_EXPORT int CMAC_Reset(CMAC_CTX *ctx); -// CMAC_Update processes |in_len| bytes of message from |in|. It returns one on +// CMAC_Update processes `in_len` bytes of message from `in`. It returns one on // success or zero on error. OPENSSL_EXPORT int CMAC_Update(CMAC_CTX *ctx, const uint8_t *in, size_t in_len); -// CMAC_Final sets |*out_len| to 16 and, if |out| is not NULL, writes 16 bytes +// CMAC_Final sets `*out_len` to 16 and, if `out` is not NULL, writes 16 bytes // of authenticator to it. It returns one on success or zero on error. OPENSSL_EXPORT int CMAC_Final(CMAC_CTX *ctx, uint8_t *out, size_t *out_len); diff --git a/third_party/boringssl/src/include/openssl/cms.h b/third_party/boringssl/src/include/openssl/cms.h index fdb8e9eec..5a445178e 100644 --- a/third_party/boringssl/src/include/openssl/cms.h +++ b/third_party/boringssl/src/include/openssl/cms.h @@ -15,7 +15,7 @@ #ifndef OPENSSL_HEADER_CMS_H #define OPENSSL_HEADER_CMS_H -#include // IWYU pragma: export +#include // IWYU pragma: export #include @@ -31,10 +31,10 @@ extern "C" { // for S/MIME, is out of scope for BoringSSL. // // As this library is intentionally not a general CMS implementation, BoringSSL -// continues to define |OPENSSL_NO_CMS|, so that most callers turn off their +// continues to define `OPENSSL_NO_CMS`, so that most callers turn off their // general-purpose CMS code. In callers that are compatible with this subset, -// the |BORINGSSL_NO_NO_CMS| build option can be used to suppress -// |OPENSSL_NO_CMS|. +// the `BORINGSSL_NO_NO_CMS` build option can be used to suppress +// `OPENSSL_NO_CMS`. DECLARE_STACK_OF(X509) @@ -51,79 +51,81 @@ DECLARE_STACK_OF(X509) #define CMS_USE_KEYID 0x10000 #define CMS_NO_SIGNING_TIME 0x400000 -// CMS_sign returns a newly-allocated |CMS_ContentInfo| structure for building a +// CMS_sign returns a newly-allocated `CMS_ContentInfo` structure for building a // SignedData (RFC 5652), or NULL on error. // -// |certs| must be NULL or zero length. BoringSSL does not support embedding +// `certs` must be NULL or zero length. BoringSSL does not support embedding // certificates in SignedData. // -// |flags| must contain |CMS_DETACHED|, which indicates an external signature. +// `flags` must contain `CMS_DETACHED`, which indicates an external signature. // BoringSSL only supports generating external signatures and does not support // embedding encapsulated content directly in a SignedData. // -// If |pkey| is non-NULL, |CMS_add1_signer| is automatically called with -// |signcert|, |pkey|, a default hash of SHA-256, and |flags|. |flags| will then -// additionally be interpreted as in |CMS_add1_signer|. -// -// If |CMS_PARTIAL| or |CMS_STREAM| is set in |flags|, the object will be left -// incomplete. |data| will then be ignored and should be NULL. The caller can -// then continue configuring it and finalizing it with |CMS_final|. Otherwise, -// the object will be finalized with |data| and |flags| passed to |CMS_final|. -OPENSSL_EXPORT CMS_ContentInfo *CMS_sign(X509 *signcert, EVP_PKEY *pkey, - STACK_OF(X509) *certs, BIO *data, +// If `pkey` is non-NULL, `CMS_add1_signer` is automatically called with +// `signcert`, `pkey`, a default hash of SHA-256, and `flags`. `flags` will then +// additionally be interpreted as in `CMS_add1_signer`. +// +// If `CMS_PARTIAL` or `CMS_STREAM` is set in `flags`, the object will be left +// incomplete. `data` will then be ignored and should be NULL. The caller can +// then continue configuring it and finalizing it with `CMS_final`. Otherwise, +// the object will be finalized with `data` and `flags` passed to `CMS_final`. +OPENSSL_EXPORT CMS_ContentInfo *CMS_sign(const X509 *signcert, + const EVP_PKEY *pkey, + const STACK_OF(X509) *certs, BIO *data, uint32_t flags); -// CMS_ContentInfo_free releases memory associated with |cms|. +// CMS_ContentInfo_free releases memory associated with `cms`. OPENSSL_EXPORT void CMS_ContentInfo_free(CMS_ContentInfo *cms); -// CMS_add1_signer adds a signer to |cms|, which must be a SignedData created by -// |CMS_sign|, with the |CMS_PARTIAL| flag set. The signer will use |signcert|, -// |pkey|, and |md| for the signing certificate, private key, and digest +// CMS_add1_signer adds a signer to `cms`, which must be a SignedData created by +// `CMS_sign`, with the `CMS_PARTIAL` flag set. The signer will use `signcert`, +// `pkey`, and `md` for the signing certificate, private key, and digest // algorithm, respectively. It returns a non-NULL pointer to the signer on -// success, and NULL on error. The signer is owned by |cms| and should not be +// success, and NULL on error. The signer is owned by `cms` and should not be // released by the caller. // -// |flags| is interpreted as follows: +// `flags` is interpreted as follows: // -// - |CMS_PARTIAL| must not be set. BoringSSL does not support configuring a +// - `CMS_PARTIAL` must not be set. BoringSSL does not support configuring a // signer in multiple steps. // -// - |CMS_NOCERTS| must be set. BoringSSL does not support embedding +// - `CMS_NOCERTS` must be set. BoringSSL does not support embedding // certificates in SignedData. // -// - |CMS_NOATTR| must be set. BoringSSL does not support attributes in +// - `CMS_NOATTR` must be set. BoringSSL does not support attributes in // SignedData. // -// - If |CMS_USE_KEYID| is set, SignerInfos will be identified by subject key -// identifier instead of issuer and serial number. |signcert| must then have +// - If `CMS_USE_KEYID` is set, SignerInfos will be identified by subject key +// identifier instead of issuer and serial number. `signcert` must then have // the subject key identifier extension. // -// BoringSSL currently only supports one signer per |CMS_ContentInfo|. +// BoringSSL currently only supports one signer per `CMS_ContentInfo`. // Subsequent calls will fail. Additionally, only RSA keys are currently -// supported for |pkey|. +// supported for `pkey`. OPENSSL_EXPORT CMS_SignerInfo *CMS_add1_signer(CMS_ContentInfo *cms, - X509 *signcert, EVP_PKEY *pkey, + const X509 *signcert, + const EVP_PKEY *pkey, const EVP_MD *md, uint32_t flags); -// CMS_final finalizes constructing |cms|, which must have been initialized with -// the |CMS_PARTIAL| flag. |data| is read, until EOF, as the data to be +// CMS_final finalizes constructing `cms`, which must have been initialized with +// the `CMS_PARTIAL` flag. `data` is read, until EOF, as the data to be // processed by CMS. It returns one on success and zero on error. // -// |CMS_BINARY| must be set in |flags|. BoringSSL does not support translating +// `CMS_BINARY` must be set in `flags`. BoringSSL does not support translating // inputs according to S/MIME. // -// |dcont| must be NULL. What a non-NULL |dcont| does is not clearly documented +// `dcont` must be NULL. What a non-NULL `dcont` does is not clearly documented // by OpenSSL, and there are no tests to demonstrate its behavior. OPENSSL_EXPORT int CMS_final(CMS_ContentInfo *cms, BIO *data, BIO *dcont, uint32_t flags); -// i2d_CMS_bio encodes |cms| as a DER-encoded ContentInfo structure (RFC 5652). +// i2d_CMS_bio encodes `cms` as a DER-encoded ContentInfo structure (RFC 5652). // It returns one on success and zero on failure. OPENSSL_EXPORT int i2d_CMS_bio(BIO *out, CMS_ContentInfo *cms); -// i2d_CMS_bio_stream calls |i2d_CMS_bio|. |in| must be NULL and |flags| must -// not contain |CMS_STREAM|. BoringSSL does not support any streaming modes for +// i2d_CMS_bio_stream calls `i2d_CMS_bio`. `in` must be NULL and `flags` must +// not contain `CMS_STREAM`. BoringSSL does not support any streaming modes for // CMS. OPENSSL_EXPORT int i2d_CMS_bio_stream(BIO *out, CMS_ContentInfo *cms, BIO *in, int flags); diff --git a/third_party/boringssl/src/include/openssl/conf.h b/third_party/boringssl/src/include/openssl/conf.h index 875124ec2..947657755 100644 --- a/third_party/boringssl/src/include/openssl/conf.h +++ b/third_party/boringssl/src/include/openssl/conf.h @@ -36,7 +36,7 @@ extern "C" { // [section_name] // key2=value2 // -// Config files are represented by a |CONF|. Use of this module is strongly +// Config files are represented by a `CONF`. Use of this module is strongly // discouraged. It is a remnant of the OpenSSL command-line tool. Parsing an // untrusted input as a config file risks string injection and denial of service // vulnerabilities. @@ -51,32 +51,32 @@ struct conf_value_st { DEFINE_STACK_OF(CONF_VALUE) -// NCONF_new returns a fresh, empty |CONF|, or NULL on error. The |method| +// NCONF_new returns a fresh, empty `CONF`, or NULL on error. The `method` // argument must be NULL. OPENSSL_EXPORT CONF *NCONF_new(void *method); -// NCONF_free frees all the data owned by |conf| and then |conf| itself. +// NCONF_free frees all the data owned by `conf` and then `conf` itself. OPENSSL_EXPORT void NCONF_free(CONF *conf); -// NCONF_load parses the file named |filename| and adds the values found to -// |conf|. It returns one on success and zero on error. In the event of an -// error, if |out_error_line| is not NULL, |*out_error_line| is set to the +// NCONF_load parses the file named `filename` and adds the values found to +// `conf`. It returns one on success and zero on error. In the event of an +// error, if `out_error_line` is not NULL, `*out_error_line` is set to the // number of the line that contained the error. OPENSSL_EXPORT int NCONF_load(CONF *conf, const char *filename, long *out_error_line); -// NCONF_load_bio acts like |NCONF_load| but reads from |bio| rather than from +// NCONF_load_bio acts like `NCONF_load` but reads from `bio` rather than from // a named file. OPENSSL_EXPORT int NCONF_load_bio(CONF *conf, BIO *bio, long *out_error_line); -// NCONF_get_section returns a stack of values for a given section in |conf|. -// If |section| is NULL, the default section is returned. It returns NULL on +// NCONF_get_section returns a stack of values for a given section in `conf`. +// If `section` is NULL, the default section is returned. It returns NULL on // error. OPENSSL_EXPORT const STACK_OF(CONF_VALUE) *NCONF_get_section( const CONF *conf, const char *section); -// NCONF_get_string returns the value of the key |name|, in section |section|. -// The |section| argument may be NULL to indicate the default section. It +// NCONF_get_string returns the value of the key `name`, in section `section`. +// The `section` argument may be NULL to indicate the default section. It // returns the value or NULL on error. OPENSSL_EXPORT const char *NCONF_get_string(const CONF *conf, const char *section, @@ -91,7 +91,7 @@ OPENSSL_EXPORT const char *NCONF_get_string(const CONF *conf, #define CONF_MFLAGS_IGNORE_MISSING_FILE 0 // CONF_modules_load_file returns one. BoringSSL is defined to have no config -// file options, thus loading from |filename| always succeeds by doing nothing. +// file options, thus loading from `filename` always succeeds by doing nothing. OPENSSL_EXPORT int CONF_modules_load_file(const char *filename, const char *appname, unsigned long flags); diff --git a/third_party/boringssl/src/include/openssl/configuration.h b/third_party/boringssl/src/include/openssl/configuration.h new file mode 100644 index 000000000..74c1d7a48 --- /dev/null +++ b/third_party/boringssl/src/include/openssl/configuration.h @@ -0,0 +1,18 @@ +// Copyright 2026 The BoringSSL Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +/* This header is provided in order to make compiling against code that expects + OpenSSL easier. */ + +#include "base.h" diff --git a/third_party/boringssl/src/include/openssl/crypto.h b/third_party/boringssl/src/include/openssl/crypto.h index a22bfa236..8f6af1f0e 100644 --- a/third_party/boringssl/src/include/openssl/crypto.h +++ b/third_party/boringssl/src/include/openssl/crypto.h @@ -18,11 +18,11 @@ #include // IWYU pragma: export #include -// Upstream OpenSSL defines |OPENSSL_malloc|, etc., in crypto.h rather than +// Upstream OpenSSL defines `OPENSSL_malloc`, etc., in crypto.h rather than // mem.h. #include -// Upstream OpenSSL defines |CRYPTO_LOCK|, etc., in crypto.h rather than +// Upstream OpenSSL defines `CRYPTO_LOCK`, etc., in crypto.h rather than // thread.h. #include @@ -111,7 +111,7 @@ enum fips_counter_t { }; // FIPS_read_counter returns a counter of the number of times the specific -// function denoted by |counter| has been used. This always returns zero unless +// function denoted by `counter` has been used. This always returns zero unless // BoringSSL was built with BORINGSSL_FIPS_COUNTERS defined. OPENSSL_EXPORT size_t FIPS_read_counter(enum fips_counter_t counter); @@ -129,7 +129,7 @@ OPENSSL_EXPORT size_t FIPS_read_counter(enum fips_counter_t counter); #define OPENSSL_DIR 4 // OpenSSL_version is a compatibility function that returns the string -// "BoringSSL" if |which| is |OPENSSL_VERSION| and placeholder strings +// "BoringSSL" if `which` is `OPENSSL_VERSION` and placeholder strings // otherwise. OPENSSL_EXPORT const char *OpenSSL_version(int which); @@ -139,7 +139,7 @@ OPENSSL_EXPORT const char *OpenSSL_version(int which); #define SSLEAY_PLATFORM OPENSSL_PLATFORM #define SSLEAY_DIR OPENSSL_DIR -// SSLeay_version calls |OpenSSL_version|. +// SSLeay_version calls `OpenSSL_version`. OPENSSL_EXPORT const char *SSLeay_version(int which); // SSLeay is a compatibility function that returns OPENSSL_VERSION_NUMBER from @@ -196,8 +196,8 @@ OPENSSL_EXPORT int OPENSSL_init_crypto(uint64_t opts, // OPENSSL_cleanup does nothing. OPENSSL_EXPORT void OPENSSL_cleanup(void); -// FIPS_mode_set returns one if |on| matches whether BoringSSL was built with -// |BORINGSSL_FIPS| and zero otherwise. +// FIPS_mode_set returns one if `on` matches whether BoringSSL was built with +// `BORINGSSL_FIPS` and zero otherwise. OPENSSL_EXPORT int FIPS_mode_set(int on); // FIPS_module_name returns the name of the FIPS module. @@ -210,10 +210,10 @@ OPENSSL_EXPORT const uint8_t *FIPS_module_hash(void); // isn't exactly at a verified version. The version, expressed in base 10, will // be a date in the form yyyymmdd. // -// (This format exceeds a |uint32_t| in the year 4294.) +// (This format exceeds a `uint32_t` in the year 4294.) OPENSSL_EXPORT uint32_t FIPS_version(void); -// FIPS_query_algorithm_status returns one if |algorithm| is FIPS validated in +// FIPS_query_algorithm_status returns one if `algorithm` is FIPS validated in // the current BoringSSL and zero otherwise. OPENSSL_EXPORT int FIPS_query_algorithm_status(const char *algorithm); diff --git a/third_party/boringssl/src/include/openssl/ctrdrbg.h b/third_party/boringssl/src/include/openssl/ctrdrbg.h index 849fc82ba..678b08b61 100644 --- a/third_party/boringssl/src/include/openssl/ctrdrbg.h +++ b/third_party/boringssl/src/include/openssl/ctrdrbg.h @@ -53,17 +53,17 @@ extern "C" { #define CTR_DRBG_NONCE_LEN 16 // CTR_DRBG_MAX_GENERATE_LENGTH is the maximum number of bytes that can be -// generated in a single call to |CTR_DRBG_generate|. +// generated in a single call to `CTR_DRBG_generate`. #define CTR_DRBG_MAX_GENERATE_LENGTH 65536 -// CTR_DRBG_new returns an initialized |CTR_DRBG_STATE|, or NULL if either -// allocation failed or if |personalization_len| is invalid. This DRBG will not +// CTR_DRBG_new returns an initialized `CTR_DRBG_STATE`, or NULL if either +// allocation failed or if `personalization_len` is invalid. This DRBG will not // use a derivation function. OPENSSL_EXPORT CTR_DRBG_STATE *CTR_DRBG_new( const uint8_t entropy[CTR_DRBG_ENTROPY_LEN], const uint8_t *personalization, size_t personalization_len); -// CTR_DRBG_new_df returns an initialized |CTR_DRBG_STATE|, or NULL if either +// CTR_DRBG_new_df returns an initialized `CTR_DRBG_STATE`, or NULL if either // allocation failed or if an argument is invalid. This DRBG will use a // derivation function. OPENSSL_EXPORT CTR_DRBG_STATE *CTR_DRBG_new_df( @@ -71,11 +71,11 @@ OPENSSL_EXPORT CTR_DRBG_STATE *CTR_DRBG_new_df( const uint8_t nonce[CTR_DRBG_NONCE_LEN], const uint8_t *personalization, size_t personalization_len); -// CTR_DRBG_free frees |state| if non-NULL, or else does nothing. +// CTR_DRBG_free frees `state` if non-NULL, or else does nothing. OPENSSL_EXPORT void CTR_DRBG_free(CTR_DRBG_STATE *state); -// CTR_DRBG_reseed reseeds |drbg| given |CTR_DRBG_ENTROPY_LEN| bytes of entropy -// in |entropy| and, optionally, up to |CTR_DRBG_SEED_LEN| bytes of +// CTR_DRBG_reseed reseeds `drbg` given `CTR_DRBG_ENTROPY_LEN` bytes of entropy +// in `entropy` and, optionally, up to `CTR_DRBG_SEED_LEN` bytes of // additional data. It returns one on success or zero on error. OPENSSL_EXPORT int CTR_DRBG_reseed(CTR_DRBG_STATE *drbg, const uint8_t entropy[CTR_DRBG_ENTROPY_LEN], @@ -83,23 +83,23 @@ OPENSSL_EXPORT int CTR_DRBG_reseed(CTR_DRBG_STATE *drbg, size_t additional_data_len); // CTR_DRBG_reseed_ex acts like `CTR_DRBG_reseed` but with variable-length -// entropy input, up to |CTR_DRBG_MAX_ENTROPY_LEN|. +// entropy input, up to `CTR_DRBG_MAX_ENTROPY_LEN`. OPENSSL_EXPORT int CTR_DRBG_reseed_ex(CTR_DRBG_STATE *drbg, const uint8_t *entropy, size_t entropy_len, const uint8_t *additional_data, size_t additional_data_len); -// CTR_DRBG_generate processes to up |CTR_DRBG_ENTROPY_LEN| bytes of additional -// data (if any) and then writes |out_len| random bytes to |out|, where -// |out_len| <= |CTR_DRBG_MAX_GENERATE_LENGTH|. It returns one on success or +// CTR_DRBG_generate processes to up `CTR_DRBG_ENTROPY_LEN` bytes of additional +// data (if any) and then writes `out_len` random bytes to `out`, where +// `out_len` <= `CTR_DRBG_MAX_GENERATE_LENGTH`. It returns one on success or // zero on error. OPENSSL_EXPORT int CTR_DRBG_generate(CTR_DRBG_STATE *drbg, uint8_t *out, size_t out_len, const uint8_t *additional_data, size_t additional_data_len); -// CTR_DRBG_clear zeroises the state of |drbg|. +// CTR_DRBG_clear zeroises the state of `drbg`. OPENSSL_EXPORT void CTR_DRBG_clear(CTR_DRBG_STATE *drbg); diff --git a/third_party/boringssl/src/include/openssl/curve25519.h b/third_party/boringssl/src/include/openssl/curve25519.h index 85a08b80a..1eac821d8 100644 --- a/third_party/boringssl/src/include/openssl/curve25519.h +++ b/third_party/boringssl/src/include/openssl/curve25519.h @@ -15,7 +15,8 @@ #ifndef OPENSSL_HEADER_CURVE25519_H #define OPENSSL_HEADER_CURVE25519_H -#include // IWYU pragma: export +#include // IWYU pragma: export +#include #if defined(__cplusplus) extern "C" { @@ -37,12 +38,12 @@ extern "C" { #define X25519_PUBLIC_VALUE_LEN 32 #define X25519_SHARED_KEY_LEN 32 -// X25519_keypair sets |out_public_value| and |out_private_key| to a freshly +// X25519_keypair sets `out_public_value` and `out_private_key` to a freshly // generated, public–private key pair. OPENSSL_EXPORT void X25519_keypair(uint8_t out_public_value[32], uint8_t out_private_key[32]); -// X25519 writes a shared key to |out_shared_key| that is calculated from the +// X25519 writes a shared key to `out_shared_key` that is calculated from the // given private key and the peer's public value. It returns one on success and // zero on error. // @@ -53,7 +54,7 @@ OPENSSL_EXPORT int X25519(uint8_t out_shared_key[32], const uint8_t peer_public_value[32]); // X25519_public_from_private calculates a Diffie-Hellman public value from the -// given private key and writes it to |out_public_value|. +// given private key and writes it to `out_public_value`. OPENSSL_EXPORT void X25519_public_from_private(uint8_t out_public_value[32], const uint8_t private_key[32]); @@ -72,25 +73,61 @@ OPENSSL_EXPORT void X25519_public_from_private(uint8_t out_public_value[32], #define ED25519_PUBLIC_KEY_LEN 32 #define ED25519_SIGNATURE_LEN 64 -// ED25519_keypair sets |out_public_key| and |out_private_key| to a freshly +// ED25519_keypair sets `out_public_key` and `out_private_key` to a freshly // generated, public–private key pair. OPENSSL_EXPORT void ED25519_keypair(uint8_t out_public_key[32], uint8_t out_private_key[64]); -// ED25519_sign sets |out_sig| to be a signature of |message_len| bytes from -// |message| using |private_key|. It returns one on success or zero on +// ED25519_sign sets `out_sig` to be a signature of `message_len` bytes from +// `message` using `private_key`. It returns one on success or zero on // allocation failure. OPENSSL_EXPORT int ED25519_sign(uint8_t out_sig[64], const uint8_t *message, size_t message_len, const uint8_t private_key[64]); -// ED25519_verify returns one iff |signature| is a valid signature, by -// |public_key| of |message_len| bytes from |message|. It returns zero +// ED25519_verify returns one iff `signature` is a valid signature, by +// `public_key` of `message_len` bytes from `message`. It returns zero // otherwise. OPENSSL_EXPORT int ED25519_verify(const uint8_t *message, size_t message_len, const uint8_t signature[64], const uint8_t public_key[32]); +// ED25519_sign_prehashed sets `out_sig` to be a signature of `sha512_digest` +// using `private_key` following the Ed25519ph algorithm. It returns one on +// success, or zero otherwise. `sha512_digest` must be the 64-byte result of +// hashing the input message with SHA-512. `context` allows for domain +// separation; see https://www.rfc-editor.org/info/rfc8032/#section-8.3. +// +// This function can return zero either on allocation failure, or if +// `context_len` is greater than 255. +// +// Note that Ed25519ph (pre-hashed) and "pure" Ed25519 are different algorithms, +// calling `ED25519_sign` with the digest of a message will *not* return the +// same output as `ED25519_sign_prehashed`. +OPENSSL_EXPORT int ED25519_sign_prehashed(uint8_t out_sig[64], + const uint8_t *context, + size_t context_len, + const uint8_t sha512_digest[64], + const uint8_t private_key[64]); + +// ED25519_verify_prehashed returns one if `signature` is a valid signature, by +// `public_key`, of `sha512_digest` following the Ed25519ph algorithm. It +// returns zero otherwise. `sha512_digest` must be the 64-byte result of hashing +// the input message with SHA-512. `context` allows for domain separation; see +// https://www.rfc-editor.org/info/rfc8032/#section-8.3. +// +// This function can return zero either if the signature is invalid, or if +// `context_len` is greater than 255. +// +// Note that Ed25519ph (pre-hashed) and "pure" Ed25519 are different algorithms, +// calling `ED25519_verify` with the digest of a message will *not* return the +// same output as `ED25519_verify_prehashed`. +OPENSSL_EXPORT int ED25519_verify_prehashed(const uint8_t *context, + size_t context_len, + const uint8_t sha512_digest[64], + const uint8_t signature[64], + const uint8_t public_key[32]); + // ED25519_keypair_from_seed calculates a public and private key from an // Ed25519 “seed”. Seed values are not exposed by this API (although they // happen to be the first 32 bytes of a private key) so this function is for @@ -118,33 +155,34 @@ enum spake2_role_t { spake2_role_bob, }; -// SPAKE2_CTX_new creates a new |SPAKE2_CTX| (which can only be used for a +// SPAKE2_CTX_new creates a new `SPAKE2_CTX` (which can only be used for a // single execution of the protocol). SPAKE2 requires the symmetry of the two -// parties to be broken which is indicated via |my_role| – each party must pass +// parties to be broken which is indicated via `my_role` – each party must pass // a different value for this argument. // -// The |my_name| and |their_name| arguments allow optional, opaque names to be +// The `my_name` and `their_name` arguments allow optional, opaque names to be // bound into the protocol. For example MAC addresses, hostnames, usernames // etc. These values are not exposed and can avoid context-confusion attacks // when a password is shared between several devices. -OPENSSL_EXPORT SPAKE2_CTX *SPAKE2_CTX_new( - enum spake2_role_t my_role, - const uint8_t *my_name, size_t my_name_len, - const uint8_t *their_name, size_t their_name_len); +OPENSSL_EXPORT SPAKE2_CTX *SPAKE2_CTX_new(enum spake2_role_t my_role, + const uint8_t *my_name, + size_t my_name_len, + const uint8_t *their_name, + size_t their_name_len); -// SPAKE2_CTX_free frees |ctx| and all the resources that it has allocated. +// SPAKE2_CTX_free frees `ctx` and all the resources that it has allocated. OPENSSL_EXPORT void SPAKE2_CTX_free(SPAKE2_CTX *ctx); // SPAKE2_MAX_MSG_SIZE is the maximum size of a SPAKE2 message. #define SPAKE2_MAX_MSG_SIZE 32 -// SPAKE2_generate_msg generates a SPAKE2 message given |password|, writes -// it to |out| and sets |*out_len| to the number of bytes written. +// SPAKE2_generate_msg generates a SPAKE2 message given `password`, writes +// it to `out` and sets `*out_len` to the number of bytes written. // -// At most |max_out_len| bytes are written to |out| and, in order to ensure -// success, |max_out_len| should be at least |SPAKE2_MAX_MSG_SIZE| bytes. +// At most `max_out_len` bytes are written to `out` and, in order to ensure +// success, `max_out_len` should be at least `SPAKE2_MAX_MSG_SIZE` bytes. // -// This function can only be called once for a given |SPAKE2_CTX|. +// This function can only be called once for a given `SPAKE2_CTX`. // // It returns one on success and zero on error. OPENSSL_EXPORT int SPAKE2_generate_msg(SPAKE2_CTX *ctx, uint8_t *out, @@ -157,8 +195,8 @@ OPENSSL_EXPORT int SPAKE2_generate_msg(SPAKE2_CTX *ctx, uint8_t *out, #define SPAKE2_MAX_KEY_SIZE 64 // SPAKE2_process_msg completes the SPAKE2 exchange given the peer's message in -// |their_msg|, writes at most |max_out_key_len| bytes to |out_key| and sets -// |*out_key_len| to the number of bytes written. +// `their_msg`, writes at most `max_out_key_len` bytes to `out_key` and sets +// `*out_key_len` to the number of bytes written. // // The resulting keying material is suitable for: // - Using directly in a key-confirmation step: i.e. each side could @@ -167,13 +205,13 @@ OPENSSL_EXPORT int SPAKE2_generate_msg(SPAKE2_CTX *ctx, uint8_t *out, // - Using as input keying material to HKDF to generate a variety of subkeys // for encryption etc. // -// If |max_out_key_key| is smaller than the amount of key material generated +// If `max_out_key_key` is smaller than the amount of key material generated // then the key is silently truncated. If you want to ensure that no truncation -// occurs then |max_out_key| should be at least |SPAKE2_MAX_KEY_SIZE|. +// occurs then `max_out_key` should be at least `SPAKE2_MAX_KEY_SIZE`. // -// You must call |SPAKE2_generate_msg| on a given |SPAKE2_CTX| before calling -// this function. On successful return, |ctx| is complete and calling -// |SPAKE2_CTX_free| is the only acceptable operation on it. +// You must call `SPAKE2_generate_msg` on a given `SPAKE2_CTX` before calling +// this function. On successful return, `ctx` is complete and calling +// `SPAKE2_CTX_free` is the only acceptable operation on it. // // Returns one on success or zero on error. OPENSSL_EXPORT int SPAKE2_process_msg(SPAKE2_CTX *ctx, uint8_t *out_key, @@ -183,6 +221,75 @@ OPENSSL_EXPORT int SPAKE2_process_msg(SPAKE2_CTX *ctx, uint8_t *out_key, size_t their_msg_len); +// CPace. +// +// See https://datatracker.ietf.org/doc/draft-irtf-cfrg-cpace/ + +// The role that a party plays in the CPACE key agreement protocol. +enum cpace_role_t { + // This role corresponds to the party A in the specification. + cpace_role_initiator, + // This role corresponds to the party B in the specification. + cpace_role_responder, +}; + +// CPACE_CTX_new returns a newly-allocated `CPACE_CTX` object that implements +// one side of the CPace protocol, or nullptr on error. This function implements +// CPACE-X25519-SHA512 in initiator-responder mode, acting as `role` and with +// `password` as the password-related string (PRS). +// +// `channel_id` and `session_id` are the channel identifier (CI) and session +// identifier (sid) values, respectively. Both values must match for the +// exchange to proceed. `assoc_data` is the cleartext associated data (ADa or +// ADb) to be sent to the peer. These values may be empty if not used. See +// Section 4.1 of draft-irtf-cfrg-cpace-21 for details. +// +// The application-level identities of each party in the protocol should be +// incorporated into either `channel_id` or each party's respective `assoc_data` +// values. See Section 10.1 of draft-irtf-cfrg-cpace-21. +// +// Once a `CPACE_CTX` is successfully created, the caller should call +// `CPACE_generate_msg` to generate the message to send to the peer. +OPENSSL_EXPORT CPACE_CTX *CPACE_CTX_new( + enum cpace_role_t role, const uint8_t *password, size_t password_len, + const uint8_t *channel_id, size_t channel_id_len, const uint8_t *assoc_data, + size_t assoc_data_len, const uint8_t *session_id, size_t session_id_len); + +// CPACE_CTX_free frees `ctx` and all the resources that it has allocated. +OPENSSL_EXPORT void CPACE_CTX_free(CPACE_CTX *ctx); + +#define CPACE_MSG_LEN 32 +#define CPACE_SHARED_SECRET_LEN 64 + +// CPACE_generate_msg generates the message to be sent to the peer. On success, +// it writes the message to `out_msg` and returns one. Otherwise, it returns +// zero. `ctx` must have been allocated by `CPACE_CTX_new`, with no subsequent +// functions called on it. +// +// `out_msg` only contains the Ya or Yb value. It does not include ADa or ADb. +// On success, the caller should separately send `out_msg` and their +// `assoc_data` value to the peer. It is the caller's responsibility to send +// `assoc_data` together with `out_msg`. After receiving the peer's reply, the +// caller should call `CPACE_process_msg` to complete the protocol. +OPENSSL_EXPORT int CPACE_generate_msg(CPACE_CTX *ctx, + uint8_t out_msg[CPACE_MSG_LEN]); + +// CPACE_process_msg processes `msg` and `peer_assoc_data` from the peer and +// computes the shared secret, or intermediate session key (ISK). On success, it +// writes the secret to `out_shared_secret` and returns one; it also writes a +// derived session ID if `out_sid` is not NULL. Otherwise, it returns zero. +// `ctx` must have successfully been passed to `CPACE_generate_msg`, with no +// subsequent functions called on it. +// +// `msg` should contain the peer's Ya or Yb value, and `peer_assoc_data` the +// peer's ADa or ADb value. +OPENSSL_EXPORT int CPACE_process_msg( + CPACE_CTX *ctx, const uint8_t in_msg[CPACE_MSG_LEN], + const uint8_t *peer_assoc_data, size_t peer_assoc_data_len, + uint8_t shared_secret[CPACE_SHARED_SECRET_LEN], + uint8_t sid_out[SHA512_DIGEST_LENGTH]); + + #if defined(__cplusplus) } // extern C @@ -190,6 +297,7 @@ extern "C++" { BSSL_NAMESPACE_BEGIN +BORINGSSL_MAKE_DELETER(CPACE_CTX, CPACE_CTX_free) BORINGSSL_MAKE_DELETER(SPAKE2_CTX, SPAKE2_CTX_free) BSSL_NAMESPACE_END diff --git a/third_party/boringssl/src/include/openssl/des.h b/third_party/boringssl/src/include/openssl/des.h index c60139106..ce4571ff7 100644 --- a/third_party/boringssl/src/include/openssl/des.h +++ b/third_party/boringssl/src/include/openssl/des.h @@ -49,32 +49,32 @@ typedef struct DES_ks { #define DES_CBC_MODE 0 #define DES_PCBC_MODE 1 -// DES_set_key performs a key schedule and initialises |schedule| with |key|. +// DES_set_key performs a key schedule and initialises `schedule` with `key`. OPENSSL_EXPORT void DES_set_key(const DES_cblock *key, DES_key_schedule *schedule); // DES_set_odd_parity sets the parity bits (the least-significant bits in each -// byte) of |key| given the other bits in each byte. +// byte) of `key` given the other bits in each byte. OPENSSL_EXPORT void DES_set_odd_parity(DES_cblock *key); -// DES_ecb_encrypt encrypts (or decrypts, if |is_encrypt| is |DES_DECRYPT|) a -// single DES block (8 bytes) from |in| to |out|, using the key configured in -// |schedule|. +// DES_ecb_encrypt encrypts (or decrypts, if `is_encrypt` is `DES_DECRYPT`) a +// single DES block (8 bytes) from `in` to `out`, using the key configured in +// `schedule`. OPENSSL_EXPORT void DES_ecb_encrypt(const DES_cblock *in, DES_cblock *out, const DES_key_schedule *schedule, int is_encrypt); -// DES_ncbc_encrypt encrypts (or decrypts, if |enc| is |DES_DECRYPT|) |len| -// bytes from |in| to |out| with DES in CBC mode. |len| must be a multiple of 8. -// The IV is taken from |ivec|. When the function completes, the IV for the next -// block is written to |ivec|. +// DES_ncbc_encrypt encrypts (or decrypts, if `enc` is `DES_DECRYPT`) `len` +// bytes from `in` to `out` with DES in CBC mode. `len` must be a multiple of 8. +// The IV is taken from `ivec`. When the function completes, the IV for the next +// block is written to `ivec`. OPENSSL_EXPORT void DES_ncbc_encrypt(const uint8_t *in, uint8_t *out, size_t len, const DES_key_schedule *schedule, DES_cblock *ivec, int enc); -// DES_ecb3_encrypt encrypts (or decrypts, if |enc| is |DES_DECRYPT|) a single -// block (8 bytes) of data from |input| to |output| using 3DES. +// DES_ecb3_encrypt encrypts (or decrypts, if `enc` is `DES_DECRYPT`) a single +// block (8 bytes) of data from `input` to `output` using 3DES. OPENSSL_EXPORT void DES_ecb3_encrypt(const DES_cblock *input, DES_cblock *output, const DES_key_schedule *ks1, @@ -82,11 +82,11 @@ OPENSSL_EXPORT void DES_ecb3_encrypt(const DES_cblock *input, const DES_key_schedule *ks3, int enc); -// DES_ede3_cbc_encrypt encrypts (or decrypts, if |enc| is |DES_DECRYPT|) |len| -// bytes from |in| to |out| with 3DES in CBC mode. 3DES uses three keys, thus -// the function takes three different |DES_key_schedule|s. |len| must be a -// multiple of 8. The IV is taken from |ivec|. When the function completes, the -// IV for the next block is written to |ivec|. +// DES_ede3_cbc_encrypt encrypts (or decrypts, if `enc` is `DES_DECRYPT`) `len` +// bytes from `in` to `out` with 3DES in CBC mode. 3DES uses three keys, thus +// the function takes three different `DES_key_schedule`s. `len` must be a +// multiple of 8. The IV is taken from `ivec`. When the function completes, the +// IV for the next block is written to `ivec`. OPENSSL_EXPORT void DES_ede3_cbc_encrypt(const uint8_t *in, uint8_t *out, size_t len, const DES_key_schedule *ks1, @@ -94,12 +94,12 @@ OPENSSL_EXPORT void DES_ede3_cbc_encrypt(const uint8_t *in, uint8_t *out, const DES_key_schedule *ks3, DES_cblock *ivec, int enc); -// DES_ede2_cbc_encrypt encrypts (or decrypts, if |enc| is |DES_DECRYPT|) |len| -// bytes from |in| to |out| with 3DES in CBC mode. With this keying option, the +// DES_ede2_cbc_encrypt encrypts (or decrypts, if `enc` is `DES_DECRYPT`) `len` +// bytes from `in` to `out` with 3DES in CBC mode. With this keying option, the // first and third 3DES keys are identical. Thus, this function takes only two -// different |DES_key_schedule|s. |len| must be a multiple of 8. The IV is taken -// from |ivec|. When the function completes, the IV for the next block is -// written to |ivec|. +// different `DES_key_schedule`s. `len` must be a multiple of 8. The IV is taken +// from `ivec`. When the function completes, the IV for the next block is +// written to `ivec`. OPENSSL_EXPORT void DES_ede2_cbc_encrypt(const uint8_t *in, uint8_t *out, size_t len, const DES_key_schedule *ks1, @@ -109,7 +109,7 @@ OPENSSL_EXPORT void DES_ede2_cbc_encrypt(const uint8_t *in, uint8_t *out, // Deprecated functions. -// DES_set_key_unchecked calls |DES_set_key|. +// DES_set_key_unchecked calls `DES_set_key`. OPENSSL_EXPORT void DES_set_key_unchecked(const DES_cblock *key, DES_key_schedule *schedule); diff --git a/third_party/boringssl/src/include/openssl/dh.h b/third_party/boringssl/src/include/openssl/dh.h index acba59f3c..6cb5d9e36 100644 --- a/third_party/boringssl/src/include/openssl/dh.h +++ b/third_party/boringssl/src/include/openssl/dh.h @@ -15,7 +15,7 @@ #ifndef OPENSSL_HEADER_DH_H #define OPENSSL_HEADER_DH_H -#include // IWYU pragma: export +#include // IWYU pragma: export #if defined(__cplusplus) extern "C" { @@ -32,23 +32,28 @@ extern "C" { // Allocation and destruction. // -// A |DH| object represents a Diffie-Hellman key or group parameters. A given +// A `DH` object represents a Diffie-Hellman key or group parameters. A given // object may be used concurrently on multiple threads by non-mutating // functions, provided no other thread is concurrently calling a mutating -// function. Unless otherwise documented, functions which take a |const| pointer -// are non-mutating and functions which take a non-|const| pointer are mutating. +// function. Unless otherwise documented, functions which take a `const` pointer +// are non-mutating and functions which take a non-`const` pointer are mutating. // DH_new returns a new, empty DH object or NULL on error. OPENSSL_EXPORT DH *DH_new(void); -// DH_free decrements the reference count of |dh| and frees it if the reference +// DH_free decrements the reference count of `dh` and frees it if the reference // count drops to zero. OPENSSL_EXPORT void DH_free(DH *dh); -// DH_up_ref increments the reference count of |dh| and returns one. It does not -// mutate |dh| for thread-safety purposes and may be used concurrently. +// DH_up_ref increments the reference count of `dh` and returns one. It does not +// mutate `dh` for thread-safety purposes and may be used concurrently. OPENSSL_EXPORT int DH_up_ref(DH *dh); +// DH_dup_ref increments the reference count of `dh` and returns the same +// handle. It does not mutate `dh` for thread-safety purposes and may be used +// concurrently. +OPENSSL_EXPORT DH *DH_dup_ref(const DH *dh); + // Properties. @@ -56,52 +61,52 @@ OPENSSL_EXPORT int DH_up_ref(DH *dh); // modulus, in bits. #define OPENSSL_DH_MAX_MODULUS_BITS 8192 -// DH_bits returns the size of |dh|'s group modulus, in bits. +// DH_bits returns the size of `dh`'s group modulus, in bits. OPENSSL_EXPORT unsigned DH_bits(const DH *dh); // DH_size returns the number of bytes in the DH group's prime. OPENSSL_EXPORT int DH_size(const DH *dh); -// DH_get0_pub_key returns |dh|'s public key. +// DH_get0_pub_key returns `dh`'s public key. OPENSSL_EXPORT const BIGNUM *DH_get0_pub_key(const DH *dh); -// DH_get0_priv_key returns |dh|'s private key, or NULL if |dh| is a public key. +// DH_get0_priv_key returns `dh`'s private key, or NULL if `dh` is a public key. OPENSSL_EXPORT const BIGNUM *DH_get0_priv_key(const DH *dh); -// DH_get0_p returns |dh|'s group modulus. +// DH_get0_p returns `dh`'s group modulus. OPENSSL_EXPORT const BIGNUM *DH_get0_p(const DH *dh); -// DH_get0_q returns the size of |dh|'s subgroup, or NULL if it is unset. +// DH_get0_q returns the size of `dh`'s subgroup, or NULL if it is unset. OPENSSL_EXPORT const BIGNUM *DH_get0_q(const DH *dh); -// DH_get0_g returns |dh|'s group generator. +// DH_get0_g returns `dh`'s group generator. OPENSSL_EXPORT const BIGNUM *DH_get0_g(const DH *dh); -// DH_get0_key sets |*out_pub_key| and |*out_priv_key|, if non-NULL, to |dh|'s -// public and private key, respectively. If |dh| is a public key, the private +// DH_get0_key sets `*out_pub_key` and `*out_priv_key`, if non-NULL, to `dh`'s +// public and private key, respectively. If `dh` is a public key, the private // key will be set to NULL. OPENSSL_EXPORT void DH_get0_key(const DH *dh, const BIGNUM **out_pub_key, const BIGNUM **out_priv_key); -// DH_set0_key sets |dh|'s public and private key to the specified values. If +// DH_set0_key sets `dh`'s public and private key to the specified values. If // NULL, the field is left unchanged. On success, it takes ownership of each // argument and returns one. Otherwise, it returns zero. OPENSSL_EXPORT int DH_set0_key(DH *dh, BIGNUM *pub_key, BIGNUM *priv_key); -// DH_get0_pqg sets |*out_p|, |*out_q|, and |*out_g|, if non-NULL, to |dh|'s p, +// DH_get0_pqg sets `*out_p`, `*out_q`, and `*out_g`, if non-NULL, to `dh`'s p, // q, and g parameters, respectively. OPENSSL_EXPORT void DH_get0_pqg(const DH *dh, const BIGNUM **out_p, const BIGNUM **out_q, const BIGNUM **out_g); -// DH_set0_pqg sets |dh|'s p, q, and g parameters to the specified values. If +// DH_set0_pqg sets `dh`'s p, q, and g parameters to the specified values. If // NULL, the field is left unchanged. On success, it takes ownership of each -// argument and returns one. Otherwise, it returns zero. |q| may be NULL, but -// |p| and |g| must either be specified or already configured on |dh|. +// argument and returns one. Otherwise, it returns zero. `q` may be NULL, but +// `p` and `g` must either be specified or already configured on `dh`. OPENSSL_EXPORT int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g); // DH_set_length sets the number of bits to use for the secret exponent when -// calling |DH_generate_key| on |dh| and returns one. If unset, -// |DH_generate_key| will use the bit length of p. +// calling `DH_generate_key` on `dh` and returns one. If unset, +// `DH_generate_key` will use the bit length of p. OPENSSL_EXPORT int DH_set_length(DH *dh, unsigned priv_length); @@ -112,38 +117,38 @@ OPENSSL_EXPORT int DH_set_length(DH *dh, unsigned priv_length); // of memory. OPENSSL_EXPORT DH *DH_get_rfc7919_2048(void); -// BN_get_rfc3526_prime_1536 sets |*ret| to the 1536-bit MODP group from RFC -// 3526 and returns |ret|. If |ret| is NULL then a fresh |BIGNUM| is allocated +// BN_get_rfc3526_prime_1536 sets `*ret` to the 1536-bit MODP group from RFC +// 3526 and returns `ret`. If `ret` is NULL then a fresh `BIGNUM` is allocated // and returned. It returns NULL on allocation failure. The generator for this // group is 2. OPENSSL_EXPORT BIGNUM *BN_get_rfc3526_prime_1536(BIGNUM *ret); -// BN_get_rfc3526_prime_2048 sets |*ret| to the 2048-bit MODP group from RFC -// 3526 and returns |ret|. If |ret| is NULL then a fresh |BIGNUM| is allocated +// BN_get_rfc3526_prime_2048 sets `*ret` to the 2048-bit MODP group from RFC +// 3526 and returns `ret`. If `ret` is NULL then a fresh `BIGNUM` is allocated // and returned. It returns NULL on allocation failure. The generator for this // group is 2. OPENSSL_EXPORT BIGNUM *BN_get_rfc3526_prime_2048(BIGNUM *ret); -// BN_get_rfc3526_prime_3072 sets |*ret| to the 3072-bit MODP group from RFC -// 3526 and returns |ret|. If |ret| is NULL then a fresh |BIGNUM| is allocated +// BN_get_rfc3526_prime_3072 sets `*ret` to the 3072-bit MODP group from RFC +// 3526 and returns `ret`. If `ret` is NULL then a fresh `BIGNUM` is allocated // and returned. It returns NULL on allocation failure. The generator for this // group is 2. OPENSSL_EXPORT BIGNUM *BN_get_rfc3526_prime_3072(BIGNUM *ret); -// BN_get_rfc3526_prime_4096 sets |*ret| to the 4096-bit MODP group from RFC -// 3526 and returns |ret|. If |ret| is NULL then a fresh |BIGNUM| is allocated +// BN_get_rfc3526_prime_4096 sets `*ret` to the 4096-bit MODP group from RFC +// 3526 and returns `ret`. If `ret` is NULL then a fresh `BIGNUM` is allocated // and returned. It returns NULL on allocation failure. The generator for this // group is 2. OPENSSL_EXPORT BIGNUM *BN_get_rfc3526_prime_4096(BIGNUM *ret); -// BN_get_rfc3526_prime_6144 sets |*ret| to the 6144-bit MODP group from RFC -// 3526 and returns |ret|. If |ret| is NULL then a fresh |BIGNUM| is allocated +// BN_get_rfc3526_prime_6144 sets `*ret` to the 6144-bit MODP group from RFC +// 3526 and returns `ret`. If `ret` is NULL then a fresh `BIGNUM` is allocated // and returned. It returns NULL on allocation failure. The generator for this // group is 2. OPENSSL_EXPORT BIGNUM *BN_get_rfc3526_prime_6144(BIGNUM *ret); -// BN_get_rfc3526_prime_8192 sets |*ret| to the 8192-bit MODP group from RFC -// 3526 and returns |ret|. If |ret| is NULL then a fresh |BIGNUM| is allocated +// BN_get_rfc3526_prime_8192 sets `*ret` to the 8192-bit MODP group from RFC +// 3526 and returns `ret`. If `ret` is NULL then a fresh `BIGNUM` is allocated // and returned. It returns NULL on allocation failure. The generator for this // group is 2. OPENSSL_EXPORT BIGNUM *BN_get_rfc3526_prime_8192(BIGNUM *ret); @@ -155,12 +160,12 @@ OPENSSL_EXPORT BIGNUM *BN_get_rfc3526_prime_8192(BIGNUM *ret); #define DH_GENERATOR_5 5 // DH_generate_parameters_ex generates a suitable Diffie-Hellman group with a -// prime that is |prime_bits| long and stores it in |dh|. The generator of the -// group will be |generator|, which should be |DH_GENERATOR_2| unless there's a -// good reason to use a different value. The |cb| argument contains a callback +// prime that is `prime_bits` long and stores it in `dh`. The generator of the +// group will be `generator`, which should be `DH_GENERATOR_2` unless there's a +// good reason to use a different value. The `cb` argument contains a callback // function that will be called during the generation. See the documentation in -// |bn.h| about this. In addition to the callback invocations from |BN|, |cb| -// will also be called with |event| equal to three when the generation is +// `bn.h` about this. In addition to the callback invocations from `BN`, `cb` +// will also be called with `event` equal to three when the generation is // complete. OPENSSL_EXPORT int DH_generate_parameters_ex(DH *dh, int prime_bits, int generator, BN_GENCB *cb); @@ -169,43 +174,43 @@ OPENSSL_EXPORT int DH_generate_parameters_ex(DH *dh, int prime_bits, // Diffie-Hellman operations. // DH_generate_key generates a new, random, private key and stores it in -// |dh|, if |dh| does not already have a private key. Otherwise, it updates -// |dh|'s public key to match the private key. It returns one on success and +// `dh`, if `dh` does not already have a private key. Otherwise, it updates +// `dh`'s public key to match the private key. It returns one on success and // zero on error. OPENSSL_EXPORT int DH_generate_key(DH *dh); -// DH_compute_key_padded calculates the shared key between |dh| and |peers_key| -// and writes it as a big-endian integer into |out|, padded up to |DH_size| -// bytes. It returns the number of bytes written, which is always |DH_size|, or -// a negative number on error. |out| must have |DH_size| bytes of space. +// DH_compute_key_padded calculates the shared key between `dh` and `peers_key` +// and writes it as a big-endian integer into `out`, padded up to `DH_size` +// bytes. It returns the number of bytes written, which is always `DH_size`, or +// a negative number on error. `out` must have `DH_size` bytes of space. // // WARNING: this differs from the usual BoringSSL return-value convention. // -// Note this function differs from |DH_compute_key| in that it preserves leading +// Note this function differs from `DH_compute_key` in that it preserves leading // zeros in the secret. This function is the preferred variant. It matches PKCS // #3 and avoids some side channel attacks. However, the two functions are not // drop-in replacements for each other. Using a different variant than the // application expects will result in sporadic key mismatches. // // Callers that expect a fixed-width secret should use this function over -// |DH_compute_key|. Callers that use either function should migrate to a modern +// `DH_compute_key`. Callers that use either function should migrate to a modern // primitive such as X25519 or ECDH with P-256 instead. // -// This function does not mutate |dh| for thread-safety purposes and may be used +// This function does not mutate `dh` for thread-safety purposes and may be used // concurrently. OPENSSL_EXPORT int DH_compute_key_padded(uint8_t *out, const BIGNUM *peers_key, DH *dh); -// DH_compute_key_hashed calculates the shared key between |dh| and |peers_key| -// and hashes it with the given |digest|. If the hash output is less than -// |max_out_len| bytes then it writes the hash output to |out| and sets -// |*out_len| to the number of bytes written. Otherwise it signals an error. It +// DH_compute_key_hashed calculates the shared key between `dh` and `peers_key` +// and hashes it with the given `digest`. If the hash output is less than +// `max_out_len` bytes then it writes the hash output to `out` and sets +// `*out_len` to the number of bytes written. Otherwise it signals an error. It // returns one on success or zero on error. // // NOTE: this follows the usual BoringSSL return-value convention, but that's -// different from |DH_compute_key| and |DH_compute_key_padded|. +// different from `DH_compute_key` and `DH_compute_key_padded`. // -// This function does not mutate |dh| for thread-safety purposes and may be used +// This function does not mutate `dh` for thread-safety purposes and may be used // concurrently. OPENSSL_EXPORT int DH_compute_key_hashed(DH *dh, uint8_t *out, size_t *out_len, size_t max_out_len, @@ -226,9 +231,9 @@ OPENSSL_EXPORT int DH_compute_key_hashed(DH *dh, uint8_t *out, size_t *out_len, #define DH_NOT_SUITABLE_GENERATOR DH_CHECK_NOT_SUITABLE_GENERATOR #define DH_UNABLE_TO_CHECK_GENERATOR DH_CHECK_UNABLE_TO_CHECK_GENERATOR -// DH_check checks the suitability of |dh| as a Diffie-Hellman group. and sets -// |DH_CHECK_*| flags in |*out_flags| if it finds any errors. It returns one if -// |*out_flags| was successfully set and zero on error. +// DH_check checks the suitability of `dh` as a Diffie-Hellman group. and sets +// `DH_CHECK_*` flags in `*out_flags` if it finds any errors. It returns one if +// `*out_flags` was successfully set and zero on error. // // Note: these checks may be quite computationally expensive. OPENSSL_EXPORT int DH_check(const DH *dh, int *out_flags); @@ -237,73 +242,73 @@ OPENSSL_EXPORT int DH_check(const DH *dh, int *out_flags); #define DH_CHECK_PUBKEY_TOO_LARGE 0x2 #define DH_CHECK_PUBKEY_INVALID 0x4 -// DH_check_pub_key checks the suitability of |pub_key| as a public key for the -// DH group in |dh| and sets |DH_CHECK_PUBKEY_*| flags in |*out_flags| if it -// finds any errors. It returns one if |*out_flags| was successfully set and +// DH_check_pub_key checks the suitability of `pub_key` as a public key for the +// DH group in `dh` and sets `DH_CHECK_PUBKEY_*` flags in `*out_flags` if it +// finds any errors. It returns one if `*out_flags` was successfully set and // zero on error. OPENSSL_EXPORT int DH_check_pub_key(const DH *dh, const BIGNUM *pub_key, int *out_flags); -// DHparams_dup allocates a fresh |DH| and copies the parameters from |dh| into -// it. It returns the new |DH| or NULL on error. +// DHparams_dup allocates a fresh `DH` and copies the parameters from `dh` into +// it. It returns the new `DH` or NULL on error. OPENSSL_EXPORT DH *DHparams_dup(const DH *dh); // ASN.1 functions. // DH_parse_parameters decodes a DER-encoded DHParameter structure (PKCS #3) -// from |cbs| and advances |cbs|. It returns a newly-allocated |DH| or NULL on +// from `cbs` and advances `cbs`. It returns a newly-allocated `DH` or NULL on // error. OPENSSL_EXPORT DH *DH_parse_parameters(CBS *cbs); -// DH_marshal_parameters marshals |dh| as a DER-encoded DHParameter structure -// (PKCS #3) and appends the result to |cbb|. It returns one on success and zero +// DH_marshal_parameters marshals `dh` as a DER-encoded DHParameter structure +// (PKCS #3) and appends the result to `cbb`. It returns one on success and zero // on error. OPENSSL_EXPORT int DH_marshal_parameters(CBB *cbb, const DH *dh); // Deprecated functions. -// DH_generate_parameters behaves like |DH_generate_parameters_ex|, which is +// DH_generate_parameters behaves like `DH_generate_parameters_ex`, which is // what you should use instead. It returns NULL on error, or a newly-allocated -// |DH| on success. This function is provided for compatibility only. +// `DH` on success. This function is provided for compatibility only. OPENSSL_EXPORT DH *DH_generate_parameters(int prime_len, int generator, void (*callback)(int, int, void *), void *cb_arg); -// d2i_DHparams parses a DER-encoded DHParameter structure (PKCS #3) from |len| -// bytes at |*inp|, as in |d2i_SAMPLE|. +// d2i_DHparams parses a DER-encoded DHParameter structure (PKCS #3) from `len` +// bytes at `*inp`, as in `d2i_SAMPLE`. // -// Use |DH_parse_parameters| instead. +// Use `DH_parse_parameters` instead. OPENSSL_EXPORT DH *d2i_DHparams(DH **ret, const unsigned char **inp, long len); -// i2d_DHparams marshals |in| to a DER-encoded DHParameter structure (PKCS #3), -// as described in |i2d_SAMPLE|. +// i2d_DHparams marshals `in` to a DER-encoded DHParameter structure (PKCS #3), +// as described in `i2d_SAMPLE`. // -// Use |DH_marshal_parameters| instead. +// Use `DH_marshal_parameters` instead. OPENSSL_EXPORT int i2d_DHparams(const DH *in, unsigned char **outp); -// DH_compute_key behaves like |DH_compute_key_padded| but, contrary to PKCS #3, +// DH_compute_key behaves like `DH_compute_key_padded` but, contrary to PKCS #3, // returns a variable-length shared key with leading zeros. It returns the -// number of bytes written, or a negative number on error. |out| must have -// |DH_size| bytes of space. +// number of bytes written, or a negative number on error. `out` must have +// `DH_size` bytes of space. // // WARNING: this differs from the usual BoringSSL return-value convention. // // Note this function's running time and memory access pattern leaks information -// about the shared secret. Particularly if |dh| is reused, this may result in +// about the shared secret. Particularly if `dh` is reused, this may result in // side channel attacks such as https://raccoon-attack.com/. // -// |DH_compute_key_padded| is the preferred variant and avoids the above +// `DH_compute_key_padded` is the preferred variant and avoids the above // attacks. However, the two functions are not drop-in replacements for each // other. Using a different variant than the application expects will result in // sporadic key mismatches. // -// Callers that expect a fixed-width secret should use |DH_compute_key_padded| +// Callers that expect a fixed-width secret should use `DH_compute_key_padded` // instead. Callers that use either function should migrate to a modern // primitive such as X25519 or ECDH with P-256 instead. // -// This function does not mutate |dh| for thread-safety purposes and may be used +// This function does not mutate `dh` for thread-safety purposes and may be used // concurrently. OPENSSL_EXPORT int DH_compute_key(uint8_t *out, const BIGNUM *peers_key, DH *dh); @@ -317,7 +322,7 @@ extern "C++" { BSSL_NAMESPACE_BEGIN BORINGSSL_MAKE_DELETER(DH, DH_free) -BORINGSSL_MAKE_UP_REF(DH, DH_up_ref) +BORINGSSL_MAKE_UP_REF(DH, DH_dup_ref) BSSL_NAMESPACE_END diff --git a/third_party/boringssl/src/include/openssl/digest.h b/third_party/boringssl/src/include/openssl/digest.h index 62ad57368..2ce07382d 100644 --- a/third_party/boringssl/src/include/openssl/digest.h +++ b/third_party/boringssl/src/include/openssl/digest.h @@ -31,7 +31,7 @@ extern "C" { // Hash algorithms. // -// The following functions return |EVP_MD| objects that implement the named hash +// The following functions return `EVP_MD` objects that implement the named hash // function. OPENSSL_EXPORT const EVP_MD *EVP_md4(void); @@ -44,15 +44,42 @@ OPENSSL_EXPORT const EVP_MD *EVP_sha512(void); OPENSSL_EXPORT const EVP_MD *EVP_sha512_256(void); OPENSSL_EXPORT const EVP_MD *EVP_blake2b256(void); -// EVP_md5_sha1 is a TLS-specific |EVP_MD| which computes the concatenation of +// EVP_md5_sha1 is a TLS-specific `EVP_MD` which computes the concatenation of // MD5 and SHA-1, as used in TLS 1.1 and below. OPENSSL_EXPORT const EVP_MD *EVP_md5_sha1(void); -// EVP_get_digestbynid returns an |EVP_MD| for the given NID, or NULL if no +// SHA-3 family of digest functions. +// +// WARNING: Prefer to use SHA-2 (e.g. `EVP_sha256`) over SHA-3. Despite being a +// newer hash function, SHA-3 performs poorly and should only be used when +// necessary for interoperability with existing systems. + +OPENSSL_EXPORT const EVP_MD *EVP_sha3_256(void); +OPENSSL_EXPORT const EVP_MD *EVP_sha3_384(void); +OPENSSL_EXPORT const EVP_MD *EVP_sha3_512(void); + +// SHAKE256 functions. +// +// These EVP_MD objects are extendable-output functions (XOFs), meaning they can +// return arbitrarily many bytes of output. See `EVP_DigestSqueeze`. + +// EVP_turboshake256 is TurboSHAKE extendable output function. +OPENSSL_EXPORT const EVP_MD *EVP_turboshake256(void); + +// EVP_shake256 is SHAKE256 extendable-output function. +// +// WARNING: SHAKE is slow. Prefer SHA-2, or TurboSHAKE (`EVP_turboshake256`) if +// an XOF is needed. +OPENSSL_EXPORT const EVP_MD *EVP_shake256(void); + + +// Digest lookup functions. + +// EVP_get_digestbynid returns an `EVP_MD` for the given NID, or NULL if no // such digest is known. OPENSSL_EXPORT const EVP_MD *EVP_get_digestbynid(int nid); -// EVP_get_digestbyobj returns an |EVP_MD| for the given |ASN1_OBJECT|, or NULL +// EVP_get_digestbyobj returns an `EVP_MD` for the given `ASN1_OBJECT`, or NULL // if no such digest is known. OPENSSL_EXPORT const EVP_MD *EVP_get_digestbyobj(const ASN1_OBJECT *obj); @@ -62,56 +89,61 @@ OPENSSL_EXPORT const EVP_MD *EVP_get_digestbyobj(const ASN1_OBJECT *obj); // An EVP_MD_CTX represents the state of a specific digest operation in // progress. -// EVP_MD_CTX_init initialises an, already allocated, |EVP_MD_CTX|. This is the -// same as setting the structure to zero. +// EVP_MD_CTX_init sets an uninitialized `EVP_MD_CTX` to the zero state. This +// is the same as setting the structure to zero. +// +// This function is used for initializing uninitialized memory in an +// `EVP_MD_CTX`, e.g. if it is declared as a local variable on the stack. This +// function should not be used on objects that have already been initialized. OPENSSL_EXPORT void EVP_MD_CTX_init(EVP_MD_CTX *ctx); -// EVP_MD_CTX_new allocates and initialises a fresh |EVP_MD_CTX| and returns -// it, or NULL on allocation failure. The caller must use |EVP_MD_CTX_free| to -// release the resulting object. +// EVP_MD_CTX_new returns a newly-allocated `EVP_MD_CTX` in the zero state, or +// NULL on allocation failure. The caller must use `EVP_MD_CTX_free` to release +// the resulting object. OPENSSL_EXPORT EVP_MD_CTX *EVP_MD_CTX_new(void); -// EVP_MD_CTX_cleanup frees any resources owned by |ctx| and resets it to a -// freshly initialised state. It does not free |ctx| itself. It returns one. +// EVP_MD_CTX_cleanup frees any resources owned by `ctx` and resets it to the +// zero state. It does not free `ctx` itself. It returns one. OPENSSL_EXPORT int EVP_MD_CTX_cleanup(EVP_MD_CTX *ctx); -// EVP_MD_CTX_cleanse zeros the digest state in |ctx| and then performs the -// actions of |EVP_MD_CTX_cleanup|. Note that some |EVP_MD_CTX| objects contain -// more than just a digest (e.g. those resulting from |EVP_DigestSignInit|) but +// EVP_MD_CTX_cleanse zeros the digest state in `ctx` and then performs the +// actions of `EVP_MD_CTX_cleanup`. Note that some `EVP_MD_CTX` objects contain +// more than just a digest (e.g. those resulting from `EVP_DigestSignInit`) but // this function does not zero out more than just the digest state even in that // case. OPENSSL_EXPORT void EVP_MD_CTX_cleanse(EVP_MD_CTX *ctx); -// EVP_MD_CTX_free calls |EVP_MD_CTX_cleanup| and then frees |ctx| itself. +// EVP_MD_CTX_free calls `EVP_MD_CTX_cleanup` and then frees `ctx` itself. OPENSSL_EXPORT void EVP_MD_CTX_free(EVP_MD_CTX *ctx); -// EVP_MD_CTX_copy_ex sets |out|, which must already be initialised, to be a -// copy of |in|. It returns one on success and zero on allocation failure. +// EVP_MD_CTX_copy_ex sets `out` to be a copy of `in`. It returns one on success +// and zero on allocation failure. `out` must have been previously initialized, +// e.g. with `EVP_MD_CTX_init` or `EVP_MD_CTX_new`. OPENSSL_EXPORT int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in); -// EVP_MD_CTX_move sets |out|, which must already be initialised, to the hash -// state in |in|. |in| is mutated and left in an empty state. +// EVP_MD_CTX_move sets `out`, which must already be initialised, to the hash +// state in `in`. `in` is mutated and left in an empty state. OPENSSL_EXPORT void EVP_MD_CTX_move(EVP_MD_CTX *out, EVP_MD_CTX *in); -// EVP_MD_CTX_reset calls |EVP_MD_CTX_cleanup| followed by |EVP_MD_CTX_init|. It +// EVP_MD_CTX_reset calls `EVP_MD_CTX_cleanup` followed by `EVP_MD_CTX_init`. It // returns one. OPENSSL_EXPORT int EVP_MD_CTX_reset(EVP_MD_CTX *ctx); // Digest operations. -// EVP_DigestInit_ex configures |ctx|, which must already have been -// initialised, for a fresh hashing operation using |type|. It returns one on -// success and zero on allocation failure. +// EVP_DigestInit_ex configures `ctx`, whose memory must already have been +// initialised (e.g. with `EVP_MD_CTX_init` or `EVP_MD_CTX_new`), for a fresh +// hashing operation using `type`. It returns one on success and zero on +// allocation failure. +// +// This function may be used to reconfigure an `EVP_MD_CTX` that was previously +// used for another operation. OPENSSL_EXPORT int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *engine); -// EVP_DigestInit acts like |EVP_DigestInit_ex| except that |ctx| is -// initialised before use. -OPENSSL_EXPORT int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type); - -// EVP_DigestUpdate hashes |len| bytes from |data| into the hashing operation -// in |ctx|. It returns one. +// EVP_DigestUpdate hashes `len` bytes from `data` into the hashing operation +// in `ctx`. It returns one. OPENSSL_EXPORT int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *data, size_t len); @@ -122,26 +154,35 @@ OPENSSL_EXPORT int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *data, // EVP_MAX_MD_BLOCK_SIZE is the largest digest block size supported, in // bytes. -#define EVP_MAX_MD_BLOCK_SIZE 128 // SHA-512 is the longest so far. +#define EVP_MAX_MD_BLOCK_SIZE 136 // SHA3-256 is the longest so far. -// EVP_DigestFinal_ex finishes the digest in |ctx| and writes the output to -// |md_out|. |EVP_MD_CTX_size| bytes are written, which is at most -// |EVP_MAX_MD_SIZE|. If |out_size| is not NULL then |*out_size| is set to the +// EVP_DigestFinal_ex finishes the digest in `ctx` and writes the output to +// `md_out`. `EVP_MD_CTX_size` bytes are written, which is at most +// `EVP_MAX_MD_SIZE`. If `out_size` is not NULL then `*out_size` is set to the // number of bytes written. It returns one. After this call, the hash cannot be -// updated or finished again until |EVP_DigestInit_ex| is called to start +// updated or finished again until `EVP_DigestInit_ex` is called to start // another hashing operation. OPENSSL_EXPORT int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, uint8_t *md_out, unsigned int *out_size); -// EVP_DigestFinal acts like |EVP_DigestFinal_ex| except that -// |EVP_MD_CTX_cleanup| is called on |ctx| before returning. +// EVP_DigestFinal acts like `EVP_DigestFinal_ex` except that +// `EVP_MD_CTX_cleanup` is called on `ctx` before returning. OPENSSL_EXPORT int EVP_DigestFinal(EVP_MD_CTX *ctx, uint8_t *md_out, unsigned int *out_size); -// EVP_Digest performs a complete hashing operation in one call. It hashes |len| -// bytes from |data| and writes the digest to |md_out|. |EVP_MD_CTX_size| bytes -// are written, which is at most |EVP_MAX_MD_SIZE|. If |out_size| is not NULL -// then |*out_size| is set to the number of bytes written. It returns one on +// EVP_DigestSqueeze writes len bytes of output from `ctx` to `out` and returns +// one. This operation is only implemented if `ctx` was initialized with an +// extendable-output function, which can return arbitrarily long outputs. If +// not, this function returns zero. This function may be repeated to split its +// output over multiple calls. Doing so will give the same output as +// concatenating the calls into a single call. +OPENSSL_EXPORT int EVP_DigestSqueeze(EVP_MD_CTX *ctx, unsigned char *out, + size_t len); + +// EVP_Digest performs a complete hashing operation in one call. It hashes `len` +// bytes from `data` and writes the digest to `md_out`. `EVP_MD_CTX_size` bytes +// are written, which is at most `EVP_MAX_MD_SIZE`. If `out_size` is not NULL +// then `*out_size` is set to the number of bytes written. It returns one on // success and zero otherwise. OPENSSL_EXPORT int EVP_Digest(const void *data, size_t len, uint8_t *md_out, unsigned int *md_out_size, const EVP_MD *type, @@ -153,17 +194,17 @@ OPENSSL_EXPORT int EVP_Digest(const void *data, size_t len, uint8_t *md_out, // These functions allow code to learn details about an abstract hash // function. -// EVP_MD_type returns a NID identifying |md|. (For example, |NID_sha256|.) +// EVP_MD_type returns a NID identifying `md`. (For example, `NID_sha256`.) OPENSSL_EXPORT int EVP_MD_type(const EVP_MD *md); -// EVP_MD_flags returns the flags for |md|, which is a set of |EVP_MD_FLAG_*| +// EVP_MD_flags returns the flags for `md`, which is a set of `EVP_MD_FLAG_*` // values, ORed together. OPENSSL_EXPORT uint32_t EVP_MD_flags(const EVP_MD *md); -// EVP_MD_size returns the digest size of |md|, in bytes. +// EVP_MD_size returns the digest size of `md`, in bytes. OPENSSL_EXPORT size_t EVP_MD_size(const EVP_MD *md); -// EVP_MD_block_size returns the native block-size of |md|, in bytes. +// EVP_MD_block_size returns the native block-size of `md`, in bytes. OPENSSL_EXPORT size_t EVP_MD_block_size(const EVP_MD *md); // EVP_MD_FLAG_DIGALGID_ABSENT indicates that the parameter type in an X.509 @@ -173,7 +214,7 @@ OPENSSL_EXPORT size_t EVP_MD_block_size(const EVP_MD *md); // EVP_MD_FLAG_XOF indicates that the digest is an extensible-output function // (XOF). This flag is defined for compatibility and will never be set in any -// |EVP_MD| in BoringSSL. +// `EVP_MD` in BoringSSL. #define EVP_MD_FLAG_XOF 4 @@ -184,26 +225,26 @@ OPENSSL_EXPORT size_t EVP_MD_block_size(const EVP_MD *md); OPENSSL_EXPORT const EVP_MD *EVP_MD_CTX_get0_md(const EVP_MD_CTX *ctx); // EVP_MD_CTX_md returns the underlying digest function, or NULL if one has not -// been set. (This is the same as |EVP_MD_CTX_get0_md| but OpenSSL has +// been set. (This is the same as `EVP_MD_CTX_get0_md` but OpenSSL has // deprecated this spelling.) OPENSSL_EXPORT const EVP_MD *EVP_MD_CTX_md(const EVP_MD_CTX *ctx); -// EVP_MD_CTX_size returns the digest size of |ctx|, in bytes. It -// will crash if a digest hasn't been set on |ctx|. +// EVP_MD_CTX_size returns the digest size of `ctx`, in bytes. It +// will crash if a digest hasn't been set on `ctx`. OPENSSL_EXPORT size_t EVP_MD_CTX_size(const EVP_MD_CTX *ctx); // EVP_MD_CTX_block_size returns the block size of the digest function used by -// |ctx|, in bytes. It will crash if a digest hasn't been set on |ctx|. +// `ctx`, in bytes. It will crash if a digest hasn't been set on `ctx`. OPENSSL_EXPORT size_t EVP_MD_CTX_block_size(const EVP_MD_CTX *ctx); -// EVP_MD_CTX_type returns a NID describing the digest function used by |ctx|. -// (For example, |NID_sha256|.) It will crash if a digest hasn't been set on -// |ctx|. +// EVP_MD_CTX_type returns a NID describing the digest function used by `ctx`. +// (For example, `NID_sha256`.) It will crash if a digest hasn't been set on +// `ctx`. OPENSSL_EXPORT int EVP_MD_CTX_type(const EVP_MD_CTX *ctx); -// EVP_MD_CTX_pkey_ctx returns the |EVP_PKEY_CTX| used to configure additional -// parameters on |ctx| if |ctx| is used for a sign or verify operation with -// |EVP_DigestSignInit| or |EVP_DigestVerifyInit|. It returns NULL otherwise. +// EVP_MD_CTX_pkey_ctx returns the `EVP_PKEY_CTX` used to configure additional +// parameters on `ctx` if `ctx` is used for a sign or verify operation with +// `EVP_DigestSignInit` or `EVP_DigestVerifyInit`. It returns NULL otherwise. OPENSSL_EXPORT EVP_PKEY_CTX *EVP_MD_CTX_pkey_ctx(const EVP_MD_CTX *ctx); @@ -214,19 +255,19 @@ OPENSSL_EXPORT EVP_PKEY_CTX *EVP_MD_CTX_pkey_ctx(const EVP_MD_CTX *ctx); // EVP_parse_digest_algorithm parses an AlgorithmIdentifier structure containing // a hash function OID (for example, 2.16.840.1.101.3.4.2.1 is SHA-256) and -// advances |cbs|. The parameters field may either be omitted or a NULL. It +// advances `cbs`. The parameters field may either be omitted or a NULL. It // returns the digest function or NULL on error. OPENSSL_EXPORT const EVP_MD *EVP_parse_digest_algorithm(CBS *cbs); -// EVP_parse_digest_algorithm_nid behaves like |EVP_parse_digest_algorithm| -// except it returns |NID_undef| on error and some other value on success. This +// EVP_parse_digest_algorithm_nid behaves like `EVP_parse_digest_algorithm` +// except it returns `NID_undef` on error and some other value on success. This // may be used to avoid depending on every digest algorithm in the library. OPENSSL_EXPORT int EVP_parse_digest_algorithm_nid(CBS *cbs); -// EVP_marshal_digest_algorithm marshals |md| as an AlgorithmIdentifier -// structure and appends the result to |cbb|. It returns one on success and zero +// EVP_marshal_digest_algorithm marshals `md` as an AlgorithmIdentifier +// structure and appends the result to `cbb`. It returns one on success and zero // on error. It sets the parameters field to NULL. Use -// |EVP_marshal_digest_algorithm_no_params| to omit the parameters instead. +// `EVP_marshal_digest_algorithm_no_params` to omit the parameters instead. // // In general, the parameters should be omitted for digest algorithms, but the // following specifications require a NULL parameter instead. @@ -241,23 +282,47 @@ OPENSSL_EXPORT int EVP_parse_digest_algorithm_nid(CBS *cbs); OPENSSL_EXPORT int EVP_marshal_digest_algorithm(CBB *cbb, const EVP_MD *md); // EVP_marshal_digest_algorithm_no_params behaves like -// |EVP_marshal_digest_algorithm| but omits the parameters field. +// `EVP_marshal_digest_algorithm` but omits the parameters field. OPENSSL_EXPORT int EVP_marshal_digest_algorithm_no_params(CBB *cbb, const EVP_MD *md); // Deprecated functions. -// EVP_MD_CTX_copy sets |out|, which must /not/ be initialised, to be a copy of -// |in|. It returns one on success and zero on error. +// EVP_DigestInit calls `EVP_MD_CTX_init`, followed by `EVP_DigestInit_ex`. +// +// `EVP_MD_CTX_init` is used for initializing uninitialized memory in an +// `EVP_MD_CTX`, e.g. if it is declared as a local variable on the stack. Thus +// this function should not be used on objects that have already been +// initialized. +// +// WARNING: This differs from OpenSSL 1.1.x, where `EVP_DigestInit` and +// `EVP_DigestInit_ex` are largely equivalent. This difference is because +// BoringSSL, like OpenSSL 1.0.x, still supports stack-allocating `EVP_MD_CTX`. +// Implementing the OpenSSL 1.1.x semantics would introduce uninitialized reads +// in those callers. +OPENSSL_EXPORT int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type); + +// EVP_MD_CTX_copy calls `EVP_MD_CTX_init`, followed by `EVP_MD_CTX_copy_ex`. +// +// `EVP_MD_CTX_init` is used for initializing uninitialized memory in an +// `EVP_MD_CTX`, e.g. if it is declared as a local variable on the stack. Thus +// this function should not be used on objects that have already been +// initialized. +// +// WARNING: This differs from OpenSSL 1.1.x, where `EVP_MD_CTX_copy` and +// |EVP_MD_CTX_copy_ex| are largely equivalent. This difference is because +// BoringSSL, like OpenSSL 1.0.x, still supports stack-allocating `EVP_MD_CTX`. +// Implementing the OpenSSL 1.1.x semantics would introduce uninitialized reads +// in those callers. OPENSSL_EXPORT int EVP_MD_CTX_copy(EVP_MD_CTX *out, const EVP_MD_CTX *in); // EVP_add_digest does nothing and returns one. It exists only for // compatibility with OpenSSL. OPENSSL_EXPORT int EVP_add_digest(const EVP_MD *digest); -// EVP_get_digestbyname returns an |EVP_MD| given a human readable name in -// |name|, or NULL if the name is unknown. +// EVP_get_digestbyname returns an `EVP_MD` given a human readable name in +// `name`, or NULL if the name is unknown. OPENSSL_EXPORT const EVP_MD *EVP_get_digestbyname(const char *name); // EVP_dss1 returns the value of EVP_sha1(). This was provided by OpenSSL to @@ -266,18 +331,19 @@ OPENSSL_EXPORT const EVP_MD *EVP_get_digestbyname(const char *name); // interface will always fail. OPENSSL_EXPORT const EVP_MD *EVP_dss1(void); -// EVP_MD_CTX_create calls |EVP_MD_CTX_new|. +// EVP_MD_CTX_create calls `EVP_MD_CTX_new`. OPENSSL_EXPORT EVP_MD_CTX *EVP_MD_CTX_create(void); -// EVP_MD_CTX_destroy calls |EVP_MD_CTX_free|. +// EVP_MD_CTX_destroy calls `EVP_MD_CTX_free`. OPENSSL_EXPORT void EVP_MD_CTX_destroy(EVP_MD_CTX *ctx); -// EVP_DigestFinalXOF returns zero and adds an error to the error queue. -// BoringSSL does not support any XOF digests. +// EVP_DigestFinalXOF behaves like `EVP_DigestSqueeze` but calls +// `EVP_MD_CTX_cleanup` after being called, so it is not possible to call it +// multiple times. OPENSSL_EXPORT int EVP_DigestFinalXOF(EVP_MD_CTX *ctx, uint8_t *out, size_t len); -// EVP_MD_meth_get_flags calls |EVP_MD_flags|. +// EVP_MD_meth_get_flags calls `EVP_MD_flags`. OPENSSL_EXPORT uint32_t EVP_MD_meth_get_flags(const EVP_MD *md); // EVP_MD_CTX_set_flags does nothing. @@ -285,27 +351,27 @@ OPENSSL_EXPORT void EVP_MD_CTX_set_flags(EVP_MD_CTX *ctx, int flags); // EVP_MD_CTX_FLAG_NON_FIPS_ALLOW is meaningless. In OpenSSL it permits non-FIPS // algorithms in FIPS mode. But BoringSSL FIPS mode doesn't prohibit algorithms -// (it's up the the caller to use the FIPS module in a fashion compliant with +// (it's up to the caller to use the FIPS module in a fashion compliant with // their needs). Thus this exists only to allow code to compile. #define EVP_MD_CTX_FLAG_NON_FIPS_ALLOW 0 -// EVP_MD_nid calls |EVP_MD_type|. +// EVP_MD_nid calls `EVP_MD_type`. OPENSSL_EXPORT int EVP_MD_nid(const EVP_MD *md); -// EVP_MD_fetch behaves like |EVP_get_digestbyname|. |libctx| and |propq| are -// ignored. Although it returns a non-const pointer, |EVP_MD|s in BoringSSL are +// EVP_MD_fetch behaves like `EVP_get_digestbyname`. `libctx` and `propq` are +// ignored. Although it returns a non-const pointer, `EVP_MD`s in BoringSSL are // static and do not need to be freed. OPENSSL_EXPORT EVP_MD *EVP_MD_fetch(OSSL_LIB_CTX *libctx, const char *name, const char *propq); -// EVP_MD_up_ref returns one. |EVP_MD|s in BoringSSL are static. +// EVP_MD_up_ref returns one. `EVP_MD`s in BoringSSL are static. OPENSSL_EXPORT int EVP_MD_up_ref(EVP_MD *md); -// EVP_MD_free does nothing. |EVP_MD|s in BoringSSL are static. +// EVP_MD_free does nothing. `EVP_MD`s in BoringSSL are static. OPENSSL_EXPORT void EVP_MD_free(EVP_MD *md); -// EVP_Q_digest behaves like |EVP_Digest| but specifies the digest by a string -// |name|. |libctx| and |propq| are ignored. +// EVP_Q_digest behaves like `EVP_Digest` but specifies the digest by a string +// `name`. `libctx` and `propq` are ignored. OPENSSL_EXPORT int EVP_Q_digest(OSSL_LIB_CTX *libctx, const char *name, const char *propq, const void *in, size_t in_len, uint8_t *out, size_t *out_len); @@ -315,9 +381,9 @@ OPENSSL_EXPORT int EVP_Q_digest(OSSL_LIB_CTX *libctx, const char *name, struct evp_md_pctx_ops; // EVP_MAX_MD_DATA_SIZE is a private constant which specifies the size of the -// largest digest state. SHA-512 and BLAKE2b are joint-largest. Consuming code -// only uses this via the `EVP_MD_CTX` type. -#define EVP_MAX_MD_DATA_SIZE 208 +// largest digest state. SHA3-256 is the largest. Consuming code only uses this +// via the `EVP_MD_CTX` type. +#define EVP_MAX_MD_DATA_SIZE 240 // env_md_ctx_st is typoed ("evp" -> "env"), but the typo comes from OpenSSL // and some consumers forward-declare these structures so we're leaving it @@ -337,7 +403,7 @@ struct env_md_ctx_st { EVP_PKEY_CTX *pctx; // pctx_ops, if not NULL, points to a vtable that contains functions to - // manipulate |pctx|. + // manipulate `pctx`. const struct evp_md_pctx_ops *pctx_ops; } /* EVP_MD_CTX */; @@ -352,9 +418,9 @@ BSSL_NAMESPACE_BEGIN BORINGSSL_MAKE_DELETER(EVP_MD_CTX, EVP_MD_CTX_free) -using ScopedEVP_MD_CTX = - internal::StackAllocatedMovable; +BORINGSSL_MAKE_STACK_TRAITS_MOVABLE(EVP_MD_CTX, EVP_MD_CTX_init, + EVP_MD_CTX_cleanup, EVP_MD_CTX_move) +using ScopedEVP_MD_CTX = internal::StackAllocatedMovable; BSSL_NAMESPACE_END diff --git a/third_party/boringssl/src/include/openssl/dsa.h b/third_party/boringssl/src/include/openssl/dsa.h index 9b5028613..a77bf9105 100644 --- a/third_party/boringssl/src/include/openssl/dsa.h +++ b/third_party/boringssl/src/include/openssl/dsa.h @@ -15,7 +15,7 @@ #ifndef OPENSSL_HEADER_DSA_H #define OPENSSL_HEADER_DSA_H -#include // IWYU pragma: export +#include // IWYU pragma: export #include @@ -34,23 +34,28 @@ extern "C" { // Allocation and destruction. // -// A |DSA| object represents a DSA key or group parameters. A given object may +// A `DSA` object represents a DSA key or group parameters. A given object may // be used concurrently on multiple threads by non-mutating functions, provided // no other thread is concurrently calling a mutating function. Unless otherwise -// documented, functions which take a |const| pointer are non-mutating and -// functions which take a non-|const| pointer are mutating. +// documented, functions which take a `const` pointer are non-mutating and +// functions which take a non-`const` pointer are mutating. // DSA_new returns a new, empty DSA object or NULL on error. OPENSSL_EXPORT DSA *DSA_new(void); -// DSA_free decrements the reference count of |dsa| and frees it if the +// DSA_free decrements the reference count of `dsa` and frees it if the // reference count drops to zero. OPENSSL_EXPORT void DSA_free(DSA *dsa); -// DSA_up_ref increments the reference count of |dsa| and returns one. It does -// not mutate |dsa| for thread-safety purposes and may be used concurrently. +// DSA_up_ref increments the reference count of `dsa` and returns one. It does +// not mutate `dsa` for thread-safety purposes and may be used concurrently. OPENSSL_EXPORT int DSA_up_ref(DSA *dsa); +// DSA_dup_ref increments the reference count of `dsa` and returns the same +// handle. It does not mutate `dsa` for thread-safety purposes and may be used +// concurrently. +OPENSSL_EXPORT DSA *DSA_dup_ref(const DSA *dsa); + // Properties. @@ -58,49 +63,49 @@ OPENSSL_EXPORT int DSA_up_ref(DSA *dsa); // bits. #define OPENSSL_DSA_MAX_MODULUS_BITS 8192 -// DSA_bits returns the size of |dsa|'s group modulus, in bits. +// DSA_bits returns the size of `dsa`'s group modulus, in bits. OPENSSL_EXPORT unsigned DSA_bits(const DSA *dsa); -// DSA_get0_pub_key returns |dsa|'s public key. +// DSA_get0_pub_key returns `dsa`'s public key. OPENSSL_EXPORT const BIGNUM *DSA_get0_pub_key(const DSA *dsa); -// DSA_get0_priv_key returns |dsa|'s private key, or NULL if |dsa| is a public +// DSA_get0_priv_key returns `dsa`'s private key, or NULL if `dsa` is a public // key. OPENSSL_EXPORT const BIGNUM *DSA_get0_priv_key(const DSA *dsa); -// DSA_get0_p returns |dsa|'s group modulus. +// DSA_get0_p returns `dsa`'s group modulus. OPENSSL_EXPORT const BIGNUM *DSA_get0_p(const DSA *dsa); -// DSA_get0_q returns the size of |dsa|'s subgroup. +// DSA_get0_q returns the size of `dsa`'s subgroup. OPENSSL_EXPORT const BIGNUM *DSA_get0_q(const DSA *dsa); -// DSA_get0_g returns |dsa|'s group generator. +// DSA_get0_g returns `dsa`'s group generator. OPENSSL_EXPORT const BIGNUM *DSA_get0_g(const DSA *dsa); -// DSA_get0_key sets |*out_pub_key| and |*out_priv_key|, if non-NULL, to |dsa|'s -// public and private key, respectively. If |dsa| is a public key, the private +// DSA_get0_key sets `*out_pub_key` and `*out_priv_key`, if non-NULL, to `dsa`'s +// public and private key, respectively. If `dsa` is a public key, the private // key will be set to NULL. OPENSSL_EXPORT void DSA_get0_key(const DSA *dsa, const BIGNUM **out_pub_key, const BIGNUM **out_priv_key); -// DSA_get0_pqg sets |*out_p|, |*out_q|, and |*out_g|, if non-NULL, to |dsa|'s +// DSA_get0_pqg sets `*out_p`, `*out_q`, and `*out_g`, if non-NULL, to `dsa`'s // p, q, and g parameters, respectively. OPENSSL_EXPORT void DSA_get0_pqg(const DSA *dsa, const BIGNUM **out_p, const BIGNUM **out_q, const BIGNUM **out_g); -// DSA_set0_key sets |dsa|'s public and private key to |pub_key| and |priv_key|, +// DSA_set0_key sets `dsa`'s public and private key to `pub_key` and `priv_key`, // respectively, if non-NULL. On success, it takes ownership of each argument // and returns one. Otherwise, it returns zero. // -// |priv_key| may be NULL, but |pub_key| must either be non-NULL or already -// configured on |dsa|. +// `priv_key` may be NULL, but `pub_key` must either be non-NULL or already +// configured on `dsa`. OPENSSL_EXPORT int DSA_set0_key(DSA *dsa, BIGNUM *pub_key, BIGNUM *priv_key); -// DSA_set0_pqg sets |dsa|'s parameters to |p|, |q|, and |g|, if non-NULL, and +// DSA_set0_pqg sets `dsa`'s parameters to `p`, `q`, and `g`, if non-NULL, and // takes ownership of them. On success, it takes ownership of each argument and // returns one. Otherwise, it returns zero. // -// Each argument must either be non-NULL or already configured on |dsa|. +// Each argument must either be non-NULL or already configured on `dsa`. OPENSSL_EXPORT int DSA_set0_pqg(DSA *dsa, BIGNUM *p, BIGNUM *q, BIGNUM *g); @@ -110,17 +115,17 @@ OPENSSL_EXPORT int DSA_set0_pqg(DSA *dsa, BIGNUM *p, BIGNUM *q, BIGNUM *g); // the procedure given in FIPS 186-4, appendix A. // (http://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.186-4.pdf) // -// The larger prime will have a length of |bits| (e.g. 2048). The |seed| value +// The larger prime will have a length of `bits` (e.g. 2048). The `seed` value // allows others to generate and verify the same parameters and should be -// random input which is kept for reference. If |out_counter| or |out_h| are +// random input which is kept for reference. If `out_counter` or `out_h` are // not NULL then the counter and h value used in the generation are written to // them. // -// The |cb| argument is passed to |BN_generate_prime_ex| and is thus called +// The `cb` argument is passed to `BN_generate_prime_ex` and is thus called // during the generation process in order to indicate progress. See the // comments for that function for details. In addition to the calls made by -// |BN_generate_prime_ex|, |DSA_generate_parameters_ex| will call it with -// |event| equal to 2 and 3 at different stages of the process. +// `BN_generate_prime_ex`, `DSA_generate_parameters_ex` will call it with +// `event` equal to 2 and 3 at different stages of the process. // // It returns one on success and zero otherwise. OPENSSL_EXPORT int DSA_generate_parameters_ex(DSA *dsa, unsigned bits, @@ -129,14 +134,14 @@ OPENSSL_EXPORT int DSA_generate_parameters_ex(DSA *dsa, unsigned bits, unsigned long *out_h, BN_GENCB *cb); -// DSAparams_dup returns a freshly allocated |DSA| that contains a copy of the -// parameters from |dsa|. It returns NULL on error. +// DSAparams_dup returns a freshly allocated `DSA` that contains a copy of the +// parameters from `dsa`. It returns NULL on error. OPENSSL_EXPORT DSA *DSAparams_dup(const DSA *dsa); // Key generation. -// DSA_generate_key generates a public/private key pair in |dsa|, which must +// DSA_generate_key generates a public/private key pair in `dsa`, which must // already have parameters setup. It returns one on success and zero on // error. OPENSSL_EXPORT int DSA_generate_key(DSA *dsa); @@ -144,49 +149,49 @@ OPENSSL_EXPORT int DSA_generate_key(DSA *dsa); // Signatures. -// DSA_SIG_st (aka |DSA_SIG|) contains a DSA signature as a pair of integers. +// DSA_SIG_st (aka `DSA_SIG`) contains a DSA signature as a pair of integers. struct DSA_SIG_st { BIGNUM *r, *s; }; // DSA_SIG_new returns a freshly allocated, DIG_SIG structure or NULL on error. -// Both |r| and |s| in the signature will be NULL. +// Both `r` and `s` in the signature will be NULL. OPENSSL_EXPORT DSA_SIG *DSA_SIG_new(void); -// DSA_SIG_free frees the contents of |sig| and then frees |sig| itself. +// DSA_SIG_free frees the contents of `sig` and then frees `sig` itself. OPENSSL_EXPORT void DSA_SIG_free(DSA_SIG *sig); -// DSA_SIG_get0 sets |*out_r| and |*out_s|, if non-NULL, to the two components -// of |sig|. +// DSA_SIG_get0 sets `*out_r` and `*out_s`, if non-NULL, to the two components +// of `sig`. OPENSSL_EXPORT void DSA_SIG_get0(const DSA_SIG *sig, const BIGNUM **out_r, const BIGNUM **out_s); -// DSA_SIG_set0 sets |sig|'s components to |r| and |s|, neither of which may be +// DSA_SIG_set0 sets `sig`'s components to `r` and `s`, neither of which may be // NULL. On success, it takes ownership of each argument and returns one. // Otherwise, it returns zero. OPENSSL_EXPORT int DSA_SIG_set0(DSA_SIG *sig, BIGNUM *r, BIGNUM *s); -// DSA_do_sign returns a signature of the hash in |digest| by the key in |dsa| +// DSA_do_sign returns a signature of the hash in `digest` by the key in `dsa` // and returns an allocated, DSA_SIG structure, or NULL on error. OPENSSL_EXPORT DSA_SIG *DSA_do_sign(const uint8_t *digest, size_t digest_len, const DSA *dsa); -// DSA_do_verify verifies that |sig| is a valid signature, by the public key in -// |dsa|, of the hash in |digest|. It returns one if so, zero if invalid and -1 +// DSA_do_verify verifies that `sig` is a valid signature, by the public key in +// `dsa`, of the hash in `digest`. It returns one if so, zero if invalid and -1 // on error. // // WARNING: do not use. This function returns -1 for error, 0 for invalid and 1 // for valid. However, this is dangerously different to the usual OpenSSL -// convention and could be a disaster if a user did |if (DSA_do_verify(...))|. -// Because of this, |DSA_check_signature| is a safer version of this. +// convention and could be a disaster if a user did `if (DSA_do_verify(...))`. +// Because of this, `DSA_check_signature` is a safer version of this. // // TODO(fork): deprecate. OPENSSL_EXPORT int DSA_do_verify(const uint8_t *digest, size_t digest_len, const DSA_SIG *sig, const DSA *dsa); -// DSA_do_check_signature sets |*out_valid| to zero. Then it verifies that |sig| -// is a valid signature, by the public key in |dsa| of the hash in |digest| -// and, if so, it sets |*out_valid| to one. +// DSA_do_check_signature sets `*out_valid` to zero. Then it verifies that `sig` +// is a valid signature, by the public key in `dsa` of the hash in `digest` +// and, if so, it sets `*out_valid` to one. // // It returns one if it was able to verify the signature as valid or invalid, // and zero on error. @@ -198,38 +203,38 @@ OPENSSL_EXPORT int DSA_do_check_signature(int *out_valid, const uint8_t *digest, // ASN.1 signatures. // // These functions also perform DSA signature operations, but deal with ASN.1 -// encoded signatures as opposed to raw |BIGNUM|s. If you don't know what +// encoded signatures as opposed to raw `BIGNUM`s. If you don't know what // encoding a DSA signature is in, it's probably ASN.1. -// DSA_sign signs |digest| with the key in |dsa| and writes the resulting -// signature, in ASN.1 form, to |out_sig| and the length of the signature to -// |*out_siglen|. There must be, at least, |DSA_size(dsa)| bytes of space in -// |out_sig|. It returns one on success and zero otherwise. +// DSA_sign signs `digest` with the key in `dsa` and writes the resulting +// signature, in ASN.1 form, to `out_sig` and the length of the signature to +// `*out_siglen`. There must be, at least, `DSA_size(dsa)` bytes of space in +// `out_sig`. It returns one on success and zero otherwise. // -// (The |type| argument is ignored.) +// (The `type` argument is ignored.) OPENSSL_EXPORT int DSA_sign(int type, const uint8_t *digest, size_t digest_len, uint8_t *out_sig, unsigned int *out_siglen, const DSA *dsa); -// DSA_verify verifies that |sig| is a valid, ASN.1 signature, by the public -// key in |dsa|, of the hash in |digest|. It returns one if so, zero if invalid +// DSA_verify verifies that `sig` is a valid, ASN.1 signature, by the public +// key in `dsa`, of the hash in `digest`. It returns one if so, zero if invalid // and -1 on error. // -// (The |type| argument is ignored.) +// (The `type` argument is ignored.) // // WARNING: do not use. This function returns -1 for error, 0 for invalid and 1 // for valid. However, this is dangerously different to the usual OpenSSL -// convention and could be a disaster if a user did |if (DSA_do_verify(...))|. -// Because of this, |DSA_check_signature| is a safer version of this. +// convention and could be a disaster if a user did `if (DSA_do_verify(...))`. +// Because of this, `DSA_check_signature` is a safer version of this. // // TODO(fork): deprecate. OPENSSL_EXPORT int DSA_verify(int type, const uint8_t *digest, size_t digest_len, const uint8_t *sig, size_t sig_len, const DSA *dsa); -// DSA_check_signature sets |*out_valid| to zero. Then it verifies that |sig| -// is a valid, ASN.1 signature, by the public key in |dsa|, of the hash in -// |digest|. If so, it sets |*out_valid| to one. +// DSA_check_signature sets `*out_valid` to zero. Then it verifies that `sig` +// is a valid, ASN.1 signature, by the public key in `dsa`, of the hash in +// `digest`. If so, it sets `*out_valid` to one. // // It returns one if it was able to verify the signature as valid or invalid, // and zero on error. @@ -238,60 +243,60 @@ OPENSSL_EXPORT int DSA_check_signature(int *out_valid, const uint8_t *digest, size_t sig_len, const DSA *dsa); // DSA_size returns the size, in bytes, of an ASN.1 encoded, DSA signature -// generated by |dsa|. Parameters must already have been setup in |dsa|. +// generated by `dsa`. Parameters must already have been setup in `dsa`. OPENSSL_EXPORT int DSA_size(const DSA *dsa); // ASN.1 encoding. -// DSA_SIG_parse parses a DER-encoded DSA-Sig-Value structure from |cbs| and -// advances |cbs|. It returns a newly-allocated |DSA_SIG| or NULL on error. +// DSA_SIG_parse parses a DER-encoded DSA-Sig-Value structure from `cbs` and +// advances `cbs`. It returns a newly-allocated `DSA_SIG` or NULL on error. OPENSSL_EXPORT DSA_SIG *DSA_SIG_parse(CBS *cbs); -// DSA_SIG_marshal marshals |sig| as a DER-encoded DSA-Sig-Value and appends the -// result to |cbb|. It returns one on success and zero on error. +// DSA_SIG_marshal marshals `sig` as a DER-encoded DSA-Sig-Value and appends the +// result to `cbb`. It returns one on success and zero on error. OPENSSL_EXPORT int DSA_SIG_marshal(CBB *cbb, const DSA_SIG *sig); -// DSA_parse_public_key parses a DER-encoded DSA public key from |cbs| and -// advances |cbs|. It returns a newly-allocated |DSA| or NULL on error. +// DSA_parse_public_key parses a DER-encoded DSA public key from `cbs` and +// advances `cbs`. It returns a newly-allocated `DSA` or NULL on error. OPENSSL_EXPORT DSA *DSA_parse_public_key(CBS *cbs); -// DSA_marshal_public_key marshals |dsa| as a DER-encoded DSA public key and -// appends the result to |cbb|. It returns one on success and zero on +// DSA_marshal_public_key marshals `dsa` as a DER-encoded DSA public key and +// appends the result to `cbb`. It returns one on success and zero on // failure. OPENSSL_EXPORT int DSA_marshal_public_key(CBB *cbb, const DSA *dsa); -// DSA_parse_private_key parses a DER-encoded DSA private key from |cbs| and -// advances |cbs|. It returns a newly-allocated |DSA| or NULL on error. +// DSA_parse_private_key parses a DER-encoded DSA private key from `cbs` and +// advances `cbs`. It returns a newly-allocated `DSA` or NULL on error. OPENSSL_EXPORT DSA *DSA_parse_private_key(CBS *cbs); -// DSA_marshal_private_key marshals |dsa| as a DER-encoded DSA private key and -// appends the result to |cbb|. It returns one on success and zero on +// DSA_marshal_private_key marshals `dsa` as a DER-encoded DSA private key and +// appends the result to `cbb`. It returns one on success and zero on // failure. OPENSSL_EXPORT int DSA_marshal_private_key(CBB *cbb, const DSA *dsa); // DSA_parse_parameters parses a DER-encoded Dss-Parms structure (RFC 3279) -// from |cbs| and advances |cbs|. It returns a newly-allocated |DSA| or NULL on +// from `cbs` and advances `cbs`. It returns a newly-allocated `DSA` or NULL on // error. OPENSSL_EXPORT DSA *DSA_parse_parameters(CBS *cbs); -// DSA_marshal_parameters marshals |dsa| as a DER-encoded Dss-Parms structure -// (RFC 3279) and appends the result to |cbb|. It returns one on success and +// DSA_marshal_parameters marshals `dsa` as a DER-encoded Dss-Parms structure +// (RFC 3279) and appends the result to `cbb`. It returns one on success and // zero on failure. OPENSSL_EXPORT int DSA_marshal_parameters(CBB *cbb, const DSA *dsa); // Conversion. -// DSA_dup_DH returns a |DH| constructed from the parameters of |dsa|. This is +// DSA_dup_DH returns a `DH` constructed from the parameters of `dsa`. This is // sometimes needed when Diffie-Hellman parameters are stored in the form of -// DSA parameters. It returns an allocated |DH| on success or NULL on error. +// DSA parameters. It returns an allocated `DH` on success or NULL on error. OPENSSL_EXPORT DH *DSA_dup_DH(const DSA *dsa); // ex_data functions. // -// See |ex_data.h| for details. +// See `ex_data.h` for details. OPENSSL_EXPORT int DSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused, @@ -303,57 +308,57 @@ OPENSSL_EXPORT void *DSA_get_ex_data(const DSA *dsa, int idx); // Deprecated functions. -// d2i_DSA_SIG parses a DER-encoded DSA-Sig-Value structure from |len| bytes at -// |*inp|, as described in |d2i_SAMPLE|. +// d2i_DSA_SIG parses a DER-encoded DSA-Sig-Value structure from `len` bytes at +// `*inp`, as described in `d2i_SAMPLE`. // -// Use |DSA_SIG_parse| instead. +// Use `DSA_SIG_parse` instead. OPENSSL_EXPORT DSA_SIG *d2i_DSA_SIG(DSA_SIG **out_sig, const uint8_t **inp, long len); -// i2d_DSA_SIG marshals |in| to a DER-encoded DSA-Sig-Value structure, as -// described in |i2d_SAMPLE|. +// i2d_DSA_SIG marshals `in` to a DER-encoded DSA-Sig-Value structure, as +// described in `i2d_SAMPLE`. // -// Use |DSA_SIG_marshal| instead. +// Use `DSA_SIG_marshal` instead. OPENSSL_EXPORT int i2d_DSA_SIG(const DSA_SIG *in, uint8_t **outp); -// d2i_DSAPublicKey parses a DER-encoded DSA public key from |len| bytes at -// |*inp|, as described in |d2i_SAMPLE|. +// d2i_DSAPublicKey parses a DER-encoded DSA public key from `len` bytes at +// `*inp`, as described in `d2i_SAMPLE`. // -// Use |DSA_parse_public_key| instead. +// Use `DSA_parse_public_key` instead. OPENSSL_EXPORT DSA *d2i_DSAPublicKey(DSA **out, const uint8_t **inp, long len); -// i2d_DSAPublicKey marshals |in| as a DER-encoded DSA public key, as described -// in |i2d_SAMPLE|. +// i2d_DSAPublicKey marshals `in` as a DER-encoded DSA public key, as described +// in `i2d_SAMPLE`. // -// Use |DSA_marshal_public_key| instead. +// Use `DSA_marshal_public_key` instead. OPENSSL_EXPORT int i2d_DSAPublicKey(const DSA *in, uint8_t **outp); -// d2i_DSAPrivateKey parses a DER-encoded DSA private key from |len| bytes at -// |*inp|, as described in |d2i_SAMPLE|. +// d2i_DSAPrivateKey parses a DER-encoded DSA private key from `len` bytes at +// `*inp`, as described in `d2i_SAMPLE`. // -// Use |DSA_parse_private_key| instead. +// Use `DSA_parse_private_key` instead. OPENSSL_EXPORT DSA *d2i_DSAPrivateKey(DSA **out, const uint8_t **inp, long len); -// i2d_DSAPrivateKey marshals |in| as a DER-encoded DSA private key, as -// described in |i2d_SAMPLE|. +// i2d_DSAPrivateKey marshals `in` as a DER-encoded DSA private key, as +// described in `i2d_SAMPLE`. // -// Use |DSA_marshal_private_key| instead. +// Use `DSA_marshal_private_key` instead. OPENSSL_EXPORT int i2d_DSAPrivateKey(const DSA *in, uint8_t **outp); -// d2i_DSAparams parses a DER-encoded Dss-Parms structure (RFC 3279) from |len| -// bytes at |*inp|, as described in |d2i_SAMPLE|. +// d2i_DSAparams parses a DER-encoded Dss-Parms structure (RFC 3279) from `len` +// bytes at `*inp`, as described in `d2i_SAMPLE`. // -// Use |DSA_parse_parameters| instead. +// Use `DSA_parse_parameters` instead. OPENSSL_EXPORT DSA *d2i_DSAparams(DSA **out, const uint8_t **inp, long len); -// i2d_DSAparams marshals |in|'s parameters as a DER-encoded Dss-Parms structure -// (RFC 3279), as described in |i2d_SAMPLE|. +// i2d_DSAparams marshals `in`'s parameters as a DER-encoded Dss-Parms structure +// (RFC 3279), as described in `i2d_SAMPLE`. // -// Use |DSA_marshal_parameters| instead. +// Use `DSA_marshal_parameters` instead. OPENSSL_EXPORT int i2d_DSAparams(const DSA *in, uint8_t **outp); // DSA_generate_parameters is a deprecated version of -// |DSA_generate_parameters_ex| that creates and returns a |DSA*|. Don't use +// `DSA_generate_parameters_ex` that creates and returns a `DSA*`. Don't use // it. OPENSSL_EXPORT DSA *DSA_generate_parameters(int bits, unsigned char *seed, int seed_len, int *counter_ret, @@ -370,7 +375,7 @@ extern "C++" { BSSL_NAMESPACE_BEGIN BORINGSSL_MAKE_DELETER(DSA, DSA_free) -BORINGSSL_MAKE_UP_REF(DSA, DSA_up_ref) +BORINGSSL_MAKE_UP_REF(DSA, DSA_dup_ref) BORINGSSL_MAKE_DELETER(DSA_SIG, DSA_SIG_free) BSSL_NAMESPACE_END diff --git a/third_party/boringssl/src/include/openssl/ec.h b/third_party/boringssl/src/include/openssl/ec.h index 247922866..3d8d4e962 100644 --- a/third_party/boringssl/src/include/openssl/ec.h +++ b/third_party/boringssl/src/include/openssl/ec.h @@ -27,7 +27,7 @@ extern "C" { // point_conversion_form_t enumerates forms, as defined in X9.62 (ECDSA), for -// the encoding of a elliptic curve point (x,y) +// the encoding of an elliptic curve point (x,y) typedef enum { // POINT_CONVERSION_COMPRESSED indicates that the point is encoded as z||x, // where the octet z specifies which solution of the quadratic equation y @@ -49,143 +49,143 @@ typedef enum { // Elliptic curve groups. // -// Elliptic curve groups are represented by |EC_GROUP| objects. Unlike OpenSSL, -// if limited to the APIs in this section, callers may treat |EC_GROUP|s as +// Elliptic curve groups are represented by `EC_GROUP` objects. Unlike OpenSSL, +// if limited to the APIs in this section, callers may treat `EC_GROUP`s as // static, immutable objects which do not need to be copied or released. In -// BoringSSL, only custom |EC_GROUP|s created by |EC_GROUP_new_curve_GFp| +// BoringSSL, only custom `EC_GROUP`s created by `EC_GROUP_new_curve_GFp` // (deprecated) are dynamic. // -// Callers may cast away |const| and use |EC_GROUP_dup| and |EC_GROUP_free| with +// Callers may cast away `const` and use `EC_GROUP_dup` and `EC_GROUP_free` with // static groups, for compatibility with OpenSSL or dynamic groups, but it is // otherwise unnecessary. -// EC_group_p224 returns an |EC_GROUP| for P-224, also known as secp224r1. +// EC_group_p224 returns an `EC_GROUP` for P-224, also known as secp224r1. OPENSSL_EXPORT const EC_GROUP *EC_group_p224(void); -// EC_group_p256 returns an |EC_GROUP| for P-256, also known as secp256r1 or +// EC_group_p256 returns an `EC_GROUP` for P-256, also known as secp256r1 or // prime256v1. OPENSSL_EXPORT const EC_GROUP *EC_group_p256(void); -// EC_group_p384 returns an |EC_GROUP| for P-384, also known as secp384r1. +// EC_group_p384 returns an `EC_GROUP` for P-384, also known as secp384r1. OPENSSL_EXPORT const EC_GROUP *EC_group_p384(void); -// EC_group_p521 returns an |EC_GROUP| for P-521, also known as secp521r1. +// EC_group_p521 returns an `EC_GROUP` for P-521, also known as secp521r1. OPENSSL_EXPORT const EC_GROUP *EC_group_p521(void); -// EC_GROUP_new_by_curve_name returns the |EC_GROUP| object for the elliptic -// curve specified by |nid|, or NULL on unsupported NID. For OpenSSL +// EC_GROUP_new_by_curve_name returns the `EC_GROUP` object for the elliptic +// curve specified by `nid`, or NULL on unsupported NID. For OpenSSL // compatibility, this function returns a non-const pointer which may be passed -// to |EC_GROUP_free|. However, the resulting object is actually static and -// calling |EC_GROUP_free| is optional. +// to `EC_GROUP_free`. However, the resulting object is actually static and +// calling `EC_GROUP_free` is optional. // // The supported NIDs are: -// - |NID_secp224r1| (P-224) -// - |NID_X9_62_prime256v1| (P-256) -// - |NID_secp384r1| (P-384) -// - |NID_secp521r1| (P-521) +// - `NID_secp224r1` (P-224) +// - `NID_X9_62_prime256v1` (P-256) +// - `NID_secp384r1` (P-384) +// - `NID_secp521r1` (P-521) // // Calling this function causes all four curves to be linked into the binary. -// Prefer calling |EC_group_*| to allow the static linker to drop unused curves. +// Prefer calling `EC_group_*` to allow the static linker to drop unused curves. // -// If in doubt, use |NID_X9_62_prime256v1|, or see the curve25519.h header for +// If in doubt, use `NID_X9_62_prime256v1`, or see the curve25519.h header for // more modern primitives. OPENSSL_EXPORT EC_GROUP *EC_GROUP_new_by_curve_name(int nid); -// EC_GROUP_cmp returns zero if |a| and |b| are the same group and non-zero +// EC_GROUP_cmp returns zero if `a` and `b` are the same group and non-zero // otherwise. OPENSSL_EXPORT int EC_GROUP_cmp(const EC_GROUP *a, const EC_GROUP *b, BN_CTX *ignored); -// EC_GROUP_get0_generator returns a pointer to the internal |EC_POINT| object -// in |group| that specifies the generator for the group. +// EC_GROUP_get0_generator returns a pointer to the internal `EC_POINT` object +// in `group` that specifies the generator for the group. OPENSSL_EXPORT const EC_POINT *EC_GROUP_get0_generator(const EC_GROUP *group); -// EC_GROUP_get0_order returns a pointer to the internal |BIGNUM| object in -// |group| that specifies the order of the group. +// EC_GROUP_get0_order returns a pointer to the internal `BIGNUM` object in +// `group` that specifies the order of the group. OPENSSL_EXPORT const BIGNUM *EC_GROUP_get0_order(const EC_GROUP *group); -// EC_GROUP_order_bits returns the number of bits of the order of |group|. +// EC_GROUP_order_bits returns the number of bits of the order of `group`. OPENSSL_EXPORT int EC_GROUP_order_bits(const EC_GROUP *group); -// EC_GROUP_get_cofactor sets |*cofactor| to the cofactor of |group|. It returns -// one on success and zero otherwise. |ctx| is ignored and may be NULL. +// EC_GROUP_get_cofactor sets `*cofactor` to the cofactor of `group`. It returns +// one on success and zero otherwise. `ctx` is ignored and may be NULL. OPENSSL_EXPORT int EC_GROUP_get_cofactor(const EC_GROUP *group, BIGNUM *cofactor, BN_CTX *ctx); // EC_GROUP_get_curve_GFp gets various parameters about a group. It sets -// |*out_p| to the order of the coordinate field and |*out_a| and |*out_b| to +// `*out_p` to the order of the coordinate field and `*out_a` and `*out_b` to // the parameters of the curve when expressed as y² = x³ + ax + b. Any of the // output parameters can be NULL. It returns one on success and zero on -// error. |ctx| is ignored and may be NULL. +// error. `ctx` is ignored and may be NULL. OPENSSL_EXPORT int EC_GROUP_get_curve_GFp(const EC_GROUP *group, BIGNUM *out_p, BIGNUM *out_a, BIGNUM *out_b, BN_CTX *ctx); -// EC_GROUP_get_curve_name returns a NID that identifies |group|. +// EC_GROUP_get_curve_name returns a NID that identifies `group`. OPENSSL_EXPORT int EC_GROUP_get_curve_name(const EC_GROUP *group); // EC_GROUP_get_degree returns the number of bits needed to represent an -// element of the field underlying |group|. +// element of the field underlying `group`. OPENSSL_EXPORT unsigned EC_GROUP_get_degree(const EC_GROUP *group); // EC_curve_nid2nist returns the NIST name of the elliptic curve specified by -// |nid|, or NULL if |nid| is not a NIST curve. For example, it returns "P-256" -// for |NID_X9_62_prime256v1|. +// `nid`, or NULL if `nid` is not a NIST curve. For example, it returns "P-256" +// for `NID_X9_62_prime256v1`. OPENSSL_EXPORT const char *EC_curve_nid2nist(int nid); // EC_curve_nist2nid returns the NID of the elliptic curve specified by the NIST -// name |name|, or |NID_undef| if |name| is not a recognized name. For example, -// it returns |NID_X9_62_prime256v1| for "P-256". +// name `name`, or `NID_undef` if `name` is not a recognized name. For example, +// it returns `NID_X9_62_prime256v1` for "P-256". OPENSSL_EXPORT int EC_curve_nist2nid(const char *name); // Points on elliptic curves. -// EC_POINT_new returns a fresh |EC_POINT| object in the given group, or NULL +// EC_POINT_new returns a fresh `EC_POINT` object in the given group, or NULL // on error. OPENSSL_EXPORT EC_POINT *EC_POINT_new(const EC_GROUP *group); -// EC_POINT_free frees |point| and the data that it points to. +// EC_POINT_free frees `point` and the data that it points to. OPENSSL_EXPORT void EC_POINT_free(EC_POINT *point); -// EC_POINT_copy sets |*dest| equal to |*src|. It returns one on success and +// EC_POINT_copy sets `*dest` equal to `*src`. It returns one on success and // zero otherwise. OPENSSL_EXPORT int EC_POINT_copy(EC_POINT *dest, const EC_POINT *src); -// EC_POINT_dup returns a fresh |EC_POINT| that contains the same values as -// |src|, or NULL on error. +// EC_POINT_dup returns a fresh `EC_POINT` that contains the same values as +// `src`, or NULL on error. OPENSSL_EXPORT EC_POINT *EC_POINT_dup(const EC_POINT *src, const EC_GROUP *group); -// EC_POINT_set_to_infinity sets |point| to be the "point at infinity" for the +// EC_POINT_set_to_infinity sets `point` to be the "point at infinity" for the // given group. OPENSSL_EXPORT int EC_POINT_set_to_infinity(const EC_GROUP *group, EC_POINT *point); -// EC_POINT_is_at_infinity returns one iff |point| is the point at infinity and +// EC_POINT_is_at_infinity returns one iff `point` is the point at infinity and // zero otherwise. OPENSSL_EXPORT int EC_POINT_is_at_infinity(const EC_GROUP *group, const EC_POINT *point); -// EC_POINT_is_on_curve returns one if |point| is an element of |group| and +// EC_POINT_is_on_curve returns one if `point` is an element of `group` and // and zero otherwise or when an error occurs. This is different from OpenSSL, -// which returns -1 on error. |ctx| is ignored and may be NULL. +// which returns -1 on error. `ctx` is ignored and may be NULL. OPENSSL_EXPORT int EC_POINT_is_on_curve(const EC_GROUP *group, const EC_POINT *point, BN_CTX *ctx); -// EC_POINT_cmp returns zero if |a| is equal to |b|, greater than zero if -// not equal and -1 on error. |ctx| is ignored and may be NULL. +// EC_POINT_cmp returns zero if `a` is equal to `b`, greater than zero if +// not equal and -1 on error. `ctx` is ignored and may be NULL. OPENSSL_EXPORT int EC_POINT_cmp(const EC_GROUP *group, const EC_POINT *a, const EC_POINT *b, BN_CTX *ctx); // Point conversion. -// EC_POINT_get_affine_coordinates_GFp sets |x| and |y| to the affine value of -// |point|. It returns one on success and zero otherwise. |ctx| is ignored and +// EC_POINT_get_affine_coordinates_GFp sets `x` and `y` to the affine value of +// `point`. It returns one on success and zero otherwise. `ctx` is ignored and // may be NULL. // -// Either |x| or |y| may be NULL to skip computing that coordinate. This is +// Either `x` or `y` may be NULL to skip computing that coordinate. This is // slightly faster in the common case where only the x-coordinate is needed. OPENSSL_EXPORT int EC_POINT_get_affine_coordinates_GFp(const EC_GROUP *group, const EC_POINT *point, @@ -193,21 +193,21 @@ OPENSSL_EXPORT int EC_POINT_get_affine_coordinates_GFp(const EC_GROUP *group, BN_CTX *ctx); // EC_POINT_get_affine_coordinates is an alias of -// |EC_POINT_get_affine_coordinates_GFp|. +// `EC_POINT_get_affine_coordinates_GFp`. OPENSSL_EXPORT int EC_POINT_get_affine_coordinates(const EC_GROUP *group, const EC_POINT *point, BIGNUM *x, BIGNUM *y, BN_CTX *ctx); -// EC_POINT_set_affine_coordinates_GFp sets the value of |point| to be -// (|x|, |y|). |ctx| is ignored and may be NULL. It returns one on success or +// EC_POINT_set_affine_coordinates_GFp sets the value of `point` to be +// (`x`, `y`). `ctx` is ignored and may be NULL. It returns one on success or // zero on error. It's considered an error if the point is not on the curve. // // Note that the corresponding function in OpenSSL versions prior to 1.0.2s does // not check if the point is on the curve. This is a security-critical check, so // code additionally supporting OpenSSL should repeat the check with -// |EC_POINT_is_on_curve| or check for older OpenSSL versions with -// |OPENSSL_VERSION_NUMBER|. +// `EC_POINT_is_on_curve` or check for older OpenSSL versions with +// `OPENSSL_VERSION_NUMBER`. OPENSSL_EXPORT int EC_POINT_set_affine_coordinates_GFp(const EC_GROUP *group, EC_POINT *point, const BIGNUM *x, @@ -215,51 +215,51 @@ OPENSSL_EXPORT int EC_POINT_set_affine_coordinates_GFp(const EC_GROUP *group, BN_CTX *ctx); // EC_POINT_set_affine_coordinates is an alias of -// |EC_POINT_set_affine_coordinates_GFp|. +// `EC_POINT_set_affine_coordinates_GFp`. OPENSSL_EXPORT int EC_POINT_set_affine_coordinates(const EC_GROUP *group, EC_POINT *point, const BIGNUM *x, const BIGNUM *y, BN_CTX *ctx); -// EC_POINT_point2oct serialises |point| into the X9.62 form given by |form| -// into, at most, |max_out| bytes at |buf|. It returns the number of bytes -// written or zero on error if |buf| is non-NULL, else the number of bytes -// needed. |ctx| is ignored and may be NULL. +// EC_POINT_point2oct serialises `point` into the X9.62 form given by `form` +// into, at most, `max_out` bytes at `buf`. It returns the number of bytes +// written or zero on error if `buf` is non-NULL, else the number of bytes +// needed. `ctx` is ignored and may be NULL. OPENSSL_EXPORT size_t EC_POINT_point2oct(const EC_GROUP *group, const EC_POINT *point, point_conversion_form_t form, uint8_t *buf, size_t max_out, BN_CTX *ctx); -// EC_POINT_point2buf serialises |point| into the X9.62 form given by |form| to -// a newly-allocated buffer and sets |*out_buf| to point to it. It returns the +// EC_POINT_point2buf serialises `point` into the X9.62 form given by `form` to +// a newly-allocated buffer and sets `*out_buf` to point to it. It returns the // length of the result on success or zero on error. The caller must release -// |*out_buf| with |OPENSSL_free| when done. |ctx| is ignored and may be NULL. +// `*out_buf` with `OPENSSL_free` when done. `ctx` is ignored and may be NULL. OPENSSL_EXPORT size_t EC_POINT_point2buf(const EC_GROUP *group, const EC_POINT *point, point_conversion_form_t form, uint8_t **out_buf, BN_CTX *ctx); -// EC_POINT_point2cbb behaves like |EC_POINT_point2oct| but appends the -// serialised point to |cbb|. It returns one on success and zero on error. |ctx| +// EC_POINT_point2cbb behaves like `EC_POINT_point2oct` but appends the +// serialised point to `cbb`. It returns one on success and zero on error. `ctx` // is ignored and may be NULL. OPENSSL_EXPORT int EC_POINT_point2cbb(CBB *out, const EC_GROUP *group, const EC_POINT *point, point_conversion_form_t form, BN_CTX *ctx); -// EC_POINT_oct2point sets |point| from |len| bytes of X9.62 format -// serialisation in |buf|. It returns one on success and zero on error. |ctx| -// may be NULL. It's considered an error if |buf| does not represent a point on +// EC_POINT_oct2point sets `point` from `len` bytes of X9.62 format +// serialisation in `buf`. It returns one on success and zero on error. `ctx` +// may be NULL. It's considered an error if `buf` does not represent a point on // the curve. OPENSSL_EXPORT int EC_POINT_oct2point(const EC_GROUP *group, EC_POINT *point, const uint8_t *buf, size_t len, BN_CTX *ctx); -// EC_POINT_set_compressed_coordinates_GFp sets |point| to equal the point with -// the given |x| coordinate and the y coordinate specified by |y_bit| (see -// X9.62). It returns one on success and zero otherwise. |ctx| may be NULL. +// EC_POINT_set_compressed_coordinates_GFp sets `point` to equal the point with +// the given `x` coordinate and the y coordinate specified by `y_bit` (see +// X9.62). It returns one on success and zero otherwise. `ctx` may be NULL. OPENSSL_EXPORT int EC_POINT_set_compressed_coordinates_GFp( const EC_GROUP *group, EC_POINT *point, const BIGNUM *x, int y_bit, BN_CTX *ctx); @@ -267,24 +267,24 @@ OPENSSL_EXPORT int EC_POINT_set_compressed_coordinates_GFp( // Group operations. -// EC_POINT_add sets |r| equal to |a| plus |b|. It returns one on success and -// zero otherwise. |ctx| is ignored and may be NULL. +// EC_POINT_add sets `r` equal to `a` plus `b`. It returns one on success and +// zero otherwise. `ctx` is ignored and may be NULL. OPENSSL_EXPORT int EC_POINT_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a, const EC_POINT *b, BN_CTX *ctx); -// EC_POINT_dbl sets |r| equal to |a| plus |a|. It returns one on success and -// zero otherwise. |ctx| is ignored and may be NULL. +// EC_POINT_dbl sets `r` equal to `a` plus `a`. It returns one on success and +// zero otherwise. `ctx` is ignored and may be NULL. OPENSSL_EXPORT int EC_POINT_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a, BN_CTX *ctx); -// EC_POINT_invert sets |a| equal to minus |a|. It returns one on success and -// zero otherwise. |ctx| is ignored and may be NULL. +// EC_POINT_invert sets `a` equal to minus `a`. It returns one on success and +// zero otherwise. `ctx` is ignored and may be NULL. OPENSSL_EXPORT int EC_POINT_invert(const EC_GROUP *group, EC_POINT *a, BN_CTX *ctx); // EC_POINT_mul sets r = generator*n + q*m. It returns one on success and zero -// otherwise. |ctx| may be NULL. +// otherwise. `ctx` may be NULL. OPENSSL_EXPORT int EC_POINT_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *n, const EC_POINT *q, const BIGNUM *m, BN_CTX *ctx); @@ -292,108 +292,142 @@ OPENSSL_EXPORT int EC_POINT_mul(const EC_GROUP *group, EC_POINT *r, // Hash-to-curve. // -// The following functions implement primitives from RFC 9380. The |dst| +// The following functions implement primitives from RFC 9380. The `dst` // parameter in each function is the domain separation tag and must be unique -// for each protocol and between the |hash_to_curve| and |hash_to_scalar| +// for each protocol and between the `hash_to_curve` and `hash_to_scalar` // variants. See section 3.1 of the spec for additional guidance on this // parameter. -// EC_hash_to_curve_p256_xmd_sha256_sswu hashes |msg| to a point on |group| and -// writes the result to |out|, implementing the P256_XMD:SHA-256_SSWU_RO_ suite +// EC_hash_to_curve_p256_xmd_sha256_sswu hashes `msg` to a point on `group` and +// writes the result to `out`, implementing the P256_XMD:SHA-256_SSWU_RO_ suite // from RFC 9380. It returns one on success and zero on error. OPENSSL_EXPORT int EC_hash_to_curve_p256_xmd_sha256_sswu( const EC_GROUP *group, EC_POINT *out, const uint8_t *dst, size_t dst_len, const uint8_t *msg, size_t msg_len); -// EC_hash_to_curve_p384_xmd_sha384_sswu hashes |msg| to a point on |group| and -// writes the result to |out|, implementing the P384_XMD:SHA-384_SSWU_RO_ suite +// EC_hash_to_curve_p384_xmd_sha384_sswu hashes `msg` to a point on `group` and +// writes the result to `out`, implementing the P384_XMD:SHA-384_SSWU_RO_ suite // from RFC 9380. It returns one on success and zero on error. OPENSSL_EXPORT int EC_hash_to_curve_p384_xmd_sha384_sswu( const EC_GROUP *group, EC_POINT *out, const uint8_t *dst, size_t dst_len, const uint8_t *msg, size_t msg_len); -// EC_encode_to_curve_p256_xmd_sha256_sswu hashes |msg| to a point on |group| -// and writes the result to |out|, implementing the P256_XMD:SHA-256_SSWU_NU_ +// EC_encode_to_curve_p256_xmd_sha256_sswu hashes `msg` to a point on `group` +// and writes the result to `out`, implementing the P256_XMD:SHA-256_SSWU_NU_ // suite from RFC 9380. It returns one on success and zero on error. // -// This is a nonuniform encoding from byte strings to points in |group|. That -// is, the set of possible outputs is only a fraction of the points in |group|, +// This is a nonuniform encoding from byte strings to points in `group`. That +// is, the set of possible outputs is only a fraction of the points in `group`, // and some points in this set are more likely to be output than others. See // also RFC 9380, section 10.4 OPENSSL_EXPORT int EC_encode_to_curve_p256_xmd_sha256_sswu( const EC_GROUP *group, EC_POINT *out, const uint8_t *dst, size_t dst_len, const uint8_t *msg, size_t msg_len); -// EC_encode_to_curve_p384_xmd_sha384_sswu hashes |msg| to a point on |group| -// and writes the result to |out|, implementing the P384_XMD:SHA-384_SSWU_NU_ +// EC_encode_to_curve_p384_xmd_sha384_sswu hashes `msg` to a point on `group` +// and writes the result to `out`, implementing the P384_XMD:SHA-384_SSWU_NU_ // suite from RFC 9380. It returns one on success and zero on error. // -// This is a nonuniform encoding from byte strings to points in |group|. That -// is, the set of possible outputs is only a fraction of the points in |group|, +// This is a nonuniform encoding from byte strings to points in `group`. That +// is, the set of possible outputs is only a fraction of the points in `group`, // and some points in this set are more likely to be output than others. See // also RFC 9380, section 10.4 OPENSSL_EXPORT int EC_encode_to_curve_p384_xmd_sha384_sswu( const EC_GROUP *group, EC_POINT *out, const uint8_t *dst, size_t dst_len, const uint8_t *msg, size_t msg_len); +// EC_wpa3_sae_hash_to_curve_p256 hashes `salt` and `ikm` to a point on `group` +// and writes the result to `out`, implementing the WPA3 SAE hash-to-curve +// variant for P-256 defined in IEEE Std 802.11-2020, Section 12.4.4.2.3. It +// returns one on success and zero on error. `salt` is the HKDF-Extract salt, +// which is the SSID. `ikm` is the HKDF-Extract input, or password and +// identifier concatenation. +// +// This generates the PT value. The PWE value still requires multiplication by +// the hashed MAC addresses. +OPENSSL_EXPORT int EC_wpa3_sae_hash_to_curve_p256( + const EC_GROUP *group, EC_POINT *out, const uint8_t *salt, size_t salt_len, + const uint8_t *ikm, size_t ikm_len); + +// EC_wpa3_sae_hunt_and_peck_p256 hashes `salt` and `password` to a point on +// `group` and writes the result to `out`, using the WPA3 "hunt and peck" +// algorithm defined in IEEE Std 802.11-2020, Section 12.4.4.2.2. It returns one +// on success and zero on error. `group` must be P-256. `salt` is the +// HKDF-Extract salt, which is the MAC concatenation. `password` is the +// password, used to compute the HKDF-Extract input. +// +// Unlike a proper hash-to-curve construction, this algorithm is based on +// rejection sampling and cannot run in time independent of `in`. To reduce +// information leakage, the number of iterations is blinded up to +// `min_iterations`. If at most `min_iterations` are needed to find a point, +// only this fact is leaked. However, past `min_iterations`, the number of +// iterations used may be leaked as a side channel. +// +// Each iteration has a roughly 50% probability of finding an output. For +// example, a 64 iteration minimum reduces the chance of leak to 2^-64. However, +// running time scales linearly with `min_iterations`. +OPENSSL_EXPORT int EC_wpa3_sae_hunt_and_peck_p256( + const EC_GROUP *group, EC_POINT *out, const uint8_t *salt, size_t salt_len, + const uint8_t *password, size_t password_len, uint8_t min_iterations); + // Deprecated functions. -// EC_GROUP_free releases a reference to |group|, if |group| was created by -// |EC_GROUP_new_curve_GFp|. If |group| is static, it does nothing. +// EC_GROUP_free releases a reference to `group`, if `group` was created by +// `EC_GROUP_new_curve_GFp`. If `group` is static, it does nothing. // // This function exists for OpenSSL compatibility, and to manage dynamic -// |EC_GROUP|s constructed by |EC_GROUP_new_curve_GFp|. Callers that do not need +// `EC_GROUP`s constructed by `EC_GROUP_new_curve_GFp`. Callers that do not need // either may ignore this function. OPENSSL_EXPORT void EC_GROUP_free(EC_GROUP *group); -// EC_GROUP_dup increments |group|'s reference count and returns it, if |group| -// was created by |EC_GROUP_new_curve_GFp|. If |group| is static, it simply -// returns |group|. +// EC_GROUP_dup increments `group`'s reference count and returns it, if `group` +// was created by `EC_GROUP_new_curve_GFp`. If `group` is static, it simply +// returns `group`. // // This function exists for OpenSSL compatibility, and to manage dynamic -// |EC_GROUP|s constructed by |EC_GROUP_new_curve_GFp|. Callers that do not need +// `EC_GROUP`s constructed by `EC_GROUP_new_curve_GFp`. Callers that do not need // either may ignore this function. OPENSSL_EXPORT EC_GROUP *EC_GROUP_dup(const EC_GROUP *group); // EC_GROUP_new_curve_GFp creates a new, arbitrary elliptic curve group based // on the equation y² = x³ + a·x + b. It returns the new group or NULL on // error. The lifetime of the resulting object must be managed with -// |EC_GROUP_dup| and |EC_GROUP_free|. +// `EC_GROUP_dup` and `EC_GROUP_free`. // // This new group has no generator. It is an error to use a generator-less group -// with any functions except for |EC_GROUP_free|, |EC_POINT_new|, -// |EC_POINT_set_affine_coordinates_GFp|, and |EC_GROUP_set_generator|. +// with any functions except for `EC_GROUP_free`, `EC_POINT_new`, +// `EC_POINT_set_affine_coordinates_GFp`, and `EC_GROUP_set_generator`. // -// |EC_GROUP|s returned by this function will always compare as unequal via -// |EC_GROUP_cmp| (even to themselves). |EC_GROUP_get_curve_name| will always -// return |NID_undef|. +// `EC_GROUP`s returned by this function will always compare as unequal via +// `EC_GROUP_cmp` (even to themselves). `EC_GROUP_get_curve_name` will always +// return `NID_undef`. // // This function is provided for compatibility with some legacy applications -// only. Avoid using arbitrary curves and use |EC_GROUP_new_by_curve_name| +// only. Avoid using arbitrary curves and use `EC_GROUP_new_by_curve_name` // instead. This ensures the result meets preconditions necessary for // elliptic curve algorithms to function correctly and securely. // // Given invalid parameters, this function may fail or it may return an -// |EC_GROUP| which breaks these preconditions. Subsequent operations may then +// `EC_GROUP` which breaks these preconditions. Subsequent operations may then // return arbitrary, incorrect values. Callers should not pass // attacker-controlled values to this function. OPENSSL_EXPORT EC_GROUP *EC_GROUP_new_curve_GFp(const BIGNUM *p, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx); -// EC_GROUP_set_generator sets the generator for |group| to |generator|, which -// must have the given order and cofactor. It may only be used with |EC_GROUP| -// objects returned by |EC_GROUP_new_curve_GFp| and may only be used once on -// each group. |generator| must have been created using |group|. +// EC_GROUP_set_generator sets the generator for `group` to `generator`, which +// must have the given order and cofactor. It may only be used with `EC_GROUP` +// objects returned by `EC_GROUP_new_curve_GFp` and may only be used once on +// each group. `generator` must have been created using `group`. OPENSSL_EXPORT int EC_GROUP_set_generator(EC_GROUP *group, const EC_POINT *generator, const BIGNUM *order, const BIGNUM *cofactor); -// EC_GROUP_get_order sets |*order| to the order of |group|, if it's not -// NULL. It returns one on success and zero otherwise. |ctx| is ignored and may -// be NULL. Use |EC_GROUP_get0_order| instead. +// EC_GROUP_get_order sets `*order` to the order of `group`, if it's not +// NULL. It returns one on success and zero otherwise. `ctx` is ignored and may +// be NULL. Use `EC_GROUP_get0_order` instead. OPENSSL_EXPORT int EC_GROUP_get_order(const EC_GROUP *group, BIGNUM *order, BN_CTX *ctx); @@ -403,7 +437,7 @@ OPENSSL_EXPORT int EC_GROUP_get_order(const EC_GROUP *group, BIGNUM *order, // EC_GROUP_set_asn1_flag does nothing. OPENSSL_EXPORT void EC_GROUP_set_asn1_flag(EC_GROUP *group, int flag); -// EC_GROUP_get_asn1_flag returns |OPENSSL_EC_NAMED_CURVE|. +// EC_GROUP_get_asn1_flag returns `OPENSSL_EC_NAMED_CURVE`. OPENSSL_EXPORT int EC_GROUP_get_asn1_flag(const EC_GROUP *group); typedef struct ec_method_st EC_METHOD; @@ -414,8 +448,8 @@ OPENSSL_EXPORT const EC_METHOD *EC_GROUP_method_of(const EC_GROUP *group); // EC_METHOD_get_field_type returns NID_X9_62_prime_field. OPENSSL_EXPORT int EC_METHOD_get_field_type(const EC_METHOD *meth); -// EC_GROUP_set_point_conversion_form aborts the process if |form| is not -// |POINT_CONVERSION_UNCOMPRESSED| and otherwise does nothing. +// EC_GROUP_set_point_conversion_form aborts the process if `form` is not +// `POINT_CONVERSION_UNCOMPRESSED` and otherwise does nothing. OPENSSL_EXPORT void EC_GROUP_set_point_conversion_form( EC_GROUP *group, point_conversion_form_t form); @@ -425,15 +459,15 @@ typedef struct { const char *comment; } EC_builtin_curve; -// EC_get_builtin_curves writes at most |max_num_curves| elements to -// |out_curves| and returns the total number that it would have written, had -// |max_num_curves| been large enough. +// EC_get_builtin_curves writes at most `max_num_curves` elements to +// `out_curves` and returns the total number that it would have written, had +// `max_num_curves` been large enough. // -// The |EC_builtin_curve| items describe the supported elliptic curves. +// The `EC_builtin_curve` items describe the supported elliptic curves. OPENSSL_EXPORT size_t EC_get_builtin_curves(EC_builtin_curve *out_curves, size_t max_num_curves); -// EC_POINT_clear_free calls |EC_POINT_free|. +// EC_POINT_clear_free calls `EC_POINT_free`. OPENSSL_EXPORT void EC_POINT_clear_free(EC_POINT *point); diff --git a/third_party/boringssl/src/include/openssl/ec_key.h b/third_party/boringssl/src/include/openssl/ec_key.h index 47d50636c..296b7c0f8 100644 --- a/third_party/boringssl/src/include/openssl/ec_key.h +++ b/third_party/boringssl/src/include/openssl/ec_key.h @@ -16,7 +16,7 @@ #ifndef OPENSSL_HEADER_EC_KEY_H #define OPENSSL_HEADER_EC_KEY_H -#include // IWYU pragma: export +#include // IWYU pragma: export #include #include @@ -33,83 +33,88 @@ extern "C" { // EC key objects. // -// An |EC_KEY| object represents a public or private EC key. A given object may +// An `EC_KEY` object represents a public or private EC key. A given object may // be used concurrently on multiple threads by non-mutating functions, provided // no other thread is concurrently calling a mutating function. Unless otherwise -// documented, functions which take a |const| pointer are non-mutating and -// functions which take a non-|const| pointer are mutating. +// documented, functions which take a `const` pointer are non-mutating and +// functions which take a non-`const` pointer are mutating. -// EC_KEY_new returns a fresh |EC_KEY| object or NULL on error. +// EC_KEY_new returns a fresh `EC_KEY` object or NULL on error. OPENSSL_EXPORT EC_KEY *EC_KEY_new(void); -// EC_KEY_new_method acts the same as |EC_KEY_new|, but takes an explicit -// |ENGINE|. +// EC_KEY_new_method acts the same as `EC_KEY_new`, but takes an explicit +// `ENGINE`. OPENSSL_EXPORT EC_KEY *EC_KEY_new_method(const ENGINE *engine); -// EC_KEY_new_by_curve_name returns a fresh EC_KEY for group specified by |nid| +// EC_KEY_new_by_curve_name returns a fresh EC_KEY for group specified by `nid` // or NULL on error. OPENSSL_EXPORT EC_KEY *EC_KEY_new_by_curve_name(int nid); -// EC_KEY_free frees all the data owned by |key| and |key| itself. +// EC_KEY_free frees all the data owned by `key` and `key` itself. OPENSSL_EXPORT void EC_KEY_free(EC_KEY *key); -// EC_KEY_dup returns a fresh copy of |src| or NULL on error. +// EC_KEY_dup returns a fresh copy of `src` or NULL on error. OPENSSL_EXPORT EC_KEY *EC_KEY_dup(const EC_KEY *src); -// EC_KEY_up_ref increases the reference count of |key| and returns one. It does -// not mutate |key| for thread-safety purposes and may be used concurrently. +// EC_KEY_up_ref increases the reference count of `key` and returns one. It does +// not mutate `key` for thread-safety purposes and may be used concurrently. OPENSSL_EXPORT int EC_KEY_up_ref(EC_KEY *key); -// EC_KEY_is_opaque returns one if |key| is opaque and doesn't expose its key +// EC_KEY_dup_ref increases the reference count of `key` and returns the same +// handle. It does not mutate `key` for thread-safety purposes and may be used +// concurrently. +OPENSSL_EXPORT EC_KEY *EC_KEY_dup_ref(const EC_KEY *key); + +// EC_KEY_is_opaque returns one if `key` is opaque and doesn't expose its key // material. Otherwise it return zero. OPENSSL_EXPORT int EC_KEY_is_opaque(const EC_KEY *key); -// EC_KEY_get0_group returns a pointer to the |EC_GROUP| object inside |key|. +// EC_KEY_get0_group returns a pointer to the `EC_GROUP` object inside `key`. OPENSSL_EXPORT const EC_GROUP *EC_KEY_get0_group(const EC_KEY *key); -// EC_KEY_set_group sets the |EC_GROUP| object that |key| will use to |group|. -// It returns one on success and zero if |key| is already configured with a +// EC_KEY_set_group sets the `EC_GROUP` object that `key` will use to `group`. +// It returns one on success and zero if `key` is already configured with a // different group. OPENSSL_EXPORT int EC_KEY_set_group(EC_KEY *key, const EC_GROUP *group); -// EC_KEY_get0_private_key returns a pointer to the private key inside |key|. +// EC_KEY_get0_private_key returns a pointer to the private key inside `key`. OPENSSL_EXPORT const BIGNUM *EC_KEY_get0_private_key(const EC_KEY *key); -// EC_KEY_set_private_key sets the private key of |key| to |priv|. It returns -// one on success and zero otherwise. |key| must already have had a group -// configured (see |EC_KEY_set_group| and |EC_KEY_new_by_curve_name|). +// EC_KEY_set_private_key sets the private key of `key` to `priv`. It returns +// one on success and zero otherwise. `key` must already have had a group +// configured (see `EC_KEY_set_group` and `EC_KEY_new_by_curve_name`). OPENSSL_EXPORT int EC_KEY_set_private_key(EC_KEY *key, const BIGNUM *priv); // EC_KEY_get0_public_key returns a pointer to the public key point inside -// |key|. +// `key`. OPENSSL_EXPORT const EC_POINT *EC_KEY_get0_public_key(const EC_KEY *key); -// EC_KEY_set_public_key sets the public key of |key| to |pub|, by copying it. -// It returns one on success and zero otherwise. |key| must already have had a -// group configured (see |EC_KEY_set_group| and |EC_KEY_new_by_curve_name|), and -// |pub| must also belong to that group, and must not be the point at infinity. +// EC_KEY_set_public_key sets the public key of `key` to `pub`, by copying it. +// It returns one on success and zero otherwise. `key` must already have had a +// group configured (see `EC_KEY_set_group` and `EC_KEY_new_by_curve_name`), and +// `pub` must also belong to that group, and must not be the point at infinity. OPENSSL_EXPORT int EC_KEY_set_public_key(EC_KEY *key, const EC_POINT *pub); #define EC_PKEY_NO_PARAMETERS 0x001 #define EC_PKEY_NO_PUBKEY 0x002 -// EC_KEY_get_enc_flags returns the encoding flags for |key|, which is a -// bitwise-OR of |EC_PKEY_*| values. +// EC_KEY_get_enc_flags returns the encoding flags for `key`, which is a +// bitwise-OR of `EC_PKEY_*` values. OPENSSL_EXPORT unsigned EC_KEY_get_enc_flags(const EC_KEY *key); -// EC_KEY_set_enc_flags sets the encoding flags for |key|, which is a -// bitwise-OR of |EC_PKEY_*| values. +// EC_KEY_set_enc_flags sets the encoding flags for `key`, which is a +// bitwise-OR of `EC_PKEY_*` values. OPENSSL_EXPORT void EC_KEY_set_enc_flags(EC_KEY *key, unsigned flags); -// EC_KEY_get_conv_form returns the conversation form that will be used by -// |key|. +// EC_KEY_get_conv_form returns the conversion form that will be used by +// `key`. OPENSSL_EXPORT point_conversion_form_t EC_KEY_get_conv_form(const EC_KEY *key); -// EC_KEY_set_conv_form sets the conversion form to be used by |key|. +// EC_KEY_set_conv_form sets the conversion form to be used by `key`. OPENSSL_EXPORT void EC_KEY_set_conv_form(EC_KEY *key, point_conversion_form_t cform); -// EC_KEY_check_key performs several checks on |key| (possibly including an +// EC_KEY_check_key performs several checks on `key` (possibly including an // expensive check that the public key is in the primary subgroup). It returns // one if all checks pass and zero otherwise. If it returns zero then detail // about the problem can be found on the error stack. @@ -120,70 +125,70 @@ OPENSSL_EXPORT int EC_KEY_check_key(const EC_KEY *key); // 5.6.2.1.4. It returns one if it passes and zero otherwise. OPENSSL_EXPORT int EC_KEY_check_fips(const EC_KEY *key); -// EC_KEY_set_public_key_affine_coordinates sets the public key in |key| to -// (|x|, |y|). It returns one on success and zero on error. It's considered an -// error if |x| and |y| do not represent a point on |key|'s curve. +// EC_KEY_set_public_key_affine_coordinates sets the public key in `key` to +// (`x`, `y`). It returns one on success and zero on error. It's considered an +// error if `x` and `y` do not represent a point on `key`'s curve. OPENSSL_EXPORT int EC_KEY_set_public_key_affine_coordinates(EC_KEY *key, const BIGNUM *x, const BIGNUM *y); -// EC_KEY_oct2key decodes |len| bytes from |in| as an EC public key in X9.62 -// form. |key| must already have a group configured. On success, it sets the -// public key in |key| to the result and returns one. Otherwise, it returns -// zero. |ctx| may be NULL. +// EC_KEY_oct2key decodes `len` bytes from `in` as an EC public key in X9.62 +// form. `key` must already have a group configured. On success, it sets the +// public key in `key` to the result and returns one. Otherwise, it returns +// zero. `ctx` may be NULL. OPENSSL_EXPORT int EC_KEY_oct2key(EC_KEY *key, const uint8_t *in, size_t len, BN_CTX *ctx); -// EC_KEY_key2buf behaves like |EC_POINT_point2buf|, except it encodes the -// public key in |key|. |ctx| is ignored and may be NULL. +// EC_KEY_key2buf behaves like `EC_POINT_point2buf`, except it encodes the +// public key in `key`. `ctx` is ignored and may be NULL. OPENSSL_EXPORT size_t EC_KEY_key2buf(const EC_KEY *key, point_conversion_form_t form, uint8_t **out_buf, BN_CTX *ctx); -// EC_KEY_oct2priv decodes a big-endian, zero-padded integer from |len| bytes -// from |in| and sets |key|'s private key to the result. It returns one on -// success and zero on error. The input must be padded to the size of |key|'s +// EC_KEY_oct2priv decodes a big-endian, zero-padded integer from `len` bytes +// from `in` and sets `key`'s private key to the result. It returns one on +// success and zero on error. The input must be padded to the size of `key`'s // group order. OPENSSL_EXPORT int EC_KEY_oct2priv(EC_KEY *key, const uint8_t *in, size_t len); -// EC_KEY_priv2oct serializes |key|'s private key as a big-endian integer, -// zero-padded to the size of |key|'s group order and writes the result to at -// most |max_out| bytes of |out|. It returns the number of bytes written on -// success and zero on error. If |out| is NULL, it returns the number of bytes +// EC_KEY_priv2oct serializes `key`'s private key as a big-endian integer, +// zero-padded to the size of `key`'s group order and writes the result to at +// most `max_out` bytes of `out`. It returns the number of bytes written on +// success and zero on error. If `out` is NULL, it returns the number of bytes // needed without writing anything. OPENSSL_EXPORT size_t EC_KEY_priv2oct(const EC_KEY *key, uint8_t *out, size_t max_out); -// EC_KEY_priv2buf behaves like |EC_KEY_priv2oct| but sets |*out_buf| to a +// EC_KEY_priv2buf behaves like `EC_KEY_priv2oct` but sets `*out_buf` to a // newly-allocated buffer containing the result. It returns the size of the -// result on success and zero on error. The caller must release |*out_buf| with -// |OPENSSL_free| when done. +// result on success and zero on error. The caller must release `*out_buf` with +// `OPENSSL_free` when done. OPENSSL_EXPORT size_t EC_KEY_priv2buf(const EC_KEY *key, uint8_t **out_buf); // Key generation. // EC_KEY_generate_key generates a random, private key, calculates the -// corresponding public key and stores both in |key|. It returns one on success +// corresponding public key and stores both in `key`. It returns one on success // or zero otherwise. OPENSSL_EXPORT int EC_KEY_generate_key(EC_KEY *key); -// EC_KEY_generate_key_fips behaves like |EC_KEY_generate_key| but performs +// EC_KEY_generate_key_fips behaves like `EC_KEY_generate_key` but performs // additional checks for FIPS compliance. This function is applicable when // generating keys for either signing/verification or key agreement because // both types of consistency check (PCT) are performed. OPENSSL_EXPORT int EC_KEY_generate_key_fips(EC_KEY *key); -// EC_KEY_derive_from_secret deterministically derives a private key for |group| -// from an input secret using HKDF-SHA256. It returns a newly-allocated |EC_KEY| -// on success or NULL on error. |secret| must not be used in any other +// EC_KEY_derive_from_secret deterministically derives a private key for `group` +// from an input secret using HKDF-SHA256. It returns a newly-allocated `EC_KEY` +// on success or NULL on error. `secret` must not be used in any other // algorithm. If using a base secret for multiple operations, derive separate // values with a KDF such as HKDF first. // // Note this function implements an arbitrary derivation scheme, rather than any // particular standard one. New protocols are recommended to use X25519 and // Ed25519, which have standard byte import functions. See -// |X25519_public_from_private| and |ED25519_keypair_from_seed|. +// `X25519_public_from_private` and `ED25519_keypair_from_seed`. OPENSSL_EXPORT EC_KEY *EC_KEY_derive_from_secret(const EC_GROUP *group, const uint8_t *secret, size_t secret_len); @@ -192,45 +197,45 @@ OPENSSL_EXPORT EC_KEY *EC_KEY_derive_from_secret(const EC_GROUP *group, // Serialisation. // EC_KEY_parse_private_key parses a DER-encoded ECPrivateKey structure (RFC -// 5915) from |cbs| and advances |cbs|. It returns a newly-allocated |EC_KEY| or -// NULL on error. If |group| is non-null, the parameters field of the -// ECPrivateKey may be omitted (but must match |group| if present). Otherwise, +// 5915) from `cbs` and advances `cbs`. It returns a newly-allocated `EC_KEY` or +// NULL on error. If `group` is non-null, the parameters field of the +// ECPrivateKey may be omitted (but must match `group` if present). Otherwise, // the parameters field is required. OPENSSL_EXPORT EC_KEY *EC_KEY_parse_private_key(CBS *cbs, const EC_GROUP *group); -// EC_KEY_marshal_private_key marshals |key| as a DER-encoded ECPrivateKey -// structure (RFC 5915) and appends the result to |cbb|. It returns one on -// success and zero on failure. |enc_flags| is a combination of |EC_PKEY_*| +// EC_KEY_marshal_private_key marshals `key` as a DER-encoded ECPrivateKey +// structure (RFC 5915) and appends the result to `cbb`. It returns one on +// success and zero on failure. `enc_flags` is a combination of `EC_PKEY_*` // values and controls whether corresponding fields are omitted. OPENSSL_EXPORT int EC_KEY_marshal_private_key(CBB *cbb, const EC_KEY *key, unsigned enc_flags); // EC_KEY_parse_curve_name parses a DER-encoded OBJECT IDENTIFIER as a curve -// name from |cbs| and advances |cbs|. It returns the decoded |EC_GROUP| or NULL +// name from `cbs` and advances `cbs`. It returns the decoded `EC_GROUP` or NULL // on error. // // This function returns a non-const pointer which may be passed to -// |EC_GROUP_free|. However, the resulting object is actually static and calling -// |EC_GROUP_free| is optional. +// `EC_GROUP_free`. However, the resulting object is actually static and calling +// `EC_GROUP_free` is optional. // // TODO(davidben): Make this return a const pointer, if it does not break too // many callers. OPENSSL_EXPORT EC_GROUP *EC_KEY_parse_curve_name(CBS *cbs); -// EC_KEY_marshal_curve_name marshals |group| as a DER-encoded OBJECT IDENTIFIER -// and appends the result to |cbb|. It returns one on success and zero on +// EC_KEY_marshal_curve_name marshals `group` as a DER-encoded OBJECT IDENTIFIER +// and appends the result to `cbb`. It returns one on success and zero on // failure. OPENSSL_EXPORT int EC_KEY_marshal_curve_name(CBB *cbb, const EC_GROUP *group); // EC_KEY_parse_parameters parses a DER-encoded ECParameters structure (RFC -// 5480) from |cbs| and advances |cbs|. It returns the resulting |EC_GROUP| or +// 5480) from `cbs` and advances `cbs`. It returns the resulting `EC_GROUP` or // NULL on error. It supports the namedCurve and specifiedCurve options, but use -// of specifiedCurve is deprecated. Use |EC_KEY_parse_curve_name| instead. +// of specifiedCurve is deprecated. Use `EC_KEY_parse_curve_name` instead. // // This function returns a non-const pointer which may be passed to -// |EC_GROUP_free|. However, the resulting object is actually static and calling -// |EC_GROUP_free| is optional. +// `EC_GROUP_free`. However, the resulting object is actually static and calling +// `EC_GROUP_free` is optional. // // TODO(davidben): Make this return a const pointer, if it does not break too // many callers. @@ -239,7 +244,7 @@ OPENSSL_EXPORT EC_GROUP *EC_KEY_parse_parameters(CBS *cbs); // ex_data functions. // -// These functions are wrappers. See |ex_data.h| for details. +// These functions are wrappers. See `ex_data.h` for details. OPENSSL_EXPORT int EC_KEY_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused, @@ -266,7 +271,7 @@ struct ecdsa_method_st { int (*init)(EC_KEY *key); int (*finish)(EC_KEY *key); - // sign matches the arguments and behaviour of |ECDSA_sign|. + // sign matches the arguments and behaviour of `ECDSA_sign`. int (*sign)(const uint8_t *digest, size_t digest_len, uint8_t *sig, unsigned int *sig_len, EC_KEY *eckey); @@ -280,62 +285,62 @@ struct ecdsa_method_st { OPENSSL_EXPORT void EC_KEY_set_asn1_flag(EC_KEY *key, int flag); // d2i_ECPrivateKey parses a DER-encoded ECPrivateKey structure (RFC 5915) from -// |len| bytes at |*inp|, as described in |d2i_SAMPLE|. On input, if |*out_key| +// `len` bytes at `*inp`, as described in `d2i_SAMPLE`. On input, if `*out_key` // is non-NULL and has a group configured, the parameters field may be omitted // but must match that group if present. // -// Use |EC_KEY_parse_private_key| instead. +// Use `EC_KEY_parse_private_key` instead. OPENSSL_EXPORT EC_KEY *d2i_ECPrivateKey(EC_KEY **out_key, const uint8_t **inp, long len); -// i2d_ECPrivateKey marshals |key| as a DER-encoded ECPrivateKey structure (RFC -// 5915), as described in |i2d_SAMPLE|. +// i2d_ECPrivateKey marshals `key` as a DER-encoded ECPrivateKey structure (RFC +// 5915), as described in `i2d_SAMPLE`. // -// Use |EC_KEY_marshal_private_key| instead. +// Use `EC_KEY_marshal_private_key` instead. OPENSSL_EXPORT int i2d_ECPrivateKey(const EC_KEY *key, uint8_t **outp); // d2i_ECPKParameters parses a DER-encoded ECParameters structure (RFC 5480) -// from |len| bytes at |*inp|, as described in |d2i_SAMPLE|. For legacy reasons, +// from `len` bytes at `*inp`, as described in `d2i_SAMPLE`. For legacy reasons, // it recognizes the specifiedCurve form, but only for curves that are already // supported as named curves. // -// Use |EC_KEY_parse_parameters| or |EC_KEY_parse_curve_name| instead. +// Use `EC_KEY_parse_parameters` or `EC_KEY_parse_curve_name` instead. OPENSSL_EXPORT EC_GROUP *d2i_ECPKParameters(EC_GROUP **out, const uint8_t **inp, long len); -// i2d_ECPKParameters marshals |group| as a DER-encoded ECParameters structure -// (RFC 5480), as described in |i2d_SAMPLE|. +// i2d_ECPKParameters marshals `group` as a DER-encoded ECParameters structure +// (RFC 5480), as described in `i2d_SAMPLE`. // -// Use |EC_KEY_marshal_curve_name| instead. +// Use `EC_KEY_marshal_curve_name` instead. OPENSSL_EXPORT int i2d_ECPKParameters(const EC_GROUP *group, uint8_t **outp); // d2i_ECParameters parses a DER-encoded ECParameters structure (RFC 5480) from -// |len| bytes at |*inp|, as described in |d2i_SAMPLE|. It returns the result as -// an |EC_KEY| with parameters, but no key, configured. +// `len` bytes at `*inp`, as described in `d2i_SAMPLE`. It returns the result as +// an `EC_KEY` with parameters, but no key, configured. // -// Use |EC_KEY_parse_parameters| or |EC_KEY_parse_curve_name| instead. +// Use `EC_KEY_parse_parameters` or `EC_KEY_parse_curve_name` instead. OPENSSL_EXPORT EC_KEY *d2i_ECParameters(EC_KEY **out_key, const uint8_t **inp, long len); -// i2d_ECParameters marshals |key|'s parameters as a DER-encoded OBJECT -// IDENTIFIER, as described in |i2d_SAMPLE|. +// i2d_ECParameters marshals `key`'s parameters as a DER-encoded OBJECT +// IDENTIFIER, as described in `i2d_SAMPLE`. // -// Use |EC_KEY_marshal_curve_name| instead. +// Use `EC_KEY_marshal_curve_name` instead. OPENSSL_EXPORT int i2d_ECParameters(const EC_KEY *key, uint8_t **outp); -// o2i_ECPublicKey parses an EC point from |len| bytes at |*inp| into -// |*out_key|. Note that this differs from the d2i format in that |*out_key| -// must be non-NULL with a group set. On successful exit, |*inp| is advanced by -// |len| bytes. It returns |*out_key| or NULL on error. +// o2i_ECPublicKey parses an EC point from `len` bytes at `*inp` into +// `*out_key`. Note that this differs from the d2i format in that `*out_key` +// must be non-NULL with a group set. On successful exit, `*inp` is advanced by +// `len` bytes. It returns `*out_key` or NULL on error. // -// Use |EC_POINT_oct2point| instead. +// Use `EC_POINT_oct2point` instead. OPENSSL_EXPORT EC_KEY *o2i_ECPublicKey(EC_KEY **out_key, const uint8_t **inp, long len); -// i2o_ECPublicKey marshals an EC point from |key|, as described in -// |i2d_SAMPLE|, except it returns zero on error instead of a negative value. +// i2o_ECPublicKey marshals an EC point from `key`, as described in +// `i2d_SAMPLE`, except it returns zero on error instead of a negative value. // -// Use |EC_POINT_point2cbb| instead. +// Use `EC_POINT_point2cbb` instead. OPENSSL_EXPORT int i2o_ECPublicKey(const EC_KEY *key, unsigned char **outp); @@ -347,7 +352,7 @@ extern "C++" { BSSL_NAMESPACE_BEGIN BORINGSSL_MAKE_DELETER(EC_KEY, EC_KEY_free) -BORINGSSL_MAKE_UP_REF(EC_KEY, EC_KEY_up_ref) +BORINGSSL_MAKE_UP_REF(EC_KEY, EC_KEY_dup_ref) BSSL_NAMESPACE_END diff --git a/third_party/boringssl/src/include/openssl/ecdh.h b/third_party/boringssl/src/include/openssl/ecdh.h index 35a99e632..a1dff7b4f 100644 --- a/third_party/boringssl/src/include/openssl/ecdh.h +++ b/third_party/boringssl/src/include/openssl/ecdh.h @@ -28,22 +28,22 @@ extern "C" { // Elliptic curve Diffie-Hellman. -// ECDH_compute_key calculates the shared key between |pub_key| and |priv_key|. -// If |kdf| is not NULL, then it is called with the bytes of the shared key and -// the parameter |out|. When |kdf| returns, the value of |*outlen| becomes the +// ECDH_compute_key calculates the shared key between `pub_key` and `priv_key`. +// If `kdf` is not NULL, then it is called with the bytes of the shared key and +// the parameter `out`. When `kdf` returns, the value of `*outlen` becomes the // return value. Otherwise, as many bytes of the shared key as will fit are -// copied directly to, at most, |outlen| bytes at |out|. It returns the number -// of bytes written to |out|, or -1 on error. +// copied directly to, at most, `outlen` bytes at `out`. It returns the number +// of bytes written to `out`, or -1 on error. OPENSSL_EXPORT int ECDH_compute_key( void *out, size_t outlen, const EC_POINT *pub_key, const EC_KEY *priv_key, void *(*kdf)(const void *in, size_t inlen, void *out, size_t *outlen)); -// ECDH_compute_key_fips calculates the shared key between |pub_key| and -// |priv_key| and hashes it with the appropriate SHA function for |out_len|. The -// only value values for |out_len| are thus 24 (SHA-224), 32 (SHA-256), 48 +// ECDH_compute_key_fips calculates the shared key between `pub_key` and +// `priv_key` and hashes it with the appropriate SHA function for `out_len`. The +// only value values for `out_len` are thus 24 (SHA-224), 32 (SHA-256), 48 // (SHA-384), and 64 (SHA-512). It returns one on success and zero on error. // -// Note that the return value is different to |ECDH_compute_key|: it returns an +// Note that the return value is different to `ECDH_compute_key`: it returns an // error flag (as is common for BoringSSL) rather than the number of bytes // written. // diff --git a/third_party/boringssl/src/include/openssl/ecdsa.h b/third_party/boringssl/src/include/openssl/ecdsa.h index 3eee16fcb..5eee8090f 100644 --- a/third_party/boringssl/src/include/openssl/ecdsa.h +++ b/third_party/boringssl/src/include/openssl/ecdsa.h @@ -36,24 +36,24 @@ extern "C" { // format is variable-length. Callers must be prepared to receive signatures // that are slightly shorter than the maximum for the ECDSA curve. -// ECDSA_sign signs |digest_len| bytes from |digest| with |key| and writes the -// resulting ASN.1-based signature to |sig|, which must have |ECDSA_size(key)| -// bytes of space. On successful exit, |*sig_len| is set to the actual number of -// bytes written. The |type| argument should be zero. It returns one on success +// ECDSA_sign signs `digest_len` bytes from `digest` with `key` and writes the +// resulting ASN.1-based signature to `sig`, which must have `ECDSA_size(key)` +// bytes of space. On successful exit, `*sig_len` is set to the actual number of +// bytes written. The `type` argument should be zero. It returns one on success // and zero otherwise. // -// WARNING: |digest| must be the output of some hash function on the data to be +// WARNING: `digest` must be the output of some hash function on the data to be // signed. Passing unhashed inputs will not result in a secure signature scheme. OPENSSL_EXPORT int ECDSA_sign(int type, const uint8_t *digest, size_t digest_len, uint8_t *sig, unsigned int *sig_len, const EC_KEY *key); -// ECDSA_verify verifies that |sig_len| bytes from |sig| constitute a valid -// ASN.1-based signature by |key| of |digest|. (The |type| argument should be +// ECDSA_verify verifies that `sig_len` bytes from `sig` constitute a valid +// ASN.1-based signature by `key` of `digest`. (The `type` argument should be // zero.) It returns one on success or zero if the signature is invalid or an // error occurred. // -// WARNING: |digest| must be the output of some hash function on the data to be +// WARNING: `digest` must be the output of some hash function on the data to be // verified. Passing unhashed inputs will not result in a secure signature // scheme. OPENSSL_EXPORT int ECDSA_verify(int type, const uint8_t *digest, @@ -61,13 +61,13 @@ OPENSSL_EXPORT int ECDSA_verify(int type, const uint8_t *digest, size_t sig_len, const EC_KEY *key); // ECDSA_size returns the maximum size of an ASN.1-based ECDSA signature using -// |key|. It returns zero if |key| is NULL or if it doesn't have a group set. +// `key`. It returns zero if `key` is NULL or if it doesn't have a group set. OPENSSL_EXPORT size_t ECDSA_size(const EC_KEY *key); // Low-level signing and verification. // -// Low-level functions handle signatures as |ECDSA_SIG| structures which allow +// Low-level functions handle signatures as `ECDSA_SIG` structures which allow // the two values in an ECDSA signature to be handled separately. struct ecdsa_sig_st { @@ -75,41 +75,41 @@ struct ecdsa_sig_st { BIGNUM *s; }; -// ECDSA_SIG_new returns a fresh |ECDSA_SIG| structure or NULL on error. +// ECDSA_SIG_new returns a fresh `ECDSA_SIG` structure or NULL on error. OPENSSL_EXPORT ECDSA_SIG *ECDSA_SIG_new(void); -// ECDSA_SIG_free frees |sig| its member |BIGNUM|s. +// ECDSA_SIG_free frees `sig` its member `BIGNUM`s. OPENSSL_EXPORT void ECDSA_SIG_free(ECDSA_SIG *sig); -// ECDSA_SIG_get0_r returns the r component of |sig|. +// ECDSA_SIG_get0_r returns the r component of `sig`. OPENSSL_EXPORT const BIGNUM *ECDSA_SIG_get0_r(const ECDSA_SIG *sig); -// ECDSA_SIG_get0_s returns the s component of |sig|. +// ECDSA_SIG_get0_s returns the s component of `sig`. OPENSSL_EXPORT const BIGNUM *ECDSA_SIG_get0_s(const ECDSA_SIG *sig); -// ECDSA_SIG_get0 sets |*out_r| and |*out_s|, if non-NULL, to the two -// components of |sig|. +// ECDSA_SIG_get0 sets `*out_r` and `*out_s`, if non-NULL, to the two +// components of `sig`. OPENSSL_EXPORT void ECDSA_SIG_get0(const ECDSA_SIG *sig, const BIGNUM **out_r, const BIGNUM **out_s); -// ECDSA_SIG_set0 sets |sig|'s components to |r| and |s|, neither of which may +// ECDSA_SIG_set0 sets `sig`'s components to `r` and `s`, neither of which may // be NULL. On success, it takes ownership of each argument and returns one. // Otherwise, it returns zero. OPENSSL_EXPORT int ECDSA_SIG_set0(ECDSA_SIG *sig, BIGNUM *r, BIGNUM *s); -// ECDSA_do_sign signs |digest_len| bytes from |digest| with |key| and returns +// ECDSA_do_sign signs `digest_len` bytes from `digest` with `key` and returns // the resulting signature structure, or NULL on error. // -// WARNING: |digest| must be the output of some hash function on the data to be +// WARNING: `digest` must be the output of some hash function on the data to be // signed. Passing unhashed inputs will not result in a secure signature scheme. OPENSSL_EXPORT ECDSA_SIG *ECDSA_do_sign(const uint8_t *digest, size_t digest_len, const EC_KEY *key); -// ECDSA_do_verify verifies that |sig| constitutes a valid signature by |key| -// of |digest|. It returns one on success or zero if the signature is invalid +// ECDSA_do_verify verifies that `sig` constitutes a valid signature by `key` +// of `digest`. It returns one on success or zero if the signature is invalid // or on error. // -// WARNING: |digest| must be the output of some hash function on the data to be +// WARNING: `digest` must be the output of some hash function on the data to be // verified. Passing unhashed inputs will not result in a secure signature // scheme. OPENSSL_EXPORT int ECDSA_do_verify(const uint8_t *digest, size_t digest_len, @@ -118,28 +118,28 @@ OPENSSL_EXPORT int ECDSA_do_verify(const uint8_t *digest, size_t digest_len, // ASN.1 functions. -// ECDSA_SIG_parse parses a DER-encoded ECDSA-Sig-Value structure from |cbs| and -// advances |cbs|. It returns a newly-allocated |ECDSA_SIG| or NULL on error. +// ECDSA_SIG_parse parses a DER-encoded ECDSA-Sig-Value structure from `cbs` and +// advances `cbs`. It returns a newly-allocated `ECDSA_SIG` or NULL on error. OPENSSL_EXPORT ECDSA_SIG *ECDSA_SIG_parse(CBS *cbs); -// ECDSA_SIG_from_bytes parses |in| as a DER-encoded ECDSA-Sig-Value structure. -// It returns a newly-allocated |ECDSA_SIG| structure or NULL on error. +// ECDSA_SIG_from_bytes parses `in` as a DER-encoded ECDSA-Sig-Value structure. +// It returns a newly-allocated `ECDSA_SIG` structure or NULL on error. OPENSSL_EXPORT ECDSA_SIG *ECDSA_SIG_from_bytes(const uint8_t *in, size_t in_len); -// ECDSA_SIG_marshal marshals |sig| as a DER-encoded ECDSA-Sig-Value and appends -// the result to |cbb|. It returns one on success and zero on error. +// ECDSA_SIG_marshal marshals `sig` as a DER-encoded ECDSA-Sig-Value and appends +// the result to `cbb`. It returns one on success and zero on error. OPENSSL_EXPORT int ECDSA_SIG_marshal(CBB *cbb, const ECDSA_SIG *sig); -// ECDSA_SIG_to_bytes marshals |sig| as a DER-encoded ECDSA-Sig-Value and, on -// success, sets |*out_bytes| to a newly allocated buffer containing the result +// ECDSA_SIG_to_bytes marshals `sig` as a DER-encoded ECDSA-Sig-Value and, on +// success, sets `*out_bytes` to a newly allocated buffer containing the result // and returns one. Otherwise, it returns zero. The result should be freed with -// |OPENSSL_free|. +// `OPENSSL_free`. OPENSSL_EXPORT int ECDSA_SIG_to_bytes(uint8_t **out_bytes, size_t *out_len, const ECDSA_SIG *sig); // ECDSA_SIG_max_len returns the maximum length of a DER-encoded ECDSA-Sig-Value -// structure for a group whose order is represented in |order_len| bytes, or +// structure for a group whose order is represented in `order_len` bytes, or // zero on overflow. OPENSSL_EXPORT size_t ECDSA_SIG_max_len(size_t order_len); @@ -153,23 +153,23 @@ OPENSSL_EXPORT size_t ECDSA_SIG_max_len(size_t order_len); // the width of the group order. This format is fixed-width, so a given ECDSA // curve's signatures will always have the same size. -// ECDSA_sign_p1363 signs |digest_len| bytes from |digest| with |key| and writes -// the resulting P1363-based signature to |sig|, which must have -// |ECDSA_size_p1363(key)| bytes of space. On successful exit, |*out_sig_len| is +// ECDSA_sign_p1363 signs `digest_len` bytes from `digest` with `key` and writes +// the resulting P1363-based signature to `sig`, which must have +// `ECDSA_size_p1363(key)` bytes of space. On successful exit, `*out_sig_len` is // set to the actual number of bytes written, which will always match -// |ECDSA_size_p1363(key)|. It returns one on success and zero otherwise. +// `ECDSA_size_p1363(key)`. It returns one on success and zero otherwise. // -// WARNING: |digest| must be the output of some hash function on the data to be +// WARNING: `digest` must be the output of some hash function on the data to be // signed. Passing unhashed inputs will not result in a secure signature scheme. OPENSSL_EXPORT int ECDSA_sign_p1363(const uint8_t *digest, size_t digest_len, uint8_t *sig, size_t *out_sig_len, size_t max_sig_len, const EC_KEY *key); -// ECDSA_verify_p1363 verifies that |sig_len| bytes from |sig| constitute a -// valid P1363-based signature by |key| of |digest|. It returns one on success +// ECDSA_verify_p1363 verifies that `sig_len` bytes from `sig` constitute a +// valid P1363-based signature by `key` of `digest`. It returns one on success // or zero if the signature is invalid or an error occurred. // -// WARNING: |digest| must be the output of some hash function on the data to be +// WARNING: `digest` must be the output of some hash function on the data to be // verified. Passing unhashed inputs will not result in a secure signature // scheme. OPENSSL_EXPORT int ECDSA_verify_p1363(const uint8_t *digest, size_t digest_len, @@ -177,16 +177,16 @@ OPENSSL_EXPORT int ECDSA_verify_p1363(const uint8_t *digest, size_t digest_len, const EC_KEY *key); // ECDSA_size_p1363 returns the size of a P1363-based ECDSA signature using -// |key|. It returns zero if |key| is NULL or if it doesn't have a group set. +// `key`. It returns zero if `key` is NULL or if it doesn't have a group set. OPENSSL_EXPORT size_t ECDSA_size_p1363(const EC_KEY *key); // Testing-only functions. // ECDSA_sign_with_nonce_and_leak_private_key_for_testing behaves like -// |ECDSA_do_sign| but uses |nonce| for the ECDSA nonce 'k', instead of a random -// value. |nonce| is interpreted as a big-endian integer. It must be reduced -// modulo the group order and padded with zeros up to |BN_num_bytes(order)| +// `ECDSA_do_sign` but uses `nonce` for the ECDSA nonce 'k', instead of a random +// value. `nonce` is interpreted as a big-endian integer. It must be reduced +// modulo the group order and padded with zeros up to `BN_num_bytes(order)` // bytes. // // WARNING: This function is only exported for testing purposes, when using test @@ -202,17 +202,17 @@ ECDSA_sign_with_nonce_and_leak_private_key_for_testing(const uint8_t *digest, // Deprecated functions. -// d2i_ECDSA_SIG parses aa DER-encoded ECDSA-Sig-Value structure from |len| -// bytes at |*inp|, as described in |d2i_SAMPLE|. +// d2i_ECDSA_SIG parses aa DER-encoded ECDSA-Sig-Value structure from `len` +// bytes at `*inp`, as described in `d2i_SAMPLE`. // -// Use |ECDSA_SIG_parse| instead. +// Use `ECDSA_SIG_parse` instead. OPENSSL_EXPORT ECDSA_SIG *d2i_ECDSA_SIG(ECDSA_SIG **out, const uint8_t **inp, long len); -// i2d_ECDSA_SIG marshals |sig| as a DER-encoded ECDSA-Sig-Value, as described -// in |i2d_SAMPLE|. +// i2d_ECDSA_SIG marshals `sig` as a DER-encoded ECDSA-Sig-Value, as described +// in `i2d_SAMPLE`. // -// Use |ECDSA_SIG_marshal| instead. +// Use `ECDSA_SIG_marshal` instead. OPENSSL_EXPORT int i2d_ECDSA_SIG(const ECDSA_SIG *sig, uint8_t **outp); diff --git a/third_party/boringssl/src/include/openssl/engine.h b/third_party/boringssl/src/include/openssl/engine.h index 6765c28bd..3c2dd5ec2 100644 --- a/third_party/boringssl/src/include/openssl/engine.h +++ b/third_party/boringssl/src/include/openssl/engine.h @@ -27,9 +27,9 @@ extern "C" { // be overridden via a callback. This can be used, for example, to implement an // RSA* that forwards operations to a hardware module. // -// Methods are reference counted but |ENGINE|s are not. When creating a method, +// Methods are reference counted but `ENGINE`s are not. When creating a method, // you should zero the whole structure and fill in the function pointers that -// you wish before setting it on an |ENGINE|. Any functions pointers that +// you wish before setting it on an `ENGINE`. Any functions pointers that // are NULL indicate that the default behaviour should be used. @@ -40,7 +40,7 @@ extern "C" { OPENSSL_EXPORT ENGINE *ENGINE_new(void); // ENGINE_free decrements the reference counts for all methods linked from -// |engine| and frees |engine| itself. It returns one. +// `engine` and frees `engine` itself. It returns one. OPENSSL_EXPORT int ENGINE_free(ENGINE *engine); @@ -69,11 +69,11 @@ OPENSSL_EXPORT ECDSA_METHOD *ENGINE_get_ECDSA_method(const ENGINE *engine); // These functions take a void* type but actually operate on all method // structures. -// METHOD_ref increments the reference count of |method|. This is a no-op for +// METHOD_ref increments the reference count of `method`. This is a no-op for // now because all methods are currently static. void METHOD_ref(void *method); -// METHOD_unref decrements the reference count of |method| and frees it if the +// METHOD_unref decrements the reference count of `method` and frees it if the // reference count drops to zero. This is a no-op for now because all methods // are currently static. void METHOD_unref(void *method); diff --git a/third_party/boringssl/src/include/openssl/err.h b/third_party/boringssl/src/include/openssl/err.h index c7923fe75..7b94af4c6 100644 --- a/third_party/boringssl/src/include/openssl/err.h +++ b/third_party/boringssl/src/include/openssl/err.h @@ -48,20 +48,20 @@ extern "C" { // Reading and formatting errors. // ERR_GET_LIB returns the library code for the error. This is one of -// the |ERR_LIB_*| values. +// the `ERR_LIB_*` values. OPENSSL_INLINE int ERR_GET_LIB(uint32_t packed_error) { return (int)((packed_error >> 24) & 0xff); } // ERR_GET_REASON returns the reason code for the error. This is one of -// library-specific |LIB_R_*| values where |LIB| is the library (see -// |ERR_GET_LIB|). Note that reason codes are specific to the library. +// library-specific `LIB_R_*` values where `LIB` is the library (see +// `ERR_GET_LIB`). Note that reason codes are specific to the library. OPENSSL_INLINE int ERR_GET_REASON(uint32_t packed_error) { return (int)(packed_error & 0xfff); } -// ERR_equals returns one if |packed_error|'s library and reason code are |lib| -// and |reason|, respectively, and zero otherwise. +// ERR_equals returns one if `packed_error`'s library and reason code are `lib` +// and `reason`, respectively, and zero otherwise. OPENSSL_INLINE int ERR_equals(uint32_t packed_error, int lib, int reason) { return ERR_GET_LIB(packed_error) == lib && ERR_GET_REASON(packed_error) == reason; @@ -72,33 +72,33 @@ OPENSSL_INLINE int ERR_equals(uint32_t packed_error, int lib, int reason) { // it returns zero. OPENSSL_EXPORT uint32_t ERR_get_error(void); -// ERR_get_error_line acts like |ERR_get_error|, except that the file and line +// ERR_get_error_line acts like `ERR_get_error`, except that the file and line // number of the call that added the error are also returned. OPENSSL_EXPORT uint32_t ERR_get_error_line(const char **file, int *line); -// ERR_FLAG_STRING means that the |data| member is a NUL-terminated string that -// can be printed. This is always set if |data| is non-NULL. +// ERR_FLAG_STRING means that the `data` member is a NUL-terminated string that +// can be printed. This is always set if `data` is non-NULL. #define ERR_FLAG_STRING 1 -// ERR_FLAG_MALLOCED is passed into |ERR_set_error_data| to indicate that |data| -// was allocated with |OPENSSL_malloc|. +// ERR_FLAG_MALLOCED is passed into `ERR_set_error_data` to indicate that `data` +// was allocated with `OPENSSL_malloc`. // -// It is, separately, returned in |*flags| from |ERR_get_error_line_data| to -// indicate that |*data| has a non-static lifetime, but this lifetime is still -// managed by the library. The caller must not call |OPENSSL_free| or |free| on -// |data|. +// It is, separately, returned in `*flags` from `ERR_get_error_line_data` to +// indicate that `*data` has a non-static lifetime, but this lifetime is still +// managed by the library. The caller must not call `OPENSSL_free` or `free` on +// `data`. #define ERR_FLAG_MALLOCED 2 -// ERR_get_error_line_data acts like |ERR_get_error_line|, but also returns the +// ERR_get_error_line_data acts like `ERR_get_error_line`, but also returns the // error-specific data pointer and flags. The flags are a bitwise-OR of -// |ERR_FLAG_*| values. The error-specific data is owned by the error queue +// `ERR_FLAG_*` values. The error-specific data is owned by the error queue // and the pointer becomes invalid after the next call that affects the same -// thread's error queue. If |*flags| contains |ERR_FLAG_STRING| then |*data| is +// thread's error queue. If `*flags` contains `ERR_FLAG_STRING` then `*data` is // human-readable. OPENSSL_EXPORT uint32_t ERR_get_error_line_data(const char **file, int *line, const char **data, int *flags); -// The "peek" functions act like the |ERR_get_error| functions, above, but they +// The "peek" functions act like the `ERR_get_error` functions, above, but they // do not remove the error from the queue. OPENSSL_EXPORT uint32_t ERR_peek_error(void); OPENSSL_EXPORT uint32_t ERR_peek_error_line(const char **file, int *line); @@ -115,9 +115,9 @@ OPENSSL_EXPORT uint32_t ERR_peek_last_error_line_data(const char **file, int *flags); // ERR_error_string_n generates a human-readable string representing -// |packed_error|, places it at |buf|, and returns |buf|. It writes at most -// |len| bytes (including the terminating NUL) and truncates the string if -// necessary. If |len| is greater than zero then |buf| is always NUL terminated. +// `packed_error`, places it at `buf`, and returns `buf`. It writes at most +// `len` bytes (including the terminating NUL) and truncates the string if +// necessary. If `len` is greater than zero then `buf` is always NUL terminated. // // The string will have the following format: // @@ -129,31 +129,31 @@ OPENSSL_EXPORT char *ERR_error_string_n(uint32_t packed_error, char *buf, size_t len); // ERR_lib_error_string returns a string representation of the library that -// generated |packed_error|, or a placeholder string is the library is +// generated `packed_error`, or a placeholder string is the library is // unrecognized. OPENSSL_EXPORT const char *ERR_lib_error_string(uint32_t packed_error); // ERR_reason_error_string returns a string representation of the reason for -// |packed_error|, or a placeholder string if the reason is unrecognized. +// `packed_error`, or a placeholder string if the reason is unrecognized. OPENSSL_EXPORT const char *ERR_reason_error_string(uint32_t packed_error); // ERR_lib_symbol_name returns the symbol name of library that generated -// |packed_error|, or NULL if unrecognized. For example, an error from -// |ERR_LIB_EVP| would return "EVP". +// `packed_error`, or NULL if unrecognized. For example, an error from +// `ERR_LIB_EVP` would return "EVP". OPENSSL_EXPORT const char *ERR_lib_symbol_name(uint32_t packed_error); // ERR_reason_symbol_name returns the symbol name of the reason for -// |packed_error|, or NULL if unrecognized. For example, |ERR_R_INTERNAL_ERROR| +// `packed_error`, or NULL if unrecognized. For example, `ERR_R_INTERNAL_ERROR` // would return "INTERNAL_ERROR". // -// Errors from the |ERR_LIB_SYS| library are typically |errno| values and will +// Errors from the `ERR_LIB_SYS` library are typically `errno` values and will // return NULL. User-defined errors will also return NULL. OPENSSL_EXPORT const char *ERR_reason_symbol_name(uint32_t packed_error); // ERR_print_errors_callback_t is the type of a function used by -// |ERR_print_errors_cb|. It takes a pointer to a human readable string (and -// its length) that describes an entry in the error queue. The |ctx| argument -// is an opaque pointer given to |ERR_print_errors_cb|. +// `ERR_print_errors_cb`. It takes a pointer to a human readable string (and +// its length) that describes an entry in the error queue. The `ctx` argument +// is an opaque pointer given to `ERR_print_errors_cb`. // // It should return one on success or zero on error, which will stop the // iteration over the error queue. @@ -161,22 +161,22 @@ typedef int (*ERR_print_errors_callback_t)(const char *str, size_t len, void *ctx); // ERR_print_errors_cb clears the current thread's error queue, calling -// |callback| with a string representation of each error, from the least recent +// `callback` with a string representation of each error, from the least recent // to the most recent error. // // The string will have the following format (which differs from -// |ERR_error_string|): +// `ERR_error_string`): // // [thread id]:error:[error code]:[library name]:OPENSSL_internal:[reason string]:[file]:[line number]:[optional string data] // // The callback can return one to continue the iteration or zero to stop it. -// The |ctx| argument is an opaque value that is passed through to the +// The `ctx` argument is an opaque value that is passed through to the // callback. OPENSSL_EXPORT void ERR_print_errors_cb(ERR_print_errors_callback_t callback, void *ctx); // ERR_print_errors_fp clears the current thread's error queue, printing each -// error to |file|. See |ERR_print_errors_cb| for the format. +// error to `file`. See `ERR_print_errors_cb` for the format. OPENSSL_EXPORT void ERR_print_errors_fp(FILE *file); @@ -185,21 +185,21 @@ OPENSSL_EXPORT void ERR_print_errors_fp(FILE *file); // ERR_clear_error clears the error queue for the current thread. OPENSSL_EXPORT void ERR_clear_error(void); -// ERR_set_mark "marks" the most recent error for use with |ERR_pop_to_mark|. +// ERR_set_mark "marks" the most recent error for use with `ERR_pop_to_mark`. // It returns one if an error was marked and zero if there are no errors. OPENSSL_EXPORT int ERR_set_mark(void); // ERR_pop_to_mark removes errors from the most recent to the least recent // until (and not including) a "marked" error. It returns zero if no marked // error was found (and thus all errors were removed) and one otherwise. Errors -// are marked using |ERR_set_mark|. +// are marked using `ERR_set_mark`. OPENSSL_EXPORT int ERR_pop_to_mark(void); // Custom errors. // ERR_get_next_error_library returns a value suitable for passing as the -// |library| argument to |ERR_put_error|. This is intended for code that wishes +// `library` argument to `ERR_put_error`. This is intended for code that wishes // to push its own, non-standard errors to the error queue. OPENSSL_EXPORT int ERR_get_next_error_library(void); @@ -312,29 +312,29 @@ OPENSSL_EXPORT void ERR_load_RAND_strings(void); // ERR_free_strings does nothing. OPENSSL_EXPORT void ERR_free_strings(void); -// ERR_remove_state calls |ERR_clear_error|. +// ERR_remove_state calls `ERR_clear_error`. OPENSSL_EXPORT void ERR_remove_state(unsigned long pid); // ERR_remove_thread_state clears the error queue for the current thread if -// |tid| is NULL. Otherwise it calls |assert(0)|, because it's no longer +// `tid` is NULL. Otherwise it calls `assert(0)`, because it's no longer // possible to delete the error queue for other threads. // -// Use |ERR_clear_error| instead. Note error queues are deleted automatically on +// Use `ERR_clear_error` instead. Note error queues are deleted automatically on // thread exit. You do not need to call this function to release memory. OPENSSL_EXPORT void ERR_remove_thread_state(const CRYPTO_THREADID *tid); // ERR_func_error_string returns the string "OPENSSL_internal". OPENSSL_EXPORT const char *ERR_func_error_string(uint32_t packed_error); -// ERR_error_string behaves like |ERR_error_string_n| but |len| is implicitly -// |ERR_ERROR_STRING_BUF_LEN|. +// ERR_error_string behaves like `ERR_error_string_n` but `len` is implicitly +// `ERR_ERROR_STRING_BUF_LEN`. // -// Additionally, if |buf| is NULL, the error string is placed in a static buffer +// Additionally, if `buf` is NULL, the error string is placed in a static buffer // which is returned. This is not thread-safe and only exists for backwards // compatibility with legacy callers. The static buffer will be overridden by // calls in other threads. // -// Use |ERR_error_string_n| instead. +// Use `ERR_error_string_n` instead. // // TODO(fork): remove this function. OPENSSL_EXPORT char *ERR_error_string(uint32_t packed_error, char *buf); @@ -373,7 +373,7 @@ OPENSSL_EXPORT void ERR_clear_system_error(void); OPENSSL_EXPORT void ERR_put_error(int library, int unused, int reason, const char *file, unsigned line); -// ERR_add_error_data takes a variable number (|count|) of const char* +// ERR_add_error_data takes a variable number (`count`) of const char* // pointers, concatenates them and sets the result as the data on the most // recent error. OPENSSL_EXPORT void ERR_add_error_data(unsigned count, ...); @@ -383,13 +383,13 @@ OPENSSL_EXPORT void ERR_add_error_data(unsigned count, ...); OPENSSL_EXPORT void ERR_add_error_dataf(const char *format, ...) OPENSSL_PRINTF_FORMAT_FUNC(1, 2); -// ERR_set_error_data sets the data on the most recent error to |data|, which -// must be a NUL-terminated string. |flags| must contain |ERR_FLAG_STRING|. If -// |flags| contains |ERR_FLAG_MALLOCED|, this function takes ownership of -// |data|, which must have been allocated with |OPENSSL_malloc|. Otherwise, it -// saves a copy of |data|. +// ERR_set_error_data sets the data on the most recent error to `data`, which +// must be a NUL-terminated string. `flags` must contain `ERR_FLAG_STRING`. If +// `flags` contains `ERR_FLAG_MALLOCED`, this function takes ownership of +// `data`, which must have been allocated with `OPENSSL_malloc`. Otherwise, it +// saves a copy of `data`. // -// Note this differs from OpenSSL which, when |ERR_FLAG_MALLOCED| is unset, +// Note this differs from OpenSSL which, when `ERR_FLAG_MALLOCED` is unset, // saves the pointer as-is and requires it remain valid for the lifetime of the // address space. OPENSSL_EXPORT void ERR_set_error_data(char *data, int flags); @@ -404,7 +404,7 @@ OPENSSL_EXPORT void ERR_set_error_data(char *data, int flags); // OPENSSL_DECLARE_ERROR_REASON is used by util/make_errors.h (which generates // the error defines) to recognise that an additional reason value is needed. // This is needed when the reason value is used outside of an -// |OPENSSL_PUT_ERROR| macro. The resulting define will be +// `OPENSSL_PUT_ERROR` macro. The resulting define will be // ${lib}_R_${reason}. #define OPENSSL_DECLARE_ERROR_REASON(lib, reason) diff --git a/third_party/boringssl/src/include/openssl/evp.h b/third_party/boringssl/src/include/openssl/evp.h index bb168db42..edd94dce2 100644 --- a/third_party/boringssl/src/include/openssl/evp.h +++ b/third_party/boringssl/src/include/openssl/evp.h @@ -37,80 +37,81 @@ extern "C" { // Public/private key objects. // -// An |EVP_PKEY| object represents a public or private key. A given object may +// An `EVP_PKEY` object represents a public or private key. A given object may // be used concurrently on multiple threads by non-mutating functions, provided // no other thread is concurrently calling a mutating function. Unless otherwise -// documented, functions which take a |const| pointer are non-mutating and -// functions which take a non-|const| pointer are mutating. +// documented, functions which take a `const` pointer are non-mutating and +// functions which take a non-`const` pointer are mutating. // EVP_PKEY_new creates a new, empty public-key object and returns it or NULL // on allocation failure. OPENSSL_EXPORT EVP_PKEY *EVP_PKEY_new(void); -// EVP_PKEY_free decrements the reference count of |pkey| and frees it if the +// EVP_PKEY_free decrements the reference count of `pkey` and frees it if the // reference count drops to zero. OPENSSL_EXPORT void EVP_PKEY_free(EVP_PKEY *pkey); -// EVP_PKEY_up_ref increments the reference count of |pkey| and returns one. It -// does not mutate |pkey| for thread-safety purposes and may be used +// EVP_PKEY_up_ref increments the reference count of `pkey` and returns one. It +// does not mutate `pkey` for thread-safety purposes and may be used // concurrently. OPENSSL_EXPORT int EVP_PKEY_up_ref(EVP_PKEY *pkey); -// EVP_PKEY_dup_ref increments the reference count of |pkey| and returns |pkey|. -// The caller must call |EVP_PKEY_free| on the result to release the reference. +// EVP_PKEY_dup_ref increments the reference count of `pkey`, if non-null, and +// returns `pkey`. The caller must call `EVP_PKEY_free` on the result to +// release the reference. // -// WARNING: Although the result is non-const for use with |EVP_PKEY_free|, it is +// WARNING: Although the result is non-const for use with `EVP_PKEY_free`, it is // still shared with other parts of the application that share the same object. -// Avoid mutating shared |EVP_PKEY|s. +// Avoid mutating shared `EVP_PKEY`s. OPENSSL_EXPORT EVP_PKEY *EVP_PKEY_dup_ref(const EVP_PKEY *pkey); -// EVP_PKEY_is_opaque returns one if |pkey| is opaque. Opaque keys are backed by +// EVP_PKEY_is_opaque returns one if `pkey` is opaque. Opaque keys are backed by // custom implementations which do not expose key material and parameters. It is // an error to attempt to duplicate, export, or compare an opaque key. OPENSSL_EXPORT int EVP_PKEY_is_opaque(const EVP_PKEY *pkey); -// EVP_PKEY_eq compares |a| and |b| and returns one if their public keys are +// EVP_PKEY_eq compares `a` and `b` and returns one if their public keys are // equal and zero otherwise. OPENSSL_EXPORT int EVP_PKEY_eq(const EVP_PKEY *a, const EVP_PKEY *b); -// EVP_PKEY_copy_parameters sets the parameters of |to| to equal the parameters -// of |from|. It returns one on success and zero on error. +// EVP_PKEY_copy_parameters sets the parameters of `to` to equal the parameters +// of `from`. It returns one on success and zero on error. OPENSSL_EXPORT int EVP_PKEY_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from); -// EVP_PKEY_missing_parameters returns one if |pkey| is missing needed +// EVP_PKEY_missing_parameters returns one if `pkey` is missing needed // parameters or zero if not, or if the algorithm doesn't take parameters. OPENSSL_EXPORT int EVP_PKEY_missing_parameters(const EVP_PKEY *pkey); -// EVP_PKEY_parameters_eq compares the parameters of |a| and |b|. It returns one +// EVP_PKEY_parameters_eq compares the parameters of `a` and `b`. It returns one // if they match and zero otherwise. In algorithms that do not use parameters, // this function returns one; null parameters are vacuously equal. OPENSSL_EXPORT int EVP_PKEY_parameters_eq(const EVP_PKEY *a, const EVP_PKEY *b); // EVP_PKEY_size returns the maximum size, in bytes, of a signature signed by -// |pkey|. For an RSA key, this returns the number of bytes needed to represent +// `pkey`. For an RSA key, this returns the number of bytes needed to represent // the modulus. For an EC key, this returns the maximum size of a DER-encoded // ECDSA signature. OPENSSL_EXPORT int EVP_PKEY_size(const EVP_PKEY *pkey); -// EVP_PKEY_bits returns the "size", in bits, of |pkey|. For an RSA key, this +// EVP_PKEY_bits returns the "size", in bits, of `pkey`. For an RSA key, this // returns the bit length of the modulus. For an EC key, this returns the bit // length of the group order. OPENSSL_EXPORT int EVP_PKEY_bits(const EVP_PKEY *pkey); -// EVP_PKEY_has_public returns one if |pkey| has a public key, or zero +// EVP_PKEY_has_public returns one if `pkey` has a public key, or zero // otherwise. OPENSSL_EXPORT int EVP_PKEY_has_public(const EVP_PKEY *pkey); -// EVP_PKEY_has_private returns one if |pkey| has a private key, or zero +// EVP_PKEY_has_private returns one if `pkey` has a private key, or zero // otherwise. OPENSSL_EXPORT int EVP_PKEY_has_private(const EVP_PKEY *pkey); -// EVP_PKEY_copy_public returns a newly-allocated |EVP_PKEY| that contains only -// the public key of |pkey|, or NULL on error. Parameters, if relevant for the +// EVP_PKEY_copy_public returns a newly-allocated `EVP_PKEY` that contains only +// the public key of `pkey`, or NULL on error. Parameters, if relevant for the // key type, are also copied. OPENSSL_EXPORT EVP_PKEY *EVP_PKEY_copy_public(const EVP_PKEY *pkey); -// The following constants are returned by |EVP_PKEY_id| and specify the type of +// The following constants are returned by `EVP_PKEY_id` and specify the type of // key. #define EVP_PKEY_NONE NID_undef #define EVP_PKEY_RSA NID_rsaEncryption @@ -127,83 +128,99 @@ OPENSSL_EXPORT EVP_PKEY *EVP_PKEY_copy_public(const EVP_PKEY *pkey); #define EVP_PKEY_ML_KEM_768 NID_ML_KEM_768 #define EVP_PKEY_ML_KEM_1024 NID_ML_KEM_1024 #define EVP_PKEY_XWING NID_X_Wing +#define EVP_PKEY_SLH_DSA_SHA2_128S NID_SLH_DSA_SHA2_128s +#define EVP_PKEY_SLH_DSA_SHAKE_256F NID_SLH_DSA_SHAKE_256f +#define EVP_PKEY_SLH_DSA_SHA2_128S_WITH_SHA256 NID_SLH_DSA_SHA2_128s_WITH_SHA256 -// EVP_PKEY_id returns the type of |pkey|, which is one of the |EVP_PKEY_*| +// EVP_PKEY_id returns the type of `pkey`, which is one of the `EVP_PKEY_*` // values above. These type values generally correspond to the algorithm OID, // but not the parameters, of a SubjectPublicKeyInfo (RFC 5280) or // PrivateKeyInfo (RFC 5208) AlgorithmIdentifier. Algorithm parameters can be // inspected with algorithm-specific accessors, e.g. -// |EVP_PKEY_get_ec_curve_nid|. +// `EVP_PKEY_get_ec_curve_nid`. OPENSSL_EXPORT int EVP_PKEY_id(const EVP_PKEY *pkey); // Algorithms. // -// An |EVP_PKEY| may carry a key from one of several algorithms, represented by -// |EVP_PKEY_ALG|. |EVP_PKEY_ALG|s are used by functions that construct -// |EVP_PKEY|s, such as parsing, so that callers can specify the algorithm(s) to +// An `EVP_PKEY` may carry a key from one of several algorithms, represented by +// `EVP_PKEY_ALG`. `EVP_PKEY_ALG`s are used by functions that construct +// `EVP_PKEY`s, such as parsing, so that callers can specify the algorithm(s) to // use. // -// Each |EVP_PKEY_ALG| generally corresponds to the AlgorithmIdentifier of a +// Each `EVP_PKEY_ALG` generally corresponds to the AlgorithmIdentifier of a // SubjectPublicKeyInfo (RFC 5280) or PrivateKeyInfo (RFC 5208), but some may // support multiple sets of AlgorithmIdentifier parameters, while others may be // specific to one parameter. // EVP_pkey_rsa implements RSA keys (RFC 8017), encoded as rsaEncryption (RFC // 3279, Section 2.3.1). The rsaEncryption encoding is confusingly named: these -// keys are used for all RSA operations, including signing. The |EVP_PKEY_id| -// value is |EVP_PKEY_RSA|. +// keys are used for all RSA operations, including signing. The `EVP_PKEY_id` +// value is `EVP_PKEY_RSA`. // -// WARNING: This |EVP_PKEY_ALG| accepts all RSA key sizes supported by +// WARNING: This `EVP_PKEY_ALG` accepts all RSA key sizes supported by // BoringSSL. When parsing RSA keys, callers should check the size is within -// their desired bounds with |EVP_PKEY_bits|. RSA public key operations scale +// their desired bounds with `EVP_PKEY_bits`. RSA public key operations scale // quadratically and RSA private key operations scale cubicly, so key sizes may // be a DoS vector. OPENSSL_EXPORT const EVP_PKEY_ALG *EVP_pkey_rsa(void); // EVP_pkey_ec_* implement EC keys, encoded as id-ecPublicKey (RFC 5480, // Section 2.1.1). The id-ecPublicKey encoding is confusingly named: it is also -// used for private keys (RFC 5915). The |EVP_PKEY_id| value is |EVP_PKEY_EC|. +// used for private keys (RFC 5915). The `EVP_PKEY_id` value is `EVP_PKEY_EC`. // // Each function only supports the specified curve, but curves are not reflected -// in |EVP_PKEY_id|. The curve can be inspected with -// |EVP_PKEY_get_ec_curve_nid|. +// in `EVP_PKEY_id`. The curve can be inspected with +// `EVP_PKEY_get_ec_curve_nid`. OPENSSL_EXPORT const EVP_PKEY_ALG *EVP_pkey_ec_p224(void); OPENSSL_EXPORT const EVP_PKEY_ALG *EVP_pkey_ec_p256(void); OPENSSL_EXPORT const EVP_PKEY_ALG *EVP_pkey_ec_p384(void); OPENSSL_EXPORT const EVP_PKEY_ALG *EVP_pkey_ec_p521(void); // EVP_pkey_x25519 implements X25519 keys (RFC 7748), encoded as in RFC 8410. -// The |EVP_PKEY_id| value is |EVP_PKEY_X25519|. +// The `EVP_PKEY_id` value is `EVP_PKEY_X25519`. OPENSSL_EXPORT const EVP_PKEY_ALG *EVP_pkey_x25519(void); // EVP_pkey_ed25519 implements Ed25519 keys (RFC 8032), encoded as in RFC 8410. -// The |EVP_PKEY_id| value is |EVP_PKEY_ED25519|. +// The `EVP_PKEY_id` value is `EVP_PKEY_ED25519`. OPENSSL_EXPORT const EVP_PKEY_ALG *EVP_pkey_ed25519(void); -// EVP_pkey_ml_dsa_* implement ML-DSA keys, encoded as in -// draft-ietf-lamps-dilithium-certificates. The |EVP_PKEY_id| values are -// |EVP_PKEY_ML_DSA_*|. In the private key representation, only the "seed" form -// is serialized or parsed. +// EVP_pkey_ml_dsa_* implement ML-DSA keys, encoded as in RFC 9881. The +// `EVP_PKEY_id` values are `EVP_PKEY_ML_DSA_*`. In the private key +// representation, only the "seed" form is serialized or parsed. // // To configure OpenSSL to output the standard "seed" form, configure the // "ml-dsa.output_formats" provider parameter so that "seed-only" is first. This // can be done programmatically with OpenSSL's -// |OSSL_PROVIDER_add_conf_parameter| function, or by passing "-provparam" to +// `OSSL_PROVIDER_add_conf_parameter` function, or by passing "-provparam" to // the command-line tool. +// +// In OpenSSL 4.0, the defaults can also be fixed on a per-encoder basis by +// setting the "output_formats" parameter to "seed-only" with +// `OSSL_ENCODER_CTX_set_params`. OPENSSL_EXPORT const EVP_PKEY_ALG *EVP_pkey_ml_dsa_44(void); OPENSSL_EXPORT const EVP_PKEY_ALG *EVP_pkey_ml_dsa_65(void); OPENSSL_EXPORT const EVP_PKEY_ALG *EVP_pkey_ml_dsa_87(void); +// EVP_pkey_slh_dsa_* implement SLH-DSA and HashSLH-DSA keys, encoded as in +// RFC 9909. The `EVP_PKEY_id` values are `EVP_PKEY_SLH_DSA_*`. +OPENSSL_EXPORT const EVP_PKEY_ALG *EVP_pkey_slh_dsa_sha2_128s(void); +OPENSSL_EXPORT const EVP_PKEY_ALG *EVP_pkey_slh_dsa_sha2_128s_with_sha256(void); +OPENSSL_EXPORT const EVP_PKEY_ALG *EVP_pkey_slh_dsa_shake_256f(void); + // EVP_pkey_ml_kem_* implement ML-KEM keys, encoded as in RFC 9935. The -// |EVP_PKEY_id| values are |EVP_PKEY_ML_KEM_*|. In the private key +// `EVP_PKEY_id` values are `EVP_PKEY_ML_KEM_*`. In the private key // representation, only the "seed" form is serialized or parsed. // // To configure OpenSSL to output the standard "seed" form, configure the // "ml-kem.output_formats" provider parameter so that "seed-only" is first. This // can be done programmatically with OpenSSL's -// |OSSL_PROVIDER_add_conf_parameter| function, or by passing "-provparam" to +// `OSSL_PROVIDER_add_conf_parameter` function, or by passing "-provparam" to // the command-line tool. +// +// In OpenSSL 4.0, the defaults can also be fixed on a per-encoder basis by +// setting the "output_formats" parameter to "seed-only" with +// `OSSL_ENCODER_CTX_set_params`. OPENSSL_EXPORT const EVP_PKEY_ALG *EVP_pkey_ml_kem_768(void); OPENSSL_EXPORT const EVP_PKEY_ALG *EVP_pkey_ml_kem_1024(void); @@ -214,22 +231,22 @@ OPENSSL_EXPORT const EVP_PKEY_ALG *EVP_pkey_ml_kem_1024(void); OPENSSL_EXPORT const EVP_PKEY_ALG *EVP_pkey_xwing(void); // EVP_pkey_dsa implements DSA keys, encoded as in RFC 3279, Section 2.3.2. The -// |EVP_PKEY_id| value is |EVP_PKEY_DSA|. This |EVP_PKEY_ALG| accepts all DSA +// `EVP_PKEY_id` value is `EVP_PKEY_DSA`. This `EVP_PKEY_ALG` accepts all DSA // parameters supported by BoringSSL. // // Keys of this type are not usable with any operations, though the underlying -// |DSA| object can be extracted with |EVP_PKEY_get0_DSA|. This key type is +// `DSA` object can be extracted with `EVP_PKEY_get0_DSA`. This key type is // deprecated and only implemented for compatibility with legacy applications. // -// TODO(crbug.com/42290364): We didn't wire up |EVP_PKEY_sign| and -// |EVP_PKEY_verify| just so it was auditable which callers used DSA. Once DSA +// TODO(crbug.com/42290364): We didn't wire up `EVP_PKEY_sign` and +// `EVP_PKEY_verify` just so it was auditable which callers used DSA. Once DSA // is removed from the default SPKI and PKCS#8 parser and DSA users explicitly -// request |EVP_pkey_dsa|, we could change that. +// request `EVP_pkey_dsa`, we could change that. OPENSSL_EXPORT const EVP_PKEY_ALG *EVP_pkey_dsa(void); // EVP_pkey_rsa_pss_* implements RSASSA-PSS keys, encoded as id-RSASSA-PSS -// (RFC 4055, Section 3.1). The |EVP_PKEY_id| value is |EVP_PKEY_RSA_PSS|. Each -// |EVP_PKEY_ALG| only accepts keys whose parameters specify: +// (RFC 4055, Section 3.1). The `EVP_PKEY_id` value is `EVP_PKEY_RSA_PSS`. Each +// `EVP_PKEY_ALG` only accepts keys whose parameters specify: // // - A hashAlgorithm of the specified hash // - A maskGenAlgorithm of MGF1 with the specified hash @@ -248,18 +265,18 @@ OPENSSL_EXPORT const EVP_PKEY_ALG *EVP_pkey_dsa(void); // parameters chosen. // // Note the id-RSASSA-PSS key type is distinct from the RSASSA-PSS signature -// algorithm. The widely implemented id-rsaEncryption key type (|EVP_pkey_rsa| -// and |EVP_PKEY_RSA|) also supports RSASSA-PSS signatures. +// algorithm. The widely implemented id-rsaEncryption key type (`EVP_pkey_rsa` +// and `EVP_PKEY_RSA`) also supports RSASSA-PSS signatures. // -// WARNING: Any |EVP_PKEY|s produced by this algorithm will return a non-NULL -// |RSA| object through |EVP_PKEY_get1_RSA| and |EVP_PKEY_get0_RSA|. This is +// WARNING: Any `EVP_PKEY`s produced by this algorithm will return a non-NULL +// `RSA` object through `EVP_PKEY_get1_RSA` and `EVP_PKEY_get0_RSA`. This is // dangerous as existing code may assume a non-NULL return implies the more // common id-rsaEncryption key. Additionally, the operations on the underlying -// |RSA| object will not capture the RSA-PSS constraints, so callers risk +// `RSA` object will not capture the RSA-PSS constraints, so callers risk // misusing the key by calling these functions. Callers using this algorithm -// must use |EVP_PKEY_id| to distinguish |EVP_PKEY_RSA| and |EVP_PKEY_RSA_PSS|. +// must use `EVP_PKEY_id` to distinguish `EVP_PKEY_RSA` and `EVP_PKEY_RSA_PSS`. // -// WARNING: BoringSSL does not currently implement |RSA_get0_pss_params| with +// WARNING: BoringSSL does not currently implement `RSA_get0_pss_params` with // these keys. Callers that require this functionality should contact the // BoringSSL team. OPENSSL_EXPORT const EVP_PKEY_ALG *EVP_pkey_rsa_pss_sha256(void); @@ -270,46 +287,46 @@ OPENSSL_EXPORT const EVP_PKEY_ALG *EVP_pkey_rsa_pss_sha512(void); // Getting and setting concrete key types. // // The following functions get and set the underlying key representation in an -// |EVP_PKEY| object. The |set1| functions take an additional reference to the -// underlying key and return one on success or zero if |key| is NULL. The -// |assign| functions adopt the caller's reference and return one on success or -// zero if |key| is NULL. The |get1| functions return a fresh reference to the -// underlying object or NULL if |pkey| is not of the correct type. The |get0| +// `EVP_PKEY` object. The `set1` functions take an additional reference to the +// underlying key and return one on success or zero if `key` is NULL. The +// `assign` functions adopt the caller's reference and return one on success or +// zero if `key` is NULL. The `get1` functions return a fresh reference to the +// underlying object or NULL if `pkey` is not of the correct type. The `get0` // functions behave the same but return a non-owning pointer. // -// The |get0| and |get1| functions take |const| pointers and are thus +// The `get0` and `get1` functions take `const` pointers and are thus // non-mutating for thread-safety purposes, but mutating functions on the -// returned lower-level objects are considered to also mutate the |EVP_PKEY| and -// may not be called concurrently with other operations on the |EVP_PKEY|. +// returned lower-level objects are considered to also mutate the `EVP_PKEY` and +// may not be called concurrently with other operations on the `EVP_PKEY`. // // WARNING: Matching OpenSSL, the RSA functions behave non-uniformly. -// |EVP_PKEY_set1_RSA| and |EVP_PKEY_assign_RSA| construct an |EVP_PKEY_RSA| -// key, while the |EVP_PKEY_get0_RSA| and |EVP_PKEY_get1_RSA| will return -// non-NULL for both |EVP_PKEY_RSA| and |EVP_PKEY_RSA_PSS|. +// `EVP_PKEY_set1_RSA` and `EVP_PKEY_assign_RSA` construct an `EVP_PKEY_RSA` +// key, while the `EVP_PKEY_get0_RSA` and `EVP_PKEY_get1_RSA` will return +// non-NULL for both `EVP_PKEY_RSA` and `EVP_PKEY_RSA_PSS`. // // This means callers risk misusing a key if they assume a non-NULL return from -// |EVP_PKEY_get0_RSA| or |EVP_PKEY_get1_RSA| implies |EVP_PKEY_RSA|. Prefer -// |EVP_PKEY_id| to check the type of a key. To reduce this risk, BoringSSL does -// not make |EVP_PKEY_RSA_PSS| available by default, only when callers opt in -// via |EVP_pkey_rsa_pss_sha256|. This differs from upstream OpenSSL, where -// callers are exposed to |EVP_PKEY_RSA_PSS| by default. +// `EVP_PKEY_get0_RSA` or `EVP_PKEY_get1_RSA` implies `EVP_PKEY_RSA`. Prefer +// `EVP_PKEY_id` to check the type of a key. To reduce this risk, BoringSSL does +// not make `EVP_PKEY_RSA_PSS` available by default, only when callers opt in +// via `EVP_pkey_rsa_pss_sha256`. This differs from upstream OpenSSL, where +// callers are exposed to `EVP_PKEY_RSA_PSS` by default. -OPENSSL_EXPORT int EVP_PKEY_set1_RSA(EVP_PKEY *pkey, RSA *key); +OPENSSL_EXPORT int EVP_PKEY_set1_RSA(EVP_PKEY *pkey, const RSA *key); OPENSSL_EXPORT int EVP_PKEY_assign_RSA(EVP_PKEY *pkey, RSA *key); OPENSSL_EXPORT RSA *EVP_PKEY_get0_RSA(const EVP_PKEY *pkey); OPENSSL_EXPORT RSA *EVP_PKEY_get1_RSA(const EVP_PKEY *pkey); -OPENSSL_EXPORT int EVP_PKEY_set1_DSA(EVP_PKEY *pkey, DSA *key); +OPENSSL_EXPORT int EVP_PKEY_set1_DSA(EVP_PKEY *pkey, const DSA *key); OPENSSL_EXPORT int EVP_PKEY_assign_DSA(EVP_PKEY *pkey, DSA *key); OPENSSL_EXPORT DSA *EVP_PKEY_get0_DSA(const EVP_PKEY *pkey); OPENSSL_EXPORT DSA *EVP_PKEY_get1_DSA(const EVP_PKEY *pkey); -OPENSSL_EXPORT int EVP_PKEY_set1_EC_KEY(EVP_PKEY *pkey, EC_KEY *key); +OPENSSL_EXPORT int EVP_PKEY_set1_EC_KEY(EVP_PKEY *pkey, const EC_KEY *key); OPENSSL_EXPORT int EVP_PKEY_assign_EC_KEY(EVP_PKEY *pkey, EC_KEY *key); OPENSSL_EXPORT EC_KEY *EVP_PKEY_get0_EC_KEY(const EVP_PKEY *pkey); OPENSSL_EXPORT EC_KEY *EVP_PKEY_get1_EC_KEY(const EVP_PKEY *pkey); -OPENSSL_EXPORT int EVP_PKEY_set1_DH(EVP_PKEY *pkey, DH *key); +OPENSSL_EXPORT int EVP_PKEY_set1_DH(EVP_PKEY *pkey, const DH *key); OPENSSL_EXPORT int EVP_PKEY_assign_DH(EVP_PKEY *pkey, DH *key); OPENSSL_EXPORT DH *EVP_PKEY_get0_DH(const EVP_PKEY *pkey); OPENSSL_EXPORT DH *EVP_PKEY_get1_DH(const EVP_PKEY *pkey); @@ -318,21 +335,21 @@ OPENSSL_EXPORT DH *EVP_PKEY_get1_DH(const EVP_PKEY *pkey); // ASN.1 functions // EVP_PKEY_from_subject_public_key_info decodes a DER-encoded -// SubjectPublicKeyInfo structure (RFC 5280) from |in|. It returns a -// newly-allocated |EVP_PKEY| or NULL on error. Only the |num_algs| algorithms -// in |algs| will be considered when parsing. +// SubjectPublicKeyInfo structure (RFC 5280) from `in`. It returns a +// newly-allocated `EVP_PKEY` or NULL on error. Only the `num_algs` algorithms +// in `algs` will be considered when parsing. OPENSSL_EXPORT EVP_PKEY *EVP_PKEY_from_subject_public_key_info( const uint8_t *in, size_t len, const EVP_PKEY_ALG *const *algs, size_t num_algs); // EVP_parse_public_key decodes a DER-encoded SubjectPublicKeyInfo structure -// (RFC 5280) from |cbs| and advances |cbs|. It returns a newly-allocated -// |EVP_PKEY| or NULL on error. +// (RFC 5280) from `cbs` and advances `cbs`. It returns a newly-allocated +// `EVP_PKEY` or NULL on error. // -// Prefer |EVP_PKEY_from_subject_public_key_info| instead. This function has +// Prefer `EVP_PKEY_from_subject_public_key_info` instead. This function has // several pitfalls: // -// Callers are expected to handle trailing data returned from |cbs|, making more +// Callers are expected to handle trailing data returned from `cbs`, making more // common cases error-prone. // // There is also no way to pass in supported algorithms. This function instead @@ -346,14 +363,14 @@ OPENSSL_EXPORT EVP_PKEY *EVP_PKEY_from_subject_public_key_info( // size or EC curve. OPENSSL_EXPORT EVP_PKEY *EVP_parse_public_key(CBS *cbs); -// EVP_marshal_public_key marshals |key| as a DER-encoded SubjectPublicKeyInfo -// structure (RFC 5280) and appends the result to |cbb|. It returns one on +// EVP_marshal_public_key marshals `key` as a DER-encoded SubjectPublicKeyInfo +// structure (RFC 5280) and appends the result to `cbb`. It returns one on // success and zero on error. OPENSSL_EXPORT int EVP_marshal_public_key(CBB *cbb, const EVP_PKEY *key); // EVP_PKEY_from_private_key_info decodes a DER-encoded PrivateKeyInfo structure -// (RFC 5208) from |in|. It returns a newly-allocated |EVP_PKEY| or NULL on -// error. Only the |num_algs| algorithms in |algs| will be considered when +// (RFC 5208) from `in`. It returns a newly-allocated `EVP_PKEY` or NULL on +// error. Only the `num_algs` algorithms in `algs` will be considered when // parsing. // // A PrivateKeyInfo ends with an optional set of attributes. These are silently @@ -363,13 +380,13 @@ OPENSSL_EXPORT EVP_PKEY *EVP_PKEY_from_private_key_info( size_t num_algs); // EVP_parse_private_key decodes a DER-encoded PrivateKeyInfo structure (RFC -// 5208) from |cbs| and advances |cbs|. It returns a newly-allocated |EVP_PKEY| +// 5208) from `cbs` and advances `cbs`. It returns a newly-allocated `EVP_PKEY` // or NULL on error. // -// Prefer |EVP_PKEY_from_private_key_info| instead. This function has +// Prefer `EVP_PKEY_from_private_key_info` instead. This function has // several pitfalls: // -// Callers are expected to handle trailing data returned from |cbs|, making more +// Callers are expected to handle trailing data returned from `cbs`, making more // common cases error-prone. // // There is also no way to pass in supported algorithms. This function instead @@ -382,14 +399,14 @@ OPENSSL_EXPORT EVP_PKEY *EVP_PKEY_from_private_key_info( // it is suitable and validate other desired key properties such as RSA modulus // size or EC curve. In particular, RSA private key operations scale cubicly, so // applications accepting RSA private keys from external sources may need to -// bound key sizes (use |EVP_PKEY_bits| or |RSA_bits|) to avoid a DoS vector. +// bound key sizes (use `EVP_PKEY_bits` or `RSA_bits`) to avoid a DoS vector. // // A PrivateKeyInfo ends with an optional set of attributes. These are silently // ignored. OPENSSL_EXPORT EVP_PKEY *EVP_parse_private_key(CBS *cbs); -// EVP_marshal_private_key marshals |key| as a DER-encoded PrivateKeyInfo -// structure (RFC 5208) and appends the result to |cbb|. It returns one on +// EVP_marshal_private_key marshals `key` as a DER-encoded PrivateKeyInfo +// structure (RFC 5208) and appends the result to `cbb`. It returns one on // success and zero on error. OPENSSL_EXPORT int EVP_marshal_private_key(CBB *cbb, const EVP_PKEY *key); @@ -402,66 +419,69 @@ OPENSSL_EXPORT int EVP_marshal_private_key(CBB *cbb, const EVP_PKEY *key); // - X25519, using the formats in RFC 7748. // // - Ed25519, using the formats in RFC 8032. Note the RFC 8032 private key -// format is the 32-byte prefix of |ED25519_sign|'s 64-byte private key. +// format is the 32-byte prefix of `ED25519_sign`'s 64-byte private key. // // - ML-DSA, using the formats in FIPS 204. The private key representation // supported by BoringSSL is the 32-byte "seed", defined in FIPS 204 as 𝜉, not // the larger expanded form. For OpenSSL compatibility, it is not used with -// the |EVP_PKEY_from_raw_private_key| and |EVP_PKEY_get_raw_private_key| -// APIs, but instead the |EVP_PKEY_from_private_seed| and -// |EVP_PKEY_get_private_seed| APIs. +// the `EVP_PKEY_from_raw_private_key` and `EVP_PKEY_get_raw_private_key` +// APIs, but instead the `EVP_PKEY_from_private_seed` and +// `EVP_PKEY_get_private_seed` APIs. // // - ML-KEM, using the formats in FIPS 203. The private key representation // supported by BoringSSL is the 64-byte "seed" resulting from the // concatenation of d||z, as each is defined in FIPS 203. // +// - SLH-DSA, using the 2n-byte public key and 4n-byte private key formats in +// FIPS 205 and RFC 9909. +// // These formats are suitable if serializing a key in a context where the // algorithm is already known and there is no need to encode it. -// EVP_PKEY_from_raw_private_key interprets |in| as a raw private key of type -// |alg| and returns a newly-allocated |EVP_PKEY|, or nullptr on error. +// EVP_PKEY_from_raw_private_key interprets `in` as a raw private key of type +// `alg` and returns a newly-allocated `EVP_PKEY`, or nullptr on error. OPENSSL_EXPORT EVP_PKEY *EVP_PKEY_from_raw_private_key(const EVP_PKEY_ALG *alg, const uint8_t *in, size_t len); -// EVP_PKEY_from_private_seed interprets |in| as a private seed of type |alg| -// and returns a newly-allocated |EVP_PKEY|, or nullptr on error. +// EVP_PKEY_from_private_seed interprets `in` as a private seed of type `alg` +// and returns a newly-allocated `EVP_PKEY`, or nullptr on error. OPENSSL_EXPORT EVP_PKEY *EVP_PKEY_from_private_seed(const EVP_PKEY_ALG *alg, const uint8_t *in, size_t len); -// EVP_PKEY_from_raw_public_key interprets |in| as a raw public key of type -// |alg| and returns a newly-allocated |EVP_PKEY|, or nullptr on error. +// EVP_PKEY_from_raw_public_key interprets `in` as a raw public key of type +// `alg` and returns a newly-allocated `EVP_PKEY`, or nullptr on error. OPENSSL_EXPORT EVP_PKEY *EVP_PKEY_from_raw_public_key(const EVP_PKEY_ALG *alg, const uint8_t *in, size_t len); -// EVP_PKEY_get_raw_private_key outputs the private key for |pkey| in raw form. -// If |out| is NULL, it sets |*out_len| to the size of the raw private key. -// Otherwise, it writes at most |*out_len| bytes to |out| and sets |*out_len| to +// EVP_PKEY_get_raw_private_key outputs the private key for `pkey` in raw form. +// If `out` is NULL, it sets `*out_len` to the size of the raw private key. +// Otherwise, it writes at most `*out_len` bytes to `out` and sets `*out_len` to // the number of bytes written. // -// It returns one on success and zero if |pkey| has no private key, the key +// It returns one on success and zero if `pkey` has no private key, the key // type does not support this format, or the buffer is too small. OPENSSL_EXPORT int EVP_PKEY_get_raw_private_key(const EVP_PKEY *pkey, uint8_t *out, size_t *out_len); -// EVP_PKEY_get_private_seed outputs the private key for |pkey| as a private -// seed. If |out| is NULL, it sets |*out_len| to the size of the seed. -// Otherwise, it writes at most |*out_len| bytes to |out| and sets -// |*out_len| to the number of bytes written. +// EVP_PKEY_get_private_seed outputs the private key for `pkey` as a private +// seed. If `out` is NULL, it sets `*out_len` to the size of the seed. +// Otherwise, it writes at most `*out_len` bytes to `out` and sets +// `*out_len` to the number of bytes written. // -// It returns one on success and zero if |pkey| has no private key, the key +// It returns one on success and zero if `pkey` has no private key, the key // type does not support this format, or the buffer is too small. OPENSSL_EXPORT int EVP_PKEY_get_private_seed(const EVP_PKEY *pkey, uint8_t *out, size_t *out_len); -// EVP_PKEY_get_raw_public_key outputs the public key for |pkey| in raw form. -// If |out| is NULL, it sets |*out_len| to the size of the raw public key. -// Otherwise, it writes at most |*out_len| bytes to |out| and sets |*out_len| to +// EVP_PKEY_get_raw_public_key outputs the public key for `pkey` in raw form. +// If `out` is NULL, it sets `*out_len` to the size of the raw public key. +// Otherwise, it writes at most `*out_len` bytes to `out` and sets `*out_len` to // the number of bytes written. // -// It returns one on success and zero if |pkey| has no public key, the key +// It returns one on success and zero if `pkey` has no public key, the key // type does not support this format, or the buffer is too small. OPENSSL_EXPORT int EVP_PKEY_get_raw_public_key(const EVP_PKEY *pkey, uint8_t *out, size_t *out_len); @@ -469,64 +489,61 @@ OPENSSL_EXPORT int EVP_PKEY_get_raw_public_key(const EVP_PKEY *pkey, // Key generation -// EVP_PKEY_generate_from_alg generates a new key of type |alg|. It returns a -// newly-allocated |EVP_PKEY| or nullptr on error. +// EVP_PKEY_generate_from_alg generates a new key of type `alg`. It returns a +// newly-allocated `EVP_PKEY` or nullptr on error. // -// When passed |EVP_pkey_rsa|, this function generates an RSA-2048 key with the -// recommended public exponent of 65537, or |RSA_F4|. Use |EVP_RSA_gen| or -// |EVP_PKEY_keygen| instead to customize these parameters. +// When passed `EVP_pkey_rsa`, this function generates an RSA-2048 key with the +// recommended public exponent of 65537, or `RSA_F4`. Use `EVP_RSA_gen` or +// `EVP_PKEY_keygen` instead to customize these parameters. OPENSSL_EXPORT EVP_PKEY *EVP_PKEY_generate_from_alg(const EVP_PKEY_ALG *alg); // Signing -// EVP_DigestSignInit sets up |ctx| for a signing operation with |type| and -// |pkey|. The |ctx| argument must have been initialised with -// |EVP_MD_CTX_init|. If |pctx| is not NULL, the |EVP_PKEY_CTX| of the signing -// operation will be written to |*pctx|; this can be used to set alternative +// EVP_DigestSignInit sets up `ctx` for a signing operation with `type` and +// `pkey`. The `ctx` argument must have been initialised with +// `EVP_MD_CTX_init`. If `pctx` is not NULL, the `EVP_PKEY_CTX` of the signing +// operation will be written to `*pctx`; this can be used to set alternative // signing options. // // For single-shot signing algorithms which do not use a pre-hash, such as -// Ed25519, |type| should be NULL. The |EVP_MD_CTX| itself is unused but is -// present so the API is uniform. See |EVP_DigestSign|. -// -// This function does not mutate |pkey| for thread-safety purposes and may be -// used concurrently with other non-mutating functions on |pkey|. +// Ed25519, `type` should be NULL. The `EVP_MD_CTX` itself is unused but is +// present so the API is uniform. See `EVP_DigestSign`. // // It returns one on success, or zero on error. OPENSSL_EXPORT int EVP_DigestSignInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx, const EVP_MD *type, ENGINE *e, - EVP_PKEY *pkey); + const EVP_PKEY *pkey); -// EVP_DigestSignUpdate appends |len| bytes from |data| to the data which will -// be signed in |EVP_DigestSignFinal|. It returns one. +// EVP_DigestSignUpdate appends `len` bytes from `data` to the data which will +// be signed in `EVP_DigestSignFinal`. It returns one. // // This function performs a streaming signing operation and will fail for -// signature algorithms which do not support this. Use |EVP_DigestSign| for a +// signature algorithms which do not support this. Use `EVP_DigestSign` for a // single-shot operation. OPENSSL_EXPORT int EVP_DigestSignUpdate(EVP_MD_CTX *ctx, const void *data, size_t len); // EVP_DigestSignFinal signs the data that has been included by one or more -// calls to |EVP_DigestSignUpdate|. If |out_sig| is NULL then |*out_sig_len| is +// calls to `EVP_DigestSignUpdate`. If `out_sig` is NULL then `*out_sig_len` is // set to the maximum number of output bytes. Otherwise, on entry, -// |*out_sig_len| must contain the length of the |out_sig| buffer. If the call -// is successful, the signature is written to |out_sig| and |*out_sig_len| is +// `*out_sig_len` must contain the length of the `out_sig` buffer. If the call +// is successful, the signature is written to `out_sig` and `*out_sig_len` is // set to its length. // // This function performs a streaming signing operation and will fail for -// signature algorithms which do not support this. Use |EVP_DigestSign| for a +// signature algorithms which do not support this. Use `EVP_DigestSign` for a // single-shot operation. // // It returns one on success, or zero on error. OPENSSL_EXPORT int EVP_DigestSignFinal(EVP_MD_CTX *ctx, uint8_t *out_sig, size_t *out_sig_len); -// EVP_DigestSign signs |data_len| bytes from |data| using |ctx|. If |out_sig| -// is NULL then |*out_sig_len| is set to the maximum number of output -// bytes. Otherwise, on entry, |*out_sig_len| must contain the length of the -// |out_sig| buffer. If the call is successful, the signature is written to -// |out_sig| and |*out_sig_len| is set to its length. +// EVP_DigestSign signs `data_len` bytes from `data` using `ctx`. If `out_sig` +// is NULL then `*out_sig_len` is set to the maximum number of output +// bytes. Otherwise, on entry, `*out_sig_len` must contain the length of the +// `out_sig` buffer. If the call is successful, the signature is written to +// `out_sig` and `*out_sig_len` is set to its length. // // It returns one on success and zero on error. OPENSSL_EXPORT int EVP_DigestSign(EVP_MD_CTX *ctx, uint8_t *out_sig, @@ -536,45 +553,42 @@ OPENSSL_EXPORT int EVP_DigestSign(EVP_MD_CTX *ctx, uint8_t *out_sig, // Verifying -// EVP_DigestVerifyInit sets up |ctx| for a signature verification operation -// with |type| and |pkey|. The |ctx| argument must have been initialised with -// |EVP_MD_CTX_init|. If |pctx| is not NULL, the |EVP_PKEY_CTX| of the signing -// operation will be written to |*pctx|; this can be used to set alternative +// EVP_DigestVerifyInit sets up `ctx` for a signature verification operation +// with `type` and `pkey`. The `ctx` argument must have been initialised with +// `EVP_MD_CTX_init`. If `pctx` is not NULL, the `EVP_PKEY_CTX` of the signing +// operation will be written to `*pctx`; this can be used to set alternative // signing options. // // For single-shot signing algorithms which do not use a pre-hash, such as -// Ed25519, |type| should be NULL. The |EVP_MD_CTX| itself is unused but is -// present so the API is uniform. See |EVP_DigestVerify|. -// -// This function does not mutate |pkey| for thread-safety purposes and may be -// used concurrently with other non-mutating functions on |pkey|. +// Ed25519, `type` should be NULL. The `EVP_MD_CTX` itself is unused but is +// present so the API is uniform. See `EVP_DigestVerify`. // // It returns one on success, or zero on error. OPENSSL_EXPORT int EVP_DigestVerifyInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx, const EVP_MD *type, ENGINE *e, - EVP_PKEY *pkey); + const EVP_PKEY *pkey); -// EVP_DigestVerifyUpdate appends |len| bytes from |data| to the data which -// will be verified by |EVP_DigestVerifyFinal|. It returns one. +// EVP_DigestVerifyUpdate appends `len` bytes from `data` to the data which +// will be verified by `EVP_DigestVerifyFinal`. It returns one. // // This function performs streaming signature verification and will fail for -// signature algorithms which do not support this. Use |EVP_DigestVerify| for a +// signature algorithms which do not support this. Use `EVP_DigestVerify` for a // single-shot verification. OPENSSL_EXPORT int EVP_DigestVerifyUpdate(EVP_MD_CTX *ctx, const void *data, size_t len); -// EVP_DigestVerifyFinal verifies that |sig_len| bytes of |sig| are a valid +// EVP_DigestVerifyFinal verifies that `sig_len` bytes of `sig` are a valid // signature for the data that has been included by one or more calls to -// |EVP_DigestVerifyUpdate|. It returns one on success and zero otherwise. +// `EVP_DigestVerifyUpdate`. It returns one on success and zero otherwise. // // This function performs streaming signature verification and will fail for -// signature algorithms which do not support this. Use |EVP_DigestVerify| for a +// signature algorithms which do not support this. Use `EVP_DigestVerify` for a // single-shot verification. OPENSSL_EXPORT int EVP_DigestVerifyFinal(EVP_MD_CTX *ctx, const uint8_t *sig, size_t sig_len); -// EVP_DigestVerify verifies that |sig_len| bytes from |sig| are a valid -// signature for |data|. It returns one on success or zero on error. +// EVP_DigestVerify verifies that `sig_len` bytes from `sig` are a valid +// signature for `data`. It returns one on success or zero on error. OPENSSL_EXPORT int EVP_DigestVerify(EVP_MD_CTX *ctx, const uint8_t *sig, size_t sig_len, const uint8_t *data, size_t len); @@ -582,89 +596,80 @@ OPENSSL_EXPORT int EVP_DigestVerify(EVP_MD_CTX *ctx, const uint8_t *sig, // Signing (old functions) -// EVP_SignInit_ex configures |ctx|, which must already have been initialised, -// for a fresh signing operation using the hash function |type|. It returns one +// EVP_SignInit_ex configures `ctx`, which must already have been initialised, +// for a fresh signing operation using the hash function `type`. It returns one // on success and zero otherwise. // -// (In order to initialise |ctx|, either obtain it initialised with -// |EVP_MD_CTX_create|, or use |EVP_MD_CTX_init|.) +// (In order to initialise `ctx`, either obtain it initialised with +// `EVP_MD_CTX_create`, or use `EVP_MD_CTX_init`.) OPENSSL_EXPORT int EVP_SignInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl); -// EVP_SignInit is a deprecated version of |EVP_SignInit_ex|. +// EVP_SignInit is a deprecated version of `EVP_SignInit_ex`. // // TODO(fork): remove. OPENSSL_EXPORT int EVP_SignInit(EVP_MD_CTX *ctx, const EVP_MD *type); -// EVP_SignUpdate appends |len| bytes from |data| to the data which will be -// signed in |EVP_SignFinal|. +// EVP_SignUpdate appends `len` bytes from `data` to the data which will be +// signed in `EVP_SignFinal`. OPENSSL_EXPORT int EVP_SignUpdate(EVP_MD_CTX *ctx, const void *data, size_t len); // EVP_SignFinal signs the data that has been included by one or more calls to -// |EVP_SignUpdate|, using the key |pkey|, and writes it to |sig|. On entry, -// |sig| must point to at least |EVP_PKEY_size(pkey)| bytes of space. The -// actual size of the signature is written to |*out_sig_len|. +// `EVP_SignUpdate`, using the key `pkey`, and writes it to `sig`. On entry, +// `sig` must point to at least `EVP_PKEY_size(pkey)` bytes of space. The +// actual size of the signature is written to `*out_sig_len`. // // It returns one on success and zero otherwise. -// -// It does not modify |ctx|, thus it's possible to continue to use |ctx| in -// order to sign a longer message. It also does not mutate |pkey| for -// thread-safety purposes and may be used concurrently with other non-mutating -// functions on |pkey|. OPENSSL_EXPORT int EVP_SignFinal(const EVP_MD_CTX *ctx, uint8_t *sig, - unsigned int *out_sig_len, EVP_PKEY *pkey); + unsigned int *out_sig_len, + const EVP_PKEY *pkey); // Verifying (old functions) -// EVP_VerifyInit_ex configures |ctx|, which must already have been +// EVP_VerifyInit_ex configures `ctx`, which must already have been // initialised, for a fresh signature verification operation using the hash -// function |type|. It returns one on success and zero otherwise. +// function `type`. It returns one on success and zero otherwise. // -// (In order to initialise |ctx|, either obtain it initialised with -// |EVP_MD_CTX_create|, or use |EVP_MD_CTX_init|.) +// (In order to initialise `ctx`, either obtain it initialised with +// `EVP_MD_CTX_create`, or use `EVP_MD_CTX_init`.) OPENSSL_EXPORT int EVP_VerifyInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl); -// EVP_VerifyInit is a deprecated version of |EVP_VerifyInit_ex|. +// EVP_VerifyInit is a deprecated version of `EVP_VerifyInit_ex`. // // TODO(fork): remove. OPENSSL_EXPORT int EVP_VerifyInit(EVP_MD_CTX *ctx, const EVP_MD *type); -// EVP_VerifyUpdate appends |len| bytes from |data| to the data which will be -// signed in |EVP_VerifyFinal|. +// EVP_VerifyUpdate appends `len` bytes from `data` to the data which will be +// signed in `EVP_VerifyFinal`. OPENSSL_EXPORT int EVP_VerifyUpdate(EVP_MD_CTX *ctx, const void *data, size_t len); -// EVP_VerifyFinal verifies that |sig_len| bytes of |sig| are a valid -// signature, by |pkey|, for the data that has been included by one or more -// calls to |EVP_VerifyUpdate|. +// EVP_VerifyFinal verifies that `sig_len` bytes of `sig` are a valid +// signature, by `pkey`, for the data that has been included by one or more +// calls to `EVP_VerifyUpdate`. // // It returns one on success and zero otherwise. -// -// It does not modify |ctx|, thus it's possible to continue to use |ctx| in -// order to verify a longer message. It also does not mutate |pkey| for -// thread-safety purposes and may be used concurrently with other non-mutating -// functions on |pkey|. OPENSSL_EXPORT int EVP_VerifyFinal(EVP_MD_CTX *ctx, const uint8_t *sig, - size_t sig_len, EVP_PKEY *pkey); + size_t sig_len, const EVP_PKEY *pkey); // Printing // EVP_PKEY_print_public prints a textual representation of the public key in -// |pkey| to |out|. Returns one on success or zero otherwise. +// `pkey` to `out`. Returns one on success or zero otherwise. OPENSSL_EXPORT int EVP_PKEY_print_public(BIO *out, const EVP_PKEY *pkey, int indent, ASN1_PCTX *pctx); // EVP_PKEY_print_private prints a textual representation of the private key in -// |pkey| to |out|. Returns one on success or zero otherwise. +// `pkey` to `out`. Returns one on success or zero otherwise. OPENSSL_EXPORT int EVP_PKEY_print_private(BIO *out, const EVP_PKEY *pkey, int indent, ASN1_PCTX *pctx); // EVP_PKEY_print_params prints a textual representation of the parameters in -// |pkey| to |out|. Returns one on success or zero otherwise. +// `pkey` to `out`. Returns one on success or zero otherwise. OPENSSL_EXPORT int EVP_PKEY_print_params(BIO *out, const EVP_PKEY *pkey, int indent, ASN1_PCTX *pctx); @@ -675,38 +680,38 @@ OPENSSL_EXPORT int EVP_PKEY_print_params(BIO *out, const EVP_PKEY *pkey, // function that results in a key suitable for use in symmetric // cryptography. -// PKCS5_PBKDF2_HMAC computes |iterations| iterations of PBKDF2 of |password| -// and |salt|, using |digest|, and outputs |key_len| bytes to |out_key|. It +// PKCS5_PBKDF2_HMAC computes `iterations` iterations of PBKDF2 of `password` +// and `salt`, using `digest`, and outputs `key_len` bytes to `out_key`. It // returns one on success and zero on allocation failure or if iterations is 0. OPENSSL_EXPORT int PKCS5_PBKDF2_HMAC(const char *password, size_t password_len, const uint8_t *salt, size_t salt_len, uint32_t iterations, const EVP_MD *digest, size_t key_len, uint8_t *out_key); -// PKCS5_PBKDF2_HMAC_SHA1 is the same as PKCS5_PBKDF2_HMAC, but with |digest| -// fixed to |EVP_sha1|. +// PKCS5_PBKDF2_HMAC_SHA1 is the same as PKCS5_PBKDF2_HMAC, but with `digest` +// fixed to `EVP_sha1`. OPENSSL_EXPORT int PKCS5_PBKDF2_HMAC_SHA1(const char *password, size_t password_len, const uint8_t *salt, size_t salt_len, uint32_t iterations, size_t key_len, uint8_t *out_key); -// EVP_PBE_scrypt expands |password| into a secret key of length |key_len| using -// scrypt, as described in RFC 7914, and writes the result to |out_key|. It +// EVP_PBE_scrypt expands `password` into a secret key of length `key_len` using +// scrypt, as described in RFC 7914, and writes the result to `out_key`. It // returns one on success and zero on allocation failure, if the memory required -// for the operation exceeds |max_mem|, or if any of the parameters are invalid +// for the operation exceeds `max_mem`, or if any of the parameters are invalid // as described below. // -// |N|, |r|, and |p| are as described in RFC 7914 section 6. They determine the -// cost of the operation. If |max_mem| is zero, a default limit of 65MiB will be +// `N`, `r`, and `p` are as described in RFC 7914 section 6. They determine the +// cost of the operation. If `max_mem` is zero, a default limit of 65MiB will be // used. // // The parameters are considered invalid under any of the following conditions: -// - |r| or |p| are zero -// - |p| > (2^30 - 1) / |r| -// - |N| is not a power of two -// - |N| > 2^32 -// - |N| > 2^(128 * |r| / 8) +// - `r` or `p` are zero +// - `p` > (2^30 - 1) / `r` +// - `N` is not a power of two +// - `N` > 2^32 +// - `N` > 2^(128 * `r` / 8) OPENSSL_EXPORT int EVP_PBE_scrypt(const char *password, size_t password_len, const uint8_t *salt, size_t salt_len, uint64_t N, uint64_t r, uint64_t p, @@ -716,72 +721,72 @@ OPENSSL_EXPORT int EVP_PBE_scrypt(const char *password, size_t password_len, // Operations. // -// |EVP_PKEY_CTX| objects hold the context for an operation (e.g. signing or -// encrypting) that uses an |EVP_PKEY|. They are used to configure +// `EVP_PKEY_CTX` objects hold the context for an operation (e.g. signing or +// encrypting) that uses an `EVP_PKEY`. They are used to configure // algorithm-specific parameters for the operation before performing the // operation. The general pattern for performing an operation in EVP is: // -// 1. Construct an |EVP_PKEY_CTX|, either with |EVP_PKEY_CTX_new| (operations -// using a key, like signing) or |EVP_PKEY_CTX_new_id| (operations not using +// 1. Construct an `EVP_PKEY_CTX`, either with `EVP_PKEY_CTX_new` (operations +// using a key, like signing) or `EVP_PKEY_CTX_new_id` (operations not using // an existing key, like key generation). // -// 2. Initialize it for an operation. For example, |EVP_PKEY_sign_init| -// initializes an |EVP_PKEY_CTX| for signing. +// 2. Initialize it for an operation. For example, `EVP_PKEY_sign_init` +// initializes an `EVP_PKEY_CTX` for signing. // // 3. Configure algorithm-specific parameters for the operation by calling -// control functions on the |EVP_PKEY_CTX|. Some functions are generic, such -// as |EVP_PKEY_CTX_set_signature_md|, and some are specific to an algorithm, -// such as |EVP_PKEY_CTX_set_rsa_padding|. +// control functions on the `EVP_PKEY_CTX`. Some functions are generic, such +// as `EVP_PKEY_CTX_set_signature_md`, and some are specific to an algorithm, +// such as `EVP_PKEY_CTX_set_rsa_padding`. // -// 4. Perform the operation. For example, |EVP_PKEY_sign| signs with the +// 4. Perform the operation. For example, `EVP_PKEY_sign` signs with the // corresponding parameters. // -// 5. Release the |EVP_PKEY_CTX| with |EVP_PKEY_CTX_free|. +// 5. Release the `EVP_PKEY_CTX` with `EVP_PKEY_CTX_free`. // -// Each |EVP_PKEY| algorithm interprets operations and parameters differently. +// Each `EVP_PKEY` algorithm interprets operations and parameters differently. // Not all algorithms support all operations. Functions will fail if the // algorithm does not support the parameter or operation. -// EVP_PKEY_CTX_new allocates a fresh |EVP_PKEY_CTX| for use with |pkey|. It +// EVP_PKEY_CTX_new allocates a fresh `EVP_PKEY_CTX` for use with `pkey`. It // returns the context or NULL on error. -OPENSSL_EXPORT EVP_PKEY_CTX *EVP_PKEY_CTX_new(EVP_PKEY *pkey, ENGINE *e); +OPENSSL_EXPORT EVP_PKEY_CTX *EVP_PKEY_CTX_new(const EVP_PKEY *pkey, ENGINE *e); -// EVP_PKEY_CTX_new_id allocates a fresh |EVP_PKEY_CTX| for a key of type |id| -// (e.g. |EVP_PKEY_HMAC|). This can be used for key generation where -// |EVP_PKEY_CTX_new| can't be used because there isn't an |EVP_PKEY| to pass +// EVP_PKEY_CTX_new_id allocates a fresh `EVP_PKEY_CTX` for a key of type `id` +// (e.g. `EVP_PKEY_HMAC`). This can be used for key generation where +// `EVP_PKEY_CTX_new` can't be used because there isn't an `EVP_PKEY` to pass // it. It returns the context or NULL on error. // -// For key generation, prefer to use |EVP_PKEY_generate_from_alg|. +// For key generation, prefer to use `EVP_PKEY_generate_from_alg`. OPENSSL_EXPORT EVP_PKEY_CTX *EVP_PKEY_CTX_new_id(int id, ENGINE *e); -// EVP_PKEY_CTX_free frees |ctx| and the data it owns. +// EVP_PKEY_CTX_free frees `ctx` and the data it owns. OPENSSL_EXPORT void EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx); -// EVP_PKEY_CTX_dup allocates a fresh |EVP_PKEY_CTX| and sets it equal to the -// state of |ctx|. It returns the fresh |EVP_PKEY_CTX| or NULL on error. +// EVP_PKEY_CTX_dup allocates a fresh `EVP_PKEY_CTX` and sets it equal to the +// state of `ctx`. It returns the fresh `EVP_PKEY_CTX` or NULL on error. OPENSSL_EXPORT EVP_PKEY_CTX *EVP_PKEY_CTX_dup(EVP_PKEY_CTX *ctx); -// EVP_PKEY_CTX_get0_pkey returns the |EVP_PKEY| associated with |ctx|. -OPENSSL_EXPORT EVP_PKEY *EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx); +// EVP_PKEY_CTX_get0_pkey returns the `EVP_PKEY` associated with `ctx`. +OPENSSL_EXPORT EVP_PKEY *EVP_PKEY_CTX_get0_pkey(const EVP_PKEY_CTX *ctx); -// EVP_PKEY_sign_init initialises an |EVP_PKEY_CTX| for a signing operation. It -// should be called before |EVP_PKEY_sign|. +// EVP_PKEY_sign_init initialises an `EVP_PKEY_CTX` for a signing operation. It +// should be called before `EVP_PKEY_sign`. // // It returns one on success or zero on error. OPENSSL_EXPORT int EVP_PKEY_sign_init(EVP_PKEY_CTX *ctx); -// EVP_PKEY_sign signs |digest_len| bytes from |digest| using |ctx|. If |sig| is -// NULL, the maximum size of the signature is written to |out_sig_len|. -// Otherwise, |*sig_len| must contain the number of bytes of space available at -// |sig|. If sufficient, the signature will be written to |sig| and |*sig_len| +// EVP_PKEY_sign signs `digest_len` bytes from `digest` using `ctx`. If `sig` is +// NULL, the maximum size of the signature is written to `out_sig_len`. +// Otherwise, `*sig_len` must contain the number of bytes of space available at +// `sig`. If sufficient, the signature will be written to `sig` and `*sig_len` // updated with the true length. This function will fail for signature // algorithms like Ed25519 that do not support signing pre-hashed inputs. // -// WARNING: |digest| must be the output of some hash function on the data to be +// WARNING: `digest` must be the output of some hash function on the data to be // signed. Passing unhashed inputs will not result in a secure signature scheme. -// Use |EVP_DigestSignInit| to sign an unhashed input. +// Use `EVP_DigestSignInit` to sign an unhashed input. // -// WARNING: Setting |sig| to NULL only gives the maximum size of the +// WARNING: Setting `sig` to NULL only gives the maximum size of the // signature. The actual signature may be smaller. // // It returns one on success or zero on error. (Note: this differs from @@ -790,19 +795,19 @@ OPENSSL_EXPORT int EVP_PKEY_sign(EVP_PKEY_CTX *ctx, uint8_t *sig, size_t *sig_len, const uint8_t *digest, size_t digest_len); -// EVP_PKEY_verify_init initialises an |EVP_PKEY_CTX| for a signature -// verification operation. It should be called before |EVP_PKEY_verify|. +// EVP_PKEY_verify_init initialises an `EVP_PKEY_CTX` for a signature +// verification operation. It should be called before `EVP_PKEY_verify`. // // It returns one on success or zero on error. OPENSSL_EXPORT int EVP_PKEY_verify_init(EVP_PKEY_CTX *ctx); -// EVP_PKEY_verify verifies that |sig_len| bytes from |sig| are a valid -// signature for |digest|. This function will fail for signature +// EVP_PKEY_verify verifies that `sig_len` bytes from `sig` are a valid +// signature for `digest`. This function will fail for signature // algorithms like Ed25519 that do not support signing pre-hashed inputs. // -// WARNING: |digest| must be the output of some hash function on the data to be +// WARNING: `digest` must be the output of some hash function on the data to be // verified. Passing unhashed inputs will not result in a secure signature -// scheme. Use |EVP_DigestVerifyInit| to verify a signature given the unhashed +// scheme. Use `EVP_DigestVerifyInit` to verify a signature given the unhashed // input. // // It returns one on success or zero on error. @@ -810,19 +815,19 @@ OPENSSL_EXPORT int EVP_PKEY_verify(EVP_PKEY_CTX *ctx, const uint8_t *sig, size_t sig_len, const uint8_t *digest, size_t digest_len); -// EVP_PKEY_encrypt_init initialises an |EVP_PKEY_CTX| for an encryption -// operation. It should be called before |EVP_PKEY_encrypt|. +// EVP_PKEY_encrypt_init initialises an `EVP_PKEY_CTX` for an encryption +// operation. It should be called before `EVP_PKEY_encrypt`. // // It returns one on success or zero on error. OPENSSL_EXPORT int EVP_PKEY_encrypt_init(EVP_PKEY_CTX *ctx); -// EVP_PKEY_encrypt encrypts |in_len| bytes from |in|. If |out| is NULL, the -// maximum size of the ciphertext is written to |out_len|. Otherwise, |*out_len| -// must contain the number of bytes of space available at |out|. If sufficient, -// the ciphertext will be written to |out| and |*out_len| updated with the true +// EVP_PKEY_encrypt encrypts `in_len` bytes from `in`. If `out` is NULL, the +// maximum size of the ciphertext is written to `out_len`. Otherwise, `*out_len` +// must contain the number of bytes of space available at `out`. If sufficient, +// the ciphertext will be written to `out` and `*out_len` updated with the true // length. // -// WARNING: Setting |out| to NULL only gives the maximum size of the +// WARNING: Setting `out` to NULL only gives the maximum size of the // ciphertext. The actual ciphertext may be smaller. // // It returns one on success or zero on error. @@ -830,19 +835,19 @@ OPENSSL_EXPORT int EVP_PKEY_encrypt(EVP_PKEY_CTX *ctx, uint8_t *out, size_t *out_len, const uint8_t *in, size_t in_len); -// EVP_PKEY_decrypt_init initialises an |EVP_PKEY_CTX| for a decryption -// operation. It should be called before |EVP_PKEY_decrypt|. +// EVP_PKEY_decrypt_init initialises an `EVP_PKEY_CTX` for a decryption +// operation. It should be called before `EVP_PKEY_decrypt`. // // It returns one on success or zero on error. OPENSSL_EXPORT int EVP_PKEY_decrypt_init(EVP_PKEY_CTX *ctx); -// EVP_PKEY_decrypt decrypts |in_len| bytes from |in|. If |out| is NULL, the -// maximum size of the plaintext is written to |out_len|. Otherwise, |*out_len| -// must contain the number of bytes of space available at |out|. If sufficient, -// the ciphertext will be written to |out| and |*out_len| updated with the true +// EVP_PKEY_decrypt decrypts `in_len` bytes from `in`. If `out` is NULL, the +// maximum size of the plaintext is written to `out_len`. Otherwise, `*out_len` +// must contain the number of bytes of space available at `out`. If sufficient, +// the ciphertext will be written to `out` and `*out_len` updated with the true // length. // -// WARNING: Setting |out| to NULL only gives the maximum size of the +// WARNING: Setting `out` to NULL only gives the maximum size of the // plaintext. The actual plaintext may be smaller. // // It returns one on success or zero on error. @@ -850,8 +855,8 @@ OPENSSL_EXPORT int EVP_PKEY_decrypt(EVP_PKEY_CTX *ctx, uint8_t *out, size_t *out_len, const uint8_t *in, size_t in_len); -// EVP_PKEY_verify_recover_init initialises an |EVP_PKEY_CTX| for a public-key -// decryption operation. It should be called before |EVP_PKEY_verify_recover|. +// EVP_PKEY_verify_recover_init initialises an `EVP_PKEY_CTX` for a public-key +// decryption operation. It should be called before `EVP_PKEY_verify_recover`. // // Public-key decryption is a very obscure operation that is only implemented // by RSA keys. It is effectively a signature verification operation that @@ -861,16 +866,16 @@ OPENSSL_EXPORT int EVP_PKEY_decrypt(EVP_PKEY_CTX *ctx, uint8_t *out, // It returns one on success or zero on error. OPENSSL_EXPORT int EVP_PKEY_verify_recover_init(EVP_PKEY_CTX *ctx); -// EVP_PKEY_verify_recover decrypts |sig_len| bytes from |sig|. If |out| is -// NULL, the maximum size of the plaintext is written to |out_len|. Otherwise, -// |*out_len| must contain the number of bytes of space available at |out|. If -// sufficient, the ciphertext will be written to |out| and |*out_len| updated +// EVP_PKEY_verify_recover decrypts `sig_len` bytes from `sig`. If `out` is +// NULL, the maximum size of the plaintext is written to `out_len`. Otherwise, +// `*out_len` must contain the number of bytes of space available at `out`. If +// sufficient, the ciphertext will be written to `out` and `*out_len` updated // with the true length. // -// WARNING: Setting |out| to NULL only gives the maximum size of the +// WARNING: Setting `out` to NULL only gives the maximum size of the // plaintext. The actual plaintext may be smaller. // -// See the warning about this operation in |EVP_PKEY_verify_recover_init|. It +// See the warning about this operation in `EVP_PKEY_verify_recover_init`. It // is probably not what you want. // // It returns one on success or zero on error. @@ -878,81 +883,82 @@ OPENSSL_EXPORT int EVP_PKEY_verify_recover(EVP_PKEY_CTX *ctx, uint8_t *out, size_t *out_len, const uint8_t *sig, size_t siglen); -// EVP_PKEY_derive_init initialises an |EVP_PKEY_CTX| for a key derivation -// operation. It should be called before |EVP_PKEY_derive_set_peer| and -// |EVP_PKEY_derive|. +// EVP_PKEY_derive_init initialises an `EVP_PKEY_CTX` for a key derivation +// operation. It should be called before `EVP_PKEY_derive_set_peer` and +// `EVP_PKEY_derive`. // // It returns one on success or zero on error. OPENSSL_EXPORT int EVP_PKEY_derive_init(EVP_PKEY_CTX *ctx); // EVP_PKEY_derive_set_peer sets the peer's key to be used for key derivation -// by |ctx| to |peer|. It should be called after |EVP_PKEY_derive_init|. (For +// by `ctx` to `peer`. It should be called after `EVP_PKEY_derive_init`. (For // example, this is used to set the peer's key in (EC)DH.) It returns one on // success and zero on error. -OPENSSL_EXPORT int EVP_PKEY_derive_set_peer(EVP_PKEY_CTX *ctx, EVP_PKEY *peer); +OPENSSL_EXPORT int EVP_PKEY_derive_set_peer(EVP_PKEY_CTX *ctx, + const EVP_PKEY *peer); -// EVP_PKEY_derive derives a shared key from |ctx|. If |key| is non-NULL then, -// on entry, |out_key_len| must contain the amount of space at |key|. If -// sufficient then the shared key will be written to |key| and |*out_key_len| -// will be set to the length. If |key| is NULL then |out_key_len| will be set to +// EVP_PKEY_derive derives a shared key from `ctx`. If `key` is non-NULL then, +// on entry, `out_key_len` must contain the amount of space at `key`. If +// sufficient then the shared key will be written to `key` and `*out_key_len` +// will be set to the length. If `key` is NULL then `out_key_len` will be set to // the maximum length. // -// WARNING: Setting |out| to NULL only gives the maximum size of the key. The +// WARNING: Setting `out` to NULL only gives the maximum size of the key. The // actual key may be smaller. // // It returns one on success and zero on error. OPENSSL_EXPORT int EVP_PKEY_derive(EVP_PKEY_CTX *ctx, uint8_t *key, size_t *out_key_len); -// EVP_PKEY_keygen_init initialises an |EVP_PKEY_CTX| for a key generation -// operation. It should be called before |EVP_PKEY_keygen|. +// EVP_PKEY_keygen_init initialises an `EVP_PKEY_CTX` for a key generation +// operation. It should be called before `EVP_PKEY_keygen`. // // It returns one on success or zero on error. OPENSSL_EXPORT int EVP_PKEY_keygen_init(EVP_PKEY_CTX *ctx); // EVP_PKEY_keygen performs a key generation operation using the values from -// |ctx|. If |*out_pkey| is non-NULL, it overwrites |*out_pkey| with the -// resulting key. Otherwise, it sets |*out_pkey| to a newly-allocated |EVP_PKEY| +// `ctx`. If `*out_pkey` is non-NULL, it overwrites `*out_pkey` with the +// resulting key. Otherwise, it sets `*out_pkey` to a newly-allocated `EVP_PKEY` // containing the result. It returns one on success or zero on error. OPENSSL_EXPORT int EVP_PKEY_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY **out_pkey); -// EVP_PKEY_paramgen_init initialises an |EVP_PKEY_CTX| for a parameter -// generation operation. It should be called before |EVP_PKEY_paramgen|. +// EVP_PKEY_paramgen_init initialises an `EVP_PKEY_CTX` for a parameter +// generation operation. It should be called before `EVP_PKEY_paramgen`. // // It returns one on success or zero on error. OPENSSL_EXPORT int EVP_PKEY_paramgen_init(EVP_PKEY_CTX *ctx); // EVP_PKEY_paramgen performs a parameter generation using the values from -// |ctx|. If |*out_pkey| is non-NULL, it overwrites |*out_pkey| with the -// resulting parameters, but no key. Otherwise, it sets |*out_pkey| to a -// newly-allocated |EVP_PKEY| containing the result. It returns one on success +// `ctx`. If `*out_pkey` is non-NULL, it overwrites `*out_pkey` with the +// resulting parameters, but no key. Otherwise, it sets `*out_pkey` to a +// newly-allocated `EVP_PKEY` containing the result. It returns one on success // or zero on error. OPENSSL_EXPORT int EVP_PKEY_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY **out_pkey); -// EVP_PKEY_encapsulate_init initialises an |EVP_PKEY_CTX| for an encapsulate -// operation. It should be called before |EVP_PKEY_encapsulate|. |params| is +// EVP_PKEY_encapsulate_init initialises an `EVP_PKEY_CTX` for an encapsulate +// operation. It should be called before `EVP_PKEY_encapsulate`. `params` is // included for OpenSSL compatibility, but this parameter should be NULL or have -// |OSSL_PARAM_END| as its first element. +// `OSSL_PARAM_END` as its first element. // // It returns one on success or zero on error. OPENSSL_EXPORT int EVP_PKEY_encapsulate_init(EVP_PKEY_CTX *ctx, const OSSL_PARAM *params); -// EVP_PKEY_encapsulate implements public key encapsulation using |ctx|. It +// EVP_PKEY_encapsulate implements public key encapsulation using `ctx`. It // either performs the operation or returns the maximum output sizes, depending -// on whether |out_ciphertext| is NULL: +// on whether `out_ciphertext` is NULL: // -// If |out_ciphertext| is NULL, it writes the maximum ciphertext length to -// |*out_ciphertext_len| and the maximum shared secret length to -// |*out_secret_len|. Either of |out_ciphertext_len| or |out_secret_len| may be +// If `out_ciphertext` is NULL, it writes the maximum ciphertext length to +// `*out_ciphertext_len` and the maximum shared secret length to +// `*out_secret_len`. Either of `out_ciphertext_len` or `out_secret_len` may be // NULL to ignore the corresponding output. // -// If |out_ciphertext| is non-NULL, it performs the operation and, on success, -// writes the ciphertext to |out_ciphertext|, the ciphertext size to -// |out_ciphertext_len|, the shared secret to |out_secret|, and the shared -// secret length to |out_secret_len|. On input, |*out_ciphertext_len| and -// |*out_secret_len| must contain the amount of space available in -// |out_ciphertext| and |out_secret|, respectively. If there is insufficient +// If `out_ciphertext` is non-NULL, it performs the operation and, on success, +// writes the ciphertext to `out_ciphertext`, the ciphertext size to +// `out_ciphertext_len`, the shared secret to `out_secret`, and the shared +// secret length to `out_secret_len`. On input, `*out_ciphertext_len` and +// `*out_secret_len` must contain the amount of space available in +// `out_ciphertext` and `out_secret`, respectively. If there is insufficient // space to write the output, the operation will fail. // // In both modes, this function returns one on success or zero on error. @@ -962,26 +968,26 @@ OPENSSL_EXPORT int EVP_PKEY_encapsulate(EVP_PKEY_CTX *ctx, uint8_t *out_secret, size_t *out_secret_len); -// EVP_PKEY_decapsulate_init initialises an |EVP_PKEY_CTX| for a decapsulate -// operation. It should be called before |EVP_PKEY_decapsulate|. |params| is +// EVP_PKEY_decapsulate_init initialises an `EVP_PKEY_CTX` for a decapsulate +// operation. It should be called before `EVP_PKEY_decapsulate`. `params` is // included for OpenSSL compatibility, but this parameter should be NULL or have -// |OSSL_PARAM_END| as its first element. +// `OSSL_PARAM_END` as its first element. // // It returns one on success or zero on error. OPENSSL_EXPORT int EVP_PKEY_decapsulate_init(EVP_PKEY_CTX *ctx, const OSSL_PARAM *params); -// EVP_PKEY_decapsulate implements private key decapsulation using |ctx|. -// |ciphertext| and |ciphertext_len| specify the ciphertext to be decapsulated. -// If |out_secret| is NULL, it writes the maximum size of the shared secret -// output to |*out_secret_len| and returns one. Otherwise, |*out_secret_len| -// must contain the number of bytes of space available at |out_secret|. If the +// EVP_PKEY_decapsulate implements private key decapsulation using `ctx`. +// `ciphertext` and `ciphertext_len` specify the ciphertext to be decapsulated. +// If `out_secret` is NULL, it writes the maximum size of the shared secret +// output to `*out_secret_len` and returns one. Otherwise, `*out_secret_len` +// must contain the number of bytes of space available at `out_secret`. If the // space is insufficient, this function returns zero. If the space is -// sufficient, the decapsulated shared secret will be written to |out_secret| -// and the size of the output to |out_secret_len|, and this function will return -// one. If |ciphertext| has been corrupted, the function may fail or it may +// sufficient, the decapsulated shared secret will be written to `out_secret` +// and the size of the output to `out_secret_len`, and this function will return +// one. If `ciphertext` has been corrupted, the function may fail or it may // output a shared secret that appears to be random. Any subsequent symmetric -// encryption using |out_secret| must use an authenticated encryption scheme to +// encryption using `out_secret` must use an authenticated encryption scheme to // discover the decapsulation failure. OPENSSL_EXPORT int EVP_PKEY_decapsulate(EVP_PKEY_CTX *ctx, uint8_t *out_secret, size_t *out_secret_len, @@ -991,18 +997,18 @@ OPENSSL_EXPORT int EVP_PKEY_decapsulate(EVP_PKEY_CTX *ctx, uint8_t *out_secret, // Generic control functions. -// EVP_PKEY_CTX_set_signature_md sets |md| as the digest to be used in a +// EVP_PKEY_CTX_set_signature_md sets `md` as the digest to be used in a // signature operation. It returns one on success or zero on error. OPENSSL_EXPORT int EVP_PKEY_CTX_set_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD *md); -// EVP_PKEY_CTX_get_signature_md sets |*out_md| to the digest to be used in a +// EVP_PKEY_CTX_get_signature_md sets `*out_md` to the digest to be used in a // signature operation. It returns one on success or zero on error. OPENSSL_EXPORT int EVP_PKEY_CTX_get_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD **out_md); // EVP_PKEY_CTX_set1_signature_context_string sets the context string for a -// signature or verification operation to |context|. It returns one success and +// signature or verification operation to `context`. It returns one success and // zero on error. The context string is an additional input to some signature // algorithms, such as ML-DSA, to separate different uses of the same key. This // is known as domain separation. Section 8.3 of RFC 8032 provides some @@ -1019,41 +1025,85 @@ OPENSSL_EXPORT int EVP_PKEY_CTX_set1_signature_context_string( // RSA specific control functions. // EVP_RSA_gen generates a new RSA key with the specified number of bits. It -// returns a newly-allocated |EVP_PKEY| or nullptr on error. +// returns a newly-allocated `EVP_PKEY` or nullptr on error. // // This function sets the public exponent to the recommended value of 65537, or -// |RSA_F4|. To use a less common value, instead use -// |EVP_PKEY_CTX_set_rsa_keygen_pubexp| and |EVP_PKEY_keygen|. +// `RSA_F4`. To use a less common value, instead use +// `EVP_PKEY_CTX_set_rsa_keygen_pubexp` and `EVP_PKEY_keygen`. OPENSSL_EXPORT EVP_PKEY *EVP_RSA_gen(unsigned bits); +// EVP_PKEY_from_rsa_public_key decodes `len` bytes from `in` as a DER-encoded +// RSAPublicKey structure (RFC 8017) of algorithm `alg`. It returns a +// newly-allocated `EVP_PKEY` on success or NULL on error. `alg` must be an +// RSA-based algorithm, i.e. `EVP_pkey_rsa` or one of `EVP_pkey_rsa_pss_*`. If +// unsure, use `EVP_pkey_rsa`. +OPENSSL_EXPORT EVP_PKEY *EVP_PKEY_from_rsa_public_key(const EVP_PKEY_ALG *alg, + const uint8_t *in, + size_t len); + +// EVP_PKEY_from_rsa_private_key decodes `len` bytes from `in` as a DER-encoded +// RSAPrivateKey structure (RFC 8017) of algorithm `alg`. It returns a +// newly-allocated `EVP_PKEY` on success or NULL on error. `alg` must be an +// RSA-based algorithm, i.e. `EVP_pkey_rsa` or one of `EVP_pkey_rsa_pss_*`. If +// unsure, use `EVP_pkey_rsa`. +OPENSSL_EXPORT EVP_PKEY *EVP_PKEY_from_rsa_private_key(const EVP_PKEY_ALG *alg, + const uint8_t *in, + size_t len); + +// EVP_PKEY_marshal_rsa_public_key marshals `key` as a DER-encoded RSAPublicKey +// structure (RFC 8017) and writes the result to `cbb`. It returns one on +// success and zero on error. If `key` is not of type `EVP_PKEY_RSA` or +// `EVP_PKEY_RSA_PSS`, it returns zero. +// +// WARNING: If `key` is of type `EVP_PKEY_RSA_PSS`, this representation does not +// encode the key's RSA-PSS restrictions. If decoding with +// `EVP_PKEY_from_rsa_public_key`, callers are expected to pass an appropriate +// `EVP_PKEY_ALG` out of band. If decoding into a low-level `RSA` object, the +// result will be an unrestricted key. +OPENSSL_EXPORT int EVP_PKEY_marshal_rsa_public_key(CBB *cbb, + const EVP_PKEY *key); + +// EVP_PKEY_marshal_rsa_private_key marshals `key` as a DER-encoded +// RSAPrivateKey structure (RFC 8017) and writes the result to `cbb`. It returns +// one on success and zero on error. If `key` is not of type `EVP_PKEY_RSA` or +// `EVP_PKEY_RSA_PSS`, it returns zero. +// +// WARNING: If `key` is of type `EVP_PKEY_RSA_PSS`, this representation does not +// encode the key's RSA-PSS restrictions. If decoding with +// `EVP_PKEY_from_rsa_private_key`, callers are expected to pass an appropriate +// `EVP_PKEY_ALG` out of band. If decoding into a low-level `RSA` object, the +// result will be an unrestricted key. +OPENSSL_EXPORT int EVP_PKEY_marshal_rsa_private_key(CBB *cbb, + const EVP_PKEY *key); + // EVP_PKEY_CTX_set_rsa_padding sets the padding type to use. It should be one -// of the |RSA_*_PADDING| values. Returns one on success or zero on error. By -// default, the padding is |RSA_PKCS1_PADDING|. +// of the `RSA_*_PADDING` values. Returns one on success or zero on error. By +// default, the padding is `RSA_PKCS1_PADDING`. OPENSSL_EXPORT int EVP_PKEY_CTX_set_rsa_padding(EVP_PKEY_CTX *ctx, int padding); -// EVP_PKEY_CTX_get_rsa_padding sets |*out_padding| to the current padding -// value, which is one of the |RSA_*_PADDING| values. Returns one on success or +// EVP_PKEY_CTX_get_rsa_padding sets `*out_padding` to the current padding +// value, which is one of the `RSA_*_PADDING` values. Returns one on success or // zero on error. OPENSSL_EXPORT int EVP_PKEY_CTX_get_rsa_padding(EVP_PKEY_CTX *ctx, int *out_padding); // EVP_PKEY_CTX_set_rsa_pss_saltlen sets the length of the salt in a PSS-padded -// signature. A value of |RSA_PSS_SALTLEN_DIGEST| causes the salt to be the same -// length as the digest in the signature. A value of |RSA_PSS_SALTLEN_AUTO| +// signature. A value of `RSA_PSS_SALTLEN_DIGEST` causes the salt to be the same +// length as the digest in the signature. A value of `RSA_PSS_SALTLEN_AUTO` // causes the salt to be the maximum length that will fit when signing and // recovered from the signature when verifying. Otherwise the value gives the // size of the salt in bytes. // -// If unsure, use |RSA_PSS_SALTLEN_DIGEST|, which is the default. Note this -// differs from OpenSSL, which defaults to |RSA_PSS_SALTLEN_AUTO|. +// If unsure, use `RSA_PSS_SALTLEN_DIGEST`, which is the default. Note this +// differs from OpenSSL, which defaults to `RSA_PSS_SALTLEN_AUTO`. // // Returns one on success or zero on error. OPENSSL_EXPORT int EVP_PKEY_CTX_set_rsa_pss_saltlen(EVP_PKEY_CTX *ctx, int salt_len); -// EVP_PKEY_CTX_get_rsa_pss_saltlen sets |*out_salt_len| to the salt length of +// EVP_PKEY_CTX_get_rsa_pss_saltlen sets `*out_salt_len` to the salt length of // a PSS-padded signature. See the documentation for -// |EVP_PKEY_CTX_set_rsa_pss_saltlen| for details of the special values that it +// `EVP_PKEY_CTX_set_rsa_pss_saltlen` for details of the special values that it // can take. // // Returns one on success or zero on error. @@ -1066,14 +1116,14 @@ OPENSSL_EXPORT int EVP_PKEY_CTX_get_rsa_pss_saltlen(EVP_PKEY_CTX *ctx, OPENSSL_EXPORT int EVP_PKEY_CTX_set_rsa_keygen_bits(EVP_PKEY_CTX *ctx, int bits); -// EVP_PKEY_CTX_set_rsa_keygen_pubexp sets |e| as the public exponent for key -// generation. Returns one on success or zero on error. On success, |ctx| takes -// ownership of |e|. The library will then call |BN_free| on |e| when |ctx| is +// EVP_PKEY_CTX_set_rsa_keygen_pubexp sets `e` as the public exponent for key +// generation. Returns one on success or zero on error. On success, `ctx` takes +// ownership of `e`. The library will then call `BN_free` on `e` when `ctx` is // destroyed. OPENSSL_EXPORT int EVP_PKEY_CTX_set_rsa_keygen_pubexp(EVP_PKEY_CTX *ctx, BIGNUM *e); -// EVP_PKEY_CTX_set_rsa_oaep_md sets |md| as the digest used in OAEP padding. +// EVP_PKEY_CTX_set_rsa_oaep_md sets `md` as the digest used in OAEP padding. // Returns one on success or zero on error. If unset, the default is SHA-1. // Callers are recommended to overwrite this default. // @@ -1081,35 +1131,35 @@ OPENSSL_EXPORT int EVP_PKEY_CTX_set_rsa_keygen_pubexp(EVP_PKEY_CTX *ctx, OPENSSL_EXPORT int EVP_PKEY_CTX_set_rsa_oaep_md(EVP_PKEY_CTX *ctx, const EVP_MD *md); -// EVP_PKEY_CTX_get_rsa_oaep_md sets |*out_md| to the digest function used in +// EVP_PKEY_CTX_get_rsa_oaep_md sets `*out_md` to the digest function used in // OAEP padding. Returns one on success or zero on error. OPENSSL_EXPORT int EVP_PKEY_CTX_get_rsa_oaep_md(EVP_PKEY_CTX *ctx, const EVP_MD **out_md); -// EVP_PKEY_CTX_set_rsa_mgf1_md sets |md| as the digest used in MGF1. Returns +// EVP_PKEY_CTX_set_rsa_mgf1_md sets `md` as the digest used in MGF1. Returns // one on success or zero on error. // -// If unset, the default is the signing hash for |RSA_PKCS1_PSS_PADDING| and the -// OAEP hash for |RSA_PKCS1_OAEP_PADDING|. Callers are recommended to use this +// If unset, the default is the signing hash for `RSA_PKCS1_PSS_PADDING` and the +// OAEP hash for `RSA_PKCS1_OAEP_PADDING`. Callers are recommended to use this // default and not call this function. OPENSSL_EXPORT int EVP_PKEY_CTX_set_rsa_mgf1_md(EVP_PKEY_CTX *ctx, const EVP_MD *md); -// EVP_PKEY_CTX_get_rsa_mgf1_md sets |*out_md| to the digest function used in +// EVP_PKEY_CTX_get_rsa_mgf1_md sets `*out_md` to the digest function used in // MGF1. Returns one on success or zero on error. OPENSSL_EXPORT int EVP_PKEY_CTX_get_rsa_mgf1_md(EVP_PKEY_CTX *ctx, const EVP_MD **out_md); -// EVP_PKEY_CTX_set0_rsa_oaep_label sets |label_len| bytes from |label| as the -// label used in OAEP. DANGER: On success, this call takes ownership of |label| -// and will call |OPENSSL_free| on it when |ctx| is destroyed. +// EVP_PKEY_CTX_set0_rsa_oaep_label sets `label_len` bytes from `label` as the +// label used in OAEP. DANGER: On success, this call takes ownership of `label` +// and will call `OPENSSL_free` on it when `ctx` is destroyed. // // Returns one on success or zero on error. OPENSSL_EXPORT int EVP_PKEY_CTX_set0_rsa_oaep_label(EVP_PKEY_CTX *ctx, uint8_t *label, size_t label_len); -// EVP_PKEY_CTX_get0_rsa_oaep_label sets |*out_label| to point to the internal +// EVP_PKEY_CTX_get0_rsa_oaep_label sets `*out_label` to point to the internal // buffer containing the OAEP label (which may be NULL) and returns the length // of the label or a negative value on error. // @@ -1120,16 +1170,60 @@ OPENSSL_EXPORT int EVP_PKEY_CTX_get0_rsa_oaep_label(EVP_PKEY_CTX *ctx, // EC specific control functions. -// EVP_PKEY_get_ec_curve_nid returns |pkey|'s curve as a NID constant, such as -// |NID_X9_62_prime256v1|, or |NID_undef| if |pkey| is not an EC key. +// The following functions decode `len` bytes from `in` as an EC public key in +// compressed or uncompressed X9.62 format, respectively. The EC curve is +// determined by `alg`, which must be an EC algorithm (i.e. one of +// `EVP_pkey_ec_*`). +// +// The functions return a newly-allocated `EVP_PKEY` on success or NULL on +// error. +// +// To support both uncompressed and compressed forms, call +// `EVP_PKEY_from_ec_uncompressed_point` when the input begins with 0x04 and +// `EVP_PKEY_from_ec_compressed_point` otherwise. Uncompressed form is more +// common. +OPENSSL_EXPORT EVP_PKEY *EVP_PKEY_from_ec_uncompressed_point( + const EVP_PKEY_ALG *alg, const uint8_t *in, size_t len); +OPENSSL_EXPORT EVP_PKEY *EVP_PKEY_from_ec_compressed_point( + const EVP_PKEY_ALG *alg, const uint8_t *in, size_t len); + +// The following functions marshal `key`'s EC public key in compressed or +// uncompressed X9.62 format, respectively, and write the result to `cbb`. They +// return one on success and zero on error. It is an error if `key` is not an EC +// key. +OPENSSL_EXPORT int EVP_PKEY_marshal_ec_uncompressed_point(CBB *cbb, + const EVP_PKEY *key); +OPENSSL_EXPORT int EVP_PKEY_marshal_ec_compressed_point(CBB *cbb, + const EVP_PKEY *key); + +// EVP_PKEY_from_ec_private_scalar decodes `len` bytes from `in` as an EC +// private key, encoded as a big-endian, zero-padded integer. The EC curve is +// determined by `alg`, which must be an EC algorithm (i.e. one of +// `EVP_pkey_ec_*`). The input must be fully reduced and padded to the size of +// the group order (e.g. 32 bytes for P-256) or the import will fail. +// +// This function returns a newly-allocated `EVP_PKEY` on success or NULL on +// error. +OPENSSL_EXPORT EVP_PKEY *EVP_PKEY_from_ec_private_scalar( + const EVP_PKEY_ALG *alg, const uint8_t *in, size_t len); + +// EVP_PKEY_marshal_ec_private_scalar marshals `key`'s private key as a +// big-endian, zero-padded integer EC scalar and writes the result to `cbb`. It +// returns one on success or zero on error. It is an error if `key` is not an EC +// key. +OPENSSL_EXPORT int EVP_PKEY_marshal_ec_private_scalar(CBB *cbb, + const EVP_PKEY *key); + +// EVP_PKEY_get_ec_curve_nid returns `pkey`'s curve as a NID constant, such as +// `NID_X9_62_prime256v1`, or `NID_undef` if `pkey` is not an EC key. OPENSSL_EXPORT int EVP_PKEY_get_ec_curve_nid(const EVP_PKEY *pkey); -// EVP_PKEY_get_ec_point_conv_form returns |pkey|'s point conversion form as a -// |POINT_CONVERSION_*| constant, or zero if |pkey| is not an EC key. +// EVP_PKEY_get_ec_point_conv_form returns `pkey`'s point conversion form as a +// `POINT_CONVERSION_*` constant, or zero if `pkey` is not an EC key. OPENSSL_EXPORT int EVP_PKEY_get_ec_point_conv_form(const EVP_PKEY *pkey); // EVP_PKEY_CTX_set_ec_paramgen_curve_nid sets the curve used for -// |EVP_PKEY_keygen| or |EVP_PKEY_paramgen| operations to |nid|. It returns one +// `EVP_PKEY_keygen` or `EVP_PKEY_paramgen` operations to `nid`. It returns one // on success and zero on error. OPENSSL_EXPORT int EVP_PKEY_CTX_set_ec_paramgen_curve_nid(EVP_PKEY_CTX *ctx, int nid); @@ -1137,14 +1231,14 @@ OPENSSL_EXPORT int EVP_PKEY_CTX_set_ec_paramgen_curve_nid(EVP_PKEY_CTX *ctx, // Diffie-Hellman-specific control functions. -// EVP_PKEY_CTX_set_dh_pad configures configures whether |ctx|, which must be an -// |EVP_PKEY_derive| operation, configures the handling of leading zeros in the -// Diffie-Hellman shared secret. If |pad| is zero, leading zeros are removed -// from the secret. If |pad| is non-zero, the fixed-width shared secret is used +// EVP_PKEY_CTX_set_dh_pad configures whether `ctx`, which must be an +// `EVP_PKEY_derive` operation, configures the handling of leading zeros in the +// Diffie-Hellman shared secret. If `pad` is zero, leading zeros are removed +// from the secret. If `pad` is non-zero, the fixed-width shared secret is used // unmodified, as in PKCS #3. If this function is not called, the default is to // remove leading zeros. // -// WARNING: The behavior when |pad| is zero leaks information about the shared +// WARNING: The behavior when `pad` is zero leaks information about the shared // secret. This may result in side channel attacks such as // https://raccoon-attack.com/, particularly when the same private key is used // for multiple operations. @@ -1156,15 +1250,15 @@ OPENSSL_EXPORT int EVP_PKEY_CTX_set_dh_pad(EVP_PKEY_CTX *ctx, int pad); // Two APIs for working with key encapsulation mechanism (KEM) algorithms are // provided: // -// 1. Create an |EVP_PKEY_CTX|, initialize it for the appropriate KEM operation -// (see |EVP_PKEY_encapsulate_init| and |EVP_PKEY_decapsulate_init|), then +// 1. Create an `EVP_PKEY_CTX`, initialize it for the appropriate KEM operation +// (see `EVP_PKEY_encapsulate_init` and `EVP_PKEY_decapsulate_init`), then // run the operation. This matches the OpenSSL API. // -// 2. Pass an appropriate |EVP_KEM| object to the functions below to use it -// for encapsulation and decapsulation operations with compatible |EVP_PKEY| +// 2. Pass an appropriate `EVP_KEM` object to the functions below to use it +// for encapsulation and decapsulation operations with compatible `EVP_PKEY` // objects. This API requires fewer steps. // -// The |EVP_KEM| API is only compatible with KEMs that use fixed-length +// The `EVP_KEM` API is only compatible with KEMs that use fixed-length // ciphertexts and secrets. // EVP_kem_ml_kem_* implement ML-KEM, defined in FIPS 203. @@ -1175,35 +1269,55 @@ OPENSSL_EXPORT const EVP_KEM *EVP_kem_ml_kem_1024(void); // defined in draft-irtf-cfrg-concrete-hybrid-kems. OPENSSL_EXPORT const EVP_KEM *EVP_kem_xwing(void); -// TODO(crbug.com/449751916): Add more supported KEMs. +// Additional KEMs are defined in hpke.h: See `EVP_kem_dhkem_*`. // EVP_KEM_ciphertext_len returns the fixed length, in bytes, of a ciphertext -// produced and consumed by |kem|. +// produced and consumed by `kem`. OPENSSL_EXPORT size_t EVP_KEM_ciphertext_len(const EVP_KEM *kem); // EVP_KEM_secret_len returns the fixed length, in bytes, of the shared -// secret produced and consumed by |kem|. +// secret produced and consumed by `kem`. OPENSSL_EXPORT size_t EVP_KEM_secret_len(const EVP_KEM *kem); -// EVP_KEM_encap uses |kem| to encapsulate a |peer_key|. It outputs a -// ciphertext of length |ciphertext_len| into |*out_ciphertext| and outputs a -// shared secret of length |secret_len| into |*out_secret|. |peer_key| must be -// a public key of the type expected by |kem|. |ciphertext_len| and -// |secret_len| must match the output of |EVP_KEM_ciphertext_len| and -// |EVP_KEM_secret_len|, respectively, when called with |kem|. This function +// EVP_KEM_entropy_len_for_testing returns the fixed length, in bytes, of the +// external entropy to provide to `EVP_KEM_encap_external_entropy_for_testing`. +// +// WARNING: There should be no reason to use this function in production. +OPENSSL_EXPORT size_t EVP_KEM_entropy_len_for_testing(const EVP_KEM *kem); + +// EVP_KEM_encap uses `kem` to encapsulate a `peer_key`. It outputs a +// ciphertext of length `ciphertext_len` into `*out_ciphertext` and outputs a +// shared secret of length `secret_len` into `*out_secret`. `peer_key` must be +// a public key of the type expected by `kem`. `ciphertext_len` and +// `secret_len` must match the output of `EVP_KEM_ciphertext_len` and +// `EVP_KEM_secret_len`, respectively, when called with `kem`. This function // returns one on success or zero on failure. OPENSSL_EXPORT int EVP_KEM_encap(const EVP_KEM *kem, uint8_t *out_ciphertext, size_t ciphertext_len, uint8_t *out_secret, size_t secret_len, const EVP_PKEY *peer_key); -// EVP_KEM_decap uses |kem| to decapsulate a |ciphertext| of length -// |ciphertext_len|, using |key| as a decapsulation key. It outputs a shared -// secret of length |secret_len| into |*out_secret|. |key| must be a private key -// of the type expected by |kem|. |secret_len| must match the output of -// |EVP_KEM_secret_len| when called with |kem|. This function returns one on -// success or zero on failure. If |ciphertext| has been corrupted, the function +// EVP_KEM_encap_external_entropy_for_testing behaves like `EVP_KEM_encap`, but +// takes `entropy_len` bytes of external `entropy` to behave deterministically +// for testing. The format of `entropy` depends on `kem`. `entropy_len` must +// match the output of `EVP_KEM_entropy_len_for_testing` when called with `kem`. +// +// WARNING: This function is only exported to help callers in testing systems +// against fixed test vectors. It must only be used for this purpose, and not in +// production. If `entropy` is predictable or known to an attacker, security +// properties of the KEM will not hold. +OPENSSL_EXPORT int EVP_KEM_encap_external_entropy_for_testing( + const EVP_KEM *kem, uint8_t *out_ciphertext, size_t ciphertext_len, + uint8_t *out_secret, size_t secret_len, const EVP_PKEY *peer_key, + const uint8_t *entropy, size_t entropy_len); + +// EVP_KEM_decap uses `kem` to decapsulate a `ciphertext` of length +// `ciphertext_len`, using `key` as a decapsulation key. It outputs a shared +// secret of length `secret_len` into `*out_secret`. `key` must be a private key +// of the type expected by `kem`. `secret_len` must match the output of +// `EVP_KEM_secret_len` when called with `kem`. This function returns one on +// success or zero on failure. If `ciphertext` has been corrupted, the function // may fail or it may output a shared secret that appears to be random. Any -// subsequent symmetric encryption using |*out_secret| must use an authenticated +// subsequent symmetric encryption using `*out_secret` must use an authenticated // encryption scheme in order to discover the decapsulation failure. OPENSSL_EXPORT int EVP_KEM_decap(const EVP_KEM *kem, uint8_t *out_secret, size_t secret_len, const uint8_t *ciphertext, @@ -1225,7 +1339,7 @@ OPENSSL_EXPORT int EVP_KEM_decap(const EVP_KEM *kem, uint8_t *out_secret, #define EVP_PKEY_ED448 NID_ED448 // EVP_PKEY_get0 returns NULL. This function is provided for compatibility with -// OpenSSL but does not return anything. Use the typed |EVP_PKEY_get0_*| +// OpenSSL but does not return anything. Use the typed `EVP_PKEY_get0_*` // functions instead. OPENSSL_EXPORT void *EVP_PKEY_get0(const EVP_PKEY *pkey); @@ -1244,7 +1358,7 @@ OPENSSL_EXPORT void OpenSSL_add_all_digests(void); // EVP_cleanup does nothing. OPENSSL_EXPORT void EVP_cleanup(void); -// EVP_default_properties_is_fips_enabled calls |FIPS_mode|. +// EVP_default_properties_is_fips_enabled calls `FIPS_mode`. OPENSSL_EXPORT int EVP_default_properties_is_fips_enabled(OSSL_LIB_CTX *libctx); OPENSSL_EXPORT void EVP_CIPHER_do_all_sorted( @@ -1267,82 +1381,82 @@ OPENSSL_EXPORT void EVP_MD_do_all(void (*callback)(const EVP_MD *md, OPENSSL_EXPORT void EVP_MD_do_all_provided( OSSL_LIB_CTX *libctx, void (*callback)(EVP_MD *md, void *arg), void *arg); -// i2d_PrivateKey marshals a private key from |key| to type-specific format, as -// described in |i2d_SAMPLE|. +// i2d_PrivateKey marshals a private key from `key` to type-specific format, as +// described in `i2d_SAMPLE`. // // RSA keys are serialized as a DER-encoded RSAPublicKey (RFC 8017) structure. // EC keys are serialized as a DER-encoded ECPrivateKey (RFC 5915) structure. // -// Use |RSA_marshal_private_key| or |EC_KEY_marshal_private_key| instead. +// Use `RSA_marshal_private_key` or `EC_KEY_marshal_private_key` instead. OPENSSL_EXPORT int i2d_PrivateKey(const EVP_PKEY *key, uint8_t **outp); -// i2d_PublicKey marshals a public key from |key| to a type-specific format, as -// described in |i2d_SAMPLE|. +// i2d_PublicKey marshals a public key from `key` to a type-specific format, as +// described in `i2d_SAMPLE`. // // RSA keys are serialized as a DER-encoded RSAPublicKey (RFC 8017) structure. // EC keys are serialized as an EC point per SEC 1. // -// Use |RSA_marshal_public_key| or |EC_POINT_point2cbb| instead. +// Use `RSA_marshal_public_key` or `EC_POINT_point2cbb` instead. OPENSSL_EXPORT int i2d_PublicKey(const EVP_PKEY *key, uint8_t **outp); -// d2i_PrivateKey parses a DER-encoded private key from |len| bytes at |*inp|, -// as described in |d2i_SAMPLE|. The private key must have type |type|, +// d2i_PrivateKey parses a DER-encoded private key from `len` bytes at `*inp`, +// as described in `d2i_SAMPLE`. The private key must have type `type`, // otherwise it will be rejected. // // This function tries to detect one of several formats. Instead, use -// |EVP_parse_private_key| for a PrivateKeyInfo, |RSA_parse_private_key| for an -// RSAPrivateKey, and |EC_parse_private_key| for an ECPrivateKey. +// `EVP_parse_private_key` for a PrivateKeyInfo, `RSA_parse_private_key` for an +// RSAPrivateKey, and `EC_parse_private_key` for an ECPrivateKey. OPENSSL_EXPORT EVP_PKEY *d2i_PrivateKey(int type, EVP_PKEY **out, const uint8_t **inp, long len); -// d2i_AutoPrivateKey acts the same as |d2i_PrivateKey|, but detects the type +// d2i_AutoPrivateKey acts the same as `d2i_PrivateKey`, but detects the type // of the private key. // // This function tries to detect one of several formats. Instead, use -// |EVP_parse_private_key| for a PrivateKeyInfo, |RSA_parse_private_key| for an -// RSAPrivateKey, and |EC_parse_private_key| for an ECPrivateKey. +// `EVP_parse_private_key` for a PrivateKeyInfo, `RSA_parse_private_key` for an +// RSAPrivateKey, and `EC_parse_private_key` for an ECPrivateKey. OPENSSL_EXPORT EVP_PKEY *d2i_AutoPrivateKey(EVP_PKEY **out, const uint8_t **inp, long len); -// d2i_PublicKey parses a public key from |len| bytes at |*inp| in a type- -// specific format specified by |type|, as described in |d2i_SAMPLE|. +// d2i_PublicKey parses a public key from `len` bytes at `*inp` in a type- +// specific format specified by `type`, as described in `d2i_SAMPLE`. // -// The only supported value for |type| is |EVP_PKEY_RSA|, which parses a +// The only supported value for `type` is `EVP_PKEY_RSA`, which parses a // DER-encoded RSAPublicKey (RFC 8017) structure. Parsing EC keys is not // supported by this function. // -// Use |RSA_parse_public_key| instead. +// Use `RSA_parse_public_key` instead. OPENSSL_EXPORT EVP_PKEY *d2i_PublicKey(int type, EVP_PKEY **out, const uint8_t **inp, long len); -// EVP_PKEY_CTX_set_ec_param_enc returns one if |encoding| is -// |OPENSSL_EC_NAMED_CURVE| or zero with an error otherwise. +// EVP_PKEY_CTX_set_ec_param_enc returns one if `encoding` is +// `OPENSSL_EC_NAMED_CURVE` or zero with an error otherwise. OPENSSL_EXPORT int EVP_PKEY_CTX_set_ec_param_enc(EVP_PKEY_CTX *ctx, int encoding); -// EVP_PKEY_set_type sets the type of |pkey| to |type|. It returns one if -// successful or zero if the |type| argument is not one of the |EVP_PKEY_*| -// values supported for use with this function. If |pkey| is NULL, it simply +// EVP_PKEY_set_type sets the type of `pkey` to `type`. It returns one if +// successful or zero if the `type` argument is not one of the `EVP_PKEY_*` +// values supported for use with this function. If `pkey` is NULL, it simply // reports whether the type is known. // -// There are very few cases where this function is useful. Changing |pkey|'s +// There are very few cases where this function is useful. Changing `pkey`'s // type clears any previously stored keys, so there is no benefit to loading a -// key and then changing its type. Although |pkey| is left with a type +// key and then changing its type. Although `pkey` is left with a type // configured, it has no key, and functions which set a key, such as -// |EVP_PKEY_set1_RSA|, will configure a type anyway. If writing unit tests that +// `EVP_PKEY_set1_RSA`, will configure a type anyway. If writing unit tests that // are only sensitive to the type of a key, it is preferable to construct a real // key, so that tests are more representative of production code. // // The only API pattern which requires this function is -// |EVP_PKEY_set1_tls_encodedpoint| with X25519, which requires a half-empty -// |EVP_PKEY| that was first configured with |EVP_PKEY_X25519|. Currently, all -// other values of |type| will result in an error. +// `EVP_PKEY_set1_tls_encodedpoint` with X25519, which requires a half-empty +// `EVP_PKEY` that was first configured with `EVP_PKEY_X25519`. Currently, all +// other values of `type` will result in an error. OPENSSL_EXPORT int EVP_PKEY_set_type(EVP_PKEY *pkey, int type); -// EVP_PKEY_set1_tls_encodedpoint replaces |pkey| with a public key encoded by -// |in|. It returns one on success and zero on error. +// EVP_PKEY_set1_tls_encodedpoint replaces `pkey` with a public key encoded by +// `in`. It returns one on success and zero on error. // -// If |pkey| is an EC key, the format is an X9.62 point and |pkey| must already +// If `pkey` is an EC key, the format is an X9.62 point and `pkey` must already // have an EC group configured. If it is an X25519 key, it is the 32-byte X25519 // public key representation. This function is not supported for other key types // and will fail. @@ -1350,19 +1464,19 @@ OPENSSL_EXPORT int EVP_PKEY_set1_tls_encodedpoint(EVP_PKEY *pkey, const uint8_t *in, size_t len); -// EVP_PKEY_get1_tls_encodedpoint sets |*out_ptr| to a newly-allocated buffer -// containing the raw encoded public key for |pkey|. The caller must call -// |OPENSSL_free| to release this buffer. The function returns the length of the +// EVP_PKEY_get1_tls_encodedpoint sets `*out_ptr` to a newly-allocated buffer +// containing the raw encoded public key for `pkey`. The caller must call +// `OPENSSL_free` to release this buffer. The function returns the length of the // buffer on success and zero on error. // -// If |pkey| is an EC key, the format is an X9.62 point with uncompressed +// If `pkey` is an EC key, the format is an X9.62 point with uncompressed // coordinates. If it is an X25519 key, it is the 32-byte X25519 public key // representation. This function is not supported for other key types and will // fail. OPENSSL_EXPORT size_t EVP_PKEY_get1_tls_encodedpoint(const EVP_PKEY *pkey, uint8_t **out_ptr); -// EVP_PKEY_base_id calls |EVP_PKEY_id|. +// EVP_PKEY_base_id calls `EVP_PKEY_id`. OPENSSL_EXPORT int EVP_PKEY_base_id(const EVP_PKEY *pkey); // EVP_PKEY_CTX_set_rsa_pss_keygen_md returns 0. @@ -1377,56 +1491,56 @@ OPENSSL_EXPORT int EVP_PKEY_CTX_set_rsa_pss_keygen_saltlen(EVP_PKEY_CTX *ctx, OPENSSL_EXPORT int EVP_PKEY_CTX_set_rsa_pss_keygen_mgf1_md(EVP_PKEY_CTX *ctx, const EVP_MD *md); -// i2d_PUBKEY marshals |pkey| as a DER-encoded SubjectPublicKeyInfo, as -// described in |i2d_SAMPLE|. +// i2d_PUBKEY marshals `pkey` as a DER-encoded SubjectPublicKeyInfo, as +// described in `i2d_SAMPLE`. // -// Use |EVP_marshal_public_key| instead. +// Use `EVP_marshal_public_key` instead. OPENSSL_EXPORT int i2d_PUBKEY(const EVP_PKEY *pkey, uint8_t **outp); -// d2i_PUBKEY parses a DER-encoded SubjectPublicKeyInfo from |len| bytes at -// |*inp|, as described in |d2i_SAMPLE|. +// d2i_PUBKEY parses a DER-encoded SubjectPublicKeyInfo from `len` bytes at +// `*inp`, as described in `d2i_SAMPLE`. // -// Use |EVP_parse_public_key| instead. +// Use `EVP_parse_public_key` instead. OPENSSL_EXPORT EVP_PKEY *d2i_PUBKEY(EVP_PKEY **out, const uint8_t **inp, long len); -// i2d_RSA_PUBKEY marshals |rsa| as a DER-encoded SubjectPublicKeyInfo -// structure, as described in |i2d_SAMPLE|. +// i2d_RSA_PUBKEY marshals `rsa` as a DER-encoded SubjectPublicKeyInfo +// structure, as described in `i2d_SAMPLE`. // -// Use |EVP_marshal_public_key| instead. +// Use `EVP_marshal_public_key` instead. OPENSSL_EXPORT int i2d_RSA_PUBKEY(const RSA *rsa, uint8_t **outp); // d2i_RSA_PUBKEY parses an RSA public key as a DER-encoded SubjectPublicKeyInfo -// from |len| bytes at |*inp|, as described in |d2i_SAMPLE|. +// from `len` bytes at `*inp`, as described in `d2i_SAMPLE`. // SubjectPublicKeyInfo structures containing other key types are rejected. // -// Use |EVP_parse_public_key| instead. +// Use `EVP_parse_public_key` instead. OPENSSL_EXPORT RSA *d2i_RSA_PUBKEY(RSA **out, const uint8_t **inp, long len); -// i2d_DSA_PUBKEY marshals |dsa| as a DER-encoded SubjectPublicKeyInfo, as -// described in |i2d_SAMPLE|. +// i2d_DSA_PUBKEY marshals `dsa` as a DER-encoded SubjectPublicKeyInfo, as +// described in `i2d_SAMPLE`. // -// Use |EVP_marshal_public_key| instead. +// Use `EVP_marshal_public_key` instead. OPENSSL_EXPORT int i2d_DSA_PUBKEY(const DSA *dsa, uint8_t **outp); // d2i_DSA_PUBKEY parses a DSA public key as a DER-encoded SubjectPublicKeyInfo -// from |len| bytes at |*inp|, as described in |d2i_SAMPLE|. +// from `len` bytes at `*inp`, as described in `d2i_SAMPLE`. // SubjectPublicKeyInfo structures containing other key types are rejected. // -// Use |EVP_parse_public_key| instead. +// Use `EVP_parse_public_key` instead. OPENSSL_EXPORT DSA *d2i_DSA_PUBKEY(DSA **out, const uint8_t **inp, long len); -// i2d_EC_PUBKEY marshals |ec_key| as a DER-encoded SubjectPublicKeyInfo, as -// described in |i2d_SAMPLE|. +// i2d_EC_PUBKEY marshals `ec_key` as a DER-encoded SubjectPublicKeyInfo, as +// described in `i2d_SAMPLE`. // -// Use |EVP_marshal_public_key| instead. +// Use `EVP_marshal_public_key` instead. OPENSSL_EXPORT int i2d_EC_PUBKEY(const EC_KEY *ec_key, uint8_t **outp); // d2i_EC_PUBKEY parses an EC public key as a DER-encoded SubjectPublicKeyInfo -// from |len| bytes at |*inp|, as described in |d2i_SAMPLE|. +// from `len` bytes at `*inp`, as described in `d2i_SAMPLE`. // SubjectPublicKeyInfo structures containing other key types are rejected. // -// Use |EVP_parse_public_key| instead. +// Use `EVP_parse_public_key` instead. OPENSSL_EXPORT EC_KEY *d2i_EC_PUBKEY(EC_KEY **out, const uint8_t **inp, long len); @@ -1438,45 +1552,45 @@ OPENSSL_EXPORT int EVP_PKEY_CTX_set_dsa_paramgen_bits(EVP_PKEY_CTX *ctx, OPENSSL_EXPORT int EVP_PKEY_CTX_set_dsa_paramgen_q_bits(EVP_PKEY_CTX *ctx, int qbits); -// EVP_PKEY_assign sets the underlying key of |pkey| to |key|, which must be of -// the given type. If successful, it returns one. If the |type| argument -// is not one of |EVP_PKEY_RSA|, |EVP_PKEY_DSA|, or |EVP_PKEY_EC| values or if -// |key| is NULL, it returns zero. This function may not be used with other -// |EVP_PKEY_*| types. +// EVP_PKEY_assign sets the underlying key of `pkey` to `key`, which must be of +// the given type. If successful, it returns one. If the `type` argument +// is not one of `EVP_PKEY_RSA`, `EVP_PKEY_DSA`, or `EVP_PKEY_EC` values or if +// `key` is NULL, it returns zero. This function may not be used with other +// `EVP_PKEY_*` types. // -// Use the |EVP_PKEY_assign_*| functions instead. +// Use the `EVP_PKEY_assign_*` functions instead. OPENSSL_EXPORT int EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key); -// EVP_PKEY_type returns |nid|. +// EVP_PKEY_type returns `nid`. OPENSSL_EXPORT int EVP_PKEY_type(int nid); -// EVP_PKEY_new_raw_private_key interprets |in| as a raw private key of type -// |type|, which must be an |EVP_PKEY_*| constant, such as |EVP_PKEY_X25519|, -// and returns a newly-allocated |EVP_PKEY|, or nullptr on error. +// EVP_PKEY_new_raw_private_key interprets `in` as a raw private key of type +// `type`, which must be an `EVP_PKEY_*` constant, such as `EVP_PKEY_X25519`, +// and returns a newly-allocated `EVP_PKEY`, or nullptr on error. // -// Prefer |EVP_PKEY_from_raw_private_key|, which allows dead code elimination to +// Prefer `EVP_PKEY_from_raw_private_key`, which allows dead code elimination to // discard algorithms that aren't reachable from the caller. OPENSSL_EXPORT EVP_PKEY *EVP_PKEY_new_raw_private_key(int type, ENGINE *unused, const uint8_t *in, size_t len); -// EVP_PKEY_new_raw_public_key interprets |in| as a raw public key of type -// |type|, which must be an |EVP_PKEY_*| constant, such as |EVP_PKEY_X25519|, -// and returns a newly-allocated |EVP_PKEY|, or nullptr on error. +// EVP_PKEY_new_raw_public_key interprets `in` as a raw public key of type +// `type`, which must be an `EVP_PKEY_*` constant, such as `EVP_PKEY_X25519`, +// and returns a newly-allocated `EVP_PKEY`, or nullptr on error. // -// Prefer |EVP_PKEY_from_raw_private_key|, which allows dead code elimination to +// Prefer `EVP_PKEY_from_raw_private_key`, which allows dead code elimination to // discard algorithms that aren't reachable from the caller. OPENSSL_EXPORT EVP_PKEY *EVP_PKEY_new_raw_public_key(int type, ENGINE *unused, const uint8_t *in, size_t len); -// EVP_PKEY_cmp calls |EVP_PKEY_eq|. It returns one if public keys are equal and +// EVP_PKEY_cmp calls `EVP_PKEY_eq`. It returns one if public keys are equal and // zero otherwise. // // WARNING: This differs from the traditional return value of a "cmp" function. OPENSSL_EXPORT int EVP_PKEY_cmp(const EVP_PKEY *a, const EVP_PKEY *b); -// EVP_PKEY_cmp_parameters calls |EVP_PKEY_parameters_eq|. It returns one if +// EVP_PKEY_cmp_parameters calls `EVP_PKEY_parameters_eq`. It returns one if // parameters are equal and zero otherwise. // // WARNING: This differs from the traditional return value of a "cmp" function. @@ -1490,7 +1604,7 @@ OPENSSL_EXPORT int EVP_PKEY_cmp_parameters(const EVP_PKEY *a, // constants to 'ctrl' functions. To avoid breaking #ifdefs in consumers, this // section defines a number of legacy macros. -// |BORINGSSL_PREFIX| already makes some of these symbols into macros, so there +// `BORINGSSL_PREFIX` already makes some of these symbols into macros, so there // is no need to define conflicting macros; however it is compiler specific // which ones become macros. #if !defined(EVP_PKEY_CTX_set_rsa_oaep_md) @@ -1517,7 +1631,7 @@ extern "C++" { BSSL_NAMESPACE_BEGIN BORINGSSL_MAKE_DELETER(EVP_PKEY, EVP_PKEY_free) -BORINGSSL_MAKE_UP_REF(EVP_PKEY, EVP_PKEY_up_ref) +BORINGSSL_MAKE_UP_REF(EVP_PKEY, EVP_PKEY_dup_ref) BORINGSSL_MAKE_DELETER(EVP_PKEY_CTX, EVP_PKEY_CTX_free) BSSL_NAMESPACE_END diff --git a/third_party/boringssl/src/include/openssl/evp_errors.h b/third_party/boringssl/src/include/openssl/evp_errors.h index 77b7ab5c4..ebb72cd87 100644 --- a/third_party/boringssl/src/include/openssl/evp_errors.h +++ b/third_party/boringssl/src/include/openssl/evp_errors.h @@ -58,6 +58,7 @@ #define EVP_R_MISSING_PUBLIC_KEY 140 #define EVP_R_INVALID_CIPHERTEXT_LENGTH 141 #define EVP_R_INVALID_SECRET_LENGTH 142 +#define EVP_R_INVALID_ENTROPY_LENGTH 143 // Compatibility defines. #define EVP_R_OPERATON_NOT_INITIALIZED EVP_R_OPERATION_NOT_INITIALIZED diff --git a/third_party/boringssl/src/include/openssl/ex_data.h b/third_party/boringssl/src/include/openssl/ex_data.h index 6bb187471..1e567fe05 100644 --- a/third_party/boringssl/src/include/openssl/ex_data.h +++ b/third_party/boringssl/src/include/openssl/ex_data.h @@ -35,9 +35,9 @@ extern "C" { // Each type that supports ex_data provides three functions: -// TYPE_get_ex_new_index allocates a new index for |TYPE|. An optional -// |free_func| argument may be provided which is called when the owning object -// is destroyed. See |CRYPTO_EX_free| for details. The |argl| and |argp| +// TYPE_get_ex_new_index allocates a new index for `TYPE`. An optional +// `free_func` argument may be provided which is called when the owning object +// is destroyed. See `CRYPTO_EX_free` for details. The `argl` and `argp` // arguments are opaque values that are passed to the callback. It returns the // new index or a negative number on error. OPENSSL_EXPORT int TYPE_get_ex_new_index(long argl, void *argp, @@ -45,24 +45,24 @@ OPENSSL_EXPORT int TYPE_get_ex_new_index(long argl, void *argp, CRYPTO_EX_dup *dup_unused, CRYPTO_EX_free *free_func); -// TYPE_set_ex_data sets an extra data pointer on |t|. The |index| argument -// must have been returned from a previous call to |TYPE_get_ex_new_index|. +// TYPE_set_ex_data sets an extra data pointer on `t`. The `index` argument +// must have been returned from a previous call to `TYPE_get_ex_new_index`. OPENSSL_EXPORT int TYPE_set_ex_data(TYPE *t, int index, void *arg); -// TYPE_get_ex_data returns an extra data pointer for |t|, or NULL if no such -// pointer exists. The |index| argument should have been returned from a -// previous call to |TYPE_get_ex_new_index|. +// TYPE_get_ex_data returns an extra data pointer for `t`, or NULL if no such +// pointer exists. The `index` argument should have been returned from a +// previous call to `TYPE_get_ex_new_index`. OPENSSL_EXPORT void *TYPE_get_ex_data(const TYPE *t, int index); // Some types additionally preallocate index zero, with all callbacks set to // NULL. Applications that do not need the general ex_data machinery may use // this instead. -// TYPE_set_app_data sets |t|'s application data pointer to |arg|. It returns +// TYPE_set_app_data sets `t`'s application data pointer to `arg`. It returns // one on success and zero on error. OPENSSL_EXPORT int TYPE_set_app_data(TYPE *t, void *arg); -// TYPE_get_app_data returns the application data pointer for |t|, or NULL if no +// TYPE_get_app_data returns the application data pointer for `t`, or NULL if no // such pointer exists. OPENSSL_EXPORT void *TYPE_get_app_data(const TYPE *t); @@ -77,19 +77,19 @@ typedef struct crypto_ex_data_st CRYPTO_EX_DATA; // CRYPTO_EX_free is a callback function that is called when an object of the // class with extra data pointers is being destroyed. For example, if this -// callback has been passed to |SSL_get_ex_new_index| then it may be called each -// time an |SSL*| is destroyed. +// callback has been passed to `SSL_get_ex_new_index` then it may be called each +// time an `SSL*` is destroyed. // -// |parent| and |ad| will be NULL. Historically, the parent object was passed in -// |parent|, but accessing the pointer was not safe because |parent| was in the +// `parent` and `ad` will be NULL. Historically, the parent object was passed in +// `parent`, but accessing the pointer was not safe because `parent` was in the // process of being destroyed. If the callback has access to some other pointer // to the parent object, it must not pass the pointer to any BoringSSL APIs. // Mid-destruction, invariants on the parent object no longer hold. // -// The arguments |argl| and |argp| contain opaque values that were given to -// |CRYPTO_get_ex_new_index_ex|. +// The arguments `argl` and `argp` contain opaque values that were given to +// `CRYPTO_get_ex_new_index_ex`. // -// This callback may be called with a NULL value for |ptr| if the object has no +// This callback may be called with a NULL value for `ptr` if the object has no // value set for this index. However, the callbacks may also be skipped entirely // if no extra data pointers are set on the object at all. typedef void CRYPTO_EX_free(void *parent, void *ptr, CRYPTO_EX_DATA *ad, diff --git a/third_party/boringssl/src/include/openssl/hkdf.h b/third_party/boringssl/src/include/openssl/hkdf.h index 19663db11..2e249e25c 100644 --- a/third_party/boringssl/src/include/openssl/hkdf.h +++ b/third_party/boringssl/src/include/openssl/hkdf.h @@ -26,8 +26,8 @@ extern "C" { // HKDF computes HKDF (as specified by RFC 5869) of initial keying material -// |secret| with |salt| and |info| using |digest|, and outputs |out_len| bytes -// to |out_key|. It returns one on success and zero on error. +// `secret` with `salt` and `info` using `digest`, and outputs `out_len` bytes +// to `out_key`. It returns one on success and zero on error. // // HKDF is an Extract-and-Expand algorithm. It does not do any key stretching, // and as such, is not suited to be used alone to generate a key from a @@ -38,8 +38,8 @@ OPENSSL_EXPORT int HKDF(uint8_t *out_key, size_t out_len, const EVP_MD *digest, const uint8_t *info, size_t info_len); // HKDF_extract computes a HKDF PRK (as specified by RFC 5869) from initial -// keying material |secret| and salt |salt| using |digest|, and outputs -// |out_len| bytes to |out_key|. The maximum output size is |EVP_MAX_MD_SIZE|. +// keying material `secret` and salt `salt` using `digest`, and outputs +// `out_len` bytes to `out_key`. The maximum output size is `EVP_MAX_MD_SIZE`. // It returns one on success and zero on error. // // WARNING: This function orders the inputs differently from RFC 5869 @@ -51,8 +51,8 @@ OPENSSL_EXPORT int HKDF_extract(uint8_t *out_key, size_t *out_len, size_t salt_len); // HKDF_expand computes a HKDF OKM (as specified by RFC 5869) of length -// |out_len| from the PRK |prk| and info |info| using |digest|, and outputs -// the result to |out_key|. It returns one on success and zero on error. +// `out_len` from the PRK `prk` and info `info` using `digest`, and outputs +// the result to `out_key`. It returns one on success and zero on error. OPENSSL_EXPORT int HKDF_expand(uint8_t *out_key, size_t out_len, const EVP_MD *digest, const uint8_t *prk, size_t prk_len, const uint8_t *info, diff --git a/third_party/boringssl/src/include/openssl/hmac.h b/third_party/boringssl/src/include/openssl/hmac.h index 2266f99f3..4fbc7646a 100644 --- a/third_party/boringssl/src/include/openssl/hmac.h +++ b/third_party/boringssl/src/include/openssl/hmac.h @@ -30,11 +30,11 @@ extern "C" { // One-shot operation. -// HMAC calculates the HMAC of |data_len| bytes of |data|, using the given key -// and hash function, and writes the result to |out|. On entry, |out| must -// contain at least |EVP_MD_size| bytes of space. The actual length of the -// result is written to |*out_len|. An output size of |EVP_MAX_MD_SIZE| will -// always be large enough. It returns |out| or NULL on error. +// HMAC calculates the HMAC of `data_len` bytes of `data`, using the given key +// and hash function, and writes the result to `out`. On entry, `out` must +// contain at least `EVP_MD_size` bytes of space. The actual length of the +// result is written to `*out_len`. An output size of `EVP_MAX_MD_SIZE` will +// always be large enough. It returns `out` or NULL on error. OPENSSL_EXPORT uint8_t *HMAC(const EVP_MD *evp_md, const void *key, size_t key_len, const uint8_t *data, size_t data_len, uint8_t *out, @@ -43,47 +43,47 @@ OPENSSL_EXPORT uint8_t *HMAC(const EVP_MD *evp_md, const void *key, // Incremental operation. -// HMAC_CTX_init initialises |ctx| for use in an HMAC operation. It's assumed +// HMAC_CTX_init initialises `ctx` for use in an HMAC operation. It's assumed // that HMAC_CTX objects will be allocated on the stack thus no allocation // function is provided. OPENSSL_EXPORT void HMAC_CTX_init(HMAC_CTX *ctx); -// HMAC_CTX_new allocates and initialises a new |HMAC_CTX| and returns it, or -// NULL on allocation failure. The caller must use |HMAC_CTX_free| to release +// HMAC_CTX_new allocates and initialises a new `HMAC_CTX` and returns it, or +// NULL on allocation failure. The caller must use `HMAC_CTX_free` to release // the resulting object. OPENSSL_EXPORT HMAC_CTX *HMAC_CTX_new(void); -// HMAC_CTX_cleanup frees data owned by |ctx|. It does not free |ctx| itself. +// HMAC_CTX_cleanup frees data owned by `ctx`. It does not free `ctx` itself. OPENSSL_EXPORT void HMAC_CTX_cleanup(HMAC_CTX *ctx); -// HMAC_CTX_cleanse zeros the digest state from |ctx| and then performs the -// actions of |HMAC_CTX_cleanup|. +// HMAC_CTX_cleanse zeros the digest state from `ctx` and then performs the +// actions of `HMAC_CTX_cleanup`. OPENSSL_EXPORT void HMAC_CTX_cleanse(HMAC_CTX *ctx); -// HMAC_CTX_free calls |HMAC_CTX_cleanup| and then frees |ctx| itself. +// HMAC_CTX_free calls `HMAC_CTX_cleanup` and then frees `ctx` itself. OPENSSL_EXPORT void HMAC_CTX_free(HMAC_CTX *ctx); -// HMAC_Init_ex sets up an initialised |HMAC_CTX| to use |md| as the hash -// function and |key| as the key. For a non-initial call, |md| may be NULL, in +// HMAC_Init_ex sets up an initialised `HMAC_CTX` to use `md` as the hash +// function and `key` as the key. For a non-initial call, `md` may be NULL, in // which case the previous hash function will be used. If the hash function has -// not changed and |key| is NULL, |ctx| reuses the previous key. It returns one +// not changed and `key` is NULL, `ctx` reuses the previous key. It returns one // on success or zero on allocation failure. // // WARNING: NULL and empty keys are ambiguous on non-initial calls. Passing NULL -// |key| but repeating the previous |md| reuses the previous key rather than the +// `key` but repeating the previous `md` reuses the previous key rather than the // empty key. OPENSSL_EXPORT int HMAC_Init_ex(HMAC_CTX *ctx, const void *key, size_t key_len, const EVP_MD *md, ENGINE *impl); -// HMAC_Update hashes |data_len| bytes from |data| into the current HMAC -// operation in |ctx|. It returns one. +// HMAC_Update hashes `data_len` bytes from `data` into the current HMAC +// operation in `ctx`. It returns one. OPENSSL_EXPORT int HMAC_Update(HMAC_CTX *ctx, const uint8_t *data, size_t data_len); -// HMAC_Final completes the HMAC operation in |ctx| and writes the result to -// |out|. If |out_len| is not |NULL| then it writes the length of the result to -// |*out_len|. On entry, |out| must contain at least |HMAC_size| bytes of -// space. An output size of |EVP_MAX_MD_SIZE| will always be large enough. It +// HMAC_Final completes the HMAC operation in `ctx` and writes the result to +// `out`. If `out_len` is not `NULL` then it writes the length of the result to +// `*out_len`. On entry, `out` must contain at least `HMAC_size` bytes of +// space. An output size of `EVP_MAX_MD_SIZE` will always be large enough. It // returns one on success or zero on allocation failure. OPENSSL_EXPORT int HMAC_Final(HMAC_CTX *ctx, uint8_t *out, unsigned int *out_len); @@ -92,18 +92,18 @@ OPENSSL_EXPORT int HMAC_Final(HMAC_CTX *ctx, uint8_t *out, // Utility functions. // HMAC_size returns the size, in bytes, of the HMAC that will be produced by -// |ctx|. On entry, |ctx| must have been setup with |HMAC_Init_ex|. +// `ctx`. On entry, `ctx` must have been setup with `HMAC_Init_ex`. OPENSSL_EXPORT size_t HMAC_size(const HMAC_CTX *ctx); -// HMAC_CTX_get_md returns |ctx|'s hash function. +// HMAC_CTX_get_md returns `ctx`'s hash function. OPENSSL_EXPORT const EVP_MD *HMAC_CTX_get_md(const HMAC_CTX *ctx); -// HMAC_CTX_copy_ex sets |dest| equal to |src|. On entry, |dest| must have been -// initialised by calling |HMAC_CTX_init|. It returns one on success and zero +// HMAC_CTX_copy_ex sets `dest` equal to `src`. On entry, `dest` must have been +// initialised by calling `HMAC_CTX_init`. It returns one on success and zero // on error. OPENSSL_EXPORT int HMAC_CTX_copy_ex(HMAC_CTX *dest, const HMAC_CTX *src); -// HMAC_CTX_reset calls |HMAC_CTX_cleanup| followed by |HMAC_CTX_init|. +// HMAC_CTX_reset calls `HMAC_CTX_cleanup` followed by `HMAC_CTX_init`. OPENSSL_EXPORT void HMAC_CTX_reset(HMAC_CTX *ctx); @@ -112,9 +112,9 @@ OPENSSL_EXPORT void HMAC_CTX_reset(HMAC_CTX *ctx); OPENSSL_EXPORT int HMAC_Init(HMAC_CTX *ctx, const void *key, int key_len, const EVP_MD *md); -// HMAC_CTX_copy calls |HMAC_CTX_init| on |dest| and then sets it equal to -// |src|. On entry, |dest| must /not/ be initialised for an operation with -// |HMAC_Init_ex|. It returns one on success and zero on error. +// HMAC_CTX_copy calls `HMAC_CTX_init` on `dest` and then sets it equal to +// `src`. On entry, `dest` must /not/ be initialised for an operation with +// `HMAC_Init_ex`. It returns one on success and zero on error. OPENSSL_EXPORT int HMAC_CTX_copy(HMAC_CTX *dest, const HMAC_CTX *src); @@ -138,8 +138,8 @@ BSSL_NAMESPACE_BEGIN BORINGSSL_MAKE_DELETER(HMAC_CTX, HMAC_CTX_free) -using ScopedHMAC_CTX = - internal::StackAllocated; +BORINGSSL_MAKE_STACK_TRAITS(HMAC_CTX, HMAC_CTX_init, HMAC_CTX_cleanup) +using ScopedHMAC_CTX = internal::StackAllocated; BSSL_NAMESPACE_END diff --git a/third_party/boringssl/src/include/openssl/hpke.h b/third_party/boringssl/src/include/openssl/hpke.h index deacbc95e..6cc8bbd36 100644 --- a/third_party/boringssl/src/include/openssl/hpke.h +++ b/third_party/boringssl/src/include/openssl/hpke.h @@ -36,7 +36,7 @@ extern "C" { // Parameters. // // An HPKE context is parameterized by KEM, KDF, and AEAD algorithms, -// represented by |EVP_HPKE_KEM|, |EVP_HPKE_KDF|, and |EVP_HPKE_AEAD| types, +// represented by `EVP_HPKE_KEM`, `EVP_HPKE_KDF`, and `EVP_HPKE_AEAD` types, // respectively. // The following constants are KEM identifiers. @@ -48,31 +48,35 @@ extern "C" { // The following functions are KEM algorithms which may be used with HPKE. Note // that, while some HPKE KEMs use KDFs internally, this is separate from the -// |EVP_HPKE_KDF| selection. +// `EVP_HPKE_KDF` selection. OPENSSL_EXPORT const EVP_HPKE_KEM *EVP_hpke_x25519_hkdf_sha256(void); OPENSSL_EXPORT const EVP_HPKE_KEM *EVP_hpke_p256_hkdf_sha256(void); OPENSSL_EXPORT const EVP_HPKE_KEM *EVP_hpke_xwing(void); OPENSSL_EXPORT const EVP_HPKE_KEM *EVP_hpke_mlkem768(void); OPENSSL_EXPORT const EVP_HPKE_KEM *EVP_hpke_mlkem1024(void); -// EVP_HPKE_KEM_id returns the HPKE KEM identifier for |kem|, which -// will be one of the |EVP_HPKE_KEM_*| constants. +// EVP_kem_dhkem_* implement DHKEM variants defined in RFC 9180. +OPENSSL_EXPORT const EVP_KEM *EVP_kem_dhkem_p256_hkdf_sha256(void); +OPENSSL_EXPORT const EVP_KEM *EVP_kem_dhkem_x25519_hkdf_sha256(void); + +// EVP_HPKE_KEM_id returns the HPKE KEM identifier for `kem`, which +// will be one of the `EVP_HPKE_KEM_*` constants. OPENSSL_EXPORT uint16_t EVP_HPKE_KEM_id(const EVP_HPKE_KEM *kem); // EVP_HPKE_MAX_PUBLIC_KEY_LENGTH is the maximum length of an encoded public key // for all KEMs currently supported by this library. #define EVP_HPKE_MAX_PUBLIC_KEY_LENGTH 1568 -// EVP_HPKE_KEM_public_key_len returns the length of a public key for |kem|. -// This value will be at most |EVP_HPKE_MAX_PUBLIC_KEY_LENGTH|. +// EVP_HPKE_KEM_public_key_len returns the length of a public key for `kem`. +// This value will be at most `EVP_HPKE_MAX_PUBLIC_KEY_LENGTH`. OPENSSL_EXPORT size_t EVP_HPKE_KEM_public_key_len(const EVP_HPKE_KEM *kem); // EVP_HPKE_MAX_PRIVATE_KEY_LENGTH is the maximum length of an encoded private // key for all KEMs currently supported by this library. #define EVP_HPKE_MAX_PRIVATE_KEY_LENGTH 64 -// EVP_HPKE_KEM_private_key_len returns the length of a private key for |kem|. -// This value will be at most |EVP_HPKE_MAX_PRIVATE_KEY_LENGTH|. +// EVP_HPKE_KEM_private_key_len returns the length of a private key for `kem`. +// This value will be at most `EVP_HPKE_MAX_PRIVATE_KEY_LENGTH`. OPENSSL_EXPORT size_t EVP_HPKE_KEM_private_key_len(const EVP_HPKE_KEM *kem); // EVP_HPKE_MAX_ENC_LENGTH is the maximum length of "enc", the encapsulated @@ -80,23 +84,27 @@ OPENSSL_EXPORT size_t EVP_HPKE_KEM_private_key_len(const EVP_HPKE_KEM *kem); #define EVP_HPKE_MAX_ENC_LENGTH 1568 // EVP_HPKE_KEM_enc_len returns the length of the "enc", the encapsulated shared -// secret, for |kem|. This value will be at most |EVP_HPKE_MAX_ENC_LENGTH|. +// secret, for `kem`. This value will be at most `EVP_HPKE_MAX_ENC_LENGTH`. OPENSSL_EXPORT size_t EVP_HPKE_KEM_enc_len(const EVP_HPKE_KEM *kem); +// EVP_HPKE_KEM_shared_secret_len returns the length of the shared secret +// produced by `kem`. +OPENSSL_EXPORT size_t EVP_HPKE_KEM_shared_secret_len(const EVP_HPKE_KEM *kem); + // The following constants are KDF identifiers. #define EVP_HPKE_HKDF_SHA256 0x0001 #define EVP_HPKE_HKDF_SHA384 0x0002 // The following functions are KDF algorithms which may be used with HPKE. If -// unsure, use |EVP_hpke_hkdf_sha256|. +// unsure, use `EVP_hpke_hkdf_sha256`. OPENSSL_EXPORT const EVP_HPKE_KDF *EVP_hpke_hkdf_sha256(void); OPENSSL_EXPORT const EVP_HPKE_KDF *EVP_hpke_hkdf_sha384(void); -// EVP_HPKE_KDF_id returns the HPKE KDF identifier for |kdf|. +// EVP_HPKE_KDF_id returns the HPKE KDF identifier for `kdf`. OPENSSL_EXPORT uint16_t EVP_HPKE_KDF_id(const EVP_HPKE_KDF *kdf); -// EVP_HPKE_KDF_hkdf_md returns the HKDF hash function corresponding to |kdf|, -// or NULL if |kdf| is not an HKDF-based KDF. All currently supported KDFs are +// EVP_HPKE_KDF_hkdf_md returns the HKDF hash function corresponding to `kdf`, +// or NULL if `kdf` is not an HKDF-based KDF. All currently supported KDFs are // HKDF-based. OPENSSL_EXPORT const EVP_MD *EVP_HPKE_KDF_hkdf_md(const EVP_HPKE_KDF *kdf); @@ -110,90 +118,90 @@ OPENSSL_EXPORT const EVP_HPKE_AEAD *EVP_hpke_aes_128_gcm(void); OPENSSL_EXPORT const EVP_HPKE_AEAD *EVP_hpke_aes_256_gcm(void); OPENSSL_EXPORT const EVP_HPKE_AEAD *EVP_hpke_chacha20_poly1305(void); -// EVP_HPKE_AEAD_id returns the HPKE AEAD identifier for |aead|. +// EVP_HPKE_AEAD_id returns the HPKE AEAD identifier for `aead`. OPENSSL_EXPORT uint16_t EVP_HPKE_AEAD_id(const EVP_HPKE_AEAD *aead); -// EVP_HPKE_AEAD_aead returns the |EVP_AEAD| corresponding to |aead|. +// EVP_HPKE_AEAD_aead returns the `EVP_AEAD` corresponding to `aead`. OPENSSL_EXPORT const EVP_AEAD *EVP_HPKE_AEAD_aead(const EVP_HPKE_AEAD *aead); // Recipient keys. // // An HPKE recipient maintains a long-term KEM key. This library represents keys -// with the |EVP_HPKE_KEY| type. +// with the `EVP_HPKE_KEY` type. -// EVP_HPKE_KEY_zero sets an uninitialized |EVP_HPKE_KEY| to the zero state. The -// caller should then use |EVP_HPKE_KEY_init|, |EVP_HPKE_KEY_copy|, or -// |EVP_HPKE_KEY_generate| to finish initializing |key|. +// EVP_HPKE_KEY_zero sets an uninitialized `EVP_HPKE_KEY` to the zero state. The +// caller should then use `EVP_HPKE_KEY_init`, `EVP_HPKE_KEY_copy`, or +// `EVP_HPKE_KEY_generate` to finish initializing `key`. // -// It is safe, but not necessary to call |EVP_HPKE_KEY_cleanup| in this state. -// This may be used for more uniform cleanup of |EVP_HPKE_KEY|. +// It is safe, but not necessary to call `EVP_HPKE_KEY_cleanup` in this state. +// This may be used for more uniform cleanup of `EVP_HPKE_KEY`. OPENSSL_EXPORT void EVP_HPKE_KEY_zero(EVP_HPKE_KEY *key); -// EVP_HPKE_KEY_cleanup releases memory referenced by |key|. +// EVP_HPKE_KEY_cleanup releases memory referenced by `key`. OPENSSL_EXPORT void EVP_HPKE_KEY_cleanup(EVP_HPKE_KEY *key); -// EVP_HPKE_KEY_new returns a newly-allocated |EVP_HPKE_KEY|, or NULL on error. -// The caller must call |EVP_HPKE_KEY_free| on the result to release it. +// EVP_HPKE_KEY_new returns a newly-allocated `EVP_HPKE_KEY`, or NULL on error. +// The caller must call `EVP_HPKE_KEY_free` on the result to release it. // // This is a convenience function for callers that need a heap-allocated -// |EVP_HPKE_KEY|. +// `EVP_HPKE_KEY`. OPENSSL_EXPORT EVP_HPKE_KEY *EVP_HPKE_KEY_new(void); -// EVP_HPKE_KEY_free releases memory associated with |key|, which must have been -// created with |EVP_HPKE_KEY_new|. +// EVP_HPKE_KEY_free releases memory associated with `key`, which must have been +// created with `EVP_HPKE_KEY_new`. OPENSSL_EXPORT void EVP_HPKE_KEY_free(EVP_HPKE_KEY *key); -// EVP_HPKE_KEY_copy sets |dst| to a copy of |src|. It returns one on success -// and zero on error. On success, the caller must call |EVP_HPKE_KEY_cleanup| to -// release |dst|. On failure, calling |EVP_HPKE_KEY_cleanup| is safe, but not +// EVP_HPKE_KEY_copy sets `dst` to a copy of `src`. It returns one on success +// and zero on error. On success, the caller must call `EVP_HPKE_KEY_cleanup` to +// release `dst`. On failure, calling `EVP_HPKE_KEY_cleanup` is safe, but not // necessary. OPENSSL_EXPORT int EVP_HPKE_KEY_copy(EVP_HPKE_KEY *dst, const EVP_HPKE_KEY *src); -// EVP_HPKE_KEY_move sets |out|, which must be initialized or in the zero state, -// to the key in |in|. |in| is mutated and left in the zero state. +// EVP_HPKE_KEY_move sets `out`, which must be initialized or in the zero state, +// to the key in `in`. `in` is mutated and left in the zero state. OPENSSL_EXPORT void EVP_HPKE_KEY_move(EVP_HPKE_KEY *out, EVP_HPKE_KEY *in); -// EVP_HPKE_KEY_init decodes |priv_key| as a private key for |kem| and -// initializes |key| with the result. It returns one on success and zero if -// |priv_key| was invalid. On success, the caller must call -// |EVP_HPKE_KEY_cleanup| to release the key. On failure, calling -// |EVP_HPKE_KEY_cleanup| is safe, but not necessary. +// EVP_HPKE_KEY_init decodes `priv_key` as a private key for `kem` and +// initializes `key` with the result. It returns one on success and zero if +// `priv_key` was invalid. On success, the caller must call +// `EVP_HPKE_KEY_cleanup` to release the key. On failure, calling +// `EVP_HPKE_KEY_cleanup` is safe, but not necessary. OPENSSL_EXPORT int EVP_HPKE_KEY_init(EVP_HPKE_KEY *key, const EVP_HPKE_KEM *kem, const uint8_t *priv_key, size_t priv_key_len); -// EVP_HPKE_KEY_generate sets |key| to a newly-generated key using |kem|. +// EVP_HPKE_KEY_generate sets `key` to a newly-generated key using `kem`. OPENSSL_EXPORT int EVP_HPKE_KEY_generate(EVP_HPKE_KEY *key, const EVP_HPKE_KEM *kem); -// EVP_HPKE_KEY_derive deterministically derives a key from |ikm| for use with -// |kem| and initializes |key| with the result. It returns one on success and +// EVP_HPKE_KEY_derive deterministically derives a key from `ikm` for use with +// `kem` and initializes `key` with the result. It returns one on success and // zero on error. This corresponds to the DeriveKeyPair operation in RFC 9180. OPENSSL_EXPORT int EVP_HPKE_KEY_derive(EVP_HPKE_KEY *key, const EVP_HPKE_KEM *kem, const uint8_t *ikm, size_t ikm_len); -// EVP_HPKE_KEY_kem returns the HPKE KEM used by |key|. +// EVP_HPKE_KEY_kem returns the HPKE KEM used by `key`. OPENSSL_EXPORT const EVP_HPKE_KEM *EVP_HPKE_KEY_kem(const EVP_HPKE_KEY *key); -// EVP_HPKE_KEY_public_key writes |key|'s public key to |out| and sets -// |*out_len| to the number of bytes written. On success, it returns one and -// writes at most |max_out| bytes. If |max_out| is too small, it returns zero. -// Setting |max_out| to |EVP_HPKE_MAX_PUBLIC_KEY_LENGTH| will ensure the public +// EVP_HPKE_KEY_public_key writes `key`'s public key to `out` and sets +// `*out_len` to the number of bytes written. On success, it returns one and +// writes at most `max_out` bytes. If `max_out` is too small, it returns zero. +// Setting `max_out` to `EVP_HPKE_MAX_PUBLIC_KEY_LENGTH` will ensure the public // key fits. An exact size can also be determined by -// |EVP_HPKE_KEM_public_key_len|. +// `EVP_HPKE_KEM_public_key_len`. OPENSSL_EXPORT int EVP_HPKE_KEY_public_key(const EVP_HPKE_KEY *key, uint8_t *out, size_t *out_len, size_t max_out); -// EVP_HPKE_KEY_private_key writes |key|'s private key to |out| and sets -// |*out_len| to the number of bytes written. On success, it returns one and -// writes at most |max_out| bytes. If |max_out| is too small, it returns zero. -// Setting |max_out| to |EVP_HPKE_MAX_PRIVATE_KEY_LENGTH| will ensure the +// EVP_HPKE_KEY_private_key writes `key`'s private key to `out` and sets +// `*out_len` to the number of bytes written. On success, it returns one and +// writes at most `max_out` bytes. If `max_out` is too small, it returns zero. +// Setting `max_out` to `EVP_HPKE_MAX_PRIVATE_KEY_LENGTH` will ensure the // private key fits. An exact size can also be determined by -// |EVP_HPKE_KEM_private_key_len|. +// `EVP_HPKE_KEM_private_key_len`. OPENSSL_EXPORT int EVP_HPKE_KEY_private_key(const EVP_HPKE_KEY *key, uint8_t *out, size_t *out_len, size_t max_out); @@ -201,47 +209,47 @@ OPENSSL_EXPORT int EVP_HPKE_KEY_private_key(const EVP_HPKE_KEY *key, // Encryption contexts. // -// An HPKE encryption context is represented by the |EVP_HPKE_CTX| type. +// An HPKE encryption context is represented by the `EVP_HPKE_CTX` type. -// EVP_HPKE_CTX_zero sets an uninitialized |EVP_HPKE_CTX| to the zero state. The -// caller should then use one of the |EVP_HPKE_CTX_setup_*| functions to finish -// setting up |ctx|. +// EVP_HPKE_CTX_zero sets an uninitialized `EVP_HPKE_CTX` to the zero state. The +// caller should then use one of the `EVP_HPKE_CTX_setup_*` functions to finish +// setting up `ctx`. // -// It is safe, but not necessary to call |EVP_HPKE_CTX_cleanup| in this state. -// This may be used for more uniform cleanup of |EVP_HPKE_CTX|. +// It is safe, but not necessary to call `EVP_HPKE_CTX_cleanup` in this state. +// This may be used for more uniform cleanup of `EVP_HPKE_CTX`. OPENSSL_EXPORT void EVP_HPKE_CTX_zero(EVP_HPKE_CTX *ctx); -// EVP_HPKE_CTX_cleanup releases memory referenced by |ctx|. |ctx| must have -// been initialized with |EVP_HPKE_CTX_zero| or one of the -// |EVP_HPKE_CTX_setup_*| functions. +// EVP_HPKE_CTX_cleanup releases memory referenced by `ctx`. `ctx` must have +// been initialized with `EVP_HPKE_CTX_zero` or one of the +// `EVP_HPKE_CTX_setup_*` functions. OPENSSL_EXPORT void EVP_HPKE_CTX_cleanup(EVP_HPKE_CTX *ctx); -// EVP_HPKE_CTX_new returns a newly-allocated |EVP_HPKE_CTX|, or NULL on error. -// The caller must call |EVP_HPKE_CTX_free| on the result to release it. +// EVP_HPKE_CTX_new returns a newly-allocated `EVP_HPKE_CTX`, or NULL on error. +// The caller must call `EVP_HPKE_CTX_free` on the result to release it. // // This is a convenience function for callers that need a heap-allocated -// |EVP_HPKE_CTX|. +// `EVP_HPKE_CTX`. OPENSSL_EXPORT EVP_HPKE_CTX *EVP_HPKE_CTX_new(void); -// EVP_HPKE_CTX_free releases memory associated with |ctx|, which must have been -// created with |EVP_HPKE_CTX_new|. +// EVP_HPKE_CTX_free releases memory associated with `ctx`, which must have been +// created with `EVP_HPKE_CTX_new`. OPENSSL_EXPORT void EVP_HPKE_CTX_free(EVP_HPKE_CTX *ctx); // EVP_HPKE_CTX_setup_sender implements the SetupBaseS HPKE operation. It -// encapsulates a shared secret for |peer_public_key| and sets up |ctx| as a -// sender context. It writes the encapsulated shared secret to |out_enc| and -// sets |*out_enc_len| to the number of bytes written. It writes at most -// |max_enc| bytes and fails if the buffer is too small. Setting |max_enc| to at -// least |EVP_HPKE_MAX_ENC_LENGTH| will ensure the buffer is large enough. An -// exact size may also be determined by |EVP_PKEY_KEM_enc_len|. +// encapsulates a shared secret for `peer_public_key` and sets up `ctx` as a +// sender context. It writes the encapsulated shared secret to `out_enc` and +// sets `*out_enc_len` to the number of bytes written. It writes at most +// `max_enc` bytes and fails if the buffer is too small. Setting `max_enc` to at +// least `EVP_HPKE_MAX_ENC_LENGTH` will ensure the buffer is large enough. An +// exact size may also be determined by `EVP_HPKE_KEM_enc_len`. // // This function returns one on success and zero on error. Note that -// |peer_public_key| may be invalid, in which case this function will return an +// `peer_public_key` may be invalid, in which case this function will return an // error. // -// On success, callers may call |EVP_HPKE_CTX_seal| to encrypt messages for the -// recipient. Callers must then call |EVP_HPKE_CTX_cleanup| when done. On -// failure, calling |EVP_HPKE_CTX_cleanup| is safe, but not required. +// On success, callers may call `EVP_HPKE_CTX_seal` to encrypt messages for the +// recipient. Callers must then call `EVP_HPKE_CTX_cleanup` when done. On +// failure, calling `EVP_HPKE_CTX_cleanup` is safe, but not required. OPENSSL_EXPORT int EVP_HPKE_CTX_setup_sender( EVP_HPKE_CTX *ctx, uint8_t *out_enc, size_t *out_enc_len, size_t max_enc, const EVP_HPKE_KEM *kem, const EVP_HPKE_KDF *kdf, const EVP_HPKE_AEAD *aead, @@ -249,8 +257,8 @@ OPENSSL_EXPORT int EVP_HPKE_CTX_setup_sender( const uint8_t *info, size_t info_len); // EVP_HPKE_CTX_setup_sender_with_seed_for_testing behaves like -// |EVP_HPKE_CTX_setup_sender|, but takes a seed to behave deterministically. -// The seed's format depends on |kem|. For X25519, it is the sender's +// `EVP_HPKE_CTX_setup_sender`, but takes a seed to behave deterministically. +// The seed's format depends on `kem`. For X25519, it is the sender's // ephemeral private key. For P256, it's an HKDF input. OPENSSL_EXPORT int EVP_HPKE_CTX_setup_sender_with_seed_for_testing( EVP_HPKE_CTX *ctx, uint8_t *out_enc, size_t *out_enc_len, size_t max_enc, @@ -259,21 +267,40 @@ OPENSSL_EXPORT int EVP_HPKE_CTX_setup_sender_with_seed_for_testing( const uint8_t *info, size_t info_len, const uint8_t *seed, size_t seed_len); // EVP_HPKE_CTX_setup_recipient implements the SetupBaseR HPKE operation. It -// decapsulates the shared secret in |enc| with |key| and sets up |ctx| as a +// decapsulates the shared secret in `enc` with `key` and sets up `ctx` as a // recipient context. It returns one on success and zero on failure. Note that -// |enc| may be invalid, in which case this function will return an error. +// `enc` may be invalid, in which case this function will return an error. // -// On success, callers may call |EVP_HPKE_CTX_open| to decrypt messages from the -// sender. Callers must then call |EVP_HPKE_CTX_cleanup| when done. On failure, -// calling |EVP_HPKE_CTX_cleanup| is safe, but not required. +// On success, callers may call `EVP_HPKE_CTX_open` to decrypt messages from the +// sender. Callers must then call `EVP_HPKE_CTX_cleanup` when done. On failure, +// calling `EVP_HPKE_CTX_cleanup` is safe, but not required. OPENSSL_EXPORT int EVP_HPKE_CTX_setup_recipient( EVP_HPKE_CTX *ctx, const EVP_HPKE_KEY *key, const EVP_HPKE_KDF *kdf, const EVP_HPKE_AEAD *aead, const uint8_t *enc, size_t enc_len, const uint8_t *info, size_t info_len); +// EVP_HPKE_CTX_setup_recipient_with_shared_secret behaves like +// `EVP_HPKE_CTX_setup_recipient`, but takes the shared secret explicitly. +// +// WARNING: `shared_secret` must be the result of decapsulating the sender's +// `enc` value with the recipient's private key. Using the wrong shared secret +// will result in an `EVP_HPKE_CTX` that cannot decrypt the sender's messages. +// Additionally, an attacker with knowledge of `shared_secret` will be able to +// decrypt and forge encrypted messages. +// +// It returns one on success and zero on error. +// +// On success, callers may call `EVP_HPKE_CTX_open` to decrypt messages from the +// sender. Callers must then call `EVP_HPKE_CTX_cleanup` when done. On failure, +// calling `EVP_HPKE_CTX_cleanup` is safe, but not required. +OPENSSL_EXPORT int EVP_HPKE_CTX_setup_recipient_with_shared_secret( + EVP_HPKE_CTX *ctx, const EVP_HPKE_KEM *kem, const EVP_HPKE_KDF *kdf, + const EVP_HPKE_AEAD *aead, const uint8_t *shared_secret, + size_t shared_secret_len, const uint8_t *info, size_t info_len); + // EVP_HPKE_CTX_setup_auth_sender implements the SetupAuthS HPKE operation. It -// behaves like |EVP_HPKE_CTX_setup_sender| but authenticates the resulting -// context with |key|. +// behaves like `EVP_HPKE_CTX_setup_sender` but authenticates the resulting +// context with `key`. OPENSSL_EXPORT int EVP_HPKE_CTX_setup_auth_sender( EVP_HPKE_CTX *ctx, uint8_t *out_enc, size_t *out_enc_len, size_t max_enc, const EVP_HPKE_KEY *key, const EVP_HPKE_KDF *kdf, const EVP_HPKE_AEAD *aead, @@ -281,8 +308,8 @@ OPENSSL_EXPORT int EVP_HPKE_CTX_setup_auth_sender( const uint8_t *info, size_t info_len); // EVP_HPKE_CTX_setup_auth_sender_with_seed_for_testing behaves like -// |EVP_HPKE_CTX_setup_auth_sender|, but takes a seed to behave -// deterministically. The seed's format depends on |kem|. For X25519, it is the +// `EVP_HPKE_CTX_setup_auth_sender`, but takes a seed to behave +// deterministically. The seed's format depends on `kem`. For X25519, it is the // sender's ephemeral private key. For P256, it's an HKDF input. OPENSSL_EXPORT int EVP_HPKE_CTX_setup_auth_sender_with_seed_for_testing( EVP_HPKE_CTX *ctx, uint8_t *out_enc, size_t *out_enc_len, size_t max_enc, @@ -291,8 +318,8 @@ OPENSSL_EXPORT int EVP_HPKE_CTX_setup_auth_sender_with_seed_for_testing( const uint8_t *info, size_t info_len, const uint8_t *seed, size_t seed_len); // EVP_HPKE_CTX_setup_auth_recipient implements the SetupAuthR HPKE operation. -// It behaves like |EVP_HPKE_CTX_setup_recipient| but checks the resulting -// context was authenticated with |peer_public_key|. +// It behaves like `EVP_HPKE_CTX_setup_recipient` but checks the resulting +// context was authenticated with `peer_public_key`. OPENSSL_EXPORT int EVP_HPKE_CTX_setup_auth_recipient( EVP_HPKE_CTX *ctx, const EVP_HPKE_KEY *key, const EVP_HPKE_KDF *kdf, const EVP_HPKE_AEAD *aead, const uint8_t *enc, size_t enc_len, @@ -302,50 +329,50 @@ OPENSSL_EXPORT int EVP_HPKE_CTX_setup_auth_recipient( // Using an HPKE context. // -// Once set up, callers may encrypt or decrypt with an |EVP_HPKE_CTX| using the +// Once set up, callers may encrypt or decrypt with an `EVP_HPKE_CTX` using the // following functions. -// EVP_HPKE_CTX_open uses the HPKE context |ctx| to authenticate |in_len| bytes -// from |in| and |ad_len| bytes from |ad| and to decrypt at most |in_len| bytes -// into |out|. It returns one on success, and zero otherwise. +// EVP_HPKE_CTX_open uses the HPKE context `ctx` to authenticate `in_len` bytes +// from `in` and `ad_len` bytes from `ad` and to decrypt at most `in_len` bytes +// into `out`. It returns one on success, and zero otherwise. // -// This operation will fail if the |ctx| context is not set up as a receiver. +// This operation will fail if the `ctx` context is not set up as a receiver. // // Note that HPKE encryption is stateful and ordered. The sender's first call to -// |EVP_HPKE_CTX_seal| must correspond to the recipient's first call to -// |EVP_HPKE_CTX_open|, etc. +// `EVP_HPKE_CTX_seal` must correspond to the recipient's first call to +// `EVP_HPKE_CTX_open`, etc. // -// At most |in_len| bytes are written to |out|. In order to ensure success, -// |max_out_len| should be at least |in_len|. On successful return, |*out_len| +// At most `in_len` bytes are written to `out`. In order to ensure success, +// `max_out_len` should be at least `in_len`. On successful return, `*out_len` // is set to the actual number of bytes written. OPENSSL_EXPORT int EVP_HPKE_CTX_open(EVP_HPKE_CTX *ctx, uint8_t *out, size_t *out_len, size_t max_out_len, const uint8_t *in, size_t in_len, const uint8_t *ad, size_t ad_len); -// EVP_HPKE_CTX_seal uses the HPKE context |ctx| to encrypt and authenticate -// |in_len| bytes of ciphertext |in| and authenticate |ad_len| bytes from |ad|, -// writing the result to |out|. It returns one on success and zero otherwise. +// EVP_HPKE_CTX_seal uses the HPKE context `ctx` to encrypt and authenticate +// `in_len` bytes of ciphertext `in` and authenticate `ad_len` bytes from `ad`, +// writing the result to `out`. It returns one on success and zero otherwise. // -// This operation will fail if the |ctx| context is not set up as a sender. +// This operation will fail if the `ctx` context is not set up as a sender. // // Note that HPKE encryption is stateful and ordered. The sender's first call to -// |EVP_HPKE_CTX_seal| must correspond to the recipient's first call to -// |EVP_HPKE_CTX_open|, etc. +// `EVP_HPKE_CTX_seal` must correspond to the recipient's first call to +// `EVP_HPKE_CTX_open`, etc. // -// At most, |max_out_len| encrypted bytes are written to |out|. On successful -// return, |*out_len| is set to the actual number of bytes written. +// At most, `max_out_len` encrypted bytes are written to `out`. On successful +// return, `*out_len` is set to the actual number of bytes written. // -// To ensure success, |max_out_len| should be |in_len| plus the result of -// |EVP_HPKE_CTX_max_overhead| or |EVP_HPKE_MAX_OVERHEAD|. +// To ensure success, `max_out_len` should be `in_len` plus the result of +// `EVP_HPKE_CTX_max_overhead` or `EVP_HPKE_MAX_OVERHEAD`. OPENSSL_EXPORT int EVP_HPKE_CTX_seal(EVP_HPKE_CTX *ctx, uint8_t *out, size_t *out_len, size_t max_out_len, const uint8_t *in, size_t in_len, const uint8_t *ad, size_t ad_len); -// EVP_HPKE_CTX_export uses the HPKE context |ctx| to export a secret of -// |secret_len| bytes into |out|. This function uses |context_len| bytes from -// |context| as a context string for the secret. This is necessary to separate +// EVP_HPKE_CTX_export uses the HPKE context `ctx` to export a secret of +// `secret_len` bytes into `out`. This function uses `context_len` bytes from +// `context` as a context string for the secret. This is necessary to separate // different uses of exported secrets and bind relevant caller-specific context // into the output. It returns one on success and zero otherwise. OPENSSL_EXPORT int EVP_HPKE_CTX_export(const EVP_HPKE_CTX *ctx, uint8_t *out, @@ -354,23 +381,23 @@ OPENSSL_EXPORT int EVP_HPKE_CTX_export(const EVP_HPKE_CTX *ctx, uint8_t *out, size_t context_len); // EVP_HPKE_MAX_OVERHEAD contains the largest value that -// |EVP_HPKE_CTX_max_overhead| would ever return for any context. +// `EVP_HPKE_CTX_max_overhead` would ever return for any context. #define EVP_HPKE_MAX_OVERHEAD EVP_AEAD_MAX_OVERHEAD // EVP_HPKE_CTX_max_overhead returns the maximum number of additional bytes -// added by sealing data with |EVP_HPKE_CTX_seal|. The |ctx| context must be set +// added by sealing data with `EVP_HPKE_CTX_seal`. The `ctx` context must be set // up as a sender. OPENSSL_EXPORT size_t EVP_HPKE_CTX_max_overhead(const EVP_HPKE_CTX *ctx); -// EVP_HPKE_CTX_kem returns |ctx|'s configured KEM, or NULL if the context has +// EVP_HPKE_CTX_kem returns `ctx`'s configured KEM, or NULL if the context has // not been set up. OPENSSL_EXPORT const EVP_HPKE_KEM *EVP_HPKE_CTX_kem(const EVP_HPKE_CTX *ctx); -// EVP_HPKE_CTX_aead returns |ctx|'s configured AEAD, or NULL if the context has +// EVP_HPKE_CTX_aead returns `ctx`'s configured AEAD, or NULL if the context has // not been set up. OPENSSL_EXPORT const EVP_HPKE_AEAD *EVP_HPKE_CTX_aead(const EVP_HPKE_CTX *ctx); -// EVP_HPKE_CTX_kdf returns |ctx|'s configured KDF, or NULL if the context has +// EVP_HPKE_CTX_kdf returns `ctx`'s configured KDF, or NULL if the context has // not been set up. OPENSSL_EXPORT const EVP_HPKE_KDF *EVP_HPKE_CTX_kdf(const EVP_HPKE_CTX *ctx); @@ -393,8 +420,7 @@ struct evp_hpke_ctx_st { struct evp_hpke_key_st { const EVP_HPKE_KEM *kem; - uint8_t private_key[EVP_HPKE_MAX_PRIVATE_KEY_LENGTH]; - uint8_t public_key[EVP_HPKE_MAX_PUBLIC_KEY_LENGTH]; + EVP_PKEY *pkey; }; @@ -407,12 +433,13 @@ extern "C++" { BSSL_NAMESPACE_BEGIN -using ScopedEVP_HPKE_CTX = - internal::StackAllocated; -using ScopedEVP_HPKE_KEY = - internal::StackAllocatedMovable; +BORINGSSL_MAKE_STACK_TRAITS(EVP_HPKE_CTX, EVP_HPKE_CTX_zero, + EVP_HPKE_CTX_cleanup) +using ScopedEVP_HPKE_CTX = internal::StackAllocated; + +BORINGSSL_MAKE_STACK_TRAITS_MOVABLE(EVP_HPKE_KEY, EVP_HPKE_KEY_zero, + EVP_HPKE_KEY_cleanup, EVP_HPKE_KEY_move) +using ScopedEVP_HPKE_KEY = internal::StackAllocatedMovable; BORINGSSL_MAKE_DELETER(EVP_HPKE_CTX, EVP_HPKE_CTX_free) BORINGSSL_MAKE_DELETER(EVP_HPKE_KEY, EVP_HPKE_KEY_free) diff --git a/third_party/boringssl/src/include/openssl/hrss.h b/third_party/boringssl/src/include/openssl/hrss.h index 53fdade1a..348ad4972 100644 --- a/third_party/boringssl/src/include/openssl/hrss.h +++ b/third_party/boringssl/src/include/openssl/hrss.h @@ -72,18 +72,18 @@ OPENSSL_EXPORT int HRSS_generate_key( const uint8_t input[HRSS_GENERATE_KEY_BYTES]); // HRSS_encap is a deterministic function the generates and encrypts a random -// session key from the given entropy, writing those values to |out_shared_key| -// and |out_ciphertext|, respectively. It returns one on success or zero on +// session key from the given entropy, writing those values to `out_shared_key` +// and `out_ciphertext`, respectively. It returns one on success or zero on // malloc failure. OPENSSL_EXPORT int HRSS_encap(uint8_t out_ciphertext[HRSS_CIPHERTEXT_BYTES], uint8_t out_shared_key[HRSS_KEY_BYTES], const struct HRSS_public_key *in_pub, const uint8_t in[HRSS_ENCAP_BYTES]); -// HRSS_decap decrypts a session key from |ciphertext_len| bytes of -// |ciphertext|. If the ciphertext is valid, the decrypted key is written to -// |out_shared_key|. Otherwise the HMAC of |ciphertext| under a secret key (kept -// in |in_priv|) is written. If the ciphertext is the wrong length then it will +// HRSS_decap decrypts a session key from `ciphertext_len` bytes of +// `ciphertext`. If the ciphertext is valid, the decrypted key is written to +// `out_shared_key`. Otherwise the HMAC of `ciphertext` under a secret key (kept +// in `in_priv`) is written. If the ciphertext is the wrong length then it will // leak which was done via side-channels. Otherwise it should perform either // action in constant-time. It returns one on success (whether the ciphertext // was valid or not) and zero on malloc failure. @@ -91,11 +91,11 @@ OPENSSL_EXPORT int HRSS_decap(uint8_t out_shared_key[HRSS_KEY_BYTES], const struct HRSS_private_key *in_priv, const uint8_t *ciphertext, size_t ciphertext_len); -// HRSS_marshal_public_key serialises |in_pub| to |out|. +// HRSS_marshal_public_key serialises `in_pub` to `out`. OPENSSL_EXPORT void HRSS_marshal_public_key( uint8_t out[HRSS_PUBLIC_KEY_BYTES], const struct HRSS_public_key *in_pub); -// HRSS_parse_public_key sets |*out| to the public-key encoded in |in|. It +// HRSS_parse_public_key sets `*out` to the public-key encoded in `in`. It // returns true on success and zero on error. OPENSSL_EXPORT int HRSS_parse_public_key( struct HRSS_public_key *out, const uint8_t in[HRSS_PUBLIC_KEY_BYTES]); diff --git a/third_party/boringssl/src/include/openssl/kdf.h b/third_party/boringssl/src/include/openssl/kdf.h index b434e2d88..c619af1a8 100644 --- a/third_party/boringssl/src/include/openssl/kdf.h +++ b/third_party/boringssl/src/include/openssl/kdf.h @@ -28,11 +28,11 @@ extern "C" { // HKDF-specific functions. // // The following functions are provided for OpenSSL compatibility. Prefer the -// HKDF functions in . In each, |ctx| must be created with -// |EVP_PKEY_CTX_new_id| with |EVP_PKEY_HKDF| and then initialized with -// |EVP_PKEY_derive_init|. +// HKDF functions in . In each, `ctx` must be created with +// `EVP_PKEY_CTX_new_id` with `EVP_PKEY_HKDF` and then initialized with +// `EVP_PKEY_derive_init`. -// EVP_PKEY_HKDEF_MODE_* define "modes" for use with |EVP_PKEY_CTX_hkdf_mode|. +// EVP_PKEY_HKDEF_MODE_* define "modes" for use with `EVP_PKEY_CTX_hkdf_mode`. // The misspelling of "HKDF" as "HKDEF" is intentional for OpenSSL // compatibility. #define EVP_PKEY_HKDEF_MODE_EXTRACT_AND_EXPAND 0 @@ -40,12 +40,12 @@ extern "C" { #define EVP_PKEY_HKDEF_MODE_EXPAND_ONLY 2 // EVP_PKEY_CTX_hkdf_mode configures which HKDF operation to run. It returns one -// on success and zero on error. |mode| must be one of |EVP_PKEY_HKDEF_MODE_*|. -// By default, the mode is |EVP_PKEY_HKDEF_MODE_EXTRACT_AND_EXPAND|. +// on success and zero on error. `mode` must be one of `EVP_PKEY_HKDEF_MODE_*`. +// By default, the mode is `EVP_PKEY_HKDEF_MODE_EXTRACT_AND_EXPAND`. // -// If |mode| is |EVP_PKEY_HKDEF_MODE_EXTRACT_AND_EXPAND| or -// |EVP_PKEY_HKDEF_MODE_EXPAND_ONLY|, the output is variable-length. -// |EVP_PKEY_derive| uses the size of the output buffer as the output length for +// If `mode` is `EVP_PKEY_HKDEF_MODE_EXTRACT_AND_EXPAND` or +// `EVP_PKEY_HKDEF_MODE_EXPAND_ONLY`, the output is variable-length. +// `EVP_PKEY_derive` uses the size of the output buffer as the output length for // HKDF-Expand. // // WARNING: Although this API calls it a "mode", HKDF-Extract and HKDF-Expand @@ -53,31 +53,31 @@ extern "C" { // Callers should not pass input secrets for one operation into the other. OPENSSL_EXPORT int EVP_PKEY_CTX_hkdf_mode(EVP_PKEY_CTX *ctx, int mode); -// EVP_PKEY_CTX_set_hkdf_md sets |md| as the digest to use with HKDF. It returns +// EVP_PKEY_CTX_set_hkdf_md sets `md` as the digest to use with HKDF. It returns // one on success and zero on error. OPENSSL_EXPORT int EVP_PKEY_CTX_set_hkdf_md(EVP_PKEY_CTX *ctx, const EVP_MD *md); -// EVP_PKEY_CTX_set1_hkdf_key configures HKDF to use |key_len| bytes from |key| +// EVP_PKEY_CTX_set1_hkdf_key configures HKDF to use `key_len` bytes from `key` // as the "key", described below. It returns one on success and zero on error. // -// Which input is the key depends on the "mode" (see |EVP_PKEY_CTX_hkdf_mode|). -// If |EVP_PKEY_HKDEF_MODE_EXTRACT_AND_EXPAND| or -// |EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY|, this function specifies the input keying -// material (IKM) for HKDF-Extract. If |EVP_PKEY_HKDEF_MODE_EXPAND_ONLY|, it +// Which input is the key depends on the "mode" (see `EVP_PKEY_CTX_hkdf_mode`). +// If `EVP_PKEY_HKDEF_MODE_EXTRACT_AND_EXPAND` or +// `EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY`, this function specifies the input keying +// material (IKM) for HKDF-Extract. If `EVP_PKEY_HKDEF_MODE_EXPAND_ONLY`, it // instead specifies the pseudorandom key (PRK) for HKDF-Expand. OPENSSL_EXPORT int EVP_PKEY_CTX_set1_hkdf_key(EVP_PKEY_CTX *ctx, const uint8_t *key, size_t key_len); -// EVP_PKEY_CTX_set1_hkdf_salt configures HKDF to use |salt_len| bytes from -// |salt| as the salt parameter to HKDF-Extract. It returns one on success and +// EVP_PKEY_CTX_set1_hkdf_salt configures HKDF to use `salt_len` bytes from +// `salt` as the salt parameter to HKDF-Extract. It returns one on success and // zero on error. If performing HKDF-Expand only, this parameter is ignored. OPENSSL_EXPORT int EVP_PKEY_CTX_set1_hkdf_salt(EVP_PKEY_CTX *ctx, const uint8_t *salt, size_t salt_len); -// EVP_PKEY_CTX_add1_hkdf_info appends |info_len| bytes from |info| to the info +// EVP_PKEY_CTX_add1_hkdf_info appends `info_len` bytes from `info` to the info // parameter used with HKDF-Expand. It returns one on success and zero on error. // If performing HKDF-Extract only, this parameter is ignored. OPENSSL_EXPORT int EVP_PKEY_CTX_add1_hkdf_info(EVP_PKEY_CTX *ctx, diff --git a/third_party/boringssl/src/include/openssl/md4.h b/third_party/boringssl/src/include/openssl/md4.h index 6703d72d8..a43c2d8bb 100644 --- a/third_party/boringssl/src/include/openssl/md4.h +++ b/third_party/boringssl/src/include/openssl/md4.h @@ -37,24 +37,24 @@ extern "C" { // MD4_DIGEST_LENGTH is the length of an MD4 digest. #define MD4_DIGEST_LENGTH 16 -// MD4_Init initialises |md4| and returns one. +// MD4_Init initialises `md4` and returns one. OPENSSL_EXPORT int MD4_Init(MD4_CTX *md4); -// MD4_Update adds |len| bytes from |data| to |md4| and returns one. +// MD4_Update adds `len` bytes from `data` to `md4` and returns one. OPENSSL_EXPORT int MD4_Update(MD4_CTX *md4, const void *data, size_t len); -// MD4_Final adds the final padding to |md4| and writes the resulting digest to -// |out|, which must have at least |MD4_DIGEST_LENGTH| bytes of space. It +// MD4_Final adds the final padding to `md4` and writes the resulting digest to +// `out`, which must have at least `MD4_DIGEST_LENGTH` bytes of space. It // returns one. OPENSSL_EXPORT int MD4_Final(uint8_t out[MD4_DIGEST_LENGTH], MD4_CTX *md4); -// MD4 writes the digest of |len| bytes from |data| to |out| and returns |out|. -// There must be at least |MD4_DIGEST_LENGTH| bytes of space in |out|. +// MD4 writes the digest of `len` bytes from `data` to `out` and returns `out`. +// There must be at least `MD4_DIGEST_LENGTH` bytes of space in `out`. OPENSSL_EXPORT uint8_t *MD4(const uint8_t *data, size_t len, uint8_t out[MD4_DIGEST_LENGTH]); // MD4_Transform is a low-level function that performs a single, MD4 block -// transformation using the state from |md4| and 64 bytes from |block|. +// transformation using the state from `md4` and 64 bytes from `block`. OPENSSL_EXPORT void MD4_Transform(MD4_CTX *md4, const uint8_t block[MD4_CBLOCK]); diff --git a/third_party/boringssl/src/include/openssl/md5.h b/third_party/boringssl/src/include/openssl/md5.h index 1e7d51907..df069ddce 100644 --- a/third_party/boringssl/src/include/openssl/md5.h +++ b/third_party/boringssl/src/include/openssl/md5.h @@ -31,24 +31,44 @@ extern "C" { // MD5_DIGEST_LENGTH is the length of an MD5 digest. #define MD5_DIGEST_LENGTH 16 -// MD5_Init initialises |md5| and returns one. +// MD5_STATE_MAX_BYTES is the maximum length, in bytes, of a marshaled `MD5_CTX` +// state. +#define MD5_STATE_MAX_BYTES (16 + 8 + MD5_CBLOCK - 1) + +// MD5_Init initialises `md5` and returns one. OPENSSL_EXPORT int MD5_Init(MD5_CTX *md5); -// MD5_Update adds |len| bytes from |data| to |md5| and returns one. +// MD5_Update adds `len` bytes from `data` to `md5` and returns one. OPENSSL_EXPORT int MD5_Update(MD5_CTX *md5, const void *data, size_t len); -// MD5_Final adds the final padding to |md5| and writes the resulting digest to -// |out|, which must have at least |MD5_DIGEST_LENGTH| bytes of space. It +// MD5_Final adds the final padding to `md5` and writes the resulting digest to +// `out`, which must have at least `MD5_DIGEST_LENGTH` bytes of space. It // returns one. OPENSSL_EXPORT int MD5_Final(uint8_t out[MD5_DIGEST_LENGTH], MD5_CTX *md5); -// MD5 writes the digest of |len| bytes from |data| to |out| and returns |out|. -// There must be at least |MD5_DIGEST_LENGTH| bytes of space in |out|. +// MD5 writes the digest of `len` bytes from `data` to `out` and returns `out`. +// There must be at least `MD5_DIGEST_LENGTH` bytes of space in `out`. OPENSSL_EXPORT uint8_t *MD5(const uint8_t *data, size_t len, uint8_t out[MD5_DIGEST_LENGTH]); +// MD5_marshal_state serializes the intermediate hash state in `md5` into `out`, +// sets `*out_len` to the number of bytes written (at most +// `MD5_STATE_MAX_BYTES`), and returns one. `md5` is not modified. The +// serialized format is guaranteed to be backward- and forward-compatible across +// versions of BoringSSL. +OPENSSL_EXPORT int MD5_marshal_state(const MD5_CTX *md5, + uint8_t out[MD5_STATE_MAX_BYTES], + size_t *out_len); + +// MD5_parse_state deserializes `in_len` bytes from `in` (in the format produced +// by `MD5_marshal_state`) into `md5` and returns one. If `in` is malformed, it +// returns zero and leaves `md5` unmodified. The serialized format is guaranteed +// to be backward- and forward-compatible across versions of BoringSSL. +OPENSSL_EXPORT int MD5_parse_state(MD5_CTX *md5, const uint8_t *in, + size_t in_len); + // MD5_Transform is a low-level function that performs a single, MD5 block -// transformation using the state from |md5| and 64 bytes from |block|. +// transformation using the state from `md5` and 64 bytes from `block`. OPENSSL_EXPORT void MD5_Transform(MD5_CTX *md5, const uint8_t block[MD5_CBLOCK]); diff --git a/third_party/boringssl/src/include/openssl/mem.h b/third_party/boringssl/src/include/openssl/mem.h index e0ac688b2..ca265d0f3 100644 --- a/third_party/boringssl/src/include/openssl/mem.h +++ b/third_party/boringssl/src/include/openssl/mem.h @@ -15,10 +15,10 @@ #ifndef OPENSSL_HEADER_MEM_H #define OPENSSL_HEADER_MEM_H -#include // IWYU pragma: export +#include // IWYU pragma: export -#include #include +#include #if defined(__cplusplus) extern "C" { @@ -29,45 +29,45 @@ extern "C" { // // BoringSSL has its own set of allocation functions, which keep track of // allocation lengths and zero them out before freeing. All memory returned by -// BoringSSL API calls must therefore generally be freed using |OPENSSL_free| +// BoringSSL API calls must therefore generally be freed using `OPENSSL_free` // unless stated otherwise. #ifndef _BORINGSSL_PROHIBIT_OPENSSL_MALLOC -// OPENSSL_malloc is similar to a regular |malloc|, but allocates additional -// private data. The resulting pointer must be freed with |OPENSSL_free|. In -// the case of a malloc failure, prior to returning NULL |OPENSSL_malloc| will -// push |ERR_R_MALLOC_FAILURE| onto the openssl error stack. +// OPENSSL_malloc is similar to a regular `malloc`, but allocates additional +// private data. The resulting pointer must be freed with `OPENSSL_free`. In +// the case of a malloc failure, prior to returning NULL `OPENSSL_malloc` will +// push `ERR_R_MALLOC_FAILURE` onto the openssl error stack. OPENSSL_EXPORT void *OPENSSL_malloc(size_t size); -// OPENSSL_zalloc behaves like |OPENSSL_malloc| except it also initializes the +// OPENSSL_zalloc behaves like `OPENSSL_malloc` except it also initializes the // resulting memory to zero. OPENSSL_EXPORT void *OPENSSL_zalloc(size_t size); -// OPENSSL_calloc is similar to a regular |calloc|, but allocates data with -// |OPENSSL_malloc|. On overflow, it will push |ERR_R_OVERFLOW| onto the error +// OPENSSL_calloc is similar to a regular `calloc`, but allocates data with +// `OPENSSL_malloc`. On overflow, it will push `ERR_R_OVERFLOW` onto the error // queue. OPENSSL_EXPORT void *OPENSSL_calloc(size_t num, size_t size); -// OPENSSL_realloc returns a pointer to a buffer of |new_size| bytes that -// contains the contents of |ptr|. Unlike |realloc|, a new buffer is always -// allocated and the data at |ptr| is always wiped and freed. Memory is -// allocated with |OPENSSL_malloc| and must be freed with |OPENSSL_free|. +// OPENSSL_realloc returns a pointer to a buffer of `new_size` bytes that +// contains the contents of `ptr`. Unlike `realloc`, a new buffer is always +// allocated and the data at `ptr` is always wiped and freed. Memory is +// allocated with `OPENSSL_malloc` and must be freed with `OPENSSL_free`. OPENSSL_EXPORT void *OPENSSL_realloc(void *ptr, size_t new_size); -#endif // !_BORINGSSL_PROHIBIT_OPENSSL_MALLOC +#endif // !_BORINGSSL_PROHIBIT_OPENSSL_MALLOC -// OPENSSL_free does nothing if |ptr| is NULL. Otherwise it zeros out the -// memory allocated at |ptr| and frees it along with the private data. -// It must only be used on on |ptr| values obtained from |OPENSSL_malloc| +// OPENSSL_free does nothing if `ptr` is NULL. Otherwise it zeros out the +// memory allocated at `ptr` and frees it along with the private data. +// It must only be used on `ptr` values obtained from `OPENSSL_malloc` OPENSSL_EXPORT void OPENSSL_free(void *ptr); -// OPENSSL_cleanse zeros out |len| bytes of memory at |ptr|. This is similar to -// |memset_s| from C11. +// OPENSSL_cleanse zeros out `len` bytes of memory at `ptr`. This is similar to +// `memset_s` from C11. OPENSSL_EXPORT void OPENSSL_cleanse(void *ptr, size_t len); -// CRYPTO_memcmp returns zero iff the |len| bytes at |a| and |b| are equal. It -// takes an amount of time dependent on |len|, but independent of the contents -// of |a| and |b|. Unlike memcmp, it cannot be used to put elements into a +// CRYPTO_memcmp returns zero iff the `len` bytes at `a` and `b` are equal. It +// takes an amount of time dependent on `len`, but independent of the contents +// of `a` and `b`. Unlike memcmp, it cannot be used to put elements into a // defined order as the return value when a != b is undefined, other than to be // non-zero. OPENSSL_EXPORT int CRYPTO_memcmp(const void *a, const void *b, size_t len); @@ -75,7 +75,7 @@ OPENSSL_EXPORT int CRYPTO_memcmp(const void *a, const void *b, size_t len); // OPENSSL_hash32 implements the 32 bit, FNV-1a hash. OPENSSL_EXPORT uint32_t OPENSSL_hash32(const void *ptr, size_t len); -// OPENSSL_strhash calls |OPENSSL_hash32| on the NUL-terminated string |s|. +// OPENSSL_strhash calls `OPENSSL_hash32` on the NUL-terminated string `s`. OPENSSL_EXPORT uint32_t OPENSSL_strhash(const char *s); // OPENSSL_strdup has the same behaviour as strdup(3). @@ -97,13 +97,13 @@ OPENSSL_EXPORT int OPENSSL_isdigit(int c); // digits. OPENSSL_EXPORT int OPENSSL_isxdigit(int c); -// OPENSSL_fromxdigit returns one if |c| is a hexadecimal digit as recognized -// by OPENSSL_isxdigit, and sets |out| to the corresponding value. Otherwise +// OPENSSL_fromxdigit returns one if `c` is a hexadecimal digit as recognized +// by OPENSSL_isxdigit, and sets `out` to the corresponding value. Otherwise // zero is returned. OPENSSL_EXPORT int OPENSSL_fromxdigit(uint8_t *out, int c); // OPENSSL_isalnum is a locale-independent, ASCII-only version of isalnum(3), It -// only recognizes what |OPENSSL_isalpha| and |OPENSSL_isdigit| recognize. +// only recognizes what `OPENSSL_isalpha` and `OPENSSL_isdigit` recognize. OPENSSL_EXPORT int OPENSSL_isalnum(int c); // OPENSSL_tolower is a locale-independent, ASCII-only version of tolower(3). It @@ -124,7 +124,7 @@ OPENSSL_EXPORT int OPENSSL_strncasecmp(const char *a, const char *b, size_t n); // DECIMAL_SIZE returns an upper bound for the length of the decimal // representation of the given type. -#define DECIMAL_SIZE(type) ((sizeof(type)*8+2)/3+1) +#define DECIMAL_SIZE(type) ((sizeof(type) * 8 + 2) / 3 + 1) // BIO_snprintf has the same behavior as snprintf(3). OPENSSL_EXPORT int BIO_snprintf(char *buf, size_t n, const char *format, ...) @@ -135,24 +135,24 @@ OPENSSL_EXPORT int BIO_vsnprintf(char *buf, size_t n, const char *format, va_list args) OPENSSL_PRINTF_FORMAT_FUNC(3, 0); // OPENSSL_vasprintf has the same behavior as vasprintf(3), except that -// memory allocated in a returned string must be freed with |OPENSSL_free|. +// memory allocated in a returned string must be freed with `OPENSSL_free`. OPENSSL_EXPORT int OPENSSL_vasprintf(char **str, const char *format, va_list args) OPENSSL_PRINTF_FORMAT_FUNC(2, 0); // OPENSSL_asprintf has the same behavior as asprintf(3), except that -// memory allocated in a returned string must be freed with |OPENSSL_free|. +// memory allocated in a returned string must be freed with `OPENSSL_free`. OPENSSL_EXPORT int OPENSSL_asprintf(char **str, const char *format, ...) OPENSSL_PRINTF_FORMAT_FUNC(2, 3); -// OPENSSL_strndup returns an allocated, duplicate of |str|, which is, at most, -// |size| bytes. The result is always NUL terminated. The memory allocated -// must be freed with |OPENSSL_free|. +// OPENSSL_strndup returns an allocated, duplicate of `str`, which is, at most, +// `size` bytes. The result is always NUL terminated. The memory allocated +// must be freed with `OPENSSL_free`. OPENSSL_EXPORT char *OPENSSL_strndup(const char *str, size_t size); -// OPENSSL_memdup returns an allocated, duplicate of |size| bytes from |data| or +// OPENSSL_memdup returns an allocated, duplicate of `size` bytes from `data` or // NULL on allocation failure. The memory allocated must be freed with -// |OPENSSL_free|. +// `OPENSSL_free`. OPENSSL_EXPORT void *OPENSSL_memdup(const void *data, size_t size); // OPENSSL_strlcpy acts like strlcpy(3). @@ -166,18 +166,18 @@ OPENSSL_EXPORT size_t OPENSSL_strlcat(char *dst, const char *src, // Deprecated functions. -// CRYPTO_malloc calls |OPENSSL_malloc|. |file| and |line| are ignored. +// CRYPTO_malloc calls `OPENSSL_malloc`. `file` and `line` are ignored. OPENSSL_EXPORT void *CRYPTO_malloc(size_t size, const char *file, int line); -// CRYPTO_realloc calls |OPENSSL_realloc|. |file| and |line| are ignored. +// CRYPTO_realloc calls `OPENSSL_realloc`. `file` and `line` are ignored. OPENSSL_EXPORT void *CRYPTO_realloc(void *ptr, size_t new_size, const char *file, int line); -// CRYPTO_free calls |OPENSSL_free|. |file| and |line| are ignored. +// CRYPTO_free calls `OPENSSL_free`. `file` and `line` are ignored. OPENSSL_EXPORT void CRYPTO_free(void *ptr, const char *file, int line); -// OPENSSL_clear_free calls |OPENSSL_free|. BoringSSL automatically clears all -// allocations on free, but we define |OPENSSL_clear_free| for compatibility. +// OPENSSL_clear_free calls `OPENSSL_free`. BoringSSL automatically clears all +// allocations on free, but we define `OPENSSL_clear_free` for compatibility. OPENSSL_EXPORT void OPENSSL_clear_free(void *ptr, size_t len); // CRYPTO_secure_malloc_init returns zero. @@ -189,10 +189,10 @@ OPENSSL_EXPORT int CRYPTO_secure_malloc_initialized(void); // CRYPTO_secure_used returns zero. OPENSSL_EXPORT size_t CRYPTO_secure_used(void); -// OPENSSL_secure_malloc calls |OPENSSL_malloc|. +// OPENSSL_secure_malloc calls `OPENSSL_malloc`. OPENSSL_EXPORT void *OPENSSL_secure_malloc(size_t size); -// OPENSSL_secure_clear_free calls |OPENSSL_clear_free|. +// OPENSSL_secure_clear_free calls `OPENSSL_clear_free`. OPENSSL_EXPORT void OPENSSL_secure_clear_free(void *ptr, size_t len); diff --git a/third_party/boringssl/src/include/openssl/mldsa.h b/third_party/boringssl/src/include/openssl/mldsa.h index 3d6b04c76..6651f75bb 100644 --- a/third_party/boringssl/src/include/openssl/mldsa.h +++ b/third_party/boringssl/src/include/openssl/mldsa.h @@ -74,8 +74,8 @@ struct MLDSA65_prehash { #define MLDSA65_SIGNATURE_BYTES 3309 // MLDSA65_generate_key generates a random public/private key pair, writes the -// encoded public key to |out_encoded_public_key|, writes the seed to -// |out_seed|, and sets |out_private_key| to the private key. Returns 1 on +// encoded public key to `out_encoded_public_key`, writes the seed to +// `out_seed`, and sets `out_private_key` to the private key. Returns 1 on // success and 0 on allocation failure. OPENSSL_EXPORT int MLDSA65_generate_key( uint8_t out_encoded_public_key[MLDSA65_PUBLIC_KEY_BYTES], @@ -83,34 +83,34 @@ OPENSSL_EXPORT int MLDSA65_generate_key( struct MLDSA65_private_key *out_private_key); // MLDSA65_private_key_from_seed regenerates a private key from a seed value -// that was generated by |MLDSA65_generate_key|. Returns 1 on success and 0 on -// allocation failure or if |seed_len| is incorrect. +// that was generated by `MLDSA65_generate_key`. Returns 1 on success and 0 on +// allocation failure or if `seed_len` is incorrect. OPENSSL_EXPORT int MLDSA65_private_key_from_seed( struct MLDSA65_private_key *out_private_key, const uint8_t *seed, size_t seed_len); -// MLDSA65_public_from_private sets |*out_public_key| to the public key that -// corresponds to |private_key|. Returns 1 on success and 0 on failure. +// MLDSA65_public_from_private sets `*out_public_key` to the public key that +// corresponds to `private_key`. Returns 1 on success and 0 on failure. OPENSSL_EXPORT int MLDSA65_public_from_private( struct MLDSA65_public_key *out_public_key, const struct MLDSA65_private_key *private_key); -// MLDSA65_sign generates a signature for the message |msg| of length -// |msg_len| using |private_key| (following the randomized algorithm), and -// writes the encoded signature to |out_encoded_signature|. The |context| +// MLDSA65_sign generates a signature for the message `msg` of length +// `msg_len` using `private_key` (following the randomized algorithm), and +// writes the encoded signature to `out_encoded_signature`. The `context` // argument is also signed over and can be used to include implicit contextual -// information that isn't included in |msg|. The same value of |context| must be -// presented to |MLDSA65_verify| in order for the generated signature to be -// considered valid. |context| and |context_len| may be |NULL| and 0 to use an +// information that isn't included in `msg`. The same value of `context` must be +// presented to `MLDSA65_verify` in order for the generated signature to be +// considered valid. `context` and `context_len` may be `NULL` and 0 to use an // empty context (this is common). Returns 1 on success and 0 on failure. OPENSSL_EXPORT int MLDSA65_sign( uint8_t out_encoded_signature[MLDSA65_SIGNATURE_BYTES], const struct MLDSA65_private_key *private_key, const uint8_t *msg, size_t msg_len, const uint8_t *context, size_t context_len); -// MLDSA65_verify verifies that |signature| constitutes a valid -// signature for the message |msg| of length |msg_len| using |public_key|. The -// value of |context| must equal the value that was passed to |MLDSA65_sign| +// MLDSA65_verify verifies that `signature` constitutes a valid +// signature for the message `msg` of length `msg_len` using `public_key`. The +// value of `context` must equal the value that was passed to `MLDSA65_sign` // when the signature was generated. Returns 1 on success or 0 on error. OPENSSL_EXPORT int MLDSA65_verify(const struct MLDSA65_public_key *public_key, const uint8_t *signature, @@ -118,11 +118,11 @@ OPENSSL_EXPORT int MLDSA65_verify(const struct MLDSA65_public_key *public_key, size_t msg_len, const uint8_t *context, size_t context_len); -// MLDSA65_prehash_init initializes a pre-hashing state using |public_key|. The -// |context| argument can be used to include implicit contextual information -// that isn't included in the message. The same value of |context| must be -// presented to |MLDSA65_verify| in order for the generated signature to be -// considered valid. |context| and |context_len| may be |NULL| and 0 to use an +// MLDSA65_prehash_init initializes a pre-hashing state using `public_key`. The +// `context` argument can be used to include implicit contextual information +// that isn't included in the message. The same value of `context` must be +// presented to `MLDSA65_verify` in order for the generated signature to be +// considered valid. `context` and `context_len` may be `NULL` and 0 to use an // empty context (this is common). Returns 1 on success and 0 on failure (if the // context is too long). OPENSSL_EXPORT int MLDSA65_prehash_init( @@ -130,26 +130,26 @@ OPENSSL_EXPORT int MLDSA65_prehash_init( const struct MLDSA65_public_key *public_key, const uint8_t *context, size_t context_len); -// MLDSA65_prehash_update incorporates the given |msg| of length |msg_len| into +// MLDSA65_prehash_update incorporates the given `msg` of length `msg_len` into // the pre-hashing state. This can be called multiple times on successive chunks -// of the message. This should be called after |MLDSA65_prehash_init| and before -// |MLDSA65_prehash_finalize|. +// of the message. This should be called after `MLDSA65_prehash_init` and before +// `MLDSA65_prehash_finalize`. OPENSSL_EXPORT void MLDSA65_prehash_update(struct MLDSA65_prehash *inout_state, const uint8_t *msg, size_t msg_len); // MLDSA65_prehash_finalize extracts a pre-hashed message representative from // the given pre-hashing state. This should be called after -// |MLDSA65_prehash_init| and |MLDSA65_prehash_update|. The resulting -// |out_msg_rep| should then be passed to |MLDSA65_sign_message_representative| +// `MLDSA65_prehash_init` and `MLDSA65_prehash_update`. The resulting +// `out_msg_rep` should then be passed to `MLDSA65_sign_message_representative` // to obtain a signature. OPENSSL_EXPORT void MLDSA65_prehash_finalize( uint8_t out_msg_rep[MLDSA_MU_BYTES], struct MLDSA65_prehash *inout_state); // MLDSA65_sign_message_representative generates a signature for the pre-hashed -// message |msg_rep| using |private_key| (following the randomized algorithm), -// and writes the encoded signature to |out_encoded_signature|. The |msg_rep| -// should be obtained via calls to |MLDSA65_prehash_init|, -// |MLDSA65_prehash_update| and |MLDSA65_prehash_finalize| using the public key +// message `msg_rep` using `private_key` (following the randomized algorithm), +// and writes the encoded signature to `out_encoded_signature`. The `msg_rep` +// should be obtained via calls to `MLDSA65_prehash_init`, +// `MLDSA65_prehash_update` and `MLDSA65_prehash_finalize` using the public key // from the same key pair, otherwise the signature will not verify. Returns 1 on // success and 0 on failure. OPENSSL_EXPORT int MLDSA65_sign_message_representative( @@ -157,10 +157,10 @@ OPENSSL_EXPORT int MLDSA65_sign_message_representative( const struct MLDSA65_private_key *private_key, const uint8_t msg_rep[MLDSA_MU_BYTES]); -// MLDSA65_verify_message_representative verifies that |signature| constitutes a -// valid signature for the pre-hashed message |msg_rep| using |public_key|. The -// |msg_rep| should be obtained via calls to |MLDSA65_prehash_init|, -// |MLDSA65_prehash_update| and |MLDSA65_prehash_finalize| using |public key| +// MLDSA65_verify_message_representative verifies that `signature` constitutes a +// valid signature for the pre-hashed message `msg_rep` using `public_key`. The +// `msg_rep` should be obtained via calls to `MLDSA65_prehash_init`, +// `MLDSA65_prehash_update` and `MLDSA65_prehash_finalize` using `public_key` // and the same context as when the signature was generated. Returns 1 on // success or 0 on error. OPENSSL_EXPORT int MLDSA65_verify_message_representative( @@ -168,16 +168,16 @@ OPENSSL_EXPORT int MLDSA65_verify_message_representative( const uint8_t *signature, size_t signature_len, const uint8_t msg_rep[MLDSA_MU_BYTES]); -// MLDSA65_marshal_public_key serializes |public_key| to |out| in the standard +// MLDSA65_marshal_public_key serializes `public_key` to `out` in the standard // format for ML-DSA-65 public keys. It returns 1 on success or 0 on // allocation error. OPENSSL_EXPORT int MLDSA65_marshal_public_key( CBB *out, const struct MLDSA65_public_key *public_key); // MLDSA65_parse_public_key parses a public key, in the format generated by -// |MLDSA65_marshal_public_key|, from |in| and writes the result to -// |out_public_key|. It returns 1 on success or 0 on parse error or if -// there are trailing bytes in |in|. +// `MLDSA65_marshal_public_key`, from `in` and writes the result to +// `out_public_key`. It returns 1 on success or 0 on parse error or if +// there are trailing bytes in `in`. OPENSSL_EXPORT int MLDSA65_parse_public_key( struct MLDSA65_public_key *public_key, CBS *in); @@ -224,8 +224,8 @@ struct MLDSA87_prehash { #define MLDSA87_SIGNATURE_BYTES 4627 // MLDSA87_generate_key generates a random public/private key pair, writes the -// encoded public key to |out_encoded_public_key|, writes the seed to -// |out_seed|, and sets |out_private_key| to the private key. Returns 1 on +// encoded public key to `out_encoded_public_key`, writes the seed to +// `out_seed`, and sets `out_private_key` to the private key. Returns 1 on // success and 0 on allocation failure. OPENSSL_EXPORT int MLDSA87_generate_key( uint8_t out_encoded_public_key[MLDSA87_PUBLIC_KEY_BYTES], @@ -233,34 +233,34 @@ OPENSSL_EXPORT int MLDSA87_generate_key( struct MLDSA87_private_key *out_private_key); // MLDSA87_private_key_from_seed regenerates a private key from a seed value -// that was generated by |MLDSA87_generate_key|. Returns 1 on success and 0 on -// allocation failure or if |seed_len| is incorrect. +// that was generated by `MLDSA87_generate_key`. Returns 1 on success and 0 on +// allocation failure or if `seed_len` is incorrect. OPENSSL_EXPORT int MLDSA87_private_key_from_seed( struct MLDSA87_private_key *out_private_key, const uint8_t *seed, size_t seed_len); -// MLDSA87_public_from_private sets |*out_public_key| to the public key that -// corresponds to |private_key|. Returns 1 on success and 0 on failure. +// MLDSA87_public_from_private sets `*out_public_key` to the public key that +// corresponds to `private_key`. Returns 1 on success and 0 on failure. OPENSSL_EXPORT int MLDSA87_public_from_private( struct MLDSA87_public_key *out_public_key, const struct MLDSA87_private_key *private_key); -// MLDSA87_sign generates a signature for the message |msg| of length -// |msg_len| using |private_key| (following the randomized algorithm), and -// writes the encoded signature to |out_encoded_signature|. The |context| +// MLDSA87_sign generates a signature for the message `msg` of length +// `msg_len` using `private_key` (following the randomized algorithm), and +// writes the encoded signature to `out_encoded_signature`. The `context` // argument is also signed over and can be used to include implicit contextual -// information that isn't included in |msg|. The same value of |context| must be -// presented to |MLDSA87_verify| in order for the generated signature to be -// considered valid. |context| and |context_len| may be |NULL| and 0 to use an +// information that isn't included in `msg`. The same value of `context` must be +// presented to `MLDSA87_verify` in order for the generated signature to be +// considered valid. `context` and `context_len` may be `NULL` and 0 to use an // empty context (this is common). Returns 1 on success and 0 on failure. OPENSSL_EXPORT int MLDSA87_sign( uint8_t out_encoded_signature[MLDSA87_SIGNATURE_BYTES], const struct MLDSA87_private_key *private_key, const uint8_t *msg, size_t msg_len, const uint8_t *context, size_t context_len); -// MLDSA87_verify verifies that |signature| constitutes a valid -// signature for the message |msg| of length |msg_len| using |public_key|. The -// value of |context| must equal the value that was passed to |MLDSA87_sign| +// MLDSA87_verify verifies that `signature` constitutes a valid +// signature for the message `msg` of length `msg_len` using `public_key`. The +// value of `context` must equal the value that was passed to `MLDSA87_sign` // when the signature was generated. Returns 1 on success or 0 on error. OPENSSL_EXPORT int MLDSA87_verify(const struct MLDSA87_public_key *public_key, const uint8_t *signature, @@ -268,11 +268,11 @@ OPENSSL_EXPORT int MLDSA87_verify(const struct MLDSA87_public_key *public_key, size_t msg_len, const uint8_t *context, size_t context_len); -// MLDSA87_prehash_init initializes a pre-hashing state using |public_key|. The -// |context| argument can be used to include implicit contextual information -// that isn't included in the message. The same value of |context| must be -// presented to |MLDSA87_verify| in order for the generated signature to be -// considered valid. |context| and |context_len| may be |NULL| and 0 to use an +// MLDSA87_prehash_init initializes a pre-hashing state using `public_key`. The +// `context` argument can be used to include implicit contextual information +// that isn't included in the message. The same value of `context` must be +// presented to `MLDSA87_verify` in order for the generated signature to be +// considered valid. `context` and `context_len` may be `NULL` and 0 to use an // empty context (this is common). Returns 1 on success and 0 on failure (if the // context is too long). OPENSSL_EXPORT int MLDSA87_prehash_init( @@ -280,26 +280,26 @@ OPENSSL_EXPORT int MLDSA87_prehash_init( const struct MLDSA87_public_key *public_key, const uint8_t *context, size_t context_len); -// MLDSA87_prehash_update incorporates the given |msg| of length |msg_len| into +// MLDSA87_prehash_update incorporates the given `msg` of length `msg_len` into // the pre-hashing state. This can be called multiple times on successive chunks -// of the message. This should be called after |MLDSA87_prehash_init| and before -// |MLDSA87_prehash_finalize|. +// of the message. This should be called after `MLDSA87_prehash_init` and before +// `MLDSA87_prehash_finalize`. OPENSSL_EXPORT void MLDSA87_prehash_update(struct MLDSA87_prehash *inout_state, const uint8_t *msg, size_t msg_len); // MLDSA87_prehash_finalize extracts a pre-hashed message representative from // the given pre-hashing state. This should be called after -// |MLDSA87_prehash_init| and |MLDSA87_prehash_update|. The resulting -// |out_msg_rep| should then be passed to |MLDSA87_sign_message_representative| +// `MLDSA87_prehash_init` and `MLDSA87_prehash_update`. The resulting +// `out_msg_rep` should then be passed to `MLDSA87_sign_message_representative` // to obtain a signature. OPENSSL_EXPORT void MLDSA87_prehash_finalize( uint8_t out_msg_rep[MLDSA_MU_BYTES], struct MLDSA87_prehash *inout_state); // MLDSA87_sign_message_representative generates a signature for the pre-hashed -// message |msg_rep| using |private_key| (following the randomized algorithm), -// and writes the encoded signature to |out_encoded_signature|. The |msg_rep| -// should be obtained via calls to |MLDSA87_prehash_init|, -// |MLDSA87_prehash_update| and |MLDSA87_prehash_finalize| using the public key +// message `msg_rep` using `private_key` (following the randomized algorithm), +// and writes the encoded signature to `out_encoded_signature`. The `msg_rep` +// should be obtained via calls to `MLDSA87_prehash_init`, +// `MLDSA87_prehash_update` and `MLDSA87_prehash_finalize` using the public key // from the same key pair, otherwise the signature will not verify. Returns 1 on // success and 0 on failure. OPENSSL_EXPORT int MLDSA87_sign_message_representative( @@ -307,10 +307,10 @@ OPENSSL_EXPORT int MLDSA87_sign_message_representative( const struct MLDSA87_private_key *private_key, const uint8_t msg_rep[MLDSA_MU_BYTES]); -// MLDSA87_verify_message_representative verifies that |signature| constitutes a -// valid signature for the pre-hashed message |msg_rep| using |public_key|. The -// |msg_rep| should be obtained via calls to |MLDSA87_prehash_init|, -// |MLDSA87_prehash_update| and |MLDSA87_prehash_finalize| using |public key| +// MLDSA87_verify_message_representative verifies that `signature` constitutes a +// valid signature for the pre-hashed message `msg_rep` using `public_key`. The +// `msg_rep` should be obtained via calls to `MLDSA87_prehash_init`, +// `MLDSA87_prehash_update` and `MLDSA87_prehash_finalize` using `public_key` // and the same context as when the signature was generated. Returns 1 on // success or 0 on error. OPENSSL_EXPORT int MLDSA87_verify_message_representative( @@ -318,16 +318,16 @@ OPENSSL_EXPORT int MLDSA87_verify_message_representative( const uint8_t *signature, size_t signature_len, const uint8_t msg_rep[MLDSA_MU_BYTES]); -// MLDSA87_marshal_public_key serializes |public_key| to |out| in the standard +// MLDSA87_marshal_public_key serializes `public_key` to `out` in the standard // format for ML-DSA-87 public keys. It returns 1 on success or 0 on // allocation error. OPENSSL_EXPORT int MLDSA87_marshal_public_key( CBB *out, const struct MLDSA87_public_key *public_key); // MLDSA87_parse_public_key parses a public key, in the format generated by -// |MLDSA87_marshal_public_key|, from |in| and writes the result to -// |out_public_key|. It returns 1 on success or 0 on parse error or if -// there are trailing bytes in |in|. +// `MLDSA87_marshal_public_key`, from `in` and writes the result to +// `out_public_key`. It returns 1 on success or 0 on parse error or if +// there are trailing bytes in `in`. OPENSSL_EXPORT int MLDSA87_parse_public_key( struct MLDSA87_public_key *public_key, CBS *in); @@ -371,8 +371,8 @@ struct MLDSA44_prehash { #define MLDSA44_SIGNATURE_BYTES 2420 // MLDSA44_generate_key generates a random public/private key pair, writes the -// encoded public key to |out_encoded_public_key|, writes the seed to -// |out_seed|, and sets |out_private_key| to the private key. Returns 1 on +// encoded public key to `out_encoded_public_key`, writes the seed to +// `out_seed`, and sets `out_private_key` to the private key. Returns 1 on // success and 0 on allocation failure. OPENSSL_EXPORT int MLDSA44_generate_key( uint8_t out_encoded_public_key[MLDSA44_PUBLIC_KEY_BYTES], @@ -380,34 +380,34 @@ OPENSSL_EXPORT int MLDSA44_generate_key( struct MLDSA44_private_key *out_private_key); // MLDSA44_private_key_from_seed regenerates a private key from a seed value -// that was generated by |MLDSA44_generate_key|. Returns 1 on success and 0 on -// allocation failure or if |seed_len| is incorrect. +// that was generated by `MLDSA44_generate_key`. Returns 1 on success and 0 on +// allocation failure or if `seed_len` is incorrect. OPENSSL_EXPORT int MLDSA44_private_key_from_seed( struct MLDSA44_private_key *out_private_key, const uint8_t *seed, size_t seed_len); -// MLDSA44_public_from_private sets |*out_public_key| to the public key that -// corresponds to |private_key|. Returns 1 on success and 0 on failure. +// MLDSA44_public_from_private sets `*out_public_key` to the public key that +// corresponds to `private_key`. Returns 1 on success and 0 on failure. OPENSSL_EXPORT int MLDSA44_public_from_private( struct MLDSA44_public_key *out_public_key, const struct MLDSA44_private_key *private_key); -// MLDSA44_sign generates a signature for the message |msg| of length -// |msg_len| using |private_key| (following the randomized algorithm), and -// writes the encoded signature to |out_encoded_signature|. The |context| +// MLDSA44_sign generates a signature for the message `msg` of length +// `msg_len` using `private_key` (following the randomized algorithm), and +// writes the encoded signature to `out_encoded_signature`. The `context` // argument is also signed over and can be used to include implicit contextual -// information that isn't included in |msg|. The same value of |context| must be -// presented to |MLDSA44_verify| in order for the generated signature to be -// considered valid. |context| and |context_len| may be |NULL| and 0 to use an +// information that isn't included in `msg`. The same value of `context` must be +// presented to `MLDSA44_verify` in order for the generated signature to be +// considered valid. `context` and `context_len` may be `NULL` and 0 to use an // empty context (this is common). Returns 1 on success and 0 on failure. OPENSSL_EXPORT int MLDSA44_sign( uint8_t out_encoded_signature[MLDSA44_SIGNATURE_BYTES], const struct MLDSA44_private_key *private_key, const uint8_t *msg, size_t msg_len, const uint8_t *context, size_t context_len); -// MLDSA44_verify verifies that |signature| constitutes a valid -// signature for the message |msg| of length |msg_len| using |public_key|. The -// value of |context| must equal the value that was passed to |MLDSA44_sign| +// MLDSA44_verify verifies that `signature` constitutes a valid +// signature for the message `msg` of length `msg_len` using `public_key`. The +// value of `context` must equal the value that was passed to `MLDSA44_sign` // when the signature was generated. Returns 1 on success or 0 on error. OPENSSL_EXPORT int MLDSA44_verify(const struct MLDSA44_public_key *public_key, const uint8_t *signature, @@ -415,11 +415,11 @@ OPENSSL_EXPORT int MLDSA44_verify(const struct MLDSA44_public_key *public_key, size_t msg_len, const uint8_t *context, size_t context_len); -// MLDSA44_prehash_init initializes a pre-hashing state using |public_key|. The -// |context| argument can be used to include implicit contextual information -// that isn't included in the message. The same value of |context| must be -// presented to |MLDSA44_verify| in order for the generated signature to be -// considered valid. |context| and |context_len| may be |NULL| and 0 to use an +// MLDSA44_prehash_init initializes a pre-hashing state using `public_key`. The +// `context` argument can be used to include implicit contextual information +// that isn't included in the message. The same value of `context` must be +// presented to `MLDSA44_verify` in order for the generated signature to be +// considered valid. `context` and `context_len` may be `NULL` and 0 to use an // empty context (this is common). Returns 1 on success and 0 on failure (if the // context is too long). OPENSSL_EXPORT int MLDSA44_prehash_init( @@ -427,26 +427,26 @@ OPENSSL_EXPORT int MLDSA44_prehash_init( const struct MLDSA44_public_key *public_key, const uint8_t *context, size_t context_len); -// MLDSA44_prehash_update incorporates the given |msg| of length |msg_len| into +// MLDSA44_prehash_update incorporates the given `msg` of length `msg_len` into // the pre-hashing state. This can be called multiple times on successive chunks -// of the message. This should be called after |MLDSA44_prehash_init| and before -// |MLDSA44_prehash_finalize|. +// of the message. This should be called after `MLDSA44_prehash_init` and before +// `MLDSA44_prehash_finalize`. OPENSSL_EXPORT void MLDSA44_prehash_update(struct MLDSA44_prehash *inout_state, const uint8_t *msg, size_t msg_len); // MLDSA44_prehash_finalize extracts a pre-hashed message representative from // the given pre-hashing state. This should be called after -// |MLDSA44_prehash_init| and |MLDSA44_prehash_update|. The resulting -// |out_msg_rep| should then be passed to |MLDSA44_sign_message_representative| +// `MLDSA44_prehash_init` and `MLDSA44_prehash_update`. The resulting +// `out_msg_rep` should then be passed to `MLDSA44_sign_message_representative` // to obtain a signature. OPENSSL_EXPORT void MLDSA44_prehash_finalize( uint8_t out_msg_rep[MLDSA_MU_BYTES], struct MLDSA44_prehash *inout_state); // MLDSA44_sign_message_representative generates a signature for the pre-hashed -// message |msg_rep| using |private_key| (following the randomized algorithm), -// and writes the encoded signature to |out_encoded_signature|. The |msg_rep| -// should be obtained via calls to |MLDSA44_prehash_init|, -// |MLDSA44_prehash_update| and |MLDSA44_prehash_finalize| using the public key +// message `msg_rep` using `private_key` (following the randomized algorithm), +// and writes the encoded signature to `out_encoded_signature`. The `msg_rep` +// should be obtained via calls to `MLDSA44_prehash_init`, +// `MLDSA44_prehash_update` and `MLDSA44_prehash_finalize` using the public key // from the same key pair, otherwise the signature will not verify. Returns 1 on // success and 0 on failure. OPENSSL_EXPORT int MLDSA44_sign_message_representative( @@ -454,10 +454,10 @@ OPENSSL_EXPORT int MLDSA44_sign_message_representative( const struct MLDSA44_private_key *private_key, const uint8_t msg_rep[MLDSA_MU_BYTES]); -// MLDSA44_verify_message_representative verifies that |signature| constitutes a -// valid signature for the pre-hashed message |msg_rep| using |public_key|. The -// |msg_rep| should be obtained via calls to |MLDSA44_prehash_init|, -// |MLDSA44_prehash_update| and |MLDSA44_prehash_finalize| using |public key| +// MLDSA44_verify_message_representative verifies that `signature` constitutes a +// valid signature for the pre-hashed message `msg_rep` using `public_key`. The +// `msg_rep` should be obtained via calls to `MLDSA44_prehash_init`, +// `MLDSA44_prehash_update` and `MLDSA44_prehash_finalize` using `public_key` // and the same context as when the signature was generated. Returns 1 on // success or 0 on error. OPENSSL_EXPORT int MLDSA44_verify_message_representative( @@ -465,16 +465,16 @@ OPENSSL_EXPORT int MLDSA44_verify_message_representative( const uint8_t *signature, size_t signature_len, const uint8_t msg_rep[MLDSA_MU_BYTES]); -// MLDSA44_marshal_public_key serializes |public_key| to |out| in the standard +// MLDSA44_marshal_public_key serializes `public_key` to `out` in the standard // format for ML-DSA-44 public keys. It returns 1 on success or 0 on // allocation error. OPENSSL_EXPORT int MLDSA44_marshal_public_key( CBB *out, const struct MLDSA44_public_key *public_key); // MLDSA44_parse_public_key parses a public key, in the format generated by -// |MLDSA44_marshal_public_key|, from |in| and writes the result to -// |out_public_key|. It returns 1 on success or 0 on parse error or if -// there are trailing bytes in |in|. +// `MLDSA44_marshal_public_key`, from `in` and writes the result to +// `out_public_key`. It returns 1 on success or 0 on parse error or if +// there are trailing bytes in `in`. OPENSSL_EXPORT int MLDSA44_parse_public_key( struct MLDSA44_public_key *public_key, CBS *in); diff --git a/third_party/boringssl/src/include/openssl/mlkem.h b/third_party/boringssl/src/include/openssl/mlkem.h index 65b4e90f2..89c224754 100644 --- a/third_party/boringssl/src/include/openssl/mlkem.h +++ b/third_party/boringssl/src/include/openssl/mlkem.h @@ -54,8 +54,8 @@ struct MLKEM768_private_key { #define MLKEM_SEED_BYTES 64 // MLKEM768_generate_key generates a random public/private key pair, writes the -// encoded public key to |out_encoded_public_key| and sets |out_private_key| to -// the private key. If |optional_out_seed| is not NULL then the seed used to +// encoded public key to `out_encoded_public_key` and sets `out_private_key` to +// the private key. If `optional_out_seed` is not NULL then the seed used to // generate the private key is written to it. OPENSSL_EXPORT void MLKEM768_generate_key( uint8_t out_encoded_public_key[MLKEM768_PUBLIC_KEY_BYTES], @@ -63,15 +63,15 @@ OPENSSL_EXPORT void MLKEM768_generate_key( struct MLKEM768_private_key *out_private_key); // MLKEM768_private_key_from_seed derives a private key from a seed that was -// generated by |MLKEM768_generate_key|. It fails and returns 0 if |seed_len| is -// incorrect, otherwise it writes |*out_private_key| and returns 1. +// generated by `MLKEM768_generate_key`. It fails and returns 0 if `seed_len` is +// incorrect, otherwise it writes `*out_private_key` and returns 1. OPENSSL_EXPORT int MLKEM768_private_key_from_seed( struct MLKEM768_private_key *out_private_key, const uint8_t *seed, size_t seed_len); -// MLKEM768_public_from_private sets |*out_public_key| to the public key that -// corresponds to |private_key|. (This is faster than parsing the output of -// |MLKEM768_generate_key| if, for some reason, you need to encapsulate to a key +// MLKEM768_public_from_private sets `*out_public_key` to the public key that +// corresponds to `private_key`. (This is faster than parsing the output of +// `MLKEM768_generate_key` if, for some reason, you need to encapsulate to a key // that was just generated.) OPENSSL_EXPORT void MLKEM768_public_from_private( struct MLKEM768_public_key *out_public_key, @@ -83,21 +83,21 @@ OPENSSL_EXPORT void MLKEM768_public_from_private( // MLKEM_SHARED_SECRET_BYTES is the number of bytes in an ML-KEM shared secret. #define MLKEM_SHARED_SECRET_BYTES 32 -// MLKEM768_encap encrypts a random shared secret for |public_key|, writes the -// ciphertext to |out_ciphertext|, and writes the random shared secret to -// |out_shared_secret|. +// MLKEM768_encap encrypts a random shared secret for `public_key`, writes the +// ciphertext to `out_ciphertext`, and writes the random shared secret to +// `out_shared_secret`. OPENSSL_EXPORT void MLKEM768_encap( uint8_t out_ciphertext[MLKEM768_CIPHERTEXT_BYTES], uint8_t out_shared_secret[MLKEM_SHARED_SECRET_BYTES], const struct MLKEM768_public_key *public_key); -// MLKEM768_decap decrypts a shared secret from |ciphertext| using |private_key| -// and writes it to |out_shared_secret|. If |ciphertext_len| is incorrect it -// returns 0, otherwise it returns 1. If |ciphertext| is invalid (but of the -// correct length), |out_shared_secret| is filled with a key that will always be -// the same for the same |ciphertext| and |private_key|, but which appears to be -// random unless one has access to |private_key|. These alternatives occur in -// constant time. Any subsequent symmetric encryption using |out_shared_secret| +// MLKEM768_decap decrypts a shared secret from `ciphertext` using `private_key` +// and writes it to `out_shared_secret`. If `ciphertext_len` is incorrect it +// returns 0, otherwise it returns 1. If `ciphertext` is invalid (but of the +// correct length), `out_shared_secret` is filled with a key that will always be +// the same for the same `ciphertext` and `private_key`, but which appears to be +// random unless one has access to `private_key`. These alternatives occur in +// constant time. Any subsequent symmetric encryption using `out_shared_secret` // must use an authenticated encryption scheme in order to discover the // decapsulation failure. OPENSSL_EXPORT int MLKEM768_decap( @@ -108,16 +108,16 @@ OPENSSL_EXPORT int MLKEM768_decap( // Serialisation of keys. -// MLKEM768_marshal_public_key serializes |public_key| to |out| in the standard +// MLKEM768_marshal_public_key serializes `public_key` to `out` in the standard // format for ML-KEM-768 public keys. It returns one on success or zero on // allocation error. OPENSSL_EXPORT int MLKEM768_marshal_public_key( CBB *out, const struct MLKEM768_public_key *public_key); // MLKEM768_parse_public_key parses a public key, in the format generated by -// |MLKEM768_marshal_public_key|, from |in| and writes the result to -// |out_public_key|. It returns one on success or zero on parse error or if -// there are trailing bytes in |in|. +// `MLKEM768_marshal_public_key`, from `in` and writes the result to +// `out_public_key`. It returns one on success or zero on parse error or if +// there are trailing bytes in `in`. OPENSSL_EXPORT int MLKEM768_parse_public_key( struct MLKEM768_public_key *out_public_key, CBS *in); @@ -150,8 +150,8 @@ struct MLKEM1024_private_key { #define MLKEM1024_PUBLIC_KEY_BYTES 1568 // MLKEM1024_generate_key generates a random public/private key pair, writes the -// encoded public key to |out_encoded_public_key| and sets |out_private_key| to -// the private key. If |optional_out_seed| is not NULL then the seed used to +// encoded public key to `out_encoded_public_key` and sets `out_private_key` to +// the private key. If `optional_out_seed` is not NULL then the seed used to // generate the private key is written to it. OPENSSL_EXPORT void MLKEM1024_generate_key( uint8_t out_encoded_public_key[MLKEM1024_PUBLIC_KEY_BYTES], @@ -159,15 +159,15 @@ OPENSSL_EXPORT void MLKEM1024_generate_key( struct MLKEM1024_private_key *out_private_key); // MLKEM1024_private_key_from_seed derives a private key from a seed that was -// generated by |MLKEM1024_generate_key|. It fails and returns 0 if |seed_len| -// is incorrect, otherwise it writes |*out_private_key| and returns 1. +// generated by `MLKEM1024_generate_key`. It fails and returns 0 if `seed_len` +// is incorrect, otherwise it writes `*out_private_key` and returns 1. OPENSSL_EXPORT int MLKEM1024_private_key_from_seed( struct MLKEM1024_private_key *out_private_key, const uint8_t *seed, size_t seed_len); -// MLKEM1024_public_from_private sets |*out_public_key| to the public key that -// corresponds to |private_key|. (This is faster than parsing the output of -// |MLKEM1024_generate_key| if, for some reason, you need to encapsulate to a +// MLKEM1024_public_from_private sets `*out_public_key` to the public key that +// corresponds to `private_key`. (This is faster than parsing the output of +// `MLKEM1024_generate_key` if, for some reason, you need to encapsulate to a // key that was just generated.) OPENSSL_EXPORT void MLKEM1024_public_from_private( struct MLKEM1024_public_key *out_public_key, @@ -176,22 +176,22 @@ OPENSSL_EXPORT void MLKEM1024_public_from_private( // MLKEM1024_CIPHERTEXT_BYTES is number of bytes in the ML-KEM-1024 ciphertext. #define MLKEM1024_CIPHERTEXT_BYTES 1568 -// MLKEM1024_encap encrypts a random shared secret for |public_key|, writes the -// ciphertext to |out_ciphertext|, and writes the random shared secret to -// |out_shared_secret|. +// MLKEM1024_encap encrypts a random shared secret for `public_key`, writes the +// ciphertext to `out_ciphertext`, and writes the random shared secret to +// `out_shared_secret`. OPENSSL_EXPORT void MLKEM1024_encap( uint8_t out_ciphertext[MLKEM1024_CIPHERTEXT_BYTES], uint8_t out_shared_secret[MLKEM_SHARED_SECRET_BYTES], const struct MLKEM1024_public_key *public_key); -// MLKEM1024_decap decrypts a shared secret from |ciphertext| using -// |private_key| and writes it to |out_shared_secret|. If |ciphertext_len| is -// incorrect it returns 0, otherwise it returns 1. If |ciphertext| is invalid -// (but of the correct length), |out_shared_secret| is filled with a key that -// will always be the same for the same |ciphertext| and |private_key|, but -// which appears to be random unless one has access to |private_key|. These +// MLKEM1024_decap decrypts a shared secret from `ciphertext` using +// `private_key` and writes it to `out_shared_secret`. If `ciphertext_len` is +// incorrect it returns 0, otherwise it returns 1. If `ciphertext` is invalid +// (but of the correct length), `out_shared_secret` is filled with a key that +// will always be the same for the same `ciphertext` and `private_key`, but +// which appears to be random unless one has access to `private_key`. These // alternatives occur in constant time. Any subsequent symmetric encryption -// using |out_shared_secret| must use an authenticated encryption scheme in +// using `out_shared_secret` must use an authenticated encryption scheme in // order to discover the decapsulation failure. OPENSSL_EXPORT int MLKEM1024_decap( uint8_t out_shared_secret[MLKEM_SHARED_SECRET_BYTES], @@ -201,16 +201,16 @@ OPENSSL_EXPORT int MLKEM1024_decap( // Serialisation of ML-KEM-1024 keys. -// MLKEM1024_marshal_public_key serializes |public_key| to |out| in the standard +// MLKEM1024_marshal_public_key serializes `public_key` to `out` in the standard // format for ML-KEM-1024 public keys. It returns one on success or zero on // allocation error. OPENSSL_EXPORT int MLKEM1024_marshal_public_key( CBB *out, const struct MLKEM1024_public_key *public_key); // MLKEM1024_parse_public_key parses a public key, in the format generated by -// |MLKEM1024_marshal_public_key|, from |in| and writes the result to -// |out_public_key|. It returns one on success or zero on parse error or if -// there are trailing bytes in |in|. +// `MLKEM1024_marshal_public_key`, from `in` and writes the result to +// `out_public_key`. It returns one on success or zero on parse error or if +// there are trailing bytes in `in`. OPENSSL_EXPORT int MLKEM1024_parse_public_key( struct MLKEM1024_public_key *out_public_key, CBS *in); diff --git a/third_party/boringssl/src/include/openssl/nid.h b/third_party/boringssl/src/include/openssl/nid.h index 6b3bb4506..38e5dc86d 100644 --- a/third_party/boringssl/src/include/openssl/nid.h +++ b/third_party/boringssl/src/include/openssl/nid.h @@ -5511,6 +5511,47 @@ extern "C" { #define SN_X_Wing "X-Wing" #define NID_X_Wing 972 +#define SN_SLH_DSA_SHA2_128s "id-slh-dsa-sha2-128s" +#define LN_SLH_DSA_SHA2_128s "SLH-DSA-SHA2-128s" +#define NID_SLH_DSA_SHA2_128s 973 +#define OBJ_SLH_DSA_SHA2_128s 2L, 16L, 840L, 1L, 101L, 3L, 4L, 3L, 20L +#define OBJ_ENC_SLH_DSA_SHA2_128s \ + 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x03, 0x14 + +#define SN_SLH_DSA_SHAKE_256f "id-slh-dsa-shake-256f" +#define LN_SLH_DSA_SHAKE_256f "SLH-DSA-SHAKE-256f" +#define NID_SLH_DSA_SHAKE_256f 974 +#define OBJ_SLH_DSA_SHAKE_256f 2L, 16L, 840L, 1L, 101L, 3L, 4L, 3L, 31L +#define OBJ_ENC_SLH_DSA_SHAKE_256f \ + 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x03, 0x1f + +#define SN_SLH_DSA_SHA2_128s_WITH_SHA256 "id-hash-slh-dsa-sha2-128s-with-sha256" +#define LN_SLH_DSA_SHA2_128s_WITH_SHA256 "SLH-DSA-SHA2-128s-WITH-SHA256" +#define NID_SLH_DSA_SHA2_128s_WITH_SHA256 975 +#define OBJ_SLH_DSA_SHA2_128s_WITH_SHA256 \ + 2L, 16L, 840L, 1L, 101L, 3L, 4L, 3L, 35L +#define OBJ_ENC_SLH_DSA_SHA2_128s_WITH_SHA256 \ + 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x03, 0x23 + +#define SN_alg_mtcProof "alg-mtcProof" +#define NID_alg_mtcProof 976 +#define OBJ_alg_mtcProof 1L, 3L, 6L, 1L, 5L, 5L, 7L, 6L, 67L +#define OBJ_ENC_alg_mtcProof 0x2b, 0x06, 0x01, 0x05, 0x05, 0x07, 0x06, 0x43 + +#define SN_rdna_trustAnchorID "rdna-trustAnchorID" +#define NID_rdna_trustAnchorID 977 +#define OBJ_rdna_trustAnchorID 1L, 3L, 6L, 1L, 5L, 5L, 7L, 25L, 3L +#define OBJ_ENC_rdna_trustAnchorID \ + 0x2b, 0x06, 0x01, 0x05, 0x05, 0x07, 0x19, 0x03 + +#define SN_pe_mtcCertificationAuthority_SHA256 \ + "pe-mtcCertificationAuthority-SHA256" +#define NID_pe_mtcCertificationAuthority_SHA256 978 +#define OBJ_pe_mtcCertificationAuthority_SHA256 \ + 1L, 3L, 6L, 1L, 5L, 5L, 7L, 1L, 38L +#define OBJ_ENC_pe_mtcCertificationAuthority_SHA256 \ + 0x2b, 0x06, 0x01, 0x05, 0x05, 0x07, 0x01, 0x26 + #if defined(__cplusplus) } /* extern C */ diff --git a/third_party/boringssl/src/include/openssl/obj.h b/third_party/boringssl/src/include/openssl/obj.h index b3fabca21..ba4f8032f 100644 --- a/third_party/boringssl/src/include/openssl/obj.h +++ b/third_party/boringssl/src/include/openssl/obj.h @@ -42,46 +42,46 @@ extern "C" { // Basic operations. -// OBJ_dup returns a duplicate copy of |obj| or NULL on allocation failure. The -// caller must call |ASN1_OBJECT_free| on the result to release it. +// OBJ_dup returns a duplicate copy of `obj` or NULL on allocation failure. The +// caller must call `ASN1_OBJECT_free` on the result to release it. OPENSSL_EXPORT ASN1_OBJECT *OBJ_dup(const ASN1_OBJECT *obj); -// OBJ_cmp returns a value less than, equal to or greater than zero if |a| is -// less than, equal to or greater than |b|, respectively. +// OBJ_cmp returns a value less than, equal to or greater than zero if `a` is +// less than, equal to or greater than `b`, respectively. OPENSSL_EXPORT int OBJ_cmp(const ASN1_OBJECT *a, const ASN1_OBJECT *b); -// OBJ_get0_data returns a pointer to the DER representation of |obj|. This is +// OBJ_get0_data returns a pointer to the DER representation of `obj`. This is // the contents of the DER-encoded identifier, not including the tag and length. -// If |obj| does not have an associated object identifier (i.e. it is a nid-only +// If `obj` does not have an associated object identifier (i.e. it is a nid-only // value), this value is the empty string. OPENSSL_EXPORT const uint8_t *OBJ_get0_data(const ASN1_OBJECT *obj); -// OBJ_length returns the length of the DER representation of |obj|. This is the +// OBJ_length returns the length of the DER representation of `obj`. This is the // contents of the DER-encoded identifier, not including the tag and length. If -// |obj| does not have an associated object identifier (i.e. it is a nid-only +// `obj` does not have an associated object identifier (i.e. it is a nid-only // value), this value is the empty string. OPENSSL_EXPORT size_t OBJ_length(const ASN1_OBJECT *obj); // Looking up nids. -// OBJ_obj2nid returns the nid corresponding to |obj|, or |NID_undef| if no +// OBJ_obj2nid returns the nid corresponding to `obj`, or `NID_undef` if no // such object is known. OPENSSL_EXPORT int OBJ_obj2nid(const ASN1_OBJECT *obj); -// OBJ_cbs2nid returns the nid corresponding to the DER data in |cbs|, or -// |NID_undef| if no such object is known. +// OBJ_cbs2nid returns the nid corresponding to the DER data in `cbs`, or +// `NID_undef` if no such object is known. OPENSSL_EXPORT int OBJ_cbs2nid(const CBS *cbs); -// OBJ_sn2nid returns the nid corresponding to |short_name|, or |NID_undef| if +// OBJ_sn2nid returns the nid corresponding to `short_name`, or `NID_undef` if // no such short name is known. OPENSSL_EXPORT int OBJ_sn2nid(const char *short_name); -// OBJ_ln2nid returns the nid corresponding to |long_name|, or |NID_undef| if +// OBJ_ln2nid returns the nid corresponding to `long_name`, or `NID_undef` if // no such long name is known. OPENSSL_EXPORT int OBJ_ln2nid(const char *long_name); -// OBJ_txt2nid returns the nid corresponding to |s|, which may be a short name, +// OBJ_txt2nid returns the nid corresponding to `s`, which may be a short name, // long name, or an ASCII string containing a dotted sequence of numbers. It // returns the nid or NID_undef if unknown. OPENSSL_EXPORT int OBJ_txt2nid(const char *s); @@ -89,54 +89,54 @@ OPENSSL_EXPORT int OBJ_txt2nid(const char *s); // Getting information about nids. -// OBJ_nid2obj returns the |ASN1_OBJECT| corresponding to |nid|, or NULL if -// |nid| is unknown. +// OBJ_nid2obj returns the `ASN1_OBJECT` corresponding to `nid`, or NULL if +// `nid` is unknown. // // Although the output is not const, this function returns a static, immutable -// |ASN1_OBJECT|. It is not necessary to release the object with -// |ASN1_OBJECT_free|. +// `ASN1_OBJECT`. It is not necessary to release the object with +// `ASN1_OBJECT_free`. // -// However, functions like |X509_ALGOR_set0| expect to take ownership of a -// possibly dynamically-allocated |ASN1_OBJECT|. |ASN1_OBJECT_free| is a no-op -// for static |ASN1_OBJECT|s, so |OBJ_nid2obj| is compatible with such +// However, functions like `X509_ALGOR_set0` expect to take ownership of a +// possibly dynamically-allocated `ASN1_OBJECT`. `ASN1_OBJECT_free` is a no-op +// for static `ASN1_OBJECT`s, so `OBJ_nid2obj` is compatible with such // functions. // // Callers are encouraged to store the result of this function in a const -// pointer. However, if using functions like |X509_ALGOR_set0|, callers may use +// pointer. However, if using functions like `X509_ALGOR_set0`, callers may use // a non-const pointer and manage ownership. OPENSSL_EXPORT ASN1_OBJECT *OBJ_nid2obj(int nid); -// OBJ_get_undef returns the object for |NID_undef|. Prefer this function over -// |OBJ_nid2obj| to avoid pulling in the full OID table. +// OBJ_get_undef returns the object for `NID_undef`. Prefer this function over +// `OBJ_nid2obj` to avoid pulling in the full OID table. OPENSSL_EXPORT const ASN1_OBJECT *OBJ_get_undef(void); -// OBJ_nid2sn returns the short name for |nid|, or NULL if |nid| is unknown. +// OBJ_nid2sn returns the short name for `nid`, or NULL if `nid` is unknown. OPENSSL_EXPORT const char *OBJ_nid2sn(int nid); -// OBJ_nid2ln returns the long name for |nid|, or NULL if |nid| is unknown. +// OBJ_nid2ln returns the long name for `nid`, or NULL if `nid` is unknown. OPENSSL_EXPORT const char *OBJ_nid2ln(int nid); -// OBJ_nid2cbb writes |nid| as an ASN.1 OBJECT IDENTIFIER to |out|. It returns +// OBJ_nid2cbb writes `nid` as an ASN.1 OBJECT IDENTIFIER to `out`. It returns // one on success or zero otherwise. OPENSSL_EXPORT int OBJ_nid2cbb(CBB *out, int nid); // Dealing with textual representations of object identifiers. -// OBJ_txt2obj returns an ASN1_OBJECT for the textual representation in |s|. -// If |dont_search_names| is zero, then |s| will be matched against the long -// and short names of a known objects to find a match. Otherwise |s| must +// OBJ_txt2obj returns an ASN1_OBJECT for the textual representation in `s`. +// If `dont_search_names` is zero, then `s` will be matched against the long +// and short names of a known objects to find a match. Otherwise `s` must // contain an ASCII string with a dotted sequence of numbers. The resulting // object need not be previously known. It returns a freshly allocated -// |ASN1_OBJECT| or NULL on error. +// `ASN1_OBJECT` or NULL on error. OPENSSL_EXPORT ASN1_OBJECT *OBJ_txt2obj(const char *s, int dont_search_names); -// OBJ_obj2txt converts |obj| to a textual representation. If -// |always_return_oid| is zero then |obj| will be matched against known objects +// OBJ_obj2txt converts `obj` to a textual representation. If +// `always_return_oid` is zero then `obj` will be matched against known objects // and the long (preferably) or short name will be used if found. Otherwise -// |obj| will be converted into a dotted sequence of integers. If |out| is not -// NULL, then at most |out_len| bytes of the textual form will be written -// there. If |out_len| is at least one, then string written to |out| will +// `obj` will be converted into a dotted sequence of integers. If `out` is not +// NULL, then at most `out_len` bytes of the textual form will be written +// there. If `out_len` is at least one, then string written to `out` will // always be NUL terminated. It returns the number of characters that could // have been written, not including the final NUL, or -1 on error. OPENSSL_EXPORT int OBJ_obj2txt(char *out, int out_len, const ASN1_OBJECT *obj, @@ -153,7 +153,7 @@ OPENSSL_EXPORT int OBJ_obj2txt(char *out, int out_len, const ASN1_OBJECT *obj, // address space add conflicting values, only one registration will take effect. // Avoid this function if possible. Instead, callers can process OIDs unknown to // BoringSSL by acting on the byte representation directly. See -// |ASN1_OBJECT_create|, |OBJ_get0_data|, and |OBJ_length|. +// `ASN1_OBJECT_create`, `OBJ_get0_data`, and `OBJ_length`. OPENSSL_EXPORT int OBJ_create(const char *oid, const char *short_name, const char *long_name); @@ -165,20 +165,20 @@ OPENSSL_EXPORT int OBJ_create(const char *oid, const char *short_name, // and public-key algorithms and the NIDs that specify their combination. // // Sometimes the combination NID leaves the digest unspecified (e.g. -// rsassaPss). In these cases, the digest NID is |NID_undef|. +// rsassaPss). In these cases, the digest NID is `NID_undef`. // OBJ_find_sigid_algs finds the digest and public-key NIDs that correspond to -// the signing algorithm |sign_nid|. If successful, it sets |*out_digest_nid| -// and |*out_pkey_nid| and returns one. Otherwise it returns zero. Any of -// |out_digest_nid| or |out_pkey_nid| can be NULL if the caller doesn't need +// the signing algorithm `sign_nid`. If successful, it sets `*out_digest_nid` +// and `*out_pkey_nid` and returns one. Otherwise it returns zero. Any of +// `out_digest_nid` or `out_pkey_nid` can be NULL if the caller doesn't need // that output value. OPENSSL_EXPORT int OBJ_find_sigid_algs(int sign_nid, int *out_digest_nid, int *out_pkey_nid); // OBJ_find_sigid_by_algs finds the signature NID that corresponds to the -// combination of |digest_nid| and |pkey_nid|. If success, it sets -// |*out_sign_nid| and returns one. Otherwise it returns zero. The -// |out_sign_nid| argument can be NULL if the caller only wishes to learn +// combination of `digest_nid` and `pkey_nid`. If success, it sets +// `*out_sign_nid` and returns one. Otherwise it returns zero. The +// `out_sign_nid` argument can be NULL if the caller only wishes to learn // whether the combination is valid. OPENSSL_EXPORT int OBJ_find_sigid_by_algs(int *out_sign_nid, int digest_nid, int pkey_nid); @@ -196,17 +196,17 @@ typedef struct obj_name_st { #define OBJ_NAME_TYPE_MD_METH 1 #define OBJ_NAME_TYPE_CIPHER_METH 2 -// OBJ_NAME_do_all_sorted calls |callback| zero or more times, each time with -// the name of a different primitive. If |type| is |OBJ_NAME_TYPE_MD_METH| then -// the primitives will be hash functions, alternatively if |type| is -// |OBJ_NAME_TYPE_CIPHER_METH| then the primitives will be ciphers or cipher +// OBJ_NAME_do_all_sorted calls `callback` zero or more times, each time with +// the name of a different primitive. If `type` is `OBJ_NAME_TYPE_MD_METH` then +// the primitives will be hash functions, alternatively if `type` is +// `OBJ_NAME_TYPE_CIPHER_METH` then the primitives will be ciphers or cipher // modes. // // This function is ill-specified and should never be used. OPENSSL_EXPORT void OBJ_NAME_do_all_sorted( int type, void (*callback)(const OBJ_NAME *, void *arg), void *arg); -// OBJ_NAME_do_all calls |OBJ_NAME_do_all_sorted|. +// OBJ_NAME_do_all calls `OBJ_NAME_do_all_sorted`. OPENSSL_EXPORT void OBJ_NAME_do_all(int type, void (*callback)(const OBJ_NAME *, void *arg), void *arg); diff --git a/third_party/boringssl/src/include/openssl/opensslconf.h b/third_party/boringssl/src/include/openssl/opensslconf.h index a41e49be5..edd792cc2 100644 --- a/third_party/boringssl/src/include/openssl/opensslconf.h +++ b/third_party/boringssl/src/include/openssl/opensslconf.h @@ -98,8 +98,8 @@ // https://stackoverflow.com/questions/216510/extern-inline // // So in C, always use static inline, whereas in C++, use static inline -// only if |BORINGSSL_ALWAYS_USE_STATIC_INLINE| is defined (which may be useful -// for some FFI integrations in conjunction with |BORINGSSL_PREFIX|, as static +// only if `BORINGSSL_ALWAYS_USE_STATIC_INLINE` is defined (which may be useful +// for some FFI integrations in conjunction with `BORINGSSL_PREFIX`, as static // inline functions are local to the compilation unit and need no prefix). #if !defined(__cplusplus) && !defined(BORINGSSL_ALWAYS_USE_STATIC_INLINE) #define BORINGSSL_ALWAYS_USE_STATIC_INLINE diff --git a/third_party/boringssl/src/include/openssl/params.h b/third_party/boringssl/src/include/openssl/params.h index 95e3f4ebf..21f22e7a4 100644 --- a/third_party/boringssl/src/include/openssl/params.h +++ b/third_party/boringssl/src/include/openssl/params.h @@ -32,7 +32,7 @@ struct ossl_param_st { size_t return_size; }; -// OSSL_PARAM_END is a terminating element in an array of |OSSL_PARAM|s. +// OSSL_PARAM_END is a terminating element in an array of `OSSL_PARAM`s. #define OSSL_PARAM_END {NULL, 0, NULL, 0, 0} #if defined(__cplusplus) diff --git a/third_party/boringssl/src/include/openssl/pem.h b/third_party/boringssl/src/include/openssl/pem.h index 0e4916258..cd36899b0 100644 --- a/third_party/boringssl/src/include/openssl/pem.h +++ b/third_party/boringssl/src/include/openssl/pem.h @@ -25,7 +25,7 @@ #include // For compatibility with open-iscsi, which assumes that it can get -// |OPENSSL_malloc| from pem.h or err.h +// `OPENSSL_malloc` from pem.h or err.h #include #if defined(__cplusplus) @@ -36,16 +36,501 @@ extern "C" { // PEM. // // This library contains functions for reading and writing data encoded in PEM -// format. This format originated in Privacy-Enhanced Mail (RFC 1421). +// format. This format originated in Privacy-Enhanced Mail (RFC 1421). PEM +// consists of a series of PEM blocks, which are line-wrapped, base64-encoded +// data, wrapped in BEGIN and END lines, for example: // -// As an exception to RFC 1421, generally PEM data is limited to 1 GiB by this -// library. This limit should not affect anyone in practice. +// -----BEGIN PUBLIC KEY----- +// MCowBQYDK2VwAyEAGb9ECWmEzf6FQbrBZ9w7lshQhqowtrbLDFw4rXAxZuE= +// -----END PUBLIC KEY----- // -// TODO(crbug.com/42290574): Finish documenting this header. +// The BEGIN and END markers specify the type of data encoded. Multiple PEM +// blocks can be concatenated together. For example, it is common to represent a +// certificate chain as a series of PEM blocks of type CERTIFICATE. Owing to its +// email roots, PEM blocks can also be embedded in non-PEM data. The parsers in +// this library will generally skip over any non-PEM data, as well as any PEM +// blocks that are not of the expected type. +// +// PEM blocks can be encrypted with a password, specified in RFC 1423. PEM +// encryption is vulnerable to padding oracle attacks and should not be used as +// a load-bearing security measure. It is implemented for interoperability with +// legacy systems only. +// +// Callers should only use PEM for compatibility with legacy systems. PEM use +// should be limited to the components that directly interoperate with those +// systems, converting to and from more modern formats. PEM adds overhead to fit +// in email's 7-bit ASCII limitations, a constraint that is not relevant to most +// applications. + + +// API conventions. + +// pem_password_cb is an application-supplied callback to supply a PEM password. +// On success, the callback should write the password into `out`, which has room +// for at most `max_out` bytes. On error, including if `max_out` is too small, +// it should return -1. `enc` is one if the password will be used for encrypting +// and zero if it is used for decrypting. `userdata` is the application-supplied +// parameter of the same name to the PEM function. +typedef int pem_password_cb(char *out, int max_out, int enc, void *userdata); + +// PEM_def_callback is the default implementation of `pem_password_cb`. It +// interprets `userdata` as a NUL-terminated C string and outputs it according +// to `pem_password_cb`. If `userdata` is NULL, it returns -1. +// +// This differs from OpenSSL, which interactively prompts for a password by +// default. +OPENSSL_EXPORT int PEM_def_callback(char *buf, int size, int enc, + void *userdata); + +// The following sample functions document the calling conventions used by this +// library. + +#if 0 // Sample functions + +// PEM_read_bio_SAMPLE reads a PEM block from `bio`, skipping non-PEM data and +// unexpected block types. It then decodes the resulting PEM block and returns a +// newly-allocated `SAMPLE` object containing the parsed structure. If `out` is +// non-NULL, it additionally frees the previous value at `*out` and updates +// `*out` to the result. +// +// On decode or allocation error, or if EOF is reached before a matching PEM +// block is found, it returns NULL. In the error case, it will add +// `PEM_R_NO_START_LINE` to the error queue. Callers can check the error queue +// to distinguish these cases. +// +// If the PEM block is encrypted, `cb` will be called to look up the password. +// See `pem_password_cb` for details. If `cb` is NULL, `PEM_def_callback` is +// used and `userdata` should be a NUL-terminated C string containing the +// password. Set both `cb` and `userdata` to NULL to only handle plaintext +// blocks. +SAMPLE *PEM_read_bio_SAMPLE(BIO *bio, SAMPLE **out, pem_password_cb *cb, + void *userdata); + +// PEM_write_bio_SAMPLE encodes `in` as a PEM block and writes it to `bio`. It +// returns one on success and zero on error. +// +// If `enc` is non-NULL, the PEM block is encrypted with the specified cipher +// and a password. If `pass` is non-NULL, `pass_len` bytes from `pass` is +// used as the password. Otherwise, `cb` is called. If `cb` is NULL, +// `PEM_def_callback` is used. PEM encryption is vulnerable to padding oracle +// attacks and should not be used. +// +// Some functions of this convention do not support encryption. In this case, +// the encryption-related parameters will be omitted. +// +// On error, the state of `bio` is undefined. It is possible a prefix of a PEM +// block was left in the `bio`. +int PEM_write_bio_SAMPLE(BIO *bio, const SAMPLE *in, const EVP_CIPHER *enc, + const uint8_t *pass, int pass_len, pem_password_cb *cb, + void *userdata); + +#endif // Sample functions + + +// Reading and writing X.509 structures as PEM. + +// PEM_read_bio_X509 reads a PEM block of type "CERTIFICATE" (RFC 7468) or "X509 +// CERTIFICATE", as described in `PEM_read_bio_SAMPLE`. +OPENSSL_EXPORT X509 *PEM_read_bio_X509(BIO *bio, X509 **out, + pem_password_cb *cb, void *userdata); + +// PEM_write_bio_X509 writes a PEM block of type "CERTIFICATE" (RFC 7468), as +// described in `PEM_write_bio_SAMPLE`. +OPENSSL_EXPORT int PEM_write_bio_X509(BIO *bio, const X509 *in); + +// PEM_read_bio_X509_AUX reads a PEM block of type "CERTIFICATE" (RFC 7468), +// "X509 CERTIFICATE", or "TRUSTED CERTIFICATE", as described in +// `PEM_read_bio_SAMPLE`. +// +// WARNING: This function parses auxiliary properties as in `d2i_X509_AUX`. +// Passing untrusted input to this function allows an attacker to influence +// those properties. See `d2i_X509_AUX` for details. +OPENSSL_EXPORT X509 *PEM_read_bio_X509_AUX(BIO *bio, X509 **out, + pem_password_cb *cb, void *userdata); + +// PEM_write_bio_X509_AUX writes a PEM block of type "TRUSTED CERTIFICATE", as +// described in `PEM_write_bio_SAMPLE`. +// +// WARNING: This function writes a custom OpenSSL-specific format that includes +// auxiliary properties. See `i2d_X509_AUX`. +OPENSSL_EXPORT int PEM_write_bio_X509_AUX(BIO *bio, const X509 *in); + +// PEM_read_bio_X509_CRL reads a PEM block of type "X509 CRL" (RFC 7468), as +// described in `PEM_read_bio_SAMPLE`. +OPENSSL_EXPORT X509_CRL *PEM_read_bio_X509_CRL(BIO *bio, X509_CRL **out, + pem_password_cb *cb, + void *userdata); + +// PEM_write_bio_X509_CRL writes a PEM block of type "X509 CRL" (RFC 7468), as +// described in `PEM_write_bio_SAMPLE`. +OPENSSL_EXPORT int PEM_write_bio_X509_CRL(BIO *bio, const X509_CRL *in); + +// PEM_read_bio_X509_REQ reads a PEM block of type "CERTIFICATE REQUEST" +// (RFC 7468) or "NEW CERTIFICATE REQUEST", as described in +// `PEM_read_bio_SAMPLE`. +OPENSSL_EXPORT X509_REQ *PEM_read_bio_X509_REQ(BIO *bio, X509_REQ **out, + pem_password_cb *cb, + void *userdata); + +// PEM_write_bio_X509_REQ writes a PEM block of type "CERTIFICATE REQUEST" +// (RFC 7468), as described in `PEM_write_bio_SAMPLE`. +OPENSSL_EXPORT int PEM_write_bio_X509_REQ(BIO *bio, const X509_REQ *in); + +// PEM_write_bio_X509_REQ_NEW writes a PEM block of type "NEW CERTIFICATE +// REQUEST", as described in `PEM_write_bio_SAMPLE`. Prefer to use +// `PEM_write_bio_X509_REQ`. "NEW CERTIFICATE REQUEST" is the older type. +OPENSSL_EXPORT int PEM_write_bio_X509_REQ_NEW(BIO *bio, const X509_REQ *in); + +// PEM_read_bio_PKCS7 reads a PEM block of type "PKCS7" (RFC 7468) or "PKCS #7 +// SIGNED DATA", as described in `PEM_read_bio_SAMPLE`. +// +// This function also accepts type "CERTIFICATE" but decodes the contents as a +// PKCS #7 structure rather than a certificate. This is a historical workaround +// for an old CA bug. +OPENSSL_EXPORT PKCS7 *PEM_read_bio_PKCS7(BIO *bio, PKCS7 **out, + pem_password_cb *cb, void *userdata); + +// PEM_write_bio_PKCS7 writes a PEM block of type "PKCS7" (RFC 7468), as +// described in `PEM_write_bio_SAMPLE`. +OPENSSL_EXPORT int PEM_write_bio_PKCS7(BIO *bio, const PKCS7 *in); + +// PEM_X509_INFO_read_bio reads PEM blocks from `bio` and decodes any +// certificates, CRLs, and private keys found. It returns a +// `STACK_OF(X509_INFO)` structure containing the results, or NULL on error. +// +// If `sk` is NULL, the result on success will be a newly-allocated +// `STACK_OF(X509_INFO)` structure which should be released with +// `sk_X509_INFO_pop_free` and `X509_INFO_free` when done. +// +// If `sk` is non-NULL, it appends the results to `sk` instead and returns `sk` +// on success. In this case, the caller retains ownership of `sk` in both +// success and failure. +// +// This function will decrypt any encrypted certificates in `bio`, using `cb`, +// but it will not decrypt encrypted private keys. Encrypted private keys are +// instead represented as placeholder `X509_INFO` objects with an empty `x_pkey` +// field. This allows this function to be used with inputs with unencrypted +// certificates, but encrypted passwords, without knowing the password. However, +// it also means that this function cannot be used to decrypt the private key +// when the password is known. +// +// WARNING: If the input contains "TRUSTED CERTIFICATE" PEM blocks, this +// function parses auxiliary properties as in `d2i_X509_AUX`. Passing untrusted +// input to this function allows an attacker to influence those properties. See +// `d2i_X509_AUX` for details. +OPENSSL_EXPORT STACK_OF(X509_INFO) *PEM_X509_INFO_read_bio( + BIO *bio, STACK_OF(X509_INFO) *sk, pem_password_cb *cb, void *userdata); +// Reading and writing keys as PEM. +// +// There are multiple PEM formats for public and private keys: +// +// Public keys are generally encoded with type "PUBLIC KEY" (RFC 7468), which +// encodes a SubjectPublicKeyInfo structure (RFC 5280). +// +// Private keys may be encoded with type "PRIVATE KEY" or "ENCRYPTED PRIVATE +// KEY" (RFC 7468), which encode a PrivateKeyInfo or EncryptedPrivateKeyInfo +// (RFC 5208) structure. EncryptedPrivateKeyInfo is, itself, a mechanism for +// encrypting private keys with a password, so private keys may be encrypted +// with PEM encryption, PKCS #8 encryption, or both. +// +// There are also older algorithm-specific PEM types for public and private +// keys, including "RSA PUBLIC KEY", "RSA PRIVATE KEY", "EC PRIVATE KEY", and +// "DSA PRIVATE KEY". Some functions in this library will read or write them. +// +// If unsure, use the "PUBLIC KEY" and "PRIVATE KEY" formats. + +// PEM_read_bio_PrivateKey reads a PEM block containing a private key, as +// described in `PEM_read_bio_SAMPLE`. It handles generic PKCS #8 blocks of type +// "PRIVATE KEY" and "ENCRYPTED PRIVATE KEY", as well as key-specific formats +// like "RSA PRIVATE KEY", "EC PRIVATE KEY", and "DSA PRIVATE KEY". +// +// `cb` and `userdata` are used to look up the password for both PEM-level +// encryption as well as PKCS #8 encryption, in the case of "ENCRYPTED PRIVATE +// KEY" blocks. +OPENSSL_EXPORT EVP_PKEY *PEM_read_bio_PrivateKey(BIO *bio, EVP_PKEY **out, + pem_password_cb *cb, + void *userdata); + +// PEM_write_bio_PrivateKey writes `in` to `bio` in PKCS#8 format, as described +// in `PEM_write_bio_SAMPLE`. However, it interprets encryption parameters +// differently: +// +// - If not encrypting (`enc` is NULL), it writes the key as a PEM block of type +// "PRIVATE KEY". +// +// - If encrypted (`enc` is not NULL), it encrypts the key in an +// EncryptedPrivateKeyInfo using PBES2 (see `PKCS8_encrypt`) and writes the +// result as a PEM block of type "ENCRYPTED PRIVATE KEY". +OPENSSL_EXPORT int PEM_write_bio_PrivateKey(BIO *bio, const EVP_PKEY *in, + const EVP_CIPHER *enc, + const uint8_t *pass, int pass_len, + pem_password_cb *cb, + void *userdata); + +// PEM_read_bio_PUBKEY reads a PEM block of type "PUBLIC KEY", as described in +// `PEM_read_bio_SAMPLE`. +OPENSSL_EXPORT EVP_PKEY *PEM_read_bio_PUBKEY(BIO *bio, EVP_PKEY **out, + pem_password_cb *cb, + void *userdata); + +// PEM_write_bio_PUBKEY writes `in` as a PEM block of type "PUBLIC KEY", as +// described in `PEM_write_bio_SAMPLE`. +OPENSSL_EXPORT int PEM_write_bio_PUBKEY(BIO *bio, const EVP_PKEY *in); + +// PEM_write_bio_PKCS8PrivateKey_nid behaves like `PEM_write_bio_PrivateKey` +// but uses the PBES1 algorithm specified by `nid` for encryption. See also +// `PKCS8_encrypt`. If `nid` is -1, it writes the private key unencrypted. +OPENSSL_EXPORT int PEM_write_bio_PKCS8PrivateKey_nid( + BIO *bio, const EVP_PKEY *in, int nid, const char *pass, int pass_len, + pem_password_cb *cb, void *userdata); + +// PEM_write_bio_PKCS8PrivateKey is an alias for `PEM_write_bio_PrivateKey`. +OPENSSL_EXPORT int PEM_write_bio_PKCS8PrivateKey(BIO *bio, const EVP_PKEY *in, + const EVP_CIPHER *enc, + const char *pass, int pass_len, + pem_password_cb *cb, + void *userdata); + +// PEM_write_PKCS8PrivateKey_nid behaves like +// `PEM_write_bio_PKCS8PrivateKey_nid` but writes to `fp`. +OPENSSL_EXPORT int PEM_write_PKCS8PrivateKey_nid(FILE *fp, const EVP_PKEY *in, + int nid, const char *pass, + int pass_len, + pem_password_cb *cb, + void *userdata); + +// PEM_write_PKCS8PrivateKey behaves like `PEM_write_bio_PKCS8PrivateKey` but +// writes to `fp`. +OPENSSL_EXPORT int PEM_write_PKCS8PrivateKey(FILE *fp, const EVP_PKEY *in, + const EVP_CIPHER *enc, + const char *pass, int pass_len, + pem_password_cb *cb, + void *userdata); + +// PEM_read_bio_PKCS8 reads a PEM block of type "ENCRYPTED PRIVATE KEY", as +// described in `PEM_read_bio_SAMPLE`. +// +// Although this function accepts `cb` and `userdata` to decrypt the PEM-level +// encryption, it does not decrypt the EncryptedPrivateKeyInfo structure. It +// returns the encrypted key as an `X509_SIG` object which, despite its name, is +// an algorithm and octet string pair. +OPENSSL_EXPORT X509_SIG *PEM_read_bio_PKCS8(BIO *bio, X509_SIG **out, + pem_password_cb *cb, + void *userdata); + +// PEM_write_bio_PKCS8 writes a PEM block of type "ENCRYPTED PRIVATE KEY", as +// described in `PEM_write_bio_SAMPLE`. `in` is used to represent an +// already-encrypted EncryptedPrivateKeyInfo structure. +OPENSSL_EXPORT int PEM_write_bio_PKCS8(BIO *bio, const X509_SIG *in); + +// PEM_read_bio_PKCS8_PRIV_KEY_INFO reads a PEM block of type "PRIVATE KEY", as +// described in `PEM_read_bio_SAMPLE`. +OPENSSL_EXPORT PKCS8_PRIV_KEY_INFO *PEM_read_bio_PKCS8_PRIV_KEY_INFO( + BIO *bio, PKCS8_PRIV_KEY_INFO **out, pem_password_cb *cb, void *userdata); + +// PEM_write_bio_PKCS8_PRIV_KEY_INFO writes a PEM block of type "PRIVATE KEY", +// as described in `PEM_write_bio_SAMPLE`. +OPENSSL_EXPORT int PEM_write_bio_PKCS8_PRIV_KEY_INFO( + BIO *bio, const PKCS8_PRIV_KEY_INFO *in); + +// PEM_read_bio_RSAPrivateKey behaves like `PEM_read_bio_PrivateKey` but only +// returns RSA keys, represented as an `RSA` object. Keys of other types result +// in an error. +OPENSSL_EXPORT RSA *PEM_read_bio_RSAPrivateKey(BIO *bio, RSA **out, + pem_password_cb *cb, + void *userdata); + +// PEM_write_bio_RSAPrivateKey writes `in` as a PEM block of type "RSA PRIVATE +// KEY", as described in `PEM_write_bio_SAMPLE`. +OPENSSL_EXPORT int PEM_write_bio_RSAPrivateKey( + BIO *bio, const RSA *in, const EVP_CIPHER *enc, const uint8_t *pass, + int pass_len, pem_password_cb *cb, void *userdata); + +// PEM_read_bio_RSAPublicKey reads a PEM block of type "RSA PUBLIC KEY", as +// described in `PEM_read_bio_SAMPLE`. +OPENSSL_EXPORT RSA *PEM_read_bio_RSAPublicKey(BIO *bio, RSA **out, + pem_password_cb *cb, + void *userdata); + +// PEM_write_bio_RSAPublicKey writes a PEM block of type "RSA PUBLIC KEY", as +// described in `PEM_write_bio_SAMPLE`. +OPENSSL_EXPORT int PEM_write_bio_RSAPublicKey(BIO *bio, const RSA *in); + +// PEM_read_bio_RSA_PUBKEY behaves like `PEM_read_bio_PUBKEY` but only returns +// RSA keys, represented as an `RSA` object. Keys of other types result in an +// error. +OPENSSL_EXPORT RSA *PEM_read_bio_RSA_PUBKEY(BIO *bio, RSA **out, + pem_password_cb *cb, + void *userdata); + +// PEM_write_bio_RSA_PUBKEY writes `in` as a PEM block of type "PUBLIC KEY", as +// described in `PEM_write_bio_SAMPLE`. +OPENSSL_EXPORT int PEM_write_bio_RSA_PUBKEY(BIO *bio, const RSA *in); + +// PEM_read_bio_DSAPrivateKey behaves like `PEM_read_bio_PrivateKey` but only +// returns DSA keys, represented as a `DSA` object. Keys of other types result +// in an error. +OPENSSL_EXPORT DSA *PEM_read_bio_DSAPrivateKey(BIO *bio, DSA **out, + pem_password_cb *cb, + void *userdata); + +// PEM_write_bio_DSAPrivateKey writes `in` as a PEM block of type "DSA PRIVATE +// KEY", as described in `PEM_write_bio_SAMPLE`. +OPENSSL_EXPORT int PEM_write_bio_DSAPrivateKey( + BIO *bio, const DSA *in, const EVP_CIPHER *enc, const uint8_t *pass, + int pass_len, pem_password_cb *cb, void *userdata); + +// PEM_read_bio_DSA_PUBKEY behaves like `PEM_read_bio_PUBKEY` but only returns +// DSA keys, represented as a `DSA` object. Keys of other types result in an +// error. +OPENSSL_EXPORT DSA *PEM_read_bio_DSA_PUBKEY(BIO *bio, DSA **out, + pem_password_cb *cb, + void *userdata); + +// PEM_write_bio_DSA_PUBKEY writes `in` in SubjectPublicKeyInfo format as a PEM +// block of type "PUBLIC KEY", as described in `PEM_write_bio_SAMPLE`. +OPENSSL_EXPORT int PEM_write_bio_DSA_PUBKEY(BIO *bio, const DSA *in); + +// PEM_read_bio_DSAparams reads a PEM block of type "DSA PARAMETERS", as +// described in `PEM_read_bio_SAMPLE`. +OPENSSL_EXPORT DSA *PEM_read_bio_DSAparams(BIO *bio, DSA **out, + pem_password_cb *cb, void *userdata); + +// PEM_write_bio_DSAparams writes a PEM block of type "DSA PARAMETERS", as +// described in `PEM_write_bio_SAMPLE`. +OPENSSL_EXPORT int PEM_write_bio_DSAparams(BIO *bio, const DSA *in); + +// PEM_read_bio_ECPrivateKey behaves like `PEM_read_bio_PrivateKey` but only +// returns EC keys, represented as an `EC_KEY` object. Keys of other types +// result in an error. +OPENSSL_EXPORT EC_KEY *PEM_read_bio_ECPrivateKey(BIO *bio, EC_KEY **out, + pem_password_cb *cb, + void *userdata); + +// PEM_write_bio_ECPrivateKey writes `in` as a PEM block of type "EC PRIVATE +// KEY", as described in `PEM_write_bio_SAMPLE`. +OPENSSL_EXPORT int PEM_write_bio_ECPrivateKey(BIO *bio, const EC_KEY *in, + const EVP_CIPHER *enc, + const uint8_t *pass, int pass_len, + pem_password_cb *cb, + void *userdata); + +// PEM_read_bio_EC_PUBKEY behaves like `PEM_read_bio_PUBKEY` but only returns +// EC keys, represented as an `EC_KEY` object. Keys of other types result in an +// error. +OPENSSL_EXPORT EC_KEY *PEM_read_bio_EC_PUBKEY(BIO *bio, EC_KEY **out, + pem_password_cb *cb, + void *userdata); + +// PEM_write_bio_EC_PUBKEY writes `in` in SubjectPublicKeyInfo format as a PEM +// block of type "PUBLIC KEY", as described in `PEM_write_bio_SAMPLE`. +OPENSSL_EXPORT int PEM_write_bio_EC_PUBKEY(BIO *bio, const EC_KEY *in); + +// PEM_read_bio_DHparams reads a PEM block of type "DH PARAMETERS", as described +// in `PEM_read_bio_SAMPLE`. +OPENSSL_EXPORT DH *PEM_read_bio_DHparams(BIO *bio, DH **out, + pem_password_cb *cb, void *userdata); + +// PEM_write_bio_DHparams writes a PEM block of type "DH PARAMETERS", as +// described in `PEM_write_bio_SAMPLE`. +OPENSSL_EXPORT int PEM_write_bio_DHparams(BIO *bio, const DH *in); + + +// File-based functions. + +// The following functions behave like the corresponding `PEM_read_bio_*` +// functions, but read from `fp`. +OPENSSL_EXPORT X509 *PEM_read_X509(FILE *fp, X509 **out, pem_password_cb *cb, + void *userdata); +OPENSSL_EXPORT X509_CRL *PEM_read_X509_CRL(FILE *fp, X509_CRL **out, + pem_password_cb *cb, void *userdata); +OPENSSL_EXPORT X509 *PEM_read_X509_AUX(FILE *fp, X509 **out, + pem_password_cb *cb, void *userdata); +OPENSSL_EXPORT STACK_OF(X509_INFO) *PEM_X509_INFO_read(FILE *fp, + STACK_OF(X509_INFO) *sk, + pem_password_cb *cb, + void *userdata); +OPENSSL_EXPORT X509_REQ *PEM_read_X509_REQ(FILE *fp, X509_REQ **out, + pem_password_cb *cb, void *userdata); +OPENSSL_EXPORT PKCS7 *PEM_read_PKCS7(FILE *fp, PKCS7 **out, pem_password_cb *cb, + void *userdata); +OPENSSL_EXPORT X509_SIG *PEM_read_PKCS8(FILE *fp, X509_SIG **out, + pem_password_cb *cb, void *userdata); +OPENSSL_EXPORT PKCS8_PRIV_KEY_INFO *PEM_read_PKCS8_PRIV_KEY_INFO( + FILE *fp, PKCS8_PRIV_KEY_INFO **out, pem_password_cb *cb, void *userdata); +OPENSSL_EXPORT RSA *PEM_read_RSAPrivateKey(FILE *fp, RSA **out, + pem_password_cb *cb, void *userdata); +OPENSSL_EXPORT RSA *PEM_read_RSAPublicKey(FILE *fp, RSA **out, + pem_password_cb *cb, void *userdata); +OPENSSL_EXPORT RSA *PEM_read_RSA_PUBKEY(FILE *fp, RSA **out, + pem_password_cb *cb, void *userdata); +OPENSSL_EXPORT DSA *PEM_read_DSAPrivateKey(FILE *fp, DSA **out, + pem_password_cb *cb, void *userdata); +OPENSSL_EXPORT DSA *PEM_read_DSA_PUBKEY(FILE *fp, DSA **out, + pem_password_cb *cb, void *userdata); +OPENSSL_EXPORT DSA *PEM_read_DSAparams(FILE *fp, DSA **out, pem_password_cb *cb, + void *userdata); +OPENSSL_EXPORT EC_KEY *PEM_read_ECPrivateKey(FILE *fp, EC_KEY **out, + pem_password_cb *cb, + void *userdata); +OPENSSL_EXPORT EC_KEY *PEM_read_EC_PUBKEY(FILE *fp, EC_KEY **out, + pem_password_cb *cb, void *userdata); +OPENSSL_EXPORT DH *PEM_read_DHparams(FILE *fp, DH **out, pem_password_cb *cb, + void *userdata); +OPENSSL_EXPORT EVP_PKEY *PEM_read_PrivateKey(FILE *fp, EVP_PKEY **out, + pem_password_cb *cb, + void *userdata); +OPENSSL_EXPORT EVP_PKEY *PEM_read_PUBKEY(FILE *fp, EVP_PKEY **out, + pem_password_cb *cb, void *userdata); + +// The following functions behave like the corresponding `PEM_write_bio_*` +// functions, but write to `fp`. +OPENSSL_EXPORT int PEM_write_X509(FILE *fp, const X509 *x); +OPENSSL_EXPORT int PEM_write_X509_CRL(FILE *fp, const X509_CRL *in); +OPENSSL_EXPORT int PEM_write_X509_AUX(FILE *fp, const X509 *in); +OPENSSL_EXPORT int PEM_write_X509_REQ(FILE *fp, const X509_REQ *in); +OPENSSL_EXPORT int PEM_write_X509_REQ_NEW(FILE *fp, const X509_REQ *in); +OPENSSL_EXPORT int PEM_write_PKCS7(FILE *fp, const PKCS7 *in); +OPENSSL_EXPORT int PEM_write_PKCS8(FILE *fp, const X509_SIG *in); +OPENSSL_EXPORT int PEM_write_PKCS8_PRIV_KEY_INFO(FILE *fp, + const PKCS8_PRIV_KEY_INFO *in); +OPENSSL_EXPORT int PEM_write_RSAPrivateKey(FILE *fp, const RSA *in, + const EVP_CIPHER *enc, + const uint8_t *pass, int pass_len, + pem_password_cb *cb, void *userdata); +OPENSSL_EXPORT int PEM_write_RSAPublicKey(FILE *fp, const RSA *in); +OPENSSL_EXPORT int PEM_write_RSA_PUBKEY(FILE *fp, const RSA *in); +OPENSSL_EXPORT int PEM_write_DSAPrivateKey(FILE *fp, const DSA *in, + const EVP_CIPHER *enc, + const uint8_t *pass, int pass_len, + pem_password_cb *cb, void *userdata); +OPENSSL_EXPORT int PEM_write_DSA_PUBKEY(FILE *fp, const DSA *in); +OPENSSL_EXPORT int PEM_write_DSAparams(FILE *fp, const DSA *in); +OPENSSL_EXPORT int PEM_write_ECPrivateKey(FILE *fp, const EC_KEY *in, + const EVP_CIPHER *enc, + const uint8_t *pass, int pass_len, + pem_password_cb *cb, void *userdata); +OPENSSL_EXPORT int PEM_write_EC_PUBKEY(FILE *fp, const EC_KEY *in); +OPENSSL_EXPORT int PEM_write_DHparams(FILE *fp, const DH *in); +OPENSSL_EXPORT int PEM_write_PrivateKey(FILE *fp, const EVP_PKEY *in, + const EVP_CIPHER *enc, + const uint8_t *pass, int pass_len, + pem_password_cb *cb, void *userdata); +OPENSSL_EXPORT int PEM_write_PUBKEY(FILE *fp, const EVP_PKEY *in); + + +// Reading and writing raw PEM blocks. +// +// The functions in this section read and write PEM blocks without decoding +// their contents. + +// PEM_BUFSIZE is an arbitrary buffer size used within the library. Some +// external callers depend on it being defined. #define PEM_BUFSIZE 1024 +// The following constants are a collection of known PEM types. #define PEM_STRING_X509_OLD "X509 CERTIFICATE" #define PEM_STRING_X509 "CERTIFICATE" #define PEM_STRING_X509_PAIR "CERTIFICATE PAIR" @@ -53,7 +538,6 @@ extern "C" { #define PEM_STRING_X509_REQ_OLD "NEW CERTIFICATE REQUEST" #define PEM_STRING_X509_REQ "CERTIFICATE REQUEST" #define PEM_STRING_X509_CRL "X509 CRL" -#define PEM_STRING_EVP_PKEY "ANY PRIVATE KEY" #define PEM_STRING_PUBLIC "PUBLIC KEY" #define PEM_STRING_RSA "RSA PRIVATE KEY" #define PEM_STRING_RSA_PUBLIC "RSA PUBLIC KEY" @@ -71,381 +555,193 @@ extern "C" { #define PEM_STRING_ECPRIVATEKEY "EC PRIVATE KEY" #define PEM_STRING_CMS "CMS" -#define PEM_TYPE_ENCRYPTED 10 -#define PEM_TYPE_MIC_ONLY 20 -#define PEM_TYPE_MIC_CLEAR 30 -#define PEM_TYPE_CLEAR 40 - -// These macros make the PEM_read/PEM_write functions easier to maintain and -// write. Now they are all implemented with either: -// IMPLEMENT_PEM_rw(...) or IMPLEMENT_PEM_rw_cb(...) - - -#define IMPLEMENT_PEM_read_fp(name, type, str, asn1) \ - static void *pem_read_##name##_d2i(void **x, const unsigned char **inp, \ - long len) { \ - return d2i_##asn1((type **)x, inp, len); \ - } \ - OPENSSL_EXPORT type *PEM_read_##name(FILE *fp, type **x, \ - pem_password_cb *cb, void *u) { \ - return (type *)PEM_ASN1_read(pem_read_##name##_d2i, str, fp, (void **)x, \ - cb, u); \ - } - -#define IMPLEMENT_PEM_write_fp(name, type, str, asn1) \ - static int pem_write_##name##_i2d(const void *x, unsigned char **outp) { \ - return i2d_##asn1((type *)x, outp); \ - } \ - OPENSSL_EXPORT int PEM_write_##name(FILE *fp, type *x) { \ - return PEM_ASN1_write(pem_write_##name##_i2d, str, fp, x, NULL, NULL, 0, \ - NULL, NULL); \ - } - -#define IMPLEMENT_PEM_write_fp_const(name, type, str, asn1) \ - static int pem_write_##name##_i2d(const void *x, unsigned char **outp) { \ - return i2d_##asn1((const type *)x, outp); \ - } \ - OPENSSL_EXPORT int PEM_write_##name(FILE *fp, const type *x) { \ - return PEM_ASN1_write(pem_write_##name##_i2d, str, fp, (void *)x, NULL, \ - NULL, 0, NULL, NULL); \ - } - -#define IMPLEMENT_PEM_write_cb_fp(name, type, str, asn1) \ - static int pem_write_##name##_i2d(const void *x, unsigned char **outp) { \ - return i2d_##asn1((type *)x, outp); \ - } \ - OPENSSL_EXPORT int PEM_write_##name( \ - FILE *fp, type *x, const EVP_CIPHER *enc, const unsigned char *pass, \ - int pass_len, pem_password_cb *cb, void *u) { \ - return PEM_ASN1_write(pem_write_##name##_i2d, str, fp, x, enc, pass, \ - pass_len, cb, u); \ - } - -#define IMPLEMENT_PEM_write_cb_fp_const(name, type, str, asn1) \ - static int pem_write_##name##_i2d(const void *x, unsigned char **outp) { \ - return i2d_##asn1((const type *)x, outp); \ - } \ - OPENSSL_EXPORT int PEM_write_##name( \ - FILE *fp, type *x, const EVP_CIPHER *enc, const unsigned char *pass, \ - int pass_len, pem_password_cb *cb, void *u) { \ - return PEM_ASN1_write(pem_write_##name##_i2d, str, fp, x, enc, pass, \ - pass_len, cb, u); \ - } - - -#define IMPLEMENT_PEM_read_bio(name, type, str, asn1) \ - static void *pem_read_bio_##name##_d2i(void **x, const unsigned char **inp, \ - long len) { \ - return d2i_##asn1((type **)x, inp, len); \ - } \ - OPENSSL_EXPORT type *PEM_read_bio_##name(BIO *bp, type **x, \ - pem_password_cb *cb, void *u) { \ - return (type *)PEM_ASN1_read_bio(pem_read_bio_##name##_d2i, str, bp, \ - (void **)x, cb, u); \ - } - -#define IMPLEMENT_PEM_write_bio(name, type, str, asn1) \ - static int pem_write_bio_##name##_i2d(const void *x, unsigned char **outp) { \ - return i2d_##asn1((type *)x, outp); \ - } \ - OPENSSL_EXPORT int PEM_write_bio_##name(BIO *bp, type *x) { \ - return PEM_ASN1_write_bio(pem_write_bio_##name##_i2d, str, bp, x, NULL, \ - NULL, 0, NULL, NULL); \ - } - -#define IMPLEMENT_PEM_write_bio_const(name, type, str, asn1) \ - static int pem_write_bio_##name##_i2d(const void *x, unsigned char **outp) { \ - return i2d_##asn1((const type *)x, outp); \ - } \ - OPENSSL_EXPORT int PEM_write_bio_##name(BIO *bp, const type *x) { \ - return PEM_ASN1_write_bio(pem_write_bio_##name##_i2d, str, bp, (void *)x, \ - NULL, NULL, 0, NULL, NULL); \ - } - -#define IMPLEMENT_PEM_write_cb_bio(name, type, str, asn1) \ - static int pem_write_bio_##name##_i2d(const void *x, unsigned char **outp) { \ - return i2d_##asn1((type *)x, outp); \ - } \ - OPENSSL_EXPORT int PEM_write_bio_##name( \ - BIO *bp, type *x, const EVP_CIPHER *enc, const unsigned char *pass, \ - int pass_len, pem_password_cb *cb, void *u) { \ - return PEM_ASN1_write_bio(pem_write_bio_##name##_i2d, str, bp, x, enc, \ - pass, pass_len, cb, u); \ - } - -#define IMPLEMENT_PEM_write_cb_bio_const(name, type, str, asn1) \ - static int pem_write_bio_##name##_i2d(const void *x, unsigned char **outp) { \ - return i2d_##asn1((const type *)x, outp); \ - } \ - OPENSSL_EXPORT int PEM_write_bio_##name( \ - BIO *bp, type *x, const EVP_CIPHER *enc, const unsigned char *pass, \ - int pass_len, pem_password_cb *cb, void *u) { \ - return PEM_ASN1_write_bio(pem_write_bio_##name##_i2d, str, bp, (void *)x, \ - enc, pass, pass_len, cb, u); \ - } - -#define IMPLEMENT_PEM_write(name, type, str, asn1) \ - IMPLEMENT_PEM_write_bio(name, type, str, asn1) \ - IMPLEMENT_PEM_write_fp(name, type, str, asn1) - -#define IMPLEMENT_PEM_write_const(name, type, str, asn1) \ - IMPLEMENT_PEM_write_bio_const(name, type, str, asn1) \ - IMPLEMENT_PEM_write_fp_const(name, type, str, asn1) - -#define IMPLEMENT_PEM_write_cb(name, type, str, asn1) \ - IMPLEMENT_PEM_write_cb_bio(name, type, str, asn1) \ - IMPLEMENT_PEM_write_cb_fp(name, type, str, asn1) - -#define IMPLEMENT_PEM_write_cb_const(name, type, str, asn1) \ - IMPLEMENT_PEM_write_cb_bio_const(name, type, str, asn1) \ - IMPLEMENT_PEM_write_cb_fp_const(name, type, str, asn1) - -#define IMPLEMENT_PEM_read(name, type, str, asn1) \ - IMPLEMENT_PEM_read_bio(name, type, str, asn1) \ - IMPLEMENT_PEM_read_fp(name, type, str, asn1) - -#define IMPLEMENT_PEM_rw(name, type, str, asn1) \ - IMPLEMENT_PEM_read(name, type, str, asn1) \ - IMPLEMENT_PEM_write(name, type, str, asn1) - -#define IMPLEMENT_PEM_rw_const(name, type, str, asn1) \ - IMPLEMENT_PEM_read(name, type, str, asn1) \ - IMPLEMENT_PEM_write_const(name, type, str, asn1) - -#define IMPLEMENT_PEM_rw_cb(name, type, str, asn1) \ - IMPLEMENT_PEM_read(name, type, str, asn1) \ - IMPLEMENT_PEM_write_cb(name, type, str, asn1) - -// These are the same except they are for the declarations - -#define DECLARE_PEM_read_fp(name, type) \ - OPENSSL_EXPORT type *PEM_read_##name(FILE *fp, type **x, \ - pem_password_cb *cb, void *u); - -#define DECLARE_PEM_write_fp(name, type) \ - OPENSSL_EXPORT int PEM_write_##name(FILE *fp, type *x); - -#define DECLARE_PEM_write_fp_const(name, type) \ - OPENSSL_EXPORT int PEM_write_##name(FILE *fp, const type *x); - -#define DECLARE_PEM_write_cb_fp(name, type) \ - OPENSSL_EXPORT int PEM_write_##name( \ - FILE *fp, type *x, const EVP_CIPHER *enc, const unsigned char *pass, \ - int pass_len, pem_password_cb *cb, void *u); - -#define DECLARE_PEM_read_bio(name, type) \ - OPENSSL_EXPORT type *PEM_read_bio_##name(BIO *bp, type **x, \ - pem_password_cb *cb, void *u); - -#define DECLARE_PEM_write_bio(name, type) \ - OPENSSL_EXPORT int PEM_write_bio_##name(BIO *bp, type *x); - -#define DECLARE_PEM_write_bio_const(name, type) \ - OPENSSL_EXPORT int PEM_write_bio_##name(BIO *bp, const type *x); - -#define DECLARE_PEM_write_cb_bio(name, type) \ - OPENSSL_EXPORT int PEM_write_bio_##name( \ - BIO *bp, type *x, const EVP_CIPHER *enc, const unsigned char *pass, \ - int pass_len, pem_password_cb *cb, void *u); - - -#define DECLARE_PEM_write(name, type) \ - DECLARE_PEM_write_bio(name, type) \ - DECLARE_PEM_write_fp(name, type) - -#define DECLARE_PEM_write_const(name, type) \ - DECLARE_PEM_write_bio_const(name, type) \ - DECLARE_PEM_write_fp_const(name, type) - -#define DECLARE_PEM_write_cb(name, type) \ - DECLARE_PEM_write_cb_bio(name, type) \ - DECLARE_PEM_write_cb_fp(name, type) - -#define DECLARE_PEM_read(name, type) \ - DECLARE_PEM_read_bio(name, type) \ - DECLARE_PEM_read_fp(name, type) - -#define DECLARE_PEM_rw(name, type) \ - DECLARE_PEM_read(name, type) \ - DECLARE_PEM_write(name, type) - -#define DECLARE_PEM_rw_const(name, type) \ - DECLARE_PEM_read(name, type) \ - DECLARE_PEM_write_const(name, type) - -#define DECLARE_PEM_rw_cb(name, type) \ - DECLARE_PEM_read(name, type) \ - DECLARE_PEM_write_cb(name, type) - -// "userdata": new with OpenSSL 0.9.4 -typedef int pem_password_cb(char *buf, int size, int rwflag, void *userdata); - -// PEM_read_bio reads from |bp|, until the next PEM block. If one is found, it -// returns one and sets |*name|, |*header|, and |*data| to newly-allocated -// buffers containing the PEM type, the header block, and the decoded data, -// respectively. |*name| and |*header| are NUL-terminated C strings, while -// |*data| has |*len| bytes. The caller must release each of |*name|, |*header|, -// and |*data| with |OPENSSL_free| when done. If no PEM block is found, this -// function returns zero and pushes |PEM_R_NO_START_LINE| to the error queue. If -// one is found, but there is an error decoding it, it returns zero and pushes -// some other error to the error queue. -OPENSSL_EXPORT int PEM_read_bio(BIO *bp, char **name, char **header, - unsigned char **data, long *len); +// PEM_STRING_EVP_PKEY is not a PEM type, but is an implementation detail of +// `PEM_read_bio_PrivateKey`. +#define PEM_STRING_EVP_PKEY "ANY PRIVATE KEY" -// PEM_write_bio writes a PEM block to |bp|, containing |len| bytes from |data| -// as data. |name| and |hdr| are NUL-terminated C strings containing the PEM +// PEM_read_bio reads from `bio` until the next PEM block. If one is found, it +// returns one and sets `*out_name`, `*out_header`, and `*out_data` to +// newly-allocated buffers containing the PEM type, the header block, and the +// decoded data, respectively. `*out_name` and `*out_header` are NUL-terminated +// C strings, while `*out_data` has `*out_len` bytes. The caller must release +// each of `*out_name`, `*out_header`, and `*out_data` with `OPENSSL_free` when +// done. +// +// If no PEM block is found, this function returns zero and pushes +// `PEM_R_NO_START_LINE` to the error queue. If one is found, but there is an +// error decoding it, it returns zero and pushes some other error to the error +// queue. +// +// This function does not decrypt encrypted PEM blocks and instead returns the +// header and (possibly encrypted) data unprocessed. See `PEM_bytes_read_bio` to +// decrypt blocks. +OPENSSL_EXPORT int PEM_read_bio(BIO *bio, char **out_name, char **out_header, + uint8_t **out_data, long *out_len); + +// PEM_read behaves like `PEM_read_bio` but reads from `fp`. +OPENSSL_EXPORT int PEM_read(FILE *fp, char **out_name, char **out_header, + uint8_t **out_data, long *out_len); + +// PEM_write_bio writes a PEM block to `bio`, containing `len` bytes from `data` +// as data. `name` and `hdr` are NUL-terminated C strings containing the PEM // type and header block, respectively. This function returns zero on error and // the number of bytes written on success. -OPENSSL_EXPORT int PEM_write_bio(BIO *bp, const char *name, const char *hdr, - const unsigned char *data, long len); +OPENSSL_EXPORT int PEM_write_bio(BIO *bio, const char *name, const char *hdr, + const uint8_t *data, long len); -OPENSSL_EXPORT int PEM_bytes_read_bio(unsigned char **pdata, long *plen, - char **pnm, const char *name, BIO *bp, - pem_password_cb *cb, void *u); -OPENSSL_EXPORT void *PEM_ASN1_read_bio(d2i_of_void *d2i, const char *name, - BIO *bp, void **x, pem_password_cb *cb, - void *u); -OPENSSL_EXPORT int PEM_ASN1_write_bio(i2d_of_void *i2d, const char *name, - BIO *bp, void *x, const EVP_CIPHER *enc, - const unsigned char *pass, int pass_len, - pem_password_cb *cb, void *u); - -// PEM_X509_INFO_read_bio reads PEM blocks from |bp| and decodes any -// certificates, CRLs, and private keys found. It returns a -// |STACK_OF(X509_INFO)| structure containing the results, or NULL on error. +// PEM_write behaves like `PEM_write_bio` but reads from `fp`. +OPENSSL_EXPORT int PEM_write(FILE *fp, const char *name, const char *hdr, + const uint8_t *data, long len); + +// PEM_bytes_read_bio reads from `bio` until it finds a PEM block whose name +// matches `expected_name`. If one is found, it sets `*out_name` and `*out_data` +// to newly-allocated buffers containing the PEM type and (possibly decrypted) +// PEM data. `*out_name` is a NUL-terminated C string, while `*out_data` has +// `*out_len` bytes. The caller must release `*out_name` and `*out_data` with +// `OPENSSL_free` when done. // -// If |sk| is NULL, the result on success will be a newly-allocated -// |STACK_OF(X509_INFO)| structure which should be released with -// |sk_X509_INFO_pop_free| and |X509_INFO_free| when done. +// If the PEM block is encrypted, `cb` will be called to look up the password. +// See `pem_password_cb` for details. If `cb` is NULL, `PEM_def_callback` is +// used and `userdata` should be a NUL-terminated C string containing the +// password. Set both `cb` and `userdata` to NULL to only handle plaintext +// blocks. // -// If |sk| is non-NULL, it appends the results to |sk| instead and returns |sk| -// on success. In this case, the caller retains ownership of |sk| in both -// success and failure. +// `expected_name` and `*out_name` may not necessarily be the same value, so +// callers must check `*out_name` before decoding `*out_data`. In addition to an +// exact match, the following values are also accepted: // -// This function will decrypt any encrypted certificates in |bp|, using |cb|, -// but it will not decrypt encrypted private keys. Encrypted private keys are -// instead represented as placeholder |X509_INFO| objects with an empty |x_pkey| -// field. This allows this function to be used with inputs with unencrypted -// certificates, but encrypted passwords, without knowing the password. However, -// it also means that this function cannot be used to decrypt the private key -// when the password is known. +// - If `expected_name` is "CERTIFICATE", the older "X509 CERTIFICATE" type is +// also accepted. // -// WARNING: If the input contains "TRUSTED CERTIFICATE" PEM blocks, this -// function parses auxiliary properties as in |d2i_X509_AUX|. Passing untrusted -// input to this function allows an attacker to influence those properties. See -// |d2i_X509_AUX| for details. -OPENSSL_EXPORT STACK_OF(X509_INFO) *PEM_X509_INFO_read_bio( - BIO *bp, STACK_OF(X509_INFO) *sk, pem_password_cb *cb, void *u); - -// PEM_X509_INFO_read behaves like |PEM_X509_INFO_read_bio| but reads from a -// |FILE|. -OPENSSL_EXPORT STACK_OF(X509_INFO) *PEM_X509_INFO_read(FILE *fp, - STACK_OF(X509_INFO) *sk, - pem_password_cb *cb, - void *u); - -OPENSSL_EXPORT int PEM_read(FILE *fp, char **name, char **header, - unsigned char **data, long *len); -OPENSSL_EXPORT int PEM_write(FILE *fp, const char *name, const char *hdr, - const unsigned char *data, long len); -OPENSSL_EXPORT void *PEM_ASN1_read(d2i_of_void *d2i, const char *name, FILE *fp, - void **x, pem_password_cb *cb, void *u); -OPENSSL_EXPORT int PEM_ASN1_write(i2d_of_void *i2d, const char *name, FILE *fp, - void *x, const EVP_CIPHER *enc, - const unsigned char *pass, int pass_len, - pem_password_cb *callback, void *u); - -// PEM_def_callback treats |userdata| as a string and copies it into |buf|, -// assuming its |size| is sufficient. Returns the length of the string, or -1 on -// error. Error cases the buffer being too small, or |buf| and |userdata| being -// NULL. Note that this is different from OpenSSL, which prompts for a password. -OPENSSL_EXPORT int PEM_def_callback(char *buf, int size, int rwflag, - void *userdata); - - -DECLARE_PEM_rw(X509, X509) - -// TODO(crbug.com/boringssl/426): When documenting these, copy the warning -// about auxiliary properties from |PEM_X509_INFO_read_bio|. - -DECLARE_PEM_rw(X509_AUX, X509) - -DECLARE_PEM_rw(X509_REQ, X509_REQ) -DECLARE_PEM_write(X509_REQ_NEW, X509_REQ) - -DECLARE_PEM_rw(X509_CRL, X509_CRL) - -DECLARE_PEM_rw(PKCS7, PKCS7) -DECLARE_PEM_rw(PKCS8, X509_SIG) - -DECLARE_PEM_rw(PKCS8_PRIV_KEY_INFO, PKCS8_PRIV_KEY_INFO) - -DECLARE_PEM_rw_cb(RSAPrivateKey, RSA) - -DECLARE_PEM_rw_const(RSAPublicKey, RSA) -DECLARE_PEM_rw(RSA_PUBKEY, RSA) - -#ifndef OPENSSL_NO_DSA - -DECLARE_PEM_rw_cb(DSAPrivateKey, DSA) - -DECLARE_PEM_rw(DSA_PUBKEY, DSA) - -DECLARE_PEM_rw_const(DSAparams, DSA) - -#endif - -DECLARE_PEM_rw_cb(ECPrivateKey, EC_KEY) -DECLARE_PEM_rw(EC_PUBKEY, EC_KEY) - - -DECLARE_PEM_rw_const(DHparams, DH) - - -DECLARE_PEM_rw_cb(PrivateKey, EVP_PKEY) - -DECLARE_PEM_rw(PUBKEY, EVP_PKEY) - -OPENSSL_EXPORT int PEM_write_bio_PKCS8PrivateKey_nid(BIO *bp, const EVP_PKEY *x, - int nid, const char *pass, - int pass_len, - pem_password_cb *cb, - void *u); -OPENSSL_EXPORT int PEM_write_bio_PKCS8PrivateKey(BIO *bp, const EVP_PKEY *x, - const EVP_CIPHER *enc, - const char *pass, int pass_len, - pem_password_cb *cb, void *u); -OPENSSL_EXPORT int i2d_PKCS8PrivateKey_bio(BIO *bp, const EVP_PKEY *x, +// - If `expected_name` is "CERTIFICATE REQUEST", the older "NEW CERTIFICATE +// REQUEST" type is also accepted. +// +// - If `expected_name` is "TRUSTED CERTIFICATE", "CERTIFICATE" and the older +// "X509 CERTIFICATE" are also accepted. +// +// - If `expected_name` is "PKCS7", "PKCS #7 SIGNED DATA" is also accepted. +// +// - If `expected_name` is "PKCS7", "CERTIFICATE" is also accepted. This is an +// exposed implementation detail of `PKCS7_get_PEM_certificates`, which works +// around a 2000-era mistake by some CAs. +// +// - If `expected_name` is "ANY PRIVATE KEY", the type "ANY PRIVATE KEY" is not +// accepted and, instead, the function accepts "PRIVATE KEY", "ENCRYPTED +// PRIVATE KEY", "RSA PRIVATE KEY", "EC PRIVATE KEY", and "DSA PRIVATE KEY". +// This is an exposed implementation detail of `PEM_read_bio_PrivateKey`. +// +// TODO(davidben): Can some of the older aliases and workarounds be removed now? +// +// If no PEM block is found, this function returns zero and pushes +// `PEM_R_NO_START_LINE` to the error queue. If one is found, but there is an +// error decoding it, it returns zero and pushes some other error to the error +// queue. +OPENSSL_EXPORT int PEM_bytes_read_bio(uint8_t **out_data, long *out_len, + char **out_name, + const char *expected_name, BIO *bio, + pem_password_cb *cb, void *userdata); + + +// Reading and writing DER-encoded private keys. + +// d2i_PKCS8PrivateKey_bio reads a DER-encoded EncryptedPrivateKey structure +// (RFC 5208) from `bio`, decrypts it with `PKCS8_decrypt`, and returns the +// result as a newly-allocated `EVP_PKEY`, or NULL on error. On success, if +// `out` is non-NULL, it additionally frees the previous value at `*out` and +// updates `*out` to the result. The password is determined by calling `cb`, or +// `PEM_def_callback` if NULL. +OPENSSL_EXPORT EVP_PKEY *d2i_PKCS8PrivateKey_bio(BIO *bio, EVP_PKEY **out, + pem_password_cb *cb, + void *userdata); + +// i2d_PKCS8PrivateKey_bio encodes `in` as a DER-encoded structure and writes +// it to `bio`. It returns one on success and zero on error. The structure used +// depends on `enc`: +// +// - If `enc` is NULL, it writes a PrivateKeyInfo structure (RFC 5208). +// +// - If `enc` is non-NULL, it writes an EncryptedPrivateKeyInfo structure (RFC +// 5280). The key is encrypted with PBES2, as in `PKCS8_encrypt`. The password +// is specified by `pass`, `cb`, and `userdata`, as in `PEM_write_bio_SAMPLE`. +// +// WARNING: PrivateKeyInfo and EncryptedPrivateKeyInfo are different formats, +// and DER does not include a type header. The encrypted and unencrypted modes +// of this function should not be mixed in the same context. +OPENSSL_EXPORT int i2d_PKCS8PrivateKey_bio(BIO *bio, const EVP_PKEY *in, const EVP_CIPHER *enc, const char *pass, int pass_len, - pem_password_cb *cb, void *u); -OPENSSL_EXPORT int i2d_PKCS8PrivateKey_nid_bio(BIO *bp, const EVP_PKEY *x, + pem_password_cb *cb, void *userdata); + +// i2d_PKCS8PrivateKey_nid_bio encodes `in` as a DER-encoded structure and +// writes it to `bio`. It returns one on success and zero on error. The +// structure used depends on `nid`: +// +// - If `nid` is -1, it writes a PrivateKeyInfo structure (RFC 5208). +// +// - Otherwise, it writes an EncryptedPrivateKeyInfo structure (RFC 5280). The +// key is encrypted with PBES1, as in `PKCS8_encrypt`. The password is +// specified by `pass`, `cb`, and `userdata`, as in `PEM_write_bio_SAMPLE`. +// +// WARNING: PrivateKeyInfo and EncryptedPrivateKeyInfo are different formats, +// and DER does not include a type header. The encrypted and unencrypted modes +// of this function should not be mixed in the same context. +OPENSSL_EXPORT int i2d_PKCS8PrivateKey_nid_bio(BIO *bio, const EVP_PKEY *in, int nid, const char *pass, int pass_len, - pem_password_cb *cb, void *u); -OPENSSL_EXPORT EVP_PKEY *d2i_PKCS8PrivateKey_bio(BIO *bp, EVP_PKEY **x, - pem_password_cb *cb, void *u); + pem_password_cb *cb, + void *userdata); -OPENSSL_EXPORT int i2d_PKCS8PrivateKey_fp(FILE *fp, const EVP_PKEY *x, +// i2d_PKCS8PrivateKey_fp behaves like `i2d_PKCS8PrivateKey_bio` but writes to +// `fp`. +OPENSSL_EXPORT int i2d_PKCS8PrivateKey_fp(FILE *fp, const EVP_PKEY *in, const EVP_CIPHER *enc, const char *pass, int pass_len, - pem_password_cb *cb, void *u); -OPENSSL_EXPORT int i2d_PKCS8PrivateKey_nid_fp(FILE *fp, const EVP_PKEY *x, + pem_password_cb *cb, void *userdata); + +// i2d_PKCS8PrivateKey_nid_fp behaves like `i2d_PKCS8PrivateKey_nid_bio` but +// writes to `fp`. +OPENSSL_EXPORT int i2d_PKCS8PrivateKey_nid_fp(FILE *fp, const EVP_PKEY *in, int nid, const char *pass, int pass_len, pem_password_cb *cb, - void *u); -OPENSSL_EXPORT int PEM_write_PKCS8PrivateKey_nid(FILE *fp, const EVP_PKEY *x, - int nid, const char *pass, - int pass_len, - pem_password_cb *cb, void *u); + void *userdata); -OPENSSL_EXPORT EVP_PKEY *d2i_PKCS8PrivateKey_fp(FILE *fp, EVP_PKEY **x, - pem_password_cb *cb, void *u); +// d2i_PKCS8PrivateKey_fp behaves like `d2i_PKCS8PrivateKey_bio` but reads from +// `fp`. +OPENSSL_EXPORT EVP_PKEY *d2i_PKCS8PrivateKey_fp(FILE *fp, EVP_PKEY **out, + pem_password_cb *cb, + void *userdata); -OPENSSL_EXPORT int PEM_write_PKCS8PrivateKey(FILE *fp, const EVP_PKEY *x, - const EVP_CIPHER *enc, - const char *pass, int pass_len, - pem_password_cb *cd, void *u); + +// Internal functions. +// +// The following functions are used to implement `PEM_read_bio_*` and +// `PEM_write_bio_*`. They should not be used outside the library. + +// PEM_ASN1_read_bio calls `PEM_bytes_read_bio` and then decodes the resulting +// data with `d2i`, which should behave as in `d2i_SAMPLE`. +OPENSSL_EXPORT void *PEM_ASN1_read_bio(d2i_of_void *d2i, const char *name, + BIO *bio, void **out, + pem_password_cb *cb, void *userdata); + +// PEM_ASN1_read behaves like `PEM_ASN1_read_bio` but reads from `fp`. +OPENSSL_EXPORT void *PEM_ASN1_read(d2i_of_void *d2i, const char *name, FILE *fp, + void **out, pem_password_cb *cb, + void *userdata); + +// PEM_ASN1_write_bio encodes `in` with `i2d`, which should behave as in +// `i2d_SAMPLE`. It then writes the result to `bio` as in +// `PEM_write_bio_SAMPLE`. +OPENSSL_EXPORT int PEM_ASN1_write_bio(i2d_of_void *i2d, const char *name, + BIO *bio, const void *in, + const EVP_CIPHER *enc, + const uint8_t *pass, int pass_len, + pem_password_cb *cb, void *userdata); + +// PEM_ASN1_write behaves like `PEM_ASN1_write_bio` but reads from `fp`. +OPENSSL_EXPORT int PEM_ASN1_write(i2d_of_void *i2d, const char *name, FILE *fp, + const void *in, const EVP_CIPHER *enc, + const uint8_t *pass, int pass_len, + pem_password_cb *callback, void *userdata); #if defined(__cplusplus) diff --git a/third_party/boringssl/src/include/openssl/pkcs7.h b/third_party/boringssl/src/include/openssl/pkcs7.h index ec72683fc..dc36d3d3b 100644 --- a/third_party/boringssl/src/include/openssl/pkcs7.h +++ b/third_party/boringssl/src/include/openssl/pkcs7.h @@ -15,7 +15,7 @@ #ifndef OPENSSL_HEADER_PKCS7_H #define OPENSSL_HEADER_PKCS7_H -#include // IWYU pragma: export +#include // IWYU pragma: export #include @@ -34,70 +34,70 @@ DECLARE_STACK_OF(CRYPTO_BUFFER) DECLARE_STACK_OF(X509) DECLARE_STACK_OF(X509_CRL) -// PKCS7_get_raw_certificates parses a PKCS#7, SignedData structure from |cbs| -// and appends the included certificates to |out_certs|. It returns one on -// success and zero on error. |cbs| is advanced passed the structure. +// PKCS7_get_raw_certificates parses a PKCS#7, SignedData structure from `cbs` +// and appends the included certificates to `out_certs`. It returns one on +// success and zero on error. `cbs` is advanced passed the structure. // // Note that a SignedData structure may contain no certificates, in which case // this function succeeds but does not append any certificates. Additionally, // certificates in SignedData structures are unordered. Callers should not -// assume a particular order in |*out_certs| and may need to search for matches +// assume a particular order in `*out_certs` and may need to search for matches // or run path-building algorithms. OPENSSL_EXPORT int PKCS7_get_raw_certificates( STACK_OF(CRYPTO_BUFFER) *out_certs, CBS *cbs, CRYPTO_BUFFER_POOL *pool); -// PKCS7_get_certificates behaves like |PKCS7_get_raw_certificates| but parses -// them into |X509| objects. +// PKCS7_get_certificates behaves like `PKCS7_get_raw_certificates` but parses +// them into `X509` objects. OPENSSL_EXPORT int PKCS7_get_certificates(STACK_OF(X509) *out_certs, CBS *cbs); // PKCS7_bundle_raw_certificates appends a PKCS#7, SignedData structure -// containing |certs| to |out|. It returns one on success and zero on error. +// containing `certs` to `out`. It returns one on success and zero on error. // Note that certificates in SignedData structures are unordered. The order in -// |certs| will not be preserved. +// `certs` will not be preserved. OPENSSL_EXPORT int PKCS7_bundle_raw_certificates( CBB *out, const STACK_OF(CRYPTO_BUFFER) *certs); -// PKCS7_bundle_certificates behaves like |PKCS7_bundle_raw_certificates| but -// takes |X509| objects as input. -OPENSSL_EXPORT int PKCS7_bundle_certificates( - CBB *out, const STACK_OF(X509) *certs); +// PKCS7_bundle_certificates behaves like `PKCS7_bundle_raw_certificates` but +// takes `X509` objects as input. +OPENSSL_EXPORT int PKCS7_bundle_certificates(CBB *out, + const STACK_OF(X509) *certs); -// PKCS7_get_CRLs parses a PKCS#7, SignedData structure from |cbs| and appends -// the included CRLs to |out_crls|. It returns one on success and zero on error. -// |cbs| is advanced passed the structure. +// PKCS7_get_CRLs parses a PKCS#7, SignedData structure from `cbs` and appends +// the included CRLs to `out_crls`. It returns one on success and zero on error. +// `cbs` is advanced passed the structure. // // Note that a SignedData structure may contain no CRLs, in which case this // function succeeds but does not append any CRLs. Additionally, CRLs in // SignedData structures are unordered. Callers should not assume an order in -// |*out_crls| and may need to search for matches. +// `*out_crls` and may need to search for matches. OPENSSL_EXPORT int PKCS7_get_CRLs(STACK_OF(X509_CRL) *out_crls, CBS *cbs); // PKCS7_bundle_CRLs appends a PKCS#7, SignedData structure containing -// |crls| to |out|. It returns one on success and zero on error. Note that CRLs -// in SignedData structures are unordered. The order in |crls| will not be +// `crls` to `out`. It returns one on success and zero on error. Note that CRLs +// in SignedData structures are unordered. The order in `crls` will not be // preserved. OPENSSL_EXPORT int PKCS7_bundle_CRLs(CBB *out, const STACK_OF(X509_CRL) *crls); // PKCS7_get_PEM_certificates reads a PEM-encoded, PKCS#7, SignedData structure -// from |pem_bio| and appends the included certificates to |out_certs|. It +// from `pem_bio` and appends the included certificates to `out_certs`. It // returns one on success and zero on error. // // Note that a SignedData structure may contain no certificates, in which case // this function succeeds but does not append any certificates. Additionally, // certificates in SignedData structures are unordered. Callers should not -// assume a particular order in |*out_certs| and may need to search for matches +// assume a particular order in `*out_certs` and may need to search for matches // or run path-building algorithms. OPENSSL_EXPORT int PKCS7_get_PEM_certificates(STACK_OF(X509) *out_certs, BIO *pem_bio); // PKCS7_get_PEM_CRLs reads a PEM-encoded, PKCS#7, SignedData structure from -// |pem_bio| and appends the included CRLs to |out_crls|. It returns one on +// `pem_bio` and appends the included CRLs to `out_crls`. It returns one on // success and zero on error. // // Note that a SignedData structure may contain no CRLs, in which case this // function succeeds but does not append any CRLs. Additionally, CRLs in // SignedData structures are unordered. Callers should not assume an order in -// |*out_crls| and may need to search for matches. +// `*out_crls` and may need to search for matches. OPENSSL_EXPORT int PKCS7_get_PEM_CRLs(STACK_OF(X509_CRL) *out_crls, BIO *pem_bio); @@ -143,26 +143,25 @@ typedef struct { } PKCS7; // d2i_PKCS7 parses a BER-encoded, PKCS#7 signed data ContentInfo structure from -// |len| bytes at |*inp|, as described in |d2i_SAMPLE|. -OPENSSL_EXPORT PKCS7 *d2i_PKCS7(PKCS7 **out, const uint8_t **inp, - size_t len); +// `len` bytes at `*inp`, as described in `d2i_SAMPLE`. +OPENSSL_EXPORT PKCS7 *d2i_PKCS7(PKCS7 **out, const uint8_t **inp, size_t len); -// d2i_PKCS7_bio behaves like |d2i_PKCS7| but reads the input from |bio|. If -// the length of the object is indefinite the full contents of |bio| are read. +// d2i_PKCS7_bio behaves like `d2i_PKCS7` but reads the input from `bio`. If +// the length of the object is indefinite the full contents of `bio` are read. // // If the function fails then some unknown amount of data may have been read -// from |bio|. +// from `bio`. OPENSSL_EXPORT PKCS7 *d2i_PKCS7_bio(BIO *bio, PKCS7 **out); -// i2d_PKCS7 marshals |p7| as a DER-encoded PKCS#7 ContentInfo structure, as -// described in |i2d_SAMPLE|. +// i2d_PKCS7 marshals `p7` as a DER-encoded PKCS#7 ContentInfo structure, as +// described in `i2d_SAMPLE`. OPENSSL_EXPORT int i2d_PKCS7(const PKCS7 *p7, uint8_t **out); -// i2d_PKCS7_bio writes |p7| to |bio|. It returns one on success and zero on +// i2d_PKCS7_bio writes `p7` to `bio`. It returns one on success and zero on // error. OPENSSL_EXPORT int i2d_PKCS7_bio(BIO *bio, const PKCS7 *p7); -// PKCS7_free releases memory associated with |p7|. +// PKCS7_free releases memory associated with `p7`. OPENSSL_EXPORT void PKCS7_free(PKCS7 *p7); // PKCS7_type_is_data returns zero. @@ -186,7 +185,7 @@ OPENSSL_EXPORT int PKCS7_type_is_signedAndEnveloped(const PKCS7 *p7); // PKCS7_DETACHED indicates that the PKCS#7 file specifies its data externally. #define PKCS7_DETACHED 0x40 -// The following flags cause |PKCS7_sign| to fail. +// The following flags cause `PKCS7_sign` to fail. #define PKCS7_TEXT 0x1 #define PKCS7_NOCERTS 0x2 #define PKCS7_NOSIGS 0x4 @@ -201,22 +200,23 @@ OPENSSL_EXPORT int PKCS7_type_is_signedAndEnveloped(const PKCS7 *p7); // PKCS7_sign can operate in two modes to provide some backwards compatibility: // -// The first mode assembles |certs| into a PKCS#7 signed data ContentInfo with -// external data and no signatures. It returns a newly-allocated |PKCS7| on -// success or NULL on error. |sign_cert| and |pkey| must be NULL. |data| is -// ignored. |flags| must be equal to |PKCS7_DETACHED|. Additionally, -// certificates in SignedData structures are unordered. The order of |certs| +// The first mode assembles `certs` into a PKCS#7 signed data ContentInfo with +// external data and no signatures. It returns a newly-allocated `PKCS7` on +// success or NULL on error. `sign_cert` and `pkey` must be NULL. `data` is +// ignored. `flags` must be equal to `PKCS7_DETACHED`. Additionally, +// certificates in SignedData structures are unordered. The order of `certs` // will not be preserved. // -// The second mode generates a detached RSA SHA-256 signature of |data| using -// |pkey| and produces a PKCS#7 SignedData structure containing it. |certs| -// must be NULL and |flags| must be exactly |PKCS7_NOATTR | PKCS7_BINARY | -// PKCS7_NOCERTS | PKCS7_DETACHED|. +// The second mode generates a detached RSA SHA-256 signature of `data` using +// `pkey` and produces a PKCS#7 SignedData structure containing it. `certs` +// must be NULL and `flags` must be exactly +// `PKCS7_NOATTR | PKCS7_BINARY | PKCS7_NOCERTS | PKCS7_DETACHED`. // // Note this function only implements a subset of the corresponding OpenSSL // function. It is provided for backwards compatibility only. -OPENSSL_EXPORT PKCS7 *PKCS7_sign(X509 *sign_cert, EVP_PKEY *pkey, - STACK_OF(X509) *certs, BIO *data, int flags); +OPENSSL_EXPORT PKCS7 *PKCS7_sign(const X509 *sign_cert, const EVP_PKEY *pkey, + const STACK_OF(X509) *certs, BIO *data, + int flags); #if defined(__cplusplus) diff --git a/third_party/boringssl/src/include/openssl/pkcs8.h b/third_party/boringssl/src/include/openssl/pkcs8.h index a7052089a..2ced28599 100644 --- a/third_party/boringssl/src/include/openssl/pkcs8.h +++ b/third_party/boringssl/src/include/openssl/pkcs8.h @@ -34,18 +34,18 @@ extern "C" { // PKCS8_encrypt serializes and encrypts a PKCS8_PRIV_KEY_INFO with PBES1 or // PBES2 as defined in PKCS #5. Only pbeWithSHAAnd128BitRC4, // pbeWithSHAAnd3-KeyTripleDES-CBC and pbeWithSHA1And40BitRC2, defined in PKCS -// #12, and PBES2, are supported. PBES2 is selected by setting |cipher| and -// passing -1 for |pbe_nid|. Otherwise, PBES1 is used and |cipher| is ignored. +// #12, and PBES2, are supported. PBES2 is selected by setting `cipher` and +// passing -1 for `pbe_nid`. Otherwise, PBES1 is used and `cipher` is ignored. // -// |pass| is used as the password. If a PBES1 scheme from PKCS #12 is used, this +// `pass` is used as the password. If a PBES1 scheme from PKCS #12 is used, this // will be converted to a raw byte string as specified in B.1 of PKCS #12. If -// |pass| is NULL, it will be encoded as the empty byte string rather than two +// `pass` is NULL, it will be encoded as the empty byte string rather than two // zero bytes, the PKCS #12 encoding of the empty string. // -// If |salt| is NULL, a random salt of |salt_len| bytes is generated. If -// |salt_len| is zero, a default salt length is used instead. +// If `salt` is NULL, a random salt of `salt_len` bytes is generated. If +// `salt_len` is zero, a default salt length is used instead. // -// The resulting structure is stored in an |X509_SIG| which must be freed by the +// The resulting structure is stored in an `X509_SIG` which must be freed by the // caller. OPENSSL_EXPORT X509_SIG *PKCS8_encrypt(int pbe_nid, const EVP_CIPHER *cipher, const char *pass, int pass_len, @@ -53,8 +53,8 @@ OPENSSL_EXPORT X509_SIG *PKCS8_encrypt(int pbe_nid, const EVP_CIPHER *cipher, int iterations, PKCS8_PRIV_KEY_INFO *p8inf); -// PKCS8_marshal_encrypted_private_key behaves like |PKCS8_encrypt| but encrypts -// an |EVP_PKEY| and writes the serialized EncryptedPrivateKeyInfo to |out|. It +// PKCS8_marshal_encrypted_private_key behaves like `PKCS8_encrypt` but encrypts +// an `EVP_PKEY` and writes the serialized EncryptedPrivateKeyInfo to `out`. It // returns one on success and zero on error. OPENSSL_EXPORT int PKCS8_marshal_encrypted_private_key( CBB *out, int pbe_nid, const EVP_CIPHER *cipher, const char *pass, @@ -66,9 +66,9 @@ OPENSSL_EXPORT int PKCS8_marshal_encrypted_private_key( // pbeWithSHAAnd3-KeyTripleDES-CBC and pbeWithSHA1And40BitRC2, and PBES2, // defined in PKCS #12, are supported. // -// |pass| is used as the password. If a PBES1 scheme from PKCS #12 is used, this +// `pass` is used as the password. If a PBES1 scheme from PKCS #12 is used, this // will be converted to a raw byte string as specified in B.1 of PKCS #12. If -// |pass| is NULL, it will be encoded as the empty byte string rather than two +// `pass` is NULL, it will be encoded as the empty byte string rather than two // zero bytes, the PKCS #12 encoding of the empty string. // // The resulting structure must be freed by the caller. @@ -76,19 +76,19 @@ OPENSSL_EXPORT PKCS8_PRIV_KEY_INFO *PKCS8_decrypt(X509_SIG *pkcs8, const char *pass, int pass_len); -// PKCS8_parse_encrypted_private_key behaves like |PKCS8_decrypt| but it parses -// the EncryptedPrivateKeyInfo structure from |cbs| and advances |cbs|. It -// returns a newly-allocated |EVP_PKEY| on success and zero on error. +// PKCS8_parse_encrypted_private_key behaves like `PKCS8_decrypt` but it parses +// the EncryptedPrivateKeyInfo structure from `cbs` and advances `cbs`. It +// returns a newly-allocated `EVP_PKEY` on success and zero on error. OPENSSL_EXPORT EVP_PKEY *PKCS8_parse_encrypted_private_key(CBS *cbs, const char *pass, size_t pass_len); -// PKCS12_get_key_and_certs parses a PKCS#12 structure from |in|, authenticates -// and decrypts it using |password|, sets |*out_key| to the included private -// key and appends the included certificates to |out_certs|. It returns one on +// PKCS12_get_key_and_certs parses a PKCS#12 structure from `in`, authenticates +// and decrypts it using `password`, sets `*out_key` to the included private +// key and appends the included certificates to `out_certs`. It returns one on // success and zero on error. The caller takes ownership of the outputs. // Any friendlyName attributes (RFC 2985) in the PKCS#12 structure will be -// returned on the |X509| objects as aliases. See also |X509_alias_get0|. +// returned on the `X509` objects as aliases. See also `X509_alias_get0`. OPENSSL_EXPORT int PKCS12_get_key_and_certs(EVP_PKEY **out_key, STACK_OF(X509) *out_certs, CBS *in, const char *password); @@ -99,95 +99,95 @@ OPENSSL_EXPORT int PKCS12_get_key_and_certs(EVP_PKEY **out_key, // PKCS12_PBE_add does nothing. It exists for compatibility with OpenSSL. OPENSSL_EXPORT void PKCS12_PBE_add(void); -// d2i_PKCS12 is a dummy function that copies |*ber_bytes| into a -// |PKCS12| structure. The |out_p12| argument should be NULL(✝). On exit, -// |*ber_bytes| will be advanced by |ber_len|. It returns a fresh |PKCS12| +// d2i_PKCS12 is a dummy function that copies `*ber_bytes` into a +// `PKCS12` structure. The `out_p12` argument should be NULL(✝). On exit, +// `*ber_bytes` will be advanced by `ber_len`. It returns a fresh `PKCS12` // structure or NULL on error. // -// Note: unlike other d2i functions, |d2i_PKCS12| will always consume |ber_len| +// Note: unlike other d2i functions, `d2i_PKCS12` will always consume `ber_len` // bytes. // -// (✝) If |out_p12| is not NULL and the function is successful, |*out_p12| will -// be freed if not NULL itself and the result will be written to |*out_p12|. +// (✝) If `out_p12` is not NULL and the function is successful, `*out_p12` will +// be freed if not NULL itself and the result will be written to `*out_p12`. // New code should not depend on this. OPENSSL_EXPORT PKCS12 *d2i_PKCS12(PKCS12 **out_p12, const uint8_t **ber_bytes, size_t ber_len); -// d2i_PKCS12_bio acts like |d2i_PKCS12| but reads from a |BIO|. +// d2i_PKCS12_bio acts like `d2i_PKCS12` but reads from a `BIO`. OPENSSL_EXPORT PKCS12* d2i_PKCS12_bio(BIO *bio, PKCS12 **out_p12); -// d2i_PKCS12_fp acts like |d2i_PKCS12| but reads from a |FILE|. +// d2i_PKCS12_fp acts like `d2i_PKCS12` but reads from a `FILE`. OPENSSL_EXPORT PKCS12* d2i_PKCS12_fp(FILE *fp, PKCS12 **out_p12); -// i2d_PKCS12 is a dummy function which copies the contents of |p12|. If |out| -// is not NULL then the result is written to |*out| and |*out| is advanced just +// i2d_PKCS12 is a dummy function which copies the contents of `p12`. If `out` +// is not NULL then the result is written to `*out` and `*out` is advanced just // past the output. It returns the number of bytes in the result, whether // written or not, or a negative value on error. OPENSSL_EXPORT int i2d_PKCS12(const PKCS12 *p12, uint8_t **out); -// i2d_PKCS12_bio writes the contents of |p12| to |bio|. It returns one on +// i2d_PKCS12_bio writes the contents of `p12` to `bio`. It returns one on // success and zero on error. OPENSSL_EXPORT int i2d_PKCS12_bio(BIO *bio, const PKCS12 *p12); -// i2d_PKCS12_fp writes the contents of |p12| to |fp|. It returns one on +// i2d_PKCS12_fp writes the contents of `p12` to `fp`. It returns one on // success and zero on error. OPENSSL_EXPORT int i2d_PKCS12_fp(FILE *fp, const PKCS12 *p12); -// PKCS12_parse calls |PKCS12_get_key_and_certs| on the ASN.1 data stored in -// |p12|. The |out_pkey| and |out_cert| arguments must not be NULL and, on +// PKCS12_parse calls `PKCS12_get_key_and_certs` on the ASN.1 data stored in +// `p12`. The `out_pkey` and `out_cert` arguments must not be NULL and, on // successful exit, the private key and matching certificate will be stored in -// them. The |out_ca_certs| argument may be NULL but, if not, then any extra -// certificates will be appended to |*out_ca_certs|. If |*out_ca_certs| is NULL +// them. The `out_ca_certs` argument may be NULL but, if not, then any extra +// certificates will be appended to `*out_ca_certs`. If `*out_ca_certs` is NULL // then it will be set to a freshly allocated stack containing the extra certs. // -// Note if |p12| does not contain a private key, both |*out_pkey| and -// |*out_cert| will be set to NULL and all certificates will be returned via -// |*out_ca_certs|. Also note this function differs from OpenSSL in that extra +// Note if `p12` does not contain a private key, both `*out_pkey` and +// `*out_cert` will be set to NULL and all certificates will be returned via +// `*out_ca_certs`. Also note this function differs from OpenSSL in that extra // certificates are returned in the order they appear in the file. OpenSSL 1.1.1 // returns them in reverse order, but this will be fixed in OpenSSL 3.0. // // It returns one on success and zero on error. // -// Use |PKCS12_get_key_and_certs| instead. +// Use `PKCS12_get_key_and_certs` instead. OPENSSL_EXPORT int PKCS12_parse(const PKCS12 *p12, const char *password, EVP_PKEY **out_pkey, X509 **out_cert, STACK_OF(X509) **out_ca_certs); -// PKCS12_verify_mac returns one if |password| is a valid password for |p12| -// and zero otherwise. Since |PKCS12_parse| doesn't take a length parameter, +// PKCS12_verify_mac returns one if `password` is a valid password for `p12` +// and zero otherwise. Since `PKCS12_parse` doesn't take a length parameter, // it's not actually possible to use a non-NUL-terminated password to actually -// get anything from a |PKCS12|. Thus |password| and |password_len| may be -// |NULL| and zero, respectively, or else |password_len| may be -1, or else -// |password[password_len]| must be zero and no other NUL bytes may appear in -// |password|. If the |password_len| checks fail, zero is returned +// get anything from a `PKCS12`. Thus `password` and `password_len` may be +// `NULL` and zero, respectively, or else `password_len` may be -1, or else +// `password[password_len]` must be zero and no other NUL bytes may appear in +// `password`. If the `password_len` checks fail, zero is returned // immediately. OPENSSL_EXPORT int PKCS12_verify_mac(const PKCS12 *p12, const char *password, int password_len); // PKCS12_DEFAULT_ITER is the default number of KDF iterations used when -// creating a |PKCS12| object. +// creating a `PKCS12` object. #define PKCS12_DEFAULT_ITER 2048 -// PKCS12_create returns a newly-allocated |PKCS12| object containing |pkey|, -// |cert|, and |chain|, encrypted with the specified password. |name|, if not +// PKCS12_create returns a newly-allocated `PKCS12` object containing `pkey`, +// `cert`, and `chain`, encrypted with the specified password. `name`, if not // NULL, specifies a user-friendly name to encode with the key and -// certificate. The key and certificates are encrypted with |key_nid| and -// |cert_nid|, respectively, using |iterations| iterations in the -// KDF. |mac_iterations| is the number of iterations when deriving the MAC -// key. |key_type| must be zero. |pkey| and |cert| may be NULL to omit them. +// certificate. The key and certificates are encrypted with `key_nid` and +// `cert_nid`, respectively, using `iterations` iterations in the +// KDF. `mac_iterations` is the number of iterations when deriving the MAC +// key. `key_type` must be zero. `pkey` and `cert` may be NULL to omit them. // -// Each of |key_nid|, |cert_nid|, |iterations|, and |mac_iterations| may be zero -// to use defaults, which are |NID_aes_256_cbc|, |NID_aes_256_cbc|, -// |PKCS12_DEFAULT_ITER|, and |PKCS12_DEFAULT_ITER|, respectively. +// Each of `key_nid`, `cert_nid`, `iterations`, and `mac_iterations` may be zero +// to use defaults, which are `NID_aes_256_cbc`, `NID_aes_256_cbc`, +// `PKCS12_DEFAULT_ITER`, and `PKCS12_DEFAULT_ITER`, respectively. // -// |key_nid| and |cert_nid| are then interpreted as follows: +// `key_nid` and `cert_nid` are then interpreted as follows: // // * If the NID is a cipher that is supported with PBES2, e.g. -// |NID_aes_256_cbc|, this function will use it with PBES2 and a default KDF +// `NID_aes_256_cbc`, this function will use it with PBES2 and a default KDF // (currently PBKDF2 with HMAC-SHA256). There is no way to specify the KDF in // this function. // -// * If the NID is a PBES1 suite, e.g. |NID_pbe_WithSHA1And3_Key_TripleDES_CBC|, +// * If the NID is a PBES1 suite, e.g. `NID_pbe_WithSHA1And3_Key_TripleDES_CBC`, // this function will use the specified suite. // // * If the NID is -1, this function will disable encryption for the key or @@ -197,19 +197,19 @@ OPENSSL_EXPORT int PKCS12_verify_mac(const PKCS12 *p12, const char *password, // supported and may not open in other implementations. // // WARNING: This differs from other functions in this module, which use a pair -// of NID and |EVP_CIPHER| parameters to pick between PBES1 and PBES2 schemes. +// of NID and `EVP_CIPHER` parameters to pick between PBES1 and PBES2 schemes. // -// If |cert| or |chain| have associated aliases (see |X509_alias_set1|), they +// If `cert` or `chain` have associated aliases (see `X509_alias_set1`), they // will be included in the output as friendlyName attributes (RFC 2985). It is -// an error to specify both an alias on |cert| and a non-NULL |name| +// an error to specify both an alias on `cert` and a non-NULL `name` // parameter. OPENSSL_EXPORT PKCS12 *PKCS12_create(const char *password, const char *name, - const EVP_PKEY *pkey, X509 *cert, + const EVP_PKEY *pkey, const X509 *cert, const STACK_OF(X509) *chain, int key_nid, int cert_nid, int iterations, int mac_iterations, int key_type); -// PKCS12_free frees |p12| and its contents. +// PKCS12_free frees `p12` and its contents. OPENSSL_EXPORT void PKCS12_free(PKCS12 *p12); diff --git a/third_party/boringssl/src/include/openssl/poly1305.h b/third_party/boringssl/src/include/openssl/poly1305.h index ac0865f8c..d60b75310 100644 --- a/third_party/boringssl/src/include/openssl/poly1305.h +++ b/third_party/boringssl/src/include/openssl/poly1305.h @@ -27,20 +27,20 @@ extern "C" { typedef uint8_t poly1305_state[512]; -// CRYPTO_poly1305_init sets up |state| so that it can be used to calculate an -// authentication tag with the one-time key |key|. Note that |key| is a +// CRYPTO_poly1305_init sets up `state` so that it can be used to calculate an +// authentication tag with the one-time key `key`. Note that `key` is a // one-time key and therefore there is no `reset' method because that would // enable several messages to be authenticated with the same key. OPENSSL_EXPORT void CRYPTO_poly1305_init(poly1305_state *state, const uint8_t key[32]); -// CRYPTO_poly1305_update processes |in_len| bytes from |in|. It can be called +// CRYPTO_poly1305_update processes `in_len` bytes from `in`. It can be called // zero or more times after poly1305_init. OPENSSL_EXPORT void CRYPTO_poly1305_update(poly1305_state *state, const uint8_t *in, size_t in_len); // CRYPTO_poly1305_finish completes the poly1305 calculation and writes a 16 -// byte authentication tag to |mac|. +// byte authentication tag to `mac`. OPENSSL_EXPORT void CRYPTO_poly1305_finish(poly1305_state *state, uint8_t mac[16]); diff --git a/third_party/boringssl/src/include/openssl/pool.h b/third_party/boringssl/src/include/openssl/pool.h index 461504789..2c3855b43 100644 --- a/third_party/boringssl/src/include/openssl/pool.h +++ b/third_party/boringssl/src/include/openssl/pool.h @@ -26,93 +26,99 @@ extern "C" { // Buffers and buffer pools. // -// |CRYPTO_BUFFER|s are simply reference-counted, immutable byte string. A -// single |CRYPTO_BUFFER| can be referenced from multiple parts of an +// `CRYPTO_BUFFER`s are simply reference-counted, immutable byte string. A +// single `CRYPTO_BUFFER` can be referenced from multiple parts of an // application without storing multiple copies of the underlying data in // memory. // -// A |CRYPTO_BUFFER_POOL| can be used to additionally deduplicate -// |CRYPTO_BUFFER|s on construction. It maintains weak references to associated -// |CRYPTO_BUFFER|s and returns an existing |CRYPTO_BUFFER| if matching ones +// A `CRYPTO_BUFFER_POOL` can be used to additionally deduplicate +// `CRYPTO_BUFFER`s on construction. It maintains weak references to associated +// `CRYPTO_BUFFER`s and returns an existing `CRYPTO_BUFFER` if matching ones // already exist. // -// Without a |CRYPTO_BUFFER_POOL|, if two parts of application construct an -// identical |CRYPTO_BUFFER| (e.g. if two TLS connections received the same +// Without a `CRYPTO_BUFFER_POOL`, if two parts of application construct an +// identical `CRYPTO_BUFFER` (e.g. if two TLS connections received the same // certificate), there will be two copies of the buffer in memory. A -// |CRYPTO_BUFFER_POOL| allows two parts of an application that construct -// |CRYPTO_BUFFER|s to deduplicate their contents, at the cost of more thread +// `CRYPTO_BUFFER_POOL` allows two parts of an application that construct +// `CRYPTO_BUFFER`s to deduplicate their contents, at the cost of more thread // contention. DEFINE_STACK_OF(CRYPTO_BUFFER) -// CRYPTO_BUFFER_POOL_new returns a freshly allocated |CRYPTO_BUFFER_POOL| or +// CRYPTO_BUFFER_POOL_new returns a freshly allocated `CRYPTO_BUFFER_POOL` or // NULL on error. OPENSSL_EXPORT CRYPTO_BUFFER_POOL *CRYPTO_BUFFER_POOL_new(void); -// CRYPTO_BUFFER_POOL_free decrements the reference count of |pool| and frees it +// CRYPTO_BUFFER_POOL_free decrements the reference count of `pool` and frees it // if the reference count drops to zero. OPENSSL_EXPORT void CRYPTO_BUFFER_POOL_free(CRYPTO_BUFFER_POOL *pool); -// CRYPTO_BUFFER_POOL_up_ref increments the reference count of |pool| and -// returns one. It does not mutate |pool| for thread-safety purposes and may be +// CRYPTO_BUFFER_POOL_up_ref increments the reference count of `pool` and +// returns one. It does not mutate `pool` for thread-safety purposes and may be // used concurrently. OPENSSL_EXPORT int CRYPTO_BUFFER_POOL_up_ref(CRYPTO_BUFFER_POOL *pool); -// CRYPTO_BUFFER_new returns a |CRYPTO_BUFFER| containing a copy of |data|, or -// else NULL on error. If |pool| is not NULL then the returned value may be a -// reference to a previously existing |CRYPTO_BUFFER| that contained the same -// data. Otherwise, the returned, fresh |CRYPTO_BUFFER| will be added to the +// CRYPTO_BUFFER_POOL_dup_ref increments the reference count of `pool` and +// returns the same handle. It does not mutate `pool` for thread-safety purposes +// and may be used concurrently. +OPENSSL_EXPORT CRYPTO_BUFFER_POOL *CRYPTO_BUFFER_POOL_dup_ref( + const CRYPTO_BUFFER_POOL *pool); + +// CRYPTO_BUFFER_new returns a `CRYPTO_BUFFER` containing a copy of `data`, or +// else NULL on error. If `pool` is not NULL then the returned value may be a +// reference to a previously existing `CRYPTO_BUFFER` that contained the same +// data. Otherwise, the returned, fresh `CRYPTO_BUFFER` will be added to the // pool. // -// There is no requirement that |pool| outlive the |CRYPTO_BUFFER|, or vice -// versa. If the |CRYPTO_BUFFER| is released first, it will be removed from -// |pool|. If |pool| is released first, the |CRYPTO_BUFFER| remains valid. +// There is no requirement that `pool` outlive the `CRYPTO_BUFFER`, or vice +// versa. If the `CRYPTO_BUFFER` is released first, it will be removed from +// `pool`. If `pool` is released first, the `CRYPTO_BUFFER` remains valid. OPENSSL_EXPORT CRYPTO_BUFFER *CRYPTO_BUFFER_new(const uint8_t *data, size_t len, CRYPTO_BUFFER_POOL *pool); -// CRYPTO_BUFFER_alloc creates an unpooled |CRYPTO_BUFFER| of the given size and -// writes the underlying data pointer to |*out_data|. It returns NULL on error. +// CRYPTO_BUFFER_alloc creates an unpooled `CRYPTO_BUFFER` of the given size and +// writes the underlying data pointer to `*out_data`. It returns NULL on error. // -// After calling this function, |len| bytes of contents must be written to -// |out_data| before passing the returned pointer to any other BoringSSL -// functions. Once initialized, the |CRYPTO_BUFFER| should be treated as +// After calling this function, `len` bytes of contents must be written to +// `out_data` before passing the returned pointer to any other BoringSSL +// functions. Once initialized, the `CRYPTO_BUFFER` should be treated as // immutable. OPENSSL_EXPORT CRYPTO_BUFFER *CRYPTO_BUFFER_alloc(uint8_t **out_data, size_t len); -// CRYPTO_BUFFER_new_from_CBS acts the same as |CRYPTO_BUFFER_new|. +// CRYPTO_BUFFER_new_from_CBS acts the same as `CRYPTO_BUFFER_new`. OPENSSL_EXPORT CRYPTO_BUFFER *CRYPTO_BUFFER_new_from_CBS( const CBS *cbs, CRYPTO_BUFFER_POOL *pool); -// CRYPTO_BUFFER_new_from_static_data_unsafe behaves like |CRYPTO_BUFFER_new| -// but does not copy |data|. |data| must be immutable and last for the lifetime +// CRYPTO_BUFFER_new_from_static_data_unsafe behaves like `CRYPTO_BUFFER_new` +// but does not copy `data`. `data` must be immutable and last for the lifetime // of the address space. OPENSSL_EXPORT CRYPTO_BUFFER *CRYPTO_BUFFER_new_from_static_data_unsafe( const uint8_t *data, size_t len, CRYPTO_BUFFER_POOL *pool); -// CRYPTO_BUFFER_free decrements the reference count of |buf|. If there are no +// CRYPTO_BUFFER_free decrements the reference count of `buf`. If there are no // other references, or if the only remaining reference is from a pool, then -// |buf| will be freed. +// `buf` will be freed. OPENSSL_EXPORT void CRYPTO_BUFFER_free(CRYPTO_BUFFER *buf); -// CRYPTO_BUFFER_up_ref increments the reference count of |buf| and returns +// CRYPTO_BUFFER_up_ref increments the reference count of `buf` and returns // one. OPENSSL_EXPORT int CRYPTO_BUFFER_up_ref(CRYPTO_BUFFER *buf); -// CRYPTO_BUFFER_dup_ref increments the reference count of |buf| and returns -// |buf|. The caller must call |CRYPTO_BUFFER_free| on the result to release the +// CRYPTO_BUFFER_dup_ref increments the reference count of `buf` and returns +// `buf`. The caller must call `CRYPTO_BUFFER_free` on the result to release the // reference. OPENSSL_EXPORT CRYPTO_BUFFER *CRYPTO_BUFFER_dup_ref(const CRYPTO_BUFFER *buf); -// CRYPTO_BUFFER_data returns a pointer to the data contained in |buf|. +// CRYPTO_BUFFER_data returns a pointer to the data contained in `buf`. OPENSSL_EXPORT const uint8_t *CRYPTO_BUFFER_data(const CRYPTO_BUFFER *buf); // CRYPTO_BUFFER_len returns the length, in bytes, of the data contained in -// |buf|. +// `buf`. OPENSSL_EXPORT size_t CRYPTO_BUFFER_len(const CRYPTO_BUFFER *buf); -// CRYPTO_BUFFER_init_CBS initialises |out| to point at the data from |buf|. +// CRYPTO_BUFFER_init_CBS initialises `out` to point at the data from `buf`. OPENSSL_EXPORT void CRYPTO_BUFFER_init_CBS(const CRYPTO_BUFFER *buf, CBS *out); @@ -124,9 +130,9 @@ extern "C++" { BSSL_NAMESPACE_BEGIN BORINGSSL_MAKE_DELETER(CRYPTO_BUFFER_POOL, CRYPTO_BUFFER_POOL_free) -BORINGSSL_MAKE_UP_REF(CRYPTO_BUFFER_POOL, CRYPTO_BUFFER_POOL_up_ref) +BORINGSSL_MAKE_UP_REF(CRYPTO_BUFFER_POOL, CRYPTO_BUFFER_POOL_dup_ref) BORINGSSL_MAKE_DELETER(CRYPTO_BUFFER, CRYPTO_BUFFER_free) -BORINGSSL_MAKE_UP_REF(CRYPTO_BUFFER, CRYPTO_BUFFER_up_ref) +BORINGSSL_MAKE_UP_REF(CRYPTO_BUFFER, CRYPTO_BUFFER_dup_ref) BSSL_NAMESPACE_END diff --git a/third_party/boringssl/src/include/openssl/posix_time.h b/third_party/boringssl/src/include/openssl/posix_time.h index b9e0a77ea..0a4b6a5c3 100644 --- a/third_party/boringssl/src/include/openssl/posix_time.h +++ b/third_party/boringssl/src/include/openssl/posix_time.h @@ -15,7 +15,7 @@ #ifndef OPENSSL_HEADER_POSIX_TIME_H #define OPENSSL_HEADER_POSIX_TIME_H -#include // IWYU pragma: export +#include // IWYU pragma: export #include @@ -27,18 +27,18 @@ extern "C" { // Time functions. -// OPENSSL_posix_to_tm converts a int64_t POSIX time value in |time|, which must -// be in the range of year 0000 to 9999, to a broken out time value in |tm|. It -// returns one on success and zero on error. +// OPENSSL_posix_to_tm converts an int64_t POSIX time value in `time`, which +// must be in the range of year 0000 to 9999, to a broken out time value in +// `tm`. It returns one on success and zero on error. OPENSSL_EXPORT int OPENSSL_posix_to_tm(int64_t time, struct tm *out_tm); // OPENSSL_tm_to_posix converts a time value between the years 0 and 9999 in -// |tm| to a POSIX time value in |out|. One is returned on success, zero is -// returned on failure. It is a failure if |tm| contains out of range values. +// `tm` to a POSIX time value in `out`. One is returned on success, zero is +// returned on failure. It is a failure if `tm` contains out of range values. OPENSSL_EXPORT int OPENSSL_tm_to_posix(const struct tm *tm, int64_t *out); -// OPENSSL_timegm converts a time value between the years 0 and 9999 in |tm| to -// a time_t value in |out|. One is returned on success, zero is returned on +// OPENSSL_timegm converts a time value between the years 0 and 9999 in `tm` to +// a time_t value in `out`. One is returned on success, zero is returned on // failure. It is a failure if the converted time can not be represented in a // time_t, or if the tm contains out of range values. OPENSSL_EXPORT int OPENSSL_timegm(const struct tm *tm, time_t *out); diff --git a/third_party/boringssl/src/include/openssl/prefix_symbols.h b/third_party/boringssl/src/include/openssl/prefix_symbols.h index c25242da8..2778fd68f 100644 --- a/third_party/boringssl/src/include/openssl/prefix_symbols.h +++ b/third_party/boringssl/src/include/openssl/prefix_symbols.h @@ -130,6 +130,7 @@ #pragma redefine_extname ASN1_STRING_free BORINGSSL_ADD_USER_LABEL_AND_PREFIX(ASN1_STRING_free) #pragma redefine_extname ASN1_STRING_get0_data BORINGSSL_ADD_USER_LABEL_AND_PREFIX(ASN1_STRING_get0_data) #pragma redefine_extname ASN1_STRING_get_default_mask BORINGSSL_ADD_USER_LABEL_AND_PREFIX(ASN1_STRING_get_default_mask) +#pragma redefine_extname ASN1_STRING_get_length BORINGSSL_ADD_USER_LABEL_AND_PREFIX(ASN1_STRING_get_length) #pragma redefine_extname ASN1_STRING_length BORINGSSL_ADD_USER_LABEL_AND_PREFIX(ASN1_STRING_length) #pragma redefine_extname ASN1_STRING_new BORINGSSL_ADD_USER_LABEL_AND_PREFIX(ASN1_STRING_new) #pragma redefine_extname ASN1_STRING_print BORINGSSL_ADD_USER_LABEL_AND_PREFIX(ASN1_STRING_print) @@ -232,6 +233,7 @@ #pragma redefine_extname BIO_ctrl_get_write_guarantee BORINGSSL_ADD_USER_LABEL_AND_PREFIX(BIO_ctrl_get_write_guarantee) #pragma redefine_extname BIO_ctrl_pending BORINGSSL_ADD_USER_LABEL_AND_PREFIX(BIO_ctrl_pending) #pragma redefine_extname BIO_do_connect BORINGSSL_ADD_USER_LABEL_AND_PREFIX(BIO_do_connect) +#pragma redefine_extname BIO_dup_ref BORINGSSL_ADD_USER_LABEL_AND_PREFIX(BIO_dup_ref) #pragma redefine_extname BIO_eof BORINGSSL_ADD_USER_LABEL_AND_PREFIX(BIO_eof) #pragma redefine_extname BIO_f_base64 BORINGSSL_ADD_USER_LABEL_AND_PREFIX(BIO_f_base64) #pragma redefine_extname BIO_f_ssl BORINGSSL_ADD_USER_LABEL_AND_PREFIX(BIO_f_ssl) @@ -274,6 +276,7 @@ #pragma redefine_extname BIO_meth_set_puts BORINGSSL_ADD_USER_LABEL_AND_PREFIX(BIO_meth_set_puts) #pragma redefine_extname BIO_meth_set_read BORINGSSL_ADD_USER_LABEL_AND_PREFIX(BIO_meth_set_read) #pragma redefine_extname BIO_meth_set_write BORINGSSL_ADD_USER_LABEL_AND_PREFIX(BIO_meth_set_write) +#pragma redefine_extname BIO_meth_set_write_ex BORINGSSL_ADD_USER_LABEL_AND_PREFIX(BIO_meth_set_write_ex) #pragma redefine_extname BIO_method_type BORINGSSL_ADD_USER_LABEL_AND_PREFIX(BIO_method_type) #pragma redefine_extname BIO_new BORINGSSL_ADD_USER_LABEL_AND_PREFIX(BIO_new) #pragma redefine_extname BIO_new_bio_pair BORINGSSL_ADD_USER_LABEL_AND_PREFIX(BIO_new_bio_pair) @@ -337,6 +340,7 @@ #pragma redefine_extname BIO_wpending BORINGSSL_ADD_USER_LABEL_AND_PREFIX(BIO_wpending) #pragma redefine_extname BIO_write BORINGSSL_ADD_USER_LABEL_AND_PREFIX(BIO_write) #pragma redefine_extname BIO_write_all BORINGSSL_ADD_USER_LABEL_AND_PREFIX(BIO_write_all) +#pragma redefine_extname BIO_write_ex BORINGSSL_ADD_USER_LABEL_AND_PREFIX(BIO_write_ex) #pragma redefine_extname BIO_write_filename BORINGSSL_ADD_USER_LABEL_AND_PREFIX(BIO_write_filename) #pragma redefine_extname BLAKE2B256 BORINGSSL_ADD_USER_LABEL_AND_PREFIX(BLAKE2B256) #pragma redefine_extname BLAKE2B256_Final BORINGSSL_ADD_USER_LABEL_AND_PREFIX(BLAKE2B256_Final) @@ -504,6 +508,7 @@ #pragma redefine_extname CBB_add_asn1_octet_string BORINGSSL_ADD_USER_LABEL_AND_PREFIX(CBB_add_asn1_octet_string) #pragma redefine_extname CBB_add_asn1_oid_component BORINGSSL_ADD_USER_LABEL_AND_PREFIX(CBB_add_asn1_oid_component) #pragma redefine_extname CBB_add_asn1_oid_from_text BORINGSSL_ADD_USER_LABEL_AND_PREFIX(CBB_add_asn1_oid_from_text) +#pragma redefine_extname CBB_add_asn1_relative_oid_from_der_to_text BORINGSSL_ADD_USER_LABEL_AND_PREFIX(CBB_add_asn1_relative_oid_from_der_to_text) #pragma redefine_extname CBB_add_asn1_relative_oid_from_text BORINGSSL_ADD_USER_LABEL_AND_PREFIX(CBB_add_asn1_relative_oid_from_text) #pragma redefine_extname CBB_add_asn1_uint64 BORINGSSL_ADD_USER_LABEL_AND_PREFIX(CBB_add_asn1_uint64) #pragma redefine_extname CBB_add_asn1_uint64_with_tag BORINGSSL_ADD_USER_LABEL_AND_PREFIX(CBB_add_asn1_uint64_with_tag) @@ -517,6 +522,7 @@ #pragma redefine_extname CBB_add_u24_length_prefixed BORINGSSL_ADD_USER_LABEL_AND_PREFIX(CBB_add_u24_length_prefixed) #pragma redefine_extname CBB_add_u32 BORINGSSL_ADD_USER_LABEL_AND_PREFIX(CBB_add_u32) #pragma redefine_extname CBB_add_u32le BORINGSSL_ADD_USER_LABEL_AND_PREFIX(CBB_add_u32le) +#pragma redefine_extname CBB_add_u48 BORINGSSL_ADD_USER_LABEL_AND_PREFIX(CBB_add_u48) #pragma redefine_extname CBB_add_u64 BORINGSSL_ADD_USER_LABEL_AND_PREFIX(CBB_add_u64) #pragma redefine_extname CBB_add_u64le BORINGSSL_ADD_USER_LABEL_AND_PREFIX(CBB_add_u64le) #pragma redefine_extname CBB_add_u8 BORINGSSL_ADD_USER_LABEL_AND_PREFIX(CBB_add_u8) @@ -552,6 +558,7 @@ #pragma redefine_extname CBS_get_asn1_element BORINGSSL_ADD_USER_LABEL_AND_PREFIX(CBS_get_asn1_element) #pragma redefine_extname CBS_get_asn1_int64 BORINGSSL_ADD_USER_LABEL_AND_PREFIX(CBS_get_asn1_int64) #pragma redefine_extname CBS_get_asn1_int64_with_tag BORINGSSL_ADD_USER_LABEL_AND_PREFIX(CBS_get_asn1_int64_with_tag) +#pragma redefine_extname CBS_get_asn1_oid_component BORINGSSL_ADD_USER_LABEL_AND_PREFIX(CBS_get_asn1_oid_component) #pragma redefine_extname CBS_get_asn1_uint64 BORINGSSL_ADD_USER_LABEL_AND_PREFIX(CBS_get_asn1_uint64) #pragma redefine_extname CBS_get_asn1_uint64_with_tag BORINGSSL_ADD_USER_LABEL_AND_PREFIX(CBS_get_asn1_uint64_with_tag) #pragma redefine_extname CBS_get_bytes BORINGSSL_ADD_USER_LABEL_AND_PREFIX(CBS_get_bytes) @@ -568,6 +575,7 @@ #pragma redefine_extname CBS_get_u24_length_prefixed BORINGSSL_ADD_USER_LABEL_AND_PREFIX(CBS_get_u24_length_prefixed) #pragma redefine_extname CBS_get_u32 BORINGSSL_ADD_USER_LABEL_AND_PREFIX(CBS_get_u32) #pragma redefine_extname CBS_get_u32le BORINGSSL_ADD_USER_LABEL_AND_PREFIX(CBS_get_u32le) +#pragma redefine_extname CBS_get_u48 BORINGSSL_ADD_USER_LABEL_AND_PREFIX(CBS_get_u48) #pragma redefine_extname CBS_get_u64 BORINGSSL_ADD_USER_LABEL_AND_PREFIX(CBS_get_u64) #pragma redefine_extname CBS_get_u64_decimal BORINGSSL_ADD_USER_LABEL_AND_PREFIX(CBS_get_u64_decimal) #pragma redefine_extname CBS_get_u64le BORINGSSL_ADD_USER_LABEL_AND_PREFIX(CBS_get_u64le) @@ -584,9 +592,11 @@ #pragma redefine_extname CBS_is_valid_asn1_integer BORINGSSL_ADD_USER_LABEL_AND_PREFIX(CBS_is_valid_asn1_integer) #pragma redefine_extname CBS_is_valid_asn1_oid BORINGSSL_ADD_USER_LABEL_AND_PREFIX(CBS_is_valid_asn1_oid) #pragma redefine_extname CBS_is_valid_asn1_relative_oid BORINGSSL_ADD_USER_LABEL_AND_PREFIX(CBS_is_valid_asn1_relative_oid) +#pragma redefine_extname CBS_is_valid_asn1_set_of BORINGSSL_ADD_USER_LABEL_AND_PREFIX(CBS_is_valid_asn1_set_of) #pragma redefine_extname CBS_mem_equal BORINGSSL_ADD_USER_LABEL_AND_PREFIX(CBS_mem_equal) #pragma redefine_extname CBS_parse_generalized_time BORINGSSL_ADD_USER_LABEL_AND_PREFIX(CBS_parse_generalized_time) #pragma redefine_extname CBS_parse_utc_time BORINGSSL_ADD_USER_LABEL_AND_PREFIX(CBS_parse_utc_time) +#pragma redefine_extname CBS_peek_any_asn1_tag BORINGSSL_ADD_USER_LABEL_AND_PREFIX(CBS_peek_any_asn1_tag) #pragma redefine_extname CBS_peek_asn1_tag BORINGSSL_ADD_USER_LABEL_AND_PREFIX(CBS_peek_asn1_tag) #pragma redefine_extname CBS_skip BORINGSSL_ADD_USER_LABEL_AND_PREFIX(CBS_skip) #pragma redefine_extname CBS_stow BORINGSSL_ADD_USER_LABEL_AND_PREFIX(CBS_stow) @@ -608,9 +618,14 @@ #pragma redefine_extname CONF_modules_free BORINGSSL_ADD_USER_LABEL_AND_PREFIX(CONF_modules_free) #pragma redefine_extname CONF_modules_load_file BORINGSSL_ADD_USER_LABEL_AND_PREFIX(CONF_modules_load_file) #pragma redefine_extname CONF_modules_unload BORINGSSL_ADD_USER_LABEL_AND_PREFIX(CONF_modules_unload) +#pragma redefine_extname CPACE_CTX_free BORINGSSL_ADD_USER_LABEL_AND_PREFIX(CPACE_CTX_free) +#pragma redefine_extname CPACE_CTX_new BORINGSSL_ADD_USER_LABEL_AND_PREFIX(CPACE_CTX_new) +#pragma redefine_extname CPACE_generate_msg BORINGSSL_ADD_USER_LABEL_AND_PREFIX(CPACE_generate_msg) +#pragma redefine_extname CPACE_process_msg BORINGSSL_ADD_USER_LABEL_AND_PREFIX(CPACE_process_msg) #pragma redefine_extname CRL_DIST_POINTS_free BORINGSSL_ADD_USER_LABEL_AND_PREFIX(CRL_DIST_POINTS_free) #pragma redefine_extname CRL_DIST_POINTS_it BORINGSSL_ADD_USER_LABEL_AND_PREFIX(CRL_DIST_POINTS_it) #pragma redefine_extname CRL_DIST_POINTS_new BORINGSSL_ADD_USER_LABEL_AND_PREFIX(CRL_DIST_POINTS_new) +#pragma redefine_extname CRYPTO_BUFFER_POOL_dup_ref BORINGSSL_ADD_USER_LABEL_AND_PREFIX(CRYPTO_BUFFER_POOL_dup_ref) #pragma redefine_extname CRYPTO_BUFFER_POOL_free BORINGSSL_ADD_USER_LABEL_AND_PREFIX(CRYPTO_BUFFER_POOL_free) #pragma redefine_extname CRYPTO_BUFFER_POOL_new BORINGSSL_ADD_USER_LABEL_AND_PREFIX(CRYPTO_BUFFER_POOL_new) #pragma redefine_extname CRYPTO_BUFFER_POOL_up_ref BORINGSSL_ADD_USER_LABEL_AND_PREFIX(CRYPTO_BUFFER_POOL_up_ref) @@ -685,6 +700,7 @@ #pragma redefine_extname DH_compute_key BORINGSSL_ADD_USER_LABEL_AND_PREFIX(DH_compute_key) #pragma redefine_extname DH_compute_key_hashed BORINGSSL_ADD_USER_LABEL_AND_PREFIX(DH_compute_key_hashed) #pragma redefine_extname DH_compute_key_padded BORINGSSL_ADD_USER_LABEL_AND_PREFIX(DH_compute_key_padded) +#pragma redefine_extname DH_dup_ref BORINGSSL_ADD_USER_LABEL_AND_PREFIX(DH_dup_ref) #pragma redefine_extname DH_free BORINGSSL_ADD_USER_LABEL_AND_PREFIX(DH_free) #pragma redefine_extname DH_generate_key BORINGSSL_ADD_USER_LABEL_AND_PREFIX(DH_generate_key) #pragma redefine_extname DH_generate_parameters BORINGSSL_ADD_USER_LABEL_AND_PREFIX(DH_generate_parameters) @@ -726,6 +742,7 @@ #pragma redefine_extname DSA_do_sign BORINGSSL_ADD_USER_LABEL_AND_PREFIX(DSA_do_sign) #pragma redefine_extname DSA_do_verify BORINGSSL_ADD_USER_LABEL_AND_PREFIX(DSA_do_verify) #pragma redefine_extname DSA_dup_DH BORINGSSL_ADD_USER_LABEL_AND_PREFIX(DSA_dup_DH) +#pragma redefine_extname DSA_dup_ref BORINGSSL_ADD_USER_LABEL_AND_PREFIX(DSA_dup_ref) #pragma redefine_extname DSA_free BORINGSSL_ADD_USER_LABEL_AND_PREFIX(DSA_free) #pragma redefine_extname DSA_generate_key BORINGSSL_ADD_USER_LABEL_AND_PREFIX(DSA_generate_key) #pragma redefine_extname DSA_generate_parameters BORINGSSL_ADD_USER_LABEL_AND_PREFIX(DSA_generate_parameters) @@ -811,6 +828,7 @@ #pragma redefine_extname EC_KEY_check_key BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EC_KEY_check_key) #pragma redefine_extname EC_KEY_derive_from_secret BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EC_KEY_derive_from_secret) #pragma redefine_extname EC_KEY_dup BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EC_KEY_dup) +#pragma redefine_extname EC_KEY_dup_ref BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EC_KEY_dup_ref) #pragma redefine_extname EC_KEY_free BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EC_KEY_free) #pragma redefine_extname EC_KEY_generate_key BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EC_KEY_generate_key) #pragma redefine_extname EC_KEY_generate_key_fips BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EC_KEY_generate_key_fips) @@ -878,10 +896,14 @@ #pragma redefine_extname EC_group_p521 BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EC_group_p521) #pragma redefine_extname EC_hash_to_curve_p256_xmd_sha256_sswu BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EC_hash_to_curve_p256_xmd_sha256_sswu) #pragma redefine_extname EC_hash_to_curve_p384_xmd_sha384_sswu BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EC_hash_to_curve_p384_xmd_sha384_sswu) +#pragma redefine_extname EC_wpa3_sae_hash_to_curve_p256 BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EC_wpa3_sae_hash_to_curve_p256) +#pragma redefine_extname EC_wpa3_sae_hunt_and_peck_p256 BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EC_wpa3_sae_hunt_and_peck_p256) #pragma redefine_extname ED25519_keypair BORINGSSL_ADD_USER_LABEL_AND_PREFIX(ED25519_keypair) #pragma redefine_extname ED25519_keypair_from_seed BORINGSSL_ADD_USER_LABEL_AND_PREFIX(ED25519_keypair_from_seed) #pragma redefine_extname ED25519_sign BORINGSSL_ADD_USER_LABEL_AND_PREFIX(ED25519_sign) +#pragma redefine_extname ED25519_sign_prehashed BORINGSSL_ADD_USER_LABEL_AND_PREFIX(ED25519_sign_prehashed) #pragma redefine_extname ED25519_verify BORINGSSL_ADD_USER_LABEL_AND_PREFIX(ED25519_verify) +#pragma redefine_extname ED25519_verify_prehashed BORINGSSL_ADD_USER_LABEL_AND_PREFIX(ED25519_verify_prehashed) #pragma redefine_extname EDIPARTYNAME_free BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EDIPARTYNAME_free) #pragma redefine_extname EDIPARTYNAME_new BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EDIPARTYNAME_new) #pragma redefine_extname ENGINE_cleanup BORINGSSL_ADD_USER_LABEL_AND_PREFIX(ENGINE_cleanup) @@ -962,6 +984,8 @@ #pragma redefine_extname EVP_CIPHER_CTX_init BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_CIPHER_CTX_init) #pragma redefine_extname EVP_CIPHER_CTX_iv_length BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_CIPHER_CTX_iv_length) #pragma redefine_extname EVP_CIPHER_CTX_key_length BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_CIPHER_CTX_key_length) +#pragma redefine_extname EVP_CIPHER_CTX_max_final BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_CIPHER_CTX_max_final) +#pragma redefine_extname EVP_CIPHER_CTX_max_next_update BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_CIPHER_CTX_max_next_update) #pragma redefine_extname EVP_CIPHER_CTX_mode BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_CIPHER_CTX_mode) #pragma redefine_extname EVP_CIPHER_CTX_new BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_CIPHER_CTX_new) #pragma redefine_extname EVP_CIPHER_CTX_nid BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_CIPHER_CTX_nid) @@ -1009,6 +1033,7 @@ #pragma redefine_extname EVP_DigestSignFinal BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_DigestSignFinal) #pragma redefine_extname EVP_DigestSignInit BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_DigestSignInit) #pragma redefine_extname EVP_DigestSignUpdate BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_DigestSignUpdate) +#pragma redefine_extname EVP_DigestSqueeze BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_DigestSqueeze) #pragma redefine_extname EVP_DigestUpdate BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_DigestUpdate) #pragma redefine_extname EVP_DigestVerify BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_DigestVerify) #pragma redefine_extname EVP_DigestVerifyFinal BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_DigestVerifyFinal) @@ -1044,6 +1069,7 @@ #pragma redefine_extname EVP_HPKE_CTX_setup_auth_sender BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_HPKE_CTX_setup_auth_sender) #pragma redefine_extname EVP_HPKE_CTX_setup_auth_sender_with_seed_for_testing BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_HPKE_CTX_setup_auth_sender_with_seed_for_testing) #pragma redefine_extname EVP_HPKE_CTX_setup_recipient BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_HPKE_CTX_setup_recipient) +#pragma redefine_extname EVP_HPKE_CTX_setup_recipient_with_shared_secret BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_HPKE_CTX_setup_recipient_with_shared_secret) #pragma redefine_extname EVP_HPKE_CTX_setup_sender BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_HPKE_CTX_setup_sender) #pragma redefine_extname EVP_HPKE_CTX_setup_sender_with_seed_for_testing BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_HPKE_CTX_setup_sender_with_seed_for_testing) #pragma redefine_extname EVP_HPKE_CTX_zero BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_HPKE_CTX_zero) @@ -1053,6 +1079,7 @@ #pragma redefine_extname EVP_HPKE_KEM_id BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_HPKE_KEM_id) #pragma redefine_extname EVP_HPKE_KEM_private_key_len BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_HPKE_KEM_private_key_len) #pragma redefine_extname EVP_HPKE_KEM_public_key_len BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_HPKE_KEM_public_key_len) +#pragma redefine_extname EVP_HPKE_KEM_shared_secret_len BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_HPKE_KEM_shared_secret_len) #pragma redefine_extname EVP_HPKE_KEY_cleanup BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_HPKE_KEY_cleanup) #pragma redefine_extname EVP_HPKE_KEY_copy BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_HPKE_KEY_copy) #pragma redefine_extname EVP_HPKE_KEY_derive BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_HPKE_KEY_derive) @@ -1068,6 +1095,8 @@ #pragma redefine_extname EVP_KEM_ciphertext_len BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_KEM_ciphertext_len) #pragma redefine_extname EVP_KEM_decap BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_KEM_decap) #pragma redefine_extname EVP_KEM_encap BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_KEM_encap) +#pragma redefine_extname EVP_KEM_encap_external_entropy_for_testing BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_KEM_encap_external_entropy_for_testing) +#pragma redefine_extname EVP_KEM_entropy_len_for_testing BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_KEM_entropy_len_for_testing) #pragma redefine_extname EVP_KEM_secret_len BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_KEM_secret_len) #pragma redefine_extname EVP_MD_CTX_block_size BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_MD_CTX_block_size) #pragma redefine_extname EVP_MD_CTX_cleanse BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_MD_CTX_cleanse) @@ -1160,10 +1189,15 @@ #pragma redefine_extname EVP_PKEY_encrypt_init BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_PKEY_encrypt_init) #pragma redefine_extname EVP_PKEY_eq BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_PKEY_eq) #pragma redefine_extname EVP_PKEY_free BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_PKEY_free) +#pragma redefine_extname EVP_PKEY_from_ec_compressed_point BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_PKEY_from_ec_compressed_point) +#pragma redefine_extname EVP_PKEY_from_ec_private_scalar BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_PKEY_from_ec_private_scalar) +#pragma redefine_extname EVP_PKEY_from_ec_uncompressed_point BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_PKEY_from_ec_uncompressed_point) #pragma redefine_extname EVP_PKEY_from_private_key_info BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_PKEY_from_private_key_info) #pragma redefine_extname EVP_PKEY_from_private_seed BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_PKEY_from_private_seed) #pragma redefine_extname EVP_PKEY_from_raw_private_key BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_PKEY_from_raw_private_key) #pragma redefine_extname EVP_PKEY_from_raw_public_key BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_PKEY_from_raw_public_key) +#pragma redefine_extname EVP_PKEY_from_rsa_private_key BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_PKEY_from_rsa_private_key) +#pragma redefine_extname EVP_PKEY_from_rsa_public_key BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_PKEY_from_rsa_public_key) #pragma redefine_extname EVP_PKEY_from_subject_public_key_info BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_PKEY_from_subject_public_key_info) #pragma redefine_extname EVP_PKEY_generate_from_alg BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_PKEY_generate_from_alg) #pragma redefine_extname EVP_PKEY_get0 BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_PKEY_get0) @@ -1187,6 +1221,11 @@ #pragma redefine_extname EVP_PKEY_is_opaque BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_PKEY_is_opaque) #pragma redefine_extname EVP_PKEY_keygen BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_PKEY_keygen) #pragma redefine_extname EVP_PKEY_keygen_init BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_PKEY_keygen_init) +#pragma redefine_extname EVP_PKEY_marshal_ec_compressed_point BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_PKEY_marshal_ec_compressed_point) +#pragma redefine_extname EVP_PKEY_marshal_ec_private_scalar BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_PKEY_marshal_ec_private_scalar) +#pragma redefine_extname EVP_PKEY_marshal_ec_uncompressed_point BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_PKEY_marshal_ec_uncompressed_point) +#pragma redefine_extname EVP_PKEY_marshal_rsa_private_key BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_PKEY_marshal_rsa_private_key) +#pragma redefine_extname EVP_PKEY_marshal_rsa_public_key BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_PKEY_marshal_rsa_public_key) #pragma redefine_extname EVP_PKEY_missing_parameters BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_PKEY_missing_parameters) #pragma redefine_extname EVP_PKEY_new BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_PKEY_new) #pragma redefine_extname EVP_PKEY_new_raw_private_key BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_PKEY_new_raw_private_key) @@ -1306,6 +1345,8 @@ #pragma redefine_extname EVP_hpke_p256_hkdf_sha256 BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_hpke_p256_hkdf_sha256) #pragma redefine_extname EVP_hpke_x25519_hkdf_sha256 BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_hpke_x25519_hkdf_sha256) #pragma redefine_extname EVP_hpke_xwing BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_hpke_xwing) +#pragma redefine_extname EVP_kem_dhkem_p256_hkdf_sha256 BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_kem_dhkem_p256_hkdf_sha256) +#pragma redefine_extname EVP_kem_dhkem_x25519_hkdf_sha256 BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_kem_dhkem_x25519_hkdf_sha256) #pragma redefine_extname EVP_kem_ml_kem_1024 BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_kem_ml_kem_1024) #pragma redefine_extname EVP_kem_ml_kem_768 BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_kem_ml_kem_768) #pragma redefine_extname EVP_kem_xwing BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_kem_xwing) @@ -1335,6 +1376,9 @@ #pragma redefine_extname EVP_pkey_rsa_pss_sha256 BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_pkey_rsa_pss_sha256) #pragma redefine_extname EVP_pkey_rsa_pss_sha384 BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_pkey_rsa_pss_sha384) #pragma redefine_extname EVP_pkey_rsa_pss_sha512 BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_pkey_rsa_pss_sha512) +#pragma redefine_extname EVP_pkey_slh_dsa_sha2_128s BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_pkey_slh_dsa_sha2_128s) +#pragma redefine_extname EVP_pkey_slh_dsa_sha2_128s_with_sha256 BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_pkey_slh_dsa_sha2_128s_with_sha256) +#pragma redefine_extname EVP_pkey_slh_dsa_shake_256f BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_pkey_slh_dsa_shake_256f) #pragma redefine_extname EVP_pkey_x25519 BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_pkey_x25519) #pragma redefine_extname EVP_pkey_xwing BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_pkey_xwing) #pragma redefine_extname EVP_rc2_40_cbc BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_rc2_40_cbc) @@ -1344,8 +1388,13 @@ #pragma redefine_extname EVP_sha224 BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_sha224) #pragma redefine_extname EVP_sha256 BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_sha256) #pragma redefine_extname EVP_sha384 BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_sha384) +#pragma redefine_extname EVP_sha3_256 BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_sha3_256) +#pragma redefine_extname EVP_sha3_384 BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_sha3_384) +#pragma redefine_extname EVP_sha3_512 BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_sha3_512) #pragma redefine_extname EVP_sha512 BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_sha512) #pragma redefine_extname EVP_sha512_256 BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_sha512_256) +#pragma redefine_extname EVP_shake256 BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_shake256) +#pragma redefine_extname EVP_turboshake256 BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EVP_turboshake256) #pragma redefine_extname EXTENDED_KEY_USAGE_free BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EXTENDED_KEY_USAGE_free) #pragma redefine_extname EXTENDED_KEY_USAGE_it BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EXTENDED_KEY_USAGE_it) #pragma redefine_extname EXTENDED_KEY_USAGE_new BORINGSSL_ADD_USER_LABEL_AND_PREFIX(EXTENDED_KEY_USAGE_new) @@ -1404,6 +1453,8 @@ #pragma redefine_extname MD5_Init BORINGSSL_ADD_USER_LABEL_AND_PREFIX(MD5_Init) #pragma redefine_extname MD5_Transform BORINGSSL_ADD_USER_LABEL_AND_PREFIX(MD5_Transform) #pragma redefine_extname MD5_Update BORINGSSL_ADD_USER_LABEL_AND_PREFIX(MD5_Update) +#pragma redefine_extname MD5_marshal_state BORINGSSL_ADD_USER_LABEL_AND_PREFIX(MD5_marshal_state) +#pragma redefine_extname MD5_parse_state BORINGSSL_ADD_USER_LABEL_AND_PREFIX(MD5_parse_state) #pragma redefine_extname METHOD_ref BORINGSSL_ADD_USER_LABEL_AND_PREFIX(METHOD_ref) #pragma redefine_extname METHOD_unref BORINGSSL_ADD_USER_LABEL_AND_PREFIX(METHOD_unref) #pragma redefine_extname MLDSA44_generate_key BORINGSSL_ADD_USER_LABEL_AND_PREFIX(MLDSA44_generate_key) @@ -1535,6 +1586,7 @@ #pragma redefine_extname OPENSSL_sk_free BORINGSSL_ADD_USER_LABEL_AND_PREFIX(OPENSSL_sk_free) #pragma redefine_extname OPENSSL_sk_insert BORINGSSL_ADD_USER_LABEL_AND_PREFIX(OPENSSL_sk_insert) #pragma redefine_extname OPENSSL_sk_is_sorted BORINGSSL_ADD_USER_LABEL_AND_PREFIX(OPENSSL_sk_is_sorted) +#pragma redefine_extname OPENSSL_sk_last BORINGSSL_ADD_USER_LABEL_AND_PREFIX(OPENSSL_sk_last) #pragma redefine_extname OPENSSL_sk_new BORINGSSL_ADD_USER_LABEL_AND_PREFIX(OPENSSL_sk_new) #pragma redefine_extname OPENSSL_sk_new_null BORINGSSL_ADD_USER_LABEL_AND_PREFIX(OPENSSL_sk_new_null) #pragma redefine_extname OPENSSL_sk_num BORINGSSL_ADD_USER_LABEL_AND_PREFIX(OPENSSL_sk_num) @@ -1714,7 +1766,6 @@ #pragma redefine_extname RAND_get_rand_method BORINGSSL_ADD_USER_LABEL_AND_PREFIX(RAND_get_rand_method) #pragma redefine_extname RAND_get_system_entropy_for_custom_prng BORINGSSL_ADD_USER_LABEL_AND_PREFIX(RAND_get_system_entropy_for_custom_prng) #pragma redefine_extname RAND_load_file BORINGSSL_ADD_USER_LABEL_AND_PREFIX(RAND_load_file) -#pragma redefine_extname RAND_maybe_reseed BORINGSSL_ADD_USER_LABEL_AND_PREFIX(RAND_maybe_reseed) #pragma redefine_extname RAND_poll BORINGSSL_ADD_USER_LABEL_AND_PREFIX(RAND_poll) #pragma redefine_extname RAND_pseudo_bytes BORINGSSL_ADD_USER_LABEL_AND_PREFIX(RAND_pseudo_bytes) #pragma redefine_extname RAND_reset_for_fuzzing BORINGSSL_ADD_USER_LABEL_AND_PREFIX(RAND_reset_for_fuzzing) @@ -1741,6 +1792,7 @@ #pragma redefine_extname RSA_check_fips BORINGSSL_ADD_USER_LABEL_AND_PREFIX(RSA_check_fips) #pragma redefine_extname RSA_check_key BORINGSSL_ADD_USER_LABEL_AND_PREFIX(RSA_check_key) #pragma redefine_extname RSA_decrypt BORINGSSL_ADD_USER_LABEL_AND_PREFIX(RSA_decrypt) +#pragma redefine_extname RSA_dup_ref BORINGSSL_ADD_USER_LABEL_AND_PREFIX(RSA_dup_ref) #pragma redefine_extname RSA_encrypt BORINGSSL_ADD_USER_LABEL_AND_PREFIX(RSA_encrypt) #pragma redefine_extname RSA_flags BORINGSSL_ADD_USER_LABEL_AND_PREFIX(RSA_flags) #pragma redefine_extname RSA_free BORINGSSL_ADD_USER_LABEL_AND_PREFIX(RSA_free) @@ -1803,27 +1855,21 @@ #pragma redefine_extname RSA_verify_PKCS1_PSS_mgf1 BORINGSSL_ADD_USER_LABEL_AND_PREFIX(RSA_verify_PKCS1_PSS_mgf1) #pragma redefine_extname RSA_verify_pss_mgf1 BORINGSSL_ADD_USER_LABEL_AND_PREFIX(RSA_verify_pss_mgf1) #pragma redefine_extname RSA_verify_raw BORINGSSL_ADD_USER_LABEL_AND_PREFIX(RSA_verify_raw) -#pragma redefine_extname SHA1 BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SHA1) #pragma redefine_extname SHA1_Final BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SHA1_Final) #pragma redefine_extname SHA1_Init BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SHA1_Init) #pragma redefine_extname SHA1_Transform BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SHA1_Transform) #pragma redefine_extname SHA1_Update BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SHA1_Update) -#pragma redefine_extname SHA224 BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SHA224) #pragma redefine_extname SHA224_Final BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SHA224_Final) #pragma redefine_extname SHA224_Init BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SHA224_Init) #pragma redefine_extname SHA224_Update BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SHA224_Update) -#pragma redefine_extname SHA256 BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SHA256) #pragma redefine_extname SHA256_Final BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SHA256_Final) #pragma redefine_extname SHA256_Init BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SHA256_Init) #pragma redefine_extname SHA256_Transform BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SHA256_Transform) #pragma redefine_extname SHA256_TransformBlocks BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SHA256_TransformBlocks) #pragma redefine_extname SHA256_Update BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SHA256_Update) -#pragma redefine_extname SHA384 BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SHA384) #pragma redefine_extname SHA384_Final BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SHA384_Final) #pragma redefine_extname SHA384_Init BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SHA384_Init) #pragma redefine_extname SHA384_Update BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SHA384_Update) -#pragma redefine_extname SHA512 BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SHA512) -#pragma redefine_extname SHA512_256 BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SHA512_256) #pragma redefine_extname SHA512_256_Final BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SHA512_256_Final) #pragma redefine_extname SHA512_256_Init BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SHA512_256_Init) #pragma redefine_extname SHA512_256_Update BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SHA512_256_Update) @@ -1833,6 +1879,7 @@ #pragma redefine_extname SHA512_Update BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SHA512_Update) #pragma redefine_extname SIPHASH_24 BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SIPHASH_24) #pragma redefine_extname SLHDSA_SHA2_128S_generate_key BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SLHDSA_SHA2_128S_generate_key) +#pragma redefine_extname SLHDSA_SHA2_128S_generate_key_from_seed BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SLHDSA_SHA2_128S_generate_key_from_seed) #pragma redefine_extname SLHDSA_SHA2_128S_prehash_sign BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SLHDSA_SHA2_128S_prehash_sign) #pragma redefine_extname SLHDSA_SHA2_128S_prehash_verify BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SLHDSA_SHA2_128S_prehash_verify) #pragma redefine_extname SLHDSA_SHA2_128S_prehash_warning_nonstandard_sign BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SLHDSA_SHA2_128S_prehash_warning_nonstandard_sign) @@ -1841,6 +1888,7 @@ #pragma redefine_extname SLHDSA_SHA2_128S_sign BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SLHDSA_SHA2_128S_sign) #pragma redefine_extname SLHDSA_SHA2_128S_verify BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SLHDSA_SHA2_128S_verify) #pragma redefine_extname SLHDSA_SHAKE_256F_generate_key BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SLHDSA_SHAKE_256F_generate_key) +#pragma redefine_extname SLHDSA_SHAKE_256F_generate_key_from_seed BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SLHDSA_SHAKE_256F_generate_key_from_seed) #pragma redefine_extname SLHDSA_SHAKE_256F_public_from_private BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SLHDSA_SHAKE_256F_public_from_private) #pragma redefine_extname SLHDSA_SHAKE_256F_sign BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SLHDSA_SHAKE_256F_sign) #pragma redefine_extname SLHDSA_SHAKE_256F_verify BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SLHDSA_SHAKE_256F_verify) @@ -1872,8 +1920,10 @@ #pragma redefine_extname SSL_COMP_get_compression_methods BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_COMP_get_compression_methods) #pragma redefine_extname SSL_COMP_get_id BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_COMP_get_id) #pragma redefine_extname SSL_COMP_get_name BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_COMP_get_name) +#pragma redefine_extname SSL_CREDENTIAL_add1_trust_anchor_group BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_CREDENTIAL_add1_trust_anchor_group) #pragma redefine_extname SSL_CREDENTIAL_dup_ref BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_CREDENTIAL_dup_ref) #pragma redefine_extname SSL_CREDENTIAL_free BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_CREDENTIAL_free) +#pragma redefine_extname SSL_CREDENTIAL_get0_pre_shared_key_id BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_CREDENTIAL_get0_pre_shared_key_id) #pragma redefine_extname SSL_CREDENTIAL_get_ex_data BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_CREDENTIAL_get_ex_data) #pragma redefine_extname SSL_CREDENTIAL_get_ex_new_index BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_CREDENTIAL_get_ex_new_index) #pragma redefine_extname SSL_CREDENTIAL_is_complete BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_CREDENTIAL_is_complete) @@ -1889,6 +1939,7 @@ #pragma redefine_extname SSL_CREDENTIAL_set1_delegated_credential BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_CREDENTIAL_set1_delegated_credential) #pragma redefine_extname SSL_CREDENTIAL_set1_ocsp_response BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_CREDENTIAL_set1_ocsp_response) #pragma redefine_extname SSL_CREDENTIAL_set1_private_key BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_CREDENTIAL_set1_private_key) +#pragma redefine_extname SSL_CREDENTIAL_set1_session_id_context BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_CREDENTIAL_set1_session_id_context) #pragma redefine_extname SSL_CREDENTIAL_set1_signed_cert_timestamp_list BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_CREDENTIAL_set1_signed_cert_timestamp_list) #pragma redefine_extname SSL_CREDENTIAL_set1_signing_algorithm_prefs BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_CREDENTIAL_set1_signing_algorithm_prefs) #pragma redefine_extname SSL_CREDENTIAL_set1_trust_anchor_id BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_CREDENTIAL_set1_trust_anchor_id) @@ -1909,6 +1960,7 @@ #pragma redefine_extname SSL_CTX_clear_extra_chain_certs BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_CTX_clear_extra_chain_certs) #pragma redefine_extname SSL_CTX_clear_mode BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_CTX_clear_mode) #pragma redefine_extname SSL_CTX_clear_options BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_CTX_clear_options) +#pragma redefine_extname SSL_CTX_dup_ref BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_CTX_dup_ref) #pragma redefine_extname SSL_CTX_enable_ocsp_stapling BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_CTX_enable_ocsp_stapling) #pragma redefine_extname SSL_CTX_enable_signed_cert_timestamps BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_CTX_enable_signed_cert_timestamps) #pragma redefine_extname SSL_CTX_enable_tls_channel_id BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_CTX_enable_tls_channel_id) @@ -1989,12 +2041,14 @@ #pragma redefine_extname SSL_CTX_set1_requested_trust_anchors BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_CTX_set1_requested_trust_anchors) #pragma redefine_extname SSL_CTX_set1_sigalgs BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_CTX_set1_sigalgs) #pragma redefine_extname SSL_CTX_set1_sigalgs_list BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_CTX_set1_sigalgs_list) +#pragma redefine_extname SSL_CTX_set1_tls13_ciphers BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_CTX_set1_tls13_ciphers) #pragma redefine_extname SSL_CTX_set1_tls_channel_id BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_CTX_set1_tls_channel_id) #pragma redefine_extname SSL_CTX_set1_verify_cert_store BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_CTX_set1_verify_cert_store) #pragma redefine_extname SSL_CTX_set_allow_unknown_alpn_protos BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_CTX_set_allow_unknown_alpn_protos) #pragma redefine_extname SSL_CTX_set_alpn_protos BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_CTX_set_alpn_protos) #pragma redefine_extname SSL_CTX_set_alpn_select_cb BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_CTX_set_alpn_select_cb) #pragma redefine_extname SSL_CTX_set_cert_cb BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_CTX_set_cert_cb) +#pragma redefine_extname SSL_CTX_set_cert_cb_ex BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_CTX_set_cert_cb_ex) #pragma redefine_extname SSL_CTX_set_cert_store BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_CTX_set_cert_store) #pragma redefine_extname SSL_CTX_set_cert_verify_callback BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_CTX_set_cert_verify_callback) #pragma redefine_extname SSL_CTX_set_chain_and_key BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_CTX_set_chain_and_key) @@ -2078,12 +2132,14 @@ #pragma redefine_extname SSL_CTX_use_certificate_file BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_CTX_use_certificate_file) #pragma redefine_extname SSL_CTX_use_psk_identity_hint BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_CTX_use_psk_identity_hint) #pragma redefine_extname SSL_ECH_KEYS_add BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_ECH_KEYS_add) +#pragma redefine_extname SSL_ECH_KEYS_dup_ref BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_ECH_KEYS_dup_ref) #pragma redefine_extname SSL_ECH_KEYS_free BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_ECH_KEYS_free) #pragma redefine_extname SSL_ECH_KEYS_has_duplicate_config_id BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_ECH_KEYS_has_duplicate_config_id) #pragma redefine_extname SSL_ECH_KEYS_marshal_retry_configs BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_ECH_KEYS_marshal_retry_configs) #pragma redefine_extname SSL_ECH_KEYS_new BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_ECH_KEYS_new) #pragma redefine_extname SSL_ECH_KEYS_up_ref BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_ECH_KEYS_up_ref) #pragma redefine_extname SSL_SESSION_copy_without_early_data BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_SESSION_copy_without_early_data) +#pragma redefine_extname SSL_SESSION_dup_ref BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_SESSION_dup_ref) #pragma redefine_extname SSL_SESSION_early_data_capable BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_SESSION_early_data_capable) #pragma redefine_extname SSL_SESSION_free BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_SESSION_free) #pragma redefine_extname SSL_SESSION_from_bytes BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_SESSION_from_bytes) @@ -2232,6 +2288,7 @@ #pragma redefine_extname SSL_get_peer_full_cert_chain BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_get_peer_full_cert_chain) #pragma redefine_extname SSL_get_peer_quic_transport_params BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_get_peer_quic_transport_params) #pragma redefine_extname SSL_get_peer_signature_algorithm BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_get_peer_signature_algorithm) +#pragma redefine_extname SSL_get_peer_tmp_key BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_get_peer_tmp_key) #pragma redefine_extname SSL_get_pending_cipher BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_get_pending_cipher) #pragma redefine_extname SSL_get_privatekey BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_get_privatekey) #pragma redefine_extname SSL_get_psk_identity BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_get_psk_identity) @@ -2335,6 +2392,7 @@ #pragma redefine_extname SSL_set1_server_supported_groups_hint BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_set1_server_supported_groups_hint) #pragma redefine_extname SSL_set1_sigalgs BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_set1_sigalgs) #pragma redefine_extname SSL_set1_sigalgs_list BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_set1_sigalgs_list) +#pragma redefine_extname SSL_set1_tls13_ciphers BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_set1_tls13_ciphers) #pragma redefine_extname SSL_set1_tls_channel_id BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_set1_tls_channel_id) #pragma redefine_extname SSL_set1_verify_cert_store BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_set1_verify_cert_store) #pragma redefine_extname SSL_set_SSL_CTX BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_set_SSL_CTX) @@ -2343,6 +2401,7 @@ #pragma redefine_extname SSL_set_alps_use_new_codepoint BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_set_alps_use_new_codepoint) #pragma redefine_extname SSL_set_bio BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_set_bio) #pragma redefine_extname SSL_set_cert_cb BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_set_cert_cb) +#pragma redefine_extname SSL_set_cert_cb_ex BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_set_cert_cb_ex) #pragma redefine_extname SSL_set_chain_and_key BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_set_chain_and_key) #pragma redefine_extname SSL_set_cipher_list BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_set_cipher_list) #pragma redefine_extname SSL_set_client_CA_list BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_set_client_CA_list) @@ -2351,7 +2410,6 @@ #pragma redefine_extname SSL_set_custom_verify BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_set_custom_verify) #pragma redefine_extname SSL_set_early_data_enabled BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_set_early_data_enabled) #pragma redefine_extname SSL_set_enable_ech_grease BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_set_enable_ech_grease) -#pragma redefine_extname SSL_set_enforce_rsa_key_usage BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_set_enforce_rsa_key_usage) #pragma redefine_extname SSL_set_ex_data BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_set_ex_data) #pragma redefine_extname SSL_set_fd BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_set_fd) #pragma redefine_extname SSL_set_handshake_hints BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_set_handshake_hints) @@ -2379,6 +2437,7 @@ #pragma redefine_extname SSL_set_quic_use_legacy_codepoint BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_set_quic_use_legacy_codepoint) #pragma redefine_extname SSL_set_quiet_shutdown BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_set_quiet_shutdown) #pragma redefine_extname SSL_set_read_ahead BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_set_read_ahead) +#pragma redefine_extname SSL_set_reject_unusable_ech_config BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_set_reject_unusable_ech_config) #pragma redefine_extname SSL_set_renegotiate_mode BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_set_renegotiate_mode) #pragma redefine_extname SSL_set_resumption_across_names_enabled BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_set_resumption_across_names_enabled) #pragma redefine_extname SSL_set_retain_only_sha256_of_client_certs BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_set_retain_only_sha256_of_client_certs) @@ -2428,7 +2487,6 @@ #pragma redefine_extname SSL_used_hello_retry_request BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_used_hello_retry_request) #pragma redefine_extname SSL_version BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_version) #pragma redefine_extname SSL_want BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_want) -#pragma redefine_extname SSL_was_key_usage_invalid BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_was_key_usage_invalid) #pragma redefine_extname SSL_write BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSL_write) #pragma redefine_extname SSLeay BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSLeay) #pragma redefine_extname SSLeay_version BORINGSSL_ADD_USER_LABEL_AND_PREFIX(SSLeay_version) @@ -2532,6 +2590,7 @@ #pragma redefine_extname X509_CRL_delete_ext BORINGSSL_ADD_USER_LABEL_AND_PREFIX(X509_CRL_delete_ext) #pragma redefine_extname X509_CRL_digest BORINGSSL_ADD_USER_LABEL_AND_PREFIX(X509_CRL_digest) #pragma redefine_extname X509_CRL_dup BORINGSSL_ADD_USER_LABEL_AND_PREFIX(X509_CRL_dup) +#pragma redefine_extname X509_CRL_dup_ref BORINGSSL_ADD_USER_LABEL_AND_PREFIX(X509_CRL_dup_ref) #pragma redefine_extname X509_CRL_free BORINGSSL_ADD_USER_LABEL_AND_PREFIX(X509_CRL_free) #pragma redefine_extname X509_CRL_get0_by_cert BORINGSSL_ADD_USER_LABEL_AND_PREFIX(X509_CRL_get0_by_cert) #pragma redefine_extname X509_CRL_get0_by_serial BORINGSSL_ADD_USER_LABEL_AND_PREFIX(X509_CRL_get0_by_serial) @@ -2698,7 +2757,6 @@ #pragma redefine_extname X509_STORE_CTX_free BORINGSSL_ADD_USER_LABEL_AND_PREFIX(X509_STORE_CTX_free) #pragma redefine_extname X509_STORE_CTX_get0_cert BORINGSSL_ADD_USER_LABEL_AND_PREFIX(X509_STORE_CTX_get0_cert) #pragma redefine_extname X509_STORE_CTX_get0_chain BORINGSSL_ADD_USER_LABEL_AND_PREFIX(X509_STORE_CTX_get0_chain) -#pragma redefine_extname X509_STORE_CTX_get0_current_crl BORINGSSL_ADD_USER_LABEL_AND_PREFIX(X509_STORE_CTX_get0_current_crl) #pragma redefine_extname X509_STORE_CTX_get0_param BORINGSSL_ADD_USER_LABEL_AND_PREFIX(X509_STORE_CTX_get0_param) #pragma redefine_extname X509_STORE_CTX_get0_parent_ctx BORINGSSL_ADD_USER_LABEL_AND_PREFIX(X509_STORE_CTX_get0_parent_ctx) #pragma redefine_extname X509_STORE_CTX_get0_store BORINGSSL_ADD_USER_LABEL_AND_PREFIX(X509_STORE_CTX_get0_store) @@ -2734,6 +2792,7 @@ #pragma redefine_extname X509_STORE_add_cert BORINGSSL_ADD_USER_LABEL_AND_PREFIX(X509_STORE_add_cert) #pragma redefine_extname X509_STORE_add_crl BORINGSSL_ADD_USER_LABEL_AND_PREFIX(X509_STORE_add_crl) #pragma redefine_extname X509_STORE_add_lookup BORINGSSL_ADD_USER_LABEL_AND_PREFIX(X509_STORE_add_lookup) +#pragma redefine_extname X509_STORE_dup_ref BORINGSSL_ADD_USER_LABEL_AND_PREFIX(X509_STORE_dup_ref) #pragma redefine_extname X509_STORE_free BORINGSSL_ADD_USER_LABEL_AND_PREFIX(X509_STORE_free) #pragma redefine_extname X509_STORE_get0_objects BORINGSSL_ADD_USER_LABEL_AND_PREFIX(X509_STORE_get0_objects) #pragma redefine_extname X509_STORE_get0_param BORINGSSL_ADD_USER_LABEL_AND_PREFIX(X509_STORE_get0_param) @@ -2789,6 +2848,7 @@ #pragma redefine_extname X509_cmp_current_time BORINGSSL_ADD_USER_LABEL_AND_PREFIX(X509_cmp_current_time) #pragma redefine_extname X509_cmp_time BORINGSSL_ADD_USER_LABEL_AND_PREFIX(X509_cmp_time) #pragma redefine_extname X509_cmp_time_posix BORINGSSL_ADD_USER_LABEL_AND_PREFIX(X509_cmp_time_posix) +#pragma redefine_extname X509_cmp_time_posix_nonstandard BORINGSSL_ADD_USER_LABEL_AND_PREFIX(X509_cmp_time_posix_nonstandard) #pragma redefine_extname X509_delete_ext BORINGSSL_ADD_USER_LABEL_AND_PREFIX(X509_delete_ext) #pragma redefine_extname X509_digest BORINGSSL_ADD_USER_LABEL_AND_PREFIX(X509_digest) #pragma redefine_extname X509_dup BORINGSSL_ADD_USER_LABEL_AND_PREFIX(X509_dup) @@ -3244,6 +3304,7 @@ #define ASN1_STRING_free BORINGSSL_ADD_PREFIX(ASN1_STRING_free) #define ASN1_STRING_get0_data BORINGSSL_ADD_PREFIX(ASN1_STRING_get0_data) #define ASN1_STRING_get_default_mask BORINGSSL_ADD_PREFIX(ASN1_STRING_get_default_mask) +#define ASN1_STRING_get_length BORINGSSL_ADD_PREFIX(ASN1_STRING_get_length) #define ASN1_STRING_length BORINGSSL_ADD_PREFIX(ASN1_STRING_length) #define ASN1_STRING_new BORINGSSL_ADD_PREFIX(ASN1_STRING_new) #define ASN1_STRING_print BORINGSSL_ADD_PREFIX(ASN1_STRING_print) @@ -3346,6 +3407,7 @@ #define BIO_ctrl_get_write_guarantee BORINGSSL_ADD_PREFIX(BIO_ctrl_get_write_guarantee) #define BIO_ctrl_pending BORINGSSL_ADD_PREFIX(BIO_ctrl_pending) #define BIO_do_connect BORINGSSL_ADD_PREFIX(BIO_do_connect) +#define BIO_dup_ref BORINGSSL_ADD_PREFIX(BIO_dup_ref) #define BIO_eof BORINGSSL_ADD_PREFIX(BIO_eof) #define BIO_f_base64 BORINGSSL_ADD_PREFIX(BIO_f_base64) #define BIO_f_ssl BORINGSSL_ADD_PREFIX(BIO_f_ssl) @@ -3388,6 +3450,7 @@ #define BIO_meth_set_puts BORINGSSL_ADD_PREFIX(BIO_meth_set_puts) #define BIO_meth_set_read BORINGSSL_ADD_PREFIX(BIO_meth_set_read) #define BIO_meth_set_write BORINGSSL_ADD_PREFIX(BIO_meth_set_write) +#define BIO_meth_set_write_ex BORINGSSL_ADD_PREFIX(BIO_meth_set_write_ex) #define BIO_method_type BORINGSSL_ADD_PREFIX(BIO_method_type) #define BIO_new BORINGSSL_ADD_PREFIX(BIO_new) #define BIO_new_bio_pair BORINGSSL_ADD_PREFIX(BIO_new_bio_pair) @@ -3451,6 +3514,7 @@ #define BIO_wpending BORINGSSL_ADD_PREFIX(BIO_wpending) #define BIO_write BORINGSSL_ADD_PREFIX(BIO_write) #define BIO_write_all BORINGSSL_ADD_PREFIX(BIO_write_all) +#define BIO_write_ex BORINGSSL_ADD_PREFIX(BIO_write_ex) #define BIO_write_filename BORINGSSL_ADD_PREFIX(BIO_write_filename) #define BLAKE2B256 BORINGSSL_ADD_PREFIX(BLAKE2B256) #define BLAKE2B256_Final BORINGSSL_ADD_PREFIX(BLAKE2B256_Final) @@ -3618,6 +3682,7 @@ #define CBB_add_asn1_octet_string BORINGSSL_ADD_PREFIX(CBB_add_asn1_octet_string) #define CBB_add_asn1_oid_component BORINGSSL_ADD_PREFIX(CBB_add_asn1_oid_component) #define CBB_add_asn1_oid_from_text BORINGSSL_ADD_PREFIX(CBB_add_asn1_oid_from_text) +#define CBB_add_asn1_relative_oid_from_der_to_text BORINGSSL_ADD_PREFIX(CBB_add_asn1_relative_oid_from_der_to_text) #define CBB_add_asn1_relative_oid_from_text BORINGSSL_ADD_PREFIX(CBB_add_asn1_relative_oid_from_text) #define CBB_add_asn1_uint64 BORINGSSL_ADD_PREFIX(CBB_add_asn1_uint64) #define CBB_add_asn1_uint64_with_tag BORINGSSL_ADD_PREFIX(CBB_add_asn1_uint64_with_tag) @@ -3631,6 +3696,7 @@ #define CBB_add_u24_length_prefixed BORINGSSL_ADD_PREFIX(CBB_add_u24_length_prefixed) #define CBB_add_u32 BORINGSSL_ADD_PREFIX(CBB_add_u32) #define CBB_add_u32le BORINGSSL_ADD_PREFIX(CBB_add_u32le) +#define CBB_add_u48 BORINGSSL_ADD_PREFIX(CBB_add_u48) #define CBB_add_u64 BORINGSSL_ADD_PREFIX(CBB_add_u64) #define CBB_add_u64le BORINGSSL_ADD_PREFIX(CBB_add_u64le) #define CBB_add_u8 BORINGSSL_ADD_PREFIX(CBB_add_u8) @@ -3666,6 +3732,7 @@ #define CBS_get_asn1_element BORINGSSL_ADD_PREFIX(CBS_get_asn1_element) #define CBS_get_asn1_int64 BORINGSSL_ADD_PREFIX(CBS_get_asn1_int64) #define CBS_get_asn1_int64_with_tag BORINGSSL_ADD_PREFIX(CBS_get_asn1_int64_with_tag) +#define CBS_get_asn1_oid_component BORINGSSL_ADD_PREFIX(CBS_get_asn1_oid_component) #define CBS_get_asn1_uint64 BORINGSSL_ADD_PREFIX(CBS_get_asn1_uint64) #define CBS_get_asn1_uint64_with_tag BORINGSSL_ADD_PREFIX(CBS_get_asn1_uint64_with_tag) #define CBS_get_bytes BORINGSSL_ADD_PREFIX(CBS_get_bytes) @@ -3682,6 +3749,7 @@ #define CBS_get_u24_length_prefixed BORINGSSL_ADD_PREFIX(CBS_get_u24_length_prefixed) #define CBS_get_u32 BORINGSSL_ADD_PREFIX(CBS_get_u32) #define CBS_get_u32le BORINGSSL_ADD_PREFIX(CBS_get_u32le) +#define CBS_get_u48 BORINGSSL_ADD_PREFIX(CBS_get_u48) #define CBS_get_u64 BORINGSSL_ADD_PREFIX(CBS_get_u64) #define CBS_get_u64_decimal BORINGSSL_ADD_PREFIX(CBS_get_u64_decimal) #define CBS_get_u64le BORINGSSL_ADD_PREFIX(CBS_get_u64le) @@ -3698,9 +3766,11 @@ #define CBS_is_valid_asn1_integer BORINGSSL_ADD_PREFIX(CBS_is_valid_asn1_integer) #define CBS_is_valid_asn1_oid BORINGSSL_ADD_PREFIX(CBS_is_valid_asn1_oid) #define CBS_is_valid_asn1_relative_oid BORINGSSL_ADD_PREFIX(CBS_is_valid_asn1_relative_oid) +#define CBS_is_valid_asn1_set_of BORINGSSL_ADD_PREFIX(CBS_is_valid_asn1_set_of) #define CBS_mem_equal BORINGSSL_ADD_PREFIX(CBS_mem_equal) #define CBS_parse_generalized_time BORINGSSL_ADD_PREFIX(CBS_parse_generalized_time) #define CBS_parse_utc_time BORINGSSL_ADD_PREFIX(CBS_parse_utc_time) +#define CBS_peek_any_asn1_tag BORINGSSL_ADD_PREFIX(CBS_peek_any_asn1_tag) #define CBS_peek_asn1_tag BORINGSSL_ADD_PREFIX(CBS_peek_asn1_tag) #define CBS_skip BORINGSSL_ADD_PREFIX(CBS_skip) #define CBS_stow BORINGSSL_ADD_PREFIX(CBS_stow) @@ -3722,9 +3792,14 @@ #define CONF_modules_free BORINGSSL_ADD_PREFIX(CONF_modules_free) #define CONF_modules_load_file BORINGSSL_ADD_PREFIX(CONF_modules_load_file) #define CONF_modules_unload BORINGSSL_ADD_PREFIX(CONF_modules_unload) +#define CPACE_CTX_free BORINGSSL_ADD_PREFIX(CPACE_CTX_free) +#define CPACE_CTX_new BORINGSSL_ADD_PREFIX(CPACE_CTX_new) +#define CPACE_generate_msg BORINGSSL_ADD_PREFIX(CPACE_generate_msg) +#define CPACE_process_msg BORINGSSL_ADD_PREFIX(CPACE_process_msg) #define CRL_DIST_POINTS_free BORINGSSL_ADD_PREFIX(CRL_DIST_POINTS_free) #define CRL_DIST_POINTS_it BORINGSSL_ADD_PREFIX(CRL_DIST_POINTS_it) #define CRL_DIST_POINTS_new BORINGSSL_ADD_PREFIX(CRL_DIST_POINTS_new) +#define CRYPTO_BUFFER_POOL_dup_ref BORINGSSL_ADD_PREFIX(CRYPTO_BUFFER_POOL_dup_ref) #define CRYPTO_BUFFER_POOL_free BORINGSSL_ADD_PREFIX(CRYPTO_BUFFER_POOL_free) #define CRYPTO_BUFFER_POOL_new BORINGSSL_ADD_PREFIX(CRYPTO_BUFFER_POOL_new) #define CRYPTO_BUFFER_POOL_up_ref BORINGSSL_ADD_PREFIX(CRYPTO_BUFFER_POOL_up_ref) @@ -3799,6 +3874,7 @@ #define DH_compute_key BORINGSSL_ADD_PREFIX(DH_compute_key) #define DH_compute_key_hashed BORINGSSL_ADD_PREFIX(DH_compute_key_hashed) #define DH_compute_key_padded BORINGSSL_ADD_PREFIX(DH_compute_key_padded) +#define DH_dup_ref BORINGSSL_ADD_PREFIX(DH_dup_ref) #define DH_free BORINGSSL_ADD_PREFIX(DH_free) #define DH_generate_key BORINGSSL_ADD_PREFIX(DH_generate_key) #define DH_generate_parameters BORINGSSL_ADD_PREFIX(DH_generate_parameters) @@ -3840,6 +3916,7 @@ #define DSA_do_sign BORINGSSL_ADD_PREFIX(DSA_do_sign) #define DSA_do_verify BORINGSSL_ADD_PREFIX(DSA_do_verify) #define DSA_dup_DH BORINGSSL_ADD_PREFIX(DSA_dup_DH) +#define DSA_dup_ref BORINGSSL_ADD_PREFIX(DSA_dup_ref) #define DSA_free BORINGSSL_ADD_PREFIX(DSA_free) #define DSA_generate_key BORINGSSL_ADD_PREFIX(DSA_generate_key) #define DSA_generate_parameters BORINGSSL_ADD_PREFIX(DSA_generate_parameters) @@ -3925,6 +4002,7 @@ #define EC_KEY_check_key BORINGSSL_ADD_PREFIX(EC_KEY_check_key) #define EC_KEY_derive_from_secret BORINGSSL_ADD_PREFIX(EC_KEY_derive_from_secret) #define EC_KEY_dup BORINGSSL_ADD_PREFIX(EC_KEY_dup) +#define EC_KEY_dup_ref BORINGSSL_ADD_PREFIX(EC_KEY_dup_ref) #define EC_KEY_free BORINGSSL_ADD_PREFIX(EC_KEY_free) #define EC_KEY_generate_key BORINGSSL_ADD_PREFIX(EC_KEY_generate_key) #define EC_KEY_generate_key_fips BORINGSSL_ADD_PREFIX(EC_KEY_generate_key_fips) @@ -3992,10 +4070,14 @@ #define EC_group_p521 BORINGSSL_ADD_PREFIX(EC_group_p521) #define EC_hash_to_curve_p256_xmd_sha256_sswu BORINGSSL_ADD_PREFIX(EC_hash_to_curve_p256_xmd_sha256_sswu) #define EC_hash_to_curve_p384_xmd_sha384_sswu BORINGSSL_ADD_PREFIX(EC_hash_to_curve_p384_xmd_sha384_sswu) +#define EC_wpa3_sae_hash_to_curve_p256 BORINGSSL_ADD_PREFIX(EC_wpa3_sae_hash_to_curve_p256) +#define EC_wpa3_sae_hunt_and_peck_p256 BORINGSSL_ADD_PREFIX(EC_wpa3_sae_hunt_and_peck_p256) #define ED25519_keypair BORINGSSL_ADD_PREFIX(ED25519_keypair) #define ED25519_keypair_from_seed BORINGSSL_ADD_PREFIX(ED25519_keypair_from_seed) #define ED25519_sign BORINGSSL_ADD_PREFIX(ED25519_sign) +#define ED25519_sign_prehashed BORINGSSL_ADD_PREFIX(ED25519_sign_prehashed) #define ED25519_verify BORINGSSL_ADD_PREFIX(ED25519_verify) +#define ED25519_verify_prehashed BORINGSSL_ADD_PREFIX(ED25519_verify_prehashed) #define EDIPARTYNAME_free BORINGSSL_ADD_PREFIX(EDIPARTYNAME_free) #define EDIPARTYNAME_new BORINGSSL_ADD_PREFIX(EDIPARTYNAME_new) #define ENGINE_cleanup BORINGSSL_ADD_PREFIX(ENGINE_cleanup) @@ -4076,6 +4158,8 @@ #define EVP_CIPHER_CTX_init BORINGSSL_ADD_PREFIX(EVP_CIPHER_CTX_init) #define EVP_CIPHER_CTX_iv_length BORINGSSL_ADD_PREFIX(EVP_CIPHER_CTX_iv_length) #define EVP_CIPHER_CTX_key_length BORINGSSL_ADD_PREFIX(EVP_CIPHER_CTX_key_length) +#define EVP_CIPHER_CTX_max_final BORINGSSL_ADD_PREFIX(EVP_CIPHER_CTX_max_final) +#define EVP_CIPHER_CTX_max_next_update BORINGSSL_ADD_PREFIX(EVP_CIPHER_CTX_max_next_update) #define EVP_CIPHER_CTX_mode BORINGSSL_ADD_PREFIX(EVP_CIPHER_CTX_mode) #define EVP_CIPHER_CTX_new BORINGSSL_ADD_PREFIX(EVP_CIPHER_CTX_new) #define EVP_CIPHER_CTX_nid BORINGSSL_ADD_PREFIX(EVP_CIPHER_CTX_nid) @@ -4123,6 +4207,7 @@ #define EVP_DigestSignFinal BORINGSSL_ADD_PREFIX(EVP_DigestSignFinal) #define EVP_DigestSignInit BORINGSSL_ADD_PREFIX(EVP_DigestSignInit) #define EVP_DigestSignUpdate BORINGSSL_ADD_PREFIX(EVP_DigestSignUpdate) +#define EVP_DigestSqueeze BORINGSSL_ADD_PREFIX(EVP_DigestSqueeze) #define EVP_DigestUpdate BORINGSSL_ADD_PREFIX(EVP_DigestUpdate) #define EVP_DigestVerify BORINGSSL_ADD_PREFIX(EVP_DigestVerify) #define EVP_DigestVerifyFinal BORINGSSL_ADD_PREFIX(EVP_DigestVerifyFinal) @@ -4158,6 +4243,7 @@ #define EVP_HPKE_CTX_setup_auth_sender BORINGSSL_ADD_PREFIX(EVP_HPKE_CTX_setup_auth_sender) #define EVP_HPKE_CTX_setup_auth_sender_with_seed_for_testing BORINGSSL_ADD_PREFIX(EVP_HPKE_CTX_setup_auth_sender_with_seed_for_testing) #define EVP_HPKE_CTX_setup_recipient BORINGSSL_ADD_PREFIX(EVP_HPKE_CTX_setup_recipient) +#define EVP_HPKE_CTX_setup_recipient_with_shared_secret BORINGSSL_ADD_PREFIX(EVP_HPKE_CTX_setup_recipient_with_shared_secret) #define EVP_HPKE_CTX_setup_sender BORINGSSL_ADD_PREFIX(EVP_HPKE_CTX_setup_sender) #define EVP_HPKE_CTX_setup_sender_with_seed_for_testing BORINGSSL_ADD_PREFIX(EVP_HPKE_CTX_setup_sender_with_seed_for_testing) #define EVP_HPKE_CTX_zero BORINGSSL_ADD_PREFIX(EVP_HPKE_CTX_zero) @@ -4167,6 +4253,7 @@ #define EVP_HPKE_KEM_id BORINGSSL_ADD_PREFIX(EVP_HPKE_KEM_id) #define EVP_HPKE_KEM_private_key_len BORINGSSL_ADD_PREFIX(EVP_HPKE_KEM_private_key_len) #define EVP_HPKE_KEM_public_key_len BORINGSSL_ADD_PREFIX(EVP_HPKE_KEM_public_key_len) +#define EVP_HPKE_KEM_shared_secret_len BORINGSSL_ADD_PREFIX(EVP_HPKE_KEM_shared_secret_len) #define EVP_HPKE_KEY_cleanup BORINGSSL_ADD_PREFIX(EVP_HPKE_KEY_cleanup) #define EVP_HPKE_KEY_copy BORINGSSL_ADD_PREFIX(EVP_HPKE_KEY_copy) #define EVP_HPKE_KEY_derive BORINGSSL_ADD_PREFIX(EVP_HPKE_KEY_derive) @@ -4182,6 +4269,8 @@ #define EVP_KEM_ciphertext_len BORINGSSL_ADD_PREFIX(EVP_KEM_ciphertext_len) #define EVP_KEM_decap BORINGSSL_ADD_PREFIX(EVP_KEM_decap) #define EVP_KEM_encap BORINGSSL_ADD_PREFIX(EVP_KEM_encap) +#define EVP_KEM_encap_external_entropy_for_testing BORINGSSL_ADD_PREFIX(EVP_KEM_encap_external_entropy_for_testing) +#define EVP_KEM_entropy_len_for_testing BORINGSSL_ADD_PREFIX(EVP_KEM_entropy_len_for_testing) #define EVP_KEM_secret_len BORINGSSL_ADD_PREFIX(EVP_KEM_secret_len) #define EVP_MD_CTX_block_size BORINGSSL_ADD_PREFIX(EVP_MD_CTX_block_size) #define EVP_MD_CTX_cleanse BORINGSSL_ADD_PREFIX(EVP_MD_CTX_cleanse) @@ -4274,10 +4363,15 @@ #define EVP_PKEY_encrypt_init BORINGSSL_ADD_PREFIX(EVP_PKEY_encrypt_init) #define EVP_PKEY_eq BORINGSSL_ADD_PREFIX(EVP_PKEY_eq) #define EVP_PKEY_free BORINGSSL_ADD_PREFIX(EVP_PKEY_free) +#define EVP_PKEY_from_ec_compressed_point BORINGSSL_ADD_PREFIX(EVP_PKEY_from_ec_compressed_point) +#define EVP_PKEY_from_ec_private_scalar BORINGSSL_ADD_PREFIX(EVP_PKEY_from_ec_private_scalar) +#define EVP_PKEY_from_ec_uncompressed_point BORINGSSL_ADD_PREFIX(EVP_PKEY_from_ec_uncompressed_point) #define EVP_PKEY_from_private_key_info BORINGSSL_ADD_PREFIX(EVP_PKEY_from_private_key_info) #define EVP_PKEY_from_private_seed BORINGSSL_ADD_PREFIX(EVP_PKEY_from_private_seed) #define EVP_PKEY_from_raw_private_key BORINGSSL_ADD_PREFIX(EVP_PKEY_from_raw_private_key) #define EVP_PKEY_from_raw_public_key BORINGSSL_ADD_PREFIX(EVP_PKEY_from_raw_public_key) +#define EVP_PKEY_from_rsa_private_key BORINGSSL_ADD_PREFIX(EVP_PKEY_from_rsa_private_key) +#define EVP_PKEY_from_rsa_public_key BORINGSSL_ADD_PREFIX(EVP_PKEY_from_rsa_public_key) #define EVP_PKEY_from_subject_public_key_info BORINGSSL_ADD_PREFIX(EVP_PKEY_from_subject_public_key_info) #define EVP_PKEY_generate_from_alg BORINGSSL_ADD_PREFIX(EVP_PKEY_generate_from_alg) #define EVP_PKEY_get0 BORINGSSL_ADD_PREFIX(EVP_PKEY_get0) @@ -4301,6 +4395,11 @@ #define EVP_PKEY_is_opaque BORINGSSL_ADD_PREFIX(EVP_PKEY_is_opaque) #define EVP_PKEY_keygen BORINGSSL_ADD_PREFIX(EVP_PKEY_keygen) #define EVP_PKEY_keygen_init BORINGSSL_ADD_PREFIX(EVP_PKEY_keygen_init) +#define EVP_PKEY_marshal_ec_compressed_point BORINGSSL_ADD_PREFIX(EVP_PKEY_marshal_ec_compressed_point) +#define EVP_PKEY_marshal_ec_private_scalar BORINGSSL_ADD_PREFIX(EVP_PKEY_marshal_ec_private_scalar) +#define EVP_PKEY_marshal_ec_uncompressed_point BORINGSSL_ADD_PREFIX(EVP_PKEY_marshal_ec_uncompressed_point) +#define EVP_PKEY_marshal_rsa_private_key BORINGSSL_ADD_PREFIX(EVP_PKEY_marshal_rsa_private_key) +#define EVP_PKEY_marshal_rsa_public_key BORINGSSL_ADD_PREFIX(EVP_PKEY_marshal_rsa_public_key) #define EVP_PKEY_missing_parameters BORINGSSL_ADD_PREFIX(EVP_PKEY_missing_parameters) #define EVP_PKEY_new BORINGSSL_ADD_PREFIX(EVP_PKEY_new) #define EVP_PKEY_new_raw_private_key BORINGSSL_ADD_PREFIX(EVP_PKEY_new_raw_private_key) @@ -4420,6 +4519,8 @@ #define EVP_hpke_p256_hkdf_sha256 BORINGSSL_ADD_PREFIX(EVP_hpke_p256_hkdf_sha256) #define EVP_hpke_x25519_hkdf_sha256 BORINGSSL_ADD_PREFIX(EVP_hpke_x25519_hkdf_sha256) #define EVP_hpke_xwing BORINGSSL_ADD_PREFIX(EVP_hpke_xwing) +#define EVP_kem_dhkem_p256_hkdf_sha256 BORINGSSL_ADD_PREFIX(EVP_kem_dhkem_p256_hkdf_sha256) +#define EVP_kem_dhkem_x25519_hkdf_sha256 BORINGSSL_ADD_PREFIX(EVP_kem_dhkem_x25519_hkdf_sha256) #define EVP_kem_ml_kem_1024 BORINGSSL_ADD_PREFIX(EVP_kem_ml_kem_1024) #define EVP_kem_ml_kem_768 BORINGSSL_ADD_PREFIX(EVP_kem_ml_kem_768) #define EVP_kem_xwing BORINGSSL_ADD_PREFIX(EVP_kem_xwing) @@ -4449,6 +4550,9 @@ #define EVP_pkey_rsa_pss_sha256 BORINGSSL_ADD_PREFIX(EVP_pkey_rsa_pss_sha256) #define EVP_pkey_rsa_pss_sha384 BORINGSSL_ADD_PREFIX(EVP_pkey_rsa_pss_sha384) #define EVP_pkey_rsa_pss_sha512 BORINGSSL_ADD_PREFIX(EVP_pkey_rsa_pss_sha512) +#define EVP_pkey_slh_dsa_sha2_128s BORINGSSL_ADD_PREFIX(EVP_pkey_slh_dsa_sha2_128s) +#define EVP_pkey_slh_dsa_sha2_128s_with_sha256 BORINGSSL_ADD_PREFIX(EVP_pkey_slh_dsa_sha2_128s_with_sha256) +#define EVP_pkey_slh_dsa_shake_256f BORINGSSL_ADD_PREFIX(EVP_pkey_slh_dsa_shake_256f) #define EVP_pkey_x25519 BORINGSSL_ADD_PREFIX(EVP_pkey_x25519) #define EVP_pkey_xwing BORINGSSL_ADD_PREFIX(EVP_pkey_xwing) #define EVP_rc2_40_cbc BORINGSSL_ADD_PREFIX(EVP_rc2_40_cbc) @@ -4458,8 +4562,13 @@ #define EVP_sha224 BORINGSSL_ADD_PREFIX(EVP_sha224) #define EVP_sha256 BORINGSSL_ADD_PREFIX(EVP_sha256) #define EVP_sha384 BORINGSSL_ADD_PREFIX(EVP_sha384) +#define EVP_sha3_256 BORINGSSL_ADD_PREFIX(EVP_sha3_256) +#define EVP_sha3_384 BORINGSSL_ADD_PREFIX(EVP_sha3_384) +#define EVP_sha3_512 BORINGSSL_ADD_PREFIX(EVP_sha3_512) #define EVP_sha512 BORINGSSL_ADD_PREFIX(EVP_sha512) #define EVP_sha512_256 BORINGSSL_ADD_PREFIX(EVP_sha512_256) +#define EVP_shake256 BORINGSSL_ADD_PREFIX(EVP_shake256) +#define EVP_turboshake256 BORINGSSL_ADD_PREFIX(EVP_turboshake256) #define EXTENDED_KEY_USAGE_free BORINGSSL_ADD_PREFIX(EXTENDED_KEY_USAGE_free) #define EXTENDED_KEY_USAGE_it BORINGSSL_ADD_PREFIX(EXTENDED_KEY_USAGE_it) #define EXTENDED_KEY_USAGE_new BORINGSSL_ADD_PREFIX(EXTENDED_KEY_USAGE_new) @@ -4518,6 +4627,8 @@ #define MD5_Init BORINGSSL_ADD_PREFIX(MD5_Init) #define MD5_Transform BORINGSSL_ADD_PREFIX(MD5_Transform) #define MD5_Update BORINGSSL_ADD_PREFIX(MD5_Update) +#define MD5_marshal_state BORINGSSL_ADD_PREFIX(MD5_marshal_state) +#define MD5_parse_state BORINGSSL_ADD_PREFIX(MD5_parse_state) #define METHOD_ref BORINGSSL_ADD_PREFIX(METHOD_ref) #define METHOD_unref BORINGSSL_ADD_PREFIX(METHOD_unref) #define MLDSA44_generate_key BORINGSSL_ADD_PREFIX(MLDSA44_generate_key) @@ -4649,6 +4760,7 @@ #define OPENSSL_sk_free BORINGSSL_ADD_PREFIX(OPENSSL_sk_free) #define OPENSSL_sk_insert BORINGSSL_ADD_PREFIX(OPENSSL_sk_insert) #define OPENSSL_sk_is_sorted BORINGSSL_ADD_PREFIX(OPENSSL_sk_is_sorted) +#define OPENSSL_sk_last BORINGSSL_ADD_PREFIX(OPENSSL_sk_last) #define OPENSSL_sk_new BORINGSSL_ADD_PREFIX(OPENSSL_sk_new) #define OPENSSL_sk_new_null BORINGSSL_ADD_PREFIX(OPENSSL_sk_new_null) #define OPENSSL_sk_num BORINGSSL_ADD_PREFIX(OPENSSL_sk_num) @@ -4828,7 +4940,6 @@ #define RAND_get_rand_method BORINGSSL_ADD_PREFIX(RAND_get_rand_method) #define RAND_get_system_entropy_for_custom_prng BORINGSSL_ADD_PREFIX(RAND_get_system_entropy_for_custom_prng) #define RAND_load_file BORINGSSL_ADD_PREFIX(RAND_load_file) -#define RAND_maybe_reseed BORINGSSL_ADD_PREFIX(RAND_maybe_reseed) #define RAND_poll BORINGSSL_ADD_PREFIX(RAND_poll) #define RAND_pseudo_bytes BORINGSSL_ADD_PREFIX(RAND_pseudo_bytes) #define RAND_reset_for_fuzzing BORINGSSL_ADD_PREFIX(RAND_reset_for_fuzzing) @@ -4855,6 +4966,7 @@ #define RSA_check_fips BORINGSSL_ADD_PREFIX(RSA_check_fips) #define RSA_check_key BORINGSSL_ADD_PREFIX(RSA_check_key) #define RSA_decrypt BORINGSSL_ADD_PREFIX(RSA_decrypt) +#define RSA_dup_ref BORINGSSL_ADD_PREFIX(RSA_dup_ref) #define RSA_encrypt BORINGSSL_ADD_PREFIX(RSA_encrypt) #define RSA_flags BORINGSSL_ADD_PREFIX(RSA_flags) #define RSA_free BORINGSSL_ADD_PREFIX(RSA_free) @@ -4917,27 +5029,21 @@ #define RSA_verify_PKCS1_PSS_mgf1 BORINGSSL_ADD_PREFIX(RSA_verify_PKCS1_PSS_mgf1) #define RSA_verify_pss_mgf1 BORINGSSL_ADD_PREFIX(RSA_verify_pss_mgf1) #define RSA_verify_raw BORINGSSL_ADD_PREFIX(RSA_verify_raw) -#define SHA1 BORINGSSL_ADD_PREFIX(SHA1) #define SHA1_Final BORINGSSL_ADD_PREFIX(SHA1_Final) #define SHA1_Init BORINGSSL_ADD_PREFIX(SHA1_Init) #define SHA1_Transform BORINGSSL_ADD_PREFIX(SHA1_Transform) #define SHA1_Update BORINGSSL_ADD_PREFIX(SHA1_Update) -#define SHA224 BORINGSSL_ADD_PREFIX(SHA224) #define SHA224_Final BORINGSSL_ADD_PREFIX(SHA224_Final) #define SHA224_Init BORINGSSL_ADD_PREFIX(SHA224_Init) #define SHA224_Update BORINGSSL_ADD_PREFIX(SHA224_Update) -#define SHA256 BORINGSSL_ADD_PREFIX(SHA256) #define SHA256_Final BORINGSSL_ADD_PREFIX(SHA256_Final) #define SHA256_Init BORINGSSL_ADD_PREFIX(SHA256_Init) #define SHA256_Transform BORINGSSL_ADD_PREFIX(SHA256_Transform) #define SHA256_TransformBlocks BORINGSSL_ADD_PREFIX(SHA256_TransformBlocks) #define SHA256_Update BORINGSSL_ADD_PREFIX(SHA256_Update) -#define SHA384 BORINGSSL_ADD_PREFIX(SHA384) #define SHA384_Final BORINGSSL_ADD_PREFIX(SHA384_Final) #define SHA384_Init BORINGSSL_ADD_PREFIX(SHA384_Init) #define SHA384_Update BORINGSSL_ADD_PREFIX(SHA384_Update) -#define SHA512 BORINGSSL_ADD_PREFIX(SHA512) -#define SHA512_256 BORINGSSL_ADD_PREFIX(SHA512_256) #define SHA512_256_Final BORINGSSL_ADD_PREFIX(SHA512_256_Final) #define SHA512_256_Init BORINGSSL_ADD_PREFIX(SHA512_256_Init) #define SHA512_256_Update BORINGSSL_ADD_PREFIX(SHA512_256_Update) @@ -4947,6 +5053,7 @@ #define SHA512_Update BORINGSSL_ADD_PREFIX(SHA512_Update) #define SIPHASH_24 BORINGSSL_ADD_PREFIX(SIPHASH_24) #define SLHDSA_SHA2_128S_generate_key BORINGSSL_ADD_PREFIX(SLHDSA_SHA2_128S_generate_key) +#define SLHDSA_SHA2_128S_generate_key_from_seed BORINGSSL_ADD_PREFIX(SLHDSA_SHA2_128S_generate_key_from_seed) #define SLHDSA_SHA2_128S_prehash_sign BORINGSSL_ADD_PREFIX(SLHDSA_SHA2_128S_prehash_sign) #define SLHDSA_SHA2_128S_prehash_verify BORINGSSL_ADD_PREFIX(SLHDSA_SHA2_128S_prehash_verify) #define SLHDSA_SHA2_128S_prehash_warning_nonstandard_sign BORINGSSL_ADD_PREFIX(SLHDSA_SHA2_128S_prehash_warning_nonstandard_sign) @@ -4955,6 +5062,7 @@ #define SLHDSA_SHA2_128S_sign BORINGSSL_ADD_PREFIX(SLHDSA_SHA2_128S_sign) #define SLHDSA_SHA2_128S_verify BORINGSSL_ADD_PREFIX(SLHDSA_SHA2_128S_verify) #define SLHDSA_SHAKE_256F_generate_key BORINGSSL_ADD_PREFIX(SLHDSA_SHAKE_256F_generate_key) +#define SLHDSA_SHAKE_256F_generate_key_from_seed BORINGSSL_ADD_PREFIX(SLHDSA_SHAKE_256F_generate_key_from_seed) #define SLHDSA_SHAKE_256F_public_from_private BORINGSSL_ADD_PREFIX(SLHDSA_SHAKE_256F_public_from_private) #define SLHDSA_SHAKE_256F_sign BORINGSSL_ADD_PREFIX(SLHDSA_SHAKE_256F_sign) #define SLHDSA_SHAKE_256F_verify BORINGSSL_ADD_PREFIX(SLHDSA_SHAKE_256F_verify) @@ -4986,8 +5094,10 @@ #define SSL_COMP_get_compression_methods BORINGSSL_ADD_PREFIX(SSL_COMP_get_compression_methods) #define SSL_COMP_get_id BORINGSSL_ADD_PREFIX(SSL_COMP_get_id) #define SSL_COMP_get_name BORINGSSL_ADD_PREFIX(SSL_COMP_get_name) +#define SSL_CREDENTIAL_add1_trust_anchor_group BORINGSSL_ADD_PREFIX(SSL_CREDENTIAL_add1_trust_anchor_group) #define SSL_CREDENTIAL_dup_ref BORINGSSL_ADD_PREFIX(SSL_CREDENTIAL_dup_ref) #define SSL_CREDENTIAL_free BORINGSSL_ADD_PREFIX(SSL_CREDENTIAL_free) +#define SSL_CREDENTIAL_get0_pre_shared_key_id BORINGSSL_ADD_PREFIX(SSL_CREDENTIAL_get0_pre_shared_key_id) #define SSL_CREDENTIAL_get_ex_data BORINGSSL_ADD_PREFIX(SSL_CREDENTIAL_get_ex_data) #define SSL_CREDENTIAL_get_ex_new_index BORINGSSL_ADD_PREFIX(SSL_CREDENTIAL_get_ex_new_index) #define SSL_CREDENTIAL_is_complete BORINGSSL_ADD_PREFIX(SSL_CREDENTIAL_is_complete) @@ -5003,6 +5113,7 @@ #define SSL_CREDENTIAL_set1_delegated_credential BORINGSSL_ADD_PREFIX(SSL_CREDENTIAL_set1_delegated_credential) #define SSL_CREDENTIAL_set1_ocsp_response BORINGSSL_ADD_PREFIX(SSL_CREDENTIAL_set1_ocsp_response) #define SSL_CREDENTIAL_set1_private_key BORINGSSL_ADD_PREFIX(SSL_CREDENTIAL_set1_private_key) +#define SSL_CREDENTIAL_set1_session_id_context BORINGSSL_ADD_PREFIX(SSL_CREDENTIAL_set1_session_id_context) #define SSL_CREDENTIAL_set1_signed_cert_timestamp_list BORINGSSL_ADD_PREFIX(SSL_CREDENTIAL_set1_signed_cert_timestamp_list) #define SSL_CREDENTIAL_set1_signing_algorithm_prefs BORINGSSL_ADD_PREFIX(SSL_CREDENTIAL_set1_signing_algorithm_prefs) #define SSL_CREDENTIAL_set1_trust_anchor_id BORINGSSL_ADD_PREFIX(SSL_CREDENTIAL_set1_trust_anchor_id) @@ -5023,6 +5134,7 @@ #define SSL_CTX_clear_extra_chain_certs BORINGSSL_ADD_PREFIX(SSL_CTX_clear_extra_chain_certs) #define SSL_CTX_clear_mode BORINGSSL_ADD_PREFIX(SSL_CTX_clear_mode) #define SSL_CTX_clear_options BORINGSSL_ADD_PREFIX(SSL_CTX_clear_options) +#define SSL_CTX_dup_ref BORINGSSL_ADD_PREFIX(SSL_CTX_dup_ref) #define SSL_CTX_enable_ocsp_stapling BORINGSSL_ADD_PREFIX(SSL_CTX_enable_ocsp_stapling) #define SSL_CTX_enable_signed_cert_timestamps BORINGSSL_ADD_PREFIX(SSL_CTX_enable_signed_cert_timestamps) #define SSL_CTX_enable_tls_channel_id BORINGSSL_ADD_PREFIX(SSL_CTX_enable_tls_channel_id) @@ -5103,12 +5215,14 @@ #define SSL_CTX_set1_requested_trust_anchors BORINGSSL_ADD_PREFIX(SSL_CTX_set1_requested_trust_anchors) #define SSL_CTX_set1_sigalgs BORINGSSL_ADD_PREFIX(SSL_CTX_set1_sigalgs) #define SSL_CTX_set1_sigalgs_list BORINGSSL_ADD_PREFIX(SSL_CTX_set1_sigalgs_list) +#define SSL_CTX_set1_tls13_ciphers BORINGSSL_ADD_PREFIX(SSL_CTX_set1_tls13_ciphers) #define SSL_CTX_set1_tls_channel_id BORINGSSL_ADD_PREFIX(SSL_CTX_set1_tls_channel_id) #define SSL_CTX_set1_verify_cert_store BORINGSSL_ADD_PREFIX(SSL_CTX_set1_verify_cert_store) #define SSL_CTX_set_allow_unknown_alpn_protos BORINGSSL_ADD_PREFIX(SSL_CTX_set_allow_unknown_alpn_protos) #define SSL_CTX_set_alpn_protos BORINGSSL_ADD_PREFIX(SSL_CTX_set_alpn_protos) #define SSL_CTX_set_alpn_select_cb BORINGSSL_ADD_PREFIX(SSL_CTX_set_alpn_select_cb) #define SSL_CTX_set_cert_cb BORINGSSL_ADD_PREFIX(SSL_CTX_set_cert_cb) +#define SSL_CTX_set_cert_cb_ex BORINGSSL_ADD_PREFIX(SSL_CTX_set_cert_cb_ex) #define SSL_CTX_set_cert_store BORINGSSL_ADD_PREFIX(SSL_CTX_set_cert_store) #define SSL_CTX_set_cert_verify_callback BORINGSSL_ADD_PREFIX(SSL_CTX_set_cert_verify_callback) #define SSL_CTX_set_chain_and_key BORINGSSL_ADD_PREFIX(SSL_CTX_set_chain_and_key) @@ -5192,12 +5306,14 @@ #define SSL_CTX_use_certificate_file BORINGSSL_ADD_PREFIX(SSL_CTX_use_certificate_file) #define SSL_CTX_use_psk_identity_hint BORINGSSL_ADD_PREFIX(SSL_CTX_use_psk_identity_hint) #define SSL_ECH_KEYS_add BORINGSSL_ADD_PREFIX(SSL_ECH_KEYS_add) +#define SSL_ECH_KEYS_dup_ref BORINGSSL_ADD_PREFIX(SSL_ECH_KEYS_dup_ref) #define SSL_ECH_KEYS_free BORINGSSL_ADD_PREFIX(SSL_ECH_KEYS_free) #define SSL_ECH_KEYS_has_duplicate_config_id BORINGSSL_ADD_PREFIX(SSL_ECH_KEYS_has_duplicate_config_id) #define SSL_ECH_KEYS_marshal_retry_configs BORINGSSL_ADD_PREFIX(SSL_ECH_KEYS_marshal_retry_configs) #define SSL_ECH_KEYS_new BORINGSSL_ADD_PREFIX(SSL_ECH_KEYS_new) #define SSL_ECH_KEYS_up_ref BORINGSSL_ADD_PREFIX(SSL_ECH_KEYS_up_ref) #define SSL_SESSION_copy_without_early_data BORINGSSL_ADD_PREFIX(SSL_SESSION_copy_without_early_data) +#define SSL_SESSION_dup_ref BORINGSSL_ADD_PREFIX(SSL_SESSION_dup_ref) #define SSL_SESSION_early_data_capable BORINGSSL_ADD_PREFIX(SSL_SESSION_early_data_capable) #define SSL_SESSION_free BORINGSSL_ADD_PREFIX(SSL_SESSION_free) #define SSL_SESSION_from_bytes BORINGSSL_ADD_PREFIX(SSL_SESSION_from_bytes) @@ -5346,6 +5462,7 @@ #define SSL_get_peer_full_cert_chain BORINGSSL_ADD_PREFIX(SSL_get_peer_full_cert_chain) #define SSL_get_peer_quic_transport_params BORINGSSL_ADD_PREFIX(SSL_get_peer_quic_transport_params) #define SSL_get_peer_signature_algorithm BORINGSSL_ADD_PREFIX(SSL_get_peer_signature_algorithm) +#define SSL_get_peer_tmp_key BORINGSSL_ADD_PREFIX(SSL_get_peer_tmp_key) #define SSL_get_pending_cipher BORINGSSL_ADD_PREFIX(SSL_get_pending_cipher) #define SSL_get_privatekey BORINGSSL_ADD_PREFIX(SSL_get_privatekey) #define SSL_get_psk_identity BORINGSSL_ADD_PREFIX(SSL_get_psk_identity) @@ -5449,6 +5566,7 @@ #define SSL_set1_server_supported_groups_hint BORINGSSL_ADD_PREFIX(SSL_set1_server_supported_groups_hint) #define SSL_set1_sigalgs BORINGSSL_ADD_PREFIX(SSL_set1_sigalgs) #define SSL_set1_sigalgs_list BORINGSSL_ADD_PREFIX(SSL_set1_sigalgs_list) +#define SSL_set1_tls13_ciphers BORINGSSL_ADD_PREFIX(SSL_set1_tls13_ciphers) #define SSL_set1_tls_channel_id BORINGSSL_ADD_PREFIX(SSL_set1_tls_channel_id) #define SSL_set1_verify_cert_store BORINGSSL_ADD_PREFIX(SSL_set1_verify_cert_store) #define SSL_set_SSL_CTX BORINGSSL_ADD_PREFIX(SSL_set_SSL_CTX) @@ -5457,6 +5575,7 @@ #define SSL_set_alps_use_new_codepoint BORINGSSL_ADD_PREFIX(SSL_set_alps_use_new_codepoint) #define SSL_set_bio BORINGSSL_ADD_PREFIX(SSL_set_bio) #define SSL_set_cert_cb BORINGSSL_ADD_PREFIX(SSL_set_cert_cb) +#define SSL_set_cert_cb_ex BORINGSSL_ADD_PREFIX(SSL_set_cert_cb_ex) #define SSL_set_chain_and_key BORINGSSL_ADD_PREFIX(SSL_set_chain_and_key) #define SSL_set_cipher_list BORINGSSL_ADD_PREFIX(SSL_set_cipher_list) #define SSL_set_client_CA_list BORINGSSL_ADD_PREFIX(SSL_set_client_CA_list) @@ -5465,7 +5584,6 @@ #define SSL_set_custom_verify BORINGSSL_ADD_PREFIX(SSL_set_custom_verify) #define SSL_set_early_data_enabled BORINGSSL_ADD_PREFIX(SSL_set_early_data_enabled) #define SSL_set_enable_ech_grease BORINGSSL_ADD_PREFIX(SSL_set_enable_ech_grease) -#define SSL_set_enforce_rsa_key_usage BORINGSSL_ADD_PREFIX(SSL_set_enforce_rsa_key_usage) #define SSL_set_ex_data BORINGSSL_ADD_PREFIX(SSL_set_ex_data) #define SSL_set_fd BORINGSSL_ADD_PREFIX(SSL_set_fd) #define SSL_set_handshake_hints BORINGSSL_ADD_PREFIX(SSL_set_handshake_hints) @@ -5493,6 +5611,7 @@ #define SSL_set_quic_use_legacy_codepoint BORINGSSL_ADD_PREFIX(SSL_set_quic_use_legacy_codepoint) #define SSL_set_quiet_shutdown BORINGSSL_ADD_PREFIX(SSL_set_quiet_shutdown) #define SSL_set_read_ahead BORINGSSL_ADD_PREFIX(SSL_set_read_ahead) +#define SSL_set_reject_unusable_ech_config BORINGSSL_ADD_PREFIX(SSL_set_reject_unusable_ech_config) #define SSL_set_renegotiate_mode BORINGSSL_ADD_PREFIX(SSL_set_renegotiate_mode) #define SSL_set_resumption_across_names_enabled BORINGSSL_ADD_PREFIX(SSL_set_resumption_across_names_enabled) #define SSL_set_retain_only_sha256_of_client_certs BORINGSSL_ADD_PREFIX(SSL_set_retain_only_sha256_of_client_certs) @@ -5542,7 +5661,6 @@ #define SSL_used_hello_retry_request BORINGSSL_ADD_PREFIX(SSL_used_hello_retry_request) #define SSL_version BORINGSSL_ADD_PREFIX(SSL_version) #define SSL_want BORINGSSL_ADD_PREFIX(SSL_want) -#define SSL_was_key_usage_invalid BORINGSSL_ADD_PREFIX(SSL_was_key_usage_invalid) #define SSL_write BORINGSSL_ADD_PREFIX(SSL_write) #define SSLeay BORINGSSL_ADD_PREFIX(SSLeay) #define SSLeay_version BORINGSSL_ADD_PREFIX(SSLeay_version) @@ -5646,6 +5764,7 @@ #define X509_CRL_delete_ext BORINGSSL_ADD_PREFIX(X509_CRL_delete_ext) #define X509_CRL_digest BORINGSSL_ADD_PREFIX(X509_CRL_digest) #define X509_CRL_dup BORINGSSL_ADD_PREFIX(X509_CRL_dup) +#define X509_CRL_dup_ref BORINGSSL_ADD_PREFIX(X509_CRL_dup_ref) #define X509_CRL_free BORINGSSL_ADD_PREFIX(X509_CRL_free) #define X509_CRL_get0_by_cert BORINGSSL_ADD_PREFIX(X509_CRL_get0_by_cert) #define X509_CRL_get0_by_serial BORINGSSL_ADD_PREFIX(X509_CRL_get0_by_serial) @@ -5812,7 +5931,6 @@ #define X509_STORE_CTX_free BORINGSSL_ADD_PREFIX(X509_STORE_CTX_free) #define X509_STORE_CTX_get0_cert BORINGSSL_ADD_PREFIX(X509_STORE_CTX_get0_cert) #define X509_STORE_CTX_get0_chain BORINGSSL_ADD_PREFIX(X509_STORE_CTX_get0_chain) -#define X509_STORE_CTX_get0_current_crl BORINGSSL_ADD_PREFIX(X509_STORE_CTX_get0_current_crl) #define X509_STORE_CTX_get0_param BORINGSSL_ADD_PREFIX(X509_STORE_CTX_get0_param) #define X509_STORE_CTX_get0_parent_ctx BORINGSSL_ADD_PREFIX(X509_STORE_CTX_get0_parent_ctx) #define X509_STORE_CTX_get0_store BORINGSSL_ADD_PREFIX(X509_STORE_CTX_get0_store) @@ -5848,6 +5966,7 @@ #define X509_STORE_add_cert BORINGSSL_ADD_PREFIX(X509_STORE_add_cert) #define X509_STORE_add_crl BORINGSSL_ADD_PREFIX(X509_STORE_add_crl) #define X509_STORE_add_lookup BORINGSSL_ADD_PREFIX(X509_STORE_add_lookup) +#define X509_STORE_dup_ref BORINGSSL_ADD_PREFIX(X509_STORE_dup_ref) #define X509_STORE_free BORINGSSL_ADD_PREFIX(X509_STORE_free) #define X509_STORE_get0_objects BORINGSSL_ADD_PREFIX(X509_STORE_get0_objects) #define X509_STORE_get0_param BORINGSSL_ADD_PREFIX(X509_STORE_get0_param) @@ -5903,6 +6022,7 @@ #define X509_cmp_current_time BORINGSSL_ADD_PREFIX(X509_cmp_current_time) #define X509_cmp_time BORINGSSL_ADD_PREFIX(X509_cmp_time) #define X509_cmp_time_posix BORINGSSL_ADD_PREFIX(X509_cmp_time_posix) +#define X509_cmp_time_posix_nonstandard BORINGSSL_ADD_PREFIX(X509_cmp_time_posix_nonstandard) #define X509_delete_ext BORINGSSL_ADD_PREFIX(X509_delete_ext) #define X509_digest BORINGSSL_ADD_PREFIX(X509_digest) #define X509_dup BORINGSSL_ADD_PREFIX(X509_dup) @@ -6289,6 +6409,7 @@ #pragma redefine_extname sk_ACCESS_DESCRIPTION_free BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_ACCESS_DESCRIPTION_free) #pragma redefine_extname sk_ACCESS_DESCRIPTION_insert BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_ACCESS_DESCRIPTION_insert) #pragma redefine_extname sk_ACCESS_DESCRIPTION_is_sorted BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_ACCESS_DESCRIPTION_is_sorted) +#pragma redefine_extname sk_ACCESS_DESCRIPTION_last BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_ACCESS_DESCRIPTION_last) #pragma redefine_extname sk_ACCESS_DESCRIPTION_new BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_ACCESS_DESCRIPTION_new) #pragma redefine_extname sk_ACCESS_DESCRIPTION_new_null BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_ACCESS_DESCRIPTION_new_null) #pragma redefine_extname sk_ACCESS_DESCRIPTION_num BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_ACCESS_DESCRIPTION_num) @@ -6315,6 +6436,7 @@ #pragma redefine_extname sk_ASN1_INTEGER_free BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_ASN1_INTEGER_free) #pragma redefine_extname sk_ASN1_INTEGER_insert BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_ASN1_INTEGER_insert) #pragma redefine_extname sk_ASN1_INTEGER_is_sorted BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_ASN1_INTEGER_is_sorted) +#pragma redefine_extname sk_ASN1_INTEGER_last BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_ASN1_INTEGER_last) #pragma redefine_extname sk_ASN1_INTEGER_new BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_ASN1_INTEGER_new) #pragma redefine_extname sk_ASN1_INTEGER_new_null BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_ASN1_INTEGER_new_null) #pragma redefine_extname sk_ASN1_INTEGER_num BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_ASN1_INTEGER_num) @@ -6341,6 +6463,7 @@ #pragma redefine_extname sk_ASN1_OBJECT_free BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_ASN1_OBJECT_free) #pragma redefine_extname sk_ASN1_OBJECT_insert BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_ASN1_OBJECT_insert) #pragma redefine_extname sk_ASN1_OBJECT_is_sorted BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_ASN1_OBJECT_is_sorted) +#pragma redefine_extname sk_ASN1_OBJECT_last BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_ASN1_OBJECT_last) #pragma redefine_extname sk_ASN1_OBJECT_new BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_ASN1_OBJECT_new) #pragma redefine_extname sk_ASN1_OBJECT_new_null BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_ASN1_OBJECT_new_null) #pragma redefine_extname sk_ASN1_OBJECT_num BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_ASN1_OBJECT_num) @@ -6367,6 +6490,7 @@ #pragma redefine_extname sk_ASN1_TYPE_free BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_ASN1_TYPE_free) #pragma redefine_extname sk_ASN1_TYPE_insert BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_ASN1_TYPE_insert) #pragma redefine_extname sk_ASN1_TYPE_is_sorted BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_ASN1_TYPE_is_sorted) +#pragma redefine_extname sk_ASN1_TYPE_last BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_ASN1_TYPE_last) #pragma redefine_extname sk_ASN1_TYPE_new BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_ASN1_TYPE_new) #pragma redefine_extname sk_ASN1_TYPE_new_null BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_ASN1_TYPE_new_null) #pragma redefine_extname sk_ASN1_TYPE_num BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_ASN1_TYPE_num) @@ -6393,6 +6517,7 @@ #pragma redefine_extname sk_ASN1_VALUE_free BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_ASN1_VALUE_free) #pragma redefine_extname sk_ASN1_VALUE_insert BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_ASN1_VALUE_insert) #pragma redefine_extname sk_ASN1_VALUE_is_sorted BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_ASN1_VALUE_is_sorted) +#pragma redefine_extname sk_ASN1_VALUE_last BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_ASN1_VALUE_last) #pragma redefine_extname sk_ASN1_VALUE_new BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_ASN1_VALUE_new) #pragma redefine_extname sk_ASN1_VALUE_new_null BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_ASN1_VALUE_new_null) #pragma redefine_extname sk_ASN1_VALUE_num BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_ASN1_VALUE_num) @@ -6419,6 +6544,7 @@ #pragma redefine_extname sk_BIO_free BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_BIO_free) #pragma redefine_extname sk_BIO_insert BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_BIO_insert) #pragma redefine_extname sk_BIO_is_sorted BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_BIO_is_sorted) +#pragma redefine_extname sk_BIO_last BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_BIO_last) #pragma redefine_extname sk_BIO_new BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_BIO_new) #pragma redefine_extname sk_BIO_new_null BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_BIO_new_null) #pragma redefine_extname sk_BIO_num BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_BIO_num) @@ -6445,6 +6571,7 @@ #pragma redefine_extname sk_CONF_VALUE_free BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_CONF_VALUE_free) #pragma redefine_extname sk_CONF_VALUE_insert BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_CONF_VALUE_insert) #pragma redefine_extname sk_CONF_VALUE_is_sorted BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_CONF_VALUE_is_sorted) +#pragma redefine_extname sk_CONF_VALUE_last BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_CONF_VALUE_last) #pragma redefine_extname sk_CONF_VALUE_new BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_CONF_VALUE_new) #pragma redefine_extname sk_CONF_VALUE_new_null BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_CONF_VALUE_new_null) #pragma redefine_extname sk_CONF_VALUE_num BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_CONF_VALUE_num) @@ -6471,6 +6598,7 @@ #pragma redefine_extname sk_CRYPTO_BUFFER_free BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_CRYPTO_BUFFER_free) #pragma redefine_extname sk_CRYPTO_BUFFER_insert BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_CRYPTO_BUFFER_insert) #pragma redefine_extname sk_CRYPTO_BUFFER_is_sorted BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_CRYPTO_BUFFER_is_sorted) +#pragma redefine_extname sk_CRYPTO_BUFFER_last BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_CRYPTO_BUFFER_last) #pragma redefine_extname sk_CRYPTO_BUFFER_new BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_CRYPTO_BUFFER_new) #pragma redefine_extname sk_CRYPTO_BUFFER_new_null BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_CRYPTO_BUFFER_new_null) #pragma redefine_extname sk_CRYPTO_BUFFER_num BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_CRYPTO_BUFFER_num) @@ -6497,6 +6625,7 @@ #pragma redefine_extname sk_DIST_POINT_free BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_DIST_POINT_free) #pragma redefine_extname sk_DIST_POINT_insert BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_DIST_POINT_insert) #pragma redefine_extname sk_DIST_POINT_is_sorted BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_DIST_POINT_is_sorted) +#pragma redefine_extname sk_DIST_POINT_last BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_DIST_POINT_last) #pragma redefine_extname sk_DIST_POINT_new BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_DIST_POINT_new) #pragma redefine_extname sk_DIST_POINT_new_null BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_DIST_POINT_new_null) #pragma redefine_extname sk_DIST_POINT_num BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_DIST_POINT_num) @@ -6523,6 +6652,7 @@ #pragma redefine_extname sk_GENERAL_NAME_free BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_GENERAL_NAME_free) #pragma redefine_extname sk_GENERAL_NAME_insert BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_GENERAL_NAME_insert) #pragma redefine_extname sk_GENERAL_NAME_is_sorted BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_GENERAL_NAME_is_sorted) +#pragma redefine_extname sk_GENERAL_NAME_last BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_GENERAL_NAME_last) #pragma redefine_extname sk_GENERAL_NAME_new BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_GENERAL_NAME_new) #pragma redefine_extname sk_GENERAL_NAME_new_null BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_GENERAL_NAME_new_null) #pragma redefine_extname sk_GENERAL_NAME_num BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_GENERAL_NAME_num) @@ -6549,6 +6679,7 @@ #pragma redefine_extname sk_GENERAL_SUBTREE_free BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_GENERAL_SUBTREE_free) #pragma redefine_extname sk_GENERAL_SUBTREE_insert BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_GENERAL_SUBTREE_insert) #pragma redefine_extname sk_GENERAL_SUBTREE_is_sorted BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_GENERAL_SUBTREE_is_sorted) +#pragma redefine_extname sk_GENERAL_SUBTREE_last BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_GENERAL_SUBTREE_last) #pragma redefine_extname sk_GENERAL_SUBTREE_new BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_GENERAL_SUBTREE_new) #pragma redefine_extname sk_GENERAL_SUBTREE_new_null BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_GENERAL_SUBTREE_new_null) #pragma redefine_extname sk_GENERAL_SUBTREE_num BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_GENERAL_SUBTREE_num) @@ -6575,6 +6706,7 @@ #pragma redefine_extname sk_OPENSSL_STRING_free BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_OPENSSL_STRING_free) #pragma redefine_extname sk_OPENSSL_STRING_insert BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_OPENSSL_STRING_insert) #pragma redefine_extname sk_OPENSSL_STRING_is_sorted BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_OPENSSL_STRING_is_sorted) +#pragma redefine_extname sk_OPENSSL_STRING_last BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_OPENSSL_STRING_last) #pragma redefine_extname sk_OPENSSL_STRING_new BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_OPENSSL_STRING_new) #pragma redefine_extname sk_OPENSSL_STRING_new_null BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_OPENSSL_STRING_new_null) #pragma redefine_extname sk_OPENSSL_STRING_num BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_OPENSSL_STRING_num) @@ -6601,6 +6733,7 @@ #pragma redefine_extname sk_POLICYINFO_free BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_POLICYINFO_free) #pragma redefine_extname sk_POLICYINFO_insert BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_POLICYINFO_insert) #pragma redefine_extname sk_POLICYINFO_is_sorted BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_POLICYINFO_is_sorted) +#pragma redefine_extname sk_POLICYINFO_last BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_POLICYINFO_last) #pragma redefine_extname sk_POLICYINFO_new BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_POLICYINFO_new) #pragma redefine_extname sk_POLICYINFO_new_null BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_POLICYINFO_new_null) #pragma redefine_extname sk_POLICYINFO_num BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_POLICYINFO_num) @@ -6627,6 +6760,7 @@ #pragma redefine_extname sk_POLICYQUALINFO_free BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_POLICYQUALINFO_free) #pragma redefine_extname sk_POLICYQUALINFO_insert BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_POLICYQUALINFO_insert) #pragma redefine_extname sk_POLICYQUALINFO_is_sorted BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_POLICYQUALINFO_is_sorted) +#pragma redefine_extname sk_POLICYQUALINFO_last BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_POLICYQUALINFO_last) #pragma redefine_extname sk_POLICYQUALINFO_new BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_POLICYQUALINFO_new) #pragma redefine_extname sk_POLICYQUALINFO_new_null BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_POLICYQUALINFO_new_null) #pragma redefine_extname sk_POLICYQUALINFO_num BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_POLICYQUALINFO_num) @@ -6653,6 +6787,7 @@ #pragma redefine_extname sk_POLICY_MAPPING_free BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_POLICY_MAPPING_free) #pragma redefine_extname sk_POLICY_MAPPING_insert BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_POLICY_MAPPING_insert) #pragma redefine_extname sk_POLICY_MAPPING_is_sorted BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_POLICY_MAPPING_is_sorted) +#pragma redefine_extname sk_POLICY_MAPPING_last BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_POLICY_MAPPING_last) #pragma redefine_extname sk_POLICY_MAPPING_new BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_POLICY_MAPPING_new) #pragma redefine_extname sk_POLICY_MAPPING_new_null BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_POLICY_MAPPING_new_null) #pragma redefine_extname sk_POLICY_MAPPING_num BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_POLICY_MAPPING_num) @@ -6679,6 +6814,7 @@ #pragma redefine_extname sk_SRTP_PROTECTION_PROFILE_free BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_SRTP_PROTECTION_PROFILE_free) #pragma redefine_extname sk_SRTP_PROTECTION_PROFILE_insert BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_SRTP_PROTECTION_PROFILE_insert) #pragma redefine_extname sk_SRTP_PROTECTION_PROFILE_is_sorted BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_SRTP_PROTECTION_PROFILE_is_sorted) +#pragma redefine_extname sk_SRTP_PROTECTION_PROFILE_last BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_SRTP_PROTECTION_PROFILE_last) #pragma redefine_extname sk_SRTP_PROTECTION_PROFILE_new BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_SRTP_PROTECTION_PROFILE_new) #pragma redefine_extname sk_SRTP_PROTECTION_PROFILE_new_null BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_SRTP_PROTECTION_PROFILE_new_null) #pragma redefine_extname sk_SRTP_PROTECTION_PROFILE_num BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_SRTP_PROTECTION_PROFILE_num) @@ -6705,6 +6841,7 @@ #pragma redefine_extname sk_SSL_CIPHER_free BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_SSL_CIPHER_free) #pragma redefine_extname sk_SSL_CIPHER_insert BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_SSL_CIPHER_insert) #pragma redefine_extname sk_SSL_CIPHER_is_sorted BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_SSL_CIPHER_is_sorted) +#pragma redefine_extname sk_SSL_CIPHER_last BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_SSL_CIPHER_last) #pragma redefine_extname sk_SSL_CIPHER_new BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_SSL_CIPHER_new) #pragma redefine_extname sk_SSL_CIPHER_new_null BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_SSL_CIPHER_new_null) #pragma redefine_extname sk_SSL_CIPHER_num BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_SSL_CIPHER_num) @@ -6731,6 +6868,7 @@ #pragma redefine_extname sk_SSL_COMP_free BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_SSL_COMP_free) #pragma redefine_extname sk_SSL_COMP_insert BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_SSL_COMP_insert) #pragma redefine_extname sk_SSL_COMP_is_sorted BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_SSL_COMP_is_sorted) +#pragma redefine_extname sk_SSL_COMP_last BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_SSL_COMP_last) #pragma redefine_extname sk_SSL_COMP_new BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_SSL_COMP_new) #pragma redefine_extname sk_SSL_COMP_new_null BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_SSL_COMP_new_null) #pragma redefine_extname sk_SSL_COMP_num BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_SSL_COMP_num) @@ -6757,6 +6895,7 @@ #pragma redefine_extname sk_TRUST_TOKEN_free BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_TRUST_TOKEN_free) #pragma redefine_extname sk_TRUST_TOKEN_insert BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_TRUST_TOKEN_insert) #pragma redefine_extname sk_TRUST_TOKEN_is_sorted BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_TRUST_TOKEN_is_sorted) +#pragma redefine_extname sk_TRUST_TOKEN_last BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_TRUST_TOKEN_last) #pragma redefine_extname sk_TRUST_TOKEN_new BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_TRUST_TOKEN_new) #pragma redefine_extname sk_TRUST_TOKEN_new_null BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_TRUST_TOKEN_new_null) #pragma redefine_extname sk_TRUST_TOKEN_num BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_TRUST_TOKEN_num) @@ -6783,6 +6922,7 @@ #pragma redefine_extname sk_X509_ALGOR_free BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_X509_ALGOR_free) #pragma redefine_extname sk_X509_ALGOR_insert BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_X509_ALGOR_insert) #pragma redefine_extname sk_X509_ALGOR_is_sorted BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_X509_ALGOR_is_sorted) +#pragma redefine_extname sk_X509_ALGOR_last BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_X509_ALGOR_last) #pragma redefine_extname sk_X509_ALGOR_new BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_X509_ALGOR_new) #pragma redefine_extname sk_X509_ALGOR_new_null BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_X509_ALGOR_new_null) #pragma redefine_extname sk_X509_ALGOR_num BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_X509_ALGOR_num) @@ -6809,6 +6949,7 @@ #pragma redefine_extname sk_X509_ATTRIBUTE_free BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_X509_ATTRIBUTE_free) #pragma redefine_extname sk_X509_ATTRIBUTE_insert BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_X509_ATTRIBUTE_insert) #pragma redefine_extname sk_X509_ATTRIBUTE_is_sorted BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_X509_ATTRIBUTE_is_sorted) +#pragma redefine_extname sk_X509_ATTRIBUTE_last BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_X509_ATTRIBUTE_last) #pragma redefine_extname sk_X509_ATTRIBUTE_new BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_X509_ATTRIBUTE_new) #pragma redefine_extname sk_X509_ATTRIBUTE_new_null BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_X509_ATTRIBUTE_new_null) #pragma redefine_extname sk_X509_ATTRIBUTE_num BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_X509_ATTRIBUTE_num) @@ -6835,6 +6976,7 @@ #pragma redefine_extname sk_X509_CRL_free BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_X509_CRL_free) #pragma redefine_extname sk_X509_CRL_insert BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_X509_CRL_insert) #pragma redefine_extname sk_X509_CRL_is_sorted BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_X509_CRL_is_sorted) +#pragma redefine_extname sk_X509_CRL_last BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_X509_CRL_last) #pragma redefine_extname sk_X509_CRL_new BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_X509_CRL_new) #pragma redefine_extname sk_X509_CRL_new_null BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_X509_CRL_new_null) #pragma redefine_extname sk_X509_CRL_num BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_X509_CRL_num) @@ -6861,6 +7003,7 @@ #pragma redefine_extname sk_X509_EXTENSION_free BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_X509_EXTENSION_free) #pragma redefine_extname sk_X509_EXTENSION_insert BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_X509_EXTENSION_insert) #pragma redefine_extname sk_X509_EXTENSION_is_sorted BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_X509_EXTENSION_is_sorted) +#pragma redefine_extname sk_X509_EXTENSION_last BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_X509_EXTENSION_last) #pragma redefine_extname sk_X509_EXTENSION_new BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_X509_EXTENSION_new) #pragma redefine_extname sk_X509_EXTENSION_new_null BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_X509_EXTENSION_new_null) #pragma redefine_extname sk_X509_EXTENSION_num BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_X509_EXTENSION_num) @@ -6887,6 +7030,7 @@ #pragma redefine_extname sk_X509_INFO_free BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_X509_INFO_free) #pragma redefine_extname sk_X509_INFO_insert BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_X509_INFO_insert) #pragma redefine_extname sk_X509_INFO_is_sorted BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_X509_INFO_is_sorted) +#pragma redefine_extname sk_X509_INFO_last BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_X509_INFO_last) #pragma redefine_extname sk_X509_INFO_new BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_X509_INFO_new) #pragma redefine_extname sk_X509_INFO_new_null BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_X509_INFO_new_null) #pragma redefine_extname sk_X509_INFO_num BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_X509_INFO_num) @@ -6913,6 +7057,7 @@ #pragma redefine_extname sk_X509_NAME_ENTRY_free BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_X509_NAME_ENTRY_free) #pragma redefine_extname sk_X509_NAME_ENTRY_insert BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_X509_NAME_ENTRY_insert) #pragma redefine_extname sk_X509_NAME_ENTRY_is_sorted BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_X509_NAME_ENTRY_is_sorted) +#pragma redefine_extname sk_X509_NAME_ENTRY_last BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_X509_NAME_ENTRY_last) #pragma redefine_extname sk_X509_NAME_ENTRY_new BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_X509_NAME_ENTRY_new) #pragma redefine_extname sk_X509_NAME_ENTRY_new_null BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_X509_NAME_ENTRY_new_null) #pragma redefine_extname sk_X509_NAME_ENTRY_num BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_X509_NAME_ENTRY_num) @@ -6939,6 +7084,7 @@ #pragma redefine_extname sk_X509_NAME_free BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_X509_NAME_free) #pragma redefine_extname sk_X509_NAME_insert BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_X509_NAME_insert) #pragma redefine_extname sk_X509_NAME_is_sorted BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_X509_NAME_is_sorted) +#pragma redefine_extname sk_X509_NAME_last BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_X509_NAME_last) #pragma redefine_extname sk_X509_NAME_new BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_X509_NAME_new) #pragma redefine_extname sk_X509_NAME_new_null BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_X509_NAME_new_null) #pragma redefine_extname sk_X509_NAME_num BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_X509_NAME_num) @@ -6965,6 +7111,7 @@ #pragma redefine_extname sk_X509_OBJECT_free BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_X509_OBJECT_free) #pragma redefine_extname sk_X509_OBJECT_insert BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_X509_OBJECT_insert) #pragma redefine_extname sk_X509_OBJECT_is_sorted BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_X509_OBJECT_is_sorted) +#pragma redefine_extname sk_X509_OBJECT_last BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_X509_OBJECT_last) #pragma redefine_extname sk_X509_OBJECT_new BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_X509_OBJECT_new) #pragma redefine_extname sk_X509_OBJECT_new_null BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_X509_OBJECT_new_null) #pragma redefine_extname sk_X509_OBJECT_num BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_X509_OBJECT_num) @@ -6991,6 +7138,7 @@ #pragma redefine_extname sk_X509_REVOKED_free BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_X509_REVOKED_free) #pragma redefine_extname sk_X509_REVOKED_insert BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_X509_REVOKED_insert) #pragma redefine_extname sk_X509_REVOKED_is_sorted BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_X509_REVOKED_is_sorted) +#pragma redefine_extname sk_X509_REVOKED_last BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_X509_REVOKED_last) #pragma redefine_extname sk_X509_REVOKED_new BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_X509_REVOKED_new) #pragma redefine_extname sk_X509_REVOKED_new_null BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_X509_REVOKED_new_null) #pragma redefine_extname sk_X509_REVOKED_num BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_X509_REVOKED_num) @@ -7017,6 +7165,7 @@ #pragma redefine_extname sk_X509_free BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_X509_free) #pragma redefine_extname sk_X509_insert BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_X509_insert) #pragma redefine_extname sk_X509_is_sorted BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_X509_is_sorted) +#pragma redefine_extname sk_X509_last BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_X509_last) #pragma redefine_extname sk_X509_new BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_X509_new) #pragma redefine_extname sk_X509_new_null BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_X509_new_null) #pragma redefine_extname sk_X509_num BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_X509_num) @@ -7043,6 +7192,7 @@ #pragma redefine_extname sk_void_free BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_void_free) #pragma redefine_extname sk_void_insert BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_void_insert) #pragma redefine_extname sk_void_is_sorted BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_void_is_sorted) +#pragma redefine_extname sk_void_last BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_void_last) #pragma redefine_extname sk_void_new BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_void_new) #pragma redefine_extname sk_void_new_null BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_void_new_null) #pragma redefine_extname sk_void_num BORINGSSL_ADD_USER_LABEL_AND_PREFIX(sk_void_num) @@ -7079,6 +7229,7 @@ #define sk_ACCESS_DESCRIPTION_free BORINGSSL_ADD_PREFIX(sk_ACCESS_DESCRIPTION_free) #define sk_ACCESS_DESCRIPTION_insert BORINGSSL_ADD_PREFIX(sk_ACCESS_DESCRIPTION_insert) #define sk_ACCESS_DESCRIPTION_is_sorted BORINGSSL_ADD_PREFIX(sk_ACCESS_DESCRIPTION_is_sorted) +#define sk_ACCESS_DESCRIPTION_last BORINGSSL_ADD_PREFIX(sk_ACCESS_DESCRIPTION_last) #define sk_ACCESS_DESCRIPTION_new BORINGSSL_ADD_PREFIX(sk_ACCESS_DESCRIPTION_new) #define sk_ACCESS_DESCRIPTION_new_null BORINGSSL_ADD_PREFIX(sk_ACCESS_DESCRIPTION_new_null) #define sk_ACCESS_DESCRIPTION_num BORINGSSL_ADD_PREFIX(sk_ACCESS_DESCRIPTION_num) @@ -7105,6 +7256,7 @@ #define sk_ASN1_INTEGER_free BORINGSSL_ADD_PREFIX(sk_ASN1_INTEGER_free) #define sk_ASN1_INTEGER_insert BORINGSSL_ADD_PREFIX(sk_ASN1_INTEGER_insert) #define sk_ASN1_INTEGER_is_sorted BORINGSSL_ADD_PREFIX(sk_ASN1_INTEGER_is_sorted) +#define sk_ASN1_INTEGER_last BORINGSSL_ADD_PREFIX(sk_ASN1_INTEGER_last) #define sk_ASN1_INTEGER_new BORINGSSL_ADD_PREFIX(sk_ASN1_INTEGER_new) #define sk_ASN1_INTEGER_new_null BORINGSSL_ADD_PREFIX(sk_ASN1_INTEGER_new_null) #define sk_ASN1_INTEGER_num BORINGSSL_ADD_PREFIX(sk_ASN1_INTEGER_num) @@ -7131,6 +7283,7 @@ #define sk_ASN1_OBJECT_free BORINGSSL_ADD_PREFIX(sk_ASN1_OBJECT_free) #define sk_ASN1_OBJECT_insert BORINGSSL_ADD_PREFIX(sk_ASN1_OBJECT_insert) #define sk_ASN1_OBJECT_is_sorted BORINGSSL_ADD_PREFIX(sk_ASN1_OBJECT_is_sorted) +#define sk_ASN1_OBJECT_last BORINGSSL_ADD_PREFIX(sk_ASN1_OBJECT_last) #define sk_ASN1_OBJECT_new BORINGSSL_ADD_PREFIX(sk_ASN1_OBJECT_new) #define sk_ASN1_OBJECT_new_null BORINGSSL_ADD_PREFIX(sk_ASN1_OBJECT_new_null) #define sk_ASN1_OBJECT_num BORINGSSL_ADD_PREFIX(sk_ASN1_OBJECT_num) @@ -7157,6 +7310,7 @@ #define sk_ASN1_TYPE_free BORINGSSL_ADD_PREFIX(sk_ASN1_TYPE_free) #define sk_ASN1_TYPE_insert BORINGSSL_ADD_PREFIX(sk_ASN1_TYPE_insert) #define sk_ASN1_TYPE_is_sorted BORINGSSL_ADD_PREFIX(sk_ASN1_TYPE_is_sorted) +#define sk_ASN1_TYPE_last BORINGSSL_ADD_PREFIX(sk_ASN1_TYPE_last) #define sk_ASN1_TYPE_new BORINGSSL_ADD_PREFIX(sk_ASN1_TYPE_new) #define sk_ASN1_TYPE_new_null BORINGSSL_ADD_PREFIX(sk_ASN1_TYPE_new_null) #define sk_ASN1_TYPE_num BORINGSSL_ADD_PREFIX(sk_ASN1_TYPE_num) @@ -7183,6 +7337,7 @@ #define sk_ASN1_VALUE_free BORINGSSL_ADD_PREFIX(sk_ASN1_VALUE_free) #define sk_ASN1_VALUE_insert BORINGSSL_ADD_PREFIX(sk_ASN1_VALUE_insert) #define sk_ASN1_VALUE_is_sorted BORINGSSL_ADD_PREFIX(sk_ASN1_VALUE_is_sorted) +#define sk_ASN1_VALUE_last BORINGSSL_ADD_PREFIX(sk_ASN1_VALUE_last) #define sk_ASN1_VALUE_new BORINGSSL_ADD_PREFIX(sk_ASN1_VALUE_new) #define sk_ASN1_VALUE_new_null BORINGSSL_ADD_PREFIX(sk_ASN1_VALUE_new_null) #define sk_ASN1_VALUE_num BORINGSSL_ADD_PREFIX(sk_ASN1_VALUE_num) @@ -7209,6 +7364,7 @@ #define sk_BIO_free BORINGSSL_ADD_PREFIX(sk_BIO_free) #define sk_BIO_insert BORINGSSL_ADD_PREFIX(sk_BIO_insert) #define sk_BIO_is_sorted BORINGSSL_ADD_PREFIX(sk_BIO_is_sorted) +#define sk_BIO_last BORINGSSL_ADD_PREFIX(sk_BIO_last) #define sk_BIO_new BORINGSSL_ADD_PREFIX(sk_BIO_new) #define sk_BIO_new_null BORINGSSL_ADD_PREFIX(sk_BIO_new_null) #define sk_BIO_num BORINGSSL_ADD_PREFIX(sk_BIO_num) @@ -7235,6 +7391,7 @@ #define sk_CONF_VALUE_free BORINGSSL_ADD_PREFIX(sk_CONF_VALUE_free) #define sk_CONF_VALUE_insert BORINGSSL_ADD_PREFIX(sk_CONF_VALUE_insert) #define sk_CONF_VALUE_is_sorted BORINGSSL_ADD_PREFIX(sk_CONF_VALUE_is_sorted) +#define sk_CONF_VALUE_last BORINGSSL_ADD_PREFIX(sk_CONF_VALUE_last) #define sk_CONF_VALUE_new BORINGSSL_ADD_PREFIX(sk_CONF_VALUE_new) #define sk_CONF_VALUE_new_null BORINGSSL_ADD_PREFIX(sk_CONF_VALUE_new_null) #define sk_CONF_VALUE_num BORINGSSL_ADD_PREFIX(sk_CONF_VALUE_num) @@ -7261,6 +7418,7 @@ #define sk_CRYPTO_BUFFER_free BORINGSSL_ADD_PREFIX(sk_CRYPTO_BUFFER_free) #define sk_CRYPTO_BUFFER_insert BORINGSSL_ADD_PREFIX(sk_CRYPTO_BUFFER_insert) #define sk_CRYPTO_BUFFER_is_sorted BORINGSSL_ADD_PREFIX(sk_CRYPTO_BUFFER_is_sorted) +#define sk_CRYPTO_BUFFER_last BORINGSSL_ADD_PREFIX(sk_CRYPTO_BUFFER_last) #define sk_CRYPTO_BUFFER_new BORINGSSL_ADD_PREFIX(sk_CRYPTO_BUFFER_new) #define sk_CRYPTO_BUFFER_new_null BORINGSSL_ADD_PREFIX(sk_CRYPTO_BUFFER_new_null) #define sk_CRYPTO_BUFFER_num BORINGSSL_ADD_PREFIX(sk_CRYPTO_BUFFER_num) @@ -7287,6 +7445,7 @@ #define sk_DIST_POINT_free BORINGSSL_ADD_PREFIX(sk_DIST_POINT_free) #define sk_DIST_POINT_insert BORINGSSL_ADD_PREFIX(sk_DIST_POINT_insert) #define sk_DIST_POINT_is_sorted BORINGSSL_ADD_PREFIX(sk_DIST_POINT_is_sorted) +#define sk_DIST_POINT_last BORINGSSL_ADD_PREFIX(sk_DIST_POINT_last) #define sk_DIST_POINT_new BORINGSSL_ADD_PREFIX(sk_DIST_POINT_new) #define sk_DIST_POINT_new_null BORINGSSL_ADD_PREFIX(sk_DIST_POINT_new_null) #define sk_DIST_POINT_num BORINGSSL_ADD_PREFIX(sk_DIST_POINT_num) @@ -7313,6 +7472,7 @@ #define sk_GENERAL_NAME_free BORINGSSL_ADD_PREFIX(sk_GENERAL_NAME_free) #define sk_GENERAL_NAME_insert BORINGSSL_ADD_PREFIX(sk_GENERAL_NAME_insert) #define sk_GENERAL_NAME_is_sorted BORINGSSL_ADD_PREFIX(sk_GENERAL_NAME_is_sorted) +#define sk_GENERAL_NAME_last BORINGSSL_ADD_PREFIX(sk_GENERAL_NAME_last) #define sk_GENERAL_NAME_new BORINGSSL_ADD_PREFIX(sk_GENERAL_NAME_new) #define sk_GENERAL_NAME_new_null BORINGSSL_ADD_PREFIX(sk_GENERAL_NAME_new_null) #define sk_GENERAL_NAME_num BORINGSSL_ADD_PREFIX(sk_GENERAL_NAME_num) @@ -7339,6 +7499,7 @@ #define sk_GENERAL_SUBTREE_free BORINGSSL_ADD_PREFIX(sk_GENERAL_SUBTREE_free) #define sk_GENERAL_SUBTREE_insert BORINGSSL_ADD_PREFIX(sk_GENERAL_SUBTREE_insert) #define sk_GENERAL_SUBTREE_is_sorted BORINGSSL_ADD_PREFIX(sk_GENERAL_SUBTREE_is_sorted) +#define sk_GENERAL_SUBTREE_last BORINGSSL_ADD_PREFIX(sk_GENERAL_SUBTREE_last) #define sk_GENERAL_SUBTREE_new BORINGSSL_ADD_PREFIX(sk_GENERAL_SUBTREE_new) #define sk_GENERAL_SUBTREE_new_null BORINGSSL_ADD_PREFIX(sk_GENERAL_SUBTREE_new_null) #define sk_GENERAL_SUBTREE_num BORINGSSL_ADD_PREFIX(sk_GENERAL_SUBTREE_num) @@ -7365,6 +7526,7 @@ #define sk_OPENSSL_STRING_free BORINGSSL_ADD_PREFIX(sk_OPENSSL_STRING_free) #define sk_OPENSSL_STRING_insert BORINGSSL_ADD_PREFIX(sk_OPENSSL_STRING_insert) #define sk_OPENSSL_STRING_is_sorted BORINGSSL_ADD_PREFIX(sk_OPENSSL_STRING_is_sorted) +#define sk_OPENSSL_STRING_last BORINGSSL_ADD_PREFIX(sk_OPENSSL_STRING_last) #define sk_OPENSSL_STRING_new BORINGSSL_ADD_PREFIX(sk_OPENSSL_STRING_new) #define sk_OPENSSL_STRING_new_null BORINGSSL_ADD_PREFIX(sk_OPENSSL_STRING_new_null) #define sk_OPENSSL_STRING_num BORINGSSL_ADD_PREFIX(sk_OPENSSL_STRING_num) @@ -7391,6 +7553,7 @@ #define sk_POLICYINFO_free BORINGSSL_ADD_PREFIX(sk_POLICYINFO_free) #define sk_POLICYINFO_insert BORINGSSL_ADD_PREFIX(sk_POLICYINFO_insert) #define sk_POLICYINFO_is_sorted BORINGSSL_ADD_PREFIX(sk_POLICYINFO_is_sorted) +#define sk_POLICYINFO_last BORINGSSL_ADD_PREFIX(sk_POLICYINFO_last) #define sk_POLICYINFO_new BORINGSSL_ADD_PREFIX(sk_POLICYINFO_new) #define sk_POLICYINFO_new_null BORINGSSL_ADD_PREFIX(sk_POLICYINFO_new_null) #define sk_POLICYINFO_num BORINGSSL_ADD_PREFIX(sk_POLICYINFO_num) @@ -7417,6 +7580,7 @@ #define sk_POLICYQUALINFO_free BORINGSSL_ADD_PREFIX(sk_POLICYQUALINFO_free) #define sk_POLICYQUALINFO_insert BORINGSSL_ADD_PREFIX(sk_POLICYQUALINFO_insert) #define sk_POLICYQUALINFO_is_sorted BORINGSSL_ADD_PREFIX(sk_POLICYQUALINFO_is_sorted) +#define sk_POLICYQUALINFO_last BORINGSSL_ADD_PREFIX(sk_POLICYQUALINFO_last) #define sk_POLICYQUALINFO_new BORINGSSL_ADD_PREFIX(sk_POLICYQUALINFO_new) #define sk_POLICYQUALINFO_new_null BORINGSSL_ADD_PREFIX(sk_POLICYQUALINFO_new_null) #define sk_POLICYQUALINFO_num BORINGSSL_ADD_PREFIX(sk_POLICYQUALINFO_num) @@ -7443,6 +7607,7 @@ #define sk_POLICY_MAPPING_free BORINGSSL_ADD_PREFIX(sk_POLICY_MAPPING_free) #define sk_POLICY_MAPPING_insert BORINGSSL_ADD_PREFIX(sk_POLICY_MAPPING_insert) #define sk_POLICY_MAPPING_is_sorted BORINGSSL_ADD_PREFIX(sk_POLICY_MAPPING_is_sorted) +#define sk_POLICY_MAPPING_last BORINGSSL_ADD_PREFIX(sk_POLICY_MAPPING_last) #define sk_POLICY_MAPPING_new BORINGSSL_ADD_PREFIX(sk_POLICY_MAPPING_new) #define sk_POLICY_MAPPING_new_null BORINGSSL_ADD_PREFIX(sk_POLICY_MAPPING_new_null) #define sk_POLICY_MAPPING_num BORINGSSL_ADD_PREFIX(sk_POLICY_MAPPING_num) @@ -7469,6 +7634,7 @@ #define sk_SRTP_PROTECTION_PROFILE_free BORINGSSL_ADD_PREFIX(sk_SRTP_PROTECTION_PROFILE_free) #define sk_SRTP_PROTECTION_PROFILE_insert BORINGSSL_ADD_PREFIX(sk_SRTP_PROTECTION_PROFILE_insert) #define sk_SRTP_PROTECTION_PROFILE_is_sorted BORINGSSL_ADD_PREFIX(sk_SRTP_PROTECTION_PROFILE_is_sorted) +#define sk_SRTP_PROTECTION_PROFILE_last BORINGSSL_ADD_PREFIX(sk_SRTP_PROTECTION_PROFILE_last) #define sk_SRTP_PROTECTION_PROFILE_new BORINGSSL_ADD_PREFIX(sk_SRTP_PROTECTION_PROFILE_new) #define sk_SRTP_PROTECTION_PROFILE_new_null BORINGSSL_ADD_PREFIX(sk_SRTP_PROTECTION_PROFILE_new_null) #define sk_SRTP_PROTECTION_PROFILE_num BORINGSSL_ADD_PREFIX(sk_SRTP_PROTECTION_PROFILE_num) @@ -7495,6 +7661,7 @@ #define sk_SSL_CIPHER_free BORINGSSL_ADD_PREFIX(sk_SSL_CIPHER_free) #define sk_SSL_CIPHER_insert BORINGSSL_ADD_PREFIX(sk_SSL_CIPHER_insert) #define sk_SSL_CIPHER_is_sorted BORINGSSL_ADD_PREFIX(sk_SSL_CIPHER_is_sorted) +#define sk_SSL_CIPHER_last BORINGSSL_ADD_PREFIX(sk_SSL_CIPHER_last) #define sk_SSL_CIPHER_new BORINGSSL_ADD_PREFIX(sk_SSL_CIPHER_new) #define sk_SSL_CIPHER_new_null BORINGSSL_ADD_PREFIX(sk_SSL_CIPHER_new_null) #define sk_SSL_CIPHER_num BORINGSSL_ADD_PREFIX(sk_SSL_CIPHER_num) @@ -7521,6 +7688,7 @@ #define sk_SSL_COMP_free BORINGSSL_ADD_PREFIX(sk_SSL_COMP_free) #define sk_SSL_COMP_insert BORINGSSL_ADD_PREFIX(sk_SSL_COMP_insert) #define sk_SSL_COMP_is_sorted BORINGSSL_ADD_PREFIX(sk_SSL_COMP_is_sorted) +#define sk_SSL_COMP_last BORINGSSL_ADD_PREFIX(sk_SSL_COMP_last) #define sk_SSL_COMP_new BORINGSSL_ADD_PREFIX(sk_SSL_COMP_new) #define sk_SSL_COMP_new_null BORINGSSL_ADD_PREFIX(sk_SSL_COMP_new_null) #define sk_SSL_COMP_num BORINGSSL_ADD_PREFIX(sk_SSL_COMP_num) @@ -7547,6 +7715,7 @@ #define sk_TRUST_TOKEN_free BORINGSSL_ADD_PREFIX(sk_TRUST_TOKEN_free) #define sk_TRUST_TOKEN_insert BORINGSSL_ADD_PREFIX(sk_TRUST_TOKEN_insert) #define sk_TRUST_TOKEN_is_sorted BORINGSSL_ADD_PREFIX(sk_TRUST_TOKEN_is_sorted) +#define sk_TRUST_TOKEN_last BORINGSSL_ADD_PREFIX(sk_TRUST_TOKEN_last) #define sk_TRUST_TOKEN_new BORINGSSL_ADD_PREFIX(sk_TRUST_TOKEN_new) #define sk_TRUST_TOKEN_new_null BORINGSSL_ADD_PREFIX(sk_TRUST_TOKEN_new_null) #define sk_TRUST_TOKEN_num BORINGSSL_ADD_PREFIX(sk_TRUST_TOKEN_num) @@ -7573,6 +7742,7 @@ #define sk_X509_ALGOR_free BORINGSSL_ADD_PREFIX(sk_X509_ALGOR_free) #define sk_X509_ALGOR_insert BORINGSSL_ADD_PREFIX(sk_X509_ALGOR_insert) #define sk_X509_ALGOR_is_sorted BORINGSSL_ADD_PREFIX(sk_X509_ALGOR_is_sorted) +#define sk_X509_ALGOR_last BORINGSSL_ADD_PREFIX(sk_X509_ALGOR_last) #define sk_X509_ALGOR_new BORINGSSL_ADD_PREFIX(sk_X509_ALGOR_new) #define sk_X509_ALGOR_new_null BORINGSSL_ADD_PREFIX(sk_X509_ALGOR_new_null) #define sk_X509_ALGOR_num BORINGSSL_ADD_PREFIX(sk_X509_ALGOR_num) @@ -7599,6 +7769,7 @@ #define sk_X509_ATTRIBUTE_free BORINGSSL_ADD_PREFIX(sk_X509_ATTRIBUTE_free) #define sk_X509_ATTRIBUTE_insert BORINGSSL_ADD_PREFIX(sk_X509_ATTRIBUTE_insert) #define sk_X509_ATTRIBUTE_is_sorted BORINGSSL_ADD_PREFIX(sk_X509_ATTRIBUTE_is_sorted) +#define sk_X509_ATTRIBUTE_last BORINGSSL_ADD_PREFIX(sk_X509_ATTRIBUTE_last) #define sk_X509_ATTRIBUTE_new BORINGSSL_ADD_PREFIX(sk_X509_ATTRIBUTE_new) #define sk_X509_ATTRIBUTE_new_null BORINGSSL_ADD_PREFIX(sk_X509_ATTRIBUTE_new_null) #define sk_X509_ATTRIBUTE_num BORINGSSL_ADD_PREFIX(sk_X509_ATTRIBUTE_num) @@ -7625,6 +7796,7 @@ #define sk_X509_CRL_free BORINGSSL_ADD_PREFIX(sk_X509_CRL_free) #define sk_X509_CRL_insert BORINGSSL_ADD_PREFIX(sk_X509_CRL_insert) #define sk_X509_CRL_is_sorted BORINGSSL_ADD_PREFIX(sk_X509_CRL_is_sorted) +#define sk_X509_CRL_last BORINGSSL_ADD_PREFIX(sk_X509_CRL_last) #define sk_X509_CRL_new BORINGSSL_ADD_PREFIX(sk_X509_CRL_new) #define sk_X509_CRL_new_null BORINGSSL_ADD_PREFIX(sk_X509_CRL_new_null) #define sk_X509_CRL_num BORINGSSL_ADD_PREFIX(sk_X509_CRL_num) @@ -7651,6 +7823,7 @@ #define sk_X509_EXTENSION_free BORINGSSL_ADD_PREFIX(sk_X509_EXTENSION_free) #define sk_X509_EXTENSION_insert BORINGSSL_ADD_PREFIX(sk_X509_EXTENSION_insert) #define sk_X509_EXTENSION_is_sorted BORINGSSL_ADD_PREFIX(sk_X509_EXTENSION_is_sorted) +#define sk_X509_EXTENSION_last BORINGSSL_ADD_PREFIX(sk_X509_EXTENSION_last) #define sk_X509_EXTENSION_new BORINGSSL_ADD_PREFIX(sk_X509_EXTENSION_new) #define sk_X509_EXTENSION_new_null BORINGSSL_ADD_PREFIX(sk_X509_EXTENSION_new_null) #define sk_X509_EXTENSION_num BORINGSSL_ADD_PREFIX(sk_X509_EXTENSION_num) @@ -7677,6 +7850,7 @@ #define sk_X509_INFO_free BORINGSSL_ADD_PREFIX(sk_X509_INFO_free) #define sk_X509_INFO_insert BORINGSSL_ADD_PREFIX(sk_X509_INFO_insert) #define sk_X509_INFO_is_sorted BORINGSSL_ADD_PREFIX(sk_X509_INFO_is_sorted) +#define sk_X509_INFO_last BORINGSSL_ADD_PREFIX(sk_X509_INFO_last) #define sk_X509_INFO_new BORINGSSL_ADD_PREFIX(sk_X509_INFO_new) #define sk_X509_INFO_new_null BORINGSSL_ADD_PREFIX(sk_X509_INFO_new_null) #define sk_X509_INFO_num BORINGSSL_ADD_PREFIX(sk_X509_INFO_num) @@ -7703,6 +7877,7 @@ #define sk_X509_NAME_ENTRY_free BORINGSSL_ADD_PREFIX(sk_X509_NAME_ENTRY_free) #define sk_X509_NAME_ENTRY_insert BORINGSSL_ADD_PREFIX(sk_X509_NAME_ENTRY_insert) #define sk_X509_NAME_ENTRY_is_sorted BORINGSSL_ADD_PREFIX(sk_X509_NAME_ENTRY_is_sorted) +#define sk_X509_NAME_ENTRY_last BORINGSSL_ADD_PREFIX(sk_X509_NAME_ENTRY_last) #define sk_X509_NAME_ENTRY_new BORINGSSL_ADD_PREFIX(sk_X509_NAME_ENTRY_new) #define sk_X509_NAME_ENTRY_new_null BORINGSSL_ADD_PREFIX(sk_X509_NAME_ENTRY_new_null) #define sk_X509_NAME_ENTRY_num BORINGSSL_ADD_PREFIX(sk_X509_NAME_ENTRY_num) @@ -7729,6 +7904,7 @@ #define sk_X509_NAME_free BORINGSSL_ADD_PREFIX(sk_X509_NAME_free) #define sk_X509_NAME_insert BORINGSSL_ADD_PREFIX(sk_X509_NAME_insert) #define sk_X509_NAME_is_sorted BORINGSSL_ADD_PREFIX(sk_X509_NAME_is_sorted) +#define sk_X509_NAME_last BORINGSSL_ADD_PREFIX(sk_X509_NAME_last) #define sk_X509_NAME_new BORINGSSL_ADD_PREFIX(sk_X509_NAME_new) #define sk_X509_NAME_new_null BORINGSSL_ADD_PREFIX(sk_X509_NAME_new_null) #define sk_X509_NAME_num BORINGSSL_ADD_PREFIX(sk_X509_NAME_num) @@ -7755,6 +7931,7 @@ #define sk_X509_OBJECT_free BORINGSSL_ADD_PREFIX(sk_X509_OBJECT_free) #define sk_X509_OBJECT_insert BORINGSSL_ADD_PREFIX(sk_X509_OBJECT_insert) #define sk_X509_OBJECT_is_sorted BORINGSSL_ADD_PREFIX(sk_X509_OBJECT_is_sorted) +#define sk_X509_OBJECT_last BORINGSSL_ADD_PREFIX(sk_X509_OBJECT_last) #define sk_X509_OBJECT_new BORINGSSL_ADD_PREFIX(sk_X509_OBJECT_new) #define sk_X509_OBJECT_new_null BORINGSSL_ADD_PREFIX(sk_X509_OBJECT_new_null) #define sk_X509_OBJECT_num BORINGSSL_ADD_PREFIX(sk_X509_OBJECT_num) @@ -7781,6 +7958,7 @@ #define sk_X509_REVOKED_free BORINGSSL_ADD_PREFIX(sk_X509_REVOKED_free) #define sk_X509_REVOKED_insert BORINGSSL_ADD_PREFIX(sk_X509_REVOKED_insert) #define sk_X509_REVOKED_is_sorted BORINGSSL_ADD_PREFIX(sk_X509_REVOKED_is_sorted) +#define sk_X509_REVOKED_last BORINGSSL_ADD_PREFIX(sk_X509_REVOKED_last) #define sk_X509_REVOKED_new BORINGSSL_ADD_PREFIX(sk_X509_REVOKED_new) #define sk_X509_REVOKED_new_null BORINGSSL_ADD_PREFIX(sk_X509_REVOKED_new_null) #define sk_X509_REVOKED_num BORINGSSL_ADD_PREFIX(sk_X509_REVOKED_num) @@ -7807,6 +7985,7 @@ #define sk_X509_free BORINGSSL_ADD_PREFIX(sk_X509_free) #define sk_X509_insert BORINGSSL_ADD_PREFIX(sk_X509_insert) #define sk_X509_is_sorted BORINGSSL_ADD_PREFIX(sk_X509_is_sorted) +#define sk_X509_last BORINGSSL_ADD_PREFIX(sk_X509_last) #define sk_X509_new BORINGSSL_ADD_PREFIX(sk_X509_new) #define sk_X509_new_null BORINGSSL_ADD_PREFIX(sk_X509_new_null) #define sk_X509_num BORINGSSL_ADD_PREFIX(sk_X509_num) @@ -7833,6 +8012,7 @@ #define sk_void_free BORINGSSL_ADD_PREFIX(sk_void_free) #define sk_void_insert BORINGSSL_ADD_PREFIX(sk_void_insert) #define sk_void_is_sorted BORINGSSL_ADD_PREFIX(sk_void_is_sorted) +#define sk_void_last BORINGSSL_ADD_PREFIX(sk_void_last) #define sk_void_new BORINGSSL_ADD_PREFIX(sk_void_new) #define sk_void_new_null BORINGSSL_ADD_PREFIX(sk_void_new_null) #define sk_void_num BORINGSSL_ADD_PREFIX(sk_void_num) diff --git a/third_party/boringssl/src/include/openssl/rand.h b/third_party/boringssl/src/include/openssl/rand.h index 9d66a895b..3bb3855a8 100644 --- a/third_party/boringssl/src/include/openssl/rand.h +++ b/third_party/boringssl/src/include/openssl/rand.h @@ -25,8 +25,8 @@ extern "C" { // Random number generation. -// RAND_bytes writes |len| bytes of random data to |buf| and returns one. In the -// event that sufficient random data can not be obtained, |abort| is called. +// RAND_bytes writes `len` bytes of random data to `buf` and returns one. In the +// event that sufficient random data can not be obtained, `abort` is called. OPENSSL_EXPORT int RAND_bytes(uint8_t *buf, size_t len); @@ -34,10 +34,10 @@ OPENSSL_EXPORT int RAND_bytes(uint8_t *buf, size_t len); #if !defined(OPENSSL_WINDOWS) // RAND_enable_fork_unsafe_buffering indicates that clones of the address space, -// e.g. via |fork|, will never call into BoringSSL. It may be used to disable +// e.g. via `fork`, will never call into BoringSSL. It may be used to disable // BoringSSL's more expensive fork-safety measures. However, calling this -// function and then using BoringSSL across |fork| calls will leak secret keys. -// |fd| must be -1. +// function and then using BoringSSL across `fork` calls will leak secret keys. +// `fd` must be -1. // // WARNING: This function affects BoringSSL for the entire address space. Thus // this function should never be called by library code, only by code with @@ -46,7 +46,7 @@ OPENSSL_EXPORT int RAND_bytes(uint8_t *buf, size_t len); // Do not use this function unless a performance issue was measured with the // default behavior. BoringSSL can efficiently detect forks on most platforms, // in which case this function is a no-op and is unnecessary. In particular, -// Linux kernel versions 4.14 or later provide |MADV_WIPEONFORK|. Future +// Linux kernel versions 4.14 or later provide `MADV_WIPEONFORK`. Future // versions of BoringSSL will remove this functionality when older kernels are // sufficiently rare. // @@ -55,7 +55,7 @@ OPENSSL_EXPORT int RAND_bytes(uint8_t *buf, size_t len); OPENSSL_EXPORT void RAND_enable_fork_unsafe_buffering(int fd); // RAND_disable_fork_unsafe_buffering restores BoringSSL's default fork-safety -// protections. See also |RAND_enable_fork_unsafe_buffering|. +// protections. See also `RAND_enable_fork_unsafe_buffering`. OPENSSL_EXPORT void RAND_disable_fork_unsafe_buffering(void); #endif @@ -65,31 +65,20 @@ OPENSSL_EXPORT void RAND_disable_fork_unsafe_buffering(void); OPENSSL_EXPORT void RAND_reset_for_fuzzing(void); #endif -// RAND_get_system_entropy_for_custom_prng writes |len| bytes of random data -// from a system entropy source to |buf|. The maximum length of entropy which +// RAND_get_system_entropy_for_custom_prng writes `len` bytes of random data +// from a system entropy source to `buf`. The maximum length of entropy which // may be requested is 256 bytes. If more than 256 bytes of data is requested, -// or if sufficient random data can not be obtained, |abort| is called. -// |RAND_bytes| should normally be used instead of this function. This function -// should only be used for seed values or where |malloc| should not be called +// or if sufficient random data can not be obtained, `abort` is called. +// `RAND_bytes` should normally be used instead of this function. This function +// should only be used for seed values or where `malloc` should not be called // from BoringSSL. This function is not FIPS compliant. OPENSSL_EXPORT void RAND_get_system_entropy_for_custom_prng(uint8_t *buf, size_t len); -// RAND_maybe_reseed might reseed the PRNG if it's getting close to the reseed -// limit. If it does so, it may briefly block other threads that are -// concurrently calling `RAND_bytes`, but only for ~microseconds. Applications -// may wish to periodically call this function to avoid hitting a reseed while -// servicing a `RAND_bytes` call, which could happen from anywhere and take -// milliseconds or more in FIPS configurations. _Most_ applications, however, -// should ignore this and it only makes a difference in FIPS builds. -// -// Returns one if a reseed was performed and zero otherwise. -OPENSSL_EXPORT int RAND_maybe_reseed(void); - // Deprecated functions -// RAND_pseudo_bytes is a wrapper around |RAND_bytes|. +// RAND_pseudo_bytes is a wrapper around `RAND_bytes`. OPENSSL_EXPORT int RAND_pseudo_bytes(uint8_t *buf, size_t len); // RAND_seed reads a single byte of random data to ensure that any file @@ -117,8 +106,8 @@ OPENSSL_EXPORT int RAND_status(void); // RAND_cleanup does nothing. OPENSSL_EXPORT void RAND_cleanup(void); -// rand_meth_st is typedefed to |RAND_METHOD| in base.h. It isn't used; it -// exists only to be the return type of |RAND_SSLeay|. It's +// rand_meth_st is typedefed to `RAND_METHOD` in base.h. It isn't used; it +// exists only to be the return type of `RAND_SSLeay`. It's // external so that variables of this type can be initialized. struct rand_meth_st { void (*seed) (const void *buf, int num); @@ -129,13 +118,13 @@ struct rand_meth_st { int (*status) (void); }; -// RAND_SSLeay returns a pointer to a dummy |RAND_METHOD|. +// RAND_SSLeay returns a pointer to a dummy `RAND_METHOD`. OPENSSL_EXPORT RAND_METHOD *RAND_SSLeay(void); -// RAND_OpenSSL returns a pointer to a dummy |RAND_METHOD|. +// RAND_OpenSSL returns a pointer to a dummy `RAND_METHOD`. OPENSSL_EXPORT RAND_METHOD *RAND_OpenSSL(void); -// RAND_get_rand_method returns |RAND_SSLeay()|. +// RAND_get_rand_method returns `RAND_SSLeay()`. OPENSSL_EXPORT const RAND_METHOD *RAND_get_rand_method(void); // RAND_set_rand_method returns one. diff --git a/third_party/boringssl/src/include/openssl/rc4.h b/third_party/boringssl/src/include/openssl/rc4.h index 520c3a688..b45eaaffe 100644 --- a/third_party/boringssl/src/include/openssl/rc4.h +++ b/third_party/boringssl/src/include/openssl/rc4.h @@ -30,13 +30,13 @@ struct rc4_key_st { uint32_t data[256]; } /* RC4_KEY */; -// RC4_set_key performs an RC4 key schedule and initialises |rc4key| with |len| -// bytes of key material from |key|. +// RC4_set_key performs an RC4 key schedule and initialises `rc4key` with `len` +// bytes of key material from `key`. OPENSSL_EXPORT void RC4_set_key(RC4_KEY *rc4key, unsigned len, const uint8_t *key); -// RC4 encrypts (or decrypts, it's the same with RC4) |len| bytes from |in| to -// |out|. +// RC4 encrypts (or decrypts, it's the same with RC4) `len` bytes from `in` to +// `out`. OPENSSL_EXPORT void RC4(RC4_KEY *key, size_t len, const uint8_t *in, uint8_t *out); diff --git a/third_party/boringssl/src/include/openssl/ripemd.h b/third_party/boringssl/src/include/openssl/ripemd.h index d21c72e70..b59a1a653 100644 --- a/third_party/boringssl/src/include/openssl/ripemd.h +++ b/third_party/boringssl/src/include/openssl/ripemd.h @@ -38,28 +38,28 @@ struct RIPEMD160state_st { unsigned num; }; -// RIPEMD160_Init initialises |ctx| and returns one. +// RIPEMD160_Init initialises `ctx` and returns one. OPENSSL_EXPORT int RIPEMD160_Init(RIPEMD160_CTX *ctx); -// RIPEMD160_Update adds |len| bytes from |data| to |ctx| and returns one. +// RIPEMD160_Update adds `len` bytes from `data` to `ctx` and returns one. OPENSSL_EXPORT int RIPEMD160_Update(RIPEMD160_CTX *ctx, const void *data, size_t len); -// RIPEMD160_Final adds the final padding to |ctx| and writes the resulting -// digest to |out|, which must have at least |RIPEMD160_DIGEST_LENGTH| bytes of +// RIPEMD160_Final adds the final padding to `ctx` and writes the resulting +// digest to `out`, which must have at least `RIPEMD160_DIGEST_LENGTH` bytes of // space. It returns one. OPENSSL_EXPORT int RIPEMD160_Final(uint8_t out[RIPEMD160_DIGEST_LENGTH], RIPEMD160_CTX *ctx); -// RIPEMD160 writes the digest of |len| bytes from |data| to |out| and returns -// |out|. There must be at least |RIPEMD160_DIGEST_LENGTH| bytes of space in -// |out|. +// RIPEMD160 writes the digest of `len` bytes from `data` to `out` and returns +// `out`. There must be at least `RIPEMD160_DIGEST_LENGTH` bytes of space in +// `out`. OPENSSL_EXPORT uint8_t *RIPEMD160(const uint8_t *data, size_t len, uint8_t out[RIPEMD160_DIGEST_LENGTH]); // RIPEMD160_Transform is a low-level function that performs a single, -// RIPEMD160 block transformation using the state from |ctx| and 64 bytes from -// |block|. +// RIPEMD160 block transformation using the state from `ctx` and 64 bytes from +// `block`. OPENSSL_EXPORT void RIPEMD160_Transform(RIPEMD160_CTX *ctx, const uint8_t block[RIPEMD160_CBLOCK]); diff --git a/third_party/boringssl/src/include/openssl/rsa.h b/third_party/boringssl/src/include/openssl/rsa.h index 692d50956..f7c6cfaf9 100644 --- a/third_party/boringssl/src/include/openssl/rsa.h +++ b/third_party/boringssl/src/include/openssl/rsa.h @@ -15,7 +15,7 @@ #ifndef OPENSSL_HEADER_RSA_H #define OPENSSL_HEADER_RSA_H -#include // IWYU pragma: export +#include // IWYU pragma: export #include #include @@ -30,17 +30,17 @@ extern "C" { // Allocation and destruction. // -// An |RSA| object represents a public or private RSA key. A given object may be +// An `RSA` object represents a public or private RSA key. A given object may be // used concurrently on multiple threads by non-mutating functions, provided no // other thread is concurrently calling a mutating function. Unless otherwise -// documented, functions which take a |const| pointer are non-mutating and -// functions which take a non-|const| pointer are mutating. +// documented, functions which take a `const` pointer are non-mutating and +// functions which take a non-`const` pointer are mutating. -// RSA_new_public_key returns a new |RSA| object containing a public key with +// RSA_new_public_key returns a new `RSA` object containing a public key with // the specified parameters, or NULL on error or invalid input. OPENSSL_EXPORT RSA *RSA_new_public_key(const BIGNUM *n, const BIGNUM *e); -// RSA_new_private_key returns a new |RSA| object containing a private key with +// RSA_new_private_key returns a new `RSA` object containing a private key with // the specified parameters, or NULL on error or invalid input. All parameters // are mandatory and may not be NULL. // @@ -50,21 +50,26 @@ OPENSSL_EXPORT RSA *RSA_new_private_key(const BIGNUM *n, const BIGNUM *e, const BIGNUM *q, const BIGNUM *dmp1, const BIGNUM *dmq1, const BIGNUM *iqmp); -// RSA_new returns a new, empty |RSA| object or NULL on error. Prefer using -// |RSA_new_public_key| or |RSA_new_private_key| to import an RSA key. +// RSA_new returns a new, empty `RSA` object or NULL on error. Prefer using +// `RSA_new_public_key` or `RSA_new_private_key` to import an RSA key. OPENSSL_EXPORT RSA *RSA_new(void); -// RSA_new_method acts the same as |RSA_new| but takes an explicit |ENGINE|. +// RSA_new_method acts the same as `RSA_new` but takes an explicit `ENGINE`. OPENSSL_EXPORT RSA *RSA_new_method(const ENGINE *engine); -// RSA_free decrements the reference count of |rsa| and frees it if the +// RSA_free decrements the reference count of `rsa` and frees it if the // reference count drops to zero. OPENSSL_EXPORT void RSA_free(RSA *rsa); -// RSA_up_ref increments the reference count of |rsa| and returns one. It does -// not mutate |rsa| for thread-safety purposes and may be used concurrently. +// RSA_up_ref increments the reference count of `rsa` and returns one. It does +// not mutate `rsa` for thread-safety purposes and may be used concurrently. OPENSSL_EXPORT int RSA_up_ref(RSA *rsa); +// RSA_dup_ref increments the reference count of `rsa` and returns the same +// handle. It does not mutate `rsa` for thread-safety purposes and may be used +// concurrently. +OPENSSL_EXPORT RSA *RSA_dup_ref(const RSA *rsa); + // Properties. @@ -80,53 +85,53 @@ OPENSSL_EXPORT int RSA_up_ref(RSA *rsa); // opt-in, or impose a tighter limit on private key operations. #define OPENSSL_RSA_MAX_MODULUS_BITS 16384 -// RSA_bits returns the size of |rsa|, in bits. +// RSA_bits returns the size of `rsa`, in bits. OPENSSL_EXPORT unsigned RSA_bits(const RSA *rsa); -// RSA_get0_n returns |rsa|'s public modulus. +// RSA_get0_n returns `rsa`'s public modulus. OPENSSL_EXPORT const BIGNUM *RSA_get0_n(const RSA *rsa); -// RSA_get0_e returns |rsa|'s public exponent. +// RSA_get0_e returns `rsa`'s public exponent. OPENSSL_EXPORT const BIGNUM *RSA_get0_e(const RSA *rsa); -// RSA_get0_d returns |rsa|'s private exponent. If |rsa| is a public key, this +// RSA_get0_d returns `rsa`'s private exponent. If `rsa` is a public key, this // value will be NULL. OPENSSL_EXPORT const BIGNUM *RSA_get0_d(const RSA *rsa); -// RSA_get0_p returns |rsa|'s first private prime factor. If |rsa| is a public +// RSA_get0_p returns `rsa`'s first private prime factor. If `rsa` is a public // key or lacks its prime factors, this value will be NULL. OPENSSL_EXPORT const BIGNUM *RSA_get0_p(const RSA *rsa); -// RSA_get0_q returns |rsa|'s second private prime factor. If |rsa| is a public +// RSA_get0_q returns `rsa`'s second private prime factor. If `rsa` is a public // key or lacks its prime factors, this value will be NULL. OPENSSL_EXPORT const BIGNUM *RSA_get0_q(const RSA *rsa); -// RSA_get0_dmp1 returns d (mod p-1) for |rsa|. If |rsa| is a public key or +// RSA_get0_dmp1 returns d (mod p-1) for `rsa`. If `rsa` is a public key or // lacks CRT parameters, this value will be NULL. OPENSSL_EXPORT const BIGNUM *RSA_get0_dmp1(const RSA *rsa); -// RSA_get0_dmq1 returns d (mod q-1) for |rsa|. If |rsa| is a public key or +// RSA_get0_dmq1 returns d (mod q-1) for `rsa`. If `rsa` is a public key or // lacks CRT parameters, this value will be NULL. OPENSSL_EXPORT const BIGNUM *RSA_get0_dmq1(const RSA *rsa); -// RSA_get0_iqmp returns q^-1 (mod p). If |rsa| is a public key or lacks CRT +// RSA_get0_iqmp returns q^-1 (mod p). If `rsa` is a public key or lacks CRT // parameters, this value will be NULL. OPENSSL_EXPORT const BIGNUM *RSA_get0_iqmp(const RSA *rsa); -// RSA_get0_key sets |*out_n|, |*out_e|, and |*out_d|, if non-NULL, to |rsa|'s -// modulus, public exponent, and private exponent, respectively. If |rsa| is a +// RSA_get0_key sets `*out_n`, `*out_e`, and `*out_d`, if non-NULL, to `rsa`'s +// modulus, public exponent, and private exponent, respectively. If `rsa` is a // public key, the private exponent will be set to NULL. OPENSSL_EXPORT void RSA_get0_key(const RSA *rsa, const BIGNUM **out_n, const BIGNUM **out_e, const BIGNUM **out_d); -// RSA_get0_factors sets |*out_p| and |*out_q|, if non-NULL, to |rsa|'s prime -// factors. If |rsa| is a public key, they will be set to NULL. +// RSA_get0_factors sets `*out_p` and `*out_q`, if non-NULL, to `rsa`'s prime +// factors. If `rsa` is a public key, they will be set to NULL. OPENSSL_EXPORT void RSA_get0_factors(const RSA *rsa, const BIGNUM **out_p, const BIGNUM **out_q); -// RSA_get0_crt_params sets |*out_dmp1|, |*out_dmq1|, and |*out_iqmp|, if -// non-NULL, to |rsa|'s CRT parameters. These are d (mod p-1), d (mod q-1) and -// q^-1 (mod p), respectively. If |rsa| is a public key, each parameter will be +// RSA_get0_crt_params sets `*out_dmp1`, `*out_dmq1`, and `*out_iqmp`, if +// non-NULL, to `rsa`'s CRT parameters. These are d (mod p-1), d (mod q-1) and +// q^-1 (mod p), respectively. If `rsa` is a public key, each parameter will be // set to NULL. OPENSSL_EXPORT void RSA_get0_crt_params(const RSA *rsa, const BIGNUM **out_dmp1, const BIGNUM **out_dmq1, @@ -135,10 +140,10 @@ OPENSSL_EXPORT void RSA_get0_crt_params(const RSA *rsa, const BIGNUM **out_dmp1, // Setting individual properties. // -// These functions allow setting individual properties of an |RSA| object. This -// is typically used with |RSA_new| to construct an RSA key field by field. -// Prefer instead to use |RSA_new_public_key| and |RSA_new_private_key|. These -// functions defer some initialization to the first use of an |RSA| object. This +// These functions allow setting individual properties of an `RSA` object. This +// is typically used with `RSA_new` to construct an RSA key field by field. +// Prefer instead to use `RSA_new_public_key` and `RSA_new_private_key`. These +// functions defer some initialization to the first use of an `RSA` object. This // means invalid inputs may be caught late. // // TODO(crbug.com/boringssl/316): This deferred initialization also causes @@ -146,35 +151,35 @@ OPENSSL_EXPORT void RSA_get0_crt_params(const RSA *rsa, const BIGNUM **out_dmp1, // currently have the same issues, but they will initialize eagerly in the // future. -// RSA_set0_key sets |rsa|'s modulus, public exponent, and private exponent to -// |n|, |e|, and |d| respectively, if non-NULL. On success, it takes ownership +// RSA_set0_key sets `rsa`'s modulus, public exponent, and private exponent to +// `n`, `e`, and `d` respectively, if non-NULL. On success, it takes ownership // of each argument and returns one. Otherwise, it returns zero. // -// |d| may be NULL, but |n| and |e| must either be non-NULL or already -// configured on |rsa|. +// `d` may be NULL, but `n` and `e` must either be non-NULL or already +// configured on `rsa`. // -// It is an error to call this function after |rsa| has been used for a -// cryptographic operation. Construct a new |RSA| object instead. +// It is an error to call this function after `rsa` has been used for a +// cryptographic operation. Construct a new `RSA` object instead. OPENSSL_EXPORT int RSA_set0_key(RSA *rsa, BIGNUM *n, BIGNUM *e, BIGNUM *d); -// RSA_set0_factors sets |rsa|'s prime factors to |p| and |q|, if non-NULL, and +// RSA_set0_factors sets `rsa`'s prime factors to `p` and `q`, if non-NULL, and // takes ownership of them. On success, it takes ownership of each argument and // returns one. Otherwise, it returns zero. // -// Each argument must either be non-NULL or already configured on |rsa|. +// Each argument must either be non-NULL or already configured on `rsa`. // -// It is an error to call this function after |rsa| has been used for a -// cryptographic operation. Construct a new |RSA| object instead. +// It is an error to call this function after `rsa` has been used for a +// cryptographic operation. Construct a new `RSA` object instead. OPENSSL_EXPORT int RSA_set0_factors(RSA *rsa, BIGNUM *p, BIGNUM *q); -// RSA_set0_crt_params sets |rsa|'s CRT parameters to |dmp1|, |dmq1|, and -// |iqmp|, if non-NULL, and takes ownership of them. On success, it takes +// RSA_set0_crt_params sets `rsa`'s CRT parameters to `dmp1`, `dmq1`, and +// `iqmp`, if non-NULL, and takes ownership of them. On success, it takes // ownership of its parameters and returns one. Otherwise, it returns zero. // -// Each argument must either be non-NULL or already configured on |rsa|. +// Each argument must either be non-NULL or already configured on `rsa`. // -// It is an error to call this function after |rsa| has been used for a -// cryptographic operation. Construct a new |RSA| object instead. +// It is an error to call this function after `rsa` has been used for a +// cryptographic operation. Construct a new `RSA` object instead. OPENSSL_EXPORT int RSA_set0_crt_params(RSA *rsa, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp); @@ -182,19 +187,19 @@ OPENSSL_EXPORT int RSA_set0_crt_params(RSA *rsa, BIGNUM *dmp1, BIGNUM *dmq1, // Key generation. // RSA_generate_key_ex generates a new RSA key where the modulus has size -// |bits| and the public exponent is |e|. If unsure, |RSA_F4| is a good value -// for |e|. If |cb| is not NULL then it is called during the key generation -// process. In addition to the calls documented for |BN_generate_prime_ex|, it +// `bits` and the public exponent is `e`. If unsure, `RSA_F4` is a good value +// for `e`. If `cb` is not NULL then it is called during the key generation +// process. In addition to the calls documented for `BN_generate_prime_ex`, it // is called with event=2 when the n'th prime is rejected as unsuitable and -// with event=3 when a suitable value for |p| is found. +// with event=3 when a suitable value for `p` is found. // // It returns one on success or zero on error. OPENSSL_EXPORT int RSA_generate_key_ex(RSA *rsa, int bits, const BIGNUM *e, BN_GENCB *cb); -// RSA_generate_key_fips behaves like |RSA_generate_key_ex| but performs +// RSA_generate_key_fips behaves like `RSA_generate_key_ex` but performs // additional checks for FIPS compliance. The public exponent is always 65537 -// and |bits| must be either 2048 or 3072. +// and `bits` must be either 2048 or 3072. OPENSSL_EXPORT int RSA_generate_key_fips(RSA *rsa, int bits, BN_GENCB *cb); @@ -221,40 +226,40 @@ OPENSSL_EXPORT int RSA_generate_key_fips(RSA *rsa, int bits, BN_GENCB *cb); #define RSA_PKCS1_OAEP_PADDING 4 // RSA_PKCS1_PSS_PADDING denotes the RSASSA-PSS signature scheme. This value may -// not be passed into |RSA_sign_raw|, only |EVP_PKEY_CTX_set_rsa_padding|. See -// also |RSA_sign_pss_mgf1| and |RSA_verify_pss_mgf1|. +// not be passed into `RSA_sign_raw`, only `EVP_PKEY_CTX_set_rsa_padding`. See +// also `RSA_sign_pss_mgf1` and `RSA_verify_pss_mgf1`. #define RSA_PKCS1_PSS_PADDING 6 -// RSA_encrypt encrypts |in_len| bytes from |in| to the public key from |rsa| -// and writes, at most, |max_out| bytes of encrypted data to |out|. The -// |max_out| argument must be, at least, |RSA_size| in order to ensure success. +// RSA_encrypt encrypts `in_len` bytes from `in` to the public key from `rsa` +// and writes, at most, `max_out` bytes of encrypted data to `out`. The +// `max_out` argument must be, at least, `RSA_size` in order to ensure success. // // It returns 1 on success or zero on error. // -// The |padding| argument must be one of the |RSA_*_PADDING| values. If in -// doubt, use |RSA_PKCS1_OAEP_PADDING| for new protocols. When |padding| is -// |RSA_PKCS1_OAEP_PADDING|, this function has no way to set the OAEP or MGF-1 -// digest, so it is always SHA-1. For other OAEP parameters, wrap |rsa| in an -// |EVP_PKEY| and use |EVP_PKEY_encrypt| with |EVP_PKEY_CTX_set_rsa_padding| and +// The `padding` argument must be one of the `RSA_*_PADDING` values. If in +// doubt, use `RSA_PKCS1_OAEP_PADDING` for new protocols. When `padding` is +// `RSA_PKCS1_OAEP_PADDING`, this function has no way to set the OAEP or MGF-1 +// digest, so it is always SHA-1. For other OAEP parameters, wrap `rsa` in an +// `EVP_PKEY` and use `EVP_PKEY_encrypt` with `EVP_PKEY_CTX_set_rsa_padding` and // related functions. OPENSSL_EXPORT int RSA_encrypt(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out, const uint8_t *in, size_t in_len, int padding); -// RSA_decrypt decrypts |in_len| bytes from |in| with the private key from -// |rsa| and writes, at most, |max_out| bytes of plaintext to |out|. The -// |max_out| argument must be, at least, |RSA_size| in order to ensure success. +// RSA_decrypt decrypts `in_len` bytes from `in` with the private key from +// `rsa` and writes, at most, `max_out` bytes of plaintext to `out`. The +// `max_out` argument must be, at least, `RSA_size` in order to ensure success. // // It returns 1 on success or zero on error. // -// The |padding| argument must be one of the |RSA_*_PADDING| values. If in -// doubt, use |RSA_PKCS1_OAEP_PADDING| for new protocols. When |padding| is -// |RSA_PKCS1_OAEP_PADDING|, this function has no way to set the OAEP or MGF-1 -// digest, so it is always SHA-1. For other OAEP parameters, wrap |rsa| in an -// |EVP_PKEY| and use |EVP_PKEY_decrypt| with |EVP_PKEY_CTX_set_rsa_padding| and +// The `padding` argument must be one of the `RSA_*_PADDING` values. If in +// doubt, use `RSA_PKCS1_OAEP_PADDING` for new protocols. When `padding` is +// `RSA_PKCS1_OAEP_PADDING`, this function has no way to set the OAEP or MGF-1 +// digest, so it is always SHA-1. For other OAEP parameters, wrap `rsa` in an +// `EVP_PKEY` and use `EVP_PKEY_decrypt` with `EVP_PKEY_CTX_set_rsa_padding` and // related functions. // -// WARNING: Passing |RSA_PKCS1_PADDING| into this function is deprecated and +// WARNING: Passing `RSA_PKCS1_PADDING` into this function is deprecated and // insecure. RSAES-PKCS1-v1_5 is vulnerable to a chosen-ciphertext attack. // Decrypting attacker-supplied ciphertext with RSAES-PKCS1-v1_5 may give the // attacker control over your private key. See "Chosen Ciphertext Attacks @@ -263,42 +268,42 @@ OPENSSL_EXPORT int RSA_encrypt(RSA *rsa, size_t *out_len, uint8_t *out, // // In some limited cases, such as TLS RSA key exchange, it is possible to // mitigate this flaw with custom, protocol-specific padding logic. This -// should be implemented with |RSA_NO_PADDING|, not |RSA_PKCS1_PADDING|. +// should be implemented with `RSA_NO_PADDING`, not `RSA_PKCS1_PADDING`. OPENSSL_EXPORT int RSA_decrypt(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out, const uint8_t *in, size_t in_len, int padding); -// RSA_public_encrypt encrypts |flen| bytes from |from| to the public key in -// |rsa| and writes the encrypted data to |to|. The |to| buffer must have at -// least |RSA_size| bytes of space. It returns the number of bytes written, or -// -1 on error. The |padding| argument must be one of the |RSA_*_PADDING| -// values. If in doubt, use |RSA_PKCS1_OAEP_PADDING| for new protocols. +// RSA_public_encrypt encrypts `flen` bytes from `from` to the public key in +// `rsa` and writes the encrypted data to `to`. The `to` buffer must have at +// least `RSA_size` bytes of space. It returns the number of bytes written, or +// -1 on error. The `padding` argument must be one of the `RSA_*_PADDING` +// values. If in doubt, use `RSA_PKCS1_OAEP_PADDING` for new protocols. // -// When |padding| is |RSA_PKCS1_OAEP_PADDING|, this function has no way to set +// When `padding` is `RSA_PKCS1_OAEP_PADDING`, this function has no way to set // the OAEP or MGF-1 digest, so it is always SHA-1. For other OAEP parameters, -// wrap |rsa| in an |EVP_PKEY| and use |EVP_PKEY_encrypt| with -// |EVP_PKEY_CTX_set_rsa_padding| and related functions. +// wrap `rsa` in an `EVP_PKEY` and use `EVP_PKEY_encrypt` with +// `EVP_PKEY_CTX_set_rsa_padding` and related functions. // // WARNING: this function is dangerous because it breaks the usual return value -// convention. Use |RSA_encrypt| instead. +// convention. Use `RSA_encrypt` instead. OPENSSL_EXPORT int RSA_public_encrypt(size_t flen, const uint8_t *from, uint8_t *to, RSA *rsa, int padding); -// RSA_private_decrypt decrypts |flen| bytes from |from| with the public key in -// |rsa| and writes the plaintext to |to|. The |to| buffer must have at least -// |RSA_size| bytes of space. It returns the number of bytes written, or -1 on -// error. The |padding| argument must be one of the |RSA_*_PADDING| values. If -// in doubt, use |RSA_PKCS1_OAEP_PADDING| for new protocols. Passing -// |RSA_PKCS1_PADDING| into this function is deprecated and insecure. See -// |RSA_decrypt|. +// RSA_private_decrypt decrypts `flen` bytes from `from` with the public key in +// `rsa` and writes the plaintext to `to`. The `to` buffer must have at least +// `RSA_size` bytes of space. It returns the number of bytes written, or -1 on +// error. The `padding` argument must be one of the `RSA_*_PADDING` values. If +// in doubt, use `RSA_PKCS1_OAEP_PADDING` for new protocols. Passing +// `RSA_PKCS1_PADDING` into this function is deprecated and insecure. See +// `RSA_decrypt`. // -// When |padding| is |RSA_PKCS1_OAEP_PADDING|, this function has no way to set +// When `padding` is `RSA_PKCS1_OAEP_PADDING`, this function has no way to set // the OAEP or MGF-1 digest, so it is always SHA-1. For other OAEP parameters, -// wrap |rsa| in an |EVP_PKEY| and use |EVP_PKEY_decrypt| with -// |EVP_PKEY_CTX_set_rsa_padding| and related functions. +// wrap `rsa` in an `EVP_PKEY` and use `EVP_PKEY_decrypt` with +// `EVP_PKEY_CTX_set_rsa_padding` and related functions. // // WARNING: this function is dangerous because it breaks the usual return value -// convention. Use |RSA_decrypt| instead. +// convention. Use `RSA_decrypt` instead. OPENSSL_EXPORT int RSA_private_decrypt(size_t flen, const uint8_t *from, uint8_t *to, RSA *rsa, int padding); @@ -308,19 +313,19 @@ OPENSSL_EXPORT int RSA_private_decrypt(size_t flen, const uint8_t *from, // These functions are considered non-mutating for thread-safety purposes and // may be used concurrently. -// RSA_sign signs |digest_len| bytes of digest from |digest| with |rsa| using -// RSASSA-PKCS1-v1_5. It writes, at most, |RSA_size(rsa)| bytes to |out|. On +// RSA_sign signs `digest_len` bytes of digest from `digest` with `rsa` using +// RSASSA-PKCS1-v1_5. It writes, at most, `RSA_size(rsa)` bytes to `out`. On // successful return, the actual number of bytes written is written to -// |*out_len|. +// `*out_len`. // -// The |hash_nid| argument identifies the hash function used to calculate -// |digest| and is embedded in the resulting signature. For example, it might be -// |NID_sha256|. +// The `hash_nid` argument identifies the hash function used to calculate +// `digest` and is embedded in the resulting signature. For example, it might be +// `NID_sha256`. // // It returns 1 on success and zero on error. // -// WARNING: |digest| must be the result of hashing the data to be signed with -// |hash_nid|. Passing unhashed inputs will not result in a secure signature +// WARNING: `digest` must be the result of hashing the data to be signed with +// `hash_nid`. Passing unhashed inputs will not result in a secure signature // scheme. OPENSSL_EXPORT int RSA_sign(int hash_nid, const uint8_t *digest, size_t digest_len, uint8_t *out, unsigned *out_len, @@ -335,139 +340,139 @@ OPENSSL_EXPORT int RSA_sign(int hash_nid, const uint8_t *digest, // with FIPS 186-5. #define RSA_PSS_SALTLEN_AUTO (-2) -// RSA_sign_pss_mgf1 signs |digest_len| bytes from |digest| with the public key -// from |rsa| using RSASSA-PSS with MGF1 as the mask generation function. It -// writes, at most, |max_out| bytes of signature data to |out|. The |max_out| -// argument must be, at least, |RSA_size| in order to ensure success. It returns +// RSA_sign_pss_mgf1 signs `digest_len` bytes from `digest` with the public key +// from `rsa` using RSASSA-PSS with MGF1 as the mask generation function. It +// writes, at most, `max_out` bytes of signature data to `out`. The `max_out` +// argument must be, at least, `RSA_size` in order to ensure success. It returns // 1 on success or zero on error. // -// The |md| and |mgf1_md| arguments identify the hash used to calculate |digest| -// and the MGF1 hash, respectively. If |mgf1_md| is NULL, |md| is +// The `md` and `mgf1_md` arguments identify the hash used to calculate `digest` +// and the MGF1 hash, respectively. If `mgf1_md` is NULL, `md` is // used. // -// |salt_len| specifies the expected salt length in bytes. If |salt_len| is -// |RSA_PSS_SALTLEN_DIGEST|, then the salt length is the same as the hash -// length. If |RSA_PSS_SALTLEN_AUTO|, then the salt length is maximal given the -// size of |rsa|. If unsure, use |RSA_PSS_SALTLEN_DIGEST|. +// `salt_len` specifies the expected salt length in bytes. If `salt_len` is +// `RSA_PSS_SALTLEN_DIGEST`, then the salt length is the same as the hash +// length. If `RSA_PSS_SALTLEN_AUTO`, then the salt length is maximal given the +// size of `rsa`. If unsure, use `RSA_PSS_SALTLEN_DIGEST`. // -// WARNING: |digest| must be the result of hashing the data to be signed with -// |md|. Passing unhashed inputs will not result in a secure signature scheme. +// WARNING: `digest` must be the result of hashing the data to be signed with +// `md`. Passing unhashed inputs will not result in a secure signature scheme. OPENSSL_EXPORT int RSA_sign_pss_mgf1(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out, const uint8_t *digest, size_t digest_len, const EVP_MD *md, const EVP_MD *mgf1_md, int salt_len); // RSA_sign_raw performs the private key portion of computing a signature with -// |rsa|. It writes, at most, |max_out| bytes of signature data to |out|. The -// |max_out| argument must be, at least, |RSA_size| in order to ensure the +// `rsa`. It writes, at most, `max_out` bytes of signature data to `out`. The +// `max_out` argument must be, at least, `RSA_size` in order to ensure the // output fits. It returns 1 on success or zero on error. // -// If |padding| is |RSA_PKCS1_PADDING|, this function wraps |in| with the +// If `padding` is `RSA_PKCS1_PADDING`, this function wraps `in` with the // padding portion of RSASSA-PKCS1-v1_5 and then performs the raw private key // operation. The caller is responsible for hashing the input and wrapping it in // a DigestInfo structure. // -// If |padding| is |RSA_NO_PADDING|, this function only performs the raw private -// key operation, interpreting |in| as a integer modulo n. The caller is +// If `padding` is `RSA_NO_PADDING`, this function only performs the raw private +// key operation, interpreting `in` as an integer modulo n. The caller is // responsible for hashing the input and encoding it for the signature scheme // being implemented. // // WARNING: This function is a building block for a signature scheme, not a -// complete one. |in| must be the result of hashing and encoding the data as +// complete one. `in` must be the result of hashing and encoding the data as // needed for the scheme being implemented. Passing in arbitrary inputs will not // result in a secure signature scheme. OPENSSL_EXPORT int RSA_sign_raw(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out, const uint8_t *in, size_t in_len, int padding); -// RSA_verify verifies that |sig_len| bytes from |sig| are a valid, -// RSASSA-PKCS1-v1_5 signature of |digest_len| bytes at |digest| by |rsa|. +// RSA_verify verifies that `sig_len` bytes from `sig` are a valid, +// RSASSA-PKCS1-v1_5 signature of `digest_len` bytes at `digest` by `rsa`. // -// The |hash_nid| argument identifies the hash function used to calculate -// |digest| and is embedded in the resulting signature in order to prevent hash -// confusion attacks. For example, it might be |NID_sha256|. +// The `hash_nid` argument identifies the hash function used to calculate +// `digest` and is embedded in the resulting signature in order to prevent hash +// confusion attacks. For example, it might be `NID_sha256`. // // It returns one if the signature is valid and zero otherwise. // // WARNING: this differs from the original, OpenSSL function which additionally // returned -1 on error. // -// WARNING: |digest| must be the result of hashing the data to be verified with -// |hash_nid|. Passing unhashed input will not result in a secure signature +// WARNING: `digest` must be the result of hashing the data to be verified with +// `hash_nid`. Passing unhashed input will not result in a secure signature // scheme. OPENSSL_EXPORT int RSA_verify(int hash_nid, const uint8_t *digest, size_t digest_len, const uint8_t *sig, size_t sig_len, RSA *rsa); -// RSA_verify_pss_mgf1 verifies that |sig_len| bytes from |sig| are a valid, -// RSASSA-PSS signature of |digest_len| bytes at |digest| by |rsa|. It returns +// RSA_verify_pss_mgf1 verifies that `sig_len` bytes from `sig` are a valid, +// RSASSA-PSS signature of `digest_len` bytes at `digest` by `rsa`. It returns // one if the signature is valid and zero otherwise. MGF1 is used as the mask // generation function. // -// The |md| and |mgf1_md| arguments identify the hash used to calculate |digest| -// and the MGF1 hash, respectively. If |mgf1_md| is NULL, |md| is -// used. |salt_len| specifies the expected salt length in bytes. +// The `md` and `mgf1_md` arguments identify the hash used to calculate `digest` +// and the MGF1 hash, respectively. If `mgf1_md` is NULL, `md` is +// used. `salt_len` specifies the expected salt length in bytes. // -// If |salt_len| is |RSA_PSS_SALTLEN_DIGEST|, then the salt length is the same -// as the hash length. If |RSA_PSS_SALTLEN_AUTO|, then the salt length is -// recovered and all values accepted. If unsure, use |RSA_PSS_SALTLEN_DIGEST|. +// If `salt_len` is `RSA_PSS_SALTLEN_DIGEST`, then the salt length is the same +// as the hash length. If `RSA_PSS_SALTLEN_AUTO`, then the salt length is +// recovered and all values accepted. If unsure, use `RSA_PSS_SALTLEN_DIGEST`. // -// WARNING: |digest| must be the result of hashing the data to be verified with -// |md|. Passing unhashed input will not result in a secure signature scheme. +// WARNING: `digest` must be the result of hashing the data to be verified with +// `md`. Passing unhashed input will not result in a secure signature scheme. OPENSSL_EXPORT int RSA_verify_pss_mgf1(RSA *rsa, const uint8_t *digest, size_t digest_len, const EVP_MD *md, const EVP_MD *mgf1_md, int salt_len, const uint8_t *sig, size_t sig_len); -// RSA_verify_raw performs the public key portion of verifying |in_len| bytes of -// signature from |in| using the public key from |rsa|. On success, it returns -// one and writes, at most, |max_out| bytes of output to |out|. The |max_out| -// argument must be, at least, |RSA_size| in order to ensure the output fits. On +// RSA_verify_raw performs the public key portion of verifying `in_len` bytes of +// signature from `in` using the public key from `rsa`. On success, it returns +// one and writes, at most, `max_out` bytes of output to `out`. The `max_out` +// argument must be, at least, `RSA_size` in order to ensure the output fits. On // failure or invalid input, it returns zero. // -// If |padding| is |RSA_PKCS1_PADDING|, this function checks the padding portion +// If `padding` is `RSA_PKCS1_PADDING`, this function checks the padding portion // of RSASSA-PKCS1-v1_5 and outputs the remainder of the encoded digest. The // caller is responsible for checking the output is a DigestInfo-wrapped digest // of the message. // -// If |padding| is |RSA_NO_PADDING|, this function only performs the raw public +// If `padding` is `RSA_NO_PADDING`, this function only performs the raw public // key operation. The caller is responsible for checking the output is a valid // result for the signature scheme being implemented. // // WARNING: This function is a building block for a signature scheme, not a -// complete one. Checking for arbitrary strings in |out| will not result in a +// complete one. Checking for arbitrary strings in `out` will not result in a // secure signature scheme. OPENSSL_EXPORT int RSA_verify_raw(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out, const uint8_t *in, size_t in_len, int padding); // RSA_private_encrypt performs the private key portion of computing a signature -// with |rsa|. It takes |flen| bytes from |from| as input and writes the result -// to |to|. The |to| buffer must have at least |RSA_size| bytes of space. It +// with `rsa`. It takes `flen` bytes from `from` as input and writes the result +// to `to`. The `to` buffer must have at least `RSA_size` bytes of space. It // returns the number of bytes written, or -1 on error. // -// For the interpretation of |padding| and the input, see |RSA_sign_raw|. +// For the interpretation of `padding` and the input, see `RSA_sign_raw`. // // WARNING: This function is a building block for a signature scheme, not a -// complete one. See |RSA_sign_raw| for details. +// complete one. See `RSA_sign_raw` for details. // // WARNING: This function is dangerous because it breaks the usual return value -// convention. Use |RSA_sign_raw| instead. +// convention. Use `RSA_sign_raw` instead. OPENSSL_EXPORT int RSA_private_encrypt(size_t flen, const uint8_t *from, uint8_t *to, RSA *rsa, int padding); -// RSA_public_decrypt performs the public key portion of verifying |flen| bytes -// of signature from |from| using the public key from |rsa|. It writes the -// result to |to|, which must have at least |RSA_size| bytes of space. It +// RSA_public_decrypt performs the public key portion of verifying `flen` bytes +// of signature from `from` using the public key from `rsa`. It writes the +// result to `to`, which must have at least `RSA_size` bytes of space. It // returns the number of bytes written, or -1 on error. // -// For the interpretation of |padding| and the result, see |RSA_verify_raw|. +// For the interpretation of `padding` and the result, see `RSA_verify_raw`. // // WARNING: This function is a building block for a signature scheme, not a -// complete one. See |RSA_verify_raw| for details. +// complete one. See `RSA_verify_raw` for details. // // WARNING: This function is dangerous because it breaks the usual return value -// convention. Use |RSA_verify_raw| instead. +// convention. Use `RSA_verify_raw` instead. OPENSSL_EXPORT int RSA_public_decrypt(size_t flen, const uint8_t *from, uint8_t *to, RSA *rsa, int padding); @@ -475,36 +480,36 @@ OPENSSL_EXPORT int RSA_public_decrypt(size_t flen, const uint8_t *from, // Utility functions. // RSA_size returns the number of bytes in the modulus, which is also the size -// of a signature or encrypted value using |rsa|. +// of a signature or encrypted value using `rsa`. OPENSSL_EXPORT unsigned RSA_size(const RSA *rsa); -// RSA_is_opaque returns one if |rsa| is opaque and doesn't expose its key +// RSA_is_opaque returns one if `rsa` is opaque and doesn't expose its key // material. Otherwise it returns zero. OPENSSL_EXPORT int RSA_is_opaque(const RSA *rsa); -// RSAPublicKey_dup allocates a fresh |RSA| and copies the public key from -// |rsa| into it. It returns the fresh |RSA| object, or NULL on error. +// RSAPublicKey_dup allocates a fresh `RSA` and copies the public key from +// `rsa` into it. It returns the fresh `RSA` object, or NULL on error. OPENSSL_EXPORT RSA *RSAPublicKey_dup(const RSA *rsa); -// RSAPrivateKey_dup allocates a fresh |RSA| and copies the private key from -// |rsa| into it. It returns the fresh |RSA| object, or NULL on error. +// RSAPrivateKey_dup allocates a fresh `RSA` and copies the private key from +// `rsa` into it. It returns the fresh `RSA` object, or NULL on error. OPENSSL_EXPORT RSA *RSAPrivateKey_dup(const RSA *rsa); -// RSA_check_key performs basic validity tests on |rsa|. It returns one if +// RSA_check_key performs basic validity tests on `rsa`. It returns one if // they pass and zero otherwise. Opaque keys and public keys always pass. If it // returns zero then a more detailed error is available on the error queue. OPENSSL_EXPORT int RSA_check_key(const RSA *rsa); -// RSA_check_fips performs public key validity tests on |key|. It returns one if +// RSA_check_fips performs public key validity tests on `key`. It returns one if // they pass and zero otherwise. Opaque keys always fail. This function does not -// mutate |rsa| for thread-safety purposes and may be used concurrently. +// mutate `rsa` for thread-safety purposes and may be used concurrently. OPENSSL_EXPORT int RSA_check_fips(RSA *key); -// RSA_verify_PKCS1_PSS_mgf1 verifies that |EM| is a correct PSS padding of -// |mHash|, where |mHash| is a digest produced by |Hash|. |EM| must point to -// exactly |RSA_size(rsa)| bytes of data. The |mgf1Hash| argument specifies the -// hash function for generating the mask. If NULL, |Hash| is used. The |sLen| -// argument specifies the expected salt length in bytes. If |sLen| is -1 then +// RSA_verify_PKCS1_PSS_mgf1 verifies that `EM` is a correct PSS padding of +// `mHash`, where `mHash` is a digest produced by `Hash`. `EM` must point to +// exactly `RSA_size(rsa)` bytes of data. The `mgf1Hash` argument specifies the +// hash function for generating the mask. If NULL, `Hash` is used. The `sLen` +// argument specifies the expected salt length in bytes. If `sLen` is -1 then // the salt length is the same as the hash length. If -2, then the salt length // is recovered and all values accepted. // @@ -513,34 +518,34 @@ OPENSSL_EXPORT int RSA_check_fips(RSA *key); // It returns one on success or zero on error. // // This function implements only the low-level padding logic. Use -// |RSA_verify_pss_mgf1| instead. +// `RSA_verify_pss_mgf1` instead. OPENSSL_EXPORT int RSA_verify_PKCS1_PSS_mgf1(const RSA *rsa, const uint8_t *mHash, const EVP_MD *Hash, const EVP_MD *mgf1Hash, const uint8_t *EM, int sLen); -// RSA_padding_add_PKCS1_PSS_mgf1 writes a PSS padding of |mHash| to |EM|, -// where |mHash| is a digest produced by |Hash|. |RSA_size(rsa)| bytes of -// output will be written to |EM|. The |mgf1Hash| argument specifies the hash -// function for generating the mask. If NULL, |Hash| is used. The |sLen| -// argument specifies the expected salt length in bytes. If |sLen| is -1 then +// RSA_padding_add_PKCS1_PSS_mgf1 writes a PSS padding of `mHash` to `EM`, +// where `mHash` is a digest produced by `Hash`. `RSA_size(rsa)` bytes of +// output will be written to `EM`. The `mgf1Hash` argument specifies the hash +// function for generating the mask. If NULL, `Hash` is used. The `sLen` +// argument specifies the expected salt length in bytes. If `sLen` is -1 then // the salt length is the same as the hash length. If -2, then the salt length -// is maximal given the space in |EM|. +// is maximal given the space in `EM`. // // It returns one on success or zero on error. // // This function implements only the low-level padding logic. Use -// |RSA_sign_pss_mgf1| instead. +// `RSA_sign_pss_mgf1` instead. OPENSSL_EXPORT int RSA_padding_add_PKCS1_PSS_mgf1(const RSA *rsa, uint8_t *EM, const uint8_t *mHash, const EVP_MD *Hash, const EVP_MD *mgf1Hash, int sLen); -// RSA_padding_add_PKCS1_OAEP_mgf1 writes an OAEP padding of |from| to |to| -// with the given parameters and hash functions. If |md| is NULL then SHA-1 is -// used. If |mgf1md| is NULL then the value of |md| is used (which means SHA-1 +// RSA_padding_add_PKCS1_OAEP_mgf1 writes an OAEP padding of `from` to `to` +// with the given parameters and hash functions. If `md` is NULL then SHA-1 is +// used. If `mgf1md` is NULL then the value of `md` is used (which means SHA-1 // if that, in turn, is NULL). // // It returns one on success or zero on error. @@ -549,10 +554,10 @@ OPENSSL_EXPORT int RSA_padding_add_PKCS1_OAEP_mgf1( const uint8_t *param, size_t param_len, const EVP_MD *md, const EVP_MD *mgf1md); -// RSA_add_pkcs1_prefix builds a version of |digest| prefixed with the -// DigestInfo header for the given hash function and sets |out_msg| to point to -// it. On successful return, if |*is_alloced| is one, the caller must release -// |*out_msg| with |OPENSSL_free|. +// RSA_add_pkcs1_prefix builds a version of `digest` prefixed with the +// DigestInfo header for the given hash function and sets `out_msg` to point to +// it. On successful return, if `*is_alloced` is one, the caller must release +// `*out_msg` with `OPENSSL_free`. OPENSSL_EXPORT int RSA_add_pkcs1_prefix(uint8_t **out_msg, size_t *out_msg_len, int *is_alloced, int hash_nid, const uint8_t *digest, @@ -562,45 +567,45 @@ OPENSSL_EXPORT int RSA_add_pkcs1_prefix(uint8_t **out_msg, size_t *out_msg_len, // ASN.1 functions. // RSA_parse_public_key parses a DER-encoded RSAPublicKey structure (RFC 8017) -// from |cbs| and advances |cbs|. It returns a newly-allocated |RSA| or NULL on +// from `cbs` and advances `cbs`. It returns a newly-allocated `RSA` or NULL on // error. OPENSSL_EXPORT RSA *RSA_parse_public_key(CBS *cbs); -// RSA_public_key_from_bytes parses |in| as a DER-encoded RSAPublicKey structure -// (RFC 8017). It returns a newly-allocated |RSA| or NULL on error. +// RSA_public_key_from_bytes parses `in` as a DER-encoded RSAPublicKey structure +// (RFC 8017). It returns a newly-allocated `RSA` or NULL on error. OPENSSL_EXPORT RSA *RSA_public_key_from_bytes(const uint8_t *in, size_t in_len); -// RSA_marshal_public_key marshals |rsa| as a DER-encoded RSAPublicKey structure -// (RFC 8017) and appends the result to |cbb|. It returns one on success and +// RSA_marshal_public_key marshals `rsa` as a DER-encoded RSAPublicKey structure +// (RFC 8017) and appends the result to `cbb`. It returns one on success and // zero on failure. OPENSSL_EXPORT int RSA_marshal_public_key(CBB *cbb, const RSA *rsa); -// RSA_public_key_to_bytes marshals |rsa| as a DER-encoded RSAPublicKey -// structure (RFC 8017) and, on success, sets |*out_bytes| to a newly allocated +// RSA_public_key_to_bytes marshals `rsa` as a DER-encoded RSAPublicKey +// structure (RFC 8017) and, on success, sets `*out_bytes` to a newly allocated // buffer containing the result and returns one. Otherwise, it returns zero. The -// result should be freed with |OPENSSL_free|. +// result should be freed with `OPENSSL_free`. OPENSSL_EXPORT int RSA_public_key_to_bytes(uint8_t **out_bytes, size_t *out_len, const RSA *rsa); // RSA_parse_private_key parses a DER-encoded RSAPrivateKey structure (RFC 8017) -// from |cbs| and advances |cbs|. It returns a newly-allocated |RSA| or NULL on +// from `cbs` and advances `cbs`. It returns a newly-allocated `RSA` or NULL on // error. OPENSSL_EXPORT RSA *RSA_parse_private_key(CBS *cbs); -// RSA_private_key_from_bytes parses |in| as a DER-encoded RSAPrivateKey -// structure (RFC 8017). It returns a newly-allocated |RSA| or NULL on error. +// RSA_private_key_from_bytes parses `in` as a DER-encoded RSAPrivateKey +// structure (RFC 8017). It returns a newly-allocated `RSA` or NULL on error. OPENSSL_EXPORT RSA *RSA_private_key_from_bytes(const uint8_t *in, size_t in_len); -// RSA_marshal_private_key marshals |rsa| as a DER-encoded RSAPrivateKey -// structure (RFC 8017) and appends the result to |cbb|. It returns one on +// RSA_marshal_private_key marshals `rsa` as a DER-encoded RSAPrivateKey +// structure (RFC 8017) and appends the result to `cbb`. It returns one on // success and zero on failure. OPENSSL_EXPORT int RSA_marshal_private_key(CBB *cbb, const RSA *rsa); -// RSA_private_key_to_bytes marshals |rsa| as a DER-encoded RSAPrivateKey -// structure (RFC 8017) and, on success, sets |*out_bytes| to a newly allocated +// RSA_private_key_to_bytes marshals `rsa` as a DER-encoded RSAPrivateKey +// structure (RFC 8017) and, on success, sets `*out_bytes` to a newly allocated // buffer containing the result and returns one. Otherwise, it returns zero. The -// result should be freed with |OPENSSL_free|. +// result should be freed with `OPENSSL_free`. OPENSSL_EXPORT int RSA_private_key_to_bytes(uint8_t **out_bytes, size_t *out_len, const RSA *rsa); @@ -610,7 +615,7 @@ OPENSSL_EXPORT int RSA_private_key_to_bytes(uint8_t **out_bytes, // These functions allow creating RSA keys with obscure combinations of // parameters. -// RSA_new_private_key_no_crt behaves like |RSA_new_private_key| but constructs +// RSA_new_private_key_no_crt behaves like `RSA_new_private_key` but constructs // an RSA key without CRT coefficients. // // Keys created by this function will be less performant and cannot be @@ -618,7 +623,7 @@ OPENSSL_EXPORT int RSA_private_key_to_bytes(uint8_t **out_bytes, OPENSSL_EXPORT RSA *RSA_new_private_key_no_crt(const BIGNUM *n, const BIGNUM *e, const BIGNUM *d); -// RSA_new_private_key_no_e behaves like |RSA_new_private_key| but constructs an +// RSA_new_private_key_no_e behaves like `RSA_new_private_key` but constructs an // RSA key without CRT parameters or public exponent. // // Keys created by this function will be less performant, cannot be serialized, @@ -626,21 +631,21 @@ OPENSSL_EXPORT RSA *RSA_new_private_key_no_crt(const BIGNUM *n, const BIGNUM *e, // attacks. OPENSSL_EXPORT RSA *RSA_new_private_key_no_e(const BIGNUM *n, const BIGNUM *d); -// RSA_new_public_key_large_e behaves like |RSA_new_public_key| but allows any -// |e| up to |n|. +// RSA_new_public_key_large_e behaves like `RSA_new_public_key` but allows any +// `e` up to `n`. // // BoringSSL typically bounds public exponents as a denial-of-service // mitigation. Keys created by this function may perform worse than those -// created by |RSA_new_public_key|. +// created by `RSA_new_public_key`. OPENSSL_EXPORT RSA *RSA_new_public_key_large_e(const BIGNUM *n, const BIGNUM *e); -// RSA_new_private_key_large_e behaves like |RSA_new_private_key| but allows any -// |e| up to |n|. +// RSA_new_private_key_large_e behaves like `RSA_new_private_key` but allows any +// `e` up to `n`. // // BoringSSL typically bounds public exponents as a denial-of-service // mitigation. Keys created by this function may perform worse than those -// created by |RSA_new_private_key|. +// created by `RSA_new_private_key`. OPENSSL_EXPORT RSA *RSA_new_private_key_large_e( const BIGNUM *n, const BIGNUM *e, const BIGNUM *d, const BIGNUM *p, const BIGNUM *q, const BIGNUM *dmp1, const BIGNUM *dmq1, @@ -649,7 +654,7 @@ OPENSSL_EXPORT RSA *RSA_new_private_key_large_e( // ex_data functions. // -// See |ex_data.h| for details. +// See `ex_data.h` for details. OPENSSL_EXPORT int RSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused, @@ -674,12 +679,12 @@ OPENSSL_EXPORT void *RSA_get_ex_data(const RSA *rsa, int idx); // RSA_FLAG_NO_PUBLIC_EXPONENT indicates that private keys without a public // exponent are allowed. This is an internal constant. Use -// |RSA_new_private_key_no_e| to construct such keys. +// `RSA_new_private_key_no_e` to construct such keys. #define RSA_FLAG_NO_PUBLIC_EXPONENT 0x40 // RSA_FLAG_LARGE_PUBLIC_EXPONENT indicates that keys with a large public // exponent are allowed. This is an internal constant. Use -// |RSA_new_public_key_large_e| and |RSA_new_private_key_large_e| to construct +// `RSA_new_public_key_large_e` and `RSA_new_private_key_large_e` to construct // such keys. #define RSA_FLAG_LARGE_PUBLIC_EXPONENT 0x80 @@ -694,11 +699,11 @@ OPENSSL_EXPORT void *RSA_get_ex_data(const RSA *rsa, int idx); #define RSA_METHOD_FLAG_NO_CHECK RSA_FLAG_OPAQUE -// RSA_flags returns the flags for |rsa|. These are a bitwise OR of |RSA_FLAG_*| +// RSA_flags returns the flags for `rsa`. These are a bitwise OR of `RSA_FLAG_*` // constants. OPENSSL_EXPORT int RSA_flags(const RSA *rsa); -// RSA_test_flags returns the subset of flags in |flags| which are set in |rsa|. +// RSA_test_flags returns the subset of flags in `flags` which are set in `rsa`. OPENSSL_EXPORT int RSA_test_flags(const RSA *rsa, int flags); // RSA_blinding_on returns one. @@ -707,57 +712,57 @@ OPENSSL_EXPORT int RSA_blinding_on(RSA *rsa, BN_CTX *ctx); // RSA_blinding_off does nothing. OPENSSL_EXPORT void RSA_blinding_off(RSA *rsa); -// RSA_generate_key behaves like |RSA_generate_key_ex|, which is what you -// should use instead. It returns NULL on error, or a newly-allocated |RSA| on -// success. This function is provided for compatibility only. The |callback| -// and |cb_arg| parameters must be NULL. +// RSA_generate_key behaves like `RSA_generate_key_ex`, which is what you +// should use instead. It returns NULL on error, or a newly-allocated `RSA` on +// success. This function is provided for compatibility only. The `callback` +// and `cb_arg` parameters must be NULL. OPENSSL_EXPORT RSA *RSA_generate_key(int bits, uint64_t e, void *callback, void *cb_arg); // d2i_RSAPublicKey parses a DER-encoded RSAPublicKey structure (RFC 8017) from -// |len| bytes at |*inp|, as described in |d2i_SAMPLE|. +// `len` bytes at `*inp`, as described in `d2i_SAMPLE`. // -// Use |RSA_parse_public_key| instead. +// Use `RSA_parse_public_key` instead. OPENSSL_EXPORT RSA *d2i_RSAPublicKey(RSA **out, const uint8_t **inp, long len); -// i2d_RSAPublicKey marshals |in| to a DER-encoded RSAPublicKey structure (RFC -// 8017), as described in |i2d_SAMPLE|. +// i2d_RSAPublicKey marshals `in` to a DER-encoded RSAPublicKey structure (RFC +// 8017), as described in `i2d_SAMPLE`. // -// Use |RSA_marshal_public_key| instead. +// Use `RSA_marshal_public_key` instead. OPENSSL_EXPORT int i2d_RSAPublicKey(const RSA *in, uint8_t **outp); // d2i_RSAPrivateKey parses a DER-encoded RSAPrivateKey structure (RFC 8017) -// from |len| bytes at |*inp|, as described in |d2i_SAMPLE|. +// from `len` bytes at `*inp`, as described in `d2i_SAMPLE`. // -// Use |RSA_parse_private_key| instead. +// Use `RSA_parse_private_key` instead. OPENSSL_EXPORT RSA *d2i_RSAPrivateKey(RSA **out, const uint8_t **inp, long len); -// i2d_RSAPrivateKey marshals |in| to a DER-encoded RSAPrivateKey structure (RFC -// 8017), as described in |i2d_SAMPLE|. +// i2d_RSAPrivateKey marshals `in` to a DER-encoded RSAPrivateKey structure (RFC +// 8017), as described in `i2d_SAMPLE`. // -// Use |RSA_marshal_private_key| instead. +// Use `RSA_marshal_private_key` instead. OPENSSL_EXPORT int i2d_RSAPrivateKey(const RSA *in, uint8_t **outp); -// RSA_padding_add_PKCS1_PSS acts like |RSA_padding_add_PKCS1_PSS_mgf1| but the -// |mgf1Hash| parameter of the latter is implicitly set to |Hash|. +// RSA_padding_add_PKCS1_PSS acts like `RSA_padding_add_PKCS1_PSS_mgf1` but the +// `mgf1Hash` parameter of the latter is implicitly set to `Hash`. // // This function implements only the low-level padding logic. Use -// |RSA_sign_pss_mgf1| instead. +// `RSA_sign_pss_mgf1` instead. OPENSSL_EXPORT int RSA_padding_add_PKCS1_PSS(const RSA *rsa, uint8_t *EM, const uint8_t *mHash, const EVP_MD *Hash, int sLen); -// RSA_verify_PKCS1_PSS acts like |RSA_verify_PKCS1_PSS_mgf1| but the -// |mgf1Hash| parameter of the latter is implicitly set to |Hash|. +// RSA_verify_PKCS1_PSS acts like `RSA_verify_PKCS1_PSS_mgf1` but the +// `mgf1Hash` parameter of the latter is implicitly set to `Hash`. // // This function implements only the low-level padding logic. Use -// |RSA_verify_pss_mgf1| instead. +// `RSA_verify_pss_mgf1` instead. OPENSSL_EXPORT int RSA_verify_PKCS1_PSS(const RSA *rsa, const uint8_t *mHash, const EVP_MD *Hash, const uint8_t *EM, int sLen); -// RSA_padding_add_PKCS1_OAEP acts like |RSA_padding_add_PKCS1_OAEP_mgf1| but -// the |md| and |mgf1md| parameters of the latter are implicitly set to NULL, +// RSA_padding_add_PKCS1_OAEP acts like `RSA_padding_add_PKCS1_OAEP_mgf1` but +// the `md` and `mgf1md` parameters of the latter are implicitly set to NULL, // which means SHA-1. OPENSSL_EXPORT int RSA_padding_add_PKCS1_OAEP(uint8_t *to, size_t to_len, const uint8_t *from, @@ -765,22 +770,22 @@ OPENSSL_EXPORT int RSA_padding_add_PKCS1_OAEP(uint8_t *to, size_t to_len, const uint8_t *param, size_t param_len); -// RSA_print prints a textual representation of |rsa| to |bio|. It returns one +// RSA_print prints a textual representation of `rsa` to `bio`. It returns one // on success or zero otherwise. OPENSSL_EXPORT int RSA_print(BIO *bio, const RSA *rsa, int indent); // RSA_get0_pss_params returns NULL. In OpenSSL, this function retries RSA-PSS -// parameters associated with |RSA| objects, but BoringSSL does not enable the +// parameters associated with `RSA` objects, but BoringSSL does not enable the // id-RSASSA-PSS key encoding by default. // // WARNING: BoringSSL does support id-RSASSA-PSS parameters when callers opt in -// (see |EVP_pkey_rsa_pss_sha256| and others). We currently assume such callers +// (see `EVP_pkey_rsa_pss_sha256` and others). We currently assume such callers // do not need this function. Callers that opt into id-RSASSA-PSS support and // require this functionality should contact the BoringSSL team. OPENSSL_EXPORT const RSA_PSS_PARAMS *RSA_get0_pss_params(const RSA *rsa); -// RSA_new_method_no_e returns a newly-allocated |RSA| object backed by -// |engine|, with a public modulus of |n| and no known public exponent. +// RSA_new_method_no_e returns a newly-allocated `RSA` object backed by +// `engine`, with a public modulus of `n` and no known public exponent. // // Do not use this function. It exists only to support Conscrypt, whose use // should be replaced with a more sound mechanism. See @@ -799,17 +804,17 @@ struct rsa_meth_st { int (*sign)(int type, const uint8_t *m, unsigned int m_length, uint8_t *sigret, unsigned int *siglen, const RSA *rsa); - // These functions mirror the |RSA_*| functions of the same name. + // These functions mirror the `RSA_*` functions of the same name. int (*sign_raw)(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out, const uint8_t *in, size_t in_len, int padding); int (*decrypt)(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out, const uint8_t *in, size_t in_len, int padding); - // private_transform takes a big-endian integer from |in|, calculates the + // private_transform takes a big-endian integer from `in`, calculates the // d'th power of it, modulo the RSA modulus and writes the result as a - // big-endian integer to |out|. Both |in| and |out| are |len| bytes long and - // |len| is always equal to |RSA_size(rsa)|. If the result of the transform - // can be represented in fewer than |len| bytes, then |out| must be zero + // big-endian integer to `out`. Both `in` and `out` are `len` bytes long and + // `len` is always equal to `RSA_size(rsa)`. If the result of the transform + // can be represented in fewer than `len` bytes, then `out` must be zero // padded on the left. // // It returns one on success and zero otherwise. @@ -832,7 +837,7 @@ extern "C++" { BSSL_NAMESPACE_BEGIN BORINGSSL_MAKE_DELETER(RSA, RSA_free) -BORINGSSL_MAKE_UP_REF(RSA, RSA_up_ref) +BORINGSSL_MAKE_UP_REF(RSA, RSA_dup_ref) BSSL_NAMESPACE_END diff --git a/third_party/boringssl/src/include/openssl/sha.h b/third_party/boringssl/src/include/openssl/sha.h index ba17bfaa1..da94386ff 100644 --- a/third_party/boringssl/src/include/openssl/sha.h +++ b/third_party/boringssl/src/include/openssl/sha.h @@ -16,6 +16,7 @@ #define OPENSSL_HEADER_SHA_H #include // IWYU pragma: export +#include // `sha.h` historically included SHA-1 and SHA-2 hash functions. So, for // backward compatibility `sha2.h` is included here. New uses of this header @@ -27,7 +28,7 @@ extern "C" { #endif -// The SHA family of hash functions (SHA-1 and SHA-2). +// The SHA-1 hash function. // SHA-1. @@ -38,34 +39,11 @@ extern "C" { // SHA_DIGEST_LENGTH is the length of a SHA-1 digest. #define SHA_DIGEST_LENGTH 20 -// SHA1_Init initialises |sha| and returns one. -OPENSSL_EXPORT int SHA1_Init(SHA_CTX *sha); - -// SHA1_Update adds |len| bytes from |data| to |sha| and returns one. -OPENSSL_EXPORT int SHA1_Update(SHA_CTX *sha, const void *data, size_t len); - -// SHA1_Final adds the final padding to |sha| and writes the resulting digest to -// |out|, which must have at least |SHA_DIGEST_LENGTH| bytes of space. It -// returns one. -OPENSSL_EXPORT int SHA1_Final(uint8_t out[SHA_DIGEST_LENGTH], SHA_CTX *sha); - -// SHA1 writes the digest of |len| bytes from |data| to |out| and returns -// |out|. There must be at least |SHA_DIGEST_LENGTH| bytes of space in -// |out|. -OPENSSL_EXPORT uint8_t *SHA1(const uint8_t *data, size_t len, - uint8_t out[SHA_DIGEST_LENGTH]); - -// SHA1_Transform is a low-level function that performs a single, SHA-1 block -// transformation using the state from |sha| and |SHA_CBLOCK| bytes from -// |block|. -OPENSSL_EXPORT void SHA1_Transform(SHA_CTX *sha, - const uint8_t block[SHA_CBLOCK]); - struct sha_state_st { #if defined(__cplusplus) || defined(OPENSSL_WINDOWS) uint32_t h[5]; #else - // wpa_supplicant accesses |h0|..|h4| so we must support those names for + // wpa_supplicant accesses `h0`..`h4` so we must support those names for // compatibility with it until it can be updated. Anonymous unions are only // standard in C11, so disable this workaround in C++. union { @@ -84,21 +62,53 @@ struct sha_state_st { unsigned num; } /* SHA_CTX */; -// CRYPTO_fips_186_2_prf derives |out_len| bytes from |xkey| using the PRF +// SHA1_Init initialises `sha` and returns one. +OPENSSL_EXPORT int SHA1_Init(SHA_CTX* sha); + +// SHA1_Update adds `len` bytes from `data` to `sha` and returns one. +OPENSSL_EXPORT int SHA1_Update(SHA_CTX* sha, const void* data, size_t len); + +// SHA1_Final adds the final padding to `sha` and writes the resulting digest to +// `out`, which must have at least `SHA_DIGEST_LENGTH` bytes of space. It +// returns one. +OPENSSL_EXPORT int SHA1_Final(uint8_t out[SHA_DIGEST_LENGTH], SHA_CTX* sha); + +// SHA1 writes the digest of `len` bytes from `data` to `out` and returns +// `out`. There must be at least `SHA_DIGEST_LENGTH` bytes of space in +// `out`. +// +// `static inline` to not generate such short-named linker symbols or macros. +static inline OPENSSL_UNUSED uint8_t* SHA1(const uint8_t* data, size_t len, + uint8_t out[SHA_DIGEST_LENGTH]) { + SHA_CTX ctx; + SHA1_Init(&ctx); + SHA1_Update(&ctx, data, len); + SHA1_Final(out, &ctx); + OPENSSL_cleanse(&ctx, sizeof(ctx)); + return out; +} + +// SHA1_Transform is a low-level function that performs a single, SHA-1 block +// transformation using the state from `sha` and `SHA_CBLOCK` bytes from +// `block`. +OPENSSL_EXPORT void SHA1_Transform(SHA_CTX* sha, + const uint8_t block[SHA_CBLOCK]); + +// CRYPTO_fips_186_2_prf derives `out_len` bytes from `xkey` using the PRF // defined in FIPS 186-2, Appendix 3.1, with change notice 1 applied. The b // parameter is 160 and seed, XKEY, is also 160 bits. The optional XSEED user // input is all zeros. // // The PRF generates a sequence of 320-bit numbers. Each number is encoded as a -// 40-byte string in big-endian and then concatenated to form |out|. If -// |out_len| is not a multiple of 40, the result is truncated. This matches the +// 40-byte string in big-endian and then concatenated to form `out`. If +// `out_len` is not a multiple of 40, the result is truncated. This matches the // construction used in Section 7 of RFC 4186 and Section 7 of RFC 4187. // // This PRF is based on SHA-1, a weak hash function, and should not be used // in new protocols. It is provided for compatibility with some legacy EAP // methods. OPENSSL_EXPORT void CRYPTO_fips_186_2_prf( - uint8_t *out, size_t out_len, const uint8_t xkey[SHA_DIGEST_LENGTH]); + uint8_t* out, size_t out_len, const uint8_t xkey[SHA_DIGEST_LENGTH]); #if defined(__cplusplus) diff --git a/third_party/boringssl/src/include/openssl/sha2.h b/third_party/boringssl/src/include/openssl/sha2.h index 7bb3f21a2..7ae94c9b5 100644 --- a/third_party/boringssl/src/include/openssl/sha2.h +++ b/third_party/boringssl/src/include/openssl/sha2.h @@ -16,6 +16,7 @@ #define OPENSSL_HEADER_SHA2_H #include // IWYU pragma: export +#include #if defined(__cplusplus) extern "C" { @@ -25,7 +26,7 @@ extern "C" { // The SHA-2 family of hash functions. -// SHA-224. +// SHA-256 based hashes. // SHA224_CBLOCK is the block size of SHA-224. #define SHA224_CBLOCK 64 @@ -33,74 +34,97 @@ extern "C" { // SHA224_DIGEST_LENGTH is the length of a SHA-224 digest. #define SHA224_DIGEST_LENGTH 28 -// SHA224_Init initialises |sha| and returns 1. +// SHA256_CBLOCK is the block size of SHA-256. +#define SHA256_CBLOCK 64 + +// SHA256_DIGEST_LENGTH is the length of a SHA-256 digest. +#define SHA256_DIGEST_LENGTH 32 + +struct sha256_state_st { + uint32_t h[8]; + uint32_t Nl, Nh; + uint8_t data[SHA256_CBLOCK]; + unsigned num, md_len; +} /* SHA256_CTX */; + +// SHA224_Init initialises `sha` for SHA-224 and returns 1. OPENSSL_EXPORT int SHA224_Init(SHA256_CTX *sha); -// SHA224_Update adds |len| bytes from |data| to |sha| and returns 1. +// SHA224_Update adds `len` bytes from `data` to `sha` and returns 1. +// +// `sha` must have been initialised for SHA-224. OPENSSL_EXPORT int SHA224_Update(SHA256_CTX *sha, const void *data, size_t len); -// SHA224_Final adds the final padding to |sha| and writes the resulting digest -// to |out|, which must have at least |SHA224_DIGEST_LENGTH| bytes of space. It +// SHA224_Final adds the final padding to `sha` and writes the resulting digest +// to `out`, which must have at least `SHA224_DIGEST_LENGTH` bytes of space. It // returns 1. +// +// `sha` must have been initialised for SHA-224. OPENSSL_EXPORT int SHA224_Final(uint8_t out[SHA224_DIGEST_LENGTH], SHA256_CTX *sha); -// SHA224 writes the digest of |len| bytes from |data| to |out| and returns -// |out|. There must be at least |SHA224_DIGEST_LENGTH| bytes of space in -// |out|. -OPENSSL_EXPORT uint8_t *SHA224(const uint8_t *data, size_t len, - uint8_t out[SHA224_DIGEST_LENGTH]); - - -// SHA-256. - -// SHA256_CBLOCK is the block size of SHA-256. -#define SHA256_CBLOCK 64 - -// SHA256_DIGEST_LENGTH is the length of a SHA-256 digest. -#define SHA256_DIGEST_LENGTH 32 - -// SHA256_Init initialises |sha| and returns 1. +// SHA224 writes the digest of `len` bytes from `data` to `out` and returns +// `out`. There must be at least `SHA224_DIGEST_LENGTH` bytes of space in +// `out`. +// +// `static inline` to not generate such short-named linker symbols or macros. +static inline OPENSSL_UNUSED uint8_t* SHA224( + const uint8_t* data, size_t len, uint8_t out[SHA224_DIGEST_LENGTH]) { + SHA256_CTX ctx; + SHA224_Init(&ctx); + SHA224_Update(&ctx, data, len); + SHA224_Final(out, &ctx); + OPENSSL_cleanse(&ctx, sizeof(ctx)); + return out; +} + +// SHA256_Init initialises `sha` for SHA-256 and returns 1. OPENSSL_EXPORT int SHA256_Init(SHA256_CTX *sha); -// SHA256_Update adds |len| bytes from |data| to |sha| and returns 1. +// SHA256_Update adds `len` bytes from `data` to `sha` and returns 1. +// +// `sha` must have been initialised for SHA-256. OPENSSL_EXPORT int SHA256_Update(SHA256_CTX *sha, const void *data, size_t len); -// SHA256_Final adds the final padding to |sha| and writes the resulting digest -// to |out|, which must have at least |SHA256_DIGEST_LENGTH| bytes of space. It +// SHA256_Final adds the final padding to `sha` and writes the resulting digest +// to `out`, which must have at least `SHA256_DIGEST_LENGTH` bytes of space. It // returns one on success and zero on programmer error. +// +// `sha` must have been initialised for SHA-256. OPENSSL_EXPORT int SHA256_Final(uint8_t out[SHA256_DIGEST_LENGTH], SHA256_CTX *sha); -// SHA256 writes the digest of |len| bytes from |data| to |out| and returns -// |out|. There must be at least |SHA256_DIGEST_LENGTH| bytes of space in -// |out|. -OPENSSL_EXPORT uint8_t *SHA256(const uint8_t *data, size_t len, - uint8_t out[SHA256_DIGEST_LENGTH]); +// SHA256 writes the digest of `len` bytes from `data` to `out` and returns +// `out`. There must be at least `SHA256_DIGEST_LENGTH` bytes of space in +// `out`. +// +// `static inline` to not generate such short-named linker symbols or macros. +static inline OPENSSL_UNUSED uint8_t* SHA256( + const uint8_t* data, size_t len, uint8_t out[SHA256_DIGEST_LENGTH]) { + SHA256_CTX ctx; + SHA256_Init(&ctx); + SHA256_Update(&ctx, data, len); + SHA256_Final(out, &ctx); + OPENSSL_cleanse(&ctx, sizeof(ctx)); + return out; +} // SHA256_Transform is a low-level function that performs a single, SHA-256 -// block transformation using the state from |sha| and |SHA256_CBLOCK| bytes -// from |block|. -OPENSSL_EXPORT void SHA256_Transform(SHA256_CTX *sha, +// block transformation using the state from `sha` and `SHA256_CBLOCK` bytes +// from `block`. +OPENSSL_EXPORT void SHA256_Transform(SHA256_CTX* sha, const uint8_t block[SHA256_CBLOCK]); -// SHA256_TransformBlocks is a low-level function that takes |num_blocks| * -// |SHA256_CBLOCK| bytes of data and performs SHA-256 transforms on it to update -// |state|. You should not use this function unless you are implementing a +// SHA256_TransformBlocks is a low-level function that takes `num_blocks` * +// `SHA256_CBLOCK` bytes of data and performs SHA-256 transforms on it to update +// `state`. You should not use this function unless you are implementing a // derivative of SHA-256. OPENSSL_EXPORT void SHA256_TransformBlocks(uint32_t state[8], const uint8_t *data, size_t num_blocks); -struct sha256_state_st { - uint32_t h[8]; - uint32_t Nl, Nh; - uint8_t data[SHA256_CBLOCK]; - unsigned num, md_len; -} /* SHA256_CTX */; - -// SHA-384. +// SHA-512 based hashes. // SHA384_CBLOCK is the block size of SHA-384. #define SHA384_CBLOCK 128 @@ -108,90 +132,124 @@ struct sha256_state_st { // SHA384_DIGEST_LENGTH is the length of a SHA-384 digest. #define SHA384_DIGEST_LENGTH 48 -// SHA384_Init initialises |sha| and returns 1. +// SHA512_CBLOCK is the block size of SHA-512. +#define SHA512_CBLOCK 128 + +// SHA512_DIGEST_LENGTH is the length of a SHA-512 digest. +#define SHA512_DIGEST_LENGTH 64 + +// SHA512_256_DIGEST_LENGTH is the length of a SHA-512/256 digest. +// +// See https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf section 5.3.6 +#define SHA512_256_DIGEST_LENGTH 32 + +struct sha512_state_st { + uint64_t h[8]; + uint16_t num, md_len; + uint32_t bytes_so_far_high; + uint64_t bytes_so_far_low; + uint8_t p[SHA512_CBLOCK]; +} /* SHA512_CTX */; + +// SHA384_Init initialises `sha` for SHA-384 and returns 1. OPENSSL_EXPORT int SHA384_Init(SHA512_CTX *sha); -// SHA384_Update adds |len| bytes from |data| to |sha| and returns 1. +// SHA384_Update adds `len` bytes from `data` to `sha` and returns 1. +// +// `sha` must have been initialised for SHA-384. OPENSSL_EXPORT int SHA384_Update(SHA512_CTX *sha, const void *data, size_t len); -// SHA384_Final adds the final padding to |sha| and writes the resulting digest -// to |out|, which must have at least |SHA384_DIGEST_LENGTH| bytes of space. It +// SHA384_Final adds the final padding to `sha` and writes the resulting digest +// to `out`, which must have at least `SHA384_DIGEST_LENGTH` bytes of space. It // returns one on success and zero on programmer error. +// +// `sha` must have been initialised for SHA-384. OPENSSL_EXPORT int SHA384_Final(uint8_t out[SHA384_DIGEST_LENGTH], SHA512_CTX *sha); -// SHA384 writes the digest of |len| bytes from |data| to |out| and returns -// |out|. There must be at least |SHA384_DIGEST_LENGTH| bytes of space in -// |out|. -OPENSSL_EXPORT uint8_t *SHA384(const uint8_t *data, size_t len, - uint8_t out[SHA384_DIGEST_LENGTH]); - - -// SHA-512. - -// SHA512_CBLOCK is the block size of SHA-512. -#define SHA512_CBLOCK 128 - -// SHA512_DIGEST_LENGTH is the length of a SHA-512 digest. -#define SHA512_DIGEST_LENGTH 64 - -// SHA512_Init initialises |sha| and returns 1. +// SHA384 writes the digest of `len` bytes from `data` to `out` and returns +// `out`. There must be at least `SHA384_DIGEST_LENGTH` bytes of space in +// `out`. +// +// `static inline` to not generate such short-named linker symbols or macros. +static inline OPENSSL_UNUSED uint8_t* SHA384( + const uint8_t* data, size_t len, uint8_t out[SHA384_DIGEST_LENGTH]) { + SHA512_CTX ctx; + SHA384_Init(&ctx); + SHA384_Update(&ctx, data, len); + SHA384_Final(out, &ctx); + OPENSSL_cleanse(&ctx, sizeof(ctx)); + return out; +} + +// SHA512_Init initialises `sha` and returns 1. OPENSSL_EXPORT int SHA512_Init(SHA512_CTX *sha); -// SHA512_Update adds |len| bytes from |data| to |sha| and returns 1. +// SHA512_Update adds `len` bytes from `data` to `sha` and returns 1. +// +// `sha` must have been initialised for SHA-512. OPENSSL_EXPORT int SHA512_Update(SHA512_CTX *sha, const void *data, size_t len); -// SHA512_Final adds the final padding to |sha| and writes the resulting digest -// to |out|, which must have at least |SHA512_DIGEST_LENGTH| bytes of space. It +// SHA512_Final adds the final padding to `sha` and writes the resulting digest +// to `out`, which must have at least `SHA512_DIGEST_LENGTH` bytes of space. It // returns one on success and zero on programmer error. +// +// `sha` must have been initialised for SHA-512. OPENSSL_EXPORT int SHA512_Final(uint8_t out[SHA512_DIGEST_LENGTH], SHA512_CTX *sha); -// SHA512 writes the digest of |len| bytes from |data| to |out| and returns -// |out|. There must be at least |SHA512_DIGEST_LENGTH| bytes of space in -// |out|. -OPENSSL_EXPORT uint8_t *SHA512(const uint8_t *data, size_t len, - uint8_t out[SHA512_DIGEST_LENGTH]); +// SHA512 writes the digest of `len` bytes from `data` to `out` and returns +// `out`. There must be at least `SHA512_DIGEST_LENGTH` bytes of space in +// `out`. +// +// `static inline` to not generate such short-named linker symbols or macros. +static inline OPENSSL_UNUSED uint8_t* SHA512( + const uint8_t* data, size_t len, uint8_t out[SHA512_DIGEST_LENGTH]) { + SHA512_CTX ctx; + SHA512_Init(&ctx); + SHA512_Update(&ctx, data, len); + SHA512_Final(out, &ctx); + OPENSSL_cleanse(&ctx, sizeof(ctx)); + return out; +} // SHA512_Transform is a low-level function that performs a single, SHA-512 -// block transformation using the state from |sha| and |SHA512_CBLOCK| bytes -// from |block|. +// block transformation using the state from `sha` and `SHA512_CBLOCK` bytes +// from `block`. OPENSSL_EXPORT void SHA512_Transform(SHA512_CTX *sha, const uint8_t block[SHA512_CBLOCK]); -struct sha512_state_st { - uint64_t h[8]; - uint16_t num, md_len; - uint32_t bytes_so_far_high; - uint64_t bytes_so_far_low; - uint8_t p[SHA512_CBLOCK]; -} /* SHA512_CTX */; - - -// SHA-512-256 -// -// See https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf section 5.3.6 - -#define SHA512_256_DIGEST_LENGTH 32 - -// SHA512_256_Init initialises |sha| and returns 1. +// SHA512_256_Init initialises `sha` for SHA-512/256 and returns 1. OPENSSL_EXPORT int SHA512_256_Init(SHA512_CTX *sha); -// SHA512_256_Update adds |len| bytes from |data| to |sha| and returns 1. +// SHA512_256_Update adds `len` bytes from `data` to `sha` and returns 1. +// +// `sha` must have been initialised for SHA-512/256. OPENSSL_EXPORT int SHA512_256_Update(SHA512_CTX *sha, const void *data, size_t len); -// SHA512_256_Final adds the final padding to |sha| and writes the resulting -// digest to |out|, which must have at least |SHA512_256_DIGEST_LENGTH| bytes of +// SHA512_256_Final adds the final padding to `sha` and writes the resulting +// digest to `out`, which must have at least `SHA512_256_DIGEST_LENGTH` bytes of // space. It returns one on success and zero on programmer error. +// +// `sha` must have been initialised for SHA-512/256. OPENSSL_EXPORT int SHA512_256_Final(uint8_t out[SHA512_256_DIGEST_LENGTH], SHA512_CTX *sha); -// SHA512_256 writes the digest of |len| bytes from |data| to |out| and returns -// |out|. There must be at least |SHA512_256_DIGEST_LENGTH| bytes of space in -// |out|. -OPENSSL_EXPORT uint8_t *SHA512_256(const uint8_t *data, size_t len, - uint8_t out[SHA512_256_DIGEST_LENGTH]); +// SHA512_256 writes the digest of `len` bytes from `data` to `out` and returns +// `out`. There must be at least `SHA512_256_DIGEST_LENGTH` bytes of space in +// `out`. +// +// `static inline` to not generate such short-named linker symbols or macros. +static inline OPENSSL_UNUSED uint8_t* SHA512_256( + const uint8_t* data, size_t len, uint8_t out[SHA512_256_DIGEST_LENGTH]) { + SHA512_CTX ctx; + SHA512_256_Init(&ctx); + SHA512_256_Update(&ctx, data, len); + SHA512_256_Final(out, &ctx); + OPENSSL_cleanse(&ctx, sizeof(ctx)); + return out; +} #if defined(__cplusplus) diff --git a/third_party/boringssl/src/include/openssl/slhdsa.h b/third_party/boringssl/src/include/openssl/slhdsa.h index c9cda0c9c..996158eca 100644 --- a/third_party/boringssl/src/include/openssl/slhdsa.h +++ b/third_party/boringssl/src/include/openssl/slhdsa.h @@ -15,7 +15,7 @@ #ifndef OPENSSL_HEADER_SLHDSA_H #define OPENSSL_HEADER_SLHDSA_H -#include // IWYU pragma: export +#include // IWYU pragma: export #if defined(__cplusplus) extern "C" { @@ -25,6 +25,10 @@ extern "C" { // SLH-DSA. +// SLHDSA_SHA2_128S_SEED_BYTES is the number of bytes in an SLH-DSA-SHA2-128s +// private seed. +#define SLHDSA_SHA2_128S_SEED_BYTES 48 + // SLHDSA_SHA2_128S_PUBLIC_KEY_BYTES is the number of bytes in an // SLH-DSA-SHA2-128s public key. #define SLHDSA_SHA2_128S_PUBLIC_KEY_BYTES 32 @@ -37,6 +41,10 @@ extern "C" { // SLH-DSA-SHA2-128s signature. #define SLHDSA_SHA2_128S_SIGNATURE_BYTES 7856 +// SLHDSA_SHAKE_256F_SEED_BYTES is the number of bytes in an SLH-DSA-SHAKE-256f +// private seed. +#define SLHDSA_SHAKE_256F_SEED_BYTES 96 + // SLHDSA_SHAKE_256F_PUBLIC_KEY_BYTES is the number of bytes in an // SLH-DSA-SHAKE-256f public key. #define SLHDSA_SHAKE_256F_PUBLIC_KEY_BYTES 64 @@ -49,38 +57,54 @@ extern "C" { // SLH-DSA-SHAKE-256f signature. #define SLHDSA_SHAKE_256F_SIGNATURE_BYTES 49856 +// SLHDSA_SHA2_128S_generate_key_from_seed generates a SLH-DSA-SHA2-128s key +// pair from the given `seed` and writes the result to `out_public_key` and +// `out_private_key`. +OPENSSL_EXPORT void SLHDSA_SHA2_128S_generate_key_from_seed( + uint8_t out_public_key[SLHDSA_SHA2_128S_PUBLIC_KEY_BYTES], + uint8_t out_private_key[SLHDSA_SHA2_128S_PRIVATE_KEY_BYTES], + const uint8_t seed[SLHDSA_SHA2_128S_SEED_BYTES]); + +// SLHDSA_SHAKE_256F_generate_key_from_seed generates a SLH-DSA-SHA2-128s key +// pair from the given `seed` and writes the result to `out_public_key` and +// `out_private_key`. +OPENSSL_EXPORT void SLHDSA_SHAKE_256F_generate_key_from_seed( + uint8_t out_public_key[SLHDSA_SHAKE_256F_PUBLIC_KEY_BYTES], + uint8_t out_private_key[SLHDSA_SHAKE_256F_PRIVATE_KEY_BYTES], + const uint8_t seed[SLHDSA_SHAKE_256F_SEED_BYTES]); + // SLHDSA_SHA2_128S_generate_key generates a SLH-DSA-SHA2-128s key pair and -// writes the result to |out_public_key| and |out_private_key|. +// writes the result to `out_public_key` and `out_private_key`. OPENSSL_EXPORT void SLHDSA_SHA2_128S_generate_key( uint8_t out_public_key[SLHDSA_SHA2_128S_PUBLIC_KEY_BYTES], uint8_t out_private_key[SLHDSA_SHA2_128S_PRIVATE_KEY_BYTES]); // SLHDSA_SHAKE_256F_generate_key generates a SLH-DSA-SHAKE-256f key pair and -// writes the result to |out_public_key| and |out_private_key|. +// writes the result to `out_public_key` and `out_private_key`. OPENSSL_EXPORT void SLHDSA_SHAKE_256F_generate_key( uint8_t out_public_key[SLHDSA_SHAKE_256F_PUBLIC_KEY_BYTES], uint8_t out_private_key[SLHDSA_SHAKE_256F_PRIVATE_KEY_BYTES]); // SLHDSA_SHA2_128S_public_from_private writes the public key corresponding to -// |private_key| to |out_public_key|. +// `private_key` to `out_public_key`. OPENSSL_EXPORT void SLHDSA_SHA2_128S_public_from_private( uint8_t out_public_key[SLHDSA_SHA2_128S_PUBLIC_KEY_BYTES], const uint8_t private_key[SLHDSA_SHA2_128S_PRIVATE_KEY_BYTES]); // SLHDSA_SHAKE_256F_public_from_private writes the public key corresponding to -// |private_key| to |out_public_key|. +// `private_key` to `out_public_key`. OPENSSL_EXPORT void SLHDSA_SHAKE_256F_public_from_private( uint8_t out_public_key[SLHDSA_SHAKE_256F_PUBLIC_KEY_BYTES], const uint8_t private_key[SLHDSA_SHAKE_256F_PRIVATE_KEY_BYTES]); -// SLHDSA_SHA2_128S_sign slowly generates a SLH-DSA-SHA2-128s signature of |msg| -// using |private_key| and writes it to |out_signature|. The |context| argument +// SLHDSA_SHA2_128S_sign slowly generates a SLH-DSA-SHA2-128s signature of `msg` +// using `private_key` and writes it to `out_signature`. The `context` argument // is also signed over and can be used to include implicit contextual -// information that isn't included in |msg|. The same value of |context| must be -// presented to |SLHDSA_SHA2_128S_verify| in order for the generated signature -// to be considered valid. |context| and |context_len| may be |NULL| and 0 to +// information that isn't included in `msg`. The same value of `context` must be +// presented to `SLHDSA_SHA2_128S_verify` in order for the generated signature +// to be considered valid. `context` and `context_len` may be `NULL` and 0 to // use an empty context (this is common). It returns 1 on success and 0 if -// |context_len| is larger than 255. +// `context_len` is larger than 255. OPENSSL_EXPORT int SLHDSA_SHA2_128S_sign( uint8_t out_signature[SLHDSA_SHA2_128S_SIGNATURE_BYTES], const uint8_t private_key[SLHDSA_SHA2_128S_PRIVATE_KEY_BYTES], @@ -88,22 +112,22 @@ OPENSSL_EXPORT int SLHDSA_SHA2_128S_sign( size_t context_len); // SLHDSA_SHAKE_256F_sign slowly generates a SLH-DSA-SHAKE-256f signature of -// |msg| using |private_key| and writes it to |out_signature|. The |context| +// `msg` using `private_key` and writes it to `out_signature`. The `context` // argument is also signed over and can be used to include implicit contextual -// information that isn't included in |msg|. The same value of |context| must be -// presented to |SLHDSA_SHAKE_256F_verify| in order for the generated signature -// to be considered valid. |context| and |context_len| may be |NULL| and 0 to +// information that isn't included in `msg`. The same value of `context` must be +// presented to `SLHDSA_SHAKE_256F_verify` in order for the generated signature +// to be considered valid. `context` and `context_len` may be `NULL` and 0 to // use an empty context (this is common). It returns 1 on success and 0 if -// |context_len| is larger than 255. +// `context_len` is larger than 255. OPENSSL_EXPORT int SLHDSA_SHAKE_256F_sign( uint8_t out_signature[SLHDSA_SHAKE_256F_SIGNATURE_BYTES], const uint8_t private_key[SLHDSA_SHAKE_256F_PRIVATE_KEY_BYTES], const uint8_t *msg, size_t msg_len, const uint8_t *context, size_t context_len); -// SLHDSA_SHA2_128S_verify verifies that |signature| is a valid -// SLH-DSA-SHA2-128s signature of |msg| by |public_key|. The value of |context| -// must equal the value that was passed to |SLHDSA_SHA2_128S_sign| when the +// SLHDSA_SHA2_128S_verify verifies that `signature` is a valid +// SLH-DSA-SHA2-128s signature of `msg` by `public_key`. The value of `context` +// must equal the value that was passed to `SLHDSA_SHA2_128S_sign` when the // signature was generated. It returns 1 if the signature is valid and 0 // otherwise. OPENSSL_EXPORT int SLHDSA_SHA2_128S_verify( @@ -112,9 +136,9 @@ OPENSSL_EXPORT int SLHDSA_SHA2_128S_verify( const uint8_t *msg, size_t msg_len, const uint8_t *context, size_t context_len); -// SLHDSA_SHAKE_256F_verify verifies that |signature| is a valid -// SLH-DSA-SHAKE-256f signature of |msg| by |public_key|. The value of |context| -// must equal the value that was passed to |SLHDSA_SHAKE_256F_sign| when the +// SLHDSA_SHAKE_256F_verify verifies that `signature` is a valid +// SLH-DSA-SHAKE-256f signature of `msg` by `public_key`. The value of `context` +// must equal the value that was passed to `SLHDSA_SHAKE_256F_sign` when the // signature was generated. It returns 1 if the signature is valid and 0 // otherwise. OPENSSL_EXPORT int SLHDSA_SHAKE_256F_verify( @@ -137,20 +161,20 @@ OPENSSL_EXPORT int SLHDSA_SHAKE_256F_verify( // and there's no other way to prevent ambiguity. // SLHDSA_SHA2_128S_prehash_sign slowly generates a SLH-DSA-SHA2-128s signature -// of the prehashed |hashed_msg| using |private_key| and writes it to -// |out_signature|. The |context| argument is also signed over and can be used +// of the prehashed `hashed_msg` using `private_key` and writes it to +// `out_signature`. The `context` argument is also signed over and can be used // to include implicit contextual information that isn't included in -// |hashed_msg|. The same value of |context| must be presented to -// |SLHDSA_SHA2_128S_prehash_verify| in order for the generated signature to be -// considered valid. |context| and |context_len| may be |NULL| and 0 to use an +// `hashed_msg`. The same value of `context` must be presented to +// `SLHDSA_SHA2_128S_prehash_verify` in order for the generated signature to be +// considered valid. `context` and `context_len` may be `NULL` and 0 to use an // empty context (this is common). // -// The |hash_nid| argument must specify the hash function that was used to -// generate |hashed_msg|. This function only accepts hash functions listed in +// The `hash_nid` argument must specify the hash function that was used to +// generate `hashed_msg`. This function only accepts hash functions listed in // FIPS 205. // -// This function returns 1 on success and 0 if |context_len| is larger than 255, -// if the hash function is not supported, or if |hashed_msg| is the wrong +// This function returns 1 on success and 0 if `context_len` is larger than 255, +// if the hash function is not supported, or if `hashed_msg` is the wrong // length. OPENSSL_EXPORT int SLHDSA_SHA2_128S_prehash_sign( uint8_t out_signature[SLHDSA_SHA2_128S_SIGNATURE_BYTES], @@ -158,18 +182,18 @@ OPENSSL_EXPORT int SLHDSA_SHA2_128S_prehash_sign( const uint8_t *hashed_msg, size_t hashed_msg_len, int hash_nid, const uint8_t *context, size_t context_len); -// SLHDSA_SHA2_128S_prehash_verify verifies that |signature| is a valid -// SLH-DSA-SHA2-128s signature of the prehashed |hashed_msg| by |public_key|, -// using the hash algorithm identified by |hash_nid|. The value of |context| -// must equal the value that was passed to |SLHDSA_SHA2_128S_prehash_sign| when +// SLHDSA_SHA2_128S_prehash_verify verifies that `signature` is a valid +// SLH-DSA-SHA2-128s signature of the prehashed `hashed_msg` by `public_key`, +// using the hash algorithm identified by `hash_nid`. The value of `context` +// must equal the value that was passed to `SLHDSA_SHA2_128S_prehash_sign` when // the signature was generated. // -// The |hash_nid| argument must specify the hash function that was used to -// generate |hashed_msg|. This function only accepts hash functions that are +// The `hash_nid` argument must specify the hash function that was used to +// generate `hashed_msg`. This function only accepts hash functions that are // listed in FIPS 205. // // This function returns 1 if the signature is valid and 0 if the signature is -// invalid, the hash function is not supported, or if |hashed_msg| is the wrong +// invalid, the hash function is not supported, or if `hashed_msg` is the wrong // length. OPENSSL_EXPORT int SLHDSA_SHA2_128S_prehash_verify( const uint8_t *signature, size_t signature_len, @@ -178,20 +202,20 @@ OPENSSL_EXPORT int SLHDSA_SHA2_128S_prehash_verify( const uint8_t *context, size_t context_len); // SLHDSA_SHA2_128S_prehash_warning_nonstandard_sign slowly generates a -// SLH-DSA-SHA2-128s signature of the prehashed |hashed_msg| using |private_key| -// and writes it to |out_signature|. The |context| argument is also signed over +// SLH-DSA-SHA2-128s signature of the prehashed `hashed_msg` using `private_key` +// and writes it to `out_signature`. The `context` argument is also signed over // and can be used to include implicit contextual information that isn't -// included in |hashed_msg|. The same value of |context| must be presented to -// |SLHDSA_SHA2_128S_prehash_warning_nonstandard_verify| in order for the -// generated signature to be considered valid. |context| and |context_len| may -// be |NULL| and 0 to use an empty context (this is common). +// included in `hashed_msg`. The same value of `context` must be presented to +// `SLHDSA_SHA2_128S_prehash_warning_nonstandard_verify` in order for the +// generated signature to be considered valid. `context` and `context_len` may +// be `NULL` and 0 to use an empty context (this is common). // -// The |hash_nid| argument must specify the hash function that was used to -// generate |hashed_msg|. This function only accepts non-standard hash functions +// The `hash_nid` argument must specify the hash function that was used to +// generate `hashed_msg`. This function only accepts non-standard hash functions // that are not compliant with FIPS 205. // -// This function returns 1 on success and 0 if |context_len| is larger than 255, -// if the hash function is not supported, or if |hashed_msg| is the wrong +// This function returns 1 on success and 0 if `context_len` is larger than 255, +// if the hash function is not supported, or if `hashed_msg` is the wrong // length. OPENSSL_EXPORT int SLHDSA_SHA2_128S_prehash_warning_nonstandard_sign( uint8_t out_signature[SLHDSA_SHA2_128S_SIGNATURE_BYTES], @@ -199,18 +223,18 @@ OPENSSL_EXPORT int SLHDSA_SHA2_128S_prehash_warning_nonstandard_sign( const uint8_t *hashed_msg, size_t hashed_msg_len, int hash_nid, const uint8_t *context, size_t context_len); -// SLHDSA_SHA2_128S_prehash_warning_nonstandard_verify verifies that |signature| -// is a valid SLH-DSA-SHA2-128s signature of the prehashed |hashed_msg| by -// |public_key|, using the hash algorithm identified by |hash_nid|. The value of -// |context| must equal the value that was passed to -// |SLHDSA_SHA2_128S_prehash_sign| when the signature was generated. +// SLHDSA_SHA2_128S_prehash_warning_nonstandard_verify verifies that `signature` +// is a valid SLH-DSA-SHA2-128s signature of the prehashed `hashed_msg` by +// `public_key`, using the hash algorithm identified by `hash_nid`. The value of +// `context` must equal the value that was passed to +// `SLHDSA_SHA2_128S_prehash_sign` when the signature was generated. // -// The |hash_nid| argument must specify the hash function that was used to -// generate |hashed_msg|. This function only accepts non-standard hash functions +// The `hash_nid` argument must specify the hash function that was used to +// generate `hashed_msg`. This function only accepts non-standard hash functions // that are not compliant with FIPS 205. // // This function returns 1 if the signature is valid and 0 if the signature is -// invalid, the hash function is not supported, or if |hashed_msg| is the wrong +// invalid, the hash function is not supported, or if `hashed_msg` is the wrong // length. OPENSSL_EXPORT int SLHDSA_SHA2_128S_prehash_warning_nonstandard_verify( const uint8_t *signature, size_t signature_len, diff --git a/third_party/boringssl/src/include/openssl/span.h b/third_party/boringssl/src/include/openssl/span.h index eb05f1353..72447af41 100644 --- a/third_party/boringssl/src/include/openssl/span.h +++ b/third_party/boringssl/src/include/openssl/span.h @@ -15,7 +15,7 @@ #ifndef OPENSSL_HEADER_SSL_SPAN_H #define OPENSSL_HEADER_SSL_SPAN_H -#include // IWYU pragma: export +#include // IWYU pragma: export #if !defined(BORINGSSL_NO_CXX) @@ -113,12 +113,12 @@ struct AllowRedeclaringConstructor {}; } // namespace internal // A Span is a non-owning reference to a contiguous array of objects of type -// |T|. Conceptually, a Span is a simple a pointer to |T| and a count of +// `T`. Conceptually, a Span is a simple a pointer to `T` and a count of // elements accessible via that pointer. The elements referenced by the Span can -// be mutated if |T| is mutable. +// be mutated if `T` is mutable. // -// A Span can be constructed from container types implementing |data()| and -// |size()| methods. If |T| is constant, construction from a container type is +// A Span can be constructed from container types implementing `data()` and +// `size()` methods. If `T` is constant, construction from a container type is // implicit. This allows writing methods that accept data from some unspecified // container type: // @@ -255,7 +255,7 @@ class Span : public internal::SpanStorage { public: // NOTE: This method may abort() at runtime if pos or len are out of range. - // NOTE: As opposed to std::span, the |dynamic_extent| value of |len| is not + // NOTE: As opposed to std::span, the `dynamic_extent` value of `len` is not // magical here. This gets rid of a lot of runtime checks. constexpr Span subspan(size_t pos, size_t len) const { // absl::Span throws an exception here. Note std::span and Chromium diff --git a/third_party/boringssl/src/include/openssl/stack.h b/third_party/boringssl/src/include/openssl/stack.h index c88f6b375..129aae047 100644 --- a/third_party/boringssl/src/include/openssl/stack.h +++ b/third_party/boringssl/src/include/openssl/stack.h @@ -26,56 +26,56 @@ extern "C" { // used collection object. // // This file defines macros for type-safe use of the stack functions. A stack -// type is named like |STACK_OF(FOO)| and is accessed with functions named -// like |sk_FOO_*|. Note the stack will typically contain /pointers/ to |FOO|. +// type is named like `STACK_OF(FOO)` and is accessed with functions named +// like `sk_FOO_*`. Note the stack will typically contain /pointers/ to `FOO`. // -// The |DECLARE_STACK_OF| macro makes |STACK_OF(FOO)| available, and -// |DEFINE_STACK_OF| makes the corresponding functions available. +// The `DECLARE_STACK_OF` macro makes `STACK_OF(FOO)` available, and +// `DEFINE_STACK_OF` makes the corresponding functions available. // Defining stacks. -// STACK_OF expands to the stack type for |type|. +// STACK_OF expands to the stack type for `type`. #define STACK_OF(type) struct stack_st_##type -// DECLARE_STACK_OF declares the |STACK_OF(type)| type. It does not make the -// corresponding |sk_type_*| functions available. This macro should be used in +// DECLARE_STACK_OF declares the `STACK_OF(type)` type. It does not make the +// corresponding `sk_type_*` functions available. This macro should be used in // files which only need the type. #define DECLARE_STACK_OF(type) STACK_OF(type); -// DEFINE_NAMED_STACK_OF defines |STACK_OF(name)| to be a stack whose elements -// are |type| *. This macro makes the |sk_name_*| functions available. +// DEFINE_NAMED_STACK_OF defines `STACK_OF(name)` to be a stack whose elements +// are `type` *. This macro makes the `sk_name_*` functions available. // -// It is not necessary to use |DECLARE_STACK_OF| in files which use this macro. +// It is not necessary to use `DECLARE_STACK_OF` in files which use this macro. // // Must be used from the global namespace. #define DEFINE_NAMED_STACK_OF(name, type) \ BORINGSSL_DEFINE_STACK_OF_IMPL(name, type *, const type *) \ BORINGSSL_DEFINE_STACK_TRAITS(name, type, false) -// DEFINE_STACK_OF defines |STACK_OF(type)| to be a stack whose elements are -// |type| *. This macro makes the |sk_type_*| functions available. +// DEFINE_STACK_OF defines `STACK_OF(type)` to be a stack whose elements are +// `type` *. This macro makes the `sk_type_*` functions available. // -// It is not necessary to use |DECLARE_STACK_OF| in files which use this macro. +// It is not necessary to use `DECLARE_STACK_OF` in files which use this macro. // // Must be used from the global namespace. #define DEFINE_STACK_OF(type) DEFINE_NAMED_STACK_OF(type, type) -// DEFINE_CONST_STACK_OF defines |STACK_OF(type)| to be a stack whose elements -// are const |type| *. This macro makes the |sk_type_*| functions available. +// DEFINE_CONST_STACK_OF defines `STACK_OF(type)` to be a stack whose elements +// are const `type` *. This macro makes the `sk_type_*` functions available. // -// It is not necessary to use |DECLARE_STACK_OF| in files which use this macro. +// It is not necessary to use `DECLARE_STACK_OF` in files which use this macro. // // Must be used from the global namespace. #define DEFINE_CONST_STACK_OF(type) \ BORINGSSL_DEFINE_STACK_OF_IMPL(type, const type *, const type *) \ BORINGSSL_DEFINE_STACK_TRAITS(type, const type, true) -// DEFINE_NAMESPACED_STACK_OF is same as |DEFINE_STACK_OF| but to be used for +// DEFINE_NAMESPACED_STACK_OF is same as `DEFINE_STACK_OF` but to be used for // internal stacks from within the bssl namespace. // -// Such stacks then can only be accessed using |STACK_OF| if in the |bssl| -// namespace or if the |bssl| namespace has been imported with a +// Such stacks then can only be accessed using `STACK_OF` if in the `bssl` +// namespace or if the `bssl` namespace has been imported with a // using-directive. #define DEFINE_NAMESPACED_STACK_OF(type) \ BORINGSSL_DEFINE_STACK_OF_IMPL(type, type *, const type *) \ @@ -86,7 +86,7 @@ extern "C" { // Using stacks. // -// After the |DEFINE_STACK_OF| macro is used, the following functions are +// After the `DEFINE_STACK_OF` macro is used, the following functions are // available. #if 0 // Sample @@ -98,11 +98,11 @@ typedef void (*sk_SAMPLE_free_func)(SAMPLE *); // return the copy or NULL on error. typedef SAMPLE *(*sk_SAMPLE_copy_func)(const SAMPLE *); -// sk_SAMPLE_cmp_func is a callback to compare |*a| to |*b|. It should return a -// value < 0, 0, or > 0 if |*a| is less than, equal to, or greater than |*b|, +// sk_SAMPLE_cmp_func is a callback to compare `*a` to `*b`. It should return a +// value < 0, 0, or > 0 if `*a` is less than, equal to, or greater than `*b`, // respectively. Note the extra indirection - the function is given a pointer -// to a pointer to the element. This is the |qsort|/|bsearch| comparison -// function applied to an array of |SAMPLE*|. +// to a pointer to the element. This is the `qsort`/`bsearch` comparison +// function applied to an array of `SAMPLE*`. typedef int (*sk_SAMPLE_cmp_func)(const SAMPLE *const *a, const SAMPLE *const *b); @@ -114,113 +114,117 @@ STACK_OF(SAMPLE) *sk_SAMPLE_new(sk_SAMPLE_cmp_func comp); // NULL on allocation failure. STACK_OF(SAMPLE) *sk_SAMPLE_new_null(void); -// sk_SAMPLE_num returns the number of elements in |sk|. It is safe to cast this -// value to |int|. |sk| is guaranteed to have at most |INT_MAX| elements. If -// |sk| is NULL, it is treated as the empty list and this function returns zero. +// sk_SAMPLE_num returns the number of elements in `sk`. It is safe to cast this +// value to `int`. `sk` is guaranteed to have at most `INT_MAX` elements. If +// `sk` is NULL, it is treated as the empty list and this function returns zero. size_t sk_SAMPLE_num(const STACK_OF(SAMPLE) *sk); -// sk_SAMPLE_zero resets |sk| to the empty state but does nothing to free the +// sk_SAMPLE_zero resets `sk` to the empty state but does nothing to free the // individual elements themselves. void sk_SAMPLE_zero(STACK_OF(SAMPLE) *sk); -// sk_SAMPLE_value returns the |i|th pointer in |sk|, or NULL if |i| is out of -// range. If |sk| is NULL, it is treated as an empty list and the function +// sk_SAMPLE_value returns the `i`th pointer in `sk`, or NULL if `i` is out of +// range. If `sk` is NULL, it is treated as an empty list and the function // returns NULL. SAMPLE *sk_SAMPLE_value(const STACK_OF(SAMPLE) *sk, size_t i); -// sk_SAMPLE_set sets the |i|th pointer in |sk| to |p| and returns |p|. If |i| +// sk_SAMPLE_last returns the last pointer in `sk`, or NULL if `sk` is empty. If +// `sk` is NULL, it is treated as an empty list and the function returns NULL. +SAMPLE *sk_SAMPLE_last(const STACK_OF(SAMPLE) *sk); + +// sk_SAMPLE_set sets the `i`th pointer in `sk` to `p` and returns `p`. If `i` // is out of range, it returns NULL. SAMPLE *sk_SAMPLE_set(STACK_OF(SAMPLE) *sk, size_t i, SAMPLE *p); -// sk_SAMPLE_free frees |sk|, but does nothing to free the individual elements. -// Use |sk_SAMPLE_pop_free| to also free the elements. +// sk_SAMPLE_free frees `sk`, but does nothing to free the individual elements. +// Use `sk_SAMPLE_pop_free` to also free the elements. void sk_SAMPLE_free(STACK_OF(SAMPLE) *sk); -// sk_SAMPLE_pop_free calls |free_func| on each element in |sk| and then +// sk_SAMPLE_pop_free calls `free_func` on each element in `sk` and then // frees the stack itself. void sk_SAMPLE_pop_free(STACK_OF(SAMPLE) *sk, sk_SAMPLE_free_func free_func); -// sk_SAMPLE_insert inserts |p| into the stack at index |where|, moving existing +// sk_SAMPLE_insert inserts `p` into the stack at index `where`, moving existing // elements if needed. It returns the length of the new stack, or zero on // error. size_t sk_SAMPLE_insert(STACK_OF(SAMPLE) *sk, SAMPLE *p, size_t where); -// sk_SAMPLE_delete removes the pointer at index |where|, moving other elements -// down if needed. It returns the removed pointer, or NULL if |where| is out of +// sk_SAMPLE_delete removes the pointer at index `where`, moving other elements +// down if needed. It returns the removed pointer, or NULL if `where` is out of // range. SAMPLE *sk_SAMPLE_delete(STACK_OF(SAMPLE) *sk, size_t where); -// sk_SAMPLE_delete_ptr removes, at most, one instance of |p| from |sk| based on -// pointer equality. If an instance of |p| is found then |p| is returned, +// sk_SAMPLE_delete_ptr removes, at most, one instance of `p` from `sk` based on +// pointer equality. If an instance of `p` is found then `p` is returned, // otherwise it returns NULL. SAMPLE *sk_SAMPLE_delete_ptr(STACK_OF(SAMPLE) *sk, const SAMPLE *p); -// sk_SAMPLE_delete_if_func is the callback function for |sk_SAMPLE_delete_if|. -// It should return one to remove |p| and zero to keep it. +// sk_SAMPLE_delete_if_func is the callback function for `sk_SAMPLE_delete_if`. +// It should return one to remove `p` and zero to keep it. typedef int (*sk_SAMPLE_delete_if_func)(SAMPLE *p, void *data); -// sk_SAMPLE_delete_if calls |func| with each element of |sk| and removes the -// entries where |func| returned one. This function does not free or return -// removed pointers so, if |sk| owns its contents, |func| should release the +// sk_SAMPLE_delete_if calls `func` with each element of `sk` and removes the +// entries where `func` returned one. This function does not free or return +// removed pointers so, if `sk` owns its contents, `func` should release the // pointers prior to returning one. void sk_SAMPLE_delete_if(STACK_OF(SAMPLE) *sk, sk_SAMPLE_delete_if_func func, void *data); -// sk_SAMPLE_find find the first value in |sk| equal to |p|. |sk|'s comparison -// function determines equality, or pointer equality if |sk| has no comparison +// sk_SAMPLE_find find the first value in `sk` equal to `p`. `sk`'s comparison +// function determines equality, or pointer equality if `sk` has no comparison // function. // -// If the stack is sorted (see |sk_SAMPLE_sort|), this function uses a binary +// If the stack is sorted (see `sk_SAMPLE_sort`), this function uses a binary // search. Otherwise it performs a linear search. If it finds a matching -// element, it writes the index to |*out_index| (if |out_index| is not NULL) and -// returns one. Otherwise, it returns zero. If |sk| is NULL, it is treated as +// element, it writes the index to `*out_index` (if `out_index` is not NULL) and +// returns one. Otherwise, it returns zero. If `sk` is NULL, it is treated as // the empty list and the function returns zero. // // Note this differs from OpenSSL. The type signature is slightly different, and -// OpenSSL's version will implicitly sort |sk| if it has a comparison function +// OpenSSL's version will implicitly sort `sk` if it has a comparison function // defined. int sk_SAMPLE_find(const STACK_OF(SAMPLE) *sk, size_t *out_index, const SAMPLE *p); -// sk_SAMPLE_shift removes and returns the first element in |sk|, or NULL if -// |sk| is empty. +// sk_SAMPLE_shift removes and returns the first element in `sk`, or NULL if +// `sk` is empty. SAMPLE *sk_SAMPLE_shift(STACK_OF(SAMPLE) *sk); -// sk_SAMPLE_push appends |p| to |sk| and returns the length of the new stack, +// sk_SAMPLE_push appends `p` to `sk` and returns the length of the new stack, // or 0 on allocation failure. size_t sk_SAMPLE_push(STACK_OF(SAMPLE) *sk, SAMPLE *p); -// sk_SAMPLE_pop removes and returns the last element of |sk|, or NULL if |sk| +// sk_SAMPLE_pop removes and returns the last element of `sk`, or NULL if `sk` // is empty. SAMPLE *sk_SAMPLE_pop(STACK_OF(SAMPLE) *sk); // sk_SAMPLE_dup performs a shallow copy of a stack and returns the new stack, -// or NULL on error. Use |sk_SAMPLE_deep_copy| to also copy the elements. +// or NULL on error. Use `sk_SAMPLE_deep_copy` to also copy the elements. STACK_OF(SAMPLE) *sk_SAMPLE_dup(const STACK_OF(SAMPLE) *sk); -// sk_SAMPLE_sort sorts the elements of |sk| into ascending order based on the +// sk_SAMPLE_sort sorts the elements of `sk` into ascending order based on the // comparison function. The stack maintains a "sorted" flag and sorting an // already sorted stack is a no-op. void sk_SAMPLE_sort(STACK_OF(SAMPLE) *sk); -// sk_SAMPLE_sort_and_dedup sorts the elements of |sk| based on the comparison -// function and removes duplicates. If |free_func| is not NULL, it is called on +// sk_SAMPLE_sort_and_dedup sorts the elements of `sk` based on the comparison +// function and removes duplicates. If `free_func` is not NULL, it is called on // every removed element. void sk_SAMPLE_sort_and_dedup(STACK_OF(SAMPLE) *sk, sk_SAMPLE_free_func free_func); -// sk_SAMPLE_is_sorted returns one if |sk| is known to be sorted and zero +// sk_SAMPLE_is_sorted returns one if `sk` is known to be sorted and zero // otherwise. int sk_SAMPLE_is_sorted(const STACK_OF(SAMPLE) *sk); -// sk_SAMPLE_set_cmp_func sets the comparison function to be used by |sk| and +// sk_SAMPLE_set_cmp_func sets the comparison function to be used by `sk` and // returns the previous one. sk_SAMPLE_cmp_func sk_SAMPLE_set_cmp_func(STACK_OF(SAMPLE) *sk, sk_SAMPLE_cmp_func comp); -// sk_SAMPLE_deep_copy performs a copy of |sk| and of each of the non-NULL -// elements in |sk| by using |copy_func|. If an error occurs, it calls -// |free_func| to free any copies already made and returns NULL. +// sk_SAMPLE_deep_copy performs a copy of `sk` and of each of the non-NULL +// elements in `sk` by using `copy_func`. If an error occurs, it calls +// `free_func` to free any copies already made and returns NULL. STACK_OF(SAMPLE) *sk_SAMPLE_deep_copy(const STACK_OF(SAMPLE) *sk, sk_SAMPLE_copy_func copy_func, sk_SAMPLE_free_func free_func); @@ -230,34 +234,34 @@ STACK_OF(SAMPLE) *sk_SAMPLE_deep_copy(const STACK_OF(SAMPLE) *sk, // Private functions. // -// The |sk_*| functions generated above are implemented internally using the +// The `sk_*` functions generated above are implemented internally using the // type-erased functions below. Callers should use the typed wrappers instead. // When using the type-erased functions, callers are responsible for ensuring // the underlying types are correct. Casting pointers to the wrong types will // result in memory errors. // OPENSSL_sk_free_func is a function that frees an element in a stack. Note its -// actual type is void (*)(T *) for some T. Low-level |sk_*| functions will be +// actual type is void (*)(T *) for some T. Low-level `sk_*` functions will be // passed a type-specific wrapper to call it correctly. typedef void (*OPENSSL_sk_free_func)(void *ptr); // OPENSSL_sk_copy_func is a function that copies an element in a stack. Note -// its actual type is T *(*)(const T *) for some T. Low-level |sk_*| functions +// its actual type is T *(*)(const T *) for some T. Low-level `sk_*` functions // will be passed a type-specific wrapper to call it correctly. typedef void *(*OPENSSL_sk_copy_func)(const void *ptr); // OPENSSL_sk_cmp_func is a comparison function that returns a value < 0, 0 or > -// 0 if |*a| is less than, equal to or greater than |*b|, respectively. Note +// 0 if `*a` is less than, equal to or greater than `*b`, respectively. Note // the extra indirection - the function is given a pointer to a pointer to the // element. This differs from the usual qsort/bsearch comparison function. // -// Note its actual type is |int (*)(const T *const *a, const T *const *b)|. -// Low-level |sk_*| functions will be passed a type-specific wrapper to call it +// Note its actual type is `int (*)(const T *const *a, const T *const *b)`. +// Low-level `sk_*` functions will be passed a type-specific wrapper to call it // correctly. typedef int (*OPENSSL_sk_cmp_func)(const void *const *a, const void *const *b); // OPENSSL_sk_delete_if_func is the generic version of -// |sk_SAMPLE_delete_if_func|. +// `sk_SAMPLE_delete_if_func`. typedef int (*OPENSSL_sk_delete_if_func)(void *obj, void *data); // The following function types call the above type-erased signatures with the @@ -274,13 +278,14 @@ typedef int (*OPENSSL_sk_call_delete_if_func)(OPENSSL_sk_delete_if_func, void *, typedef struct stack_st OPENSSL_STACK; // The following are raw stack functions. They implement the corresponding typed -// |sk_SAMPLE_*| functions generated by |DEFINE_STACK_OF|. Callers shouldn't be +// `sk_SAMPLE_*` functions generated by `DEFINE_STACK_OF`. Callers shouldn't be // using them. Rather, callers should use the typed functions. OPENSSL_EXPORT OPENSSL_STACK *OPENSSL_sk_new(OPENSSL_sk_cmp_func comp); OPENSSL_EXPORT OPENSSL_STACK *OPENSSL_sk_new_null(void); OPENSSL_EXPORT size_t OPENSSL_sk_num(const OPENSSL_STACK *sk); OPENSSL_EXPORT void OPENSSL_sk_zero(OPENSSL_STACK *sk); OPENSSL_EXPORT void *OPENSSL_sk_value(const OPENSSL_STACK *sk, size_t i); +OPENSSL_EXPORT void *OPENSSL_sk_last(const OPENSSL_STACK *sk); OPENSSL_EXPORT void *OPENSSL_sk_set(OPENSSL_STACK *sk, size_t i, void *p); OPENSSL_EXPORT void OPENSSL_sk_free(OPENSSL_STACK *sk); OPENSSL_EXPORT void OPENSSL_sk_pop_free_ex( @@ -324,7 +329,7 @@ OPENSSL_EXPORT OPENSSL_STACK *OPENSSL_sk_deep_copy( typedef OPENSSL_STACK _STACK; -// The following functions call the corresponding |OPENSSL_sk_*| function. +// The following functions call the corresponding `OPENSSL_sk_*` function. OPENSSL_EXPORT OPENSSL_DEPRECATED OPENSSL_STACK *sk_new_null(void); OPENSSL_EXPORT OPENSSL_DEPRECATED size_t sk_num(const OPENSSL_STACK *sk); OPENSSL_EXPORT OPENSSL_DEPRECATED void *sk_value(const OPENSSL_STACK *sk, @@ -333,16 +338,16 @@ OPENSSL_EXPORT OPENSSL_DEPRECATED void sk_free(OPENSSL_STACK *sk); OPENSSL_EXPORT OPENSSL_DEPRECATED size_t sk_push(OPENSSL_STACK *sk, void *p); OPENSSL_EXPORT OPENSSL_DEPRECATED void *sk_pop(OPENSSL_STACK *sk); -// sk_pop_free_ex calls |OPENSSL_sk_pop_free_ex|. +// sk_pop_free_ex calls `OPENSSL_sk_pop_free_ex`. // // TODO(b/291994116): Remove this. OPENSSL_EXPORT OPENSSL_DEPRECATED void sk_pop_free_ex( OPENSSL_STACK *sk, OPENSSL_sk_call_free_func call_free_func, OPENSSL_sk_free_func free_func); -// sk_pop_free behaves like |OPENSSL_sk_pop_free_ex| but performs an invalid +// sk_pop_free behaves like `OPENSSL_sk_pop_free_ex` but performs an invalid // function pointer cast. It exists because some existing callers called -// |sk_pop_free| directly. +// `sk_pop_free` directly. // // TODO(davidben): Migrate callers to bssl::UniquePtr and remove this. OPENSSL_EXPORT OPENSSL_DEPRECATED void sk_pop_free( @@ -381,9 +386,9 @@ BSSL_NAMESPACE_END /* We disable MSVC C4191 in this macro, which warns when pointers are cast \ * to the wrong type. While the cast itself is valid, it is often a bug \ * because calling it through the cast is UB. However, we never actually \ - * call functions as |OPENSSL_sk_cmp_func|. The type is just a type-erased \ + * call functions as `OPENSSL_sk_cmp_func`. The type is just a type-erased \ * function pointer. (C does not guarantee function pointers fit in \ - * |void*|, and GCC will warn on this.) Thus we just disable the false \ + * `void*`, and GCC will warn on this.) Thus we just disable the false \ * positive warning. */ \ OPENSSL_MSVC_PRAGMA(warning(push)) \ OPENSSL_MSVC_PRAGMA(warning(disable : 4191)) \ @@ -419,7 +424,7 @@ BSSL_NAMESPACE_END const void *a, const void *b) { \ constptrtype a_ptr = (constptrtype)a; \ constptrtype b_ptr = (constptrtype)b; \ - /* |cmp_func| expects an extra layer of pointers to match qsort. */ \ + /* `cmp_func` expects an extra layer of pointers to match qsort. */ \ return ((sk_##name##_cmp_func)cmp_func)(&a_ptr, &b_ptr); \ } \ \ @@ -449,6 +454,10 @@ BSSL_NAMESPACE_END return (ptrtype)OPENSSL_sk_value((const OPENSSL_STACK *)sk, i); \ } \ \ + OPENSSL_INLINE ptrtype sk_##name##_last(const STACK_OF(name) *sk) { \ + return (ptrtype)OPENSSL_sk_last((const OPENSSL_STACK *)sk); \ + } \ + \ OPENSSL_INLINE ptrtype sk_##name##_set(STACK_OF(name) *sk, size_t i, \ ptrtype p) { \ return (ptrtype)OPENSSL_sk_set((OPENSSL_STACK *)sk, i, (void *)p); \ @@ -564,7 +573,7 @@ BSSL_NAMESPACE_BEGIN namespace internal { -// Stacks defined with |DEFINE_CONST_STACK_OF| are freed with |sk_free|. +// Stacks defined with `DEFINE_CONST_STACK_OF` are freed with `sk_free`. template struct DeleterImpl::kIsConst>> { static void Free(Stack *sk) { @@ -572,7 +581,7 @@ struct DeleterImpl::kIsConst>> { } }; -// Stacks defined with |DEFINE_STACK_OF| are freed with |sk_pop_free| and the +// Stacks defined with `DEFINE_STACK_OF` are freed with `sk_pop_free` and the // corresponding type's deleter. template struct DeleterImpl::kIsConst>> { @@ -631,7 +640,7 @@ using StackIterator = } // namespace internal -// PushToStack pushes |elem| to |sk|. It returns true on success and false on +// PushToStack pushes `elem` to `sk`. It returns true on success and false on // allocation failure. template inline std::enable_if_t::kIsConst, bool> diff --git a/third_party/boringssl/src/include/openssl/target.h b/third_party/boringssl/src/include/openssl/target.h index 2c03389c4..2372a055d 100644 --- a/third_party/boringssl/src/include/openssl/target.h +++ b/third_party/boringssl/src/include/openssl/target.h @@ -105,6 +105,11 @@ #define OPENSSL_FUCHSIA #endif +// See go/boringssl-on-pythia. +#if defined(__pythia__) +#define OPENSSL_PYTHIA +#endif + // Trusty is Android's TEE target. See // https://source.android.com/docs/security/features/trusty // @@ -165,6 +170,11 @@ #define OPENSSL_NO_SOCK #endif +#if defined(OPENSSL_PYTHIA) +#define OPENSSL_NO_POSIX_IO +#define OPENSSL_NO_SOCK +#endif + #if defined(__ANDROID_API__) #define OPENSSL_ANDROID #endif diff --git a/third_party/boringssl/src/include/openssl/tls_prf.h b/third_party/boringssl/src/include/openssl/tls_prf.h index 4740d02f3..83ce9a648 100644 --- a/third_party/boringssl/src/include/openssl/tls_prf.h +++ b/third_party/boringssl/src/include/openssl/tls_prf.h @@ -27,10 +27,10 @@ extern "C" { // The TLS PRF is defined in Section 5 of RFC 5246. -// CRYPTO_tls1_prf calculates |out_len| bytes of the TLS PRF, using |digest|, -// and writes them to |out|. It is defined in Section 5 of RFC 5246, acting on -// |secret_len| bytes of shared |secret|, |label_len| bytes of |label|, -// |seed1_len| bytes of |seed1| and |seed2_len| bytes of |seed2|. It returns one +// CRYPTO_tls1_prf calculates `out_len` bytes of the TLS PRF, using `digest`, +// and writes them to `out`. It is defined in Section 5 of RFC 5246, acting on +// `secret_len` bytes of shared `secret`, `label_len` bytes of `label`, +// `seed1_len` bytes of `seed1` and `seed2_len` bytes of `seed2`. It returns one // on success and zero on error. OPENSSL_EXPORT int CRYPTO_tls1_prf(const EVP_MD *digest, uint8_t *out, size_t out_len, const uint8_t *secret, diff --git a/third_party/boringssl/src/include/openssl/trust_token.h b/third_party/boringssl/src/include/openssl/trust_token.h index e9c466793..e0fbcea8d 100644 --- a/third_party/boringssl/src/include/openssl/trust_token.h +++ b/third_party/boringssl/src/include/openssl/trust_token.h @@ -15,7 +15,7 @@ #ifndef OPENSSL_HEADER_TRUST_TOKEN_H #define OPENSSL_HEADER_TRUST_TOKEN_H -#include // IWYU pragma: export +#include // IWYU pragma: export #include #if defined(__cplusplus) @@ -67,25 +67,25 @@ struct trust_token_st { DEFINE_STACK_OF(TRUST_TOKEN) -// TRUST_TOKEN_new creates a newly-allocated |TRUST_TOKEN| with value |data| or +// TRUST_TOKEN_new creates a newly-allocated `TRUST_TOKEN` with value `data` or // NULL on allocation failure. OPENSSL_EXPORT TRUST_TOKEN *TRUST_TOKEN_new(const uint8_t *data, size_t len); -// TRUST_TOKEN_free releases memory associated with |token|. +// TRUST_TOKEN_free releases memory associated with `token`. OPENSSL_EXPORT void TRUST_TOKEN_free(TRUST_TOKEN *token); #define TRUST_TOKEN_MAX_PRIVATE_KEY_SIZE 512 #define TRUST_TOKEN_MAX_PUBLIC_KEY_SIZE 512 -// TRUST_TOKEN_generate_key creates a new Trust Token keypair labeled with |id| +// TRUST_TOKEN_generate_key creates a new Trust Token keypair labeled with `id` // and serializes the private and public keys, writing the private key to -// |out_priv_key| and setting |*out_priv_key_len| to the number of bytes -// written, and writing the public key to |out_pub_key| and setting -// |*out_pub_key_len| to the number of bytes written. +// `out_priv_key` and setting `*out_priv_key_len` to the number of bytes +// written, and writing the public key to `out_pub_key` and setting +// `*out_pub_key_len` to the number of bytes written. // -// At most |max_priv_key_len| and |max_pub_key_len| bytes are written. In order +// At most `max_priv_key_len` and `max_pub_key_len` bytes are written. In order // to ensure success, these should be at least -// |TRUST_TOKEN_MAX_PRIVATE_KEY_SIZE| and |TRUST_TOKEN_MAX_PUBLIC_KEY_SIZE|. +// `TRUST_TOKEN_MAX_PRIVATE_KEY_SIZE` and `TRUST_TOKEN_MAX_PUBLIC_KEY_SIZE`. // // This function returns one on success or zero on error. OPENSSL_EXPORT int TRUST_TOKEN_generate_key( @@ -94,15 +94,15 @@ OPENSSL_EXPORT int TRUST_TOKEN_generate_key( size_t *out_pub_key_len, size_t max_pub_key_len, uint32_t id); // TRUST_TOKEN_derive_key_from_secret deterministically derives a new Trust -// Token keypair labeled with |id| from an input |secret| and serializes the -// private and public keys, writing the private key to |out_priv_key| and -// setting |*out_priv_key_len| to the number of bytes written, and writing the -// public key to |out_pub_key| and setting |*out_pub_key_len| to the number of +// Token keypair labeled with `id` from an input `secret` and serializes the +// private and public keys, writing the private key to `out_priv_key` and +// setting `*out_priv_key_len` to the number of bytes written, and writing the +// public key to `out_pub_key` and setting `*out_pub_key_len` to the number of // bytes written. // -// At most |max_priv_key_len| and |max_pub_key_len| bytes are written. In order +// At most `max_priv_key_len` and `max_pub_key_len` bytes are written. In order // to ensure success, these should be at least -// |TRUST_TOKEN_MAX_PRIVATE_KEY_SIZE| and |TRUST_TOKEN_MAX_PUBLIC_KEY_SIZE|. +// `TRUST_TOKEN_MAX_PRIVATE_KEY_SIZE` and `TRUST_TOKEN_MAX_PUBLIC_KEY_SIZE`. // // This function returns one on success or zero on error. OPENSSL_EXPORT int TRUST_TOKEN_derive_key_from_secret( @@ -115,34 +115,34 @@ OPENSSL_EXPORT int TRUST_TOKEN_derive_key_from_secret( // Trust Token client implementation. // // These functions implements the client half of the Trust Token protocol. A -// single |TRUST_TOKEN_CLIENT| can perform a single protocol operation. +// single `TRUST_TOKEN_CLIENT` can perform a single protocol operation. -// TRUST_TOKEN_CLIENT_new returns a newly-allocated |TRUST_TOKEN_CLIENT| -// configured to use a max batchsize of |max_batchsize| or NULL on error. -// Issuance requests must be made in batches smaller than |max_batchsize|. This -// function will return an error if |max_batchsize| is too large for Trust +// TRUST_TOKEN_CLIENT_new returns a newly-allocated `TRUST_TOKEN_CLIENT` +// configured to use a max batchsize of `max_batchsize` or NULL on error. +// Issuance requests must be made in batches smaller than `max_batchsize`. This +// function will return an error if `max_batchsize` is too large for Trust // Tokens. OPENSSL_EXPORT TRUST_TOKEN_CLIENT *TRUST_TOKEN_CLIENT_new( const TRUST_TOKEN_METHOD *method, size_t max_batchsize); -// TRUST_TOKEN_CLIENT_free releases memory associated with |ctx|. +// TRUST_TOKEN_CLIENT_free releases memory associated with `ctx`. OPENSSL_EXPORT void TRUST_TOKEN_CLIENT_free(TRUST_TOKEN_CLIENT *ctx); -// TRUST_TOKEN_CLIENT_dup_for_testing returns a newly-allocated copy of |ctx|, +// TRUST_TOKEN_CLIENT_dup_for_testing returns a newly-allocated copy of `ctx`, // or NULL on error. This may be useful for testing the library, e.g. to // benchmark an individual operation. // // WARNING: This function should never be used in production. A -// |TRUST_TOKEN_CLIENT| maintains single-use state between -// |TRUST_TOKEN_CLIENT_begin_issuance| and |TRUST_TOKEN_CLIENT_finish_issuance| +// `TRUST_TOKEN_CLIENT` maintains single-use state between +// `TRUST_TOKEN_CLIENT_begin_issuance` and `TRUST_TOKEN_CLIENT_finish_issuance` // operations. Cloning this state will cause tokens to be linkable and no longer // anonymized. OPENSSL_EXPORT TRUST_TOKEN_CLIENT *TRUST_TOKEN_CLIENT_dup_for_testing( const TRUST_TOKEN_CLIENT *ctx); -// TRUST_TOKEN_CLIENT_add_key configures the |ctx| to support the public key -// |key|. It sets |*out_key_index| to the index this key has been configured to. -// It returns one on success or zero on error if the |key| can't be parsed or +// TRUST_TOKEN_CLIENT_add_key configures the `ctx` to support the public key +// `key`. It sets `*out_key_index` to the index this key has been configured to. +// It returns one on success or zero on error if the `key` can't be parsed or // too many keys have been configured. OPENSSL_EXPORT int TRUST_TOKEN_CLIENT_add_key(TRUST_TOKEN_CLIENT *ctx, size_t *out_key_index, @@ -152,12 +152,12 @@ OPENSSL_EXPORT int TRUST_TOKEN_CLIENT_add_key(TRUST_TOKEN_CLIENT *ctx, // TRUST_TOKEN_CLIENT_set_srr_key sets the public key used to verify the SRR. It // returns one on success and zero on error. OPENSSL_EXPORT int TRUST_TOKEN_CLIENT_set_srr_key(TRUST_TOKEN_CLIENT *ctx, - EVP_PKEY *key); + const EVP_PKEY *key); -// TRUST_TOKEN_CLIENT_begin_issuance produces a request for |count| trust tokens -// and serializes the request into a newly-allocated buffer, setting |*out| to -// that buffer and |*out_len| to its length. The caller takes ownership of the -// buffer and must call |OPENSSL_free| when done. It returns one on success and +// TRUST_TOKEN_CLIENT_begin_issuance produces a request for `count` trust tokens +// and serializes the request into a newly-allocated buffer, setting `*out` to +// that buffer and `*out_len` to its length. The caller takes ownership of the +// buffer and must call `OPENSSL_free` when done. It returns one on success and // zero on error. OPENSSL_EXPORT int TRUST_TOKEN_CLIENT_begin_issuance(TRUST_TOKEN_CLIENT *ctx, uint8_t **out, @@ -165,46 +165,44 @@ OPENSSL_EXPORT int TRUST_TOKEN_CLIENT_begin_issuance(TRUST_TOKEN_CLIENT *ctx, size_t count); // TRUST_TOKEN_CLIENT_begin_issuance_over_message produces a request for a trust -// token derived from |msg| and serializes the request into a newly-allocated -// buffer, setting |*out| to that buffer and |*out_len| to its length. The -// caller takes ownership of the buffer and must call |OPENSSL_free| when done. +// token derived from `msg` and serializes the request into a newly-allocated +// buffer, setting `*out` to that buffer and `*out_len` to its length. The +// caller takes ownership of the buffer and must call `OPENSSL_free` when done. // It returns one on success and zero on error. OPENSSL_EXPORT int TRUST_TOKEN_CLIENT_begin_issuance_over_message( TRUST_TOKEN_CLIENT *ctx, uint8_t **out, size_t *out_len, size_t count, const uint8_t *msg, size_t msg_len); -// TRUST_TOKEN_CLIENT_finish_issuance consumes |response| from the issuer and +// TRUST_TOKEN_CLIENT_finish_issuance consumes `response` from the issuer and // extracts the tokens, returning a list of tokens and the index of the key used -// to sign the tokens in |*out_key_index|. The caller can use this to determine +// to sign the tokens in `*out_key_index`. The caller can use this to determine // what key was used in an issuance and to drop tokens if a new key commitment // arrives without the specified key present. The caller takes ownership of the -// list and must call |sk_TRUST_TOKEN_pop_free| when done. The list is empty if +// list and must call `sk_TRUST_TOKEN_pop_free` when done. The list is empty if // issuance fails. -OPENSSL_EXPORT STACK_OF(TRUST_TOKEN) * - TRUST_TOKEN_CLIENT_finish_issuance(TRUST_TOKEN_CLIENT *ctx, - size_t *out_key_index, - const uint8_t *response, - size_t response_len); +OPENSSL_EXPORT STACK_OF(TRUST_TOKEN) *TRUST_TOKEN_CLIENT_finish_issuance( + TRUST_TOKEN_CLIENT *ctx, size_t *out_key_index, const uint8_t *response, + size_t response_len); // TRUST_TOKEN_CLIENT_begin_redemption produces a request to redeem a token -// |token| and receive a signature over |data| and serializes the request into -// a newly-allocated buffer, setting |*out| to that buffer and |*out_len| to -// its length. |time| is the number of seconds since the UNIX epoch and used to +// `token` and receive a signature over `data` and serializes the request into +// a newly-allocated buffer, setting `*out` to that buffer and `*out_len` to +// its length. `time` is the number of seconds since the UNIX epoch and used to // verify the validity of the issuer's response in TrustTokenV1 and ignored in // other versions. The caller takes ownership of the buffer and must call -// |OPENSSL_free| when done. It returns one on success or zero on error. +// `OPENSSL_free` when done. It returns one on success or zero on error. OPENSSL_EXPORT int TRUST_TOKEN_CLIENT_begin_redemption( TRUST_TOKEN_CLIENT *ctx, uint8_t **out, size_t *out_len, const TRUST_TOKEN *token, const uint8_t *data, size_t data_len, uint64_t time); -// TRUST_TOKEN_CLIENT_finish_redemption consumes |response| from the issuer. In -// |TRUST_TOKEN_experiment_v1|, it then verifies the SRR and if valid sets -// |*out_rr| and |*out_rr_len| (respectively, |*out_sig| and |*out_sig_len|) +// TRUST_TOKEN_CLIENT_finish_redemption consumes `response` from the issuer. In +// `TRUST_TOKEN_experiment_v1`, it then verifies the SRR and if valid sets +// `*out_rr` and `*out_rr_len` (respectively, `*out_sig` and `*out_sig_len`) // to a newly-allocated buffer containing the SRR (respectively, the SRR -// signature). In other versions, it sets |*out_rr| and |*out_rr_len| -// to a newly-allocated buffer containing |response| and leaves all validation +// signature). In other versions, it sets `*out_rr` and `*out_rr_len` +// to a newly-allocated buffer containing `response` and leaves all validation // to the caller. It returns one on success or zero on failure. OPENSSL_EXPORT int TRUST_TOKEN_CLIENT_finish_redemption( TRUST_TOKEN_CLIENT *ctx, uint8_t **out_rr, size_t *out_rr_len, @@ -215,26 +213,26 @@ OPENSSL_EXPORT int TRUST_TOKEN_CLIENT_finish_redemption( // Trust Token issuer implementation. // // These functions implement the issuer half of the Trust Token protocol. A -// |TRUST_TOKEN_ISSUER| can be reused across multiple protocol operations. It +// `TRUST_TOKEN_ISSUER` can be reused across multiple protocol operations. It // may be used concurrently on multiple threads by non-mutating functions, // provided no other thread is concurrently calling a mutating function. -// Functions which take a |const| pointer are non-mutating and functions which -// take a non-|const| pointer are mutating. +// Functions which take a `const` pointer are non-mutating and functions which +// take a non-`const` pointer are mutating. -// TRUST_TOKEN_ISSUER_new returns a newly-allocated |TRUST_TOKEN_ISSUER| -// configured to use a max batchsize of |max_batchsize| or NULL on error. -// Issuance requests must be made in batches smaller than |max_batchsize|. This -// function will return an error if |max_batchsize| is too large for Trust +// TRUST_TOKEN_ISSUER_new returns a newly-allocated `TRUST_TOKEN_ISSUER` +// configured to use a max batchsize of `max_batchsize` or NULL on error. +// Issuance requests must be made in batches smaller than `max_batchsize`. This +// function will return an error if `max_batchsize` is too large for Trust // Tokens. OPENSSL_EXPORT TRUST_TOKEN_ISSUER *TRUST_TOKEN_ISSUER_new( const TRUST_TOKEN_METHOD *method, size_t max_batchsize); -// TRUST_TOKEN_ISSUER_free releases memory associated with |ctx|. +// TRUST_TOKEN_ISSUER_free releases memory associated with `ctx`. OPENSSL_EXPORT void TRUST_TOKEN_ISSUER_free(TRUST_TOKEN_ISSUER *ctx); -// TRUST_TOKEN_ISSUER_add_key configures the |ctx| to support the private key -// |key|. It must be a private key returned by |TRUST_TOKEN_generate_key|. It -// returns one on success or zero on error. This function may fail if the |key| +// TRUST_TOKEN_ISSUER_add_key configures the `ctx` to support the private key +// `key`. It must be a private key returned by `TRUST_TOKEN_generate_key`. It +// returns one on success or zero on error. This function may fail if the `key` // can't be parsed or too many keys have been configured. OPENSSL_EXPORT int TRUST_TOKEN_ISSUER_add_key(TRUST_TOKEN_ISSUER *ctx, const uint8_t *key, @@ -243,33 +241,33 @@ OPENSSL_EXPORT int TRUST_TOKEN_ISSUER_add_key(TRUST_TOKEN_ISSUER *ctx, // TRUST_TOKEN_ISSUER_set_srr_key sets the private key used to sign the SRR. It // returns one on success and zero on error. OPENSSL_EXPORT int TRUST_TOKEN_ISSUER_set_srr_key(TRUST_TOKEN_ISSUER *ctx, - EVP_PKEY *key); + const EVP_PKEY *key); -// TRUST_TOKEN_ISSUER_issue ingests |request| for token issuance -// and generates up to |max_issuance| valid tokens, producing a list of blinded +// TRUST_TOKEN_ISSUER_issue ingests `request` for token issuance +// and generates up to `max_issuance` valid tokens, producing a list of blinded // tokens and storing the response into a newly-allocated buffer and setting -// |*out| to that buffer, |*out_len| to its length, and |*out_tokens_issued| to +// `*out` to that buffer, `*out_len` to its length, and `*out_tokens_issued` to // the number of tokens issued. The tokens are issued with public metadata of -// |public_metadata| and a private metadata value of |private_metadata|. -// |public_metadata| must be one of the previously configured key IDs. -// |private_metadata| must be 0 or 1. The caller takes ownership of the buffer -// and must call |OPENSSL_free| when done. It returns one on success or zero on +// `public_metadata` and a private metadata value of `private_metadata`. +// `public_metadata` must be one of the previously configured key IDs. +// `private_metadata` must be 0 or 1. The caller takes ownership of the buffer +// and must call `OPENSSL_free` when done. It returns one on success or zero on // error. OPENSSL_EXPORT int TRUST_TOKEN_ISSUER_issue( const TRUST_TOKEN_ISSUER *ctx, uint8_t **out, size_t *out_len, size_t *out_tokens_issued, const uint8_t *request, size_t request_len, uint32_t public_metadata, uint8_t private_metadata, size_t max_issuance); -// TRUST_TOKEN_ISSUER_redeem ingests a |request| for token redemption and -// verifies the token. The public metadata is stored in |*out_public|. The -// private metadata (if any) is stored in |*out_private|. The extracted -// |TRUST_TOKEN| is stored into a newly-allocated buffer and stored in -// |*out_token|. The extracted client data is stored into a newly-allocated -// buffer and stored in |*out_client_data|. The caller takes ownership of each -// output buffer and must call |OPENSSL_free| when done. It returns one on +// TRUST_TOKEN_ISSUER_redeem ingests a `request` for token redemption and +// verifies the token. The public metadata is stored in `*out_public`. The +// private metadata (if any) is stored in `*out_private`. The extracted +// `TRUST_TOKEN` is stored into a newly-allocated buffer and stored in +// `*out_token`. The extracted client data is stored into a newly-allocated +// buffer and stored in `*out_client_data`. The caller takes ownership of each +// output buffer and must call `OPENSSL_free` when done. It returns one on // success or zero on error. // -// The caller must keep track of all values of |*out_token| seen globally before +// The caller must keep track of all values of `*out_token` seen globally before // returning a response to the client. If the value has been reused, the caller // must report an error to the client. Returning a response with replayed values // allows an attacker to double-spend tokens. @@ -279,20 +277,20 @@ OPENSSL_EXPORT int TRUST_TOKEN_ISSUER_redeem( size_t *out_client_data_len, const uint8_t *request, size_t request_len); // TRUST_TOKEN_ISSUER_redeem_raw is a legacy alias for -// |TRUST_TOKEN_ISSUER_redeem|. +// `TRUST_TOKEN_ISSUER_redeem`. #define TRUST_TOKEN_ISSUER_redeem_raw TRUST_TOKEN_ISSUER_redeem -// TRUST_TOKEN_ISSUER_redeem_over_message ingests a |request| for token +// TRUST_TOKEN_ISSUER_redeem_over_message ingests a `request` for token // redemption and a message and verifies the token and that it is derived from -// the provided |msg|. The public metadata is stored in -// |*out_public|. The private metadata (if any) is stored in |*out_private|. The -// extracted |TRUST_TOKEN| is stored into a newly-allocated buffer and stored in -// |*out_token|. The extracted client data is stored into a newly-allocated -// buffer and stored in |*out_client_data|. The caller takes ownership of each -// output buffer and must call |OPENSSL_free| when done. It returns one on +// the provided `msg`. The public metadata is stored in +// `*out_public`. The private metadata (if any) is stored in `*out_private`. The +// extracted `TRUST_TOKEN` is stored into a newly-allocated buffer and stored in +// `*out_token`. The extracted client data is stored into a newly-allocated +// buffer and stored in `*out_client_data`. The caller takes ownership of each +// output buffer and must call `OPENSSL_free` when done. It returns one on // success or zero on error. // -// The caller must keep track of all values of |*out_token| seen globally before +// The caller must keep track of all values of `*out_token` seen globally before // returning a response to the client. If the value has been reused, the caller // must report an error to the client. Returning a response with replayed values // allows an attacker to double-spend tokens. @@ -302,10 +300,10 @@ OPENSSL_EXPORT int TRUST_TOKEN_ISSUER_redeem_over_message( size_t *out_client_data_len, const uint8_t *request, size_t request_len, const uint8_t *msg, size_t msg_len); -// TRUST_TOKEN_decode_private_metadata decodes |encrypted_bit| using the -// private metadata key specified by a |key| buffer of length |key_len| and the -// nonce by a |nonce| buffer of length |nonce_len|. The nonce in -// |TRUST_TOKEN_experiment_v1| is the token-hash field of the SRR. |*out_value| +// TRUST_TOKEN_decode_private_metadata decodes `encrypted_bit` using the +// private metadata key specified by a `key` buffer of length `key_len` and the +// nonce by a `nonce` buffer of length `nonce_len`. The nonce in +// `TRUST_TOKEN_experiment_v1` is the token-hash field of the SRR. `*out_value` // is set to the decrypted value, either zero or one. It returns one on success // and zero on error. OPENSSL_EXPORT int TRUST_TOKEN_decode_private_metadata( diff --git a/third_party/boringssl/src/include/openssl/type_check.h b/third_party/boringssl/src/include/openssl/type_check.h index 185b76567..a95bf1318 100644 --- a/third_party/boringssl/src/include/openssl/type_check.h +++ b/third_party/boringssl/src/include/openssl/type_check.h @@ -22,7 +22,7 @@ extern "C" { #endif -// CHECKED_CAST casts |p| from type |from| to type |to|. +// CHECKED_CAST casts `p` from type `from` to type `to`. // // TODO(davidben): Although this macro is not public API and is unused in // BoringSSL, wpa_supplicant uses it to define its own stacks. Remove this once diff --git a/third_party/boringssl/src/include/openssl/types.h b/third_party/boringssl/src/include/openssl/types.h new file mode 100644 index 000000000..74c1d7a48 --- /dev/null +++ b/third_party/boringssl/src/include/openssl/types.h @@ -0,0 +1,18 @@ +// Copyright 2026 The BoringSSL Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +/* This header is provided in order to make compiling against code that expects + OpenSSL easier. */ + +#include "base.h" diff --git a/third_party/boringssl/src/include/openssl/x509.h b/third_party/boringssl/src/include/openssl/x509.h index e72d9ca9b..9329ad149 100644 --- a/third_party/boringssl/src/include/openssl/x509.h +++ b/third_party/boringssl/src/include/openssl/x509.h @@ -57,11 +57,11 @@ extern "C" { // Certificates. // -// An |X509| object represents an X.509 certificate, defined in RFC 5280. +// An `X509` object represents an X.509 certificate, defined in RFC 5280. // -// Although an |X509| is a mutable object, mutating an |X509| can give incorrect -// results. Callers typically obtain |X509|s by parsing some input with -// |d2i_X509|, etc. Such objects carry information such as the serialized +// Although an `X509` is a mutable object, mutating an `X509` can give incorrect +// results. Callers typically obtain `X509`s by parsing some input with +// `d2i_X509`, etc. Such objects carry information such as the serialized // TBSCertificate and decoded extensions, which will become inconsistent when // mutated. // @@ -70,59 +70,59 @@ extern "C" { DEFINE_STACK_OF(X509) -// X509 is an |ASN1_ITEM| whose ASN.1 type is X.509 Certificate (RFC 5280) and C -// type is |X509*|. +// X509 is an `ASN1_ITEM` whose ASN.1 type is X.509 Certificate (RFC 5280) and C +// type is `X509*`. DECLARE_ASN1_ITEM(X509) -// X509_up_ref adds one to the reference count of |x509| and returns one. +// X509_up_ref adds one to the reference count of `x509` and returns one. OPENSSL_EXPORT int X509_up_ref(X509 *x509); -// X509_dup_ref increments the reference count of |x509| and returns |x509|. -// The caller must call |X509_free| on the result to release the reference. +// X509_dup_ref increments the reference count of `x509` and returns `x509`. +// The caller must call `X509_free` on the result to release the reference. // -// WARNING: Although the result is non-const for use with |X509_free|, it is -// still shared with other parts of the appplication for the same object. Avoid -// mutating shared |X509|s. +// WARNING: Although the result is non-const for use with `X509_free`, it is +// still shared with other parts of the application for the same object. Avoid +// mutating shared `X509`s. OPENSSL_EXPORT X509 *X509_dup_ref(const X509 *x509); -// X509_chain_up_ref returns a newly-allocated |STACK_OF(X509)| containing a -// shallow copy of |chain|, or NULL on error. That is, the return value has the -// same contents as |chain|, and each |X509|'s reference count is incremented by +// X509_chain_up_ref returns a newly-allocated `STACK_OF(X509)` containing a +// shallow copy of `chain`, or NULL on error. That is, the return value has the +// same contents as `chain`, and each `X509`'s reference count is incremented by // one. -OPENSSL_EXPORT STACK_OF(X509) *X509_chain_up_ref(STACK_OF(X509) *chain); +OPENSSL_EXPORT STACK_OF(X509) *X509_chain_up_ref(const STACK_OF(X509) *chain); -// X509_dup returns a newly-allocated copy of |x509|, or NULL on error. This +// X509_dup returns a newly-allocated copy of `x509`, or NULL on error. This // function works by serializing the structure, so auxiliary properties (see -// |i2d_X509_AUX|) are not preserved. Additionally, if |x509| is incomplete, +// `i2d_X509_AUX`) are not preserved. Additionally, if `x509` is incomplete, // this function may fail. OPENSSL_EXPORT X509 *X509_dup(const X509 *x509); -// X509_free decrements |x509|'s reference count and, if zero, releases memory -// associated with |x509|. +// X509_free decrements `x509`'s reference count and, if zero, releases memory +// associated with `x509`. OPENSSL_EXPORT void X509_free(X509 *x509); -// d2i_X509 parses up to |len| bytes from |*inp| as a DER-encoded X.509 -// Certificate (RFC 5280), as described in |d2i_SAMPLE|. +// d2i_X509 parses up to `len` bytes from `*inp` as a DER-encoded X.509 +// Certificate (RFC 5280), as described in `d2i_SAMPLE`. OPENSSL_EXPORT X509 *d2i_X509(X509 **out, const uint8_t **inp, long len); -// X509_parse_with_algorithms parses an X.509 structure from |buf| and returns a -// fresh X509 or NULL on error. There must not be any trailing data in |buf|. -// The returned structure (if any) increment's |buf|'s reference count and +// X509_parse_with_algorithms parses an X.509 structure from `buf` and returns a +// fresh X509 or NULL on error. There must not be any trailing data in `buf`. +// The returned structure (if any) increments `buf`'s reference count and // retains a reference to it. // -// Only the |num_algs| algorithms from |algs| will be considered when parsing +// Only the `num_algs` algorithms from `algs` will be considered when parsing // the certificate's public key. If the certificate uses a different algorithm, -// it will still be parsed, but |X509_get0_pubkey| will return NULL. -OPENSSL_EXPORT X509 *X509_parse_with_algorithms(CRYPTO_BUFFER *buf, +// it will still be parsed, but `X509_get0_pubkey` will return NULL. +OPENSSL_EXPORT X509 *X509_parse_with_algorithms(const CRYPTO_BUFFER *buf, const EVP_PKEY_ALG *const *algs, size_t num_algs); -// X509_parse_from_buffer behaves like |X509_parse_with_algorithms| but uses a +// X509_parse_from_buffer behaves like `X509_parse_with_algorithms` but uses a // default algorithm list. -OPENSSL_EXPORT X509 *X509_parse_from_buffer(CRYPTO_BUFFER *buf); +OPENSSL_EXPORT X509 *X509_parse_from_buffer(const CRYPTO_BUFFER *buf); -// i2d_X509 marshals |x509| as a DER-encoded X.509 Certificate (RFC 5280), as -// described in |i2d_SAMPLE|. +// i2d_X509 marshals `x509` as a DER-encoded X.509 Certificate (RFC 5280), as +// described in `i2d_SAMPLE`. OPENSSL_EXPORT int i2d_X509(const X509 *x509, uint8_t **outp); // X509_VERSION_* are X.509 version numbers. Note the numerical values of all @@ -131,65 +131,65 @@ OPENSSL_EXPORT int i2d_X509(const X509 *x509, uint8_t **outp); #define X509_VERSION_2 1 #define X509_VERSION_3 2 -// X509_get_version returns the numerical value of |x509|'s version, which will -// be one of the |X509_VERSION_*| constants. +// X509_get_version returns the numerical value of `x509`'s version, which will +// be one of the `X509_VERSION_*` constants. OPENSSL_EXPORT long X509_get_version(const X509 *x509); -// X509_get0_serialNumber returns |x509|'s serial number. +// X509_get0_serialNumber returns `x509`'s serial number. OPENSSL_EXPORT const ASN1_INTEGER *X509_get0_serialNumber(const X509 *x509); -// X509_get0_notBefore returns |x509|'s notBefore time. +// X509_get0_notBefore returns `x509`'s notBefore time. OPENSSL_EXPORT const ASN1_TIME *X509_get0_notBefore(const X509 *x509); -// X509_get0_notAfter returns |x509|'s notAfter time. +// X509_get0_notAfter returns `x509`'s notAfter time. OPENSSL_EXPORT const ASN1_TIME *X509_get0_notAfter(const X509 *x509); -// X509_get_issuer_name returns |x509|'s issuer. +// X509_get_issuer_name returns `x509`'s issuer. OPENSSL_EXPORT X509_NAME *X509_get_issuer_name(const X509 *x509); -// X509_get_subject_name returns |x509|'s subject. +// X509_get_subject_name returns `x509`'s subject. OPENSSL_EXPORT X509_NAME *X509_get_subject_name(const X509 *x509); -// X509_get_X509_PUBKEY returns the public key of |x509|. Note this function is +// X509_get_X509_PUBKEY returns the public key of `x509`. Note this function is // not const-correct for legacy reasons. Callers should not modify the returned // object. OPENSSL_EXPORT X509_PUBKEY *X509_get_X509_PUBKEY(const X509 *x509); -// X509_get0_pubkey returns |x509|'s public key as an |EVP_PKEY|, or NULL if the -// public key was unsupported or could not be decoded. The |EVP_PKEY| is cached -// in |x509|, so callers must not mutate the result. +// X509_get0_pubkey returns `x509`'s public key as an `EVP_PKEY`, or NULL if the +// public key was unsupported or could not be decoded. The `EVP_PKEY` is cached +// in `x509`, so callers must not mutate the result. OPENSSL_EXPORT EVP_PKEY *X509_get0_pubkey(const X509 *x509); -// X509_get_pubkey behaves like |X509_get0_pubkey| but increments the reference -// count on the |EVP_PKEY|. The caller must release the result with -// |EVP_PKEY_free| when done. The |EVP_PKEY| is cached in |x509|, so callers +// X509_get_pubkey behaves like `X509_get0_pubkey` but increments the reference +// count on the `EVP_PKEY`. The caller must release the result with +// `EVP_PKEY_free` when done. The `EVP_PKEY` is cached in `x509`, so callers // must not mutate the result. OPENSSL_EXPORT EVP_PKEY *X509_get_pubkey(const X509 *x509); -// X509_get0_pubkey_bitstr returns the BIT STRING portion of |x509|'s public +// X509_get0_pubkey_bitstr returns the BIT STRING portion of `x509`'s public // key. Note this does not contain the AlgorithmIdentifier portion. // // WARNING: This function returns a non-const pointer for OpenSSL compatibility, // but the caller must not modify the resulting object. Doing so will break -// internal invariants in |x509|. +// internal invariants in `x509`. OPENSSL_EXPORT ASN1_BIT_STRING *X509_get0_pubkey_bitstr(const X509 *x509); -// X509_check_private_key returns one if |x509|'s public key matches |pkey| and +// X509_check_private_key returns one if `x509`'s public key matches `pkey` and // zero otherwise. OPENSSL_EXPORT int X509_check_private_key(const X509 *x509, const EVP_PKEY *pkey); -// X509_get0_uids sets |*out_issuer_uid| to a non-owning pointer to the -// issuerUID field of |x509|, or NULL if |x509| has no issuerUID. It similarly -// outputs |x509|'s subjectUID field to |*out_subject_uid|. +// X509_get0_uids sets `*out_issuer_uid` to a non-owning pointer to the +// issuerUID field of `x509`, or NULL if `x509` has no issuerUID. It similarly +// outputs `x509`'s subjectUID field to `*out_subject_uid`. // -// Callers may pass NULL to either |out_issuer_uid| or |out_subject_uid| to +// Callers may pass NULL to either `out_issuer_uid` or `out_subject_uid` to // ignore the corresponding field. OPENSSL_EXPORT void X509_get0_uids(const X509 *x509, const ASN1_BIT_STRING **out_issuer_uid, const ASN1_BIT_STRING **out_subject_uid); -// The following bits are returned from |X509_get_extension_flags|. +// The following bits are returned from `X509_get_extension_flags`. // EXFLAG_BCONS indicates the certificate has a basic constraints extension. #define EXFLAG_BCONS 0x1 @@ -220,21 +220,21 @@ OPENSSL_EXPORT void X509_get0_uids(const X509 *x509, // itself is not checked in computing this bit. #define EXFLAG_SS 0x2000 -// X509_get_extension_flags decodes a set of extensions from |x509| and returns -// a collection of |EXFLAG_*| bits which reflect |x509|. If there was an error -// in computing this bitmask, the result will include the |EXFLAG_INVALID| bit. -OPENSSL_EXPORT uint32_t X509_get_extension_flags(X509 *x509); +// X509_get_extension_flags decodes a set of extensions from `x509` and returns +// a collection of `EXFLAG_*` bits which reflect `x509`. If there was an error +// in computing this bitmask, the result will include the `EXFLAG_INVALID` bit. +OPENSSL_EXPORT uint32_t X509_get_extension_flags(const X509 *x509); // X509_get_pathlen returns path length constraint from the basic constraints -// extension in |x509|. (See RFC 5280, section 4.2.1.9.) It returns -1 if the -// constraint is not present, or if some extension in |x509| was invalid. +// extension in `x509`. (See RFC 5280, section 4.2.1.9.) It returns -1 if the +// constraint is not present, or if some extension in `x509` was invalid. // -// TODO(crbug.com/boringssl/381): Decoding an |X509| object will not check for +// TODO(crbug.com/boringssl/381): Decoding an `X509` object will not check for // invalid extensions. To detect the error case, call -// |X509_get_extension_flags| and check the |EXFLAG_INVALID| bit. -OPENSSL_EXPORT long X509_get_pathlen(X509 *x509); +// `X509_get_extension_flags` and check the `EXFLAG_INVALID` bit. +OPENSSL_EXPORT long X509_get_pathlen(const X509 *x509); -// X509v3_KU_* are key usage bits returned from |X509_get_key_usage|. +// X509v3_KU_* are key usage bits returned from `X509_get_key_usage`. #define X509v3_KU_DIGITAL_SIGNATURE 0x0080 #define X509v3_KU_NON_REPUDIATION 0x0040 #define X509v3_KU_KEY_ENCIPHERMENT 0x0020 @@ -246,22 +246,22 @@ OPENSSL_EXPORT long X509_get_pathlen(X509 *x509); #define X509v3_KU_DECIPHER_ONLY 0x8000 // X509_get_key_usage returns a bitmask of key usages (see Section 4.2.1.3 of -// RFC 5280) which |x509| is valid for. This function only reports the first 16 +// RFC 5280) which `x509` is valid for. This function only reports the first 16 // bits, in a little-endian byte order, but big-endian bit order. That is, bits // 0 though 7 are reported at 1<<7 through 1<<0, and bits 8 through 15 are // reported at 1<<15 through 1<<8. // // Instead of depending on this bit order, callers should compare against the -// |X509v3_KU_*| constants. +// `X509v3_KU_*` constants. // -// If |x509| has no key usage extension, all key usages are valid and this -// function returns |UINT32_MAX|. If there was an error processing |x509|'s +// If `x509` has no key usage extension, all key usages are valid and this +// function returns `UINT32_MAX`. If there was an error processing `x509`'s // extensions, or if the first 16 bits in the key usage extension were all zero, // this function returns zero. -OPENSSL_EXPORT uint32_t X509_get_key_usage(X509 *x509); +OPENSSL_EXPORT uint32_t X509_get_key_usage(const X509 *x509); // XKU_* are extended key usage bits returned from -// |X509_get_extended_key_usage|. +// `X509_get_extended_key_usage`. #define XKU_SSL_SERVER 0x1 #define XKU_SSL_CLIENT 0x2 #define XKU_SMIME 0x4 @@ -273,285 +273,288 @@ OPENSSL_EXPORT uint32_t X509_get_key_usage(X509 *x509); #define XKU_ANYEKU 0x100 // X509_get_extended_key_usage returns a bitmask of extended key usages (see -// Section 4.2.1.12 of RFC 5280) which |x509| is valid for. The result will be -// a combination of |XKU_*| constants. If checking an extended key usage not +// Section 4.2.1.12 of RFC 5280) which `x509` is valid for. The result will be +// a combination of `XKU_*` constants. If checking an extended key usage not // defined above, callers should extract the extended key usage extension -// separately, e.g. via |X509_get_ext_d2i|. +// separately, e.g. via `X509_get_ext_d2i`. // -// If |x509| has no extended key usage extension, all extended key usages are -// valid and this function returns |UINT32_MAX|. If there was an error -// processing |x509|'s extensions, or if |x509|'s extended key usage extension +// If `x509` has no extended key usage extension, all extended key usages are +// valid and this function returns `UINT32_MAX`. If there was an error +// processing `x509`'s extensions, or if `x509`'s extended key usage extension // contained no recognized usages, this function returns zero. -OPENSSL_EXPORT uint32_t X509_get_extended_key_usage(X509 *x509); +OPENSSL_EXPORT uint32_t X509_get_extended_key_usage(const X509 *x509); -// X509_get0_subject_key_id returns |x509|'s subject key identifier, if present. +// X509_get0_subject_key_id returns `x509`'s subject key identifier, if present. // (See RFC 5280, section 4.2.1.2.) It returns NULL if the extension is not -// present or if some extension in |x509| was invalid. +// present or if some extension in `x509` was invalid. // -// TODO(crbug.com/boringssl/381): Decoding an |X509| object will not check for +// TODO(crbug.com/boringssl/381): Decoding an `X509` object will not check for // invalid extensions. To detect the error case, call -// |X509_get_extension_flags| and check the |EXFLAG_INVALID| bit. -OPENSSL_EXPORT const ASN1_OCTET_STRING *X509_get0_subject_key_id(X509 *x509); +// `X509_get_extension_flags` and check the `EXFLAG_INVALID` bit. +OPENSSL_EXPORT const ASN1_OCTET_STRING *X509_get0_subject_key_id( + const X509 *x509); -// X509_get0_authority_key_id returns keyIdentifier of |x509|'s authority key +// X509_get0_authority_key_id returns keyIdentifier of `x509`'s authority key // identifier, if the extension and field are present. (See RFC 5280, // section 4.2.1.1.) It returns NULL if the extension is not present, if it is -// present but lacks a keyIdentifier field, or if some extension in |x509| was +// present but lacks a keyIdentifier field, or if some extension in `x509` was // invalid. // -// TODO(crbug.com/boringssl/381): Decoding an |X509| object will not check for +// TODO(crbug.com/boringssl/381): Decoding an `X509` object will not check for // invalid extensions. To detect the error case, call -// |X509_get_extension_flags| and check the |EXFLAG_INVALID| bit. -OPENSSL_EXPORT const ASN1_OCTET_STRING *X509_get0_authority_key_id(X509 *x509); +// `X509_get_extension_flags` and check the `EXFLAG_INVALID` bit. +OPENSSL_EXPORT const ASN1_OCTET_STRING *X509_get0_authority_key_id( + const X509 *x509); DEFINE_STACK_OF(GENERAL_NAME) typedef STACK_OF(GENERAL_NAME) GENERAL_NAMES; -// X509_get0_authority_issuer returns the authorityCertIssuer of |x509|'s +// X509_get0_authority_issuer returns the authorityCertIssuer of `x509`'s // authority key identifier, if the extension and field are present. (See // RFC 5280, section 4.2.1.1.) It returns NULL if the extension is not present, -// if it is present but lacks a authorityCertIssuer field, or if some extension -// in |x509| was invalid. +// if it is present but lacks an authorityCertIssuer field, or if some extension +// in `x509` was invalid. // -// TODO(crbug.com/boringssl/381): Decoding an |X509| object will not check for +// TODO(crbug.com/boringssl/381): Decoding an `X509` object will not check for // invalid extensions. To detect the error case, call -// |X509_get_extension_flags| and check the |EXFLAG_INVALID| bit. -OPENSSL_EXPORT const GENERAL_NAMES *X509_get0_authority_issuer(X509 *x509); +// `X509_get_extension_flags` and check the `EXFLAG_INVALID` bit. +OPENSSL_EXPORT const GENERAL_NAMES *X509_get0_authority_issuer( + const X509 *x509); -// X509_get0_authority_serial returns the authorityCertSerialNumber of |x509|'s +// X509_get0_authority_serial returns the authorityCertSerialNumber of `x509`'s // authority key identifier, if the extension and field are present. (See // RFC 5280, section 4.2.1.1.) It returns NULL if the extension is not present, -// if it is present but lacks a authorityCertSerialNumber field, or if some -// extension in |x509| was invalid. +// if it is present but lacks an authorityCertSerialNumber field, or if some +// extension in `x509` was invalid. // -// TODO(crbug.com/boringssl/381): Decoding an |X509| object will not check for +// TODO(crbug.com/boringssl/381): Decoding an `X509` object will not check for // invalid extensions. To detect the error case, call -// |X509_get_extension_flags| and check the |EXFLAG_INVALID| bit. -OPENSSL_EXPORT const ASN1_INTEGER *X509_get0_authority_serial(X509 *x509); +// `X509_get_extension_flags` and check the `EXFLAG_INVALID` bit. +OPENSSL_EXPORT const ASN1_INTEGER *X509_get0_authority_serial(const X509 *x509); -// X509_get0_extensions returns |x509|'s extension list, or NULL if |x509| omits +// X509_get0_extensions returns `x509`'s extension list, or NULL if `x509` omits // it. OPENSSL_EXPORT const STACK_OF(X509_EXTENSION) *X509_get0_extensions( const X509 *x509); -// X509_get_ext_count returns the number of extensions in |x|. +// X509_get_ext_count returns the number of extensions in `x`. OPENSSL_EXPORT int X509_get_ext_count(const X509 *x); -// X509_get_ext_by_NID behaves like |X509v3_get_ext_by_NID| but searches for -// extensions in |x|. +// X509_get_ext_by_NID behaves like `X509v3_get_ext_by_NID` but searches for +// extensions in `x`. OPENSSL_EXPORT int X509_get_ext_by_NID(const X509 *x, int nid, int lastpos); -// X509_get_ext_by_OBJ behaves like |X509v3_get_ext_by_OBJ| but searches for -// extensions in |x|. +// X509_get_ext_by_OBJ behaves like `X509v3_get_ext_by_OBJ` but searches for +// extensions in `x`. OPENSSL_EXPORT int X509_get_ext_by_OBJ(const X509 *x, const ASN1_OBJECT *obj, int lastpos); -// X509_get_ext_by_critical behaves like |X509v3_get_ext_by_critical| but -// searches for extensions in |x|. +// X509_get_ext_by_critical behaves like `X509v3_get_ext_by_critical` but +// searches for extensions in `x`. OPENSSL_EXPORT int X509_get_ext_by_critical(const X509 *x, int crit, int lastpos); -// X509_get_ext returns the extension in |x| at index |loc|, or NULL if |loc| is +// X509_get_ext returns the extension in `x` at index `loc`, or NULL if `loc` is // out of bounds. This function returns a non-const pointer for OpenSSL // compatibility, but callers should not mutate the result. OPENSSL_EXPORT X509_EXTENSION *X509_get_ext(const X509 *x, int loc); -// X509_get_ext_d2i behaves like |X509V3_get_d2i| but looks for the extension in -// |x509|'s extension list. +// X509_get_ext_d2i behaves like `X509V3_get_d2i` but looks for the extension in +// `x509`'s extension list. // // WARNING: This function is difficult to use correctly. See the documentation -// for |X509V3_get_d2i| for details. +// for `X509V3_get_d2i` for details. OPENSSL_EXPORT void *X509_get_ext_d2i(const X509 *x509, int nid, int *out_critical, int *out_idx); -// X509_get0_tbs_sigalg returns the signature algorithm in |x509|'s -// TBSCertificate. For the outer signature algorithm, see |X509_get0_signature|. +// X509_get0_tbs_sigalg returns the signature algorithm in `x509`'s +// TBSCertificate. For the outer signature algorithm, see `X509_get0_signature`. // // Certificates with mismatched signature algorithms will successfully parse, // but they will be rejected when verifying. OPENSSL_EXPORT const X509_ALGOR *X509_get0_tbs_sigalg(const X509 *x509); -// X509_get0_signature sets |*out_sig| and |*out_alg| to the signature and -// signature algorithm of |x509|, respectively. Either output pointer may be +// X509_get0_signature sets `*out_sig` and `*out_alg` to the signature and +// signature algorithm of `x509`, respectively. Either output pointer may be // NULL to ignore the value. // // This function outputs the outer signature algorithm. For the one in the -// TBSCertificate, see |X509_get0_tbs_sigalg|. Certificates with mismatched +// TBSCertificate, see `X509_get0_tbs_sigalg`. Certificates with mismatched // signature algorithms will successfully parse, but they will be rejected when // verifying. OPENSSL_EXPORT void X509_get0_signature(const ASN1_BIT_STRING **out_sig, const X509_ALGOR **out_alg, const X509 *x509); -// X509_get_signature_nid returns the NID corresponding to |x509|'s signature -// algorithm, or |NID_undef| if the signature algorithm does not correspond to +// X509_get_signature_nid returns the NID corresponding to `x509`'s signature +// algorithm, or `NID_undef` if the signature algorithm does not correspond to // a known NID. OPENSSL_EXPORT int X509_get_signature_nid(const X509 *x509); -// i2d_X509_tbs serializes the TBSCertificate portion of |x509|, as described in -// |i2d_SAMPLE|. +// i2d_X509_tbs serializes the TBSCertificate portion of `x509`, as described in +// `i2d_SAMPLE`. // // This function preserves the original encoding of the TBSCertificate and may -// not reflect modifications made to |x509|. It may be used to manually verify +// not reflect modifications made to `x509`. It may be used to manually verify // the signature of an existing certificate. To generate certificates, use -// |i2d_re_X509_tbs| instead. +// `i2d_re_X509_tbs` instead. OPENSSL_EXPORT int i2d_X509_tbs(const X509 *x509, uint8_t **outp); -// X509_verify checks that |x509| has a valid signature by |pkey|. It returns +// X509_verify checks that `x509` has a valid signature by `pkey`. It returns // one if the signature is valid and zero otherwise. Note this function only // checks the signature itself and does not perform a full certificate // validation. -OPENSSL_EXPORT int X509_verify(const X509 *x509, EVP_PKEY *pkey); +OPENSSL_EXPORT int X509_verify(const X509 *x509, const EVP_PKEY *pkey); // X509_get1_email returns a newly-allocated list of NUL-terminated strings -// containing all email addresses in |x509|'s subject and all rfc822name names -// in |x509|'s subject alternative names. Email addresses which contain embedded +// containing all email addresses in `x509`'s subject and all rfc822name names +// in `x509`'s subject alternative names. Email addresses which contain embedded // NUL bytes are skipped. The results are returned in an arbitrary order. // // On error, or if there are no such email addresses, it returns NULL. When -// done, the caller must release the result with |X509_email_free|. +// done, the caller must release the result with `X509_email_free`. OPENSSL_EXPORT STACK_OF(OPENSSL_STRING) *X509_get1_email(const X509 *x509); // X509_get1_ocsp returns a newly-allocated list of NUL-terminated strings -// containing all OCSP URIs in |x509|. That is, it collects all URI -// AccessDescriptions with an accessMethod of id-ad-ocsp in |x509|'s authority +// containing all OCSP URIs in `x509`. That is, it collects all URI +// AccessDescriptions with an accessMethod of id-ad-ocsp in `x509`'s authority // information access extension. URIs which contain embedded NUL bytes are // skipped. The results are returned in an arbitrary order. // // On error, or if there are no such URIs, it returns NULL. When done, the -// caller must release the result with |X509_email_free|. +// caller must release the result with `X509_email_free`. OPENSSL_EXPORT STACK_OF(OPENSSL_STRING) *X509_get1_ocsp(const X509 *x509); -// X509_email_free releases memory associated with |sk|, including |sk| itself. -// Each |OPENSSL_STRING| in |sk| must be a NUL-terminated string allocated with -// |OPENSSL_malloc|. If |sk| is NULL, no action is taken. +// X509_email_free releases memory associated with `sk`, including `sk` itself. +// Each `OPENSSL_STRING` in `sk` must be a NUL-terminated string allocated with +// `OPENSSL_malloc`. If `sk` is NULL, no action is taken. OPENSSL_EXPORT void X509_email_free(STACK_OF(OPENSSL_STRING) *sk); -// X509_cmp compares |a| and |b| and returns zero if they are equal, a negative -// number if |b| sorts after |a| and a negative number if |a| sorts after |b|. +// X509_cmp compares `a` and `b` and returns zero if they are equal, a negative +// number if `b` sorts after `a` and a negative number if `a` sorts after `b`. // The sort order implemented by this function is arbitrary and does not // reflect properties of the certificate such as expiry. Applications should not // rely on the order itself. // // TODO(https://crbug.com/boringssl/355): This function works by comparing a -// cached hash of the encoded certificate. If |a| or |b| could not be +// cached hash of the encoded certificate. If `a` or `b` could not be // serialized, the current behavior is to compare all unencodable certificates -// as equal. This function should only be used with |X509| objects that were +// as equal. This function should only be used with `X509` objects that were // parsed from bytes and never mutated. OPENSSL_EXPORT int X509_cmp(const X509 *a, const X509 *b); // Issuing certificates. // -// An |X509| object may also represent an incomplete certificate. Callers may -// construct empty |X509| objects, fill in fields individually, and finally sign +// An `X509` object may also represent an incomplete certificate. Callers may +// construct empty `X509` objects, fill in fields individually, and finally sign // the result. The following functions may be used for this purpose. -// X509_new returns a newly-allocated, empty |X509| object, or NULL on error. +// X509_new returns a newly-allocated, empty `X509` object, or NULL on error. // This produces an incomplete certificate which may be filled in to issue a new // certificate. OPENSSL_EXPORT X509 *X509_new(void); -// X509_set_version sets |x509|'s version to |version|, which should be one of -// the |X509V_VERSION_*| constants. It returns one on success and zero on error. +// X509_set_version sets `x509`'s version to `version`, which should be one of +// the `X509V_VERSION_*` constants. It returns one on success and zero on error. // -// If unsure, use |X509_VERSION_3|. +// If unsure, use `X509_VERSION_3`. OPENSSL_EXPORT int X509_set_version(X509 *x509, long version); -// X509_set_serialNumber sets |x509|'s serial number to |serial|. It returns one +// X509_set_serialNumber sets `x509`'s serial number to `serial`. It returns one // on success and zero on error. OPENSSL_EXPORT int X509_set_serialNumber(X509 *x509, const ASN1_INTEGER *serial); -// X509_set1_notBefore sets |x509|'s notBefore time to |tm|. It returns one on +// X509_set1_notBefore sets `x509`'s notBefore time to `tm`. It returns one on // success and zero on error. OPENSSL_EXPORT int X509_set1_notBefore(X509 *x509, const ASN1_TIME *tm); -// X509_set1_notAfter sets |x509|'s notAfter time to |tm|. it returns one on +// X509_set1_notAfter sets `x509`'s notAfter time to `tm`. it returns one on // success and zero on error. OPENSSL_EXPORT int X509_set1_notAfter(X509 *x509, const ASN1_TIME *tm); -// X509_getm_notBefore returns a mutable pointer to |x509|'s notBefore time. +// X509_getm_notBefore returns a mutable pointer to `x509`'s notBefore time. OPENSSL_EXPORT ASN1_TIME *X509_getm_notBefore(X509 *x509); -// X509_getm_notAfter returns a mutable pointer to |x509|'s notAfter time. +// X509_getm_notAfter returns a mutable pointer to `x509`'s notAfter time. OPENSSL_EXPORT ASN1_TIME *X509_getm_notAfter(X509 *x); -// X509_set_issuer_name sets |x509|'s issuer to a copy of |name|. It returns one +// X509_set_issuer_name sets `x509`'s issuer to a copy of `name`. It returns one // on success and zero on error. OPENSSL_EXPORT int X509_set_issuer_name(X509 *x509, const X509_NAME *name); -// X509_set_subject_name sets |x509|'s subject to a copy of |name|. It returns +// X509_set_subject_name sets `x509`'s subject to a copy of `name`. It returns // one on success and zero on error. OPENSSL_EXPORT int X509_set_subject_name(X509 *x509, const X509_NAME *name); -// X509_set_pubkey sets |x509|'s public key to |pkey|. It returns one on success -// and zero on error. This function does not take ownership of |pkey| and -// internally copies and updates reference counts as needed. -OPENSSL_EXPORT int X509_set_pubkey(X509 *x509, EVP_PKEY *pkey); +// X509_set_pubkey sets `x509`'s public key to `pkey`. It returns one on success +// and zero on error. +OPENSSL_EXPORT int X509_set_pubkey(X509 *x509, const EVP_PKEY *pkey); -// X509_delete_ext removes the extension in |x| at index |loc| and returns the -// removed extension, or NULL if |loc| was out of bounds. If non-NULL, the -// caller must release the result with |X509_EXTENSION_free|. +// X509_delete_ext removes the extension in `x` at index `loc` and returns the +// removed extension, or NULL if `loc` was out of bounds. If non-NULL, the +// caller must release the result with `X509_EXTENSION_free`. OPENSSL_EXPORT X509_EXTENSION *X509_delete_ext(X509 *x, int loc); -// X509_add_ext adds a copy of |ex| to |x|. It returns one on success and zero -// on failure. The caller retains ownership of |ex| and can release it -// independently of |x|. +// X509_add_ext adds a copy of `ex` to `x`. It returns one on success and zero +// on failure. The caller retains ownership of `ex` and can release it +// independently of `x`. // -// The new extension is inserted at index |loc|, shifting extensions to the -// right. If |loc| is -1 or out of bounds, the new extension is appended to the +// The new extension is inserted at index `loc`, shifting extensions to the +// right. If `loc` is -1 or out of bounds, the new extension is appended to the // list. OPENSSL_EXPORT int X509_add_ext(X509 *x, const X509_EXTENSION *ex, int loc); -// X509_add1_ext_i2d behaves like |X509V3_add1_i2d| but adds the extension to -// |x|'s extension list. +// X509_add1_ext_i2d behaves like `X509V3_add1_i2d` but adds the extension to +// `x`'s extension list. // // WARNING: This function may return zero or -1 on error. The caller must also -// ensure |value|'s type matches |nid|. See the documentation for -// |X509V3_add1_i2d| for details. +// ensure `value`'s type matches `nid`. See the documentation for +// `X509V3_add1_i2d` for details. OPENSSL_EXPORT int X509_add1_ext_i2d(X509 *x, int nid, void *value, int crit, unsigned long flags); -// X509_sign signs |x509| with |pkey| and replaces the signature algorithm and +// X509_sign signs `x509` with `pkey` and replaces the signature algorithm and // signature fields. It returns the length of the signature on success and zero -// on error. This function uses digest algorithm |md|, or |pkey|'s default if -// NULL. Other signing parameters use |pkey|'s defaults. To customize them, use -// |X509_sign_ctx|. -OPENSSL_EXPORT int X509_sign(X509 *x509, EVP_PKEY *pkey, const EVP_MD *md); +// on error. This function uses digest algorithm `md`, or `pkey`'s default if +// NULL. Other signing parameters use `pkey`'s defaults. To customize them, use +// `X509_sign_ctx`. +OPENSSL_EXPORT int X509_sign(X509 *x509, const EVP_PKEY *pkey, + const EVP_MD *md); -// X509_sign_ctx signs |x509| with |ctx| and replaces the signature algorithm +// X509_sign_ctx signs `x509` with `ctx` and replaces the signature algorithm // and signature fields. It returns the length of the signature on success and -// zero on error. The signature algorithm and parameters come from |ctx|, which -// must have been initialized with |EVP_DigestSignInit|. The caller should -// configure the corresponding |EVP_PKEY_CTX| before calling this function. +// zero on error. The signature algorithm and parameters come from `ctx`, which +// must have been initialized with `EVP_DigestSignInit`. The caller should +// configure the corresponding `EVP_PKEY_CTX` before calling this function. // -// On success or failure, this function mutates |ctx| and resets it to the empty +// On success or failure, this function mutates `ctx` and resets it to the empty // state. Caller should not rely on its contents after the function returns. OPENSSL_EXPORT int X509_sign_ctx(X509 *x509, EVP_MD_CTX *ctx); -// i2d_re_X509_tbs serializes the TBSCertificate portion of |x509|, as described -// in |i2d_SAMPLE|. +// i2d_re_X509_tbs serializes the TBSCertificate portion of `x509`, as described +// in `i2d_SAMPLE`. // -// This function re-encodes the TBSCertificate and may not reflect |x509|'s +// This function re-encodes the TBSCertificate and may not reflect `x509`'s // original encoding. It may be used to manually generate a signature for a new -// certificate. To verify certificates, use |i2d_X509_tbs| instead. +// certificate. To verify certificates, use `i2d_X509_tbs` instead. // -// Unlike |i2d_X509_tbs|, this function is not |const| and thus may not be to -// use concurrently with other functions that access |x509|. It mutates |x509| +// Unlike `i2d_X509_tbs`, this function is not `const` and thus may not be to +// use concurrently with other functions that access `x509`. It mutates `x509` // by dropping the cached encoding. This function is intended to be used during -// certificate construction, where |x509| is still single-threaded and being +// certificate construction, where `x509` is still single-threaded and being // mutated. OPENSSL_EXPORT int i2d_re_X509_tbs(X509 *x509, uint8_t **outp); -// X509_set1_signature_algo sets |x509|'s signature algorithm to |algo| and +// X509_set1_signature_algo sets `x509`'s signature algorithm to `algo` and // returns one on success or zero on error. It updates both the signature field // of the TBSCertificate structure, and the signatureAlgorithm field of the // Certificate. OPENSSL_EXPORT int X509_set1_signature_algo(X509 *x509, const X509_ALGOR *algo); -// X509_set1_signature_value sets |x509|'s signature to a copy of the |sig_len| -// bytes pointed by |sig|. It returns one on success and zero on error. +// X509_set1_signature_value sets `x509`'s signature to a copy of the `sig_len` +// bytes pointed by `sig`. It returns one on success and zero on error. // // Due to a specification error, X.509 certificates store signatures in ASN.1 // BIT STRINGs, but signature algorithms return byte strings rather than bit @@ -564,29 +567,29 @@ OPENSSL_EXPORT int X509_set1_signature_value(X509 *x509, const uint8_t *sig, // Auxiliary certificate properties. // -// |X509| objects optionally maintain auxiliary properties. These are not part +// `X509` objects optionally maintain auxiliary properties. These are not part // of the certificates themselves, and thus are not covered by signatures or // preserved by the standard serialization. They are used as inputs or outputs // to other functions in this library. -// i2d_X509_AUX marshals |x509| as a DER-encoded X.509 Certificate (RFC 5280), +// i2d_X509_AUX marshals `x509` as a DER-encoded X.509 Certificate (RFC 5280), // followed optionally by a separate, OpenSSL-specific structure with auxiliary -// properties. It behaves as described in |i2d_SAMPLE|. +// properties. It behaves as described in `i2d_SAMPLE`. // // Unlike similarly-named functions, this function does not output a single // ASN.1 element. Directly embedding the output in a larger ASN.1 structure will // not behave correctly. OPENSSL_EXPORT int i2d_X509_AUX(const X509 *x509, uint8_t **outp); -// d2i_X509_AUX parses up to |length| bytes from |*inp| as a DER-encoded X.509 +// d2i_X509_AUX parses up to `length` bytes from `*inp` as a DER-encoded X.509 // Certificate (RFC 5280), followed optionally by a separate, OpenSSL-specific -// structure with auxiliary properties. It behaves as described in |d2i_SAMPLE|. +// structure with auxiliary properties. It behaves as described in `d2i_SAMPLE`. // // WARNING: Passing untrusted input to this function allows an attacker to // control auxiliary properties. This can allow unexpected influence over the // application if the certificate is used in a context that reads auxiliary // properties. This includes PKCS#12 serialization, trusted certificates in -// |X509_STORE|, and callers of |X509_alias_get0| or |X509_keyid_get0|. +// `X509_STORE`, and callers of `X509_alias_get0` or `X509_keyid_get0`. // // Unlike similarly-named functions, this function does not parse a single // ASN.1 element. Trying to parse data directly embedded in a larger ASN.1 @@ -594,82 +597,82 @@ OPENSSL_EXPORT int i2d_X509_AUX(const X509 *x509, uint8_t **outp); OPENSSL_EXPORT X509 *d2i_X509_AUX(X509 **x509, const uint8_t **inp, long length); -// X509_alias_set1 sets |x509|'s alias to |len| bytes from |name|. If |name| is +// X509_alias_set1 sets `x509`'s alias to `len` bytes from `name`. If `name` is // NULL, the alias is cleared instead. Aliases are not part of the certificate -// itself and will not be serialized by |i2d_X509|. If |x509| is serialized in +// itself and will not be serialized by `i2d_X509`. If `x509` is serialized in // a PKCS#12 structure, the friendlyName attribute (RFC 2985) will contain this // alias. OPENSSL_EXPORT int X509_alias_set1(X509 *x509, const uint8_t *name, ossl_ssize_t len); -// X509_keyid_set1 sets |x509|'s key ID to |len| bytes from |id|. If |id| is +// X509_keyid_set1 sets `x509`'s key ID to `len` bytes from `id`. If `id` is // NULL, the key ID is cleared instead. Key IDs are not part of the certificate -// itself and will not be serialized by |i2d_X509|. +// itself and will not be serialized by `i2d_X509`. OPENSSL_EXPORT int X509_keyid_set1(X509 *x509, const uint8_t *id, ossl_ssize_t len); -// X509_alias_get0 looks up |x509|'s alias. If found, it sets |*out_len| to the +// X509_alias_get0 looks up `x509`'s alias. If found, it sets `*out_len` to the // alias's length and returns a pointer to a buffer containing the contents. If // not found, it outputs the empty string by returning NULL and setting -// |*out_len| to zero. +// `*out_len` to zero. // -// If |x509| was parsed from a PKCS#12 structure (see -// |PKCS12_get_key_and_certs|), the alias will reflect the friendlyName +// If `x509` was parsed from a PKCS#12 structure (see +// `PKCS12_get_key_and_certs`), the alias will reflect the friendlyName // attribute (RFC 2985). // -// WARNING: In OpenSSL, this function did not set |*out_len| when the alias was +// WARNING: In OpenSSL, this function did not set `*out_len` when the alias was // missing. Callers that target both OpenSSL and BoringSSL should set the value // to zero before calling this function. OPENSSL_EXPORT const uint8_t *X509_alias_get0(const X509 *x509, int *out_len); -// X509_keyid_get0 looks up |x509|'s key ID. If found, it sets |*out_len| to the +// X509_keyid_get0 looks up `x509`'s key ID. If found, it sets `*out_len` to the // key ID's length and returns a pointer to a buffer containing the contents. If // not found, it outputs the empty string by returning NULL and setting -// |*out_len| to zero. +// `*out_len` to zero. // -// WARNING: In OpenSSL, this function did not set |*out_len| when the alias was +// WARNING: In OpenSSL, this function did not set `*out_len` when the alias was // missing. Callers that target both OpenSSL and BoringSSL should set the value // to zero before calling this function. OPENSSL_EXPORT const uint8_t *X509_keyid_get0(const X509 *x509, int *out_len); -// X509_add1_trust_object configures |x509| as a valid trust anchor for |obj|. -// It returns one on success and zero on error. |obj| should be a certificate -// usage OID associated with an |X509_TRUST_*| constant. +// X509_add1_trust_object configures `x509` as a valid trust anchor for `obj`. +// It returns one on success and zero on error. `obj` should be a certificate +// usage OID associated with an `X509_TRUST_*` constant. // -// See |X509_VERIFY_PARAM_set_trust| for details on how this value is evaluated. -// Note this only takes effect if |x509| was configured as a trusted certificate -// via |X509_STORE|. +// See `X509_VERIFY_PARAM_set_trust` for details on how this value is evaluated. +// Note this only takes effect if `x509` was configured as a trusted certificate +// via `X509_STORE`. OPENSSL_EXPORT int X509_add1_trust_object(X509 *x509, const ASN1_OBJECT *obj); -// X509_add1_reject_object configures |x509| as distrusted for |obj|. It returns -// one on success and zero on error. |obj| should be a certificate usage OID -// associated with an |X509_TRUST_*| constant. +// X509_add1_reject_object configures `x509` as distrusted for `obj`. It returns +// one on success and zero on error. `obj` should be a certificate usage OID +// associated with an `X509_TRUST_*` constant. // -// See |X509_VERIFY_PARAM_set_trust| for details on how this value is evaluated. -// Note this only takes effect if |x509| was configured as a trusted certificate -// via |X509_STORE|. +// See `X509_VERIFY_PARAM_set_trust` for details on how this value is evaluated. +// Note this only takes effect if `x509` was configured as a trusted certificate +// via `X509_STORE`. OPENSSL_EXPORT int X509_add1_reject_object(X509 *x509, const ASN1_OBJECT *obj); -// X509_trust_clear clears the list of OIDs for which |x509| is trusted. See -// also |X509_add1_trust_object|. +// X509_trust_clear clears the list of OIDs for which `x509` is trusted. See +// also `X509_add1_trust_object`. OPENSSL_EXPORT void X509_trust_clear(X509 *x509); -// X509_reject_clear clears the list of OIDs for which |x509| is distrusted. See -// also |X509_add1_reject_object|. +// X509_reject_clear clears the list of OIDs for which `x509` is distrusted. See +// also `X509_add1_reject_object`. OPENSSL_EXPORT void X509_reject_clear(X509 *x509); // Certificate revocation lists. // -// An |X509_CRL| object represents an X.509 certificate revocation list (CRL), +// An `X509_CRL` object represents an X.509 certificate revocation list (CRL), // defined in RFC 5280. A CRL is a signed list of certificates, the // revokedCertificates field, which are no longer considered valid. Each entry -// of this list is represented with an |X509_REVOKED| object, documented in the +// of this list is represented with an `X509_REVOKED` object, documented in the // "CRL entries" section below. // -// Although an |X509_CRL| is a mutable object, mutating an |X509_CRL| or its -// |X509_REVOKED|s can give incorrect results. Callers typically obtain -// |X509_CRL|s by parsing some input with |d2i_X509_CRL|, etc. Such objects +// Although an `X509_CRL` is a mutable object, mutating an `X509_CRL` or its +// `X509_REVOKED`s can give incorrect results. Callers typically obtain +// `X509_CRL`s by parsing some input with `d2i_X509_CRL`, etc. Such objects // carry information such as the serialized TBSCertList and decoded extensions, // which will become inconsistent when mutated. // @@ -679,128 +682,132 @@ OPENSSL_EXPORT void X509_reject_clear(X509 *x509); DEFINE_STACK_OF(X509_CRL) DEFINE_STACK_OF(X509_REVOKED) -// X509_CRL_up_ref adds one to the reference count of |crl| and returns one. +// X509_CRL_up_ref adds one to the reference count of `crl` and returns one. OPENSSL_EXPORT int X509_CRL_up_ref(X509_CRL *crl); -// X509_CRL_dup returns a newly-allocated copy of |crl|, or NULL on error. This -// function works by serializing the structure, so if |crl| is incomplete, it +// X509_CRL_dup_ref adds one to the reference count of `crl` and returns the +// same. +OPENSSL_EXPORT X509_CRL *X509_CRL_dup_ref(const X509_CRL *crl); + +// X509_CRL_dup returns a newly-allocated copy of `crl`, or NULL on error. This +// function works by serializing the structure, so if `crl` is incomplete, it // may fail. OPENSSL_EXPORT X509_CRL *X509_CRL_dup(const X509_CRL *crl); -// X509_CRL_free decrements |crl|'s reference count and, if zero, releases -// memory associated with |crl|. +// X509_CRL_free decrements `crl`'s reference count and, if zero, releases +// memory associated with `crl`. OPENSSL_EXPORT void X509_CRL_free(X509_CRL *crl); -// d2i_X509_CRL parses up to |len| bytes from |*inp| as a DER-encoded X.509 -// CertificateList (RFC 5280), as described in |d2i_SAMPLE|. +// d2i_X509_CRL parses up to `len` bytes from `*inp` as a DER-encoded X.509 +// CertificateList (RFC 5280), as described in `d2i_SAMPLE`. OPENSSL_EXPORT X509_CRL *d2i_X509_CRL(X509_CRL **out, const uint8_t **inp, long len); -// i2d_X509_CRL marshals |crl| as a X.509 CertificateList (RFC 5280), as -// described in |i2d_SAMPLE|. +// i2d_X509_CRL marshals `crl` as a X.509 CertificateList (RFC 5280), as +// described in `i2d_SAMPLE`. OPENSSL_EXPORT int i2d_X509_CRL(const X509_CRL *crl, uint8_t **outp); -// X509_CRL_match compares |a| and |b| and returns zero if they are equal, a -// negative number if |b| sorts after |a| and a negative number if |a| sorts -// after |b|. The sort order implemented by this function is arbitrary and does +// X509_CRL_match compares `a` and `b` and returns zero if they are equal, a +// negative number if `b` sorts after `a` and a negative number if `a` sorts +// after `b`. The sort order implemented by this function is arbitrary and does // not reflect properties of the CRL such as expiry. Applications should not // rely on the order itself. // // TODO(https://crbug.com/boringssl/355): This function works by comparing a // cached hash of the encoded CRL. This cached hash is computed when the CRL is // parsed, but not when mutating or issuing CRLs. This function should only be -// used with |X509_CRL| objects that were parsed from bytes and never mutated. +// used with `X509_CRL` objects that were parsed from bytes and never mutated. OPENSSL_EXPORT int X509_CRL_match(const X509_CRL *a, const X509_CRL *b); #define X509_CRL_VERSION_1 0 #define X509_CRL_VERSION_2 1 -// X509_CRL_get_version returns the numerical value of |crl|'s version, which -// will be one of the |X509_CRL_VERSION_*| constants. +// X509_CRL_get_version returns the numerical value of `crl`'s version, which +// will be one of the `X509_CRL_VERSION_*` constants. OPENSSL_EXPORT long X509_CRL_get_version(const X509_CRL *crl); -// X509_CRL_get0_lastUpdate returns |crl|'s thisUpdate time. The OpenSSL API +// X509_CRL_get0_lastUpdate returns `crl`'s thisUpdate time. The OpenSSL API // refers to this field as lastUpdate. OPENSSL_EXPORT const ASN1_TIME *X509_CRL_get0_lastUpdate(const X509_CRL *crl); -// X509_CRL_get0_nextUpdate returns |crl|'s nextUpdate time, or NULL if |crl| +// X509_CRL_get0_nextUpdate returns `crl`'s nextUpdate time, or NULL if `crl` // has none. OPENSSL_EXPORT const ASN1_TIME *X509_CRL_get0_nextUpdate(const X509_CRL *crl); -// X509_CRL_get_issuer returns |crl|'s issuer name. Note this function is not +// X509_CRL_get_issuer returns `crl`'s issuer name. Note this function is not // const-correct for legacy reasons. OPENSSL_EXPORT X509_NAME *X509_CRL_get_issuer(const X509_CRL *crl); -// X509_CRL_get0_by_serial finds the entry in |crl| whose serial number is -// |serial|. If found, it sets |*out| to the entry and returns one. If not +// X509_CRL_get0_by_serial finds the entry in `crl` whose serial number is +// `serial`. If found, it sets `*out` to the entry and returns one. If not // found, it returns zero. // -// On success, |*out| continues to be owned by |crl|. It is an error to free or -// otherwise modify |*out|. +// On success, `*out` continues to be owned by `crl`. It is an error to free or +// otherwise modify `*out`. // -// TODO(crbug.com/42290473): Ideally |crl| would be const. It is broadly -// thread-safe, but changes the order of entries in |crl|. It cannot be called -// concurrently with |i2d_X509_CRL|. +// TODO(crbug.com/42290473): Ideally `crl` would be const. It is broadly +// thread-safe, but changes the order of entries in `crl`. It cannot be called +// concurrently with `i2d_X509_CRL`. OPENSSL_EXPORT int X509_CRL_get0_by_serial(X509_CRL *crl, X509_REVOKED **out, const ASN1_INTEGER *serial); -// X509_CRL_get0_by_cert behaves like |X509_CRL_get0_by_serial|, except it looks -// for the entry that matches |x509|. +// X509_CRL_get0_by_cert behaves like `X509_CRL_get0_by_serial`, except it looks +// for the entry that matches `x509`. // -// TODO(crbug.com/42290473): Ideally |crl| would be const. It is broadly -// thread-safe, but changes the order of entries in |crl|. It cannot be called -// concurrently with |i2d_X509_CRL|. +// TODO(crbug.com/42290473): Ideally `crl` would be const. It is broadly +// thread-safe, but changes the order of entries in `crl`. It cannot be called +// concurrently with `i2d_X509_CRL`. OPENSSL_EXPORT int X509_CRL_get0_by_cert(X509_CRL *crl, X509_REVOKED **out, const X509 *x509); -// X509_CRL_get_REVOKED returns the list of revoked certificates in |crl|, or -// NULL if |crl| omits it. +// X509_CRL_get_REVOKED returns the list of revoked certificates in `crl`, or +// NULL if `crl` omits it. // // TODO(davidben): This function was originally a macro, without clear const // semantics. It should take a const input and give const output, but the latter // would break existing callers. For now, we match upstream. OPENSSL_EXPORT STACK_OF(X509_REVOKED) *X509_CRL_get_REVOKED(X509_CRL *crl); -// X509_CRL_get0_extensions returns |crl|'s extension list, or NULL if |crl| +// X509_CRL_get0_extensions returns `crl`'s extension list, or NULL if `crl` // omits it. A CRL can have extensions on individual entries, which is -// |X509_REVOKED_get0_extensions|, or on the overall CRL, which is this +// `X509_REVOKED_get0_extensions`, or on the overall CRL, which is this // function. OPENSSL_EXPORT const STACK_OF(X509_EXTENSION) *X509_CRL_get0_extensions( const X509_CRL *crl); -// X509_CRL_get_ext_count returns the number of extensions in |x|. +// X509_CRL_get_ext_count returns the number of extensions in `x`. OPENSSL_EXPORT int X509_CRL_get_ext_count(const X509_CRL *x); -// X509_CRL_get_ext_by_NID behaves like |X509v3_get_ext_by_NID| but searches for -// extensions in |x|. +// X509_CRL_get_ext_by_NID behaves like `X509v3_get_ext_by_NID` but searches for +// extensions in `x`. OPENSSL_EXPORT int X509_CRL_get_ext_by_NID(const X509_CRL *x, int nid, int lastpos); -// X509_CRL_get_ext_by_OBJ behaves like |X509v3_get_ext_by_OBJ| but searches for -// extensions in |x|. +// X509_CRL_get_ext_by_OBJ behaves like `X509v3_get_ext_by_OBJ` but searches for +// extensions in `x`. OPENSSL_EXPORT int X509_CRL_get_ext_by_OBJ(const X509_CRL *x, const ASN1_OBJECT *obj, int lastpos); -// X509_CRL_get_ext_by_critical behaves like |X509v3_get_ext_by_critical| but -// searches for extensions in |x|. +// X509_CRL_get_ext_by_critical behaves like `X509v3_get_ext_by_critical` but +// searches for extensions in `x`. OPENSSL_EXPORT int X509_CRL_get_ext_by_critical(const X509_CRL *x, int crit, int lastpos); -// X509_CRL_get_ext returns the extension in |x| at index |loc|, or NULL if -// |loc| is out of bounds. This function returns a non-const pointer for OpenSSL +// X509_CRL_get_ext returns the extension in `x` at index `loc`, or NULL if +// `loc` is out of bounds. This function returns a non-const pointer for OpenSSL // compatibility, but callers should not mutate the result. OPENSSL_EXPORT X509_EXTENSION *X509_CRL_get_ext(const X509_CRL *x, int loc); -// X509_CRL_get_ext_d2i behaves like |X509V3_get_d2i| but looks for the -// extension in |crl|'s extension list. +// X509_CRL_get_ext_d2i behaves like `X509V3_get_d2i` but looks for the +// extension in `crl`'s extension list. // // WARNING: This function is difficult to use correctly. See the documentation -// for |X509V3_get_d2i| for details. +// for `X509V3_get_d2i` for details. OPENSSL_EXPORT void *X509_CRL_get_ext_d2i(const X509_CRL *crl, int nid, int *out_critical, int *out_idx); -// X509_CRL_get0_signature sets |*out_sig| and |*out_alg| to the signature and -// signature algorithm of |crl|, respectively. Either output pointer may be NULL +// X509_CRL_get0_signature sets `*out_sig` and `*out_alg` to the signature and +// signature algorithm of `crl`, respectively. Either output pointer may be NULL // to ignore the value. // // This function outputs the outer signature algorithm, not the one in the @@ -810,124 +817,124 @@ OPENSSL_EXPORT void X509_CRL_get0_signature(const X509_CRL *crl, const ASN1_BIT_STRING **out_sig, const X509_ALGOR **out_alg); -// X509_CRL_get_signature_nid returns the NID corresponding to |crl|'s signature -// algorithm, or |NID_undef| if the signature algorithm does not correspond to +// X509_CRL_get_signature_nid returns the NID corresponding to `crl`'s signature +// algorithm, or `NID_undef` if the signature algorithm does not correspond to // a known NID. OPENSSL_EXPORT int X509_CRL_get_signature_nid(const X509_CRL *crl); -// i2d_X509_CRL_tbs serializes the TBSCertList portion of |crl|, as described in -// |i2d_SAMPLE|. +// i2d_X509_CRL_tbs serializes the TBSCertList portion of `crl`, as described in +// `i2d_SAMPLE`. // // This function preserves the original encoding of the TBSCertList and may not -// reflect modifications made to |crl|. It may be used to manually verify the -// signature of an existing CRL. To generate CRLs, use |i2d_re_X509_CRL_tbs| +// reflect modifications made to `crl`. It may be used to manually verify the +// signature of an existing CRL. To generate CRLs, use `i2d_re_X509_CRL_tbs` // instead. OPENSSL_EXPORT int i2d_X509_CRL_tbs(const X509_CRL *crl, unsigned char **outp); -// X509_CRL_verify checks that |crl| has a valid signature by |pkey|. It returns +// X509_CRL_verify checks that `crl` has a valid signature by `pkey`. It returns // one if the signature is valid and zero otherwise. -OPENSSL_EXPORT int X509_CRL_verify(const X509_CRL *crl, EVP_PKEY *pkey); +OPENSSL_EXPORT int X509_CRL_verify(const X509_CRL *crl, const EVP_PKEY *pkey); // Issuing certificate revocation lists. // -// An |X509_CRL| object may also represent an incomplete CRL. Callers may -// construct empty |X509_CRL| objects, fill in fields individually, and finally +// An `X509_CRL` object may also represent an incomplete CRL. Callers may +// construct empty `X509_CRL` objects, fill in fields individually, and finally // sign the result. The following functions may be used for this purpose. -// X509_CRL_new returns a newly-allocated, empty |X509_CRL| object, or NULL on +// X509_CRL_new returns a newly-allocated, empty `X509_CRL` object, or NULL on // error. This object may be filled in and then signed to construct a CRL. OPENSSL_EXPORT X509_CRL *X509_CRL_new(void); -// X509_CRL_set_version sets |crl|'s version to |version|, which should be one -// of the |X509_CRL_VERSION_*| constants. It returns one on success and zero on +// X509_CRL_set_version sets `crl`'s version to `version`, which should be one +// of the `X509_CRL_VERSION_*` constants. It returns one on success and zero on // error. // -// If unsure, use |X509_CRL_VERSION_2|. Note that, unlike certificates, CRL -// versions are only defined up to v2. Callers should not use |X509_VERSION_3|. +// If unsure, use `X509_CRL_VERSION_2`. Note that, unlike certificates, CRL +// versions are only defined up to v2. Callers should not use `X509_VERSION_3`. OPENSSL_EXPORT int X509_CRL_set_version(X509_CRL *crl, long version); -// X509_CRL_set_issuer_name sets |crl|'s issuer to a copy of |name|. It returns +// X509_CRL_set_issuer_name sets `crl`'s issuer to a copy of `name`. It returns // one on success and zero on error. OPENSSL_EXPORT int X509_CRL_set_issuer_name(X509_CRL *crl, const X509_NAME *name); -// X509_CRL_set1_lastUpdate sets |crl|'s thisUpdate time to |tm|. It returns one +// X509_CRL_set1_lastUpdate sets `crl`'s thisUpdate time to `tm`. It returns one // on success and zero on error. The OpenSSL API refers to this field as // lastUpdate. OPENSSL_EXPORT int X509_CRL_set1_lastUpdate(X509_CRL *crl, const ASN1_TIME *tm); -// X509_CRL_set1_nextUpdate sets |crl|'s nextUpdate time to |tm|. It returns one +// X509_CRL_set1_nextUpdate sets `crl`'s nextUpdate time to `tm`. It returns one // on success and zero on error. OPENSSL_EXPORT int X509_CRL_set1_nextUpdate(X509_CRL *crl, const ASN1_TIME *tm); -// X509_CRL_add0_revoked adds |rev| to |crl|. On success, it takes ownership of -// |rev| and returns one. On error, it returns zero. If this function fails, the -// caller retains ownership of |rev| and must release it when done. +// X509_CRL_add0_revoked adds `rev` to `crl`. On success, it takes ownership of +// `rev` and returns one. On error, it returns zero. If this function fails, the +// caller retains ownership of `rev` and must release it when done. OPENSSL_EXPORT int X509_CRL_add0_revoked(X509_CRL *crl, X509_REVOKED *rev); -// X509_CRL_sort sorts the entries in |crl| by serial number. It returns one on +// X509_CRL_sort sorts the entries in `crl` by serial number. It returns one on // success and zero on error. OPENSSL_EXPORT int X509_CRL_sort(X509_CRL *crl); -// X509_CRL_delete_ext removes the extension in |x| at index |loc| and returns -// the removed extension, or NULL if |loc| was out of bounds. If non-NULL, the -// caller must release the result with |X509_EXTENSION_free|. +// X509_CRL_delete_ext removes the extension in `x` at index `loc` and returns +// the removed extension, or NULL if `loc` was out of bounds. If non-NULL, the +// caller must release the result with `X509_EXTENSION_free`. OPENSSL_EXPORT X509_EXTENSION *X509_CRL_delete_ext(X509_CRL *x, int loc); -// X509_CRL_add_ext adds a copy of |ex| to |x|. It returns one on success and -// zero on failure. The caller retains ownership of |ex| and can release it -// independently of |x|. +// X509_CRL_add_ext adds a copy of `ex` to `x`. It returns one on success and +// zero on failure. The caller retains ownership of `ex` and can release it +// independently of `x`. // -// The new extension is inserted at index |loc|, shifting extensions to the -// right. If |loc| is -1 or out of bounds, the new extension is appended to the +// The new extension is inserted at index `loc`, shifting extensions to the +// right. If `loc` is -1 or out of bounds, the new extension is appended to the // list. OPENSSL_EXPORT int X509_CRL_add_ext(X509_CRL *x, const X509_EXTENSION *ex, int loc); -// X509_CRL_add1_ext_i2d behaves like |X509V3_add1_i2d| but adds the extension -// to |x|'s extension list. +// X509_CRL_add1_ext_i2d behaves like `X509V3_add1_i2d` but adds the extension +// to `x`'s extension list. // // WARNING: This function may return zero or -1 on error. The caller must also -// ensure |value|'s type matches |nid|. See the documentation for -// |X509V3_add1_i2d| for details. +// ensure `value`'s type matches `nid`. See the documentation for +// `X509V3_add1_i2d` for details. OPENSSL_EXPORT int X509_CRL_add1_ext_i2d(X509_CRL *x, int nid, void *value, int crit, unsigned long flags); -// X509_CRL_sign signs |crl| with |pkey| and replaces the signature algorithm +// X509_CRL_sign signs `crl` with `pkey` and replaces the signature algorithm // and signature fields. It returns the length of the signature on success and -// zero on error. This function uses digest algorithm |md|, or |pkey|'s default -// if NULL. Other signing parameters use |pkey|'s defaults. To customize them, -// use |X509_CRL_sign_ctx|. -OPENSSL_EXPORT int X509_CRL_sign(X509_CRL *crl, EVP_PKEY *pkey, +// zero on error. This function uses digest algorithm `md`, or `pkey`'s default +// if NULL. Other signing parameters use `pkey`'s defaults. To customize them, +// use `X509_CRL_sign_ctx`. +OPENSSL_EXPORT int X509_CRL_sign(X509_CRL *crl, const EVP_PKEY *pkey, const EVP_MD *md); -// X509_CRL_sign_ctx signs |crl| with |ctx| and replaces the signature algorithm +// X509_CRL_sign_ctx signs `crl` with `ctx` and replaces the signature algorithm // and signature fields. It returns the length of the signature on success and -// zero on error. The signature algorithm and parameters come from |ctx|, which -// must have been initialized with |EVP_DigestSignInit|. The caller should -// configure the corresponding |EVP_PKEY_CTX| before calling this function. +// zero on error. The signature algorithm and parameters come from `ctx`, which +// must have been initialized with `EVP_DigestSignInit`. The caller should +// configure the corresponding `EVP_PKEY_CTX` before calling this function. // -// On success or failure, this function mutates |ctx| and resets it to the empty +// On success or failure, this function mutates `ctx` and resets it to the empty // state. Caller should not rely on its contents after the function returns. OPENSSL_EXPORT int X509_CRL_sign_ctx(X509_CRL *crl, EVP_MD_CTX *ctx); -// i2d_re_X509_CRL_tbs serializes the TBSCertList portion of |crl|, as described -// in |i2d_SAMPLE|. +// i2d_re_X509_CRL_tbs serializes the TBSCertList portion of `crl`, as described +// in `i2d_SAMPLE`. // -// This function re-encodes the TBSCertList and may not reflect |crl|'s original +// This function re-encodes the TBSCertList and may not reflect `crl`'s original // encoding. It may be used to manually generate a signature for a new CRL. To -// verify CRLs, use |i2d_X509_CRL_tbs| instead. +// verify CRLs, use `i2d_X509_CRL_tbs` instead. OPENSSL_EXPORT int i2d_re_X509_CRL_tbs(X509_CRL *crl, unsigned char **outp); -// X509_CRL_set1_signature_algo sets |crl|'s signature algorithm to |algo| and +// X509_CRL_set1_signature_algo sets `crl`'s signature algorithm to `algo` and // returns one on success or zero on error. It updates both the signature field // of the TBSCertList structure, and the signatureAlgorithm field of the CRL. OPENSSL_EXPORT int X509_CRL_set1_signature_algo(X509_CRL *crl, const X509_ALGOR *algo); -// X509_CRL_set1_signature_value sets |crl|'s signature to a copy of the -// |sig_len| bytes pointed by |sig|. It returns one on success and zero on +// X509_CRL_set1_signature_value sets `crl`'s signature to a copy of the +// `sig_len` bytes pointed by `sig`. It returns one on success and zero on // error. // // Due to a specification error, X.509 CRLs store signatures in ASN.1 BIT @@ -942,116 +949,116 @@ OPENSSL_EXPORT int X509_CRL_set1_signature_value(X509_CRL *crl, // CRL entries. // -// Each entry of a CRL is represented as an |X509_REVOKED| object, which +// Each entry of a CRL is represented as an `X509_REVOKED` object, which // describes a revoked certificate by serial number. // -// When an |X509_REVOKED| is obtained from an |X509_CRL| object, it is an error -// to mutate the object. Doing so may break |X509_CRL|'s and cause the library +// When an `X509_REVOKED` is obtained from an `X509_CRL` object, it is an error +// to mutate the object. Doing so may break `X509_CRL`'s and cause the library // to behave incorrectly. -// X509_REVOKED_new returns a newly-allocated, empty |X509_REVOKED| object, or +// X509_REVOKED_new returns a newly-allocated, empty `X509_REVOKED` object, or // NULL on allocation error. OPENSSL_EXPORT X509_REVOKED *X509_REVOKED_new(void); -// X509_REVOKED_free releases memory associated with |rev|. +// X509_REVOKED_free releases memory associated with `rev`. OPENSSL_EXPORT void X509_REVOKED_free(X509_REVOKED *rev); -// d2i_X509_REVOKED parses up to |len| bytes from |*inp| as a DER-encoded X.509 -// CRL entry, as described in |d2i_SAMPLE|. +// d2i_X509_REVOKED parses up to `len` bytes from `*inp` as a DER-encoded X.509 +// CRL entry, as described in `d2i_SAMPLE`. OPENSSL_EXPORT X509_REVOKED *d2i_X509_REVOKED(X509_REVOKED **out, const uint8_t **inp, long len); -// i2d_X509_REVOKED marshals |alg| as a DER-encoded X.509 CRL entry, as -// described in |i2d_SAMPLE|. +// i2d_X509_REVOKED marshals `alg` as a DER-encoded X.509 CRL entry, as +// described in `i2d_SAMPLE`. OPENSSL_EXPORT int i2d_X509_REVOKED(const X509_REVOKED *alg, uint8_t **outp); -// X509_REVOKED_dup returns a newly-allocated copy of |rev|, or NULL on error. -// This function works by serializing the structure, so if |rev| is incomplete, +// X509_REVOKED_dup returns a newly-allocated copy of `rev`, or NULL on error. +// This function works by serializing the structure, so if `rev` is incomplete, // it may fail. OPENSSL_EXPORT X509_REVOKED *X509_REVOKED_dup(const X509_REVOKED *rev); // X509_REVOKED_get0_serialNumber returns the serial number of the certificate -// revoked by |revoked|. +// revoked by `revoked`. OPENSSL_EXPORT const ASN1_INTEGER *X509_REVOKED_get0_serialNumber( const X509_REVOKED *revoked); -// X509_REVOKED_set_serialNumber sets |revoked|'s serial number to |serial|. It +// X509_REVOKED_set_serialNumber sets `revoked`'s serial number to `serial`. It // returns one on success or zero on error. OPENSSL_EXPORT int X509_REVOKED_set_serialNumber(X509_REVOKED *revoked, const ASN1_INTEGER *serial); // X509_REVOKED_get0_revocationDate returns the revocation time of the -// certificate revoked by |revoked|. +// certificate revoked by `revoked`. OPENSSL_EXPORT const ASN1_TIME *X509_REVOKED_get0_revocationDate( const X509_REVOKED *revoked); -// X509_REVOKED_set_revocationDate sets |revoked|'s revocation time to |tm|. It +// X509_REVOKED_set_revocationDate sets `revoked`'s revocation time to `tm`. It // returns one on success or zero on error. OPENSSL_EXPORT int X509_REVOKED_set_revocationDate(X509_REVOKED *revoked, const ASN1_TIME *tm); -// X509_REVOKED_get0_extensions returns |r|'s extensions list, or NULL if |r| +// X509_REVOKED_get0_extensions returns `r`'s extensions list, or NULL if `r` // omits it. A CRL can have extensions on individual entries, which is this -// function, or on the overall CRL, which is |X509_CRL_get0_extensions|. +// function, or on the overall CRL, which is `X509_CRL_get0_extensions`. OPENSSL_EXPORT const STACK_OF(X509_EXTENSION) *X509_REVOKED_get0_extensions( const X509_REVOKED *r); -// X509_REVOKED_get_ext_count returns the number of extensions in |x|. +// X509_REVOKED_get_ext_count returns the number of extensions in `x`. OPENSSL_EXPORT int X509_REVOKED_get_ext_count(const X509_REVOKED *x); -// X509_REVOKED_get_ext_by_NID behaves like |X509v3_get_ext_by_NID| but searches -// for extensions in |x|. +// X509_REVOKED_get_ext_by_NID behaves like `X509v3_get_ext_by_NID` but searches +// for extensions in `x`. OPENSSL_EXPORT int X509_REVOKED_get_ext_by_NID(const X509_REVOKED *x, int nid, int lastpos); -// X509_REVOKED_get_ext_by_OBJ behaves like |X509v3_get_ext_by_OBJ| but searches -// for extensions in |x|. +// X509_REVOKED_get_ext_by_OBJ behaves like `X509v3_get_ext_by_OBJ` but searches +// for extensions in `x`. OPENSSL_EXPORT int X509_REVOKED_get_ext_by_OBJ(const X509_REVOKED *x, const ASN1_OBJECT *obj, int lastpos); -// X509_REVOKED_get_ext_by_critical behaves like |X509v3_get_ext_by_critical| -// but searches for extensions in |x|. +// X509_REVOKED_get_ext_by_critical behaves like `X509v3_get_ext_by_critical` +// but searches for extensions in `x`. OPENSSL_EXPORT int X509_REVOKED_get_ext_by_critical(const X509_REVOKED *x, int crit, int lastpos); -// X509_REVOKED_get_ext returns the extension in |x| at index |loc|, or NULL if -// |loc| is out of bounds. This function returns a non-const pointer for OpenSSL +// X509_REVOKED_get_ext returns the extension in `x` at index `loc`, or NULL if +// `loc` is out of bounds. This function returns a non-const pointer for OpenSSL // compatibility, but callers should not mutate the result. OPENSSL_EXPORT X509_EXTENSION *X509_REVOKED_get_ext(const X509_REVOKED *x, int loc); -// X509_REVOKED_delete_ext removes the extension in |x| at index |loc| and -// returns the removed extension, or NULL if |loc| was out of bounds. If -// non-NULL, the caller must release the result with |X509_EXTENSION_free|. +// X509_REVOKED_delete_ext removes the extension in `x` at index `loc` and +// returns the removed extension, or NULL if `loc` was out of bounds. If +// non-NULL, the caller must release the result with `X509_EXTENSION_free`. OPENSSL_EXPORT X509_EXTENSION *X509_REVOKED_delete_ext(X509_REVOKED *x, int loc); -// X509_REVOKED_add_ext adds a copy of |ex| to |x|. It returns one on success -// and zero on failure. The caller retains ownership of |ex| and can release it -// independently of |x|. +// X509_REVOKED_add_ext adds a copy of `ex` to `x`. It returns one on success +// and zero on failure. The caller retains ownership of `ex` and can release it +// independently of `x`. // -// The new extension is inserted at index |loc|, shifting extensions to the -// right. If |loc| is -1 or out of bounds, the new extension is appended to the +// The new extension is inserted at index `loc`, shifting extensions to the +// right. If `loc` is -1 or out of bounds, the new extension is appended to the // list. OPENSSL_EXPORT int X509_REVOKED_add_ext(X509_REVOKED *x, const X509_EXTENSION *ex, int loc); -// X509_REVOKED_get_ext_d2i behaves like |X509V3_get_d2i| but looks for the -// extension in |revoked|'s extension list. +// X509_REVOKED_get_ext_d2i behaves like `X509V3_get_d2i` but looks for the +// extension in `revoked`'s extension list. // // WARNING: This function is difficult to use correctly. See the documentation -// for |X509V3_get_d2i| for details. +// for `X509V3_get_d2i` for details. OPENSSL_EXPORT void *X509_REVOKED_get_ext_d2i(const X509_REVOKED *revoked, int nid, int *out_critical, int *out_idx); -// X509_REVOKED_add1_ext_i2d behaves like |X509V3_add1_i2d| but adds the -// extension to |x|'s extension list. +// X509_REVOKED_add1_ext_i2d behaves like `X509V3_add1_i2d` but adds the +// extension to `x`'s extension list. // // WARNING: This function may return zero or -1 on error. The caller must also -// ensure |value|'s type matches |nid|. See the documentation for -// |X509V3_add1_i2d| for details. +// ensure `value`'s type matches `nid`. See the documentation for +// `X509V3_add1_i2d` for details. OPENSSL_EXPORT int X509_REVOKED_add1_ext_i2d(X509_REVOKED *x, int nid, void *value, int crit, unsigned long flags); @@ -1059,245 +1066,245 @@ OPENSSL_EXPORT int X509_REVOKED_add1_ext_i2d(X509_REVOKED *x, int nid, // Certificate requests. // -// An |X509_REQ| represents a PKCS #10 certificate request (RFC 2986). These are +// An `X509_REQ` represents a PKCS #10 certificate request (RFC 2986). These are // also referred to as certificate signing requests or CSRs. CSRs are a common // format used to request a certificate from a CA. // -// Although an |X509_REQ| is a mutable object, mutating an |X509_REQ| can give -// incorrect results. Callers typically obtain |X509_REQ|s by parsing some input -// with |d2i_X509_REQ|, etc. Such objects carry information such as the +// Although an `X509_REQ` is a mutable object, mutating an `X509_REQ` can give +// incorrect results. Callers typically obtain `X509_REQ`s by parsing some input +// with `d2i_X509_REQ`, etc. Such objects carry information such as the // serialized CertificationRequestInfo, which will become inconsistent when // mutated. // // Instead, mutation functions should only be used when issuing new CRLs, as // described in a later section. -// X509_REQ_dup returns a newly-allocated copy of |req|, or NULL on error. This -// function works by serializing the structure, so if |req| is incomplete, it +// X509_REQ_dup returns a newly-allocated copy of `req`, or NULL on error. This +// function works by serializing the structure, so if `req` is incomplete, it // may fail. OPENSSL_EXPORT X509_REQ *X509_REQ_dup(const X509_REQ *req); -// X509_REQ_free releases memory associated with |req|. +// X509_REQ_free releases memory associated with `req`. OPENSSL_EXPORT void X509_REQ_free(X509_REQ *req); -// d2i_X509_REQ parses up to |len| bytes from |*inp| as a DER-encoded -// CertificateRequest (RFC 2986), as described in |d2i_SAMPLE|. +// d2i_X509_REQ parses up to `len` bytes from `*inp` as a DER-encoded +// CertificateRequest (RFC 2986), as described in `d2i_SAMPLE`. OPENSSL_EXPORT X509_REQ *d2i_X509_REQ(X509_REQ **out, const uint8_t **inp, long len); -// i2d_X509_REQ marshals |req| as a CertificateRequest (RFC 2986), as described -// in |i2d_SAMPLE|. +// i2d_X509_REQ marshals `req` as a CertificateRequest (RFC 2986), as described +// in `i2d_SAMPLE`. OPENSSL_EXPORT int i2d_X509_REQ(const X509_REQ *req, uint8_t **outp); -// X509_REQ_VERSION_1 is the version constant for |X509_REQ| objects. No other +// X509_REQ_VERSION_1 is the version constant for `X509_REQ` objects. No other // versions are defined. #define X509_REQ_VERSION_1 0 -// X509_REQ_get_version returns the numerical value of |req|'s version. This -// will always be |X509_REQ_VERSION_1| for valid CSRs. For compatibility, -// |d2i_X509_REQ| also accepts some invalid version numbers, in which case this +// X509_REQ_get_version returns the numerical value of `req`'s version. This +// will always be `X509_REQ_VERSION_1` for valid CSRs. For compatibility, +// `d2i_X509_REQ` also accepts some invalid version numbers, in which case this // function may return other values. OPENSSL_EXPORT long X509_REQ_get_version(const X509_REQ *req); -// X509_REQ_get_subject_name returns |req|'s subject name. Note this function is +// X509_REQ_get_subject_name returns `req`'s subject name. Note this function is // not const-correct for legacy reasons. OPENSSL_EXPORT X509_NAME *X509_REQ_get_subject_name(const X509_REQ *req); -// X509_REQ_get0_pubkey returns |req|'s public key as an |EVP_PKEY|, or NULL if -// the public key was unsupported or could not be decoded. The |EVP_PKEY| is -// cached in |req|, so callers must not mutate the result. +// X509_REQ_get0_pubkey returns `req`'s public key as an `EVP_PKEY`, or NULL if +// the public key was unsupported or could not be decoded. The `EVP_PKEY` is +// cached in `req`, so callers must not mutate the result. OPENSSL_EXPORT EVP_PKEY *X509_REQ_get0_pubkey(const X509_REQ *req); -// X509_REQ_get_pubkey behaves like |X509_REQ_get0_pubkey| but increments the -// reference count on the |EVP_PKEY|. The caller must release the result with -// |EVP_PKEY_free| when done. The |EVP_PKEY| is cached in |req|, so callers must +// X509_REQ_get_pubkey behaves like `X509_REQ_get0_pubkey` but increments the +// reference count on the `EVP_PKEY`. The caller must release the result with +// `EVP_PKEY_free` when done. The `EVP_PKEY` is cached in `req`, so callers must // not mutate the result. OPENSSL_EXPORT EVP_PKEY *X509_REQ_get_pubkey(const X509_REQ *req); -// X509_REQ_check_private_key returns one if |req|'s public key matches |pkey| +// X509_REQ_check_private_key returns one if `req`'s public key matches `pkey` // and zero otherwise. OPENSSL_EXPORT int X509_REQ_check_private_key(const X509_REQ *req, const EVP_PKEY *pkey); -// X509_REQ_get_attr_count returns the number of attributes in |req|. +// X509_REQ_get_attr_count returns the number of attributes in `req`. OPENSSL_EXPORT int X509_REQ_get_attr_count(const X509_REQ *req); -// X509_REQ_get_attr returns the attribute at index |loc| in |req|, or NULL if +// X509_REQ_get_attr returns the attribute at index `loc` in `req`, or NULL if // out of bounds. OPENSSL_EXPORT X509_ATTRIBUTE *X509_REQ_get_attr(const X509_REQ *req, int loc); -// X509_REQ_get_attr_by_NID returns the index of the attribute in |req| of type -// |nid|, or a negative number if not found. If found, callers can use -// |X509_REQ_get_attr| to look up the attribute by index. +// X509_REQ_get_attr_by_NID returns the index of the attribute in `req` of type +// `nid`, or a negative number if not found. If found, callers can use +// `X509_REQ_get_attr` to look up the attribute by index. // -// If |lastpos| is non-negative, it begins searching at |lastpos| + 1. Callers +// If `lastpos` is non-negative, it begins searching at `lastpos` + 1. Callers // can thus loop over all matching attributes by first passing -1 and then // passing the previously-returned value until no match is returned. OPENSSL_EXPORT int X509_REQ_get_attr_by_NID(const X509_REQ *req, int nid, int lastpos); -// X509_REQ_get_attr_by_OBJ behaves like |X509_REQ_get_attr_by_NID| but looks -// for attributes of type |obj|. +// X509_REQ_get_attr_by_OBJ behaves like `X509_REQ_get_attr_by_NID` but looks +// for attributes of type `obj`. OPENSSL_EXPORT int X509_REQ_get_attr_by_OBJ(const X509_REQ *req, const ASN1_OBJECT *obj, int lastpos); -// X509_REQ_extension_nid returns one if |nid| is a supported CSR attribute type +// X509_REQ_extension_nid returns one if `nid` is a supported CSR attribute type // for carrying extensions and zero otherwise. The supported types are -// |NID_ext_req| (pkcs-9-at-extensionRequest from RFC 2985) and |NID_ms_ext_req| +// `NID_ext_req` (pkcs-9-at-extensionRequest from RFC 2985) and `NID_ms_ext_req` // (a Microsoft szOID_CERT_EXTENSIONS variant). OPENSSL_EXPORT int X509_REQ_extension_nid(int nid); // X509_REQ_get_extensions decodes the most preferred list of requested -// extensions in |req| and returns a newly-allocated |STACK_OF(X509_EXTENSION)| -// containing the result. It returns NULL on error, or if |req| did not request +// extensions in `req` and returns a newly-allocated `STACK_OF(X509_EXTENSION)` +// containing the result. It returns NULL on error, or if `req` did not request // extensions. // // CSRs do not store extensions directly. Instead there are attribute types -// which are defined to hold extensions. See |X509_REQ_extension_nid|. This +// which are defined to hold extensions. See `X509_REQ_extension_nid`. This // function supports both pkcs-9-at-extensionRequest from RFC 2985 and the // Microsoft szOID_CERT_EXTENSIONS variant. If both are present, // pkcs-9-at-extensionRequest is preferred. OPENSSL_EXPORT STACK_OF(X509_EXTENSION) *X509_REQ_get_extensions( const X509_REQ *req); -// X509_REQ_get0_signature sets |*out_sig| and |*out_alg| to the signature and -// signature algorithm of |req|, respectively. Either output pointer may be NULL +// X509_REQ_get0_signature sets `*out_sig` and `*out_alg` to the signature and +// signature algorithm of `req`, respectively. Either output pointer may be NULL // to ignore the value. OPENSSL_EXPORT void X509_REQ_get0_signature(const X509_REQ *req, const ASN1_BIT_STRING **out_sig, const X509_ALGOR **out_alg); -// X509_REQ_get_signature_nid returns the NID corresponding to |req|'s signature -// algorithm, or |NID_undef| if the signature algorithm does not correspond to +// X509_REQ_get_signature_nid returns the NID corresponding to `req`'s signature +// algorithm, or `NID_undef` if the signature algorithm does not correspond to // a known NID. OPENSSL_EXPORT int X509_REQ_get_signature_nid(const X509_REQ *req); -// X509_REQ_verify checks that |req| has a valid signature by |pkey|. It returns +// X509_REQ_verify checks that `req` has a valid signature by `pkey`. It returns // one if the signature is valid and zero otherwise. -OPENSSL_EXPORT int X509_REQ_verify(const X509_REQ *req, EVP_PKEY *pkey); +OPENSSL_EXPORT int X509_REQ_verify(const X509_REQ *req, const EVP_PKEY *pkey); // X509_REQ_get1_email returns a newly-allocated list of NUL-terminated strings -// containing all email addresses in |req|'s subject and all rfc822name names -// in |req|'s subject alternative names. The subject alternative names extension -// is extracted from the result of |X509_REQ_get_extensions|. Email addresses +// containing all email addresses in `req`'s subject and all rfc822name names +// in `req`'s subject alternative names. The subject alternative names extension +// is extracted from the result of `X509_REQ_get_extensions`. Email addresses // which contain embedded NUL bytes are skipped. The results are returned in an // arbitrary order. // // On error, or if there are no such email addresses, it returns NULL. When -// done, the caller must release the result with |X509_email_free|. +// done, the caller must release the result with `X509_email_free`. OPENSSL_EXPORT STACK_OF(OPENSSL_STRING) *X509_REQ_get1_email( const X509_REQ *req); // Issuing certificate requests. // -// An |X509_REQ| object may also represent an incomplete CSR. Callers may -// construct empty |X509_REQ| objects, fill in fields individually, and finally +// An `X509_REQ` object may also represent an incomplete CSR. Callers may +// construct empty `X509_REQ` objects, fill in fields individually, and finally // sign the result. The following functions may be used for this purpose. -// X509_REQ_new returns a newly-allocated, empty |X509_REQ| object, or NULL on +// X509_REQ_new returns a newly-allocated, empty `X509_REQ` object, or NULL on // error. This object may be filled in and then signed to construct a CSR. OPENSSL_EXPORT X509_REQ *X509_REQ_new(void); -// X509_REQ_set_version sets |req|'s version to |version|, which should be -// |X509_REQ_VERSION_1|. It returns one on success and zero on error. +// X509_REQ_set_version sets `req`'s version to `version`, which should be +// `X509_REQ_VERSION_1`. It returns one on success and zero on error. // -// The only defined CSR version is |X509_REQ_VERSION_1|, so there is no need to +// The only defined CSR version is `X509_REQ_VERSION_1`, so there is no need to // call this function. OPENSSL_EXPORT int X509_REQ_set_version(X509_REQ *req, long version); -// X509_REQ_set_subject_name sets |req|'s subject to a copy of |name|. It +// X509_REQ_set_subject_name sets `req`'s subject to a copy of `name`. It // returns one on success and zero on error. -OPENSSL_EXPORT int X509_REQ_set_subject_name(X509_REQ *req, X509_NAME *name); +OPENSSL_EXPORT int X509_REQ_set_subject_name(X509_REQ *req, + const X509_NAME *name); -// X509_REQ_set_pubkey sets |req|'s public key to |pkey|. It returns one on -// success and zero on error. This function does not take ownership of |pkey| -// and internally copies and updates reference counts as needed. -OPENSSL_EXPORT int X509_REQ_set_pubkey(X509_REQ *req, EVP_PKEY *pkey); +// X509_REQ_set_pubkey sets `req`'s public key to `pkey`. It returns one on +// success and zero on error. +OPENSSL_EXPORT int X509_REQ_set_pubkey(X509_REQ *req, const EVP_PKEY *pkey); -// X509_REQ_delete_attr removes the attribute at index |loc| in |req|. It -// returns the removed attribute to the caller, or NULL if |loc| was out of +// X509_REQ_delete_attr removes the attribute at index `loc` in `req`. It +// returns the removed attribute to the caller, or NULL if `loc` was out of // bounds. If non-NULL, the caller must release the result with -// |X509_ATTRIBUTE_free| when done. It is also safe, but not necessary, to call -// |X509_ATTRIBUTE_free| if the result is NULL. +// `X509_ATTRIBUTE_free` when done. It is also safe, but not necessary, to call +// `X509_ATTRIBUTE_free` if the result is NULL. OPENSSL_EXPORT X509_ATTRIBUTE *X509_REQ_delete_attr(X509_REQ *req, int loc); -// X509_REQ_add1_attr appends a copy of |attr| to |req|'s list of attributes. It +// X509_REQ_add1_attr appends a copy of `attr` to `req`'s list of attributes. It // returns one on success and zero on error. OPENSSL_EXPORT int X509_REQ_add1_attr(X509_REQ *req, const X509_ATTRIBUTE *attr); -// X509_REQ_add1_attr_by_OBJ appends a new attribute to |req| with type |obj|. +// X509_REQ_add1_attr_by_OBJ appends a new attribute to `req` with type `obj`. // It returns one on success and zero on error. The value is determined by -// |X509_ATTRIBUTE_set1_data|. +// `X509_ATTRIBUTE_set1_data`. // -// WARNING: The interpretation of |attrtype|, |data|, and |len| is complex and -// error-prone. See |X509_ATTRIBUTE_set1_data| for details. +// WARNING: The interpretation of `attrtype`, `data`, and `len` is complex and +// error-prone. See `X509_ATTRIBUTE_set1_data` for details. OPENSSL_EXPORT int X509_REQ_add1_attr_by_OBJ(X509_REQ *req, const ASN1_OBJECT *obj, int attrtype, const unsigned char *data, int len); -// X509_REQ_add1_attr_by_NID behaves like |X509_REQ_add1_attr_by_OBJ| except the -// attribute type is determined by |nid|. +// X509_REQ_add1_attr_by_NID behaves like `X509_REQ_add1_attr_by_OBJ` except the +// attribute type is determined by `nid`. OPENSSL_EXPORT int X509_REQ_add1_attr_by_NID(X509_REQ *req, int nid, int attrtype, const unsigned char *data, int len); -// X509_REQ_add1_attr_by_txt behaves like |X509_REQ_add1_attr_by_OBJ| except the -// attribute type is determined by calling |OBJ_txt2obj| with |attrname|. +// X509_REQ_add1_attr_by_txt behaves like `X509_REQ_add1_attr_by_OBJ` except the +// attribute type is determined by calling `OBJ_txt2obj` with `attrname`. OPENSSL_EXPORT int X509_REQ_add1_attr_by_txt(X509_REQ *req, const char *attrname, int attrtype, const unsigned char *data, int len); -// X509_REQ_add_extensions_nid adds an attribute to |req| of type |nid|, to -// request the certificate extensions in |exts|. It returns one on success and -// zero on error. |nid| should be |NID_ext_req| or |NID_ms_ext_req|. +// X509_REQ_add_extensions_nid adds an attribute to `req` of type `nid`, to +// request the certificate extensions in `exts`. It returns one on success and +// zero on error. `nid` should be `NID_ext_req` or `NID_ms_ext_req`. OPENSSL_EXPORT int X509_REQ_add_extensions_nid( X509_REQ *req, const STACK_OF(X509_EXTENSION) *exts, int nid); -// X509_REQ_add_extensions behaves like |X509_REQ_add_extensions_nid|, using the -// standard |NID_ext_req| for the attribute type. +// X509_REQ_add_extensions behaves like `X509_REQ_add_extensions_nid`, using the +// standard `NID_ext_req` for the attribute type. OPENSSL_EXPORT int X509_REQ_add_extensions( X509_REQ *req, const STACK_OF(X509_EXTENSION) *exts); -// X509_REQ_sign signs |req| with |pkey| and replaces the signature algorithm +// X509_REQ_sign signs `req` with `pkey` and replaces the signature algorithm // and signature fields. It returns the length of the signature on success and -// zero on error. This function uses digest algorithm |md|, or |pkey|'s default -// if NULL. Other signing parameters use |pkey|'s defaults. To customize them, -// use |X509_REQ_sign_ctx|. -OPENSSL_EXPORT int X509_REQ_sign(X509_REQ *req, EVP_PKEY *pkey, +// zero on error. This function uses digest algorithm `md`, or `pkey`'s default +// if NULL. Other signing parameters use `pkey`'s defaults. To customize them, +// use `X509_REQ_sign_ctx`. +OPENSSL_EXPORT int X509_REQ_sign(X509_REQ *req, const EVP_PKEY *pkey, const EVP_MD *md); -// X509_REQ_sign_ctx signs |req| with |ctx| and replaces the signature algorithm +// X509_REQ_sign_ctx signs `req` with `ctx` and replaces the signature algorithm // and signature fields. It returns the length of the signature on success and -// zero on error. The signature algorithm and parameters come from |ctx|, which -// must have been initialized with |EVP_DigestSignInit|. The caller should -// configure the corresponding |EVP_PKEY_CTX| before calling this function. +// zero on error. The signature algorithm and parameters come from `ctx`, which +// must have been initialized with `EVP_DigestSignInit`. The caller should +// configure the corresponding `EVP_PKEY_CTX` before calling this function. // -// On success or failure, this function mutates |ctx| and resets it to the empty +// On success or failure, this function mutates `ctx` and resets it to the empty // state. Caller should not rely on its contents after the function returns. OPENSSL_EXPORT int X509_REQ_sign_ctx(X509_REQ *req, EVP_MD_CTX *ctx); // i2d_re_X509_REQ_tbs serializes the CertificationRequestInfo (see RFC 2986) -// portion of |req|, as described in |i2d_SAMPLE|. +// portion of `req`, as described in `i2d_SAMPLE`. // // This function re-encodes the CertificationRequestInfo and may not reflect -// |req|'s original encoding. It may be used to manually generate a signature +// `req`'s original encoding. It may be used to manually generate a signature // for a new certificate request. OPENSSL_EXPORT int i2d_re_X509_REQ_tbs(X509_REQ *req, uint8_t **outp); -// X509_REQ_set1_signature_algo sets |req|'s signature algorithm to |algo| and +// X509_REQ_set1_signature_algo sets `req`'s signature algorithm to `algo` and // returns one on success or zero on error. OPENSSL_EXPORT int X509_REQ_set1_signature_algo(X509_REQ *req, const X509_ALGOR *algo); -// X509_REQ_set1_signature_value sets |req|'s signature to a copy of the -// |sig_len| bytes pointed by |sig|. It returns one on success and zero on +// X509_REQ_set1_signature_value sets `req`'s signature to a copy of the +// `sig_len` bytes pointed by `sig`. It returns one on success and zero on // error. // // Due to a specification error, PKCS#10 certificate requests store signatures @@ -1312,7 +1319,7 @@ OPENSSL_EXPORT int X509_REQ_set1_signature_value(X509_REQ *req, // Names. // -// An |X509_NAME| represents an X.509 Name structure (RFC 5280). X.509 names are +// An `X509_NAME` represents an X.509 Name structure (RFC 5280). X.509 names are // a complex, hierarchical structure over a collection of attributes. Each name // is sequence of relative distinguished names (RDNs), decreasing in // specificity. For example, the first RDN may specify the country, while the @@ -1322,10 +1329,10 @@ OPENSSL_EXPORT int X509_REQ_set1_signature_value(X509_REQ *req, // OBJECT IDENTIFIERs. This determines the ASN.1 type of the value, which is // commonly a string but may be other types. // -// The |X509_NAME| representation flattens this two-level structure into a -// single list of attributes. Each attribute is stored in an |X509_NAME_ENTRY|, +// The `X509_NAME` representation flattens this two-level structure into a +// single list of attributes. Each attribute is stored in an `X509_NAME_ENTRY`, // with also maintains the index of the RDN it is part of, accessible via -// |X509_NAME_ENTRY_set|. This can be used to recover the two-level structure. +// `X509_NAME_ENTRY_set`. This can be used to recover the two-level structure. // // X.509 names are largely vestigial. Historically, DNS names were parsed out of // the subject's common name attribute, but this is deprecated and has since @@ -1335,30 +1342,30 @@ OPENSSL_EXPORT int X509_REQ_set1_signature_value(X509_REQ *req, DEFINE_STACK_OF(X509_NAME_ENTRY) DEFINE_STACK_OF(X509_NAME) -// X509_NAME is an |ASN1_ITEM| whose ASN.1 type is X.509 Name (RFC 5280) and C -// type is |X509_NAME*|. +// X509_NAME is an `ASN1_ITEM` whose ASN.1 type is X.509 Name (RFC 5280) and C +// type is `X509_NAME*`. DECLARE_ASN1_ITEM(X509_NAME) -// X509_NAME_new returns a new, empty |X509_NAME|, or NULL on error. +// X509_NAME_new returns a new, empty `X509_NAME`, or NULL on error. OPENSSL_EXPORT X509_NAME *X509_NAME_new(void); -// X509_NAME_free releases memory associated with |name|. +// X509_NAME_free releases memory associated with `name`. OPENSSL_EXPORT void X509_NAME_free(X509_NAME *name); -// d2i_X509_NAME parses up to |len| bytes from |*inp| as a DER-encoded X.509 -// Name (RFC 5280), as described in |d2i_SAMPLE|. +// d2i_X509_NAME parses up to `len` bytes from `*inp` as a DER-encoded X.509 +// Name (RFC 5280), as described in `d2i_SAMPLE`. OPENSSL_EXPORT X509_NAME *d2i_X509_NAME(X509_NAME **out, const uint8_t **inp, long len); -// i2d_X509_NAME marshals |in| as a DER-encoded X.509 Name (RFC 5280), as -// described in |i2d_SAMPLE|. +// i2d_X509_NAME marshals `in` as a DER-encoded X.509 Name (RFC 5280), as +// described in `i2d_SAMPLE`. OPENSSL_EXPORT int i2d_X509_NAME(const X509_NAME *in, uint8_t **outp); -// X509_NAME_dup returns a newly-allocated copy of |name|, or NULL on error. +// X509_NAME_dup returns a newly-allocated copy of `name`, or NULL on error. OPENSSL_EXPORT X509_NAME *X509_NAME_dup(const X509_NAME *name); -// X509_NAME_cmp compares |a| and |b|'s canonicalized forms. It returns zero if -// they are equal, one if |a| sorts after |b|, -1 if |b| sorts after |a|, and -2 +// X509_NAME_cmp compares `a` and `b`'s canonicalized forms. It returns zero if +// they are equal, one if `a` sorts after `b`, -1 if `b` sorts after `a`, and -2 // on error. // // TODO(https://crbug.com/boringssl/355): The -2 return is very inconvenient to @@ -1367,173 +1374,173 @@ OPENSSL_EXPORT X509_NAME *X509_NAME_dup(const X509_NAME *name); // Although even the library itself passes this to a sorting function. OPENSSL_EXPORT int X509_NAME_cmp(const X509_NAME *a, const X509_NAME *b); -// X509_NAME_get0_der marshals |name| as a DER-encoded X.509 Name (RFC 5280). On -// success, it returns one and sets |*out_der| and |*out_der_len| to a buffer -// containing the result. Otherwise, it returns zero. |*out_der| is owned by -// |name| and must not be freed by the caller. It is invalidated after |name| is +// X509_NAME_get0_der marshals `name` as a DER-encoded X.509 Name (RFC 5280). On +// success, it returns one and sets `*out_der` and `*out_der_len` to a buffer +// containing the result. Otherwise, it returns zero. `*out_der` is owned by +// `name` and must not be freed by the caller. It is invalidated after `name` is // mutated or freed. OPENSSL_EXPORT int X509_NAME_get0_der(const X509_NAME *name, const uint8_t **out_der, size_t *out_der_len); -// X509_NAME_set makes a copy of |name|. On success, it frees |*xn|, sets |*xn| +// X509_NAME_set makes a copy of `name`. On success, it frees `*xn`, sets `*xn` // to the copy, and returns one. Otherwise, it returns zero. OPENSSL_EXPORT int X509_NAME_set(X509_NAME **xn, const X509_NAME *name); -// X509_NAME_entry_count returns the number of entries in |name|. +// X509_NAME_entry_count returns the number of entries in `name`. OPENSSL_EXPORT int X509_NAME_entry_count(const X509_NAME *name); // X509_NAME_get_index_by_NID returns the zero-based index of the first -// attribute in |name| with type |nid|, or -1 if there is none. |nid| should be -// one of the |NID_*| constants. If |lastpos| is non-negative, it begins -// searching at |lastpos+1|. To search all attributes, pass in -1, not zero. +// attribute in `name` with type `nid`, or -1 if there is none. `nid` should be +// one of the `NID_*` constants. If `lastpos` is non-negative, it begins +// searching at `lastpos+1`. To search all attributes, pass in -1, not zero. // -// Indices from this function refer to |X509_NAME|'s flattened representation. +// Indices from this function refer to `X509_NAME`'s flattened representation. OPENSSL_EXPORT int X509_NAME_get_index_by_NID(const X509_NAME *name, int nid, int lastpos); -// X509_NAME_get_index_by_OBJ behaves like |X509_NAME_get_index_by_NID| but -// looks for attributes with type |obj|. +// X509_NAME_get_index_by_OBJ behaves like `X509_NAME_get_index_by_NID` but +// looks for attributes with type `obj`. OPENSSL_EXPORT int X509_NAME_get_index_by_OBJ(const X509_NAME *name, const ASN1_OBJECT *obj, int lastpos); -// X509_NAME_get_entry returns the attribute in |name| at index |loc|, or NULL -// if |loc| is out of range. |loc| is interpreted using |X509_NAME|'s flattened +// X509_NAME_get_entry returns the attribute in `name` at index `loc`, or NULL +// if `loc` is out of range. `loc` is interpreted using `X509_NAME`'s flattened // representation. This function returns a non-const pointer for OpenSSL // compatibility, but callers should not mutate the result. Doing so will break // internal invariants in the library. OPENSSL_EXPORT X509_NAME_ENTRY *X509_NAME_get_entry(const X509_NAME *name, int loc); -// X509_NAME_delete_entry removes and returns the attribute in |name| at index -// |loc|, or NULL if |loc| is out of range. |loc| is interpreted using -// |X509_NAME|'s flattened representation. If the attribute is found, the caller -// is responsible for releasing the result with |X509_NAME_ENTRY_free|. +// X509_NAME_delete_entry removes and returns the attribute in `name` at index +// `loc`, or NULL if `loc` is out of range. `loc` is interpreted using +// `X509_NAME`'s flattened representation. If the attribute is found, the caller +// is responsible for releasing the result with `X509_NAME_ENTRY_free`. // -// This function will internally update RDN indices (see |X509_NAME_ENTRY_set|) +// This function will internally update RDN indices (see `X509_NAME_ENTRY_set`) // so they continue to be consecutive. OPENSSL_EXPORT X509_NAME_ENTRY *X509_NAME_delete_entry(X509_NAME *name, int loc); -// X509_NAME_add_entry adds a copy of |entry| to |name| and returns one on -// success or zero on error. If |loc| is -1, the entry is appended to |name|. -// Otherwise, it is inserted at index |loc|. If |set| is -1, the entry is added +// X509_NAME_add_entry adds a copy of `entry` to `name` and returns one on +// success or zero on error. If `loc` is -1, the entry is appended to `name`. +// Otherwise, it is inserted at index `loc`. If `set` is -1, the entry is added // to the previous entry's RDN. If it is 0, the entry becomes a singleton RDN. // If 1, it is added to next entry's RDN. // -// This function will internally update RDN indices (see |X509_NAME_ENTRY_set|) +// This function will internally update RDN indices (see `X509_NAME_ENTRY_set`) // so they continue to be consecutive. OPENSSL_EXPORT int X509_NAME_add_entry(X509_NAME *name, const X509_NAME_ENTRY *entry, int loc, int set); -// X509_NAME_add_entry_by_OBJ adds a new entry to |name| and returns one on -// success or zero on error. The entry's attribute type is |obj|. The entry's -// attribute value is determined by |type|, |bytes|, and |len|, as in -// |X509_NAME_ENTRY_set_data|. The entry's position is determined by |loc| and -// |set| as in |X509_NAME_add_entry|. +// X509_NAME_add_entry_by_OBJ adds a new entry to `name` and returns one on +// success or zero on error. The entry's attribute type is `obj`. The entry's +// attribute value is determined by `type`, `bytes`, and `len`, as in +// `X509_NAME_ENTRY_set_data`. The entry's position is determined by `loc` and +// `set` as in `X509_NAME_add_entry`. OPENSSL_EXPORT int X509_NAME_add_entry_by_OBJ(X509_NAME *name, const ASN1_OBJECT *obj, int type, const uint8_t *bytes, ossl_ssize_t len, int loc, int set); -// X509_NAME_add_entry_by_NID behaves like |X509_NAME_add_entry_by_OBJ| but sets -// the entry's attribute type to |nid|, which should be one of the |NID_*| +// X509_NAME_add_entry_by_NID behaves like `X509_NAME_add_entry_by_OBJ` but sets +// the entry's attribute type to `nid`, which should be one of the `NID_*` // constants. OPENSSL_EXPORT int X509_NAME_add_entry_by_NID(X509_NAME *name, int nid, int type, const uint8_t *bytes, ossl_ssize_t len, int loc, int set); -// X509_NAME_add_entry_by_txt behaves like |X509_NAME_add_entry_by_OBJ| but sets -// the entry's attribute type to |field|, which is passed to |OBJ_txt2obj|. +// X509_NAME_add_entry_by_txt behaves like `X509_NAME_add_entry_by_OBJ` but sets +// the entry's attribute type to `field`, which is passed to `OBJ_txt2obj`. OPENSSL_EXPORT int X509_NAME_add_entry_by_txt(X509_NAME *name, const char *field, int type, const uint8_t *bytes, ossl_ssize_t len, int loc, int set); -// X509_NAME_ENTRY_new returns a new, empty |X509_NAME_ENTRY|, or NULL on error. +// X509_NAME_ENTRY_new returns a new, empty `X509_NAME_ENTRY`, or NULL on error. OPENSSL_EXPORT X509_NAME_ENTRY *X509_NAME_ENTRY_new(void); -// X509_NAME_ENTRY_free releases memory associated with |entry|. +// X509_NAME_ENTRY_free releases memory associated with `entry`. OPENSSL_EXPORT void X509_NAME_ENTRY_free(X509_NAME_ENTRY *entry); -// X509_NAME_ENTRY_dup returns a newly-allocated copy of |entry|, or NULL on +// X509_NAME_ENTRY_dup returns a newly-allocated copy of `entry`, or NULL on // error. OPENSSL_EXPORT X509_NAME_ENTRY *X509_NAME_ENTRY_dup( const X509_NAME_ENTRY *entry); -// X509_NAME_ENTRY_get_object returns |entry|'s attribute type. This function +// X509_NAME_ENTRY_get_object returns `entry`'s attribute type. This function // returns a non-const pointer for OpenSSL compatibility, but callers should not // mutate the result. Doing so will break internal invariants in the library. OPENSSL_EXPORT ASN1_OBJECT *X509_NAME_ENTRY_get_object( const X509_NAME_ENTRY *entry); -// X509_NAME_ENTRY_set_object sets |entry|'s attribute type to |obj|. It returns +// X509_NAME_ENTRY_set_object sets `entry`'s attribute type to `obj`. It returns // one on success and zero on error. OPENSSL_EXPORT int X509_NAME_ENTRY_set_object(X509_NAME_ENTRY *entry, const ASN1_OBJECT *obj); -// X509_NAME_ENTRY_get_data returns |entry|'s attribute value, represented as an -// |ASN1_STRING|. This value may have any ASN.1 type, so callers must check the +// X509_NAME_ENTRY_get_data returns `entry`'s attribute value, represented as an +// `ASN1_STRING`. This value may have any ASN.1 type, so callers must check the // type before interpreting the contents. This function returns a non-const // pointer for OpenSSL compatibility, but callers should not mutate the result. // Doing so will break internal invariants in the library. // -// See |ASN1_STRING| for how values are represented in this library. Where a -// specific |ASN1_STRING| representation exists, that representation is used. -// Otherwise, the |V_ASN1_OTHER| representation is used. Note that NULL, OBJECT -// IDENTIFIER, and BOOLEAN attribute values are represented as |V_ASN1_OTHER|, +// See `ASN1_STRING` for how values are represented in this library. Where a +// specific `ASN1_STRING` representation exists, that representation is used. +// Otherwise, the `V_ASN1_OTHER` representation is used. Note that NULL, OBJECT +// IDENTIFIER, and BOOLEAN attribute values are represented as `V_ASN1_OTHER`, // because their usual representation in this library is not -// |ASN1_STRING|-compatible. +// `ASN1_STRING`-compatible. OPENSSL_EXPORT ASN1_STRING *X509_NAME_ENTRY_get_data( const X509_NAME_ENTRY *entry); -// X509_NAME_ENTRY_set_data sets |entry|'s value to |len| bytes from |bytes|. It -// returns one on success and zero on error. If |len| is -1, |bytes| must be a -// NUL-terminated C string and the length is determined by |strlen|. |bytes| is +// X509_NAME_ENTRY_set_data sets `entry`'s value to `len` bytes from `bytes`. It +// returns one on success and zero on error. If `len` is -1, `bytes` must be a +// NUL-terminated C string and the length is determined by `strlen`. `bytes` is // converted to an ASN.1 type as follows: // -// If |type| is a |MBSTRING_*| constant, the value is an ASN.1 string. The -// string is determined by decoding |bytes| in the encoding specified by |type|, -// and then re-encoding it in a form appropriate for |entry|'s attribute type. -// See |ASN1_STRING_set_by_NID| for details. +// If `type` is a `MBSTRING_*` constant, the value is an ASN.1 string. The +// string is determined by decoding `bytes` in the encoding specified by `type`, +// and then re-encoding it in a form appropriate for `entry`'s attribute type. +// See `ASN1_STRING_set_by_NID` for details. // -// Otherwise, the value is an |ASN1_STRING| with type |type| and value |bytes|. -// See |ASN1_STRING| for how to format ASN.1 types as an |ASN1_STRING|. If -// |type| is |V_ASN1_UNDEF| the previous |ASN1_STRING| type is reused. +// Otherwise, the value is an `ASN1_STRING` with type `type` and value `bytes`. +// See `ASN1_STRING` for how to format ASN.1 types as an `ASN1_STRING`. If +// `type` is `V_ASN1_UNDEF` the previous `ASN1_STRING` type is reused. OPENSSL_EXPORT int X509_NAME_ENTRY_set_data(X509_NAME_ENTRY *entry, int type, const uint8_t *bytes, ossl_ssize_t len); // X509_NAME_ENTRY_set returns the zero-based index of the RDN which contains -// |entry|. Consecutive entries with the same index are part of the same RDN. +// `entry`. Consecutive entries with the same index are part of the same RDN. OPENSSL_EXPORT int X509_NAME_ENTRY_set(const X509_NAME_ENTRY *entry); -// X509_NAME_ENTRY_create_by_OBJ creates a new |X509_NAME_ENTRY| with attribute -// type |obj|. The attribute value is determined from |type|, |bytes|, and |len| -// as in |X509_NAME_ENTRY_set_data|. It returns the |X509_NAME_ENTRY| on success +// X509_NAME_ENTRY_create_by_OBJ creates a new `X509_NAME_ENTRY` with attribute +// type `obj`. The attribute value is determined from `type`, `bytes`, and `len` +// as in `X509_NAME_ENTRY_set_data`. It returns the `X509_NAME_ENTRY` on success // and NULL on error. // -// If |out| is non-NULL and |*out| is NULL, it additionally sets |*out| to the -// result on success. If both |out| and |*out| are non-NULL, it updates the -// object at |*out| instead of allocating a new one. +// If `out` is non-NULL and `*out` is NULL, it additionally sets `*out` to the +// result on success. If both `out` and `*out` are non-NULL, it updates the +// object at `*out` instead of allocating a new one. OPENSSL_EXPORT X509_NAME_ENTRY *X509_NAME_ENTRY_create_by_OBJ( X509_NAME_ENTRY **out, const ASN1_OBJECT *obj, int type, const uint8_t *bytes, ossl_ssize_t len); -// X509_NAME_ENTRY_create_by_NID behaves like |X509_NAME_ENTRY_create_by_OBJ| -// except the attribute type is |nid|, which should be one of the |NID_*| +// X509_NAME_ENTRY_create_by_NID behaves like `X509_NAME_ENTRY_create_by_OBJ` +// except the attribute type is `nid`, which should be one of the `NID_*` // constants. OPENSSL_EXPORT X509_NAME_ENTRY *X509_NAME_ENTRY_create_by_NID( X509_NAME_ENTRY **out, int nid, int type, const uint8_t *bytes, ossl_ssize_t len); -// X509_NAME_ENTRY_create_by_txt behaves like |X509_NAME_ENTRY_create_by_OBJ| -// except the attribute type is |field|, which is passed to |OBJ_txt2obj|. +// X509_NAME_ENTRY_create_by_txt behaves like `X509_NAME_ENTRY_create_by_OBJ` +// except the attribute type is `field`, which is passed to `OBJ_txt2obj`. OPENSSL_EXPORT X509_NAME_ENTRY *X509_NAME_ENTRY_create_by_txt( X509_NAME_ENTRY **out, const char *field, int type, const uint8_t *bytes, ossl_ssize_t len); @@ -1542,74 +1549,74 @@ OPENSSL_EXPORT X509_NAME_ENTRY *X509_NAME_ENTRY_create_by_txt( // Public keys. // // X.509 encodes public keys as SubjectPublicKeyInfo (RFC 5280), sometimes -// referred to as SPKI. These are represented in this library by |X509_PUBKEY|. +// referred to as SPKI. These are represented in this library by `X509_PUBKEY`. -// X509_PUBKEY_new returns a newly-allocated, empty |X509_PUBKEY| object, or +// X509_PUBKEY_new returns a newly-allocated, empty `X509_PUBKEY` object, or // NULL on error. OPENSSL_EXPORT X509_PUBKEY *X509_PUBKEY_new(void); -// X509_PUBKEY_free releases memory associated with |key|. +// X509_PUBKEY_free releases memory associated with `key`. OPENSSL_EXPORT void X509_PUBKEY_free(X509_PUBKEY *key); -// d2i_X509_PUBKEY parses up to |len| bytes from |*inp| as a DER-encoded -// SubjectPublicKeyInfo, as described in |d2i_SAMPLE|. +// d2i_X509_PUBKEY parses up to `len` bytes from `*inp` as a DER-encoded +// SubjectPublicKeyInfo, as described in `d2i_SAMPLE`. OPENSSL_EXPORT X509_PUBKEY *d2i_X509_PUBKEY(X509_PUBKEY **out, const uint8_t **inp, long len); -// i2d_X509_PUBKEY marshals |key| as a DER-encoded SubjectPublicKeyInfo, as -// described in |i2d_SAMPLE|. +// i2d_X509_PUBKEY marshals `key` as a DER-encoded SubjectPublicKeyInfo, as +// described in `i2d_SAMPLE`. OPENSSL_EXPORT int i2d_X509_PUBKEY(const X509_PUBKEY *key, uint8_t **outp); -// X509_PUBKEY_set serializes |pkey| into a newly-allocated |X509_PUBKEY| -// structure. On success, it frees |*x| if non-NULL, then sets |*x| to the new +// X509_PUBKEY_set serializes `pkey` into a newly-allocated `X509_PUBKEY` +// structure. On success, it frees `*x` if non-NULL, then sets `*x` to the new // object, and returns one. Otherwise, it returns zero. -OPENSSL_EXPORT int X509_PUBKEY_set(X509_PUBKEY **x, EVP_PKEY *pkey); +OPENSSL_EXPORT int X509_PUBKEY_set(X509_PUBKEY **x, const EVP_PKEY *pkey); -// X509_PUBKEY_get0 returns |key| as an |EVP_PKEY|, or NULL if |key| either -// could not be parsed or is an unrecognized algorithm. The |EVP_PKEY| is cached -// in |key|, so callers must not mutate the result. +// X509_PUBKEY_get0 returns `key` as an `EVP_PKEY`, or NULL if `key` either +// could not be parsed or is an unrecognized algorithm. The `EVP_PKEY` is cached +// in `key`, so callers must not mutate the result. OPENSSL_EXPORT EVP_PKEY *X509_PUBKEY_get0(const X509_PUBKEY *key); -// X509_PUBKEY_get behaves like |X509_PUBKEY_get0| but increments the reference -// count on the |EVP_PKEY|. The caller must release the result with -// |EVP_PKEY_free| when done. The |EVP_PKEY| is cached in |key|, so callers must +// X509_PUBKEY_get behaves like `X509_PUBKEY_get0` but increments the reference +// count on the `EVP_PKEY`. The caller must release the result with +// `EVP_PKEY_free` when done. The `EVP_PKEY` is cached in `key`, so callers must // not mutate the result. OPENSSL_EXPORT EVP_PKEY *X509_PUBKEY_get(const X509_PUBKEY *key); -// X509_PUBKEY_set0_param sets |pub| to a key with AlgorithmIdentifier -// determined by |obj|, |param_type|, and |param_value|, and an encoded -// public key of |key|. On success, it gives |pub| ownership of all the other -// parameters and returns one. Otherwise, it returns zero. |key| must have been -// allocated by |OPENSSL_malloc|. |obj| and, if applicable, |param_value| must +// X509_PUBKEY_set0_param sets `pub` to a key with AlgorithmIdentifier +// determined by `obj`, `param_type`, and `param_value`, and an encoded +// public key of `key`. On success, it gives `pub` ownership of all the other +// parameters and returns one. Otherwise, it returns zero. `key` must have been +// allocated by `OPENSSL_malloc`. `obj` and, if applicable, `param_value` must // not be freed after a successful call, and must have been allocated in a -// manner compatible with |ASN1_OBJECT_free| or |ASN1_STRING_free|. +// manner compatible with `ASN1_OBJECT_free` or `ASN1_STRING_free`. // -// |obj|, |param_type|, and |param_value| are interpreted as in -// |X509_ALGOR_set0|. See |X509_ALGOR_set0| for details. +// `obj`, `param_type`, and `param_value` are interpreted as in +// `X509_ALGOR_set0`. See `X509_ALGOR_set0` for details. OPENSSL_EXPORT int X509_PUBKEY_set0_param(X509_PUBKEY *pub, ASN1_OBJECT *obj, int param_type, void *param_value, uint8_t *key, int key_len); -// X509_PUBKEY_get0_param outputs fields of |pub| and returns one. If |out_obj| -// is not NULL, it sets |*out_obj| to AlgorithmIdentifier's OID. If |out_key| -// is not NULL, it sets |*out_key| and |*out_key_len| to the encoded public key. -// If |out_alg| is not NULL, it sets |*out_alg| to the AlgorithmIdentifier. +// X509_PUBKEY_get0_param outputs fields of `pub` and returns one. If `out_obj` +// is not NULL, it sets `*out_obj` to AlgorithmIdentifier's OID. If `out_key` +// is not NULL, it sets `*out_key` and `*out_key_len` to the encoded public key. +// If `out_alg` is not NULL, it sets `*out_alg` to the AlgorithmIdentifier. // -// All pointers outputted by this function are internal to |pub| and must not be +// All pointers outputted by this function are internal to `pub` and must not be // freed by the caller. Additionally, although some outputs are non-const, // callers must not mutate the resulting objects. // // Note: X.509 SubjectPublicKeyInfo structures store the encoded public key as a -// BIT STRING. |*out_key| and |*out_key_len| will silently pad the key with zero -// bits if |pub| did not contain a whole number of bytes. Use -// |X509_PUBKEY_get0_public_key| to preserve this information. +// BIT STRING. `*out_key` and `*out_key_len` will silently pad the key with zero +// bits if `pub` did not contain a whole number of bytes. Use +// `X509_PUBKEY_get0_public_key` to preserve this information. OPENSSL_EXPORT int X509_PUBKEY_get0_param(ASN1_OBJECT **out_obj, const uint8_t **out_key, int *out_key_len, X509_ALGOR **out_alg, X509_PUBKEY *pub); -// X509_PUBKEY_get0_public_key returns |pub|'s encoded public key. +// X509_PUBKEY_get0_public_key returns `pub`'s encoded public key. OPENSSL_EXPORT const ASN1_BIT_STRING *X509_PUBKEY_get0_public_key( const X509_PUBKEY *pub); @@ -1617,79 +1624,79 @@ OPENSSL_EXPORT const ASN1_BIT_STRING *X509_PUBKEY_get0_public_key( // Extensions. // // X.509 certificates and CRLs may contain a list of extensions (RFC 5280). -// Extensions have a type, specified by an object identifier (|ASN1_OBJECT|) and +// Extensions have a type, specified by an object identifier (`ASN1_OBJECT`) and // a byte string value, which should a DER-encoded structure whose type is // determined by the extension type. This library represents extensions with the -// |X509_EXTENSION| type. +// `X509_EXTENSION` type. -// X509_EXTENSION is an |ASN1_ITEM| whose ASN.1 type is X.509 Extension (RFC -// 5280) and C type is |X509_EXTENSION*|. +// X509_EXTENSION is an `ASN1_ITEM` whose ASN.1 type is X.509 Extension (RFC +// 5280) and C type is `X509_EXTENSION*`. DECLARE_ASN1_ITEM(X509_EXTENSION) -// X509_EXTENSION_new returns a newly-allocated, empty |X509_EXTENSION| object +// X509_EXTENSION_new returns a newly-allocated, empty `X509_EXTENSION` object // or NULL on error. OPENSSL_EXPORT X509_EXTENSION *X509_EXTENSION_new(void); -// X509_EXTENSION_free releases memory associated with |ex|. +// X509_EXTENSION_free releases memory associated with `ex`. OPENSSL_EXPORT void X509_EXTENSION_free(X509_EXTENSION *ex); -// d2i_X509_EXTENSION parses up to |len| bytes from |*inp| as a DER-encoded -// X.509 Extension (RFC 5280), as described in |d2i_SAMPLE|. +// d2i_X509_EXTENSION parses up to `len` bytes from `*inp` as a DER-encoded +// X.509 Extension (RFC 5280), as described in `d2i_SAMPLE`. OPENSSL_EXPORT X509_EXTENSION *d2i_X509_EXTENSION(X509_EXTENSION **out, const uint8_t **inp, long len); -// i2d_X509_EXTENSION marshals |ex| as a DER-encoded X.509 Extension (RFC -// 5280), as described in |i2d_SAMPLE|. +// i2d_X509_EXTENSION marshals `ex` as a DER-encoded X.509 Extension (RFC +// 5280), as described in `i2d_SAMPLE`. OPENSSL_EXPORT int i2d_X509_EXTENSION(const X509_EXTENSION *ex, uint8_t **outp); -// X509_EXTENSION_dup returns a newly-allocated copy of |ex|, or NULL on error. -// This function works by serializing the structure, so if |ex| is incomplete, +// X509_EXTENSION_dup returns a newly-allocated copy of `ex`, or NULL on error. +// This function works by serializing the structure, so if `ex` is incomplete, // it may fail. OPENSSL_EXPORT X509_EXTENSION *X509_EXTENSION_dup(const X509_EXTENSION *ex); -// X509_EXTENSION_create_by_NID creates a new |X509_EXTENSION| with type |nid|, -// value |data|, and critical bit |crit|. It returns an |X509_EXTENSION| on -// success, and NULL on error. |nid| should be a |NID_*| constant. +// X509_EXTENSION_create_by_NID creates a new `X509_EXTENSION` with type `nid`, +// value `data`, and critical bit `crit`. It returns an `X509_EXTENSION` on +// success, and NULL on error. `nid` should be a `NID_*` constant. // -// If |ex| and |*ex| are both non-NULL, |*ex| is used to hold the result, -// otherwise a new object is allocated. If |ex| is non-NULL and |*ex| is NULL, -// the function sets |*ex| to point to the newly allocated result, in addition +// If `ex` and `*ex` are both non-NULL, `*ex` is used to hold the result, +// otherwise a new object is allocated. If `ex` is non-NULL and `*ex` is NULL, +// the function sets `*ex` to point to the newly allocated result, in addition // to returning the result. OPENSSL_EXPORT X509_EXTENSION *X509_EXTENSION_create_by_NID( X509_EXTENSION **ex, int nid, int crit, const ASN1_OCTET_STRING *data); -// X509_EXTENSION_create_by_OBJ behaves like |X509_EXTENSION_create_by_NID|, but -// the extension type is determined by an |ASN1_OBJECT|. +// X509_EXTENSION_create_by_OBJ behaves like `X509_EXTENSION_create_by_NID`, but +// the extension type is determined by an `ASN1_OBJECT`. OPENSSL_EXPORT X509_EXTENSION *X509_EXTENSION_create_by_OBJ( X509_EXTENSION **ex, const ASN1_OBJECT *obj, int crit, const ASN1_OCTET_STRING *data); -// X509_EXTENSION_get_object returns |ex|'s extension type. This function +// X509_EXTENSION_get_object returns `ex`'s extension type. This function // returns a non-const pointer for OpenSSL compatibility, but callers should not // mutate the result. OPENSSL_EXPORT ASN1_OBJECT *X509_EXTENSION_get_object(const X509_EXTENSION *ex); -// X509_EXTENSION_get_data returns |ne|'s extension value. This function returns +// X509_EXTENSION_get_data returns `ne`'s extension value. This function returns // a non-const pointer for OpenSSL compatibility, but callers should not mutate // the result. OPENSSL_EXPORT ASN1_OCTET_STRING *X509_EXTENSION_get_data( const X509_EXTENSION *ne); -// X509_EXTENSION_get_critical returns one if |ex| is critical and zero +// X509_EXTENSION_get_critical returns one if `ex` is critical and zero // otherwise. OPENSSL_EXPORT int X509_EXTENSION_get_critical(const X509_EXTENSION *ex); -// X509_EXTENSION_set_object sets |ex|'s extension type to |obj|. It returns one +// X509_EXTENSION_set_object sets `ex`'s extension type to `obj`. It returns one // on success and zero on error. OPENSSL_EXPORT int X509_EXTENSION_set_object(X509_EXTENSION *ex, const ASN1_OBJECT *obj); -// X509_EXTENSION_set_critical sets |ex| to critical if |crit| is non-zero and -// to non-critical if |crit| is zero. +// X509_EXTENSION_set_critical sets `ex` to critical if `crit` is non-zero and +// to non-critical if `crit` is zero. OPENSSL_EXPORT int X509_EXTENSION_set_critical(X509_EXTENSION *ex, int crit); -// X509_EXTENSION_set_data set's |ex|'s extension value to a copy of |data|. It +// X509_EXTENSION_set_data sets `ex`'s extension value to a copy of `data`. It // returns one on success and zero on error. OPENSSL_EXPORT int X509_EXTENSION_set_data(X509_EXTENSION *ex, const ASN1_OCTET_STRING *data); @@ -1698,70 +1705,70 @@ OPENSSL_EXPORT int X509_EXTENSION_set_data(X509_EXTENSION *ex, // Extension lists. // // The following functions manipulate lists of extensions. Most of them have -// corresponding functions on the containing |X509|, |X509_CRL|, or -// |X509_REVOKED|. +// corresponding functions on the containing `X509`, `X509_CRL`, or +// `X509_REVOKED`. DEFINE_STACK_OF(X509_EXTENSION) typedef STACK_OF(X509_EXTENSION) X509_EXTENSIONS; -// d2i_X509_EXTENSIONS parses up to |len| bytes from |*inp| as a DER-encoded -// SEQUENCE OF Extension (RFC 5280), as described in |d2i_SAMPLE|. +// d2i_X509_EXTENSIONS parses up to `len` bytes from `*inp` as a DER-encoded +// SEQUENCE OF Extension (RFC 5280), as described in `d2i_SAMPLE`. OPENSSL_EXPORT X509_EXTENSIONS *d2i_X509_EXTENSIONS(X509_EXTENSIONS **out, const uint8_t **inp, long len); -// i2d_X509_EXTENSIONS marshals |alg| as a DER-encoded SEQUENCE OF Extension -// (RFC 5280), as described in |i2d_SAMPLE|. +// i2d_X509_EXTENSIONS marshals `alg` as a DER-encoded SEQUENCE OF Extension +// (RFC 5280), as described in `i2d_SAMPLE`. OPENSSL_EXPORT int i2d_X509_EXTENSIONS(const X509_EXTENSIONS *alg, uint8_t **outp); -// X509v3_get_ext_count returns the number of extensions in |x|. +// X509v3_get_ext_count returns the number of extensions in `x`. OPENSSL_EXPORT int X509v3_get_ext_count(const STACK_OF(X509_EXTENSION) *x); -// X509v3_get_ext_by_NID returns the index of the first extension in |x| with -// type |nid|, or a negative number if not found. If found, callers can use -// |X509v3_get_ext| to look up the extension by index. +// X509v3_get_ext_by_NID returns the index of the first extension in `x` with +// type `nid`, or a negative number if not found. If found, callers can use +// `X509v3_get_ext` to look up the extension by index. // -// If |lastpos| is non-negative, it begins searching at |lastpos| + 1. Callers +// If `lastpos` is non-negative, it begins searching at `lastpos` + 1. Callers // can thus loop over all matching extensions by first passing -1 and then // passing the previously-returned value until no match is returned. OPENSSL_EXPORT int X509v3_get_ext_by_NID(const STACK_OF(X509_EXTENSION) *x, int nid, int lastpos); -// X509v3_get_ext_by_OBJ behaves like |X509v3_get_ext_by_NID| but looks for -// extensions matching |obj|. +// X509v3_get_ext_by_OBJ behaves like `X509v3_get_ext_by_NID` but looks for +// extensions matching `obj`. OPENSSL_EXPORT int X509v3_get_ext_by_OBJ(const STACK_OF(X509_EXTENSION) *x, const ASN1_OBJECT *obj, int lastpos); -// X509v3_get_ext_by_critical returns the index of the first extension in |x| -// whose critical bit matches |crit|, or a negative number if no such extension +// X509v3_get_ext_by_critical returns the index of the first extension in `x` +// whose critical bit matches `crit`, or a negative number if no such extension // was found. // -// If |lastpos| is non-negative, it begins searching at |lastpos| + 1. Callers +// If `lastpos` is non-negative, it begins searching at `lastpos` + 1. Callers // can thus loop over all matching extensions by first passing -1 and then // passing the previously-returned value until no match is returned. OPENSSL_EXPORT int X509v3_get_ext_by_critical(const STACK_OF(X509_EXTENSION) *x, int crit, int lastpos); -// X509v3_get_ext returns the extension in |x| at index |loc|, or NULL if |loc| +// X509v3_get_ext returns the extension in `x` at index `loc`, or NULL if `loc` // is out of bounds. This function returns a non-const pointer for OpenSSL // compatibility, but callers should not mutate the result. OPENSSL_EXPORT X509_EXTENSION *X509v3_get_ext(const STACK_OF(X509_EXTENSION) *x, int loc); -// X509v3_delete_ext removes the extension in |x| at index |loc| and returns the -// removed extension, or NULL if |loc| was out of bounds. If an extension was -// returned, the caller must release it with |X509_EXTENSION_free|. +// X509v3_delete_ext removes the extension in `x` at index `loc` and returns the +// removed extension, or NULL if `loc` was out of bounds. If an extension was +// returned, the caller must release it with `X509_EXTENSION_free`. OPENSSL_EXPORT X509_EXTENSION *X509v3_delete_ext(STACK_OF(X509_EXTENSION) *x, int loc); -// X509v3_add_ext adds a copy of |ex| to the extension list in |*x|. If |*x| is -// NULL, it allocates a new |STACK_OF(X509_EXTENSION)| to hold the copy and sets -// |*x| to the new list. It returns |*x| on success and NULL on error. The -// caller retains ownership of |ex| and can release it independently of |*x|. +// X509v3_add_ext adds a copy of `ex` to the extension list in `*x`. If `*x` is +// NULL, it allocates a new `STACK_OF(X509_EXTENSION)` to hold the copy and sets +// `*x` to the new list. It returns `*x` on success and NULL on error. The +// caller retains ownership of `ex` and can release it independently of `*x`. // -// The new extension is inserted at index |loc|, shifting extensions to the -// right. If |loc| is -1 or out of bounds, the new extension is appended to the +// The new extension is inserted at index `loc`, shifting extensions to the +// right. If `loc` is -1 or out of bounds, the new extension is appended to the // list. OPENSSL_EXPORT STACK_OF(X509_EXTENSION) *X509v3_add_ext( STACK_OF(X509_EXTENSION) **x, const X509_EXTENSION *ex, int loc); @@ -1772,111 +1779,111 @@ OPENSSL_EXPORT STACK_OF(X509_EXTENSION) *X509v3_add_ext( // Several functions in the library encode and decode extension values into a // C structure to that extension. The following extensions are supported: // -// - |NID_authority_key_identifier| with type |AUTHORITY_KEYID| -// - |NID_basic_constraints| with type |BASIC_CONSTRAINTS| -// - |NID_certificate_issuer| with type |GENERAL_NAMES| -// - |NID_certificate_policies| with type |CERTIFICATEPOLICIES| -// - |NID_crl_distribution_points| with type |CRL_DIST_POINTS| -// - |NID_crl_number| with type |ASN1_INTEGER| -// - |NID_crl_reason| with type |ASN1_ENUMERATED| -// - |NID_delta_crl| with type |ASN1_INTEGER| -// - |NID_ext_key_usage| with type |EXTENDED_KEY_USAGE| -// - |NID_freshest_crl| with type |ISSUING_DIST_POINT| -// - |NID_id_pkix_OCSP_noCheck| with type |ASN1_NULL| -// - |NID_info_access| with type |AUTHORITY_INFO_ACCESS| -// - |NID_inhibit_any_policy| with type |ASN1_INTEGER| -// - |NID_invalidity_date| with type |ASN1_GENERALIZEDTIME| -// - |NID_issuer_alt_name| with type |GENERAL_NAMES| -// - |NID_issuing_distribution_point| with type |ISSUING_DIST_POINT| -// - |NID_key_usage| with type |ASN1_BIT_STRING| -// - |NID_name_constraints| with type |NAME_CONSTRAINTS| -// - |NID_netscape_base_url| with type |ASN1_IA5STRING| -// - |NID_netscape_ca_policy_url| with type |ASN1_IA5STRING| -// - |NID_netscape_ca_revocation_url| with type |ASN1_IA5STRING| -// - |NID_netscape_cert_type| with type |ASN1_BIT_STRING| -// - |NID_netscape_comment| with type |ASN1_IA5STRING| -// - |NID_netscape_renewal_url| with type |ASN1_IA5STRING| -// - |NID_netscape_revocation_url| with type |ASN1_IA5STRING| -// - |NID_netscape_ssl_server_name| with type |ASN1_IA5STRING| -// - |NID_policy_constraints| with type |POLICY_CONSTRAINTS| -// - |NID_policy_mappings| with type |POLICY_MAPPINGS| -// - |NID_sinfo_access| with type |AUTHORITY_INFO_ACCESS| -// - |NID_subject_alt_name| with type |GENERAL_NAMES| -// - |NID_subject_key_identifier| with type |ASN1_OCTET_STRING| +// - `NID_authority_key_identifier` with type `AUTHORITY_KEYID` +// - `NID_basic_constraints` with type `BASIC_CONSTRAINTS` +// - `NID_certificate_issuer` with type `GENERAL_NAMES` +// - `NID_certificate_policies` with type `CERTIFICATEPOLICIES` +// - `NID_crl_distribution_points` with type `CRL_DIST_POINTS` +// - `NID_crl_number` with type `ASN1_INTEGER` +// - `NID_crl_reason` with type `ASN1_ENUMERATED` +// - `NID_delta_crl` with type `ASN1_INTEGER` +// - `NID_ext_key_usage` with type `EXTENDED_KEY_USAGE` +// - `NID_freshest_crl` with type `ISSUING_DIST_POINT` +// - `NID_id_pkix_OCSP_noCheck` with type `ASN1_NULL` +// - `NID_info_access` with type `AUTHORITY_INFO_ACCESS` +// - `NID_inhibit_any_policy` with type `ASN1_INTEGER` +// - `NID_invalidity_date` with type `ASN1_GENERALIZEDTIME` +// - `NID_issuer_alt_name` with type `GENERAL_NAMES` +// - `NID_issuing_distribution_point` with type `ISSUING_DIST_POINT` +// - `NID_key_usage` with type `ASN1_BIT_STRING` +// - `NID_name_constraints` with type `NAME_CONSTRAINTS` +// - `NID_netscape_base_url` with type `ASN1_IA5STRING` +// - `NID_netscape_ca_policy_url` with type `ASN1_IA5STRING` +// - `NID_netscape_ca_revocation_url` with type `ASN1_IA5STRING` +// - `NID_netscape_cert_type` with type `ASN1_BIT_STRING` +// - `NID_netscape_comment` with type `ASN1_IA5STRING` +// - `NID_netscape_renewal_url` with type `ASN1_IA5STRING` +// - `NID_netscape_revocation_url` with type `ASN1_IA5STRING` +// - `NID_netscape_ssl_server_name` with type `ASN1_IA5STRING` +// - `NID_policy_constraints` with type `POLICY_CONSTRAINTS` +// - `NID_policy_mappings` with type `POLICY_MAPPINGS` +// - `NID_sinfo_access` with type `AUTHORITY_INFO_ACCESS` +// - `NID_subject_alt_name` with type `GENERAL_NAMES` +// - `NID_subject_key_identifier` with type `ASN1_OCTET_STRING` // // If an extension does not appear in this list, e.g. for a custom extension, -// callers can instead use functions such as |X509_get_ext_by_OBJ|, -// |X509_EXTENSION_get_data|, and |X509_EXTENSION_create_by_OBJ| to inspect or -// create extensions directly. Although the |X509V3_EXT_METHOD| mechanism allows +// callers can instead use functions such as `X509_get_ext_by_OBJ`, +// `X509_EXTENSION_get_data`, and `X509_EXTENSION_create_by_OBJ` to inspect or +// create extensions directly. Although the `X509V3_EXT_METHOD` mechanism allows // registering custom extensions, doing so is deprecated and may result in // threading or memory errors. -// X509V3_EXT_d2i decodes |ext| and returns a pointer to a newly-allocated +// X509V3_EXT_d2i decodes `ext` and returns a pointer to a newly-allocated // structure, with type dependent on the type of the extension. It returns NULL -// if |ext| is an unsupported extension or if there was a syntax error in the +// if `ext` is an unsupported extension or if there was a syntax error in the // extension. The caller should cast the return value to the expected type and // free the structure when done. // // WARNING: Casting the return value to the wrong type is a potentially // exploitable memory error, so callers must not use this function before -// checking |ext| is of a known type. See the list at the top of this section +// checking `ext` is of a known type. See the list at the top of this section // for the correct types. OPENSSL_EXPORT void *X509V3_EXT_d2i(const X509_EXTENSION *ext); -// X509V3_get_d2i finds and decodes the extension in |extensions| of type |nid|. +// X509V3_get_d2i finds and decodes the extension in `extensions` of type `nid`. // If found, it decodes it and returns a newly-allocated structure, with type -// dependent on |nid|. If the extension is not found or on error, it returns -// NULL. The caller may distinguish these cases using the |out_critical| value. +// dependent on `nid`. If the extension is not found or on error, it returns +// NULL. The caller may distinguish these cases using the `out_critical` value. // -// If |out_critical| is not NULL, this function sets |*out_critical| to one if +// If `out_critical` is not NULL, this function sets `*out_critical` to one if // the extension is found and critical, zero if it is found and not critical, -1 // if it is not found, and -2 if there is an invalid duplicate extension. Note -// this function may set |*out_critical| to one or zero and still return NULL if +// this function may set `*out_critical` to one or zero and still return NULL if // the extension is found but has a syntax error. // -// If |out_idx| is not NULL, this function looks for the first occurrence of the -// extension after |*out_idx|. It then sets |*out_idx| to the index of the -// extension, or -1 if not found. If |out_idx| is non-NULL, duplicate extensions +// If `out_idx` is not NULL, this function looks for the first occurrence of the +// extension after `*out_idx`. It then sets `*out_idx` to the index of the +// extension, or -1 if not found. If `out_idx` is non-NULL, duplicate extensions // are not treated as an error. Callers, however, should not rely on this // behavior as it may be removed in the future. Duplicate extensions are // forbidden in RFC 5280. // // WARNING: This function is difficult to use correctly. Callers should pass a -// non-NULL |out_critical| and check both the return value and |*out_critical| -// to handle errors. If the return value is NULL and |*out_critical| is not -1, +// non-NULL `out_critical` and check both the return value and `*out_critical` +// to handle errors. If the return value is NULL and `*out_critical` is not -1, // there was an error. Otherwise, the function succeeded and but may return NULL -// for a missing extension. Callers should pass NULL to |out_idx| so that +// for a missing extension. Callers should pass NULL to `out_idx` so that // duplicate extensions are handled correctly. // // Additionally, casting the return value to the wrong type is a potentially -// exploitable memory error, so callers must ensure the cast and |nid| match. +// exploitable memory error, so callers must ensure the cast and `nid` match. // See the list at the top of this section for the correct types. OPENSSL_EXPORT void *X509V3_get_d2i(const STACK_OF(X509_EXTENSION) *extensions, int nid, int *out_critical, int *out_idx); -// X509V3_EXT_free casts |ext_data| into the type that corresponds to |nid| and +// X509V3_EXT_free casts `ext_data` into the type that corresponds to `nid` and // releases memory associated with it. It returns one on success and zero if -// |nid| is not a known extension. +// `nid` is not a known extension. // -// WARNING: Casting |ext_data| to the wrong type is a potentially exploitable -// memory error, so callers must ensure |ext_data|'s type matches |nid|. See the +// WARNING: Casting `ext_data` to the wrong type is a potentially exploitable +// memory error, so callers must ensure `ext_data`'s type matches `nid`. See the // list at the top of this section for the correct types. // // TODO(davidben): OpenSSL upstream no longer exposes this function. Remove it? OPENSSL_EXPORT int X509V3_EXT_free(int nid, void *ext_data); -// X509V3_EXT_i2d casts |ext_struc| into the type that corresponds to -// |ext_nid|, serializes it, and returns a newly-allocated |X509_EXTENSION| -// object containing the serialization, or NULL on error. The |X509_EXTENSION| -// has OID |ext_nid| and is critical if |crit| is one. +// X509V3_EXT_i2d casts `ext_struc` into the type that corresponds to +// `ext_nid`, serializes it, and returns a newly-allocated `X509_EXTENSION` +// object containing the serialization, or NULL on error. The `X509_EXTENSION` +// has OID `ext_nid` and is critical if `crit` is one. // -// WARNING: Casting |ext_struc| to the wrong type is a potentially exploitable -// memory error, so callers must ensure |ext_struct|'s type matches |ext_nid|. +// WARNING: Casting `ext_struc` to the wrong type is a potentially exploitable +// memory error, so callers must ensure `ext_struct`'s type matches `ext_nid`. // See the list at the top of this section for the correct types. OPENSSL_EXPORT X509_EXTENSION *X509V3_EXT_i2d(int ext_nid, int crit, void *ext_struc); -// The following constants control the behavior of |X509V3_add1_i2d| and related +// The following constants control the behavior of `X509V3_add1_i2d` and related // functions. // X509V3_ADD_OP_MASK can be ANDed with the flags to determine how duplicate @@ -1888,7 +1895,7 @@ OPENSSL_EXPORT X509_EXTENSION *X509V3_EXT_i2d(int ext_nid, int crit, #define X509V3_ADD_DEFAULT 0L // X509V3_ADD_APPEND causes the function to unconditionally appended the new -// extension to to the extensions list, even if there is a duplicate. +// extension to the extensions list, even if there is a duplicate. #define X509V3_ADD_APPEND 1L // X509V3_ADD_REPLACE causes the function to replace the existing extension, or @@ -1905,7 +1912,7 @@ OPENSSL_EXPORT X509_EXTENSION *X509V3_EXT_i2d(int ext_nid, int crit, // X509V3_ADD_DELETE causes the function to remove the matching extension. No // new extension is added. If there is no matching extension, the function -// fails. The |value| parameter is ignored in this mode. +// fails. The `value` parameter is ignored in this mode. #define X509V3_ADD_DELETE 5L // X509V3_ADD_SILENT may be ORed into one of the values above to indicate the @@ -1914,18 +1921,18 @@ OPENSSL_EXPORT X509_EXTENSION *X509V3_EXT_i2d(int ext_nid, int crit, // continue to return -1 and add to the error queue on other errors. #define X509V3_ADD_SILENT 0x10 -// X509V3_add1_i2d casts |value| to the type that corresponds to |nid|, -// serializes it, and appends it to the extension list in |*x|. If |*x| is NULL, -// it will set |*x| to a newly-allocated |STACK_OF(X509_EXTENSION)| as needed. -// The |crit| parameter determines whether the new extension is critical. -// |flags| may be some combination of the |X509V3_ADD_*| constants to control +// X509V3_add1_i2d casts `value` to the type that corresponds to `nid`, +// serializes it, and appends it to the extension list in `*x`. If `*x` is NULL, +// it will set `*x` to a newly-allocated `STACK_OF(X509_EXTENSION)` as needed. +// The `crit` parameter determines whether the new extension is critical. +// `flags` may be some combination of the `X509V3_ADD_*` constants to control // the function's behavior on duplicate extension. // // This function returns one on success, zero if the operation failed due to a // missing or duplicate extension, and -1 on other errors. // -// WARNING: Casting |value| to the wrong type is a potentially exploitable -// memory error, so callers must ensure |value|'s type matches |nid|. See the +// WARNING: Casting `value` to the wrong type is a potentially exploitable +// memory error, so callers must ensure `value`'s type matches `nid`. See the // list at the top of this section for the correct types. OPENSSL_EXPORT int X509V3_add1_i2d(STACK_OF(X509_EXTENSION) **x, int nid, void *value, int crit, unsigned long flags); @@ -1937,32 +1944,32 @@ OPENSSL_EXPORT int X509V3_add1_i2d(STACK_OF(X509_EXTENSION) **x, int nid, // whether a certificate is a CA certificate and, if so, optionally constrains // the maximum depth of the certificate chain. -// A BASIC_CONSTRAINTS_st, aka |BASIC_CONSTRAINTS| represents an +// A BASIC_CONSTRAINTS_st, aka `BASIC_CONSTRAINTS` represents an // BasicConstraints structure (RFC 5280). struct BASIC_CONSTRAINTS_st { ASN1_BOOLEAN ca; ASN1_INTEGER *pathlen; } /* BASIC_CONSTRAINTS */; -// BASIC_CONSTRAINTS is an |ASN1_ITEM| whose ASN.1 type is BasicConstraints (RFC -// 5280) and C type is |BASIC_CONSTRAINTS*|. +// BASIC_CONSTRAINTS is an `ASN1_ITEM` whose ASN.1 type is BasicConstraints (RFC +// 5280) and C type is `BASIC_CONSTRAINTS*`. DECLARE_ASN1_ITEM(BASIC_CONSTRAINTS) -// BASIC_CONSTRAINTS_new returns a newly-allocated, empty |BASIC_CONSTRAINTS| +// BASIC_CONSTRAINTS_new returns a newly-allocated, empty `BASIC_CONSTRAINTS` // object, or NULL on error. OPENSSL_EXPORT BASIC_CONSTRAINTS *BASIC_CONSTRAINTS_new(void); -// BASIC_CONSTRAINTS_free releases memory associated with |bcons|. +// BASIC_CONSTRAINTS_free releases memory associated with `bcons`. OPENSSL_EXPORT void BASIC_CONSTRAINTS_free(BASIC_CONSTRAINTS *bcons); -// d2i_BASIC_CONSTRAINTS parses up to |len| bytes from |*inp| as a DER-encoded -// BasicConstraints (RFC 5280), as described in |d2i_SAMPLE|. +// d2i_BASIC_CONSTRAINTS parses up to `len` bytes from `*inp` as a DER-encoded +// BasicConstraints (RFC 5280), as described in `d2i_SAMPLE`. OPENSSL_EXPORT BASIC_CONSTRAINTS *d2i_BASIC_CONSTRAINTS(BASIC_CONSTRAINTS **out, const uint8_t **inp, long len); -// i2d_BASIC_CONSTRAINTS marshals |bcons| as a DER-encoded BasicConstraints (RFC -// 5280), as described in |i2d_SAMPLE|. +// i2d_BASIC_CONSTRAINTS marshals `bcons` as a DER-encoded BasicConstraints (RFC +// 5280), as described in `i2d_SAMPLE`. OPENSSL_EXPORT int i2d_BASIC_CONSTRAINTS(const BASIC_CONSTRAINTS *bcons, uint8_t **outp); @@ -1975,39 +1982,39 @@ OPENSSL_EXPORT int i2d_BASIC_CONSTRAINTS(const BASIC_CONSTRAINTS *bcons, typedef STACK_OF(ASN1_OBJECT) EXTENDED_KEY_USAGE; -// EXTENDED_KEY_USAGE is an |ASN1_ITEM| whose ASN.1 type is ExtKeyUsageSyntax -// (RFC 5280) and C type is |STACK_OF(ASN1_OBJECT)*|, or |EXTENDED_KEY_USAGE*|. +// EXTENDED_KEY_USAGE is an `ASN1_ITEM` whose ASN.1 type is ExtKeyUsageSyntax +// (RFC 5280) and C type is `STACK_OF(ASN1_OBJECT)*`, or `EXTENDED_KEY_USAGE*`. DECLARE_ASN1_ITEM(EXTENDED_KEY_USAGE) -// EXTENDED_KEY_USAGE_new returns a newly-allocated, empty |EXTENDED_KEY_USAGE| +// EXTENDED_KEY_USAGE_new returns a newly-allocated, empty `EXTENDED_KEY_USAGE` // object, or NULL on error. OPENSSL_EXPORT EXTENDED_KEY_USAGE *EXTENDED_KEY_USAGE_new(void); -// EXTENDED_KEY_USAGE_free releases memory associated with |eku|. +// EXTENDED_KEY_USAGE_free releases memory associated with `eku`. OPENSSL_EXPORT void EXTENDED_KEY_USAGE_free(EXTENDED_KEY_USAGE *eku); -// d2i_EXTENDED_KEY_USAGE parses up to |len| bytes from |*inp| as a DER-encoded -// ExtKeyUsageSyntax (RFC 5280), as described in |d2i_SAMPLE|. +// d2i_EXTENDED_KEY_USAGE parses up to `len` bytes from `*inp` as a DER-encoded +// ExtKeyUsageSyntax (RFC 5280), as described in `d2i_SAMPLE`. OPENSSL_EXPORT EXTENDED_KEY_USAGE *d2i_EXTENDED_KEY_USAGE( EXTENDED_KEY_USAGE **out, const uint8_t **inp, long len); -// i2d_EXTENDED_KEY_USAGE marshals |eku| as a DER-encoded ExtKeyUsageSyntax (RFC -// 5280), as described in |i2d_SAMPLE|. +// i2d_EXTENDED_KEY_USAGE marshals `eku` as a DER-encoded ExtKeyUsageSyntax (RFC +// 5280), as described in `i2d_SAMPLE`. OPENSSL_EXPORT int i2d_EXTENDED_KEY_USAGE(const EXTENDED_KEY_USAGE *eku, uint8_t **outp); // General names. // -// A |GENERAL_NAME| represents an X.509 GeneralName structure, defined in RFC -// 5280, Section 4.2.1.6. General names are distinct from names (|X509_NAME|). A +// A `GENERAL_NAME` represents an X.509 GeneralName structure, defined in RFC +// 5280, Section 4.2.1.6. General names are distinct from names (`X509_NAME`). A // general name is a CHOICE type which may contain one of several name types, // most commonly a DNS name or an IP address. General names most commonly appear // in the subject alternative name (SAN) extension, though they are also used in // other extensions. // // Many extensions contain a SEQUENCE OF GeneralName, or GeneralNames, so -// |STACK_OF(GENERAL_NAME)| is defined and aliased to |GENERAL_NAMES|. +// `STACK_OF(GENERAL_NAME)` is defined and aliased to `GENERAL_NAMES`. typedef struct otherName_st { ASN1_OBJECT *type_id; @@ -2019,7 +2026,7 @@ typedef struct EDIPartyName_st { ASN1_STRING *partyName; } EDIPARTYNAME; -// GEN_* are constants for the |type| field of |GENERAL_NAME|, defined below. +// GEN_* are constants for the `type` field of `GENERAL_NAME`, defined below. #define GEN_OTHERNAME 0 #define GEN_EMAIL 1 #define GEN_DNS 2 @@ -2030,13 +2037,13 @@ typedef struct EDIPartyName_st { #define GEN_IPADD 7 #define GEN_RID 8 -// A GENERAL_NAME_st, aka |GENERAL_NAME|, represents an X.509 GeneralName. The -// |type| field determines which member of |d| is active. A |GENERAL_NAME| may -// also be empty, in which case |type| is -1 and |d| is NULL. Empty -// |GENERAL_NAME|s are invalid and will never be returned from the parser, but -// may be created temporarily, e.g. by |GENERAL_NAME_new|. +// A GENERAL_NAME_st, aka `GENERAL_NAME`, represents an X.509 GeneralName. The +// `type` field determines which member of `d` is active. A `GENERAL_NAME` may +// also be empty, in which case `type` is -1 and `d` is NULL. Empty +// `GENERAL_NAME`s are invalid and will never be returned from the parser, but +// may be created temporarily, e.g. by `GENERAL_NAME_new`. // -// WARNING: |type| and |d| must be kept consistent. An inconsistency will result +// WARNING: `type` and `d` must be kept consistent. An inconsistency will result // in a potentially exploitable memory error. struct GENERAL_NAME_st { int type; @@ -2060,75 +2067,75 @@ struct GENERAL_NAME_st { } d; } /* GENERAL_NAME */; -// GENERAL_NAME_new returns a new, empty |GENERAL_NAME|, or NULL on error. +// GENERAL_NAME_new returns a new, empty `GENERAL_NAME`, or NULL on error. OPENSSL_EXPORT GENERAL_NAME *GENERAL_NAME_new(void); -// GENERAL_NAME_free releases memory associated with |gen|. +// GENERAL_NAME_free releases memory associated with `gen`. OPENSSL_EXPORT void GENERAL_NAME_free(GENERAL_NAME *gen); -// d2i_GENERAL_NAME parses up to |len| bytes from |*inp| as a DER-encoded X.509 -// GeneralName (RFC 5280), as described in |d2i_SAMPLE|. +// d2i_GENERAL_NAME parses up to `len` bytes from `*inp` as a DER-encoded X.509 +// GeneralName (RFC 5280), as described in `d2i_SAMPLE`. OPENSSL_EXPORT GENERAL_NAME *d2i_GENERAL_NAME(GENERAL_NAME **out, const uint8_t **inp, long len); -// i2d_GENERAL_NAME marshals |in| as a DER-encoded X.509 GeneralName (RFC 5280), -// as described in |i2d_SAMPLE|. +// i2d_GENERAL_NAME marshals `in` as a DER-encoded X.509 GeneralName (RFC 5280), +// as described in `i2d_SAMPLE`. OPENSSL_EXPORT int i2d_GENERAL_NAME(const GENERAL_NAME *in, uint8_t **outp); -// GENERAL_NAME_dup returns a newly-allocated copy of |gen|, or NULL on error. -// This function works by serializing the structure, so it will fail if |gen| is +// GENERAL_NAME_dup returns a newly-allocated copy of `gen`, or NULL on error. +// This function works by serializing the structure, so it will fail if `gen` is // empty. OPENSSL_EXPORT GENERAL_NAME *GENERAL_NAME_dup(const GENERAL_NAME *gen); -// GENERAL_NAMES_new returns a new, empty |GENERAL_NAMES|, or NULL on error. +// GENERAL_NAMES_new returns a new, empty `GENERAL_NAMES`, or NULL on error. OPENSSL_EXPORT GENERAL_NAMES *GENERAL_NAMES_new(void); -// GENERAL_NAMES_free releases memory associated with |gens|. +// GENERAL_NAMES_free releases memory associated with `gens`. OPENSSL_EXPORT void GENERAL_NAMES_free(GENERAL_NAMES *gens); -// d2i_GENERAL_NAMES parses up to |len| bytes from |*inp| as a DER-encoded -// SEQUENCE OF GeneralName, as described in |d2i_SAMPLE|. +// d2i_GENERAL_NAMES parses up to `len` bytes from `*inp` as a DER-encoded +// SEQUENCE OF GeneralName, as described in `d2i_SAMPLE`. OPENSSL_EXPORT GENERAL_NAMES *d2i_GENERAL_NAMES(GENERAL_NAMES **out, const uint8_t **inp, long len); -// i2d_GENERAL_NAMES marshals |in| as a DER-encoded SEQUENCE OF GeneralName, as -// described in |i2d_SAMPLE|. +// i2d_GENERAL_NAMES marshals `in` as a DER-encoded SEQUENCE OF GeneralName, as +// described in `i2d_SAMPLE`. OPENSSL_EXPORT int i2d_GENERAL_NAMES(const GENERAL_NAMES *in, uint8_t **outp); -// OTHERNAME_new returns a new, empty |OTHERNAME|, or NULL on error. +// OTHERNAME_new returns a new, empty `OTHERNAME`, or NULL on error. OPENSSL_EXPORT OTHERNAME *OTHERNAME_new(void); -// OTHERNAME_free releases memory associated with |name|. +// OTHERNAME_free releases memory associated with `name`. OPENSSL_EXPORT void OTHERNAME_free(OTHERNAME *name); -// EDIPARTYNAME_new returns a new, empty |EDIPARTYNAME|, or NULL on error. +// EDIPARTYNAME_new returns a new, empty `EDIPARTYNAME`, or NULL on error. // EDIPartyName is rarely used in practice, so callers are unlikely to need this // function. OPENSSL_EXPORT EDIPARTYNAME *EDIPARTYNAME_new(void); -// EDIPARTYNAME_free releases memory associated with |name|. EDIPartyName is +// EDIPARTYNAME_free releases memory associated with `name`. EDIPartyName is // rarely used in practice, so callers are unlikely to need this function. OPENSSL_EXPORT void EDIPARTYNAME_free(EDIPARTYNAME *name); -// GENERAL_NAME_set0_value set |gen|'s type and value to |type| and |value|. -// |type| must be a |GEN_*| constant and |value| must be an object of the -// corresponding type. |gen| takes ownership of |value|, so |value| must have +// GENERAL_NAME_set0_value set `gen`'s type and value to `type` and `value`. +// `type` must be a `GEN_*` constant and `value` must be an object of the +// corresponding type. `gen` takes ownership of `value`, so `value` must have // been an allocated object. // -// WARNING: |gen| must be empty (typically as returned from |GENERAL_NAME_new|) -// before calling this function. If |gen| already contained a value, the +// WARNING: `gen` must be empty (typically as returned from `GENERAL_NAME_new`) +// before calling this function. If `gen` already contained a value, the // previous contents will be leaked. OPENSSL_EXPORT void GENERAL_NAME_set0_value(GENERAL_NAME *gen, int type, void *value); -// GENERAL_NAME_get0_value returns the in-memory representation of |gen|'s -// contents and, |out_type| is not NULL, sets |*out_type| to the type of |gen|, -// which will be a |GEN_*| constant. If |gen| is incomplete, the return value +// GENERAL_NAME_get0_value returns the in-memory representation of `gen`'s +// contents and, `out_type` is not NULL, sets `*out_type` to the type of `gen`, +// which will be a `GEN_*` constant. If `gen` is incomplete, the return value // will be NULL and the type will be -1. // // WARNING: Casting the result of this function to the wrong type is a -// potentially exploitable memory error. Callers must check |gen|'s type, either -// via |*out_type| or checking |gen->type| directly, before inspecting the +// potentially exploitable memory error. Callers must check `gen`'s type, either +// via `*out_type` or checking `gen->type` directly, before inspecting the // result. // // WARNING: This function is not const-correct. The return value should be @@ -2136,27 +2143,27 @@ OPENSSL_EXPORT void GENERAL_NAME_set0_value(GENERAL_NAME *gen, int type, OPENSSL_EXPORT void *GENERAL_NAME_get0_value(const GENERAL_NAME *gen, int *out_type); -// GENERAL_NAME_set0_othername sets |gen| to be an OtherName with type |oid| and -// value |value|. On success, it returns one and takes ownership of |oid| and -// |value|, which must be created in a way compatible with |ASN1_OBJECT_free| -// and |ASN1_TYPE_free|, respectively. On allocation failure, it returns zero. -// In the failure case, the caller retains ownership of |oid| and |value| and +// GENERAL_NAME_set0_othername sets `gen` to be an OtherName with type `oid` and +// value `value`. On success, it returns one and takes ownership of `oid` and +// `value`, which must be created in a way compatible with `ASN1_OBJECT_free` +// and `ASN1_TYPE_free`, respectively. On allocation failure, it returns zero. +// In the failure case, the caller retains ownership of `oid` and `value` and // must release them when done. // -// WARNING: |gen| must be empty (typically as returned from |GENERAL_NAME_new|) -// before calling this function. If |gen| already contained a value, the +// WARNING: `gen` must be empty (typically as returned from `GENERAL_NAME_new`) +// before calling this function. If `gen` already contained a value, the // previously contents will be leaked. OPENSSL_EXPORT int GENERAL_NAME_set0_othername(GENERAL_NAME *gen, ASN1_OBJECT *oid, ASN1_TYPE *value); -// GENERAL_NAME_get0_otherName, if |gen| is an OtherName, sets |*out_oid| and -// |*out_value| to the OtherName's type-id and value, respectively, and returns -// one. If |gen| is not an OtherName, it returns zero and leaves |*out_oid| and -// |*out_value| unmodified. Either of |out_oid| or |out_value| may be NULL to +// GENERAL_NAME_get0_otherName, if `gen` is an OtherName, sets `*out_oid` and +// `*out_value` to the OtherName's type-id and value, respectively, and returns +// one. If `gen` is not an OtherName, it returns zero and leaves `*out_oid` and +// `*out_value` unmodified. Either of `out_oid` or `out_value` may be NULL to // ignore the value. // -// WARNING: This function is not const-correct. |out_oid| and |out_value| are +// WARNING: This function is not const-correct. `out_oid` and `out_value` are // not const, but callers should not mutate the resulting objects. OPENSSL_EXPORT int GENERAL_NAME_get0_otherName(const GENERAL_NAME *gen, ASN1_OBJECT **out_oid, @@ -2167,10 +2174,10 @@ OPENSSL_EXPORT int GENERAL_NAME_get0_otherName(const GENERAL_NAME *gen, // // The authority key identifier extension (RFC 5280, section 4.2.1.1) allows a // certificate to more precisely identify its issuer. This is helpful when -// multiple certificates share a name. Only the keyIdentifier (|keyid| in -// |AUTHORITY_KEYID|) field is used in practice. +// multiple certificates share a name. Only the keyIdentifier (`keyid` in +// `AUTHORITY_KEYID`) field is used in practice. -// A AUTHORITY_KEYID_st, aka |AUTHORITY_KEYID|, represents an +// A AUTHORITY_KEYID_st, aka `AUTHORITY_KEYID`, represents an // AuthorityKeyIdentifier structure (RFC 5280). struct AUTHORITY_KEYID_st { ASN1_OCTET_STRING *keyid; @@ -2178,25 +2185,25 @@ struct AUTHORITY_KEYID_st { ASN1_INTEGER *serial; } /* AUTHORITY_KEYID */; -// AUTHORITY_KEYID is an |ASN1_ITEM| whose ASN.1 type is AuthorityKeyIdentifier -// (RFC 5280) and C type is |AUTHORITY_KEYID*|. +// AUTHORITY_KEYID is an `ASN1_ITEM` whose ASN.1 type is AuthorityKeyIdentifier +// (RFC 5280) and C type is `AUTHORITY_KEYID*`. DECLARE_ASN1_ITEM(AUTHORITY_KEYID) -// AUTHORITY_KEYID_new returns a newly-allocated, empty |AUTHORITY_KEYID| +// AUTHORITY_KEYID_new returns a newly-allocated, empty `AUTHORITY_KEYID` // object, or NULL on error. OPENSSL_EXPORT AUTHORITY_KEYID *AUTHORITY_KEYID_new(void); -// AUTHORITY_KEYID_free releases memory associated with |akid|. +// AUTHORITY_KEYID_free releases memory associated with `akid`. OPENSSL_EXPORT void AUTHORITY_KEYID_free(AUTHORITY_KEYID *akid); -// d2i_AUTHORITY_KEYID parses up to |len| bytes from |*inp| as a DER-encoded -// AuthorityKeyIdentifier (RFC 5280), as described in |d2i_SAMPLE|. +// d2i_AUTHORITY_KEYID parses up to `len` bytes from `*inp` as a DER-encoded +// AuthorityKeyIdentifier (RFC 5280), as described in `d2i_SAMPLE`. OPENSSL_EXPORT AUTHORITY_KEYID *d2i_AUTHORITY_KEYID(AUTHORITY_KEYID **out, const uint8_t **inp, long len); -// i2d_AUTHORITY_KEYID marshals |akid| as a DER-encoded AuthorityKeyIdentifier -// (RFC 5280), as described in |i2d_SAMPLE|. +// i2d_AUTHORITY_KEYID marshals `akid` as a DER-encoded AuthorityKeyIdentifier +// (RFC 5280), as described in `i2d_SAMPLE`. OPENSSL_EXPORT int i2d_AUTHORITY_KEYID(const AUTHORITY_KEYID *akid, uint8_t **outp); @@ -2217,29 +2224,29 @@ typedef struct GENERAL_SUBTREE_st { DEFINE_STACK_OF(GENERAL_SUBTREE) -// GENERAL_SUBTREE_new returns a newly-allocated, empty |GENERAL_SUBTREE| +// GENERAL_SUBTREE_new returns a newly-allocated, empty `GENERAL_SUBTREE` // object, or NULL on error. OPENSSL_EXPORT GENERAL_SUBTREE *GENERAL_SUBTREE_new(void); -// GENERAL_SUBTREE_free releases memory associated with |subtree|. +// GENERAL_SUBTREE_free releases memory associated with `subtree`. OPENSSL_EXPORT void GENERAL_SUBTREE_free(GENERAL_SUBTREE *subtree); -// A NAME_CONSTRAINTS_st, aka |NAME_CONSTRAINTS|, represents a NameConstraints +// A NAME_CONSTRAINTS_st, aka `NAME_CONSTRAINTS`, represents a NameConstraints // structure (RFC 5280). struct NAME_CONSTRAINTS_st { STACK_OF(GENERAL_SUBTREE) *permittedSubtrees; STACK_OF(GENERAL_SUBTREE) *excludedSubtrees; } /* NAME_CONSTRAINTS */; -// NAME_CONSTRAINTS is an |ASN1_ITEM| whose ASN.1 type is NameConstraints (RFC -// 5280) and C type is |NAME_CONSTRAINTS*|. +// NAME_CONSTRAINTS is an `ASN1_ITEM` whose ASN.1 type is NameConstraints (RFC +// 5280) and C type is `NAME_CONSTRAINTS*`. DECLARE_ASN1_ITEM(NAME_CONSTRAINTS) -// NAME_CONSTRAINTS_new returns a newly-allocated, empty |NAME_CONSTRAINTS| +// NAME_CONSTRAINTS_new returns a newly-allocated, empty `NAME_CONSTRAINTS` // object, or NULL on error. OPENSSL_EXPORT NAME_CONSTRAINTS *NAME_CONSTRAINTS_new(void); -// NAME_CONSTRAINTS_free releases memory associated with |ncons|. +// NAME_CONSTRAINTS_free releases memory associated with `ncons`. OPENSSL_EXPORT void NAME_CONSTRAINTS_free(NAME_CONSTRAINTS *ncons); @@ -2259,35 +2266,35 @@ typedef struct ACCESS_DESCRIPTION_st { DEFINE_STACK_OF(ACCESS_DESCRIPTION) -// ACCESS_DESCRIPTION_new returns a newly-allocated, empty |ACCESS_DESCRIPTION| +// ACCESS_DESCRIPTION_new returns a newly-allocated, empty `ACCESS_DESCRIPTION` // object, or NULL on error. OPENSSL_EXPORT ACCESS_DESCRIPTION *ACCESS_DESCRIPTION_new(void); -// ACCESS_DESCRIPTION_free releases memory associated with |desc|. +// ACCESS_DESCRIPTION_free releases memory associated with `desc`. OPENSSL_EXPORT void ACCESS_DESCRIPTION_free(ACCESS_DESCRIPTION *desc); typedef STACK_OF(ACCESS_DESCRIPTION) AUTHORITY_INFO_ACCESS; -// AUTHORITY_INFO_ACCESS is an |ASN1_ITEM| whose ASN.1 type is +// AUTHORITY_INFO_ACCESS is an `ASN1_ITEM` whose ASN.1 type is // AuthorityInfoAccessSyntax (RFC 5280) and C type is -// |STACK_OF(ACCESS_DESCRIPTION)*|, or |AUTHORITY_INFO_ACCESS*|. +// `STACK_OF(ACCESS_DESCRIPTION)*`, or `AUTHORITY_INFO_ACCESS*`. DECLARE_ASN1_ITEM(AUTHORITY_INFO_ACCESS) // AUTHORITY_INFO_ACCESS_new returns a newly-allocated, empty -// |AUTHORITY_INFO_ACCESS| object, or NULL on error. +// `AUTHORITY_INFO_ACCESS` object, or NULL on error. OPENSSL_EXPORT AUTHORITY_INFO_ACCESS *AUTHORITY_INFO_ACCESS_new(void); -// AUTHORITY_INFO_ACCESS_free releases memory associated with |aia|. +// AUTHORITY_INFO_ACCESS_free releases memory associated with `aia`. OPENSSL_EXPORT void AUTHORITY_INFO_ACCESS_free(AUTHORITY_INFO_ACCESS *aia); -// d2i_AUTHORITY_INFO_ACCESS parses up to |len| bytes from |*inp| as a +// d2i_AUTHORITY_INFO_ACCESS parses up to `len` bytes from `*inp` as a // DER-encoded AuthorityInfoAccessSyntax (RFC 5280), as described in -// |d2i_SAMPLE|. +// `d2i_SAMPLE`. OPENSSL_EXPORT AUTHORITY_INFO_ACCESS *d2i_AUTHORITY_INFO_ACCESS( AUTHORITY_INFO_ACCESS **out, const uint8_t **inp, long len); -// i2d_AUTHORITY_INFO_ACCESS marshals |aia| as a DER-encoded -// AuthorityInfoAccessSyntax (RFC 5280), as described in |i2d_SAMPLE|. +// i2d_AUTHORITY_INFO_ACCESS marshals `aia` as a DER-encoded +// AuthorityInfoAccessSyntax (RFC 5280), as described in `i2d_SAMPLE`. OPENSSL_EXPORT int i2d_AUTHORITY_INFO_ACCESS(const AUTHORITY_INFO_ACCESS *aia, uint8_t **outp); @@ -2299,11 +2306,11 @@ OPENSSL_EXPORT int i2d_AUTHORITY_INFO_ACCESS(const AUTHORITY_INFO_ACCESS *aia, // point CRL extension (RFC 5280, section 5.2.5) matches against this extension. // A DIST_POINT_NAME represents a DistributionPointName structure (RFC 5280). -// The |name| field contains the CHOICE value and is determined by |type|. If -// |type| is zero, |name| must be a |fullname|. If |type| is one, |name| must be -// a |relativename|. +// The `name` field contains the CHOICE value and is determined by `type`. If +// `type` is zero, `name` must be a `fullname`. If `type` is one, `name` must be +// a `relativename`. // -// WARNING: |type| and |name| must be kept consistent. An inconsistency will +// WARNING: `type` and `name` must be kept consistent. An inconsistency will // result in a potentially exploitable memory error. typedef struct DIST_POINT_NAME_st { int type; @@ -2311,18 +2318,16 @@ typedef struct DIST_POINT_NAME_st { GENERAL_NAMES *fullname; STACK_OF(X509_NAME_ENTRY) *relativename; } name; - // If relativename then this contains the full distribution point name - X509_NAME *dpname; } DIST_POINT_NAME; -// DIST_POINT_NAME_new returns a newly-allocated, empty |DIST_POINT_NAME| +// DIST_POINT_NAME_new returns a newly-allocated, empty `DIST_POINT_NAME` // object, or NULL on error. OPENSSL_EXPORT DIST_POINT_NAME *DIST_POINT_NAME_new(void); -// DIST_POINT_NAME_free releases memory associated with |name|. +// DIST_POINT_NAME_free releases memory associated with `name`. OPENSSL_EXPORT void DIST_POINT_NAME_free(DIST_POINT_NAME *name); -// A DIST_POINT_st, aka |DIST_POINT|, represents a DistributionPoint structure +// A DIST_POINT_st, aka `DIST_POINT`, represents a DistributionPoint structure // (RFC 5280). struct DIST_POINT_st { DIST_POINT_NAME *distpoint; @@ -2332,38 +2337,38 @@ struct DIST_POINT_st { DEFINE_STACK_OF(DIST_POINT) -// DIST_POINT_new returns a newly-allocated, empty |DIST_POINT| object, or NULL +// DIST_POINT_new returns a newly-allocated, empty `DIST_POINT` object, or NULL // on error. OPENSSL_EXPORT DIST_POINT *DIST_POINT_new(void); -// DIST_POINT_free releases memory associated with |dp|. +// DIST_POINT_free releases memory associated with `dp`. OPENSSL_EXPORT void DIST_POINT_free(DIST_POINT *dp); typedef STACK_OF(DIST_POINT) CRL_DIST_POINTS; -// CRL_DIST_POINTS is an |ASN1_ITEM| whose ASN.1 type is CRLDistributionPoints -// (RFC 5280) and C type is |CRL_DIST_POINTS*|. +// CRL_DIST_POINTS is an `ASN1_ITEM` whose ASN.1 type is CRLDistributionPoints +// (RFC 5280) and C type is `CRL_DIST_POINTS*`. DECLARE_ASN1_ITEM(CRL_DIST_POINTS) -// CRL_DIST_POINTS_new returns a newly-allocated, empty |CRL_DIST_POINTS| +// CRL_DIST_POINTS_new returns a newly-allocated, empty `CRL_DIST_POINTS` // object, or NULL on error. OPENSSL_EXPORT CRL_DIST_POINTS *CRL_DIST_POINTS_new(void); -// CRL_DIST_POINTS_free releases memory associated with |crldp|. +// CRL_DIST_POINTS_free releases memory associated with `crldp`. OPENSSL_EXPORT void CRL_DIST_POINTS_free(CRL_DIST_POINTS *crldp); -// d2i_CRL_DIST_POINTS parses up to |len| bytes from |*inp| as a DER-encoded -// CRLDistributionPoints (RFC 5280), as described in |d2i_SAMPLE|. +// d2i_CRL_DIST_POINTS parses up to `len` bytes from `*inp` as a DER-encoded +// CRLDistributionPoints (RFC 5280), as described in `d2i_SAMPLE`. OPENSSL_EXPORT CRL_DIST_POINTS *d2i_CRL_DIST_POINTS(CRL_DIST_POINTS **out, const uint8_t **inp, long len); -// i2d_CRL_DIST_POINTS marshals |crldp| as a DER-encoded CRLDistributionPoints -// (RFC 5280), as described in |i2d_SAMPLE|. +// i2d_CRL_DIST_POINTS marshals `crldp` as a DER-encoded CRLDistributionPoints +// (RFC 5280), as described in `i2d_SAMPLE`. OPENSSL_EXPORT int i2d_CRL_DIST_POINTS(const CRL_DIST_POINTS *crldp, uint8_t **outp); -// A ISSUING_DIST_POINT_st, aka |ISSUING_DIST_POINT|, represents a +// A ISSUING_DIST_POINT_st, aka `ISSUING_DIST_POINT`, represents a // IssuingDistributionPoint structure (RFC 5280). struct ISSUING_DIST_POINT_st { DIST_POINT_NAME *distpoint; @@ -2374,24 +2379,24 @@ struct ISSUING_DIST_POINT_st { ASN1_BOOLEAN onlyattr; } /* ISSUING_DIST_POINT */; -// ISSUING_DIST_POINT is an |ASN1_ITEM| whose ASN.1 type is -// IssuingDistributionPoint (RFC 5280) and C type is |ISSUING_DIST_POINT*|. +// ISSUING_DIST_POINT is an `ASN1_ITEM` whose ASN.1 type is +// IssuingDistributionPoint (RFC 5280) and C type is `ISSUING_DIST_POINT*`. DECLARE_ASN1_ITEM(ISSUING_DIST_POINT) -// ISSUING_DIST_POINT_new returns a newly-allocated, empty |ISSUING_DIST_POINT| +// ISSUING_DIST_POINT_new returns a newly-allocated, empty `ISSUING_DIST_POINT` // object, or NULL on error. OPENSSL_EXPORT ISSUING_DIST_POINT *ISSUING_DIST_POINT_new(void); -// ISSUING_DIST_POINT_free releases memory associated with |idp|. +// ISSUING_DIST_POINT_free releases memory associated with `idp`. OPENSSL_EXPORT void ISSUING_DIST_POINT_free(ISSUING_DIST_POINT *idp); -// d2i_ISSUING_DIST_POINT parses up to |len| bytes from |*inp| as a DER-encoded -// IssuingDistributionPoint (RFC 5280), as described in |d2i_SAMPLE|. +// d2i_ISSUING_DIST_POINT parses up to `len` bytes from `*inp` as a DER-encoded +// IssuingDistributionPoint (RFC 5280), as described in `d2i_SAMPLE`. OPENSSL_EXPORT ISSUING_DIST_POINT *d2i_ISSUING_DIST_POINT( ISSUING_DIST_POINT **out, const uint8_t **inp, long len); -// i2d_ISSUING_DIST_POINT marshals |idp| as a DER-encoded -// IssuingDistributionPoint (RFC 5280), as described in |i2d_SAMPLE|. +// i2d_ISSUING_DIST_POINT marshals `idp` as a DER-encoded +// IssuingDistributionPoint (RFC 5280), as described in `i2d_SAMPLE`. OPENSSL_EXPORT int i2d_ISSUING_DIST_POINT(const ISSUING_DIST_POINT *idp, uint8_t **outp); @@ -2412,11 +2417,11 @@ typedef struct NOTICEREF_st { STACK_OF(ASN1_INTEGER) *noticenos; } NOTICEREF; -// NOTICEREF_new returns a newly-allocated, empty |NOTICEREF| object, or NULL +// NOTICEREF_new returns a newly-allocated, empty `NOTICEREF` object, or NULL // on error. OPENSSL_EXPORT NOTICEREF *NOTICEREF_new(void); -// NOTICEREF_free releases memory associated with |ref|. +// NOTICEREF_free releases memory associated with `ref`. OPENSSL_EXPORT void NOTICEREF_free(NOTICEREF *ref); // A USERNOTICE represents a UserNotice structure (RFC 5280). @@ -2425,20 +2430,20 @@ typedef struct USERNOTICE_st { ASN1_STRING *exptext; } USERNOTICE; -// USERNOTICE_new returns a newly-allocated, empty |USERNOTICE| object, or NULL +// USERNOTICE_new returns a newly-allocated, empty `USERNOTICE` object, or NULL // on error. OPENSSL_EXPORT USERNOTICE *USERNOTICE_new(void); -// USERNOTICE_free releases memory associated with |notice|. +// USERNOTICE_free releases memory associated with `notice`. OPENSSL_EXPORT void USERNOTICE_free(USERNOTICE *notice); -// A POLICYQUALINFO represents a PolicyQualifierInfo structure (RFC 5280). |d| +// A POLICYQUALINFO represents a PolicyQualifierInfo structure (RFC 5280). `d` // contains the qualifier field of the PolicyQualifierInfo. Its type is -// determined by |pqualid|. If |pqualid| is |NID_id_qt_cps|, |d| must be -// |cpsuri|. If |pqualid| is |NID_id_qt_unotice|, |d| must be |usernotice|. -// Otherwise, |d| must be |other|. +// determined by `pqualid`. If `pqualid` is `NID_id_qt_cps`, `d` must be +// `cpsuri`. If `pqualid` is `NID_id_qt_unotice`, `d` must be `usernotice`. +// Otherwise, `d` must be `other`. // -// WARNING: |pqualid| and |d| must be kept consistent. An inconsistency will +// WARNING: `pqualid` and `d` must be kept consistent. An inconsistency will // result in a potentially exploitable memory error. typedef struct POLICYQUALINFO_st { ASN1_OBJECT *pqualid; @@ -2451,11 +2456,11 @@ typedef struct POLICYQUALINFO_st { DEFINE_STACK_OF(POLICYQUALINFO) -// POLICYQUALINFO_new returns a newly-allocated, empty |POLICYQUALINFO| object, +// POLICYQUALINFO_new returns a newly-allocated, empty `POLICYQUALINFO` object, // or NULL on error. OPENSSL_EXPORT POLICYQUALINFO *POLICYQUALINFO_new(void); -// POLICYQUALINFO_free releases memory associated with |info|. +// POLICYQUALINFO_free releases memory associated with `info`. OPENSSL_EXPORT void POLICYQUALINFO_free(POLICYQUALINFO *info); // A POLICYINFO represents a PolicyInformation structure (RFC 5280). @@ -2466,33 +2471,33 @@ typedef struct POLICYINFO_st { DEFINE_STACK_OF(POLICYINFO) -// POLICYINFO_new returns a newly-allocated, empty |POLICYINFO| object, or NULL +// POLICYINFO_new returns a newly-allocated, empty `POLICYINFO` object, or NULL // on error. OPENSSL_EXPORT POLICYINFO *POLICYINFO_new(void); -// POLICYINFO_free releases memory associated with |info|. +// POLICYINFO_free releases memory associated with `info`. OPENSSL_EXPORT void POLICYINFO_free(POLICYINFO *info); typedef STACK_OF(POLICYINFO) CERTIFICATEPOLICIES; -// CERTIFICATEPOLICIES is an |ASN1_ITEM| whose ASN.1 type is CertificatePolicies -// (RFC 5280) and C type is |STACK_OF(POLICYINFO)*|, or |CERTIFICATEPOLICIES*|. +// CERTIFICATEPOLICIES is an `ASN1_ITEM` whose ASN.1 type is CertificatePolicies +// (RFC 5280) and C type is `STACK_OF(POLICYINFO)*`, or `CERTIFICATEPOLICIES*`. DECLARE_ASN1_ITEM(CERTIFICATEPOLICIES) // CERTIFICATEPOLICIES_new returns a newly-allocated, empty -// |CERTIFICATEPOLICIES| object, or NULL on error. +// `CERTIFICATEPOLICIES` object, or NULL on error. OPENSSL_EXPORT CERTIFICATEPOLICIES *CERTIFICATEPOLICIES_new(void); -// CERTIFICATEPOLICIES_free releases memory associated with |policies|. +// CERTIFICATEPOLICIES_free releases memory associated with `policies`. OPENSSL_EXPORT void CERTIFICATEPOLICIES_free(CERTIFICATEPOLICIES *policies); -// d2i_CERTIFICATEPOLICIES parses up to |len| bytes from |*inp| as a DER-encoded -// CertificatePolicies (RFC 5280), as described in |d2i_SAMPLE|. +// d2i_CERTIFICATEPOLICIES parses up to `len` bytes from `*inp` as a DER-encoded +// CertificatePolicies (RFC 5280), as described in `d2i_SAMPLE`. OPENSSL_EXPORT CERTIFICATEPOLICIES *d2i_CERTIFICATEPOLICIES( CERTIFICATEPOLICIES **out, const uint8_t **inp, long len); -// i2d_CERTIFICATEPOLICIES marshals |policies| as a DER-encoded -// CertificatePolicies (RFC 5280), as described in |i2d_SAMPLE|. +// i2d_CERTIFICATEPOLICIES marshals `policies` as a DER-encoded +// CertificatePolicies (RFC 5280), as described in `i2d_SAMPLE`. OPENSSL_EXPORT int i2d_CERTIFICATEPOLICIES(const CERTIFICATEPOLICIES *policies, uint8_t **outp); @@ -2505,17 +2510,17 @@ typedef struct POLICY_MAPPING_st { DEFINE_STACK_OF(POLICY_MAPPING) -// POLICY_MAPPING_new returns a newly-allocated, empty |POLICY_MAPPING| object, +// POLICY_MAPPING_new returns a newly-allocated, empty `POLICY_MAPPING` object, // or NULL on error. OPENSSL_EXPORT POLICY_MAPPING *POLICY_MAPPING_new(void); -// POLICY_MAPPING_free releases memory associated with |mapping|. +// POLICY_MAPPING_free releases memory associated with `mapping`. OPENSSL_EXPORT void POLICY_MAPPING_free(POLICY_MAPPING *mapping); typedef STACK_OF(POLICY_MAPPING) POLICY_MAPPINGS; -// POLICY_MAPPINGS is an |ASN1_ITEM| whose ASN.1 type is PolicyMappings (RFC -// 5280) and C type is |STACK_OF(POLICY_MAPPING)*|, or |POLICY_MAPPINGS*|. +// POLICY_MAPPINGS is an `ASN1_ITEM` whose ASN.1 type is PolicyMappings (RFC +// 5280) and C type is `STACK_OF(POLICY_MAPPING)*`, or `POLICY_MAPPINGS*`. DECLARE_ASN1_ITEM(POLICY_MAPPINGS) // A POLICY_CONSTRAINTS represents a PolicyConstraints structure (RFC 5280). @@ -2524,96 +2529,96 @@ typedef struct POLICY_CONSTRAINTS_st { ASN1_INTEGER *inhibitPolicyMapping; } POLICY_CONSTRAINTS; -// POLICY_CONSTRAINTS is an |ASN1_ITEM| whose ASN.1 type is PolicyConstraints -// (RFC 5280) and C type is |POLICY_CONSTRAINTS*|. +// POLICY_CONSTRAINTS is an `ASN1_ITEM` whose ASN.1 type is PolicyConstraints +// (RFC 5280) and C type is `POLICY_CONSTRAINTS*`. DECLARE_ASN1_ITEM(POLICY_CONSTRAINTS) -// POLICY_CONSTRAINTS_new returns a newly-allocated, empty |POLICY_CONSTRAINTS| +// POLICY_CONSTRAINTS_new returns a newly-allocated, empty `POLICY_CONSTRAINTS` // object, or NULL on error. OPENSSL_EXPORT POLICY_CONSTRAINTS *POLICY_CONSTRAINTS_new(void); -// POLICY_CONSTRAINTS_free releases memory associated with |pcons|. +// POLICY_CONSTRAINTS_free releases memory associated with `pcons`. OPENSSL_EXPORT void POLICY_CONSTRAINTS_free(POLICY_CONSTRAINTS *pcons); // Algorithm identifiers. // -// An |X509_ALGOR| represents an AlgorithmIdentifier structure, used in X.509 +// An `X509_ALGOR` represents an AlgorithmIdentifier structure, used in X.509 // to represent signature algorithms and public key algorithms. DEFINE_STACK_OF(X509_ALGOR) -// X509_ALGOR is an |ASN1_ITEM| whose ASN.1 type is AlgorithmIdentifier and C -// type is |X509_ALGOR*|. +// X509_ALGOR is an `ASN1_ITEM` whose ASN.1 type is AlgorithmIdentifier and C +// type is `X509_ALGOR*`. DECLARE_ASN1_ITEM(X509_ALGOR) -// X509_ALGOR_new returns a newly-allocated, empty |X509_ALGOR| object, or NULL +// X509_ALGOR_new returns a newly-allocated, empty `X509_ALGOR` object, or NULL // on error. OPENSSL_EXPORT X509_ALGOR *X509_ALGOR_new(void); -// X509_ALGOR_dup returns a newly-allocated copy of |alg|, or NULL on error. -// This function works by serializing the structure, so if |alg| is incomplete, +// X509_ALGOR_dup returns a newly-allocated copy of `alg`, or NULL on error. +// This function works by serializing the structure, so if `alg` is incomplete, // it may fail. OPENSSL_EXPORT X509_ALGOR *X509_ALGOR_dup(const X509_ALGOR *alg); -// X509_ALGOR_copy sets |dst| to a copy of the contents of |src|. It returns one +// X509_ALGOR_copy sets `dst` to a copy of the contents of `src`. It returns one // on success and zero on error. OPENSSL_EXPORT int X509_ALGOR_copy(X509_ALGOR *dst, const X509_ALGOR *src); -// X509_ALGOR_free releases memory associated with |alg|. +// X509_ALGOR_free releases memory associated with `alg`. OPENSSL_EXPORT void X509_ALGOR_free(X509_ALGOR *alg); -// d2i_X509_ALGOR parses up to |len| bytes from |*inp| as a DER-encoded -// AlgorithmIdentifier, as described in |d2i_SAMPLE|. +// d2i_X509_ALGOR parses up to `len` bytes from `*inp` as a DER-encoded +// AlgorithmIdentifier, as described in `d2i_SAMPLE`. OPENSSL_EXPORT X509_ALGOR *d2i_X509_ALGOR(X509_ALGOR **out, const uint8_t **inp, long len); -// i2d_X509_ALGOR marshals |alg| as a DER-encoded AlgorithmIdentifier, as -// described in |i2d_SAMPLE|. +// i2d_X509_ALGOR marshals `alg` as a DER-encoded AlgorithmIdentifier, as +// described in `i2d_SAMPLE`. OPENSSL_EXPORT int i2d_X509_ALGOR(const X509_ALGOR *alg, uint8_t **outp); -// X509_ALGOR_set0 sets |alg| to an AlgorithmIdentifier with algorithm |obj| and -// parameter determined by |param_type| and |param_value|. It returns one on -// success and zero on error. This function takes ownership of |obj| and -// |param_value| on success. +// X509_ALGOR_set0 sets `alg` to an AlgorithmIdentifier with algorithm `obj` and +// parameter determined by `param_type` and `param_value`. It returns one on +// success and zero on error. This function takes ownership of `obj` and +// `param_value` on success. // -// If |param_type| is |V_ASN1_UNDEF|, the parameter is omitted. If |param_type| -// is zero, the parameter is left unchanged. Otherwise, |param_type| and -// |param_value| are interpreted as in |ASN1_TYPE_set|. +// If `param_type` is `V_ASN1_UNDEF`, the parameter is omitted. If `param_type` +// is zero, the parameter is left unchanged. Otherwise, `param_type` and +// `param_value` are interpreted as in `ASN1_TYPE_set`. // -// Note omitting the parameter (|V_ASN1_UNDEF|) and encoding an explicit NULL -// value (|V_ASN1_NULL|) are different. Some algorithms require one and some the +// Note omitting the parameter (`V_ASN1_UNDEF`) and encoding an explicit NULL +// value (`V_ASN1_NULL`) are different. Some algorithms require one and some the // other. Consult the relevant specification before calling this function. The -// correct parameter for an RSASSA-PKCS1-v1_5 signature is |V_ASN1_NULL|. The -// correct one for an ECDSA or Ed25519 signature is |V_ASN1_UNDEF|. +// correct parameter for an RSASSA-PKCS1-v1_5 signature is `V_ASN1_NULL`. The +// correct one for an ECDSA or Ed25519 signature is `V_ASN1_UNDEF`. OPENSSL_EXPORT int X509_ALGOR_set0(X509_ALGOR *alg, ASN1_OBJECT *obj, int param_type, void *param_value); -// X509_ALGOR_get0 sets |*out_obj| to the |alg|'s algorithm. If |alg|'s -// parameter is omitted, it sets |*out_param_type| and |*out_param_value| to -// |V_ASN1_UNDEF| and NULL. Otherwise, it sets |*out_param_type| and -// |*out_param_value| to the parameter, using the same representation as -// |ASN1_TYPE_set0|. See |ASN1_TYPE_set0| and |ASN1_TYPE| for details. +// X509_ALGOR_get0 sets `*out_obj` to the `alg`'s algorithm. If `alg`'s +// parameter is omitted, it sets `*out_param_type` and `*out_param_value` to +// `V_ASN1_UNDEF` and NULL. Otherwise, it sets `*out_param_type` and +// `*out_param_value` to the parameter, using the same representation as +// `ASN1_TYPE_set0`. See `ASN1_TYPE_set0` and `ASN1_TYPE` for details. // // Callers that require the parameter in serialized form should, after checking -// for |V_ASN1_UNDEF|, use |ASN1_TYPE_set1| and |d2i_ASN1_TYPE|, rather than -// inspecting |*out_param_value|. +// for `V_ASN1_UNDEF`, use `ASN1_TYPE_set1` and `d2i_ASN1_TYPE`, rather than +// inspecting `*out_param_value`. // -// Each of |out_obj|, |out_param_type|, and |out_param_value| may be NULL to -// ignore the output. If |out_param_type| is NULL, |out_param_value| is ignored. +// Each of `out_obj`, `out_param_type`, and `out_param_value` may be NULL to +// ignore the output. If `out_param_type` is NULL, `out_param_value` is ignored. // -// WARNING: If |*out_param_type| is set to |V_ASN1_UNDEF|, OpenSSL and older -// revisions of BoringSSL leave |*out_param_value| unset rather than setting it +// WARNING: If `*out_param_type` is set to `V_ASN1_UNDEF`, OpenSSL and older +// revisions of BoringSSL leave `*out_param_value` unset rather than setting it // to NULL. Callers that support both OpenSSL and BoringSSL should not assume -// |*out_param_value| is uniformly initialized. +// `*out_param_value` is uniformly initialized. OPENSSL_EXPORT void X509_ALGOR_get0(const ASN1_OBJECT **out_obj, int *out_param_type, const void **out_param_value, const X509_ALGOR *alg); -// X509_ALGOR_set_md sets |alg| to the hash function |md|. Note this +// X509_ALGOR_set_md sets `alg` to the hash function `md`. Note this // AlgorithmIdentifier represents the hash function itself, not a signature -// algorithm that uses |md|. It returns one on success and zero on error. +// algorithm that uses `md`. It returns one on success and zero on error. // // Due to historical specification mistakes (see Section 2.1 of RFC 4055), the // parameters field is sometimes omitted and sometimes a NULL value. When used @@ -2626,9 +2631,11 @@ OPENSSL_EXPORT void X509_ALGOR_get0(const ASN1_OBJECT **out_obj, // constructing PSS and move on. OPENSSL_EXPORT int X509_ALGOR_set_md(X509_ALGOR *alg, const EVP_MD *md); -// X509_ALGOR_cmp returns zero if |a| and |b| are equal, and some non-zero value -// otherwise. Note this function can only be used for equality checks, not an -// ordering. +// X509_ALGOR_cmp compares `a` and `b`. It returns zero if they are equal, and a +// negative or positive value if `a` sorts before or after `b`, respectively. +// Values are ordered first by algorithm, then by parameter, with an absent +// parameter sorting before a present one. The order is arbitrary but suitable +// for sorting. Callers should not rely on the exact order. OPENSSL_EXPORT int X509_ALGOR_cmp(const X509_ALGOR *a, const X509_ALGOR *b); @@ -2636,51 +2643,51 @@ OPENSSL_EXPORT int X509_ALGOR_cmp(const X509_ALGOR *a, const X509_ALGOR *b); // // Unlike certificates and CRLs, CSRs use a separate Attribute structure (RFC // 2985, RFC 2986) for extensibility. This is represented by the library as -// |X509_ATTRIBUTE|. +// `X509_ATTRIBUTE`. DEFINE_STACK_OF(X509_ATTRIBUTE) -// X509_ATTRIBUTE_new returns a newly-allocated, empty |X509_ATTRIBUTE| object, -// or NULL on error. |X509_ATTRIBUTE_set1_*| may be used to finish initializing +// X509_ATTRIBUTE_new returns a newly-allocated, empty `X509_ATTRIBUTE` object, +// or NULL on error. `X509_ATTRIBUTE_set1_*` may be used to finish initializing // it. OPENSSL_EXPORT X509_ATTRIBUTE *X509_ATTRIBUTE_new(void); -// X509_ATTRIBUTE_dup returns a newly-allocated copy of |attr|, or NULL on -// error. This function works by serializing the structure, so if |attr| is +// X509_ATTRIBUTE_dup returns a newly-allocated copy of `attr`, or NULL on +// error. This function works by serializing the structure, so if `attr` is // incomplete, it may fail. OPENSSL_EXPORT X509_ATTRIBUTE *X509_ATTRIBUTE_dup(const X509_ATTRIBUTE *attr); -// X509_ATTRIBUTE_free releases memory associated with |attr|. +// X509_ATTRIBUTE_free releases memory associated with `attr`. OPENSSL_EXPORT void X509_ATTRIBUTE_free(X509_ATTRIBUTE *attr); -// d2i_X509_ATTRIBUTE parses up to |len| bytes from |*inp| as a DER-encoded -// Attribute (RFC 2986), as described in |d2i_SAMPLE|. +// d2i_X509_ATTRIBUTE parses up to `len` bytes from `*inp` as a DER-encoded +// Attribute (RFC 2986), as described in `d2i_SAMPLE`. OPENSSL_EXPORT X509_ATTRIBUTE *d2i_X509_ATTRIBUTE(X509_ATTRIBUTE **out, const uint8_t **inp, long len); -// i2d_X509_ATTRIBUTE marshals |alg| as a DER-encoded Attribute (RFC 2986), as -// described in |i2d_SAMPLE|. +// i2d_X509_ATTRIBUTE marshals `alg` as a DER-encoded Attribute (RFC 2986), as +// described in `i2d_SAMPLE`. OPENSSL_EXPORT int i2d_X509_ATTRIBUTE(const X509_ATTRIBUTE *alg, uint8_t **outp); -// X509_ATTRIBUTE_create returns a newly-allocated |X509_ATTRIBUTE|, or NULL on -// error. The attribute has type |nid| and contains a single value determined by -// |attrtype| and |value|, which are interpreted as in |ASN1_TYPE_set|. Note -// this function takes ownership of |value|. +// X509_ATTRIBUTE_create returns a newly-allocated `X509_ATTRIBUTE`, or NULL on +// error. The attribute has type `nid` and contains a single value determined by +// `attrtype` and `value`, which are interpreted as in `ASN1_TYPE_set`. Note +// this function takes ownership of `value`. OPENSSL_EXPORT X509_ATTRIBUTE *X509_ATTRIBUTE_create(int nid, int attrtype, void *value); -// X509_ATTRIBUTE_create_by_NID returns a newly-allocated |X509_ATTRIBUTE| of -// type |nid|, or NULL on error. The value is determined as in -// |X509_ATTRIBUTE_set1_data|. +// X509_ATTRIBUTE_create_by_NID returns a newly-allocated `X509_ATTRIBUTE` of +// type `nid`, or NULL on error. The value is determined as in +// `X509_ATTRIBUTE_set1_data`. // -// If |attr| is non-NULL, the resulting |X509_ATTRIBUTE| is also written to -// |*attr|. If |*attr| was non-NULL when the function was called, |*attr| is +// If `attr` is non-NULL, the resulting `X509_ATTRIBUTE` is also written to +// `*attr`. If `*attr` was non-NULL when the function was called, `*attr` is // reused instead of creating a new object. // -// WARNING: The interpretation of |attrtype|, |data|, and |len| is complex and -// error-prone. See |X509_ATTRIBUTE_set1_data| for details. +// WARNING: The interpretation of `attrtype`, `data`, and `len` is complex and +// error-prone. See `X509_ATTRIBUTE_set1_data` for details. // // WARNING: The object reuse form is deprecated and may be removed in the // future. It also currently incorrectly appends to the reused object's value @@ -2688,201 +2695,205 @@ OPENSSL_EXPORT X509_ATTRIBUTE *X509_ATTRIBUTE_create(int nid, int attrtype, OPENSSL_EXPORT X509_ATTRIBUTE *X509_ATTRIBUTE_create_by_NID( X509_ATTRIBUTE **attr, int nid, int attrtype, const void *data, int len); -// X509_ATTRIBUTE_create_by_OBJ behaves like |X509_ATTRIBUTE_create_by_NID| -// except the attribute's type is determined by |obj|. +// X509_ATTRIBUTE_create_by_OBJ behaves like `X509_ATTRIBUTE_create_by_NID` +// except the attribute's type is determined by `obj`. OPENSSL_EXPORT X509_ATTRIBUTE *X509_ATTRIBUTE_create_by_OBJ( X509_ATTRIBUTE **attr, const ASN1_OBJECT *obj, int attrtype, const void *data, int len); -// X509_ATTRIBUTE_create_by_txt behaves like |X509_ATTRIBUTE_create_by_NID| -// except the attribute's type is determined by calling |OBJ_txt2obj| with -// |attrname|. +// X509_ATTRIBUTE_create_by_txt behaves like `X509_ATTRIBUTE_create_by_NID` +// except the attribute's type is determined by calling `OBJ_txt2obj` with +// `attrname`. OPENSSL_EXPORT X509_ATTRIBUTE *X509_ATTRIBUTE_create_by_txt( X509_ATTRIBUTE **attr, const char *attrname, int type, const unsigned char *bytes, int len); -// X509_ATTRIBUTE_set1_object sets |attr|'s type to |obj|. It returns one on +// X509_ATTRIBUTE_set1_object sets `attr`'s type to `obj`. It returns one on // success and zero on error. OPENSSL_EXPORT int X509_ATTRIBUTE_set1_object(X509_ATTRIBUTE *attr, const ASN1_OBJECT *obj); -// X509_ATTRIBUTE_set1_data appends a value to |attr|'s value set and returns +// X509_ATTRIBUTE_set1_data appends a value to `attr`'s value set and returns // one on success or zero on error. The value is determined as follows: // -// If |attrtype| is zero, this function returns one and does nothing. This form -// may be used when calling |X509_ATTRIBUTE_create_by_*| to create an attribute +// If `attrtype` is zero, this function returns one and does nothing. This form +// may be used when calling `X509_ATTRIBUTE_create_by_*` to create an attribute // with an empty value set. Such attributes are invalid, but OpenSSL supports // creating them. // -// Otherwise, if |attrtype| is a |MBSTRING_*| constant, the value is an ASN.1 -// string. The string is determined by decoding |len| bytes from |data| in the -// encoding specified by |attrtype|, and then re-encoding it in a form -// appropriate for |attr|'s type. If |len| is -1, |strlen(data)| is used -// instead. See |ASN1_STRING_set_by_NID| for details. +// Otherwise, if `attrtype` is a `MBSTRING_*` constant, the value is an ASN.1 +// string. The string is determined by decoding `len` bytes from `data` in the +// encoding specified by `attrtype`, and then re-encoding it in a form +// appropriate for `attr`'s type. If `len` is -1, `strlen(data)` is used +// instead. See `ASN1_STRING_set_by_NID` for details. // -// Otherwise, if |len| is not -1, the value is an ASN.1 string. |attrtype| is an -// |ASN1_STRING| type value and the |len| bytes from |data| are copied as the -// type-specific representation of |ASN1_STRING|. See |ASN1_STRING| for details. +// Otherwise, if `len` is not -1, the value is an ASN.1 string. `attrtype` is an +// `ASN1_STRING` type value and the `len` bytes from `data` are copied as the +// type-specific representation of `ASN1_STRING`. See `ASN1_STRING` for details. // -// Otherwise, if |len| is -1, the value is constructed by passing |attrtype| and -// |data| to |ASN1_TYPE_set1|. That is, |attrtype| is an |ASN1_TYPE| type value, -// and |data| is cast to the corresponding pointer type. +// Otherwise, if `len` is -1, the value is constructed by passing `attrtype` and +// `data` to `ASN1_TYPE_set1`. That is, `attrtype` is an `ASN1_TYPE` type value, +// and `data` is cast to the corresponding pointer type. // -// WARNING: Despite the name, this function appends to |attr|'s value set, +// WARNING: Despite the name, this function appends to `attr`'s value set, // rather than overwriting it. To overwrite the value set, create a new -// |X509_ATTRIBUTE| with |X509_ATTRIBUTE_new|. +// `X509_ATTRIBUTE` with `X509_ATTRIBUTE_new`. // -// WARNING: If using the |MBSTRING_*| form, pass a length rather than relying on -// |strlen|. In particular, |strlen| will not behave correctly if the input is -// |MBSTRING_BMP| or |MBSTRING_UNIV|. +// WARNING: If using the `MBSTRING_*` form, pass a length rather than relying on +// `strlen`. In particular, `strlen` will not behave correctly if the input is +// `MBSTRING_BMP` or `MBSTRING_UNIV`. // -// WARNING: This function currently misinterprets |V_ASN1_OTHER| as an -// |MBSTRING_*| constant. This matches OpenSSL but means it is impossible to +// WARNING: This function currently misinterprets `V_ASN1_OTHER` as an +// `MBSTRING_*` constant. This matches OpenSSL but means it is impossible to // construct a value with a non-universal tag. OPENSSL_EXPORT int X509_ATTRIBUTE_set1_data(X509_ATTRIBUTE *attr, int attrtype, const void *data, int len); -// X509_ATTRIBUTE_get0_data returns the |idx|th value of |attr| in a -// type-specific representation to |attrtype|, or NULL if out of bounds or the -// type does not match. |attrtype| is one of the type values in |ASN1_TYPE|. On -// match, the return value uses the same representation as |ASN1_TYPE_set0|. See -// |ASN1_TYPE| for details. +// X509_ATTRIBUTE_get0_data returns the `idx`th value of `attr` in a +// type-specific representation to `attrtype`, or NULL if out of bounds or the +// type does not match. `attrtype` is one of the type values in `ASN1_TYPE`. On +// match, the return value uses the same representation as `ASN1_TYPE_set0`. See +// `ASN1_TYPE` for details. OPENSSL_EXPORT void *X509_ATTRIBUTE_get0_data(X509_ATTRIBUTE *attr, int idx, int attrtype, void *unused); -// X509_ATTRIBUTE_count returns the number of values in |attr|. +// X509_ATTRIBUTE_count returns the number of values in `attr`. OPENSSL_EXPORT int X509_ATTRIBUTE_count(const X509_ATTRIBUTE *attr); -// X509_ATTRIBUTE_get0_object returns the type of |attr|. +// X509_ATTRIBUTE_get0_object returns the type of `attr`. OPENSSL_EXPORT ASN1_OBJECT *X509_ATTRIBUTE_get0_object(X509_ATTRIBUTE *attr); -// X509_ATTRIBUTE_get0_type returns the |idx|th value in |attr|, or NULL if out -// of bounds. Note this function returns one of |attr|'s values, not the type. +// X509_ATTRIBUTE_get0_type returns the `idx`th value in `attr`, or NULL if out +// of bounds. Note this function returns one of `attr`'s values, not the type. OPENSSL_EXPORT ASN1_TYPE *X509_ATTRIBUTE_get0_type(X509_ATTRIBUTE *attr, int idx); // Certificate stores. // -// An |X509_STORE| contains trusted certificates, CRLs, and verification +// An `X509_STORE` contains trusted certificates, CRLs, and verification // parameters that are shared between multiple certificate verifications. // -// Certificates in an |X509_STORE| are referred to as "trusted certificates", +// Certificates in an `X509_STORE` are referred to as "trusted certificates", // but an individual certificate verification may not necessarily treat every -// trusted certificate as a trust anchor. See |X509_VERIFY_PARAM_set_trust| for +// trusted certificate as a trust anchor. See `X509_VERIFY_PARAM_set_trust` for // details. // // WARNING: Although a trusted certificate which fails the -// |X509_VERIFY_PARAM_set_trust| check is functionally an untrusted +// `X509_VERIFY_PARAM_set_trust` check is functionally an untrusted // intermediate certificate, callers should not rely on this to configure -// untrusted intermediates in an |X509_STORE|. The trust check is complex, so +// untrusted intermediates in an `X509_STORE`. The trust check is complex, so // this risks inadvertently treating it as a trust anchor. Instead, configure -// untrusted intermediates with the |chain| parameter of |X509_STORE_CTX_init|. +// untrusted intermediates with the `chain` parameter of `X509_STORE_CTX_init`. // -// Certificates in |X509_STORE| may be specified in several ways: -// - Added by |X509_STORE_add_cert|. -// - Returned by an |X509_LOOKUP| added by |X509_STORE_add_lookup|. +// Certificates in `X509_STORE` may be specified in several ways: +// - Added by `X509_STORE_add_cert`. +// - Returned by an `X509_LOOKUP` added by `X509_STORE_add_lookup`. // -// |X509_STORE|s are reference-counted and may be shared by certificate +// `X509_STORE`s are reference-counted and may be shared by certificate // verifications running concurrently on multiple threads. However, an -// |X509_STORE|'s verification parameters may not be modified concurrently with +// `X509_STORE`'s verification parameters may not be modified concurrently with // certificate verification or other operations. Unless otherwise documented, // functions which take const pointer may be used concurrently, while // functions which take a non-const pointer may not. Callers that wish to modify -// verification parameters in a shared |X509_STORE| should instead modify -// |X509_STORE_CTX|s individually. +// verification parameters in a shared `X509_STORE` should instead modify +// `X509_STORE_CTX`s individually. // -// Objects in an |X509_STORE| are represented as an |X509_OBJECT|. Some +// Objects in an `X509_STORE` are represented as an `X509_OBJECT`. Some // functions in this library return values with this type. -// X509_STORE_new returns a newly-allocated |X509_STORE|, or NULL on error. +// X509_STORE_new returns a newly-allocated `X509_STORE`, or NULL on error. OPENSSL_EXPORT X509_STORE *X509_STORE_new(void); -// X509_STORE_up_ref adds one to the reference count of |store| and returns one. -// Although |store| is not const, this function's use of |store| is thread-safe. +// X509_STORE_up_ref adds one to the reference count of `store` and returns one. +// Although `store` is not const, this function's use of `store` is thread-safe. OPENSSL_EXPORT int X509_STORE_up_ref(X509_STORE *store); -// X509_STORE_free releases memory associated with |store|. +// X509_STORE_dup_ref adds one to the reference count of `store` and returns +// the same handle. +OPENSSL_EXPORT X509_STORE *X509_STORE_dup_ref(const X509_STORE *store); + +// X509_STORE_free releases memory associated with `store`. OPENSSL_EXPORT void X509_STORE_free(X509_STORE *store); -// X509_STORE_add_cert adds |x509| to |store| as a trusted certificate. It +// X509_STORE_add_cert adds `x509` to `store` as a trusted certificate. It // returns one on success and zero on error. This function internally increments -// |x509|'s reference count, so the caller retains ownership of |x509|. +// `x509`'s reference count, so the caller retains ownership of `x509`. // -// Certificates configured by this function are still subject to the checks -// described in |X509_VERIFY_PARAM_set_trust|. +// Certificates configured by this function are potential trust anchors, but +// whether they are trust anchors for any given verification depends on the +// checks described in `X509_VERIFY_PARAM_set_trust`. // -// Although |store| is not const, this function's use of |store| is thread-safe. -// However, if this function is called concurrently with |X509_verify_cert|, it -// is a race condition whether |x509| is available for issuer lookups. +// Although `store` is not const, this function's use of `store` is thread-safe. +// However, if this function is called concurrently with `X509_verify_cert`, it +// is a race condition whether `x509` is available for issuer lookups. // Moreover, the result may differ for each issuer lookup performed by a single -// |X509_verify_cert| call. -OPENSSL_EXPORT int X509_STORE_add_cert(X509_STORE *store, X509 *x509); +// `X509_verify_cert` call. +OPENSSL_EXPORT int X509_STORE_add_cert(X509_STORE *store, const X509 *x509); -// X509_STORE_add_crl adds |crl| to |store|. It returns one on success and zero -// on error. This function internally increments |crl|'s reference count, so the -// caller retains ownership of |crl|. CRLs added in this way are candidates for -// CRL lookup when |X509_V_FLAG_CRL_CHECK| is set. +// X509_STORE_add_crl adds `crl` to `store`. It returns one on success and zero +// on error. This function internally increments `crl`'s reference count, so the +// caller retains ownership of `crl`. CRLs added in this way are candidates for +// CRL lookup when `X509_V_FLAG_CRL_CHECK` is set. // -// Although |store| is not const, this function's use of |store| is thread-safe. -// However, if this function is called concurrently with |X509_verify_cert|, it -// is a race condition whether |crl| is available for CRL checks. Moreover, the +// Although `store` is not const, this function's use of `store` is thread-safe. +// However, if this function is called concurrently with `X509_verify_cert`, it +// is a race condition whether `crl` is available for CRL checks. Moreover, the // result may differ for each CRL check performed by a single -// |X509_verify_cert| call. +// `X509_verify_cert` call. // -// Note there are no supported APIs to remove CRLs from |store| once inserted. +// Note there are no supported APIs to remove CRLs from `store` once inserted. // To vary the set of CRLs over time, callers should either create a new -// |X509_STORE| or configure CRLs on a per-verification basis with -// |X509_STORE_CTX_set0_crls|. -OPENSSL_EXPORT int X509_STORE_add_crl(X509_STORE *store, X509_CRL *crl); +// `X509_STORE` or configure CRLs on a per-verification basis with +// `X509_STORE_CTX_set0_crls`. +OPENSSL_EXPORT int X509_STORE_add_crl(X509_STORE *store, const X509_CRL *crl); -// X509_STORE_get0_param returns |store|'s verification parameters. This object +// X509_STORE_get0_param returns `store`'s verification parameters. This object // is mutable and may be modified by the caller. For an individual certificate -// verification operation, |X509_STORE_CTX_init| initializes the -// |X509_STORE_CTX|'s parameters with these parameters. +// verification operation, `X509_STORE_CTX_init` initializes the +// `X509_STORE_CTX`'s parameters with these parameters. // -// WARNING: |X509_STORE_CTX_init| applies some default parameters (as in -// |X509_VERIFY_PARAM_inherit|) after copying |store|'s parameters. This means -// it is impossible to leave some parameters unset at |store|. They must be -// explicitly unset after creating the |X509_STORE_CTX|. +// WARNING: `X509_STORE_CTX_init` applies some default parameters (as in +// `X509_VERIFY_PARAM_inherit`) after copying `store`'s parameters. This means +// it is impossible to leave some parameters unset at `store`. They must be +// explicitly unset after creating the `X509_STORE_CTX`. // // As of writing these late defaults are a depth limit (see -// |X509_VERIFY_PARAM_set_depth|) and the |X509_V_FLAG_TRUSTED_FIRST| flag. This -// warning does not apply if the parameters were set in |store|. +// `X509_VERIFY_PARAM_set_depth`). This warning does not apply if the parameters +// were set in `store`. // // TODO(crbug.com/boringssl/441): This behavior is very surprising. Can we -// remove this notion of late defaults? The unsettable value at |X509_STORE| is +// remove this notion of late defaults? The unsettable value at `X509_STORE` is // -1, which rejects everything but explicitly-trusted self-signed certificates. -// |X509_V_FLAG_TRUSTED_FIRST| is mostly a workaround for poor path-building. OPENSSL_EXPORT X509_VERIFY_PARAM *X509_STORE_get0_param(X509_STORE *store); -// X509_STORE_set1_param copies verification parameters from |param| as in -// |X509_VERIFY_PARAM_set1|. It returns one on success and zero on error. +// X509_STORE_set1_param copies verification parameters from `param` as in +// `X509_VERIFY_PARAM_set1`. It returns one on success and zero on error. OPENSSL_EXPORT int X509_STORE_set1_param(X509_STORE *store, const X509_VERIFY_PARAM *param); -// X509_STORE_set_flags enables all values in |flags| in |store|'s verification -// flags. |flags| should be a combination of |X509_V_FLAG_*| constants. +// X509_STORE_set_flags enables all values in `flags` in `store`'s verification +// flags. `flags` should be a combination of `X509_V_FLAG_*` constants. // // WARNING: These flags will be combined with default flags when copied to an -// |X509_STORE_CTX|. This means it is impossible to unset those defaults from -// the |X509_STORE|. See discussion in |X509_STORE_get0_param|. +// `X509_STORE_CTX`. This means it is impossible to unset those defaults from +// the `X509_STORE`. See discussion in `X509_STORE_get0_param`. OPENSSL_EXPORT int X509_STORE_set_flags(X509_STORE *store, unsigned long flags); -// X509_STORE_set_depth configures |store| to, by default, limit certificate -// chains to |depth| intermediate certificates. This count excludes both the +// X509_STORE_set_depth configures `store` to, by default, limit certificate +// chains to `depth` intermediate certificates. This count excludes both the // target certificate and the trust anchor (root certificate). OPENSSL_EXPORT int X509_STORE_set_depth(X509_STORE *store, int depth); -// X509_STORE_set_purpose configures the purpose check for |store|. See -// |X509_VERIFY_PARAM_set_purpose| for details. +// X509_STORE_set_purpose configures the purpose check for `store`. See +// `X509_VERIFY_PARAM_set_purpose` for details. OPENSSL_EXPORT int X509_STORE_set_purpose(X509_STORE *store, int purpose); -// X509_STORE_set_trust configures the trust check for |store|. See -// |X509_VERIFY_PARAM_set_trust| for details. +// X509_STORE_set_trust configures the trust check for `store`. See +// `X509_VERIFY_PARAM_set_trust` for details. OPENSSL_EXPORT int X509_STORE_set_trust(X509_STORE *store, int trust); -// The following constants indicate the type of an |X509_OBJECT|. +// The following constants indicate the type of an `X509_OBJECT`. #define X509_LU_NONE 0 #define X509_LU_X509 1 #define X509_LU_CRL 2 @@ -2890,28 +2901,28 @@ OPENSSL_EXPORT int X509_STORE_set_trust(X509_STORE *store, int trust); DEFINE_STACK_OF(X509_OBJECT) -// X509_OBJECT_new returns a newly-allocated, empty |X509_OBJECT| or NULL on +// X509_OBJECT_new returns a newly-allocated, empty `X509_OBJECT` or NULL on // error. OPENSSL_EXPORT X509_OBJECT *X509_OBJECT_new(void); -// X509_OBJECT_free releases memory associated with |obj|. +// X509_OBJECT_free releases memory associated with `obj`. OPENSSL_EXPORT void X509_OBJECT_free(X509_OBJECT *obj); -// X509_OBJECT_get_type returns the type of |obj|, which will be one of the -// |X509_LU_*| constants. +// X509_OBJECT_get_type returns the type of `obj`, which will be one of the +// `X509_LU_*` constants. OPENSSL_EXPORT int X509_OBJECT_get_type(const X509_OBJECT *obj); -// X509_OBJECT_get0_X509 returns |obj| as a certificate, or NULL if |obj| is not +// X509_OBJECT_get0_X509 returns `obj` as a certificate, or NULL if `obj` is not // a certificate. OPENSSL_EXPORT X509 *X509_OBJECT_get0_X509(const X509_OBJECT *obj); // X509_STORE_get1_objects returns a newly-allocated stack containing the -// contents of |store|, or NULL on error. The caller must release the result -// with |sk_X509_OBJECT_pop_free| and |X509_OBJECT_free| when done. +// contents of `store`, or NULL on error. The caller must release the result +// with `sk_X509_OBJECT_pop_free` and `X509_OBJECT_free` when done. // // The result will include all certificates and CRLs added via -// |X509_STORE_add_cert| and |X509_STORE_add_crl|, as well as any cached objects -// added by |X509_LOOKUP_add_dir|. The last of these may change over time, as +// `X509_STORE_add_cert` and `X509_STORE_add_crl`, as well as any cached objects +// added by `X509_LOOKUP_add_dir`. The last of these may change over time, as // different objects are loaded from the filesystem. Callers should not depend // on this caching behavior. The objects are returned in no particular order. OPENSSL_EXPORT STACK_OF(X509_OBJECT) *X509_STORE_get1_objects( @@ -2920,41 +2931,41 @@ OPENSSL_EXPORT STACK_OF(X509_OBJECT) *X509_STORE_get1_objects( // Certificate verification. // -// An |X509_STORE_CTX| object represents a single certificate verification +// An `X509_STORE_CTX` object represents a single certificate verification // operation. To verify a certificate chain, callers construct an -// |X509_STORE_CTX|, initialize it with |X509_STORE_CTX_init|, configure extra -// parameters with |X509_STORE_CTX_get0_param|, and call |X509_verify_cert|. +// `X509_STORE_CTX`, initialize it with `X509_STORE_CTX_init`, configure extra +// parameters with `X509_STORE_CTX_get0_param`, and call `X509_verify_cert`. -// X509_STORE_CTX_new returns a newly-allocated, empty |X509_STORE_CTX|, or NULL +// X509_STORE_CTX_new returns a newly-allocated, empty `X509_STORE_CTX`, or NULL // on error. OPENSSL_EXPORT X509_STORE_CTX *X509_STORE_CTX_new(void); -// X509_STORE_CTX_free releases memory associated with |ctx|. +// X509_STORE_CTX_free releases memory associated with `ctx`. OPENSSL_EXPORT void X509_STORE_CTX_free(X509_STORE_CTX *ctx); -// X509_STORE_CTX_init initializes |ctx| to verify |x509|, using trusted -// certificates and parameters in |store|. It returns one on success and zero on -// error. |chain| is a list of untrusted intermediate certificates to use in +// X509_STORE_CTX_init initializes `ctx` to verify `x509`, using trusted +// certificates and parameters in `store`. It returns one on success and zero on +// error. `chain` is a list of untrusted intermediate certificates to use in // verification. // -// |ctx| stores pointers to |store|, |x509|, and |chain|. Each of these objects -// must outlive |ctx| and may not be mutated for the duration of the certificate +// `ctx` stores pointers to `store`, `x509`, and `chain`. Each of these objects +// must outlive `ctx` and may not be mutated for the duration of the certificate // verification. OPENSSL_EXPORT int X509_STORE_CTX_init(X509_STORE_CTX *ctx, X509_STORE *store, X509 *x509, STACK_OF(X509) *chain); -// X509_verify_cert performs certificate verification with |ctx|, which must -// have been initialized with |X509_STORE_CTX_init|. It returns one on success -// and zero on error. On success, |X509_STORE_CTX_get0_chain| or -// |X509_STORE_CTX_get1_chain| may be used to return the verified certificate -// chain. On error, |X509_STORE_CTX_get_error| may be used to return additional +// X509_verify_cert performs certificate verification with `ctx`, which must +// have been initialized with `X509_STORE_CTX_init`. It returns one on success +// and zero on error. On success, `X509_STORE_CTX_get0_chain` or +// `X509_STORE_CTX_get1_chain` may be used to return the verified certificate +// chain. On error, `X509_STORE_CTX_get_error` may be used to return additional // error information. // // WARNING: Most failure conditions from this function do not use the error -// queue. Use |X509_STORE_CTX_get_error| to determine the cause of the error. +// queue. Use `X509_STORE_CTX_get_error` to determine the cause of the error. OPENSSL_EXPORT int X509_verify_cert(X509_STORE_CTX *ctx); -// X509_STORE_CTX_get0_chain, after a successful |X509_verify_cert| call, +// X509_STORE_CTX_get0_chain, after a successful `X509_verify_cert` call, // returns the verified certificate chain. The chain begins with the leaf and // ends with trust anchor. // @@ -2965,14 +2976,14 @@ OPENSSL_EXPORT int X509_verify_cert(X509_STORE_CTX *ctx); OPENSSL_EXPORT STACK_OF(X509) *X509_STORE_CTX_get0_chain( const X509_STORE_CTX *ctx); -// X509_STORE_CTX_get1_chain behaves like |X509_STORE_CTX_get0_chain| but -// returns a newly-allocated |STACK_OF(X509)| containing the completed chain, +// X509_STORE_CTX_get1_chain behaves like `X509_STORE_CTX_get0_chain` but +// returns a newly-allocated `STACK_OF(X509)` containing the completed chain, // with each certificate's reference count incremented. Callers must free the -// result with |sk_X509_pop_free| and |X509_free| when done. +// result with `sk_X509_pop_free` and `X509_free` when done. OPENSSL_EXPORT STACK_OF(X509) *X509_STORE_CTX_get1_chain( const X509_STORE_CTX *ctx); -// The following values are possible outputs of |X509_STORE_CTX_get_error|. +// The following values are possible outputs of `X509_STORE_CTX_get_error`. #define X509_V_OK 0 #define X509_V_ERR_UNSPECIFIED 1 #define X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT 2 @@ -3035,85 +3046,80 @@ OPENSSL_EXPORT STACK_OF(X509) *X509_STORE_CTX_get1_chain( #define X509_V_ERR_STORE_LOOKUP 66 #define X509_V_ERR_NAME_CONSTRAINTS_WITHOUT_SANS 67 -// X509_STORE_CTX_get_error, after |X509_verify_cert| returns, returns -// |X509_V_OK| if verification succeeded or an |X509_V_ERR_*| describing why -// verification failed. This will be consistent with |X509_verify_cert|'s return +// X509_STORE_CTX_get_error, after `X509_verify_cert` returns, returns +// `X509_V_OK` if verification succeeded or an `X509_V_ERR_*` describing why +// verification failed. This will be consistent with `X509_verify_cert`'s return // value, unless the caller used the deprecated verification callback (see -// |X509_STORE_CTX_set_verify_cb|) in a way that breaks |ctx|'s invariants. +// `X509_STORE_CTX_set_verify_cb`) in a way that breaks `ctx`'s invariants. // -// If called during the deprecated verification callback when |ok| is zero, it +// If called during the deprecated verification callback when `ok` is zero, it // returns the current error under consideration. OPENSSL_EXPORT int X509_STORE_CTX_get_error(const X509_STORE_CTX *ctx); -// X509_STORE_CTX_set_error sets |ctx|'s error to |err|, which should be -// |X509_V_OK| or an |X509_V_ERR_*| constant. It is not expected to be called in -// typical |X509_STORE_CTX| usage, but may be used in callback APIs where -// applications synthesize |X509_STORE_CTX| error conditions. See also -// |X509_STORE_CTX_set_verify_cb| and |SSL_CTX_set_cert_verify_callback|. +// X509_STORE_CTX_set_error sets `ctx`'s error to `err`, which should be +// `X509_V_OK` or an `X509_V_ERR_*` constant. It is not expected to be called in +// typical `X509_STORE_CTX` usage, but may be used in callback APIs where +// applications synthesize `X509_STORE_CTX` error conditions. See also +// `X509_STORE_CTX_set_verify_cb` and `SSL_CTX_set_cert_verify_callback`. OPENSSL_EXPORT void X509_STORE_CTX_set_error(X509_STORE_CTX *ctx, int err); -// X509_verify_cert_error_string returns |err| as a human-readable string, where -// |err| should be one of the |X509_V_*| values. If |err| is unknown, it returns +// X509_verify_cert_error_string returns `err` as a human-readable string, where +// `err` should be one of the `X509_V_*` values. If `err` is unknown, it returns // a default description. OPENSSL_EXPORT const char *X509_verify_cert_error_string(long err); // X509_STORE_CTX_get_error_depth returns the depth at which the error returned -// by |X509_STORE_CTX_get_error| occurred. This is zero-indexed integer into the +// by `X509_STORE_CTX_get_error` occurred. This is zero-indexed integer into the // certificate chain. Zero indicates the target certificate, one its issuer, and // so on. OPENSSL_EXPORT int X509_STORE_CTX_get_error_depth(const X509_STORE_CTX *ctx); // X509_STORE_CTX_get_current_cert returns the certificate which caused the -// error returned by |X509_STORE_CTX_get_error|. +// error returned by `X509_STORE_CTX_get_error`. OPENSSL_EXPORT X509 *X509_STORE_CTX_get_current_cert(const X509_STORE_CTX *ctx); -// X509_STORE_CTX_get0_current_crl returns the CRL which caused the error -// returned by |X509_STORE_CTX_get_error|. -OPENSSL_EXPORT X509_CRL *X509_STORE_CTX_get0_current_crl( - const X509_STORE_CTX *ctx); - -// X509_STORE_CTX_get0_store returns the |X509_STORE| that |ctx| uses. +// X509_STORE_CTX_get0_store returns the `X509_STORE` that `ctx` uses. OPENSSL_EXPORT X509_STORE *X509_STORE_CTX_get0_store(const X509_STORE_CTX *ctx); -// X509_STORE_CTX_get0_cert returns the leaf certificate that |ctx| is +// X509_STORE_CTX_get0_cert returns the leaf certificate that `ctx` is // verifying. OPENSSL_EXPORT X509 *X509_STORE_CTX_get0_cert(const X509_STORE_CTX *ctx); // X509_STORE_CTX_get0_untrusted returns the stack of untrusted intermediates -// used by |ctx| for certificate verification. +// used by `ctx` for certificate verification. OPENSSL_EXPORT STACK_OF(X509) *X509_STORE_CTX_get0_untrusted( const X509_STORE_CTX *ctx); -// X509_STORE_CTX_set0_trusted_stack configures |ctx| to trust the certificates -// in |sk|. |sk| must remain valid for the duration of |ctx|. Calling this -// function causes |ctx| to ignore any certificates configured in the -// |X509_STORE|. Certificates in |sk| are still subject to the check described -// in |X509_VERIFY_PARAM_set_trust|. +// X509_STORE_CTX_set0_trusted_stack configures `ctx` to trust the certificates +// in `sk`. `sk` must remain valid for the duration of `ctx`. Calling this +// function causes `ctx` to ignore any certificates configured in the +// `X509_STORE`. Certificates in `sk` are still subject to the check described +// in `X509_VERIFY_PARAM_set_trust`. // -// WARNING: This function differs from most |set0| functions in that it does not +// WARNING: This function differs from most `set0` functions in that it does not // take ownership of its input. The caller is required to ensure the lifetimes // are consistent. OPENSSL_EXPORT void X509_STORE_CTX_set0_trusted_stack(X509_STORE_CTX *ctx, STACK_OF(X509) *sk); -// X509_STORE_CTX_set0_crls configures |ctx| to consider the CRLs in |sk| as -// candidates for CRL lookup. |sk| must remain valid for the duration of |ctx|. -// These CRLs are considered in addition to CRLs found in |X509_STORE|. +// X509_STORE_CTX_set0_crls configures `ctx` to consider the CRLs in `sk` as +// candidates for CRL lookup. `sk` must remain valid for the duration of `ctx`. +// These CRLs are considered in addition to CRLs found in `X509_STORE`. // -// WARNING: This function differs from most |set0| functions in that it does not +// WARNING: This function differs from most `set0` functions in that it does not // take ownership of its input. The caller is required to ensure the lifetimes // are consistent. OPENSSL_EXPORT void X509_STORE_CTX_set0_crls(X509_STORE_CTX *ctx, STACK_OF(X509_CRL) *sk); -// X509_STORE_CTX_set_default looks up the set of parameters named |name| and -// applies those default verification parameters for |ctx|. As in -// |X509_VERIFY_PARAM_inherit|, only unset parameters are changed. This function +// X509_STORE_CTX_set_default looks up the set of parameters named `name` and +// applies those default verification parameters for `ctx`. As in +// `X509_VERIFY_PARAM_inherit`, only unset parameters are changed. This function // returns one on success and zero on error. // -// The supported values of |name| are: +// The supported values of `name` are: // - "default" is an internal value which configures some late defaults. See the -// discussion in |X509_STORE_get0_param|. +// discussion in `X509_STORE_get0_param`. // - "pkcs7" configures default trust and purpose checks for PKCS#7 signatures. // - "smime_sign" configures trust and purpose checks for S/MIME signatures. // - "ssl_client" configures trust and purpose checks for TLS clients. @@ -3123,133 +3129,130 @@ OPENSSL_EXPORT void X509_STORE_CTX_set0_crls(X509_STORE_CTX *ctx, OPENSSL_EXPORT int X509_STORE_CTX_set_default(X509_STORE_CTX *ctx, const char *name); -// X509_STORE_CTX_get0_param returns |ctx|'s verification parameters. This +// X509_STORE_CTX_get0_param returns `ctx`'s verification parameters. This // object is mutable and may be modified by the caller. OPENSSL_EXPORT X509_VERIFY_PARAM *X509_STORE_CTX_get0_param( X509_STORE_CTX *ctx); -// X509_STORE_CTX_set0_param returns |ctx|'s verification parameters to |param| -// and takes ownership of |param|. After this function returns, the caller -// should not free |param|. +// X509_STORE_CTX_set0_param returns `ctx`'s verification parameters to `param` +// and takes ownership of `param`. After this function returns, the caller +// should not free `param`. // // WARNING: This function discards any values which were previously applied in -// |ctx|, including the "default" parameters applied late in -// |X509_STORE_CTX_init|. These late defaults are not applied to parameters -// created standalone by |X509_VERIFY_PARAM_new|. +// `ctx`, including the "default" parameters applied late in +// `X509_STORE_CTX_init`. These late defaults are not applied to parameters +// created standalone by `X509_VERIFY_PARAM_new`. // // TODO(crbug.com/boringssl/441): This behavior is very surprising. Should we -// re-apply the late defaults in |param|, or somehow avoid this notion of late +// re-apply the late defaults in `param`, or somehow avoid this notion of late // defaults altogether? OPENSSL_EXPORT void X509_STORE_CTX_set0_param(X509_STORE_CTX *ctx, X509_VERIFY_PARAM *param); -// X509_STORE_CTX_set_flags enables all values in |flags| in |ctx|'s -// verification flags. |flags| should be a combination of |X509_V_FLAG_*| +// X509_STORE_CTX_set_flags enables all values in `flags` in `ctx`'s +// verification flags. `flags` should be a combination of `X509_V_FLAG_*` // constants. OPENSSL_EXPORT void X509_STORE_CTX_set_flags(X509_STORE_CTX *ctx, unsigned long flags); -// X509_STORE_CTX_set_time configures certificate verification to use |t| -// instead of the current time. |flags| is ignored and should be zero. +// X509_STORE_CTX_set_time configures certificate verification to use `t` +// instead of the current time. `flags` is ignored and should be zero. OPENSSL_EXPORT void X509_STORE_CTX_set_time(X509_STORE_CTX *ctx, unsigned long flags, time_t t); -// X509_STORE_CTX_set_time_posix configures certificate verification to use |t| -// instead of the current time. |t| is interpreted as a POSIX timestamp in -// seconds. |flags| is ignored and should be zero. +// X509_STORE_CTX_set_time_posix configures certificate verification to use `t` +// instead of the current time. `t` is interpreted as a POSIX timestamp in +// seconds. `flags` is ignored and should be zero. OPENSSL_EXPORT void X509_STORE_CTX_set_time_posix(X509_STORE_CTX *ctx, unsigned long flags, int64_t t); -// X509_STORE_CTX_set_depth configures |ctx| to, by default, limit certificate -// chains to |depth| intermediate certificates. This count excludes both the +// X509_STORE_CTX_set_depth configures `ctx` to, by default, limit certificate +// chains to `depth` intermediate certificates. This count excludes both the // target certificate and the trust anchor (root certificate). OPENSSL_EXPORT void X509_STORE_CTX_set_depth(X509_STORE_CTX *ctx, int depth); -// X509_STORE_CTX_set_purpose simultaneously configures |ctx|'s purpose and -// trust checks, if unset. It returns one on success and zero if |purpose| is -// not a valid purpose value. |purpose| should be an |X509_PURPOSE_*| constant. -// If so, it configures |ctx| with a purpose check of |purpose| and a trust -// check of |purpose|'s corresponding trust value. If either the purpose or -// trust check had already been specified for |ctx|, that corresponding +// X509_STORE_CTX_set_purpose simultaneously configures `ctx`'s purpose and +// trust checks, if unset. It returns one on success and zero if `purpose` is +// not a valid purpose value. `purpose` should be an `X509_PURPOSE_*` constant. +// If so, it configures `ctx` with a purpose check of `purpose` and a trust +// check of `purpose`'s corresponding trust value. If either the purpose or +// trust check had already been specified for `ctx`, that corresponding // modification is silently dropped. // -// See |X509_VERIFY_PARAM_set_purpose| and |X509_VERIFY_PARAM_set_trust| for +// See `X509_VERIFY_PARAM_set_purpose` and `X509_VERIFY_PARAM_set_trust` for // details on the purpose and trust checks, respectively. // -// If |purpose| is |X509_PURPOSE_ANY|, this function returns an error because it -// has no corresponding |X509_TRUST_*| value. It is not possible to set -// |X509_PURPOSE_ANY| with this function, only |X509_VERIFY_PARAM_set_purpose|. +// If `purpose` is `X509_PURPOSE_ANY`, this function returns an error because it +// has no corresponding `X509_TRUST_*` value. It is not possible to set +// `X509_PURPOSE_ANY` with this function, only `X509_VERIFY_PARAM_set_purpose`. // // WARNING: Unlike similarly named functions in this header, this function -// silently does not behave the same as |X509_VERIFY_PARAM_set_purpose|. Callers -// may use |X509_VERIFY_PARAM_set_purpose| with |X509_STORE_CTX_get0_param| to +// silently does not behave the same as `X509_VERIFY_PARAM_set_purpose`. Callers +// may use `X509_VERIFY_PARAM_set_purpose` with `X509_STORE_CTX_get0_param` to // avoid this difference. OPENSSL_EXPORT int X509_STORE_CTX_set_purpose(X509_STORE_CTX *ctx, int purpose); -// X509_STORE_CTX_set_trust configures |ctx|'s trust check, if unset. It returns -// one on success and zero if |trust| is not a valid trust value. |trust| should -// be an |X509_TRUST_*| constant. If so, it configures |ctx| with a trust check -// of |trust|. If the trust check had already been specified for |ctx|, it +// X509_STORE_CTX_set_trust configures `ctx`'s trust check, if unset. It returns +// one on success and zero if `trust` is not a valid trust value. `trust` should +// be an `X509_TRUST_*` constant. If so, it configures `ctx` with a trust check +// of `trust`. If the trust check had already been specified for `ctx`, it // silently does nothing. // -// See |X509_VERIFY_PARAM_set_trust| for details on the purpose and trust check. +// See `X509_VERIFY_PARAM_set_trust` for details on the purpose and trust check. // // WARNING: Unlike similarly named functions in this header, this function -// does not behave the same as |X509_VERIFY_PARAM_set_trust|. Callers may use -// |X509_VERIFY_PARAM_set_trust| with |X509_STORE_CTX_get0_param| to avoid this +// does not behave the same as `X509_VERIFY_PARAM_set_trust`. Callers may use +// `X509_VERIFY_PARAM_set_trust` with `X509_STORE_CTX_get0_param` to avoid this // difference. OPENSSL_EXPORT int X509_STORE_CTX_set_trust(X509_STORE_CTX *ctx, int trust); // Verification parameters. // -// An |X509_VERIFY_PARAM| contains a set of parameters for certificate +// An `X509_VERIFY_PARAM` contains a set of parameters for certificate // verification. -// X509_VERIFY_PARAM_new returns a newly-allocated |X509_VERIFY_PARAM|, or NULL +// X509_VERIFY_PARAM_new returns a newly-allocated `X509_VERIFY_PARAM`, or NULL // on error. OPENSSL_EXPORT X509_VERIFY_PARAM *X509_VERIFY_PARAM_new(void); -// X509_VERIFY_PARAM_free releases memory associated with |param|. +// X509_VERIFY_PARAM_free releases memory associated with `param`. OPENSSL_EXPORT void X509_VERIFY_PARAM_free(X509_VERIFY_PARAM *param); -// X509_VERIFY_PARAM_inherit applies |from| as the default values for |to|. That -// is, for each parameter that is unset in |to|, it copies the value in |from|. +// X509_VERIFY_PARAM_inherit applies `from` as the default values for `to`. That +// is, for each parameter that is unset in `to`, it copies the value in `from`. // This function returns one on success and zero on error. OPENSSL_EXPORT int X509_VERIFY_PARAM_inherit(X509_VERIFY_PARAM *to, const X509_VERIFY_PARAM *from); -// X509_VERIFY_PARAM_set1 copies parameters from |from| to |to|. If a parameter -// is unset in |from|, the existing value in |to| is preserved. This function +// X509_VERIFY_PARAM_set1 copies parameters from `from` to `to`. If a parameter +// is unset in `from`, the existing value in `to` is preserved. This function // returns one on success and zero on error. OPENSSL_EXPORT int X509_VERIFY_PARAM_set1(X509_VERIFY_PARAM *to, const X509_VERIFY_PARAM *from); -// X509_V_FLAG_* are flags for |X509_VERIFY_PARAM_set_flags| and -// |X509_VERIFY_PARAM_clear_flags|. +// X509_V_FLAG_* are flags for `X509_VERIFY_PARAM_set_flags` and +// `X509_VERIFY_PARAM_clear_flags`. // X509_V_FLAG_CB_ISSUER_CHECK causes the deprecated verify callback (see -// |X509_STORE_CTX_set_verify_cb|) to be called for errors while matching +// `X509_STORE_CTX_set_verify_cb`) to be called for errors while matching // subject and issuer certificates. #define X509_V_FLAG_CB_ISSUER_CHECK 0x1 // X509_V_FLAG_USE_CHECK_TIME is an internal flag used to track whether -// |X509_STORE_CTX_set_time| has been used. If cleared, the system time is +// `X509_STORE_CTX_set_time` has been used. If cleared, the system time is // restored. #define X509_V_FLAG_USE_CHECK_TIME 0x2 // X509_V_FLAG_CRL_CHECK enables CRL lookup and checking for the leaf. #define X509_V_FLAG_CRL_CHECK 0x4 // X509_V_FLAG_CRL_CHECK_ALL enables CRL lookup and checking for the entire -// certificate chain. |X509_V_FLAG_CRL_CHECK| must be set for this flag to take +// certificate chain. `X509_V_FLAG_CRL_CHECK` must be set for this flag to take // effect. #define X509_V_FLAG_CRL_CHECK_ALL 0x8 // X509_V_FLAG_IGNORE_CRITICAL ignores unhandled critical extensions. Do not use // this option. Critical extensions ensure the verifier does not bypass // unrecognized security restrictions in certificates. #define X509_V_FLAG_IGNORE_CRITICAL 0x10 -// X509_V_FLAG_X509_STRICT does nothing. Its functionality has been enabled by -// default. -#define X509_V_FLAG_X509_STRICT 0x00 // X509_V_FLAG_ALLOW_PROXY_CERTS does nothing. Proxy certificate support has // been removed. #define X509_V_FLAG_ALLOW_PROXY_CERTS 0x40 @@ -3273,93 +3276,109 @@ OPENSSL_EXPORT int X509_VERIFY_PARAM_set1(X509_VERIFY_PARAM *to, // X509_V_FLAG_USE_DELTAS causes all verifications to fail. Delta CRL support // has been removed. #define X509_V_FLAG_USE_DELTAS 0x2000 -// X509_V_FLAG_CHECK_SS_SIGNATURE checks the redundant signature on self-signed -// trust anchors. This check provides no security benefit and only wastes CPU. -#define X509_V_FLAG_CHECK_SS_SIGNATURE 0x4000 -// X509_V_FLAG_TRUSTED_FIRST, during path-building, checks for a match in the -// trust store before considering an untrusted intermediate. This flag is -// enabled by default. -#define X509_V_FLAG_TRUSTED_FIRST 0x8000 // X509_V_FLAG_PARTIAL_CHAIN treats all trusted certificates as trust anchors, -// independent of the |X509_VERIFY_PARAM_set_trust| setting. +// independent of the `X509_VERIFY_PARAM_set_trust` setting. #define X509_V_FLAG_PARTIAL_CHAIN 0x80000 -// X509_V_FLAG_NO_ALT_CHAINS disables building alternative chains if the initial -// one was rejected. -#define X509_V_FLAG_NO_ALT_CHAINS 0x100000 // X509_V_FLAG_NO_CHECK_TIME disables all time checks in certificate // verification. #define X509_V_FLAG_NO_CHECK_TIME 0x200000 +// X509_V_FLAG_USE_MTC_DRAFT_PLANTS_07 enables the verification of Merkle Tree +// Certificates as specified in draft-ietf-plants-merkle-tree-certs-07. +#define X509_V_FLAG_USE_MTC_DRAFT_PLANTS_07 0x400000 +// X509_V_FLAG_ALLOW_TIMEZONE_OFFSET allows `notBefore` and `notAfter` fields +// to contain a time zone offset. +#define X509_V_FLAG_ALLOW_TIMEZONE_OFFSET 0x1000000 +// X509_V_FLAG_ALLOW_SHA1 allows certificates and CRLs signed with SHA-1 to be +// accepted during verification. +// This flag is tentatively set by default. Users should add this explicitly if +// they want to allow SHA-1. +#define X509_V_FLAG_ALLOW_SHA1 0x2000000 + +// Deprecated flags. -// X509_VERIFY_PARAM_set_flags enables all values in |flags| in |param|'s -// verification flags and returns one. |flags| should be a combination of -// |X509_V_FLAG_*| constants. +// X509_V_FLAG_X509_STRICT does nothing. Its functionality has been enabled by +// default. +#define X509_V_FLAG_X509_STRICT 0x00 +// X509_V_FLAG_CHECK_SS_SIGNATURE does nothing. Historically it would check the +// redundant signature on self-signed trust anchors. This check provided no +// security benefit and only wasted CPU. +#define X509_V_FLAG_CHECK_SS_SIGNATURE 0x0 +// X509_V_FLAG_TRUSTED_FIRST does nothing. The behavior it controls is always +// enabled. +#define X509_V_FLAG_TRUSTED_FIRST 0x0 +// X509_V_FLAG_NO_ALT_CHAINS does nothing. +#define X509_V_FLAG_NO_ALT_CHAINS 0x0 + +// X509_VERIFY_PARAM_set_flags enables all values in `flags` in `param`'s +// verification flags and returns one. `flags` should be a combination of +// `X509_V_FLAG_*` constants. OPENSSL_EXPORT int X509_VERIFY_PARAM_set_flags(X509_VERIFY_PARAM *param, unsigned long flags); -// X509_VERIFY_PARAM_clear_flags disables all values in |flags| in |param|'s -// verification flags and returns one. |flags| should be a combination of -// |X509_V_FLAG_*| constants. +// X509_VERIFY_PARAM_clear_flags disables all values in `flags` in `param`'s +// verification flags and returns one. `flags` should be a combination of +// `X509_V_FLAG_*` constants. OPENSSL_EXPORT int X509_VERIFY_PARAM_clear_flags(X509_VERIFY_PARAM *param, unsigned long flags); -// X509_VERIFY_PARAM_get_flags returns |param|'s verification flags. +// X509_VERIFY_PARAM_get_flags returns `param`'s verification flags. OPENSSL_EXPORT unsigned long X509_VERIFY_PARAM_get_flags( const X509_VERIFY_PARAM *param); -// X509_VERIFY_PARAM_set_depth configures |param| to limit certificate chains to -// |depth| intermediate certificates. This count excludes both the target +// X509_VERIFY_PARAM_set_depth configures `param` to limit certificate chains to +// `depth` intermediate certificates. This count excludes both the target // certificate and the trust anchor (root certificate). OPENSSL_EXPORT void X509_VERIFY_PARAM_set_depth(X509_VERIFY_PARAM *param, int depth); -// X509_VERIFY_PARAM_get_depth returns the maximum depth configured in |param|. -// See |X509_VERIFY_PARAM_set_depth|. +// X509_VERIFY_PARAM_get_depth returns the maximum depth configured in `param`. +// See `X509_VERIFY_PARAM_set_depth`. OPENSSL_EXPORT int X509_VERIFY_PARAM_get_depth(const X509_VERIFY_PARAM *param); -// X509_VERIFY_PARAM_set_time configures certificate verification to use |t| +// X509_VERIFY_PARAM_set_time configures certificate verification to use `t` // instead of the current time. OPENSSL_EXPORT void X509_VERIFY_PARAM_set_time(X509_VERIFY_PARAM *param, time_t t); // X509_VERIFY_PARAM_set_time_posix configures certificate verification to use -// |t| instead of the current time. |t| is interpreted as a POSIX timestamp in +// `t` instead of the current time. `t` is interpreted as a POSIX timestamp in // seconds. OPENSSL_EXPORT void X509_VERIFY_PARAM_set_time_posix(X509_VERIFY_PARAM *param, int64_t t); -// X509_VERIFY_PARAM_add0_policy adds |policy| to the user-initial-policy-set +// X509_VERIFY_PARAM_add0_policy adds `policy` to the user-initial-policy-set // (see Section 6.1.1 of RFC 5280). On success, it takes ownership of -// |policy| and returns one. Otherwise, it returns zero and the caller retains -// owneship of |policy|. +// `policy` and returns one. Otherwise, it returns zero and the caller retains +// ownership of `policy`. OPENSSL_EXPORT int X509_VERIFY_PARAM_add0_policy(X509_VERIFY_PARAM *param, ASN1_OBJECT *policy); // X509_VERIFY_PARAM_set1_policies sets the user-initial-policy-set (see -// Section 6.1.1 of RFC 5280) to a copy of |policies|. It returns one on success +// Section 6.1.1 of RFC 5280) to a copy of `policies`. It returns one on success // and zero on error. OPENSSL_EXPORT int X509_VERIFY_PARAM_set1_policies( X509_VERIFY_PARAM *param, const STACK_OF(ASN1_OBJECT) *policies); -// X509_VERIFY_PARAM_set1_host configures |param| to check for the DNS name -// specified by |name|. It returns one on success and zero on error. +// X509_VERIFY_PARAM_set1_host configures `param` to check for the DNS name +// specified by `name`. It returns one on success and zero on error. // // By default, both subject alternative names and the subject's common name // attribute are checked. The latter has long been deprecated, so callers should -// call |X509_VERIFY_PARAM_set_hostflags| with -// |X509_CHECK_FLAG_NEVER_CHECK_SUBJECT| to use the standard behavior. +// call `X509_VERIFY_PARAM_set_hostflags` with +// `X509_CHECK_FLAG_NEVER_CHECK_SUBJECT` to use the standard behavior. // https://crbug.com/boringssl/464 tracks fixing the default. OPENSSL_EXPORT int X509_VERIFY_PARAM_set1_host(X509_VERIFY_PARAM *param, const char *name, size_t name_len); -// X509_VERIFY_PARAM_add1_host adds |name| to the list of names checked by -// |param|. If any configured DNS name matches the certificate, verification +// X509_VERIFY_PARAM_add1_host adds `name` to the list of names checked by +// `param`. If any configured DNS name matches the certificate, verification // succeeds. It returns one on success and zero on error. // // By default, both subject alternative names and the subject's common name // attribute are checked. The latter has long been deprecated, so callers should -// call |X509_VERIFY_PARAM_set_hostflags| with -// |X509_CHECK_FLAG_NEVER_CHECK_SUBJECT| to use the standard behavior. +// call `X509_VERIFY_PARAM_set_hostflags` with +// `X509_CHECK_FLAG_NEVER_CHECK_SUBJECT` to use the standard behavior. // https://crbug.com/boringssl/464 tracks fixing the default. OPENSSL_EXPORT int X509_VERIFY_PARAM_add1_host(X509_VERIFY_PARAM *param, const char *name, @@ -3372,30 +3391,30 @@ OPENSSL_EXPORT int X509_VERIFY_PARAM_add1_host(X509_VERIFY_PARAM *param, // enabled when subjectAltNames is missing. #define X509_CHECK_FLAG_NEVER_CHECK_SUBJECT 0x20 -// X509_VERIFY_PARAM_set_hostflags sets the name-checking flags on |param| to -// |flags|. |flags| should be a combination of |X509_CHECK_FLAG_*| constants. +// X509_VERIFY_PARAM_set_hostflags sets the name-checking flags on `param` to +// `flags`. `flags` should be a combination of `X509_CHECK_FLAG_*` constants. OPENSSL_EXPORT void X509_VERIFY_PARAM_set_hostflags(X509_VERIFY_PARAM *param, unsigned int flags); -// X509_VERIFY_PARAM_set1_email configures |param| to check for the email -// address specified by |email|. It returns one on success and zero on error. +// X509_VERIFY_PARAM_set1_email configures `param` to check for the email +// address specified by `email`. It returns one on success and zero on error. // // By default, both subject alternative names and the subject's email address -// attribute are checked. The |X509_CHECK_FLAG_NEVER_CHECK_SUBJECT| flag may be +// attribute are checked. The `X509_CHECK_FLAG_NEVER_CHECK_SUBJECT` flag may be // used to change this behavior. OPENSSL_EXPORT int X509_VERIFY_PARAM_set1_email(X509_VERIFY_PARAM *param, const char *email, size_t email_len); -// X509_VERIFY_PARAM_set1_ip configures |param| to check for the IP address -// specified by |ip|. It returns one on success and zero on error. The IP -// address is specified in its binary representation. |ip_len| must be 4 for an +// X509_VERIFY_PARAM_set1_ip configures `param` to check for the IP address +// specified by `ip`. It returns one on success and zero on error. The IP +// address is specified in its binary representation. `ip_len` must be 4 for an // IPv4 address and 16 for an IPv6 address. OPENSSL_EXPORT int X509_VERIFY_PARAM_set1_ip(X509_VERIFY_PARAM *param, const uint8_t *ip, size_t ip_len); -// X509_VERIFY_PARAM_set1_ip_asc decodes |ipasc| as the ASCII representation of -// an IPv4 or IPv6 address, and configures |param| to check for it. It returns +// X509_VERIFY_PARAM_set1_ip_asc decodes `ipasc` as the ASCII representation of +// an IPv4 or IPv6 address, and configures `param` to check for it. It returns // one on success and zero on error. OPENSSL_EXPORT int X509_VERIFY_PARAM_set1_ip_asc(X509_VERIFY_PARAM *param, const char *ipasc); @@ -3411,7 +3430,7 @@ OPENSSL_EXPORT int X509_VERIFY_PARAM_set1_ip_asc(X509_VERIFY_PARAM *param, // usage specific to the negotiated TLS parameters. #define X509_PURPOSE_SSL_SERVER 2 // X509_PURPOSE_NS_SSL_SERVER is a legacy mode. It behaves like -// |X509_PURPOSE_SSL_SERVER|, but only accepts the keyEncipherment key usage, +// `X509_PURPOSE_SSL_SERVER`, but only accepts the keyEncipherment key usage, // used by SSL 2.0 and RSA key exchange. Do not use this. #define X509_PURPOSE_NS_SSL_SERVER 3 // X509_PURPOSE_SMIME_SIGN validates S/MIME signing certificates. It checks for @@ -3439,9 +3458,9 @@ OPENSSL_EXPORT int X509_VERIFY_PARAM_set1_ip_asc(X509_VERIFY_PARAM *param, // asserted. #define X509_PURPOSE_TIMESTAMP_SIGN 9 -// X509_VERIFY_PARAM_set_purpose configures |param| to validate certificates for -// a specified purpose. It returns one on success and zero if |purpose| is not a -// valid purpose type. |purpose| should be one of the |X509_PURPOSE_*| values. +// X509_VERIFY_PARAM_set_purpose configures `param` to validate certificates for +// a specified purpose. It returns one on success and zero if `purpose` is not a +// valid purpose type. `purpose` should be one of the `X509_PURPOSE_*` values. // // This option controls checking the extended key usage (EKU) and key usage // extensions. These extensions specify how a certificate's public key may be @@ -3455,13 +3474,13 @@ OPENSSL_EXPORT int X509_VERIFY_PARAM_set1_ip_asc(X509_VERIFY_PARAM *param, // not accept anyExtendedKeyUsage. // // Many purpose values have a corresponding trust value, which is not configured -// by this function. See |X509_VERIFY_PARAM_set_trust| for details. Callers +// by this function. See `X509_VERIFY_PARAM_set_trust` for details. Callers // that wish to configure both should either call both functions, or use -// |X509_STORE_CTX_set_purpose|. +// `X509_STORE_CTX_set_purpose`. // // It is currently not possible to configure custom EKU OIDs or key usage bits. // Contact the BoringSSL maintainers if your application needs to do so. OpenSSL -// had an |X509_PURPOSE_add| API, but it was not thread-safe and relied on +// had an `X509_PURPOSE_add` API, but it was not thread-safe and relied on // global mutable state, so we removed it. // // TODO(davidben): This function additionally configures checking the legacy @@ -3472,52 +3491,52 @@ OPENSSL_EXPORT int X509_VERIFY_PARAM_set_purpose(X509_VERIFY_PARAM *param, // X509_TRUST_COMPAT evaluates trust using only the self-signed fallback. Trust // and distrust OIDs are ignored. #define X509_TRUST_COMPAT 1 -// X509_TRUST_SSL_CLIENT evaluates trust with the |NID_client_auth| OID, for +// X509_TRUST_SSL_CLIENT evaluates trust with the `NID_client_auth` OID, for // validating TLS client certificates. #define X509_TRUST_SSL_CLIENT 2 -// X509_TRUST_SSL_SERVER evaluates trust with the |NID_server_auth| OID, for +// X509_TRUST_SSL_SERVER evaluates trust with the `NID_server_auth` OID, for // validating TLS server certificates. #define X509_TRUST_SSL_SERVER 3 -// X509_TRUST_EMAIL evaluates trust with the |NID_email_protect| OID, for +// X509_TRUST_EMAIL evaluates trust with the `NID_email_protect` OID, for // validating S/MIME email certificates. #define X509_TRUST_EMAIL 4 -// X509_TRUST_OBJECT_SIGN evaluates trust with the |NID_code_sign| OID, for +// X509_TRUST_OBJECT_SIGN evaluates trust with the `NID_code_sign` OID, for // validating code signing certificates. #define X509_TRUST_OBJECT_SIGN 5 -// X509_TRUST_TSA evaluates trust with the |NID_time_stamp| OID, for validating +// X509_TRUST_TSA evaluates trust with the `NID_time_stamp` OID, for validating // Time Stamping Authority (RFC 3161) certificates. #define X509_TRUST_TSA 8 -// X509_VERIFY_PARAM_set_trust configures which certificates from |X509_STORE| -// are trust anchors. It returns one on success and zero if |trust| is not a -// valid trust value. |trust| should be one of the |X509_TRUST_*| constants. +// X509_VERIFY_PARAM_set_trust configures which certificates from `X509_STORE` +// are trust anchors. It returns one on success and zero if `trust` is not a +// valid trust value. `trust` should be one of the `X509_TRUST_*` constants. // This function allows applications to vary trust anchors when the same set of // trusted certificates is used in multiple contexts. // // Two properties determine whether a certificate is a trust anchor: // // - Whether it is trusted or distrusted for some OID, via auxiliary information -// configured by |X509_add1_trust_object| or |X509_add1_reject_object|. +// configured by `X509_add1_trust_object` or `X509_add1_reject_object`. // -// - Whether it is "self-signed". That is, whether |X509_get_extension_flags| -// includes |EXFLAG_SS|. The signature itself is not checked. +// - Whether it is "self-signed". That is, whether `X509_get_extension_flags` +// includes `EXFLAG_SS`. The signature itself is not checked. // -// When this function is called, |trust| determines the OID to check in the +// When this function is called, `trust` determines the OID to check in the // first case. If the certificate is not explicitly trusted or distrusted for // any OID, it is trusted if self-signed instead. // -// If unset, the default behavior is to check for the |NID_anyExtendedKeyUsage| +// If unset, the default behavior is to check for the `NID_anyExtendedKeyUsage` // OID. If the certificate is not explicitly trusted or distrusted for this OID, // it is trusted if self-signed instead. Note this slightly differs from the // above. // -// If the |X509_V_FLAG_PARTIAL_CHAIN| is set, every certificate from -// |X509_STORE| is a trust anchor, unless it was explicitly distrusted for the +// If the `X509_V_FLAG_PARTIAL_CHAIN` is set, every certificate from +// `X509_STORE` is a trust anchor, unless it was explicitly distrusted for the // OID. // // It is currently not possible to configure custom trust OIDs. Contact the // BoringSSL maintainers if your application needs to do so. OpenSSL had an -// |X509_TRUST_add| API, but it was not thread-safe and relied on global mutable +// `X509_TRUST_add` API, but it was not thread-safe and relied on global mutable // state, so we removed it. OPENSSL_EXPORT int X509_VERIFY_PARAM_set_trust(X509_VERIFY_PARAM *param, int trust); @@ -3525,160 +3544,160 @@ OPENSSL_EXPORT int X509_VERIFY_PARAM_set_trust(X509_VERIFY_PARAM *param, // Filesystem-based certificate stores. // -// An |X509_STORE| may be configured to get its contents from the filesystem. -// This is done by adding |X509_LOOKUP| structures to the |X509_STORE| with -// |X509_STORE_add_lookup| and then configuring the |X509_LOOKUP| with paths. +// An `X509_STORE` may be configured to get its contents from the filesystem. +// This is done by adding `X509_LOOKUP` structures to the `X509_STORE` with +// `X509_STORE_add_lookup` and then configuring the `X509_LOOKUP` with paths. // -// Most cases can use |X509_STORE_load_locations|, which configures the same +// Most cases can use `X509_STORE_load_locations`, which configures the same // thing but is simpler to use. -// X509_STORE_load_locations configures |store| to load data from filepaths -// |file| and |dir|. It returns one on success and zero on error. Either of -// |file| or |dir| may be NULL, but at least one must be non-NULL. +// X509_STORE_load_locations configures `store` to load data from filepaths +// `file` and `dir`. It returns one on success and zero on error. Either of +// `file` or `dir` may be NULL, but at least one must be non-NULL. // -// If |file| is non-NULL, it loads CRLs and trusted certificates in PEM format -// from the file at |file|, and them to |store|, as in |X509_load_cert_crl_file| -// with |X509_FILETYPE_PEM|. +// If `file` is non-NULL, it loads CRLs and trusted certificates in PEM format +// from the file at `file`, and them to `store`, as in `X509_load_cert_crl_file` +// with `X509_FILETYPE_PEM`. // -// If |dir| is non-NULL, it configures |store| to load CRLs and trusted -// certificates from the directory at |dir| in PEM format, as in -// |X509_LOOKUP_add_dir| with |X509_FILETYPE_PEM|. +// If `dir` is non-NULL, it configures `store` to load CRLs and trusted +// certificates from the directory at `dir` in PEM format, as in +// `X509_LOOKUP_add_dir` with `X509_FILETYPE_PEM`. OPENSSL_EXPORT int X509_STORE_load_locations(X509_STORE *store, const char *file, const char *dir); -// X509_STORE_add_lookup returns an |X509_LOOKUP| associated with |store| with -// type |method|, or NULL on error. The result is owned by |store|, so callers -// are not expected to free it. This may be used with |X509_LOOKUP_add_dir| or -// |X509_LOOKUP_load_file|, depending on |method|, to configure |store|. +// X509_STORE_add_lookup returns an `X509_LOOKUP` associated with `store` with +// type `method`, or NULL on error. The result is owned by `store`, so callers +// are not expected to free it. This may be used with `X509_LOOKUP_add_dir` or +// `X509_LOOKUP_load_file`, depending on `method`, to configure `store`. // -// A single |X509_LOOKUP| may be configured with multiple paths, and an -// |X509_STORE| only contains one |X509_LOOKUP| of each type, so there is no +// A single `X509_LOOKUP` may be configured with multiple paths, and an +// `X509_STORE` only contains one `X509_LOOKUP` of each type, so there is no // need to call this function multiple times for a single type. Calling it -// multiple times will return the previous |X509_LOOKUP| of that type. +// multiple times will return the previous `X509_LOOKUP` of that type. OPENSSL_EXPORT X509_LOOKUP *X509_STORE_add_lookup( X509_STORE *store, const X509_LOOKUP_METHOD *method); -// X509_LOOKUP_hash_dir creates |X509_LOOKUP|s that may be used with -// |X509_LOOKUP_add_dir|. +// X509_LOOKUP_hash_dir creates `X509_LOOKUP`s that may be used with +// `X509_LOOKUP_add_dir`. OPENSSL_EXPORT const X509_LOOKUP_METHOD *X509_LOOKUP_hash_dir(void); -// X509_LOOKUP_file creates |X509_LOOKUP|s that may be used with -// |X509_LOOKUP_load_file|. +// X509_LOOKUP_file creates `X509_LOOKUP`s that may be used with +// `X509_LOOKUP_load_file`. // -// Although this is modeled as an |X509_LOOKUP|, this function is redundant. It +// Although this is modeled as an `X509_LOOKUP`, this function is redundant. It // has the same effect as loading a certificate or CRL from the filesystem, in -// the caller's desired format, and then adding it with |X509_STORE_add_cert| -// and |X509_STORE_add_crl|. +// the caller's desired format, and then adding it with `X509_STORE_add_cert` +// and `X509_STORE_add_crl`. OPENSSL_EXPORT const X509_LOOKUP_METHOD *X509_LOOKUP_file(void); // The following constants are used to specify the format of files in an -// |X509_LOOKUP|. +// `X509_LOOKUP`. #define X509_FILETYPE_PEM 1 #define X509_FILETYPE_ASN1 2 #define X509_FILETYPE_DEFAULT 3 -// X509_LOOKUP_load_file calls |X509_load_cert_crl_file|. |lookup| must have -// been constructed with |X509_LOOKUP_file|. +// X509_LOOKUP_load_file calls `X509_load_cert_crl_file`. `lookup` must have +// been constructed with `X509_LOOKUP_file`. // -// If |type| is |X509_FILETYPE_DEFAULT|, it ignores |file| and instead uses some -// default system path with |X509_FILETYPE_PEM|. See also -// |X509_STORE_set_default_paths|. +// If `type` is `X509_FILETYPE_DEFAULT`, it ignores `file` and instead uses some +// default system path with `X509_FILETYPE_PEM`. See also +// `X509_STORE_set_default_paths`. OPENSSL_EXPORT int X509_LOOKUP_load_file(X509_LOOKUP *lookup, const char *file, int type); -// X509_LOOKUP_add_dir configures |lookup| to load CRLs and trusted certificates -// from the directories in |path|. It returns one on success and zero on error. -// |lookup| must have been constructed with |X509_LOOKUP_hash_dir|. +// X509_LOOKUP_add_dir configures `lookup` to load CRLs and trusted certificates +// from the directories in `path`. It returns one on success and zero on error. +// `lookup` must have been constructed with `X509_LOOKUP_hash_dir`. // -// WARNING: |path| is interpreted as a colon-separated (semicolon-separated on +// WARNING: `path` is interpreted as a colon-separated (semicolon-separated on // Windows) list of paths. It is not possible to configure a path containing the // separator character. https://crbug.com/boringssl/691 tracks removing this // behavior. // -// |type| should be one of the |X509_FILETYPE_*| constants and determines the -// format of the files. If |type| is |X509_FILETYPE_DEFAULT|, |path| is ignored -// and some default system path is used with |X509_FILETYPE_PEM|. See also -// |X509_STORE_set_default_paths|. +// `type` should be one of the `X509_FILETYPE_*` constants and determines the +// format of the files. If `type` is `X509_FILETYPE_DEFAULT`, `path` is ignored +// and some default system path is used with `X509_FILETYPE_PEM`. See also +// `X509_STORE_set_default_paths`. // // Trusted certificates should be named HASH.N and CRLs should be -// named HASH.rN. HASH is |X509_NAME_hash| of the certificate subject and CRL +// named HASH.rN. HASH is `X509_NAME_hash` of the certificate subject and CRL // issuer, respectively, in hexadecimal. N is in decimal and counts hash // collisions consecutively, starting from zero. For example, "002c0b4f.0" and // "002c0b4f.r0". // -// WARNING: Objects from |path| are loaded on demand, but cached in memory on -// the |X509_STORE|. If a CA is removed from the directory, existing -// |X509_STORE|s will continue to trust it. Cache entries are not evicted for -// the lifetime of the |X509_STORE|. +// WARNING: Objects from `path` are loaded on demand, but cached in memory on +// the `X509_STORE`. If a CA is removed from the directory, existing +// `X509_STORE`s will continue to trust it. Cache entries are not evicted for +// the lifetime of the `X509_STORE`. // // WARNING: This mechanism is also not well-suited for CRL updates. -// |X509_STORE|s rely on this cache and never load the same CRL file twice. CRL +// `X509_STORE`s rely on this cache and never load the same CRL file twice. CRL // updates must use a new file, with an incremented suffix, to be reflected in -// existing |X509_STORE|s. However, this means each CRL update will use +// existing `X509_STORE`s. However, this means each CRL update will use // additional storage and memory. Instead, configure inputs that vary per -// verification, such as CRLs, on each |X509_STORE_CTX| separately, using -// functions like |X509_STORE_CTX_set0_crl|. +// verification, such as CRLs, on each `X509_STORE_CTX` separately, using +// functions like `X509_STORE_CTX_set0_crl`. OPENSSL_EXPORT int X509_LOOKUP_add_dir(X509_LOOKUP *lookup, const char *path, int type); -// X509_L_* are commands for |X509_LOOKUP_ctrl|. +// X509_L_* are commands for `X509_LOOKUP_ctrl`. #define X509_L_FILE_LOAD 1 #define X509_L_ADD_DIR 2 -// X509_LOOKUP_ctrl implements commands on |lookup|. |cmd| specifies the +// X509_LOOKUP_ctrl implements commands on `lookup`. `cmd` specifies the // command. The other arguments specify the operation in a command-specific way. -// Use |X509_LOOKUP_load_file| or |X509_LOOKUP_add_dir| instead. +// Use `X509_LOOKUP_load_file` or `X509_LOOKUP_add_dir` instead. OPENSSL_EXPORT int X509_LOOKUP_ctrl(X509_LOOKUP *lookup, int cmd, const char *argc, long argl, char **ret); -// X509_load_cert_file loads trusted certificates from |file| and adds them to -// |lookup|'s |X509_STORE|. It returns one on success and zero on error. +// X509_load_cert_file loads trusted certificates from `file` and adds them to +// `lookup`'s `X509_STORE`. It returns one on success and zero on error. // -// If |type| is |X509_FILETYPE_ASN1|, it loads a single DER-encoded certificate. -// If |type| is |X509_FILETYPE_PEM|, it loads a sequence of PEM-encoded -// certificates. |type| may not be |X509_FILETYPE_DEFAULT|. +// If `type` is `X509_FILETYPE_ASN1`, it loads a single DER-encoded certificate. +// If `type` is `X509_FILETYPE_PEM`, it loads a sequence of PEM-encoded +// certificates. `type` may not be `X509_FILETYPE_DEFAULT`. OPENSSL_EXPORT int X509_load_cert_file(X509_LOOKUP *lookup, const char *file, int type); -// X509_load_crl_file loads CRLs from |file| and add them it to |lookup|'s -// |X509_STORE|. It returns one on success and zero on error. +// X509_load_crl_file loads CRLs from `file` and add them it to `lookup`'s +// `X509_STORE`. It returns one on success and zero on error. // -// If |type| is |X509_FILETYPE_ASN1|, it loads a single DER-encoded CRL. If -// |type| is |X509_FILETYPE_PEM|, it loads a sequence of PEM-encoded CRLs. -// |type| may not be |X509_FILETYPE_DEFAULT|. +// If `type` is `X509_FILETYPE_ASN1`, it loads a single DER-encoded CRL. If +// `type` is `X509_FILETYPE_PEM`, it loads a sequence of PEM-encoded CRLs. +// `type` may not be `X509_FILETYPE_DEFAULT`. OPENSSL_EXPORT int X509_load_crl_file(X509_LOOKUP *lookup, const char *file, int type); -// X509_load_cert_crl_file loads CRLs and trusted certificates from |file| and -// adds them to |lookup|'s |X509_STORE|. It returns one on success and zero on +// X509_load_cert_crl_file loads CRLs and trusted certificates from `file` and +// adds them to `lookup`'s `X509_STORE`. It returns one on success and zero on // error. // -// If |type| is |X509_FILETYPE_ASN1|, it loads a single DER-encoded certificate. -// This function cannot be used to load a DER-encoded CRL. If |type| is -// |X509_FILETYPE_PEM|, it loads a sequence of PEM-encoded certificates and -// CRLs. |type| may not be |X509_FILETYPE_DEFAULT|. +// If `type` is `X509_FILETYPE_ASN1`, it loads a single DER-encoded certificate. +// This function cannot be used to load a DER-encoded CRL. If `type` is +// `X509_FILETYPE_PEM`, it loads a sequence of PEM-encoded certificates and +// CRLs. `type` may not be `X509_FILETYPE_DEFAULT`. OPENSSL_EXPORT int X509_load_cert_crl_file(X509_LOOKUP *lookup, const char *file, int type); -// X509_NAME_hash returns a hash of |name|, or zero on error. This is the new -// hash used by |X509_LOOKUP_add_dir|. +// X509_NAME_hash returns a hash of `name`, or zero on error. This is the new +// hash used by `X509_LOOKUP_add_dir`. // -// This hash is specific to the |X509_LOOKUP_add_dir| filesystem format and is +// This hash is specific to the `X509_LOOKUP_add_dir` filesystem format and is // not suitable for general-purpose X.509 name processing. It is very short, so // there will be hash collisions. It also depends on an OpenSSL-specific // canonicalization process. OPENSSL_EXPORT uint32_t X509_NAME_hash(const X509_NAME *name); -// X509_NAME_hash_old returns a hash of |name|, or zero on error. This is the -// legacy hash used by |X509_LOOKUP_add_dir|, which is still supported for +// X509_NAME_hash_old returns a hash of `name`, or zero on error. This is the +// legacy hash used by `X509_LOOKUP_add_dir`, which is still supported for // compatibility. // -// This hash is specific to the |X509_LOOKUP_add_dir| filesystem format and is +// This hash is specific to the `X509_LOOKUP_add_dir` filesystem format and is // not suitable for general-purpose X.509 name processing. It is very short, so // there will be hash collisions. OPENSSL_EXPORT uint32_t X509_NAME_hash_old(const X509_NAME *name); -// X509_STORE_set_default_paths configures |store| to read from some "default" +// X509_STORE_set_default_paths configures `store` to read from some "default" // filesystem paths. It returns one on success and zero on error. The filesystem // paths are determined by a combination of hardcoded paths and the SSL_CERT_DIR // and SSL_CERT_FILE environment variables. @@ -3714,14 +3733,14 @@ OPENSSL_EXPORT const char *X509_get_default_cert_file_env(void); // // The SignedPublicKeyAndChallenge (SPKAC) is a legacy structure to request // certificates, primarily in the legacy HTML tag. An SPKAC structure -// is represented by a |NETSCAPE_SPKI| structure. +// is represented by a `NETSCAPE_SPKI` structure. // // The structure is described in // https://developer.mozilla.org/en-US/docs/Web/HTML/Element/keygen -// A Netscape_spki_st, or |NETSCAPE_SPKI|, represents a +// A Netscape_spki_st, or `NETSCAPE_SPKI`, represents a // SignedPublicKeyAndChallenge structure. Although this structure contains a -// |spkac| field of type |NETSCAPE_SPKAC|, these are misnamed. The SPKAC is the +// `spkac` field of type `NETSCAPE_SPKAC`, these are misnamed. The SPKAC is the // entire structure, not the signed portion. struct Netscape_spki_st { NETSCAPE_SPKAC *spkac; @@ -3729,81 +3748,81 @@ struct Netscape_spki_st { ASN1_BIT_STRING *signature; } /* NETSCAPE_SPKI */; -// NETSCAPE_SPKI_new returns a newly-allocated, empty |NETSCAPE_SPKI| object, or +// NETSCAPE_SPKI_new returns a newly-allocated, empty `NETSCAPE_SPKI` object, or // NULL on error. OPENSSL_EXPORT NETSCAPE_SPKI *NETSCAPE_SPKI_new(void); -// NETSCAPE_SPKI_free releases memory associated with |spki|. +// NETSCAPE_SPKI_free releases memory associated with `spki`. OPENSSL_EXPORT void NETSCAPE_SPKI_free(NETSCAPE_SPKI *spki); -// d2i_NETSCAPE_SPKI parses up to |len| bytes from |*inp| as a DER-encoded -// SignedPublicKeyAndChallenge structure, as described in |d2i_SAMPLE|. +// d2i_NETSCAPE_SPKI parses up to `len` bytes from `*inp` as a DER-encoded +// SignedPublicKeyAndChallenge structure, as described in `d2i_SAMPLE`. OPENSSL_EXPORT NETSCAPE_SPKI *d2i_NETSCAPE_SPKI(NETSCAPE_SPKI **out, const uint8_t **inp, long len); -// i2d_NETSCAPE_SPKI marshals |spki| as a DER-encoded -// SignedPublicKeyAndChallenge structure, as described in |i2d_SAMPLE|. +// i2d_NETSCAPE_SPKI marshals `spki` as a DER-encoded +// SignedPublicKeyAndChallenge structure, as described in `i2d_SAMPLE`. OPENSSL_EXPORT int i2d_NETSCAPE_SPKI(const NETSCAPE_SPKI *spki, uint8_t **outp); -// NETSCAPE_SPKI_verify checks that |spki| has a valid signature by |pkey|. It +// NETSCAPE_SPKI_verify checks that `spki` has a valid signature by `pkey`. It // returns one if the signature is valid and zero otherwise. -OPENSSL_EXPORT int NETSCAPE_SPKI_verify(NETSCAPE_SPKI *spki, EVP_PKEY *pkey); +OPENSSL_EXPORT int NETSCAPE_SPKI_verify(const NETSCAPE_SPKI *spki, + const EVP_PKEY *pkey); -// NETSCAPE_SPKI_b64_decode decodes |len| bytes from |str| as a base64-encoded +// NETSCAPE_SPKI_b64_decode decodes `len` bytes from `str` as a base64-encoded // SignedPublicKeyAndChallenge structure. It returns a newly-allocated -// |NETSCAPE_SPKI| structure with the result, or NULL on error. If |len| is 0 or -// negative, the length is calculated with |strlen| and |str| must be a +// `NETSCAPE_SPKI` structure with the result, or NULL on error. If `len` is 0 or +// negative, the length is calculated with `strlen` and `str` must be a // NUL-terminated C string. OPENSSL_EXPORT NETSCAPE_SPKI *NETSCAPE_SPKI_b64_decode(const char *str, ossl_ssize_t len); -// NETSCAPE_SPKI_b64_encode encodes |spki| as a base64-encoded +// NETSCAPE_SPKI_b64_encode encodes `spki` as a base64-encoded // SignedPublicKeyAndChallenge structure. It returns a newly-allocated // NUL-terminated C string with the result, or NULL on error. The caller must -// release the memory with |OPENSSL_free| when done. -OPENSSL_EXPORT char *NETSCAPE_SPKI_b64_encode(NETSCAPE_SPKI *spki); +// release the memory with `OPENSSL_free` when done. +OPENSSL_EXPORT char *NETSCAPE_SPKI_b64_encode(const NETSCAPE_SPKI *spki); -// NETSCAPE_SPKI_get_pubkey decodes and returns the public key in |spki| as an -// |EVP_PKEY|, or NULL on error. The caller takes ownership of the resulting -// pointer and must call |EVP_PKEY_free| when done. +// NETSCAPE_SPKI_get_pubkey decodes and returns the public key in `spki` as an +// `EVP_PKEY`, or NULL on error. The caller takes ownership of the resulting +// pointer and must call `EVP_PKEY_free` when done. OPENSSL_EXPORT EVP_PKEY *NETSCAPE_SPKI_get_pubkey(const NETSCAPE_SPKI *spki); -// NETSCAPE_SPKI_set_pubkey sets |spki|'s public key to |pkey|. It returns one -// on success or zero on error. This function does not take ownership of |pkey|, -// so the caller may continue to manage its lifetime independently of |spki|. +// NETSCAPE_SPKI_set_pubkey sets `spki`'s public key to `pkey`. It returns one +// on success or zero on error. OPENSSL_EXPORT int NETSCAPE_SPKI_set_pubkey(NETSCAPE_SPKI *spki, - EVP_PKEY *pkey); + const EVP_PKEY *pkey); -// NETSCAPE_SPKI_sign signs |spki| with |pkey| and replaces the signature +// NETSCAPE_SPKI_sign signs `spki` with `pkey` and replaces the signature // algorithm and signature fields. It returns the length of the signature on -// success and zero on error. This function uses digest algorithm |md|, or -// |pkey|'s default if NULL. Other signing parameters use |pkey|'s defaults. -OPENSSL_EXPORT int NETSCAPE_SPKI_sign(NETSCAPE_SPKI *spki, EVP_PKEY *pkey, +// success and zero on error. This function uses digest algorithm `md`, or +// `pkey`'s default if NULL. Other signing parameters use `pkey`'s defaults. +OPENSSL_EXPORT int NETSCAPE_SPKI_sign(NETSCAPE_SPKI *spki, const EVP_PKEY *pkey, const EVP_MD *md); -// A Netscape_spkac_st, or |NETSCAPE_SPKAC|, represents a PublicKeyAndChallenge +// A Netscape_spkac_st, or `NETSCAPE_SPKAC`, represents a PublicKeyAndChallenge // structure. This type is misnamed. The full SPKAC includes the signature, -// which is represented with the |NETSCAPE_SPKI| type. +// which is represented with the `NETSCAPE_SPKI` type. struct Netscape_spkac_st { X509_PUBKEY *pubkey; ASN1_IA5STRING *challenge; } /* NETSCAPE_SPKAC */; -// NETSCAPE_SPKAC_new returns a newly-allocated, empty |NETSCAPE_SPKAC| object, +// NETSCAPE_SPKAC_new returns a newly-allocated, empty `NETSCAPE_SPKAC` object, // or NULL on error. OPENSSL_EXPORT NETSCAPE_SPKAC *NETSCAPE_SPKAC_new(void); -// NETSCAPE_SPKAC_free releases memory associated with |spkac|. +// NETSCAPE_SPKAC_free releases memory associated with `spkac`. OPENSSL_EXPORT void NETSCAPE_SPKAC_free(NETSCAPE_SPKAC *spkac); -// d2i_NETSCAPE_SPKAC parses up to |len| bytes from |*inp| as a DER-encoded -// PublicKeyAndChallenge structure, as described in |d2i_SAMPLE|. +// d2i_NETSCAPE_SPKAC parses up to `len` bytes from `*inp` as a DER-encoded +// PublicKeyAndChallenge structure, as described in `d2i_SAMPLE`. OPENSSL_EXPORT NETSCAPE_SPKAC *d2i_NETSCAPE_SPKAC(NETSCAPE_SPKAC **out, const uint8_t **inp, long len); -// i2d_NETSCAPE_SPKAC marshals |spkac| as a DER-encoded PublicKeyAndChallenge -// structure, as described in |i2d_SAMPLE|. +// i2d_NETSCAPE_SPKAC marshals `spkac` as a DER-encoded PublicKeyAndChallenge +// structure, as described in `i2d_SAMPLE`. OPENSSL_EXPORT int i2d_NETSCAPE_SPKAC(const NETSCAPE_SPKAC *spkac, uint8_t **outp); @@ -3816,43 +3835,43 @@ OPENSSL_EXPORT int i2d_NETSCAPE_SPKAC(const NETSCAPE_SPKAC *spkac, // discouraged. The parameters structure is very complex, and it takes more // bytes to merely encode parameters than an entire P-256 ECDSA signature. -// An rsa_pss_params_st, aka |RSA_PSS_PARAMS|, represents a parsed +// An rsa_pss_params_st, aka `RSA_PSS_PARAMS`, represents a parsed // RSASSA-PSS-params structure, as defined in (RFC 4055). struct rsa_pss_params_st { X509_ALGOR *hashAlgorithm; X509_ALGOR *maskGenAlgorithm; ASN1_INTEGER *saltLength; ASN1_INTEGER *trailerField; - // OpenSSL caches the MGF hash on |RSA_PSS_PARAMS| in some cases. None of the + // OpenSSL caches the MGF hash on `RSA_PSS_PARAMS` in some cases. None of the // cases apply to BoringSSL, so this is always NULL, but Node expects the // field to be present. X509_ALGOR *maskHash; } /* RSA_PSS_PARAMS */; -// RSA_PSS_PARAMS is an |ASN1_ITEM| whose ASN.1 type is RSASSA-PSS-params (RFC -// 4055) and C type is |RSA_PSS_PARAMS*|. +// RSA_PSS_PARAMS is an `ASN1_ITEM` whose ASN.1 type is RSASSA-PSS-params (RFC +// 4055) and C type is `RSA_PSS_PARAMS*`. DECLARE_ASN1_ITEM(RSA_PSS_PARAMS) -// RSA_PSS_PARAMS_new returns a new, empty |RSA_PSS_PARAMS|, or NULL on error. +// RSA_PSS_PARAMS_new returns a new, empty `RSA_PSS_PARAMS`, or NULL on error. OPENSSL_EXPORT RSA_PSS_PARAMS *RSA_PSS_PARAMS_new(void); -// RSA_PSS_PARAMS_free releases memory associated with |params|. +// RSA_PSS_PARAMS_free releases memory associated with `params`. OPENSSL_EXPORT void RSA_PSS_PARAMS_free(RSA_PSS_PARAMS *params); -// d2i_RSA_PSS_PARAMS parses up to |len| bytes from |*inp| as a DER-encoded -// RSASSA-PSS-params (RFC 4055), as described in |d2i_SAMPLE|. +// d2i_RSA_PSS_PARAMS parses up to `len` bytes from `*inp` as a DER-encoded +// RSASSA-PSS-params (RFC 4055), as described in `d2i_SAMPLE`. OPENSSL_EXPORT RSA_PSS_PARAMS *d2i_RSA_PSS_PARAMS(RSA_PSS_PARAMS **out, const uint8_t **inp, long len); -// i2d_RSA_PSS_PARAMS marshals |in| as a DER-encoded RSASSA-PSS-params (RFC -// 4055), as described in |i2d_SAMPLE|. +// i2d_RSA_PSS_PARAMS marshals `in` as a DER-encoded RSASSA-PSS-params (RFC +// 4055), as described in `i2d_SAMPLE`. OPENSSL_EXPORT int i2d_RSA_PSS_PARAMS(const RSA_PSS_PARAMS *in, uint8_t **outp); // PKCS#8 private keys. // -// The |PKCS8_PRIV_KEY_INFO| type represents a PKCS#8 PrivateKeyInfo (RFC 5208) +// The `PKCS8_PRIV_KEY_INFO` type represents a PKCS#8 PrivateKeyInfo (RFC 5208) // structure. This is analogous to SubjectPublicKeyInfo and uses the same // AlgorithmIdentifiers, but carries private keys and is not part of X.509 // itself. @@ -3860,68 +3879,68 @@ OPENSSL_EXPORT int i2d_RSA_PSS_PARAMS(const RSA_PSS_PARAMS *in, uint8_t **outp); // TODO(davidben): Do these functions really belong in this header? // PKCS8_PRIV_KEY_INFO_new returns a newly-allocated, empty -// |PKCS8_PRIV_KEY_INFO| object, or NULL on error. +// `PKCS8_PRIV_KEY_INFO` object, or NULL on error. OPENSSL_EXPORT PKCS8_PRIV_KEY_INFO *PKCS8_PRIV_KEY_INFO_new(void); -// PKCS8_PRIV_KEY_INFO_free releases memory associated with |key|. +// PKCS8_PRIV_KEY_INFO_free releases memory associated with `key`. OPENSSL_EXPORT void PKCS8_PRIV_KEY_INFO_free(PKCS8_PRIV_KEY_INFO *key); -// d2i_PKCS8_PRIV_KEY_INFO parses up to |len| bytes from |*inp| as a DER-encoded -// PrivateKeyInfo, as described in |d2i_SAMPLE|. +// d2i_PKCS8_PRIV_KEY_INFO parses up to `len` bytes from `*inp` as a DER-encoded +// PrivateKeyInfo, as described in `d2i_SAMPLE`. OPENSSL_EXPORT PKCS8_PRIV_KEY_INFO *d2i_PKCS8_PRIV_KEY_INFO( PKCS8_PRIV_KEY_INFO **out, const uint8_t **inp, long len); -// i2d_PKCS8_PRIV_KEY_INFO marshals |key| as a DER-encoded PrivateKeyInfo, as -// described in |i2d_SAMPLE|. +// i2d_PKCS8_PRIV_KEY_INFO marshals `key` as a DER-encoded PrivateKeyInfo, as +// described in `i2d_SAMPLE`. OPENSSL_EXPORT int i2d_PKCS8_PRIV_KEY_INFO(const PKCS8_PRIV_KEY_INFO *key, uint8_t **outp); -// EVP_PKCS82PKEY returns |p8| as a newly-allocated |EVP_PKEY|, or NULL if the +// EVP_PKCS82PKEY returns `p8` as a newly-allocated `EVP_PKEY`, or NULL if the // key was unsupported or could not be decoded. The caller must release the -// result with |EVP_PKEY_free| when done. +// result with `EVP_PKEY_free` when done. // -// Use |EVP_parse_private_key| instead. +// Use `EVP_parse_private_key` instead. OPENSSL_EXPORT EVP_PKEY *EVP_PKCS82PKEY(const PKCS8_PRIV_KEY_INFO *p8); -// EVP_PKEY2PKCS8 encodes |pkey| as a PKCS#8 PrivateKeyInfo (RFC 5208), -// represented as a newly-allocated |PKCS8_PRIV_KEY_INFO|, or NULL on error. The -// caller must release the result with |PKCS8_PRIV_KEY_INFO_free| when done. +// EVP_PKEY2PKCS8 encodes `pkey` as a PKCS#8 PrivateKeyInfo (RFC 5208), +// represented as a newly-allocated `PKCS8_PRIV_KEY_INFO`, or NULL on error. The +// caller must release the result with `PKCS8_PRIV_KEY_INFO_free` when done. // -// Use |EVP_marshal_private_key| instead. +// Use `EVP_marshal_private_key` instead. OPENSSL_EXPORT PKCS8_PRIV_KEY_INFO *EVP_PKEY2PKCS8(const EVP_PKEY *pkey); // Algorithm and octet string pairs. // -// The |X509_SIG| type represents an ASN.1 SEQUENCE type of an -// AlgorithmIdentifier and an OCTET STRING. Although named |X509_SIG|, there is +// The `X509_SIG` type represents an ASN.1 SEQUENCE type of an +// AlgorithmIdentifier and an OCTET STRING. Although named `X509_SIG`, there is // no type in X.509 which matches this format. The two common types which do are // DigestInfo (RFC 2315 and RFC 8017), and EncryptedPrivateKeyInfo (RFC 5208). -// X509_SIG_new returns a newly-allocated, empty |X509_SIG| object, or NULL on +// X509_SIG_new returns a newly-allocated, empty `X509_SIG` object, or NULL on // error. OPENSSL_EXPORT X509_SIG *X509_SIG_new(void); -// X509_SIG_free releases memory associated with |key|. +// X509_SIG_free releases memory associated with `key`. OPENSSL_EXPORT void X509_SIG_free(X509_SIG *key); -// d2i_X509_SIG parses up to |len| bytes from |*inp| as a DER-encoded algorithm -// and octet string pair, as described in |d2i_SAMPLE|. +// d2i_X509_SIG parses up to `len` bytes from `*inp` as a DER-encoded algorithm +// and octet string pair, as described in `d2i_SAMPLE`. OPENSSL_EXPORT X509_SIG *d2i_X509_SIG(X509_SIG **out, const uint8_t **inp, long len); -// i2d_X509_SIG marshals |sig| as a DER-encoded algorithm -// and octet string pair, as described in |i2d_SAMPLE|. +// i2d_X509_SIG marshals `sig` as a DER-encoded algorithm +// and octet string pair, as described in `i2d_SAMPLE`. OPENSSL_EXPORT int i2d_X509_SIG(const X509_SIG *sig, uint8_t **outp); -// X509_SIG_get0 sets |*out_alg| and |*out_digest| to non-owning pointers to -// |sig|'s algorithm and digest fields, respectively. Either |out_alg| and -// |out_digest| may be NULL to skip those fields. +// X509_SIG_get0 sets `*out_alg` and `*out_digest` to non-owning pointers to +// `sig`'s algorithm and digest fields, respectively. Either `out_alg` and +// `out_digest` may be NULL to skip those fields. OPENSSL_EXPORT void X509_SIG_get0(const X509_SIG *sig, const X509_ALGOR **out_alg, const ASN1_OCTET_STRING **out_digest); -// X509_SIG_getm behaves like |X509_SIG_get0| but returns mutable pointers. +// X509_SIG_getm behaves like `X509_SIG_get0` but returns mutable pointers. OPENSSL_EXPORT void X509_SIG_getm(X509_SIG *sig, X509_ALGOR **out_alg, ASN1_OCTET_STRING **out_digest); @@ -3933,8 +3952,8 @@ OPENSSL_EXPORT void X509_SIG_getm(X509_SIG *sig, X509_ALGOR **out_alg, // and not parsed programmatically. In many cases, the outputs are ambiguous, so // attempting to parse them can lead to string injection vulnerabilities. -// The following flags control |X509_print_ex| and |X509_REQ_print_ex|. These -// flags co-exist with |X509V3_EXT_*|, so avoid collisions when adding new ones. +// The following flags control `X509_print_ex` and `X509_REQ_print_ex`. These +// flags co-exist with `X509V3_EXT_*`, so avoid collisions when adding new ones. // X509_FLAG_COMPAT disables all flags. It additionally causes names to be // printed with a 16-byte indent. @@ -3947,18 +3966,18 @@ OPENSSL_EXPORT void X509_SIG_getm(X509_SIG *sig, X509_ALGOR **out_alg, #define X509_FLAG_NO_VERSION (1L << 1) // X509_FLAG_NO_SERIAL skips printing the serial number. It is ignored in -// |X509_REQ_print_fp|. +// `X509_REQ_print_fp`. #define X509_FLAG_NO_SERIAL (1L << 2) // X509_FLAG_NO_SIGNAME skips printing the signature algorithm in the -// TBSCertificate. It is ignored in |X509_REQ_print_fp|. +// TBSCertificate. It is ignored in `X509_REQ_print_fp`. #define X509_FLAG_NO_SIGNAME (1L << 3) // X509_FLAG_NO_ISSUER skips printing the issuer. #define X509_FLAG_NO_ISSUER (1L << 4) // X509_FLAG_NO_VALIDITY skips printing the notBefore and notAfter times. It is -// ignored in |X509_REQ_print_fp|. +// ignored in `X509_REQ_print_fp`. #define X509_FLAG_NO_VALIDITY (1L << 5) // X509_FLAG_NO_SUBJECT skips printing the subject. @@ -3968,15 +3987,15 @@ OPENSSL_EXPORT void X509_SIG_getm(X509_SIG *sig, X509_ALGOR **out_alg, #define X509_FLAG_NO_PUBKEY (1L << 7) // X509_FLAG_NO_EXTENSIONS skips printing the extension list. It is ignored in -// |X509_REQ_print_fp|. CSRs instead have attributes, which is controlled by -// |X509_FLAG_NO_ATTRIBUTES|. +// `X509_REQ_print_fp`. CSRs instead have attributes, which is controlled by +// `X509_FLAG_NO_ATTRIBUTES`. #define X509_FLAG_NO_EXTENSIONS (1L << 8) // X509_FLAG_NO_SIGDUMP skips printing the signature and outer signature // algorithm. #define X509_FLAG_NO_SIGDUMP (1L << 9) -// X509_FLAG_NO_AUX skips printing auxiliary properties. (See |d2i_X509_AUX| and +// X509_FLAG_NO_AUX skips printing auxiliary properties. (See `d2i_X509_AUX` and // related functions.) #define X509_FLAG_NO_AUX (1L << 10) @@ -3985,12 +4004,12 @@ OPENSSL_EXPORT void X509_SIG_getm(X509_SIG *sig, X509_ALGOR **out_alg, #define X509_FLAG_NO_ATTRIBUTES (1L << 11) // X509_FLAG_NO_IDS skips printing the issuerUniqueID and subjectUniqueID in a -// certificate. It is ignored in |X509_REQ_print_fp|. +// certificate. It is ignored in `X509_REQ_print_fp`. #define X509_FLAG_NO_IDS (1L << 12) -// The following flags control |X509_print_ex|, |X509_REQ_print_ex|, -// |X509V3_EXT_print|, and |X509V3_extensions_print|. These flags coexist with -// |X509_FLAG_*|, so avoid collisions when adding new ones. +// The following flags control `X509_print_ex`, `X509_REQ_print_ex`, +// `X509V3_EXT_print`, and `X509V3_extensions_print`. These flags coexist with +// `X509_FLAG_*`, so avoid collisions when adding new ones. // X509V3_EXT_UNKNOWN_MASK is a mask that determines how unknown extensions are // processed. @@ -4005,59 +4024,59 @@ OPENSSL_EXPORT void X509_SIG_getm(X509_SIG *sig, X509_ALGOR **out_alg, #define X509V3_EXT_ERROR_UNKNOWN (1L << 16) // X509V3_EXT_PARSE_UNKNOWN is deprecated and behaves like -// |X509V3_EXT_DUMP_UNKNOWN|. +// `X509V3_EXT_DUMP_UNKNOWN`. #define X509V3_EXT_PARSE_UNKNOWN (2L << 16) // X509V3_EXT_DUMP_UNKNOWN causes unknown extensions to be displayed as a // hexdump. #define X509V3_EXT_DUMP_UNKNOWN (3L << 16) -// X509_print_ex writes a human-readable representation of |x| to |bp|. It -// returns one on success and zero on error. |nmflags| is the flags parameter -// for |X509_NAME_print_ex| when printing the subject and issuer. |cflag| should -// be some combination of the |X509_FLAG_*| and |X509V3_EXT_*| constants. +// X509_print_ex writes a human-readable representation of `x` to `bp`. It +// returns one on success and zero on error. `nmflags` is the flags parameter +// for `X509_NAME_print_ex` when printing the subject and issuer. `cflag` should +// be some combination of the `X509_FLAG_*` and `X509V3_EXT_*` constants. OPENSSL_EXPORT int X509_print_ex(BIO *bp, const X509 *x, unsigned long nmflag, unsigned long cflag); -// X509_print_ex_fp behaves like |X509_print_ex| but writes to |fp|. +// X509_print_ex_fp behaves like `X509_print_ex` but writes to `fp`. OPENSSL_EXPORT int X509_print_ex_fp(FILE *fp, const X509 *x, unsigned long nmflag, unsigned long cflag); -// X509_print calls |X509_print_ex| with |XN_FLAG_COMPAT| and |X509_FLAG_COMPAT| +// X509_print calls `X509_print_ex` with `XN_FLAG_COMPAT` and `X509_FLAG_COMPAT` // flags. OPENSSL_EXPORT int X509_print(BIO *bp, const X509 *x); -// X509_print_fp behaves like |X509_print| but writes to |fp|. +// X509_print_fp behaves like `X509_print` but writes to `fp`. OPENSSL_EXPORT int X509_print_fp(FILE *fp, const X509 *x); -// X509_CRL_print writes a human-readable representation of |x| to |bp|. It +// X509_CRL_print writes a human-readable representation of `x` to `bp`. It // returns one on success and zero on error. OPENSSL_EXPORT int X509_CRL_print(BIO *bp, const X509_CRL *x); -// X509_CRL_print_fp behaves like |X509_CRL_print| but writes to |fp|. +// X509_CRL_print_fp behaves like `X509_CRL_print` but writes to `fp`. OPENSSL_EXPORT int X509_CRL_print_fp(FILE *fp, const X509_CRL *x); -// X509_REQ_print_ex writes a human-readable representation of |x| to |bp|. It -// returns one on success and zero on error. |nmflags| is the flags parameter -// for |X509_NAME_print_ex|, when printing the subject. |cflag| should be some -// combination of the |X509_FLAG_*| and |X509V3_EXT_*| constants. +// X509_REQ_print_ex writes a human-readable representation of `x` to `bp`. It +// returns one on success and zero on error. `nmflags` is the flags parameter +// for `X509_NAME_print_ex`, when printing the subject. `cflag` should be some +// combination of the `X509_FLAG_*` and `X509V3_EXT_*` constants. OPENSSL_EXPORT int X509_REQ_print_ex(BIO *bp, const X509_REQ *x, unsigned long nmflag, unsigned long cflag); -// X509_REQ_print calls |X509_REQ_print_ex| with |XN_FLAG_COMPAT| and -// |X509_FLAG_COMPAT| flags. +// X509_REQ_print calls `X509_REQ_print_ex` with `XN_FLAG_COMPAT` and +// `X509_FLAG_COMPAT` flags. OPENSSL_EXPORT int X509_REQ_print(BIO *bp, const X509_REQ *req); -// X509_REQ_print_fp behaves like |X509_REQ_print| but writes to |fp|. +// X509_REQ_print_fp behaves like `X509_REQ_print` but writes to `fp`. OPENSSL_EXPORT int X509_REQ_print_fp(FILE *fp, const X509_REQ *req); -// The following flags are control |X509_NAME_print_ex|. They must not collide -// with |ASN1_STRFLGS_*|. +// The following flags are control `X509_NAME_print_ex`. They must not collide +// with `ASN1_STRFLGS_*`. // // TODO(davidben): This is far, far too many options and most of them are // useless. Trim this down. -// XN_FLAG_COMPAT prints with |X509_NAME_print|'s format and return value +// XN_FLAG_COMPAT prints with `X509_NAME_print`'s format and return value // convention. #define XN_FLAG_COMPAT 0ul @@ -4068,7 +4087,7 @@ OPENSSL_EXPORT int X509_REQ_print_fp(FILE *fp, const X509_REQ *req); // with "+", as in RFC 2253. #define XN_FLAG_SEP_COMMA_PLUS (1ul << 16) -// XN_FLAG_SEP_CPLUS_SPC behaves like |XN_FLAG_SEP_COMMA_PLUS| but adds spaces +// XN_FLAG_SEP_CPLUS_SPC behaves like `XN_FLAG_SEP_COMMA_PLUS` but adds spaces // between the separators. #define XN_FLAG_SEP_CPLUS_SPC (2ul << 16) @@ -4106,62 +4125,62 @@ OPENSSL_EXPORT int X509_REQ_print_fp(FILE *fp, const X509_REQ *req); (ASN1_STRFLGS_RFC2253 | ASN1_STRFLGS_ESC_QUOTE | XN_FLAG_SEP_CPLUS_SPC | \ XN_FLAG_SPC_EQ | XN_FLAG_FN_SN) -// X509_NAME_print_ex writes a human-readable representation of |nm| to |out|. -// Each line of output is indented by |indent| spaces. It returns the number of -// bytes written on success, and -1 on error. If |out| is NULL, it returns the -// number of bytes it would have written but does not write anything. |flags| -// should be some combination of |XN_FLAG_*| and |ASN1_STRFLGS_*| values and -// determines the output. If unsure, use |XN_FLAG_RFC2253|. +// X509_NAME_print_ex writes a human-readable representation of `nm` to `out`. +// Each line of output is indented by `indent` spaces. It returns the number of +// bytes written on success, and -1 on error. If `out` is NULL, it returns the +// number of bytes it would have written but does not write anything. `flags` +// should be some combination of `XN_FLAG_*` and `ASN1_STRFLGS_*` values and +// determines the output. If unsure, use `XN_FLAG_RFC2253`. // -// If |flags| is |XN_FLAG_COMPAT|, or zero, this function calls -// |X509_NAME_print| instead. In that case, it returns one on success, rather +// If `flags` is `XN_FLAG_COMPAT`, or zero, this function calls +// `X509_NAME_print` instead. In that case, it returns one on success, rather // than the output length. OPENSSL_EXPORT int X509_NAME_print_ex(BIO *out, const X509_NAME *nm, int indent, unsigned long flags); -// X509_NAME_print prints a human-readable representation of |name| to |bp|. It -// returns one on success and zero on error. |obase| is ignored. +// X509_NAME_print prints a human-readable representation of `name` to `bp`. It +// returns one on success and zero on error. `obase` is ignored. // // This function outputs a legacy format that does not correctly handle string -// encodings and other cases. Prefer |X509_NAME_print_ex| if printing a name for +// encodings and other cases. Prefer `X509_NAME_print_ex` if printing a name for // debugging purposes. OPENSSL_EXPORT int X509_NAME_print(BIO *bp, const X509_NAME *name, int obase); -// X509_NAME_oneline writes a human-readable representation to |name| to a +// X509_NAME_oneline writes a human-readable representation to `name` to a // buffer as a NUL-terminated C string. // -// If |buf| is NULL, returns a newly-allocated buffer containing the result on -// success, or NULL on error. The buffer must be released with |OPENSSL_free| +// If `buf` is NULL, returns a newly-allocated buffer containing the result on +// success, or NULL on error. The buffer must be released with `OPENSSL_free` // when done. // -// If |buf| is non-NULL, at most |size| bytes of output are written to |buf| -// instead. |size| includes the trailing NUL. The function then returns |buf| on -// success or NULL on error. If the output does not fit in |size| bytes, the +// If `buf` is non-NULL, at most `size` bytes of output are written to `buf` +// instead. `size` includes the trailing NUL. The function then returns `buf` on +// success or NULL on error. If the output does not fit in `size` bytes, the // output is silently truncated at an attribute boundary. // // This function outputs a legacy format that does not correctly handle string -// encodings and other cases. Prefer |X509_NAME_print_ex| if printing a name for +// encodings and other cases. Prefer `X509_NAME_print_ex` if printing a name for // debugging purposes. OPENSSL_EXPORT char *X509_NAME_oneline(const X509_NAME *name, char *buf, int size); -// X509_NAME_print_ex_fp behaves like |X509_NAME_print_ex| but writes to |fp|. +// X509_NAME_print_ex_fp behaves like `X509_NAME_print_ex` but writes to `fp`. OPENSSL_EXPORT int X509_NAME_print_ex_fp(FILE *fp, const X509_NAME *nm, int indent, unsigned long flags); -// X509_signature_dump writes a human-readable representation of |sig| to |bio|, -// indented with |indent| spaces. It returns one on success and zero on error. +// X509_signature_dump writes a human-readable representation of `sig` to `bio`, +// indented with `indent` spaces. It returns one on success and zero on error. OPENSSL_EXPORT int X509_signature_dump(BIO *bio, const ASN1_STRING *sig, int indent); -// X509_signature_print writes a human-readable representation of |alg| and -// |sig| to |bio|. It returns one on success and zero on error. +// X509_signature_print writes a human-readable representation of `alg` and +// `sig` to `bio`. It returns one on success and zero on error. OPENSSL_EXPORT int X509_signature_print(BIO *bio, const X509_ALGOR *alg, const ASN1_STRING *sig); -// X509V3_EXT_print prints a human-readable representation of |ext| to out. It -// returns one on success and zero on error. The output is indented by |indent| -// spaces. |flag| is one of the |X509V3_EXT_*| constants and controls printing +// X509V3_EXT_print prints a human-readable representation of `ext` to out. It +// returns one on success and zero on error. The output is indented by `indent` +// spaces. `flag` is one of the `X509V3_EXT_*` constants and controls printing // of unknown extensions and syntax errors. // // WARNING: Although some applications programmatically parse the output of this @@ -4172,21 +4191,21 @@ OPENSSL_EXPORT int X509_signature_print(BIO *bio, const X509_ALGOR *alg, OPENSSL_EXPORT int X509V3_EXT_print(BIO *out, const X509_EXTENSION *ext, unsigned long flag, int indent); -// X509V3_EXT_print_fp behaves like |X509V3_EXT_print| but writes to a |FILE| -// instead of a |BIO|. +// X509V3_EXT_print_fp behaves like `X509V3_EXT_print` but writes to a `FILE` +// instead of a `BIO`. OPENSSL_EXPORT int X509V3_EXT_print_fp(FILE *out, const X509_EXTENSION *ext, int flag, int indent); -// X509V3_extensions_print prints |title|, followed by a human-readable -// representation of |exts| to |out|. It returns one on success and zero on -// error. The output is indented by |indent| spaces. |flag| is one of the -// |X509V3_EXT_*| constants and controls printing of unknown extensions and +// X509V3_extensions_print prints `title`, followed by a human-readable +// representation of `exts` to `out`. It returns one on success and zero on +// error. The output is indented by `indent` spaces. `flag` is one of the +// `X509V3_EXT_*` constants and controls printing of unknown extensions and // syntax errors. OPENSSL_EXPORT int X509V3_extensions_print(BIO *out, const char *title, const STACK_OF(X509_EXTENSION) *exts, unsigned long flag, int indent); -// GENERAL_NAME_print prints a human-readable representation of |gen| to |out|. +// GENERAL_NAME_print prints a human-readable representation of `gen` to `out`. // It returns one on success and zero on error. // // TODO(davidben): Actually, it just returns one and doesn't check for I/O or @@ -4196,10 +4215,10 @@ OPENSSL_EXPORT int GENERAL_NAME_print(BIO *out, const GENERAL_NAME *gen); // Convenience functions. -// X509_pubkey_digest hashes the contents of the BIT STRING in |x509|'s -// subjectPublicKeyInfo field with |md| and writes the result to |out|. -// |EVP_MD_CTX_size| bytes are written, which is at most |EVP_MAX_MD_SIZE|. If -// |out_len| is not NULL, |*out_len| is set to the number of bytes written. This +// X509_pubkey_digest hashes the contents of the BIT STRING in `x509`'s +// subjectPublicKeyInfo field with `md` and writes the result to `out`. +// `EVP_MD_CTX_size` bytes are written, which is at most `EVP_MAX_MD_SIZE`. If +// `out_len` is not NULL, `*out_len` is set to the number of bytes written. This // function returns one on success and zero on error. // // This hash omits the BIT STRING tag, length, and number of unused bits. It @@ -4209,42 +4228,42 @@ OPENSSL_EXPORT int GENERAL_NAME_print(BIO *out, const GENERAL_NAME *gen); OPENSSL_EXPORT int X509_pubkey_digest(const X509 *x509, const EVP_MD *md, uint8_t *out, unsigned *out_len); -// X509_digest hashes |x509|'s DER encoding with |md| and writes the result to -// |out|. |EVP_MD_CTX_size| bytes are written, which is at most -// |EVP_MAX_MD_SIZE|. If |out_len| is not NULL, |*out_len| is set to the number +// X509_digest hashes `x509`'s DER encoding with `md` and writes the result to +// `out`. `EVP_MD_CTX_size` bytes are written, which is at most +// `EVP_MAX_MD_SIZE`. If `out_len` is not NULL, `*out_len` is set to the number // of bytes written. This function returns one on success and zero on error. // Note this digest covers the entire certificate, not just the signed portion. OPENSSL_EXPORT int X509_digest(const X509 *x509, const EVP_MD *md, uint8_t *out, unsigned *out_len); -// X509_CRL_digest hashes |crl|'s DER encoding with |md| and writes the result -// to |out|. |EVP_MD_CTX_size| bytes are written, which is at most -// |EVP_MAX_MD_SIZE|. If |out_len| is not NULL, |*out_len| is set to the number +// X509_CRL_digest hashes `crl`'s DER encoding with `md` and writes the result +// to `out`. `EVP_MD_CTX_size` bytes are written, which is at most +// `EVP_MAX_MD_SIZE`. If `out_len` is not NULL, `*out_len` is set to the number // of bytes written. This function returns one on success and zero on error. // Note this digest covers the entire CRL, not just the signed portion. OPENSSL_EXPORT int X509_CRL_digest(const X509_CRL *crl, const EVP_MD *md, uint8_t *out, unsigned *out_len); -// X509_REQ_digest hashes |req|'s DER encoding with |md| and writes the result -// to |out|. |EVP_MD_CTX_size| bytes are written, which is at most -// |EVP_MAX_MD_SIZE|. If |out_len| is not NULL, |*out_len| is set to the number +// X509_REQ_digest hashes `req`'s DER encoding with `md` and writes the result +// to `out`. `EVP_MD_CTX_size` bytes are written, which is at most +// `EVP_MAX_MD_SIZE`. If `out_len` is not NULL, `*out_len` is set to the number // of bytes written. This function returns one on success and zero on error. // Note this digest covers the entire certificate request, not just the signed // portion. OPENSSL_EXPORT int X509_REQ_digest(const X509_REQ *req, const EVP_MD *md, uint8_t *out, unsigned *out_len); -// X509_NAME_digest hashes |name|'s DER encoding with |md| and writes the result -// to |out|. |EVP_MD_CTX_size| bytes are written, which is at most -// |EVP_MAX_MD_SIZE|. If |out_len| is not NULL, |*out_len| is set to the number +// X509_NAME_digest hashes `name`'s DER encoding with `md` and writes the result +// to `out`. `EVP_MD_CTX_size` bytes are written, which is at most +// `EVP_MAX_MD_SIZE`. If `out_len` is not NULL, `*out_len` is set to the number // of bytes written. This function returns one on success and zero on error. OPENSSL_EXPORT int X509_NAME_digest(const X509_NAME *name, const EVP_MD *md, uint8_t *out, unsigned *out_len); -// The following functions behave like the corresponding unsuffixed |d2i_*| -// functions, but read the result from |bp| instead. Callers using these -// functions with memory |BIO|s to parse structures already in memory should use -// |d2i_*| instead. +// The following functions behave like the corresponding unsuffixed `d2i_*` +// functions, but read the result from `bp` instead. Callers using these +// functions with memory `BIO`s to parse structures already in memory should use +// `d2i_*` instead. OPENSSL_EXPORT X509 *d2i_X509_bio(BIO *bp, X509 **x509); OPENSSL_EXPORT X509_CRL *d2i_X509_CRL_bio(BIO *bp, X509_CRL **crl); OPENSSL_EXPORT X509_REQ *d2i_X509_REQ_bio(BIO *bp, X509_REQ **req); @@ -4261,14 +4280,14 @@ OPENSSL_EXPORT PKCS8_PRIV_KEY_INFO *d2i_PKCS8_PRIV_KEY_INFO_bio( OPENSSL_EXPORT EVP_PKEY *d2i_PUBKEY_bio(BIO *bp, EVP_PKEY **a); OPENSSL_EXPORT DH *d2i_DHparams_bio(BIO *bp, DH **dh); -// d2i_PrivateKey_bio behaves like |d2i_AutoPrivateKey|, but reads from |bp| +// d2i_PrivateKey_bio behaves like `d2i_AutoPrivateKey`, but reads from `bp` // instead. OPENSSL_EXPORT EVP_PKEY *d2i_PrivateKey_bio(BIO *bp, EVP_PKEY **a); -// The following functions behave like the corresponding unsuffixed |i2d_*| -// functions, but write the result to |bp|. They return one on success and zero -// on error. Callers using them with memory |BIO|s to encode structures to -// memory should use |i2d_*| directly instead. +// The following functions behave like the corresponding unsuffixed `i2d_*` +// functions, but write the result to `bp`. They return one on success and zero +// on error. Callers using them with memory `BIO`s to encode structures to +// memory should use `i2d_*` directly instead. OPENSSL_EXPORT int i2d_X509_bio(BIO *bp, const X509 *x509); OPENSSL_EXPORT int i2d_X509_CRL_bio(BIO *bp, const X509_CRL *crl); OPENSSL_EXPORT int i2d_X509_REQ_bio(BIO *bp, const X509_REQ *req); @@ -4286,13 +4305,13 @@ OPENSSL_EXPORT int i2d_PrivateKey_bio(BIO *bp, const EVP_PKEY *pkey); OPENSSL_EXPORT int i2d_PUBKEY_bio(BIO *bp, const EVP_PKEY *pkey); OPENSSL_EXPORT int i2d_DHparams_bio(BIO *bp, const DH *dh); -// i2d_PKCS8PrivateKeyInfo_bio encodes |key| as a PKCS#8 PrivateKeyInfo -// structure (see |EVP_marshal_private_key|) and writes the result to |bp|. It +// i2d_PKCS8PrivateKeyInfo_bio encodes `key` as a PKCS#8 PrivateKeyInfo +// structure (see `EVP_marshal_private_key`) and writes the result to `bp`. It // returns one on success and zero on error. OPENSSL_EXPORT int i2d_PKCS8PrivateKeyInfo_bio(BIO *bp, const EVP_PKEY *key); -// The following functions behave like the corresponding |d2i_*_bio| functions, -// but read from |fp| instead. +// The following functions behave like the corresponding `d2i_*_bio` functions, +// but read from `fp` instead. OPENSSL_EXPORT X509 *d2i_X509_fp(FILE *fp, X509 **x509); OPENSSL_EXPORT X509_CRL *d2i_X509_CRL_fp(FILE *fp, X509_CRL **crl); OPENSSL_EXPORT X509_REQ *d2i_X509_REQ_fp(FILE *fp, X509_REQ **req); @@ -4309,8 +4328,8 @@ OPENSSL_EXPORT PKCS8_PRIV_KEY_INFO *d2i_PKCS8_PRIV_KEY_INFO_fp( OPENSSL_EXPORT EVP_PKEY *d2i_PrivateKey_fp(FILE *fp, EVP_PKEY **a); OPENSSL_EXPORT EVP_PKEY *d2i_PUBKEY_fp(FILE *fp, EVP_PKEY **a); -// The following functions behave like the corresponding |i2d_*_bio| functions, -// but write to |fp| instead. +// The following functions behave like the corresponding `i2d_*_bio` functions, +// but write to `fp` instead. OPENSSL_EXPORT int i2d_X509_fp(FILE *fp, const X509 *x509); OPENSSL_EXPORT int i2d_X509_CRL_fp(FILE *fp, const X509_CRL *crl); OPENSSL_EXPORT int i2d_X509_REQ_fp(FILE *fp, const X509_REQ *req); @@ -4328,96 +4347,110 @@ OPENSSL_EXPORT int i2d_PKCS8PrivateKeyInfo_fp(FILE *fp, const EVP_PKEY *key); OPENSSL_EXPORT int i2d_PrivateKey_fp(FILE *fp, const EVP_PKEY *pkey); OPENSSL_EXPORT int i2d_PUBKEY_fp(FILE *fp, const EVP_PKEY *pkey); -// X509_find_by_issuer_and_serial returns the first |X509| in |sk| whose issuer -// and serial are |name| and |serial|, respectively. If no match is found, it +// X509_find_by_issuer_and_serial returns the first `X509` in `sk` whose issuer +// and serial are `name` and `serial`, respectively. If no match is found, it // returns NULL. OPENSSL_EXPORT X509 *X509_find_by_issuer_and_serial(const STACK_OF(X509) *sk, const X509_NAME *name, const ASN1_INTEGER *serial); -// X509_find_by_subject returns the first |X509| in |sk| whose subject is -// |name|. If no match is found, it returns NULL. +// X509_find_by_subject returns the first `X509` in `sk` whose subject is +// `name`. If no match is found, it returns NULL. OPENSSL_EXPORT X509 *X509_find_by_subject(const STACK_OF(X509) *sk, const X509_NAME *name); -// X509_cmp_time compares |s| against |*t|. On success, it returns a negative -// number if |s| <= |*t| and a positive number if |s| > |*t|. On error, it -// returns zero. If |t| is NULL, it uses the current time instead of |*t|. +// X509_cmp_time compares `s` against `*t`. On success, it returns a negative +// number if `s` <= `*t` and a positive number if `s` > `*t`. On error, it +// returns zero. If `t` is NULL, it uses the current time instead of `*t`. // // WARNING: Unlike most comparison functions, this function returns zero on // error, not equality. OPENSSL_EXPORT int X509_cmp_time(const ASN1_TIME *s, const time_t *t); -// X509_cmp_time_posix compares |s| against |t|. On success, it returns a -// negative number if |s| <= |t| and a positive number if |s| > |t|. On error, +// X509_cmp_time_posix compares `s` against `t`. On success, it returns a +// negative number if `s` <= `t` and a positive number if `s` > `t`. On error, // it returns zero. // +// If `s` has a time zone offset, it returns an error (0). +// // WARNING: Unlike most comparison functions, this function returns zero on // error, not equality. OPENSSL_EXPORT int X509_cmp_time_posix(const ASN1_TIME *s, int64_t t); -// X509_cmp_current_time behaves like |X509_cmp_time| but compares |s| against +// X509_cmp_time_posix_nonstandard compares `s` against `t`. On success, it +// returns a negative number if `s` <= `t` and a positive number if `s` > `t`. +// On error, it returns zero. +// +// If `s` has a time zone offset, it applies it before comparing to `t`. See +// `ASN1_TIME_to_posix_nonstandard` for more details. +// +// WARNING: Unlike most comparison functions, this function returns zero on +// error, not equality. +OPENSSL_EXPORT int X509_cmp_time_posix_nonstandard(const ASN1_TIME *s, + int64_t t); + +// X509_cmp_current_time behaves like `X509_cmp_time` but compares `s` against // the current time. OPENSSL_EXPORT int X509_cmp_current_time(const ASN1_TIME *s); -// X509_time_adj calls |X509_time_adj_ex| with |offset_day| equal to zero. +// X509_time_adj calls `X509_time_adj_ex` with `offset_day` equal to zero. OPENSSL_EXPORT ASN1_TIME *X509_time_adj(ASN1_TIME *s, long offset_sec, const time_t *t); -// X509_time_adj_ex behaves like |ASN1_TIME_adj|, but adds an offset to |*t|. If -// |t| is NULL, it uses the current time instead of |*t|. +// X509_time_adj_ex behaves like `ASN1_TIME_adj`, but adds an offset to `*t`. If +// `t` is NULL, it uses the current time instead of `*t`. OPENSSL_EXPORT ASN1_TIME *X509_time_adj_ex(ASN1_TIME *s, int offset_day, long offset_sec, const time_t *t); -// X509_gmtime_adj behaves like |X509_time_adj_ex| but adds |offset_sec| to the +// X509_gmtime_adj behaves like `X509_time_adj_ex` but adds `offset_sec` to the // current time. OPENSSL_EXPORT ASN1_TIME *X509_gmtime_adj(ASN1_TIME *s, long offset_sec); -// X509_issuer_name_cmp behaves like |X509_NAME_cmp|, but compares |a| and |b|'s +// X509_issuer_name_cmp behaves like `X509_NAME_cmp`, but compares `a` and `b`'s // issuer names. OPENSSL_EXPORT int X509_issuer_name_cmp(const X509 *a, const X509 *b); -// X509_subject_name_cmp behaves like |X509_NAME_cmp|, but compares |a| and -// |b|'s subject names. +// X509_subject_name_cmp behaves like `X509_NAME_cmp`, but compares `a` and +// `b`'s subject names. OPENSSL_EXPORT int X509_subject_name_cmp(const X509 *a, const X509 *b); -// X509_CRL_cmp behaves like |X509_NAME_cmp|, but compares |a| and |b|'s +// X509_CRL_cmp behaves like `X509_NAME_cmp`, but compares `a` and `b`'s // issuer names. // // WARNING: This function is misnamed. It does not compare other parts of the -// CRL, only the issuer fields using |X509_NAME_cmp|. +// CRL, only the issuer fields using `X509_NAME_cmp`. OPENSSL_EXPORT int X509_CRL_cmp(const X509_CRL *a, const X509_CRL *b); -// X509_issuer_name_hash returns the hash of |x509|'s issuer name with -// |X509_NAME_hash|. +// X509_issuer_name_hash returns the hash of `x509`'s issuer name with +// `X509_NAME_hash`. // -// This hash is specific to the |X509_LOOKUP_add_dir| filesystem format and is +// This hash is specific to the `X509_LOOKUP_add_dir` filesystem format and is // not suitable for general-purpose X.509 name processing. It is very short, so // there will be hash collisions. It also depends on an OpenSSL-specific // canonicalization process. OPENSSL_EXPORT uint32_t X509_issuer_name_hash(const X509 *x509); -// X509_subject_name_hash returns the hash of |x509|'s subject name with -// |X509_NAME_hash|. +// X509_subject_name_hash returns the hash of `x509`'s subject name with +// `X509_NAME_hash`. // -// This hash is specific to the |X509_LOOKUP_add_dir| filesystem format and is +// This hash is specific to the `X509_LOOKUP_add_dir` filesystem format and is // not suitable for general-purpose X.509 name processing. It is very short, so // there will be hash collisions. It also depends on an OpenSSL-specific // canonicalization process. OPENSSL_EXPORT uint32_t X509_subject_name_hash(const X509 *x509); -// X509_issuer_name_hash_old returns the hash of |x509|'s issuer name with -// |X509_NAME_hash_old|. +// X509_issuer_name_hash_old returns the hash of `x509`'s issuer name with +// `X509_NAME_hash_old`. // -// This hash is specific to the |X509_LOOKUP_add_dir| filesystem format and is +// This hash is specific to the `X509_LOOKUP_add_dir` filesystem format and is // not suitable for general-purpose X.509 name processing. It is very short, so // there will be hash collisions. OPENSSL_EXPORT uint32_t X509_issuer_name_hash_old(const X509 *x509); -// X509_subject_name_hash_old returns the hash of |x509|'s usjbect name with -// |X509_NAME_hash_old|. +// X509_subject_name_hash_old returns the hash of `x509`'s usjbect name with +// `X509_NAME_hash_old`. // -// This hash is specific to the |X509_LOOKUP_add_dir| filesystem format and is +// This hash is specific to the `X509_LOOKUP_add_dir` filesystem format and is // not suitable for general-purpose X.509 name processing. It is very short, so // there will be hash collisions. OPENSSL_EXPORT uint32_t X509_subject_name_hash_old(const X509 *x509); @@ -4425,7 +4458,7 @@ OPENSSL_EXPORT uint32_t X509_subject_name_hash_old(const X509 *x509); // ex_data functions. // -// See |ex_data.h| for details. +// See `ex_data.h` for details. OPENSSL_EXPORT int X509_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused, @@ -4449,70 +4482,70 @@ OPENSSL_EXPORT void *X509_STORE_CTX_get_ex_data(X509_STORE_CTX *ctx, int idx); // Hashing and signing ASN.1 structures. -// ASN1_digest serializes |data| with |i2d| and then hashes the result with -// |type|. On success, it returns one, writes the digest to |md|, and sets -// |*len| to the digest length if non-NULL. On error, it returns zero. +// ASN1_digest serializes `data` with `i2d` and then hashes the result with +// `type`. On success, it returns one, writes the digest to `md`, and sets +// `*len` to the digest length if non-NULL. On error, it returns zero. // -// |EVP_MD_CTX_size| bytes are written, which is at most |EVP_MAX_MD_SIZE|. The +// `EVP_MD_CTX_size` bytes are written, which is at most `EVP_MAX_MD_SIZE`. The // buffer must have sufficient space for this output. OPENSSL_EXPORT int ASN1_digest(i2d_of_void *i2d, const EVP_MD *type, char *data, unsigned char *md, unsigned int *len); -// ASN1_item_digest serializes |data| with |it| and then hashes the result with -// |type|. On success, it returns one, writes the digest to |md|, and sets -// |*len| to the digest length if non-NULL. On error, it returns zero. +// ASN1_item_digest serializes `data` with `it` and then hashes the result with +// `type`. On success, it returns one, writes the digest to `md`, and sets +// `*len` to the digest length if non-NULL. On error, it returns zero. // -// |EVP_MD_CTX_size| bytes are written, which is at most |EVP_MAX_MD_SIZE|. The +// `EVP_MD_CTX_size` bytes are written, which is at most `EVP_MAX_MD_SIZE`. The // buffer must have sufficient space for this output. // -// WARNING: |data| must be a pointer with the same type as |it|'s corresponding +// WARNING: `data` must be a pointer with the same type as `it`'s corresponding // C type. Using the wrong type is a potentially exploitable memory error. OPENSSL_EXPORT int ASN1_item_digest(const ASN1_ITEM *it, const EVP_MD *type, void *data, unsigned char *md, unsigned int *len); -// ASN1_item_verify serializes |data| with |it| and then verifies |signature| is -// a valid signature for the result with |algor1| and |pkey|. It returns one on +// ASN1_item_verify serializes `data` with `it` and then verifies `signature` is +// a valid signature for the result with `algor1` and `pkey`. It returns one on // success and zero on error. The signature and algorithm are interpreted as in // X.509. // -// WARNING: |data| must be a pointer with the same type as |it|'s corresponding +// WARNING: `data` must be a pointer with the same type as `it`'s corresponding // C type. Using the wrong type is a potentially exploitable memory error. OPENSSL_EXPORT int ASN1_item_verify(const ASN1_ITEM *it, const X509_ALGOR *algor1, const ASN1_BIT_STRING *signature, - void *data, EVP_PKEY *pkey); + void *data, const EVP_PKEY *pkey); -// ASN1_item_sign serializes |data| with |it| and then signs the result with -// the private key |pkey|. It returns the length of the signature on success and -// zero on error. On success, it writes the signature to |signature| and the -// signature algorithm to each of |algor1| and |algor2|. Either of |algor1| or -// |algor2| may be NULL to ignore them. This function uses digest algorithm -// |md|, or |pkey|'s default if NULL. Other signing parameters use |pkey|'s -// defaults. To customize them, use |ASN1_item_sign_ctx|. +// ASN1_item_sign serializes `data` with `it` and then signs the result with +// the private key `pkey`. It returns the length of the signature on success and +// zero on error. On success, it writes the signature to `signature` and the +// signature algorithm to each of `algor1` and `algor2`. Either of `algor1` or +// `algor2` may be NULL to ignore them. This function uses digest algorithm +// `md`, or `pkey`'s default if NULL. Other signing parameters use `pkey`'s +// defaults. To customize them, use `ASN1_item_sign_ctx`. // -// |algor1| and |algor2| may point into part of |asn| and will be updated before -// |asn| is serialized. +// `algor1` and `algor2` may point into part of `asn` and will be updated before +// `asn` is serialized. // -// WARNING: |data| must be a pointer with the same type as |it|'s corresponding +// WARNING: `data` must be a pointer with the same type as `it`'s corresponding // C type. Using the wrong type is a potentially exploitable memory error. OPENSSL_EXPORT int ASN1_item_sign(const ASN1_ITEM *it, X509_ALGOR *algor1, X509_ALGOR *algor2, ASN1_BIT_STRING *signature, void *data, - EVP_PKEY *pkey, const EVP_MD *type); + const EVP_PKEY *pkey, const EVP_MD *type); -// ASN1_item_sign_ctx behaves like |ASN1_item_sign| except the signature is -// signed with |ctx|, |ctx|, which must have been initialized with -// |EVP_DigestSignInit|. The caller should configure the corresponding -// |EVP_PKEY_CTX| with any additional parameters before calling this function. +// ASN1_item_sign_ctx behaves like `ASN1_item_sign` except the signature is +// signed with `ctx`, `ctx`, which must have been initialized with +// `EVP_DigestSignInit`. The caller should configure the corresponding +// `EVP_PKEY_CTX` with any additional parameters before calling this function. // -// On success or failure, this function mutates |ctx| and resets it to the empty +// On success or failure, this function mutates `ctx` and resets it to the empty // state. Caller should not rely on its contents after the function returns. // -// |algor1| and |algor2| may point into part of |asn| and will be updated before -// |asn| is serialized. +// `algor1` and `algor2` may point into part of `asn` and will be updated before +// `asn` is serialized. // -// WARNING: |data| must be a pointer with the same type as |it|'s corresponding +// WARNING: `data` must be a pointer with the same type as `it`'s corresponding // C type. Using the wrong type is a potentially exploitable memory error. OPENSSL_EXPORT int ASN1_item_sign_ctx(const ASN1_ITEM *it, X509_ALGOR *algor1, X509_ALGOR *algor2, @@ -4525,55 +4558,55 @@ OPENSSL_EXPORT int ASN1_item_sign_ctx(const ASN1_ITEM *it, X509_ALGOR *algor1, // The following functions expose portions of certificate validation. They are // exported for compatibility with existing callers, or to support some obscure // use cases. Most callers, however, will not need these functions and should -// instead use |X509_STORE_CTX| APIs. +// instead use `X509_STORE_CTX` APIs. -// X509_supported_extension returns one if |ex| is a critical X.509 certificate -// extension, supported by |X509_verify_cert|, and zero otherwise. +// X509_supported_extension returns one if `ex` is a critical X.509 certificate +// extension, supported by `X509_verify_cert`, and zero otherwise. // // Note this function only reports certificate extensions (as opposed to CRL or // CRL extensions), and only extensions that are expected to be marked critical. -// Additionally, |X509_verify_cert| checks for unsupported critical extensions +// Additionally, `X509_verify_cert` checks for unsupported critical extensions // internally, so most callers will not need to call this function separately. OPENSSL_EXPORT int X509_supported_extension(const X509_EXTENSION *ex); -// X509_check_ca returns one if |x509| may be considered a CA certificate, +// X509_check_ca returns one if `x509` may be considered a CA certificate, // according to basic constraints and key usage extensions. Otherwise, it -// returns zero. If |x509| is an X509v1 certificate, and thus has no extensions, +// returns zero. If `x509` is an X509v1 certificate, and thus has no extensions, // it is considered eligible. // -// This function returning one does not indicate that |x509| is trusted, only +// This function returning one does not indicate that `x509` is trusted, only // that it is eligible to be a CA. OPENSSL_EXPORT int X509_check_ca(const X509 *x509); -// X509_check_issued checks if |issuer| and |subject|'s name, authority key -// identifier, and key usage fields allow |issuer| to have issued |subject|. It -// returns |X509_V_OK| on success and an |X509_V_ERR_*| value otherwise. +// X509_check_issued checks if `issuer` and `subject`'s name, authority key +// identifier, and key usage fields allow `issuer` to have issued `subject`. It +// returns `X509_V_OK` on success and an `X509_V_ERR_*` value otherwise. // -// This function does not check the signature on |subject|. Rather, it is +// This function does not check the signature on `subject`. Rather, it is // intended to prune the set of possible issuer certificates during // path-building. OPENSSL_EXPORT int X509_check_issued(const X509 *issuer, const X509 *subject); -// NAME_CONSTRAINTS_check checks if |x509| satisfies name constraints in |nc|. -// It returns |X509_V_OK| on success and some |X509_V_ERR_*| constant on error. +// NAME_CONSTRAINTS_check checks if `x509` satisfies name constraints in `nc`. +// It returns `X509_V_OK` on success and some `X509_V_ERR_*` constant on error. OPENSSL_EXPORT int NAME_CONSTRAINTS_check(const X509 *x509, const NAME_CONSTRAINTS *nc); -// X509_check_host checks if |x509| matches the DNS name |chk|. It returns one -// on match, zero on mismatch, or a negative number on error. |flags| should be -// some combination of |X509_CHECK_FLAG_*| and modifies the behavior. On match, -// if |out_peername| is non-NULL, it additionally sets |*out_peername| to a +// X509_check_host checks if `x509` matches the DNS name `chk`. It returns one +// on match, zero on mismatch, or a negative number on error. `flags` should be +// some combination of `X509_CHECK_FLAG_*` and modifies the behavior. On match, +// if `out_peername` is non-NULL, it additionally sets `*out_peername` to a // newly-allocated, NUL-terminated string containing the DNS name or wildcard in -// the certificate which matched. The caller must then free |*out_peername| with -// |OPENSSL_free| when done. +// the certificate which matched. The caller must then free `*out_peername` with +// `OPENSSL_free` when done. // // By default, both subject alternative names and the subject's common name // attribute are checked. The latter has long been deprecated, so callers should -// include |X509_CHECK_FLAG_NEVER_CHECK_SUBJECT| in |flags| to use the standard +// include `X509_CHECK_FLAG_NEVER_CHECK_SUBJECT` in `flags` to use the standard // behavior. https://crbug.com/boringssl/464 tracks fixing the default. // -// This function does not check if |x509| is a trusted certificate, only if, -// were it trusted, it would match |chk|. +// This function does not check if `x509` is a trusted certificate, only if, +// were it trusted, it would match `chk`. // // WARNING: This function differs from the usual calling convention and may // return either 0 or a negative number on error. @@ -4583,16 +4616,16 @@ OPENSSL_EXPORT int X509_check_host(const X509 *x509, const char *chk, size_t chklen, unsigned int flags, char **out_peername); -// X509_check_email checks if |x509| matches the email address |chk|. It returns -// one on match, zero on mismatch, or a negative number on error. |flags| should -// be some combination of |X509_CHECK_FLAG_*| and modifies the behavior. +// X509_check_email checks if `x509` matches the email address `chk`. It returns +// one on match, zero on mismatch, or a negative number on error. `flags` should +// be some combination of `X509_CHECK_FLAG_*` and modifies the behavior. // // By default, both subject alternative names and the subject's email address -// attribute are checked. The |X509_CHECK_FLAG_NEVER_CHECK_SUBJECT| flag may be +// attribute are checked. The `X509_CHECK_FLAG_NEVER_CHECK_SUBJECT` flag may be // used to change this behavior. // -// This function does not check if |x509| is a trusted certificate, only if, -// were it trusted, it would match |chk|. +// This function does not check if `x509` is a trusted certificate, only if, +// were it trusted, it would match `chk`. // // WARNING: This function differs from the usual calling convention and may // return either 0 or a negative number on error. @@ -4601,14 +4634,14 @@ OPENSSL_EXPORT int X509_check_host(const X509 *x509, const char *chk, OPENSSL_EXPORT int X509_check_email(const X509 *x509, const char *chk, size_t chklen, unsigned int flags); -// X509_check_ip checks if |x509| matches the IP address |chk|. The IP address +// X509_check_ip checks if `x509` matches the IP address `chk`. The IP address // is represented in byte form and should be 4 bytes for an IPv4 address and 16 // bytes for an IPv6 address. It returns one on match, zero on mismatch, or a -// negative number on error. |flags| should be some combination of -// |X509_CHECK_FLAG_*| and modifies the behavior. +// negative number on error. `flags` should be some combination of +// `X509_CHECK_FLAG_*` and modifies the behavior. // -// This function does not check if |x509| is a trusted certificate, only if, -// were it trusted, it would match |chk|. +// This function does not check if `x509` is a trusted certificate, only if, +// were it trusted, it would match `chk`. // // WARNING: This function differs from the usual calling convention and may // return either 0 or a negative number on error. @@ -4617,8 +4650,8 @@ OPENSSL_EXPORT int X509_check_email(const X509 *x509, const char *chk, OPENSSL_EXPORT int X509_check_ip(const X509 *x509, const uint8_t *chk, size_t chklen, unsigned int flags); -// X509_check_ip_asc behaves like |X509_check_ip| except the IP address is -// specified in textual form in |ipasc|. +// X509_check_ip_asc behaves like `X509_check_ip` except the IP address is +// specified in textual form in `ipasc`. // // WARNING: This function differs from the usual calling convention and may // return either 0 or a negative number on error. @@ -4627,70 +4660,74 @@ OPENSSL_EXPORT int X509_check_ip(const X509 *x509, const uint8_t *chk, OPENSSL_EXPORT int X509_check_ip_asc(const X509 *x509, const char *ipasc, unsigned int flags); -// X509_STORE_CTX_get1_issuer looks up a candidate trusted issuer for |x509| out -// of |ctx|'s |X509_STORE|, based on the criteria in |X509_check_issued|. If one -// was found, it returns one and sets |*out_issuer| to the issuer. The caller -// must release |*out_issuer| with |X509_free| when done. If none was found, it -// returns zero and leaves |*out_issuer| unchanged. +// X509_STORE_CTX_get1_issuer looks up a candidate trusted issuer for `x509` out +// of `ctx`'s `X509_STORE`, based on the criteria in `X509_check_issued`. If one +// was found, it returns one and sets `*out_issuer` to the issuer. The caller +// must release `*out_issuer` with `X509_free` when done. If none was found, it +// returns zero and leaves `*out_issuer` unchanged. // // This function only searches for trusted issuers. It does not consider -// untrusted intermediates passed in to |X509_STORE_CTX_init|. +// untrusted intermediates passed in to `X509_STORE_CTX_init`. OPENSSL_EXPORT int X509_STORE_CTX_get1_issuer(X509 **out_issuer, X509_STORE_CTX *ctx, const X509 *x509); -// X509_check_purpose performs checks if |x509|'s basic constraints, key usage, -// and extended key usage extensions for the specified purpose. |purpose| should -// be one of |X509_PURPOSE_*| constants. See |X509_VERIFY_PARAM_set_purpose| for -// details. It returns one if |x509|'s extensions are consistent with |purpose| -// and zero otherwise. If |ca| is non-zero, |x509| is checked as a CA +// X509_check_purpose performs checks if `x509`'s basic constraints, key usage, +// and extended key usage extensions for the specified purpose. `purpose` should +// be one of `X509_PURPOSE_*` constants. See `X509_VERIFY_PARAM_set_purpose` for +// details. It returns one if `x509`'s extensions are consistent with `purpose` +// and zero otherwise. If `ca` is non-zero, `x509` is checked as a CA // certificate. Otherwise, it is checked as an end-entity certificate. // -// If |purpose| is -1, this function performs no purpose checks, but it parses -// some extensions in |x509| and may return zero on syntax error. Historically, +// If `purpose` is -1, this function performs no purpose checks, but it parses +// some extensions in `x509` and may return zero on syntax error. Historically, // callers primarily used this function to trigger this parsing, but this is no -// longer necessary. Functions acting on |X509| will internally parse as needed. -OPENSSL_EXPORT int X509_check_purpose(X509 *x509, int purpose, int ca); +// longer necessary. Functions acting on `X509` will internally parse as needed. +OPENSSL_EXPORT int X509_check_purpose(const X509 *x509, int purpose, int ca); #define X509_TRUST_TRUSTED 1 #define X509_TRUST_REJECTED 2 #define X509_TRUST_UNTRUSTED 3 -// X509_check_trust checks if |x509| is a valid trust anchor for trust type -// |id|. See |X509_VERIFY_PARAM_set_trust| for details. It returns -// |X509_TRUST_TRUSTED| if |x509| is a trust anchor, |X509_TRUST_REJECTED| if it -// was distrusted, and |X509_TRUST_UNTRUSTED| otherwise. |id| should be one of -// the |X509_TRUST_*| constants, or zero to indicate the default behavior. -// |flags| should be zero and is ignored. -OPENSSL_EXPORT int X509_check_trust(X509 *x509, int id, int flags); +// X509_check_trust checks if `x509` is a valid trust anchor for trust type +// `id`. See `X509_VERIFY_PARAM_set_trust` for details. It returns +// `X509_TRUST_TRUSTED` if `x509` is a trust anchor, `X509_TRUST_REJECTED` if it +// was distrusted, and `X509_TRUST_UNTRUSTED` otherwise. `id` should be one of +// the `X509_TRUST_*` constants, or zero to indicate the default behavior. +// `flags` should be zero and is ignored. +// +// WARNING: This function assumes `x509` is a trusted certificate, installed in +// an `X509_STORE`. If passed an arbitrary certificate from an untrusted source, +// it may return `X509_TRUST_TRUSTED`. +OPENSSL_EXPORT int X509_check_trust(const X509 *x509, int id, int flags); // X509_STORE_CTX_get1_certs returns a newly-allocated stack containing all -// trusted certificates in |ctx|'s |X509_STORE| whose subject matches |name|, or -// NULL on error. The caller must release the result with |sk_X509_pop_free| and -// |X509_free| when done. +// trusted certificates in `ctx`'s `X509_STORE` whose subject matches `name`, or +// NULL on error. The caller must release the result with `sk_X509_pop_free` and +// `X509_free` when done. OPENSSL_EXPORT STACK_OF(X509) *X509_STORE_CTX_get1_certs(X509_STORE_CTX *ctx, const X509_NAME *name); // X509_STORE_CTX_get1_crls returns a newly-allocated stack containing all -// CRLs in |ctx|'s |X509_STORE| whose subject matches |name|, or NULL on error. -// The caller must release the result with |sk_X509_CRL_pop_free| and -// |X509_CRL_free| when done. +// CRLs in `ctx`'s `X509_STORE` whose subject matches `name`, or NULL on error. +// The caller must release the result with `sk_X509_CRL_pop_free` and +// `X509_CRL_free` when done. OPENSSL_EXPORT STACK_OF(X509_CRL) *X509_STORE_CTX_get1_crls( X509_STORE_CTX *ctx, const X509_NAME *name); -// X509_STORE_CTX_get_by_subject looks up an object of type |type| in |ctx|'s -// |X509_STORE| that matches |name|. |type| should be one of the |X509_LU_*| +// X509_STORE_CTX_get_by_subject looks up an object of type `type` in `ctx`'s +// `X509_STORE` that matches `name`. `type` should be one of the `X509_LU_*` // constants to indicate the type of object. If a match was found, it stores the -// result in |ret| and returns one. Otherwise, it returns zero. If multiple +// result in `ret` and returns one. Otherwise, it returns zero. If multiple // objects match, this function outputs an arbitrary one. // -// WARNING: |ret| must be in the empty state, as returned by |X509_OBJECT_new|. -// Otherwise, the object currently in |ret| will be leaked when overwritten. +// WARNING: `ret` must be in the empty state, as returned by `X509_OBJECT_new`. +// Otherwise, the object currently in `ret` will be leaked when overwritten. // https://crbug.com/boringssl/685 tracks fixing this. // // WARNING: Multiple trusted certificates or CRLs may share a name. In this // case, this function returns an arbitrary match. Use -// |X509_STORE_CTX_get1_certs| or |X509_STORE_CTX_get1_crls| instead. +// `X509_STORE_CTX_get1_certs` or `X509_STORE_CTX_get1_crls` instead. OPENSSL_EXPORT int X509_STORE_CTX_get_by_subject(X509_STORE_CTX *ctx, int type, const X509_NAME *name, X509_OBJECT *ret); @@ -4698,7 +4735,7 @@ OPENSSL_EXPORT int X509_STORE_CTX_get_by_subject(X509_STORE_CTX *ctx, int type, // X.509 information. // -// |X509_INFO| is the return type for |PEM_X509_INFO_read_bio|, defined in +// `X509_INFO` is the return type for `PEM_X509_INFO_read_bio`, defined in // . It is used to store a certificate, CRL, or private key. This // type is defined in this header for OpenSSL compatibility. @@ -4718,25 +4755,25 @@ struct X509_info_st { DEFINE_STACK_OF(X509_INFO) -// X509_INFO_free releases memory associated with |info|. +// X509_INFO_free releases memory associated with `info`. OPENSSL_EXPORT void X509_INFO_free(X509_INFO *info); // Deprecated custom extension registration. // // The following functions allow callers to register custom extensions for use -// with |X509V3_EXT_d2i| and related functions. This mechanism is deprecated and -// will be removed in the future. As discussed in |X509V3_EXT_add|, it is not +// with `X509V3_EXT_d2i` and related functions. This mechanism is deprecated and +// will be removed in the future. As discussed in `X509V3_EXT_add`, it is not // possible to safely register a custom extension without risking race // conditions and memory errors when linked with other users of BoringSSL. // // Moreover, it is not necessary to register a custom extension to process // extensions unknown to BoringSSL. Registration does not impact certificate // verification. Caller should instead use functions such as -// |ASN1_OBJECT_create|, |X509_get_ext_by_OBJ|, |X509_EXTENSION_get_data|, and -// |X509_EXTENSION_create_by_OBJ| to inspect or create extensions directly. +// `ASN1_OBJECT_create`, `X509_get_ext_by_OBJ`, `X509_EXTENSION_get_data`, and +// `X509_EXTENSION_create_by_OBJ` to inspect or create extensions directly. -// The following function pointer types are used in |X509V3_EXT_METHOD|. +// The following function pointer types are used in `X509V3_EXT_METHOD`. typedef void *(*X509V3_EXT_NEW)(void); typedef void (*X509V3_EXT_FREE)(void *ext); typedef void *(*X509V3_EXT_D2I)(void *ext, const uint8_t **inp, long len); @@ -4755,20 +4792,20 @@ typedef int (*X509V3_EXT_I2R)(const X509V3_EXT_METHOD *method, void *ext, typedef void *(*X509V3_EXT_R2I)(const X509V3_EXT_METHOD *method, const X509V3_CTX *ctx, const char *str); -// A v3_ext_method, aka |X509V3_EXT_METHOD|, is a deprecated type which defines +// A v3_ext_method, aka `X509V3_EXT_METHOD`, is a deprecated type which defines // a custom extension. struct v3_ext_method { // ext_nid is the NID of the extension. int ext_nid; - // ext_flags is a combination of |X509V3_EXT_*| constants. + // ext_flags is a combination of `X509V3_EXT_*` constants. int ext_flags; // it determines how values of this extension are allocated, released, parsed, // and marshalled. This must be non-NULL. ASN1_ITEM_EXP *it; - // The following functions are ignored in favor of |it|. They are retained in + // The following functions are ignored in favor of `it`. They are retained in // the struct only for source compatibility with existing struct definitions. X509V3_EXT_NEW ext_new; X509V3_EXT_FREE ext_free; @@ -4791,31 +4828,31 @@ struct v3_ext_method { void *usr_data; // Any extension specific data } /* X509V3_EXT_METHOD */; -// X509V3_EXT_MULTILINE causes the result of an |X509V3_EXT_METHOD|'s |i2v| +// X509V3_EXT_MULTILINE causes the result of an `X509V3_EXT_METHOD`'s `i2v` // function to be printed on separate lines, rather than separated by commas. #define X509V3_EXT_MULTILINE 0x4 -// X509V3_EXT_get returns the |X509V3_EXT_METHOD| corresponding to |ext|'s +// X509V3_EXT_get returns the `X509V3_EXT_METHOD` corresponding to `ext`'s // extension type, or NULL if none was registered. OPENSSL_EXPORT const X509V3_EXT_METHOD *X509V3_EXT_get( const X509_EXTENSION *ext); -// X509V3_EXT_get_nid returns the |X509V3_EXT_METHOD| corresponding to |nid|, or +// X509V3_EXT_get_nid returns the `X509V3_EXT_METHOD` corresponding to `nid`, or // NULL if none was registered. OPENSSL_EXPORT const X509V3_EXT_METHOD *X509V3_EXT_get_nid(int nid); -// X509V3_EXT_add registers |ext| as a custom extension for the extension type -// |ext->ext_nid|. |ext| must be valid for the remainder of the address space's +// X509V3_EXT_add registers `ext` as a custom extension for the extension type +// `ext->ext_nid`. `ext` must be valid for the remainder of the address space's // lifetime. It returns one on success and zero on error. // // WARNING: This function modifies global state. If other code in the same -// address space also registers an extension with type |ext->ext_nid|, the two +// address space also registers an extension with type `ext->ext_nid`, the two // registrations will conflict. Which registration takes effect is undefined. If // the two registrations use incompatible in-memory representations, code // expecting the other registration will then cast a type to the wrong type, // resulting in a potentially exploitable memory error. This conflict can also -// occur if BoringSSL later adds support for |ext->ext_nid|, with a different -// in-memory representation than the one expected by |ext|. +// occur if BoringSSL later adds support for `ext->ext_nid`, with a different +// in-memory representation than the one expected by `ext`. // // This function, additionally, is not thread-safe and cannot be called // concurrently with any other BoringSSL function. @@ -4823,15 +4860,15 @@ OPENSSL_EXPORT const X509V3_EXT_METHOD *X509V3_EXT_get_nid(int nid); // As a result, it is impossible to safely use this function. Registering a // custom extension has no impact on certificate verification so, instead, // callers should simply handle the custom extension with the byte-based -// |X509_EXTENSION| APIs directly. Registering |ext| with the library has little +// `X509_EXTENSION` APIs directly. Registering `ext` with the library has little // practical value. OPENSSL_EXPORT OPENSSL_DEPRECATED int X509V3_EXT_add(X509V3_EXT_METHOD *ext); -// X509V3_EXT_add_alias registers a custom extension with NID |nid_to|. The -// corresponding ASN.1 type is copied from |nid_from|. It returns one on success +// X509V3_EXT_add_alias registers a custom extension with NID `nid_to`. The +// corresponding ASN.1 type is copied from `nid_from`. It returns one on success // and zero on error. // -// WARNING: Do not use this function. See |X509V3_EXT_add|. +// WARNING: Do not use this function. See `X509V3_EXT_add`. OPENSSL_EXPORT OPENSSL_DEPRECATED int X509V3_EXT_add_alias(int nid_to, int nid_from); @@ -4855,10 +4892,10 @@ OPENSSL_EXPORT OPENSSL_DEPRECATED int X509V3_EXT_add_alias(int nid_to, // review than most of the library and may have bugs including memory leaks or // crashes. -// v3_ext_ctx, aka |X509V3_CTX|, contains additional context information for +// v3_ext_ctx, aka `X509V3_CTX`, contains additional context information for // constructing extensions. Some string formats reference additional values in -// these objects. It must be initialized with |X509V3_set_ctx| or -// |X509V3_set_ctx_test| before use. +// these objects. It must be initialized with `X509V3_set_ctx` or +// `X509V3_set_ctx_test` before use. struct v3_ext_ctx { int flags; const X509 *issuer_cert; @@ -4870,17 +4907,17 @@ struct v3_ext_ctx { #define X509V3_CTX_TEST 0x1 -// X509V3_set_ctx initializes |ctx| with the specified objects. Some string +// X509V3_set_ctx initializes `ctx` with the specified objects. Some string // formats will reference fields in these objects. Each object may be NULL to -// omit it, in which case those formats cannot be used. |flags| should be zero, -// unless called via |X509V3_set_ctx_test|. +// omit it, in which case those formats cannot be used. `flags` should be zero, +// unless called via `X509V3_set_ctx_test`. // -// |issuer|, |subject|, |req|, and |crl|, if non-NULL, must outlive |ctx|. +// `issuer`, `subject`, `req`, and `crl`, if non-NULL, must outlive `ctx`. OPENSSL_EXPORT void X509V3_set_ctx(X509V3_CTX *ctx, const X509 *issuer, const X509 *subject, const X509_REQ *req, const X509_CRL *crl, int flags); -// X509V3_set_ctx_test calls |X509V3_set_ctx| without any reference objects and +// X509V3_set_ctx_test calls `X509V3_set_ctx` without any reference objects and // mocks out some features that use them. The resulting extensions may be // incomplete and should be discarded. This can be used to partially validate // syntax. @@ -4889,149 +4926,149 @@ OPENSSL_EXPORT void X509V3_set_ctx(X509V3_CTX *ctx, const X509 *issuer, #define X509V3_set_ctx_test(ctx) \ X509V3_set_ctx(ctx, NULL, NULL, NULL, NULL, X509V3_CTX_TEST) -// X509V3_set_nconf sets |ctx| to use |conf| as the config database. |ctx| must -// have previously been initialized by |X509V3_set_ctx| or -// |X509V3_set_ctx_test|. Some string formats will reference sections in |conf|. -// |conf| may be NULL, in which case these formats cannot be used. If non-NULL, -// |conf| must outlive |ctx|. +// X509V3_set_nconf sets `ctx` to use `conf` as the config database. `ctx` must +// have previously been initialized by `X509V3_set_ctx` or +// `X509V3_set_ctx_test`. Some string formats will reference sections in `conf`. +// `conf` may be NULL, in which case these formats cannot be used. If non-NULL, +// `conf` must outlive `ctx`. OPENSSL_EXPORT void X509V3_set_nconf(X509V3_CTX *ctx, const CONF *conf); -// X509V3_set_ctx_nodb calls |X509V3_set_nconf| with no config database. +// X509V3_set_ctx_nodb calls `X509V3_set_nconf` with no config database. #define X509V3_set_ctx_nodb(ctx) X509V3_set_nconf(ctx, NULL) -// X509V3_EXT_nconf constructs an extension of type specified by |name|, and -// value specified by |value|. It returns a newly-allocated |X509_EXTENSION| -// object on success, or NULL on error. |conf| and |ctx| specify additional -// information referenced by some formats. Either |conf| or |ctx| may be NULL, +// X509V3_EXT_nconf constructs an extension of type specified by `name`, and +// value specified by `value`. It returns a newly-allocated `X509_EXTENSION` +// object on success, or NULL on error. `conf` and `ctx` specify additional +// information referenced by some formats. Either `conf` or `ctx` may be NULL, // in which case features which use it will be disabled. // -// If non-NULL, |ctx| must be initialized with |X509V3_set_ctx| or -// |X509V3_set_ctx_test|. +// If non-NULL, `ctx` must be initialized with `X509V3_set_ctx` or +// `X509V3_set_ctx_test`. // -// Both |conf| and |ctx| provide a |CONF| object. When |ctx| is non-NULL, most -// features use the |ctx| copy, configured with |X509V3_set_ctx|, but some use -// |conf|. Callers should ensure the two match to avoid surprisingly behavior. +// Both `conf` and `ctx` provide a `CONF` object. When `ctx` is non-NULL, most +// features use the `ctx` copy, configured with `X509V3_set_ctx`, but some use +// `conf`. Callers should ensure the two match to avoid surprisingly behavior. OPENSSL_EXPORT X509_EXTENSION *X509V3_EXT_nconf(const CONF *conf, const X509V3_CTX *ctx, const char *name, const char *value); -// X509V3_EXT_nconf_nid behaves like |X509V3_EXT_nconf|, except the extension +// X509V3_EXT_nconf_nid behaves like `X509V3_EXT_nconf`, except the extension // type is specified as a NID. OPENSSL_EXPORT X509_EXTENSION *X509V3_EXT_nconf_nid(const CONF *conf, const X509V3_CTX *ctx, int ext_nid, const char *value); -// X509V3_EXT_conf_nid calls |X509V3_EXT_nconf_nid|. |conf| must be NULL. +// X509V3_EXT_conf_nid calls `X509V3_EXT_nconf_nid`. `conf` must be NULL. OPENSSL_EXPORT X509_EXTENSION *X509V3_EXT_conf_nid(CRYPTO_MUST_BE_NULL *conf, const X509V3_CTX *ctx, int ext_nid, const char *value); -// X509V3_EXT_add_nconf_sk looks up the section named |section| in |conf|. For -// each |CONF_VALUE| in the section, it constructs an extension as in -// |X509V3_EXT_nconf|, taking |name| and |value| from the |CONF_VALUE|. Each new -// extension is appended to |*sk|. If |*sk| is non-NULL, and at least one -// extension is added, it sets |*sk| to a newly-allocated -// |STACK_OF(X509_EXTENSION)|. It returns one on success and zero on error. +// X509V3_EXT_add_nconf_sk looks up the section named `section` in `conf`. For +// each `CONF_VALUE` in the section, it constructs an extension as in +// `X509V3_EXT_nconf`, taking `name` and `value` from the `CONF_VALUE`. Each new +// extension is appended to `*sk`. If `*sk` is non-NULL, and at least one +// extension is added, it sets `*sk` to a newly-allocated +// `STACK_OF(X509_EXTENSION)`. It returns one on success and zero on error. OPENSSL_EXPORT int X509V3_EXT_add_nconf_sk(const CONF *conf, const X509V3_CTX *ctx, const char *section, STACK_OF(X509_EXTENSION) **sk); -// X509V3_EXT_add_nconf adds extensions to |cert| as in -// |X509V3_EXT_add_nconf_sk|. It returns one on success and zero on error. +// X509V3_EXT_add_nconf adds extensions to `cert` as in +// `X509V3_EXT_add_nconf_sk`. It returns one on success and zero on error. OPENSSL_EXPORT int X509V3_EXT_add_nconf(const CONF *conf, const X509V3_CTX *ctx, const char *section, X509 *cert); -// X509V3_EXT_REQ_add_nconf adds extensions to |req| as in -// |X509V3_EXT_add_nconf_sk|. It returns one on success and zero on error. +// X509V3_EXT_REQ_add_nconf adds extensions to `req` as in +// `X509V3_EXT_add_nconf_sk`. It returns one on success and zero on error. OPENSSL_EXPORT int X509V3_EXT_REQ_add_nconf(const CONF *conf, const X509V3_CTX *ctx, const char *section, X509_REQ *req); -// X509V3_EXT_CRL_add_nconf adds extensions to |crl| as in -// |X509V3_EXT_add_nconf_sk|. It returns one on success and zero on error. +// X509V3_EXT_CRL_add_nconf adds extensions to `crl` as in +// `X509V3_EXT_add_nconf_sk`. It returns one on success and zero on error. OPENSSL_EXPORT int X509V3_EXT_CRL_add_nconf(const CONF *conf, const X509V3_CTX *ctx, const char *section, X509_CRL *crl); -// i2s_ASN1_OCTET_STRING returns a human-readable representation of |oct| as a -// newly-allocated, NUL-terminated string, or NULL on error. |method| is -// ignored. The caller must release the result with |OPENSSL_free| when done. +// i2s_ASN1_OCTET_STRING returns a human-readable representation of `oct` as a +// newly-allocated, NUL-terminated string, or NULL on error. `method` is +// ignored. The caller must release the result with `OPENSSL_free` when done. OPENSSL_EXPORT char *i2s_ASN1_OCTET_STRING(const X509V3_EXT_METHOD *method, const ASN1_OCTET_STRING *oct); -// s2i_ASN1_OCTET_STRING decodes |str| as a hexadecimal byte string, with +// s2i_ASN1_OCTET_STRING decodes `str` as a hexadecimal byte string, with // optional colon separators between bytes. It returns a newly-allocated -// |ASN1_OCTET_STRING| with the result on success, or NULL on error. |method| -// and |ctx| are ignored. +// `ASN1_OCTET_STRING` with the result on success, or NULL on error. `method` +// and `ctx` are ignored. OPENSSL_EXPORT ASN1_OCTET_STRING *s2i_ASN1_OCTET_STRING( const X509V3_EXT_METHOD *method, const X509V3_CTX *ctx, const char *str); -// i2s_ASN1_INTEGER returns a human-readable representation of |aint| as a -// newly-allocated, NUL-terminated string, or NULL on error. |method| is -// ignored. The caller must release the result with |OPENSSL_free| when done. +// i2s_ASN1_INTEGER returns a human-readable representation of `aint` as a +// newly-allocated, NUL-terminated string, or NULL on error. `method` is +// ignored. The caller must release the result with `OPENSSL_free` when done. OPENSSL_EXPORT char *i2s_ASN1_INTEGER(const X509V3_EXT_METHOD *method, const ASN1_INTEGER *aint); -// s2i_ASN1_INTEGER decodes |value| as the ASCII representation of an integer, -// and returns a newly-allocated |ASN1_INTEGER| containing the result, or NULL -// on error. |method| is ignored. If |value| begins with "0x" or "0X", the input +// s2i_ASN1_INTEGER decodes `value` as the ASCII representation of an integer, +// and returns a newly-allocated `ASN1_INTEGER` containing the result, or NULL +// on error. `method` is ignored. If `value` begins with "0x" or "0X", the input // is decoded in hexadecimal, otherwise decimal. OPENSSL_EXPORT ASN1_INTEGER *s2i_ASN1_INTEGER(const X509V3_EXT_METHOD *method, const char *value); -// i2s_ASN1_ENUMERATED returns a human-readable representation of |aint| as a -// newly-allocated, NUL-terminated string, or NULL on error. |method| is -// ignored. The caller must release the result with |OPENSSL_free| when done. +// i2s_ASN1_ENUMERATED returns a human-readable representation of `aint` as a +// newly-allocated, NUL-terminated string, or NULL on error. `method` is +// ignored. The caller must release the result with `OPENSSL_free` when done. OPENSSL_EXPORT char *i2s_ASN1_ENUMERATED(const X509V3_EXT_METHOD *method, const ASN1_ENUMERATED *aint); -// X509V3_conf_free releases memory associated with |CONF_VALUE|. +// X509V3_conf_free releases memory associated with `CONF_VALUE`. OPENSSL_EXPORT void X509V3_conf_free(CONF_VALUE *val); -// i2v_GENERAL_NAME serializes |gen| as a |CONF_VALUE|. If |ret| is non-NULL, it -// appends the value to |ret| and returns |ret| on success or NULL on error. If -// it returns NULL, the caller is still responsible for freeing |ret|. If |ret| -// is NULL, it returns a newly-allocated |STACK_OF(CONF_VALUE)| containing the -// result. |method| is ignored. When done, the caller should release the result -// with |sk_CONF_VALUE_pop_free| and |X509V3_conf_free|. +// i2v_GENERAL_NAME serializes `gen` as a `CONF_VALUE`. If `ret` is non-NULL, it +// appends the value to `ret` and returns `ret` on success or NULL on error. If +// it returns NULL, the caller is still responsible for freeing `ret`. If `ret` +// is NULL, it returns a newly-allocated `STACK_OF(CONF_VALUE)` containing the +// result. `method` is ignored. When done, the caller should release the result +// with `sk_CONF_VALUE_pop_free` and `X509V3_conf_free`. // // Do not use this function. This is an internal implementation detail of the // human-readable print functions. If extracting a SAN list from a certificate, -// look at |gen| directly. +// look at `gen` directly. OPENSSL_EXPORT STACK_OF(CONF_VALUE) *i2v_GENERAL_NAME( const X509V3_EXT_METHOD *method, const GENERAL_NAME *gen, STACK_OF(CONF_VALUE) *ret); -// i2v_GENERAL_NAMES serializes |gen| as a list of |CONF_VALUE|s. If |ret| is -// non-NULL, it appends the values to |ret| and returns |ret| on success or NULL +// i2v_GENERAL_NAMES serializes `gen` as a list of `CONF_VALUE`s. If `ret` is +// non-NULL, it appends the values to `ret` and returns `ret` on success or NULL // on error. If it returns NULL, the caller is still responsible for freeing -// |ret|. If |ret| is NULL, it returns a newly-allocated |STACK_OF(CONF_VALUE)| -// containing the results. |method| is ignored. +// `ret`. If `ret` is NULL, it returns a newly-allocated `STACK_OF(CONF_VALUE)` +// containing the results. `method` is ignored. // // Do not use this function. This is an internal implementation detail of the // human-readable print functions. If extracting a SAN list from a certificate, -// look at |gen| directly. +// look at `gen` directly. OPENSSL_EXPORT STACK_OF(CONF_VALUE) *i2v_GENERAL_NAMES( const X509V3_EXT_METHOD *method, const GENERAL_NAMES *gen, STACK_OF(CONF_VALUE) *extlist); -// a2i_IPADDRESS decodes |ipasc| as the textual representation of an IPv4 or -// IPv6 address. On success, it returns a newly-allocated |ASN1_OCTET_STRING| +// a2i_IPADDRESS decodes `ipasc` as the textual representation of an IPv4 or +// IPv6 address. On success, it returns a newly-allocated `ASN1_OCTET_STRING` // containing the decoded IP address. IPv4 addresses are represented as 4-byte // strings and IPv6 addresses as 16-byte strings. On failure, it returns NULL. OPENSSL_EXPORT ASN1_OCTET_STRING *a2i_IPADDRESS(const char *ipasc); -// a2i_IPADDRESS_NC decodes |ipasc| as the textual representation of an IPv4 or +// a2i_IPADDRESS_NC decodes `ipasc` as the textual representation of an IPv4 or // IPv6 address range. On success, it returns a newly-allocated -// |ASN1_OCTET_STRING| containing the decoded IP address, followed by the +// `ASN1_OCTET_STRING` containing the decoded IP address, followed by the // decoded mask. IPv4 ranges are represented as 8-byte strings and IPv6 ranges // as 32-byte strings. On failure, it returns NULL. // -// The text format decoded by this function is not the standard CIDR notiation. +// The text format decoded by this function is not the standard CIDR notation. // Instead, the mask after the "/" is represented as another IP address. For // example, "192.168.0.0/16" would be written "192.168.0.0/255.255.0.0". OPENSSL_EXPORT ASN1_OCTET_STRING *a2i_IPADDRESS_NC(const char *ipasc); @@ -5039,76 +5076,76 @@ OPENSSL_EXPORT ASN1_OCTET_STRING *a2i_IPADDRESS_NC(const char *ipasc); // Deprecated functions. -// X509_get_notBefore returns |x509|'s notBefore time. Note this function is not -// const-correct for legacy reasons. Use |X509_get0_notBefore| or -// |X509_getm_notBefore| instead. +// X509_get_notBefore returns `x509`'s notBefore time. Note this function is not +// const-correct for legacy reasons. Use `X509_get0_notBefore` or +// `X509_getm_notBefore` instead. OPENSSL_EXPORT ASN1_TIME *X509_get_notBefore(const X509 *x509); -// X509_get_notAfter returns |x509|'s notAfter time. Note this function is not -// const-correct for legacy reasons. Use |X509_get0_notAfter| or -// |X509_getm_notAfter| instead. +// X509_get_notAfter returns `x509`'s notAfter time. Note this function is not +// const-correct for legacy reasons. Use `X509_get0_notAfter` or +// `X509_getm_notAfter` instead. OPENSSL_EXPORT ASN1_TIME *X509_get_notAfter(const X509 *x509); -// X509_set_notBefore calls |X509_set1_notBefore|. Use |X509_set1_notBefore| +// X509_set_notBefore calls `X509_set1_notBefore`. Use `X509_set1_notBefore` // instead. OPENSSL_EXPORT int X509_set_notBefore(X509 *x509, const ASN1_TIME *tm); -// X509_set_notAfter calls |X509_set1_notAfter|. Use |X509_set1_notAfter| +// X509_set_notAfter calls `X509_set1_notAfter`. Use `X509_set1_notAfter` // instead. OPENSSL_EXPORT int X509_set_notAfter(X509 *x509, const ASN1_TIME *tm); -// X509_CRL_get_lastUpdate returns a mutable pointer to |crl|'s thisUpdate time. +// X509_CRL_get_lastUpdate returns a mutable pointer to `crl`'s thisUpdate time. // The OpenSSL API refers to this field as lastUpdate. // -// Use |X509_CRL_get0_lastUpdate| or |X509_CRL_set1_lastUpdate| instead. +// Use `X509_CRL_get0_lastUpdate` or `X509_CRL_set1_lastUpdate` instead. OPENSSL_EXPORT ASN1_TIME *X509_CRL_get_lastUpdate(X509_CRL *crl); -// X509_CRL_get_nextUpdate returns a mutable pointer to |crl|'s nextUpdate time, -// or NULL if |crl| has none. Use |X509_CRL_get0_nextUpdate| or -// |X509_CRL_set1_nextUpdate| instead. +// X509_CRL_get_nextUpdate returns a mutable pointer to `crl`'s nextUpdate time, +// or NULL if `crl` has none. Use `X509_CRL_get0_nextUpdate` or +// `X509_CRL_set1_nextUpdate` instead. OPENSSL_EXPORT ASN1_TIME *X509_CRL_get_nextUpdate(X509_CRL *crl); -// X509_extract_key is a legacy alias to |X509_get_pubkey|. Use -// |X509_get_pubkey| instead. +// X509_extract_key is a legacy alias to `X509_get_pubkey`. Use +// `X509_get_pubkey` instead. #define X509_extract_key(x) X509_get_pubkey(x) -// X509_REQ_extract_key is a legacy alias for |X509_REQ_get_pubkey|. +// X509_REQ_extract_key is a legacy alias for `X509_REQ_get_pubkey`. #define X509_REQ_extract_key(a) X509_REQ_get_pubkey(a) -// X509_name_cmp is a legacy alias for |X509_NAME_cmp|. +// X509_name_cmp is a legacy alias for `X509_NAME_cmp`. #define X509_name_cmp(a, b) X509_NAME_cmp((a), (b)) -// The following symbols are deprecated aliases to |X509_CRL_set1_*|. +// The following symbols are deprecated aliases to `X509_CRL_set1_*`. #define X509_CRL_set_lastUpdate X509_CRL_set1_lastUpdate #define X509_CRL_set_nextUpdate X509_CRL_set1_nextUpdate -// X509_get_serialNumber returns a mutable pointer to |x509|'s serial number. -// Prefer |X509_get0_serialNumber|. +// X509_get_serialNumber returns a mutable pointer to `x509`'s serial number. +// Prefer `X509_get0_serialNumber`. OPENSSL_EXPORT ASN1_INTEGER *X509_get_serialNumber(X509 *x509); -// X509_NAME_get_text_by_OBJ finds the first attribute with type |obj| in -// |name|. If found, it writes the value's UTF-8 representation to |buf|. +// X509_NAME_get_text_by_OBJ finds the first attribute with type `obj` in +// `name`. If found, it writes the value's UTF-8 representation to `buf`. // followed by a NUL byte, and returns the number of bytes in the output, // excluding the NUL byte. This is unlike OpenSSL which returns the raw -// ASN1_STRING data. The UTF-8 encoding of the |ASN1_STRING| may not contain a 0 +// ASN1_STRING data. The UTF-8 encoding of the `ASN1_STRING` may not contain a 0 // codepoint. // -// This function writes at most |len| bytes, including the NUL byte. If |buf| +// This function writes at most `len` bytes, including the NUL byte. If `buf` // is NULL, it writes nothing and returns the number of bytes in the // output, excluding the NUL byte that would be required for the full UTF-8 // output. // // This function may return -1 if an error occurs for any reason, including the -// value not being a recognized string type, |len| being of insufficient size to +// value not being a recognized string type, `len` being of insufficient size to // hold the full UTF-8 encoding and NUL byte, memory allocation failures, an -// object with type |obj| not existing in |name|, or if the UTF-8 encoding of +// object with type `obj` not existing in `name`, or if the UTF-8 encoding of // the string contains a zero byte. OPENSSL_EXPORT int X509_NAME_get_text_by_OBJ(const X509_NAME *name, const ASN1_OBJECT *obj, char *buf, int len); -// X509_NAME_get_text_by_NID behaves like |X509_NAME_get_text_by_OBJ| except it -// finds an attribute of type |nid|, which should be one of the |NID_*| +// X509_NAME_get_text_by_NID behaves like `X509_NAME_get_text_by_OBJ` except it +// finds an attribute of type `nid`, which should be one of the `NID_*` // constants. OPENSSL_EXPORT int X509_NAME_get_text_by_NID(const X509_NAME *name, int nid, char *buf, int len); @@ -5117,56 +5154,56 @@ OPENSSL_EXPORT int X509_NAME_get_text_by_NID(const X509_NAME *name, int nid, OPENSSL_EXPORT X509_STORE_CTX *X509_STORE_CTX_get0_parent_ctx( const X509_STORE_CTX *ctx); -// X509_OBJECT_free_contents sets |obj| to the empty object, freeing any values +// X509_OBJECT_free_contents sets `obj` to the empty object, freeing any values // that were previously there. // // TODO(davidben): Unexport this function after rust-openssl is fixed to no // longer call it. OPENSSL_EXPORT void X509_OBJECT_free_contents(X509_OBJECT *obj); -// X509_LOOKUP_free releases memory associated with |ctx|. This function should +// X509_LOOKUP_free releases memory associated with `ctx`. This function should // never be used outside the library. No function in the public API hands -// ownership of an |X509_LOOKUP| to the caller. +// ownership of an `X509_LOOKUP` to the caller. // // TODO(davidben): Unexport this function after rust-openssl is fixed to no // longer call it. OPENSSL_EXPORT void X509_LOOKUP_free(X509_LOOKUP *ctx); -// X509_STORE_CTX_cleanup resets |ctx| to the empty state. +// X509_STORE_CTX_cleanup resets `ctx` to the empty state. // -// This function is a remnant of when |X509_STORE_CTX| was stack-allocated and -// should not be used. If releasing |ctx|, call |X509_STORE_CTX_free|. If -// reusing |ctx| for a new verification, release the old one and create a new +// This function is a remnant of when `X509_STORE_CTX` was stack-allocated and +// should not be used. If releasing `ctx`, call `X509_STORE_CTX_free`. If +// reusing `ctx` for a new verification, release the old one and create a new // one. OPENSSL_EXPORT void X509_STORE_CTX_cleanup(X509_STORE_CTX *ctx); // X509V3_add_standard_extensions returns one. OPENSSL_EXPORT int X509V3_add_standard_extensions(void); -// The following symbols are legacy aliases for |X509_STORE_CTX| functions. +// The following symbols are legacy aliases for `X509_STORE_CTX` functions. #define X509_STORE_get_by_subject X509_STORE_CTX_get_by_subject #define X509_STORE_get1_certs X509_STORE_CTX_get1_certs #define X509_STORE_get1_crls X509_STORE_CTX_get1_crls -// X509_STORE_CTX_get_chain is a legacy alias for |X509_STORE_CTX_get0_chain|. +// X509_STORE_CTX_get_chain is a legacy alias for `X509_STORE_CTX_get0_chain`. OPENSSL_EXPORT STACK_OF(X509) *X509_STORE_CTX_get_chain( const X509_STORE_CTX *ctx); // X509_STORE_CTX_trusted_stack is a deprecated alias for -// |X509_STORE_CTX_set0_trusted_stack|. +// `X509_STORE_CTX_set0_trusted_stack`. OPENSSL_EXPORT void X509_STORE_CTX_trusted_stack(X509_STORE_CTX *ctx, STACK_OF(X509) *sk); typedef int (*X509_STORE_CTX_verify_cb)(int, X509_STORE_CTX *); -// X509_STORE_CTX_set_verify_cb configures a callback function for |ctx| that is -// called multiple times during |X509_verify_cert|. The callback returns zero to -// fail verification and one to proceed. Typically, it will return |ok|, which -// preserves the default behavior. Returning one when |ok| is zero will proceed -// past some error. The callback may inspect |ctx| and the error queue to +// X509_STORE_CTX_set_verify_cb configures a callback function for `ctx` that is +// called multiple times during `X509_verify_cert`. The callback returns zero to +// fail verification and one to proceed. Typically, it will return `ok`, which +// preserves the default behavior. Returning one when `ok` is zero will proceed +// past some error. The callback may inspect `ctx` and the error queue to // attempt to determine the current stage of certificate verification, but this // is often unreliable. When synthesizing an error, callbacks should use -// |X509_STORE_CTX_set_error| to set a corresponding error. +// `X509_STORE_CTX_set_error` to set a corresponding error. // // WARNING: Do not use this function. It is extremely fragile and unpredictable. // This callback exposes implementation details of certificate verification, @@ -5174,36 +5211,36 @@ typedef int (*X509_STORE_CTX_verify_cb)(int, X509_STORE_CTX *); // can introduce vulnerabilities if making incorrect assumptions about when the // callback is called. Some errors, when suppressed, may implicitly suppress // other errors due to internal implementation details. Additionally, overriding -// |ok| may leave |ctx| in an inconsistent state and break invariants. +// `ok` may leave `ctx` in an inconsistent state and break invariants. // // Instead, customize certificate verification by configuring options on the -// |X509_STORE_CTX| before verification, or applying additional checks after -// |X509_verify_cert| completes successfully. +// `X509_STORE_CTX` before verification, or applying additional checks after +// `X509_verify_cert` completes successfully. OPENSSL_EXPORT void X509_STORE_CTX_set_verify_cb( X509_STORE_CTX *ctx, int (*verify_cb)(int ok, X509_STORE_CTX *ctx)); -// X509_STORE_set_verify_cb acts like |X509_STORE_CTX_set_verify_cb| but sets -// the verify callback for any |X509_STORE_CTX| created from this |X509_STORE| +// X509_STORE_set_verify_cb acts like `X509_STORE_CTX_set_verify_cb` but sets +// the verify callback for any `X509_STORE_CTX` created from this `X509_STORE` // -// Do not use this function. See |X509_STORE_CTX_set_verify_cb| for details. +// Do not use this function. See `X509_STORE_CTX_set_verify_cb` for details. OPENSSL_EXPORT void X509_STORE_set_verify_cb( X509_STORE *store, X509_STORE_CTX_verify_cb verify_cb); // X509_STORE_set_verify_cb_func is a deprecated alias for -// |X509_STORE_set_verify_cb|. +// `X509_STORE_set_verify_cb`. #define X509_STORE_set_verify_cb_func(store, func) \ X509_STORE_set_verify_cb((store), (func)) -// X509_STORE_CTX_set_chain configures |ctx| to use |sk| for untrusted +// X509_STORE_CTX_set_chain configures `ctx` to use `sk` for untrusted // intermediate certificates to use in verification. This function is redundant -// with the |chain| parameter of |X509_STORE_CTX_init|. Use the parameter +// with the `chain` parameter of `X509_STORE_CTX_init`. Use the parameter // instead. // // WARNING: Despite the similar name, this function is unrelated to -// |X509_STORE_CTX_get0_chain|. +// `X509_STORE_CTX_get0_chain`. // -// WARNING: This function saves a pointer to |sk| without copying or -// incrementing reference counts. |sk| must outlive |ctx| and may not be mutated +// WARNING: This function saves a pointer to `sk` without copying or +// incrementing reference counts. `sk` must outlive `ctx` and may not be mutated // for the duration of the certificate verification. OPENSSL_EXPORT void X509_STORE_CTX_set_chain(X509_STORE_CTX *ctx, STACK_OF(X509) *sk); @@ -5219,43 +5256,43 @@ OPENSSL_EXPORT void X509_STORE_CTX_set_chain(X509_STORE_CTX *ctx, // always enabled. #define X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS 0 -// X509_STORE_get0_objects returns a non-owning pointer of |store|'s internal +// X509_STORE_get0_objects returns a non-owning pointer of `store`'s internal // object list. Although this function is not const, callers must not modify // the result of this function. // -// WARNING: This function is not thread-safe. If |store| is shared across +// WARNING: This function is not thread-safe. If `store` is shared across // multiple threads, callers cannot safely inspect the result of this function, // because another thread may have concurrently added to it. In particular, -// |X509_LOOKUP_add_dir| treats this list as a cache and may add to it in the +// `X509_LOOKUP_add_dir` treats this list as a cache and may add to it in the // course of certificate verification. This API additionally prevents fixing -// some quadratic worst-case behavior in |X509_STORE| and may be removed in the -// future. Use |X509_STORE_get1_objects| instead. +// some quadratic worst-case behavior in `X509_STORE` and may be removed in the +// future. Use `X509_STORE_get1_objects` instead. OPENSSL_EXPORT STACK_OF(X509_OBJECT) *X509_STORE_get0_objects( X509_STORE *store); -// X509_PURPOSE_get_by_sname returns the |X509_PURPOSE_*| constant corresponding -// a short name |sname|, or -1 if |sname| was not recognized. +// X509_PURPOSE_get_by_sname returns the `X509_PURPOSE_*` constant corresponding +// a short name `sname`, or -1 if `sname` was not recognized. // -// Use |X509_PURPOSE_*| constants directly instead. The short names used by this +// Use `X509_PURPOSE_*` constants directly instead. The short names used by this // function look like "sslserver" or "smimeencrypt", so they do not make // especially good APIs. // // This function differs from OpenSSL, which returns an "index" to be passed to -// |X509_PURPOSE_get0|, followed by |X509_PURPOSE_get_id|, to finally obtain an -// |X509_PURPOSE_*| value suitable for use with |X509_VERIFY_PARAM_set_purpose|. +// `X509_PURPOSE_get0`, followed by `X509_PURPOSE_get_id`, to finally obtain an +// `X509_PURPOSE_*` value suitable for use with `X509_VERIFY_PARAM_set_purpose`. OPENSSL_EXPORT int X509_PURPOSE_get_by_sname(const char *sname); -// X509_PURPOSE_get0 returns the |X509_PURPOSE| object corresponding to |id|, -// which should be one of the |X509_PURPOSE_*| constants, or NULL if none +// X509_PURPOSE_get0 returns the `X509_PURPOSE` object corresponding to `id`, +// which should be one of the `X509_PURPOSE_*` constants, or NULL if none // exists. // // This function differs from OpenSSL, which takes an "index", returned from -// |X509_PURPOSE_get_by_sname|. In BoringSSL, indices and |X509_PURPOSE_*| IDs +// `X509_PURPOSE_get_by_sname`. In BoringSSL, indices and `X509_PURPOSE_*` IDs // are the same. OPENSSL_EXPORT const X509_PURPOSE *X509_PURPOSE_get0(int id); -// X509_PURPOSE_get_id returns |purpose|'s ID. This will be one of the -// |X509_PURPOSE_*| constants. +// X509_PURPOSE_get_id returns `purpose`'s ID. This will be one of the +// `X509_PURPOSE_*` constants. OPENSSL_EXPORT int X509_PURPOSE_get_id(const X509_PURPOSE *purpose); // The following constants are values for the legacy Netscape certificate type @@ -5299,20 +5336,25 @@ BORINGSSL_MAKE_DELETER(BASIC_CONSTRAINTS, BASIC_CONSTRAINTS_free) // TODO(davidben): Move this to conf.h and rename to CONF_VALUE_free. BORINGSSL_MAKE_DELETER(CONF_VALUE, X509V3_conf_free) BORINGSSL_MAKE_DELETER(DIST_POINT, DIST_POINT_free) +BORINGSSL_MAKE_DELETER(DIST_POINT_NAME, DIST_POINT_NAME_free) BORINGSSL_MAKE_DELETER(GENERAL_NAME, GENERAL_NAME_free) BORINGSSL_MAKE_DELETER(GENERAL_SUBTREE, GENERAL_SUBTREE_free) +BORINGSSL_MAKE_DELETER(ISSUING_DIST_POINT, ISSUING_DIST_POINT_free) BORINGSSL_MAKE_DELETER(NAME_CONSTRAINTS, NAME_CONSTRAINTS_free) BORINGSSL_MAKE_DELETER(NETSCAPE_SPKI, NETSCAPE_SPKI_free) +BORINGSSL_MAKE_DELETER(NOTICEREF, NOTICEREF_free) BORINGSSL_MAKE_DELETER(POLICY_CONSTRAINTS, POLICY_CONSTRAINTS_free) BORINGSSL_MAKE_DELETER(POLICY_MAPPING, POLICY_MAPPING_free) BORINGSSL_MAKE_DELETER(POLICYINFO, POLICYINFO_free) +BORINGSSL_MAKE_DELETER(POLICYQUALINFO, POLICYQUALINFO_free) BORINGSSL_MAKE_DELETER(RSA_PSS_PARAMS, RSA_PSS_PARAMS_free) +BORINGSSL_MAKE_DELETER(USERNOTICE, USERNOTICE_free) BORINGSSL_MAKE_DELETER(X509, X509_free) -BORINGSSL_MAKE_UP_REF(X509, X509_up_ref) +BORINGSSL_MAKE_UP_REF(X509, X509_dup_ref) BORINGSSL_MAKE_DELETER(X509_ALGOR, X509_ALGOR_free) BORINGSSL_MAKE_DELETER(X509_ATTRIBUTE, X509_ATTRIBUTE_free) BORINGSSL_MAKE_DELETER(X509_CRL, X509_CRL_free) -BORINGSSL_MAKE_UP_REF(X509_CRL, X509_CRL_up_ref) +BORINGSSL_MAKE_UP_REF(X509_CRL, X509_CRL_dup_ref) BORINGSSL_MAKE_DELETER(X509_EXTENSION, X509_EXTENSION_free) BORINGSSL_MAKE_DELETER(X509_INFO, X509_INFO_free) BORINGSSL_MAKE_DELETER(X509_LOOKUP, X509_LOOKUP_free) @@ -5324,7 +5366,7 @@ BORINGSSL_MAKE_DELETER(X509_REQ, X509_REQ_free) BORINGSSL_MAKE_DELETER(X509_REVOKED, X509_REVOKED_free) BORINGSSL_MAKE_DELETER(X509_SIG, X509_SIG_free) BORINGSSL_MAKE_DELETER(X509_STORE, X509_STORE_free) -BORINGSSL_MAKE_UP_REF(X509_STORE, X509_STORE_up_ref) +BORINGSSL_MAKE_UP_REF(X509_STORE, X509_STORE_dup_ref) BORINGSSL_MAKE_DELETER(X509_STORE_CTX, X509_STORE_CTX_free) BORINGSSL_MAKE_DELETER(X509_VERIFY_PARAM, X509_VERIFY_PARAM_free) @@ -5378,5 +5420,7 @@ BSSL_NAMESPACE_END #define X509_R_NO_CERTIFICATE_OR_CRL_FOUND 142 #define X509_R_NO_CRL_FOUND 143 #define X509_R_INVALID_POLICY_EXTENSION 144 +#define X509_R_INVALID_MTC_CA 145 +#define X509_R_INVALID_MTC_PROOF 146 #endif // OPENSSL_HEADER_X509_H diff --git a/third_party/boringssl/src/include/openssl/x509v3.h b/third_party/boringssl/src/include/openssl/x509v3.h index 3e8a7bebd..838e855c4 100644 --- a/third_party/boringssl/src/include/openssl/x509v3.h +++ b/third_party/boringssl/src/include/openssl/x509v3.h @@ -46,7 +46,7 @@ // Deprecated constants. -// The following constants are legacy aliases for |X509v3_KU_*|. They are +// The following constants are legacy aliases for `X509v3_KU_*`. They are // defined here instead of in because NSS's public headers use // the same symbols. Some callers have inadvertently relied on the conflicts // only being defined in this header. diff --git a/third_party/boringssl/src/include/openssl/xwing.h b/third_party/boringssl/src/include/openssl/xwing.h index a270637a3..59f884ba3 100644 --- a/third_party/boringssl/src/include/openssl/xwing.h +++ b/third_party/boringssl/src/include/openssl/xwing.h @@ -54,29 +54,29 @@ struct XWING_private_key { // XWING_generate_key generates a random public/private key pair, writes the -// encoded public key to |out_encoded_public_key| and the private key to -// |out_private_key|. Returns one on success and zero on error. +// encoded public key to `out_encoded_public_key` and the private key to +// `out_private_key`. Returns one on success and zero on error. OPENSSL_EXPORT int XWING_generate_key( uint8_t out_encoded_public_key[XWING_PUBLIC_KEY_BYTES], struct XWING_private_key *out_private_key); -// XWING_public_from_private sets |out_encoded_public_key| to the public key -// that corresponds to |private_key|. Returns one on success and zero on error. +// XWING_public_from_private sets `out_encoded_public_key` to the public key +// that corresponds to `private_key`. Returns one on success and zero on error. OPENSSL_EXPORT int XWING_public_from_private( uint8_t out_encoded_public_key[XWING_PUBLIC_KEY_BYTES], const struct XWING_private_key *private_key); -// XWING_encap encapsulates a random shared secret for |encoded_public_key|, -// writes the ciphertext to |out_ciphertext|, and writes the random shared -// secret to |out_shared_secret|. Returns one on success and zero on error. +// XWING_encap encapsulates a random shared secret for `encoded_public_key`, +// writes the ciphertext to `out_ciphertext`, and writes the random shared +// secret to `out_shared_secret`. Returns one on success and zero on error. OPENSSL_EXPORT int XWING_encap( uint8_t out_ciphertext[XWING_CIPHERTEXT_BYTES], uint8_t out_shared_secret[XWING_SHARED_SECRET_BYTES], const uint8_t encoded_public_key[XWING_PUBLIC_KEY_BYTES]); // XWING_encap_external_entropy encapsulates the shared secret for the given -// |eseed| entropy using |encoded_public_key|, writes the ciphertext to -// |out_ciphertext|, and writes the random shared secret to |out_shared_secret|. +// `eseed` entropy using `encoded_public_key`, writes the ciphertext to +// `out_ciphertext`, and writes the random shared secret to `out_shared_secret`. // Returns one on success and zero on error. OPENSSL_EXPORT int XWING_encap_external_entropy( uint8_t out_ciphertext[XWING_CIPHERTEXT_BYTES], @@ -84,8 +84,8 @@ OPENSSL_EXPORT int XWING_encap_external_entropy( const uint8_t encoded_public_key[XWING_PUBLIC_KEY_BYTES], const uint8_t eseed[64]); -// XWING_decap decapsulates a shared secret from |ciphertext| using -// |private_key| and writes it to |out_shared_secret|. Returns one on success +// XWING_decap decapsulates a shared secret from `ciphertext` using +// `private_key` and writes it to `out_shared_secret`. Returns one on success // and zero on error. OPENSSL_EXPORT int XWING_decap( uint8_t out_shared_secret[XWING_SHARED_SECRET_BYTES], @@ -94,16 +94,16 @@ OPENSSL_EXPORT int XWING_decap( // Serialisation of keys. -// XWING_marshal_private_key serializes |private_key| to |out| in the standard +// XWING_marshal_private_key serializes `private_key` to `out` in the standard // format for X-Wing private keys. It returns one on success or zero on // allocation error. OPENSSL_EXPORT int XWING_marshal_private_key( CBB *out, const struct XWING_private_key *private_key); // XWING_parse_private_key parses a private key in the standard format for -// X-Wing private keys from |in| and writes the result to |out_public_key|. It +// X-Wing private keys from `in` and writes the result to `out_public_key`. It // returns one on success or zero on parse error or if there are trailing bytes -// in |in|. +// in `in`. OPENSSL_EXPORT int XWING_parse_private_key( struct XWING_private_key *out_private_key, CBS *in); diff --git a/third_party/boringssl/src/third_party/fiat/bedrock_unverified_platform.c.inc b/third_party/boringssl/src/third_party/fiat/bedrock_unverified_platform.c.inc index d2b03c1fa..cb9490a16 100644 --- a/third_party/boringssl/src/third_party/fiat/bedrock_unverified_platform.c.inc +++ b/third_party/boringssl/src/third_party/fiat/bedrock_unverified_platform.c.inc @@ -118,6 +118,16 @@ static inline br_word_t br_cmov(br_word_t c, br_word_t vnz, br_word_t vz) { : [vz] "r"(vz), [c] "r"(c) : "cc"); return vnz; +#elif !defined(OPENSSL_NO_ASM) && (defined(__GNUC__) || defined(__clang__)) && \ + defined(__aarch64__) + br_word_t ret; + __asm__( + "cmp %[c], #0\n" + "csel %[ret], %[vz], %[vnz], EQ" + : [ret] "=r"(ret) + : [vz] "r"(vz), [vnz] "r"(vnz), [c] "r"(c) + : "cc"); + return ret; #else br_word_t m = br_broadcast_nonzero(c); return (m & vnz) | (~m & vz); diff --git a/third_party/boringssl/src/third_party/fiat/p256_64.h b/third_party/boringssl/src/third_party/fiat/p256_64.h index 8cad1695a..31f317ba1 100644 --- a/third_party/boringssl/src/third_party/fiat/p256_64.h +++ b/third_party/boringssl/src/third_party/fiat/p256_64.h @@ -3,7 +3,15 @@ #include "p256_field_64.br.c.inc" #include "../../crypto/internal.h" -#if !defined(OPENSSL_NO_ASM) && defined(__GNUC__) && defined(__x86_64__) +#if !defined(OPENSSL_NO_ASM) && (defined(__ELF__) || defined(__APPLE__)) && \ + defined(OPENSSL_X86_64) && !defined(OPENSSL_NANOLIBC) +// These functions are only available with gas and SysV ABI, used by Apple and +// ELF-based platforms. Unlike most of our SysV assembly, they currently rely on +// the SysV redzone. This trips one target which looks like it targets SysV but +// has no redzone. This happens to define `OPENSSL_NANOLIBC`, so gate on that. +// +// TODO(crbug.com/522255483): Come up with a clearer story for the redzone +// situation. extern "C" { void fiat_p256_adx_mul(uint64_t*, const uint64_t*, const uint64_t*); void fiat_p256_adx_sqr(uint64_t*, const uint64_t*); @@ -178,7 +186,8 @@ static FIAT_P256_FIAT_INLINE void fiat_p256_cmovznz_u64(uint64_t* out1, fiat_p25 */ static FIAT_P256_FIAT_INLINE void fiat_p256_mul(fiat_p256_montgomery_domain_field_element out1, const fiat_p256_montgomery_domain_field_element arg1, const fiat_p256_montgomery_domain_field_element arg2) { // NOTE: edited by hand, see third_party/fiat/README.md -#if !defined(OPENSSL_NO_ASM) && defined(__GNUC__) && defined(__x86_64__) +#if !defined(OPENSSL_NO_ASM) && (defined(__ELF__) || defined(__APPLE__)) && \ + defined(OPENSSL_X86_64) && !defined(OPENSSL_NANOLIBC) if (bssl::CRYPTO_is_BMI1_capable() && bssl::CRYPTO_is_BMI2_capable() && bssl::CRYPTO_is_ADX_capable()) { fiat_p256_adx_mul(out1, arg1, arg2); @@ -493,7 +502,8 @@ static FIAT_P256_FIAT_INLINE void fiat_p256_mul(fiat_p256_montgomery_domain_fiel */ static FIAT_P256_FIAT_INLINE void fiat_p256_square(fiat_p256_montgomery_domain_field_element out1, const fiat_p256_montgomery_domain_field_element arg1) { // NOTE: edited by hand, see third_party/fiat/README.md -#if !defined(OPENSSL_NO_ASM) && defined(__GNUC__) && defined(__x86_64__) +#if !defined(OPENSSL_NO_ASM) && (defined(__ELF__) || defined(__APPLE__)) && \ + defined(OPENSSL_X86_64) && !defined(OPENSSL_NANOLIBC) if (bssl::CRYPTO_is_BMI1_capable() && bssl::CRYPTO_is_BMI2_capable() && bssl::CRYPTO_is_ADX_capable()) { fiat_p256_adx_sqr(out1, arg1); diff --git a/tool/REVISION b/tool/REVISION index 574301bad..fe466d083 100644 --- a/tool/REVISION +++ b/tool/REVISION @@ -1 +1 @@ -5ee9407bc28dd9086507f02851886a185088f3a0 +fd0ed4d0a1273570612a08a31bace08e4dd73c81 From de615b965dc066b3d5a3cdeccb3846813a91a286 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Wed, 4 Nov 2026 22:35:29 +0000 Subject: [PATCH 41/69] kiln: manifest of release native-e8a7f4f332215a48 --- native_artifacts/prebuilt.json | 114 ++++++++++++++++----------------- 1 file changed, 57 insertions(+), 57 deletions(-) diff --git a/native_artifacts/prebuilt.json b/native_artifacts/prebuilt.json index 838d32889..57ecdbb8f 100644 --- a/native_artifacts/prebuilt.json +++ b/native_artifacts/prebuilt.json @@ -1,179 +1,179 @@ { "schema": 1, - "sourceKey": "f946ab06ef46444f2b2e106273335c4caa32e3c5ef300a3943b25e55e8962536", - "release": "https://github.com/Telosnex/webcrypto.dart/releases/tag/native-f946ab06ef46444f", + "sourceKey": "e8a7f4f332215a4878e2a282418abb2493d4c21228f96e51ae2b889a2ddfe166", + "release": "https://github.com/Telosnex/webcrypto.dart/releases/tag/native-e8a7f4f332215a48", "targets": { "android-arm": { - "runner": "ubuntu22 20261012.322.1", + "runner": "ubuntu22 20261101.347.1", "toolchain": "Android NDK 28.2.13676358, cmake version 3.31.6", "minOSVersion": 24, "files": [ { "name": "libwebcrypto.so", - "sha256": "ca311dda5aaac1e313861d37cfeba6437deebf0796530e3379480996587d79e5", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-f946ab06ef46444f/android-arm-libwebcrypto.so.gz", - "downloadSha256": "b97a7edbe31893adef3a0d721cd36baa3c258ea270dd594a7f786faa762c404c", + "sha256": "9bfecc0bf98a8e1ca721478885b1d0cc1d14b462373e368da7055bdb95572227", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-e8a7f4f332215a48/android-arm-libwebcrypto.so.gz", + "downloadSha256": "08dfd9eb39080389e35a227c3bdd9a178a983bf5be4ddf619c613988c7c18f6f", "delivery": "bundle", "asset": "webcrypto.dart" } ] }, "android-arm64": { - "runner": "ubuntu22 20261012.322.1", + "runner": "ubuntu22 20261101.347.1", "toolchain": "Android NDK 28.2.13676358, cmake version 3.31.6", "minOSVersion": 24, "files": [ { "name": "libwebcrypto.so", - "sha256": "68dbb5a39f8abb6612276489030883193a7f5f988de9d4556c93458fdc69f78f", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-f946ab06ef46444f/android-arm64-libwebcrypto.so.gz", - "downloadSha256": "2f72d84faf5b23af07bc795f6fa41f3e215d6fd2fe5dea61c574b48a1b1507fe", + "sha256": "3fe780e97de654c44457388a69a5248d3738c7c0800f9bb480996dad5c5a9fae", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-e8a7f4f332215a48/android-arm64-libwebcrypto.so.gz", + "downloadSha256": "9655b314df68b845c5c470052b025051525e242674678ff11e03890abebde40b", "delivery": "bundle", "asset": "webcrypto.dart" } ] }, "android-x64": { - "runner": "ubuntu22 20261012.322.1", + "runner": "ubuntu22 20261101.347.1", "toolchain": "Android NDK 28.2.13676358, cmake version 3.31.6", "minOSVersion": 24, "files": [ { "name": "libwebcrypto.so", - "sha256": "a90b4d6593f2cc7b2b84a9955f10c0dec90a9d787c0032b4ffd5bbbb2ff09f60", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-f946ab06ef46444f/android-x64-libwebcrypto.so.gz", - "downloadSha256": "35b3f4d30ce02d107d31b067dc156d04b11a23e83c9355a41b623edfe8677c1c", + "sha256": "a3e3c26c89dcc1e5f2a282ae7dc8252681e4abbd5f3009f17451fb8d2bcdafc1", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-e8a7f4f332215a48/android-x64-libwebcrypto.so.gz", + "downloadSha256": "2d955cdb7b8f06ab47c13854ae6011465c50ddc945e8f5b41983d7b6f3a81a43", "delivery": "bundle", "asset": "webcrypto.dart" } ] }, "ios-arm64-iphoneos": { - "runner": "macos15 20260907.0337.1", - "toolchain": "Xcode 16.4, cmake version 4.4.3", + "runner": "macos15 20261028.0489.1", + "toolchain": "Xcode 16.4, cmake version 4.4.4", "minOSVersion": 13, "files": [ { "name": "libwebcrypto.dylib", - "sha256": "0a7466fc185fcf577ed8c0e6da70f9ad33f596d0b28a487459e87db76a4e49d6", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-f946ab06ef46444f/ios-arm64-iphoneos-libwebcrypto.dylib.gz", - "downloadSha256": "9ea467950695bd526292a2b815236f3fd14cb8b0e7c00bdde0dbcb04e2770690", + "sha256": "a6d431ff20127a247a09838dbbce3bc6e1679e819cb42cb14ac4349db3c4ac8c", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-e8a7f4f332215a48/ios-arm64-iphoneos-libwebcrypto.dylib.gz", + "downloadSha256": "0b9581badbbb595cb07f24dcae3bce684d3121b17e1430bc5c3208b9d03a62d5", "delivery": "bundle", "asset": "webcrypto.dart" } ] }, "ios-arm64-iphonesimulator": { - "runner": "macos15 20260907.0337.1", - "toolchain": "Xcode 16.4, cmake version 4.4.3", + "runner": "macos15 20261028.0489.1", + "toolchain": "Xcode 16.4, cmake version 4.4.4", "minOSVersion": 13, "files": [ { "name": "libwebcrypto.dylib", - "sha256": "3393b602567febb40fdc5b9b54943b8325aee02656d6fcb93bfce80fd3395861", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-f946ab06ef46444f/ios-arm64-iphonesimulator-libwebcrypto.dylib.gz", - "downloadSha256": "8cc335c7277fc3f58a203ac899b5111c99d79ea49ba89f8715eca8ba7f00186f", + "sha256": "90a20819380cc4871187eb3256f54c9d880724dc0f587b1b72916eec2e074661", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-e8a7f4f332215a48/ios-arm64-iphonesimulator-libwebcrypto.dylib.gz", + "downloadSha256": "546095fb6c68b2d81e2c52d833e98340f61a3ecb7836ebf0b9f16e03404434b4", "delivery": "bundle", "asset": "webcrypto.dart" } ] }, "ios-x64-iphonesimulator": { - "runner": "macos15 20260907.0337.1", - "toolchain": "Xcode 16.4, cmake version 4.4.3", + "runner": "macos15 20261028.0489.1", + "toolchain": "Xcode 16.4, cmake version 4.4.4", "minOSVersion": 13, "files": [ { "name": "libwebcrypto.dylib", - "sha256": "084ca81cb6e9d67a2680db33209f4e7d0267917e1ea4e3851405b4099d094845", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-f946ab06ef46444f/ios-x64-iphonesimulator-libwebcrypto.dylib.gz", - "downloadSha256": "654b113782a02e7991c2113bb8fb5d1fc69926558e1f73ad9edcd7f236419e54", + "sha256": "4829448997180cd6ea174b7d236bf882f16dbc487f2e135454c8146afffe5833", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-e8a7f4f332215a48/ios-x64-iphonesimulator-libwebcrypto.dylib.gz", + "downloadSha256": "39d0666c37b9ec68b5685efc55d9bc42024a203d4e4bbafb7fa08a53845931d0", "delivery": "bundle", "asset": "webcrypto.dart" } ] }, "linux-arm64": { - "runner": "ubuntu22-arm64 20261023.156.3", + "runner": "ubuntu22-arm64 20261026.173.1", "toolchain": "gcc (Ubuntu 11.4.0-1ubuntu1~22.04.3) 11.4.0, cmake version 3.31.6", "files": [ { "name": "libwebcrypto.so", - "sha256": "b314cac1cd104679023513dd43d6e3a2a7c654ee1af84006b9ab4df81324ab9d", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-f946ab06ef46444f/linux-arm64-libwebcrypto.so.gz", - "downloadSha256": "acba41b1f7b4042d993e65030d82eff83679e67dcfb7c36a202c341cf45f1625", + "sha256": "65db805f157adabdf83095915dd7e317b3479e1148d6941d9c1b3c28ad568404", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-e8a7f4f332215a48/linux-arm64-libwebcrypto.so.gz", + "downloadSha256": "a21a12811fdcfcdaa2265e8abed00934ca02787ee7e0a54f15ba9077ecac9dec", "delivery": "bundle", "asset": "webcrypto.dart" } ] }, "linux-x64": { - "runner": "ubuntu22 20261023.322.3", + "runner": "ubuntu22 20261101.347.1", "toolchain": "gcc (Ubuntu 11.4.0-1ubuntu1~22.04.3) 11.4.0, cmake version 3.31.6", "files": [ { "name": "libwebcrypto.so", - "sha256": "b1b2eb86748511627ba6452f3ed8b70e3738cbe057bd49716aac79817532d10a", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-f946ab06ef46444f/linux-x64-libwebcrypto.so.gz", - "downloadSha256": "0c37b4b10b86b2eed622ffc5069f037e3f59b8ec8c8fe71371001e77a71e3c57", + "sha256": "6e25a4ad41d21971e8a223e95674a97fc8a3e4a212ba0f011d82d711346c5818", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-e8a7f4f332215a48/linux-x64-libwebcrypto.so.gz", + "downloadSha256": "5e06e65b8b3e8da741989ae77ca8994f5bd105394f32044c8b292add46993ca7", "delivery": "bundle", "asset": "webcrypto.dart" } ] }, "macos-arm64": { - "runner": "macos15 20260907.0337.1", - "toolchain": "Xcode 16.4, cmake version 4.4.3", + "runner": "macos15 20261028.0489.1", + "toolchain": "Xcode 16.4, cmake version 4.4.4", "minOSVersion": 12, "files": [ { "name": "libwebcrypto.dylib", - "sha256": "66a5a2366db930b2865132477b7de1b284926b644fff55ccfe648480c75d3f94", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-f946ab06ef46444f/macos-arm64-libwebcrypto.dylib.gz", - "downloadSha256": "ae0b8038ada762c26a0f95b62e5e6fdc8a87ab3f5adba0aa2ad9b2db276dc787", + "sha256": "89d60e9dac9a0850e064cc7c935f1ff4568ec9a984a3fe60a3e4363429569d5f", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-e8a7f4f332215a48/macos-arm64-libwebcrypto.dylib.gz", + "downloadSha256": "ee3e95c18cf000f705da601d43b8e7d78b6ae1288794c949a5654273c1566ae8", "delivery": "bundle", "asset": "webcrypto.dart" } ] }, "macos-x64": { - "runner": "macos15 20260907.0337.1", - "toolchain": "Xcode 16.4, cmake version 4.4.3", + "runner": "macos15 20261028.0489.1", + "toolchain": "Xcode 16.4, cmake version 4.4.4", "minOSVersion": 12, "files": [ { "name": "libwebcrypto.dylib", - "sha256": "8e12dd585a5ea205f387399e04a0207fbe47a1b5fc7dab7b774de76c7e4dd0c6", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-f946ab06ef46444f/macos-x64-libwebcrypto.dylib.gz", - "downloadSha256": "5844d45cc9342043ee90bedd226380e8da524102be12ea2702b57cf16a79dccf", + "sha256": "f574c753d80a21f3e1938a813cf4d18bd9968909ed2462648fecb1c69d4c7c42", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-e8a7f4f332215a48/macos-x64-libwebcrypto.dylib.gz", + "downloadSha256": "666e0a346d13fac9341395353e3d2c10ddff1f4ca144c0b63cefcd37c9da42a4", "delivery": "bundle", "asset": "webcrypto.dart" } ] }, "windows-arm64": { - "runner": "win11-vs2026-arm64 20261022.194.1", - "toolchain": "Visual Studio 18.11.1, cmake version 4.4.4, NASM 2.16.03", + "runner": "win11-vs2026-arm64 20261101.203.1", + "toolchain": "Visual Studio 18.11.3, cmake version 4.4.5, NASM 2.16.03", "files": [ { "name": "webcrypto.dll", - "sha256": "93cf9310215254f8d64a414190f4008922ceb9875568b549155f7f5ce49772e3", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-f946ab06ef46444f/windows-arm64-webcrypto.dll.gz", - "downloadSha256": "2c3a51dc16804297196ce461d41e729901075283239ac34be5ca4be699ac0112", + "sha256": "081f5078921753fb526485590187f8b4ad8d819a022da48c20ddc89288d141b3", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-e8a7f4f332215a48/windows-arm64-webcrypto.dll.gz", + "downloadSha256": "343ce56995f026233cce864900f2633c6f40c47c2cc59208935c03de11628bd6", "delivery": "bundle", "asset": "webcrypto.dart" } ] }, "windows-x64": { - "runner": "win22 20261022.344.1", - "toolchain": "Visual Studio 17.14.42, cmake version 3.31.6, NASM 2.16.03", + "runner": "win22 20261026.347.1", + "toolchain": "Visual Studio 17.14.43, cmake version 3.31.6, NASM 2.16.03", "files": [ { "name": "webcrypto.dll", - "sha256": "26a1728105e7a44236da83f57eb6186767e4152716a3f7398db9417cfdc4ae16", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-f946ab06ef46444f/windows-x64-webcrypto.dll.gz", - "downloadSha256": "e268fcce5b0f6ad0e96cca0fb18cd919a52cee27c6d11031ecb156fd5ec28f17", + "sha256": "8fdf864a5a6dd548dd43d3e1e0734b5cf822a1f759590c5f276ceb23a4aead0a", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-e8a7f4f332215a48/windows-x64-webcrypto.dll.gz", + "downloadSha256": "aa3d1603fb0389bf40168037656e93a870571877c4c826b9ab31be883a1024d6", "delivery": "bundle", "asset": "webcrypto.dart" } From 80a1d66906851e7c5064ddcd6f258168d24e2ee9 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Wed, 4 Nov 2026 23:22:29 +0000 Subject: [PATCH 42/69] kiln: manifest of release native-fdcb040a60a7f049 --- native_artifacts/prebuilt.json | 54 +++++++++++++++++----------------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/native_artifacts/prebuilt.json b/native_artifacts/prebuilt.json index 57ecdbb8f..23f2af544 100644 --- a/native_artifacts/prebuilt.json +++ b/native_artifacts/prebuilt.json @@ -1,48 +1,48 @@ { "schema": 1, - "sourceKey": "e8a7f4f332215a4878e2a282418abb2493d4c21228f96e51ae2b889a2ddfe166", - "release": "https://github.com/Telosnex/webcrypto.dart/releases/tag/native-e8a7f4f332215a48", + "sourceKey": "fdcb040a60a7f04932dc2266ba3f2b1b1642f3f2c288590ad20ea3096ed18fb7", + "release": "https://github.com/Telosnex/webcrypto.dart/releases/tag/native-fdcb040a60a7f049", "targets": { "android-arm": { - "runner": "ubuntu22 20261101.347.1", + "runner": "ubuntu22 20261026.341.1", "toolchain": "Android NDK 28.2.13676358, cmake version 3.31.6", "minOSVersion": 24, "files": [ { "name": "libwebcrypto.so", - "sha256": "9bfecc0bf98a8e1ca721478885b1d0cc1d14b462373e368da7055bdb95572227", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-e8a7f4f332215a48/android-arm-libwebcrypto.so.gz", - "downloadSha256": "08dfd9eb39080389e35a227c3bdd9a178a983bf5be4ddf619c613988c7c18f6f", + "sha256": "b6f1c5c0c05e17eadfae63f5480a45cca9211521ccf0fbe5fef407e6b9d00c03", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-fdcb040a60a7f049/android-arm-libwebcrypto.so.gz", + "downloadSha256": "d387dec0622b0f04c3b9c55ad50e5d2d39e0475b37a16336a0cf985544bf0432", "delivery": "bundle", "asset": "webcrypto.dart" } ] }, "android-arm64": { - "runner": "ubuntu22 20261101.347.1", + "runner": "ubuntu22 20261026.341.1", "toolchain": "Android NDK 28.2.13676358, cmake version 3.31.6", "minOSVersion": 24, "files": [ { "name": "libwebcrypto.so", - "sha256": "3fe780e97de654c44457388a69a5248d3738c7c0800f9bb480996dad5c5a9fae", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-e8a7f4f332215a48/android-arm64-libwebcrypto.so.gz", - "downloadSha256": "9655b314df68b845c5c470052b025051525e242674678ff11e03890abebde40b", + "sha256": "1c5a0d65dda7f9e2a82136ffee383ac1c8a4c82819f296e795c51f401897bd61", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-fdcb040a60a7f049/android-arm64-libwebcrypto.so.gz", + "downloadSha256": "77353669977c1907fed1c1eb0177535f0b1a7a00df0a91773a7ab69171807598", "delivery": "bundle", "asset": "webcrypto.dart" } ] }, "android-x64": { - "runner": "ubuntu22 20261101.347.1", + "runner": "ubuntu22 20261026.341.1", "toolchain": "Android NDK 28.2.13676358, cmake version 3.31.6", "minOSVersion": 24, "files": [ { "name": "libwebcrypto.so", - "sha256": "a3e3c26c89dcc1e5f2a282ae7dc8252681e4abbd5f3009f17451fb8d2bcdafc1", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-e8a7f4f332215a48/android-x64-libwebcrypto.so.gz", - "downloadSha256": "2d955cdb7b8f06ab47c13854ae6011465c50ddc945e8f5b41983d7b6f3a81a43", + "sha256": "6e60993bffb29877dfc19073752175f2857a5757f04c08db4637c789d75f3920", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-fdcb040a60a7f049/android-x64-libwebcrypto.so.gz", + "downloadSha256": "ea82b803d2b2efd27dcdf2b890afd508cfbb1c9efe6ab809817dd5f7c9a89d31", "delivery": "bundle", "asset": "webcrypto.dart" } @@ -56,7 +56,7 @@ { "name": "libwebcrypto.dylib", "sha256": "a6d431ff20127a247a09838dbbce3bc6e1679e819cb42cb14ac4349db3c4ac8c", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-e8a7f4f332215a48/ios-arm64-iphoneos-libwebcrypto.dylib.gz", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-fdcb040a60a7f049/ios-arm64-iphoneos-libwebcrypto.dylib.gz", "downloadSha256": "0b9581badbbb595cb07f24dcae3bce684d3121b17e1430bc5c3208b9d03a62d5", "delivery": "bundle", "asset": "webcrypto.dart" @@ -71,7 +71,7 @@ { "name": "libwebcrypto.dylib", "sha256": "90a20819380cc4871187eb3256f54c9d880724dc0f587b1b72916eec2e074661", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-e8a7f4f332215a48/ios-arm64-iphonesimulator-libwebcrypto.dylib.gz", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-fdcb040a60a7f049/ios-arm64-iphonesimulator-libwebcrypto.dylib.gz", "downloadSha256": "546095fb6c68b2d81e2c52d833e98340f61a3ecb7836ebf0b9f16e03404434b4", "delivery": "bundle", "asset": "webcrypto.dart" @@ -86,7 +86,7 @@ { "name": "libwebcrypto.dylib", "sha256": "4829448997180cd6ea174b7d236bf882f16dbc487f2e135454c8146afffe5833", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-e8a7f4f332215a48/ios-x64-iphonesimulator-libwebcrypto.dylib.gz", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-fdcb040a60a7f049/ios-x64-iphonesimulator-libwebcrypto.dylib.gz", "downloadSha256": "39d0666c37b9ec68b5685efc55d9bc42024a203d4e4bbafb7fa08a53845931d0", "delivery": "bundle", "asset": "webcrypto.dart" @@ -100,7 +100,7 @@ { "name": "libwebcrypto.so", "sha256": "65db805f157adabdf83095915dd7e317b3479e1148d6941d9c1b3c28ad568404", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-e8a7f4f332215a48/linux-arm64-libwebcrypto.so.gz", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-fdcb040a60a7f049/linux-arm64-libwebcrypto.so.gz", "downloadSha256": "a21a12811fdcfcdaa2265e8abed00934ca02787ee7e0a54f15ba9077ecac9dec", "delivery": "bundle", "asset": "webcrypto.dart" @@ -114,7 +114,7 @@ { "name": "libwebcrypto.so", "sha256": "6e25a4ad41d21971e8a223e95674a97fc8a3e4a212ba0f011d82d711346c5818", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-e8a7f4f332215a48/linux-x64-libwebcrypto.so.gz", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-fdcb040a60a7f049/linux-x64-libwebcrypto.so.gz", "downloadSha256": "5e06e65b8b3e8da741989ae77ca8994f5bd105394f32044c8b292add46993ca7", "delivery": "bundle", "asset": "webcrypto.dart" @@ -129,7 +129,7 @@ { "name": "libwebcrypto.dylib", "sha256": "89d60e9dac9a0850e064cc7c935f1ff4568ec9a984a3fe60a3e4363429569d5f", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-e8a7f4f332215a48/macos-arm64-libwebcrypto.dylib.gz", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-fdcb040a60a7f049/macos-arm64-libwebcrypto.dylib.gz", "downloadSha256": "ee3e95c18cf000f705da601d43b8e7d78b6ae1288794c949a5654273c1566ae8", "delivery": "bundle", "asset": "webcrypto.dart" @@ -144,7 +144,7 @@ { "name": "libwebcrypto.dylib", "sha256": "f574c753d80a21f3e1938a813cf4d18bd9968909ed2462648fecb1c69d4c7c42", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-e8a7f4f332215a48/macos-x64-libwebcrypto.dylib.gz", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-fdcb040a60a7f049/macos-x64-libwebcrypto.dylib.gz", "downloadSha256": "666e0a346d13fac9341395353e3d2c10ddff1f4ca144c0b63cefcd37c9da42a4", "delivery": "bundle", "asset": "webcrypto.dart" @@ -157,9 +157,9 @@ "files": [ { "name": "webcrypto.dll", - "sha256": "081f5078921753fb526485590187f8b4ad8d819a022da48c20ddc89288d141b3", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-e8a7f4f332215a48/windows-arm64-webcrypto.dll.gz", - "downloadSha256": "343ce56995f026233cce864900f2633c6f40c47c2cc59208935c03de11628bd6", + "sha256": "19ee49b80dbb701d85c91798bd27419425f7dcd17bd9518c14905d153f471fa3", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-fdcb040a60a7f049/windows-arm64-webcrypto.dll.gz", + "downloadSha256": "0fe8569cbee890535f50eef4fb2c16a0bca32c9310597e25c2ca6816ee1a8f73", "delivery": "bundle", "asset": "webcrypto.dart" } @@ -171,9 +171,9 @@ "files": [ { "name": "webcrypto.dll", - "sha256": "8fdf864a5a6dd548dd43d3e1e0734b5cf822a1f759590c5f276ceb23a4aead0a", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-e8a7f4f332215a48/windows-x64-webcrypto.dll.gz", - "downloadSha256": "aa3d1603fb0389bf40168037656e93a870571877c4c826b9ab31be883a1024d6", + "sha256": "271f8206d6d7689e3986daac1a6cfa533d95f1d52dd20949cf7df0d045ac38ad", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-fdcb040a60a7f049/windows-x64-webcrypto.dll.gz", + "downloadSha256": "6b54fc51e200f789997c9a2bfbf523c0b2691efe1423b0ad55fa71432240d039", "delivery": "bundle", "asset": "webcrypto.dart" } From bac9dcf1863e7b87f7e2b2a6fa75036bbfa1cea1 Mon Sep 17 00:00:00 2001 From: James O'Leary Date: Wed, 4 Nov 2026 15:53:00 -0800 Subject: [PATCH 43/69] Safari: pass upstream's new tests The fork runs Safari in CI; upstream does not, and 6 of its new tests failed there. - JWK import on the web now rejects members that are not url-safe base64 without padding, as the FFI implementation does since #329. Chrome and Firefox rejected them already; Safari accepted padded values like AQ==. - Safari rejects HMAC lengths that are not whole bytes (importKey and generateKey, checked with 9, 255 and 524289 bits). The length-9 JWK test is skipped there, and the random-fill quota test uses 65537 whole bytes. - Safari accepts Chromium's bad EC key vectors 14 and 19: skipped there, like 14, 16, 17 and 19 on Firefox. - test/ffi/sanitizer_test.dart runs on the VM only (dart-vm, which also matches vm-asan), like the rest of test/ffi. In Safari it failed because Safari accepts a corrupt ECDH PKCS8 key. Tested on macOS arm64: VM 1595, Chrome 1586, Firefox 1555, Safari 1389. --- lib/src/impl_js/impl_js.utils.dart | 34 +++++++++++++++++++ .../regression/hmac_large_generated_key.dart | 9 +++-- .../regression/issue_302_hmac_jwk_length.dart | 3 ++ lib/src/testing/utils/detected_runtime.dart | 8 +++++ test/chromium_bad_ec_browser_test.dart | 9 +++-- test/ffi/sanitizer_test.dart | 3 ++ 6 files changed, 61 insertions(+), 5 deletions(-) diff --git a/lib/src/impl_js/impl_js.utils.dart b/lib/src/impl_js/impl_js.utils.dart index 08210fde3..e4b0018b4 100644 --- a/lib/src/impl_js/impl_js.utils.dart +++ b/lib/src/impl_js/impl_js.utils.dart @@ -160,6 +160,7 @@ Future _importJsonWebKey( List usages, String expectedType, ) { + _checkJwkBase64Url(jwk); return _handleDomException(() async { final jwkObj = subtle.JsonWebKey.fromJson(jwk); // TODO: Validate expected 'use' the way we have it in the FFI implementation @@ -310,6 +311,39 @@ Future> _exportJsonWebKey( }); } +const _jwkBase64UrlMembers = [ + 'k', + 'x', + 'y', + 'd', + 'n', + 'e', + 'p', + 'q', + 'dp', + 'dq', + 'qi', +]; +final _base64UrlNoPadding = RegExp(r'^[A-Za-z0-9_-]*$'); + +/// Throws [FormatException] if a JWK member that holds bytes is not url-safe +/// base64 without padding (RFC 7518), as the FFI implementation does. +/// +/// Chrome and Firefox reject such keys themselves, but Safari accepts padded +/// values such as `AQ==`. +void _checkJwkBase64Url(Map jwk) { + for (final member in _jwkBase64UrlMembers) { + final value = jwk[member]; + if (value is String && + (!_base64UrlNoPadding.hasMatch(value) || value.length % 4 == 1)) { + throw FormatException( + 'JWK property "$member" is not url-safe base64 without padding', + value, + ); + } + } +} + /// Adapt `crypto.subtle.generateKey` to Dart types. Future _generateKey( subtle.Algorithm algorithm, diff --git a/lib/src/testing/regression/hmac_large_generated_key.dart b/lib/src/testing/regression/hmac_large_generated_key.dart index 5b8bd7aad..607bd4b6a 100644 --- a/lib/src/testing/regression/hmac_large_generated_key.dart +++ b/lib/src/testing/regression/hmac_large_generated_key.dart @@ -14,18 +14,23 @@ import 'package:webcrypto/webcrypto.dart'; +import '../utils/detected_runtime.dart'; import '../utils/utils.dart'; List<({String name, Future Function() test})> tests() => [ ( name: 'HMAC generates a key above the random-fill quota', test: () async { - const length = 65536 * 8 + 1; + // Safari only generates whole bytes: test the quota there with 65537 + // bytes, and the unused bits elsewhere. + final length = hmacLengthMustBeWholeBytes ? 65537 * 8 : 65536 * 8 + 1; final key = await HmacSecretKey.generateKey(Hash.sha256, length: length); final raw = await key.exportRawKey(); check(raw.length == 65537, 'Expected a 65537-byte generated key'); - check(raw.last & 0x7f == 0, 'Unused bits must be zero'); + if (!hmacLengthMustBeWholeBytes) { + check(raw.last & 0x7f == 0, 'Unused bits must be zero'); + } final data = [1, 2, 3]; final signature = await key.signBytes(data); diff --git a/lib/src/testing/regression/issue_302_hmac_jwk_length.dart b/lib/src/testing/regression/issue_302_hmac_jwk_length.dart index 38d02f987..ac23c63a5 100644 --- a/lib/src/testing/regression/issue_302_hmac_jwk_length.dart +++ b/lib/src/testing/regression/issue_302_hmac_jwk_length.dart @@ -13,6 +13,7 @@ // limitations under the License. import 'package:webcrypto/webcrypto.dart'; +import '../utils/detected_runtime.dart'; import '../utils/utils.dart'; void main() => tests().runTests(); @@ -23,6 +24,8 @@ List<({String name, Future Function() test})> tests() { tests.add((name: name, test: fn)); test('Hmac: importJsonWebKey applies length like importRawKey', () async { + // Safari rejects a 9-bit key in importRawKey and importJsonWebKey alike. + if (hmacLengthMustBeWholeBytes) return; final keyData = [0xff, 0xe0]; final jwk = {'kty': 'oct', 'alg': 'HS256', 'k': '_-A'}; diff --git a/lib/src/testing/utils/detected_runtime.dart b/lib/src/testing/utils/detected_runtime.dart index f814c960e..fd141f524 100644 --- a/lib/src/testing/utils/detected_runtime.dart +++ b/lib/src/testing/utils/detected_runtime.dart @@ -34,6 +34,14 @@ T? nullOnFirefox(T value) => detectedRuntime == 'firefox' ? null : value; /// Safari and which has been documented in the API documentation. T? nullOnSafari(T value) => detectedRuntime == 'safari' ? null : value; +/// Whether HMAC keys must be a whole number of bytes. +/// +/// Safari rejects an HMAC `length` that is not a multiple of 8: `importKey` +/// throws `DataError` and `generateKey` throws `OperationError` (checked in +/// Safari 26 with lengths 9, 255 and 524289; 16, 256 and 524296 work). Chrome, +/// Firefox and the VM accept any length. +bool get hmacLengthMustBeWholeBytes => detectedRuntime == 'safari'; + /// Removes, on Safari, the test cases that generate an RSA key with public /// exponent 3; other browsers and the VM keep them. /// diff --git a/test/chromium_bad_ec_browser_test.dart b/test/chromium_bad_ec_browser_test.dart index 89d97e579..572e07645 100644 --- a/test/chromium_bad_ec_browser_test.dart +++ b/test/chromium_bad_ec_browser_test.dart @@ -79,9 +79,12 @@ void main() { reason: 'Chromium vector $index: ${vector['error']}', ); }, - onPlatform: {14, 16, 17, 19}.contains(index) - ? {'firefox': Skip('Firefox accepts this EC key')} - : null, + onPlatform: { + if ({14, 16, 17, 19}.contains(index)) + 'firefox': Skip('Firefox accepts this EC key'), + if ({14, 19}.contains(index)) + 'safari': Skip('Safari accepts this EC key'), + }, ); } } diff --git a/test/ffi/sanitizer_test.dart b/test/ffi/sanitizer_test.dart index 351870458..923b41c54 100644 --- a/test/ffi/sanitizer_test.dart +++ b/test/ffi/sanitizer_test.dart @@ -12,6 +12,9 @@ // See the License for the specific language governing permissions and // limitations under the License. +// FFI only, like the rest of test/ffi; 'dart-vm' also matches the sanitizer +// runtimes (vm-asan, ...). +@TestOn('dart-vm') @Tags(['sanitizer']) @Timeout(Duration(minutes: 5)) library; From a9adc9ec880fc718a611fb929b989272a504ec48 Mon Sep 17 00:00:00 2001 From: James O'Leary Date: Thu, 5 Nov 2026 09:44:44 -0800 Subject: [PATCH 44/69] ci: Telosnex/telosnex_ci_tools v0.5.8 --- .github/workflows/integration.yml | 14 +++++++------- .github/workflows/kiln_check.yml | 2 +- .github/workflows/kiln_release.yml | 2 +- .github/workflows/roll_boringssl.yml | 2 +- .github/workflows/unit.yml | 4 ++-- .github/workflows/upstream_watch.yml | 2 +- 6 files changed, 13 insertions(+), 13 deletions(-) diff --git a/.github/workflows/integration.yml b/.github/workflows/integration.yml index 545e5617f..7d6c7c5ef 100644 --- a/.github/workflows/integration.yml +++ b/.github/workflows/integration.yml @@ -36,7 +36,7 @@ jobs: run: | sudo apt-get update -qq sudo apt-get install -y -qq clang cmake ninja-build pkg-config libgtk-3-dev xvfb > /dev/null - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@c03733211dca28d8590002199d7a87af85edde51 # v0.5.7 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@4a798518378fb0c362ebe42940227560e6b4ecde # v0.5.8 - run: flutter pub get - run: dart test --exclude-tags browser-interop -p vm -r expanded - name: Demo app integration test @@ -99,9 +99,9 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@c03733211dca28d8590002199d7a87af85edde51 # v0.5.7 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@4a798518378fb0c362ebe42940227560e6b4ecde # v0.5.8 - run: flutter pub get - - uses: Telosnex/telosnex_ci_tools/actions/dart-test-browser@c03733211dca28d8590002199d7a87af85edde51 # v0.5.7 + - uses: Telosnex/telosnex_ci_tools/actions/dart-test-browser@4a798518378fb0c362ebe42940227560e6b4ecde # v0.5.8 with: platform: ${{ env.BROWSER }} compiler: ${{ env.COMPILER }} @@ -151,9 +151,9 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@c03733211dca28d8590002199d7a87af85edde51 # v0.5.7 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@4a798518378fb0c362ebe42940227560e6b4ecde # v0.5.8 - run: flutter pub get - - uses: Telosnex/telosnex_ci_tools/actions/ios-flutter-test@c03733211dca28d8590002199d7a87af85edde51 # v0.5.7 + - uses: Telosnex/telosnex_ci_tools/actions/ios-flutter-test@4a798518378fb0c362ebe42940227560e6b4ecde # v0.5.8 with: working-directory: example/webcrypto_demo_flutter_app target: integration_test/webcrypto_test.dart @@ -167,9 +167,9 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@c03733211dca28d8590002199d7a87af85edde51 # v0.5.7 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@4a798518378fb0c362ebe42940227560e6b4ecde # v0.5.8 - run: flutter pub get - - uses: Telosnex/telosnex_ci_tools/actions/android-emulator@c03733211dca28d8590002199d7a87af85edde51 # v0.5.7 + - uses: Telosnex/telosnex_ci_tools/actions/android-emulator@4a798518378fb0c362ebe42940227560e6b4ecde # v0.5.8 id: emulator with: api-level: '36' diff --git a/.github/workflows/kiln_check.yml b/.github/workflows/kiln_check.yml index e3cf2a110..332fa2b83 100644 --- a/.github/workflows/kiln_check.yml +++ b/.github/workflows/kiln_check.yml @@ -16,4 +16,4 @@ permissions: jobs: kiln-check: - uses: Telosnex/telosnex_ci_tools/.github/workflows/kiln_check.yml@c03733211dca28d8590002199d7a87af85edde51 # v0.5.7 + uses: Telosnex/telosnex_ci_tools/.github/workflows/kiln_check.yml@4a798518378fb0c362ebe42940227560e6b4ecde # v0.5.8 diff --git a/.github/workflows/kiln_release.yml b/.github/workflows/kiln_release.yml index fb1a42ac6..a6cee7217 100644 --- a/.github/workflows/kiln_release.yml +++ b/.github/workflows/kiln_release.yml @@ -25,7 +25,7 @@ permissions: jobs: kiln-release: - uses: Telosnex/telosnex_ci_tools/.github/workflows/kiln_release.yml@c03733211dca28d8590002199d7a87af85edde51 # v0.5.7 + uses: Telosnex/telosnex_ci_tools/.github/workflows/kiln_release.yml@4a798518378fb0c362ebe42940227560e6b4ecde # v0.5.8 permissions: contents: write with: diff --git a/.github/workflows/roll_boringssl.yml b/.github/workflows/roll_boringssl.yml index 343b57d85..87d711dcd 100644 --- a/.github/workflows/roll_boringssl.yml +++ b/.github/workflows/roll_boringssl.yml @@ -28,7 +28,7 @@ jobs: with: fetch-depth: 0 - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@c03733211dca28d8590002199d7a87af85edde51 # v0.5.7 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@4a798518378fb0c362ebe42940227560e6b4ecde # v0.5.8 - name: Install system dependencies run: | diff --git a/.github/workflows/unit.yml b/.github/workflows/unit.yml index 95a3f5284..aaec1ae78 100644 --- a/.github/workflows/unit.yml +++ b/.github/workflows/unit.yml @@ -25,8 +25,8 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@c03733211dca28d8590002199d7a87af85edde51 # v0.5.7 - - uses: Telosnex/telosnex_ci_tools/actions/lint@c03733211dca28d8590002199d7a87af85edde51 # v0.5.7 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@4a798518378fb0c362ebe42940227560e6b4ecde # v0.5.8 + - uses: Telosnex/telosnex_ci_tools/actions/lint@4a798518378fb0c362ebe42940227560e6b4ecde # v0.5.8 - run: flutter pub get - run: dart format --output=none --set-exit-if-changed . - run: dart analyze --fatal-warnings . diff --git a/.github/workflows/upstream_watch.yml b/.github/workflows/upstream_watch.yml index 128c655b7..094bacd64 100644 --- a/.github/workflows/upstream_watch.yml +++ b/.github/workflows/upstream_watch.yml @@ -16,4 +16,4 @@ permissions: jobs: watch: - uses: Telosnex/telosnex_ci_tools/.github/workflows/upstream_watch.yml@c03733211dca28d8590002199d7a87af85edde51 # v0.5.7 + uses: Telosnex/telosnex_ci_tools/.github/workflows/upstream_watch.yml@4a798518378fb0c362ebe42940227560e6b4ecde # v0.5.8 From 3786078211b16e0bec95a58b6166c8c6a4dc81fd Mon Sep 17 00:00:00 2001 From: James O'Leary Date: Fri, 6 Nov 2026 15:10:48 -0800 Subject: [PATCH 45/69] code_assets ^2.2.0, hooks ^2.2.0, kiln ^0.2.0 --- CHANGELOG.md | 1 + hook/build.dart | 6 ++++-- pubspec.yaml | 8 ++++---- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9b9b0cda4..59bf02566 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,5 @@ # 0.6.2-wip +* Requires `code_assets` 2.2, `hooks` 2.2 and `kiln` 0.2. * Synced with the latest upstream `google/webcrypto.dart` changes. * Added a content-addressed native build cache under `~/.cache/webcrypto.dart` to avoid recompiling BoringSSL when the native inputs are unchanged. diff --git a/hook/build.dart b/hook/build.dart index e359ad57a..2014a6e26 100644 --- a/hook/build.dart +++ b/hook/build.dart @@ -340,9 +340,11 @@ Future _computeBuildKey({ addText('androidApi=${code.android.targetNdkApi}'); } else if (code.targetOS == OS.iOS) { addText('iosSdk=${code.iOS.targetSdk}'); - addText('iosVersion=${code.iOS.targetVersion}'); + final v = code.iOS.deploymentVersion; + addText('iosVersion=${v.major}.${v.minor}.${v.patch}'); } else if (code.targetOS == OS.macOS) { - addText('macosVersion=${code.macOS.targetVersion}'); + final v = code.macOS.deploymentVersion; + addText('macosVersion=${v.major}.${v.minor}.${v.patch}'); } for (final define in defines.entries.toList()..sort((a, b) => a.key.compareTo(b.key))) { diff --git a/pubspec.yaml b/pubspec.yaml index 8cd20e691..e8bec7c49 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -23,12 +23,12 @@ environment: dependencies: crypto: ^3.0.7 ffi: ^2.0.0 - hooks: ^2.0.0 + hooks: ^2.2.0 logging: ^1.3.0 - kiln: ^0.1.1 - native_toolchain_cmake: ^0.3.0 + kiln: ^0.2.0 + native_toolchain_cmake: ^0.3.2 path: ^1.9.0 - code_assets: ^1.0.0 + code_assets: ^2.2.0 meta: ^1.3.0 dev_dependencies: From 25ab269197ea11b13e60febf1cbae4b594570046 Mon Sep 17 00:00:00 2001 From: James O'Leary Date: Fri, 6 Nov 2026 15:11:04 -0800 Subject: [PATCH 46/69] dev: ffigen ^23.0.0 (code_assets 2) --- pubspec.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pubspec.yaml b/pubspec.yaml index e8bec7c49..7bfc08a04 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -33,7 +33,7 @@ dependencies: dev_dependencies: convert: ^3.1.2 - ffigen: ^21.0.0 + ffigen: ^23.0.0 test: ^1.30.0 stream_channel: ^2.1.2 yaml: ^3.0.0 From 66c07e01a9f12219cb6f58848e3f09c7c9d0ba8d Mon Sep 17 00:00:00 2001 From: James O'Leary Date: Fri, 6 Nov 2026 15:14:19 -0800 Subject: [PATCH 47/69] code_assets 2: non-const Architecture sets in the hook; tool/ffigen.dart on the ffigen 23 visitor API (output unchanged) --- hook/build.dart | 17 +- tool/ffigen.dart | 474 ++++++++++++++++++++++++----------------------- 2 files changed, 253 insertions(+), 238 deletions(-) diff --git a/hook/build.dart b/hook/build.dart index 2014a6e26..f10359686 100644 --- a/hook/build.dart +++ b/hook/build.dart @@ -264,22 +264,19 @@ Future _buildIntoCache({ } void _validateTarget(OS os, Architecture architecture) { + // Not const: Architecture overrides == (code_assets 2). final supported = switch (os) { - OS.android => const { + OS.android => { Architecture.arm, Architecture.arm64, Architecture.ia32, Architecture.x64, }, - OS.iOS => const {Architecture.arm64, Architecture.x64}, - OS.linux => const {Architecture.arm64, Architecture.x64}, - OS.macOS => const {Architecture.arm64, Architecture.x64}, - OS.windows => const { - Architecture.arm64, - Architecture.ia32, - Architecture.x64, - }, - _ => const {}, + OS.iOS => {Architecture.arm64, Architecture.x64}, + OS.linux => {Architecture.arm64, Architecture.x64}, + OS.macOS => {Architecture.arm64, Architecture.x64}, + OS.windows => {Architecture.arm64, Architecture.ia32, Architecture.x64}, + _ => {}, }; if (!supported.contains(architecture)) { throw UnsupportedError( diff --git a/tool/ffigen.dart b/tool/ffigen.dart index 4c5bc143b..f0cfc3b78 100644 --- a/tool/ffigen.dart +++ b/tool/ffigen.dart @@ -16,18 +16,20 @@ import 'dart:io'; import 'package:ffigen/ffigen.dart'; -void main() { +Future main() async { final packageRoot = Platform.script.resolve('../'); - generateWebCryptoBindings(packageRoot); - generateBoringSslBindings(packageRoot); + await generateWebCryptoBindings(packageRoot); + await generateBoringSslBindings(packageRoot); } -void generateWebCryptoBindings(Uri packageRoot) { - FfiGenerator( +Future generateWebCryptoBindings(Uri packageRoot) { + return FfiGenerator( output: Output( - dartFile: packageRoot.resolve( - 'lib/src/boringssl/bindings/generated_bindings.dart', + dart: DartOutput( + path: packageRoot.resolve( + 'lib/src/boringssl/bindings/generated_bindings.dart', + ), ), style: const NativeExternalBindings( assetId: 'package:webcrypto/webcrypto.dart', @@ -53,19 +55,24 @@ void generateWebCryptoBindings(Uri packageRoot) { // ignore_for_file: non_constant_identifier_names ''', ), - headers: Headers(entryPoints: [packageRoot.resolve('src/webcrypto.h')]), - functions: Functions( - include: Declarations.includeSet({'webcrypto_get_CBB_size'}), - ), - structs: const Structs(dependencies: CompoundDependencies.opaque), + input: Input(entryPoints: [packageRoot.resolve('src/webcrypto.h')]), + visitors: [ + Visitor( + func: (node) => + node.isIncluded = node.originalName == 'webcrypto_get_CBB_size', + struct: (node) => node.dependencies = CompoundDependencies.opaque, + ), + ], ).generate(); } -void generateBoringSslBindings(Uri packageRoot) { - FfiGenerator( +Future generateBoringSslBindings(Uri packageRoot) { + return FfiGenerator( output: Output( - dartFile: packageRoot.resolve( - 'lib/src/third_party/boringssl/generated_bindings.dart', + dart: DartOutput( + path: packageRoot.resolve( + 'lib/src/third_party/boringssl/generated_bindings.dart', + ), ), style: const NativeExternalBindings( assetId: 'package:webcrypto/webcrypto.dart', @@ -131,7 +138,7 @@ void generateBoringSslBindings(Uri packageRoot) { // ignore_for_file: unused_field ''', ), - headers: Headers( + input: Input( entryPoints: [ packageRoot.resolve('third_party/boringssl/src/include/openssl/aead.h'), packageRoot.resolve('third_party/boringssl/src/include/openssl/aes.h'), @@ -170,218 +177,229 @@ void generateBoringSslBindings(Uri packageRoot) { '-U__PRAGMA_REDEFINE_EXTNAME', ], ), - macros: Macros( - include: Declarations.includeSet({ - 'AES_BLOCK_SIZE', - 'EC_PKEY_NO_PUBKEY', - 'EVP_PKEY_EC', - 'EVP_PKEY_RSA', - 'HKDF_R_OUTPUT_TOO_LARGE', - 'NID_secp384r1', - 'NID_secp521r1', - 'NID_X9_62_prime256v1', - 'RSA_PKCS1_OAEP_PADDING', - 'RSA_PKCS1_PADDING', - 'RSA_PKCS1_PSS_PADDING', - }), - ), - enums: Enums( - include: Declarations.includeSet({'point_conversion_form_t'}), - style: (decl, suggestedStyle) => EnumStyle.intConstants, - ), - unnamedEnums: UnnamedEnums( - include: Declarations.includeSet({'ERR_LIB_HKDF'}), - ), - structs: Structs( - include: Declarations.includeSet({'cbs_st', 'cbb_st'}), - dependencies: CompoundDependencies.opaque, - ), // BoringSSL uses a pointer-sized unsigned word on both 32-bit and 64-bit // targets. Generation runs on a 64-bit host, so map this typedef explicitly. - typedefs: Typedefs( - // ignore: deprecated_member_use - imported: [ - ImportedType( - const LibraryImport('ffi', 'dart:ffi'), - 'UintPtr', - 'int', - 'BN_ULONG', - ), - ], - include: Declarations.includeSet({ - 'BIGNUM', - 'BN_CTX', - 'BN_GENCB', - 'BN_ULONG', - 'CBB', - 'CBS', - 'ECDSA_SIG', - 'EC_GROUP', - 'EC_KEY', - 'EC_POINT', - 'ENGINE', - 'EVP_AEAD', - 'EVP_AEAD_CTX', - 'EVP_CIPHER', - 'EVP_CIPHER_CTX', - 'EVP_MD', - 'EVP_MD_CTX', - 'EVP_PKEY', - 'EVP_PKEY_CTX', - 'HMAC_CTX', - 'RSA', - }), - ), - functions: Functions( - include: Declarations.includeSet({ - // Source of truth for BoringSSL entry points bound from Dart. - 'webcrypto_BN_bin2bn', - 'webcrypto_BN_bn2bin_padded', - 'webcrypto_BN_free', - 'webcrypto_BN_new', - 'webcrypto_BN_num_bytes', - 'webcrypto_BN_set_word', - 'webcrypto_CBB_cleanup', - 'webcrypto_CBB_data', - 'webcrypto_CBB_flush', - 'webcrypto_CBB_init', - 'webcrypto_CBB_len', - 'webcrypto_CBB_zero', - 'webcrypto_CRYPTO_memcmp', - 'webcrypto_ECDH_compute_key', - 'webcrypto_ECDSA_SIG_free', - 'webcrypto_ECDSA_SIG_get0', - 'webcrypto_ECDSA_SIG_marshal', - 'webcrypto_ECDSA_SIG_new', - 'webcrypto_ECDSA_SIG_parse', - 'webcrypto_EC_GROUP_get0_order', - 'webcrypto_EC_GROUP_get_curve_name', - 'webcrypto_EC_GROUP_get_degree', - 'webcrypto_EC_KEY_check_key', - 'webcrypto_EC_KEY_free', - 'webcrypto_EC_KEY_generate_key', - 'webcrypto_EC_KEY_get0_group', - 'webcrypto_EC_KEY_get0_private_key', - 'webcrypto_EC_KEY_get0_public_key', - 'webcrypto_EC_KEY_get_enc_flags', - 'webcrypto_EC_KEY_new_by_curve_name', - 'webcrypto_EC_KEY_set_enc_flags', - 'webcrypto_EC_KEY_set_private_key', - 'webcrypto_EC_KEY_set_public_key', - 'webcrypto_EC_KEY_set_public_key_affine_coordinates', - 'webcrypto_EC_POINT_free', - 'webcrypto_EC_POINT_get_affine_coordinates_GFp', - 'webcrypto_EC_POINT_new', - 'webcrypto_EC_POINT_oct2point', - 'webcrypto_EC_POINT_point2cbb', - 'webcrypto_ERR_clear_error', - 'webcrypto_ERR_error_string_n', - 'webcrypto_ERR_get_error', - 'webcrypto_ERR_peek_error', - 'webcrypto_EVP_aead_aes_128_gcm', - 'webcrypto_EVP_aead_aes_256_gcm', - 'webcrypto_EVP_AEAD_CTX_free', - 'webcrypto_EVP_AEAD_CTX_new', - 'webcrypto_EVP_AEAD_CTX_open', - 'webcrypto_EVP_AEAD_CTX_seal', - 'webcrypto_EVP_AEAD_max_overhead', - 'webcrypto_EVP_aes_128_cbc', - 'webcrypto_EVP_aes_128_ctr', - 'webcrypto_EVP_aes_256_cbc', - 'webcrypto_EVP_aes_256_ctr', - 'webcrypto_EVP_CIPHER_CTX_free', - 'webcrypto_EVP_CIPHER_CTX_new', - 'webcrypto_EVP_CipherFinal_ex', - 'webcrypto_EVP_CipherInit_ex', - 'webcrypto_EVP_CIPHER_iv_length', - 'webcrypto_EVP_CipherUpdate', - 'webcrypto_EVP_DigestFinal', - 'webcrypto_EVP_DigestInit', - 'webcrypto_EVP_DigestSignFinal', - 'webcrypto_EVP_DigestSignInit', - 'webcrypto_EVP_DigestSignUpdate', - 'webcrypto_EVP_DigestUpdate', - 'webcrypto_EVP_DigestVerifyFinal', - 'webcrypto_EVP_DigestVerifyInit', - 'webcrypto_EVP_DigestVerifyUpdate', - 'webcrypto_EVP_marshal_private_key', - 'webcrypto_EVP_marshal_public_key', - 'webcrypto_EVP_MD_CTX_free', - 'webcrypto_EVP_MD_CTX_new', - 'webcrypto_EVP_MD_CTX_size', - 'webcrypto_EVP_parse_private_key', - 'webcrypto_EVP_parse_public_key', - 'webcrypto_EVP_PKEY_CTX_free', - 'webcrypto_EVP_PKEY_CTX_new', - 'webcrypto_EVP_PKEY_CTX_set0_rsa_oaep_label', - 'webcrypto_EVP_PKEY_CTX_set_rsa_mgf1_md', - 'webcrypto_EVP_PKEY_CTX_set_rsa_oaep_md', - 'webcrypto_EVP_PKEY_CTX_set_rsa_padding', - 'webcrypto_EVP_PKEY_CTX_set_rsa_pss_saltlen', - 'webcrypto_EVP_PKEY_decrypt', - 'webcrypto_EVP_PKEY_decrypt_init', - 'webcrypto_EVP_PKEY_encrypt', - 'webcrypto_EVP_PKEY_encrypt_init', - 'webcrypto_EVP_PKEY_free', - 'webcrypto_EVP_PKEY_get1_EC_KEY', - 'webcrypto_EVP_PKEY_get1_RSA', - 'webcrypto_EVP_PKEY_id', - 'webcrypto_EVP_PKEY_new', - 'webcrypto_EVP_PKEY_set1_EC_KEY', - 'webcrypto_EVP_PKEY_set1_RSA', - 'webcrypto_EVP_sha1', - 'webcrypto_EVP_sha256', - 'webcrypto_EVP_sha384', - 'webcrypto_EVP_sha512', - 'webcrypto_HKDF', - 'webcrypto_HMAC_CTX_free', - 'webcrypto_HMAC_CTX_new', - 'webcrypto_HMAC_Final', - 'webcrypto_HMAC_Init_ex', - 'webcrypto_HMAC_size', - 'webcrypto_HMAC_Update', - 'webcrypto_OPENSSL_malloc', - 'webcrypto_PKCS5_PBKDF2_HMAC', - 'webcrypto_RAND_bytes', - 'webcrypto_RSA_check_key', - 'webcrypto_RSA_free', - 'webcrypto_RSA_generate_key_ex', - 'webcrypto_RSA_get0_crt_params', - 'webcrypto_RSA_get0_factors', - 'webcrypto_RSA_get0_key', - 'webcrypto_RSA_new', - 'webcrypto_RSAPublicKey_dup', - 'webcrypto_RSA_set0_crt_params', - 'webcrypto_RSA_set0_factors', - 'webcrypto_RSA_set0_key', - // These are referenced, just need to be sorted right. - 'webcrypto_OPENSSL_free', - 'webcrypto_OPENSSL_memdup', - 'webcrypto_EVP_MD_size', - // Not used but maybe they should be: - 'webcrypto_EVP_AEAD_nonce_length', - 'webcrypto_EVP_AEAD_max_tag_len', - // Not used by nice to have, maybe, or maybe we remove / comment them out - 'webcrypto_EVP_AEAD_key_length', - 'webcrypto_BN_value_one', - 'webcrypto_BN_add', - 'webcrypto_BN_sub', - 'webcrypto_BN_cmp', - 'webcrypto_BN_lshift', - 'webcrypto_EVP_CIPHER_block_size', - 'webcrypto_EC_GROUP_new_by_curve_name', - 'webcrypto_EC_GROUP_free', - 'webcrypto_EVP_PKEY_set_type', - // Self testing only.. - 'webcrypto_BORINGSSL_self_test', - }), - rename: (decl) { - final name = decl.originalName; - if (name.startsWith('webcrypto_')) { - return name.substring('webcrypto_'.length); - } - return name; - }, - ), + importType: (declaration) => declaration.originalName == 'BN_ULONG' + ? ImportedType( + const LibraryImport('ffi', 'dart:ffi'), + 'UintPtr', + 'int', + 'BN_ULONG', + ) + : null, + visitors: [ + Visitor( + macroConstant: (node) => + node.isIncluded = _boringSslMacros.contains(node.originalName), + enumClass: (node) { + node.isIncluded = _boringSslEnums.contains(node.originalName); + node.style = EnumStyle.intConstants; + }, + unnamedEnumConstant: (node) => node.isIncluded = _boringSslUnnamedEnums + .contains(node.originalName), + struct: (node) { + node.isIncluded = _boringSslStructs.contains(node.originalName); + node.dependencies = CompoundDependencies.opaque; + }, + typealias: (node) => + node.isIncluded = _boringSslTypedefs.contains(node.originalName) + ? TypealiasInclude.ifUsed + : TypealiasInclude.never, + func: (node) { + node.isIncluded = _boringSslFunctions.contains(node.originalName); + final name = node.originalName; + if (name.startsWith('webcrypto_')) { + node.name = name.substring('webcrypto_'.length); + } + }, + ), + ], ).generate(); } + +const _boringSslMacros = { + 'AES_BLOCK_SIZE', + 'EC_PKEY_NO_PUBKEY', + 'EVP_PKEY_EC', + 'EVP_PKEY_RSA', + 'HKDF_R_OUTPUT_TOO_LARGE', + 'NID_secp384r1', + 'NID_secp521r1', + 'NID_X9_62_prime256v1', + 'RSA_PKCS1_OAEP_PADDING', + 'RSA_PKCS1_PADDING', + 'RSA_PKCS1_PSS_PADDING', +}; + +const _boringSslEnums = {'point_conversion_form_t'}; + +const _boringSslUnnamedEnums = {'ERR_LIB_HKDF'}; + +const _boringSslStructs = {'cbs_st', 'cbb_st'}; + +const _boringSslTypedefs = { + 'BIGNUM', + 'BN_CTX', + 'BN_GENCB', + 'BN_ULONG', + 'CBB', + 'CBS', + 'ECDSA_SIG', + 'EC_GROUP', + 'EC_KEY', + 'EC_POINT', + 'ENGINE', + 'EVP_AEAD', + 'EVP_AEAD_CTX', + 'EVP_CIPHER', + 'EVP_CIPHER_CTX', + 'EVP_MD', + 'EVP_MD_CTX', + 'EVP_PKEY', + 'EVP_PKEY_CTX', + 'HMAC_CTX', + 'RSA', +}; + +const _boringSslFunctions = { + // Source of truth for BoringSSL entry points bound from Dart. + 'webcrypto_BN_bin2bn', + 'webcrypto_BN_bn2bin_padded', + 'webcrypto_BN_free', + 'webcrypto_BN_new', + 'webcrypto_BN_num_bytes', + 'webcrypto_BN_set_word', + 'webcrypto_CBB_cleanup', + 'webcrypto_CBB_data', + 'webcrypto_CBB_flush', + 'webcrypto_CBB_init', + 'webcrypto_CBB_len', + 'webcrypto_CBB_zero', + 'webcrypto_CRYPTO_memcmp', + 'webcrypto_ECDH_compute_key', + 'webcrypto_ECDSA_SIG_free', + 'webcrypto_ECDSA_SIG_get0', + 'webcrypto_ECDSA_SIG_marshal', + 'webcrypto_ECDSA_SIG_new', + 'webcrypto_ECDSA_SIG_parse', + 'webcrypto_EC_GROUP_get0_order', + 'webcrypto_EC_GROUP_get_curve_name', + 'webcrypto_EC_GROUP_get_degree', + 'webcrypto_EC_KEY_check_key', + 'webcrypto_EC_KEY_free', + 'webcrypto_EC_KEY_generate_key', + 'webcrypto_EC_KEY_get0_group', + 'webcrypto_EC_KEY_get0_private_key', + 'webcrypto_EC_KEY_get0_public_key', + 'webcrypto_EC_KEY_get_enc_flags', + 'webcrypto_EC_KEY_new_by_curve_name', + 'webcrypto_EC_KEY_set_enc_flags', + 'webcrypto_EC_KEY_set_private_key', + 'webcrypto_EC_KEY_set_public_key', + 'webcrypto_EC_KEY_set_public_key_affine_coordinates', + 'webcrypto_EC_POINT_free', + 'webcrypto_EC_POINT_get_affine_coordinates_GFp', + 'webcrypto_EC_POINT_new', + 'webcrypto_EC_POINT_oct2point', + 'webcrypto_EC_POINT_point2cbb', + 'webcrypto_ERR_clear_error', + 'webcrypto_ERR_error_string_n', + 'webcrypto_ERR_get_error', + 'webcrypto_ERR_peek_error', + 'webcrypto_EVP_aead_aes_128_gcm', + 'webcrypto_EVP_aead_aes_256_gcm', + 'webcrypto_EVP_AEAD_CTX_free', + 'webcrypto_EVP_AEAD_CTX_new', + 'webcrypto_EVP_AEAD_CTX_open', + 'webcrypto_EVP_AEAD_CTX_seal', + 'webcrypto_EVP_AEAD_max_overhead', + 'webcrypto_EVP_aes_128_cbc', + 'webcrypto_EVP_aes_128_ctr', + 'webcrypto_EVP_aes_256_cbc', + 'webcrypto_EVP_aes_256_ctr', + 'webcrypto_EVP_CIPHER_CTX_free', + 'webcrypto_EVP_CIPHER_CTX_new', + 'webcrypto_EVP_CipherFinal_ex', + 'webcrypto_EVP_CipherInit_ex', + 'webcrypto_EVP_CIPHER_iv_length', + 'webcrypto_EVP_CipherUpdate', + 'webcrypto_EVP_DigestFinal', + 'webcrypto_EVP_DigestInit', + 'webcrypto_EVP_DigestSignFinal', + 'webcrypto_EVP_DigestSignInit', + 'webcrypto_EVP_DigestSignUpdate', + 'webcrypto_EVP_DigestUpdate', + 'webcrypto_EVP_DigestVerifyFinal', + 'webcrypto_EVP_DigestVerifyInit', + 'webcrypto_EVP_DigestVerifyUpdate', + 'webcrypto_EVP_marshal_private_key', + 'webcrypto_EVP_marshal_public_key', + 'webcrypto_EVP_MD_CTX_free', + 'webcrypto_EVP_MD_CTX_new', + 'webcrypto_EVP_MD_CTX_size', + 'webcrypto_EVP_parse_private_key', + 'webcrypto_EVP_parse_public_key', + 'webcrypto_EVP_PKEY_CTX_free', + 'webcrypto_EVP_PKEY_CTX_new', + 'webcrypto_EVP_PKEY_CTX_set0_rsa_oaep_label', + 'webcrypto_EVP_PKEY_CTX_set_rsa_mgf1_md', + 'webcrypto_EVP_PKEY_CTX_set_rsa_oaep_md', + 'webcrypto_EVP_PKEY_CTX_set_rsa_padding', + 'webcrypto_EVP_PKEY_CTX_set_rsa_pss_saltlen', + 'webcrypto_EVP_PKEY_decrypt', + 'webcrypto_EVP_PKEY_decrypt_init', + 'webcrypto_EVP_PKEY_encrypt', + 'webcrypto_EVP_PKEY_encrypt_init', + 'webcrypto_EVP_PKEY_free', + 'webcrypto_EVP_PKEY_get1_EC_KEY', + 'webcrypto_EVP_PKEY_get1_RSA', + 'webcrypto_EVP_PKEY_id', + 'webcrypto_EVP_PKEY_new', + 'webcrypto_EVP_PKEY_set1_EC_KEY', + 'webcrypto_EVP_PKEY_set1_RSA', + 'webcrypto_EVP_sha1', + 'webcrypto_EVP_sha256', + 'webcrypto_EVP_sha384', + 'webcrypto_EVP_sha512', + 'webcrypto_HKDF', + 'webcrypto_HMAC_CTX_free', + 'webcrypto_HMAC_CTX_new', + 'webcrypto_HMAC_Final', + 'webcrypto_HMAC_Init_ex', + 'webcrypto_HMAC_size', + 'webcrypto_HMAC_Update', + 'webcrypto_OPENSSL_malloc', + 'webcrypto_PKCS5_PBKDF2_HMAC', + 'webcrypto_RAND_bytes', + 'webcrypto_RSA_check_key', + 'webcrypto_RSA_free', + 'webcrypto_RSA_generate_key_ex', + 'webcrypto_RSA_get0_crt_params', + 'webcrypto_RSA_get0_factors', + 'webcrypto_RSA_get0_key', + 'webcrypto_RSA_new', + 'webcrypto_RSAPublicKey_dup', + 'webcrypto_RSA_set0_crt_params', + 'webcrypto_RSA_set0_factors', + 'webcrypto_RSA_set0_key', + // These are referenced, just need to be sorted right. + 'webcrypto_OPENSSL_free', + 'webcrypto_OPENSSL_memdup', + 'webcrypto_EVP_MD_size', + // Not used but maybe they should be: + 'webcrypto_EVP_AEAD_nonce_length', + 'webcrypto_EVP_AEAD_max_tag_len', + // Not used by nice to have, maybe, or maybe we remove / comment them out + 'webcrypto_EVP_AEAD_key_length', + 'webcrypto_BN_value_one', + 'webcrypto_BN_add', + 'webcrypto_BN_sub', + 'webcrypto_BN_cmp', + 'webcrypto_BN_lshift', + 'webcrypto_EVP_CIPHER_block_size', + 'webcrypto_EC_GROUP_new_by_curve_name', + 'webcrypto_EC_GROUP_free', + 'webcrypto_EVP_PKEY_set_type', + // Self testing only.. + 'webcrypto_BORINGSSL_self_test', +}; From c4e86b6cc7d61a4263eb8b5b3d1ede79fc66aaac Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Fri, 6 Nov 2026 23:22:18 +0000 Subject: [PATCH 48/69] kiln: manifest of release native-915dcd84d3b5df6a --- native_artifacts/prebuilt.json | 80 +++++++++++++++++----------------- 1 file changed, 40 insertions(+), 40 deletions(-) diff --git a/native_artifacts/prebuilt.json b/native_artifacts/prebuilt.json index 23f2af544..0d64b3f31 100644 --- a/native_artifacts/prebuilt.json +++ b/native_artifacts/prebuilt.json @@ -1,62 +1,62 @@ { "schema": 1, - "sourceKey": "fdcb040a60a7f04932dc2266ba3f2b1b1642f3f2c288590ad20ea3096ed18fb7", - "release": "https://github.com/Telosnex/webcrypto.dart/releases/tag/native-fdcb040a60a7f049", + "sourceKey": "915dcd84d3b5df6a9a76eff7605a9aa70622bbbfaa83fcd9b82b9335dac6761f", + "release": "https://github.com/Telosnex/webcrypto.dart/releases/tag/native-915dcd84d3b5df6a", "targets": { "android-arm": { - "runner": "ubuntu22 20261026.341.1", + "runner": "ubuntu22 20261101.347.1", "toolchain": "Android NDK 28.2.13676358, cmake version 3.31.6", "minOSVersion": 24, "files": [ { "name": "libwebcrypto.so", - "sha256": "b6f1c5c0c05e17eadfae63f5480a45cca9211521ccf0fbe5fef407e6b9d00c03", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-fdcb040a60a7f049/android-arm-libwebcrypto.so.gz", - "downloadSha256": "d387dec0622b0f04c3b9c55ad50e5d2d39e0475b37a16336a0cf985544bf0432", + "sha256": "4f4b6345ad7469b8ea8fe732f756e74ad7b7dc58125a5ce39b656d110cfbb160", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-915dcd84d3b5df6a/android-arm-libwebcrypto.so.gz", + "downloadSha256": "b6c559ec71323f4859ca3eb61cc4de862eb1a70e3614dfca9b76cc8f75da1781", "delivery": "bundle", "asset": "webcrypto.dart" } ] }, "android-arm64": { - "runner": "ubuntu22 20261026.341.1", + "runner": "ubuntu22 20261101.347.1", "toolchain": "Android NDK 28.2.13676358, cmake version 3.31.6", "minOSVersion": 24, "files": [ { "name": "libwebcrypto.so", - "sha256": "1c5a0d65dda7f9e2a82136ffee383ac1c8a4c82819f296e795c51f401897bd61", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-fdcb040a60a7f049/android-arm64-libwebcrypto.so.gz", - "downloadSha256": "77353669977c1907fed1c1eb0177535f0b1a7a00df0a91773a7ab69171807598", + "sha256": "f04df3e0e2eb2776307f68108f66cc8065ac8d35f7e054d0ed4a747f7b29bd29", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-915dcd84d3b5df6a/android-arm64-libwebcrypto.so.gz", + "downloadSha256": "eac3f55e2619cc1a2adeb237dbe07de0585f39a56fa3bc9079bf8f1c2f0ac9a6", "delivery": "bundle", "asset": "webcrypto.dart" } ] }, "android-x64": { - "runner": "ubuntu22 20261026.341.1", + "runner": "ubuntu22 20261101.347.1", "toolchain": "Android NDK 28.2.13676358, cmake version 3.31.6", "minOSVersion": 24, "files": [ { "name": "libwebcrypto.so", - "sha256": "6e60993bffb29877dfc19073752175f2857a5757f04c08db4637c789d75f3920", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-fdcb040a60a7f049/android-x64-libwebcrypto.so.gz", - "downloadSha256": "ea82b803d2b2efd27dcdf2b890afd508cfbb1c9efe6ab809817dd5f7c9a89d31", + "sha256": "a23d0adc86f83c10e559831e0506aa72e646e41518294fbb3ec6842a031e003a", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-915dcd84d3b5df6a/android-x64-libwebcrypto.so.gz", + "downloadSha256": "f199e8083bb600227fed8830b54412860f4016390a36aa434ffc29a588f6d483", "delivery": "bundle", "asset": "webcrypto.dart" } ] }, "ios-arm64-iphoneos": { - "runner": "macos15 20261028.0489.1", - "toolchain": "Xcode 16.4, cmake version 4.4.4", + "runner": "macos15 20261104.0514.1", + "toolchain": "Xcode 16.4, cmake version 4.4.5", "minOSVersion": 13, "files": [ { "name": "libwebcrypto.dylib", "sha256": "a6d431ff20127a247a09838dbbce3bc6e1679e819cb42cb14ac4349db3c4ac8c", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-fdcb040a60a7f049/ios-arm64-iphoneos-libwebcrypto.dylib.gz", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-915dcd84d3b5df6a/ios-arm64-iphoneos-libwebcrypto.dylib.gz", "downloadSha256": "0b9581badbbb595cb07f24dcae3bce684d3121b17e1430bc5c3208b9d03a62d5", "delivery": "bundle", "asset": "webcrypto.dart" @@ -64,14 +64,14 @@ ] }, "ios-arm64-iphonesimulator": { - "runner": "macos15 20261028.0489.1", - "toolchain": "Xcode 16.4, cmake version 4.4.4", + "runner": "macos15 20261104.0514.1", + "toolchain": "Xcode 16.4, cmake version 4.4.5", "minOSVersion": 13, "files": [ { "name": "libwebcrypto.dylib", "sha256": "90a20819380cc4871187eb3256f54c9d880724dc0f587b1b72916eec2e074661", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-fdcb040a60a7f049/ios-arm64-iphonesimulator-libwebcrypto.dylib.gz", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-915dcd84d3b5df6a/ios-arm64-iphonesimulator-libwebcrypto.dylib.gz", "downloadSha256": "546095fb6c68b2d81e2c52d833e98340f61a3ecb7836ebf0b9f16e03404434b4", "delivery": "bundle", "asset": "webcrypto.dart" @@ -79,14 +79,14 @@ ] }, "ios-x64-iphonesimulator": { - "runner": "macos15 20261028.0489.1", - "toolchain": "Xcode 16.4, cmake version 4.4.4", + "runner": "macos15 20261104.0514.1", + "toolchain": "Xcode 16.4, cmake version 4.4.5", "minOSVersion": 13, "files": [ { "name": "libwebcrypto.dylib", "sha256": "4829448997180cd6ea174b7d236bf882f16dbc487f2e135454c8146afffe5833", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-fdcb040a60a7f049/ios-x64-iphonesimulator-libwebcrypto.dylib.gz", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-915dcd84d3b5df6a/ios-x64-iphonesimulator-libwebcrypto.dylib.gz", "downloadSha256": "39d0666c37b9ec68b5685efc55d9bc42024a203d4e4bbafb7fa08a53845931d0", "delivery": "bundle", "asset": "webcrypto.dart" @@ -94,13 +94,13 @@ ] }, "linux-arm64": { - "runner": "ubuntu22-arm64 20261026.173.1", + "runner": "ubuntu22-arm64 20261101.179.1", "toolchain": "gcc (Ubuntu 11.4.0-1ubuntu1~22.04.3) 11.4.0, cmake version 3.31.6", "files": [ { "name": "libwebcrypto.so", "sha256": "65db805f157adabdf83095915dd7e317b3479e1148d6941d9c1b3c28ad568404", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-fdcb040a60a7f049/linux-arm64-libwebcrypto.so.gz", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-915dcd84d3b5df6a/linux-arm64-libwebcrypto.so.gz", "downloadSha256": "a21a12811fdcfcdaa2265e8abed00934ca02787ee7e0a54f15ba9077ecac9dec", "delivery": "bundle", "asset": "webcrypto.dart" @@ -114,7 +114,7 @@ { "name": "libwebcrypto.so", "sha256": "6e25a4ad41d21971e8a223e95674a97fc8a3e4a212ba0f011d82d711346c5818", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-fdcb040a60a7f049/linux-x64-libwebcrypto.so.gz", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-915dcd84d3b5df6a/linux-x64-libwebcrypto.so.gz", "downloadSha256": "5e06e65b8b3e8da741989ae77ca8994f5bd105394f32044c8b292add46993ca7", "delivery": "bundle", "asset": "webcrypto.dart" @@ -122,14 +122,14 @@ ] }, "macos-arm64": { - "runner": "macos15 20261028.0489.1", - "toolchain": "Xcode 16.4, cmake version 4.4.4", + "runner": "macos15 20261104.0514.1", + "toolchain": "Xcode 16.4, cmake version 4.4.5", "minOSVersion": 12, "files": [ { "name": "libwebcrypto.dylib", "sha256": "89d60e9dac9a0850e064cc7c935f1ff4568ec9a984a3fe60a3e4363429569d5f", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-fdcb040a60a7f049/macos-arm64-libwebcrypto.dylib.gz", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-915dcd84d3b5df6a/macos-arm64-libwebcrypto.dylib.gz", "downloadSha256": "ee3e95c18cf000f705da601d43b8e7d78b6ae1288794c949a5654273c1566ae8", "delivery": "bundle", "asset": "webcrypto.dart" @@ -137,14 +137,14 @@ ] }, "macos-x64": { - "runner": "macos15 20261028.0489.1", - "toolchain": "Xcode 16.4, cmake version 4.4.4", + "runner": "macos15 20261104.0514.1", + "toolchain": "Xcode 16.4, cmake version 4.4.5", "minOSVersion": 12, "files": [ { "name": "libwebcrypto.dylib", "sha256": "f574c753d80a21f3e1938a813cf4d18bd9968909ed2462648fecb1c69d4c7c42", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-fdcb040a60a7f049/macos-x64-libwebcrypto.dylib.gz", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-915dcd84d3b5df6a/macos-x64-libwebcrypto.dylib.gz", "downloadSha256": "666e0a346d13fac9341395353e3d2c10ddff1f4ca144c0b63cefcd37c9da42a4", "delivery": "bundle", "asset": "webcrypto.dart" @@ -157,23 +157,23 @@ "files": [ { "name": "webcrypto.dll", - "sha256": "19ee49b80dbb701d85c91798bd27419425f7dcd17bd9518c14905d153f471fa3", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-fdcb040a60a7f049/windows-arm64-webcrypto.dll.gz", - "downloadSha256": "0fe8569cbee890535f50eef4fb2c16a0bca32c9310597e25c2ca6816ee1a8f73", + "sha256": "ef2b36af531f727ee6c15f5e3cfd465dc80a55d4c0a61a5485eea89f74cf9f16", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-915dcd84d3b5df6a/windows-arm64-webcrypto.dll.gz", + "downloadSha256": "8bc4569b3494bc4679e5c9d0c3ed5343bb3e51c1bb249802f65a8364828e3b0b", "delivery": "bundle", "asset": "webcrypto.dart" } ] }, "windows-x64": { - "runner": "win22 20261026.347.1", - "toolchain": "Visual Studio 17.14.43, cmake version 3.31.6, NASM 2.16.03", + "runner": "win22 20261101.353.1", + "toolchain": "Visual Studio 17.14.44, cmake version 3.31.6, NASM 2.16.03", "files": [ { "name": "webcrypto.dll", - "sha256": "271f8206d6d7689e3986daac1a6cfa533d95f1d52dd20949cf7df0d045ac38ad", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-fdcb040a60a7f049/windows-x64-webcrypto.dll.gz", - "downloadSha256": "6b54fc51e200f789997c9a2bfbf523c0b2691efe1423b0ad55fa71432240d039", + "sha256": "a45441803199f21f783a2b6711f52b2c84a5be5e4ea553eaf2e11962192807b3", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-915dcd84d3b5df6a/windows-x64-webcrypto.dll.gz", + "downloadSha256": "c50d5edc7dc549767e854d79f6cfe9df7f521902f74c65949a1eed40bc829ae9", "delivery": "bundle", "asset": "webcrypto.dart" } From 19875961ba98029c5ee9cd9eb19824a48e8c7b47 Mon Sep 17 00:00:00 2001 From: James O'Leary Date: Sat, 7 Nov 2026 04:08:05 -0800 Subject: [PATCH 49/69] ci: Telosnex/telosnex_ci_tools v0.5.9 --- .github/workflows/integration.yml | 14 +++++++------- .github/workflows/kiln_check.yml | 2 +- .github/workflows/kiln_release.yml | 2 +- .github/workflows/roll_boringssl.yml | 2 +- .github/workflows/unit.yml | 4 ++-- .github/workflows/upstream_watch.yml | 2 +- 6 files changed, 13 insertions(+), 13 deletions(-) diff --git a/.github/workflows/integration.yml b/.github/workflows/integration.yml index 7d6c7c5ef..f038af476 100644 --- a/.github/workflows/integration.yml +++ b/.github/workflows/integration.yml @@ -36,7 +36,7 @@ jobs: run: | sudo apt-get update -qq sudo apt-get install -y -qq clang cmake ninja-build pkg-config libgtk-3-dev xvfb > /dev/null - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@4a798518378fb0c362ebe42940227560e6b4ecde # v0.5.8 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@46806695c593ec632eb26996046ea89649a1a6f7 # v0.5.9 - run: flutter pub get - run: dart test --exclude-tags browser-interop -p vm -r expanded - name: Demo app integration test @@ -99,9 +99,9 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@4a798518378fb0c362ebe42940227560e6b4ecde # v0.5.8 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@46806695c593ec632eb26996046ea89649a1a6f7 # v0.5.9 - run: flutter pub get - - uses: Telosnex/telosnex_ci_tools/actions/dart-test-browser@4a798518378fb0c362ebe42940227560e6b4ecde # v0.5.8 + - uses: Telosnex/telosnex_ci_tools/actions/dart-test-browser@46806695c593ec632eb26996046ea89649a1a6f7 # v0.5.9 with: platform: ${{ env.BROWSER }} compiler: ${{ env.COMPILER }} @@ -151,9 +151,9 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@4a798518378fb0c362ebe42940227560e6b4ecde # v0.5.8 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@46806695c593ec632eb26996046ea89649a1a6f7 # v0.5.9 - run: flutter pub get - - uses: Telosnex/telosnex_ci_tools/actions/ios-flutter-test@4a798518378fb0c362ebe42940227560e6b4ecde # v0.5.8 + - uses: Telosnex/telosnex_ci_tools/actions/ios-flutter-test@46806695c593ec632eb26996046ea89649a1a6f7 # v0.5.9 with: working-directory: example/webcrypto_demo_flutter_app target: integration_test/webcrypto_test.dart @@ -167,9 +167,9 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@4a798518378fb0c362ebe42940227560e6b4ecde # v0.5.8 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@46806695c593ec632eb26996046ea89649a1a6f7 # v0.5.9 - run: flutter pub get - - uses: Telosnex/telosnex_ci_tools/actions/android-emulator@4a798518378fb0c362ebe42940227560e6b4ecde # v0.5.8 + - uses: Telosnex/telosnex_ci_tools/actions/android-emulator@46806695c593ec632eb26996046ea89649a1a6f7 # v0.5.9 id: emulator with: api-level: '36' diff --git a/.github/workflows/kiln_check.yml b/.github/workflows/kiln_check.yml index 332fa2b83..6784c3ca7 100644 --- a/.github/workflows/kiln_check.yml +++ b/.github/workflows/kiln_check.yml @@ -16,4 +16,4 @@ permissions: jobs: kiln-check: - uses: Telosnex/telosnex_ci_tools/.github/workflows/kiln_check.yml@4a798518378fb0c362ebe42940227560e6b4ecde # v0.5.8 + uses: Telosnex/telosnex_ci_tools/.github/workflows/kiln_check.yml@46806695c593ec632eb26996046ea89649a1a6f7 # v0.5.9 diff --git a/.github/workflows/kiln_release.yml b/.github/workflows/kiln_release.yml index a6cee7217..9d1392aab 100644 --- a/.github/workflows/kiln_release.yml +++ b/.github/workflows/kiln_release.yml @@ -25,7 +25,7 @@ permissions: jobs: kiln-release: - uses: Telosnex/telosnex_ci_tools/.github/workflows/kiln_release.yml@4a798518378fb0c362ebe42940227560e6b4ecde # v0.5.8 + uses: Telosnex/telosnex_ci_tools/.github/workflows/kiln_release.yml@46806695c593ec632eb26996046ea89649a1a6f7 # v0.5.9 permissions: contents: write with: diff --git a/.github/workflows/roll_boringssl.yml b/.github/workflows/roll_boringssl.yml index 87d711dcd..994ad1845 100644 --- a/.github/workflows/roll_boringssl.yml +++ b/.github/workflows/roll_boringssl.yml @@ -28,7 +28,7 @@ jobs: with: fetch-depth: 0 - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@4a798518378fb0c362ebe42940227560e6b4ecde # v0.5.8 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@46806695c593ec632eb26996046ea89649a1a6f7 # v0.5.9 - name: Install system dependencies run: | diff --git a/.github/workflows/unit.yml b/.github/workflows/unit.yml index aaec1ae78..445cb61d9 100644 --- a/.github/workflows/unit.yml +++ b/.github/workflows/unit.yml @@ -25,8 +25,8 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@4a798518378fb0c362ebe42940227560e6b4ecde # v0.5.8 - - uses: Telosnex/telosnex_ci_tools/actions/lint@4a798518378fb0c362ebe42940227560e6b4ecde # v0.5.8 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@46806695c593ec632eb26996046ea89649a1a6f7 # v0.5.9 + - uses: Telosnex/telosnex_ci_tools/actions/lint@46806695c593ec632eb26996046ea89649a1a6f7 # v0.5.9 - run: flutter pub get - run: dart format --output=none --set-exit-if-changed . - run: dart analyze --fatal-warnings . diff --git a/.github/workflows/upstream_watch.yml b/.github/workflows/upstream_watch.yml index 094bacd64..edb132abc 100644 --- a/.github/workflows/upstream_watch.yml +++ b/.github/workflows/upstream_watch.yml @@ -16,4 +16,4 @@ permissions: jobs: watch: - uses: Telosnex/telosnex_ci_tools/.github/workflows/upstream_watch.yml@4a798518378fb0c362ebe42940227560e6b4ecde # v0.5.8 + uses: Telosnex/telosnex_ci_tools/.github/workflows/upstream_watch.yml@46806695c593ec632eb26996046ea89649a1a6f7 # v0.5.9 From ac90647e05a7efd1fafbcfd1e89c41deb81673b8 Mon Sep 17 00:00:00 2001 From: James O'Leary Date: Sat, 7 Nov 2026 04:21:26 -0800 Subject: [PATCH 50/69] ci: Telosnex/telosnex_ci_tools v0.5.10 --- .github/workflows/integration.yml | 14 +++++++------- .github/workflows/kiln_check.yml | 2 +- .github/workflows/kiln_release.yml | 2 +- .github/workflows/roll_boringssl.yml | 2 +- .github/workflows/unit.yml | 4 ++-- .github/workflows/upstream_watch.yml | 2 +- 6 files changed, 13 insertions(+), 13 deletions(-) diff --git a/.github/workflows/integration.yml b/.github/workflows/integration.yml index f038af476..5ddd9bf6b 100644 --- a/.github/workflows/integration.yml +++ b/.github/workflows/integration.yml @@ -36,7 +36,7 @@ jobs: run: | sudo apt-get update -qq sudo apt-get install -y -qq clang cmake ninja-build pkg-config libgtk-3-dev xvfb > /dev/null - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@46806695c593ec632eb26996046ea89649a1a6f7 # v0.5.9 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@578473461fc30c68c68fc6e07941dcfc2c070b54 # v0.5.10 - run: flutter pub get - run: dart test --exclude-tags browser-interop -p vm -r expanded - name: Demo app integration test @@ -99,9 +99,9 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@46806695c593ec632eb26996046ea89649a1a6f7 # v0.5.9 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@578473461fc30c68c68fc6e07941dcfc2c070b54 # v0.5.10 - run: flutter pub get - - uses: Telosnex/telosnex_ci_tools/actions/dart-test-browser@46806695c593ec632eb26996046ea89649a1a6f7 # v0.5.9 + - uses: Telosnex/telosnex_ci_tools/actions/dart-test-browser@578473461fc30c68c68fc6e07941dcfc2c070b54 # v0.5.10 with: platform: ${{ env.BROWSER }} compiler: ${{ env.COMPILER }} @@ -151,9 +151,9 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@46806695c593ec632eb26996046ea89649a1a6f7 # v0.5.9 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@578473461fc30c68c68fc6e07941dcfc2c070b54 # v0.5.10 - run: flutter pub get - - uses: Telosnex/telosnex_ci_tools/actions/ios-flutter-test@46806695c593ec632eb26996046ea89649a1a6f7 # v0.5.9 + - uses: Telosnex/telosnex_ci_tools/actions/ios-flutter-test@578473461fc30c68c68fc6e07941dcfc2c070b54 # v0.5.10 with: working-directory: example/webcrypto_demo_flutter_app target: integration_test/webcrypto_test.dart @@ -167,9 +167,9 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@46806695c593ec632eb26996046ea89649a1a6f7 # v0.5.9 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@578473461fc30c68c68fc6e07941dcfc2c070b54 # v0.5.10 - run: flutter pub get - - uses: Telosnex/telosnex_ci_tools/actions/android-emulator@46806695c593ec632eb26996046ea89649a1a6f7 # v0.5.9 + - uses: Telosnex/telosnex_ci_tools/actions/android-emulator@578473461fc30c68c68fc6e07941dcfc2c070b54 # v0.5.10 id: emulator with: api-level: '36' diff --git a/.github/workflows/kiln_check.yml b/.github/workflows/kiln_check.yml index 6784c3ca7..f9ce46056 100644 --- a/.github/workflows/kiln_check.yml +++ b/.github/workflows/kiln_check.yml @@ -16,4 +16,4 @@ permissions: jobs: kiln-check: - uses: Telosnex/telosnex_ci_tools/.github/workflows/kiln_check.yml@46806695c593ec632eb26996046ea89649a1a6f7 # v0.5.9 + uses: Telosnex/telosnex_ci_tools/.github/workflows/kiln_check.yml@578473461fc30c68c68fc6e07941dcfc2c070b54 # v0.5.10 diff --git a/.github/workflows/kiln_release.yml b/.github/workflows/kiln_release.yml index 9d1392aab..05bc27e62 100644 --- a/.github/workflows/kiln_release.yml +++ b/.github/workflows/kiln_release.yml @@ -25,7 +25,7 @@ permissions: jobs: kiln-release: - uses: Telosnex/telosnex_ci_tools/.github/workflows/kiln_release.yml@46806695c593ec632eb26996046ea89649a1a6f7 # v0.5.9 + uses: Telosnex/telosnex_ci_tools/.github/workflows/kiln_release.yml@578473461fc30c68c68fc6e07941dcfc2c070b54 # v0.5.10 permissions: contents: write with: diff --git a/.github/workflows/roll_boringssl.yml b/.github/workflows/roll_boringssl.yml index 994ad1845..cd9a97d42 100644 --- a/.github/workflows/roll_boringssl.yml +++ b/.github/workflows/roll_boringssl.yml @@ -28,7 +28,7 @@ jobs: with: fetch-depth: 0 - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@46806695c593ec632eb26996046ea89649a1a6f7 # v0.5.9 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@578473461fc30c68c68fc6e07941dcfc2c070b54 # v0.5.10 - name: Install system dependencies run: | diff --git a/.github/workflows/unit.yml b/.github/workflows/unit.yml index 445cb61d9..ff86b3e9b 100644 --- a/.github/workflows/unit.yml +++ b/.github/workflows/unit.yml @@ -25,8 +25,8 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@46806695c593ec632eb26996046ea89649a1a6f7 # v0.5.9 - - uses: Telosnex/telosnex_ci_tools/actions/lint@46806695c593ec632eb26996046ea89649a1a6f7 # v0.5.9 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@578473461fc30c68c68fc6e07941dcfc2c070b54 # v0.5.10 + - uses: Telosnex/telosnex_ci_tools/actions/lint@578473461fc30c68c68fc6e07941dcfc2c070b54 # v0.5.10 - run: flutter pub get - run: dart format --output=none --set-exit-if-changed . - run: dart analyze --fatal-warnings . diff --git a/.github/workflows/upstream_watch.yml b/.github/workflows/upstream_watch.yml index edb132abc..f14258d0b 100644 --- a/.github/workflows/upstream_watch.yml +++ b/.github/workflows/upstream_watch.yml @@ -16,4 +16,4 @@ permissions: jobs: watch: - uses: Telosnex/telosnex_ci_tools/.github/workflows/upstream_watch.yml@46806695c593ec632eb26996046ea89649a1a6f7 # v0.5.9 + uses: Telosnex/telosnex_ci_tools/.github/workflows/upstream_watch.yml@578473461fc30c68c68fc6e07941dcfc2c070b54 # v0.5.10 From 35f83f8dfc41d3a515c87acd4d074af59a30843a Mon Sep 17 00:00:00 2001 From: James O'Leary Date: Fri, 6 Nov 2026 19:37:49 -0800 Subject: [PATCH 51/69] kiln ^1.0.0 --- pubspec.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pubspec.yaml b/pubspec.yaml index 7bfc08a04..06cbce71f 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -25,7 +25,7 @@ dependencies: ffi: ^2.0.0 hooks: ^2.2.0 logging: ^1.3.0 - kiln: ^0.2.0 + kiln: ^1.0.0 native_toolchain_cmake: ^0.3.2 path: ^1.9.0 code_assets: ^2.2.0 From 60e2d93071315f0f4ed001b88a5d6aa30e127d8c Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Sat, 7 Nov 2026 03:44:20 +0000 Subject: [PATCH 52/69] kiln: manifest of release native-badf642a3e6bb56a --- native_artifacts/prebuilt.json | 48 +++++++++++++++++----------------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/native_artifacts/prebuilt.json b/native_artifacts/prebuilt.json index 0d64b3f31..73f204e5f 100644 --- a/native_artifacts/prebuilt.json +++ b/native_artifacts/prebuilt.json @@ -1,7 +1,7 @@ { "schema": 1, - "sourceKey": "915dcd84d3b5df6a9a76eff7605a9aa70622bbbfaa83fcd9b82b9335dac6761f", - "release": "https://github.com/Telosnex/webcrypto.dart/releases/tag/native-915dcd84d3b5df6a", + "sourceKey": "badf642a3e6bb56abf600f57e445c30c4cd1768b28072cce58d11cca040aaca4", + "release": "https://github.com/Telosnex/webcrypto.dart/releases/tag/native-badf642a3e6bb56a", "targets": { "android-arm": { "runner": "ubuntu22 20261101.347.1", @@ -10,9 +10,9 @@ "files": [ { "name": "libwebcrypto.so", - "sha256": "4f4b6345ad7469b8ea8fe732f756e74ad7b7dc58125a5ce39b656d110cfbb160", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-915dcd84d3b5df6a/android-arm-libwebcrypto.so.gz", - "downloadSha256": "b6c559ec71323f4859ca3eb61cc4de862eb1a70e3614dfca9b76cc8f75da1781", + "sha256": "a62d1726221bdacd501df5268e8cbf29434bfd3ceda9c29b4b52f30b54c5d448", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-badf642a3e6bb56a/android-arm-libwebcrypto.so.gz", + "downloadSha256": "f022f45d831f3a6a42825e1935d59890c24e6bc46911bc933fc7a2edccdeda59", "delivery": "bundle", "asset": "webcrypto.dart" } @@ -25,9 +25,9 @@ "files": [ { "name": "libwebcrypto.so", - "sha256": "f04df3e0e2eb2776307f68108f66cc8065ac8d35f7e054d0ed4a747f7b29bd29", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-915dcd84d3b5df6a/android-arm64-libwebcrypto.so.gz", - "downloadSha256": "eac3f55e2619cc1a2adeb237dbe07de0585f39a56fa3bc9079bf8f1c2f0ac9a6", + "sha256": "7a47f0e2f3ed10c9d44d89ba54ae7c84fb6e3c92dd5a63ec146eb4d01b1d199d", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-badf642a3e6bb56a/android-arm64-libwebcrypto.so.gz", + "downloadSha256": "0341ff6bc63d1c1ae507745c93c57f2916cb81888e7ed38cb8cbb8629987b5dd", "delivery": "bundle", "asset": "webcrypto.dart" } @@ -40,9 +40,9 @@ "files": [ { "name": "libwebcrypto.so", - "sha256": "a23d0adc86f83c10e559831e0506aa72e646e41518294fbb3ec6842a031e003a", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-915dcd84d3b5df6a/android-x64-libwebcrypto.so.gz", - "downloadSha256": "f199e8083bb600227fed8830b54412860f4016390a36aa434ffc29a588f6d483", + "sha256": "80b29430b56eb16118cbd735acbe295515c4555da8731dc5daf434646e673314", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-badf642a3e6bb56a/android-x64-libwebcrypto.so.gz", + "downloadSha256": "d79173a72d2bc8b423691af7bbdaf55050aed662ffdb3a84f083a1efd9cb34af", "delivery": "bundle", "asset": "webcrypto.dart" } @@ -56,7 +56,7 @@ { "name": "libwebcrypto.dylib", "sha256": "a6d431ff20127a247a09838dbbce3bc6e1679e819cb42cb14ac4349db3c4ac8c", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-915dcd84d3b5df6a/ios-arm64-iphoneos-libwebcrypto.dylib.gz", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-badf642a3e6bb56a/ios-arm64-iphoneos-libwebcrypto.dylib.gz", "downloadSha256": "0b9581badbbb595cb07f24dcae3bce684d3121b17e1430bc5c3208b9d03a62d5", "delivery": "bundle", "asset": "webcrypto.dart" @@ -71,7 +71,7 @@ { "name": "libwebcrypto.dylib", "sha256": "90a20819380cc4871187eb3256f54c9d880724dc0f587b1b72916eec2e074661", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-915dcd84d3b5df6a/ios-arm64-iphonesimulator-libwebcrypto.dylib.gz", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-badf642a3e6bb56a/ios-arm64-iphonesimulator-libwebcrypto.dylib.gz", "downloadSha256": "546095fb6c68b2d81e2c52d833e98340f61a3ecb7836ebf0b9f16e03404434b4", "delivery": "bundle", "asset": "webcrypto.dart" @@ -86,7 +86,7 @@ { "name": "libwebcrypto.dylib", "sha256": "4829448997180cd6ea174b7d236bf882f16dbc487f2e135454c8146afffe5833", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-915dcd84d3b5df6a/ios-x64-iphonesimulator-libwebcrypto.dylib.gz", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-badf642a3e6bb56a/ios-x64-iphonesimulator-libwebcrypto.dylib.gz", "downloadSha256": "39d0666c37b9ec68b5685efc55d9bc42024a203d4e4bbafb7fa08a53845931d0", "delivery": "bundle", "asset": "webcrypto.dart" @@ -100,7 +100,7 @@ { "name": "libwebcrypto.so", "sha256": "65db805f157adabdf83095915dd7e317b3479e1148d6941d9c1b3c28ad568404", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-915dcd84d3b5df6a/linux-arm64-libwebcrypto.so.gz", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-badf642a3e6bb56a/linux-arm64-libwebcrypto.so.gz", "downloadSha256": "a21a12811fdcfcdaa2265e8abed00934ca02787ee7e0a54f15ba9077ecac9dec", "delivery": "bundle", "asset": "webcrypto.dart" @@ -114,7 +114,7 @@ { "name": "libwebcrypto.so", "sha256": "6e25a4ad41d21971e8a223e95674a97fc8a3e4a212ba0f011d82d711346c5818", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-915dcd84d3b5df6a/linux-x64-libwebcrypto.so.gz", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-badf642a3e6bb56a/linux-x64-libwebcrypto.so.gz", "downloadSha256": "5e06e65b8b3e8da741989ae77ca8994f5bd105394f32044c8b292add46993ca7", "delivery": "bundle", "asset": "webcrypto.dart" @@ -129,7 +129,7 @@ { "name": "libwebcrypto.dylib", "sha256": "89d60e9dac9a0850e064cc7c935f1ff4568ec9a984a3fe60a3e4363429569d5f", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-915dcd84d3b5df6a/macos-arm64-libwebcrypto.dylib.gz", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-badf642a3e6bb56a/macos-arm64-libwebcrypto.dylib.gz", "downloadSha256": "ee3e95c18cf000f705da601d43b8e7d78b6ae1288794c949a5654273c1566ae8", "delivery": "bundle", "asset": "webcrypto.dart" @@ -144,7 +144,7 @@ { "name": "libwebcrypto.dylib", "sha256": "f574c753d80a21f3e1938a813cf4d18bd9968909ed2462648fecb1c69d4c7c42", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-915dcd84d3b5df6a/macos-x64-libwebcrypto.dylib.gz", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-badf642a3e6bb56a/macos-x64-libwebcrypto.dylib.gz", "downloadSha256": "666e0a346d13fac9341395353e3d2c10ddff1f4ca144c0b63cefcd37c9da42a4", "delivery": "bundle", "asset": "webcrypto.dart" @@ -157,9 +157,9 @@ "files": [ { "name": "webcrypto.dll", - "sha256": "ef2b36af531f727ee6c15f5e3cfd465dc80a55d4c0a61a5485eea89f74cf9f16", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-915dcd84d3b5df6a/windows-arm64-webcrypto.dll.gz", - "downloadSha256": "8bc4569b3494bc4679e5c9d0c3ed5343bb3e51c1bb249802f65a8364828e3b0b", + "sha256": "a100165fd2fe5e71721971dc76754b2d0d7607f45e8da1388f181a2f4ed77d8e", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-badf642a3e6bb56a/windows-arm64-webcrypto.dll.gz", + "downloadSha256": "9dfd4701603767caaeefb4218abda3761e3e4179cea7f7a1faf7d86209f90e93", "delivery": "bundle", "asset": "webcrypto.dart" } @@ -171,9 +171,9 @@ "files": [ { "name": "webcrypto.dll", - "sha256": "a45441803199f21f783a2b6711f52b2c84a5be5e4ea553eaf2e11962192807b3", - "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-915dcd84d3b5df6a/windows-x64-webcrypto.dll.gz", - "downloadSha256": "c50d5edc7dc549767e854d79f6cfe9df7f521902f74c65949a1eed40bc829ae9", + "sha256": "bca4f8fc43b7d9148e56f568be6d00690ebf233e77a1dfb9dfd92ee4ef426128", + "url": "https://github.com/Telosnex/webcrypto.dart/releases/download/native-badf642a3e6bb56a/windows-x64-webcrypto.dll.gz", + "downloadSha256": "5411af532385009ecda240646bf85e552829ef522df19f6ae441e4b7f104fba1", "delivery": "bundle", "asset": "webcrypto.dart" } From bf452c50d031558b6aa7212bdbcc1a4438854320 Mon Sep 17 00:00:00 2001 From: James O'Leary Date: Sun, 8 Nov 2026 17:23:41 -0800 Subject: [PATCH 53/69] CI: drop the macOS x64 job Telosnex does not support Intel Macs. The job failed more often than any other (slow, contended macos-15-intel runners), and the ARM64 job still builds the app. --- .github/workflows/integration.yml | 8 -------- 1 file changed, 8 deletions(-) diff --git a/.github/workflows/integration.yml b/.github/workflows/integration.yml index 5ddd9bf6b..37bd86697 100644 --- a/.github/workflows/integration.yml +++ b/.github/workflows/integration.yml @@ -63,14 +63,6 @@ jobs: DEVICE: macos steps: *native_steps - desktop-macos-x64: - name: macOS x64 - runs-on: macos-15-intel - timeout-minutes: 40 - env: - DEVICE: macos - steps: *native_steps - desktop-windows-x64: name: Windows x64 runs-on: windows-2025 From b9f9bab9b33f90d14a6751bfaca728319ccb2376 Mon Sep 17 00:00:00 2001 From: James O'Leary Date: Sun, 8 Nov 2026 17:51:37 -0800 Subject: [PATCH 54/69] ci: Telosnex/telosnex_ci_tools v0.5.11 --- .github/workflows/integration.yml | 14 +++++++------- .github/workflows/kiln_check.yml | 2 +- .github/workflows/kiln_release.yml | 2 +- .github/workflows/roll_boringssl.yml | 2 +- .github/workflows/unit.yml | 4 ++-- .github/workflows/upstream_watch.yml | 2 +- 6 files changed, 13 insertions(+), 13 deletions(-) diff --git a/.github/workflows/integration.yml b/.github/workflows/integration.yml index 37bd86697..b5c685cdc 100644 --- a/.github/workflows/integration.yml +++ b/.github/workflows/integration.yml @@ -36,7 +36,7 @@ jobs: run: | sudo apt-get update -qq sudo apt-get install -y -qq clang cmake ninja-build pkg-config libgtk-3-dev xvfb > /dev/null - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@578473461fc30c68c68fc6e07941dcfc2c070b54 # v0.5.10 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@6297b4dbe04372d8f73b1ab775afd16f938c6e23 # v0.5.11 - run: flutter pub get - run: dart test --exclude-tags browser-interop -p vm -r expanded - name: Demo app integration test @@ -91,9 +91,9 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@578473461fc30c68c68fc6e07941dcfc2c070b54 # v0.5.10 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@6297b4dbe04372d8f73b1ab775afd16f938c6e23 # v0.5.11 - run: flutter pub get - - uses: Telosnex/telosnex_ci_tools/actions/dart-test-browser@578473461fc30c68c68fc6e07941dcfc2c070b54 # v0.5.10 + - uses: Telosnex/telosnex_ci_tools/actions/dart-test-browser@6297b4dbe04372d8f73b1ab775afd16f938c6e23 # v0.5.11 with: platform: ${{ env.BROWSER }} compiler: ${{ env.COMPILER }} @@ -143,9 +143,9 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@578473461fc30c68c68fc6e07941dcfc2c070b54 # v0.5.10 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@6297b4dbe04372d8f73b1ab775afd16f938c6e23 # v0.5.11 - run: flutter pub get - - uses: Telosnex/telosnex_ci_tools/actions/ios-flutter-test@578473461fc30c68c68fc6e07941dcfc2c070b54 # v0.5.10 + - uses: Telosnex/telosnex_ci_tools/actions/ios-flutter-test@6297b4dbe04372d8f73b1ab775afd16f938c6e23 # v0.5.11 with: working-directory: example/webcrypto_demo_flutter_app target: integration_test/webcrypto_test.dart @@ -159,9 +159,9 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@578473461fc30c68c68fc6e07941dcfc2c070b54 # v0.5.10 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@6297b4dbe04372d8f73b1ab775afd16f938c6e23 # v0.5.11 - run: flutter pub get - - uses: Telosnex/telosnex_ci_tools/actions/android-emulator@578473461fc30c68c68fc6e07941dcfc2c070b54 # v0.5.10 + - uses: Telosnex/telosnex_ci_tools/actions/android-emulator@6297b4dbe04372d8f73b1ab775afd16f938c6e23 # v0.5.11 id: emulator with: api-level: '36' diff --git a/.github/workflows/kiln_check.yml b/.github/workflows/kiln_check.yml index f9ce46056..d31407af8 100644 --- a/.github/workflows/kiln_check.yml +++ b/.github/workflows/kiln_check.yml @@ -16,4 +16,4 @@ permissions: jobs: kiln-check: - uses: Telosnex/telosnex_ci_tools/.github/workflows/kiln_check.yml@578473461fc30c68c68fc6e07941dcfc2c070b54 # v0.5.10 + uses: Telosnex/telosnex_ci_tools/.github/workflows/kiln_check.yml@6297b4dbe04372d8f73b1ab775afd16f938c6e23 # v0.5.11 diff --git a/.github/workflows/kiln_release.yml b/.github/workflows/kiln_release.yml index 05bc27e62..b7d0a9220 100644 --- a/.github/workflows/kiln_release.yml +++ b/.github/workflows/kiln_release.yml @@ -25,7 +25,7 @@ permissions: jobs: kiln-release: - uses: Telosnex/telosnex_ci_tools/.github/workflows/kiln_release.yml@578473461fc30c68c68fc6e07941dcfc2c070b54 # v0.5.10 + uses: Telosnex/telosnex_ci_tools/.github/workflows/kiln_release.yml@6297b4dbe04372d8f73b1ab775afd16f938c6e23 # v0.5.11 permissions: contents: write with: diff --git a/.github/workflows/roll_boringssl.yml b/.github/workflows/roll_boringssl.yml index cd9a97d42..18bc13a78 100644 --- a/.github/workflows/roll_boringssl.yml +++ b/.github/workflows/roll_boringssl.yml @@ -28,7 +28,7 @@ jobs: with: fetch-depth: 0 - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@578473461fc30c68c68fc6e07941dcfc2c070b54 # v0.5.10 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@6297b4dbe04372d8f73b1ab775afd16f938c6e23 # v0.5.11 - name: Install system dependencies run: | diff --git a/.github/workflows/unit.yml b/.github/workflows/unit.yml index ff86b3e9b..258b471cb 100644 --- a/.github/workflows/unit.yml +++ b/.github/workflows/unit.yml @@ -25,8 +25,8 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@578473461fc30c68c68fc6e07941dcfc2c070b54 # v0.5.10 - - uses: Telosnex/telosnex_ci_tools/actions/lint@578473461fc30c68c68fc6e07941dcfc2c070b54 # v0.5.10 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@6297b4dbe04372d8f73b1ab775afd16f938c6e23 # v0.5.11 + - uses: Telosnex/telosnex_ci_tools/actions/lint@6297b4dbe04372d8f73b1ab775afd16f938c6e23 # v0.5.11 - run: flutter pub get - run: dart format --output=none --set-exit-if-changed . - run: dart analyze --fatal-warnings . diff --git a/.github/workflows/upstream_watch.yml b/.github/workflows/upstream_watch.yml index f14258d0b..aa33207c0 100644 --- a/.github/workflows/upstream_watch.yml +++ b/.github/workflows/upstream_watch.yml @@ -16,4 +16,4 @@ permissions: jobs: watch: - uses: Telosnex/telosnex_ci_tools/.github/workflows/upstream_watch.yml@578473461fc30c68c68fc6e07941dcfc2c070b54 # v0.5.10 + uses: Telosnex/telosnex_ci_tools/.github/workflows/upstream_watch.yml@6297b4dbe04372d8f73b1ab775afd16f938c6e23 # v0.5.11 From bf978e0e1675789c23ac0fae3853056422e76b00 Mon Sep 17 00:00:00 2001 From: James O'Leary Date: Mon, 9 Nov 2026 07:57:40 -0800 Subject: [PATCH 55/69] ci: Telosnex/telosnex_ci_tools v0.5.12 --- .github/workflows/integration.yml | 14 +++++++------- .github/workflows/kiln_check.yml | 2 +- .github/workflows/kiln_release.yml | 2 +- .github/workflows/roll_boringssl.yml | 2 +- .github/workflows/unit.yml | 4 ++-- .github/workflows/upstream_watch.yml | 2 +- 6 files changed, 13 insertions(+), 13 deletions(-) diff --git a/.github/workflows/integration.yml b/.github/workflows/integration.yml index b5c685cdc..dc818577b 100644 --- a/.github/workflows/integration.yml +++ b/.github/workflows/integration.yml @@ -36,7 +36,7 @@ jobs: run: | sudo apt-get update -qq sudo apt-get install -y -qq clang cmake ninja-build pkg-config libgtk-3-dev xvfb > /dev/null - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@6297b4dbe04372d8f73b1ab775afd16f938c6e23 # v0.5.11 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@c356a8d59e643d5fc8c552950fdd45c7e3bfea7f # v0.5.12 - run: flutter pub get - run: dart test --exclude-tags browser-interop -p vm -r expanded - name: Demo app integration test @@ -91,9 +91,9 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@6297b4dbe04372d8f73b1ab775afd16f938c6e23 # v0.5.11 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@c356a8d59e643d5fc8c552950fdd45c7e3bfea7f # v0.5.12 - run: flutter pub get - - uses: Telosnex/telosnex_ci_tools/actions/dart-test-browser@6297b4dbe04372d8f73b1ab775afd16f938c6e23 # v0.5.11 + - uses: Telosnex/telosnex_ci_tools/actions/dart-test-browser@c356a8d59e643d5fc8c552950fdd45c7e3bfea7f # v0.5.12 with: platform: ${{ env.BROWSER }} compiler: ${{ env.COMPILER }} @@ -143,9 +143,9 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@6297b4dbe04372d8f73b1ab775afd16f938c6e23 # v0.5.11 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@c356a8d59e643d5fc8c552950fdd45c7e3bfea7f # v0.5.12 - run: flutter pub get - - uses: Telosnex/telosnex_ci_tools/actions/ios-flutter-test@6297b4dbe04372d8f73b1ab775afd16f938c6e23 # v0.5.11 + - uses: Telosnex/telosnex_ci_tools/actions/ios-flutter-test@c356a8d59e643d5fc8c552950fdd45c7e3bfea7f # v0.5.12 with: working-directory: example/webcrypto_demo_flutter_app target: integration_test/webcrypto_test.dart @@ -159,9 +159,9 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@6297b4dbe04372d8f73b1ab775afd16f938c6e23 # v0.5.11 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@c356a8d59e643d5fc8c552950fdd45c7e3bfea7f # v0.5.12 - run: flutter pub get - - uses: Telosnex/telosnex_ci_tools/actions/android-emulator@6297b4dbe04372d8f73b1ab775afd16f938c6e23 # v0.5.11 + - uses: Telosnex/telosnex_ci_tools/actions/android-emulator@c356a8d59e643d5fc8c552950fdd45c7e3bfea7f # v0.5.12 id: emulator with: api-level: '36' diff --git a/.github/workflows/kiln_check.yml b/.github/workflows/kiln_check.yml index d31407af8..9f1c26cdc 100644 --- a/.github/workflows/kiln_check.yml +++ b/.github/workflows/kiln_check.yml @@ -16,4 +16,4 @@ permissions: jobs: kiln-check: - uses: Telosnex/telosnex_ci_tools/.github/workflows/kiln_check.yml@6297b4dbe04372d8f73b1ab775afd16f938c6e23 # v0.5.11 + uses: Telosnex/telosnex_ci_tools/.github/workflows/kiln_check.yml@c356a8d59e643d5fc8c552950fdd45c7e3bfea7f # v0.5.12 diff --git a/.github/workflows/kiln_release.yml b/.github/workflows/kiln_release.yml index b7d0a9220..56e43e0d7 100644 --- a/.github/workflows/kiln_release.yml +++ b/.github/workflows/kiln_release.yml @@ -25,7 +25,7 @@ permissions: jobs: kiln-release: - uses: Telosnex/telosnex_ci_tools/.github/workflows/kiln_release.yml@6297b4dbe04372d8f73b1ab775afd16f938c6e23 # v0.5.11 + uses: Telosnex/telosnex_ci_tools/.github/workflows/kiln_release.yml@c356a8d59e643d5fc8c552950fdd45c7e3bfea7f # v0.5.12 permissions: contents: write with: diff --git a/.github/workflows/roll_boringssl.yml b/.github/workflows/roll_boringssl.yml index 18bc13a78..78bb13fd9 100644 --- a/.github/workflows/roll_boringssl.yml +++ b/.github/workflows/roll_boringssl.yml @@ -28,7 +28,7 @@ jobs: with: fetch-depth: 0 - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@6297b4dbe04372d8f73b1ab775afd16f938c6e23 # v0.5.11 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@c356a8d59e643d5fc8c552950fdd45c7e3bfea7f # v0.5.12 - name: Install system dependencies run: | diff --git a/.github/workflows/unit.yml b/.github/workflows/unit.yml index 258b471cb..e3e3601af 100644 --- a/.github/workflows/unit.yml +++ b/.github/workflows/unit.yml @@ -25,8 +25,8 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@6297b4dbe04372d8f73b1ab775afd16f938c6e23 # v0.5.11 - - uses: Telosnex/telosnex_ci_tools/actions/lint@6297b4dbe04372d8f73b1ab775afd16f938c6e23 # v0.5.11 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@c356a8d59e643d5fc8c552950fdd45c7e3bfea7f # v0.5.12 + - uses: Telosnex/telosnex_ci_tools/actions/lint@c356a8d59e643d5fc8c552950fdd45c7e3bfea7f # v0.5.12 - run: flutter pub get - run: dart format --output=none --set-exit-if-changed . - run: dart analyze --fatal-warnings . diff --git a/.github/workflows/upstream_watch.yml b/.github/workflows/upstream_watch.yml index aa33207c0..082535e8f 100644 --- a/.github/workflows/upstream_watch.yml +++ b/.github/workflows/upstream_watch.yml @@ -16,4 +16,4 @@ permissions: jobs: watch: - uses: Telosnex/telosnex_ci_tools/.github/workflows/upstream_watch.yml@6297b4dbe04372d8f73b1ab775afd16f938c6e23 # v0.5.11 + uses: Telosnex/telosnex_ci_tools/.github/workflows/upstream_watch.yml@c356a8d59e643d5fc8c552950fdd45c7e3bfea7f # v0.5.12 From 6eb2a4de8a540bf77a54c9fbf7c15a04ecb40d79 Mon Sep 17 00:00:00 2001 From: James O'Leary Date: Mon, 9 Nov 2026 07:59:49 -0800 Subject: [PATCH 56/69] CI: integration tests on arm64 Android in a container --- .github/workflows/integration.yml | 35 +++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/.github/workflows/integration.yml b/.github/workflows/integration.yml index dc818577b..400fad6f3 100644 --- a/.github/workflows/integration.yml +++ b/.github/workflows/integration.yml @@ -172,3 +172,38 @@ jobs: run: | flutter pub get flutter test integration_test/webcrypto_test.dart -d "$SERIAL" --timeout 5m -r expanded + + # The demo app's integration test on arm64 Android. + # Built on x64 (Google builds no arm64 Linux Android build tools), run in + # arm64 Android 15 in a container (actions/android-arm64-container). + test-apks: + name: Test APKs + runs-on: ubuntu-24.04 + timeout-minutes: 45 + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@c356a8d59e643d5fc8c552950fdd45c7e3bfea7f # v0.5.12 + - uses: Telosnex/telosnex_ci_tools/actions/android-test-apks@c356a8d59e643d5fc8c552950fdd45c7e3bfea7f # v0.5.12 + with: + working-directory: example/webcrypto_demo_flutter_app + targets: integration_test/webcrypto_test.dart + + android-arm64: + name: Android ARM64 container + needs: test-apks + runs-on: ubuntu-24.04-arm + timeout-minutes: 45 + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@c356a8d59e643d5fc8c552950fdd45c7e3bfea7f # v0.5.12 + - uses: Telosnex/telosnex_ci_tools/actions/download-artifact@c356a8d59e643d5fc8c552950fdd45c7e3bfea7f # v0.5.12 + with: + name: android-test-apks + path: ${{ runner.temp }}/apks + - uses: Telosnex/telosnex_ci_tools/actions/android-arm64-container@c356a8d59e643d5fc8c552950fdd45c7e3bfea7f # v0.5.12 + with: + apks: ${{ runner.temp }}/apks From fcf413a3cbd8854695ced0a25308b7825cff18ce Mon Sep 17 00:00:00 2001 From: James O'Leary Date: Tue, 10 Nov 2026 11:04:19 -0800 Subject: [PATCH 57/69] Upstream watch: name sources by repository --- .github/upstream.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/upstream.yaml b/.github/upstream.yaml index 1cd8f776a..a1113e570 100644 --- a/.github/upstream.yaml +++ b/.github/upstream.yaml @@ -3,7 +3,7 @@ # BoringSSL has no releases and changes daily: alert when the pin is a month # behind. Update with the Roll BoringSSL workflow. sources: - - name: BoringSSL + - name: google/boringssl branch: https://github.com/google/boringssl pin: {file: tool/REVISION, pattern: '([0-9a-f]{40})'} lag_days: 30 From a56fc0bf61472d4111650c7315b6b3194aa16a9f Mon Sep 17 00:00:00 2001 From: James O'Leary Date: Tue, 10 Nov 2026 11:16:50 -0800 Subject: [PATCH 58/69] ci: Telosnex/telosnex_ci_tools v0.5.14 --- .github/workflows/integration.yml | 24 ++++++++++++------------ .github/workflows/kiln_check.yml | 2 +- .github/workflows/kiln_release.yml | 2 +- .github/workflows/roll_boringssl.yml | 2 +- .github/workflows/unit.yml | 4 ++-- .github/workflows/upstream_watch.yml | 2 +- 6 files changed, 18 insertions(+), 18 deletions(-) diff --git a/.github/workflows/integration.yml b/.github/workflows/integration.yml index 400fad6f3..70f5c09ab 100644 --- a/.github/workflows/integration.yml +++ b/.github/workflows/integration.yml @@ -36,7 +36,7 @@ jobs: run: | sudo apt-get update -qq sudo apt-get install -y -qq clang cmake ninja-build pkg-config libgtk-3-dev xvfb > /dev/null - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@c356a8d59e643d5fc8c552950fdd45c7e3bfea7f # v0.5.12 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@bba81d85dab7716cc288df9877812dd3299ff11e # v0.5.14 - run: flutter pub get - run: dart test --exclude-tags browser-interop -p vm -r expanded - name: Demo app integration test @@ -91,9 +91,9 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@c356a8d59e643d5fc8c552950fdd45c7e3bfea7f # v0.5.12 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@bba81d85dab7716cc288df9877812dd3299ff11e # v0.5.14 - run: flutter pub get - - uses: Telosnex/telosnex_ci_tools/actions/dart-test-browser@c356a8d59e643d5fc8c552950fdd45c7e3bfea7f # v0.5.12 + - uses: Telosnex/telosnex_ci_tools/actions/dart-test-browser@bba81d85dab7716cc288df9877812dd3299ff11e # v0.5.14 with: platform: ${{ env.BROWSER }} compiler: ${{ env.COMPILER }} @@ -143,9 +143,9 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@c356a8d59e643d5fc8c552950fdd45c7e3bfea7f # v0.5.12 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@bba81d85dab7716cc288df9877812dd3299ff11e # v0.5.14 - run: flutter pub get - - uses: Telosnex/telosnex_ci_tools/actions/ios-flutter-test@c356a8d59e643d5fc8c552950fdd45c7e3bfea7f # v0.5.12 + - uses: Telosnex/telosnex_ci_tools/actions/ios-flutter-test@bba81d85dab7716cc288df9877812dd3299ff11e # v0.5.14 with: working-directory: example/webcrypto_demo_flutter_app target: integration_test/webcrypto_test.dart @@ -159,9 +159,9 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@c356a8d59e643d5fc8c552950fdd45c7e3bfea7f # v0.5.12 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@bba81d85dab7716cc288df9877812dd3299ff11e # v0.5.14 - run: flutter pub get - - uses: Telosnex/telosnex_ci_tools/actions/android-emulator@c356a8d59e643d5fc8c552950fdd45c7e3bfea7f # v0.5.12 + - uses: Telosnex/telosnex_ci_tools/actions/android-emulator@bba81d85dab7716cc288df9877812dd3299ff11e # v0.5.14 id: emulator with: api-level: '36' @@ -184,8 +184,8 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@c356a8d59e643d5fc8c552950fdd45c7e3bfea7f # v0.5.12 - - uses: Telosnex/telosnex_ci_tools/actions/android-test-apks@c356a8d59e643d5fc8c552950fdd45c7e3bfea7f # v0.5.12 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@bba81d85dab7716cc288df9877812dd3299ff11e # v0.5.14 + - uses: Telosnex/telosnex_ci_tools/actions/android-test-apks@bba81d85dab7716cc288df9877812dd3299ff11e # v0.5.14 with: working-directory: example/webcrypto_demo_flutter_app targets: integration_test/webcrypto_test.dart @@ -199,11 +199,11 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@c356a8d59e643d5fc8c552950fdd45c7e3bfea7f # v0.5.12 - - uses: Telosnex/telosnex_ci_tools/actions/download-artifact@c356a8d59e643d5fc8c552950fdd45c7e3bfea7f # v0.5.12 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@bba81d85dab7716cc288df9877812dd3299ff11e # v0.5.14 + - uses: Telosnex/telosnex_ci_tools/actions/download-artifact@bba81d85dab7716cc288df9877812dd3299ff11e # v0.5.14 with: name: android-test-apks path: ${{ runner.temp }}/apks - - uses: Telosnex/telosnex_ci_tools/actions/android-arm64-container@c356a8d59e643d5fc8c552950fdd45c7e3bfea7f # v0.5.12 + - uses: Telosnex/telosnex_ci_tools/actions/android-arm64-container@bba81d85dab7716cc288df9877812dd3299ff11e # v0.5.14 with: apks: ${{ runner.temp }}/apks diff --git a/.github/workflows/kiln_check.yml b/.github/workflows/kiln_check.yml index 9f1c26cdc..e902def3d 100644 --- a/.github/workflows/kiln_check.yml +++ b/.github/workflows/kiln_check.yml @@ -16,4 +16,4 @@ permissions: jobs: kiln-check: - uses: Telosnex/telosnex_ci_tools/.github/workflows/kiln_check.yml@c356a8d59e643d5fc8c552950fdd45c7e3bfea7f # v0.5.12 + uses: Telosnex/telosnex_ci_tools/.github/workflows/kiln_check.yml@bba81d85dab7716cc288df9877812dd3299ff11e # v0.5.14 diff --git a/.github/workflows/kiln_release.yml b/.github/workflows/kiln_release.yml index 56e43e0d7..be4e91940 100644 --- a/.github/workflows/kiln_release.yml +++ b/.github/workflows/kiln_release.yml @@ -25,7 +25,7 @@ permissions: jobs: kiln-release: - uses: Telosnex/telosnex_ci_tools/.github/workflows/kiln_release.yml@c356a8d59e643d5fc8c552950fdd45c7e3bfea7f # v0.5.12 + uses: Telosnex/telosnex_ci_tools/.github/workflows/kiln_release.yml@bba81d85dab7716cc288df9877812dd3299ff11e # v0.5.14 permissions: contents: write with: diff --git a/.github/workflows/roll_boringssl.yml b/.github/workflows/roll_boringssl.yml index 78bb13fd9..652bfd56d 100644 --- a/.github/workflows/roll_boringssl.yml +++ b/.github/workflows/roll_boringssl.yml @@ -28,7 +28,7 @@ jobs: with: fetch-depth: 0 - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@c356a8d59e643d5fc8c552950fdd45c7e3bfea7f # v0.5.12 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@bba81d85dab7716cc288df9877812dd3299ff11e # v0.5.14 - name: Install system dependencies run: | diff --git a/.github/workflows/unit.yml b/.github/workflows/unit.yml index e3e3601af..d63ec4470 100644 --- a/.github/workflows/unit.yml +++ b/.github/workflows/unit.yml @@ -25,8 +25,8 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@c356a8d59e643d5fc8c552950fdd45c7e3bfea7f # v0.5.12 - - uses: Telosnex/telosnex_ci_tools/actions/lint@c356a8d59e643d5fc8c552950fdd45c7e3bfea7f # v0.5.12 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@bba81d85dab7716cc288df9877812dd3299ff11e # v0.5.14 + - uses: Telosnex/telosnex_ci_tools/actions/lint@bba81d85dab7716cc288df9877812dd3299ff11e # v0.5.14 - run: flutter pub get - run: dart format --output=none --set-exit-if-changed . - run: dart analyze --fatal-warnings . diff --git a/.github/workflows/upstream_watch.yml b/.github/workflows/upstream_watch.yml index 082535e8f..1d669196b 100644 --- a/.github/workflows/upstream_watch.yml +++ b/.github/workflows/upstream_watch.yml @@ -16,4 +16,4 @@ permissions: jobs: watch: - uses: Telosnex/telosnex_ci_tools/.github/workflows/upstream_watch.yml@c356a8d59e643d5fc8c552950fdd45c7e3bfea7f # v0.5.12 + uses: Telosnex/telosnex_ci_tools/.github/workflows/upstream_watch.yml@bba81d85dab7716cc288df9877812dd3299ff11e # v0.5.14 From d67e1cca71d3072a374ffcb16f6b6c0f480cbfdd Mon Sep 17 00:00:00 2001 From: James O'Leary Date: Tue, 10 Nov 2026 11:20:15 -0800 Subject: [PATCH 59/69] Upstream watch: google/webcrypto.dart, the repository this one is a fork of A daily check: alerts once the oldest commit of google/webcrypto.dart not merged here is a week old. The pin is the last upstream commit merged; update it with each merge. --- .github/upstream.yaml | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/.github/upstream.yaml b/.github/upstream.yaml index a1113e570..550d65840 100644 --- a/.github/upstream.yaml +++ b/.github/upstream.yaml @@ -7,3 +7,10 @@ sources: branch: https://github.com/google/boringssl pin: {file: tool/REVISION, pattern: '([0-9a-f]{40})'} lag_days: 30 + # The repository this one is a fork of. The pin is the last upstream commit + # merged here: update it with each merge. Alert once the oldest unmerged + # commit is a week old. + - name: google/webcrypto.dart + branch: https://github.com/google/webcrypto.dart + pin: 9e186d9e7224e47d952f05a66c978b97896608a4 + lag_days: 7 From 3479234b17b3b481ab52374a380bd7fb2e2e03d1 Mon Sep 17 00:00:00 2001 From: James O'Leary Date: Tue, 10 Nov 2026 13:42:57 -0800 Subject: [PATCH 60/69] ci: Telosnex/telosnex_ci_tools v0.5.15 --- .github/workflows/integration.yml | 24 ++++++++++++------------ .github/workflows/kiln_check.yml | 2 +- .github/workflows/kiln_release.yml | 2 +- .github/workflows/roll_boringssl.yml | 2 +- .github/workflows/unit.yml | 4 ++-- .github/workflows/upstream_watch.yml | 2 +- 6 files changed, 18 insertions(+), 18 deletions(-) diff --git a/.github/workflows/integration.yml b/.github/workflows/integration.yml index 70f5c09ab..4574dcd0c 100644 --- a/.github/workflows/integration.yml +++ b/.github/workflows/integration.yml @@ -36,7 +36,7 @@ jobs: run: | sudo apt-get update -qq sudo apt-get install -y -qq clang cmake ninja-build pkg-config libgtk-3-dev xvfb > /dev/null - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@bba81d85dab7716cc288df9877812dd3299ff11e # v0.5.14 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@f97b50830596a30b24ea34c643a3b7dcce349972 # v0.5.15 - run: flutter pub get - run: dart test --exclude-tags browser-interop -p vm -r expanded - name: Demo app integration test @@ -91,9 +91,9 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@bba81d85dab7716cc288df9877812dd3299ff11e # v0.5.14 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@f97b50830596a30b24ea34c643a3b7dcce349972 # v0.5.15 - run: flutter pub get - - uses: Telosnex/telosnex_ci_tools/actions/dart-test-browser@bba81d85dab7716cc288df9877812dd3299ff11e # v0.5.14 + - uses: Telosnex/telosnex_ci_tools/actions/dart-test-browser@f97b50830596a30b24ea34c643a3b7dcce349972 # v0.5.15 with: platform: ${{ env.BROWSER }} compiler: ${{ env.COMPILER }} @@ -143,9 +143,9 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@bba81d85dab7716cc288df9877812dd3299ff11e # v0.5.14 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@f97b50830596a30b24ea34c643a3b7dcce349972 # v0.5.15 - run: flutter pub get - - uses: Telosnex/telosnex_ci_tools/actions/ios-flutter-test@bba81d85dab7716cc288df9877812dd3299ff11e # v0.5.14 + - uses: Telosnex/telosnex_ci_tools/actions/ios-flutter-test@f97b50830596a30b24ea34c643a3b7dcce349972 # v0.5.15 with: working-directory: example/webcrypto_demo_flutter_app target: integration_test/webcrypto_test.dart @@ -159,9 +159,9 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@bba81d85dab7716cc288df9877812dd3299ff11e # v0.5.14 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@f97b50830596a30b24ea34c643a3b7dcce349972 # v0.5.15 - run: flutter pub get - - uses: Telosnex/telosnex_ci_tools/actions/android-emulator@bba81d85dab7716cc288df9877812dd3299ff11e # v0.5.14 + - uses: Telosnex/telosnex_ci_tools/actions/android-emulator@f97b50830596a30b24ea34c643a3b7dcce349972 # v0.5.15 id: emulator with: api-level: '36' @@ -184,8 +184,8 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@bba81d85dab7716cc288df9877812dd3299ff11e # v0.5.14 - - uses: Telosnex/telosnex_ci_tools/actions/android-test-apks@bba81d85dab7716cc288df9877812dd3299ff11e # v0.5.14 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@f97b50830596a30b24ea34c643a3b7dcce349972 # v0.5.15 + - uses: Telosnex/telosnex_ci_tools/actions/android-test-apks@f97b50830596a30b24ea34c643a3b7dcce349972 # v0.5.15 with: working-directory: example/webcrypto_demo_flutter_app targets: integration_test/webcrypto_test.dart @@ -199,11 +199,11 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@bba81d85dab7716cc288df9877812dd3299ff11e # v0.5.14 - - uses: Telosnex/telosnex_ci_tools/actions/download-artifact@bba81d85dab7716cc288df9877812dd3299ff11e # v0.5.14 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@f97b50830596a30b24ea34c643a3b7dcce349972 # v0.5.15 + - uses: Telosnex/telosnex_ci_tools/actions/download-artifact@f97b50830596a30b24ea34c643a3b7dcce349972 # v0.5.15 with: name: android-test-apks path: ${{ runner.temp }}/apks - - uses: Telosnex/telosnex_ci_tools/actions/android-arm64-container@bba81d85dab7716cc288df9877812dd3299ff11e # v0.5.14 + - uses: Telosnex/telosnex_ci_tools/actions/android-arm64-container@f97b50830596a30b24ea34c643a3b7dcce349972 # v0.5.15 with: apks: ${{ runner.temp }}/apks diff --git a/.github/workflows/kiln_check.yml b/.github/workflows/kiln_check.yml index e902def3d..48bfdbcd3 100644 --- a/.github/workflows/kiln_check.yml +++ b/.github/workflows/kiln_check.yml @@ -16,4 +16,4 @@ permissions: jobs: kiln-check: - uses: Telosnex/telosnex_ci_tools/.github/workflows/kiln_check.yml@bba81d85dab7716cc288df9877812dd3299ff11e # v0.5.14 + uses: Telosnex/telosnex_ci_tools/.github/workflows/kiln_check.yml@f97b50830596a30b24ea34c643a3b7dcce349972 # v0.5.15 diff --git a/.github/workflows/kiln_release.yml b/.github/workflows/kiln_release.yml index be4e91940..a7de1ab8c 100644 --- a/.github/workflows/kiln_release.yml +++ b/.github/workflows/kiln_release.yml @@ -25,7 +25,7 @@ permissions: jobs: kiln-release: - uses: Telosnex/telosnex_ci_tools/.github/workflows/kiln_release.yml@bba81d85dab7716cc288df9877812dd3299ff11e # v0.5.14 + uses: Telosnex/telosnex_ci_tools/.github/workflows/kiln_release.yml@f97b50830596a30b24ea34c643a3b7dcce349972 # v0.5.15 permissions: contents: write with: diff --git a/.github/workflows/roll_boringssl.yml b/.github/workflows/roll_boringssl.yml index 652bfd56d..9b8945c69 100644 --- a/.github/workflows/roll_boringssl.yml +++ b/.github/workflows/roll_boringssl.yml @@ -28,7 +28,7 @@ jobs: with: fetch-depth: 0 - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@bba81d85dab7716cc288df9877812dd3299ff11e # v0.5.14 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@f97b50830596a30b24ea34c643a3b7dcce349972 # v0.5.15 - name: Install system dependencies run: | diff --git a/.github/workflows/unit.yml b/.github/workflows/unit.yml index d63ec4470..47bd2f7db 100644 --- a/.github/workflows/unit.yml +++ b/.github/workflows/unit.yml @@ -25,8 +25,8 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@bba81d85dab7716cc288df9877812dd3299ff11e # v0.5.14 - - uses: Telosnex/telosnex_ci_tools/actions/lint@bba81d85dab7716cc288df9877812dd3299ff11e # v0.5.14 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@f97b50830596a30b24ea34c643a3b7dcce349972 # v0.5.15 + - uses: Telosnex/telosnex_ci_tools/actions/lint@f97b50830596a30b24ea34c643a3b7dcce349972 # v0.5.15 - run: flutter pub get - run: dart format --output=none --set-exit-if-changed . - run: dart analyze --fatal-warnings . diff --git a/.github/workflows/upstream_watch.yml b/.github/workflows/upstream_watch.yml index 1d669196b..0323b851a 100644 --- a/.github/workflows/upstream_watch.yml +++ b/.github/workflows/upstream_watch.yml @@ -16,4 +16,4 @@ permissions: jobs: watch: - uses: Telosnex/telosnex_ci_tools/.github/workflows/upstream_watch.yml@bba81d85dab7716cc288df9877812dd3299ff11e # v0.5.14 + uses: Telosnex/telosnex_ci_tools/.github/workflows/upstream_watch.yml@f97b50830596a30b24ea34c643a3b7dcce349972 # v0.5.15 From a32dc9a892a0bb28c22c8b5fe2b6d206411cba5c Mon Sep 17 00:00:00 2001 From: James O'Leary Date: Mon, 16 Nov 2026 12:17:10 -0800 Subject: [PATCH 61/69] ci: Telosnex/telosnex_ci_tools v0.5.16 --- .github/workflows/integration.yml | 24 ++++++++++++------------ .github/workflows/kiln_check.yml | 2 +- .github/workflows/kiln_release.yml | 2 +- .github/workflows/roll_boringssl.yml | 2 +- .github/workflows/unit.yml | 4 ++-- .github/workflows/upstream_watch.yml | 2 +- 6 files changed, 18 insertions(+), 18 deletions(-) diff --git a/.github/workflows/integration.yml b/.github/workflows/integration.yml index 4574dcd0c..a41c39b0b 100644 --- a/.github/workflows/integration.yml +++ b/.github/workflows/integration.yml @@ -36,7 +36,7 @@ jobs: run: | sudo apt-get update -qq sudo apt-get install -y -qq clang cmake ninja-build pkg-config libgtk-3-dev xvfb > /dev/null - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@f97b50830596a30b24ea34c643a3b7dcce349972 # v0.5.15 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@14aa316a575c5c7fd543a924b5c1973c24301ab2 # v0.5.16 - run: flutter pub get - run: dart test --exclude-tags browser-interop -p vm -r expanded - name: Demo app integration test @@ -91,9 +91,9 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@f97b50830596a30b24ea34c643a3b7dcce349972 # v0.5.15 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@14aa316a575c5c7fd543a924b5c1973c24301ab2 # v0.5.16 - run: flutter pub get - - uses: Telosnex/telosnex_ci_tools/actions/dart-test-browser@f97b50830596a30b24ea34c643a3b7dcce349972 # v0.5.15 + - uses: Telosnex/telosnex_ci_tools/actions/dart-test-browser@14aa316a575c5c7fd543a924b5c1973c24301ab2 # v0.5.16 with: platform: ${{ env.BROWSER }} compiler: ${{ env.COMPILER }} @@ -143,9 +143,9 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@f97b50830596a30b24ea34c643a3b7dcce349972 # v0.5.15 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@14aa316a575c5c7fd543a924b5c1973c24301ab2 # v0.5.16 - run: flutter pub get - - uses: Telosnex/telosnex_ci_tools/actions/ios-flutter-test@f97b50830596a30b24ea34c643a3b7dcce349972 # v0.5.15 + - uses: Telosnex/telosnex_ci_tools/actions/ios-flutter-test@14aa316a575c5c7fd543a924b5c1973c24301ab2 # v0.5.16 with: working-directory: example/webcrypto_demo_flutter_app target: integration_test/webcrypto_test.dart @@ -159,9 +159,9 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@f97b50830596a30b24ea34c643a3b7dcce349972 # v0.5.15 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@14aa316a575c5c7fd543a924b5c1973c24301ab2 # v0.5.16 - run: flutter pub get - - uses: Telosnex/telosnex_ci_tools/actions/android-emulator@f97b50830596a30b24ea34c643a3b7dcce349972 # v0.5.15 + - uses: Telosnex/telosnex_ci_tools/actions/android-emulator@14aa316a575c5c7fd543a924b5c1973c24301ab2 # v0.5.16 id: emulator with: api-level: '36' @@ -184,8 +184,8 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@f97b50830596a30b24ea34c643a3b7dcce349972 # v0.5.15 - - uses: Telosnex/telosnex_ci_tools/actions/android-test-apks@f97b50830596a30b24ea34c643a3b7dcce349972 # v0.5.15 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@14aa316a575c5c7fd543a924b5c1973c24301ab2 # v0.5.16 + - uses: Telosnex/telosnex_ci_tools/actions/android-test-apks@14aa316a575c5c7fd543a924b5c1973c24301ab2 # v0.5.16 with: working-directory: example/webcrypto_demo_flutter_app targets: integration_test/webcrypto_test.dart @@ -199,11 +199,11 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@f97b50830596a30b24ea34c643a3b7dcce349972 # v0.5.15 - - uses: Telosnex/telosnex_ci_tools/actions/download-artifact@f97b50830596a30b24ea34c643a3b7dcce349972 # v0.5.15 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@14aa316a575c5c7fd543a924b5c1973c24301ab2 # v0.5.16 + - uses: Telosnex/telosnex_ci_tools/actions/download-artifact@14aa316a575c5c7fd543a924b5c1973c24301ab2 # v0.5.16 with: name: android-test-apks path: ${{ runner.temp }}/apks - - uses: Telosnex/telosnex_ci_tools/actions/android-arm64-container@f97b50830596a30b24ea34c643a3b7dcce349972 # v0.5.15 + - uses: Telosnex/telosnex_ci_tools/actions/android-arm64-container@14aa316a575c5c7fd543a924b5c1973c24301ab2 # v0.5.16 with: apks: ${{ runner.temp }}/apks diff --git a/.github/workflows/kiln_check.yml b/.github/workflows/kiln_check.yml index 48bfdbcd3..60baef262 100644 --- a/.github/workflows/kiln_check.yml +++ b/.github/workflows/kiln_check.yml @@ -16,4 +16,4 @@ permissions: jobs: kiln-check: - uses: Telosnex/telosnex_ci_tools/.github/workflows/kiln_check.yml@f97b50830596a30b24ea34c643a3b7dcce349972 # v0.5.15 + uses: Telosnex/telosnex_ci_tools/.github/workflows/kiln_check.yml@14aa316a575c5c7fd543a924b5c1973c24301ab2 # v0.5.16 diff --git a/.github/workflows/kiln_release.yml b/.github/workflows/kiln_release.yml index a7de1ab8c..13fa266c2 100644 --- a/.github/workflows/kiln_release.yml +++ b/.github/workflows/kiln_release.yml @@ -25,7 +25,7 @@ permissions: jobs: kiln-release: - uses: Telosnex/telosnex_ci_tools/.github/workflows/kiln_release.yml@f97b50830596a30b24ea34c643a3b7dcce349972 # v0.5.15 + uses: Telosnex/telosnex_ci_tools/.github/workflows/kiln_release.yml@14aa316a575c5c7fd543a924b5c1973c24301ab2 # v0.5.16 permissions: contents: write with: diff --git a/.github/workflows/roll_boringssl.yml b/.github/workflows/roll_boringssl.yml index 9b8945c69..c1d9e8826 100644 --- a/.github/workflows/roll_boringssl.yml +++ b/.github/workflows/roll_boringssl.yml @@ -28,7 +28,7 @@ jobs: with: fetch-depth: 0 - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@f97b50830596a30b24ea34c643a3b7dcce349972 # v0.5.15 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@14aa316a575c5c7fd543a924b5c1973c24301ab2 # v0.5.16 - name: Install system dependencies run: | diff --git a/.github/workflows/unit.yml b/.github/workflows/unit.yml index 47bd2f7db..73434b8e9 100644 --- a/.github/workflows/unit.yml +++ b/.github/workflows/unit.yml @@ -25,8 +25,8 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@f97b50830596a30b24ea34c643a3b7dcce349972 # v0.5.15 - - uses: Telosnex/telosnex_ci_tools/actions/lint@f97b50830596a30b24ea34c643a3b7dcce349972 # v0.5.15 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@14aa316a575c5c7fd543a924b5c1973c24301ab2 # v0.5.16 + - uses: Telosnex/telosnex_ci_tools/actions/lint@14aa316a575c5c7fd543a924b5c1973c24301ab2 # v0.5.16 - run: flutter pub get - run: dart format --output=none --set-exit-if-changed . - run: dart analyze --fatal-warnings . diff --git a/.github/workflows/upstream_watch.yml b/.github/workflows/upstream_watch.yml index 0323b851a..c76913c40 100644 --- a/.github/workflows/upstream_watch.yml +++ b/.github/workflows/upstream_watch.yml @@ -16,4 +16,4 @@ permissions: jobs: watch: - uses: Telosnex/telosnex_ci_tools/.github/workflows/upstream_watch.yml@f97b50830596a30b24ea34c643a3b7dcce349972 # v0.5.15 + uses: Telosnex/telosnex_ci_tools/.github/workflows/upstream_watch.yml@14aa316a575c5c7fd543a924b5c1973c24301ab2 # v0.5.16 From e4078474fbc82978ba59f9c861e1ab5641028097 Mon Sep 17 00:00:00 2001 From: James O'Leary Date: Mon, 16 Nov 2026 12:55:28 -0800 Subject: [PATCH 62/69] ci: Telosnex/telosnex_ci_tools v0.5.17 --- .github/workflows/integration.yml | 24 ++++++++++++------------ .github/workflows/kiln_check.yml | 2 +- .github/workflows/kiln_release.yml | 2 +- .github/workflows/roll_boringssl.yml | 2 +- .github/workflows/unit.yml | 4 ++-- .github/workflows/upstream_watch.yml | 2 +- 6 files changed, 18 insertions(+), 18 deletions(-) diff --git a/.github/workflows/integration.yml b/.github/workflows/integration.yml index a41c39b0b..903199bf7 100644 --- a/.github/workflows/integration.yml +++ b/.github/workflows/integration.yml @@ -36,7 +36,7 @@ jobs: run: | sudo apt-get update -qq sudo apt-get install -y -qq clang cmake ninja-build pkg-config libgtk-3-dev xvfb > /dev/null - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@14aa316a575c5c7fd543a924b5c1973c24301ab2 # v0.5.16 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@a3654aa0e8e1662f88295adaabf25bc33e1a2cae # v0.5.17 - run: flutter pub get - run: dart test --exclude-tags browser-interop -p vm -r expanded - name: Demo app integration test @@ -91,9 +91,9 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@14aa316a575c5c7fd543a924b5c1973c24301ab2 # v0.5.16 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@a3654aa0e8e1662f88295adaabf25bc33e1a2cae # v0.5.17 - run: flutter pub get - - uses: Telosnex/telosnex_ci_tools/actions/dart-test-browser@14aa316a575c5c7fd543a924b5c1973c24301ab2 # v0.5.16 + - uses: Telosnex/telosnex_ci_tools/actions/dart-test-browser@a3654aa0e8e1662f88295adaabf25bc33e1a2cae # v0.5.17 with: platform: ${{ env.BROWSER }} compiler: ${{ env.COMPILER }} @@ -143,9 +143,9 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@14aa316a575c5c7fd543a924b5c1973c24301ab2 # v0.5.16 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@a3654aa0e8e1662f88295adaabf25bc33e1a2cae # v0.5.17 - run: flutter pub get - - uses: Telosnex/telosnex_ci_tools/actions/ios-flutter-test@14aa316a575c5c7fd543a924b5c1973c24301ab2 # v0.5.16 + - uses: Telosnex/telosnex_ci_tools/actions/ios-flutter-test@a3654aa0e8e1662f88295adaabf25bc33e1a2cae # v0.5.17 with: working-directory: example/webcrypto_demo_flutter_app target: integration_test/webcrypto_test.dart @@ -159,9 +159,9 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@14aa316a575c5c7fd543a924b5c1973c24301ab2 # v0.5.16 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@a3654aa0e8e1662f88295adaabf25bc33e1a2cae # v0.5.17 - run: flutter pub get - - uses: Telosnex/telosnex_ci_tools/actions/android-emulator@14aa316a575c5c7fd543a924b5c1973c24301ab2 # v0.5.16 + - uses: Telosnex/telosnex_ci_tools/actions/android-emulator@a3654aa0e8e1662f88295adaabf25bc33e1a2cae # v0.5.17 id: emulator with: api-level: '36' @@ -184,8 +184,8 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@14aa316a575c5c7fd543a924b5c1973c24301ab2 # v0.5.16 - - uses: Telosnex/telosnex_ci_tools/actions/android-test-apks@14aa316a575c5c7fd543a924b5c1973c24301ab2 # v0.5.16 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@a3654aa0e8e1662f88295adaabf25bc33e1a2cae # v0.5.17 + - uses: Telosnex/telosnex_ci_tools/actions/android-test-apks@a3654aa0e8e1662f88295adaabf25bc33e1a2cae # v0.5.17 with: working-directory: example/webcrypto_demo_flutter_app targets: integration_test/webcrypto_test.dart @@ -199,11 +199,11 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@14aa316a575c5c7fd543a924b5c1973c24301ab2 # v0.5.16 - - uses: Telosnex/telosnex_ci_tools/actions/download-artifact@14aa316a575c5c7fd543a924b5c1973c24301ab2 # v0.5.16 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@a3654aa0e8e1662f88295adaabf25bc33e1a2cae # v0.5.17 + - uses: Telosnex/telosnex_ci_tools/actions/download-artifact@a3654aa0e8e1662f88295adaabf25bc33e1a2cae # v0.5.17 with: name: android-test-apks path: ${{ runner.temp }}/apks - - uses: Telosnex/telosnex_ci_tools/actions/android-arm64-container@14aa316a575c5c7fd543a924b5c1973c24301ab2 # v0.5.16 + - uses: Telosnex/telosnex_ci_tools/actions/android-arm64-container@a3654aa0e8e1662f88295adaabf25bc33e1a2cae # v0.5.17 with: apks: ${{ runner.temp }}/apks diff --git a/.github/workflows/kiln_check.yml b/.github/workflows/kiln_check.yml index 60baef262..788779038 100644 --- a/.github/workflows/kiln_check.yml +++ b/.github/workflows/kiln_check.yml @@ -16,4 +16,4 @@ permissions: jobs: kiln-check: - uses: Telosnex/telosnex_ci_tools/.github/workflows/kiln_check.yml@14aa316a575c5c7fd543a924b5c1973c24301ab2 # v0.5.16 + uses: Telosnex/telosnex_ci_tools/.github/workflows/kiln_check.yml@a3654aa0e8e1662f88295adaabf25bc33e1a2cae # v0.5.17 diff --git a/.github/workflows/kiln_release.yml b/.github/workflows/kiln_release.yml index 13fa266c2..1bb08184f 100644 --- a/.github/workflows/kiln_release.yml +++ b/.github/workflows/kiln_release.yml @@ -25,7 +25,7 @@ permissions: jobs: kiln-release: - uses: Telosnex/telosnex_ci_tools/.github/workflows/kiln_release.yml@14aa316a575c5c7fd543a924b5c1973c24301ab2 # v0.5.16 + uses: Telosnex/telosnex_ci_tools/.github/workflows/kiln_release.yml@a3654aa0e8e1662f88295adaabf25bc33e1a2cae # v0.5.17 permissions: contents: write with: diff --git a/.github/workflows/roll_boringssl.yml b/.github/workflows/roll_boringssl.yml index c1d9e8826..d589eda10 100644 --- a/.github/workflows/roll_boringssl.yml +++ b/.github/workflows/roll_boringssl.yml @@ -28,7 +28,7 @@ jobs: with: fetch-depth: 0 - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@14aa316a575c5c7fd543a924b5c1973c24301ab2 # v0.5.16 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@a3654aa0e8e1662f88295adaabf25bc33e1a2cae # v0.5.17 - name: Install system dependencies run: | diff --git a/.github/workflows/unit.yml b/.github/workflows/unit.yml index 73434b8e9..db12e3f1d 100644 --- a/.github/workflows/unit.yml +++ b/.github/workflows/unit.yml @@ -25,8 +25,8 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@14aa316a575c5c7fd543a924b5c1973c24301ab2 # v0.5.16 - - uses: Telosnex/telosnex_ci_tools/actions/lint@14aa316a575c5c7fd543a924b5c1973c24301ab2 # v0.5.16 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@a3654aa0e8e1662f88295adaabf25bc33e1a2cae # v0.5.17 + - uses: Telosnex/telosnex_ci_tools/actions/lint@a3654aa0e8e1662f88295adaabf25bc33e1a2cae # v0.5.17 - run: flutter pub get - run: dart format --output=none --set-exit-if-changed . - run: dart analyze --fatal-warnings . diff --git a/.github/workflows/upstream_watch.yml b/.github/workflows/upstream_watch.yml index c76913c40..d370a2ef9 100644 --- a/.github/workflows/upstream_watch.yml +++ b/.github/workflows/upstream_watch.yml @@ -16,4 +16,4 @@ permissions: jobs: watch: - uses: Telosnex/telosnex_ci_tools/.github/workflows/upstream_watch.yml@14aa316a575c5c7fd543a924b5c1973c24301ab2 # v0.5.16 + uses: Telosnex/telosnex_ci_tools/.github/workflows/upstream_watch.yml@a3654aa0e8e1662f88295adaabf25bc33e1a2cae # v0.5.17 From f7238175617930d8d4332d457d7ad86e1c7b79ba Mon Sep 17 00:00:00 2001 From: James O'Leary Date: Mon, 16 Nov 2026 13:31:39 -0800 Subject: [PATCH 63/69] ci: Telosnex/telosnex_ci_tools v0.5.18 --- .github/workflows/integration.yml | 24 ++++++++++++------------ .github/workflows/kiln_check.yml | 2 +- .github/workflows/kiln_release.yml | 2 +- .github/workflows/roll_boringssl.yml | 2 +- .github/workflows/unit.yml | 4 ++-- .github/workflows/upstream_watch.yml | 2 +- 6 files changed, 18 insertions(+), 18 deletions(-) diff --git a/.github/workflows/integration.yml b/.github/workflows/integration.yml index 903199bf7..5f71c6ccd 100644 --- a/.github/workflows/integration.yml +++ b/.github/workflows/integration.yml @@ -36,7 +36,7 @@ jobs: run: | sudo apt-get update -qq sudo apt-get install -y -qq clang cmake ninja-build pkg-config libgtk-3-dev xvfb > /dev/null - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@a3654aa0e8e1662f88295adaabf25bc33e1a2cae # v0.5.17 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@461a03187b073db7df0237e9666e4fbd6782627d # v0.5.18 - run: flutter pub get - run: dart test --exclude-tags browser-interop -p vm -r expanded - name: Demo app integration test @@ -91,9 +91,9 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@a3654aa0e8e1662f88295adaabf25bc33e1a2cae # v0.5.17 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@461a03187b073db7df0237e9666e4fbd6782627d # v0.5.18 - run: flutter pub get - - uses: Telosnex/telosnex_ci_tools/actions/dart-test-browser@a3654aa0e8e1662f88295adaabf25bc33e1a2cae # v0.5.17 + - uses: Telosnex/telosnex_ci_tools/actions/dart-test-browser@461a03187b073db7df0237e9666e4fbd6782627d # v0.5.18 with: platform: ${{ env.BROWSER }} compiler: ${{ env.COMPILER }} @@ -143,9 +143,9 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@a3654aa0e8e1662f88295adaabf25bc33e1a2cae # v0.5.17 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@461a03187b073db7df0237e9666e4fbd6782627d # v0.5.18 - run: flutter pub get - - uses: Telosnex/telosnex_ci_tools/actions/ios-flutter-test@a3654aa0e8e1662f88295adaabf25bc33e1a2cae # v0.5.17 + - uses: Telosnex/telosnex_ci_tools/actions/ios-flutter-test@461a03187b073db7df0237e9666e4fbd6782627d # v0.5.18 with: working-directory: example/webcrypto_demo_flutter_app target: integration_test/webcrypto_test.dart @@ -159,9 +159,9 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@a3654aa0e8e1662f88295adaabf25bc33e1a2cae # v0.5.17 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@461a03187b073db7df0237e9666e4fbd6782627d # v0.5.18 - run: flutter pub get - - uses: Telosnex/telosnex_ci_tools/actions/android-emulator@a3654aa0e8e1662f88295adaabf25bc33e1a2cae # v0.5.17 + - uses: Telosnex/telosnex_ci_tools/actions/android-emulator@461a03187b073db7df0237e9666e4fbd6782627d # v0.5.18 id: emulator with: api-level: '36' @@ -184,8 +184,8 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@a3654aa0e8e1662f88295adaabf25bc33e1a2cae # v0.5.17 - - uses: Telosnex/telosnex_ci_tools/actions/android-test-apks@a3654aa0e8e1662f88295adaabf25bc33e1a2cae # v0.5.17 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@461a03187b073db7df0237e9666e4fbd6782627d # v0.5.18 + - uses: Telosnex/telosnex_ci_tools/actions/android-test-apks@461a03187b073db7df0237e9666e4fbd6782627d # v0.5.18 with: working-directory: example/webcrypto_demo_flutter_app targets: integration_test/webcrypto_test.dart @@ -199,11 +199,11 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@a3654aa0e8e1662f88295adaabf25bc33e1a2cae # v0.5.17 - - uses: Telosnex/telosnex_ci_tools/actions/download-artifact@a3654aa0e8e1662f88295adaabf25bc33e1a2cae # v0.5.17 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@461a03187b073db7df0237e9666e4fbd6782627d # v0.5.18 + - uses: Telosnex/telosnex_ci_tools/actions/download-artifact@461a03187b073db7df0237e9666e4fbd6782627d # v0.5.18 with: name: android-test-apks path: ${{ runner.temp }}/apks - - uses: Telosnex/telosnex_ci_tools/actions/android-arm64-container@a3654aa0e8e1662f88295adaabf25bc33e1a2cae # v0.5.17 + - uses: Telosnex/telosnex_ci_tools/actions/android-arm64-container@461a03187b073db7df0237e9666e4fbd6782627d # v0.5.18 with: apks: ${{ runner.temp }}/apks diff --git a/.github/workflows/kiln_check.yml b/.github/workflows/kiln_check.yml index 788779038..a9d4a778d 100644 --- a/.github/workflows/kiln_check.yml +++ b/.github/workflows/kiln_check.yml @@ -16,4 +16,4 @@ permissions: jobs: kiln-check: - uses: Telosnex/telosnex_ci_tools/.github/workflows/kiln_check.yml@a3654aa0e8e1662f88295adaabf25bc33e1a2cae # v0.5.17 + uses: Telosnex/telosnex_ci_tools/.github/workflows/kiln_check.yml@461a03187b073db7df0237e9666e4fbd6782627d # v0.5.18 diff --git a/.github/workflows/kiln_release.yml b/.github/workflows/kiln_release.yml index 1bb08184f..e5accf441 100644 --- a/.github/workflows/kiln_release.yml +++ b/.github/workflows/kiln_release.yml @@ -25,7 +25,7 @@ permissions: jobs: kiln-release: - uses: Telosnex/telosnex_ci_tools/.github/workflows/kiln_release.yml@a3654aa0e8e1662f88295adaabf25bc33e1a2cae # v0.5.17 + uses: Telosnex/telosnex_ci_tools/.github/workflows/kiln_release.yml@461a03187b073db7df0237e9666e4fbd6782627d # v0.5.18 permissions: contents: write with: diff --git a/.github/workflows/roll_boringssl.yml b/.github/workflows/roll_boringssl.yml index d589eda10..b2cef76c0 100644 --- a/.github/workflows/roll_boringssl.yml +++ b/.github/workflows/roll_boringssl.yml @@ -28,7 +28,7 @@ jobs: with: fetch-depth: 0 - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@a3654aa0e8e1662f88295adaabf25bc33e1a2cae # v0.5.17 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@461a03187b073db7df0237e9666e4fbd6782627d # v0.5.18 - name: Install system dependencies run: | diff --git a/.github/workflows/unit.yml b/.github/workflows/unit.yml index db12e3f1d..9a34d4e1d 100644 --- a/.github/workflows/unit.yml +++ b/.github/workflows/unit.yml @@ -25,8 +25,8 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@a3654aa0e8e1662f88295adaabf25bc33e1a2cae # v0.5.17 - - uses: Telosnex/telosnex_ci_tools/actions/lint@a3654aa0e8e1662f88295adaabf25bc33e1a2cae # v0.5.17 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@461a03187b073db7df0237e9666e4fbd6782627d # v0.5.18 + - uses: Telosnex/telosnex_ci_tools/actions/lint@461a03187b073db7df0237e9666e4fbd6782627d # v0.5.18 - run: flutter pub get - run: dart format --output=none --set-exit-if-changed . - run: dart analyze --fatal-warnings . diff --git a/.github/workflows/upstream_watch.yml b/.github/workflows/upstream_watch.yml index d370a2ef9..13c8e6c4d 100644 --- a/.github/workflows/upstream_watch.yml +++ b/.github/workflows/upstream_watch.yml @@ -16,4 +16,4 @@ permissions: jobs: watch: - uses: Telosnex/telosnex_ci_tools/.github/workflows/upstream_watch.yml@a3654aa0e8e1662f88295adaabf25bc33e1a2cae # v0.5.17 + uses: Telosnex/telosnex_ci_tools/.github/workflows/upstream_watch.yml@461a03187b073db7df0237e9666e4fbd6782627d # v0.5.18 From bd2a12024b2c687ff39d3a306c8b592472ccfd54 Mon Sep 17 00:00:00 2001 From: James O'Leary Date: Mon, 16 Nov 2026 13:46:58 -0800 Subject: [PATCH 64/69] ci: Telosnex/telosnex_ci_tools v0.5.19 --- .github/workflows/integration.yml | 24 ++++++++++++------------ .github/workflows/kiln_check.yml | 2 +- .github/workflows/kiln_release.yml | 2 +- .github/workflows/roll_boringssl.yml | 2 +- .github/workflows/unit.yml | 4 ++-- .github/workflows/upstream_watch.yml | 2 +- 6 files changed, 18 insertions(+), 18 deletions(-) diff --git a/.github/workflows/integration.yml b/.github/workflows/integration.yml index 5f71c6ccd..94c6bafc9 100644 --- a/.github/workflows/integration.yml +++ b/.github/workflows/integration.yml @@ -36,7 +36,7 @@ jobs: run: | sudo apt-get update -qq sudo apt-get install -y -qq clang cmake ninja-build pkg-config libgtk-3-dev xvfb > /dev/null - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@461a03187b073db7df0237e9666e4fbd6782627d # v0.5.18 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@6747daa138553c20271dc0bd40bfb9522638537c # v0.5.19 - run: flutter pub get - run: dart test --exclude-tags browser-interop -p vm -r expanded - name: Demo app integration test @@ -91,9 +91,9 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@461a03187b073db7df0237e9666e4fbd6782627d # v0.5.18 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@6747daa138553c20271dc0bd40bfb9522638537c # v0.5.19 - run: flutter pub get - - uses: Telosnex/telosnex_ci_tools/actions/dart-test-browser@461a03187b073db7df0237e9666e4fbd6782627d # v0.5.18 + - uses: Telosnex/telosnex_ci_tools/actions/dart-test-browser@6747daa138553c20271dc0bd40bfb9522638537c # v0.5.19 with: platform: ${{ env.BROWSER }} compiler: ${{ env.COMPILER }} @@ -143,9 +143,9 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@461a03187b073db7df0237e9666e4fbd6782627d # v0.5.18 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@6747daa138553c20271dc0bd40bfb9522638537c # v0.5.19 - run: flutter pub get - - uses: Telosnex/telosnex_ci_tools/actions/ios-flutter-test@461a03187b073db7df0237e9666e4fbd6782627d # v0.5.18 + - uses: Telosnex/telosnex_ci_tools/actions/ios-flutter-test@6747daa138553c20271dc0bd40bfb9522638537c # v0.5.19 with: working-directory: example/webcrypto_demo_flutter_app target: integration_test/webcrypto_test.dart @@ -159,9 +159,9 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@461a03187b073db7df0237e9666e4fbd6782627d # v0.5.18 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@6747daa138553c20271dc0bd40bfb9522638537c # v0.5.19 - run: flutter pub get - - uses: Telosnex/telosnex_ci_tools/actions/android-emulator@461a03187b073db7df0237e9666e4fbd6782627d # v0.5.18 + - uses: Telosnex/telosnex_ci_tools/actions/android-emulator@6747daa138553c20271dc0bd40bfb9522638537c # v0.5.19 id: emulator with: api-level: '36' @@ -184,8 +184,8 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@461a03187b073db7df0237e9666e4fbd6782627d # v0.5.18 - - uses: Telosnex/telosnex_ci_tools/actions/android-test-apks@461a03187b073db7df0237e9666e4fbd6782627d # v0.5.18 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@6747daa138553c20271dc0bd40bfb9522638537c # v0.5.19 + - uses: Telosnex/telosnex_ci_tools/actions/android-test-apks@6747daa138553c20271dc0bd40bfb9522638537c # v0.5.19 with: working-directory: example/webcrypto_demo_flutter_app targets: integration_test/webcrypto_test.dart @@ -199,11 +199,11 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@461a03187b073db7df0237e9666e4fbd6782627d # v0.5.18 - - uses: Telosnex/telosnex_ci_tools/actions/download-artifact@461a03187b073db7df0237e9666e4fbd6782627d # v0.5.18 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@6747daa138553c20271dc0bd40bfb9522638537c # v0.5.19 + - uses: Telosnex/telosnex_ci_tools/actions/download-artifact@6747daa138553c20271dc0bd40bfb9522638537c # v0.5.19 with: name: android-test-apks path: ${{ runner.temp }}/apks - - uses: Telosnex/telosnex_ci_tools/actions/android-arm64-container@461a03187b073db7df0237e9666e4fbd6782627d # v0.5.18 + - uses: Telosnex/telosnex_ci_tools/actions/android-arm64-container@6747daa138553c20271dc0bd40bfb9522638537c # v0.5.19 with: apks: ${{ runner.temp }}/apks diff --git a/.github/workflows/kiln_check.yml b/.github/workflows/kiln_check.yml index a9d4a778d..5f3091cd8 100644 --- a/.github/workflows/kiln_check.yml +++ b/.github/workflows/kiln_check.yml @@ -16,4 +16,4 @@ permissions: jobs: kiln-check: - uses: Telosnex/telosnex_ci_tools/.github/workflows/kiln_check.yml@461a03187b073db7df0237e9666e4fbd6782627d # v0.5.18 + uses: Telosnex/telosnex_ci_tools/.github/workflows/kiln_check.yml@6747daa138553c20271dc0bd40bfb9522638537c # v0.5.19 diff --git a/.github/workflows/kiln_release.yml b/.github/workflows/kiln_release.yml index e5accf441..b6c505789 100644 --- a/.github/workflows/kiln_release.yml +++ b/.github/workflows/kiln_release.yml @@ -25,7 +25,7 @@ permissions: jobs: kiln-release: - uses: Telosnex/telosnex_ci_tools/.github/workflows/kiln_release.yml@461a03187b073db7df0237e9666e4fbd6782627d # v0.5.18 + uses: Telosnex/telosnex_ci_tools/.github/workflows/kiln_release.yml@6747daa138553c20271dc0bd40bfb9522638537c # v0.5.19 permissions: contents: write with: diff --git a/.github/workflows/roll_boringssl.yml b/.github/workflows/roll_boringssl.yml index b2cef76c0..db53fa260 100644 --- a/.github/workflows/roll_boringssl.yml +++ b/.github/workflows/roll_boringssl.yml @@ -28,7 +28,7 @@ jobs: with: fetch-depth: 0 - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@461a03187b073db7df0237e9666e4fbd6782627d # v0.5.18 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@6747daa138553c20271dc0bd40bfb9522638537c # v0.5.19 - name: Install system dependencies run: | diff --git a/.github/workflows/unit.yml b/.github/workflows/unit.yml index 9a34d4e1d..7ec78604a 100644 --- a/.github/workflows/unit.yml +++ b/.github/workflows/unit.yml @@ -25,8 +25,8 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@461a03187b073db7df0237e9666e4fbd6782627d # v0.5.18 - - uses: Telosnex/telosnex_ci_tools/actions/lint@461a03187b073db7df0237e9666e4fbd6782627d # v0.5.18 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@6747daa138553c20271dc0bd40bfb9522638537c # v0.5.19 + - uses: Telosnex/telosnex_ci_tools/actions/lint@6747daa138553c20271dc0bd40bfb9522638537c # v0.5.19 - run: flutter pub get - run: dart format --output=none --set-exit-if-changed . - run: dart analyze --fatal-warnings . diff --git a/.github/workflows/upstream_watch.yml b/.github/workflows/upstream_watch.yml index 13c8e6c4d..e97e9ac93 100644 --- a/.github/workflows/upstream_watch.yml +++ b/.github/workflows/upstream_watch.yml @@ -16,4 +16,4 @@ permissions: jobs: watch: - uses: Telosnex/telosnex_ci_tools/.github/workflows/upstream_watch.yml@461a03187b073db7df0237e9666e4fbd6782627d # v0.5.18 + uses: Telosnex/telosnex_ci_tools/.github/workflows/upstream_watch.yml@6747daa138553c20271dc0bd40bfb9522638537c # v0.5.19 From 88c94a1a914921af2d885ca58709dad53c709fc7 Mon Sep 17 00:00:00 2001 From: James O'Leary Date: Mon, 16 Nov 2026 14:32:06 -0800 Subject: [PATCH 65/69] ci: Telosnex/telosnex_ci_tools v0.5.20 --- .github/workflows/integration.yml | 24 ++++++++++++------------ .github/workflows/kiln_check.yml | 2 +- .github/workflows/kiln_release.yml | 2 +- .github/workflows/roll_boringssl.yml | 2 +- .github/workflows/unit.yml | 4 ++-- .github/workflows/upstream_watch.yml | 2 +- 6 files changed, 18 insertions(+), 18 deletions(-) diff --git a/.github/workflows/integration.yml b/.github/workflows/integration.yml index 94c6bafc9..a1f53e920 100644 --- a/.github/workflows/integration.yml +++ b/.github/workflows/integration.yml @@ -36,7 +36,7 @@ jobs: run: | sudo apt-get update -qq sudo apt-get install -y -qq clang cmake ninja-build pkg-config libgtk-3-dev xvfb > /dev/null - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@6747daa138553c20271dc0bd40bfb9522638537c # v0.5.19 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@c1c96dee1b426f3617d5e9caf60fd45899bb8a4f # v0.5.20 - run: flutter pub get - run: dart test --exclude-tags browser-interop -p vm -r expanded - name: Demo app integration test @@ -91,9 +91,9 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@6747daa138553c20271dc0bd40bfb9522638537c # v0.5.19 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@c1c96dee1b426f3617d5e9caf60fd45899bb8a4f # v0.5.20 - run: flutter pub get - - uses: Telosnex/telosnex_ci_tools/actions/dart-test-browser@6747daa138553c20271dc0bd40bfb9522638537c # v0.5.19 + - uses: Telosnex/telosnex_ci_tools/actions/dart-test-browser@c1c96dee1b426f3617d5e9caf60fd45899bb8a4f # v0.5.20 with: platform: ${{ env.BROWSER }} compiler: ${{ env.COMPILER }} @@ -143,9 +143,9 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@6747daa138553c20271dc0bd40bfb9522638537c # v0.5.19 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@c1c96dee1b426f3617d5e9caf60fd45899bb8a4f # v0.5.20 - run: flutter pub get - - uses: Telosnex/telosnex_ci_tools/actions/ios-flutter-test@6747daa138553c20271dc0bd40bfb9522638537c # v0.5.19 + - uses: Telosnex/telosnex_ci_tools/actions/ios-flutter-test@c1c96dee1b426f3617d5e9caf60fd45899bb8a4f # v0.5.20 with: working-directory: example/webcrypto_demo_flutter_app target: integration_test/webcrypto_test.dart @@ -159,9 +159,9 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@6747daa138553c20271dc0bd40bfb9522638537c # v0.5.19 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@c1c96dee1b426f3617d5e9caf60fd45899bb8a4f # v0.5.20 - run: flutter pub get - - uses: Telosnex/telosnex_ci_tools/actions/android-emulator@6747daa138553c20271dc0bd40bfb9522638537c # v0.5.19 + - uses: Telosnex/telosnex_ci_tools/actions/android-emulator@c1c96dee1b426f3617d5e9caf60fd45899bb8a4f # v0.5.20 id: emulator with: api-level: '36' @@ -184,8 +184,8 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@6747daa138553c20271dc0bd40bfb9522638537c # v0.5.19 - - uses: Telosnex/telosnex_ci_tools/actions/android-test-apks@6747daa138553c20271dc0bd40bfb9522638537c # v0.5.19 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@c1c96dee1b426f3617d5e9caf60fd45899bb8a4f # v0.5.20 + - uses: Telosnex/telosnex_ci_tools/actions/android-test-apks@c1c96dee1b426f3617d5e9caf60fd45899bb8a4f # v0.5.20 with: working-directory: example/webcrypto_demo_flutter_app targets: integration_test/webcrypto_test.dart @@ -199,11 +199,11 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@6747daa138553c20271dc0bd40bfb9522638537c # v0.5.19 - - uses: Telosnex/telosnex_ci_tools/actions/download-artifact@6747daa138553c20271dc0bd40bfb9522638537c # v0.5.19 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@c1c96dee1b426f3617d5e9caf60fd45899bb8a4f # v0.5.20 + - uses: Telosnex/telosnex_ci_tools/actions/download-artifact@c1c96dee1b426f3617d5e9caf60fd45899bb8a4f # v0.5.20 with: name: android-test-apks path: ${{ runner.temp }}/apks - - uses: Telosnex/telosnex_ci_tools/actions/android-arm64-container@6747daa138553c20271dc0bd40bfb9522638537c # v0.5.19 + - uses: Telosnex/telosnex_ci_tools/actions/android-arm64-container@c1c96dee1b426f3617d5e9caf60fd45899bb8a4f # v0.5.20 with: apks: ${{ runner.temp }}/apks diff --git a/.github/workflows/kiln_check.yml b/.github/workflows/kiln_check.yml index 5f3091cd8..0068fbf47 100644 --- a/.github/workflows/kiln_check.yml +++ b/.github/workflows/kiln_check.yml @@ -16,4 +16,4 @@ permissions: jobs: kiln-check: - uses: Telosnex/telosnex_ci_tools/.github/workflows/kiln_check.yml@6747daa138553c20271dc0bd40bfb9522638537c # v0.5.19 + uses: Telosnex/telosnex_ci_tools/.github/workflows/kiln_check.yml@c1c96dee1b426f3617d5e9caf60fd45899bb8a4f # v0.5.20 diff --git a/.github/workflows/kiln_release.yml b/.github/workflows/kiln_release.yml index b6c505789..235f7aacd 100644 --- a/.github/workflows/kiln_release.yml +++ b/.github/workflows/kiln_release.yml @@ -25,7 +25,7 @@ permissions: jobs: kiln-release: - uses: Telosnex/telosnex_ci_tools/.github/workflows/kiln_release.yml@6747daa138553c20271dc0bd40bfb9522638537c # v0.5.19 + uses: Telosnex/telosnex_ci_tools/.github/workflows/kiln_release.yml@c1c96dee1b426f3617d5e9caf60fd45899bb8a4f # v0.5.20 permissions: contents: write with: diff --git a/.github/workflows/roll_boringssl.yml b/.github/workflows/roll_boringssl.yml index db53fa260..f24289ad2 100644 --- a/.github/workflows/roll_boringssl.yml +++ b/.github/workflows/roll_boringssl.yml @@ -28,7 +28,7 @@ jobs: with: fetch-depth: 0 - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@6747daa138553c20271dc0bd40bfb9522638537c # v0.5.19 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@c1c96dee1b426f3617d5e9caf60fd45899bb8a4f # v0.5.20 - name: Install system dependencies run: | diff --git a/.github/workflows/unit.yml b/.github/workflows/unit.yml index 7ec78604a..f5c56a058 100644 --- a/.github/workflows/unit.yml +++ b/.github/workflows/unit.yml @@ -25,8 +25,8 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@6747daa138553c20271dc0bd40bfb9522638537c # v0.5.19 - - uses: Telosnex/telosnex_ci_tools/actions/lint@6747daa138553c20271dc0bd40bfb9522638537c # v0.5.19 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@c1c96dee1b426f3617d5e9caf60fd45899bb8a4f # v0.5.20 + - uses: Telosnex/telosnex_ci_tools/actions/lint@c1c96dee1b426f3617d5e9caf60fd45899bb8a4f # v0.5.20 - run: flutter pub get - run: dart format --output=none --set-exit-if-changed . - run: dart analyze --fatal-warnings . diff --git a/.github/workflows/upstream_watch.yml b/.github/workflows/upstream_watch.yml index e97e9ac93..bb836c537 100644 --- a/.github/workflows/upstream_watch.yml +++ b/.github/workflows/upstream_watch.yml @@ -16,4 +16,4 @@ permissions: jobs: watch: - uses: Telosnex/telosnex_ci_tools/.github/workflows/upstream_watch.yml@6747daa138553c20271dc0bd40bfb9522638537c # v0.5.19 + uses: Telosnex/telosnex_ci_tools/.github/workflows/upstream_watch.yml@c1c96dee1b426f3617d5e9caf60fd45899bb8a4f # v0.5.20 From bc7fa74ef4967023b8c25aa00147069181b2942b Mon Sep 17 00:00:00 2001 From: James O'Leary Date: Mon, 16 Nov 2026 17:04:13 -0800 Subject: [PATCH 66/69] ci: Telosnex/telosnex_ci_tools v0.5.21 --- .github/workflows/integration.yml | 24 ++++++++++++------------ .github/workflows/kiln_check.yml | 2 +- .github/workflows/kiln_release.yml | 2 +- .github/workflows/roll_boringssl.yml | 2 +- .github/workflows/unit.yml | 4 ++-- .github/workflows/upstream_watch.yml | 2 +- 6 files changed, 18 insertions(+), 18 deletions(-) diff --git a/.github/workflows/integration.yml b/.github/workflows/integration.yml index a1f53e920..82b8ba954 100644 --- a/.github/workflows/integration.yml +++ b/.github/workflows/integration.yml @@ -36,7 +36,7 @@ jobs: run: | sudo apt-get update -qq sudo apt-get install -y -qq clang cmake ninja-build pkg-config libgtk-3-dev xvfb > /dev/null - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@c1c96dee1b426f3617d5e9caf60fd45899bb8a4f # v0.5.20 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@06c7b3a7929d822adcec028a8baf4d7e960bb324 # v0.5.21 - run: flutter pub get - run: dart test --exclude-tags browser-interop -p vm -r expanded - name: Demo app integration test @@ -91,9 +91,9 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@c1c96dee1b426f3617d5e9caf60fd45899bb8a4f # v0.5.20 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@06c7b3a7929d822adcec028a8baf4d7e960bb324 # v0.5.21 - run: flutter pub get - - uses: Telosnex/telosnex_ci_tools/actions/dart-test-browser@c1c96dee1b426f3617d5e9caf60fd45899bb8a4f # v0.5.20 + - uses: Telosnex/telosnex_ci_tools/actions/dart-test-browser@06c7b3a7929d822adcec028a8baf4d7e960bb324 # v0.5.21 with: platform: ${{ env.BROWSER }} compiler: ${{ env.COMPILER }} @@ -143,9 +143,9 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@c1c96dee1b426f3617d5e9caf60fd45899bb8a4f # v0.5.20 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@06c7b3a7929d822adcec028a8baf4d7e960bb324 # v0.5.21 - run: flutter pub get - - uses: Telosnex/telosnex_ci_tools/actions/ios-flutter-test@c1c96dee1b426f3617d5e9caf60fd45899bb8a4f # v0.5.20 + - uses: Telosnex/telosnex_ci_tools/actions/ios-flutter-test@06c7b3a7929d822adcec028a8baf4d7e960bb324 # v0.5.21 with: working-directory: example/webcrypto_demo_flutter_app target: integration_test/webcrypto_test.dart @@ -159,9 +159,9 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@c1c96dee1b426f3617d5e9caf60fd45899bb8a4f # v0.5.20 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@06c7b3a7929d822adcec028a8baf4d7e960bb324 # v0.5.21 - run: flutter pub get - - uses: Telosnex/telosnex_ci_tools/actions/android-emulator@c1c96dee1b426f3617d5e9caf60fd45899bb8a4f # v0.5.20 + - uses: Telosnex/telosnex_ci_tools/actions/android-emulator@06c7b3a7929d822adcec028a8baf4d7e960bb324 # v0.5.21 id: emulator with: api-level: '36' @@ -184,8 +184,8 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@c1c96dee1b426f3617d5e9caf60fd45899bb8a4f # v0.5.20 - - uses: Telosnex/telosnex_ci_tools/actions/android-test-apks@c1c96dee1b426f3617d5e9caf60fd45899bb8a4f # v0.5.20 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@06c7b3a7929d822adcec028a8baf4d7e960bb324 # v0.5.21 + - uses: Telosnex/telosnex_ci_tools/actions/android-test-apks@06c7b3a7929d822adcec028a8baf4d7e960bb324 # v0.5.21 with: working-directory: example/webcrypto_demo_flutter_app targets: integration_test/webcrypto_test.dart @@ -199,11 +199,11 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@c1c96dee1b426f3617d5e9caf60fd45899bb8a4f # v0.5.20 - - uses: Telosnex/telosnex_ci_tools/actions/download-artifact@c1c96dee1b426f3617d5e9caf60fd45899bb8a4f # v0.5.20 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@06c7b3a7929d822adcec028a8baf4d7e960bb324 # v0.5.21 + - uses: Telosnex/telosnex_ci_tools/actions/download-artifact@06c7b3a7929d822adcec028a8baf4d7e960bb324 # v0.5.21 with: name: android-test-apks path: ${{ runner.temp }}/apks - - uses: Telosnex/telosnex_ci_tools/actions/android-arm64-container@c1c96dee1b426f3617d5e9caf60fd45899bb8a4f # v0.5.20 + - uses: Telosnex/telosnex_ci_tools/actions/android-arm64-container@06c7b3a7929d822adcec028a8baf4d7e960bb324 # v0.5.21 with: apks: ${{ runner.temp }}/apks diff --git a/.github/workflows/kiln_check.yml b/.github/workflows/kiln_check.yml index 0068fbf47..84f56d6f3 100644 --- a/.github/workflows/kiln_check.yml +++ b/.github/workflows/kiln_check.yml @@ -16,4 +16,4 @@ permissions: jobs: kiln-check: - uses: Telosnex/telosnex_ci_tools/.github/workflows/kiln_check.yml@c1c96dee1b426f3617d5e9caf60fd45899bb8a4f # v0.5.20 + uses: Telosnex/telosnex_ci_tools/.github/workflows/kiln_check.yml@06c7b3a7929d822adcec028a8baf4d7e960bb324 # v0.5.21 diff --git a/.github/workflows/kiln_release.yml b/.github/workflows/kiln_release.yml index 235f7aacd..2fae0219d 100644 --- a/.github/workflows/kiln_release.yml +++ b/.github/workflows/kiln_release.yml @@ -25,7 +25,7 @@ permissions: jobs: kiln-release: - uses: Telosnex/telosnex_ci_tools/.github/workflows/kiln_release.yml@c1c96dee1b426f3617d5e9caf60fd45899bb8a4f # v0.5.20 + uses: Telosnex/telosnex_ci_tools/.github/workflows/kiln_release.yml@06c7b3a7929d822adcec028a8baf4d7e960bb324 # v0.5.21 permissions: contents: write with: diff --git a/.github/workflows/roll_boringssl.yml b/.github/workflows/roll_boringssl.yml index f24289ad2..736b1c682 100644 --- a/.github/workflows/roll_boringssl.yml +++ b/.github/workflows/roll_boringssl.yml @@ -28,7 +28,7 @@ jobs: with: fetch-depth: 0 - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@c1c96dee1b426f3617d5e9caf60fd45899bb8a4f # v0.5.20 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@06c7b3a7929d822adcec028a8baf4d7e960bb324 # v0.5.21 - name: Install system dependencies run: | diff --git a/.github/workflows/unit.yml b/.github/workflows/unit.yml index f5c56a058..a793c0195 100644 --- a/.github/workflows/unit.yml +++ b/.github/workflows/unit.yml @@ -25,8 +25,8 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@c1c96dee1b426f3617d5e9caf60fd45899bb8a4f # v0.5.20 - - uses: Telosnex/telosnex_ci_tools/actions/lint@c1c96dee1b426f3617d5e9caf60fd45899bb8a4f # v0.5.20 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@06c7b3a7929d822adcec028a8baf4d7e960bb324 # v0.5.21 + - uses: Telosnex/telosnex_ci_tools/actions/lint@06c7b3a7929d822adcec028a8baf4d7e960bb324 # v0.5.21 - run: flutter pub get - run: dart format --output=none --set-exit-if-changed . - run: dart analyze --fatal-warnings . diff --git a/.github/workflows/upstream_watch.yml b/.github/workflows/upstream_watch.yml index bb836c537..b03d0982a 100644 --- a/.github/workflows/upstream_watch.yml +++ b/.github/workflows/upstream_watch.yml @@ -16,4 +16,4 @@ permissions: jobs: watch: - uses: Telosnex/telosnex_ci_tools/.github/workflows/upstream_watch.yml@c1c96dee1b426f3617d5e9caf60fd45899bb8a4f # v0.5.20 + uses: Telosnex/telosnex_ci_tools/.github/workflows/upstream_watch.yml@06c7b3a7929d822adcec028a8baf4d7e960bb324 # v0.5.21 From bb58ebed28356718e78417652681c03dbe78c318 Mon Sep 17 00:00:00 2001 From: James O'Leary Date: Mon, 16 Nov 2026 18:41:50 -0800 Subject: [PATCH 67/69] ci: Telosnex/telosnex_ci_tools v0.5.22 --- .github/workflows/integration.yml | 24 ++++++++++++------------ .github/workflows/kiln_check.yml | 2 +- .github/workflows/kiln_release.yml | 2 +- .github/workflows/roll_boringssl.yml | 2 +- .github/workflows/unit.yml | 4 ++-- .github/workflows/upstream_watch.yml | 2 +- 6 files changed, 18 insertions(+), 18 deletions(-) diff --git a/.github/workflows/integration.yml b/.github/workflows/integration.yml index 82b8ba954..2c1f55141 100644 --- a/.github/workflows/integration.yml +++ b/.github/workflows/integration.yml @@ -36,7 +36,7 @@ jobs: run: | sudo apt-get update -qq sudo apt-get install -y -qq clang cmake ninja-build pkg-config libgtk-3-dev xvfb > /dev/null - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@06c7b3a7929d822adcec028a8baf4d7e960bb324 # v0.5.21 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@93ab9b574e45c5cd2567fbea58acb3d76f7363ca # v0.5.22 - run: flutter pub get - run: dart test --exclude-tags browser-interop -p vm -r expanded - name: Demo app integration test @@ -91,9 +91,9 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@06c7b3a7929d822adcec028a8baf4d7e960bb324 # v0.5.21 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@93ab9b574e45c5cd2567fbea58acb3d76f7363ca # v0.5.22 - run: flutter pub get - - uses: Telosnex/telosnex_ci_tools/actions/dart-test-browser@06c7b3a7929d822adcec028a8baf4d7e960bb324 # v0.5.21 + - uses: Telosnex/telosnex_ci_tools/actions/dart-test-browser@93ab9b574e45c5cd2567fbea58acb3d76f7363ca # v0.5.22 with: platform: ${{ env.BROWSER }} compiler: ${{ env.COMPILER }} @@ -143,9 +143,9 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@06c7b3a7929d822adcec028a8baf4d7e960bb324 # v0.5.21 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@93ab9b574e45c5cd2567fbea58acb3d76f7363ca # v0.5.22 - run: flutter pub get - - uses: Telosnex/telosnex_ci_tools/actions/ios-flutter-test@06c7b3a7929d822adcec028a8baf4d7e960bb324 # v0.5.21 + - uses: Telosnex/telosnex_ci_tools/actions/ios-flutter-test@93ab9b574e45c5cd2567fbea58acb3d76f7363ca # v0.5.22 with: working-directory: example/webcrypto_demo_flutter_app target: integration_test/webcrypto_test.dart @@ -159,9 +159,9 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@06c7b3a7929d822adcec028a8baf4d7e960bb324 # v0.5.21 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@93ab9b574e45c5cd2567fbea58acb3d76f7363ca # v0.5.22 - run: flutter pub get - - uses: Telosnex/telosnex_ci_tools/actions/android-emulator@06c7b3a7929d822adcec028a8baf4d7e960bb324 # v0.5.21 + - uses: Telosnex/telosnex_ci_tools/actions/android-emulator@93ab9b574e45c5cd2567fbea58acb3d76f7363ca # v0.5.22 id: emulator with: api-level: '36' @@ -184,8 +184,8 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@06c7b3a7929d822adcec028a8baf4d7e960bb324 # v0.5.21 - - uses: Telosnex/telosnex_ci_tools/actions/android-test-apks@06c7b3a7929d822adcec028a8baf4d7e960bb324 # v0.5.21 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@93ab9b574e45c5cd2567fbea58acb3d76f7363ca # v0.5.22 + - uses: Telosnex/telosnex_ci_tools/actions/android-test-apks@93ab9b574e45c5cd2567fbea58acb3d76f7363ca # v0.5.22 with: working-directory: example/webcrypto_demo_flutter_app targets: integration_test/webcrypto_test.dart @@ -199,11 +199,11 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@06c7b3a7929d822adcec028a8baf4d7e960bb324 # v0.5.21 - - uses: Telosnex/telosnex_ci_tools/actions/download-artifact@06c7b3a7929d822adcec028a8baf4d7e960bb324 # v0.5.21 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@93ab9b574e45c5cd2567fbea58acb3d76f7363ca # v0.5.22 + - uses: Telosnex/telosnex_ci_tools/actions/download-artifact@93ab9b574e45c5cd2567fbea58acb3d76f7363ca # v0.5.22 with: name: android-test-apks path: ${{ runner.temp }}/apks - - uses: Telosnex/telosnex_ci_tools/actions/android-arm64-container@06c7b3a7929d822adcec028a8baf4d7e960bb324 # v0.5.21 + - uses: Telosnex/telosnex_ci_tools/actions/android-arm64-container@93ab9b574e45c5cd2567fbea58acb3d76f7363ca # v0.5.22 with: apks: ${{ runner.temp }}/apks diff --git a/.github/workflows/kiln_check.yml b/.github/workflows/kiln_check.yml index 84f56d6f3..2139eb8ea 100644 --- a/.github/workflows/kiln_check.yml +++ b/.github/workflows/kiln_check.yml @@ -16,4 +16,4 @@ permissions: jobs: kiln-check: - uses: Telosnex/telosnex_ci_tools/.github/workflows/kiln_check.yml@06c7b3a7929d822adcec028a8baf4d7e960bb324 # v0.5.21 + uses: Telosnex/telosnex_ci_tools/.github/workflows/kiln_check.yml@93ab9b574e45c5cd2567fbea58acb3d76f7363ca # v0.5.22 diff --git a/.github/workflows/kiln_release.yml b/.github/workflows/kiln_release.yml index 2fae0219d..fceba8c51 100644 --- a/.github/workflows/kiln_release.yml +++ b/.github/workflows/kiln_release.yml @@ -25,7 +25,7 @@ permissions: jobs: kiln-release: - uses: Telosnex/telosnex_ci_tools/.github/workflows/kiln_release.yml@06c7b3a7929d822adcec028a8baf4d7e960bb324 # v0.5.21 + uses: Telosnex/telosnex_ci_tools/.github/workflows/kiln_release.yml@93ab9b574e45c5cd2567fbea58acb3d76f7363ca # v0.5.22 permissions: contents: write with: diff --git a/.github/workflows/roll_boringssl.yml b/.github/workflows/roll_boringssl.yml index 736b1c682..0d63a3216 100644 --- a/.github/workflows/roll_boringssl.yml +++ b/.github/workflows/roll_boringssl.yml @@ -28,7 +28,7 @@ jobs: with: fetch-depth: 0 - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@06c7b3a7929d822adcec028a8baf4d7e960bb324 # v0.5.21 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@93ab9b574e45c5cd2567fbea58acb3d76f7363ca # v0.5.22 - name: Install system dependencies run: | diff --git a/.github/workflows/unit.yml b/.github/workflows/unit.yml index a793c0195..7e7d21335 100644 --- a/.github/workflows/unit.yml +++ b/.github/workflows/unit.yml @@ -25,8 +25,8 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@06c7b3a7929d822adcec028a8baf4d7e960bb324 # v0.5.21 - - uses: Telosnex/telosnex_ci_tools/actions/lint@06c7b3a7929d822adcec028a8baf4d7e960bb324 # v0.5.21 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@93ab9b574e45c5cd2567fbea58acb3d76f7363ca # v0.5.22 + - uses: Telosnex/telosnex_ci_tools/actions/lint@93ab9b574e45c5cd2567fbea58acb3d76f7363ca # v0.5.22 - run: flutter pub get - run: dart format --output=none --set-exit-if-changed . - run: dart analyze --fatal-warnings . diff --git a/.github/workflows/upstream_watch.yml b/.github/workflows/upstream_watch.yml index b03d0982a..6947fd369 100644 --- a/.github/workflows/upstream_watch.yml +++ b/.github/workflows/upstream_watch.yml @@ -16,4 +16,4 @@ permissions: jobs: watch: - uses: Telosnex/telosnex_ci_tools/.github/workflows/upstream_watch.yml@06c7b3a7929d822adcec028a8baf4d7e960bb324 # v0.5.21 + uses: Telosnex/telosnex_ci_tools/.github/workflows/upstream_watch.yml@93ab9b574e45c5cd2567fbea58acb3d76f7363ca # v0.5.22 From 9ef76ce07408038245c392932b0fdb4816264e00 Mon Sep 17 00:00:00 2001 From: James O'Leary Date: Mon, 16 Nov 2026 19:02:18 -0800 Subject: [PATCH 68/69] ci: Telosnex/telosnex_ci_tools v0.5.23 --- .github/workflows/integration.yml | 24 ++++++++++++------------ .github/workflows/kiln_check.yml | 2 +- .github/workflows/kiln_release.yml | 2 +- .github/workflows/roll_boringssl.yml | 2 +- .github/workflows/unit.yml | 4 ++-- .github/workflows/upstream_watch.yml | 2 +- 6 files changed, 18 insertions(+), 18 deletions(-) diff --git a/.github/workflows/integration.yml b/.github/workflows/integration.yml index 2c1f55141..8067b3d29 100644 --- a/.github/workflows/integration.yml +++ b/.github/workflows/integration.yml @@ -36,7 +36,7 @@ jobs: run: | sudo apt-get update -qq sudo apt-get install -y -qq clang cmake ninja-build pkg-config libgtk-3-dev xvfb > /dev/null - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@93ab9b574e45c5cd2567fbea58acb3d76f7363ca # v0.5.22 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@d807dca8c92f4b85be742a04601a47654f0e41a0 # v0.5.23 - run: flutter pub get - run: dart test --exclude-tags browser-interop -p vm -r expanded - name: Demo app integration test @@ -91,9 +91,9 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@93ab9b574e45c5cd2567fbea58acb3d76f7363ca # v0.5.22 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@d807dca8c92f4b85be742a04601a47654f0e41a0 # v0.5.23 - run: flutter pub get - - uses: Telosnex/telosnex_ci_tools/actions/dart-test-browser@93ab9b574e45c5cd2567fbea58acb3d76f7363ca # v0.5.22 + - uses: Telosnex/telosnex_ci_tools/actions/dart-test-browser@d807dca8c92f4b85be742a04601a47654f0e41a0 # v0.5.23 with: platform: ${{ env.BROWSER }} compiler: ${{ env.COMPILER }} @@ -143,9 +143,9 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@93ab9b574e45c5cd2567fbea58acb3d76f7363ca # v0.5.22 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@d807dca8c92f4b85be742a04601a47654f0e41a0 # v0.5.23 - run: flutter pub get - - uses: Telosnex/telosnex_ci_tools/actions/ios-flutter-test@93ab9b574e45c5cd2567fbea58acb3d76f7363ca # v0.5.22 + - uses: Telosnex/telosnex_ci_tools/actions/ios-flutter-test@d807dca8c92f4b85be742a04601a47654f0e41a0 # v0.5.23 with: working-directory: example/webcrypto_demo_flutter_app target: integration_test/webcrypto_test.dart @@ -159,9 +159,9 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@93ab9b574e45c5cd2567fbea58acb3d76f7363ca # v0.5.22 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@d807dca8c92f4b85be742a04601a47654f0e41a0 # v0.5.23 - run: flutter pub get - - uses: Telosnex/telosnex_ci_tools/actions/android-emulator@93ab9b574e45c5cd2567fbea58acb3d76f7363ca # v0.5.22 + - uses: Telosnex/telosnex_ci_tools/actions/android-emulator@d807dca8c92f4b85be742a04601a47654f0e41a0 # v0.5.23 id: emulator with: api-level: '36' @@ -184,8 +184,8 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@93ab9b574e45c5cd2567fbea58acb3d76f7363ca # v0.5.22 - - uses: Telosnex/telosnex_ci_tools/actions/android-test-apks@93ab9b574e45c5cd2567fbea58acb3d76f7363ca # v0.5.22 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@d807dca8c92f4b85be742a04601a47654f0e41a0 # v0.5.23 + - uses: Telosnex/telosnex_ci_tools/actions/android-test-apks@d807dca8c92f4b85be742a04601a47654f0e41a0 # v0.5.23 with: working-directory: example/webcrypto_demo_flutter_app targets: integration_test/webcrypto_test.dart @@ -199,11 +199,11 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@93ab9b574e45c5cd2567fbea58acb3d76f7363ca # v0.5.22 - - uses: Telosnex/telosnex_ci_tools/actions/download-artifact@93ab9b574e45c5cd2567fbea58acb3d76f7363ca # v0.5.22 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@d807dca8c92f4b85be742a04601a47654f0e41a0 # v0.5.23 + - uses: Telosnex/telosnex_ci_tools/actions/download-artifact@d807dca8c92f4b85be742a04601a47654f0e41a0 # v0.5.23 with: name: android-test-apks path: ${{ runner.temp }}/apks - - uses: Telosnex/telosnex_ci_tools/actions/android-arm64-container@93ab9b574e45c5cd2567fbea58acb3d76f7363ca # v0.5.22 + - uses: Telosnex/telosnex_ci_tools/actions/android-arm64-container@d807dca8c92f4b85be742a04601a47654f0e41a0 # v0.5.23 with: apks: ${{ runner.temp }}/apks diff --git a/.github/workflows/kiln_check.yml b/.github/workflows/kiln_check.yml index 2139eb8ea..8b2a3c010 100644 --- a/.github/workflows/kiln_check.yml +++ b/.github/workflows/kiln_check.yml @@ -16,4 +16,4 @@ permissions: jobs: kiln-check: - uses: Telosnex/telosnex_ci_tools/.github/workflows/kiln_check.yml@93ab9b574e45c5cd2567fbea58acb3d76f7363ca # v0.5.22 + uses: Telosnex/telosnex_ci_tools/.github/workflows/kiln_check.yml@d807dca8c92f4b85be742a04601a47654f0e41a0 # v0.5.23 diff --git a/.github/workflows/kiln_release.yml b/.github/workflows/kiln_release.yml index fceba8c51..d688306b8 100644 --- a/.github/workflows/kiln_release.yml +++ b/.github/workflows/kiln_release.yml @@ -25,7 +25,7 @@ permissions: jobs: kiln-release: - uses: Telosnex/telosnex_ci_tools/.github/workflows/kiln_release.yml@93ab9b574e45c5cd2567fbea58acb3d76f7363ca # v0.5.22 + uses: Telosnex/telosnex_ci_tools/.github/workflows/kiln_release.yml@d807dca8c92f4b85be742a04601a47654f0e41a0 # v0.5.23 permissions: contents: write with: diff --git a/.github/workflows/roll_boringssl.yml b/.github/workflows/roll_boringssl.yml index 0d63a3216..c1863ceea 100644 --- a/.github/workflows/roll_boringssl.yml +++ b/.github/workflows/roll_boringssl.yml @@ -28,7 +28,7 @@ jobs: with: fetch-depth: 0 - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@93ab9b574e45c5cd2567fbea58acb3d76f7363ca # v0.5.22 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@d807dca8c92f4b85be742a04601a47654f0e41a0 # v0.5.23 - name: Install system dependencies run: | diff --git a/.github/workflows/unit.yml b/.github/workflows/unit.yml index 7e7d21335..ff3d97ae2 100644 --- a/.github/workflows/unit.yml +++ b/.github/workflows/unit.yml @@ -25,8 +25,8 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@93ab9b574e45c5cd2567fbea58acb3d76f7363ca # v0.5.22 - - uses: Telosnex/telosnex_ci_tools/actions/lint@93ab9b574e45c5cd2567fbea58acb3d76f7363ca # v0.5.22 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@d807dca8c92f4b85be742a04601a47654f0e41a0 # v0.5.23 + - uses: Telosnex/telosnex_ci_tools/actions/lint@d807dca8c92f4b85be742a04601a47654f0e41a0 # v0.5.23 - run: flutter pub get - run: dart format --output=none --set-exit-if-changed . - run: dart analyze --fatal-warnings . diff --git a/.github/workflows/upstream_watch.yml b/.github/workflows/upstream_watch.yml index 6947fd369..dac271b43 100644 --- a/.github/workflows/upstream_watch.yml +++ b/.github/workflows/upstream_watch.yml @@ -16,4 +16,4 @@ permissions: jobs: watch: - uses: Telosnex/telosnex_ci_tools/.github/workflows/upstream_watch.yml@93ab9b574e45c5cd2567fbea58acb3d76f7363ca # v0.5.22 + uses: Telosnex/telosnex_ci_tools/.github/workflows/upstream_watch.yml@d807dca8c92f4b85be742a04601a47654f0e41a0 # v0.5.23 From 2c1fc417334e24bd6efd026b51d1a5154499d643 Mon Sep 17 00:00:00 2001 From: James O'Leary <65884233+jpohhhh@users.noreply.github.com> Date: Thu, 19 Nov 2026 10:26:55 -0800 Subject: [PATCH 69/69] ci: Telosnex/telosnex_ci_tools v0.5.24 (#6) * ci: Telosnex/telosnex_ci_tools v0.5.24 * ci(android): update demo build for Flutter 3.50 --- .github/workflows/integration.yml | 24 +++++++++---------- .github/workflows/kiln_check.yml | 2 +- .github/workflows/kiln_release.yml | 2 +- .github/workflows/roll_boringssl.yml | 2 +- .github/workflows/unit.yml | 4 ++-- .github/workflows/upstream_watch.yml | 2 +- .../android/app/build.gradle.kts | 10 ++++---- .../android/gradle.properties | 3 +++ .../gradle/wrapper/gradle-wrapper.properties | 2 +- .../android/settings.gradle.kts | 4 ++-- 10 files changed, 30 insertions(+), 25 deletions(-) diff --git a/.github/workflows/integration.yml b/.github/workflows/integration.yml index 8067b3d29..848e285e9 100644 --- a/.github/workflows/integration.yml +++ b/.github/workflows/integration.yml @@ -36,7 +36,7 @@ jobs: run: | sudo apt-get update -qq sudo apt-get install -y -qq clang cmake ninja-build pkg-config libgtk-3-dev xvfb > /dev/null - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@d807dca8c92f4b85be742a04601a47654f0e41a0 # v0.5.23 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@0ab334b66d7bb8ff0f436b8e9739eb014ab35b79 # v0.5.24 - run: flutter pub get - run: dart test --exclude-tags browser-interop -p vm -r expanded - name: Demo app integration test @@ -91,9 +91,9 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@d807dca8c92f4b85be742a04601a47654f0e41a0 # v0.5.23 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@0ab334b66d7bb8ff0f436b8e9739eb014ab35b79 # v0.5.24 - run: flutter pub get - - uses: Telosnex/telosnex_ci_tools/actions/dart-test-browser@d807dca8c92f4b85be742a04601a47654f0e41a0 # v0.5.23 + - uses: Telosnex/telosnex_ci_tools/actions/dart-test-browser@0ab334b66d7bb8ff0f436b8e9739eb014ab35b79 # v0.5.24 with: platform: ${{ env.BROWSER }} compiler: ${{ env.COMPILER }} @@ -143,9 +143,9 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@d807dca8c92f4b85be742a04601a47654f0e41a0 # v0.5.23 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@0ab334b66d7bb8ff0f436b8e9739eb014ab35b79 # v0.5.24 - run: flutter pub get - - uses: Telosnex/telosnex_ci_tools/actions/ios-flutter-test@d807dca8c92f4b85be742a04601a47654f0e41a0 # v0.5.23 + - uses: Telosnex/telosnex_ci_tools/actions/ios-flutter-test@0ab334b66d7bb8ff0f436b8e9739eb014ab35b79 # v0.5.24 with: working-directory: example/webcrypto_demo_flutter_app target: integration_test/webcrypto_test.dart @@ -159,9 +159,9 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@d807dca8c92f4b85be742a04601a47654f0e41a0 # v0.5.23 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@0ab334b66d7bb8ff0f436b8e9739eb014ab35b79 # v0.5.24 - run: flutter pub get - - uses: Telosnex/telosnex_ci_tools/actions/android-emulator@d807dca8c92f4b85be742a04601a47654f0e41a0 # v0.5.23 + - uses: Telosnex/telosnex_ci_tools/actions/android-emulator@0ab334b66d7bb8ff0f436b8e9739eb014ab35b79 # v0.5.24 id: emulator with: api-level: '36' @@ -184,8 +184,8 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@d807dca8c92f4b85be742a04601a47654f0e41a0 # v0.5.23 - - uses: Telosnex/telosnex_ci_tools/actions/android-test-apks@d807dca8c92f4b85be742a04601a47654f0e41a0 # v0.5.23 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@0ab334b66d7bb8ff0f436b8e9739eb014ab35b79 # v0.5.24 + - uses: Telosnex/telosnex_ci_tools/actions/android-test-apks@0ab334b66d7bb8ff0f436b8e9739eb014ab35b79 # v0.5.24 with: working-directory: example/webcrypto_demo_flutter_app targets: integration_test/webcrypto_test.dart @@ -199,11 +199,11 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@d807dca8c92f4b85be742a04601a47654f0e41a0 # v0.5.23 - - uses: Telosnex/telosnex_ci_tools/actions/download-artifact@d807dca8c92f4b85be742a04601a47654f0e41a0 # v0.5.23 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@0ab334b66d7bb8ff0f436b8e9739eb014ab35b79 # v0.5.24 + - uses: Telosnex/telosnex_ci_tools/actions/download-artifact@0ab334b66d7bb8ff0f436b8e9739eb014ab35b79 # v0.5.24 with: name: android-test-apks path: ${{ runner.temp }}/apks - - uses: Telosnex/telosnex_ci_tools/actions/android-arm64-container@d807dca8c92f4b85be742a04601a47654f0e41a0 # v0.5.23 + - uses: Telosnex/telosnex_ci_tools/actions/android-arm64-container@0ab334b66d7bb8ff0f436b8e9739eb014ab35b79 # v0.5.24 with: apks: ${{ runner.temp }}/apks diff --git a/.github/workflows/kiln_check.yml b/.github/workflows/kiln_check.yml index 8b2a3c010..559f226bf 100644 --- a/.github/workflows/kiln_check.yml +++ b/.github/workflows/kiln_check.yml @@ -16,4 +16,4 @@ permissions: jobs: kiln-check: - uses: Telosnex/telosnex_ci_tools/.github/workflows/kiln_check.yml@d807dca8c92f4b85be742a04601a47654f0e41a0 # v0.5.23 + uses: Telosnex/telosnex_ci_tools/.github/workflows/kiln_check.yml@0ab334b66d7bb8ff0f436b8e9739eb014ab35b79 # v0.5.24 diff --git a/.github/workflows/kiln_release.yml b/.github/workflows/kiln_release.yml index d688306b8..d65caf8f4 100644 --- a/.github/workflows/kiln_release.yml +++ b/.github/workflows/kiln_release.yml @@ -25,7 +25,7 @@ permissions: jobs: kiln-release: - uses: Telosnex/telosnex_ci_tools/.github/workflows/kiln_release.yml@d807dca8c92f4b85be742a04601a47654f0e41a0 # v0.5.23 + uses: Telosnex/telosnex_ci_tools/.github/workflows/kiln_release.yml@0ab334b66d7bb8ff0f436b8e9739eb014ab35b79 # v0.5.24 permissions: contents: write with: diff --git a/.github/workflows/roll_boringssl.yml b/.github/workflows/roll_boringssl.yml index c1863ceea..a7831d551 100644 --- a/.github/workflows/roll_boringssl.yml +++ b/.github/workflows/roll_boringssl.yml @@ -28,7 +28,7 @@ jobs: with: fetch-depth: 0 - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@d807dca8c92f4b85be742a04601a47654f0e41a0 # v0.5.23 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@0ab334b66d7bb8ff0f436b8e9739eb014ab35b79 # v0.5.24 - name: Install system dependencies run: | diff --git a/.github/workflows/unit.yml b/.github/workflows/unit.yml index ff3d97ae2..44784266a 100644 --- a/.github/workflows/unit.yml +++ b/.github/workflows/unit.yml @@ -25,8 +25,8 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@d807dca8c92f4b85be742a04601a47654f0e41a0 # v0.5.23 - - uses: Telosnex/telosnex_ci_tools/actions/lint@d807dca8c92f4b85be742a04601a47654f0e41a0 # v0.5.23 + - uses: Telosnex/telosnex_ci_tools/actions/setup-flutter@0ab334b66d7bb8ff0f436b8e9739eb014ab35b79 # v0.5.24 + - uses: Telosnex/telosnex_ci_tools/actions/lint@0ab334b66d7bb8ff0f436b8e9739eb014ab35b79 # v0.5.24 - run: flutter pub get - run: dart format --output=none --set-exit-if-changed . - run: dart analyze --fatal-warnings . diff --git a/.github/workflows/upstream_watch.yml b/.github/workflows/upstream_watch.yml index dac271b43..2525e081a 100644 --- a/.github/workflows/upstream_watch.yml +++ b/.github/workflows/upstream_watch.yml @@ -16,4 +16,4 @@ permissions: jobs: watch: - uses: Telosnex/telosnex_ci_tools/.github/workflows/upstream_watch.yml@d807dca8c92f4b85be742a04601a47654f0e41a0 # v0.5.23 + uses: Telosnex/telosnex_ci_tools/.github/workflows/upstream_watch.yml@0ab334b66d7bb8ff0f436b8e9739eb014ab35b79 # v0.5.24 diff --git a/example/webcrypto_demo_flutter_app/android/app/build.gradle.kts b/example/webcrypto_demo_flutter_app/android/app/build.gradle.kts index e3e966244..fce6104da 100644 --- a/example/webcrypto_demo_flutter_app/android/app/build.gradle.kts +++ b/example/webcrypto_demo_flutter_app/android/app/build.gradle.kts @@ -15,10 +15,6 @@ android { targetCompatibility = JavaVersion.VERSION_17 } - kotlinOptions { - jvmTarget = JavaVersion.VERSION_17.toString() - } - defaultConfig { // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html). applicationId = "com.example.webcrypto_example" @@ -40,6 +36,12 @@ android { } } +kotlin { + compilerOptions { + jvmTarget.set(org.jetbrains.kotlin.gradle.dsl.JvmTarget.JVM_17) + } +} + flutter { source = "../.." } diff --git a/example/webcrypto_demo_flutter_app/android/gradle.properties b/example/webcrypto_demo_flutter_app/android/gradle.properties index fbee1d8cd..71a201228 100644 --- a/example/webcrypto_demo_flutter_app/android/gradle.properties +++ b/example/webcrypto_demo_flutter_app/android/gradle.properties @@ -1,2 +1,5 @@ org.gradle.jvmargs=-Xmx8G -XX:MaxMetaspaceSize=4G -XX:ReservedCodeCacheSize=512m -XX:+HeapDumpOnOutOfMemoryError android.useAndroidX=true +# Retain Flutter's compatibility mode for the legacy Android and Kotlin plugins. +android.newDsl=false +android.builtInKotlin=false diff --git a/example/webcrypto_demo_flutter_app/android/gradle/wrapper/gradle-wrapper.properties b/example/webcrypto_demo_flutter_app/android/gradle/wrapper/gradle-wrapper.properties index e4ef43fb9..76d4386de 100644 --- a/example/webcrypto_demo_flutter_app/android/gradle/wrapper/gradle-wrapper.properties +++ b/example/webcrypto_demo_flutter_app/android/gradle/wrapper/gradle-wrapper.properties @@ -2,4 +2,4 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-8.14-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-9.5.0-all.zip diff --git a/example/webcrypto_demo_flutter_app/android/settings.gradle.kts b/example/webcrypto_demo_flutter_app/android/settings.gradle.kts index ca7fe065c..989ac5e46 100644 --- a/example/webcrypto_demo_flutter_app/android/settings.gradle.kts +++ b/example/webcrypto_demo_flutter_app/android/settings.gradle.kts @@ -19,8 +19,8 @@ pluginManagement { plugins { id("dev.flutter.flutter-plugin-loader") version "1.0.0" - id("com.android.application") version "8.11.1" apply false - id("org.jetbrains.kotlin.android") version "2.2.20" apply false + id("com.android.application") version "9.3.1" apply false + id("org.jetbrains.kotlin.android") version "2.4.20" apply false } include(":app")