|
| 1 | +// Copyright 2019 Intel Corporation. |
| 2 | +// Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +// Only IPv4, Only tunnel, Only ESP, Only AES-128-CBC |
| 6 | +package ipsec |
| 7 | + |
| 8 | +import "github.com/intel-go/nff-go/examples/ipsec/crypto_mb" |
| 9 | + |
| 10 | +import "crypto/aes" |
| 11 | +import "crypto/cipher" |
| 12 | +import "crypto/hmac" |
| 13 | +import "crypto/sha1" |
| 14 | +import "hash" |
| 15 | + |
| 16 | +const VECTOR = 8 |
| 17 | + |
| 18 | +type SContext struct { |
| 19 | + mac123 hash.Hash |
| 20 | + modeEnc cipher.BlockMode |
| 21 | + modeDec cipher.BlockMode |
| 22 | +} |
| 23 | + |
| 24 | +type VContext struct { |
| 25 | + mac123 crypto_mb.MultiHash |
| 26 | + modeEnc crypto_mb.MultiBlockMode |
| 27 | + modeDec crypto_mb.MultiBlockMode |
| 28 | + |
| 29 | + vectorEncryptionPart [][]byte |
| 30 | + vectorIV [][]byte |
| 31 | + vectorAuthPart [][]byte |
| 32 | + vectorAuthPlace [][]byte |
| 33 | + |
| 34 | + s SContext |
| 35 | +} |
| 36 | + |
| 37 | +type SetIVerM interface { |
| 38 | + SetIV([][]byte) |
| 39 | +} |
| 40 | + |
| 41 | +type SetIVer interface { |
| 42 | + SetIV([]byte) |
| 43 | +} |
| 44 | + |
| 45 | +func InitSContext() interface{} { |
| 46 | + var auth123Key = []byte("qqqqqqqqqqqqqqqqqqqq") |
| 47 | + var crypt123Key = []byte("AES128Key-16Char") |
| 48 | + block123, _ := aes.NewCipher(crypt123Key) |
| 49 | + |
| 50 | + tempScalarIV := make([]byte, 16) |
| 51 | + |
| 52 | + n := new(SContext) |
| 53 | + n.mac123 = hmac.New(sha1.New, auth123Key) |
| 54 | + n.modeEnc = cipher.NewCBCEncrypter(block123, tempScalarIV) |
| 55 | + n.modeDec = cipher.NewCBCDecrypter(block123, tempScalarIV) |
| 56 | + return n |
| 57 | +} |
| 58 | + |
| 59 | +func InitVContext() interface{} { |
| 60 | + var auth123Key = []byte("qqqqqqqqqqqqqqqqqqqq") |
| 61 | + var crypt123Key = []byte("AES128Key-16Char") |
| 62 | + block123 := crypto_mb.NewAESMultiBlock(crypt123Key) |
| 63 | + |
| 64 | + tempVectorIV := make([][]byte, VECTOR, VECTOR) |
| 65 | + for i := 0; i < VECTOR; i++ { |
| 66 | + tempVectorIV[i] = make([]byte, 16) |
| 67 | + } |
| 68 | + |
| 69 | + n := new(VContext) |
| 70 | + n.mac123 = crypto_mb.NewHmac(crypto_mb.New, auth123Key) |
| 71 | + n.modeEnc = crypto_mb.NewMultiCBCEncrypter(block123, tempVectorIV) |
| 72 | + n.modeDec = crypto_mb.NewMultiCBCDecrypter(block123, tempVectorIV) |
| 73 | + n.vectorEncryptionPart = make([][]byte, VECTOR, VECTOR) |
| 74 | + n.vectorIV = make([][]byte, VECTOR, VECTOR) |
| 75 | + n.vectorAuthPart = make([][]byte, VECTOR, VECTOR) |
| 76 | + n.vectorAuthPlace = make([][]byte, VECTOR, VECTOR) |
| 77 | + n.s = *InitSContext().(*SContext) |
| 78 | + return n |
| 79 | +} |
| 80 | + |
| 81 | +func (c SContext) Copy() interface{} { |
| 82 | + return InitSContext() |
| 83 | +} |
| 84 | + |
| 85 | +func (c VContext) Copy() interface{} { |
| 86 | + return InitVContext() |
| 87 | +} |
| 88 | + |
| 89 | +func (c SContext) Delete() { |
| 90 | +} |
| 91 | + |
| 92 | +func (c VContext) Delete() { |
| 93 | +} |
| 94 | + |
| 95 | +func Encrypt(EncryptionPart [][]byte, where [][]byte, IV [][]byte, Z uint, context *VContext) { |
| 96 | + if Z != VECTOR { |
| 97 | + for t := uint(0); t < Z; t++ { |
| 98 | + context.s.modeEnc.(SetIVer).SetIV(IV[t]) |
| 99 | + context.s.modeEnc.CryptBlocks(EncryptionPart[t], where[t]) |
| 100 | + } |
| 101 | + } else { |
| 102 | + context.modeEnc.(SetIVerM).SetIV(IV[:]) |
| 103 | + context.modeEnc.CryptManyBlocks(EncryptionPart, where) |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +func Authenticate(AuthenticationPart [][]byte, where [][]byte, Z uint, context *VContext) { |
| 108 | + if Z != VECTOR { |
| 109 | + for t := uint(0); t < Z; t++ { |
| 110 | + context.s.mac123.Reset() |
| 111 | + context.s.mac123.Write(where[t]) |
| 112 | + copy(where[t], context.s.mac123.Sum(nil)) |
| 113 | + } |
| 114 | + } else { |
| 115 | + context.mac123.Reset() |
| 116 | + context.mac123.Write(context.vectorAuthPart) |
| 117 | + temp := context.mac123.Sum(nil) |
| 118 | + for t := uint(0); t < VECTOR; t++ { |
| 119 | + copy(where[t], temp[t]) |
| 120 | + } |
| 121 | + } |
| 122 | +} |
0 commit comments