1// Copyright 2021 Google LLC 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// http://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//////////////////////////////////////////////////////////////////////////////// 16 17package subtle 18 19import ( 20 "crypto/rand" 21 22 "golang.org/x/crypto/curve25519" 23) 24 25// GeneratePrivateKeyX25519 generates a new 32-byte private key. 26func GeneratePrivateKeyX25519() ([]byte, error) { 27 privKey := make([]byte, curve25519.ScalarSize) 28 _, err := rand.Read(privKey) 29 return privKey, err 30} 31 32// ComputeSharedSecretX25519 returns the 32-byte shared key, i.e. 33// privKey * pubValue on the curve. 34func ComputeSharedSecretX25519(privKey, pubValue []byte) ([]byte, error) { 35 return curve25519.X25519(privKey, pubValue) 36} 37 38// PublicFromPrivateX25519 computes privKey's corresponding public key. 39func PublicFromPrivateX25519(privKey []byte) ([]byte, error) { 40 return ComputeSharedSecretX25519(privKey, curve25519.Basepoint) 41} 42