1// Copyright 2016 The BoringSSL Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//go:build ignore
16
17package main
18
19import (
20	"flag"
21	"fmt"
22	"os"
23	"os/exec"
24	"path/filepath"
25	"strings"
26	"syscall"
27)
28
29var (
30	boringsslDir = flag.String("boringssl", ".", "The path to the BoringSSL checkout.")
31	opensslDir   = flag.String("openssl", filepath.Join("..", "openssl"), "The path to the OpenSSL checkout.")
32)
33
34func mapName(path string) string {
35	path = strings.Replace(path, filepath.FromSlash("/fipsmodule/"), string(filepath.Separator), 1)
36	pathSlash := filepath.ToSlash(path)
37	if strings.HasPrefix(pathSlash, "crypto/test/") {
38		return ""
39	}
40	switch pathSlash {
41	case "crypto/aes/asm/vpaes-armv7.pl",
42		"crypto/bn/asm/bn-armv8.pl",
43		"crypto/cipher/asm/aes128gcmsiv-x86_64.pl",
44		"crypto/cipher/asm/chacha20_poly1305_armv8.pl",
45		"crypto/cipher/asm/chacha20_poly1305_x86_64.pl",
46		"crypto/ec/asm/p256_beeu-armv8-asm.pl",
47		"crypto/ec/asm/p256_beeu-x86_64-asm.pl",
48		"crypto/modes/asm/aesv8-gcm-armv8.pl",
49		"crypto/modes/asm/ghash-neon-armv8.pl",
50		"crypto/modes/asm/ghash-ssse3-x86.pl",
51		"crypto/modes/asm/ghash-ssse3-x86_64.pl",
52		"crypto/rand/asm/rdrand-x86_64.pl":
53		return ""
54	case "crypto/ec/asm/p256-x86_64-asm.pl":
55		return filepath.FromSlash("crypto/ec/asm/ecp_nistz256-x86_64.pl")
56	case "crypto/ec/asm/p256-armv8-asm.pl":
57		return filepath.FromSlash("crypto/ec/asm/ecp_nistz256-armv8.pl")
58	}
59	return path
60}
61
62func diff(from, to string) error {
63	cmd := exec.Command("diff", "-u", "--", from, to)
64	cmd.Stdout = os.Stdout
65	cmd.Stderr = os.Stderr
66	err := cmd.Run()
67	// diff returns exit code 1 if the files differ but it was otherwise
68	// successful.
69	if exitError, ok := err.(*exec.ExitError); ok && exitError.Sys().(syscall.WaitStatus).ExitStatus() == 1 {
70		return nil
71	}
72	return err
73}
74
75func main() {
76	flag.Usage = func() {
77		fmt.Fprintf(os.Stderr, "Usage: diff_asm [flag...] [filter...]\n")
78		fmt.Fprintf(os.Stderr, "Filter arguments limit to assembly files which match arguments.\n")
79		fmt.Fprintf(os.Stderr, "If not using a filter, piping to `diffstat` may be useful.\n\n")
80		flag.PrintDefaults()
81	}
82	flag.Parse()
83
84	// Find all the assembly files.
85	var files []string
86	err := filepath.Walk(*boringsslDir, func(path string, info os.FileInfo, err error) error {
87		if err != nil {
88			return nil
89		}
90
91		path, err = filepath.Rel(*boringsslDir, path)
92		if err != nil {
93			return err
94		}
95
96		dir := filepath.Base(filepath.Dir(path))
97		if !info.IsDir() && (dir == "asm" || dir == "perlasm") && strings.HasSuffix(filepath.Base(path), ".pl") {
98			files = append(files, path)
99		}
100
101		return nil
102	})
103	if err != nil {
104		fmt.Fprintf(os.Stderr, "Error finding assembly: %s\n", err)
105		os.Exit(1)
106	}
107
108	for _, file := range files {
109		opensslFile := mapName(file)
110		if len(opensslFile) == 0 {
111			continue
112		}
113
114		if flag.NArg() > 0 {
115			var found bool
116			for _, arg := range flag.Args() {
117				if strings.Contains(file, arg) {
118					found = true
119					break
120				}
121			}
122			if !found {
123				continue
124			}
125		}
126
127		if err := diff(filepath.Join(*opensslDir, opensslFile), filepath.Join(*boringsslDir, file)); err != nil {
128			fmt.Fprintf(os.Stderr, "Error comparing %s: %s\n", file, err)
129			os.Exit(1)
130		}
131	}
132}
133