• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/* Copyright (c) 2016, Google Inc.
2 *
3 * Permission to use, copy, modify, and/or distribute this software for any
4 * purpose with or without fee is hereby granted, provided that the above
5 * copyright notice and this permission notice appear in all copies.
6 *
7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14
15package main
16
17import (
18	"flag"
19	"fmt"
20	"os"
21	"os/exec"
22	"path/filepath"
23	"strings"
24	"syscall"
25)
26
27var (
28	boringsslDir = flag.String("boringssl", ".", "The path to the BoringSSL checkout.")
29	opensslDir   = flag.String("openssl", filepath.Join("..", "openssl"), "The path to the OpenSSL checkout.")
30)
31
32func mapName(path string) string {
33	path = strings.Replace(path, filepath.FromSlash("/fipsmodule/"), string(filepath.Separator), 1)
34	switch filepath.ToSlash(path) {
35	case "crypto/cipher_extra/asm/aes128gcmsiv-x86_64.pl", "crypto/cipher_extra/asm/chacha20_poly1305_x86_64.pl", "crypto/rand/asm/rdrand-x86_64.pl":
36		return ""
37	case "crypto/ec/asm/p256-x86_64-asm.pl":
38		return filepath.FromSlash("crypto/ec/asm/ecp_nistz256-x86_64.pl")
39	}
40	return path
41}
42
43func diff(from, to string) error {
44	cmd := exec.Command("diff", "-u", "--", from, to)
45	cmd.Stdout = os.Stdout
46	cmd.Stderr = os.Stderr
47	err := cmd.Run()
48	// diff returns exit code 1 if the files differ but it was otherwise
49	// successful.
50	if exitError, ok := err.(*exec.ExitError); ok && exitError.Sys().(syscall.WaitStatus).ExitStatus() == 1 {
51		return nil
52	}
53	return err
54}
55
56func main() {
57	flag.Usage = func() {
58		fmt.Fprintf(os.Stderr, "Usage: diff_asm [flag...] [filter...]\n")
59		fmt.Fprintf(os.Stderr, "Filter arguments limit to assembly files which match arguments.\n")
60		fmt.Fprintf(os.Stderr, "If not using a filter, piping to `diffstat` may be useful.\n\n")
61		flag.PrintDefaults()
62	}
63	flag.Parse()
64
65	// Find all the assembly files.
66	var files []string
67	err := filepath.Walk(*boringsslDir, func(path string, info os.FileInfo, err error) error {
68		if err != nil {
69			return nil
70		}
71
72		path, err = filepath.Rel(*boringsslDir, path)
73		if err != nil {
74			return err
75		}
76
77		dir := filepath.Base(filepath.Dir(path))
78		if !info.IsDir() && (dir == "asm" || dir == "perlasm") && strings.HasSuffix(filepath.Base(path), ".pl") {
79			files = append(files, path)
80		}
81
82		return nil
83	})
84	if err != nil {
85		fmt.Fprintf(os.Stderr, "Error finding assembly: %s\n", err)
86		os.Exit(1)
87	}
88
89	for _, file := range files {
90		opensslFile := mapName(file)
91		if len(opensslFile) == 0 {
92			continue
93		}
94
95		if flag.NArg() > 0 {
96			var found bool
97			for _, arg := range flag.Args() {
98				if strings.Contains(file, arg) {
99					found = true
100					break
101				}
102			}
103			if !found {
104				continue
105			}
106		}
107
108		if err := diff(filepath.Join(*opensslDir, opensslFile), filepath.Join(*boringsslDir, file)); err != nil {
109			fmt.Fprintf(os.Stderr, "Error comparing %s: %s\n", file, err)
110			os.Exit(1)
111		}
112	}
113}
114