new packages
This commit is contained in:
parent
aa5ccaded0
commit
4f4e320999
8 changed files with 83 additions and 70 deletions
75
crypto/crypto.go
Normal file
75
crypto/crypto.go
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
package crypto
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/aes"
|
||||
"crypto/cipher"
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"github.com/aead/cmac"
|
||||
)
|
||||
|
||||
func Create_k1() (string, error) {
|
||||
|
||||
// 16 bytes = 128 bits
|
||||
b := make([]byte, 16)
|
||||
_, err := rand.Read(b)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
str := hex.EncodeToString(b)
|
||||
|
||||
return str, nil
|
||||
}
|
||||
|
||||
// decrypt p with aes_dec
|
||||
func Aes_decrypt(key_sdm_file_read []byte, ba_p []byte) ([]byte, error) {
|
||||
|
||||
dec_p := make([]byte, 16)
|
||||
iv := make([]byte, 16)
|
||||
c1, err := aes.NewCipher(key_sdm_file_read)
|
||||
if err != nil {
|
||||
return dec_p, err
|
||||
}
|
||||
mode := cipher.NewCBCDecrypter(c1, iv)
|
||||
mode.CryptBlocks(dec_p, ba_p)
|
||||
|
||||
return dec_p, nil
|
||||
}
|
||||
|
||||
func Aes_cmac(key_sdm_file_read_mac []byte, sv2 []byte, ba_c []byte) (bool, error) {
|
||||
|
||||
c2, err := aes.NewCipher(key_sdm_file_read_mac)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
ks, err := cmac.Sum(sv2, c2, 16)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
c3, err := aes.NewCipher(ks)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
cm, err := cmac.Sum([]byte{}, c3, 16)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
ct := make([]byte, 8)
|
||||
ct[0] = cm[1]
|
||||
ct[1] = cm[3]
|
||||
ct[2] = cm[5]
|
||||
ct[3] = cm[7]
|
||||
ct[4] = cm[9]
|
||||
ct[5] = cm[11]
|
||||
ct[6] = cm[13]
|
||||
ct[7] = cm[15]
|
||||
|
||||
res_cmac := bytes.Compare(ct, ba_c)
|
||||
if res_cmac != 0 {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
return true, nil
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue