refactor user defined keypad

This commit is contained in:
2024-08-24 21:02:50 -05:00
parent 1a7dc45ab9
commit 3bf2b4d71f
19 changed files with 273 additions and 190 deletions

View File

@@ -3,7 +3,6 @@ package nkode
import (
"crypto/sha256"
"errors"
"fmt"
m "go-nkode/core/model"
"go-nkode/util"
"golang.org/x/crypto/bcrypt"
@@ -16,14 +15,15 @@ type UserCipherKeys struct {
MaskKey []uint64
Salt []byte
MaxNKodeLen int
kp *m.KeypadDimension
}
func NewUserCipherKeys(keypadSize m.KeypadSize, setVals []uint64, maxNKodeLen int) (*UserCipherKeys, error) {
if len(setVals) != keypadSize.AttrsPerKey {
return nil, errors.New(fmt.Sprintf("setVals len != attrsPerKey, %d, %d", len(setVals), keypadSize.AttrsPerKey))
func NewUserCipherKeys(kp *m.KeypadDimension, setVals []uint64, maxNKodeLen int) (*UserCipherKeys, error) {
err := kp.IsValidKeypadDimension()
if err != nil {
return nil, err
}
setKey, err := util.GenerateRandomNonRepeatingUint64(keypadSize.AttrsPerKey)
setKey, err := util.GenerateRandomNonRepeatingUint64(kp.AttrsPerKey)
if err != nil {
return nil, err
}
@@ -32,7 +32,7 @@ func NewUserCipherKeys(keypadSize m.KeypadSize, setVals []uint64, maxNKodeLen in
return nil, err
}
alphakey, _ := util.GenerateRandomNonRepeatingUint64(keypadSize.TotalAttrs())
alphakey, _ := util.GenerateRandomNonRepeatingUint64(kp.TotalAttrs())
passKey, _ := util.GenerateRandomNonRepeatingUint64(maxNKodeLen)
maskKey, _ := util.GenerateRandomNonRepeatingUint64(maxNKodeLen)
salt, _ := util.RandomBytes(10)
@@ -43,6 +43,7 @@ func NewUserCipherKeys(keypadSize m.KeypadSize, setVals []uint64, maxNKodeLen in
SetKey: setKey,
Salt: salt,
MaxNKodeLen: maxNKodeLen,
kp: kp,
}
return &userCipherKeys, nil
}
@@ -59,9 +60,9 @@ func (u *UserCipherKeys) PadUserMask(userMask []uint64, setVals []uint64) ([]uin
return paddedUserMask, nil
}
func (u *UserCipherKeys) ValidPassword(hashedPassword string, passcodeAttrIdx []int, customerAttrs CustomerAttributes) error {
func (u *UserCipherKeys) ValidPassword(hashedPassword string, passcodeAttrIdx []int, attrVals []uint64) error {
hashBytes := []byte(hashedPassword)
passcodeCipher := u.encipherCode(passcodeAttrIdx, customerAttrs)
passcodeCipher := u.encipherCode(passcodeAttrIdx, attrVals)
passwordDigest, err := u.saltAndDigest(passcodeCipher)
if err != nil {
return err
@@ -73,8 +74,8 @@ func (u *UserCipherKeys) ValidPassword(hashedPassword string, passcodeAttrIdx []
return nil
}
func (u *UserCipherKeys) EncipherSaltHashCode(passcodeAttrIdx []int, customerAttrs CustomerAttributes) (string, error) {
passcodeCipher := u.encipherCode(passcodeAttrIdx, customerAttrs)
func (u *UserCipherKeys) EncipherSaltHashCode(passcodeAttrIdx []int, attrVals []uint64) (string, error) {
passcodeCipher := u.encipherCode(passcodeAttrIdx, attrVals)
passcodeDigest, err := u.saltAndDigest(passcodeCipher)
if err != nil {
@@ -87,7 +88,7 @@ func (u *UserCipherKeys) EncipherSaltHashCode(passcodeAttrIdx []int, customerAtt
return string(passcodeBytes), nil
}
func (u *UserCipherKeys) encipherCode(passcodeAttrIdx []int, customerAttrs CustomerAttributes) []uint64 {
func (u *UserCipherKeys) encipherCode(passcodeAttrIdx []int, attrVals []uint64) []uint64 {
passcodeLen := len(passcodeAttrIdx)
passcodeCipher := make([]uint64, u.MaxNKodeLen)
@@ -95,7 +96,7 @@ func (u *UserCipherKeys) encipherCode(passcodeAttrIdx []int, customerAttrs Custo
for idx := 0; idx < passcodeLen; idx++ {
attrIdx := passcodeAttrIdx[idx]
alpha := u.AlphaKey[attrIdx]
attrVal := customerAttrs.AttrVals[idx]
attrVal := attrVals[idx]
pass := u.PassKey[idx]
passcodeCipher[idx] = alpha ^ pass ^ attrVal
}
@@ -122,8 +123,12 @@ func (u *UserCipherKeys) hashPasscode(passcodeDigest []byte) ([]byte, error) {
}
return hashedPassword, nil
}
func (u *UserCipherKeys) EncipherMask(passcodeSet []uint64, customerAttrs CustomerAttributes) (string, error) {
paddedPasscodeSets, err := u.PadUserMask(passcodeSet, customerAttrs.SetVals)
func (u *UserCipherKeys) EncipherMask(passcodeSet []uint64, customerAttrs CustomerAttributes, userKp m.KeypadDimension) (string, error) {
setVals, err := customerAttrs.SetVals(userKp)
if err != nil {
return "", err
}
paddedPasscodeSets, err := u.PadUserMask(passcodeSet, setVals)
if err != nil {
return "", err
}
@@ -166,20 +171,21 @@ func (u *UserCipherKeys) DecipherMask(mask string, setVals []uint64, passcodeLen
}
func (u *UserCipherKeys) EncipherNKode(passcodeAttrIdx []int, customerAttrs CustomerAttributes) (*m.EncipheredNKode, error) {
code, err := u.EncipherSaltHashCode(passcodeAttrIdx, customerAttrs)
attrVals, err := customerAttrs.AttrVals(*u.kp)
code, err := u.EncipherSaltHashCode(passcodeAttrIdx, attrVals)
if err != nil {
return nil, err
}
passcodeSet := make([]uint64, len(passcodeAttrIdx))
for idx := range passcodeSet {
passcodeAttr := customerAttrs.AttrVals[passcodeAttrIdx[idx]]
passcodeSet[idx], err = customerAttrs.GetAttrSetVal(passcodeAttr)
passcodeAttr := attrVals[passcodeAttrIdx[idx]]
passcodeSet[idx], err = customerAttrs.GetAttrSetVal(passcodeAttr, *u.kp)
if err != nil {
return nil, err
}
}
mask, err := u.EncipherMask(passcodeSet, customerAttrs)
mask, err := u.EncipherMask(passcodeSet, customerAttrs, *u.kp)
encipheredCode := m.EncipheredNKode{
Code: code,
Mask: mask,