| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
|
|
| |
|
|
| package nistec |
|
|
| import ( |
| "crypto/internal/fips140deps/byteorder" |
| "errors" |
| "math/bits" |
| "runtime" |
| "unsafe" |
| ) |
|
|
| |
| |
| type p256Element [4]uint64 |
|
|
| |
| var p256One = p256Element{0x0000000000000001, 0xffffffff00000000, |
| 0xffffffffffffffff, 0x00000000fffffffe} |
|
|
| var p256Zero = p256Element{} |
|
|
| |
| var p256P = p256Element{0xffffffffffffffff, 0x00000000ffffffff, |
| 0x0000000000000000, 0xffffffff00000001} |
|
|
| |
| |
| type P256Point struct { |
| |
| |
| x, y, z p256Element |
| } |
|
|
| |
| func NewP256Point() *P256Point { |
| return &P256Point{ |
| x: p256One, y: p256One, z: p256Zero, |
| } |
| } |
|
|
| |
| func (p *P256Point) SetGenerator() *P256Point { |
| p.x = p256Element{0x79e730d418a9143c, 0x75ba95fc5fedb601, |
| 0x79fb732b77622510, 0x18905f76a53755c6} |
| p.y = p256Element{0xddf25357ce95560a, 0x8b4ab8e4ba19e45c, |
| 0xd2e88688dd21f325, 0x8571ff1825885d85} |
| p.z = p256One |
| return p |
| } |
|
|
| |
| func (p *P256Point) Set(q *P256Point) *P256Point { |
| p.x, p.y, p.z = q.x, q.y, q.z |
| return p |
| } |
|
|
| const p256ElementLength = 32 |
| const p256UncompressedLength = 1 + 2*p256ElementLength |
| const p256CompressedLength = 1 + p256ElementLength |
|
|
| |
| |
| |
| |
| func (p *P256Point) SetBytes(b []byte) (*P256Point, error) { |
| |
| |
| |
| rr := p256Element{0x0000000000000003, 0xfffffffbffffffff, |
| 0xfffffffffffffffe, 0x00000004fffffffd} |
|
|
| switch { |
| |
| case len(b) == 1 && b[0] == 0: |
| return p.Set(NewP256Point()), nil |
|
|
| |
| case len(b) == p256UncompressedLength && b[0] == 4: |
| var r P256Point |
| p256BigToLittle(&r.x, (*[32]byte)(b[1:33])) |
| p256BigToLittle(&r.y, (*[32]byte)(b[33:65])) |
| if p256LessThanP(&r.x) == 0 || p256LessThanP(&r.y) == 0 { |
| return nil, errors.New("invalid P256 element encoding") |
| } |
| p256Mul(&r.x, &r.x, &rr) |
| p256Mul(&r.y, &r.y, &rr) |
| if err := p256CheckOnCurve(&r.x, &r.y); err != nil { |
| return nil, err |
| } |
| r.z = p256One |
| return p.Set(&r), nil |
|
|
| |
| case len(b) == p256CompressedLength && (b[0] == 2 || b[0] == 3): |
| var r P256Point |
| p256BigToLittle(&r.x, (*[32]byte)(b[1:33])) |
| if p256LessThanP(&r.x) == 0 { |
| return nil, errors.New("invalid P256 element encoding") |
| } |
| p256Mul(&r.x, &r.x, &rr) |
|
|
| |
| p256Polynomial(&r.y, &r.x) |
| if !p256Sqrt(&r.y, &r.y) { |
| return nil, errors.New("invalid P256 compressed point encoding") |
| } |
|
|
| |
| |
| yy := new(p256Element) |
| p256FromMont(yy, &r.y) |
| cond := int(yy[0]&1) ^ int(b[0]&1) |
| p256NegCond(&r.y, cond) |
|
|
| r.z = p256One |
| return p.Set(&r), nil |
|
|
| default: |
| return nil, errors.New("invalid P256 point encoding") |
| } |
| } |
|
|
| |
| func p256Polynomial(y2, x *p256Element) *p256Element { |
| x3 := new(p256Element) |
| p256Sqr(x3, x, 1) |
| p256Mul(x3, x3, x) |
|
|
| threeX := new(p256Element) |
| p256Add(threeX, x, x) |
| p256Add(threeX, threeX, x) |
| p256NegCond(threeX, 1) |
|
|
| p256B := &p256Element{0xd89cdf6229c4bddf, 0xacf005cd78843090, |
| 0xe5a220abf7212ed6, 0xdc30061d04874834} |
|
|
| p256Add(x3, x3, threeX) |
| p256Add(x3, x3, p256B) |
|
|
| *y2 = *x3 |
| return y2 |
| } |
|
|
| func p256CheckOnCurve(x, y *p256Element) error { |
| |
| rhs := p256Polynomial(new(p256Element), x) |
| lhs := new(p256Element) |
| p256Sqr(lhs, y, 1) |
| if p256Equal(lhs, rhs) != 1 { |
| return errors.New("P256 point not on curve") |
| } |
| return nil |
| } |
|
|
| |
| |
| |
| func p256LessThanP(x *p256Element) int { |
| var b uint64 |
| _, b = bits.Sub64(x[0], p256P[0], b) |
| _, b = bits.Sub64(x[1], p256P[1], b) |
| _, b = bits.Sub64(x[2], p256P[2], b) |
| _, b = bits.Sub64(x[3], p256P[3], b) |
| return int(b) |
| } |
|
|
| func p256BigToLittle(l *p256Element, b *[32]byte) { |
| bytesToLimbs((*[4]uint64)(l), b) |
| } |
|
|
| func bytesToLimbs(l *[4]uint64, b *[32]byte) { |
| l[0] = byteorder.BEUint64(b[24:]) |
| l[1] = byteorder.BEUint64(b[16:]) |
| l[2] = byteorder.BEUint64(b[8:]) |
| l[3] = byteorder.BEUint64(b[:]) |
| } |
|
|
| func p256LittleToBig(b *[32]byte, l *p256Element) { |
| limbsToBytes(b, (*[4]uint64)(l)) |
| } |
|
|
| func limbsToBytes(b *[32]byte, l *[4]uint64) { |
| byteorder.BEPutUint64(b[24:], l[0]) |
| byteorder.BEPutUint64(b[16:], l[1]) |
| byteorder.BEPutUint64(b[8:], l[2]) |
| byteorder.BEPutUint64(b[:], l[3]) |
| } |
|
|
| |
| func p256Add(res, x, y *p256Element) { |
| var c, b uint64 |
| t1 := make([]uint64, 4) |
| t1[0], c = bits.Add64(x[0], y[0], 0) |
| t1[1], c = bits.Add64(x[1], y[1], c) |
| t1[2], c = bits.Add64(x[2], y[2], c) |
| t1[3], c = bits.Add64(x[3], y[3], c) |
| t2 := make([]uint64, 4) |
| t2[0], b = bits.Sub64(t1[0], p256P[0], 0) |
| t2[1], b = bits.Sub64(t1[1], p256P[1], b) |
| t2[2], b = bits.Sub64(t1[2], p256P[2], b) |
| t2[3], b = bits.Sub64(t1[3], p256P[3], b) |
| |
| |
| |
| |
| |
| |
| |
| t2Mask := (c ^ b) - 1 |
| res[0] = (t1[0] & ^t2Mask) | (t2[0] & t2Mask) |
| res[1] = (t1[1] & ^t2Mask) | (t2[1] & t2Mask) |
| res[2] = (t1[2] & ^t2Mask) | (t2[2] & t2Mask) |
| res[3] = (t1[3] & ^t2Mask) | (t2[3] & t2Mask) |
| } |
|
|
| |
| |
| func p256Sqrt(e, x *p256Element) (isSquare bool) { |
| t0, t1 := new(p256Element), new(p256Element) |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| p256Sqr(t0, x, 1) |
| p256Mul(t0, x, t0) |
| p256Sqr(t1, t0, 2) |
| p256Mul(t0, t0, t1) |
| p256Sqr(t1, t0, 4) |
| p256Mul(t0, t0, t1) |
| p256Sqr(t1, t0, 8) |
| p256Mul(t0, t0, t1) |
| p256Sqr(t1, t0, 16) |
| p256Mul(t0, t0, t1) |
| p256Sqr(t0, t0, 32) |
| p256Mul(t0, x, t0) |
| p256Sqr(t0, t0, 96) |
| p256Mul(t0, x, t0) |
| p256Sqr(t0, t0, 94) |
|
|
| p256Sqr(t1, t0, 1) |
| if p256Equal(t1, x) != 1 { |
| return false |
| } |
| *e = *t0 |
| return true |
| } |
|
|
| |
|
|
| |
| |
| |
| func p256Mul(res, in1, in2 *p256Element) |
|
|
| |
| |
| |
| func p256Sqr(res, in *p256Element, n int) |
|
|
| |
| |
| |
| |
| func p256FromMont(res, in *p256Element) |
|
|
| |
| |
| |
| func p256NegCond(val *p256Element, cond int) |
|
|
| |
| |
| |
| func p256MovCond(res, a, b *P256Point, cond int) |
|
|
| |
| |
| |
| type p256Table [16]P256Point |
|
|
| |
| |
| |
| |
| func p256Select(res *P256Point, table *p256Table, idx int) |
|
|
| |
| |
| type p256AffinePoint struct { |
| x, y p256Element |
| } |
|
|
| |
| |
| type p256AffineTable [32]p256AffinePoint |
|
|
| |
| |
| |
| |
| |
| |
| |
| var p256Precomputed *[43]p256AffineTable |
|
|
| func init() { |
| p256PrecomputedPtr := unsafe.Pointer(&p256PrecomputedEmbed) |
| if runtime.GOARCH == "s390x" { |
| var newTable [43 * 32 * 2 * 4]uint64 |
| for i, x := range (*[43 * 32 * 2 * 4][8]byte)(p256PrecomputedPtr) { |
| newTable[i] = byteorder.LEUint64(x[:]) |
| } |
| p256PrecomputedPtr = unsafe.Pointer(&newTable) |
| } |
| p256Precomputed = (*[43]p256AffineTable)(p256PrecomputedPtr) |
| } |
|
|
| |
| |
| |
| |
| func p256SelectAffine(res *p256AffinePoint, table *p256AffineTable, idx int) |
|
|
| |
| |
| |
| |
| |
| func p256PointAddAffineAsm(res, in1 *P256Point, in2 *p256AffinePoint, sign, sel, zero int) |
|
|
| |
| |
| |
| |
| |
| func p256PointAddAsm(res, in1, in2 *P256Point) int |
|
|
| |
| |
| |
| func p256PointDoubleAsm(res, in *P256Point) |
|
|
| |
| |
| type p256OrdElement [4]uint64 |
|
|
| |
| func p256OrdReduce(s *p256OrdElement) { |
| |
| |
| t0, b := bits.Sub64(s[0], 0xf3b9cac2fc632551, 0) |
| t1, b := bits.Sub64(s[1], 0xbce6faada7179e84, b) |
| t2, b := bits.Sub64(s[2], 0xffffffffffffffff, b) |
| t3, b := bits.Sub64(s[3], 0xffffffff00000000, b) |
| tMask := b - 1 |
| s[0] ^= (t0 ^ s[0]) & tMask |
| s[1] ^= (t1 ^ s[1]) & tMask |
| s[2] ^= (t2 ^ s[2]) & tMask |
| s[3] ^= (t3 ^ s[3]) & tMask |
| } |
|
|
| func p256OrdLittleToBig(b *[32]byte, l *p256OrdElement) { |
| limbsToBytes(b, (*[4]uint64)(l)) |
| } |
|
|
| func p256OrdBigToLittle(l *p256OrdElement, b *[32]byte) { |
| bytesToLimbs((*[4]uint64)(l), b) |
| } |
|
|
| |
| func (q *P256Point) Add(r1, r2 *P256Point) *P256Point { |
| var sum, double P256Point |
| r1IsInfinity := r1.isInfinity() |
| r2IsInfinity := r2.isInfinity() |
| pointsEqual := p256PointAddAsm(&sum, r1, r2) |
| p256PointDoubleAsm(&double, r1) |
| p256MovCond(&sum, &double, &sum, pointsEqual) |
| p256MovCond(&sum, r1, &sum, r2IsInfinity) |
| p256MovCond(&sum, r2, &sum, r1IsInfinity) |
| return q.Set(&sum) |
| } |
|
|
| |
| func (q *P256Point) Double(p *P256Point) *P256Point { |
| var double P256Point |
| p256PointDoubleAsm(&double, p) |
| return q.Set(&double) |
| } |
|
|
| |
| |
| |
| func (r *P256Point) ScalarBaseMult(scalar []byte) (*P256Point, error) { |
| if len(scalar) != 32 { |
| return nil, errors.New("invalid scalar length") |
| } |
| scalarReversed := new(p256OrdElement) |
| p256OrdBigToLittle(scalarReversed, (*[32]byte)(scalar)) |
| p256OrdReduce(scalarReversed) |
|
|
| r.p256BaseMult(scalarReversed) |
| return r, nil |
| } |
|
|
| |
| |
| |
| func (r *P256Point) ScalarMult(q *P256Point, scalar []byte) (*P256Point, error) { |
| if len(scalar) != 32 { |
| return nil, errors.New("invalid scalar length") |
| } |
| scalarReversed := new(p256OrdElement) |
| p256OrdBigToLittle(scalarReversed, (*[32]byte)(scalar)) |
| p256OrdReduce(scalarReversed) |
|
|
| r.Set(q).p256ScalarMult(scalarReversed) |
| return r, nil |
| } |
|
|
| |
| func uint64IsZero(x uint64) int { |
| x = ^x |
| x &= x >> 32 |
| x &= x >> 16 |
| x &= x >> 8 |
| x &= x >> 4 |
| x &= x >> 2 |
| x &= x >> 1 |
| return int(x & 1) |
| } |
|
|
| |
| func p256Equal(a, b *p256Element) int { |
| var acc uint64 |
| for i := range a { |
| acc |= a[i] ^ b[i] |
| } |
| return uint64IsZero(acc) |
| } |
|
|
| |
| func (p *P256Point) isInfinity() int { |
| return p256Equal(&p.z, &p256Zero) |
| } |
|
|
| |
| |
| |
| func (p *P256Point) Bytes() []byte { |
| |
| |
| var out [p256UncompressedLength]byte |
| return p.bytes(&out) |
| } |
|
|
| func (p *P256Point) bytes(out *[p256UncompressedLength]byte) []byte { |
| |
| if p.isInfinity() == 1 { |
| return append(out[:0], 0) |
| } |
|
|
| x, y := new(p256Element), new(p256Element) |
| p.affineFromMont(x, y) |
|
|
| out[0] = 4 |
| p256LittleToBig((*[32]byte)(out[1:33]), x) |
| p256LittleToBig((*[32]byte)(out[33:65]), y) |
|
|
| return out[:] |
| } |
|
|
| |
| |
| func (p *P256Point) affineFromMont(x, y *p256Element) { |
| p256Inverse(y, &p.z) |
| p256Sqr(x, y, 1) |
| p256Mul(y, y, x) |
|
|
| p256Mul(x, &p.x, x) |
| p256Mul(y, &p.y, y) |
|
|
| p256FromMont(x, x) |
| p256FromMont(y, y) |
| } |
|
|
| |
| |
| func (p *P256Point) BytesX() ([]byte, error) { |
| |
| |
| var out [p256ElementLength]byte |
| return p.bytesX(&out) |
| } |
|
|
| func (p *P256Point) bytesX(out *[p256ElementLength]byte) ([]byte, error) { |
| if p.isInfinity() == 1 { |
| return nil, errors.New("P256 point is the point at infinity") |
| } |
|
|
| x := new(p256Element) |
| p256Inverse(x, &p.z) |
| p256Sqr(x, x, 1) |
| p256Mul(x, &p.x, x) |
| p256FromMont(x, x) |
| p256LittleToBig((*[32]byte)(out[:]), x) |
|
|
| return out[:], nil |
| } |
|
|
| |
| |
| |
| func (p *P256Point) BytesCompressed() []byte { |
| |
| |
| var out [p256CompressedLength]byte |
| return p.bytesCompressed(&out) |
| } |
|
|
| func (p *P256Point) bytesCompressed(out *[p256CompressedLength]byte) []byte { |
| if p.isInfinity() == 1 { |
| return append(out[:0], 0) |
| } |
|
|
| x, y := new(p256Element), new(p256Element) |
| p.affineFromMont(x, y) |
|
|
| out[0] = 2 | byte(y[0]&1) |
| p256LittleToBig((*[32]byte)(out[1:33]), x) |
|
|
| return out[:] |
| } |
|
|
| |
| func (q *P256Point) Select(p1, p2 *P256Point, cond int) *P256Point { |
| p256MovCond(q, p1, p2, cond) |
| return q |
| } |
|
|
| |
| func p256Inverse(out, in *p256Element) { |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| var z = new(p256Element) |
| var t0 = new(p256Element) |
| var t1 = new(p256Element) |
|
|
| p256Sqr(z, in, 1) |
| p256Mul(z, in, z) |
| p256Sqr(z, z, 1) |
| p256Mul(z, in, z) |
| p256Sqr(t0, z, 3) |
| p256Mul(t0, z, t0) |
| p256Sqr(t1, t0, 6) |
| p256Mul(t0, t0, t1) |
| p256Sqr(t0, t0, 3) |
| p256Mul(z, z, t0) |
| p256Sqr(t0, z, 1) |
| p256Mul(t0, in, t0) |
| p256Sqr(t1, t0, 16) |
| p256Mul(t0, t0, t1) |
| p256Sqr(t0, t0, 15) |
| p256Mul(z, z, t0) |
| p256Sqr(t0, t0, 17) |
| p256Mul(t0, in, t0) |
| p256Sqr(t0, t0, 143) |
| p256Mul(t0, z, t0) |
| p256Sqr(t0, t0, 47) |
| p256Mul(z, z, t0) |
| p256Sqr(z, z, 2) |
| p256Mul(out, in, z) |
| } |
|
|
| func boothW5(in uint) (int, int) { |
| var s uint = ^((in >> 5) - 1) |
| var d uint = (1 << 6) - in - 1 |
| d = (d & s) | (in & (^s)) |
| d = (d >> 1) + (d & 1) |
| return int(d), int(s & 1) |
| } |
|
|
| func boothW6(in uint) (int, int) { |
| var s uint = ^((in >> 6) - 1) |
| var d uint = (1 << 7) - in - 1 |
| d = (d & s) | (in & (^s)) |
| d = (d >> 1) + (d & 1) |
| return int(d), int(s & 1) |
| } |
|
|
| func (p *P256Point) p256BaseMult(scalar *p256OrdElement) { |
| var t0 p256AffinePoint |
|
|
| wvalue := (scalar[0] << 1) & 0x7f |
| sel, sign := boothW6(uint(wvalue)) |
| p256SelectAffine(&t0, &p256Precomputed[0], sel) |
| p.x, p.y, p.z = t0.x, t0.y, p256One |
| p256NegCond(&p.y, sign) |
|
|
| index := uint(5) |
| zero := sel |
|
|
| for i := 1; i < 43; i++ { |
| if index < 192 { |
| wvalue = ((scalar[index/64] >> (index % 64)) + (scalar[index/64+1] << (64 - (index % 64)))) & 0x7f |
| } else { |
| wvalue = (scalar[index/64] >> (index % 64)) & 0x7f |
| } |
| index += 6 |
| sel, sign = boothW6(uint(wvalue)) |
| p256SelectAffine(&t0, &p256Precomputed[i], sel) |
| p256PointAddAffineAsm(p, p, &t0, sign, sel, zero) |
| zero |= sel |
| } |
|
|
| |
| p256MovCond(p, p, NewP256Point(), zero) |
| } |
|
|
| func (p *P256Point) p256ScalarMult(scalar *p256OrdElement) { |
| |
| |
| var precomp p256Table |
| var t0, t1, t2, t3 P256Point |
|
|
| |
| precomp[0] = *p |
|
|
| p256PointDoubleAsm(&t0, p) |
| p256PointDoubleAsm(&t1, &t0) |
| p256PointDoubleAsm(&t2, &t1) |
| p256PointDoubleAsm(&t3, &t2) |
| precomp[1] = t0 |
| precomp[3] = t1 |
| precomp[7] = t2 |
| precomp[15] = t3 |
|
|
| p256PointAddAsm(&t0, &t0, p) |
| p256PointAddAsm(&t1, &t1, p) |
| p256PointAddAsm(&t2, &t2, p) |
| precomp[2] = t0 |
| precomp[4] = t1 |
| precomp[8] = t2 |
|
|
| p256PointDoubleAsm(&t0, &t0) |
| p256PointDoubleAsm(&t1, &t1) |
| precomp[5] = t0 |
| precomp[9] = t1 |
|
|
| p256PointAddAsm(&t2, &t0, p) |
| p256PointAddAsm(&t1, &t1, p) |
| precomp[6] = t2 |
| precomp[10] = t1 |
|
|
| p256PointDoubleAsm(&t0, &t0) |
| p256PointDoubleAsm(&t2, &t2) |
| precomp[11] = t0 |
| precomp[13] = t2 |
|
|
| p256PointAddAsm(&t0, &t0, p) |
| p256PointAddAsm(&t2, &t2, p) |
| precomp[12] = t0 |
| precomp[14] = t2 |
|
|
| |
| index := uint(254) |
| var sel, sign int |
|
|
| wvalue := (scalar[index/64] >> (index % 64)) & 0x3f |
| sel, _ = boothW5(uint(wvalue)) |
|
|
| p256Select(p, &precomp, sel) |
| zero := sel |
|
|
| for index > 4 { |
| index -= 5 |
| p256PointDoubleAsm(p, p) |
| p256PointDoubleAsm(p, p) |
| p256PointDoubleAsm(p, p) |
| p256PointDoubleAsm(p, p) |
| p256PointDoubleAsm(p, p) |
|
|
| if index < 192 { |
| wvalue = ((scalar[index/64] >> (index % 64)) + (scalar[index/64+1] << (64 - (index % 64)))) & 0x3f |
| } else { |
| wvalue = (scalar[index/64] >> (index % 64)) & 0x3f |
| } |
|
|
| sel, sign = boothW5(uint(wvalue)) |
|
|
| p256Select(&t0, &precomp, sel) |
| p256NegCond(&t0.y, sign) |
| p256PointAddAsm(&t1, p, &t0) |
| p256MovCond(&t1, &t1, p, sel) |
| p256MovCond(p, &t1, &t0, zero) |
| zero |= sel |
| } |
|
|
| p256PointDoubleAsm(p, p) |
| p256PointDoubleAsm(p, p) |
| p256PointDoubleAsm(p, p) |
| p256PointDoubleAsm(p, p) |
| p256PointDoubleAsm(p, p) |
|
|
| wvalue = (scalar[0] << 1) & 0x3f |
| sel, sign = boothW5(uint(wvalue)) |
|
|
| p256Select(&t0, &precomp, sel) |
| p256NegCond(&t0.y, sign) |
| p256PointAddAsm(&t1, p, &t0) |
| p256MovCond(&t1, &t1, p, sel) |
| p256MovCond(p, &t1, &t0, zero) |
| } |
|
|