...

Source file src/golang.org/x/crypto/ripemd160/ripemd160block.go

Documentation: golang.org/x/crypto/ripemd160

     1  // Copyright 2010 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // RIPEMD-160 block step.
     6  // In its own file so that a faster assembly or C version
     7  // can be substituted easily.
     8  
     9  package ripemd160
    10  
    11  import (
    12  	"math/bits"
    13  )
    14  
    15  // work buffer indices and roll amounts for one line
    16  var _n = [80]uint{
    17  	0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
    18  	7, 4, 13, 1, 10, 6, 15, 3, 12, 0, 9, 5, 2, 14, 11, 8,
    19  	3, 10, 14, 4, 9, 15, 8, 1, 2, 7, 0, 6, 13, 11, 5, 12,
    20  	1, 9, 11, 10, 0, 8, 12, 4, 13, 3, 7, 15, 14, 5, 6, 2,
    21  	4, 0, 5, 9, 7, 12, 2, 10, 14, 1, 3, 8, 11, 6, 15, 13,
    22  }
    23  
    24  var _r = [80]uint{
    25  	11, 14, 15, 12, 5, 8, 7, 9, 11, 13, 14, 15, 6, 7, 9, 8,
    26  	7, 6, 8, 13, 11, 9, 7, 15, 7, 12, 15, 9, 11, 7, 13, 12,
    27  	11, 13, 6, 7, 14, 9, 13, 15, 14, 8, 13, 6, 5, 12, 7, 5,
    28  	11, 12, 14, 15, 14, 15, 9, 8, 9, 14, 5, 6, 8, 6, 5, 12,
    29  	9, 15, 5, 11, 6, 8, 13, 12, 5, 12, 13, 14, 11, 8, 5, 6,
    30  }
    31  
    32  // same for the other parallel one
    33  var n_ = [80]uint{
    34  	5, 14, 7, 0, 9, 2, 11, 4, 13, 6, 15, 8, 1, 10, 3, 12,
    35  	6, 11, 3, 7, 0, 13, 5, 10, 14, 15, 8, 12, 4, 9, 1, 2,
    36  	15, 5, 1, 3, 7, 14, 6, 9, 11, 8, 12, 2, 10, 0, 4, 13,
    37  	8, 6, 4, 1, 3, 11, 15, 0, 5, 12, 2, 13, 9, 7, 10, 14,
    38  	12, 15, 10, 4, 1, 5, 8, 7, 6, 2, 13, 14, 0, 3, 9, 11,
    39  }
    40  
    41  var r_ = [80]uint{
    42  	8, 9, 9, 11, 13, 15, 15, 5, 7, 7, 8, 11, 14, 14, 12, 6,
    43  	9, 13, 15, 7, 12, 8, 9, 11, 7, 7, 12, 7, 6, 15, 13, 11,
    44  	9, 7, 15, 11, 8, 6, 6, 14, 12, 13, 5, 14, 13, 13, 7, 5,
    45  	15, 5, 8, 11, 14, 14, 6, 14, 6, 9, 12, 9, 12, 5, 15, 8,
    46  	8, 5, 12, 9, 12, 5, 14, 6, 8, 13, 6, 5, 15, 13, 11, 11,
    47  }
    48  
    49  func _Block(md *digest, p []byte) int {
    50  	n := 0
    51  	var x [16]uint32
    52  	var alpha, beta uint32
    53  	for len(p) >= BlockSize {
    54  		a, b, c, d, e := md.s[0], md.s[1], md.s[2], md.s[3], md.s[4]
    55  		aa, bb, cc, dd, ee := a, b, c, d, e
    56  		j := 0
    57  		for i := 0; i < 16; i++ {
    58  			x[i] = uint32(p[j]) | uint32(p[j+1])<<8 | uint32(p[j+2])<<16 | uint32(p[j+3])<<24
    59  			j += 4
    60  		}
    61  
    62  		// round 1
    63  		i := 0
    64  		for i < 16 {
    65  			alpha = a + (b ^ c ^ d) + x[_n[i]]
    66  			s := int(_r[i])
    67  			alpha = bits.RotateLeft32(alpha, s) + e
    68  			beta = bits.RotateLeft32(c, 10)
    69  			a, b, c, d, e = e, alpha, b, beta, d
    70  
    71  			// parallel line
    72  			alpha = aa + (bb ^ (cc | ^dd)) + x[n_[i]] + 0x50a28be6
    73  			s = int(r_[i])
    74  			alpha = bits.RotateLeft32(alpha, s) + ee
    75  			beta = bits.RotateLeft32(cc, 10)
    76  			aa, bb, cc, dd, ee = ee, alpha, bb, beta, dd
    77  
    78  			i++
    79  		}
    80  
    81  		// round 2
    82  		for i < 32 {
    83  			alpha = a + (b&c | ^b&d) + x[_n[i]] + 0x5a827999
    84  			s := int(_r[i])
    85  			alpha = bits.RotateLeft32(alpha, s) + e
    86  			beta = bits.RotateLeft32(c, 10)
    87  			a, b, c, d, e = e, alpha, b, beta, d
    88  
    89  			// parallel line
    90  			alpha = aa + (bb&dd | cc&^dd) + x[n_[i]] + 0x5c4dd124
    91  			s = int(r_[i])
    92  			alpha = bits.RotateLeft32(alpha, s) + ee
    93  			beta = bits.RotateLeft32(cc, 10)
    94  			aa, bb, cc, dd, ee = ee, alpha, bb, beta, dd
    95  
    96  			i++
    97  		}
    98  
    99  		// round 3
   100  		for i < 48 {
   101  			alpha = a + (b | ^c ^ d) + x[_n[i]] + 0x6ed9eba1
   102  			s := int(_r[i])
   103  			alpha = bits.RotateLeft32(alpha, s) + e
   104  			beta = bits.RotateLeft32(c, 10)
   105  			a, b, c, d, e = e, alpha, b, beta, d
   106  
   107  			// parallel line
   108  			alpha = aa + (bb | ^cc ^ dd) + x[n_[i]] + 0x6d703ef3
   109  			s = int(r_[i])
   110  			alpha = bits.RotateLeft32(alpha, s) + ee
   111  			beta = bits.RotateLeft32(cc, 10)
   112  			aa, bb, cc, dd, ee = ee, alpha, bb, beta, dd
   113  
   114  			i++
   115  		}
   116  
   117  		// round 4
   118  		for i < 64 {
   119  			alpha = a + (b&d | c&^d) + x[_n[i]] + 0x8f1bbcdc
   120  			s := int(_r[i])
   121  			alpha = bits.RotateLeft32(alpha, s) + e
   122  			beta = bits.RotateLeft32(c, 10)
   123  			a, b, c, d, e = e, alpha, b, beta, d
   124  
   125  			// parallel line
   126  			alpha = aa + (bb&cc | ^bb&dd) + x[n_[i]] + 0x7a6d76e9
   127  			s = int(r_[i])
   128  			alpha = bits.RotateLeft32(alpha, s) + ee
   129  			beta = bits.RotateLeft32(cc, 10)
   130  			aa, bb, cc, dd, ee = ee, alpha, bb, beta, dd
   131  
   132  			i++
   133  		}
   134  
   135  		// round 5
   136  		for i < 80 {
   137  			alpha = a + (b ^ (c | ^d)) + x[_n[i]] + 0xa953fd4e
   138  			s := int(_r[i])
   139  			alpha = bits.RotateLeft32(alpha, s) + e
   140  			beta = bits.RotateLeft32(c, 10)
   141  			a, b, c, d, e = e, alpha, b, beta, d
   142  
   143  			// parallel line
   144  			alpha = aa + (bb ^ cc ^ dd) + x[n_[i]]
   145  			s = int(r_[i])
   146  			alpha = bits.RotateLeft32(alpha, s) + ee
   147  			beta = bits.RotateLeft32(cc, 10)
   148  			aa, bb, cc, dd, ee = ee, alpha, bb, beta, dd
   149  
   150  			i++
   151  		}
   152  
   153  		// combine results
   154  		dd += c + md.s[1]
   155  		md.s[1] = md.s[2] + d + ee
   156  		md.s[2] = md.s[3] + e + aa
   157  		md.s[3] = md.s[4] + a + bb
   158  		md.s[4] = md.s[0] + b + cc
   159  		md.s[0] = dd
   160  
   161  		p = p[BlockSize:]
   162  		n += BlockSize
   163  	}
   164  	return n
   165  }
   166  

View as plain text