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 pathSlash := filepath.ToSlash(path) 35 if strings.HasPrefix(pathSlash, "crypto/test/") { 36 return "" 37 } 38 switch pathSlash { 39 case "crypto/aes/asm/vpaes-armv7.pl", 40 "crypto/cipher_extra/asm/aes128gcmsiv-x86_64.pl", 41 "crypto/cipher_extra/asm/chacha20_poly1305_x86_64.pl", 42 "crypto/ec/asm/p256_beeu-x86_64-asm.pl", 43 "crypto/modes/asm/ghash-neon-armv8.pl", 44 "crypto/modes/asm/ghash-ssse3-x86.pl", 45 "crypto/modes/asm/ghash-ssse3-x86_64.pl", 46 "crypto/rand/asm/rdrand-x86_64.pl": 47 return "" 48 case "crypto/ec/asm/p256-x86_64-asm.pl": 49 return filepath.FromSlash("crypto/ec/asm/ecp_nistz256-x86_64.pl") 50 } 51 return path 52} 53 54func diff(from, to string) error { 55 cmd := exec.Command("diff", "-u", "--", from, to) 56 cmd.Stdout = os.Stdout 57 cmd.Stderr = os.Stderr 58 err := cmd.Run() 59 // diff returns exit code 1 if the files differ but it was otherwise 60 // successful. 61 if exitError, ok := err.(*exec.ExitError); ok && exitError.Sys().(syscall.WaitStatus).ExitStatus() == 1 { 62 return nil 63 } 64 return err 65} 66 67func main() { 68 flag.Usage = func() { 69 fmt.Fprintf(os.Stderr, "Usage: diff_asm [flag...] [filter...]\n") 70 fmt.Fprintf(os.Stderr, "Filter arguments limit to assembly files which match arguments.\n") 71 fmt.Fprintf(os.Stderr, "If not using a filter, piping to `diffstat` may be useful.\n\n") 72 flag.PrintDefaults() 73 } 74 flag.Parse() 75 76 // Find all the assembly files. 77 var files []string 78 err := filepath.Walk(*boringsslDir, func(path string, info os.FileInfo, err error) error { 79 if err != nil { 80 return nil 81 } 82 83 path, err = filepath.Rel(*boringsslDir, path) 84 if err != nil { 85 return err 86 } 87 88 dir := filepath.Base(filepath.Dir(path)) 89 if !info.IsDir() && (dir == "asm" || dir == "perlasm") && strings.HasSuffix(filepath.Base(path), ".pl") { 90 files = append(files, path) 91 } 92 93 return nil 94 }) 95 if err != nil { 96 fmt.Fprintf(os.Stderr, "Error finding assembly: %s\n", err) 97 os.Exit(1) 98 } 99 100 for _, file := range files { 101 opensslFile := mapName(file) 102 if len(opensslFile) == 0 { 103 continue 104 } 105 106 if flag.NArg() > 0 { 107 var found bool 108 for _, arg := range flag.Args() { 109 if strings.Contains(file, arg) { 110 found = true 111 break 112 } 113 } 114 if !found { 115 continue 116 } 117 } 118 119 if err := diff(filepath.Join(*opensslDir, opensslFile), filepath.Join(*boringsslDir, file)); err != nil { 120 fmt.Fprintf(os.Stderr, "Error comparing %s: %s\n", file, err) 121 os.Exit(1) 122 } 123 } 124} 125