github.com/icodeface/tls@v0.0.0-20230910023335-34df9250cd12/internal/x/crypto/poly1305/poly1305.go (about)

     1  // Copyright 2012 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  /*
     6  Package poly1305 implements Poly1305 one-time message authentication code as
     7  specified in https://cr.yp.to/mac/poly1305-20050329.pdf.
     8  
     9  Poly1305 is a fast, one-time authentication function. It is infeasible for an
    10  attacker to generate an authenticator for a message without the key. However, a
    11  key must only be used for a single message. Authenticating two different
    12  messages with the same key allows an attacker to forge authenticators for other
    13  messages with the same key.
    14  
    15  Poly1305 was originally coupled with AES in order to make Poly1305-AES. AES was
    16  used with a fixed key in order to generate one-time keys from an nonce.
    17  However, in this package AES isn't used and the one-time key is specified
    18  directly.
    19  */
    20  package poly1305
    21  
    22  import "crypto/subtle"
    23  
    24  // TagSize is the size, in bytes, of a poly1305 authenticator.
    25  const TagSize = 16
    26  
    27  // Verify returns true if mac is a valid authenticator for m with the given
    28  // key.
    29  func Verify(mac *[16]byte, m []byte, key *[32]byte) bool {
    30  	var tmp [16]byte
    31  	Sum(&tmp, m, key)
    32  	return subtle.ConstantTimeCompare(tmp[:], mac[:]) == 1
    33  }